]> git.proxmox.com Git - mirror_edk2.git/blame - IntelFrameworkModulePkg/Universal/BdsDxe/BdsEntry.c
Remove local function IsValidDevicePath and use new interface provided by UefiDeviceP...
[mirror_edk2.git] / IntelFrameworkModulePkg / Universal / BdsDxe / BdsEntry.c
CommitLineData
5c08e117 1/** @file\r
128efbbc 2 This module produce main entry for BDS phase - BdsEntry.\r
5c08e117 3 When this module was dispatched by DxeCore, gEfiBdsArchProtocolGuid will be installed\r
4 which contains interface of BdsEntry.\r
5 After DxeCore finish DXE phase, gEfiBdsArchProtocolGuid->BdsEntry will be invoked\r
6 to enter BDS phase.\r
7\r
2df686c6 8Copyright (c) 2004 - 2012, Intel Corporation. All rights reserved.<BR>\r
180a5a35 9This program and the accompanying materials\r
5c08e117 10are licensed and made available under the terms and conditions of the BSD License\r
11which accompanies this distribution. The full text of the license may be found at\r
12http://opensource.org/licenses/bsd-license.php\r
13\r
14THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
15WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
16\r
17**/\r
18\r
19#include "Bds.h"\r
20#include "Language.h"\r
21#include "FrontPage.h"\r
22#include "Hotkey.h"\r
23#include "HwErrRecSupport.h"\r
24\r
25///\r
26/// BDS arch protocol instance initial value.\r
27///\r
28/// Note: Current BDS not directly get the BootMode, DefaultBoot,\r
29/// TimeoutDefault, MemoryTestLevel value from the BDS arch protocol.\r
128efbbc 30/// Please refer to the library useage of BdsLibGetBootMode, BdsLibGetTimeout\r
5c08e117 31/// and PlatformBdsDiagnostics in BdsPlatform.c\r
32///\r
f7cdf5cd 33EFI_HANDLE gBdsHandle = NULL;\r
34\r
35EFI_BDS_ARCH_PROTOCOL gBds = {\r
36 BdsEntry\r
5c08e117 37};\r
38\r
39UINT16 *mBootNext = NULL;\r
40\r
5c08e117 41/**\r
42\r
43 Install Boot Device Selection Protocol\r
44\r
45 @param ImageHandle The image handle.\r
46 @param SystemTable The system table.\r
47\r
48 @retval EFI_SUCEESS BDS has finished initializing.\r
49 Return the dispatcher and recall BDS.Entry\r
50 @retval Other Return status from AllocatePool() or gBS->InstallProtocolInterface\r
51\r
52**/\r
53EFI_STATUS\r
54EFIAPI\r
55BdsInitialize (\r
56 IN EFI_HANDLE ImageHandle,\r
57 IN EFI_SYSTEM_TABLE *SystemTable\r
58 )\r
59{\r
60 EFI_STATUS Status;\r
61\r
5c08e117 62 //\r
63 // Install protocol interface\r
64 //\r
f7cdf5cd 65 Status = gBS->InstallMultipleProtocolInterfaces (\r
66 &gBdsHandle,\r
67 &gEfiBdsArchProtocolGuid, &gBds,\r
68 NULL\r
5c08e117 69 );\r
70 ASSERT_EFI_ERROR (Status);\r
71\r
72 return Status;\r
73}\r
74\r
75/**\r
76\r
77 This function attempts to boot for the boot order specified\r
78 by platform policy.\r
79\r
80**/\r
81VOID\r
82BdsBootDeviceSelect (\r
83 VOID\r
84 )\r
85{\r
86 EFI_STATUS Status;\r
87 LIST_ENTRY *Link;\r
88 BDS_COMMON_OPTION *BootOption;\r
89 UINTN ExitDataSize;\r
90 CHAR16 *ExitData;\r
91 UINT16 Timeout;\r
92 LIST_ENTRY BootLists;\r
93 CHAR16 Buffer[20];\r
94 BOOLEAN BootNextExist;\r
95 LIST_ENTRY *LinkBootNext;\r
96\r
97 //\r
98 // Got the latest boot option\r
99 //\r
100 BootNextExist = FALSE;\r
101 LinkBootNext = NULL;\r
102 InitializeListHead (&BootLists);\r
103\r
104 //\r
105 // First check the boot next option\r
106 //\r
107 ZeroMem (Buffer, sizeof (Buffer));\r
108\r
109 if (mBootNext != NULL) {\r
110 //\r
111 // Indicate we have the boot next variable, so this time\r
112 // boot will always have this boot option\r
113 //\r
114 BootNextExist = TRUE;\r
115\r
116 //\r
117 // Clear the this variable so it's only exist in this time boot\r
118 //\r
119 gRT->SetVariable (\r
120 L"BootNext",\r
121 &gEfiGlobalVariableGuid,\r
122 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
123 0,\r
124 mBootNext\r
125 );\r
126\r
127 //\r
128 // Add the boot next boot option\r
129 //\r
130 UnicodeSPrint (Buffer, sizeof (Buffer), L"Boot%04x", *mBootNext);\r
131 BootOption = BdsLibVariableToOption (&BootLists, Buffer);\r
128efbbc 132\r
5c08e117 133 //\r
134 // If fail to get boot option from variable, just return and do nothing.\r
135 //\r
136 if (BootOption == NULL) {\r
137 return;\r
138 }\r
128efbbc 139\r
5c08e117 140 BootOption->BootCurrent = *mBootNext;\r
141 }\r
142 //\r
143 // Parse the boot order to get boot option\r
144 //\r
145 BdsLibBuildOptionFromVar (&BootLists, L"BootOrder");\r
d8600108
RN
146\r
147 //\r
148 // When we didn't have chance to build boot option variables in the first \r
149 // full configuration boot (e.g.: Reset in the first page or in Device Manager),\r
150 // we have no boot options in the following mini configuration boot.\r
151 // Give the last chance to enumerate the boot options.\r
152 //\r
153 if (IsListEmpty (&BootLists)) {\r
154 BdsLibEnumerateAllBootOption (&BootLists);\r
155 }\r
156\r
5c08e117 157 Link = BootLists.ForwardLink;\r
158\r
159 //\r
160 // Parameter check, make sure the loop will be valid\r
161 //\r
162 if (Link == NULL) {\r
163 return ;\r
164 }\r
165 //\r
166 // Here we make the boot in a loop, every boot success will\r
167 // return to the front page\r
168 //\r
169 for (;;) {\r
170 //\r
171 // Check the boot option list first\r
172 //\r
173 if (Link == &BootLists) {\r
174 //\r
175 // There are two ways to enter here:\r
176 // 1. There is no active boot option, give user chance to\r
177 // add new boot option\r
178 // 2. All the active boot option processed, and there is no\r
179 // one is success to boot, then we back here to allow user\r
180 // add new active boot option\r
181 //\r
182 Timeout = 0xffff;\r
183 PlatformBdsEnterFrontPage (Timeout, FALSE);\r
184 InitializeListHead (&BootLists);\r
185 BdsLibBuildOptionFromVar (&BootLists, L"BootOrder");\r
186 Link = BootLists.ForwardLink;\r
187 continue;\r
188 }\r
189 //\r
190 // Get the boot option from the link list\r
191 //\r
192 BootOption = CR (Link, BDS_COMMON_OPTION, Link, BDS_LOAD_OPTION_SIGNATURE);\r
193\r
194 //\r
195 // According to EFI Specification, if a load option is not marked\r
196 // as LOAD_OPTION_ACTIVE, the boot manager will not automatically\r
197 // load the option.\r
198 //\r
199 if (!IS_LOAD_OPTION_TYPE (BootOption->Attribute, LOAD_OPTION_ACTIVE)) {\r
200 //\r
baf46e70 201 // skip the header of the link list, because it has no boot option\r
5c08e117 202 //\r
203 Link = Link->ForwardLink;\r
204 continue;\r
205 }\r
206 //\r
207 // Make sure the boot option device path connected,\r
208 // but ignore the BBS device path\r
209 //\r
210 if (DevicePathType (BootOption->DevicePath) != BBS_DEVICE_PATH) {\r
211 //\r
212 // Notes: the internal shell can not been connected with device path\r
213 // so we do not check the status here\r
214 //\r
215 BdsLibConnectDevicePath (BootOption->DevicePath);\r
216 }\r
2df686c6 217\r
218 //\r
219 // Restore to original mode before launching boot option.\r
220 //\r
221 BdsSetConsoleMode (FALSE);\r
222 \r
5c08e117 223 //\r
224 // All the driver options should have been processed since\r
225 // now boot will be performed.\r
226 //\r
227 Status = BdsLibBootViaBootOption (BootOption, BootOption->DevicePath, &ExitDataSize, &ExitData);\r
59b9d73e 228 if (Status != EFI_SUCCESS) {\r
5c08e117 229 //\r
230 // Call platform action to indicate the boot fail\r
231 //\r
232 BootOption->StatusString = GetStringById (STRING_TOKEN (STR_BOOT_FAILED));\r
233 PlatformBdsBootFail (BootOption, Status, ExitData, ExitDataSize);\r
234\r
235 //\r
236 // Check the next boot option\r
237 //\r
238 Link = Link->ForwardLink;\r
239\r
240 } else {\r
241 //\r
242 // Call platform action to indicate the boot success\r
243 //\r
128efbbc 244 BootOption->StatusString = GetStringById (STRING_TOKEN (STR_BOOT_SUCCEEDED));\r
5c08e117 245 PlatformBdsBootSuccess (BootOption);\r
246\r
247 //\r
248 // Boot success, then stop process the boot order, and\r
249 // present the boot manager menu, front page\r
250 //\r
251 Timeout = 0xffff;\r
252 PlatformBdsEnterFrontPage (Timeout, FALSE);\r
253\r
254 //\r
baf46e70 255 // Rescan the boot option list, avoid potential risk of the boot\r
5c08e117 256 // option change in front page\r
257 //\r
258 if (BootNextExist) {\r
259 LinkBootNext = BootLists.ForwardLink;\r
260 }\r
261\r
262 InitializeListHead (&BootLists);\r
263 if (LinkBootNext != NULL) {\r
264 //\r
265 // Reserve the boot next option\r
266 //\r
267 InsertTailList (&BootLists, LinkBootNext);\r
268 }\r
269\r
270 BdsLibBuildOptionFromVar (&BootLists, L"BootOrder");\r
271 Link = BootLists.ForwardLink;\r
272 }\r
273 }\r
274\r
275}\r
276\r
7ea22852
ED
277/**\r
278\r
279 Validate input console variable data. \r
280\r
281 If found the device path is not a valid device path, remove the variable.\r
282 \r
283 @param VariableName Input console variable name.\r
284\r
285**/\r
7ea22852
ED
286VOID\r
287BdsFormalizeConsoleVariable (\r
288 IN CHAR16 *VariableName\r
289 )\r
290{\r
291 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
292 UINTN VariableSize;\r
293 EFI_STATUS Status;\r
294\r
295 DevicePath = BdsLibGetVariableAndSize (\r
296 VariableName,\r
297 &gEfiGlobalVariableGuid,\r
298 &VariableSize\r
299 );\r
98b241d6 300 if ((DevicePath != NULL) && !IsDevicePathValid (DevicePath, VariableSize)) { \r
7ea22852
ED
301 Status = gRT->SetVariable (\r
302 VariableName,\r
303 &gEfiGlobalVariableGuid,\r
304 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
305 0,\r
306 NULL\r
307 );\r
308 ASSERT_EFI_ERROR (Status);\r
309 }\r
310}\r
311\r
312/**\r
313\r
18cf3950 314 Formalize Bds global variables. \r
7ea22852 315\r
18cf3950 316 1. For ConIn/ConOut/ConErr, if found the device path is not a valid device path, remove the variable.\r
317 2. For OsIndicationsSupported, Create a BS/RT/UINT64 variable to report caps \r
318 3. Delete OsIndications variable if it is not NV/BS/RT UINT64\r
319 Item 3 is used to solve case when OS corrupts OsIndications. Here simply delete this NV variable.\r
320 \r
7ea22852
ED
321**/\r
322VOID \r
323BdsFormalizeEfiGlobalVariable (\r
324 VOID\r
325 )\r
326{\r
18cf3950 327 EFI_STATUS Status;\r
328 UINT64 OsIndicationSupport;\r
329 UINT64 OsIndication;\r
330 UINTN DataSize;\r
331 UINT32 Attributes;\r
332 \r
7ea22852
ED
333 //\r
334 // Validate Console variable.\r
335 //\r
336 BdsFormalizeConsoleVariable (L"ConIn");\r
337 BdsFormalizeConsoleVariable (L"ConOut");\r
338 BdsFormalizeConsoleVariable (L"ErrOut");\r
18cf3950 339\r
340 //\r
341 // OS indicater support variable\r
342 //\r
343 OsIndicationSupport = EFI_OS_INDICATIONS_BOOT_TO_FW_UI;\r
344 Status = gRT->SetVariable (\r
345 L"OsIndicationsSupported",\r
346 &gEfiGlobalVariableGuid,\r
347 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r
348 sizeof(UINT64),\r
349 &OsIndicationSupport\r
350 );\r
351 ASSERT_EFI_ERROR (Status);\r
352\r
353 //\r
354 // If OsIndications is invalid, remove it.\r
355 // Invalid case\r
356 // 1. Data size != UINT64\r
357 // 2. OsIndication value inconsistence\r
358 // 3. OsIndication attribute inconsistence\r
359 //\r
360 OsIndication = 0;\r
361 Attributes = 0;\r
362 DataSize = sizeof(UINT64);\r
363 Status = gRT->GetVariable (\r
364 L"OsIndications",\r
365 &gEfiGlobalVariableGuid,\r
366 &Attributes,\r
367 &DataSize,\r
368 &OsIndication\r
369 );\r
370\r
371 if (!EFI_ERROR(Status)) {\r
372 if (DataSize != sizeof(UINT64) ||\r
373 (OsIndication & ~OsIndicationSupport) != 0 ||\r
374 Attributes != (EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE)){\r
375\r
376 DEBUG ((EFI_D_ERROR, "Unformalized OsIndications variable exists. Delete it\n"));\r
377 Status = gRT->SetVariable (\r
378 L"OsIndications",\r
379 &gEfiGlobalVariableGuid,\r
380 Attributes,\r
381 0,\r
382 &OsIndication\r
383 );\r
384 ASSERT_EFI_ERROR (Status);\r
385 }\r
386 }\r
387\r
7ea22852
ED
388}\r
389\r
5c08e117 390/**\r
391\r
392 Service routine for BdsInstance->Entry(). Devices are connected, the\r
393 consoles are initialized, and the boot options are tried.\r
394\r
395 @param This Protocol Instance structure.\r
396\r
397**/\r
398VOID\r
399EFIAPI\r
400BdsEntry (\r
401 IN EFI_BDS_ARCH_PROTOCOL *This\r
402 )\r
403{\r
5c08e117 404 LIST_ENTRY DriverOptionList;\r
405 LIST_ENTRY BootOptionList;\r
406 UINTN BootNextSize;\r
9a6b4de2 407 CHAR16 *FirmwareVendor;\r
5c08e117 408\r
409 //\r
410 // Insert the performance probe\r
411 //\r
128efbbc 412 PERF_END (NULL, "DXE", NULL, 0);\r
413 PERF_START (NULL, "BDS", NULL, 0);\r
5c08e117 414\r
415 //\r
416 // Initialize the global system boot option and driver option\r
417 //\r
418 InitializeListHead (&DriverOptionList);\r
419 InitializeListHead (&BootOptionList);\r
420\r
421 //\r
422 // Initialize hotkey service\r
423 //\r
424 InitializeHotkeyService ();\r
425\r
9a6b4de2 426 //\r
427 // Fill in FirmwareVendor and FirmwareRevision from PCDs\r
428 //\r
429 FirmwareVendor = (CHAR16 *)PcdGetPtr (PcdFirmwareVendor);\r
430 gST->FirmwareVendor = AllocateRuntimeCopyPool (StrSize (FirmwareVendor), FirmwareVendor);\r
431 ASSERT (gST->FirmwareVendor != NULL);\r
432 gST->FirmwareRevision = PcdGet32 (PcdFirmwareRevision);\r
433\r
434 //\r
435 // Fixup Tasble CRC after we updated Firmware Vendor and Revision\r
436 //\r
437 gBS->CalculateCrc32 ((VOID *)gST, sizeof(EFI_SYSTEM_TABLE), &gST->Hdr.CRC32);\r
438\r
7ea22852
ED
439 //\r
440 // Validate Variable.\r
441 //\r
442 BdsFormalizeEfiGlobalVariable();\r
443\r
5c08e117 444 //\r
445 // Do the platform init, can be customized by OEM/IBV\r
446 //\r
128efbbc 447 PERF_START (NULL, "PlatformBds", "BDS", 0);\r
f7cdf5cd 448 PlatformBdsInit ();\r
5c08e117 449\r
a2b35995 450 InitializeHwErrRecSupport();\r
128efbbc 451\r
5c08e117 452 //\r
453 // bugbug: platform specific code\r
454 // Initialize the platform specific string and language\r
455 //\r
456 InitializeStringSupport ();\r
457 InitializeLanguage (TRUE);\r
458 InitializeFrontPage (TRUE);\r
459\r
460 //\r
461 // Set up the device list based on EFI 1.1 variables\r
462 // process Driver#### and Load the driver's in the\r
463 // driver option list\r
464 //\r
465 BdsLibBuildOptionFromVar (&DriverOptionList, L"DriverOrder");\r
466 if (!IsListEmpty (&DriverOptionList)) {\r
467 BdsLibLoadDrivers (&DriverOptionList);\r
468 }\r
469 //\r
470 // Check if we have the boot next option\r
471 //\r
472 mBootNext = BdsLibGetVariableAndSize (\r
473 L"BootNext",\r
474 &gEfiGlobalVariableGuid,\r
475 &BootNextSize\r
476 );\r
477\r
478 //\r
479 // Setup some platform policy here\r
480 //\r
19bf20e1 481 PlatformBdsPolicyBehavior (&DriverOptionList, &BootOptionList, BdsProcessCapsules, BdsMemoryTest);\r
128efbbc 482 PERF_END (NULL, "PlatformBds", "BDS", 0);\r
5c08e117 483\r
484 //\r
485 // BDS select the boot device to load OS\r
486 //\r
487 BdsBootDeviceSelect ();\r
488\r
489 //\r
490 // Only assert here since this is the right behavior, we should never\r
491 // return back to DxeCore.\r
492 //\r
493 ASSERT (FALSE);\r
494\r
495 return ;\r
496}\r