afs.c 6.3 KB

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