Do not allow power on if PNOR doesn't have required image

- The new BMC code requires the UBI based PNOR image to be loaded before
HOST turns on. If the PNOR is not formatted as UBI, we want a clear error in
the journal and quick fail to boot to notify the user.

Resolves openbmc/openbmc#2117

Change-Id: I547ba488a94c5b5871c6327b0fd3af678c656b1b
Signed-off-by: Saqib Khan <khansa@us.ibm.com>
Signed-off-by: Edward A. James <eajames@us.ibm.com>
diff --git a/meta-witherspoon/recipes-phosphor/mboxd/mboxd.bbappend b/meta-witherspoon/recipes-phosphor/mboxd/mboxd.bbappend
index 8b6248a..ac35ae9 100644
--- a/meta-witherspoon/recipes-phosphor/mboxd/mboxd.bbappend
+++ b/meta-witherspoon/recipes-phosphor/mboxd/mboxd.bbappend
@@ -1 +1,12 @@
 MBOXD_FLASH_SIZE = "64M"
+
+FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"
+
+SYSTEMD_SERVICE_${PN} += "check-pnor-format.service"
+
+SRC_URI += "file://check_pnor_format.sh"
+
+do_install_append() {
+	install -d ${D}${bindir}
+	install -m 0755 ${WORKDIR}/check_pnor_format.sh ${D}${bindir}/check_pnor_format.sh
+}
diff --git a/meta-witherspoon/recipes-phosphor/mboxd/mboxd/check-pnor-format.service b/meta-witherspoon/recipes-phosphor/mboxd/mboxd/check-pnor-format.service
new file mode 100644
index 0000000..88bf0ad
--- /dev/null
+++ b/meta-witherspoon/recipes-phosphor/mboxd/mboxd/check-pnor-format.service
@@ -0,0 +1,11 @@
+[Unit]
+Description=Check PNOR UBI format
+Before=mboxd.service
+
+[Service]
+ExecStart={bindir}/check_pnor_format.sh
+Type=oneshot
+RemainAfterExit=yes
+
+[Install]
+RequiredBy=mboxd.service
diff --git a/meta-witherspoon/recipes-phosphor/mboxd/mboxd/check_pnor_format.sh b/meta-witherspoon/recipes-phosphor/mboxd/mboxd/check_pnor_format.sh
new file mode 100644
index 0000000..ec043ea
--- /dev/null
+++ b/meta-witherspoon/recipes-phosphor/mboxd/mboxd/check_pnor_format.sh
@@ -0,0 +1,18 @@
+#!/bin/sh
+
+# Get the mtd device number (mtdX)
+findmtd() {
+  m="$(grep -xl "$1" /sys/class/mtd/*/name)"
+  m="${m%/name}"
+  m="${m##*/}"
+  echo "${m}"
+}
+
+pnormtd="$(findmtd pnor)"
+pnor="${pnormtd#mtd}"
+pnordev="/dev/mtd${pnor}"
+
+if [[ ! "$(dd if=${pnordev} bs=1 count=3 2> /dev/null)" = "UBI" ]]; then
+  echo "${pnordev} is not formatted UBI"
+  exit 1
+fi