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
() {
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"
110 if ! mount
-t ubifs
/dev
/$ubivol /tmp
/new_root
; then
111 echo "cannot mount ubifs volume $ubivol"
115 if mv "$1" "/tmp/new_root/$BACKUP_FILE"; then
116 if umount
/tmp
/new_root
; then
117 echo "configuration saved"
124 echo "could not save configuration to ubifs volume $ubivol"
129 nand_remove_ubiblock
() {
131 local ubiblk
=ubiblock
${ubivol:3}
132 if [ -e /dev
/$ubiblk ]; then
133 echo "removing $ubiblk"
134 if ! ubiblock
-r /dev
/$ubivol; then
135 echo "cannot remove $ubiblk"
141 nand_upgrade_prepare_ubi
() {
142 local rootfs_length
="$1"
143 local rootfs_type
="$2"
144 local rootfs_data_max
="$(fw_printenv -n rootfs_data_max 2>/dev/null)"
145 [ -n "$rootfs_data_max" ] && rootfs_data_max
=$
((rootfs_data_max
))
147 local kernel_length
="$3"
148 local has_env
="${4:-0}"
150 [ -n "$rootfs_length" -o -n "$kernel_length" ] ||
return 1
152 local mtdnum
="$( find_mtd_index "$CI_UBIPART" )"
153 if [ ! "$mtdnum" ]; then
154 echo "cannot find ubi mtd partition $CI_UBIPART"
158 local ubidev
="$( nand_find_ubi "$CI_UBIPART" )"
159 if [ ! "$ubidev" ]; then
160 ubiattach
-m "$mtdnum"
161 ubidev
="$( nand_find_ubi "$CI_UBIPART" )"
163 if [ ! "$ubidev" ]; then
164 ubiformat
/dev
/mtd
$mtdnum -y
165 ubiattach
-m "$mtdnum"
166 ubidev
="$( nand_find_ubi "$CI_UBIPART" )"
168 if [ ! "$ubidev" ]; then
169 echo "cannot attach ubi mtd partition $CI_UBIPART"
173 if [ "$has_env" -gt 0 ]; then
174 ubimkvol
/dev
/$ubidev -n 0 -N ubootenv
-s 1MiB
175 ubimkvol
/dev
/$ubidev -n 1 -N ubootenv2
-s 1MiB
180 local kern_ubivol
="$( nand_find_volume $ubidev "$CI_KERNPART" )"
181 local root_ubivol
="$( nand_find_volume $ubidev "$CI_ROOTPART" )"
182 local data_ubivol
="$( nand_find_volume $ubidev rootfs_data )"
183 [ "$root_ubivol" = "$kern_ubivol" ] && root_ubivol
=
186 [ "$kern_ubivol" ] && { nand_remove_ubiblock
$kern_ubivol ||
return 1; }
187 [ "$root_ubivol" ] && { nand_remove_ubiblock
$root_ubivol ||
return 1; }
188 [ "$data_ubivol" ] && { nand_remove_ubiblock
$data_ubivol ||
return 1; }
191 [ "$kern_ubivol" ] && ubirmvol
/dev
/$ubidev -N "$CI_KERNPART" ||
:
192 [ "$root_ubivol" ] && ubirmvol
/dev
/$ubidev -N "$CI_ROOTPART" ||
:
193 [ "$data_ubivol" ] && ubirmvol
/dev
/$ubidev -N rootfs_data ||
:
196 if [ -n "$kernel_length" ]; then
197 if ! ubimkvol
/dev
/$ubidev -N "$CI_KERNPART" -s $kernel_length; then
198 echo "cannot create kernel volume"
204 if [ -n "$rootfs_length" ]; then
205 local rootfs_size_param
206 if [ "$rootfs_type" = "ubifs" ]; then
207 rootfs_size_param
="-m"
209 rootfs_size_param
="-s $rootfs_length"
211 if ! ubimkvol
/dev
/$ubidev -N "$CI_ROOTPART" $rootfs_size_param; then
212 echo "cannot create rootfs volume"
217 # create rootfs_data vol for non-ubifs rootfs
218 if [ "$rootfs_type" != "ubifs" ]; then
219 local rootfs_data_size_param
="-m"
220 if [ -n "$rootfs_data_max" ]; then
221 rootfs_data_size_param
="-s $rootfs_data_max"
223 if ! ubimkvol
/dev
/$ubidev -N rootfs_data
$rootfs_data_size_param; then
224 if ! ubimkvol
/dev
/$ubidev -N rootfs_data
-m; then
225 echo "cannot initialize rootfs_data volume"
234 nand_do_upgrade_success
() {
235 local conf_tar
="/tmp/sysupgrade.tgz"
236 if { [ ! -f "$conf_tar" ] || nand_restore_config
"$conf_tar"; } && sync
; then
237 echo "sysupgrade successful"
243 # Flash the UBI image to MTD partition
244 nand_upgrade_ubinized
() {
246 local mtdnum
="$(find_mtd_index "$CI_UBIPART")"
248 if [ ! "$mtdnum" ]; then
249 echo "cannot find mtd device $CI_UBIPART"
254 local mtddev
="/dev/mtd${mtdnum}"
255 ubidetach
-p "${mtddev}" ||
:
256 ubiformat
"${mtddev}" -y -f "${ubi_file}"
257 ubiattach
-p "${mtddev}"
259 nand_do_upgrade_success
262 # Write the UBIFS image to UBI volume
263 nand_upgrade_ubifs
() {
264 local rootfs_length
=$
( (cat $1 |
wc -c) 2> /dev
/null
)
266 nand_upgrade_prepare_ubi
"$rootfs_length" "ubifs" "" ""
268 local ubidev
="$( nand_find_ubi "$CI_UBIPART" )"
269 local root_ubivol
="$(nand_find_volume $ubidev "$CI_ROOTPART")"
270 ubiupdatevol
/dev
/$root_ubivol -s $rootfs_length $1
272 nand_do_upgrade_success
277 local fit_length
="$(wc -c < "$fit_file")"
279 nand_upgrade_prepare_ubi
"" "" "$fit_length" "1"
281 local fit_ubidev
="$(nand_find_ubi "$CI_UBIPART")"
282 local fit_ubivol
="$(nand_find_volume $fit_ubidev "$CI_KERNPART")"
283 ubiupdatevol
/dev
/$fit_ubivol -s $fit_length $fit_file
285 nand_do_upgrade_success
291 local board_dir
="$(tar tf "$tar_file" | grep -m 1 '^sysupgrade-.*/$')"
292 board_dir
="${board_dir%/}"
294 local kernel_mtd kernel_length
295 if [ "$CI_KERNPART" != "none" ]; then
296 kernel_mtd
="$(find_mtd_index "$CI_KERNPART")"
297 kernel_length
=$
( (tar xf
"$tar_file" "$board_dir/kernel" -O |
wc -c) 2> /dev
/null
)
298 [ "$kernel_length" = 0 ] && kernel_length
=
300 local rootfs_length
=$
( (tar xf
"$tar_file" "$board_dir/root" -O |
wc -c) 2> /dev
/null
)
301 [ "$rootfs_length" = 0 ] && rootfs_length
=
303 [ "$rootfs_length" ] && rootfs_type
="$(identify_tar "$tar_file" "$board_dir/root
")"
305 local ubi_kernel_length
306 if [ "$kernel_length" ]; then
307 if [ "$kernel_mtd" ]; then
308 mtd erase
"$CI_KERNPART"
310 ubi_kernel_length
="$kernel_length"
314 nand_upgrade_prepare_ubi
"$rootfs_length" "$rootfs_type" "$ubi_kernel_length" "$has_env"
316 local ubidev
="$( nand_find_ubi "$CI_UBIPART" )"
317 if [ "$rootfs_length" ]; then
318 local root_ubivol
="$( nand_find_volume $ubidev "$CI_ROOTPART" )"
319 tar xf
"$tar_file" "$board_dir/root" -O | \
320 ubiupdatevol
/dev
/$root_ubivol -s $rootfs_length -
322 if [ "$kernel_length" ]; then
323 if [ "$kernel_mtd" ]; then
324 tar xf
"$tar_file" "$board_dir/kernel" -O | \
325 mtd
-n write - "$CI_KERNPART"
327 local kern_ubivol
="$( nand_find_volume $ubidev "$CI_KERNPART" )"
328 tar xf
"$tar_file" "$board_dir/kernel" -O | \
329 ubiupdatevol
/dev
/$kern_ubivol -s $kernel_length -
333 nand_do_upgrade_success
336 # Recognize type of passed file and start the upgrade process
338 local file_type
=$
(identify
$1)
340 [ ! "$(find_mtd_index "$CI_UBIPART")" ] && CI_UBIPART
=rootfs
343 "fit") nand_upgrade_fit
$1;;
344 "ubi") nand_upgrade_ubinized
$1;;
345 "ubifs") nand_upgrade_ubifs
$1;;
346 *) nand_upgrade_tar
$1;;
350 # Check if passed file is a valid one for NAND sysupgrade. Currently it accepts
352 # 1) UBI - should contain an ubinized image, header is checked for the proper
354 # 2) UBIFS - should contain UBIFS partition that will replace "rootfs" volume,
355 # header is checked for the proper MAGIC
356 # 3) TAR - archive has to include "sysupgrade-BOARD" directory with a non-empty
357 # "CONTROL" file (at this point its content isn't verified)
359 # You usually want to call this function in platform_check_image.
361 # $(1): board name, used in case of passing TAR file
362 # $(2): file to be checked
363 nand_do_platform_check
() {
364 local board_name
="$1"
366 local control_length
=$
( (tar xf
$tar_file sysupgrade-
$board_name/CONTROL
-O |
wc -c) 2> /dev
/null
)
367 local file_type
="$(identify $2)"
369 [ "$control_length" = 0 -a "$file_type" != "ubi" -a "$file_type" != "ubifs" -a "$file_type" != "fit" ] && {
370 echo "Invalid sysupgrade file."