armvirt: remove 5.15 configs
[openwrt/openwrt.git] / target / linux / armvirt / base-files / lib / upgrade / platform.sh
1 # SPDX-License-Identifier: GPL-2.0-or-later
2
3 RAMFS_COPY_BIN="/usr/sbin/blkid"
4
5 platform_check_image() {
6 local board=$(board_name)
7 local diskdev partdev diff
8 [ "$#" -gt 1 ] && return 1
9
10 v "Board is ${board}"
11
12 export_bootdevice && export_partdevice diskdev 0 || {
13 v "platform_check_image: Unable to determine upgrade device"
14 return 1
15 }
16
17 get_partitions "/dev/$diskdev" bootdisk
18
19 v "Extract boot sector from the image"
20 get_image_dd "$1" of=/tmp/image.bs count=63 bs=512b
21
22 get_partitions /tmp/image.bs image
23
24 #compare tables
25 diff="$(grep -F -x -v -f /tmp/partmap.bootdisk /tmp/partmap.image)"
26
27 rm -f /tmp/image.bs /tmp/partmap.bootdisk /tmp/partmap.image
28
29 if [ -n "$diff" ]; then
30 v "Partition layout has changed. Full image will be written."
31 ask_bool 0 "Abort" && exit 1
32 return 0
33 fi
34 }
35
36 platform_copy_config() {
37 local partdev parttype=ext4
38
39 if export_partdevice partdev 2; then
40 mount -t $parttype -o rw,noatime "/dev/$partdev" /mnt
41 cp -af "$UPGRADE_BACKUP" "/mnt/$BACKUP_FILE"
42 umount /mnt
43 else
44 v "ERROR: Unable to find partition to copy config data to"
45 fi
46
47 sleep 5
48 }
49
50 # To avoid writing over any firmware
51 # files (e.g ubootefi.var or firmware/X/ aka EBBR)
52 # Copy efi/openwrt and efi/boot from the new image
53 # to the existing ESP
54 platform_do_upgrade_efi_system_partition() {
55 local image_file=$1
56 local target_partdev=$2
57 local image_efisp_start=$3
58 local image_efisp_size=$4
59
60 v "Updating ESP on ${target_partdev}"
61 NEW_ESP_DIR="/mnt/new_esp_loop"
62 CUR_ESP_DIR="/mnt/cur_esp"
63 mkdir "${NEW_ESP_DIR}"
64 mkdir "${CUR_ESP_DIR}"
65
66 get_image_dd "$image_file" of="/tmp/new_efi_sys_part.img" \
67 skip="$image_efisp_start" count="$image_efisp_size"
68
69 mount -t vfat -o loop -o ro /tmp/new_efi_sys_part.img "${NEW_ESP_DIR}"
70 if [ ! -d "${NEW_ESP_DIR}/efi/boot" ]; then
71 v "ERROR: Image does not contain EFI boot files (/efi/boot)"
72 return 1
73 fi
74
75 mount -t vfat "/dev/$partdev" "${CUR_ESP_DIR}"
76
77 for d in $(find "${NEW_ESP_DIR}/efi/" -mindepth 1 -maxdepth 1 -type d); do
78 v "Copying ${d}"
79 newdir_bname=$(basename "${d}")
80 rm -rf "${CUR_ESP_DIR}/efi/${newdir_bname}"
81 cp -r "${d}" "${CUR_ESP_DIR}/efi"
82 done
83
84 umount "${NEW_ESP_DIR}"
85 umount "${CUR_ESP_DIR}"
86 }
87
88 platform_do_upgrade() {
89 local board=$(board_name)
90 local diskdev partdev diff
91
92 export_bootdevice && export_partdevice diskdev 0 || {
93 v "platform_do_upgrade: Unable to determine upgrade device"
94 return 1
95 }
96
97 sync
98
99 if [ "$UPGRADE_OPT_SAVE_PARTITIONS" = "1" ]; then
100 get_partitions "/dev/$diskdev" bootdisk
101
102 v "Extract boot sector from the image"
103 get_image_dd "$1" of=/tmp/image.bs count=63 bs=512b
104
105 get_partitions /tmp/image.bs image
106
107 #compare tables
108 diff="$(grep -F -x -v -f /tmp/partmap.bootdisk /tmp/partmap.image)"
109 else
110 diff=1
111 fi
112
113 # Only change the partition table if sysupgrade -p is set,
114 # otherwise doing so could interfere with embedded "single storage"
115 # (e.g SoC boot from SD card) setups, as well as other user
116 # created storage (like uvol)
117 if [ -n "$diff" ] && [ "${UPGRADE_OPT_SAVE_PARTITIONS}" = "0" ]; then
118 # Need to remove partitions before dd, otherwise the partitions
119 # that are added after will have minor numbers offset
120 partx -d - "/dev/$diskdev"
121
122 get_image_dd "$1" of="/dev/$diskdev" bs=4096 conv=fsync
123
124 # Separate removal and addtion is necessary; otherwise, partition 1
125 # will be missing if it overlaps with the old partition 2
126 partx -a - "/dev/$diskdev"
127
128 return 0
129 fi
130
131 #iterate over each partition from the image and write it to the boot disk
132 while read part start size; do
133 if export_partdevice partdev $part; then
134 v "Writing image to /dev/$partdev..."
135 if [ "$part" = "1" ]; then
136 platform_do_upgrade_efi_system_partition \
137 $1 $partdev $start $size || return 1
138 else
139 v "Normal partition, doing DD"
140 get_image_dd "$1" of="/dev/$partdev" ibs=512 obs=1M skip="$start" \
141 count="$size" conv=fsync
142 fi
143 else
144 v "Unable to find partition $part device, skipped."
145 fi
146 done < /tmp/partmap.image
147
148 local parttype=ext4
149
150 if (blkid > /dev/null) && export_partdevice partdev 1; then
151 part_magic_fat "/dev/$partdev" && parttype=vfat
152 mount -t $parttype -o rw,noatime "/dev/$partdev" /mnt
153 if export_partdevice partdev 2; then
154 THIS_PART_BLKID=$(blkid -o value -s PARTUUID "/dev/${partdev}")
155 v "Setting rootfs PARTUUID=${THIS_PART_BLKID}"
156 sed -i "s/\(PARTUUID=\)[a-f0-9-]\+/\1${THIS_PART_BLKID}/ig" \
157 /mnt/efi/openwrt/grub.cfg
158 fi
159 umount /mnt
160 fi
161 # Provide time for the storage medium to flush before system reset
162 # (despite the sync/umount it appears NVMe etc. do it in the background)
163 sleep 5
164 }