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