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