2 # Copyright (C) 2014 OpenWrt.org
7 # 'kernel' partition on NAND contains the kernel
8 CI_KERNPART="${CI_KERNPART:-kernel}"
10 # 'ubi' partition on NAND contains UBI
11 CI_UBIPART="${CI_UBIPART:-ubi}"
15 local dev="/dev/$(basename $dir)"
17 [ -e "$dev" ] && return 0
19 local devid="$(cat $dir/dev)"
20 local major="${devid%%:*}"
21 local minor="${devid##*:}"
22 mknod "$dev" c $major $minor
26 local ubidevdir ubivoldir
27 ubidevdir="/sys/devices/virtual/ubi/$1"
28 [ ! -d "$ubidevdir" ] && return 1
29 for ubivoldir in $ubidevdir/${1}_*; do
30 [ ! -d "$ubivoldir" ] && continue
31 if [ "$( cat $ubivoldir/name )" = "$2" ]; then
33 ubi_mknod "$ubivoldir"
40 local ubidevdir ubidev mtdnum
41 mtdnum="$( find_mtd_index $1 )"
42 [ ! "$mtdnum" ] && return 1
43 for ubidevdir in /sys/devices/virtual/ubi/ubi*; do
44 [ ! -d "$ubidevdir" ] && continue
45 cmtdnum="$( cat $ubidevdir/mtd_num )"
46 [ ! "$mtdnum" ] && continue
47 if [ "$mtdnum" = "$cmtdnum" ]; then
48 ubidev=$( basename $ubidevdir )
49 ubi_mknod "$ubidevdir"
56 nand_get_magic_long() {
57 dd if="$1" skip=$2 bs=4 count=1 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
60 get_magic_long_tar() {
61 ( tar xf $1 $2 -O | dd bs=4 count=1 | hexdump -v -n 4 -e '1/1 "%02x"') 2> /dev/null
90 identify_magic $(nand_get_magic_long "$1" "${2:-0}")
94 identify_magic $(get_magic_long_tar "$1" "$2")
97 nand_restore_config() {
99 local ubidev=$( nand_find_ubi $CI_UBIPART )
100 local ubivol="$( nand_find_volume $ubidev rootfs_data )"
102 ubivol="$( nand_find_volume $ubidev rootfs )"
104 if ! mount -t ubifs /dev/$ubivol /tmp/new_root; then
105 echo "mounting ubifs $ubivol failed"
109 mv "$1" "/tmp/new_root/sysupgrade.tgz"
115 nand_upgrade_prepare_ubi() {
116 local rootfs_length="$1"
117 local rootfs_type="$2"
118 local has_kernel="${3:-0}"
119 local has_env="${4:-0}"
121 local mtdnum="$( find_mtd_index "$CI_UBIPART" )"
122 if [ ! "$mtdnum" ]; then
123 echo "cannot find ubi mtd partition $CI_UBIPART"
127 local ubidev="$( nand_find_ubi "$CI_UBIPART" )"
128 if [ ! "$ubidev" ]; then
129 ubiattach -m "$mtdnum"
131 ubidev="$( nand_find_ubi "$CI_UBIPART" )"
134 if [ ! "$ubidev" ]; then
135 ubiformat /dev/mtd$mtdnum -y
136 ubiattach -m "$mtdnum"
138 ubidev="$( nand_find_ubi "$CI_UBIPART" )"
139 [ "$has_env" -gt 0 ] && {
140 ubimkvol /dev/$ubidev -n 0 -N ubootenv -s 1MiB
141 ubimkvol /dev/$ubidev -n 1 -N ubootenv2 -s 1MiB
145 local kern_ubivol="$( nand_find_volume $ubidev kernel )"
146 local root_ubivol="$( nand_find_volume $ubidev rootfs )"
147 local data_ubivol="$( nand_find_volume $ubidev rootfs_data )"
149 # remove ubiblock device of rootfs
150 local root_ubiblk="ubiblock${root_ubivol:3}"
151 if [ "$root_ubivol" -a -e "/dev/$root_ubiblk" ]; then
152 echo "removing $root_ubiblk"
153 if ! ubiblock -r /dev/$root_ubivol; then
154 echo "cannot remove $root_ubiblk"
160 [ "$kern_ubivol" ] && ubirmvol /dev/$ubidev -N kernel || true
161 [ "$root_ubivol" ] && ubirmvol /dev/$ubidev -N rootfs || true
162 [ "$data_ubivol" ] && ubirmvol /dev/$ubidev -N rootfs_data || true
165 if [ "$has_kernel" = "1" ]; then
166 if ! ubimkvol /dev/$ubidev -N kernel -s $kernel_length; then
167 echo "cannot create kernel volume"
173 local root_size_param
174 if [ "$rootfs_type" = "ubifs" ]; then
177 root_size_param="-s $rootfs_length"
179 if ! ubimkvol /dev/$ubidev -N rootfs $root_size_param; then
180 echo "cannot create rootfs volume"
184 # create rootfs_data for non-ubifs rootfs
185 if [ "$rootfs_type" != "ubifs" ]; then
186 if ! ubimkvol /dev/$ubidev -N rootfs_data -m; then
187 echo "cannot initialize rootfs_data volume"
195 nand_do_upgrade_success() {
196 local conf_tar="/tmp/sysupgrade.tgz"
199 [ -f "$conf_tar" ] && nand_restore_config "$conf_tar"
200 echo "sysupgrade successful"
205 # Flash the UBI image to MTD partition
206 nand_upgrade_ubinized() {
208 local mtdnum="$(find_mtd_index "$CI_UBIPART")"
212 mtdnum="$(find_mtd_index "$CI_UBIPART")"
215 if [ ! "$mtdnum" ]; then
216 echo "cannot find mtd device $CI_UBIPART"
221 local mtddev="/dev/mtd${mtdnum}"
222 ubidetach -p "${mtddev}" || true
224 ubiformat "${mtddev}" -y -f "${ubi_file}"
225 ubiattach -p "${mtddev}"
226 nand_do_upgrade_success
229 # Write the UBIFS image to UBI volume
230 nand_upgrade_ubifs() {
231 local rootfs_length=`(cat $1 | wc -c) 2> /dev/null`
233 nand_upgrade_prepare_ubi "$rootfs_length" "ubifs" "0" "0"
235 local ubidev="$( nand_find_ubi "$CI_UBIPART" )"
236 local root_ubivol="$(nand_find_volume $ubidev rootfs)"
237 ubiupdatevol /dev/$root_ubivol -s $rootfs_length $1
239 nand_do_upgrade_success
243 if type 'platform_nand_board_name' >/dev/null 2>/dev/null; then
244 platform_nand_board_name
248 cat /tmp/sysinfo/board_name
253 local board_name="$(nand_board_name)"
254 local kernel_mtd="$(find_mtd_index $CI_KERNPART)"
256 local kernel_length=`(tar xf $tar_file sysupgrade-$board_name/kernel -O | wc -c) 2> /dev/null`
257 local rootfs_length=`(tar xf $tar_file sysupgrade-$board_name/root -O | wc -c) 2> /dev/null`
259 local rootfs_type="$(identify_tar "$tar_file" sysupgrade-$board_name/root)"
264 [ "$kernel_length" != 0 -a -n "$kernel_mtd" ] && {
265 tar xf $tar_file sysupgrade-$board_name/kernel -O | mtd write - $CI_KERNPART
267 [ "$kernel_length" = 0 -o ! -z "$kernel_mtd" ] && has_kernel=0
269 nand_upgrade_prepare_ubi "$rootfs_length" "$rootfs_type" "$has_kernel" "$has_env"
271 local ubidev="$( nand_find_ubi "$CI_UBIPART" )"
272 [ "$has_kernel" = "1" ] && {
273 local kern_ubivol="$(nand_find_volume $ubidev kernel)"
274 tar xf $tar_file sysupgrade-$board_name/kernel -O | \
275 ubiupdatevol /dev/$kern_ubivol -s $kernel_length -
278 local root_ubivol="$(nand_find_volume $ubidev rootfs)"
279 tar xf $tar_file sysupgrade-$board_name/root -O | \
280 ubiupdatevol /dev/$root_ubivol -s $rootfs_length -
282 nand_do_upgrade_success
285 # Recognize type of passed file and start the upgrade process
286 nand_do_upgrade_stage2() {
287 local file_type=$(identify $1)
289 if type 'platform_nand_pre_upgrade' >/dev/null 2>/dev/null; then
290 platform_nand_pre_upgrade "$1"
293 [ ! "$(find_mtd_index "$CI_UBIPART")" ] && CI_UBIPART="rootfs"
296 "ubi") nand_upgrade_ubinized $1;;
297 "ubifs") nand_upgrade_ubifs $1;;
298 *) nand_upgrade_tar $1;;
302 nand_upgrade_stage2() {
305 touch /tmp/sysupgrade
317 if [ -n "$(rootfs_type)" ]; then
318 v "Switching to ramdisk..."
319 run_ramfs ". /lib/functions.sh; include /lib/upgrade; nand_do_upgrade_stage2 $2"
321 nand_do_upgrade_stage2 $2
325 echo "Nand upgrade failed"
330 nand_upgrade_stage1() {
331 [ -f /tmp/sysupgrade-nand-path ] && {
332 path="$(cat /tmp/sysupgrade-nand-path)"
333 [ "$SAVE_CONFIG" != 1 -a -f "$CONF_TAR" ] &&
336 ubus call system nandupgrade "{\"path\": \"$path\" }"
341 # Check if passed file is a valid one for NAND sysupgrade. Currently it accepts
343 # 1) UBI - should contain an ubinized image, header is checked for the proper
345 # 2) UBIFS - should contain UBIFS partition that will replace "rootfs" volume,
346 # header is checked for the proper MAGIC
347 # 3) TAR - archive has to include "sysupgrade-BOARD" directory with a non-empty
348 # "CONTROL" file (at this point its content isn't verified)
350 # You usually want to call this function in platform_check_image.
352 # $(1): board name, used in case of passing TAR file
353 # $(2): file to be checked
354 nand_do_platform_check() {
355 local board_name="$1"
357 local control_length=`(tar xf $tar_file sysupgrade-$board_name/CONTROL -O | wc -c) 2> /dev/null`
358 local file_type="$(identify $2)"
360 [ "$control_length" = 0 -a "$file_type" != "ubi" -a "$file_type" != "ubifs" ] && {
361 echo "Invalid sysupgrade file."
368 # Start NAND upgrade process
370 # $(1): file to be used for upgrade
372 echo -n $1 > /tmp/sysupgrade-nand-path
373 cp /sbin/upgraded /tmp/