sd_fusing.py: Refactor parameters writing
authorKarol Lewandowski <k.lewandowsk@samsung.com>
Tue, 21 May 2024 21:57:15 +0000 (23:57 +0200)
committerJaehoon Chung <jh80.chung@samsung.com>
Mon, 28 Oct 2024 11:28:45 +0000 (20:28 +0900)
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 ee380ebfc8e5bc2ec100819aa8c5d3e6015b5682..a564c2fe16de0bb748a7405f0f3a0f040b73e0f5 100755 (executable)
@@ -113,6 +113,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
@@ -171,10 +175,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):
+        self.write_parameters()
+
 class SdFusingTargetAB(SdFusingTarget):
     def __init__(self, device, ltype):
         super().__init__(device, ltype)
@@ -188,15 +197,30 @@ class SdFusingTargetAB(SdFusingTarget):
 
         return [self.binaries.get(binary, None)]
 
+    def update_parameters(self):
+        part_ab = 'a' if self.update in [None, '', 'a', '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 in [None, '', 'a', 'ab']:
+            params.append(('partition-a-status.info', 'ok'))
+        if self.update in ['b', '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))
@@ -204,6 +228,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))
@@ -213,8 +240,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))
@@ -245,7 +274,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
@@ -285,13 +313,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
@@ -340,13 +361,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
@@ -401,13 +415,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
@@ -447,8 +454,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
@@ -501,13 +506,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
@@ -555,13 +553,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
@@ -613,13 +604,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'
@@ -667,13 +651,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'
@@ -1120,6 +1097,7 @@ def fuse_image(args, target):
 
         if target.with_super and not SuperDelivered:
             do_fuse_image_super(tmpd, target)
+    target.update_parameters()
 
 def logger_notice(self, msg, *args, **kws):
     if self.isEnabledFor(LOGGING_NOTICE):