2 * (C) Copyright 2000, 2001
3 * Rich Ireland, Enterasys Networks, rireland@enterasys.com.
5 * SPDX-License-Identifier: GPL-2.0+
18 static int fpga_get_op(char *opstr);
31 /* ------------------------------------------------------------------------- */
33 * fpga <op> <device number> <data addr> <datasize>
34 * where op is 'load', 'dump', or 'info'
35 * If there is no device number field, the fpga environment variable is used.
36 * If there is no data addr field, the fpgadata environment variable is used.
37 * The info command requires no data address field.
39 int do_fpga(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
41 int op, dev = FPGA_INVALID_DEVICE;
43 void *fpga_data = NULL;
44 char *devstr = getenv("fpga");
45 char *datastr = getenv("fpgadata");
48 #if defined(CONFIG_FIT)
49 const char *fit_uname = NULL;
52 #if defined(CONFIG_CMD_FPGA_LOADFS)
53 fpga_fs_info fpga_fsinfo;
54 fpga_fsinfo.fstype = FS_TYPE_ANY;
58 dev = (int) simple_strtoul(devstr, NULL, 16);
60 fpga_data = (void *)simple_strtoul(datastr, NULL, 16);
63 #if defined(CONFIG_CMD_FPGA_LOADFS)
65 fpga_fsinfo.blocksize = (unsigned int)
66 simple_strtoul(argv[5], NULL, 16);
67 fpga_fsinfo.interface = argv[6];
68 fpga_fsinfo.dev_part = argv[7];
69 fpga_fsinfo.filename = argv[8];
71 case 5: /* fpga <op> <dev> <data> <datasize> */
72 data_size = simple_strtoul(argv[4], NULL, 16);
74 case 4: /* fpga <op> <dev> <data> */
75 #if defined(CONFIG_FIT)
76 if (fit_parse_subimage(argv[3], (ulong)fpga_data,
77 &fit_addr, &fit_uname)) {
78 fpga_data = (void *)fit_addr;
79 debug("* fpga: subimage '%s' from FIT image ",
81 debug("at 0x%08lx\n", fit_addr);
85 fpga_data = (void *)simple_strtoul(argv[3], NULL, 16);
86 debug("* fpga: cmdline image address = 0x%08lx\n",
89 debug("%s: fpga_data = 0x%x\n", __func__, (uint)fpga_data);
91 case 3: /* fpga <op> <dev | data addr> */
92 dev = (int)simple_strtoul(argv[2], NULL, 16);
93 debug("%s: device = %d\n", __func__, dev);
94 /* FIXME - this is a really weak test */
95 if ((argc == 3) && (dev > fpga_count())) {
96 /* must be buffer ptr */
97 debug("%s: Assuming buffer pointer in arg 3\n",
100 #if defined(CONFIG_FIT)
101 if (fit_parse_subimage(argv[2], (ulong)fpga_data,
102 &fit_addr, &fit_uname)) {
103 fpga_data = (void *)fit_addr;
104 debug("* fpga: subimage '%s' from FIT image ",
106 debug("at 0x%08lx\n", fit_addr);
110 fpga_data = (void *)dev;
111 debug("* fpga: cmdline image addr = 0x%08lx\n",
115 debug("%s: fpga_data = 0x%x\n",
116 __func__, (uint)fpga_data);
117 dev = FPGA_INVALID_DEVICE; /* reset device num */
120 case 2: /* fpga <op> */
121 op = (int)fpga_get_op(argv[1]);
125 debug("%s: Too many or too few args (%d)\n", __func__, argc);
126 op = FPGA_NONE; /* force usage display */
130 if (dev == FPGA_INVALID_DEVICE) {
131 puts("FPGA device not specified\n");
139 #if defined(CONFIG_CMD_FPGA_LOADFS)
141 /* Blocksize can be zero */
142 if (!fpga_fsinfo.interface || !fpga_fsinfo.dev_part ||
143 !fpga_fsinfo.filename)
151 if (!fpga_data || !data_size)
154 #if defined(CONFIG_CMD_FPGA_LOADMK)
163 puts("Wrong parameters for FPGA request\n");
169 return CMD_RET_USAGE;
176 rc = fpga_load(dev, fpga_data, data_size, BIT_FULL);
179 #if defined(CONFIG_CMD_FPGA_LOADP)
181 rc = fpga_load(dev, fpga_data, data_size, BIT_PARTIAL);
186 rc = fpga_loadbitstream(dev, fpga_data, data_size, BIT_FULL);
189 #if defined(CONFIG_CMD_FPGA_LOADBP)
191 rc = fpga_loadbitstream(dev, fpga_data, data_size, BIT_PARTIAL);
195 #if defined(CONFIG_CMD_FPGA_LOADFS)
197 rc = fpga_fsload(dev, fpga_data, data_size, &fpga_fsinfo);
201 #if defined(CONFIG_CMD_FPGA_LOADMK)
203 switch (genimg_get_format(fpga_data)) {
204 #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
205 case IMAGE_FORMAT_LEGACY:
207 image_header_t *hdr =
208 (image_header_t *)fpga_data;
212 comp = image_get_comp(hdr);
213 if (comp == IH_COMP_GZIP) {
214 #if defined(CONFIG_GZIP)
215 ulong image_buf = image_get_data(hdr);
216 data = image_get_load(hdr);
217 ulong image_size = ~0UL;
219 if (gunzip((void *)data, ~0UL,
222 puts("GUNZIP: error\n");
225 data_size = image_size;
227 puts("Gunzip image is not supported\n");
231 data = (ulong)image_get_data(hdr);
232 data_size = image_get_data_size(hdr);
234 rc = fpga_load(dev, (void *)data, data_size,
239 #if defined(CONFIG_FIT)
240 case IMAGE_FORMAT_FIT:
242 const void *fit_hdr = (const void *)fpga_data;
244 const void *fit_data;
246 if (fit_uname == NULL) {
247 puts("No FIT subimage unit name\n");
251 if (!fit_check_format(fit_hdr)) {
252 puts("Bad FIT image format\n");
256 /* get fpga component image node offset */
257 noffset = fit_image_get_node(fit_hdr,
260 printf("Can't find '%s' FIT subimage\n",
265 /* verify integrity */
266 if (!fit_image_verify(fit_hdr, noffset)) {
267 puts ("Bad Data Hash\n");
271 /* get fpga subimage data address and length */
272 if (fit_image_get_data(fit_hdr, noffset,
273 &fit_data, &data_size)) {
274 puts("Fpga subimage data not found\n");
278 rc = fpga_load(dev, fit_data, data_size,
284 puts("** Unknown image type\n");
292 rc = fpga_dump(dev, fpga_data, data_size);
296 printf("Unknown operation\n");
297 return CMD_RET_USAGE;
303 * Map op to supported operations. We don't use a table since we
304 * would just have to relocate it from flash anyway.
306 static int fpga_get_op(char *opstr)
310 if (!strcmp("info", opstr))
312 else if (!strcmp("loadb", opstr))
314 else if (!strcmp("load", opstr))
316 #if defined(CONFIG_CMD_FPGA_LOADP)
317 else if (!strcmp("loadp", opstr))
320 #if defined(CONFIG_CMD_FPGA_LOADBP)
321 else if (!strcmp("loadbp", opstr))
324 #if defined(CONFIG_CMD_FPGA_LOADFS)
325 else if (!strcmp("loadfs", opstr))
328 #if defined(CONFIG_CMD_FPGA_LOADMK)
329 else if (!strcmp("loadmk", opstr))
332 else if (!strcmp("dump", opstr))
336 printf("Unknown fpga operation \"%s\"\n", opstr);
341 #if defined(CONFIG_CMD_FPGA_LOADFS)
342 U_BOOT_CMD(fpga, 9, 1, do_fpga,
344 U_BOOT_CMD(fpga, 6, 1, do_fpga,
346 "loadable FPGA image support",
347 "[operation type] [device number] [image address] [image size]\n"
349 " dump\t[dev]\t\t\tLoad device to memory buffer\n"
350 " info\t[dev]\t\t\tlist known device information\n"
351 " load\t[dev] [address] [size]\tLoad device from memory buffer\n"
352 #if defined(CONFIG_CMD_FPGA_LOADP)
353 " loadp\t[dev] [address] [size]\t"
354 "Load device from memory buffer with partial bitstream\n"
356 " loadb\t[dev] [address] [size]\t"
357 "Load device from bitstream buffer (Xilinx only)\n"
358 #if defined(CONFIG_CMD_FPGA_LOADBP)
359 " loadbp\t[dev] [address] [size]\t"
360 "Load device from bitstream buffer with partial bitstream"
363 #if defined(CONFIG_CMD_FPGA_LOADFS)
364 "Load device from filesystem (FAT by default) (Xilinx only)\n"
365 " loadfs [dev] [address] [image size] [blocksize] <interface>\n"
366 " [<dev[:part]>] <filename>\n"
368 #if defined(CONFIG_CMD_FPGA_LOADMK)
369 " loadmk [dev] [address]\tLoad device generated with mkimage"
370 #if defined(CONFIG_FIT)
372 "\tFor loadmk operating on FIT format uImage address must include\n"
373 "\tsubimage unit name in the form of addr:<subimg_uname>"