afs.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*======================================================================
  3. drivers/mtd/afs.c: ARM Flash Layout/Partitioning
  4. Copyright © 2000 ARM Limited
  5. Copyright (C) 2019 Linus Walleij
  6. This is access code for flashes using ARM's flash partitioning
  7. standards.
  8. ======================================================================*/
  9. #include <linux/module.h>
  10. #include <linux/types.h>
  11. #include <linux/kernel.h>
  12. #include <linux/slab.h>
  13. #include <linux/string.h>
  14. #include <linux/init.h>
  15. #include <linux/mtd/mtd.h>
  16. #include <linux/mtd/map.h>
  17. #include <linux/mtd/partitions.h>
  18. #define AFSV1_FOOTER_MAGIC 0xA0FFFF9F
  19. #define AFSV2_FOOTER_MAGIC1 0x464C5348 /* "FLSH" */
  20. #define AFSV2_FOOTER_MAGIC2 0x464F4F54 /* "FOOT" */
  21. struct footer_v1 {
  22. u32 image_info_base; /* Address of first word of ImageFooter */
  23. u32 image_start; /* Start of area reserved by this footer */
  24. u32 signature; /* 'Magic' number proves it's a footer */
  25. u32 type; /* Area type: ARM Image, SIB, customer */
  26. u32 checksum; /* Just this structure */
  27. };
  28. struct image_info_v1 {
  29. u32 bootFlags; /* Boot flags, compression etc. */
  30. u32 imageNumber; /* Unique number, selects for boot etc. */
  31. u32 loadAddress; /* Address program should be loaded to */
  32. u32 length; /* Actual size of image */
  33. u32 address; /* Image is executed from here */
  34. char name[16]; /* Null terminated */
  35. u32 headerBase; /* Flash Address of any stripped header */
  36. u32 header_length; /* Length of header in memory */
  37. u32 headerType; /* AIF, RLF, s-record etc. */
  38. u32 checksum; /* Image checksum (inc. this struct) */
  39. };
  40. static u32 word_sum(void *words, int num)
  41. {
  42. u32 *p = words;
  43. u32 sum = 0;
  44. while (num--)
  45. sum += *p++;
  46. return sum;
  47. }
  48. static u32 word_sum_v2(u32 *p, u32 num)
  49. {
  50. u32 sum = 0;
  51. int i;
  52. for (i = 0; i < num; i++) {
  53. u32 val;
  54. val = p[i];
  55. if (val > ~sum)
  56. sum++;
  57. sum += val;
  58. }
  59. return ~sum;
  60. }
  61. static bool afs_is_v1(struct mtd_info *mtd, u_int off)
  62. {
  63. /* The magic is 12 bytes from the end of the erase block */
  64. u_int ptr = off + mtd->erasesize - 12;
  65. u32 magic;
  66. size_t sz;
  67. int ret;
  68. ret = mtd_read(mtd, ptr, 4, &sz, (u_char *)&magic);
  69. if (ret < 0) {
  70. printk(KERN_ERR "AFS: mtd read failed at 0x%x: %d\n",
  71. ptr, ret);
  72. return false;
  73. }
  74. if (ret >= 0 && sz != 4)
  75. return false;
  76. return (magic == AFSV1_FOOTER_MAGIC);
  77. }
  78. static bool afs_is_v2(struct mtd_info *mtd, u_int off)
  79. {
  80. /* The magic is the 8 last bytes of the erase block */
  81. u_int ptr = off + mtd->erasesize - 8;
  82. u32 foot[2];
  83. size_t sz;
  84. int ret;
  85. ret = mtd_read(mtd, ptr, 8, &sz, (u_char *)foot);
  86. if (ret < 0) {
  87. printk(KERN_ERR "AFS: mtd read failed at 0x%x: %d\n",
  88. ptr, ret);
  89. return false;
  90. }
  91. if (ret >= 0 && sz != 8)
  92. return false;
  93. return (foot[0] == AFSV2_FOOTER_MAGIC1 &&
  94. foot[1] == AFSV2_FOOTER_MAGIC2);
  95. }
  96. static int afs_parse_v1_partition(struct mtd_info *mtd,
  97. u_int off, struct mtd_partition *part)
  98. {
  99. struct footer_v1 fs;
  100. struct image_info_v1 iis;
  101. u_int mask;
  102. /*
  103. * Static checks cannot see that we bail out if we have an error
  104. * reading the footer.
  105. */
  106. u_int iis_ptr;
  107. u_int img_ptr;
  108. u_int ptr;
  109. size_t sz;
  110. int ret;
  111. int i;
  112. /*
  113. * This is the address mask; we use this to mask off out of
  114. * range address bits.
  115. */
  116. mask = mtd->size - 1;
  117. ptr = off + mtd->erasesize - sizeof(fs);
  118. ret = mtd_read(mtd, ptr, sizeof(fs), &sz, (u_char *)&fs);
  119. if (ret >= 0 && sz != sizeof(fs))
  120. ret = -EINVAL;
  121. if (ret < 0) {
  122. printk(KERN_ERR "AFS: mtd read failed at 0x%x: %d\n",
  123. ptr, ret);
  124. return ret;
  125. }
  126. /*
  127. * Check the checksum.
  128. */
  129. if (word_sum(&fs, sizeof(fs) / sizeof(u32)) != 0xffffffff)
  130. return -EINVAL;
  131. /*
  132. * Hide the SIB (System Information Block)
  133. */
  134. if (fs.type == 2)
  135. return 0;
  136. iis_ptr = fs.image_info_base & mask;
  137. img_ptr = fs.image_start & mask;
  138. /*
  139. * Check the image info base. This can not
  140. * be located after the footer structure.
  141. */
  142. if (iis_ptr >= ptr)
  143. return 0;
  144. /*
  145. * Check the start of this image. The image
  146. * data can not be located after this block.
  147. */
  148. if (img_ptr > off)
  149. return 0;
  150. /* Read the image info block */
  151. memset(&iis, 0, sizeof(iis));
  152. ret = mtd_read(mtd, iis_ptr, sizeof(iis), &sz, (u_char *)&iis);
  153. if (ret < 0) {
  154. printk(KERN_ERR "AFS: mtd read failed at 0x%x: %d\n",
  155. iis_ptr, ret);
  156. return -EINVAL;
  157. }
  158. if (sz != sizeof(iis))
  159. return -EINVAL;
  160. /*
  161. * Validate the name - it must be NUL terminated.
  162. */
  163. for (i = 0; i < sizeof(iis.name); i++)
  164. if (iis.name[i] == '\0')
  165. break;
  166. if (i > sizeof(iis.name))
  167. return -EINVAL;
  168. part->name = kstrdup(iis.name, GFP_KERNEL);
  169. if (!part->name)
  170. return -ENOMEM;
  171. part->size = (iis.length + mtd->erasesize - 1) & ~(mtd->erasesize - 1);
  172. part->offset = img_ptr;
  173. part->mask_flags = 0;
  174. printk(" mtd: at 0x%08x, %5lluKiB, %8u, %s\n",
  175. img_ptr, part->size / 1024,
  176. iis.imageNumber, part->name);
  177. return 0;
  178. }
  179. static int afs_parse_v2_partition(struct mtd_info *mtd,
  180. u_int off, struct mtd_partition *part)
  181. {
  182. u_int ptr;
  183. u32 footer[12];
  184. u32 imginfo[36];
  185. char *name;
  186. u32 version;
  187. u32 entrypoint;
  188. u32 attributes;
  189. u32 region_count;
  190. u32 block_start;
  191. u32 block_end;
  192. u32 crc;
  193. size_t sz;
  194. int ret;
  195. int i;
  196. int pad = 0;
  197. pr_debug("Parsing v2 partition @%08x-%08x\n",
  198. off, off + mtd->erasesize);
  199. /* First read the footer */
  200. ptr = off + mtd->erasesize - sizeof(footer);
  201. ret = mtd_read(mtd, ptr, sizeof(footer), &sz, (u_char *)footer);
  202. if ((ret < 0) || (ret >= 0 && sz != sizeof(footer))) {
  203. pr_err("AFS: mtd read failed at 0x%x: %d\n",
  204. ptr, ret);
  205. return -EIO;
  206. }
  207. name = (char *) &footer[0];
  208. version = footer[9];
  209. ptr = off + mtd->erasesize - sizeof(footer) - footer[8];
  210. pr_debug("found image \"%s\", version %08x, info @%08x\n",
  211. name, version, ptr);
  212. /* Then read the image information */
  213. ret = mtd_read(mtd, ptr, sizeof(imginfo), &sz, (u_char *)imginfo);
  214. if ((ret < 0) || (ret >= 0 && sz != sizeof(imginfo))) {
  215. pr_err("AFS: mtd read failed at 0x%x: %d\n",
  216. ptr, ret);
  217. return -EIO;
  218. }
  219. /* 32bit platforms have 4 bytes padding */
  220. crc = word_sum_v2(&imginfo[1], 34);
  221. if (!crc) {
  222. pr_debug("Padding 1 word (4 bytes)\n");
  223. pad = 1;
  224. } else {
  225. /* 64bit platforms have 8 bytes padding */
  226. crc = word_sum_v2(&imginfo[2], 34);
  227. if (!crc) {
  228. pr_debug("Padding 2 words (8 bytes)\n");
  229. pad = 2;
  230. }
  231. }
  232. if (crc) {
  233. pr_err("AFS: bad checksum on v2 image info: %08x\n", crc);
  234. return -EINVAL;
  235. }
  236. entrypoint = imginfo[pad];
  237. attributes = imginfo[pad+1];
  238. region_count = imginfo[pad+2];
  239. block_start = imginfo[20];
  240. block_end = imginfo[21];
  241. pr_debug("image entry=%08x, attr=%08x, regions=%08x, "
  242. "bs=%08x, be=%08x\n",
  243. entrypoint, attributes, region_count,
  244. block_start, block_end);
  245. for (i = 0; i < region_count; i++) {
  246. u32 region_load_addr = imginfo[pad + 3 + i*4];
  247. u32 region_size = imginfo[pad + 4 + i*4];
  248. u32 region_offset = imginfo[pad + 5 + i*4];
  249. u32 region_start;
  250. u32 region_end;
  251. pr_debug(" region %d: address: %08x, size: %08x, "
  252. "offset: %08x\n",
  253. i,
  254. region_load_addr,
  255. region_size,
  256. region_offset);
  257. region_start = off + region_offset;
  258. region_end = region_start + region_size;
  259. /* Align partition to end of erase block */
  260. region_end += (mtd->erasesize - 1);
  261. region_end &= ~(mtd->erasesize -1);
  262. pr_debug(" partition start = %08x, partition end = %08x\n",
  263. region_start, region_end);
  264. /* Create one partition per region */
  265. part->name = kstrdup(name, GFP_KERNEL);
  266. if (!part->name)
  267. return -ENOMEM;
  268. part->offset = region_start;
  269. part->size = region_end - region_start;
  270. part->mask_flags = 0;
  271. }
  272. return 0;
  273. }
  274. static int parse_afs_partitions(struct mtd_info *mtd,
  275. const struct mtd_partition **pparts,
  276. struct mtd_part_parser_data *data)
  277. {
  278. struct mtd_partition *parts;
  279. u_int off, sz;
  280. int ret = 0;
  281. int i;
  282. /* Count the partitions by looping over all erase blocks */
  283. for (i = off = sz = 0; off < mtd->size; off += mtd->erasesize) {
  284. if (afs_is_v1(mtd, off)) {
  285. sz += sizeof(struct mtd_partition);
  286. i += 1;
  287. }
  288. if (afs_is_v2(mtd, off)) {
  289. sz += sizeof(struct mtd_partition);
  290. i += 1;
  291. }
  292. }
  293. if (!i)
  294. return 0;
  295. parts = kzalloc(sz, GFP_KERNEL);
  296. if (!parts)
  297. return -ENOMEM;
  298. /*
  299. * Identify the partitions
  300. */
  301. for (i = off = 0; off < mtd->size; off += mtd->erasesize) {
  302. if (afs_is_v1(mtd, off)) {
  303. ret = afs_parse_v1_partition(mtd, off, &parts[i]);
  304. if (ret)
  305. goto out_free_parts;
  306. i++;
  307. }
  308. if (afs_is_v2(mtd, off)) {
  309. ret = afs_parse_v2_partition(mtd, off, &parts[i]);
  310. if (ret)
  311. goto out_free_parts;
  312. i++;
  313. }
  314. }
  315. *pparts = parts;
  316. return i;
  317. out_free_parts:
  318. while (--i >= 0)
  319. kfree(parts[i].name);
  320. kfree(parts);
  321. *pparts = NULL;
  322. return ret;
  323. }
  324. static const struct of_device_id mtd_parser_afs_of_match_table[] = {
  325. { .compatible = "arm,arm-firmware-suite" },
  326. {},
  327. };
  328. MODULE_DEVICE_TABLE(of, mtd_parser_afs_of_match_table);
  329. static struct mtd_part_parser afs_parser = {
  330. .parse_fn = parse_afs_partitions,
  331. .name = "afs",
  332. .of_match_table = mtd_parser_afs_of_match_table,
  333. };
  334. module_mtd_part_parser(afs_parser);
  335. MODULE_AUTHOR("ARM Ltd");
  336. MODULE_DESCRIPTION("ARM Firmware Suite partition parser");
  337. MODULE_LICENSE("GPL");