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