aboutsummaryrefslogtreecommitdiffstats
path: root/tools/firmware-utils/src/add_header.c
diff options
context:
space:
mode:
authorPetr Štetiar <ynezz@true.cz>2021-07-05 11:54:26 +0200
committerRafał Miłecki <rafal@milecki.pl>2021-10-05 16:20:10 +0200
commit8cc9a74a3f6bf363645efda6db417f8dadd3d844 (patch)
tree68f1648a077df8e49328f087eccaf94c2e47d1e8 /tools/firmware-utils/src/add_header.c
parentf82c93b93c0a021921ac7a30ba6e7a090c7ddd1c (diff)
downloadupstream-8cc9a74a3f6bf363645efda6db417f8dadd3d844.tar.gz
upstream-8cc9a74a3f6bf363645efda6db417f8dadd3d844.tar.bz2
upstream-8cc9a74a3f6bf363645efda6db417f8dadd3d844.zip
firmware-utils: update to version 2021-10-05
Includes following changes: db65821f006c cmake: fix missing install target 3a0cfc856991 Add initial GitLab CI support 8f47adea6f87 Add missing includes for byte swap operations fbafae9f8037 Convert to CMake based project Additionaly moves source code into separate Git project repository and converts the package build to utilize CMake. Signed-off-by: Petr Štetiar <ynezz@true.cz> [rmilecki: rebase, update to the latest repo git & rm -r src] Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
Diffstat (limited to 'tools/firmware-utils/src/add_header.c')
-rw-r--r--tools/firmware-utils/src/add_header.c126
1 files changed, 0 insertions, 126 deletions
diff --git a/tools/firmware-utils/src/add_header.c b/tools/firmware-utils/src/add_header.c
deleted file mode 100644
index a5ad5f4119..0000000000
--- a/tools/firmware-utils/src/add_header.c
+++ /dev/null
@@ -1,126 +0,0 @@
-// SPDX-License-Identifier: GPL-2.0-only
-/*
- * add_header.c - partially based on OpenWrt's motorola-bin.c
- *
- * Copyright (C) 2008 Imre Kaloz <kaloz@openwrt.org>
- * Gabor Juhos <juhosg@openwrt.org>
- */
-
-/*
- * The add_header utility used by various vendors preprends the buf
- * image with a header containing a CRC32 value which is generated for the
- * model id + reserved space for CRC32 + buf, then replaces the reserved
- * area with the actual CRC32. This replacement tool mimics this behavior.
- */
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <stddef.h>
-#include <unistd.h>
-#include <errno.h>
-#include <fcntl.h>
-#include <sys/mman.h>
-#include <string.h>
-#include <netinet/in.h>
-#include <inttypes.h>
-
-#define BPB 8 /* bits/byte */
-
-static uint32_t crc32[1<<BPB];
-
-static void init_crc32()
-{
- const uint32_t poly = ntohl(0x2083b8ed);
- int n;
-
- for (n = 0; n < 1<<BPB; n++) {
- uint32_t crc = n;
- int bit;
-
- for (bit = 0; bit < BPB; bit++)
- crc = (crc & 1) ? (poly ^ (crc >> 1)) : (crc >> 1);
- crc32[n] = crc;
- }
-}
-
-static uint32_t crc32buf(unsigned char *buf, size_t len)
-{
- uint32_t crc = 0xFFFFFFFF;
-
- for (; len; len--, buf++)
- crc = crc32[(uint8_t)crc ^ *buf] ^ (crc >> BPB);
- return ~crc;
-}
-
-struct header {
- char model[16];
- uint32_t crc;
-};
-
-static void usage(const char *) __attribute__ (( __noreturn__ ));
-
-static void usage(const char *mess)
-{
- fprintf(stderr, "Error: %s\n", mess);
- fprintf(stderr, "Usage: add_header model_id input_file output_file\n");
- fprintf(stderr, "\n");
- exit(1);
-}
-
-int main(int argc, char **argv)
-{
- off_t len; // of original buf
- off_t buflen; // of the output file
- int fd;
- void *input_file; // pointer to the input file (mmmapped)
- struct header header;
- unsigned char *buf; // pointer to prefix + copy of original buf
-
- // verify parameters
-
- if (argc != 4)
- usage("wrong number of arguments");
-
- // mmap input_file
- if ((fd = open(argv[2], O_RDONLY)) < 0
- || (len = lseek(fd, 0, SEEK_END)) < 0
- || (input_file = mmap(0, len, PROT_READ, MAP_SHARED, fd, 0)) == (void *) (-1)
- || close(fd) < 0)
- {
- fprintf(stderr, "Error loading file %s: %s\n", argv[2], strerror(errno));
- exit(1);
- }
-
- buflen = len + sizeof(header);
-
- init_crc32();
-
- // copy model name into header
- strncpy(header.model, argv[1], sizeof(header.model));
- header.crc = 0;
-
- // create a firmware image in memory and copy the input_file to it
- buf = malloc(buflen);
- memcpy(buf, &header, sizeof(header));
- memcpy(&buf[sizeof(header)], input_file, len);
-
- // CRC of temporary header + buf
- header.crc = htonl(crc32buf(buf, buflen));
-
- memcpy(buf, &header, sizeof(header));
-
- // write the buf
- if ((fd = open(argv[3], O_CREAT|O_WRONLY|O_TRUNC,0644)) < 0
- || write(fd, buf, buflen) != buflen
- || close(fd) < 0)
- {
- fprintf(stderr, "Error storing file %s: %s\n", argv[3], strerror(errno));
- exit(2);
- }
-
- free(buf);
-
- munmap(input_file,len);
-
- return 0;
-}