mkubootimage.c revision 1.18.8.2 1 /* $NetBSD: mkubootimage.c,v 1.18.8.2 2017/07/25 02:00:33 snj Exp $ */
2
3 /*-
4 * Copyright (c) 2010 Jared D. McNeill <jmcneill (at) invisible.ca>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. The name of the author may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #if HAVE_NBTOOL_CONFIG_H
29 #include "nbtool_config.h"
30 #endif
31
32 #include <sys/cdefs.h>
33 __RCSID("$NetBSD: mkubootimage.c,v 1.18.8.2 2017/07/25 02:00:33 snj Exp $");
34
35 #include <sys/mman.h>
36 #include <sys/stat.h>
37 #include <sys/endian.h>
38 #include <sys/uio.h>
39 #include <err.h>
40 #include <errno.h>
41 #include <fcntl.h>
42 #include <limits.h>
43 #include <stdint.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <time.h>
48 #include <unistd.h>
49
50 #include "uboot.h"
51
52 #ifndef __arraycount
53 #define __arraycount(__x) (sizeof(__x) / sizeof(__x[0]))
54 #endif
55
56 extern uint32_t crc32(const void *, size_t);
57 extern uint32_t crc32v(const struct iovec *, int);
58
59 static enum uboot_image_os image_os = IH_OS_NETBSD;
60 static enum uboot_image_arch image_arch = IH_ARCH_UNKNOWN;
61 static enum uboot_image_type image_type = IH_TYPE_UNKNOWN;
62 static enum uboot_image_comp image_comp = IH_COMP_NONE;
63 static uint32_t image_loadaddr = 0;
64 static uint32_t image_entrypoint = 0;
65 static char *image_name;
66 static uint32_t image_magic = IH_MAGIC;
67
68 static const struct uboot_os {
69 enum uboot_image_os os;
70 const char *name;
71 } uboot_os[] = {
72 { IH_OS_OPENBSD, "openbsd" },
73 { IH_OS_NETBSD, "netbsd" },
74 { IH_OS_FREEBSD, "freebsd" },
75 { IH_OS_LINUX, "linux" },
76 };
77
78 static enum uboot_image_os
79 get_os(const char *name)
80 {
81 unsigned int i;
82
83 for (i = 0; i < __arraycount(uboot_os); i++) {
84 if (strcmp(uboot_os[i].name, name) == 0)
85 return uboot_os[i].os;
86 }
87
88 return IH_OS_UNKNOWN;
89 }
90
91 static const char *
92 get_os_name(enum uboot_image_os os)
93 {
94 unsigned int i;
95
96 for (i = 0; i < __arraycount(uboot_os); i++) {
97 if (uboot_os[i].os == os)
98 return uboot_os[i].name;
99 }
100
101 return "Unknown";
102 }
103
104 static const struct uboot_arch {
105 enum uboot_image_arch arch;
106 const char *name;
107 } uboot_arch[] = {
108 { IH_ARCH_ARM, "arm" },
109 { IH_ARCH_I386, "i386" },
110 { IH_ARCH_MIPS, "mips" },
111 { IH_ARCH_MIPS64, "mips64" },
112 { IH_ARCH_PPC, "powerpc" },
113 { IH_ARCH_OPENRISC, "or1k" },
114 { IH_ARCH_ARM64, "arm64" },
115 };
116
117 static enum uboot_image_arch
118 get_arch(const char *name)
119 {
120 unsigned int i;
121
122 for (i = 0; i < __arraycount(uboot_arch); i++) {
123 if (strcmp(uboot_arch[i].name, name) == 0)
124 return uboot_arch[i].arch;
125 }
126
127 return IH_ARCH_UNKNOWN;
128 }
129
130 static const char *
131 get_arch_name(enum uboot_image_arch arch)
132 {
133 unsigned int i;
134
135 for (i = 0; i < __arraycount(uboot_arch); i++) {
136 if (uboot_arch[i].arch == arch)
137 return uboot_arch[i].name;
138 }
139
140 return "Unknown";
141 }
142
143 static const struct uboot_type {
144 enum uboot_image_type type;
145 const char *name;
146 } uboot_type[] = {
147 { IH_TYPE_STANDALONE, "standalone" },
148 { IH_TYPE_KERNEL, "kernel" },
149 { IH_TYPE_KERNEL_NOLOAD, "kernel_noload" },
150 { IH_TYPE_RAMDISK, "ramdisk" },
151 { IH_TYPE_FILESYSTEM, "fs" },
152 { IH_TYPE_SCRIPT, "script" },
153 };
154
155 static enum uboot_image_type
156 get_type(const char *name)
157 {
158 unsigned int i;
159
160 for (i = 0; i < __arraycount(uboot_type); i++) {
161 if (strcmp(uboot_type[i].name, name) == 0)
162 return uboot_type[i].type;
163 }
164
165 return IH_TYPE_UNKNOWN;
166 }
167
168 static const char *
169 get_type_name(enum uboot_image_type type)
170 {
171 unsigned int i;
172
173 for (i = 0; i < __arraycount(uboot_type); i++) {
174 if (uboot_type[i].type == type)
175 return uboot_type[i].name;
176 }
177
178 return "Unknown";
179 }
180
181 static const struct uboot_comp {
182 enum uboot_image_comp comp;
183 const char *name;
184 } uboot_comp[] = {
185 { IH_COMP_NONE, "none" },
186 { IH_COMP_GZIP, "gz" },
187 { IH_COMP_BZIP2, "bz2" },
188 { IH_COMP_LZMA, "lzma" },
189 { IH_COMP_LZO, "lzo" },
190 };
191
192 static enum uboot_image_comp
193 get_comp(const char *name)
194 {
195 unsigned int i;
196
197 for (i = 0; i < __arraycount(uboot_comp); i++) {
198 if (strcmp(uboot_comp[i].name, name) == 0)
199 return uboot_comp[i].comp;
200 }
201
202 return IH_TYPE_UNKNOWN;
203 }
204
205 static const char *
206 get_comp_name(enum uboot_image_comp comp)
207 {
208 unsigned int i;
209
210 for (i = 0; i < __arraycount(uboot_comp); i++) {
211 if (uboot_comp[i].comp == comp)
212 return uboot_comp[i].name;
213 }
214
215 return "Unknown";
216 }
217
218 __dead static void
219 usage(void)
220 {
221 fprintf(stderr, "usage: mkubootimage -A "
222 "<arm|arm64|i386|mips|mips64|or1k|powerpc>");
223 fprintf(stderr, " -C <none|bz2|gz|lzma|lzo>");
224 fprintf(stderr, " -O <openbsd|netbsd|freebsd|linux>");
225 fprintf(stderr, " -T <standalone|kernel|kernel_noload|ramdisk|fs|script>");
226 fprintf(stderr, " -a <addr> [-e <ep>] [-m <magic>] -n <name>");
227 fprintf(stderr, " <srcfile> <dstfile>\n");
228
229 exit(EXIT_FAILURE);
230 }
231
232 static void
233 dump_header(struct uboot_image_header *hdr)
234 {
235 time_t tm = ntohl(hdr->ih_time);
236
237 printf(" magic: 0x%08x\n", ntohl(hdr->ih_magic));
238 printf(" time: %s", ctime(&tm));
239 printf(" size: %u\n", ntohl(hdr->ih_size));
240 printf(" load addr: 0x%08x\n", ntohl(hdr->ih_load));
241 printf(" entry point: 0x%08x\n", ntohl(hdr->ih_ep));
242 printf(" data crc: 0x%08x\n", ntohl(hdr->ih_dcrc));
243 printf(" os: %d (%s)\n", hdr->ih_os,
244 get_os_name(hdr->ih_os));
245 printf(" arch: %d (%s)\n", hdr->ih_arch,
246 get_arch_name(hdr->ih_arch));
247 printf(" type: %d (%s)\n", hdr->ih_type,
248 get_type_name(hdr->ih_type));
249 printf(" comp: %d (%s)\n", hdr->ih_comp,
250 get_comp_name(hdr->ih_comp));
251 printf(" name: %s\n", hdr->ih_name);
252 printf(" header crc: 0x%08x\n", hdr->ih_hcrc);
253 }
254
255 static int
256 generate_header(struct uboot_image_header *hdr, int kernel_fd)
257 {
258 uint8_t *p;
259 struct stat st;
260 uint32_t crc, dsize, size_buf[2];
261 int error;
262
263 error = fstat(kernel_fd, &st);
264 if (error == -1) {
265 perror("stat");
266 return errno;
267 }
268
269 if (st.st_size + sizeof(*hdr) > UINT32_MAX) {
270 fprintf(stderr, "fatal: kernel too big\n");
271 return EINVAL;
272 }
273
274 p = mmap(0, st.st_size, PROT_READ, MAP_FILE|MAP_SHARED, kernel_fd, 0);
275 if (p == MAP_FAILED) {
276 perror("mmap kernel");
277 return EINVAL;
278 }
279 if (image_type == IH_TYPE_SCRIPT) {
280 struct iovec iov[3];
281 dsize = st.st_size + (sizeof(uint32_t) * 2);
282 size_buf[0] = htonl(st.st_size);
283 size_buf[1] = htonl(0);
284 iov[0].iov_base = &size_buf[0];
285 iov[0].iov_len = sizeof(size_buf[0]);
286 iov[1].iov_base = &size_buf[1];
287 iov[1].iov_len = sizeof(size_buf[1]);
288 iov[2].iov_base = p;
289 iov[2].iov_len = st.st_size;
290 crc = crc32v(iov, 3);
291 } else {
292 dsize = st.st_size;
293 crc = crc32(p, st.st_size);
294 }
295 munmap(p, st.st_size);
296
297 memset(hdr, 0, sizeof(*hdr));
298 hdr->ih_magic = htonl(image_magic);
299 hdr->ih_time = htonl(st.st_mtime);
300 hdr->ih_size = htonl(dsize);
301 hdr->ih_load = htonl(image_loadaddr);
302 hdr->ih_ep = htonl(image_entrypoint);
303 hdr->ih_dcrc = htonl(crc);
304 hdr->ih_os = image_os;
305 hdr->ih_arch = image_arch;
306 hdr->ih_type = image_type;
307 hdr->ih_comp = image_comp;
308 strlcpy((char *)hdr->ih_name, image_name, sizeof(hdr->ih_name));
309 crc = crc32((void *)hdr, sizeof(*hdr));
310 hdr->ih_hcrc = htonl(crc);
311
312 dump_header(hdr);
313
314 return 0;
315 }
316
317 static int
318 write_image(struct uboot_image_header *hdr, int kernel_fd, int image_fd)
319 {
320 uint8_t buf[4096];
321 ssize_t rlen, wlen;
322 struct stat st;
323 uint32_t size_buf[2];
324 int error;
325
326 error = fstat(kernel_fd, &st);
327 if (error == -1) {
328 perror("stat");
329 return errno;
330 }
331
332 wlen = write(image_fd, hdr, sizeof(*hdr));
333 if (wlen != sizeof(*hdr)) {
334 perror("short write");
335 return errno;
336 }
337
338 if (image_type == IH_TYPE_SCRIPT) {
339 size_buf[0] = htonl(st.st_size);
340 size_buf[1] = htonl(0);
341 wlen = write(image_fd, &size_buf, sizeof(size_buf));
342 if (wlen != sizeof(size_buf)) {
343 perror("short write");
344 return errno;
345 }
346 }
347
348 while ((rlen = read(kernel_fd, buf, sizeof(buf))) > 0) {
349 wlen = write(image_fd, buf, rlen);
350 if (wlen != rlen) {
351 perror("short write");
352 return errno;
353 }
354 }
355
356 return 0;
357 }
358
359 int
360 main(int argc, char *argv[])
361 {
362 struct uboot_image_header hdr;
363 const char *src, *dest;
364 char *ep;
365 int kernel_fd, image_fd;
366 int ch;
367 unsigned long long num;
368
369 while ((ch = getopt(argc, argv, "A:C:E:O:T:a:e:hm:n:")) != -1) {
370 switch (ch) {
371 case 'A': /* arch */
372 image_arch = get_arch(optarg);
373 break;
374 case 'C': /* comp */
375 image_comp = get_comp(optarg);
376 break;
377 case 'O': /* os */
378 image_os = get_os(optarg);
379 break;
380 case 'T': /* type */
381 image_type = get_type(optarg);
382 break;
383 case 'a': /* addr */
384 errno = 0;
385 num = strtoull(optarg, &ep, 0);
386 if (*ep != '\0' || (errno == ERANGE &&
387 (num == ULLONG_MAX || num == 0)) ||
388 ((signed long long)num != (int32_t)num &&
389 num != (uint32_t)num))
390 errx(1, "illegal number -- %s", optarg);
391 image_loadaddr = (uint32_t)num;
392 break;
393 case 'E': /* ep (byte swapped) */
394 case 'e': /* ep */
395 errno = 0;
396 num = strtoull(optarg, &ep, 0);
397 if (*ep != '\0' || (errno == ERANGE &&
398 (num == ULLONG_MAX || num == 0)) ||
399 ((signed long long)num != (int32_t)num &&
400 num != (uint32_t)num))
401 errx(1, "illegal number -- %s", optarg);
402 image_entrypoint = (uint32_t)num;
403 if (ch == 'E')
404 image_entrypoint = bswap32(image_entrypoint);
405 break;
406 case 'm': /* magic */
407 errno = 0;
408 num = strtoul(optarg, &ep, 0);
409 if (*ep != '\0' || (errno == ERANGE &&
410 (num == ULONG_MAX || num == 0)))
411 errx(1, "illegal number -- %s", optarg);
412 image_magic = (uint32_t)num;
413 case 'n': /* name */
414 image_name = strdup(optarg);
415 break;
416 case 'h':
417 default:
418 usage();
419 /* NOTREACHED */
420 }
421 }
422 argc -= optind;
423 argv += optind;
424
425 if (argc != 2)
426 usage();
427
428 if (image_entrypoint == 0)
429 image_entrypoint = image_loadaddr;
430
431 if (image_arch == IH_ARCH_UNKNOWN ||
432 image_type == IH_TYPE_UNKNOWN ||
433 image_name == NULL)
434 usage();
435
436 switch (image_type) {
437 case IH_TYPE_SCRIPT:
438 case IH_TYPE_RAMDISK:
439 case IH_TYPE_KERNEL_NOLOAD:
440 break;
441 default:
442 if (image_loadaddr == 0)
443 usage();
444 /* NOTREACHED */
445 break;
446 }
447
448 src = argv[0];
449 dest = argv[1];
450
451 kernel_fd = open(src, O_RDONLY);
452 if (kernel_fd == -1) {
453 perror("open kernel");
454 return EXIT_FAILURE;
455 }
456 image_fd = open(dest, O_WRONLY|O_CREAT|O_TRUNC, 0666);
457 if (image_fd == -1) {
458 perror("open image");
459 return EXIT_FAILURE;
460 }
461
462 if (generate_header(&hdr, kernel_fd) != 0)
463 return EXIT_FAILURE;
464
465 if (write_image(&hdr, kernel_fd, image_fd) != 0)
466 return EXIT_FAILURE;
467
468 close(image_fd);
469 close(kernel_fd);
470
471 return EXIT_SUCCESS;
472 }
473