mtdsplit_uimage.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*
  2. * Copyright (C) 2013 Gabor Juhos <[email protected]>
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License version 2 as published
  6. * by the Free Software Foundation.
  7. *
  8. */
  9. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  10. #include <linux/module.h>
  11. #include <linux/init.h>
  12. #include <linux/kernel.h>
  13. #include <linux/slab.h>
  14. #include <linux/vmalloc.h>
  15. #include <linux/mtd/mtd.h>
  16. #include <linux/mtd/partitions.h>
  17. #include <linux/version.h>
  18. #include <linux/byteorder/generic.h>
  19. #include "mtdsplit.h"
  20. /*
  21. * uimage_header itself is only 64B, but it may be prepended with another data.
  22. * Currently the biggest size is for Edimax devices: 20B + 64B
  23. */
  24. #define MAX_HEADER_LEN 84
  25. #define IH_MAGIC 0x27051956 /* Image Magic Number */
  26. #define IH_NMLEN 32 /* Image Name Length */
  27. #define IH_OS_LINUX 5 /* Linux */
  28. #define IH_TYPE_KERNEL 2 /* OS Kernel Image */
  29. #define IH_TYPE_FILESYSTEM 7 /* Filesystem Image */
  30. /*
  31. * Legacy format image header,
  32. * all data in network byte order (aka natural aka bigendian).
  33. */
  34. struct uimage_header {
  35. uint32_t ih_magic; /* Image Header Magic Number */
  36. uint32_t ih_hcrc; /* Image Header CRC Checksum */
  37. uint32_t ih_time; /* Image Creation Timestamp */
  38. uint32_t ih_size; /* Image Data Size */
  39. uint32_t ih_load; /* Data Load Address */
  40. uint32_t ih_ep; /* Entry Point Address */
  41. uint32_t ih_dcrc; /* Image Data CRC Checksum */
  42. uint8_t ih_os; /* Operating System */
  43. uint8_t ih_arch; /* CPU architecture */
  44. uint8_t ih_type; /* Image Type */
  45. uint8_t ih_comp; /* Compression Type */
  46. uint8_t ih_name[IH_NMLEN]; /* Image Name */
  47. };
  48. static int
  49. read_uimage_header(struct mtd_info *mtd, size_t offset, u_char *buf,
  50. size_t header_len)
  51. {
  52. size_t retlen;
  53. int ret;
  54. ret = mtd_read(mtd, offset, header_len, &retlen, buf);
  55. if (ret) {
  56. pr_debug("read error in \"%s\"\n", mtd->name);
  57. return ret;
  58. }
  59. if (retlen != header_len) {
  60. pr_debug("short read in \"%s\"\n", mtd->name);
  61. return -EIO;
  62. }
  63. return 0;
  64. }
  65. /**
  66. * __mtdsplit_parse_uimage - scan partition and create kernel + rootfs parts
  67. *
  68. * @find_header: function to call for a block of data that will return offset
  69. * of a valid uImage header if found
  70. */
  71. static int __mtdsplit_parse_uimage(struct mtd_info *master,
  72. const struct mtd_partition **pparts,
  73. struct mtd_part_parser_data *data,
  74. ssize_t (*find_header)(u_char *buf, size_t len))
  75. {
  76. struct mtd_partition *parts;
  77. u_char *buf;
  78. int nr_parts;
  79. size_t offset;
  80. size_t uimage_offset;
  81. size_t uimage_size = 0;
  82. size_t rootfs_offset;
  83. size_t rootfs_size = 0;
  84. int uimage_part, rf_part;
  85. int ret;
  86. enum mtdsplit_part_type type;
  87. nr_parts = 2;
  88. parts = kzalloc(nr_parts * sizeof(*parts), GFP_KERNEL);
  89. if (!parts)
  90. return -ENOMEM;
  91. buf = vmalloc(MAX_HEADER_LEN);
  92. if (!buf) {
  93. ret = -ENOMEM;
  94. goto err_free_parts;
  95. }
  96. /* find uImage on erase block boundaries */
  97. for (offset = 0; offset < master->size; offset += master->erasesize) {
  98. struct uimage_header *header;
  99. uimage_size = 0;
  100. ret = read_uimage_header(master, offset, buf, MAX_HEADER_LEN);
  101. if (ret)
  102. continue;
  103. ret = find_header(buf, MAX_HEADER_LEN);
  104. if (ret < 0) {
  105. pr_debug("no valid uImage found in \"%s\" at offset %llx\n",
  106. master->name, (unsigned long long) offset);
  107. continue;
  108. }
  109. header = (struct uimage_header *)(buf + ret);
  110. uimage_size = sizeof(*header) + be32_to_cpu(header->ih_size) + ret;
  111. if ((offset + uimage_size) > master->size) {
  112. pr_debug("uImage exceeds MTD device \"%s\"\n",
  113. master->name);
  114. continue;
  115. }
  116. break;
  117. }
  118. if (uimage_size == 0) {
  119. pr_debug("no uImage found in \"%s\"\n", master->name);
  120. ret = -ENODEV;
  121. goto err_free_buf;
  122. }
  123. uimage_offset = offset;
  124. if (uimage_offset == 0) {
  125. uimage_part = 0;
  126. rf_part = 1;
  127. /* find the roots after the uImage */
  128. ret = mtd_find_rootfs_from(master, uimage_offset + uimage_size,
  129. master->size, &rootfs_offset, &type);
  130. if (ret) {
  131. pr_debug("no rootfs after uImage in \"%s\"\n",
  132. master->name);
  133. goto err_free_buf;
  134. }
  135. rootfs_size = master->size - rootfs_offset;
  136. uimage_size = rootfs_offset - uimage_offset;
  137. } else {
  138. rf_part = 0;
  139. uimage_part = 1;
  140. /* check rootfs presence at offset 0 */
  141. ret = mtd_check_rootfs_magic(master, 0, &type);
  142. if (ret) {
  143. pr_debug("no rootfs before uImage in \"%s\"\n",
  144. master->name);
  145. goto err_free_buf;
  146. }
  147. rootfs_offset = 0;
  148. rootfs_size = uimage_offset;
  149. }
  150. if (rootfs_size == 0) {
  151. pr_debug("no rootfs found in \"%s\"\n", master->name);
  152. ret = -ENODEV;
  153. goto err_free_buf;
  154. }
  155. parts[uimage_part].name = KERNEL_PART_NAME;
  156. parts[uimage_part].offset = uimage_offset;
  157. parts[uimage_part].size = uimage_size;
  158. if (type == MTDSPLIT_PART_TYPE_UBI)
  159. parts[rf_part].name = UBI_PART_NAME;
  160. else
  161. parts[rf_part].name = ROOTFS_PART_NAME;
  162. parts[rf_part].offset = rootfs_offset;
  163. parts[rf_part].size = rootfs_size;
  164. vfree(buf);
  165. *pparts = parts;
  166. return nr_parts;
  167. err_free_buf:
  168. vfree(buf);
  169. err_free_parts:
  170. kfree(parts);
  171. return ret;
  172. }
  173. static ssize_t uimage_verify_default(u_char *buf, size_t len)
  174. {
  175. struct uimage_header *header = (struct uimage_header *)buf;
  176. /* default sanity checks */
  177. if (be32_to_cpu(header->ih_magic) != IH_MAGIC) {
  178. pr_debug("invalid uImage magic: %08x\n",
  179. be32_to_cpu(header->ih_magic));
  180. return -EINVAL;
  181. }
  182. if (header->ih_os != IH_OS_LINUX) {
  183. pr_debug("invalid uImage OS: %08x\n",
  184. be32_to_cpu(header->ih_os));
  185. return -EINVAL;
  186. }
  187. if (header->ih_type != IH_TYPE_KERNEL) {
  188. pr_debug("invalid uImage type: %08x\n",
  189. be32_to_cpu(header->ih_type));
  190. return -EINVAL;
  191. }
  192. return 0;
  193. }
  194. static int
  195. mtdsplit_uimage_parse_generic(struct mtd_info *master,
  196. const struct mtd_partition **pparts,
  197. struct mtd_part_parser_data *data)
  198. {
  199. return __mtdsplit_parse_uimage(master, pparts, data,
  200. uimage_verify_default);
  201. }
  202. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 9, 0)
  203. static const struct of_device_id mtdsplit_uimage_of_match_table[] = {
  204. { .compatible = "denx,uimage" },
  205. {},
  206. };
  207. #endif
  208. static struct mtd_part_parser uimage_generic_parser = {
  209. .owner = THIS_MODULE,
  210. .name = "uimage-fw",
  211. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 9, 0)
  212. .of_match_table = mtdsplit_uimage_of_match_table,
  213. #endif
  214. .parse_fn = mtdsplit_uimage_parse_generic,
  215. .type = MTD_PARSER_TYPE_FIRMWARE,
  216. };
  217. #define FW_MAGIC_WNR2000V1 0x32303031
  218. #define FW_MAGIC_WNR2000V3 0x32303033
  219. #define FW_MAGIC_WNR2000V4 0x32303034
  220. #define FW_MAGIC_WNR2200 0x32323030
  221. #define FW_MAGIC_WNR612V2 0x32303631
  222. #define FW_MAGIC_WNR1000V2 0x31303031
  223. #define FW_MAGIC_WNR1000V2_VC 0x31303030
  224. #define FW_MAGIC_WNDR3700 0x33373030
  225. #define FW_MAGIC_WNDR3700V2 0x33373031
  226. #define FW_MAGIC_WPN824N 0x31313030
  227. static ssize_t uimage_verify_wndr3700(u_char *buf, size_t len)
  228. {
  229. struct uimage_header *header = (struct uimage_header *)buf;
  230. uint8_t expected_type = IH_TYPE_FILESYSTEM;
  231. switch (be32_to_cpu(header->ih_magic)) {
  232. case FW_MAGIC_WNR612V2:
  233. case FW_MAGIC_WNR1000V2:
  234. case FW_MAGIC_WNR1000V2_VC:
  235. case FW_MAGIC_WNR2000V1:
  236. case FW_MAGIC_WNR2000V3:
  237. case FW_MAGIC_WNR2200:
  238. case FW_MAGIC_WNDR3700:
  239. case FW_MAGIC_WNDR3700V2:
  240. case FW_MAGIC_WPN824N:
  241. break;
  242. case FW_MAGIC_WNR2000V4:
  243. expected_type = IH_TYPE_KERNEL;
  244. break;
  245. default:
  246. return -EINVAL;
  247. }
  248. if (header->ih_os != IH_OS_LINUX ||
  249. header->ih_type != expected_type)
  250. return -EINVAL;
  251. return 0;
  252. }
  253. static int
  254. mtdsplit_uimage_parse_netgear(struct mtd_info *master,
  255. const struct mtd_partition **pparts,
  256. struct mtd_part_parser_data *data)
  257. {
  258. return __mtdsplit_parse_uimage(master, pparts, data,
  259. uimage_verify_wndr3700);
  260. }
  261. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 9, 0)
  262. static const struct of_device_id mtdsplit_uimage_netgear_of_match_table[] = {
  263. { .compatible = "netgear,uimage" },
  264. {},
  265. };
  266. #endif
  267. static struct mtd_part_parser uimage_netgear_parser = {
  268. .owner = THIS_MODULE,
  269. .name = "netgear-fw",
  270. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 9, 0)
  271. .of_match_table = mtdsplit_uimage_netgear_of_match_table,
  272. #endif
  273. .parse_fn = mtdsplit_uimage_parse_netgear,
  274. .type = MTD_PARSER_TYPE_FIRMWARE,
  275. };
  276. /**************************************************
  277. * Edimax
  278. **************************************************/
  279. #define FW_EDIMAX_OFFSET 20
  280. #define FW_MAGIC_EDIMAX 0x43535953
  281. static ssize_t uimage_find_edimax(u_char *buf, size_t len)
  282. {
  283. u32 *magic;
  284. if (len < FW_EDIMAX_OFFSET + sizeof(struct uimage_header)) {
  285. pr_err("Buffer too small for checking Edimax header\n");
  286. return -ENOSPC;
  287. }
  288. magic = (u32 *)buf;
  289. if (be32_to_cpu(*magic) != FW_MAGIC_EDIMAX)
  290. return -EINVAL;
  291. if (!uimage_verify_default(buf + FW_EDIMAX_OFFSET, len))
  292. return FW_EDIMAX_OFFSET;
  293. return -EINVAL;
  294. }
  295. static int
  296. mtdsplit_uimage_parse_edimax(struct mtd_info *master,
  297. const struct mtd_partition **pparts,
  298. struct mtd_part_parser_data *data)
  299. {
  300. return __mtdsplit_parse_uimage(master, pparts, data,
  301. uimage_find_edimax);
  302. }
  303. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 9, 0)
  304. static const struct of_device_id mtdsplit_uimage_edimax_of_match_table[] = {
  305. { .compatible = "edimax,uimage" },
  306. {},
  307. };
  308. #endif
  309. static struct mtd_part_parser uimage_edimax_parser = {
  310. .owner = THIS_MODULE,
  311. .name = "edimax-fw",
  312. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 9, 0)
  313. .of_match_table = mtdsplit_uimage_edimax_of_match_table,
  314. #endif
  315. .parse_fn = mtdsplit_uimage_parse_edimax,
  316. .type = MTD_PARSER_TYPE_FIRMWARE,
  317. };
  318. /**************************************************
  319. * Init
  320. **************************************************/
  321. static int __init mtdsplit_uimage_init(void)
  322. {
  323. register_mtd_parser(&uimage_generic_parser);
  324. register_mtd_parser(&uimage_netgear_parser);
  325. register_mtd_parser(&uimage_edimax_parser);
  326. return 0;
  327. }
  328. module_init(mtdsplit_uimage_init);