armflash.c 7.0 KB

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