0fe01f07079f7af00a560dc8e2b68fdc41222dd0
4 * This utility creates Netgear .chk files.
6 * Copyright (C) 2008 Dave C. Reeve <Dave.Reeve@dreeve.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include <arpa/inet.h>
30 #define BUF_LEN (2048)
32 #define MAX_BOARD_ID_LEN (64)
35 * Note on the reserved field of the chk_header:
36 * OFW naming scheme is typically: DEVICENAME-VA.B.C.D_E.F.G.chk, with A-G
37 * between 0 and 255. For instance: EX3700_EX3800-V1.0.0.58_1.0.38.chk
38 * The reserved field works like this:
39 * reserved[0]: region code. 1 for WW (WorldWide) and 2 for NA (North America)
52 uint32_t kernel_chksum
;
53 uint32_t rootfs_chksum
;
56 uint32_t image_chksum
;
57 uint32_t header_chksum
;
58 /* char board_id[] - upto MAX_BOARD_ID_LEN */
61 static void __attribute__ ((format (printf
, 2, 3)))
62 fatal_error (int maybe_errno
, const char * format
, ...)
66 fprintf (stderr
, "mkchkimg: ");
67 va_start (ap
, format
);
68 vfprintf (stderr
, format
, ap
);
72 fprintf (stderr
, ": %s\n", strerror (maybe_errno
));
74 fprintf (stderr
, "\n");
80 static void __attribute__ ((format (printf
, 1, 2)))
81 message (const char * format
, ...)
85 fprintf (stderr
, "mkchkimg: ");
86 va_start (ap
, format
);
87 vfprintf (stderr
, format
, ap
);
89 fprintf (stderr
, "\n");
98 netgear_checksum_init (struct ngr_checksum
* c
)
104 netgear_checksum_add (struct ngr_checksum
* c
, unsigned char * buf
, size_t len
)
108 for (i
=0; i
<len
; i
++) {
109 c
->c0
+= buf
[i
] & 0xff;
114 static inline unsigned long
115 netgear_checksum_fini (struct ngr_checksum
* c
)
117 uint32_t b
, checksum
;
119 b
= (c
->c0
& 65535) + ((c
->c0
>> 16) & 65535);
120 c
->c0
= ((b
>> 16) + b
) & 65535;
121 b
= (c
->c1
& 65535) + ((c
->c1
>> 16) & 65535);
122 c
->c1
= ((b
>> 16) + b
) & 65535;
123 checksum
= ((c
->c1
<< 16) | c
->c0
);
130 fprintf (stderr
, "Usage: mkchkimg -o output -k kernel [-f filesys] [-b board_id] [-r region]\n");
134 main (int argc
, char * argv
[])
140 struct chk_header
* hdr
;
141 struct ngr_checksum chk_part
, chk_whole
;
143 char * output_file
, * kern_file
, * fs_file
;
144 FILE * out_fp
, * kern_fp
, * fs_fp
;
146 unsigned long region
;
149 board_id
= "U12H072T00_NETGEAR";
150 region
= 1; /* 1=WW, 2=NA */
156 while ((opt
= getopt (argc
, argv
, ":b:r:k:f:o:h")) != -1) {
160 if (strlen (optarg
) > MAX_BOARD_ID_LEN
) {
161 fatal_error (0, "Board lenght exceeds %d",
170 region
= strtoul (optarg
, &ptr
, 0);
171 if (errno
|| ptr
==optarg
|| *ptr
!='\0') {
172 fatal_error (0, "Cannot parse region %s", optarg
);
175 fatal_error (0, "Region cannot exceed 0xff");
191 output_file
= optarg
;
200 fatal_error (0, "Option -%c missing argument", optopt
);
205 fatal_error (0, "Unknown argument -%c", optopt
);
213 /* Check we have all the options expected */
216 fatal_error (0, "Kernel file expected");
220 fatal_error (0, "Output file required");
222 message ("Netgear CHK writer - v0.1");
224 /* Open the input file */
225 kern_fp
= fopen (kern_file
, "r");
227 fatal_error (errno
, "Cannot open %s", kern_file
);
230 /* Open the fs file, if specified */
232 fs_fp
= fopen (fs_file
, "r");
234 fatal_error (errno
, "Cannot open %s", fs_file
);
238 /* Open the output file */
239 out_fp
= fopen (output_file
, "w+");
241 fatal_error (errno
, "Cannot open %s", output_file
);
244 /* Write zeros when the chk header will be */
246 header_len
= sizeof (struct chk_header
) + strlen (board_id
);
247 if (fwrite (buf
, 1, header_len
, out_fp
) != header_len
) {
248 fatal_error (errno
, "Cannot write header");
251 /* Allocate storage for header, we fill in as we go */
252 hdr
= malloc (sizeof (struct chk_header
));
254 fatal_error (0, "malloc failed");
256 bzero (hdr
, sizeof (struct chk_header
));
258 /* Fill in known values */
259 hdr
->magic
= htonl (0x2a23245e);
260 hdr
->header_len
= htonl(header_len
);
261 hdr
->reserved
[0] = (unsigned char)(region
& 0xff);
262 hdr
->reserved
[1] = 1; /* Major */
263 hdr
->reserved
[2] = 1; /* Minor */
264 hdr
->reserved
[3] = 99; /* Build */
265 hdr
->reserved
[4] = 0;
266 hdr
->reserved
[5] = 0;
267 hdr
->reserved
[6] = 0;
268 hdr
->reserved
[7] = 0;
269 message (" Board Id: %s", board_id
);
270 message (" Region: %s", region
== 1 ? "World Wide (WW)"
271 : (region
== 2 ? "North America (NA)" : "Unknown"));
273 /* Copy the trx file, calculating the checksum as we go */
274 netgear_checksum_init (&chk_part
);
275 netgear_checksum_init (&chk_whole
);
276 while (!feof (kern_fp
)) {
277 len
= fread (buf
, 1, BUF_LEN
, kern_fp
);
281 if (fwrite (buf
, len
, 1, out_fp
) != 1) {
282 fatal_error (errno
, "Write error");
284 hdr
->kernel_len
+= len
;
285 netgear_checksum_add (&chk_part
, (unsigned char *)buf
, len
);
286 netgear_checksum_add (&chk_whole
, (unsigned char *)buf
, len
);
288 hdr
->kernel_chksum
= netgear_checksum_fini (&chk_part
);
289 message (" Kernel Len: %u", hdr
->kernel_len
);
290 message ("Kernel Checksum: 0x%08x", hdr
->kernel_chksum
);
291 hdr
->kernel_len
= htonl (hdr
->kernel_len
);
292 hdr
->kernel_chksum
= htonl (hdr
->kernel_chksum
);
294 /* Now copy the root fs, calculating the checksum as we go */
296 netgear_checksum_init (&chk_part
);
297 while (!feof (fs_fp
)) {
298 len
= fread (buf
, 1, BUF_LEN
, fs_fp
);
302 if (fwrite (buf
, len
, 1, out_fp
) != 1) {
303 fatal_error (errno
, "Write error");
305 hdr
->rootfs_len
+= len
;
306 netgear_checksum_add (&chk_part
, (unsigned char *)buf
, len
);
307 netgear_checksum_add (&chk_whole
, (unsigned char *)buf
, len
);
309 hdr
->rootfs_chksum
= (netgear_checksum_fini (&chk_part
));
310 message (" Rootfs Len: %u", hdr
->rootfs_len
);
311 message ("Rootfs Checksum: 0x%08x", hdr
->rootfs_chksum
);
312 hdr
->rootfs_len
= htonl (hdr
->rootfs_len
);
313 hdr
->rootfs_chksum
= htonl (hdr
->rootfs_chksum
);
316 /* Calcautate the image checksum */
317 hdr
->image_chksum
= netgear_checksum_fini (&chk_whole
);
318 message (" Image Checksum: 0x%08x", hdr
->image_chksum
);
319 hdr
->image_chksum
= htonl (hdr
->image_chksum
);
321 /* Calculate the header checksum */
322 netgear_checksum_init (&chk_part
);
323 netgear_checksum_add (&chk_part
, (unsigned char *)hdr
,
324 sizeof (struct chk_header
));
325 netgear_checksum_add (&chk_part
, (unsigned char *)board_id
,
327 hdr
->header_chksum
= htonl (netgear_checksum_fini (&chk_part
));
329 /* Finally rewind the output and write headers */
331 if (fwrite (hdr
, sizeof (struct chk_header
), 1, out_fp
) != 1) {
332 fatal_error (errno
, "Cannot write header");
334 if (fwrite (board_id
, strlen (board_id
), 1, out_fp
) != 1) {
335 fatal_error (errno
, "Cannot write board id");