3 * Copyright (c) 2011, ARM Limited. All rights reserved.
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
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.
15 #include "BdsInternal.h"
17 extern EFI_HANDLE mImageHandle
;
18 extern BDS_LOAD_OPTION_SUPPORT
*BdsLoadOptionSupportList
;
22 OUT BDS_SUPPORTED_DEVICE
** SupportedBootDevice
26 LIST_ENTRY SupportedDeviceList
;
27 UINTN SupportedDeviceCount
;
29 UINTN SupportedDeviceSelected
;
33 // List the Boot Devices supported
36 // Start all the drivers first
37 BdsConnectAllDrivers ();
39 // List the supported devices
40 Status
= BootDeviceListSupportedInit (&SupportedDeviceList
);
41 ASSERT_EFI_ERROR(Status
);
43 SupportedDeviceCount
= 0;
44 for (Entry
= GetFirstNode (&SupportedDeviceList
);
45 !IsNull (&SupportedDeviceList
,Entry
);
46 Entry
= GetNextNode (&SupportedDeviceList
,Entry
)
49 *SupportedBootDevice
= SUPPORTED_BOOT_DEVICE_FROM_LINK(Entry
);
50 Print(L
"[%d] %s\n",SupportedDeviceCount
+1,(*SupportedBootDevice
)->Description
);
53 CHAR16
* DevicePathTxt
;
54 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL
* DevicePathToTextProtocol
;
56 Status
= gBS
->LocateProtocol (&gEfiDevicePathToTextProtocolGuid
, NULL
, (VOID
**)&DevicePathToTextProtocol
);
57 ASSERT_EFI_ERROR(Status
);
58 DevicePathTxt
= DevicePathToTextProtocol
->ConvertDevicePathToText ((*SupportedBootDevice
)->DevicePathProtocol
,TRUE
,TRUE
);
60 Print(L
"\t- %s\n",DevicePathTxt
);
62 FreePool(DevicePathTxt
);
65 SupportedDeviceCount
++;
68 if (SupportedDeviceCount
== 0) {
69 Print(L
"There is no supported device.\n");
75 // Select the Boot Device
77 SupportedDeviceSelected
= 0;
78 while (SupportedDeviceSelected
== 0) {
79 Print(L
"Select the Boot Device: ");
80 Status
= GetHIInputInteger (&SupportedDeviceSelected
);
81 if (EFI_ERROR(Status
)) {
84 } else if ((SupportedDeviceSelected
== 0) || (SupportedDeviceSelected
> SupportedDeviceCount
)) {
85 Print(L
"Invalid input (max %d)\n",SupportedDeviceCount
);
86 SupportedDeviceSelected
= 0;
91 // Get the Device Path for the selected boot device
94 for (Entry
= GetFirstNode (&SupportedDeviceList
);
95 !IsNull (&SupportedDeviceList
,Entry
);
96 Entry
= GetNextNode (&SupportedDeviceList
,Entry
)
99 if (Index
== SupportedDeviceSelected
) {
100 *SupportedBootDevice
= SUPPORTED_BOOT_DEVICE_FROM_LINK(Entry
);
107 BootDeviceListSupportedFree (&SupportedDeviceList
, *SupportedBootDevice
);
112 BootMenuAddBootOption (
113 IN LIST_ENTRY
*BootOptionsList
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
];
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
;
132 SupportedBootDevice
= NULL
;
134 // List the Boot Devices supported
135 Status
= SelectBootDevice (&SupportedBootDevice
);
136 if (EFI_ERROR(Status
)) {
137 Status
= EFI_ABORTED
;
141 // Create the specific device path node
142 Print(L
"File path of the EFI Application or the kernel: ");
143 Status
= SupportedBootDevice
->Support
->CreateDevicePathNode (SupportedBootDevice
, &DevicePathNode
, &BootType
, &Attributes
);
144 if (EFI_ERROR(Status
)) {
145 Status
= EFI_ABORTED
;
148 // Append the Device Path node to the select device path
149 DevicePath
= AppendDevicePathNode (SupportedBootDevice
->DevicePathProtocol
, (CONST EFI_DEVICE_PATH_PROTOCOL
*)DevicePathNode
);
151 if ((BootType
== BDS_LOADER_KERNEL_LINUX_ATAG
) || (BootType
== BDS_LOADER_KERNEL_LINUX_FDT
)) {
152 // Create the specific device path node
153 Print(L
"File path of the initrd: ");
154 Status
= SupportedBootDevice
->Support
->CreateDevicePathNode (SupportedBootDevice
, &InitrdPathNode
, NULL
, NULL
);
155 if (EFI_ERROR(Status
) && Status
!= EFI_NOT_FOUND
) { // EFI_NOT_FOUND is returned on empty input string, but we can boot without an initrd
156 Status
= EFI_ABORTED
;
160 if (InitrdPathNode
!= NULL
) {
161 // Append the Device Path node to the select device path
162 InitrdPath
= AppendDevicePathNode (SupportedBootDevice
->DevicePathProtocol
, (CONST EFI_DEVICE_PATH_PROTOCOL
*)InitrdPathNode
);
167 Print(L
"Arguments to pass to the binary: ");
168 Status
= GetHIInputAscii (CmdLine
,BOOT_DEVICE_OPTION_MAX
);
169 if (EFI_ERROR(Status
)) {
170 Status
= EFI_ABORTED
;
171 goto FREE_DEVICE_PATH
;
174 CmdLineSize
= AsciiStrSize (CmdLine
);
175 InitrdSize
= GetDevicePathSize (InitrdPath
);
177 BootArguments
= (ARM_BDS_LOADER_ARGUMENTS
*)AllocatePool (sizeof(ARM_BDS_LOADER_ARGUMENTS
) + CmdLineSize
+ InitrdSize
);
179 BootArguments
->LinuxArguments
.CmdLineSize
= CmdLineSize
;
180 BootArguments
->LinuxArguments
.InitrdSize
= InitrdSize
;
181 CopyMem ((VOID
*)(&BootArguments
->LinuxArguments
+ 1), CmdLine
, CmdLineSize
);
182 CopyMem ((VOID
*)((UINTN
)(&BootArguments
->LinuxArguments
+ 1) + CmdLineSize
), InitrdPath
, InitrdSize
);
184 BootArguments
= NULL
;
187 Print(L
"Description for this new Entry: ");
188 Status
= GetHIInputStr (BootDescription
, BOOT_DEVICE_DESCRIPTION_MAX
);
189 if (EFI_ERROR(Status
)) {
190 Status
= EFI_ABORTED
;
191 goto FREE_DEVICE_PATH
;
195 BdsLoadOptionEntry
= (BDS_LOAD_OPTION_ENTRY
*)AllocatePool (sizeof(BDS_LOAD_OPTION_ENTRY
));
196 Status
= BootOptionCreate (Attributes
, BootDescription
, DevicePath
, BootType
, BootArguments
, &BdsLoadOptionEntry
->BdsLoadOption
);
197 if (!EFI_ERROR(Status
)) {
198 InsertTailList (BootOptionsList
, &BdsLoadOptionEntry
->Link
);
202 FreePool (DevicePath
);
206 if (Status
== EFI_ABORTED
) {
209 FreePool(SupportedBootDevice
);
215 BootMenuSelectBootOption (
216 IN LIST_ENTRY
* BootOptionsList
,
217 IN CONST CHAR16
* InputStatement
,
218 IN BOOLEAN OnlyArmBdsBootEntry
,
219 OUT BDS_LOAD_OPTION_ENTRY
** BdsLoadOptionEntry
224 BDS_LOAD_OPTION
* BdsLoadOption
;
225 UINTN BootOptionSelected
;
226 UINTN BootOptionCount
;
229 // Display the list of supported boot devices
231 for (Entry
= GetFirstNode (BootOptionsList
);
232 !IsNull (BootOptionsList
,Entry
);
233 Entry
= GetNextNode (BootOptionsList
, Entry
)
236 BdsLoadOption
= LOAD_OPTION_FROM_LINK(Entry
);
238 if (OnlyArmBdsBootEntry
&& !IS_ARM_BDS_BOOTENTRY (BdsLoadOption
)) {
242 Print (L
"[%d] %s\n", BootOptionCount
, BdsLoadOption
->Description
);
245 CHAR16
* DevicePathTxt
;
246 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL
* DevicePathToTextProtocol
;
247 ARM_BDS_LOADER_TYPE LoaderType
;
248 ARM_BDS_LOADER_OPTIONAL_DATA
* OptionalData
;
250 Status
= gBS
->LocateProtocol(&gEfiDevicePathToTextProtocolGuid
, NULL
, (VOID
**)&DevicePathToTextProtocol
);
251 ASSERT_EFI_ERROR(Status
);
252 DevicePathTxt
= DevicePathToTextProtocol
->ConvertDevicePathToText(BdsLoadOption
->FilePathList
,TRUE
,TRUE
);
254 Print(L
"\t- %s\n",DevicePathTxt
);
255 OptionalData
= BdsLoadOption
->OptionalData
;
256 LoaderType
= (ARM_BDS_LOADER_TYPE
)ReadUnaligned32 ((CONST UINT32
*)&OptionalData
->Header
.LoaderType
);
257 if ((LoaderType
== BDS_LOADER_KERNEL_LINUX_ATAG
) || (LoaderType
== BDS_LOADER_KERNEL_LINUX_FDT
)) {
258 Print (L
"\t- Arguments: %a\n",&OptionalData
->Arguments
.LinuxArguments
+ 1);
261 FreePool(DevicePathTxt
);
267 if (BootOptionCount
== 0) {
268 Print (L
"No supported Boot Entry.\n");
269 return EFI_NOT_FOUND
;
272 // Get the index of the boot device to delete
273 BootOptionSelected
= 0;
274 while (BootOptionSelected
== 0) {
275 Print(InputStatement
);
276 Status
= GetHIInputInteger (&BootOptionSelected
);
277 if (EFI_ERROR(Status
)) {
279 } else if ((BootOptionSelected
== 0) || (BootOptionSelected
>= BootOptionCount
)) {
280 Print(L
"Invalid input (max %d)\n",BootOptionCount
-1);
281 BootOptionSelected
= 0;
285 // Get the structure of the Boot device to delete
287 for (Entry
= GetFirstNode (BootOptionsList
);
288 !IsNull (BootOptionsList
, Entry
);
289 Entry
= GetNextNode (BootOptionsList
,Entry
)
292 if (Index
== BootOptionSelected
) {
293 *BdsLoadOptionEntry
= LOAD_OPTION_ENTRY_FROM_LINK(Entry
);
303 BootMenuRemoveBootOption (
304 IN LIST_ENTRY
*BootOptionsList
308 BDS_LOAD_OPTION_ENTRY
* BootOptionEntry
;
310 Status
= BootMenuSelectBootOption (BootOptionsList
, L
"Delete entry: ", FALSE
, &BootOptionEntry
);
311 if (EFI_ERROR(Status
)) {
315 // If the Boot Option was attached to a list remove it
316 if (!IsListEmpty (&BootOptionEntry
->Link
)) {
317 // Remove the entry from the list
318 RemoveEntryList (&BootOptionEntry
->Link
);
321 // Delete the BDS Load option structures
322 BootOptionDelete (BootOptionEntry
->BdsLoadOption
);
328 BootMenuUpdateBootOption (
329 IN LIST_ENTRY
*BootOptionsList
333 BDS_LOAD_OPTION_ENTRY
*BootOptionEntry
;
334 BDS_LOAD_OPTION
*BootOption
;
335 BDS_LOAD_OPTION_SUPPORT
* DeviceSupport
;
336 ARM_BDS_LOADER_ARGUMENTS
* BootArguments
;
337 CHAR16 BootDescription
[BOOT_DEVICE_DESCRIPTION_MAX
];
338 CHAR8 CmdLine
[BOOT_DEVICE_OPTION_MAX
];
339 EFI_DEVICE_PATH
* DevicePath
;
340 ARM_BDS_LOADER_TYPE BootType
;
341 ARM_BDS_LOADER_OPTIONAL_DATA
* OptionalData
;
342 ARM_BDS_LINUX_ARGUMENTS
* LinuxArguments
;
343 EFI_DEVICE_PATH
* InitrdPathList
;
347 Status
= BootMenuSelectBootOption (BootOptionsList
, L
"Update entry: ", TRUE
, &BootOptionEntry
);
348 if (EFI_ERROR(Status
)) {
351 BootOption
= BootOptionEntry
->BdsLoadOption
;
353 // Get the device support for this Boot Option
354 Status
= BootDeviceGetDeviceSupport (BootOption
, &DeviceSupport
);
355 if (EFI_ERROR(Status
)) {
356 Print(L
"Not possible to retrieve the supported device for the update\n");
357 return EFI_UNSUPPORTED
;
360 Print(L
"File path of the EFI Application or the kernel: ");
361 Status
= DeviceSupport
->UpdateDevicePathNode (BootOption
->FilePathList
, &DevicePath
, NULL
, NULL
);
362 if (EFI_ERROR(Status
)) {
363 Status
= EFI_ABORTED
;
367 OptionalData
= BootOption
->OptionalData
;
368 BootType
= (ARM_BDS_LOADER_TYPE
)ReadUnaligned32 ((UINT32
*)(&OptionalData
->Header
.LoaderType
));
370 // TODO: Allow adding an initrd to a boot entry without one
371 if ((BootType
== BDS_LOADER_KERNEL_LINUX_ATAG
) || (BootType
== BDS_LOADER_KERNEL_LINUX_FDT
)) {
372 LinuxArguments
= &OptionalData
->Arguments
.LinuxArguments
;
374 CmdLineSize
= ReadUnaligned16 ((CONST UINT16
*)&LinuxArguments
->CmdLineSize
);
376 InitrdSize
= ReadUnaligned16 ((CONST UINT16
*)&LinuxArguments
->InitrdSize
);
377 if (InitrdSize
> 0) {
378 Print(L
"File path of the initrd: ");
379 Status
= DeviceSupport
->UpdateDevicePathNode ((EFI_DEVICE_PATH
*)((LinuxArguments
+ 1) + CmdLineSize
), &InitrdPathList
, NULL
, NULL
);
380 if (EFI_ERROR(Status
) && Status
!= EFI_NOT_FOUND
) {// EFI_NOT_FOUND is returned on empty input string, but we can boot without an initrd
381 Status
= EFI_ABORTED
;
384 InitrdSize
= GetDevicePathSize (InitrdPathList
);
387 Print(L
"Arguments to pass to the binary: ");
388 if (CmdLineSize
> 0) {
389 AsciiStrnCpy(CmdLine
, (CONST CHAR8
*)(LinuxArguments
+ 1), CmdLineSize
);
393 Status
= EditHIInputAscii (CmdLine
, BOOT_DEVICE_OPTION_MAX
);
394 if (EFI_ERROR(Status
)) {
395 Status
= EFI_ABORTED
;
396 goto FREE_DEVICE_PATH
;
399 CmdLineSize
= AsciiStrSize (CmdLine
);
401 BootArguments
= (ARM_BDS_LOADER_ARGUMENTS
*)AllocatePool(sizeof(ARM_BDS_LOADER_ARGUMENTS
) + CmdLineSize
+ InitrdSize
);
402 BootArguments
->LinuxArguments
.CmdLineSize
= CmdLineSize
;
403 BootArguments
->LinuxArguments
.InitrdSize
= InitrdSize
;
404 CopyMem (&BootArguments
->LinuxArguments
+ 1, CmdLine
, CmdLineSize
);
405 CopyMem ((UINTN
)(&BootArguments
->LinuxArguments
+ 1) + CmdLine
, InitrdPathList
, InitrdSize
);
407 BootArguments
= NULL
;
410 Print(L
"Description for this new Entry: ");
411 StrnCpy (BootDescription
, BootOption
->Description
, BOOT_DEVICE_DESCRIPTION_MAX
);
412 Status
= EditHIInputStr (BootDescription
, BOOT_DEVICE_DESCRIPTION_MAX
);
413 if (EFI_ERROR(Status
)) {
414 Status
= EFI_ABORTED
;
415 goto FREE_DEVICE_PATH
;
419 Status
= BootOptionUpdate (BootOption
, BootOption
->Attributes
, BootDescription
, DevicePath
, BootType
, BootArguments
);
422 FreePool (DevicePath
);
425 if (Status
== EFI_ABORTED
) {
431 struct BOOT_MANAGER_ENTRY
{
432 CONST CHAR16
* Description
;
433 EFI_STATUS (*Callback
) (IN LIST_ENTRY
*BootOptionsList
);
434 } BootManagerEntries
[] = {
435 { L
"Add Boot Device Entry", BootMenuAddBootOption
},
436 { L
"Update Boot Device Entry", BootMenuUpdateBootOption
},
437 { L
"Remove Boot Device Entry", BootMenuRemoveBootOption
},
442 IN LIST_ENTRY
*BootOptionsList
446 UINTN OptionSelected
;
447 UINTN BootManagerEntryCount
;
450 BootManagerEntryCount
= sizeof(BootManagerEntries
) / sizeof(struct BOOT_MANAGER_ENTRY
);
453 // Display Boot Manager menu
454 for (Index
= 0; Index
< BootManagerEntryCount
; Index
++) {
455 Print(L
"[%d] %s\n",Index
+1,BootManagerEntries
[Index
]);
457 Print(L
"[%d] Return to main menu\n",Index
+1);
459 // Select which entry to call
461 Status
= GetHIInputInteger (&OptionSelected
);
462 if (EFI_ERROR(Status
) || (OptionSelected
== (BootManagerEntryCount
+1))) {
463 if (EFI_ERROR(Status
)) {
467 } else if ((OptionSelected
> 0) && (OptionSelected
<= BootManagerEntryCount
)) {
468 BootManagerEntries
[OptionSelected
-1].Callback (BootOptionsList
);
477 IN LIST_ENTRY
*BootOptionsList
483 Status
= BdsLoadApplication (mImageHandle
, L
"Ebl", 0, NULL
);
484 if (Status
== EFI_NOT_FOUND
) {
485 Print (L
"Error: EFI Application not found.\n");
486 } else if (EFI_ERROR(Status
)) {
487 Print (L
"Error: Status Code: 0x%X\n",(UINT32
)Status
);
493 struct BOOT_MAIN_ENTRY
{
494 CONST CHAR16
* Description
;
495 EFI_STATUS (*Callback
) (IN LIST_ENTRY
*BootOptionsList
);
496 } BootMainEntries
[] = {
498 { L
"Boot Manager", BootMenuManager
},
507 LIST_ENTRY BootOptionsList
;
509 UINTN BootOptionCount
;
512 BDS_LOAD_OPTION
* BootOption
;
513 UINTN BootOptionSelected
;
515 UINTN BootMainEntryCount
;
518 BootMainEntryCount
= sizeof(BootMainEntries
) / sizeof(struct BOOT_MAIN_ENTRY
);
522 BootOptionList (&BootOptionsList
);
526 // Display the Boot options
527 for (Entry
= GetFirstNode (&BootOptionsList
);
528 !IsNull (&BootOptionsList
,Entry
);
529 Entry
= GetNextNode (&BootOptionsList
,Entry
)
532 BootOption
= LOAD_OPTION_FROM_LINK(Entry
);
534 Print(L
"[%d] %s\n", OptionCount
, BootOption
->Description
);
537 CHAR16
* DevicePathTxt
;
538 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL
* DevicePathToTextProtocol
;
539 ARM_BDS_LOADER_OPTIONAL_DATA
* OptionalData
;
541 ARM_BDS_LOADER_TYPE LoaderType
;
543 Status
= gBS
->LocateProtocol (&gEfiDevicePathToTextProtocolGuid
, NULL
, (VOID
**)&DevicePathToTextProtocol
);
544 if (EFI_ERROR(Status
)) {
545 // You must provide an implementation of DevicePathToTextProtocol in your firmware (eg: DevicePathDxe)
546 DEBUG((EFI_D_ERROR
,"Error: Bds requires DevicePathToTextProtocol\n"));
549 DevicePathTxt
= DevicePathToTextProtocol
->ConvertDevicePathToText (BootOption
->FilePathList
, TRUE
, TRUE
);
551 Print(L
"\t- %s\n",DevicePathTxt
);
553 // If it is a supported BootEntry then print its details
554 if (IS_ARM_BDS_BOOTENTRY (BootOption
)) {
555 OptionalData
= BootOption
->OptionalData
;
556 LoaderType
= (ARM_BDS_LOADER_TYPE
)ReadUnaligned32 ((CONST UINT32
*)&OptionalData
->Header
.LoaderType
);
557 if ((LoaderType
== BDS_LOADER_KERNEL_LINUX_ATAG
) || (LoaderType
== BDS_LOADER_KERNEL_LINUX_FDT
)) {
558 if (ReadUnaligned16 (&OptionalData
->Arguments
.LinuxArguments
.InitrdSize
) > 0) {
559 CmdLineSize
= ReadUnaligned16 (&OptionalData
->Arguments
.LinuxArguments
.CmdLineSize
);
560 DevicePathTxt
= DevicePathToTextProtocol
->ConvertDevicePathToText (
561 GetAlignedDevicePath ((EFI_DEVICE_PATH
*)((UINTN
)(&OptionalData
->Arguments
.LinuxArguments
+ 1) + CmdLineSize
)), TRUE
, TRUE
);
562 Print(L
"\t- Initrd: %s\n", DevicePathTxt
);
564 Print(L
"\t- Arguments: %a\n", (&OptionalData
->Arguments
.LinuxArguments
+ 1));
566 Print(L
"\t- LoaderType: %d\n", LoaderType
);
568 FreePool(DevicePathTxt
);
573 BootOptionCount
= OptionCount
-1;
575 // Display the hardcoded Boot entries
576 for (Index
= 0; Index
< BootMainEntryCount
; Index
++) {
577 Print(L
"[%d] %s\n",OptionCount
,BootMainEntries
[Index
]);
581 // Request the boot entry from the user
582 BootOptionSelected
= 0;
583 while (BootOptionSelected
== 0) {
585 Status
= GetHIInputInteger (&BootOptionSelected
);
586 if (EFI_ERROR(Status
) || (BootOptionSelected
== 0) || (BootOptionSelected
> OptionCount
)) {
587 Print(L
"Invalid input (max %d)\n",(OptionCount
-1));
588 BootOptionSelected
= 0;
592 // Start the selected entry
593 if (BootOptionSelected
> BootOptionCount
) {
594 // Start the hardcoded entry
595 Status
= BootMainEntries
[BootOptionSelected
- BootOptionCount
- 1].Callback (&BootOptionsList
);
597 // Find the selected entry from the Boot#### list
599 for (Entry
= GetFirstNode (&BootOptionsList
);
600 !IsNull (&BootOptionsList
,Entry
);
601 Entry
= GetNextNode (&BootOptionsList
,Entry
)
604 if (Index
== BootOptionSelected
) {
605 BootOption
= LOAD_OPTION_FROM_LINK(Entry
);
611 Status
= BootOptionStart (BootOption
);