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