a527a4233db8f71dd463ebd5c9667a9f78914519
[mirror_edk2.git] / Nt32Pkg / 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
18
19 EFI_GUID mUefiShellFileGuid = { 0x7C04A583, 0x9E3E, 0x4f1c, 0xAD, 0x65, 0xE0, 0x52, 0x68, 0xD0, 0xB4, 0xD1 };
20
21 /**
22 Return the index of the load option in the load option array.
23
24 The function consider two load options are equal when the
25 OptionType, Attributes, Description, FilePath and OptionalData are equal.
26
27 @param Key Pointer to the load option to be found.
28 @param Array Pointer to the array of load options to be found.
29 @param Count Number of entries in the Array.
30
31 @retval -1 Key wasn't found in the Array.
32 @retval 0 ~ Count-1 The index of the Key in the Array.
33 **/
34 INTN
35 PlatformFindLoadOption (
36 IN CONST EFI_BOOT_MANAGER_LOAD_OPTION *Key,
37 IN CONST EFI_BOOT_MANAGER_LOAD_OPTION *Array,
38 IN UINTN Count
39 )
40 {
41 UINTN Index;
42
43 for (Index = 0; Index < Count; Index++) {
44 if ((Key->OptionType == Array[Index].OptionType) &&
45 (Key->Attributes == Array[Index].Attributes) &&
46 (StrCmp (Key->Description, Array[Index].Description) == 0) &&
47 (CompareMem (Key->FilePath, Array[Index].FilePath, GetDevicePathSize (Key->FilePath)) == 0) &&
48 (Key->OptionalDataSize == Array[Index].OptionalDataSize) &&
49 (CompareMem (Key->OptionalData, Array[Index].OptionalData, Key->OptionalDataSize) == 0)) {
50 return (INTN) Index;
51 }
52 }
53
54 return -1;
55 }
56
57 VOID
58 PlatformRegisterFvBootOption (
59 EFI_GUID *FileGuid,
60 CHAR16 *Description,
61 UINT32 Attributes
62 )
63 {
64 EFI_STATUS Status;
65 UINTN OptionIndex;
66 EFI_BOOT_MANAGER_LOAD_OPTION NewOption;
67 EFI_BOOT_MANAGER_LOAD_OPTION *BootOptions;
68 UINTN BootOptionCount;
69 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH FileNode;
70 EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;
71 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
72
73 Status = gBS->HandleProtocol (gImageHandle, &gEfiLoadedImageProtocolGuid, (VOID **) &LoadedImage);
74 ASSERT_EFI_ERROR (Status);
75
76 EfiInitializeFwVolDevicepathNode (&FileNode, FileGuid);
77 DevicePath = AppendDevicePathNode (
78 DevicePathFromHandle (LoadedImage->DeviceHandle),
79 (EFI_DEVICE_PATH_PROTOCOL *) &FileNode
80 );
81
82 Status = EfiBootManagerInitializeLoadOption (
83 &NewOption,
84 LoadOptionNumberUnassigned,
85 LoadOptionTypeBoot,
86 Attributes,
87 Description,
88 DevicePath,
89 NULL,
90 0
91 );
92 if (!EFI_ERROR (Status)) {
93 BootOptions = EfiBootManagerGetLoadOptions (&BootOptionCount, LoadOptionTypeBoot);
94
95 OptionIndex = PlatformFindLoadOption (&NewOption, BootOptions, BootOptionCount);
96
97 if (OptionIndex == -1) {
98 Status = EfiBootManagerAddLoadOptionVariable (&NewOption, (UINTN) -1);
99 ASSERT_EFI_ERROR (Status);
100 }
101 EfiBootManagerFreeLoadOption (&NewOption);
102 EfiBootManagerFreeLoadOptions (BootOptions, BootOptionCount);
103 }
104 }
105
106 /**
107 Do the platform specific action before the console is connected.
108
109 Such as:
110 Update console variable;
111 Register new Driver#### or Boot####;
112 Signal ReadyToLock event.
113 **/
114 VOID
115 EFIAPI
116 PlatformBootManagerBeforeConsole (
117 VOID
118 )
119 {
120 UINTN Index;
121 EFI_STATUS Status;
122 WIN_NT_SYSTEM_CONFIGURATION *Configuration;
123 EFI_INPUT_KEY Enter;
124 EFI_INPUT_KEY F2;
125 EFI_BOOT_MANAGER_LOAD_OPTION BootOption;
126
127 GetVariable2 (L"Setup", &gEfiWinNtSystemConfigGuid, (VOID **) &Configuration, NULL);
128 if (Configuration != NULL) {
129 //
130 // SetupVariable is corrupt
131 //
132 Configuration->ConOutRow = PcdGet32 (PcdConOutColumn);
133 Configuration->ConOutColumn = PcdGet32 (PcdConOutRow);
134
135 Status = gRT->SetVariable (
136 L"Setup",
137 &gEfiWinNtSystemConfigGuid,
138 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
139 sizeof (WIN_NT_SYSTEM_CONFIGURATION),
140 Configuration
141 );
142 if (EFI_ERROR (Status)) {
143 DEBUG ((EFI_D_ERROR, "Failed to save Setup Variable to non-volatile storage, Status = %r\n", Status));
144 }
145 FreePool (Configuration);
146 }
147
148 //
149 // Update the ocnsole variables.
150 //
151 for (Index = 0; gPlatformConsole[Index].DevicePath != NULL; Index++) {
152 if ((gPlatformConsole[Index].ConnectType & CONSOLE_IN) == CONSOLE_IN) {
153 EfiBootManagerUpdateConsoleVariable (ConIn, gPlatformConsole[Index].DevicePath, NULL);
154 }
155
156 if ((gPlatformConsole[Index].ConnectType & CONSOLE_OUT) == CONSOLE_OUT) {
157 EfiBootManagerUpdateConsoleVariable (ConOut, gPlatformConsole[Index].DevicePath, NULL);
158 }
159
160 if ((gPlatformConsole[Index].ConnectType & STD_ERROR) == STD_ERROR) {
161 EfiBootManagerUpdateConsoleVariable (ErrOut, gPlatformConsole[Index].DevicePath, NULL);
162 }
163 }
164
165 //
166 // Register ENTER as CONTINUE key
167 //
168 Enter.ScanCode = SCAN_NULL;
169 Enter.UnicodeChar = CHAR_CARRIAGE_RETURN;
170 EfiBootManagerRegisterContinueKeyOption (0, &Enter, NULL);
171 //
172 // Map F2 to Boot Manager Menu
173 //
174 F2.ScanCode = SCAN_F2;
175 F2.UnicodeChar = CHAR_NULL;
176 EfiBootManagerGetBootManagerMenu (&BootOption);
177 EfiBootManagerAddKeyOptionVariable (NULL, (UINT16) BootOption.OptionNumber, 0, &F2, NULL);
178 //
179 // Register UEFI Shell
180 //
181 PlatformRegisterFvBootOption (&mUefiShellFileGuid, L"UEFI Shell", LOAD_OPTION_ACTIVE);
182 }
183
184 /**
185 Do the platform specific action after the console is connected.
186
187 Such as:
188 Dynamically switch output mode;
189 Signal console ready platform customized event;
190 Run diagnostics like memory testing;
191 Connect certain devices;
192 Dispatch aditional option roms.
193 **/
194 VOID
195 EFIAPI
196 PlatformBootManagerAfterConsole (
197 VOID
198 )
199 {
200 EfiBootManagerConnectAll ();
201 EfiBootManagerRefreshAllBootOption ();
202 Print (
203 L"\n"
204 L"F2 to enter Boot Manager Menu.\n"
205 L"Enter to boot directly.\n"
206 L"\n"
207 );
208 }
209
210 /**
211 This function is called each second during the boot manager waits the timeout.
212
213 @param TimeoutRemain The remaining timeout.
214 **/
215 VOID
216 EFIAPI
217 PlatformBootManagerWaitCallback (
218 UINT16 TimeoutRemain
219 )
220 {
221 Print (L"\r%-2d seconds remained...", TimeoutRemain);
222 }