sd_fusing.py: Refactor parameters writing 75/311475/3
authorKarol Lewandowski <k.lewandowsk@samsung.com>
Tue, 21 May 2024 21:57:15 +0000 (23:57 +0200)
committerKarol Lewandowski <k.lewandowsk@samsung.com>
Wed, 22 May 2024 21:32:56 +0000 (23:32 +0200)
This commit considerably alters when and which parameters are written
to "inform" partition:

 - Parameters are now updated always, not only when --format has been
   specified (this is compatible with old sd_fusing.sh behaviour)

 - Initial parameters are collected from configuration and from user
   input to reflect actual flashing options, eg. if user chosen to
   update only 'a' partition script won't set the flag that both
   partitions are cloned

   Dynamic parameters generation allowed to drop a lot of duplicated
   params.

Change-Id: I597f104b2b7972728663e2d85555407c721b7bb0
Signed-off-by: Karol Lewandowski <k.lewandowsk@samsung.com>
scripts/tizen/sd_fusing.py

index c36fda51bbc42fc37a5f8139a2a3a4a62a9a5888..3b58d748906a27e546a55ed96850fe995ae6c003 100755 (executable)
@@ -112,6 +112,10 @@ class Label:
         return output
 
 class SdFusingTarget:
+    params = (('reboot-param.bin', 'norm'),
+              ('reboot-param.info', 'norm'),
+              ('upgrade-status.info', '0'))
+
     def __init__(self, device, ltype):
         # TODO: make a copy of a sublcass part_table
         self.with_super = False
@@ -170,10 +174,15 @@ class SdFusingTarget:
             sys.exit(1)
         return [self.binaries.get(binary, None)]
 
-    params = ()
     def initialize_parameters(self):
         pass
 
+    def write_parameters(self, params = None):
+        pass
+
+    def update_parameters(self):
+        pass
+
 class SdFusingTargetAB(SdFusingTarget):
     def __init__(self, device, ltype):
         super().__init__(device, ltype)
@@ -187,15 +196,30 @@ class SdFusingTargetAB(SdFusingTarget):
 
         return [self.binaries.get(binary, None)]
 
+    def update_parameters(self):
+        part_ab = 'a' if not self.update or self.update == 'a' or self.update == 'ab' else 'b'
+        part_cloned = '1' if self.update == 'ab' else '0'
+        params = [('partition-ab.info', part_ab),
+                  ('partition-ab-cloned.info', part_cloned)]
+        if not self.update or self.update == 'a' or self.update == 'ab':
+            params.append(('partition-a-status.info', 'ok'))
+        if self.update == 'b' or self.update == 'ab':
+            params.append(('partition-b-status.info', 'ok'))
+        self.write_parameters(self.params + tuple(params))
+
 class InitParams:
-    def initialize_parameters(self):
-        logging.debug("Initializing parameterss")
+    def find_inform(self):
         n = None
         for i, p in enumerate(self.part_table):
             if p['name'] == 'inform':
                 n = i + 1;
                 break
         d = "/dev/" + get_partition_device(self.device, n)
+        return d
+
+    def initialize_parameters(self):
+        logging.debug("Initializing parameters")
+        d = self.find_inform()
 
         argv = ['tune2fs', '-O', '^metadata_csum', d]
         logging.debug(" ".join(argv))
@@ -203,6 +227,9 @@ class InitParams:
                        stdin=subprocess.DEVNULL,
                        stdout=None, stderr=None)
 
+    def write_parameters(self, params = None):
+        d = self.find_inform()
+        logging.debug(f"Writing parameters to {d}")
         with tempfile.TemporaryDirectory() as mnt:
             argv = ['mount', '-t', 'ext4', d, mnt]
             logging.debug(" ".join(argv))
@@ -212,8 +239,10 @@ class InitParams:
             if proc.returncode != 0:
                 logging.error(f"Failed to mount {d} in {mnt} (Has the device been initialized with --format?)")
                 return
-            for param, value in self.params:
+            parameters = self.params if params is None else params
+            for param, value in parameters:
                 with open(os.path.join(mnt, param), 'w') as f:
+                    logging.debug(f"Writing parameter {param}={value}")
                     f.write(value + '\n')
             argv = ['umount', d]
             logging.debug(" ".join(argv))
@@ -244,7 +273,6 @@ class Rpi3(InitParams, SdFusingTarget):
          "binaries": "hal.img"},
         {"size": 125,  "fstype": "ext4", "name": "reserved2"},
     ]
-    params = (('reboot-param.bin', ''),)
 
     def __init__(self, device, args):
         self.reserved_space = 12
@@ -284,13 +312,6 @@ class Rpi4Super(InitParams, SdFusingTargetAB):
         {"size": 64,   "fstype": "ext4", "name": "reserved1"},
         {"size": 125,  "fstype": "ext4", "name": "reserved2"}
     ]
-    params = (('reboot-param.bin', 'norm'),
-              ('reboot-param.info', 'norm'),
-              ('partition-ab.info', 'a'),
-              ('partition-ab-cloned.info', '1'),
-              ('upgrade-status.info', '0'),
-              ('partition-a-status.info', 'ok'),
-              ('partition-b-status.info', 'ok'))
 
     def __init__(self, device, args):
         self.reserved_space = 8
