base-files: fix and clean up nand sysupgrade code
[openwrt/staging/noltari.git] / package / base-files / files / lib / upgrade / nand.sh
1 # Copyright (C) 2014 OpenWrt.org
2 #
3
4 . /lib/functions.sh
5
6 # 'kernel' partition or UBI volume on NAND contains the kernel
7 CI_KERNPART="${CI_KERNPART:-kernel}"
8
9 # 'ubi' partition on NAND contains UBI
10 CI_UBIPART="${CI_UBIPART:-ubi}"
11
12 # 'rootfs' UBI volume on NAND contains the rootfs
13 CI_ROOTPART="${CI_ROOTPART:-rootfs}"
14
15 ubi_mknod() {
16 local dir="$1"
17 local dev="/dev/$(basename $dir)"
18
19 [ -e "$dev" ] && return 0
20
21 local devid="$(cat $dir/dev)"
22 local major="${devid%%:*}"
23 local minor="${devid##*:}"
24 mknod "$dev" c $major $minor
25 }
26
27 nand_find_volume() {
28 local ubidevdir ubivoldir
29 ubidevdir="/sys/devices/virtual/ubi/$1"
30 [ ! -d "$ubidevdir" ] && return 1
31 for ubivoldir in $ubidevdir/${1}_*; do
32 [ ! -d "$ubivoldir" ] && continue
33 if [ "$( cat $ubivoldir/name )" = "$2" ]; then
34 basename $ubivoldir
35 ubi_mknod "$ubivoldir"
36 return 0
37 fi
38 done
39 }
40
41 nand_find_ubi() {
42 local ubidevdir ubidev mtdnum
43 mtdnum="$( find_mtd_index $1 )"
44 [ ! "$mtdnum" ] && return 1
45 for ubidevdir in /sys/devices/virtual/ubi/ubi*; do
46 [ ! -d "$ubidevdir" ] && continue
47 cmtdnum="$( cat $ubidevdir/mtd_num )"
48 [ ! "$mtdnum" ] && continue
49 if [ "$mtdnum" = "$cmtdnum" ]; then
50 ubidev=$( basename $ubidevdir )
51 ubi_mknod "$ubidevdir"
52 echo $ubidev
53 return 0
54 fi
55 done
56 }
57
58 nand_get_magic_long() {
59 dd if="$1" skip=$2 bs=4 count=1 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
60 }
61
62 get_magic_long_tar() {
63 ( tar xf $1 $2 -O | dd bs=4 count=1 | hexdump -v -n 4 -e '1/1 "%02x"') 2> /dev/null
64 }
65
66 identify_magic() {
67 local magic=$1
68 case "$magic" in
69 "55424923")
70 echo "ubi"
71 ;;
72 "31181006")
73 echo "ubifs"
74 ;;
75 "68737173")
76 echo "squashfs"
77 ;;
78 "d00dfeed")
79 echo "fit"
80 ;;
81 "4349"*)
82 echo "combined"
83 ;;
84 *)
85 echo "unknown $magic"
86 ;;
87 esac
88 }
89
90
91 identify() {
92 identify_magic $(nand_get_magic_long "$1" "${2:-0}")
93 }
94
95 identify_tar() {
96 identify_magic $(get_magic_long_tar "$1" "$2")
97 }
98
99 nand_restore_config() {
100 local ubidev=$( nand_find_ubi "$CI_UBIPART" )
101 local ubivol="$( nand_find_volume $ubidev rootfs_data )"
102 if [ ! "$ubivol" ]; then
103 ubivol="$( nand_find_volume $ubidev "$CI_ROOTPART" )"
104 if [ ! "$ubivol" ]; then
105 echo "cannot find ubifs data volume"
106 return 1
107 fi
108 fi
109 mkdir /tmp/new_root
110 if ! mount -t ubifs /dev/$ubivol /tmp/new_root; then
111 echo "cannot mount ubifs volume $ubivol"
112 rmdir /tmp/new_root
113 return 1
114 fi
115 if mv "$1" "/tmp/new_root/$BACKUP_FILE"; then
116 if umount /tmp/new_root; then
117 echo "configuration saved"
118 rmdir /tmp/new_root
119 return 0
120 fi
121 else
122 umount /tmp/new_root
123 fi
124 echo "could not save configuration to ubifs volume $ubivol"
125 rmdir /tmp/new_root
126 return 1
127 }
128
129 nand_remove_ubiblock() {
130 local ubivol=$1
131 local ubiblk=ubiblock${ubivol:3}
132 if [ -e /dev/$ubiblk ]; then
133 echo "removing $ubiblk"
134 if ! ubiblock -r /dev/$ubivol; then
135 echo "cannot remove $ubiblk"
136 return 1
137 fi
138 fi
139 }
140
141 nand_upgrade_prepare_ubi() {
142 local rootfs_length="$1"
143 local rootfs_type="$2"
144 local rootfs_data_max="$(fw_printenv -n rootfs_data_max 2>/dev/null)"
145 [ -n "$rootfs_data_max" ] && rootfs_data_max=$((rootfs_data_max))
146
147 local kernel_length="$3"
148 local has_env="${4:-0}"
149
150 [ -n "$rootfs_length" -o -n "$kernel_length" ] || return 1
151
152 local mtdnum="$( find_mtd_index "$CI_UBIPART" )"
153 if [ ! "$mtdnum" ]; then
154 echo "cannot find ubi mtd partition $CI_UBIPART"
155 return 1
156 fi
157
158 local ubidev="$( nand_find_ubi "$CI_UBIPART" )"
159 if [ ! "$ubidev" ]; then
160 ubiattach -m "$mtdnum"
161 ubidev="$( nand_find_ubi "$CI_UBIPART" )"
162
163 if [ ! "$ubidev" ]; then
164 ubiformat /dev/mtd$mtdnum -y
165 ubiattach -m "$mtdnum"
166 ubidev="$( nand_find_ubi "$CI_UBIPART" )"
167
168 if [ ! "$ubidev" ]; then
169 echo "cannot attach ubi mtd partition $CI_UBIPART"
170 return 1
171 fi
172
173 if [ "$has_env" -gt 0 ]; then
174 ubimkvol /dev/$ubidev -n 0 -N ubootenv -s 1MiB
175 ubimkvol /dev/$ubidev -n 1 -N ubootenv2 -s 1MiB
176 fi
177 fi
178 fi
179
180 local kern_ubivol="$( nand_find_volume $ubidev "$CI_KERNPART" )"
181 local root_ubivol="$( nand_find_volume $ubidev "$CI_ROOTPART" )"
182 local data_ubivol="$( nand_find_volume $ubidev rootfs_data )"
183 [ "$root_ubivol" = "$kern_ubivol" ] && root_ubivol=
184
185 # remove ubiblocks
186 [ "$kern_ubivol" ] && { nand_remove_ubiblock $kern_ubivol || return 1; }
187 [ "$root_ubivol" ] && { nand_remove_ubiblock $root_ubivol || return 1; }
188 [ "$data_ubivol" ] && { nand_remove_ubiblock $data_ubivol || return 1; }
189
190 # kill volumes
191 [ "$kern_ubivol" ] && ubirmvol /dev/$ubidev -N "$CI_KERNPART" || :
192 [ "$root_ubivol" ] && ubirmvol /dev/$ubidev -N "$CI_ROOTPART" || :
193 [ "$data_ubivol" ] && ubirmvol /dev/$ubidev -N rootfs_data || :
194
195 # create kernel vol
196 if [ -n "$kernel_length" ]; then
197 if ! ubimkvol /dev/$ubidev -N "$CI_KERNPART" -s $kernel_length; then
198 echo "cannot create kernel volume"
199 return 1;
200 fi
201 fi
202
203 # create rootfs vol
204 if [ -n "$rootfs_length" ]; then
205 local rootfs_size_param
206 if [ "$rootfs_type" = "ubifs" ]; then
207 rootfs_size_param="-m"
208 else
209 rootfs_size_param="-s $rootfs_length"
210 fi
211 if ! ubimkvol /dev/$ubidev -N "$CI_ROOTPART" $rootfs_size_param; then
212 echo "cannot create rootfs volume"
213 return 1;
214 fi
215 fi
216
217 # create rootfs_data vol for non-ubifs rootfs
218 if [ "$rootfs_type" != "ubifs" ]; then
219 local rootfs_data_size_param="-m"
220 if [ -n "$rootfs_data_max" ]; then
221 rootfs_data_size_param="-s $rootfs_data_max"
222 fi
223 if ! ubimkvol /dev/$ubidev -N rootfs_data $rootfs_data_size_param; then
224 if ! ubimkvol /dev/$ubidev -N rootfs_data -m; then
225 echo "cannot initialize rootfs_data volume"
226 return 1
227 fi
228 fi
229 fi
230
231 return 0
232 }
233
234 # Write the UBI image to MTD ubi partition
235 nand_upgrade_ubinized() {
236 local ubi_file="$1"
237
238 local mtdnum="$( find_mtd_index "$CI_UBIPART" )"
239 if [ ! "$mtdnum" ]; then
240 echo "cannot find ubi mtd partition $CI_UBIPART"
241 return 1
242 fi
243
244 local mtddev="/dev/mtd${mtdnum}"
245 ubidetach -p "${mtddev}" || :
246 ubiformat "${mtddev}" -y -f "${ubi_file}" && ubiattach -p "${mtddev}"
247 }
248
249 # Write the UBIFS image to UBI rootfs volume
250 nand_upgrade_ubifs() {
251 local rootfs_length=$( (cat $1 | wc -c) 2> /dev/null)
252
253 nand_upgrade_prepare_ubi "$rootfs_length" "ubifs" "" "" || return 1
254
255 local ubidev="$( nand_find_ubi "$CI_UBIPART" )"
256 local root_ubivol="$(nand_find_volume $ubidev "$CI_ROOTPART")"
257 ubiupdatevol /dev/$root_ubivol -s $rootfs_length $1
258 }
259
260 # Write the FIT image to UBI kernel volume
261 nand_upgrade_fit() {
262 local fit_file="$1"
263 local fit_length="$(wc -c < "$fit_file")"
264
265 nand_upgrade_prepare_ubi "" "" "$fit_length" "1" || return 1
266
267 local fit_ubidev="$(nand_find_ubi "$CI_UBIPART")"
268 local fit_ubivol="$(nand_find_volume $fit_ubidev "$CI_KERNPART")"
269 ubiupdatevol /dev/$fit_ubivol -s $fit_length $fit_file
270 }
271
272 # Write images in the TAR file to MTD partitions and/or UBI volumes as required
273 nand_upgrade_tar() {
274 local tar_file="$1"
275
276 # WARNING: This fails if tar contains more than one 'sysupgrade-*' directory.
277 local board_dir="$(tar tf "$tar_file" | grep -m 1 '^sysupgrade-.*/$')"
278 board_dir="${board_dir%/}"
279
280 local kernel_mtd kernel_length
281 if [ "$CI_KERNPART" != "none" ]; then
282 kernel_mtd="$(find_mtd_index "$CI_KERNPART")"
283 kernel_length=$( (tar xf "$tar_file" "$board_dir/kernel" -O | wc -c) 2> /dev/null)
284 [ "$kernel_length" = 0 ] && kernel_length=
285 fi
286 local rootfs_length=$( (tar xf "$tar_file" "$board_dir/root" -O | wc -c) 2> /dev/null)
287 [ "$rootfs_length" = 0 ] && rootfs_length=
288 local rootfs_type
289 [ "$rootfs_length" ] && rootfs_type="$(identify_tar "$tar_file" "$board_dir/root")"
290
291 local ubi_kernel_length
292 if [ "$kernel_length" ]; then
293 if [ "$kernel_mtd" ]; then
294 # On some devices, the raw kernel and ubi partitions overlap.
295 # These devices brick if the kernel partition is erased.
296 # Hence only invalidate kernel for now.
297 dd if=/dev/zero bs=4096 count=1 2>/dev/null | \
298 mtd write - "$CI_KERNPART"
299 else
300 ubi_kernel_length="$kernel_length"
301 fi
302 fi
303 local has_env=0
304 nand_upgrade_prepare_ubi "$rootfs_length" "$rootfs_type" "$ubi_kernel_length" "$has_env" || return 1
305
306 local ubidev="$( nand_find_ubi "$CI_UBIPART" )"
307 if [ "$rootfs_length" ]; then
308 local root_ubivol="$( nand_find_volume $ubidev "$CI_ROOTPART" )"
309 tar xf "$tar_file" "$board_dir/root" -O | \
310 ubiupdatevol /dev/$root_ubivol -s $rootfs_length -
311 fi
312 if [ "$kernel_length" ]; then
313 if [ "$kernel_mtd" ]; then
314 tar xf "$tar_file" "$board_dir/kernel" -O | \
315 mtd write - "$CI_KERNPART"
316 else
317 local kern_ubivol="$( nand_find_volume $ubidev "$CI_KERNPART" )"
318 tar xf "$tar_file" "$board_dir/kernel" -O | \
319 ubiupdatevol /dev/$kern_ubivol -s $kernel_length -
320 fi
321 fi
322
323 return 0
324 }
325
326 nand_do_flash_file() {
327 local file_type=$(identify "$1")
328
329 [ ! "$(find_mtd_index "$CI_UBIPART")" ] && CI_UBIPART=rootfs
330
331 case "$file_type" in
332 "fit") nand_upgrade_fit "$1";;
333 "ubi") nand_upgrade_ubinized "$1";;
334 "ubifs") nand_upgrade_ubifs "$1";;
335 *) nand_upgrade_tar "$1";;
336 esac
337 }
338
339 nand_do_restore_config() {
340 local conf_tar="/tmp/sysupgrade.tgz"
341 [ ! -f "$conf_tar" ] || nand_restore_config "$conf_tar"
342 }
343
344 # Recognize type of passed file and start the upgrade process
345 nand_do_upgrade() {
346 sync
347 if nand_do_flash_file "$1" && nand_do_restore_config && sync; then
348 echo "sysupgrade successful"
349 umount -a
350 reboot -f
351 fi
352
353 sync
354 echo "sysupgrade failed"
355 # Should we reboot or bring up some failsafe mode instead?
356 umount -a
357 reboot -f
358 }
359
360 # Check if passed file is a valid one for NAND sysupgrade.
361 # Currently it accepts 4 types of files:
362 # 1) UBI: a ubinized image containing required UBI volumes.
363 # 2) UBIFS: a UBIFS rootfs volume image.
364 # 3) FIT: a FIT image containing kernel and rootfs.
365 # 4) TAR: an archive that includes directory "sysupgrade-${BOARD_NAME}" containing
366 # a non-empty "CONTROL" file and required partition and/or volume images.
367 #
368 # You usually want to call this function in platform_check_image.
369 #
370 # $(1): board name, used in case of passing TAR file
371 # $(2): file to be checked
372 nand_do_platform_check() {
373 local board_name="$1"
374 local tar_file="$2"
375 local control_length=$( (tar xf $tar_file sysupgrade-$board_name/CONTROL -O | wc -c) 2> /dev/null)
376 local file_type="$(identify $2)"
377
378 [ "$control_length" = 0 -a "$file_type" != "ubi" -a "$file_type" != "ubifs" -a "$file_type" != "fit" ] && {
379 echo "Invalid sysupgrade file."
380 return 1
381 }
382
383 return 0
384 }