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