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