layerscape: support sysupgrade for SD card ext4 rootfs
authorYangbo Lu <yangbo.lu@nxp.com>
Thu, 28 May 2020 08:02:28 +0000 (16:02 +0800)
committerPetr Štetiar <ynezz@true.cz>
Sat, 11 Jul 2020 12:44:23 +0000 (14:44 +0200)
Support sysupgrade for SD card ext4 rootfs.

Signed-off-by: Yangbo Lu <yangbo.lu@nxp.com>
target/linux/layerscape/Makefile
target/linux/layerscape/base-files/lib/upgrade/platform.sh
target/linux/layerscape/image/armv7.mk
target/linux/layerscape/image/armv8_64b.mk

index 476297a678e3ab7a644960e685db7dde01e5f551..c5b27ad0c7c33adafe4dda551888f0186dcfd7d2 100644 (file)
@@ -18,6 +18,7 @@ endef
 
 include $(INCLUDE_DIR)/target.mk
 
-DEFAULT_PACKAGES += kmod-usb3 kmod-usb-dwc3 kmod-usb-storage
+DEFAULT_PACKAGES += kmod-usb3 kmod-usb-dwc3 kmod-usb-storage \
+  partx-utils
 
 $(eval $(call BuildTarget))
index 9b8c07138fa77574a7314b17f3fe517af2d78e74..8a136d9439f632f29aa448bed6c893b5556fe3b4 100644 (file)
@@ -8,6 +8,85 @@ RAMFS_COPY_DATA="/etc/fw_env.config /var/lock/fw_printenv.lock"
 
 REQUIRE_IMAGE_METADATA=1
 
+platform_check_image_sdboot() {
+       local diskdev partdev diff
+
+       export_bootdevice && export_partdevice diskdev 0 || {
+               echo "Unable to determine upgrade device"
+               return 1
+       }
+
+       # get partitions table from boot device
+       get_partitions "/dev/$diskdev" bootdisk
+
+       # get partitions table from sysupgrade.bin
+       dd if="$1" of=/tmp/image.bs bs=512b count=1 > /dev/null 2>&1
+       sync
+       get_partitions /tmp/image.bs image
+
+       # compare tables
+       diff="$(grep -F -x -v -f /tmp/partmap.bootdisk /tmp/partmap.image)"
+
+       rm -f /tmp/image.bs /tmp/partmap.bootdisk /tmp/partmap.image
+
+       if [ -n "$diff" ]; then
+               echo "Partition layout has changed. Full image will be written."
+               ask_bool 0 "Abort" && exit 1
+               return 0
+       fi
+}
+platform_do_upgrade_sdboot() {
+       local diskdev partdev diff
+
+       export_bootdevice && export_partdevice diskdev 0 || {
+               echo "Unable to determine upgrade device"
+               return 1
+       }
+
+       if [ "$UPGRADE_OPT_SAVE_PARTITIONS" = "1" ]; then
+               # get partitions table from boot device
+               get_partitions "/dev/$diskdev" bootdisk
+
+               # get partitions table from sysupgrade.bin
+               dd if="$1" of=/tmp/image.bs bs=512b count=1 > /dev/null 2>&1
+               sync
+               get_partitions /tmp/image.bs image
+
+               # compare tables
+               diff="$(grep -F -x -v -f /tmp/partmap.bootdisk /tmp/partmap.image)"
+       else
+               diff=1
+       fi
+
+       if [ -n "$diff" ]; then
+               dd if="$1" of="/dev/$diskdev" bs=1024 count=4 > /dev/null 2>&1
+               dd if="$1" of="$diskdev" bs=1024 skip=4 seek=16384 > /dev/null 2>&1
+               sync
+
+               # Separate removal and addtion is necessary; otherwise, partition 1
+               # will be missing if it overlaps with the old partition 2
+               partx -d - "/dev/$diskdev"
+               partx -a - "/dev/$diskdev"
+
+               return 0
+       fi
+
+       # write kernel image
+       dd if="$1" of="$diskdev" bs=1024 skip=4 seek=16384 count=16384 > /dev/null 2>&1
+       sync
+
+       # iterate over each partition from the image and write it to the boot disk
+       while read part start size; do
+               if export_partdevice partdev $part; then
+                       echo "Writing image to /dev/$partdev..."
+                       dd if="$1" of="/dev/$partdev" bs=512 skip="$start" count="$size" > /dev/null 2>&1
+                       sync
+               else
+                       echo "Unable to find partition $part device, skipped."
+               fi
+       done < /tmp/partmap.image
+
+}
 platform_do_upgrade_traverse_nandubi() {
        bootsys=$(fw_printenv bootsys | awk -F= '{{print $2}}')
        newbootsys=2
@@ -25,6 +104,15 @@ platform_do_upgrade_traverse_nandubi() {
        nand_do_upgrade "$1" || (echo "Upgrade failed, setting bootsys ${bootsys}" && fw_setenv bootsys $bootsys)
 
 }
+platform_copy_config() {
+       local partdev parttype=ext4
+
+       if export_partdevice partdev 1; then
+               mount -t $parttype -o rw,noatime "/dev/$partdev" /mnt
+               cp -af "$UPGRADE_BACKUP" "/mnt/$BACKUP_FILE"
+               umount /mnt
+       fi
+}
 platform_check_image() {
        local board=$(board_name)
 
@@ -43,6 +131,14 @@ platform_check_image() {
        fsl,ls2088a-rdb)
                return 0
                ;;
+       fsl,ls1012a-frwy-sdboot | \
+       fsl,ls1021a-twr-sdboot | \
+       fsl,ls1043a-rdb-sdboot | \
+       fsl,ls1046a-rdb-sdboot | \
+       fsl,ls1088a-rdb-sdboot)
+               platform_check_image_sdboot "$1"
+               return 0
+               ;;
        *)
                echo "Sysupgrade is not currently supported on $board"
                ;;
