forked from qualcomm-linux/qcom-ptool
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgen_contents.py
More file actions
executable file
·150 lines (127 loc) · 5.71 KB
/
gen_contents.py
File metadata and controls
executable file
·150 lines (127 loc) · 5.71 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/env python3
# Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
# SPDX-License-Identifier: BSD-3-Clause
import getopt
import os
import sys
from xml.etree import ElementTree as ET
def usage():
print(("\n\tUsage: %s -t <template> -p <partitions_xml_path> -o <output> \n\tVersion 0.1\n" % (sys.argv[0])))
sys.exit(1)
def ParseXML(XMLFile):
try:
tree = ET.parse(XMLFile)
root = tree.getroot()
return root
except FileNotFoundError:
print(f"Error: File '{XMLFile}' not found")
return None
except ET.ParseError as e:
print(f"Error: Failed parsing '{XMLFile}': {e}")
return None
def UpdateMetaData(TemplateRoot, PartitionRoot, BuildId):
ChipIdList = TemplateRoot.findall('product_info/chipid')
DefaultStorageType = None
for ChipId in ChipIdList:
Flavor = ChipId.get('flavor')
StorageType = ChipId.get('storage_type')
print(f"Chipid Flavor: {Flavor} Storage Type: {StorageType}")
if Flavor == "default":
DefaultStorageType = ChipId.get('storage_type')
PhyPartition = PartitionRoot.findall('physical_partition')
Partitions = []
for partition in PartitionRoot.findall('physical_partition/partition'):
label = partition.get('label')
filename = partition.get('filename')
if label and filename:
Partitions.append({'label': label, 'filename': filename})
print(f"Partitions: {Partitions}")
def _add_file_elements(parent_element, pathname, file_path_flavor=None):
"""Helper function to add file_name and file_path sub-elements."""
file_name_text = os.path.basename(pathname)
file_path_text = os.path.dirname(pathname)
if not file_path_text: # no directory, use explicit . as current dir
file_path_text = "."
new_file_name = ET.SubElement(parent_element, "file_name")
new_file_name.text = file_name_text
new_file_path = ET.SubElement(parent_element, "file_path")
if file_path_flavor:
new_file_path.set("flavor", file_path_flavor)
new_file_path.text = file_path_text
builds = TemplateRoot.findall('builds_flat/build')
for build in builds:
Name = build.find('name')
print(f"Build Name: {Name.text}")
new_build_id = ET.SubElement(build, "build_id")
new_build_id.text = BuildId
if Name.text != "common":
continue
DownloadFile = build.find('download_file')
if DownloadFile is not None:
build.remove(DownloadFile)
# Partition entires
for Partition in Partitions:
new_download_file = ET.SubElement(build, "download_file")
new_download_file.set("fastboot_complete", Partition['label'])
_add_file_elements(new_download_file, Partition['filename'])
# GPT Main & GPT Backup entries
for PhysicalPartitionNumber in range(0, len(PhyPartition)):
new_download_file = ET.SubElement(build, "download_file")
new_download_file.set("storage_type", DefaultStorageType)
_add_file_elements(new_download_file, 'gpt_main%d.bin' % (PhysicalPartitionNumber))
new_download_file = ET.SubElement(build, "download_file")
new_download_file.set("storage_type", DefaultStorageType)
_add_file_elements(new_download_file, 'gpt_backup%d.bin' % (PhysicalPartitionNumber))
PartitionFile = build.find('partition_file')
if PartitionFile is not None:
build.remove(PartitionFile)
# Rawprogram entries
for PhysicalPartitionNumber in range(0, len(PhyPartition)):
new_partition_file = ET.SubElement(build, "partition_file")
new_partition_file.set("storage_type", DefaultStorageType)
_add_file_elements(new_partition_file, 'rawprogram%d.xml' % (PhysicalPartitionNumber), "default")
PartitionPatchFile = build.find('partition_patch_file')
if PartitionPatchFile is not None:
build.remove(PartitionPatchFile)
# Patch entries
for PhysicalPartitionNumber in range(0, len(PhyPartition)):
new_partition_patch_file = ET.SubElement(build, "partition_patch_file")
new_partition_patch_file.set("storage_type", DefaultStorageType)
_add_file_elements(new_partition_patch_file, 'patch%d.xml' % (PhysicalPartitionNumber), "default")
###############################################################################
# main
###############################################################################
if len(sys.argv) < 3:
usage()
try:
if sys.argv[1] == "-h" or sys.argv[1] == "--help":
usage()
try:
build_id = ""
opts, rem = getopt.getopt(sys.argv[1:], "t:p:o:b:")
for (opt, arg) in opts:
if opt in ["-t"]:
template = arg
elif opt in ["-p"]:
partition_xml = arg
elif opt in ["-o"]:
output_xml = arg
elif opt in ["-b"]:
build_id = arg
else:
usage()
except Exception as argerr:
print(str(argerr))
usage()
print("Selected Template: " + template)
xml_root = ParseXML(template)
print("Selected Partition XML: " + partition_xml)
partition_root = ParseXML(partition_xml)
UpdateMetaData(xml_root, partition_root, build_id)
OutputTree = ET.ElementTree(xml_root)
ET.indent(OutputTree, space="\t", level=0)
OutputTree.write(output_xml, encoding="utf-8", xml_declaration=True, short_empty_elements=False)
except Exception as e:
print(("Error: ", e))
sys.exit(1)
sys.exit(0)