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);
25 if (dev == FPGA_INVALID_DEVICE && arg)
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;
65 #if defined(CONFIG_CMD_FPGA_LOAD_SECURE)
66 int do_fpga_loads(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
71 struct fpga_secure_info fpga_sec_info;
73 memset(&fpga_sec_info, 0, sizeof(fpga_sec_info));
76 debug("fpga: incorrect parameters passed\n");
81 fpga_sec_info.userkey_addr = (u8 *)(uintptr_t)
82 simple_strtoull(argv[5],
86 * If 6th parameter is not passed then do_fpga_check_params
87 * will get 5 instead of expected 6 which means that function
88 * return CMD_RET_USAGE. Increase number of params +1 to pass
93 fpga_sec_info.encflag = (u8)simple_strtoul(argv[4], NULL, 16);
94 fpga_sec_info.authflag = (u8)simple_strtoul(argv[3], NULL, 16);
96 if (fpga_sec_info.authflag >= FPGA_NO_ENC_OR_NO_AUTH &&
97 fpga_sec_info.encflag >= FPGA_NO_ENC_OR_NO_AUTH) {
98 debug("fpga: Use <fpga load> for NonSecure bitstream\n");
102 if (fpga_sec_info.encflag == FPGA_ENC_USR_KEY &&
103 !fpga_sec_info.userkey_addr) {
104 debug("fpga: User key not provided\n");
105 return CMD_RET_USAGE;
108 ret = do_fpga_check_params(&dev, &fpga_data, &data_size,
113 return fpga_loads(dev, (void *)fpga_data, data_size, &fpga_sec_info);
117 #if defined(CONFIG_CMD_FPGA_LOADFS)
118 static int do_fpga_loadfs(cmd_tbl_t *cmdtp, int flag, int argc,
121 size_t data_size = 0;
124 fpga_fs_info fpga_fsinfo;
126 ret = do_fpga_check_params(&dev, &fpga_data, &data_size,
131 fpga_fsinfo.fstype = FS_TYPE_ANY;
132 fpga_fsinfo.blocksize = (unsigned int)simple_strtoul(argv[3], NULL, 16);
133 fpga_fsinfo.interface = argv[4];
134 fpga_fsinfo.dev_part = argv[5];
135 fpga_fsinfo.filename = argv[6];
137 return fpga_fsload(dev, (void *)fpga_data, data_size, &fpga_fsinfo);
141 static int do_fpga_info(cmd_tbl_t *cmdtp, int flag, int argc,
144 long dev = do_fpga_get_device(argv[0]);
146 return fpga_info(dev);
149 static int do_fpga_dump(cmd_tbl_t *cmdtp, int flag, int argc,
152 size_t data_size = 0;
156 ret = do_fpga_check_params(&dev, &fpga_data, &data_size,
161 return fpga_dump(dev, (void *)fpga_data, data_size);
164 static int do_fpga_load(cmd_tbl_t *cmdtp, int flag, int argc,
167 size_t data_size = 0;
171 ret = do_fpga_check_params(&dev, &fpga_data, &data_size,
176 return fpga_load(dev, (void *)fpga_data, data_size, BIT_FULL);
179 static int do_fpga_loadb(cmd_tbl_t *cmdtp, int flag, int argc,
182 size_t data_size = 0;
186 ret = do_fpga_check_params(&dev, &fpga_data, &data_size,
191 return fpga_loadbitstream(dev, (void *)fpga_data, data_size, BIT_FULL);
194 #if defined(CONFIG_CMD_FPGA_LOADP)
195 static int do_fpga_loadp(cmd_tbl_t *cmdtp, int flag, int argc,
198 size_t data_size = 0;
202 ret = do_fpga_check_params(&dev, &fpga_data, &data_size,
207 return fpga_load(dev, (void *)fpga_data, data_size, BIT_PARTIAL);
211 #if defined(CONFIG_CMD_FPGA_LOADBP)
212 static int do_fpga_loadbp(cmd_tbl_t *cmdtp, int flag, int argc,
215 size_t data_size = 0;
219 ret = do_fpga_check_params(&dev, &fpga_data, &data_size,
224 return fpga_loadbitstream(dev, (void *)fpga_data, data_size,
229 #if defined(CONFIG_CMD_FPGA_LOADMK)
230 static int do_fpga_loadmk(cmd_tbl_t *cmdtp, int flag, int argc,
233 size_t data_size = 0;
234 void *fpga_data = NULL;
235 #if defined(CONFIG_FIT)
236 const char *fit_uname = NULL;
239 ulong dev = do_fpga_get_device(argv[0]);
240 char *datastr = env_get("fpgadata");
242 debug("fpga: argc %x, dev %lx, datastr %s\n", argc, dev, datastr);
244 if (dev == FPGA_INVALID_DEVICE) {
245 debug("fpga: Invalid fpga device\n");
246 return CMD_RET_USAGE;
249 if (argc == 0 && !datastr) {
250 debug("fpga: No datastr passed\n");
251 return CMD_RET_USAGE;
256 debug("fpga: Full command with two args\n");
257 } else if (argc == 1 && !datastr) {
258 debug("fpga: Dev is setup - fpgadata passed\n");
262 #if defined(CONFIG_FIT)
263 if (fit_parse_subimage(datastr, (ulong)fpga_data,
264 &fit_addr, &fit_uname)) {
265 fpga_data = (void *)fit_addr;
266 debug("* fpga: subimage '%s' from FIT image ",
268 debug("at 0x%08lx\n", fit_addr);
272 fpga_data = (void *)simple_strtoul(datastr, NULL, 16);
273 debug("* fpga: cmdline image address = 0x%08lx\n",
276 debug("%s: fpga_data = 0x%lx\n", __func__, (ulong)fpga_data);
278 puts("Zero fpga_data address\n");
279 return CMD_RET_USAGE;
282 switch (genimg_get_format(fpga_data)) {
283 #if defined(CONFIG_LEGACY_IMAGE_FORMAT)
284 case IMAGE_FORMAT_LEGACY:
286 image_header_t *hdr = (image_header_t *)fpga_data;
290 comp = image_get_comp(hdr);
291 if (comp == IH_COMP_GZIP) {
292 #if defined(CONFIG_GZIP)
293 ulong image_buf = image_get_data(hdr);
294 ulong image_size = ~0UL;
296 data = image_get_load(hdr);
298 if (gunzip((void *)data, ~0UL, (void *)image_buf,
300 puts("GUNZIP: error\n");
301 return CMD_RET_FAILURE;
303 data_size = image_size;
305 puts("Gunzip image is not supported\n");
309 data = (ulong)image_get_data(hdr);
310 data_size = image_get_data_size(hdr);
312 return fpga_load(dev, (void *)data, data_size,
316 #if defined(CONFIG_FIT)
317 case IMAGE_FORMAT_FIT:
319 const void *fit_hdr = (const void *)fpga_data;
321 const void *fit_data;
324 puts("No FIT subimage unit name\n");
325 return CMD_RET_FAILURE;
328 if (!fit_check_format(fit_hdr)) {
329 puts("Bad FIT image format\n");
330 return CMD_RET_FAILURE;
333 /* get fpga component image node offset */
334 noffset = fit_image_get_node(fit_hdr, fit_uname);
336 printf("Can't find '%s' FIT subimage\n", fit_uname);
337 return CMD_RET_FAILURE;
340 /* verify integrity */
341 if (!fit_image_verify(fit_hdr, noffset)) {
342 puts("Bad Data Hash\n");
343 return CMD_RET_FAILURE;
346 /* get fpga subimage/external data address and length */
347 if (fit_image_get_data_and_size(fit_hdr, noffset,
348 &fit_data, &data_size)) {
349 puts("Fpga subimage data not found\n");
350 return CMD_RET_FAILURE;
353 return fpga_load(dev, fit_data, data_size, BIT_FULL);
357 puts("** Unknown image type\n");
358 return CMD_RET_FAILURE;
363 static cmd_tbl_t fpga_commands[] = {
364 U_BOOT_CMD_MKENT(info, 1, 1, do_fpga_info, "", ""),
365 U_BOOT_CMD_MKENT(dump, 3, 1, do_fpga_dump, "", ""),
366 U_BOOT_CMD_MKENT(load, 3, 1, do_fpga_load, "", ""),
367 U_BOOT_CMD_MKENT(loadb, 3, 1, do_fpga_loadb, "", ""),
368 #if defined(CONFIG_CMD_FPGA_LOADP)
369 U_BOOT_CMD_MKENT(loadp, 3, 1, do_fpga_loadp, "", ""),
371 #if defined(CONFIG_CMD_FPGA_LOADBP)
372 U_BOOT_CMD_MKENT(loadbp, 3, 1, do_fpga_loadbp, "", ""),
374 #if defined(CONFIG_CMD_FPGA_LOADFS)
375 U_BOOT_CMD_MKENT(loadfs, 7, 1, do_fpga_loadfs, "", ""),
377 #if defined(CONFIG_CMD_FPGA_LOADMK)
378 U_BOOT_CMD_MKENT(loadmk, 2, 1, do_fpga_loadmk, "", ""),
380 #if defined(CONFIG_CMD_FPGA_LOAD_SECURE)
381 U_BOOT_CMD_MKENT(loads, 6, 1, do_fpga_loads, "", ""),
385 static int do_fpga_wrapper(cmd_tbl_t *cmdtp, int flag, int argc,
392 return CMD_RET_USAGE;
394 fpga_cmd = find_cmd_tbl(argv[1], fpga_commands,
395 ARRAY_SIZE(fpga_commands));
397 debug("fpga: non existing command\n");
398 return CMD_RET_USAGE;
404 if (argc > fpga_cmd->maxargs) {
405 debug("fpga: more parameters passed\n");
406 return CMD_RET_USAGE;
409 ret = fpga_cmd->cmd(fpga_cmd, flag, argc, argv);
411 return cmd_process_error(fpga_cmd, ret);
414 #if defined(CONFIG_CMD_FPGA_LOADFS) || defined(CONFIG_CMD_FPGA_LOAD_SECURE)
415 U_BOOT_CMD(fpga, 9, 1, do_fpga_wrapper,
417 U_BOOT_CMD(fpga, 6, 1, do_fpga_wrapper,
419 "loadable FPGA image support",
420 "[operation type] [device number] [image address] [image size]\n"
422 " dump\t[dev] [address] [size]\tLoad device to memory buffer\n"
423 " info\t[dev]\t\t\tlist known device information\n"
424 " load\t[dev] [address] [size]\tLoad device from memory buffer\n"
425 #if defined(CONFIG_CMD_FPGA_LOADP)
426 " loadp\t[dev] [address] [size]\t"
427 "Load device from memory buffer with partial bitstream\n"
429 " loadb\t[dev] [address] [size]\t"
430 "Load device from bitstream buffer (Xilinx only)\n"
431 #if defined(CONFIG_CMD_FPGA_LOADBP)
432 " loadbp\t[dev] [address] [size]\t"
433 "Load device from bitstream buffer with partial bitstream"
436 #if defined(CONFIG_CMD_FPGA_LOADFS)
437 "Load device from filesystem (FAT by default) (Xilinx only)\n"
438 " loadfs [dev] [address] [image size] [blocksize] <interface>\n"
439 " [<dev[:part]>] <filename>\n"
441 #if defined(CONFIG_CMD_FPGA_LOADMK)
442 " loadmk [dev] [address]\tLoad device generated with mkimage"
443 #if defined(CONFIG_FIT)
445 "\tFor loadmk operating on FIT format uImage address must include\n"
446 "\tsubimage unit name in the form of addr:<subimg_uname>"
449 #if defined(CONFIG_CMD_FPGA_LOAD_SECURE)
450 "Load encrypted bitstream (Xilinx only)\n"
451 " loads [dev] [address] [size] [auth-OCM-0/DDR-1/noauth-2]\n"
452 " [enc-devkey(0)/userkey(1)/nenc(2) [Userkey address]\n"
453 "Loads the secure bistreams(authenticated/encrypted/both\n"
454 "authenticated and encrypted) of [size] from [address].\n"
455 "The auth-OCM/DDR flag specifies to perform authentication\n"
456 "in OCM or in DDR. 0 for OCM, 1 for DDR, 2 for no authentication.\n"
457 "The enc flag specifies which key to be used for decryption\n"
458 "0-device key, 1-user key, 2-no encryption.\n"
459 "The optional Userkey address specifies from which address key\n"
460 "has to be used for decryption if user key is selected.\n"
461 "NOTE: the secure bitstream has to be created using Xilinx\n"
462 "bootgen tool only.\n"