Make rockpi image boot from wherever it is installed

Use boot scripts for detecting, and a systemd service for mounting /boot

Change-Id: I70ee352d8d773f92c11564f3a8ddd25800d2bba2
Signed-off-by: Austin Schuh <austin.linux@gmail.com>
diff --git a/frc971/rockpi/build_rootfs.sh b/frc971/rockpi/build_rootfs.sh
index 74d4dc1..dd0f177 100755
--- a/frc971/rockpi/build_rootfs.sh
+++ b/frc971/rockpi/build_rootfs.sh
@@ -16,6 +16,7 @@
     bison
     gcc-arm-none-eabi
     gcc-aarch64-linux-gnu
+    u-boot-tools
     device-tree-compiler
     swig
     debootstrap
@@ -164,14 +165,22 @@
   -C "${PARTITION}/lib/modules/" ./lib/modules
 
 # Now, configure it to start automatically.
-sudo mkdir -p ${PARTITION}/boot/extlinux/
-cat << __EOF__ | sudo tee "${PARTITION}/boot/extlinux/extlinux.conf"
+cat << __EOF__ | sudo tee "${PARTITION}/boot/sdcard_extlinux.conf"
 label Linux ${KERNEL_VERSION}
     kernel /vmlinuz-${KERNEL_VERSION}
     append earlycon=uart8250,mmio32,0xff1a0000 earlyprintk console=ttyS2,1500000n8 root=/dev/mmcblk0p2 ro rootfstype=ext4 rootwait
     fdtdir /dtbs/${KERNEL_VERSION}/
 __EOF__
+cat << __EOF__ | sudo tee "${PARTITION}/boot/emmc_extlinux.conf"
+label Linux ${KERNEL_VERSION}
+    kernel /vmlinuz-${KERNEL_VERSION}
+    append earlycon=uart8250,mmio32,0xff1a0000 earlyprintk console=ttyS2,1500000n8 root=/dev/mmcblk1p2 ro rootfstype=ext4 rootwait
+    fdtdir /dtbs/${KERNEL_VERSION}/
+__EOF__
 
+mkimage -c none -A arm -T script -d contents/boot/boot.script contents/boot/boot.scr
+copyfile root.root 644 boot/boot.scr
+rm contents/boot/boot.scr
 copyfile root.root 644 etc/apt/sources.list.d/bullseye-backports.list
 copyfile root.root 644 etc/apt/sources.list.d/frc971.list
 
@@ -186,7 +195,7 @@
 
 target "apt-get -y install -t bullseye-backports bpfcc-tools"
 
-target "apt-get install -y sudo openssh-server python3 bash-completion git v4l-utils cpufrequtils pmount rsync vim-nox chrony libopencv-calib3d4.5 libopencv-contrib4.5 libopencv-core4.5 libopencv-features2d4.5 libopencv-flann4.5 libopencv-highgui4.5 libopencv-imgcodecs4.5 libopencv-imgproc4.5 libopencv-ml4.5 libopencv-objdetect4.5 libopencv-photo4.5 libopencv-shape4.5 libopencv-stitching4.5 libopencv-superres4.5 libopencv-video4.5 libopencv-videoio4.5 libopencv-videostab4.5 libopencv-viz4.5 libnice10 pmount libnice-dev feh libgstreamer1.0-0 libgstreamer-plugins-base1.0-0 libgstreamer-plugins-bad1.0-0 gstreamer1.0-plugins-base gstreamer1.0-plugins-good gstreamer1.0-plugins-bad gstreamer1.0-plugins-ugly gstreamer1.0-nice usbutils locales trace-cmd clinfo jq"
+target "apt-get install -y sudo openssh-server python3 bash-completion git v4l-utils cpufrequtils pmount rsync vim-nox chrony libopencv-calib3d4.5 libopencv-contrib4.5 libopencv-core4.5 libopencv-features2d4.5 libopencv-flann4.5 libopencv-highgui4.5 libopencv-imgcodecs4.5 libopencv-imgproc4.5 libopencv-ml4.5 libopencv-objdetect4.5 libopencv-photo4.5 libopencv-shape4.5 libopencv-stitching4.5 libopencv-superres4.5 libopencv-video4.5 libopencv-videoio4.5 libopencv-videostab4.5 libopencv-viz4.5 libnice10 pmount libnice-dev feh libgstreamer1.0-0 libgstreamer-plugins-base1.0-0 libgstreamer-plugins-bad1.0-0 gstreamer1.0-plugins-base gstreamer1.0-plugins-good gstreamer1.0-plugins-bad gstreamer1.0-plugins-ugly gstreamer1.0-nice usbutils locales trace-cmd clinfo jq strace"
 target "cd /tmp && wget https://software.frc971.org/Build-Dependencies/libmali-midgard-t86x-r14p0-x11_1.9-1_arm64.deb && sudo dpkg -i libmali-midgard-t86x-r14p0-x11_1.9-1_arm64.deb && rm libmali-midgard-t86x-r14p0-x11_1.9-1_arm64.deb"
 
 target "apt-get clean"
@@ -208,6 +217,7 @@
 copyfile root.root 500 root/bin/deploy_kernel.sh
 copyfile root.root 500 root/bin/chrt.sh
 copyfile root.root 644 etc/systemd/system/grow-rootfs.service
+copyfile root.root 644 etc/systemd/system/mount-boot.service
 copyfile root.root 644 etc/sysctl.d/sctp.conf
 copyfile root.root 644 etc/systemd/logind.conf
 copyfile root.root 555 etc/bash_completion.d/aos_dump_autocomplete
@@ -224,6 +234,7 @@
 
 target "systemctl enable systemd-networkd"
 target "systemctl enable grow-rootfs"
+target "systemctl enable mount-boot"
 target "systemctl enable frc971"
 target "systemctl enable frc971chrt"
 target "/root/bin/change_hostname.sh pi-971-1"
diff --git a/frc971/rockpi/contents/boot/boot.script b/frc971/rockpi/contents/boot/boot.script
new file mode 100644
index 0000000..26732b6
--- /dev/null
+++ b/frc971/rockpi/contents/boot/boot.script
@@ -0,0 +1,6 @@
+if test ${devnum} -eq 1;
+then
+  sysboot ${devtype} ${devnum}:${distro_bootpart} any ${scriptaddr} sdcard_extlinux.conf
+else
+  sysboot ${devtype} ${devnum}:${distro_bootpart} any ${scriptaddr} emmc_extlinux.conf
+fi
diff --git a/frc971/rockpi/contents/etc/systemd/system/mount-boot.service b/frc971/rockpi/contents/etc/systemd/system/mount-boot.service
new file mode 100644
index 0000000..45e5783
--- /dev/null
+++ b/frc971/rockpi/contents/etc/systemd/system/mount-boot.service
@@ -0,0 +1,16 @@
+[Unit]
+Description=Mount /boot
+DefaultDependencies=no
+Before=local-fs.target
+Before=shutdown.target
+After=-.mount
+BindsTo=-.mount
+
+[Service]
+Type=oneshot
+ExecStart=/bin/bash -c "mount $(cat /proc/cmdline | sed 's/.*\(mmcblk.p\).*/\/dev\/\11/') /boot"
+TimeoutSec=0
+RemainAfterExit=yes
+
+[Install]
+WantedBy=multi-user.target