cmd: fpga: Remove fit image support passed without fpga device
[platform/kernel/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 #if defined(CONFIG_CMD_FPGA_LOADFS)
78         case FPGA_LOADFS:
79                 if (argc < 9)
80                         return CMD_RET_USAGE;
81                 fpga_fsinfo.blocksize = (unsigned int)
82                                         simple_strtoul(argv[5], NULL, 16);
83                 fpga_fsinfo.interface = argv[6];
84                 fpga_fsinfo.dev_part = argv[7];
85                 fpga_fsinfo.filename = argv[8];
86                 argc = 5;
87                 break;
88 #endif
89 #if defined(CONFIG_CMD_FPGA_LOAD_SECURE)
90         case FPGA_LOADS:
91                 if (argc < 7)
92                         return CMD_RET_USAGE;
93                 if (argc == 8)
94                         fpga_sec_info.userkey_addr = (u8 *)(uintptr_t)
95                                                      simple_strtoull(argv[7],
96                                                                      NULL, 16);
97                 fpga_sec_info.encflag = (u8)simple_strtoul(argv[6], NULL, 16);
98                 fpga_sec_info.authflag = (u8)simple_strtoul(argv[5], NULL, 16);
99                 argc = 5;
100                 break;
101 #endif
102         default:
103                 break;
104         }
105
106         switch (argc) {
107         case 5:         /* fpga <op> <dev> <data> <datasize> */
108                 data_size = simple_strtoul(argv[4], NULL, 16);
109
110         case 4:         /* fpga <op> <dev> <data> */
111 #if defined(CONFIG_FIT)
112                 if (fit_parse_subimage(argv[3], (ulong)fpga_data,
113                                        &fit_addr, &fit_uname)) {
114                         fpga_data = (void *)fit_addr;
115                         debug("*  fpga: subimage '%s' from FIT image ",
116                               fit_uname);
117                         debug("at 0x%08lx\n", fit_addr);
118                 } else
119 #endif
120                 {
121                         fpga_data = (void *)simple_strtoul(argv[3], NULL, 16);
122                         debug("*  fpga: cmdline image address = 0x%08lx\n",
123                               (ulong)fpga_data);
124                 }
125                 debug("%s: fpga_data = 0x%lx\n", __func__, (ulong)fpga_data);
126
127         case 3:         /* fpga <op> <dev | data addr> */
128                 dev = (int)simple_strtoul(argv[2], NULL, 16);
129                 debug("%s: device = %d\n", __func__, dev);
130         }
131
132         if (dev == FPGA_INVALID_DEVICE) {
133                 puts("FPGA device not specified\n");
134                 op = FPGA_NONE;
135         }
136
137         switch (op) {
138         case FPGA_NONE:
139         case FPGA_INFO:
140                 break;
141 #if defined(CONFIG_CMD_FPGA_LOADFS)
142         case FPGA_LOADFS:
143                 /* Blocksize can be zero */
144                 if (!fpga_fsinfo.interface || !fpga_fsinfo.dev_part ||
145                     !fpga_fsinfo.filename)
146                         wrong_parms = 1;
147                 break;
148 #endif
149 #if defined(CONFIG_CMD_FPGA_LOAD_SECURE)
150         case FPGA_LOADS:
151                 if (fpga_sec_info.authflag >= FPGA_NO_ENC_OR_NO_AUTH &&
152                     fpga_sec_info.encflag >= FPGA_NO_ENC_OR_NO_AUTH) {
153                         puts("ERR: use <fpga load> for NonSecure bitstream\n");
154                         wrong_parms = 1;
155                 }
156
157                 if (fpga_sec_info.encflag == FPGA_ENC_USR_KEY &&
158                     !fpga_sec_info.userkey_addr) {
159                         wrong_parms = 1;
160                         puts("ERR:User key not provided\n");
161                 }
162                 break;
163 #endif
164         case FPGA_LOAD:
165         case FPGA_LOADP:
166         case FPGA_LOADB:
167         case FPGA_LOADBP:
168         case FPGA_DUMP:
169                 if (!fpga_data || !data_size)
170                         wrong_parms = 1;
171                 break;
172 #if defined(CONFIG_CMD_FPGA_LOADMK)
173         case FPGA_LOADMK:
174                 if (!fpga_data)
175                         wrong_parms = 1;
176                 break;
177 #endif
178         }
179
180         if (wrong_parms) {
181                 puts("Wrong parameters for FPGA request\n");
182                 op = FPGA_NONE;
183         }
184
185         switch (op) {
186         case FPGA_NONE:
187                 return CMD_RET_USAGE;
188
189         case FPGA_INFO:
190                 rc = fpga_info(dev);
191                 break;
192
193         case FPGA_LOAD:
194                 rc = fpga_load(dev, fpga_data, data_size, BIT_FULL);
195                 break;
196
197 #if defined(CONFIG_CMD_FPGA_LOADP)
198         case FPGA_LOADP:
199                 rc = fpga_load(dev, fpga_data, data_size, BIT_PARTIAL);
200                 break;
201 #endif
202
203         case FPGA_LOADB:
204                 rc = fpga_loadbitstream(dev, fpga_data, data_size, BIT_FULL);
205                 break;
206
207 #if defined(CONFIG_CMD_FPGA_LOADBP)
208         case FPGA_LOADBP:
209                 rc = fpga_loadbitstream(dev, fpga_data, data_size, BIT_PARTIAL);
210                 break;
211 #endif
212
213 #if defined(CONFIG_CMD_FPGA_LOADFS)
214         case FPGA_LOADFS:
215                 rc = fpga_fsload(dev, fpga_data, data_size, &fpga_fsinfo);
216                 break;
217 #endif
218
219 #if defined(CONFIG_CMD_FPGA_LOAD_SECURE)
220         case FPGA_LOADS:
221                 rc = fpga_loads(dev, fpga_data, data_size, &fpga_sec_info);
222                 break;
223 #endif
224
225 #if defined(CONFIG_CMD_FPGA_LOADMK)
226         case FPGA_LOADMK:
227                 switch (genimg_get_format(fpga_data)) {
228 #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
229                 case IMAGE_FORMAT_LEGACY:
230                         {
231                                 image_header_t *hdr =
232                                                 (image_header_t *)fpga_data;
233                                 ulong data;
234                                 uint8_t comp;
235
236                                 comp = image_get_comp(hdr);
237                                 if (comp == IH_COMP_GZIP) {
238 #if defined(CONFIG_GZIP)
239                                         ulong image_buf = image_get_data(hdr);
240                                         data = image_get_load(hdr);
241                                         ulong image_size = ~0UL;
242
243                                         if (gunzip((void *)data, ~0UL,
244                                                    (void *)image_buf,
245                                                    &image_size) != 0) {
246                                                 puts("GUNZIP: error\n");
247                                                 return 1;
248                                         }
249                                         data_size = image_size;
250 #else
251                                         puts("Gunzip image is not supported\n");
252                                         return 1;
253 #endif
254                                 } else {
255                                         data = (ulong)image_get_data(hdr);
256                                         data_size = image_get_data_size(hdr);
257                                 }
258                                 rc = fpga_load(dev, (void *)data, data_size,
259                                                BIT_FULL);
260                         }
261                         break;
262 #endif
263 #if defined(CONFIG_FIT)
264                 case IMAGE_FORMAT_FIT:
265                         {
266                                 const void *fit_hdr = (const void *)fpga_data;
267                                 int noffset;
268                                 const void *fit_data;
269
270                                 if (fit_uname == NULL) {
271                                         puts("No FIT subimage unit name\n");
272                                         return 1;
273                                 }
274
275                                 if (!fit_check_format(fit_hdr)) {
276                                         puts("Bad FIT image format\n");
277                                         return 1;
278                                 }
279
280                                 /* get fpga component image node offset */
281                                 noffset = fit_image_get_node(fit_hdr,
282                                                              fit_uname);
283                                 if (noffset < 0) {
284                                         printf("Can't find '%s' FIT subimage\n",
285                                                fit_uname);
286                                         return 1;
287                                 }
288
289                                 /* verify integrity */
290                                 if (!fit_image_verify(fit_hdr, noffset)) {
291                                         puts ("Bad Data Hash\n");
292                                         return 1;
293                                 }
294
295                                 /* get fpga subimage data address and length */
296                                 if (fit_image_get_data(fit_hdr, noffset,
297                                                        &fit_data, &data_size)) {
298                                         puts("Fpga subimage data not found\n");
299                                         return 1;
300                                 }
301
302                                 rc = fpga_load(dev, fit_data, data_size,
303                                                BIT_FULL);
304                         }
305                         break;
306 #endif
307                 default:
308                         puts("** Unknown image type\n");
309                         rc = FPGA_FAIL;
310                         break;
311                 }
312                 break;
313 #endif
314
315         case FPGA_DUMP:
316                 rc = fpga_dump(dev, fpga_data, data_size);
317                 break;
318
319         default:
320                 printf("Unknown operation\n");
321                 return CMD_RET_USAGE;
322         }
323         return rc;
324 }
325
326 /*
327  * Map op to supported operations.  We don't use a table since we
328  * would just have to relocate it from flash anyway.
329  */
330 static int fpga_get_op(char *opstr)
331 {
332         int op = FPGA_NONE;
333
334         if (!strcmp("info", opstr))
335                 op = FPGA_INFO;
336         else if (!strcmp("loadb", opstr))
337                 op = FPGA_LOADB;
338         else if (!strcmp("load", opstr))
339                 op = FPGA_LOAD;
340 #if defined(CONFIG_CMD_FPGA_LOADP)
341         else if (!strcmp("loadp", opstr))
342                 op = FPGA_LOADP;
343 #endif
344 #if defined(CONFIG_CMD_FPGA_LOADBP)
345         else if (!strcmp("loadbp", opstr))
346                 op = FPGA_LOADBP;
347 #endif
348 #if defined(CONFIG_CMD_FPGA_LOADFS)
349         else if (!strcmp("loadfs", opstr))
350                 op = FPGA_LOADFS;
351 #endif
352 #if defined(CONFIG_CMD_FPGA_LOADMK)
353         else if (!strcmp("loadmk", opstr))
354                 op = FPGA_LOADMK;
355 #endif
356         else if (!strcmp("dump", opstr))
357                 op = FPGA_DUMP;
358 #if defined(CONFIG_CMD_FPGA_LOAD_SECURE)
359         else if (!strcmp("loads", opstr))
360                 op = FPGA_LOADS;
361 #endif
362
363         if (op == FPGA_NONE)
364                 printf("Unknown fpga operation \"%s\"\n", opstr);
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 );