BdsEntry.c 39 KB

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