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