fitblk: move shell functions to common file
authorDaniel Golle <daniel@makrotopia.org>
Wed, 17 Jul 2024 15:22:10 +0000 (16:22 +0100)
committerDaniel Golle <daniel@makrotopia.org>
Thu, 18 Jul 2024 10:48:53 +0000 (11:48 +0100)
Move shell functions used for sysupgrade into /lib/upgrade/fit.sh.
Introduce improved fitblk boot device detection function which
works also in case ubiblock devices have not yet been created or
even UBI itself not yet being attached.

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
package/utils/fitblk/Makefile
package/utils/fitblk/files/fit.sh [new file with mode: 0644]

index 4da4dc46f1fdca018a4b41b5f25147c4e76880d3..8fb6e14c7e0532d939e1b24aa738ac3f1c3e9614 100644 (file)
@@ -1,7 +1,7 @@
 include $(TOPDIR)/rules.mk
 
 PKG_NAME:=fitblk
-PKG_RELEASE:=1
+PKG_RELEASE:=2
 PKG_LICENSE:=GPL-2.0-only
 PKG_MAINTAINER:=Daniel Golle <daniel@makrotopia.org>
 
@@ -36,6 +36,8 @@ endef
 define Package/fitblk/install
        $(INSTALL_DIR) $(1)/usr/sbin
        $(INSTALL_BIN) $(PKG_BUILD_DIR)/fitblk $(1)/usr/sbin/
+       $(INSTALL_DIR) $(1)/lib/upgrade
+       $(INSTALL_DATA) ./files/fit.sh $(1)/lib/upgrade
 endef
 
 $(eval $(call BuildPackage,fitblk))
diff --git a/package/utils/fitblk/files/fit.sh b/package/utils/fitblk/files/fit.sh
new file mode 100644 (file)
index 0000000..b715a15
--- /dev/null
@@ -0,0 +1,63 @@
+export_fitblk_bootdev() {
+       [ -e /sys/firmware/devicetree/base/chosen/rootdisk ] || return
+
+       local rootdisk="$(cat /sys/firmware/devicetree/base/chosen/rootdisk)"
+       local handle bootdev
+
+       for handle in /sys/class/mtd/mtd*/of_node/volumes/*/phandle; do
+               [ ! -e "$handle" ] && continue
+               if [ "$rootdisk" = "$(cat "$handle")" ]; then
+                       if [ -e "${handle%/phandle}/volname" ]; then
+                               export CI_KERNPART="$(cat "${handle%/phandle}/volname")"
+                       elif [ -e "${handle%/phandle}/volid" ]; then
+                               export CI_KERNVOLID="$(cat "${handle%/phandle}/volid")"
+                       else
+                               return
+                       fi
+                       export CI_UBIPART="$(cat "${handle%%/of_node*}/name")"
+                       export CI_METHOD="ubi"
+                       return
+               fi
+       done
+
+       for handle in /sys/class/mtd/mtd*/of_node/phandle; do
+               [ ! -e "$handle" ] && continue
+               if [ "$rootdisk" = "$(cat $handle)" ]; then
+                       bootdev="${handle%/of_node/phandle}"
+                       bootdev="${bootdev#/sys/class/mtd/}"
+                       export PART_NAME="/dev/$bootdev"
+                       export CI_METHOD="default"
+                       return
+               fi
+       done
+
+       for handle in /sys/class/block/*/of_node/phandle; do
+               [ ! -e "$handle" ] && continue
+               if [ "$rootdisk" = "$(cat $handle)" ]; then
+                       bootdev="${handle%/of_node/phandle}"
+                       bootdev="${bootdev#/sys/class/block/}"
+                       export EMMC_KERN_DEV="/dev/$bootdev"
+                       export CI_METHOD="emmc"
+                       return
+               fi
+       done
+}
+
+fit_do_upgrade() {
+       export_fitblk_bootdev
+       [ -n "$CI_METHOD" ] || return 1
+       [ -e /dev/fit0 ] && fitblk /dev/fit0
+       [ -e /dev/fitrw ] && fitblk /dev/fitrw
+
+       case "$CI_METHOD" in
+       emmc)
+               emmc_do_upgrade "$1"
+               ;;
+       default)
+               default_do_upgrade "$1"
+               ;;
+       ubi)
+               nand_do_upgrade "$1"
+               ;;
+       esac
+}