@@ -72,6 +168,14 @@ platform_do_upgrade() {
                PART_NAME=firmware
                default_do_upgrade "$1"
                ;;
+       fsl,ls1012a-frwy-sdboot | \
+       fsl,ls1021a-twr-sdboot | \
+       fsl,ls1043a-rdb-sdboot | \
+       fsl,ls1046a-rdb-sdboot | \
+       fsl,ls1088a-rdb-sdboot)
+               platform_do_upgrade_sdboot "$1"
+               return 0
+               ;;
        *)
                echo "Sysupgrade is not currently supported on $board"
                ;;
index 27246740d570a7d4ec6be7679d96ac896582f9f4..9bcf72f2cb17861177817126e69e4cf2ffd730d7 100644 (file)
@@ -41,7 +41,7 @@ define Device/ls1021atwr-sdboot
   DEVICE_VARIANT := SD Card Boot
   DEVICE_DTS := ls1021a-twr
   FILESYSTEMS := ext4
-  IMAGES := sdcard.img
+  IMAGES := sdcard.img sysupgrade.bin
   IMAGE/sdcard.img := \
     ls-clean | \
     ls-append-sdhead $(1) | pad-to 4K | \
@@ -50,6 +50,12 @@ define Device/ls1021atwr-sdboot
     ls-append-dtb $$(DEVICE_DTS) | pad-to 16M | \
     append-kernel | pad-to $(LS_SD_ROOTFSPART_OFFSET)M | \
     append-rootfs | check-size $(LS_SD_IMAGE_SIZE)
+  IMAGE/sysupgrade.bin := \
+    ls-clean | \
+    ls-append-sdhead $(1) | pad-to 16M | \
+    append-kernel | pad-to $(LS_SD_ROOTFSPART_OFFSET)M | \
+    append-rootfs | check-size $(LS_SD_IMAGE_SIZE) | append-metadata
+  SUPPORTED_DEVICES := fsl,ls1021a-twr-sdboot
 endef
 TARGET_DEVICES += ls1021atwr-sdboot
 
index b7208b4c8f51ab7c0f339e6109c15c51187e430f..9512e0d8ad55157704d14bdeb6fdf3dcf5f5ec62 100644 (file)
@@ -72,7 +72,7 @@ define Device/ls1012afrwy
     kmod-ppfe
   DEVICE_DTS := freescale/fsl-ls1012a-frwy
   FILESYSTEMS := ext4
-  IMAGES := firmware.bin sdcard.img
+  IMAGES := firmware.bin sdcard.img sysupgrade.bin
   IMAGE/firmware.bin := \
     ls-clean | \
     ls-append $(1)-bl2.pbl | pad-to 128K | \
@@ -86,6 +86,12 @@ define Device/ls1012afrwy
     ls-append-dtb $$(DEVICE_DTS) | pad-to 16M | \
     append-kernel | pad-to $(LS_SD_ROOTFSPART_OFFSET)M | \
     append-rootfs | check-size $(LS_SD_IMAGE_SIZE)
