mkubootimage.c revision 1.6 1 /* $NetBSD: mkubootimage.c,v 1.6 2011/02/26 20:03:09 phx 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.6 2011/02/26 20:03:09 phx Exp $");
34
35 #include <sys/mman.h>
36 #include <sys/stat.h>
37 #include <err.h>
38 #include <errno.h>
39 #include <fcntl.h>
40 #include <limits.h>
41 #include <stdint.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <time.h>
46 #include <unistd.h>
47
48 #include "uboot.h"
49
50 #ifndef __arraycount
51 #define __arraycount(__x) (sizeof(__x) / sizeof(__x[0]))
52 #endif
53
54 extern uint32_t crc32(const void *, size_t);
55
56 static enum uboot_image_os image_os = IH_OS_NETBSD;
57 static enum uboot_image_arch image_arch = IH_ARCH_UNKNOWN;
58 static enum uboot_image_type image_type = IH_TYPE_UNKNOWN;
59 static enum uboot_image_comp image_comp = IH_COMP_NONE;
60 static uint32_t image_loadaddr = 0;
61 static uint32_t image_entrypoint = 0;
62 static char *image_name;
63
64 struct uboot_os {
65 enum uboot_image_os os;
66 const char *name;
67 } uboot_os[] = {
68 { IH_OS_OPENBSD, "openbsd" },
69 { IH_OS_NETBSD, "netbsd" },
70 { IH_OS_FREEBSD, "freebsd" },
71 { IH_OS_LINUX, "linux" },
72 };
73
74 static enum uboot_image_os
75 get_os(const char *name)
76 {
77 unsigned int i;
78
79 for (i = 0; i < __arraycount(uboot_os); i++) {
80 if (strcmp(uboot_os[i].name, name) == 0)
81 return uboot_os[i].os;
82 }
83
84 return IH_OS_UNKNOWN;
85 }
86
87 struct uboot_arch {
88 enum uboot_image_arch arch;
89 const char *name;
90 } uboot_arch[] = {
91 { IH_ARCH_ARM, "arm" },
92 { IH_ARCH_MIPS, "mips" },
93 { IH_ARCH_MIPS64, "mips64" },
94 { IH_ARCH_PPC, "powerpc" },
95 };
96
97 static enum uboot_image_arch
98 get_arch(const char *name)
99 {
100 unsigned int i;
101
102 for (i = 0; i < __arraycount(uboot_arch); i++) {
103 if (strcmp(uboot_arch[i].name, name) == 0)
104 return uboot_arch[i].arch;
105 }
106
107 return IH_ARCH_UNKNOWN;
108 }
109
110 struct uboot_type {
111 enum uboot_image_type type;
112 const char *name;
113 } uboot_type[] = {
114 { IH_TYPE_STANDALONE, "standalone" },
115 { IH_TYPE_KERNEL, "kernel" },
116 { IH_TYPE_RAMDISK, "ramdisk" },
117 { IH_TYPE_FILESYSTEM, "fs" },
118 };
119
120 static enum uboot_image_type
121 get_type(const char *name)
122 {
123 unsigned int i;
124
125 for (i = 0; i < __arraycount(uboot_type); i++) {
126 if (strcmp(uboot_type[i].name, name) == 0)
127 return uboot_type[i].type;
128 }
129
130 return IH_TYPE_UNKNOWN;
131 }
132
133 struct uboot_comp {
134 enum uboot_image_comp comp;
135 const char *name;
136 } uboot_comp[] = {
137 { IH_COMP_NONE, "none" },
138 { IH_COMP_GZIP, "gz" },
139 { IH_COMP_BZIP2, "bz2" },
140 };
141
142 static enum uboot_image_comp
143 get_comp(const char *name)
144 {
145 unsigned int i;
146
147 for (i = 0; i < __arraycount(uboot_comp); i++) {
148 if (strcmp(uboot_comp[i].name, name) == 0)
149 return uboot_comp[i].comp;
150 }
151
152 return IH_TYPE_UNKNOWN;
153 }
154
155 static void
156 usage(void)
157 {
158 fprintf(stderr, "usage: mkubootimage -A <arm|mips|mips64|powerpc>");
159 fprintf(stderr, " -C <none|gz|bz2>");
160 fprintf(stderr, " -O <openbsd|netbsd|freebsd|linux>");
161 fprintf(stderr, " -T <standalone|kernel|ramdisk|fs>");
162 fprintf(stderr, " -a <addr> [-e <ep>] -n <name>");
163 fprintf(stderr, " <srcfile> <dstfile>\n");
164
165 exit(EXIT_FAILURE);
166 }
167
168 static void
169 dump_header(struct uboot_image_header *hdr)
170 {
171 time_t tm = ntohl(hdr->ih_time);
172
173 printf(" magic: 0x%08x\n", ntohl(hdr->ih_magic));
174 printf(" time: %s", ctime(&tm));
175 printf(" size: %u\n", ntohl(hdr->ih_size));
176 printf(" load addr: 0x%08x\n", ntohl(hdr->ih_load));
177 printf(" entry point: 0x%08x\n", ntohl(hdr->ih_ep));
178 printf(" data crc: 0x%08x\n", ntohl(hdr->ih_dcrc));
179 printf(" os: %d\n", hdr->ih_os);
180 printf(" arch: %d\n", hdr->ih_arch);
181 printf(" type: %d\n", hdr->ih_type);
182 printf(" comp: %d\n", hdr->ih_comp);
183 printf(" name: %s\n", hdr->ih_name);
184 printf(" header crc: 0x%08x\n", hdr->ih_hcrc);
185 }
186
187 static int
188 generate_header(struct uboot_image_header *hdr, int kernel_fd)
189 {
190 uint8_t *p;
191 struct stat st;
192 uint32_t crc;
193 int error;
194
195 error = fstat(kernel_fd, &st);
196 if (error == -1) {
197 perror("stat");
198 return errno;
199 }
200
201 if (st.st_size + sizeof(*hdr) > UINT32_MAX) {
202 fprintf(stderr, "fatal: kernel too big\n");
203 return EINVAL;
204 }
205
206 p = mmap(0, st.st_size, PROT_READ, MAP_FILE|MAP_SHARED, kernel_fd, 0);
207 if (p == MAP_FAILED) {
208 perror("mmap kernel");
209 return EINVAL;
210 }
211 crc = crc32(p, st.st_size);
212 munmap(p, st.st_size);
213
214 memset(hdr, 0, sizeof(*hdr));
215 hdr->ih_magic = htonl(IH_MAGIC);
216 hdr->ih_time = htonl(st.st_mtime);
217 hdr->ih_size = htonl(st.st_size);
218 hdr->ih_load = htonl(image_loadaddr);
219 hdr->ih_ep = htonl(image_entrypoint);
220 hdr->ih_dcrc = htonl(crc);
221 hdr->ih_os = image_os;
222 hdr->ih_arch = image_arch;
223 hdr->ih_type = image_type;
224 hdr->ih_comp = image_comp;
225 strncpy((char *)hdr->ih_name, image_name, sizeof(hdr->ih_name));
226 crc = crc32((void *)hdr, sizeof(*hdr));
227 hdr->ih_hcrc = htonl(crc);
228
229 dump_header(hdr);
230
231 return 0;
232 }
233
234 static int
235 write_image(struct uboot_image_header *hdr, int kernel_fd, int image_fd)
236 {
237 uint8_t buf[4096];
238 ssize_t rlen, wlen;
239
240 wlen = write(image_fd, hdr, sizeof(*hdr));
241 if (wlen != sizeof(*hdr)) {
242 perror("short write");
243 return errno;
244 }
245
246 while ((rlen = read(kernel_fd, buf, sizeof(buf))) > 0) {
247 wlen = write(image_fd, buf, rlen);
248 if (wlen != rlen) {
249 perror("short write");
250 return errno;
251 }
252 }
253
254 return 0;
255 }
256
257 int
258 main(int argc, char *argv[])
259 {
260 struct uboot_image_header hdr;
261 const char *src, *dest;
262 char *ep;
263 int kernel_fd, image_fd;
264 int ch;
265 unsigned long num;
266
267 while ((ch = getopt(argc, argv, "A:C:O:T:a:e:hn:")) != -1) {
268 switch (ch) {
269 case 'A': /* arch */
270 image_arch = get_arch(optarg);
271 break;
272 case 'C': /* comp */
273 image_comp = get_comp(optarg);
274 break;
275 case 'O': /* os */
276 image_os = get_os(optarg);
277 break;
278 case 'T': /* type */
279 image_type = get_type(optarg);
280 break;
281 case 'a': /* addr */
282 errno = 0;
283 num = strtoul(optarg, &ep, 0);
284 if (*ep != '\0' || (errno == ERANGE &&
285 (num == ULONG_MAX || num == 0)))
286 errx(1, "illegal number -- %s", optarg);
287 image_loadaddr = (uint32_t)num;
288 break;
289 case 'e': /* ep */
290 errno = 0;
291 num = strtoul(optarg, &ep, 0);
292 if (*ep != '\0' || (errno == ERANGE &&
293 (num == ULONG_MAX || num == 0)))
294 errx(1, "illegal number -- %s", optarg);
295 image_entrypoint = (uint32_t)num;
296 break;
297 case 'n': /* name */
298 image_name = strdup(optarg);
299 break;
300 case 'h':
301 default:
302 usage();
303 /* NOTREACHED */
304 }
305 }
306 argc -= optind;
307 argv += optind;
308
309 if (argc != 2)
310 usage();
311
312 if (image_entrypoint == 0)
313 image_entrypoint = image_loadaddr;
314
315 if (image_arch == IH_ARCH_UNKNOWN ||
316 image_type == IH_TYPE_UNKNOWN ||
317 image_loadaddr == 0 ||
318 image_name == NULL)
319 usage();
320
321 src = argv[0];
322 dest = argv[1];
323
324 kernel_fd = open(src, O_RDONLY);
325 if (kernel_fd == -1) {
326 perror("open kernel");
327 return EXIT_FAILURE;
328 }
329 image_fd = open(dest, O_WRONLY|O_CREAT, 0666);
330 if (image_fd == -1) {
331 perror("open image");
332 return EXIT_FAILURE;
333 }
334
335 if (generate_header(&hdr, kernel_fd) != 0)
336 return EXIT_FAILURE;
337
338 if (write_image(&hdr, kernel_fd, image_fd) != 0)
339 return EXIT_FAILURE;
340
341 close(image_fd);
342 close(kernel_fd);
343
344 return EXIT_SUCCESS;
345 }
346