|
| 1 | +REQUIRE_IMAGE_METADATA=1 |
| 2 | + |
| 3 | +UBOOT_ENV_PART=3 |
| 4 | +BOOT_PART=4 |
| 5 | +ROOTFS_PART=5 |
| 6 | + |
| 7 | +RAMFS_COPY_BIN='blockdev' |
| 8 | + |
| 9 | +export_bootdevice() { |
| 10 | + local cmdline uuid blockdev uevent line class |
| 11 | + local MAJOR MINOR DEVNAME DEVTYPE |
| 12 | + local rootpart="$(cmdline_get_var root)" |
| 13 | + |
| 14 | + case "$rootpart" in |
| 15 | + PARTUUID=????????-????-????-????-??????????0?/PARTNROFF=1 | \ |
| 16 | + PARTUUID=????????-????-????-????-??????????05) |
| 17 | + uuid="${rootpart#PARTUUID=}" |
| 18 | + uuid="${uuid%/PARTNROFF=1}" |
| 19 | + uuid="${uuid%0?}00" |
| 20 | + for disk in $(find /dev -type b); do |
| 21 | + set -- $(dd if=$disk bs=1 skip=568 count=16 2>/dev/null | hexdump -v -e '8/1 "%02x "" "2/1 "%02x""-"6/1 "%02x"') |
| 22 | + if [ "$4$3$2$1-$6$5-$8$7-$9" = "$uuid" ]; then |
| 23 | + uevent="/sys/class/block/${disk##*/}/uevent" |
| 24 | + break |
| 25 | + fi |
| 26 | + done |
| 27 | + ;; |
| 28 | + esac |
| 29 | + |
| 30 | + if [ -e "$uevent" ]; then |
| 31 | + while read line; do |
| 32 | + export -n "$line" |
| 33 | + done < "$uevent" |
| 34 | + export BOOTDEV_MAJOR=$MAJOR |
| 35 | + export BOOTDEV_MINOR=$MINOR |
| 36 | + return 0 |
| 37 | + fi |
| 38 | + |
| 39 | + return 1 |
| 40 | +} |
| 41 | + |
| 42 | +platform_check_image() { |
| 43 | + local diskdev partdev diff |
| 44 | + |
| 45 | + [ "$#" -gt 1 ] && return 1 |
| 46 | + |
| 47 | + export_bootdevice && export_partdevice diskdev 0 || { |
| 48 | + v "platform_check_image: Unable to determine upgrade device" |
| 49 | + return 1 |
| 50 | + } |
| 51 | + |
| 52 | + get_partitions "/dev/$diskdev" bootdisk |
| 53 | + |
| 54 | + v "Extract the boot sector from the image" |
| 55 | + get_image_dd "$1" of=/tmp/image.bs count=63 bs=512b |
| 56 | + |
| 57 | + get_partitions /tmp/image.bs image |
| 58 | + |
| 59 | + #compare tables |
| 60 | + diff="$(grep -F -x -v -f /tmp/partmap.bootdisk /tmp/partmap.image)" |
| 61 | + |
| 62 | + rm -f /tmp/image.bs /tmp/partmap.bootdisk /tmp/partmap.image |
| 63 | + |
| 64 | + if [ -n "$diff" ]; then |
| 65 | + echo "Partition layout has changed. Full image will be written." |
| 66 | + ask_bool 0 "Abort" && exit 1 |
| 67 | + return 0 |
| 68 | + fi |
| 69 | +} |
| 70 | + |
| 71 | +platform_do_upgrade() { |
| 72 | + local diskdev partdev diff partlabel |
| 73 | + |
| 74 | + export_bootdevice && export_partdevice diskdev 0 || { |
| 75 | + v "platform_do_upgrade: Unable to determine upgrade device" |
| 76 | + return 1 |
| 77 | + } |
| 78 | + |
| 79 | + sync |
| 80 | + |
| 81 | + if [ "$UPGRADE_OPT_SAVE_PARTITIONS" = "1" ]; then |
| 82 | + get_partitions "/dev/$diskdev" bootdisk |
| 83 | + |
| 84 | + v "Extract boot sector from the image" |
| 85 | + get_image_dd "$1" of=/tmp/image.bs count=63 bs=512b |
| 86 | + |
| 87 | + get_partitions /tmp/image.bs image |
| 88 | + |
| 89 | + #compare tables |
| 90 | + diff="$(grep -F -x -v -f /tmp/partmap.bootdisk /tmp/partmap.image)" |
| 91 | + else |
| 92 | + diff=1 |
| 93 | + fi |
| 94 | + |
| 95 | + if [ -n "$diff" ]; then |
| 96 | + rm -rf /tmp/ubootenv |
| 97 | + |
| 98 | + if export_partdevice partdev $UBOOT_ENV_PART; then |
| 99 | + v "Saving u-boot env (/dev/$partdev) before to write image" |
| 100 | + get_image_dd "/dev/$partdev" of=/tmp/ubootenv |
| 101 | + fi |
| 102 | + |
| 103 | + v "Writing image to /dev/$diskdev..." |
| 104 | + get_image_dd "$1" of="/dev/$diskdev" conv=fsync |
| 105 | + |
| 106 | + blockdev --rereadpt "/dev/$diskdev" |
| 107 | + |
| 108 | + [ -f /tmp/ubootenv ] && { |
| 109 | + # iterate over each partition from the image to find the |
| 110 | + # u-boot-env partition and restore u-boot env. |
| 111 | + while read part start size; do |
| 112 | + if export_partdevice partdev $part; then |
| 113 | + while read line; do |
| 114 | + eval "local l$line" |
| 115 | + done < "/sys/class/block/$partdev/uevent" |
| 116 | + |
| 117 | + [ "$lPARTNAME" = "u-boot-env" ] || continue |
| 118 | + |
| 119 | + v "Writting u-boot env to /dev/$partdev" |
| 120 | + get_image_dd /tmp/ubootenv of="/dev/$partdev" conv=fsync |
| 121 | + |
| 122 | + return 0 |
| 123 | + fi |
| 124 | + done < /tmp/partmap.image |
| 125 | + } |
| 126 | + return 0 |
| 127 | + fi |
| 128 | + |
| 129 | + #iterate over each partition from the image and write it to the boot disk |
| 130 | + while read part start size; do |
| 131 | + if export_partdevice partdev $part; then |
| 132 | + # do not erase u-boot env |
| 133 | + [ "$part" = "$UBOOT_ENV_PART" ] && continue |
| 134 | + |
| 135 | + v "Writing image to /dev/$partdev..." |
| 136 | + v "Normal partition, doing DD" |
| 137 | + get_image_dd "$1" of="/dev/$partdev" ibs=512 obs=1M skip="$start" \ |
| 138 | + count="$size" conv=fsync |
| 139 | + else |
| 140 | + v "Unable to find partition $part device, skipped." |
| 141 | + fi |
| 142 | + done < /tmp/partmap.image |
| 143 | + |
| 144 | + if export_partdevice partdev "$BOOT_PART"; then |
| 145 | + mount -t ext4 -o rw,noatime "/dev/$partdev" /mnt |
| 146 | + local partuuid="$(cmdline_get_var root)" |
| 147 | + v "Setting rootfs ${partuuid}" |
| 148 | + sed -i "s/PARTUUID=[a-f0-9-]\+/${partuuid}/ig" \ |
| 149 | + /mnt/extlinux/extlinux.conf |
| 150 | + umount /mnt |
| 151 | + fi |
| 152 | +} |
| 153 | + |
| 154 | +platform_copy_config() { |
| 155 | + local partdev |
| 156 | + |
| 157 | + # Iterate over each partition from the image to find the boot partition |
| 158 | + # and copy the config tarball. |
| 159 | + # The partlabel is used to find the partition. |
| 160 | + # An hardcoded partition number cannot be used, as it could be wrong if |
| 161 | + # the partition table changed, and the full image was written. |
| 162 | + while read part start size; do |
| 163 | + # config is copied in the boot partition, as for squashfs image, the |
| 164 | + # rootfs partition is not writable. |
| 165 | + if export_partdevice partdev "$part"; then |
| 166 | + while read line; do |
| 167 | + eval "local l$line" |
| 168 | + done < "/sys/class/block/$partdev/uevent" |
| 169 | + |
| 170 | + [ "$lPARTNAME" = "boot" ] || continue |
| 171 | + |
| 172 | + mount -t ext4 -o rw,noatime "/dev/$partdev" /mnt |
| 173 | + cp -af "$UPGRADE_BACKUP" "/mnt/$BACKUP_FILE" |
| 174 | + umount /mnt |
| 175 | + return 0 |
| 176 | + else |
| 177 | + v "ERROR: Unable to find partition to copy config data to" |
| 178 | + fi |
| 179 | + done < /tmp/partmap.image |
| 180 | +} |
| 181 | + |
0 commit comments