-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgen.py
executable file
·67 lines (59 loc) · 1.76 KB
/
gen.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python3
import re
import glob
import os
import subprocess
for f in glob.glob("fw/*.bin"):
os.remove(f)
subprocess.run(
[
"bindgen",
"gen_wrapper.h",
"--output=fw/bindings.rs",
"--use-core",
"--ignore-functions",
"--default-enum-style=rust",
"--no-prepend-enum-name",
"--no-layout-tests",
"--",
"-I./sdk-nrf/drivers/wifi/nrf700x/osal/fw_if/umac_if/inc/fw/",
"-I./sdk-nrf/drivers/wifi/nrf700x/osal/hw_if/hal/inc/fw/",
],
check=True,
)
h = open("fw/bindings.rs").read()
h = re.sub("= (\d+);", lambda m: "= 0x{:x};".format(int(m[1])), h)
h = h.replace("pub enum", "#[derive(num_enum::TryFromPrimitive)] pub enum")
h = h.replace("NRF_WIFI_802", "IEEE_802")
h = h.replace("NRF_WIFI_", "")
h = h.replace("nrf_wifi_", "")
open("fw/bindings.rs", "w").write(h)
subprocess.run(
[
"rustfmt",
"--edition=2021",
"fw/bindings.rs",
],
check=True,
)
h = open(
"sdk-nrf/drivers/wifi/nrf700x/osal/fw_if/umac_if/inc/fw/rpu_fw_patches.h"
).read()
flavors = {}
flavors["_radiotest"] = re.search(
re.compile("#ifdef CONFIG_NRF700X_RADIO_TEST(.*)#else", re.MULTILINE | re.DOTALL), h
)[1]
flavors[""] = re.search(re.compile("#else(.*)#endif", re.MULTILINE | re.DOTALL), h)[1]
for suffix, code in flavors.items():
for fw in re.findall(
re.compile(
"const unsigned char __aligned\\(4\\)\\s+([a-z0-9_]+)\\[\\] = \\{([a-f0-9x, \t\r\n]+)\\}",
re.MULTILINE,
),
code,
):
name = fw[0].removeprefix("wifi_nrf_") + suffix + ".bin"
data = bytes.fromhex(
"".join(c for c in fw[1].replace("0x", "") if c in "0123456789abcdef")
)
open("fw/" + name, "wb").write(data)