dual-image: Mount alt dirs

To support dual image in static layout, it needs to mount secondary's
rofs/rwfs.

Update obmc-flash-bmc tool to support mounting 'alt-xxx' directories in
/run/media/alt-xxx, and try to mount the alt-rofs, alt-rwfs before
xyz.openbmc_project.Software.BMC.Updater.service starts.

The ExecStart command is prepended with `-` so that it does not matter
if the mount fails, because the alt chip may be empty or contain invalid
data.

Tested: Verify the obmc-flash-bmc-static-mount-alt.service starts before
        xyz.openbmc_project.Software.BMC.Updater.service, and verify it
        mounts alt-rofs for dual image systems.
        The alt-rwfs will be mounted by the needed service in future
        commits.

Signed-off-by: Lei YU <yulei.sh@bytedance.com>
Change-Id: I2133a99d5324a1008c3508154f865ae54a716219
diff --git a/meson.build b/meson.build
index f6f3f35..6ec94e1 100644
--- a/meson.build
+++ b/meson.build
@@ -97,6 +97,11 @@
     conf.set_quoted('BIOS_OBJPATH', get_option('bios-object-path'))
 endif
 
+if get_option('bmc-static-dual-image').enabled()
+  conf.set('BMC_STATIC_DUAL_IMAGE', get_option('bmc-static-dual-image').enabled())
+  conf.set_quoted('ALT_ROFS_DIR', get_option('alt-rofs-dir'))
+endif
+
 configure_file(output: 'config.h', configuration: conf)
 
 sdbusplus_dep = dependency('sdbusplus', required: false)
@@ -217,6 +222,10 @@
     unit_files += 'obmc-flash-host-bios@.service.in'
 endif
 
+if get_option('bmc-static-dual-image').enabled()
+    unit_files += 'static/obmc-flash-bmc-static-mount-alt.service.in'
+endif
+
 if get_option('sync-bmc-files').enabled()
     executable(
         'phosphor-sync-software-manager',
diff --git a/meson_options.txt b/meson_options.txt
index 7dd411c..8144241 100644
--- a/meson_options.txt
+++ b/meson_options.txt
@@ -114,3 +114,12 @@
     value: '/xyz/openbmc_project/software/bios_active',
     description: 'The BIOS DBus object path.',
 )
+
+option('bmc-static-dual-image', type: 'feature', value: 'enabled',
+    description: 'Enable the dual image support for static layout.')
+
+option(
+    'alt-rofs-dir', type: 'string',
+    value: '/run/media/rofs-alt',
+    description: 'The base dir where all read-only partitions are mounted.',
+)
diff --git a/obmc-flash-bmc b/obmc-flash-bmc
index 30c3fcd..ab353bf 100644
--- a/obmc-flash-bmc
+++ b/obmc-flash-bmc
@@ -245,7 +245,7 @@
     done
 }
 
-mount_alt_rwfs() {
+mount_ubi_alt_rwfs() {
   altNum="$(findmtdnum "alt-bmc")"
   if [ ! -z "${altNum}" ]; then
     altRwfs=$(ubinfo -a -d ${altNum} | grep -w "rwfs") || true
@@ -304,6 +304,19 @@
   set_wdt2bite
 }
 
+mount_static_alt() {
+  typ=$1
+  altFs=$2
+  mountName=$3
+  altNum="$(findmtdnum ${altFs})"
+  if [ ! -z "${altNum}" ]; then
+    altFsMount="/run/media/${mountName}"
+    mkdir -p "${altFsMount}"
+    altFsBlock="/dev/mtdblock${altNum}"
+    mount -t "${typ}" "${altFsBlock}" "${altFsMount}"
+  fi
+}
+
 # Read the current env variable and set it on the alternate boot env
 copy_env_var_to_alt() {
   varName=$1
@@ -656,7 +669,7 @@
     ;;
   ubiremount)
     remount_ubi
-    mount_alt_rwfs
+    mount_ubi_alt_rwfs
     ;;
   createenvbackup)
     backup_env_vars
@@ -691,6 +704,9 @@
     flashid="$2"
     mmc_setprimary
     ;;
+  static-altfs)
+    mount_static_alt "$2" "$3" "$4"
+    ;;
   *)
     echo "Invalid argument"
     exit 1
diff --git a/static/obmc-flash-bmc-static-mount-alt.service.in b/static/obmc-flash-bmc-static-mount-alt.service.in
new file mode 100644
index 0000000..ba60f09
--- /dev/null
+++ b/static/obmc-flash-bmc-static-mount-alt.service.in
@@ -0,0 +1,15 @@
+[Unit]
+Description=Mount the alt partitions for static layout systems
+Wants=xyz.openbmc_project.Software.BMC.Updater.service
+Before=xyz.openbmc_project.Software.BMC.Updater.service
+
+[Service]
+Type=oneshot
+RemainAfterExit=no
+
+# The alt flash may be empty or contain invalid data so the mount could fail.
+# So prepend with `-` to ignore such failures
+ExecStart=-/usr/bin/obmc-flash-bmc static-altfs squashfs alt-rofs rofs-alt
+
+[Install]
+WantedBy=multi-user.target