1 // Copyright (C) 2013 Michael McMaster <michael@codesrc.com>
3 // This file is part of SCSI2SD.
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.
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.
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/>.
26 #include "cybtldr_api.h"
27 #include "cybtldr_api2.h"
29 hid_device *handle = NULL;
31 static int OpenConnection(void)
36 static int CloseConnection(void)
41 static int ReadData(unsigned char* data, int count)
43 unsigned char buf[65];
44 buf[0] = 0; // Report ID
46 int result = hid_read(handle, buf, count);
50 fprintf(stderr, "USB HID Read Failure: %ls\n", hid_error(handle));
53 memcpy(data, buf, count);
55 return (result >= 0) ? 0 : -1;
58 static int WriteData(unsigned char* data, int count)
60 unsigned char buf[65];
61 buf[0] = 0; // report ID
63 for (i = 0; i < count; ++i)
67 int result = hid_write(handle, buf, count + 1);
71 fprintf(stderr, "USB HID Write Failure: %ls\n", hid_error(handle));
74 return (result >= 0) ? 0 : -1;
78 static void ProgressUpdate(unsigned char arrayId, unsigned short rowNum)
80 printf("Programmed flash array %d, row %d\n", arrayId, rowNum);
85 printf("Usage: bootloaderhost [-v UsbVendorId] [-p UsbProductId] [-f] /path/to/firmware.cyacd\n");
86 printf("\t-f\tForce, even if the firmware doesn't match the target board.\n");
90 int main(int argc, char* argv[])
92 CyBtldr_CommunicationsData cyComms =
101 printf("PSoC 3/5LP USB HID Bootloader Host\n");
102 printf("Copyright (C) 2013 Michael McMaster <michael@codesrc.com>\n\n");
104 uint16_t vendorId = 0x04B4; // Cypress
105 uint16_t productId = 0xB71D; // Default PSoC3/5LP Bootloader
110 while ((c = getopt(argc, argv, "v:p:f")) != -1)
115 sscanf(optarg, "%hx", &vendorId);
118 sscanf(optarg, "%hx", &productId);
129 const char* filename;
132 filename = argv[optind];
141 "USB device parameters\n\tVendor ID:\t0x%04X\n\tProduct ID:\t0x%04X\n",
145 // Enumerate and print the HID devices on the system
146 struct hid_device_info *dev = hid_enumerate(vendorId, productId);
150 printf("Waiting for device connection\n");
151 printf("Connect USB cable to the bus-powered device now, or otherwise reset the device.\n");
156 dev = hid_enumerate(vendorId, productId);
157 usleep(10000); // 10ms
160 printf("Device Found\n type: %04hx %04hx\n path: %s\n serial_number: %ls",
161 dev->vendor_id, dev->product_id, dev->path, dev->serial_number);
163 printf(" Manufacturer: %ls\n", dev->manufacturer_string);
164 printf(" Product: %ls\n", dev->product_string);
166 int fileMismatch = 0;
167 const char* expectedName = NULL;
168 switch (dev->release_number)
171 printf(" Release: 3.5\" SCSI2SD\n");
172 expectedName = "SCSI2SD.cyacd";
173 if (!strstr(filename, expectedName))
179 printf(" Release: 2.5\" SCSI2SD for Apple Powerbook\n");
180 expectedName = "pbook.cyacd";
181 if (!strstr(filename, expectedName))
187 printf(" Release: Unknown hardware\n");
188 expectedName = "unknown";
193 if (fileMismatch && !force)
195 fprintf(stderr, "ERROR: Unexpected firmware file. Expected: \"%s\"\n"
196 "Using firmware design for a different board may destroy your "
198 "If you still wish to proceed, try again with the \"-f\" flag.\n",
202 //hid_free_enumeration(devs);
204 // Open the device using the VID, PID,
205 // and optionally the Serial number.
206 handle = hid_open(vendorId, productId, NULL);
211 "Could not open device %s. Check permissions.\n", dev->path
217 printf("Starting firmware upload: %s\n", filename);
218 int result = CyBtldr_Program(
224 printf("Firmware update complete\n");
228 printf("Firmware update failed\n");