scripts: generate binaries and binaries_b dictionaries
authorŁukasz Stelmach <l.stelmach@samsung.com>
Wed, 4 Oct 2023 09:17:43 +0000 (11:17 +0200)
committerJaehoon Chung <jh80.chung@samsung.com>
Tue, 17 Oct 2023 11:02:46 +0000 (20:02 +0900)
Read image filenames from the part_table and automatically
associate them with appropriate partitions. Adding or
removing partitions doesn't require manual and error-prone
changes to binaries and binaries_b dictionaries.

Change-Id: Ib1cff8cb75af8a6f6c4af6f487545c643d61f438
Signed-off-by: Łukasz Stelmach <l.stelmach@samsung.com>
scripts/tizen/sd_fusing.py

index 162a1cb..bf2428d 100755 (executable)
@@ -54,7 +54,7 @@ class ColorStreamHandler(logging.StreamHandler):
             self.formatter = DebugFormatter(format, datefmt, style)
 
 class Partition:
-    def __init__(self, name, size, start=None, ptype=None, fstype="raw", bootable=False):
+    def __init__(self, name, size, start=None, ptype=None, fstype="raw", bootable=False, **kwargs):
         self.name = name
         self.size = size
         self.start = start
@@ -101,8 +101,23 @@ class SdFusingTarget:
         if self.user_size < 100:
             logging.error(f"Not enough space for user data ({self.user_size}). Use larger storage.")
             raise OSError(errno.ENOSPC, os.strerror(errno.ENOSPC), device)
+        # self.user_partition counts from 0
         self.part_table[self.user_partition]["size"] = self.user_size
         self.label = Label(self.part_table, ltype)
+        self.binaries = self._get_binaries('binaries')
+
+    def _get_binaries(self, key):
+        binaries = {}
+        for i, p in enumerate(self.part_table):
+            b = p.get(key, None)
+            if b is None:
+                continue
+            if isinstance(b, str):
+                binaries[b] = i + 1
+            elif isinstance(b, list):
+                for f in b:
+                    binaries[f] = i + 1
+        return binaries
 
     def get_partition_index(self, binary):
         return self.binaries.get(binary, None)
@@ -112,6 +127,10 @@ class SdFusingTarget:
         pass
 
 class SdFusingTargetAB(SdFusingTarget):
+    def __init__(self, device, ltype):
+        super().__init__(device, ltype)
+        self.binaries_b = self._get_binaries('binaries_b')
+
     def get_partition_index(self, binary):
         if self.update == 'b':
             return self.binaries_b.get(binary, None)
@@ -154,28 +173,26 @@ class RpiInitParams:
 class Rpi3(RpiInitParams, SdFusingTarget):
     long_name = "Raspberry Pi 3"
     part_table = [
-        {"size": 64,   "fstype": "vfat", "name": "boot", "start": 4, "ptype": "0xe", "bootable": True},
-        {"size": 3072, "fstype": "ext4", "name": "rootfs"},
-        {"size": 1344, "fstype": "ext4", "name": "system-data"},
+        {"size": 64,   "fstype": "vfat", "name": "boot", "start": 4, "ptype": "0xe", "bootable": True,
+         "binaries": "boot.img"},
+        {"size": 3072, "fstype": "ext4", "name": "rootfs",
+         "binaries": "rootfs.img"},
+        {"size": 1344, "fstype": "ext4", "name": "system-data",
+         "binaries": "system-data.img"},
         {"size": None, "ptype":  "5",    "name": "extended", "start": 4484},
-        {"size": None, "fstype": "ext4", "name": "user"},
-        {"size": 32,   "fstype": "ext4", "name": "modules"},
-        {"size": 32,   "fstype": "ext4", "name": "ramdisk"},
-        {"size": 32,   "fstype": "ext4", "name": "ramdisk-recovery"},
+        {"size": None, "fstype": "ext4", "name": "user",
+         "binaries": "user.img"},
+        {"size": 32,   "fstype": "ext4", "name": "modules",
+         "binaries": "modules.img"},
+        {"size": 32,   "fstype": "ext4", "name": "ramdisk",
+         "binaries": "ramdisk.img"},
+        {"size": 32,   "fstype": "ext4", "name": "ramdisk-recovery",
+         "binaries": "ramdisk-recovery.img"},
         {"size": 8,    "fstype": "ext4", "name": "inform"},
-        {"size": 256,  "fstype": "ext4", "name": "hal"},
+        {"size": 256,  "fstype": "ext4", "name": "hal",
+         "binaries": "hal.img"},
         {"size": 125,  "fstype": "ext4", "name": "reserved2"},
     ]