@@ -339,13 +360,6 @@ class Rpi4(InitParams, SdFusingTargetAB):
         {"size": 64,  "fstype": "ext4", "name": "reserved1"},
         {"size": 125,  "fstype": "ext4", "name": "reserved2"},
     ]
-    params = (('reboot-param.bin', 'norm'),
-              ('reboot-param.info', 'norm'),
-              ('partition-ab.info', 'a'),
-              ('partition-ab-cloned.info', '1'),
-              ('upgrade-status.info', '0'),
-              ('partition-a-status.info', 'ok'),
-              ('partition-b-status.info', 'ok'))
 
     def __init__(self, device, args):
         self.reserved_space = 5
@@ -400,13 +414,6 @@ class Rpi4AoT(InitParams, SdFusingTargetAB):
         {"size": 64,  "fstype": "ext4", "name": "reserved1"},
         {"size": 125,  "fstype": "ext4", "name": "reserved2"},
     ]
-    params = (('reboot-param.bin', 'norm'),
-              ('reboot-param.info', 'norm'),
-              ('partition-ab.info', 'a'),
-              ('partition-ab-cloned.info', '1'),
-              ('upgrade-status.info', '0'),
-              ('partition-a-status.info', 'ok'),
-              ('partition-b-status.info', 'ok'))
 
     def __init__(self, device, args):
         self.reserved_space = 5
@@ -446,8 +453,6 @@ class RV64(InitParams, SdFusingTarget):
         {"size": 64,   "fstype": "raw",  "name": "reserved1"},
         {"size": 125,  "fstype": "raw",  "name": "reserved2"},
     ]
-    params = (('reboot-param.bin', 'norm'),
-              ('reboot-param.info', 'norm'))
 
     def __init__(self, device, args):
         self.user_partition = 6
@@ -500,13 +505,6 @@ class VF2(InitParams, SdFusingTargetAB):
         {"size": 64,   "fstype": "raw",  "name": "reserved1"},
         {"size": 125,  "fstype": "raw",  "name": "reserved2"},
     ]
-    params = (('reboot-param.bin', 'norm'),
-              ('reboot-param.info', 'norm'),
-              ('partition-ab.info', 'a'),
-              ('partition-ab-cloned.info', '1'),
-              ('upgrade-status.info', '0'),
-              ('partition-a-status.info', 'ok'),
-              ('partition-b-status.info', 'ok'))
 
     def __init__(self, device, args):
         self.user_partition = 6
@@ -554,13 +552,6 @@ class VF2Super(InitParams, SdFusingTargetAB):
         {"size": 64,   "fstype": "raw",  "name": "reserved1"},
         {"size": 125,  "fstype": "raw",  "name": "reserved2"},
     ]
-    params = (('reboot-param.bin', 'norm'),
-              ('reboot-param.info', 'norm'),
-              ('partition-ab.info', 'a'),
-              ('partition-ab-cloned.info', '1'),
-              ('upgrade-status.info', '0'),
-              ('partition-a-status.info', 'ok'),
-              ('partition-b-status.info', 'ok'))
 
     def __init__(self, device, args):
         self.user_partition = 6
@@ -612,13 +603,6 @@ class LicheePi4A(InitParams, SdFusingTargetAB):
         {"size": 64,   "fstype": "raw",  "name": "reserved1"},
         {"size": 125,  "fstype": "raw",  "name": "reserved2"},
     ]
-    params = (('reboot-param.bin', 'norm'),
-              ('reboot-param.info', 'norm'),
-              ('partition-ab.info', 'a'),
-              ('partition-ab-cloned.info', '1'),
-              ('upgrade-status.info', '0'),
-              ('partition-a-status.info', 'ok'),
-              ('partition-b-status.info', 'ok'))
 
     # bootcode written to the protective MBR, aka RV64 'J 0x4400' (sector 34)
     bootcode = b'\x6f\x40\x00\x40'
@@ -666,13 +650,6 @@ class LicheePi4ASuper(InitParams, SdFusingTargetAB):
         {"size": 64,   "fstype": "ext4", "name": "reserved1"},
         {"size": 125,  "fstype": "ext4", "name": "reserved2"}
     ]
-    params = (('reboot-param.bin', 'norm'),
-              ('reboot-param.info', 'norm'),
-              ('partition-ab.info', 'a'),
-              ('partition-ab-cloned.info', '1'),
-              ('upgrade-status.info', '0'),
-              ('partition-a-status.info', 'ok'),
-              ('partition-b-status.info', 'ok'))
 
     # bootcode written to the protective MBR, aka RV64 'J 0x4400' (sector 34)
     bootcode = b'\x6f\x40\x00\x40'
@@ -1104,6 +1081,7 @@ def fuse_image(args, target):
 
         if target.with_super:
             do_fuse_image_super(tmpd, target)
+    target.update_parameters()
 
 def logger_notice(self, msg, *args, **kws):
     if self.isEnabledFor(LOGGING_NOTICE):