Add postinst/prerm scripts and service for u-boot

- Add a uboot-imx systemd service: this checks the bootloader on eMMC,
and replaces it with the packages version if they differ.
- Enable the service during postinst, and if the boot partition exists
(on device, as opposed to a chroot), go ahead and start the service as
well.
- Disable the service in prerm.

Change-Id: I539726973036a34897770f7df004b36d3656deee
diff --git a/debian/lib/systemd/system/uboot-imx.service b/debian/lib/systemd/system/uboot-imx.service
new file mode 100644
index 0000000..326787d
--- /dev/null
+++ b/debian/lib/systemd/system/uboot-imx.service
@@ -0,0 +1,8 @@
+[Unit]
+Description=U-Boot install service
+
+[Service]
+ExecStart=/usr/bin/install-uboot.sh
+
+[Install]
+WantedBy=multi-user.target
\ No newline at end of file
diff --git a/debian/postinst b/debian/postinst
new file mode 100644
index 0000000..f991013
--- /dev/null
+++ b/debian/postinst
@@ -0,0 +1,5 @@
+#!/bin/bash
+systemctl enable uboot-imx
+if [[ -e /dev/mmcblk0boot0 ]]; then
+  systemctl start uboot-imx
+fi
\ No newline at end of file
diff --git a/debian/prerm b/debian/prerm
new file mode 100644
index 0000000..3c2c005
--- /dev/null
+++ b/debian/prerm
@@ -0,0 +1,2 @@
+#!/bin/bash
+systemctl disable uboot-imx
\ No newline at end of file
diff --git a/debian/rules b/debian/rules
index f92979f..07afe26 100755
--- a/debian/rules
+++ b/debian/rules
@@ -35,3 +35,4 @@
 override_dh_install:
 	install -d debian/uboot-imx/boot
 	install -m 644 -o 0 -g 0 u-boot.imx debian/uboot-imx/boot/u-boot.imx
+	dh_install
diff --git a/debian/uboot-imx.install b/debian/uboot-imx.install
new file mode 100644
index 0000000..b8ba0bf
--- /dev/null
+++ b/debian/uboot-imx.install
@@ -0,0 +1,2 @@
+debian/usr /
+debian/lib /
diff --git a/debian/usr/bin/install-uboot.sh b/debian/usr/bin/install-uboot.sh
new file mode 100755
index 0000000..1a7e56f
--- /dev/null
+++ b/debian/usr/bin/install-uboot.sh
@@ -0,0 +1,52 @@
+#!/bin/bash
+
+BOOT_PART=mmcblk0boot0
+MMCBLK_RO_PATH=/sys/block/${BOOT_PART}/force_ro
+
+UBOOT_PACKAGE_SIZE=$(stat -c%s /boot/u-boot.imx)
+
+function check_emmc_matches {
+  local UBOOT_CURRENT=$(mktemp)
+  dd if=/dev/${BOOT_PART} of=${UBOOT_CURRENT} \
+    count=${UBOOT_PACKAGE_SIZE} \
+    bs=512 \
+    skip=66 \
+    iflag=count_bytes
+  diff ${UBOOT_CURRENT} /boot/u-boot.imx
+  local DIFF_RETURN_CODE=$?
+  rm ${UBOOT_CURRENT}
+  return ${DIFF_RETURN_CODE}
+}
+
+# Check if the u-boot on eMMC is the same as u-boot from the package
+check_emmc_matches
+
+if [[ $? -eq 0 ]]; then
+  exit 0
+fi
+
+# Get the read-only setting for the bootloader
+MMCBLK_RO=$(cat ${MMCBLK_RO_PATH})
+
+# Disable read-only on bootloader block device
+echo 0 > ${MMCBLK_RO_PATH}
+
+UPDATE_SUCCESS=false
+# Write the bootloader image into the block device
+for i in `seq 1 5`
+do
+  dd if=/boot/u-boot.imx of=/dev/${BOOT_PART} bs=512 seek=66
+  check_emmc_matches
+  if [[ $? -eq 0 ]]; then
+    UPDATE_SUCCESS=true
+    break
+  fi
+done
+
+# Restore whatever the setting was before
+echo ${MMCBLK_RO} > ${MMCBLK_RO_PATH}
+
+if [[ "${UPDATE_SUCCESS}" != true ]]; then
+  echo "Failed to update u-boot! Rebooting is unsafe!"
+  exit 1
+fi
\ No newline at end of file