]> git.proxmox.com Git - mirror_edk2.git/blob - ArmPlatformPkg/Bds/BootMenu.c
ArmPlatformPkg/Bds: Fixed update of boot entries
[mirror_edk2.git] / ArmPlatformPkg / Bds / BootMenu.c
1 /** @file
2 *
3 * Copyright (c) 2011-2012, ARM Limited. All rights reserved.
4 *
5 * This program and the accompanying materials
6 * are licensed and made available under the terms and conditions of the BSD License
7 * which accompanies this distribution. The full text of the license may be found at
8 * http://opensource.org/licenses/bsd-license.php
9 *
10 * THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12 *
13 **/
14
15 #include "BdsInternal.h"
16
17 extern EFI_HANDLE mImageHandle;
18 extern BDS_LOAD_OPTION_SUPPORT *BdsLoadOptionSupportList;
19
20 EFI_STATUS
21 SelectBootDevice (
22 OUT BDS_SUPPORTED_DEVICE** SupportedBootDevice
23 )
24 {
25 EFI_STATUS Status;
26 LIST_ENTRY SupportedDeviceList;
27 UINTN SupportedDeviceCount;
28 LIST_ENTRY* Entry;
29 UINTN SupportedDeviceSelected;
30 UINTN Index;
31
32 //
33 // List the Boot Devices supported
34 //
35
36 // Start all the drivers first
37 BdsConnectAllDrivers ();
38
39 // List the supported devices
40 Status = BootDeviceListSupportedInit (&SupportedDeviceList);
41 ASSERT_EFI_ERROR(Status);
42
43 SupportedDeviceCount = 0;
44 for (Entry = GetFirstNode (&SupportedDeviceList);
45 !IsNull (&SupportedDeviceList,Entry);
46 Entry = GetNextNode (&SupportedDeviceList,Entry)
47 )
48 {
49 *SupportedBootDevice = SUPPORTED_BOOT_DEVICE_FROM_LINK(Entry);
50 Print(L"[%d] %s\n",SupportedDeviceCount+1,(*SupportedBootDevice)->Description);
51
52 DEBUG_CODE_BEGIN();
53 CHAR16* DevicePathTxt;
54 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL* DevicePathToTextProtocol;
55
56 Status = gBS->LocateProtocol (&gEfiDevicePathToTextProtocolGuid, NULL, (VOID **)&DevicePathToTextProtocol);
57 ASSERT_EFI_ERROR(Status);
58 DevicePathTxt = DevicePathToTextProtocol->ConvertDevicePathToText ((*SupportedBootDevice)->DevicePathProtocol,TRUE,TRUE);
59
60 Print(L"\t- %s\n",DevicePathTxt);
61
62 FreePool(DevicePathTxt);
63 DEBUG_CODE_END();
64
65 SupportedDeviceCount++;
66 }
67
68 if (SupportedDeviceCount == 0) {
69 Print(L"There is no supported device.\n");
70 Status = EFI_ABORTED;
71 goto EXIT;
72 }
73
74 //
75 // Select the Boot Device
76 //
77 SupportedDeviceSelected = 0;
78 while (SupportedDeviceSelected == 0) {
79 Print(L"Select the Boot Device: ");
80 Status = GetHIInputInteger (&SupportedDeviceSelected);
81 if (EFI_ERROR(Status)) {
82 Status = EFI_ABORTED;
83 goto EXIT;
84 } else if ((SupportedDeviceSelected == 0) || (SupportedDeviceSelected > SupportedDeviceCount)) {
85 Print(L"Invalid input (max %d)\n",SupportedDeviceCount);
86 SupportedDeviceSelected = 0;
87 }
88 }
89
90 //
91 // Get the Device Path for the selected boot device
92 //
93 Index = 1;
94 for (Entry = GetFirstNode (&SupportedDeviceList);
95 !IsNull (&SupportedDeviceList,Entry);
96 Entry = GetNextNode (&SupportedDeviceList,Entry)
97 )
98 {
99 if (Index == SupportedDeviceSelected) {
100 *SupportedBootDevice = SUPPORTED_BOOT_DEVICE_FROM_LINK(Entry);
101 break;
102 }
103 Index++;
104 }
105
106 EXIT:
107 BootDeviceListSupportedFree (&SupportedDeviceList, *SupportedBootDevice);
108 return Status;
109 }
110
111 EFI_STATUS
112 BootMenuAddBootOption (
113 IN LIST_ENTRY *BootOptionsList
114 )
115 {
116 EFI_STATUS Status;
117 BDS_SUPPORTED_DEVICE* SupportedBootDevice;
118 ARM_BDS_LOADER_ARGUMENTS* BootArguments;
119 CHAR16 BootDescription[BOOT_DEVICE_DESCRIPTION_MAX];
120 CHAR8 CmdLine[BOOT_DEVICE_OPTION_MAX];
121 UINT32 Attributes;
122 ARM_BDS_LOADER_TYPE BootType;
123 BDS_LOAD_OPTION_ENTRY *BdsLoadOptionEntry;
124 EFI_DEVICE_PATH *DevicePath;
125 EFI_DEVICE_PATH_PROTOCOL *DevicePathNode;
126 EFI_DEVICE_PATH_PROTOCOL *InitrdPathNode;
127 EFI_DEVICE_PATH_PROTOCOL *InitrdPath;
128 UINTN CmdLineSize;
129 BOOLEAN InitrdSupport;
130 UINTN InitrdSize;
131
132 Attributes = 0;
133 SupportedBootDevice = NULL;
134
135 // List the Boot Devices supported
136 Status = SelectBootDevice (&SupportedBootDevice);
137 if (EFI_ERROR(Status)) {
138 Status = EFI_ABORTED;
139 goto EXIT;
140 }
141
142 // Create the specific device path node
143 Status = SupportedBootDevice->Support->CreateDevicePathNode (L"EFI Application or the kernel", &DevicePathNode, &BootType, &Attributes);
144 if (EFI_ERROR(Status)) {
145 Status = EFI_ABORTED;
146 goto EXIT;
147 }
148 // Append the Device Path node to the select device path
149 DevicePath = AppendDevicePathNode (SupportedBootDevice->DevicePathProtocol, (CONST EFI_DEVICE_PATH_PROTOCOL *)DevicePathNode);
150
151 if ((BootType == BDS_LOADER_KERNEL_LINUX_ATAG) || (BootType == BDS_LOADER_KERNEL_LINUX_FDT)) {
152 Print(L"Add an initrd: ");
153 Status = GetHIInputBoolean (&InitrdSupport);
154 if (EFI_ERROR(Status)) {
155 Status = EFI_ABORTED;
156 goto EXIT;
157 }
158
159 if (InitrdSupport) {
160 // Create the specific device path node
161 Status = SupportedBootDevice->Support->CreateDevicePathNode (L"initrd", &InitrdPathNode, NULL, NULL);
162 if (EFI_ERROR(Status) && Status != EFI_NOT_FOUND) { // EFI_NOT_FOUND is returned on empty input string, but we can boot without an initrd
163 Status = EFI_ABORTED;
164 goto EXIT;
165 }
166
167 if (InitrdPathNode != NULL) {
168 // Append the Device Path node to the select device path
169 InitrdPath = AppendDevicePathNode (SupportedBootDevice->DevicePathProtocol, (CONST EFI_DEVICE_PATH_PROTOCOL *)InitrdPathNode);
170 } else {
171 InitrdPath = NULL;
172 }
173 } else {
174 InitrdPath = NULL;
175 }
176
177 Print(L"Arguments to pass to the binary: ");
178 Status = GetHIInputAscii (CmdLine,BOOT_DEVICE_OPTION_MAX);
179 if (EFI_ERROR(Status)) {
180 Status = EFI_ABORTED;
181 goto FREE_DEVICE_PATH;
182 }
183
184 CmdLineSize = AsciiStrSize (CmdLine);
185 InitrdSize = GetDevicePathSize (InitrdPath);
186
187 BootArguments = (ARM_BDS_LOADER_ARGUMENTS*)AllocatePool (sizeof(ARM_BDS_LOADER_ARGUMENTS) + CmdLineSize + InitrdSize);
188
189 BootArguments->LinuxArguments.CmdLineSize = CmdLineSize;
190 BootArguments->LinuxArguments.InitrdSize = InitrdSize;
191 CopyMem ((VOID*)(&BootArguments->LinuxArguments + 1), CmdLine, CmdLineSize);
192 CopyMem ((VOID*)((UINTN)(&BootArguments->LinuxArguments + 1) + CmdLineSize), InitrdPath, InitrdSize);
193 } else {
194 BootArguments = NULL;
195 }
196
197 Print(L"Description for this new Entry: ");
198 Status = GetHIInputStr (BootDescription, BOOT_DEVICE_DESCRIPTION_MAX);
199 if (EFI_ERROR(Status)) {
200 Status = EFI_ABORTED;
201 goto FREE_DEVICE_PATH;
202 }
203
204 // Create new entry
205 BdsLoadOptionEntry = (BDS_LOAD_OPTION_ENTRY*)AllocatePool (sizeof(BDS_LOAD_OPTION_ENTRY));
206 Status = BootOptionCreate (Attributes, BootDescription, DevicePath, BootType, BootArguments, &BdsLoadOptionEntry->BdsLoadOption);
207 if (!EFI_ERROR(Status)) {
208 InsertTailList (BootOptionsList, &BdsLoadOptionEntry->Link);
209 }
210
211 FREE_DEVICE_PATH:
212 FreePool (DevicePath);
213
214
215 EXIT:
216 if (Status == EFI_ABORTED) {
217 Print(L"\n");
218 }
219 FreePool(SupportedBootDevice);
220 return Status;
221 }
222
223 STATIC
224 EFI_STATUS
225 BootMenuSelectBootOption (
226 IN LIST_ENTRY* BootOptionsList,
227 IN CONST CHAR16* InputStatement,
228 IN BOOLEAN OnlyArmBdsBootEntry,
229 OUT BDS_LOAD_OPTION_ENTRY** BdsLoadOptionEntry
230 )
231 {
232 EFI_STATUS Status;
233 LIST_ENTRY* Entry;
234 BDS_LOAD_OPTION* BdsLoadOption;
235 UINTN BootOptionSelected;
236 UINTN BootOptionCount;
237 UINTN Index;
238
239 // Display the list of supported boot devices
240 BootOptionCount = 0;
241 for (Entry = GetFirstNode (BootOptionsList);
242 !IsNull (BootOptionsList,Entry);
243 Entry = GetNextNode (BootOptionsList, Entry)
244 )
245 {
246 BdsLoadOption = LOAD_OPTION_FROM_LINK(Entry);
247
248 if (OnlyArmBdsBootEntry && !IS_ARM_BDS_BOOTENTRY (BdsLoadOption)) {
249 continue;
250 }
251
252 Print (L"[%d] %s\n", (BootOptionCount + 1), BdsLoadOption->Description);
253
254 DEBUG_CODE_BEGIN();
255 CHAR16* DevicePathTxt;
256 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL* DevicePathToTextProtocol;
257 ARM_BDS_LOADER_TYPE LoaderType;
258 ARM_BDS_LOADER_OPTIONAL_DATA* OptionalData;
259
260 Status = gBS->LocateProtocol(&gEfiDevicePathToTextProtocolGuid, NULL, (VOID **)&DevicePathToTextProtocol);
261 ASSERT_EFI_ERROR(Status);
262 DevicePathTxt = DevicePathToTextProtocol->ConvertDevicePathToText(BdsLoadOption->FilePathList,TRUE,TRUE);
263
264 Print(L"\t- %s\n",DevicePathTxt);
265 OptionalData = BdsLoadOption->OptionalData;
266 LoaderType = (ARM_BDS_LOADER_TYPE)ReadUnaligned32 ((CONST UINT32*)&OptionalData->Header.LoaderType);
267 if ((LoaderType == BDS_LOADER_KERNEL_LINUX_ATAG) || (LoaderType == BDS_LOADER_KERNEL_LINUX_FDT)) {
268 Print (L"\t- Arguments: %a\n",&OptionalData->Arguments.LinuxArguments + 1);
269 }
270
271 FreePool(DevicePathTxt);
272 DEBUG_CODE_END();
273
274 BootOptionCount++;
275 }
276
277 // Check if a valid boot option(s) is found
278 if (BootOptionCount == 0) {
279 if (StrCmp (InputStatement, DELETE_BOOT_ENTRY) == 0) {
280 Print (L"Nothing to remove!\n");
281 }else if (StrCmp (InputStatement, UPDATE_BOOT_ENTRY) == 0) {
282 Print (L"Couldn't find valid boot entries\n");
283 } else{
284 Print (L"No supported Boot Entry.\n");
285 }
286
287 return EFI_NOT_FOUND;
288 }
289
290 // Get the index of the boot device to delete
291 BootOptionSelected = 0;
292 while (BootOptionSelected == 0) {
293 Print(InputStatement);
294 Status = GetHIInputInteger (&BootOptionSelected);
295 if (EFI_ERROR(Status)) {
296 return Status;
297 } else if ((BootOptionSelected == 0) || (BootOptionSelected > BootOptionCount)) {
298 Print(L"Invalid input (max %d)\n",BootOptionCount);
299 BootOptionSelected = 0;
300 }
301 }
302
303 // Get the structure of the Boot device to delete
304 Index = 1;
305 for (Entry = GetFirstNode (BootOptionsList);
306 !IsNull (BootOptionsList, Entry);
307 Entry = GetNextNode (BootOptionsList,Entry)
308 )
309 {
310 if (Index == BootOptionSelected) {
311 *BdsLoadOptionEntry = LOAD_OPTION_ENTRY_FROM_LINK(Entry);
312 break;
313 }
314 Index++;
315 }
316
317 return EFI_SUCCESS;
318 }
319
320 EFI_STATUS
321 BootMenuRemoveBootOption (
322 IN LIST_ENTRY *BootOptionsList
323 )
324 {
325 EFI_STATUS Status;
326 BDS_LOAD_OPTION_ENTRY* BootOptionEntry;
327
328 Status = BootMenuSelectBootOption (BootOptionsList, DELETE_BOOT_ENTRY, FALSE, &BootOptionEntry);
329 if (EFI_ERROR(Status)) {
330 return Status;
331 }
332
333 // If the Boot Option was attached to a list remove it
334 if (!IsListEmpty (&BootOptionEntry->Link)) {
335 // Remove the entry from the list
336 RemoveEntryList (&BootOptionEntry->Link);
337 }
338
339 // Delete the BDS Load option structures
340 BootOptionDelete (BootOptionEntry->BdsLoadOption);
341
342 return EFI_SUCCESS;
343 }
344
345 EFI_STATUS
346 BootMenuUpdateBootOption (
347 IN LIST_ENTRY *BootOptionsList
348 )
349 {
350 EFI_STATUS Status;
351 BDS_LOAD_OPTION_ENTRY *BootOptionEntry;
352 BDS_LOAD_OPTION *BootOption;
353 BDS_LOAD_OPTION_SUPPORT* DeviceSupport;
354 ARM_BDS_LOADER_ARGUMENTS* BootArguments;
355 CHAR16 BootDescription[BOOT_DEVICE_DESCRIPTION_MAX];
356 CHAR8 CmdLine[BOOT_DEVICE_OPTION_MAX];
357 EFI_DEVICE_PATH* DevicePath;
358 ARM_BDS_LOADER_TYPE BootType;
359 ARM_BDS_LOADER_OPTIONAL_DATA* OptionalData;
360 ARM_BDS_LINUX_ARGUMENTS* LinuxArguments;
361 EFI_DEVICE_PATH *InitrdPathNode;
362 EFI_DEVICE_PATH *InitrdPath;
363 UINTN InitrdSize;
364 UINTN CmdLineSize;
365 BOOLEAN InitrdSupport;
366
367 Status = BootMenuSelectBootOption (BootOptionsList, UPDATE_BOOT_ENTRY, TRUE, &BootOptionEntry);
368 if (EFI_ERROR(Status)) {
369 return Status;
370 }
371 BootOption = BootOptionEntry->BdsLoadOption;
372
373 // Get the device support for this Boot Option
374 Status = BootDeviceGetDeviceSupport (BootOption->FilePathList, &DeviceSupport);
375 if (EFI_ERROR(Status)) {
376 Print(L"Not possible to retrieve the supported device for the update\n");
377 return EFI_UNSUPPORTED;
378 }
379
380 Status = DeviceSupport->UpdateDevicePathNode (BootOption->FilePathList, L"EFI Application or the kernel", &DevicePath, NULL, NULL);
381 if (EFI_ERROR(Status)) {
382 Status = EFI_ABORTED;
383 goto EXIT;
384 }
385
386 OptionalData = BootOption->OptionalData;
387 BootType = (ARM_BDS_LOADER_TYPE)ReadUnaligned32 ((UINT32 *)(&OptionalData->Header.LoaderType));
388
389 if ((BootType == BDS_LOADER_KERNEL_LINUX_ATAG) || (BootType == BDS_LOADER_KERNEL_LINUX_FDT)) {
390 LinuxArguments = &OptionalData->Arguments.LinuxArguments;
391
392 CmdLineSize = ReadUnaligned16 ((CONST UINT16*)&LinuxArguments->CmdLineSize);
393
394 InitrdSize = ReadUnaligned16 ((CONST UINT16*)&LinuxArguments->InitrdSize);
395 if (InitrdSize > 0) {
396 Print(L"Keep the initrd: ");
397 } else {
398 Print(L"Add an initrd: ");
399 }
400 Status = GetHIInputBoolean (&InitrdSupport);
401 if (EFI_ERROR(Status)) {
402 Status = EFI_ABORTED;
403 goto EXIT;
404 }
405
406 if (InitrdSupport) {
407 if (InitrdSize > 0) {
408 // Case we update the initrd device path
409 Status = DeviceSupport->UpdateDevicePathNode ((EFI_DEVICE_PATH*)((UINTN)(LinuxArguments + 1) + CmdLineSize), L"initrd", &InitrdPath, NULL, NULL);
410 if (EFI_ERROR(Status) && Status != EFI_NOT_FOUND) {// EFI_NOT_FOUND is returned on empty input string, but we can boot without an initrd
411 Status = EFI_ABORTED;
412 goto EXIT;
413 }
414 InitrdSize = GetDevicePathSize (InitrdPath);
415 } else {
416 // Case we create the initrd device path
417
418 Status = DeviceSupport->CreateDevicePathNode (L"initrd", &InitrdPathNode, NULL, NULL);
419 if (EFI_ERROR(Status) && Status != EFI_NOT_FOUND) { // EFI_NOT_FOUND is returned on empty input string, but we can boot without an initrd
420 Status = EFI_ABORTED;
421 goto EXIT;
422 }
423
424 if (InitrdPathNode != NULL) {
425 // Duplicate Linux kernel Device Path
426 DevicePath = DuplicateDevicePath (BootOption->FilePathList);
427 // Replace Linux kernel Node by EndNode
428 SetDevicePathEndNode (GetLastDevicePathNode (DevicePath));
429 // Append the Device Path node to the select device path
430 InitrdPath = AppendDevicePathNode (DevicePath, (CONST EFI_DEVICE_PATH_PROTOCOL *)InitrdPathNode);
431 } else {
432 InitrdPath = NULL;
433 }
434 }
435 } else {
436 InitrdSize = 0;
437 }
438
439 Print(L"Arguments to pass to the binary: ");
440 if (CmdLineSize > 0) {
441 AsciiStrnCpy(CmdLine, (CONST CHAR8*)(LinuxArguments + 1), CmdLineSize);
442 } else {
443 CmdLine[0] = '\0';
444 }
445 Status = EditHIInputAscii (CmdLine, BOOT_DEVICE_OPTION_MAX);
446 if (EFI_ERROR(Status)) {
447 Status = EFI_ABORTED;
448 goto FREE_DEVICE_PATH;
449 }
450
451 CmdLineSize = AsciiStrSize (CmdLine);
452
453 BootArguments = (ARM_BDS_LOADER_ARGUMENTS*)AllocatePool(sizeof(ARM_BDS_LOADER_ARGUMENTS) + CmdLineSize + InitrdSize);
454 BootArguments->LinuxArguments.CmdLineSize = CmdLineSize;
455 BootArguments->LinuxArguments.InitrdSize = InitrdSize;
456 CopyMem (&BootArguments->LinuxArguments + 1, CmdLine, CmdLineSize);
457 CopyMem ((VOID*)((UINTN)(&BootArguments->LinuxArguments + 1) + CmdLineSize), InitrdPath, InitrdSize);
458 } else {
459 BootArguments = NULL;
460 }
461
462 Print(L"Description for this new Entry: ");
463 StrnCpy (BootDescription, BootOption->Description, BOOT_DEVICE_DESCRIPTION_MAX);
464 Status = EditHIInputStr (BootDescription, BOOT_DEVICE_DESCRIPTION_MAX);
465 if (EFI_ERROR(Status)) {
466 Status = EFI_ABORTED;
467 goto FREE_DEVICE_PATH;
468 }
469
470 // Update the entry
471 Status = BootOptionUpdate (BootOption, BootOption->Attributes, BootDescription, DevicePath, BootType, BootArguments);
472
473 FREE_DEVICE_PATH:
474 FreePool (DevicePath);
475
476 EXIT:
477 if (Status == EFI_ABORTED) {
478 Print(L"\n");
479 }
480 return Status;
481 }
482
483 EFI_STATUS
484 UpdateFdtPath (
485 IN LIST_ENTRY *BootOptionsList
486 )
487 {
488 EFI_STATUS Status;
489 UINTN FdtDevicePathSize;
490 BDS_SUPPORTED_DEVICE *SupportedBootDevice;
491 EFI_DEVICE_PATH_PROTOCOL *FdtDevicePathNode;
492 EFI_DEVICE_PATH_PROTOCOL *FdtDevicePath;
493
494 Status = SelectBootDevice (&SupportedBootDevice);
495 if (EFI_ERROR(Status)) {
496 Status = EFI_ABORTED;
497 goto EXIT;
498 }
499
500 // Create the specific device path node
501 Status = SupportedBootDevice->Support->CreateDevicePathNode (L"FDT blob", &FdtDevicePathNode, NULL, NULL);
502 if (EFI_ERROR(Status)) {
503 Status = EFI_ABORTED;
504 goto EXIT;
505 }
506
507 if (FdtDevicePathNode != NULL) {
508 // Append the Device Path node to the select device path
509 FdtDevicePath = AppendDevicePathNode (SupportedBootDevice->DevicePathProtocol, FdtDevicePathNode);
510 FdtDevicePathSize = GetDevicePathSize (FdtDevicePath);
511 Status = gRT->SetVariable ((CHAR16*)L"Fdt", &gEfiGlobalVariableGuid, (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS ), FdtDevicePathSize, FdtDevicePath);
512 ASSERT_EFI_ERROR(Status);
513 } else {
514 gRT->SetVariable ((CHAR16*)L"Fdt", &gEfiGlobalVariableGuid, (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS ), 0, NULL);
515 ASSERT_EFI_ERROR(Status);
516 }
517
518 EXIT:
519 if (Status == EFI_ABORTED) {
520 Print(L"\n");
521 }
522 FreePool(SupportedBootDevice);
523 return Status;
524 }
525
526 struct BOOT_MANAGER_ENTRY {
527 CONST CHAR16* Description;
528 EFI_STATUS (*Callback) (IN LIST_ENTRY *BootOptionsList);
529 } BootManagerEntries[] = {
530 { L"Add Boot Device Entry", BootMenuAddBootOption },
531 { L"Update Boot Device Entry", BootMenuUpdateBootOption },
532 { L"Remove Boot Device Entry", BootMenuRemoveBootOption },
533 { L"Update FDT path", UpdateFdtPath },
534 };
535
536 EFI_STATUS
537 BootMenuManager (
538 IN LIST_ENTRY *BootOptionsList
539 )
540 {
541 UINTN Index;
542 UINTN OptionSelected;
543 UINTN BootManagerEntryCount;
544 EFI_STATUS Status;
545
546 BootManagerEntryCount = sizeof(BootManagerEntries) / sizeof(struct BOOT_MANAGER_ENTRY);
547
548 while (TRUE) {
549 // Display Boot Manager menu
550 for (Index = 0; Index < BootManagerEntryCount; Index++) {
551 Print(L"[%d] %s\n",Index+1,BootManagerEntries[Index]);
552 }
553 Print(L"[%d] Return to main menu\n",Index+1);
554
555 // Select which entry to call
556 Print(L"Choice: ");
557 Status = GetHIInputInteger (&OptionSelected);
558 if (EFI_ERROR(Status) || (OptionSelected == (BootManagerEntryCount+1))) {
559 if (EFI_ERROR(Status)) {
560 Print(L"\n");
561 }
562 return EFI_SUCCESS;
563 } else if ((OptionSelected > 0) && (OptionSelected <= BootManagerEntryCount)) {
564 BootManagerEntries[OptionSelected-1].Callback (BootOptionsList);
565 }
566 }
567 // Should never go here
568 }
569
570 EFI_STATUS
571 BootEBL (
572 IN LIST_ENTRY *BootOptionsList
573 )
574 {
575 EFI_STATUS Status;
576
577 // Start EFI Shell
578 Status = BdsLoadApplication (mImageHandle, L"Ebl", 0, NULL);
579 if (Status == EFI_NOT_FOUND) {
580 Print (L"Error: EFI Application not found.\n");
581 } else if (EFI_ERROR(Status)) {
582 Print (L"Error: Status Code: 0x%X\n",(UINT32)Status);
583 }
584
585 return Status;
586 }
587
588 struct BOOT_MAIN_ENTRY {
589 CONST CHAR16* Description;
590 EFI_STATUS (*Callback) (IN LIST_ENTRY *BootOptionsList);
591 } BootMainEntries[] = {
592 { L"EBL", BootEBL },
593 { L"Boot Manager", BootMenuManager },
594 };
595
596
597 EFI_STATUS
598 BootMenuMain (
599 VOID
600 )
601 {
602 LIST_ENTRY BootOptionsList;
603 UINTN OptionCount;
604 UINTN BootOptionCount;
605 EFI_STATUS Status;
606 LIST_ENTRY* Entry;
607 BDS_LOAD_OPTION* BootOption;
608 UINTN BootOptionSelected;
609 UINTN Index;
610 UINTN BootMainEntryCount;
611
612 BootOption = NULL;
613 BootMainEntryCount = sizeof(BootMainEntries) / sizeof(struct BOOT_MAIN_ENTRY);
614
615 while (TRUE) {
616 // Get Boot#### list
617 BootOptionList (&BootOptionsList);
618
619 OptionCount = 1;
620
621 // Display the Boot options
622 for (Entry = GetFirstNode (&BootOptionsList);
623 !IsNull (&BootOptionsList,Entry);
624 Entry = GetNextNode (&BootOptionsList,Entry)
625 )
626 {
627 BootOption = LOAD_OPTION_FROM_LINK(Entry);
628
629 Print(L"[%d] %s\n", OptionCount, BootOption->Description);
630
631 DEBUG_CODE_BEGIN();
632 CHAR16* DevicePathTxt;
633 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL* DevicePathToTextProtocol;
634 ARM_BDS_LOADER_OPTIONAL_DATA* OptionalData;
635 UINTN CmdLineSize;
636 ARM_BDS_LOADER_TYPE LoaderType;
637
638 Status = gBS->LocateProtocol (&gEfiDevicePathToTextProtocolGuid, NULL, (VOID **)&DevicePathToTextProtocol);
639 if (EFI_ERROR(Status)) {
640 // You must provide an implementation of DevicePathToTextProtocol in your firmware (eg: DevicePathDxe)
641 DEBUG((EFI_D_ERROR,"Error: Bds requires DevicePathToTextProtocol\n"));
642 return Status;
643 }
644 DevicePathTxt = DevicePathToTextProtocol->ConvertDevicePathToText (BootOption->FilePathList, TRUE, TRUE);
645
646 Print(L"\t- %s\n",DevicePathTxt);
647
648 // If it is a supported BootEntry then print its details
649 if (IS_ARM_BDS_BOOTENTRY (BootOption)) {
650 OptionalData = BootOption->OptionalData;
651 LoaderType = (ARM_BDS_LOADER_TYPE)ReadUnaligned32 ((CONST UINT32*)&OptionalData->Header.LoaderType);
652 if ((LoaderType == BDS_LOADER_KERNEL_LINUX_ATAG) || (LoaderType == BDS_LOADER_KERNEL_LINUX_FDT)) {
653 if (ReadUnaligned16 (&OptionalData->Arguments.LinuxArguments.InitrdSize) > 0) {
654 CmdLineSize = ReadUnaligned16 (&OptionalData->Arguments.LinuxArguments.CmdLineSize);
655 DevicePathTxt = DevicePathToTextProtocol->ConvertDevicePathToText (
656 GetAlignedDevicePath ((EFI_DEVICE_PATH*)((UINTN)(&OptionalData->Arguments.LinuxArguments + 1) + CmdLineSize)), TRUE, TRUE);
657 Print(L"\t- Initrd: %s\n", DevicePathTxt);
658 }
659 Print(L"\t- Arguments: %a\n", (&OptionalData->Arguments.LinuxArguments + 1));
660 }
661 Print(L"\t- LoaderType: %d\n", LoaderType);
662 }
663 FreePool(DevicePathTxt);
664 DEBUG_CODE_END();
665
666 OptionCount++;
667 }
668 BootOptionCount = OptionCount-1;
669
670 // Display the hardcoded Boot entries
671 for (Index = 0; Index < BootMainEntryCount; Index++) {
672 Print(L"[%d] %s\n",OptionCount,BootMainEntries[Index]);
673 OptionCount++;
674 }
675
676 // Request the boot entry from the user
677 BootOptionSelected = 0;
678 while (BootOptionSelected == 0) {
679 Print(L"Start: ");
680 Status = GetHIInputInteger (&BootOptionSelected);
681 if (EFI_ERROR(Status) || (BootOptionSelected == 0) || (BootOptionSelected > OptionCount)) {
682 Print(L"Invalid input (max %d)\n",(OptionCount-1));
683 BootOptionSelected = 0;
684 }
685 }
686
687 // Start the selected entry
688 if (BootOptionSelected > BootOptionCount) {
689 // Start the hardcoded entry
690 Status = BootMainEntries[BootOptionSelected - BootOptionCount - 1].Callback (&BootOptionsList);
691 } else {
692 // Find the selected entry from the Boot#### list
693 Index = 1;
694 for (Entry = GetFirstNode (&BootOptionsList);
695 !IsNull (&BootOptionsList,Entry);
696 Entry = GetNextNode (&BootOptionsList,Entry)
697 )
698 {
699 if (Index == BootOptionSelected) {
700 BootOption = LOAD_OPTION_FROM_LINK(Entry);
701 break;
702 }
703 Index++;
704 }
705
706 Status = BootOptionStart (BootOption);
707 }
708 }
709 // Should never go here
710 }