phosphor-software-manager: New service file to create ubi ro volumes

Add service file and script to create ubi read-only volumes
to store the rofs and kernel. Default the sizes to the
current corresponding mtd size.
Create a ubi block for the read-only image which is a squashfs.

The mtd device locations can be passed through the recipe.
Set it to the "pnor" mtd as the default. This can be changed via a
per-system bbappend to other chips if desired, such as the alternate
bmc chip.

Resolves openbmc/openbmc#1651

Change-Id: I76aa9021a2bb5462c0e4c2efef99728d26873df0
Signed-off-by: Adriana Kobylak <anoo@us.ibm.com>
diff --git a/common/recipes-phosphor/flash/phosphor-software-manager.bb b/common/recipes-phosphor/flash/phosphor-software-manager.bb
index b7a1fd1..acdd9a3 100644
--- a/common/recipes-phosphor/flash/phosphor-software-manager.bb
+++ b/common/recipes-phosphor/flash/phosphor-software-manager.bb
@@ -62,11 +62,18 @@
 
 SYSTEMD_SERVICE_${PN}-updater += " \
     obmc-flash-bmc-ubirw.service \
+    obmc-flash-bmc-ubiro@.service \
     "
 
 # Name of the mtd device where the ubi volumes should be created
 BMC_RW_MTD ??= "pnor"
+BMC_RO_MTD ??= "pnor"
+# TODO Change kernel location to primary BMC chip once the rofs/rwfs mtd devices
+# are merged into a single ubi one openbmc/openbmc#1942
+BMC_KERNEL_MTD ??= "pnor"
 SYSTEMD_SUBSTITUTIONS += "RW_MTD:${BMC_RW_MTD}:obmc-flash-bmc-ubirw.service"
+SYSTEMD_SUBSTITUTIONS += "RO_MTD:${BMC_RO_MTD}:obmc-flash-bmc-ubiro@.service"
+SYSTEMD_SUBSTITUTIONS += "KERNEL_MTD:${BMC_KERNEL_MTD}:obmc-flash-bmc-ubiro@.service"
 
 SRC_URI += "file://obmc-flash-bmc"
 do_install_append() {
diff --git a/common/recipes-phosphor/flash/phosphor-software-manager/obmc-flash-bmc b/common/recipes-phosphor/flash/phosphor-software-manager/obmc-flash-bmc
index 16c5f32..a451503 100644
--- a/common/recipes-phosphor/flash/phosphor-software-manager/obmc-flash-bmc
+++ b/common/recipes-phosphor/flash/phosphor-software-manager/obmc-flash-bmc
@@ -28,12 +28,62 @@
   fi
 }
 
+ubi_ro() {
+  romtd="$(findmtd "${reqmtd}")"
+  ro="${romtd#mtd}"
+  ubidev="/dev/ubi${ro}"
+
+  # Create a static ubi volume
+  # TODO Get the actual image size openbmc/openbmc#1840
+  vol="$(findubi "${name}")"
+  if [ -z "${vol}" ]; then
+    ubimkvol "${ubidev}" -N "${name}" -s "${imgsize}" --type=static
+    vol="$(findubi "${name}")"
+  fi
+}
+
+# Squashfs images need a ubi block
+ubi_block() {
+  vol="$(findubi "${name}")"
+  ubidevid="${vol#ubi}"
+  block="/dev/ubiblock${ubidevid}"
+  if [ ! -e "$block" ]; then
+    ubiblock --create "/dev/ubi${ubidevid}"
+  fi
+}
+
+ubi_updatevol() {
+  vol="$(findubi "${name}")"
+  ubidevid="${vol#ubi}"
+  img="/tmp/images/${version}/${imgfile}"
+  ubiupdatevol "/dev/ubi${ubidevid}" "${img}"
+}
+
 case "$1" in
   ubirw)
     reqmtd="$2"
     name="$3"
     ubi_rw
     ;;
+  ubiro)
+    reqmtd="$2"
+    name="$3"
+    version="$4"
+    imgfile="image-rofs"
+    imgsize="16MiB"
+    ubi_ro
+    ubi_block
+    ubi_updatevol
+    ;;
+  ubikernel)
+    reqmtd="$2"
+    name="$3"
+    version="$4"
+    imgfile="image-kernel"
+    imgsize="4MiB"
+    ubi_ro
+    ubi_updatevol
+    ;;
   *)
     echo "Invalid argument"
     exit 1
diff --git a/common/recipes-phosphor/flash/phosphor-software-manager/obmc-flash-bmc-ubiro@.service b/common/recipes-phosphor/flash/phosphor-software-manager/obmc-flash-bmc-ubiro@.service
new file mode 100644
index 0000000..4dec96e
--- /dev/null
+++ b/common/recipes-phosphor/flash/phosphor-software-manager/obmc-flash-bmc-ubiro@.service
@@ -0,0 +1,8 @@
+[Unit]
+Description=Create read-only ubi volumes %I
+
+[Service]
+Type=oneshot
+RemainAfterExit=no
+ExecStart={sbindir}/obmc-flash-bmc ubiro {RO_MTD} ro-%i %i
+ExecStart={sbindir}/obmc-flash-bmc ubikernel {KERNEL_MTD} kernel-%i %i