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