61b81c6b5f3a0ec295750a95b48ae8580123bb82
[openwrt/openwrt.git] / tools / firmware-utils / src / mkmerakifw.c
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_FILLER 36
36 #define HDR_OFF_STATICHASH 40
37
38 struct board_info {
39 uint32_t magic;
40 uint32_t imagelen;
41 unsigned char statichash[20];
42 char *id;
43 char *description;
44 };
45
46 /*
47 * Globals
48 */
49 static char *progname;
50
51 static char *board_id;
52 static const struct board_info *board;
53
54 static const struct board_info boards[] = {
55 {
56 .id = "mr18",
57 .description = "Meraki MR18 Access Point",
58 .magic = 0x8e73ed8a,
59 .imagelen = 0x00800000,
60 .statichash = {0xda, 0x39, 0xa3, 0xee, 0x5e,
61 0x6b, 0x4b, 0x0d, 0x32, 0x55,
62 0xbf, 0xef, 0x95, 0x60, 0x18,
63 0x90, 0xaf, 0xd8, 0x07, 0x09},
64 }, {
65 .id = "mr24",
66 .description = "Meraki MR24 Access Point",
67 .magic = 0x8e73ed8a,
68 .imagelen = 0x00800000,
69 .statichash = {0xff, 0xff, 0xff, 0xff, 0xff,
70 0xff, 0xff, 0xff, 0xff, 0xff,
71 0xff, 0xff, 0xff, 0xff, 0xff,
72 0xff, 0xff, 0xff, 0xff, 0xff},
73 }, {
74
75 /* terminating entry */
76 }
77 };
78
79 /*
80 * Message macros
81 */
82 #define ERR(fmt, ...) do { \
83 fflush(0); \
84 fprintf(stderr, "[%s] *** error: " fmt "\n", \
85 progname, ## __VA_ARGS__); \
86 } while (0)
87
88 #define ERRS(fmt, ...) do { \
89 int save = errno; \
90 fflush(0); \
91 fprintf(stderr, "[%s] *** error: " fmt "\n", \
92 progname, ## __VA_ARGS__, strerror(save)); \
93 } while (0)
94
95 static const struct board_info *find_board(const char *id)
96 {
97 const struct board_info *ret;
98 const struct board_info *board;
99
100 ret = NULL;
101 for (board = boards; board->id != NULL; board++) {
102 if (strcasecmp(id, board->id) == 0) {
103 ret = board;
104 break;
105 }
106 }
107
108 return ret;
109 }
110
111 static void usage(int status)
112 {
113 FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
114 const struct board_info *board;
115
116 fprintf(stream, "Usage: %s [OPTIONS...]\n", progname);
117 fprintf(stream,
118 "\n"
119 "Options:\n"
120 " -B <board> create image for the board specified with <board>\n"
121 " -i <file> read kernel image from the file <file>\n"
122 " -o <file> write output to the file <file>\n"
123 " -s strip padding from the end of the image\n"
124 " -h show this screen\n"
125 );
126
127 fprintf(stream, "\nBoards:\n");
128 for (board = boards; board->id != NULL; board++)
129 fprintf(stream, " %-16s%s\n", board->id, board->description);
130
131 exit(status);
132 }
133
134 void writel(unsigned char *buf, size_t offset, uint32_t value)
135 {
136 value = htonl(value);
137 memcpy(buf + offset, &value, sizeof(uint32_t));
138 }
139
140 int main(int argc, char *argv[])
141 {
142 int ret = EXIT_FAILURE;
143 long klen;
144 size_t kspace;
145 unsigned char *kernel;
146 size_t buflen;
147 unsigned char *buf;
148 bool strip_padding = false;
149 char *ofname = NULL, *ifname = NULL;
150 FILE *out, *in;
151
152 progname = basename(argv[0]);
153
154 while (1) {
155 int c;
156
157 c = getopt(argc, argv, "B:i:o:sh");
158 if (c == -1)
159 break;
160
161 switch (c) {
162 case 'B':
163 board_id = optarg;
164 break;
165 case 'i':
166 ifname = optarg;
167 break;
168 case 'o':
169 ofname = optarg;
170 break;
171 case 's':
172 strip_padding = true;
173 break;
174 case 'h':
175 usage(EXIT_SUCCESS);
176 break;
177 default:
178 usage(EXIT_FAILURE);
179 break;
180 }
181 }
182
183 if (board_id == NULL) {
184 ERR("no board specified");
185 goto err;
186 }
187
188 board = find_board(board_id);
189 if (board == NULL) {
190 ERR("unknown board \"%s\"", board_id);
191 goto err;
192 }
193
194 if (ifname == NULL) {
195 ERR("no input file specified");
196 goto err;
197 }
198
199 if (ofname == NULL) {
200 ERR("no output file specified");
201 goto err;
202 }
203
204 in = fopen(ifname, "r");
205 if (in == NULL) {
206 ERRS("could not open \"%s\" for reading: %s", ifname);
207 goto err;
208 }
209
210 buflen = board->imagelen;
211 kspace = buflen - HDR_LENGTH;
212
213 /* Get kernel length */
214 fseek(in, 0, SEEK_END);
215 klen = ftell(in);
216 rewind(in);
217
218 if (klen > kspace) {
219 ERR("file \"%s\" is too big - max size: 0x%08lX\n",
220 ifname, kspace);
221 goto err_close_in;
222 }
223
224 /* If requested, resize buffer to remove padding */
225 if (strip_padding)
226 buflen = klen + HDR_LENGTH;
227
228 /* Allocate and initialize buffer for final image */
229 buf = malloc(buflen);
230 if (buf == NULL) {
231 ERRS("no memory for buffer: %s\n");
232 goto err_close_in;
233 }
234 memset(buf, PADDING_BYTE, buflen);
235
236 /* Load kernel */
237 kernel = buf + HDR_LENGTH;
238 fread(kernel, klen, 1, in);
239
240 /* Write magic values and filler */
241 writel(buf, HDR_OFF_MAGIC1, board->magic);
242 writel(buf, HDR_OFF_MAGIC2, board->magic);
243 writel(buf, HDR_OFF_FILLER, 0);
244
245 /* Write header and image length */
246 writel(buf, HDR_OFF_HDRLEN, HDR_LENGTH);
247 writel(buf, HDR_OFF_IMAGELEN, klen);
248
249 /* Write checksum and static hash */
250 sha1_csum(kernel, klen, buf + HDR_OFF_CHECKSUM);
251 memcpy(buf + HDR_OFF_STATICHASH, board->statichash, 20);
252
253 /* Save finished image */
254 out = fopen(ofname, "w");
255 if (out == NULL) {
256 ERRS("could not open \"%s\" for writing: %s", ofname);
257 goto err_free;
258 }
259 fwrite(buf, buflen, 1, out);
260
261 ret = EXIT_SUCCESS;
262
263 fclose(out);
264
265 err_free:
266 free(buf);
267
268 err_close_in:
269 fclose(in);
270
271 err:
272 return ret;
273 }