X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=IntelFrameworkModulePkg%2FLibrary%2FGenericBdsLib%2FBdsMisc.c;h=971188bd0a85620331b3bf5287f8bd7544518105;hb=3999f1feef2134f1bb7dc6f89dcb01e49765b392;hp=acf61ebcb6627a371ee3d466a7bd06fb0ad83743;hpb=5c08e1173703234cc2913757f237ee916087498a;p=mirror_edk2.git diff --git a/IntelFrameworkModulePkg/Library/GenericBdsLib/BdsMisc.c b/IntelFrameworkModulePkg/Library/GenericBdsLib/BdsMisc.c index acf61ebcb6..971188bd0a 100644 --- a/IntelFrameworkModulePkg/Library/GenericBdsLib/BdsMisc.c +++ b/IntelFrameworkModulePkg/Library/GenericBdsLib/BdsMisc.c @@ -1,8 +1,8 @@ /** @file Misc BDS library function -Copyright (c) 2004 - 2008, Intel Corporation.
-All rights reserved. This program and the accompanying materials +Copyright (c) 2004 - 2012, Intel Corporation. All rights reserved.
+This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License which accompanies this distribution. The full text of the license may be found at http://opensource.org/licenses/bsd-license.php @@ -22,42 +22,6 @@ BOOLEAN mResetRequired = FALSE; extern UINT16 gPlatformBootTimeOutDefault; - -/** - Return the default value for system Timeout variable. - - @return Timeout value. - -**/ -UINT16 -EFIAPI -BdsLibGetTimeout ( - VOID - ) -{ - UINT16 Timeout; - UINTN Size; - EFI_STATUS Status; - - // - // Return Timeout variable or 0xffff if no valid - // Timeout variable exists. - // - Size = sizeof (UINT16); - Status = gRT->GetVariable (L"Timeout", &gEfiGlobalVariableGuid, NULL, &Size, &Timeout); - if (EFI_ERROR (Status)) { - // - // According to UEFI 2.0 spec, it should treat the Timeout value as 0xffff - // (default value PcdPlatformBootTimeOutDefault) when L"Timeout" variable is not present. - // To make the current EFI Automatic-Test activity possible, platform can choose other value - // for automatic boot when the variable is not present. - // - Timeout = PcdGet16 (PcdPlatformBootTimeOutDefault); - } - - return Timeout; -} - /** The function will go through the driver option link list, load and start every driver the driver option device path point to. @@ -115,7 +79,7 @@ BdsLibLoadDrivers ( // Status = gBS->LoadImage ( FALSE, - mBdsImageHandle, + gImageHandle, Option->DevicePath, NULL, 0, @@ -293,6 +257,14 @@ BdsLibRegisterNewOption ( if (OptionPtr == NULL) { continue; } + + // + // Validate the variable. + // + if (!ValidateOption(OptionPtr, OptionSize)) { + continue; + } + TempPtr = OptionPtr; TempPtr += sizeof (UINT32) + sizeof (UINT16); Description = (CHAR16 *) TempPtr; @@ -426,6 +398,182 @@ BdsLibRegisterNewOption ( return Status; } +/** + Returns the size of a device path in bytes. + + This function returns the size, in bytes, of the device path data structure + specified by DevicePath including the end of device path node. If DevicePath + is NULL, then 0 is returned. If the length of the device path is bigger than + MaxSize, also return 0 to indicate this is an invalidate device path. + + @param DevicePath A pointer to a device path data structure. + @param MaxSize Max valid device path size. If big than this size, + return error. + + @retval 0 An invalid device path. + @retval Others The size of a device path in bytes. + +**/ +UINTN +GetDevicePathSizeEx ( + IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath, + IN UINTN MaxSize + ) +{ + UINTN Size; + UINTN NodeSize; + + if (DevicePath == NULL) { + return 0; + } + + // + // Search for the end of the device path structure + // + Size = 0; + while (!IsDevicePathEnd (DevicePath)) { + NodeSize = DevicePathNodeLength (DevicePath); + if (NodeSize < END_DEVICE_PATH_LENGTH) { + return 0; + } + Size += NodeSize; + if (Size > MaxSize) { + return 0; + } + DevicePath = NextDevicePathNode (DevicePath); + } + Size += DevicePathNodeLength (DevicePath); + if (Size > MaxSize) { + return 0; + } + + return Size; +} + +/** + Returns the length of a Null-terminated Unicode string. If the length is + bigger than MaxStringLen, return length 0 to indicate that this is an + invalidate string. + + This function returns the byte length of Unicode characters in the Null-terminated + Unicode string specified by String. + + If String is NULL, then ASSERT(). + If String is not aligned on a 16-bit boundary, then ASSERT(). + + @param String A pointer to a Null-terminated Unicode string. + @param MaxStringLen Max string len in this string. + + @retval 0 An invalid string. + @retval Others The length of String. + +**/ +UINTN +StrSizeEx ( + IN CONST CHAR16 *String, + IN UINTN MaxStringLen + ) +{ + UINTN Length; + + ASSERT (String != NULL && MaxStringLen != 0); + ASSERT (((UINTN) String & BIT0) == 0); + + for (Length = 0; *String != L'\0' && MaxStringLen != Length; String++, Length+=2); + + if (*String != L'\0' && MaxStringLen == Length) { + return 0; + } + + return Length + 2; +} + +/** + Validate the EFI Boot#### variable (VendorGuid/Name) + + @param Variable Boot#### variable data. + @param VariableSize Returns the size of the EFI variable that was read + + @retval TRUE The variable data is correct. + @retval FALSE The variable data is corrupted. + +**/ +BOOLEAN +ValidateOption ( + UINT8 *Variable, + UINTN VariableSize + ) +{ + UINT16 FilePathSize; + UINT8 *TempPtr; + EFI_DEVICE_PATH_PROTOCOL *DevicePath; + UINTN TempSize; + + if (VariableSize <= sizeof (UINT16) + sizeof (UINT32)) { + return FALSE; + } + + // + // Skip the option attribute + // + TempPtr = Variable; + TempPtr += sizeof (UINT32); + + // + // Get the option's device path size + // + FilePathSize = *(UINT16 *) TempPtr; + TempPtr += sizeof (UINT16); + + // + // Get the option's description string size + // + TempSize = StrSizeEx ((CHAR16 *) TempPtr, VariableSize - sizeof (UINT16) - sizeof (UINT32)); + TempPtr += TempSize; + + // + // Get the option's device path + // + DevicePath = (EFI_DEVICE_PATH_PROTOCOL *) TempPtr; + TempPtr += FilePathSize; + + // + // Validation boot option variable. + // + if ((FilePathSize == 0) || (TempSize == 0)) { + return FALSE; + } + + if (TempSize + FilePathSize + sizeof (UINT16) + sizeof (UINT32) > VariableSize) { + return FALSE; + } + + return (BOOLEAN) (GetDevicePathSizeEx (DevicePath, FilePathSize) != 0); +} + +/** + Convert a single character to number. + It assumes the input Char is in the scope of L'0' ~ L'9' and L'A' ~ L'F' + + @param Char The input char which need to change to a hex number. + +**/ +UINTN +CharToUint ( + IN CHAR16 Char + ) +{ + if ((Char >= L'0') && (Char <= L'9')) { + return (UINTN) (Char - L'0'); + } + + if ((Char >= L'A') && (Char <= L'F')) { + return (UINTN) (Char - L'A' + 0xA); + } + + ASSERT (FALSE); + return 0; +} /** Build the boot#### or driver#### option from the VariableName, the @@ -458,6 +606,7 @@ BdsLibVariableToOption ( UINT32 LoadOptionsSize; CHAR16 *Description; UINT8 NumOff; + // // Read the variable. We will never free this data. // @@ -469,6 +618,14 @@ BdsLibVariableToOption ( if (Variable == NULL) { return NULL; } + + // + // Validate Boot#### variable data. + // + if (!ValidateOption(Variable, VariableSize)) { + return NULL; + } + // // Notes: careful defined the variable of Boot#### or // Driver####, consider use some macro to abstract the code @@ -494,7 +651,7 @@ BdsLibVariableToOption ( // // Get the option's description string size // - TempPtr += StrSize ((CHAR16 *) TempPtr); + TempPtr += StrSize((CHAR16 *) TempPtr); // // Get the option's device path @@ -502,6 +659,9 @@ BdsLibVariableToOption ( DevicePath = (EFI_DEVICE_PATH_PROTOCOL *) TempPtr; TempPtr += FilePathSize; + // + // Get load opion data. + // LoadOptions = TempPtr; LoadOptionsSize = (UINT32) (VariableSize - (UINTN) (TempPtr - Variable)); @@ -535,25 +695,15 @@ BdsLibVariableToOption ( // Unicode stream to ASCII without any loss in meaning. // if (*VariableName == 'B') { - NumOff = sizeof (L"Boot")/sizeof(CHAR16) -1 ; - Option->BootCurrent = (UINT16) ((VariableName[NumOff] -'0') * 0x1000); - Option->BootCurrent = (UINT16) (Option->BootCurrent + ((VariableName[NumOff+1]-'0') * 0x100)); - Option->BootCurrent = (UINT16) (Option->BootCurrent + ((VariableName[NumOff+2]-'0') * 0x10)); - Option->BootCurrent = (UINT16) (Option->BootCurrent + ((VariableName[NumOff+3]-'0'))); - } - // - // Insert active entry to BdsDeviceList - // - if ((Option->Attribute & LOAD_OPTION_ACTIVE) == LOAD_OPTION_ACTIVE) { - InsertTailList (BdsCommonOptionList, &Option->Link); - FreePool (Variable); - return Option; + NumOff = (UINT8) (sizeof (L"Boot") / sizeof (CHAR16) - 1); + Option->BootCurrent = (UINT16) (CharToUint (VariableName[NumOff+0]) * 0x1000) + + (UINT16) (CharToUint (VariableName[NumOff+1]) * 0x100) + + (UINT16) (CharToUint (VariableName[NumOff+2]) * 0x10) + + (UINT16) (CharToUint (VariableName[NumOff+3]) * 0x1); } - + InsertTailList (BdsCommonOptionList, &Option->Link); FreePool (Variable); - FreePool (Option); - return NULL; - + return Option; } /** @@ -608,9 +758,9 @@ BdsLibBuildOptionFromVar ( } Option = BdsLibVariableToOption (BdsCommonOptionList, OptionName); - ASSERT (Option != NULL); - Option->BootCurrent = OptionOrder[Index]; - + if (Option != NULL) { + Option->BootCurrent = OptionOrder[Index]; + } } FreePool (OptionOrder); @@ -675,6 +825,7 @@ BdsLibGetVariableAndSize ( // Buffer = AllocateZeroPool (BufferSize); if (Buffer == NULL) { + *VariableSize = 0; return NULL; } // @@ -682,10 +833,15 @@ BdsLibGetVariableAndSize ( // Status = gRT->GetVariable (Name, VendorGuid, NULL, &BufferSize, Buffer); if (EFI_ERROR (Status)) { + FreePool (Buffer); BufferSize = 0; + Buffer = NULL; } } + ASSERT (((Buffer == NULL) && (BufferSize == 0)) || + ((Buffer != NULL) && (BufferSize != 0)) + ); *VariableSize = BufferSize; return Buffer; } @@ -831,7 +987,7 @@ BdsLibOutputStrings ( // If String is NULL, then it's the end of the list // String = VA_ARG (Args, CHAR16 *); - if (String != NULL) { + if (String == NULL) { break; } @@ -847,7 +1003,7 @@ BdsLibOutputStrings ( } // -// Following are BDS Lib functions which contain all the code about setup browser reset reminder feature. +// Following are BDS Lib functions which contain all the code about setup browser reset reminder feature. // Setup Browser reset reminder feature is that an reset reminder will be given before user leaves the setup browser if // user change any option setting which needs a reset to be effective, and the reset will be applied according to the user selection. // @@ -968,24 +1124,19 @@ SetupResetReminder ( ASSERT (StringBuffer1 != NULL); StringBuffer2 = AllocateZeroPool (MAX_STRING_LEN * sizeof (CHAR16)); ASSERT (StringBuffer2 != NULL); - StrCpy (StringBuffer1, L"Configuration changed. Reset to apply it Now ? "); - StrCpy (StringBuffer2, L"Enter (YES) / Esc (NO)"); + StrCpy (StringBuffer1, L"Configuration changed. Reset to apply it Now."); + StrCpy (StringBuffer2, L"Press ENTER to reset"); // // Popup a menu to notice user // do { - IfrLibCreatePopUp (2, &Key, StringBuffer1, StringBuffer2); - } while ((Key.ScanCode != SCAN_ESC) && (Key.UnicodeChar != CHAR_CARRIAGE_RETURN)); + CreatePopUp (EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, &Key, StringBuffer1, StringBuffer2, NULL); + } while (Key.UnicodeChar != CHAR_CARRIAGE_RETURN); FreePool (StringBuffer1); FreePool (StringBuffer2); - // - // If the user hits the YES Response key, reset - // - if ((Key.UnicodeChar == CHAR_CARRIAGE_RETURN)) { - gRT->ResetSystem (EfiResetCold, EFI_SUCCESS, 0, NULL); - } - gST->ConOut->ClearScreen (gST->ConOut); + + gRT->ResetSystem (EfiResetCold, EFI_SUCCESS, 0, NULL); } } } @@ -1042,11 +1193,12 @@ BdsLibGetImageHeader ( Root = NULL; goto Done; } - + ASSERT (Root != NULL); Status = Root->Open (Root, &ThisFile, FileName, EFI_FILE_MODE_READ, 0); if (EFI_ERROR (Status)) { goto Done; } + ASSERT (ThisFile != NULL); // // Get file size @@ -1131,36 +1283,58 @@ BdsLibGetImageHeader ( } /** - - This routine is a notification function for legayc boot or exit boot - service event. It will adjust the memory information for different - memory type and save them into the variables for next boot. - - - @param Event The event that triggered this notification function. - @param Context Pointer to the notification functions context. - + This routine adjust the memory information for different memory type and + save them into the variables for next boot. **/ VOID -EFIAPI BdsSetMemoryTypeInformationVariable ( - EFI_EVENT Event, - VOID *Context + VOID ) { EFI_STATUS Status; EFI_MEMORY_TYPE_INFORMATION *PreviousMemoryTypeInformation; EFI_MEMORY_TYPE_INFORMATION *CurrentMemoryTypeInformation; UINTN VariableSize; - BOOLEAN UpdateRequired; UINTN Index; UINTN Index1; UINT32 Previous; UINT32 Current; UINT32 Next; EFI_HOB_GUID_TYPE *GuidHob; + BOOLEAN MemoryTypeInformationModified; + BOOLEAN MemoryTypeInformationVariableExists; + EFI_BOOT_MODE BootMode; + + MemoryTypeInformationModified = FALSE; + MemoryTypeInformationVariableExists = FALSE; + - UpdateRequired = FALSE; + BootMode = GetBootModeHob (); + // + // In BOOT_IN_RECOVERY_MODE, Variable region is not reliable. + // + if (BootMode == BOOT_IN_RECOVERY_MODE) { + return; + } + + // + // Only check the the Memory Type Information variable in the boot mode + // other than BOOT_WITH_DEFAULT_SETTINGS because the Memory Type + // Information is not valid in this boot mode. + // + if (BootMode != BOOT_WITH_DEFAULT_SETTINGS) { + VariableSize = 0; + Status = gRT->GetVariable ( + EFI_MEMORY_TYPE_INFORMATION_VARIABLE_NAME, + &gEfiMemoryTypeInformationGuid, + NULL, + &VariableSize, + NULL + ); + if (Status == EFI_BUFFER_TOO_SMALL) { + MemoryTypeInformationVariableExists = TRUE; + } + } // // Retrieve the current memory usage statistics. If they are not found, then @@ -1192,29 +1366,41 @@ BdsSetMemoryTypeInformationVariable ( // // Use a heuristic to adjust the Memory Type Information for the next boot // + DEBUG ((EFI_D_INFO, "Memory Previous Current Next \n")); + DEBUG ((EFI_D_INFO, " Type Pages Pages Pages \n")); + DEBUG ((EFI_D_INFO, "====== ======== ======== ========\n")); + for (Index = 0; PreviousMemoryTypeInformation[Index].Type != EfiMaxMemoryType; Index++) { - Current = 0; for (Index1 = 0; CurrentMemoryTypeInformation[Index1].Type != EfiMaxMemoryType; Index1++) { if (PreviousMemoryTypeInformation[Index].Type == CurrentMemoryTypeInformation[Index1].Type) { - Current = CurrentMemoryTypeInformation[Index1].NumberOfPages; break; } } - if (CurrentMemoryTypeInformation[Index1].Type == EfiMaxMemoryType) { continue; } + // + // Previous is the number of pages pre-allocated + // Current is the number of pages actually needed + // Previous = PreviousMemoryTypeInformation[Index].NumberOfPages; + Current = CurrentMemoryTypeInformation[Index1].NumberOfPages; + Next = Previous; // - // Write next varible to 125% * current and Inconsistent Memory Reserved across bootings may lead to S4 fail + // Inconsistent Memory Reserved across bootings may lead to S4 fail + // Write next varible to 125% * current when the pre-allocated memory is: + // 1. More than 150% of needed memory and boot mode is BOOT_WITH_DEFAULT_SETTING + // 2. Less than the needed memory // - if (Current > Previous) { + if ((Current + (Current >> 1)) < Previous) { + if (BootMode == BOOT_WITH_DEFAULT_SETTINGS) { + Next = Current + (Current >> 2); + } + } else if (Current > Previous) { Next = Current + (Current >> 2); - } else { - Next = Previous; } if (Next > 0 && Next < 4) { Next = 4; @@ -1222,31 +1408,39 @@ BdsSetMemoryTypeInformationVariable ( if (Next != Previous) { PreviousMemoryTypeInformation[Index].NumberOfPages = Next; - UpdateRequired = TRUE; + MemoryTypeInformationModified = TRUE; } + DEBUG ((EFI_D_INFO, " %02x %08x %08x %08x\n", PreviousMemoryTypeInformation[Index].Type, Previous, Current, Next)); } // - // If any changes were made to the Memory Type Information settings, then set the new variable value + // If any changes were made to the Memory Type Information settings, then set the new variable value; + // Or create the variable in first boot. // - if (UpdateRequired) { + if (MemoryTypeInformationModified || !MemoryTypeInformationVariableExists) { Status = gRT->SetVariable ( - EFI_MEMORY_TYPE_INFORMATION_VARIABLE_NAME, - &gEfiMemoryTypeInformationGuid, - EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS, - VariableSize, - PreviousMemoryTypeInformation - ); - } + EFI_MEMORY_TYPE_INFORMATION_VARIABLE_NAME, + &gEfiMemoryTypeInformationGuid, + EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS, + VariableSize, + PreviousMemoryTypeInformation + ); - return; + // + // If the Memory Type Information settings have been modified, then reset the platform + // so the new Memory Type Information setting will be used to guarantee that an S4 + // entry/resume cycle will not fail. + // + if (MemoryTypeInformationModified && PcdGetBool (PcdResetOnMemoryTypeInformationChange)) { + DEBUG ((EFI_D_INFO, "Memory Type Information settings change. Warm Reset!!!\n")); + gRT->ResetSystem (EfiResetWarm, EFI_SUCCESS, 0, NULL); + } + } } /** - This routine register a function to adjust the different type memory page number - just before booting and save the updated info into the variable for next boot to use. - + This routine is kept for backward compatibility. **/ VOID EFIAPI @@ -1254,19 +1448,37 @@ BdsLibSaveMemoryTypeInformation ( VOID ) { - EFI_STATUS Status; - EFI_EVENT ReadyToBootEvent; - - Status = EfiCreateEventReadyToBootEx ( - TPL_CALLBACK, - BdsSetMemoryTypeInformationVariable, - NULL, - &ReadyToBootEvent - ); +} + + +/** + Identify a user and, if authenticated, returns the current user profile handle. + + @param[out] User Point to user profile handle. + + @retval EFI_SUCCESS User is successfully identified, or user identification + is not supported. + @retval EFI_ACCESS_DENIED User is not successfully identified + +**/ +EFI_STATUS +EFIAPI +BdsLibUserIdentify ( + OUT EFI_USER_PROFILE_HANDLE *User + ) +{ + EFI_STATUS Status; + EFI_USER_MANAGER_PROTOCOL *Manager; + + Status = gBS->LocateProtocol ( + &gEfiUserManagerProtocolGuid, + NULL, + (VOID **) &Manager + ); if (EFI_ERROR (Status)) { - DEBUG ((DEBUG_ERROR,"Bds Set Memory Type Informationa Variable Fails\n")); + return EFI_SUCCESS; } + return Manager->Identify (Manager, User); } -