OpenWrt – Blame information for rev 2
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /* |
2 | * Copyright (C) 2015 Thomas Hebb <tommyhebb@gmail.com> |
||
3 | * |
||
4 | * The format of the header this tool generates was first documented by |
||
5 | * Chris Blake <chrisrblake93 (at) gmail.com> in a shell script of the |
||
6 | * same purpose. I have created this reimplementation at his request. The |
||
7 | * original script can be found at: |
||
8 | * <https://github.com/riptidewave93/meraki-partbuilder> |
||
9 | * |
||
10 | * This program is free software; you can redistribute it and/or modify it |
||
11 | * under the terms of the GNU General Public License version 2 as published |
||
12 | * by the Free Software Foundation. |
||
13 | * |
||
14 | */ |
||
15 | |||
16 | #include <stdio.h> |
||
17 | #include <stdlib.h> |
||
18 | #include <stdbool.h> |
||
19 | #include <string.h> |
||
20 | #include <libgen.h> |
||
21 | #include <getopt.h> |
||
22 | #include <errno.h> |
||
23 | #include <arpa/inet.h> |
||
24 | |||
25 | #include "sha1.h" |
||
26 | |||
27 | #define PADDING_BYTE 0xff |
||
28 | |||
29 | #define HDR_LENGTH 0x00000400 |
||
30 | #define HDR_OFF_MAGIC1 0 |
||
31 | #define HDR_OFF_HDRLEN 4 |
||
32 | #define HDR_OFF_IMAGELEN 8 |
||
33 | #define HDR_OFF_CHECKSUM 12 |
||
34 | #define HDR_OFF_MAGIC2 32 |
||
35 | #define HDR_OFF_MAGIC3 36 |
||
36 | #define HDR_OFF_STATICHASH 40 |
||
37 | #define HDR_OFF_KERNEL_OFFSET 40 |
||
38 | #define HDR_OFF_RAMDISK_OFFSET 44 |
||
39 | #define HDR_OFF_FDT_OFFSET 48 |
||
40 | #define HDR_OFF_UNKNOWN_OFFSET 52 |
||
41 | |||
42 | struct board_info { |
||
43 | uint32_t magic1; |
||
44 | uint32_t magic2; |
||
45 | uint32_t magic3; |
||
46 | uint32_t imagelen; |
||
47 | union { |
||
48 | unsigned char statichash[20]; |
||
49 | struct { |
||
50 | uint32_t kernel_offset; |
||
51 | uint32_t ramdisk_offset; |
||
52 | uint32_t fdt_offset; |
||
53 | uint32_t unknown_offset; |
||
54 | } mx60; |
||
55 | } u; |
||
56 | char *id; |
||
57 | char *description; |
||
58 | }; |
||
59 | |||
60 | /* |
||
61 | * Globals |
||
62 | */ |
||
63 | static char *progname; |
||
64 | |||
65 | static char *board_id; |
||
66 | static const struct board_info *board; |
||
67 | |||
68 | static const struct board_info boards[] = { |
||
69 | { |
||
70 | .id = "mr18", |
||
71 | .description = "Meraki MR18 Access Point", |
||
72 | .magic1 = 0x8e73ed8a, |
||
73 | .magic2 = 0x8e73ed8a, |
||
74 | .imagelen = 0x00800000, |
||
75 | .u.statichash = {0xda, 0x39, 0xa3, 0xee, 0x5e, |
||
76 | 0x6b, 0x4b, 0x0d, 0x32, 0x55, |
||
77 | 0xbf, 0xef, 0x95, 0x60, 0x18, |
||
78 | 0x90, 0xaf, 0xd8, 0x07, 0x09}, |
||
79 | }, { |
||
80 | .id = "mr24", |
||
81 | .description = "Meraki MR24 Access Point", |
||
82 | .magic1 = 0x8e73ed8a, |
||
83 | .magic2 = 0x8e73ed8a, |
||
84 | .imagelen = 0x00800000, |
||
85 | .u.statichash = {0xff, 0xff, 0xff, 0xff, 0xff, |
||
86 | 0xff, 0xff, 0xff, 0xff, 0xff, |
||
87 | 0xff, 0xff, 0xff, 0xff, 0xff, |
||
88 | 0xff, 0xff, 0xff, 0xff, 0xff}, |
||
89 | }, { |
||
90 | .id = "mx60", |
||
91 | .description = "Meraki MX60/MX60W Security Appliance", |
||
92 | .magic1 = 0x8e73ed8a, |
||
93 | .magic2 = 0xa1f0beef, /* Enables use of load addr in statichash */ |
||
94 | .magic3 = 0x00060001, /* This goes along with magic2 */ |
||
95 | .imagelen = 0x3fd00000, |
||
96 | /* The static hash below does the following: |
||
97 | * 1st Row: Kernel Offset |
||
98 | * 2nd Row: Ramdisk Offset |
||
99 | * 3rd Row: FDT Offset |
||
100 | * 4th Row: ? Unused/Unknown ? |
||
101 | * 5th Row: ? Unused/Unknown ? |
||
102 | */ |
||
103 | .u.mx60 = { |
||
104 | .kernel_offset = 0x10000, |
||
105 | .ramdisk_offset = 0x3FFC00, |
||
106 | .fdt_offset = 0x0400, |
||
107 | .unknown_offset = 0x0400, |
||
108 | }, |
||
109 | }, { |
||
110 | /* terminating entry */ |
||
111 | } |
||
112 | }; |
||
113 | |||
114 | /* |
||
115 | * Message macros |
||
116 | */ |
||
117 | #define ERR(fmt, ...) do { \ |
||
118 | fflush(0); \ |
||
119 | fprintf(stderr, "[%s] *** error: " fmt "\n", \ |
||
120 | progname, ## __VA_ARGS__); \ |
||
121 | } while (0) |
||
122 | |||
123 | #define ERRS(fmt, ...) do { \ |
||
124 | int save = errno; \ |
||
125 | fflush(0); \ |
||
126 | fprintf(stderr, "[%s] *** error: " fmt "\n", \ |
||
127 | progname, ## __VA_ARGS__, strerror(save)); \ |
||
128 | } while (0) |
||
129 | |||
130 | static const struct board_info *find_board(const char *id) |
||
131 | { |
||
132 | const struct board_info *ret; |
||
133 | const struct board_info *board; |
||
134 | |||
135 | ret = NULL; |
||
136 | for (board = boards; board->id != NULL; board++) { |
||
137 | if (strcasecmp(id, board->id) == 0) { |
||
138 | ret = board; |
||
139 | break; |
||
140 | } |
||
141 | } |
||
142 | |||
143 | return ret; |
||
144 | } |
||
145 | |||
146 | static void usage(int status) |
||
147 | { |
||
148 | FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout; |
||
149 | const struct board_info *board; |
||
150 | |||
151 | fprintf(stream, "Usage: %s [OPTIONS...]\n", progname); |
||
152 | fprintf(stream, |
||
153 | "\n" |
||
154 | "Options:\n" |
||
155 | " -B <board> create image for the board specified with <board>\n" |
||
156 | " -i <file> read kernel image from the file <file>\n" |
||
157 | " -o <file> write output to the file <file>\n" |
||
158 | " -s strip padding from the end of the image\n" |
||
159 | " -h show this screen\n" |
||
160 | ); |
||
161 | |||
162 | fprintf(stream, "\nBoards:\n"); |
||
163 | for (board = boards; board->id != NULL; board++) |
||
164 | fprintf(stream, " %-16s%s\n", board->id, board->description); |
||
165 | |||
166 | exit(status); |
||
167 | } |
||
168 | |||
169 | void writel(unsigned char *buf, size_t offset, uint32_t value) |
||
170 | { |
||
171 | value = htonl(value); |
||
172 | memcpy(buf + offset, &value, sizeof(uint32_t)); |
||
173 | } |
||
174 | |||
175 | int main(int argc, char *argv[]) |
||
176 | { |
||
177 | int ret = EXIT_FAILURE; |
||
178 | long klen; |
||
179 | size_t kspace; |
||
180 | unsigned char *kernel; |
||
181 | size_t buflen; |
||
182 | unsigned char *buf; |
||
183 | bool strip_padding = false; |
||
184 | char *ofname = NULL, *ifname = NULL; |
||
185 | FILE *out, *in; |
||
186 | |||
187 | progname = basename(argv[0]); |
||
188 | |||
189 | while (1) { |
||
190 | int c; |
||
191 | |||
192 | c = getopt(argc, argv, "B:i:o:sh"); |
||
193 | if (c == -1) |
||
194 | break; |
||
195 | |||
196 | switch (c) { |
||
197 | case 'B': |
||
198 | board_id = optarg; |
||
199 | break; |
||
200 | case 'i': |
||
201 | ifname = optarg; |
||
202 | break; |
||
203 | case 'o': |
||
204 | ofname = optarg; |
||
205 | break; |
||
206 | case 's': |
||
207 | strip_padding = true; |
||
208 | break; |
||
209 | case 'h': |
||
210 | usage(EXIT_SUCCESS); |
||
211 | break; |
||
212 | default: |
||
213 | usage(EXIT_FAILURE); |
||
214 | break; |
||
215 | } |
||
216 | } |
||
217 | |||
218 | if (board_id == NULL) { |
||
219 | ERR("no board specified"); |
||
220 | goto err; |
||
221 | } |
||
222 | |||
223 | board = find_board(board_id); |
||
224 | if (board == NULL) { |
||
225 | ERR("unknown board \"%s\"", board_id); |
||
226 | goto err; |
||
227 | } |
||
228 | |||
229 | if (ifname == NULL) { |
||
230 | ERR("no input file specified"); |
||
231 | goto err; |
||
232 | } |
||
233 | |||
234 | if (ofname == NULL) { |
||
235 | ERR("no output file specified"); |
||
236 | goto err; |
||
237 | } |
||
238 | |||
239 | in = fopen(ifname, "r"); |
||
240 | if (in == NULL) { |
||
241 | ERRS("could not open \"%s\" for reading: %s", ifname); |
||
242 | goto err; |
||
243 | } |
||
244 | |||
245 | buflen = board->imagelen; |
||
246 | kspace = buflen - HDR_LENGTH; |
||
247 | |||
248 | /* Get kernel length */ |
||
249 | fseek(in, 0, SEEK_END); |
||
250 | klen = ftell(in); |
||
251 | rewind(in); |
||
252 | |||
253 | if (klen > kspace) { |
||
254 | ERR("file \"%s\" is too big - max size: 0x%08lX\n", |
||
255 | ifname, kspace); |
||
256 | goto err_close_in; |
||
257 | } |
||
258 | |||
259 | /* If requested, resize buffer to remove padding */ |
||
260 | if (strip_padding) |
||
261 | buflen = klen + HDR_LENGTH; |
||
262 | |||
263 | /* Allocate and initialize buffer for final image */ |
||
264 | buf = malloc(buflen); |
||
265 | if (buf == NULL) { |
||
266 | ERRS("no memory for buffer: %s\n"); |
||
267 | goto err_close_in; |
||
268 | } |
||
269 | memset(buf, PADDING_BYTE, buflen); |
||
270 | |||
271 | /* Load kernel */ |
||
272 | kernel = buf + HDR_LENGTH; |
||
273 | fread(kernel, klen, 1, in); |
||
274 | |||
275 | /* Write magic values */ |
||
276 | writel(buf, HDR_OFF_MAGIC1, board->magic1); |
||
277 | writel(buf, HDR_OFF_MAGIC2, board->magic2); |
||
278 | writel(buf, HDR_OFF_MAGIC3, board->magic3); |
||
279 | |||
280 | /* Write header and image length */ |
||
281 | writel(buf, HDR_OFF_HDRLEN, HDR_LENGTH); |
||
282 | writel(buf, HDR_OFF_IMAGELEN, klen); |
||
283 | |||
284 | /* Write checksum and static hash */ |
||
285 | sha1_csum(kernel, klen, buf + HDR_OFF_CHECKSUM); |
||
286 | |||
287 | switch (board->magic2) { |
||
288 | case 0xa1f0beef: |
||
289 | writel(buf, HDR_OFF_KERNEL_OFFSET, board->u.mx60.kernel_offset); |
||
290 | writel(buf, HDR_OFF_RAMDISK_OFFSET, board->u.mx60.ramdisk_offset); |
||
291 | writel(buf, HDR_OFF_FDT_OFFSET, board->u.mx60.fdt_offset), |
||
292 | writel(buf, HDR_OFF_UNKNOWN_OFFSET, board->u.mx60.unknown_offset); |
||
293 | break; |
||
294 | |||
295 | case 0x8e73ed8a: |
||
296 | memcpy(buf + HDR_OFF_STATICHASH, board->u.statichash, 20); |
||
297 | break; |
||
298 | } |
||
299 | |||
300 | /* Save finished image */ |
||
301 | out = fopen(ofname, "w"); |
||
302 | if (out == NULL) { |
||
303 | ERRS("could not open \"%s\" for writing: %s", ofname); |
||
304 | goto err_free; |
||
305 | } |
||
306 | fwrite(buf, buflen, 1, out); |
||
307 | |||
308 | ret = EXIT_SUCCESS; |
||
309 | |||
310 | fclose(out); |
||
311 | |||
312 | err_free: |
||
313 | free(buf); |
||
314 | |||
315 | err_close_in: |
||
316 | fclose(in); |
||
317 | |||
318 | err: |
||
319 | return ret; |
||
320 | } |