aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/make-ras.sh
blob: ccddaa0016b0c926d4737abb5757e7212b0a1157 (plain)
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
#!/usr/bin/env bash
#
# --- ZyXEL header format ---
# Original Version by Benjamin Berg <benjamin@sipsolutions.net>
#
# The firmware image prefixed with a header (which is written into the MTD device).
# The header is one erase block (~64KiB) in size, but the checksum only convers the
# first 2KiB. Padding is 0xff. All integers are in big-endian.
#
# The checksum is always a 16-Bit System V checksum (sum -s) stored in a 32-Bit integer.
#
#   4 bytes:  checksum of the rootfs image
#   4 bytes:  length of the contained rootfs image file (big endian)
#  32 bytes:  Firmware Version string (NUL terminated, 0xff padded)
#   4 bytes:  checksum over the header partition (big endian - see below)
#  32 bytes:  Model (e.g. "NBG6617", NUL termiated, 0xff padded)
#   4 bytes:  checksum of the kernel partition
#   4 bytes:  length of the contained kernel image file (big endian)
#      rest: 0xff padding
#
# The checksums are calculated by adding up all bytes and if a 16bit
# overflow occurs, one is added and the sum is masked to 16 bit:
#   csum = csum + databyte; if (csum > 0xffff) { csum += 1; csum &= 0xffff };
# Should the file have an odd number of bytes then the byte len-0x800 is
# used additionally.
#
# The checksum for the header is calculated over the first 2048 bytes with
# the rootfs image checksum as the placeholder during calculation.
#
# The header is padded with 0xff to the erase block size of the device.
#
board=""
version=""
kernel=""
rootfs=""
outfile=""
err=""

while [ "$1" ]; do
	case "$1" in
	"--board")
		board="$2"
		shift
		shift
		continue
		;;
	"--version")
		version="$2"
		shift
		shift
		continue
		;;
	"--kernel")
		kernel="$2"
		shift
		shift
		continue
		;;
	"--rootfs")
		rootfs="$2"
		shift
		shift
		continue
		;;
	"--rootfssize")
		rootfssize="$2"
		shift
		shift
		continue
		;;
	*)
		if [ ! "$outfile" ]; then
			outfile=$1
			shift
			continue
		fi
		;;
	esac
done

if [ ! -n "$board" -o ! -n "$version" -o ! -r "$kernel" -o ! -r "$rootfs" -o ! "$rootfssize" -o ! "$outfile" ]; then
	echo "syntax: $0 [--board ras-boardname] [--version ras-version] [--kernel kernelimage] [--rootfs rootfs] out"
	exit 1
fi

rootfs_len=$(wc -c < "$rootfs")

if [ "$rootfs_len" -lt "$rootfssize" ]; then
	dd if=$rootfs of=$rootfs.new bs=$rootfssize conv=sync
	mv $rootfs.new $rootfs
fi

if [ ${#version} -ge 28 ]; then
	echo "version: '$version' is too long"
	exit 1
fi

tmpdir="$( mktemp -d 2> /dev/null )"
if [ -z "$tmpdir" ]; then
	# try OSX signature
	tmpdir="$( mktemp -t 'ubitmp' -d )"
fi

if [ -z "$tmpdir" ]; then
	exit 1
fi

to_be() {
	local val="$1"
	local size="$2"

	case "$size" in
	4)
		echo $(( "$val" >> 24 | ("$val" & 0xff0000) >> 8 | ("$val" & 0xff00) << 8 | ("$val" & 0xff) << 24 ))
		;;
	2)
		echo $(( "$val" >> 8 | ("$val" & 0xff) << 8))
		;;
	esac
}

checksum_file() {
	local file=$1

	# ZyXEL seems to use System V sum mode... Now this is classy, who would have thought?!
	echo $(sum -s ${file} | cut -f1 -d" ")
}

append_bin() {
	local val=$1
	local size=$2
	local file=$3

	while [ "$size" -ne 0 ]; do
		printf \\$(printf %o $(("$val" & 0xff)))  >> "$file"
		val=$(($val >> 8))
		let size-=1
	done
	return
}

tf=${tmpdir}/out
pad=$(printf '%0.1s' $(printf "\xff"){1..64})

rootfs_header_file="$tmpdir/rootfs_header"
rootfs_chksum=$(to_be $(checksum_file ${rootfs}) 4)
rootfs_len=$(to_be $(wc -c < "$rootfs") 4)

versionpadlen=$(( 32 - ( ${#version} + 1) ))

# 4 bytes:  checksum of the rootfs image
append_bin "$rootfs_chksum" 4 "$rootfs_header_file"
# 4 bytes:  length of the contained rootfs image file (big endian)
append_bin "$rootfs_len" 4 "$rootfs_header_file"
# 32 bytes:  Firmware Version string (NUL terminated, 0xff padded)
printf "%s\x00%.*s" "$version" "$versionpadlen" "$pad" >> "$rootfs_header_file"

kernel_header_file="$tmpdir/kernel_header"
kernel_chksum=$(to_be $(checksum_file ${kernel}) 4)
kernel_len=$(to_be $(wc -c < "$kernel") 4)

# 4 bytes:  checksum of the kernel image
append_bin "$kernel_chksum" 4 "$kernel_header_file"
# 4 bytes:  length of the contained kernel image file (big endian)
append_bin "$kernel_len" 4 "$kernel_header_file"

board_header_file="$tmpdir/board_header"
board_file="$tmpdir/board"
boardpadlen=$(( 64 - ( ${#board} + 1) ))
# 32 bytes:  Model (e.g. "NBG6617", NUL termiated, 0xff padded)
printf "%s\x00%.*s" "$board" "$boardpadlen" "$pad" > "$board_file"
cat "$kernel_header_file" >> "$board_file"
printf "%.12s" "$pad" >> "$board_file"
#      rest: 0xff padding
for i in {1..511}; do
	printf "%s%s" "$pad" "$pad" >> "$board_file"
done

tmp_board_file="$tmpdir/tmp_board_file"
cat "$rootfs_header_file" > "$tmp_board_file"

# The checksum for the header is calculated over the first 2048 bytes with
# the rootfs image checksum as the placeholder during calculation.
append_bin "$rootfs_chksum" 4 "$tmp_board_file"
cat "$board_file" >> "$tmp_board_file"

truncate -s 2048 $tmp_board_file
board_chksum=$(to_be $(checksum_file ${tmp_board_file}) 4)

# 4 bytes:  checksum over the header partition (big endian)
append_bin "$board_chksum" 4 "$board_header_file"
cat "$board_file" >> "$board_header_file"

cat "$rootfs_header_file" "$board_header_file" "$rootfs" "$kernel" > "$outfile"

rm -rf "$tmpdir"