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