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