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