redboot.c 7.8 KB

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