gbmc-load-dtbo: Add package

Add a common mechanism for loading dtbo files as a service. This ensures
that the dtbocfg out of tree module is loaded into the kernel and then
loads the dtbo.

Tested: Booted on a BMC that has overlays in the rootfs that can be
loaded and have them loaded at bootup and later at runtime to verify
devices are probed as expected.

Google-Bug-Id: 540953723
Change-Id: I473a77582d9aa34adeb99a9f9d125b67720209e0
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/recipes-kernel/overlay-dts/gbmc-load-dtbo.bb b/recipes-kernel/overlay-dts/gbmc-load-dtbo.bb
new file mode 100644
index 0000000..9cdbc34
--- /dev/null
+++ b/recipes-kernel/overlay-dts/gbmc-load-dtbo.bb
@@ -0,0 +1,25 @@
+SUMMARY = "Load overlay DTS at runtime"
+PR = "r1"
+LICENSE = "Apache-2.0"
+LIC_FILES_CHKSUM = "file://${COREBASE}/meta/files/common-licenses/Apache-2.0;md5=89aea4e17d99a7cacdbeed46a0096b10"
+
+SRC_URI += " \
+  file://gbmc-load-dtbo.sh \
+  file://gbmc-load-dtbo@.service \
+  "
+
+RDEPENDS:${PN} += "bash dtbocfg"
+
+FILES:${PN} = " \
+  ${bindir}/gbmc-load-dtbo.sh \
+  ${systemd_system_unitdir}/gbmc-load-dtbo@.service \
+  "
+
+do_install() {
+  install -d ${D}/${bindir}
+  install -m0755 ${WORKDIR}/gbmc-load-dtbo.sh ${D}/${bindir}/
+
+  install -d ${D}/${systemd_system_unitdir}
+  install -m0644 ${WORKDIR}/gbmc-load-dtbo@.service ${D}/${systemd_system_unitdir}/
+
+}
diff --git a/recipes-kernel/overlay-dts/gbmc-load-dtbo/gbmc-load-dtbo.sh b/recipes-kernel/overlay-dts/gbmc-load-dtbo/gbmc-load-dtbo.sh
new file mode 100644
index 0000000..9d08d54
--- /dev/null
+++ b/recipes-kernel/overlay-dts/gbmc-load-dtbo/gbmc-load-dtbo.sh
@@ -0,0 +1,30 @@
+#!/bin/bash
+dtbo_name="${1:?Missing DTBO name}"
+dtbo=/usr/share/overlay-dts/"${dtbo_name}".dtbo
+if [[ ! -e "$dtbo" ]]; then
+  echo "DTBO file missing: $dtbo" >&2
+  exit 1
+fi
+
+modprobe -v dtbocfg || exit
+overlays=/sys/kernel/config/device-tree/overlays
+if [[ ! -e  "$overlays" ]]; then
+  END=$((SECONDS+10))
+  echo 'Waiting for dtbocfg to set up' >&2
+  while [[ ! -e "$overlays" ]]; do
+    if (( SECONDS >= END )); then
+      echo 'Timed out waiting for dtbocfg' >&2
+      exit 1
+    fi
+    sleep 0.1
+  done
+fi
+
+dtbodir="$overlays"/"$dtbo_name"
+[[ -d "$dtbodir" ]] || mkdir -v "$dtbodir" || exit
+if [[ "$(cat "$dtbodir"/status 2>/dev/null)" != "1" ]]; then
+  echo "Writing dtbo $dtbo_name" >&2
+  cat "$dtbo" >"$dtbodir"/dtbo
+  echo "Enabling dtbo $dtbo_name" >&2
+  echo 1 >"$dtbodir"/status
+fi
diff --git a/recipes-kernel/overlay-dts/gbmc-load-dtbo/gbmc-load-dtbo@.service b/recipes-kernel/overlay-dts/gbmc-load-dtbo/gbmc-load-dtbo@.service
new file mode 100644
index 0000000..6cea5f2
--- /dev/null
+++ b/recipes-kernel/overlay-dts/gbmc-load-dtbo/gbmc-load-dtbo@.service
@@ -0,0 +1,7 @@
+[Unit]
+DefaultDependencies=no
+
+[Service]
+Type=oneshot
+RemainAfterExit=yes
+ExecStart=gbmc-load-dtbo.sh "%I"