redboot.c 8.5 KB

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