X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=ShellPkg%2FApplication%2FShell%2FShell.c;h=aa4dfd37ab6503c4c12bbaeb6e3dcd8d2917a80e;hb=ea4fe84fe5d1ac7bb9a47d2e8b36cef4a26622be;hp=3401bc064446abb8bb2520e6061e451cabc2fc24;hpb=a405b86d274d32b92f69842bfb9a1ab143128f57;p=mirror_edk2.git diff --git a/ShellPkg/Application/Shell/Shell.c b/ShellPkg/Application/Shell/Shell.c index 3401bc0644..aa4dfd37ab 100644 --- a/ShellPkg/Application/Shell/Shell.c +++ b/ShellPkg/Application/Shell/Shell.c @@ -1,7 +1,7 @@ /** @file This is THE shell (application) - Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.
+ Copyright (c) 2009 - 2012, 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 @@ -52,10 +52,92 @@ SHELL_INFO ShellInfoObject = { NULL, {0,0,NULL,NULL}, {0,0}, + NULL, + NULL, + NULL, + NULL, + FALSE }; STATIC CONST CHAR16 mScriptExtension[] = L".NSH"; STATIC CONST CHAR16 mExecutableExtensions[] = L".NSH;.EFI"; +STATIC CONST CHAR16 mStartupScript[] = L"startup.nsh"; + +/** + Function to start monitoring for CTRL-S 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 +InternalEfiShellStartCtrlSMonitor( + 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); + } + + KeyData.KeyState.KeyToggleState = 0; + KeyData.Key.ScanCode = 0; + KeyData.KeyState.KeyShiftState = EFI_SHIFT_STATE_VALID|EFI_LEFT_CONTROL_PRESSED; + KeyData.Key.UnicodeChar = L's'; + + Status = SimpleEx->RegisterKeyNotify( + SimpleEx, + &KeyData, + NotificationFunction, + &ShellInfoObject.CtrlSNotifyHandle1); + + KeyData.KeyState.KeyShiftState = EFI_SHIFT_STATE_VALID|EFI_RIGHT_CONTROL_PRESSED; + if (!EFI_ERROR(Status)) { + Status = SimpleEx->RegisterKeyNotify( + SimpleEx, + &KeyData, + NotificationFunction, + &ShellInfoObject.CtrlSNotifyHandle2); + } + KeyData.KeyState.KeyShiftState = EFI_SHIFT_STATE_VALID|EFI_LEFT_CONTROL_PRESSED; + KeyData.Key.UnicodeChar = 19; + + if (!EFI_ERROR(Status)) { + Status = SimpleEx->RegisterKeyNotify( + SimpleEx, + &KeyData, + NotificationFunction, + &ShellInfoObject.CtrlSNotifyHandle2); + } + KeyData.KeyState.KeyShiftState = EFI_SHIFT_STATE_VALID|EFI_RIGHT_CONTROL_PRESSED; + if (!EFI_ERROR(Status)) { + Status = SimpleEx->RegisterKeyNotify( + SimpleEx, + &KeyData, + NotificationFunction, + &ShellInfoObject.CtrlSNotifyHandle2); + } + return (Status); +} + + /** The entry point for the application. @@ -74,17 +156,11 @@ UefiMain ( IN EFI_SYSTEM_TABLE *SystemTable ) { - EFI_STATUS Status; - CHAR16 *TempString; - UINTN Size; -// EFI_INPUT_KEY Key; - -// gST->ConOut->OutputString(gST->ConOut, L"ReadKeyStroke Calling\r\n"); -// -// Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key); -// -// gST->ConOut->OutputString(gST->ConOut, L"ReadKeyStroke Done\r\n"); -// gBS->Stall (1000000); + EFI_STATUS Status; + CHAR16 *TempString; + UINTN Size; + EFI_HANDLE ConInHandle; + EFI_SIMPLE_TEXT_INPUT_PROTOCOL *OldConIn; if (PcdGet8(PcdShellSupportLevel) > 3) { return (EFI_UNSUPPORTED); @@ -94,7 +170,9 @@ UefiMain ( // Clear the screen // Status = gST->ConOut->ClearScreen(gST->ConOut); - ASSERT_EFI_ERROR(Status); + if (EFI_ERROR(Status)) { + return (Status); + } // // Populate the global structure from PCDs @@ -136,178 +214,206 @@ UefiMain ( // install our console logger. This will keep a log of the output for back-browsing // Status = ConsoleLoggerInstall(ShellInfoObject.LogScreenCount, &ShellInfoObject.ConsoleInfo); - ASSERT_EFI_ERROR(Status); - - // - // Enable the cursor to be visible - // - gST->ConOut->EnableCursor (gST->ConOut, TRUE); + if (!EFI_ERROR(Status)) { + // + // Enable the cursor to be visible + // + gST->ConOut->EnableCursor (gST->ConOut, TRUE); - // - // If supporting EFI 1.1 we need to install HII protocol - // only do this if PcdShellRequireHiiPlatform == FALSE - // - // remove EFI_UNSUPPORTED check above when complete. - ///@todo add support for Framework HII + // + // If supporting EFI 1.1 we need to install HII protocol + // only do this if PcdShellRequireHiiPlatform == FALSE + // + // remove EFI_UNSUPPORTED check above when complete. + ///@todo add support for Framework HII - // - // install our (solitary) HII package - // - ShellInfoObject.HiiHandle = HiiAddPackages (&gEfiCallerIdGuid, gImageHandle, ShellStrings, NULL); - if (ShellInfoObject.HiiHandle == NULL) { - if (PcdGetBool(PcdShellSupportFrameworkHii)) { - ///@todo Add our package into Framework HII - } + // + // install our (solitary) HII package + // + ShellInfoObject.HiiHandle = HiiAddPackages (&gEfiCallerIdGuid, gImageHandle, ShellStrings, NULL); if (ShellInfoObject.HiiHandle == NULL) { - return (EFI_NOT_STARTED); + if (PcdGetBool(PcdShellSupportFrameworkHii)) { + ///@todo Add our package into Framework HII + } + if (ShellInfoObject.HiiHandle == NULL) { + return (EFI_NOT_STARTED); + } } - } - // - // create and install the EfiShellParametersProtocol - // - Status = CreatePopulateInstallShellParametersProtocol(&ShellInfoObject.NewShellParametersProtocol, &ShellInfoObject.RootShellInstance); - ASSERT_EFI_ERROR(Status); - ASSERT(ShellInfoObject.NewShellParametersProtocol != NULL); - - // - // create and install the EfiShellProtocol - // - Status = CreatePopulateInstallShellProtocol(&ShellInfoObject.NewEfiShellProtocol); - ASSERT_EFI_ERROR(Status); - ASSERT(ShellInfoObject.NewEfiShellProtocol != NULL); - - // - // Now initialize the shell library (it requires Shell Parameters protocol) - // - Status = ShellInitialize(); - ASSERT_EFI_ERROR(Status); + // + // create and install the EfiShellParametersProtocol + // + Status = CreatePopulateInstallShellParametersProtocol(&ShellInfoObject.NewShellParametersProtocol, &ShellInfoObject.RootShellInstance); + ASSERT_EFI_ERROR(Status); + ASSERT(ShellInfoObject.NewShellParametersProtocol != NULL); - Status = CommandInit(); - ASSERT_EFI_ERROR(Status); + // + // create and install the EfiShellProtocol + // + Status = CreatePopulateInstallShellProtocol(&ShellInfoObject.NewEfiShellProtocol); + ASSERT_EFI_ERROR(Status); + ASSERT(ShellInfoObject.NewEfiShellProtocol != NULL); - // - // Check the command line - // - Status = ProcessCommandLine(); + // + // Now initialize the shell library (it requires Shell Parameters protocol) + // + Status = ShellInitialize(); + ASSERT_EFI_ERROR(Status); - // - // If shell support level is >= 1 create the mappings and paths - // - if (PcdGet8(PcdShellSupportLevel) >= 1) { - Status = ShellCommandCreateInitialMappingsAndPaths(); - } + Status = CommandInit(); + ASSERT_EFI_ERROR(Status); - // - // save the device path for the loaded image and the device path for the filepath (under loaded image) - // These are where to look for the startup.nsh file - // - Status = GetDevicePathsForImageAndFile(&ShellInfoObject.ImageDevPath, &ShellInfoObject.FileDevPath); - ASSERT_EFI_ERROR(Status); + // + // Check the command line + // + Status = ProcessCommandLine(); - // - // Display the version - // - if (!ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoVersion) { - ShellPrintHiiEx ( - 0, - gST->ConOut->Mode->CursorRow, - NULL, - STRING_TOKEN (STR_VER_OUTPUT_MAIN), - ShellInfoObject.HiiHandle, - SupportLevel[PcdGet8(PcdShellSupportLevel)], - gEfiShellProtocol->MajorVersion, - gEfiShellProtocol->MinorVersion, - (gST->Hdr.Revision&0xffff0000)>>16, - (gST->Hdr.Revision&0x0000ffff), - gST->FirmwareVendor, - gST->FirmwareRevision - ); - } + // + // If shell support level is >= 1 create the mappings and paths + // + if (PcdGet8(PcdShellSupportLevel) >= 1) { + Status = ShellCommandCreateInitialMappingsAndPaths(); + } - // - // Display the mapping - // - if (PcdGet8(PcdShellSupportLevel) >= 2 && !ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoMap) { - Status = RunCommand(L"map"); + // + // save the device path for the loaded image and the device path for the filepath (under loaded image) + // These are where to look for the startup.nsh file + // + Status = GetDevicePathsForImageAndFile(&ShellInfoObject.ImageDevPath, &ShellInfoObject.FileDevPath); ASSERT_EFI_ERROR(Status); - } - // - // init all the built in alias' - // - Status = SetBuiltInAlias(); - ASSERT_EFI_ERROR(Status); + // + // Display the version + // + if (!ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoVersion) { + ShellPrintHiiEx ( + 0, + gST->ConOut->Mode->CursorRow, + NULL, + STRING_TOKEN (STR_VER_OUTPUT_MAIN), + ShellInfoObject.HiiHandle, + SupportLevel[PcdGet8(PcdShellSupportLevel)], + gEfiShellProtocol->MajorVersion, + gEfiShellProtocol->MinorVersion, + (gST->Hdr.Revision&0xffff0000)>>16, + (gST->Hdr.Revision&0x0000ffff), + gST->FirmwareVendor, + gST->FirmwareRevision + ); + } - // - // Initialize environment variables - // - if (ShellCommandGetProfileList() != NULL) { - Status = InternalEfiShellSetEnv(L"profiles", ShellCommandGetProfileList(), TRUE); + // + // Display the mapping + // + if (PcdGet8(PcdShellSupportLevel) >= 2 && !ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoMap) { + Status = RunCommand(L"map"); + ASSERT_EFI_ERROR(Status); + } + + // + // init all the built in alias' + // + Status = SetBuiltInAlias(); ASSERT_EFI_ERROR(Status); - } - Size = 100; - TempString = AllocateZeroPool(Size); + // + // Initialize environment variables + // + if (ShellCommandGetProfileList() != NULL) { + Status = InternalEfiShellSetEnv(L"profiles", ShellCommandGetProfileList(), TRUE); + ASSERT_EFI_ERROR(Status); + } - UnicodeSPrint(TempString, Size, L"%d", PcdGet8(PcdShellSupportLevel)); - Status = InternalEfiShellSetEnv(L"uefishellsupport", TempString, TRUE); - ASSERT_EFI_ERROR(Status); + Size = 100; + TempString = AllocateZeroPool(Size); - UnicodeSPrint(TempString, Size, L"%d.%d", ShellInfoObject.NewEfiShellProtocol->MajorVersion, ShellInfoObject.NewEfiShellProtocol->MinorVersion); - Status = InternalEfiShellSetEnv(L"uefishellversion", TempString, TRUE); - ASSERT_EFI_ERROR(Status); + UnicodeSPrint(TempString, Size, L"%d", PcdGet8(PcdShellSupportLevel)); + Status = InternalEfiShellSetEnv(L"uefishellsupport", TempString, TRUE); + ASSERT_EFI_ERROR(Status); - UnicodeSPrint(TempString, Size, L"%d.%d", (gST->Hdr.Revision & 0xFFFF0000) >> 16, gST->Hdr.Revision & 0x0000FFFF); - Status = InternalEfiShellSetEnv(L"uefiversion", TempString, TRUE); - ASSERT_EFI_ERROR(Status); + UnicodeSPrint(TempString, Size, L"%d.%d", ShellInfoObject.NewEfiShellProtocol->MajorVersion, ShellInfoObject.NewEfiShellProtocol->MinorVersion); + Status = InternalEfiShellSetEnv(L"uefishellversion", TempString, TRUE); + ASSERT_EFI_ERROR(Status); - FreePool(TempString); + UnicodeSPrint(TempString, Size, L"%d.%d", (gST->Hdr.Revision & 0xFFFF0000) >> 16, gST->Hdr.Revision & 0x0000FFFF); + Status = InternalEfiShellSetEnv(L"uefiversion", TempString, TRUE); + ASSERT_EFI_ERROR(Status); - if (!EFI_ERROR(Status)) { - if (!ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoInterrupt) { - // - // Set up the event for CTRL-C monitoring... - // + FreePool(TempString); - ///@todo add support for using SimpleInputEx here - // if SimpleInputEx is not available display a warning. - } + if (!EFI_ERROR(Status)) { + if (!ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoInterrupt) { + // + // Set up the event for CTRL-C monitoring... + // + Status = InernalEfiShellStartMonitor(); + } - if (!EFI_ERROR(Status) && PcdGet8(PcdShellSupportLevel) >= 1) { - // - // process the startup script or launch the called app. - // - Status = DoStartupScript(ShellInfoObject.ImageDevPath, ShellInfoObject.FileDevPath); - } + if (!EFI_ERROR(Status) && !ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoConsoleIn) { + // + // Set up the event for CTRL-S monitoring... + // + Status = InternalEfiShellStartCtrlSMonitor(); + } - if ((PcdGet8(PcdShellSupportLevel) >= 3 || PcdGetBool(PcdShellForceConsole)) && !EFI_ERROR(Status) && !ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoConsoleIn) { - // - // begin the UI waiting loop - // - do { + if (!EFI_ERROR(Status) && ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoConsoleIn) { // - // clean out all the memory allocated for CONST * return values - // between each shell prompt presentation + // close off the gST->ConIn // - if (!IsListEmpty(&ShellInfoObject.BufferToFreeList.Link)){ - FreeBufferList(&ShellInfoObject.BufferToFreeList); - } + OldConIn = gST->ConIn; + ConInHandle = gST->ConsoleInHandle; + gST->ConIn = CreateSimpleTextInOnFile((SHELL_FILE_HANDLE)&FileInterfaceNulFile, &gST->ConsoleInHandle); + } else { + OldConIn = NULL; + ConInHandle = NULL; + } + if (!EFI_ERROR(Status) && PcdGet8(PcdShellSupportLevel) >= 1) { // - // Reset page break back to default. + // process the startup script or launch the called app. // - ShellInfoObject.PageBreakEnabled = PcdGetBool(PcdShellPageBreakDefault); - ShellInfoObject.ConsoleInfo->Enabled = TRUE; - ShellInfoObject.ConsoleInfo->RowCounter = 0; + Status = DoStartupScript(ShellInfoObject.ImageDevPath, ShellInfoObject.FileDevPath); + } + if (!ShellCommandGetExit() && (PcdGet8(PcdShellSupportLevel) >= 3 || PcdGetBool(PcdShellForceConsole)) && !EFI_ERROR(Status) && !ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoConsoleIn) { // - // Display Prompt + // begin the UI waiting loop // - Status = DoShellPrompt(); - } while (!ShellCommandGetExit()); + do { + // + // clean out all the memory allocated for CONST * return values + // between each shell prompt presentation + // + if (!IsListEmpty(&ShellInfoObject.BufferToFreeList.Link)){ + FreeBufferList(&ShellInfoObject.BufferToFreeList); + } + + // + // Reset page break back to default. + // + ShellInfoObject.PageBreakEnabled = PcdGetBool(PcdShellPageBreakDefault); + ShellInfoObject.ConsoleInfo->Enabled = TRUE; + ShellInfoObject.ConsoleInfo->RowCounter = 0; + + // + // Reset the CTRL-C event (yes we ignore the return values) + // + Status = gBS->CheckEvent (ShellInfoObject.NewEfiShellProtocol->ExecutionBreak); + + // + // Display Prompt + // + Status = DoShellPrompt(); + } while (!ShellCommandGetExit()); + } + if (OldConIn != NULL && ConInHandle != NULL) { + CloseSimpleTextInOnFile (gST->ConIn); + gST->ConIn = OldConIn; + gST->ConsoleInHandle = ConInHandle; + } } } + // // uninstall protocols / free memory / etc... // @@ -315,11 +421,6 @@ UefiMain ( gBS->CloseEvent(ShellInfoObject.UserBreakTimer); DEBUG_CODE(ShellInfoObject.UserBreakTimer = NULL;); } - - if (ShellInfoObject.NewEfiShellProtocol->IsRootShell()){ - ShellInfoObject.NewEfiShellProtocol->SetEnv(L"cwd", L"", TRUE); - } - if (ShellInfoObject.ImageDevPath != NULL) { FreePool(ShellInfoObject.ImageDevPath); DEBUG_CODE(ShellInfoObject.ImageDevPath = NULL;); @@ -333,6 +434,9 @@ UefiMain ( DEBUG_CODE(ShellInfoObject.NewShellParametersProtocol = NULL;); } if (ShellInfoObject.NewEfiShellProtocol != NULL){ + if (ShellInfoObject.NewEfiShellProtocol->IsRootShell()){ + ShellInfoObject.NewEfiShellProtocol->SetEnv(L"cwd", L"", TRUE); + } CleanUpShellProtocol(ShellInfoObject.NewEfiShellProtocol); DEBUG_CODE(ShellInfoObject.NewEfiShellProtocol = NULL;); } @@ -342,7 +446,7 @@ UefiMain ( } if (!IsListEmpty(&ShellInfoObject.SplitList.Link)){ - ASSERT(FALSE); + ASSERT(FALSE); ///@todo finish this de-allocation. } if (ShellInfoObject.ShellInitSettings.FileName != NULL) { @@ -371,6 +475,9 @@ UefiMain ( DEBUG_CODE(ShellInfoObject.ConsoleInfo = NULL;); } + if (ShellCommandGetExit()) { + return ((EFI_STATUS)ShellCommandGetExitCode()); + } return (Status); } @@ -456,16 +563,14 @@ IsScriptOnlyCommand( return (FALSE); } - - /** This function will populate the 2 device path protocol parameters based on the global gImageHandle. The DevPath will point to the device path for the handle that has loaded image protocol installed on it. The FilePath will point to the device path for the file that was loaded. - @param[in,out] DevPath On a sucessful return the device path to the loaded image. - @param[in,out] FilePath On a sucessful return the device path to the file. + @param[in, out] DevPath On a sucessful return the device path to the loaded image. + @param[in, out] FilePath On a sucessful return the device path to the file. @retval EFI_SUCCESS The 2 device paths were sucessfully returned. @retval other A error from gBS->HandleProtocol. @@ -506,6 +611,11 @@ GetDevicePathsForImageAndFile ( if (!EFI_ERROR (Status)) { *DevPath = DuplicateDevicePath (ImageDevicePath); *FilePath = DuplicateDevicePath (LoadedImage->FilePath); + gBS->CloseProtocol( + LoadedImage->DeviceHandle, + &gEfiDevicePathProtocolGuid, + gImageHandle, + NULL); } gBS->CloseProtocol( gImageHandle, @@ -528,6 +638,7 @@ STATIC CONST SHELL_PARAM_ITEM mShellParamList[] = { {L"-delay", TypeValue}, {NULL, TypeMax} }; + /** Process all Uefi Shell 2.0 command line options. @@ -568,6 +679,7 @@ ProcessCommandLine( UINTN Count; UINTN LoopVar; CHAR16 *ProblemParam; + UINT64 Intermediate; Package = NULL; ProblemParam = NULL; @@ -578,7 +690,10 @@ ProcessCommandLine( Size = 0; TempConst = ShellCommandLineGetRawValue(Package, Count++); if (TempConst != NULL && StrLen(TempConst)) { - ShellInfoObject.ShellInitSettings.FileName = AllocatePool(StrSize(TempConst)); + ShellInfoObject.ShellInitSettings.FileName = AllocateZeroPool(StrSize(TempConst)); + if (ShellInfoObject.ShellInitSettings.FileName == NULL) { + return (EFI_OUT_OF_RESOURCES); + } StrCpy(ShellInfoObject.ShellInitSettings.FileName, TempConst); ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoStartup = 1; for (LoopVar = 0 ; LoopVar < gEfiShellParametersProtocol->Argc ; LoopVar++) { @@ -593,10 +708,18 @@ ProcessCommandLine( &Size, L" ", 0); + if (ShellInfoObject.ShellInitSettings.FileOptions == NULL) { + SHELL_FREE_NON_NULL(ShellInfoObject.ShellInitSettings.FileName); + return (EFI_OUT_OF_RESOURCES); + } StrnCatGrow(&ShellInfoObject.ShellInitSettings.FileOptions, &Size, gEfiShellParametersProtocol->Argv[LoopVar], 0); + if (ShellInfoObject.ShellInitSettings.FileOptions == NULL) { + SHELL_FREE_NON_NULL(ShellInfoObject.ShellInitSettings.FileName); + return (EFI_OUT_OF_RESOURCES); + } } } } @@ -621,17 +744,19 @@ ProcessCommandLine( ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoVersion = ShellCommandLineGetFlag(Package, L"-noversion"); ShellInfoObject.ShellInitSettings.BitUnion.Bits.Delay = ShellCommandLineGetFlag(Package, L"-delay"); - if (ShellInfoObject.ShellInitSettings.BitUnion.Bits.Delay) { + ShellInfoObject.ShellInitSettings.Delay = 5; + + if (ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoInterrupt) { + ShellInfoObject.ShellInitSettings.Delay = 0; + } else if (ShellInfoObject.ShellInitSettings.BitUnion.Bits.Delay) { TempConst = ShellCommandLineGetValue(Package, L"-delay"); - if (TempConst != NULL) { - ShellInfoObject.ShellInitSettings.Delay = StrDecimalToUintn (TempConst); - } else { - ShellInfoObject.ShellInitSettings.Delay = 5; + if (TempConst != NULL && *TempConst == L':') { + TempConst++; + } + if (TempConst != NULL && !EFI_ERROR(ShellConvertStringToUint64(TempConst, &Intermediate, FALSE, FALSE))) { + ShellInfoObject.ShellInitSettings.Delay = (UINTN)Intermediate; } - } else { - ShellInfoObject.ShellInitSettings.Delay = 5; } - ShellCommandLineFreeVarList(Package); return (Status); @@ -661,7 +786,9 @@ DoStartupScript( EFI_DEVICE_PATH_PROTOCOL *NewPath; EFI_DEVICE_PATH_PROTOCOL *NamePath; CHAR16 *FileStringPath; + CHAR16 *TempSpot; UINTN NewSize; + CONST CHAR16 *MapName; Key.UnicodeChar = CHAR_NULL; Key.ScanCode = 0; @@ -676,9 +803,12 @@ DoStartupScript( NewSize += StrSize(ShellInfoObject.ShellInitSettings.FileOptions) + sizeof(CHAR16); } FileStringPath = AllocateZeroPool(NewSize); + if (FileStringPath == NULL) { + return (EFI_OUT_OF_RESOURCES); + } StrCpy(FileStringPath, ShellInfoObject.ShellInitSettings.FileName); if (ShellInfoObject.ShellInitSettings.FileOptions != NULL) { - StrCat (FileStringPath, L" "); + StrCat(FileStringPath, L" "); StrCat(FileStringPath, ShellInfoObject.ShellInitSettings.FileOptions); } Status = RunCommand(FileStringPath); @@ -697,18 +827,22 @@ DoStartupScript( return (EFI_SUCCESS); } + gST->ConOut->EnableCursor(gST->ConOut, FALSE); // // print out our warning and see if they press a key // - for ( Status = EFI_UNSUPPORTED, Delay = (ShellInfoObject.ShellInitSettings.Delay * 10) - ; Delay > 0 && EFI_ERROR(Status) + for ( Status = EFI_UNSUPPORTED, Delay = ShellInfoObject.ShellInitSettings.Delay * 10 + ; Delay != 0 && EFI_ERROR(Status) ; Delay-- ){ ShellPrintHiiEx(0, gST->ConOut->Mode->CursorRow, NULL, STRING_TOKEN (STR_SHELL_STARTUP_QUESTION), ShellInfoObject.HiiHandle, Delay/10); gBS->Stall (100000); - Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key); + if (!ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoConsoleIn) { + Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key); + } } ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_CRLF), ShellInfoObject.HiiHandle); + gST->ConOut->EnableCursor(gST->ConOut, TRUE); // // ESC was pressed @@ -717,37 +851,59 @@ DoStartupScript( return (EFI_SUCCESS); } - NamePath = FileDevicePath (NULL, L"startup.nsh"); // - // Try the first location + // Try the first location (must be file system) // - NewPath = AppendDevicePathNode (ImagePath, NamePath); - Status = InternalOpenFileDevicePath(NewPath, &FileHandle, EFI_FILE_MODE_READ, 0); + MapName = ShellInfoObject.NewEfiShellProtocol->GetMapFromDevicePath(&ImagePath); + if (MapName != NULL) { + FileStringPath = NULL; + NewSize = 0; + FileStringPath = StrnCatGrow(&FileStringPath, &NewSize, MapName, 0); + if (FileStringPath == NULL) { + Status = EFI_OUT_OF_RESOURCES; + } else { + TempSpot = StrStr(FileStringPath, L";"); + if (TempSpot != NULL) { + *TempSpot = CHAR_NULL; + } + FileStringPath = StrnCatGrow(&FileStringPath, &NewSize, ((FILEPATH_DEVICE_PATH*)FilePath)->PathName, 0); + PathRemoveLastItem(FileStringPath); + FileStringPath = StrnCatGrow(&FileStringPath, &NewSize, mStartupScript, 0); + Status = ShellInfoObject.NewEfiShellProtocol->OpenFileByName(FileStringPath, &FileHandle, EFI_FILE_MODE_READ); + FreePool(FileStringPath); + } + } if (EFI_ERROR(Status)) { + NamePath = FileDevicePath (NULL, mStartupScript); + NewPath = AppendDevicePathNode (ImagePath, NamePath); + FreePool(NamePath); + // - // Try the second location + // Try the location // - FreePool(NewPath); - NewPath = AppendDevicePathNode (FilePath , NamePath); Status = InternalOpenFileDevicePath(NewPath, &FileHandle, EFI_FILE_MODE_READ, 0); + FreePool(NewPath); } - // // If we got a file, run it // - if (!EFI_ERROR(Status)) { - Status = RunScriptFileHandle (FileHandle, L"startup.nsh"); + if (!EFI_ERROR(Status) && FileHandle != NULL) { + Status = RunScriptFileHandle (FileHandle, mStartupScript); ShellInfoObject.NewEfiShellProtocol->CloseFile(FileHandle); } else { - // - // we return success since we dont need to have a startup script - // - Status = EFI_SUCCESS; - ASSERT(FileHandle == NULL); + FileStringPath = ShellFindFilePath(mStartupScript); + if (FileStringPath == NULL) { + // + // we return success since we dont need to have a startup script + // + Status = EFI_SUCCESS; + ASSERT(FileHandle == NULL); + } else { + Status = RunScriptFile(FileStringPath); + FreePool(FileStringPath); + } } - FreePool(NamePath); - FreePool(NewPath); return (Status); } @@ -869,10 +1025,10 @@ AddLineToCommandHistory( Checks if a string is an alias for another command. If yes, then it replaces the alias name with the correct command name. - @param[in,out] CommandString Upon entry the potential alias. Upon return the - command name if it was an alias. If it was not - an alias it will be unchanged. This function may - change the buffer to fit the command name. + @param[in, out] CommandString Upon entry the potential alias. Upon return the + command name if it was an alias. If it was not + an alias it will be unchanged. This function may + change the buffer to fit the command name. @retval EFI_SUCCESS The name was changed. @retval EFI_SUCCESS The name was not an alias. @@ -891,7 +1047,7 @@ ShellConvertAlias( return (EFI_SUCCESS); } FreePool(*CommandString); - *CommandString = AllocatePool(StrSize(NewString)); + *CommandString = AllocateZeroPool(StrSize(NewString)); if (*CommandString == NULL) { return (EFI_OUT_OF_RESOURCES); } @@ -993,6 +1149,12 @@ ShellConvertVariables ( NewCommandLine1 = AllocateZeroPool(NewSize); NewCommandLine2 = AllocateZeroPool(NewSize); ItemTemp = AllocateZeroPool(ItemSize+(2*sizeof(CHAR16))); + if (NewCommandLine1 == NULL || NewCommandLine2 == NULL || ItemTemp == NULL) { + SHELL_FREE_NON_NULL(NewCommandLine1); + SHELL_FREE_NON_NULL(NewCommandLine2); + SHELL_FREE_NON_NULL(ItemTemp); + return (NULL); + } StrCpy(NewCommandLine1, OriginalCommandLine); for (MasterEnvList = EfiShellGetEnv(NULL) ; MasterEnvList != NULL && *MasterEnvList != CHAR_NULL //&& *(MasterEnvList+1) != CHAR_NULL @@ -1059,7 +1221,16 @@ RunSplitCommand( NextCommandLine = StrnCatGrow(&NextCommandLine, &Size1, StrStr(CmdLine, L"|")+1, 0); OurCommandLine = StrnCatGrow(&OurCommandLine , &Size2, CmdLine , StrStr(CmdLine, L"|") - CmdLine); - if (NextCommandLine[0] != CHAR_NULL && + + if (NextCommandLine == NULL || OurCommandLine == NULL) { + SHELL_FREE_NON_NULL(OurCommandLine); + SHELL_FREE_NON_NULL(NextCommandLine); + return (EFI_OUT_OF_RESOURCES); + } else if (StrStr(OurCommandLine, L"|") != NULL || Size1 == 0 || Size2 == 0) { + SHELL_FREE_NON_NULL(OurCommandLine); + SHELL_FREE_NON_NULL(NextCommandLine); + return (EFI_INVALID_PARAMETER); + } else if (NextCommandLine[0] != CHAR_NULL && NextCommandLine[0] == L'a' && NextCommandLine[1] == L' ' ){ @@ -1080,7 +1251,6 @@ RunSplitCommand( ASSERT(Split->SplitStdOut != NULL); InsertHeadList(&ShellInfoObject.SplitList.Link, &Split->Link); - ASSERT(StrStr(OurCommandLine, L"|") == NULL); Status = RunCommand(OurCommandLine); // @@ -1150,12 +1320,13 @@ RunCommand( UINTN PostAliasSize; CHAR16 *PostVariableCmdLine; CHAR16 *CommandWithPath; - EFI_DEVICE_PATH_PROTOCOL *DevPath; + CONST EFI_DEVICE_PATH_PROTOCOL *DevPath; CONST CHAR16 *TempLocation; CONST CHAR16 *TempLocation2; SHELL_FILE_HANDLE OriginalStdIn; SHELL_FILE_HANDLE OriginalStdOut; SHELL_FILE_HANDLE OriginalStdErr; + SYSTEM_TABLE_INFO OriginalSystemTableInfo; CHAR16 *TempLocation3; UINTN Count; UINTN Count2; @@ -1177,6 +1348,9 @@ RunCommand( Split = NULL; CleanOriginal = StrnCatGrow(&CleanOriginal, NULL, CmdLine, 0); + if (CleanOriginal == NULL) { + return (EFI_OUT_OF_RESOURCES); + } while (CleanOriginal[StrLen(CleanOriginal)-1] == L' ') { CleanOriginal[StrLen(CleanOriginal)-1] = CHAR_NULL; } @@ -1225,6 +1399,10 @@ RunCommand( PostAliasCmdLine = NULL; } + if (PostVariableCmdLine == NULL) { + return (EFI_OUT_OF_RESOURCES); + } + while (PostVariableCmdLine[StrLen(PostVariableCmdLine)-1] == L' ') { PostVariableCmdLine[StrLen(PostVariableCmdLine)-1] = CHAR_NULL; } @@ -1258,6 +1436,9 @@ RunCommand( } else { Status = RunSplitCommand(PostVariableCmdLine, Split->SplitStdIn, Split->SplitStdOut); } + if (EFI_ERROR(Status)) { + ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_INVALID_SPLIT), ShellInfoObject.HiiHandle, PostVariableCmdLine); + } } else { // @@ -1277,25 +1458,14 @@ RunCommand( - Status = UpdateStdInStdOutStdErr(ShellInfoObject.NewShellParametersProtocol, PostVariableCmdLine, &OriginalStdIn, &OriginalStdOut, &OriginalStdErr); + Status = UpdateStdInStdOutStdErr(ShellInfoObject.NewShellParametersProtocol, PostVariableCmdLine, &OriginalStdIn, &OriginalStdOut, &OriginalStdErr, &OriginalSystemTableInfo); if (EFI_ERROR(Status)) { - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_INVALID_REDIR), ShellInfoObject.HiiHandle); - } else { - // - // remove the < and/or > from the command line now - // - for (TempLocation3 = PostVariableCmdLine ; TempLocation3 != NULL && *TempLocation3 != CHAR_NULL ; TempLocation3++) { - if (*TempLocation3 == L'^') { - if (*(TempLocation3+1) == L'<' || *(TempLocation3+1) == L'>') { - CopyMem(TempLocation3, TempLocation3+1, StrSize(TempLocation3) - sizeof(TempLocation3[0])); - } - } else if (*TempLocation3 == L'>') { - *TempLocation3 = CHAR_NULL; - } else if ((*TempLocation3 == L'1' || *TempLocation3 == L'2')&&(*(TempLocation3+1) == L'>')) { - *TempLocation3 = CHAR_NULL; - } + if (Status == EFI_NOT_FOUND) { + ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_REDUNDA_REDIR), ShellInfoObject.HiiHandle); + } else { + ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_INVALID_REDIR), ShellInfoObject.HiiHandle); } - + } else { while (PostVariableCmdLine[StrLen(PostVariableCmdLine)-1] == L' ') { PostVariableCmdLine[StrLen(PostVariableCmdLine)-1] = CHAR_NULL; } @@ -1396,7 +1566,7 @@ RunCommand( RestoreArgcArgv(ShellInfoObject.NewShellParametersProtocol, &Argv, &Argc); - RestoreStdInStdOutStdErr(ShellInfoObject.NewShellParametersProtocol, &OriginalStdIn, &OriginalStdOut, &OriginalStdErr); + RestoreStdInStdOutStdErr(ShellInfoObject.NewShellParametersProtocol, &OriginalStdIn, &OriginalStdOut, &OriginalStdErr, &OriginalSystemTableInfo); } if (CommandName != NULL) { if (ShellCommandGetCurrentScriptFile() != NULL && !IsScriptOnlyCommand(CommandName)) { @@ -1412,7 +1582,6 @@ RunCommand( SHELL_FREE_NON_NULL(CommandName); SHELL_FREE_NON_NULL(CommandWithPath); SHELL_FREE_NON_NULL(PostVariableCmdLine); - SHELL_FREE_NON_NULL(DevPath); return (Status); } @@ -1473,21 +1642,29 @@ RunScriptFileHandle ( SCRIPT_COMMAND_LIST *LastCommand; BOOLEAN Ascii; BOOLEAN PreScriptEchoState; + BOOLEAN PreCommandEchoState; CONST CHAR16 *CurDir; UINTN LineCount; + CHAR16 LeString[50]; ASSERT(!ShellCommandGetScriptExit()); PreScriptEchoState = ShellCommandGetEchoState(); NewScriptFile = (SCRIPT_FILE*)AllocateZeroPool(sizeof(SCRIPT_FILE)); - ASSERT(NewScriptFile != NULL); + if (NewScriptFile == NULL) { + return (EFI_OUT_OF_RESOURCES); + } // // Set up the name // ASSERT(NewScriptFile->ScriptName == NULL); NewScriptFile->ScriptName = StrnCatGrow(&NewScriptFile->ScriptName, NULL, Name, 0); + if (NewScriptFile->ScriptName == NULL) { + DeleteScriptFileStruct(NewScriptFile); + return (EFI_OUT_OF_RESOURCES); + } // // Save the parameters (used to replace %0 to %9 later on) @@ -1495,10 +1672,17 @@ RunScriptFileHandle ( NewScriptFile->Argc = ShellInfoObject.NewShellParametersProtocol->Argc; if (NewScriptFile->Argc != 0) { NewScriptFile->Argv = (CHAR16**)AllocateZeroPool(NewScriptFile->Argc * sizeof(CHAR16*)); - ASSERT(NewScriptFile->Argv != NULL); + if (NewScriptFile->Argv == NULL) { + DeleteScriptFileStruct(NewScriptFile); + return (EFI_OUT_OF_RESOURCES); + } for (LoopVar = 0 ; LoopVar < 10 && LoopVar < NewScriptFile->Argc; LoopVar++) { ASSERT(NewScriptFile->Argv[LoopVar] == NULL); NewScriptFile->Argv[LoopVar] = StrnCatGrow(&NewScriptFile->Argv[LoopVar], NULL, ShellInfoObject.NewShellParametersProtocol->Argv[LoopVar], 0); + if (NewScriptFile->Argv[LoopVar] == NULL) { + DeleteScriptFileStruct(NewScriptFile); + return (EFI_OUT_OF_RESOURCES); + } } } else { NewScriptFile->Argv = NULL; @@ -1518,7 +1702,10 @@ RunScriptFileHandle ( continue; } NewScriptFile->CurrentCommand = AllocateZeroPool(sizeof(SCRIPT_COMMAND_LIST)); - ASSERT(NewScriptFile->CurrentCommand != NULL); + if (NewScriptFile->CurrentCommand == NULL) { + DeleteScriptFileStruct(NewScriptFile); + return (EFI_OUT_OF_RESOURCES); + } NewScriptFile->CurrentCommand->Cl = CommandLine; NewScriptFile->CurrentCommand->Data = NULL; @@ -1535,13 +1722,23 @@ RunScriptFileHandle ( // // Now enumerate through the commands and run each one. // - CommandLine = AllocatePool(PcdGet16(PcdShellPrintBufferSize)); - CommandLine2 = AllocatePool(PcdGet16(PcdShellPrintBufferSize)); + CommandLine = AllocateZeroPool(PcdGet16(PcdShellPrintBufferSize)); + if (CommandLine == NULL) { + DeleteScriptFileStruct(NewScriptFile); + return (EFI_OUT_OF_RESOURCES); + } + CommandLine2 = AllocateZeroPool(PcdGet16(PcdShellPrintBufferSize)); + if (CommandLine2 == NULL) { + FreePool(CommandLine); + DeleteScriptFileStruct(NewScriptFile); + return (EFI_OUT_OF_RESOURCES); + } for ( NewScriptFile->CurrentCommand = (SCRIPT_COMMAND_LIST *)GetFirstNode(&NewScriptFile->CommandList) ; !IsNull(&NewScriptFile->CommandList, &NewScriptFile->CurrentCommand->Link) ; // conditional increment in the body of the loop ){ + ASSERT(CommandLine2 != NULL); StrCpy(CommandLine2, NewScriptFile->CurrentCommand->Cl); // @@ -1619,26 +1816,45 @@ RunScriptFileHandle ( for (CommandLine3 = CommandLine2 ; CommandLine3[0] == L' ' ; CommandLine3++); - if (CommandLine3[0] == L':' ) { + if (CommandLine3 != NULL && CommandLine3[0] == L':' ) { // // This line is a goto target / label // } else { if (CommandLine3 != NULL && StrLen(CommandLine3) > 0) { - if (ShellCommandGetEchoState()) { - CurDir = ShellInfoObject.NewEfiShellProtocol->GetEnv(L"cwd"); - if (CurDir != NULL && StrLen(CurDir) > 1) { - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_CURDIR), ShellInfoObject.HiiHandle, CurDir); - } else { - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_SHELL), ShellInfoObject.HiiHandle); + if (CommandLine3[0] == L'@') { + // + // We need to save the current echo state + // and disable echo for just this command. + // + PreCommandEchoState = ShellCommandGetEchoState(); + ShellCommandSetEchoState(FALSE); + Status = RunCommand(CommandLine3+1); + + // + // Now restore the pre-'@' echo state. + // + ShellCommandSetEchoState(PreCommandEchoState); + } else { + if (ShellCommandGetEchoState()) { + CurDir = ShellInfoObject.NewEfiShellProtocol->GetEnv(L"cwd"); + if (CurDir != NULL && StrLen(CurDir) > 1) { + ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_CURDIR), ShellInfoObject.HiiHandle, CurDir); + } else { + ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_SHELL), ShellInfoObject.HiiHandle); + } + ShellPrintEx(-1, -1, L"%s\r\n", CommandLine2); } - ShellPrintEx(-1, -1, L"%s\r\n", CommandLine2); + Status = RunCommand(CommandLine3); } - Status = RunCommand(CommandLine3); } if (ShellCommandGetScriptExit()) { - ShellCommandRegisterExit(FALSE); + UnicodeSPrint(LeString, sizeof(LeString)*sizeof(LeString[0]), L"0x%Lx", ShellCommandGetExitCode()); + DEBUG_CODE(InternalEfiShellSetEnv(L"DebugLasterror", LeString, TRUE);); + InternalEfiShellSetEnv(L"Lasterror", LeString, TRUE); + + ShellCommandRegisterExit(FALSE, 0); Status = EFI_SUCCESS; break; } @@ -1666,11 +1882,17 @@ RunScriptFileHandle ( } } - ShellCommandSetEchoState(PreScriptEchoState); FreePool(CommandLine); FreePool(CommandLine2); ShellCommandSetNewScript (NULL); + + // + // Only if this was the last script reset the state. + // + if (ShellCommandGetCurrentScriptFile()==NULL) { + ShellCommandSetEchoState(PreScriptEchoState); + } return (EFI_SUCCESS); }