f0a48ddeeca76b92f0cee66a94e40a42adf83925
[lede.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         echo "unknown"
63 }
64
65 platform_check_image() {
66         [ "$#" -gt 1 ] && return 1
67
68         local file_type=$(platform_identify "$1")
69         local magic
70         local error=0
71
72         case "$file_type" in
73                 "chk")
74                         local header_len=$((0x$(get_magic_long_at "$1" 4)))
75                         local board_id_len=$(($header_len - 40))
76                         local board_id=$(dd if="$1" skip=40 bs=1 count=$board_id_len 2>/dev/null | hexdump -v -e '1/1 "%c"')
77                         local dev_board_id=$(platform_expected_image)
78                         echo "Found CHK image with device board_id $board_id"
79
80                         [ -n "$dev_board_id" -a "chk $board_id" != "$dev_board_id" ] && {
81                                 echo "Firmware board_id doesn't match device board_id ($dev_board_id)"
82                                 error=1
83                         }
84
85                         if ! otrx check "$1" -o "$header_len"; then
86                                 echo "No valid TRX firmware in the CHK image"
87                                 error=1
88                         fi
89                 ;;
90                 "cybertan")
91                         local pattern=$(dd if="$1" bs=1 count=4 2>/dev/null | hexdump -v -e '1/1 "%c"')
92                         local dev_pattern=$(platform_expected_image)
93                         echo "Found CyberTAN image with device pattern: $pattern"
94
95                         [ -n "$dev_pattern" -a "cybertan $pattern" != "$dev_pattern" ] && {
96                                 echo "Firmware pattern doesn't match device pattern ($dev_pattern)"
97                                 error=1
98                         }
99
100                         if ! otrx check "$1" -o 32; then
101                                 echo "No valid TRX firmware in the CyberTAN image"
102                                 error=1
103                         fi
104                 ;;
105                 "seama")
106                         local img_signature=$(oseama info "$1" | grep "Meta entry:.*signature=" | sed "s/.*=//")
107                         local dev_signature=$(platform_expected_image)
108                         echo "Found Seama image with device signature: $img_signature"
109
110                         [ -n "$dev_signature" -a "seama $img_signature" != "$dev_signature" ] && {
111                                 echo "Firmware signature doesn't match device signature ($dev_signature)"
112                                 error=1
113                         }
114
115                         $(oseama info "$1" -e 0 | grep -q "Meta entry:.*type=firmware") || {
116                                 echo "Seama container doesn't have firmware entity"
117                                 error=1
118                         }
119                 ;;
120                 "trx")
121                         if ! otrx check "$1"; then
122                                 echo "Invalid (corrupted?) TRX firmware"
123                                 error=1
124                         fi
125                 ;;
126                 *)
127                         echo "Invalid image type. Please use only .trx files"
128                         error=1
129                 ;;
130         esac
131
132         return $error
133 }
134
135 # $(1): image for upgrade (with possible extra header)
136 # $(2): offset of trx in image
137 platform_pre_upgrade_trx() {
138         local dir="/tmp/sysupgrade-bcm53xx"
139         local trx="$1"
140         local offset="$2"
141
142         # Extract partitions from trx
143         rm -fR $dir
144         mkdir -p $dir
145         otrx extract "$trx" \
146                 ${offset:+-o $offset} \
147                 -1 $dir/kernel \
148                 -2 $dir/root
149         [ $? -ne 0 ] && {
150                 echo "Failed to extract TRX partitions."
151                 return
152         }
153
154         # Firmwares without UBI image should be flashed "normally"
155         local root_type=$(identify $dir/root)
156         [ "$root_type" != "ubi" ] && {
157                 echo "Provided firmware doesn't use UBI for rootfs."
158                 return
159         }
160
161         # Prepare TRX file with just a kernel that will replace current one
162         local linux_length=$(grep "\"linux\"" /proc/mtd | sed "s/mtd[0-9]*:[ \t]*\([^ \t]*\).*/\1/")
163         [ -z "$linux_length" ] && {
164                 echo "Unable to find \"linux\" partition size"
165                 exit 1
166         }
167         linux_length=$((0x$linux_length))
168         local kernel_length=$(wc -c $dir/kernel | cut -d ' ' -f 1)
169         [ $kernel_length -gt $linux_length ] && {
170                 echo "New kernel doesn't fit \"linux\" partition."
171                 return
172         }
173         rm -f /tmp/null.bin
174         rm -f /tmp/kernel.trx
175         touch /tmp/null.bin
176         otrx create /tmp/kernel.trx \
177                 -f $dir/kernel -b $(($linux_length + 28)) \
178                 -f /tmp/null.bin
179         [ $? -ne 0 ] && {
180                 echo "Failed to create simple TRX with new kernel."
181                 return
182         }
183
184         # Prepare UBI image (drop unwanted extra blocks)
185         local ubi_length=0
186         while [ "$(dd if=$dir/root skip=$ubi_length bs=1 count=4 2>/dev/null)" = "UBI#" ]; do
187                 ubi_length=$(($ubi_length + 131072))
188         done
189         dd if=$dir/root of=/tmp/root.ubi bs=131072 count=$((ubi_length / 131072)) 2>/dev/null
190         [ $? -ne 0 ] && {
191                 echo "Failed to prepare new UBI image."
192                 return
193         }
194
195         # Flash
196         mtd write /tmp/kernel.trx firmware
197         nand_do_upgrade /tmp/root.ubi
198 }
199
200 platform_pre_upgrade_seama() {
201         local dir="/tmp/sysupgrade-bcm53xx"
202         local seama="$1"
203         local tmp
204
205         # Extract Seama entity from Seama seal
206         rm -fR $dir
207         mkdir -p $dir
208         oseama extract "$seama" \
209                 -e 0 \
210                 -o $dir/seama.entity
211         [ $? -ne 0 ] && {
212                 echo "Failed to extract Seama entity."
213                 return
214         }
215         local entity_size=$(wc -c $dir/seama.entity | cut -d ' ' -f 1)
216
217         local ubi_offset=0
218         tmp=0
219         while [ 1 ]; do
220                 [ $tmp -ge $entity_size ] && break
221                 [ "$(dd if=$dir/seama.entity skip=$tmp bs=1 count=4 2>/dev/null)" = "UBI#" ] && {
222                         ubi_offset=$tmp
223                         break
224                 }
225                 tmp=$(($tmp + 131072))
226         done
227         [ $ubi_offset -eq 0 ] && {
228                 echo "Failed to find UBI in Seama entity."
229                 return
230         }
231
232         local ubi_length=0
233         while [ "$(dd if=$dir/seama.entity skip=$(($ubi_offset + $ubi_length)) bs=1 count=4 2>/dev/null)" = "UBI#" ]; do
234                 ubi_length=$(($ubi_length + 131072))
235         done
236
237         dd if=$dir/seama.entity of=$dir/kernel.seama bs=131072 count=$(($ubi_offset / 131072)) 2>/dev/null
238         dd if=$dir/seama.entity of=$dir/root.ubi bs=131072 skip=$(($ubi_offset / 131072)) count=$(($ubi_length / 131072)) 2>/dev/null
239
240         # Flash
241         local kernel_size=$(sed -n 's/mtd[0-9]*: \([0-9a-f]*\).*"\(kernel\|linux\)".*/\1/p' /proc/mtd)
242         mtd write $dir/kernel.seama firmware
243         mtd ${kernel_size:+-c 0x$kernel_size} fixseama firmware
244         nand_do_upgrade $dir/root.ubi
245 }
246
247 platform_pre_upgrade() {
248         export RAMFS_COPY_BIN="${RAMFS_COPY_BIN} /usr/bin/oseama /bin/sed"
249
250         local file_type=$(platform_identify "$1")
251
252         [ "$(platform_flash_type)" != "nand" ] && return
253
254         # Find trx offset
255         case "$file_type" in
256                 "chk")          platform_pre_upgrade_trx "$1" $((0x$(get_magic_long_at "$1" 4)));;
257                 "cybertan")     platform_pre_upgrade_trx "$1" 32;;
258                 "seama")        platform_pre_upgrade_seama "$1";;
259                 "trx")          platform_pre_upgrade_trx "$1";;
260         esac
261 }
262
263 platform_trx_from_chk_cmd() {
264         local header_len=$((0x$(get_magic_long_at "$1" 4)))
265
266         echo -n dd bs=$header_len skip=1
267 }
268
269 platform_trx_from_cybertan_cmd() {
270         echo -n dd bs=32 skip=1
271 }
272
273 platform_img_from_seama() {
274         local dir="/tmp/sysupgrade-bcm53xx"
275         local offset=$(oseama info "$1" -e 0 | grep "Entity offset:" | sed "s/.*:\s*//")
276         local size=$(oseama info "$1" -e 0 | grep "Entity size:" | sed "s/.*:\s*//")
277
278         # Busybox doesn't support required iflag-s
279         # echo -n dd iflag=skip_bytes,count_bytes skip=$offset count=$size
280
281         rm -fR $dir
282         mkdir -p $dir
283         dd if="$1" of=$dir/image-noheader.bin bs=$offset skip=1
284         dd if=$dir/image-noheader.bin of=$dir/image-entity.bin bs=$size count=1
285
286         echo -n $dir/image-entity.bin
287 }
288
289 platform_do_upgrade() {
290         local file_type=$(platform_identify "$1")
291         local trx="$1"
292         local cmd=
293
294         [ "$(platform_flash_type)" == "nand" ] && {
295                 echo "Writing whole image to NAND flash. All erase counters will be lost."
296         }
297
298         case "$file_type" in
299                 "chk")          cmd=$(platform_trx_from_chk_cmd "$trx");;
300                 "cybertan")     cmd=$(platform_trx_from_cybertan_cmd "$trx");;
301                 "seama")        trx=$(platform_img_from_seama "$trx");;
302         esac
303
304         default_do_upgrade "$trx" "$cmd"
305 }