fxload.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * Copyright © 2001 Stephen Williams (steve@icarus.com)
  3. * Copyright © 2001-2002 David Brownell (dbrownell@users.sourceforge.net)
  4. * Copyright © 2008 Roger Williams (rawqux@users.sourceforge.net)
  5. * Copyright © 2012 Pete Batard (pete@akeo.ie)
  6. * Copyright © 2013 Federico Manzan (f.manzan@gmail.com)
  7. *
  8. * This source code is free software; you can redistribute it
  9. * and/or modify it in source code form under the terms of the GNU
  10. * General Public License as published by the Free Software
  11. * Foundation; either version 2 of the License, or (at your option)
  12. * any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  22. */
  23. #include <config.h>
  24. #include <stdlib.h>
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <stdint.h>
  28. #include <stdarg.h>
  29. #include <sys/types.h>
  30. #include <getopt.h>
  31. #include "libusb.h"
  32. #include "ezusb.h"
  33. #if !defined(_WIN32) || defined(__CYGWIN__)
  34. #include <syslog.h>
  35. static bool dosyslog = false;
  36. #include <strings.h>
  37. #define _stricmp strcasecmp
  38. #endif
  39. #ifndef FXLOAD_VERSION
  40. #define FXLOAD_VERSION (__DATE__ " (libusb)")
  41. #endif
  42. #ifndef ARRAYSIZE
  43. #define ARRAYSIZE(A) (sizeof(A)/sizeof((A)[0]))
  44. #endif
  45. void logerror(const char *format, ...)
  46. {
  47. va_list ap;
  48. va_start(ap, format);
  49. #if !defined(_WIN32) || defined(__CYGWIN__)
  50. if (dosyslog)
  51. vsyslog(LOG_ERR, format, ap);
  52. else
  53. #endif
  54. vfprintf(stderr, format, ap);
  55. va_end(ap);
  56. }
  57. static int print_usage(int error_code) {
  58. fprintf(stderr, "\nUsage: fxload [-v] [-V] [-t type] [-d vid:pid] [-p bus,addr] [-s loader] -i firmware\n");
  59. fprintf(stderr, " -i <path> -- Firmware to upload\n");
  60. fprintf(stderr, " -s <path> -- Second stage loader\n");
  61. fprintf(stderr, " -t <type> -- Target type: an21, fx, fx2, fx2lp, fx3\n");
  62. fprintf(stderr, " -d <vid:pid> -- Target device, as an USB VID:PID\n");
  63. fprintf(stderr, " -p <bus,addr> -- Target device, as a libusb bus number and device address path\n");
  64. fprintf(stderr, " -v -- Increase verbosity\n");
  65. fprintf(stderr, " -q -- Decrease verbosity (silent mode)\n");
  66. fprintf(stderr, " -V -- Print program version\n");
  67. return error_code;
  68. }
  69. #define FIRMWARE 0
  70. #define LOADER 1
  71. int main(int argc, char*argv[])
  72. {
  73. fx_known_device known_device[] = FX_KNOWN_DEVICES;
  74. const char *path[] = { NULL, NULL };
  75. const char *device_id = NULL;
  76. const char *device_path = getenv("DEVICE");
  77. const char *type = NULL;
  78. const char *fx_name[FX_TYPE_MAX] = FX_TYPE_NAMES;
  79. const char *ext, *img_name[] = IMG_TYPE_NAMES;
  80. int fx_type = FX_TYPE_UNDEFINED, img_type[ARRAYSIZE(path)];
  81. int opt, status;
  82. unsigned int i, j;
  83. unsigned vid = 0, pid = 0;
  84. unsigned busnum = 0, devaddr = 0, _busnum, _devaddr;
  85. libusb_device *dev, **devs;
  86. libusb_device_handle *device = NULL;
  87. struct libusb_device_descriptor desc;
  88. while ((opt = getopt(argc, argv, "qvV?hd:p:i:I:s:S:t:")) != EOF)
  89. switch (opt) {
  90. case 'd':
  91. device_id = optarg;
  92. if (sscanf(device_id, "%x:%x" , &vid, &pid) != 2 ) {
  93. fputs ("please specify VID & PID as \"vid:pid\" in hexadecimal format\n", stderr);
  94. return -1;
  95. }
  96. break;
  97. case 'p':
  98. device_path = optarg;
  99. if (sscanf(device_path, "%u,%u", &busnum, &devaddr) != 2 ) {
  100. fputs ("please specify bus number & device number as \"bus,dev\" in decimal format\n", stderr);
  101. return -1;
  102. }
  103. break;
  104. case 'i':
  105. case 'I':
  106. path[FIRMWARE] = optarg;
  107. break;
  108. case 's':
  109. case 'S':
  110. path[LOADER] = optarg;
  111. break;
  112. case 'V':
  113. puts(FXLOAD_VERSION);
  114. return 0;
  115. case 't':
  116. type = optarg;
  117. break;
  118. case 'v':
  119. verbose++;
  120. break;
  121. case 'q':
  122. verbose--;
  123. break;
  124. case '?':
  125. case 'h':
  126. default:
  127. return print_usage(-1);
  128. }
  129. if (path[FIRMWARE] == NULL) {
  130. logerror("no firmware specified!\n");
  131. return print_usage(-1);
  132. }
  133. if ((device_id != NULL) && (device_path != NULL)) {
  134. logerror("only one of -d or -p can be specified\n");
  135. return print_usage(-1);
  136. }
  137. /* determine the target type */
  138. if (type != NULL) {
  139. for (i=0; i<FX_TYPE_MAX; i++) {
  140. if (strcmp(type, fx_name[i]) == 0) {
  141. fx_type = i;
  142. break;
  143. }
  144. }
  145. if (i >= FX_TYPE_MAX) {
  146. logerror("illegal microcontroller type: %s\n", type);
  147. return print_usage(-1);
  148. }
  149. }
  150. /* open the device using libusb */
  151. status = libusb_init(NULL);
  152. if (status < 0) {
  153. logerror("libusb_init() failed: %s\n", libusb_error_name(status));
  154. return -1;
  155. }
  156. libusb_set_option(NULL, LIBUSB_OPTION_LOG_LEVEL, verbose);
  157. /* try to pick up missing parameters from known devices */
  158. if ((type == NULL) || (device_id == NULL) || (device_path != NULL)) {
  159. if (libusb_get_device_list(NULL, &devs) < 0) {
  160. logerror("libusb_get_device_list() failed: %s\n", libusb_error_name(status));
  161. goto err;
  162. }
  163. for (i=0; (dev=devs[i]) != NULL; i++) {
  164. _busnum = libusb_get_bus_number(dev);
  165. _devaddr = libusb_get_device_address(dev);
  166. if ((type != NULL) && (device_path != NULL)) {
  167. // if both a type and bus,addr were specified, we just need to find our match
  168. if ((libusb_get_bus_number(dev) == busnum) && (libusb_get_device_address(dev) == devaddr))
  169. break;
  170. } else {
  171. status = libusb_get_device_descriptor(dev, &desc);
  172. if (status >= 0) {
  173. if (verbose >= 3) {
  174. logerror("examining %04x:%04x (%d,%d)\n",
  175. desc.idVendor, desc.idProduct, _busnum, _devaddr);
  176. }
  177. for (j=0; j<ARRAYSIZE(known_device); j++) {
  178. if ((desc.idVendor == known_device[j].vid)
  179. && (desc.idProduct == known_device[j].pid)) {
  180. if (// nothing was specified
  181. ((type == NULL) && (device_id == NULL) && (device_path == NULL)) ||
  182. // vid:pid was specified and we have a match
  183. ((type == NULL) && (device_id != NULL) && (vid == desc.idVendor) && (pid == desc.idProduct)) ||
  184. // bus,addr was specified and we have a match
  185. ((type == NULL) && (device_path != NULL) && (busnum == _busnum) && (devaddr == _devaddr)) ||
  186. // type was specified and we have a match
  187. ((type != NULL) && (device_id == NULL) && (device_path == NULL) && (fx_type == known_device[j].type)) ) {
  188. fx_type = known_device[j].type;
  189. vid = desc.idVendor;
  190. pid = desc.idProduct;
  191. busnum = _busnum;
  192. devaddr = _devaddr;
  193. break;
  194. }
  195. }
  196. }
  197. if (j < ARRAYSIZE(known_device)) {
  198. if (verbose)
  199. logerror("found device '%s' [%04x:%04x] (%d,%d)\n",
  200. known_device[j].designation, vid, pid, busnum, devaddr);
  201. break;
  202. }
  203. }
  204. }
  205. }
  206. if (dev == NULL) {
  207. libusb_free_device_list(devs, 1);
  208. libusb_exit(NULL);
  209. logerror("could not find a known device - please specify type and/or vid:pid and/or bus,dev\n");
  210. return print_usage(-1);
  211. }
  212. status = libusb_open(dev, &device);
  213. libusb_free_device_list(devs, 1);
  214. if (status < 0) {
  215. logerror("libusb_open() failed: %s\n", libusb_error_name(status));
  216. goto err;
  217. }
  218. } else if (device_id != NULL) {
  219. device = libusb_open_device_with_vid_pid(NULL, (uint16_t)vid, (uint16_t)pid);
  220. if (device == NULL) {
  221. logerror("libusb_open() failed\n");
  222. goto err;
  223. }
  224. }
  225. /* We need to claim the first interface */
  226. libusb_set_auto_detach_kernel_driver(device, 1);
  227. status = libusb_claim_interface(device, 0);
  228. if (status != LIBUSB_SUCCESS) {
  229. libusb_close(device);
  230. logerror("libusb_claim_interface failed: %s\n", libusb_error_name(status));
  231. goto err;
  232. }
  233. if (verbose)
  234. logerror("microcontroller type: %s\n", fx_name[fx_type]);
  235. for (i=0; i<ARRAYSIZE(path); i++) {
  236. if (path[i] != NULL) {
  237. ext = path[i] + strlen(path[i]) - 4;
  238. if ((_stricmp(ext, ".hex") == 0) || (strcmp(ext, ".ihx") == 0))
  239. img_type[i] = IMG_TYPE_HEX;
  240. else if (_stricmp(ext, ".iic") == 0)
  241. img_type[i] = IMG_TYPE_IIC;
  242. else if (_stricmp(ext, ".bix") == 0)
  243. img_type[i] = IMG_TYPE_BIX;
  244. else if (_stricmp(ext, ".img") == 0)
  245. img_type[i] = IMG_TYPE_IMG;
  246. else {
  247. logerror("%s is not a recognized image type\n", path[i]);
  248. goto err;
  249. }
  250. }
  251. if (verbose && path[i] != NULL)
  252. logerror("%s: type %s\n", path[i], img_name[img_type[i]]);
  253. }
  254. if (path[LOADER] == NULL) {
  255. /* single stage, put into internal memory */
  256. if (verbose > 1)
  257. logerror("single stage: load on-chip memory\n");
  258. status = ezusb_load_ram(device, path[FIRMWARE], fx_type, img_type[FIRMWARE], 0);
  259. } else {
  260. /* two-stage, put loader into internal memory */
  261. if (verbose > 1)
  262. logerror("1st stage: load 2nd stage loader\n");
  263. status = ezusb_load_ram(device, path[LOADER], fx_type, img_type[LOADER], 0);
  264. if (status == 0) {
  265. /* two-stage, put firmware into internal memory */
  266. if (verbose > 1)
  267. logerror("2nd state: load on-chip memory\n");
  268. status = ezusb_load_ram(device, path[FIRMWARE], fx_type, img_type[FIRMWARE], 1);
  269. }
  270. }
  271. libusb_release_interface(device, 0);
  272. libusb_close(device);
  273. libusb_exit(NULL);
  274. return status;
  275. err:
  276. libusb_exit(NULL);
  277. return -1;
  278. }