mtdsplit_uimage.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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 <linux/of.h>
  20. #include "mtdsplit.h"
  21. /*
  22. * uimage_header itself is only 64B, but it may be prepended with another data.
  23. * Currently the biggest size is for Fon(Foxconn) devices: 64B + 32B
  24. */
  25. #define MAX_HEADER_LEN 96
  26. #define IH_MAGIC 0x27051956 /* Image Magic Number */
  27. #define IH_NMLEN 32 /* Image Name Length */
  28. #define IH_OS_LINUX 5 /* Linux */
  29. #define IH_TYPE_KERNEL 2 /* OS Kernel Image */
  30. #define IH_TYPE_FILESYSTEM 7 /* Filesystem Image */
  31. /*
  32. * Legacy format image header,
  33. * all data in network byte order (aka natural aka bigendian).
  34. */
  35. struct uimage_header {
  36. uint32_t ih_magic; /* Image Header Magic Number */
  37. uint32_t ih_hcrc; /* Image Header CRC Checksum */
  38. uint32_t ih_time; /* Image Creation Timestamp */
  39. uint32_t ih_size; /* Image Data Size */
  40. uint32_t ih_load; /* Data Load Address */
  41. uint32_t ih_ep; /* Entry Point Address */
  42. uint32_t ih_dcrc; /* Image Data CRC Checksum */
  43. uint8_t ih_os; /* Operating System */
  44. uint8_t ih_arch; /* CPU architecture */
  45. uint8_t ih_type; /* Image Type */
  46. uint8_t ih_comp; /* Compression Type */
  47. uint8_t ih_name[IH_NMLEN]; /* Image Name */
  48. };
  49. static int
  50. read_uimage_header(struct mtd_info *mtd, size_t offset, u_char *buf,
  51. size_t header_len)
  52. {
  53. size_t retlen;
  54. int ret;
  55. ret = mtd_read(mtd, offset, header_len, &retlen, buf);
  56. if (ret) {
  57. pr_debug("read error in \"%s\"\n", mtd->name);
  58. return ret;
  59. }
  60. if (retlen != header_len) {
  61. pr_debug("short read in \"%s\"\n", mtd->name);
  62. return -EIO;
  63. }
  64. return 0;
  65. }
  66. /**
  67. * __mtdsplit_parse_uimage - scan partition and create kernel + rootfs parts
  68. *
  69. * @find_header: function to call for a block of data that will return offset
  70. * and tail padding length of a valid uImage header if found
  71. */
  72. static int __mtdsplit_parse_uimage(struct mtd_info *master,
  73. const struct mtd_partition **pparts,
  74. struct mtd_part_parser_data *data,
  75. ssize_t (*find_header)(u_char *buf, size_t len, int *extralen))
  76. {
  77. struct mtd_partition *parts;
  78. u_char *buf;
  79. int nr_parts;
  80. size_t offset;
  81. size_t uimage_offset;
  82. size_t uimage_size = 0;
  83. size_t rootfs_offset;
  84. size_t rootfs_size = 0;
  85. int uimage_part, rf_part;
  86. int ret;
  87. int extralen;
  88. enum mtdsplit_part_type type;
  89. nr_parts = 2;
  90. parts = kzalloc(nr_parts * sizeof(*parts), GFP_KERNEL);
  91. if (!parts)
  92. return -ENOMEM;
  93. buf = vmalloc(MAX_HEADER_LEN);
  94. if (!buf) {
  95. ret = -ENOMEM;
  96. goto err_free_parts;
  97. }
  98. /* find uImage on erase block boundaries */
  99. for (offset = 0; offset < master->size; offset += master->erasesize) {
  100. struct uimage_header *header;
  101. uimage_size = 0;
  102. ret = read_uimage_header(master, offset, buf, MAX_HEADER_LEN);
  103. if (ret)
  104. continue;
  105. extralen = 0;
  106. ret = find_header(buf, MAX_HEADER_LEN, &extralen);
  107. if (ret < 0) {
  108. pr_debug("no valid uImage found in \"%s\" at offset %llx\n",
  109. master->name, (unsigned long long) offset);
  110. continue;
  111. }
  112. header = (struct uimage_header *)(buf + ret);
  113. uimage_size = sizeof(*header) +
  114. be32_to_cpu(header->ih_size) + ret + extralen;
  115. if ((offset + uimage_size) > master->size) {
  116. pr_debug("uImage exceeds MTD device \"%s\"\n",
  117. master->name);
  118. continue;
  119. }
  120. break;
  121. }
  122. if (uimage_size == 0) {
  123. pr_debug("no uImage found in \"%s\"\n", master->name);
  124. ret = -ENODEV;
  125. goto err_free_buf;
  126. }
  127. uimage_offset = offset;
  128. if (uimage_offset == 0) {
  129. uimage_part = 0;
  130. rf_part = 1;
  131. /* find the roots after the uImage */
  132. ret = mtd_find_rootfs_from(master, uimage_offset + uimage_size,
  133. master->size, &rootfs_offset, &type);
  134. if (ret) {
  135. pr_debug("no rootfs after uImage in \"%s\"\n",
  136. master->name);
  137. goto err_free_buf;
  138. }
  139. rootfs_size = master->size - rootfs_offset;
  140. uimage_size = rootfs_offset - uimage_offset;
  141. } else {
  142. rf_part = 0;
  143. uimage_part = 1;
  144. /* check rootfs presence at offset 0 */
  145. ret = mtd_check_rootfs_magic(master, 0, &type);
  146. if (ret) {
  147. pr_debug("no rootfs before uImage in \"%s\"\n",
  148. master->name);
  149. goto err_free_buf;
  150. }
  151. rootfs_offset = 0;
  152. rootfs_size = uimage_offset;
  153. }
  154. if (rootfs_size == 0) {
  155. pr_debug("no rootfs found in \"%s\"\n", master->name);
  156. ret = -ENODEV;
  157. goto err_free_buf;
  158. }
  159. parts[uimage_part].name = KERNEL_PART_NAME;
  160. parts[uimage_part].offset = uimage_offset;
  161. parts[uimage_part].size = uimage_size;
  162. if (type == MTDSPLIT_PART_TYPE_UBI)
  163. parts[rf_part].name = UBI_PART_NAME;
  164. else
  165. parts[rf_part].name = ROOTFS_PART_NAME;
  166. parts[rf_part].offset = rootfs_offset;
  167. parts[rf_part].size = rootfs_size;
  168. vfree(buf);
  169. *pparts = parts;
  170. return nr_parts;
  171. err_free_buf:
  172. vfree(buf);
  173. err_free_parts:
  174. kfree(parts);
  175. return ret;
  176. }
  177. static ssize_t uimage_verify_default(u_char *buf, size_t len, int *extralen)
  178. {
  179. struct uimage_header *header = (struct uimage_header *)buf;
  180. /* default sanity checks */
  181. if (be32_to_cpu(header->ih_magic) != IH_MAGIC) {
  182. pr_debug("invalid uImage magic: %08x\n",
  183. be32_to_cpu(header->ih_magic));
  184. return -EINVAL;
  185. }
  186. if (header->ih_os != IH_OS_LINUX) {
  187. pr_debug("invalid uImage OS: %08x\n",
  188. be32_to_cpu(header->ih_os));
  189. return -EINVAL;
  190. }
  191. if (header->ih_type != IH_TYPE_KERNEL) {
  192. pr_debug("invalid uImage type: %08x\n",
  193. be32_to_cpu(header->ih_type));
  194. return -EINVAL;
  195. }
  196. return 0;
  197. }
  198. static int
  199. mtdsplit_uimage_parse_generic(struct mtd_info *master,
  200. const struct mtd_partition **pparts,
  201. struct mtd_part_parser_data *data)
  202. {
  203. return __mtdsplit_parse_uimage(master, pparts, data,
  204. uimage_verify_default);
  205. }
  206. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 9, 0)
  207. static const struct of_device_id mtdsplit_uimage_of_match_table[] = {
  208. { .compatible = "denx,uimage" },
  209. {},
  210. };
  211. #endif
  212. static struct mtd_part_parser uimage_generic_parser = {
  213. .owner = THIS_MODULE,
  214. .name = "uimage-fw",
  215. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 9, 0)
  216. .of_match_table = mtdsplit_uimage_of_match_table,
  217. #endif
  218. .parse_fn = mtdsplit_uimage_parse_generic,
  219. .type = MTD_PARSER_TYPE_FIRMWARE,
  220. };
  221. #define FW_MAGIC_WNR2000V1 0x32303031
  222. #define FW_MAGIC_WNR2000V3 0x32303033
  223. #define FW_MAGIC_WNR2000V4 0x32303034
  224. #define FW_MAGIC_WNR2200 0x32323030
  225. #define FW_MAGIC_WNR612V2 0x32303631
  226. #define FW_MAGIC_WNR1000V2 0x31303031
  227. #define FW_MAGIC_WNR1000V2_VC 0x31303030
  228. #define FW_MAGIC_WNDR3700 0x33373030
  229. #define FW_MAGIC_WNDR3700V2 0x33373031
  230. #define FW_MAGIC_WPN824N 0x31313030
  231. static ssize_t uimage_verify_wndr3700(u_char *buf, size_t len, int *extralen)
  232. {
  233. struct uimage_header *header = (struct uimage_header *)buf;
  234. uint8_t expected_type = IH_TYPE_FILESYSTEM;
  235. switch (be32_to_cpu(header->ih_magic)) {
  236. case FW_MAGIC_WNR612V2:
  237. case FW_MAGIC_WNR1000V2:
  238. case FW_MAGIC_WNR1000V2_VC:
  239. case FW_MAGIC_WNR2000V1:
  240. case FW_MAGIC_WNR2000V3:
  241. case FW_MAGIC_WNR2200:
  242. case FW_MAGIC_WNDR3700:
  243. case FW_MAGIC_WNDR3700V2:
  244. case FW_MAGIC_WPN824N:
  245. break;
  246. case FW_MAGIC_WNR2000V4:
  247. expected_type = IH_TYPE_KERNEL;
  248. break;
  249. default:
  250. return -EINVAL;
  251. }
  252. if (header->ih_os != IH_OS_LINUX ||
  253. header->ih_type != expected_type)
  254. return -EINVAL;
  255. return 0;
  256. }
  257. static int
  258. mtdsplit_uimage_parse_netgear(struct mtd_info *master,
  259. const struct mtd_partition **pparts,
  260. struct mtd_part_parser_data *data)
  261. {
  262. return __mtdsplit_parse_uimage(master, pparts, data,
  263. uimage_verify_wndr3700);
  264. }
  265. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 9, 0)
  266. static const struct of_device_id mtdsplit_uimage_netgear_of_match_table[] = {
  267. { .compatible = "netgear,uimage" },
  268. {},
  269. };
  270. #endif
  271. static struct mtd_part_parser uimage_netgear_parser = {
  272. .owner = THIS_MODULE,
  273. .name = "netgear-fw",
  274. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 9, 0)
  275. .of_match_table = mtdsplit_uimage_netgear_of_match_table,
  276. #endif
  277. .parse_fn = mtdsplit_uimage_parse_netgear,
  278. .type = MTD_PARSER_TYPE_FIRMWARE,
  279. };
  280. /**************************************************
  281. * Edimax
  282. **************************************************/
  283. #define FW_EDIMAX_OFFSET 20
  284. #define FW_MAGIC_EDIMAX 0x43535953
  285. static ssize_t uimage_find_edimax(u_char *buf, size_t len, int *extralen)
  286. {
  287. u32 *magic;
  288. if (len < FW_EDIMAX_OFFSET + sizeof(struct uimage_header)) {
  289. pr_err("Buffer too small for checking Edimax header\n");
  290. return -ENOSPC;
  291. }
  292. magic = (u32 *)buf;
  293. if (be32_to_cpu(*magic) != FW_MAGIC_EDIMAX)
  294. return -EINVAL;
  295. if (!uimage_verify_default(buf + FW_EDIMAX_OFFSET, len, extralen))
  296. return FW_EDIMAX_OFFSET;
  297. return -EINVAL;
  298. }
  299. static int
  300. mtdsplit_uimage_parse_edimax(struct mtd_info *master,
  301. const struct mtd_partition **pparts,
  302. struct mtd_part_parser_data *data)
  303. {
  304. return __mtdsplit_parse_uimage(master, pparts, data,
  305. uimage_find_edimax);
  306. }
  307. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 9, 0)
  308. static const struct of_device_id mtdsplit_uimage_edimax_of_match_table[] = {
  309. { .compatible = "edimax,uimage" },
  310. {},
  311. };
  312. #endif
  313. static struct mtd_part_parser uimage_edimax_parser = {
  314. .owner = THIS_MODULE,
  315. .name = "edimax-fw",
  316. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 9, 0)
  317. .of_match_table = mtdsplit_uimage_edimax_of_match_table,
  318. #endif
  319. .parse_fn = mtdsplit_uimage_parse_edimax,
  320. .type = MTD_PARSER_TYPE_FIRMWARE,
  321. };
  322. /**************************************************
  323. * Fon(Foxconn)
  324. **************************************************/
  325. #define FONFXC_PAD_LEN 32
  326. static ssize_t uimage_find_fonfxc(u_char *buf, size_t len, int *extralen)
  327. {
  328. if (uimage_verify_default(buf, len, extralen) < 0)
  329. return -EINVAL;
  330. *extralen = FONFXC_PAD_LEN;
  331. return 0;
  332. }
  333. static int
  334. mtdsplit_uimage_parse_fonfxc(struct mtd_info *master,
  335. const struct mtd_partition **pparts,
  336. struct mtd_part_parser_data *data)
  337. {
  338. return __mtdsplit_parse_uimage(master, pparts, data,
  339. uimage_find_fonfxc);
  340. }
  341. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 9, 0)
  342. static const struct of_device_id mtdsplit_uimage_fonfxc_of_match_table[] = {
  343. { .compatible = "fonfxc,uimage" },
  344. {},
  345. };
  346. #endif
  347. static struct mtd_part_parser uimage_fonfxc_parser = {
  348. .owner = THIS_MODULE,
  349. .name = "fonfxc-fw",
  350. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 9, 0)
  351. .of_match_table = mtdsplit_uimage_fonfxc_of_match_table,
  352. #endif
  353. .parse_fn = mtdsplit_uimage_parse_fonfxc,
  354. };
  355. /**************************************************
  356. * OKLI (OpenWrt Kernel Loader Image)
  357. **************************************************/
  358. #define IH_MAGIC_OKLI 0x4f4b4c49
  359. static ssize_t uimage_verify_okli(u_char *buf, size_t len, int *extralen)
  360. {
  361. struct uimage_header *header = (struct uimage_header *)buf;
  362. /* default sanity checks */
  363. if (be32_to_cpu(header->ih_magic) != IH_MAGIC_OKLI) {
  364. pr_debug("invalid uImage magic: %08x\n",
  365. be32_to_cpu(header->ih_magic));
  366. return -EINVAL;
  367. }
  368. if (header->ih_os != IH_OS_LINUX) {
  369. pr_debug("invalid uImage OS: %08x\n",
  370. be32_to_cpu(header->ih_os));
  371. return -EINVAL;
  372. }
  373. if (header->ih_type != IH_TYPE_KERNEL) {
  374. pr_debug("invalid uImage type: %08x\n",
  375. be32_to_cpu(header->ih_type));
  376. return -EINVAL;
  377. }
  378. return 0;
  379. }
  380. static int
  381. mtdsplit_uimage_parse_okli(struct mtd_info *master,
  382. const struct mtd_partition **pparts,
  383. struct mtd_part_parser_data *data)
  384. {
  385. return __mtdsplit_parse_uimage(master, pparts, data,
  386. uimage_verify_okli);
  387. }
  388. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 9, 0)
  389. static const struct of_device_id mtdsplit_uimage_okli_of_match_table[] = {
  390. { .compatible = "openwrt,okli" },
  391. {},
  392. };
  393. #endif
  394. static struct mtd_part_parser uimage_okli_parser = {
  395. .owner = THIS_MODULE,
  396. .name = "okli-fw",
  397. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 9, 0)
  398. .of_match_table = mtdsplit_uimage_okli_of_match_table,
  399. #endif
  400. .parse_fn = mtdsplit_uimage_parse_okli,
  401. };
  402. /**************************************************
  403. * Init
  404. **************************************************/
  405. static int __init mtdsplit_uimage_init(void)
  406. {
  407. register_mtd_parser(&uimage_generic_parser);
  408. register_mtd_parser(&uimage_netgear_parser);
  409. register_mtd_parser(&uimage_edimax_parser);
  410. register_mtd_parser(&uimage_fonfxc_parser);
  411. register_mtd_parser(&uimage_okli_parser);
  412. return 0;
  413. }
  414. module_init(mtdsplit_uimage_init);