ath79: add support for TP-Link TL-WR941HP v1
[project/firmware-utils.git] / src / mkzcfw.c
1 /*
2 * Copyright (C) 2010 Gabor Juhos <juhosg@openwrt.org>
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 as published
6 * by the Free Software Foundation.
7 *
8 */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <stdint.h>
13 #include <string.h>
14 #include <byteswap.h>
15 #include <unistd.h> /* for unlink() */
16 #include <libgen.h>
17 #include <getopt.h> /* for getopt() */
18 #include <stdarg.h>
19 #include <errno.h>
20 #include <sys/stat.h>
21
22 #include "cyg_crc.h"
23
24 #if (__BYTE_ORDER == __BIG_ENDIAN)
25 # define HOST_TO_BE32(x) (x)
26 # define BE32_TO_HOST(x) (x)
27 # define HOST_TO_LE32(x) bswap_32(x)
28 # define LE32_TO_HOST(x) bswap_32(x)
29 #else
30 # define HOST_TO_BE32(x) bswap_32(x)
31 # define BE32_TO_HOST(x) bswap_32(x)
32 # define HOST_TO_LE32(x) (x)
33 # define LE32_TO_HOST(x) (x)
34 #endif
35
36 #define MAGIC_FIRMWARE 0x6d726966 /* 'firm' */
37 #define MAGIC_KERNEL 0x676d694b /* 'Kimg' */
38 #define MAGIC_ROOTFS 0x676d6952 /* 'Rimg' */
39
40 struct file_info {
41 char *file_name; /* name of the file */
42 uint32_t file_size; /* length of the file */
43 };
44
45 struct fw_header {
46 uint32_t magic;
47 uint32_t length;
48 uint32_t unk1;
49 uint32_t unk2;
50 } __attribute__ ((packed));
51
52 struct fw_tail {
53 uint32_t hw_id;
54 uint32_t crc;
55 } __attribute__ ((packed));
56
57 struct board_info {
58 char *id;
59 uint32_t hw_id;
60 uint32_t kernel_len;
61 uint32_t rootfs_len;
62 };
63
64 /*
65 * Globals
66 */
67 static char *ofname;
68 static char *progname;
69
70 static char *board_id;
71 static struct board_info *board;
72 static struct file_info kernel_info;
73 static struct file_info rootfs_info;
74
75
76 static struct board_info boards[] = {
77 {
78 .id = "ZCN-1523H-2-8",
79 .hw_id = 0x66661523,
80 .kernel_len = 0x170000,
81 .rootfs_len = 0x610000,
82 }, {
83 .id = "ZCN-1523H-5-16",
84 .hw_id = 0x6615235A,
85 .kernel_len = 0x170000,
86 .rootfs_len = 0x610000,
87 }, {
88 /* terminating entry */
89 }
90 };
91
92 /*
93 * Message macros
94 */
95 #define ERR(fmt, ...) do { \
96 fflush(0); \
97 fprintf(stderr, "[%s] *** error: " fmt "\n", \
98 progname, ## __VA_ARGS__ ); \
99 } while (0)
100
101 #define ERRS(fmt, ...) do { \
102 int save = errno; \
103 fflush(0); \
104 fprintf(stderr, "[%s] *** error: " fmt ": %s\n", \
105 progname, ## __VA_ARGS__, strerror(save)); \
106 } while (0)
107
108 #define DBG(fmt, ...) do { \
109 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
110 } while (0)
111
112 static struct board_info *find_board(char *id)
113 {
114 struct board_info *ret;
115 struct board_info *board;
116
117 ret = NULL;
118 for (board = boards; board->id != NULL; board++){
119 if (strcasecmp(id, board->id) == 0) {
120 ret = board;
121 break;
122 }
123 };
124
125 return ret;
126 }
127
128 static void usage(int status)
129 {
130 FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
131
132 fprintf(stream, "Usage: %s [OPTIONS...]\n", progname);
133 fprintf(stream,
134 "\n"
135 "Options:\n"
136 " -B <board> create image for the board specified with <board>\n"
137 " -k <file> read kernel image from the file <file>\n"
138 " -r <file> read rootfs image from the file <file>\n"
139 " -o <file> write output to the file <file>\n"
140 " -h show this screen\n"
141 );
142
143 exit(status);
144 }
145
146 static int get_file_stat(struct file_info *fdata)
147 {
148 struct stat st;
149 int res;
150
151 if (fdata->file_name == NULL)
152 return 0;
153
154 res = stat(fdata->file_name, &st);
155 if (res){
156 ERRS("stat failed on %s", fdata->file_name);
157 return res;
158 }
159
160 fdata->file_size = st.st_size;
161 return 0;
162 }
163
164 static int read_to_buf(struct file_info *fdata, char *buf)
165 {
166 FILE *f;
167 int ret = EXIT_FAILURE;
168
169 f = fopen(fdata->file_name, "r");
170 if (f == NULL) {
171 ERRS("could not open \"%s\" for reading", fdata->file_name);
172 goto out;
173 }
174
175 errno = 0;
176 fread(buf, fdata->file_size, 1, f);
177 if (errno != 0) {
178 ERRS("unable to read from file \"%s\"", fdata->file_name);
179 goto out_close;
180 }
181
182 ret = EXIT_SUCCESS;
183
184 out_close:
185 fclose(f);
186 out:
187 return ret;
188 }
189
190 static int check_options(void)
191 {
192 int ret;
193
194 if (board_id == NULL) {
195 ERR("no board specified");
196 return -1;
197 }
198
199 board = find_board(board_id);
200 if (board == NULL) {
201 ERR("unknown/unsupported board id \"%s\"", board_id);
202 return -1;
203 }
204
205 if (kernel_info.file_name == NULL) {
206 ERR("no kernel image specified");
207 return -1;
208 }
209
210 ret = get_file_stat(&kernel_info);
211 if (ret)
212 return ret;
213
214 if (kernel_info.file_size > board->kernel_len) {
215 ERR("kernel image is too big");
216 return -1;
217 }
218
219 if (rootfs_info.file_name == NULL) {
220 ERR("no rootfs image specified");
221 return -1;
222 }
223
224 ret = get_file_stat(&rootfs_info);
225 if (ret)
226 return ret;
227
228 if (rootfs_info.file_size > board->rootfs_len) {
229 ERR("rootfs image is too big");
230 return -1;
231 }
232
233 if (ofname == NULL) {
234 ERR("no output file specified");
235 return -1;
236 }
237
238 return 0;
239 }
240
241 static int write_fw(char *data, int len)
242 {
243 FILE *f;
244 int ret = EXIT_FAILURE;
245
246 f = fopen(ofname, "w");
247 if (f == NULL) {
248 ERRS("could not open \"%s\" for writing", ofname);
249 goto out;
250 }
251
252 errno = 0;
253 fwrite(data, len, 1, f);
254 if (errno) {
255 ERRS("unable to write output file");
256 goto out_flush;
257 }
258
259 DBG("firmware file \"%s\" completed", ofname);
260
261 ret = EXIT_SUCCESS;
262
263 out_flush:
264 fflush(f);
265 fclose(f);
266 if (ret != EXIT_SUCCESS) {
267 unlink(ofname);
268 }
269 out:
270 return ret;
271 }
272
273 static int build_fw(void)
274 {
275 int buflen;
276 char *buf;
277 char *p;
278 int ret = EXIT_FAILURE;
279 struct fw_header *hdr;
280 struct fw_tail *tail;
281
282 buflen = 3 * sizeof(struct fw_header) +
283 kernel_info.file_size + rootfs_info.file_size +
284 3 * sizeof(struct fw_tail);
285
286 buf = malloc(buflen);
287 if (!buf) {
288 ERR("no memory for buffer\n");
289 goto out;
290 }
291
292 p = buf;
293 memset(p, 0, buflen);
294
295 /* fill firmware header */
296 hdr = (struct fw_header *) p;
297 hdr->magic = HOST_TO_LE32(MAGIC_FIRMWARE);
298 hdr->length = HOST_TO_LE32(buflen - sizeof(struct fw_header));
299 p += sizeof(struct fw_header);
300
301 /* fill kernel block header */
302 hdr = (struct fw_header *) p;
303 hdr->magic = HOST_TO_LE32(MAGIC_KERNEL);
304 hdr->length = HOST_TO_LE32(kernel_info.file_size +
305 sizeof(struct fw_tail));
306 p += sizeof(struct fw_header);
307
308 /* read kernel data */
309 ret = read_to_buf(&kernel_info, p);
310 if (ret)
311 goto out_free_buf;
312
313 /* fill firmware tail */
314 tail = (struct fw_tail *) (p + kernel_info.file_size);
315 tail->hw_id = HOST_TO_BE32(board->hw_id);
316 tail->crc = HOST_TO_BE32(cyg_crc32(p, kernel_info.file_size +
317 sizeof(struct fw_tail) - 4));
318
319 p += kernel_info.file_size + sizeof(struct fw_tail);
320
321 /* fill rootfs block header */
322 hdr = (struct fw_header *) p;
323 hdr->magic = HOST_TO_LE32(MAGIC_ROOTFS);
324 hdr->length = HOST_TO_LE32(rootfs_info.file_size +
325 sizeof(struct fw_tail));
326 p += sizeof(struct fw_header);
327
328 /* read rootfs data */
329 ret = read_to_buf(&rootfs_info, p);
330 if (ret)
331 goto out_free_buf;
332
333 /* fill firmware tail */
334 tail = (struct fw_tail *) (p + rootfs_info.file_size);
335 tail->hw_id = HOST_TO_BE32(board->hw_id);
336 tail->crc = HOST_TO_BE32(cyg_crc32(p, rootfs_info.file_size +
337 sizeof(struct fw_tail) - 4));
338
339 p += rootfs_info.file_size + sizeof(struct fw_tail);
340
341 /* fill firmware tail */
342 tail = (struct fw_tail *) p;
343 tail->hw_id = HOST_TO_BE32(board->hw_id);
344 tail->crc = HOST_TO_BE32(cyg_crc32(buf + sizeof(struct fw_header),
345 buflen - sizeof(struct fw_header) - 4));
346
347 ret = write_fw(buf, buflen);
348 if (ret)
349 goto out_free_buf;
350
351 ret = EXIT_SUCCESS;
352
353 out_free_buf:
354 free(buf);
355 out:
356 return ret;
357 }
358
359 int main(int argc, char *argv[])
360 {
361 int ret = EXIT_FAILURE;
362
363 while ( 1 ) {
364 int c;
365
366 c = getopt(argc, argv, "B:k:r:o:h");
367 if (c == -1)
368 break;
369
370 switch (c) {
371 case 'B':
372 board_id = optarg;
373 break;
374 case 'k':
375 kernel_info.file_name = optarg;
376 break;
377 case 'r':
378 rootfs_info.file_name = optarg;
379 break;
380 case 'o':
381 ofname = optarg;
382 break;
383 case 'h':
384 usage(EXIT_SUCCESS);
385 break;
386 default:
387 usage(EXIT_FAILURE);
388 break;
389 }
390 }
391
392 ret = check_options();
393 if (ret)
394 goto out;
395
396 ret = build_fw();
397
398 out:
399 return ret;
400 }
401