1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2000, 2001
4 * Rich Ireland, Enterasys Networks, rireland@enterasys.com.
16 static long do_fpga_get_device(char *arg)
18 long dev = FPGA_INVALID_DEVICE;
19 char *devstr = env_get("fpga");
22 /* Should be strtol to handle -1 cases */
23 dev = simple_strtol(devstr, NULL, 16);
26 dev = simple_strtol(arg, NULL, 16);
28 debug("%s: device = %ld\n", __func__, dev);
33 static int do_fpga_check_params(long *dev, long *fpga_data, size_t *data_size,
34 cmd_tbl_t *cmdtp, int argc, char *const argv[])
36 size_t local_data_size;
39 debug("%s %d, %d\n", __func__, argc, cmdtp->maxargs);
41 if (argc != cmdtp->maxargs) {
42 debug("fpga: incorrect parameters passed\n");
46 *dev = do_fpga_get_device(argv[0]);
48 local_fpga_data = simple_strtol(argv[1], NULL, 16);
49 if (!local_fpga_data) {
50 debug("fpga: zero fpga_data address\n");
53 *fpga_data = local_fpga_data;
55 local_data_size = simple_strtoul(argv[2], NULL, 16);
56 if (!local_data_size) {
57 debug("fpga: zero size\n");
60 *data_size = local_data_size;
72 * Map op to supported operations. We don't use a table since we
73 * would just have to relocate it from flash anyway.
75 static int fpga_get_op(char *opstr)
79 #if defined(CONFIG_CMD_FPGA_LOAD_SECURE)
80 if (!strcmp("loads", opstr))
87 /* ------------------------------------------------------------------------- */
89 * fpga <op> <device number> <data addr> <datasize>
90 * where op is 'load', 'dump', or 'info'
91 * If there is no device number field, the fpga environment variable is used.
92 * If there is no data addr field, the fpgadata environment variable is used.
93 * The info command requires no data address field.
95 int do_fpga(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
97 int op, dev = FPGA_INVALID_DEVICE;
99 void *fpga_data = NULL;
101 #if defined(CONFIG_CMD_FPGA_LOAD_SECURE)
102 struct fpga_secure_info fpga_sec_info;
104 memset(&fpga_sec_info, 0, sizeof(fpga_sec_info));
107 if (argc > 9 || argc < 2) {
108 debug("%s: Too many or too few args (%d)\n", __func__, argc);
109 return CMD_RET_USAGE;
112 op = fpga_get_op(argv[1]);
116 printf("Unknown fpga operation \"%s\"\n", argv[1]);
117 return CMD_RET_USAGE;
118 #if defined(CONFIG_CMD_FPGA_LOAD_SECURE)
121 return CMD_RET_USAGE;
123 fpga_sec_info.userkey_addr = (u8 *)(uintptr_t)
124 simple_strtoull(argv[7],
126 fpga_sec_info.encflag = (u8)simple_strtoul(argv[6], NULL, 16);
127 fpga_sec_info.authflag = (u8)simple_strtoul(argv[5], NULL, 16);
129 if (fpga_sec_info.authflag >= FPGA_NO_ENC_OR_NO_AUTH &&
130 fpga_sec_info.encflag >= FPGA_NO_ENC_OR_NO_AUTH) {
131 puts("ERR: Use <fpga load> for NonSecure bitstream\n");
132 return CMD_RET_USAGE;
135 if (fpga_sec_info.encflag == FPGA_ENC_USR_KEY &&
136 !fpga_sec_info.userkey_addr) {
137 puts("ERR: User key not provided\n");
138 return CMD_RET_USAGE;
149 case 5: /* fpga <op> <dev> <data> <datasize> */
150 data_size = simple_strtoul(argv[4], NULL, 16);
152 puts("Zero data_size\n");
153 return CMD_RET_USAGE;
155 case 4: /* fpga <op> <dev> <data> */
157 fpga_data = (void *)simple_strtoul(argv[3], NULL, 16);
158 debug("* fpga: cmdline image address = 0x%08lx\n",
161 debug("%s: fpga_data = 0x%lx\n", __func__, (ulong)fpga_data);
163 puts("Zero fpga_data address\n");
164 return CMD_RET_USAGE;
166 case 3: /* fpga <op> <dev | data addr> */
167 dev = (int)simple_strtoul(argv[2], NULL, 16);
168 debug("%s: device = %d\n", __func__, dev);
171 if (dev == FPGA_INVALID_DEVICE) {
172 puts("FPGA device not specified\n");
173 return CMD_RET_USAGE;
177 #if defined(CONFIG_CMD_FPGA_LOAD_SECURE)
179 rc = fpga_loads(dev, fpga_data, data_size, &fpga_sec_info);
184 printf("Unknown operation\n");
185 return CMD_RET_USAGE;
190 #if defined(CONFIG_CMD_FPGA_LOADFS)
191 static int do_fpga_loadfs(cmd_tbl_t *cmdtp, int flag, int argc,
194 size_t data_size = 0;
197 fpga_fs_info fpga_fsinfo;
199 ret = do_fpga_check_params(&dev, &fpga_data, &data_size,
204 fpga_fsinfo.fstype = FS_TYPE_ANY;
205 fpga_fsinfo.blocksize = (unsigned int)simple_strtoul(argv[3], NULL, 16);
206 fpga_fsinfo.interface = argv[4];
207 fpga_fsinfo.dev_part = argv[5];
208 fpga_fsinfo.filename = argv[6];
210 return fpga_fsload(dev, (void *)fpga_data, data_size, &fpga_fsinfo);
214 static int do_fpga_info(cmd_tbl_t *cmdtp, int flag, int argc,
217 long dev = do_fpga_get_device(argv[0]);
219 return fpga_info(dev);
222 static int do_fpga_dump(cmd_tbl_t *cmdtp, int flag, int argc,
225 size_t data_size = 0;
229 ret = do_fpga_check_params(&dev, &fpga_data, &data_size,
234 return fpga_dump(dev, (void *)fpga_data, data_size);
237 static int do_fpga_load(cmd_tbl_t *cmdtp, int flag, int argc,
240 size_t data_size = 0;
244 ret = do_fpga_check_params(&dev, &fpga_data, &data_size,
249 return fpga_load(dev, (void *)fpga_data, data_size, BIT_FULL);
252 static int do_fpga_loadb(cmd_tbl_t *cmdtp, int flag, int argc,
255 size_t data_size = 0;
259 ret = do_fpga_check_params(&dev, &fpga_data, &data_size,
264 return fpga_loadbitstream(dev, (void *)fpga_data, data_size, BIT_FULL);
267 #if defined(CONFIG_CMD_FPGA_LOADP)
268 static int do_fpga_loadp(cmd_tbl_t *cmdtp, int flag, int argc,
271 size_t data_size = 0;
275 ret = do_fpga_check_params(&dev, &fpga_data, &data_size,
280 return fpga_load(dev, (void *)fpga_data, data_size, BIT_PARTIAL);
284 #if defined(CONFIG_CMD_FPGA_LOADBP)
285 static int do_fpga_loadbp(cmd_tbl_t *cmdtp, int flag, int argc,
288 size_t data_size = 0;
292 ret = do_fpga_check_params(&dev, &fpga_data, &data_size,
297 return fpga_loadbitstream(dev, (void *)fpga_data, data_size,
302 #if defined(CONFIG_CMD_FPGA_LOADMK)
303 static int do_fpga_loadmk(cmd_tbl_t *cmdtp, int flag, int argc,
306 size_t data_size = 0;
307 void *fpga_data = NULL;
308 #if defined(CONFIG_FIT)
309 const char *fit_uname = NULL;
312 ulong dev = do_fpga_get_device(argv[0]);
313 char *datastr = env_get("fpgadata");
316 fpga_data = (void *)simple_strtoul(datastr, NULL, 16);
319 #if defined(CONFIG_FIT)
320 if (fit_parse_subimage(argv[1], (ulong)fpga_data,
321 &fit_addr, &fit_uname)) {
322 fpga_data = (void *)fit_addr;
323 debug("* fpga: subimage '%s' from FIT image ",
325 debug("at 0x%08lx\n", fit_addr);
329 fpga_data = (void *)simple_strtoul(argv[1], NULL, 16);
330 debug("* fpga: cmdline image address = 0x%08lx\n",
333 debug("%s: fpga_data = 0x%lx\n", __func__, (ulong)fpga_data);
335 puts("Zero fpga_data address\n");
336 return CMD_RET_USAGE;
340 switch (genimg_get_format(fpga_data)) {
341 #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
342 case IMAGE_FORMAT_LEGACY:
344 image_header_t *hdr = (image_header_t *)fpga_data;
348 comp = image_get_comp(hdr);
349 if (comp == IH_COMP_GZIP) {
350 #if defined(CONFIG_GZIP)
351 ulong image_buf = image_get_data(hdr);
352 ulong image_size = ~0UL;
354 data = image_get_load(hdr);
356 if (gunzip((void *)data, ~0UL, (void *)image_buf,
358 puts("GUNZIP: error\n");
361 data_size = image_size;
363 puts("Gunzip image is not supported\n");
367 data = (ulong)image_get_data(hdr);
368 data_size = image_get_data_size(hdr);
370 return fpga_load(dev, (void *)data, data_size,
374 #if defined(CONFIG_FIT)
375 case IMAGE_FORMAT_FIT:
377 const void *fit_hdr = (const void *)fpga_data;
379 const void *fit_data;
382 puts("No FIT subimage unit name\n");
386 if (!fit_check_format(fit_hdr)) {
387 puts("Bad FIT image format\n");
391 /* get fpga component image node offset */
392 noffset = fit_image_get_node(fit_hdr, fit_uname);
394 printf("Can't find '%s' FIT subimage\n", fit_uname);
398 /* verify integrity */
399 if (!fit_image_verify(fit_hdr, noffset)) {
400 puts("Bad Data Hash\n");
404 /* get fpga subimage data address and length */
405 if (fit_image_get_data(fit_hdr, noffset, &fit_data,
407 puts("Fpga subimage data not found\n");
411 return fpga_load(dev, fit_data, data_size, BIT_FULL);
415 puts("** Unknown image type\n");
421 static cmd_tbl_t fpga_commands[] = {
422 U_BOOT_CMD_MKENT(info, 1, 1, do_fpga_info, "", ""),
423 U_BOOT_CMD_MKENT(dump, 3, 1, do_fpga_dump, "", ""),
424 U_BOOT_CMD_MKENT(load, 3, 1, do_fpga_load, "", ""),
425 U_BOOT_CMD_MKENT(loadb, 3, 1, do_fpga_loadb, "", ""),
426 #if defined(CONFIG_CMD_FPGA_LOADP)
427 U_BOOT_CMD_MKENT(loadp, 3, 1, do_fpga_loadp, "", ""),
429 #if defined(CONFIG_CMD_FPGA_LOADBP)
430 U_BOOT_CMD_MKENT(loadbp, 3, 1, do_fpga_loadbp, "", ""),
432 #if defined(CONFIG_CMD_FPGA_LOADFS)
433 U_BOOT_CMD_MKENT(loadfs, 7, 1, do_fpga_loadfs, "", ""),
435 #if defined(CONFIG_CMD_FPGA_LOADMK)
436 U_BOOT_CMD_MKENT(loadmk, 2, 1, do_fpga_loadmk, "", ""),
440 static int do_fpga_wrapper(cmd_tbl_t *cmdtp, int flag, int argc,
447 return CMD_RET_USAGE;
449 fpga_cmd = find_cmd_tbl(argv[1], fpga_commands,
450 ARRAY_SIZE(fpga_commands));
452 /* This should be removed when all functions are converted */
454 return do_fpga(cmdtp, flag, argc, argv);
456 /* FIXME This can't be reached till all functions are converted */
458 debug("fpga: non existing command\n");
459 return CMD_RET_USAGE;
465 if (argc > fpga_cmd->maxargs) {
466 debug("fpga: more parameters passed\n");
467 return CMD_RET_USAGE;
470 ret = fpga_cmd->cmd(fpga_cmd, flag, argc, argv);
472 return cmd_process_error(fpga_cmd, ret);
475 #if defined(CONFIG_CMD_FPGA_LOADFS) || defined(CONFIG_CMD_FPGA_LOAD_SECURE)
476 U_BOOT_CMD(fpga, 9, 1, do_fpga_wrapper,
478 U_BOOT_CMD(fpga, 6, 1, do_fpga_wrapper,
480 "loadable FPGA image support",
481 "[operation type] [device number] [image address] [image size]\n"
483 " dump\t[dev] [address] [size]\tLoad device to memory buffer\n"
484 " info\t[dev]\t\t\tlist known device information\n"
485 " load\t[dev] [address] [size]\tLoad device from memory buffer\n"
486 #if defined(CONFIG_CMD_FPGA_LOADP)
487 " loadp\t[dev] [address] [size]\t"
488 "Load device from memory buffer with partial bitstream\n"
490 " loadb\t[dev] [address] [size]\t"
491 "Load device from bitstream buffer (Xilinx only)\n"
492 #if defined(CONFIG_CMD_FPGA_LOADBP)
493 " loadbp\t[dev] [address] [size]\t"
494 "Load device from bitstream buffer with partial bitstream"
497 #if defined(CONFIG_CMD_FPGA_LOADFS)
498 "Load device from filesystem (FAT by default) (Xilinx only)\n"
499 " loadfs [dev] [address] [image size] [blocksize] <interface>\n"
500 " [<dev[:part]>] <filename>\n"
502 #if defined(CONFIG_CMD_FPGA_LOADMK)
503 " loadmk [dev] [address]\tLoad device generated with mkimage"
504 #if defined(CONFIG_FIT)
506 "\tFor loadmk operating on FIT format uImage address must include\n"
507 "\tsubimage unit name in the form of addr:<subimg_uname>"
510 #if defined(CONFIG_CMD_FPGA_LOAD_SECURE)
511 "Load encrypted bitstream (Xilinx only)\n"
512 " loads [dev] [address] [size] [auth-OCM-0/DDR-1/noauth-2]\n"
513 " [enc-devkey(0)/userkey(1)/nenc(2) [Userkey address]\n"
514 "Loads the secure bistreams(authenticated/encrypted/both\n"
515 "authenticated and encrypted) of [size] from [address].\n"
516 "The auth-OCM/DDR flag specifies to perform authentication\n"
517 "in OCM or in DDR. 0 for OCM, 1 for DDR, 2 for no authentication.\n"
518 "The enc flag specifies which key to be used for decryption\n"
519 "0-device key, 1-user key, 2-no encryption.\n"
520 "The optional Userkey address specifies from which address key\n"
521 "has to be used for decryption if user key is selected.\n"
522 "NOTE: the sceure bitstream has to be created using xilinx\n"
523 "bootgen tool only.\n"