mkubootimage.c revision 1.23 1 1.23 jmcneill /* $NetBSD: mkubootimage.c,v 1.23 2018/02/04 15:44:51 jmcneill 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.23 jmcneill __RCSID("$NetBSD: mkubootimage.c,v 1.23 2018/02/04 15:44:51 jmcneill 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.23 jmcneill #include <sys/param.h>
39 1.17 jmcneill #include <sys/uio.h>
40 1.1 jmcneill #include <err.h>
41 1.1 jmcneill #include <errno.h>
42 1.1 jmcneill #include <fcntl.h>
43 1.23 jmcneill #include <inttypes.h>
44 1.1 jmcneill #include <limits.h>
45 1.1 jmcneill #include <stdint.h>
46 1.1 jmcneill #include <stdio.h>
47 1.1 jmcneill #include <stdlib.h>
48 1.1 jmcneill #include <string.h>
49 1.1 jmcneill #include <time.h>
50 1.1 jmcneill #include <unistd.h>
51 1.1 jmcneill
52 1.1 jmcneill #include "uboot.h"
53 1.23 jmcneill #include "arm64.h"
54 1.1 jmcneill
55 1.1 jmcneill #ifndef __arraycount
56 1.1 jmcneill #define __arraycount(__x) (sizeof(__x) / sizeof(__x[0]))
57 1.1 jmcneill #endif
58 1.1 jmcneill
59 1.23 jmcneill enum image_format {
60 1.23 jmcneill FMT_UNKNOWN,
61 1.23 jmcneill FMT_UIMG, /* Legacy U-Boot image */
62 1.23 jmcneill FMT_ARM64, /* Linux ARM64 image (booti) */
63 1.23 jmcneill };
64 1.23 jmcneill
65 1.1 jmcneill extern uint32_t crc32(const void *, size_t);
66 1.17 jmcneill extern uint32_t crc32v(const struct iovec *, int);
67 1.1 jmcneill
68 1.6 phx static enum uboot_image_os image_os = IH_OS_NETBSD;
69 1.1 jmcneill static enum uboot_image_arch image_arch = IH_ARCH_UNKNOWN;
70 1.1 jmcneill static enum uboot_image_type image_type = IH_TYPE_UNKNOWN;
71 1.1 jmcneill static enum uboot_image_comp image_comp = IH_COMP_NONE;
72 1.1 jmcneill static uint32_t image_loadaddr = 0;
73 1.1 jmcneill static uint32_t image_entrypoint = 0;
74 1.1 jmcneill static char *image_name;
75 1.8 riz static uint32_t image_magic = IH_MAGIC;
76 1.23 jmcneill static enum image_format image_format = FMT_UIMG;
77 1.23 jmcneill
78 1.23 jmcneill static const struct uboot_image_format {
79 1.23 jmcneill enum image_format format;
80 1.23 jmcneill const char *name;
81 1.23 jmcneill } uboot_image_format[] = {
82 1.23 jmcneill { FMT_UIMG, "uimg" },
83 1.23 jmcneill { FMT_ARM64, "arm64" },
84 1.23 jmcneill };
85 1.23 jmcneill
86 1.23 jmcneill static enum image_format
87 1.23 jmcneill get_image_format(const char *name)
88 1.23 jmcneill {
89 1.23 jmcneill unsigned int i;
90 1.23 jmcneill
91 1.23 jmcneill for (i = 0; i < __arraycount(uboot_image_format); i++) {
92 1.23 jmcneill if (strcmp(uboot_image_format[i].name, name) == 0)
93 1.23 jmcneill return uboot_image_format[i].format;
94 1.23 jmcneill }
95 1.23 jmcneill
96 1.23 jmcneill return FMT_UNKNOWN;
97 1.23 jmcneill }
98 1.23 jmcneill
99 1.23 jmcneill static const char *
100 1.23 jmcneill get_image_format_name(enum image_format format)
101 1.23 jmcneill {
102 1.23 jmcneill unsigned int i;
103 1.23 jmcneill
104 1.23 jmcneill for (i = 0; i < __arraycount(uboot_image_format); i++) {
105 1.23 jmcneill if (uboot_image_format[i].format == format)
106 1.23 jmcneill return uboot_image_format[i].name;
107 1.23 jmcneill }
108 1.23 jmcneill
109 1.23 jmcneill return "Unknown";
110 1.23 jmcneill }
111 1.1 jmcneill
112 1.14 joerg static const struct uboot_os {
113 1.6 phx enum uboot_image_os os;
114 1.6 phx const char *name;
115 1.6 phx } uboot_os[] = {
116 1.6 phx { IH_OS_OPENBSD, "openbsd" },
117 1.6 phx { IH_OS_NETBSD, "netbsd" },
118 1.6 phx { IH_OS_FREEBSD, "freebsd" },
119 1.6 phx { IH_OS_LINUX, "linux" },
120 1.6 phx };
121 1.6 phx
122 1.6 phx static enum uboot_image_os
123 1.6 phx get_os(const char *name)
124 1.6 phx {
125 1.6 phx unsigned int i;
126 1.6 phx
127 1.6 phx for (i = 0; i < __arraycount(uboot_os); i++) {
128 1.6 phx if (strcmp(uboot_os[i].name, name) == 0)
129 1.6 phx return uboot_os[i].os;
130 1.6 phx }
131 1.6 phx
132 1.6 phx return IH_OS_UNKNOWN;
133 1.6 phx }
134 1.6 phx
135 1.7 matt static const char *
136 1.7 matt get_os_name(enum uboot_image_os os)
137 1.7 matt {
138 1.7 matt unsigned int i;
139 1.7 matt
140 1.7 matt for (i = 0; i < __arraycount(uboot_os); i++) {
141 1.7 matt if (uboot_os[i].os == os)
142 1.7 matt return uboot_os[i].name;
143 1.7 matt }
144 1.7 matt
145 1.7 matt return "Unknown";
146 1.7 matt }
147 1.7 matt
148 1.13 joerg static const struct uboot_arch {
149 1.1 jmcneill enum uboot_image_arch arch;
150 1.1 jmcneill const char *name;
151 1.1 jmcneill } uboot_arch[] = {
152 1.1 jmcneill { IH_ARCH_ARM, "arm" },
153 1.21 jmcneill { IH_ARCH_ARM64, "arm64" },
154 1.18 msaitoh { IH_ARCH_I386, "i386" },
155 1.5 matt { IH_ARCH_MIPS, "mips" },
156 1.5 matt { IH_ARCH_MIPS64, "mips64" },
157 1.1 jmcneill { IH_ARCH_PPC, "powerpc" },
158 1.18 msaitoh { IH_ARCH_OPENRISC, "or1k" },
159 1.21 jmcneill { IH_ARCH_SH, "sh" },
160 1.1 jmcneill };
161 1.1 jmcneill
162 1.1 jmcneill static enum uboot_image_arch
163 1.1 jmcneill get_arch(const char *name)
164 1.1 jmcneill {
165 1.1 jmcneill unsigned int i;
166 1.1 jmcneill
167 1.1 jmcneill for (i = 0; i < __arraycount(uboot_arch); i++) {
168 1.1 jmcneill if (strcmp(uboot_arch[i].name, name) == 0)
169 1.1 jmcneill return uboot_arch[i].arch;
170 1.1 jmcneill }
171 1.1 jmcneill
172 1.1 jmcneill return IH_ARCH_UNKNOWN;
173 1.1 jmcneill }
174 1.1 jmcneill
175 1.7 matt static const char *
176 1.7 matt get_arch_name(enum uboot_image_arch arch)
177 1.7 matt {
178 1.7 matt unsigned int i;
179 1.7 matt
180 1.7 matt for (i = 0; i < __arraycount(uboot_arch); i++) {
181 1.7 matt if (uboot_arch[i].arch == arch)
182 1.7 matt return uboot_arch[i].name;
183 1.7 matt }
184 1.7 matt
185 1.7 matt return "Unknown";
186 1.7 matt }
187 1.7 matt
188 1.14 joerg static const struct uboot_type {
189 1.1 jmcneill enum uboot_image_type type;
190 1.1 jmcneill const char *name;
191 1.1 jmcneill } uboot_type[] = {
192 1.20 jmcneill { IH_TYPE_STANDALONE, "standalone" },
193 1.20 jmcneill { IH_TYPE_KERNEL, "kernel" },
194 1.20 jmcneill { IH_TYPE_KERNEL_NOLOAD, "kernel_noload" },
195 1.20 jmcneill { IH_TYPE_RAMDISK, "ramdisk" },
196 1.20 jmcneill { IH_TYPE_FILESYSTEM, "fs" },
197 1.20 jmcneill { IH_TYPE_SCRIPT, "script" },
198 1.1 jmcneill };
199 1.1 jmcneill
200 1.1 jmcneill static enum uboot_image_type
201 1.1 jmcneill get_type(const char *name)
202 1.1 jmcneill {
203 1.1 jmcneill unsigned int i;
204 1.1 jmcneill
205 1.1 jmcneill for (i = 0; i < __arraycount(uboot_type); i++) {
206 1.1 jmcneill if (strcmp(uboot_type[i].name, name) == 0)
207 1.1 jmcneill return uboot_type[i].type;
208 1.1 jmcneill }
209 1.1 jmcneill
210 1.1 jmcneill return IH_TYPE_UNKNOWN;
211 1.1 jmcneill }
212 1.1 jmcneill
213 1.7 matt static const char *
214 1.7 matt get_type_name(enum uboot_image_type type)
215 1.7 matt {
216 1.7 matt unsigned int i;
217 1.7 matt
218 1.7 matt for (i = 0; i < __arraycount(uboot_type); i++) {
219 1.7 matt if (uboot_type[i].type == type)
220 1.7 matt return uboot_type[i].name;
221 1.7 matt }
222 1.7 matt
223 1.7 matt return "Unknown";
224 1.7 matt }
225 1.7 matt
226 1.14 joerg static const struct uboot_comp {
227 1.1 jmcneill enum uboot_image_comp comp;
228 1.1 jmcneill const char *name;
229 1.1 jmcneill } uboot_comp[] = {
230 1.1 jmcneill { IH_COMP_NONE, "none" },
231 1.1 jmcneill { IH_COMP_GZIP, "gz" },
232 1.1 jmcneill { IH_COMP_BZIP2, "bz2" },
233 1.10 matt { IH_COMP_LZMA, "lzma" },
234 1.10 matt { IH_COMP_LZO, "lzo" },
235 1.1 jmcneill };
236 1.1 jmcneill
237 1.1 jmcneill static enum uboot_image_comp
238 1.1 jmcneill get_comp(const char *name)
239 1.1 jmcneill {
240 1.1 jmcneill unsigned int i;
241 1.1 jmcneill
242 1.1 jmcneill for (i = 0; i < __arraycount(uboot_comp); i++) {
243 1.1 jmcneill if (strcmp(uboot_comp[i].name, name) == 0)
244 1.1 jmcneill return uboot_comp[i].comp;
245 1.1 jmcneill }
246 1.1 jmcneill
247 1.1 jmcneill return IH_TYPE_UNKNOWN;
248 1.1 jmcneill }
249 1.1 jmcneill
250 1.7 matt static const char *
251 1.7 matt get_comp_name(enum uboot_image_comp comp)
252 1.7 matt {
253 1.7 matt unsigned int i;
254 1.7 matt
255 1.7 matt for (i = 0; i < __arraycount(uboot_comp); i++) {
256 1.7 matt if (uboot_comp[i].comp == comp)
257 1.7 matt return uboot_comp[i].name;
258 1.7 matt }
259 1.7 matt
260 1.7 matt return "Unknown";
261 1.7 matt }
262 1.7 matt
263 1.13 joerg __dead static void
264 1.1 jmcneill usage(void)
265 1.1 jmcneill {
266 1.18 msaitoh fprintf(stderr, "usage: mkubootimage -A "
267 1.21 jmcneill "<arm|arm64|i386|mips|mips64|or1k|powerpc|sh>");
268 1.12 wiz fprintf(stderr, " -C <none|bz2|gz|lzma|lzo>");
269 1.6 phx fprintf(stderr, " -O <openbsd|netbsd|freebsd|linux>");
270 1.20 jmcneill fprintf(stderr, " -T <standalone|kernel|kernel_noload|ramdisk|fs|script>");
271 1.8 riz fprintf(stderr, " -a <addr> [-e <ep>] [-m <magic>] -n <name>");
272 1.23 jmcneill fprintf(stderr, " [-f <uimg|arm64>]");
273 1.1 jmcneill fprintf(stderr, " <srcfile> <dstfile>\n");
274 1.1 jmcneill
275 1.1 jmcneill exit(EXIT_FAILURE);
276 1.1 jmcneill }
277 1.1 jmcneill
278 1.1 jmcneill static void
279 1.23 jmcneill dump_header_uimg(struct uboot_image_header *hdr)
280 1.1 jmcneill {
281 1.1 jmcneill time_t tm = ntohl(hdr->ih_time);
282 1.1 jmcneill
283 1.1 jmcneill printf(" magic: 0x%08x\n", ntohl(hdr->ih_magic));
284 1.1 jmcneill printf(" time: %s", ctime(&tm));
285 1.1 jmcneill printf(" size: %u\n", ntohl(hdr->ih_size));
286 1.1 jmcneill printf(" load addr: 0x%08x\n", ntohl(hdr->ih_load));
287 1.1 jmcneill printf(" entry point: 0x%08x\n", ntohl(hdr->ih_ep));
288 1.1 jmcneill printf(" data crc: 0x%08x\n", ntohl(hdr->ih_dcrc));
289 1.7 matt printf(" os: %d (%s)\n", hdr->ih_os,
290 1.7 matt get_os_name(hdr->ih_os));
291 1.7 matt printf(" arch: %d (%s)\n", hdr->ih_arch,
292 1.7 matt get_arch_name(hdr->ih_arch));
293 1.7 matt printf(" type: %d (%s)\n", hdr->ih_type,
294 1.7 matt get_type_name(hdr->ih_type));
295 1.7 matt printf(" comp: %d (%s)\n", hdr->ih_comp,
296 1.7 matt get_comp_name(hdr->ih_comp));
297 1.1 jmcneill printf(" name: %s\n", hdr->ih_name);
298 1.1 jmcneill printf(" header crc: 0x%08x\n", hdr->ih_hcrc);
299 1.1 jmcneill }
300 1.1 jmcneill
301 1.1 jmcneill static int
302 1.23 jmcneill generate_header_uimg(struct uboot_image_header *hdr, int kernel_fd)
303 1.1 jmcneill {
304 1.1 jmcneill uint8_t *p;
305 1.1 jmcneill struct stat st;
306 1.17 jmcneill uint32_t crc, dsize, size_buf[2];
307 1.1 jmcneill int error;
308 1.1 jmcneill
309 1.1 jmcneill error = fstat(kernel_fd, &st);
310 1.1 jmcneill if (error == -1) {
311 1.1 jmcneill perror("stat");
312 1.1 jmcneill return errno;
313 1.1 jmcneill }
314 1.1 jmcneill
315 1.1 jmcneill if (st.st_size + sizeof(*hdr) > UINT32_MAX) {
316 1.1 jmcneill fprintf(stderr, "fatal: kernel too big\n");
317 1.1 jmcneill return EINVAL;
318 1.1 jmcneill }
319 1.1 jmcneill
320 1.1 jmcneill p = mmap(0, st.st_size, PROT_READ, MAP_FILE|MAP_SHARED, kernel_fd, 0);
321 1.1 jmcneill if (p == MAP_FAILED) {
322 1.1 jmcneill perror("mmap kernel");
323 1.1 jmcneill return EINVAL;
324 1.1 jmcneill }
325 1.17 jmcneill if (image_type == IH_TYPE_SCRIPT) {
326 1.17 jmcneill struct iovec iov[3];
327 1.17 jmcneill dsize = st.st_size + (sizeof(uint32_t) * 2);
328 1.17 jmcneill size_buf[0] = htonl(st.st_size);
329 1.17 jmcneill size_buf[1] = htonl(0);
330 1.17 jmcneill iov[0].iov_base = &size_buf[0];
331 1.17 jmcneill iov[0].iov_len = sizeof(size_buf[0]);
332 1.17 jmcneill iov[1].iov_base = &size_buf[1];
333 1.17 jmcneill iov[1].iov_len = sizeof(size_buf[1]);
334 1.17 jmcneill iov[2].iov_base = p;
335 1.17 jmcneill iov[2].iov_len = st.st_size;
336 1.17 jmcneill crc = crc32v(iov, 3);
337 1.17 jmcneill } else {
338 1.17 jmcneill dsize = st.st_size;
339 1.17 jmcneill crc = crc32(p, st.st_size);
340 1.17 jmcneill }
341 1.1 jmcneill munmap(p, st.st_size);
342 1.1 jmcneill
343 1.1 jmcneill memset(hdr, 0, sizeof(*hdr));
344 1.8 riz hdr->ih_magic = htonl(image_magic);
345 1.1 jmcneill hdr->ih_time = htonl(st.st_mtime);
346 1.17 jmcneill hdr->ih_size = htonl(dsize);
347 1.1 jmcneill hdr->ih_load = htonl(image_loadaddr);
348 1.1 jmcneill hdr->ih_ep = htonl(image_entrypoint);
349 1.1 jmcneill hdr->ih_dcrc = htonl(crc);
350 1.6 phx hdr->ih_os = image_os;
351 1.1 jmcneill hdr->ih_arch = image_arch;
352 1.1 jmcneill hdr->ih_type = image_type;
353 1.1 jmcneill hdr->ih_comp = image_comp;
354 1.7 matt strlcpy((char *)hdr->ih_name, image_name, sizeof(hdr->ih_name));
355 1.1 jmcneill crc = crc32((void *)hdr, sizeof(*hdr));
356 1.1 jmcneill hdr->ih_hcrc = htonl(crc);
357 1.1 jmcneill
358 1.23 jmcneill dump_header_uimg(hdr);
359 1.1 jmcneill
360 1.1 jmcneill return 0;
361 1.1 jmcneill }
362 1.1 jmcneill
363 1.23 jmcneill static void
364 1.23 jmcneill dump_header_arm64(struct arm64_image_header *hdr)
365 1.23 jmcneill {
366 1.23 jmcneill printf(" magic: 0x%" PRIx32 "\n", le32toh(hdr->magic));
367 1.23 jmcneill printf(" text offset: 0x%" PRIx64 "\n", le64toh(hdr->text_offset));
368 1.23 jmcneill printf(" image size: %" PRIu64 "\n", le64toh(hdr->image_size));
369 1.23 jmcneill printf(" flags: 0x%" PRIx64 "\n", le64toh(hdr->flags));
370 1.23 jmcneill }
371 1.23 jmcneill
372 1.1 jmcneill static int
373 1.23 jmcneill generate_header_arm64(struct arm64_image_header *hdr, int kernel_fd)
374 1.23 jmcneill {
375 1.23 jmcneill struct stat st;
376 1.23 jmcneill uint32_t flags;
377 1.23 jmcneill int error;
378 1.23 jmcneill
379 1.23 jmcneill error = fstat(kernel_fd, &st);
380 1.23 jmcneill if (error == -1) {
381 1.23 jmcneill perror("stat");
382 1.23 jmcneill return errno;
383 1.23 jmcneill }
384 1.23 jmcneill
385 1.23 jmcneill flags = 0;
386 1.23 jmcneill
387 1.23 jmcneill flags |= __SHIFTIN(ARM64_FLAGS_PAGE_SIZE_4K,
388 1.23 jmcneill ARM64_FLAGS_PAGE_SIZE);
389 1.23 jmcneill #if 0
390 1.23 jmcneill flags |= __SHIFTIN(ARM64_FLAGS_PHYS_PLACEMENT_ANY,
391 1.23 jmcneill ARM64_FLAGS_PHYS_PLACEMENT);
392 1.23 jmcneill #endif
393 1.23 jmcneill
394 1.23 jmcneill memset(hdr, 0, sizeof(*hdr));
395 1.23 jmcneill hdr->code0 = htole32(ARM64_CODE0);
396 1.23 jmcneill hdr->text_offset = htole64(image_entrypoint);
397 1.23 jmcneill hdr->image_size = htole64(st.st_size + sizeof(*hdr));
398 1.23 jmcneill hdr->flags = htole32(flags);
399 1.23 jmcneill hdr->magic = htole32(ARM64_MAGIC);
400 1.23 jmcneill
401 1.23 jmcneill dump_header_arm64(hdr);
402 1.23 jmcneill
403 1.23 jmcneill return 0;
404 1.23 jmcneill }
405 1.23 jmcneill
406 1.23 jmcneill static int
407 1.23 jmcneill write_image(void *hdr, size_t hdrlen, int kernel_fd, int image_fd)
408 1.1 jmcneill {
409 1.1 jmcneill uint8_t buf[4096];
410 1.1 jmcneill ssize_t rlen, wlen;
411 1.17 jmcneill struct stat st;
412 1.17 jmcneill uint32_t size_buf[2];
413 1.17 jmcneill int error;
414 1.17 jmcneill
415 1.17 jmcneill error = fstat(kernel_fd, &st);
416 1.17 jmcneill if (error == -1) {
417 1.17 jmcneill perror("stat");
418 1.17 jmcneill return errno;
419 1.17 jmcneill }
420 1.1 jmcneill
421 1.23 jmcneill wlen = write(image_fd, hdr, hdrlen);
422 1.23 jmcneill if (wlen != (ssize_t)hdrlen) {
423 1.1 jmcneill perror("short write");
424 1.1 jmcneill return errno;
425 1.1 jmcneill }
426 1.1 jmcneill
427 1.17 jmcneill if (image_type == IH_TYPE_SCRIPT) {
428 1.17 jmcneill size_buf[0] = htonl(st.st_size);
429 1.17 jmcneill size_buf[1] = htonl(0);
430 1.17 jmcneill wlen = write(image_fd, &size_buf, sizeof(size_buf));
431 1.17 jmcneill if (wlen != sizeof(size_buf)) {
432 1.17 jmcneill perror("short write");
433 1.17 jmcneill return errno;
434 1.17 jmcneill }
435 1.17 jmcneill }
436 1.17 jmcneill
437 1.1 jmcneill while ((rlen = read(kernel_fd, buf, sizeof(buf))) > 0) {
438 1.1 jmcneill wlen = write(image_fd, buf, rlen);
439 1.1 jmcneill if (wlen != rlen) {
440 1.1 jmcneill perror("short write");
441 1.1 jmcneill return errno;
442 1.1 jmcneill }
443 1.1 jmcneill }
444 1.1 jmcneill
445 1.1 jmcneill return 0;
446 1.1 jmcneill }
447 1.1 jmcneill
448 1.1 jmcneill int
449 1.1 jmcneill main(int argc, char *argv[])
450 1.1 jmcneill {
451 1.23 jmcneill struct uboot_image_header hdr_uimg;
452 1.23 jmcneill struct arm64_image_header hdr_arm64;
453 1.1 jmcneill const char *src, *dest;
454 1.1 jmcneill char *ep;
455 1.1 jmcneill int kernel_fd, image_fd;
456 1.1 jmcneill int ch;
457 1.15 matt unsigned long long num;
458 1.1 jmcneill
459 1.23 jmcneill while ((ch = getopt(argc, argv, "A:C:E:O:T:a:e:f:hm:n:")) != -1) {
460 1.1 jmcneill switch (ch) {
461 1.1 jmcneill case 'A': /* arch */
462 1.1 jmcneill image_arch = get_arch(optarg);
463 1.1 jmcneill break;
464 1.1 jmcneill case 'C': /* comp */
465 1.1 jmcneill image_comp = get_comp(optarg);
466 1.1 jmcneill break;
467 1.6 phx case 'O': /* os */
468 1.6 phx image_os = get_os(optarg);
469 1.6 phx break;
470 1.1 jmcneill case 'T': /* type */
471 1.1 jmcneill image_type = get_type(optarg);
472 1.1 jmcneill break;
473 1.1 jmcneill case 'a': /* addr */
474 1.1 jmcneill errno = 0;
475 1.15 matt num = strtoull(optarg, &ep, 0);
476 1.1 jmcneill if (*ep != '\0' || (errno == ERANGE &&
477 1.15 matt (num == ULLONG_MAX || num == 0)) ||
478 1.16 matt ((signed long long)num != (int32_t)num &&
479 1.16 matt num != (uint32_t)num))
480 1.1 jmcneill errx(1, "illegal number -- %s", optarg);
481 1.1 jmcneill image_loadaddr = (uint32_t)num;
482 1.1 jmcneill break;
483 1.9 matt case 'E': /* ep (byte swapped) */
484 1.1 jmcneill case 'e': /* ep */
485 1.1 jmcneill errno = 0;
486 1.15 matt num = strtoull(optarg, &ep, 0);
487 1.1 jmcneill if (*ep != '\0' || (errno == ERANGE &&
488 1.15 matt (num == ULLONG_MAX || num == 0)) ||
489 1.16 matt ((signed long long)num != (int32_t)num &&
490 1.16 matt num != (uint32_t)num))
491 1.1 jmcneill errx(1, "illegal number -- %s", optarg);
492 1.1 jmcneill image_entrypoint = (uint32_t)num;
493 1.9 matt if (ch == 'E')
494 1.9 matt image_entrypoint = bswap32(image_entrypoint);
495 1.1 jmcneill break;
496 1.23 jmcneill case 'f': /* image format */
497 1.23 jmcneill image_format = get_image_format(optarg);
498 1.23 jmcneill break;
499 1.8 riz case 'm': /* magic */
500 1.8 riz errno = 0;
501 1.8 riz num = strtoul(optarg, &ep, 0);
502 1.8 riz if (*ep != '\0' || (errno == ERANGE &&
503 1.8 riz (num == ULONG_MAX || num == 0)))
504 1.8 riz errx(1, "illegal number -- %s", optarg);
505 1.8 riz image_magic = (uint32_t)num;
506 1.22 jmcneill break;
507 1.1 jmcneill case 'n': /* name */
508 1.1 jmcneill image_name = strdup(optarg);
509 1.1 jmcneill break;
510 1.1 jmcneill case 'h':
511 1.1 jmcneill default:
512 1.1 jmcneill usage();
513 1.1 jmcneill /* NOTREACHED */
514 1.1 jmcneill }
515 1.1 jmcneill }
516 1.1 jmcneill argc -= optind;
517 1.1 jmcneill argv += optind;
518 1.1 jmcneill
519 1.1 jmcneill if (argc != 2)
520 1.1 jmcneill usage();
521 1.1 jmcneill
522 1.4 kiyohara if (image_entrypoint == 0)
523 1.4 kiyohara image_entrypoint = image_loadaddr;
524 1.4 kiyohara
525 1.23 jmcneill switch (image_format) {
526 1.23 jmcneill case FMT_UIMG:
527 1.23 jmcneill if (image_arch == IH_ARCH_UNKNOWN ||
528 1.23 jmcneill image_type == IH_TYPE_UNKNOWN ||
529 1.23 jmcneill image_name == NULL)
530 1.23 jmcneill usage();
531 1.23 jmcneill /* NOTREACHED */
532 1.1 jmcneill
533 1.23 jmcneill switch (image_type) {
534 1.23 jmcneill case IH_TYPE_SCRIPT:
535 1.23 jmcneill case IH_TYPE_RAMDISK:
536 1.23 jmcneill case IH_TYPE_KERNEL_NOLOAD:
537 1.23 jmcneill break;
538 1.23 jmcneill default:
539 1.23 jmcneill if (image_loadaddr == 0)
540 1.23 jmcneill usage();
541 1.23 jmcneill /* NOTREACHED */
542 1.23 jmcneill break;
543 1.23 jmcneill }
544 1.20 jmcneill break;
545 1.23 jmcneill
546 1.23 jmcneill case FMT_ARM64:
547 1.23 jmcneill if (image_arch != IH_ARCH_UNKNOWN &&
548 1.23 jmcneill image_arch != IH_ARCH_ARM64)
549 1.20 jmcneill usage();
550 1.20 jmcneill /* NOTREACHED */
551 1.23 jmcneill
552 1.20 jmcneill break;
553 1.23 jmcneill
554 1.23 jmcneill default:
555 1.23 jmcneill usage();
556 1.23 jmcneill /* NOTREACHED */
557 1.20 jmcneill }
558 1.20 jmcneill
559 1.1 jmcneill src = argv[0];
560 1.1 jmcneill dest = argv[1];
561 1.1 jmcneill
562 1.1 jmcneill kernel_fd = open(src, O_RDONLY);
563 1.1 jmcneill if (kernel_fd == -1) {
564 1.1 jmcneill perror("open kernel");
565 1.1 jmcneill return EXIT_FAILURE;
566 1.1 jmcneill }
567 1.11 matt image_fd = open(dest, O_WRONLY|O_CREAT|O_TRUNC, 0666);
568 1.1 jmcneill if (image_fd == -1) {
569 1.1 jmcneill perror("open image");
570 1.1 jmcneill return EXIT_FAILURE;
571 1.1 jmcneill }
572 1.1 jmcneill
573 1.23 jmcneill printf(" image type: %s\n", get_image_format_name(image_format));
574 1.23 jmcneill
575 1.23 jmcneill switch (image_format) {
576 1.23 jmcneill case FMT_UIMG:
577 1.23 jmcneill if (generate_header_uimg(&hdr_uimg, kernel_fd) != 0)
578 1.23 jmcneill return EXIT_FAILURE;
579 1.23 jmcneill
580 1.23 jmcneill if (write_image(&hdr_uimg, sizeof(hdr_uimg),
581 1.23 jmcneill kernel_fd, image_fd) != 0)
582 1.23 jmcneill return EXIT_FAILURE;
583 1.23 jmcneill
584 1.23 jmcneill break;
585 1.23 jmcneill case FMT_ARM64:
586 1.23 jmcneill if (generate_header_arm64(&hdr_arm64, kernel_fd) != 0)
587 1.23 jmcneill return EXIT_FAILURE;
588 1.23 jmcneill
589 1.23 jmcneill if (write_image(&hdr_arm64, sizeof(hdr_arm64),
590 1.23 jmcneill kernel_fd, image_fd) != 0)
591 1.23 jmcneill return EXIT_FAILURE;
592 1.1 jmcneill
593 1.23 jmcneill break;
594 1.23 jmcneill default:
595 1.23 jmcneill break;
596 1.23 jmcneill }
597 1.1 jmcneill
598 1.1 jmcneill close(image_fd);
599 1.1 jmcneill close(kernel_fd);
600 1.1 jmcneill
601 1.1 jmcneill return EXIT_SUCCESS;
602 1.1 jmcneill }
603