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