mtdsplit_uimage.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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,
  63. u32 *ih_magic, u32 *ih_type,
  64. u32 *header_offset, u32 *part_magic)
  65. {
  66. struct device_node *np = mtd_get_of_node(master);
  67. if (!np || !of_device_is_compatible(np, "openwrt,uimage"))
  68. return;
  69. if (!of_property_read_u32(np, "openwrt,padding", extralen))
  70. pr_debug("got openwrt,padding=%d from device-tree\n", *extralen);
  71. if (!of_property_read_u32(np, "openwrt,ih-magic", ih_magic))
  72. pr_debug("got openwrt,ih-magic=%08x from device-tree\n", *ih_magic);
  73. if (!of_property_read_u32(np, "openwrt,ih-type", ih_type))
  74. pr_debug("got openwrt,ih-type=%08x from device-tree\n", *ih_type);
  75. if (!of_property_read_u32(np, "openwrt,offset", header_offset))
  76. pr_debug("got ih-start=%u from device-tree\n", *header_offset);
  77. if (!of_property_read_u32(np, "openwrt,partition-magic", part_magic))
  78. pr_debug("got openwrt,partition-magic=%08x from device-tree\n", *part_magic);
  79. }
  80. /**
  81. * __mtdsplit_parse_uimage - scan partition and create kernel + rootfs parts
  82. *
  83. * @find_header: function to call for a block of data that will return offset
  84. * and tail padding length of a valid uImage header if found
  85. */
  86. static int __mtdsplit_parse_uimage(struct mtd_info *master,
  87. const struct mtd_partition **pparts,
  88. struct mtd_part_parser_data *data,
  89. ssize_t (*find_header)(u_char *buf, size_t len, u32 ih_magic, u32 ih_type))
  90. {
  91. struct mtd_partition *parts;
  92. u_char *buf;
  93. int nr_parts;
  94. size_t offset;
  95. size_t uimage_offset;
  96. size_t uimage_size = 0;
  97. size_t rootfs_offset;
  98. size_t rootfs_size = 0;
  99. size_t buflen;
  100. int uimage_part, rf_part;
  101. int ret;
  102. int extralen = 0;
  103. u32 ih_magic = IH_MAGIC;
  104. u32 ih_type = IH_TYPE_KERNEL;
  105. u32 header_offset = 0;
  106. u32 part_magic = 0;
  107. enum mtdsplit_part_type type;
  108. nr_parts = 2;
  109. parts = kzalloc(nr_parts * sizeof(*parts), GFP_KERNEL);
  110. if (!parts)
  111. return -ENOMEM;
  112. uimage_parse_dt(master, &extralen, &ih_magic, &ih_type, &header_offset, &part_magic);
  113. buflen = MAX_HEADER_LEN;
  114. buf = vmalloc(buflen);
  115. if (!buf) {
  116. ret = -ENOMEM;
  117. goto err_free_parts;
  118. }
  119. /* find uImage on erase block boundaries */
  120. for (offset = 0; offset < master->size; offset += master->erasesize) {
  121. struct uimage_header *header;
  122. uimage_size = 0;
  123. ret = read_uimage_header(master, offset, buf, buflen);
  124. if (ret)
  125. continue;
  126. /* verify optional partition magic before uimage header */
  127. if (header_offset && part_magic && (be32_to_cpu(*(u32 *)buf) != part_magic))
  128. continue;
  129. ret = find_header(buf + header_offset, buflen, ih_magic, ih_type);
  130. if (ret < 0) {
  131. pr_debug("no valid uImage found in \"%s\" at offset %llx\n",
  132. master->name, (unsigned long long) offset);
  133. continue;
  134. }
  135. /* let uimage_find_edimax override the offset */
  136. if (ret > 0)
  137. header_offset = ret;
  138. header = (struct uimage_header *)(buf + header_offset);
  139. uimage_size = sizeof(*header) +
  140. be32_to_cpu(header->ih_size) + header_offset + extralen;
  141. if ((offset + uimage_size) > master->size) {
  142. pr_debug("uImage exceeds MTD device \"%s\"\n",
  143. master->name);
  144. continue;
  145. }
  146. break;
  147. }
  148. if (uimage_size == 0) {
  149. pr_debug("no uImage found in \"%s\"\n", master->name);
  150. ret = -ENODEV;
  151. goto err_free_buf;
  152. }
  153. uimage_offset = offset;
  154. if (uimage_offset == 0) {
  155. uimage_part = 0;
  156. rf_part = 1;
  157. /* find the roots after the uImage */
  158. ret = mtd_find_rootfs_from(master, uimage_offset + uimage_size,
  159. master->size, &rootfs_offset, &type);
  160. if (ret) {
  161. pr_debug("no rootfs after uImage in \"%s\"\n",
  162. master->name);
  163. goto err_free_buf;
  164. }
  165. rootfs_size = master->size - rootfs_offset;
  166. uimage_size = rootfs_offset - uimage_offset;
  167. } else {
  168. rf_part = 0;
  169. uimage_part = 1;
  170. /* check rootfs presence at offset 0 */
  171. ret = mtd_check_rootfs_magic(master, 0, &type);
  172. if (ret) {
  173. pr_debug("no rootfs before uImage in \"%s\"\n",
  174. master->name);
  175. goto err_free_buf;
  176. }
  177. rootfs_offset = 0;
  178. rootfs_size = uimage_offset;
  179. }
  180. if (rootfs_size == 0) {
  181. pr_debug("no rootfs found in \"%s\"\n", master->name);
  182. ret = -ENODEV;
  183. goto err_free_buf;
  184. }
  185. parts[uimage_part].name = KERNEL_PART_NAME;
  186. parts[uimage_part].offset = uimage_offset;
  187. parts[uimage_part].size = uimage_size;
  188. if (type == MTDSPLIT_PART_TYPE_UBI)
  189. parts[rf_part].name = UBI_PART_NAME;
  190. else
  191. parts[rf_part].name = ROOTFS_PART_NAME;
  192. parts[rf_part].offset = rootfs_offset;
  193. parts[rf_part].size = rootfs_size;
  194. vfree(buf);
  195. *pparts = parts;
  196. return nr_parts;
  197. err_free_buf:
  198. vfree(buf);
  199. err_free_parts:
  200. kfree(parts);
  201. return ret;
  202. }
  203. static ssize_t uimage_verify_default(u_char *buf, size_t len, u32 ih_magic, u32 ih_type)
  204. {
  205. struct uimage_header *header = (struct uimage_header *)buf;
  206. /* default sanity checks */
  207. if (be32_to_cpu(header->ih_magic) != ih_magic) {
  208. pr_debug("invalid uImage magic: %08x != %08x\n",
  209. be32_to_cpu(header->ih_magic), ih_magic);
  210. return -EINVAL;
  211. }
  212. if (header->ih_os != IH_OS_LINUX) {
  213. pr_debug("invalid uImage OS: %08x != %08x\n",
  214. be32_to_cpu(header->ih_os), IH_OS_LINUX);
  215. return -EINVAL;
  216. }
  217. if (header->ih_type != ih_type) {
  218. pr_debug("invalid uImage type: %08x != %08x\n",
  219. be32_to_cpu(header->ih_type), ih_type);
  220. return -EINVAL;
  221. }
  222. return 0;
  223. }
  224. static int
  225. mtdsplit_uimage_parse_generic(struct mtd_info *master,
  226. const struct mtd_partition **pparts,
  227. struct mtd_part_parser_data *data)
  228. {
  229. return __mtdsplit_parse_uimage(master, pparts, data,
  230. uimage_verify_default);
  231. }
  232. static const struct of_device_id mtdsplit_uimage_of_match_table[] = {
  233. { .compatible = "denx,uimage" },
  234. { .compatible = "openwrt,uimage" },
  235. {},
  236. };
  237. static struct mtd_part_parser uimage_generic_parser = {
  238. .owner = THIS_MODULE,
  239. .name = "uimage-fw",
  240. .of_match_table = mtdsplit_uimage_of_match_table,
  241. .parse_fn = mtdsplit_uimage_parse_generic,
  242. .type = MTD_PARSER_TYPE_FIRMWARE,
  243. };
  244. /**************************************************
  245. * Edimax
  246. **************************************************/
  247. #define FW_EDIMAX_OFFSET 20
  248. #define FW_MAGIC_EDIMAX 0x43535953
  249. static ssize_t uimage_find_edimax(u_char *buf, size_t len, u32 ih_magic, u32 ih_type)
  250. {
  251. u32 *magic;
  252. if (len < FW_EDIMAX_OFFSET + sizeof(struct uimage_header)) {
  253. pr_err("Buffer too small for checking Edimax header\n");
  254. return -ENOSPC;
  255. }
  256. magic = (u32 *)buf;
  257. if (be32_to_cpu(*magic) != FW_MAGIC_EDIMAX)
  258. return -EINVAL;
  259. if (!uimage_verify_default(buf + FW_EDIMAX_OFFSET, len, ih_magic, ih_type))
  260. return FW_EDIMAX_OFFSET;
  261. return -EINVAL;
  262. }
  263. static int
  264. mtdsplit_uimage_parse_edimax(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_find_edimax);
  270. }
  271. static const struct of_device_id mtdsplit_uimage_edimax_of_match_table[] = {
  272. { .compatible = "edimax,uimage" },
  273. {},
  274. };
  275. static struct mtd_part_parser uimage_edimax_parser = {
  276. .owner = THIS_MODULE,
  277. .name = "edimax-fw",
  278. .of_match_table = mtdsplit_uimage_edimax_of_match_table,
  279. .parse_fn = mtdsplit_uimage_parse_edimax,
  280. .type = MTD_PARSER_TYPE_FIRMWARE,
  281. };
  282. /**************************************************
  283. * Init
  284. **************************************************/
  285. static int __init mtdsplit_uimage_init(void)
  286. {
  287. register_mtd_parser(&uimage_generic_parser);
  288. register_mtd_parser(&uimage_edimax_parser);
  289. return 0;
  290. }
  291. module_init(mtdsplit_uimage_init);