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