cmd: fpga: Move error handling to do_fpga()
[oweals/u-boot.git] / cmd / fpga.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2000, 2001
4  * Rich Ireland, Enterasys Networks, rireland@enterasys.com.
5  */
6
7 /*
8  *  FPGA support
9  */
10 #include <common.h>
11 #include <command.h>
12 #include <fpga.h>
13 #include <fs.h>
14 #include <malloc.h>
15
16 /* Local functions */
17 static int fpga_get_op(char *opstr);
18
19 /* Local defines */
20 enum {
21         FPGA_NONE = -1,
22         FPGA_INFO,
23         FPGA_LOAD,
24         FPGA_LOADB,
25         FPGA_DUMP,
26         FPGA_LOADMK,
27         FPGA_LOADP,
28         FPGA_LOADBP,
29         FPGA_LOADFS,
30         FPGA_LOADS,
31 };
32
33 /* ------------------------------------------------------------------------- */
34 /* command form:
35  *   fpga <op> <device number> <data addr> <datasize>
36  * where op is 'load', 'dump', or 'info'
37  * If there is no device number field, the fpga environment variable is used.
38  * If there is no data addr field, the fpgadata environment variable is used.
39  * The info command requires no data address field.
40  */
41 int do_fpga(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
42 {
43         int op, dev = FPGA_INVALID_DEVICE;
44         size_t data_size = 0;
45         void *fpga_data = NULL;
46         char *devstr = env_get("fpga");
47         char *datastr = env_get("fpgadata");
48         int rc = FPGA_FAIL;
49         int wrong_parms = 0;
50 #if defined(CONFIG_FIT)
51         const char *fit_uname = NULL;
52         ulong fit_addr;
53 #endif
54 #if defined(CONFIG_CMD_FPGA_LOADFS)
55         fpga_fs_info fpga_fsinfo;
56         fpga_fsinfo.fstype = FS_TYPE_ANY;
57 #endif
58 #if defined(CONFIG_CMD_FPGA_LOAD_SECURE)
59         struct fpga_secure_info fpga_sec_info;
60
61         memset(&fpga_sec_info, 0, sizeof(fpga_sec_info));
62 #endif
63
64         if (devstr)
65                 dev = (int) simple_strtoul(devstr, NULL, 16);
66         if (datastr)
67                 fpga_data = (void *)simple_strtoul(datastr, NULL, 16);
68
69         if (argc > 9 || argc < 2) {
70                 debug("%s: Too many or too few args (%d)\n", __func__, argc);
71                 return CMD_RET_USAGE;
72         }
73
74         op = (int)fpga_get_op(argv[1]);
75
76         switch (op) {
77         case FPGA_NONE:
78                 printf("Unknown fpga operation \"%s\"\n", argv[1]);
79                 return CMD_RET_USAGE;
80 #if defined(CONFIG_CMD_FPGA_LOADFS)
81         case FPGA_LOADFS:
82                 if (argc < 9)
83                         return CMD_RET_USAGE;
84                 fpga_fsinfo.blocksize = (unsigned int)
85                                         simple_strtoul(argv[5], NULL, 16);
86                 fpga_fsinfo.interface = argv[6];
87                 fpga_fsinfo.dev_part = argv[7];
88                 fpga_fsinfo.filename = argv[8];
89                 argc = 5;
90                 break;
91 #endif
92 #if defined(CONFIG_CMD_FPGA_LOAD_SECURE)
93         case FPGA_LOADS:
94                 if (argc < 7)
95                         return CMD_RET_USAGE;
96                 if (argc == 8)
97                         fpga_sec_info.userkey_addr = (u8 *)(uintptr_t)
98                                                      simple_strtoull(argv[7],
99                                                                      NULL, 16);
100                 fpga_sec_info.encflag = (u8)simple_strtoul(argv[6], NULL, 16);
101                 fpga_sec_info.authflag = (u8)simple_strtoul(argv[5], NULL, 16);
102                 argc = 5;
103                 break;
104 #endif
105         default:
106                 break;
107         }
108
109         switch (argc) {
110         case 5:         /* fpga <op> <dev> <data> <datasize> */
111                 data_size = simple_strtoul(argv[4], NULL, 16);
112
113         case 4:         /* fpga <op> <dev> <data> */
114 #if defined(CONFIG_FIT)
115                 if (fit_parse_subimage(argv[3], (ulong)fpga_data,
116                                        &fit_addr, &fit_uname)) {
117                         fpga_data = (void *)fit_addr;
118                         debug("*  fpga: subimage '%s' from FIT image ",
119                               fit_uname);
120                         debug("at 0x%08lx\n", fit_addr);
121                 } else
122 #endif
123                 {
124                         fpga_data = (void *)simple_strtoul(argv[3], NULL, 16);
125                         debug("*  fpga: cmdline image address = 0x%08lx\n",
126                               (ulong)fpga_data);
127                 }
128                 debug("%s: fpga_data = 0x%lx\n", __func__, (ulong)fpga_data);
129
130         case 3:         /* fpga <op> <dev | data addr> */
131                 dev = (int)simple_strtoul(argv[2], NULL, 16);
132                 debug("%s: device = %d\n", __func__, dev);
133         }
134
135         if (dev == FPGA_INVALID_DEVICE) {
136                 puts("FPGA device not specified\n");
137                 op = FPGA_NONE;
138         }
139
140         switch (op) {
141         case FPGA_NONE:
142         case FPGA_INFO:
143                 break;
144 #if defined(CONFIG_CMD_FPGA_LOADFS)
145         case FPGA_LOADFS:
146                 /* Blocksize can be zero */
147                 if (!fpga_fsinfo.interface || !fpga_fsinfo.dev_part ||
148                     !fpga_fsinfo.filename)
149                         wrong_parms = 1;
150                 break;
151 #endif
152 #if defined(CONFIG_CMD_FPGA_LOAD_SECURE)
153         case FPGA_LOADS:
154                 if (fpga_sec_info.authflag >= FPGA_NO_ENC_OR_NO_AUTH &&
155                     fpga_sec_info.encflag >= FPGA_NO_ENC_OR_NO_AUTH) {
156                         puts("ERR: use <fpga load> for NonSecure bitstream\n");
157                         wrong_parms = 1;
158                 }
159
160                 if (fpga_sec_info.encflag == FPGA_ENC_USR_KEY &&
161                     !fpga_sec_info.userkey_addr) {
162                         wrong_parms = 1;
163                         puts("ERR:User key not provided\n");
164                 }
165                 break;
166 #endif
167         case FPGA_LOAD:
168         case FPGA_LOADP:
169         case FPGA_LOADB:
170         case FPGA_LOADBP:
171         case FPGA_DUMP:
172                 if (!fpga_data || !data_size)
173                         wrong_parms = 1;
174                 break;
175 #if defined(CONFIG_CMD_FPGA_LOADMK)
176         case FPGA_LOADMK:
177                 if (!fpga_data)
178                         wrong_parms = 1;
179                 break;
180 #endif
181         }
182
183         if (wrong_parms) {
184                 puts("Wrong parameters for FPGA request\n");
185                 op = FPGA_NONE;
186         }
187
188         switch (op) {
189         case FPGA_NONE:
190                 return CMD_RET_USAGE;
191
192         case FPGA_INFO:
193                 rc = fpga_info(dev);
194                 break;
195
196         case FPGA_LOAD:
197                 rc = fpga_load(dev, fpga_data, data_size, BIT_FULL);
198                 break;
199
200 #if defined(CONFIG_CMD_FPGA_LOADP)
201         case FPGA_LOADP:
202                 rc = fpga_load(dev, fpga_data, data_size, BIT_PARTIAL);
203                 break;
204 #endif
205
206         case FPGA_LOADB:
207                 rc = fpga_loadbitstream(dev, fpga_data, data_size, BIT_FULL);
208                 break;
209
210 #if defined(CONFIG_CMD_FPGA_LOADBP)
211         case FPGA_LOADBP:
212                 rc = fpga_loadbitstream(dev, fpga_data, data_size, BIT_PARTIAL);
213                 break;
214 #endif
215
216 #if defined(CONFIG_CMD_FPGA_LOADFS)
217         case FPGA_LOADFS:
218                 rc = fpga_fsload(dev, fpga_data, data_size, &fpga_fsinfo);
219                 break;
220 #endif
221
222 #if defined(CONFIG_CMD_FPGA_LOAD_SECURE)
223         case FPGA_LOADS:
224                 rc = fpga_loads(dev, fpga_data, data_size, &fpga_sec_info);
225                 break;
226 #endif
227
228 #if defined(CONFIG_CMD_FPGA_LOADMK)
229         case FPGA_LOADMK:
230                 switch (genimg_get_format(fpga_data)) {
231 #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
232                 case IMAGE_FORMAT_LEGACY:
233                         {
234                                 image_header_t *hdr =
235                                                 (image_header_t *)fpga_data;
236                                 ulong data;
237                                 uint8_t comp;
238
239                                 comp = image_get_comp(hdr);
240                                 if (comp == IH_COMP_GZIP) {
241 #if defined(CONFIG_GZIP)
242                                         ulong image_buf = image_get_data(hdr);
243                                         data = image_get_load(hdr);
244                                         ulong image_size = ~0UL;
245
246                                         if (gunzip((void *)data, ~0UL,
247                                                    (void *)image_buf,
248                                                    &image_size) != 0) {
249                                                 puts("GUNZIP: error\n");
250                                                 return 1;
251                                         }
252                                         data_size = image_size;
253 #else
254                                         puts("Gunzip image is not supported\n");
255                                         return 1;
256 #endif
257                                 } else {
258                                         data = (ulong)image_get_data(hdr);
259                                         data_size = image_get_data_size(hdr);
260                                 }
261                                 rc = fpga_load(dev, (void *)data, data_size,
262                                                BIT_FULL);
263                         }
264                         break;
265 #endif
266 #if defined(CONFIG_FIT)
267                 case IMAGE_FORMAT_FIT:
268                         {
269                                 const void *fit_hdr = (const void *)fpga_data;
270                                 int noffset;
271                                 const void *fit_data;
272
273                                 if (fit_uname == NULL) {
274                                         puts("No FIT subimage unit name\n");
275                                         return 1;
276                                 }
277
278                                 if (!fit_check_format(fit_hdr)) {
279                                         puts("Bad FIT image format\n");
280                                         return 1;
281                                 }
282
283                                 /* get fpga component image node offset */
284                                 noffset = fit_image_get_node(fit_hdr,
285                                                              fit_uname);
286                                 if (noffset < 0) {
287                                         printf("Can't find '%s' FIT subimage\n",
288                                                fit_uname);
289                                         return 1;
290                                 }
291
292                                 /* verify integrity */
293                                 if (!fit_image_verify(fit_hdr, noffset)) {
294                                         puts ("Bad Data Hash\n");
295                                         return 1;
296                                 }
297
298                                 /* get fpga subimage data address and length */
299                                 if (fit_image_get_data(fit_hdr, noffset,
300                                                        &fit_data, &data_size)) {
301                                         puts("Fpga subimage data not found\n");
302                                         return 1;
303                                 }
304
305                                 rc = fpga_load(dev, fit_data, data_size,
306                                                BIT_FULL);
307                         }
308                         break;
309 #endif
310                 default:
311                         puts("** Unknown image type\n");
312                         rc = FPGA_FAIL;
313                         break;
314                 }
315                 break;
316 #endif
317
318         case FPGA_DUMP:
319                 rc = fpga_dump(dev, fpga_data, data_size);
320                 break;
321
322         default:
323                 printf("Unknown operation\n");
324                 return CMD_RET_USAGE;
325         }
326         return rc;
327 }
328
329 /*
330  * Map op to supported operations.  We don't use a table since we
331  * would just have to relocate it from flash anyway.
332  */
333 static int fpga_get_op(char *opstr)
334 {
335         int op = FPGA_NONE;
336
337         if (!strcmp("info", opstr))
338                 op = FPGA_INFO;
339         else if (!strcmp("loadb", opstr))
340                 op = FPGA_LOADB;
341         else if (!strcmp("load", opstr))
342                 op = FPGA_LOAD;
343 #if defined(CONFIG_CMD_FPGA_LOADP)
344         else if (!strcmp("loadp", opstr))
345                 op = FPGA_LOADP;
346 #endif
347 #if defined(CONFIG_CMD_FPGA_LOADBP)
348         else if (!strcmp("loadbp", opstr))
349                 op = FPGA_LOADBP;
350 #endif
351 #if defined(CONFIG_CMD_FPGA_LOADFS)
352         else if (!strcmp("loadfs", opstr))
353                 op = FPGA_LOADFS;
354 #endif
355 #if defined(CONFIG_CMD_FPGA_LOADMK)
356         else if (!strcmp("loadmk", opstr))
357                 op = FPGA_LOADMK;
358 #endif
359         else if (!strcmp("dump", opstr))
360                 op = FPGA_DUMP;
361 #if defined(CONFIG_CMD_FPGA_LOAD_SECURE)
362         else if (!strcmp("loads", opstr))
363                 op = FPGA_LOADS;
364 #endif
365
366         return op;
367 }
368
369 #if defined(CONFIG_CMD_FPGA_LOADFS) || defined(CONFIG_CMD_FPGA_LOAD_SECURE)
370 U_BOOT_CMD(fpga, 9, 1, do_fpga,
371 #else
372 U_BOOT_CMD(fpga, 6, 1, do_fpga,
373 #endif
374            "loadable FPGA image support",
375            "[operation type] [device number] [image address] [image size]\n"
376            "fpga operations:\n"
377            "  dump\t[dev] [address] [size]\tLoad device to memory buffer\n"
378            "  info\t[dev]\t\t\tlist known device information\n"
379            "  load\t[dev] [address] [size]\tLoad device from memory buffer\n"
380 #if defined(CONFIG_CMD_FPGA_LOADP)
381            "  loadp\t[dev] [address] [size]\t"
382            "Load device from memory buffer with partial bitstream\n"
383 #endif
384            "  loadb\t[dev] [address] [size]\t"
385            "Load device from bitstream buffer (Xilinx only)\n"
386 #if defined(CONFIG_CMD_FPGA_LOADBP)
387            "  loadbp\t[dev] [address] [size]\t"
388            "Load device from bitstream buffer with partial bitstream"
389            "(Xilinx only)\n"
390 #endif
391 #if defined(CONFIG_CMD_FPGA_LOADFS)
392            "Load device from filesystem (FAT by default) (Xilinx only)\n"
393            "  loadfs [dev] [address] [image size] [blocksize] <interface>\n"
394            "        [<dev[:part]>] <filename>\n"
395 #endif
396 #if defined(CONFIG_CMD_FPGA_LOADMK)
397            "  loadmk [dev] [address]\tLoad device generated with mkimage"
398 #if defined(CONFIG_FIT)
399            "\n"
400            "\tFor loadmk operating on FIT format uImage address must include\n"
401            "\tsubimage unit name in the form of addr:<subimg_uname>"
402 #endif
403 #endif
404 #if defined(CONFIG_CMD_FPGA_LOAD_SECURE)
405            "Load encrypted bitstream (Xilinx only)\n"
406            "  loads [dev] [address] [size] [auth-OCM-0/DDR-1/noauth-2]\n"
407            "        [enc-devkey(0)/userkey(1)/nenc(2) [Userkey address]\n"
408            "Loads the secure bistreams(authenticated/encrypted/both\n"
409            "authenticated and encrypted) of [size] from [address].\n"
410            "The auth-OCM/DDR flag specifies to perform authentication\n"
411            "in OCM or in DDR. 0 for OCM, 1 for DDR, 2 for no authentication.\n"
412            "The enc flag specifies which key to be used for decryption\n"
413            "0-device key, 1-user key, 2-no encryption.\n"
414            "The optional Userkey address specifies from which address key\n"
415            "has to be used for decryption if user key is selected.\n"
416            "NOTE: the sceure bitstream has to be created using xilinx\n"
417            "bootgen tool only.\n"
418 #endif
419 );