Module Name: src Committed By: martin Date: Sun Dec 15 14:38:14 UTC 2024
Modified Files: src/usr.bin/mkubootimage [netbsd-10]: crc32.c mkubootimage.1 mkubootimage.c Added Files: src/usr.bin/mkubootimage [netbsd-10]: crc32.h Log Message: Pull up following revision(s) (requested by gutteridge in ticket #1023): usr.bin/mkubootimage/mkubootimage.1: revision 1.15 usr.bin/mkubootimage/crc32.c: revision 1.5 usr.bin/mkubootimage/mkubootimage.c: revision 1.31 usr.bin/mkubootimage/mkubootimage.c: revision 1.34 usr.bin/mkubootimage/crc32.h: revision 1.1 PR/57914: Jan-Benedict Glaw: Set reproducible timestamp Cleanup lint. mkubootimage.c: consistently use endian.h origin functions Since this already uses bswap32(3), le64toh(3), etc., switch to also use htobe32(3) and be32toh(3) instead of htonl(3) and ntohl(3). This seems the best way to fix build breakages under recent Linux distros where GCC objects to missing prototypes (since otherwise we'd need to include arpa/inet.h, which wasn't declared). Another issue related to PR lib/58674. Confirmed this fixes builds on Fedora 40 (since all these functions are provided by NetBSD compat). No changes noted to resulting installation images generated for armv7hf on a NetBSD build. To generate a diff of this commit: cvs rdiff -u -r1.4 -r1.4.40.1 src/usr.bin/mkubootimage/crc32.c cvs rdiff -u -r0 -r1.2.2.2 src/usr.bin/mkubootimage/crc32.h cvs rdiff -u -r1.14 -r1.14.8.1 src/usr.bin/mkubootimage/mkubootimage.1 cvs rdiff -u -r1.30 -r1.30.8.1 src/usr.bin/mkubootimage/mkubootimage.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/usr.bin/mkubootimage/crc32.c diff -u src/usr.bin/mkubootimage/crc32.c:1.4 src/usr.bin/mkubootimage/crc32.c:1.4.40.1 --- src/usr.bin/mkubootimage/crc32.c:1.4 Sat Dec 29 16:18:08 2012 +++ src/usr.bin/mkubootimage/crc32.c Sun Dec 15 14:38:14 2024 @@ -1,4 +1,4 @@ -/* $NetBSD: crc32.c,v 1.4 2012/12/29 16:18:08 jmcneill Exp $ */ +/* $NetBSD: crc32.c,v 1.4.40.1 2024/12/15 14:38:14 martin Exp $ */ /*- * Copyright (c) 2002 Marcel Moolenaar @@ -33,14 +33,12 @@ #endif #include <sys/cdefs.h> -__RCSID("$NetBSD: crc32.c,v 1.4 2012/12/29 16:18:08 jmcneill Exp $"); +__RCSID("$NetBSD: crc32.c,v 1.4.40.1 2024/12/15 14:38:14 martin Exp $"); #include <sys/types.h> #include <sys/uio.h> #include <stdint.h> - -uint32_t crc32(const void *, size_t); -uint32_t crc32v(const struct iovec *, int); +#include "crc32.h" static uint32_t crc32_tab[] = { 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, @@ -99,6 +97,7 @@ crc32v(const struct iovec *iov, int cnt) for (i = 0; i < cnt; i++) { p = iov[i].iov_base; +/*###102 [lint] warning conversion from 'unsigned long' to 'int' may lose accuracy [132]%%%*/ len = iov[i].iov_len; while (len--) crc = crc32_tab[(crc ^ *p++) & 0xFF] ^ (crc >> 8); Index: src/usr.bin/mkubootimage/mkubootimage.1 diff -u src/usr.bin/mkubootimage/mkubootimage.1:1.14 src/usr.bin/mkubootimage/mkubootimage.1:1.14.8.1 --- src/usr.bin/mkubootimage/mkubootimage.1:1.14 Sat Dec 7 12:34:17 2019 +++ src/usr.bin/mkubootimage/mkubootimage.1 Sun Dec 15 14:38:14 2024 @@ -1,4 +1,4 @@ -.\" $NetBSD: mkubootimage.1,v 1.14 2019/12/07 12:34:17 wiz Exp $ +.\" $NetBSD: mkubootimage.1,v 1.14.8.1 2024/12/15 14:38:14 martin Exp $ .\" .\" Copyright (c) 2012 The NetBSD Foundation, Inc. .\" All rights reserved. @@ -27,7 +27,7 @@ .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE .\" POSSIBILITY OF SUCH DAMAGE. .\" -.Dd December 4, 2019 +.Dd February 7, 2024 .Dt MKUBOOTIMAGE 1 .Os .Sh NAME @@ -119,6 +119,9 @@ This is required. Defines the operating system type. The default OS name is .Qq netbsd . +.It Fl t Ar epoch +Use given epoch timestamp as image creation time. +(This is only used for legacy U-Boot images.) .It Fl T No ( fs Ns | Ns kernel Ns | Ns kernel_noload Ns | Ns ramdisk Ns | Ns script Ns | Ns standalone ) Defines the image type. This is required for Index: src/usr.bin/mkubootimage/mkubootimage.c diff -u src/usr.bin/mkubootimage/mkubootimage.c:1.30 src/usr.bin/mkubootimage/mkubootimage.c:1.30.8.1 --- src/usr.bin/mkubootimage/mkubootimage.c:1.30 Sat Feb 8 13:27:00 2020 +++ src/usr.bin/mkubootimage/mkubootimage.c Sun Dec 15 14:38:14 2024 @@ -1,4 +1,4 @@ -/* $NetBSD: mkubootimage.c,v 1.30 2020/02/08 13:27:00 ryo Exp $ */ +/* $NetBSD: mkubootimage.c,v 1.30.8.1 2024/12/15 14:38:14 martin Exp $ */ /*- * Copyright (c) 2010 Jared D. McNeill <jmcne...@invisible.ca> @@ -30,7 +30,7 @@ #endif #include <sys/cdefs.h> -__RCSID("$NetBSD: mkubootimage.c,v 1.30 2020/02/08 13:27:00 ryo Exp $"); +__RCSID("$NetBSD: mkubootimage.c,v 1.30.8.1 2024/12/15 14:38:14 martin Exp $"); #include <sys/mman.h> #include <sys/stat.h> @@ -51,6 +51,7 @@ __RCSID("$NetBSD: mkubootimage.c,v 1.30 #include "uboot.h" #include "arm64.h" +#include "crc32.h" #ifndef __arraycount #define __arraycount(__x) (sizeof(__x) / sizeof(__x[0])) @@ -62,9 +63,6 @@ enum image_format { FMT_ARM64, /* Linux ARM64 image (booti) */ }; -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; @@ -264,12 +262,12 @@ get_comp_name(enum uboot_image_comp comp __dead static void usage(void) { - fprintf(stderr, "usage: mkubootimage [-hu] -A " - "<arm|arm64|i386|mips|mips64|or1k|powerpc|sh> -a address\n"); - fprintf(stderr, "\t-C <bz2|gz|lzma|lzo|none> [-E address] [-e address]\n"); - fprintf(stderr, "\t[-f <arm64|uimg>] [-m magic] -n image -O <freebsd|linux|netbsd|openbsd>\n"); - fprintf(stderr, "\t-T <fs|kernel|kernel_noload|ramdisk|script|standalone>\n"); - fprintf(stderr, "\tsource destination\n"); + fprintf(stderr, +"Usage: %s [-hu] -A <arm|arm64|i386|mips|mips64|or1k|powerpc|sh> -a address\n" +"\t-C <bz2|gz|lzma|lzo|none> [-E address] [-e address] [-t timestamp]\n" +"\t[-f <arm64|uimg>] [-m magic] -n image -O <freebsd|linux|netbsd|openbsd>\n" +"\t-T <fs|kernel|kernel_noload|ramdisk|script|standalone>\n" +"\tsource destination\n", getprogname()); exit(EXIT_FAILURE); } @@ -277,14 +275,14 @@ usage(void) static void dump_header_uimg(struct uboot_image_header *hdr) { - time_t tm = ntohl(hdr->ih_time); + time_t tm = be32toh(hdr->ih_time); - printf(" magic: 0x%08x\n", ntohl(hdr->ih_magic)); + printf(" magic: 0x%08x\n", be32toh(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(" size: %u\n", be32toh(hdr->ih_size)); + printf(" load addr: 0x%08x\n", be32toh(hdr->ih_load)); + printf(" entry point: 0x%08x\n", be32toh(hdr->ih_ep)); + printf(" data crc: 0x%08x\n", be32toh(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, @@ -298,7 +296,8 @@ dump_header_uimg(struct uboot_image_head } static int -generate_header_uimg(struct uboot_image_header *hdr, int kernel_fd) +generate_header_uimg(struct uboot_image_header *hdr, time_t repro_time, + int kernel_fd) { uint8_t *p; struct stat st; @@ -323,9 +322,9 @@ generate_header_uimg(struct uboot_image_ } 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); + dsize = (uint32_t)(st.st_size + (sizeof(uint32_t) * 2)); + size_buf[0] = htobe32(st.st_size); + size_buf[1] = htobe32(0); iov[0].iov_base = &size_buf[0]; iov[0].iov_len = sizeof(size_buf[0]); iov[1].iov_base = &size_buf[1]; @@ -334,26 +333,26 @@ generate_header_uimg(struct uboot_image_ iov[2].iov_len = st.st_size; crc = crc32v(iov, 3); } else { - dsize = update_image ? - (uint32_t)st.st_size - sizeof(*hdr) : (uint32_t)st.st_size; + dsize = update_image ? (uint32_t)(st.st_size - sizeof(*hdr)) : + (uint32_t)st.st_size; crc = crc32(p, st.st_size); } munmap(p, st.st_size); memset(hdr, 0, sizeof(*hdr)); - hdr->ih_magic = htonl(image_magic); - hdr->ih_time = htonl(st.st_mtime); - hdr->ih_size = htonl(dsize); - hdr->ih_load = htonl(image_loadaddr); - hdr->ih_ep = htonl(image_entrypoint); - hdr->ih_dcrc = htonl(crc); + hdr->ih_magic = htobe32(image_magic); + hdr->ih_time = htobe32(repro_time ? repro_time : st.st_mtime); + hdr->ih_size = htobe32(dsize); + hdr->ih_load = htobe32(image_loadaddr); + hdr->ih_ep = htobe32(image_entrypoint); + hdr->ih_dcrc = htobe32(crc); hdr->ih_os = image_os; hdr->ih_arch = image_arch; hdr->ih_type = image_type; hdr->ih_comp = image_comp; strlcpy((char *)hdr->ih_name, image_name, sizeof(hdr->ih_name)); crc = crc32((void *)hdr, sizeof(*hdr)); - hdr->ih_hcrc = htonl(crc); + hdr->ih_hcrc = htobe32(crc); dump_header_uimg(hdr); @@ -425,8 +424,8 @@ write_image(void *hdr, size_t hdrlen, in } if (image_type == IH_TYPE_SCRIPT) { - size_buf[0] = htonl(st.st_size); - size_buf[1] = htonl(0); + size_buf[0] = htobe32(st.st_size); + size_buf[1] = htobe32(0); wlen = write(image_fd, &size_buf, sizeof(size_buf)); if (wlen != sizeof(size_buf)) { perror("short write"); @@ -462,8 +461,9 @@ main(int argc, char *argv[]) int kernel_fd, image_fd; int ch; unsigned long long num; + time_t repro_time = 0; - while ((ch = getopt(argc, argv, "A:C:E:O:T:a:e:f:hm:n:u")) != -1) { + while ((ch = getopt(argc, argv, "A:C:E:O:T:a:e:f:hm:n:t:u")) != -1) { switch (ch) { case 'A': /* arch */ image_arch = get_arch(optarg); @@ -514,6 +514,9 @@ main(int argc, char *argv[]) case 'n': /* name */ image_name = strdup(optarg); break; + case 't': /* FS timestamp */ + repro_time = atoll(optarg); + break; case 'u': /* update image */ update_image = 1; break; @@ -584,7 +587,7 @@ main(int argc, char *argv[]) switch (image_format) { case FMT_UIMG: - if (generate_header_uimg(&hdr_uimg, kernel_fd) != 0) + if (generate_header_uimg(&hdr_uimg, repro_time, kernel_fd) != 0) return EXIT_FAILURE; if (write_image(&hdr_uimg, sizeof(hdr_uimg), Added files: Index: src/usr.bin/mkubootimage/crc32.h diff -u /dev/null src/usr.bin/mkubootimage/crc32.h:1.2.2.2 --- /dev/null Sun Dec 15 14:38:14 2024 +++ src/usr.bin/mkubootimage/crc32.h Sun Dec 15 14:38:14 2024 @@ -0,0 +1,28 @@ +/*- + * Copyright (c) 2023 The NetBSD Foundation, Inc. + * 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. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``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 FOUNDATION OR CONTRIBUTORS + * 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. + */ + +uint32_t crc32(const void *, size_t); +uint32_t crc32v(const struct iovec *, int);