mtdsplit_uimage.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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_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, int *extralen)
  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. static const struct of_device_id mtdsplit_uimage_netgear_of_match_table[] = {
  262. { .compatible = "netgear,uimage" },
  263. {},
  264. };
  265. static struct mtd_part_parser uimage_netgear_parser = {
  266. .owner = THIS_MODULE,
  267. .name = "netgear-fw",
  268. .of_match_table = mtdsplit_uimage_netgear_of_match_table,
  269. .parse_fn = mtdsplit_uimage_parse_netgear,
  270. .type = MTD_PARSER_TYPE_FIRMWARE,
  271. };
  272. /**************************************************
  273. * Edimax
  274. **************************************************/
  275. #define FW_EDIMAX_OFFSET 20
  276. #define FW_MAGIC_EDIMAX 0x43535953
  277. static ssize_t uimage_find_edimax(u_char *buf, size_t len, int *extralen)
  278. {
  279. u32 *magic;
  280. if (len < FW_EDIMAX_OFFSET + sizeof(struct uimage_header)) {
  281. pr_err("Buffer too small for checking Edimax header\n");
  282. return -ENOSPC;
  283. }
  284. magic = (u32 *)buf;
  285. if (be32_to_cpu(*magic) != FW_MAGIC_EDIMAX)
  286. return -EINVAL;
  287. if (!uimage_verify_default(buf + FW_EDIMAX_OFFSET, len, extralen))
  288. return FW_EDIMAX_OFFSET;
  289. return -EINVAL;
  290. }
  291. static int
  292. mtdsplit_uimage_parse_edimax(struct mtd_info *master,
  293. const struct mtd_partition **pparts,
  294. struct mtd_part_parser_data *data)
  295. {
  296. return __mtdsplit_parse_uimage(master, pparts, data,
  297. uimage_find_edimax);
  298. }
  299. static const struct of_device_id mtdsplit_uimage_edimax_of_match_table[] = {
  300. { .compatible = "edimax,uimage" },
  301. {},
  302. };
  303. static struct mtd_part_parser uimage_edimax_parser = {
  304. .owner = THIS_MODULE,
  305. .name = "edimax-fw",
  306. .of_match_table = mtdsplit_uimage_edimax_of_match_table,
  307. .parse_fn = mtdsplit_uimage_parse_edimax,
  308. .type = MTD_PARSER_TYPE_FIRMWARE,
  309. };
  310. /**************************************************
  311. * Fon(Foxconn)
  312. **************************************************/
  313. #define FONFXC_PAD_LEN 32
  314. static ssize_t uimage_find_fonfxc(u_char *buf, size_t len, int *extralen)
  315. {
  316. if (uimage_verify_default(buf, len, extralen) < 0)
  317. return -EINVAL;
  318. *extralen = FONFXC_PAD_LEN;
  319. return 0;
  320. }
  321. static int
  322. mtdsplit_uimage_parse_fonfxc(struct mtd_info *master,
  323. const struct mtd_partition **pparts,
  324. struct mtd_part_parser_data *data)
  325. {
  326. return __mtdsplit_parse_uimage(master, pparts, data,
  327. uimage_find_fonfxc);
  328. }
  329. static const struct of_device_id mtdsplit_uimage_fonfxc_of_match_table[] = {
  330. { .compatible = "fonfxc,uimage" },
  331. {},
  332. };
  333. static struct mtd_part_parser uimage_fonfxc_parser = {
  334. .owner = THIS_MODULE,
  335. .name = "fonfxc-fw",
  336. .of_match_table = mtdsplit_uimage_fonfxc_of_match_table,
  337. .parse_fn = mtdsplit_uimage_parse_fonfxc,
  338. };
  339. /**************************************************
  340. * OKLI (OpenWrt Kernel Loader Image)
  341. **************************************************/
  342. #define IH_MAGIC_OKLI 0x4f4b4c49
  343. static ssize_t uimage_verify_okli(u_char *buf, size_t len, int *extralen)
  344. {
  345. struct uimage_header *header = (struct uimage_header *)buf;
  346. /* default sanity checks */
  347. if (be32_to_cpu(header->ih_magic) != IH_MAGIC_OKLI) {
  348. pr_debug("invalid uImage magic: %08x\n",
  349. be32_to_cpu(header->ih_magic));
  350. return -EINVAL;
  351. }
  352. if (header->ih_os != IH_OS_LINUX) {
  353. pr_debug("invalid uImage OS: %08x\n",
  354. be32_to_cpu(header->ih_os));
  355. return -EINVAL;
  356. }
  357. if (header->ih_type != IH_TYPE_KERNEL) {
  358. pr_debug("invalid uImage type: %08x\n",
  359. be32_to_cpu(header->ih_type));
  360. return -EINVAL;
  361. }
  362. return 0;
  363. }
  364. static int
  365. mtdsplit_uimage_parse_okli(struct mtd_info *master,
  366. const struct mtd_partition **pparts,
  367. struct mtd_part_parser_data *data)
  368. {
  369. return __mtdsplit_parse_uimage(master, pparts, data,
  370. uimage_verify_okli);
  371. }
  372. static const struct of_device_id mtdsplit_uimage_okli_of_match_table[] = {
  373. { .compatible = "openwrt,okli" },
  374. {},
  375. };
  376. static struct mtd_part_parser uimage_okli_parser = {
  377. .owner = THIS_MODULE,
  378. .name = "okli-fw",
  379. .of_match_table = mtdsplit_uimage_okli_of_match_table,
  380. .parse_fn = mtdsplit_uimage_parse_okli,
  381. };
  382. /**************************************************
  383. * Init
  384. **************************************************/
  385. static int __init mtdsplit_uimage_init(void)
  386. {
  387. register_mtd_parser(&uimage_generic_parser);
  388. register_mtd_parser(&uimage_netgear_parser);
  389. register_mtd_parser(&uimage_edimax_parser);
  390. register_mtd_parser(&uimage_fonfxc_parser);
  391. register_mtd_parser(&uimage_okli_parser);
  392. return 0;
  393. }
  394. module_init(mtdsplit_uimage_init);