forked from ataradov/edbg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbg_win.c
188 lines (144 loc) · 6.18 KB
/
dbg_win.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
* Copyright (c) 2013-2015, Alex Taradov <alex@taradov.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*- Includes ----------------------------------------------------------------*/
#include <stdint.h>
#include <stdbool.h>
#include <windows.h>
#include <setupapi.h>
#include <ddk/hidsdi.h>
#include <ddk/hidpi.h>
#include "dbg.h"
#include "edbg.h"
/*- Definitions -------------------------------------------------------------*/
#define MAX_STRING_SIZE 256
/*- Variables ---------------------------------------------------------------*/
static HANDLE debugger_handle = INVALID_HANDLE_VALUE;
static uint8_t hid_buffer[1024 + 1];
static int report_size = 0;
/*- Implementations ---------------------------------------------------------*/
//-----------------------------------------------------------------------------
int dbg_enumerate(debugger_t *debuggers, int size)
{
GUID hid_guid;
HDEVINFO hid_dev_info;
HIDD_ATTRIBUTES hid_attr;
SP_DEVICE_INTERFACE_DATA dev_info_data;
PSP_DEVICE_INTERFACE_DETAIL_DATA detail_data;
DWORD detail_size;
HANDLE handle;
int rsize = 0;
HidD_GetHidGuid(&hid_guid);
hid_dev_info = SetupDiGetClassDevs(&hid_guid, NULL, NULL, DIGCF_PRESENT | DIGCF_INTERFACEDEVICE);
dev_info_data.cbSize = sizeof(dev_info_data);
for (int i = 0; i < size; i++)
{
if (FALSE == SetupDiEnumDeviceInterfaces(hid_dev_info, 0, &hid_guid, i, &dev_info_data))
break;
SetupDiGetDeviceInterfaceDetail(hid_dev_info, &dev_info_data, NULL, 0,
&detail_size, NULL);
detail_data = (PSP_DEVICE_INTERFACE_DETAIL_DATA)buf_alloc(detail_size);
detail_data->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
SetupDiGetDeviceInterfaceDetail(hid_dev_info, &dev_info_data, detail_data,
detail_size, NULL, NULL);
handle = CreateFile(detail_data->DevicePath, GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
if (INVALID_HANDLE_VALUE != handle)
{
wchar_t wstr[MAX_STRING_SIZE];
char str[MAX_STRING_SIZE];
hid_attr.Size = sizeof(hid_attr);
HidD_GetAttributes(handle, &hid_attr);
debuggers[rsize].path = strdup(detail_data->DevicePath);
HidD_GetSerialNumberString(handle, (PVOID)wstr, MAX_STRING_SIZE);
wcstombs(str, wstr, MAX_STRING_SIZE);
debuggers[rsize].serial = strdup(str);
HidD_GetManufacturerString(handle, (PVOID)wstr, MAX_STRING_SIZE);
wcstombs(str, wstr, MAX_STRING_SIZE);
debuggers[rsize].manufacturer = strdup(str);
HidD_GetProductString(handle, (PVOID)wstr, MAX_STRING_SIZE);
wcstombs(str, wstr, MAX_STRING_SIZE);
debuggers[rsize].product = strdup(str);
debuggers[rsize].vid = hid_attr.VendorID;
debuggers[rsize].pid = hid_attr.ProductID;
if (strstr(debuggers[rsize].product, "CMSIS-DAP"))
rsize++;
CloseHandle(handle);
}
buf_free(detail_data);
}
SetupDiDestroyDeviceInfoList(hid_dev_info);
return rsize;
}
//-----------------------------------------------------------------------------
void dbg_open(debugger_t *debugger)
{
HIDP_CAPS caps;
PHIDP_PREPARSED_DATA prep;
int input, output;
debugger_handle = CreateFile(debugger->path, GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
if (INVALID_HANDLE_VALUE == debugger_handle)
error_exit("unable to open device");
HidD_GetPreparsedData(debugger_handle, &prep);
HidP_GetCaps(prep, &caps);
input = caps.InputReportByteLength - 1;
output = caps.OutputReportByteLength - 1;
if (input != output)
error_exit("input and output report sizes do not match");
if (64 != input && 512 != input && 1024 != input)
error_exit("detected report size (%d) is not 64, 512 or 1024", input);
report_size = input;
}
//-----------------------------------------------------------------------------
void dbg_close(void)
{
if (INVALID_HANDLE_VALUE != debugger_handle)
CloseHandle(debugger_handle);
}
//-----------------------------------------------------------------------------
int dbg_get_report_size(void)
{
return report_size;
}
//-----------------------------------------------------------------------------
int dbg_dap_cmd(uint8_t *data, int size, int rsize)
{
char cmd = data[0];
unsigned long res;
memset(hid_buffer, 0xff, report_size + 1);
hid_buffer[0] = 0x00; // Report ID
memcpy(&hid_buffer[1], data, rsize);
if (FALSE == WriteFile(debugger_handle, (LPCVOID)hid_buffer, report_size + 1, &res, NULL))
error_exit("debugger write()");
if (FALSE == ReadFile(debugger_handle, (LPVOID)hid_buffer, report_size + 1, &res, NULL))
error_exit("debugger read()");
check(hid_buffer[1] == cmd, "invalid response received");
res -= 2;
memcpy(data, &hid_buffer[2], (size < (int)res) ? size : (int)res);
return res;
}