X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=ShellPkg%2FLibrary%2FUefiShellCommandLib%2FUefiShellCommandLib.c;h=ddc4bb1567c6e52eab7abd50fc8c0e85a5097bf5;hb=7381bd3e753c4d3b706c752ec1d4305b3378af35;hp=9e7cd0e6e1464a1b9074f50a7528ad8431aef37a;hpb=1a63ec8f82705d4fb97e4034c1471c8e2a5c9cc5;p=mirror_edk2.git diff --git a/ShellPkg/Library/UefiShellCommandLib/UefiShellCommandLib.c b/ShellPkg/Library/UefiShellCommandLib/UefiShellCommandLib.c index 9e7cd0e6e1..ddc4bb1567 100644 --- a/ShellPkg/Library/UefiShellCommandLib/UefiShellCommandLib.c +++ b/ShellPkg/Library/UefiShellCommandLib/UefiShellCommandLib.c @@ -1,7 +1,10 @@ /** @file Provides interface to shell internal functions for shell commands. - Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.
+ Copyright (c) 2009 - 2018, 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 @@ -14,18 +17,13 @@ #include "UefiShellCommandLib.h" -/// The tag for use in identifying UNICODE files. -/// If the file is UNICODE, the first 16 bits of the file will equal this value. -enum { - gUnicodeFileTag = 0xFEFF -}; - // STATIC local variables STATIC SHELL_COMMAND_INTERNAL_LIST_ENTRY mCommandList; STATIC SCRIPT_FILE_LIST mScriptList; STATIC ALIAS_LIST mAliasList; STATIC BOOLEAN mEchoState; STATIC BOOLEAN mExitRequested; +STATIC UINT64 mExitCode; STATIC BOOLEAN mExitScript; STATIC CHAR16 *mProfileList; STATIC UINTN mProfileListSize; @@ -33,13 +31,29 @@ 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_SHELL_PROTOCOL *gEfiShellProtocol = NULL; -EFI_SHELL_PARAMETERS_PROTOCOL *gEfiShellParametersProtocol = NULL; EFI_UNICODE_COLLATION_PROTOCOL *gUnicodeCollation = NULL; -EFI_DEVICE_PATH_TO_TEXT_PROTOCOL *gDevPathToText = NULL; SHELL_MAP_LIST gShellMapList; -SHELL_MAP_LIST *gShellCurDir = NULL; +SHELL_MAP_LIST *gShellCurMapping = NULL; CONST CHAR16* SupportLevel[] = { L"Minimal", @@ -58,38 +72,70 @@ CommandInit( VOID ) { - EFI_STATUS Status; - if (gEfiShellParametersProtocol == NULL) { - Status = gBS->OpenProtocol(gImageHandle, - &gEfiShellParametersProtocolGuid, - (VOID **)&gEfiShellParametersProtocol, - gImageHandle, - NULL, - EFI_OPEN_PROTOCOL_GET_PROTOCOL - ); - if (EFI_ERROR(Status)) { - return (EFI_DEVICE_ERROR); - } - } - if (gEfiShellProtocol == NULL) { - Status = gBS->LocateProtocol(&gEfiShellProtocolGuid, NULL, (VOID**)&gEfiShellProtocol); - if (EFI_ERROR(Status)) { - return (EFI_DEVICE_ERROR); - } + UINTN NumHandles; + EFI_HANDLE *Handles; + EFI_UNICODE_COLLATION_PROTOCOL *Uc; + CHAR8 *BestLanguage; + UINTN Index; + EFI_STATUS Status; + CHAR8 *PlatformLang; + + GetEfiGlobalVariable2 (EFI_PLATFORM_LANG_VARIABLE_NAME, (VOID**)&PlatformLang, NULL); + if (PlatformLang == NULL) { + return EFI_UNSUPPORTED; } + if (gUnicodeCollation == NULL) { - Status = gBS->LocateProtocol(&gEfiUnicodeCollation2ProtocolGuid, NULL, (VOID**)&gUnicodeCollation); - if (EFI_ERROR(Status)) { - return (EFI_DEVICE_ERROR); + Status = gBS->LocateHandleBuffer ( + ByProtocol, + &gEfiUnicodeCollation2ProtocolGuid, + NULL, + &NumHandles, + &Handles + ); + if (EFI_ERROR (Status)) { + NumHandles = 0; + Handles = NULL; } - } - if (gDevPathToText == NULL) { - Status = gBS->LocateProtocol(&gEfiDevicePathToTextProtocolGuid, NULL, (VOID**)&gDevPathToText); - if (EFI_ERROR(Status)) { - return (EFI_DEVICE_ERROR); + for (Index = 0; Index < NumHandles; Index++) { + // + // Open Unicode Collation Protocol + // + Status = gBS->OpenProtocol ( + Handles[Index], + &gEfiUnicodeCollation2ProtocolGuid, + (VOID **) &Uc, + gImageHandle, + NULL, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + continue; + } + + // + // Find the best matching matching language from the supported languages + // of Unicode Collation2 protocol. + // + BestLanguage = GetBestLanguage ( + Uc->SupportedLanguages, + FALSE, + PlatformLang, + NULL + ); + if (BestLanguage != NULL) { + FreePool (BestLanguage); + gUnicodeCollation = Uc; + break; + } } + if (Handles != NULL) { + FreePool (Handles); + } + FreePool (PlatformLang); } - return (EFI_SUCCESS); + + return (gUnicodeCollation == NULL) ? EFI_UNSUPPORTED : EFI_SUCCESS; } /** @@ -122,16 +168,44 @@ ShellCommandLibConstructor ( mProfileListSize = 0; mProfileList = NULL; - if (gUnicodeCollation == NULL) { - Status = gBS->LocateProtocol(&gEfiUnicodeCollation2ProtocolGuid, NULL, (VOID**)&gUnicodeCollation); - if (EFI_ERROR(Status)) { - return (EFI_DEVICE_ERROR); - } + Status = CommandInit (); + if (EFI_ERROR (Status)) { + return EFI_DEVICE_ERROR; } return (RETURN_SUCCESS); } +/** + Frees list of file handles. + + @param[in] List The list to free. +**/ +VOID +FreeFileHandleList ( + IN BUFFER_LIST *List + ) +{ + BUFFER_LIST *BufferListEntry; + + if (List == NULL){ + return; + } + // + // enumerate through the buffer list and free all memory + // + for ( BufferListEntry = ( BUFFER_LIST *)GetFirstNode(&List->Link) + ; !IsListEmpty (&List->Link) + ; BufferListEntry = (BUFFER_LIST *)GetFirstNode(&List->Link) + ){ + RemoveEntryList(&BufferListEntry->Link); + ASSERT(BufferListEntry->Buffer != NULL); + SHELL_FREE_NON_NULL(((SHELL_COMMAND_FILE_HANDLE*)(BufferListEntry->Buffer))->Path); + SHELL_FREE_NON_NULL(BufferListEntry->Buffer); + SHELL_FREE_NON_NULL(BufferListEntry); + } +} + /** Destructor for the library. free any resources. @@ -148,7 +222,7 @@ ShellCommandLibDestructor ( ) { SHELL_COMMAND_INTERNAL_LIST_ENTRY *Node; - COMMAND_LIST *Node2; + ALIAS_LIST *Node2; SCRIPT_FILE_LIST *Node3; SHELL_MAP_LIST *MapNode; // @@ -163,13 +237,14 @@ ShellCommandLibDestructor ( } // - // enumerate through the init command list and free all memory + // enumerate through the alias list and free all memory // while (!IsListEmpty (&mAliasList.Link)) { - Node2 = (COMMAND_LIST *)GetFirstNode(&mAliasList.Link); + Node2 = (ALIAS_LIST *)GetFirstNode(&mAliasList.Link); RemoveEntryList(&Node2->Link); SHELL_FREE_NON_NULL(Node2->CommandString); - FreePool(Node2); + SHELL_FREE_NON_NULL(Node2->Alias); + SHELL_FREE_NON_NULL(Node2); DEBUG_CODE(Node2 = NULL;); } @@ -200,31 +275,91 @@ ShellCommandLibDestructor ( } } if (!IsListEmpty(&mFileHandleList.Link)){ - FreeBufferList(&mFileHandleList); + FreeFileHandleList(&mFileHandleList); } if (mProfileList != NULL) { FreePool(mProfileList); } - gEfiShellProtocol = NULL; - gEfiShellParametersProtocol = NULL; gUnicodeCollation = NULL; - gDevPathToText = NULL; - gShellCurDir = NULL; + gShellCurMapping = NULL; return (RETURN_SUCCESS); } /** - Checks if a command is already on the list. + Find a dynamic command protocol instance given a command name string. + + @param CommandString the command name string + + @return instance the command protocol instance, if dynamic command instance found + @retval NULL no dynamic command protocol instance found for name +**/ +CONST EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL * +ShellCommandFindDynamicCommand ( + IN CONST CHAR16 *CommandString + ) +{ + EFI_STATUS Status; + EFI_HANDLE *CommandHandleList; + EFI_HANDLE *NextCommand; + EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL *DynamicCommand; + + CommandHandleList = GetHandleListByProtocol(&gEfiShellDynamicCommandProtocolGuid); + if (CommandHandleList == NULL) { + // + // not found or out of resources + // + return NULL; + } + + for (NextCommand = CommandHandleList; *NextCommand != NULL; NextCommand++) { + Status = gBS->HandleProtocol( + *NextCommand, + &gEfiShellDynamicCommandProtocolGuid, + (VOID **)&DynamicCommand + ); + + if (EFI_ERROR(Status)) { + continue; + } + + if (gUnicodeCollation->StriColl( + gUnicodeCollation, + (CHAR16*)CommandString, + (CHAR16*)DynamicCommand->CommandName) == 0 + ){ + FreePool(CommandHandleList); + return (DynamicCommand); + } + } + + FreePool(CommandHandleList); + return (NULL); +} + +/** + Checks if a command exists as a dynamic command protocol instance @param[in] CommandString The command string to check for on the list. **/ BOOLEAN -EFIAPI -ShellCommandIsCommandOnList ( - IN CONST CHAR16 *CommandString +ShellCommandDynamicCommandExists ( + IN CONST CHAR16 *CommandString + ) +{ + return (BOOLEAN) ((ShellCommandFindDynamicCommand(CommandString) != NULL)); +} + +/** + Checks if a command is already on the internal command list. + + @param[in] CommandString The command string to check for on the list. +**/ +BOOLEAN +ShellCommandIsCommandOnInternalList( + IN CONST CHAR16 *CommandString ) { SHELL_COMMAND_INTERNAL_LIST_ENTRY *Node; @@ -254,7 +389,51 @@ ShellCommandIsCommandOnList ( } /** - Get the help text for a command. + Checks if a command exists, either internally or through the dynamic command protocol. + + @param[in] CommandString The command string to check for on the list. +**/ +BOOLEAN +EFIAPI +ShellCommandIsCommandOnList( + IN CONST CHAR16 *CommandString + ) +{ + if (ShellCommandIsCommandOnInternalList(CommandString)) { + return TRUE; + } + + return ShellCommandDynamicCommandExists(CommandString); +} + +/** + Get the help text for a dynamic command. + + @param[in] CommandString The command name. + + @retval NULL No help text was found. + @return String of help text. Caller required to free. +**/ +CHAR16* +ShellCommandGetDynamicCommandHelp( + IN CONST CHAR16 *CommandString + ) +{ + EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL *DynamicCommand; + + DynamicCommand = (EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL *)ShellCommandFindDynamicCommand(CommandString); + if (DynamicCommand == NULL) { + return (NULL); + } + + // + // TODO: how to get proper language? + // + return DynamicCommand->GetHelp(DynamicCommand, "en"); +} + +/** + Get the help text for an internal command. @param[in] CommandString The command name. @@ -262,8 +441,7 @@ ShellCommandIsCommandOnList ( @return String of help text. Caller reuiqred to free. **/ CHAR16* -EFIAPI -ShellCommandGetCommandHelp ( +ShellCommandGetInternalCommandHelp( IN CONST CHAR16 *CommandString ) { @@ -293,6 +471,31 @@ ShellCommandGetCommandHelp ( return (NULL); } +/** + Get the help text for a command. + + @param[in] CommandString The command name. + + @retval NULL No help text was found. + @return String of help text.Caller reuiqred to free. +**/ +CHAR16* +EFIAPI +ShellCommandGetCommandHelp ( + IN CONST CHAR16 *CommandString + ) +{ + CHAR16 *HelpStr; + HelpStr = ShellCommandGetInternalCommandHelp(CommandString); + + if (HelpStr == NULL) { + HelpStr = ShellCommandGetDynamicCommandHelp(CommandString); + } + + return HelpStr; +} + + /** Registers handlers of type SHELL_RUN_COMMAND and SHELL_GET_MAN_FILENAME for each shell command. @@ -354,6 +557,16 @@ ShellCommandRegisterCommandName ( ) { SHELL_COMMAND_INTERNAL_LIST_ENTRY *Node; + SHELL_COMMAND_INTERNAL_LIST_ENTRY *Command; + SHELL_COMMAND_INTERNAL_LIST_ENTRY *PrevCommand; + INTN LexicalMatchValue; + + // + // Initialize local variables. + // + Command = NULL; + PrevCommand = NULL; + LexicalMatchValue = 0; // // ASSERTs for NULL parameters @@ -381,14 +594,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; @@ -412,9 +625,40 @@ ShellCommandRegisterCommandName ( } // - // add the new struct to the list + // Insert a new entry on top of the list // - InsertTailList (&mCommandList.Link, &Node->Link); + InsertHeadList (&mCommandList.Link, &Node->Link); + + // + // Move a new registered command to its sorted ordered location in the list + // + for (Command = (SHELL_COMMAND_INTERNAL_LIST_ENTRY *)GetFirstNode (&mCommandList.Link), + PrevCommand = (SHELL_COMMAND_INTERNAL_LIST_ENTRY *)GetFirstNode (&mCommandList.Link) + ; !IsNull (&mCommandList.Link, &Command->Link) + ; Command = (SHELL_COMMAND_INTERNAL_LIST_ENTRY *)GetNextNode (&mCommandList.Link, &Command->Link)) { + + // + // Get Lexical Comparison Value between PrevCommand and Command list entry + // + LexicalMatchValue = gUnicodeCollation->StriColl ( + gUnicodeCollation, + PrevCommand->CommandString, + Command->CommandString + ); + + // + // Swap PrevCommand and Command list entry if PrevCommand list entry + // is alphabetically greater than Command list entry + // + if (LexicalMatchValue > 0){ + Command = (SHELL_COMMAND_INTERNAL_LIST_ENTRY *) SwapListEntries (&PrevCommand->Link, &Command->Link); + } else if (LexicalMatchValue < 0) { + // + // PrevCommand entry is lexically lower than Command entry + // + break; + } + } return (RETURN_SUCCESS); } @@ -446,12 +690,12 @@ ShellCommandGetProfileList ( information will be returned. If Sections is NULL, then all help text information available will be returned. - @param[in] CommandString Pointer to the command name. This is the name - found on the command line in the shell. - @param[in,out] RetVal Pointer to the return vaule from the command handler. + @param[in] CommandString Pointer to the command name. This is the name + found on the command line in the shell. + @param[in, out] RetVal Pointer to the return vaule from the command handler. - @param[in,out] CanAffectLE indicates whether this command's return value - needs to be placed into LASTERROR environment variable. + @param[in, out] CanAffectLE indicates whether this command's return value + needs to be placed into LASTERROR environment variable. @retval RETURN_SUCCESS The handler was run. @retval RETURN_NOT_FOUND The CommandString did not match a registered @@ -466,7 +710,8 @@ ShellCommandRunCommandHandler ( IN OUT BOOLEAN *CanAffectLE OPTIONAL ) { - SHELL_COMMAND_INTERNAL_LIST_ENTRY *Node; + SHELL_COMMAND_INTERNAL_LIST_ENTRY *Node; + EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL *DynamicCommand; // // assert for NULL parameters @@ -485,7 +730,7 @@ ShellCommandRunCommandHandler ( gUnicodeCollation, (CHAR16*)CommandString, Node->CommandString) == 0 - ){ + ){ if (CanAffectLE != NULL) { *CanAffectLE = Node->LastError; } @@ -497,6 +742,20 @@ ShellCommandRunCommandHandler ( return (RETURN_SUCCESS); } } + + // + // An internal command was not found, try to find a dynamic command + // + DynamicCommand = (EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL *)ShellCommandFindDynamicCommand(CommandString); + if (DynamicCommand != NULL) { + if (RetVal != NULL) { + *RetVal = DynamicCommand->Handler(DynamicCommand, gST, gEfiShellParametersProtocol, gEfiShellProtocol); + } else { + DynamicCommand->Handler(DynamicCommand, gST, gEfiShellParametersProtocol, gEfiShellProtocol); + } + return (RETURN_SUCCESS); + } + return (RETURN_NOT_FOUND); } @@ -587,6 +846,9 @@ ShellCommandRegisterAlias ( ) { ALIAS_LIST *Node; + ALIAS_LIST *CommandAlias; + ALIAS_LIST *PrevCommandAlias; + INTN LexicalMatchValue; // // Asserts for NULL @@ -598,22 +860,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); } @@ -683,7 +975,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 @@ -699,7 +991,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. @@ -719,12 +1011,14 @@ ShellCommandSetEchoState( /** Indicate that the current shell or script should exit. - @param[in] ScriptOnly TRUE if only exiting a script, FALSE othrwise. + @param[in] ScriptOnly TRUE if exiting a script; FALSE otherwise. + @param[in] ErrorCode The 64 bit error code to return. **/ VOID EFIAPI ShellCommandRegisterExit ( - IN BOOLEAN ScriptOnly + IN BOOLEAN ScriptOnly, + IN CONST UINT64 ErrorCode ) { mExitRequested = (BOOLEAN)(!mExitRequested); @@ -733,6 +1027,7 @@ ShellCommandRegisterExit ( } else { mExitScript = FALSE; } + mExitCode = ErrorCode; } /** @@ -750,6 +1045,21 @@ ShellCommandGetExit ( return (mExitRequested); } +/** + Retrieve the Exit code. + + If ShellCommandGetExit returns FALSE than the return from this is undefined. + + @return the value passed into RegisterExit. +**/ +UINT64 +EFIAPI +ShellCommandGetExitCode ( + VOID + ) +{ + return (mExitCode); +} /** Retrieve the Exit script indicator. @@ -938,12 +1248,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); } } @@ -966,10 +1275,8 @@ ShellCommandAddMapItemAndUpdatePath( ASSERT((NewPath == NULL && NewPathSize == 0) || (NewPath != NULL)); if (OriginalPath != NULL) { StrnCatGrow(&NewPath, &NewPathSize, OriginalPath, 0); - } else { - StrnCatGrow(&NewPath, &NewPathSize, L".\\", 0); + StrnCatGrow(&NewPath, &NewPathSize, L";", 0); } - StrnCatGrow(&NewPath, &NewPathSize, L";", 0); StrnCatGrow(&NewPath, &NewPathSize, Name, 0); StrnCatGrow(&NewPath, &NewPathSize, L"\\efi\\tools\\;", 0); StrnCatGrow(&NewPath, &NewPathSize, Name, 0); @@ -1015,7 +1322,14 @@ ShellCommandCreateInitialMappingsAndPaths( CHAR16 *NewConsistName; EFI_DEVICE_PATH_PROTOCOL **ConsistMappingTable; SHELL_MAP_LIST *MapListNode; - + CONST CHAR16 *CurDir; + CHAR16 *SplitCurDir; + CHAR16 *MapName; + SHELL_MAP_LIST *MapListItem; + + SplitCurDir = NULL; + MapName = NULL; + MapListItem = NULL; HandleList = NULL; // @@ -1035,6 +1349,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 } @@ -1053,7 +1370,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]); @@ -1095,6 +1415,33 @@ ShellCommandCreateInitialMappingsAndPaths( SHELL_FREE_NON_NULL(DevicePathList); HandleList = NULL; + + // + //gShellCurMapping point to node of current file system in the gShellMapList. When reset all mappings, + //all nodes in the gShellMapList will be free. Then gShellCurMapping will be a dangling pointer, So, + //after created new mappings, we should reset the gShellCurMapping pointer back to node of current file system. + // + if (gShellCurMapping != NULL) { + gShellCurMapping = NULL; + CurDir = gEfiShellProtocol->GetEnv(L"cwd"); + if (CurDir != NULL) { + MapName = AllocateCopyPool (StrSize(CurDir), CurDir); + if (MapName == NULL) { + return EFI_OUT_OF_RESOURCES; + } + SplitCurDir = StrStr (MapName, L":"); + if (SplitCurDir == NULL) { + SHELL_FREE_NON_NULL (MapName); + return EFI_UNSUPPORTED; + } + *(SplitCurDir + 1) = CHAR_NULL; + MapListItem = ShellCommandFindMapItem (MapName); + if (MapListItem != NULL) { + gShellCurMapping = MapListItem; + } + SHELL_FREE_NON_NULL (MapName); + } + } } else { Count = (UINTN)-1; } @@ -1110,7 +1457,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]); @@ -1145,27 +1495,110 @@ ShellCommandCreateInitialMappingsAndPaths( } /** - Function to make sure all directory delimeters are backslashes. - - @param[in,out] Path The path to modify. + Add mappings for any devices without one. Do not change any existing maps. - @return Path. + @retval EFI_SUCCESS The operation was successful. **/ -CHAR16* +EFI_STATUS EFIAPI -ShellCommandCleanPath ( - IN OUT CHAR16 *Path +ShellCommandUpdateMapping ( + VOID ) { - CHAR16 *Path2; + EFI_STATUS Status; + EFI_HANDLE *HandleList; + UINTN Count; + EFI_DEVICE_PATH_PROTOCOL **DevicePathList; + CHAR16 *NewDefaultName; + CHAR16 *NewConsistName; + EFI_DEVICE_PATH_PROTOCOL **ConsistMappingTable; + + HandleList = NULL; + Status = EFI_SUCCESS; + + // + // remove mappings that represent removed devices. + // + + // + // Find each handle with Simple File System + // + HandleList = GetHandleListByProtocol(&gEfiSimpleFileSystemProtocolGuid); + if (HandleList != NULL) { + // + // Do a count of the handles + // + for (Count = 0 ; HandleList[Count] != NULL ; Count++); - for (Path2 = Path ; Path2 != NULL && *Path2 != CHAR_NULL ; Path2++) { - if (*Path2 == L'/') { - *Path2 = L'\\'; + // + // Get all Device Paths + // + DevicePathList = AllocateZeroPool(sizeof(EFI_DEVICE_PATH_PROTOCOL*) * Count); + if (DevicePathList == NULL) { + return (EFI_OUT_OF_RESOURCES); } + + for (Count = 0 ; HandleList[Count] != NULL ; Count++) { + DevicePathList[Count] = DevicePathFromHandle(HandleList[Count]); + } + + // + // Sort all DevicePaths + // + PerformQuickSort(DevicePathList, Count, sizeof(EFI_DEVICE_PATH_PROTOCOL*), DevicePathCompare); + + ShellCommandConsistMappingInitialize(&ConsistMappingTable); + + // + // Assign new Mappings to remainders + // + for (Count = 0 ; !EFI_ERROR(Status) && HandleList[Count] != NULL && !EFI_ERROR(Status); Count++) { + // + // Skip ones that already have + // + if (gEfiShellProtocol->GetMapFromDevicePath(&DevicePathList[Count]) != NULL) { + continue; + } + // + // Get default name + // + NewDefaultName = ShellCommandCreateNewMappingName(MappingTypeFileSystem); + if (NewDefaultName == NULL) { + Status = EFI_OUT_OF_RESOURCES; + break; + } + + // + // Call shell protocol SetMap function now... + // + Status = gEfiShellProtocol->SetMap(DevicePathList[Count], NewDefaultName); + + if (!EFI_ERROR(Status)) { + // + // Now do consistent name + // + NewConsistName = ShellCommandConsistMappingGenMappingName(DevicePathList[Count], ConsistMappingTable); + if (NewConsistName != NULL) { + Status = gEfiShellProtocol->SetMap(DevicePathList[Count], NewConsistName); + FreePool(NewConsistName); + } + } + + FreePool(NewDefaultName); + } + ShellCommandConsistMappingUnInitialize(ConsistMappingTable); + SHELL_FREE_NON_NULL(HandleList); + SHELL_FREE_NON_NULL(DevicePathList); + + HandleList = NULL; + } else { + Count = (UINTN)-1; } + // + // Do it all over again for gEfiBlockIoProtocolGuid + // - return (Path); + return (Status); } /** @@ -1210,11 +1643,14 @@ ConvertEfiFileProtocolToShellHandle( } NewNode = AllocateZeroPool(sizeof(BUFFER_LIST)); if (NewNode == NULL) { + SHELL_FREE_NON_NULL(Buffer); return (NULL); } Buffer->FileHandle = (EFI_FILE_PROTOCOL*)Handle; Buffer->Path = StrnCatGrow(&Buffer->Path, NULL, Path, 0); if (Buffer->Path == NULL) { + SHELL_FREE_NON_NULL(NewNode); + SHELL_FREE_NON_NULL(Buffer); return (NULL); } NewNode->Buffer = Buffer; @@ -1310,7 +1746,6 @@ ShellFileHandleEof( gEfiShellProtocol->GetFilePosition(Handle, &Pos); Info = gEfiShellProtocol->GetFileInfo (Handle); - ASSERT(Info != NULL); gEfiShellProtocol->SetFilePosition(Handle, Pos); if (Info == NULL) { @@ -1328,157 +1763,6 @@ ShellFileHandleEof( return (RetVal); } -/** - Function to read a single line from a SHELL_FILE_HANDLE. The \n is not included in the returned - buffer. The returned buffer must be callee freed. - - If the position upon start is 0, then the Ascii Boolean will be set. This should be - maintained and not changed for all operations with the same file. - - @param[in] Handle SHELL_FILE_HANDLE to read from. - @param[in,out] Ascii Boolean value for indicating whether the file is - Ascii (TRUE) or UCS2 (FALSE). - - @return The line of text from the file. - - @sa ShellFileHandleReadLine -**/ -CHAR16* -EFIAPI -ShellFileHandleReturnLine( - IN SHELL_FILE_HANDLE Handle, - IN OUT BOOLEAN *Ascii - ) -{ - CHAR16 *RetVal; - UINTN Size; - EFI_STATUS Status; - - Size = 0; - RetVal = NULL; - - Status = ShellFileHandleReadLine(Handle, RetVal, &Size, FALSE, Ascii); - if (Status == EFI_BUFFER_TOO_SMALL) { - RetVal = AllocateZeroPool(Size); - Status = ShellFileHandleReadLine(Handle, RetVal, &Size, FALSE, Ascii); - } - ASSERT_EFI_ERROR(Status); - if (EFI_ERROR(Status) && (RetVal != NULL)) { - FreePool(RetVal); - RetVal = NULL; - } - return (RetVal); -} - -/** - Function to read a single line (up to but not including the \n) from a SHELL_FILE_HANDLE. - - If the position upon start is 0, then the Ascii Boolean will be set. This should be - maintained and not changed for all operations with the same file. - - @param[in] Handle SHELL_FILE_HANDLE to read from. - @param[in,out] Buffer The pointer to buffer to read into. - @param[in,out] Size The pointer to number of bytes in Buffer. - @param[in] Truncate If the buffer is large enough, this has no effect. - If the buffer is is too small and Truncate is TRUE, - the line will be truncated. - If the buffer is is too small and Truncate is FALSE, - then no read will occur. - - @param[in,out] Ascii Boolean value for indicating whether the file is - Ascii (TRUE) or UCS2 (FALSE). - - @retval EFI_SUCCESS The operation was successful. The line is stored in - Buffer. - @retval EFI_INVALID_PARAMETER Handle was NULL. - @retval EFI_INVALID_PARAMETER Size was NULL. - @retval EFI_BUFFER_TOO_SMALL Size was not large enough to store the line. - Size was updated to the minimum space required. - @sa ShellFileHandleRead -**/ -EFI_STATUS -EFIAPI -ShellFileHandleReadLine( - IN SHELL_FILE_HANDLE Handle, - IN OUT CHAR16 *Buffer, - IN OUT UINTN *Size, - IN BOOLEAN Truncate, - IN OUT BOOLEAN *Ascii - ) -{ - EFI_STATUS Status; - CHAR16 CharBuffer; - UINTN CharSize; - UINTN CountSoFar; - UINT64 OriginalFilePosition; - - - if (Handle == NULL - ||Size == NULL - ){ - return (EFI_INVALID_PARAMETER); - } - if (Buffer == NULL) { - ASSERT(*Size == 0); - } else { - *Buffer = CHAR_NULL; - } - gEfiShellProtocol->GetFilePosition(Handle, &OriginalFilePosition); - if (OriginalFilePosition == 0) { - CharSize = sizeof(CHAR16); - Status = gEfiShellProtocol->ReadFile(Handle, &CharSize, &CharBuffer); - ASSERT_EFI_ERROR(Status); - if (CharBuffer == gUnicodeFileTag) { - *Ascii = FALSE; - } else { - *Ascii = TRUE; - gEfiShellProtocol->SetFilePosition(Handle, OriginalFilePosition); - } - } - - for (CountSoFar = 0;;CountSoFar++){ - CharBuffer = 0; - if (*Ascii) { - CharSize = sizeof(CHAR8); - } else { - CharSize = sizeof(CHAR16); - } - Status = gEfiShellProtocol->ReadFile(Handle, &CharSize, &CharBuffer); - if ( EFI_ERROR(Status) - || CharSize == 0 - || (CharBuffer == L'\n' && !(*Ascii)) - || (CharBuffer == '\n' && *Ascii) - ){ - break; - } - // - // if we have space save it... - // - if ((CountSoFar+1)*sizeof(CHAR16) < *Size){ - ASSERT(Buffer != NULL); - ((CHAR16*)Buffer)[CountSoFar] = CharBuffer; - ((CHAR16*)Buffer)[CountSoFar+1] = CHAR_NULL; - } - } - - // - // if we ran out of space tell when... - // - if ((CountSoFar+1)*sizeof(CHAR16) > *Size){ - *Size = (CountSoFar+1)*sizeof(CHAR16); - if (!Truncate) { - gEfiShellProtocol->SetFilePosition(Handle, OriginalFilePosition); - } else { - DEBUG((DEBUG_WARN, "The line was truncated in ShellFileHandleReadLine")); - } - return (EFI_BUFFER_TOO_SMALL); - } - while(Buffer[StrLen(Buffer)-1] == L'\r') { - Buffer[StrLen(Buffer)-1] = CHAR_NULL; - } - - return (Status); -} /** Frees any BUFFER_LIST defined type. @@ -1503,7 +1787,6 @@ FreeBufferList ( ; BufferListEntry = (BUFFER_LIST *)GetFirstNode(&List->Link) ){ RemoveEntryList(&BufferListEntry->Link); - ASSERT(BufferListEntry->Buffer != NULL); if (BufferListEntry->Buffer != NULL) { FreePool(BufferListEntry->Buffer); } @@ -1512,36 +1795,111 @@ FreeBufferList ( } /** - Chops off last directory or file entry in a path leaving the trailing slash - - @param[in,out] PathToReturn The path to modify. + Dump some hexadecimal data to the screen. - @retval FALSE No directory was found to chop off. - @retval TRUE A directory was chopped off. + @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. **/ -BOOLEAN +VOID EFIAPI -ChopLastSlash( - IN OUT CHAR16 *PathToReturn +DumpHex ( + IN UINTN Indent, + IN UINTN Offset, + IN UINTN DataSize, + IN VOID *UserData ) { - CHAR16 *Walker; - CHAR16 *LastSlash; - // - // get directory name from path... ('chop' off extra) - // - for ( Walker = PathToReturn, LastSlash = NULL - ; Walker != NULL && *Walker != CHAR_NULL - ; Walker++ - ){ - if (*Walker == L'\\' && *(Walker + 1) != CHAR_NULL) { - LastSlash = Walker+1; + 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 > '~') ? '.' : 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; } - if (LastSlash != NULL) { - *LastSlash = CHAR_NULL; - return (TRUE); - } - return (FALSE); } +/** + 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* +EFIAPI +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; +}