X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=ShellPkg%2FApplication%2FShell%2FShellProtocol.c;h=e1aadb845d4eb7c49a971d9ba1919efced0c84f7;hp=214693f2ae37dbeff58762930eab5740c5b42d5e;hb=e3df6949e7e2a4578660d4079988487a147c91b7;hpb=a405b86d274d32b92f69842bfb9a1ab143128f57 diff --git a/ShellPkg/Application/Shell/ShellProtocol.c b/ShellPkg/Application/Shell/ShellProtocol.c index 214693f2ae..e1aadb845d 100644 --- a/ShellPkg/Application/Shell/ShellProtocol.c +++ b/ShellPkg/Application/Shell/ShellProtocol.c @@ -2,7 +2,7 @@ Member functions of EFI_SHELL_PROTOCOL and functions for creation, manipulation, and initialization of EFI_SHELL_PROTOCOL. - Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.
+ Copyright (c) 2009 - 2013, 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 @@ -14,7 +14,6 @@ **/ #include "Shell.h" -#include /** Close an open file handle. @@ -34,7 +33,37 @@ EfiShellClose ( ) { ShellFileHandleRemove(FileHandle); - return (FileHandleClose(FileHandle)); + return (FileHandleClose(ConvertShellHandleToEfiFileProtocol(FileHandle))); +} + +/** + Internal worker to determine whether there is a BlockIo somewhere + upon the device path specified. + + @param[in] DevicePath The device path to test. + + @retval TRUE gEfiBlockIoProtocolGuid was installed on a handle with this device path + @retval FALSE gEfiBlockIoProtocolGuid was not found. +**/ +BOOLEAN +EFIAPI +InternalShellProtocolIsBlockIoPresent( + IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath + ) +{ + EFI_DEVICE_PATH_PROTOCOL *DevicePathCopy; + EFI_STATUS Status; + EFI_HANDLE Handle; + + Handle = NULL; + + DevicePathCopy = (EFI_DEVICE_PATH_PROTOCOL*)DevicePath; + Status = gBS->LocateDevicePath(&gEfiBlockIoProtocolGuid, &DevicePathCopy, &Handle); + + if ((Handle != NULL) && (!EFI_ERROR(Status))) { + return (TRUE); + } + return (FALSE); } /** @@ -86,27 +115,19 @@ InternalShellProtocolDebugPrintMessage ( IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath ) { - EFI_DEVICE_PATH_TO_TEXT_PROTOCOL *DevicePathToText; EFI_STATUS Status; CHAR16 *Temp; Status = EFI_SUCCESS; DEBUG_CODE_BEGIN(); - DevicePathToText = NULL; - Status = gBS->LocateProtocol(&gEfiDevicePathToTextProtocolGuid, - NULL, - (VOID**)&DevicePathToText); if (Mapping != NULL) { DEBUG((EFI_D_INFO, "Added new map item:\"%S\"\r\n", Mapping)); } - if (!EFI_ERROR(Status)) { - if (DevicePath != NULL) { - Temp = DevicePathToText->ConvertDevicePathToText(DevicePath, TRUE, TRUE); - DEBUG((EFI_D_INFO, "DevicePath: %S\r\n", Temp)); - FreePool(Temp); - } - } + Temp = ConvertDevicePathToText(DevicePath, TRUE, TRUE); + DEBUG((EFI_D_INFO, "DevicePath: %S\r\n", Temp)); + FreePool(Temp); + DEBUG_CODE_END(); return (Status); } @@ -176,7 +197,8 @@ EfiShellSetMap( // make sure this is a valid to add device path // ///@todo add BlockIo to this test... - if (!InternalShellProtocolIsSimpleFileSystemPresent(DevicePath)) { + if (!InternalShellProtocolIsSimpleFileSystemPresent(DevicePath) + && !InternalShellProtocolIsBlockIoPresent(DevicePath)) { return (EFI_INVALID_PARAMETER); } @@ -386,6 +408,7 @@ EfiShellGetFilePathFromDevicePath( EFI_HANDLE MapHandle; EFI_STATUS Status; FILEPATH_DEVICE_PATH *FilePath; + FILEPATH_DEVICE_PATH *AlignedNode; PathForReturn = NULL; PathSize = 0; @@ -436,7 +459,10 @@ EfiShellGetFilePathFromDevicePath( // ASSERT((PathForReturn == NULL && PathSize == 0) || (PathForReturn != NULL)); PathForReturn = StrnCatGrow(&PathForReturn, &PathSize, L"\\", 1); - PathForReturn = StrnCatGrow(&PathForReturn, &PathSize, FilePath->PathName, 0); + + AlignedNode = AllocateCopyPool (DevicePathNodeLength(FilePath), FilePath); + PathForReturn = StrnCatGrow(&PathForReturn, &PathSize, AlignedNode->PathName, 0); + FreePool(AlignedNode); } } // for loop of remaining nodes } @@ -453,10 +479,12 @@ EfiShellGetFilePathFromDevicePath( This function converts a file system style name to a device path, by replacing any mapping references to the associated device path. - @param Path the pointer to the path + @param[in] Path The pointer to the path. - @return all The pointer of the file path. The file path is callee + @return The pointer of the file path. The file path is callee allocated and should be freed by the caller. + @retval NULL The path could not be found. + @retval NULL There was not enough available memory. **/ EFI_DEVICE_PATH_PROTOCOL * EFIAPI @@ -475,8 +503,12 @@ EfiShellGetDevicePathFromFilePath( EFI_HANDLE Handle; EFI_STATUS Status; + if (Path == NULL) { + return (NULL); + } + MapName = NULL; - ASSERT(Path != NULL); + NewPath = NULL; if (StrStr(Path, L":") == NULL) { Cwd = EfiShellGetCurDir(NULL); @@ -486,10 +518,13 @@ EfiShellGetDevicePathFromFilePath( Size = StrSize(Cwd); Size += StrSize(Path); NewPath = AllocateZeroPool(Size); - ASSERT(NewPath != NULL); + if (NewPath == NULL) { + return (NULL); + } StrCpy(NewPath, Cwd); - if (NewPath[StrLen(NewPath)-1] == Path[0] == (CHAR16)L'\\') { - ((CHAR16*)NewPath)[StrLen(NewPath)-1] = CHAR_NULL; + if (*Path == L'\\') { + Path++; + while (PathRemoveLastItem(NewPath)) ; } StrCat(NewPath, Path); DevicePathForReturn = EfiShellGetDevicePathFromFilePath(NewPath); @@ -503,8 +538,7 @@ EfiShellGetDevicePathFromFilePath( // ASSERT((MapName == NULL && Size == 0) || (MapName != NULL)); MapName = StrnCatGrow(&MapName, &Size, Path, (StrStr(Path, L":")-Path+1)); - if (MapName[StrLen(MapName)-1] != L':') { - ASSERT(FALSE); + if (MapName == NULL || MapName[StrLen(MapName)-1] != L':') { return (NULL); } @@ -524,7 +558,6 @@ EfiShellGetDevicePathFromFilePath( // DevicePathCopyForFree = DevicePathCopy = DuplicateDevicePath(DevicePath); if (DevicePathCopy == NULL) { - ASSERT(FALSE); FreePool(MapName); return (NULL); } @@ -545,7 +578,11 @@ EfiShellGetDevicePathFromFilePath( // // build the full device path // - DevicePathForReturn = FileDevicePath(Handle, Path+StrLen(MapName)+1); + if (*(Path+StrLen(MapName)+1) == CHAR_NULL) { + DevicePathForReturn = FileDevicePath(Handle, L"\\"); + } else { + DevicePathForReturn = FileDevicePath(Handle, Path+StrLen(MapName)); + } FreePool(MapName); if (DevicePathCopyForFree != NULL) { @@ -602,7 +639,6 @@ EfiShellGetDeviceName( { EFI_STATUS Status; EFI_COMPONENT_NAME2_PROTOCOL *CompName2; - EFI_DEVICE_PATH_TO_TEXT_PROTOCOL *DevicePathToText; EFI_DEVICE_PATH_PROTOCOL *DevicePath; EFI_HANDLE *HandleList; UINTN HandleCount; @@ -611,6 +647,11 @@ EfiShellGetDeviceName( CHAR8 *Lang; CHAR8 *TempChar; + UINTN ParentControllerCount; + EFI_HANDLE *ParentControllerBuffer; + UINTN ParentDriverCount; + EFI_HANDLE *ParentDriverBuffer; + if (BestDeviceName == NULL || DeviceHandle == NULL ){ @@ -663,14 +704,20 @@ EfiShellGetDeviceName( continue; } if (Language == NULL) { - Lang = AllocatePool(AsciiStrSize(CompName2->SupportedLanguages)); + Lang = AllocateZeroPool(AsciiStrSize(CompName2->SupportedLanguages)); + if (Lang == NULL) { + return (EFI_OUT_OF_RESOURCES); + } AsciiStrCpy(Lang, CompName2->SupportedLanguages); TempChar = AsciiStrStr(Lang, ";"); if (TempChar != NULL){ *TempChar = CHAR_NULL; } } else { - Lang = AllocatePool(AsciiStrSize(Language)); + Lang = AllocateZeroPool(AsciiStrSize(Language)); + if (Lang == NULL) { + return (EFI_OUT_OF_RESOURCES); + } AsciiStrCpy(Lang, Language); } Status = CompName2->GetControllerName(CompName2, DeviceHandle, NULL, Lang, &DeviceNameToReturn); @@ -683,38 +730,95 @@ EfiShellGetDeviceName( if (HandleList != NULL) { FreePool(HandleList); } - if (DeviceNameToReturn != NULL){ - ASSERT(BestDeviceName == NULL); - StrnCatGrow(BestDeviceName, NULL, DeviceNameToReturn, 0); - return (EFI_SUCCESS); + + // + // Now check the parent controller using this as the child. + // + if (DeviceNameToReturn == NULL){ + PARSE_HANDLE_DATABASE_PARENTS(DeviceHandle, &ParentControllerCount, &ParentControllerBuffer); + for (LoopVar = 0 ; LoopVar < ParentControllerCount ; LoopVar++) { + PARSE_HANDLE_DATABASE_UEFI_DRIVERS(ParentControllerBuffer[LoopVar], &ParentDriverCount, &ParentDriverBuffer); + for (HandleCount = 0 ; HandleCount < ParentDriverCount ; HandleCount++) { + // + // try using that driver's component name with controller and our driver as the child. + // + Status = gBS->OpenProtocol( + ParentDriverBuffer[HandleCount], + &gEfiComponentName2ProtocolGuid, + (VOID**)&CompName2, + gImageHandle, + NULL, + EFI_OPEN_PROTOCOL_GET_PROTOCOL); + if (EFI_ERROR(Status)) { + Status = gBS->OpenProtocol( + ParentDriverBuffer[HandleCount], + &gEfiComponentNameProtocolGuid, + (VOID**)&CompName2, + gImageHandle, + NULL, + EFI_OPEN_PROTOCOL_GET_PROTOCOL); + } + + if (EFI_ERROR(Status)) { + continue; + } + if (Language == NULL) { + Lang = AllocateZeroPool(AsciiStrSize(CompName2->SupportedLanguages)); + if (Lang == NULL) { + return (EFI_OUT_OF_RESOURCES); + } + AsciiStrCpy(Lang, CompName2->SupportedLanguages); + TempChar = AsciiStrStr(Lang, ";"); + if (TempChar != NULL){ + *TempChar = CHAR_NULL; + } + } else { + Lang = AllocateZeroPool(AsciiStrSize(Language)); + if (Lang == NULL) { + return (EFI_OUT_OF_RESOURCES); + } + AsciiStrCpy(Lang, Language); + } + Status = CompName2->GetControllerName(CompName2, ParentControllerBuffer[LoopVar], DeviceHandle, Lang, &DeviceNameToReturn); + FreePool(Lang); + Lang = NULL; + if (!EFI_ERROR(Status) && DeviceNameToReturn != NULL) { + break; + } + + + + } + SHELL_FREE_NON_NULL(ParentDriverBuffer); + if (!EFI_ERROR(Status) && DeviceNameToReturn != NULL) { + break; + } + } + SHELL_FREE_NON_NULL(ParentControllerBuffer); } // // dont return on fail since we will try device path if that bit is on // + if (DeviceNameToReturn != NULL){ + ASSERT(BestDeviceName != NULL); + StrnCatGrow(BestDeviceName, NULL, DeviceNameToReturn, 0); + return (EFI_SUCCESS); + } } if ((Flags & EFI_DEVICE_NAME_USE_DEVICE_PATH) != 0) { - Status = gBS->LocateProtocol( - &gEfiDevicePathToTextProtocolGuid, + Status = gBS->OpenProtocol( + DeviceHandle, + &gEfiDevicePathProtocolGuid, + (VOID**)&DevicePath, + gImageHandle, NULL, - (VOID**)&DevicePathToText); - // - // we now have the device path to text protocol - // + EFI_OPEN_PROTOCOL_GET_PROTOCOL); if (!EFI_ERROR(Status)) { - Status = gBS->OpenProtocol( - DeviceHandle, - &gEfiDevicePathProtocolGuid, - (VOID**)&DevicePath, - gImageHandle, - NULL, - EFI_OPEN_PROTOCOL_GET_PROTOCOL); - if (!EFI_ERROR(Status)) { - // - // use device path to text on the device path - // - *BestDeviceName = DevicePathToText->ConvertDevicePathToText(DevicePath, TRUE, TRUE); - return (EFI_SUCCESS); - } + // + // use device path to text on the device path + // + *BestDeviceName = ConvertDevicePathToText(DevicePath, TRUE, TRUE); + return (EFI_SUCCESS); } } // @@ -795,6 +899,7 @@ EfiShellOpenRootByHandle( could not be opened. @retval EFI_VOLUME_CORRUPTED The data structures in the volume were corrupted. @retval EFI_DEVICE_ERROR The device had an error + @retval EFI_INVALID_PARAMETER FileHandle is NULL. **/ EFI_STATUS EFIAPI @@ -806,6 +911,10 @@ EfiShellOpenRoot( EFI_STATUS Status; EFI_HANDLE Handle; + if (FileHandle == NULL) { + return (EFI_INVALID_PARAMETER); + } + // // find the handle of the device with that device handle and the file system // @@ -867,110 +976,121 @@ InternalOpenFileDevicePath( SHELL_FILE_HANDLE ShellHandle; EFI_FILE_PROTOCOL *Handle1; EFI_FILE_PROTOCOL *Handle2; - EFI_DEVICE_PATH_PROTOCOL *DpCopy; + FILEPATH_DEVICE_PATH *AlignedNode; - ASSERT(FileHandle != NULL); - *FileHandle = NULL; - Handle1 = NULL; - DpCopy = DevicePath; + if (FileHandle == NULL) { + return (EFI_INVALID_PARAMETER); + } + *FileHandle = NULL; + Handle1 = NULL; + Handle2 = NULL; + Handle = NULL; + ShellHandle = NULL; + FilePathNode = NULL; + AlignedNode = NULL; Status = EfiShellOpenRoot(DevicePath, &ShellHandle); if (!EFI_ERROR(Status)) { Handle1 = ConvertShellHandleToEfiFileProtocol(ShellHandle); - // - // chop off the begining part before the file system part... - // - ///@todo BlockIo? - Status = gBS->LocateDevicePath(&gEfiSimpleFileSystemProtocolGuid, - &DevicePath, - &Handle); - if (!EFI_ERROR(Status)) { + if (Handle1 != NULL) { // - // To access as a file system, the file path should only - // contain file path components. Follow the file path nodes - // and find the target file + // chop off the begining part before the file system part... // - for ( FilePathNode = (FILEPATH_DEVICE_PATH *)DevicePath - ; !IsDevicePathEnd (&FilePathNode->Header) - ; FilePathNode = (FILEPATH_DEVICE_PATH *) NextDevicePathNode (&FilePathNode->Header) - ){ - // - // For file system access each node should be a file path component - // - if (DevicePathType (&FilePathNode->Header) != MEDIA_DEVICE_PATH || - DevicePathSubType (&FilePathNode->Header) != MEDIA_FILEPATH_DP - ) { - Status = EFI_UNSUPPORTED; - break; - } - - // - // Open this file path node - // - Handle2 = Handle1; - Handle1 = NULL; - + ///@todo BlockIo? + Status = gBS->LocateDevicePath(&gEfiSimpleFileSystemProtocolGuid, + &DevicePath, + &Handle); + if (!EFI_ERROR(Status)) { // - // if this is the last node in the DevicePath always create (if that was requested). + // To access as a file system, the file path should only + // contain file path components. Follow the file path nodes + // and find the target file // - if (IsDevicePathEnd ((NextDevicePathNode (&FilePathNode->Header)))) { - Status = Handle2->Open ( - Handle2, - &Handle1, - FilePathNode->PathName, - OpenMode, - Attributes - ); - } else { - + for ( FilePathNode = (FILEPATH_DEVICE_PATH *)DevicePath + ; !IsDevicePathEnd (&FilePathNode->Header) + ; FilePathNode = (FILEPATH_DEVICE_PATH *) NextDevicePathNode (&FilePathNode->Header) + ){ + SHELL_FREE_NON_NULL(AlignedNode); + AlignedNode = AllocateCopyPool (DevicePathNodeLength(FilePathNode), FilePathNode); // - // This is not the last node and we dont want to 'create' existing - // directory entries... + // For file system access each node should be a file path component // + if (DevicePathType (&FilePathNode->Header) != MEDIA_DEVICE_PATH || + DevicePathSubType (&FilePathNode->Header) != MEDIA_FILEPATH_DP + ) { + Status = EFI_UNSUPPORTED; + break; + } // - // open without letting it create - // prevents error on existing files/directories + // Open this file path node // - Status = Handle2->Open ( - Handle2, - &Handle1, - FilePathNode->PathName, - OpenMode &~EFI_FILE_MODE_CREATE, - Attributes - ); + Handle2 = Handle1; + Handle1 = NULL; + // - // if above failed now open and create the 'item' - // if OpenMode EFI_FILE_MODE_CREATE bit was on (but disabled above) + // if this is the last node in the DevicePath always create (if that was requested). // - if ((EFI_ERROR (Status)) && ((OpenMode & EFI_FILE_MODE_CREATE) != 0)) { + if (IsDevicePathEnd ((NextDevicePathNode (&FilePathNode->Header)))) { Status = Handle2->Open ( Handle2, &Handle1, - FilePathNode->PathName, + AlignedNode->PathName, OpenMode, Attributes ); + } else { + + // + // This is not the last node and we dont want to 'create' existing + // directory entries... + // + + // + // open without letting it create + // prevents error on existing files/directories + // + Status = Handle2->Open ( + Handle2, + &Handle1, + AlignedNode->PathName, + OpenMode &~EFI_FILE_MODE_CREATE, + Attributes + ); + // + // if above failed now open and create the 'item' + // if OpenMode EFI_FILE_MODE_CREATE bit was on (but disabled above) + // + if ((EFI_ERROR (Status)) && ((OpenMode & EFI_FILE_MODE_CREATE) != 0)) { + Status = Handle2->Open ( + Handle2, + &Handle1, + AlignedNode->PathName, + OpenMode, + Attributes + ); + } } - } - // - // Close the last node - // - Handle2->Close (Handle2); + // + // Close the last node + // + ShellInfoObject.NewEfiShellProtocol->CloseFile (Handle2); - // - // If there's been an error, stop - // - if (EFI_ERROR (Status)) { - break; - } - } // for loop + // + // If there's been an error, stop + // + if (EFI_ERROR (Status)) { + break; + } + } // for loop + } } } + SHELL_FREE_NON_NULL(AlignedNode); if (EFI_ERROR(Status)) { if (Handle1 != NULL) { - Handle1->Close(Handle1); + ShellInfoObject.NewEfiShellProtocol->CloseFile(Handle1); } } else { *FileHandle = ConvertEfiFileProtocolToShellHandle(Handle1, ShellFileHandleGetPath(ShellHandle)); @@ -1445,18 +1565,18 @@ EfiShellExecute( DevPath = AppendDevicePath (ShellInfoObject.ImageDevPath, ShellInfoObject.FileDevPath); DEBUG_CODE_BEGIN(); - Temp = gDevPathToText->ConvertDevicePathToText(ShellInfoObject.FileDevPath, TRUE, TRUE); + Temp = ConvertDevicePathToText(ShellInfoObject.FileDevPath, TRUE, TRUE); FreePool(Temp); - Temp = gDevPathToText->ConvertDevicePathToText(ShellInfoObject.ImageDevPath, TRUE, TRUE); + Temp = ConvertDevicePathToText(ShellInfoObject.ImageDevPath, TRUE, TRUE); FreePool(Temp); - Temp = gDevPathToText->ConvertDevicePathToText(DevPath, TRUE, TRUE); + Temp = ConvertDevicePathToText(DevPath, TRUE, TRUE); FreePool(Temp); DEBUG_CODE_END(); Temp = NULL; Size = 0; ASSERT((Temp == NULL && Size == 0) || (Temp != NULL)); - StrnCatGrow(&Temp, &Size, L"Shell.efi ", 0); + StrnCatGrow(&Temp, &Size, L"Shell.efi -_exit ", 0); StrnCatGrow(&Temp, &Size, CommandLine, 0); Status = InternalShellExecuteDevicePath( @@ -1596,14 +1716,14 @@ InternalDuplicateShellFileInfo( { EFI_SHELL_FILE_INFO *NewNode; - NewNode = AllocatePool(sizeof(EFI_SHELL_FILE_INFO)); + NewNode = AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO)); if (NewNode == NULL) { return (NULL); } NewNode->FullName = AllocateZeroPool(StrSize(Node->FullName)); NewNode->FileName = AllocateZeroPool(StrSize(Node->FileName)); - NewNode->Info = AllocatePool((UINTN)Node->Info->Size); + NewNode->Info = AllocateZeroPool((UINTN)Node->Info->Size); if ( NewNode->FullName == NULL || NewNode->FileName == NULL || NewNode->Info == NULL @@ -1658,7 +1778,7 @@ CreateAndPopulateShellFileInfo( if (ShellFileListItem == NULL) { return (NULL); } - if (Info != NULL) { + if (Info != NULL && Info->Size != 0) { ShellFileListItem->Info = AllocateZeroPool((UINTN)Info->Size); if (ShellFileListItem->Info == NULL) { FreePool(ShellFileListItem); @@ -1749,6 +1869,9 @@ EfiShellFindFilesInDir( TempString = NULL; Size = 0; TempString = StrnCatGrow(&TempString, &Size, ShellFileHandleGetPath(FileDirHandle), 0); + if (TempString == NULL) { + return (EFI_OUT_OF_RESOURCES); + } TempSpot = StrStr(TempString, L";"); if (TempSpot != NULL) { @@ -1756,6 +1879,9 @@ EfiShellFindFilesInDir( } TempString = StrnCatGrow(&TempString, &Size, BasePath, 0); + if (TempString == NULL) { + return (EFI_OUT_OF_RESOURCES); + } BasePath = TempString; } @@ -1806,8 +1932,8 @@ EfiShellFindFilesInDir( /** Updates a file name to be preceeded by the mapped drive name - @param[in] BasePath the Mapped drive name to prepend - @param[in,out] Path pointer to pointer to the file name to update. + @param[in] BasePath the Mapped drive name to prepend + @param[in, out] Path pointer to pointer to the file name to update. @retval EFI_SUCCESS @retval EFI_OUT_OF_RESOURCES @@ -1859,19 +1985,15 @@ UpdateFileName( If FileHandle is a file and matches all of the remaining Pattern (which would be on its last node), then add a EFI_SHELL_FILE_INFO object for this file to fileList. - if FileList is NULL, then ASSERT - if FilePattern is NULL, then ASSERT - if UnicodeCollation is NULL, then ASSERT - if FileHandle is NULL, then ASSERT - Upon a EFI_SUCCESS return fromt he function any the caller is responsible to call FreeFileList with FileList. - @param[in] FilePattern The FilePattern to check against. - @param[in] UnicodeCollation The pointer to EFI_UNICODE_COLLATION_PROTOCOL structure - @param[in] FileHandle The FileHandle to start with - @param[in,out] FileList pointer to pointer to list of found files. - @param[in] ParentNode The node for the parent. Same file as identified by HANDLE. + @param[in] FilePattern The FilePattern to check against. + @param[in] UnicodeCollation The pointer to EFI_UNICODE_COLLATION_PROTOCOL structure + @param[in] FileHandle The FileHandle to start with + @param[in, out] FileList pointer to pointer to list of found files. + @param[in] ParentNode The node for the parent. Same file as identified by HANDLE. + @param[in] MapName The file system name this file is on. @retval EFI_SUCCESS all files were found and the FileList contains a list. @retval EFI_NOT_FOUND no files were found @@ -1884,7 +2006,8 @@ ShellSearchHandle( IN EFI_UNICODE_COLLATION_PROTOCOL *UnicodeCollation, IN SHELL_FILE_HANDLE FileHandle, IN OUT EFI_SHELL_FILE_INFO **FileList, - IN CONST EFI_SHELL_FILE_INFO *ParentNode OPTIONAL + IN CONST EFI_SHELL_FILE_INFO *ParentNode OPTIONAL, + IN CONST CHAR16 *MapName ) { EFI_STATUS Status; @@ -1894,6 +2017,8 @@ ShellSearchHandle( EFI_SHELL_FILE_INFO *ShellInfoNode; EFI_SHELL_FILE_INFO *NewShellNode; BOOLEAN Directory; + CHAR16 *NewFullName; + UINTN Size; if ( FilePattern == NULL || UnicodeCollation == NULL @@ -1931,7 +2056,7 @@ ShellSearchHandle( } else { NewShellNode->Handle = NULL; if (*FileList == NULL) { - *FileList = AllocatePool(sizeof(EFI_SHELL_FILE_INFO)); + *FileList = AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO)); InitializeListHead(&((*FileList)->Link)); } @@ -1957,7 +2082,20 @@ ShellSearchHandle( ; ShellInfoNode = (EFI_SHELL_FILE_INFO*)GetNextNode(&ShellInfo->Link, &ShellInfoNode->Link) ){ if (UnicodeCollation->MetaiMatch(UnicodeCollation, (CHAR16*)ShellInfoNode->FileName, CurrentFilePattern)){ - if (Directory){ + if (ShellInfoNode->FullName != NULL && StrStr(ShellInfoNode->FullName, L":") == NULL) { + Size = StrSize(ShellInfoNode->FullName); + Size += StrSize(MapName) + sizeof(CHAR16); + NewFullName = AllocateZeroPool(Size); + if (NewFullName == NULL) { + Status = EFI_OUT_OF_RESOURCES; + } else { + StrCpy(NewFullName, MapName); + StrCat(NewFullName, ShellInfoNode->FullName+1); + FreePool((VOID*)ShellInfoNode->FullName); + ShellInfoNode->FullName = NewFullName; + } + } + if (Directory && !EFI_ERROR(Status) && ShellInfoNode->FullName != NULL && ShellInfoNode->FileName != NULL){ // // should be a directory // @@ -1971,7 +2109,6 @@ ShellSearchHandle( // // // - ASSERT_EFI_ERROR(Status); if (EFI_ERROR(Status)) { break; } @@ -1983,9 +2120,9 @@ ShellSearchHandle( // // recurse with the next part of the pattern // - Status = ShellSearchHandle(NextFilePatternStart, UnicodeCollation, ShellInfoNode->Handle, FileList, ShellInfoNode); + Status = ShellSearchHandle(NextFilePatternStart, UnicodeCollation, ShellInfoNode->Handle, FileList, ShellInfoNode, MapName); } - } else { + } else if (!EFI_ERROR(Status)) { // // should be a file // @@ -1999,7 +2136,7 @@ ShellSearchHandle( Status = EFI_OUT_OF_RESOURCES; } if (*FileList == NULL) { - *FileList = AllocatePool(sizeof(EFI_SHELL_FILE_INFO)); + *FileList = AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO)); InitializeListHead(&((*FileList)->Link)); } @@ -2076,21 +2213,22 @@ EfiShellFindFiles( RootDevicePath = NULL; RootFileHandle = NULL; MapName = NULL; - PatternCopy = AllocatePool(StrSize(FilePattern)); + PatternCopy = AllocateZeroPool(StrSize(FilePattern)); if (PatternCopy == NULL) { return (EFI_OUT_OF_RESOURCES); } StrCpy(PatternCopy, FilePattern); - PatternCopy = CleanPath(PatternCopy); + PatternCopy = PathCleanUpDirectories(PatternCopy); Count = StrStr(PatternCopy, L":") - PatternCopy; Count += 2; ASSERT(MapName == NULL); MapName = StrnCatGrow(&MapName, NULL, PatternCopy, Count); - - if (!EFI_ERROR(Status)) { + if (MapName == NULL) { + Status = EFI_OUT_OF_RESOURCES; + } else { RootDevicePath = EfiShellGetDevicePathFromFilePath(PatternCopy); if (RootDevicePath == NULL) { Status = EFI_INVALID_PARAMETER; @@ -2101,18 +2239,14 @@ EfiShellFindFiles( ; *PatternCurrentLocation != ':' ; PatternCurrentLocation++); PatternCurrentLocation++; - Status = ShellSearchHandle(PatternCurrentLocation, gUnicodeCollation, RootFileHandle, FileList, NULL); + Status = ShellSearchHandle(PatternCurrentLocation, gUnicodeCollation, RootFileHandle, FileList, NULL, MapName); } FreePool(RootDevicePath); } } - if (PatternCopy != NULL) { - FreePool(PatternCopy); - } - if (MapName != NULL) { - FreePool(MapName); - } + SHELL_FREE_NON_NULL(PatternCopy); + SHELL_FREE_NON_NULL(MapName); return(Status); } @@ -2145,17 +2279,19 @@ EfiShellOpenFileList( CHAR16 *Path2; UINTN Path2Size; CONST CHAR16 *CurDir; + BOOLEAN Found; - ShellCommandCleanPath(Path); + PathCleanUpDirectories(Path); Path2Size = 0; Path2 = NULL; - ASSERT(FileList != NULL); - ASSERT(*FileList != NULL); + if (FileList == NULL || *FileList == NULL) { + return (EFI_INVALID_PARAMETER); + } if (*Path == L'.' && *(Path+1) == L'\\') { - Path++; + Path+=2; } // @@ -2167,6 +2303,7 @@ EfiShellOpenFileList( StrnCatGrow(&Path2, &Path2Size, CurDir, 0); if (*Path == L'\\') { Path++; + while (PathRemoveLastItem(Path2)) ; } ASSERT((Path2 == NULL && Path2Size == 0) || (Path2 != NULL)); StrnCatGrow(&Path2, &Path2Size, Path, 0); @@ -2175,7 +2312,7 @@ EfiShellOpenFileList( StrnCatGrow(&Path2, NULL, Path, 0); } - CleanPath (Path2); + PathCleanUpDirectories (Path2); // // do the search @@ -2188,6 +2325,7 @@ EfiShellOpenFileList( return (Status); } + Found = FALSE; // // We had no errors so open all the files (that are not already opened...) // @@ -2197,9 +2335,13 @@ EfiShellOpenFileList( ){ if (ShellFileListItem->Status == 0 && ShellFileListItem->Handle == NULL) { ShellFileListItem->Status = EfiShellOpenFileByName (ShellFileListItem->FullName, &ShellFileListItem->Handle, OpenMode); + Found = TRUE; } } + if (!Found) { + return (EFI_NOT_FOUND); + } return(EFI_SUCCESS); } @@ -2268,6 +2410,12 @@ EfiShellGetEnv( Size += 2*sizeof(CHAR16); Buffer = AllocateZeroPool(Size); + if (Buffer == NULL) { + if (!IsListEmpty (&List)) { + FreeEnvironmentVariableList(&List); + } + return (NULL); + } CurrentWriteLocation = (CHAR16*)Buffer; for ( Node = (ENV_VAR_LIST*)GetFirstNode(&List) @@ -2282,7 +2430,9 @@ EfiShellGetEnv( // // Free the list... // - FreeEnvironmentVariableList(&List); + if (!IsListEmpty (&List)) { + FreeEnvironmentVariableList(&List); + } } else { // // We are doing a specific environment variable @@ -2504,7 +2654,7 @@ EfiShellSetCurDir( TempString = NULL; DirectoryName = NULL; - if (FileSystem == NULL && Dir == NULL) { + if ((FileSystem == NULL && Dir == NULL) || Dir == NULL) { return (EFI_INVALID_PARAMETER); } @@ -2515,7 +2665,7 @@ EfiShellSetCurDir( DirectoryName = StrnCatGrow(&DirectoryName, NULL, Dir, 0); ASSERT(DirectoryName != NULL); - CleanPath(DirectoryName); + PathCleanUpDirectories(DirectoryName); if (FileSystem == NULL) { // @@ -2597,7 +2747,7 @@ EfiShellSetCurDir( MapListItem->CurrentDirectoryPath = StrnCatGrow(&MapListItem->CurrentDirectoryPath, &Size, L"\\", 0); ASSERT((MapListItem->CurrentDirectoryPath == NULL && Size == 0) || (MapListItem->CurrentDirectoryPath != NULL)); MapListItem->CurrentDirectoryPath = StrnCatGrow(&MapListItem->CurrentDirectoryPath, &Size, DirectoryName, 0); - if (MapListItem->CurrentDirectoryPath[StrLen(MapListItem->CurrentDirectoryPath)-1] != L'\\') { + if (MapListItem->CurrentDirectoryPath != NULL && MapListItem->CurrentDirectoryPath[StrLen(MapListItem->CurrentDirectoryPath)-1] != L'\\') { ASSERT((MapListItem->CurrentDirectoryPath == NULL && Size == 0) || (MapListItem->CurrentDirectoryPath != NULL)); MapListItem->CurrentDirectoryPath = StrnCatGrow(&MapListItem->CurrentDirectoryPath, &Size, L"\\", 0); } @@ -2722,7 +2872,6 @@ InternalEfiShellGetListAlias( UINTN NameSize; CHAR16 *RetVal; UINTN RetSize; - CHAR16 *Alias; Status = gRT->QueryVariableInfo(EFI_VARIABLE_NON_VOLATILE|EFI_VARIABLE_BOOTSERVICE_ACCESS, &MaxStorSize, &RemStorSize, &MaxVarSize); ASSERT_EFI_ERROR(Status); @@ -2731,6 +2880,10 @@ InternalEfiShellGetListAlias( RetSize = 0; RetVal = NULL; + if (VariableName == NULL) { + return (NULL); + } + VariableName[0] = CHAR_NULL; while (TRUE) { @@ -2744,7 +2897,6 @@ InternalEfiShellGetListAlias( break; } if (CompareGuid(&Guid, &gShellAliasGuid)){ - Alias = GetVariable(VariableName, &gShellAliasGuid); ASSERT((RetVal == NULL && RetSize == 0) || (RetVal != NULL)); RetVal = StrnCatGrow(&RetVal, &RetSize, VariableName, 0); RetVal = StrnCatGrow(&RetVal, &RetSize, L";", 0); @@ -2958,7 +3110,7 @@ EFI_SHELL_PROTOCOL mShellProtocol = { This must be removed via calling CleanUpShellProtocol(). - @param[in,out] NewShell The pointer to the pointer to the structure + @param[in, out] NewShell The pointer to the pointer to the structure to install. @retval EFI_SUCCESS The operation was successful. @@ -2976,23 +3128,26 @@ CreatePopulateInstallShellProtocol ( UINTN HandleCounter; SHELL_PROTOCOL_HANDLE_LIST *OldProtocolNode; + if (NewShell == NULL) { + return (EFI_INVALID_PARAMETER); + } + BufferSize = 0; Buffer = NULL; OldProtocolNode = NULL; InitializeListHead(&ShellInfoObject.OldShellList.Link); - ASSERT(NewShell != NULL); - // // Initialize EfiShellProtocol object... // - *NewShell = &mShellProtocol; Status = gBS->CreateEvent(0, 0, NULL, NULL, &mShellProtocol.ExecutionBreak); - ASSERT_EFI_ERROR(Status); + if (EFI_ERROR(Status)) { + return (Status); + } // // Get the size of the buffer we need. @@ -3007,13 +3162,18 @@ CreatePopulateInstallShellProtocol ( // Allocate and recall with buffer of correct size // Buffer = AllocateZeroPool(BufferSize); - ASSERT(Buffer != NULL); + if (Buffer == NULL) { + return (EFI_OUT_OF_RESOURCES); + } Status = gBS->LocateHandle(ByProtocol, &gEfiShellProtocolGuid, NULL, &BufferSize, Buffer); - ASSERT_EFI_ERROR(Status); + if (EFI_ERROR(Status)) { + FreePool(Buffer); + return (Status); + } // // now overwrite each of them, but save the info to restore when we end. // @@ -3036,7 +3196,7 @@ CreatePopulateInstallShellProtocol ( OldProtocolNode->Handle, &gEfiShellProtocolGuid, OldProtocolNode->Interface, - (VOID*)(*NewShell)); + (VOID*)(&mShellProtocol)); if (!EFI_ERROR(Status)) { // // we reinstalled sucessfully. log this so we can reverse it later. @@ -3059,7 +3219,7 @@ CreatePopulateInstallShellProtocol ( &gImageHandle, &gEfiShellProtocolGuid, EFI_NATIVE_INTERFACE, - (VOID*)(*NewShell)); + (VOID*)(&mShellProtocol)); } if (PcdGetBool(PcdShellSupportOldProtocols)){ @@ -3067,16 +3227,19 @@ CreatePopulateInstallShellProtocol ( ///@todo do we need to support ShellEnvironment (not ShellEnvironment2) also? } + if (!EFI_ERROR(Status)) { + *NewShell = &mShellProtocol; + } return (Status); } /** - Opposite of CreatePopulateInstallShellProtocol. + Opposite of CreatePopulateInstallShellProtocol. Free all memory and restore the system to the state it was in before calling CreatePopulateInstallShellProtocol. - @param[in,out] NewShell The pointer to the new shell protocol structure. + @param[in, out] NewShell The pointer to the new shell protocol structure. @retval EFI_SUCCESS The operation was successful. **/ @@ -3086,8 +3249,9 @@ CleanUpShellProtocol ( IN OUT EFI_SHELL_PROTOCOL *NewShell ) { - EFI_STATUS Status; - SHELL_PROTOCOL_HANDLE_LIST *Node2; + EFI_STATUS Status; + SHELL_PROTOCOL_HANDLE_LIST *Node2; + EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *SimpleEx; // // if we need to restore old protocols... @@ -3102,7 +3266,6 @@ CleanUpShellProtocol ( &gEfiShellProtocolGuid, NewShell, Node2->Interface); - ASSERT_EFI_ERROR(Status); FreePool(Node2); } } else { @@ -3112,11 +3275,140 @@ CleanUpShellProtocol ( Status = gBS->UninstallProtocolInterface(gImageHandle, &gEfiShellProtocolGuid, NewShell); - ASSERT_EFI_ERROR(Status); } Status = gBS->CloseEvent(NewShell->ExecutionBreak); + NewShell->ExecutionBreak = NULL; + Status = gBS->OpenProtocol( + gST->ConsoleInHandle, + &gEfiSimpleTextInputExProtocolGuid, + (VOID**)&SimpleEx, + gImageHandle, + NULL, + EFI_OPEN_PROTOCOL_GET_PROTOCOL); + + if (!EFI_ERROR (Status)) { + Status = SimpleEx->UnregisterKeyNotify(SimpleEx, ShellInfoObject.CtrlCNotifyHandle1); + Status = SimpleEx->UnregisterKeyNotify(SimpleEx, ShellInfoObject.CtrlCNotifyHandle2); + Status = SimpleEx->UnregisterKeyNotify(SimpleEx, ShellInfoObject.CtrlCNotifyHandle3); + Status = SimpleEx->UnregisterKeyNotify(SimpleEx, ShellInfoObject.CtrlCNotifyHandle4); + Status = SimpleEx->UnregisterKeyNotify(SimpleEx, ShellInfoObject.CtrlSNotifyHandle1); + Status = SimpleEx->UnregisterKeyNotify(SimpleEx, ShellInfoObject.CtrlSNotifyHandle2); + Status = SimpleEx->UnregisterKeyNotify(SimpleEx, ShellInfoObject.CtrlSNotifyHandle3); + Status = SimpleEx->UnregisterKeyNotify(SimpleEx, ShellInfoObject.CtrlSNotifyHandle4); + } return (Status); } +/** + Notification function for keystrokes. + + @param[in] KeyData The key that was pressed. + + @retval EFI_SUCCESS The operation was successful. +**/ +EFI_STATUS +EFIAPI +NotificationFunction( + IN EFI_KEY_DATA *KeyData + ) +{ + EFI_INPUT_KEY Key; + if ( ((KeyData->Key.UnicodeChar == L'c') && + (KeyData->KeyState.KeyShiftState == (EFI_SHIFT_STATE_VALID|EFI_LEFT_CONTROL_PRESSED) || KeyData->KeyState.KeyShiftState == (EFI_SHIFT_STATE_VALID|EFI_RIGHT_CONTROL_PRESSED))) || + (KeyData->Key.UnicodeChar == 3) + ){ + if (ShellInfoObject.NewEfiShellProtocol->ExecutionBreak == NULL) { + return (EFI_UNSUPPORTED); + } + return (gBS->SignalEvent(ShellInfoObject.NewEfiShellProtocol->ExecutionBreak)); + } else if ((KeyData->Key.UnicodeChar == L's') && + (KeyData->KeyState.KeyShiftState == (EFI_SHIFT_STATE_VALID|EFI_LEFT_CONTROL_PRESSED) || KeyData->KeyState.KeyShiftState == (EFI_SHIFT_STATE_VALID|EFI_RIGHT_CONTROL_PRESSED)) + ){ + ShellInfoObject.HaltOutput = TRUE; + + // + // Make sure that there are no pending keystrokes to pervent the pause. + // + gST->ConIn->Reset(gST->ConIn, FALSE); + while (gST->ConIn->ReadKeyStroke (gST->ConIn, &Key)==EFI_SUCCESS); + } + return (EFI_SUCCESS); +} + +/** + Function to start monitoring for CTRL-C using SimpleTextInputEx. This + feature's enabled state was not known when the shell initially launched. + + @retval EFI_SUCCESS The feature is enabled. + @retval EFI_OUT_OF_RESOURCES There is not enough mnemory available. +**/ +EFI_STATUS +EFIAPI +InernalEfiShellStartMonitor( + VOID + ) +{ + EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *SimpleEx; + EFI_KEY_DATA KeyData; + EFI_STATUS Status; + + Status = gBS->OpenProtocol( + gST->ConsoleInHandle, + &gEfiSimpleTextInputExProtocolGuid, + (VOID**)&SimpleEx, + gImageHandle, + NULL, + EFI_OPEN_PROTOCOL_GET_PROTOCOL); + if (EFI_ERROR(Status)) { + ShellPrintHiiEx( + -1, + -1, + NULL, + STRING_TOKEN (STR_SHELL_NO_IN_EX), + ShellInfoObject.HiiHandle); + return (EFI_SUCCESS); + } + + if (ShellInfoObject.NewEfiShellProtocol->ExecutionBreak == NULL) { + return (EFI_UNSUPPORTED); + } + + KeyData.KeyState.KeyToggleState = 0; + KeyData.Key.ScanCode = 0; + KeyData.KeyState.KeyShiftState = EFI_SHIFT_STATE_VALID|EFI_LEFT_CONTROL_PRESSED; + KeyData.Key.UnicodeChar = L'c'; + Status = SimpleEx->RegisterKeyNotify( + SimpleEx, + &KeyData, + NotificationFunction, + &ShellInfoObject.CtrlCNotifyHandle1); + + KeyData.KeyState.KeyShiftState = EFI_SHIFT_STATE_VALID|EFI_RIGHT_CONTROL_PRESSED; + if (!EFI_ERROR(Status)) { + Status = SimpleEx->RegisterKeyNotify( + SimpleEx, + &KeyData, + NotificationFunction, + &ShellInfoObject.CtrlCNotifyHandle2); + } + KeyData.KeyState.KeyShiftState = EFI_SHIFT_STATE_VALID|EFI_LEFT_CONTROL_PRESSED; + KeyData.Key.UnicodeChar = 3; + if (!EFI_ERROR(Status)) { + Status = SimpleEx->RegisterKeyNotify( + SimpleEx, + &KeyData, + NotificationFunction, + &ShellInfoObject.CtrlCNotifyHandle3); + } + KeyData.KeyState.KeyShiftState = EFI_SHIFT_STATE_VALID|EFI_RIGHT_CONTROL_PRESSED; + if (!EFI_ERROR(Status)) { + Status = SimpleEx->RegisterKeyNotify( + SimpleEx, + &KeyData, + NotificationFunction, + &ShellInfoObject.CtrlCNotifyHandle4); + } + return (Status); +}