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