2 # Copyright (C) 2014 OpenWrt.org
7 # 'ubi' partition on NAND contains UBI
11 local ubidevdir ubivoldir
12 ubidevdir="/sys/devices/virtual/ubi/$1"
13 [ ! -d "$ubidevdir" ] && return 1
14 for ubivoldir in $ubidevdir/${1}_*; do
15 [ ! -d "$ubivoldir" ] && continue
16 if [ "$( cat $ubivoldir/name )" = "$2" ]; then
24 local ubidevdir ubidev mtdnum
25 mtdnum="$( find_mtd_index $1 )"
26 [ ! "$mtdnum" ] && return 1
27 for ubidevdir in /sys/devices/virtual/ubi/ubi*; do
28 [ ! -d "$ubidevdir" ] && continue
29 cmtdnum="$( cat $ubidevdir/mtd_num )"
30 [ ! "$mtdnum" ] && continue
31 if [ "$mtdnum" = "$cmtdnum" ]; then
32 ubidev=$( basename $ubidevdir )
39 nand_get_magic_long() {
40 dd if="$1" skip=$2 bs=4 count=1 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
43 get_magic_long_tar() {
44 ( tar xf $1 $2 -O | dd bs=4 count=1 | hexdump -v -n 4 -e '1/1 "%02x"') 2> /dev/null
73 identify_magic $(nand_get_magic_long "$1" "${2:-0}")
77 identify_magic $(get_magic_long_tar "$1" "$2")
80 nand_restore_config() {
82 local ubidev=$( nand_find_ubi $CI_UBIPART )
83 local ubivol="$( nand_find_volume $ubidev rootfs_data )"
85 ubivol="$( nand_find_volume $ubidev rootfs )"
87 if ! mount -t ubifs /dev/$ubivol /tmp/new_root; then
88 echo "mounting ubifs $ubivol failed"
92 mv "$1" "/tmp/new_root/sysupgrade.tgz"
98 nand_upgrade_prepare_ubi() {
99 local rootfs_length="$1"
100 local rootfs_type="$2"
101 local has_kernel="${3:-0}"
102 local has_env="${4:-0}"
104 local mtdnum="$( find_mtd_index "$CI_UBIPART" )"
105 if [ ! "$mtdnum" ]; then
106 echo "cannot find ubi mtd partition $CI_UBIPART"
110 local ubidev="$( nand_find_ubi "$CI_UBIPART" )"
111 if [ ! "$ubidev" ]; then
112 ubiattach -m "$mtdnum"
114 ubidev="$( nand_find_ubi "$CI_UBIPART" )"
117 if [ ! "$ubidev" ]; then
118 ubiformat /dev/mtd$mtdnum -y
119 ubiattach -m "$mtdnum"
121 ubidev="$( nand_find_ubi "$CI_UBIPART" )"
122 [ -z "$has_env" ] || {
123 ubimkvol /dev/$ubidev -n 0 -N ubootenv -s 1MiB
124 ubimkvol /dev/$ubidev -n 1 -N ubootenv2 -s 1MiB
128 local kern_ubivol="$( nand_find_volume $ubidev kernel )"
129 local root_ubivol="$( nand_find_volume $ubidev rootfs )"
130 local data_ubivol="$( nand_find_volume $ubidev rootfs_data )"
132 # remove ubiblock device of rootfs
133 local root_ubiblk="ubiblock${root_ubivol:3}"
134 if [ "$root_ubivol" -a -e "/dev/$root_ubiblk" ]; then
135 echo "removing $root_ubiblk"
136 if ! ubiblock -r /dev/$root_ubivol; then
137 echo "cannot remove $root_ubiblk"
143 [ "$kern_ubivol" ] && ubirmvol /dev/$ubidev -N kernel || true
144 [ "$root_ubivol" ] && ubirmvol /dev/$ubidev -N rootfs || true
145 [ "$data_ubivol" ] && ubirmvol /dev/$ubidev -N rootfs_data || true
148 if [ "$has_kernel" = "1" ]; then
149 if ! ubimkvol /dev/$ubidev -N kernel -s $kernel_length; then
150 echo "cannot create kernel volume"
156 local root_size_param
157 if [ "$rootfs_type" = "ubifs" ]; then
160 root_size_param="-s $rootfs_length"
162 if ! ubimkvol /dev/$ubidev -N rootfs $root_size_param; then
163 echo "cannot create rootfs volume"
167 # create rootfs_data for non-ubifs rootfs
168 if [ "$rootfs_type" != "ubifs" ]; then
169 if ! ubimkvol /dev/$ubidev -N rootfs_data -m; then
170 echo "cannot initialize rootfs_data volume"
178 nand_do_upgrade_success() {
179 local conf_tar="/tmp/sysupgrade.tgz"
182 [ -f "$conf_tar" ] && nand_restore_config "$conf_tar"
183 echo "sysupgrade successful"
187 nand_upgrade_ubinized() {
189 local mtdnum="$(find_mtd_index "$CI_UBIPART")"
193 mtdnum="$(find_mtd_index "$CI_UBIPART")"
196 if [ ! "$mtdnum" ]; then
197 echo "cannot find mtd device $CI_UBIPART"
201 local mtddev="/dev/mtd${mtdnum}"
202 ubidetach -p "${mtddev}" || true
204 ubiformat "${mtddev}" -y -f "${ubi_file}"
205 ubiattach -p "${mtddev}"
206 nand_do_upgrade_success
209 nand_upgrade_ubifs() {
210 local rootfs_length=`(cat $1 | wc -c) 2> /dev/null`
212 nand_upgrade_prepare_ubi "$rootfs_length" "ubifs" "0" "0"
214 local ubidev="$( nand_find_ubi "$CI_UBIPART" )"
215 local root_ubivol="$(nand_find_volume $ubidev rootfs)"
216 ubiupdatevol /dev/$root_ubivol -s $rootfs_length $1
218 nand_do_upgrade_success
223 local board_name="$(cat /tmp/sysinfo/board_name)"
224 local kernel_mtd="$(find_mtd_index kernel)"
226 local kernel_length=`(tar xf $tar_file sysupgrade-$board_name/kernel -O | wc -c) 2> /dev/null`
227 local rootfs_length=`(tar xf $tar_file sysupgrade-$board_name/root -O | wc -c) 2> /dev/null`
229 local rootfs_type="$(identify_tar "$tar_file" sysupgrade-$board_name/root)"
234 [ "kernel_length" = 0 -o -z "$kernel_mtd" ] || {
235 tar xf $tar_file sysupgrade-$board_name/kernel -O | mtd write - kernel
237 [ "kernel_length" = 0 -o ! -z "$kernel_mtd" ] && has_kernel=0
239 nand_upgrade_prepare_ubi "$rootfs_length" "$rootfs_type" "$has_kernel" "$has_env"
241 local ubidev="$( nand_find_ubi "$CI_UBIPART" )"
242 [ "$has_kernel" = "1" ] && {
243 local kern_ubivol="$(nand_find_volume $ubidev kernel)"
244 tar xf $tar_file sysupgrade-$board_name/kernel -O | \
245 ubiupdatevol /dev/$kern_ubivol -s $kernel_length -
248 local root_ubivol="$(nand_find_volume $ubidev rootfs)"
249 tar xf $tar_file sysupgrade-$board_name/root -O | \
250 ubiupdatevol /dev/$root_ubivol -s $rootfs_length -
252 nand_do_upgrade_success
255 nand_do_upgrade_stage2() {
256 local file_type=$(identify $1)
258 [ ! "$(find_mtd_index "$CI_UBIPART")" ] && CI_UBIPART="rootfs"
260 [ "$file_type" == "ubi" ] && nand_upgrade_ubinized $1
261 [ "$file_type" == "ubifs" ] && nand_upgrade_ubifs $1
265 nand_upgrade_stage2() {
268 touch /tmp/sysupgrade
280 if [ -n "$(rootfs_type)" ]; then
281 v "Switching to ramdisk..."
282 run_ramfs ". /lib/functions.sh; include /lib/upgrade; nand_do_upgrade_stage2 $2"
284 nand_do_upgrade_stage2 $2
288 echo "Nand upgrade failed"
293 nand_upgrade_stage1() {
294 [ -f /tmp/sysupgrade-nand-path ] && {
295 path="$(cat /tmp/sysupgrade-nand-path)"
296 [ "$SAVE_CONFIG" != 1 -a -f "$CONF_TAR" ] &&
299 ubus call system nandupgrade "{\"path\": \"$path\" }"
303 append sysupgrade_pre_upgrade nand_upgrade_stage1
305 nand_do_platform_check() {
306 local board_name="$1"
308 local control_length=`(tar xf $tar_file sysupgrade-$board_name/CONTROL -O | wc -c) 2> /dev/null`
309 local file_type="$(identify $2)"
311 [ "$control_length" = 0 -a "$file_type" != "ubi" -a "$file_type" != "ubifs" ] && {
312 echo "Invalid sysupgrade file."
316 echo -n $2 > /tmp/sysupgrade-nand-path
317 cp /sbin/upgraded /tmp/