]> git.proxmox.com Git - mirror_edk2.git/blob - IntelFrameworkModulePkg/Universal/BdsDxe/BdsEntry.c
Correct a typo: Change the type of the 4th parameter of EFI_DRIVER_HEALTH_PROTOCOL...
[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 - 2013, 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 An empty function to pass error checking of CreateEventEx ().
89
90 @param Event Event whose notification function is being invoked.
91 @param Context Pointer to the notification function's context,
92 which is implementation-dependent.
93
94 **/
95 VOID
96 EFIAPI
97 BdsEmptyCallbackFunction (
98 IN EFI_EVENT Event,
99 IN VOID *Context
100 )
101 {
102 }
103
104 /**
105
106 This function attempts to boot for the boot order specified
107 by platform policy.
108
109 **/
110 VOID
111 BdsBootDeviceSelect (
112 VOID
113 )
114 {
115 EFI_STATUS Status;
116 LIST_ENTRY *Link;
117 BDS_COMMON_OPTION *BootOption;
118 UINTN ExitDataSize;
119 CHAR16 *ExitData;
120 UINT16 Timeout;
121 LIST_ENTRY BootLists;
122 CHAR16 Buffer[20];
123 BOOLEAN BootNextExist;
124 LIST_ENTRY *LinkBootNext;
125 EFI_EVENT ConnectConInEvent;
126
127 //
128 // Got the latest boot option
129 //
130 BootNextExist = FALSE;
131 LinkBootNext = NULL;
132 ConnectConInEvent = NULL;
133 InitializeListHead (&BootLists);
134
135 //
136 // First check the boot next option
137 //
138 ZeroMem (Buffer, sizeof (Buffer));
139
140 //
141 // Create Event to signal ConIn connection request
142 //
143 if (PcdGetBool (PcdConInConnectOnDemand)) {
144 Status = gBS->CreateEventEx (
145 EVT_NOTIFY_SIGNAL,
146 TPL_CALLBACK,
147 BdsEmptyCallbackFunction,
148 NULL,
149 &gConnectConInEventGuid,
150 &ConnectConInEvent
151 );
152 if (EFI_ERROR(Status)) {
153 ConnectConInEvent = NULL;
154 }
155 }
156
157 if (mBootNext != NULL) {
158 //
159 // Indicate we have the boot next variable, so this time
160 // boot will always have this boot option
161 //
162 BootNextExist = TRUE;
163
164 //
165 // Clear the this variable so it's only exist in this time boot
166 //
167 gRT->SetVariable (
168 L"BootNext",
169 &gEfiGlobalVariableGuid,
170 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,
171 0,
172 mBootNext
173 );
174
175 //
176 // Add the boot next boot option
177 //
178 UnicodeSPrint (Buffer, sizeof (Buffer), L"Boot%04x", *mBootNext);
179 BootOption = BdsLibVariableToOption (&BootLists, Buffer);
180
181 //
182 // If fail to get boot option from variable, just return and do nothing.
183 //
184 if (BootOption == NULL) {
185 return;
186 }
187
188 BootOption->BootCurrent = *mBootNext;
189 }
190 //
191 // Parse the boot order to get boot option
192 //
193 BdsLibBuildOptionFromVar (&BootLists, L"BootOrder");
194
195 //
196 // When we didn't have chance to build boot option variables in the first
197 // full configuration boot (e.g.: Reset in the first page or in Device Manager),
198 // we have no boot options in the following mini configuration boot.
199 // Give the last chance to enumerate the boot options.
200 //
201 if (IsListEmpty (&BootLists)) {
202 BdsLibEnumerateAllBootOption (&BootLists);
203 }
204
205 Link = BootLists.ForwardLink;
206
207 //
208 // Parameter check, make sure the loop will be valid
209 //
210 if (Link == NULL) {
211 return ;
212 }
213 //
214 // Here we make the boot in a loop, every boot success will
215 // return to the front page
216 //
217 for (;;) {
218 //
219 // Check the boot option list first
220 //
221 if (Link == &BootLists) {
222 //
223 // When LazyConIn enabled, signal connect ConIn event before enter UI
224 //
225 if (PcdGetBool (PcdConInConnectOnDemand) && ConnectConInEvent != NULL) {
226 gBS->SignalEvent (ConnectConInEvent);
227 }
228
229 //
230 // There are two ways to enter here:
231 // 1. There is no active boot option, give user chance to
232 // add new boot option
233 // 2. All the active boot option processed, and there is no
234 // one is success to boot, then we back here to allow user
235 // add new active boot option
236 //
237 Timeout = 0xffff;
238 PlatformBdsEnterFrontPage (Timeout, FALSE);
239 InitializeListHead (&BootLists);
240 BdsLibBuildOptionFromVar (&BootLists, L"BootOrder");
241 Link = BootLists.ForwardLink;
242 continue;
243 }
244 //
245 // Get the boot option from the link list
246 //
247 BootOption = CR (Link, BDS_COMMON_OPTION, Link, BDS_LOAD_OPTION_SIGNATURE);
248
249 //
250 // According to EFI Specification, if a load option is not marked
251 // as LOAD_OPTION_ACTIVE, the boot manager will not automatically
252 // load the option.
253 //
254 if (!IS_LOAD_OPTION_TYPE (BootOption->Attribute, LOAD_OPTION_ACTIVE)) {
255 //
256 // skip the header of the link list, because it has no boot option
257 //
258 Link = Link->ForwardLink;
259 continue;
260 }
261 //
262 // Make sure the boot option device path connected,
263 // but ignore the BBS device path
264 //
265 if (DevicePathType (BootOption->DevicePath) != BBS_DEVICE_PATH) {
266 //
267 // Notes: the internal shell can not been connected with device path
268 // so we do not check the status here
269 //
270 BdsLibConnectDevicePath (BootOption->DevicePath);
271 }
272
273 //
274 // Restore to original mode before launching boot option.
275 //
276 BdsSetConsoleMode (FALSE);
277
278 //
279 // All the driver options should have been processed since
280 // now boot will be performed.
281 //
282 Status = BdsLibBootViaBootOption (BootOption, BootOption->DevicePath, &ExitDataSize, &ExitData);
283 if (Status != EFI_SUCCESS) {
284 //
285 // Call platform action to indicate the boot fail
286 //
287 BootOption->StatusString = GetStringById (STRING_TOKEN (STR_BOOT_FAILED));
288 PlatformBdsBootFail (BootOption, Status, ExitData, ExitDataSize);
289
290 //
291 // Check the next boot option
292 //
293 Link = Link->ForwardLink;
294
295 } else {
296 //
297 // Call platform action to indicate the boot success
298 //
299 BootOption->StatusString = GetStringById (STRING_TOKEN (STR_BOOT_SUCCEEDED));
300 PlatformBdsBootSuccess (BootOption);
301
302 //
303 // Boot success, then stop process the boot order, and
304 // present the boot manager menu, front page
305 //
306
307 //
308 // When LazyConIn enabled, signal connect ConIn Event before enter UI
309 //
310 if (PcdGetBool (PcdConInConnectOnDemand) && ConnectConInEvent != NULL) {
311 gBS->SignalEvent (ConnectConInEvent);
312 }
313
314 Timeout = 0xffff;
315 PlatformBdsEnterFrontPage (Timeout, FALSE);
316
317 //
318 // Rescan the boot option list, avoid potential risk of the boot
319 // option change in front page
320 //
321 if (BootNextExist) {
322 LinkBootNext = BootLists.ForwardLink;
323 }
324
325 InitializeListHead (&BootLists);
326 if (LinkBootNext != NULL) {
327 //
328 // Reserve the boot next option
329 //
330 InsertTailList (&BootLists, LinkBootNext);
331 }
332
333 BdsLibBuildOptionFromVar (&BootLists, L"BootOrder");
334 Link = BootLists.ForwardLink;
335 }
336 }
337
338 }
339
340 /**
341
342 Validate input console variable data.
343
344 If found the device path is not a valid device path, remove the variable.
345
346 @param VariableName Input console variable name.
347
348 **/
349 VOID
350 BdsFormalizeConsoleVariable (
351 IN CHAR16 *VariableName
352 )
353 {
354 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
355 UINTN VariableSize;
356 EFI_STATUS Status;
357
358 DevicePath = BdsLibGetVariableAndSize (
359 VariableName,
360 &gEfiGlobalVariableGuid,
361 &VariableSize
362 );
363 if ((DevicePath != NULL) && !IsDevicePathValid (DevicePath, VariableSize)) {
364 Status = gRT->SetVariable (
365 VariableName,
366 &gEfiGlobalVariableGuid,
367 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,
368 0,
369 NULL
370 );
371 ASSERT_EFI_ERROR (Status);
372 }
373 }
374
375 /**
376
377 Formalize Bds global variables.
378
379 1. For ConIn/ConOut/ConErr, if found the device path is not a valid device path, remove the variable.
380 2. For OsIndicationsSupported, Create a BS/RT/UINT64 variable to report caps
381 3. Delete OsIndications variable if it is not NV/BS/RT UINT64
382 Item 3 is used to solve case when OS corrupts OsIndications. Here simply delete this NV variable.
383
384 **/
385 VOID
386 BdsFormalizeEfiGlobalVariable (
387 VOID
388 )
389 {
390 EFI_STATUS Status;
391 UINT64 OsIndicationSupport;
392 UINT64 OsIndication;
393 UINTN DataSize;
394 UINT32 Attributes;
395
396 //
397 // Validate Console variable.
398 //
399 BdsFormalizeConsoleVariable (L"ConIn");
400 BdsFormalizeConsoleVariable (L"ConOut");
401 BdsFormalizeConsoleVariable (L"ErrOut");
402
403 //
404 // OS indicater support variable
405 //
406 OsIndicationSupport = EFI_OS_INDICATIONS_BOOT_TO_FW_UI;
407 Status = gRT->SetVariable (
408 L"OsIndicationsSupported",
409 &gEfiGlobalVariableGuid,
410 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
411 sizeof(UINT64),
412 &OsIndicationSupport
413 );
414 ASSERT_EFI_ERROR (Status);
415
416 //
417 // If OsIndications is invalid, remove it.
418 // Invalid case
419 // 1. Data size != UINT64
420 // 2. OsIndication value inconsistence
421 // 3. OsIndication attribute inconsistence
422 //
423 OsIndication = 0;
424 Attributes = 0;
425 DataSize = sizeof(UINT64);
426 Status = gRT->GetVariable (
427 L"OsIndications",
428 &gEfiGlobalVariableGuid,
429 &Attributes,
430 &DataSize,
431 &OsIndication
432 );
433
434 if (!EFI_ERROR(Status)) {
435 if (DataSize != sizeof(UINT64) ||
436 (OsIndication & ~OsIndicationSupport) != 0 ||
437 Attributes != (EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE)){
438
439 DEBUG ((EFI_D_ERROR, "Unformalized OsIndications variable exists. Delete it\n"));
440 Status = gRT->SetVariable (
441 L"OsIndications",
442 &gEfiGlobalVariableGuid,
443 Attributes,
444 0,
445 &OsIndication
446 );
447 ASSERT_EFI_ERROR (Status);
448 }
449 }
450
451 }
452
453 /**
454
455 Allocate a block of memory that will contain performance data to OS.
456
457 **/
458 VOID
459 BdsAllocateMemoryForPerformanceData (
460 VOID
461 )
462 {
463 EFI_STATUS Status;
464 EFI_PHYSICAL_ADDRESS AcpiLowMemoryBase;
465 EDKII_VARIABLE_LOCK_PROTOCOL *VariableLock;
466
467 AcpiLowMemoryBase = 0x0FFFFFFFFULL;
468
469 //
470 // Allocate a block of memory that will contain performance data to OS.
471 //
472 Status = gBS->AllocatePages (
473 AllocateMaxAddress,
474 EfiReservedMemoryType,
475 EFI_SIZE_TO_PAGES (PERF_DATA_MAX_LENGTH),
476 &AcpiLowMemoryBase
477 );
478 if (!EFI_ERROR (Status)) {
479 //
480 // Save the pointer to variable for use in S3 resume.
481 //
482 Status = gRT->SetVariable (
483 L"PerfDataMemAddr",
484 &gPerformanceProtocolGuid,
485 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
486 sizeof (EFI_PHYSICAL_ADDRESS),
487 &AcpiLowMemoryBase
488 );
489 ASSERT_EFI_ERROR (Status);
490 //
491 // Mark L"PerfDataMemAddr" variable to read-only if the Variable Lock protocol exists
492 //
493 Status = gBS->LocateProtocol (&gEdkiiVariableLockProtocolGuid, NULL, (VOID **) &VariableLock);
494 if (!EFI_ERROR (Status)) {
495 Status = VariableLock->RequestToLock (VariableLock, L"PerfDataMemAddr", &gPerformanceProtocolGuid);
496 ASSERT_EFI_ERROR (Status);
497 }
498 }
499 }
500
501 /**
502
503 Service routine for BdsInstance->Entry(). Devices are connected, the
504 consoles are initialized, and the boot options are tried.
505
506 @param This Protocol Instance structure.
507
508 **/
509 VOID
510 EFIAPI
511 BdsEntry (
512 IN EFI_BDS_ARCH_PROTOCOL *This
513 )
514 {
515 LIST_ENTRY DriverOptionList;
516 LIST_ENTRY BootOptionList;
517 UINTN BootNextSize;
518 CHAR16 *FirmwareVendor;
519 EFI_STATUS Status;
520 UINT16 BootTimeOut;
521 UINTN Index;
522 EDKII_VARIABLE_LOCK_PROTOCOL *VariableLock;
523
524 //
525 // Insert the performance probe
526 //
527 PERF_END (NULL, "DXE", NULL, 0);
528 PERF_START (NULL, "BDS", NULL, 0);
529
530 PERF_CODE (
531 BdsAllocateMemoryForPerformanceData ();
532 );
533
534 //
535 // Initialize the global system boot option and driver option
536 //
537 InitializeListHead (&DriverOptionList);
538 InitializeListHead (&BootOptionList);
539
540 //
541 // Initialize hotkey service
542 //
543 InitializeHotkeyService ();
544
545 //
546 // Fill in FirmwareVendor and FirmwareRevision from PCDs
547 //
548 FirmwareVendor = (CHAR16 *)PcdGetPtr (PcdFirmwareVendor);
549 gST->FirmwareVendor = AllocateRuntimeCopyPool (StrSize (FirmwareVendor), FirmwareVendor);
550 ASSERT (gST->FirmwareVendor != NULL);
551 gST->FirmwareRevision = PcdGet32 (PcdFirmwareRevision);
552
553 //
554 // Fixup Tasble CRC after we updated Firmware Vendor and Revision
555 //
556 gST->Hdr.CRC32 = 0;
557 gBS->CalculateCrc32 ((VOID *)gST, sizeof(EFI_SYSTEM_TABLE), &gST->Hdr.CRC32);
558
559 //
560 // Validate Variable.
561 //
562 BdsFormalizeEfiGlobalVariable();
563
564 //
565 // Mark the read-only variables if the Variable Lock protocol exists
566 //
567 Status = gBS->LocateProtocol (&gEdkiiVariableLockProtocolGuid, NULL, (VOID **) &VariableLock);
568 DEBUG ((EFI_D_INFO, "[BdsDxe] Locate Variable Lock protocol - %r\n", Status));
569 if (!EFI_ERROR (Status)) {
570 for (Index = 0; Index < sizeof (mReadOnlyVariables) / sizeof (mReadOnlyVariables[0]); Index++) {
571 Status = VariableLock->RequestToLock (VariableLock, mReadOnlyVariables[Index], &gEfiGlobalVariableGuid);
572 ASSERT_EFI_ERROR (Status);
573 }
574 }
575
576 //
577 // Report Status Code to indicate connecting drivers will happen
578 //
579 REPORT_STATUS_CODE (
580 EFI_PROGRESS_CODE,
581 (EFI_SOFTWARE_DXE_BS_DRIVER | EFI_SW_DXE_BS_PC_BEGIN_CONNECTING_DRIVERS)
582 );
583
584 InitializeHwErrRecSupport();
585
586 //
587 // Initialize L"Timeout" EFI global variable.
588 //
589 BootTimeOut = PcdGet16 (PcdPlatformBootTimeOut);
590 if (BootTimeOut != 0xFFFF) {
591 //
592 // If time out value equal 0xFFFF, no need set to 0xFFFF to variable area because UEFI specification
593 // define same behavior between no value or 0xFFFF value for L"Timeout".
594 //
595 Status = gRT->SetVariable (
596 L"Timeout",
597 &gEfiGlobalVariableGuid,
598 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,
599 sizeof (UINT16),
600 &BootTimeOut
601 );
602 ASSERT_EFI_ERROR(Status);
603 }
604
605 //
606 // bugbug: platform specific code
607 // Initialize the platform specific string and language
608 //
609 InitializeStringSupport ();
610 InitializeLanguage (TRUE);
611 InitializeFrontPage (TRUE);
612
613 //
614 // Do the platform init, can be customized by OEM/IBV
615 //
616 PERF_START (NULL, "PlatformBds", "BDS", 0);
617 PlatformBdsInit ();
618
619 //
620 // Set up the device list based on EFI 1.1 variables
621 // process Driver#### and Load the driver's in the
622 // driver option list
623 //
624 BdsLibBuildOptionFromVar (&DriverOptionList, L"DriverOrder");
625 if (!IsListEmpty (&DriverOptionList)) {
626 BdsLibLoadDrivers (&DriverOptionList);
627 }
628 //
629 // Check if we have the boot next option
630 //
631 mBootNext = BdsLibGetVariableAndSize (
632 L"BootNext",
633 &gEfiGlobalVariableGuid,
634 &BootNextSize
635 );
636
637 //
638 // Setup some platform policy here
639 //
640 PlatformBdsPolicyBehavior (&DriverOptionList, &BootOptionList, BdsProcessCapsules, BdsMemoryTest);
641 PERF_END (NULL, "PlatformBds", "BDS", 0);
642
643 //
644 // BDS select the boot device to load OS
645 //
646 BdsBootDeviceSelect ();
647
648 //
649 // Only assert here since this is the right behavior, we should never
650 // return back to DxeCore.
651 //
652 ASSERT (FALSE);
653
654 return ;
655 }