xref: /openbmc/u-boot/cmd/armflash.c (revision 8ee59472)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2015
4  * Linus Walleij, Linaro
5  *
6  * Support for ARM Flash Partitions
7  */
8 #include <common.h>
9 #include <command.h>
10 #include <console.h>
11 #include <asm/io.h>
12 
13 #define MAX_REGIONS 4
14 #define MAX_IMAGES 32
15 
16 struct afs_region {
17 	u32 load_address;
18 	u32 size;
19 	u32 offset;
20 };
21 
22 struct 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 
34 static struct afs_image afs_images[MAX_IMAGES];
35 static int num_afs_images;
36 
37 static 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 
58 static 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 
167 static 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 
178 static int 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 CMD_RET_FAILURE;
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 CMD_RET_FAILURE;
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 	return CMD_RET_SUCCESS;
219 }
220 
221 static void print_images(void)
222 {
223 	int i;
224 
225 	parse_flash();
226 	for (i = 0; i < num_afs_images; i++) {
227 		struct afs_image *afi = &afs_images[i];
228 		int j;
229 
230 		printf("Image: \"%s\" (v%d):\n", afi->name, afi->version);
231 		printf("    Entry point: 0x%08X\n", afi->entrypoint);
232 		printf("    Attributes: 0x%08X: ", afi->attributes);
233 		if (afi->attributes == 0x01)
234 			printf("ARM executable");
235 		if (afi->attributes == 0x08)
236 			printf("ARM backup");
237 		printf("\n");
238 		printf("    Flash mem start: 0x%08lX\n",
239 		       afi->flash_mem_start);
240 		printf("    Flash mem end: 0x%08lX\n",
241 		       afi->flash_mem_end);
242 		for (j = 0; j < afi->region_count; j++) {
243 			printf("    region %d\n"
244 			       "        load address: %08X\n"
245 			       "        size: %08X\n"
246 			       "        offset: %08X\n",
247 			       j,
248 			       afi->regions[j].load_address,
249 			       afi->regions[j].size,
250 			       afi->regions[j].offset);
251 		}
252 	}
253 }
254 
255 static int exists(const char * const name)
256 {
257 	int i;
258 
259 	parse_flash();
260 	for (i = 0; i < num_afs_images; i++) {
261 		struct afs_image *afi = &afs_images[i];
262 
263 		if (strcmp(afi->name, name) == 0)
264 			return CMD_RET_SUCCESS;
265 	}
266 	return CMD_RET_FAILURE;
267 }
268 
269 static int do_afs(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
270 {
271 	int ret = CMD_RET_SUCCESS;
272 
273 	if (argc == 1) {
274 		print_images();
275 	} else if (argc == 3 && !strcmp(argv[1], "exists")) {
276 		ret = exists(argv[2]);
277 	} else if (argc == 3 && !strcmp(argv[1], "load")) {
278 		ret = load_image(argv[2], 0x0);
279 	} else if (argc == 4 && !strcmp(argv[1], "load")) {
280 		ulong load_addr;
281 
282 		load_addr = simple_strtoul(argv[3], NULL, 16);
283 		ret = load_image(argv[2], load_addr);
284 	} else {
285 		return CMD_RET_USAGE;
286 	}
287 
288 	return ret;
289 }
290 
291 U_BOOT_CMD(afs, 4, 0, do_afs, "show AFS partitions",
292 	   "no arguments\n"
293 	   "    - list images in flash\n"
294 	   "exists <image>\n"
295 	   "    - returns 1 if an image exists, else 0\n"
296 	   "load <image>\n"
297 	   "    - load an image to the location indicated in the header\n"
298 	   "load <image> 0x<address>\n"
299 	   "    - load an image to the location specified\n");
300