Home | History | Annotate | Line # | Download | only in mkubootimage
mkubootimage.c revision 1.21
      1 /* $NetBSD: mkubootimage.c,v 1.21 2017/09/29 21:18:28 jmcneill 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.21 2017/09/29 21:18:28 jmcneill Exp $");
     34 
     35 #include <sys/mman.h>
     36 #include <sys/stat.h>
     37 #include <sys/endian.h>
     38 #include <sys/uio.h>
     39 #include <err.h>
     40 #include <errno.h>
     41 #include <fcntl.h>
     42 #include <limits.h>
     43 #include <stdint.h>
     44 #include <stdio.h>
     45 #include <stdlib.h>
     46 #include <string.h>
     47 #include <time.h>
     48 #include <unistd.h>
     49 
     50 #include "uboot.h"
     51 
     52 #ifndef __arraycount
     53 #define __arraycount(__x)	(sizeof(__x) / sizeof(__x[0]))
     54 #endif
     55 
     56 extern uint32_t crc32(const void *, size_t);
     57 extern uint32_t crc32v(const struct iovec *, int);
     58 
     59 static enum uboot_image_os image_os = IH_OS_NETBSD;
     60 static enum uboot_image_arch image_arch = IH_ARCH_UNKNOWN;
     61 static enum uboot_image_type image_type = IH_TYPE_UNKNOWN;
     62 static enum uboot_image_comp image_comp = IH_COMP_NONE;
     63 static uint32_t image_loadaddr = 0;
     64 static uint32_t image_entrypoint = 0;
     65 static char *image_name;
     66 static uint32_t image_magic = IH_MAGIC;
     67 
     68 static const struct uboot_os {
     69 	enum uboot_image_os os;
     70 	const char *name;
     71 } uboot_os[] = {
     72 	{ IH_OS_OPENBSD,	"openbsd" },
     73 	{ IH_OS_NETBSD,		"netbsd" },
     74 	{ IH_OS_FREEBSD,	"freebsd" },
     75 	{ IH_OS_LINUX,		"linux" },
     76 };
     77 
     78 static enum uboot_image_os
     79 get_os(const char *name)
     80 {
     81 	unsigned int i;
     82 
     83 	for (i = 0; i < __arraycount(uboot_os); i++) {
     84 		if (strcmp(uboot_os[i].name, name) == 0)
     85 			return uboot_os[i].os;
     86 	}
     87 
     88 	return IH_OS_UNKNOWN;
     89 }
     90 
     91 static const char *
     92 get_os_name(enum uboot_image_os os)
     93 {
     94 	unsigned int i;
     95 
     96 	for (i = 0; i < __arraycount(uboot_os); i++) {
     97 		if (uboot_os[i].os == os)
     98 			return uboot_os[i].name;
     99 	}
    100 
    101 	return "Unknown";
    102 }
    103 
    104 static const struct uboot_arch {
    105 	enum uboot_image_arch arch;
    106 	const char *name;
    107 } uboot_arch[] = {
    108 	{ IH_ARCH_ARM,		"arm" },
    109 	{ IH_ARCH_ARM64,	"arm64" },
    110 	{ IH_ARCH_I386,		"i386" },
    111 	{ IH_ARCH_MIPS,		"mips" },
    112 	{ IH_ARCH_MIPS64,	"mips64" },
    113 	{ IH_ARCH_PPC,		"powerpc" },
    114 	{ IH_ARCH_OPENRISC,	"or1k" },
    115 	{ IH_ARCH_SH,		"sh" },
    116 };
    117 
    118 static enum uboot_image_arch
    119 get_arch(const char *name)
    120 {
    121 	unsigned int i;
    122 
    123 	for (i = 0; i < __arraycount(uboot_arch); i++) {
    124 		if (strcmp(uboot_arch[i].name, name) == 0)
    125 			return uboot_arch[i].arch;
    126 	}
    127 
    128 	return IH_ARCH_UNKNOWN;
    129 }
    130 
    131 static const char *
    132 get_arch_name(enum uboot_image_arch arch)
    133 {
    134 	unsigned int i;
    135 
    136 	for (i = 0; i < __arraycount(uboot_arch); i++) {
    137 		if (uboot_arch[i].arch == arch)
    138 			return uboot_arch[i].name;
    139 	}
    140 
    141 	return "Unknown";
    142 }
    143 
    144 static const struct uboot_type {
    145 	enum uboot_image_type type;
    146 	const char *name;
    147 } uboot_type[] = {
    148 	{ IH_TYPE_STANDALONE,		"standalone" },
    149 	{ IH_TYPE_KERNEL,		"kernel" },
    150 	{ IH_TYPE_KERNEL_NOLOAD,	"kernel_noload" },
    151 	{ IH_TYPE_RAMDISK,		"ramdisk" },
    152 	{ IH_TYPE_FILESYSTEM,		"fs" },
    153 	{ IH_TYPE_SCRIPT,		"script" },
    154 };
    155 
    156 static enum uboot_image_type
    157 get_type(const char *name)
    158 {
    159 	unsigned int i;
    160 
    161 	for (i = 0; i < __arraycount(uboot_type); i++) {
    162 		if (strcmp(uboot_type[i].name, name) == 0)
    163 			return uboot_type[i].type;
    164 	}
    165 
    166 	return IH_TYPE_UNKNOWN;
    167 }
    168 
    169 static const char *
    170 get_type_name(enum uboot_image_type type)
    171 {
    172 	unsigned int i;
    173 
    174 	for (i = 0; i < __arraycount(uboot_type); i++) {
    175 		if (uboot_type[i].type == type)
    176 			return uboot_type[i].name;
    177 	}
    178 
    179 	return "Unknown";
    180 }
    181 
    182 static const struct uboot_comp {
    183 	enum uboot_image_comp comp;
    184 	const char *name;
    185 } uboot_comp[] = {
    186 	{ IH_COMP_NONE,		"none" },
    187 	{ IH_COMP_GZIP,		"gz" },
    188 	{ IH_COMP_BZIP2,	"bz2" },
    189 	{ IH_COMP_LZMA,		"lzma" },
    190 	{ IH_COMP_LZO,		"lzo" },
    191 };
    192 
    193 static enum uboot_image_comp
    194 get_comp(const char *name)
    195 {
    196 	unsigned int i;
    197 
    198 	for (i = 0; i < __arraycount(uboot_comp); i++) {
    199 		if (strcmp(uboot_comp[i].name, name) == 0)
    200 			return uboot_comp[i].comp;
    201 	}
    202 
    203 	return IH_TYPE_UNKNOWN;
    204 }
    205 
    206 static const char *
    207 get_comp_name(enum uboot_image_comp comp)
    208 {
    209 	unsigned int i;
    210 
    211 	for (i = 0; i < __arraycount(uboot_comp); i++) {
    212 		if (uboot_comp[i].comp == comp)
    213 			return uboot_comp[i].name;
    214 	}
    215 
    216 	return "Unknown";
    217 }
    218 
    219 __dead static void
    220 usage(void)
    221 {
    222 	fprintf(stderr, "usage: mkubootimage -A "
    223 	    "<arm|arm64|i386|mips|mips64|or1k|powerpc|sh>");
    224 	fprintf(stderr, " -C <none|bz2|gz|lzma|lzo>");
    225 	fprintf(stderr, " -O <openbsd|netbsd|freebsd|linux>");
    226 	fprintf(stderr, " -T <standalone|kernel|kernel_noload|ramdisk|fs|script>");
    227 	fprintf(stderr, " -a <addr> [-e <ep>] [-m <magic>] -n <name>");
    228 	fprintf(stderr, " <srcfile> <dstfile>\n");
    229 
    230 	exit(EXIT_FAILURE);
    231 }
    232 
    233 static void
    234 dump_header(struct uboot_image_header *hdr)
    235 {
    236 	time_t tm = ntohl(hdr->ih_time);
    237 
    238 	printf(" magic:       0x%08x\n", ntohl(hdr->ih_magic));
    239 	printf(" time:        %s", ctime(&tm));
    240 	printf(" size:        %u\n", ntohl(hdr->ih_size));
    241 	printf(" load addr:   0x%08x\n", ntohl(hdr->ih_load));
    242 	printf(" entry point: 0x%08x\n", ntohl(hdr->ih_ep));
    243 	printf(" data crc:    0x%08x\n", ntohl(hdr->ih_dcrc));
    244 	printf(" os:          %d (%s)\n", hdr->ih_os,
    245 	    get_os_name(hdr->ih_os));
    246 	printf(" arch:        %d (%s)\n", hdr->ih_arch,
    247 	    get_arch_name(hdr->ih_arch));
    248 	printf(" type:        %d (%s)\n", hdr->ih_type,
    249 	    get_type_name(hdr->ih_type));
    250 	printf(" comp:        %d (%s)\n", hdr->ih_comp,
    251 	    get_comp_name(hdr->ih_comp));
    252 	printf(" name:        %s\n", hdr->ih_name);
    253 	printf(" header crc:  0x%08x\n", hdr->ih_hcrc);
    254 }
    255 
    256 static int
    257 generate_header(struct uboot_image_header *hdr, int kernel_fd)
    258 {
    259 	uint8_t *p;
    260 	struct stat st;
    261 	uint32_t crc, dsize, size_buf[2];
    262 	int error;
    263 
    264 	error = fstat(kernel_fd, &st);
    265 	if (error == -1) {
    266 		perror("stat");
    267 		return errno;
    268 	}
    269 
    270 	if (st.st_size + sizeof(*hdr) > UINT32_MAX) {
    271 		fprintf(stderr, "fatal: kernel too big\n");
    272 		return EINVAL;
    273 	}
    274 
    275 	p = mmap(0, st.st_size, PROT_READ, MAP_FILE|MAP_SHARED, kernel_fd, 0);
    276 	if (p == MAP_FAILED) {
    277 		perror("mmap kernel");
    278 		return EINVAL;
    279 	}
    280 	if (image_type == IH_TYPE_SCRIPT) {
    281 		struct iovec iov[3];
    282 		dsize = st.st_size + (sizeof(uint32_t) * 2);
    283 		size_buf[0] = htonl(st.st_size);
    284 		size_buf[1] = htonl(0);
    285 		iov[0].iov_base = &size_buf[0];
    286 		iov[0].iov_len = sizeof(size_buf[0]);
    287 		iov[1].iov_base = &size_buf[1];
    288 		iov[1].iov_len = sizeof(size_buf[1]);
    289 		iov[2].iov_base = p;
    290 		iov[2].iov_len = st.st_size;
    291 		crc = crc32v(iov, 3);
    292 	} else {
    293 		dsize = st.st_size;
    294 		crc = crc32(p, st.st_size);
    295 	}
    296 	munmap(p, st.st_size);
    297 
    298 	memset(hdr, 0, sizeof(*hdr));
    299 	hdr->ih_magic = htonl(image_magic);
    300 	hdr->ih_time = htonl(st.st_mtime);
    301 	hdr->ih_size = htonl(dsize);
    302 	hdr->ih_load = htonl(image_loadaddr);
    303 	hdr->ih_ep = htonl(image_entrypoint);
    304 	hdr->ih_dcrc = htonl(crc);
    305 	hdr->ih_os = image_os;
    306 	hdr->ih_arch = image_arch;
    307 	hdr->ih_type = image_type;
    308 	hdr->ih_comp = image_comp;
    309 	strlcpy((char *)hdr->ih_name, image_name, sizeof(hdr->ih_name));
    310 	crc = crc32((void *)hdr, sizeof(*hdr));
    311 	hdr->ih_hcrc = htonl(crc);
    312 
    313 	dump_header(hdr);
    314 
    315 	return 0;
    316 }
    317 
    318 static int
    319 write_image(struct uboot_image_header *hdr, int kernel_fd, int image_fd)
    320 {
    321 	uint8_t buf[4096];
    322 	ssize_t rlen, wlen;
    323 	struct stat st;
    324 	uint32_t size_buf[2];
    325 	int error;
    326 
    327 	error = fstat(kernel_fd, &st);
    328 	if (error == -1) {
    329 		perror("stat");
    330 		return errno;
    331 	}
    332 
    333 	wlen = write(image_fd, hdr, sizeof(*hdr));
    334 	if (wlen != sizeof(*hdr)) {
    335 		perror("short write");
    336 		return errno;
    337 	}
    338 
    339 	if (image_type == IH_TYPE_SCRIPT) {
    340 		size_buf[0] = htonl(st.st_size);
    341 		size_buf[1] = htonl(0);
    342 		wlen = write(image_fd, &size_buf, sizeof(size_buf));
    343 		if (wlen != sizeof(size_buf)) {
    344 			perror("short write");
    345 			return errno;
    346 		}
    347 	}
    348 
    349 	while ((rlen = read(kernel_fd, buf, sizeof(buf))) > 0) {
    350 		wlen = write(image_fd, buf, rlen);
    351 		if (wlen != rlen) {
    352 			perror("short write");
    353 			return errno;
    354 		}
    355 	}
    356 
    357 	return 0;
    358 }
    359 
    360 int
    361 main(int argc, char *argv[])
    362 {
    363 	struct uboot_image_header hdr;
    364 	const char *src, *dest;
    365 	char *ep;
    366 	int kernel_fd, image_fd;
    367 	int ch;
    368 	unsigned long long num;
    369 
    370 	while ((ch = getopt(argc, argv, "A:C:E:O:T:a:e:hm:n:")) != -1) {
    371 		switch (ch) {
    372 		case 'A':	/* arch */
    373 			image_arch = get_arch(optarg);
    374 			break;
    375 		case 'C':	/* comp */
    376 			image_comp = get_comp(optarg);
    377 			break;
    378 		case 'O':	/* os */
    379 			image_os = get_os(optarg);
    380 			break;
    381 		case 'T':	/* type */
    382 			image_type = get_type(optarg);
    383 			break;
    384 		case 'a':	/* addr */
    385 			errno = 0;
    386 			num = strtoull(optarg, &ep, 0);
    387 			if (*ep != '\0' || (errno == ERANGE &&
    388 			    (num == ULLONG_MAX || num == 0)) ||
    389 			    ((signed long long)num != (int32_t)num &&
    390 			     num != (uint32_t)num))
    391 				errx(1, "illegal number -- %s", optarg);
    392 			image_loadaddr = (uint32_t)num;
    393 			break;
    394 		case 'E':	/* ep (byte swapped) */
    395 		case 'e':	/* ep */
    396 			errno = 0;
    397 			num = strtoull(optarg, &ep, 0);
    398 			if (*ep != '\0' || (errno == ERANGE &&
    399 			    (num == ULLONG_MAX || num == 0)) ||
    400 			    ((signed long long)num != (int32_t)num &&
    401 			     num != (uint32_t)num))
    402 				errx(1, "illegal number -- %s", optarg);
    403 			image_entrypoint = (uint32_t)num;
    404 			if (ch == 'E')
    405 				image_entrypoint = bswap32(image_entrypoint);
    406 			break;
    407 		case 'm':	/* magic */
    408 			errno = 0;
    409 			num = strtoul(optarg, &ep, 0);
    410 			if (*ep != '\0' || (errno == ERANGE &&
    411 			    (num == ULONG_MAX || num == 0)))
    412 				errx(1, "illegal number -- %s", optarg);
    413 			image_magic = (uint32_t)num;
    414 		case 'n':	/* name */
    415 			image_name = strdup(optarg);
    416 			break;
    417 		case 'h':
    418 		default:
    419 			usage();
    420 			/* NOTREACHED */
    421 		}
    422 	}
    423 	argc -= optind;
    424 	argv += optind;
    425 
    426 	if (argc != 2)
    427 		usage();
    428 
    429 	if (image_entrypoint == 0)
    430 		image_entrypoint = image_loadaddr;
    431 
    432 	if (image_arch == IH_ARCH_UNKNOWN ||
    433 	    image_type == IH_TYPE_UNKNOWN ||
    434 	    image_name == NULL)
    435 		usage();
    436 
    437 	switch (image_type) {
    438 	case IH_TYPE_SCRIPT:
    439 	case IH_TYPE_RAMDISK:
    440 	case IH_TYPE_KERNEL_NOLOAD:
    441 		break;
    442 	default:
    443 		if (image_loadaddr == 0)
    444 			usage();
    445 			/* NOTREACHED */
    446 		break;
    447 	}
    448 
    449 	src = argv[0];
    450 	dest = argv[1];
    451 
    452 	kernel_fd = open(src, O_RDONLY);
    453 	if (kernel_fd == -1) {
    454 		perror("open kernel");
    455 		return EXIT_FAILURE;
    456 	}
    457 	image_fd = open(dest, O_WRONLY|O_CREAT|O_TRUNC, 0666);
    458 	if (image_fd == -1) {
    459 		perror("open image");
    460 		return EXIT_FAILURE;
    461 	}
    462 
    463 	if (generate_header(&hdr, kernel_fd) != 0)
    464 		return EXIT_FAILURE;
    465 
    466 	if (write_image(&hdr, kernel_fd, image_fd) != 0)
    467 		return EXIT_FAILURE;
    468 
    469 	close(image_fd);
    470 	close(kernel_fd);
    471 
    472 	return EXIT_SUCCESS;
    473 }
    474