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