mtdsplit_uimage.c 12 KB

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