From ff8987a16b5e7f997ef2b6d128259279d99b2a5f Mon Sep 17 00:00:00 2001 From: kaloz Date: Fri, 27 Jun 2008 12:18:31 +0000 Subject: [PATCH] replacement of the add_header utility used by various vendors git-svn-id: svn://svn.openwrt.org/openwrt/trunk@11588 3c298f89-4303-0410-b956-a3cf2f4a3e73 --- tools/firmware-utils/Makefile | 1 + tools/firmware-utils/src/add_header.c | 138 ++++++++++++++++++++++++++ 2 files changed, 139 insertions(+) create mode 100644 tools/firmware-utils/src/add_header.c diff --git a/tools/firmware-utils/Makefile b/tools/firmware-utils/Makefile index acb429c44..e0f60c7f9 100644 --- a/tools/firmware-utils/Makefile +++ b/tools/firmware-utils/Makefile @@ -31,6 +31,7 @@ define Build/Compile $(call cc,mkcasfw) $(call cc,mkfwimage,-lz) $(call cc,imagetag) + $(call cc,add_header) endef define Build/Install diff --git a/tools/firmware-utils/src/add_header.c b/tools/firmware-utils/src/add_header.c new file mode 100644 index 000000000..946068c39 --- /dev/null +++ b/tools/firmware-utils/src/add_header.c @@ -0,0 +1,138 @@ +/* + * add_header.c - partially based on OpenWrt's motorola-bin.c + * + * Copyright (C) 2008 Imre Kaloz + * Gabor Juhos + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License, + * version 2 as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +/* + * 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define BPB 8 /* bits/byte */ + +static uint32_t crc32[1<> 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 { + unsigned 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,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; +} -- 2.35.1