]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/BdsDxe/BdsEntry.c
MdeModulePkg: Replace BSD License with BSD+Patent License
[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
9f0d7651 8Copyright (c) 2004 - 2018, 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
549 OsIndicationSupport = EFI_OS_INDICATIONS_BOOT_TO_FW_UI | EFI_OS_INDICATIONS_START_PLATFORM_RECOVERY;\r
550 EfiBootManagerFreeLoadOption (&BootManagerMenu);\r
551 } else {\r
552 OsIndicationSupport = EFI_OS_INDICATIONS_START_PLATFORM_RECOVERY;\r
553 }\r
554\r
f4cd24da 555 Status = gRT->SetVariable (\r
cc4812f6 556 EFI_OS_INDICATIONS_SUPPORT_VARIABLE_NAME,\r
f4cd24da
RN
557 &gEfiGlobalVariableGuid,\r
558 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r
559 sizeof(UINT64),\r
560 &OsIndicationSupport\r
561 );\r
562 //\r
563 // Platform needs to make sure setting volatile variable before calling 3rd party code shouldn't fail.\r
564 //\r
565 ASSERT_EFI_ERROR (Status);\r
566\r
567 //\r
568 // If OsIndications is invalid, remove it.\r
569 // Invalid case\r
570 // 1. Data size != UINT64\r
571 // 2. OsIndication value inconsistence\r
572 // 3. OsIndication attribute inconsistence\r
573 //\r
574 OsIndication = 0;\r
575 Attributes = 0;\r
576 DataSize = sizeof(UINT64);\r
577 Status = gRT->GetVariable (\r
cc4812f6 578 EFI_OS_INDICATIONS_VARIABLE_NAME,\r
f4cd24da
RN
579 &gEfiGlobalVariableGuid,\r
580 &Attributes,\r
581 &DataSize,\r
582 &OsIndication\r
583 );\r
584 if (Status == EFI_NOT_FOUND) {\r
585 return;\r
586 }\r
587\r
588 if ((DataSize != sizeof (OsIndication)) ||\r
589 ((OsIndication & ~OsIndicationSupport) != 0) ||\r
590 (Attributes != (EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE))\r
591 ){\r
592\r
593 DEBUG ((EFI_D_ERROR, "[Bds] Unformalized OsIndications variable exists. Delete it\n"));\r
594 Status = gRT->SetVariable (\r
cc4812f6 595 EFI_OS_INDICATIONS_VARIABLE_NAME,\r
f4cd24da
RN
596 &gEfiGlobalVariableGuid,\r
597 0,\r
598 0,\r
599 NULL\r
600 );\r
601 //\r
602 // Deleting variable with current variable implementation shouldn't fail.\r
603 //\r
604 ASSERT_EFI_ERROR(Status);\r
605 }\r
606}\r
607\r
608/**\r
609\r
d1102dba 610 Validate variables.\r
f4cd24da
RN
611\r
612**/\r
d1102dba 613VOID\r
f4cd24da
RN
614BdsFormalizeEfiGlobalVariable (\r
615 VOID\r
616 )\r
617{\r
618 //\r
619 // Validate Console variable.\r
620 //\r
cc4812f6
RN
621 BdsFormalizeConsoleVariable (EFI_CON_IN_VARIABLE_NAME);\r
622 BdsFormalizeConsoleVariable (EFI_CON_OUT_VARIABLE_NAME);\r
623 BdsFormalizeConsoleVariable (EFI_ERR_OUT_VARIABLE_NAME);\r
f4cd24da
RN
624\r
625 //\r
626 // Validate OSIndication related variable.\r
627 //\r
628 BdsFormalizeOSIndicationVariable ();\r
629}\r
630\r
f4cd24da
RN
631/**\r
632\r
633 Service routine for BdsInstance->Entry(). Devices are connected, the\r
634 consoles are initialized, and the boot options are tried.\r
635\r
636 @param This Protocol Instance structure.\r
637\r
638**/\r
639VOID\r
640EFIAPI\r
641BdsEntry (\r
642 IN EFI_BDS_ARCH_PROTOCOL *This\r
643 )\r
644{\r
1634214d
RN
645 EFI_BOOT_MANAGER_LOAD_OPTION *LoadOptions;\r
646 UINTN LoadOptionCount;\r
f4cd24da
RN
647 CHAR16 *FirmwareVendor;\r
648 EFI_EVENT HotkeyTriggered;\r
649 UINT64 OsIndication;\r
650 UINTN DataSize;\r
651 EFI_STATUS Status;\r
652 UINT32 BootOptionSupport;\r
653 UINT16 BootTimeOut;\r
654 EDKII_VARIABLE_LOCK_PROTOCOL *VariableLock;\r
655 UINTN Index;\r
d175f4e6 656 EFI_BOOT_MANAGER_LOAD_OPTION LoadOption;\r
f4cd24da
RN
657 UINT16 *BootNext;\r
658 CHAR16 BootNextVariableName[sizeof ("Boot####")];\r
1634214d
RN
659 EFI_BOOT_MANAGER_LOAD_OPTION BootManagerMenu;\r
660 BOOLEAN BootFwUi;\r
68456d8a
RN
661 BOOLEAN PlatformRecovery;\r
662 BOOLEAN BootSuccess;\r
d175f4e6 663 EFI_DEVICE_PATH_PROTOCOL *FilePath;\r
e58f1ae5 664 EFI_STATUS BootManagerMenuStatus;\r
f4cd24da
RN
665\r
666 HotkeyTriggered = NULL;\r
667 Status = EFI_SUCCESS;\r
68456d8a 668 BootSuccess = FALSE;\r
f4cd24da
RN
669\r
670 //\r
671 // Insert the performance probe\r
672 //\r
67e9ab84
BD
673 PERF_CROSSMODULE_END("DXE");\r
674 PERF_CROSSMODULE_BEGIN("BDS");\r
f4cd24da
RN
675 DEBUG ((EFI_D_INFO, "[Bds] Entry...\n"));\r
676\r
f4cd24da
RN
677 //\r
678 // Fill in FirmwareVendor and FirmwareRevision from PCDs\r
679 //\r
680 FirmwareVendor = (CHAR16 *) PcdGetPtr (PcdFirmwareVendor);\r
681 gST->FirmwareVendor = AllocateRuntimeCopyPool (StrSize (FirmwareVendor), FirmwareVendor);\r
682 ASSERT (gST->FirmwareVendor != NULL);\r
683 gST->FirmwareRevision = PcdGet32 (PcdFirmwareRevision);\r
684\r
685 //\r
686 // Fixup Tasble CRC after we updated Firmware Vendor and Revision\r
687 //\r
688 gST->Hdr.CRC32 = 0;\r
689 gBS->CalculateCrc32 ((VOID *) gST, sizeof (EFI_SYSTEM_TABLE), &gST->Hdr.CRC32);\r
690\r
691 //\r
692 // Validate Variable.\r
693 //\r
694 BdsFormalizeEfiGlobalVariable ();\r
695\r
696 //\r
697 // Mark the read-only variables if the Variable Lock protocol exists\r
698 //\r
699 Status = gBS->LocateProtocol (&gEdkiiVariableLockProtocolGuid, NULL, (VOID **) &VariableLock);\r
700 DEBUG ((EFI_D_INFO, "[BdsDxe] Locate Variable Lock protocol - %r\n", Status));\r
701 if (!EFI_ERROR (Status)) {\r
1a5afd71 702 for (Index = 0; Index < ARRAY_SIZE (mReadOnlyVariables); Index++) {\r
f4cd24da
RN
703 Status = VariableLock->RequestToLock (VariableLock, mReadOnlyVariables[Index], &gEfiGlobalVariableGuid);\r
704 ASSERT_EFI_ERROR (Status);\r
705 }\r
706 }\r
707\r
708 InitializeHwErrRecSupport ();\r
709\r
710 //\r
711 // Initialize L"Timeout" EFI global variable.\r
712 //\r
713 BootTimeOut = PcdGet16 (PcdPlatformBootTimeOut);\r
714 if (BootTimeOut != 0xFFFF) {\r
715 //\r
716 // If time out value equal 0xFFFF, no need set to 0xFFFF to variable area because UEFI specification\r
717 // define same behavior between no value or 0xFFFF value for L"Timeout".\r
718 //\r
719 BdsDxeSetVariableAndReportStatusCodeOnError (\r
720 EFI_TIME_OUT_VARIABLE_NAME,\r
721 &gEfiGlobalVariableGuid,\r
722 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
723 sizeof (UINT16),\r
724 &BootTimeOut\r
725 );\r
726 }\r
727\r
728 //\r
729 // Initialize L"BootOptionSupport" EFI global variable.\r
730 // Lazy-ConIn implictly disables BDS hotkey.\r
731 //\r
1634214d 732 BootOptionSupport = EFI_BOOT_OPTION_SUPPORT_APP | EFI_BOOT_OPTION_SUPPORT_SYSPREP;\r
f4cd24da
RN
733 if (!PcdGetBool (PcdConInConnectOnDemand)) {\r
734 BootOptionSupport |= EFI_BOOT_OPTION_SUPPORT_KEY;\r
735 SET_BOOT_OPTION_SUPPORT_KEY_COUNT (BootOptionSupport, 3);\r
736 }\r
737 Status = gRT->SetVariable (\r
738 EFI_BOOT_OPTION_SUPPORT_VARIABLE_NAME,\r
739 &gEfiGlobalVariableGuid,\r
740 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r
741 sizeof (BootOptionSupport),\r
742 &BootOptionSupport\r
743 );\r
744 //\r
745 // Platform needs to make sure setting volatile variable before calling 3rd party code shouldn't fail.\r
746 //\r
747 ASSERT_EFI_ERROR (Status);\r
748\r
749 //\r
0e6584e3
RN
750 // Cache the "BootNext" NV variable before calling any PlatformBootManagerLib APIs\r
751 // This could avoid the "BootNext" set by PlatformBootManagerLib be consumed in this boot.\r
f4cd24da
RN
752 //\r
753 GetEfiGlobalVariable2 (EFI_BOOT_NEXT_VARIABLE_NAME, (VOID **) &BootNext, &DataSize);\r
754 if (DataSize != sizeof (UINT16)) {\r
755 if (BootNext != NULL) {\r
756 FreePool (BootNext);\r
757 }\r
758 BootNext = NULL;\r
759 }\r
f4cd24da
RN
760\r
761 //\r
762 // Initialize the platform language variables\r
763 //\r
764 InitializeLanguage (TRUE);\r
765\r
d175f4e6
RN
766 //\r
767 // System firmware must include a PlatformRecovery#### variable specifying\r
768 // a short-form File Path Media Device Path containing the platform default\r
769 // file path for removable media\r
770 //\r
771 FilePath = FileDevicePath (NULL, EFI_REMOVABLE_MEDIA_FILE_NAME);\r
772 Status = EfiBootManagerInitializeLoadOption (\r
773 &LoadOption,\r
0dc3fb06 774 LoadOptionNumberUnassigned,\r
d175f4e6
RN
775 LoadOptionTypePlatformRecovery,\r
776 LOAD_OPTION_ACTIVE,\r
777 L"Default PlatformRecovery",\r
778 FilePath,\r
779 NULL,\r
780 0\r
781 );\r
782 ASSERT_EFI_ERROR (Status);\r
0dc3fb06
RN
783 LoadOptions = EfiBootManagerGetLoadOptions (&LoadOptionCount, LoadOptionTypePlatformRecovery);\r
784 if (EfiBootManagerFindLoadOption (&LoadOption, LoadOptions, LoadOptionCount) == -1) {\r
785 for (Index = 0; Index < LoadOptionCount; Index++) {\r
786 //\r
787 // The PlatformRecovery#### options are sorted by OptionNumber.\r
788 // Find the the smallest unused number as the new OptionNumber.\r
789 //\r
790 if (LoadOptions[Index].OptionNumber != Index) {\r
791 break;\r
792 }\r
793 }\r
794 LoadOption.OptionNumber = Index;\r
795 Status = EfiBootManagerLoadOptionToVariable (&LoadOption);\r
796 ASSERT_EFI_ERROR (Status);\r
797 }\r
d175f4e6
RN
798 EfiBootManagerFreeLoadOption (&LoadOption);\r
799 FreePool (FilePath);\r
0dc3fb06 800 EfiBootManagerFreeLoadOptions (LoadOptions, LoadOptionCount);\r
d175f4e6 801\r
f4cd24da
RN
802 //\r
803 // Report Status Code to indicate connecting drivers will happen\r
804 //\r
805 REPORT_STATUS_CODE (\r
806 EFI_PROGRESS_CODE,\r
807 (EFI_SOFTWARE_DXE_BS_DRIVER | EFI_SW_DXE_BS_PC_BEGIN_CONNECTING_DRIVERS)\r
808 );\r
809\r
8537bd7e
RN
810 //\r
811 // Initialize ConnectConIn event before calling platform code.\r
812 //\r
813 if (PcdGetBool (PcdConInConnectOnDemand)) {\r
814 Status = gBS->CreateEventEx (\r
815 EVT_NOTIFY_SIGNAL,\r
816 TPL_CALLBACK,\r
817 BdsDxeOnConnectConInCallBack,\r
818 NULL,\r
819 &gConnectConInEventGuid,\r
820 &gConnectConInEvent\r
821 );\r
822 if (EFI_ERROR (Status)) {\r
823 gConnectConInEvent = NULL;\r
824 }\r
825 }\r
826\r
f4cd24da
RN
827 //\r
828 // Do the platform init, can be customized by OEM/IBV\r
829 // Possible things that can be done in PlatformBootManagerBeforeConsole:\r
830 // > Update console variable: 1. include hot-plug devices; 2. Clear ConIn and add SOL for AMT\r
831 // > Register new Driver#### or Boot####\r
d1102dba 832 // > Register new Key####: e.g.: F12\r
f4cd24da
RN
833 // > Signal ReadyToLock event\r
834 // > Authentication action: 1. connect Auth devices; 2. Identify auto logon user.\r
835 //\r
67e9ab84 836 PERF_INMODULE_BEGIN("PlatformBootManagerBeforeConsole");\r
f4cd24da 837 PlatformBootManagerBeforeConsole ();\r
67e9ab84 838 PERF_INMODULE_END("PlatformBootManagerBeforeConsole");\r
f4cd24da
RN
839\r
840 //\r
841 // Initialize hotkey service\r
842 //\r
843 EfiBootManagerStartHotkeyService (&HotkeyTriggered);\r
844\r
845 //\r
1634214d 846 // Execute Driver Options\r
f4cd24da 847 //\r
1634214d
RN
848 LoadOptions = EfiBootManagerGetLoadOptions (&LoadOptionCount, LoadOptionTypeDriver);\r
849 ProcessLoadOptions (LoadOptions, LoadOptionCount);\r
850 EfiBootManagerFreeLoadOptions (LoadOptions, LoadOptionCount);\r
f4cd24da
RN
851\r
852 //\r
853 // Connect consoles\r
854 //\r
67e9ab84 855 PERF_INMODULE_BEGIN("EfiBootManagerConnectAllDefaultConsoles");\r
f4cd24da
RN
856 if (PcdGetBool (PcdConInConnectOnDemand)) {\r
857 EfiBootManagerConnectConsoleVariable (ConOut);\r
858 EfiBootManagerConnectConsoleVariable (ErrOut);\r
f4cd24da 859 //\r
8537bd7e 860 // Do not connect ConIn devices when lazy ConIn feature is ON.\r
f4cd24da 861 //\r
f4cd24da
RN
862 } else {\r
863 EfiBootManagerConnectAllDefaultConsoles ();\r
864 }\r
67e9ab84 865 PERF_INMODULE_END("EfiBootManagerConnectAllDefaultConsoles");\r
f4cd24da
RN
866\r
867 //\r
868 // Do the platform specific action after the console is ready\r
869 // Possible things that can be done in PlatformBootManagerAfterConsole:\r
870 // > Console post action:\r
871 // > Dynamically switch output mode from 100x31 to 80x25 for certain senarino\r
872 // > Signal console ready platform customized event\r
873 // > Run diagnostics like memory testing\r
874 // > Connect certain devices\r
875 // > Dispatch aditional option roms\r
876 // > Special boot: e.g.: USB boot, enter UI\r
d1102dba 877 //\r
67e9ab84 878 PERF_INMODULE_BEGIN("PlatformBootManagerAfterConsole");\r
f4cd24da 879 PlatformBootManagerAfterConsole ();\r
67e9ab84 880 PERF_INMODULE_END("PlatformBootManagerAfterConsole");\r
6fb8b96d
KM
881\r
882 //\r
883 // If any component set PcdTestKeyUsed to TRUE because use of a test key\r
884 // was detected, then display a warning message on the debug log and the console\r
885 //\r
886 if (PcdGetBool (PcdTestKeyUsed)) {\r
887 DEBUG ((DEBUG_ERROR, "**********************************\n"));\r
888 DEBUG ((DEBUG_ERROR, "** WARNING: Test Key is used. **\n"));\r
889 DEBUG ((DEBUG_ERROR, "**********************************\n"));\r
890 Print (L"** WARNING: Test Key is used. **\n");\r
891 }\r
892\r
68456d8a
RN
893 //\r
894 // Boot to Boot Manager Menu when EFI_OS_INDICATIONS_BOOT_TO_FW_UI is set. Skip HotkeyBoot\r
895 //\r
896 DataSize = sizeof (UINT64);\r
897 Status = gRT->GetVariable (\r
898 EFI_OS_INDICATIONS_VARIABLE_NAME,\r
899 &gEfiGlobalVariableGuid,\r
900 NULL,\r
901 &DataSize,\r
902 &OsIndication\r
903 );\r
904 if (EFI_ERROR (Status)) {\r
905 OsIndication = 0;\r
906 }\r
f4cd24da
RN
907\r
908 DEBUG_CODE (\r
1634214d 909 EFI_BOOT_MANAGER_LOAD_OPTION_TYPE LoadOptionType;\r
68456d8a 910 DEBUG ((EFI_D_INFO, "[Bds]OsIndication: %016x\n", OsIndication));\r
1634214d
RN
911 DEBUG ((EFI_D_INFO, "[Bds]=============Begin Load Options Dumping ...=============\n"));\r
912 for (LoadOptionType = 0; LoadOptionType < LoadOptionTypeMax; LoadOptionType++) {\r
f4cd24da 913 DEBUG ((\r
1634214d
RN
914 EFI_D_INFO, " %s Options:\n",\r
915 mBdsLoadOptionName[LoadOptionType]\r
f4cd24da 916 ));\r
1634214d
RN
917 LoadOptions = EfiBootManagerGetLoadOptions (&LoadOptionCount, LoadOptionType);\r
918 for (Index = 0; Index < LoadOptionCount; Index++) {\r
919 DEBUG ((\r
920 EFI_D_INFO, " %s%04x: %s \t\t 0x%04x\n",\r
921 mBdsLoadOptionName[LoadOptionType],\r
922 LoadOptions[Index].OptionNumber,\r
923 LoadOptions[Index].Description,\r
924 LoadOptions[Index].Attributes\r
925 ));\r
926 }\r
927 EfiBootManagerFreeLoadOptions (LoadOptions, LoadOptionCount);\r
f4cd24da 928 }\r
1634214d
RN
929 DEBUG ((EFI_D_INFO, "[Bds]=============End Load Options Dumping=============\n"));\r
930 );\r
f4cd24da
RN
931\r
932 //\r
e58f1ae5 933 // BootManagerMenu doesn't contain the correct information when return status is EFI_NOT_FOUND.\r
f4cd24da 934 //\r
e58f1ae5 935 BootManagerMenuStatus = EfiBootManagerGetBootManagerMenu (&BootManagerMenu);\r
1634214d 936\r
68456d8a
RN
937 BootFwUi = (BOOLEAN) ((OsIndication & EFI_OS_INDICATIONS_BOOT_TO_FW_UI) != 0);\r
938 PlatformRecovery = (BOOLEAN) ((OsIndication & EFI_OS_INDICATIONS_START_PLATFORM_RECOVERY) != 0);\r
1634214d
RN
939 //\r
940 // Clear EFI_OS_INDICATIONS_BOOT_TO_FW_UI to acknowledge OS\r
d1102dba 941 //\r
68456d8a
RN
942 if (BootFwUi || PlatformRecovery) {\r
943 OsIndication &= ~((UINT64) (EFI_OS_INDICATIONS_BOOT_TO_FW_UI | EFI_OS_INDICATIONS_START_PLATFORM_RECOVERY));\r
f4cd24da 944 Status = gRT->SetVariable (\r
cc4812f6 945 EFI_OS_INDICATIONS_VARIABLE_NAME,\r
f4cd24da
RN
946 &gEfiGlobalVariableGuid,\r
947 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
948 sizeof(UINT64),\r
949 &OsIndication\r
950 );\r
951 //\r
952 // Changing the content without increasing its size with current variable implementation shouldn't fail.\r
953 //\r
954 ASSERT_EFI_ERROR (Status);\r
1634214d 955 }\r
f4cd24da 956\r
1634214d
RN
957 //\r
958 // Launch Boot Manager Menu directly when EFI_OS_INDICATIONS_BOOT_TO_FW_UI is set. Skip HotkeyBoot\r
959 //\r
e58f1ae5 960 if (BootFwUi && (BootManagerMenuStatus != EFI_NOT_FOUND)) {\r
f4cd24da
RN
961 //\r
962 // Follow generic rule, Call BdsDxeOnConnectConInCallBack to connect ConIn before enter UI\r
963 //\r
964 if (PcdGetBool (PcdConInConnectOnDemand)) {\r
965 BdsDxeOnConnectConInCallBack (NULL, NULL);\r
966 }\r
967\r
968 //\r
1634214d 969 // Directly enter the setup page.\r
f4cd24da 970 //\r
1634214d 971 EfiBootManagerBoot (&BootManagerMenu);\r
f4cd24da
RN
972 }\r
973\r
68456d8a
RN
974 if (!PlatformRecovery) {\r
975 //\r
976 // Execute SysPrep####\r
977 //\r
978 LoadOptions = EfiBootManagerGetLoadOptions (&LoadOptionCount, LoadOptionTypeSysPrep);\r
979 ProcessLoadOptions (LoadOptions, LoadOptionCount);\r
980 EfiBootManagerFreeLoadOptions (LoadOptions, LoadOptionCount);\r
1634214d 981\r
68456d8a
RN
982 //\r
983 // Execute Key####\r
984 //\r
67e9ab84 985 PERF_INMODULE_BEGIN ("BdsWait");\r
68456d8a 986 BdsWait (HotkeyTriggered);\r
67e9ab84 987 PERF_INMODULE_END ("BdsWait");\r
68456d8a
RN
988 //\r
989 // BdsReadKeys() can be removed after all keyboard drivers invoke callback in timer callback.\r
990 //\r
991 BdsReadKeys ();\r
1634214d 992\r
68456d8a 993 EfiBootManagerHotkeyBoot ();\r
1634214d 994\r
68456d8a 995 if (BootNext != NULL) {\r
0e6584e3
RN
996 //\r
997 // Delete "BootNext" NV variable before transferring control to it to prevent loops.\r
998 //\r
999 Status = gRT->SetVariable (\r
1000 EFI_BOOT_NEXT_VARIABLE_NAME,\r
1001 &gEfiGlobalVariableGuid,\r
1002 0,\r
1003 0,\r
1004 NULL\r
1005 );\r
1006 //\r
1007 // Deleting NV variable shouldn't fail unless it doesn't exist.\r
1008 //\r
1009 ASSERT (Status == EFI_SUCCESS || Status == EFI_NOT_FOUND);\r
1010\r
1011 //\r
1012 // Boot to "BootNext"\r
1013 //\r
68456d8a
RN
1014 UnicodeSPrint (BootNextVariableName, sizeof (BootNextVariableName), L"Boot%04x", *BootNext);\r
1015 Status = EfiBootManagerVariableToLoadOption (BootNextVariableName, &LoadOption);\r
1016 if (!EFI_ERROR (Status)) {\r
d175f4e6
RN
1017 EfiBootManagerBoot (&LoadOption);\r
1018 EfiBootManagerFreeLoadOption (&LoadOption);\r
d1102dba 1019 if ((LoadOption.Status == EFI_SUCCESS) &&\r
e58f1ae5
SW
1020 (BootManagerMenuStatus != EFI_NOT_FOUND) &&\r
1021 (LoadOption.OptionNumber != BootManagerMenu.OptionNumber)) {\r
68456d8a
RN
1022 //\r
1023 // Boot to Boot Manager Menu upon EFI_SUCCESS\r
f5cbc197 1024 // Exception: Do not boot again when the BootNext points to Boot Manager Menu.\r
68456d8a
RN
1025 //\r
1026 EfiBootManagerBoot (&BootManagerMenu);\r
1027 }\r
f4cd24da
RN
1028 }\r
1029 }\r
68456d8a
RN
1030\r
1031 do {\r
1032 //\r
1033 // Retry to boot if any of the boot succeeds\r
1034 //\r
1035 LoadOptions = EfiBootManagerGetLoadOptions (&LoadOptionCount, LoadOptionTypeBoot);\r
e58f1ae5 1036 BootSuccess = BootBootOptions (LoadOptions, LoadOptionCount, (BootManagerMenuStatus != EFI_NOT_FOUND) ? &BootManagerMenu : NULL);\r
68456d8a
RN
1037 EfiBootManagerFreeLoadOptions (LoadOptions, LoadOptionCount);\r
1038 } while (BootSuccess);\r
f4cd24da
RN
1039 }\r
1040\r
f50db933
RN
1041 if (BootManagerMenuStatus != EFI_NOT_FOUND) {\r
1042 EfiBootManagerFreeLoadOption (&BootManagerMenu);\r
1043 }\r
1044\r
68456d8a
RN
1045 if (!BootSuccess) {\r
1046 LoadOptions = EfiBootManagerGetLoadOptions (&LoadOptionCount, LoadOptionTypePlatformRecovery);\r
1047 ProcessLoadOptions (LoadOptions, LoadOptionCount);\r
1048 EfiBootManagerFreeLoadOptions (LoadOptions, LoadOptionCount);\r
f4cd24da 1049 }\r
68456d8a
RN
1050\r
1051 DEBUG ((EFI_D_ERROR, "[Bds] Unable to boot!\n"));\r
1010873b 1052 PlatformBootManagerUnableToBoot ();\r
68456d8a 1053 CpuDeadLoop ();\r
f4cd24da
RN
1054}\r
1055\r
1056/**\r
1057 Set the variable and report the error through status code upon failure.\r
1058\r
1059 @param VariableName A Null-terminated string that is the name of the vendor's variable.\r
1060 Each VariableName is unique for each VendorGuid. VariableName must\r
1061 contain 1 or more characters. If VariableName is an empty string,\r
1062 then EFI_INVALID_PARAMETER is returned.\r
1063 @param VendorGuid A unique identifier for the vendor.\r
1064 @param Attributes Attributes bitmask to set for the variable.\r
d1102dba 1065 @param DataSize The size in bytes of the Data buffer. Unless the EFI_VARIABLE_APPEND_WRITE,\r
4073f85d 1066 or EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS attribute is set, a size of zero\r
d1102dba
LG
1067 causes the variable to be deleted. When the EFI_VARIABLE_APPEND_WRITE attribute is\r
1068 set, then a SetVariable() call with a DataSize of zero will not cause any change to\r
1069 the variable value (the timestamp associated with the variable may be updated however\r
1070 even if no new data value is provided,see the description of the\r
1071 EFI_VARIABLE_AUTHENTICATION_2 descriptor below. In this case the DataSize will not\r
1072 be zero since the EFI_VARIABLE_AUTHENTICATION_2 descriptor will be populated).\r
f4cd24da
RN
1073 @param Data The contents for the variable.\r
1074\r
1075 @retval EFI_SUCCESS The firmware has successfully stored the variable and its data as\r
1076 defined by the Attributes.\r
1077 @retval EFI_INVALID_PARAMETER An invalid combination of attribute bits, name, and GUID was supplied, or the\r
1078 DataSize exceeds the maximum allowed.\r
1079 @retval EFI_INVALID_PARAMETER VariableName is an empty string.\r
1080 @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the variable and its data.\r
1081 @retval EFI_DEVICE_ERROR The variable could not be retrieved due to a hardware error.\r
1082 @retval EFI_WRITE_PROTECTED The variable in question is read-only.\r
1083 @retval EFI_WRITE_PROTECTED The variable in question cannot be deleted.\r
4073f85d
ZC
1084 @retval EFI_SECURITY_VIOLATION The variable could not be written due to EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACESS\r
1085 being set, but the AuthInfo does NOT pass the validation check carried out by the firmware.\r
f4cd24da
RN
1086\r
1087 @retval EFI_NOT_FOUND The variable trying to be updated or deleted was not found.\r
1088**/\r
1089EFI_STATUS\r
1090BdsDxeSetVariableAndReportStatusCodeOnError (\r
1091 IN CHAR16 *VariableName,\r
1092 IN EFI_GUID *VendorGuid,\r
1093 IN UINT32 Attributes,\r
1094 IN UINTN DataSize,\r
1095 IN VOID *Data\r
1096 )\r
1097{\r
1098 EFI_STATUS Status;\r
1099 EDKII_SET_VARIABLE_STATUS *SetVariableStatus;\r
1100 UINTN NameSize;\r
1101\r
1102 Status = gRT->SetVariable (\r
1103 VariableName,\r
1104 VendorGuid,\r
1105 Attributes,\r
1106 DataSize,\r
1107 Data\r
1108 );\r
1109 if (EFI_ERROR (Status)) {\r
1110 NameSize = StrSize (VariableName);\r
1111 SetVariableStatus = AllocatePool (sizeof (EDKII_SET_VARIABLE_STATUS) + NameSize + DataSize);\r
1112 if (SetVariableStatus != NULL) {\r
1113 CopyGuid (&SetVariableStatus->Guid, VendorGuid);\r
1114 SetVariableStatus->NameSize = NameSize;\r
1115 SetVariableStatus->DataSize = DataSize;\r
1116 SetVariableStatus->SetStatus = Status;\r
1117 SetVariableStatus->Attributes = Attributes;\r
1118 CopyMem (SetVariableStatus + 1, VariableName, NameSize);\r
1119 CopyMem (((UINT8 *) (SetVariableStatus + 1)) + NameSize, Data, DataSize);\r
1120\r
1121 REPORT_STATUS_CODE_EX (\r
1122 EFI_ERROR_CODE,\r
1123 PcdGet32 (PcdErrorCodeSetVariable),\r
1124 0,\r
1125 NULL,\r
1126 &gEdkiiStatusCodeDataTypeVariableGuid,\r
1127 SetVariableStatus,\r
1128 sizeof (EDKII_SET_VARIABLE_STATUS) + NameSize + DataSize\r
1129 );\r
1130\r
1131 FreePool (SetVariableStatus);\r
1132 }\r
1133 }\r
1134\r
1135 return Status;\r
1136}\r