-
Notifications
You must be signed in to change notification settings - Fork 10
/
create.sh
executable file
·230 lines (194 loc) · 6.35 KB
/
create.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/bin/bash
_help() {
printf -- 'Tool for creating new WAS-110 local upgrade images\n\n'
printf -- 'Usage: %s [options]\n\n' "$0"
printf -- 'Options:\n'
printf -- '-i --image <filename>\t\t\tSpecify local upgrade (img or tar) file to create (required).\n'
printf -- '-w --basic\t\t\t\tBuild a basic local upgrade tar file.\n'
printf -- '-W --bfw\t\t\t\tBuild a bfw local upgrade image file.\n'
printf -- '-H --header <filename>\t\t\tSpecify filename of image header to base image off of (default: header.bin).\n'
printf -- '-b --bootcore <filename>\t\tSpecify filename of bootcore image to place in created (bfw) image (default: bootcore.bin).\n'
printf -- '-k --kernel <filename>\t\t\tSpecify filename of kernel image to place in created image (default: kernel.bin).\n'
printf -- '-r --rootfs <filename>\t\t\tSpecify filename of rootfs image to place in created image (default: rootfs.img).\n'
printf -- '-F --version-file <filename>\t\tSpecify 8311 version file of basic firmeware image.\n'
printf -- '-V --image-version <version>\t\tSpecify version string to set on created image (15 characters max).\n'
printf -- '-L --image-long-version <version>\tSpecify detailed version string to set on created bfw image (31 characters max).\n'
printf -- '-D --date <date>\t\tSpecify date to use on all files. Helps with reproducability.\n'
printf -- '-h --help\t\t\t\tThis help text\n'
}
OUT=
HEADER="header.bin"
BOOTCORE="bootcore.bin"
KERNEL="kernel.bin"
ROOTFS="rootfs.img"
UPGRADE_SCRIPT="files/common/usr/sbin/8311-firmware-upgrade.sh"
VERSION=
LONGVERSION=
VARIANT="bfw"
VERSION_FILE=
DATE="@$(date '+%s')"
while [ $# -gt 0 ]; do
case "$1" in
-i|--image)
OUT="$2"
shift
;;
-w|--basic)
VARIANT="basic"
;;
-W|--bfw)
VARIANT="bfw"
;;
-H|--header)
HEADER="$2"
shift
;;
-b|--bootcore)
BOOTCORE="$2"
shift
;;
-k|--kernel)
KERNEL="$2"
shift
;;
-r|--rootfs)
ROOTFS="$2"
shift
;;
-F|--version-file)
VERSION_FILE="$2"
shift
;;
-V|--image-version)
VERSION="$2"
shift
;;
-L|--image-long-version)
LONGVERSION="$2"
shift
;;
-D|--date)
DATE="$2"
shift
;;
--help|-h)
_help
exit 0
;;
*)
_help
exit 1
;;
esac
shift
done
_err() {
echo "$1" >&2
exit ${2:-1}
}
sha256() {
sha256sum "$@" | awk '{print $1}'
}
file_size() {
stat -c '%s' "$1"
}
sed_escape() {
sed 's#\\#\\\\#g' | sed 's/#/\\#/g'
}
tar_trans() {
local INPUT="$(echo "$1" | sed_escape)"
local NAME="$(echo "$2" | sed_escape)"
echo "s#$INPUT#$NAME#"
}
bfw_add_image() {
IMAGE="$1"
FILE="${2:-$1}"
NAME_OFFSET=$((FILE_OFFSET + NUM * 48))
SIZE_OFFSET=$((NAME_OFFSET + 32))
if [ ! -f "$FILE" ]; then
echo "File '$FILE' for image '$IMAGE' does not exist" >&2
exit 1
fi
FILES+=("$FILE")
SIZE=$(stat -c "%s" "$FILE")
echo "Adding image #$NUM ($IMAGE) from '$FILE' ($SIZE bytes)"
{ echo -n "$IMAGE"; cat /dev/zero; } | dd of="$OUT" seek="$NAME_OFFSET" bs=1 count=32 conv=notrunc 2>/dev/null
{ echo -n "$SIZE"; cat /dev/zero; } | dd of="$OUT" seek="$SIZE_OFFSET" bs=1 count=16 conv=notrunc 2>/dev/null
cat "$FILE" >> "$OUT"
DATA_OFFSET=$((DATA_OFFSET + SIZE))
NUM=$((NUM + 1))
}
set -e
[ -n "$OUT" ] || _err "Error: Image file to create must be specified."
[ -n "$BOOTCORE" ] || _err "Error: bootcore file must be specified."
[ -f "$BOOTCORE" ] || _err "Error: bootcore file '$BOOTCORE' not found."
[ -n "$KERNEL" ] || _err "Error: kernel file must be specified."
[ -f "$KERNEL" ] || _err "Error: kernel file '$KERNEL' not found."
[ -n "$ROOTFS" ] || _err "Error: rootfs file must be specified."
[ -f "$ROOTFS" ] || _err "Error: rootfs file '$ROOTFS' not found."
touch -d "$DATE" "$BOOTCORE" "$KERNEL" "$ROOTFS"
if [ "$VARIANT" = "basic" ]; then
[ -n "$VERSION_FILE" ] || _err "Error: version file must be specified."
[ -f "$VERSION_FILE" ] || _err "Error: version file '$VERSION_FILE' not found."
SHA256_KERNEL=$(sha256 "$KERNEL")
SHA256_BOOTCORE=$(sha256 "$BOOTCORE")
SHA256_ROOTFS=$(sha256 "$ROOTFS")
SIZE_KERNEL=$(file_size "$KERNEL")
SIZE_BOOTCORE=$(file_size "$BOOTCORE")
SIZE_ROOTFS=$(file_size "$ROOTFS")
VER_8311=$(cat "$VERSION_FILE")
CONTROL="$(mktemp)"
cat > "$CONTROL" <<CONTROL
$VER_8311
SIZE_KERNEL=$SIZE_KERNEL
SIZE_BOOTCORE=$SIZE_BOOTCORE
SIZE_ROOTFS=$SIZE_ROOTFS
SHA256_KERNEL=$SHA256_KERNEL
SHA256_BOOTCORE=$SHA256_BOOTCORE
SHA256_ROOTFS=$SHA256_ROOTFS
CONTROL
touch -d "$DATE" "$UPGRADE_SCRIPT" "$CONTROL"
echo "Creating local upgrade tar file"
TAR=("-c" "-P" "-h" "--sparse" "-f" "$OUT")
TAR+=("--transform" "$(tar_trans "$UPGRADE_SCRIPT" "upgrade.sh")")
TAR+=("--transform" "$(tar_trans "$CONTROL" "control")")
TAR+=("--transform" "$(tar_trans "$KERNEL" "kernel.bin")")
TAR+=("--transform" "$(tar_trans "$BOOTCORE" "bootcore.bin")")
TAR+=("--transform" "$(tar_trans "$ROOTFS" "rootfs.img")")
TAR+=("--" "$UPGRADE_SCRIPT" "$CONTROL" "$KERNEL" "$BOOTCORE" "$ROOTFS")
tar "${TAR[@]}" || { rm -f "$CONTROL"; exit 1; }
rm -f "$CONTROL"
touch -d "$DATE" "$OUT"
echo "Local upgrade tar file '$OUT' created successfully."
else
[ -n "$HEADER" ] || _err "Error: header file must be specified."
[ -f "$HEADER" ] || _err "Error: header file '$HEADER' not found."
NUM=0
FILE_OFFSET=$((0x100))
LEN_HDR=$((0xD00))
DATA_OFFSET=$LEN_HDR
HW_OFFSET=$((0x10))
VERSION_OFFSET=$((0x30))
LONGVERSION_OFFSET=$((0x46))
FILES=()
echo "Creating local upgrade image file from header file '$HEADER'"
head -c "$LEN_HDR" "$HEADER" > "$OUT"
if [ -n "$VERSION" ]; then
echo "Setting image version string to '$VERSION'"
{ echo -n "$VERSION" | head -c 15; cat /dev/zero; } | dd of="$OUT" seek="$VERSION_OFFSET" bs=1 count=16 conv=notrunc 2>/dev/null
fi
if [ -n "$LONGVERSION" ]; then
echo "Setting long image version string to '$LONGVERSION'"
{ echo -n "$LONGVERSION" | head -c 31; cat /dev/zero; } | dd of="$OUT" seek="$LONGVERSION_OFFSET" bs=1 count=32 conv=notrunc 2>/dev/null
fi
bfw_add_image "bootcore.bin" "$BOOTCORE"
bfw_add_image "kernel.bin" "$KERNEL"
bfw_add_image "rootfs.img" "$ROOTFS"
CONTENT_CRC_OFFSET=$((0x66))
HEADER_CRC_OFFSET=$((0x6A))
echo "Updating CRCs"
{ cat "${FILES[@]}" | tools/bfw-crc.pl; cat /dev/zero; } | dd of="$OUT" seek="$CONTENT_CRC_OFFSET" bs=1 count=8 conv=notrunc 2>/dev/null
head -c "$LEN_HDR" "$OUT" | tools/bfw-crc.pl | dd of="$OUT" seek="$HEADER_CRC_OFFSET" bs=1 count=4 conv=notrunc 2>/dev/null
touch -d "$DATE" "$OUT"
echo "Local upgrade image file '$OUT' created successfully."
fi