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