From 7ea22852094257713c3d7f31f1c02b0433c2729f Mon Sep 17 00:00:00 2001 From: ydong10 Date: Fri, 18 May 2012 02:13:29 +0000 Subject: [PATCH] Add logic to validate console variables when enter BDS. Signed-off-by: Eric Dong Reviewed-by: Ruiyu Ni git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@13322 6f19259b-4bc3-4df7-8a09-765794883524 --- .../Universal/BdsDxe/BdsEntry.c | 111 ++++++++++++++++++ 1 file changed, 111 insertions(+) diff --git a/IntelFrameworkModulePkg/Universal/BdsDxe/BdsEntry.c b/IntelFrameworkModulePkg/Universal/BdsDxe/BdsEntry.c index 6561efe5a7..e0862aed5e 100644 --- a/IntelFrameworkModulePkg/Universal/BdsDxe/BdsEntry.c +++ b/IntelFrameworkModulePkg/Universal/BdsDxe/BdsEntry.c @@ -274,6 +274,112 @@ BdsBootDeviceSelect ( } +/** + Validate the device path instance. + + Only base on the length filed in the device path node to validate the 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 TRUE An valid device path. + @retval FALSE An invalid device path. + +**/ +BOOLEAN +IsValidDevicePath ( + IN EFI_DEVICE_PATH_PROTOCOL *DevicePath, + IN UINTN MaxSize + ) +{ + UINTN Size; + UINTN NodeSize; + + if (DevicePath == NULL) { + return TRUE; + } + + Size = 0; + + while (!IsDevicePathEnd (DevicePath)) { + NodeSize = DevicePathNodeLength (DevicePath); + if (NodeSize < END_DEVICE_PATH_LENGTH) { + return FALSE; + } + + Size += NodeSize; + if (Size > MaxSize) { + return FALSE; + } + + DevicePath = NextDevicePathNode (DevicePath); + } + + Size += DevicePathNodeLength (DevicePath); + if (Size > MaxSize) { + return FALSE; + } + + return TRUE; +} + +/** + + Validate input console variable data. + + If found the device path is not a valid device path, remove the variable. + + @param VariableName Input console variable name. + +**/ + +VOID +BdsFormalizeConsoleVariable ( + IN CHAR16 *VariableName + ) +{ + EFI_DEVICE_PATH_PROTOCOL *DevicePath; + UINTN VariableSize; + EFI_STATUS Status; + + DevicePath = BdsLibGetVariableAndSize ( + VariableName, + &gEfiGlobalVariableGuid, + &VariableSize + ); + if (!IsValidDevicePath (DevicePath, VariableSize)) { + Status = gRT->SetVariable ( + VariableName, + &gEfiGlobalVariableGuid, + EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE, + 0, + NULL + ); + ASSERT_EFI_ERROR (Status); + } +} + +/** + + Validate variables. + + If found the device path is not a valid device path, remove the variable. + +**/ +VOID +BdsFormalizeEfiGlobalVariable ( + VOID + ) +{ + // + // Validate Console variable. + // + BdsFormalizeConsoleVariable (L"ConIn"); + BdsFormalizeConsoleVariable (L"ConOut"); + BdsFormalizeConsoleVariable (L"ErrOut"); +} + /** Service routine for BdsInstance->Entry(). Devices are connected, the @@ -323,6 +429,11 @@ BdsEntry ( // gBS->CalculateCrc32 ((VOID *)gST, sizeof(EFI_SYSTEM_TABLE), &gST->Hdr.CRC32); + // + // Validate Variable. + // + BdsFormalizeEfiGlobalVariable(); + // // Do the platform init, can be customized by OEM/IBV //