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