]> git.proxmox.com Git - mirror_edk2.git/blame - ShellPkg/Application/Shell/Shell.c
ShellPkg: Fixes and updates for the 'drivers' command
[mirror_edk2.git] / ShellPkg / Application / Shell / Shell.c
CommitLineData
a405b86d 1/** @file\r
2 This is THE shell (application)\r
3\r
de4ebdcf 4 Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>\r
d6e88a6c 5 (C) Copyright 2013-2014, Hewlett-Packard Development Company, L.P.\r
a405b86d 6 This program and the accompanying materials\r
7 are licensed and made available under the terms and conditions of the BSD License\r
8 which accompanies this distribution. The full text of the license may be found at\r
9 http://opensource.org/licenses/bsd-license.php\r
10\r
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13\r
14**/\r
15\r
16#include "Shell.h"\r
17\r
18//\r
19// Initialize the global structure\r
20//\r
21SHELL_INFO ShellInfoObject = {\r
22 NULL,\r
23 NULL,\r
24 FALSE,\r
25 FALSE,\r
26 {\r
ce68d3bc
SZ
27 {{\r
28 0,\r
29 0,\r
30 0,\r
31 0,\r
32 0,\r
33 0,\r
34 0,\r
35 0,\r
36 0\r
37 }},\r
a405b86d 38 0,\r
39 NULL,\r
40 NULL\r
41 },\r
ce68d3bc 42 {{NULL, NULL}, NULL},\r
a405b86d 43 {\r
ce68d3bc 44 {{NULL, NULL}, NULL},\r
a405b86d 45 0,\r
46 0,\r
47 TRUE\r
48 },\r
49 NULL,\r
50 0,\r
51 NULL,\r
52 NULL,\r
53 NULL,\r
54 NULL,\r
55 NULL,\r
ce68d3bc
SZ
56 {{NULL, NULL}, NULL, NULL},\r
57 {{NULL, NULL}, NULL, NULL},\r
58 NULL,\r
59 NULL,\r
60 NULL,\r
61 NULL,\r
8be0ba36 62 NULL,\r
63 NULL,\r
64 NULL,\r
a49f6a2f 65 NULL,\r
66 FALSE\r
a405b86d 67};\r
68\r
69STATIC CONST CHAR16 mScriptExtension[] = L".NSH";\r
70STATIC CONST CHAR16 mExecutableExtensions[] = L".NSH;.EFI";\r
3c865f20 71STATIC CONST CHAR16 mStartupScript[] = L"startup.nsh";\r
a405b86d 72\r
ad2bc854 73/**\r
51429264 74 Cleans off leading and trailing spaces and tabs.\r
ad2bc854 75\r
51429264 76 @param[in] String pointer to the string to trim them off.\r
ad2bc854
JC
77**/\r
78EFI_STATUS\r
79EFIAPI\r
80TrimSpaces(\r
81 IN CHAR16 **String\r
82 )\r
83{\r
84 ASSERT(String != NULL);\r
85 ASSERT(*String!= NULL);\r
86 //\r
87 // Remove any spaces and tabs at the beginning of the (*String).\r
88 //\r
89 while (((*String)[0] == L' ') || ((*String)[0] == L'\t')) {\r
90 CopyMem((*String), (*String)+1, StrSize((*String)) - sizeof((*String)[0]));\r
91 }\r
92\r
93 //\r
404b3f43 94 // Remove any spaces and tabs at the end of the (*String).\r
ad2bc854 95 //\r
404b3f43 96 while ((StrLen (*String) > 0) && (((*String)[StrLen((*String))-1] == L' ') || ((*String)[StrLen((*String))-1] == L'\t'))) {\r
ad2bc854
JC
97 (*String)[StrLen((*String))-1] = CHAR_NULL;\r
98 }\r
99\r
100 return (EFI_SUCCESS);\r
101}\r
102\r
19a44972 103/**\r
518c8cdc 104 Find a command line contains a split operation\r
19a44972
JC
105\r
106 @param[in] CmdLine The command line to parse.\r
107\r
518c8cdc 108 @retval A pointer to the | character in CmdLine or NULL if not present.\r
19a44972 109**/\r
51429264 110CONST CHAR16*\r
19a44972 111EFIAPI\r
518c8cdc 112FindSplit(\r
19a44972
JC
113 IN CONST CHAR16 *CmdLine\r
114 )\r
115{\r
116 CONST CHAR16 *TempSpot;\r
117 TempSpot = NULL;\r
118 if (StrStr(CmdLine, L"|") != NULL) {\r
119 for (TempSpot = CmdLine ; TempSpot != NULL && *TempSpot != CHAR_NULL ; TempSpot++) {\r
120 if (*TempSpot == L'^' && *(TempSpot+1) == L'|') {\r
121 TempSpot++;\r
122 } else if (*TempSpot == L'|') {\r
123 break;\r
124 }\r
125 }\r
126 }\r
518c8cdc
JC
127 return (TempSpot);\r
128}\r
129\r
130/**\r
131 Determine if a command line contains a split operation\r
132\r
133 @param[in] CmdLine The command line to parse.\r
134\r
135 @retval TRUE CmdLine has a valid split.\r
136 @retval FALSE CmdLine does not have a valid split.\r
137**/\r
138BOOLEAN\r
139EFIAPI\r
140ContainsSplit(\r
141 IN CONST CHAR16 *CmdLine\r
142 )\r
143{\r
144 CONST CHAR16 *TempSpot;\r
145 TempSpot = FindSplit(CmdLine);\r
8dcd84b9 146 return (BOOLEAN) ((TempSpot != NULL) && (*TempSpot != CHAR_NULL));\r
19a44972
JC
147}\r
148\r
733f138d 149/**\r
150 Function to start monitoring for CTRL-S using SimpleTextInputEx. This \r
151 feature's enabled state was not known when the shell initially launched.\r
152\r
153 @retval EFI_SUCCESS The feature is enabled.\r
154 @retval EFI_OUT_OF_RESOURCES There is not enough mnemory available.\r
155**/\r
156EFI_STATUS\r
157EFIAPI\r
158InternalEfiShellStartCtrlSMonitor(\r
159 VOID\r
160 )\r
161{\r
162 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *SimpleEx;\r
163 EFI_KEY_DATA KeyData;\r
164 EFI_STATUS Status;\r
165\r
166 Status = gBS->OpenProtocol(\r
167 gST->ConsoleInHandle,\r
168 &gEfiSimpleTextInputExProtocolGuid,\r
169 (VOID**)&SimpleEx,\r
170 gImageHandle,\r
171 NULL,\r
172 EFI_OPEN_PROTOCOL_GET_PROTOCOL);\r
173 if (EFI_ERROR(Status)) {\r
174 ShellPrintHiiEx(\r
175 -1, \r
176 -1, \r
177 NULL,\r
178 STRING_TOKEN (STR_SHELL_NO_IN_EX),\r
179 ShellInfoObject.HiiHandle);\r
180 return (EFI_SUCCESS);\r
181 }\r
182\r
183 KeyData.KeyState.KeyToggleState = 0;\r
184 KeyData.Key.ScanCode = 0;\r
185 KeyData.KeyState.KeyShiftState = EFI_SHIFT_STATE_VALID|EFI_LEFT_CONTROL_PRESSED;\r
186 KeyData.Key.UnicodeChar = L's';\r
187\r
188 Status = SimpleEx->RegisterKeyNotify(\r
189 SimpleEx,\r
190 &KeyData,\r
191 NotificationFunction,\r
192 &ShellInfoObject.CtrlSNotifyHandle1);\r
193 \r
194 KeyData.KeyState.KeyShiftState = EFI_SHIFT_STATE_VALID|EFI_RIGHT_CONTROL_PRESSED;\r
195 if (!EFI_ERROR(Status)) {\r
196 Status = SimpleEx->RegisterKeyNotify(\r
197 SimpleEx,\r
198 &KeyData,\r
199 NotificationFunction,\r
200 &ShellInfoObject.CtrlSNotifyHandle2);\r
201 }\r
202 KeyData.KeyState.KeyShiftState = EFI_SHIFT_STATE_VALID|EFI_LEFT_CONTROL_PRESSED;\r
203 KeyData.Key.UnicodeChar = 19;\r
204\r
205 if (!EFI_ERROR(Status)) {\r
206 Status = SimpleEx->RegisterKeyNotify(\r
207 SimpleEx,\r
208 &KeyData,\r
209 NotificationFunction,\r
ed8b5297 210 &ShellInfoObject.CtrlSNotifyHandle3);\r
733f138d 211 } \r
212 KeyData.KeyState.KeyShiftState = EFI_SHIFT_STATE_VALID|EFI_RIGHT_CONTROL_PRESSED;\r
213 if (!EFI_ERROR(Status)) {\r
214 Status = SimpleEx->RegisterKeyNotify(\r
215 SimpleEx,\r
216 &KeyData,\r
217 NotificationFunction,\r
ed8b5297 218 &ShellInfoObject.CtrlSNotifyHandle4);\r
733f138d 219 }\r
220 return (Status);\r
221}\r
222\r
223\r
224\r
a405b86d 225/**\r
226 The entry point for the application.\r
227\r
228 @param[in] ImageHandle The firmware allocated handle for the EFI image.\r
229 @param[in] SystemTable A pointer to the EFI System Table.\r
230\r
231 @retval EFI_SUCCESS The entry point is executed successfully.\r
232 @retval other Some error occurs when executing this entry point.\r
233\r
234**/\r
235EFI_STATUS\r
236EFIAPI\r
237UefiMain (\r
238 IN EFI_HANDLE ImageHandle,\r
239 IN EFI_SYSTEM_TABLE *SystemTable\r
240 )\r
241{\r
a49f6a2f 242 EFI_STATUS Status;\r
243 CHAR16 *TempString;\r
244 UINTN Size;\r
245 EFI_HANDLE ConInHandle;\r
246 EFI_SIMPLE_TEXT_INPUT_PROTOCOL *OldConIn;\r
5223c121
BJ
247 UINTN ExitDataSize;\r
248 CHAR16 *ExitData;\r
249 SHELL_STATUS ExitStatus;\r
a405b86d 250\r
251 if (PcdGet8(PcdShellSupportLevel) > 3) {\r
252 return (EFI_UNSUPPORTED);\r
253 }\r
254\r
255 //\r
256 // Clear the screen\r
257 //\r
258 Status = gST->ConOut->ClearScreen(gST->ConOut);\r
3c865f20 259 if (EFI_ERROR(Status)) {\r
260 return (Status);\r
261 }\r
a405b86d 262\r
263 //\r
264 // Populate the global structure from PCDs\r
265 //\r
266 ShellInfoObject.ImageDevPath = NULL;\r
267 ShellInfoObject.FileDevPath = NULL;\r
268 ShellInfoObject.PageBreakEnabled = PcdGetBool(PcdShellPageBreakDefault);\r
269 ShellInfoObject.ViewingSettings.InsertMode = PcdGetBool(PcdShellInsertModeDefault);\r
270 ShellInfoObject.LogScreenCount = PcdGet8 (PcdShellScreenLogCount );\r
271\r
272 //\r
273 // verify we dont allow for spec violation\r
274 //\r
275 ASSERT(ShellInfoObject.LogScreenCount >= 3);\r
276\r
277 //\r
278 // Initialize the LIST ENTRY objects...\r
279 //\r
280 InitializeListHead(&ShellInfoObject.BufferToFreeList.Link);\r
281 InitializeListHead(&ShellInfoObject.ViewingSettings.CommandHistory.Link);\r
282 InitializeListHead(&ShellInfoObject.SplitList.Link);\r
283\r
284 //\r
285 // Check PCDs for optional features that are not implemented yet.\r
286 //\r
287 if ( PcdGetBool(PcdShellSupportOldProtocols)\r
288 || !FeaturePcdGet(PcdShellRequireHiiPlatform)\r
289 || FeaturePcdGet(PcdShellSupportFrameworkHii)\r
290 ) {\r
291 return (EFI_UNSUPPORTED);\r
292 }\r
293\r
294 //\r
295 // turn off the watchdog timer\r
296 //\r
297 gBS->SetWatchdogTimer (0, 0, 0, NULL);\r
298\r
299 //\r
300 // install our console logger. This will keep a log of the output for back-browsing\r
301 //\r
302 Status = ConsoleLoggerInstall(ShellInfoObject.LogScreenCount, &ShellInfoObject.ConsoleInfo);\r
71c49eaf
SQ
303 if(EFI_ERROR (Status)) {\r
304 ExitStatus = (SHELL_STATUS) (Status & (~MAX_BIT));\r
305 } else {\r
306 ExitStatus = SHELL_SUCCESS;\r
307 }\r
308 \r
3c865f20 309 if (!EFI_ERROR(Status)) {\r
310 //\r
311 // Enable the cursor to be visible\r
312 //\r
313 gST->ConOut->EnableCursor (gST->ConOut, TRUE);\r
a405b86d 314\r
3c865f20 315 //\r
316 // If supporting EFI 1.1 we need to install HII protocol\r
317 // only do this if PcdShellRequireHiiPlatform == FALSE\r
318 //\r
319 // remove EFI_UNSUPPORTED check above when complete.\r
320 ///@todo add support for Framework HII\r
a405b86d 321\r
3c865f20 322 //\r
323 // install our (solitary) HII package\r
324 //\r
325 ShellInfoObject.HiiHandle = HiiAddPackages (&gEfiCallerIdGuid, gImageHandle, ShellStrings, NULL);\r
a405b86d 326 if (ShellInfoObject.HiiHandle == NULL) {\r
3c865f20 327 if (PcdGetBool(PcdShellSupportFrameworkHii)) {\r
328 ///@todo Add our package into Framework HII\r
329 }\r
330 if (ShellInfoObject.HiiHandle == NULL) {\r
0477054b
BJ
331 Status = EFI_NOT_STARTED;\r
332 goto FreeResources;\r
3c865f20 333 }\r
a405b86d 334 }\r
a405b86d 335\r
3c865f20 336 //\r
337 // create and install the EfiShellParametersProtocol\r
338 //\r
339 Status = CreatePopulateInstallShellParametersProtocol(&ShellInfoObject.NewShellParametersProtocol, &ShellInfoObject.RootShellInstance);\r
340 ASSERT_EFI_ERROR(Status);\r
341 ASSERT(ShellInfoObject.NewShellParametersProtocol != NULL);\r
a405b86d 342\r
3c865f20 343 //\r
344 // create and install the EfiShellProtocol\r
345 //\r
346 Status = CreatePopulateInstallShellProtocol(&ShellInfoObject.NewEfiShellProtocol);\r
347 ASSERT_EFI_ERROR(Status);\r
348 ASSERT(ShellInfoObject.NewEfiShellProtocol != NULL);\r
a405b86d 349\r
3c865f20 350 //\r
351 // Now initialize the shell library (it requires Shell Parameters protocol)\r
352 //\r
353 Status = ShellInitialize();\r
354 ASSERT_EFI_ERROR(Status);\r
a405b86d 355\r
3c865f20 356 Status = CommandInit();\r
357 ASSERT_EFI_ERROR(Status);\r
a405b86d 358\r
3c865f20 359 //\r
360 // Check the command line\r
361 //\r
da92bf85
BJ
362 Status = ProcessCommandLine ();\r
363 if (EFI_ERROR (Status)) {\r
364 goto FreeResources;\r
365 }\r
a405b86d 366\r
3c865f20 367 //\r
368 // If shell support level is >= 1 create the mappings and paths\r
369 //\r
370 if (PcdGet8(PcdShellSupportLevel) >= 1) {\r
371 Status = ShellCommandCreateInitialMappingsAndPaths();\r
372 }\r
a405b86d 373\r
3c865f20 374 //\r
375 // save the device path for the loaded image and the device path for the filepath (under loaded image)\r
376 // These are where to look for the startup.nsh file\r
377 //\r
378 Status = GetDevicePathsForImageAndFile(&ShellInfoObject.ImageDevPath, &ShellInfoObject.FileDevPath);\r
a405b86d 379 ASSERT_EFI_ERROR(Status);\r
a405b86d 380\r
3c865f20 381 //\r
382 // Display the version\r
383 //\r
384 if (!ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoVersion) {\r
385 ShellPrintHiiEx (\r
386 0,\r
387 gST->ConOut->Mode->CursorRow,\r
388 NULL,\r
284e034f 389 STRING_TOKEN (STR_VER_OUTPUT_MAIN_SHELL),\r
3c865f20 390 ShellInfoObject.HiiHandle,\r
391 SupportLevel[PcdGet8(PcdShellSupportLevel)],\r
392 gEfiShellProtocol->MajorVersion,\r
284e034f
CP
393 gEfiShellProtocol->MinorVersion\r
394 );\r
395\r
396 ShellPrintHiiEx (\r
397 -1,\r
398 -1,\r
399 NULL,\r
400 STRING_TOKEN (STR_VER_OUTPUT_MAIN_SUPPLIER),\r
401 ShellInfoObject.HiiHandle,\r
402 (CHAR16 *) PcdGetPtr (PcdShellSupplier)\r
403 );\r
404\r
405 ShellPrintHiiEx (\r
406 -1,\r
407 -1,\r
408 NULL,\r
409 STRING_TOKEN (STR_VER_OUTPUT_MAIN_UEFI),\r
410 ShellInfoObject.HiiHandle,\r
3c865f20 411 (gST->Hdr.Revision&0xffff0000)>>16,\r
412 (gST->Hdr.Revision&0x0000ffff),\r
413 gST->FirmwareVendor,\r
414 gST->FirmwareRevision\r
415 );\r
416 }\r
a405b86d 417\r
3c865f20 418 //\r
419 // Display the mapping\r
420 //\r
421 if (PcdGet8(PcdShellSupportLevel) >= 2 && !ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoMap) {\r
5223c121 422 Status = RunCommand(L"map", NULL);\r
3c865f20 423 ASSERT_EFI_ERROR(Status);\r
424 }\r
a405b86d 425\r
3c865f20 426 //\r
427 // init all the built in alias'\r
428 //\r
429 Status = SetBuiltInAlias();\r
430 ASSERT_EFI_ERROR(Status);\r
a405b86d 431\r
3c865f20 432 //\r
433 // Initialize environment variables\r
434 //\r
435 if (ShellCommandGetProfileList() != NULL) {\r
436 Status = InternalEfiShellSetEnv(L"profiles", ShellCommandGetProfileList(), TRUE);\r
437 ASSERT_EFI_ERROR(Status);\r
438 }\r
a405b86d 439\r
3c865f20 440 Size = 100;\r
441 TempString = AllocateZeroPool(Size);\r
a405b86d 442\r
3c865f20 443 UnicodeSPrint(TempString, Size, L"%d", PcdGet8(PcdShellSupportLevel));\r
444 Status = InternalEfiShellSetEnv(L"uefishellsupport", TempString, TRUE);\r
445 ASSERT_EFI_ERROR(Status);\r
a405b86d 446\r
3c865f20 447 UnicodeSPrint(TempString, Size, L"%d.%d", ShellInfoObject.NewEfiShellProtocol->MajorVersion, ShellInfoObject.NewEfiShellProtocol->MinorVersion);\r
448 Status = InternalEfiShellSetEnv(L"uefishellversion", TempString, TRUE);\r
449 ASSERT_EFI_ERROR(Status);\r
a405b86d 450\r
3c865f20 451 UnicodeSPrint(TempString, Size, L"%d.%d", (gST->Hdr.Revision & 0xFFFF0000) >> 16, gST->Hdr.Revision & 0x0000FFFF);\r
452 Status = InternalEfiShellSetEnv(L"uefiversion", TempString, TRUE);\r
453 ASSERT_EFI_ERROR(Status);\r
a405b86d 454\r
3c865f20 455 FreePool(TempString);\r
a405b86d 456\r
3c865f20 457 if (!EFI_ERROR(Status)) {\r
458 if (!ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoInterrupt) {\r
a405b86d 459 //\r
3c865f20 460 // Set up the event for CTRL-C monitoring...\r
a405b86d 461 //\r
8be0ba36 462 Status = InernalEfiShellStartMonitor();\r
3c865f20 463 }\r
464\r
733f138d 465 if (!EFI_ERROR(Status) && !ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoConsoleIn) {\r
466 //\r
467 // Set up the event for CTRL-S monitoring...\r
468 //\r
469 Status = InternalEfiShellStartCtrlSMonitor();\r
470 }\r
471\r
a49f6a2f 472 if (!EFI_ERROR(Status) && ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoConsoleIn) {\r
473 //\r
474 // close off the gST->ConIn\r
475 //\r
476 OldConIn = gST->ConIn;\r
477 ConInHandle = gST->ConsoleInHandle;\r
478 gST->ConIn = CreateSimpleTextInOnFile((SHELL_FILE_HANDLE)&FileInterfaceNulFile, &gST->ConsoleInHandle);\r
479 } else {\r
480 OldConIn = NULL;\r
481 ConInHandle = NULL;\r
482 }\r
483\r
3c865f20 484 if (!EFI_ERROR(Status) && PcdGet8(PcdShellSupportLevel) >= 1) {\r
a405b86d 485 //\r
3c865f20 486 // process the startup script or launch the called app.\r
a405b86d 487 //\r
5223c121
BJ
488 Status = DoStartupScript(\r
489 ShellInfoObject.ImageDevPath,\r
490 ShellInfoObject.FileDevPath,\r
491 &ExitStatus\r
492 );\r
3c865f20 493 }\r
a405b86d 494\r
ca79c798 495 if (!ShellInfoObject.ShellInitSettings.BitUnion.Bits.Exit && !ShellCommandGetExit() && (PcdGet8(PcdShellSupportLevel) >= 3 || PcdGetBool(PcdShellForceConsole)) && !EFI_ERROR(Status) && !ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoConsoleIn) {\r
a405b86d 496 //\r
3c865f20 497 // begin the UI waiting loop\r
a405b86d 498 //\r
3c865f20 499 do {\r
500 //\r
501 // clean out all the memory allocated for CONST <something> * return values\r
502 // between each shell prompt presentation\r
503 //\r
504 if (!IsListEmpty(&ShellInfoObject.BufferToFreeList.Link)){\r
505 FreeBufferList(&ShellInfoObject.BufferToFreeList);\r
506 }\r
507\r
508 //\r
509 // Reset page break back to default.\r
510 //\r
511 ShellInfoObject.PageBreakEnabled = PcdGetBool(PcdShellPageBreakDefault);\r
81cd2f53 512 ASSERT (ShellInfoObject.ConsoleInfo != NULL);\r
3c865f20 513 ShellInfoObject.ConsoleInfo->Enabled = TRUE;\r
514 ShellInfoObject.ConsoleInfo->RowCounter = 0;\r
515\r
8be0ba36 516 //\r
517 // Reset the CTRL-C event (yes we ignore the return values)\r
518 //\r
519 Status = gBS->CheckEvent (ShellInfoObject.NewEfiShellProtocol->ExecutionBreak);\r
520\r
3c865f20 521 //\r
522 // Display Prompt\r
523 //\r
524 Status = DoShellPrompt();\r
525 } while (!ShellCommandGetExit());\r
5223c121 526 ExitStatus = (SHELL_STATUS) ShellCommandGetExitCode();\r
3c865f20 527 }\r
a49f6a2f 528 if (OldConIn != NULL && ConInHandle != NULL) {\r
529 CloseSimpleTextInOnFile (gST->ConIn);\r
530 gST->ConIn = OldConIn;\r
531 gST->ConsoleInHandle = ConInHandle;\r
532 }\r
a405b86d 533 }\r
534 }\r
3c865f20 535\r
0477054b 536FreeResources:\r
a405b86d 537 //\r
538 // uninstall protocols / free memory / etc...\r
539 //\r
540 if (ShellInfoObject.UserBreakTimer != NULL) {\r
541 gBS->CloseEvent(ShellInfoObject.UserBreakTimer);\r
542 DEBUG_CODE(ShellInfoObject.UserBreakTimer = NULL;);\r
543 }\r
a405b86d 544 if (ShellInfoObject.ImageDevPath != NULL) {\r
545 FreePool(ShellInfoObject.ImageDevPath);\r
546 DEBUG_CODE(ShellInfoObject.ImageDevPath = NULL;);\r
547 }\r
548 if (ShellInfoObject.FileDevPath != NULL) {\r
549 FreePool(ShellInfoObject.FileDevPath);\r
550 DEBUG_CODE(ShellInfoObject.FileDevPath = NULL;);\r
551 }\r
552 if (ShellInfoObject.NewShellParametersProtocol != NULL) {\r
553 CleanUpShellParametersProtocol(ShellInfoObject.NewShellParametersProtocol);\r
554 DEBUG_CODE(ShellInfoObject.NewShellParametersProtocol = NULL;);\r
555 }\r
556 if (ShellInfoObject.NewEfiShellProtocol != NULL){\r
8be0ba36 557 if (ShellInfoObject.NewEfiShellProtocol->IsRootShell()){\r
92670471 558 InternalEfiShellSetEnv(L"cwd", NULL, TRUE);\r
8be0ba36 559 }\r
a405b86d 560 CleanUpShellProtocol(ShellInfoObject.NewEfiShellProtocol);\r
561 DEBUG_CODE(ShellInfoObject.NewEfiShellProtocol = NULL;);\r
562 }\r
563\r
564 if (!IsListEmpty(&ShellInfoObject.BufferToFreeList.Link)){\r
565 FreeBufferList(&ShellInfoObject.BufferToFreeList);\r
566 }\r
567\r
568 if (!IsListEmpty(&ShellInfoObject.SplitList.Link)){\r
3c865f20 569 ASSERT(FALSE); ///@todo finish this de-allocation.\r
a405b86d 570 }\r
571\r
572 if (ShellInfoObject.ShellInitSettings.FileName != NULL) {\r
573 FreePool(ShellInfoObject.ShellInitSettings.FileName);\r
574 DEBUG_CODE(ShellInfoObject.ShellInitSettings.FileName = NULL;);\r
575 }\r
576\r
577 if (ShellInfoObject.ShellInitSettings.FileOptions != NULL) {\r
578 FreePool(ShellInfoObject.ShellInitSettings.FileOptions);\r
579 DEBUG_CODE(ShellInfoObject.ShellInitSettings.FileOptions = NULL;);\r
580 }\r
581\r
582 if (ShellInfoObject.HiiHandle != NULL) {\r
583 HiiRemovePackages(ShellInfoObject.HiiHandle);\r
584 DEBUG_CODE(ShellInfoObject.HiiHandle = NULL;);\r
585 }\r
586\r
587 if (!IsListEmpty(&ShellInfoObject.ViewingSettings.CommandHistory.Link)){\r
588 FreeBufferList(&ShellInfoObject.ViewingSettings.CommandHistory);\r
589 }\r
590\r
591 ASSERT(ShellInfoObject.ConsoleInfo != NULL);\r
592 if (ShellInfoObject.ConsoleInfo != NULL) {\r
593 ConsoleLoggerUninstall(ShellInfoObject.ConsoleInfo);\r
594 FreePool(ShellInfoObject.ConsoleInfo);\r
595 DEBUG_CODE(ShellInfoObject.ConsoleInfo = NULL;);\r
596 }\r
597\r
0477054b
BJ
598 if (!EFI_ERROR (Status)) {\r
599 // If the command exited with an error, we pass this error out in the ExitData\r
600 // so that it can be retrieved by the EfiShellExecute function (which may\r
601 // start the shell with gBS->StartImage)\r
602 if (ExitStatus != SHELL_SUCCESS) {\r
603 // Allocate a buffer for exit data to pass to gBS->Exit().\r
604 // This buffer will contain the empty string immediately followed by\r
605 // the shell's exit status. (The empty string is required by the UEFI spec)\r
606 ExitDataSize = (sizeof (CHAR16) + sizeof (SHELL_STATUS));\r
607 ExitData = AllocatePool (ExitDataSize);\r
608 if (ExitData == NULL) {\r
609 return EFI_OUT_OF_RESOURCES;\r
610 }\r
611 ExitData[0] = '\0';\r
612 // Use CopyMem to avoid alignment faults\r
613 CopyMem ((ExitData + 1), &ExitStatus, sizeof (ExitStatus));\r
614\r
615 gBS->Exit (ImageHandle, EFI_ABORTED, ExitDataSize, ExitData);\r
5223c121 616\r
0477054b
BJ
617 ASSERT (FALSE);\r
618 return EFI_SUCCESS;\r
619 } else {\r
620 return EFI_SUCCESS;\r
621 }\r
5223c121 622 } else {\r
0477054b 623 return Status;\r
b6b22b13 624 }\r
a405b86d 625}\r
626\r
627/**\r
628 Sets all the alias' that were registered with the ShellCommandLib library.\r
629\r
630 @retval EFI_SUCCESS all init commands were run sucessfully.\r
631**/\r
632EFI_STATUS\r
633EFIAPI\r
634SetBuiltInAlias(\r
635 )\r
636{\r
637 EFI_STATUS Status;\r
638 CONST ALIAS_LIST *List;\r
639 ALIAS_LIST *Node;\r
640\r
641 //\r
642 // Get all the commands we want to run\r
643 //\r
644 List = ShellCommandGetInitAliasList();\r
645\r
646 //\r
647 // for each command in the List\r
648 //\r
649 for ( Node = (ALIAS_LIST*)GetFirstNode(&List->Link)\r
650 ; !IsNull (&List->Link, &Node->Link)\r
651 ; Node = (ALIAS_LIST *)GetNextNode(&List->Link, &Node->Link)\r
652 ){\r
653 //\r
654 // install the alias'\r
655 //\r
656 Status = InternalSetAlias(Node->CommandString, Node->Alias, TRUE);\r
657 ASSERT_EFI_ERROR(Status);\r
658 }\r
659 return (EFI_SUCCESS);\r
660}\r
661\r
662/**\r
663 Internal function to determine if 2 command names are really the same.\r
664\r
665 @param[in] Command1 The pointer to the first command name.\r
666 @param[in] Command2 The pointer to the second command name.\r
667\r
668 @retval TRUE The 2 command names are the same.\r
669 @retval FALSE The 2 command names are not the same.\r
670**/\r
671BOOLEAN\r
672EFIAPI\r
673IsCommand(\r
674 IN CONST CHAR16 *Command1,\r
675 IN CONST CHAR16 *Command2\r
676 )\r
677{\r
678 if (StringNoCaseCompare(&Command1, &Command2) == 0) {\r
679 return (TRUE);\r
680 }\r
681 return (FALSE);\r
682}\r
683\r
684/**\r
685 Internal function to determine if a command is a script only command.\r
686\r
687 @param[in] CommandName The pointer to the command name.\r
688\r
689 @retval TRUE The command is a script only command.\r
690 @retval FALSE The command is not a script only command.\r
691**/\r
692BOOLEAN\r
693EFIAPI\r
694IsScriptOnlyCommand(\r
695 IN CONST CHAR16 *CommandName\r
696 )\r
697{\r
698 if (IsCommand(CommandName, L"for")\r
699 ||IsCommand(CommandName, L"endfor")\r
700 ||IsCommand(CommandName, L"if")\r
701 ||IsCommand(CommandName, L"else")\r
702 ||IsCommand(CommandName, L"endif")\r
703 ||IsCommand(CommandName, L"goto")) {\r
704 return (TRUE);\r
705 }\r
706 return (FALSE);\r
707}\r
708\r
a405b86d 709/**\r
710 This function will populate the 2 device path protocol parameters based on the\r
711 global gImageHandle. The DevPath will point to the device path for the handle that has\r
712 loaded image protocol installed on it. The FilePath will point to the device path\r
713 for the file that was loaded.\r
714\r
4ff7e37b
ED
715 @param[in, out] DevPath On a sucessful return the device path to the loaded image.\r
716 @param[in, out] FilePath On a sucessful return the device path to the file.\r
a405b86d 717\r
718 @retval EFI_SUCCESS The 2 device paths were sucessfully returned.\r
719 @retval other A error from gBS->HandleProtocol.\r
720\r
721 @sa HandleProtocol\r
722**/\r
723EFI_STATUS\r
724EFIAPI\r
725GetDevicePathsForImageAndFile (\r
726 IN OUT EFI_DEVICE_PATH_PROTOCOL **DevPath,\r
727 IN OUT EFI_DEVICE_PATH_PROTOCOL **FilePath\r
728 )\r
729{\r
730 EFI_STATUS Status;\r
731 EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;\r
732 EFI_DEVICE_PATH_PROTOCOL *ImageDevicePath;\r
733\r
734 ASSERT(DevPath != NULL);\r
735 ASSERT(FilePath != NULL);\r
736\r
737 Status = gBS->OpenProtocol (\r
738 gImageHandle,\r
739 &gEfiLoadedImageProtocolGuid,\r
740 (VOID**)&LoadedImage,\r
741 gImageHandle,\r
742 NULL,\r
743 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
744 );\r
745 if (!EFI_ERROR (Status)) {\r
746 Status = gBS->OpenProtocol (\r
747 LoadedImage->DeviceHandle,\r
748 &gEfiDevicePathProtocolGuid,\r
749 (VOID**)&ImageDevicePath,\r
750 gImageHandle,\r
751 NULL,\r
752 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
753 );\r
754 if (!EFI_ERROR (Status)) {\r
755 *DevPath = DuplicateDevicePath (ImageDevicePath);\r
756 *FilePath = DuplicateDevicePath (LoadedImage->FilePath);\r
a49f6a2f 757 gBS->CloseProtocol(\r
758 LoadedImage->DeviceHandle,\r
759 &gEfiDevicePathProtocolGuid,\r
760 gImageHandle,\r
761 NULL);\r
a405b86d 762 }\r
763 gBS->CloseProtocol(\r
764 gImageHandle,\r
765 &gEfiLoadedImageProtocolGuid,\r
766 gImageHandle,\r
767 NULL);\r
768 }\r
769 return (Status);\r
770}\r
771\r
a405b86d 772/**\r
773 Process all Uefi Shell 2.0 command line options.\r
774\r
775 see Uefi Shell 2.0 section 3.2 for full details.\r
776\r
777 the command line must resemble the following:\r
778\r
779 shell.efi [ShellOpt-options] [options] [file-name [file-name-options]]\r
780\r
781 ShellOpt-options Options which control the initialization behavior of the shell.\r
782 These options are read from the EFI global variable "ShellOpt"\r
783 and are processed before options or file-name.\r
784\r
785 options Options which control the initialization behavior of the shell.\r
786\r
787 file-name The name of a UEFI shell application or script to be executed\r
788 after initialization is complete. By default, if file-name is\r
789 specified, then -nostartup is implied. Scripts are not supported\r
790 by level 0.\r
791\r
792 file-name-options The command-line options that are passed to file-name when it\r
793 is invoked.\r
794\r
795 This will initialize the ShellInfoObject.ShellInitSettings global variable.\r
796\r
797 @retval EFI_SUCCESS The variable is initialized.\r
798**/\r
799EFI_STATUS\r
800EFIAPI\r
801ProcessCommandLine(\r
802 VOID\r
803 )\r
804{\r
23385d63
BJ
805 UINTN Size;\r
806 UINTN LoopVar;\r
807 CHAR16 *CurrentArg;\r
808 CHAR16 *DelayValueStr;\r
809 UINT64 DelayValue;\r
810 EFI_STATUS Status;\r
811 EFI_UNICODE_COLLATION_PROTOCOL *UnicodeCollation;\r
812\r
813 // `file-name-options` will contain arguments to `file-name` that we don't\r
814 // know about. This would cause ShellCommandLineParse to error, so we parse\r
815 // arguments manually, ignoring those after the first thing that doesn't look\r
816 // like a shell option (which is assumed to be `file-name`).\r
817\r
818 Status = gBS->LocateProtocol (\r
819 &gEfiUnicodeCollationProtocolGuid,\r
820 NULL,\r
821 (VOID **) &UnicodeCollation\r
822 );\r
823 if (EFI_ERROR (Status)) {\r
824 return Status;\r
825 }\r
826\r
827 // Set default options\r
828 ShellInfoObject.ShellInitSettings.BitUnion.Bits.Startup = FALSE;\r
829 ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoStartup = FALSE;\r
830 ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoConsoleOut = FALSE;\r
831 ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoConsoleIn = FALSE;\r
832 ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoInterrupt = FALSE;\r
833 ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoMap = FALSE;\r
834 ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoVersion = FALSE;\r
835 ShellInfoObject.ShellInitSettings.BitUnion.Bits.Delay = FALSE;\r
836 ShellInfoObject.ShellInitSettings.BitUnion.Bits.Exit = FALSE;\r
837 ShellInfoObject.ShellInitSettings.Delay = 5;\r
838\r
e9d19a80
CP
839 //\r
840 // Start LoopVar at 0 to parse only optional arguments at Argv[0]\r
841 // and parse other parameters from Argv[1]. This is for use case that\r
842 // UEFI Shell boot option is created, and OptionalData is provided\r
843 // that starts with shell command-line options.\r
844 //\r
845 for (LoopVar = 0 ; LoopVar < gEfiShellParametersProtocol->Argc ; LoopVar++) {\r
23385d63
BJ
846 CurrentArg = gEfiShellParametersProtocol->Argv[LoopVar];\r
847 if (UnicodeCollation->StriColl (\r
848 UnicodeCollation,\r
849 L"-startup",\r
850 CurrentArg\r
851 ) == 0) {\r
852 ShellInfoObject.ShellInitSettings.BitUnion.Bits.Startup = TRUE;\r
3c865f20 853 }\r
23385d63
BJ
854 else if (UnicodeCollation->StriColl (\r
855 UnicodeCollation,\r
856 L"-nostartup",\r
857 CurrentArg\r
858 ) == 0) {\r
859 ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoStartup = TRUE;\r
860 }\r
861 else if (UnicodeCollation->StriColl (\r
862 UnicodeCollation,\r
863 L"-noconsoleout",\r
864 CurrentArg\r
865 ) == 0) {\r
866 ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoConsoleOut = TRUE;\r
867 }\r
868 else if (UnicodeCollation->StriColl (\r
869 UnicodeCollation,\r
870 L"-noconsolein",\r
871 CurrentArg\r
872 ) == 0) {\r
873 ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoConsoleIn = TRUE;\r
874 }\r
875 else if (UnicodeCollation->StriColl (\r
876 UnicodeCollation,\r
877 L"-nointerrupt",\r
878 CurrentArg\r
879 ) == 0) {\r
880 ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoInterrupt = TRUE;\r
881 }\r
882 else if (UnicodeCollation->StriColl (\r
883 UnicodeCollation,\r
884 L"-nomap",\r
885 CurrentArg\r
886 ) == 0) {\r
887 ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoMap = TRUE;\r
888 }\r
889 else if (UnicodeCollation->StriColl (\r
890 UnicodeCollation,\r
891 L"-noversion",\r
892 CurrentArg\r
893 ) == 0) {\r
894 ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoVersion = TRUE;\r
895 }\r
896 else if (UnicodeCollation->StriColl (\r
897 UnicodeCollation,\r
898 L"-delay",\r
899 CurrentArg\r
900 ) == 0) {\r
901 ShellInfoObject.ShellInitSettings.BitUnion.Bits.Delay = TRUE;\r
902 // Check for optional delay value following "-delay"\r
903 DelayValueStr = gEfiShellParametersProtocol->Argv[LoopVar + 1];\r
904 if (DelayValueStr != NULL){\r
905 if (*DelayValueStr == L':') {\r
906 DelayValueStr++;\r
907 }\r
908 if (!EFI_ERROR(ShellConvertStringToUint64 (\r
909 DelayValueStr,\r
910 &DelayValue,\r
911 FALSE,\r
912 FALSE\r
913 ))) {\r
914 ShellInfoObject.ShellInitSettings.Delay = (UINTN)DelayValue;\r
915 LoopVar++;\r
916 }\r
917 }\r
918 } else if (UnicodeCollation->StriColl (\r
919 UnicodeCollation,\r
920 L"-_exit",\r
921 CurrentArg\r
922 ) == 0) {\r
923 ShellInfoObject.ShellInitSettings.BitUnion.Bits.Exit = TRUE;\r
924 } else if (StrnCmp (L"-", CurrentArg, 1) == 0) {\r
925 // Unrecognised option\r
926 ShellPrintHiiEx(-1, -1, NULL,\r
927 STRING_TOKEN (STR_GEN_PROBLEM),\r
928 ShellInfoObject.HiiHandle,\r
929 CurrentArg\r
930 );\r
931 return EFI_INVALID_PARAMETER;\r
932 } else {\r
e9d19a80
CP
933 //\r
934 // First argument should be Shell.efi image name\r
935 //\r
936 if (LoopVar == 0) {\r
937 continue;\r
938 }\r
939\r
23385d63
BJ
940 ShellInfoObject.ShellInitSettings.FileName = AllocateZeroPool(StrSize(CurrentArg));\r
941 if (ShellInfoObject.ShellInitSettings.FileName == NULL) {\r
942 return (EFI_OUT_OF_RESOURCES);\r
943 }\r
944 //\r
945 // We found `file-name`.\r
946 //\r
947 ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoStartup = 1;\r
948\r
949 StrCpy (ShellInfoObject.ShellInitSettings.FileName, CurrentArg);\r
950 LoopVar++;\r
951\r
952 // Add `file-name-options`\r
ba71f790 953 for (Size = 0 ; LoopVar < gEfiShellParametersProtocol->Argc ; LoopVar++) {\r
23385d63
BJ
954 ASSERT((ShellInfoObject.ShellInitSettings.FileOptions == NULL && Size == 0) || (ShellInfoObject.ShellInitSettings.FileOptions != NULL));\r
955 StrnCatGrow(&ShellInfoObject.ShellInitSettings.FileOptions,\r
956 &Size,\r
957 L" ",\r
958 0);\r
959 if (ShellInfoObject.ShellInitSettings.FileOptions == NULL) {\r
960 SHELL_FREE_NON_NULL(ShellInfoObject.ShellInitSettings.FileName);\r
961 return (EFI_OUT_OF_RESOURCES);\r
962 }\r
963 StrnCatGrow(&ShellInfoObject.ShellInitSettings.FileOptions,\r
964 &Size,\r
965 gEfiShellParametersProtocol->Argv[LoopVar],\r
966 0);\r
967 if (ShellInfoObject.ShellInitSettings.FileOptions == NULL) {\r
968 SHELL_FREE_NON_NULL(ShellInfoObject.ShellInitSettings.FileName);\r
969 return (EFI_OUT_OF_RESOURCES);\r
a405b86d 970 }\r
971 }\r
a405b86d 972 }\r
973 }\r
974\r
23385d63 975 // "-nointerrupt" overrides "-delay"\r
733f138d 976 if (ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoInterrupt) {\r
977 ShellInfoObject.ShellInitSettings.Delay = 0;\r
a405b86d 978 }\r
a405b86d 979\r
23385d63 980 return EFI_SUCCESS;\r
a405b86d 981}\r
982\r
983/**\r
984 Handles all interaction with the default startup script.\r
985\r
986 this will check that the correct command line parameters were passed, handle the delay, and then start running the script.\r
987\r
988 @param ImagePath the path to the image for shell. first place to look for the startup script\r
989 @param FilePath the path to the file for shell. second place to look for the startup script.\r
990\r
5223c121
BJ
991 @param[out] ExitStatus The exit code of the script. Ignored if NULL.\r
992\r
a405b86d 993 @retval EFI_SUCCESS the variable is initialized.\r
994**/\r
995EFI_STATUS\r
996EFIAPI\r
997DoStartupScript(\r
5223c121
BJ
998 IN EFI_DEVICE_PATH_PROTOCOL *ImagePath,\r
999 IN EFI_DEVICE_PATH_PROTOCOL *FilePath,\r
1000 OUT SHELL_STATUS *ExitStatus\r
a405b86d 1001 )\r
1002{\r
1003 EFI_STATUS Status;\r
1004 UINTN Delay;\r
1005 EFI_INPUT_KEY Key;\r
1006 SHELL_FILE_HANDLE FileHandle;\r
1007 EFI_DEVICE_PATH_PROTOCOL *NewPath;\r
1008 EFI_DEVICE_PATH_PROTOCOL *NamePath;\r
1009 CHAR16 *FileStringPath;\r
733f138d 1010 CHAR16 *TempSpot;\r
a405b86d 1011 UINTN NewSize;\r
733f138d 1012 CONST CHAR16 *MapName;\r
a405b86d 1013\r
1014 Key.UnicodeChar = CHAR_NULL;\r
1015 Key.ScanCode = 0;\r
1016 FileHandle = NULL;\r
1017\r
1018 if (!ShellInfoObject.ShellInitSettings.BitUnion.Bits.Startup && ShellInfoObject.ShellInitSettings.FileName != NULL) {\r
1019 //\r
1020 // launch something else instead\r
1021 //\r
1022 NewSize = StrSize(ShellInfoObject.ShellInitSettings.FileName);\r
1023 if (ShellInfoObject.ShellInitSettings.FileOptions != NULL) {\r
1024 NewSize += StrSize(ShellInfoObject.ShellInitSettings.FileOptions) + sizeof(CHAR16);\r
1025 }\r
1026 FileStringPath = AllocateZeroPool(NewSize);\r
3c865f20 1027 if (FileStringPath == NULL) {\r
1028 return (EFI_OUT_OF_RESOURCES);\r
1029 }\r
a405b86d 1030 StrCpy(FileStringPath, ShellInfoObject.ShellInitSettings.FileName);\r
1031 if (ShellInfoObject.ShellInitSettings.FileOptions != NULL) {\r
3c865f20 1032 StrCat(FileStringPath, L" ");\r
a405b86d 1033 StrCat(FileStringPath, ShellInfoObject.ShellInitSettings.FileOptions);\r
1034 }\r
5223c121 1035 Status = RunCommand(FileStringPath, ExitStatus);\r
a405b86d 1036 FreePool(FileStringPath);\r
1037 return (Status);\r
1038\r
1039 }\r
1040\r
1041 //\r
1042 // for shell level 0 we do no scripts\r
1043 // Without the Startup bit overriding we allow for nostartup to prevent scripts\r
1044 //\r
1045 if ( (PcdGet8(PcdShellSupportLevel) < 1)\r
1046 || (ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoStartup && !ShellInfoObject.ShellInitSettings.BitUnion.Bits.Startup)\r
1047 ){\r
1048 return (EFI_SUCCESS);\r
1049 }\r
1050\r
5559a41a 1051 gST->ConOut->EnableCursor(gST->ConOut, FALSE);\r
a405b86d 1052 //\r
1053 // print out our warning and see if they press a key\r
1054 //\r
284e034f 1055 for ( Status = EFI_UNSUPPORTED, Delay = ShellInfoObject.ShellInitSettings.Delay\r
733f138d 1056 ; Delay != 0 && EFI_ERROR(Status)\r
a405b86d 1057 ; Delay--\r
1058 ){\r
284e034f
CP
1059 ShellPrintHiiEx(0, gST->ConOut->Mode->CursorRow, NULL, STRING_TOKEN (STR_SHELL_STARTUP_QUESTION), ShellInfoObject.HiiHandle, Delay);\r
1060 gBS->Stall (1000000);\r
733f138d 1061 if (!ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoConsoleIn) {\r
1062 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);\r
1063 }\r
a405b86d 1064 }\r
1065 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_CRLF), ShellInfoObject.HiiHandle);\r
5559a41a 1066 gST->ConOut->EnableCursor(gST->ConOut, TRUE);\r
a405b86d 1067\r
1068 //\r
1069 // ESC was pressed\r
1070 //\r
1071 if (Status == EFI_SUCCESS && Key.UnicodeChar == 0 && Key.ScanCode == SCAN_ESC) {\r
1072 return (EFI_SUCCESS);\r
1073 }\r
1074\r
a405b86d 1075 //\r
733f138d 1076 // Try the first location (must be file system)\r
a405b86d 1077 //\r
733f138d 1078 MapName = ShellInfoObject.NewEfiShellProtocol->GetMapFromDevicePath(&ImagePath);\r
1079 if (MapName != NULL) {\r
1080 FileStringPath = NULL;\r
1081 NewSize = 0;\r
1082 FileStringPath = StrnCatGrow(&FileStringPath, &NewSize, MapName, 0);\r
532691c8 1083 if (FileStringPath == NULL) {\r
1084 Status = EFI_OUT_OF_RESOURCES;\r
1085 } else {\r
1086 TempSpot = StrStr(FileStringPath, L";");\r
1087 if (TempSpot != NULL) {\r
1088 *TempSpot = CHAR_NULL;\r
1089 }\r
1090 FileStringPath = StrnCatGrow(&FileStringPath, &NewSize, ((FILEPATH_DEVICE_PATH*)FilePath)->PathName, 0);\r
1091 PathRemoveLastItem(FileStringPath);\r
1092 FileStringPath = StrnCatGrow(&FileStringPath, &NewSize, mStartupScript, 0);\r
1093 Status = ShellInfoObject.NewEfiShellProtocol->OpenFileByName(FileStringPath, &FileHandle, EFI_FILE_MODE_READ);\r
1094 FreePool(FileStringPath);\r
733f138d 1095 }\r
733f138d 1096 }\r
a405b86d 1097 if (EFI_ERROR(Status)) {\r
733f138d 1098 NamePath = FileDevicePath (NULL, mStartupScript);\r
1099 NewPath = AppendDevicePathNode (ImagePath, NamePath);\r
1100 FreePool(NamePath);\r
1101\r
a405b86d 1102 //\r
733f138d 1103 // Try the location\r
a405b86d 1104 //\r
a405b86d 1105 Status = InternalOpenFileDevicePath(NewPath, &FileHandle, EFI_FILE_MODE_READ, 0);\r
733f138d 1106 FreePool(NewPath);\r
a405b86d 1107 }\r
a405b86d 1108 //\r
1109 // If we got a file, run it\r
1110 //\r
733f138d 1111 if (!EFI_ERROR(Status) && FileHandle != NULL) {\r
5223c121
BJ
1112 Status = RunScriptFile (\r
1113 mStartupScript,\r
1114 FileHandle,\r
1115 L"",\r
1116 ShellInfoObject.NewShellParametersProtocol,\r
1117 ExitStatus\r
1118 );\r
a405b86d 1119 ShellInfoObject.NewEfiShellProtocol->CloseFile(FileHandle);\r
1120 } else {\r
3c865f20 1121 FileStringPath = ShellFindFilePath(mStartupScript);\r
1122 if (FileStringPath == NULL) {\r
1123 //\r
1124 // we return success since we dont need to have a startup script\r
1125 //\r
1126 Status = EFI_SUCCESS;\r
1127 ASSERT(FileHandle == NULL);\r
1128 } else {\r
5223c121
BJ
1129 Status = RunScriptFile(\r
1130 FileStringPath,\r
1131 NULL,\r
1132 L"",\r
1133 ShellInfoObject.NewShellParametersProtocol,\r
1134 ExitStatus\r
1135 );\r
3c865f20 1136 FreePool(FileStringPath);\r
1137 }\r
a405b86d 1138 }\r
1139\r
a405b86d 1140\r
1141 return (Status);\r
1142}\r
1143\r
1144/**\r
1145 Function to perform the shell prompt looping. It will do a single prompt,\r
1146 dispatch the result, and then return. It is expected that the caller will\r
1147 call this function in a loop many times.\r
1148\r
1149 @retval EFI_SUCCESS\r
1150 @retval RETURN_ABORTED\r
1151**/\r
1152EFI_STATUS\r
1153EFIAPI\r
1154DoShellPrompt (\r
1155 VOID\r
1156 )\r
1157{\r
1158 UINTN Column;\r
1159 UINTN Row;\r
1160 CHAR16 *CmdLine;\r
1161 CONST CHAR16 *CurDir;\r
1162 UINTN BufferSize;\r
1163 EFI_STATUS Status;\r
1164\r
1165 CurDir = NULL;\r
1166\r
1167 //\r
1168 // Get screen setting to decide size of the command line buffer\r
1169 //\r
1170 gST->ConOut->QueryMode (gST->ConOut, gST->ConOut->Mode->Mode, &Column, &Row);\r
1171 BufferSize = Column * Row * sizeof (CHAR16);\r
1172 CmdLine = AllocateZeroPool (BufferSize);\r
1173 if (CmdLine == NULL) {\r
1174 return EFI_OUT_OF_RESOURCES;\r
1175 }\r
1176\r
1177 CurDir = ShellInfoObject.NewEfiShellProtocol->GetEnv(L"cwd");\r
1178\r
1179 //\r
1180 // Prompt for input\r
1181 //\r
1182 gST->ConOut->SetCursorPosition (gST->ConOut, 0, gST->ConOut->Mode->CursorRow);\r
1183\r
1184 if (CurDir != NULL && StrLen(CurDir) > 1) {\r
1185 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_CURDIR), ShellInfoObject.HiiHandle, CurDir);\r
1186 } else {\r
1187 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_SHELL), ShellInfoObject.HiiHandle);\r
1188 }\r
1189\r
1190 //\r
1191 // Read a line from the console\r
1192 //\r
1193 Status = ShellInfoObject.NewEfiShellProtocol->ReadFile(ShellInfoObject.NewShellParametersProtocol->StdIn, &BufferSize, CmdLine);\r
1194\r
1195 //\r
1196 // Null terminate the string and parse it\r
1197 //\r
1198 if (!EFI_ERROR (Status)) {\r
1199 CmdLine[BufferSize / sizeof (CHAR16)] = CHAR_NULL;\r
5223c121 1200 Status = RunCommand(CmdLine, NULL);\r
4922715d 1201 }\r
a405b86d 1202\r
1203 //\r
1204 // Done with this command\r
1205 //\r
1206 FreePool (CmdLine);\r
1207 return Status;\r
1208}\r
1209\r
1210/**\r
1211 Add a buffer to the Buffer To Free List for safely returning buffers to other\r
1212 places without risking letting them modify internal shell information.\r
1213\r
1214 @param Buffer Something to pass to FreePool when the shell is exiting.\r
1215**/\r
1216VOID*\r
1217EFIAPI\r
1218AddBufferToFreeList(\r
1219 VOID *Buffer\r
1220 )\r
1221{\r
1222 BUFFER_LIST *BufferListEntry;\r
1223\r
1224 if (Buffer == NULL) {\r
1225 return (NULL);\r
1226 }\r
1227\r
1228 BufferListEntry = AllocateZeroPool(sizeof(BUFFER_LIST));\r
1229 ASSERT(BufferListEntry != NULL);\r
1230 BufferListEntry->Buffer = Buffer;\r
1231 InsertTailList(&ShellInfoObject.BufferToFreeList.Link, &BufferListEntry->Link);\r
1232 return (Buffer);\r
1233}\r
1234\r
1235/**\r
1236 Add a buffer to the Line History List\r
1237\r
1238 @param Buffer The line buffer to add.\r
1239**/\r
1240VOID\r
1241EFIAPI\r
1242AddLineToCommandHistory(\r
1243 IN CONST CHAR16 *Buffer\r
1244 )\r
1245{\r
1246 BUFFER_LIST *Node;\r
1247\r
1248 Node = AllocateZeroPool(sizeof(BUFFER_LIST));\r
1249 ASSERT(Node != NULL);\r
1250 Node->Buffer = AllocateZeroPool(StrSize(Buffer));\r
1251 ASSERT(Node->Buffer != NULL);\r
1252 StrCpy(Node->Buffer, Buffer);\r
1253\r
1254 InsertTailList(&ShellInfoObject.ViewingSettings.CommandHistory.Link, &Node->Link);\r
1255}\r
1256\r
1257/**\r
1258 Checks if a string is an alias for another command. If yes, then it replaces the alias name\r
1259 with the correct command name.\r
1260\r
4ff7e37b
ED
1261 @param[in, out] CommandString Upon entry the potential alias. Upon return the\r
1262 command name if it was an alias. If it was not\r
1263 an alias it will be unchanged. This function may\r
1264 change the buffer to fit the command name.\r
a405b86d 1265\r
1266 @retval EFI_SUCCESS The name was changed.\r
1267 @retval EFI_SUCCESS The name was not an alias.\r
1268 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.\r
1269**/\r
1270EFI_STATUS\r
1271EFIAPI\r
1272ShellConvertAlias(\r
1273 IN OUT CHAR16 **CommandString\r
1274 )\r
1275{\r
1276 CONST CHAR16 *NewString;\r
1277\r
1278 NewString = ShellInfoObject.NewEfiShellProtocol->GetAlias(*CommandString, NULL);\r
1279 if (NewString == NULL) {\r
1280 return (EFI_SUCCESS);\r
1281 }\r
1282 FreePool(*CommandString);\r
733f138d 1283 *CommandString = AllocateZeroPool(StrSize(NewString));\r
a405b86d 1284 if (*CommandString == NULL) {\r
1285 return (EFI_OUT_OF_RESOURCES);\r
1286 }\r
1287 StrCpy(*CommandString, NewString);\r
1288 return (EFI_SUCCESS);\r
1289}\r
1290\r
d5b5440b
JC
1291/**\r
1292 Parse for the next instance of one string within another string. Can optionally make sure that \r
1293 the string was not escaped (^ character) per the shell specification.\r
1294\r
1295 @param[in] SourceString The string to search within\r
1296 @param[in] FindString The string to look for\r
1297 @param[in] CheckForEscapeCharacter TRUE to skip escaped instances of FinfString, otherwise will return even escaped instances\r
1298**/\r
1299CHAR16*\r
1300EFIAPI\r
1301FindNextInstance(\r
1302 IN CONST CHAR16 *SourceString,\r
1303 IN CONST CHAR16 *FindString,\r
1304 IN CONST BOOLEAN CheckForEscapeCharacter\r
1305 )\r
1306{\r
1307 CHAR16 *Temp;\r
1308 if (SourceString == NULL) {\r
1309 return (NULL);\r
1310 }\r
1311 Temp = StrStr(SourceString, FindString);\r
1312\r
1313 //\r
1314 // If nothing found, or we dont care about escape characters\r
1315 //\r
1316 if (Temp == NULL || !CheckForEscapeCharacter) {\r
1317 return (Temp);\r
1318 }\r
1319\r
1320 //\r
1321 // If we found an escaped character, try again on the remainder of the string\r
1322 //\r
1323 if ((Temp > (SourceString)) && *(Temp-1) == L'^') {\r
1324 return FindNextInstance(Temp+1, FindString, CheckForEscapeCharacter);\r
1325 }\r
1326\r
1327 //\r
1328 // we found the right character\r
1329 //\r
1330 return (Temp);\r
1331}\r
1332\r
1333/**\r
1334 This function will eliminate unreplaced (and therefore non-found) environment variables.\r
1335\r
1336 @param[in,out] CmdLine The command line to update.\r
1337**/\r
1338EFI_STATUS\r
1339EFIAPI\r
1340StripUnreplacedEnvironmentVariables(\r
1341 IN OUT CHAR16 *CmdLine\r
1342 )\r
1343{\r
1344 CHAR16 *FirstPercent;\r
1345 CHAR16 *FirstQuote;\r
1346 CHAR16 *SecondPercent;\r
1347 CHAR16 *SecondQuote;\r
1348 CHAR16 *CurrentLocator;\r
1349\r
1350 for (CurrentLocator = CmdLine ; CurrentLocator != NULL ; ) {\r
1351 FirstQuote = FindNextInstance(CurrentLocator, L"\"", TRUE);\r
1352 FirstPercent = FindNextInstance(CurrentLocator, L"%", TRUE);\r
1353 SecondPercent = FirstPercent!=NULL?FindNextInstance(FirstPercent+1, L"%", TRUE):NULL;\r
1354 if (FirstPercent == NULL || SecondPercent == NULL) {\r
1355 //\r
1356 // If we ever dont have 2 % we are done.\r
1357 //\r
1358 break;\r
1359 }\r
1360\r
1361 if (FirstQuote < FirstPercent) {\r
1362 SecondQuote = FirstQuote!= NULL?FindNextInstance(FirstQuote+1, L"\"", TRUE):NULL;\r
1363 //\r
1364 // Quote is first found\r
1365 //\r
1366\r
1367 if (SecondQuote < FirstPercent) {\r
1368 //\r
1369 // restart after the pair of "\r
1370 //\r
1371 CurrentLocator = SecondQuote + 1;\r
1372 } else /* FirstPercent < SecondQuote */{\r
1373 //\r
1374 // Restart on the first percent\r
1375 //\r
1376 CurrentLocator = FirstPercent;\r
1377 }\r
1378 continue;\r
1379 }\r
1380 ASSERT(FirstPercent < FirstQuote);\r
1381 if (SecondPercent < FirstQuote) {\r
0c41d28e
JC
1382 FirstPercent[0] = L'\"';\r
1383 SecondPercent[0] = L'\"';\r
d5b5440b
JC
1384\r
1385 //\r
0c41d28e 1386 // We need to remove from FirstPercent to SecondPercent\r
d5b5440b 1387 //\r
0c41d28e
JC
1388 CopyMem(FirstPercent + 1, SecondPercent, StrSize(SecondPercent));\r
1389 CurrentLocator = FirstPercent + 2;\r
d5b5440b
JC
1390 continue;\r
1391 }\r
1392 ASSERT(FirstQuote < SecondPercent);\r
1393 CurrentLocator = FirstQuote;\r
1394 }\r
1395 return (EFI_SUCCESS);\r
1396}\r
1397\r
a405b86d 1398/**\r
1399 Function allocates a new command line and replaces all instances of environment\r
1400 variable names that are correctly preset to their values.\r
1401\r
1402 If the return value is not NULL the memory must be caller freed.\r
1403\r
1404 @param[in] OriginalCommandLine The original command line\r
1405\r
1406 @retval NULL An error ocurred.\r
1407 @return The new command line with no environment variables present.\r
1408**/\r
1409CHAR16*\r
1410EFIAPI\r
1411ShellConvertVariables (\r
1412 IN CONST CHAR16 *OriginalCommandLine\r
1413 )\r
1414{\r
1415 CONST CHAR16 *MasterEnvList;\r
1416 UINTN NewSize;\r
1417 CHAR16 *NewCommandLine1;\r
1418 CHAR16 *NewCommandLine2;\r
1419 CHAR16 *Temp;\r
1420 UINTN ItemSize;\r
1421 CHAR16 *ItemTemp;\r
1422 SCRIPT_FILE *CurrentScriptFile;\r
1423 ALIAS_LIST *AliasListNode;\r
1424\r
1425 ASSERT(OriginalCommandLine != NULL);\r
1426\r
1427 ItemSize = 0;\r
1428 NewSize = StrSize(OriginalCommandLine);\r
1429 CurrentScriptFile = ShellCommandGetCurrentScriptFile();\r
1430 Temp = NULL;\r
1431\r
1432 ///@todo update this to handle the %0 - %9 for scripting only (borrow from line 1256 area) ? ? ?\r
1433\r
1434 //\r
1435 // calculate the size required for the post-conversion string...\r
1436 //\r
1437 if (CurrentScriptFile != NULL) {\r
1438 for (AliasListNode = (ALIAS_LIST*)GetFirstNode(&CurrentScriptFile->SubstList)\r
1439 ; !IsNull(&CurrentScriptFile->SubstList, &AliasListNode->Link)\r
1440 ; AliasListNode = (ALIAS_LIST*)GetNextNode(&CurrentScriptFile->SubstList, &AliasListNode->Link)\r
1441 ){\r
1442 for (Temp = StrStr(OriginalCommandLine, AliasListNode->Alias)\r
1443 ; Temp != NULL\r
1444 ; Temp = StrStr(Temp+1, AliasListNode->Alias)\r
1445 ){\r
1446 //\r
1447 // we need a preceeding and if there is space no ^ preceeding (if no space ignore)\r
1448 //\r
1449 if ((((Temp-OriginalCommandLine)>2) && *(Temp-2) != L'^') || ((Temp-OriginalCommandLine)<=2)) {\r
1450 NewSize += StrSize(AliasListNode->CommandString);\r
1451 }\r
1452 }\r
1453 }\r
1454 }\r
1455\r
1456 for (MasterEnvList = EfiShellGetEnv(NULL)\r
1457 ; MasterEnvList != NULL && *MasterEnvList != CHAR_NULL //&& *(MasterEnvList+1) != CHAR_NULL\r
1458 ; MasterEnvList += StrLen(MasterEnvList) + 1\r
1459 ){\r
1460 if (StrSize(MasterEnvList) > ItemSize) {\r
1461 ItemSize = StrSize(MasterEnvList);\r
1462 }\r
1463 for (Temp = StrStr(OriginalCommandLine, MasterEnvList)\r
1464 ; Temp != NULL\r
1465 ; Temp = StrStr(Temp+1, MasterEnvList)\r
1466 ){\r
1467 //\r
1468 // we need a preceeding and following % and if there is space no ^ preceeding (if no space ignore)\r
1469 //\r
1470 if (*(Temp-1) == L'%' && *(Temp+StrLen(MasterEnvList)) == L'%' &&\r
1471 ((((Temp-OriginalCommandLine)>2) && *(Temp-2) != L'^') || ((Temp-OriginalCommandLine)<=2))) {\r
1472 NewSize+=StrSize(EfiShellGetEnv(MasterEnvList));\r
1473 }\r
1474 }\r
1475 }\r
1476\r
a405b86d 1477 //\r
1478 // now do the replacements...\r
1479 //\r
1480 NewCommandLine1 = AllocateZeroPool(NewSize);\r
1481 NewCommandLine2 = AllocateZeroPool(NewSize);\r
1482 ItemTemp = AllocateZeroPool(ItemSize+(2*sizeof(CHAR16)));\r
3c865f20 1483 if (NewCommandLine1 == NULL || NewCommandLine2 == NULL || ItemTemp == NULL) {\r
1484 SHELL_FREE_NON_NULL(NewCommandLine1);\r
1485 SHELL_FREE_NON_NULL(NewCommandLine2);\r
1486 SHELL_FREE_NON_NULL(ItemTemp);\r
1487 return (NULL);\r
1488 }\r
a405b86d 1489 StrCpy(NewCommandLine1, OriginalCommandLine);\r
1490 for (MasterEnvList = EfiShellGetEnv(NULL)\r
1491 ; MasterEnvList != NULL && *MasterEnvList != CHAR_NULL //&& *(MasterEnvList+1) != CHAR_NULL\r
1492 ; MasterEnvList += StrLen(MasterEnvList) + 1\r
1493 ){\r
1494 StrCpy(ItemTemp, L"%");\r
1495 StrCat(ItemTemp, MasterEnvList);\r
1496 StrCat(ItemTemp, L"%");\r
1497 ShellCopySearchAndReplace(NewCommandLine1, NewCommandLine2, NewSize, ItemTemp, EfiShellGetEnv(MasterEnvList), TRUE, FALSE);\r
1498 StrCpy(NewCommandLine1, NewCommandLine2);\r
1499 }\r
1500 if (CurrentScriptFile != NULL) {\r
1501 for (AliasListNode = (ALIAS_LIST*)GetFirstNode(&CurrentScriptFile->SubstList)\r
1502 ; !IsNull(&CurrentScriptFile->SubstList, &AliasListNode->Link)\r
1503 ; AliasListNode = (ALIAS_LIST*)GetNextNode(&CurrentScriptFile->SubstList, &AliasListNode->Link)\r
1504 ){\r
1505 ShellCopySearchAndReplace(NewCommandLine1, NewCommandLine2, NewSize, AliasListNode->Alias, AliasListNode->CommandString, TRUE, FALSE);\r
1506 StrCpy(NewCommandLine1, NewCommandLine2);\r
1507 }\r
df07baea
JC
1508\r
1509 //\r
1510 // Remove non-existant environment variables in scripts only\r
1511 //\r
d5b5440b 1512 StripUnreplacedEnvironmentVariables(NewCommandLine1);\r
a405b86d 1513 }\r
1514\r
df07baea
JC
1515 //\r
1516 // Now cleanup any straggler intentionally ignored "%" characters\r
1517 //\r
1518 ShellCopySearchAndReplace(NewCommandLine1, NewCommandLine2, NewSize, L"^%", L"%", TRUE, FALSE);\r
1519 StrCpy(NewCommandLine1, NewCommandLine2);\r
1520 \r
a405b86d 1521 FreePool(NewCommandLine2);\r
1522 FreePool(ItemTemp);\r
1523\r
1524 return (NewCommandLine1);\r
1525}\r
1526\r
1527/**\r
1528 Internal function to run a command line with pipe usage.\r
1529\r
1530 @param[in] CmdLine The pointer to the command line.\r
1531 @param[in] StdIn The pointer to the Standard input.\r
1532 @param[in] StdOut The pointer to the Standard output.\r
1533\r
5223c121
BJ
1534 @param[out] ExitStatus The exit code of the last command in the pipeline.\r
1535 Ignored if NULL.\r
1536\r
a405b86d 1537 @retval EFI_SUCCESS The split command is executed successfully.\r
1538 @retval other Some error occurs when executing the split command.\r
1539**/\r
1540EFI_STATUS\r
1541EFIAPI\r
1542RunSplitCommand(\r
1543 IN CONST CHAR16 *CmdLine,\r
1544 IN SHELL_FILE_HANDLE *StdIn,\r
5223c121
BJ
1545 IN SHELL_FILE_HANDLE *StdOut,\r
1546 OUT SHELL_STATUS *ExitStatus\r
a405b86d 1547 )\r
1548{\r
1549 EFI_STATUS Status;\r
1550 CHAR16 *NextCommandLine;\r
1551 CHAR16 *OurCommandLine;\r
1552 UINTN Size1;\r
1553 UINTN Size2;\r
1554 SPLIT_LIST *Split;\r
1555 SHELL_FILE_HANDLE *TempFileHandle;\r
1556 BOOLEAN Unicode;\r
1557\r
1558 ASSERT(StdOut == NULL);\r
1559\r
1560 ASSERT(StrStr(CmdLine, L"|") != NULL);\r
1561\r
1562 Status = EFI_SUCCESS;\r
1563 NextCommandLine = NULL;\r
1564 OurCommandLine = NULL;\r
1565 Size1 = 0;\r
1566 Size2 = 0;\r
1567\r
1568 NextCommandLine = StrnCatGrow(&NextCommandLine, &Size1, StrStr(CmdLine, L"|")+1, 0);\r
1569 OurCommandLine = StrnCatGrow(&OurCommandLine , &Size2, CmdLine , StrStr(CmdLine, L"|") - CmdLine);\r
532691c8 1570\r
1571 if (NextCommandLine == NULL || OurCommandLine == NULL) {\r
1572 SHELL_FREE_NON_NULL(OurCommandLine);\r
1573 SHELL_FREE_NON_NULL(NextCommandLine);\r
1574 return (EFI_OUT_OF_RESOURCES);\r
19216573 1575 } else if (StrStr(OurCommandLine, L"|") != NULL || Size1 == 0 || Size2 == 0) {\r
1576 SHELL_FREE_NON_NULL(OurCommandLine);\r
1577 SHELL_FREE_NON_NULL(NextCommandLine);\r
1578 return (EFI_INVALID_PARAMETER);\r
1579 } else if (NextCommandLine[0] != CHAR_NULL &&\r
a405b86d 1580 NextCommandLine[0] == L'a' &&\r
1581 NextCommandLine[1] == L' '\r
1582 ){\r
1583 CopyMem(NextCommandLine, NextCommandLine+1, StrSize(NextCommandLine) - sizeof(NextCommandLine[0]));\r
1584 Unicode = FALSE;\r
1585 } else {\r
1586 Unicode = TRUE;\r
1587 }\r
1588\r
1589\r
1590 //\r
1591 // make a SPLIT_LIST item and add to list\r
1592 //\r
1593 Split = AllocateZeroPool(sizeof(SPLIT_LIST));\r
1594 ASSERT(Split != NULL);\r
1595 Split->SplitStdIn = StdIn;\r
1596 Split->SplitStdOut = ConvertEfiFileProtocolToShellHandle(CreateFileInterfaceMem(Unicode), NULL);\r
1597 ASSERT(Split->SplitStdOut != NULL);\r
1598 InsertHeadList(&ShellInfoObject.SplitList.Link, &Split->Link);\r
1599\r
5223c121 1600 Status = RunCommand(OurCommandLine, NULL);\r
a405b86d 1601\r
1602 //\r
1603 // move the output from the first to the in to the second.\r
1604 //\r
1605 TempFileHandle = Split->SplitStdOut;\r
1606 if (Split->SplitStdIn == StdIn) {\r
1607 Split->SplitStdOut = NULL;\r
1608 } else {\r
1609 Split->SplitStdOut = Split->SplitStdIn;\r
1610 }\r
1611 Split->SplitStdIn = TempFileHandle;\r
1612 ShellInfoObject.NewEfiShellProtocol->SetFilePosition(ConvertShellHandleToEfiFileProtocol(Split->SplitStdIn), 0);\r
1613\r
1614 if (!EFI_ERROR(Status)) {\r
5223c121 1615 Status = RunCommand(NextCommandLine, ExitStatus);\r
a405b86d 1616 }\r
1617\r
1618 //\r
1619 // remove the top level from the ScriptList\r
1620 //\r
1621 ASSERT((SPLIT_LIST*)GetFirstNode(&ShellInfoObject.SplitList.Link) == Split);\r
1622 RemoveEntryList(&Split->Link);\r
1623\r
1624 //\r
1625 // Note that the original StdIn is now the StdOut...\r
1626 //\r
1627 if (Split->SplitStdOut != NULL && Split->SplitStdOut != StdIn) {\r
1628 ShellInfoObject.NewEfiShellProtocol->CloseFile(ConvertShellHandleToEfiFileProtocol(Split->SplitStdOut));\r
1629 }\r
1630 if (Split->SplitStdIn != NULL) {\r
1631 ShellInfoObject.NewEfiShellProtocol->CloseFile(ConvertShellHandleToEfiFileProtocol(Split->SplitStdIn));\r
1632 }\r
1633\r
1634 FreePool(Split);\r
1635 FreePool(NextCommandLine);\r
1636 FreePool(OurCommandLine);\r
1637\r
1638 return (Status);\r
1639}\r
1640\r
1ef61d03
JC
1641/**\r
1642 Take the original command line, substitute any variables, free \r
51429264 1643 the original string, return the modified copy.\r
1ef61d03 1644\r
51429264 1645 @param[in] CmdLine pointer to the command line to update.\r
1ef61d03 1646\r
51429264
SQ
1647 @retval EFI_SUCCESS the function was successful.\r
1648 @retval EFI_OUT_OF_RESOURCES a memory allocation failed.\r
1ef61d03
JC
1649**/\r
1650EFI_STATUS\r
1651EFIAPI\r
1652ShellSubstituteVariables(\r
1653 IN CHAR16 **CmdLine\r
1654 )\r
1655{\r
1656 CHAR16 *NewCmdLine;\r
1657 NewCmdLine = ShellConvertVariables(*CmdLine);\r
1658 SHELL_FREE_NON_NULL(*CmdLine);\r
1659 if (NewCmdLine == NULL) {\r
1660 return (EFI_OUT_OF_RESOURCES);\r
1661 }\r
1662 *CmdLine = NewCmdLine;\r
1663 return (EFI_SUCCESS);\r
1664}\r
1665\r
ca53c0af
JC
1666/**\r
1667 Take the original command line, substitute any alias in the first group of space delimited characters, free \r
51429264 1668 the original string, return the modified copy.\r
ca53c0af 1669\r
51429264 1670 @param[in] CmdLine pointer to the command line to update.\r
ca53c0af 1671\r
51429264
SQ
1672 @retval EFI_SUCCESS the function was successful.\r
1673 @retval EFI_OUT_OF_RESOURCES a memory allocation failed.\r
ca53c0af
JC
1674**/\r
1675EFI_STATUS\r
1676EFIAPI\r
1677ShellSubstituteAliases(\r
1678 IN CHAR16 **CmdLine\r
1679 )\r
1680{\r
1681 CHAR16 *NewCmdLine;\r
1682 CHAR16 *CommandName;\r
1683 EFI_STATUS Status;\r
1684 UINTN PostAliasSize;\r
1685 ASSERT(CmdLine != NULL);\r
1686 ASSERT(*CmdLine!= NULL);\r
1687\r
1688\r
1689 CommandName = NULL;\r
1690 if (StrStr((*CmdLine), L" ") == NULL){\r
1691 StrnCatGrow(&CommandName, NULL, (*CmdLine), 0);\r
1692 } else {\r
1693 StrnCatGrow(&CommandName, NULL, (*CmdLine), StrStr((*CmdLine), L" ") - (*CmdLine));\r
1694 }\r
1695\r
1696 //\r
1697 // This cannot happen 'inline' since the CmdLine can need extra space.\r
1698 //\r
1699 NewCmdLine = NULL;\r
1700 if (!ShellCommandIsCommandOnList(CommandName)) {\r
1701 //\r
1702 // Convert via alias\r
1703 //\r
1704 Status = ShellConvertAlias(&CommandName);\r
1705 if (EFI_ERROR(Status)){\r
1706 return (Status);\r
1707 }\r
1708 PostAliasSize = 0;\r
1709 NewCmdLine = StrnCatGrow(&NewCmdLine, &PostAliasSize, CommandName, 0);\r
1710 if (NewCmdLine == NULL) {\r
1711 SHELL_FREE_NON_NULL(CommandName);\r
1712 SHELL_FREE_NON_NULL(*CmdLine);\r
1713 return (EFI_OUT_OF_RESOURCES);\r
1714 }\r
1715 NewCmdLine = StrnCatGrow(&NewCmdLine, &PostAliasSize, StrStr((*CmdLine), L" "), 0);\r
1716 if (NewCmdLine == NULL) {\r
1717 SHELL_FREE_NON_NULL(CommandName);\r
1718 SHELL_FREE_NON_NULL(*CmdLine);\r
1719 return (EFI_OUT_OF_RESOURCES);\r
1720 }\r
1721 } else {\r
1722 NewCmdLine = StrnCatGrow(&NewCmdLine, NULL, (*CmdLine), 0);\r
1723 }\r
1724\r
1725 SHELL_FREE_NON_NULL(*CmdLine);\r
1726 SHELL_FREE_NON_NULL(CommandName);\r
1727 \r
1728 //\r
1729 // re-assign the passed in double pointer to point to our newly allocated buffer\r
1730 //\r
1731 *CmdLine = NewCmdLine;\r
1732\r
1733 return (EFI_SUCCESS);\r
1734}\r
1735\r
6ba2921d
JC
1736/**\r
1737 Takes the Argv[0] part of the command line and determine the meaning of it.\r
e7831c90 1738\r
51429264 1739 @param[in] CmdName pointer to the command line to update.\r
e7831c90 1740 \r
51429264
SQ
1741 @retval Internal_Command The name is an internal command.\r
1742 @retval File_Sys_Change the name is a file system change.\r
1743 @retval Script_File_Name the name is a NSH script file.\r
1744 @retval Unknown_Invalid the name is unknown.\r
1745 @retval Efi_Application the name is an application (.EFI).\r
6ba2921d
JC
1746**/\r
1747SHELL_OPERATION_TYPES\r
1748EFIAPI\r
1749GetOperationType(\r
1750 IN CONST CHAR16 *CmdName\r
1751 )\r
1752{\r
1753 CHAR16* FileWithPath;\r
1754 CONST CHAR16* TempLocation;\r
1755 CONST CHAR16* TempLocation2;\r
1756\r
1757 FileWithPath = NULL;\r
1758 //\r
1759 // test for an internal command.\r
1760 //\r
1761 if (ShellCommandIsCommandOnList(CmdName)) {\r
51429264 1762 return (Internal_Command);\r
6ba2921d
JC
1763 }\r
1764\r
1765 //\r
fc4c7b30 1766 // Test for file system change request. anything ending with first : and cant have spaces.\r
6ba2921d
JC
1767 //\r
1768 if (CmdName[(StrLen(CmdName)-1)] == L':') {\r
fc4c7b30
JC
1769 if ( StrStr(CmdName, L" ") != NULL \r
1770 || StrLen(StrStr(CmdName, L":")) > 1\r
1771 ) {\r
51429264 1772 return (Unknown_Invalid);\r
6ba2921d 1773 }\r
51429264 1774 return (File_Sys_Change);\r
6ba2921d
JC
1775 }\r
1776\r
1777 //\r
1778 // Test for a file\r
1779 //\r
1780 if ((FileWithPath = ShellFindFilePathEx(CmdName, mExecutableExtensions)) != NULL) {\r
1781 //\r
1782 // See if that file has a script file extension\r
1783 //\r
1784 if (StrLen(FileWithPath) > 4) {\r
1785 TempLocation = FileWithPath+StrLen(FileWithPath)-4;\r
1786 TempLocation2 = mScriptExtension;\r
1787 if (StringNoCaseCompare((VOID*)(&TempLocation), (VOID*)(&TempLocation2)) == 0) {\r
1788 SHELL_FREE_NON_NULL(FileWithPath);\r
51429264 1789 return (Script_File_Name);\r
6ba2921d
JC
1790 }\r
1791 }\r
1792\r
1793 //\r
1794 // Was a file, but not a script. we treat this as an application.\r
1795 //\r
1796 SHELL_FREE_NON_NULL(FileWithPath);\r
51429264 1797 return (Efi_Application);\r
6ba2921d
JC
1798 }\r
1799 \r
1800 SHELL_FREE_NON_NULL(FileWithPath);\r
1801 //\r
1802 // No clue what this is... return invalid flag...\r
1803 //\r
51429264 1804 return (Unknown_Invalid);\r
6ba2921d
JC
1805}\r
1806\r
de4ebdcf
SQ
1807/**\r
1808 Determine if the first item in a command line is valid.\r
1809\r
1810 @param[in] CmdLine The command line to parse.\r
1811\r
1812 @retval EFI_SUCCESS The item is valid.\r
1813 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.\r
1814 @retval EFI_NOT_FOUND The operation type is unknown or invalid.\r
1815**/\r
6bd64463
JC
1816EFI_STATUS \r
1817EFIAPI\r
1818IsValidSplit(\r
1819 IN CONST CHAR16 *CmdLine\r
1820 )\r
1821{\r
1822 CHAR16 *Temp;\r
1823 CHAR16 *FirstParameter;\r
1824 CHAR16 *TempWalker;\r
1825 EFI_STATUS Status;\r
1826\r
1827 Temp = NULL;\r
1828\r
1829 Temp = StrnCatGrow(&Temp, NULL, CmdLine, 0);\r
1830 if (Temp == NULL) {\r
1831 return (EFI_OUT_OF_RESOURCES);\r
1832 }\r
1833\r
1834 FirstParameter = StrStr(Temp, L"|");\r
1835 if (FirstParameter != NULL) {\r
1836 *FirstParameter = CHAR_NULL;\r
1837 }\r
1838\r
1839 FirstParameter = NULL;\r
1840\r
1841 //\r
1842 // Process the command line\r
1843 //\r
1844 Status = ProcessCommandLineToFinal(&Temp);\r
1845\r
1846 if (!EFI_ERROR(Status)) {\r
1847 FirstParameter = AllocateZeroPool(StrSize(CmdLine));\r
1848 if (FirstParameter == NULL) {\r
1849 SHELL_FREE_NON_NULL(Temp);\r
1850 return (EFI_OUT_OF_RESOURCES);\r
1851 }\r
1852 TempWalker = (CHAR16*)Temp;\r
1853 GetNextParameter(&TempWalker, &FirstParameter);\r
1854\r
51429264 1855 if (GetOperationType(FirstParameter) == Unknown_Invalid) {\r
6bd64463
JC
1856 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_NOT_FOUND), ShellInfoObject.HiiHandle, FirstParameter);\r
1857 SetLastError(SHELL_NOT_FOUND);\r
1858 Status = EFI_NOT_FOUND;\r
1859 }\r
1860 }\r
1861\r
1862 SHELL_FREE_NON_NULL(Temp);\r
1863 SHELL_FREE_NON_NULL(FirstParameter);\r
1864 return Status;\r
1865}\r
1866\r
1867/**\r
51429264 1868 Determine if a command line contains with a split contains only valid commands.\r
6bd64463
JC
1869\r
1870 @param[in] CmdLine The command line to parse.\r
1871\r
1872 @retval EFI_SUCCESS CmdLine has only valid commands, application, or has no split.\r
1873 @retval EFI_ABORTED CmdLine has at least one invalid command or application.\r
1874**/\r
1875EFI_STATUS\r
1876EFIAPI\r
1877VerifySplit(\r
1878 IN CONST CHAR16 *CmdLine\r
1879 )\r
1880{\r
1881 CONST CHAR16 *TempSpot;\r
1882 EFI_STATUS Status;\r
1883\r
1884 //\r
1885 // Verify up to the pipe or end character\r
1886 //\r
1887 Status = IsValidSplit(CmdLine);\r
1888 if (EFI_ERROR(Status)) {\r
1889 return (Status);\r
1890 }\r
1891\r
1892 //\r
1893 // If this was the only item, then get out\r
1894 //\r
1895 if (!ContainsSplit(CmdLine)) {\r
1896 return (EFI_SUCCESS);\r
1897 }\r
1898\r
1899 //\r
1900 // recurse to verify the next item\r
1901 //\r
1902 TempSpot = FindSplit(CmdLine)+1;\r
1903 return (VerifySplit(TempSpot));\r
1904}\r
1905\r
680db511 1906/**\r
e7831c90
JC
1907 Process a split based operation.\r
1908\r
5223c121
BJ
1909 @param[in] CmdLine Pointer to the command line to process\r
1910 @param[out] ExitStatus The exit status of the command. Ignored if NULL.\r
1911 Invalid if this function returns an error.\r
e7831c90
JC
1912\r
1913 @retval EFI_SUCCESS The operation was successful\r
1914 @return an error occured.\r
680db511
JC
1915**/\r
1916EFI_STATUS\r
1917EFIAPI\r
1918ProcessNewSplitCommandLine(\r
5223c121
BJ
1919 IN CONST CHAR16 *CmdLine,\r
1920 OUT SHELL_STATUS *ExitStatus\r
680db511
JC
1921 )\r
1922{\r
1923 SPLIT_LIST *Split;\r
1924 EFI_STATUS Status;\r
1925\r
6bd64463
JC
1926 Status = VerifySplit(CmdLine);\r
1927 if (EFI_ERROR(Status)) {\r
1928 return (Status);\r
1929 }\r
1930\r
680db511
JC
1931 Split = NULL;\r
1932\r
1933 //\r
1934 // are we in an existing split???\r
1935 //\r
1936 if (!IsListEmpty(&ShellInfoObject.SplitList.Link)) {\r
1937 Split = (SPLIT_LIST*)GetFirstNode(&ShellInfoObject.SplitList.Link);\r
1938 }\r
1939\r
1940 if (Split == NULL) {\r
5223c121 1941 Status = RunSplitCommand(CmdLine, NULL, NULL, ExitStatus);\r
680db511 1942 } else {\r
5223c121
BJ
1943 Status = RunSplitCommand(\r
1944 CmdLine,\r
1945 Split->SplitStdIn,\r
1946 Split->SplitStdOut,\r
1947 ExitStatus\r
1948 );\r
680db511
JC
1949 }\r
1950 if (EFI_ERROR(Status)) {\r
1951 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_INVALID_SPLIT), ShellInfoObject.HiiHandle, CmdLine);\r
1952 }\r
1953 return (Status);\r
1954}\r
daf70584
JC
1955\r
1956/**\r
51429264 1957 Handle a request to change the current file system.\r
daf70584 1958\r
51429264 1959 @param[in] CmdLine The passed in command line.\r
daf70584 1960\r
51429264 1961 @retval EFI_SUCCESS The operation was successful.\r
daf70584
JC
1962**/\r
1963EFI_STATUS\r
1964EFIAPI\r
1965ChangeMappedDrive(\r
1966 IN CONST CHAR16 *CmdLine\r
1967 )\r
1968{\r
1969 EFI_STATUS Status;\r
1970 Status = EFI_SUCCESS;\r
1971\r
1972 //\r
1973 // make sure we are the right operation\r
1974 //\r
1975 ASSERT(CmdLine[(StrLen(CmdLine)-1)] == L':' && StrStr(CmdLine, L" ") == NULL);\r
1976 \r
1977 //\r
1978 // Call the protocol API to do the work\r
1979 //\r
1980 Status = ShellInfoObject.NewEfiShellProtocol->SetCurDir(NULL, CmdLine);\r
1981\r
1982 //\r
1983 // Report any errors\r
1984 //\r
1985 if (EFI_ERROR(Status)) {\r
1986 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_INVALID_MAPPING), ShellInfoObject.HiiHandle, CmdLine);\r
1987 }\r
1988\r
1989 return (Status);\r
1990}\r
1991\r
5a5eb806
JC
1992/**\r
1993 Reprocess the command line to direct all -? to the help command.\r
1994\r
1995 if found, will add "help" as argv[0], and move the rest later.\r
1996\r
806c49db 1997 @param[in,out] CmdLine pointer to the command line to update\r
5a5eb806
JC
1998**/\r
1999EFI_STATUS\r
2000EFIAPI\r
806c49db
JC
2001DoHelpUpdate(\r
2002 IN OUT CHAR16 **CmdLine\r
5a5eb806
JC
2003 )\r
2004{\r
806c49db
JC
2005 CHAR16 *CurrentParameter;\r
2006 CHAR16 *Walker;\r
2007 CHAR16 *LastWalker;\r
2008 CHAR16 *NewCommandLine;\r
2009 EFI_STATUS Status;\r
2010\r
2011 Status = EFI_SUCCESS;\r
2012\r
2013 CurrentParameter = AllocateZeroPool(StrSize(*CmdLine));\r
2014 if (CurrentParameter == NULL) {\r
2015 return (EFI_OUT_OF_RESOURCES);\r
2016 }\r
2017\r
2018 Walker = *CmdLine;\r
2019 while(Walker != NULL && *Walker != CHAR_NULL) {\r
2020 LastWalker = Walker;\r
2021 GetNextParameter(&Walker, &CurrentParameter);\r
2022 if (StrStr(CurrentParameter, L"-?") == CurrentParameter) {\r
2023 LastWalker[0] = L' ';\r
2024 LastWalker[1] = L' ';\r
2025 NewCommandLine = AllocateZeroPool(StrSize(L"help ") + StrSize(*CmdLine));\r
2026 if (NewCommandLine == NULL) {\r
2027 Status = EFI_OUT_OF_RESOURCES;\r
2028 break;\r
2029 }\r
2030 StrCpy(NewCommandLine, L"help ");\r
2031 StrCat(NewCommandLine, *CmdLine);\r
2032 SHELL_FREE_NON_NULL(*CmdLine);\r
2033 *CmdLine = NewCommandLine;\r
2034 break;\r
2035 }\r
2036 }\r
2037\r
2038 SHELL_FREE_NON_NULL(CurrentParameter);\r
2039\r
2040 return (Status);\r
2041}\r
2042\r
2043/**\r
51429264 2044 Function to update the shell variable "lasterror".\r
806c49db 2045\r
51429264 2046 @param[in] ErrorCode the error code to put into lasterror.\r
806c49db
JC
2047**/\r
2048EFI_STATUS\r
2049EFIAPI\r
2050SetLastError(\r
6bd64463 2051 IN CONST SHELL_STATUS ErrorCode\r
806c49db
JC
2052 )\r
2053{\r
2054 CHAR16 LeString[19];\r
2055 if (sizeof(EFI_STATUS) == sizeof(UINT64)) {\r
2056 UnicodeSPrint(LeString, sizeof(LeString), L"0x%Lx", ErrorCode);\r
2057 } else {\r
2058 UnicodeSPrint(LeString, sizeof(LeString), L"0x%x", ErrorCode);\r
2059 }\r
2060 DEBUG_CODE(InternalEfiShellSetEnv(L"debuglasterror", LeString, TRUE););\r
2061 InternalEfiShellSetEnv(L"lasterror", LeString, TRUE);\r
2062\r
2063 return (EFI_SUCCESS);\r
2064}\r
2065\r
2066/**\r
2067 Converts the command line to it's post-processed form. this replaces variables and alias' per UEFI Shell spec.\r
2068\r
2069 @param[in,out] CmdLine pointer to the command line to update\r
2070\r
2071 @retval EFI_SUCCESS The operation was successful\r
2072 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.\r
2073 @return some other error occured\r
2074**/\r
2075EFI_STATUS\r
2076EFIAPI\r
12a27a6d 2077ProcessCommandLineToFinal(\r
806c49db
JC
2078 IN OUT CHAR16 **CmdLine\r
2079 )\r
2080{\r
2081 EFI_STATUS Status;\r
2082 TrimSpaces(CmdLine);\r
2083\r
2084 Status = ShellSubstituteAliases(CmdLine);\r
2085 if (EFI_ERROR(Status)) {\r
2086 return (Status);\r
2087 }\r
2088\r
2089 TrimSpaces(CmdLine);\r
2090\r
2091 Status = ShellSubstituteVariables(CmdLine);\r
2092 if (EFI_ERROR(Status)) {\r
2093 return (Status);\r
2094 }\r
81cd2f53 2095 ASSERT (*CmdLine != NULL);\r
806c49db
JC
2096\r
2097 TrimSpaces(CmdLine);\r
2098\r
2099 //\r
2100 // update for help parsing\r
2101 //\r
2102 if (StrStr(*CmdLine, L"?") != NULL) {\r
2103 //\r
2104 // This may do nothing if the ? does not indicate help.\r
2105 // Save all the details for in the API below.\r
2106 //\r
2107 Status = DoHelpUpdate(CmdLine);\r
2108 }\r
2109\r
2110 TrimSpaces(CmdLine);\r
2111\r
2112 return (EFI_SUCCESS);\r
2113}\r
2114\r
2115/**\r
2116 Run an internal shell command.\r
2117\r
2118 This API will upadate the shell's environment since these commands are libraries.\r
2119 \r
2120 @param[in] CmdLine the command line to run.\r
2121 @param[in] FirstParameter the first parameter on the command line\r
2122 @param[in] ParamProtocol the shell parameters protocol pointer\r
2123\r
5223c121
BJ
2124 @param[out] ExitStatus The exit code of the command. Ignored if NULL.\r
2125\r
806c49db
JC
2126 @retval EFI_SUCCESS The command was completed.\r
2127 @retval EFI_ABORTED The command's operation was aborted.\r
2128**/\r
2129EFI_STATUS\r
2130EFIAPI\r
2131RunInternalCommand(\r
2132 IN CONST CHAR16 *CmdLine,\r
2133 IN CHAR16 *FirstParameter,\r
5223c121
BJ
2134 IN EFI_SHELL_PARAMETERS_PROTOCOL *ParamProtocol,\r
2135 OUT SHELL_STATUS *ExitStatus OPTIONAL\r
806c49db
JC
2136)\r
2137{\r
2138 EFI_STATUS Status;\r
2139 UINTN Argc;\r
2140 CHAR16 **Argv;\r
2141 SHELL_STATUS CommandReturnedStatus;\r
2142 BOOLEAN LastError;\r
2143\r
5a5eb806 2144 //\r
806c49db 2145 // get the argc and argv updated for internal commands\r
5a5eb806 2146 //\r
806c49db
JC
2147 Status = UpdateArgcArgv(ParamProtocol, CmdLine, &Argv, &Argc);\r
2148 if (!EFI_ERROR(Status)) {\r
5a5eb806 2149 //\r
806c49db 2150 // Run the internal command.\r
5a5eb806 2151 //\r
806c49db
JC
2152 Status = ShellCommandRunCommandHandler(FirstParameter, &CommandReturnedStatus, &LastError);\r
2153\r
2154 if (!EFI_ERROR(Status)) {\r
5a5eb806 2155 //\r
806c49db
JC
2156 // Update last error status.\r
2157 // some commands do not update last error.\r
5a5eb806 2158 //\r
806c49db
JC
2159 if (LastError) {\r
2160 SetLastError(CommandReturnedStatus);\r
5a5eb806 2161 }\r
5223c121
BJ
2162 if (ExitStatus != NULL) {\r
2163 *ExitStatus = CommandReturnedStatus;\r
2164 }\r
806c49db
JC
2165\r
2166 //\r
2167 // Pass thru the exitcode from the app.\r
2168 //\r
2169 if (ShellCommandGetExit()) {\r
e3eb7d82
JC
2170 //\r
2171 // An Exit was requested ("exit" command), pass its value up.\r
2172 //\r
806c49db 2173 Status = CommandReturnedStatus;\r
e3eb7d82
JC
2174 } else if (CommandReturnedStatus != SHELL_SUCCESS && IsScriptOnlyCommand(FirstParameter)) {\r
2175 //\r
2176 // Always abort when a script only command fails for any reason\r
2177 //\r
2178 Status = EFI_ABORTED;\r
2179 } else if (ShellCommandGetCurrentScriptFile() != NULL && CommandReturnedStatus == SHELL_ABORTED) {\r
2180 //\r
2181 // Abort when in a script and a command aborted\r
2182 //\r
806c49db 2183 Status = EFI_ABORTED;\r
5a5eb806 2184 }\r
806c49db
JC
2185 }\r
2186 }\r
2187\r
2188 //\r
2189 // This is guarenteed to be called after UpdateArgcArgv no matter what else happened.\r
2190 // This is safe even if the update API failed. In this case, it may be a no-op.\r
2191 //\r
2192 RestoreArgcArgv(ParamProtocol, &Argv, &Argc);\r
2193\r
e3eb7d82
JC
2194 //\r
2195 // If a script is running and the command is not a scipt only command, then\r
2196 // change return value to success so the script won't halt (unless aborted).\r
2197 //\r
2198 // Script only commands have to be able halt the script since the script will\r
2199 // not operate if they are failing.\r
2200 //\r
2201 if ( ShellCommandGetCurrentScriptFile() != NULL\r
2202 && !IsScriptOnlyCommand(FirstParameter)\r
2203 && Status != EFI_ABORTED\r
2204 ) {\r
806c49db
JC
2205 Status = EFI_SUCCESS;\r
2206 }\r
2207\r
2208 return (Status);\r
2209}\r
2210\r
2211/**\r
2212 Function to run the command or file.\r
2213\r
2214 @param[in] Type the type of operation being run.\r
2215 @param[in] CmdLine the command line to run.\r
2216 @param[in] FirstParameter the first parameter on the command line\r
2217 @param[in] ParamProtocol the shell parameters protocol pointer\r
2218\r
5223c121
BJ
2219 @param[out] ExitStatus The exit code of the command or file.\r
2220 Ignored if NULL.\r
2221\r
806c49db
JC
2222 @retval EFI_SUCCESS The command was completed.\r
2223 @retval EFI_ABORTED The command's operation was aborted.\r
2224**/\r
2225EFI_STATUS\r
2226EFIAPI\r
2227RunCommandOrFile(\r
2228 IN SHELL_OPERATION_TYPES Type,\r
2229 IN CONST CHAR16 *CmdLine,\r
2230 IN CHAR16 *FirstParameter,\r
5223c121
BJ
2231 IN EFI_SHELL_PARAMETERS_PROTOCOL *ParamProtocol,\r
2232 OUT SHELL_STATUS *ExitStatus\r
806c49db
JC
2233)\r
2234{\r
2235 EFI_STATUS Status;\r
cd39fe08 2236 EFI_STATUS StartStatus;\r
806c49db
JC
2237 CHAR16 *CommandWithPath;\r
2238 EFI_DEVICE_PATH_PROTOCOL *DevPath;\r
5223c121 2239 SHELL_STATUS CalleeExitStatus;\r
806c49db
JC
2240\r
2241 Status = EFI_SUCCESS;\r
2242 CommandWithPath = NULL;\r
2243 DevPath = NULL;\r
71c49eaf 2244 CalleeExitStatus = SHELL_INVALID_PARAMETER;\r
806c49db
JC
2245\r
2246 switch (Type) {\r
51429264 2247 case Internal_Command:\r
5223c121
BJ
2248 Status = RunInternalCommand(\r
2249 CmdLine,\r
2250 FirstParameter,\r
2251 ParamProtocol,\r
2252 &CalleeExitStatus\r
2253 );\r
806c49db 2254 break;\r
51429264
SQ
2255 case Script_File_Name:\r
2256 case Efi_Application:\r
806c49db
JC
2257 //\r
2258 // Process a fully qualified path\r
2259 //\r
2260 if (StrStr(FirstParameter, L":") != NULL) {\r
2261 ASSERT (CommandWithPath == NULL);\r
2262 if (ShellIsFile(FirstParameter) == EFI_SUCCESS) {\r
2263 CommandWithPath = StrnCatGrow(&CommandWithPath, NULL, FirstParameter, 0);\r
2264 }\r
2265 }\r
2266\r
2267 //\r
2268 // Process a relative path and also check in the path environment variable\r
2269 //\r
2270 if (CommandWithPath == NULL) {\r
2271 CommandWithPath = ShellFindFilePathEx(FirstParameter, mExecutableExtensions);\r
2272 }\r
2273\r
2274 //\r
2275 // This should be impossible now.\r
2276 //\r
2277 ASSERT(CommandWithPath != NULL);\r
2278\r
2279 //\r
2280 // Make sure that path is not just a directory (or not found)\r
2281 //\r
2282 if (!EFI_ERROR(ShellIsDirectory(CommandWithPath))) {\r
2283 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_NOT_FOUND), ShellInfoObject.HiiHandle, FirstParameter);\r
6bd64463 2284 SetLastError(SHELL_NOT_FOUND);\r
806c49db
JC
2285 }\r
2286 switch (Type) {\r
51429264 2287 case Script_File_Name:\r
5223c121
BJ
2288 Status = RunScriptFile (\r
2289 CommandWithPath,\r
2290 NULL,\r
2291 CmdLine,\r
2292 ParamProtocol,\r
2293 &CalleeExitStatus\r
2294 );\r
806c49db 2295 break;\r
51429264 2296 case Efi_Application:\r
806c49db
JC
2297 //\r
2298 // Get the device path of the application image\r
2299 //\r
2300 DevPath = ShellInfoObject.NewEfiShellProtocol->GetDevicePathFromFilePath(CommandWithPath);\r
2301 if (DevPath == NULL){\r
2302 Status = EFI_OUT_OF_RESOURCES;\r
2303 break;\r
2304 }\r
2305\r
2306 //\r
2307 // Execute the device path\r
2308 //\r
2309 Status = InternalShellExecuteDevicePath(\r
2310 &gImageHandle,\r
2311 DevPath,\r
2312 CmdLine,\r
2313 NULL,\r
cd39fe08 2314 &StartStatus,\r
5223c121
BJ
2315 NULL,\r
2316 NULL\r
806c49db
JC
2317 );\r
2318\r
2319 SHELL_FREE_NON_NULL(DevPath);\r
2320\r
71c49eaf
SQ
2321 if(EFI_ERROR (Status)) {\r
2322 CalleeExitStatus = (SHELL_STATUS) (Status & (~MAX_BIT));\r
2323 } else {\r
cd39fe08 2324 CalleeExitStatus = (SHELL_STATUS) StartStatus;\r
71c49eaf
SQ
2325 }\r
2326\r
806c49db
JC
2327 //\r
2328 // Update last error status.\r
2329 //\r
5223c121 2330 // Status is an EFI_STATUS. Clear top bit to convert to SHELL_STATUS\r
71c49eaf 2331 SetLastError(CalleeExitStatus);\r
806c49db 2332 break;\r
6fa0da7d
LE
2333 default:\r
2334 //\r
2335 // Do nothing.\r
2336 //\r
2337 break;\r
5a5eb806
JC
2338 }\r
2339 break;\r
6fa0da7d
LE
2340 default:\r
2341 //\r
2342 // Do nothing.\r
2343 //\r
2344 break;\r
5a5eb806 2345 }\r
806c49db
JC
2346\r
2347 SHELL_FREE_NON_NULL(CommandWithPath);\r
2348\r
5223c121
BJ
2349 if (ExitStatus != NULL) {\r
2350 *ExitStatus = CalleeExitStatus;\r
2351 }\r
2352\r
806c49db
JC
2353 return (Status);\r
2354}\r
2355\r
2356/**\r
2357 Function to setup StdIn, StdErr, StdOut, and then run the command or file.\r
2358\r
2359 @param[in] Type the type of operation being run.\r
2360 @param[in] CmdLine the command line to run.\r
2361 @param[in] FirstParameter the first parameter on the command line.\r
2362 @param[in] ParamProtocol the shell parameters protocol pointer\r
2363\r
5223c121
BJ
2364 @param[out] ExitStatus The exit code of the command or file.\r
2365 Ignored if NULL.\r
2366\r
806c49db
JC
2367 @retval EFI_SUCCESS The command was completed.\r
2368 @retval EFI_ABORTED The command's operation was aborted.\r
2369**/\r
2370EFI_STATUS\r
2371EFIAPI\r
2372SetupAndRunCommandOrFile(\r
5223c121
BJ
2373 IN SHELL_OPERATION_TYPES Type,\r
2374 IN CHAR16 *CmdLine,\r
2375 IN CHAR16 *FirstParameter,\r
2376 IN EFI_SHELL_PARAMETERS_PROTOCOL *ParamProtocol,\r
2377 OUT SHELL_STATUS *ExitStatus\r
806c49db
JC
2378)\r
2379{\r
2380 EFI_STATUS Status;\r
2381 SHELL_FILE_HANDLE OriginalStdIn;\r
2382 SHELL_FILE_HANDLE OriginalStdOut;\r
2383 SHELL_FILE_HANDLE OriginalStdErr;\r
2384 SYSTEM_TABLE_INFO OriginalSystemTableInfo;\r
2385\r
2386 //\r
2387 // Update the StdIn, StdOut, and StdErr for redirection to environment variables, files, etc... unicode and ASCII\r
2388 //\r
2389 Status = UpdateStdInStdOutStdErr(ParamProtocol, CmdLine, &OriginalStdIn, &OriginalStdOut, &OriginalStdErr, &OriginalSystemTableInfo);\r
2390\r
2391 //\r
2392 // The StdIn, StdOut, and StdErr are set up.\r
2393 // Now run the command, script, or application\r
2394 //\r
2395 if (!EFI_ERROR(Status)) {\r
5223c121
BJ
2396 Status = RunCommandOrFile(\r
2397 Type,\r
2398 CmdLine,\r
2399 FirstParameter,\r
2400 ParamProtocol,\r
2401 ExitStatus\r
2402 );\r
806c49db
JC
2403 }\r
2404\r
2405 //\r
2406 // Now print errors\r
2407 //\r
2408 if (EFI_ERROR(Status)) {\r
2409 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_ERROR), ShellInfoObject.HiiHandle, (VOID*)(Status));\r
2410 }\r
2411\r
2412 //\r
2413 // put back the original StdIn, StdOut, and StdErr\r
2414 //\r
2415 RestoreStdInStdOutStdErr(ParamProtocol, &OriginalStdIn, &OriginalStdOut, &OriginalStdErr, &OriginalSystemTableInfo);\r
2416\r
2417 return (Status);\r
5a5eb806
JC
2418}\r
2419\r
a405b86d 2420/**\r
2421 Function will process and run a command line.\r
2422\r
2423 This will determine if the command line represents an internal shell \r
2424 command or dispatch an external application.\r
2425\r
2426 @param[in] CmdLine The command line to parse.\r
5223c121 2427 @param[out] ExitStatus The exit code of the command. Ignored if NULL.\r
a405b86d 2428\r
2429 @retval EFI_SUCCESS The command was completed.\r
2430 @retval EFI_ABORTED The command's operation was aborted.\r
2431**/\r
2432EFI_STATUS\r
2433EFIAPI\r
2434RunCommand(\r
5223c121
BJ
2435 IN CONST CHAR16 *CmdLine,\r
2436 OUT SHELL_STATUS *ExitStatus\r
a405b86d 2437 )\r
2438{\r
2439 EFI_STATUS Status;\r
a405b86d 2440 CHAR16 *CleanOriginal;\r
806c49db
JC
2441 CHAR16 *FirstParameter;\r
2442 CHAR16 *TempWalker;\r
2443 SHELL_OPERATION_TYPES Type;\r
a405b86d 2444\r
2445 ASSERT(CmdLine != NULL);\r
2446 if (StrLen(CmdLine) == 0) {\r
2447 return (EFI_SUCCESS);\r
2448 }\r
2449\r
a405b86d 2450 Status = EFI_SUCCESS;\r
2451 CleanOriginal = NULL;\r
a405b86d 2452\r
2453 CleanOriginal = StrnCatGrow(&CleanOriginal, NULL, CmdLine, 0);\r
532691c8 2454 if (CleanOriginal == NULL) {\r
2455 return (EFI_OUT_OF_RESOURCES);\r
2456 }\r
284e034f 2457\r
ad2bc854 2458 TrimSpaces(&CleanOriginal);\r
a405b86d 2459\r
284e034f
CP
2460 //\r
2461 // Handle case that passed in command line is just 1 or more " " characters.\r
2462 //\r
2463 if (StrLen (CleanOriginal) == 0) {\r
806c49db 2464 SHELL_FREE_NON_NULL(CleanOriginal);\r
284e034f
CP
2465 return (EFI_SUCCESS);\r
2466 }\r
2467\r
12a27a6d 2468 Status = ProcessCommandLineToFinal(&CleanOriginal);\r
ca53c0af 2469 if (EFI_ERROR(Status)) {\r
806c49db 2470 SHELL_FREE_NON_NULL(CleanOriginal);\r
ca53c0af 2471 return (Status);\r
a405b86d 2472 }\r
2473\r
a405b86d 2474 //\r
2475 // We dont do normal processing with a split command line (output from one command input to another)\r
2476 //\r
1ef61d03 2477 if (ContainsSplit(CleanOriginal)) {\r
5223c121 2478 Status = ProcessNewSplitCommandLine(CleanOriginal, ExitStatus);\r
806c49db
JC
2479 SHELL_FREE_NON_NULL(CleanOriginal);\r
2480 return (Status);\r
2481 } \r
5b5cd144 2482\r
806c49db
JC
2483 //\r
2484 // We need the first parameter information so we can determine the operation type\r
2485 //\r
2486 FirstParameter = AllocateZeroPool(StrSize(CleanOriginal));\r
2487 if (FirstParameter == NULL) {\r
2488 SHELL_FREE_NON_NULL(CleanOriginal);\r
2489 return (EFI_OUT_OF_RESOURCES);\r
2490 }\r
2491 TempWalker = CleanOriginal;\r
2492 GetNextParameter(&TempWalker, &FirstParameter);\r
64c7a749 2493\r
806c49db
JC
2494 //\r
2495 // Depending on the first parameter we change the behavior\r
2496 //\r
2497 switch (Type = GetOperationType(FirstParameter)) {\r
51429264 2498 case File_Sys_Change:\r
d6e88a6c 2499 Status = ChangeMappedDrive (FirstParameter);\r
806c49db 2500 break;\r
51429264
SQ
2501 case Internal_Command:\r
2502 case Script_File_Name:\r
2503 case Efi_Application:\r
5223c121
BJ
2504 Status = SetupAndRunCommandOrFile(\r
2505 Type,\r
2506 CleanOriginal,\r
2507 FirstParameter,\r
2508 ShellInfoObject.NewShellParametersProtocol,\r
2509 ExitStatus\r
2510 );\r
806c49db
JC
2511 break;\r
2512 default:\r
64c7a749 2513 //\r
806c49db 2514 // Whatever was typed, it was invalid.\r
64c7a749 2515 //\r
806c49db 2516 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_NOT_FOUND), ShellInfoObject.HiiHandle, FirstParameter);\r
6bd64463 2517 SetLastError(SHELL_NOT_FOUND);\r
806c49db 2518 break;\r
a405b86d 2519 }\r
806c49db 2520 \r
ca53c0af 2521 SHELL_FREE_NON_NULL(CleanOriginal);\r
806c49db 2522 SHELL_FREE_NON_NULL(FirstParameter);\r
a405b86d 2523\r
2524 return (Status);\r
2525}\r
2526\r
2527STATIC CONST UINT16 InvalidChars[] = {L'*', L'?', L'<', L'>', L'\\', L'/', L'\"', 0x0001, 0x0002};\r
2528/**\r
2529 Function determins if the CommandName COULD be a valid command. It does not determine whether\r
2530 this is a valid command. It only checks for invalid characters.\r
2531\r
2532 @param[in] CommandName The name to check\r
2533\r
2534 @retval TRUE CommandName could be a command name\r
2535 @retval FALSE CommandName could not be a valid command name\r
2536**/\r
2537BOOLEAN\r
2538EFIAPI\r
2539IsValidCommandName(\r
2540 IN CONST CHAR16 *CommandName\r
2541 )\r
2542{\r
2543 UINTN Count;\r
2544 if (CommandName == NULL) {\r
2545 ASSERT(FALSE);\r
2546 return (FALSE);\r
2547 }\r
2548 for ( Count = 0\r
2549 ; Count < sizeof(InvalidChars) / sizeof(InvalidChars[0])\r
2550 ; Count++\r
2551 ){\r
2552 if (ScanMem16(CommandName, StrSize(CommandName), InvalidChars[Count]) != NULL) {\r
2553 return (FALSE);\r
2554 }\r
2555 }\r
2556 return (TRUE);\r
2557}\r
2558\r
2559/**\r
2560 Function to process a NSH script file via SHELL_FILE_HANDLE.\r
2561\r
2562 @param[in] Handle The handle to the already opened file.\r
2563 @param[in] Name The name of the script file.\r
2564\r
5223c121
BJ
2565 @param[out] ExitStatus The exit code of the script. Ignored if NULL.\r
2566\r
a405b86d 2567 @retval EFI_SUCCESS the script completed sucessfully\r
2568**/\r
2569EFI_STATUS\r
2570EFIAPI\r
2571RunScriptFileHandle (\r
5223c121
BJ
2572 IN SHELL_FILE_HANDLE Handle,\r
2573 IN CONST CHAR16 *Name,\r
2574 OUT SHELL_STATUS *ExitStatus\r
a405b86d 2575 )\r
2576{\r
2577 EFI_STATUS Status;\r
2578 SCRIPT_FILE *NewScriptFile;\r
2579 UINTN LoopVar;\r
2580 CHAR16 *CommandLine;\r
2581 CHAR16 *CommandLine2;\r
2582 CHAR16 *CommandLine3;\r
2583 SCRIPT_COMMAND_LIST *LastCommand;\r
2584 BOOLEAN Ascii;\r
2585 BOOLEAN PreScriptEchoState;\r
848997b5 2586 BOOLEAN PreCommandEchoState;\r
a405b86d 2587 CONST CHAR16 *CurDir;\r
2588 UINTN LineCount;\r
b6b22b13 2589 CHAR16 LeString[50];\r
7bc3ec3d 2590 SHELL_STATUS CalleeExitStatus;\r
a405b86d 2591\r
2592 ASSERT(!ShellCommandGetScriptExit());\r
7bc3ec3d
SQ
2593 \r
2594 CalleeExitStatus = SHELL_SUCCESS;\r
a405b86d 2595\r
2596 PreScriptEchoState = ShellCommandGetEchoState();\r
2597\r
2598 NewScriptFile = (SCRIPT_FILE*)AllocateZeroPool(sizeof(SCRIPT_FILE));\r
3c865f20 2599 if (NewScriptFile == NULL) {\r
2600 return (EFI_OUT_OF_RESOURCES);\r
2601 }\r
a405b86d 2602\r
2603 //\r
2604 // Set up the name\r
2605 //\r
2606 ASSERT(NewScriptFile->ScriptName == NULL);\r
2607 NewScriptFile->ScriptName = StrnCatGrow(&NewScriptFile->ScriptName, NULL, Name, 0);\r
3c865f20 2608 if (NewScriptFile->ScriptName == NULL) {\r
2609 DeleteScriptFileStruct(NewScriptFile);\r
2610 return (EFI_OUT_OF_RESOURCES);\r
2611 }\r
a405b86d 2612\r
2613 //\r
2614 // Save the parameters (used to replace %0 to %9 later on)\r
2615 //\r
2616 NewScriptFile->Argc = ShellInfoObject.NewShellParametersProtocol->Argc;\r
2617 if (NewScriptFile->Argc != 0) {\r
2618 NewScriptFile->Argv = (CHAR16**)AllocateZeroPool(NewScriptFile->Argc * sizeof(CHAR16*));\r
3c865f20 2619 if (NewScriptFile->Argv == NULL) {\r
2620 DeleteScriptFileStruct(NewScriptFile);\r
2621 return (EFI_OUT_OF_RESOURCES);\r
2622 }\r
a405b86d 2623 for (LoopVar = 0 ; LoopVar < 10 && LoopVar < NewScriptFile->Argc; LoopVar++) {\r
2624 ASSERT(NewScriptFile->Argv[LoopVar] == NULL);\r
2625 NewScriptFile->Argv[LoopVar] = StrnCatGrow(&NewScriptFile->Argv[LoopVar], NULL, ShellInfoObject.NewShellParametersProtocol->Argv[LoopVar], 0);\r
3c865f20 2626 if (NewScriptFile->Argv[LoopVar] == NULL) {\r
2627 DeleteScriptFileStruct(NewScriptFile);\r
2628 return (EFI_OUT_OF_RESOURCES);\r
2629 }\r
a405b86d 2630 }\r
2631 } else {\r
2632 NewScriptFile->Argv = NULL;\r
2633 }\r
2634\r
2635 InitializeListHead(&NewScriptFile->CommandList);\r
2636 InitializeListHead(&NewScriptFile->SubstList);\r
2637\r
2638 //\r
2639 // Now build the list of all script commands.\r
2640 //\r
2641 LineCount = 0;\r
2642 while(!ShellFileHandleEof(Handle)) {\r
2643 CommandLine = ShellFileHandleReturnLine(Handle, &Ascii);\r
2644 LineCount++;\r
4922715d
JC
2645 if (CommandLine == NULL || StrLen(CommandLine) == 0 || CommandLine[0] == '#') {\r
2646 SHELL_FREE_NON_NULL(CommandLine);\r
a405b86d 2647 continue;\r
2648 }\r
2649 NewScriptFile->CurrentCommand = AllocateZeroPool(sizeof(SCRIPT_COMMAND_LIST));\r
3c865f20 2650 if (NewScriptFile->CurrentCommand == NULL) {\r
4922715d 2651 SHELL_FREE_NON_NULL(CommandLine);\r
3c865f20 2652 DeleteScriptFileStruct(NewScriptFile);\r
2653 return (EFI_OUT_OF_RESOURCES);\r
2654 }\r
a405b86d 2655\r
2656 NewScriptFile->CurrentCommand->Cl = CommandLine;\r
2657 NewScriptFile->CurrentCommand->Data = NULL;\r
2658 NewScriptFile->CurrentCommand->Line = LineCount;\r
2659\r
2660 InsertTailList(&NewScriptFile->CommandList, &NewScriptFile->CurrentCommand->Link);\r
2661 }\r
2662\r
2663 //\r
2664 // Add this as the topmost script file\r
2665 //\r
2666 ShellCommandSetNewScript (NewScriptFile);\r
2667\r
2668 //\r
2669 // Now enumerate through the commands and run each one.\r
2670 //\r
733f138d 2671 CommandLine = AllocateZeroPool(PcdGet16(PcdShellPrintBufferSize));\r
3c865f20 2672 if (CommandLine == NULL) {\r
2673 DeleteScriptFileStruct(NewScriptFile);\r
2674 return (EFI_OUT_OF_RESOURCES);\r
2675 }\r
733f138d 2676 CommandLine2 = AllocateZeroPool(PcdGet16(PcdShellPrintBufferSize));\r
3c865f20 2677 if (CommandLine2 == NULL) {\r
2678 FreePool(CommandLine);\r
2679 DeleteScriptFileStruct(NewScriptFile);\r
2680 return (EFI_OUT_OF_RESOURCES);\r
2681 }\r
a405b86d 2682\r
2683 for ( NewScriptFile->CurrentCommand = (SCRIPT_COMMAND_LIST *)GetFirstNode(&NewScriptFile->CommandList)\r
2684 ; !IsNull(&NewScriptFile->CommandList, &NewScriptFile->CurrentCommand->Link)\r
2685 ; // conditional increment in the body of the loop\r
2686 ){\r
3c865f20 2687 ASSERT(CommandLine2 != NULL);\r
a405b86d 2688 StrCpy(CommandLine2, NewScriptFile->CurrentCommand->Cl);\r
2689\r
2690 //\r
2691 // NULL out comments\r
2692 //\r
2693 for (CommandLine3 = CommandLine2 ; CommandLine3 != NULL && *CommandLine3 != CHAR_NULL ; CommandLine3++) {\r
2694 if (*CommandLine3 == L'^') {\r
2695 if (*(CommandLine3+1) == L'#' || *(CommandLine3+1) == L':') {\r
2696 CopyMem(CommandLine3, CommandLine3+1, StrSize(CommandLine3) - sizeof(CommandLine3[0]));\r
2697 }\r
2698 } else if (*CommandLine3 == L'#') {\r
2699 *CommandLine3 = CHAR_NULL;\r
2700 }\r
2701 }\r
2702\r
2703 if (CommandLine2 != NULL && StrLen(CommandLine2) >= 1) {\r
2704 //\r
2705 // Due to variability in starting the find and replace action we need to have both buffers the same.\r
2706 //\r
2707 StrCpy(CommandLine, CommandLine2);\r
2708\r
2709 //\r
2710 // Remove the %0 to %9 from the command line (if we have some arguments)\r
2711 //\r
2712 if (NewScriptFile->Argv != NULL) {\r
2713 switch (NewScriptFile->Argc) {\r
2714 default:\r
2715 Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PcdGet16 (PcdShellPrintBufferSize), L"%9", NewScriptFile->Argv[9], FALSE, TRUE);\r
2716 ASSERT_EFI_ERROR(Status);\r
2717 case 9:\r
2718 Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PcdGet16 (PcdShellPrintBufferSize), L"%8", NewScriptFile->Argv[8], FALSE, TRUE);\r
2719 ASSERT_EFI_ERROR(Status);\r
2720 case 8:\r
2721 Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PcdGet16 (PcdShellPrintBufferSize), L"%7", NewScriptFile->Argv[7], FALSE, TRUE);\r
2722 ASSERT_EFI_ERROR(Status);\r
2723 case 7:\r
2724 Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PcdGet16 (PcdShellPrintBufferSize), L"%6", NewScriptFile->Argv[6], FALSE, TRUE);\r
2725 ASSERT_EFI_ERROR(Status);\r
2726 case 6:\r
2727 Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PcdGet16 (PcdShellPrintBufferSize), L"%5", NewScriptFile->Argv[5], FALSE, TRUE);\r
2728 ASSERT_EFI_ERROR(Status);\r
2729 case 5:\r
2730 Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PcdGet16 (PcdShellPrintBufferSize), L"%4", NewScriptFile->Argv[4], FALSE, TRUE);\r
2731 ASSERT_EFI_ERROR(Status);\r
2732 case 4:\r
2733 Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PcdGet16 (PcdShellPrintBufferSize), L"%3", NewScriptFile->Argv[3], FALSE, TRUE);\r
2734 ASSERT_EFI_ERROR(Status);\r
2735 case 3:\r
2736 Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PcdGet16 (PcdShellPrintBufferSize), L"%2", NewScriptFile->Argv[2], FALSE, TRUE);\r
2737 ASSERT_EFI_ERROR(Status);\r
2738 case 2:\r
2739 Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PcdGet16 (PcdShellPrintBufferSize), L"%1", NewScriptFile->Argv[1], FALSE, TRUE);\r
2740 ASSERT_EFI_ERROR(Status);\r
2741 case 1:\r
2742 Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PcdGet16 (PcdShellPrintBufferSize), L"%0", NewScriptFile->Argv[0], FALSE, TRUE);\r
2743 ASSERT_EFI_ERROR(Status);\r
2744 break;\r
2745 case 0:\r
2746 break;\r
2747 }\r
2748 }\r
2749 Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PcdGet16 (PcdShellPrintBufferSize), L"%1", L"\"\"", FALSE, FALSE);\r
2750 Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PcdGet16 (PcdShellPrintBufferSize), L"%2", L"\"\"", FALSE, FALSE);\r
2751 Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PcdGet16 (PcdShellPrintBufferSize), L"%3", L"\"\"", FALSE, FALSE);\r
2752 Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PcdGet16 (PcdShellPrintBufferSize), L"%4", L"\"\"", FALSE, FALSE);\r
2753 Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PcdGet16 (PcdShellPrintBufferSize), L"%5", L"\"\"", FALSE, FALSE);\r
2754 Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PcdGet16 (PcdShellPrintBufferSize), L"%6", L"\"\"", FALSE, FALSE);\r
2755 Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PcdGet16 (PcdShellPrintBufferSize), L"%7", L"\"\"", FALSE, FALSE);\r
2756 Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PcdGet16 (PcdShellPrintBufferSize), L"%8", L"\"\"", FALSE, FALSE);\r
2757 Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PcdGet16 (PcdShellPrintBufferSize), L"%9", L"\"\"", FALSE, FALSE);\r
2758\r
2759 StrCpy(CommandLine2, CommandLine);\r
2760\r
2761 LastCommand = NewScriptFile->CurrentCommand;\r
2762\r
2763 for (CommandLine3 = CommandLine2 ; CommandLine3[0] == L' ' ; CommandLine3++);\r
2764\r
3c865f20 2765 if (CommandLine3 != NULL && CommandLine3[0] == L':' ) {\r
a405b86d 2766 //\r
2767 // This line is a goto target / label\r
2768 //\r
2769 } else {\r
2770 if (CommandLine3 != NULL && StrLen(CommandLine3) > 0) {\r
848997b5 2771 if (CommandLine3[0] == L'@') {\r
2772 //\r
2773 // We need to save the current echo state\r
2774 // and disable echo for just this command.\r
2775 //\r
2776 PreCommandEchoState = ShellCommandGetEchoState();\r
2777 ShellCommandSetEchoState(FALSE);\r
5223c121 2778 Status = RunCommand(CommandLine3+1, NULL);\r
848997b5 2779\r
2780 //\r
284e034f 2781 // If command was "@echo -off" or "@echo -on" then don't restore echo state\r
848997b5 2782 //\r
284e034f
CP
2783 if (StrCmp (L"@echo -off", CommandLine3) != 0 &&\r
2784 StrCmp (L"@echo -on", CommandLine3) != 0) {\r
2785 //\r
2786 // Now restore the pre-'@' echo state.\r
2787 //\r
2788 ShellCommandSetEchoState(PreCommandEchoState);\r
2789 }\r
848997b5 2790 } else {\r
0d11446d 2791 if (ShellCommandGetEchoState()) {\r
2792 CurDir = ShellInfoObject.NewEfiShellProtocol->GetEnv(L"cwd");\r
2793 if (CurDir != NULL && StrLen(CurDir) > 1) {\r
2794 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_CURDIR), ShellInfoObject.HiiHandle, CurDir);\r
2795 } else {\r
2796 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_SHELL), ShellInfoObject.HiiHandle);\r
2797 }\r
2798 ShellPrintEx(-1, -1, L"%s\r\n", CommandLine2);\r
2799 }\r
5223c121 2800 Status = RunCommand(CommandLine3, NULL);\r
848997b5 2801 }\r
a405b86d 2802 }\r
2803\r
2804 if (ShellCommandGetScriptExit()) {\r
5b5cd144
CP
2805 //\r
2806 // ShellCommandGetExitCode() always returns a UINT64\r
2807 //\r
5223c121
BJ
2808 CalleeExitStatus = (SHELL_STATUS) ShellCommandGetExitCode();\r
2809 UnicodeSPrint(LeString, sizeof(LeString), L"0x%Lx", CalleeExitStatus);\r
5b5cd144
CP
2810 DEBUG_CODE(InternalEfiShellSetEnv(L"debuglasterror", LeString, TRUE););\r
2811 InternalEfiShellSetEnv(L"lasterror", LeString, TRUE);\r
b6b22b13 2812\r
2813 ShellCommandRegisterExit(FALSE, 0);\r
a405b86d 2814 Status = EFI_SUCCESS;\r
2815 break;\r
2816 }\r
c6a7fef8
ED
2817 if (ShellGetExecutionBreakFlag()) {\r
2818 break;\r
2819 }\r
a405b86d 2820 if (EFI_ERROR(Status)) {\r
5223c121 2821 CalleeExitStatus = (SHELL_STATUS) Status;\r
a405b86d 2822 break;\r
2823 }\r
2824 if (ShellCommandGetExit()) {\r
5223c121 2825 CalleeExitStatus = (SHELL_STATUS) ShellCommandGetExitCode();\r
a405b86d 2826 break;\r
2827 }\r
2828 }\r
2829 //\r
2830 // If that commend did not update the CurrentCommand then we need to advance it...\r
2831 //\r
2832 if (LastCommand == NewScriptFile->CurrentCommand) {\r
2833 NewScriptFile->CurrentCommand = (SCRIPT_COMMAND_LIST *)GetNextNode(&NewScriptFile->CommandList, &NewScriptFile->CurrentCommand->Link);\r
2834 if (!IsNull(&NewScriptFile->CommandList, &NewScriptFile->CurrentCommand->Link)) {\r
2835 NewScriptFile->CurrentCommand->Reset = TRUE;\r
2836 }\r
2837 }\r
2838 } else {\r
2839 NewScriptFile->CurrentCommand = (SCRIPT_COMMAND_LIST *)GetNextNode(&NewScriptFile->CommandList, &NewScriptFile->CurrentCommand->Link);\r
2840 if (!IsNull(&NewScriptFile->CommandList, &NewScriptFile->CurrentCommand->Link)) {\r
2841 NewScriptFile->CurrentCommand->Reset = TRUE;\r
2842 }\r
2843 }\r
2844 }\r
2845\r
a405b86d 2846\r
2847 FreePool(CommandLine);\r
2848 FreePool(CommandLine2);\r
2849 ShellCommandSetNewScript (NULL);\r
733f138d 2850\r
2851 //\r
2852 // Only if this was the last script reset the state.\r
2853 //\r
2854 if (ShellCommandGetCurrentScriptFile()==NULL) {\r
2855 ShellCommandSetEchoState(PreScriptEchoState);\r
2856 }\r
5223c121
BJ
2857\r
2858 if (ExitStatus != NULL) {\r
2859 *ExitStatus = CalleeExitStatus;\r
2860 }\r
2861\r
a405b86d 2862 return (EFI_SUCCESS);\r
2863}\r
2864\r
2865/**\r
2866 Function to process a NSH script file.\r
2867\r
2868 @param[in] ScriptPath Pointer to the script file name (including file system path).\r
e958b946
JC
2869 @param[in] Handle the handle of the script file already opened.\r
2870 @param[in] CmdLine the command line to run.\r
2871 @param[in] ParamProtocol the shell parameters protocol pointer\r
a405b86d 2872\r
5223c121
BJ
2873 @param[out] ExitStatus The exit code of the script. Ignored if NULL.\r
2874\r
a405b86d 2875 @retval EFI_SUCCESS the script completed sucessfully\r
2876**/\r
2877EFI_STATUS\r
2878EFIAPI\r
2879RunScriptFile (\r
5223c121
BJ
2880 IN CONST CHAR16 *ScriptPath,\r
2881 IN SHELL_FILE_HANDLE Handle OPTIONAL,\r
2882 IN CONST CHAR16 *CmdLine,\r
2883 IN EFI_SHELL_PARAMETERS_PROTOCOL *ParamProtocol,\r
2884 OUT SHELL_STATUS *ExitStatus\r
a405b86d 2885 )\r
2886{\r
2887 EFI_STATUS Status;\r
2888 SHELL_FILE_HANDLE FileHandle;\r
e958b946
JC
2889 UINTN Argc;\r
2890 CHAR16 **Argv;\r
a405b86d 2891\r
2892 if (ShellIsFile(ScriptPath) != EFI_SUCCESS) {\r
2893 return (EFI_INVALID_PARAMETER);\r
2894 }\r
2895\r
e958b946
JC
2896 //\r
2897 // get the argc and argv updated for scripts\r
2898 //\r
2899 Status = UpdateArgcArgv(ParamProtocol, CmdLine, &Argv, &Argc);\r
2900 if (!EFI_ERROR(Status)) {\r
a405b86d 2901\r
e958b946
JC
2902 if (Handle == NULL) {\r
2903 //\r
2904 // open the file\r
2905 //\r
2906 Status = ShellOpenFileByName(ScriptPath, &FileHandle, EFI_FILE_MODE_READ, 0);\r
2907 if (!EFI_ERROR(Status)) {\r
2908 //\r
2909 // run it\r
2910 //\r
5223c121 2911 Status = RunScriptFileHandle(FileHandle, ScriptPath, ExitStatus);\r
a405b86d 2912\r
e958b946
JC
2913 //\r
2914 // now close the file\r
2915 //\r
2916 ShellCloseFile(&FileHandle);\r
2917 }\r
2918 } else {\r
5223c121 2919 Status = RunScriptFileHandle(Handle, ScriptPath, ExitStatus);\r
e958b946
JC
2920 }\r
2921 }\r
2922\r
2923 //\r
2924 // This is guarenteed to be called after UpdateArgcArgv no matter what else happened.\r
2925 // This is safe even if the update API failed. In this case, it may be a no-op.\r
2926 //\r
2927 RestoreArgcArgv(ParamProtocol, &Argv, &Argc);\r
a405b86d 2928\r
2929 return (Status);\r
2930}\r