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