firmware-utils: when using open with O_CREAT and O_WRONLY, also use O_TRUNC to ensure...
[openwrt/svn-archive/archive.git] / tools / firmware-utils / src / add_header.c
1 /*
2 * add_header.c - partially based on OpenWrt's motorola-bin.c
3 *
4 * Copyright (C) 2008 Imre Kaloz <kaloz@openwrt.org>
5 * Gabor Juhos <juhosg@openwrt.org>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License,
9 * version 2 as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21 /*
22 * The add_header utility used by various vendors preprends the buf
23 * image with a header containing a CRC32 value which is generated for the
24 * model id + reserved space for CRC32 + buf, then replaces the reserved
25 * area with the actual CRC32. This replacement tool mimics this behavior.
26 */
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <stddef.h>
31 #include <unistd.h>
32 #include <errno.h>
33 #include <fcntl.h>
34 #include <sys/mman.h>
35 #include <string.h>
36 #include <netinet/in.h>
37 #include <inttypes.h>
38
39 #define BPB 8 /* bits/byte */
40
41 static uint32_t crc32[1<<BPB];
42
43 static void init_crc32()
44 {
45 const uint32_t poly = ntohl(0x2083b8ed);
46 int n;
47
48 for (n = 0; n < 1<<BPB; n++) {
49 uint32_t crc = n;
50 int bit;
51
52 for (bit = 0; bit < BPB; bit++)
53 crc = (crc & 1) ? (poly ^ (crc >> 1)) : (crc >> 1);
54 crc32[n] = crc;
55 }
56 }
57
58 static uint32_t crc32buf(unsigned char *buf, size_t len)
59 {
60 uint32_t crc = 0xFFFFFFFF;
61
62 for (; len; len--, buf++)
63 crc = crc32[(uint8_t)crc ^ *buf] ^ (crc >> BPB);
64 return ~crc;
65 }
66
67 struct header {
68 unsigned char model[16];
69 uint32_t crc;
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: add_header model_id 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 != 4)
94 usage("wrong number of arguments");
95
96 // mmap input_file
97 if ((fd = open(argv[2], 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[2], strerror(errno));
103 exit(1);
104 }
105
106 buflen = len + sizeof(header);
107
108 init_crc32();
109
110 // copy model name into header
111 strncpy(header.model, argv[1], sizeof(header.model));
112 header.crc = 0;
113
114 // create a firmware image in memory and copy the input_file to it
115 buf = malloc(buflen);
116 memcpy(buf, &header, sizeof(header));
117 memcpy(&buf[sizeof(header)], input_file, len);
118
119 // CRC of temporary header + buf
120 header.crc = htonl(crc32buf(buf, buflen));
121
122 memcpy(buf, &header, sizeof(header));
123
124 // write the buf
125 if ((fd = open(argv[3], O_CREAT|O_WRONLY|O_TRUNC,0644)) < 0
126 || write(fd, buf, buflen) != buflen
127 || close(fd) < 0)
128 {
129 fprintf(stderr, "Error storing file %s: %s\n", argv[3], strerror(errno));
130 exit(2);
131 }
132
133 free(buf);
134
135 munmap(input_file,len);
136
137 return 0;
138 }