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