X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=ShellPkg%2FLibrary%2FUefiShellCommandLib%2FUefiShellCommandLib.c;h=35e0611a8e4dcf12f0e249e2f976fa79463a9d12;hp=ebb84dd55ec1d5405221370d65b9c3b8c1bbb6f3;hb=107d05a433c3ea5d877baaa4867ede7708323d38;hpb=ae315cc26984d308dbe07b8e01dea7c56a78f79d diff --git a/ShellPkg/Library/UefiShellCommandLib/UefiShellCommandLib.c b/ShellPkg/Library/UefiShellCommandLib/UefiShellCommandLib.c index ebb84dd55e..35e0611a8e 100644 --- a/ShellPkg/Library/UefiShellCommandLib/UefiShellCommandLib.c +++ b/ShellPkg/Library/UefiShellCommandLib/UefiShellCommandLib.c @@ -1,8 +1,10 @@ /** @file Provides interface to shell internal functions for shell commands. - (C) Copyright 2013-2014, Hewlett-Packard Development Company, L.P. - Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.
+ Copyright (c) 2009 - 2016, Intel Corporation. All rights reserved.
+ (C) Copyright 2013-2015 Hewlett-Packard Development Company, L.P.
+ (C) Copyright 2016 Hewlett Packard Enterprise Development LP
+ 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 @@ -29,6 +31,25 @@ STATIC UINTN mFsMaxCount = 0; STATIC UINTN mBlkMaxCount = 0; STATIC BUFFER_LIST mFileHandleList; +STATIC CONST CHAR8 Hex[] = { + '0', + '1', + '2', + '3', + '4', + '5', + '6', + '7', + '8', + '9', + 'A', + 'B', + 'C', + 'D', + 'E', + 'F' +}; + // global variables required by library class. EFI_UNICODE_COLLATION_PROTOCOL *gUnicodeCollation = NULL; SHELL_MAP_LIST gShellMapList; @@ -215,7 +236,7 @@ ShellCommandLibDestructor ( } /** - Find a dynamic command protocol instance given a command name string + Find a dynamic command protocol instance given a command name string. @param CommandString the command name string @@ -277,7 +298,7 @@ ShellCommandDynamicCommandExists ( IN CONST CHAR16 *CommandString ) { - return (ShellCommandFindDynamicCommand(CommandString) != NULL); + return (BOOLEAN) ((ShellCommandFindDynamicCommand(CommandString) != NULL)); } /** @@ -525,14 +546,14 @@ ShellCommandRegisterCommandName ( // allocate memory for new struct // Node = AllocateZeroPool(sizeof(SHELL_COMMAND_INTERNAL_LIST_ENTRY)); - ASSERT(Node != NULL); - Node->CommandString = AllocateZeroPool(StrSize(CommandString)); - ASSERT(Node->CommandString != NULL); - - // - // populate the new struct - // - StrCpy(Node->CommandString, CommandString); + if (Node == NULL) { + return RETURN_OUT_OF_RESOURCES; + } + Node->CommandString = AllocateCopyPool(StrSize(CommandString), CommandString); + if (Node->CommandString == NULL) { + FreePool (Node); + return RETURN_OUT_OF_RESOURCES; + } Node->GetManFileName = GetManFileName; Node->CommandHandler = CommandHandler; @@ -777,6 +798,9 @@ ShellCommandRegisterAlias ( ) { ALIAS_LIST *Node; + ALIAS_LIST *CommandAlias; + ALIAS_LIST *PrevCommandAlias; + INTN LexicalMatchValue; // // Asserts for NULL @@ -788,22 +812,52 @@ ShellCommandRegisterAlias ( // allocate memory for new struct // Node = AllocateZeroPool(sizeof(ALIAS_LIST)); - ASSERT(Node != NULL); - Node->CommandString = AllocateZeroPool(StrSize(Command)); - Node->Alias = AllocateZeroPool(StrSize(Alias)); - ASSERT(Node->CommandString != NULL); - ASSERT(Node->Alias != NULL); + if (Node == NULL) { + return RETURN_OUT_OF_RESOURCES; + } + Node->CommandString = AllocateCopyPool(StrSize(Command), Command); + if (Node->CommandString == NULL) { + FreePool (Node); + return RETURN_OUT_OF_RESOURCES; + } + Node->Alias = AllocateCopyPool(StrSize(Alias), Alias); + if (Node->Alias == NULL) { + FreePool (Node->CommandString); + FreePool (Node); + return RETURN_OUT_OF_RESOURCES; + } - // - // populate the new struct - // - StrCpy(Node->CommandString, Command); - StrCpy(Node->Alias , Alias ); + InsertHeadList (&mAliasList.Link, &Node->Link); // - // add the new struct to the list + // Move a new pre-defined registered alias to its sorted ordered location in the list // - InsertTailList (&mAliasList.Link, &Node->Link); + for ( CommandAlias = (ALIAS_LIST *)GetFirstNode (&mAliasList.Link), + PrevCommandAlias = (ALIAS_LIST *)GetFirstNode (&mAliasList.Link) + ; !IsNull (&mAliasList.Link, &CommandAlias->Link) + ; CommandAlias = (ALIAS_LIST *) GetNextNode (&mAliasList.Link, &CommandAlias->Link) ) { + // + // Get Lexical comparison value between PrevCommandAlias and CommandAlias List Entry + // + LexicalMatchValue = gUnicodeCollation->StriColl ( + gUnicodeCollation, + PrevCommandAlias->Alias, + CommandAlias->Alias + ); + + // + // Swap PrevCommandAlias and CommandAlias list entry if PrevCommandAlias list entry + // is alphabetically greater than CommandAlias list entry + // + if (LexicalMatchValue > 0) { + CommandAlias = (ALIAS_LIST *) SwapListEntries (&PrevCommandAlias->Link, &CommandAlias->Link); + } else if (LexicalMatchValue < 0) { + // + // PrevCommandAlias entry is lexically lower than CommandAlias entry + // + break; + } + } return (RETURN_SUCCESS); } @@ -873,7 +927,7 @@ ShellCommandIsOnAliasList( } /** - Function to determine current state of ECHO. Echo determins if lines from scripts + Function to determine current state of ECHO. Echo determines if lines from scripts and ECHO commands are enabled. @retval TRUE Echo is currently enabled @@ -889,7 +943,7 @@ ShellCommandGetEchoState( } /** - Function to set current state of ECHO. Echo determins if lines from scripts + Function to set current state of ECHO. Echo determines if lines from scripts and ECHO commands are enabled. If State is TRUE, Echo will be enabled. @@ -1146,12 +1200,11 @@ ShellCommandAddMapItemAndUpdatePath( Status = EFI_OUT_OF_RESOURCES; } else { MapListNode->Flags = Flags; - MapListNode->MapName = AllocateZeroPool(StrSize(Name)); + MapListNode->MapName = AllocateCopyPool(StrSize(Name), Name); MapListNode->DevicePath = DuplicateDevicePath(DevicePath); if ((MapListNode->MapName == NULL) || (MapListNode->DevicePath == NULL)){ Status = EFI_OUT_OF_RESOURCES; } else { - StrCpy(MapListNode->MapName, Name); InsertTailList(&gShellMapList.Link, &MapListNode->Link); } } @@ -1243,6 +1296,9 @@ ShellCommandCreateInitialMappingsAndPaths( ; MapListNode = (SHELL_MAP_LIST *)GetFirstNode(&gShellMapList.Link) ){ RemoveEntryList(&MapListNode->Link); + SHELL_FREE_NON_NULL(MapListNode->DevicePath); + SHELL_FREE_NON_NULL(MapListNode->MapName); + SHELL_FREE_NON_NULL(MapListNode->CurrentDirectoryPath); FreePool(MapListNode); } // for loop } @@ -1261,7 +1317,10 @@ ShellCommandCreateInitialMappingsAndPaths( // Get all Device Paths // DevicePathList = AllocateZeroPool(sizeof(EFI_DEVICE_PATH_PROTOCOL*) * Count); - ASSERT(DevicePathList != NULL); + if (DevicePathList == NULL) { + SHELL_FREE_NON_NULL (HandleList); + return EFI_OUT_OF_RESOURCES; + } for (Count = 0 ; HandleList[Count] != NULL ; Count++) { DevicePathList[Count] = DevicePathFromHandle(HandleList[Count]); @@ -1318,7 +1377,10 @@ ShellCommandCreateInitialMappingsAndPaths( // Get all Device Paths // DevicePathList = AllocateZeroPool(sizeof(EFI_DEVICE_PATH_PROTOCOL*) * Count); - ASSERT(DevicePathList != NULL); + if (DevicePathList == NULL) { + SHELL_FREE_NON_NULL (HandleList); + return EFI_OUT_OF_RESOURCES; + } for (Count = 0 ; HandleList[Count] != NULL ; Count++) { DevicePathList[Count] = DevicePathFromHandle(HandleList[Count]); @@ -1604,7 +1666,6 @@ ShellFileHandleEof( gEfiShellProtocol->GetFilePosition(Handle, &Pos); Info = gEfiShellProtocol->GetFileInfo (Handle); - ASSERT(Info != NULL); gEfiShellProtocol->SetFilePosition(Handle, Pos); if (Info == NULL) { @@ -1653,3 +1714,110 @@ FreeBufferList ( } } +/** + Dump some hexadecimal data to the screen. + + @param[in] Indent How many spaces to indent the output. + @param[in] Offset The offset of the printing. + @param[in] DataSize The size in bytes of UserData. + @param[in] UserData The data to print out. +**/ +VOID +DumpHex ( + IN UINTN Indent, + IN UINTN Offset, + IN UINTN DataSize, + IN VOID *UserData + ) +{ + UINT8 *Data; + + CHAR8 Val[50]; + + CHAR8 Str[20]; + + UINT8 TempByte; + UINTN Size; + UINTN Index; + + Data = UserData; + while (DataSize != 0) { + Size = 16; + if (Size > DataSize) { + Size = DataSize; + } + + for (Index = 0; Index < Size; Index += 1) { + TempByte = Data[Index]; + Val[Index * 3 + 0] = Hex[TempByte >> 4]; + Val[Index * 3 + 1] = Hex[TempByte & 0xF]; + Val[Index * 3 + 2] = (CHAR8) ((Index == 7) ? '-' : ' '); + Str[Index] = (CHAR8) ((TempByte < ' ' || TempByte > 'z') ? '.' : TempByte); + } + + Val[Index * 3] = 0; + Str[Index] = 0; + ShellPrintEx(-1, -1, L"%*a%08X: %-48a *%a*\r\n", Indent, "", Offset, Val, Str); + + Data += Size; + Offset += Size; + DataSize -= Size; + } +} + +/** + Dump HEX data into buffer. + + @param[in] Buffer HEX data to be dumped in Buffer. + @param[in] Indent How many spaces to indent the output. + @param[in] Offset The offset of the printing. + @param[in] DataSize The size in bytes of UserData. + @param[in] UserData The data to print out. +**/ +CHAR16* +CatSDumpHex ( + IN CHAR16 *Buffer, + IN UINTN Indent, + IN UINTN Offset, + IN UINTN DataSize, + IN VOID *UserData + ) +{ + UINT8 *Data; + UINT8 TempByte; + UINTN Size; + UINTN Index; + CHAR8 Val[50]; + CHAR8 Str[20]; + CHAR16 *RetVal; + CHAR16 *TempRetVal; + + Data = UserData; + RetVal = Buffer; + while (DataSize != 0) { + Size = 16; + if (Size > DataSize) { + Size = DataSize; + } + + for (Index = 0; Index < Size; Index += 1) { + TempByte = Data[Index]; + Val[Index * 3 + 0] = Hex[TempByte >> 4]; + Val[Index * 3 + 1] = Hex[TempByte & 0xF]; + Val[Index * 3 + 2] = (CHAR8) ((Index == 7) ? '-' : ' '); + Str[Index] = (CHAR8) ((TempByte < ' ' || TempByte > 'z') ? '.' : TempByte); + } + + Val[Index * 3] = 0; + Str[Index] = 0; + TempRetVal = CatSPrint (RetVal, L"%*a%08X: %-48a *%a*\r\n", Indent, "", Offset, Val, Str); + SHELL_FREE_NON_NULL (RetVal); + RetVal = TempRetVal; + + Data += Size; + Offset += Size; + DataSize -= Size; + } + + return RetVal; +}