redboot.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Parse RedBoot-style Flash Image System (FIS) tables and
  4. * produce a Linux partition array to match.
  5. *
  6. * Copyright © 2001 Red Hat UK Limited
  7. * Copyright © 2001-2010 David Woodhouse <[email protected]>
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/slab.h>
  11. #include <linux/init.h>
  12. #include <linux/vmalloc.h>
  13. #include <linux/of.h>
  14. #include <linux/mtd/mtd.h>
  15. #include <linux/mtd/partitions.h>
  16. #include <linux/module.h>
  17. struct fis_image_desc {
  18. unsigned char name[16]; // Null terminated name
  19. u32 flash_base; // Address within FLASH of image
  20. u32 mem_base; // Address in memory where it executes
  21. u32 size; // Length of image
  22. u32 entry_point; // Execution entry point
  23. u32 data_length; // Length of actual data
  24. unsigned char _pad[256 - (16 + 7 * sizeof(u32))];
  25. u32 desc_cksum; // Checksum over image descriptor
  26. u32 file_cksum; // Checksum over image data
  27. };
  28. struct fis_list {
  29. struct fis_image_desc *img;
  30. struct fis_list *next;
  31. };
  32. static int directory = CONFIG_MTD_REDBOOT_DIRECTORY_BLOCK;
  33. module_param(directory, int, 0);
  34. static inline int redboot_checksum(struct fis_image_desc *img)
  35. {
  36. /* RedBoot doesn't actually write the desc_cksum field yet AFAICT */
  37. return 1;
  38. }
  39. static void parse_redboot_of(struct mtd_info *master)
  40. {
  41. struct device_node *np;
  42. struct device_node *npart;
  43. u32 dirblock;
  44. int ret;
  45. np = mtd_get_of_node(master);
  46. if (!np)
  47. return;
  48. npart = of_get_child_by_name(np, "partitions");
  49. if (!npart)
  50. return;
  51. ret = of_property_read_u32(npart, "fis-index-block", &dirblock);
  52. of_node_put(npart);
  53. if (ret)
  54. return;
  55. /*
  56. * Assign the block found in the device tree to the local
  57. * directory block pointer.
  58. */
  59. directory = dirblock;
  60. }
  61. static int parse_redboot_partitions(struct mtd_info *master,
  62. const struct mtd_partition **pparts,
  63. struct mtd_part_parser_data *data)
  64. {
  65. int nrparts = 0;
  66. struct fis_image_desc *buf;
  67. struct mtd_partition *parts;
  68. struct fis_list *fl = NULL, *tmp_fl;
  69. int ret, i;
  70. size_t retlen;
  71. char *names;
  72. char *nullname;
  73. int namelen = 0;
  74. int nulllen = 0;
  75. int numslots;
  76. unsigned long offset;
  77. #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
  78. static char nullstring[] = "unallocated";
  79. #endif
  80. parse_redboot_of(master);
  81. if (directory < 0) {
  82. offset = master->size + directory * master->erasesize;
  83. while (mtd_block_isbad(master, offset)) {
  84. if (!offset) {
  85. nogood:
  86. pr_notice("Failed to find a non-bad block to check for RedBoot partition table\n");
  87. return -EIO;
  88. }
  89. offset -= master->erasesize;
  90. }
  91. } else {
  92. offset = directory * master->erasesize;
  93. while (mtd_block_isbad(master, offset)) {
  94. offset += master->erasesize;
  95. if (offset == master->size)
  96. goto nogood;
  97. }
  98. }
  99. buf = vmalloc(master->erasesize);
  100. if (!buf)
  101. return -ENOMEM;
  102. pr_notice("Searching for RedBoot partition table in %s at offset 0x%lx\n",
  103. master->name, offset);
  104. ret = mtd_read(master, offset, master->erasesize, &retlen,
  105. (void *)buf);
  106. if (ret)
  107. goto out;
  108. if (retlen != master->erasesize) {
  109. ret = -EIO;
  110. goto out;
  111. }
  112. numslots = (master->erasesize / sizeof(struct fis_image_desc));
  113. for (i = 0; i < numslots; i++) {
  114. if (!memcmp(buf[i].name, "FIS directory", 14)) {
  115. /* This is apparently the FIS directory entry for the
  116. * FIS directory itself. The FIS directory size is
  117. * one erase block; if the buf[i].size field is
  118. * swab32(erasesize) then we know we are looking at
  119. * a byte swapped FIS directory - swap all the entries!
  120. * (NOTE: this is 'size' not 'data_length'; size is
  121. * the full size of the entry.)
  122. */
  123. /* RedBoot can combine the FIS directory and
  124. config partitions into a single eraseblock;
  125. we assume wrong-endian if either the swapped
  126. 'size' matches the eraseblock size precisely,
  127. or if the swapped size actually fits in an
  128. eraseblock while the unswapped size doesn't. */
  129. if (swab32(buf[i].size) == master->erasesize ||
  130. (buf[i].size > master->erasesize
  131. && swab32(buf[i].size) < master->erasesize)) {
  132. int j;
  133. /* Update numslots based on actual FIS directory size */
  134. numslots = swab32(buf[i].size) / sizeof(struct fis_image_desc);
  135. for (j = 0; j < numslots; ++j) {
  136. /* A single 0xff denotes a deleted entry.
  137. * Two of them in a row is the end of the table.
  138. */
  139. if (buf[j].name[0] == 0xff) {
  140. if (buf[j].name[1] == 0xff) {
  141. break;
  142. } else {
  143. continue;
  144. }
  145. }
  146. /* The unsigned long fields were written with the
  147. * wrong byte sex, name and pad have no byte sex.
  148. */
  149. swab32s(&buf[j].flash_base);
  150. swab32s(&buf[j].mem_base);
  151. swab32s(&buf[j].size);
  152. swab32s(&buf[j].entry_point);
  153. swab32s(&buf[j].data_length);
  154. swab32s(&buf[j].desc_cksum);
  155. swab32s(&buf[j].file_cksum);
  156. }
  157. } else if (buf[i].size < master->erasesize) {
  158. /* Update numslots based on actual FIS directory size */
  159. numslots = buf[i].size / sizeof(struct fis_image_desc);
  160. }
  161. break;
  162. }
  163. }
  164. if (i == numslots) {
  165. /* Didn't find it */
  166. pr_notice("No RedBoot partition table detected in %s\n",
  167. master->name);
  168. ret = 0;
  169. goto out;
  170. }
  171. for (i = 0; i < numslots; i++) {
  172. struct fis_list *new_fl, **prev;
  173. if (buf[i].name[0] == 0xff) {
  174. if (buf[i].name[1] == 0xff) {
  175. break;
  176. } else {
  177. continue;
  178. }
  179. }
  180. if (!redboot_checksum(&buf[i]))
  181. break;
  182. new_fl = kmalloc(sizeof(struct fis_list), GFP_KERNEL);
  183. namelen += strlen(buf[i].name) + 1;
  184. if (!new_fl) {
  185. ret = -ENOMEM;
  186. goto out;
  187. }
  188. new_fl->img = &buf[i];
  189. if (data && data->origin)
  190. buf[i].flash_base -= data->origin;
  191. else
  192. buf[i].flash_base &= master->size - 1;
  193. /* I'm sure the JFFS2 code has done me permanent damage.
  194. * I now think the following is _normal_
  195. */
  196. prev = &fl;
  197. while (*prev && (*prev)->img->flash_base < new_fl->img->flash_base)
  198. prev = &(*prev)->next;
  199. new_fl->next = *prev;
  200. *prev = new_fl;
  201. nrparts++;
  202. }
  203. #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
  204. if (fl->img->flash_base) {
  205. nrparts++;
  206. nulllen = sizeof(nullstring);
  207. }
  208. for (tmp_fl = fl; tmp_fl->next; tmp_fl = tmp_fl->next) {
  209. if (tmp_fl->img->flash_base + tmp_fl->img->size + master->erasesize <= tmp_fl->next->img->flash_base) {
  210. nrparts++;
  211. nulllen = sizeof(nullstring);
  212. }
  213. }
  214. #endif
  215. parts = kzalloc(sizeof(*parts) * nrparts + nulllen + namelen, GFP_KERNEL);
  216. if (!parts) {
  217. ret = -ENOMEM;
  218. goto out;
  219. }
  220. nullname = (char *)&parts[nrparts];
  221. #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
  222. if (nulllen > 0)
  223. strcpy(nullname, nullstring);
  224. #endif
  225. names = nullname + nulllen;
  226. i = 0;
  227. #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
  228. if (fl->img->flash_base) {
  229. parts[0].name = nullname;
  230. parts[0].size = fl->img->flash_base;
  231. parts[0].offset = 0;
  232. i++;
  233. }
  234. #endif
  235. for ( ; i < nrparts; i++) {
  236. parts[i].size = fl->img->size;
  237. parts[i].offset = fl->img->flash_base;
  238. parts[i].name = names;
  239. strcpy(names, fl->img->name);
  240. #ifdef CONFIG_MTD_REDBOOT_PARTS_READONLY
  241. if (!memcmp(names, "RedBoot", 8) ||
  242. !memcmp(names, "RedBoot config", 15) ||
  243. !memcmp(names, "FIS directory", 14)) {
  244. parts[i].mask_flags = MTD_WRITEABLE;
  245. }
  246. #endif
  247. names += strlen(names) + 1;
  248. #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
  249. if (fl->next && fl->img->flash_base + fl->img->size + master->erasesize <= fl->next->img->flash_base) {
  250. i++;
  251. parts[i].offset = parts[i - 1].size + parts[i - 1].offset;
  252. parts[i].size = fl->next->img->flash_base - parts[i].offset;
  253. parts[i].name = nullname;
  254. }
  255. #endif
  256. tmp_fl = fl;
  257. fl = fl->next;
  258. kfree(tmp_fl);
  259. }
  260. ret = nrparts;
  261. *pparts = parts;
  262. out:
  263. while (fl) {
  264. struct fis_list *old = fl;
  265. fl = fl->next;
  266. kfree(old);
  267. }
  268. vfree(buf);
  269. return ret;
  270. }
  271. static const struct of_device_id mtd_parser_redboot_of_match_table[] = {
  272. { .compatible = "redboot-fis" },
  273. {},
  274. };
  275. MODULE_DEVICE_TABLE(of, mtd_parser_redboot_of_match_table);
  276. static struct mtd_part_parser redboot_parser = {
  277. .parse_fn = parse_redboot_partitions,
  278. .name = "RedBoot",
  279. .of_match_table = mtd_parser_redboot_of_match_table,
  280. };
  281. module_mtd_part_parser(redboot_parser);
  282. /* mtd parsers will request the module by parser name */
  283. MODULE_ALIAS("RedBoot");
  284. MODULE_LICENSE("GPL");
  285. MODULE_AUTHOR("David Woodhouse <[email protected]>");
  286. MODULE_DESCRIPTION("Parsing code for RedBoot Flash Image System (FIS) tables");