Home | History | Annotate | Line # | Download | only in mkubootimage
mkubootimage.c revision 1.6.2.2
      1  1.6.2.2  matt /* $NetBSD: mkubootimage.c,v 1.6.2.2 2011/05/20 15:05:11 matt Exp $ */
      2  1.6.2.2  matt 
      3  1.6.2.2  matt /*-
      4  1.6.2.2  matt  * Copyright (c) 2010 Jared D. McNeill <jmcneill (at) invisible.ca>
      5  1.6.2.2  matt  * All rights reserved.
      6  1.6.2.2  matt  *
      7  1.6.2.2  matt  * Redistribution and use in source and binary forms, with or without
      8  1.6.2.2  matt  * modification, are permitted provided that the following conditions
      9  1.6.2.2  matt  * are met:
     10  1.6.2.2  matt  * 1. Redistributions of source code must retain the above copyright
     11  1.6.2.2  matt  *    notice, this list of conditions and the following disclaimer.
     12  1.6.2.2  matt  * 2. The name of the author may not be used to endorse or promote products
     13  1.6.2.2  matt  *    derived from this software without specific prior written permission.
     14  1.6.2.2  matt  *
     15  1.6.2.2  matt  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16  1.6.2.2  matt  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     17  1.6.2.2  matt  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     18  1.6.2.2  matt  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     19  1.6.2.2  matt  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     20  1.6.2.2  matt  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     21  1.6.2.2  matt  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     22  1.6.2.2  matt  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     23  1.6.2.2  matt  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24  1.6.2.2  matt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25  1.6.2.2  matt  * SUCH DAMAGE.
     26  1.6.2.2  matt  */
     27  1.6.2.2  matt 
     28  1.6.2.2  matt #if HAVE_NBTOOL_CONFIG_H
     29  1.6.2.2  matt #include "nbtool_config.h"
     30  1.6.2.2  matt #endif
     31  1.6.2.2  matt 
     32  1.6.2.2  matt #include <sys/cdefs.h>
     33  1.6.2.2  matt __RCSID("$NetBSD: mkubootimage.c,v 1.6.2.2 2011/05/20 15:05:11 matt Exp $");
     34  1.6.2.2  matt 
     35  1.6.2.2  matt #include <sys/mman.h>
     36  1.6.2.2  matt #include <sys/stat.h>
     37  1.6.2.2  matt #include <err.h>
     38  1.6.2.2  matt #include <errno.h>
     39  1.6.2.2  matt #include <fcntl.h>
     40  1.6.2.2  matt #include <limits.h>
     41  1.6.2.2  matt #include <stdint.h>
     42  1.6.2.2  matt #include <stdio.h>
     43  1.6.2.2  matt #include <stdlib.h>
     44  1.6.2.2  matt #include <string.h>
     45  1.6.2.2  matt #include <time.h>
     46  1.6.2.2  matt #include <unistd.h>
     47  1.6.2.2  matt 
     48  1.6.2.2  matt #include "uboot.h"
     49  1.6.2.2  matt 
     50  1.6.2.2  matt #ifndef __arraycount
     51  1.6.2.2  matt #define __arraycount(__x)	(sizeof(__x) / sizeof(__x[0]))
     52  1.6.2.2  matt #endif
     53  1.6.2.2  matt 
     54  1.6.2.2  matt extern uint32_t crc32(const void *, size_t);
     55  1.6.2.2  matt 
     56  1.6.2.2  matt static enum uboot_image_os image_os = IH_OS_NETBSD;
     57  1.6.2.2  matt static enum uboot_image_arch image_arch = IH_ARCH_UNKNOWN;
     58  1.6.2.2  matt static enum uboot_image_type image_type = IH_TYPE_UNKNOWN;
     59  1.6.2.2  matt static enum uboot_image_comp image_comp = IH_COMP_NONE;
     60  1.6.2.2  matt static uint32_t image_loadaddr = 0;
     61  1.6.2.2  matt static uint32_t image_entrypoint = 0;
     62  1.6.2.2  matt static char *image_name;
     63  1.6.2.2  matt 
     64  1.6.2.2  matt struct uboot_os {
     65  1.6.2.2  matt 	enum uboot_image_os os;
     66  1.6.2.2  matt 	const char *name;
     67  1.6.2.2  matt } uboot_os[] = {
     68  1.6.2.2  matt 	{ IH_OS_OPENBSD,	"openbsd" },
     69  1.6.2.2  matt 	{ IH_OS_NETBSD,		"netbsd" },
     70  1.6.2.2  matt 	{ IH_OS_FREEBSD,	"freebsd" },
     71  1.6.2.2  matt 	{ IH_OS_LINUX,		"linux" },
     72  1.6.2.2  matt };
     73  1.6.2.2  matt 
     74  1.6.2.2  matt static enum uboot_image_os
     75  1.6.2.2  matt get_os(const char *name)
     76  1.6.2.2  matt {
     77  1.6.2.2  matt 	unsigned int i;
     78  1.6.2.2  matt 
     79  1.6.2.2  matt 	for (i = 0; i < __arraycount(uboot_os); i++) {
     80  1.6.2.2  matt 		if (strcmp(uboot_os[i].name, name) == 0)
     81  1.6.2.2  matt 			return uboot_os[i].os;
     82  1.6.2.2  matt 	}
     83  1.6.2.2  matt 
     84  1.6.2.2  matt 	return IH_OS_UNKNOWN;
     85  1.6.2.2  matt }
     86  1.6.2.2  matt 
     87  1.6.2.2  matt struct uboot_arch {
     88  1.6.2.2  matt 	enum uboot_image_arch arch;
     89  1.6.2.2  matt 	const char *name;
     90  1.6.2.2  matt } uboot_arch[] = {
     91  1.6.2.2  matt 	{ IH_ARCH_ARM,		"arm" },
     92  1.6.2.2  matt 	{ IH_ARCH_MIPS,		"mips" },
     93  1.6.2.2  matt 	{ IH_ARCH_MIPS64,	"mips64" },
     94  1.6.2.2  matt 	{ IH_ARCH_PPC,		"powerpc" },
     95  1.6.2.2  matt };
     96  1.6.2.2  matt 
     97  1.6.2.2  matt static enum uboot_image_arch
     98  1.6.2.2  matt get_arch(const char *name)
     99  1.6.2.2  matt {
    100  1.6.2.2  matt 	unsigned int i;
    101  1.6.2.2  matt 
    102  1.6.2.2  matt 	for (i = 0; i < __arraycount(uboot_arch); i++) {
    103  1.6.2.2  matt 		if (strcmp(uboot_arch[i].name, name) == 0)
    104  1.6.2.2  matt 			return uboot_arch[i].arch;
    105  1.6.2.2  matt 	}
    106  1.6.2.2  matt 
    107  1.6.2.2  matt 	return IH_ARCH_UNKNOWN;
    108  1.6.2.2  matt }
    109  1.6.2.2  matt 
    110  1.6.2.2  matt struct uboot_type {
    111  1.6.2.2  matt 	enum uboot_image_type type;
    112  1.6.2.2  matt 	const char *name;
    113  1.6.2.2  matt } uboot_type[] = {
    114  1.6.2.2  matt 	{ IH_TYPE_STANDALONE,	"standalone" },
    115  1.6.2.2  matt 	{ IH_TYPE_KERNEL,	"kernel" },
    116  1.6.2.2  matt 	{ IH_TYPE_RAMDISK,	"ramdisk" },
    117  1.6.2.2  matt 	{ IH_TYPE_FILESYSTEM,	"fs" },
    118  1.6.2.2  matt };
    119  1.6.2.2  matt 
    120  1.6.2.2  matt static enum uboot_image_type
    121  1.6.2.2  matt get_type(const char *name)
    122  1.6.2.2  matt {
    123  1.6.2.2  matt 	unsigned int i;
    124  1.6.2.2  matt 
    125  1.6.2.2  matt 	for (i = 0; i < __arraycount(uboot_type); i++) {
    126  1.6.2.2  matt 		if (strcmp(uboot_type[i].name, name) == 0)
    127  1.6.2.2  matt 			return uboot_type[i].type;
    128  1.6.2.2  matt 	}
    129  1.6.2.2  matt 
    130  1.6.2.2  matt 	return IH_TYPE_UNKNOWN;
    131  1.6.2.2  matt }
    132  1.6.2.2  matt 
    133  1.6.2.2  matt struct uboot_comp {
    134  1.6.2.2  matt 	enum uboot_image_comp comp;
    135  1.6.2.2  matt 	const char *name;
    136  1.6.2.2  matt } uboot_comp[] = {
    137  1.6.2.2  matt 	{ IH_COMP_NONE,		"none" },
    138  1.6.2.2  matt 	{ IH_COMP_GZIP,		"gz" },
    139  1.6.2.2  matt 	{ IH_COMP_BZIP2,	"bz2" },
    140  1.6.2.2  matt };
    141  1.6.2.2  matt 
    142  1.6.2.2  matt static enum uboot_image_comp
    143  1.6.2.2  matt get_comp(const char *name)
    144  1.6.2.2  matt {
    145  1.6.2.2  matt 	unsigned int i;
    146  1.6.2.2  matt 
    147  1.6.2.2  matt 	for (i = 0; i < __arraycount(uboot_comp); i++) {
    148  1.6.2.2  matt 		if (strcmp(uboot_comp[i].name, name) == 0)
    149  1.6.2.2  matt 			return uboot_comp[i].comp;
    150  1.6.2.2  matt 	}
    151  1.6.2.2  matt 
    152  1.6.2.2  matt 	return IH_TYPE_UNKNOWN;
    153  1.6.2.2  matt }
    154  1.6.2.2  matt 
    155  1.6.2.2  matt static void
    156  1.6.2.2  matt usage(void)
    157  1.6.2.2  matt {
    158  1.6.2.2  matt 	fprintf(stderr, "usage: mkubootimage -A <arm|mips|mips64|powerpc>");
    159  1.6.2.2  matt 	fprintf(stderr, " -C <none|gz|bz2>");
    160  1.6.2.2  matt 	fprintf(stderr, " -O <openbsd|netbsd|freebsd|linux>");
    161  1.6.2.2  matt 	fprintf(stderr, " -T <standalone|kernel|ramdisk|fs>");
    162  1.6.2.2  matt 	fprintf(stderr, " -a <addr> [-e <ep>] -n <name>");
    163  1.6.2.2  matt 	fprintf(stderr, " <srcfile> <dstfile>\n");
    164  1.6.2.2  matt 
    165  1.6.2.2  matt 	exit(EXIT_FAILURE);
    166  1.6.2.2  matt }
    167  1.6.2.2  matt 
    168  1.6.2.2  matt static void
    169  1.6.2.2  matt dump_header(struct uboot_image_header *hdr)
    170  1.6.2.2  matt {
    171  1.6.2.2  matt 	time_t tm = ntohl(hdr->ih_time);
    172  1.6.2.2  matt 
    173  1.6.2.2  matt 	printf(" magic:       0x%08x\n", ntohl(hdr->ih_magic));
    174  1.6.2.2  matt 	printf(" time:        %s", ctime(&tm));
    175  1.6.2.2  matt 	printf(" size:        %u\n", ntohl(hdr->ih_size));
    176  1.6.2.2  matt 	printf(" load addr:   0x%08x\n", ntohl(hdr->ih_load));
    177  1.6.2.2  matt 	printf(" entry point: 0x%08x\n", ntohl(hdr->ih_ep));
    178  1.6.2.2  matt 	printf(" data crc:    0x%08x\n", ntohl(hdr->ih_dcrc));
    179  1.6.2.2  matt 	printf(" os:          %d\n", hdr->ih_os);
    180  1.6.2.2  matt 	printf(" arch:        %d\n", hdr->ih_arch);
    181  1.6.2.2  matt 	printf(" type:        %d\n", hdr->ih_type);
    182  1.6.2.2  matt 	printf(" comp:        %d\n", hdr->ih_comp);
    183  1.6.2.2  matt 	printf(" name:        %s\n", hdr->ih_name);
    184  1.6.2.2  matt 	printf(" header crc:  0x%08x\n", hdr->ih_hcrc);
    185  1.6.2.2  matt }
    186  1.6.2.2  matt 
    187  1.6.2.2  matt static int
    188  1.6.2.2  matt generate_header(struct uboot_image_header *hdr, int kernel_fd)
    189  1.6.2.2  matt {
    190  1.6.2.2  matt 	uint8_t *p;
    191  1.6.2.2  matt 	struct stat st;
    192  1.6.2.2  matt 	uint32_t crc;
    193  1.6.2.2  matt 	int error;
    194  1.6.2.2  matt 
    195  1.6.2.2  matt 	error = fstat(kernel_fd, &st);
    196  1.6.2.2  matt 	if (error == -1) {
    197  1.6.2.2  matt 		perror("stat");
    198  1.6.2.2  matt 		return errno;
    199  1.6.2.2  matt 	}
    200  1.6.2.2  matt 
    201  1.6.2.2  matt 	if (st.st_size + sizeof(*hdr) > UINT32_MAX) {
    202  1.6.2.2  matt 		fprintf(stderr, "fatal: kernel too big\n");
    203  1.6.2.2  matt 		return EINVAL;
    204  1.6.2.2  matt 	}
    205  1.6.2.2  matt 
    206  1.6.2.2  matt 	p = mmap(0, st.st_size, PROT_READ, MAP_FILE|MAP_SHARED, kernel_fd, 0);
    207  1.6.2.2  matt 	if (p == MAP_FAILED) {
    208  1.6.2.2  matt 		perror("mmap kernel");
    209  1.6.2.2  matt 		return EINVAL;
    210  1.6.2.2  matt 	}
    211  1.6.2.2  matt 	crc = crc32(p, st.st_size);
    212  1.6.2.2  matt 	munmap(p, st.st_size);
    213  1.6.2.2  matt 
    214  1.6.2.2  matt 	memset(hdr, 0, sizeof(*hdr));
    215  1.6.2.2  matt 	hdr->ih_magic = htonl(IH_MAGIC);
    216  1.6.2.2  matt 	hdr->ih_time = htonl(st.st_mtime);
    217  1.6.2.2  matt 	hdr->ih_size = htonl(st.st_size);
    218  1.6.2.2  matt 	hdr->ih_load = htonl(image_loadaddr);
    219  1.6.2.2  matt 	hdr->ih_ep = htonl(image_entrypoint);
    220  1.6.2.2  matt 	hdr->ih_dcrc = htonl(crc);
    221  1.6.2.2  matt 	hdr->ih_os = image_os;
    222  1.6.2.2  matt 	hdr->ih_arch = image_arch;
    223  1.6.2.2  matt 	hdr->ih_type = image_type;
    224  1.6.2.2  matt 	hdr->ih_comp = image_comp;
    225  1.6.2.2  matt 	strncpy((char *)hdr->ih_name, image_name, sizeof(hdr->ih_name));
    226  1.6.2.2  matt 	crc = crc32((void *)hdr, sizeof(*hdr));
    227  1.6.2.2  matt 	hdr->ih_hcrc = htonl(crc);
    228  1.6.2.2  matt 
    229  1.6.2.2  matt 	dump_header(hdr);
    230  1.6.2.2  matt 
    231  1.6.2.2  matt 	return 0;
    232  1.6.2.2  matt }
    233  1.6.2.2  matt 
    234  1.6.2.2  matt static int
    235  1.6.2.2  matt write_image(struct uboot_image_header *hdr, int kernel_fd, int image_fd)
    236  1.6.2.2  matt {
    237  1.6.2.2  matt 	uint8_t buf[4096];
    238  1.6.2.2  matt 	ssize_t rlen, wlen;
    239  1.6.2.2  matt 
    240  1.6.2.2  matt 	wlen = write(image_fd, hdr, sizeof(*hdr));
    241  1.6.2.2  matt 	if (wlen != sizeof(*hdr)) {
    242  1.6.2.2  matt 		perror("short write");
    243  1.6.2.2  matt 		return errno;
    244  1.6.2.2  matt 	}
    245  1.6.2.2  matt 
    246  1.6.2.2  matt 	while ((rlen = read(kernel_fd, buf, sizeof(buf))) > 0) {
    247  1.6.2.2  matt 		wlen = write(image_fd, buf, rlen);
    248  1.6.2.2  matt 		if (wlen != rlen) {
    249  1.6.2.2  matt 			perror("short write");
    250  1.6.2.2  matt 			return errno;
    251  1.6.2.2  matt 		}
    252  1.6.2.2  matt 	}
    253  1.6.2.2  matt 
    254  1.6.2.2  matt 	return 0;
    255  1.6.2.2  matt }
    256  1.6.2.2  matt 
    257  1.6.2.2  matt int
    258  1.6.2.2  matt main(int argc, char *argv[])
    259  1.6.2.2  matt {
    260  1.6.2.2  matt 	struct uboot_image_header hdr;
    261  1.6.2.2  matt 	const char *src, *dest;
    262  1.6.2.2  matt 	char *ep;
    263  1.6.2.2  matt 	int kernel_fd, image_fd;
    264  1.6.2.2  matt 	int ch;
    265  1.6.2.2  matt 	unsigned long num;
    266  1.6.2.2  matt 
    267  1.6.2.2  matt 	while ((ch = getopt(argc, argv, "A:C:O:T:a:e:hn:")) != -1) {
    268  1.6.2.2  matt 		switch (ch) {
    269  1.6.2.2  matt 		case 'A':	/* arch */
    270  1.6.2.2  matt 			image_arch = get_arch(optarg);
    271  1.6.2.2  matt 			break;
    272  1.6.2.2  matt 		case 'C':	/* comp */
    273  1.6.2.2  matt 			image_comp = get_comp(optarg);
    274  1.6.2.2  matt 			break;
    275  1.6.2.2  matt 		case 'O':	/* os */
    276  1.6.2.2  matt 			image_os = get_os(optarg);
    277  1.6.2.2  matt 			break;
    278  1.6.2.2  matt 		case 'T':	/* type */
    279  1.6.2.2  matt 			image_type = get_type(optarg);
    280  1.6.2.2  matt 			break;
    281  1.6.2.2  matt 		case 'a':	/* addr */
    282  1.6.2.2  matt 			errno = 0;
    283  1.6.2.2  matt 			num = strtoul(optarg, &ep, 0);
    284  1.6.2.2  matt 			if (*ep != '\0' || (errno == ERANGE &&
    285  1.6.2.2  matt 			    (num == ULONG_MAX || num == 0)))
    286  1.6.2.2  matt 				errx(1, "illegal number -- %s", optarg);
    287  1.6.2.2  matt 			image_loadaddr = (uint32_t)num;
    288  1.6.2.2  matt 			break;
    289  1.6.2.2  matt 		case 'e':	/* ep */
    290  1.6.2.2  matt 			errno = 0;
    291  1.6.2.2  matt 			num = strtoul(optarg, &ep, 0);
    292  1.6.2.2  matt 			if (*ep != '\0' || (errno == ERANGE &&
    293  1.6.2.2  matt 			    (num == ULONG_MAX || num == 0)))
    294  1.6.2.2  matt 				errx(1, "illegal number -- %s", optarg);
    295  1.6.2.2  matt 			image_entrypoint = (uint32_t)num;
    296  1.6.2.2  matt 			break;
    297  1.6.2.2  matt 		case 'n':	/* name */
    298  1.6.2.2  matt 			image_name = strdup(optarg);
    299  1.6.2.2  matt 			break;
    300  1.6.2.2  matt 		case 'h':
    301  1.6.2.2  matt 		default:
    302  1.6.2.2  matt 			usage();
    303  1.6.2.2  matt 			/* NOTREACHED */
    304  1.6.2.2  matt 		}
    305  1.6.2.2  matt 	}
    306  1.6.2.2  matt 	argc -= optind;
    307  1.6.2.2  matt 	argv += optind;
    308  1.6.2.2  matt 
    309  1.6.2.2  matt 	if (argc != 2)
    310  1.6.2.2  matt 		usage();
    311  1.6.2.2  matt 
    312  1.6.2.2  matt 	if (image_entrypoint == 0)
    313  1.6.2.2  matt 		image_entrypoint = image_loadaddr;
    314  1.6.2.2  matt 
    315  1.6.2.2  matt 	if (image_arch == IH_ARCH_UNKNOWN ||
    316  1.6.2.2  matt 	    image_type == IH_TYPE_UNKNOWN ||
    317  1.6.2.2  matt 	    image_loadaddr == 0 ||
    318  1.6.2.2  matt 	    image_name == NULL)
    319  1.6.2.2  matt 		usage();
    320  1.6.2.2  matt 
    321  1.6.2.2  matt 	src = argv[0];
    322  1.6.2.2  matt 	dest = argv[1];
    323  1.6.2.2  matt 
    324  1.6.2.2  matt 	kernel_fd = open(src, O_RDONLY);
    325  1.6.2.2  matt 	if (kernel_fd == -1) {
    326  1.6.2.2  matt 		perror("open kernel");
    327  1.6.2.2  matt 		return EXIT_FAILURE;
    328  1.6.2.2  matt 	}
    329  1.6.2.2  matt 	image_fd = open(dest, O_WRONLY|O_CREAT, 0666);
    330  1.6.2.2  matt 	if (image_fd == -1) {
    331  1.6.2.2  matt 		perror("open image");
    332  1.6.2.2  matt 		return EXIT_FAILURE;
    333  1.6.2.2  matt 	}
    334  1.6.2.2  matt 
    335  1.6.2.2  matt 	if (generate_header(&hdr, kernel_fd) != 0)
    336  1.6.2.2  matt 		return EXIT_FAILURE;
    337  1.6.2.2  matt 
    338  1.6.2.2  matt 	if (write_image(&hdr, kernel_fd, image_fd) != 0)
    339  1.6.2.2  matt 		return EXIT_FAILURE;
    340  1.6.2.2  matt 
    341  1.6.2.2  matt 	close(image_fd);
    342  1.6.2.2  matt 	close(kernel_fd);
    343  1.6.2.2  matt 
    344  1.6.2.2  matt 	return EXIT_SUCCESS;
    345  1.6.2.2  matt }
    346