main.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. // Copyright (C) 2013 Michael McMaster <michael@codesrc.com>
  2. //
  3. // This file is part of SCSI2SD.
  4. //
  5. // SCSI2SD is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // SCSI2SD is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with SCSI2SD. If not, see <http://www.gnu.org/licenses/>.
  17. #include <stdint.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <unistd.h>
  22. #include "hidapi.h"
  23. #include "cybtldr_api.h"
  24. #include "cybtldr_api2.h"
  25. hid_device *handle = NULL;
  26. static int OpenConnection(void)
  27. {
  28. return 0;
  29. }
  30. static int CloseConnection(void)
  31. {
  32. return 0;
  33. }
  34. static int ReadData(unsigned char* data, int count)
  35. {
  36. unsigned char buf[65];
  37. buf[0] = 0; // Report ID
  38. int result = hid_read(handle, buf, count);
  39. if (result < 0)
  40. {
  41. fprintf(stderr, "USB HID Read Failure: %ls\n", hid_error(handle));
  42. }
  43. memcpy(data, buf, count);
  44. return (result >= 0) ? 0 : -1;
  45. }
  46. static int WriteData(unsigned char* data, int count)
  47. {
  48. unsigned char buf[65];
  49. buf[0] = 0; // report ID
  50. int i;
  51. for (i = 0; i < count; ++i)
  52. {
  53. buf[i+1] = data[i];
  54. }
  55. int result = hid_write(handle, buf, count + 1);
  56. if (result < 0)
  57. {
  58. fprintf(stderr, "USB HID Write Failure: %ls\n", hid_error(handle));
  59. }
  60. return (result >= 0) ? 0 : -1;
  61. }
  62. static void ProgressUpdate(unsigned char arrayId, unsigned short rowNum)
  63. {
  64. printf("Programmed flash array %d, row %d\n", arrayId, rowNum);
  65. }
  66. static void usage()
  67. {
  68. printf("Usage: bootloaderhost [-v UsbVendorId] [-p UsbProductId] [-f] /path/to/firmware.cyacd\n");
  69. printf("\t-f\tForce, even if the firmware doesn't match the target board.\n");
  70. printf("\n\n");
  71. }
  72. int main(int argc, char* argv[])
  73. {
  74. CyBtldr_CommunicationsData cyComms =
  75. {
  76. &OpenConnection,
  77. &CloseConnection,
  78. &ReadData,
  79. &WriteData,
  80. 64
  81. };
  82. printf("PSoC 3/5LP USB HID Bootloader Host\n");
  83. printf("Copyright (C) 2013 Michael McMaster <michael@codesrc.com>\n\n");
  84. uint16_t vendorId = 0x04B4; // Cypress
  85. uint16_t productId = 0xB71D; // Default PSoC3/5LP Bootloader
  86. int force = 0;
  87. opterr = 0;
  88. int c;
  89. while ((c = getopt(argc, argv, "v:p:f")) != -1)
  90. {
  91. switch (c)
  92. {
  93. case 'v':
  94. sscanf(optarg, "%hx", &vendorId);
  95. break;
  96. case 'p':
  97. sscanf(optarg, "%hx", &productId);
  98. break;
  99. case 'f':
  100. force = 1;
  101. break;
  102. case '?':
  103. usage();
  104. exit(1);
  105. }
  106. }
  107. const char* filename;
  108. if (optind < argc)
  109. {
  110. filename = argv[optind];
  111. }
  112. else
  113. {
  114. usage();
  115. exit(1);
  116. }
  117. printf(
  118. "USB device parameters\n\tVendor ID:\t0x%04X\n\tProduct ID:\t0x%04X\n",
  119. vendorId,
  120. productId);
  121. // Enumerate and print the HID devices on the system
  122. struct hid_device_info *dev = hid_enumerate(vendorId, productId);
  123. if (!dev)
  124. {
  125. printf("Waiting for device connection\n");
  126. printf("Connect USB cable to the bus-powered device now, or otherwise reset the device.\n");
  127. }
  128. while (!dev)
  129. {
  130. dev = hid_enumerate(vendorId, productId);
  131. usleep(10000); // 10ms
  132. }
  133. printf("Device Found\n type: %04hx %04hx\n path: %s\n serial_number: %ls",
  134. dev->vendor_id, dev->product_id, dev->path, dev->serial_number);
  135. printf("\n");
  136. printf(" Manufacturer: %ls\n", dev->manufacturer_string);
  137. printf(" Product: %ls\n", dev->product_string);
  138. int fileMismatch = 0;
  139. const char* expectedName = NULL;
  140. switch (dev->release_number)
  141. {
  142. case 0x3001:
  143. printf(" Release: 3.5\" SCSI2SD\n");
  144. expectedName = "SCSI2SD.cyacd";
  145. if (!strstr(filename, expectedName))
  146. {
  147. fileMismatch = 1;
  148. }
  149. break;
  150. case 0x3002:
  151. printf(" Release: 2.5\" SCSI2SD for Apple Powerbook\n");
  152. expectedName = "pbook.cyacd";
  153. if (!strstr(filename, expectedName))
  154. {
  155. fileMismatch = 1;
  156. }
  157. break;
  158. default:
  159. printf(" Release: Unknown hardware\n");
  160. expectedName = "unknown";
  161. fileMismatch = 1;
  162. }
  163. printf("\n");
  164. if (fileMismatch && !force)
  165. {
  166. fprintf(stderr, "ERROR: Unexpected firmware file. Expected: \"%s\"\n"
  167. "Using firmware design for a different board may destroy your "
  168. "hardware.\n"
  169. "If you still wish to proceed, try again with the \"-f\" flag.\n",
  170. expectedName);
  171. exit(1);
  172. }
  173. //hid_free_enumeration(devs);
  174. // Open the device using the VID, PID,
  175. // and optionally the Serial number.
  176. handle = hid_open(vendorId, productId, NULL);
  177. if (!handle)
  178. {
  179. fprintf(
  180. stderr,
  181. "Could not open device %s. Check permissions.\n", dev->path
  182. );
  183. exit(1);
  184. }
  185. printf("Starting firmware upload: %s\n", filename);
  186. int result = CyBtldr_Program(
  187. filename,
  188. &cyComms,
  189. &ProgressUpdate);
  190. if (result == 0)
  191. {
  192. printf("Firmware update complete\n");
  193. }
  194. else
  195. {
  196. printf("Firmware update failed\n");
  197. }
  198. return 0;
  199. }