mmc-init.sh: Wait for mmc device

The initramfs was accessing the mmc device before it was probed
in some cases, leading to this error message:

[    4.412464] mmcblk0rpmb: mmc0:0001 R1J56L partition 3 128 KiB, chardev (248:0)
tail: can't open '/dev/mmcblk0': No such file or directory
tail: no files
[    5.471158]  mmcblk0: p1 p2 p3 p4 p5 p6 p7

Implement a wait loop of up to 5s to wait for the device,
similar to what the kernel would do with rootwait.

Tested: Verified the error is not longer seen. Printing the count
        value as debug, it took one sleep iteration to appear:

[    4.396492] mmcblk0boot1: mmc0:0001 R1J56L partition 2 16.0 MiB
0
[    4.403500] mmcblk0rpmb: mmc0:0001 R1J56L partition 3 128 KiB, chardev (248:0)
[    4.416176]  mmcblk0: p1 p2 p3 p4 p5 p6 p7
1
[    6.159693] EXT4-fs (mmcblk0p4): mounted filesystem with ordered data mode. Opts: (null)

(From meta-phosphor rev: b7dccc1c380431f4cc96e0228fb9975d33df1f88)

Change-Id: I625a879882311285dbdeaa2ea271c379366f4b9b
Signed-off-by: Adriana Kobylak <anoo@us.ibm.com>
Signed-off-by: Andrew Geissler <geissonator@yahoo.com>
diff --git a/meta-phosphor/recipes-phosphor/initrdscripts/phosphor-mmc-init/mmc-init.sh b/meta-phosphor/recipes-phosphor/initrdscripts/phosphor-mmc-init/mmc-init.sh
index ec4b745..d41ddf7 100644
--- a/meta-phosphor/recipes-phosphor/initrdscripts/phosphor-mmc-init/mmc-init.sh
+++ b/meta-phosphor/recipes-phosphor/initrdscripts/phosphor-mmc-init/mmc-init.sh
@@ -17,11 +17,23 @@
 mount proc proc -tproc
 mount tmpfs run -t tmpfs -o mode=755,nodev
 
+# Wait up to 5s for the mmc device to appear. Continue even if the count is
+# exceeded. A failure will be caught later like in the mount command.
+mmcdev="/dev/mmcblk0"
+count=0
+while [ $count -lt 5 ]; do
+    if [ -e "${mmcdev}" ]; then
+        break
+    fi
+    sleep 1
+    count=$((count + 1))
+done
+
 # Move the secondary GPT to the end of the device if needed. Look for the GPT
 # header signature "EFI PART" located 512 bytes from the end of the device.
-magic=$(tail -c 512 /dev/mmcblk0 | hexdump -C -n 8 | grep "EFI PART")
+magic=$(tail -c 512 "${mmcdev}" | hexdump -C -n 8 | grep "EFI PART")
 if test -z "${magic}"; then
-    sgdisk -e /dev/mmcblk0
+    sgdisk -e "${mmcdev}"
     partprobe
 fi