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