redboot.c 7.7 KB

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