mtdsplit_uimage.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. * Legacy format image header,
  24. * all data in network byte order (aka natural aka bigendian).
  25. */
  26. struct uimage_header {
  27. uint32_t ih_magic; /* Image Header Magic Number */
  28. uint32_t ih_hcrc; /* Image Header CRC Checksum */
  29. uint32_t ih_time; /* Image Creation Timestamp */
  30. uint32_t ih_size; /* Image Data Size */
  31. uint32_t ih_load; /* Data Load Address */
  32. uint32_t ih_ep; /* Entry Point Address */
  33. uint32_t ih_dcrc; /* Image Data CRC Checksum */
  34. uint8_t ih_os; /* Operating System */
  35. uint8_t ih_arch; /* CPU architecture */
  36. uint8_t ih_type; /* Image Type */
  37. uint8_t ih_comp; /* Compression Type */
  38. uint8_t ih_name[IH_NMLEN]; /* Image Name */
  39. };
  40. static int
  41. read_uimage_header(struct mtd_info *mtd, size_t offset, u_char *buf,
  42. size_t header_len)
  43. {
  44. size_t retlen;
  45. int ret;
  46. ret = mtd_read(mtd, offset, header_len, &retlen, buf);
  47. if (ret) {
  48. pr_debug("read error in \"%s\"\n", mtd->name);
  49. return ret;
  50. }
  51. if (retlen != header_len) {
  52. pr_debug("short read in \"%s\"\n", mtd->name);
  53. return -EIO;
  54. }
  55. return 0;
  56. }
  57. static void uimage_parse_dt(struct mtd_info *master, int *extralen,
  58. u32 *ih_magic, u32 *ih_type,
  59. u32 *header_offset, u32 *part_magic)
  60. {
  61. struct device_node *np = mtd_get_of_node(master);
  62. if (!np || !of_device_is_compatible(np, "openwrt,uimage"))
  63. return;
  64. if (!of_property_read_u32(np, "openwrt,padding", extralen))
  65. pr_debug("got openwrt,padding=%d from device-tree\n", *extralen);
  66. if (!of_property_read_u32(np, "openwrt,ih-magic", ih_magic))
  67. pr_debug("got openwrt,ih-magic=%08x from device-tree\n", *ih_magic);
  68. if (!of_property_read_u32(np, "openwrt,ih-type", ih_type))
  69. pr_debug("got openwrt,ih-type=%08x from device-tree\n", *ih_type);
  70. if (!of_property_read_u32(np, "openwrt,offset", header_offset))
  71. pr_debug("got ih-start=%u from device-tree\n", *header_offset);
  72. if (!of_property_read_u32(np, "openwrt,partition-magic", part_magic))
  73. pr_debug("got openwrt,partition-magic=%08x from device-tree\n", *part_magic);
  74. }
  75. static ssize_t uimage_verify_default(u_char *buf, u32 ih_magic, u32 ih_type)
  76. {
  77. struct uimage_header *header = (struct uimage_header *)buf;
  78. /* default sanity checks */
  79. if (be32_to_cpu(header->ih_magic) != ih_magic) {
  80. pr_debug("invalid uImage magic: %08x != %08x\n",
  81. be32_to_cpu(header->ih_magic), ih_magic);
  82. return -EINVAL;
  83. }
  84. if (header->ih_os != IH_OS_LINUX) {
  85. pr_debug("invalid uImage OS: %08x != %08x\n",
  86. be32_to_cpu(header->ih_os), IH_OS_LINUX);
  87. return -EINVAL;
  88. }
  89. if (header->ih_type != ih_type) {
  90. pr_debug("invalid uImage type: %08x != %08x\n",
  91. be32_to_cpu(header->ih_type), ih_type);
  92. return -EINVAL;
  93. }
  94. return 0;
  95. }
  96. /**
  97. * __mtdsplit_parse_uimage - scan partition and create kernel + rootfs parts
  98. *
  99. * @find_header: function to call for a block of data that will return offset
  100. * and tail padding length of a valid uImage header if found
  101. */
  102. static int __mtdsplit_parse_uimage(struct mtd_info *master,
  103. const struct mtd_partition **pparts,
  104. struct mtd_part_parser_data *data)
  105. {
  106. struct mtd_partition *parts;
  107. u_char *buf;
  108. int nr_parts;
  109. size_t offset;
  110. size_t uimage_offset;
  111. size_t uimage_size = 0;
  112. size_t rootfs_offset;
  113. size_t rootfs_size = 0;
  114. size_t buflen;
  115. int uimage_part, rf_part;
  116. int ret;
  117. int extralen = 0;
  118. u32 ih_magic = IH_MAGIC;
  119. u32 ih_type = IH_TYPE_KERNEL;
  120. u32 header_offset = 0;
  121. u32 part_magic = 0;
  122. enum mtdsplit_part_type type;
  123. nr_parts = 2;
  124. parts = kzalloc(nr_parts * sizeof(*parts), GFP_KERNEL);
  125. if (!parts)
  126. return -ENOMEM;
  127. uimage_parse_dt(master, &extralen, &ih_magic, &ih_type, &header_offset, &part_magic);
  128. buflen = sizeof(struct uimage_header) + header_offset;
  129. buf = vmalloc(buflen);
  130. if (!buf) {
  131. ret = -ENOMEM;
  132. goto err_free_parts;
  133. }
  134. /* find uImage on erase block boundaries */
  135. for (offset = 0; offset < master->size; offset += master->erasesize) {
  136. struct uimage_header *header;
  137. uimage_size = 0;
  138. ret = read_uimage_header(master, offset, buf, buflen);
  139. if (ret)
  140. continue;
  141. /* verify optional partition magic before uimage header */
  142. if (header_offset && part_magic && (be32_to_cpu(*(u32 *)buf) != part_magic))
  143. continue;
  144. ret = uimage_verify_default(buf + header_offset, ih_magic, ih_type);
  145. if (ret < 0) {
  146. pr_debug("no valid uImage found in \"%s\" at offset %llx\n",
  147. master->name, (unsigned long long) offset);
  148. continue;
  149. }
  150. header = (struct uimage_header *)(buf + header_offset);
  151. uimage_size = sizeof(*header) +
  152. be32_to_cpu(header->ih_size) + header_offset + extralen;
  153. if ((offset + uimage_size) > master->size) {
  154. pr_debug("uImage exceeds MTD device \"%s\"\n",
  155. master->name);
  156. continue;
  157. }
  158. break;
  159. }
  160. if (uimage_size == 0) {
  161. pr_debug("no uImage found in \"%s\"\n", master->name);
  162. ret = -ENODEV;
  163. goto err_free_buf;
  164. }
  165. uimage_offset = offset;
  166. if (uimage_offset == 0) {
  167. uimage_part = 0;
  168. rf_part = 1;
  169. /* find the roots after the uImage */
  170. ret = mtd_find_rootfs_from(master, uimage_offset + uimage_size,
  171. master->size, &rootfs_offset, &type);
  172. if (ret) {
  173. pr_debug("no rootfs after uImage in \"%s\"\n",
  174. master->name);
  175. goto err_free_buf;
  176. }
  177. rootfs_size = master->size - rootfs_offset;
  178. uimage_size = rootfs_offset - uimage_offset;
  179. } else {
  180. rf_part = 0;
  181. uimage_part = 1;
  182. /* check rootfs presence at offset 0 */
  183. ret = mtd_check_rootfs_magic(master, 0, &type);
  184. if (ret) {
  185. pr_debug("no rootfs before uImage in \"%s\"\n",
  186. master->name);
  187. goto err_free_buf;
  188. }
  189. rootfs_offset = 0;
  190. rootfs_size = uimage_offset;
  191. }
  192. if (rootfs_size == 0) {
  193. pr_debug("no rootfs found in \"%s\"\n", master->name);
  194. ret = -ENODEV;
  195. goto err_free_buf;
  196. }
  197. parts[uimage_part].name = KERNEL_PART_NAME;
  198. parts[uimage_part].offset = uimage_offset;
  199. parts[uimage_part].size = uimage_size;
  200. if (type == MTDSPLIT_PART_TYPE_UBI)
  201. parts[rf_part].name = UBI_PART_NAME;
  202. else
  203. parts[rf_part].name = ROOTFS_PART_NAME;
  204. parts[rf_part].offset = rootfs_offset;
  205. parts[rf_part].size = rootfs_size;
  206. vfree(buf);
  207. *pparts = parts;
  208. return nr_parts;
  209. err_free_buf:
  210. vfree(buf);
  211. err_free_parts:
  212. kfree(parts);
  213. return ret;
  214. }
  215. static const struct of_device_id mtdsplit_uimage_of_match_table[] = {
  216. { .compatible = "denx,uimage" },
  217. { .compatible = "openwrt,uimage" },
  218. {},
  219. };
  220. static struct mtd_part_parser uimage_generic_parser = {
  221. .owner = THIS_MODULE,
  222. .name = "uimage-fw",
  223. .of_match_table = mtdsplit_uimage_of_match_table,
  224. .parse_fn = __mtdsplit_parse_uimage,
  225. .type = MTD_PARSER_TYPE_FIRMWARE,
  226. };
  227. /**************************************************
  228. * Init
  229. **************************************************/
  230. static int __init mtdsplit_uimage_init(void)
  231. {
  232. register_mtd_parser(&uimage_generic_parser);
  233. return 0;
  234. }
  235. module_init(mtdsplit_uimage_init);