Home | History | Annotate | Line # | Download | only in mkubootimage
mkubootimage.c revision 1.5
      1 /* $NetBSD: mkubootimage.c,v 1.5 2011/01/31 03:37:28 matt 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.5 2011/01/31 03:37:28 matt Exp $");
     34 
     35 #include <sys/mman.h>
     36 #include <sys/stat.h>
     37 #include <err.h>
     38 #include <errno.h>
     39 #include <fcntl.h>
     40 #include <limits.h>
     41 #include <stdint.h>
     42 #include <stdio.h>
     43 #include <stdlib.h>
     44 #include <string.h>
     45 #include <time.h>
     46 #include <unistd.h>
     47 
     48 #include "uboot.h"
     49 
     50 #ifndef __arraycount
     51 #define __arraycount(__x)	(sizeof(__x) / sizeof(__x[0]))
     52 #endif
     53 
     54 extern uint32_t crc32(const void *, size_t);
     55 
     56 static enum uboot_image_arch image_arch = IH_ARCH_UNKNOWN;
     57 static enum uboot_image_type image_type = IH_TYPE_UNKNOWN;
     58 static enum uboot_image_comp image_comp = IH_COMP_NONE;
     59 static uint32_t image_loadaddr = 0;
     60 static uint32_t image_entrypoint = 0;
     61 static char *image_name;
     62 
     63 struct uboot_arch {
     64 	enum uboot_image_arch arch;
     65 	const char *name;
     66 } uboot_arch[] = {
     67 	{ IH_ARCH_ARM,		"arm" },
     68 	{ IH_ARCH_MIPS,		"mips" },
     69 	{ IH_ARCH_MIPS64,	"mips64" },
     70 	{ IH_ARCH_PPC,		"powerpc" },
     71 };
     72 
     73 static enum uboot_image_arch
     74 get_arch(const char *name)
     75 {
     76 	unsigned int i;
     77 
     78 	for (i = 0; i < __arraycount(uboot_arch); i++) {
     79 		if (strcmp(uboot_arch[i].name, name) == 0)
     80 			return uboot_arch[i].arch;
     81 	}
     82 
     83 	return IH_ARCH_UNKNOWN;
     84 }
     85 
     86 struct uboot_type {
     87 	enum uboot_image_type type;
     88 	const char *name;
     89 } uboot_type[] = {
     90 	{ IH_TYPE_KERNEL,	"kernel" },
     91 	{ IH_TYPE_RAMDISK,	"ramdisk" },
     92 	{ IH_TYPE_FILESYSTEM,	"fs" },
     93 };
     94 
     95 static enum uboot_image_type
     96 get_type(const char *name)
     97 {
     98 	unsigned int i;
     99 
    100 	for (i = 0; i < __arraycount(uboot_type); i++) {
    101 		if (strcmp(uboot_type[i].name, name) == 0)
    102 			return uboot_type[i].type;
    103 	}
    104 
    105 	return IH_TYPE_UNKNOWN;
    106 }
    107 
    108 struct uboot_comp {
    109 	enum uboot_image_comp comp;
    110 	const char *name;
    111 } uboot_comp[] = {
    112 	{ IH_COMP_NONE,		"none" },
    113 	{ IH_COMP_GZIP,		"gz" },
    114 	{ IH_COMP_BZIP2,	"bz2" },
    115 };
    116 
    117 static enum uboot_image_comp
    118 get_comp(const char *name)
    119 {
    120 	unsigned int i;
    121 
    122 	for (i = 0; i < __arraycount(uboot_comp); i++) {
    123 		if (strcmp(uboot_comp[i].name, name) == 0)
    124 			return uboot_comp[i].comp;
    125 	}
    126 
    127 	return IH_TYPE_UNKNOWN;
    128 }
    129 
    130 static void
    131 usage(void)
    132 {
    133 	fprintf(stderr, "usage: mkubootimage -A <arm|mips|mips64|powerpc>");
    134 	fprintf(stderr, " -T <kernel|ramdisk|fs>");
    135 	fprintf(stderr, " -C <none|gz|bz2>");
    136 	fprintf(stderr, " -a <addr> [-e <ep>] -n <name>");
    137 	fprintf(stderr, " <srcfile> <dstfile>\n");
    138 
    139 	exit(EXIT_FAILURE);
    140 }
    141 
    142 static void
    143 dump_header(struct uboot_image_header *hdr)
    144 {
    145 	time_t tm = ntohl(hdr->ih_time);
    146 
    147 	printf(" magic:       0x%08x\n", ntohl(hdr->ih_magic));
    148 	printf(" time:        %s", ctime(&tm));
    149 	printf(" size:        %u\n", ntohl(hdr->ih_size));
    150 	printf(" load addr:   0x%08x\n", ntohl(hdr->ih_load));
    151 	printf(" entry point: 0x%08x\n", ntohl(hdr->ih_ep));
    152 	printf(" data crc:    0x%08x\n", ntohl(hdr->ih_dcrc));
    153 	printf(" os:          %d\n", hdr->ih_os);
    154 	printf(" arch:        %d\n", hdr->ih_arch);
    155 	printf(" type:        %d\n", hdr->ih_type);
    156 	printf(" comp:        %d\n", hdr->ih_comp);
    157 	printf(" name:        %s\n", hdr->ih_name);
    158 	printf(" header crc:  0x%08x\n", hdr->ih_hcrc);
    159 }
    160 
    161 static int
    162 generate_header(struct uboot_image_header *hdr, int kernel_fd)
    163 {
    164 	uint8_t *p;
    165 	struct stat st;
    166 	uint32_t crc;
    167 	int error;
    168 
    169 	error = fstat(kernel_fd, &st);
    170 	if (error == -1) {
    171 		perror("stat");
    172 		return errno;
    173 	}
    174 
    175 	if (st.st_size + sizeof(*hdr) > UINT32_MAX) {
    176 		fprintf(stderr, "fatal: kernel too big\n");
    177 		return EINVAL;
    178 	}
    179 
    180 	p = mmap(0, st.st_size, PROT_READ, MAP_FILE|MAP_SHARED, kernel_fd, 0);
    181 	if (p == MAP_FAILED) {
    182 		perror("mmap kernel");
    183 		return EINVAL;
    184 	}
    185 	crc = crc32(p, st.st_size);
    186 	munmap(p, st.st_size);
    187 
    188 	memset(hdr, 0, sizeof(*hdr));
    189 	hdr->ih_magic = htonl(IH_MAGIC);
    190 	hdr->ih_time = htonl(st.st_mtime);
    191 	hdr->ih_size = htonl(st.st_size);
    192 	hdr->ih_load = htonl(image_loadaddr);
    193 	hdr->ih_ep = htonl(image_entrypoint);
    194 	hdr->ih_dcrc = htonl(crc);
    195 	hdr->ih_os = IH_OS_NETBSD;
    196 	hdr->ih_arch = image_arch;
    197 	hdr->ih_type = image_type;
    198 	hdr->ih_comp = image_comp;
    199 	strncpy((char *)hdr->ih_name, image_name, sizeof(hdr->ih_name));
    200 	crc = crc32((void *)hdr, sizeof(*hdr));
    201 	hdr->ih_hcrc = htonl(crc);
    202 
    203 	dump_header(hdr);
    204 
    205 	return 0;
    206 }
    207 
    208 static int
    209 write_image(struct uboot_image_header *hdr, int kernel_fd, int image_fd)
    210 {
    211 	uint8_t buf[4096];
    212 	ssize_t rlen, wlen;
    213 
    214 	wlen = write(image_fd, hdr, sizeof(*hdr));
    215 	if (wlen != sizeof(*hdr)) {
    216 		perror("short write");
    217 		return errno;
    218 	}
    219 
    220 	while ((rlen = read(kernel_fd, buf, sizeof(buf))) > 0) {
    221 		wlen = write(image_fd, buf, rlen);
    222 		if (wlen != rlen) {
    223 			perror("short write");
    224 			return errno;
    225 		}
    226 	}
    227 
    228 	return 0;
    229 }
    230 
    231 int
    232 main(int argc, char *argv[])
    233 {
    234 	struct uboot_image_header hdr;
    235 	const char *src, *dest;
    236 	char *ep;
    237 	int kernel_fd, image_fd;
    238 	int ch;
    239 	unsigned long num;
    240 
    241 	while ((ch = getopt(argc, argv, "A:C:T:a:e:hn:")) != -1) {
    242 		switch (ch) {
    243 		case 'A':	/* arch */
    244 			image_arch = get_arch(optarg);
    245 			break;
    246 		case 'C':	/* comp */
    247 			image_comp = get_comp(optarg);
    248 			break;
    249 		case 'T':	/* type */
    250 			image_type = get_type(optarg);
    251 			break;
    252 		case 'a':	/* addr */
    253 			errno = 0;
    254 			num = strtoul(optarg, &ep, 0);
    255 			if (*ep != '\0' || (errno == ERANGE &&
    256 			    (num == ULONG_MAX || num == 0)))
    257 				errx(1, "illegal number -- %s", optarg);
    258 			image_loadaddr = (uint32_t)num;
    259 			break;
    260 		case 'e':	/* ep */
    261 			errno = 0;
    262 			num = strtoul(optarg, &ep, 0);
    263 			if (*ep != '\0' || (errno == ERANGE &&
    264 			    (num == ULONG_MAX || num == 0)))
    265 				errx(1, "illegal number -- %s", optarg);
    266 			image_entrypoint = (uint32_t)num;
    267 			break;
    268 		case 'n':	/* name */
    269 			image_name = strdup(optarg);
    270 			break;
    271 		case 'h':
    272 		default:
    273 			usage();
    274 			/* NOTREACHED */
    275 		}
    276 	}
    277 	argc -= optind;
    278 	argv += optind;
    279 
    280 	if (argc != 2)
    281 		usage();
    282 
    283 	if (image_entrypoint == 0)
    284 		image_entrypoint = image_loadaddr;
    285 
    286 	if (image_arch == IH_ARCH_UNKNOWN ||
    287 	    image_type == IH_TYPE_UNKNOWN ||
    288 	    image_loadaddr == 0 ||
    289 	    image_name == NULL)
    290 		usage();
    291 
    292 	src = argv[0];
    293 	dest = argv[1];
    294 
    295 	kernel_fd = open(src, O_RDONLY);
    296 	if (kernel_fd == -1) {
    297 		perror("open kernel");
    298 		return EXIT_FAILURE;
    299 	}
    300 	image_fd = open(dest, O_WRONLY|O_CREAT, 0666);
    301 	if (image_fd == -1) {
    302 		perror("open image");
    303 		return EXIT_FAILURE;
    304 	}
    305 
    306 	if (generate_header(&hdr, kernel_fd) != 0)
    307 		return EXIT_FAILURE;
    308 
    309 	if (write_image(&hdr, kernel_fd, image_fd) != 0)
    310 		return EXIT_FAILURE;
    311 
    312 	close(image_fd);
    313 	close(kernel_fd);
    314 
    315 	return EXIT_SUCCESS;
    316 }
    317