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