| 1 | /* |
| 2 | * wndr3700.c - partially based on OpenWrt's add_header.c |
| 3 | * |
| 4 | * Copyright (C) 2009 Anael Orlinski <naouel@naouel.org> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License, |
| 8 | * version 2 as published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * The add_header utility used by various vendors preprends the buf |
| 22 | * image with a header containing a CRC32 value which is generated for the |
| 23 | * model id + reserved space for CRC32 + buf, then replaces the reserved |
| 24 | * area with the actual CRC32. This replacement tool mimics this behavior. |
| 25 | */ |
| 26 | |
| 27 | #include <stdio.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <stddef.h> |
| 30 | #include <unistd.h> |
| 31 | #include <errno.h> |
| 32 | #include <fcntl.h> |
| 33 | #include <sys/mman.h> |
| 34 | #include <string.h> |
| 35 | #include <netinet/in.h> |
| 36 | #include <inttypes.h> |
| 37 | |
| 38 | #define BPB 8 /* bits/byte */ |
| 39 | |
| 40 | static uint32_t crc32[1<<BPB]; |
| 41 | |
| 42 | static void init_crc32() |
| 43 | { |
| 44 | const uint32_t poly = ntohl(0x2083b8ed); |
| 45 | int n; |
| 46 | |
| 47 | for (n = 0; n < 1<<BPB; n++) { |
| 48 | uint32_t crc = n; |
| 49 | int bit; |
| 50 | |
| 51 | for (bit = 0; bit < BPB; bit++) |
| 52 | crc = (crc & 1) ? (poly ^ (crc >> 1)) : (crc >> 1); |
| 53 | crc32[n] = crc; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | static uint32_t crc32buf(unsigned char *buf, size_t len) |
| 58 | { |
| 59 | uint32_t crc = 0xFFFFFFFF; |
| 60 | |
| 61 | for (; len; len--, buf++) |
| 62 | crc = crc32[(uint8_t)crc ^ *buf] ^ (crc >> BPB); |
| 63 | return ~crc; |
| 64 | } |
| 65 | |
| 66 | struct header { |
| 67 | uint32_t magic; |
| 68 | uint32_t crc; |
| 69 | unsigned char stuff[56]; |
| 70 | }; |
| 71 | |
| 72 | static void usage(const char *) __attribute__ (( __noreturn__ )); |
| 73 | |
| 74 | static void usage(const char *mess) |
| 75 | { |
| 76 | fprintf(stderr, "Error: %s\n", mess); |
| 77 | fprintf(stderr, "Usage: wndr3700 input_file output_file\n"); |
| 78 | fprintf(stderr, "\n"); |
| 79 | exit(1); |
| 80 | } |
| 81 | |
| 82 | int main(int argc, char **argv) |
| 83 | { |
| 84 | off_t len; // of original buf |
| 85 | off_t buflen; // of the output file |
| 86 | int fd; |
| 87 | void *input_file; // pointer to the input file (mmmapped) |
| 88 | struct header header; |
| 89 | unsigned char *buf; // pointer to prefix + copy of original buf |
| 90 | |
| 91 | // verify parameters |
| 92 | |
| 93 | if (argc != 3) |
| 94 | usage("wrong number of arguments"); |
| 95 | |
| 96 | // mmap input_file |
| 97 | if ((fd = open(argv[1], O_RDONLY)) < 0 |
| 98 | || (len = lseek(fd, 0, SEEK_END)) < 0 |
| 99 | || (input_file = mmap(0, len, PROT_READ, MAP_SHARED, fd, 0)) == (void *) (-1) |
| 100 | || close(fd) < 0) |
| 101 | { |
| 102 | fprintf(stderr, "Error loading file %s: %s\n", argv[1], strerror(errno)); |
| 103 | exit(1); |
| 104 | } |
| 105 | |
| 106 | buflen = len; |
| 107 | |
| 108 | init_crc32(); |
| 109 | |
| 110 | // preload header |
| 111 | memcpy(&header, input_file, sizeof(header)); |
| 112 | |
| 113 | header.magic = htonl(0x33373030); /* 3700 in ascii */ |
| 114 | header.crc = 0; |
| 115 | |
| 116 | // create a firmware image in memory and copy the input_file to it |
| 117 | buf = malloc(buflen); |
| 118 | memcpy(buf, input_file, len); |
| 119 | |
| 120 | // CRC of temporary header |
| 121 | header.crc = htonl(crc32buf((unsigned char*)&header, sizeof(header))); |
| 122 | |
| 123 | memcpy(buf, &header, sizeof(header)); |
| 124 | |
| 125 | // write the buf |
| 126 | if ((fd = open(argv[2], O_CREAT|O_WRONLY|O_TRUNC,0644)) < 0 |
| 127 | || write(fd, buf, buflen) != buflen |
| 128 | || close(fd) < 0) |
| 129 | { |
| 130 | fprintf(stderr, "Error storing file %s: %s\n", argv[2], strerror(errno)); |
| 131 | exit(2); |
| 132 | } |
| 133 | |
| 134 | free(buf); |
| 135 | |
| 136 | munmap(input_file,len); |
| 137 | |
| 138 | return 0; |
| 139 | } |
| 140 | |