]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Library/GenericBdsLib/BdsMisc.c
1) Sync in patch for EDK 1063 Boot option enumeration algorithm enhancement
[mirror_edk2.git] / MdeModulePkg / Library / GenericBdsLib / BdsMisc.c
1 /** @file
2
3 Copyright (c) 2004 - 2007, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12 Module Name:
13
14 BdsMisc.c
15
16 Abstract:
17
18 Misc BDS library function
19
20
21 **/
22
23 #include "InternalBdsLib.h"
24
25
26 #define MAX_STRING_LEN 200
27
28 static BOOLEAN mFeaturerSwitch = TRUE;
29 static BOOLEAN mResetRequired = FALSE;
30
31 extern UINT16 gPlatformBootTimeOutDefault;
32
33
34 /**
35 Return the default value for system Timeout variable.
36
37 None
38
39 @return Timeout value.
40
41 **/
42 UINT16
43 BdsLibGetTimeout (
44 VOID
45 )
46 {
47 UINT16 Timeout;
48 UINTN Size;
49 EFI_STATUS Status;
50
51 //
52 // Return Timeout variable or 0xffff if no valid
53 // Timeout variable exists.
54 //
55 Size = sizeof (UINT16);
56 Status = gRT->GetVariable (L"Timeout", &gEfiGlobalVariableGuid, NULL, &Size, &Timeout);
57 if (!EFI_ERROR (Status)) {
58 return Timeout;
59 }
60 //
61 // To make the current EFI Automatic-Test activity possible, just add
62 // following code to make AutoBoot enabled when this variable is not
63 // present.
64 // This code should be removed later.
65 //
66 Timeout = PcdGet16 (PcdPlatformBootTimeOutDefault);
67
68 //
69 // Notes: Platform should set default variable if non exists on all error cases!!!
70 //
71 Status = gRT->SetVariable (
72 L"Timeout",
73 &gEfiGlobalVariableGuid,
74 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,
75 sizeof (UINT16),
76 &Timeout
77 );
78 return Timeout;
79 }
80
81
82 /**
83 The function will go through the driver optoin link list, load and start
84 every driver the driver optoin device path point to.
85
86 @param BdsDriverLists The header of the current driver option link list
87
88 @return None
89
90 **/
91 VOID
92 BdsLibLoadDrivers (
93 IN LIST_ENTRY *BdsDriverLists
94 )
95 {
96 EFI_STATUS Status;
97 LIST_ENTRY *Link;
98 BDS_COMMON_OPTION *Option;
99 EFI_HANDLE ImageHandle;
100 EFI_LOADED_IMAGE_PROTOCOL *ImageInfo;
101 UINTN ExitDataSize;
102 CHAR16 *ExitData;
103 BOOLEAN ReconnectAll;
104
105 ReconnectAll = FALSE;
106
107 //
108 // Process the driver option
109 //
110 for (Link = BdsDriverLists->ForwardLink; Link != BdsDriverLists; Link = Link->ForwardLink) {
111 Option = CR (Link, BDS_COMMON_OPTION, Link, BDS_LOAD_OPTION_SIGNATURE);
112 //
113 // If a load option is not marked as LOAD_OPTION_ACTIVE,
114 // the boot manager will not automatically load the option.
115 //
116 if (!IS_LOAD_OPTION_TYPE (Option->Attribute, LOAD_OPTION_ACTIVE)) {
117 continue;
118 }
119 //
120 // If a driver load option is marked as LOAD_OPTION_FORCE_RECONNECT,
121 // then all of the EFI drivers in the system will be disconnected and
122 // reconnected after the last driver load option is processed.
123 //
124 if (IS_LOAD_OPTION_TYPE (Option->Attribute, LOAD_OPTION_FORCE_RECONNECT)) {
125 ReconnectAll = TRUE;
126 }
127 //
128 // Make sure the driver path is connected.
129 //
130 BdsLibConnectDevicePath (Option->DevicePath);
131
132 //
133 // Load and start the image that Driver#### describes
134 //
135 Status = gBS->LoadImage (
136 FALSE,
137 mBdsImageHandle,
138 Option->DevicePath,
139 NULL,
140 0,
141 &ImageHandle
142 );
143
144 if (!EFI_ERROR (Status)) {
145 gBS->HandleProtocol (ImageHandle, &gEfiLoadedImageProtocolGuid, (VOID **) &ImageInfo);
146
147 //
148 // Verify whether this image is a driver, if not,
149 // exit it and continue to parse next load option
150 //
151 if (ImageInfo->ImageCodeType != EfiBootServicesCode && ImageInfo->ImageCodeType != EfiRuntimeServicesCode) {
152 gBS->Exit (ImageHandle, EFI_INVALID_PARAMETER, 0, NULL);
153 continue;
154 }
155
156 if (Option->LoadOptionsSize != 0) {
157 ImageInfo->LoadOptionsSize = Option->LoadOptionsSize;
158 ImageInfo->LoadOptions = Option->LoadOptions;
159 }
160 //
161 // Before calling the image, enable the Watchdog Timer for
162 // the 5 Minute period
163 //
164 gBS->SetWatchdogTimer (5 * 60, 0x0000, 0x00, NULL);
165
166 Status = gBS->StartImage (ImageHandle, &ExitDataSize, &ExitData);
167 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Driver Return Status = %r\n", Status));
168
169 //
170 // Clear the Watchdog Timer after the image returns
171 //
172 gBS->SetWatchdogTimer (0x0000, 0x0000, 0x0000, NULL);
173 }
174 }
175 //
176 // Process the LOAD_OPTION_FORCE_RECONNECT driver option
177 //
178 if (ReconnectAll) {
179 BdsLibDisconnectAllEfi ();
180 BdsLibConnectAll ();
181 }
182
183 }
184
185
186 /**
187 Get the Option Number that does not used
188 Try to locate the specific option variable one by one untile find a free number
189
190 @param VariableName Indicate if the boot#### or driver#### option
191
192 @return The Minimal Free Option Number
193
194 **/
195 UINT16
196 BdsLibGetFreeOptionNumber (
197 IN CHAR16 *VariableName
198 )
199 {
200 UINT16 Number;
201 UINTN Index;
202 CHAR16 StrTemp[10];
203 UINT16 *OptionBuffer;
204 UINTN OptionSize;
205
206 //
207 // Try to find the minimum free number from 0, 1, 2, 3....
208 //
209 Index = 0;
210 do {
211 if (*VariableName == 'B') {
212 UnicodeSPrint (StrTemp, sizeof (StrTemp), L"Boot%04x", Index);
213 } else {
214 UnicodeSPrint (StrTemp, sizeof (StrTemp), L"Driver%04x", Index);
215 }
216 //
217 // try if the option number is used
218 //
219 OptionBuffer = BdsLibGetVariableAndSize (
220 StrTemp,
221 &gEfiGlobalVariableGuid,
222 &OptionSize
223 );
224 if (OptionBuffer == NULL) {
225 break;
226 }
227 Index++;
228 } while (1);
229
230 Number = (UINT16) Index;
231 return Number;
232 }
233
234
235 /**
236 This function will register the new boot#### or driver#### option base on
237 the VariableName. The new registered boot#### or driver#### will be linked
238 to BdsOptionList and also update to the VariableName. After the boot#### or
239 driver#### updated, the BootOrder or DriverOrder will also be updated.
240
241 @param BdsOptionList The header of the boot#### or driver#### link list
242 @param DevicePath The device path which the boot#### or driver####
243 option present
244 @param String The description of the boot#### or driver####
245 @param VariableName Indicate if the boot#### or driver#### option
246
247 @retval EFI_SUCCESS The boot#### or driver#### have been success
248 registered
249 @retval EFI_STATUS Return the status of gRT->SetVariable ().
250
251 **/
252 EFI_STATUS
253 BdsLibRegisterNewOption (
254 IN LIST_ENTRY *BdsOptionList,
255 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath,
256 IN CHAR16 *String,
257 IN CHAR16 *VariableName
258 )
259 {
260 EFI_STATUS Status;
261 UINTN Index;
262 UINT16 RegisterOptionNumber;
263 UINT16 *TempOptionPtr;
264 UINTN TempOptionSize;
265 UINT16 *OptionOrderPtr;
266 VOID *OptionPtr;
267 UINTN OptionSize;
268 UINT8 *TempPtr;
269 EFI_DEVICE_PATH_PROTOCOL *OptionDevicePath;
270 CHAR16 *Description;
271 CHAR16 OptionName[10];
272 BOOLEAN UpdateDescription;
273 UINT16 BootOrderEntry;
274 UINTN OrderItemNum;
275
276
277 OptionPtr = NULL;
278 OptionSize = 0;
279 TempPtr = NULL;
280 OptionDevicePath = NULL;
281 Description = NULL;
282 OptionOrderPtr = NULL;
283 UpdateDescription = FALSE;
284 ZeroMem (OptionName, sizeof (OptionName));
285
286 TempOptionSize = 0;
287 TempOptionPtr = BdsLibGetVariableAndSize (
288 VariableName,
289 &gEfiGlobalVariableGuid,
290 &TempOptionSize
291 );
292
293 //
294 // Compare with current option variable
295 //
296 for (Index = 0; Index < TempOptionSize / sizeof (UINT16); Index++) {
297
298 if (*VariableName == 'B') {
299 UnicodeSPrint (OptionName, sizeof (OptionName), L"Boot%04x", TempOptionPtr[Index]);
300 } else {
301 UnicodeSPrint (OptionName, sizeof (OptionName), L"Driver%04x", TempOptionPtr[Index]);
302 }
303
304 OptionPtr = BdsLibGetVariableAndSize (
305 OptionName,
306 &gEfiGlobalVariableGuid,
307 &OptionSize
308 );
309 if (OptionPtr == NULL) {
310 continue;
311 }
312 TempPtr = OptionPtr;
313 TempPtr += sizeof (UINT32) + sizeof (UINT16);
314 Description = (CHAR16 *) TempPtr;
315 TempPtr += StrSize ((CHAR16 *) TempPtr);
316 OptionDevicePath = (EFI_DEVICE_PATH_PROTOCOL *) TempPtr;
317
318 //
319 // Notes: the description may will change base on the GetStringToken
320 //
321 if (CompareMem (OptionDevicePath, DevicePath, GetDevicePathSize (OptionDevicePath)) == 0) {
322 if (CompareMem (Description, String, StrSize (Description)) == 0) {
323 //
324 // Got the option, so just return
325 //
326 gBS->FreePool (OptionPtr);
327 gBS->FreePool (TempOptionPtr);
328 return EFI_SUCCESS;
329 } else {
330 //
331 // Option description changed, need update.
332 //
333 UpdateDescription = TRUE;
334 gBS->FreePool (OptionPtr);
335 break;
336 }
337 }
338
339 gBS->FreePool (OptionPtr);
340 }
341
342 OptionSize = sizeof (UINT32) + sizeof (UINT16) + StrSize (String);
343 OptionSize += GetDevicePathSize (DevicePath);
344 OptionPtr = AllocateZeroPool (OptionSize);
345 TempPtr = OptionPtr;
346 *(UINT32 *) TempPtr = LOAD_OPTION_ACTIVE;
347 TempPtr += sizeof (UINT32);
348 *(UINT16 *) TempPtr = (UINT16) GetDevicePathSize (DevicePath);
349 TempPtr += sizeof (UINT16);
350 CopyMem (TempPtr, String, StrSize (String));
351 TempPtr += StrSize (String);
352 CopyMem (TempPtr, DevicePath, GetDevicePathSize (DevicePath));
353
354 if (UpdateDescription) {
355 //
356 // The number in option#### to be updated
357 //
358 RegisterOptionNumber = TempOptionPtr[Index];
359 } else {
360 //
361 // The new option#### number
362 //
363 RegisterOptionNumber = BdsLibGetFreeOptionNumber(VariableName);
364 }
365
366 if (*VariableName == 'B') {
367 UnicodeSPrint (OptionName, sizeof (OptionName), L"Boot%04x", RegisterOptionNumber);
368 } else {
369 UnicodeSPrint (OptionName, sizeof (OptionName), L"Driver%04x", RegisterOptionNumber);
370 }
371
372 Status = gRT->SetVariable (
373 OptionName,
374 &gEfiGlobalVariableGuid,
375 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,
376 OptionSize,
377 OptionPtr
378 );
379 //
380 // Return if only need to update a changed description or fail to set option.
381 //
382 if (EFI_ERROR (Status) || UpdateDescription) {
383 gBS->FreePool (OptionPtr);
384 gBS->FreePool (TempOptionPtr);
385 return Status;
386 }
387
388 gBS->FreePool (OptionPtr);
389
390 //
391 // Update the option order variable
392 //
393
394 //
395 // If no option order
396 //
397 if (TempOptionSize == 0) {
398 BootOrderEntry = 0;
399 Status = gRT->SetVariable (
400 VariableName,
401 &gEfiGlobalVariableGuid,
402 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,
403 sizeof (UINT16),
404 &BootOrderEntry
405 );
406 if (EFI_ERROR (Status)) {
407 gBS->FreePool (TempOptionPtr);
408 return Status;
409 }
410 return EFI_SUCCESS;
411 }
412
413 //
414 // Append the new option number to the original option order
415 //
416 OrderItemNum = (TempOptionSize / sizeof (UINT16)) + 1 ;
417 OptionOrderPtr = AllocateZeroPool ( OrderItemNum * sizeof (UINT16));
418 CopyMem (OptionOrderPtr, TempOptionPtr, (OrderItemNum - 1) * sizeof (UINT16));
419
420 OptionOrderPtr[Index] = RegisterOptionNumber;
421
422 Status = gRT->SetVariable (
423 VariableName,
424 &gEfiGlobalVariableGuid,
425 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,
426 OrderItemNum * sizeof (UINT16),
427 OptionOrderPtr
428 );
429 if (EFI_ERROR (Status)) {
430 gBS->FreePool (TempOptionPtr);
431 gBS->FreePool (OptionOrderPtr);
432 return Status;
433 }
434
435 gBS->FreePool (TempOptionPtr);
436 gBS->FreePool (OptionOrderPtr);
437
438 return EFI_SUCCESS;
439 }
440
441
442 /**
443 Build the boot#### or driver#### option from the VariableName, the
444 build boot#### or driver#### will also be linked to BdsCommonOptionList
445
446 @param BdsCommonOptionList The header of the boot#### or driver#### option
447 link list
448 @param VariableName EFI Variable name indicate if it is boot#### or
449 driver####
450
451 @retval BDS_COMMON_OPTION Get the option just been created
452 @retval NULL Failed to get the new option
453
454 **/
455 BDS_COMMON_OPTION *
456 BdsLibVariableToOption (
457 IN OUT LIST_ENTRY *BdsCommonOptionList,
458 IN CHAR16 *VariableName
459 )
460 {
461 UINT32 Attribute;
462 UINT16 FilePathSize;
463 UINT8 *Variable;
464 UINT8 *TempPtr;
465 UINTN VariableSize;
466 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
467 BDS_COMMON_OPTION *Option;
468 VOID *LoadOptions;
469 UINT32 LoadOptionsSize;
470 CHAR16 *Description;
471 UINT8 NumOff;
472 //
473 // Read the variable. We will never free this data.
474 //
475 Variable = BdsLibGetVariableAndSize (
476 VariableName,
477 &gEfiGlobalVariableGuid,
478 &VariableSize
479 );
480 if (Variable == NULL) {
481 return NULL;
482 }
483 //
484 // Notes: careful defined the variable of Boot#### or
485 // Driver####, consider use some macro to abstract the code
486 //
487 //
488 // Get the option attribute
489 //
490 TempPtr = Variable;
491 Attribute = *(UINT32 *) Variable;
492 TempPtr += sizeof (UINT32);
493
494 //
495 // Get the option's device path size
496 //
497 FilePathSize = *(UINT16 *) TempPtr;
498 TempPtr += sizeof (UINT16);
499
500 //
501 // Get the option's description string
502 //
503 Description = (CHAR16 *) TempPtr;
504
505 //
506 // Get the option's description string size
507 //
508 TempPtr += StrSize ((CHAR16 *) TempPtr);
509
510 //
511 // Get the option's device path
512 //
513 DevicePath = (EFI_DEVICE_PATH_PROTOCOL *) TempPtr;
514 TempPtr += FilePathSize;
515
516 LoadOptions = TempPtr;
517 LoadOptionsSize = (UINT32) (VariableSize - (UINTN) (TempPtr - Variable));
518
519 //
520 // The Console variables may have multiple device paths, so make
521 // an Entry for each one.
522 //
523 Option = AllocateZeroPool (sizeof (BDS_COMMON_OPTION));
524 if (Option == NULL) {
525 return NULL;
526 }
527
528 Option->Signature = BDS_LOAD_OPTION_SIGNATURE;
529 Option->DevicePath = AllocateZeroPool (GetDevicePathSize (DevicePath));
530 CopyMem (Option->DevicePath, DevicePath, GetDevicePathSize (DevicePath));
531 Option->Attribute = Attribute;
532 Option->Description = AllocateZeroPool (StrSize (Description));
533 CopyMem (Option->Description, Description, StrSize (Description));
534 Option->LoadOptions = AllocateZeroPool (LoadOptionsSize);
535 CopyMem (Option->LoadOptions, LoadOptions, LoadOptionsSize);
536 Option->LoadOptionsSize = LoadOptionsSize;
537
538 //
539 // Get the value from VariableName Unicode string
540 // since the ISO standard assumes ASCII equivalent abbreviations, we can be safe in converting this
541 // Unicode stream to ASCII without any loss in meaning.
542 //
543 if (*VariableName == 'B') {
544 NumOff = sizeof (L"Boot")/sizeof(CHAR16) -1 ;
545 Option->BootCurrent = (UINT16) ((VariableName[NumOff] -'0') * 0x1000);
546 Option->BootCurrent = (UINT16) (Option->BootCurrent + ((VariableName[NumOff+1]-'0') * 0x100));
547 Option->BootCurrent = (UINT16) (Option->BootCurrent + ((VariableName[NumOff+2]-'0') * 0x10));
548 Option->BootCurrent = (UINT16) (Option->BootCurrent + ((VariableName[NumOff+3]-'0')));
549 }
550 //
551 // Insert active entry to BdsDeviceList
552 //
553 if ((Option->Attribute & LOAD_OPTION_ACTIVE) == LOAD_OPTION_ACTIVE) {
554 InsertTailList (BdsCommonOptionList, &Option->Link);
555 gBS->FreePool (Variable);
556 return Option;
557 }
558
559 gBS->FreePool (Variable);
560 gBS->FreePool (Option);
561 return NULL;
562
563 }
564
565
566 /**
567 Process BootOrder, or DriverOrder variables, by calling
568 BdsLibVariableToOption () for each UINT16 in the variables.
569
570 @param BdsCommonOptionList The header of the option list base on variable
571 VariableName
572 @param VariableName EFI Variable name indicate the BootOrder or
573 DriverOrder
574
575 @retval EFI_SUCCESS Success create the boot option or driver option
576 list
577 @retval EFI_OUT_OF_RESOURCES Failed to get the boot option or driver option list
578
579 **/
580 EFI_STATUS
581 BdsLibBuildOptionFromVar (
582 IN LIST_ENTRY *BdsCommonOptionList,
583 IN CHAR16 *VariableName
584 )
585 {
586 UINT16 *OptionOrder;
587 UINTN OptionOrderSize;
588 UINTN Index;
589 BDS_COMMON_OPTION *Option;
590 CHAR16 OptionName[20];
591
592 //
593 // Zero Buffer in order to get all BOOT#### variables
594 //
595 ZeroMem (OptionName, sizeof (OptionName));
596
597 //
598 // Read the BootOrder, or DriverOrder variable.
599 //
600 OptionOrder = BdsLibGetVariableAndSize (
601 VariableName,
602 &gEfiGlobalVariableGuid,
603 &OptionOrderSize
604 );
605 if (OptionOrder == NULL) {
606 return EFI_OUT_OF_RESOURCES;
607 }
608
609 for (Index = 0; Index < OptionOrderSize / sizeof (UINT16); Index++) {
610 if (*VariableName == 'B') {
611 UnicodeSPrint (OptionName, sizeof (OptionName), L"Boot%04x", OptionOrder[Index]);
612 } else {
613 UnicodeSPrint (OptionName, sizeof (OptionName), L"Driver%04x", OptionOrder[Index]);
614 }
615
616 Option = BdsLibVariableToOption (BdsCommonOptionList, OptionName);
617 Option->BootCurrent = OptionOrder[Index];
618
619 }
620
621 gBS->FreePool (OptionOrder);
622
623 return EFI_SUCCESS;
624 }
625
626
627 /**
628 Get boot mode by looking up configuration table and parsing HOB list
629
630 @param BootMode Boot mode from PEI handoff HOB.
631
632 @retval EFI_SUCCESS Successfully get boot mode
633 @retval EFI_NOT_FOUND Can not find the current system boot mode
634
635 **/
636 EFI_STATUS
637 BdsLibGetBootMode (
638 OUT EFI_BOOT_MODE *BootMode
639 )
640 {
641 VOID *HobList;
642 EFI_STATUS Status;
643
644 //
645 // Get Hob list
646 //
647 Status = EfiGetSystemConfigurationTable (&gEfiHobListGuid, &HobList);
648 if (EFI_ERROR (Status)) {
649 DEBUG ((DEBUG_ERROR, "Hob list not found\n"));
650 *BootMode = 0;
651 return EFI_NOT_FOUND;
652 }
653
654 Status = R8_GetHobBootMode (HobList, BootMode);
655 if (EFI_ERROR (Status)) {
656 return EFI_NOT_FOUND;
657 }
658
659 return EFI_SUCCESS;
660 }
661
662
663 /**
664 Read the EFI variable (VendorGuid/Name) and return a dynamically allocated
665 buffer, and the size of the buffer. If failure return NULL.
666
667 @param Name String part of EFI variable name
668 @param VendorGuid GUID part of EFI variable name
669 @param VariableSize Returns the size of the EFI variable that was read
670
671 @return Dynamically allocated memory that contains a copy of the EFI variable.
672 @return Caller is responsible freeing the buffer.
673 @retval NULL Variable was not read
674
675 **/
676 VOID *
677 BdsLibGetVariableAndSize (
678 IN CHAR16 *Name,
679 IN EFI_GUID *VendorGuid,
680 OUT UINTN *VariableSize
681 )
682 {
683 EFI_STATUS Status;
684 UINTN BufferSize;
685 VOID *Buffer;
686
687 Buffer = NULL;
688
689 //
690 // Pass in a zero size buffer to find the required buffer size.
691 //
692 BufferSize = 0;
693 Status = gRT->GetVariable (Name, VendorGuid, NULL, &BufferSize, Buffer);
694 if (Status == EFI_BUFFER_TOO_SMALL) {
695 //
696 // Allocate the buffer to return
697 //
698 Buffer = AllocateZeroPool (BufferSize);
699 if (Buffer == NULL) {
700 return NULL;
701 }
702 //
703 // Read variable into the allocated buffer.
704 //
705 Status = gRT->GetVariable (Name, VendorGuid, NULL, &BufferSize, Buffer);
706 if (EFI_ERROR (Status)) {
707 BufferSize = 0;
708 }
709 }
710
711 *VariableSize = BufferSize;
712 return Buffer;
713 }
714
715
716 /**
717 Delete the instance in Multi which matches partly with Single instance
718
719 @param Multi A pointer to a multi-instance device path data
720 structure.
721 @param Single A pointer to a single-instance device path data
722 structure.
723
724 @return This function will remove the device path instances in Multi which partly
725 @return match with the Single, and return the result device path. If there is no
726 @return remaining device path as a result, this function will return NULL.
727
728 **/
729 EFI_DEVICE_PATH_PROTOCOL *
730 BdsLibDelPartMatchInstance (
731 IN EFI_DEVICE_PATH_PROTOCOL *Multi,
732 IN EFI_DEVICE_PATH_PROTOCOL *Single
733 )
734 {
735 EFI_DEVICE_PATH_PROTOCOL *Instance;
736 EFI_DEVICE_PATH_PROTOCOL *NewDevicePath;
737 EFI_DEVICE_PATH_PROTOCOL *TempNewDevicePath;
738 UINTN InstanceSize;
739 UINTN SingleDpSize;
740 UINTN Size;
741
742 NewDevicePath = NULL;
743 TempNewDevicePath = NULL;
744
745 if (Multi == NULL || Single == NULL) {
746 return Multi;
747 }
748
749 Instance = GetNextDevicePathInstance (&Multi, &InstanceSize);
750 SingleDpSize = GetDevicePathSize (Single) - END_DEVICE_PATH_LENGTH;
751 InstanceSize -= END_DEVICE_PATH_LENGTH;
752
753 while (Instance != NULL) {
754
755 Size = (SingleDpSize < InstanceSize) ? SingleDpSize : InstanceSize;
756
757 if ((CompareMem (Instance, Single, Size) != 0)) {
758 //
759 // Append the device path instance which does not match with Single
760 //
761 TempNewDevicePath = NewDevicePath;
762 NewDevicePath = AppendDevicePathInstance (NewDevicePath, Instance);
763 SafeFreePool(TempNewDevicePath);
764 }
765 SafeFreePool(Instance);
766 Instance = GetNextDevicePathInstance (&Multi, &InstanceSize);
767 InstanceSize -= END_DEVICE_PATH_LENGTH;
768 }
769
770 return NewDevicePath;
771 }
772
773
774 /**
775 Function compares a device path data structure to that of all the nodes of a
776 second device path instance.
777
778 @param Multi A pointer to a multi-instance device path data
779 structure.
780 @param Single A pointer to a single-instance device path data
781 structure.
782
783 @retval TRUE If the Single is contained within Multi
784 @retval FALSE The Single is not match within Multi
785
786 **/
787 BOOLEAN
788 BdsLibMatchDevicePaths (
789 IN EFI_DEVICE_PATH_PROTOCOL *Multi,
790 IN EFI_DEVICE_PATH_PROTOCOL *Single
791 )
792 {
793 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
794 EFI_DEVICE_PATH_PROTOCOL *DevicePathInst;
795 UINTN Size;
796
797 if (!Multi || !Single) {
798 return FALSE;
799 }
800
801 DevicePath = Multi;
802 DevicePathInst = GetNextDevicePathInstance (&DevicePath, &Size);
803
804 //
805 // Search for the match of 'Single' in 'Multi'
806 //
807 while (DevicePathInst != NULL) {
808 //
809 // If the single device path is found in multiple device paths,
810 // return success
811 //
812 if (CompareMem (Single, DevicePathInst, Size) == 0) {
813 gBS->FreePool (DevicePathInst);
814 return TRUE;
815 }
816
817 gBS->FreePool (DevicePathInst);
818 DevicePathInst = GetNextDevicePathInstance (&DevicePath, &Size);
819 }
820
821 return FALSE;
822 }
823
824
825 /**
826 This function prints a series of strings.
827
828 @param ConOut Pointer to EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
829 @param ... A variable argument list containing series of
830 strings, the last string must be NULL.
831
832 @retval EFI_SUCCESS Success print out the string using ConOut.
833 @retval EFI_STATUS Return the status of the ConOut->OutputString ().
834
835 **/
836 EFI_STATUS
837 BdsLibOutputStrings (
838 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *ConOut,
839 ...
840 )
841 {
842 VA_LIST args;
843 EFI_STATUS Status;
844 CHAR16 *String;
845
846 Status = EFI_SUCCESS;
847 VA_START (args, ConOut);
848
849 while (!EFI_ERROR (Status)) {
850 //
851 // If String is NULL, then it's the end of the list
852 //
853 String = VA_ARG (args, CHAR16 *);
854 if (!String) {
855 break;
856 }
857
858 Status = ConOut->OutputString (ConOut, String);
859
860 if (EFI_ERROR (Status)) {
861 break;
862 }
863 }
864
865 return Status;
866 }
867
868 //
869 // Following are BDS Lib functions which contain all the code about setup browser reset reminder feature.
870 // Setup Browser reset reminder feature is that an reset reminder will be given before user leaves the setup browser if
871 // user change any option setting which needs a reset to be effective, and the reset will be applied according to the user selection.
872 //
873
874
875 /**
876 Enable the setup browser reset reminder feature.
877 This routine is used in platform tip. If the platform policy need the feature, use the routine to enable it.
878
879 VOID
880
881 @return VOID
882
883 **/
884 VOID
885 EnableResetReminderFeature (
886 VOID
887 )
888 {
889 mFeaturerSwitch = TRUE;
890 }
891
892
893 /**
894 Disable the setup browser reset reminder feature.
895 This routine is used in platform tip. If the platform policy do not want the feature, use the routine to disable it.
896
897 VOID
898
899 @return VOID
900
901 **/
902 VOID
903 DisableResetReminderFeature (
904 VOID
905 )
906 {
907 mFeaturerSwitch = FALSE;
908 }
909
910
911 /**
912 Record the info that a reset is required.
913 A module boolean variable is used to record whether a reset is required.
914
915 VOID
916
917 @return VOID
918
919 **/
920 VOID
921 EnableResetRequired (
922 VOID
923 )
924 {
925 mResetRequired = TRUE;
926 }
927
928
929 /**
930 Record the info that no reset is required.
931 A module boolean variable is used to record whether a reset is required.
932
933 VOID
934
935 @return VOID
936
937 **/
938 VOID
939 DisableResetRequired (
940 VOID
941 )
942 {
943 mResetRequired = FALSE;
944 }
945
946
947 /**
948 Check whether platform policy enable the reset reminder feature. The default is enabled.
949
950 VOID
951
952 @return VOID
953
954 **/
955 BOOLEAN
956 IsResetReminderFeatureEnable (
957 VOID
958 )
959 {
960 return mFeaturerSwitch;
961 }
962
963
964 /**
965 Check if user changed any option setting which needs a system reset to be effective.
966
967 VOID
968
969 @return VOID
970
971 **/
972 BOOLEAN
973 IsResetRequired (
974 VOID
975 )
976 {
977 return mResetRequired;
978 }
979
980
981 /**
982 Check whether a reset is needed, and finish the reset reminder feature.
983 If a reset is needed, Popup a menu to notice user, and finish the feature
984 according to the user selection.
985
986 VOID
987
988 @return VOID
989
990 **/
991 VOID
992 SetupResetReminder (
993 VOID
994 )
995 {
996 EFI_INPUT_KEY Key;
997 CHAR16 *StringBuffer1;
998 CHAR16 *StringBuffer2;
999
1000
1001 //
1002 //check any reset required change is applied? if yes, reset system
1003 //
1004 if (IsResetReminderFeatureEnable ()) {
1005 if (IsResetRequired ()) {
1006
1007 StringBuffer1 = AllocateZeroPool (MAX_STRING_LEN * sizeof (CHAR16));
1008 ASSERT (StringBuffer1 != NULL);
1009 StringBuffer2 = AllocateZeroPool (MAX_STRING_LEN * sizeof (CHAR16));
1010 ASSERT (StringBuffer2 != NULL);
1011 StrCpy (StringBuffer1, L"Configuration changed. Reset to apply it Now ? ");
1012 StrCpy (StringBuffer2, L"Enter (YES) / Esc (NO)");
1013 //
1014 // Popup a menu to notice user
1015 //
1016 do {
1017 IfrLibCreatePopUp (2, &Key, StringBuffer1, StringBuffer2);
1018 } while ((Key.ScanCode != SCAN_ESC) && (Key.UnicodeChar != CHAR_CARRIAGE_RETURN));
1019
1020 gBS->FreePool (StringBuffer1);
1021 gBS->FreePool (StringBuffer2);
1022 //
1023 // If the user hits the YES Response key, reset
1024 //
1025 if ((Key.UnicodeChar == CHAR_CARRIAGE_RETURN)) {
1026 gRT->ResetSystem (EfiResetCold, EFI_SUCCESS, 0, NULL);
1027 }
1028 gST->ConOut->ClearScreen (gST->ConOut);
1029 }
1030 }
1031 }
1032
1033
1034 /**
1035 Get the headers (dos, image, optional header) from an image
1036
1037 @param Device SimpleFileSystem device handle
1038 @param FileName File name for the image
1039 @param DosHeader Pointer to dos header
1040 @param ImageHeader Pointer to image header
1041 @param OptionalHeader Pointer to optional header
1042
1043 @retval EFI_SUCCESS Successfully get the machine type.
1044 @retval EFI_NOT_FOUND The file is not found.
1045 @retval EFI_LOAD_ERROR File is not a valid image file.
1046
1047 **/
1048 EFI_STATUS
1049 BdsLibGetImageHeader (
1050 IN EFI_HANDLE Device,
1051 IN CHAR16 *FileName,
1052 OUT EFI_IMAGE_DOS_HEADER *DosHeader,
1053 OUT EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
1054 )
1055 {
1056 EFI_STATUS Status;
1057 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *Volume;
1058 EFI_FILE_HANDLE Root;
1059 EFI_FILE_HANDLE ThisFile;
1060 UINTN BufferSize;
1061 UINT64 FileSize;
1062 EFI_FILE_INFO *Info;
1063
1064 Root = NULL;
1065 ThisFile = NULL;
1066 //
1067 // Handle the file system interface to the device
1068 //
1069 Status = gBS->HandleProtocol (
1070 Device,
1071 &gEfiSimpleFileSystemProtocolGuid,
1072 (VOID *) &Volume
1073 );
1074 if (EFI_ERROR (Status)) {
1075 goto Done;
1076 }
1077
1078 Status = Volume->OpenVolume (
1079 Volume,
1080 &Root
1081 );
1082 if (EFI_ERROR (Status)) {
1083 goto Done;
1084 }
1085
1086 Status = Root->Open (Root, &ThisFile, FileName, EFI_FILE_MODE_READ, 0);
1087 if (EFI_ERROR (Status)) {
1088 goto Done;
1089 }
1090
1091 //
1092 // Get file size
1093 //
1094 BufferSize = SIZE_OF_EFI_FILE_INFO + 200;
1095 do {
1096 Info = NULL;
1097 Status = gBS->AllocatePool (EfiBootServicesData, BufferSize, (VOID **) &Info);
1098 if (EFI_ERROR (Status)) {
1099 goto Done;
1100 }
1101 Status = ThisFile->GetInfo (
1102 ThisFile,
1103 &gEfiFileInfoGuid,
1104 &BufferSize,
1105 Info
1106 );
1107 if (!EFI_ERROR (Status)) {
1108 break;
1109 }
1110 if (Status != EFI_BUFFER_TOO_SMALL) {
1111 goto Done;
1112 }
1113 gBS->FreePool (Info);
1114 } while (TRUE);
1115
1116 FileSize = Info->FileSize;
1117 gBS->FreePool (Info);
1118
1119 //
1120 // Read dos header
1121 //
1122 BufferSize = sizeof (EFI_IMAGE_DOS_HEADER);
1123 Status = ThisFile->Read (ThisFile, &BufferSize, DosHeader);
1124 if (EFI_ERROR (Status) ||
1125 BufferSize < sizeof (EFI_IMAGE_DOS_HEADER) ||
1126 FileSize <= DosHeader->e_lfanew ||
1127 DosHeader->e_magic != EFI_IMAGE_DOS_SIGNATURE) {
1128 Status = EFI_LOAD_ERROR;
1129 goto Done;
1130 }
1131
1132 //
1133 // Move to PE signature
1134 //
1135 Status = ThisFile->SetPosition (ThisFile, DosHeader->e_lfanew);
1136 if (EFI_ERROR (Status)) {
1137 Status = EFI_LOAD_ERROR;
1138 goto Done;
1139 }
1140
1141 //
1142 // Read and check PE signature
1143 //
1144 BufferSize = sizeof (EFI_IMAGE_OPTIONAL_HEADER_UNION);
1145 Status = ThisFile->Read (ThisFile, &BufferSize, Hdr.Pe32);
1146 if (EFI_ERROR (Status) ||
1147 BufferSize < sizeof (EFI_IMAGE_OPTIONAL_HEADER_UNION) ||
1148 Hdr.Pe32->Signature != EFI_IMAGE_NT_SIGNATURE) {
1149 Status = EFI_LOAD_ERROR;
1150 goto Done;
1151 }
1152
1153 //
1154 // Check PE32 or PE32+ magic
1155 //
1156 if (Hdr.Pe32->OptionalHeader.Magic != EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC &&
1157 Hdr.Pe32->OptionalHeader.Magic != EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC) {
1158 Status = EFI_LOAD_ERROR;
1159 goto Done;
1160 }
1161
1162 Done:
1163 if (ThisFile != NULL) {
1164 ThisFile->Close (ThisFile);
1165 }
1166 if (Root != NULL) {
1167 Root->Close (Root);
1168 }
1169 return Status;
1170 }
1171
1172 VOID
1173 EFIAPI
1174 BdsSetMemoryTypeInformationVariable (
1175 EFI_EVENT Event,
1176 VOID *Context
1177 )
1178 /*++
1179
1180 Routine Description:
1181
1182 This routine is a notification function for legayc boot or exit boot
1183 service event. It will adjust the memory information for different
1184 memory type and save them into the variables for next boot
1185
1186 Arguments:
1187
1188 Event - The event that triggered this notification function
1189 Context - Pointer to the notification functions context
1190
1191 Returns:
1192
1193 None.
1194
1195 --*/
1196 {
1197 EFI_STATUS Status;
1198 EFI_MEMORY_TYPE_INFORMATION *PreviousMemoryTypeInformation;
1199 EFI_MEMORY_TYPE_INFORMATION *CurrentMemoryTypeInformation;
1200 UINTN VariableSize;
1201 BOOLEAN UpdateRequired;
1202 UINTN Index;
1203 UINTN Index1;
1204 UINT32 Previous;
1205 UINT32 Current;
1206 UINT32 Next;
1207 VOID *HobList;
1208
1209 UpdateRequired = FALSE;
1210
1211 //
1212 // Retrieve the current memory usage statistics. If they are not found, then
1213 // no adjustments can be made to the Memory Type Information variable.
1214 //
1215 Status = EfiGetSystemConfigurationTable (
1216 &gEfiMemoryTypeInformationGuid,
1217 (VOID **) &CurrentMemoryTypeInformation
1218 );
1219 if (EFI_ERROR (Status)) {
1220 return;
1221 }
1222
1223 //
1224 // Get the Memory Type Information settings from Hob if they exist,
1225 // PEI is responsible for getting them from variable and build a Hob to save them.
1226 // If the previous Memory Type Information is not available, then set defaults
1227 //
1228 EfiGetSystemConfigurationTable (&gEfiHobListGuid, &HobList);
1229 Status = R8_GetNextGuidHob (&HobList, &gEfiMemoryTypeInformationGuid, (VOID **) &PreviousMemoryTypeInformation, &VariableSize);
1230 if (EFI_ERROR (Status) || PreviousMemoryTypeInformation == NULL) {
1231 //
1232 // If Platform has not built Memory Type Info into the Hob, just return.
1233 //
1234 return;
1235 }
1236
1237 //
1238 // Use a heuristic to adjust the Memory Type Information for the next boot
1239 //
1240 for (Index = 0; PreviousMemoryTypeInformation[Index].Type != EfiMaxMemoryType; Index++) {
1241
1242 Current = 0;
1243 for (Index1 = 0; CurrentMemoryTypeInformation[Index1].Type != EfiMaxMemoryType; Index1++) {
1244 if (PreviousMemoryTypeInformation[Index].Type == CurrentMemoryTypeInformation[Index1].Type) {
1245 Current = CurrentMemoryTypeInformation[Index1].NumberOfPages;
1246 break;
1247 }
1248 }
1249
1250 if (CurrentMemoryTypeInformation[Index1].Type == EfiMaxMemoryType) {
1251 continue;
1252 }
1253
1254 Previous = PreviousMemoryTypeInformation[Index].NumberOfPages;
1255
1256 //
1257 // Write next varible to 125% * current and Inconsistent Memory Reserved across bootings may lead to S4 fail
1258 //
1259 if (Current > Previous) {
1260 Next = Current + (Current >> 2);
1261 } else {
1262 Next = Previous;
1263 }
1264 if (Next > 0 && Next < 4) {
1265 Next = 4;
1266 }
1267
1268 if (Next != Previous) {
1269 PreviousMemoryTypeInformation[Index].NumberOfPages = Next;
1270 UpdateRequired = TRUE;
1271 }
1272
1273 }
1274
1275 //
1276 // If any changes were made to the Memory Type Information settings, then set the new variable value
1277 //
1278 if (UpdateRequired) {
1279 Status = gRT->SetVariable (
1280 EFI_MEMORY_TYPE_INFORMATION_VARIABLE_NAME,
1281 &gEfiMemoryTypeInformationGuid,
1282 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
1283 VariableSize,
1284 PreviousMemoryTypeInformation
1285 );
1286 }
1287
1288 return;
1289 }
1290
1291
1292 /**
1293 This routine register a function to adjust the different type memory page number just before booting
1294 and save the updated info into the variable for next boot to use
1295
1296 None
1297
1298 @return None.
1299
1300 **/
1301 VOID
1302 EFIAPI
1303 BdsLibSaveMemoryTypeInformation (
1304 VOID
1305 )
1306 {
1307 EFI_STATUS Status;
1308 EFI_EVENT ReadyToBootEvent;
1309
1310 Status = EfiCreateEventReadyToBootEx (
1311 TPL_CALLBACK,
1312 BdsSetMemoryTypeInformationVariable,
1313 NULL,
1314 &ReadyToBootEvent
1315 );
1316 if (EFI_ERROR (Status)) {
1317 DEBUG ((DEBUG_ERROR,"Bds Set Memory Type Informationa Variable Fails\n"));
1318 }
1319
1320 }
1321
1322
1323 /**
1324 return the current TPL, copied from the EDKII glue lib.
1325
1326 VOID
1327
1328 @return Current TPL
1329
1330 **/
1331 EFI_TPL
1332 BdsLibGetCurrentTpl (
1333 VOID
1334 )
1335 {
1336 EFI_TPL Tpl;
1337
1338 Tpl = gBS->RaiseTPL (TPL_HIGH_LEVEL);
1339 gBS->RestoreTPL (Tpl);
1340
1341 return Tpl;
1342 }