]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/BdsDxe/BdsEntry.c
MdeModulePkg: Skip registering BootManagerMenu if absent
[mirror_edk2.git] / MdeModulePkg / Universal / BdsDxe / BdsEntry.c
CommitLineData
f4cd24da
RN
1/** @file\r
2 This module produce main entry for BDS phase - BdsEntry.\r
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
f5cbc197 8Copyright (c) 2004 - 2016, Intel Corporation. All rights reserved.<BR>\r
e58f1ae5
SW
9(C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>\r
10(C) Copyright 2015 Hewlett-Packard Development Company, L.P.<BR>\r
f4cd24da
RN
11This program and the accompanying materials\r
12are licensed and made available under the terms and conditions of the BSD License\r
13which accompanies this distribution. The full text of the license may be found at\r
14http://opensource.org/licenses/bsd-license.php\r
15\r
16THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
17WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
18\r
19**/\r
20\r
21#include "Bds.h"\r
22#include "Language.h"\r
23#include "HwErrRecSupport.h"\r
24\r
25#define SET_BOOT_OPTION_SUPPORT_KEY_COUNT(a, c) { \\r
26 (a) = ((a) & ~EFI_BOOT_OPTION_SUPPORT_COUNT) | (((c) << LowBitSet32 (EFI_BOOT_OPTION_SUPPORT_COUNT)) & EFI_BOOT_OPTION_SUPPORT_COUNT); \\r
27 }\r
28\r
29///\r
30/// BDS arch protocol instance initial value.\r
31///\r
32EFI_BDS_ARCH_PROTOCOL gBds = {\r
33 BdsEntry\r
34};\r
35\r
36//\r
37// gConnectConInEvent - Event which is signaled when ConIn connection is required\r
38//\r
39EFI_EVENT gConnectConInEvent = NULL;\r
40\r
41///\r
42/// The read-only variables defined in UEFI Spec.\r
43///\r
44CHAR16 *mReadOnlyVariables[] = {\r
45 EFI_PLATFORM_LANG_CODES_VARIABLE_NAME,\r
46 EFI_LANG_CODES_VARIABLE_NAME,\r
47 EFI_BOOT_OPTION_SUPPORT_VARIABLE_NAME,\r
48 EFI_HW_ERR_REC_SUPPORT_VARIABLE_NAME,\r
49 EFI_OS_INDICATIONS_SUPPORT_VARIABLE_NAME\r
50 };\r
51\r
1634214d
RN
52CHAR16 *mBdsLoadOptionName[] = {\r
53 L"Driver",\r
54 L"SysPrep",\r
68456d8a
RN
55 L"Boot",\r
56 L"PlatformRecovery"\r
1634214d
RN
57};\r
58\r
f4cd24da
RN
59/**\r
60 Event to Connect ConIn.\r
61\r
62 @param Event Event whose notification function is being invoked.\r
63 @param Context Pointer to the notification function's context,\r
64 which is implementation-dependent.\r
65\r
66**/\r
67VOID\r
68EFIAPI\r
69BdsDxeOnConnectConInCallBack (\r
70 IN EFI_EVENT Event,\r
71 IN VOID *Context\r
72 )\r
73{\r
74 EFI_STATUS Status;\r
75\r
76 //\r
77 // When Osloader call ReadKeyStroke to signal this event\r
78 // no driver dependency is assumed existing. So use a non-dispatch version\r
79 //\r
80 Status = EfiBootManagerConnectConsoleVariable (ConIn);\r
81 if (EFI_ERROR (Status)) {\r
82 //\r
83 // Should not enter this case, if enter, the keyboard will not work.\r
84 // May need platfrom policy to connect keyboard.\r
85 //\r
1634214d 86 DEBUG ((EFI_D_WARN, "[Bds] Connect ConIn failed - %r!!!\n", Status));\r
f4cd24da
RN
87 }\r
88}\r
89\r
90/**\r
91\r
92 Install Boot Device Selection Protocol\r
93\r
94 @param ImageHandle The image handle.\r
95 @param SystemTable The system table.\r
96\r
97 @retval EFI_SUCEESS BDS has finished initializing.\r
98 Return the dispatcher and recall BDS.Entry\r
99 @retval Other Return status from AllocatePool() or gBS->InstallProtocolInterface\r
100\r
101**/\r
102EFI_STATUS\r
103EFIAPI\r
104BdsInitialize (\r
105 IN EFI_HANDLE ImageHandle,\r
106 IN EFI_SYSTEM_TABLE *SystemTable\r
107 )\r
108{\r
109 EFI_STATUS Status;\r
110 EFI_HANDLE Handle;\r
111 //\r
112 // Install protocol interface\r
113 //\r
114 Handle = NULL;\r
115 Status = gBS->InstallMultipleProtocolInterfaces (\r
116 &Handle,\r
117 &gEfiBdsArchProtocolGuid, &gBds,\r
118 NULL\r
119 );\r
120 ASSERT_EFI_ERROR (Status);\r
121\r
122 return Status;\r
123}\r
124\r
f4cd24da
RN
125/**\r
126 Function waits for a given event to fire, or for an optional timeout to expire.\r
127\r
128 @param Event The event to wait for\r
129 @param Timeout An optional timeout value in 100 ns units.\r
130\r
131 @retval EFI_SUCCESS Event fired before Timeout expired.\r
132 @retval EFI_TIME_OUT Timout expired before Event fired..\r
133\r
134**/\r
135EFI_STATUS\r
136BdsWaitForSingleEvent (\r
137 IN EFI_EVENT Event,\r
138 IN UINT64 Timeout OPTIONAL\r
139 )\r
140{\r
141 UINTN Index;\r
142 EFI_STATUS Status;\r
143 EFI_EVENT TimerEvent;\r
144 EFI_EVENT WaitList[2];\r
145\r
146 if (Timeout != 0) {\r
147 //\r
148 // Create a timer event\r
149 //\r
150 Status = gBS->CreateEvent (EVT_TIMER, 0, NULL, NULL, &TimerEvent);\r
151 if (!EFI_ERROR (Status)) {\r
152 //\r
153 // Set the timer event\r
154 //\r
155 gBS->SetTimer (\r
156 TimerEvent,\r
157 TimerRelative,\r
158 Timeout\r
159 );\r
160\r
161 //\r
162 // Wait for the original event or the timer\r
163 //\r
164 WaitList[0] = Event;\r
165 WaitList[1] = TimerEvent;\r
166 Status = gBS->WaitForEvent (2, WaitList, &Index);\r
167 ASSERT_EFI_ERROR (Status);\r
168 gBS->CloseEvent (TimerEvent);\r
169\r
170 //\r
171 // If the timer expired, change the return to timed out\r
172 //\r
173 if (Index == 1) {\r
174 Status = EFI_TIMEOUT;\r
175 }\r
176 }\r
177 } else {\r
178 //\r
179 // No timeout... just wait on the event\r
180 //\r
181 Status = gBS->WaitForEvent (1, &Event, &Index);\r
182 ASSERT (!EFI_ERROR (Status));\r
183 ASSERT (Index == 0);\r
184 }\r
185\r
186 return Status;\r
187}\r
188\r
189/**\r
190 The function reads user inputs.\r
191\r
192**/\r
193VOID\r
194BdsReadKeys (\r
195 VOID\r
196 )\r
197{\r
198 EFI_STATUS Status;\r
199 EFI_INPUT_KEY Key;\r
200\r
201 if (PcdGetBool (PcdConInConnectOnDemand)) {\r
202 return;\r
203 }\r
204\r
205 while (gST->ConIn != NULL) {\r
206 \r
207 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);\r
208 \r
209 if (EFI_ERROR (Status)) {\r
210 //\r
211 // No more keys.\r
212 //\r
213 break;\r
214 }\r
215 }\r
216}\r
217\r
218/**\r
219 The function waits for the boot manager timeout expires or hotkey is pressed.\r
220\r
221 It calls PlatformBootManagerWaitCallback each second.\r
222\r
223 @param HotkeyTriggered Input hotkey event.\r
224**/\r
225VOID\r
226BdsWait (\r
227 IN EFI_EVENT HotkeyTriggered\r
228 )\r
229{\r
230 EFI_STATUS Status;\r
231 UINT16 TimeoutRemain;\r
232\r
233 DEBUG ((EFI_D_INFO, "[Bds]BdsWait ...Zzzzzzzzzzzz...\n"));\r
234\r
235 TimeoutRemain = PcdGet16 (PcdPlatformBootTimeOut);\r
236 while (TimeoutRemain != 0) {\r
237 DEBUG ((EFI_D_INFO, "[Bds]BdsWait(%d)..Zzzz...\n", (UINTN) TimeoutRemain));\r
238 PlatformBootManagerWaitCallback (TimeoutRemain);\r
239\r
240 BdsReadKeys (); // BUGBUG: Only reading can signal HotkeyTriggered\r
241 // Can be removed after all keyboard drivers invoke callback in timer callback.\r
242\r
243 if (HotkeyTriggered != NULL) {\r
244 Status = BdsWaitForSingleEvent (HotkeyTriggered, EFI_TIMER_PERIOD_SECONDS (1));\r
245 if (!EFI_ERROR (Status)) {\r
246 break;\r
247 }\r
248 } else {\r
249 gBS->Stall (1000000);\r
250 }\r
251\r
252 //\r
253 // 0xffff means waiting forever\r
254 // BDS with no hotkey provided and 0xffff as timeout will "hang" in the loop\r
255 //\r
256 if (TimeoutRemain != 0xffff) {\r
257 TimeoutRemain--;\r
258 }\r
259 }\r
260 DEBUG ((EFI_D_INFO, "[Bds]Exit the waiting!\n"));\r
261}\r
262\r
263/**\r
264 Attempt to boot each boot option in the BootOptions array.\r
265\r
266 @param BootOptions Input boot option array.\r
267 @param BootOptionCount Input boot option count.\r
68456d8a 268 @param BootManagerMenu Input boot manager menu.\r
f4cd24da
RN
269\r
270 @retval TRUE Successfully boot one of the boot options.\r
271 @retval FALSE Failed boot any of the boot options.\r
272**/\r
273BOOLEAN\r
68456d8a 274BootBootOptions (\r
f4cd24da 275 IN EFI_BOOT_MANAGER_LOAD_OPTION *BootOptions,\r
68456d8a 276 IN UINTN BootOptionCount,\r
e58f1ae5 277 IN EFI_BOOT_MANAGER_LOAD_OPTION *BootManagerMenu OPTIONAL\r
f4cd24da
RN
278 )\r
279{\r
280 UINTN Index;\r
281\r
282 //\r
283 // Attempt boot each boot option\r
284 //\r
285 for (Index = 0; Index < BootOptionCount; Index++) {\r
286 //\r
287 // According to EFI Specification, if a load option is not marked\r
288 // as LOAD_OPTION_ACTIVE, the boot manager will not automatically\r
289 // load the option.\r
290 //\r
291 if ((BootOptions[Index].Attributes & LOAD_OPTION_ACTIVE) == 0) {\r
292 continue;\r
293 }\r
294\r
295 //\r
296 // Boot#### load options with LOAD_OPTION_CATEGORY_APP are executables which are not\r
297 // part of the normal boot processing. Boot options with reserved category values will be\r
298 // ignored by the boot manager.\r
299 //\r
300 if ((BootOptions[Index].Attributes & LOAD_OPTION_CATEGORY) != LOAD_OPTION_CATEGORY_BOOT) {\r
301 continue;\r
302 }\r
303\r
304 //\r
305 // All the driver options should have been processed since\r
306 // now boot will be performed.\r
307 //\r
308 EfiBootManagerBoot (&BootOptions[Index]);\r
309\r
310 //\r
311b5a6f
ED
311 // If the boot via Boot#### returns with a status of EFI_SUCCESS, platform firmware\r
312 // supports boot manager menu, and if firmware is configured to boot in an\r
313 // interactive mode, the boot manager will stop processing the BootOrder variable and\r
314 // present a boot manager menu to the user.\r
f4cd24da 315 //\r
e58f1ae5 316 if ((BootManagerMenu != NULL) && (BootOptions[Index].Status == EFI_SUCCESS)) {\r
68456d8a 317 EfiBootManagerBoot (BootManagerMenu);\r
f4cd24da
RN
318 break;\r
319 }\r
320 }\r
321\r
322 return (BOOLEAN) (Index < BootOptionCount);\r
323}\r
324\r
325/**\r
68456d8a 326 The function will load and start every Driver####, SysPrep#### or PlatformRecovery####.\r
f4cd24da 327\r
1634214d
RN
328 @param LoadOptions Load option array.\r
329 @param LoadOptionCount Load option count.\r
f4cd24da
RN
330**/\r
331VOID\r
1634214d
RN
332ProcessLoadOptions (\r
333 IN EFI_BOOT_MANAGER_LOAD_OPTION *LoadOptions,\r
334 IN UINTN LoadOptionCount\r
f4cd24da
RN
335 )\r
336{\r
1634214d
RN
337 EFI_STATUS Status;\r
338 UINTN Index;\r
339 BOOLEAN ReconnectAll;\r
340 EFI_BOOT_MANAGER_LOAD_OPTION_TYPE LoadOptionType;\r
f4cd24da 341\r
68456d8a 342 ReconnectAll = FALSE;\r
1634214d 343 LoadOptionType = LoadOptionTypeMax;\r
f4cd24da
RN
344\r
345 //\r
346 // Process the driver option\r
347 //\r
1634214d 348 for (Index = 0; Index < LoadOptionCount; Index++) {\r
f4cd24da 349 //\r
1634214d 350 // All the load options in the array should be of the same type.\r
f4cd24da 351 //\r
68456d8a 352 if (Index == 0) {\r
1634214d 353 LoadOptionType = LoadOptions[Index].OptionType;\r
f4cd24da 354 }\r
1634214d 355 ASSERT (LoadOptionType == LoadOptions[Index].OptionType);\r
f4cd24da 356\r
1634214d 357 Status = EfiBootManagerProcessLoadOption (&LoadOptions[Index]);\r
f4cd24da 358\r
68456d8a
RN
359 if (!EFI_ERROR (Status)) {\r
360 if (LoadOptionType == LoadOptionTypePlatformRecovery) {\r
361 //\r
362 // Stop processing if any entry is successful\r
363 //\r
364 break;\r
365 }\r
366 if ((LoadOptions[Index].Attributes & LOAD_OPTION_FORCE_RECONNECT) != 0) {\r
367 ReconnectAll = TRUE;\r
368 }\r
f4cd24da
RN
369 }\r
370 }\r
1634214d 371\r
f4cd24da 372 //\r
1634214d
RN
373 // If a driver load option is marked as LOAD_OPTION_FORCE_RECONNECT,\r
374 // then all of the EFI drivers in the system will be disconnected and\r
375 // reconnected after the last driver load option is processed.\r
f4cd24da 376 //\r
1634214d 377 if (ReconnectAll && LoadOptionType == LoadOptionTypeDriver) {\r
f4cd24da
RN
378 EfiBootManagerDisconnectAll ();\r
379 EfiBootManagerConnectAll ();\r
380 }\r
f4cd24da
RN
381}\r
382\r
383/**\r
384\r
385 Validate input console variable data. \r
386\r
387 If found the device path is not a valid device path, remove the variable.\r
388 \r
389 @param VariableName Input console variable name.\r
390\r
391**/\r
392VOID\r
393BdsFormalizeConsoleVariable (\r
394 IN CHAR16 *VariableName\r
395 )\r
396{\r
397 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
398 UINTN VariableSize;\r
399 EFI_STATUS Status;\r
400\r
401 GetEfiGlobalVariable2 (VariableName, (VOID **) &DevicePath, &VariableSize);\r
402 if ((DevicePath != NULL) && !IsDevicePathValid (DevicePath, VariableSize)) { \r
403 Status = gRT->SetVariable (\r
404 VariableName,\r
405 &gEfiGlobalVariableGuid,\r
406 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
407 0,\r
408 NULL\r
409 );\r
410 //\r
411 // Deleting variable with current variable implementation shouldn't fail.\r
412 //\r
413 ASSERT_EFI_ERROR (Status);\r
414 }\r
415\r
416 if (DevicePath != NULL) {\r
417 FreePool (DevicePath);\r
418 }\r
419}\r
420\r
421/**\r
422 Formalize OsIndication related variables. \r
423 \r
424 For OsIndicationsSupported, Create a BS/RT/UINT64 variable to report caps \r
425 Delete OsIndications variable if it is not NV/BS/RT UINT64.\r
426 \r
427 Item 3 is used to solve case when OS corrupts OsIndications. Here simply delete this NV variable.\r
428\r
e58f1ae5
SW
429 Create a boot option for BootManagerMenu if it hasn't been created yet\r
430\r
f4cd24da
RN
431**/\r
432VOID \r
433BdsFormalizeOSIndicationVariable (\r
434 VOID\r
435 )\r
436{\r
e58f1ae5
SW
437 EFI_STATUS Status;\r
438 UINT64 OsIndicationSupport;\r
439 UINT64 OsIndication;\r
440 UINTN DataSize;\r
441 UINT32 Attributes;\r
442 EFI_BOOT_MANAGER_LOAD_OPTION BootManagerMenu;\r
f4cd24da
RN
443\r
444 //\r
445 // OS indicater support variable\r
446 //\r
e58f1ae5
SW
447 Status = EfiBootManagerGetBootManagerMenu (&BootManagerMenu);\r
448 if (Status != EFI_NOT_FOUND) {\r
449 OsIndicationSupport = EFI_OS_INDICATIONS_BOOT_TO_FW_UI | EFI_OS_INDICATIONS_START_PLATFORM_RECOVERY;\r
450 EfiBootManagerFreeLoadOption (&BootManagerMenu);\r
451 } else {\r
452 OsIndicationSupport = EFI_OS_INDICATIONS_START_PLATFORM_RECOVERY;\r
453 }\r
454\r
f4cd24da 455 Status = gRT->SetVariable (\r
cc4812f6 456 EFI_OS_INDICATIONS_SUPPORT_VARIABLE_NAME,\r
f4cd24da
RN
457 &gEfiGlobalVariableGuid,\r
458 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r
459 sizeof(UINT64),\r
460 &OsIndicationSupport\r
461 );\r
462 //\r
463 // Platform needs to make sure setting volatile variable before calling 3rd party code shouldn't fail.\r
464 //\r
465 ASSERT_EFI_ERROR (Status);\r
466\r
467 //\r
468 // If OsIndications is invalid, remove it.\r
469 // Invalid case\r
470 // 1. Data size != UINT64\r
471 // 2. OsIndication value inconsistence\r
472 // 3. OsIndication attribute inconsistence\r
473 //\r
474 OsIndication = 0;\r
475 Attributes = 0;\r
476 DataSize = sizeof(UINT64);\r
477 Status = gRT->GetVariable (\r
cc4812f6 478 EFI_OS_INDICATIONS_VARIABLE_NAME,\r
f4cd24da
RN
479 &gEfiGlobalVariableGuid,\r
480 &Attributes,\r
481 &DataSize,\r
482 &OsIndication\r
483 );\r
484 if (Status == EFI_NOT_FOUND) {\r
485 return;\r
486 }\r
487\r
488 if ((DataSize != sizeof (OsIndication)) ||\r
489 ((OsIndication & ~OsIndicationSupport) != 0) ||\r
490 (Attributes != (EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE))\r
491 ){\r
492\r
493 DEBUG ((EFI_D_ERROR, "[Bds] Unformalized OsIndications variable exists. Delete it\n"));\r
494 Status = gRT->SetVariable (\r
cc4812f6 495 EFI_OS_INDICATIONS_VARIABLE_NAME,\r
f4cd24da
RN
496 &gEfiGlobalVariableGuid,\r
497 0,\r
498 0,\r
499 NULL\r
500 );\r
501 //\r
502 // Deleting variable with current variable implementation shouldn't fail.\r
503 //\r
504 ASSERT_EFI_ERROR(Status);\r
505 }\r
506}\r
507\r
508/**\r
509\r
510 Validate variables. \r
511\r
512**/\r
513VOID \r
514BdsFormalizeEfiGlobalVariable (\r
515 VOID\r
516 )\r
517{\r
518 //\r
519 // Validate Console variable.\r
520 //\r
cc4812f6
RN
521 BdsFormalizeConsoleVariable (EFI_CON_IN_VARIABLE_NAME);\r
522 BdsFormalizeConsoleVariable (EFI_CON_OUT_VARIABLE_NAME);\r
523 BdsFormalizeConsoleVariable (EFI_ERR_OUT_VARIABLE_NAME);\r
f4cd24da
RN
524\r
525 //\r
526 // Validate OSIndication related variable.\r
527 //\r
528 BdsFormalizeOSIndicationVariable ();\r
529}\r
530\r
531/**\r
532\r
533 Allocate a block of memory that will contain performance data to OS.\r
534\r
535**/\r
536VOID\r
537BdsAllocateMemoryForPerformanceData (\r
538 VOID\r
539 )\r
540{\r
541 EFI_STATUS Status;\r
542 EFI_PHYSICAL_ADDRESS AcpiLowMemoryBase;\r
543 EDKII_VARIABLE_LOCK_PROTOCOL *VariableLock;\r
544\r
545 AcpiLowMemoryBase = 0x0FFFFFFFFULL;\r
546\r
547 //\r
548 // Allocate a block of memory that will contain performance data to OS.\r
549 //\r
550 Status = gBS->AllocatePages (\r
551 AllocateMaxAddress,\r
552 EfiReservedMemoryType,\r
553 EFI_SIZE_TO_PAGES (PERF_DATA_MAX_LENGTH),\r
554 &AcpiLowMemoryBase\r
555 );\r
556 if (!EFI_ERROR (Status)) {\r
557 //\r
558 // Save the pointer to variable for use in S3 resume.\r
559 //\r
560 Status = BdsDxeSetVariableAndReportStatusCodeOnError (\r
561 L"PerfDataMemAddr",\r
562 &gPerformanceProtocolGuid,\r
563 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r
564 sizeof (EFI_PHYSICAL_ADDRESS),\r
565 &AcpiLowMemoryBase\r
566 );\r
567 if (EFI_ERROR (Status)) {\r
568 DEBUG ((EFI_D_ERROR, "[Bds] PerfDataMemAddr (%08x) cannot be saved to NV storage.\n", AcpiLowMemoryBase));\r
569 }\r
570 //\r
571 // Mark L"PerfDataMemAddr" variable to read-only if the Variable Lock protocol exists\r
572 // Still lock it even the variable cannot be saved to prevent it's set by 3rd party code.\r
573 //\r
574 Status = gBS->LocateProtocol (&gEdkiiVariableLockProtocolGuid, NULL, (VOID **) &VariableLock);\r
575 if (!EFI_ERROR (Status)) {\r
576 Status = VariableLock->RequestToLock (VariableLock, L"PerfDataMemAddr", &gPerformanceProtocolGuid);\r
577 ASSERT_EFI_ERROR (Status);\r
578 }\r
579 }\r
580}\r
581\r
582/**\r
583\r
584 Service routine for BdsInstance->Entry(). Devices are connected, the\r
585 consoles are initialized, and the boot options are tried.\r
586\r
587 @param This Protocol Instance structure.\r
588\r
589**/\r
590VOID\r
591EFIAPI\r
592BdsEntry (\r
593 IN EFI_BDS_ARCH_PROTOCOL *This\r
594 )\r
595{\r
1634214d
RN
596 EFI_BOOT_MANAGER_LOAD_OPTION *LoadOptions;\r
597 UINTN LoadOptionCount;\r
f4cd24da
RN
598 CHAR16 *FirmwareVendor;\r
599 EFI_EVENT HotkeyTriggered;\r
600 UINT64 OsIndication;\r
601 UINTN DataSize;\r
602 EFI_STATUS Status;\r
603 UINT32 BootOptionSupport;\r
604 UINT16 BootTimeOut;\r
605 EDKII_VARIABLE_LOCK_PROTOCOL *VariableLock;\r
606 UINTN Index;\r
d175f4e6 607 EFI_BOOT_MANAGER_LOAD_OPTION LoadOption;\r
f4cd24da
RN
608 UINT16 *BootNext;\r
609 CHAR16 BootNextVariableName[sizeof ("Boot####")];\r
1634214d
RN
610 EFI_BOOT_MANAGER_LOAD_OPTION BootManagerMenu;\r
611 BOOLEAN BootFwUi;\r
68456d8a
RN
612 BOOLEAN PlatformRecovery;\r
613 BOOLEAN BootSuccess;\r
d175f4e6 614 EFI_DEVICE_PATH_PROTOCOL *FilePath;\r
e58f1ae5 615 EFI_STATUS BootManagerMenuStatus;\r
f4cd24da
RN
616\r
617 HotkeyTriggered = NULL;\r
618 Status = EFI_SUCCESS;\r
68456d8a 619 BootSuccess = FALSE;\r
f4cd24da
RN
620\r
621 //\r
622 // Insert the performance probe\r
623 //\r
624 PERF_END (NULL, "DXE", NULL, 0);\r
625 PERF_START (NULL, "BDS", NULL, 0);\r
626 DEBUG ((EFI_D_INFO, "[Bds] Entry...\n"));\r
627\r
628 PERF_CODE (\r
629 BdsAllocateMemoryForPerformanceData ();\r
630 );\r
631\r
632 //\r
633 // Fill in FirmwareVendor and FirmwareRevision from PCDs\r
634 //\r
635 FirmwareVendor = (CHAR16 *) PcdGetPtr (PcdFirmwareVendor);\r
636 gST->FirmwareVendor = AllocateRuntimeCopyPool (StrSize (FirmwareVendor), FirmwareVendor);\r
637 ASSERT (gST->FirmwareVendor != NULL);\r
638 gST->FirmwareRevision = PcdGet32 (PcdFirmwareRevision);\r
639\r
640 //\r
641 // Fixup Tasble CRC after we updated Firmware Vendor and Revision\r
642 //\r
643 gST->Hdr.CRC32 = 0;\r
644 gBS->CalculateCrc32 ((VOID *) gST, sizeof (EFI_SYSTEM_TABLE), &gST->Hdr.CRC32);\r
645\r
646 //\r
647 // Validate Variable.\r
648 //\r
649 BdsFormalizeEfiGlobalVariable ();\r
650\r
651 //\r
652 // Mark the read-only variables if the Variable Lock protocol exists\r
653 //\r
654 Status = gBS->LocateProtocol (&gEdkiiVariableLockProtocolGuid, NULL, (VOID **) &VariableLock);\r
655 DEBUG ((EFI_D_INFO, "[BdsDxe] Locate Variable Lock protocol - %r\n", Status));\r
656 if (!EFI_ERROR (Status)) {\r
657 for (Index = 0; Index < sizeof (mReadOnlyVariables) / sizeof (mReadOnlyVariables[0]); Index++) {\r
658 Status = VariableLock->RequestToLock (VariableLock, mReadOnlyVariables[Index], &gEfiGlobalVariableGuid);\r
659 ASSERT_EFI_ERROR (Status);\r
660 }\r
661 }\r
662\r
663 InitializeHwErrRecSupport ();\r
664\r
665 //\r
666 // Initialize L"Timeout" EFI global variable.\r
667 //\r
668 BootTimeOut = PcdGet16 (PcdPlatformBootTimeOut);\r
669 if (BootTimeOut != 0xFFFF) {\r
670 //\r
671 // If time out value equal 0xFFFF, no need set to 0xFFFF to variable area because UEFI specification\r
672 // define same behavior between no value or 0xFFFF value for L"Timeout".\r
673 //\r
674 BdsDxeSetVariableAndReportStatusCodeOnError (\r
675 EFI_TIME_OUT_VARIABLE_NAME,\r
676 &gEfiGlobalVariableGuid,\r
677 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
678 sizeof (UINT16),\r
679 &BootTimeOut\r
680 );\r
681 }\r
682\r
683 //\r
684 // Initialize L"BootOptionSupport" EFI global variable.\r
685 // Lazy-ConIn implictly disables BDS hotkey.\r
686 //\r
1634214d 687 BootOptionSupport = EFI_BOOT_OPTION_SUPPORT_APP | EFI_BOOT_OPTION_SUPPORT_SYSPREP;\r
f4cd24da
RN
688 if (!PcdGetBool (PcdConInConnectOnDemand)) {\r
689 BootOptionSupport |= EFI_BOOT_OPTION_SUPPORT_KEY;\r
690 SET_BOOT_OPTION_SUPPORT_KEY_COUNT (BootOptionSupport, 3);\r
691 }\r
692 Status = gRT->SetVariable (\r
693 EFI_BOOT_OPTION_SUPPORT_VARIABLE_NAME,\r
694 &gEfiGlobalVariableGuid,\r
695 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r
696 sizeof (BootOptionSupport),\r
697 &BootOptionSupport\r
698 );\r
699 //\r
700 // Platform needs to make sure setting volatile variable before calling 3rd party code shouldn't fail.\r
701 //\r
702 ASSERT_EFI_ERROR (Status);\r
703\r
704 //\r
705 // Cache and remove the "BootNext" NV variable.\r
706 //\r
707 GetEfiGlobalVariable2 (EFI_BOOT_NEXT_VARIABLE_NAME, (VOID **) &BootNext, &DataSize);\r
708 if (DataSize != sizeof (UINT16)) {\r
709 if (BootNext != NULL) {\r
710 FreePool (BootNext);\r
711 }\r
712 BootNext = NULL;\r
713 }\r
714 Status = gRT->SetVariable (\r
715 EFI_BOOT_NEXT_VARIABLE_NAME,\r
716 &gEfiGlobalVariableGuid,\r
717 0,\r
718 0,\r
719 NULL\r
720 );\r
721 //\r
722 // Deleting NV variable shouldn't fail unless it doesn't exist.\r
723 //\r
724 ASSERT (Status == EFI_SUCCESS || Status == EFI_NOT_FOUND);\r
725\r
726 //\r
727 // Initialize the platform language variables\r
728 //\r
729 InitializeLanguage (TRUE);\r
730\r
d175f4e6
RN
731 //\r
732 // System firmware must include a PlatformRecovery#### variable specifying\r
733 // a short-form File Path Media Device Path containing the platform default\r
734 // file path for removable media\r
735 //\r
736 FilePath = FileDevicePath (NULL, EFI_REMOVABLE_MEDIA_FILE_NAME);\r
737 Status = EfiBootManagerInitializeLoadOption (\r
738 &LoadOption,\r
739 0,\r
740 LoadOptionTypePlatformRecovery,\r
741 LOAD_OPTION_ACTIVE,\r
742 L"Default PlatformRecovery",\r
743 FilePath,\r
744 NULL,\r
745 0\r
746 );\r
747 ASSERT_EFI_ERROR (Status);\r
748 EfiBootManagerLoadOptionToVariable (&LoadOption);\r
749 EfiBootManagerFreeLoadOption (&LoadOption);\r
750 FreePool (FilePath);\r
751\r
f4cd24da
RN
752 //\r
753 // Report Status Code to indicate connecting drivers will happen\r
754 //\r
755 REPORT_STATUS_CODE (\r
756 EFI_PROGRESS_CODE,\r
757 (EFI_SOFTWARE_DXE_BS_DRIVER | EFI_SW_DXE_BS_PC_BEGIN_CONNECTING_DRIVERS)\r
758 );\r
759\r
760 //\r
761 // Do the platform init, can be customized by OEM/IBV\r
762 // Possible things that can be done in PlatformBootManagerBeforeConsole:\r
763 // > Update console variable: 1. include hot-plug devices; 2. Clear ConIn and add SOL for AMT\r
764 // > Register new Driver#### or Boot####\r
765 // > Register new Key####: e.g.: F12 \r
766 // > Signal ReadyToLock event\r
767 // > Authentication action: 1. connect Auth devices; 2. Identify auto logon user.\r
768 //\r
769 PERF_START (NULL, "PlatformBootManagerBeforeConsole", "BDS", 0);\r
770 PlatformBootManagerBeforeConsole ();\r
771 PERF_END (NULL, "PlatformBootManagerBeforeConsole", "BDS", 0);\r
772\r
773 //\r
774 // Initialize hotkey service\r
775 //\r
776 EfiBootManagerStartHotkeyService (&HotkeyTriggered);\r
777\r
778 //\r
1634214d 779 // Execute Driver Options\r
f4cd24da 780 //\r
1634214d
RN
781 LoadOptions = EfiBootManagerGetLoadOptions (&LoadOptionCount, LoadOptionTypeDriver);\r
782 ProcessLoadOptions (LoadOptions, LoadOptionCount);\r
783 EfiBootManagerFreeLoadOptions (LoadOptions, LoadOptionCount);\r
f4cd24da
RN
784\r
785 //\r
786 // Connect consoles\r
787 //\r
788 PERF_START (NULL, "EfiBootManagerConnectAllDefaultConsoles", "BDS", 0);\r
789 if (PcdGetBool (PcdConInConnectOnDemand)) {\r
790 EfiBootManagerConnectConsoleVariable (ConOut);\r
791 EfiBootManagerConnectConsoleVariable (ErrOut);\r
792\r
793 //\r
794 // Initialize ConnectConIn event\r
795 //\r
796 Status = gBS->CreateEventEx (\r
797 EVT_NOTIFY_SIGNAL,\r
798 TPL_CALLBACK,\r
799 BdsDxeOnConnectConInCallBack,\r
800 NULL,\r
801 &gConnectConInEventGuid,\r
802 &gConnectConInEvent\r
803 );\r
804 if (EFI_ERROR (Status)) {\r
805 gConnectConInEvent = NULL;\r
806 }\r
807 } else {\r
808 EfiBootManagerConnectAllDefaultConsoles ();\r
809 }\r
810 PERF_END (NULL, "EfiBootManagerConnectAllDefaultConsoles", "BDS", 0);\r
811\r
812 //\r
813 // Do the platform specific action after the console is ready\r
814 // Possible things that can be done in PlatformBootManagerAfterConsole:\r
815 // > Console post action:\r
816 // > Dynamically switch output mode from 100x31 to 80x25 for certain senarino\r
817 // > Signal console ready platform customized event\r
818 // > Run diagnostics like memory testing\r
819 // > Connect certain devices\r
820 // > Dispatch aditional option roms\r
821 // > Special boot: e.g.: USB boot, enter UI\r
822 // \r
823 PERF_START (NULL, "PlatformBootManagerAfterConsole", "BDS", 0);\r
824 PlatformBootManagerAfterConsole ();\r
825 PERF_END (NULL, "PlatformBootManagerAfterConsole", "BDS", 0);\r
68456d8a
RN
826 //\r
827 // Boot to Boot Manager Menu when EFI_OS_INDICATIONS_BOOT_TO_FW_UI is set. Skip HotkeyBoot\r
828 //\r
829 DataSize = sizeof (UINT64);\r
830 Status = gRT->GetVariable (\r
831 EFI_OS_INDICATIONS_VARIABLE_NAME,\r
832 &gEfiGlobalVariableGuid,\r
833 NULL,\r
834 &DataSize,\r
835 &OsIndication\r
836 );\r
837 if (EFI_ERROR (Status)) {\r
838 OsIndication = 0;\r
839 }\r
f4cd24da
RN
840\r
841 DEBUG_CODE (\r
1634214d 842 EFI_BOOT_MANAGER_LOAD_OPTION_TYPE LoadOptionType;\r
68456d8a 843 DEBUG ((EFI_D_INFO, "[Bds]OsIndication: %016x\n", OsIndication));\r
1634214d
RN
844 DEBUG ((EFI_D_INFO, "[Bds]=============Begin Load Options Dumping ...=============\n"));\r
845 for (LoadOptionType = 0; LoadOptionType < LoadOptionTypeMax; LoadOptionType++) {\r
f4cd24da 846 DEBUG ((\r
1634214d
RN
847 EFI_D_INFO, " %s Options:\n",\r
848 mBdsLoadOptionName[LoadOptionType]\r
f4cd24da 849 ));\r
1634214d
RN
850 LoadOptions = EfiBootManagerGetLoadOptions (&LoadOptionCount, LoadOptionType);\r
851 for (Index = 0; Index < LoadOptionCount; Index++) {\r
852 DEBUG ((\r
853 EFI_D_INFO, " %s%04x: %s \t\t 0x%04x\n",\r
854 mBdsLoadOptionName[LoadOptionType],\r
855 LoadOptions[Index].OptionNumber,\r
856 LoadOptions[Index].Description,\r
857 LoadOptions[Index].Attributes\r
858 ));\r
859 }\r
860 EfiBootManagerFreeLoadOptions (LoadOptions, LoadOptionCount);\r
f4cd24da 861 }\r
1634214d
RN
862 DEBUG ((EFI_D_INFO, "[Bds]=============End Load Options Dumping=============\n"));\r
863 );\r
f4cd24da
RN
864\r
865 //\r
e58f1ae5 866 // BootManagerMenu doesn't contain the correct information when return status is EFI_NOT_FOUND.\r
f4cd24da 867 //\r
e58f1ae5 868 BootManagerMenuStatus = EfiBootManagerGetBootManagerMenu (&BootManagerMenu);\r
1634214d 869\r
68456d8a
RN
870 BootFwUi = (BOOLEAN) ((OsIndication & EFI_OS_INDICATIONS_BOOT_TO_FW_UI) != 0);\r
871 PlatformRecovery = (BOOLEAN) ((OsIndication & EFI_OS_INDICATIONS_START_PLATFORM_RECOVERY) != 0);\r
1634214d
RN
872 //\r
873 // Clear EFI_OS_INDICATIONS_BOOT_TO_FW_UI to acknowledge OS\r
874 // \r
68456d8a
RN
875 if (BootFwUi || PlatformRecovery) {\r
876 OsIndication &= ~((UINT64) (EFI_OS_INDICATIONS_BOOT_TO_FW_UI | EFI_OS_INDICATIONS_START_PLATFORM_RECOVERY));\r
f4cd24da 877 Status = gRT->SetVariable (\r
cc4812f6 878 EFI_OS_INDICATIONS_VARIABLE_NAME,\r
f4cd24da
RN
879 &gEfiGlobalVariableGuid,\r
880 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
881 sizeof(UINT64),\r
882 &OsIndication\r
883 );\r
884 //\r
885 // Changing the content without increasing its size with current variable implementation shouldn't fail.\r
886 //\r
887 ASSERT_EFI_ERROR (Status);\r
1634214d 888 }\r
f4cd24da 889\r
1634214d
RN
890 //\r
891 // Launch Boot Manager Menu directly when EFI_OS_INDICATIONS_BOOT_TO_FW_UI is set. Skip HotkeyBoot\r
892 //\r
e58f1ae5 893 if (BootFwUi && (BootManagerMenuStatus != EFI_NOT_FOUND)) {\r
f4cd24da
RN
894 //\r
895 // Follow generic rule, Call BdsDxeOnConnectConInCallBack to connect ConIn before enter UI\r
896 //\r
897 if (PcdGetBool (PcdConInConnectOnDemand)) {\r
898 BdsDxeOnConnectConInCallBack (NULL, NULL);\r
899 }\r
900\r
901 //\r
1634214d 902 // Directly enter the setup page.\r
f4cd24da 903 //\r
1634214d 904 EfiBootManagerBoot (&BootManagerMenu);\r
f4cd24da
RN
905 }\r
906\r
68456d8a
RN
907 if (!PlatformRecovery) {\r
908 //\r
909 // Execute SysPrep####\r
910 //\r
911 LoadOptions = EfiBootManagerGetLoadOptions (&LoadOptionCount, LoadOptionTypeSysPrep);\r
912 ProcessLoadOptions (LoadOptions, LoadOptionCount);\r
913 EfiBootManagerFreeLoadOptions (LoadOptions, LoadOptionCount);\r
1634214d 914\r
68456d8a
RN
915 //\r
916 // Execute Key####\r
917 //\r
918 PERF_START (NULL, "BdsWait", "BDS", 0);\r
919 BdsWait (HotkeyTriggered);\r
920 PERF_END (NULL, "BdsWait", "BDS", 0);\r
1634214d 921\r
68456d8a
RN
922 //\r
923 // BdsReadKeys() can be removed after all keyboard drivers invoke callback in timer callback.\r
924 //\r
925 BdsReadKeys ();\r
1634214d 926\r
68456d8a 927 EfiBootManagerHotkeyBoot ();\r
1634214d 928\r
68456d8a
RN
929 //\r
930 // Boot to "BootNext"\r
931 //\r
932 if (BootNext != NULL) {\r
933 UnicodeSPrint (BootNextVariableName, sizeof (BootNextVariableName), L"Boot%04x", *BootNext);\r
934 Status = EfiBootManagerVariableToLoadOption (BootNextVariableName, &LoadOption);\r
935 if (!EFI_ERROR (Status)) {\r
d175f4e6
RN
936 EfiBootManagerBoot (&LoadOption);\r
937 EfiBootManagerFreeLoadOption (&LoadOption);\r
e58f1ae5
SW
938 if ((LoadOption.Status == EFI_SUCCESS) && \r
939 (BootManagerMenuStatus != EFI_NOT_FOUND) &&\r
940 (LoadOption.OptionNumber != BootManagerMenu.OptionNumber)) {\r
68456d8a
RN
941 //\r
942 // Boot to Boot Manager Menu upon EFI_SUCCESS\r
f5cbc197 943 // Exception: Do not boot again when the BootNext points to Boot Manager Menu.\r
68456d8a
RN
944 //\r
945 EfiBootManagerBoot (&BootManagerMenu);\r
946 }\r
f4cd24da
RN
947 }\r
948 }\r
68456d8a
RN
949\r
950 do {\r
951 //\r
952 // Retry to boot if any of the boot succeeds\r
953 //\r
954 LoadOptions = EfiBootManagerGetLoadOptions (&LoadOptionCount, LoadOptionTypeBoot);\r
e58f1ae5 955 BootSuccess = BootBootOptions (LoadOptions, LoadOptionCount, (BootManagerMenuStatus != EFI_NOT_FOUND) ? &BootManagerMenu : NULL);\r
68456d8a
RN
956 EfiBootManagerFreeLoadOptions (LoadOptions, LoadOptionCount);\r
957 } while (BootSuccess);\r
f4cd24da
RN
958 }\r
959\r
e58f1ae5
SW
960 if (BootManagerMenuStatus != EFI_NOT_FOUND) {\r
961 EfiBootManagerFreeLoadOption (&BootManagerMenu);\r
962 }\r
68456d8a
RN
963\r
964 if (!BootSuccess) {\r
965 LoadOptions = EfiBootManagerGetLoadOptions (&LoadOptionCount, LoadOptionTypePlatformRecovery);\r
966 ProcessLoadOptions (LoadOptions, LoadOptionCount);\r
967 EfiBootManagerFreeLoadOptions (LoadOptions, LoadOptionCount);\r
f4cd24da 968 }\r
68456d8a
RN
969\r
970 DEBUG ((EFI_D_ERROR, "[Bds] Unable to boot!\n"));\r
971 CpuDeadLoop ();\r
f4cd24da
RN
972}\r
973\r
974/**\r
975 Set the variable and report the error through status code upon failure.\r
976\r
977 @param VariableName A Null-terminated string that is the name of the vendor's variable.\r
978 Each VariableName is unique for each VendorGuid. VariableName must\r
979 contain 1 or more characters. If VariableName is an empty string,\r
980 then EFI_INVALID_PARAMETER is returned.\r
981 @param VendorGuid A unique identifier for the vendor.\r
982 @param Attributes Attributes bitmask to set for the variable.\r
983 @param DataSize The size in bytes of the Data buffer. Unless the EFI_VARIABLE_APPEND_WRITE, \r
984 EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS, or \r
985 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS attribute is set, a size of zero \r
986 causes the variable to be deleted. When the EFI_VARIABLE_APPEND_WRITE attribute is \r
987 set, then a SetVariable() call with a DataSize of zero will not cause any change to \r
988 the variable value (the timestamp associated with the variable may be updated however \r
989 even if no new data value is provided,see the description of the \r
990 EFI_VARIABLE_AUTHENTICATION_2 descriptor below. In this case the DataSize will not \r
991 be zero since the EFI_VARIABLE_AUTHENTICATION_2 descriptor will be populated). \r
992 @param Data The contents for the variable.\r
993\r
994 @retval EFI_SUCCESS The firmware has successfully stored the variable and its data as\r
995 defined by the Attributes.\r
996 @retval EFI_INVALID_PARAMETER An invalid combination of attribute bits, name, and GUID was supplied, or the\r
997 DataSize exceeds the maximum allowed.\r
998 @retval EFI_INVALID_PARAMETER VariableName is an empty string.\r
999 @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the variable and its data.\r
1000 @retval EFI_DEVICE_ERROR The variable could not be retrieved due to a hardware error.\r
1001 @retval EFI_WRITE_PROTECTED The variable in question is read-only.\r
1002 @retval EFI_WRITE_PROTECTED The variable in question cannot be deleted.\r
1003 @retval EFI_SECURITY_VIOLATION The variable could not be written due to EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS \r
1004 or EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACESS being set, but the AuthInfo \r
1005 does NOT pass the validation check carried out by the firmware.\r
1006\r
1007 @retval EFI_NOT_FOUND The variable trying to be updated or deleted was not found.\r
1008**/\r
1009EFI_STATUS\r
1010BdsDxeSetVariableAndReportStatusCodeOnError (\r
1011 IN CHAR16 *VariableName,\r
1012 IN EFI_GUID *VendorGuid,\r
1013 IN UINT32 Attributes,\r
1014 IN UINTN DataSize,\r
1015 IN VOID *Data\r
1016 )\r
1017{\r
1018 EFI_STATUS Status;\r
1019 EDKII_SET_VARIABLE_STATUS *SetVariableStatus;\r
1020 UINTN NameSize;\r
1021\r
1022 Status = gRT->SetVariable (\r
1023 VariableName,\r
1024 VendorGuid,\r
1025 Attributes,\r
1026 DataSize,\r
1027 Data\r
1028 );\r
1029 if (EFI_ERROR (Status)) {\r
1030 NameSize = StrSize (VariableName);\r
1031 SetVariableStatus = AllocatePool (sizeof (EDKII_SET_VARIABLE_STATUS) + NameSize + DataSize);\r
1032 if (SetVariableStatus != NULL) {\r
1033 CopyGuid (&SetVariableStatus->Guid, VendorGuid);\r
1034 SetVariableStatus->NameSize = NameSize;\r
1035 SetVariableStatus->DataSize = DataSize;\r
1036 SetVariableStatus->SetStatus = Status;\r
1037 SetVariableStatus->Attributes = Attributes;\r
1038 CopyMem (SetVariableStatus + 1, VariableName, NameSize);\r
1039 CopyMem (((UINT8 *) (SetVariableStatus + 1)) + NameSize, Data, DataSize);\r
1040\r
1041 REPORT_STATUS_CODE_EX (\r
1042 EFI_ERROR_CODE,\r
1043 PcdGet32 (PcdErrorCodeSetVariable),\r
1044 0,\r
1045 NULL,\r
1046 &gEdkiiStatusCodeDataTypeVariableGuid,\r
1047 SetVariableStatus,\r
1048 sizeof (EDKII_SET_VARIABLE_STATUS) + NameSize + DataSize\r
1049 );\r
1050\r
1051 FreePool (SetVariableStatus);\r
1052 }\r
1053 }\r
1054\r
1055 return Status;\r
1056}\r