afs.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*======================================================================
  2. drivers/mtd/afs.c: ARM Flash Layout/Partitioning
  3. Copyright (C) 2000 ARM Limited
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. This is access code for flashes using ARM's flash partitioning
  16. standards.
  17. $Id: afs.c,v 1.15 2005/11/07 11:14:19 gleixner Exp $
  18. ======================================================================*/
  19. #include <linux/module.h>
  20. #include <linux/types.h>
  21. #include <linux/kernel.h>
  22. #include <linux/slab.h>
  23. #include <linux/string.h>
  24. #include <linux/init.h>
  25. #include <linux/mtd/mtd.h>
  26. #include <linux/mtd/map.h>
  27. #include <linux/mtd/partitions.h>
  28. struct footer_struct {
  29. u32 image_info_base; /* Address of first word of ImageFooter */
  30. u32 image_start; /* Start of area reserved by this footer */
  31. u32 signature; /* 'Magic' number proves it's a footer */
  32. u32 type; /* Area type: ARM Image, SIB, customer */
  33. u32 checksum; /* Just this structure */
  34. };
  35. struct image_info_struct {
  36. u32 bootFlags; /* Boot flags, compression etc. */
  37. u32 imageNumber; /* Unique number, selects for boot etc. */
  38. u32 loadAddress; /* Address program should be loaded to */
  39. u32 length; /* Actual size of image */
  40. u32 address; /* Image is executed from here */
  41. char name[16]; /* Null terminated */
  42. u32 headerBase; /* Flash Address of any stripped header */
  43. u32 header_length; /* Length of header in memory */
  44. u32 headerType; /* AIF, RLF, s-record etc. */
  45. u32 checksum; /* Image checksum (inc. this struct) */
  46. };
  47. static u32 word_sum(void *words, int num)
  48. {
  49. u32 *p = words;
  50. u32 sum = 0;
  51. while (num--)
  52. sum += *p++;
  53. return sum;
  54. }
  55. static int
  56. afs_read_footer(struct mtd_info *mtd, u_int *img_start, u_int *iis_start,
  57. u_int off, u_int mask)
  58. {
  59. struct footer_struct fs;
  60. u_int ptr = off + mtd->erasesize - sizeof(fs);
  61. size_t sz;
  62. int ret;
  63. ret = mtd->read(mtd, ptr, sizeof(fs), &sz, (u_char *) &fs);
  64. if (ret >= 0 && sz != sizeof(fs))
  65. ret = -EINVAL;
  66. if (ret < 0) {
  67. printk(KERN_ERR "AFS: mtd read failed at 0x%x: %d\n",
  68. ptr, ret);
  69. return ret;
  70. }
  71. ret = 1;
  72. /*
  73. * Does it contain the magic number?
  74. */
  75. if (fs.signature != 0xa0ffff9f)
  76. ret = 0;
  77. /*
  78. * Check the checksum.
  79. */
  80. if (word_sum(&fs, sizeof(fs) / sizeof(u32)) != 0xffffffff)
  81. ret = 0;
  82. /*
  83. * Don't touch the SIB.
  84. */
  85. if (fs.type == 2)
  86. ret = 0;
  87. *iis_start = fs.image_info_base & mask;
  88. *img_start = fs.image_start & mask;
  89. /*
  90. * Check the image info base. This can not
  91. * be located after the footer structure.
  92. */
  93. if (*iis_start >= ptr)
  94. ret = 0;
  95. /*
  96. * Check the start of this image. The image
  97. * data can not be located after this block.
  98. */
  99. if (*img_start > off)
  100. ret = 0;
  101. return ret;
  102. }
  103. static int
  104. afs_read_iis(struct mtd_info *mtd, struct image_info_struct *iis, u_int ptr)
  105. {
  106. size_t sz;
  107. int ret, i;
  108. memset(iis, 0, sizeof(*iis));
  109. ret = mtd->read(mtd, ptr, sizeof(*iis), &sz, (u_char *) iis);
  110. if (ret < 0)
  111. goto failed;
  112. if (sz != sizeof(*iis)) {
  113. ret = -EINVAL;
  114. goto failed;
  115. }
  116. ret = 0;
  117. /*
  118. * Validate the name - it must be NUL terminated.
  119. */
  120. for (i = 0; i < sizeof(iis->name); i++)
  121. if (iis->name[i] == '\0')
  122. break;
  123. if (i < sizeof(iis->name))
  124. ret = 1;
  125. return ret;
  126. failed:
  127. printk(KERN_ERR "AFS: mtd read failed at 0x%x: %d\n",
  128. ptr, ret);
  129. return ret;
  130. }
  131. static int parse_afs_partitions(struct mtd_info *mtd,
  132. struct mtd_partition **pparts,
  133. unsigned long origin)
  134. {
  135. struct mtd_partition *parts;
  136. u_int mask, off, idx, sz;
  137. int ret = 0;
  138. char *str;
  139. /*
  140. * This is the address mask; we use this to mask off out of
  141. * range address bits.
  142. */
  143. mask = mtd->size - 1;
  144. /*
  145. * First, calculate the size of the array we need for the
  146. * partition information. We include in this the size of
  147. * the strings.
  148. */
  149. for (idx = off = sz = 0; off < mtd->size; off += mtd->erasesize) {
  150. struct image_info_struct iis;
  151. u_int iis_ptr, img_ptr;
  152. ret = afs_read_footer(mtd, &img_ptr, &iis_ptr, off, mask);
  153. if (ret < 0)
  154. break;
  155. if (ret == 0)
  156. continue;
  157. ret = afs_read_iis(mtd, &iis, iis_ptr);
  158. if (ret < 0)
  159. break;
  160. if (ret == 0)
  161. continue;
  162. sz += sizeof(struct mtd_partition);
  163. sz += strlen(iis.name) + 1;
  164. idx += 1;
  165. }
  166. if (!sz)
  167. return ret;
  168. parts = kmalloc(sz, GFP_KERNEL);
  169. if (!parts)
  170. return -ENOMEM;
  171. memset(parts, 0, sz);
  172. str = (char *)(parts + idx);
  173. /*
  174. * Identify the partitions
  175. */
  176. for (idx = off = 0; off < mtd->size; off += mtd->erasesize) {
  177. struct image_info_struct iis;
  178. u_int iis_ptr, img_ptr;
  179. /* Read the footer. */
  180. ret = afs_read_footer(mtd, &img_ptr, &iis_ptr, off, mask);
  181. if (ret < 0)
  182. break;
  183. if (ret == 0)
  184. continue;
  185. /* Read the image info block */
  186. ret = afs_read_iis(mtd, &iis, iis_ptr);
  187. if (ret < 0)
  188. break;
  189. if (ret == 0)
  190. continue;
  191. strcpy(str, iis.name);
  192. parts[idx].name = str;
  193. parts[idx].size = (iis.length + mtd->erasesize - 1) & ~(mtd->erasesize - 1);
  194. parts[idx].offset = img_ptr;
  195. parts[idx].mask_flags = 0;
  196. printk(" mtd%d: at 0x%08x, %5dKB, %8u, %s\n",
  197. idx, img_ptr, parts[idx].size / 1024,
  198. iis.imageNumber, str);
  199. idx += 1;
  200. str = str + strlen(iis.name) + 1;
  201. }
  202. if (!idx) {
  203. kfree(parts);
  204. parts = NULL;
  205. }
  206. *pparts = parts;
  207. return idx ? idx : ret;
  208. }
  209. static struct mtd_part_parser afs_parser = {
  210. .owner = THIS_MODULE,
  211. .parse_fn = parse_afs_partitions,
  212. .name = "afs",
  213. };
  214. static int __init afs_parser_init(void)
  215. {
  216. return register_mtd_parser(&afs_parser);
  217. }
  218. static void __exit afs_parser_exit(void)
  219. {
  220. deregister_mtd_parser(&afs_parser);
  221. }
  222. module_init(afs_parser_init);
  223. module_exit(afs_parser_exit);
  224. MODULE_AUTHOR("ARM Ltd");
  225. MODULE_DESCRIPTION("ARM Firmware Suite partition parser");
  226. MODULE_LICENSE("GPL");