Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/perseant-stdc-iso10646]: src/usr.bin/mkubootimage 3317810



details:   https://anonhg.NetBSD.org/src/rev/8a669c24583e
branches:  perseant-stdc-iso10646
changeset: 850634:8a669c24583e
user:      jmcneill <jmcneill%NetBSD.org@localhost>
date:      Sat Jul 15 11:13:09 2017 +0000

description:
3317810

diffstat:

 usr.bin/mkubootimage/mkubootimage.c |  472 ++++++++++++++++++++++++++++++++++++
 usr.bin/mkubootimage/uboot.h        |   86 ++++++
 2 files changed, 558 insertions(+), 0 deletions(-)

diffs (truncated from 566 to 300 lines):

diff -r 94afd6d1e1ed -r 8a669c24583e usr.bin/mkubootimage/mkubootimage.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/usr.bin/mkubootimage/mkubootimage.c       Sat Jul 15 11:13:09 2017 +0000
@@ -0,0 +1,472 @@
+/* $NetBSD: mkubootimage.c,v 1.20.2.2 2017/07/15 11:13:09 jmcneill Exp $ */
+
+/*-
+ * Copyright (c) 2010 Jared D. McNeill <jmcneill%invisible.ca@localhost>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#if HAVE_NBTOOL_CONFIG_H
+#include "nbtool_config.h"
+#endif
+
+#include <sys/cdefs.h>
+__RCSID("$NetBSD: mkubootimage.c,v 1.20.2.2 2017/07/15 11:13:09 jmcneill Exp $");
+
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/endian.h>
+#include <sys/uio.h>
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+#include "uboot.h"
+
+#ifndef __arraycount
+#define __arraycount(__x)      (sizeof(__x) / sizeof(__x[0]))
+#endif
+
+extern uint32_t crc32(const void *, size_t);
+extern uint32_t crc32v(const struct iovec *, int);
+
+static enum uboot_image_os image_os = IH_OS_NETBSD;
+static enum uboot_image_arch image_arch = IH_ARCH_UNKNOWN;
+static enum uboot_image_type image_type = IH_TYPE_UNKNOWN;
+static enum uboot_image_comp image_comp = IH_COMP_NONE;
+static uint32_t image_loadaddr = 0;
+static uint32_t image_entrypoint = 0;
+static char *image_name;
+static uint32_t image_magic = IH_MAGIC;
+
+static const struct uboot_os {
+       enum uboot_image_os os;
+       const char *name;
+} uboot_os[] = {
+       { IH_OS_OPENBSD,        "openbsd" },
+       { IH_OS_NETBSD,         "netbsd" },
+       { IH_OS_FREEBSD,        "freebsd" },
+       { IH_OS_LINUX,          "linux" },
+};
+
+static enum uboot_image_os
+get_os(const char *name)
+{
+       unsigned int i;
+
+       for (i = 0; i < __arraycount(uboot_os); i++) {
+               if (strcmp(uboot_os[i].name, name) == 0)
+                       return uboot_os[i].os;
+       }
+
+       return IH_OS_UNKNOWN;
+}
+
+static const char *
+get_os_name(enum uboot_image_os os)
+{
+       unsigned int i;
+
+       for (i = 0; i < __arraycount(uboot_os); i++) {
+               if (uboot_os[i].os == os)
+                       return uboot_os[i].name;
+       }
+
+       return "Unknown";
+}
+
+static const struct uboot_arch {
+       enum uboot_image_arch arch;
+       const char *name;
+} uboot_arch[] = {
+       { IH_ARCH_ARM,          "arm" },
+       { IH_ARCH_I386,         "i386" },
+       { IH_ARCH_MIPS,         "mips" },
+       { IH_ARCH_MIPS64,       "mips64" },
+       { IH_ARCH_PPC,          "powerpc" },
+       { IH_ARCH_OPENRISC,     "or1k" },
+       { IH_ARCH_ARM64,        "arm64" },
+};
+
+static enum uboot_image_arch
+get_arch(const char *name)
+{
+       unsigned int i;
+
+       for (i = 0; i < __arraycount(uboot_arch); i++) {
+               if (strcmp(uboot_arch[i].name, name) == 0)
+                       return uboot_arch[i].arch;
+       }
+
+       return IH_ARCH_UNKNOWN;
+}
+
+static const char * 
+get_arch_name(enum uboot_image_arch arch)
+{
+       unsigned int i;
+
+       for (i = 0; i < __arraycount(uboot_arch); i++) {
+               if (uboot_arch[i].arch == arch)
+                       return uboot_arch[i].name;
+       }
+
+       return "Unknown";
+}
+
+static const struct uboot_type {
+       enum uboot_image_type type;
+       const char *name;
+} uboot_type[] = {
+       { IH_TYPE_STANDALONE,           "standalone" },
+       { IH_TYPE_KERNEL,               "kernel" },
+       { IH_TYPE_KERNEL_NOLOAD,        "kernel_noload" },
+       { IH_TYPE_RAMDISK,              "ramdisk" },
+       { IH_TYPE_FILESYSTEM,           "fs" },
+       { IH_TYPE_SCRIPT,               "script" },
+};
+
+static enum uboot_image_type
+get_type(const char *name)
+{
+       unsigned int i;
+
+       for (i = 0; i < __arraycount(uboot_type); i++) {
+               if (strcmp(uboot_type[i].name, name) == 0)
+                       return uboot_type[i].type;
+       }
+
+       return IH_TYPE_UNKNOWN;
+}
+
+static const char *
+get_type_name(enum uboot_image_type type)
+{
+       unsigned int i;
+
+       for (i = 0; i < __arraycount(uboot_type); i++) {
+               if (uboot_type[i].type == type)
+                       return uboot_type[i].name;
+       }
+
+       return "Unknown";
+}
+
+static const struct uboot_comp {
+       enum uboot_image_comp comp;
+       const char *name;
+} uboot_comp[] = {
+       { IH_COMP_NONE,         "none" },
+       { IH_COMP_GZIP,         "gz" },
+       { IH_COMP_BZIP2,        "bz2" },
+       { IH_COMP_LZMA,         "lzma" },
+       { IH_COMP_LZO,          "lzo" },
+};
+
+static enum uboot_image_comp
+get_comp(const char *name)
+{
+       unsigned int i;
+
+       for (i = 0; i < __arraycount(uboot_comp); i++) {
+               if (strcmp(uboot_comp[i].name, name) == 0)
+                       return uboot_comp[i].comp;
+       }
+
+       return IH_TYPE_UNKNOWN;
+}
+
+static const char *
+get_comp_name(enum uboot_image_comp comp)
+{
+       unsigned int i;
+
+       for (i = 0; i < __arraycount(uboot_comp); i++) {
+               if (uboot_comp[i].comp == comp)
+                       return uboot_comp[i].name;
+       }
+
+       return "Unknown";
+}
+
+__dead static void
+usage(void)
+{
+       fprintf(stderr, "usage: mkubootimage -A "
+           "<arm|arm64|i386|mips|mips64|or1k|powerpc>");
+       fprintf(stderr, " -C <none|bz2|gz|lzma|lzo>");
+       fprintf(stderr, " -O <openbsd|netbsd|freebsd|linux>");
+       fprintf(stderr, " -T <standalone|kernel|kernel_noload|ramdisk|fs|script>");
+       fprintf(stderr, " -a <addr> [-e <ep>] [-m <magic>] -n <name>");
+       fprintf(stderr, " <srcfile> <dstfile>\n");
+
+       exit(EXIT_FAILURE);
+}
+
+static void
+dump_header(struct uboot_image_header *hdr)
+{
+       time_t tm = ntohl(hdr->ih_time);
+
+       printf(" magic:       0x%08x\n", ntohl(hdr->ih_magic));
+       printf(" time:        %s", ctime(&tm));
+       printf(" size:        %u\n", ntohl(hdr->ih_size));
+       printf(" load addr:   0x%08x\n", ntohl(hdr->ih_load));
+       printf(" entry point: 0x%08x\n", ntohl(hdr->ih_ep));
+       printf(" data crc:    0x%08x\n", ntohl(hdr->ih_dcrc));
+       printf(" os:          %d (%s)\n", hdr->ih_os,
+           get_os_name(hdr->ih_os));
+       printf(" arch:        %d (%s)\n", hdr->ih_arch,
+           get_arch_name(hdr->ih_arch));
+       printf(" type:        %d (%s)\n", hdr->ih_type,
+           get_type_name(hdr->ih_type));
+       printf(" comp:        %d (%s)\n", hdr->ih_comp,
+           get_comp_name(hdr->ih_comp));
+       printf(" name:        %s\n", hdr->ih_name);
+       printf(" header crc:  0x%08x\n", hdr->ih_hcrc);
+}
+
+static int
+generate_header(struct uboot_image_header *hdr, int kernel_fd)
+{
+       uint8_t *p;
+       struct stat st;
+       uint32_t crc, dsize, size_buf[2];
+       int error;
+
+       error = fstat(kernel_fd, &st);
+       if (error == -1) {
+               perror("stat");
+               return errno;
+       }
+
+       if (st.st_size + sizeof(*hdr) > UINT32_MAX) {
+               fprintf(stderr, "fatal: kernel too big\n");
+               return EINVAL;
+       }
+
+       p = mmap(0, st.st_size, PROT_READ, MAP_FILE|MAP_SHARED, kernel_fd, 0);
+       if (p == MAP_FAILED) {
+               perror("mmap kernel");
+               return EINVAL;
+       }
+       if (image_type == IH_TYPE_SCRIPT) {
+               struct iovec iov[3];
+               dsize = st.st_size + (sizeof(uint32_t) * 2);
+               size_buf[0] = htonl(st.st_size);
+               size_buf[1] = htonl(0);
+               iov[0].iov_base = &size_buf[0];
+               iov[0].iov_len = sizeof(size_buf[0]);
+               iov[1].iov_base = &size_buf[1];
+               iov[1].iov_len = sizeof(size_buf[1]);
+               iov[2].iov_base = p;
+               iov[2].iov_len = st.st_size;
+               crc = crc32v(iov, 3);
+       } else {
+               dsize = st.st_size;
+               crc = crc32(p, st.st_size);
+       }
+       munmap(p, st.st_size);
+



Home | Main Index | Thread Index | Old Index