redboot.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. struct fis_image_desc {
  30. unsigned char name[16]; // Null terminated name
  31. uint32_t flash_base; // Address within FLASH of image
  32. uint32_t mem_base; // Address in memory where it executes
  33. uint32_t size; // Length of image
  34. uint32_t entry_point; // Execution entry point
  35. uint32_t data_length; // Length of actual data
  36. unsigned char _pad[256-(16+7*sizeof(uint32_t))];
  37. uint32_t desc_cksum; // Checksum over image descriptor
  38. uint32_t file_cksum; // Checksum over image data
  39. };
  40. struct fis_list {
  41. struct fis_image_desc *img;
  42. struct fis_list *next;
  43. };
  44. static int directory = CONFIG_MTD_REDBOOT_DIRECTORY_BLOCK;
  45. module_param(directory, int, 0);
  46. static inline int redboot_checksum(struct fis_image_desc *img)
  47. {
  48. /* RedBoot doesn't actually write the desc_cksum field yet AFAICT */
  49. return 1;
  50. }
  51. static int parse_redboot_partitions(struct mtd_info *master,
  52. struct mtd_partition **pparts,
  53. unsigned long fis_origin)
  54. {
  55. int nrparts = 0;
  56. struct fis_image_desc *buf;
  57. struct mtd_partition *parts;
  58. struct fis_list *fl = NULL, *tmp_fl;
  59. int ret, i;
  60. size_t retlen;
  61. char *names;
  62. char *nullname;
  63. int namelen = 0;
  64. int nulllen = 0;
  65. int numslots;
  66. unsigned long offset;
  67. #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
  68. static char nullstring[] = "unallocated";
  69. #endif
  70. if ( directory < 0 ) {
  71. offset = master->size + directory * master->erasesize;
  72. while (master->block_isbad &&
  73. master->block_isbad(master, offset)) {
  74. if (!offset) {
  75. nogood:
  76. printk(KERN_NOTICE "Failed to find a non-bad block to check for RedBoot partition table\n");
  77. return -EIO;
  78. }
  79. offset -= master->erasesize;
  80. }
  81. } else {
  82. offset = directory * master->erasesize;
  83. while (master->block_isbad &&
  84. master->block_isbad(master, offset)) {
  85. offset += master->erasesize;
  86. if (offset == master->size)
  87. goto nogood;
  88. }
  89. }
  90. buf = vmalloc(master->erasesize);
  91. if (!buf)
  92. return -ENOMEM;
  93. printk(KERN_NOTICE "Searching for RedBoot partition table in %s at offset 0x%lx\n",
  94. master->name, offset);
  95. ret = master->read(master, offset,
  96. master->erasesize, &retlen, (void *)buf);
  97. if (ret)
  98. goto out;
  99. if (retlen != master->erasesize) {
  100. ret = -EIO;
  101. goto out;
  102. }
  103. numslots = (master->erasesize / sizeof(struct fis_image_desc));
  104. for (i = 0; i < numslots; i++) {
  105. if (!memcmp(buf[i].name, "FIS directory", 14)) {
  106. /* This is apparently the FIS directory entry for the
  107. * FIS directory itself. The FIS directory size is
  108. * one erase block; if the buf[i].size field is
  109. * swab32(erasesize) then we know we are looking at
  110. * a byte swapped FIS directory - swap all the entries!
  111. * (NOTE: this is 'size' not 'data_length'; size is
  112. * the full size of the entry.)
  113. */
  114. /* RedBoot can combine the FIS directory and
  115. config partitions into a single eraseblock;
  116. we assume wrong-endian if either the swapped
  117. 'size' matches the eraseblock size precisely,
  118. or if the swapped size actually fits in an
  119. eraseblock while the unswapped size doesn't. */
  120. if (swab32(buf[i].size) == master->erasesize ||
  121. (buf[i].size > master->erasesize
  122. && swab32(buf[i].size) < master->erasesize)) {
  123. int j;
  124. /* Update numslots based on actual FIS directory size */
  125. numslots = swab32(buf[i].size) / sizeof (struct fis_image_desc);
  126. for (j = 0; j < numslots; ++j) {
  127. /* A single 0xff denotes a deleted entry.
  128. * Two of them in a row is the end of the table.
  129. */
  130. if (buf[j].name[0] == 0xff) {
  131. if (buf[j].name[1] == 0xff) {
  132. break;
  133. } else {
  134. continue;
  135. }
  136. }
  137. /* The unsigned long fields were written with the
  138. * wrong byte sex, name and pad have no byte sex.
  139. */
  140. swab32s(&buf[j].flash_base);
  141. swab32s(&buf[j].mem_base);
  142. swab32s(&buf[j].size);
  143. swab32s(&buf[j].entry_point);
  144. swab32s(&buf[j].data_length);
  145. swab32s(&buf[j].desc_cksum);
  146. swab32s(&buf[j].file_cksum);
  147. }
  148. } else if (buf[i].size < master->erasesize) {
  149. /* Update numslots based on actual FIS directory size */
  150. numslots = buf[i].size / sizeof(struct fis_image_desc);
  151. }
  152. break;
  153. }
  154. }
  155. if (i == numslots) {
  156. /* Didn't find it */
  157. printk(KERN_NOTICE "No RedBoot partition table detected in %s\n",
  158. master->name);
  159. ret = 0;
  160. goto out;
  161. }
  162. for (i = 0; i < numslots; i++) {
  163. struct fis_list *new_fl, **prev;
  164. if (buf[i].name[0] == 0xff) {
  165. if (buf[i].name[1] == 0xff) {
  166. break;
  167. } else {
  168. continue;
  169. }
  170. }
  171. if (!redboot_checksum(&buf[i]))
  172. break;
  173. new_fl = kmalloc(sizeof(struct fis_list), GFP_KERNEL);
  174. namelen += strlen(buf[i].name)+1;
  175. if (!new_fl) {
  176. ret = -ENOMEM;
  177. goto out;
  178. }
  179. new_fl->img = &buf[i];
  180. if (fis_origin) {
  181. buf[i].flash_base -= fis_origin;
  182. } else {
  183. buf[i].flash_base &= master->size-1;
  184. }
  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. static int __init redboot_parser_init(void)
  270. {
  271. return register_mtd_parser(&redboot_parser);
  272. }
  273. static void __exit redboot_parser_exit(void)
  274. {
  275. deregister_mtd_parser(&redboot_parser);
  276. }
  277. module_init(redboot_parser_init);
  278. module_exit(redboot_parser_exit);
  279. MODULE_LICENSE("GPL");
  280. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
  281. MODULE_DESCRIPTION("Parsing code for RedBoot Flash Image System (FIS) tables");