Home | History | Annotate | Line # | Download | only in mkubootimage
mkubootimage.c revision 1.24.6.1
      1  1.24.6.1    martin /* $NetBSD: mkubootimage.c,v 1.24.6.1 2019/12/09 15:19:30 martin 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.24.6.1    martin __RCSID("$NetBSD: mkubootimage.c,v 1.24.6.1 2019/12/09 15:19:30 martin 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.23  jmcneill #include <sys/param.h>
     39      1.17  jmcneill #include <sys/uio.h>
     40       1.1  jmcneill #include <err.h>
     41       1.1  jmcneill #include <errno.h>
     42       1.1  jmcneill #include <fcntl.h>
     43      1.23  jmcneill #include <inttypes.h>
     44       1.1  jmcneill #include <limits.h>
     45       1.1  jmcneill #include <stdint.h>
     46       1.1  jmcneill #include <stdio.h>
     47       1.1  jmcneill #include <stdlib.h>
     48       1.1  jmcneill #include <string.h>
     49       1.1  jmcneill #include <time.h>
     50       1.1  jmcneill #include <unistd.h>
     51       1.1  jmcneill 
     52       1.1  jmcneill #include "uboot.h"
     53      1.23  jmcneill #include "arm64.h"
     54       1.1  jmcneill 
     55       1.1  jmcneill #ifndef __arraycount
     56       1.1  jmcneill #define __arraycount(__x)	(sizeof(__x) / sizeof(__x[0]))
     57       1.1  jmcneill #endif
     58       1.1  jmcneill 
     59      1.23  jmcneill enum image_format {
     60      1.23  jmcneill 	FMT_UNKNOWN,
     61      1.23  jmcneill 	FMT_UIMG,	/* Legacy U-Boot image */
     62      1.23  jmcneill 	FMT_ARM64,	/* Linux ARM64 image (booti) */
     63      1.23  jmcneill };
     64      1.23  jmcneill 
     65       1.1  jmcneill extern uint32_t crc32(const void *, size_t);
     66      1.17  jmcneill extern uint32_t crc32v(const struct iovec *, int);
     67       1.1  jmcneill 
     68       1.6       phx static enum uboot_image_os image_os = IH_OS_NETBSD;
     69       1.1  jmcneill static enum uboot_image_arch image_arch = IH_ARCH_UNKNOWN;
     70       1.1  jmcneill static enum uboot_image_type image_type = IH_TYPE_UNKNOWN;
     71       1.1  jmcneill static enum uboot_image_comp image_comp = IH_COMP_NONE;
     72       1.1  jmcneill static uint32_t image_loadaddr = 0;
     73       1.1  jmcneill static uint32_t image_entrypoint = 0;
     74       1.1  jmcneill static char *image_name;
     75       1.8       riz static uint32_t image_magic = IH_MAGIC;
     76      1.23  jmcneill static enum image_format image_format = FMT_UIMG;
     77  1.24.6.1    martin static int update_image = 0;
     78      1.23  jmcneill 
     79      1.23  jmcneill static const struct uboot_image_format {
     80      1.23  jmcneill 	enum image_format format;
     81      1.23  jmcneill 	const char *name;
     82      1.23  jmcneill } uboot_image_format[] = {
     83      1.23  jmcneill 	{ FMT_UIMG,		"uimg" },
     84      1.23  jmcneill 	{ FMT_ARM64,		"arm64" },
     85      1.23  jmcneill };
     86      1.23  jmcneill 
     87      1.23  jmcneill static enum image_format
     88      1.23  jmcneill get_image_format(const char *name)
     89      1.23  jmcneill {
     90      1.23  jmcneill 	unsigned int i;
     91      1.23  jmcneill 
     92      1.23  jmcneill 	for (i = 0; i < __arraycount(uboot_image_format); i++) {
     93      1.23  jmcneill 		if (strcmp(uboot_image_format[i].name, name) == 0)
     94      1.23  jmcneill 			return uboot_image_format[i].format;
     95      1.23  jmcneill 	}
     96      1.23  jmcneill 
     97      1.23  jmcneill 	return FMT_UNKNOWN;
     98      1.23  jmcneill }
     99      1.23  jmcneill 
    100      1.23  jmcneill static const char *
    101      1.23  jmcneill get_image_format_name(enum image_format format)
    102      1.23  jmcneill {
    103      1.23  jmcneill 	unsigned int i;
    104      1.23  jmcneill 
    105      1.23  jmcneill 	for (i = 0; i < __arraycount(uboot_image_format); i++) {
    106      1.23  jmcneill 		if (uboot_image_format[i].format == format)
    107      1.23  jmcneill 			return uboot_image_format[i].name;
    108      1.23  jmcneill 	}
    109      1.23  jmcneill 
    110      1.23  jmcneill 	return "Unknown";
    111      1.23  jmcneill }
    112       1.1  jmcneill 
    113      1.14     joerg static const struct uboot_os {
    114       1.6       phx 	enum uboot_image_os os;
    115       1.6       phx 	const char *name;
    116       1.6       phx } uboot_os[] = {
    117       1.6       phx 	{ IH_OS_OPENBSD,	"openbsd" },
    118       1.6       phx 	{ IH_OS_NETBSD,		"netbsd" },
    119       1.6       phx 	{ IH_OS_FREEBSD,	"freebsd" },
    120       1.6       phx 	{ IH_OS_LINUX,		"linux" },
    121       1.6       phx };
    122       1.6       phx 
    123       1.6       phx static enum uboot_image_os
    124       1.6       phx get_os(const char *name)
    125       1.6       phx {
    126       1.6       phx 	unsigned int i;
    127       1.6       phx 
    128       1.6       phx 	for (i = 0; i < __arraycount(uboot_os); i++) {
    129       1.6       phx 		if (strcmp(uboot_os[i].name, name) == 0)
    130       1.6       phx 			return uboot_os[i].os;
    131       1.6       phx 	}
    132       1.6       phx 
    133       1.6       phx 	return IH_OS_UNKNOWN;
    134       1.6       phx }
    135       1.6       phx 
    136       1.7      matt static const char *
    137       1.7      matt get_os_name(enum uboot_image_os os)
    138       1.7      matt {
    139       1.7      matt 	unsigned int i;
    140       1.7      matt 
    141       1.7      matt 	for (i = 0; i < __arraycount(uboot_os); i++) {
    142       1.7      matt 		if (uboot_os[i].os == os)
    143       1.7      matt 			return uboot_os[i].name;
    144       1.7      matt 	}
    145       1.7      matt 
    146       1.7      matt 	return "Unknown";
    147       1.7      matt }
    148       1.7      matt 
    149      1.13     joerg static const struct uboot_arch {
    150       1.1  jmcneill 	enum uboot_image_arch arch;
    151       1.1  jmcneill 	const char *name;
    152       1.1  jmcneill } uboot_arch[] = {
    153       1.1  jmcneill 	{ IH_ARCH_ARM,		"arm" },
    154      1.21  jmcneill 	{ IH_ARCH_ARM64,	"arm64" },
    155      1.18   msaitoh 	{ IH_ARCH_I386,		"i386" },
    156       1.5      matt 	{ IH_ARCH_MIPS,		"mips" },
    157       1.5      matt 	{ IH_ARCH_MIPS64,	"mips64" },
    158       1.1  jmcneill 	{ IH_ARCH_PPC,		"powerpc" },
    159      1.18   msaitoh 	{ IH_ARCH_OPENRISC,	"or1k" },
    160      1.21  jmcneill 	{ IH_ARCH_SH,		"sh" },
    161       1.1  jmcneill };
    162       1.1  jmcneill 
    163       1.1  jmcneill static enum uboot_image_arch
    164       1.1  jmcneill get_arch(const char *name)
    165       1.1  jmcneill {
    166       1.1  jmcneill 	unsigned int i;
    167       1.1  jmcneill 
    168       1.1  jmcneill 	for (i = 0; i < __arraycount(uboot_arch); i++) {
    169       1.1  jmcneill 		if (strcmp(uboot_arch[i].name, name) == 0)
    170       1.1  jmcneill 			return uboot_arch[i].arch;
    171       1.1  jmcneill 	}
    172       1.1  jmcneill 
    173       1.1  jmcneill 	return IH_ARCH_UNKNOWN;
    174       1.1  jmcneill }
    175       1.1  jmcneill 
    176       1.7      matt static const char *
    177       1.7      matt get_arch_name(enum uboot_image_arch arch)
    178       1.7      matt {
    179       1.7      matt 	unsigned int i;
    180       1.7      matt 
    181       1.7      matt 	for (i = 0; i < __arraycount(uboot_arch); i++) {
    182       1.7      matt 		if (uboot_arch[i].arch == arch)
    183       1.7      matt 			return uboot_arch[i].name;
    184       1.7      matt 	}
    185       1.7      matt 
    186       1.7      matt 	return "Unknown";
    187       1.7      matt }
    188       1.7      matt 
    189      1.14     joerg static const struct uboot_type {
    190       1.1  jmcneill 	enum uboot_image_type type;
    191       1.1  jmcneill 	const char *name;
    192       1.1  jmcneill } uboot_type[] = {
    193      1.20  jmcneill 	{ IH_TYPE_STANDALONE,		"standalone" },
    194      1.20  jmcneill 	{ IH_TYPE_KERNEL,		"kernel" },
    195      1.20  jmcneill 	{ IH_TYPE_KERNEL_NOLOAD,	"kernel_noload" },
    196      1.20  jmcneill 	{ IH_TYPE_RAMDISK,		"ramdisk" },
    197      1.20  jmcneill 	{ IH_TYPE_FILESYSTEM,		"fs" },
    198      1.20  jmcneill 	{ IH_TYPE_SCRIPT,		"script" },
    199       1.1  jmcneill };
    200       1.1  jmcneill 
    201       1.1  jmcneill static enum uboot_image_type
    202       1.1  jmcneill get_type(const char *name)
    203       1.1  jmcneill {
    204       1.1  jmcneill 	unsigned int i;
    205       1.1  jmcneill 
    206       1.1  jmcneill 	for (i = 0; i < __arraycount(uboot_type); i++) {
    207       1.1  jmcneill 		if (strcmp(uboot_type[i].name, name) == 0)
    208       1.1  jmcneill 			return uboot_type[i].type;
    209       1.1  jmcneill 	}
    210       1.1  jmcneill 
    211       1.1  jmcneill 	return IH_TYPE_UNKNOWN;
    212       1.1  jmcneill }
    213       1.1  jmcneill 
    214       1.7      matt static const char *
    215       1.7      matt get_type_name(enum uboot_image_type type)
    216       1.7      matt {
    217       1.7      matt 	unsigned int i;
    218       1.7      matt 
    219       1.7      matt 	for (i = 0; i < __arraycount(uboot_type); i++) {
    220       1.7      matt 		if (uboot_type[i].type == type)
    221       1.7      matt 			return uboot_type[i].name;
    222       1.7      matt 	}
    223       1.7      matt 
    224       1.7      matt 	return "Unknown";
    225       1.7      matt }
    226       1.7      matt 
    227      1.14     joerg static const struct uboot_comp {
    228       1.1  jmcneill 	enum uboot_image_comp comp;
    229       1.1  jmcneill 	const char *name;
    230       1.1  jmcneill } uboot_comp[] = {
    231       1.1  jmcneill 	{ IH_COMP_NONE,		"none" },
    232       1.1  jmcneill 	{ IH_COMP_GZIP,		"gz" },
    233       1.1  jmcneill 	{ IH_COMP_BZIP2,	"bz2" },
    234      1.10      matt 	{ IH_COMP_LZMA,		"lzma" },
    235      1.10      matt 	{ IH_COMP_LZO,		"lzo" },
    236       1.1  jmcneill };
    237       1.1  jmcneill 
    238       1.1  jmcneill static enum uboot_image_comp
    239       1.1  jmcneill get_comp(const char *name)
    240       1.1  jmcneill {
    241       1.1  jmcneill 	unsigned int i;
    242       1.1  jmcneill 
    243       1.1  jmcneill 	for (i = 0; i < __arraycount(uboot_comp); i++) {
    244       1.1  jmcneill 		if (strcmp(uboot_comp[i].name, name) == 0)
    245       1.1  jmcneill 			return uboot_comp[i].comp;
    246       1.1  jmcneill 	}
    247       1.1  jmcneill 
    248      1.24  jmcneill 	return IH_COMP_NONE;
    249       1.1  jmcneill }
    250       1.1  jmcneill 
    251       1.7      matt static const char *
    252       1.7      matt get_comp_name(enum uboot_image_comp comp)
    253       1.7      matt {
    254       1.7      matt 	unsigned int i;
    255       1.7      matt 
    256       1.7      matt 	for (i = 0; i < __arraycount(uboot_comp); i++) {
    257       1.7      matt 		if (uboot_comp[i].comp == comp)
    258       1.7      matt 			return uboot_comp[i].name;
    259       1.7      matt 	}
    260       1.7      matt 
    261       1.7      matt 	return "Unknown";
    262       1.7      matt }
    263       1.7      matt 
    264      1.13     joerg __dead static void
    265       1.1  jmcneill usage(void)
    266       1.1  jmcneill {
    267      1.18   msaitoh 	fprintf(stderr, "usage: mkubootimage -A "
    268      1.21  jmcneill 	    "<arm|arm64|i386|mips|mips64|or1k|powerpc|sh>");
    269      1.12       wiz 	fprintf(stderr, " -C <none|bz2|gz|lzma|lzo>");
    270       1.6       phx 	fprintf(stderr, " -O <openbsd|netbsd|freebsd|linux>");
    271      1.20  jmcneill 	fprintf(stderr, " -T <standalone|kernel|kernel_noload|ramdisk|fs|script>");
    272       1.8       riz 	fprintf(stderr, " -a <addr> [-e <ep>] [-m <magic>] -n <name>");
    273  1.24.6.1    martin 	fprintf(stderr, " [-f <uimg|arm64>] [-u]");
    274       1.1  jmcneill 	fprintf(stderr, " <srcfile> <dstfile>\n");
    275       1.1  jmcneill 
    276       1.1  jmcneill 	exit(EXIT_FAILURE);
    277       1.1  jmcneill }
    278       1.1  jmcneill 
    279       1.1  jmcneill static void
    280      1.23  jmcneill dump_header_uimg(struct uboot_image_header *hdr)
    281       1.1  jmcneill {
    282       1.1  jmcneill 	time_t tm = ntohl(hdr->ih_time);
    283       1.1  jmcneill 
    284       1.1  jmcneill 	printf(" magic:       0x%08x\n", ntohl(hdr->ih_magic));
    285       1.1  jmcneill 	printf(" time:        %s", ctime(&tm));
    286       1.1  jmcneill 	printf(" size:        %u\n", ntohl(hdr->ih_size));
    287       1.1  jmcneill 	printf(" load addr:   0x%08x\n", ntohl(hdr->ih_load));
    288       1.1  jmcneill 	printf(" entry point: 0x%08x\n", ntohl(hdr->ih_ep));
    289       1.1  jmcneill 	printf(" data crc:    0x%08x\n", ntohl(hdr->ih_dcrc));
    290       1.7      matt 	printf(" os:          %d (%s)\n", hdr->ih_os,
    291       1.7      matt 	    get_os_name(hdr->ih_os));
    292       1.7      matt 	printf(" arch:        %d (%s)\n", hdr->ih_arch,
    293       1.7      matt 	    get_arch_name(hdr->ih_arch));
    294       1.7      matt 	printf(" type:        %d (%s)\n", hdr->ih_type,
    295       1.7      matt 	    get_type_name(hdr->ih_type));
    296       1.7      matt 	printf(" comp:        %d (%s)\n", hdr->ih_comp,
    297       1.7      matt 	    get_comp_name(hdr->ih_comp));
    298       1.1  jmcneill 	printf(" name:        %s\n", hdr->ih_name);
    299       1.1  jmcneill 	printf(" header crc:  0x%08x\n", hdr->ih_hcrc);
    300       1.1  jmcneill }
    301       1.1  jmcneill 
    302       1.1  jmcneill static int
    303      1.23  jmcneill generate_header_uimg(struct uboot_image_header *hdr, int kernel_fd)
    304       1.1  jmcneill {
    305       1.1  jmcneill 	uint8_t *p;
    306       1.1  jmcneill 	struct stat st;
    307      1.17  jmcneill 	uint32_t crc, dsize, size_buf[2];
    308       1.1  jmcneill 	int error;
    309       1.1  jmcneill 
    310       1.1  jmcneill 	error = fstat(kernel_fd, &st);
    311       1.1  jmcneill 	if (error == -1) {
    312       1.1  jmcneill 		perror("stat");
    313       1.1  jmcneill 		return errno;
    314       1.1  jmcneill 	}
    315       1.1  jmcneill 
    316       1.1  jmcneill 	if (st.st_size + sizeof(*hdr) > UINT32_MAX) {
    317       1.1  jmcneill 		fprintf(stderr, "fatal: kernel too big\n");
    318       1.1  jmcneill 		return EINVAL;
    319       1.1  jmcneill 	}
    320       1.1  jmcneill 
    321       1.1  jmcneill 	p = mmap(0, st.st_size, PROT_READ, MAP_FILE|MAP_SHARED, kernel_fd, 0);
    322       1.1  jmcneill 	if (p == MAP_FAILED) {
    323       1.1  jmcneill 		perror("mmap kernel");
    324       1.1  jmcneill 		return EINVAL;
    325       1.1  jmcneill 	}
    326      1.17  jmcneill 	if (image_type == IH_TYPE_SCRIPT) {
    327      1.17  jmcneill 		struct iovec iov[3];
    328      1.17  jmcneill 		dsize = st.st_size + (sizeof(uint32_t) * 2);
    329      1.17  jmcneill 		size_buf[0] = htonl(st.st_size);
    330      1.17  jmcneill 		size_buf[1] = htonl(0);
    331      1.17  jmcneill 		iov[0].iov_base = &size_buf[0];
    332      1.17  jmcneill 		iov[0].iov_len = sizeof(size_buf[0]);
    333      1.17  jmcneill 		iov[1].iov_base = &size_buf[1];
    334      1.17  jmcneill 		iov[1].iov_len = sizeof(size_buf[1]);
    335      1.17  jmcneill 		iov[2].iov_base = p;
    336      1.17  jmcneill 		iov[2].iov_len = st.st_size;
    337      1.17  jmcneill 		crc = crc32v(iov, 3);
    338      1.17  jmcneill 	} else {
    339      1.17  jmcneill 		dsize = st.st_size;
    340      1.17  jmcneill 		crc = crc32(p, st.st_size);
    341      1.17  jmcneill 	}
    342       1.1  jmcneill 	munmap(p, st.st_size);
    343       1.1  jmcneill 
    344       1.1  jmcneill 	memset(hdr, 0, sizeof(*hdr));
    345       1.8       riz 	hdr->ih_magic = htonl(image_magic);
    346       1.1  jmcneill 	hdr->ih_time = htonl(st.st_mtime);
    347      1.17  jmcneill 	hdr->ih_size = htonl(dsize);
    348       1.1  jmcneill 	hdr->ih_load = htonl(image_loadaddr);
    349       1.1  jmcneill 	hdr->ih_ep = htonl(image_entrypoint);
    350       1.1  jmcneill 	hdr->ih_dcrc = htonl(crc);
    351       1.6       phx 	hdr->ih_os = image_os;
    352       1.1  jmcneill 	hdr->ih_arch = image_arch;
    353       1.1  jmcneill 	hdr->ih_type = image_type;
    354       1.1  jmcneill 	hdr->ih_comp = image_comp;
    355       1.7      matt 	strlcpy((char *)hdr->ih_name, image_name, sizeof(hdr->ih_name));
    356       1.1  jmcneill 	crc = crc32((void *)hdr, sizeof(*hdr));
    357       1.1  jmcneill 	hdr->ih_hcrc = htonl(crc);
    358       1.1  jmcneill 
    359      1.23  jmcneill 	dump_header_uimg(hdr);
    360       1.1  jmcneill 
    361       1.1  jmcneill 	return 0;
    362       1.1  jmcneill }
    363       1.1  jmcneill 
    364      1.23  jmcneill static void
    365      1.23  jmcneill dump_header_arm64(struct arm64_image_header *hdr)
    366      1.23  jmcneill {
    367      1.23  jmcneill 	printf(" magic:       0x%" PRIx32 "\n", le32toh(hdr->magic));
    368      1.23  jmcneill 	printf(" text offset: 0x%" PRIx64 "\n", le64toh(hdr->text_offset));
    369      1.23  jmcneill 	printf(" image size:  %" PRIu64 "\n", le64toh(hdr->image_size));
    370      1.23  jmcneill 	printf(" flags:       0x%" PRIx64 "\n", le64toh(hdr->flags));
    371      1.23  jmcneill }
    372      1.23  jmcneill 
    373       1.1  jmcneill static int
    374      1.23  jmcneill generate_header_arm64(struct arm64_image_header *hdr, int kernel_fd)
    375      1.23  jmcneill {
    376      1.23  jmcneill 	struct stat st;
    377      1.23  jmcneill 	uint32_t flags;
    378      1.23  jmcneill 	int error;
    379      1.23  jmcneill 
    380      1.23  jmcneill 	error = fstat(kernel_fd, &st);
    381      1.23  jmcneill 	if (error == -1) {
    382      1.23  jmcneill 		perror("stat");
    383      1.23  jmcneill 		return errno;
    384      1.23  jmcneill 	}
    385      1.23  jmcneill 
    386      1.23  jmcneill 	flags = 0;
    387      1.24  jmcneill 	flags |= ARM64_FLAGS_PAGE_SIZE_4K;
    388      1.23  jmcneill #if 0
    389      1.24  jmcneill 	flags |= ARM64_FLAGS_PHYS_PLACEMENT_ANY;
    390      1.23  jmcneill #endif
    391      1.23  jmcneill 
    392      1.23  jmcneill 	memset(hdr, 0, sizeof(*hdr));
    393      1.23  jmcneill 	hdr->code0 = htole32(ARM64_CODE0);
    394      1.23  jmcneill 	hdr->text_offset = htole64(image_entrypoint);
    395      1.23  jmcneill 	hdr->image_size = htole64(st.st_size + sizeof(*hdr));
    396      1.23  jmcneill 	hdr->flags = htole32(flags);
    397      1.23  jmcneill 	hdr->magic = htole32(ARM64_MAGIC);
    398      1.23  jmcneill 
    399      1.23  jmcneill 	dump_header_arm64(hdr);
    400      1.23  jmcneill 
    401      1.23  jmcneill 	return 0;
    402      1.23  jmcneill }
    403      1.23  jmcneill 
    404      1.23  jmcneill static int
    405      1.23  jmcneill write_image(void *hdr, size_t hdrlen, int kernel_fd, int image_fd)
    406       1.1  jmcneill {
    407       1.1  jmcneill 	uint8_t buf[4096];
    408       1.1  jmcneill 	ssize_t rlen, wlen;
    409      1.17  jmcneill 	struct stat st;
    410      1.17  jmcneill 	uint32_t size_buf[2];
    411      1.17  jmcneill 	int error;
    412      1.17  jmcneill 
    413      1.17  jmcneill 	error = fstat(kernel_fd, &st);
    414      1.17  jmcneill 	if (error == -1) {
    415      1.17  jmcneill 		perror("stat");
    416      1.17  jmcneill 		return errno;
    417      1.17  jmcneill 	}
    418       1.1  jmcneill 
    419      1.23  jmcneill 	wlen = write(image_fd, hdr, hdrlen);
    420      1.23  jmcneill 	if (wlen != (ssize_t)hdrlen) {
    421       1.1  jmcneill 		perror("short write");
    422       1.1  jmcneill 		return errno;
    423       1.1  jmcneill 	}
    424       1.1  jmcneill 
    425      1.17  jmcneill 	if (image_type == IH_TYPE_SCRIPT) {
    426      1.17  jmcneill 		size_buf[0] = htonl(st.st_size);
    427      1.17  jmcneill 		size_buf[1] = htonl(0);
    428      1.17  jmcneill 		wlen = write(image_fd, &size_buf, sizeof(size_buf));
    429      1.17  jmcneill 		if (wlen != sizeof(size_buf)) {
    430      1.17  jmcneill 			perror("short write");
    431      1.17  jmcneill 			return errno;
    432      1.17  jmcneill 		}
    433      1.17  jmcneill 	}
    434      1.17  jmcneill 
    435  1.24.6.1    martin 	if (update_image) {
    436  1.24.6.1    martin 		if (lseek(kernel_fd, hdrlen, SEEK_SET) != (off_t)hdrlen) {
    437  1.24.6.1    martin 			perror("seek failed");
    438  1.24.6.1    martin 			return errno;
    439  1.24.6.1    martin 		}
    440  1.24.6.1    martin 	}
    441  1.24.6.1    martin 
    442       1.1  jmcneill 	while ((rlen = read(kernel_fd, buf, sizeof(buf))) > 0) {
    443       1.1  jmcneill 		wlen = write(image_fd, buf, rlen);
    444       1.1  jmcneill 		if (wlen != rlen) {
    445       1.1  jmcneill 			perror("short write");
    446       1.1  jmcneill 			return errno;
    447       1.1  jmcneill 		}
    448       1.1  jmcneill 	}
    449       1.1  jmcneill 
    450       1.1  jmcneill 	return 0;
    451       1.1  jmcneill }
    452       1.1  jmcneill 
    453       1.1  jmcneill int
    454       1.1  jmcneill main(int argc, char *argv[])
    455       1.1  jmcneill {
    456      1.23  jmcneill 	struct uboot_image_header hdr_uimg;
    457      1.23  jmcneill 	struct arm64_image_header hdr_arm64;
    458       1.1  jmcneill 	const char *src, *dest;
    459       1.1  jmcneill 	char *ep;
    460       1.1  jmcneill 	int kernel_fd, image_fd;
    461       1.1  jmcneill 	int ch;
    462      1.15      matt 	unsigned long long num;
    463       1.1  jmcneill 
    464  1.24.6.1    martin 	while ((ch = getopt(argc, argv, "A:C:E:O:T:a:e:f:hm:n:u")) != -1) {
    465       1.1  jmcneill 		switch (ch) {
    466       1.1  jmcneill 		case 'A':	/* arch */
    467       1.1  jmcneill 			image_arch = get_arch(optarg);
    468       1.1  jmcneill 			break;
    469       1.1  jmcneill 		case 'C':	/* comp */
    470       1.1  jmcneill 			image_comp = get_comp(optarg);
    471       1.1  jmcneill 			break;
    472       1.6       phx 		case 'O':	/* os */
    473       1.6       phx 			image_os = get_os(optarg);
    474       1.6       phx 			break;
    475       1.1  jmcneill 		case 'T':	/* type */
    476       1.1  jmcneill 			image_type = get_type(optarg);
    477       1.1  jmcneill 			break;
    478       1.1  jmcneill 		case 'a':	/* addr */
    479       1.1  jmcneill 			errno = 0;
    480      1.15      matt 			num = strtoull(optarg, &ep, 0);
    481       1.1  jmcneill 			if (*ep != '\0' || (errno == ERANGE &&
    482      1.15      matt 			    (num == ULLONG_MAX || num == 0)) ||
    483      1.16      matt 			    ((signed long long)num != (int32_t)num &&
    484      1.16      matt 			     num != (uint32_t)num))
    485       1.1  jmcneill 				errx(1, "illegal number -- %s", optarg);
    486       1.1  jmcneill 			image_loadaddr = (uint32_t)num;
    487       1.1  jmcneill 			break;
    488       1.9      matt 		case 'E':	/* ep (byte swapped) */
    489       1.1  jmcneill 		case 'e':	/* ep */
    490       1.1  jmcneill 			errno = 0;
    491      1.15      matt 			num = strtoull(optarg, &ep, 0);
    492       1.1  jmcneill 			if (*ep != '\0' || (errno == ERANGE &&
    493      1.15      matt 			    (num == ULLONG_MAX || num == 0)) ||
    494      1.16      matt 			    ((signed long long)num != (int32_t)num &&
    495      1.16      matt 			     num != (uint32_t)num))
    496       1.1  jmcneill 				errx(1, "illegal number -- %s", optarg);
    497       1.1  jmcneill 			image_entrypoint = (uint32_t)num;
    498       1.9      matt 			if (ch == 'E')
    499       1.9      matt 				image_entrypoint = bswap32(image_entrypoint);
    500       1.1  jmcneill 			break;
    501      1.23  jmcneill 		case 'f':	/* image format */
    502      1.23  jmcneill 			image_format = get_image_format(optarg);
    503      1.23  jmcneill 			break;
    504       1.8       riz 		case 'm':	/* magic */
    505       1.8       riz 			errno = 0;
    506       1.8       riz 			num = strtoul(optarg, &ep, 0);
    507       1.8       riz 			if (*ep != '\0' || (errno == ERANGE &&
    508       1.8       riz 			    (num == ULONG_MAX || num == 0)))
    509       1.8       riz 				errx(1, "illegal number -- %s", optarg);
    510       1.8       riz 			image_magic = (uint32_t)num;
    511      1.22  jmcneill 			break;
    512       1.1  jmcneill 		case 'n':	/* name */
    513       1.1  jmcneill 			image_name = strdup(optarg);
    514       1.1  jmcneill 			break;
    515  1.24.6.1    martin 		case 'u':	/* update image */
    516  1.24.6.1    martin 			update_image = 1;
    517  1.24.6.1    martin 			break;
    518       1.1  jmcneill 		case 'h':
    519       1.1  jmcneill 		default:
    520       1.1  jmcneill 			usage();
    521       1.1  jmcneill 			/* NOTREACHED */
    522       1.1  jmcneill 		}
    523       1.1  jmcneill 	}
    524       1.1  jmcneill 	argc -= optind;
    525       1.1  jmcneill 	argv += optind;
    526       1.1  jmcneill 
    527       1.1  jmcneill 	if (argc != 2)
    528       1.1  jmcneill 		usage();
    529       1.1  jmcneill 
    530       1.4  kiyohara 	if (image_entrypoint == 0)
    531       1.4  kiyohara 		image_entrypoint = image_loadaddr;
    532       1.4  kiyohara 
    533      1.23  jmcneill 	switch (image_format) {
    534      1.23  jmcneill 	case FMT_UIMG:
    535      1.23  jmcneill 		if (image_arch == IH_ARCH_UNKNOWN ||
    536      1.23  jmcneill 		    image_type == IH_TYPE_UNKNOWN ||
    537      1.23  jmcneill 		    image_name == NULL)
    538      1.23  jmcneill 			usage();
    539      1.23  jmcneill 			/* NOTREACHED */
    540       1.1  jmcneill 
    541      1.23  jmcneill 		switch (image_type) {
    542      1.23  jmcneill 		case IH_TYPE_SCRIPT:
    543      1.23  jmcneill 		case IH_TYPE_RAMDISK:
    544      1.23  jmcneill 		case IH_TYPE_KERNEL_NOLOAD:
    545      1.23  jmcneill 			break;
    546      1.23  jmcneill 		default:
    547      1.23  jmcneill 			if (image_loadaddr == 0)
    548      1.23  jmcneill 				usage();
    549      1.23  jmcneill 				/* NOTREACHED */
    550      1.23  jmcneill 			break;
    551      1.23  jmcneill 		}
    552      1.20  jmcneill 		break;
    553      1.23  jmcneill 
    554      1.23  jmcneill 	case FMT_ARM64:
    555      1.23  jmcneill 		if (image_arch != IH_ARCH_UNKNOWN &&
    556      1.23  jmcneill 		    image_arch != IH_ARCH_ARM64)
    557      1.20  jmcneill 			usage();
    558      1.20  jmcneill 			/* NOTREACHED */
    559      1.23  jmcneill 
    560      1.20  jmcneill 		break;
    561      1.23  jmcneill 
    562      1.23  jmcneill 	default:
    563      1.23  jmcneill 		usage();
    564      1.23  jmcneill 		/* NOTREACHED */
    565      1.20  jmcneill 	}
    566      1.20  jmcneill 
    567       1.1  jmcneill 	src = argv[0];
    568       1.1  jmcneill 	dest = argv[1];
    569       1.1  jmcneill 
    570       1.1  jmcneill 	kernel_fd = open(src, O_RDONLY);
    571       1.1  jmcneill 	if (kernel_fd == -1) {
    572       1.1  jmcneill 		perror("open kernel");
    573       1.1  jmcneill 		return EXIT_FAILURE;
    574       1.1  jmcneill 	}
    575      1.11      matt 	image_fd = open(dest, O_WRONLY|O_CREAT|O_TRUNC, 0666);
    576       1.1  jmcneill 	if (image_fd == -1) {
    577       1.1  jmcneill 		perror("open image");
    578       1.1  jmcneill 		return EXIT_FAILURE;
    579       1.1  jmcneill 	}
    580       1.1  jmcneill 
    581      1.23  jmcneill 	printf(" image type:  %s\n", get_image_format_name(image_format));
    582      1.23  jmcneill 
    583      1.23  jmcneill 	switch (image_format) {
    584      1.23  jmcneill 	case FMT_UIMG:
    585      1.23  jmcneill 		if (generate_header_uimg(&hdr_uimg, kernel_fd) != 0)
    586      1.23  jmcneill 			return EXIT_FAILURE;
    587      1.23  jmcneill 
    588      1.23  jmcneill 		if (write_image(&hdr_uimg, sizeof(hdr_uimg),
    589      1.23  jmcneill 		    kernel_fd, image_fd) != 0)
    590      1.23  jmcneill 			return EXIT_FAILURE;
    591      1.23  jmcneill 
    592      1.23  jmcneill 		break;
    593      1.23  jmcneill 	case FMT_ARM64:
    594      1.23  jmcneill 		if (generate_header_arm64(&hdr_arm64, kernel_fd) != 0)
    595      1.23  jmcneill 			return EXIT_FAILURE;
    596      1.23  jmcneill 
    597      1.23  jmcneill 		if (write_image(&hdr_arm64, sizeof(hdr_arm64),
    598      1.23  jmcneill 		    kernel_fd, image_fd) != 0)
    599      1.23  jmcneill 			return EXIT_FAILURE;
    600       1.1  jmcneill 
    601      1.23  jmcneill 		break;
    602      1.23  jmcneill 	default:
    603      1.23  jmcneill 		break;
    604      1.23  jmcneill 	}
    605       1.1  jmcneill 
    606       1.1  jmcneill 	close(image_fd);
    607       1.1  jmcneill 	close(kernel_fd);
    608       1.1  jmcneill 
    609       1.1  jmcneill 	return EXIT_SUCCESS;
    610       1.1  jmcneill }
    611