2 * --- ZyXEL header format ---
3 * Original Version by Benjamin Berg <benjamin@sipsolutions.net>
4 * C implementation based on generation-script by Christian Lamparter <chunkeey@gmail.com>
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 * 64 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)
19 * rest: 0xff padding (To erase block size)
21 * The kernel partition checksum and length is not used for every device.
22 * If it's notused, pad those 8 bytes with 0xFF.
24 * The checksums are calculated by adding up all bytes and if a 16bit
25 * overflow occurs, one is added and the sum is masked to 16 bit:
26 * csum = csum + databyte; if (csum > 0xffff) { csum += 1; csum &= 0xffff };
27 * Should the file have an odd number of bytes then the byte len-0x800 is
30 * The checksum for the header is calculated over the first 2048 bytes with
31 * the rootfs image checksum as the placeholder during calculation.
33 * This program is free software; you can redistribute it and/or modify it
34 * under the terms of the GNU General Public License version 2 as published
35 * by the Free Software Foundation.
49 #include <arpa/inet.h>
51 #define VERSION_STRING_LEN 31
52 #define ROOTFS_HEADER_LEN 40
54 #define KERNEL_HEADER_LEN 8
56 #define BOARD_NAME_LEN 64
57 #define BOARD_HEADER_LEN 68
59 #define HEADER_PARTITION_CALC_LENGTH 2048
60 #define HEADER_PARTITION_LENGTH 0x10000
63 char *name; /* name of the file */
64 char *data; /* file content */
65 size_t size; /* length of the file */
68 static char *progname;
70 static char *board_name = 0;
71 static char *version_name = 0;
72 static unsigned int rootfs_size = 0;
74 static struct file_info kernel = { NULL, NULL, 0 };
75 static struct file_info rootfs = { NULL, NULL, 0 };
76 static struct file_info rootfs_out = { NULL, NULL, 0 };
77 static struct file_info out = { NULL, NULL, 0 };
79 #define ERR(fmt, ...) do { \
80 fprintf(stderr, "[%s] *** error: " fmt "\n", \
81 progname, ## __VA_ARGS__ ); \
84 void map_file(struct file_info *finfo)
86 struct stat file_stat = {0};
89 fd = open(finfo->name, O_RDONLY, (mode_t)0600);
91 ERR("Error while opening file %s.", finfo->name);
95 if (fstat(fd, &file_stat) == -1) {
96 ERR("Error getting file size for %s.", finfo->name);
100 finfo->size = file_stat.st_size;
101 finfo->data = mmap(0, finfo->size, PROT_READ, MAP_SHARED, fd, 0);
103 if (finfo->data == MAP_FAILED) {
104 ERR("Error mapping file %s.", finfo->name);
111 void unmap_file(struct file_info *finfo)
113 if(munmap(finfo->data, finfo->size) == -1) {
114 ERR("Error unmapping file %s.", finfo->name);
119 void write_file(struct file_info *finfo)
121 FILE *fout = fopen(finfo->name, "w");
123 fwrite(finfo->data, finfo->size, 1, fout);
126 ERR("Wanted to write, but something went wrong.");
133 void usage(int status)
135 FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
137 fprintf(stream, "Usage: %s [OPTIONS...]\n", progname);
141 " -k <kernel> path for kernel image\n"
142 " -r <rootfs> path for rootfs image\n"
143 " -s <rfssize> size of output rootfs\n"
144 " -v <version> version string\n"
145 " -b <boardname> name of board to generate image for\n"
146 " -o <out_name> name of output image\n"
147 " -h show this screen\n"
153 static int sysv_chksm(const unsigned char *data, int size)
157 unsigned int s = 0; /* The sum of all the input bytes, modulo (UINT_MAX + 1). */
160 for (int i = 0; i < size; i++) {
164 r = (s & 0xffff) + ((s & 0xffffffff) >> 16);
165 checksum = (r & 0xffff) + (r >> 16);
170 static int zyxel_chksm(const unsigned char *data, int size)
172 return htonl(sysv_chksm(data, size));
175 char *generate_rootfs_header(struct file_info filesystem, char *version)
177 size_t version_string_length;
178 unsigned int chksm, size;
182 rootfs_header = malloc(ROOTFS_HEADER_LEN);
183 if (!rootfs_header) {
184 ERR("Couldn't allocate memory for rootfs header!");
188 /* Prepare padding for firmware-version string here */
189 memset(rootfs_header, 0xff, ROOTFS_HEADER_LEN);
191 chksm = zyxel_chksm((const unsigned char *)filesystem.data, filesystem.size);
192 size = htonl(filesystem.size);
194 /* 4 bytes: checksum of the rootfs image */
195 memcpy(rootfs_header + ptr, &chksm, 4);
198 /* 4 bytes: length of the contained rootfs image file (big endian) */
199 memcpy(rootfs_header + ptr, &size, 4);
202 /* 32 bytes: Firmware Version string (NUL terminated, 0xff padded) */
203 version_string_length = strlen(version) <= VERSION_STRING_LEN ? strlen(version) : VERSION_STRING_LEN;
204 memcpy(rootfs_header + ptr, version, version_string_length);
205 ptr += version_string_length;
206 /* Add null-terminator */
207 rootfs_header[ptr] = 0x0;
209 return rootfs_header;
212 char *generate_kernel_header(struct file_info kernel)
214 unsigned int chksm, size;
218 kernel_header = malloc(KERNEL_HEADER_LEN);
219 if (!kernel_header) {
220 ERR("Couldn't allocate memory for kernel header!");
224 chksm = zyxel_chksm((const unsigned char *)kernel.data, kernel.size);
225 size = htonl(kernel.size);
227 /* 4 bytes: checksum of the kernel image */
228 memcpy(kernel_header + ptr, &chksm, 4);
231 /* 4 bytes: length of the contained kernel image file (big endian) */
232 memcpy(kernel_header + ptr, &size, 4);
234 return kernel_header;
237 unsigned int generate_board_header_checksum(char *kernel_hdr, char *rootfs_hdr, char *boardname)
244 * The checksum of the board header is calculated over the first 2048 bytes of
245 * the header partition with the rootfs checksum used as a placeholder for then
246 * board checksum we calculate in this step. The checksum gained from this step
247 * is then used for the final board header partition.
250 board_hdr_tmp = malloc(HEADER_PARTITION_CALC_LENGTH);
251 if (!board_hdr_tmp) {
252 ERR("Couldn't allocate memory for temporary board header!");
255 memset(board_hdr_tmp, 0xff, HEADER_PARTITION_CALC_LENGTH);
257 /* 40 bytes: RootFS header */
258 memcpy(board_hdr_tmp, rootfs_hdr, ROOTFS_HEADER_LEN);
259 ptr += ROOTFS_HEADER_LEN;
261 /* 4 bytes: RootFS checksum (BE) as placeholder for board-header checksum */
262 memcpy(board_hdr_tmp + ptr, rootfs_hdr, 4);
265 /* 32 bytes: Model (e.g. "NBG6617", NUL termiated, 0xff padded) */
266 memcpy(board_hdr_tmp + ptr, boardname, strlen(boardname));
267 ptr += strlen(boardname);
268 /* Add null-terminator */
269 board_hdr_tmp[ptr] = 0x0;
270 ptr = ROOTFS_HEADER_LEN + 4 + BOARD_NAME_LEN;
272 /* 8 bytes: Kernel header */
274 memcpy(board_hdr_tmp + ptr, kernel_hdr, 8);
276 /* Calculate the checksum over the first 2048 bytes */
277 sum = zyxel_chksm((const unsigned char *)board_hdr_tmp, HEADER_PARTITION_CALC_LENGTH);
282 char *generate_board_header(char *kernel_hdr, char *rootfs_hdr, char *boardname)
284 unsigned int board_checksum;
287 board_hdr = malloc(BOARD_HEADER_LEN);
289 ERR("Couldn't allocate memory for board header!");
292 memset(board_hdr, 0xff, BOARD_HEADER_LEN);
294 /* 4 bytes: checksum over the header partition (big endian) */
295 board_checksum = generate_board_header_checksum(kernel_hdr, rootfs_hdr, boardname);
296 memcpy(board_hdr, &board_checksum, 4);
298 /* 32 bytes: Model (e.g. "NBG6617", NUL termiated, 0xff padded) */
299 memcpy(board_hdr + 4, boardname, strlen(boardname));
300 board_hdr[4 + strlen(boardname)] = 0x0;
307 char *rootfs_header = NULL;
308 char *kernel_header = NULL;
309 char *board_header = NULL;
319 * Allocate memory and copy input rootfs for temporary output rootfs.
320 * This is important as we have to generate the rootfs checksum over the
321 * entire rootfs partition. As we might have to pad the partition to allow
322 * for flashing via ZyXEL's Web-GUI, we prepare the rootfs partition for the
323 * output image here (and also use it for calculating the rootfs checksum).
325 * The roofs padding has to be done with 0x00.
327 rootfs_out.data = calloc(rootfs_out.size, sizeof(char));
328 memcpy(rootfs_out.data, rootfs.data, rootfs.size);
330 /* Prepare headers */
331 rootfs_header = generate_rootfs_header(rootfs_out, version_name);
333 kernel_header = generate_kernel_header(kernel);
334 board_header = generate_board_header(kernel_header, rootfs_header, board_name);
336 /* Prepare output file */
337 out.size = HEADER_PARTITION_LENGTH + rootfs_out.size;
339 out.size += kernel.size;
340 out.data = malloc(out.size);
341 memset(out.data, 0xFF, out.size);
343 /* Build output image */
344 memcpy(out.data, rootfs_header, ROOTFS_HEADER_LEN);
345 memcpy(out.data + ROOTFS_HEADER_LEN, board_header, BOARD_HEADER_LEN);
347 memcpy(out.data + ROOTFS_HEADER_LEN + BOARD_HEADER_LEN, kernel_header, KERNEL_HEADER_LEN);
348 ptr = HEADER_PARTITION_LENGTH;
349 memcpy(out.data + ptr, rootfs_out.data, rootfs_out.size);
350 ptr += rootfs_out.size;
352 memcpy(out.data + ptr, kernel.data, kernel.size);
354 /* Write back output image */
357 /* Free allocated memory */
362 free(rootfs_out.data);
375 ERR("No rootfs filename supplied");
380 ERR("No output filename supplied");
385 ERR("No board-name supplied");
390 ERR("No version supplied");
394 if (rootfs_size <= 0) {
395 ERR("Invalid rootfs size supplied");
399 if (strlen(board_name) > 31) {
400 ERR("Board name is to long");
406 int main(int argc, char *argv[])
409 progname = basename(argv[0]);
413 c = getopt(argc, argv, "b:k:o:r:s:v:h");
425 kernel.name = optarg;
431 rootfs.name = optarg;
434 sscanf(optarg, "%u", &rootfs_size);
437 version_name = optarg;
445 ret = check_options();
449 /* As ZyXEL Web-GUI only accept images with a rootfs equal or larger than the first firmware shipped
450 * for the device, we need to pad rootfs partition to this size. To perform further calculations, we
451 * decide the size of this part here. In case the rootfs we want to integrate in our image is larger,
452 * take it's size, otherwise the supplied size.
454 * Be careful! We rely on assertion of correct size to be performed beforehand. It is unknown if images
455 * with a to large rootfs are accepted or not.
457 rootfs_out.size = rootfs_size < rootfs.size ? rootfs.size : rootfs_size;
458 return build_image();