afs.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. #define AFSV1_FOOTER_MAGIC 0xA0FFFF9F
  28. struct footer_v1 {
  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_v1 {
  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_v1(struct mtd_info *mtd, u_int *img_start, u_int *iis_start,
  57. u_int off, u_int mask)
  58. {
  59. struct footer_v1 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. /*
  72. * Does it contain the magic number?
  73. */
  74. if (fs.signature != AFSV1_FOOTER_MAGIC)
  75. return 0;
  76. /*
  77. * Check the checksum.
  78. */
  79. if (word_sum(&fs, sizeof(fs) / sizeof(u32)) != 0xffffffff)
  80. return 0;
  81. /*
  82. * Don't touch the SIB.
  83. */
  84. if (fs.type == 2)
  85. return 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. return 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. return 0;
  100. return 1;
  101. }
  102. static int
  103. afs_read_iis_v1(struct mtd_info *mtd, struct image_info_v1 *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. const 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_v1 iis;
  150. u_int iis_ptr, img_ptr;
  151. ret = afs_read_footer_v1(mtd, &img_ptr, &iis_ptr, off, mask);
  152. if (ret < 0)
  153. break;
  154. if (ret) {
  155. ret = afs_read_iis_v1(mtd, &iis, iis_ptr);
  156. if (ret < 0)
  157. break;
  158. if (ret == 0)
  159. continue;
  160. sz += sizeof(struct mtd_partition);
  161. sz += strlen(iis.name) + 1;
  162. idx += 1;
  163. }
  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_v1 iis;
  176. u_int iis_ptr, img_ptr;
  177. /* Read the footer. */
  178. ret = afs_read_footer_v1(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_v1(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. .parse_fn = parse_afs_partitions,
  209. .name = "afs",
  210. };
  211. module_mtd_part_parser(afs_parser);
  212. MODULE_AUTHOR("ARM Ltd");
  213. MODULE_DESCRIPTION("ARM Firmware Suite partition parser");
  214. MODULE_LICENSE("GPL");