mkmerakifw.c 6.9 KB

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