fs/fat: split out helper to init fsdata
[platform/kernel/u-boot.git] / cmd / armflash.c
1 /*
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 <console.h>
12 #include <asm/io.h>
13
14 #define MAX_REGIONS 4
15 #define MAX_IMAGES 32
16
17 struct afs_region {
18         u32 load_address;
19         u32 size;
20         u32 offset;
21 };
22
23 struct 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
35 static struct afs_image afs_images[MAX_IMAGES];
36 static int num_afs_images;
37
38 static 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
59 static 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
168 static 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
179 static int load_image(const char * const name, const ulong address)
180 {
181         struct afs_image *afi = NULL;
182         int i;
183
184         parse_flash();
185         for (i = 0; i < num_afs_images; i++) {
186                 struct afs_image *tmp = &afs_images[i];
187
188                 if (!strcmp(tmp->name, name)) {
189                         afi = tmp;
190                         break;
191                 }
192         }
193         if (!afi) {
194                 printf("image \"%s\" not found in flash\n", name);
195                 return CMD_RET_FAILURE;
196         }
197
198         for (i = 0; i < afi->region_count; i++) {
199                 ulong from, to;
200
201                 from = afi->flash_mem_start + afi->regions[i].offset;
202                 if (address) {
203                         to = address;
204                 } else if (afi->regions[i].load_address) {
205                         to = afi->regions[i].load_address;
206                 } else {
207                         printf("no valid load address\n");
208                         return CMD_RET_FAILURE;
209                 }
210
211                 memcpy((void *)to, (void *)from, afi->regions[i].size);
212
213                 printf("loaded region %d from %08lX to %08lX, %08X bytes\n",
214                        i,
215                        from,
216                        to,
217                        afi->regions[i].size);
218         }
219         return CMD_RET_SUCCESS;
220 }
221
222 static void print_images(void)
223 {
224         int i;
225
226         parse_flash();
227         for (i = 0; i < num_afs_images; i++) {
228                 struct afs_image *afi = &afs_images[i];
229                 int j;
230
231                 printf("Image: \"%s\" (v%d):\n", afi->name, afi->version);
232                 printf("    Entry point: 0x%08X\n", afi->entrypoint);
233                 printf("    Attributes: 0x%08X: ", afi->attributes);
234                 if (afi->attributes == 0x01)
235                         printf("ARM executable");
236                 if (afi->attributes == 0x08)
237                         printf("ARM backup");
238                 printf("\n");
239                 printf("    Flash mem start: 0x%08lX\n",
240                        afi->flash_mem_start);
241                 printf("    Flash mem end: 0x%08lX\n",
242                        afi->flash_mem_end);
243                 for (j = 0; j < afi->region_count; j++) {
244                         printf("    region %d\n"
245                                "        load address: %08X\n"
246                                "        size: %08X\n"
247                                "        offset: %08X\n",
248                                j,
249                                afi->regions[j].load_address,
250                                afi->regions[j].size,
251                                afi->regions[j].offset);
252                 }
253         }
254 }
255
256 static int exists(const char * const name)
257 {
258         int i;
259
260         parse_flash();
261         for (i = 0; i < num_afs_images; i++) {
262                 struct afs_image *afi = &afs_images[i];
263
264                 if (strcmp(afi->name, name) == 0)
265                         return CMD_RET_SUCCESS;
266         }
267         return CMD_RET_FAILURE;
268 }
269
270 static int do_afs(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
271 {
272         int ret = CMD_RET_SUCCESS;
273
274         if (argc == 1) {
275                 print_images();
276         } else if (argc == 3 && !strcmp(argv[1], "exists")) {
277                 ret = exists(argv[2]);
278         } else if (argc == 3 && !strcmp(argv[1], "load")) {
279                 ret = load_image(argv[2], 0x0);
280         } else if (argc == 4 && !strcmp(argv[1], "load")) {
281                 ulong load_addr;
282
283                 load_addr = simple_strtoul(argv[3], NULL, 16);
284                 ret = load_image(argv[2], load_addr);
285         } else {
286                 return CMD_RET_USAGE;
287         }
288
289         return ret;
290 }
291
292 U_BOOT_CMD(afs, 4, 0, do_afs, "show AFS partitions",
293            "no arguments\n"
294            "    - list images in flash\n"
295            "exists <image>\n"
296            "    - returns 1 if an image exists, else 0\n"
297            "load <image>\n"
298            "    - load an image to the location indicated in the header\n"
299            "load <image> 0x<address>\n"
300            "    - load an image to the location specified\n");