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)
38 uint32_t kernel_chksum
;
39 uint32_t rootfs_chksum
;
42 uint32_t image_chksum
;
43 uint32_t header_chksum
;
44 /* char board_id[] - upto MAX_BOARD_ID_LEN */
47 static void __attribute__ ((format (printf
, 2, 3)))
48 fatal_error (int maybe_errno
, const char * format
, ...)
52 fprintf (stderr
, "mkchkimg: ");
53 va_start (ap
, format
);
54 vfprintf (stderr
, format
, ap
);
58 fprintf (stderr
, ": %s\n", strerror (maybe_errno
));
60 fprintf (stderr
, "\n");
66 static void __attribute__ ((format (printf
, 1, 2)))
67 message (const char * format
, ...)
71 fprintf (stderr
, "mkchkimg: ");
72 va_start (ap
, format
);
73 vfprintf (stderr
, format
, ap
);
75 fprintf (stderr
, "\n");
84 netgear_checksum_init (struct ngr_checksum
* c
)
90 netgear_checksum_add (struct ngr_checksum
* c
, unsigned char * buf
, size_t len
)
94 for (i
=0; i
<len
; i
++) {
95 c
->c0
+= buf
[i
] & 0xff;
100 static inline unsigned long
101 netgear_checksum_fini (struct ngr_checksum
* c
)
103 uint32_t b
, checksum
;
105 b
= (c
->c0
& 65535) + ((c
->c0
>> 16) & 65535);
106 c
->c0
= ((b
>> 16) + b
) & 65535;
107 b
= (c
->c1
& 65535) + ((c
->c1
>> 16) & 65535);
108 c
->c1
= ((b
>> 16) + b
) & 65535;
109 checksum
= ((c
->c1
<< 16) | c
->c0
);
116 fprintf (stderr
, "Usage: mkchkimg -o output -k kernel [-f filesys] [-b board_id] [-r region]\n");
120 main (int argc
, char * argv
[])
126 struct chk_header
* hdr
;
127 struct ngr_checksum chk_part
, chk_whole
;
129 char * output_file
, * kern_file
, * fs_file
;
130 FILE * out_fp
, * kern_fp
, * fs_fp
;
132 unsigned long region
;
135 board_id
= "U12H072T00_NETGEAR";
136 region
= 1; /* 1=WW, 2=NA */
142 while ((opt
= getopt (argc
, argv
, ":b:r:k:f:o:h")) != -1) {
146 if (strlen (optarg
) > MAX_BOARD_ID_LEN
) {
147 fatal_error (0, "Board lenght exceeds %d",
156 region
= strtoul (optarg
, &ptr
, 0);
157 if (errno
|| ptr
==optarg
|| *ptr
!='\0') {
158 fatal_error (0, "Cannot parse region %s", optarg
);
161 fatal_error (0, "Region cannot exceed 0xff");
177 output_file
= optarg
;
186 fatal_error (0, "Option -%c missing argument", optopt
);
191 fatal_error (0, "Unknown argument -%c", optopt
);
199 /* Check we have all the options expected */
202 fatal_error (0, "Kernel file expected");
206 fatal_error (0, "Output file required");
208 message ("Netgear CHK writer - v0.1");
210 /* Open the input file */
211 kern_fp
= fopen (kern_file
, "r");
213 fatal_error (errno
, "Cannot open %s", kern_file
);
216 /* Open the fs file, if specified */
218 fs_fp
= fopen (fs_file
, "r");
220 fatal_error (errno
, "Cannot open %s", fs_file
);
224 /* Open the output file */
225 out_fp
= fopen (output_file
, "w+");
227 fatal_error (errno
, "Cannot open %s", output_file
);
230 /* Write zeros when the chk header will be */
232 header_len
= sizeof (struct chk_header
) + strlen (board_id
);
233 if (fwrite (buf
, 1, header_len
, out_fp
) != header_len
) {
234 fatal_error (errno
, "Cannot write header");
237 /* Allocate storage for header, we fill in as we go */
238 hdr
= malloc (sizeof (struct chk_header
));
240 fatal_error (0, "malloc failed");
242 bzero (hdr
, sizeof (struct chk_header
));
244 /* Fill in known values */
245 hdr
->magic
= htonl (0x2a23245e);
246 hdr
->header_len
= htonl(header_len
);
247 hdr
->reserved
[0] = (unsigned char)(region
& 0xff);
248 hdr
->reserved
[1] = 1; /* Major */
249 hdr
->reserved
[2] = 1; /* Minor */
250 hdr
->reserved
[3] = 99; /* Build */
251 hdr
->reserved
[4] = 0; /* Unknown t1 ? was 1 */
252 hdr
->reserved
[5] = 0; /* Unknonw t2 ? was 0 */
253 hdr
->reserved
[6] = 0; /* Unknonw t3 ? was 1 */
254 hdr
->reserved
[7] = 0; /* Unused ? */
255 message (" Board Id: %s", board_id
);
256 message (" Region: %s", region
== 1 ? "World Wide (WW)"
257 : (region
== 2 ? "North America (NA)" : "Unknown"));
259 /* Copy the trx file, calculating the checksum as we go */
260 netgear_checksum_init (&chk_part
);
261 netgear_checksum_init (&chk_whole
);
262 while (!feof (kern_fp
)) {
263 len
= fread (buf
, 1, BUF_LEN
, kern_fp
);
267 if (fwrite (buf
, len
, 1, out_fp
) != 1) {
268 fatal_error (errno
, "Write error");
270 hdr
->kernel_len
+= len
;
271 netgear_checksum_add (&chk_part
, (unsigned char *)buf
, len
);
272 netgear_checksum_add (&chk_whole
, (unsigned char *)buf
, len
);
274 hdr
->kernel_chksum
= netgear_checksum_fini (&chk_part
);
275 message (" Kernel Len: %u", hdr
->kernel_len
);
276 message ("Kernel Checksum: 0x%08x", hdr
->kernel_chksum
);
277 hdr
->kernel_len
= htonl (hdr
->kernel_len
);
278 hdr
->kernel_chksum
= htonl (hdr
->kernel_chksum
);
280 /* Now copy the root fs, calculating the checksum as we go */
282 netgear_checksum_init (&chk_part
);
283 while (!feof (fs_fp
)) {
284 len
= fread (buf
, 1, BUF_LEN
, fs_fp
);
288 if (fwrite (buf
, len
, 1, out_fp
) != 1) {
289 fatal_error (errno
, "Write error");
291 hdr
->rootfs_len
+= len
;
292 netgear_checksum_add (&chk_part
, (unsigned char *)buf
, len
);
293 netgear_checksum_add (&chk_whole
, (unsigned char *)buf
, len
);
295 hdr
->rootfs_chksum
= (netgear_checksum_fini (&chk_part
));
296 message (" Rootfs Len: %u", hdr
->rootfs_len
);
297 message ("Rootfs Checksum: 0x%08x", hdr
->rootfs_chksum
);
298 hdr
->rootfs_len
= htonl (hdr
->rootfs_len
);
299 hdr
->rootfs_chksum
= htonl (hdr
->rootfs_chksum
);
302 /* Calcautate the image checksum */
303 hdr
->image_chksum
= netgear_checksum_fini (&chk_whole
);
304 message (" Image Checksum: 0x%08x", hdr
->image_chksum
);
305 hdr
->image_chksum
= htonl (hdr
->image_chksum
);
307 /* Calculate the header checksum */
308 netgear_checksum_init (&chk_part
);
309 netgear_checksum_add (&chk_part
, (unsigned char *)hdr
,
310 sizeof (struct chk_header
));
311 netgear_checksum_add (&chk_part
, (unsigned char *)board_id
,
313 hdr
->header_chksum
= htonl (netgear_checksum_fini (&chk_part
));
315 /* Finally rewind the output and write headers */
317 if (fwrite (hdr
, sizeof (struct chk_header
), 1, out_fp
) != 1) {
318 fatal_error (errno
, "Cannot write header");
320 if (fwrite (board_id
, strlen (board_id
), 1, out_fp
) != 1) {
321 fatal_error (errno
, "Cannot write board id");
This page took 0.064716 seconds and 5 git commands to generate.