-    binaries = {
-        "boot.img":             1,
-        "rootfs.img":           2,
-        "system-data.img":      3,
-        "user.img":             5,
-        "modules.img":          6,
-        "ramdisk.img":          7,
-        "ramdisk-recovery.img": 8,
-        "hal.img":              10,
-    }
     params = (('reboot-param.bin', ''),)
 
     def __init__(self, device, args):
@@ -187,39 +204,35 @@ class Rpi4Super(RpiInitParams, SdFusingTargetAB):
     long_name = "Raspberry Pi 4 w/ super partition"
     part_table = [
         {"size": 64,   "fstype": "vfat", "name": "boot_a","start": 4,
-         "ptype": "C12A7328-F81F-11D2-BA4B-00A0C93EC93B"},
-        {"size": 6656, "fstype": "ext4", "name": "super"},
-        {"size": 1344, "fstype": "ext4", "name": "system-data"},
+         "ptype": "C12A7328-F81F-11D2-BA4B-00A0C93EC93B",
+         "binaries": "boot.img"},
+        {"size": 6656, "fstype": "ext4", "name": "super",
+         "binaries": "super.img"},
+        {"size": 1344, "fstype": "ext4", "name": "system-data",
+         "binaries": "system-data.img"},
         {"size": 36,   "fstype": "raw",  "name": "none"},
-        {"size": None, "fstype": "ext4", "name": "user"},
-        {"size": 32,   "fstype": "ext4", "name": "module_a"},
-        {"size": 32,   "fstype": "ext4", "name": "ramdisk_a"},
-        {"size": 32,   "fstype": "ext4", "name": "ramdisk-recovery_a"},
+        {"size": None, "fstype": "ext4", "name": "user",
+         "binaries": "user.img"},
+        {"size": 32,   "fstype": "ext4", "name": "module_a",
+         "binaries": "modules.img"},
+        {"size": 32,   "fstype": "ext4", "name": "ramdisk_a",
+         "binaries": "ramdisk.img"},
+        {"size": 32,   "fstype": "ext4", "name": "ramdisk-recovery_a",
+         "binaries": "ramdisk-recovery.img"},
         {"size": 8,    "fstype": "ext4", "name": "inform"},
         {"size": 64,   "fstype": "vfat", "name": "boot_b",
-         "ptype": "C12A7328-F81F-11D2-BA4B-00A0C93EC93B"},
-        {"size": 32,   "fstype": "ext4", "name": "module_b"},
-        {"size": 32,   "fstype": "ext4", "name": "ramdisk_b"},
-        {"size": 32,   "fstype": "ext4", "name": "ramdisk-recovery_b"},
+         "ptype": "C12A7328-F81F-11D2-BA4B-00A0C93EC93B",
+         "binaries_b": "boot.img"},
+        {"size": 32,   "fstype": "ext4", "name": "module_b",
+         "binaries_b": "modules.img"},
+        {"size": 32,   "fstype": "ext4", "name": "ramdisk_b",
+         "binaries_b": "ramdisk.img"},
+        {"size": 32,   "fstype": "ext4", "name": "ramdisk-recovery_b",
+         "binaries_b": "ramdisk-recovery.img"},
         {"size": 4,    "fstype": "ext4", "name": "reserved0"},
         {"size": 64,   "fstype": "ext4", "name": "reserved1"},
         {"size": 125,  "fstype": "ext4", "name": "reserved2"}
     ]
