200ea9580877a41648a4f4ebe4e28930491b05d2
[mirror_edk2.git] / CorebootPayloadPkg / Library / PlatformBootManagerLib / PlatformBootManager.c
1 /** @file
2 This file include all platform action which can be customized
3 by IBV/OEM.
4
5 Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16 #include "PlatformBootManager.h"
17 #include "PlatformConsole.h"
18
19 /**
20 Return the index of the load option in the load option array.
21
22 The function consider two load options are equal when the
23 OptionType, Attributes, Description, FilePath and OptionalData are equal.
24
25 @param Key Pointer to the load option to be found.
26 @param Array Pointer to the array of load options to be found.
27 @param Count Number of entries in the Array.
28
29 @retval -1 Key wasn't found in the Array.
30 @retval 0 ~ Count-1 The index of the Key in the Array.
31 **/
32 INTN
33 PlatformFindLoadOption (
34 IN CONST EFI_BOOT_MANAGER_LOAD_OPTION *Key,
35 IN CONST EFI_BOOT_MANAGER_LOAD_OPTION *Array,
36 IN UINTN Count
37 )
38 {
39 UINTN Index;
40
41 for (Index = 0; Index < Count; Index++) {
42 if ((Key->OptionType == Array[Index].OptionType) &&
43 (Key->Attributes == Array[Index].Attributes) &&
44 (StrCmp (Key->Description, Array[Index].Description) == 0) &&
45 (CompareMem (Key->FilePath, Array[Index].FilePath, GetDevicePathSize (Key->FilePath)) == 0) &&
46 (Key->OptionalDataSize == Array[Index].OptionalDataSize) &&
47 (CompareMem (Key->OptionalData, Array[Index].OptionalData, Key->OptionalDataSize) == 0)) {
48 return (INTN) Index;
49 }
50 }
51
52 return -1;
53 }
54
55 /**
56 Register a boot option using a file GUID in the FV.
57
58 @param FileGuid The file GUID name in FV.
59 @param Description The boot option description.
60 @param Attributes The attributes used for the boot option loading.
61 **/
62 VOID
63 PlatformRegisterFvBootOption (
64 EFI_GUID *FileGuid,
65 CHAR16 *Description,
66 UINT32 Attributes
67 )
68 {
69 EFI_STATUS Status;
70 UINTN OptionIndex;
71 EFI_BOOT_MANAGER_LOAD_OPTION NewOption;
72 EFI_BOOT_MANAGER_LOAD_OPTION *BootOptions;
73 UINTN BootOptionCount;
74 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH FileNode;
75 EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;
76 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
77
78 Status = gBS->HandleProtocol (gImageHandle, &gEfiLoadedImageProtocolGuid, (VOID **) &LoadedImage);
79 ASSERT_EFI_ERROR (Status);
80
81 EfiInitializeFwVolDevicepathNode (&FileNode, FileGuid);
82 DevicePath = AppendDevicePathNode (
83 DevicePathFromHandle (LoadedImage->DeviceHandle),
84 (EFI_DEVICE_PATH_PROTOCOL *) &FileNode
85 );
86
87 Status = EfiBootManagerInitializeLoadOption (
88 &NewOption,
89 LoadOptionNumberUnassigned,
90 LoadOptionTypeBoot,
91 Attributes,
92 Description,
93 DevicePath,
94 NULL,
95 0
96 );
97 if (!EFI_ERROR (Status)) {
98 BootOptions = EfiBootManagerGetLoadOptions (&BootOptionCount, LoadOptionTypeBoot);
99
100 OptionIndex = PlatformFindLoadOption (&NewOption, BootOptions, BootOptionCount);
101
102 if (OptionIndex == -1) {
103 Status = EfiBootManagerAddLoadOptionVariable (&NewOption, (UINTN) -1);
104 ASSERT_EFI_ERROR (Status);
105 }
106 EfiBootManagerFreeLoadOption (&NewOption);
107 EfiBootManagerFreeLoadOptions (BootOptions, BootOptionCount);
108 }
109 }
110
111 /**
112 Do the platform specific action before the console is connected.
113
114 Such as:
115 Update console variable;
116 Register new Driver#### or Boot####;
117 Signal ReadyToLock event.
118 **/
119 VOID
120 EFIAPI
121 PlatformBootManagerBeforeConsole (
122 VOID
123 )
124 {
125 EFI_INPUT_KEY Enter;
126 EFI_INPUT_KEY F2;
127 EFI_BOOT_MANAGER_LOAD_OPTION BootOption;
128
129 PlatformConsoleInit ();
130
131 //
132 // Register ENTER as CONTINUE key
133 //
134 Enter.ScanCode = SCAN_NULL;
135 Enter.UnicodeChar = CHAR_CARRIAGE_RETURN;
136 EfiBootManagerRegisterContinueKeyOption (0, &Enter, NULL);
137
138 //
139 // Map F2 to Boot Manager Menu
140 //
141 F2.ScanCode = SCAN_F2;
142 F2.UnicodeChar = CHAR_NULL;
143 EfiBootManagerGetBootManagerMenu (&BootOption);
144 EfiBootManagerAddKeyOptionVariable (NULL, (UINT16) BootOption.OptionNumber, 0, &F2, NULL);
145
146 //
147 // Register UEFI Shell
148 //
149 PlatformRegisterFvBootOption (PcdGetPtr (PcdShellFile), L"UEFI Shell", LOAD_OPTION_ACTIVE);
150 }
151
152 /**
153 Do the platform specific action after the console is connected.
154
155 Such as:
156 Dynamically switch output mode;
157 Signal console ready platform customized event;
158 Run diagnostics like memory testing;
159 Connect certain devices;
160 Dispatch aditional option roms.
161 **/
162 VOID
163 EFIAPI
164 PlatformBootManagerAfterConsole (
165 VOID
166 )
167 {
168 EFI_GRAPHICS_OUTPUT_BLT_PIXEL Black;
169 EFI_GRAPHICS_OUTPUT_BLT_PIXEL White;
170
171 Black.Blue = Black.Green = Black.Red = Black.Reserved = 0;
172 White.Blue = White.Green = White.Red = White.Reserved = 0xFF;
173
174 EfiBootManagerConnectAll ();
175 EfiBootManagerRefreshAllBootOption ();
176
177 Print (
178 L"\n"
179 L"F2 to enter Boot Manager Menu.\n"
180 L"ENTER to boot directly.\n"
181 L"\n"
182 );
183
184 }
185
186 /**
187 This function is called each second during the boot manager waits the timeout.
188
189 @param TimeoutRemain The remaining timeout.
190 **/
191 VOID
192 EFIAPI
193 PlatformBootManagerWaitCallback (
194 UINT16 TimeoutRemain
195 )
196 {
197 return;
198 }