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