From 6e7ca70449f2fb10007918cdd4616c556ccc2ae1 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rafa=C5=82=20Mi=C5=82ecki?= Date: Wed, 13 Jan 2021 08:11:00 +0100 Subject: [PATCH] firmware-utils: bcm4908kernel: tool adding BCM4908 kernel header MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit BCM4908 CFE bootloader requires kernel to be prepended with a custom header. This simple tool implements support for such headers. Signed-off-by: Rafał Miłecki --- tools/firmware-utils/Makefile | 1 + tools/firmware-utils/src/bcm4908kernel.c | 127 +++++++++++++++++++++++ 2 files changed, 128 insertions(+) create mode 100644 tools/firmware-utils/src/bcm4908kernel.c diff --git a/tools/firmware-utils/Makefile b/tools/firmware-utils/Makefile index 6074ecc608d0..a8c6ab4f6510 100644 --- a/tools/firmware-utils/Makefile +++ b/tools/firmware-utils/Makefile @@ -27,6 +27,7 @@ define Host/Compile $(call cc,add_header) $(call cc,addpattern) $(call cc,asustrx) + $(call cc,bcm4908kernel,-Wall) $(call cc,buffalo-enc buffalo-lib,-Wall) $(call cc,buffalo-tag buffalo-lib,-Wall) $(call cc,buffalo-tftp buffalo-lib,-Wall) diff --git a/tools/firmware-utils/src/bcm4908kernel.c b/tools/firmware-utils/src/bcm4908kernel.c new file mode 100644 index 000000000000..fc5b2812ef3f --- /dev/null +++ b/tools/firmware-utils/src/bcm4908kernel.c @@ -0,0 +1,127 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (C) 2021 Rafał Miłecki + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#if !defined(__BYTE_ORDER) +#error "Unknown byte order" +#endif + +#if __BYTE_ORDER == __BIG_ENDIAN +#define cpu_to_le32(x) bswap_32(x) +#define le32_to_cpu(x) bswap_32(x) +#elif __BYTE_ORDER == __LITTLE_ENDIAN +#define cpu_to_le32(x) (x) +#define le32_to_cpu(x) (x) +#else +#error "Unsupported endianness" +#endif + +struct bcm4908kernel_header { + uint32_t unk1; + uint32_t unk2; + uint32_t length; + uint8_t magic[4]; + uint32_t unused; +}; + +static void usage() { + printf("Usage:\n"); + printf("\n"); + printf("\t-i pathname\t\t\tinput kernel filepath\n"); + printf("\t-o pathname\t\t\toutput kernel filepath\n"); +} + +int main(int argc, char **argv) { + struct bcm4908kernel_header header; + uint8_t buf[1024]; + FILE *out = NULL; + FILE *in = NULL; + size_t length; + size_t bytes; + int err = 0; + char c; + + if (argc >= 2 && !strcmp(argv[1], "-h")) { + usage(); + return 0; + } + + while ((c = getopt(argc, argv, "i:o:")) != -1) { + switch (c) { + case 'i': + in = fopen(optarg, "r"); + break; + case 'o': + out = fopen(optarg, "w+"); + break; + } + } + + if (!in || !out) { + fprintf(stderr, "Failed to open input and/or output file\n"); + usage(); + return -EINVAL; + } + + if (fread(&header, 1, sizeof(header), in) != sizeof(header)) { + fprintf(stderr, "Failed to read %zu bytes from input file\n", sizeof(header)); + err = -EIO; + goto err_close; + } + + if (!memcmp(header.magic, "BRCM", 4)) { + fprintf(stderr, "Input file already contains BCM4908 kernel header\n"); + err = -EIO; + goto err_close; + } + + err = fseek(out, sizeof(header), SEEK_SET); + if (err) { + err = -errno; + fprintf(stderr, "Failed to fseek(): %d\n", err); + goto err_close; + } + + length = 0; + rewind(in); + while ((bytes = fread(buf, 1, sizeof(buf), in)) > 0) { + if (fwrite(buf, 1, bytes, out) != bytes) { + fprintf(stderr, "Failed to write %zu B to the output file\n", bytes); + err = -EIO; + goto err_close; + } + length += bytes; + } + + header.unk1 = cpu_to_le32(0x00080000); + header.unk2 = cpu_to_le32(0x00080000); + header.length = cpu_to_le32(length); + header.magic[0] = 'B'; + header.magic[1] = 'R'; + header.magic[2] = 'C'; + header.magic[3] = 'M'; + header.unused = 0; + + fseek(out, 0, SEEK_SET); + + if (fwrite(&header, 1, sizeof(header), out) != sizeof(header)) { + fprintf(stderr, "Failed to write header to the output file\n"); + err = -EIO; + goto err_close; + } + +err_close: + fclose(out); + fclose(in); + return err; +} -- 2.30.2