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