scripts/dump-target-info.pl: add new function to DUMP devices
[openwrt/staging/stintel.git] / scripts / linksys-image.sh
1 #!/bin/sh
2 #
3 # Copyright (C) 2018 Oceanic Systems (UK) Ltd
4 #
5 # This is free software, licensed under the GNU General Public License v2.
6 # See /LICENSE for more information.
7 #
8 # Maintained by: Ryan Pannell <ryan [at] o s u k l .com> <github.com/Escalion>
9 #
10 # Write Linksys signature for factory image
11 # This is appended to the factory image and is tested by the Linksys Upgrader - as observed in civic.
12 # The footer is 256 bytes. The format is:
13 # .LINKSYS. This is detected by the Linksys upgrader before continuing with upgrade. (9 bytes)
14 # <VERSION> The version number of upgrade. Not checked so use arbitrary value (8 bytes)
15 # <TYPE> Model of target device, padded (0x20) to (15 bytes)
16 # <CRC> CRC checksum of the image to flash (8 byte)
17 # <padding> Padding ('0' + 0x20 *7) (8 bytes)
18 # <signature> Signature of signer. Not checked so use arbitrary value (16 bytes)
19 # <padding> Padding (0x00) (192 bytes)
20
21 ## version history
22 # * version 1: initial commit
23
24 set -e
25
26 ME="${0##*/}"
27
28 usage() {
29 echo "Usage: $ME <type> <in filename>"
30 [ "$IMG_OUT" ] && rm -f "$IMG_OUT"
31 exit 1
32 }
33
34 [ "$#" -lt 3 ] && usage
35
36 TYPE=$1
37
38 tmpdir="$( mktemp -d 2> /dev/null )"
39 if [ -z "$tmpdir" ]; then
40 # try OSX signature
41 tmpdir="$( mktemp -t 'ubitmp' -d )"
42 fi
43
44 if [ -z "$tmpdir" ]; then
45 exit 1
46 fi
47
48 trap "rm -rf $tmpdir" EXIT
49
50 IMG_TMP_OUT="${tmpdir}/out"
51
52 IMG_IN=$2
53 IMG_OUT="${IMG_IN}.new"
54
55 [ ! -f "$IMG_IN" ] && echo "$ME: Not a valid image: $IMG_IN" && usage
56
57 dd if="${IMG_IN}" of="${IMG_TMP_OUT}"
58 CRC=$(printf "%08X" $(dd if="${IMG_IN}" bs=$(stat -c%s "${IMG_IN}") count=1|cksum| cut -d ' ' -f1))
59
60 printf ".LINKSYS.01000409%-15s%-8s%-8s%-16s" "${TYPE}" "${CRC}" "0" "K0000000F0246434" >> "${IMG_TMP_OUT}"
61
62 dd if=/dev/zero bs=1 count=192 conv=notrunc >> "${IMG_TMP_OUT}"
63
64 cp "${IMG_TMP_OUT}" "${IMG_OUT}"