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