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