mtdsplit_jimage.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * Copyright (C) 2018 Paweł Dembicki <[email protected]>
  3. *
  4. * Based on: mtdsplit_uimage.c
  5. * Copyright (C) 2013 Gabor Juhos <[email protected]>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License version 2 as published
  9. * by the Free Software Foundation.
  10. *
  11. */
  12. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/kernel.h>
  16. #include <linux/slab.h>
  17. #include <linux/vmalloc.h>
  18. #include <linux/mtd/mtd.h>
  19. #include <linux/mtd/partitions.h>
  20. #include <linux/byteorder/generic.h>
  21. #include "mtdsplit.h"
  22. #define MAX_HEADER_LEN ( STAG_SIZE + SCH2_SIZE )
  23. #define STAG_SIZE 16
  24. #define STAG_ID 0x04
  25. #define STAG_MAGIC 0x2B24
  26. #define SCH2_SIZE 40
  27. #define SCH2_MAGIC 0x2124
  28. #define SCH2_VER 0x02
  29. /*
  30. * Jboot image header,
  31. * all data in little endian.
  32. */
  33. struct jimage_header //stag + sch2 jboot joined headers
  34. {
  35. uint8_t stag_cmark; // in factory 0xFF , in sysupgrade must be the same as stag_id
  36. uint8_t stag_id; // 0x04
  37. uint16_t stag_magic; //magic 0x2B24
  38. uint32_t stag_time_stamp; // timestamp calculated in jboot way
  39. uint32_t stag_image_length; // lentgh of kernel + sch2 header
  40. uint16_t stag_image_checksum; // negated jboot_checksum of sch2 + kernel
  41. uint16_t stag_tag_checksum; // negated jboot_checksum of stag header data
  42. uint16_t sch2_magic; // magic 0x2124
  43. uint8_t sch2_cp_type; // 0x00 for flat, 0x01 for jz, 0x02 for gzip, 0x03 for lzma
  44. uint8_t sch2_version; // 0x02 for sch2
  45. uint32_t sch2_ram_addr; // ram entry address
  46. uint32_t sch2_image_len; // kernel image length
  47. uint32_t sch2_image_crc32; // kernel image crc
  48. uint32_t sch2_start_addr; // ram start address
  49. uint32_t sch2_rootfs_addr; // rootfs flash address
  50. uint32_t sch2_rootfs_len; // rootfls length
  51. uint32_t sch2_rootfs_crc32; // rootfs crc32
  52. uint32_t sch2_header_crc32; // sch2 header crc32, durring calculation this area is replaced by zero
  53. uint16_t sch2_header_length; // sch2 header length: 0x28
  54. uint16_t sch2_cmd_line_length; // cmd line length, known zeros
  55. };
  56. static int
  57. read_jimage_header(struct mtd_info *mtd, size_t offset, u_char *buf,
  58. size_t header_len)
  59. {
  60. size_t retlen;
  61. int ret;
  62. ret = mtd_read(mtd, offset, header_len, &retlen, buf);
  63. if (ret) {
  64. pr_debug("read error in \"%s\"\n", mtd->name);
  65. return ret;
  66. }
  67. if (retlen != header_len) {
  68. pr_debug("short read in \"%s\"\n", mtd->name);
  69. return -EIO;
  70. }
  71. return 0;
  72. }
  73. /**
  74. * __mtdsplit_parse_jimage - scan partition and create kernel + rootfs parts
  75. *
  76. * @find_header: function to call for a block of data that will return offset
  77. * of a valid jImage header if found
  78. */
  79. static int __mtdsplit_parse_jimage(struct mtd_info *master,
  80. const struct mtd_partition **pparts,
  81. struct mtd_part_parser_data *data,
  82. ssize_t (*find_header)(u_char *buf, size_t len))
  83. {
  84. struct mtd_partition *parts;
  85. u_char *buf;
  86. int nr_parts;
  87. size_t offset;
  88. size_t jimage_offset;
  89. size_t jimage_size = 0;
  90. size_t rootfs_offset;
  91. size_t rootfs_size = 0;
  92. int jimage_part, rf_part;
  93. int ret;
  94. enum mtdsplit_part_type type;
  95. nr_parts = 2;
  96. parts = kzalloc(nr_parts * sizeof(*parts), GFP_KERNEL);
  97. if (!parts)
  98. return -ENOMEM;
  99. buf = vmalloc(MAX_HEADER_LEN);
  100. if (!buf) {
  101. ret = -ENOMEM;
  102. goto err_free_parts;
  103. }
  104. /* find jImage on erase block boundaries */
  105. for (offset = 0; offset < master->size; offset += master->erasesize) {
  106. struct jimage_header *header;
  107. jimage_size = 0;
  108. ret = read_jimage_header(master, offset, buf, MAX_HEADER_LEN);
  109. if (ret)
  110. continue;
  111. ret = find_header(buf, MAX_HEADER_LEN);
  112. if (ret < 0) {
  113. pr_debug("no valid jImage found in \"%s\" at offset %llx\n",
  114. master->name, (unsigned long long) offset);
  115. continue;
  116. }
  117. header = (struct jimage_header *)(buf + ret);
  118. jimage_size = sizeof(*header) + header->sch2_image_len + ret;
  119. if ((offset + jimage_size) > master->size) {
  120. pr_debug("jImage exceeds MTD device \"%s\"\n",
  121. master->name);
  122. continue;
  123. }
  124. break;
  125. }
  126. if (jimage_size == 0) {
  127. pr_debug("no jImage found in \"%s\"\n", master->name);
  128. ret = -ENODEV;
  129. goto err_free_buf;
  130. }
  131. jimage_offset = offset;
  132. if (jimage_offset == 0) {
  133. jimage_part = 0;
  134. rf_part = 1;
  135. /* find the roots after the jImage */
  136. ret = mtd_find_rootfs_from(master, jimage_offset + jimage_size,
  137. master->size, &rootfs_offset, &type);
  138. if (ret) {
  139. pr_debug("no rootfs after jImage in \"%s\"\n",
  140. master->name);
  141. goto err_free_buf;
  142. }
  143. rootfs_size = master->size - rootfs_offset;
  144. jimage_size = rootfs_offset - jimage_offset;
  145. } else {
  146. rf_part = 0;
  147. jimage_part = 1;
  148. /* check rootfs presence at offset 0 */
  149. ret = mtd_check_rootfs_magic(master, 0, &type);
  150. if (ret) {
  151. pr_debug("no rootfs before jImage in \"%s\"\n",
  152. master->name);
  153. goto err_free_buf;
  154. }
  155. rootfs_offset = 0;
  156. rootfs_size = jimage_offset;
  157. }
  158. if (rootfs_size == 0) {
  159. pr_debug("no rootfs found in \"%s\"\n", master->name);
  160. ret = -ENODEV;
  161. goto err_free_buf;
  162. }
  163. parts[jimage_part].name = KERNEL_PART_NAME;
  164. parts[jimage_part].offset = jimage_offset;
  165. parts[jimage_part].size = jimage_size;
  166. if (type == MTDSPLIT_PART_TYPE_UBI)
  167. parts[rf_part].name = UBI_PART_NAME;
  168. else
  169. parts[rf_part].name = ROOTFS_PART_NAME;
  170. parts[rf_part].offset = rootfs_offset;
  171. parts[rf_part].size = rootfs_size;
  172. vfree(buf);
  173. *pparts = parts;
  174. return nr_parts;
  175. err_free_buf:
  176. vfree(buf);
  177. err_free_parts:
  178. kfree(parts);
  179. return ret;
  180. }
  181. static ssize_t jimage_verify_default(u_char *buf, size_t len)
  182. {
  183. struct jimage_header *header = (struct jimage_header *)buf;
  184. /* default sanity checks */
  185. if (header->stag_magic != STAG_MAGIC) {
  186. pr_debug("invalid jImage stag header magic: %04x\n",
  187. header->stag_magic);
  188. return -EINVAL;
  189. }
  190. if (header->sch2_magic != SCH2_MAGIC) {
  191. pr_debug("invalid jImage sch2 header magic: %04x\n",
  192. header->stag_magic);
  193. return -EINVAL;
  194. }
  195. if (header->stag_cmark != header->stag_id) {
  196. pr_debug("invalid jImage stag header cmark: %02x\n",
  197. header->stag_magic);
  198. return -EINVAL;
  199. }
  200. if (header->stag_id != STAG_ID) {
  201. pr_debug("invalid jImage stag header id: %02x\n",
  202. header->stag_magic);
  203. return -EINVAL;
  204. }
  205. if (header->sch2_version != SCH2_VER) {
  206. pr_debug("invalid jImage sch2 header version: %02x\n",
  207. header->stag_magic);
  208. return -EINVAL;
  209. }
  210. return 0;
  211. }
  212. static int
  213. mtdsplit_jimage_parse_generic(struct mtd_info *master,
  214. const struct mtd_partition **pparts,
  215. struct mtd_part_parser_data *data)
  216. {
  217. return __mtdsplit_parse_jimage(master, pparts, data,
  218. jimage_verify_default);
  219. }
  220. static const struct of_device_id mtdsplit_jimage_of_match_table[] = {
  221. { .compatible = "amit,jimage" },
  222. {},
  223. };
  224. static struct mtd_part_parser jimage_generic_parser = {
  225. .owner = THIS_MODULE,
  226. .name = "jimage-fw",
  227. .of_match_table = mtdsplit_jimage_of_match_table,
  228. .parse_fn = mtdsplit_jimage_parse_generic,
  229. .type = MTD_PARSER_TYPE_FIRMWARE,
  230. };
  231. /**************************************************
  232. * Init
  233. **************************************************/
  234. static int __init mtdsplit_jimage_init(void)
  235. {
  236. register_mtd_parser(&jimage_generic_parser);
  237. return 0;
  238. }
  239. module_init(mtdsplit_jimage_init);