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);
32 /* ------------------------------------------------------------------------- */
34 * fpga <op> <device number> <data addr> <datasize>
35 * where op is 'load', 'dump', or 'info'
36 * If there is no device number field, the fpga environment variable is used.
37 * If there is no data addr field, the fpgadata environment variable is used.
38 * The info command requires no data address field.
40 int do_fpga(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
42 int op, dev = FPGA_INVALID_DEVICE;
44 void *fpga_data = NULL;
45 char *devstr = env_get("fpga");
46 char *datastr = env_get("fpgadata");
49 #if defined(CONFIG_FIT)
50 const char *fit_uname = NULL;
53 #if defined(CONFIG_CMD_FPGA_LOADFS)
54 fpga_fs_info fpga_fsinfo;
55 fpga_fsinfo.fstype = FS_TYPE_ANY;
59 dev = (int) simple_strtoul(devstr, NULL, 16);
61 fpga_data = (void *)simple_strtoul(datastr, NULL, 16);
63 if (argc > 9 || argc < 2) {
64 debug("%s: Too many or too few args (%d)\n", __func__, argc);
68 op = (int)fpga_get_op(argv[1]);
71 #if defined(CONFIG_CMD_FPGA_LOADFS)
75 fpga_fsinfo.blocksize = (unsigned int)
76 simple_strtoul(argv[5], NULL, 16);
77 fpga_fsinfo.interface = argv[6];
78 fpga_fsinfo.dev_part = argv[7];
79 fpga_fsinfo.filename = argv[8];
88 case 5: /* fpga <op> <dev> <data> <datasize> */
89 data_size = simple_strtoul(argv[4], NULL, 16);
91 case 4: /* fpga <op> <dev> <data> */
92 #if defined(CONFIG_FIT)
93 if (fit_parse_subimage(argv[3], (ulong)fpga_data,
94 &fit_addr, &fit_uname)) {
95 fpga_data = (void *)fit_addr;
96 debug("* fpga: subimage '%s' from FIT image ",
98 debug("at 0x%08lx\n", fit_addr);
102 fpga_data = (void *)simple_strtoul(argv[3], NULL, 16);
103 debug("* fpga: cmdline image address = 0x%08lx\n",
106 debug("%s: fpga_data = 0x%lx\n", __func__, (ulong)fpga_data);
108 case 3: /* fpga <op> <dev | data addr> */
109 dev = (int)simple_strtoul(argv[2], NULL, 16);
110 debug("%s: device = %d\n", __func__, dev);
111 /* FIXME - this is a really weak test */
112 if ((argc == 3) && (dev > fpga_count())) {
113 /* must be buffer ptr */
114 debug("%s: Assuming buffer pointer in arg 3\n",
117 #if defined(CONFIG_FIT)
118 if (fit_parse_subimage(argv[2], (ulong)fpga_data,
119 &fit_addr, &fit_uname)) {
120 fpga_data = (void *)fit_addr;
121 debug("* fpga: subimage '%s' from FIT image ",
123 debug("at 0x%08lx\n", fit_addr);
127 fpga_data = (void *)(uintptr_t)dev;
128 debug("* fpga: cmdline image addr = 0x%08lx\n",
132 debug("%s: fpga_data = 0x%lx\n",
133 __func__, (ulong)fpga_data);
134 dev = FPGA_INVALID_DEVICE; /* reset device num */
138 if (dev == FPGA_INVALID_DEVICE) {
139 puts("FPGA device not specified\n");
147 #if defined(CONFIG_CMD_FPGA_LOADFS)
149 /* Blocksize can be zero */
150 if (!fpga_fsinfo.interface || !fpga_fsinfo.dev_part ||
151 !fpga_fsinfo.filename)
159 if (!fpga_data || !data_size)
162 #if defined(CONFIG_CMD_FPGA_LOADMK)
171 puts("Wrong parameters for FPGA request\n");
177 return CMD_RET_USAGE;
184 rc = fpga_load(dev, fpga_data, data_size, BIT_FULL);
187 #if defined(CONFIG_CMD_FPGA_LOADP)
189 rc = fpga_load(dev, fpga_data, data_size, BIT_PARTIAL);
194 rc = fpga_loadbitstream(dev, fpga_data, data_size, BIT_FULL);
197 #if defined(CONFIG_CMD_FPGA_LOADBP)
199 rc = fpga_loadbitstream(dev, fpga_data, data_size, BIT_PARTIAL);
203 #if defined(CONFIG_CMD_FPGA_LOADFS)
205 rc = fpga_fsload(dev, fpga_data, data_size, &fpga_fsinfo);
209 #if defined(CONFIG_CMD_FPGA_LOADMK)
211 switch (genimg_get_format(fpga_data)) {
212 #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
213 case IMAGE_FORMAT_LEGACY:
215 image_header_t *hdr =
216 (image_header_t *)fpga_data;
220 comp = image_get_comp(hdr);
221 if (comp == IH_COMP_GZIP) {
222 #if defined(CONFIG_GZIP)
223 ulong image_buf = image_get_data(hdr);
224 data = image_get_load(hdr);
225 ulong image_size = ~0UL;
227 if (gunzip((void *)data, ~0UL,
230 puts("GUNZIP: error\n");
233 data_size = image_size;
235 puts("Gunzip image is not supported\n");
239 data = (ulong)image_get_data(hdr);
240 data_size = image_get_data_size(hdr);
242 rc = fpga_load(dev, (void *)data, data_size,
247 #if defined(CONFIG_FIT)
248 case IMAGE_FORMAT_FIT:
250 const void *fit_hdr = (const void *)fpga_data;
252 const void *fit_data;
254 if (fit_uname == NULL) {
255 puts("No FIT subimage unit name\n");
259 if (!fit_check_format(fit_hdr)) {
260 puts("Bad FIT image format\n");
264 /* get fpga component image node offset */
265 noffset = fit_image_get_node(fit_hdr,
268 printf("Can't find '%s' FIT subimage\n",
273 /* verify integrity */
274 if (!fit_image_verify(fit_hdr, noffset)) {
275 puts ("Bad Data Hash\n");
279 /* get fpga subimage data address and length */
280 if (fit_image_get_data(fit_hdr, noffset,
281 &fit_data, &data_size)) {
282 puts("Fpga subimage data not found\n");
286 rc = fpga_load(dev, fit_data, data_size,
292 puts("** Unknown image type\n");
300 rc = fpga_dump(dev, fpga_data, data_size);
304 printf("Unknown operation\n");
305 return CMD_RET_USAGE;
311 * Map op to supported operations. We don't use a table since we
312 * would just have to relocate it from flash anyway.
314 static int fpga_get_op(char *opstr)
318 if (!strcmp("info", opstr))
320 else if (!strcmp("loadb", opstr))
322 else if (!strcmp("load", opstr))
324 #if defined(CONFIG_CMD_FPGA_LOADP)
325 else if (!strcmp("loadp", opstr))
328 #if defined(CONFIG_CMD_FPGA_LOADBP)
329 else if (!strcmp("loadbp", opstr))
332 #if defined(CONFIG_CMD_FPGA_LOADFS)
333 else if (!strcmp("loadfs", opstr))
336 #if defined(CONFIG_CMD_FPGA_LOADMK)
337 else if (!strcmp("loadmk", opstr))
340 else if (!strcmp("dump", opstr))
344 printf("Unknown fpga operation \"%s\"\n", opstr);
349 #if defined(CONFIG_CMD_FPGA_LOADFS)
350 U_BOOT_CMD(fpga, 9, 1, do_fpga,
352 U_BOOT_CMD(fpga, 6, 1, do_fpga,
354 "loadable FPGA image support",
355 "[operation type] [device number] [image address] [image size]\n"
357 " dump\t[dev] [address] [size]\tLoad device to memory buffer\n"
358 " info\t[dev]\t\t\tlist known device information\n"
359 " load\t[dev] [address] [size]\tLoad device from memory buffer\n"
360 #if defined(CONFIG_CMD_FPGA_LOADP)
361 " loadp\t[dev] [address] [size]\t"
362 "Load device from memory buffer with partial bitstream\n"
364 " loadb\t[dev] [address] [size]\t"
365 "Load device from bitstream buffer (Xilinx only)\n"
366 #if defined(CONFIG_CMD_FPGA_LOADBP)
367 " loadbp\t[dev] [address] [size]\t"
368 "Load device from bitstream buffer with partial bitstream"
371 #if defined(CONFIG_CMD_FPGA_LOADFS)
372 "Load device from filesystem (FAT by default) (Xilinx only)\n"
373 " loadfs [dev] [address] [image size] [blocksize] <interface>\n"
374 " [<dev[:part]>] <filename>\n"
376 #if defined(CONFIG_CMD_FPGA_LOADMK)
377 " loadmk [dev] [address]\tLoad device generated with mkimage"
378 #if defined(CONFIG_FIT)
380 "\tFor loadmk operating on FIT format uImage address must include\n"
381 "\tsubimage unit name in the form of addr:<subimg_uname>"