Home | History | Annotate | Line # | Download | only in mkubootimage
mkubootimage.c revision 1.22
      1  1.22  jmcneill /* $NetBSD: mkubootimage.c,v 1.22 2017/11/05 11:07:32 jmcneill Exp $ */
      2   1.1  jmcneill 
      3   1.1  jmcneill /*-
      4   1.1  jmcneill  * Copyright (c) 2010 Jared D. McNeill <jmcneill (at) invisible.ca>
      5   1.1  jmcneill  * All rights reserved.
      6   1.1  jmcneill  *
      7   1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8   1.1  jmcneill  * modification, are permitted provided that the following conditions
      9   1.1  jmcneill  * are met:
     10   1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11   1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     12   1.1  jmcneill  * 2. The name of the author may not be used to endorse or promote products
     13   1.1  jmcneill  *    derived from this software without specific prior written permission.
     14   1.1  jmcneill  *
     15   1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16   1.1  jmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     17   1.1  jmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     18   1.1  jmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     19   1.1  jmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     20   1.1  jmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     21   1.1  jmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     22   1.1  jmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     23   1.1  jmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24   1.1  jmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25   1.1  jmcneill  * SUCH DAMAGE.
     26   1.1  jmcneill  */
     27   1.1  jmcneill 
     28   1.2    dogcow #if HAVE_NBTOOL_CONFIG_H
     29   1.2    dogcow #include "nbtool_config.h"
     30   1.2    dogcow #endif
     31   1.2    dogcow 
     32   1.1  jmcneill #include <sys/cdefs.h>
     33  1.22  jmcneill __RCSID("$NetBSD: mkubootimage.c,v 1.22 2017/11/05 11:07:32 jmcneill Exp $");
     34   1.1  jmcneill 
     35   1.1  jmcneill #include <sys/mman.h>
     36   1.1  jmcneill #include <sys/stat.h>
     37   1.9      matt #include <sys/endian.h>
     38  1.17  jmcneill #include <sys/uio.h>
     39   1.1  jmcneill #include <err.h>
     40   1.1  jmcneill #include <errno.h>
     41   1.1  jmcneill #include <fcntl.h>
     42   1.1  jmcneill #include <limits.h>
     43   1.1  jmcneill #include <stdint.h>
     44   1.1  jmcneill #include <stdio.h>
     45   1.1  jmcneill #include <stdlib.h>
     46   1.1  jmcneill #include <string.h>
     47   1.1  jmcneill #include <time.h>
     48   1.1  jmcneill #include <unistd.h>
     49   1.1  jmcneill 
     50   1.1  jmcneill #include "uboot.h"
     51   1.1  jmcneill 
     52   1.1  jmcneill #ifndef __arraycount
     53   1.1  jmcneill #define __arraycount(__x)	(sizeof(__x) / sizeof(__x[0]))
     54   1.1  jmcneill #endif
     55   1.1  jmcneill 
     56   1.1  jmcneill extern uint32_t crc32(const void *, size_t);
     57  1.17  jmcneill extern uint32_t crc32v(const struct iovec *, int);
     58   1.1  jmcneill 
     59   1.6       phx static enum uboot_image_os image_os = IH_OS_NETBSD;
     60   1.1  jmcneill static enum uboot_image_arch image_arch = IH_ARCH_UNKNOWN;
     61   1.1  jmcneill static enum uboot_image_type image_type = IH_TYPE_UNKNOWN;
     62   1.1  jmcneill static enum uboot_image_comp image_comp = IH_COMP_NONE;
     63   1.1  jmcneill static uint32_t image_loadaddr = 0;
     64   1.1  jmcneill static uint32_t image_entrypoint = 0;
     65   1.1  jmcneill static char *image_name;
     66   1.8       riz static uint32_t image_magic = IH_MAGIC;
     67   1.1  jmcneill 
     68  1.14     joerg static const struct uboot_os {
     69   1.6       phx 	enum uboot_image_os os;
     70   1.6       phx 	const char *name;
     71   1.6       phx } uboot_os[] = {
     72   1.6       phx 	{ IH_OS_OPENBSD,	"openbsd" },
     73   1.6       phx 	{ IH_OS_NETBSD,		"netbsd" },
     74   1.6       phx 	{ IH_OS_FREEBSD,	"freebsd" },
     75   1.6       phx 	{ IH_OS_LINUX,		"linux" },
     76   1.6       phx };
     77   1.6       phx 
     78   1.6       phx static enum uboot_image_os
     79   1.6       phx get_os(const char *name)
     80   1.6       phx {
     81   1.6       phx 	unsigned int i;
     82   1.6       phx 
     83   1.6       phx 	for (i = 0; i < __arraycount(uboot_os); i++) {
     84   1.6       phx 		if (strcmp(uboot_os[i].name, name) == 0)
     85   1.6       phx 			return uboot_os[i].os;
     86   1.6       phx 	}
     87   1.6       phx 
     88   1.6       phx 	return IH_OS_UNKNOWN;
     89   1.6       phx }
     90   1.6       phx 
     91   1.7      matt static const char *
     92   1.7      matt get_os_name(enum uboot_image_os os)
     93   1.7      matt {
     94   1.7      matt 	unsigned int i;
     95   1.7      matt 
     96   1.7      matt 	for (i = 0; i < __arraycount(uboot_os); i++) {
     97   1.7      matt 		if (uboot_os[i].os == os)
     98   1.7      matt 			return uboot_os[i].name;
     99   1.7      matt 	}
    100   1.7      matt 
    101   1.7      matt 	return "Unknown";
    102   1.7      matt }
    103   1.7      matt 
    104  1.13     joerg static const struct uboot_arch {
    105   1.1  jmcneill 	enum uboot_image_arch arch;
    106   1.1  jmcneill 	const char *name;
    107   1.1  jmcneill } uboot_arch[] = {
    108   1.1  jmcneill 	{ IH_ARCH_ARM,		"arm" },
    109  1.21  jmcneill 	{ IH_ARCH_ARM64,	"arm64" },
    110  1.18   msaitoh 	{ IH_ARCH_I386,		"i386" },
    111   1.5      matt 	{ IH_ARCH_MIPS,		"mips" },
    112   1.5      matt 	{ IH_ARCH_MIPS64,	"mips64" },
    113   1.1  jmcneill 	{ IH_ARCH_PPC,		"powerpc" },
    114  1.18   msaitoh 	{ IH_ARCH_OPENRISC,	"or1k" },
    115  1.21  jmcneill 	{ IH_ARCH_SH,		"sh" },
    116   1.1  jmcneill };
    117   1.1  jmcneill 
    118   1.1  jmcneill static enum uboot_image_arch
    119   1.1  jmcneill get_arch(const char *name)
    120   1.1  jmcneill {
    121   1.1  jmcneill 	unsigned int i;
    122   1.1  jmcneill 
    123   1.1  jmcneill 	for (i = 0; i < __arraycount(uboot_arch); i++) {
    124   1.1  jmcneill 		if (strcmp(uboot_arch[i].name, name) == 0)
    125   1.1  jmcneill 			return uboot_arch[i].arch;
    126   1.1  jmcneill 	}
    127   1.1  jmcneill 
    128   1.1  jmcneill 	return IH_ARCH_UNKNOWN;
    129   1.1  jmcneill }
    130   1.1  jmcneill 
    131   1.7      matt static const char *
    132   1.7      matt get_arch_name(enum uboot_image_arch arch)
    133   1.7      matt {
    134   1.7      matt 	unsigned int i;
    135   1.7      matt 
    136   1.7      matt 	for (i = 0; i < __arraycount(uboot_arch); i++) {
    137   1.7      matt 		if (uboot_arch[i].arch == arch)
    138   1.7      matt 			return uboot_arch[i].name;
    139   1.7      matt 	}
    140   1.7      matt 
    141   1.7      matt 	return "Unknown";
    142   1.7      matt }
    143   1.7      matt 
    144  1.14     joerg static const struct uboot_type {
    145   1.1  jmcneill 	enum uboot_image_type type;
    146   1.1  jmcneill 	const char *name;
    147   1.1  jmcneill } uboot_type[] = {
    148  1.20  jmcneill 	{ IH_TYPE_STANDALONE,		"standalone" },
    149  1.20  jmcneill 	{ IH_TYPE_KERNEL,		"kernel" },
    150  1.20  jmcneill 	{ IH_TYPE_KERNEL_NOLOAD,	"kernel_noload" },
    151  1.20  jmcneill 	{ IH_TYPE_RAMDISK,		"ramdisk" },
    152  1.20  jmcneill 	{ IH_TYPE_FILESYSTEM,		"fs" },
    153  1.20  jmcneill 	{ IH_TYPE_SCRIPT,		"script" },
    154   1.1  jmcneill };
    155   1.1  jmcneill 
    156   1.1  jmcneill static enum uboot_image_type
    157   1.1  jmcneill get_type(const char *name)
    158   1.1  jmcneill {
    159   1.1  jmcneill 	unsigned int i;
    160   1.1  jmcneill 
    161   1.1  jmcneill 	for (i = 0; i < __arraycount(uboot_type); i++) {
    162   1.1  jmcneill 		if (strcmp(uboot_type[i].name, name) == 0)
    163   1.1  jmcneill 			return uboot_type[i].type;
    164   1.1  jmcneill 	}
    165   1.1  jmcneill 
    166   1.1  jmcneill 	return IH_TYPE_UNKNOWN;
    167   1.1  jmcneill }
    168   1.1  jmcneill 
    169   1.7      matt static const char *
    170   1.7      matt get_type_name(enum uboot_image_type type)
    171   1.7      matt {
    172   1.7      matt 	unsigned int i;
    173   1.7      matt 
    174   1.7      matt 	for (i = 0; i < __arraycount(uboot_type); i++) {
    175   1.7      matt 		if (uboot_type[i].type == type)
    176   1.7      matt 			return uboot_type[i].name;
    177   1.7      matt 	}
    178   1.7      matt 
    179   1.7      matt 	return "Unknown";
    180   1.7      matt }
    181   1.7      matt 
    182  1.14     joerg static const struct uboot_comp {
    183   1.1  jmcneill 	enum uboot_image_comp comp;
    184   1.1  jmcneill 	const char *name;
    185   1.1  jmcneill } uboot_comp[] = {
    186   1.1  jmcneill 	{ IH_COMP_NONE,		"none" },
    187   1.1  jmcneill 	{ IH_COMP_GZIP,		"gz" },
    188   1.1  jmcneill 	{ IH_COMP_BZIP2,	"bz2" },
    189  1.10      matt 	{ IH_COMP_LZMA,		"lzma" },
    190  1.10      matt 	{ IH_COMP_LZO,		"lzo" },
    191   1.1  jmcneill };
    192   1.1  jmcneill 
    193   1.1  jmcneill static enum uboot_image_comp
    194   1.1  jmcneill get_comp(const char *name)
    195   1.1  jmcneill {
    196   1.1  jmcneill 	unsigned int i;
    197   1.1  jmcneill 
    198   1.1  jmcneill 	for (i = 0; i < __arraycount(uboot_comp); i++) {
    199   1.1  jmcneill 		if (strcmp(uboot_comp[i].name, name) == 0)
    200   1.1  jmcneill 			return uboot_comp[i].comp;
    201   1.1  jmcneill 	}
    202   1.1  jmcneill 
    203   1.1  jmcneill 	return IH_TYPE_UNKNOWN;
    204   1.1  jmcneill }
    205   1.1  jmcneill 
    206   1.7      matt static const char *
    207   1.7      matt get_comp_name(enum uboot_image_comp comp)
    208   1.7      matt {
    209   1.7      matt 	unsigned int i;
    210   1.7      matt 
    211   1.7      matt 	for (i = 0; i < __arraycount(uboot_comp); i++) {
    212   1.7      matt 		if (uboot_comp[i].comp == comp)
    213   1.7      matt 			return uboot_comp[i].name;
    214   1.7      matt 	}
    215   1.7      matt 
    216   1.7      matt 	return "Unknown";
    217   1.7      matt }
    218   1.7      matt 
    219  1.13     joerg __dead static void
    220   1.1  jmcneill usage(void)
    221   1.1  jmcneill {
    222  1.18   msaitoh 	fprintf(stderr, "usage: mkubootimage -A "
    223  1.21  jmcneill 	    "<arm|arm64|i386|mips|mips64|or1k|powerpc|sh>");
    224  1.12       wiz 	fprintf(stderr, " -C <none|bz2|gz|lzma|lzo>");
    225   1.6       phx 	fprintf(stderr, " -O <openbsd|netbsd|freebsd|linux>");
    226  1.20  jmcneill 	fprintf(stderr, " -T <standalone|kernel|kernel_noload|ramdisk|fs|script>");
    227   1.8       riz 	fprintf(stderr, " -a <addr> [-e <ep>] [-m <magic>] -n <name>");
    228   1.1  jmcneill 	fprintf(stderr, " <srcfile> <dstfile>\n");
    229   1.1  jmcneill 
    230   1.1  jmcneill 	exit(EXIT_FAILURE);
    231   1.1  jmcneill }
    232   1.1  jmcneill 
    233   1.1  jmcneill static void
    234   1.1  jmcneill dump_header(struct uboot_image_header *hdr)
    235   1.1  jmcneill {
    236   1.1  jmcneill 	time_t tm = ntohl(hdr->ih_time);
    237   1.1  jmcneill 
    238   1.1  jmcneill 	printf(" magic:       0x%08x\n", ntohl(hdr->ih_magic));
    239   1.1  jmcneill 	printf(" time:        %s", ctime(&tm));
    240   1.1  jmcneill 	printf(" size:        %u\n", ntohl(hdr->ih_size));
    241   1.1  jmcneill 	printf(" load addr:   0x%08x\n", ntohl(hdr->ih_load));
    242   1.1  jmcneill 	printf(" entry point: 0x%08x\n", ntohl(hdr->ih_ep));
    243   1.1  jmcneill 	printf(" data crc:    0x%08x\n", ntohl(hdr->ih_dcrc));
    244   1.7      matt 	printf(" os:          %d (%s)\n", hdr->ih_os,
    245   1.7      matt 	    get_os_name(hdr->ih_os));
    246   1.7      matt 	printf(" arch:        %d (%s)\n", hdr->ih_arch,
    247   1.7      matt 	    get_arch_name(hdr->ih_arch));
    248   1.7      matt 	printf(" type:        %d (%s)\n", hdr->ih_type,
    249   1.7      matt 	    get_type_name(hdr->ih_type));
    250   1.7      matt 	printf(" comp:        %d (%s)\n", hdr->ih_comp,
    251   1.7      matt 	    get_comp_name(hdr->ih_comp));
    252   1.1  jmcneill 	printf(" name:        %s\n", hdr->ih_name);
    253   1.1  jmcneill 	printf(" header crc:  0x%08x\n", hdr->ih_hcrc);
    254   1.1  jmcneill }
    255   1.1  jmcneill 
    256   1.1  jmcneill static int
    257   1.1  jmcneill generate_header(struct uboot_image_header *hdr, int kernel_fd)
    258   1.1  jmcneill {
    259   1.1  jmcneill 	uint8_t *p;
    260   1.1  jmcneill 	struct stat st;
    261  1.17  jmcneill 	uint32_t crc, dsize, size_buf[2];
    262   1.1  jmcneill 	int error;
    263   1.1  jmcneill 
    264   1.1  jmcneill 	error = fstat(kernel_fd, &st);
    265   1.1  jmcneill 	if (error == -1) {
    266   1.1  jmcneill 		perror("stat");
    267   1.1  jmcneill 		return errno;
    268   1.1  jmcneill 	}
    269   1.1  jmcneill 
    270   1.1  jmcneill 	if (st.st_size + sizeof(*hdr) > UINT32_MAX) {
    271   1.1  jmcneill 		fprintf(stderr, "fatal: kernel too big\n");
    272   1.1  jmcneill 		return EINVAL;
    273   1.1  jmcneill 	}
    274   1.1  jmcneill 
    275   1.1  jmcneill 	p = mmap(0, st.st_size, PROT_READ, MAP_FILE|MAP_SHARED, kernel_fd, 0);
    276   1.1  jmcneill 	if (p == MAP_FAILED) {
    277   1.1  jmcneill 		perror("mmap kernel");
    278   1.1  jmcneill 		return EINVAL;
    279   1.1  jmcneill 	}
    280  1.17  jmcneill 	if (image_type == IH_TYPE_SCRIPT) {
    281  1.17  jmcneill 		struct iovec iov[3];
    282  1.17  jmcneill 		dsize = st.st_size + (sizeof(uint32_t) * 2);
    283  1.17  jmcneill 		size_buf[0] = htonl(st.st_size);
    284  1.17  jmcneill 		size_buf[1] = htonl(0);
    285  1.17  jmcneill 		iov[0].iov_base = &size_buf[0];
    286  1.17  jmcneill 		iov[0].iov_len = sizeof(size_buf[0]);
    287  1.17  jmcneill 		iov[1].iov_base = &size_buf[1];
    288  1.17  jmcneill 		iov[1].iov_len = sizeof(size_buf[1]);
    289  1.17  jmcneill 		iov[2].iov_base = p;
    290  1.17  jmcneill 		iov[2].iov_len = st.st_size;
    291  1.17  jmcneill 		crc = crc32v(iov, 3);
    292  1.17  jmcneill 	} else {
    293  1.17  jmcneill 		dsize = st.st_size;
    294  1.17  jmcneill 		crc = crc32(p, st.st_size);
    295  1.17  jmcneill 	}
    296   1.1  jmcneill 	munmap(p, st.st_size);
    297   1.1  jmcneill 
    298   1.1  jmcneill 	memset(hdr, 0, sizeof(*hdr));
    299   1.8       riz 	hdr->ih_magic = htonl(image_magic);
    300   1.1  jmcneill 	hdr->ih_time = htonl(st.st_mtime);
    301  1.17  jmcneill 	hdr->ih_size = htonl(dsize);
    302   1.1  jmcneill 	hdr->ih_load = htonl(image_loadaddr);
    303   1.1  jmcneill 	hdr->ih_ep = htonl(image_entrypoint);
    304   1.1  jmcneill 	hdr->ih_dcrc = htonl(crc);
    305   1.6       phx 	hdr->ih_os = image_os;
    306   1.1  jmcneill 	hdr->ih_arch = image_arch;
    307   1.1  jmcneill 	hdr->ih_type = image_type;
    308   1.1  jmcneill 	hdr->ih_comp = image_comp;
    309   1.7      matt 	strlcpy((char *)hdr->ih_name, image_name, sizeof(hdr->ih_name));
    310   1.1  jmcneill 	crc = crc32((void *)hdr, sizeof(*hdr));
    311   1.1  jmcneill 	hdr->ih_hcrc = htonl(crc);
    312   1.1  jmcneill 
    313   1.1  jmcneill 	dump_header(hdr);
    314   1.1  jmcneill 
    315   1.1  jmcneill 	return 0;
    316   1.1  jmcneill }
    317   1.1  jmcneill 
    318   1.1  jmcneill static int
    319   1.1  jmcneill write_image(struct uboot_image_header *hdr, int kernel_fd, int image_fd)
    320   1.1  jmcneill {
    321   1.1  jmcneill 	uint8_t buf[4096];
    322   1.1  jmcneill 	ssize_t rlen, wlen;
    323  1.17  jmcneill 	struct stat st;
    324  1.17  jmcneill 	uint32_t size_buf[2];
    325  1.17  jmcneill 	int error;
    326  1.17  jmcneill 
    327  1.17  jmcneill 	error = fstat(kernel_fd, &st);
    328  1.17  jmcneill 	if (error == -1) {
    329  1.17  jmcneill 		perror("stat");
    330  1.17  jmcneill 		return errno;
    331  1.17  jmcneill 	}
    332   1.1  jmcneill 
    333   1.1  jmcneill 	wlen = write(image_fd, hdr, sizeof(*hdr));
    334   1.1  jmcneill 	if (wlen != sizeof(*hdr)) {
    335   1.1  jmcneill 		perror("short write");
    336   1.1  jmcneill 		return errno;
    337   1.1  jmcneill 	}
    338   1.1  jmcneill 
    339  1.17  jmcneill 	if (image_type == IH_TYPE_SCRIPT) {
    340  1.17  jmcneill 		size_buf[0] = htonl(st.st_size);
    341  1.17  jmcneill 		size_buf[1] = htonl(0);
    342  1.17  jmcneill 		wlen = write(image_fd, &size_buf, sizeof(size_buf));
    343  1.17  jmcneill 		if (wlen != sizeof(size_buf)) {
    344  1.17  jmcneill 			perror("short write");
    345  1.17  jmcneill 			return errno;
    346  1.17  jmcneill 		}
    347  1.17  jmcneill 	}
    348  1.17  jmcneill 
    349   1.1  jmcneill 	while ((rlen = read(kernel_fd, buf, sizeof(buf))) > 0) {
    350   1.1  jmcneill 		wlen = write(image_fd, buf, rlen);
    351   1.1  jmcneill 		if (wlen != rlen) {
    352   1.1  jmcneill 			perror("short write");
    353   1.1  jmcneill 			return errno;
    354   1.1  jmcneill 		}
    355   1.1  jmcneill 	}
    356   1.1  jmcneill 
    357   1.1  jmcneill 	return 0;
    358   1.1  jmcneill }
    359   1.1  jmcneill 
    360   1.1  jmcneill int
    361   1.1  jmcneill main(int argc, char *argv[])
    362   1.1  jmcneill {
    363   1.1  jmcneill 	struct uboot_image_header hdr;
    364   1.1  jmcneill 	const char *src, *dest;
    365   1.1  jmcneill 	char *ep;
    366   1.1  jmcneill 	int kernel_fd, image_fd;
    367   1.1  jmcneill 	int ch;
    368  1.15      matt 	unsigned long long num;
    369   1.1  jmcneill 
    370   1.9      matt 	while ((ch = getopt(argc, argv, "A:C:E:O:T:a:e:hm:n:")) != -1) {
    371   1.1  jmcneill 		switch (ch) {
    372   1.1  jmcneill 		case 'A':	/* arch */
    373   1.1  jmcneill 			image_arch = get_arch(optarg);
    374   1.1  jmcneill 			break;
    375   1.1  jmcneill 		case 'C':	/* comp */
    376   1.1  jmcneill 			image_comp = get_comp(optarg);
    377   1.1  jmcneill 			break;
    378   1.6       phx 		case 'O':	/* os */
    379   1.6       phx 			image_os = get_os(optarg);
    380   1.6       phx 			break;
    381   1.1  jmcneill 		case 'T':	/* type */
    382   1.1  jmcneill 			image_type = get_type(optarg);
    383   1.1  jmcneill 			break;
    384   1.1  jmcneill 		case 'a':	/* addr */
    385   1.1  jmcneill 			errno = 0;
    386  1.15      matt 			num = strtoull(optarg, &ep, 0);
    387   1.1  jmcneill 			if (*ep != '\0' || (errno == ERANGE &&
    388  1.15      matt 			    (num == ULLONG_MAX || num == 0)) ||
    389  1.16      matt 			    ((signed long long)num != (int32_t)num &&
    390  1.16      matt 			     num != (uint32_t)num))
    391   1.1  jmcneill 				errx(1, "illegal number -- %s", optarg);
    392   1.1  jmcneill 			image_loadaddr = (uint32_t)num;
    393   1.1  jmcneill 			break;
    394   1.9      matt 		case 'E':	/* ep (byte swapped) */
    395   1.1  jmcneill 		case 'e':	/* ep */
    396   1.1  jmcneill 			errno = 0;
    397  1.15      matt 			num = strtoull(optarg, &ep, 0);
    398   1.1  jmcneill 			if (*ep != '\0' || (errno == ERANGE &&
    399  1.15      matt 			    (num == ULLONG_MAX || num == 0)) ||
    400  1.16      matt 			    ((signed long long)num != (int32_t)num &&
    401  1.16      matt 			     num != (uint32_t)num))
    402   1.1  jmcneill 				errx(1, "illegal number -- %s", optarg);
    403   1.1  jmcneill 			image_entrypoint = (uint32_t)num;
    404   1.9      matt 			if (ch == 'E')
    405   1.9      matt 				image_entrypoint = bswap32(image_entrypoint);
    406   1.1  jmcneill 			break;
    407   1.8       riz 		case 'm':	/* magic */
    408   1.8       riz 			errno = 0;
    409   1.8       riz 			num = strtoul(optarg, &ep, 0);
    410   1.8       riz 			if (*ep != '\0' || (errno == ERANGE &&
    411   1.8       riz 			    (num == ULONG_MAX || num == 0)))
    412   1.8       riz 				errx(1, "illegal number -- %s", optarg);
    413   1.8       riz 			image_magic = (uint32_t)num;
    414  1.22  jmcneill 			break;
    415   1.1  jmcneill 		case 'n':	/* name */
    416   1.1  jmcneill 			image_name = strdup(optarg);
    417   1.1  jmcneill 			break;
    418   1.1  jmcneill 		case 'h':
    419   1.1  jmcneill 		default:
    420   1.1  jmcneill 			usage();
    421   1.1  jmcneill 			/* NOTREACHED */
    422   1.1  jmcneill 		}
    423   1.1  jmcneill 	}
    424   1.1  jmcneill 	argc -= optind;
    425   1.1  jmcneill 	argv += optind;
    426   1.1  jmcneill 
    427   1.1  jmcneill 	if (argc != 2)
    428   1.1  jmcneill 		usage();
    429   1.1  jmcneill 
    430   1.4  kiyohara 	if (image_entrypoint == 0)
    431   1.4  kiyohara 		image_entrypoint = image_loadaddr;
    432   1.4  kiyohara 
    433   1.1  jmcneill 	if (image_arch == IH_ARCH_UNKNOWN ||
    434   1.1  jmcneill 	    image_type == IH_TYPE_UNKNOWN ||
    435   1.1  jmcneill 	    image_name == NULL)
    436   1.1  jmcneill 		usage();
    437   1.1  jmcneill 
    438  1.20  jmcneill 	switch (image_type) {
    439  1.20  jmcneill 	case IH_TYPE_SCRIPT:
    440  1.20  jmcneill 	case IH_TYPE_RAMDISK:
    441  1.20  jmcneill 	case IH_TYPE_KERNEL_NOLOAD:
    442  1.20  jmcneill 		break;
    443  1.20  jmcneill 	default:
    444  1.20  jmcneill 		if (image_loadaddr == 0)
    445  1.20  jmcneill 			usage();
    446  1.20  jmcneill 			/* NOTREACHED */
    447  1.20  jmcneill 		break;
    448  1.20  jmcneill 	}
    449  1.20  jmcneill 
    450   1.1  jmcneill 	src = argv[0];
    451   1.1  jmcneill 	dest = argv[1];
    452   1.1  jmcneill 
    453   1.1  jmcneill 	kernel_fd = open(src, O_RDONLY);
    454   1.1  jmcneill 	if (kernel_fd == -1) {
    455   1.1  jmcneill 		perror("open kernel");
    456   1.1  jmcneill 		return EXIT_FAILURE;
    457   1.1  jmcneill 	}
    458  1.11      matt 	image_fd = open(dest, O_WRONLY|O_CREAT|O_TRUNC, 0666);
    459   1.1  jmcneill 	if (image_fd == -1) {
    460   1.1  jmcneill 		perror("open image");
    461   1.1  jmcneill 		return EXIT_FAILURE;
    462   1.1  jmcneill 	}
    463   1.1  jmcneill 
    464   1.1  jmcneill 	if (generate_header(&hdr, kernel_fd) != 0)
    465   1.1  jmcneill 		return EXIT_FAILURE;
    466   1.1  jmcneill 
    467   1.1  jmcneill 	if (write_image(&hdr, kernel_fd, image_fd) != 0)
    468   1.1  jmcneill 		return EXIT_FAILURE;
    469   1.1  jmcneill 
    470   1.1  jmcneill 	close(image_fd);
    471   1.1  jmcneill 	close(kernel_fd);
    472   1.1  jmcneill 
    473   1.1  jmcneill 	return EXIT_SUCCESS;
    474   1.1  jmcneill }
    475