X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=ShellPkg%2FLibrary%2FUefiShellDebug1CommandsLib%2FSetVar.c;h=d98a3469a31ddf2d951c403d99ef8c110f1baeb6;hb=26ca6f7e1e2f3ba247d1d3150d6bfb22043a8cda;hp=f8c6dd2e30d3c2383a369adaf7f92616893be9e5;hpb=5d73d92f560b079f41f62e91d15ddc1fda897867;p=mirror_edk2.git diff --git a/ShellPkg/Library/UefiShellDebug1CommandsLib/SetVar.c b/ShellPkg/Library/UefiShellDebug1CommandsLib/SetVar.c index f8c6dd2e30..d98a3469a3 100644 --- a/ShellPkg/Library/UefiShellDebug1CommandsLib/SetVar.c +++ b/ShellPkg/Library/UefiShellDebug1CommandsLib/SetVar.c @@ -1,7 +1,8 @@ /** @file Main file for SetVar shell Debug1 function. - Copyright (c) 2010, Intel Corporation. All rights reserved.
+ (C) Copyright 2015 Hewlett-Packard Development Company, L.P.
+ Copyright (c) 2010 - 2014, 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 @@ -22,6 +23,40 @@ STATIC CONST SHELL_PARAM_ITEM ParamList[] = { {NULL, TypeMax} }; + +/** + Check if the input is a (potentially empty) string of hexadecimal nibbles. + + @param[in] String The CHAR16 string to check. + + @retval FALSE A character has been found in String for which + ShellIsHexaDecimalDigitCharacter() returned FALSE. + + @retval TRUE Otherwise. (Note that this covers the case when String is + empty.) +**/ +BOOLEAN +IsStringOfHexNibbles ( + IN CONST CHAR16 *String + ) +{ + CONST CHAR16 *Pos; + + for (Pos = String; *Pos != L'\0'; ++Pos) { + if (!ShellIsHexaDecimalDigitCharacter (*Pos)) { + return FALSE; + } + } + return TRUE; +} + + +/** + Function for 'setvar' command. + + @param[in] ImageHandle Handle to the Image (NULL if Internal). + @param[in] SystemTable Pointer to the System Table (NULL if Internal). +**/ SHELL_STATUS EFIAPI ShellCommandRunSetVar ( @@ -42,7 +77,6 @@ ShellCommandRunSetVar ( UINTN Size; UINTN LoopVar; EFI_DEVICE_PATH_PROTOCOL *DevPath; - EFI_DEVICE_PATH_FROM_TEXT_PROTOCOL *DevPathFromText; ShellStatus = SHELL_SUCCESS; Status = EFI_SUCCESS; @@ -66,7 +100,7 @@ ShellCommandRunSetVar ( Status = ShellCommandLineParse (ParamList, &Package, &ProblemParam, TRUE); if (EFI_ERROR(Status)) { if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) { - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellDebug1HiiHandle, ProblemParam); + ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellDebug1HiiHandle, L"setvar", ProblemParam); FreePool(ProblemParam); ShellStatus = SHELL_INVALID_PARAMETER; } else { @@ -74,10 +108,10 @@ ShellCommandRunSetVar ( } } else { if (ShellCommandLineGetCount(Package) < 2) { - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellDebug1HiiHandle); + 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); + ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellDebug1HiiHandle, L"setvar"); ShellStatus = SHELL_INVALID_PARAMETER; } else { VariableName = ShellCommandLineGetRawValue(Package, 1); @@ -88,16 +122,17 @@ ShellCommandRunSetVar ( StringGuid = ShellCommandLineGetValue(Package, L"-guid"); Status = ConvertStringToGuid(StringGuid, &Guid); if (EFI_ERROR(Status)) { - ShellStatus = SHELL_NOT_FOUND; + ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV), gShellDebug1HiiHandle, L"setvar", StringGuid); + ShellStatus = SHELL_INVALID_PARAMETER; } } - if (Data == NULL) { + if (Data == NULL || Data[0] != L'=') { // // Display what's there // Status = gRT->GetVariable((CHAR16*)VariableName, &Guid, &Attributes, &Size, Buffer); if (Status == EFI_BUFFER_TOO_SMALL) { - Buffer = AllocatePool(Size); + Buffer = AllocateZeroPool(Size); Status = gRT->GetVariable((CHAR16*)VariableName, &Guid, &Attributes, &Size, Buffer); } if (!EFI_ERROR(Status)&& Buffer != NULL) { @@ -107,7 +142,7 @@ ShellCommandRunSetVar ( } ShellPrintEx(-1, -1, L"\r\n"); } else { - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SETVAR_ERROR_GET), gShellDebug1HiiHandle, &Guid, VariableName, Status); + 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) { @@ -116,42 +151,74 @@ ShellCommandRunSetVar ( // Status = gRT->SetVariable((CHAR16*)VariableName, &Guid, Attributes, 0, NULL); if (EFI_ERROR(Status)) { - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SETVAR_ERROR_SET), gShellDebug1HiiHandle, &Guid, VariableName, 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 (Data[0] == L'=') { - Data++; - } // - // Change what's there + // Change what's there or create a new one. // - if (ShellCommandLineGetFlag(Package, L"-bs")) { - Attributes |= EFI_VARIABLE_BOOTSERVICE_ACCESS; - } - if (ShellCommandLineGetFlag(Package, L"-rt")) { - Attributes |= EFI_VARIABLE_RUNTIME_ACCESS; - } - if (ShellCommandLineGetFlag(Package, L"-nv")) { - Attributes |= EFI_VARIABLE_NON_VOLATILE; + + ASSERT(Data[0] == L'='); + Data++; + ASSERT(Data[0] != L'\0'); + + // + // Determine if the variable exists and get the attributes + // + 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 (ShellIsHexOrDecimalNumber(Data, TRUE, FALSE)) { + + if (EFI_ERROR(Status) || Buffer == NULL) { // - // arbitrary buffer + // Creating a new variable. determine attributes from command line. // - Buffer = AllocateZeroPool((StrLen(Data) / 2)); - 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])); + Attributes = 0; + if (ShellCommandLineGetFlag(Package, L"-bs")) { + Attributes |= EFI_VARIABLE_BOOTSERVICE_ACCESS; } - 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, &Guid, VariableName, Status); - ShellStatus = SHELL_ACCESS_DENIED; + if (ShellCommandLineGetFlag(Package, L"-rt")) { + Attributes |= EFI_VARIABLE_RUNTIME_ACCESS | + EFI_VARIABLE_BOOTSERVICE_ACCESS; + } + if (ShellCommandLineGetFlag(Package, L"-nv")) { + Attributes |= EFI_VARIABLE_NON_VOLATILE; + } + } + 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 { - ASSERT(ShellStatus == SHELL_SUCCESS); + // + // 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) { // @@ -159,12 +226,15 @@ ShellCommandRunSetVar ( // Data++; Buffer = AllocateZeroPool(StrSize(Data) / 2); - 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 (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, &Guid, VariableName, 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); @@ -176,15 +246,20 @@ ShellCommandRunSetVar ( Data++; Data++; Buffer = AllocateZeroPool(StrSize(Data)); - 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, &Guid, VariableName, Status); - ShellStatus = SHELL_ACCESS_DENIED; + if (Buffer == NULL) { + ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_OUT_MEM), gShellDebug1HiiHandle, L"setvar"); + ShellStatus = SHELL_OUT_OF_RESOURCES; } else { - ASSERT(ShellStatus == SHELL_SUCCESS); + 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) { // @@ -192,23 +267,21 @@ ShellCommandRunSetVar ( // Data++; Data++; - Status = gBS->LocateProtocol(&gEfiDevicePathFromTextProtocolGuid, NULL, (VOID**)&DevPathFromText); - ASSERT_EFI_ERROR(Status); - DevPath = DevPathFromText->ConvertTextToDevicePath(Data); + DevPath = ConvertTextToDevicePath(Data); if (DevPath == NULL) { - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SETVAR_ERROR_DPFT), gShellDebug1HiiHandle, Status); + 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, &Guid, VariableName, 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 { - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellDebug1HiiHandle, Data); + ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV), gShellDebug1HiiHandle, L"setvar", Data); ShellStatus = SHELL_INVALID_PARAMETER; } }