-    binaries = {
-        "boot.img":              1,
-        "super.img":             2,
-        "system-data.img":       3,
-        "user.img":              5,
-        "modules.img":           6,
-        "ramdisk.img":           7,
-        "ramdisk-recovery.img":  8,
-    }
-    binaries_b = {
-        "boot.img":             10,
-        "modules.img":          11,
-        "ramdisk.img":          12,
-        "ramdisk-recovery.img": 13,
-    }
     params = (('reboot-param.bin', 'norm'),
               ('reboot-param.info', 'norm'),
               ('partition-ab.info', 'a'),
@@ -240,45 +253,41 @@ class Rpi4(RpiInitParams, SdFusingTargetAB):
     long_name = "Raspberry Pi 4"
     part_table = [
         {"size": 64,   "fstype": "vfat", "name": "boot_a", "start": 4,
-         "ptype": "C12A7328-F81F-11D2-BA4B-00A0C93EC93B"},
-        {"size": 3072, "fstype": "ext4", "name": "rootfs_a"},
-        {"size": 1344, "fstype": "ext4", "name": "system-data"},
+         "ptype": "C12A7328-F81F-11D2-BA4B-00A0C93EC93B",
+         "binaries": "boot.img"},
+        {"size": 3072, "fstype": "ext4", "name": "rootfs_a",
+         "binaries": "rootfs.img"},
+        {"size": 1344, "fstype": "ext4", "name": "system-data",
+         "binaries": "system-data.img"},
         {"size": 36,   "fstype": "raw",  "name": "none"},
-        {"size": None, "fstype": "ext4", "name": "user"},
-        {"size": 32,   "fstype": "ext4", "name": "module_a"},
-        {"size": 32,   "fstype": "ext4", "name": "ramdisk_a"},
-        {"size": 32,   "fstype": "ext4", "name": "ramdisk-recovery_a"},
+        {"size": None, "fstype": "ext4", "name": "user",
+         "binaries": "user.img"},
+        {"size": 32,   "fstype": "ext4", "name": "module_a",
+         "binaries": "modules.img"},
+        {"size": 32,   "fstype": "ext4", "name": "ramdisk_a",
+         "binaries": "ramdisk.img"},
+        {"size": 32,   "fstype": "ext4", "name": "ramdisk-recovery_a",
+         "binaries": "ramdisk-recovery.img"},
         {"size": 8,    "fstype": "ext4", "name": "inform"},
-        {"size": 256,  "fstype": "ext4", "name": "hal_a"},
+        {"size": 256,  "fstype": "ext4", "name": "hal_a",
+         "binaries": "hal.img"},
         {"size": 64,   "fstype": "vfat", "name": "boot_b",
-         "ptype": "C12A7328-F81F-11D2-BA4B-00A0C93EC93B"},
-        {"size": 3072, "fstype": "ext4", "name": "rootfs_b"},
-        {"size": 32,   "fstype": "ext4", "name": "module_b"},
-        {"size": 32,   "fstype": "ext4", "name": "ramdisk_b"},
-        {"size": 32,   "fstype": "ext4", "name": "ramdisk-recovery_b"},
-        {"size": 256,  "fstype": "ext4", "name": "hal_b"},
+         "ptype": "C12A7328-F81F-11D2-BA4B-00A0C93EC93B",
+         "binaries_b": "boot.img"},
+        {"size": 3072, "fstype": "ext4", "name": "rootfs_b",
+         "binaries_b": "rootfs.img"},
+        {"size": 32,   "fstype": "ext4", "name": "module_b",
+         "binaries_b": "modules.img"},
+        {"size": 32,   "fstype": "ext4", "name": "ramdisk_b",
+         "binaries_b": "ramdisk.img"},
+        {"size": 32,   "fstype": "ext4", "name": "ramdisk-recovery_b",
+         "binaries_b": "ramdisk-recovery.img"},
+        {"size": 256,  "fstype": "ext4", "name": "hal_b",
+         "binaries_b": "hal.img"},
         {"size": 4,    "fstype": "ext4", "name": "param"},
         {"size": 64,  "fstype": "ext4", "name": "reserved1"},
         {"size": 125,  "fstype": "ext4", "name": "reserved2"},
     ]
