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