X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=ShellPkg%2FLibrary%2FUefiShellDebug1CommandsLib%2FSetVar.c;h=8fb918d0822d811616eb1333c61640360090d833;hp=d98a3469a31ddf2d951c403d99ef8c110f1baeb6;hb=6877b9bd0428de299b3cdd790843d00843305e2c;hpb=c4e74e9b814cfb4b51cf832f3bb218cd2aba348b diff --git a/ShellPkg/Library/UefiShellDebug1CommandsLib/SetVar.c b/ShellPkg/Library/UefiShellDebug1CommandsLib/SetVar.c index d98a3469a3..8fb918d082 100644 --- a/ShellPkg/Library/UefiShellDebug1CommandsLib/SetVar.c +++ b/ShellPkg/Library/UefiShellDebug1CommandsLib/SetVar.c @@ -2,7 +2,7 @@ Main file for SetVar shell Debug1 function. (C) Copyright 2015 Hewlett-Packard Development Company, L.P.
- Copyright (c) 2010 - 2014, Intel Corporation. All rights reserved.
+ Copyright (c) 2010 - 2017, 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 @@ -23,6 +23,21 @@ STATIC CONST SHELL_PARAM_ITEM ParamList[] = { {NULL, TypeMax} }; +typedef enum { + DataTypeHexNumber = 0, + DataTypeHexArray = 1, + DataTypeAscii = 2, + DataTypeUnicode = 3, + DataTypeDevicePath = 4, + DataTypeUnKnow = 5 +} DATA_TYPE; + +typedef union { + UINT8 HexNumber8; + UINT16 HexNumber16; + UINT32 HexNumber32; + UINT64 HexNumber64; +} HEX_NUMBER; /** Check if the input is a (potentially empty) string of hexadecimal nibbles. @@ -50,6 +65,271 @@ IsStringOfHexNibbles ( return TRUE; } +/** + Function to check the TYPE of Data. + + @param[in] Data The Data to be check. + + @retval DATA_TYPE The TYPE of Data. +**/ +DATA_TYPE +TestDataType ( + IN CONST CHAR16 *Data + ) +{ + if (Data[0] == L'0' && (Data[1] == L'x' || Data[1] == L'X')) { + if (IsStringOfHexNibbles (Data+2) && StrLen (Data + 2) <= 16) { + return DataTypeHexNumber; + } else { + return DataTypeUnKnow; + } + } else if (Data[0] == L'H') { + if (IsStringOfHexNibbles (Data + 1) && StrLen (Data + 1) % 2 == 0) { + return DataTypeHexArray; + } else { + return DataTypeUnKnow; + } + } else if (Data[0] == L'S') { + return DataTypeAscii; + } else if (Data[0] == L'L') { + return DataTypeUnicode; + } else if (Data[0] == L'P' || StrnCmp (Data, L"--", 2) == 0) { + return DataTypeDevicePath; + } + + if (IsStringOfHexNibbles (Data) && StrLen (Data) % 2 == 0) { + return DataTypeHexArray; + } + + return DataTypeAscii; +} + +/** + Function to parse the Data by the type of Data, and save in the Buffer. + + @param[in] Data A pointer to a buffer to be parsed. + @param[out] Buffer A pointer to a buffer to hold the return data. + @param[in,out] BufferSize On input, indicates the size of Buffer in bytes. + On output,indicates the size of data return in Buffer. + Or the size in bytes of the buffer needed to obtain. + + @retval EFI_INVALID_PARAMETER The Buffer or BufferSize is NULL. + @retval EFI_BUFFER_TOO_SMALL The Buffer is too small to hold the data. + @retval EFI_OUT_OF_RESOURCES A memory allcation failed. + @retval EFI_SUCCESS The Data parsed successful and save in the Buffer. +**/ +EFI_STATUS +ParseParameterData ( + IN CONST CHAR16 *Data, + OUT VOID *Buffer, + IN OUT UINTN *BufferSize + ) +{ + UINT64 HexNumber; + UINTN HexNumberLen; + UINTN Size; + CHAR8 *AsciiBuffer; + DATA_TYPE DataType; + EFI_DEVICE_PATH_PROTOCOL *DevPath; + EFI_STATUS Status; + + HexNumber = 0; + HexNumberLen = 0; + Size = 0; + AsciiBuffer = NULL; + DevPath = NULL; + Status = EFI_SUCCESS; + + if (Data == NULL || BufferSize == NULL) { + return EFI_INVALID_PARAMETER; + } + + DataType = TestDataType (Data); + if (DataType == DataTypeHexNumber) { + // + // hex number + // + StrHexToUint64S (Data + 2, NULL, &HexNumber); + HexNumberLen = StrLen (Data + 2); + if (HexNumberLen >= 1 && HexNumberLen <= 2) { + Size = 1; + } else if (HexNumberLen >= 3 && HexNumberLen <= 4) { + Size = 2; + } else if (HexNumberLen >= 5 && HexNumberLen <= 8) { + Size = 4; + } else if (HexNumberLen >= 9 && HexNumberLen <= 16) { + Size = 8; + } + if (Buffer != NULL && *BufferSize >= Size) { + CopyMem(Buffer, (VOID *)&HexNumber, Size); + } else { + Status = EFI_BUFFER_TOO_SMALL; + } + *BufferSize = Size; + } else if (DataType == DataTypeHexArray) { + // + // hex array + // + if (*Data == L'H') { + Data = Data + 1; + } + + Size = StrLen (Data) / 2; + if (Buffer != NULL && *BufferSize >= Size) { + StrHexToBytes(Data, StrLen (Data), (UINT8 *)Buffer, Size); + } else { + Status = EFI_BUFFER_TOO_SMALL; + } + *BufferSize = Size; + } else if (DataType == DataTypeAscii) { + // + // ascii text + // + if (*Data == L'S') { + Data = Data + 1; + } + AsciiBuffer = AllocateZeroPool (StrSize (Data) / 2); + if (AsciiBuffer == NULL) { + Status = EFI_OUT_OF_RESOURCES; + } else { + AsciiSPrint (AsciiBuffer, StrSize (Data) / 2, "%s", (CHAR8 *)Data); + + Size = StrSize (Data) / 2 - 1; + if (Buffer != NULL && *BufferSize >= Size) { + CopyMem (Buffer, AsciiBuffer, Size); + } else { + Status = EFI_BUFFER_TOO_SMALL; + } + *BufferSize = Size; + } + SHELL_FREE_NON_NULL (AsciiBuffer); + } else if (DataType == DataTypeUnicode) { + // + // unicode text + // + if (*Data == L'L') { + Data = Data + 1; + } + Size = StrSize (Data) - sizeof (CHAR16); + if (Buffer != NULL && *BufferSize >= Size) { + CopyMem (Buffer, Data, Size); + } else { + Status = EFI_BUFFER_TOO_SMALL; + } + *BufferSize = Size; + } else if (DataType == DataTypeDevicePath) { + if (*Data == L'P') { + Data = Data + 1; + } else if (StrnCmp (Data, L"--", 2) == 0) { + Data = Data + 2; + } + DevPath = ConvertTextToDevicePath (Data); + if (DevPath == NULL) { + ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_SETVAR_ERROR_DPFT), gShellDebug1HiiHandle, L"setvar"); + Status = EFI_INVALID_PARAMETER; + } else { + Size = GetDevicePathSize (DevPath); + if (Buffer != NULL && *BufferSize >= Size) { + CopyMem (Buffer, DevPath, Size); + } else { + Status = EFI_BUFFER_TOO_SMALL; + } + *BufferSize = Size; + } + SHELL_FREE_NON_NULL (DevPath); + } else { + Status = EFI_INVALID_PARAMETER; + } + + return Status; +} + +/** + Function to get each data from parameters. + + @param[in] Package The package of checked values. + @param[out] Buffer A pointer to a buffer to hold the return data. + @param[out] BufferSize Indicates the size of data in bytes return in Buffer. + + @retval EFI_INVALID_PARAMETER Buffer or BufferSize is NULL. + @retval EFI_OUT_OF_RESOURCES A memory allcation failed. + @retval EFI_SUCCESS Get each parameter data was successful. +**/ +EFI_STATUS +GetVariableDataFromParameter ( + IN CONST LIST_ENTRY *Package, + OUT UINT8 **Buffer, + OUT UINTN *BufferSize + ) +{ + CONST CHAR16 *TempData; + UINTN Index; + UINTN TotalSize; + UINTN Size; + UINT8 *BufferWalker; + EFI_STATUS Status; + + TotalSize = 0; + Size = 0; + Status = EFI_SUCCESS; + + if (BufferSize == NULL || Buffer == NULL || ShellCommandLineGetCount (Package) < 3) { + return EFI_INVALID_PARAMETER; + } + + for (Index = 2; Index < ShellCommandLineGetCount (Package); Index++) { + TempData = ShellCommandLineGetRawValue (Package, Index); + ASSERT (TempData != NULL); + + if (TempData[0] != L'=') { + ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV), gShellDebug1HiiHandle, L"setvar", TempData); + return EFI_INVALID_PARAMETER; + } + + TempData = TempData + 1; + Size = 0; + Status = ParseParameterData (TempData, NULL, &Size); + if (EFI_ERROR (Status)) { + if (Status == EFI_BUFFER_TOO_SMALL) { + // + // We expect return EFI_BUFFER_TOO_SMALL when pass 'NULL' as second parameter to the function ParseParameterData. + // + TotalSize += Size; + } else { + if (Status == EFI_INVALID_PARAMETER) { + ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV), gShellDebug1HiiHandle, L"setvar", TempData); + } else if (Status == EFI_NOT_FOUND) { + ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_SETVAR_ERROR_DPFT), gShellDebug1HiiHandle, L"setvar"); + } + return Status; + } + } + } + + *BufferSize = TotalSize; + *Buffer = AllocateZeroPool (TotalSize); + + if (*Buffer == NULL) { + Status = EFI_OUT_OF_RESOURCES; + } else { + BufferWalker = *Buffer; + for (Index = 2; Index < ShellCommandLineGetCount (Package); Index++) { + TempData = ShellCommandLineGetRawValue (Package, Index); + TempData = TempData + 1; + + Size = TotalSize; + Status = ParseParameterData (TempData, (VOID *)BufferWalker, &Size); + if (!EFI_ERROR (Status)) { + BufferWalker = BufferWalker + Size; + TotalSize = TotalSize - Size; + } else { + return Status; + } + } + } + + return EFI_SUCCESS; +} /** Function for 'setvar' command. @@ -65,25 +345,23 @@ ShellCommandRunSetVar ( ) { EFI_STATUS Status; + RETURN_STATUS RStatus; LIST_ENTRY *Package; CHAR16 *ProblemParam; SHELL_STATUS ShellStatus; CONST CHAR16 *VariableName; - CONST CHAR16 *Data; EFI_GUID Guid; CONST CHAR16 *StringGuid; UINT32 Attributes; VOID *Buffer; UINTN Size; UINTN LoopVar; - EFI_DEVICE_PATH_PROTOCOL *DevPath; ShellStatus = SHELL_SUCCESS; Status = EFI_SUCCESS; Buffer = NULL; Size = 0; Attributes = 0; - DevPath = NULL; // // initialize the shell lib (we must be in non-auto-init...) @@ -110,70 +388,47 @@ ShellCommandRunSetVar ( if (ShellCommandLineGetCount(Package) < 2) { ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellDebug1HiiHandle, L"setvar"); ShellStatus = SHELL_INVALID_PARAMETER; - } else if (ShellCommandLineGetCount(Package) > 3) { - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellDebug1HiiHandle, L"setvar"); - ShellStatus = SHELL_INVALID_PARAMETER; } else { VariableName = ShellCommandLineGetRawValue(Package, 1); - Data = ShellCommandLineGetRawValue(Package, 2); if (!ShellCommandLineGetFlag(Package, L"-guid")){ CopyGuid(&Guid, &gEfiGlobalVariableGuid); } else { StringGuid = ShellCommandLineGetValue(Package, L"-guid"); - Status = ConvertStringToGuid(StringGuid, &Guid); - if (EFI_ERROR(Status)) { + RStatus = StrToGuid (StringGuid, &Guid); + if (RETURN_ERROR (RStatus) || (StringGuid[GUID_STRING_LENGTH] != L'\0')) { ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV), gShellDebug1HiiHandle, L"setvar", StringGuid); ShellStatus = SHELL_INVALID_PARAMETER; } } - if (Data == NULL || Data[0] != L'=') { + + if (ShellCommandLineGetCount(Package) == 2) { // - // Display what's there + // Display // Status = gRT->GetVariable((CHAR16*)VariableName, &Guid, &Attributes, &Size, Buffer); if (Status == EFI_BUFFER_TOO_SMALL) { Buffer = AllocateZeroPool(Size); Status = gRT->GetVariable((CHAR16*)VariableName, &Guid, &Attributes, &Size, Buffer); } - if (!EFI_ERROR(Status)&& Buffer != NULL) { - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SETVAR_PRINT), gShellDebug1HiiHandle, &Guid, VariableName, Size); - for (LoopVar = 0 ; LoopVar < Size ; LoopVar++) { + if (!EFI_ERROR(Status) && Buffer != NULL) { + ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN(STR_SETVAR_PRINT), gShellDebug1HiiHandle, &Guid, VariableName, Size); + for (LoopVar = 0; LoopVar < Size; LoopVar++) { ShellPrintEx(-1, -1, L"%02x ", ((UINT8*)Buffer)[LoopVar]); } ShellPrintEx(-1, -1, L"\r\n"); } else { - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SETVAR_ERROR_GET), gShellDebug1HiiHandle, L"setvar", &Guid, VariableName); + ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SETVAR_ERROR_GET), gShellDebug1HiiHandle, L"setvar", &Guid, VariableName); ShellStatus = SHELL_ACCESS_DENIED; } - } else if (StrCmp(Data, L"=") == 0) { - // - // Delete what's there! - // - Status = gRT->SetVariable((CHAR16*)VariableName, &Guid, Attributes, 0, NULL); - if (EFI_ERROR(Status)) { - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SETVAR_ERROR_SET), gShellDebug1HiiHandle, L"setvar", &Guid, VariableName); - ShellStatus = SHELL_ACCESS_DENIED; - } else { - ASSERT(ShellStatus == SHELL_SUCCESS); - } } else { // - // Change what's there or create a new one. - // - - ASSERT(Data[0] == L'='); - Data++; - ASSERT(Data[0] != L'\0'); - - // - // Determine if the variable exists and get the attributes + // Create, Delete or Modify. // Status = gRT->GetVariable((CHAR16*)VariableName, &Guid, &Attributes, &Size, Buffer); if (Status == EFI_BUFFER_TOO_SMALL) { Buffer = AllocateZeroPool(Size); Status = gRT->GetVariable((CHAR16*)VariableName, &Guid, &Attributes, &Size, Buffer); } - if (EFI_ERROR(Status) || Buffer == NULL) { // // Creating a new variable. determine attributes from command line. @@ -192,97 +447,16 @@ ShellCommandRunSetVar ( } SHELL_FREE_NON_NULL(Buffer); - // - // What type is the new data. - // - if (IsStringOfHexNibbles(Data)) { - if (StrLen(Data) % 2 != 0) { - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV), gShellDebug1HiiHandle, L"setvar", Data); - ShellStatus = SHELL_INVALID_PARAMETER; - } else { - // - // arbitrary buffer - // - Buffer = AllocateZeroPool((StrLen(Data) / 2)); - if (Buffer == NULL) { - Status = EFI_OUT_OF_RESOURCES; - } else { - for (LoopVar = 0 ; LoopVar < (StrLen(Data) / 2) ; LoopVar++) { - ((UINT8*)Buffer)[LoopVar] = (UINT8)(HexCharToUintn(Data[LoopVar*2]) * 16); - ((UINT8*)Buffer)[LoopVar] = (UINT8)(((UINT8*)Buffer)[LoopVar] + HexCharToUintn(Data[LoopVar*2+1])); - } - Status = gRT->SetVariable((CHAR16*)VariableName, &Guid, Attributes, StrLen(Data) / 2, Buffer); - } - if (EFI_ERROR(Status)) { - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SETVAR_ERROR_SET), gShellDebug1HiiHandle, L"setvar", &Guid, VariableName); - ShellStatus = SHELL_ACCESS_DENIED; - } else { - ASSERT(ShellStatus == SHELL_SUCCESS); - } - } - } else if (StrnCmp(Data, L"\"", 1) == 0) { - // - // ascii text - // - Data++; - Buffer = AllocateZeroPool(StrSize(Data) / 2); - if (Buffer == NULL) { - Status = EFI_OUT_OF_RESOURCES; - } else { - AsciiSPrint(Buffer, StrSize(Data) / 2, "%s", Data); - ((CHAR8*)Buffer)[AsciiStrLen(Buffer)-1] = CHAR_NULL; - Status = gRT->SetVariable((CHAR16*)VariableName, &Guid, Attributes, AsciiStrSize(Buffer)-sizeof(CHAR8), Buffer); - } - if (EFI_ERROR(Status)) { - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SETVAR_ERROR_SET), gShellDebug1HiiHandle, L"setvar", &Guid, VariableName); - ShellStatus = SHELL_ACCESS_DENIED; - } else { - ASSERT(ShellStatus == SHELL_SUCCESS); - } - } else if (StrnCmp(Data, L"L\"", 2) == 0) { - // - // ucs2 text - // - Data++; - Data++; - Buffer = AllocateZeroPool(StrSize(Data)); - if (Buffer == NULL) { - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_OUT_MEM), gShellDebug1HiiHandle, L"setvar"); - ShellStatus = SHELL_OUT_OF_RESOURCES; - } else { - UnicodeSPrint(Buffer, StrSize(Data), L"%s", Data); - ((CHAR16*)Buffer)[StrLen(Buffer)-1] = CHAR_NULL; - - Status = gRT->SetVariable((CHAR16*)VariableName, &Guid, Attributes, StrSize(Buffer)-sizeof(CHAR16), Buffer); - if (EFI_ERROR(Status)) { - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SETVAR_ERROR_SET), gShellDebug1HiiHandle, L"setvar", &Guid, VariableName); - ShellStatus = SHELL_ACCESS_DENIED; - } else { - ASSERT(ShellStatus == SHELL_SUCCESS); - } - } - } else if (StrnCmp(Data, L"--", 2) == 0) { - // - // device path in text format - // - Data++; - Data++; - DevPath = ConvertTextToDevicePath(Data); - if (DevPath == NULL) { - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SETVAR_ERROR_DPFT), gShellDebug1HiiHandle, L"setvar"); - ShellStatus = SHELL_INVALID_PARAMETER; - } else { - Status = gRT->SetVariable((CHAR16*)VariableName, &Guid, Attributes, GetDevicePathSize(DevPath), DevPath); - if (EFI_ERROR(Status)) { - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SETVAR_ERROR_SET), gShellDebug1HiiHandle, L"setvar", &Guid, VariableName); - ShellStatus = SHELL_ACCESS_DENIED; - } else { - ASSERT(ShellStatus == SHELL_SUCCESS); - } - } + Size = 0; + Status = GetVariableDataFromParameter(Package, (UINT8 **)&Buffer, &Size); + if (!EFI_ERROR(Status)) { + Status = gRT->SetVariable((CHAR16*)VariableName, &Guid, Attributes, Size, Buffer); + } + if (EFI_ERROR(Status)) { + ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN(STR_SETVAR_ERROR_SET), gShellDebug1HiiHandle, L"setvar", &Guid, VariableName); + ShellStatus = SHELL_ACCESS_DENIED; } else { - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV), gShellDebug1HiiHandle, L"setvar", Data); - ShellStatus = SHELL_INVALID_PARAMETER; + ASSERT(ShellStatus == SHELL_SUCCESS); } } } @@ -293,9 +467,5 @@ ShellCommandRunSetVar ( FreePool(Buffer); } - if (DevPath != NULL) { - FreePool(DevPath); - } - return (ShellStatus); }