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