blob: e292cf85c45dc17eebcc353541f19e47213e7d71 [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>
Simon Glass8e201882020-05-10 11:39:54 -060010#include <flash.h>
Tom Rinidec7ea02024-05-20 13:35:03 -060011#include <vsprintf.h>
Linus Walleij5df417b2015-04-05 01:48:31 +020012#include <asm/io.h>
13
14#define MAX_REGIONS 4
15#define MAX_IMAGES 32
16
17struct afs_region {
18 u32 load_address;
19 u32 size;
20 u32 offset;
21};
22
23struct 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
35static struct afs_image afs_images[MAX_IMAGES];
36static int num_afs_images;
37
38static 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
59static 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
168static 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 Harkin46143ed2015-10-09 17:18:05 +0100179static int load_image(const char * const name, const ulong address)
Linus Walleij5df417b2015-04-05 01:48:31 +0200180{
181 struct afs_image *afi = NULL;
182 int i;
Robert Catherall1431df42023-11-23 14:16:01 +0000183 loff_t len_read = 0;
Linus Walleij5df417b2015-04-05 01:48:31 +0200184
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 Harkin46143ed2015-10-09 17:18:05 +0100196 return CMD_RET_FAILURE;
Linus Walleij5df417b2015-04-05 01:48:31 +0200197 }
198
199 for (i = 0; i < afi->region_count; i++) {
200 ulong from, to;
Robert Catherall1431df42023-11-23 14:16:01 +0000201 u32 size;
Linus Walleij5df417b2015-04-05 01:48:31 +0200202
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 Harkin46143ed2015-10-09 17:18:05 +0100210 return CMD_RET_FAILURE;
Linus Walleij5df417b2015-04-05 01:48:31 +0200211 }
212
Robert Catherall1431df42023-11-23 14:16:01 +0000213 size = afi->regions[i].size;
214 memcpy((void *)to, (void *)from, size);
Linus Walleij5df417b2015-04-05 01:48:31 +0200215
216 printf("loaded region %d from %08lX to %08lX, %08X bytes\n",
217 i,
218 from,
219 to,
Robert Catherall1431df42023-11-23 14:16:01 +0000220 size);
221
222 len_read += size;
Linus Walleij5df417b2015-04-05 01:48:31 +0200223 }
Robert Catherall1431df42023-11-23 14:16:01 +0000224
225 env_set_hex("filesize", len_read);
226
Ryan Harkin46143ed2015-10-09 17:18:05 +0100227 return CMD_RET_SUCCESS;
Linus Walleij5df417b2015-04-05 01:48:31 +0200228}
229
230static 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 Harkin37f31702015-10-09 17:18:04 +0100264static 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 Glassed38aef2020-05-10 11:40:03 -0600278static int do_afs(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Linus Walleij5df417b2015-04-05 01:48:31 +0200279{
Ryan Harkin37f31702015-10-09 17:18:04 +0100280 int ret = CMD_RET_SUCCESS;
281
Linus Walleij5df417b2015-04-05 01:48:31 +0200282 if (argc == 1) {
283 print_images();
Ryan Harkin37f31702015-10-09 17:18:04 +0100284 } else if (argc == 3 && !strcmp(argv[1], "exists")) {
285 ret = exists(argv[2]);
Linus Walleij5df417b2015-04-05 01:48:31 +0200286 } else if (argc == 3 && !strcmp(argv[1], "load")) {
Ryan Harkin46143ed2015-10-09 17:18:05 +0100287 ret = load_image(argv[2], 0x0);
Linus Walleij5df417b2015-04-05 01:48:31 +0200288 } else if (argc == 4 && !strcmp(argv[1], "load")) {
289 ulong load_addr;
290
Simon Glass3ff49ec2021-07-24 09:03:29 -0600291 load_addr = hextoul(argv[3], NULL);
Ryan Harkin46143ed2015-10-09 17:18:05 +0100292 ret = load_image(argv[2], load_addr);
Linus Walleij5df417b2015-04-05 01:48:31 +0200293 } else {
294 return CMD_RET_USAGE;
295 }
296
Ryan Harkin37f31702015-10-09 17:18:04 +0100297 return ret;
Linus Walleij5df417b2015-04-05 01:48:31 +0200298}
299
300U_BOOT_CMD(afs, 4, 0, do_afs, "show AFS partitions",
301 "no arguments\n"
302 " - list images in flash\n"
Ryan Harkin37f31702015-10-09 17:18:04 +0100303 "exists <image>\n"
304 " - returns 1 if an image exists, else 0\n"
Linus Walleij5df417b2015-04-05 01:48:31 +0200305 "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");