mtdsplit_uimage.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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. static const struct of_device_id mtdsplit_uimage_of_match_table[] = {
  207. { .compatible = "denx,uimage" },
  208. {},
  209. };
  210. static struct mtd_part_parser uimage_generic_parser = {
  211. .owner = THIS_MODULE,
  212. .name = "uimage-fw",
  213. .of_match_table = mtdsplit_uimage_of_match_table,
  214. .parse_fn = mtdsplit_uimage_parse_generic,
  215. .type = MTD_PARSER_TYPE_FIRMWARE,
  216. };
  217. #define FW_MAGIC_GS110TPPV1 0x4e474520
  218. #define FW_MAGIC_WNR2000V1 0x32303031
  219. #define FW_MAGIC_WNR2000V3 0x32303033
  220. #define FW_MAGIC_WNR2000V4 0x32303034
  221. #define FW_MAGIC_WNR2200 0x32323030
  222. #define FW_MAGIC_WNR612V2 0x32303631
  223. #define FW_MAGIC_WNR1000V2 0x31303031
  224. #define FW_MAGIC_WNR1000V2_VC 0x31303030
  225. #define FW_MAGIC_WNDR3700 0x33373030
  226. #define FW_MAGIC_WNDR3700V2 0x33373031
  227. #define FW_MAGIC_WPN824N 0x31313030
  228. static ssize_t uimage_verify_wndr3700(u_char *buf, size_t len, int *extralen)
  229. {
  230. struct uimage_header *header = (struct uimage_header *)buf;
  231. uint8_t expected_type = IH_TYPE_FILESYSTEM;
  232. switch (be32_to_cpu(header->ih_magic)) {
  233. case FW_MAGIC_GS110TPPV1:
  234. case FW_MAGIC_WNR2000V4:
  235. expected_type = IH_TYPE_KERNEL;
  236. break;
  237. case FW_MAGIC_WNR612V2:
  238. case FW_MAGIC_WNR1000V2:
  239. case FW_MAGIC_WNR1000V2_VC:
  240. case FW_MAGIC_WNR2000V1:
  241. case FW_MAGIC_WNR2000V3:
  242. case FW_MAGIC_WNR2200:
  243. case FW_MAGIC_WNDR3700:
  244. case FW_MAGIC_WNDR3700V2:
  245. case FW_MAGIC_WPN824N:
  246. break;
  247. default:
  248. return -EINVAL;
  249. }
  250. if (header->ih_os != IH_OS_LINUX ||
  251. header->ih_type != expected_type)
  252. return -EINVAL;
  253. return 0;
  254. }
  255. static int
  256. mtdsplit_uimage_parse_netgear(struct mtd_info *master,
  257. const struct mtd_partition **pparts,
  258. struct mtd_part_parser_data *data)
  259. {
  260. return __mtdsplit_parse_uimage(master, pparts, data,
  261. uimage_verify_wndr3700);
  262. }
  263. static const struct of_device_id mtdsplit_uimage_netgear_of_match_table[] = {
  264. { .compatible = "netgear,uimage" },
  265. {},
  266. };
  267. static struct mtd_part_parser uimage_netgear_parser = {
  268. .owner = THIS_MODULE,
  269. .name = "netgear-fw",
  270. .of_match_table = mtdsplit_uimage_netgear_of_match_table,
  271. .parse_fn = mtdsplit_uimage_parse_netgear,
  272. .type = MTD_PARSER_TYPE_FIRMWARE,
  273. };
  274. /**************************************************
  275. * ALLNET
  276. **************************************************/
  277. #define FW_MAGIC_SG8208M 0x00000006
  278. #define FW_MAGIC_SG8310PM 0x83000006
  279. static ssize_t uimage_verify_allnet(u_char *buf, size_t len, int *extralen)
  280. {
  281. struct uimage_header *header = (struct uimage_header *)buf;
  282. switch (be32_to_cpu(header->ih_magic)) {
  283. case FW_MAGIC_SG8208M:
  284. case FW_MAGIC_SG8310PM:
  285. break;
  286. default:
  287. return -EINVAL;
  288. }
  289. if (header->ih_os != IH_OS_LINUX)
  290. return -EINVAL;
  291. return 0;
  292. }
  293. static int
  294. mtdsplit_uimage_parse_allnet(struct mtd_info *master,
  295. const struct mtd_partition **pparts,
  296. struct mtd_part_parser_data *data)
  297. {
  298. return __mtdsplit_parse_uimage(master, pparts, data,
  299. uimage_verify_allnet);
  300. }
  301. static const struct of_device_id mtdsplit_uimage_allnet_of_match_table[] = {
  302. { .compatible = "allnet,uimage" },
  303. {},
  304. };
  305. static struct mtd_part_parser uimage_allnet_parser = {
  306. .owner = THIS_MODULE,
  307. .name = "allnet-fw",
  308. .of_match_table = mtdsplit_uimage_allnet_of_match_table,
  309. .parse_fn = mtdsplit_uimage_parse_allnet,
  310. };
  311. /**************************************************
  312. * Edimax
  313. **************************************************/
  314. #define FW_EDIMAX_OFFSET 20
  315. #define FW_MAGIC_EDIMAX 0x43535953
  316. static ssize_t uimage_find_edimax(u_char *buf, size_t len, int *extralen)
  317. {
  318. u32 *magic;
  319. if (len < FW_EDIMAX_OFFSET + sizeof(struct uimage_header)) {
  320. pr_err("Buffer too small for checking Edimax header\n");
  321. return -ENOSPC;
  322. }
  323. magic = (u32 *)buf;
  324. if (be32_to_cpu(*magic) != FW_MAGIC_EDIMAX)
  325. return -EINVAL;
  326. if (!uimage_verify_default(buf + FW_EDIMAX_OFFSET, len, extralen))
  327. return FW_EDIMAX_OFFSET;
  328. return -EINVAL;
  329. }
  330. static int
  331. mtdsplit_uimage_parse_edimax(struct mtd_info *master,
  332. const struct mtd_partition **pparts,
  333. struct mtd_part_parser_data *data)
  334. {
  335. return __mtdsplit_parse_uimage(master, pparts, data,
  336. uimage_find_edimax);
  337. }
  338. static const struct of_device_id mtdsplit_uimage_edimax_of_match_table[] = {
  339. { .compatible = "edimax,uimage" },
  340. {},
  341. };
  342. static struct mtd_part_parser uimage_edimax_parser = {
  343. .owner = THIS_MODULE,
  344. .name = "edimax-fw",
  345. .of_match_table = mtdsplit_uimage_edimax_of_match_table,
  346. .parse_fn = mtdsplit_uimage_parse_edimax,
  347. .type = MTD_PARSER_TYPE_FIRMWARE,
  348. };
  349. /**************************************************
  350. * Fon(Foxconn)
  351. **************************************************/
  352. #define FONFXC_PAD_LEN 32
  353. static ssize_t uimage_find_fonfxc(u_char *buf, size_t len, int *extralen)
  354. {
  355. if (uimage_verify_default(buf, len, extralen) < 0)
  356. return -EINVAL;
  357. *extralen = FONFXC_PAD_LEN;
  358. return 0;
  359. }
  360. static int
  361. mtdsplit_uimage_parse_fonfxc(struct mtd_info *master,
  362. const struct mtd_partition **pparts,
  363. struct mtd_part_parser_data *data)
  364. {
  365. return __mtdsplit_parse_uimage(master, pparts, data,
  366. uimage_find_fonfxc);
  367. }
  368. static const struct of_device_id mtdsplit_uimage_fonfxc_of_match_table[] = {
  369. { .compatible = "fonfxc,uimage" },
  370. {},
  371. };
  372. static struct mtd_part_parser uimage_fonfxc_parser = {
  373. .owner = THIS_MODULE,
  374. .name = "fonfxc-fw",
  375. .of_match_table = mtdsplit_uimage_fonfxc_of_match_table,
  376. .parse_fn = mtdsplit_uimage_parse_fonfxc,
  377. };
  378. /**************************************************
  379. * SGE (T&W) Shenzhen Gongjin Electronics
  380. **************************************************/
  381. #define SGE_PAD_LEN 96
  382. static ssize_t uimage_find_sge(u_char *buf, size_t len, int *extralen)
  383. {
  384. if (uimage_verify_default(buf, len, extralen) < 0)
  385. return -EINVAL;
  386. *extralen = SGE_PAD_LEN;
  387. return 0;
  388. }
  389. static int
  390. mtdsplit_uimage_parse_sge(struct mtd_info *master,
  391. const struct mtd_partition **pparts,
  392. struct mtd_part_parser_data *data)
  393. {
  394. return __mtdsplit_parse_uimage(master, pparts, data,
  395. uimage_find_sge);
  396. }
  397. static const struct of_device_id mtdsplit_uimage_sge_of_match_table[] = {
  398. { .compatible = "sge,uimage" },
  399. {},
  400. };
  401. static struct mtd_part_parser uimage_sge_parser = {
  402. .owner = THIS_MODULE,
  403. .name = "sge-fw",
  404. .of_match_table = mtdsplit_uimage_sge_of_match_table,
  405. .parse_fn = mtdsplit_uimage_parse_sge,
  406. };
  407. /**************************************************
  408. * OKLI (OpenWrt Kernel Loader Image)
  409. **************************************************/
  410. #define IH_MAGIC_OKLI 0x4f4b4c49
  411. static ssize_t uimage_verify_okli(u_char *buf, size_t len, int *extralen)
  412. {
  413. struct uimage_header *header = (struct uimage_header *)buf;
  414. /* default sanity checks */
  415. if (be32_to_cpu(header->ih_magic) != IH_MAGIC_OKLI) {
  416. pr_debug("invalid uImage magic: %08x\n",
  417. be32_to_cpu(header->ih_magic));
  418. return -EINVAL;
  419. }
  420. if (header->ih_os != IH_OS_LINUX) {
  421. pr_debug("invalid uImage OS: %08x\n",
  422. be32_to_cpu(header->ih_os));
  423. return -EINVAL;
  424. }
  425. if (header->ih_type != IH_TYPE_KERNEL) {
  426. pr_debug("invalid uImage type: %08x\n",
  427. be32_to_cpu(header->ih_type));
  428. return -EINVAL;
  429. }
  430. return 0;
  431. }
  432. static int
  433. mtdsplit_uimage_parse_okli(struct mtd_info *master,
  434. const struct mtd_partition **pparts,
  435. struct mtd_part_parser_data *data)
  436. {
  437. return __mtdsplit_parse_uimage(master, pparts, data,
  438. uimage_verify_okli);
  439. }
  440. static const struct of_device_id mtdsplit_uimage_okli_of_match_table[] = {
  441. { .compatible = "openwrt,okli" },
  442. {},
  443. };
  444. static struct mtd_part_parser uimage_okli_parser = {
  445. .owner = THIS_MODULE,
  446. .name = "okli-fw",
  447. .of_match_table = mtdsplit_uimage_okli_of_match_table,
  448. .parse_fn = mtdsplit_uimage_parse_okli,
  449. };
  450. /**************************************************
  451. * Init
  452. **************************************************/
  453. static int __init mtdsplit_uimage_init(void)
  454. {
  455. register_mtd_parser(&uimage_generic_parser);
  456. register_mtd_parser(&uimage_netgear_parser);
  457. register_mtd_parser(&uimage_allnet_parser);
  458. register_mtd_parser(&uimage_edimax_parser);
  459. register_mtd_parser(&uimage_fonfxc_parser);
  460. register_mtd_parser(&uimage_sge_parser);
  461. register_mtd_parser(&uimage_okli_parser);
  462. return 0;
  463. }
  464. module_init(mtdsplit_uimage_init);