+  IMAGE/sysupgrade.bin := \
+    ls-clean | \
+    ls-append-sdhead $(1) | pad-to 16M | \
+    append-kernel | pad-to $(LS_SD_ROOTFSPART_OFFSET)M | \
+    append-rootfs | check-size $(LS_SD_IMAGE_SIZE) | append-metadata
+  SUPPORTED_DEVICES := fsl,ls1012a-frwy-sdboot
 endef
 TARGET_DEVICES += ls1012afrwy
 
@@ -121,7 +127,7 @@ define Device/ls1043ardb-sdboot
     fmc fmc-eth-config
   DEVICE_DTS := freescale/fsl-ls1043a-rdb-sdk
   FILESYSTEMS := ext4
-  IMAGES := sdcard.img
+  IMAGES := sdcard.img sysupgrade.bin
   IMAGE/sdcard.img := \
     ls-clean | \
     ls-append-sdhead $(1) | pad-to 4K | \
@@ -132,6 +138,12 @@ define Device/ls1043ardb-sdboot
     ls-append-dtb $$(DEVICE_DTS) | pad-to 16M | \
     append-kernel | pad-to $(LS_SD_ROOTFSPART_OFFSET)M | \
     append-rootfs | check-size $(LS_SD_IMAGE_SIZE)
+  IMAGE/sysupgrade.bin := \
+    ls-clean | \
+    ls-append-sdhead $(1) | pad-to 16M | \
+    append-kernel | pad-to $(LS_SD_ROOTFSPART_OFFSET)M | \
+    append-rootfs | check-size $(LS_SD_IMAGE_SIZE) | append-metadata
+  SUPPORTED_DEVICES := fsl,ls1043a-rdb-sdboot
 endef
 TARGET_DEVICES += ls1043ardb-sdboot
 
@@ -167,7 +179,7 @@ define Device/ls1046ardb-sdboot
     fmc fmc-eth-config
   DEVICE_DTS := freescale/fsl-ls1046a-rdb-sdk
   FILESYSTEMS := ext4
-  IMAGES := sdcard.img
+  IMAGES := sdcard.img sysupgrade.bin
   IMAGE/sdcard.img := \
     ls-clean | \
     ls-append-sdhead $(1) | pad-to 4K | \
@@ -178,6 +190,12 @@ define Device/ls1046ardb-sdboot
     ls-append-dtb $$(DEVICE_DTS) | pad-to 16M | \
     append-kernel | pad-to $(LS_SD_ROOTFSPART_OFFSET)M | \
     append-rootfs | check-size $(LS_SD_IMAGE_SIZE)
+  IMAGE/sysupgrade.bin := \
+    ls-clean | \
+    ls-append-sdhead $(1) | pad-to 16M | \
+    append-kernel | pad-to $(LS_SD_ROOTFSPART_OFFSET)M | \
+    append-rootfs | check-size $(LS_SD_IMAGE_SIZE) | append-metadata
+  SUPPORTED_DEVICES := fsl,ls1046a-rdb-sdboot
 endef
 TARGET_DEVICES += ls1046ardb-sdboot
 
@@ -217,7 +235,7 @@ define Device/ls1088ardb-sdboot
     restool
   DEVICE_DTS := freescale/fsl-ls1088a-rdb
   FILESYSTEMS := ext4
-  IMAGES := sdcard.img
+  IMAGES := sdcard.img sysupgrade.bin
   IMAGE/sdcard.img := \
     ls-clean | \
     ls-append-sdhead $(1) | pad-to 4K | \
@@ -230,6 +248,12 @@ define Device/ls1088ardb-sdboot
     ls-append-dtb $$(DEVICE_DTS) | pad-to 16M | \
     append-kernel | pad-to $(LS_SD_ROOTFSPART_OFFSET)M | \
     append-rootfs | check-size $(LS_SD_IMAGE_SIZE)
+  IMAGE/sysupgrade.bin := \
+    ls-clean | \
+    ls-append-sdhead $(1) | pad-to 16M | \
+    append-kernel | pad-to $(LS_SD_ROOTFSPART_OFFSET)M | \
+    append-rootfs | check-size $(LS_SD_IMAGE_SIZE) | append-metadata
+  SUPPORTED_DEVICES := fsl,ls1088a-rdb-sdboot
 endef
 TARGET_DEVICES += ls1088ardb-sdboot