-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgen_imgcfg.py
More file actions
executable file
·97 lines (80 loc) · 2.49 KB
/
Copy pathgen_imgcfg.py
File metadata and controls
executable file
·97 lines (80 loc) · 2.49 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env python3
# Generate genimage.cfg from partition JSON for Android builds
# Based on Spacemit Buildroot scripts/gen_imgcfg.py
import json
import sys
import os
def generate_genimage_cfg(name, partitions):
cfg_content = '''
# This is a genimage configuration file auto generated by gen_imgcfg.py.
# require genimage version 16
'''
cfg_content += 'image %s {\n' % name
cfg_content += ''' hdimage {
partition-table-type = gpt
}
'''
for partition in partitions:
part_name = partition.get("name", "")
src_size = partition.get("size", "")
hidden = partition.get("hidden", False)
visible = "true"
if hidden == True:
visible = "false"
if src_size == "-":
src_size = ""
offset = partition.get("offset", "")
image = partition.get("image", "")
holes = partition.get("holes", "{}")
cfg_content += f'''
partition {part_name} {{
image = "{image}"
offset = "{offset}"
size = "{src_size}"
holes = {holes}
in-partition-table = "{visible}"
}}
'''
cfg_content += '''
}
'''
return cfg_content
def main():
json_file = "partition_android.json"
image_name = "android-sdcard.img"
cfg_file = "genimage.cfg"
args = sys.argv[1:]
i = 0
while i < len(args):
if args[i] == "-i":
json_file = args[i+1]
i += 2
elif args[i] == "-n":
image_name = args[i+1]
i += 2
elif args[i] == "-o":
cfg_file = args[i+1]
i += 2
elif args[i] in ["-h", "--help"]:
print(f"Usage: {sys.argv[0]} [-i <partitions json file>] [-n <image name>] [-o <cfg file>]")
sys.exit(0)
else:
print(f"Unknown argument: {args[i]}")
print(f"Usage: {sys.argv[0]} [-i <partitions json file>] [-n <image name>] [-o <cfg file>]")
sys.exit(1)
try:
with open(json_file, 'r') as file:
cfg_content = json.load(file)
partitions = cfg_content["partitions"]
genimage_cfg = generate_genimage_cfg(image_name, partitions)
except FileNotFoundError:
print(f"File not found: {json_file}")
sys.exit(1)
except json.JSONDecodeError as e:
print(f"JSON parse error: {e}")
sys.exit(1)
with open(cfg_file, "w") as f:
f.write(genimage_cfg)
print(f"Generated: {cfg_file}")
if __name__ == "__main__":
main()