2 # Copyright (C) 2014 OpenWrt.org
7 # combined-image uses 64k blocks
9 # 'data' partition on NAND contains UBI
13 local ubidevdir ubivoldir
14 ubidevdir="/sys/devices/virtual/ubi/$1"
15 [ ! -d "$ubidevdir" ] && return 1
16 for ubivoldir in $ubidevdir/${1}_*; do
17 [ ! -d "$ubivoldir" ] && continue
18 if [ "$( cat $ubivoldir/name )" = "$2" ]; then
26 local ubidevdir ubidev mtdnum
27 mtdnum="$( find_mtd_index $1 )"
28 [ ! "$mtdnum" ] && return 1
29 for ubidevdir in /sys/devices/virtual/ubi/ubi*; do
30 [ ! -d "$ubidevdir" ] && continue
31 cmtdnum="$( cat $ubidevdir/mtd_num )"
32 [ ! "$mtdnum" ] && continue
33 if [ "$mtdnum" = "$cmtdnum" ]; then
34 ubidev=$( basename $ubidevdir )
41 nand_restore_config() {
43 local ubidev=$( nand_find_ubi $CI_UBIPART )
44 local ubivol="$( nand_find_volume $ubidev rootfs_data )"
46 ubivol="$( nand_find_volume $ubidev rootfs )"
48 if ! mount -t ubifs /dev/$ubivol /tmp/new_root; then
49 echo "mounting ubifs $ubivol failed"
53 mv "$1" "/tmp/new_root/sysupgrade.tgz"
59 nand_upgrade_ubinized() {
60 local upgrade_image="$1"
62 local save_config="$3"
63 local mtdnum="$( find_mtd_index "$CI_UBIPART" )"
64 if [ ! "$mtdnum" ]; then
65 echo "cannot find mtd device $CI_UBIPART"
68 local mtddev="/dev/mtd${mtdnum}"
69 ubidetach -p "${mtddev}" || true
71 ubiformat "${mtddev}" -y -f "$upgrade_image"
72 ubiattach -p "${mtddev}"
74 if [ -f "$conf_tar" -a "$save_config" -eq 1 ]; then
75 nand_restore_config "$conf_tar"
80 # get the first 4 bytes (magic) of a given file starting at offset in hex format
82 dd if="$2" skip=$1 bs=$CI_BLKSZ count=1 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
87 local magic=$( get_magic_long_at ${2:-0} "$1" )
105 echo "unknown $magic"
110 nand_upgrade_combined_ubi() {
111 local kernel_image="$1"
112 local kernel_length=0
113 local rootfs_image="$2"
114 local rootfs_length=`ls -la $rootfs_image | awk '{ print $5}')`
116 local has_env="${4:-0}"
118 local root_fs="$( identify "$rootfs_image" )"
119 local mtdnum="$( find_mtd_index "$CI_UBIPART" )"
122 [ -z "$kernel_image" ] || {
124 kernel_length=`ls -la $kernel_image | awk '{ print $5}')`
125 echo "kernel length $kernel_length"
127 [ "$has_kernel" = 0 ] || echo "kernel is inside ubi"
128 echo "rootfs type $root_fs, length $rootfs_length"
130 if [ ! "$mtdnum" ]; then
131 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" )"
140 if [ ! "$ubidev" ]; then
141 ubiformat /dev/mtd$mtdnum -y
142 ubiattach -m "$mtdnum"
144 ubidev="$( nand_find_ubi "$CI_UBIPART" )"
145 [ -z "$has_env" ] || {
146 ubimkvol /dev/$ubidev -n 0 -N ubootenv -s 1MiB
147 ubimkvol /dev/$ubidev -n 1 -N ubootenv2 -s 1MiB
150 local kern_ubivol="$( nand_find_volume $ubidev kernel )"
151 local root_ubivol="$( nand_find_volume $ubidev rootfs )"
152 local data_ubivol="$( nand_find_volume $ubidev rootfs_data )"
154 # remove ubiblock device of rootfs
155 local root_ubiblk="ubiblock${root_ubivol:3}"
156 if [ "$root_ubivol" -a -e "/dev/$root_ubiblk" ]; then
157 echo "removing $root_ubiblk"
158 if ! ubiblock -r /dev/$root_ubivol; then
159 echo "cannot remove $root_ubiblk"
165 if [ "$kern_ubivol" ]; then
166 ubirmvol /dev/$ubidev -N kernel || true
168 if [ "$root_ubivol" ]; then
169 ubirmvol /dev/$ubidev -N rootfs || true
171 if [ "$data_ubivol" ]; then
172 ubirmvol /dev/$ubidev -N rootfs_data || true
176 if [ "$has_kernel" = "1" ]; then
177 if ! ubimkvol /dev/$ubidev -N kernel -s $kernel_length; then
178 echo "cannot create kernel volume"
184 local root_size_param
185 if [ "$root_fs" = "ubifs" ]; then
188 root_size_param="-s $rootfs_length"
190 if ! ubimkvol /dev/$ubidev -N rootfs $root_size_param; then
191 echo "cannot create rootfs volume"
195 # create rootfs_data for non-ubifs rootfs
196 if [ "$root_fs" != "ubifs" ]; then
197 if ! ubimkvol /dev/$ubidev -N rootfs_data -m; then
198 echo "cannot initialize rootfs_data volume"
204 if [ "$has_kernel" = "1" ]; then
205 local kern_ubivol="$( nand_find_volume $ubidev kernel )"
206 ubiupdatevol /dev/$kern_ubivol -s $kernel_length $kernel_image
209 local root_ubivol="$( nand_find_volume $ubidev rootfs )"
210 ubiupdatevol /dev/$root_ubivol -s $rootfs_length $rootfs_image
211 if [ -f "$conf_tar" ]; then
212 nand_restore_config "$conf_tar"
214 echo "sysupgrade successfull"
218 nand_do_upgrade_stage1() {
219 local board_name="$1"
224 tar xzf $tar_file -C /tmp/
225 [ -f "/tmp/sysupgrade-$board_name/CONTROL" ] || {
226 echo "failed to find /tmp/sysupgrade-$board_name/CONTROL"
230 kernel_file=/tmp/sysupgrade-$board_name/kernel
231 [ -f "$kernel_file" ] || {
232 echo "$kernel_file is missing"
236 rootfs_file=/tmp/sysupgrade-$board_name/root
237 [ -f "$rootfs_file" ] || {
238 echo "$rootfs_file is missing"
242 echo -n /tmp/sysupgrade-$board_name > /tmp/sysupgrade-nand-folder
243 cp /sbin/upgraded /tmp/
248 nand_do_upgrade_stage2() {
250 kernel_file=$1/kernel
251 config_file=$1/config
253 [ -f $config_file ] || config_file=""
257 [ "$UBI_KERNEL" = "1" ] || {
258 mtd write $kernel_file kernel
261 nand_upgrade_combined_ubi "$kernel_file" "$rootfs_file" "$conf_tar" "$UBI_ENV"
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-folder ] && {
295 folder="$(cat /tmp/sysupgrade-nand-folder)"
296 [ "$SAVE_CONFIG" = 1 -a -f "$CONF_TAR" ] &&
297 cp $CONF_TAR $folder/config
299 ubus call system nandupgrade "{\"folder\": \"$folder\" }"
303 append sysupgrade_pre_upgrade nand_upgrade_stage1