forked from pbatard/uefi-ntfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboot.c
596 lines (514 loc) · 20.5 KB
/
boot.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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
/*
* uefi-ntfs: UEFI/NTFS chain loader
* Copyright © 2014-2016 Pete Batard <[email protected]>
* With parts from GRUB © 2006-2015 Free Software Foundation, Inc.
* With parts from rEFInd © 2012-2016 Roderick W. Smith
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <efi.h>
#include <efilib.h>
#include <efistdarg.h>
#define FILE_INFO_SIZE (512 * sizeof(CHAR16))
#define NUM_RETRIES 4
#define DELAY 1 // delay before retry, in seconds
#define PAGE_SIZE 8
#define AUTOBOOT_TIME 9
// Use 'rufus' in the driver path, so that we don't accidentally latch onto a user driver
// We'll need to fix the casing as our target is a case sensitive file system and Microsoft
// indiscriminately seems to uses "EFI\Boot" or "efi\boot"
#if defined(_M_X64) || defined(__x86_64__)
static CHAR16* DriverPath = L"\\efi\\rufus\\ntfs_x64.efi";
static CHAR16* LoaderPath = L"\\efi\\boot\\bootx64.efi";
#else
static CHAR16* DriverPath = L"\\efi\\rufus\\ntfs_ia32.efi";
static CHAR16* LoaderPath = L"\\efi\\boot\\bootia32.efi";
#endif
static CHAR8 NTFSMagic[] = { 'N', 'T', 'F', 'S', ' ', ' ', ' ', ' ' };
EFI_HANDLE g_hImage = NULL;
// Return the device path node right before the end node
static EFI_DEVICE_PATH* GetLastDevicePath(CONST EFI_DEVICE_PATH* dp)
{
EFI_DEVICE_PATH *next, *p;
if (IsDevicePathEnd(dp))
return NULL;
for (p = (EFI_DEVICE_PATH *) dp, next = NextDevicePathNode(p);
!IsDevicePathEnd(next);
p = next, next = NextDevicePathNode(next));
return p;
}
// Get the parent device in an EFI_DEVICE_PATH
// Note: the returned device path is allocated and must be freed
static EFI_DEVICE_PATH* GetParentDevice(CONST EFI_DEVICE_PATH* DevicePath)
{
EFI_DEVICE_PATH *dp, *ldp;
dp = DuplicateDevicePath((EFI_DEVICE_PATH*)DevicePath);
if (dp == NULL)
return NULL;
ldp = GetLastDevicePath(dp);
if (ldp == NULL)
return NULL;
ldp->Type = END_DEVICE_PATH_TYPE;
ldp->SubType = END_ENTIRE_DEVICE_PATH_SUBTYPE;
SetDevicePathNodeLength(ldp, sizeof (*ldp));
return dp;
}
// Compare device paths
static INTN CompareDevicePaths(CONST EFI_DEVICE_PATH *dp1, CONST EFI_DEVICE_PATH *dp2)
{
if (dp1 == NULL || dp2 == NULL)
return -1;
while (1) {
UINT8 type1, type2;
UINT8 subtype1, subtype2;
UINT16 len1, len2;
INTN ret;
type1 = DevicePathType(dp1);
type2 = DevicePathType(dp2);
if (type1 != type2)
return (int) type2 - (int) type1;
subtype1 = DevicePathSubType(dp1);
subtype2 = DevicePathSubType(dp2);
if (subtype1 != subtype2)
return (int) subtype1 - (int) subtype2;
len1 = DevicePathNodeLength(dp1);
len2 = DevicePathNodeLength(dp2);
if (len1 != len2)
return (int) len1 - (int) len2;
ret = CompareMem(dp1, dp2, len1);
if (ret != 0)
return ret;
if (IsDevicePathEnd(dp1))
break;
dp1 = (EFI_DEVICE_PATH*) ((char *)dp1 + len1);
dp2 = (EFI_DEVICE_PATH*) ((char *)dp2 + len2);
}
return 0;
}
// Some UEFI firmwares have a *BROKEN* Unicode collation implementation
// so we must provide our own version of StriCmp for ASCII comparison...
static CHAR16 _tolower(CONST CHAR16 c)
{
if(('A' <= c) && (c <= 'Z'))
return 'a' + (c - 'A');
return c;
}
static INTN _StriCmp(CONST CHAR16 *s1, CONST CHAR16 *s2)
{
while ((*s1 != L'\0') && (_tolower(*s1) == _tolower(*s2)))
s1++, s2++;
return (INTN)(*s1 - *s2);
}
// Fix the case of a path by looking it up on the file system
static EFI_STATUS SetPathCase(CONST EFI_FILE_HANDLE Root, CHAR16* Path)
{
EFI_FILE_HANDLE FileHandle = NULL;
EFI_FILE_INFO* FileInfo;
UINTN i, Len;
UINTN Size;
EFI_STATUS Status;
if ((Root == NULL) || (Path == NULL) || (Path[0] != L'\\'))
return EFI_INVALID_PARAMETER;
FileInfo = (EFI_FILE_INFO*)AllocatePool(FILE_INFO_SIZE);
if (FileInfo == NULL)
return EFI_OUT_OF_RESOURCES;
Len = StrLen(Path);
// Find the last backslash in the path
for (i = Len-1; (i != 0) && (Path[i] != L'\\'); i--);
if (i != 0) {
Path[i] = 0;
// Recursively fix the case
Status = SetPathCase(Root, Path);
if (EFI_ERROR(Status))
goto out;
}
Status = Root->Open(Root, &FileHandle, (i==0)?L"\\":Path, EFI_FILE_MODE_READ, 0);
if (EFI_ERROR(Status))
goto out;
do {
Size = FILE_INFO_SIZE;
Status = FileHandle->Read(FileHandle, &Size, (VOID*)FileInfo);
if (EFI_ERROR(Status))
goto out;
if (_StriCmp(&Path[i+1], FileInfo->FileName) == 0) {
StrCpy(&Path[i+1], FileInfo->FileName);
Status = EFI_SUCCESS;
goto out;
}
Status = EFI_NOT_FOUND;
} while (Size != 0);
out:
Path[i] = L'\\';
if (FileHandle != NULL)
FileHandle->Close(FileHandle);
FreePool((VOID*)FileInfo);
return Status;
}
/* Get the driver name from a driver handle */
static CHAR16* GetDriverName(CONST EFI_HANDLE DriverHandle)
{
CHAR16 *DriverName;
EFI_COMPONENT_NAME *ComponentName;
EFI_COMPONENT_NAME2 *ComponentName2;
// Try EFI_COMPONENT_NAME2 protocol first
if ((BS->OpenProtocol(DriverHandle, &ComponentName2Protocol, (VOID**)&ComponentName2,
g_hImage, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL) == EFI_SUCCESS) &&
(ComponentName2->GetDriverName(ComponentName2, "", &DriverName) == EFI_SUCCESS))
return DriverName;
// Fallback to EFI_COMPONENT_NAME if that didn't work
if ((BS->OpenProtocol(DriverHandle, &ComponentNameProtocol, (VOID**)&ComponentName,
g_hImage, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL) == EFI_SUCCESS) &&
(ComponentName->GetDriverName(ComponentName, "", &DriverName) == EFI_SUCCESS))
return DriverName;
return L"(unknown driver)";
}
/*
* Some UEFI firmwares (like HPQ EFI from HP notebooks) have DiskIo protocols
* opened BY_DRIVER (by Partition driver in HP case) even when no file system
* is produced from this DiskIo. This then blocks our FS driver from connecting
* and producing file systems.
* To fix it we disconnect drivers that connected to DiskIo BY_DRIVER if this
* is a partition volume and if those drivers did not produce file system.
*/
static VOID DisconnectBlockingDrivers(VOID) {
EFI_STATUS Status;
UINTN HandleCount = 0, Index, OpenInfoIndex, OpenInfoCount;
EFI_HANDLE *Handles = NULL;
CHAR16 *DevicePathString;
EFI_FILE_IO_INTERFACE *Volume;
EFI_BLOCK_IO *BlockIo;
EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *OpenInfo;
// Get all DiskIo handles
Status = BS->LocateHandleBuffer(ByProtocol, &DiskIoProtocol, NULL, &HandleCount, &Handles);
if (EFI_ERROR(Status) || (HandleCount == 0))
return;
// Check every DiskIo handle
for (Index = 0; Index < HandleCount; Index++) {
// If this is not partition - skip it.
// This is then whole disk and DiskIo
// should be opened here BY_DRIVER by Partition driver
// to produce partition volumes.
Status = BS->HandleProtocol(Handles[Index], &BlockIoProtocol, (VOID **)&BlockIo);
if (EFI_ERROR(Status))
continue;
if ((BlockIo->Media == NULL) || (!BlockIo->Media->LogicalPartition))
continue;
// If SimpleFileSystem is already produced - skip it, this is ok
Status = BS->HandleProtocol(Handles[Index], &FileSystemProtocol, (VOID **)&Volume);
if (Status == EFI_SUCCESS)
continue;
DevicePathString = DevicePathToStr(DevicePathFromHandle(Handles[Index]));
Print(L"%N\r[ INFO ] Probing %d devices... [%d] %s", HandleCount, Index + 1, DevicePathString);
FreePool(DevicePathString);
// If no SimpleFileSystem on this handle but DiskIo is opened BY_DRIVER
// then disconnect this connection
Status = BS->OpenProtocolInformation(Handles[Index], &DiskIoProtocol, &OpenInfo, &OpenInfoCount);
if (EFI_ERROR(Status)) {
Print(L"%H\n[ WARN ] Could not get DiskIo protocol: %r\n", Status);
FreePool(DevicePathString);
continue;
}
if (OpenInfoCount > 0)
Print(L"%N\n[ INFO ] Disconnecting %d drivers...", OpenInfoCount);
for (OpenInfoIndex = 0; OpenInfoIndex < OpenInfoCount; OpenInfoIndex++) {
if ((OpenInfo[OpenInfoIndex].Attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) == EFI_OPEN_PROTOCOL_BY_DRIVER) {
Print(L"%N\r[ INFO ] Disconnecting %d drivers... [%d] %s", OpenInfoCount, OpenInfoIndex+1, GetDriverName(OpenInfo[OpenInfoIndex].AgentHandle));
Status = BS->DisconnectController(Handles[Index], OpenInfo[OpenInfoIndex].AgentHandle, NULL);
if (EFI_ERROR(Status)) {
Print(L"%H\n[ WARN ] Could not disconnect driver: %r\n", Status);
}
}
}
FreePool(OpenInfo);
}
FreePool(Handles);
}
typedef struct _LINKED_LOADER_PATH_LIST_NODE {
EFI_DEVICE_PATH *ldr;
struct _LINKED_LOADER_PATH_LIST_NODE *next;
} LINKED_LOADER_PATH_LIST_NODE;
VOID ListAppend(LINKED_LOADER_PATH_LIST_NODE **list, EFI_DEVICE_PATH *loader) {
LINKED_LOADER_PATH_LIST_NODE *node = *list;
if (node == NULL) {
node = *list = AllocateZeroPool(sizeof(LINKED_LOADER_PATH_LIST_NODE));
}
else {
while (node->next != NULL) node = node->next;
node->next = AllocateZeroPool(sizeof(LINKED_LOADER_PATH_LIST_NODE));
node = node->next;
}
node->ldr = loader;
}
VOID ListTraverse(LINKED_LOADER_PATH_LIST_NODE **list, VOID (*fun)(EFI_DEVICE_PATH *ldr, UINTN slice, UINTN index, VOID *ctx), UINTN start, UINTN count, VOID *ctx) {
LINKED_LOADER_PATH_LIST_NODE *node = *list;
UINTN index = 0;
while (node != NULL) {
if (index >= start) {
if (!count || (index < (start + count))) {
fun(node->ldr, start, index, ctx);
}
}
node = node->next;
index++;
}
}
VOID ListDestroy(LINKED_LOADER_PATH_LIST_NODE **list) {
LINKED_LOADER_PATH_LIST_NODE *node = *list;
while (node != NULL) {
LINKED_LOADER_PATH_LIST_NODE *prev = node;
node = node->next;
FreePool(prev);
}
*list = NULL;
}
VOID DisplayEntries(EFI_DEVICE_PATH *ldr, UINTN slice, UINTN index, VOID *ctx) {
EFI_DEVICE_PATH *pDevice = GetParentDevice(ldr);
CHAR16 *pszDevice = DevicePathToStr(GetLastDevicePath(pDevice));
Print(L"[%d] %s\n", index - slice + 1, pszDevice);
FreePool(pszDevice);
FreePool(pDevice);
}
VOID DestroyEntries(EFI_DEVICE_PATH *ldr, UINTN slice, UINTN index, VOID *ctx) {
FreePool(ldr);
}
VOID CountEntries(EFI_DEVICE_PATH *ldr, UINTN slice, UINTN index, VOID *ctx) {
(*(UINTN*)ctx)++;
}
VOID ReadEntry(EFI_DEVICE_PATH *ldr, UINTN slice, UINTN index, VOID *ctx) {
*((EFI_DEVICE_PATH**)ctx) = ldr;
}
EFI_STATUS EfiMain(EFI_HANDLE hImage, EFI_SYSTEM_TABLE *pST)
{
EFI_LOADED_IMAGE *pImage;
EFI_STATUS nStatus;
EFI_HANDLE hDriver, *hBlkDevs;
UINTN nBlkDevs;
EFI_DEVICE_PATH *pBootPart, *pBootDisk = NULL;
g_hImage = hImage;
InitializeLib(hImage, pST);
Print(L"%H\n*** UEFI:NTFS multiboot ***");
if (EFI_ERROR((nStatus = BS->OpenProtocol(hImage, &LoadedImageProtocol, &pImage, hImage, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL)))) {
Print(L"%E\nUnable to convert handle to interface: %r\n", nStatus);
goto end;
}
pBootPart = DevicePathFromHandle(pImage->DeviceHandle);
pBootDisk = GetParentDevice(pBootPart);
CHAR16 *pszDev = DevicePathToStr(pBootDisk);
Print(L"%N\nDisk: %s\n", pszDev);
FreePool(pszDev);
Print(L"%H\n[ INFO ] Disconnecting problematic File System drivers\n");
DisconnectBlockingDrivers();
Print(L"%H\n[ WAIT ] Loading NTFS driver");
EFI_DEVICE_PATH *pDrvPath = FileDevicePath(pImage->DeviceHandle, DriverPath);
if (pDrvPath == NULL) {
Print(L"%E\r[ FAIL ] Unable to construct path to NTFS driver\n");
goto end;
}
nStatus = BS->LoadImage(FALSE, hImage, pDrvPath, NULL, 0, &hDriver);
FreePool(pDrvPath);
if (EFI_ERROR(nStatus)) {
Print(L"%E\r[ FAIL ] Unable to load NTFS driver: %r\n", nStatus);
goto end;
}
if (EFI_ERROR((nStatus = BS->StartImage(hDriver, NULL, NULL)))) {
Print(L"%E\r[ FAIL ] Unable to start NTFS driver: %r\n", nStatus);
goto end;
}
Print(L"%H\r[ OK ] NTFS driver loaded and started\n");
LINKED_LOADER_PATH_LIST_NODE *list = NULL;
EFI_DEVICE_PATH *ldr;
if (EFI_ERROR((nStatus = BS->LocateHandleBuffer(ByProtocol, &BlockIoProtocol, NULL, &nBlkDevs, &hBlkDevs)))) {
Print(L"%E\r[ FAIL ] Unable to enumerate block devices: %r\n", nStatus);
goto end;
}
for (UINTN i = 0; i < nBlkDevs; i++) {
EFI_DEVICE_PATH *pDevice = DevicePathFromHandle(hBlkDevs[i]);
pszDev = DevicePathToStr(pDevice);
Print(L"%N\r[ INFO ] Probing %d devices... [%d] %s", nBlkDevs, i + 1, pszDev);
FreePool(pszDev);
if (CompareDevicePaths(pDevice, pBootPart) == 0) continue;
if (CompareDevicePaths(pDevice, pBootDisk) == 0) continue;
EFI_DEVICE_PATH *pDisk = GetParentDevice(pDevice);
_Bool probe = CompareDevicePaths(pDisk, pBootDisk) == 0;
FreePool(pDisk);
#if !defined(_DEBUG)
if (!probe) continue;
#endif
EFI_BLOCK_IO *blkIo;
if (EFI_ERROR((nStatus = BS->OpenProtocol(hBlkDevs[i], &BlockIoProtocol, &blkIo, hImage, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL)))) {
Print(L"%E\n[ WARN ] Unable to open block device, skipping: %r\n", nStatus);
continue;
}
//No media or not a partition.
if ((blkIo->Media == NULL) || (!blkIo->Media->LogicalPartition))
continue;
CHAR8 *buffer = AllocatePool(blkIo->Media->BlockSize);
if (buffer == NULL) {
Print(L"%E\n[ WARN ] Unable to allocate buffer of size %d\n", blkIo->Media->BlockSize);
continue;
}
nStatus = blkIo->ReadBlocks(blkIo, blkIo->Media->MediaId, 0, blkIo->Media->BlockSize, buffer);
_Bool isNTFS = CompareMem(&buffer[3], NTFSMagic, sizeof(NTFSMagic)) == 0;
FreePool(buffer);
if (EFI_ERROR(nStatus)) {
Print(L"%E\n[ WARN ] Unable to read block device, skipping: %r\n", nStatus);
continue;
}
if (!isNTFS) continue;
Print(L"%H\n[ WAIT ] Attaching NTFS driver to device");
EFI_FILE_IO_INTERFACE *fs;
nStatus = BS->OpenProtocol(hBlkDevs[i], &FileSystemProtocol, NULL, hImage, NULL, EFI_OPEN_PROTOCOL_TEST_PROTOCOL);
if (nStatus == EFI_UNSUPPORTED) {
nStatus = BS->ConnectController(hBlkDevs[i], NULL, NULL, TRUE);
}
for (UINTN j = 0; j < NUM_RETRIES; j++) {
if ((!EFI_ERROR((nStatus = BS->OpenProtocol(hBlkDevs[i], &FileSystemProtocol, &fs, hImage, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL))))) {
break;
}
Print(L".");
BS->Stall(DELAY * 1000000);
}
if(EFI_ERROR(nStatus)) {
Print(L"%E\r[ WARN ] Unable to attach NTFS driver, skipping: %r\n", nStatus);
continue;
}
Print(L"%H\r[ OK ] NTFS driver attached to current device\n");
Print(L"%H\r[ WAIT ] Locating EFI boot loader");
EFI_FILE *fsRoot;
if (EFI_ERROR((nStatus = fs->OpenVolume(fs, &fsRoot)))) {
Print(L"%E\r[ WARN ] Unable to open root directory, skipping: %r\n", nStatus);
continue;
}
CHAR16 *loader = StrDuplicate(LoaderPath);
if (EFI_ERROR((nStatus = SetPathCase(fsRoot, loader)))) {
FreePool(loader);
Print(L"%E\r[ WARN ] Unable to locate EFI boot loader on this device: %r\n", nStatus);
continue;
}
Print(L"%H\r[ OK ] EFI boot loader located at %s\n", loader);
ldr = FileDevicePath(hBlkDevs[i], loader);
ListAppend(&list, ldr);
FreePool(loader);
}
UINTN nListEntries = 0, nBootEntry = 0, nPage = 0, nTotalPage = 0;
EFI_INPUT_KEY key;
_Bool interactive = FALSE;
ListTraverse(&list, CountEntries, 0, 0, &nListEntries);
switch (nListEntries) {
case 0:
Print(L"%E\n[ FAIL ] No bootable partition\n", nStatus);
nStatus = EFI_NOT_FOUND;
goto end;
case 1:
goto boot;
default:
nTotalPage = (nListEntries - 1) / PAGE_SIZE + 1;
while (1) {
ST->ConOut->ClearScreen(ST->ConOut);
Print(L"%H*** UEFI:NTFS Multiboot ***\n");
pszDev = DevicePathToStr(pBootDisk);
Print(L"%NDisk: %s\n\n%H", pszDev);
FreePool(pszDev);
ListTraverse(&list, DisplayEntries, nPage * PAGE_SIZE, PAGE_SIZE, NULL);
Print(L"%N\nPage %hd / %hd, %hd entries\n", nPage + 1, nTotalPage, nListEntries);
Print(L"%H\n[F1 - F8] [1 - 8] %N Boot corresponding entry");
Print(L"%H\n[PgUp] [<] [-] %N Previous page");
Print(L"%H\n[PgDn] [>] [+] %N Next page");
if (!interactive) {
INTN nCountDown = AUTOBOOT_TIME;
Print(L"%N\n\n");
while (nCountDown >= 0) {
Print(L"\rWill automatically boot the first entry in %d seconds...", nCountDown);
if (WaitForSingleEvent(ST->ConIn->WaitForKey, 1000 * 1000 * 10) != EFI_TIMEOUT) {
interactive = TRUE;
break;
}
nCountDown--;
}
if (!interactive) {
goto boot;
}
}
else {
WaitForSingleEvent(ST->ConIn->WaitForKey, 0);
}
ST->ConIn->ReadKeyStroke(ST->ConIn, &key);
switch (key.UnicodeChar) {
case L'1':case L'2':case L'3':case L'4':case L'5':case L'6':case L'7':case L'8':
nBootEntry = nPage * PAGE_SIZE + (key.UnicodeChar - L'1');
goto boot;
case L'+':case L'=':case L'>':case L'.':
if ((nPage + 1) != nTotalPage) {
nPage++;
}
break;
case L'-':case L'_': case L'<': case L',':
if (nPage != 0) {
nPage--;
}
break;
default:
switch (key.ScanCode) {
case 0x09:
if (nPage != 0) {
nPage--;
}
break;
case 0x0a:
if ((nPage + 1) != nTotalPage) {
nPage++;
}
break;
case 0x0b:case 0x0c:case 0x0d:case 0x0e:case 0x0f:case 0x10:case 0x11:case 0x12:
nBootEntry = nPage * PAGE_SIZE + (key.ScanCode - 0x0b);
goto boot;
}
}
}
}
boot:
ldr = NULL;
Print(L"%H");
ST->ConOut->ClearScreen(ST->ConOut);
ListTraverse(&list, ReadEntry, nBootEntry, 1, &ldr);
ListTraverse(&list, DisplayEntries, nBootEntry, 1, NULL);
if (ldr == NULL) {
Print(L"%E\n[ FAIL ] No such boot entry\n", nStatus);
nStatus = EFI_NOT_FOUND;
goto end;
}
ldr = DuplicateDevicePath(ldr);
ListTraverse(&list, DestroyEntries, 0, 0, NULL);
ListDestroy(&list);
EFI_HANDLE hChain;
nStatus = BS->LoadImage(FALSE, hImage, ldr, NULL, 0, &hChain);
FreePool(ldr);
if (EFI_ERROR(nStatus)) {
Print(L"%E\n[ FAIL ] Unable to load boot loader: %r\n", nStatus);
goto end;
}
Print(L"%N");
if (EFI_ERROR((nStatus = BS->StartImage(hChain, NULL, NULL)))) {
Print(L"%E\n[ FAIL ] Unable to start boot loader: %r\n", nStatus);
goto end;
}
end:
if (pBootDisk) FreePool(pBootDisk);
if (EFI_ERROR(nStatus)) {
Print(L"Press any key to exit\n");
WaitForSingleEvent(ST->ConIn->WaitForKey, 0);
ST->ConIn->ReadKeyStroke(ST->ConIn, &key);
}
return nStatus;
}