Skip to content

Commit c9a224c

Browse files
committed
gen_contents.py: Add utility to generate contents.xml
gen_contents.py accepts a contents template and parition.xml as inputs and generates 'contents.xml' by updating image information in a format compatible with Axiom for automatic image flashing. Signed-off-by: Viswanath Kraleti <[email protected]>
1 parent 959edef commit c9a224c

File tree

1 file changed

+190
-0
lines changed

1 file changed

+190
-0
lines changed

gen_contents.py

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
3+
# SPDX-License-Identifier: BSD-3-Clause
4+
5+
import getopt
6+
import sys
7+
8+
from xml.etree import ElementTree as ET
9+
from xml.etree.ElementTree import Element, SubElement
10+
11+
def usage():
12+
print(("\n\tUsage: %s -t <template> -p <partitions_xml_path> -o <output> \n\tVersion 0.1\n" %(sys.argv[0])))
13+
sys.exit(1)
14+
15+
def ParseXML(XMLFile):
16+
try:
17+
tree = ET.parse(XMLFile)
18+
root = tree.getroot()
19+
return root
20+
except FileNotFoundError:
21+
print(f"Error: The file '{XMLFile}' not found")
22+
return None
23+
except ET.ParseError as e:
24+
print(f"Error parsing '{XMLFile}': {e}")
25+
return None
26+
27+
def UpdateMetaData(TemplateRoot, PartitionRoot):
28+
ChipIdList = TemplateRoot.findall('product_info/chipid')
29+
DefaultStorageType = None
30+
SailnorStorageType = None
31+
for ChipId in ChipIdList:
32+
Flavor = ChipId.get('flavor')
33+
StorageType = ChipId.get('storage_type')
34+
print(f"Chipid Flavor: {Flavor} Storage Type: {StorageType}")
35+
if Flavor == "default":
36+
DefaultStorageType = ChipId.get('storage_type')
37+
if Flavor == "sail_nor":
38+
SailnorStorageType = ChipId.get('storage_type')
39+
40+
PhyPartition = PartitionRoot.findall('physical_partition')
41+
PartitionsSet = set()
42+
Partitions = PartitionRoot.findall('physical_partition/partition')
43+
for partition in Partitions:
44+
label = partition.get('label')
45+
filename = partition.get('filename')
46+
if label and filename:
47+
PartitionsSet.add((label, filename))
48+
print(f"PartitionsSet: {PartitionsSet}")
49+
50+
builds = TemplateRoot.findall('builds_flat/build')
51+
for build in builds:
52+
Name = build.find('name')
53+
print(f"Build Name: {Name.text}")
54+
if Name.text != "common":
55+
continue
56+
DownloadFile = build.find('download_file')
57+
if DownloadFile is not None:
58+
build.remove(DownloadFile)
59+
# Partition entires
60+
for Partition in PartitionsSet:
61+
new_download_file = ET.SubElement(build, "download_file")
62+
new_download_file.set("fastboot_complete", Partition[0])
63+
new_file_name = ET.SubElement(new_download_file, "file_name")
64+
new_file_name.text = Partition[1]
65+
new_file_path = ET.SubElement(new_download_file, "file_path")
66+
new_file_path.text = "."
67+
# GPT Main & GPT Backup entires
68+
for PhysicalPartitionNumber in range(0,len(PhyPartition)):
69+
new_download_file = ET.SubElement(build, "download_file")
70+
new_download_file.set("storage_type", DefaultStorageType)
71+
new_file_name = ET.SubElement(new_download_file, "file_name")
72+
new_file_name.text = 'gpt_main%d.xml' % (PhysicalPartitionNumber)
73+
new_file_path = ET.SubElement(new_download_file, "file_path")
74+
new_file_path.text = "."
75+
new_download_file = ET.SubElement(build, "download_file")
76+
new_download_file.set("storage_type", DefaultStorageType)
77+
new_file_name = ET.SubElement(new_download_file, "file_name")
78+
new_file_name.text = 'gpt_backup%d.xml' % (PhysicalPartitionNumber)
79+
new_file_path = ET.SubElement(new_download_file, "file_path")
80+
new_file_path.text = "."
81+
82+
PartitionFile = build.find('partition_file')
83+
if PartitionFile is not None:
84+
build.remove(PartitionFile)
85+
# Rawprogram entires
86+
for PhysicalPartitionNumber in range(0,len(PhyPartition)):
87+
new_partition_file = ET.SubElement(build, "partition_file")
88+
new_partition_file.set("storage_type", DefaultStorageType)
89+
new_file_name = ET.SubElement(new_partition_file, "file_name")
90+
new_file_name.text = 'rawprogram%d.xml' % (PhysicalPartitionNumber)
91+
new_file_path = ET.SubElement(new_partition_file, "file_path")
92+
new_file_path.set("flavor", "default")
93+
new_file_path.text = "."
94+
95+
PartitionPatchFile = build.find('partition_patch_file')
96+
if PartitionPatchFile is not None:
97+
build.remove(PartitionPatchFile)
98+
# Patch entires
99+
for PhysicalPartitionNumber in range(0,len(PhyPartition)):
100+
new_partition_patch_file = ET.SubElement(build, "partition_patch_file")
101+
new_partition_patch_file.set("storage_type", DefaultStorageType)
102+
new_file_name = ET.SubElement(new_partition_patch_file, "file_name")
103+
new_file_name.text = 'patch%d.xml' % (PhysicalPartitionNumber)
104+
new_file_path = ET.SubElement(new_partition_patch_file, "file_path")
105+
new_file_path.set("flavor", "default")
106+
new_file_path.text = "."
107+
108+
# sail_nor is assumed to be a single parition.
109+
if SailnorStorageType is not None:
110+
new_download_file = ET.SubElement(build, "download_file")
111+
new_download_file.set("storage_type", SailnorStorageType)
112+
new_download_file.set("fastboot_complete", "SAIL_HYP")
113+
new_file_name = ET.SubElement(new_download_file, "file_name")
114+
new_file_name.text = "sailfreertos.elf"
115+
new_file_path = ET.SubElement(new_download_file, "file_path")
116+
new_file_path.set("flavor", "sail_nor")
117+
new_file_path.text = "./sail_nor"
118+
new_download_file = ET.SubElement(build, "download_file")
119+
new_download_file.set("storage_type", SailnorStorageType)
120+
new_download_file.set("flavor", "sail_nor")
121+
new_file_name = ET.SubElement(new_download_file, "file_name")
122+
new_file_name.text = "gpt_main0.bin"
123+
new_file_path = ET.SubElement(new_download_file, "file_path")
124+
new_file_path.set("flavor", "sail_nor")
125+
new_file_path.text = "./sail_nor"
126+
new_download_file = ET.SubElement(build, "download_file")
127+
new_download_file.set("storage_type", SailnorStorageType)
128+
new_download_file.set("flavor", "sail_nor")
129+
new_file_name = ET.SubElement(new_download_file, "file_name")
130+
new_file_name.text = "gpt_backup0.bin"
131+
new_file_path = ET.SubElement(new_download_file, "file_path")
132+
new_file_path.set("flavor", "sail_nor")
133+
new_file_path.text = "./sail_nor"
134+
new_partition_file = ET.SubElement(build, "partition_file")
135+
new_partition_file.set("storage_type", SailnorStorageType)
136+
new_partition_file.set("flavor", "sail_nor")
137+
new_file_name = ET.SubElement(new_partition_file, "file_name")
138+
new_file_name.text = 'rawprogram0.xml'
139+
new_file_path = ET.SubElement(new_partition_file, "file_path")
140+
new_file_path.set("flavor", "sail_nor")
141+
new_file_path.text = "./sail_nor"
142+
new_partition_patch_file = ET.SubElement(build, "partition_patch_file")
143+
new_partition_patch_file.set("storage_type", SailnorStorageType)
144+
new_partition_patch_file.set("flavor", "sail_nor")
145+
new_file_name = ET.SubElement(new_partition_patch_file, "file_name")
146+
new_file_name.text = 'patch0.xml'
147+
new_file_path = ET.SubElement(new_partition_patch_file, "file_path")
148+
new_file_path.set("flavor", "sail_nor")
149+
new_file_path.text = "./sail_nor"
150+
151+
###############################################################################
152+
# main
153+
###############################################################################
154+
if len(sys.argv) < 3:
155+
usage()
156+
157+
try:
158+
if sys.argv[1] == "-h" or sys.argv[1] == "--help":
159+
usage()
160+
try:
161+
opts, rem = getopt.getopt(sys.argv[1:], "t:p:o:")
162+
for (opt, arg) in opts:
163+
if opt in ["-t"]:
164+
template=arg
165+
elif opt in ["-p"]:
166+
partition_xml=arg
167+
elif opt in ["-o"]:
168+
output_xml=arg
169+
else:
170+
usage()
171+
except Exception as argerr:
172+
print(str(argerr))
173+
usage()
174+
175+
print(f"Selected Template: " + template)
176+
xml_root = ParseXML(template)
177+
178+
print(f"Selected Parition XML: " + partition_xml)
179+
partition_root = ParseXML(partition_xml)
180+
181+
UpdateMetaData(xml_root, partition_root)
182+
183+
OutputTree = ET.ElementTree(xml_root)
184+
ET.indent(OutputTree, space="\t", level=0)
185+
OutputTree.write(output_xml, encoding="utf-8", xml_declaration=True)
186+
except Exception as e:
187+
print(("Error: ", e))
188+
sys.exit(1)
189+
190+
sys.exit(0)

0 commit comments

Comments
 (0)