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/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