VirtualBox

source: vbox/trunk/src/VBox/Devices/EFI/Firmware2/VBoxPkg/Library/VBoxGenericBdsLib/BdsMisc.c@ 46284

Last change on this file since 46284 was 33676, checked in by vboxsync, 14 years ago

scm cleanup run.

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette