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