ramips: define some magic values in the rt288x pci code
[openwrt.git] / tools / firmware-utils / src / mkplanexfw.c
1 /*
2 * Copyright (C) 2009 Gabor Juhos <juhosg@openwrt.org>
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 as published
6 * by the Free Software Foundation.
7 *
8 */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <stdint.h>
13 #include <string.h>
14 #include <unistd.h> /* for unlink() */
15 #include <libgen.h>
16 #include <getopt.h> /* for getopt() */
17 #include <stdarg.h>
18 #include <errno.h>
19 #include <sys/stat.h>
20
21 #include "sha1.h"
22
23 #if (__BYTE_ORDER == __BIG_ENDIAN)
24 # define HOST_TO_BE32(x) (x)
25 # define BE32_TO_HOST(x) (x)
26 #else
27 # define HOST_TO_BE32(x) bswap_32(x)
28 # define BE32_TO_HOST(x) bswap_32(x)
29 #endif
30
31
32 struct planex_hdr {
33 uint8_t sha1sum[20];
34 char version[8];
35 uint8_t unk1[2];
36 uint32_t datalen;
37 } __attribute__ ((packed));
38
39 struct board_info {
40 char *id;
41 uint32_t seed;
42 uint8_t unk[2];
43 };
44
45 /*
46 * Globals
47 */
48 static char *ifname;
49 static char *progname;
50 static char *ofname;
51 static char *version = "1.00.00";
52
53 static char *board_id;
54 static struct board_info *board;
55
56 static struct board_info boards[] = {
57 {
58 .id = "MZK-W04NU",
59 .seed = 2,
60 .unk = {0x04, 0x08},
61 }, {
62 .id = "MZK-W300NH",
63 .seed = 4,
64 .unk = {0x00, 0x00},
65 }, {
66 /* terminating entry */
67 }
68 };
69
70 /*
71 * Message macros
72 */
73 #define ERR(fmt, ...) do { \
74 fflush(0); \
75 fprintf(stderr, "[%s] *** error: " fmt "\n", \
76 progname, ## __VA_ARGS__ ); \
77 } while (0)
78
79 #define ERRS(fmt, ...) do { \
80 int save = errno; \
81 fflush(0); \
82 fprintf(stderr, "[%s] *** error: " fmt "\n", \
83 progname, ## __VA_ARGS__, strerror(save)); \
84 } while (0)
85
86 static struct board_info *find_board(char *id)
87 {
88 struct board_info *ret;
89 struct board_info *board;
90
91 ret = NULL;
92 for (board = boards; board->id != NULL; board++){
93 if (strcasecmp(id, board->id) == 0) {
94 ret = board;
95 break;
96 }
97 };
98
99 return ret;
100 }
101
102 void usage(int status)
103 {
104 FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
105 struct board_info *board;
106
107 fprintf(stream, "Usage: %s [OPTIONS...]\n", progname);
108 fprintf(stream,
109 "\n"
110 "Options:\n"
111 " -B <board> create image for the board specified with <board>\n"
112 " -i <file> read input from the file <file>\n"
113 " -o <file> write output to the file <file>\n"
114 " -v <version> set image version to <version>\n"
115 " -h show this screen\n"
116 );
117
118 exit(status);
119 }
120
121 int main(int argc, char *argv[])
122 {
123 int res = EXIT_FAILURE;
124 int buflen;
125 int err;
126 struct stat st;
127 char *buf;
128 struct planex_hdr *hdr;
129 sha1_context ctx;
130 uint32_t seed;
131
132 FILE *outfile, *infile;
133
134 progname = basename(argv[0]);
135
136 while ( 1 ) {
137 int c;
138
139 c = getopt(argc, argv, "B:i:o:v:h");
140 if (c == -1)
141 break;
142
143 switch (c) {
144 case 'B':
145 board_id = optarg;
146 break;
147 case 'i':
148 ifname = optarg;
149 break;
150 case 'o':
151 ofname = optarg;
152 break;
153 case 'v':
154 version = optarg;
155 break;
156 case 'h':
157 usage(EXIT_SUCCESS);
158 break;
159 default:
160 usage(EXIT_FAILURE);
161 break;
162 }
163 }
164
165 if (board_id == NULL) {
166 ERR("no board specified");
167 goto err;
168 }
169
170 board = find_board(board_id);
171 if (board == NULL) {
172 ERR("unknown board '%s'", board_id);
173 goto err;
174 };
175
176 if (ifname == NULL) {
177 ERR("no input file specified");
178 goto err;
179 }
180
181 if (ofname == NULL) {
182 ERR("no output file specified");
183 goto err;
184 }
185
186 err = stat(ifname, &st);
187 if (err){
188 ERRS("stat failed on %s", ifname);
189 goto err;
190 }
191
192 buflen = (st.st_size + 3) & ~3;
193 buflen += sizeof(*hdr);
194
195 buf = malloc(buflen);
196 if (!buf) {
197 ERR("no memory for buffer\n");
198 goto err;
199 }
200
201 memset(buf, 0xff, buflen);
202 hdr = (struct planex_hdr *)buf;
203
204 hdr->datalen = HOST_TO_BE32(buflen - sizeof(*hdr));
205 hdr->unk1[0] = board->unk[0];
206 hdr->unk1[1] = board->unk[1];
207
208 snprintf(hdr->version, sizeof(hdr->version), "%s", version);
209
210 infile = fopen(ifname, "r");
211 if (infile == NULL) {
212 ERRS("could not open \"%s\" for reading", ifname);
213 goto err_free;
214 }
215
216 errno = 0;
217 fread(buf + sizeof(*hdr), st.st_size, 1, infile);
218 if (errno != 0) {
219 ERRS("unable to read from file %s", ifname);
220 goto err_close_in;
221 }
222
223 seed = HOST_TO_BE32(board->seed);
224 sha1_starts(&ctx);
225 sha1_update(&ctx, (uchar *) &seed, sizeof(seed));
226 sha1_update(&ctx, buf + sizeof(*hdr), buflen - sizeof(*hdr));
227 sha1_finish(&ctx, hdr->sha1sum);
228
229 outfile = fopen(ofname, "w");
230 if (outfile == NULL) {
231 ERRS("could not open \"%s\" for writing", ofname);
232 goto err_close_in;
233 }
234
235 errno = 0;
236 fwrite(buf, buflen, 1, outfile);
237 if (errno) {
238 ERRS("unable to write to file %s", ofname);
239 goto err_close_out;
240 }
241
242 res = EXIT_SUCCESS;
243
244 out_flush:
245 fflush(outfile);
246
247 err_close_out:
248 fclose(outfile);
249 if (res != EXIT_SUCCESS) {
250 unlink(ofname);
251 }
252
253 err_close_in:
254 fclose(infile);
255
256 err_free:
257 free(buf);
258
259 err:
260 return res;
261 }
262
This page took 0.065886 seconds and 5 git commands to generate.