From c101df6b77e0b0e1fccbaf3c639311971a050d58 Mon Sep 17 00:00:00 2001 From: Nicolas Dechesne Date: Mon, 9 Feb 2026 19:02:19 +0100 Subject: [PATCH 1/3] gen_partition: convert partition_entries_dict into list partition_entries_dict was set as a dict, but it is really just a list. the 'key' is set to an index which is never used. Let's convert to a proper list to make the code more readable. There is no functional change. Signed-off-by: Nicolas Dechesne --- gen_partition.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/gen_partition.py b/gen_partition.py index ea79efd..c879fd8 100755 --- a/gen_partition.py +++ b/gen_partition.py @@ -64,7 +64,7 @@ def usage(): ################################################################## # store entries read from input file disk_entry = None -partition_entries_dict = {} +partition_entries = [] # store partition image map passed from command line partition_image_map = {} input_file = None @@ -152,7 +152,7 @@ def parse_disk_entry(disk_entry): print (str(e)) usage() -def generate_multi_lun_xml (disk_params, partition_entries_dict, output_xml): +def generate_multi_lun_xml (disk_params, partition_entries, output_xml): root = ET.Element("configuration") parser_instruction_text = "" @@ -167,7 +167,7 @@ def generate_multi_lun_xml (disk_params, partition_entries_dict, output_xml): while lun_index < 6: found = False - for partition_index, entry in partition_entries_dict.items(): + for entry in partition_entries: part_entry = parse_partition_entry(entry) if part_entry["physical_partition"] == str(lun_index): del part_entry["physical_partition"] @@ -183,12 +183,12 @@ def generate_multi_lun_xml (disk_params, partition_entries_dict, output_xml): with open(output_xml, "w") as f: f.write(xmlstr) -def generate_partition_xml (disk_entry, partition_entries_dict, output_xml): +def generate_partition_xml (disk_entry, partition_entries, output_xml): parse_disk_entry(disk_entry) print("Generating %s XML %s" %(disk_params["type"].upper(), output_xml)) if disk_params["type"] in ("emmc", "nvme", "spinor", "ufs"): - generate_multi_lun_xml(disk_params, partition_entries_dict, output_xml) + generate_multi_lun_xml(disk_params, partition_entries, output_xml) else: print("%s XML generation is curently not supported." %(disk_params["type"].upper())) @@ -222,7 +222,6 @@ def generate_partition_xml (disk_entry, partition_entries_dict, output_xml): usage() f = open(input_file) line = f.readline() - partition_index = 0 while line: if not re.search(r'^\s*#', line) and not re.search(r'^\s*$', line): line = line.strip() @@ -234,8 +233,7 @@ def generate_partition_xml (disk_entry, partition_entries_dict, output_xml): print("%s\n%s" %(disk_entry, line)) sys.exit(1) elif re.search("^--partition", line): - partition_entries_dict[partition_index] = line - partition_index += 1 + partition_entries.append(line) else: print("Ignoring %s" %(line)) line = f.readline() @@ -244,6 +242,6 @@ def generate_partition_xml (disk_entry, partition_entries_dict, output_xml): print("Error: ", e) sys.exit(1) -generate_partition_xml(disk_entry, partition_entries_dict, output_xml) +generate_partition_xml(disk_entry, partition_entries, output_xml) sys.exit(0) From a96ec7e9fb00861fb6153a67ab1c63b3d5aceafe Mon Sep 17 00:00:00 2001 From: Nicolas Dechesne Date: Mon, 9 Feb 2026 19:13:38 +0100 Subject: [PATCH 2/3] gen_partition: improve how disk parameters are parsed The disk and partitions data is first read from the partition conf file into an intermediate list, and it is parsed into actual/usable data later, inside generate_partition_xml() which is slightly convoluted. Instead, let's parse all input files in a first step before we start the actual generation of partition xml file. In this commit, move the parsing of disk_options first. Use that as an opportunity to remove the global/static disk_params, and make a copy from disk_options(). Signed-off-by: Nicolas Dechesne --- gen_partition.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/gen_partition.py b/gen_partition.py index c879fd8..4d8d5be 100755 --- a/gen_partition.py +++ b/gen_partition.py @@ -40,7 +40,7 @@ def usage(): ################################################################## # defaults to be used -disk_params = OrderedDict({ +disk_params_defaults = OrderedDict({ "type": "", "size": "", "SECTOR_SIZE_IN_BYTES": "512", @@ -71,6 +71,7 @@ def usage(): output_xml = None def disk_options(argv): + disk_params = disk_params_defaults.copy() for (opt, arg) in argv: if opt in ['--type']: disk_params["type"] = arg @@ -85,6 +86,7 @@ def disk_options(argv): elif opt in ['--align-partitions']: disk_params["ALIGN_PARTITIONS_TO_PERFORMANCE_BOUNDARY"] = "true" disk_params["PERFORMANCE_BOUNDARY_IN_KB"] = (int(arg)/1024) + return disk_params def partition_size_in_kb(size): if not re.search('[a-zA-Z]+', size): @@ -147,7 +149,7 @@ def parse_disk_entry(disk_entry): options, remainders = getopt.gnu_getopt(opts_list[1:], '', ['type=', 'size=','sector-size-in-bytes=', 'write-protect-boundary=', 'grow-last-partition', 'align-partitions=']) - disk_options(options) + return disk_options(options) except Exception as e: print (str(e)) usage() @@ -183,8 +185,7 @@ def generate_multi_lun_xml (disk_params, partition_entries, output_xml): with open(output_xml, "w") as f: f.write(xmlstr) -def generate_partition_xml (disk_entry, partition_entries, output_xml): - parse_disk_entry(disk_entry) +def generate_partition_xml (disk_params, partition_entries, output_xml): print("Generating %s XML %s" %(disk_params["type"].upper(), output_xml)) if disk_params["type"] in ("emmc", "nvme", "spinor", "ufs"): @@ -242,6 +243,7 @@ def generate_partition_xml (disk_entry, partition_entries, output_xml): print("Error: ", e) sys.exit(1) -generate_partition_xml(disk_entry, partition_entries, output_xml) +disk_params = parse_disk_entry(disk_entry) +generate_partition_xml(disk_params, partition_entries, output_xml) sys.exit(0) From c670d61e67d58481c31ade739f2cef899d98757f Mon Sep 17 00:00:00 2001 From: Nicolas Dechesne Date: Wed, 11 Feb 2026 08:27:55 +0100 Subject: [PATCH 3/3] gen_partition: improve parsing and managing partitions The parsing of the partition entries is embedded deep into generate_multi_lun_xml() which makes the code difficult to parse/understand. Instead let's first parse the partition conf file and create a proper data structure for it. Then adjust the generate_multi_lun_xml() to use the new data struct. The config file contains the list of all partitions that are needed, on all the physical partitions (aks LUN in UFS). The data structure to store all information is a dict where the key is the phys_part, and the value is the list of all partitions on this specific physical part. The new implementation is much more flexible, in particular, we no longer hardcode the max phys_part to 5, and make no assumption on how many phys_part there (it's 1 or more). There is no functional change and the generated XML files are the same. Signed-off-by: Nicolas Dechesne --- gen_partition.py | 61 ++++++++++++++++++++++++------------------------ 1 file changed, 31 insertions(+), 30 deletions(-) diff --git a/gen_partition.py b/gen_partition.py index 4d8d5be..b443ffe 100755 --- a/gen_partition.py +++ b/gen_partition.py @@ -58,7 +58,6 @@ def usage(): "readonly": "true", "filename": "", "sparse" : "false", - "physical_partition": "0" } ################################################################## @@ -100,9 +99,10 @@ def partition_size_in_kb(size): def partition_options(argv): partition_entry = partition_entry_defaults.copy() + phys_part = 0 for (opt, arg) in argv: if opt in ['--lun', '--phys-part']: - partition_entry["physical_partition"] = arg + phys_part = arg elif opt in ['--name']: partition_entry["label"] = arg elif opt in ['--size']: @@ -126,21 +126,25 @@ def partition_options(argv): partition_entry["sparse"] = arg if partition_entry["label"] in partition_image_map.keys(): partition_entry["filename"] = partition_image_map[partition_entry["label"]] - return partition_entry + return phys_part, partition_entry -def parse_partition_entry(partition_entry): - opts_list = list(partition_entry.split(' ')) - if opts_list[0] == "--partition": - try: - options, remainders = getopt.gnu_getopt(opts_list[1:], '', +def parse_partition_entries(partition_entries): + partitions_params = {} + + for partition_entry in partition_entries: + opts_list = list(partition_entry.split(' ')) + if opts_list[0] == "--partition": + try: + options, remainders = getopt.gnu_getopt(opts_list[1:], '', ['lun=', 'phys-part=', 'name=', 'size=','type-guid=', 'filename=', 'attributes=', 'sparse=']) - return partition_options(options) - except Exception as e: - print (str(e)) - usage() + phys_part, partition = partition_options(options) + partitions_params.setdefault(phys_part, []).append(partition) + except Exception as e: + print (str(e)) + usage() - return None + return partitions_params def parse_disk_entry(disk_entry): opts_list = list(disk_entry.split(' ')) @@ -154,7 +158,7 @@ def parse_disk_entry(disk_entry): print (str(e)) usage() -def generate_multi_lun_xml (disk_params, partition_entries, output_xml): +def generate_multi_lun_xml (disk_params, partitions, output_xml): root = ET.Element("configuration") parser_instruction_text = "" @@ -165,31 +169,27 @@ def generate_multi_lun_xml (disk_params, partition_entries, output_xml): parser_inst = ET.SubElement(root,"parser_instructions").text = ( parser_instruction_text ) - lun_index=0 - while lun_index < 6: + + for phys_part, entries in sorted(partitions.items(), key=lambda item: int(item[0])): found = False + for part_entry in entries: - for entry in partition_entries: - part_entry = parse_partition_entry(entry) - if part_entry["physical_partition"] == str(lun_index): - del part_entry["physical_partition"] - # if there is no partition in the LUN, skip the physical_partition in XML - # only create the physical_partition once we have at least one partition - if not found: - phy_part = ET.SubElement(root, "physical_partition") - part = ET.SubElement(phy_part, "partition", attrib=part_entry) - found = True - lun_index +=1 + # if there is no partition in the LUN, skip the physical_partition in XML + # only create the physical_partition once we have at least one partition + if not found: + phy_part = ET.SubElement(root, "physical_partition") + part = ET.SubElement(phy_part, "partition", attrib=part_entry) + found = True xmlstr = minidom.parseString(ET.tostring(root)).toprettyxml() with open(output_xml, "w") as f: f.write(xmlstr) -def generate_partition_xml (disk_params, partition_entries, output_xml): +def generate_partition_xml (disk_params, partitions, output_xml): print("Generating %s XML %s" %(disk_params["type"].upper(), output_xml)) if disk_params["type"] in ("emmc", "nvme", "spinor", "ufs"): - generate_multi_lun_xml(disk_params, partition_entries, output_xml) + generate_multi_lun_xml(disk_params, partitions, output_xml) else: print("%s XML generation is curently not supported." %(disk_params["type"].upper())) @@ -244,6 +244,7 @@ def generate_partition_xml (disk_params, partition_entries, output_xml): sys.exit(1) disk_params = parse_disk_entry(disk_entry) -generate_partition_xml(disk_params, partition_entries, output_xml) +partitions = parse_partition_entries(partition_entries) +generate_partition_xml(disk_params, partitions, output_xml) sys.exit(0)