9131ddaa7bb949d30ae39ebff8ed3bbc53e05df7
[openwrt/svn-archive/archive.git] / package / system / procd / files / nand.sh
1 #!/bin/sh
2 # Copyright (C) 2014 OpenWrt.org
3 #
4
5 . /lib/functions.sh
6
7 # 'data' partition on NAND contains UBI
8 CI_UBIPART="ubi"
9
10 nand_find_volume() {
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
17 basename $ubivoldir
18 return 0
19 fi
20 done
21 }
22
23 nand_find_ubi() {
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 )
33 echo $ubidev
34 return 0
35 fi
36 done
37 }
38
39 get_magic_long() {
40 dd if="$1" skip=$2 bs=4 count=1 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
41 }
42
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
45 }
46
47 identify_magic() {
48 local magic=$1
49 case "$magic" in
50 "55424923")
51 echo "ubi"
52 ;;
53 "31181006")
54 echo "ubifs"
55 ;;
56 "68737173")
57 echo "squashfs"
58 ;;
59 "d00dfeed")
60 echo "fit"
61 ;;
62 "4349"*)
63 echo "combined"
64 ;;
65 *)
66 echo "unknown $magic"
67 ;;
68 esac
69 }
70
71
72 identify() {
73 identify_magic $(get_magic_long "$1" "${2:-0}")
74 }
75
76 identify_tar() {
77 identify_magic $(get_magic_long_tar "$1" "$2")
78 }
79
80 nand_restore_config() {
81 sync
82 local ubidev=$( nand_find_ubi $CI_UBIPART )
83 local ubivol="$( nand_find_volume $ubidev rootfs_data )"
84 [ ! "$ubivol" ] &&
85 ubivol="$( nand_find_volume $ubidev rootfs )"
86 mkdir /tmp/new_root
87 if ! mount -t ubifs /dev/$ubivol /tmp/new_root; then
88 echo "mounting ubifs $ubivol failed"
89 rmdir /tmp/new_root
90 return 1
91 fi
92 mv "$1" "/tmp/new_root/sysupgrade.tgz"
93 umount /tmp/new_root
94 sync
95 rmdir /tmp/new_root
96 }
97
98 nand_upgrade_prepare_ubi() {
99 local rootfs_length="$1"
100 local rootfs_type="$1"
101 local has_kernel="${2:-0}"
102 local has_env="${3:-0}"
103
104 local mtdnum="$( find_mtd_index "$CI_UBIPART" )"
105 if [ ! "$mtdnum" ]; then
106 echo "cannot find ubi mtd partition $CI_UBIPART"
107 return 1
108 fi
109
110 local ubidev="$( nand_find_ubi "$CI_UBIPART" )"
111 if [ ! "$ubidev" ]; then
112 ubiattach -m "$mtdnum"
113 sync
114 ubidev="$( nand_find_ubi "$CI_UBIPART" )"
115 fi
116
117 if [ ! "$ubidev" ]; then
118 ubiformat /dev/mtd$mtdnum -y
119 ubiattach -m "$mtdnum"
120 sync
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
125 }
126 fi
127
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 )"
131
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"
138 return 1;
139 fi
140 fi
141
142 # kill volumes
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
146
147 # update kernel
148 if [ "$has_kernel" = "1" ]; then
149 if ! ubimkvol /dev/$ubidev -N kernel -s $kernel_length; then
150 echo "cannot create kernel volume"
151 return 1;
152 fi
153 fi
154
155 # update rootfs
156 local root_size_param
157 if [ "$rootfs_type" = "ubifs" ]; then
158 root_size_param="-m"
159 else
160 root_size_param="-s $rootfs_length"
161 fi
162 if ! ubimkvol /dev/$ubidev -N rootfs $root_size_param; then
163 echo "cannot create rootfs volume"
164 return 1;
165 fi
166
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"
171 return 1
172 fi
173 fi
174 sync
175 return 0
176 }
177
178 nand_do_upgrade_success() {
179 local conf_tar="/tmp/sysupgrade.tgz"
180
181 sync
182 [ -f "$conf_tar" ] && nand_restore_config "$conf_tar"
183 echo "sysupgrade successfull"
184 reboot -f
185 }
186
187 nand_upgrade_ubinized() {
188 local ubi_file="$1"
189 local mtdnum="$(find_mtd_index "$CI_UBIPART")"
190
191 if [ ! "$mtdnum" ]; then
192 echo "cannot find mtd device $CI_UBIPART"
193 reboot -f
194 fi
195
196 local mtddev="/dev/mtd${mtdnum}"
197 ubidetach -p "${mtddev}" || true
198 sync
199 ubiformat "${mtddev}" -y -f "${ubi_file}"
200 ubiattach -p "${mtddev}"
201 nand_do_upgrade_success
202 }
203
204 nand_do_upgrade_stage2() {
205 [ "$(identify $1)" == "ubi" ] && nand_upgrade_ubinized $1
206
207 local tar_file="$1"
208 local board_name="$(cat /tmp/sysinfo/board_name)"
209 local kernel_mtd="$(find_mtd_index kernel)"
210
211 local kernel_length=`(tar xf $tar_file sysupgrade-$board_name/kernel -O | wc -c) 2> /dev/null`
212 local rootfs_length=`(tar xf $tar_file sysupgrade-$board_name/root -O | wc -c) 2> /dev/null`
213
214 local rootfs_type="$(identify_tar "$tar_file" root)"
215
216 local has_kernel=1
217 local has_env=0
218
219 [ "kernel_length" = 0 -o -z "$kernel_mtd" ] || {
220 tar xf $tar_file sysupgrade-$board_name/kernel -O | mtd write - kernel
221 }
222 [ "kernel_length" = 0 -o ! -z "$kernel_mtd" ] && has_kernel=0
223
224 nand_upgrade_prepare_ubi "$rootfs_length" "$rootfs_type" "$has_kernel" "$has_env"
225
226 local ubidev="$( nand_find_ubi "$CI_UBIPART" )"
227 [ "$has_kernel" = "1" ] && {
228 local kern_ubivol="$(nand_find_volume $ubidev kernel)"
229 tar xf $tar_file sysupgrade-$board_name/kernel -O | \
230 ubiupdatevol /dev/$kern_ubivol -s $kern_length -
231 }
232
233 local root_ubivol="$(nand_find_volume $ubidev rootfs)"
234 tar xf $tar_file sysupgrade-$board_name/root -O | \
235 ubiupdatevol /dev/$root_ubivol -s $rootfs_length -
236
237 nand_do_upgrade_success
238 }
239
240 nand_upgrade_stage2() {
241 [ $1 = "nand" ] && {
242 [ -f "$2" ] && {
243 touch /tmp/sysupgrade
244
245 killall -9 telnetd
246 killall -9 dropbear
247 killall -9 ash
248
249 kill_remaining TERM
250 sleep 3
251 kill_remaining KILL
252
253 sleep 1
254
255 if [ -n "$(rootfs_type)" ]; then
256 v "Switching to ramdisk..."
257 run_ramfs ". /lib/functions.sh; include /lib/upgrade; nand_do_upgrade_stage2 $2"
258 else
259 nand_do_upgrade_stage2 $2
260 fi
261 return 0
262 }
263 echo "Nand upgrade failed"
264 exit 1
265 }
266 }
267
268 nand_upgrade_stage1() {
269 [ -f /tmp/sysupgrade-nand-path ] && {
270 path="$(cat /tmp/sysupgrade-nand-path)"
271 [ "$SAVE_CONFIG" != 1 -a -f "$CONF_TAR" ] &&
272 rm $CONF_TAR
273
274 ubus call system nandupgrade "{\"path\": \"$path\" }"
275 exit 0
276 }
277 }
278 append sysupgrade_pre_upgrade nand_upgrade_stage1
279
280 nand_do_platform_check() {
281 local board_name="$1"
282 local tar_file="$2"
283 local control_length=`(tar xf $tar_file sysupgrade-$board_name/CONTROL -O | wc -c) 2> /dev/null`
284
285 [ "$control_length" = 0 -a "$(identify $2)" != "ubi" ] && {
286 echo "Invalid sysupgrade file."
287 return 1
288 }
289
290 echo -n $2 > /tmp/sysupgrade-nand-path
291 cp /sbin/upgraded /tmp/
292
293 return 0
294 }