num = read_intel_hex(filename);
if (num < 0) die("error reading intel hex file \"%s\"", filename);
printf_verbose("Read \"%s\": %d bytes, %.1f%% usage\n",
- filename, num, (double)num / (double)code_size * 100.0);
+ filename, num, (double)num / (double)code_size * 100.0);
}
// program the data
/****************************************************************/
/* */
-/* USB Access - libusb (Linux & FreeBSD) */
+/* USB Access - libusb (Linux, Windows & FreeBSD) */
/* */
/****************************************************************/
#endif
-/****************************************************************/
-/* */
-/* USB Access - Microsoft WIN32 */
-/* */
-/****************************************************************/
-
-#if defined(USE_WIN32)
-
-// http://msdn.microsoft.com/en-us/library/ms790932.aspx
-#include <windows.h>
-#include <setupapi.h>
-#include <ddk/hidsdi.h>
-#include <ddk/hidclass.h>
-
-HANDLE open_usb_device(int vid, int pid)
-{
- GUID guid;
- HDEVINFO info;
- DWORD index, required_size;
- SP_DEVICE_INTERFACE_DATA iface;
- SP_DEVICE_INTERFACE_DETAIL_DATA *details;
- HIDD_ATTRIBUTES attrib;
- HANDLE h;
- BOOL ret;
-
- HidD_GetHidGuid(&guid);
- info = SetupDiGetClassDevs(&guid, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
- if (info == INVALID_HANDLE_VALUE) return NULL;
- for (index=0; 1 ;index++) {
- iface.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
- ret = SetupDiEnumDeviceInterfaces(info, NULL, &guid, index, &iface);
- if (!ret) {
- SetupDiDestroyDeviceInfoList(info);
- break;
- }
- SetupDiGetInterfaceDeviceDetail(info, &iface, NULL, 0, &required_size, NULL);
- details = (SP_DEVICE_INTERFACE_DETAIL_DATA *)malloc(required_size);
- if (details == NULL) continue;
- memset(details, 0, required_size);
- details->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
- ret = SetupDiGetDeviceInterfaceDetail(info, &iface, details,
- required_size, NULL, NULL);
- if (!ret) {
- free(details);
- continue;
- }
- h = CreateFile(details->DevicePath, GENERIC_READ|GENERIC_WRITE,
- FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
- FILE_FLAG_OVERLAPPED, NULL);
- free(details);
- if (h == INVALID_HANDLE_VALUE) continue;
- attrib.Size = sizeof(HIDD_ATTRIBUTES);
- ret = HidD_GetAttributes(h, &attrib);
- if (!ret) {
- CloseHandle(h);
- continue;
- }
- if (attrib.VendorID != vid || attrib.ProductID != pid) {
- CloseHandle(h);
- continue;
- }
- SetupDiDestroyDeviceInfoList(info);
- return h;
- }
- return NULL;
-}
-
-int write_usb_device(HANDLE h, void *buf, int len, int timeout)
-{
- static HANDLE event = NULL;
- unsigned char tmpbuf[1040];
- OVERLAPPED ov;
- DWORD n, r;
-
- if (len > sizeof(tmpbuf) - 1) return 0;
- if (event == NULL) {
- event = CreateEvent(NULL, TRUE, TRUE, NULL);
- if (!event) return 0;
- }
- ResetEvent(&event);
- memset(&ov, 0, sizeof(ov));
- ov.hEvent = event;
- tmpbuf[0] = 0;
- memcpy(tmpbuf + 1, buf, len);
- if (!WriteFile(h, tmpbuf, len + 1, NULL, &ov)) {
- if (GetLastError() != ERROR_IO_PENDING) return 0;
- r = WaitForSingleObject(event, timeout);
- if (r == WAIT_TIMEOUT) {
- CancelIo(h);
- return 0;
- }
- if (r != WAIT_OBJECT_0) return 0;
- }
- if (!GetOverlappedResult(h, &ov, &n, FALSE)) return 0;
- if (n <= 0) return 0;
- return 1;
-}
-
-void print_win32_err(void)
-{
- char buf[256];
- DWORD err;
-
- err = GetLastError();
- FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, err,
- 0, buf, sizeof(buf), NULL);
- printf("err %ld: %s\n", err, buf);
-}
-
-static HANDLE win32_teensy_handle = NULL;
-
-int teensy_open(void)
-{
- teensy_close();
- win32_teensy_handle = open_usb_device(0x16C0, 0x0478);
- if (win32_teensy_handle) return 1;
- return 0;
-}
-
-int teensy_write(void *buf, int len, double timeout)
-{
- int r;
- if (!win32_teensy_handle) return 0;
- r = write_usb_device(win32_teensy_handle, buf, len, (int)(timeout * 1000.0));
- //if (!r) print_win32_err();
- return r;
-}
-
-void teensy_close(void)
-{
- if (!win32_teensy_handle) return;
- CloseHandle(win32_teensy_handle);
- win32_teensy_handle = NULL;
-}
-
-int hard_reboot(void)
-{
- HANDLE rebootor;
- int r;
-
- rebootor = open_usb_device(0x16C0, 0x0477);
- if (!rebootor) return 0;
- r = write_usb_device(rebootor, "reboot", 6, 100);
- CloseHandle(rebootor);
- return r;
-}
-
-#endif
-
-
/****************************************************************/
/* */
parse_hex_line(char *line)
{
int addr, code, num;
- int sum, len, cksum, i;
- char *ptr;
-
- num = 0;
- if (line[0] != ':') return 0;
- if (strlen(line) < 11) return 0;
- ptr = line+1;
- if (!sscanf(ptr, "%02x", &len)) return 0;
- ptr += 2;
- if ((int)strlen(line) < (11 + (len * 2)) ) return 0;
- if (!sscanf(ptr, "%04x", &addr)) return 0;
- ptr += 4;
- /* printf("Line: length=%d Addr=%d\n", len, addr); */
- if (!sscanf(ptr, "%02x", &code)) return 0;
+ int sum, len, cksum, i;
+ char *ptr;
+
+ num = 0;
+ if (line[0] != ':') return 0;
+ if (strlen(line) < 11) return 0;
+ ptr = line+1;
+ if (!sscanf(ptr, "%02x", &len)) return 0;
+ ptr += 2;
+ if ((int)strlen(line) < (11 + (len * 2)) ) return 0;
+ if (!sscanf(ptr, "%04x", &addr)) return 0;
+ ptr += 4;
+ /* printf("Line: length=%d Addr=%d\n", len, addr); */
+ if (!sscanf(ptr, "%02x", &code)) return 0;
if (addr + extended_addr + len >= MAX_MEMORY_SIZE) return 0;
- ptr += 2;
- sum = (len & 255) + ((addr >> 8) & 255) + (addr & 255) + (code & 255);
+ ptr += 2;
+ sum = (len & 255) + ((addr >> 8) & 255) + (addr & 255) + (code & 255);
if (code != 0) {
if (code == 1) {
end_record_seen = 1;
if (!sscanf(ptr, "%04x", &i)) return 1;
ptr += 4;
sum += ((i >> 8) & 255) + (i & 255);
- if (!sscanf(ptr, "%02x", &cksum)) return 1;
+ if (!sscanf(ptr, "%02x", &cksum)) return 1;
if (((sum & 255) + (cksum & 255)) & 255) return 1;
extended_addr = i << 4;
//printf("ext addr = %05X\n", extended_addr);
if (!sscanf(ptr, "%04x", &i)) return 1;
ptr += 4;
sum += ((i >> 8) & 255) + (i & 255);
- if (!sscanf(ptr, "%02x", &cksum)) return 1;
+ if (!sscanf(ptr, "%02x", &cksum)) return 1;
if (((sum & 255) + (cksum & 255)) & 255) return 1;
extended_addr = i << 16;
//printf("ext addr = %08X\n", extended_addr);
}
- return 1; // non-data line
+ return 1; // non-data line
}
byte_count += len;
- while (num != len) {
- if (sscanf(ptr, "%02x", &i) != 1) return 0;
+ while (num != len) {
+ if (sscanf(ptr, "%02x", &i) != 1) return 0;
i &= 255;
firmware_image[addr + extended_addr + num] = i;
firmware_mask[addr + extended_addr + num] = 1;
- ptr += 2;
- sum += i;
- (num)++;
- if (num >= 256) return 0;
- }
- if (!sscanf(ptr, "%02x", &cksum)) return 0;
- if (((sum & 255) + (cksum & 255)) & 255) return 0; /* checksum error */
- return 1;
+ ptr += 2;
+ sum += i;
+ (num)++;
+ if (num >= 256) return 0;
+ }
+ if (!sscanf(ptr, "%02x", &cksum)) return 0;
+ if (((sum & 255) + (cksum & 255)) & 255) return 0; /* checksum error */
+ return 1;
}
int ihex_bytes_within_range(int begin, int end)