-
Notifications
You must be signed in to change notification settings - Fork 9
/
core.ts
257 lines (226 loc) · 7.19 KB
/
core.ts
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
export type DFUseMemorySegment = {
start: number;
end: number;
sectorSize: number;
readable: boolean;
erasable: boolean;
writable: boolean;
};
export enum DFUseCommands {
GET_COMMANDS = 0x00,
SET_ADDRESS = 0x21,
ERASE_SECTOR = 0x41,
}
export type WebDFUSettings = {
name?: string;
configuration: USBConfiguration;
interface: USBInterface;
alternate: USBAlternateInterface;
};
export type WebDFUDeviceDescriptor = {
bLength: number;
bDescriptorType: number;
bcdUSB: number;
bDeviceClass: number;
bDeviceSubClass: number;
bDeviceProtocol: number;
bMaxPacketSize: number;
idVendor: number;
idProduct: number;
bcdDevice: number;
iManufacturer: number;
iProduct: number;
iSerialNumber: number;
bNumConfigurations: number;
};
export type WebDFUFunctionalDescriptor = {
bLength: number;
bDescriptorType: number;
bmAttributes: number;
wDetachTimeOut: number;
wTransferSize: number;
bcdDFUVersion: number;
};
export type WebDFUInterfaceDescriptor = {
bLength: number;
bDescriptorType: number;
bInterfaceNumber: number;
bAlternateSetting: number;
bNumEndpoints: number;
bInterfaceClass: number;
bInterfaceSubClass: number;
bInterfaceProtocol: number;
iInterface: number;
descriptors: (WebDFUFunctionalDescriptor | WebDFUInterfaceSubDescriptor)[];
};
export type WebDFUInterfaceSubDescriptor = {
descData: DataView;
bLength: number;
bDescriptorType: number;
bmAttributes: number;
wDetachTimeOut: number;
wTransferSize: number;
bcdDFUVersion: number;
};
export type WebDFUEvent = {
init: () => void;
connect: () => void;
disconnect: (error?: Error) => void;
};
export type WebDFUOptions = {
forceInterfacesName?: boolean;
};
export type WebDFUProperties = {
WillDetach: boolean;
ManifestationTolerant: boolean;
CanUpload: boolean;
CanDownload: boolean;
TransferSize: number;
DetachTimeOut: number;
DFUVersion: number;
};
export type WebDFULog = Record<"info" | "warning", (msg: string) => void> & {
progress: (done: number, total?: number) => void;
};
export const WebDFUType: Record<"DFU" | "SDFUse", number> = {
DFU: 1,
SDFUse: 2,
};
export class WebDFUError extends Error {}
// Parse descriptors
export function parseMemoryDescriptor(desc: string): { name: string; segments: DFUseMemorySegment[] } {
const nameEndIndex = desc.indexOf("/");
if (!desc.startsWith("@") || nameEndIndex == -1) {
throw new WebDFUError(`Not a DfuSe memory descriptor: "${desc}"`);
}
const name = desc.substring(1, nameEndIndex).trim();
const segmentString = desc.substring(nameEndIndex);
let segments = [];
const sectorMultipliers: Record<string, number> = {
" ": 1,
B: 1,
K: 1024,
M: 1048576,
};
let contiguousSegmentRegex = /\/\s*(0x[0-9a-fA-F]{1,8})\s*\/(\s*[0-9]+\s*\*\s*[0-9]+\s?[ BKM]\s*[abcdefg]\s*,?\s*)+/g;
let contiguousSegmentMatch: RegExpExecArray | null;
while ((contiguousSegmentMatch = contiguousSegmentRegex.exec(segmentString))) {
let segmentRegex = /([0-9]+)\s*\*\s*([0-9]+)\s?([ BKM])\s*([abcdefg])\s*,?\s*/g;
let startAddress = parseInt(contiguousSegmentMatch?.[1] ?? "", 16);
let segmentMatch: RegExpExecArray | null;
while ((segmentMatch = segmentRegex.exec(contiguousSegmentMatch[0]!))) {
let sectorCount = parseInt(segmentMatch[1]!, 10);
let sectorSize = parseInt(segmentMatch[2]!) * (sectorMultipliers[segmentMatch?.[3] ?? ""] ?? 0);
let properties = (segmentMatch?.[4] ?? "")?.charCodeAt(0) - "a".charCodeAt(0) + 1;
let segment = {
start: startAddress,
sectorSize: sectorSize,
end: startAddress + sectorSize * sectorCount,
readable: (properties & 0x1) != 0,
erasable: (properties & 0x2) != 0,
writable: (properties & 0x4) != 0,
};
segments.push(segment);
startAddress += sectorSize * sectorCount;
}
}
return { name, segments };
}
export function parseDeviceDescriptor(data: DataView): WebDFUDeviceDescriptor {
return {
bLength: data.getUint8(0),
bDescriptorType: data.getUint8(1),
bcdUSB: data.getUint16(2, true),
bDeviceClass: data.getUint8(4),
bDeviceSubClass: data.getUint8(5),
bDeviceProtocol: data.getUint8(6),
bMaxPacketSize: data.getUint8(7),
idVendor: data.getUint16(8, true),
idProduct: data.getUint16(10, true),
bcdDevice: data.getUint16(12, true),
iManufacturer: data.getUint8(14),
iProduct: data.getUint8(15),
iSerialNumber: data.getUint8(16),
bNumConfigurations: data.getUint8(17),
};
}
export function parseFunctionalDescriptor(data: DataView): WebDFUFunctionalDescriptor {
return {
bLength: data.getUint8(0),
bDescriptorType: data.getUint8(1),
bmAttributes: data.getUint8(2),
wDetachTimeOut: data.getUint16(3, true),
wTransferSize: data.getUint16(5, true),
bcdDFUVersion: data.getUint16(7, true),
};
}
export function parseInterfaceDescriptor(data: DataView): WebDFUInterfaceDescriptor {
return {
bLength: data.getUint8(0),
bDescriptorType: data.getUint8(1),
bInterfaceNumber: data.getUint8(2),
bAlternateSetting: data.getUint8(3),
bNumEndpoints: data.getUint8(4),
bInterfaceClass: data.getUint8(5),
bInterfaceSubClass: data.getUint8(6),
bInterfaceProtocol: data.getUint8(7),
iInterface: data.getUint8(8),
descriptors: [],
};
}
export function parseSubDescriptors(descriptorData: DataView) {
const DT_INTERFACE = 4;
// const DT_ENDPOINT = 5;
const DT_DFU_FUNCTIONAL = 0x21;
const USB_CLASS_APP_SPECIFIC = 0xfe;
const USB_SUBCLASS_DFU = 0x01;
let remainingData: DataView = descriptorData;
let descriptors = [];
let currIntf;
let inDfuIntf = false;
while (remainingData.byteLength > 2) {
let bLength = remainingData.getUint8(0);
let bDescriptorType = remainingData.getUint8(1);
let descData = new DataView(remainingData.buffer.slice(0, bLength));
if (bDescriptorType == DT_INTERFACE) {
currIntf = parseInterfaceDescriptor(descData);
if (currIntf.bInterfaceClass == USB_CLASS_APP_SPECIFIC && currIntf.bInterfaceSubClass == USB_SUBCLASS_DFU) {
inDfuIntf = true;
} else {
inDfuIntf = false;
}
descriptors.push(currIntf);
} else if (inDfuIntf && bDescriptorType == DT_DFU_FUNCTIONAL) {
let funcDesc = parseFunctionalDescriptor(descData);
descriptors.push(funcDesc);
currIntf?.descriptors.push(funcDesc);
} else {
let desc = {
bLength: bLength,
bDescriptorType: bDescriptorType,
descData: descData,
} as WebDFUInterfaceSubDescriptor;
descriptors.push(desc);
if (currIntf) {
currIntf.descriptors.push(desc);
}
}
remainingData = new DataView(remainingData.buffer.slice(bLength));
}
return descriptors;
}
export function parseConfigurationDescriptor(data: DataView) {
let descriptorData = new DataView(data.buffer.slice(9));
let descriptors = parseSubDescriptors(descriptorData);
return {
bLength: data.getUint8(0),
bDescriptorType: data.getUint8(1),
wTotalLength: data.getUint16(2, true),
bNumInterfaces: data.getUint8(4),
bConfigurationValue: data.getUint8(5),
iConfiguration: data.getUint8(6),
bmAttributes: data.getUint8(7),
bMaxPower: data.getUint8(8),
descriptors: descriptors,
};
}