a93c9acccdb0c8fee6e57b6268ea111adbfd97ce
[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 sync
101 local ubidev=$( nand_find_ubi $CI_UBIPART )
102 local ubivol="$( nand_find_volume $ubidev rootfs_data )"
103 [ ! "$ubivol" ] &&
104 ubivol="$( nand_find_volume $ubidev $CI_ROOTPART )"
105 mkdir /tmp/new_root
106 if ! mount -t ubifs /dev/$ubivol /tmp/new_root; then
107 echo "mounting ubifs $ubivol failed"
108 rmdir /tmp/new_root
109 return 1
110 fi
111 mv "$1" "/tmp/new_root/$BACKUP_FILE"
112 umount /tmp/new_root
113 sync
114 rmdir /tmp/new_root
115 }
116
117 nand_remove_ubiblock() {
118 local ubivol=$1
119 local ubiblk=ubiblock${ubivol:3}
120 if [ -e /dev/$ubiblk ]; then
121 echo "removing $ubiblk"
122 if ! ubiblock -r /dev/$ubivol; then
123 echo "cannot remove $ubiblk"
124 return 1
125 fi
126 fi
127 }
128
129 nand_upgrade_prepare_ubi() {
130 local rootfs_length="$1"
131 local rootfs_type="$2"
132 local rootfs_data_max="$(fw_printenv -n rootfs_data_max 2>/dev/null)"
133 [ -n "$rootfs_data_max" ] && rootfs_data_max=$((rootfs_data_max))
134
135 local kernel_length="$3"
136 local has_env="${4:-0}"
137
138 [ -n "$rootfs_length" -o -n "$kernel_length" ] || return 1
139
140 local mtdnum="$( find_mtd_index "$CI_UBIPART" )"
141 if [ ! "$mtdnum" ]; then
142 echo "cannot find ubi mtd partition $CI_UBIPART"
143 return 1
144 fi
145
146 local ubidev="$( nand_find_ubi "$CI_UBIPART" )"
147 if [ ! "$ubidev" ]; then
148 ubiattach -m "$mtdnum"
149 sync
150 ubidev="$( nand_find_ubi "$CI_UBIPART" )"
151
152 if [ ! "$ubidev" ]; then
153 ubiformat /dev/mtd$mtdnum -y
154 ubiattach -m "$mtdnum"
155 sync
156 ubidev="$( nand_find_ubi "$CI_UBIPART" )"
157
158 if [ ! "$ubidev" ]; then
159 echo "cannot attach ubi mtd partition $CI_UBIPART"
160 return 1
161 fi
162
163 if [ "$has_env" -gt 0 ]; then
164 ubimkvol /dev/$ubidev -n 0 -N ubootenv -s 1MiB
165 ubimkvol /dev/$ubidev -n 1 -N ubootenv2 -s 1MiB
166 fi
167 fi
168 fi
169
170 local kern_ubivol="$( nand_find_volume $ubidev $CI_KERNPART )"
171 local root_ubivol="$( nand_find_volume $ubidev $CI_ROOTPART )"
172 local data_ubivol="$( nand_find_volume $ubidev rootfs_data )"
173 [ "$root_ubivol" = "$kern_ubivol" ] && root_ubivol=
174
175 # remove ubiblocks
176 [ "$kern_ubivol" ] && { nand_remove_ubiblock $kern_ubivol || return 1; }
177 [ "$root_ubivol" ] && { nand_remove_ubiblock $root_ubivol || return 1; }
178 [ "$data_ubivol" ] && { nand_remove_ubiblock $data_ubivol || return 1; }
179
180 # kill volumes
181 [ "$kern_ubivol" ] && ubirmvol /dev/$ubidev -N $CI_KERNPART || :
182 [ "$root_ubivol" ] && ubirmvol /dev/$ubidev -N $CI_ROOTPART || :
183 [ "$data_ubivol" ] && ubirmvol /dev/$ubidev -N rootfs_data || :
184
185 # create kernel vol
186 if [ -n "$kernel_length" ]; then
187 if ! ubimkvol /dev/$ubidev -N $CI_KERNPART -s $kernel_length; then
188 echo "cannot create kernel volume"
189 return 1;
190 fi
191 fi
192
193 # create rootfs vol
194 if [ -n "$rootfs_length" ]; then
195 local rootfs_size_param
196 if [ "$rootfs_type" = "ubifs" ]; then
197 rootfs_size_param="-m"
198 else
199 rootfs_size_param="-s $rootfs_length"
200 fi
201 if ! ubimkvol /dev/$ubidev -N $CI_ROOTPART $rootfs_size_param; then
202 echo "cannot create rootfs volume"
203 return 1;
204 fi
205 fi
206
207 # create rootfs_data vol for non-ubifs rootfs
208 if [ "$rootfs_type" != "ubifs" ]; then
209 local rootfs_data_size_param="-m"
210 if [ -n "$rootfs_data_max" ]; then
211 rootfs_data_size_param="-s $rootfs_data_max"
212 fi
213 if ! ubimkvol /dev/$ubidev -N rootfs_data $rootfs_data_size_param; then
214 if ! ubimkvol /dev/$ubidev -N rootfs_data -m; then
215 echo "cannot initialize rootfs_data volume"
216 return 1
217 fi
218 fi
219 fi
220 sync
221 return 0
222 }
223
224 nand_do_upgrade_success() {
225 local conf_tar="/tmp/sysupgrade.tgz"
226
227 sync
228 [ -f "$conf_tar" ] && nand_restore_config "$conf_tar"
229 echo "sysupgrade successful"
230 umount -a
231 reboot -f
232 }
233
234 # Flash the UBI image to MTD partition
235 nand_upgrade_ubinized() {
236 local ubi_file="$1"
237 local mtdnum="$(find_mtd_index "$CI_UBIPART")"
238
239 [ ! "$mtdnum" ] && {
240 CI_UBIPART="rootfs"
241 mtdnum="$(find_mtd_index "$CI_UBIPART")"
242 }
243
244 if [ ! "$mtdnum" ]; then
245 echo "cannot find mtd device $CI_UBIPART"
246 umount -a
247 reboot -f
248 fi
249
250 local mtddev="/dev/mtd${mtdnum}"
251 ubidetach -p "${mtddev}" || true
252 sync
253 ubiformat "${mtddev}" -y -f "${ubi_file}"
254 ubiattach -p "${mtddev}"
255 nand_do_upgrade_success
256 }
257
258 # Write the UBIFS image to UBI volume
259 nand_upgrade_ubifs() {
260 local rootfs_length=$( (cat $1 | wc -c) 2> /dev/null)
261
262 nand_upgrade_prepare_ubi "$rootfs_length" "ubifs" "" ""
263
264 local ubidev="$( nand_find_ubi "$CI_UBIPART" )"
265 local root_ubivol="$(nand_find_volume $ubidev $CI_ROOTPART)"
266 ubiupdatevol /dev/$root_ubivol -s $rootfs_length $1
267
268 nand_do_upgrade_success
269 }
270
271 nand_upgrade_fit() {
272 local fit_file="$1"
273 local fit_length="$(wc -c < "$fit_file")"
274
275 nand_upgrade_prepare_ubi "" "" "$fit_length" "1"
276
277 local fit_ubidev="$(nand_find_ubi "$CI_UBIPART")"
278 local fit_ubivol="$(nand_find_volume $fit_ubidev "$CI_KERNPART")"
279 ubiupdatevol /dev/$fit_ubivol -s $fit_length $fit_file
280
281 nand_do_upgrade_success
282 }
283
284 nand_upgrade_tar() {
285 local tar_file="$1"
286 local kernel_mtd="$(find_mtd_index $CI_KERNPART)"
287
288 local board_dir=$(tar tf "$tar_file" | grep -m 1 '^sysupgrade-.*/$')
289 board_dir=${board_dir%/}
290
291 local kernel_length=$( (tar xf "$tar_file" ${board_dir}/kernel -O | wc -c) 2> /dev/null)
292 local has_rootfs=0
293 local rootfs_length
294 local rootfs_type
295
296 tar tf "$tar_file" ${board_dir}/root 1>/dev/null 2>/dev/null && has_rootfs=1
297 [ "$has_rootfs" = "1" ] && {
298 rootfs_length=$( (tar xf "$tar_file" ${board_dir}/root -O | wc -c) 2> /dev/null)
299 rootfs_type="$(identify_tar "$tar_file" ${board_dir}/root)"
300 }
301
302 local has_kernel=1
303 local has_env=0
304
305 [ "$kernel_length" != 0 -a -n "$kernel_mtd" ] && {
306 mtd erase $CI_KERNPART
307 }
308 [ "$kernel_length" = 0 -o ! -z "$kernel_mtd" ] && has_kernel=
309 [ "$CI_KERNPART" = "none" ] && has_kernel=
310
311 nand_upgrade_prepare_ubi "$rootfs_length" "$rootfs_type" "${has_kernel:+$kernel_length}" "$has_env"
312
313 local ubidev="$( nand_find_ubi "$CI_UBIPART" )"
314 [ "$has_rootfs" = "1" ] && {
315 local root_ubivol="$( nand_find_volume $ubidev $CI_ROOTPART )"
316 tar xf "$tar_file" ${board_dir}/root -O | \
317 ubiupdatevol /dev/$root_ubivol -s $rootfs_length -
318 }
319
320 [ "$kernel_length" != 0 -a -n "$kernel_mtd" ] && {
321 tar xf "$tar_file" ${board_dir}/kernel -O | \
322 mtd -n write - $CI_KERNPART
323 }
324 [ "$has_kernel" = "1" ] && {
325 local kern_ubivol="$( nand_find_volume $ubidev $CI_KERNPART )"
326 tar xf "$tar_file" ${board_dir}/kernel -O | \
327 ubiupdatevol /dev/$kern_ubivol -s $kernel_length -
328 }
329 nand_do_upgrade_success
330 }
331
332 # Recognize type of passed file and start the upgrade process
333 nand_do_upgrade() {
334 local file_type=$(identify $1)
335
336 [ ! "$(find_mtd_index "$CI_UBIPART")" ] && CI_UBIPART="rootfs"
337
338 case "$file_type" in
339 "fit") nand_upgrade_fit $1;;
340 "ubi") nand_upgrade_ubinized $1;;
341 "ubifs") nand_upgrade_ubifs $1;;
342 *) nand_upgrade_tar $1;;
343 esac
344 }
345
346 # Check if passed file is a valid one for NAND sysupgrade. Currently it accepts
347 # 3 types of files:
348 # 1) UBI - should contain an ubinized image, header is checked for the proper
349 # MAGIC
350 # 2) UBIFS - should contain UBIFS partition that will replace "rootfs" volume,
351 # header is checked for the proper MAGIC
352 # 3) TAR - archive has to include "sysupgrade-BOARD" directory with a non-empty
353 # "CONTROL" file (at this point its content isn't verified)
354 #
355 # You usually want to call this function in platform_check_image.
356 #
357 # $(1): board name, used in case of passing TAR file
358 # $(2): file to be checked
359 nand_do_platform_check() {
360 local board_name="$1"
361 local tar_file="$2"
362 local control_length=$( (tar xf $tar_file sysupgrade-$board_name/CONTROL -O | wc -c) 2> /dev/null)
363 local file_type="$(identify $2)"
364
365 [ "$control_length" = 0 -a "$file_type" != "ubi" -a "$file_type" != "ubifs" -a "$file_type" != "fit" ] && {
366 echo "Invalid sysupgrade file."
367 return 1
368 }
369
370 return 0
371 }