-    binaries = {
-        "boot.img":              1,
-        "rootfs.img":            2,
-        "system-data.img":       3,
-        "user.img":              5,
-        "modules.img":           6,
-        "ramdisk.img":           7,
-        "ramdisk-recovery.img":  8,
-        "hal.img":              10,
-    }
-    binaries_b = {
-        "boot.img":             11,
-        "rootfs.img":           12,
-        "modules.img":          13,
-        "ramdisk.img":          14,
-        "ramdisk-recovery.img": 15,
-        "hal.img":              16,
-    }
     params = (('reboot-param.bin', 'norm'),
               ('reboot-param.info', 'norm'),
               ('partition-ab.info', 'a'),
@@ -297,37 +306,34 @@ class RV64(SdFusingTarget):
     long_name = "QEMU RISC-V 64-bit"
     part_table = [
         {"size": 2,    "fstype": "raw",  "name": "SPL", "start": 4,
-         "ptype": "2E54B353-1271-4842-806F-E436D6AF6985"},
+         "ptype": "2E54B353-1271-4842-806F-E436D6AF6985",
+         "binaries": ""},
         {"size": 4,    "fstype": "raw",  "name": "u-boot",
-         "ptype": "5B193300-FC78-40CD-8002-E86C45580B47"},
+         "ptype": "5B193300-FC78-40CD-8002-E86C45580B47",
+         "binaries": ["u-boot.img", "u-boot.itb"],},
         {"size": 292,  "fstype": "vfat", "name": "boot_a",
-         "ptype": "C12A7328-F81F-11D2-BA4B-00A0C93EC93B"},
+         "ptype": "C12A7328-F81F-11D2-BA4B-00A0C93EC93B",
+         "binaries": "boot.img"},
         {"size": 36,   "fstype": "raw",  "name": "none"},
-        {"size": 3072, "fstype": "ext4", "name": "rootfs_a"},
-        {"size": 1344, "fstype": "ext4", "name": "system-data"},
-        {"size": None, "fstype": "ext4", "name": "user"},
-        {"size": 32,   "fstype": "ext4", "name": "module_a"},
-        {"size": 32,   "fstype": "ext4", "name": "ramdisk_a"},
-        {"size": 32,   "fstype": "ext4", "name": "ramdisk-recovery_a"},
+        {"size": 3072, "fstype": "ext4", "name": "rootfs_a",
+         "binaries": "rootfs.img"},
+        {"size": 1344, "fstype": "ext4", "name": "system-data",
+         "binaries": "system-data.img"},
+        {"size": None, "fstype": "ext4", "name": "user",
+         "binaries": "user.img"},
+        {"size": 32,   "fstype": "ext4", "name": "module_a",
+         "binaries": "modules.img"},
+        {"size": 32,   "fstype": "ext4", "name": "ramdisk_a",
+         "binaries": "ramdisk.img"},
+        {"size": 32,   "fstype": "ext4", "name": "ramdisk-recovery_a",
+         "binaries": "ramdisk-recovery.img"},
         {"size": 8,    "fstype": "raw",  "name": "inform"},
-        {"size": 256,  "fstype": "ext4", "name": "hal_a"},
+        {"size": 256,  "fstype": "ext4", "name": "hal_a",
+         "binaries": "hal.img"},
         {"size": 4,    "fstype": "raw",  "name": "reserved0"},
         {"size": 64,   "fstype": "raw",  "name": "reserved1"},
         {"size": 125,  "fstype": "raw",  "name": "reserved2"},
     ]
-    binaries = {
-        "u-boot-spl.bin.normal.out":     1,
-        "u-boot.img":                    2,
-        "u-boot.itb":                    2,
-        "boot.img":                      3,
-        "rootfs.img":                    5,
-        "system-data.img":               6,
-        "user.img":                      7,
-        "modules.img":                   8,
-        "ramdisk.img":                   9,
-        "ramdisk-recovery.img":          10,
-        "hal.img":                       12,
-    }
 
     def __init__(self, device, args):
         self.user_partition = 6