]> git.proxmox.com Git - mirror_edk2.git/blob - IntelFrameworkModulePkg/Universal/BdsDxe/BdsEntry.c
IntelFrameworkModulePkg: Clean up source files
[mirror_edk2.git] / IntelFrameworkModulePkg / 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 - 2018, Intel Corporation. All rights reserved.<BR>
9 This program and the accompanying materials
10 are licensed and made available under the terms and conditions of the BSD License
11 which accompanies this distribution. The full text of the license may be found at
12 http://opensource.org/licenses/bsd-license.php
13
14 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
15 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16
17 **/
18
19 #include "Bds.h"
20 #include "Language.h"
21 #include "FrontPage.h"
22 #include "Hotkey.h"
23 #include "HwErrRecSupport.h"
24
25 ///
26 /// BDS arch protocol instance initial value.
27 ///
28 /// Note: Current BDS not directly get the BootMode, DefaultBoot,
29 /// TimeoutDefault, MemoryTestLevel value from the BDS arch protocol.
30 /// Please refer to the library useage of BdsLibGetBootMode, BdsLibGetTimeout
31 /// and PlatformBdsDiagnostics in BdsPlatform.c
32 ///
33 EFI_HANDLE gBdsHandle = NULL;
34
35 EFI_BDS_ARCH_PROTOCOL gBds = {
36 BdsEntry
37 };
38
39 UINT16 *mBootNext = NULL;
40
41 ///
42 /// The read-only variables defined in UEFI Spec.
43 ///
44 CHAR16 *mReadOnlyVariables[] = {
45 L"PlatformLangCodes",
46 L"LangCodes",
47 L"BootOptionSupport",
48 L"HwErrRecSupport",
49 L"OsIndicationsSupported"
50 };
51
52 /**
53
54 Install Boot Device Selection Protocol
55
56 @param ImageHandle The image handle.
57 @param SystemTable The system table.
58
59 @retval EFI_SUCEESS BDS has finished initializing.
60 Return the dispatcher and recall BDS.Entry
61 @retval Other Return status from AllocatePool() or gBS->InstallProtocolInterface
62
63 **/
64 EFI_STATUS
65 EFIAPI
66 BdsInitialize (
67 IN EFI_HANDLE ImageHandle,
68 IN EFI_SYSTEM_TABLE *SystemTable
69 )
70 {
71 EFI_STATUS Status;
72
73 //
74 // Install protocol interface
75 //
76 Status = gBS->InstallMultipleProtocolInterfaces (
77 &gBdsHandle,
78 &gEfiBdsArchProtocolGuid, &gBds,
79 NULL
80 );
81 ASSERT_EFI_ERROR (Status);
82
83 return Status;
84 }
85
86 /**
87
88 This function attempts to boot for the boot order specified
89 by platform policy.
90
91 **/
92 VOID
93 BdsBootDeviceSelect (
94 VOID
95 )
96 {
97 EFI_STATUS Status;
98 LIST_ENTRY *Link;
99 BDS_COMMON_OPTION *BootOption;
100 UINTN ExitDataSize;
101 CHAR16 *ExitData;
102 UINT16 Timeout;
103 LIST_ENTRY BootLists;
104 CHAR16 Buffer[20];
105 BOOLEAN BootNextExist;
106 LIST_ENTRY *LinkBootNext;
107 EFI_EVENT ConnectConInEvent;
108
109 //
110 // Got the latest boot option
111 //
112 BootNextExist = FALSE;
113 LinkBootNext = NULL;
114 ConnectConInEvent = NULL;
115 InitializeListHead (&BootLists);
116
117 //
118 // First check the boot next option
119 //
120 ZeroMem (Buffer, sizeof (Buffer));
121
122 //
123 // Create Event to signal ConIn connection request
124 //
125 if (PcdGetBool (PcdConInConnectOnDemand)) {
126 Status = gBS->CreateEventEx (
127 EVT_NOTIFY_SIGNAL,
128 TPL_CALLBACK,
129 EfiEventEmptyFunction,
130 NULL,
131 &gConnectConInEventGuid,
132 &ConnectConInEvent
133 );
134 if (EFI_ERROR(Status)) {
135 ConnectConInEvent = NULL;
136 }
137 }
138
139 if (mBootNext != NULL) {
140 //
141 // Indicate we have the boot next variable, so this time
142 // boot will always have this boot option
143 //
144 BootNextExist = TRUE;
145
146 //
147 // Clear the this variable so it's only exist in this time boot
148 //
149 Status = gRT->SetVariable (
150 L"BootNext",
151 &gEfiGlobalVariableGuid,
152 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,
153 0,
154 NULL
155 );
156 //
157 // Deleting variable with current variable implementation shouldn't fail.
158 //
159 ASSERT_EFI_ERROR (Status);
160
161 //
162 // Add the boot next boot option
163 //
164 UnicodeSPrint (Buffer, sizeof (Buffer), L"Boot%04x", *mBootNext);
165 BootOption = BdsLibVariableToOption (&BootLists, Buffer);
166
167 //
168 // If fail to get boot option from variable, just return and do nothing.
169 //
170 if (BootOption == NULL) {
171 return;
172 }
173
174 BootOption->BootCurrent = *mBootNext;
175 }
176 //
177 // Parse the boot order to get boot option
178 //
179 BdsLibBuildOptionFromVar (&BootLists, L"BootOrder");
180
181 //
182 // When we didn't have chance to build boot option variables in the first
183 // full configuration boot (e.g.: Reset in the first page or in Device Manager),
184 // we have no boot options in the following mini configuration boot.
185 // Give the last chance to enumerate the boot options.
186 //
187 if (IsListEmpty (&BootLists)) {
188 BdsLibEnumerateAllBootOption (&BootLists);
189 }
190
191 Link = BootLists.ForwardLink;
192
193 //
194 // Parameter check, make sure the loop will be valid
195 //
196 if (Link == NULL) {
197 return ;
198 }
199 //
200 // Here we make the boot in a loop, every boot success will
201 // return to the front page
202 //
203 for (;;) {
204 //
205 // Check the boot option list first
206 //
207 if (Link == &BootLists) {
208 //
209 // When LazyConIn enabled, signal connect ConIn event before enter UI
210 //
211 if (PcdGetBool (PcdConInConnectOnDemand) && ConnectConInEvent != NULL) {
212 gBS->SignalEvent (ConnectConInEvent);
213 }
214
215 //
216 // There are two ways to enter here:
217 // 1. There is no active boot option, give user chance to
218 // add new boot option
219 // 2. All the active boot option processed, and there is no
220 // one is success to boot, then we back here to allow user
221 // add new active boot option
222 //
223 Timeout = 0xffff;
224 PlatformBdsEnterFrontPage (Timeout, FALSE);
225 InitializeListHead (&BootLists);
226 BdsLibBuildOptionFromVar (&BootLists, L"BootOrder");
227 Link = BootLists.ForwardLink;
228 continue;
229 }
230 //
231 // Get the boot option from the link list
232 //
233 BootOption = CR (Link, BDS_COMMON_OPTION, Link, BDS_LOAD_OPTION_SIGNATURE);
234
235 //
236 // According to EFI Specification, if a load option is not marked
237 // as LOAD_OPTION_ACTIVE, the boot manager will not automatically
238 // load the option.
239 //
240 if (!IS_LOAD_OPTION_TYPE (BootOption->Attribute, LOAD_OPTION_ACTIVE)) {
241 //
242 // skip the header of the link list, because it has no boot option
243 //
244 Link = Link->ForwardLink;
245 continue;
246 }
247 //
248 // Make sure the boot option device path connected,
249 // but ignore the BBS device path
250 //
251 if (DevicePathType (BootOption->DevicePath) != BBS_DEVICE_PATH) {
252 //
253 // Notes: the internal shell can not been connected with device path
254 // so we do not check the status here
255 //
256 BdsLibConnectDevicePath (BootOption->DevicePath);
257 }
258
259 //
260 // Restore to original mode before launching boot option.
261 //
262 BdsSetConsoleMode (FALSE);
263
264 //
265 // All the driver options should have been processed since
266 // now boot will be performed.
267 //
268 Status = BdsLibBootViaBootOption (BootOption, BootOption->DevicePath, &ExitDataSize, &ExitData);
269 if (Status != EFI_SUCCESS) {
270 //
271 // Call platform action to indicate the boot fail
272 //
273 BootOption->StatusString = GetStringById (STRING_TOKEN (STR_BOOT_FAILED));
274 PlatformBdsBootFail (BootOption, Status, ExitData, ExitDataSize);
275
276 //
277 // Check the next boot option
278 //
279 Link = Link->ForwardLink;
280
281 } else {
282 //
283 // Call platform action to indicate the boot success
284 //
285 BootOption->StatusString = GetStringById (STRING_TOKEN (STR_BOOT_SUCCEEDED));
286 PlatformBdsBootSuccess (BootOption);
287
288 //
289 // Boot success, then stop process the boot order, and
290 // present the boot manager menu, front page
291 //
292
293 //
294 // When LazyConIn enabled, signal connect ConIn Event before enter UI
295 //
296 if (PcdGetBool (PcdConInConnectOnDemand) && ConnectConInEvent != NULL) {
297 gBS->SignalEvent (ConnectConInEvent);
298 }
299
300 Timeout = 0xffff;
301 PlatformBdsEnterFrontPage (Timeout, FALSE);
302
303 //
304 // Rescan the boot option list, avoid potential risk of the boot
305 // option change in front page
306 //
307 if (BootNextExist) {
308 LinkBootNext = BootLists.ForwardLink;
309 }
310
311 InitializeListHead (&BootLists);
312 if (LinkBootNext != NULL) {
313 //
314 // Reserve the boot next option
315 //
316 InsertTailList (&BootLists, LinkBootNext);
317 }
318
319 BdsLibBuildOptionFromVar (&BootLists, L"BootOrder");
320 Link = BootLists.ForwardLink;
321 }
322 }
323
324 }
325
326 /**
327
328 Validate input console variable data.
329
330 If found the device path is not a valid device path, remove the variable.
331
332 @param VariableName Input console variable name.
333
334 **/
335 VOID
336 BdsFormalizeConsoleVariable (
337 IN CHAR16 *VariableName
338 )
339 {
340 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
341 UINTN VariableSize;
342 EFI_STATUS Status;
343
344 DevicePath = BdsLibGetVariableAndSize (
345 VariableName,
346 &gEfiGlobalVariableGuid,
347 &VariableSize
348 );
349 if ((DevicePath != NULL) && !IsDevicePathValid (DevicePath, VariableSize)) {
350 Status = gRT->SetVariable (
351 VariableName,
352 &gEfiGlobalVariableGuid,
353 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,
354 0,
355 NULL
356 );
357 //
358 // Deleting variable with current variable implementation shouldn't fail.
359 //
360 ASSERT_EFI_ERROR (Status);
361 }
362 }
363
364 /**
365
366 Formalize Bds global variables.
367
368 1. For ConIn/ConOut/ConErr, if found the device path is not a valid device path, remove the variable.
369 2. For OsIndicationsSupported, Create a BS/RT/UINT64 variable to report caps
370 3. Delete OsIndications variable if it is not NV/BS/RT UINT64
371 Item 3 is used to solve case when OS corrupts OsIndications. Here simply delete this NV variable.
372
373 **/
374 VOID
375 BdsFormalizeEfiGlobalVariable (
376 VOID
377 )
378 {
379 EFI_STATUS Status;
380 UINT64 OsIndicationSupport;
381 UINT64 OsIndication;
382 UINTN DataSize;
383 UINT32 Attributes;
384
385 //
386 // Validate Console variable.
387 //
388 BdsFormalizeConsoleVariable (L"ConIn");
389 BdsFormalizeConsoleVariable (L"ConOut");
390 BdsFormalizeConsoleVariable (L"ErrOut");
391
392 //
393 // OS indicater support variable
394 //
395 OsIndicationSupport = EFI_OS_INDICATIONS_BOOT_TO_FW_UI \
396 | EFI_OS_INDICATIONS_FMP_CAPSULE_SUPPORTED;
397
398 BdsDxeSetVariableAndReportStatusCodeOnError (
399 L"OsIndicationsSupported",
400 &gEfiGlobalVariableGuid,
401 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
402 sizeof(UINT64),
403 &OsIndicationSupport
404 );
405
406 //
407 // If OsIndications is invalid, remove it.
408 // Invalid case
409 // 1. Data size != UINT64
410 // 2. OsIndication value inconsistence
411 // 3. OsIndication attribute inconsistence
412 //
413 OsIndication = 0;
414 Attributes = 0;
415 DataSize = sizeof(UINT64);
416 Status = gRT->GetVariable (
417 L"OsIndications",
418 &gEfiGlobalVariableGuid,
419 &Attributes,
420 &DataSize,
421 &OsIndication
422 );
423
424 if (!EFI_ERROR(Status)) {
425 if (DataSize != sizeof(UINT64) ||
426 (OsIndication & ~OsIndicationSupport) != 0 ||
427 Attributes != (EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE)){
428
429 DEBUG ((EFI_D_ERROR, "Unformalized OsIndications variable exists. Delete it\n"));
430 Status = gRT->SetVariable (
431 L"OsIndications",
432 &gEfiGlobalVariableGuid,
433 0,
434 0,
435 NULL
436 );
437 //
438 // Deleting variable with current variable implementation shouldn't fail.
439 //
440 ASSERT_EFI_ERROR (Status);
441 }
442 }
443
444 }
445
446 /**
447
448 Service routine for BdsInstance->Entry(). Devices are connected, the
449 consoles are initialized, and the boot options are tried.
450
451 @param This Protocol Instance structure.
452
453 **/
454 VOID
455 EFIAPI
456 BdsEntry (
457 IN EFI_BDS_ARCH_PROTOCOL *This
458 )
459 {
460 LIST_ENTRY DriverOptionList;
461 LIST_ENTRY BootOptionList;
462 UINTN BootNextSize;
463 CHAR16 *FirmwareVendor;
464 EFI_STATUS Status;
465 UINT16 BootTimeOut;
466 UINTN Index;
467 EDKII_VARIABLE_LOCK_PROTOCOL *VariableLock;
468
469 //
470 // Insert the performance probe
471 //
472 PERF_END (NULL, "DXE", NULL, 0);
473 PERF_START (NULL, "BDS", NULL, 0);
474
475 //
476 // Initialize the global system boot option and driver option
477 //
478 InitializeListHead (&DriverOptionList);
479 InitializeListHead (&BootOptionList);
480
481 //
482 // Initialize hotkey service
483 //
484 InitializeHotkeyService ();
485
486 //
487 // Fill in FirmwareVendor and FirmwareRevision from PCDs
488 //
489 FirmwareVendor = (CHAR16 *)PcdGetPtr (PcdFirmwareVendor);
490 gST->FirmwareVendor = AllocateRuntimeCopyPool (StrSize (FirmwareVendor), FirmwareVendor);
491 ASSERT (gST->FirmwareVendor != NULL);
492 gST->FirmwareRevision = PcdGet32 (PcdFirmwareRevision);
493
494 //
495 // Fixup Tasble CRC after we updated Firmware Vendor and Revision
496 //
497 gST->Hdr.CRC32 = 0;
498 gBS->CalculateCrc32 ((VOID *)gST, sizeof(EFI_SYSTEM_TABLE), &gST->Hdr.CRC32);
499
500 //
501 // Validate Variable.
502 //
503 BdsFormalizeEfiGlobalVariable();
504
505 //
506 // Mark the read-only variables if the Variable Lock protocol exists
507 //
508 Status = gBS->LocateProtocol (&gEdkiiVariableLockProtocolGuid, NULL, (VOID **) &VariableLock);
509 DEBUG ((EFI_D_INFO, "[BdsDxe] Locate Variable Lock protocol - %r\n", Status));
510 if (!EFI_ERROR (Status)) {
511 for (Index = 0; Index < ARRAY_SIZE (mReadOnlyVariables); Index++) {
512 Status = VariableLock->RequestToLock (VariableLock, mReadOnlyVariables[Index], &gEfiGlobalVariableGuid);
513 ASSERT_EFI_ERROR (Status);
514 }
515 }
516
517 //
518 // Report Status Code to indicate connecting drivers will happen
519 //
520 REPORT_STATUS_CODE (
521 EFI_PROGRESS_CODE,
522 (EFI_SOFTWARE_DXE_BS_DRIVER | EFI_SW_DXE_BS_PC_BEGIN_CONNECTING_DRIVERS)
523 );
524
525 InitializeHwErrRecSupport();
526
527 //
528 // Initialize L"Timeout" EFI global variable.
529 //
530 BootTimeOut = PcdGet16 (PcdPlatformBootTimeOut);
531 if (BootTimeOut != 0xFFFF) {
532 //
533 // If time out value equal 0xFFFF, no need set to 0xFFFF to variable area because UEFI specification
534 // define same behavior between no value or 0xFFFF value for L"Timeout".
535 //
536 BdsDxeSetVariableAndReportStatusCodeOnError (
537 L"Timeout",
538 &gEfiGlobalVariableGuid,
539 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,
540 sizeof (UINT16),
541 &BootTimeOut
542 );
543 }
544
545 //
546 // bugbug: platform specific code
547 // Initialize the platform specific string and language
548 //
549 InitializeStringSupport ();
550 InitializeLanguage (TRUE);
551 InitializeFrontPage (TRUE);
552
553 //
554 // Do the platform init, can be customized by OEM/IBV
555 //
556 PERF_START (NULL, "PlatformBds", "BDS", 0);
557 PlatformBdsInit ();
558
559 //
560 // Set up the device list based on EFI 1.1 variables
561 // process Driver#### and Load the driver's in the
562 // driver option list
563 //
564 BdsLibBuildOptionFromVar (&DriverOptionList, L"DriverOrder");
565 if (!IsListEmpty (&DriverOptionList)) {
566 BdsLibLoadDrivers (&DriverOptionList);
567 }
568 //
569 // Check if we have the boot next option
570 //
571 mBootNext = BdsLibGetVariableAndSize (
572 L"BootNext",
573 &gEfiGlobalVariableGuid,
574 &BootNextSize
575 );
576
577 //
578 // Setup some platform policy here
579 //
580 PlatformBdsPolicyBehavior (&DriverOptionList, &BootOptionList, BdsProcessCapsules, BdsMemoryTest);
581 PERF_END (NULL, "PlatformBds", "BDS", 0);
582
583 //
584 // BDS select the boot device to load OS
585 //
586 BdsBootDeviceSelect ();
587
588 //
589 // Only assert here since this is the right behavior, we should never
590 // return back to DxeCore.
591 //
592 ASSERT (FALSE);
593
594 return ;
595 }
596
597
598 /**
599 Set the variable and report the error through status code upon failure.
600
601 @param VariableName A Null-terminated string that is the name of the vendor's variable.
602 Each VariableName is unique for each VendorGuid. VariableName must
603 contain 1 or more characters. If VariableName is an empty string,
604 then EFI_INVALID_PARAMETER is returned.
605 @param VendorGuid A unique identifier for the vendor.
606 @param Attributes Attributes bitmask to set for the variable.
607 @param DataSize The size in bytes of the Data buffer. Unless the EFI_VARIABLE_APPEND_WRITE,
608 EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS, or
609 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS attribute is set, a size of zero
610 causes the variable to be deleted. When the EFI_VARIABLE_APPEND_WRITE attribute is
611 set, then a SetVariable() call with a DataSize of zero will not cause any change to
612 the variable value (the timestamp associated with the variable may be updated however
613 even if no new data value is provided,see the description of the
614 EFI_VARIABLE_AUTHENTICATION_2 descriptor below. In this case the DataSize will not
615 be zero since the EFI_VARIABLE_AUTHENTICATION_2 descriptor will be populated).
616 @param Data The contents for the variable.
617
618 @retval EFI_SUCCESS The firmware has successfully stored the variable and its data as
619 defined by the Attributes.
620 @retval EFI_INVALID_PARAMETER An invalid combination of attribute bits, name, and GUID was supplied, or the
621 DataSize exceeds the maximum allowed.
622 @retval EFI_INVALID_PARAMETER VariableName is an empty string.
623 @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the variable and its data.
624 @retval EFI_DEVICE_ERROR The variable could not be retrieved due to a hardware error.
625 @retval EFI_WRITE_PROTECTED The variable in question is read-only.
626 @retval EFI_WRITE_PROTECTED The variable in question cannot be deleted.
627 @retval EFI_SECURITY_VIOLATION The variable could not be written due to EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
628 or EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACESS being set, but the AuthInfo
629 does NOT pass the validation check carried out by the firmware.
630
631 @retval EFI_NOT_FOUND The variable trying to be updated or deleted was not found.
632 **/
633 EFI_STATUS
634 BdsDxeSetVariableAndReportStatusCodeOnError (
635 IN CHAR16 *VariableName,
636 IN EFI_GUID *VendorGuid,
637 IN UINT32 Attributes,
638 IN UINTN DataSize,
639 IN VOID *Data
640 )
641 {
642 EFI_STATUS Status;
643 EDKII_SET_VARIABLE_STATUS *SetVariableStatus;
644 UINTN NameSize;
645
646 Status = gRT->SetVariable (
647 VariableName,
648 VendorGuid,
649 Attributes,
650 DataSize,
651 Data
652 );
653 if (EFI_ERROR (Status)) {
654 NameSize = StrSize (VariableName);
655 SetVariableStatus = AllocatePool (sizeof (EDKII_SET_VARIABLE_STATUS) + NameSize + DataSize);
656 if (SetVariableStatus != NULL) {
657 CopyGuid (&SetVariableStatus->Guid, VendorGuid);
658 SetVariableStatus->NameSize = NameSize;
659 SetVariableStatus->DataSize = DataSize;
660 SetVariableStatus->SetStatus = Status;
661 SetVariableStatus->Attributes = Attributes;
662 CopyMem (SetVariableStatus + 1, VariableName, NameSize);
663 CopyMem (((UINT8 *) (SetVariableStatus + 1)) + NameSize, Data, DataSize);
664
665 REPORT_STATUS_CODE_EX (
666 EFI_ERROR_CODE,
667 PcdGet32 (PcdErrorCodeSetVariable),
668 0,
669 NULL,
670 &gEdkiiStatusCodeDataTypeVariableGuid,
671 SetVariableStatus,
672 sizeof (EDKII_SET_VARIABLE_STATUS) + NameSize + DataSize
673 );
674
675 FreePool (SetVariableStatus);
676 }
677 }
678
679 return Status;
680 }
681