2 Copyright (c) 2004 - 2008, Intel Corporation
3 All rights reserved. This program and the accompanying materials
4 are licensed and made available under the terms and conditions of the BSD License
5 which accompanies this distribution. The full text of the license may be found at
6 http://opensource.org/licenses/bsd-license.php
8 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
9 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 Provide boot option support for Application "BootMaint"
19 Include file system navigation, system handle selection
21 Boot option manipulation
27 #include "BootMaint.h"
28 #include "BBSsupport.h"
31 BOpt_CreateMenuEntry (
37 Create Menu Entry for future use, make all types together
38 in order to reduce code size
41 MenuType Use this parameter to identify current
45 NULL Cannot allocate memory for current menu
47 Others A valid pointer pointing to the allocated
48 memory pool for current menu entry
52 BM_MENU_ENTRY
*MenuEntry
;
56 case BM_LOAD_CONTEXT_SELECT
:
57 ContextSize
= sizeof (BM_LOAD_CONTEXT
);
60 case BM_FILE_CONTEXT_SELECT
:
61 ContextSize
= sizeof (BM_FILE_CONTEXT
);
64 case BM_CONSOLE_CONTEXT_SELECT
:
65 ContextSize
= sizeof (BM_CONSOLE_CONTEXT
);
68 case BM_TERMINAL_CONTEXT_SELECT
:
69 ContextSize
= sizeof (BM_TERMINAL_CONTEXT
);
72 case BM_HANDLE_CONTEXT_SELECT
:
73 ContextSize
= sizeof (BM_HANDLE_CONTEXT
);
76 case BM_LEGACY_DEV_CONTEXT_SELECT
:
77 ContextSize
= sizeof (BM_LEGACY_DEVICE_CONTEXT
);
86 if (0 == ContextSize
) {
90 MenuEntry
= EfiAllocateZeroPool (sizeof (BM_MENU_ENTRY
));
91 if (NULL
== MenuEntry
) {
95 MenuEntry
->VariableContext
= EfiAllocateZeroPool (ContextSize
);
96 if (NULL
== MenuEntry
->VariableContext
) {
97 SafeFreePool (MenuEntry
);
102 MenuEntry
->Signature
= BM_MENU_ENTRY_SIGNATURE
;
103 MenuEntry
->ContextSelection
= MenuType
;
108 BOpt_DestroyMenuEntry (
109 BM_MENU_ENTRY
*MenuEntry
112 Routine Description :
113 Destroy the menu entry passed in
116 The menu entry need to be destroyed
123 BM_LOAD_CONTEXT
*LoadContext
;
124 BM_FILE_CONTEXT
*FileContext
;
125 BM_CONSOLE_CONTEXT
*ConsoleContext
;
126 BM_TERMINAL_CONTEXT
*TerminalContext
;
127 BM_HANDLE_CONTEXT
*HandleContext
;
128 BM_LEGACY_DEVICE_CONTEXT
*LegacyDevContext
;
131 // Select by the type in Menu entry for current context type
133 switch (MenuEntry
->ContextSelection
) {
134 case BM_LOAD_CONTEXT_SELECT
:
135 LoadContext
= (BM_LOAD_CONTEXT
*) MenuEntry
->VariableContext
;
136 SafeFreePool (LoadContext
->FilePathList
);
137 SafeFreePool (LoadContext
->LoadOption
);
138 SafeFreePool (LoadContext
->OptionalData
);
139 SafeFreePool (LoadContext
);
142 case BM_FILE_CONTEXT_SELECT
:
143 FileContext
= (BM_FILE_CONTEXT
*) MenuEntry
->VariableContext
;
145 if (!FileContext
->IsRoot
) {
146 SafeFreePool (FileContext
->DevicePath
);
148 if (FileContext
->FHandle
!= NULL
) {
149 FileContext
->FHandle
->Close (FileContext
->FHandle
);
153 SafeFreePool (FileContext
->FileName
);
154 SafeFreePool (FileContext
->Info
);
155 SafeFreePool (FileContext
);
158 case BM_CONSOLE_CONTEXT_SELECT
:
159 ConsoleContext
= (BM_CONSOLE_CONTEXT
*) MenuEntry
->VariableContext
;
160 SafeFreePool (ConsoleContext
->DevicePath
);
161 SafeFreePool (ConsoleContext
);
164 case BM_TERMINAL_CONTEXT_SELECT
:
165 TerminalContext
= (BM_TERMINAL_CONTEXT
*) MenuEntry
->VariableContext
;
166 SafeFreePool (TerminalContext
->DevicePath
);
167 SafeFreePool (TerminalContext
);
170 case BM_HANDLE_CONTEXT_SELECT
:
171 HandleContext
= (BM_HANDLE_CONTEXT
*) MenuEntry
->VariableContext
;
172 SafeFreePool (HandleContext
);
175 case BM_LEGACY_DEV_CONTEXT_SELECT
:
176 LegacyDevContext
= (BM_LEGACY_DEVICE_CONTEXT
*) MenuEntry
->VariableContext
;
177 SafeFreePool (LegacyDevContext
);
183 SafeFreePool (MenuEntry
->DisplayString
);
184 if (NULL
!= MenuEntry
->HelpString
) {
185 SafeFreePool (MenuEntry
->HelpString
);
188 SafeFreePool (MenuEntry
);
193 BM_MENU_OPTION
*MenuOption
,
197 Rountine Description :
198 Use this routine to get one particular menu entry in specified
202 MenuOption The menu that we will search
204 MenuNumber The menunubmer that we want
207 The desired menu entry
211 BM_MENU_ENTRY
*NewMenuEntry
;
215 if (MenuNumber
>= MenuOption
->MenuNumber
) {
219 List
= MenuOption
->Head
.ForwardLink
;
220 for (Index
= 0; Index
< MenuNumber
; Index
++) {
221 List
= List
->ForwardLink
;
224 NewMenuEntry
= CR (List
, BM_MENU_ENTRY
, Link
, BM_MENU_ENTRY_SIGNATURE
);
230 BOpt_FindFileSystem (
231 IN BMM_CALLBACK_DATA
*CallbackData
236 Find file systems for current Extensible Firmware
237 Including Handles that support Simple File System
238 protocol, Load File protocol.
240 Building up the FileSystem Menu for user selection
241 All file system will be stored in FsOptionMenu
245 CallbackData - BMM context data
248 EFI_SUCCESS - Success find the file system
249 EFI_OUT_OF_RESOURCES - Can not create menu entry
253 UINTN NoBlkIoHandles
;
254 UINTN NoSimpleFsHandles
;
255 UINTN NoLoadFileHandles
;
256 EFI_HANDLE
*BlkIoHandle
;
257 EFI_HANDLE
*SimpleFsHandle
;
258 EFI_HANDLE
*LoadFileHandle
;
260 EFI_BLOCK_IO_PROTOCOL
*BlkIo
;
263 BM_MENU_ENTRY
*MenuEntry
;
264 BM_FILE_CONTEXT
*FileContext
;
268 EFI_LEGACY_BIOS_PROTOCOL
*LegacyBios
;
270 BBS_BBS_DEVICE_PATH BbsDevicePathNode
;
271 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
272 BOOLEAN RemovableMedia
;
275 NoSimpleFsHandles
= 0;
276 NoLoadFileHandles
= 0;
278 InitializeListHead (&FsOptionMenu
.Head
);
281 // Locate Handles that support BlockIo protocol
283 Status
= gBS
->LocateHandleBuffer (
285 &gEfiBlockIoProtocolGuid
,
290 if (!EFI_ERROR (Status
)) {
292 for (Index
= 0; Index
< NoBlkIoHandles
; Index
++) {
293 Status
= gBS
->HandleProtocol (
295 &gEfiBlockIoProtocolGuid
,
299 if (EFI_ERROR (Status
)) {
304 // Issue a dummy read to trigger reinstall of BlockIo protocol for removable media
306 if (BlkIo
->Media
->RemovableMedia
) {
307 Buffer
= EfiAllocateZeroPool (BlkIo
->Media
->BlockSize
);
308 if (NULL
== Buffer
) {
309 SafeFreePool (BlkIoHandle
);
310 return EFI_OUT_OF_RESOURCES
;
315 BlkIo
->Media
->MediaId
,
317 BlkIo
->Media
->BlockSize
,
320 SafeFreePool (Buffer
);
323 SafeFreePool (BlkIoHandle
);
327 // Locate Handles that support Simple File System protocol
329 Status
= gBS
->LocateHandleBuffer (
331 &gEfiSimpleFileSystemProtocolGuid
,
336 if (!EFI_ERROR (Status
)) {
338 // Find all the instances of the File System prototocol
340 for (Index
= 0; Index
< NoSimpleFsHandles
; Index
++) {
341 Status
= gBS
->HandleProtocol (
342 SimpleFsHandle
[Index
],
343 &gEfiBlockIoProtocolGuid
,
346 if (EFI_ERROR (Status
)) {
348 // If no block IO exists assume it's NOT a removable media
350 RemovableMedia
= FALSE
;
353 // If block IO exists check to see if it's remobable media
355 RemovableMedia
= BlkIo
->Media
->RemovableMedia
;
359 // Allocate pool for this load option
361 MenuEntry
= BOpt_CreateMenuEntry (BM_FILE_CONTEXT_SELECT
);
362 if (NULL
== MenuEntry
) {
363 SafeFreePool (SimpleFsHandle
);
364 return EFI_OUT_OF_RESOURCES
;
367 FileContext
= (BM_FILE_CONTEXT
*) MenuEntry
->VariableContext
;
369 FileContext
->Handle
= SimpleFsHandle
[Index
];
370 MenuEntry
->OptionNumber
= Index
;
371 FileContext
->FHandle
= EfiLibOpenRoot (FileContext
->Handle
);
372 if (!FileContext
->FHandle
) {
373 BOpt_DestroyMenuEntry (MenuEntry
);
377 MenuEntry
->HelpString
= DevicePathToStr (DevicePathFromHandle (FileContext
->Handle
));
378 FileContext
->Info
= EfiLibFileSystemVolumeLabelInfo (FileContext
->FHandle
);
379 FileContext
->FileName
= EfiStrDuplicate (L
"\\");
380 FileContext
->DevicePath
= FileDevicePath (
382 FileContext
->FileName
384 FileContext
->IsDir
= TRUE
;
385 FileContext
->IsRoot
= TRUE
;
386 FileContext
->IsRemovableMedia
= RemovableMedia
;
387 FileContext
->IsLoadFile
= FALSE
;
390 // Get current file system's Volume Label
392 if (FileContext
->Info
== NULL
) {
393 VolumeLabel
= L
"NO FILE SYSTEM INFO";
395 if (FileContext
->Info
->VolumeLabel
== NULL
) {
396 VolumeLabel
= L
"NULL VOLUME LABEL";
398 VolumeLabel
= FileContext
->Info
->VolumeLabel
;
399 if (*VolumeLabel
== 0x0000) {
400 VolumeLabel
= L
"NO VOLUME LABEL";
405 TempStr
= MenuEntry
->HelpString
;
406 MenuEntry
->DisplayString
= EfiAllocateZeroPool (MAX_CHAR
);
407 ASSERT (MenuEntry
->DisplayString
!= NULL
);
409 MenuEntry
->DisplayString
,
416 InsertTailList (&FsOptionMenu
.Head
, &MenuEntry
->Link
);
420 if (NoSimpleFsHandles
!= 0) {
421 SafeFreePool (SimpleFsHandle
);
424 // Searching for handles that support Load File protocol
426 Status
= gBS
->LocateHandleBuffer (
428 &gEfiLoadFileProtocolGuid
,
434 if (!EFI_ERROR (Status
)) {
435 for (Index
= 0; Index
< NoLoadFileHandles
; Index
++) {
436 MenuEntry
= BOpt_CreateMenuEntry (BM_FILE_CONTEXT_SELECT
);
437 if (NULL
== MenuEntry
) {
438 SafeFreePool (LoadFileHandle
);
439 return EFI_OUT_OF_RESOURCES
;
442 FileContext
= (BM_FILE_CONTEXT
*) MenuEntry
->VariableContext
;
443 FileContext
->IsRemovableMedia
= FALSE
;
444 FileContext
->IsLoadFile
= TRUE
;
445 FileContext
->Handle
= LoadFileHandle
[Index
];
446 FileContext
->IsRoot
= TRUE
;
448 FileContext
->DevicePath
= DevicePathFromHandle (FileContext
->Handle
);
450 MenuEntry
->HelpString
= DevicePathToStr (FileContext
->DevicePath
);
452 TempStr
= MenuEntry
->HelpString
;
453 MenuEntry
->DisplayString
= EfiAllocateZeroPool (MAX_CHAR
);
454 ASSERT (MenuEntry
->DisplayString
!= NULL
);
456 MenuEntry
->DisplayString
,
462 MenuEntry
->OptionNumber
= OptionNumber
;
464 InsertTailList (&FsOptionMenu
.Head
, &MenuEntry
->Link
);
468 if (NoLoadFileHandles
!= 0) {
469 SafeFreePool (LoadFileHandle
);
473 // Add Legacy Boot Option Support Here
475 Status
= gBS
->LocateProtocol (
476 &gEfiLegacyBiosProtocolGuid
,
478 (VOID
**) &LegacyBios
480 if (!EFI_ERROR (Status
)) {
482 for (Index
= BBS_TYPE_FLOPPY
; Index
<= BBS_TYPE_EMBEDDED_NETWORK
; Index
++) {
483 MenuEntry
= BOpt_CreateMenuEntry (BM_FILE_CONTEXT_SELECT
);
484 if (NULL
== MenuEntry
) {
485 return EFI_OUT_OF_RESOURCES
;
488 FileContext
= (BM_FILE_CONTEXT
*) MenuEntry
->VariableContext
;
490 FileContext
->IsRemovableMedia
= FALSE
;
491 FileContext
->IsLoadFile
= TRUE
;
492 FileContext
->IsBootLegacy
= TRUE
;
493 DeviceType
= (UINT16
) Index
;
494 BbsDevicePathNode
.Header
.Type
= BBS_DEVICE_PATH
;
495 BbsDevicePathNode
.Header
.SubType
= BBS_BBS_DP
;
496 SetDevicePathNodeLength (
497 &BbsDevicePathNode
.Header
,
498 sizeof (BBS_BBS_DEVICE_PATH
)
500 BbsDevicePathNode
.DeviceType
= DeviceType
;
501 BbsDevicePathNode
.StatusFlag
= 0;
502 BbsDevicePathNode
.String
[0] = 0;
503 DevicePath
= AppendDevicePathNode (
505 (EFI_DEVICE_PATH_PROTOCOL
*) &BbsDevicePathNode
508 FileContext
->DevicePath
= DevicePath
;
509 MenuEntry
->HelpString
= DevicePathToStr (FileContext
->DevicePath
);
511 TempStr
= MenuEntry
->HelpString
;
512 MenuEntry
->DisplayString
= EfiAllocateZeroPool (MAX_CHAR
);
513 ASSERT (MenuEntry
->DisplayString
!= NULL
);
515 MenuEntry
->DisplayString
,
520 MenuEntry
->OptionNumber
= OptionNumber
;
522 InsertTailList (&FsOptionMenu
.Head
, &MenuEntry
->Link
);
526 // Remember how many file system options are here
528 FsOptionMenu
.MenuNumber
= OptionNumber
;
534 BM_MENU_OPTION
*FreeMenu
539 Free resources allocated in Allocate Rountine
542 FreeMenu Menu to be freed
549 BM_MENU_ENTRY
*MenuEntry
;
550 while (!IsListEmpty (&FreeMenu
->Head
)) {
552 FreeMenu
->Head
.ForwardLink
,
555 BM_MENU_ENTRY_SIGNATURE
557 RemoveEntryList (&MenuEntry
->Link
);
558 BOpt_DestroyMenuEntry (MenuEntry
);
564 IN BMM_CALLBACK_DATA
*CallbackData
,
565 IN BM_MENU_ENTRY
*MenuEntry
570 Find files under current directory
571 All files and sub-directories in current directory
572 will be stored in DirectoryMenu for future use.
575 FileOption -- Pointer for Dir to explore
578 TRUE -- Get files from current dir successfully
579 FALSE -- Can't get files from current dir
583 EFI_FILE_HANDLE NewDir
;
585 EFI_FILE_INFO
*DirInfo
;
588 BM_MENU_ENTRY
*NewMenuEntry
;
589 BM_FILE_CONTEXT
*FileContext
;
590 BM_FILE_CONTEXT
*NewFileContext
;
595 FileContext
= (BM_FILE_CONTEXT
*) MenuEntry
->VariableContext
;
596 Dir
= FileContext
->FHandle
;
599 // Open current directory to get files from it
604 FileContext
->FileName
,
608 if (!FileContext
->IsRoot
) {
612 if (EFI_ERROR (Status
)) {
616 DirInfo
= EfiLibFileInfo (NewDir
);
618 return EFI_NOT_FOUND
;
621 if (!(DirInfo
->Attribute
& EFI_FILE_DIRECTORY
)) {
622 return EFI_INVALID_PARAMETER
;
625 FileContext
->DevicePath
= FileDevicePath (
627 FileContext
->FileName
630 DirBufferSize
= sizeof (EFI_FILE_INFO
) + 1024;
631 DirInfo
= EfiAllocateZeroPool (DirBufferSize
);
633 return EFI_OUT_OF_RESOURCES
;
636 // Get all files in current directory
637 // Pass 1 to get Directories
638 // Pass 2 to get files that are EFI images
640 for (Pass
= 1; Pass
<= 2; Pass
++) {
641 NewDir
->SetPosition (NewDir
, 0);
643 BufferSize
= DirBufferSize
;
644 Status
= NewDir
->Read (NewDir
, &BufferSize
, DirInfo
);
645 if (EFI_ERROR (Status
) || BufferSize
== 0) {
649 if ((DirInfo
->Attribute
& EFI_FILE_DIRECTORY
&& Pass
== 2) ||
650 (!(DirInfo
->Attribute
& EFI_FILE_DIRECTORY
) && Pass
== 1)
653 // Pass 1 is for Directories
654 // Pass 2 is for file names
659 if (!(BOpt_IsEfiImageName (DirInfo
->FileName
) || DirInfo
->Attribute
& EFI_FILE_DIRECTORY
)) {
661 // Slip file unless it is a directory entry or a .EFI file
666 NewMenuEntry
= BOpt_CreateMenuEntry (BM_FILE_CONTEXT_SELECT
);
667 if (NULL
== NewMenuEntry
) {
668 return EFI_OUT_OF_RESOURCES
;
671 NewFileContext
= (BM_FILE_CONTEXT
*) NewMenuEntry
->VariableContext
;
672 NewFileContext
->Handle
= FileContext
->Handle
;
673 NewFileContext
->FileName
= BOpt_AppendFileName (
674 FileContext
->FileName
,
677 NewFileContext
->FHandle
= NewDir
;
678 NewFileContext
->DevicePath
= FileDevicePath (
679 NewFileContext
->Handle
,
680 NewFileContext
->FileName
682 NewMenuEntry
->HelpString
= NULL
;
684 MenuEntry
->DisplayStringToken
= GetStringTokenFromDepository (
686 FileOptionStrDepository
689 NewFileContext
->IsDir
= (BOOLEAN
) ((DirInfo
->Attribute
& EFI_FILE_DIRECTORY
) == EFI_FILE_DIRECTORY
);
691 if (NewFileContext
->IsDir
) {
692 BufferSize
= StrLen (DirInfo
->FileName
) * 2 + 6;
693 NewMenuEntry
->DisplayString
= EfiAllocateZeroPool (BufferSize
);
696 NewMenuEntry
->DisplayString
,
703 NewMenuEntry
->DisplayString
= EfiStrDuplicate (DirInfo
->FileName
);
706 NewFileContext
->IsRoot
= FALSE
;
707 NewFileContext
->IsLoadFile
= FALSE
;
708 NewFileContext
->IsRemovableMedia
= FALSE
;
710 NewMenuEntry
->OptionNumber
= OptionNumber
;
712 InsertTailList (&DirectoryMenu
.Head
, &NewMenuEntry
->Link
);
716 DirectoryMenu
.MenuNumber
= OptionNumber
;
717 SafeFreePool (DirInfo
);
722 BOpt_GetLegacyOptions (
728 Build the LegacyFDMenu LegacyHDMenu LegacyCDMenu according to LegacyBios.GetBbsInfo().
734 The device info of legacy device.
738 BM_MENU_ENTRY
*NewMenuEntry
;
739 BM_LEGACY_DEVICE_CONTEXT
*NewLegacyDevContext
;
741 EFI_LEGACY_BIOS_PROTOCOL
*LegacyBios
;
747 CHAR16 DescString
[100];
760 // Initialize Bbs Table Context from BBS info data
762 InitializeListHead (&LegacyFDMenu
.Head
);
763 InitializeListHead (&LegacyHDMenu
.Head
);
764 InitializeListHead (&LegacyCDMenu
.Head
);
765 InitializeListHead (&LegacyNETMenu
.Head
);
766 InitializeListHead (&LegacyBEVMenu
.Head
);
768 Status
= gBS
->LocateProtocol (
769 &gEfiLegacyBiosProtocolGuid
,
771 (VOID
**) &LegacyBios
773 if (!EFI_ERROR (Status
)) {
774 Status
= LegacyBios
->GetBbsInfo (
781 if (EFI_ERROR (Status
)) {
792 for (Index
= 0; Index
< BbsCount
; Index
++) {
793 if ((BBS_IGNORE_ENTRY
== BbsTable
[Index
].BootPriority
) ||
794 (BBS_DO_NOT_BOOT_FROM
== BbsTable
[Index
].BootPriority
)
799 NewMenuEntry
= BOpt_CreateMenuEntry (BM_LEGACY_DEV_CONTEXT_SELECT
);
800 if (NULL
== NewMenuEntry
) {
804 NewLegacyDevContext
= (BM_LEGACY_DEVICE_CONTEXT
*) NewMenuEntry
->VariableContext
;
805 NewLegacyDevContext
->BbsTable
= &BbsTable
[Index
];
806 NewLegacyDevContext
->Index
= Index
;
807 NewLegacyDevContext
->BbsCount
= BbsCount
;
808 BdsBuildLegacyDevNameString (
814 NewLegacyDevContext
->Description
= EfiAllocateZeroPool (StrSize (DescString
));
815 if (NULL
== NewLegacyDevContext
->Description
) {
819 CopyMem (NewLegacyDevContext
->Description
, DescString
, StrSize (DescString
));
820 NewMenuEntry
->DisplayString
= NewLegacyDevContext
->Description
;
821 NewMenuEntry
->HelpString
= NULL
;
823 switch (BbsTable
[Index
].DeviceType
) {
825 InsertTailList (&LegacyFDMenu
.Head
, &NewMenuEntry
->Link
);
830 InsertTailList (&LegacyHDMenu
.Head
, &NewMenuEntry
->Link
);
835 InsertTailList (&LegacyCDMenu
.Head
, &NewMenuEntry
->Link
);
839 case BBS_EMBED_NETWORK
:
840 InsertTailList (&LegacyNETMenu
.Head
, &NewMenuEntry
->Link
);
845 InsertTailList (&LegacyBEVMenu
.Head
, &NewMenuEntry
->Link
);
851 if (Index
!= BbsCount
) {
852 BOpt_FreeLegacyOptions ();
853 return EFI_OUT_OF_RESOURCES
;
856 LegacyFDMenu
.MenuNumber
= FDNum
;
857 LegacyHDMenu
.MenuNumber
= HDNum
;
858 LegacyCDMenu
.MenuNumber
= CDNum
;
859 LegacyNETMenu
.MenuNumber
= NETNum
;
860 LegacyBEVMenu
.MenuNumber
= BEVNum
;
865 BOpt_FreeLegacyOptions (
869 BOpt_FreeMenu (&LegacyFDMenu
);
870 BOpt_FreeMenu (&LegacyHDMenu
);
871 BOpt_FreeMenu (&LegacyCDMenu
);
872 BOpt_FreeMenu (&LegacyNETMenu
);
873 BOpt_FreeMenu (&LegacyBEVMenu
);
877 BOpt_GetBootOptions (
878 IN BMM_CALLBACK_DATA
*CallbackData
884 Build the BootOptionMenu according to BootOrder Variable.
885 This Routine will access the Boot#### to get EFI_LOAD_OPTION
891 The number of the Var Boot####
896 UINT16 BootString
[10];
897 UINT8
*LoadOptionFromVar
;
899 UINTN BootOptionSize
;
900 BOOLEAN BootNextFlag
;
901 UINT16
*BootOrderList
;
902 UINTN BootOrderListSize
;
905 BM_MENU_ENTRY
*NewMenuEntry
;
906 BM_LOAD_CONTEXT
*NewLoadContext
;
907 UINT8
*LoadOptionPtr
;
909 UINTN OptionalDataSize
;
910 UINT8
*LoadOptionEnd
;
911 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
916 BootOrderListSize
= 0;
918 BootOrderList
= NULL
;
920 LoadOptionFromVar
= NULL
;
921 BOpt_FreeMenu (&BootOptionMenu
);
922 InitializeListHead (&BootOptionMenu
.Head
);
925 // Get the BootOrder from the Var
927 BootOrderList
= BdsLibGetVariableAndSize (
929 &gEfiGlobalVariableGuid
,
934 // Get the BootNext from the Var
936 BootNext
= BdsLibGetVariableAndSize (
938 &gEfiGlobalVariableGuid
,
943 if (BootNextSize
!= sizeof (UINT16
)) {
944 SafeFreePool (BootNext
);
949 for (Index
= 0; Index
< BootOrderListSize
/ sizeof (UINT16
); Index
++) {
950 UnicodeSPrint (BootString
, sizeof (BootString
), L
"Boot%04x", BootOrderList
[Index
]);
952 // Get all loadoptions from the VAR
954 LoadOptionFromVar
= BdsLibGetVariableAndSize (
956 &gEfiGlobalVariableGuid
,
959 if (!LoadOptionFromVar
) {
963 LoadOption
= EfiAllocateZeroPool (BootOptionSize
);
968 CopyMem (LoadOption
, LoadOptionFromVar
, BootOptionSize
);
969 SafeFreePool (LoadOptionFromVar
);
972 BootNextFlag
= (BOOLEAN
) (*BootNext
== BootOrderList
[Index
]);
974 BootNextFlag
= FALSE
;
977 if (0 == (*((UINT32
*) LoadOption
) & LOAD_OPTION_ACTIVE
)) {
978 SafeFreePool (LoadOption
);
982 // BUGBUG: could not return EFI_OUT_OF_RESOURCES here directly.
983 // the buffer allocated already should be freed before returning.
985 NewMenuEntry
= BOpt_CreateMenuEntry (BM_LOAD_CONTEXT_SELECT
);
986 if (NULL
== NewMenuEntry
) {
987 return EFI_OUT_OF_RESOURCES
;
990 NewLoadContext
= (BM_LOAD_CONTEXT
*) NewMenuEntry
->VariableContext
;
992 LoadOptionPtr
= LoadOption
;
993 LoadOptionEnd
= LoadOption
+ BootOptionSize
;
995 NewMenuEntry
->OptionNumber
= BootOrderList
[Index
];
996 NewLoadContext
->LoadOptionModified
= FALSE
;
997 NewLoadContext
->Deleted
= FALSE
;
998 NewLoadContext
->IsBootNext
= BootNextFlag
;
1001 // Is a Legacy Device?
1003 Ptr
= (UINT8
*) LoadOption
;
1006 // Attribute = *(UINT32 *)Ptr;
1008 Ptr
+= sizeof (UINT32
);
1011 // FilePathSize = *(UINT16 *)Ptr;
1013 Ptr
+= sizeof (UINT16
);
1016 // Description = (CHAR16 *)Ptr;
1018 Ptr
+= StrSize ((CHAR16
*) Ptr
);
1021 // Now Ptr point to Device Path
1023 DevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) Ptr
;
1024 if ((BBS_DEVICE_PATH
== DevicePath
->Type
) && (BBS_BBS_DP
== DevicePath
->SubType
)) {
1025 NewLoadContext
->IsLegacy
= TRUE
;
1027 NewLoadContext
->IsLegacy
= FALSE
;
1030 // LoadOption is a pointer type of UINT8
1031 // for easy use with following LOAD_OPTION
1032 // embedded in this struct
1034 NewLoadContext
->LoadOption
= LoadOption
;
1035 NewLoadContext
->LoadOptionSize
= BootOptionSize
;
1037 NewLoadContext
->Attributes
= *(UINT32
*) LoadOptionPtr
;
1038 NewLoadContext
->IsActive
= (BOOLEAN
) (NewLoadContext
->Attributes
& LOAD_OPTION_ACTIVE
);
1040 NewLoadContext
->ForceReconnect
= (BOOLEAN
) (NewLoadContext
->Attributes
& LOAD_OPTION_FORCE_RECONNECT
);
1042 LoadOptionPtr
+= sizeof (UINT32
);
1044 NewLoadContext
->FilePathListLength
= *(UINT16
*) LoadOptionPtr
;
1045 LoadOptionPtr
+= sizeof (UINT16
);
1047 StringSize
= StrSize ((UINT16
*) LoadOptionPtr
);
1048 NewLoadContext
->Description
= EfiAllocateZeroPool (StringSize
);
1049 ASSERT (NewLoadContext
->Description
!= NULL
);
1051 NewLoadContext
->Description
,
1052 (UINT16
*) LoadOptionPtr
,
1055 NewMenuEntry
->DisplayString
= NewLoadContext
->Description
;
1057 LoadOptionPtr
+= StringSize
;
1059 NewLoadContext
->FilePathList
= EfiAllocateZeroPool (NewLoadContext
->FilePathListLength
);
1060 ASSERT (NewLoadContext
->FilePathList
!= NULL
);
1062 NewLoadContext
->FilePathList
,
1063 (EFI_DEVICE_PATH_PROTOCOL
*) LoadOptionPtr
,
1064 NewLoadContext
->FilePathListLength
1067 NewMenuEntry
->HelpString
= DevicePathToStr (NewLoadContext
->FilePathList
);
1068 NewMenuEntry
->DisplayStringToken
= GetStringTokenFromDepository (
1070 BootOptionStrDepository
1072 NewMenuEntry
->HelpStringToken
= GetStringTokenFromDepository (
1074 BootOptionHelpStrDepository
1076 LoadOptionPtr
+= NewLoadContext
->FilePathListLength
;
1078 if (LoadOptionPtr
< LoadOptionEnd
) {
1079 OptionalDataSize
= BootOptionSize
-
1083 NewLoadContext
->FilePathListLength
;
1085 NewLoadContext
->OptionalData
= EfiAllocateZeroPool (OptionalDataSize
);
1086 ASSERT (NewLoadContext
->OptionalData
!= NULL
);
1088 NewLoadContext
->OptionalData
,
1093 NewLoadContext
->OptionalDataSize
= OptionalDataSize
;
1096 InsertTailList (&BootOptionMenu
.Head
, &NewMenuEntry
->Link
);
1100 SafeFreePool (BootNext
);
1101 SafeFreePool (BootOrderList
);
1102 BootOptionMenu
.MenuNumber
= MenuCount
;
1108 OUT CHAR16
*Destination
,
1109 IN CONST CHAR16
*Source
1112 CHAR16
*ReturnValue
;
1115 // Destination cannot be NULL
1117 ASSERT (Destination
!= NULL
);
1119 ReturnValue
= Destination
;
1121 *(Destination
++) = *(Source
++);
1128 BOpt_AppendFileName (
1135 Append file name to existing file name.
1138 Str1 - existing file name
1139 Str2 - file name to be appended
1142 Allocate a new string to hold the appended result.
1143 Caller is responsible to free the returned string.
1153 Size1
= StrSize (Str1
);
1154 Size2
= StrSize (Str2
);
1155 Str
= EfiAllocateZeroPool (Size1
+ Size2
+ sizeof (CHAR16
));
1156 ASSERT (Str
!= NULL
);
1159 if (!((*Str
== '\\') && (*(Str
+ 1) == 0))) {
1160 StrCat (Str
, L
"\\");
1168 if (*Ptr
== '\\' && *(Ptr
+ 1) == '.' && *(Ptr
+ 2) == '.' && *(Ptr
+ 3) != 0) {
1170 // Convert \Name\..\ to \
1171 // DO NOT convert the .. if it is at the end of the string. This will
1172 // break the .. behavior in changing directories.
1174 BdsStrCpy (LastSlash
, Ptr
+ 3);
1176 } else if (*Ptr
== '\\' && *(Ptr
+ 1) == '.' && *(Ptr
+ 2) == '\\') {
1178 // Convert a \.\ to a \
1180 BdsStrCpy (Ptr
, Ptr
+ 2);
1182 } else if (*Ptr
== '\\') {
1193 BOpt_IsEfiImageName (
1199 Check whether current FileName point to a valid
1203 FileName - File need to be checked.
1207 FALSE - Not a valid Efi Image
1212 // Search for ".efi" extension
1215 if (FileName
[0] == '.') {
1216 if (FileName
[1] == 'e' || FileName
[1] == 'E') {
1217 if (FileName
[2] == 'f' || FileName
[2] == 'F') {
1218 if (FileName
[3] == 'i' || FileName
[3] == 'I') {
1220 } else if (FileName
[3] == 0x0000) {
1223 } else if (FileName
[2] == 0x0000) {
1226 } else if (FileName
[1] == 0x0000) {
1239 IN EFI_FILE_HANDLE Dir
,
1244 Routine Description:
1245 Check whether current FileName point to a valid Efi Application
1248 Dir - Pointer to current Directory
1249 FileName - Pointer to current File name.
1252 TRUE - Is a valid Efi Application
1253 FALSE - not a valid Efi Application
1258 EFI_IMAGE_DOS_HEADER DosHdr
;
1259 EFI_IMAGE_NT_HEADERS PeHdr
;
1260 EFI_IMAGE_OPTIONAL_HEADER32
*PeOpt32
;
1261 EFI_IMAGE_OPTIONAL_HEADER64
*PeOpt64
;
1263 EFI_FILE_HANDLE File
;
1266 Status
= Dir
->Open (Dir
, &File
, FileName
, EFI_FILE_MODE_READ
, 0);
1268 if (EFI_ERROR (Status
)) {
1272 BufferSize
= sizeof (EFI_IMAGE_DOS_HEADER
);
1273 File
->Read (File
, &BufferSize
, &DosHdr
);
1274 if (DosHdr
.e_magic
!= EFI_IMAGE_DOS_SIGNATURE
) {
1279 File
->SetPosition (File
, DosHdr
.e_lfanew
);
1280 BufferSize
= sizeof (EFI_IMAGE_NT_HEADERS
);
1281 File
->Read (File
, &BufferSize
, &PeHdr
);
1282 if (PeHdr
.Signature
!= EFI_IMAGE_NT_SIGNATURE
) {
1287 // Determine PE type and read subsytem
1288 // BugBug : We should be using EFI_IMAGE_MACHINE_TYPE_SUPPORTED (machine)
1289 // macro to detect the machine type.
1290 // We should not be using EFI_IMAGE_OPTIONAL_HEADER32 and
1291 // EFI_IMAGE_OPTIONAL_HEADER64
1293 if (PeHdr
.OptionalHeader
.Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
1294 PeOpt32
= (EFI_IMAGE_OPTIONAL_HEADER32
*) &(PeHdr
.OptionalHeader
);
1295 Subsystem
= PeOpt32
->Subsystem
;
1296 } else if (PeHdr
.OptionalHeader
.Magic
== EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
) {
1297 PeOpt64
= (EFI_IMAGE_OPTIONAL_HEADER64
*) &(PeHdr
.OptionalHeader
);
1298 Subsystem
= PeOpt64
->Subsystem
;
1303 if (Subsystem
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
) {
1319 Find drivers that will be added as Driver#### variables from handles
1320 in current system environment
1321 All valid handles in the system except those consume SimpleFs, LoadFile
1322 are stored in DriverMenu for future use.
1333 UINTN NoDevicePathHandles
;
1334 EFI_HANDLE
*DevicePathHandle
;
1337 BM_MENU_ENTRY
*NewMenuEntry
;
1338 BM_HANDLE_CONTEXT
*NewHandleContext
;
1339 EFI_HANDLE CurHandle
;
1341 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*SimpleFs
;
1342 EFI_LOAD_FILE_PROTOCOL
*LoadFile
;
1347 InitializeListHead (&DriverMenu
.Head
);
1350 // At first, get all handles that support Device Path
1351 // protocol which is the basic requirement for
1354 Status
= gBS
->LocateHandleBuffer (
1356 &gEfiDevicePathProtocolGuid
,
1358 &NoDevicePathHandles
,
1361 if (EFI_ERROR (Status
)) {
1366 for (Index
= 0; Index
< NoDevicePathHandles
; Index
++) {
1367 CurHandle
= DevicePathHandle
[Index
];
1370 // Check whether this handle support
1373 Status
= gBS
->HandleProtocol (
1375 &gEfiSimpleFileSystemProtocolGuid
,
1378 if (Status
== EFI_SUCCESS
) {
1382 Status
= gBS
->HandleProtocol (
1384 &gEfiLoadFileProtocolGuid
,
1387 if (Status
== EFI_SUCCESS
) {
1391 NewMenuEntry
= BOpt_CreateMenuEntry (BM_HANDLE_CONTEXT_SELECT
);
1392 if (NULL
== NewMenuEntry
) {
1393 SafeFreePool (DevicePathHandle
);
1394 return EFI_OUT_OF_RESOURCES
;
1397 NewHandleContext
= (BM_HANDLE_CONTEXT
*) NewMenuEntry
->VariableContext
;
1398 NewHandleContext
->Handle
= CurHandle
;
1399 NewHandleContext
->DevicePath
= DevicePathFromHandle (CurHandle
);
1400 NewMenuEntry
->DisplayString
= DevicePathToStr (NewHandleContext
->DevicePath
);
1401 NewMenuEntry
->HelpString
= NULL
;
1402 NewMenuEntry
->OptionNumber
= OptionNumber
;
1404 InsertTailList (&DriverMenu
.Head
, &NewMenuEntry
->Link
);
1407 SafeFreePool (DevicePathHandle
);
1409 DriverMenu
.MenuNumber
= OptionNumber
;
1414 BOpt_GetBootOptionNumber (
1419 Routine Description:
1420 Get the Option Number that does not used
1429 BM_MENU_ENTRY
*NewMenuEntry
;
1430 UINT16
*BootOrderList
;
1431 UINTN BootOrderListSize
;
1436 CHAR16 StrTemp
[100];
1437 UINT16
*OptionBuffer
;
1440 BootOrderListSize
= 0;
1441 BootOrderList
= NULL
;
1443 BootOrderList
= BdsLibGetVariableAndSize (
1445 &gEfiGlobalVariableGuid
,
1448 if (BootOrderList
) {
1450 // already have Boot####
1452 // AlreadyBootNumbers = BootOrderListSize / sizeof(UINT16);
1454 for (Index
= 0; Index
< BootOrderListSize
/ sizeof (UINT16
); Index
++) {
1456 for (Index2
= 0; Index2
< BootOptionMenu
.MenuNumber
; Index2
++) {
1457 NewMenuEntry
= BOpt_GetMenuEntry (&BootOptionMenu
, Index2
);
1458 if (Index
== NewMenuEntry
->OptionNumber
) {
1465 UnicodeSPrint (StrTemp
, 100, L
"Boot%04x", Index
);
1466 DEBUG((DEBUG_ERROR
,"INdex= %s\n", StrTemp
));
1467 OptionBuffer
= BdsLibGetVariableAndSize (
1469 &gEfiGlobalVariableGuid
,
1472 if (NULL
== OptionBuffer
)
1479 Number
= (UINT16
) Index
;
1491 BOpt_GetDriverOptionNumber (
1496 Routine Description:
1497 Get the Option Number that does not used
1506 BM_MENU_ENTRY
*NewMenuEntry
;
1507 UINT16
*DriverOrderList
;
1508 UINTN DriverOrderListSize
;
1514 DriverOrderListSize
= 0;
1515 DriverOrderList
= NULL
;
1517 DriverOrderList
= BdsLibGetVariableAndSize (
1519 &gEfiGlobalVariableGuid
,
1520 &DriverOrderListSize
1522 if (DriverOrderList
) {
1524 // already have Driver####
1526 // AlreadyDriverNumbers = DriverOrderListSize / sizeof(UINT16);
1528 for (Index
= 0; Index
< DriverOrderListSize
/ sizeof (UINT16
); Index
++) {
1530 for (Index2
= 0; Index2
< DriverOptionMenu
.MenuNumber
; Index2
++) {
1531 NewMenuEntry
= BOpt_GetMenuEntry (&DriverOptionMenu
, Index2
);
1532 if (Index
== NewMenuEntry
->OptionNumber
) {
1545 Number
= (UINT16
) Index
;
1557 BOpt_GetDriverOptions (
1558 IN BMM_CALLBACK_DATA
*CallbackData
1562 Routine Description:
1563 Build up all DriverOptionMenu
1573 UINT16 DriverString
[12];
1574 UINT8
*LoadOptionFromVar
;
1576 UINTN DriverOptionSize
;
1578 UINT16
*DriverOrderList
;
1579 UINTN DriverOrderListSize
;
1580 BM_MENU_ENTRY
*NewMenuEntry
;
1581 BM_LOAD_CONTEXT
*NewLoadContext
;
1582 UINT8
*LoadOptionPtr
;
1584 UINTN OptionalDataSize
;
1585 UINT8
*LoadOptionEnd
;
1587 DriverOrderListSize
= 0;
1588 DriverOrderList
= NULL
;
1589 DriverOptionSize
= 0;
1590 LoadOptionFromVar
= NULL
;
1591 BOpt_FreeMenu (&DriverOptionMenu
);
1592 InitializeListHead (&DriverOptionMenu
.Head
);
1594 // Get the DriverOrder from the Var
1596 DriverOrderList
= BdsLibGetVariableAndSize (
1598 &gEfiGlobalVariableGuid
,
1599 &DriverOrderListSize
1602 for (Index
= 0; Index
< DriverOrderListSize
/ sizeof (UINT16
); Index
++) {
1605 sizeof (DriverString
),
1607 DriverOrderList
[Index
]
1610 // Get all loadoptions from the VAR
1612 LoadOptionFromVar
= BdsLibGetVariableAndSize (
1614 &gEfiGlobalVariableGuid
,
1617 if (!LoadOptionFromVar
) {
1621 LoadOption
= EfiAllocateZeroPool (DriverOptionSize
);
1626 CopyMem (LoadOption
, LoadOptionFromVar
, DriverOptionSize
);
1627 SafeFreePool (LoadOptionFromVar
);
1629 NewMenuEntry
= BOpt_CreateMenuEntry (BM_LOAD_CONTEXT_SELECT
);
1630 if (NULL
== NewMenuEntry
) {
1631 return EFI_OUT_OF_RESOURCES
;
1634 NewLoadContext
= (BM_LOAD_CONTEXT
*) NewMenuEntry
->VariableContext
;
1635 LoadOptionPtr
= LoadOption
;
1636 LoadOptionEnd
= LoadOption
+ DriverOptionSize
;
1637 NewMenuEntry
->OptionNumber
= DriverOrderList
[Index
];
1638 NewLoadContext
->LoadOptionModified
= FALSE
;
1639 NewLoadContext
->Deleted
= FALSE
;
1640 NewLoadContext
->IsLegacy
= FALSE
;
1643 // LoadOption is a pointer type of UINT8
1644 // for easy use with following LOAD_OPTION
1645 // embedded in this struct
1647 NewLoadContext
->LoadOption
= LoadOption
;
1648 NewLoadContext
->LoadOptionSize
= DriverOptionSize
;
1650 NewLoadContext
->Attributes
= *(UINT32
*) LoadOptionPtr
;
1651 NewLoadContext
->IsActive
= (BOOLEAN
) (NewLoadContext
->Attributes
& LOAD_OPTION_ACTIVE
);
1653 NewLoadContext
->ForceReconnect
= (BOOLEAN
) (NewLoadContext
->Attributes
& LOAD_OPTION_FORCE_RECONNECT
);
1655 LoadOptionPtr
+= sizeof (UINT32
);
1657 NewLoadContext
->FilePathListLength
= *(UINT16
*) LoadOptionPtr
;
1658 LoadOptionPtr
+= sizeof (UINT16
);
1660 StringSize
= StrSize ((UINT16
*) LoadOptionPtr
);
1661 NewLoadContext
->Description
= EfiAllocateZeroPool (StringSize
);
1662 ASSERT (NewLoadContext
->Description
!= NULL
);
1664 NewLoadContext
->Description
,
1665 (UINT16
*) LoadOptionPtr
,
1668 NewMenuEntry
->DisplayString
= NewLoadContext
->Description
;
1670 LoadOptionPtr
+= StringSize
;
1672 NewLoadContext
->FilePathList
= EfiAllocateZeroPool (NewLoadContext
->FilePathListLength
);
1673 ASSERT (NewLoadContext
->FilePathList
!= NULL
);
1675 NewLoadContext
->FilePathList
,
1676 (EFI_DEVICE_PATH_PROTOCOL
*) LoadOptionPtr
,
1677 NewLoadContext
->FilePathListLength
1680 NewMenuEntry
->HelpString
= DevicePathToStr (NewLoadContext
->FilePathList
);
1681 NewMenuEntry
->DisplayStringToken
= GetStringTokenFromDepository (
1683 DriverOptionStrDepository
1685 NewMenuEntry
->HelpStringToken
= GetStringTokenFromDepository (
1687 DriverOptionHelpStrDepository
1689 LoadOptionPtr
+= NewLoadContext
->FilePathListLength
;
1691 if (LoadOptionPtr
< LoadOptionEnd
) {
1692 OptionalDataSize
= DriverOptionSize
-
1696 NewLoadContext
->FilePathListLength
;
1698 NewLoadContext
->OptionalData
= EfiAllocateZeroPool (OptionalDataSize
);
1699 ASSERT (NewLoadContext
->OptionalData
!= NULL
);
1701 NewLoadContext
->OptionalData
,
1706 NewLoadContext
->OptionalDataSize
= OptionalDataSize
;
1709 InsertTailList (&DriverOptionMenu
.Head
, &NewMenuEntry
->Link
);
1713 SafeFreePool (DriverOrderList
);
1714 DriverOptionMenu
.MenuNumber
= Index
;