X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=IntelFrameworkModulePkg%2FLibrary%2FGenericBdsLib%2FBdsMisc.c;h=9993e4b19d4bdfcfc242c45af99ecd43d2f2bfc2;hb=8c08a567c64814f36f7261ca5652ef0350ca660e;hp=351dfd59139b220b11ebf796058d2d21d1b22988;hpb=da166a5dbd9fbd560ddf21eedcbffe571281f7c5;p=mirror_edk2.git diff --git a/IntelFrameworkModulePkg/Library/GenericBdsLib/BdsMisc.c b/IntelFrameworkModulePkg/Library/GenericBdsLib/BdsMisc.c index 351dfd5913..9993e4b19d 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 - 2010, 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 @@ -79,7 +79,7 @@ BdsLibLoadDrivers ( // Status = gBS->LoadImage ( FALSE, - mBdsImageHandle, + gImageHandle, Option->DevicePath, NULL, 0, @@ -257,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; @@ -390,6 +398,189 @@ 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 number 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++); + + if (*String != L'\0' && MaxStringLen == Length) { + return 0; + } + + return (Length + 1) * sizeof (*String); +} + +/** + 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; + EFI_DEVICE_PATH_PROTOCOL *TempPath; + UINTN TempSize; + + // + // 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); + 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 (UINT16) > VariableSize) { + return FALSE; + } + + TempPath = DevicePath; + while (FilePathSize > 0) { + TempSize = GetDevicePathSizeEx (TempPath, FilePathSize); + if (TempSize == 0) { + return FALSE; + } + FilePathSize = (UINT16) (FilePathSize - TempSize); + TempPath += TempSize; + } + + return TRUE; +} + +/** + 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 @@ -417,11 +608,13 @@ BdsLibVariableToOption ( UINT8 *TempPtr; UINTN VariableSize; EFI_DEVICE_PATH_PROTOCOL *DevicePath; + EFI_DEVICE_PATH_PROTOCOL *TempPath; BDS_COMMON_OPTION *Option; VOID *LoadOptions; UINT32 LoadOptionsSize; CHAR16 *Description; UINT8 NumOff; + UINTN TempSize; // // Read the variable. We will never free this data. // @@ -433,6 +626,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 @@ -458,7 +659,11 @@ BdsLibVariableToOption ( // // Get the option's description string size // - TempPtr += StrSize ((CHAR16 *) TempPtr); + TempSize = StrSizeEx ((CHAR16 *) TempPtr, VariableSize); + if (TempSize == 0) { + return NULL; + } + TempPtr += TempSize; // // Get the option's device path @@ -466,7 +671,26 @@ BdsLibVariableToOption ( DevicePath = (EFI_DEVICE_PATH_PROTOCOL *) TempPtr; TempPtr += FilePathSize; + // + // Validation device path. + // + TempPath = DevicePath; + while (FilePathSize > 0) { + TempSize = GetDevicePathSizeEx (TempPath, FilePathSize); + if (TempSize == 0) { + return NULL; + } + FilePathSize = (UINT16) (FilePathSize - TempSize); + TempPath += TempSize; + } + + // + // Get load opion data. + // LoadOptions = TempPtr; + if (VariableSize < (UINTN)(TempPtr - Variable)) { + return NULL; + } LoadOptionsSize = (UINT32) (VariableSize - (UINTN) (TempPtr - Variable)); // @@ -499,11 +723,11 @@ 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'))); + 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); } // // Insert active entry to BdsDeviceList @@ -572,9 +796,9 @@ BdsLibBuildOptionFromVar ( } Option = BdsLibVariableToOption (BdsCommonOptionList, OptionName); - ASSERT (Option != NULL); - Option->BootCurrent = OptionOrder[Index]; - + if (Option != NULL) { + Option->BootCurrent = OptionOrder[Index]; + } } FreePool (OptionOrder); @@ -946,7 +1170,7 @@ SetupResetReminder ( // // If the user hits the YES Response key, reset // - if ((Key.UnicodeChar == CHAR_CARRIAGE_RETURN)) { + if (Key.UnicodeChar == CHAR_CARRIAGE_RETURN) { gRT->ResetSystem (EfiResetCold, EFI_SUCCESS, 0, NULL); } gST->ConOut->ClearScreen (gST->ConOut); @@ -1096,36 +1320,60 @@ 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 adjusts the memory information for different memory type and + saves them into the variables for next boot. It conditionally resets the + system when the memory information changes. Platform can reserve memory + large enough (125% of actual requirement) to avoid the reset in the first 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; - UpdateRequired = FALSE; + MemoryTypeInformationModified = FALSE; + MemoryTypeInformationVariableExists = 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 @@ -1157,29 +1405,40 @@ 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 // - if (Current > Previous) { + if (Current < Previous) { + if (BootMode == BOOT_WITH_DEFAULT_SETTINGS) { + Next = Current + (Current >> 2); + } else if (!MemoryTypeInformationVariableExists) { + Next = MAX (Current + (Current >> 2), Previous); + } + } else if (Current > Previous) { Next = Current + (Current >> 2); - } else { - Next = Previous; } if (Next > 0 && Next < 4) { Next = 4; @@ -1187,31 +1446,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 @@ -1219,19 +1486,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); } -