dual-image: Detect the running image slot

Add detect-slot-aspeed to detect which slot it is running on, and
assign the member variable, which will be used in future commits.

Tested:
    * In normal case, verify the primary slot is 0 and secondary slot
      is 1.
    * Manually assign value 1 in /run/media/slot, and verify the slot
      number is reversed.
    * Manually trigger wdt2 timeout and verify in secondary flash that
      the slot number is correct, that the running slot is 1 and the
      other slot is 0.

Signed-off-by: Lei YU <yulei.sh@bytedance.com>
Change-Id: I69edb1961b4c4bb79e7897c38b9ce8dded42f829
diff --git a/detect-slot-aspeed b/detect-slot-aspeed
new file mode 100644
index 0000000..231eb33
--- /dev/null
+++ b/detect-slot-aspeed
@@ -0,0 +1,17 @@
+#!/bin/bash
+set -eo pipefail
+
+# Check the /sys/class/watchdog/watchdog1/access_cs0 and tell if it's running on the primary or the secondary flash.
+
+ACCESS_CS0="/sys/class/watchdog/watchdog1/access_cs0"
+SLOT_FILE="/run/media/slot"
+
+# Create directory if not exist
+mkdir -p "$(dirname "${SLOT_FILE}")"
+
+# Write slot info
+if [ -f ${ACCESS_CS0} ]; then
+    echo "1" > ${SLOT_FILE}
+else
+    echo "0" > ${SLOT_FILE}
+fi
diff --git a/item_updater.cpp b/item_updater.cpp
index ea7cb2d..de79dd8 100644
--- a/item_updater.cpp
+++ b/item_updater.cpp
@@ -811,6 +811,14 @@
 }
 #endif
 
+void ItemUpdater::getRunningSlot()
+{
+    // Check /run/media/slot to get the slot number
+    constexpr auto slotFile = "/run/media/slot";
+    std::fstream f(slotFile, std::ios_base::in);
+    f >> runningImageSlot;
+}
+
 } // namespace updater
 } // namespace software
 } // namespace phosphor
diff --git a/item_updater.hpp b/item_updater.hpp
index 794aa4d..4c47c8b 100644
--- a/item_updater.hpp
+++ b/item_updater.hpp
@@ -59,6 +59,7 @@
                      std::bind(std::mem_fn(&ItemUpdater::createActivation),
                                this, std::placeholders::_1))
     {
+        getRunningSlot();
         setBMCInventoryPath();
         processBMCImage();
         restoreFieldModeStatus();
@@ -168,6 +169,9 @@
     /** @brief Vector of needed BMC images in the tarball*/
     std::vector<std::string> imageUpdateList;
 
+    /** @breif The slot of running BMC image */
+    uint32_t runningImageSlot = 0;
+
   private:
     /** @brief Callback function for Software.Version match.
      *  @details Creates an Activation D-Bus object.
@@ -273,6 +277,9 @@
     /** @brief Persistent Version D-Bus object for BIOS */
     std::unique_ptr<VersionClass> biosVersion;
 #endif
+
+    /** @brief Get the slot number of running image */
+    void getRunningSlot();
 };
 
 } // namespace updater
diff --git a/meson.build b/meson.build
index 6ec94e1..51f0aba 100644
--- a/meson.build
+++ b/meson.build
@@ -284,6 +284,11 @@
     install_dir: get_option('bindir')
 )
 
+install_data('detect-slot-aspeed',
+    install_mode: 'rwxr-xr-x',
+    install_dir: get_option('bindir')
+)
+
 install_data('software.conf',
     install_dir: '/usr/lib/tmpfiles.d/'
 )
diff --git a/static/obmc-flash-bmc-static-mount-alt.service.in b/static/obmc-flash-bmc-static-mount-alt.service.in
index ba60f09..cfb1c9d 100644
--- a/static/obmc-flash-bmc-static-mount-alt.service.in
+++ b/static/obmc-flash-bmc-static-mount-alt.service.in
@@ -10,6 +10,7 @@
 # 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
+ExecStart=-/usr/bin/detect-slot-aspeed
 
 [Install]
 WantedBy=multi-user.target