1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2000, 2001
4 * Rich Ireland, Enterasys Networks, rireland@enterasys.com.
17 static int fpga_get_op(char *opstr);
33 /* ------------------------------------------------------------------------- */
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.
41 int do_fpga(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
43 int op, dev = FPGA_INVALID_DEVICE;
45 void *fpga_data = NULL;
46 char *devstr = env_get("fpga");
47 char *datastr = env_get("fpgadata");
50 #if defined(CONFIG_FIT)
51 const char *fit_uname = NULL;
54 #if defined(CONFIG_CMD_FPGA_LOADFS)
55 fpga_fs_info fpga_fsinfo;
56 fpga_fsinfo.fstype = FS_TYPE_ANY;
58 #if defined(CONFIG_CMD_FPGA_LOAD_SECURE)
59 struct fpga_secure_info fpga_sec_info;
61 memset(&fpga_sec_info, 0, sizeof(fpga_sec_info));
65 dev = (int) simple_strtoul(devstr, NULL, 16);
67 fpga_data = (void *)simple_strtoul(datastr, NULL, 16);
69 if (argc > 9 || argc < 2) {
70 debug("%s: Too many or too few args (%d)\n", __func__, argc);
74 op = (int)fpga_get_op(argv[1]);
77 #if defined(CONFIG_CMD_FPGA_LOADFS)
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];
89 #if defined(CONFIG_CMD_FPGA_LOAD_SECURE)
94 fpga_sec_info.userkey_addr = (u8 *)(uintptr_t)
95 simple_strtoull(argv[7],
97 fpga_sec_info.encflag = (u8)simple_strtoul(argv[6], NULL, 16);
98 fpga_sec_info.authflag = (u8)simple_strtoul(argv[5], NULL, 16);
107 case 5: /* fpga <op> <dev> <data> <datasize> */
108 data_size = simple_strtoul(argv[4], NULL, 16);
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 ",
117 debug("at 0x%08lx\n", fit_addr);
121 fpga_data = (void *)simple_strtoul(argv[3], NULL, 16);
122 debug("* fpga: cmdline image address = 0x%08lx\n",
125 debug("%s: fpga_data = 0x%lx\n", __func__, (ulong)fpga_data);
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 /* FIXME - this is a really weak test */
131 if ((argc == 3) && (dev > fpga_count())) {
132 /* must be buffer ptr */
133 debug("%s: Assuming buffer pointer in arg 3\n",
136 #if defined(CONFIG_FIT)
137 if (fit_parse_subimage(argv[2], (ulong)fpga_data,
138 &fit_addr, &fit_uname)) {
139 fpga_data = (void *)fit_addr;
140 debug("* fpga: subimage '%s' from FIT image ",
142 debug("at 0x%08lx\n", fit_addr);
146 fpga_data = (void *)(uintptr_t)dev;
147 debug("* fpga: cmdline image addr = 0x%08lx\n",
151 debug("%s: fpga_data = 0x%lx\n",
152 __func__, (ulong)fpga_data);
153 dev = FPGA_INVALID_DEVICE; /* reset device num */
157 if (dev == FPGA_INVALID_DEVICE) {
158 puts("FPGA device not specified\n");
166 #if defined(CONFIG_CMD_FPGA_LOADFS)
168 /* Blocksize can be zero */
169 if (!fpga_fsinfo.interface || !fpga_fsinfo.dev_part ||
170 !fpga_fsinfo.filename)
174 #if defined(CONFIG_CMD_FPGA_LOAD_SECURE)
176 if (fpga_sec_info.authflag >= FPGA_NO_ENC_OR_NO_AUTH &&
177 fpga_sec_info.encflag >= FPGA_NO_ENC_OR_NO_AUTH) {
178 puts("ERR: use <fpga load> for NonSecure bitstream\n");
182 if (fpga_sec_info.encflag == FPGA_ENC_USR_KEY &&
183 !fpga_sec_info.userkey_addr) {
185 puts("ERR:User key not provided\n");
194 if (!fpga_data || !data_size)
197 #if defined(CONFIG_CMD_FPGA_LOADMK)
206 puts("Wrong parameters for FPGA request\n");
212 return CMD_RET_USAGE;
219 rc = fpga_load(dev, fpga_data, data_size, BIT_FULL);
222 #if defined(CONFIG_CMD_FPGA_LOADP)
224 rc = fpga_load(dev, fpga_data, data_size, BIT_PARTIAL);
229 rc = fpga_loadbitstream(dev, fpga_data, data_size, BIT_FULL);
232 #if defined(CONFIG_CMD_FPGA_LOADBP)
234 rc = fpga_loadbitstream(dev, fpga_data, data_size, BIT_PARTIAL);
238 #if defined(CONFIG_CMD_FPGA_LOADFS)
240 rc = fpga_fsload(dev, fpga_data, data_size, &fpga_fsinfo);
244 #if defined(CONFIG_CMD_FPGA_LOAD_SECURE)
246 rc = fpga_loads(dev, fpga_data, data_size, &fpga_sec_info);
250 #if defined(CONFIG_CMD_FPGA_LOADMK)
252 switch (genimg_get_format(fpga_data)) {
253 #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
254 case IMAGE_FORMAT_LEGACY:
256 image_header_t *hdr =
257 (image_header_t *)fpga_data;
261 comp = image_get_comp(hdr);
262 if (comp == IH_COMP_GZIP) {
263 #if defined(CONFIG_GZIP)
264 ulong image_buf = image_get_data(hdr);
265 data = image_get_load(hdr);
266 ulong image_size = ~0UL;
268 if (gunzip((void *)data, ~0UL,
271 puts("GUNZIP: error\n");
274 data_size = image_size;
276 puts("Gunzip image is not supported\n");
280 data = (ulong)image_get_data(hdr);
281 data_size = image_get_data_size(hdr);
283 rc = fpga_load(dev, (void *)data, data_size,
288 #if defined(CONFIG_FIT)
289 case IMAGE_FORMAT_FIT:
291 const void *fit_hdr = (const void *)fpga_data;
293 const void *fit_data;
295 if (fit_uname == NULL) {
296 puts("No FIT subimage unit name\n");
300 if (!fit_check_format(fit_hdr)) {
301 puts("Bad FIT image format\n");
305 /* get fpga component image node offset */
306 noffset = fit_image_get_node(fit_hdr,
309 printf("Can't find '%s' FIT subimage\n",
314 /* verify integrity */
315 if (!fit_image_verify(fit_hdr, noffset)) {
316 puts ("Bad Data Hash\n");
320 /* get fpga subimage data address and length */
321 if (fit_image_get_data(fit_hdr, noffset,
322 &fit_data, &data_size)) {
323 puts("Fpga subimage data not found\n");
327 rc = fpga_load(dev, fit_data, data_size,
333 puts("** Unknown image type\n");
341 rc = fpga_dump(dev, fpga_data, data_size);
345 printf("Unknown operation\n");
346 return CMD_RET_USAGE;
352 * Map op to supported operations. We don't use a table since we
353 * would just have to relocate it from flash anyway.
355 static int fpga_get_op(char *opstr)
359 if (!strcmp("info", opstr))
361 else if (!strcmp("loadb", opstr))
363 else if (!strcmp("load", opstr))
365 #if defined(CONFIG_CMD_FPGA_LOADP)
366 else if (!strcmp("loadp", opstr))
369 #if defined(CONFIG_CMD_FPGA_LOADBP)
370 else if (!strcmp("loadbp", opstr))
373 #if defined(CONFIG_CMD_FPGA_LOADFS)
374 else if (!strcmp("loadfs", opstr))
377 #if defined(CONFIG_CMD_FPGA_LOADMK)
378 else if (!strcmp("loadmk", opstr))
381 else if (!strcmp("dump", opstr))
383 #if defined(CONFIG_CMD_FPGA_LOAD_SECURE)
384 else if (!strcmp("loads", opstr))
389 printf("Unknown fpga operation \"%s\"\n", opstr);
394 #if defined(CONFIG_CMD_FPGA_LOADFS) || defined(CONFIG_CMD_FPGA_LOAD_SECURE)
395 U_BOOT_CMD(fpga, 9, 1, do_fpga,
397 U_BOOT_CMD(fpga, 6, 1, do_fpga,
399 "loadable FPGA image support",
400 "[operation type] [device number] [image address] [image size]\n"
402 " dump\t[dev] [address] [size]\tLoad device to memory buffer\n"
403 " info\t[dev]\t\t\tlist known device information\n"
404 " load\t[dev] [address] [size]\tLoad device from memory buffer\n"
405 #if defined(CONFIG_CMD_FPGA_LOADP)
406 " loadp\t[dev] [address] [size]\t"
407 "Load device from memory buffer with partial bitstream\n"
409 " loadb\t[dev] [address] [size]\t"
410 "Load device from bitstream buffer (Xilinx only)\n"
411 #if defined(CONFIG_CMD_FPGA_LOADBP)
412 " loadbp\t[dev] [address] [size]\t"
413 "Load device from bitstream buffer with partial bitstream"
416 #if defined(CONFIG_CMD_FPGA_LOADFS)
417 "Load device from filesystem (FAT by default) (Xilinx only)\n"
418 " loadfs [dev] [address] [image size] [blocksize] <interface>\n"
419 " [<dev[:part]>] <filename>\n"
421 #if defined(CONFIG_CMD_FPGA_LOADMK)
422 " loadmk [dev] [address]\tLoad device generated with mkimage"
423 #if defined(CONFIG_FIT)
425 "\tFor loadmk operating on FIT format uImage address must include\n"
426 "\tsubimage unit name in the form of addr:<subimg_uname>"
429 #if defined(CONFIG_CMD_FPGA_LOAD_SECURE)
430 "Load encrypted bitstream (Xilinx only)\n"
431 " loads [dev] [address] [size] [auth-OCM-0/DDR-1/noauth-2]\n"
432 " [enc-devkey(0)/userkey(1)/nenc(2) [Userkey address]\n"
433 "Loads the secure bistreams(authenticated/encrypted/both\n"
434 "authenticated and encrypted) of [size] from [address].\n"
435 "The auth-OCM/DDR flag specifies to perform authentication\n"
436 "in OCM or in DDR. 0 for OCM, 1 for DDR, 2 for no authentication.\n"
437 "The enc flag specifies which key to be used for decryption\n"
438 "0-device key, 1-user key, 2-no encryption.\n"
439 "The optional Userkey address specifies from which address key\n"
440 "has to be used for decryption if user key is selected.\n"
441 "NOTE: the sceure bitstream has to be created using xilinx\n"
442 "bootgen tool only.\n"