base-files: fix ubinized nand sysupgrade
[openwrt/openwrt.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
132 local ubiblk="ubiblock${ubivol:3}"
133 if [ -e "/dev/$ubiblk" ]; then
134 umount "/dev/$ubiblk" && echo "unmounted /dev/$ubiblk" || :
135 if ! ubiblock -r "/dev/$ubivol"; then
136 echo "cannot remove $ubiblk"
137 return 1
138 fi
139 fi
140 }
141
142 nand_detach_ubi() {
143 local ubipart="$1"
144
145 local mtdnum="$( find_mtd_index "$ubipart" )"
146 if [ ! "$mtdnum" ]; then
147 echo "cannot find ubi mtd partition $ubipart"
148 return 1
149 fi
150
151 local ubidev="$( nand_find_ubi "$ubipart" )"
152 if [ "$ubidev" ]; then
153 for ubivol in $(find /dev -name "${ubidev}_*" -maxdepth 1 | sort); do
154 ubivol="${ubivol:5}"
155 nand_remove_ubiblock "$ubivol" || :
156 umount "/dev/$ubivol" && echo "unmounted /dev/$ubivol" || :
157 done
158 if ! ubidetach -m "$mtdnum"; then
159 echo "cannot detach ubi mtd partition $ubipart"
160 return 1
161 fi
162 fi
163 }
164
165 nand_upgrade_prepare_ubi() {
166 local rootfs_length="$1"
167 local rootfs_type="$2"
168 local rootfs_data_max="$(fw_printenv -n rootfs_data_max 2>/dev/null)"
169 [ -n "$rootfs_data_max" ] && rootfs_data_max=$((rootfs_data_max))
170
171 local kernel_length="$3"
172 local has_env="${4:-0}"
173
174 [ -n "$rootfs_length" -o -n "$kernel_length" ] || return 1
175
176 local mtdnum="$( find_mtd_index "$CI_UBIPART" )"
177 if [ ! "$mtdnum" ]; then
178 echo "cannot find ubi mtd partition $CI_UBIPART"
179 return 1
180 fi
181
182 local ubidev="$( nand_find_ubi "$CI_UBIPART" )"
183 if [ ! "$ubidev" ]; then
184 ubiattach -m "$mtdnum"
185 ubidev="$( nand_find_ubi "$CI_UBIPART" )"
186
187 if [ ! "$ubidev" ]; then
188 ubiformat /dev/mtd$mtdnum -y
189 ubiattach -m "$mtdnum"
190 ubidev="$( nand_find_ubi "$CI_UBIPART" )"
191
192 if [ ! "$ubidev" ]; then
193 echo "cannot attach ubi mtd partition $CI_UBIPART"
194 return 1
195 fi
196
197 if [ "$has_env" -gt 0 ]; then
198 ubimkvol /dev/$ubidev -n 0 -N ubootenv -s 1MiB
199 ubimkvol /dev/$ubidev -n 1 -N ubootenv2 -s 1MiB
200 fi
201 fi
202 fi
203
204 local kern_ubivol="$( nand_find_volume $ubidev "$CI_KERNPART" )"
205 local root_ubivol="$( nand_find_volume $ubidev "$CI_ROOTPART" )"
206 local data_ubivol="$( nand_find_volume $ubidev rootfs_data )"
207 [ "$root_ubivol" = "$kern_ubivol" ] && root_ubivol=
208
209 # remove ubiblocks
210 [ "$kern_ubivol" ] && { nand_remove_ubiblock $kern_ubivol || return 1; }
211 [ "$root_ubivol" ] && { nand_remove_ubiblock $root_ubivol || return 1; }
212 [ "$data_ubivol" ] && { nand_remove_ubiblock $data_ubivol || return 1; }
213
214 # kill volumes
215 [ "$kern_ubivol" ] && ubirmvol /dev/$ubidev -N "$CI_KERNPART" || :
216 [ "$root_ubivol" ] && ubirmvol /dev/$ubidev -N "$CI_ROOTPART" || :
217 [ "$data_ubivol" ] && ubirmvol /dev/$ubidev -N rootfs_data || :
218
219 # create kernel vol
220 if [ -n "$kernel_length" ]; then
221 if ! ubimkvol /dev/$ubidev -N "$CI_KERNPART" -s $kernel_length; then
222 echo "cannot create kernel volume"
223 return 1;
224 fi
225 fi
226
227 # create rootfs vol
228 if [ -n "$rootfs_length" ]; then
229 local rootfs_size_param
230 if [ "$rootfs_type" = "ubifs" ]; then
231 rootfs_size_param="-m"
232 else
233 rootfs_size_param="-s $rootfs_length"
234 fi
235 if ! ubimkvol /dev/$ubidev -N "$CI_ROOTPART" $rootfs_size_param; then
236 echo "cannot create rootfs volume"
237 return 1;
238 fi
239 fi
240
241 # create rootfs_data vol for non-ubifs rootfs
242 if [ "$rootfs_type" != "ubifs" ]; then
243 local rootfs_data_size_param="-m"
244 if [ -n "$rootfs_data_max" ]; then
245 rootfs_data_size_param="-s $rootfs_data_max"
246 fi
247 if ! ubimkvol /dev/$ubidev -N rootfs_data $rootfs_data_size_param; then
248 if ! ubimkvol /dev/$ubidev -N rootfs_data -m; then
249 echo "cannot initialize rootfs_data volume"
250 return 1
251 fi
252 fi
253 fi
254
255 return 0
256 }
257
258 # Write the UBI image to MTD ubi partition
259 nand_upgrade_ubinized() {
260 local ubi_file="$1"
261
262 nand_detach_ubi "$CI_UBIPART" || return 1
263
264 local mtdnum="$( find_mtd_index "$CI_UBIPART" )"
265 ubiformat "/dev/mtd$mtdnum" -y -f "$ubi_file" && ubiattach -m "$mtdnum"
266 }
267
268 # Write the UBIFS image to UBI rootfs volume
269 nand_upgrade_ubifs() {
270 local rootfs_length=$( (cat $1 | wc -c) 2> /dev/null)
271
272 nand_upgrade_prepare_ubi "$rootfs_length" "ubifs" "" "" || return 1
273
274 local ubidev="$( nand_find_ubi "$CI_UBIPART" )"
275 local root_ubivol="$(nand_find_volume $ubidev "$CI_ROOTPART")"
276 ubiupdatevol /dev/$root_ubivol -s $rootfs_length $1
277 }
278
279 # Write the FIT image to UBI kernel volume
280 nand_upgrade_fit() {
281 local fit_file="$1"
282 local fit_length="$(wc -c < "$fit_file")"
283
284 nand_upgrade_prepare_ubi "" "" "$fit_length" "1" || return 1
285
286 local fit_ubidev="$(nand_find_ubi "$CI_UBIPART")"
287 local fit_ubivol="$(nand_find_volume $fit_ubidev "$CI_KERNPART")"
288 ubiupdatevol /dev/$fit_ubivol -s $fit_length $fit_file
289 }
290
291 # Write images in the TAR file to MTD partitions and/or UBI volumes as required
292 nand_upgrade_tar() {
293 local tar_file="$1"
294
295 # WARNING: This fails if tar contains more than one 'sysupgrade-*' directory.
296 local board_dir="$(tar tf "$tar_file" | grep -m 1 '^sysupgrade-.*/$')"
297 board_dir="${board_dir%/}"
298
299 local kernel_mtd kernel_length
300 if [ "$CI_KERNPART" != "none" ]; then
301 kernel_mtd="$(find_mtd_index "$CI_KERNPART")"
302 kernel_length=$( (tar xf "$tar_file" "$board_dir/kernel" -O | wc -c) 2> /dev/null)
303 [ "$kernel_length" = 0 ] && kernel_length=
304 fi
305 local rootfs_length=$( (tar xf "$tar_file" "$board_dir/root" -O | wc -c) 2> /dev/null)
306 [ "$rootfs_length" = 0 ] && rootfs_length=
307 local rootfs_type
308 [ "$rootfs_length" ] && rootfs_type="$(identify_tar "$tar_file" "$board_dir/root")"
309
310 local ubi_kernel_length
311 if [ "$kernel_length" ]; then
312 if [ "$kernel_mtd" ]; then
313 # On some devices, the raw kernel and ubi partitions overlap.
314 # These devices brick if the kernel partition is erased.
315 # Hence only invalidate kernel for now.
316 dd if=/dev/zero bs=4096 count=1 2>/dev/null | \
317 mtd write - "$CI_KERNPART"
318 else
319 ubi_kernel_length="$kernel_length"
320 fi
321 fi
322 local has_env=0
323 nand_upgrade_prepare_ubi "$rootfs_length" "$rootfs_type" "$ubi_kernel_length" "$has_env" || return 1
324
325 local ubidev="$( nand_find_ubi "$CI_UBIPART" )"
326 if [ "$rootfs_length" ]; then
327 local root_ubivol="$( nand_find_volume $ubidev "$CI_ROOTPART" )"
328 tar xf "$tar_file" "$board_dir/root" -O | \
329 ubiupdatevol /dev/$root_ubivol -s $rootfs_length -
330 fi
331 if [ "$kernel_length" ]; then
332 if [ "$kernel_mtd" ]; then
333 tar xf "$tar_file" "$board_dir/kernel" -O | \
334 mtd write - "$CI_KERNPART"
335 else
336 local kern_ubivol="$( nand_find_volume $ubidev "$CI_KERNPART" )"
337 tar xf "$tar_file" "$board_dir/kernel" -O | \
338 ubiupdatevol /dev/$kern_ubivol -s $kernel_length -
339 fi
340 fi
341
342 return 0
343 }
344
345 nand_do_flash_file() {
346 local file_type=$(identify "$1")
347
348 [ ! "$(find_mtd_index "$CI_UBIPART")" ] && CI_UBIPART=rootfs
349
350 case "$file_type" in
351 "fit") nand_upgrade_fit "$1";;
352 "ubi") nand_upgrade_ubinized "$1";;
353 "ubifs") nand_upgrade_ubifs "$1";;
354 *) nand_upgrade_tar "$1";;
355 esac
356 }
357
358 nand_do_restore_config() {
359 local conf_tar="/tmp/sysupgrade.tgz"
360 [ ! -f "$conf_tar" ] || nand_restore_config "$conf_tar"
361 }
362
363 # Recognize type of passed file and start the upgrade process
364 nand_do_upgrade() {
365 sync
366 if nand_do_flash_file "$1" && nand_do_restore_config && sync; then
367 echo "sysupgrade successful"
368 umount -a
369 reboot -f
370 fi
371
372 sync
373 echo "sysupgrade failed"
374 # Should we reboot or bring up some failsafe mode instead?
375 umount -a
376 reboot -f
377 }
378
379 # Check if passed file is a valid one for NAND sysupgrade.
380 # Currently it accepts 4 types of files:
381 # 1) UBI: a ubinized image containing required UBI volumes.
382 # 2) UBIFS: a UBIFS rootfs volume image.
383 # 3) FIT: a FIT image containing kernel and rootfs.
384 # 4) TAR: an archive that includes directory "sysupgrade-${BOARD_NAME}" containing
385 # a non-empty "CONTROL" file and required partition and/or volume images.
386 #
387 # You usually want to call this function in platform_check_image.
388 #
389 # $(1): board name, used in case of passing TAR file
390 # $(2): file to be checked
391 nand_do_platform_check() {
392 local board_name="$1"
393 local tar_file="$2"
394 local control_length=$( (tar xf $tar_file sysupgrade-$board_name/CONTROL -O | wc -c) 2> /dev/null)
395 local file_type="$(identify $2)"
396
397 [ "$control_length" = 0 -a "$file_type" != "ubi" -a "$file_type" != "ubifs" -a "$file_type" != "fit" ] && {
398 echo "Invalid sysupgrade file."
399 return 1
400 }
401
402 return 0
403 }