base-files: drop support for NAND upgrade in platform_pre_upgrade()
[openwrt/staging/mkresin.git] / package / base-files / files / lib / upgrade / nand.sh
1 #!/bin/sh
2 # Copyright (C) 2014 OpenWrt.org
3 #
4
5 . /lib/functions.sh
6
7 # 'kernel' partition on NAND contains the kernel
8 CI_KERNPART="${CI_KERNPART:-kernel}"
9
10 # 'ubi' partition on NAND contains UBI
11 CI_UBIPART="${CI_UBIPART:-ubi}"
12
13 # 'rootfs' partition on NAND contains the rootfs
14 CI_ROOTPART="${CI_ROOTPART:-rootfs}"
15
16 ubi_mknod() {
17 local dir="$1"
18 local dev="/dev/$(basename $dir)"
19
20 [ -e "$dev" ] && return 0
21
22 local devid="$(cat $dir/dev)"
23 local major="${devid%%:*}"
24 local minor="${devid##*:}"
25 mknod "$dev" c $major $minor
26 }
27
28 nand_find_volume() {
29 local ubidevdir ubivoldir
30 ubidevdir="/sys/devices/virtual/ubi/$1"
31 [ ! -d "$ubidevdir" ] && return 1
32 for ubivoldir in $ubidevdir/${1}_*; do
33 [ ! -d "$ubivoldir" ] && continue
34 if [ "$( cat $ubivoldir/name )" = "$2" ]; then
35 basename $ubivoldir
36 ubi_mknod "$ubivoldir"
37 return 0
38 fi
39 done
40 }
41
42 nand_find_ubi() {
43 local ubidevdir ubidev mtdnum
44 mtdnum="$( find_mtd_index $1 )"
45 [ ! "$mtdnum" ] && return 1
46 for ubidevdir in /sys/devices/virtual/ubi/ubi*; do
47 [ ! -d "$ubidevdir" ] && continue
48 cmtdnum="$( cat $ubidevdir/mtd_num )"
49 [ ! "$mtdnum" ] && continue
50 if [ "$mtdnum" = "$cmtdnum" ]; then
51 ubidev=$( basename $ubidevdir )
52 ubi_mknod "$ubidevdir"
53 echo $ubidev
54 return 0
55 fi
56 done
57 }
58
59 nand_get_magic_long() {
60 dd if="$1" skip=$2 bs=4 count=1 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
61 }
62
63 get_magic_long_tar() {
64 ( tar xf $1 $2 -O | dd bs=4 count=1 | hexdump -v -n 4 -e '1/1 "%02x"') 2> /dev/null
65 }
66
67 identify_magic() {
68 local magic=$1
69 case "$magic" in
70 "55424923")
71 echo "ubi"
72 ;;
73 "31181006")
74 echo "ubifs"
75 ;;
76 "68737173")
77 echo "squashfs"
78 ;;
79 "d00dfeed")
80 echo "fit"
81 ;;
82 "4349"*)
83 echo "combined"
84 ;;
85 *)
86 echo "unknown $magic"
87 ;;
88 esac
89 }
90
91
92 identify() {
93 identify_magic $(nand_get_magic_long "$1" "${2:-0}")
94 }
95
96 identify_tar() {
97 identify_magic $(get_magic_long_tar "$1" "$2")
98 }
99
100 nand_restore_config() {
101 sync
102 local ubidev=$( nand_find_ubi $CI_UBIPART )
103 local ubivol="$( nand_find_volume $ubidev rootfs_data )"
104 [ ! "$ubivol" ] &&
105 ubivol="$( nand_find_volume $ubidev $CI_ROOTPART )"
106 mkdir /tmp/new_root
107 if ! mount -t ubifs /dev/$ubivol /tmp/new_root; then
108 echo "mounting ubifs $ubivol failed"
109 rmdir /tmp/new_root
110 return 1
111 fi
112 mv "$1" "/tmp/new_root/sysupgrade.tgz"
113 umount /tmp/new_root
114 sync
115 rmdir /tmp/new_root
116 }
117
118 nand_upgrade_prepare_ubi() {
119 local rootfs_length="$1"
120 local rootfs_type="$2"
121 local has_kernel="${3:-0}"
122 local has_env="${4:-0}"
123
124 local mtdnum="$( find_mtd_index "$CI_UBIPART" )"
125 if [ ! "$mtdnum" ]; then
126 echo "cannot find ubi mtd partition $CI_UBIPART"
127 return 1
128 fi
129
130 local ubidev="$( nand_find_ubi "$CI_UBIPART" )"
131 if [ ! "$ubidev" ]; then
132 ubiattach -m "$mtdnum"
133 sync
134 ubidev="$( nand_find_ubi "$CI_UBIPART" )"
135 fi
136
137 if [ ! "$ubidev" ]; then
138 ubiformat /dev/mtd$mtdnum -y
139 ubiattach -m "$mtdnum"
140 sync
141 ubidev="$( nand_find_ubi "$CI_UBIPART" )"
142 [ "$has_env" -gt 0 ] && {
143 ubimkvol /dev/$ubidev -n 0 -N ubootenv -s 1MiB
144 ubimkvol /dev/$ubidev -n 1 -N ubootenv2 -s 1MiB
145 }
146 fi
147
148 local kern_ubivol="$( nand_find_volume $ubidev $CI_KERNPART )"
149 local root_ubivol="$( nand_find_volume $ubidev $CI_ROOTPART )"
150 local data_ubivol="$( nand_find_volume $ubidev rootfs_data )"
151
152 # remove ubiblock device of rootfs
153 local root_ubiblk="ubiblock${root_ubivol:3}"
154 if [ "$root_ubivol" -a -e "/dev/$root_ubiblk" ]; then
155 echo "removing $root_ubiblk"
156 if ! ubiblock -r /dev/$root_ubivol; then
157 echo "cannot remove $root_ubiblk"
158 return 1;
159 fi
160 fi
161
162 # kill volumes
163 [ "$kern_ubivol" ] && ubirmvol /dev/$ubidev -N $CI_KERNPART || true
164 [ "$root_ubivol" ] && ubirmvol /dev/$ubidev -N $CI_ROOTPART || true
165 [ "$data_ubivol" ] && ubirmvol /dev/$ubidev -N rootfs_data || true
166
167 # update kernel
168 if [ "$has_kernel" = "1" ]; then
169 if ! ubimkvol /dev/$ubidev -N $CI_KERNPART -s $kernel_length; then
170 echo "cannot create kernel volume"
171 return 1;
172 fi
173 fi
174
175 # update rootfs
176 local root_size_param
177 if [ "$rootfs_type" = "ubifs" ]; then
178 root_size_param="-m"
179 else
180 root_size_param="-s $rootfs_length"
181 fi
182 if ! ubimkvol /dev/$ubidev -N $CI_ROOTPART $root_size_param; then
183 echo "cannot create rootfs volume"
184 return 1;
185 fi
186
187 # create rootfs_data for non-ubifs rootfs
188 if [ "$rootfs_type" != "ubifs" ]; then
189 if ! ubimkvol /dev/$ubidev -N rootfs_data -m; then
190 echo "cannot initialize rootfs_data volume"
191 return 1
192 fi
193 fi
194 sync
195 return 0
196 }
197
198 nand_do_upgrade_success() {
199 local conf_tar="/tmp/sysupgrade.tgz"
200
201 sync
202 [ -f "$conf_tar" ] && nand_restore_config "$conf_tar"
203 echo "sysupgrade successful"
204 umount -a
205 reboot -f
206 }
207
208 # Flash the UBI image to MTD partition
209 nand_upgrade_ubinized() {
210 local ubi_file="$1"
211 local mtdnum="$(find_mtd_index "$CI_UBIPART")"
212
213 [ ! "$mtdnum" ] && {
214 CI_UBIPART="rootfs"
215 mtdnum="$(find_mtd_index "$CI_UBIPART")"
216 }
217
218 if [ ! "$mtdnum" ]; then
219 echo "cannot find mtd device $CI_UBIPART"
220 umount -a
221 reboot -f
222 fi
223
224 local mtddev="/dev/mtd${mtdnum}"
225 ubidetach -p "${mtddev}" || true
226 sync
227 ubiformat "${mtddev}" -y -f "${ubi_file}"
228 ubiattach -p "${mtddev}"
229 nand_do_upgrade_success
230 }
231
232 # Write the UBIFS image to UBI volume
233 nand_upgrade_ubifs() {
234 local rootfs_length=`(cat $1 | wc -c) 2> /dev/null`
235
236 nand_upgrade_prepare_ubi "$rootfs_length" "ubifs" "0" "0"
237
238 local ubidev="$( nand_find_ubi "$CI_UBIPART" )"
239 local root_ubivol="$(nand_find_volume $ubidev $CI_ROOTPART)"
240 ubiupdatevol /dev/$root_ubivol -s $rootfs_length $1
241
242 nand_do_upgrade_success
243 }
244
245 nand_upgrade_tar() {
246 local tar_file="$1"
247 local kernel_mtd="$(find_mtd_index $CI_KERNPART)"
248
249 local board_dir=$(tar tf $tar_file | grep -m 1 '^sysupgrade-.*/$')
250 board_dir=${board_dir%/}
251
252 local kernel_length=`(tar xf $tar_file ${board_dir}/kernel -O | wc -c) 2> /dev/null`
253 local rootfs_length=`(tar xf $tar_file ${board_dir}/root -O | wc -c) 2> /dev/null`
254
255 local rootfs_type="$(identify_tar "$tar_file" ${board_dir}/root)"
256
257 local has_kernel=1
258 local has_env=0
259
260 [ "$kernel_length" != 0 -a -n "$kernel_mtd" ] && {
261 tar xf $tar_file ${board_dir}/kernel -O | mtd write - $CI_KERNPART
262 }
263 [ "$kernel_length" = 0 -o ! -z "$kernel_mtd" ] && has_kernel=0
264
265 nand_upgrade_prepare_ubi "$rootfs_length" "$rootfs_type" "$has_kernel" "$has_env"
266
267 local ubidev="$( nand_find_ubi "$CI_UBIPART" )"
268 [ "$has_kernel" = "1" ] && {
269 local kern_ubivol="$(nand_find_volume $ubidev $CI_KERNPART)"
270 tar xf $tar_file ${board_dir}/kernel -O | \
271 ubiupdatevol /dev/$kern_ubivol -s $kernel_length -
272 }
273
274 local root_ubivol="$(nand_find_volume $ubidev $CI_ROOTPART)"
275 tar xf $tar_file ${board_dir}/root -O | \
276 ubiupdatevol /dev/$root_ubivol -s $rootfs_length -
277
278 nand_do_upgrade_success
279 }
280
281 # Recognize type of passed file and start the upgrade process
282 nand_do_upgrade() {
283 local file_type=$(identify $1)
284
285 if type 'platform_nand_pre_upgrade' >/dev/null 2>/dev/null; then
286 platform_nand_pre_upgrade "$1"
287 fi
288
289 [ ! "$(find_mtd_index "$CI_UBIPART")" ] && CI_UBIPART="rootfs"
290
291 case "$file_type" in
292 "ubi") nand_upgrade_ubinized $1;;
293 "ubifs") nand_upgrade_ubifs $1;;
294 *) nand_upgrade_tar $1;;
295 esac
296 }
297
298 # Check if passed file is a valid one for NAND sysupgrade. Currently it accepts
299 # 3 types of files:
300 # 1) UBI - should contain an ubinized image, header is checked for the proper
301 # MAGIC
302 # 2) UBIFS - should contain UBIFS partition that will replace "rootfs" volume,
303 # header is checked for the proper MAGIC
304 # 3) TAR - archive has to include "sysupgrade-BOARD" directory with a non-empty
305 # "CONTROL" file (at this point its content isn't verified)
306 #
307 # You usually want to call this function in platform_check_image.
308 #
309 # $(1): board name, used in case of passing TAR file
310 # $(2): file to be checked
311 nand_do_platform_check() {
312 local board_name="$1"
313 local tar_file="$2"
314 local control_length=`(tar xf $tar_file sysupgrade-$board_name/CONTROL -O | wc -c) 2> /dev/null`
315 local file_type="$(identify $2)"
316
317 [ "$control_length" = 0 -a "$file_type" != "ubi" -a "$file_type" != "ubifs" ] && {
318 echo "Invalid sysupgrade file."
319 return 1
320 }
321
322 return 0
323 }