-
Notifications
You must be signed in to change notification settings - Fork 4
/
onekey_usb_bootloader_mode.hpp
449 lines (395 loc) · 19.9 KB
/
onekey_usb_bootloader_mode.hpp
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
#ifndef ONEKEY_USB_BOOTLOADER_MODE_HPP
#define ONEKEY_USB_BOOTLOADER_MODE_HPP
#include "onekey_protocol.hpp"
#include "onekey_usb_common.hpp"
#include "onekey_usb_configfs.hpp"
#include "onekey_usb_functionfs.hpp"
#define RAC_ON_FALSE_BOOTLOADER \
{ \
RAC_ON_FALSE \
return false; \
}
namespace onekey
{
namespace usb
{
namespace bootloader
{
// configfs
static const struct usbg_gadget_attrs g_attrs = {
.bcdUSB = 0x0210,
.bDeviceClass = 0x00, // USB_CLASS_PER_INTERFACE
.bDeviceSubClass = 0x00,
.bDeviceProtocol = 0x00,
.bMaxPacketSize0 = 64, /* Max allowed ep0 packet size */
.idVendor = 0x1209,
.idProduct = 0x53C0,
.bcdDevice = 0x0200, /* Verson of device */
};
static const struct usbg_gadget_strs g_strs = {
.manufacturer = (char*)"OneKey", /* Manufacturer */
.product = (char*)"OneKey", /* Product string */
.serial = (char*)"000000000000000000000000", /* Serial number */
};
static const struct usbg_config_strs c_strs = {
.configuration = (char*)"OneKeyUSB",
};
static const struct usbg_config_attrs c_attrs = {
.bmAttributes = 0x80, // bus powered
.bMaxPower = CONFIG_DESC_MAXPOWER_mA(500), // 500ma
};
// functionfs
static const struct functionfs::func_desc fs_descriptors = {
.intf =
{
.bLength = sizeof(fs_descriptors.intf),
.bDescriptorType = USB_DT_INTERFACE,
.bInterfaceNumber = 0,
.bNumEndpoints = 2,
.bInterfaceClass = USB_CLASS_VENDOR_SPEC,
.bInterfaceSubClass = 0,
.bInterfaceProtocol = 0,
.iInterface = 1, /* first string from the provided table */
},
.source =
{
.bLength = sizeof(fs_descriptors.source),
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = 1 | USB_DIR_IN,
.bmAttributes = USB_ENDPOINT_XFER_INT,
.wMaxPacketSize = cpu_to_le16(functionfs::MAX_PACKET_SIZE_FS),
.bInterval = 1,
},
.sink =
{
.bLength = sizeof(fs_descriptors.sink),
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = 1 | USB_DIR_OUT,
.bmAttributes = USB_ENDPOINT_XFER_INT,
.wMaxPacketSize = cpu_to_le16(functionfs::MAX_PACKET_SIZE_FS),
.bInterval = 1,
},
};
static const struct functionfs::func_desc hs_descriptors = {
.intf =
{
.bLength = sizeof(hs_descriptors.intf),
.bDescriptorType = USB_DT_INTERFACE,
.bInterfaceNumber = 0,
.bNumEndpoints = 2,
.bInterfaceClass = USB_CLASS_VENDOR_SPEC,
.bInterfaceSubClass = 0,
.bInterfaceProtocol = 0,
.iInterface = 1, /* first string from the provided table */
},
.source =
{
.bLength = sizeof(hs_descriptors.source),
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = 1 | USB_DIR_IN,
.bmAttributes = USB_ENDPOINT_XFER_INT,
.wMaxPacketSize = cpu_to_le16(functionfs::MAX_PACKET_SIZE_FS),
// .wMaxPacketSize = cpu_to_le16(functionfs::MAX_PACKET_SIZE_HS),
.bInterval = 1,
},
.sink =
{
.bLength = sizeof(hs_descriptors.sink),
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = 1 | USB_DIR_OUT,
.bmAttributes = USB_ENDPOINT_XFER_INT,
.wMaxPacketSize = cpu_to_le16(functionfs::MAX_PACKET_SIZE_FS),
// .wMaxPacketSize = cpu_to_le16(functionfs::MAX_PACKET_SIZE_HS),
.bInterval = 1,
},
};
static const struct functionfs::ss_func_desc ss_descriptors = {
.intf =
{
.bLength = sizeof(ss_descriptors.intf),
.bDescriptorType = USB_DT_INTERFACE,
.bInterfaceNumber = 0,
.bNumEndpoints = 2,
.bInterfaceClass = USB_CLASS_VENDOR_SPEC,
.bInterfaceSubClass = 0,
.bInterfaceProtocol = 0,
.iInterface = 1, /* first string from the provided table */
},
.source =
{
.bLength = sizeof(ss_descriptors.source),
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = 1 | USB_DIR_OUT,
.bmAttributes = USB_ENDPOINT_XFER_BULK,
.wMaxPacketSize = cpu_to_le16(functionfs::MAX_PACKET_SIZE_SS),
},
.source_comp =
{
.bLength = sizeof(ss_descriptors.source_comp),
.bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
.bMaxBurst = 4,
},
.sink =
{
.bLength = sizeof(ss_descriptors.sink),
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = 2 | USB_DIR_IN,
.bmAttributes = USB_ENDPOINT_XFER_BULK,
.wMaxPacketSize = cpu_to_le16(functionfs::MAX_PACKET_SIZE_SS),
},
.sink_comp =
{
.bLength = sizeof(ss_descriptors.sink_comp),
.bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
.bMaxBurst = 4,
},
};
static const struct usb_ext_compat_desc os_desc_compat = {
.bFirstInterfaceNumber = 0,
.Reserved1 = cpu_to_le32(1),
.CompatibleID = {0},
.SubCompatibleID = {0},
.Reserved2 = {0},
};
static const struct usb_os_desc_header os_desc_header = {
.interface = cpu_to_le32(1),
.dwLength = cpu_to_le32(sizeof(os_desc_header) + sizeof(os_desc_compat)),
.bcdVersion = cpu_to_le32(1),
.wIndex = cpu_to_le32(4),
.bCount = cpu_to_le32(1),
.Reserved = cpu_to_le32(0),
};
static const struct functionfs::ffs_strings strings = {
.header =
{
.magic = cpu_to_le32(FUNCTIONFS_STRINGS_MAGIC),
.length = cpu_to_le32(sizeof(strings)),
.str_count = cpu_to_le32(1),
.lang_count = cpu_to_le32(1),
},
.lang1 =
{
.code = cpu_to_le16(0x0409), /* en-us */
.str1 = INTERFACE_STR,
},
};
// class
class BOOTLOADER
{
public:
// instances
configfs_handle cfs_h;
functionfs::handler ffs_h;
protocol::parser parser_h;
protocol::composer composer_h;
time_t usb_timeout_ms = 1000;
BOOTLOADER()
{
}
~BOOTLOADER()
{
}
bool init()
{
AUTOLOG_FUNC_ONLY;
cfs_h.g_attrs = g_attrs;
cfs_h.g_strs = g_strs;
cfs_h.c_attrs = c_attrs;
cfs_h.c_strs = c_strs;
ffs_h.fs_descriptors = fs_descriptors;
ffs_h.hs_descriptors = hs_descriptors;
// ffs_h.ss_descriptors = ss_descriptors;
ffs_h.os_desc_compat = os_desc_compat;
ffs_h.os_desc_header = os_desc_header;
ffs_h.strings = strings;
ffs_h.aio_set_timeout(8000);
RAC_ADVANCE(cfs_h.init(), (RAC_ret == true), RAC_ON_FALSE_BOOTLOADER);
RAC_ADVANCE(ffs_h.init(), (RAC_ret == true), RAC_ON_FALSE_BOOTLOADER);
RAC_ADVANCE(cfs_h.enable(), (RAC_ret == true), RAC_ON_FALSE_BOOTLOADER);
return true;
}
bool cleanup()
{
AUTOLOG_FUNC_ONLY;
RAC_ADVANCE(cfs_h.disable(), (RAC_ret == true), RAC_ON_FALSE_BOOTLOADER);
RAC_ADVANCE(ffs_h.cleanup(), (RAC_ret == true), RAC_ON_FALSE_BOOTLOADER);
RAC_ADVANCE(cfs_h.cleanup(), (RAC_ret == true), RAC_ON_FALSE_BOOTLOADER);
return true;
}
// thread main
void usb_service_routine(std::atomic_bool &run_control, std::atomic_bool &run_status)
{
AUTOLOG_FUNC_ONLY;
// buffer
std::array<uint8_t, sizeof(usb_functionfs_event)> buf_ctrl;
std::vector<uint8_t> buf_write_host, buf_read_host;
buf_write_host.resize(functionfs::USB_FFS_MAX_TRANSFER_SIZE);
buf_read_host.resize(functionfs::USB_FFS_MAX_TRANSFER_SIZE);
int32_t res = -1;
if ( !run_control )
{
bzh_utils::log::StdLog::Error("service_routine called when run_control is false!");
return;
}
struct usb_functionfs_event* p_event =
reinterpret_cast<struct usb_functionfs_event*>(buf_ctrl.data());
while ( run_control )
{
if ( !init() )
{
run_control = false;
break;
}
int wait_result = -1;
uint64_t eventfd_result = -1;
while ( run_control )
{
// set status
run_status = true;
// wait for control or eventfd have event
bzh_utils::log::StdLog::Debug("wait_read_management_ready");
wait_result = ffs_h.wait_read_management_ready(usb_timeout_ms);
if ( wait_result == 0 )
{
bzh_utils::log::StdLog::Debug("wait_read_management_ready timeout");
continue;
}
if ( wait_result < 0 )
{
bzh_utils::log::StdLog::Debug("wait_read_management_ready error");
run_control = false;
break;
}
bzh_utils::log::StdLog::Debug(
"wait_read_management_ready end -> wait_result = " +
std::to_string(wait_result)
);
// pull control
bzh_utils::log::StdLog::Debug("is_read_ctrl_ready");
if ( ffs_h.is_read_ctrl_ready() )
{
bzh_utils::log::StdLog::Debug("is_read_ctrl_ready true");
RAC_ADVANCE(
ffs_h.read_ctrl(p_event), (RAC_ret == true),
{
RAC_ON_FALSE
run_control = false;
break;
}
);
ffs_h.process_control_event(p_event);
}
if ( !ffs_h.ffs_enabled )
{
bzh_utils::log::StdLog::Debug("ffs not enabled yet");
std::this_thread::sleep_for(500ms);
continue;
}
// check if we got event
bzh_utils::log::StdLog::Debug("is_read_eventfd_ready");
if ( ffs_h.is_read_eventfd_ready() )
{
bzh_utils::log::StdLog::Debug("is_read_eventfd_ready true");
// read to clear the event
RAC_ADVANCE(ffs_h.read_eventfd(&eventfd_result), (RAC_ret), {
RAC_ON_FALSE
run_control = false;
return;
});
bzh_utils::log::StdLog::Debug("eventfd_result -> ", std::to_string(eventfd_result));
// pull host
res = -1;
RAC_ADVANCE(ffs_h.read_host(buf_read_host, res), (RAC_ret), {
RAC_ON_FALSE
continue;
});
// process request
bzh_utils::log::StdLog::Debug("==== PACKET DUMP buf_read_host ====");
bzh_utils::container::print_stl_container(buf_read_host);
bzh_utils::log::StdLog::Debug("==== PACKET PARSE ====");
RAC_SIMPLE(parser_h.parse_legacy(buf_read_host), (RAC_ret));
protocol::message_parsed msg_parsed = parser_h.get_parsed();
bzh_utils::log::StdLog::Debug(
"msg_id -> ",
std::string(MessageType_Name(static_cast<MessageType>(msg_parsed.id)))
);
bzh_utils::log::StdLog::Debug("msg_size -> ", std::to_string(msg_parsed.size));
bzh_utils::log::StdLog::Debug("msg_protobuf_data -> ");
bzh_utils::container::print_stl_container(msg_parsed.protobuf_data);
bzh_utils::log::StdLog::Debug("==== Create Features ====");
Features feature;
feature.set_vendor("onekey.so");
feature.set_major_version(4);
feature.set_minor_version(0);
feature.set_patch_version(0);
feature.set_bootloader_mode(true);
feature.set_model("T");
feature.set_firmware_present(false);
feature.set_ble_name("FFFFF");
feature.set_ble_ver("1.2.3");
feature.set_ble_enable(true);
feature.set_se_ver("1.0.0");
feature.set_serial_no("TC01WBD202210170824500000067");
bzh_utils::log::StdLog::Debug("==== Get Serialized Features ====");
std::string feature_serialized_string = feature.SerializeAsString();
std::vector<uint8_t> feature_serialized = std::vector<uint8_t>(
feature_serialized_string.begin(), feature_serialized_string.end()
);
bzh_utils::log::StdLog::Debug(
"feature_serialized_string.size() -> ",
std::to_string(feature_serialized_string.size())
);
bzh_utils::log::StdLog::Debug("==== PACKET DUMP Features ====");
bzh_utils::container::print_stl_container(feature_serialized);
bzh_utils::log::StdLog::Debug(
"feature_serialized.size() -> ", std::to_string(feature_serialized.size())
);
protocol::message_parsed mpsed;
for ( uint8_t element : feature_serialized )
{
mpsed.protobuf_data.push_back(reinterpret_cast<uint8_t>(element));
}
mpsed.size = mpsed.protobuf_data.size();
mpsed.id = static_cast<uint16_t>(MessageType_Features);
bzh_utils::log::StdLog::Debug(
"mpsed.protobuf_data.size() -> ",
std::to_string(mpsed.protobuf_data.size())
);
bzh_utils::log::StdLog::Debug("mpsed.size -> ", std::to_string(mpsed.size));
RAC_SIMPLE(composer_h.compose_legacy(mpsed), (RAC_ret));
std::vector<protocol::message_composed> mcomposed_list =
composer_h.get_composed();
bzh_utils::log::StdLog::Debug(
"mcomposed_list.size() -> ", std::to_string(mcomposed_list.size())
);
// put host
for ( auto mcomposed : mcomposed_list )
{
bzh_utils::log::StdLog::Debug("mcomposed.size() -> ", std::to_string(mcomposed.size()));
bzh_utils::log::StdLog::Debug("==== PACKET DUMP buf_write_host ====");
bzh_utils::container::print_stl_container(mcomposed);
ffs_h.write_host(mcomposed);
}
// ffs_h.write_host(buf_write_host);
}
// if not, we continue
else
{
continue;
}
}
bzh_utils::log::StdLog::Debug("loop out");
if ( !cleanup() )
{
run_control = false;
break;
} // I know it's not needed, just for keep the style
}
run_status = false;
return;
}
};
} // namespace bootloader
} // namespace usb
} // namespace onekey
#endif // ONEKEY_USB_BOOTLOADER_MODE_HPP