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