cmd_fpga.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. * (C) Copyright 2000, 2001
  3. * Rich Ireland, Enterasys Networks, rireland@enterasys.com.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. *
  23. */
  24. /*
  25. * FPGA support
  26. */
  27. #include <common.h>
  28. #include <command.h>
  29. #include <fpga.h>
  30. #include <malloc.h>
  31. /* Local functions */
  32. static int fpga_get_op(char *opstr);
  33. /* Local defines */
  34. #define FPGA_NONE -1
  35. #define FPGA_INFO 0
  36. #define FPGA_LOAD 1
  37. #define FPGA_LOADB 2
  38. #define FPGA_DUMP 3
  39. #define FPGA_LOADMK 4
  40. /* ------------------------------------------------------------------------- */
  41. /* command form:
  42. * fpga <op> <device number> <data addr> <datasize>
  43. * where op is 'load', 'dump', or 'info'
  44. * If there is no device number field, the fpga environment variable is used.
  45. * If there is no data addr field, the fpgadata environment variable is used.
  46. * The info command requires no data address field.
  47. */
  48. int do_fpga(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
  49. {
  50. int op, dev = FPGA_INVALID_DEVICE;
  51. size_t data_size = 0;
  52. void *fpga_data = NULL;
  53. char *devstr = getenv("fpga");
  54. char *datastr = getenv("fpgadata");
  55. int rc = FPGA_FAIL;
  56. int wrong_parms = 0;
  57. #if defined(CONFIG_FIT)
  58. const char *fit_uname = NULL;
  59. ulong fit_addr;
  60. #endif
  61. if (devstr)
  62. dev = (int) simple_strtoul(devstr, NULL, 16);
  63. if (datastr)
  64. fpga_data = (void *)simple_strtoul(datastr, NULL, 16);
  65. switch (argc) {
  66. case 5: /* fpga <op> <dev> <data> <datasize> */
  67. data_size = simple_strtoul(argv[4], NULL, 16);
  68. case 4: /* fpga <op> <dev> <data> */
  69. #if defined(CONFIG_FIT)
  70. if (fit_parse_subimage(argv[3], (ulong)fpga_data,
  71. &fit_addr, &fit_uname)) {
  72. fpga_data = (void *)fit_addr;
  73. debug("* fpga: subimage '%s' from FIT image ",
  74. fit_uname);
  75. debug("at 0x%08lx\n", fit_addr);
  76. } else
  77. #endif
  78. {
  79. fpga_data = (void *)simple_strtoul(argv[3], NULL, 16);
  80. debug("* fpga: cmdline image address = 0x%08lx\n",
  81. (ulong)fpga_data);
  82. }
  83. debug("%s: fpga_data = 0x%x\n", __func__, (uint)fpga_data);
  84. case 3: /* fpga <op> <dev | data addr> */
  85. dev = (int)simple_strtoul(argv[2], NULL, 16);
  86. debug("%s: device = %d\n", __func__, dev);
  87. /* FIXME - this is a really weak test */
  88. if ((argc == 3) && (dev > fpga_count())) {
  89. /* must be buffer ptr */
  90. debug("%s: Assuming buffer pointer in arg 3\n",
  91. __func__);
  92. #if defined(CONFIG_FIT)
  93. if (fit_parse_subimage(argv[2], (ulong)fpga_data,
  94. &fit_addr, &fit_uname)) {
  95. fpga_data = (void *)fit_addr;
  96. debug("* fpga: subimage '%s' from FIT image ",
  97. fit_uname);
  98. debug("at 0x%08lx\n", fit_addr);
  99. } else
  100. #endif
  101. {
  102. fpga_data = (void *)dev;
  103. debug("* fpga: cmdline image addr = 0x%08lx\n",
  104. (ulong)fpga_data);
  105. }
  106. debug("%s: fpga_data = 0x%x\n",
  107. __func__, (uint)fpga_data);
  108. dev = FPGA_INVALID_DEVICE; /* reset device num */
  109. }
  110. case 2: /* fpga <op> */
  111. op = (int)fpga_get_op(argv[1]);
  112. break;
  113. default:
  114. debug("%s: Too many or too few args (%d)\n", __func__, argc);
  115. op = FPGA_NONE; /* force usage display */
  116. break;
  117. }
  118. if (dev == FPGA_INVALID_DEVICE) {
  119. puts("FPGA device not specified\n");
  120. op = FPGA_NONE;
  121. }
  122. switch (op) {
  123. case FPGA_NONE:
  124. case FPGA_INFO:
  125. break;
  126. case FPGA_LOAD:
  127. case FPGA_LOADB:
  128. case FPGA_DUMP:
  129. if (!fpga_data || !data_size)
  130. wrong_parms = 1;
  131. break;
  132. case FPGA_LOADMK:
  133. if (!fpga_data)
  134. wrong_parms = 1;
  135. break;
  136. }
  137. if (wrong_parms) {
  138. puts("Wrong parameters for FPGA request\n");
  139. op = FPGA_NONE;
  140. }
  141. switch (op) {
  142. case FPGA_NONE:
  143. return CMD_RET_USAGE;
  144. case FPGA_INFO:
  145. rc = fpga_info(dev);
  146. break;
  147. case FPGA_LOAD:
  148. rc = fpga_load(dev, fpga_data, data_size);
  149. break;
  150. case FPGA_LOADB:
  151. rc = fpga_loadbitstream(dev, fpga_data, data_size);
  152. break;
  153. case FPGA_LOADMK:
  154. switch (genimg_get_format(fpga_data)) {
  155. case IMAGE_FORMAT_LEGACY:
  156. {
  157. image_header_t *hdr =
  158. (image_header_t *)fpga_data;
  159. ulong data;
  160. data = (ulong)image_get_data(hdr);
  161. data_size = image_get_data_size(hdr);
  162. rc = fpga_load(dev, (void *)data, data_size);
  163. }
  164. break;
  165. #if defined(CONFIG_FIT)
  166. case IMAGE_FORMAT_FIT:
  167. {
  168. const void *fit_hdr = (const void *)fpga_data;
  169. int noffset;
  170. const void *fit_data;
  171. if (fit_uname == NULL) {
  172. puts("No FIT subimage unit name\n");
  173. return 1;
  174. }
  175. if (!fit_check_format(fit_hdr)) {
  176. puts("Bad FIT image format\n");
  177. return 1;
  178. }
  179. /* get fpga component image node offset */
  180. noffset = fit_image_get_node(fit_hdr,
  181. fit_uname);
  182. if (noffset < 0) {
  183. printf("Can't find '%s' FIT subimage\n",
  184. fit_uname);
  185. return 1;
  186. }
  187. /* verify integrity */
  188. if (!fit_image_verify(fit_hdr, noffset)) {
  189. puts ("Bad Data Hash\n");
  190. return 1;
  191. }
  192. /* get fpga subimage data address and length */
  193. if (fit_image_get_data(fit_hdr, noffset,
  194. &fit_data, &data_size)) {
  195. puts("Fpga subimage data not found\n");
  196. return 1;
  197. }
  198. rc = fpga_load(dev, fit_data, data_size);
  199. }
  200. break;
  201. #endif
  202. default:
  203. puts("** Unknown image type\n");
  204. rc = FPGA_FAIL;
  205. break;
  206. }
  207. break;
  208. case FPGA_DUMP:
  209. rc = fpga_dump(dev, fpga_data, data_size);
  210. break;
  211. default:
  212. printf("Unknown operation\n");
  213. return CMD_RET_USAGE;
  214. }
  215. return rc;
  216. }
  217. /*
  218. * Map op to supported operations. We don't use a table since we
  219. * would just have to relocate it from flash anyway.
  220. */
  221. static int fpga_get_op(char *opstr)
  222. {
  223. int op = FPGA_NONE;
  224. if (!strcmp("info", opstr))
  225. op = FPGA_INFO;
  226. else if (!strcmp("loadb", opstr))
  227. op = FPGA_LOADB;
  228. else if (!strcmp("load", opstr))
  229. op = FPGA_LOAD;
  230. else if (!strcmp("loadmk", opstr))
  231. op = FPGA_LOADMK;
  232. else if (!strcmp("dump", opstr))
  233. op = FPGA_DUMP;
  234. if (op == FPGA_NONE)
  235. printf("Unknown fpga operation \"%s\"\n", opstr);
  236. return op;
  237. }
  238. U_BOOT_CMD(fpga, 6, 1, do_fpga,
  239. "loadable FPGA image support",
  240. "[operation type] [device number] [image address] [image size]\n"
  241. "fpga operations:\n"
  242. " dump\t[dev]\t\t\tLoad device to memory buffer\n"
  243. " info\t[dev]\t\t\tlist known device information\n"
  244. " load\t[dev] [address] [size]\tLoad device from memory buffer\n"
  245. " loadb\t[dev] [address] [size]\t"
  246. "Load device from bitstream buffer (Xilinx only)\n"
  247. " loadmk [dev] [address]\tLoad device generated with mkimage"
  248. #if defined(CONFIG_FIT)
  249. "\n"
  250. "\tFor loadmk operating on FIT format uImage address must include\n"
  251. "\tsubimage unit name in the form of addr:<subimg_uname>"
  252. #endif
  253. );