-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathFastboot.c
476 lines (402 loc) · 10 KB
/
Fastboot.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
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
#include "EFIDroidUi.h"
#define STATE_OFFLINE 0
#define STATE_COMMAND 1
#define STATE_COMPLETE 2
#define STATE_ERROR 3
#define STATE_STOP 4
#define STATE_STOPPED 5
typedef struct _FASTBOOT_COMMAND FASTBOOT_COMMAND;
struct _FASTBOOT_COMMAND {
struct _FASTBOOT_COMMAND *Next;
CONST CHAR8 *Prefix;
UINT32 PrefixLen;
VOID (*Handle)(CHAR8 *Arg, VOID *Data, UINT32 Size);
};
typedef struct _FASTBOOT_VAR FASTBOOT_VAR;
struct _FASTBOOT_VAR {
struct _FASTBOOT_VAR *Next;
CONST CHAR8 *Name;
CONST CHAR8 *Value;
};
STATIC EFI_EVENT mExitBootServicesEvent;
STATIC EFI_EVENT mUsbOnlineEvent;
STATIC lkapi_usbgadget_iface_t* mUsbInterface = NULL;
STATIC UINT32 mFastbootState = STATE_OFFLINE;
STATIC VOID *DownloadBase = NULL;
STATIC UINT32 DownloadSize = 0;
STATIC UINTN DownloadPages = 0;
STATIC FASTBOOT_COMMAND *CommandList;
STATIC FASTBOOT_VAR *VariableList;
STATIC VOID
FastbootNotify (
lkapi_udc_gadget_t *Gadget,
UINT32 Event
)
{
if (Event == LKAPI_UDC_EVENT_ONLINE) {
gBS->SignalEvent (mUsbOnlineEvent);
}
}
STATIC lkapi_udc_device_t surf_udc_device = {
.vendor_id = 0x18d1,
.product_id = 0xD00D,
.version_id = 0x0100,
.manufacturer = "Google",
.product = "Android",
};
STATIC lkapi_udc_gadget_t fastboot_gadget = {
.notify = FastbootNotify,
.ifc_class = 0xff,
.ifc_subclass = 0x42,
.ifc_protocol = 0x03,
.ifc_endpoints = 2,
.ifc_string = "fastboot",
};
STATIC VOID
FastbootAck (
CONST CHAR8 *Code,
CONST CHAR8 *Reason,
BOOLEAN ChangeState
)
{
STACKBUF_DMA_ALIGN(Response, FASTBOOT_COMMAND_MAX_LENGTH);
if (mFastbootState != STATE_COMMAND)
return;
if (Reason == 0)
Reason = "";
AsciiSPrint((CHAR8*)Response, FASTBOOT_COMMAND_MAX_LENGTH, "%a%a", Code, Reason);
if(fastboot_gadget.usb_write(&fastboot_gadget, Response, AsciiStrLen((CHAR8*)Response))<0)
mFastbootState = STATE_ERROR;
else if(ChangeState)
mFastbootState = STATE_COMPLETE;
}
VOID
FastbootInfo (
CONST CHAR8 *Reason
)
{
FastbootAck("INFO", Reason, FALSE);
}
VOID
FastbootFail (
CONST CHAR8 *Reason
)
{
FastbootAck("FAIL", Reason, TRUE);
}
VOID
FastbootOkay (
CONST CHAR8 *Info
)
{
FastbootAck("OKAY", Info, TRUE);
}
VOID
FastbootRegister (
CHAR8 *Prefix,
VOID (*Handle)(CHAR8 *Arg, VOID *Data, UINT32 Size)
)
{
FASTBOOT_COMMAND *Command;
Command = AllocatePool(sizeof(*Command));
if (Command) {
Command->Prefix = Prefix;
Command->PrefixLen = AsciiStrLen(Prefix);
Command->Handle = Handle;
Command->Next = CommandList;
CommandList = Command;
}
}
VOID
FastbootPublish (
CONST CHAR8 *Name,
CONST CHAR8 *Value
)
{
FASTBOOT_VAR *Variable;
Variable = AllocatePool(sizeof(*Variable));
if (Variable) {
Variable->Name = Name;
Variable->Value = Value;
Variable->Next = VariableList;
VariableList = Variable;
}
}
STATIC VOID
CommandGetVar (
CHAR8 *Arg,
VOID *Data,
UINT32 Size
)
{
FASTBOOT_VAR *Variable;
BOOLEAN All = FALSE;
CHAR8 Response[128];
All = !AsciiStrCmp("all", Arg);
for (Variable = VariableList; Variable; Variable = Variable->Next) {
if (All) {
AsciiSPrint(Response, sizeof(Response), "\t%a: [%a]", Variable->Name, Variable->Value);
FastbootInfo(Response);
}
else if (!AsciiStrCmp(Variable->Name, Arg)) {
FastbootOkay(Variable->Value);
return;
}
}
FastbootOkay("");
}
STATIC VOID
CommandHelp (
CHAR8 *Arg,
VOID *Data,
UINT32 Size
)
{
FASTBOOT_COMMAND *Command;
CHAR8 Response[128];
// print commands
FastbootInfo("commands:");
for (Command = CommandList; Command; Command = Command->Next) {
AsciiSPrint(Response, sizeof(Response), "\t%a", Command->Prefix);
FastbootInfo(Response);
}
FastbootOkay("");
}
/* todo: give lk strtoul and nuke this */
STATIC UINT32
HexToUnsigned (
CONST CHAR8 *x
)
{
UINT32 n = 0;
while(*x) {
switch(*x) {
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
n = (n << 4) | (*x - '0');
break;
case 'a': case 'b': case 'c':
case 'd': case 'e': case 'f':
n = (n << 4) | (*x - 'a' + 10);
break;
case 'A': case 'B': case 'C':
case 'D': case 'E': case 'F':
n = (n << 4) | (*x - 'A' + 10);
break;
default:
return n;
}
x++;
}
return n;
}
STATIC VOID
CommandDownload (
CHAR8 *Arg,
VOID *Data,
UINT32 Size
)
{
CHAR8 Response[FASTBOOT_COMMAND_MAX_LENGTH];
UINT32 Length = HexToUnsigned(Arg);
INT32 r;
// free old data
if(DownloadBase) {
FreeAlignedPages(DownloadBase, DownloadPages);
DownloadBase = NULL;
DownloadSize = 0;
DownloadPages = 0;
}
// allocate data buffer
DownloadSize = 0;
DownloadPages = ROUNDUP(Length, EFI_PAGE_SIZE)/EFI_PAGE_SIZE;
DownloadBase = AllocateAlignedPages(DownloadPages, EFI_PAGE_SIZE);
if(DownloadBase == NULL) {
FastbootFail("data too large");
return;
}
// write response
AsciiSPrint(Response, FASTBOOT_COMMAND_MAX_LENGTH, "DATA%08x", Length);
if(fastboot_gadget.usb_write(&fastboot_gadget, Response, AsciiStrLen(Response))<0) {
mFastbootState = STATE_ERROR;
return;
}
// Discard the cache contents before starting the download
InvalidateDataCacheRange(DownloadBase, Length);
// read data
r = fastboot_gadget.usb_read(&fastboot_gadget, DownloadBase, Length);
if ((r < 0) || ((UINT32) r != Length)) {
mFastbootState = STATE_ERROR;
return;
}
// set size and send OKAY
DownloadSize = Length;
FastbootOkay("");
}
STATIC
VOID
FastbootCommandLoop (
VOID
)
{
INT32 r;
FASTBOOT_COMMAND *Command;
DEBUG((EFI_D_INFO, "fastboot: processing commands\n"));
UINT8* Buffer = AllocateAlignedPages(ArmDataCacheLineLength(), ROUNDUP(4096, ArmDataCacheLineLength()));
ASSERT(Buffer);
AGAIN:
while (mFastbootState != STATE_ERROR) {
SetMem(Buffer, FASTBOOT_COMMAND_MAX_LENGTH, 0);
InvalidateDataCacheRange(Buffer, FASTBOOT_COMMAND_MAX_LENGTH);
MenuShowProgressDialog("Waiting for commands", FALSE);
r = fastboot_gadget.usb_read(&fastboot_gadget, Buffer, FASTBOOT_COMMAND_MAX_LENGTH);
if (r < 0) break;
// Commands aren't null-terminated. Let's get a null-terminated version.
Buffer[r] = 0;
mFastbootState = STATE_COMMAND;
for (Command = CommandList; Command; Command = Command->Next) {
UINTN CmdLen = AsciiStrLen((CHAR8*)Buffer);
if (CompareMem(Buffer, Command->Prefix, Command->PrefixLen))
continue;
if (CmdLen>Command->PrefixLen && Buffer[Command->PrefixLen]!=' ' && Buffer[Command->PrefixLen-1]!=':')
continue;
CHAR8* Arg = (CHAR8*) Buffer + Command->PrefixLen;
if(Arg[0]==' ')
Arg++;
Command->Handle(Arg, DownloadBase, DownloadSize);
if (mFastbootState == STATE_STOP)
goto STOP;
if (mFastbootState == STATE_COMMAND)
FastbootFail("unknown reason");
RenderActiveMenu();
MenuDrawDarkBackground();
goto AGAIN;
}
FastbootInfo("unknown command");
FastbootInfo("See 'fastboot oem help'");
FastbootFail("");
}
STOP:
if (mFastbootState!=STATE_STOP && mFastbootState!=STATE_STOPPED) {
mFastbootState = STATE_OFFLINE;
DEBUG((EFI_D_ERROR, "fastboot: oops!\n"));
}
FreeAlignedPages(Buffer, 1);
if (DownloadBase!=NULL) {
FreeAlignedPages(DownloadBase, DownloadPages);
DownloadBase = NULL;
DownloadSize = 0;
DownloadPages = 0;
}
}
STATIC
VOID
FastbootHandler (
VOID
)
{
for (;;) {
UINTN EventIndex;
MenuShowProgressDialog("Connect USB Cable", FALSE);
gBS->WaitForEvent (1, &mUsbOnlineEvent, &EventIndex);
FastbootCommandLoop();
if (mFastbootState==STATE_STOP || mFastbootState==STATE_STOPPED)
break;
}
}
STATIC
VOID
EFIAPI
ExitBootServicesEvent (
IN EFI_EVENT Event,
IN VOID *Context
)
{
mUsbInterface->udc_stop(mUsbInterface);
}
VOID
FastbootInit (
VOID
)
{
EFI_STATUS Status;
INT32 r;
MenuShowProgressDialog("Starting Fastboot", TRUE);
mFastbootState = STATE_OFFLINE;
Status = gBS->CreateEvent (0, TPL_CALLBACK, NULL, NULL, &mUsbOnlineEvent);
ASSERT_EFI_ERROR (Status);
mUsbInterface = mLKApi->usbgadget_get_interface();
ASSERT(mUsbInterface);
FastbootRegister("oem help", CommandHelp);
FastbootRegister("getvar:", CommandGetVar);
FastbootRegister("download:", CommandDownload);
FastbootPublish("version", "0.5");
// we use dynamic allocations, so report the highest possible value
FastbootPublish("max-download-size", "0xffffffff");
surf_udc_device.serialno = AsciiStrDup("EFIDroid");
r = mUsbInterface->udc_init(mUsbInterface, &surf_udc_device);
ASSERT(r==0);
r= mUsbInterface->udc_register_gadget(mUsbInterface, &fastboot_gadget);
ASSERT(r==0);
r = mUsbInterface->udc_start(mUsbInterface);
ASSERT(r==0);
Status = gBS->CreateEvent (EVT_SIGNAL_EXIT_BOOT_SERVICES, TPL_NOTIFY, ExitBootServicesEvent, NULL, &mExitBootServicesEvent);
ASSERT_EFI_ERROR (Status);
FastbootHandler();
if (mFastbootState!=STATE_STOPPED) {
Status = gBS->CloseEvent(mExitBootServicesEvent);
mUsbInterface->udc_stop(mUsbInterface);
}
}
VOID
FastbootRequestStop (
VOID
)
{
mFastbootState = STATE_STOP;
}
VOID
FastbootStopNow (
VOID
)
{
gBS->CloseEvent(mExitBootServicesEvent);
mUsbInterface->udc_stop(mUsbInterface);
mFastbootState = STATE_STOPPED;
}
VOID
FastbootSendString (
IN CONST CHAR8 *Data,
IN UINTN Size
)
{
UINTN Index;
CHAR8 Buffer[FASTBOOT_COMMAND_MAX_LENGTH];
UINT8* Data8 = (UINT8*)Data;
for (Index=0; Index<Size; Index+=FASTBOOT_COMMAND_MAX_LENGTH-5) {
UINTN CopySize = MIN(Size-Index, FASTBOOT_COMMAND_MAX_LENGTH-5);
CopyMem(Buffer, &Data8[Index], CopySize);
Buffer[CopySize] = 0;
FastbootInfo(Buffer);
}
}
VOID
FastbootSendBuf (
IN CONST VOID *Data,
IN UINTN Size
)
{
UINTN B64DataSize = BASE64_ENCODED_SIZE(Size);
CHAR8* B64Data = AllocatePool(B64DataSize);
if (B64Data==NULL) {
FastbootFail("out of memory");
return;
}
INT32 Ret = UtilBase64Encode (Data, Size, B64Data, B64DataSize);
if (Ret<=0) {
FastbootFail("encoding error");
return;
}
FastbootSendString(B64Data, Ret);
FreePool(B64Data);
}