blob: 0ecaed86e8279900bedee53b7a7d6e066e079373 [file] [log] [blame]
developer9ff67652022-06-13 11:20:05 +08001diff --git a/package/base-files/files/lib/upgrade/nand.sh b/package/base-files/files/lib/upgrade/nand.sh
2index e6f58df..ba8c0fc 100644
3--- a/package/base-files/files/lib/upgrade/nand.sh
4+++ b/package/base-files/files/lib/upgrade/nand.sh
5@@ -316,3 +316,158 @@ nand_do_platform_check() {
6
7 return 0
8 }
9+
10+dual_boot_upgrade_prepare_ubi() {
11+ local kernel_vol_name="$1"
12+ local rootfs_vol_name="$2"
13+ local kernel_length="$3"
14+ local rootfs_length="$4"
15+ local reserve_rootfs_data="$5"
16+
17+ local mtdnum="$( find_mtd_index "$CI_UBIPART" )"
18+ if [ ! "$mtdnum" ]; then
19+ echo "cannot find ubi mtd partition $CI_UBIPART"
20+ return 1
21+ fi
22+
23+ local ubidev="$( nand_find_ubi "$CI_UBIPART" )"
24+ if [ ! "$ubidev" ]; then
25+ ubiattach -m "$mtdnum"
26+ sync
27+ ubidev="$( nand_find_ubi "$CI_UBIPART" )"
28+ fi
29+
30+ if [ ! "$ubidev" ]; then
31+ ubiformat /dev/mtd$mtdnum -y
32+ ubiattach -m "$mtdnum"
33+ sync
34+ ubidev="$( nand_find_ubi "$CI_UBIPART" )"
35+ ubimkvol /dev/$ubidev -n 0 -N u-boot-env -s 512KiB
36+ fi
37+
38+ local rootfs_data_vol_name=$(cat /sys/module/boot_param/parameters/rootfs_data_part 2>/dev/null)
39+
40+ local kern_ubivol="$( nand_find_volume $ubidev $kernel_vol_name )"
41+ local root_ubivol="$( nand_find_volume $ubidev $rootfs_vol_name )"
42+ local data_ubivol="$( nand_find_volume $ubidev $rootfs_data_vol_name )"
43+
44+ # remove ubiblock device of rootfs
45+ local root_ubiblk="ubiblock${root_ubivol:3}"
46+ if [ "$root_ubivol" -a -e "/dev/$root_ubiblk" ]; then
47+ echo "removing $root_ubiblk"
48+ if ! ubiblock -r /dev/$root_ubivol; then
49+ echo "cannot remove $root_ubiblk"
50+ return 1;
51+ fi
52+ fi
53+
54+ # kill volumes
55+ [ "$kern_ubivol" ] && ubirmvol /dev/$ubidev -N $kernel_vol_name || true
56+ [ "$root_ubivol" ] && ubirmvol /dev/$ubidev -N $rootfs_vol_name || true
57+
58+ # update kernel
59+ if ! ubimkvol /dev/$ubidev -N $kernel_vol_name -s $kernel_length; then
60+ echo "cannot create kernel volume"
61+ return 1;
62+ fi
63+
64+ # update rootfs
65+ if ! ubimkvol /dev/$ubidev -N $rootfs_vol_name -s $rootfs_length; then
66+ echo "cannot create rootfs volume"
67+ return 1;
68+ fi
69+
70+ if [ x"${reserve_rootfs_data}" = xY ]; then
71+ # Do not touch rootfs_data
72+ sync
73+ return 0
74+ fi
75+
76+ # 'format' rootfs_data volume
77+ [ "$data_ubivol" ] && {
78+ local rootfs_data_length=$(cat /sys/class/ubi/$data_ubivol/data_bytes)
79+
80+ # kill rootfs_data volume
81+ ubirmvol /dev/$ubidev -N $rootfs_data_vol_name || true
82+
83+ # update rootfs_data
84+ if ! ubimkvol /dev/$ubidev -N $rootfs_data_vol_name -s $rootfs_data_length; then
85+ echo "cannot create $rootfs_data_vol_name volume"
86+ fi
87+ }
88+
89+ sync
90+ return 0
91+}
92+
93+ubi_dual_boot_upgrade_tar() {
94+ local tar_file="$1"
95+ local board_dir=$(tar tf ${tar_file} | grep -m 1 '^sysupgrade-.*/$')
96+ local reserve_rootfs_data=$(cat /sys/module/boot_param/parameters/reserve_rootfs_data 2>/dev/null)
97+ board_dir=${board_dir%/}
98+
99+ kernel_vol_name=$(cat /sys/module/boot_param/parameters/upgrade_kernel_part 2>/dev/null)
100+ [ -z "${kernel_vol_name}" -o $? -ne 0 ] && return 1
101+
102+ rootfs_vol_name=$(cat /sys/module/boot_param/parameters/upgrade_rootfs_part 2>/dev/null)
103+ [ -z "${rootfs_vol_name}" -o $? -ne 0 ] && return 1
104+
105+ local kernel_length=$( (tar xf ${tar_file} ${board_dir}/kernel -O | wc -c) 2> /dev/null)
106+ local rootfs_length=$( (tar xf ${tar_file} ${board_dir}/root -O | wc -c) 2> /dev/null)
107+
108+ dual_boot_upgrade_prepare_ubi "${kernel_vol_name}" "${rootfs_vol_name}" \
109+ "${kernel_length}" "${rootfs_length}" \
110+ "${reserve_rootfs_data}"
111+
112+ local ubidev="$( nand_find_ubi "$CI_UBIPART" )"
113+
114+ [ "${kernel_length}" != 0 ] && {
115+ local kern_ubivol="$(nand_find_volume $ubidev ${kernel_vol_name})"
116+ tar xf ${tar_file} ${board_dir}/kernel -O | \
117+ ubiupdatevol /dev/${kern_ubivol} -s ${kernel_length} -
118+ }
119+
120+ [ "${rootfs_length}" != 0 ] && {
121+ local root_ubivol="$(nand_find_volume $ubidev ${rootfs_vol_name})"
122+ tar xf ${tar_file} ${board_dir}/root -O | \
123+ ubiupdatevol /dev/${root_ubivol} -s ${rootfs_length} -
124+ }
125+
126+ upgrade_image_slot=$(cat /sys/module/boot_param/parameters/upgrade_image_slot 2>/dev/null)
127+ [ -n "${upgrade_image_slot}" ] && {
128+ v "Set new boot image slot to ${upgrade_image_slot}"
129+ # Force the creation of fw_printenv.lock
130+ mkdir -p /var/lock
131+ touch /var/lock/fw_printenv.lock
132+ fw_setenv "dual_boot.current_slot" "${upgrade_image_slot}"
133+ fw_setenv "dual_boot.slot_${upgrade_image_slot}_invalid" "0"
134+ }
135+
136+ if [ x"${reserve_rootfs_data}" != xY ]; then
137+ # do normal upgrade flow
138+ nand_do_upgrade_success
139+ fi
140+
141+ # Do not touch rootfs_data
142+ sync
143+
144+ echo "sysupgrade successful"
145+ umount -a
146+ reboot -f
147+}
148+
149+ubi_do_upgrade() {
150+ local dual_boot=$(cat /sys/module/boot_param/parameters/dual_boot 2>/dev/null)
151+ local file_type=$(identify $1)
152+
153+ if [ x"${dual_boot}" != xY ]; then
154+ nand_do_upgrade "$1"
155+ return
156+ fi
157+
158+ case "$file_type" in
159+ "ubi") v "Unsupported firmware type: ubinized";;
160+ "ubifs") v "Unsupported firmware type: ubifs";;
161+ *) ubi_dual_boot_upgrade_tar $1;;
162+ esac
163+}