3 # --- ZyXEL header format ---
4 # Original Version by Benjamin Berg <benjamin@sipsolutions.net>
6 # The firmware image prefixed with a header (which is written into the MTD device).
7 # The header is one erase block (~64KiB) in size, but the checksum only convers the
8 # first 2KiB. Padding is 0xff. All integers are in big-endian.
10 # The checksum is always a 16-Bit System V checksum (sum -s) stored in a 32-Bit integer.
12 # 4 bytes: checksum of the rootfs image
13 # 4 bytes: length of the contained rootfs image file (big endian)
14 # 32 bytes: Firmware Version string (NUL terminated, 0xff padded)
15 # 4 bytes: checksum over the header partition (big endian - see below)
16 # 32 bytes: Model (e.g. "NBG6617", NUL termiated, 0xff padded)
17 # 4 bytes: checksum of the kernel partition
18 # 4 bytes: length of the contained kernel image file (big endian)
21 # The checksums are calculated by adding up all bytes and if a 16bit
22 # overflow occurs, one is added and the sum is masked to 16 bit:
23 # csum = csum + databyte; if (csum > 0xffff) { csum += 1; csum &= 0xffff };
24 # Should the file have an odd number of bytes then the byte len-0x800 is
27 # The checksum for the header is calculated over the first 2048 bytes with
28 # the rootfs image checksum as the placeholder during calculation.
30 # The header is padded with 0xff to the erase block size of the device.
72 if [ ! "$outfile" ]; then
81 if [ ! -n "$board" -o ! -n "$version" -o ! -r "$kernel" -o ! -r "$rootfs" -o ! "$rootfssize" -o ! "$outfile" ]; then
82 echo "syntax: $0 [--board ras-boardname] [--version ras-version] [--kernel kernelimage] [--rootfs rootfs] out"
86 rootfs_len=$(wc -c < "$rootfs")
88 if [ "$rootfs_len" -lt "$rootfssize" ]; then
89 dd if=$rootfs of=$rootfs.new bs=$rootfssize conv=sync
90 mv $rootfs.new $rootfs
93 if [ ${#version} -ge 28 ]; then
94 echo "version: '$version' is too long"
98 tmpdir="$( mktemp -d 2> /dev/null )"
99 if [ -z "$tmpdir" ]; then
101 tmpdir="$( mktemp -t 'ubitmp' -d )"
104 if [ -z "$tmpdir" ]; then
114 echo $(( "$val" >> 24 | ("$val" & 0xff0000) >> 8 | ("$val" & 0xff00) << 8 | ("$val" & 0xff) << 24 ))
117 echo $(( "$val" >> 8 | ("$val" & 0xff) << 8))
125 # ZyXEL seems to use System V sum mode... Now this is classy, who would have thought?!
126 echo $(sum -s ${file} | cut -f1 -d" ")
134 while [ "$size" -ne 0 ]; do
135 printf \\$(printf %o $(("$val" & 0xff))) >> "$file"
143 pad=$(printf '%0.1s' $(printf "\xff"){1..64})
145 rootfs_header_file="$tmpdir/rootfs_header"
146 rootfs_chksum=$(to_be $(checksum_file ${rootfs}) 4)
147 rootfs_len=$(to_be $(wc -c < "$rootfs") 4)
149 versionpadlen=$(( 32 - ( ${#version} + 1) ))
151 # 4 bytes: checksum of the rootfs image
152 append_bin "$rootfs_chksum" 4 "$rootfs_header_file"
153 # 4 bytes: length of the contained rootfs image file (big endian)
154 append_bin "$rootfs_len" 4 "$rootfs_header_file"
155 # 32 bytes: Firmware Version string (NUL terminated, 0xff padded)
156 printf "%s\x00%.*s" "$version" "$versionpadlen" "$pad" >> "$rootfs_header_file"
158 kernel_header_file="$tmpdir/kernel_header"
159 kernel_chksum=$(to_be $(checksum_file ${kernel}) 4)
160 kernel_len=$(to_be $(wc -c < "$kernel") 4)
162 # 4 bytes: checksum of the kernel image
163 append_bin "$kernel_chksum" 4 "$kernel_header_file"
164 # 4 bytes: length of the contained kernel image file (big endian)
165 append_bin "$kernel_len" 4 "$kernel_header_file"
167 board_header_file="$tmpdir/board_header"
168 board_file="$tmpdir/board"
169 boardpadlen=$(( 64 - ( ${#board} + 1) ))
170 # 32 bytes: Model (e.g. "NBG6617", NUL termiated, 0xff padded)
171 printf "%s\x00%.*s" "$board" "$boardpadlen" "$pad" > "$board_file"
172 cat "$kernel_header_file" >> "$board_file"
173 printf "%.12s" "$pad" >> "$board_file"
175 for i in {1..511}; do
176 printf "%s%s" "$pad" "$pad" >> "$board_file"
179 tmp_board_file="$tmpdir/tmp_board_file"
180 cat "$rootfs_header_file" > "$tmp_board_file"
182 # The checksum for the header is calculated over the first 2048 bytes with
183 # the rootfs image checksum as the placeholder during calculation.
184 append_bin "$rootfs_chksum" 4 "$tmp_board_file"
185 cat "$board_file" >> "$tmp_board_file"
187 truncate -s 2048 $tmp_board_file
188 board_chksum=$(to_be $(checksum_file ${tmp_board_file}) 4)
190 # 4 bytes: checksum over the header partition (big endian)
191 append_bin "$board_chksum" 4 "$board_header_file"
192 cat "$board_file" >> "$board_header_file"
194 cat "$rootfs_header_file" "$board_header_file" "$rootfs" "$kernel" > "$outfile"