istoreos/target/linux/amlogic/base-files/lib/upgrade/platform.sh
Liangbin Lian f517288e11 target/amlogic: sysupgrade require image metadata
Signed-off-by: Liangbin Lian <jjm2473@gmail.com>
2025-12-30 23:26:01 +08:00

119 lines
3.4 KiB
Bash

# modified by jjm2473
# 1. keep overlay partition when upgrade
# 2. reset rom uuid in ext_overlay (aka sandbox mode) when upgrade (stage2:istoreos_pre_upgrade)
REQUIRE_IMAGE_METADATA=1
platform_check_part_size() {
local part start size cursize
#iterate over each partition from the image and check boot disk partition size
while read part start size; do
if [ "$part" -ge 3 ]; then
continue
fi
cursize=$(echo $(grep -m1 '^ *'"$part"' *' /tmp/partmap.bootdisk) | cut -d' ' -f 3)
if [ -z "$cursize" ]; then
v "Unable to find partition $part on boot disk"
return 1
fi
if [ "$size" -gt "$cursize" ]; then
v "Partition $part on image is larger than boot disk ($size > $cursize)"
return 1
fi
done < /tmp/partmap.image
return 0
}
platform_check_image() {
local diskdev partdev diff
export_bootdevice && export_partdevice diskdev 0 || {
v "Unable to determine upgrade device"
return 1
}
get_partitions "/dev/$diskdev" bootdisk
#extract the boot sector from the image
get_image "$@" | dd of=/tmp/image.bs count=1 bs=512b 2>/dev/null
get_partitions /tmp/image.bs image
platform_check_part_size || diff=1
rm -f /tmp/image.bs /tmp/partmap.bootdisk /tmp/partmap.image
if [ -n "$diff" ]; then
if echo "$diskdev" | grep -q '^mmcblk' && [ -e "/dev/${diskdev}boot0" ]; then
v "Target is eMMC, but image partition layout has changed, cannot process."
notify_firmware_broken || exit 1
return 1
fi
v "Partition layout has changed. Full image will be written."
ask_bool 0 "Abort" && exit 1
return 0
fi
}
platform_do_upgrade() {
local diskdev partdev diff part start size
export_bootdevice && export_partdevice diskdev 0 || {
v "Unable to determine upgrade device"
return 1
}
sync
get_partitions "/dev/$diskdev" bootdisk
#extract the boot sector from the image
get_image "$@" | dd of=/tmp/image.bs count=1 bs=512b
get_partitions /tmp/image.bs image
if [ "$UPGRADE_OPT_SAVE_PARTITIONS" = "1" -o -n "$UPGRADE_BACKUP" ] || ( echo "$diskdev" | grep -q '^mmcblk' && [ -e "/dev/${diskdev}boot0" ] ); then
platform_check_part_size || diff=1
else
#compare tables
diff="$(grep -F -x -v -f /tmp/partmap.bootdisk /tmp/partmap.image)"
fi
if [ -n "$diff" ]; then
grep /overlay /proc/mounts > /dev/null && {
/bin/mount -o noatime,remount,ro /overlay
/usr/bin/umount -R -d -l /overlay 2>/dev/null || /bin/umount -l /overlay
}
get_image "$@" | dd of="/dev/$diskdev" bs=4096 conv=fsync
# 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
#iterate over each partition from the image and write it to the boot disk
while read part start size; do
if [ -n "$UPGRADE_BACKUP" -a "$part" -ge 3 ]; then
v "Skip partition $part >= 3 when upgrading"
continue
fi
if export_partdevice partdev $part; then
v "Writing image to /dev/$partdev..."
if [ "$part" -eq 3 ]; then
echo "RESET000" | dd of="/dev/$partdev" bs=512 count=1 conv=sync,fsync 2>/dev/null
else
get_image "$@" | dd of="/dev/$partdev" ibs="512" obs=1M skip="$start" count="$size" conv=fsync
fi
else
v "Unable to find partition $part device, skipped."
fi
done < /tmp/partmap.image
#copy partition uuid
v "Writing new UUID to /dev/$diskdev..."
get_image "$@" | dd of="/dev/$diskdev" bs=1 skip=440 count=4 seek=440 conv=fsync
}