]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Library/GenericBdsLib/BdsMisc.c
Clean up BDS lib to remove assembly files, remove R8Lib.h & R8Lib.c by applying Mde...
[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 static BOOLEAN mFeaturerSwitch = TRUE;
21 static 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 /**
74 The function will go through the driver optoin link list, load and start
75 every driver the driver optoin device path point to.
76
77 @param BdsDriverLists The header of the current driver option link list
78
79 **/
80 VOID
81 EFIAPI
82 BdsLibLoadDrivers (
83 IN LIST_ENTRY *BdsDriverLists
84 )
85 {
86 EFI_STATUS Status;
87 LIST_ENTRY *Link;
88 BDS_COMMON_OPTION *Option;
89 EFI_HANDLE ImageHandle;
90 EFI_LOADED_IMAGE_PROTOCOL *ImageInfo;
91 UINTN ExitDataSize;
92 CHAR16 *ExitData;
93 BOOLEAN ReconnectAll;
94
95 ReconnectAll = FALSE;
96
97 //
98 // Process the driver option
99 //
100 for (Link = BdsDriverLists->ForwardLink; Link != BdsDriverLists; Link = Link->ForwardLink) {
101 Option = CR (Link, BDS_COMMON_OPTION, Link, BDS_LOAD_OPTION_SIGNATURE);
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 // If a driver load option is marked as LOAD_OPTION_FORCE_RECONNECT,
111 // then all of the EFI drivers in the system will be disconnected and
112 // reconnected after the last driver load option is processed.
113 //
114 if (IS_LOAD_OPTION_TYPE (Option->Attribute, LOAD_OPTION_FORCE_RECONNECT)) {
115 ReconnectAll = TRUE;
116 }
117 //
118 // Make sure the driver path is connected.
119 //
120 BdsLibConnectDevicePath (Option->DevicePath);
121
122 //
123 // Load and start the image that Driver#### describes
124 //
125 Status = gBS->LoadImage (
126 FALSE,
127 mBdsImageHandle,
128 Option->DevicePath,
129 NULL,
130 0,
131 &ImageHandle
132 );
133
134 if (!EFI_ERROR (Status)) {
135 gBS->HandleProtocol (ImageHandle, &gEfiLoadedImageProtocolGuid, (VOID **) &ImageInfo);
136
137 //
138 // Verify whether this image is a driver, if not,
139 // exit it and continue to parse next load option
140 //
141 if (ImageInfo->ImageCodeType != EfiBootServicesCode && ImageInfo->ImageCodeType != EfiRuntimeServicesCode) {
142 gBS->Exit (ImageHandle, EFI_INVALID_PARAMETER, 0, NULL);
143 continue;
144 }
145
146 if (Option->LoadOptionsSize != 0) {
147 ImageInfo->LoadOptionsSize = Option->LoadOptionsSize;
148 ImageInfo->LoadOptions = Option->LoadOptions;
149 }
150 //
151 // Before calling the image, enable the Watchdog Timer for
152 // the 5 Minute period
153 //
154 gBS->SetWatchdogTimer (5 * 60, 0x0000, 0x00, NULL);
155
156 Status = gBS->StartImage (ImageHandle, &ExitDataSize, &ExitData);
157 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Driver Return Status = %r\n", Status));
158
159 //
160 // Clear the Watchdog Timer after the image returns
161 //
162 gBS->SetWatchdogTimer (0x0000, 0x0000, 0x0000, NULL);
163 }
164 }
165 //
166 // Process the LOAD_OPTION_FORCE_RECONNECT driver option
167 //
168 if (ReconnectAll) {
169 BdsLibDisconnectAllEfi ();
170 BdsLibConnectAll ();
171 }
172
173 }
174
175
176 /**
177 Get the Option Number that does not used
178 Try to locate the specific option variable one by one untile find a free number
179
180 @param VariableName Indicate if the boot#### or driver#### option
181
182 @return The Minimal Free Option Number
183
184 **/
185 UINT16
186 BdsLibGetFreeOptionNumber (
187 IN CHAR16 *VariableName
188 )
189 {
190 UINT16 Number;
191 UINTN Index;
192 CHAR16 StrTemp[10];
193 UINT16 *OptionBuffer;
194 UINTN OptionSize;
195
196 //
197 // Try to find the minimum free number from 0, 1, 2, 3....
198 //
199 Index = 0;
200 do {
201 if (*VariableName == 'B') {
202 UnicodeSPrint (StrTemp, sizeof (StrTemp), L"Boot%04x", Index);
203 } else {
204 UnicodeSPrint (StrTemp, sizeof (StrTemp), L"Driver%04x", Index);
205 }
206 //
207 // try if the option number is used
208 //
209 OptionBuffer = BdsLibGetVariableAndSize (
210 StrTemp,
211 &gEfiGlobalVariableGuid,
212 &OptionSize
213 );
214 if (OptionBuffer == NULL) {
215 break;
216 }
217 Index++;
218 } while (1);
219
220 Number = (UINT16) Index;
221 return Number;
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 ZeroMem (OptionName, sizeof (OptionName));
276
277 TempOptionSize = 0;
278 TempOptionPtr = BdsLibGetVariableAndSize (
279 VariableName,
280 &gEfiGlobalVariableGuid,
281 &TempOptionSize
282 );
283
284 //
285 // Compare with current option variable
286 //
287 for (Index = 0; Index < TempOptionSize / sizeof (UINT16); Index++) {
288
289 if (*VariableName == 'B') {
290 UnicodeSPrint (OptionName, sizeof (OptionName), L"Boot%04x", TempOptionPtr[Index]);
291 } else {
292 UnicodeSPrint (OptionName, sizeof (OptionName), L"Driver%04x", TempOptionPtr[Index]);
293 }
294
295 OptionPtr = BdsLibGetVariableAndSize (
296 OptionName,
297 &gEfiGlobalVariableGuid,
298 &OptionSize
299 );
300 if (OptionPtr == NULL) {
301 continue;
302 }
303 TempPtr = OptionPtr;
304 TempPtr += sizeof (UINT32) + sizeof (UINT16);
305 Description = (CHAR16 *) TempPtr;
306 TempPtr += StrSize ((CHAR16 *) TempPtr);
307 OptionDevicePath = (EFI_DEVICE_PATH_PROTOCOL *) TempPtr;
308
309 //
310 // Notes: the description may will change base on the GetStringToken
311 //
312 if (CompareMem (OptionDevicePath, DevicePath, GetDevicePathSize (OptionDevicePath)) == 0) {
313 if (CompareMem (Description, String, StrSize (Description)) == 0) {
314 //
315 // Got the option, so just return
316 //
317 gBS->FreePool (OptionPtr);
318 gBS->FreePool (TempOptionPtr);
319 return EFI_SUCCESS;
320 } else {
321 //
322 // Option description changed, need update.
323 //
324 UpdateDescription = TRUE;
325 gBS->FreePool (OptionPtr);
326 break;
327 }
328 }
329
330 gBS->FreePool (OptionPtr);
331 }
332
333 OptionSize = sizeof (UINT32) + sizeof (UINT16) + StrSize (String);
334 OptionSize += GetDevicePathSize (DevicePath);
335 OptionPtr = AllocateZeroPool (OptionSize);
336 TempPtr = OptionPtr;
337 *(UINT32 *) TempPtr = LOAD_OPTION_ACTIVE;
338 TempPtr += sizeof (UINT32);
339 *(UINT16 *) TempPtr = (UINT16) GetDevicePathSize (DevicePath);
340 TempPtr += sizeof (UINT16);
341 CopyMem (TempPtr, String, StrSize (String));
342 TempPtr += StrSize (String);
343 CopyMem (TempPtr, DevicePath, GetDevicePathSize (DevicePath));
344
345 if (UpdateDescription) {
346 //
347 // The number in option#### to be updated
348 //
349 RegisterOptionNumber = TempOptionPtr[Index];
350 } else {
351 //
352 // The new option#### number
353 //
354 RegisterOptionNumber = BdsLibGetFreeOptionNumber(VariableName);
355 }
356
357 if (*VariableName == 'B') {
358 UnicodeSPrint (OptionName, sizeof (OptionName), L"Boot%04x", RegisterOptionNumber);
359 } else {
360 UnicodeSPrint (OptionName, sizeof (OptionName), L"Driver%04x", RegisterOptionNumber);
361 }
362
363 Status = gRT->SetVariable (
364 OptionName,
365 &gEfiGlobalVariableGuid,
366 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,
367 OptionSize,
368 OptionPtr
369 );
370 //
371 // Return if only need to update a changed description or fail to set option.
372 //
373 if (EFI_ERROR (Status) || UpdateDescription) {
374 gBS->FreePool (OptionPtr);
375 gBS->FreePool (TempOptionPtr);
376 return Status;
377 }
378
379 gBS->FreePool (OptionPtr);
380
381 //
382 // Update the option order variable
383 //
384
385 //
386 // If no option order
387 //
388 if (TempOptionSize == 0) {
389 BootOrderEntry = 0;
390 Status = gRT->SetVariable (
391 VariableName,
392 &gEfiGlobalVariableGuid,
393 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,
394 sizeof (UINT16),
395 &BootOrderEntry
396 );
397 if (EFI_ERROR (Status)) {
398 gBS->FreePool (TempOptionPtr);
399 return Status;
400 }
401 return EFI_SUCCESS;
402 }
403
404 //
405 // Append the new option number to the original option order
406 //
407 OrderItemNum = (TempOptionSize / sizeof (UINT16)) + 1 ;
408 OptionOrderPtr = AllocateZeroPool ( OrderItemNum * sizeof (UINT16));
409 CopyMem (OptionOrderPtr, TempOptionPtr, (OrderItemNum - 1) * sizeof (UINT16));
410
411 OptionOrderPtr[Index] = RegisterOptionNumber;
412
413 Status = gRT->SetVariable (
414 VariableName,
415 &gEfiGlobalVariableGuid,
416 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,
417 OrderItemNum * sizeof (UINT16),
418 OptionOrderPtr
419 );
420 if (EFI_ERROR (Status)) {
421 gBS->FreePool (TempOptionPtr);
422 gBS->FreePool (OptionOrderPtr);
423 return Status;
424 }
425
426 gBS->FreePool (TempOptionPtr);
427 gBS->FreePool (OptionOrderPtr);
428
429 return EFI_SUCCESS;
430 }
431
432
433 /**
434 Build the boot#### or driver#### option from the VariableName, the
435 build boot#### or driver#### will also be linked to BdsCommonOptionList
436
437 @param BdsCommonOptionList The header of the boot#### or driver#### option
438 link list
439 @param VariableName EFI Variable name indicate if it is boot#### or
440 driver####
441
442 @retval BDS_COMMON_OPTION Get the option just been created
443 @retval NULL Failed to get the new option
444
445 **/
446 BDS_COMMON_OPTION *
447 EFIAPI
448 BdsLibVariableToOption (
449 IN OUT LIST_ENTRY *BdsCommonOptionList,
450 IN CHAR16 *VariableName
451 )
452 {
453 UINT32 Attribute;
454 UINT16 FilePathSize;
455 UINT8 *Variable;
456 UINT8 *TempPtr;
457 UINTN VariableSize;
458 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
459 BDS_COMMON_OPTION *Option;
460 VOID *LoadOptions;
461 UINT32 LoadOptionsSize;
462 CHAR16 *Description;
463 UINT8 NumOff;
464 //
465 // Read the variable. We will never free this data.
466 //
467 Variable = BdsLibGetVariableAndSize (
468 VariableName,
469 &gEfiGlobalVariableGuid,
470 &VariableSize
471 );
472 if (Variable == NULL) {
473 return NULL;
474 }
475 //
476 // Notes: careful defined the variable of Boot#### or
477 // Driver####, consider use some macro to abstract the code
478 //
479 //
480 // Get the option attribute
481 //
482 TempPtr = Variable;
483 Attribute = *(UINT32 *) Variable;
484 TempPtr += sizeof (UINT32);
485
486 //
487 // Get the option's device path size
488 //
489 FilePathSize = *(UINT16 *) TempPtr;
490 TempPtr += sizeof (UINT16);
491
492 //
493 // Get the option's description string
494 //
495 Description = (CHAR16 *) TempPtr;
496
497 //
498 // Get the option's description string size
499 //
500 TempPtr += StrSize ((CHAR16 *) TempPtr);
501
502 //
503 // Get the option's device path
504 //
505 DevicePath = (EFI_DEVICE_PATH_PROTOCOL *) TempPtr;
506 TempPtr += FilePathSize;
507
508 LoadOptions = TempPtr;
509 LoadOptionsSize = (UINT32) (VariableSize - (UINTN) (TempPtr - Variable));
510
511 //
512 // The Console variables may have multiple device paths, so make
513 // an Entry for each one.
514 //
515 Option = AllocateZeroPool (sizeof (BDS_COMMON_OPTION));
516 if (Option == NULL) {
517 return NULL;
518 }
519
520 Option->Signature = BDS_LOAD_OPTION_SIGNATURE;
521 Option->DevicePath = AllocateZeroPool (GetDevicePathSize (DevicePath));
522 CopyMem (Option->DevicePath, DevicePath, GetDevicePathSize (DevicePath));
523 Option->Attribute = Attribute;
524 Option->Description = AllocateZeroPool (StrSize (Description));
525 CopyMem (Option->Description, Description, StrSize (Description));
526 Option->LoadOptions = AllocateZeroPool (LoadOptionsSize);
527 CopyMem (Option->LoadOptions, LoadOptions, LoadOptionsSize);
528 Option->LoadOptionsSize = LoadOptionsSize;
529
530 //
531 // Get the value from VariableName Unicode string
532 // since the ISO standard assumes ASCII equivalent abbreviations, we can be safe in converting this
533 // Unicode stream to ASCII without any loss in meaning.
534 //
535 if (*VariableName == 'B') {
536 NumOff = sizeof (L"Boot")/sizeof(CHAR16) -1 ;
537 Option->BootCurrent = (UINT16) ((VariableName[NumOff] -'0') * 0x1000);
538 Option->BootCurrent = (UINT16) (Option->BootCurrent + ((VariableName[NumOff+1]-'0') * 0x100));
539 Option->BootCurrent = (UINT16) (Option->BootCurrent + ((VariableName[NumOff+2]-'0') * 0x10));
540 Option->BootCurrent = (UINT16) (Option->BootCurrent + ((VariableName[NumOff+3]-'0')));
541 }
542 //
543 // Insert active entry to BdsDeviceList
544 //
545 if ((Option->Attribute & LOAD_OPTION_ACTIVE) == LOAD_OPTION_ACTIVE) {
546 InsertTailList (BdsCommonOptionList, &Option->Link);
547 gBS->FreePool (Variable);
548 return Option;
549 }
550
551 gBS->FreePool (Variable);
552 gBS->FreePool (Option);
553 return NULL;
554
555 }
556
557
558 /**
559 Process BootOrder, or DriverOrder variables, by calling
560 BdsLibVariableToOption () for each UINT16 in the variables.
561
562 @param BdsCommonOptionList The header of the option list base on variable
563 VariableName
564 @param VariableName EFI Variable name indicate the BootOrder or
565 DriverOrder
566
567 @retval EFI_SUCCESS Success create the boot option or driver option
568 list
569 @retval EFI_OUT_OF_RESOURCES Failed to get the boot option or driver option list
570
571 **/
572 EFI_STATUS
573 EFIAPI
574 BdsLibBuildOptionFromVar (
575 IN LIST_ENTRY *BdsCommonOptionList,
576 IN CHAR16 *VariableName
577 )
578 {
579 UINT16 *OptionOrder;
580 UINTN OptionOrderSize;
581 UINTN Index;
582 BDS_COMMON_OPTION *Option;
583 CHAR16 OptionName[20];
584
585 //
586 // Zero Buffer in order to get all BOOT#### variables
587 //
588 ZeroMem (OptionName, sizeof (OptionName));
589
590 //
591 // Read the BootOrder, or DriverOrder variable.
592 //
593 OptionOrder = BdsLibGetVariableAndSize (
594 VariableName,
595 &gEfiGlobalVariableGuid,
596 &OptionOrderSize
597 );
598 if (OptionOrder == NULL) {
599 return EFI_OUT_OF_RESOURCES;
600 }
601
602 for (Index = 0; Index < OptionOrderSize / sizeof (UINT16); Index++) {
603 if (*VariableName == 'B') {
604 UnicodeSPrint (OptionName, sizeof (OptionName), L"Boot%04x", OptionOrder[Index]);
605 } else {
606 UnicodeSPrint (OptionName, sizeof (OptionName), L"Driver%04x", OptionOrder[Index]);
607 }
608
609 Option = BdsLibVariableToOption (BdsCommonOptionList, OptionName);
610 Option->BootCurrent = OptionOrder[Index];
611
612 }
613
614 gBS->FreePool (OptionOrder);
615
616 return EFI_SUCCESS;
617 }
618
619
620 /**
621 Get boot mode by looking up configuration table and parsing HOB list
622
623 @param BootMode Boot mode from PEI handoff HOB.
624
625 @retval EFI_SUCCESS Successfully get boot mode
626
627 **/
628 EFI_STATUS
629 EFIAPI
630 BdsLibGetBootMode (
631 OUT EFI_BOOT_MODE *BootMode
632 )
633 {
634 *BootMode = GetBootModeHob ();
635
636 return EFI_SUCCESS;
637 }
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 @return 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 /**
695 Delete the instance in Multi which matches partly with Single instance
696
697 @param Multi A pointer to a multi-instance device path data
698 structure.
699 @param Single A pointer to a single-instance device path data
700 structure.
701
702 @return This function will remove the device path instances in Multi which partly
703 match with the Single, and return the result device path. If there is no
704 remaining device path as a result, this function will return NULL.
705
706 **/
707 EFI_DEVICE_PATH_PROTOCOL *
708 EFIAPI
709 BdsLibDelPartMatchInstance (
710 IN EFI_DEVICE_PATH_PROTOCOL *Multi,
711 IN EFI_DEVICE_PATH_PROTOCOL *Single
712 )
713 {
714 EFI_DEVICE_PATH_PROTOCOL *Instance;
715 EFI_DEVICE_PATH_PROTOCOL *NewDevicePath;
716 EFI_DEVICE_PATH_PROTOCOL *TempNewDevicePath;
717 UINTN InstanceSize;
718 UINTN SingleDpSize;
719 UINTN Size;
720
721 NewDevicePath = NULL;
722 TempNewDevicePath = NULL;
723
724 if (Multi == NULL || Single == NULL) {
725 return Multi;
726 }
727
728 Instance = GetNextDevicePathInstance (&Multi, &InstanceSize);
729 SingleDpSize = GetDevicePathSize (Single) - END_DEVICE_PATH_LENGTH;
730 InstanceSize -= END_DEVICE_PATH_LENGTH;
731
732 while (Instance != NULL) {
733
734 Size = (SingleDpSize < InstanceSize) ? SingleDpSize : InstanceSize;
735
736 if ((CompareMem (Instance, Single, Size) != 0)) {
737 //
738 // Append the device path instance which does not match with Single
739 //
740 TempNewDevicePath = NewDevicePath;
741 NewDevicePath = AppendDevicePathInstance (NewDevicePath, Instance);
742 SafeFreePool(TempNewDevicePath);
743 }
744 SafeFreePool(Instance);
745 Instance = GetNextDevicePathInstance (&Multi, &InstanceSize);
746 InstanceSize -= END_DEVICE_PATH_LENGTH;
747 }
748
749 return NewDevicePath;
750 }
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 is contained within Multi
763 @retval FALSE The Single is not match within Multi
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 || !Single) {
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 gBS->FreePool (DevicePathInst);
794 return TRUE;
795 }
796
797 gBS->FreePool (DevicePathInst);
798 DevicePathInst = GetNextDevicePathInstance (&DevicePath, &Size);
799 }
800
801 return FALSE;
802 }
803
804
805 /**
806 This function prints a series of strings.
807
808 @param ConOut Pointer to EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
809 @param ... A variable argument list containing series of
810 strings, the last string must be NULL.
811
812 @retval EFI_SUCCESS Success print out the string using ConOut.
813 @retval EFI_STATUS Return the status of the ConOut->OutputString ().
814
815 **/
816 EFI_STATUS
817 EFIAPI
818 BdsLibOutputStrings (
819 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *ConOut,
820 ...
821 )
822 {
823 VA_LIST args;
824 EFI_STATUS Status;
825 CHAR16 *String;
826
827 Status = EFI_SUCCESS;
828 VA_START (args, ConOut);
829
830 while (!EFI_ERROR (Status)) {
831 //
832 // If String is NULL, then it's the end of the list
833 //
834 String = VA_ARG (args, CHAR16 *);
835 if (!String) {
836 break;
837 }
838
839 Status = ConOut->OutputString (ConOut, String);
840
841 if (EFI_ERROR (Status)) {
842 break;
843 }
844 }
845
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 gBS->FreePool (StringBuffer1);
981 gBS->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 /**
995 Get the headers (dos, image, optional header) from an image
996
997 @param Device SimpleFileSystem device handle
998 @param FileName File name for the image
999 @param DosHeader Pointer to dos header
1000 @param ImageHeader Pointer to image header
1001 @param OptionalHeader Pointer to optional header
1002
1003 @retval EFI_SUCCESS Successfully get the machine type.
1004 @retval EFI_NOT_FOUND The file is not found.
1005 @retval EFI_LOAD_ERROR File is not a valid image file.
1006
1007 **/
1008 EFI_STATUS
1009 EFIAPI
1010 BdsLibGetImageHeader (
1011 IN EFI_HANDLE Device,
1012 IN CHAR16 *FileName,
1013 OUT EFI_IMAGE_DOS_HEADER *DosHeader,
1014 OUT EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
1015 )
1016 {
1017 EFI_STATUS Status;
1018 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *Volume;
1019 EFI_FILE_HANDLE Root;
1020 EFI_FILE_HANDLE ThisFile;
1021 UINTN BufferSize;
1022 UINT64 FileSize;
1023 EFI_FILE_INFO *Info;
1024
1025 Root = NULL;
1026 ThisFile = NULL;
1027 //
1028 // Handle the file system interface to the device
1029 //
1030 Status = gBS->HandleProtocol (
1031 Device,
1032 &gEfiSimpleFileSystemProtocolGuid,
1033 (VOID *) &Volume
1034 );
1035 if (EFI_ERROR (Status)) {
1036 goto Done;
1037 }
1038
1039 Status = Volume->OpenVolume (
1040 Volume,
1041 &Root
1042 );
1043 if (EFI_ERROR (Status)) {
1044 goto Done;
1045 }
1046
1047 Status = Root->Open (Root, &ThisFile, FileName, EFI_FILE_MODE_READ, 0);
1048 if (EFI_ERROR (Status)) {
1049 goto Done;
1050 }
1051
1052 //
1053 // Get file size
1054 //
1055 BufferSize = SIZE_OF_EFI_FILE_INFO + 200;
1056 do {
1057 Info = NULL;
1058 Status = gBS->AllocatePool (EfiBootServicesData, BufferSize, (VOID **) &Info);
1059 if (EFI_ERROR (Status)) {
1060 goto Done;
1061 }
1062 Status = ThisFile->GetInfo (
1063 ThisFile,
1064 &gEfiFileInfoGuid,
1065 &BufferSize,
1066 Info
1067 );
1068 if (!EFI_ERROR (Status)) {
1069 break;
1070 }
1071 if (Status != EFI_BUFFER_TOO_SMALL) {
1072 goto Done;
1073 }
1074 gBS->FreePool (Info);
1075 } while (TRUE);
1076
1077 FileSize = Info->FileSize;
1078 gBS->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 VOID
1134 EFIAPI
1135 BdsSetMemoryTypeInformationVariable (
1136 EFI_EVENT Event,
1137 VOID *Context
1138 )
1139 /*++
1140
1141 Routine Description:
1142
1143 This routine is a notification function for legayc boot or exit boot
1144 service event. It will adjust the memory information for different
1145 memory type and save them into the variables for next boot
1146
1147 Arguments:
1148
1149 Event - The event that triggered this notification function
1150 Context - Pointer to the notification functions context
1151
1152 Returns:
1153
1154 None.
1155
1156 --*/
1157 {
1158 EFI_STATUS Status;
1159 EFI_MEMORY_TYPE_INFORMATION *PreviousMemoryTypeInformation;
1160 EFI_MEMORY_TYPE_INFORMATION *CurrentMemoryTypeInformation;
1161 UINTN VariableSize;
1162 BOOLEAN UpdateRequired;
1163 UINTN Index;
1164 UINTN Index1;
1165 UINT32 Previous;
1166 UINT32 Current;
1167 UINT32 Next;
1168 EFI_HOB_GUID_TYPE *GuidHob;
1169
1170 UpdateRequired = FALSE;
1171
1172 //
1173 // Retrieve the current memory usage statistics. If they are not found, then
1174 // no adjustments can be made to the Memory Type Information variable.
1175 //
1176 Status = EfiGetSystemConfigurationTable (
1177 &gEfiMemoryTypeInformationGuid,
1178 (VOID **) &CurrentMemoryTypeInformation
1179 );
1180 if (EFI_ERROR (Status)) {
1181 return;
1182 }
1183
1184 //
1185 // Get the Memory Type Information settings from Hob if they exist,
1186 // PEI is responsible for getting them from variable and build a Hob to save them.
1187 // If the previous Memory Type Information is not available, then set defaults
1188 //
1189 GuidHob = GetFirstGuidHob (&gEfiMemoryTypeInformationGuid);
1190 if (GuidHob == NULL) {
1191 //
1192 // If Platform has not built Memory Type Info into the Hob, just return.
1193 //
1194 return;
1195 }
1196 PreviousMemoryTypeInformation = GET_GUID_HOB_DATA (GuidHob);
1197 VariableSize = GET_GUID_HOB_DATA_SIZE (GuidHob);
1198
1199 //
1200 // Use a heuristic to adjust the Memory Type Information for the next boot
1201 //
1202 for (Index = 0; PreviousMemoryTypeInformation[Index].Type != EfiMaxMemoryType; Index++) {
1203
1204 Current = 0;
1205 for (Index1 = 0; CurrentMemoryTypeInformation[Index1].Type != EfiMaxMemoryType; Index1++) {
1206 if (PreviousMemoryTypeInformation[Index].Type == CurrentMemoryTypeInformation[Index1].Type) {
1207 Current = CurrentMemoryTypeInformation[Index1].NumberOfPages;
1208 break;
1209 }
1210 }
1211
1212 if (CurrentMemoryTypeInformation[Index1].Type == EfiMaxMemoryType) {
1213 continue;
1214 }
1215
1216 Previous = PreviousMemoryTypeInformation[Index].NumberOfPages;
1217
1218 //
1219 // Write next varible to 125% * current and Inconsistent Memory Reserved across bootings may lead to S4 fail
1220 //
1221 if (Current > Previous) {
1222 Next = Current + (Current >> 2);
1223 } else {
1224 Next = Previous;
1225 }
1226 if (Next > 0 && Next < 4) {
1227 Next = 4;
1228 }
1229
1230 if (Next != Previous) {
1231 PreviousMemoryTypeInformation[Index].NumberOfPages = Next;
1232 UpdateRequired = TRUE;
1233 }
1234
1235 }
1236
1237 //
1238 // If any changes were made to the Memory Type Information settings, then set the new variable value
1239 //
1240 if (UpdateRequired) {
1241 Status = gRT->SetVariable (
1242 EFI_MEMORY_TYPE_INFORMATION_VARIABLE_NAME,
1243 &gEfiMemoryTypeInformationGuid,
1244 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
1245 VariableSize,
1246 PreviousMemoryTypeInformation
1247 );
1248 }
1249
1250 return;
1251 }
1252
1253
1254 /**
1255 This routine register a function to adjust the different type memory page number just before booting
1256 and save the updated info into the variable for next boot to use
1257
1258 **/
1259 VOID
1260 EFIAPI
1261 BdsLibSaveMemoryTypeInformation (
1262 VOID
1263 )
1264 {
1265 EFI_STATUS Status;
1266 EFI_EVENT ReadyToBootEvent;
1267
1268 Status = EfiCreateEventReadyToBootEx (
1269 TPL_CALLBACK,
1270 BdsSetMemoryTypeInformationVariable,
1271 NULL,
1272 &ReadyToBootEvent
1273 );
1274 if (EFI_ERROR (Status)) {
1275 DEBUG ((DEBUG_ERROR,"Bds Set Memory Type Informationa Variable Fails\n"));
1276 }
1277
1278 }
1279
1280