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