bcm53xx: specify magic expected in sysupgrade for D-Link DIR-885L
[openwrt/staging/chunkeey.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,r8000") echo "chk U12H315T00_NETGEAR"; return;;
32 esac
33 }
34
35 platform_identify() {
36 local magic
37
38 magic=$(get_magic_long "$1")
39 case "$magic" in
40 "48445230")
41 echo "trx"
42 return
43 ;;
44 "2a23245e")
45 echo "chk"
46 return
47 ;;
48 "5ea3a417")
49 echo "seama"
50 return
51 ;;
52 esac
53
54 magic=$(get_magic_long_at "$1" 14)
55 [ "$magic" = "55324e44" ] && {
56 echo "cybertan"
57 return
58 }
59
60 echo "unknown"
61 }
62
63 platform_check_image() {
64 [ "$#" -gt 1 ] && return 1
65
66 local file_type=$(platform_identify "$1")
67 local magic
68 local error=0
69
70 case "$file_type" in
71 "chk")
72 local header_len=$((0x$(get_magic_long_at "$1" 4)))
73 local board_id_len=$(($header_len - 40))
74 local board_id=$(dd if="$1" skip=40 bs=1 count=$board_id_len 2>/dev/null | hexdump -v -e '1/1 "%c"')
75 local dev_board_id=$(platform_expected_image)
76 echo "Found CHK image with device board_id $board_id"
77
78 [ -n "$dev_board_id" -a "chk $board_id" != "$dev_board_id" ] && {
79 echo "Firmware board_id doesn't match device board_id ($dev_board_id)"
80 error=1
81 }
82
83 if ! otrx check "$1" -o "$header_len"; then
84 echo "No valid TRX firmware in the CHK image"
85 error=1
86 fi
87 ;;
88 "cybertan")
89 local pattern=$(dd if="$1" bs=1 count=4 2>/dev/null | hexdump -v -e '1/1 "%c"')
90 local dev_pattern=$(platform_expected_image)
91 echo "Found CyberTAN image with device pattern: $pattern"
92
93 [ -n "$dev_pattern" -a "cybertan $pattern" != "$dev_pattern" ] && {
94 echo "Firmware pattern doesn't match device pattern ($dev_pattern)"
95 error=1
96 }
97
98 if ! otrx check "$1" -o 32; then
99 echo "No valid TRX firmware in the CyberTAN image"
100 error=1
101 fi
102 ;;
103 "seama")
104 local img_signature=$(oseama info "$1" | grep "Meta entry:.*signature=" | sed "s/.*=//")
105 local dev_signature=$(platform_expected_image)
106 echo "Found Seama image with device signature: $img_signature"
107
108 [ -n "$dev_signature" -a "seama $img_signature" != "$dev_signature" ] && {
109 echo "Firmware signature doesn't match device signature ($dev_signature)"
110 error=1
111 }
112
113 $(oseama info "$1" -e 0 | grep -q "Meta entry:.*type=firmware") || {
114 echo "Seama container doesn't have firmware entity"
115 error=1
116 }
117 ;;
118 "trx")
119 if ! otrx check "$1"; then
120 echo "Invalid (corrupted?) TRX firmware"
121 error=1
122 fi
123 ;;
124 *)
125 echo "Invalid image type. Please use only .trx files"
126 error=1
127 ;;
128 esac
129
130 return $error
131 }
132
133 platform_pre_upgrade() {
134 export RAMFS_COPY_BIN="${RAMFS_COPY_BIN} /usr/bin/oseama /bin/sed"
135
136 local file_type=$(platform_identify "$1")
137 local dir="/tmp/sysupgrade-bcm53xx"
138 local trx="$1"
139 local offset
140
141 [ "$(platform_flash_type)" != "nand" ] && return
142
143 # Find trx offset
144 case "$file_type" in
145 "chk") offset=$((0x$(get_magic_long_at "$1" 4)));;
146 "cybertan") offset=32;;
147 "seama") return;;
148 esac
149
150 # Extract partitions from trx
151 rm -fR $dir
152 mkdir -p $dir
153 otrx extract "$trx" \
154 ${offset:+-o $offset} \
155 -1 $dir/kernel \
156 -2 $dir/root
157 [ $? -ne 0 ] && {
158 echo "Failed to extract TRX partitions."
159 return
160 }
161
162 # Firmwares without UBI image should be flashed "normally"
163 local root_type=$(identify $dir/root)
164 [ "$root_type" != "ubi" ] && {
165 echo "Provided firmware doesn't use UBI for rootfs."
166 return
167 }
168
169 # Prepare TRX file with just a kernel that will replace current one
170 local linux_length=$(grep "\"linux\"" /proc/mtd | sed "s/mtd[0-9]*:[ \t]*\([^ \t]*\).*/\1/")
171 [ -z "$linux_length" ] && {
172 echo "Unable to find \"linux\" partition size"
173 exit 1
174 }
175 linux_length=$((0x$linux_length))
176 local kernel_length=$(wc -c $dir/kernel | cut -d ' ' -f 1)
177 [ $kernel_length -gt $linux_length ] && {
178 echo "New kernel doesn't fit \"linux\" partition."
179 return
180 }
181 rm -f /tmp/null.bin
182 rm -f /tmp/kernel.trx
183 touch /tmp/null.bin
184 otrx create /tmp/kernel.trx \
185 -f $dir/kernel -b $(($linux_length + 28)) \
186 -f /tmp/null.bin
187 [ $? -ne 0 ] && {
188 echo "Failed to create simple TRX with new kernel."
189 return
190 }
191
192 # Prepare UBI image (drop unwanted extra blocks)
193 local ubi_length=0
194 while [ "$(dd if=$dir/root skip=$ubi_length bs=1 count=4 2>/dev/null)" = "UBI#" ]; do
195 ubi_length=$(($ubi_length + 131072))
196 done
197 dd if=$dir/root of=/tmp/root.ubi bs=131072 count=$((ubi_length / 131072)) 2>/dev/null
198 [ $? -ne 0 ] && {
199 echo "Failed to prepare new UBI image."
200 return
201 }
202
203 # Flash
204 mtd write /tmp/kernel.trx firmware
205 nand_do_upgrade /tmp/root.ubi
206 }
207
208 platform_trx_from_chk_cmd() {
209 local header_len=$((0x$(get_magic_long_at "$1" 4)))
210
211 echo -n dd bs=$header_len skip=1
212 }
213
214 platform_trx_from_cybertan_cmd() {
215 echo -n dd bs=32 skip=1
216 }
217
218 platform_img_from_seama() {
219 local dir="/tmp/sysupgrade-bcm53xx"
220 local offset=$(oseama info "$1" -e 0 | grep "Entity offset:" | sed "s/.*:\s*//")
221 local size=$(oseama info "$1" -e 0 | grep "Entity size:" | sed "s/.*:\s*//")
222
223 # Busybox doesn't support required iflag-s
224 # echo -n dd iflag=skip_bytes,count_bytes skip=$offset count=$size
225
226 rm -fR $dir
227 mkdir -p $dir
228 dd if="$1" of=$dir/image-noheader.bin bs=$offset skip=1
229 dd if=$dir/image-noheader.bin of=$dir/image-entity.bin bs=$size count=1
230
231 echo -n $dir/image-entity.bin
232 }
233
234 platform_do_upgrade() {
235 local file_type=$(platform_identify "$1")
236 local trx="$1"
237 local cmd=
238
239 [ "$(platform_flash_type)" == "nand" ] && {
240 echo "Writing whole image to NAND flash. All erase counters will be lost."
241 }
242
243 case "$file_type" in
244 "chk") cmd=$(platform_trx_from_chk_cmd "$trx");;
245 "cybertan") cmd=$(platform_trx_from_cybertan_cmd "$trx");;
246 "seama") trx=$(platform_img_from_seama "$trx");;
247 esac
248
249 default_do_upgrade "$trx" "$cmd"
250 }