OpenWrt – Diff between revs 2 and 3
?pathlinks?
Rev 2 | Rev 3 | |||
---|---|---|---|---|
Line 15... | Line 15... | |||
15 | #include <getopt.h> |
15 | #include <getopt.h> |
|
16 | #include <errno.h> |
16 | #include <errno.h> |
|
17 | #include <sys/stat.h> |
17 | #include <sys/stat.h> |
|
18 | #include <endian.h> /* for __BYTE_ORDER */ |
18 | #include <endian.h> /* for __BYTE_ORDER */ |
|
Line 19... | Line -... | |||
19 | |
- | ||
20 | #define FALSE 0 |
- | ||
21 | #define TRUE 1 |
- | ||
22 | |
19 | |
|
23 | #if (__BYTE_ORDER == __LITTLE_ENDIAN) |
20 | #if (__BYTE_ORDER == __LITTLE_ENDIAN) |
|
24 | # define HOST_TO_LE16(x) (x) |
21 | # define HOST_TO_LE16(x) (x) |
|
25 | # define HOST_TO_LE32(x) (x) |
- | ||
26 | # define HOST_TO_BE16(x) bswap_16(x) |
- | ||
27 | # define HOST_TO_BE32(x) bswap_32(x) |
22 | # define HOST_TO_LE32(x) (x) |
|
28 | #else |
23 | #else |
|
29 | # define HOST_TO_LE16(x) bswap_16(x) |
24 | # define HOST_TO_LE16(x) bswap_16(x) |
|
30 | # define HOST_TO_LE32(x) bswap_32(x) |
- | ||
31 | # define HOST_TO_BE16(x) (x) |
- | ||
32 | # define HOST_TO_BE32(x) (x) |
25 | # define HOST_TO_LE32(x) bswap_32(x) |
|
Line 33... | Line 26... | |||
33 | #endif |
26 | #endif |
|
34 | |
27 | |
|
35 | struct header |
28 | struct header |
|
Line 52... | Line 45... | |||
52 | char *start; |
45 | char *start; |
|
53 | size_t size; |
46 | size_t size; |
|
54 | }; |
47 | }; |
|
Line 55... | Line 48... | |||
55 | |
48 | |
|
56 | static char *progname; |
- | ||
Line 57... | Line 49... | |||
57 | static int force_be = FALSE; |
49 | static char *progname; |
|
58 | |
50 | |
|
59 | static void usage(int status) |
51 | static void usage(int status) |
|
Line 67... | Line 59... | |||
67 | " -s <sig> set image signature to <sig>\n" |
59 | " -s <sig> set image signature to <sig>\n" |
|
68 | " -m <model> set model to <model>\n" |
60 | " -m <model> set model to <model>\n" |
|
69 | " -i <file> read input from file <file>\n" |
61 | " -i <file> read input from file <file>\n" |
|
70 | " -o <file> write output to file <file>\n" |
62 | " -o <file> write output to file <file>\n" |
|
71 | " -f <flash> set flash address to <flash>\n" |
63 | " -f <flash> set flash address to <flash>\n" |
|
72 | " -S <start> set start address to <start>\n" |
64 | " -S <start> set start address to <start>\n"); |
|
73 | " -b big-endianness mode\n"); |
- | ||
Line 74... | Line 65... | |||
74 | |
65 | |
|
75 | exit(status); |
66 | exit(status); |
|
Line 76... | Line 67... | |||
76 | } |
67 | } |
|
Line 90... | Line 81... | |||
90 | |
81 | |
|
91 | static unsigned short fwcsum (struct buf *buf) { |
82 | static unsigned short fwcsum (struct buf *buf) { |
|
92 | int i; |
83 | int i; |
|
Line 93... | Line 84... | |||
93 | unsigned short ret = 0; |
84 | unsigned short ret = 0; |
|
94 | |
- | ||
95 | for (i = 0; i < buf->size / 2; i++) { |
85 | |
|
96 | if (force_be == FALSE) |
- | ||
97 | ret -= ((unsigned short *) buf->start)[i]; |
- | ||
98 | else |
- | ||
Line 99... | Line 86... | |||
99 | ret -= HOST_TO_BE16(((unsigned short *) buf->start)[i]); |
86 | for (i = 0; i < buf->size / 2; i++) |
|
100 | } |
87 | ret -= ((unsigned short *) buf->start)[i]; |
|
Line 101... | Line 88... | |||
101 | |
88 | |
|
Line 155... | Line 142... | |||
155 | |
142 | |
|
156 | ifinfo.name = ofinfo.name = NULL; |
143 | ifinfo.name = ofinfo.name = NULL; |
|
157 | header.flash = header.size = header.start = 0; |
144 | header.flash = header.size = header.start = 0; |
|
Line 158... | Line 145... | |||
158 | progname = basename(argv[0]); |
145 | progname = basename(argv[0]); |
|
159 | |
146 | |
|
160 | while((c = getopt(argc, argv, "i:o:m:s:f:S:h:b")) != -1) { |
147 | while((c = getopt(argc, argv, "i:o:m:s:f:S:h")) != -1) { |
|
161 | switch (c) { |
148 | switch (c) { |
|
162 | case 'i': |
149 | case 'i': |
|
163 | ifinfo.name = optarg; |
150 | ifinfo.name = optarg; |
|
Line 192... | Line 179... | |||
192 | if (!strtou32(optarg, &header.start)) { |
179 | if (!strtou32(optarg, &header.start)) { |
|
193 | fprintf(stderr, "invalid start address specified\n"); |
180 | fprintf(stderr, "invalid start address specified\n"); |
|
194 | usage(EXIT_FAILURE); |
181 | usage(EXIT_FAILURE); |
|
195 | } |
182 | } |
|
196 | break; |
183 | break; |
|
197 | case 'b': |
- | ||
198 | force_be = TRUE; |
- | ||
199 | break; |
- | ||
200 | default: |
184 | default: |
|
201 | usage(EXIT_FAILURE); |
185 | usage(EXIT_FAILURE); |
|
202 | break; |
186 | break; |
|
203 | } |
187 | } |
|
204 | } |
188 | } |
|
Line 255... | Line 239... | |||
255 | ibuf.start = obuf.start + sizeof(struct header); |
239 | ibuf.start = obuf.start + sizeof(struct header); |
|
Line 256... | Line 240... | |||
256 | |
240 | |
|
257 | if (fwread(&ifinfo, &ibuf)) |
241 | if (fwread(&ifinfo, &ibuf)) |
|
Line 258... | Line -... | |||
258 | usage(EXIT_FAILURE); |
- | ||
259 | |
242 | usage(EXIT_FAILURE); |
|
260 | if (force_be == FALSE) { |
243 | |
|
261 | header.flash = HOST_TO_LE32(header.flash); |
244 | header.flash = HOST_TO_LE32(header.flash); |
|
262 | header.size = HOST_TO_LE32(obuf.size - sizeof(struct header)); |
- | ||
263 | header.start = HOST_TO_LE32(header.start); |
- | ||
264 | } else { |
- | ||
265 | header.flash = HOST_TO_BE32(header.flash); |
- | ||
266 | header.size = HOST_TO_BE32(obuf.size - sizeof(struct header)); |
- | ||
267 | header.start = HOST_TO_BE32(header.start); |
- | ||
268 | } |
245 | header.size = HOST_TO_LE32(obuf.size - sizeof(struct header)); |
|
Line 269... | Line -... | |||
269 | |
- | ||
270 | memcpy (obuf.start, &header, sizeof(struct header)); |
246 | header.start = HOST_TO_LE32(header.start); |
|
271 | |
- | ||
272 | if (force_be == FALSE) |
- | ||
273 | csum = HOST_TO_LE16(fwcsum(&ibuf)); |
- | ||
274 | else |
247 | memcpy (obuf.start, &header, sizeof(struct header)); |
|
275 | csum = HOST_TO_BE16(fwcsum(&ibuf)); |
248 | |
|
Line 276... | Line 249... | |||
276 | |
249 | csum = HOST_TO_LE16(fwcsum(&ibuf)); |