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