mtdsplit_uimage.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /*
  2. * Copyright (C) 2013 Gabor Juhos <juhosg@openwrt.org>
  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/byteorder/generic.h>
  18. #include "mtdsplit.h"
  19. /*
  20. * uimage_header itself is only 64B, but it may be prepended with another data.
  21. * Currently the biggest size is for Edimax devices: 20B + 64B
  22. */
  23. #define MAX_HEADER_LEN 84
  24. #define IH_MAGIC 0x27051956 /* Image Magic Number */
  25. #define IH_NMLEN 32 /* Image Name Length */
  26. #define IH_OS_LINUX 5 /* Linux */
  27. #define IH_TYPE_KERNEL 2 /* OS Kernel Image */
  28. #define IH_TYPE_FILESYSTEM 7 /* Filesystem Image */
  29. /*
  30. * Legacy format image header,
  31. * all data in network byte order (aka natural aka bigendian).
  32. */
  33. struct uimage_header {
  34. uint32_t ih_magic; /* Image Header Magic Number */
  35. uint32_t ih_hcrc; /* Image Header CRC Checksum */
  36. uint32_t ih_time; /* Image Creation Timestamp */
  37. uint32_t ih_size; /* Image Data Size */
  38. uint32_t ih_load; /* Data Load Address */
  39. uint32_t ih_ep; /* Entry Point Address */
  40. uint32_t ih_dcrc; /* Image Data CRC Checksum */
  41. uint8_t ih_os; /* Operating System */
  42. uint8_t ih_arch; /* CPU architecture */
  43. uint8_t ih_type; /* Image Type */
  44. uint8_t ih_comp; /* Compression Type */
  45. uint8_t ih_name[IH_NMLEN]; /* Image Name */
  46. };
  47. static int
  48. read_uimage_header(struct mtd_info *mtd, size_t offset, u_char *buf,
  49. size_t header_len)
  50. {
  51. size_t retlen;
  52. int ret;
  53. ret = mtd_read(mtd, offset, header_len, &retlen, buf);
  54. if (ret) {
  55. pr_debug("read error in \"%s\"\n", mtd->name);
  56. return ret;
  57. }
  58. if (retlen != header_len) {
  59. pr_debug("short read in \"%s\"\n", mtd->name);
  60. return -EIO;
  61. }
  62. return 0;
  63. }
  64. /**
  65. * __mtdsplit_parse_uimage - scan partition and create kernel + rootfs parts
  66. *
  67. * @find_header: function to call for a block of data that will return offset
  68. * of a valid uImage header if found
  69. */
  70. static int __mtdsplit_parse_uimage(struct mtd_info *master,
  71. struct mtd_partition **pparts,
  72. struct mtd_part_parser_data *data,
  73. ssize_t (*find_header)(u_char *buf, size_t len))
  74. {
  75. struct mtd_partition *parts;
  76. u_char *buf;
  77. int nr_parts;
  78. size_t offset;
  79. size_t uimage_offset;
  80. size_t uimage_size = 0;
  81. size_t rootfs_offset;
  82. size_t rootfs_size = 0;
  83. int uimage_part, rf_part;
  84. int ret;
  85. enum mtdsplit_part_type type;
  86. nr_parts = 2;
  87. parts = kzalloc(nr_parts * sizeof(*parts), GFP_KERNEL);
  88. if (!parts)
  89. return -ENOMEM;
  90. buf = vmalloc(MAX_HEADER_LEN);
  91. if (!buf) {
  92. ret = -ENOMEM;
  93. goto err_free_parts;
  94. }
  95. /* find uImage on erase block boundaries */
  96. for (offset = 0; offset < master->size; offset += master->erasesize) {
  97. struct uimage_header *header;
  98. uimage_size = 0;
  99. ret = read_uimage_header(master, offset, buf, MAX_HEADER_LEN);
  100. if (ret)
  101. continue;
  102. ret = find_header(buf, MAX_HEADER_LEN);
  103. if (ret < 0) {
  104. pr_debug("no valid uImage found in \"%s\" at offset %llx\n",
  105. master->name, (unsigned long long) offset);
  106. continue;
  107. }
  108. header = (struct uimage_header *)(buf + ret);
  109. uimage_size = sizeof(*header) + be32_to_cpu(header->ih_size) + ret;
  110. if ((offset + uimage_size) > master->size) {
  111. pr_debug("uImage exceeds MTD device \"%s\"\n",
  112. master->name);
  113. continue;
  114. }
  115. break;
  116. }
  117. if (uimage_size == 0) {
  118. pr_debug("no uImage found in \"%s\"\n", master->name);
  119. ret = -ENODEV;
  120. goto err_free_buf;
  121. }
  122. uimage_offset = offset;
  123. if (uimage_offset == 0) {
  124. uimage_part = 0;
  125. rf_part = 1;
  126. /* find the roots after the uImage */
  127. ret = mtd_find_rootfs_from(master, uimage_offset + uimage_size,
  128. master->size, &rootfs_offset, &type);
  129. if (ret) {
  130. pr_debug("no rootfs after uImage in \"%s\"\n",
  131. master->name);
  132. goto err_free_buf;
  133. }
  134. rootfs_size = master->size - rootfs_offset;
  135. uimage_size = rootfs_offset - uimage_offset;
  136. } else {
  137. rf_part = 0;
  138. uimage_part = 1;
  139. /* check rootfs presence at offset 0 */
  140. ret = mtd_check_rootfs_magic(master, 0, &type);
  141. if (ret) {
  142. pr_debug("no rootfs before uImage in \"%s\"\n",
  143. master->name);
  144. goto err_free_buf;
  145. }
  146. rootfs_offset = 0;
  147. rootfs_size = uimage_offset;
  148. }
  149. if (rootfs_size == 0) {
  150. pr_debug("no rootfs found in \"%s\"\n", master->name);
  151. ret = -ENODEV;
  152. goto err_free_buf;
  153. }
  154. parts[uimage_part].name = KERNEL_PART_NAME;
  155. parts[uimage_part].offset = uimage_offset;
  156. parts[uimage_part].size = uimage_size;
  157. if (type == MTDSPLIT_PART_TYPE_UBI)
  158. parts[rf_part].name = UBI_PART_NAME;
  159. else
  160. parts[rf_part].name = ROOTFS_PART_NAME;
  161. parts[rf_part].offset = rootfs_offset;
  162. parts[rf_part].size = rootfs_size;
  163. vfree(buf);
  164. *pparts = parts;
  165. return nr_parts;
  166. err_free_buf:
  167. vfree(buf);
  168. err_free_parts:
  169. kfree(parts);
  170. return ret;
  171. }
  172. static ssize_t uimage_verify_default(u_char *buf, size_t len)
  173. {
  174. struct uimage_header *header = (struct uimage_header *)buf;
  175. /* default sanity checks */
  176. if (be32_to_cpu(header->ih_magic) != IH_MAGIC) {
  177. pr_debug("invalid uImage magic: %08x\n",
  178. be32_to_cpu(header->ih_magic));
  179. return -EINVAL;
  180. }
  181. if (header->ih_os != IH_OS_LINUX) {
  182. pr_debug("invalid uImage OS: %08x\n",
  183. be32_to_cpu(header->ih_os));
  184. return -EINVAL;
  185. }
  186. if (header->ih_type != IH_TYPE_KERNEL) {
  187. pr_debug("invalid uImage type: %08x\n",
  188. be32_to_cpu(header->ih_type));
  189. return -EINVAL;
  190. }
  191. return 0;
  192. }
  193. static int
  194. mtdsplit_uimage_parse_generic(struct mtd_info *master,
  195. struct mtd_partition **pparts,
  196. struct mtd_part_parser_data *data)
  197. {
  198. return __mtdsplit_parse_uimage(master, pparts, data,
  199. uimage_verify_default);
  200. }
  201. static struct mtd_part_parser uimage_generic_parser = {
  202. .owner = THIS_MODULE,
  203. .name = "uimage-fw",
  204. .parse_fn = mtdsplit_uimage_parse_generic,
  205. .type = MTD_PARSER_TYPE_FIRMWARE,
  206. };
  207. #define FW_MAGIC_WNR2000V1 0x32303031
  208. #define FW_MAGIC_WNR2000V3 0x32303033
  209. #define FW_MAGIC_WNR2000V4 0x32303034
  210. #define FW_MAGIC_WNR2200 0x32323030
  211. #define FW_MAGIC_WNR612V2 0x32303631
  212. #define FW_MAGIC_WNR1000V2 0x31303031
  213. #define FW_MAGIC_WNR1000V2_VC 0x31303030
  214. #define FW_MAGIC_WNDR3700 0x33373030
  215. #define FW_MAGIC_WNDR3700V2 0x33373031
  216. #define FW_MAGIC_WPN824N 0x31313030
  217. static ssize_t uimage_verify_wndr3700(u_char *buf, size_t len)
  218. {
  219. struct uimage_header *header = (struct uimage_header *)buf;
  220. uint8_t expected_type = IH_TYPE_FILESYSTEM;
  221. switch be32_to_cpu(header->ih_magic) {
  222. case FW_MAGIC_WNR612V2:
  223. case FW_MAGIC_WNR1000V2:
  224. case FW_MAGIC_WNR1000V2_VC:
  225. case FW_MAGIC_WNR2000V1:
  226. case FW_MAGIC_WNR2000V3:
  227. case FW_MAGIC_WNR2200:
  228. case FW_MAGIC_WNDR3700:
  229. case FW_MAGIC_WNDR3700V2:
  230. case FW_MAGIC_WPN824N:
  231. break;
  232. case FW_MAGIC_WNR2000V4:
  233. expected_type = IH_TYPE_KERNEL;
  234. break;
  235. default:
  236. return -EINVAL;
  237. }
  238. if (header->ih_os != IH_OS_LINUX ||
  239. header->ih_type != expected_type)
  240. return -EINVAL;
  241. return 0;
  242. }
  243. static int
  244. mtdsplit_uimage_parse_netgear(struct mtd_info *master,
  245. struct mtd_partition **pparts,
  246. struct mtd_part_parser_data *data)
  247. {
  248. return __mtdsplit_parse_uimage(master, pparts, data,
  249. uimage_verify_wndr3700);
  250. }
  251. static struct mtd_part_parser uimage_netgear_parser = {
  252. .owner = THIS_MODULE,
  253. .name = "netgear-fw",
  254. .parse_fn = mtdsplit_uimage_parse_netgear,
  255. .type = MTD_PARSER_TYPE_FIRMWARE,
  256. };
  257. /**************************************************
  258. * Edimax
  259. **************************************************/
  260. #define FW_EDIMAX_OFFSET 20
  261. #define FW_MAGIC_EDIMAX 0x43535953
  262. static ssize_t uimage_find_edimax(u_char *buf, size_t len)
  263. {
  264. u32 *magic;
  265. if (len < FW_EDIMAX_OFFSET + sizeof(struct uimage_header)) {
  266. pr_err("Buffer too small for checking Edimax header\n");
  267. return -ENOSPC;
  268. }
  269. magic = (u32 *)buf;
  270. if (be32_to_cpu(*magic) != FW_MAGIC_EDIMAX)
  271. return -EINVAL;
  272. if (!uimage_verify_default(buf + FW_EDIMAX_OFFSET, len))
  273. return FW_EDIMAX_OFFSET;
  274. return -EINVAL;
  275. }
  276. static int
  277. mtdsplit_uimage_parse_edimax(struct mtd_info *master,
  278. struct mtd_partition **pparts,
  279. struct mtd_part_parser_data *data)
  280. {
  281. return __mtdsplit_parse_uimage(master, pparts, data,
  282. uimage_find_edimax);
  283. }
  284. static struct mtd_part_parser uimage_edimax_parser = {
  285. .owner = THIS_MODULE,
  286. .name = "edimax-fw",
  287. .parse_fn = mtdsplit_uimage_parse_edimax,
  288. .type = MTD_PARSER_TYPE_FIRMWARE,
  289. };
  290. /**************************************************
  291. * Init
  292. **************************************************/
  293. static int __init mtdsplit_uimage_init(void)
  294. {
  295. register_mtd_parser(&uimage_generic_parser);
  296. register_mtd_parser(&uimage_netgear_parser);
  297. register_mtd_parser(&uimage_edimax_parser);
  298. return 0;
  299. }
  300. module_init(mtdsplit_uimage_init);