1 # Copyright (C) 2014 OpenWrt.org
6 # 'kernel' partition or UBI volume on NAND contains the kernel
7 CI_KERNPART
="${CI_KERNPART:-kernel}"
9 # 'ubi' partition on NAND contains UBI
10 CI_UBIPART
="${CI_UBIPART:-ubi}"
12 # 'rootfs' UBI volume on NAND contains the rootfs
13 CI_ROOTPART
="${CI_ROOTPART:-rootfs}"
17 local dev
="/dev/$(basename $dir)"
19 [ -e "$dev" ] && return 0
21 local devid
="$(cat $dir/dev)"
22 local major
="${devid%%:*}"
23 local minor
="${devid##*:}"
24 mknod
"$dev" c
$major $minor
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
35 ubi_mknod
"$ubivoldir"
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"
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"'
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
92 identify_magic $
(nand_get_magic_long
"$1" "${2:-0}")
96 identify_magic $
(get_magic_long_tar
"$1" "$2")
99 nand_restore_config
() {
101 local ubidev
=$
( nand_find_ubi
$CI_UBIPART )
102 local ubivol
="$( nand_find_volume $ubidev rootfs_data )"
104 ubivol
="$( nand_find_volume $ubidev $CI_ROOTPART )"
106 if ! mount
-t ubifs
/dev
/$ubivol /tmp
/new_root
; then
107 echo "mounting ubifs $ubivol failed"
111 mv "$1" "/tmp/new_root/$BACKUP_FILE"
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
))
123 local kernel_length
="$3"
124 local has_env
="${4:-0}"
126 [ -n "$rootfs_length" -o -n "$kernel_length" ] ||
return 1
128 local mtdnum
="$( find_mtd_index "$CI_UBIPART" )"
129 if [ ! "$mtdnum" ]; then
130 echo "cannot find ubi mtd partition $CI_UBIPART"
134 local ubidev
="$( nand_find_ubi "$CI_UBIPART" )"
135 if [ ! "$ubidev" ]; then
136 ubiattach
-m "$mtdnum"
138 ubidev
="$( nand_find_ubi "$CI_UBIPART" )"
141 if [ ! "$ubidev" ]; then
142 ubiformat
/dev
/mtd
$mtdnum -y
143 ubiattach
-m "$mtdnum"
145 ubidev
="$( nand_find_ubi "$CI_UBIPART" )"
146 [ ! "$ubidev" ] && return 1
147 [ "$has_env" -gt 0 ] && {
148 ubimkvol
/dev
/$ubidev -n 0 -N ubootenv
-s 1MiB
149 ubimkvol
/dev
/$ubidev -n 1 -N ubootenv2
-s 1MiB
153 local kern_ubivol
="$( nand_find_volume $ubidev $CI_KERNPART )"
154 local root_ubivol
="$( nand_find_volume $ubidev $CI_ROOTPART )"
155 local data_ubivol
="$( nand_find_volume $ubidev rootfs_data )"
157 local ubiblk ubiblkvol
158 for ubiblk
in /dev
/ubiblock
${ubidev:3}_
* ; do
159 [ -e "$ubiblk" ] ||
continue
165 echo "removing ubiblock${ubiblk:13}"
166 ubiblkvol
=ubi
${ubiblk:13}
167 if ! ubiblock
-r /dev
/$ubiblkvol; then
168 echo "cannot remove $ubiblk"
174 [ "$kern_ubivol" ] && ubirmvol
/dev
/$ubidev -N $CI_KERNPART ||
:
175 [ "$root_ubivol" -a "$root_ubivol" != "$kern_ubivol" ] && ubirmvol
/dev
/$ubidev -N $CI_ROOTPART ||
:
176 [ "$data_ubivol" ] && ubirmvol
/dev
/$ubidev -N rootfs_data ||
:
179 if [ -n "$kernel_length" ]; then
180 if ! ubimkvol
/dev
/$ubidev -N $CI_KERNPART -s $kernel_length; then
181 echo "cannot create kernel volume"
187 if [ -n "$rootfs_length" ]; then
188 local rootfs_size_param
189 if [ "$rootfs_type" = "ubifs" ]; then
190 rootfs_size_param
="-m"
192 rootfs_size_param
="-s $rootfs_length"
194 if ! ubimkvol
/dev
/$ubidev -N $CI_ROOTPART $rootfs_size_param; then
195 echo "cannot create rootfs volume"
200 # create rootfs_data for non-ubifs rootfs
201 if [ "$rootfs_type" != "ubifs" ]; then
202 local rootfs_data_size_param
="-m"
203 if [ -n "$rootfs_data_max" ]; then
204 rootfs_data_size_param
="-s $rootfs_data_max"
206 if ! ubimkvol
/dev
/$ubidev -N rootfs_data
$rootfs_data_size_param; then
207 if ! ubimkvol
/dev
/$ubidev -N rootfs_data
-m; then
208 echo "cannot initialize rootfs_data volume"
217 nand_do_upgrade_success
() {
218 local conf_tar
="/tmp/sysupgrade.tgz"
221 [ -f "$conf_tar" ] && nand_restore_config
"$conf_tar"
222 echo "sysupgrade successful"
227 # Flash the UBI image to MTD partition
228 nand_upgrade_ubinized
() {
230 local mtdnum
="$(find_mtd_index "$CI_UBIPART")"
234 mtdnum
="$(find_mtd_index "$CI_UBIPART")"
237 if [ ! "$mtdnum" ]; then
238 echo "cannot find mtd device $CI_UBIPART"
243 local mtddev
="/dev/mtd${mtdnum}"
244 ubidetach
-p "${mtddev}" || true
246 ubiformat
"${mtddev}" -y -f "${ubi_file}"
247 ubiattach
-p "${mtddev}"
248 nand_do_upgrade_success
251 # Write the UBIFS image to UBI volume
252 nand_upgrade_ubifs
() {
253 local rootfs_length
=$
( (cat $1 |
wc -c) 2> /dev
/null
)
255 nand_upgrade_prepare_ubi
"$rootfs_length" "ubifs" "" ""
257 local ubidev
="$( nand_find_ubi "$CI_UBIPART" )"
258 local root_ubivol
="$(nand_find_volume $ubidev $CI_ROOTPART)"
259 ubiupdatevol
/dev
/$root_ubivol -s $rootfs_length $1
261 nand_do_upgrade_success
266 local fit_length
="$(wc -c < "$fit_file")"
268 nand_upgrade_prepare_ubi
"" "" "$fit_length" "1"
270 local fit_ubidev
="$(nand_find_ubi "$CI_UBIPART")"
271 local fit_ubivol
="$(nand_find_volume $fit_ubidev "$CI_KERNPART")"
272 ubiupdatevol
/dev
/$fit_ubivol -s $fit_length $fit_file
274 nand_do_upgrade_success
279 local kernel_mtd
="$(find_mtd_index $CI_KERNPART)"
281 local board_dir
=$
(tar tf
"$tar_file" |
grep -m 1 '^sysupgrade-.*/$')
282 board_dir
=${board_dir%/}
284 kernel_length
=$
( (tar xf
"$tar_file" ${board_dir}/kernel
-O |
wc -c) 2> /dev
/null
)
289 tar tf
"$tar_file" ${board_dir}/root
1>/dev
/null
2>/dev
/null
&& has_rootfs
=1
290 [ "$has_rootfs" = "1" ] && {
291 rootfs_length
=$
( (tar xf
"$tar_file" ${board_dir}/root
-O |
wc -c) 2> /dev
/null
)
292 rootfs_type
="$(identify_tar "$tar_file" ${board_dir}/root)"
298 [ "$kernel_length" != 0 -a -n "$kernel_mtd" ] && {
299 tar xf
"$tar_file" ${board_dir}/kernel
-O | mtd
write - $CI_KERNPART
301 [ "$kernel_length" = 0 -o ! -z "$kernel_mtd" ] && has_kernel
=
302 [ "$CI_KERNPART" = "none" ] && has_kernel
=
304 nand_upgrade_prepare_ubi
"$rootfs_length" "$rootfs_type" "${has_kernel:+$kernel_length}" "$has_env"
306 local ubidev
="$( nand_find_ubi "$CI_UBIPART" )"
307 [ "$has_kernel" = "1" ] && {
308 local kern_ubivol
="$( nand_find_volume $ubidev $CI_KERNPART )"
309 tar xf
"$tar_file" ${board_dir}/kernel
-O | \
310 ubiupdatevol
/dev
/$kern_ubivol -s $kernel_length -
313 [ "$has_rootfs" = "1" ] && {
314 local root_ubivol
="$( nand_find_volume $ubidev $CI_ROOTPART )"
315 tar xf
"$tar_file" ${board_dir}/root
-O | \
316 ubiupdatevol
/dev
/$root_ubivol -s $rootfs_length -
318 nand_do_upgrade_success
321 # Recognize type of passed file and start the upgrade process
323 local file_type
=$
(identify
$1)
325 [ ! "$(find_mtd_index "$CI_UBIPART")" ] && CI_UBIPART
="rootfs"
328 "fit") nand_upgrade_fit
$1;;
329 "ubi") nand_upgrade_ubinized
$1;;
330 "ubifs") nand_upgrade_ubifs
$1;;
331 *) nand_upgrade_tar
$1;;
335 # Check if passed file is a valid one for NAND sysupgrade. Currently it accepts
337 # 1) UBI - should contain an ubinized image, header is checked for the proper
339 # 2) UBIFS - should contain UBIFS partition that will replace "rootfs" volume,
340 # header is checked for the proper MAGIC
341 # 3) TAR - archive has to include "sysupgrade-BOARD" directory with a non-empty
342 # "CONTROL" file (at this point its content isn't verified)
344 # You usually want to call this function in platform_check_image.
346 # $(1): board name, used in case of passing TAR file
347 # $(2): file to be checked
348 nand_do_platform_check
() {
349 local board_name
="$1"
351 local control_length
=$
( (tar xf
$tar_file sysupgrade-
$board_name/CONTROL
-O |
wc -c) 2> /dev
/null
)
352 local file_type
="$(identify $2)"
354 [ "$control_length" = 0 -a "$file_type" != "ubi" -a "$file_type" != "ubifs" -a "$file_type" != "fit" ] && {
355 echo "Invalid sysupgrade file."