armflash.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2015
  4. * Linus Walleij, Linaro
  5. *
  6. * Support for ARM Flash Partitions
  7. */
  8. #include <common.h>
  9. #include <command.h>
  10. #include <console.h>
  11. #include <flash.h>
  12. #include <asm/io.h>
  13. #define MAX_REGIONS 4
  14. #define MAX_IMAGES 32
  15. struct afs_region {
  16. u32 load_address;
  17. u32 size;
  18. u32 offset;
  19. };
  20. struct afs_image {
  21. flash_info_t *flinfo;
  22. const char *name;
  23. u32 version;
  24. u32 entrypoint;
  25. u32 attributes;
  26. u32 region_count;
  27. struct afs_region regions[MAX_REGIONS];
  28. ulong flash_mem_start;
  29. ulong flash_mem_end;
  30. };
  31. static struct afs_image afs_images[MAX_IMAGES];
  32. static int num_afs_images;
  33. static u32 compute_crc(ulong start, u32 len)
  34. {
  35. u32 sum = 0;
  36. int i;
  37. if (len % 4 != 0) {
  38. printf("bad checksumming\n");
  39. return 0;
  40. }
  41. for (i = 0; i < len; i += 4) {
  42. u32 val;
  43. val = readl((void *)start + i);
  44. if (val > ~sum)
  45. sum++;
  46. sum += val;
  47. }
  48. return ~sum;
  49. }
  50. static void parse_bank(ulong bank)
  51. {
  52. int i;
  53. ulong flstart, flend;
  54. flash_info_t *info;
  55. info = &flash_info[bank];
  56. if (info->flash_id != FLASH_MAN_CFI) {
  57. printf("Bank %lu: missing or unknown FLASH type\n", bank);
  58. return;
  59. }
  60. if (!info->sector_count) {
  61. printf("Bank %lu: no FLASH sectors\n", bank);
  62. return;
  63. }
  64. flstart = info->start[0];
  65. flend = flstart + info->size;
  66. for (i = 0; i < info->sector_count; ++i) {
  67. ulong secend;
  68. u32 foot1, foot2;
  69. if (ctrlc())
  70. break;
  71. if (i == info->sector_count-1)
  72. secend = flend;
  73. else
  74. secend = info->start[i+1];
  75. /* Check for v1 header */
  76. foot1 = readl((void *)secend - 0x0c);
  77. if (foot1 == 0xA0FFFF9FU) {
  78. struct afs_image *afi = &afs_images[num_afs_images];
  79. ulong imginfo;
  80. afi->flinfo = info;
  81. afi->version = 1;
  82. afi->flash_mem_start = readl((void *)secend - 0x10);
  83. afi->flash_mem_end = readl((void *)secend - 0x14);
  84. afi->attributes = readl((void *)secend - 0x08);
  85. /* Adjust to even address */
  86. imginfo = afi->flash_mem_end + afi->flash_mem_end % 4;
  87. /* Record as a single region */
  88. afi->region_count = 1;
  89. afi->regions[0].offset = readl((void *)imginfo + 0x04);
  90. afi->regions[0].load_address =
  91. readl((void *)imginfo + 0x08);
  92. afi->regions[0].size = readl((void *)imginfo + 0x0C);
  93. afi->entrypoint = readl((void *)imginfo + 0x10);
  94. afi->name = (const char *)imginfo + 0x14;
  95. num_afs_images++;
  96. }
  97. /* Check for v2 header */
  98. foot1 = readl((void *)secend - 0x04);
  99. foot2 = readl((void *)secend - 0x08);
  100. /* This makes up the string "HSLFTOOF" flash footer */
  101. if (foot1 == 0x464F4F54U && foot2 == 0x464C5348U) {
  102. struct afs_image *afi = &afs_images[num_afs_images];
  103. ulong imginfo;
  104. u32 block_start, block_end;
  105. int j;
  106. afi->flinfo = info;
  107. afi->version = readl((void *)secend - 0x0c);
  108. imginfo = secend - 0x30 - readl((void *)secend - 0x10);
  109. afi->name = (const char *)secend - 0x30;
  110. afi->entrypoint = readl((void *)imginfo+0x08);
  111. afi->attributes = readl((void *)imginfo+0x0c);
  112. afi->region_count = readl((void *)imginfo+0x10);
  113. block_start = readl((void *)imginfo+0x54);
  114. block_end = readl((void *)imginfo+0x58);
  115. afi->flash_mem_start = afi->flinfo->start[block_start];
  116. afi->flash_mem_end = afi->flinfo->start[block_end];
  117. /*
  118. * Check footer CRC, the algorithm saves the inverse
  119. * checksum as part of the summed words, and thus
  120. * the result should be zero.
  121. */
  122. if (compute_crc(imginfo + 8, 0x88) != 0) {
  123. printf("BAD CRC on ARM image info\n");
  124. printf("(continuing anyway)\n");
  125. }
  126. /* Parse regions */
  127. for (j = 0; j < afi->region_count; j++) {
  128. afi->regions[j].load_address =
  129. readl((void *)imginfo+0x14 + j*0x10);
  130. afi->regions[j].size =
  131. readl((void *)imginfo+0x18 + j*0x10);
  132. afi->regions[j].offset =
  133. readl((void *)imginfo+0x1c + j*0x10);
  134. /*
  135. * At offset 0x20 + j*0x10 there is a region
  136. * checksum which seems to be the running
  137. * sum + 3, however since we anyway checksum
  138. * the entire footer this is skipped over for
  139. * checking here.
  140. */
  141. }
  142. num_afs_images++;
  143. }
  144. }
  145. }
  146. static void parse_flash(void)
  147. {
  148. ulong bank;
  149. /* We have already parsed the images in flash */
  150. if (num_afs_images > 0)
  151. return;
  152. for (bank = 0; bank < CONFIG_SYS_MAX_FLASH_BANKS; ++bank)
  153. parse_bank(bank);
  154. }
  155. static int load_image(const char * const name, const ulong address)
  156. {
  157. struct afs_image *afi = NULL;
  158. int i;
  159. parse_flash();
  160. for (i = 0; i < num_afs_images; i++) {
  161. struct afs_image *tmp = &afs_images[i];
  162. if (!strcmp(tmp->name, name)) {
  163. afi = tmp;
  164. break;
  165. }
  166. }
  167. if (!afi) {
  168. printf("image \"%s\" not found in flash\n", name);
  169. return CMD_RET_FAILURE;
  170. }
  171. for (i = 0; i < afi->region_count; i++) {
  172. ulong from, to;
  173. from = afi->flash_mem_start + afi->regions[i].offset;
  174. if (address) {
  175. to = address;
  176. } else if (afi->regions[i].load_address) {
  177. to = afi->regions[i].load_address;
  178. } else {
  179. printf("no valid load address\n");
  180. return CMD_RET_FAILURE;
  181. }
  182. memcpy((void *)to, (void *)from, afi->regions[i].size);
  183. printf("loaded region %d from %08lX to %08lX, %08X bytes\n",
  184. i,
  185. from,
  186. to,
  187. afi->regions[i].size);
  188. }
  189. return CMD_RET_SUCCESS;
  190. }
  191. static void print_images(void)
  192. {
  193. int i;
  194. parse_flash();
  195. for (i = 0; i < num_afs_images; i++) {
  196. struct afs_image *afi = &afs_images[i];
  197. int j;
  198. printf("Image: \"%s\" (v%d):\n", afi->name, afi->version);
  199. printf(" Entry point: 0x%08X\n", afi->entrypoint);
  200. printf(" Attributes: 0x%08X: ", afi->attributes);
  201. if (afi->attributes == 0x01)
  202. printf("ARM executable");
  203. if (afi->attributes == 0x08)
  204. printf("ARM backup");
  205. printf("\n");
  206. printf(" Flash mem start: 0x%08lX\n",
  207. afi->flash_mem_start);
  208. printf(" Flash mem end: 0x%08lX\n",
  209. afi->flash_mem_end);
  210. for (j = 0; j < afi->region_count; j++) {
  211. printf(" region %d\n"
  212. " load address: %08X\n"
  213. " size: %08X\n"
  214. " offset: %08X\n",
  215. j,
  216. afi->regions[j].load_address,
  217. afi->regions[j].size,
  218. afi->regions[j].offset);
  219. }
  220. }
  221. }
  222. static int exists(const char * const name)
  223. {
  224. int i;
  225. parse_flash();
  226. for (i = 0; i < num_afs_images; i++) {
  227. struct afs_image *afi = &afs_images[i];
  228. if (strcmp(afi->name, name) == 0)
  229. return CMD_RET_SUCCESS;
  230. }
  231. return CMD_RET_FAILURE;
  232. }
  233. static int do_afs(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
  234. {
  235. int ret = CMD_RET_SUCCESS;
  236. if (argc == 1) {
  237. print_images();
  238. } else if (argc == 3 && !strcmp(argv[1], "exists")) {
  239. ret = exists(argv[2]);
  240. } else if (argc == 3 && !strcmp(argv[1], "load")) {
  241. ret = load_image(argv[2], 0x0);
  242. } else if (argc == 4 && !strcmp(argv[1], "load")) {
  243. ulong load_addr;
  244. load_addr = hextoul(argv[3], NULL);
  245. ret = load_image(argv[2], load_addr);
  246. } else {
  247. return CMD_RET_USAGE;
  248. }
  249. return ret;
  250. }
  251. U_BOOT_CMD(afs, 4, 0, do_afs, "show AFS partitions",
  252. "no arguments\n"
  253. " - list images in flash\n"
  254. "exists <image>\n"
  255. " - returns 1 if an image exists, else 0\n"
  256. "load <image>\n"
  257. " - load an image to the location indicated in the header\n"
  258. "load <image> 0x<address>\n"
  259. " - load an image to the location specified\n");