2
0

mktplinkfw-kernel.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /*
  2. * Copyright (C) 2009 Gabor Juhos <juhosg@openwrt.org>
  3. * Copyright (C) 2016 Tal Keren <kooolk@gmail.com>
  4. *
  5. * Stripped down version of the regular tplink firmware that is only used
  6. * for compressing and booting the kernel.
  7. *
  8. * This tool was based on:
  9. * TP-Link WR941 V2 firmware checksum fixing tool.
  10. * Copyright (C) 2008,2009 Wang Jian <lark@linux.net.cn>
  11. *
  12. * This program is free software; you can redistribute it and/or modify it
  13. * under the terms of the GNU General Public License version 2 as published
  14. * by the Free Software Foundation.
  15. *
  16. */
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <stdint.h>
  20. #include <string.h>
  21. #include <unistd.h> /* for unlink() */
  22. #include <libgen.h>
  23. #include <getopt.h> /* for getopt() */
  24. #include <stdarg.h>
  25. #include <errno.h>
  26. #include <sys/stat.h>
  27. #include <arpa/inet.h>
  28. #include <netinet/in.h>
  29. #define ALIGN(x,a) ({ typeof(a) __a = (a); (((x) + __a - 1) & ~(__a - 1)); })
  30. #define HEADER_VERSION_V1 0x01000000
  31. #define MD5SUM_LEN 16
  32. struct file_info {
  33. char *file_name; /* name of the file */
  34. uint32_t file_size; /* length of the file */
  35. };
  36. struct fw_header {
  37. uint32_t version; /* header version */
  38. char vendor_name[24];
  39. char fw_version[36];
  40. uint32_t hw_id; /* hardware id */
  41. uint32_t hw_rev; /* hardware revision */
  42. uint32_t unk1;
  43. uint8_t md5sum1[MD5SUM_LEN];
  44. uint32_t unk2;
  45. uint8_t md5sum2[MD5SUM_LEN];
  46. uint32_t unk3;
  47. uint32_t kernel_la; /* kernel load address */
  48. uint32_t kernel_ep; /* kernel entry point */
  49. uint32_t fw_length; /* total length of the firmware */
  50. uint32_t kernel_ofs; /* kernel data offset */
  51. uint32_t kernel_len; /* kernel data length */
  52. uint32_t rootfs_ofs; /* rootfs data offset */
  53. uint32_t rootfs_len; /* rootfs data length */
  54. uint32_t boot_ofs; /* bootloader data offset */
  55. uint32_t boot_len; /* bootloader data length */
  56. uint16_t ver_hi;
  57. uint16_t ver_mid;
  58. uint16_t ver_lo;
  59. uint8_t pad[354];
  60. } __attribute__ ((packed));
  61. /*
  62. * Globals
  63. */
  64. static char *ofname;
  65. static char *progname;
  66. static char *vendor = "TP-LINK Technologies";
  67. static char *version = "ver. 1.0";
  68. static char *fw_ver = "0.0.0";
  69. static uint32_t hdr_ver = HEADER_VERSION_V1;
  70. static char *opt_hw_id;
  71. static uint32_t hw_id = 0;
  72. static struct file_info kernel_info;
  73. static uint32_t kernel_la = 0;
  74. static uint32_t kernel_ep = 0;
  75. static uint32_t kernel_len = 0;
  76. /*
  77. * Message macros
  78. */
  79. #define ERR(fmt, ...) do { \
  80. fflush(0); \
  81. fprintf(stderr, "[%s] *** error: " fmt "\n", \
  82. progname, ## __VA_ARGS__ ); \
  83. } while (0)
  84. #define ERRS(fmt, ...) do { \
  85. int save = errno; \
  86. fflush(0); \
  87. fprintf(stderr, "[%s] *** error: " fmt ": %s\n", \
  88. progname, ## __VA_ARGS__, strerror(save)); \
  89. } while (0)
  90. #define DBG(fmt, ...) do { \
  91. fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
  92. } while (0)
  93. static void usage(int status)
  94. {
  95. FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
  96. struct board_info *board;
  97. fprintf(stream, "Usage: %s [OPTIONS...]\n", progname);
  98. fprintf(stream,
  99. "\n"
  100. "Options:\n"
  101. " -E <ep> kernel entry point with <ep> (hexval prefixed with 0x)\n"
  102. " -L <la> kernel load address with <la> (hexval prefixed with 0x)\n"
  103. " -H <hwid> use hardware id specified with <hwid>\n"
  104. " -k <file> read kernel image from the file <file>\n"
  105. " -o <file> write output to the file <file>\n"
  106. " -N <vendor> set image vendor to <vendor>\n"
  107. " -V <version> set image version to <version>\n"
  108. " -h show this screen\n"
  109. );
  110. exit(status);
  111. }
  112. static int get_file_stat(struct file_info *fdata)
  113. {
  114. struct stat st;
  115. int res;
  116. if (fdata->file_name == NULL)
  117. return 0;
  118. res = stat(fdata->file_name, &st);
  119. if (res){
  120. ERRS("stat failed on %s", fdata->file_name);
  121. return res;
  122. }
  123. fdata->file_size = st.st_size;
  124. return 0;
  125. }
  126. static int read_to_buf(struct file_info *fdata, char *buf)
  127. {
  128. FILE *f;
  129. int ret = EXIT_FAILURE;
  130. f = fopen(fdata->file_name, "r");
  131. if (f == NULL) {
  132. ERRS("could not open \"%s\" for reading", fdata->file_name);
  133. goto out;
  134. }
  135. errno = 0;
  136. fread(buf, fdata->file_size, 1, f);
  137. if (errno != 0) {
  138. ERRS("unable to read from file \"%s\"", fdata->file_name);
  139. goto out_close;
  140. }
  141. ret = EXIT_SUCCESS;
  142. out_close:
  143. fclose(f);
  144. out:
  145. return ret;
  146. }
  147. static int check_options(void)
  148. {
  149. int ret;
  150. if (opt_hw_id) {
  151. hw_id = strtoul(opt_hw_id, NULL, 0);
  152. }
  153. if (!kernel_la || !kernel_ep) {
  154. ERR("kernel loading address and entry point must be specified");
  155. return -1;
  156. }
  157. if (kernel_info.file_name == NULL) {
  158. ERR("no kernel image specified");
  159. return -1;
  160. }
  161. ret = get_file_stat(&kernel_info);
  162. if (ret)
  163. return ret;
  164. kernel_len = kernel_info.file_size;
  165. if (ofname == NULL) {
  166. ERR("no output file specified");
  167. return -1;
  168. }
  169. return 0;
  170. }
  171. static void fill_header(char *buf)
  172. {
  173. struct fw_header *hdr = (struct fw_header *)buf;
  174. memset(hdr, 0, sizeof(struct fw_header));
  175. hdr->version = htonl(hdr_ver);
  176. strncpy(hdr->vendor_name, vendor, sizeof(hdr->vendor_name));
  177. strncpy(hdr->fw_version, version, sizeof(hdr->fw_version));
  178. /**
  179. * This field is ignored and not specified in stock firmware
  180. * It is specified here to ensure sysupgrade hardware compatibility
  181. * The hardware id of a device is in the product-info partition
  182. */
  183. hdr->hw_id = htonl(hw_id);
  184. hdr->kernel_la = htonl(kernel_la);
  185. hdr->kernel_ep = htonl(kernel_ep);
  186. hdr->kernel_ofs = htonl(sizeof(struct fw_header));
  187. hdr->kernel_len = htonl(kernel_len);
  188. }
  189. static int write_fw(char *data, int len)
  190. {
  191. FILE *f;
  192. int ret = EXIT_FAILURE;
  193. f = fopen(ofname, "w");
  194. if (f == NULL) {
  195. ERRS("could not open \"%s\" for writing", ofname);
  196. goto out;
  197. }
  198. errno = 0;
  199. fwrite(data, len, 1, f);
  200. if (errno) {
  201. ERRS("unable to write output file");
  202. goto out_flush;
  203. }
  204. DBG("firmware file \"%s\" completed", ofname);
  205. ret = EXIT_SUCCESS;
  206. out_flush:
  207. fflush(f);
  208. fclose(f);
  209. if (ret != EXIT_SUCCESS) {
  210. unlink(ofname);
  211. }
  212. out:
  213. return ret;
  214. }
  215. static int build_fw(void)
  216. {
  217. int buflen;
  218. char *buf;
  219. char *p;
  220. int ret = EXIT_FAILURE;
  221. buflen = sizeof(struct fw_header) + kernel_len;
  222. buflen = ALIGN(buflen, 0x4);
  223. buf = malloc(buflen);
  224. if (!buf) {
  225. ERR("no memory for buffer\n");
  226. goto out;
  227. }
  228. memset(buf, 0, buflen);
  229. p = buf + sizeof(struct fw_header);
  230. ret = read_to_buf(&kernel_info, p);
  231. if (ret)
  232. goto out_free_buf;
  233. fill_header(buf);
  234. ret = write_fw(buf, buflen);
  235. if (ret)
  236. goto out_free_buf;
  237. ret = EXIT_SUCCESS;
  238. out_free_buf:
  239. free(buf);
  240. out:
  241. return ret;
  242. }
  243. int main(int argc, char *argv[])
  244. {
  245. int ret = EXIT_FAILURE;
  246. int err;
  247. FILE *outfile;
  248. progname = basename(argv[0]);
  249. while ( 1 ) {
  250. int c;
  251. c = getopt(argc, argv, "H:E:L:V:N:k:o:h");
  252. if (c == -1)
  253. break;
  254. switch (c) {
  255. case 'H':
  256. opt_hw_id = optarg;
  257. break;
  258. case 'E':
  259. sscanf(optarg, "0x%x", &kernel_ep);
  260. break;
  261. case 'L':
  262. sscanf(optarg, "0x%x", &kernel_la);
  263. break;
  264. case 'V':
  265. version = optarg;
  266. break;
  267. case 'N':
  268. vendor = optarg;
  269. break;
  270. case 'k':
  271. kernel_info.file_name = optarg;
  272. break;
  273. case 'o':
  274. ofname = optarg;
  275. break;
  276. case 'h':
  277. usage(EXIT_SUCCESS);
  278. break;
  279. default:
  280. usage(EXIT_FAILURE);
  281. break;
  282. }
  283. }
  284. ret = check_options();
  285. if (ret)
  286. goto out;
  287. ret = build_fw();
  288. out:
  289. return ret;
  290. }