3e4eae5a82fba979e4f50b51646cb93ce610b1f1
[oweals/openwrt.git] / target / linux / bcm53xx / base-files / lib / upgrade / platform.sh
1 PART_NAME=firmware
2
3 # $(1): file to read magic from
4 # $(2): offset in bytes
5 get_magic_long_at() {
6         dd if="$1" skip=$2 bs=1 count=4 2>/dev/null | hexdump -v -e '1/1 "%02x"'
7 }
8
9 platform_machine() {
10         cat /proc/device-tree/compatible | tr '\0' '\t' | cut -f 1
11 }
12
13 platform_flash_type() {
14         # On NAND devices "rootfs" is UBI volume, so won't be find in /proc/mtd
15         grep -q "\"rootfs\"" /proc/mtd && {
16                 echo "serial"
17                 return
18         }
19
20         echo "nand"
21 }
22
23 platform_expected_image() {
24         local machine=$(platform_machine)
25
26         case "$machine" in
27                 "dlink,dir-885l")       echo "seama wrgac42_dlink.2015_dir885l"; return;;
28                 "netgear,r6250v1")      echo "chk U12H245T00_NETGEAR"; return;;
29                 "netgear,r6300v2")      echo "chk U12H240T00_NETGEAR"; return;;
30                 "netgear,r7000")        echo "chk U12H270T00_NETGEAR"; return;;
31                 "netgear,r7900")        echo "chk U12H315T30_NETGEAR"; return;;
32                 "netgear,r8000")        echo "chk U12H315T00_NETGEAR"; return;;
33                 "netgear,r8500")        echo "chk U12H334T00_NETGEAR"; return;;
34         esac
35 }
36
37 platform_identify() {
38         local magic
39
40         magic=$(get_magic_long "$1")
41         case "$magic" in
42                 "48445230")
43                         echo "trx"
44                         return
45                         ;;
46                 "2a23245e")
47                         echo "chk"
48                         return
49                         ;;
50                 "5ea3a417")
51                         echo "seama"
52                         return
53                         ;;
54         esac
55
56         magic=$(get_magic_long_at "$1" 14)
57         [ "$magic" = "55324e44" ] && {
58                 echo "cybertan"
59                 return
60         }
61
62         if osafeloader info "$1" > /dev/null; then
63                 echo "safeloader"
64                 return
65         fi
66
67         echo "unknown"
68 }
69
70 platform_check_image() {
71         [ "$#" -gt 1 ] && return 1
72
73         local file_type=$(platform_identify "$1")
74         local magic
75         local error=0
76
77         case "$file_type" in
78                 "chk")
79                         local header_len=$((0x$(get_magic_long_at "$1" 4)))
80                         local board_id_len=$(($header_len - 40))
81                         local board_id=$(dd if="$1" skip=40 bs=1 count=$board_id_len 2>/dev/null | hexdump -v -e '1/1 "%c"')
82                         local dev_board_id=$(platform_expected_image)
83                         echo "Found CHK image with device board_id $board_id"
84
85                         [ -n "$dev_board_id" -a "chk $board_id" != "$dev_board_id" ] && {
86                                 echo "Firmware board_id doesn't match device board_id ($dev_board_id)"
87                                 error=1
88                         }
89
90                         if ! otrx check "$1" -o "$header_len"; then
91                                 echo "No valid TRX firmware in the CHK image"
92                                 error=1
93                         fi
94                 ;;
95                 "cybertan")
96                         local pattern=$(dd if="$1" bs=1 count=4 2>/dev/null | hexdump -v -e '1/1 "%c"')
97                         local dev_pattern=$(platform_expected_image)
98                         echo "Found CyberTAN image with device pattern: $pattern"
99
100                         [ -n "$dev_pattern" -a "cybertan $pattern" != "$dev_pattern" ] && {
101                                 echo "Firmware pattern doesn't match device pattern ($dev_pattern)"
102                                 error=1
103                         }
104
105                         if ! otrx check "$1" -o 32; then
106                                 echo "No valid TRX firmware in the CyberTAN image"
107                                 error=1
108                         fi
109                 ;;
110                 "safeloader")
111                 ;;
112                 "seama")
113                         local img_signature=$(oseama info "$1" | grep "Meta entry:.*signature=" | sed "s/.*=//")
114                         local dev_signature=$(platform_expected_image)
115                         echo "Found Seama image with device signature: $img_signature"
116
117                         [ -n "$dev_signature" -a "seama $img_signature" != "$dev_signature" ] && {
118                                 echo "Firmware signature doesn't match device signature ($dev_signature)"
119                                 error=1
120                         }
121
122                         $(oseama info "$1" -e 0 | grep -q "Meta entry:.*type=firmware") || {
123                                 echo "Seama container doesn't have firmware entity"
124                                 error=1
125                         }
126                 ;;
127                 "trx")
128                         local expected=$(platform_expected_image)
129
130                         [ "$expected" == "safeloader" ] && {
131                                 echo "This device expects SafeLoader format and may not work with TRX"
132                                 error=1
133                         }
134
135                         if ! otrx check "$1"; then
136                                 echo "Invalid (corrupted?) TRX firmware"
137                                 error=1
138                         fi
139                 ;;
140                 *)
141                         echo "Invalid image type. Please use only .trx files"
142                         error=1
143                 ;;
144         esac
145
146         return $error
147 }
148
149 # $(1): image for upgrade (with possible extra header)
150 # $(2): offset of trx in image
151 platform_pre_upgrade_trx() {
152         local dir="/tmp/sysupgrade-bcm53xx"
153         local trx="$1"
154         local offset="$2"
155
156         # Extract partitions from trx
157         rm -fR $dir
158         mkdir -p $dir
159         otrx extract "$trx" \
160                 ${offset:+-o $offset} \
161                 -1 $dir/kernel \
162                 -2 $dir/root
163         [ $? -ne 0 ] && {
164                 echo "Failed to extract TRX partitions."
165                 return
166         }
167
168         # Firmwares without UBI image should be flashed "normally"
169         local root_type=$(identify $dir/root)
170         [ "$root_type" != "ubi" ] && {
171                 echo "Provided firmware doesn't use UBI for rootfs."
172                 return
173         }
174
175         # Prepare TRX file with just a kernel that will replace current one
176         local linux_length=$(grep "\"linux\"" /proc/mtd | sed "s/mtd[0-9]*:[ \t]*\([^ \t]*\).*/\1/")
177         [ -z "$linux_length" ] && {
178                 echo "Unable to find \"linux\" partition size"
179                 exit 1
180         }
181         linux_length=$((0x$linux_length))
182         local kernel_length=$(wc -c $dir/kernel | cut -d ' ' -f 1)
183         [ $kernel_length -gt $linux_length ] && {
184                 echo "New kernel doesn't fit \"linux\" partition."
185                 return
186         }
187         rm -f /tmp/null.bin
188         rm -f /tmp/kernel.trx
189         touch /tmp/null.bin
190         otrx create /tmp/kernel.trx \
191                 -f $dir/kernel -b $(($linux_length + 28)) \
192                 -f /tmp/null.bin
193         [ $? -ne 0 ] && {
194                 echo "Failed to create simple TRX with new kernel."
195                 return
196         }
197
198         # Prepare UBI image (drop unwanted extra blocks)
199         local ubi_length=0
200         while [ "$(dd if=$dir/root skip=$ubi_length bs=1 count=4 2>/dev/null)" = "UBI#" ]; do
201                 ubi_length=$(($ubi_length + 131072))
202         done
203         dd if=$dir/root of=/tmp/root.ubi bs=131072 count=$((ubi_length / 131072)) 2>/dev/null
204         [ $? -ne 0 ] && {
205                 echo "Failed to prepare new UBI image."
206                 return
207         }
208
209         # Flash
210         mtd write /tmp/kernel.trx firmware
211         nand_do_upgrade /tmp/root.ubi
212 }
213
214 platform_pre_upgrade_seama() {
215         local dir="/tmp/sysupgrade-bcm53xx"
216         local seama="$1"
217         local tmp
218
219         # Extract Seama entity from Seama seal
220         rm -fR $dir
221         mkdir -p $dir
222         oseama extract "$seama" \
223                 -e 0 \
224                 -o $dir/seama.entity
225         [ $? -ne 0 ] && {
226                 echo "Failed to extract Seama entity."
227                 return
228         }
229         local entity_size=$(wc -c $dir/seama.entity | cut -d ' ' -f 1)
230
231         local ubi_offset=0
232         tmp=0
233         while [ 1 ]; do
234                 [ $tmp -ge $entity_size ] && break
235                 [ "$(dd if=$dir/seama.entity skip=$tmp bs=1 count=4 2>/dev/null)" = "UBI#" ] && {
236                         ubi_offset=$tmp
237                         break
238                 }
239                 tmp=$(($tmp + 131072))
240         done
241         [ $ubi_offset -eq 0 ] && {
242                 echo "Failed to find UBI in Seama entity."
243                 return
244         }
245
246         local ubi_length=0
247         while [ "$(dd if=$dir/seama.entity skip=$(($ubi_offset + $ubi_length)) bs=1 count=4 2>/dev/null)" = "UBI#" ]; do
248                 ubi_length=$(($ubi_length + 131072))
249         done
250
251         dd if=$dir/seama.entity of=$dir/kernel.seama bs=131072 count=$(($ubi_offset / 131072)) 2>/dev/null
252         dd if=$dir/seama.entity of=$dir/root.ubi bs=131072 skip=$(($ubi_offset / 131072)) count=$(($ubi_length / 131072)) 2>/dev/null
253
254         # Flash
255         local kernel_size=$(sed -n 's/mtd[0-9]*: \([0-9a-f]*\).*"\(kernel\|linux\)".*/\1/p' /proc/mtd)
256         mtd write $dir/kernel.seama firmware
257         mtd ${kernel_size:+-c 0x$kernel_size} fixseama firmware
258         nand_do_upgrade $dir/root.ubi
259 }
260
261 platform_pre_upgrade() {
262         export RAMFS_COPY_BIN="${RAMFS_COPY_BIN} /usr/bin/osafeloader /usr/bin/oseama /bin/sed"
263
264         local file_type=$(platform_identify "$1")
265
266         [ "$(platform_flash_type)" != "nand" ] && return
267
268         # Find trx offset
269         case "$file_type" in
270                 "chk")          platform_pre_upgrade_trx "$1" $((0x$(get_magic_long_at "$1" 4)));;
271                 "cybertan")     platform_pre_upgrade_trx "$1" 32;;
272                 "seama")        platform_pre_upgrade_seama "$1";;
273                 "trx")          platform_pre_upgrade_trx "$1";;
274         esac
275 }
276
277 platform_trx_from_chk_cmd() {
278         local header_len=$((0x$(get_magic_long_at "$1" 4)))
279
280         echo -n dd bs=$header_len skip=1
281 }
282
283 platform_trx_from_cybertan_cmd() {
284         echo -n dd bs=32 skip=1
285 }
286
287 platform_img_from_safeloader() {
288         local dir="/tmp/sysupgrade-bcm53xx"
289
290         # Extract partitions from SafeLoader
291         rm -fR $dir
292         mkdir -p $dir
293         osafeloader extract "$1" \
294                 -p "os-image" \
295                 -o $dir/os-image
296         osafeloader extract "$1" \
297                 -p "file-system" \
298                 -o $dir/file-system
299
300         mtd write $dir/file-system rootfs
301
302         echo -n $dir/os-image
303 }
304
305 platform_img_from_seama() {
306         local dir="/tmp/sysupgrade-bcm53xx"
307         local offset=$(oseama info "$1" -e 0 | grep "Entity offset:" | sed "s/.*:\s*//")
308         local size=$(oseama info "$1" -e 0 | grep "Entity size:" | sed "s/.*:\s*//")
309
310         # Busybox doesn't support required iflag-s
311         # echo -n dd iflag=skip_bytes,count_bytes skip=$offset count=$size
312
313         rm -fR $dir
314         mkdir -p $dir
315         dd if="$1" of=$dir/image-noheader.bin bs=$offset skip=1
316         dd if=$dir/image-noheader.bin of=$dir/image-entity.bin bs=$size count=1
317
318         echo -n $dir/image-entity.bin
319 }
320
321 platform_do_upgrade() {
322         local file_type=$(platform_identify "$1")
323         local trx="$1"
324         local cmd=
325
326         [ "$(platform_flash_type)" == "nand" ] && {
327                 echo "Writing whole image to NAND flash. All erase counters will be lost."
328         }
329
330         case "$file_type" in
331                 "chk")          cmd=$(platform_trx_from_chk_cmd "$trx");;
332                 "cybertan")     cmd=$(platform_trx_from_cybertan_cmd "$trx");;
333                 "safeloader")   trx=$(platform_img_from_safeloader "$trx");;
334                 "seama")        trx=$(platform_img_from_seama "$trx");;
335         esac
336
337         default_do_upgrade "$trx" "$cmd"
338 }