]> git.proxmox.com Git - mirror_edk2.git/blame - ShellPkg/Application/Shell/Shell.c
ShellPkg: Always remove non-defined environment variable
[mirror_edk2.git] / ShellPkg / Application / Shell / Shell.c
CommitLineData
a405b86d 1/** @file\r
2 This is THE shell (application)\r
3\r
fdd52bde 4 Copyright (c) 2009 - 2015, Intel Corporation. All rights reserved.<BR>\r
d6e88a6c 5 (C) Copyright 2013-2014, Hewlett-Packard Development Company, L.P.\r
a405b86d 6 This program and the accompanying materials\r
7 are licensed and made available under the terms and conditions of the BSD License\r
8 which accompanies this distribution. The full text of the license may be found at\r
9 http://opensource.org/licenses/bsd-license.php\r
10\r
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13\r
14**/\r
15\r
16#include "Shell.h"\r
17\r
18//\r
19// Initialize the global structure\r
20//\r
21SHELL_INFO ShellInfoObject = {\r
22 NULL,\r
23 NULL,\r
24 FALSE,\r
25 FALSE,\r
26 {\r
ce68d3bc
SZ
27 {{\r
28 0,\r
29 0,\r
30 0,\r
31 0,\r
32 0,\r
33 0,\r
34 0,\r
35 0,\r
36 0\r
37 }},\r
a405b86d 38 0,\r
39 NULL,\r
40 NULL\r
41 },\r
ce68d3bc 42 {{NULL, NULL}, NULL},\r
a405b86d 43 {\r
ce68d3bc 44 {{NULL, NULL}, NULL},\r
a405b86d 45 0,\r
46 0,\r
47 TRUE\r
48 },\r
49 NULL,\r
50 0,\r
51 NULL,\r
52 NULL,\r
53 NULL,\r
54 NULL,\r
55 NULL,\r
ce68d3bc
SZ
56 {{NULL, NULL}, NULL, NULL},\r
57 {{NULL, NULL}, NULL, NULL},\r
58 NULL,\r
59 NULL,\r
60 NULL,\r
61 NULL,\r
8be0ba36 62 NULL,\r
63 NULL,\r
64 NULL,\r
a49f6a2f 65 NULL,\r
66 FALSE\r
a405b86d 67};\r
68\r
69STATIC CONST CHAR16 mScriptExtension[] = L".NSH";\r
70STATIC CONST CHAR16 mExecutableExtensions[] = L".NSH;.EFI";\r
3c865f20 71STATIC CONST CHAR16 mStartupScript[] = L"startup.nsh";\r
a405b86d 72\r
ad2bc854 73/**\r
51429264 74 Cleans off leading and trailing spaces and tabs.\r
ad2bc854 75\r
51429264 76 @param[in] String pointer to the string to trim them off.\r
ad2bc854
JC
77**/\r
78EFI_STATUS\r
79EFIAPI\r
80TrimSpaces(\r
81 IN CHAR16 **String\r
82 )\r
83{\r
84 ASSERT(String != NULL);\r
85 ASSERT(*String!= NULL);\r
86 //\r
87 // Remove any spaces and tabs at the beginning of the (*String).\r
88 //\r
89 while (((*String)[0] == L' ') || ((*String)[0] == L'\t')) {\r
90 CopyMem((*String), (*String)+1, StrSize((*String)) - sizeof((*String)[0]));\r
91 }\r
92\r
93 //\r
404b3f43 94 // Remove any spaces and tabs at the end of the (*String).\r
ad2bc854 95 //\r
404b3f43 96 while ((StrLen (*String) > 0) && (((*String)[StrLen((*String))-1] == L' ') || ((*String)[StrLen((*String))-1] == L'\t'))) {\r
ad2bc854
JC
97 (*String)[StrLen((*String))-1] = CHAR_NULL;\r
98 }\r
99\r
100 return (EFI_SUCCESS);\r
101}\r
102\r
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
fdd52bde
JC
1384 if (FirstQuote!= NULL && FirstQuote < FirstPercent) {\r
1385 SecondQuote = FindNextInstance(FirstQuote+1, L"\"", TRUE);\r
d5b5440b
JC
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
fdd52bde
JC
1403 \r
1404 if (FirstQuote == NULL || 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
d5b5440b
JC
1418 CurrentLocator = FirstQuote;\r
1419 }\r
1420 return (EFI_SUCCESS);\r
1421}\r
1422\r
a405b86d 1423/**\r
1424 Function allocates a new command line and replaces all instances of environment\r
1425 variable names that are correctly preset to their values.\r
1426\r
1427 If the return value is not NULL the memory must be caller freed.\r
1428\r
1429 @param[in] OriginalCommandLine The original command line\r
1430\r
1431 @retval NULL An error ocurred.\r
1432 @return The new command line with no environment variables present.\r
1433**/\r
1434CHAR16*\r
1435EFIAPI\r
1436ShellConvertVariables (\r
1437 IN CONST CHAR16 *OriginalCommandLine\r
1438 )\r
1439{\r
1440 CONST CHAR16 *MasterEnvList;\r
1441 UINTN NewSize;\r
1442 CHAR16 *NewCommandLine1;\r
1443 CHAR16 *NewCommandLine2;\r
1444 CHAR16 *Temp;\r
1445 UINTN ItemSize;\r
1446 CHAR16 *ItemTemp;\r
1447 SCRIPT_FILE *CurrentScriptFile;\r
1448 ALIAS_LIST *AliasListNode;\r
1449\r
1450 ASSERT(OriginalCommandLine != NULL);\r
1451\r
1452 ItemSize = 0;\r
1453 NewSize = StrSize(OriginalCommandLine);\r
1454 CurrentScriptFile = ShellCommandGetCurrentScriptFile();\r
1455 Temp = NULL;\r
1456\r
1457 ///@todo update this to handle the %0 - %9 for scripting only (borrow from line 1256 area) ? ? ?\r
1458\r
1459 //\r
1460 // calculate the size required for the post-conversion string...\r
1461 //\r
1462 if (CurrentScriptFile != NULL) {\r
1463 for (AliasListNode = (ALIAS_LIST*)GetFirstNode(&CurrentScriptFile->SubstList)\r
1464 ; !IsNull(&CurrentScriptFile->SubstList, &AliasListNode->Link)\r
1465 ; AliasListNode = (ALIAS_LIST*)GetNextNode(&CurrentScriptFile->SubstList, &AliasListNode->Link)\r
1466 ){\r
1467 for (Temp = StrStr(OriginalCommandLine, AliasListNode->Alias)\r
1468 ; Temp != NULL\r
1469 ; Temp = StrStr(Temp+1, AliasListNode->Alias)\r
1470 ){\r
1471 //\r
1472 // we need a preceeding and if there is space no ^ preceeding (if no space ignore)\r
1473 //\r
1474 if ((((Temp-OriginalCommandLine)>2) && *(Temp-2) != L'^') || ((Temp-OriginalCommandLine)<=2)) {\r
1475 NewSize += StrSize(AliasListNode->CommandString);\r
1476 }\r
1477 }\r
1478 }\r
1479 }\r
1480\r
1481 for (MasterEnvList = EfiShellGetEnv(NULL)\r
1482 ; MasterEnvList != NULL && *MasterEnvList != CHAR_NULL //&& *(MasterEnvList+1) != CHAR_NULL\r
1483 ; MasterEnvList += StrLen(MasterEnvList) + 1\r
1484 ){\r
1485 if (StrSize(MasterEnvList) > ItemSize) {\r
1486 ItemSize = StrSize(MasterEnvList);\r
1487 }\r
1488 for (Temp = StrStr(OriginalCommandLine, MasterEnvList)\r
1489 ; Temp != NULL\r
1490 ; Temp = StrStr(Temp+1, MasterEnvList)\r
1491 ){\r
1492 //\r
1493 // we need a preceeding and following % and if there is space no ^ preceeding (if no space ignore)\r
1494 //\r
1495 if (*(Temp-1) == L'%' && *(Temp+StrLen(MasterEnvList)) == L'%' &&\r
1496 ((((Temp-OriginalCommandLine)>2) && *(Temp-2) != L'^') || ((Temp-OriginalCommandLine)<=2))) {\r
1497 NewSize+=StrSize(EfiShellGetEnv(MasterEnvList));\r
1498 }\r
1499 }\r
1500 }\r
1501\r
a405b86d 1502 //\r
1503 // now do the replacements...\r
1504 //\r
7f79b01e 1505 NewCommandLine1 = AllocateCopyPool(NewSize, OriginalCommandLine);\r
a405b86d 1506 NewCommandLine2 = AllocateZeroPool(NewSize);\r
1507 ItemTemp = AllocateZeroPool(ItemSize+(2*sizeof(CHAR16)));\r
3c865f20 1508 if (NewCommandLine1 == NULL || NewCommandLine2 == NULL || ItemTemp == NULL) {\r
1509 SHELL_FREE_NON_NULL(NewCommandLine1);\r
1510 SHELL_FREE_NON_NULL(NewCommandLine2);\r
1511 SHELL_FREE_NON_NULL(ItemTemp);\r
1512 return (NULL);\r
1513 }\r
a405b86d 1514 for (MasterEnvList = EfiShellGetEnv(NULL)\r
7f79b01e 1515 ; MasterEnvList != NULL && *MasterEnvList != CHAR_NULL\r
a405b86d 1516 ; MasterEnvList += StrLen(MasterEnvList) + 1\r
1517 ){\r
cc72674b 1518 StrnCpy(ItemTemp, L"%", ((ItemSize+(2*sizeof(CHAR16)))/sizeof(CHAR16))-1);\r
7f79b01e
JC
1519 StrnCat(ItemTemp, MasterEnvList, ((ItemSize+(2*sizeof(CHAR16)))/sizeof(CHAR16))-1 - StrLen(ItemTemp));\r
1520 StrnCat(ItemTemp, L"%", ((ItemSize+(2*sizeof(CHAR16)))/sizeof(CHAR16))-1 - StrLen(ItemTemp));\r
a405b86d 1521 ShellCopySearchAndReplace(NewCommandLine1, NewCommandLine2, NewSize, ItemTemp, EfiShellGetEnv(MasterEnvList), TRUE, FALSE);\r
7f79b01e 1522 StrnCpy(NewCommandLine1, NewCommandLine2, NewSize/sizeof(CHAR16)-1);\r
a405b86d 1523 }\r
1524 if (CurrentScriptFile != NULL) {\r
1525 for (AliasListNode = (ALIAS_LIST*)GetFirstNode(&CurrentScriptFile->SubstList)\r
1526 ; !IsNull(&CurrentScriptFile->SubstList, &AliasListNode->Link)\r
1527 ; AliasListNode = (ALIAS_LIST*)GetNextNode(&CurrentScriptFile->SubstList, &AliasListNode->Link)\r
1528 ){\r
1529 ShellCopySearchAndReplace(NewCommandLine1, NewCommandLine2, NewSize, AliasListNode->Alias, AliasListNode->CommandString, TRUE, FALSE);\r
7f79b01e 1530 StrnCpy(NewCommandLine1, NewCommandLine2, NewSize/sizeof(CHAR16)-1);\r
a405b86d 1531 }\r
1532 }\r
1533\r
fdd52bde
JC
1534 //\r
1535 // Remove non-existant environment variables\r
1536 //\r
1537 StripUnreplacedEnvironmentVariables(NewCommandLine1);\r
1538\r
df07baea
JC
1539 //\r
1540 // Now cleanup any straggler intentionally ignored "%" characters\r
1541 //\r
1542 ShellCopySearchAndReplace(NewCommandLine1, NewCommandLine2, NewSize, L"^%", L"%", TRUE, FALSE);\r
7f79b01e 1543 StrnCpy(NewCommandLine1, NewCommandLine2, NewSize/sizeof(CHAR16)-1);\r
df07baea 1544 \r
a405b86d 1545 FreePool(NewCommandLine2);\r
1546 FreePool(ItemTemp);\r
1547\r
1548 return (NewCommandLine1);\r
1549}\r
1550\r
1551/**\r
1552 Internal function to run a command line with pipe usage.\r
1553\r
1554 @param[in] CmdLine The pointer to the command line.\r
1555 @param[in] StdIn The pointer to the Standard input.\r
1556 @param[in] StdOut The pointer to the Standard output.\r
1557\r
1558 @retval EFI_SUCCESS The split command is executed successfully.\r
1559 @retval other Some error occurs when executing the split command.\r
1560**/\r
1561EFI_STATUS\r
1562EFIAPI\r
1563RunSplitCommand(\r
1564 IN CONST CHAR16 *CmdLine,\r
1565 IN SHELL_FILE_HANDLE *StdIn,\r
a308e058 1566 IN SHELL_FILE_HANDLE *StdOut\r
a405b86d 1567 )\r
1568{\r
1569 EFI_STATUS Status;\r
1570 CHAR16 *NextCommandLine;\r
1571 CHAR16 *OurCommandLine;\r
1572 UINTN Size1;\r
1573 UINTN Size2;\r
1574 SPLIT_LIST *Split;\r
1575 SHELL_FILE_HANDLE *TempFileHandle;\r
1576 BOOLEAN Unicode;\r
1577\r
1578 ASSERT(StdOut == NULL);\r
1579\r
1580 ASSERT(StrStr(CmdLine, L"|") != NULL);\r
1581\r
1582 Status = EFI_SUCCESS;\r
1583 NextCommandLine = NULL;\r
1584 OurCommandLine = NULL;\r
1585 Size1 = 0;\r
1586 Size2 = 0;\r
1587\r
1588 NextCommandLine = StrnCatGrow(&NextCommandLine, &Size1, StrStr(CmdLine, L"|")+1, 0);\r
1589 OurCommandLine = StrnCatGrow(&OurCommandLine , &Size2, CmdLine , StrStr(CmdLine, L"|") - CmdLine);\r
532691c8 1590\r
1591 if (NextCommandLine == NULL || OurCommandLine == NULL) {\r
1592 SHELL_FREE_NON_NULL(OurCommandLine);\r
1593 SHELL_FREE_NON_NULL(NextCommandLine);\r
1594 return (EFI_OUT_OF_RESOURCES);\r
19216573 1595 } else if (StrStr(OurCommandLine, L"|") != NULL || Size1 == 0 || Size2 == 0) {\r
1596 SHELL_FREE_NON_NULL(OurCommandLine);\r
1597 SHELL_FREE_NON_NULL(NextCommandLine);\r
1598 return (EFI_INVALID_PARAMETER);\r
1599 } else if (NextCommandLine[0] != CHAR_NULL &&\r
a405b86d 1600 NextCommandLine[0] == L'a' &&\r
1601 NextCommandLine[1] == L' '\r
1602 ){\r
1603 CopyMem(NextCommandLine, NextCommandLine+1, StrSize(NextCommandLine) - sizeof(NextCommandLine[0]));\r
1604 Unicode = FALSE;\r
1605 } else {\r
1606 Unicode = TRUE;\r
1607 }\r
1608\r
1609\r
1610 //\r
1611 // make a SPLIT_LIST item and add to list\r
1612 //\r
1613 Split = AllocateZeroPool(sizeof(SPLIT_LIST));\r
1614 ASSERT(Split != NULL);\r
1615 Split->SplitStdIn = StdIn;\r
1616 Split->SplitStdOut = ConvertEfiFileProtocolToShellHandle(CreateFileInterfaceMem(Unicode), NULL);\r
1617 ASSERT(Split->SplitStdOut != NULL);\r
1618 InsertHeadList(&ShellInfoObject.SplitList.Link, &Split->Link);\r
1619\r
a308e058 1620 Status = RunCommand(OurCommandLine);\r
a405b86d 1621\r
1622 //\r
1623 // move the output from the first to the in to the second.\r
1624 //\r
1625 TempFileHandle = Split->SplitStdOut;\r
1626 if (Split->SplitStdIn == StdIn) {\r
1627 Split->SplitStdOut = NULL;\r
1628 } else {\r
1629 Split->SplitStdOut = Split->SplitStdIn;\r
1630 }\r
1631 Split->SplitStdIn = TempFileHandle;\r
1632 ShellInfoObject.NewEfiShellProtocol->SetFilePosition(ConvertShellHandleToEfiFileProtocol(Split->SplitStdIn), 0);\r
1633\r
1634 if (!EFI_ERROR(Status)) {\r
a308e058 1635 Status = RunCommand(NextCommandLine);\r
a405b86d 1636 }\r
1637\r
1638 //\r
1639 // remove the top level from the ScriptList\r
1640 //\r
1641 ASSERT((SPLIT_LIST*)GetFirstNode(&ShellInfoObject.SplitList.Link) == Split);\r
1642 RemoveEntryList(&Split->Link);\r
1643\r
1644 //\r
1645 // Note that the original StdIn is now the StdOut...\r
1646 //\r
1647 if (Split->SplitStdOut != NULL && Split->SplitStdOut != StdIn) {\r
1648 ShellInfoObject.NewEfiShellProtocol->CloseFile(ConvertShellHandleToEfiFileProtocol(Split->SplitStdOut));\r
1649 }\r
1650 if (Split->SplitStdIn != NULL) {\r
1651 ShellInfoObject.NewEfiShellProtocol->CloseFile(ConvertShellHandleToEfiFileProtocol(Split->SplitStdIn));\r
1652 }\r
1653\r
1654 FreePool(Split);\r
1655 FreePool(NextCommandLine);\r
1656 FreePool(OurCommandLine);\r
1657\r
1658 return (Status);\r
1659}\r
1660\r
1ef61d03
JC
1661/**\r
1662 Take the original command line, substitute any variables, free \r
51429264 1663 the original string, return the modified copy.\r
1ef61d03 1664\r
51429264 1665 @param[in] CmdLine pointer to the command line to update.\r
1ef61d03 1666\r
51429264
SQ
1667 @retval EFI_SUCCESS the function was successful.\r
1668 @retval EFI_OUT_OF_RESOURCES a memory allocation failed.\r
1ef61d03
JC
1669**/\r
1670EFI_STATUS\r
1671EFIAPI\r
1672ShellSubstituteVariables(\r
1673 IN CHAR16 **CmdLine\r
1674 )\r
1675{\r
1676 CHAR16 *NewCmdLine;\r
1677 NewCmdLine = ShellConvertVariables(*CmdLine);\r
1678 SHELL_FREE_NON_NULL(*CmdLine);\r
1679 if (NewCmdLine == NULL) {\r
1680 return (EFI_OUT_OF_RESOURCES);\r
1681 }\r
1682 *CmdLine = NewCmdLine;\r
1683 return (EFI_SUCCESS);\r
1684}\r
1685\r
ca53c0af
JC
1686/**\r
1687 Take the original command line, substitute any alias in the first group of space delimited characters, free \r
51429264 1688 the original string, return the modified copy.\r
ca53c0af 1689\r
51429264 1690 @param[in] CmdLine pointer to the command line to update.\r
ca53c0af 1691\r
51429264
SQ
1692 @retval EFI_SUCCESS the function was successful.\r
1693 @retval EFI_OUT_OF_RESOURCES a memory allocation failed.\r
ca53c0af
JC
1694**/\r
1695EFI_STATUS\r
1696EFIAPI\r
1697ShellSubstituteAliases(\r
1698 IN CHAR16 **CmdLine\r
1699 )\r
1700{\r
1701 CHAR16 *NewCmdLine;\r
1702 CHAR16 *CommandName;\r
1703 EFI_STATUS Status;\r
1704 UINTN PostAliasSize;\r
1705 ASSERT(CmdLine != NULL);\r
1706 ASSERT(*CmdLine!= NULL);\r
1707\r
1708\r
1709 CommandName = NULL;\r
1710 if (StrStr((*CmdLine), L" ") == NULL){\r
1711 StrnCatGrow(&CommandName, NULL, (*CmdLine), 0);\r
1712 } else {\r
1713 StrnCatGrow(&CommandName, NULL, (*CmdLine), StrStr((*CmdLine), L" ") - (*CmdLine));\r
1714 }\r
1715\r
1716 //\r
1717 // This cannot happen 'inline' since the CmdLine can need extra space.\r
1718 //\r
1719 NewCmdLine = NULL;\r
1720 if (!ShellCommandIsCommandOnList(CommandName)) {\r
1721 //\r
1722 // Convert via alias\r
1723 //\r
1724 Status = ShellConvertAlias(&CommandName);\r
1725 if (EFI_ERROR(Status)){\r
1726 return (Status);\r
1727 }\r
1728 PostAliasSize = 0;\r
1729 NewCmdLine = StrnCatGrow(&NewCmdLine, &PostAliasSize, CommandName, 0);\r
1730 if (NewCmdLine == NULL) {\r
1731 SHELL_FREE_NON_NULL(CommandName);\r
1732 SHELL_FREE_NON_NULL(*CmdLine);\r
1733 return (EFI_OUT_OF_RESOURCES);\r
1734 }\r
1735 NewCmdLine = StrnCatGrow(&NewCmdLine, &PostAliasSize, StrStr((*CmdLine), L" "), 0);\r
1736 if (NewCmdLine == NULL) {\r
1737 SHELL_FREE_NON_NULL(CommandName);\r
1738 SHELL_FREE_NON_NULL(*CmdLine);\r
1739 return (EFI_OUT_OF_RESOURCES);\r
1740 }\r
1741 } else {\r
1742 NewCmdLine = StrnCatGrow(&NewCmdLine, NULL, (*CmdLine), 0);\r
1743 }\r
1744\r
1745 SHELL_FREE_NON_NULL(*CmdLine);\r
1746 SHELL_FREE_NON_NULL(CommandName);\r
1747 \r
1748 //\r
1749 // re-assign the passed in double pointer to point to our newly allocated buffer\r
1750 //\r
1751 *CmdLine = NewCmdLine;\r
1752\r
1753 return (EFI_SUCCESS);\r
1754}\r
1755\r
6ba2921d
JC
1756/**\r
1757 Takes the Argv[0] part of the command line and determine the meaning of it.\r
e7831c90 1758\r
51429264 1759 @param[in] CmdName pointer to the command line to update.\r
e7831c90 1760 \r
51429264
SQ
1761 @retval Internal_Command The name is an internal command.\r
1762 @retval File_Sys_Change the name is a file system change.\r
1763 @retval Script_File_Name the name is a NSH script file.\r
1764 @retval Unknown_Invalid the name is unknown.\r
1765 @retval Efi_Application the name is an application (.EFI).\r
6ba2921d
JC
1766**/\r
1767SHELL_OPERATION_TYPES\r
1768EFIAPI\r
1769GetOperationType(\r
1770 IN CONST CHAR16 *CmdName\r
1771 )\r
1772{\r
1773 CHAR16* FileWithPath;\r
1774 CONST CHAR16* TempLocation;\r
1775 CONST CHAR16* TempLocation2;\r
1776\r
1777 FileWithPath = NULL;\r
1778 //\r
1779 // test for an internal command.\r
1780 //\r
1781 if (ShellCommandIsCommandOnList(CmdName)) {\r
51429264 1782 return (Internal_Command);\r
6ba2921d
JC
1783 }\r
1784\r
1785 //\r
fc4c7b30 1786 // Test for file system change request. anything ending with first : and cant have spaces.\r
6ba2921d
JC
1787 //\r
1788 if (CmdName[(StrLen(CmdName)-1)] == L':') {\r
fc4c7b30
JC
1789 if ( StrStr(CmdName, L" ") != NULL \r
1790 || StrLen(StrStr(CmdName, L":")) > 1\r
1791 ) {\r
51429264 1792 return (Unknown_Invalid);\r
6ba2921d 1793 }\r
51429264 1794 return (File_Sys_Change);\r
6ba2921d
JC
1795 }\r
1796\r
1797 //\r
1798 // Test for a file\r
1799 //\r
1800 if ((FileWithPath = ShellFindFilePathEx(CmdName, mExecutableExtensions)) != NULL) {\r
1801 //\r
1802 // See if that file has a script file extension\r
1803 //\r
1804 if (StrLen(FileWithPath) > 4) {\r
1805 TempLocation = FileWithPath+StrLen(FileWithPath)-4;\r
1806 TempLocation2 = mScriptExtension;\r
1807 if (StringNoCaseCompare((VOID*)(&TempLocation), (VOID*)(&TempLocation2)) == 0) {\r
1808 SHELL_FREE_NON_NULL(FileWithPath);\r
51429264 1809 return (Script_File_Name);\r
6ba2921d
JC
1810 }\r
1811 }\r
1812\r
1813 //\r
1814 // Was a file, but not a script. we treat this as an application.\r
1815 //\r
1816 SHELL_FREE_NON_NULL(FileWithPath);\r
51429264 1817 return (Efi_Application);\r
6ba2921d
JC
1818 }\r
1819 \r
1820 SHELL_FREE_NON_NULL(FileWithPath);\r
1821 //\r
1822 // No clue what this is... return invalid flag...\r
1823 //\r
51429264 1824 return (Unknown_Invalid);\r
6ba2921d
JC
1825}\r
1826\r
de4ebdcf
SQ
1827/**\r
1828 Determine if the first item in a command line is valid.\r
1829\r
1830 @param[in] CmdLine The command line to parse.\r
1831\r
1832 @retval EFI_SUCCESS The item is valid.\r
1833 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.\r
1834 @retval EFI_NOT_FOUND The operation type is unknown or invalid.\r
1835**/\r
6bd64463
JC
1836EFI_STATUS \r
1837EFIAPI\r
1838IsValidSplit(\r
1839 IN CONST CHAR16 *CmdLine\r
1840 )\r
1841{\r
1842 CHAR16 *Temp;\r
1843 CHAR16 *FirstParameter;\r
1844 CHAR16 *TempWalker;\r
1845 EFI_STATUS Status;\r
1846\r
1847 Temp = NULL;\r
1848\r
1849 Temp = StrnCatGrow(&Temp, NULL, CmdLine, 0);\r
1850 if (Temp == NULL) {\r
1851 return (EFI_OUT_OF_RESOURCES);\r
1852 }\r
1853\r
1854 FirstParameter = StrStr(Temp, L"|");\r
1855 if (FirstParameter != NULL) {\r
1856 *FirstParameter = CHAR_NULL;\r
1857 }\r
1858\r
1859 FirstParameter = NULL;\r
1860\r
1861 //\r
1862 // Process the command line\r
1863 //\r
1864 Status = ProcessCommandLineToFinal(&Temp);\r
1865\r
1866 if (!EFI_ERROR(Status)) {\r
1867 FirstParameter = AllocateZeroPool(StrSize(CmdLine));\r
1868 if (FirstParameter == NULL) {\r
1869 SHELL_FREE_NON_NULL(Temp);\r
1870 return (EFI_OUT_OF_RESOURCES);\r
1871 }\r
1872 TempWalker = (CHAR16*)Temp;\r
7f79b01e 1873 GetNextParameter(&TempWalker, &FirstParameter, StrSize(CmdLine));\r
6bd64463 1874\r
51429264 1875 if (GetOperationType(FirstParameter) == Unknown_Invalid) {\r
6bd64463
JC
1876 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_NOT_FOUND), ShellInfoObject.HiiHandle, FirstParameter);\r
1877 SetLastError(SHELL_NOT_FOUND);\r
1878 Status = EFI_NOT_FOUND;\r
1879 }\r
1880 }\r
1881\r
1882 SHELL_FREE_NON_NULL(Temp);\r
1883 SHELL_FREE_NON_NULL(FirstParameter);\r
1884 return Status;\r
1885}\r
1886\r
1887/**\r
51429264 1888 Determine if a command line contains with a split contains only valid commands.\r
6bd64463
JC
1889\r
1890 @param[in] CmdLine The command line to parse.\r
1891\r
1892 @retval EFI_SUCCESS CmdLine has only valid commands, application, or has no split.\r
1893 @retval EFI_ABORTED CmdLine has at least one invalid command or application.\r
1894**/\r
1895EFI_STATUS\r
1896EFIAPI\r
1897VerifySplit(\r
1898 IN CONST CHAR16 *CmdLine\r
1899 )\r
1900{\r
1901 CONST CHAR16 *TempSpot;\r
1902 EFI_STATUS Status;\r
1903\r
1904 //\r
1905 // Verify up to the pipe or end character\r
1906 //\r
1907 Status = IsValidSplit(CmdLine);\r
1908 if (EFI_ERROR(Status)) {\r
1909 return (Status);\r
1910 }\r
1911\r
1912 //\r
1913 // If this was the only item, then get out\r
1914 //\r
1915 if (!ContainsSplit(CmdLine)) {\r
1916 return (EFI_SUCCESS);\r
1917 }\r
1918\r
1919 //\r
1920 // recurse to verify the next item\r
1921 //\r
1922 TempSpot = FindSplit(CmdLine)+1;\r
1923 return (VerifySplit(TempSpot));\r
1924}\r
1925\r
680db511 1926/**\r
e7831c90
JC
1927 Process a split based operation.\r
1928\r
a308e058 1929 @param[in] CmdLine pointer to the command line to process\r
e7831c90
JC
1930\r
1931 @retval EFI_SUCCESS The operation was successful\r
1932 @return an error occured.\r
680db511
JC
1933**/\r
1934EFI_STATUS\r
1935EFIAPI\r
1936ProcessNewSplitCommandLine(\r
a308e058 1937 IN CONST CHAR16 *CmdLine\r
680db511
JC
1938 )\r
1939{\r
1940 SPLIT_LIST *Split;\r
1941 EFI_STATUS Status;\r
1942\r
6bd64463
JC
1943 Status = VerifySplit(CmdLine);\r
1944 if (EFI_ERROR(Status)) {\r
1945 return (Status);\r
1946 }\r
1947\r
680db511
JC
1948 Split = NULL;\r
1949\r
1950 //\r
1951 // are we in an existing split???\r
1952 //\r
1953 if (!IsListEmpty(&ShellInfoObject.SplitList.Link)) {\r
1954 Split = (SPLIT_LIST*)GetFirstNode(&ShellInfoObject.SplitList.Link);\r
1955 }\r
1956\r
1957 if (Split == NULL) {\r
a308e058 1958 Status = RunSplitCommand(CmdLine, NULL, NULL);\r
680db511 1959 } else {\r
a308e058 1960 Status = RunSplitCommand(CmdLine, Split->SplitStdIn, Split->SplitStdOut);\r
680db511
JC
1961 }\r
1962 if (EFI_ERROR(Status)) {\r
1963 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_INVALID_SPLIT), ShellInfoObject.HiiHandle, CmdLine);\r
1964 }\r
1965 return (Status);\r
1966}\r
daf70584
JC
1967\r
1968/**\r
51429264 1969 Handle a request to change the current file system.\r
daf70584 1970\r
51429264 1971 @param[in] CmdLine The passed in command line.\r
daf70584 1972\r
51429264 1973 @retval EFI_SUCCESS The operation was successful.\r
daf70584
JC
1974**/\r
1975EFI_STATUS\r
1976EFIAPI\r
1977ChangeMappedDrive(\r
1978 IN CONST CHAR16 *CmdLine\r
1979 )\r
1980{\r
1981 EFI_STATUS Status;\r
1982 Status = EFI_SUCCESS;\r
1983\r
1984 //\r
1985 // make sure we are the right operation\r
1986 //\r
1987 ASSERT(CmdLine[(StrLen(CmdLine)-1)] == L':' && StrStr(CmdLine, L" ") == NULL);\r
1988 \r
1989 //\r
1990 // Call the protocol API to do the work\r
1991 //\r
1992 Status = ShellInfoObject.NewEfiShellProtocol->SetCurDir(NULL, CmdLine);\r
1993\r
1994 //\r
1995 // Report any errors\r
1996 //\r
1997 if (EFI_ERROR(Status)) {\r
1998 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_INVALID_MAPPING), ShellInfoObject.HiiHandle, CmdLine);\r
1999 }\r
2000\r
2001 return (Status);\r
2002}\r
2003\r
5a5eb806
JC
2004/**\r
2005 Reprocess the command line to direct all -? to the help command.\r
2006\r
2007 if found, will add "help" as argv[0], and move the rest later.\r
2008\r
806c49db 2009 @param[in,out] CmdLine pointer to the command line to update\r
5a5eb806
JC
2010**/\r
2011EFI_STATUS\r
2012EFIAPI\r
806c49db
JC
2013DoHelpUpdate(\r
2014 IN OUT CHAR16 **CmdLine\r
5a5eb806
JC
2015 )\r
2016{\r
806c49db
JC
2017 CHAR16 *CurrentParameter;\r
2018 CHAR16 *Walker;\r
2019 CHAR16 *LastWalker;\r
2020 CHAR16 *NewCommandLine;\r
2021 EFI_STATUS Status;\r
2022\r
2023 Status = EFI_SUCCESS;\r
2024\r
2025 CurrentParameter = AllocateZeroPool(StrSize(*CmdLine));\r
2026 if (CurrentParameter == NULL) {\r
2027 return (EFI_OUT_OF_RESOURCES);\r
2028 }\r
2029\r
2030 Walker = *CmdLine;\r
2031 while(Walker != NULL && *Walker != CHAR_NULL) {\r
2032 LastWalker = Walker;\r
7f79b01e 2033 GetNextParameter(&Walker, &CurrentParameter, StrSize(*CmdLine));\r
806c49db
JC
2034 if (StrStr(CurrentParameter, L"-?") == CurrentParameter) {\r
2035 LastWalker[0] = L' ';\r
2036 LastWalker[1] = L' ';\r
2037 NewCommandLine = AllocateZeroPool(StrSize(L"help ") + StrSize(*CmdLine));\r
2038 if (NewCommandLine == NULL) {\r
2039 Status = EFI_OUT_OF_RESOURCES;\r
2040 break;\r
2041 }\r
7f79b01e
JC
2042\r
2043 //\r
2044 // We know the space is sufficient since we just calculated it.\r
2045 //\r
2046 StrnCpy(NewCommandLine, L"help ", 5);\r
2047 StrnCat(NewCommandLine, *CmdLine, StrLen(*CmdLine));\r
806c49db
JC
2048 SHELL_FREE_NON_NULL(*CmdLine);\r
2049 *CmdLine = NewCommandLine;\r
2050 break;\r
2051 }\r
2052 }\r
2053\r
2054 SHELL_FREE_NON_NULL(CurrentParameter);\r
2055\r
2056 return (Status);\r
2057}\r
2058\r
2059/**\r
51429264 2060 Function to update the shell variable "lasterror".\r
806c49db 2061\r
51429264 2062 @param[in] ErrorCode the error code to put into lasterror.\r
806c49db
JC
2063**/\r
2064EFI_STATUS\r
2065EFIAPI\r
2066SetLastError(\r
6bd64463 2067 IN CONST SHELL_STATUS ErrorCode\r
806c49db
JC
2068 )\r
2069{\r
2070 CHAR16 LeString[19];\r
2071 if (sizeof(EFI_STATUS) == sizeof(UINT64)) {\r
2072 UnicodeSPrint(LeString, sizeof(LeString), L"0x%Lx", ErrorCode);\r
2073 } else {\r
2074 UnicodeSPrint(LeString, sizeof(LeString), L"0x%x", ErrorCode);\r
2075 }\r
2076 DEBUG_CODE(InternalEfiShellSetEnv(L"debuglasterror", LeString, TRUE););\r
2077 InternalEfiShellSetEnv(L"lasterror", LeString, TRUE);\r
2078\r
2079 return (EFI_SUCCESS);\r
2080}\r
2081\r
2082/**\r
2083 Converts the command line to it's post-processed form. this replaces variables and alias' per UEFI Shell spec.\r
2084\r
2085 @param[in,out] CmdLine pointer to the command line to update\r
2086\r
2087 @retval EFI_SUCCESS The operation was successful\r
2088 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.\r
2089 @return some other error occured\r
2090**/\r
2091EFI_STATUS\r
2092EFIAPI\r
12a27a6d 2093ProcessCommandLineToFinal(\r
806c49db
JC
2094 IN OUT CHAR16 **CmdLine\r
2095 )\r
2096{\r
2097 EFI_STATUS Status;\r
2098 TrimSpaces(CmdLine);\r
2099\r
2100 Status = ShellSubstituteAliases(CmdLine);\r
2101 if (EFI_ERROR(Status)) {\r
2102 return (Status);\r
2103 }\r
2104\r
2105 TrimSpaces(CmdLine);\r
2106\r
2107 Status = ShellSubstituteVariables(CmdLine);\r
2108 if (EFI_ERROR(Status)) {\r
2109 return (Status);\r
2110 }\r
81cd2f53 2111 ASSERT (*CmdLine != NULL);\r
806c49db
JC
2112\r
2113 TrimSpaces(CmdLine);\r
2114\r
2115 //\r
2116 // update for help parsing\r
2117 //\r
2118 if (StrStr(*CmdLine, L"?") != NULL) {\r
2119 //\r
2120 // This may do nothing if the ? does not indicate help.\r
2121 // Save all the details for in the API below.\r
2122 //\r
2123 Status = DoHelpUpdate(CmdLine);\r
2124 }\r
2125\r
2126 TrimSpaces(CmdLine);\r
2127\r
2128 return (EFI_SUCCESS);\r
2129}\r
2130\r
2131/**\r
2132 Run an internal shell command.\r
2133\r
2134 This API will upadate the shell's environment since these commands are libraries.\r
2135 \r
2136 @param[in] CmdLine the command line to run.\r
2137 @param[in] FirstParameter the first parameter on the command line\r
2138 @param[in] ParamProtocol the shell parameters protocol pointer\r
2139\r
2140 @retval EFI_SUCCESS The command was completed.\r
2141 @retval EFI_ABORTED The command's operation was aborted.\r
2142**/\r
2143EFI_STATUS\r
2144EFIAPI\r
2145RunInternalCommand(\r
2146 IN CONST CHAR16 *CmdLine,\r
2147 IN CHAR16 *FirstParameter,\r
a308e058 2148 IN EFI_SHELL_PARAMETERS_PROTOCOL *ParamProtocol\r
806c49db
JC
2149)\r
2150{\r
2151 EFI_STATUS Status;\r
2152 UINTN Argc;\r
2153 CHAR16 **Argv;\r
2154 SHELL_STATUS CommandReturnedStatus;\r
2155 BOOLEAN LastError;\r
4b6b543e
QS
2156 CHAR16 *Walker;\r
2157 CHAR16 *NewCmdLine; \r
2158\r
2159 NewCmdLine = AllocateCopyPool (StrSize (CmdLine), CmdLine);\r
2160 if (NewCmdLine == NULL) {\r
2161 return EFI_OUT_OF_RESOURCES;\r
2162 }\r
2163\r
2164 for (Walker = NewCmdLine; Walker != NULL && *Walker != CHAR_NULL ; Walker++) {\r
2165 if (*Walker == L'^' && *(Walker+1) == L'#') {\r
2166 CopyMem(Walker, Walker+1, StrSize(Walker) - sizeof(Walker[0]));\r
2167 }\r
2168 }\r
806c49db 2169\r
5a5eb806 2170 //\r
806c49db 2171 // get the argc and argv updated for internal commands\r
5a5eb806 2172 //\r
4b6b543e 2173 Status = UpdateArgcArgv(ParamProtocol, NewCmdLine, &Argv, &Argc);\r
806c49db 2174 if (!EFI_ERROR(Status)) {\r
5a5eb806 2175 //\r
806c49db 2176 // Run the internal command.\r
5a5eb806 2177 //\r
806c49db
JC
2178 Status = ShellCommandRunCommandHandler(FirstParameter, &CommandReturnedStatus, &LastError);\r
2179\r
2180 if (!EFI_ERROR(Status)) {\r
5a5eb806 2181 //\r
806c49db
JC
2182 // Update last error status.\r
2183 // some commands do not update last error.\r
5a5eb806 2184 //\r
806c49db
JC
2185 if (LastError) {\r
2186 SetLastError(CommandReturnedStatus);\r
5a5eb806 2187 }\r
806c49db
JC
2188\r
2189 //\r
2190 // Pass thru the exitcode from the app.\r
2191 //\r
2192 if (ShellCommandGetExit()) {\r
e3eb7d82
JC
2193 //\r
2194 // An Exit was requested ("exit" command), pass its value up.\r
2195 //\r
806c49db 2196 Status = CommandReturnedStatus;\r
e3eb7d82
JC
2197 } else if (CommandReturnedStatus != SHELL_SUCCESS && IsScriptOnlyCommand(FirstParameter)) {\r
2198 //\r
2199 // Always abort when a script only command fails for any reason\r
2200 //\r
2201 Status = EFI_ABORTED;\r
2202 } else if (ShellCommandGetCurrentScriptFile() != NULL && CommandReturnedStatus == SHELL_ABORTED) {\r
2203 //\r
2204 // Abort when in a script and a command aborted\r
2205 //\r
806c49db 2206 Status = EFI_ABORTED;\r
5a5eb806 2207 }\r
806c49db
JC
2208 }\r
2209 }\r
2210\r
2211 //\r
2212 // This is guarenteed to be called after UpdateArgcArgv no matter what else happened.\r
2213 // This is safe even if the update API failed. In this case, it may be a no-op.\r
2214 //\r
2215 RestoreArgcArgv(ParamProtocol, &Argv, &Argc);\r
2216\r
e3eb7d82
JC
2217 //\r
2218 // If a script is running and the command is not a scipt only command, then\r
2219 // change return value to success so the script won't halt (unless aborted).\r
2220 //\r
2221 // Script only commands have to be able halt the script since the script will\r
2222 // not operate if they are failing.\r
2223 //\r
2224 if ( ShellCommandGetCurrentScriptFile() != NULL\r
2225 && !IsScriptOnlyCommand(FirstParameter)\r
2226 && Status != EFI_ABORTED\r
2227 ) {\r
806c49db
JC
2228 Status = EFI_SUCCESS;\r
2229 }\r
2230\r
4b6b543e 2231 FreePool (NewCmdLine);\r
806c49db
JC
2232 return (Status);\r
2233}\r
2234\r
2235/**\r
2236 Function to run the command or file.\r
2237\r
2238 @param[in] Type the type of operation being run.\r
2239 @param[in] CmdLine the command line to run.\r
2240 @param[in] FirstParameter the first parameter on the command line\r
2241 @param[in] ParamProtocol the shell parameters protocol pointer\r
2242\r
2243 @retval EFI_SUCCESS The command was completed.\r
2244 @retval EFI_ABORTED The command's operation was aborted.\r
2245**/\r
2246EFI_STATUS\r
2247EFIAPI\r
2248RunCommandOrFile(\r
2249 IN SHELL_OPERATION_TYPES Type,\r
2250 IN CONST CHAR16 *CmdLine,\r
2251 IN CHAR16 *FirstParameter,\r
a308e058 2252 IN EFI_SHELL_PARAMETERS_PROTOCOL *ParamProtocol\r
806c49db
JC
2253)\r
2254{\r
2255 EFI_STATUS Status;\r
cd39fe08 2256 EFI_STATUS StartStatus;\r
806c49db
JC
2257 CHAR16 *CommandWithPath;\r
2258 EFI_DEVICE_PATH_PROTOCOL *DevPath;\r
5223c121 2259 SHELL_STATUS CalleeExitStatus;\r
806c49db
JC
2260\r
2261 Status = EFI_SUCCESS;\r
2262 CommandWithPath = NULL;\r
2263 DevPath = NULL;\r
71c49eaf 2264 CalleeExitStatus = SHELL_INVALID_PARAMETER;\r
806c49db
JC
2265\r
2266 switch (Type) {\r
51429264 2267 case Internal_Command:\r
a308e058 2268 Status = RunInternalCommand(CmdLine, FirstParameter, ParamProtocol);\r
806c49db 2269 break;\r
51429264
SQ
2270 case Script_File_Name:\r
2271 case Efi_Application:\r
806c49db
JC
2272 //\r
2273 // Process a fully qualified path\r
2274 //\r
2275 if (StrStr(FirstParameter, L":") != NULL) {\r
2276 ASSERT (CommandWithPath == NULL);\r
2277 if (ShellIsFile(FirstParameter) == EFI_SUCCESS) {\r
2278 CommandWithPath = StrnCatGrow(&CommandWithPath, NULL, FirstParameter, 0);\r
2279 }\r
2280 }\r
2281\r
2282 //\r
2283 // Process a relative path and also check in the path environment variable\r
2284 //\r
2285 if (CommandWithPath == NULL) {\r
2286 CommandWithPath = ShellFindFilePathEx(FirstParameter, mExecutableExtensions);\r
2287 }\r
2288\r
2289 //\r
2290 // This should be impossible now.\r
2291 //\r
2292 ASSERT(CommandWithPath != NULL);\r
2293\r
2294 //\r
2295 // Make sure that path is not just a directory (or not found)\r
2296 //\r
2297 if (!EFI_ERROR(ShellIsDirectory(CommandWithPath))) {\r
2298 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_NOT_FOUND), ShellInfoObject.HiiHandle, FirstParameter);\r
6bd64463 2299 SetLastError(SHELL_NOT_FOUND);\r
806c49db
JC
2300 }\r
2301 switch (Type) {\r
51429264 2302 case Script_File_Name:\r
a308e058 2303 Status = RunScriptFile (CommandWithPath, NULL, CmdLine, ParamProtocol);\r
806c49db 2304 break;\r
51429264 2305 case Efi_Application:\r
806c49db
JC
2306 //\r
2307 // Get the device path of the application image\r
2308 //\r
2309 DevPath = ShellInfoObject.NewEfiShellProtocol->GetDevicePathFromFilePath(CommandWithPath);\r
2310 if (DevPath == NULL){\r
2311 Status = EFI_OUT_OF_RESOURCES;\r
2312 break;\r
2313 }\r
2314\r
2315 //\r
2316 // Execute the device path\r
2317 //\r
2318 Status = InternalShellExecuteDevicePath(\r
2319 &gImageHandle,\r
2320 DevPath,\r
2321 CmdLine,\r
2322 NULL,\r
a308e058 2323 &StartStatus\r
806c49db
JC
2324 );\r
2325\r
2326 SHELL_FREE_NON_NULL(DevPath);\r
2327\r
71c49eaf
SQ
2328 if(EFI_ERROR (Status)) {\r
2329 CalleeExitStatus = (SHELL_STATUS) (Status & (~MAX_BIT));\r
2330 } else {\r
cd39fe08 2331 CalleeExitStatus = (SHELL_STATUS) StartStatus;\r
71c49eaf
SQ
2332 }\r
2333\r
806c49db
JC
2334 //\r
2335 // Update last error status.\r
2336 //\r
5223c121 2337 // Status is an EFI_STATUS. Clear top bit to convert to SHELL_STATUS\r
71c49eaf 2338 SetLastError(CalleeExitStatus);\r
806c49db 2339 break;\r
6fa0da7d
LE
2340 default:\r
2341 //\r
2342 // Do nothing.\r
2343 //\r
2344 break;\r
5a5eb806
JC
2345 }\r
2346 break;\r
6fa0da7d
LE
2347 default:\r
2348 //\r
2349 // Do nothing.\r
2350 //\r
2351 break;\r
5a5eb806 2352 }\r
806c49db
JC
2353\r
2354 SHELL_FREE_NON_NULL(CommandWithPath);\r
2355\r
2356 return (Status);\r
2357}\r
2358\r
2359/**\r
2360 Function to setup StdIn, StdErr, StdOut, and then run the command or file.\r
2361\r
2362 @param[in] Type the type of operation being run.\r
2363 @param[in] CmdLine the command line to run.\r
2364 @param[in] FirstParameter the first parameter on the command line.\r
2365 @param[in] ParamProtocol the shell parameters protocol pointer\r
2366\r
2367 @retval EFI_SUCCESS The command was completed.\r
2368 @retval EFI_ABORTED The command's operation was aborted.\r
2369**/\r
2370EFI_STATUS\r
2371EFIAPI\r
2372SetupAndRunCommandOrFile(\r
a308e058
RN
2373 IN SHELL_OPERATION_TYPES Type,\r
2374 IN CHAR16 *CmdLine,\r
2375 IN CHAR16 *FirstParameter,\r
2376 IN EFI_SHELL_PARAMETERS_PROTOCOL *ParamProtocol\r
806c49db
JC
2377)\r
2378{\r
2379 EFI_STATUS Status;\r
2380 SHELL_FILE_HANDLE OriginalStdIn;\r
2381 SHELL_FILE_HANDLE OriginalStdOut;\r
2382 SHELL_FILE_HANDLE OriginalStdErr;\r
2383 SYSTEM_TABLE_INFO OriginalSystemTableInfo;\r
2384\r
2385 //\r
2386 // Update the StdIn, StdOut, and StdErr for redirection to environment variables, files, etc... unicode and ASCII\r
2387 //\r
2388 Status = UpdateStdInStdOutStdErr(ParamProtocol, CmdLine, &OriginalStdIn, &OriginalStdOut, &OriginalStdErr, &OriginalSystemTableInfo);\r
2389\r
2390 //\r
2391 // The StdIn, StdOut, and StdErr are set up.\r
2392 // Now run the command, script, or application\r
2393 //\r
2394 if (!EFI_ERROR(Status)) {\r
75eb337f 2395 TrimSpaces(&CmdLine);\r
a308e058 2396 Status = RunCommandOrFile(Type, CmdLine, FirstParameter, ParamProtocol);\r
806c49db
JC
2397 }\r
2398\r
2399 //\r
2400 // Now print errors\r
2401 //\r
2402 if (EFI_ERROR(Status)) {\r
2403 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_ERROR), ShellInfoObject.HiiHandle, (VOID*)(Status));\r
2404 }\r
2405\r
2406 //\r
2407 // put back the original StdIn, StdOut, and StdErr\r
2408 //\r
2409 RestoreStdInStdOutStdErr(ParamProtocol, &OriginalStdIn, &OriginalStdOut, &OriginalStdErr, &OriginalSystemTableInfo);\r
2410\r
2411 return (Status);\r
5a5eb806
JC
2412}\r
2413\r
a405b86d 2414/**\r
2415 Function will process and run a command line.\r
2416\r
2417 This will determine if the command line represents an internal shell \r
2418 command or dispatch an external application.\r
2419\r
2420 @param[in] CmdLine The command line to parse.\r
2421\r
2422 @retval EFI_SUCCESS The command was completed.\r
2423 @retval EFI_ABORTED The command's operation was aborted.\r
2424**/\r
2425EFI_STATUS\r
2426EFIAPI\r
2427RunCommand(\r
a308e058 2428 IN CONST CHAR16 *CmdLine\r
a405b86d 2429 )\r
2430{\r
2431 EFI_STATUS Status;\r
a405b86d 2432 CHAR16 *CleanOriginal;\r
806c49db
JC
2433 CHAR16 *FirstParameter;\r
2434 CHAR16 *TempWalker;\r
2435 SHELL_OPERATION_TYPES Type;\r
a405b86d 2436\r
2437 ASSERT(CmdLine != NULL);\r
2438 if (StrLen(CmdLine) == 0) {\r
2439 return (EFI_SUCCESS);\r
2440 }\r
2441\r
a405b86d 2442 Status = EFI_SUCCESS;\r
2443 CleanOriginal = NULL;\r
a405b86d 2444\r
2445 CleanOriginal = StrnCatGrow(&CleanOriginal, NULL, CmdLine, 0);\r
532691c8 2446 if (CleanOriginal == NULL) {\r
2447 return (EFI_OUT_OF_RESOURCES);\r
2448 }\r
284e034f 2449\r
ad2bc854 2450 TrimSpaces(&CleanOriginal);\r
a405b86d 2451\r
284e034f 2452 //\r
d41a79a0
CP
2453 // NULL out comments (leveraged from RunScriptFileHandle() ).\r
2454 // The # character on a line is used to denote that all characters on the same line\r
2455 // and to the right of the # are to be ignored by the shell.\r
2456 // Afterward, again remove spaces, in case any were between the last command-parameter and '#'.\r
2457 //\r
2458 for (TempWalker = CleanOriginal; TempWalker != NULL && *TempWalker != CHAR_NULL; TempWalker++) {\r
2459 if (*TempWalker == L'^') {\r
2460 if (*(TempWalker + 1) == L'#') {\r
4b6b543e 2461 TempWalker++;\r
d41a79a0
CP
2462 }\r
2463 } else if (*TempWalker == L'#') {\r
2464 *TempWalker = CHAR_NULL;\r
2465 }\r
2466 }\r
2467\r
2468 TrimSpaces(&CleanOriginal);\r
2469\r
2470 //\r
284e034f
CP
2471 // Handle case that passed in command line is just 1 or more " " characters.\r
2472 //\r
2473 if (StrLen (CleanOriginal) == 0) {\r
806c49db 2474 SHELL_FREE_NON_NULL(CleanOriginal);\r
284e034f
CP
2475 return (EFI_SUCCESS);\r
2476 }\r
2477\r
12a27a6d 2478 Status = ProcessCommandLineToFinal(&CleanOriginal);\r
ca53c0af 2479 if (EFI_ERROR(Status)) {\r
806c49db 2480 SHELL_FREE_NON_NULL(CleanOriginal);\r
ca53c0af 2481 return (Status);\r
a405b86d 2482 }\r
2483\r
a405b86d 2484 //\r
2485 // We dont do normal processing with a split command line (output from one command input to another)\r
2486 //\r
1ef61d03 2487 if (ContainsSplit(CleanOriginal)) {\r
a308e058 2488 Status = ProcessNewSplitCommandLine(CleanOriginal);\r
806c49db
JC
2489 SHELL_FREE_NON_NULL(CleanOriginal);\r
2490 return (Status);\r
2491 } \r
5b5cd144 2492\r
806c49db
JC
2493 //\r
2494 // We need the first parameter information so we can determine the operation type\r
2495 //\r
2496 FirstParameter = AllocateZeroPool(StrSize(CleanOriginal));\r
2497 if (FirstParameter == NULL) {\r
2498 SHELL_FREE_NON_NULL(CleanOriginal);\r
2499 return (EFI_OUT_OF_RESOURCES);\r
2500 }\r
2501 TempWalker = CleanOriginal;\r
7f79b01e 2502 GetNextParameter(&TempWalker, &FirstParameter, StrSize(CleanOriginal));\r
64c7a749 2503\r
806c49db
JC
2504 //\r
2505 // Depending on the first parameter we change the behavior\r
2506 //\r
2507 switch (Type = GetOperationType(FirstParameter)) {\r
51429264 2508 case File_Sys_Change:\r
d6e88a6c 2509 Status = ChangeMappedDrive (FirstParameter);\r
806c49db 2510 break;\r
51429264
SQ
2511 case Internal_Command:\r
2512 case Script_File_Name:\r
2513 case Efi_Application:\r
a308e058 2514 Status = SetupAndRunCommandOrFile(Type, CleanOriginal, FirstParameter, ShellInfoObject.NewShellParametersProtocol);\r
806c49db
JC
2515 break;\r
2516 default:\r
64c7a749 2517 //\r
806c49db 2518 // Whatever was typed, it was invalid.\r
64c7a749 2519 //\r
806c49db 2520 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_NOT_FOUND), ShellInfoObject.HiiHandle, FirstParameter);\r
6bd64463 2521 SetLastError(SHELL_NOT_FOUND);\r
806c49db 2522 break;\r
a405b86d 2523 }\r
806c49db 2524 \r
ca53c0af 2525 SHELL_FREE_NON_NULL(CleanOriginal);\r
806c49db 2526 SHELL_FREE_NON_NULL(FirstParameter);\r
a405b86d 2527\r
2528 return (Status);\r
2529}\r
2530\r
2531STATIC CONST UINT16 InvalidChars[] = {L'*', L'?', L'<', L'>', L'\\', L'/', L'\"', 0x0001, 0x0002};\r
2532/**\r
2533 Function determins if the CommandName COULD be a valid command. It does not determine whether\r
2534 this is a valid command. It only checks for invalid characters.\r
2535\r
2536 @param[in] CommandName The name to check\r
2537\r
2538 @retval TRUE CommandName could be a command name\r
2539 @retval FALSE CommandName could not be a valid command name\r
2540**/\r
2541BOOLEAN\r
2542EFIAPI\r
2543IsValidCommandName(\r
2544 IN CONST CHAR16 *CommandName\r
2545 )\r
2546{\r
2547 UINTN Count;\r
2548 if (CommandName == NULL) {\r
2549 ASSERT(FALSE);\r
2550 return (FALSE);\r
2551 }\r
2552 for ( Count = 0\r
2553 ; Count < sizeof(InvalidChars) / sizeof(InvalidChars[0])\r
2554 ; Count++\r
2555 ){\r
2556 if (ScanMem16(CommandName, StrSize(CommandName), InvalidChars[Count]) != NULL) {\r
2557 return (FALSE);\r
2558 }\r
2559 }\r
2560 return (TRUE);\r
2561}\r
2562\r
2563/**\r
2564 Function to process a NSH script file via SHELL_FILE_HANDLE.\r
2565\r
2566 @param[in] Handle The handle to the already opened file.\r
2567 @param[in] Name The name of the script file.\r
2568\r
2569 @retval EFI_SUCCESS the script completed sucessfully\r
2570**/\r
2571EFI_STATUS\r
2572EFIAPI\r
2573RunScriptFileHandle (\r
a308e058
RN
2574 IN SHELL_FILE_HANDLE Handle,\r
2575 IN CONST CHAR16 *Name\r
a405b86d 2576 )\r
2577{\r
2578 EFI_STATUS Status;\r
2579 SCRIPT_FILE *NewScriptFile;\r
2580 UINTN LoopVar;\r
2581 CHAR16 *CommandLine;\r
2582 CHAR16 *CommandLine2;\r
2583 CHAR16 *CommandLine3;\r
2584 SCRIPT_COMMAND_LIST *LastCommand;\r
2585 BOOLEAN Ascii;\r
2586 BOOLEAN PreScriptEchoState;\r
848997b5 2587 BOOLEAN PreCommandEchoState;\r
a405b86d 2588 CONST CHAR16 *CurDir;\r
2589 UINTN LineCount;\r
b6b22b13 2590 CHAR16 LeString[50];\r
a405b86d 2591\r
2592 ASSERT(!ShellCommandGetScriptExit());\r
2593\r
2594 PreScriptEchoState = ShellCommandGetEchoState();\r
2595\r
2596 NewScriptFile = (SCRIPT_FILE*)AllocateZeroPool(sizeof(SCRIPT_FILE));\r
3c865f20 2597 if (NewScriptFile == NULL) {\r
2598 return (EFI_OUT_OF_RESOURCES);\r
2599 }\r
a405b86d 2600\r
2601 //\r
2602 // Set up the name\r
2603 //\r
2604 ASSERT(NewScriptFile->ScriptName == NULL);\r
2605 NewScriptFile->ScriptName = StrnCatGrow(&NewScriptFile->ScriptName, NULL, Name, 0);\r
3c865f20 2606 if (NewScriptFile->ScriptName == NULL) {\r
2607 DeleteScriptFileStruct(NewScriptFile);\r
2608 return (EFI_OUT_OF_RESOURCES);\r
2609 }\r
a405b86d 2610\r
2611 //\r
2612 // Save the parameters (used to replace %0 to %9 later on)\r
2613 //\r
2614 NewScriptFile->Argc = ShellInfoObject.NewShellParametersProtocol->Argc;\r
2615 if (NewScriptFile->Argc != 0) {\r
2616 NewScriptFile->Argv = (CHAR16**)AllocateZeroPool(NewScriptFile->Argc * sizeof(CHAR16*));\r
3c865f20 2617 if (NewScriptFile->Argv == NULL) {\r
2618 DeleteScriptFileStruct(NewScriptFile);\r
2619 return (EFI_OUT_OF_RESOURCES);\r
2620 }\r
a405b86d 2621 for (LoopVar = 0 ; LoopVar < 10 && LoopVar < NewScriptFile->Argc; LoopVar++) {\r
2622 ASSERT(NewScriptFile->Argv[LoopVar] == NULL);\r
2623 NewScriptFile->Argv[LoopVar] = StrnCatGrow(&NewScriptFile->Argv[LoopVar], NULL, ShellInfoObject.NewShellParametersProtocol->Argv[LoopVar], 0);\r
3c865f20 2624 if (NewScriptFile->Argv[LoopVar] == NULL) {\r
2625 DeleteScriptFileStruct(NewScriptFile);\r
2626 return (EFI_OUT_OF_RESOURCES);\r
2627 }\r
a405b86d 2628 }\r
2629 } else {\r
2630 NewScriptFile->Argv = NULL;\r
2631 }\r
2632\r
2633 InitializeListHead(&NewScriptFile->CommandList);\r
2634 InitializeListHead(&NewScriptFile->SubstList);\r
2635\r
2636 //\r
2637 // Now build the list of all script commands.\r
2638 //\r
2639 LineCount = 0;\r
2640 while(!ShellFileHandleEof(Handle)) {\r
2641 CommandLine = ShellFileHandleReturnLine(Handle, &Ascii);\r
2642 LineCount++;\r
4922715d
JC
2643 if (CommandLine == NULL || StrLen(CommandLine) == 0 || CommandLine[0] == '#') {\r
2644 SHELL_FREE_NON_NULL(CommandLine);\r
a405b86d 2645 continue;\r
2646 }\r
2647 NewScriptFile->CurrentCommand = AllocateZeroPool(sizeof(SCRIPT_COMMAND_LIST));\r
3c865f20 2648 if (NewScriptFile->CurrentCommand == NULL) {\r
4922715d 2649 SHELL_FREE_NON_NULL(CommandLine);\r
3c865f20 2650 DeleteScriptFileStruct(NewScriptFile);\r
2651 return (EFI_OUT_OF_RESOURCES);\r
2652 }\r
a405b86d 2653\r
2654 NewScriptFile->CurrentCommand->Cl = CommandLine;\r
2655 NewScriptFile->CurrentCommand->Data = NULL;\r
2656 NewScriptFile->CurrentCommand->Line = LineCount;\r
2657\r
2658 InsertTailList(&NewScriptFile->CommandList, &NewScriptFile->CurrentCommand->Link);\r
2659 }\r
2660\r
2661 //\r
2662 // Add this as the topmost script file\r
2663 //\r
2664 ShellCommandSetNewScript (NewScriptFile);\r
2665\r
2666 //\r
2667 // Now enumerate through the commands and run each one.\r
2668 //\r
733f138d 2669 CommandLine = AllocateZeroPool(PcdGet16(PcdShellPrintBufferSize));\r
3c865f20 2670 if (CommandLine == NULL) {\r
2671 DeleteScriptFileStruct(NewScriptFile);\r
2672 return (EFI_OUT_OF_RESOURCES);\r
2673 }\r
733f138d 2674 CommandLine2 = AllocateZeroPool(PcdGet16(PcdShellPrintBufferSize));\r
3c865f20 2675 if (CommandLine2 == NULL) {\r
2676 FreePool(CommandLine);\r
2677 DeleteScriptFileStruct(NewScriptFile);\r
2678 return (EFI_OUT_OF_RESOURCES);\r
2679 }\r
a405b86d 2680\r
2681 for ( NewScriptFile->CurrentCommand = (SCRIPT_COMMAND_LIST *)GetFirstNode(&NewScriptFile->CommandList)\r
2682 ; !IsNull(&NewScriptFile->CommandList, &NewScriptFile->CurrentCommand->Link)\r
2683 ; // conditional increment in the body of the loop\r
2684 ){\r
3c865f20 2685 ASSERT(CommandLine2 != NULL);\r
7f79b01e 2686 StrnCpy(CommandLine2, NewScriptFile->CurrentCommand->Cl, PcdGet16(PcdShellPrintBufferSize)/sizeof(CHAR16)-1);\r
a405b86d 2687\r
2688 //\r
2689 // NULL out comments\r
2690 //\r
2691 for (CommandLine3 = CommandLine2 ; CommandLine3 != NULL && *CommandLine3 != CHAR_NULL ; CommandLine3++) {\r
2692 if (*CommandLine3 == L'^') {\r
4b6b543e 2693 if ( *(CommandLine3+1) == L':') {\r
a405b86d 2694 CopyMem(CommandLine3, CommandLine3+1, StrSize(CommandLine3) - sizeof(CommandLine3[0]));\r
4b6b543e
QS
2695 } else if (*(CommandLine3+1) == L'#') {\r
2696 CommandLine3++;\r
a405b86d 2697 }\r
2698 } else if (*CommandLine3 == L'#') {\r
2699 *CommandLine3 = CHAR_NULL;\r
2700 }\r
2701 }\r
2702\r
2703 if (CommandLine2 != NULL && StrLen(CommandLine2) >= 1) {\r
2704 //\r
2705 // Due to variability in starting the find and replace action we need to have both buffers the same.\r
2706 //\r
7f79b01e 2707 StrnCpy(CommandLine, CommandLine2, PcdGet16(PcdShellPrintBufferSize)/sizeof(CHAR16)-1);\r
a405b86d 2708\r
2709 //\r
2710 // Remove the %0 to %9 from the command line (if we have some arguments)\r
2711 //\r
2712 if (NewScriptFile->Argv != NULL) {\r
2713 switch (NewScriptFile->Argc) {\r
2714 default:\r
a753677e 2715 Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PcdGet16 (PcdShellPrintBufferSize), L"%9", NewScriptFile->Argv[9], FALSE, FALSE);\r
a405b86d 2716 ASSERT_EFI_ERROR(Status);\r
2717 case 9:\r
a753677e 2718 Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PcdGet16 (PcdShellPrintBufferSize), L"%8", NewScriptFile->Argv[8], FALSE, FALSE);\r
a405b86d 2719 ASSERT_EFI_ERROR(Status);\r
2720 case 8:\r
a753677e 2721 Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PcdGet16 (PcdShellPrintBufferSize), L"%7", NewScriptFile->Argv[7], FALSE, FALSE);\r
a405b86d 2722 ASSERT_EFI_ERROR(Status);\r
2723 case 7:\r
a753677e 2724 Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PcdGet16 (PcdShellPrintBufferSize), L"%6", NewScriptFile->Argv[6], FALSE, FALSE);\r
a405b86d 2725 ASSERT_EFI_ERROR(Status);\r
2726 case 6:\r
a753677e 2727 Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PcdGet16 (PcdShellPrintBufferSize), L"%5", NewScriptFile->Argv[5], FALSE, FALSE);\r
a405b86d 2728 ASSERT_EFI_ERROR(Status);\r
2729 case 5:\r
a753677e 2730 Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PcdGet16 (PcdShellPrintBufferSize), L"%4", NewScriptFile->Argv[4], FALSE, FALSE);\r
a405b86d 2731 ASSERT_EFI_ERROR(Status);\r
2732 case 4:\r
a753677e 2733 Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PcdGet16 (PcdShellPrintBufferSize), L"%3", NewScriptFile->Argv[3], FALSE, FALSE);\r
a405b86d 2734 ASSERT_EFI_ERROR(Status);\r
2735 case 3:\r
a753677e 2736 Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PcdGet16 (PcdShellPrintBufferSize), L"%2", NewScriptFile->Argv[2], FALSE, FALSE);\r
a405b86d 2737 ASSERT_EFI_ERROR(Status);\r
2738 case 2:\r
a753677e 2739 Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PcdGet16 (PcdShellPrintBufferSize), L"%1", NewScriptFile->Argv[1], FALSE, FALSE);\r
a405b86d 2740 ASSERT_EFI_ERROR(Status);\r
2741 case 1:\r
a753677e 2742 Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PcdGet16 (PcdShellPrintBufferSize), L"%0", NewScriptFile->Argv[0], FALSE, FALSE);\r
a405b86d 2743 ASSERT_EFI_ERROR(Status);\r
2744 break;\r
2745 case 0:\r
2746 break;\r
2747 }\r
2748 }\r
2749 Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PcdGet16 (PcdShellPrintBufferSize), L"%1", L"\"\"", FALSE, FALSE);\r
2750 Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PcdGet16 (PcdShellPrintBufferSize), L"%2", L"\"\"", FALSE, FALSE);\r
2751 Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PcdGet16 (PcdShellPrintBufferSize), L"%3", L"\"\"", FALSE, FALSE);\r
2752 Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PcdGet16 (PcdShellPrintBufferSize), L"%4", L"\"\"", FALSE, FALSE);\r
2753 Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PcdGet16 (PcdShellPrintBufferSize), L"%5", L"\"\"", FALSE, FALSE);\r
2754 Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PcdGet16 (PcdShellPrintBufferSize), L"%6", L"\"\"", FALSE, FALSE);\r
2755 Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PcdGet16 (PcdShellPrintBufferSize), L"%7", L"\"\"", FALSE, FALSE);\r
2756 Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PcdGet16 (PcdShellPrintBufferSize), L"%8", L"\"\"", FALSE, FALSE);\r
2757 Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PcdGet16 (PcdShellPrintBufferSize), L"%9", L"\"\"", FALSE, FALSE);\r
2758\r
7f79b01e 2759 StrnCpy(CommandLine2, CommandLine, PcdGet16(PcdShellPrintBufferSize)/sizeof(CHAR16)-1);\r
a405b86d 2760\r
2761 LastCommand = NewScriptFile->CurrentCommand;\r
2762\r
2763 for (CommandLine3 = CommandLine2 ; CommandLine3[0] == L' ' ; CommandLine3++);\r
2764\r
3c865f20 2765 if (CommandLine3 != NULL && CommandLine3[0] == L':' ) {\r
a405b86d 2766 //\r
2767 // This line is a goto target / label\r
2768 //\r
2769 } else {\r
2770 if (CommandLine3 != NULL && StrLen(CommandLine3) > 0) {\r
848997b5 2771 if (CommandLine3[0] == L'@') {\r
2772 //\r
2773 // We need to save the current echo state\r
2774 // and disable echo for just this command.\r
2775 //\r
2776 PreCommandEchoState = ShellCommandGetEchoState();\r
2777 ShellCommandSetEchoState(FALSE);\r
a308e058 2778 Status = RunCommand(CommandLine3+1);\r
848997b5 2779\r
2780 //\r
284e034f 2781 // If command was "@echo -off" or "@echo -on" then don't restore echo state\r
848997b5 2782 //\r
284e034f
CP
2783 if (StrCmp (L"@echo -off", CommandLine3) != 0 &&\r
2784 StrCmp (L"@echo -on", CommandLine3) != 0) {\r
2785 //\r
2786 // Now restore the pre-'@' echo state.\r
2787 //\r
2788 ShellCommandSetEchoState(PreCommandEchoState);\r
2789 }\r
848997b5 2790 } else {\r
0d11446d 2791 if (ShellCommandGetEchoState()) {\r
2792 CurDir = ShellInfoObject.NewEfiShellProtocol->GetEnv(L"cwd");\r
2793 if (CurDir != NULL && StrLen(CurDir) > 1) {\r
2794 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_CURDIR), ShellInfoObject.HiiHandle, CurDir);\r
2795 } else {\r
2796 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_SHELL), ShellInfoObject.HiiHandle);\r
2797 }\r
2798 ShellPrintEx(-1, -1, L"%s\r\n", CommandLine2);\r
2799 }\r
a308e058 2800 Status = RunCommand(CommandLine3);\r
848997b5 2801 }\r
a405b86d 2802 }\r
2803\r
2804 if (ShellCommandGetScriptExit()) {\r
5b5cd144
CP
2805 //\r
2806 // ShellCommandGetExitCode() always returns a UINT64\r
2807 //\r
a308e058 2808 UnicodeSPrint(LeString, sizeof(LeString), L"0x%Lx", ShellCommandGetExitCode());\r
5b5cd144
CP
2809 DEBUG_CODE(InternalEfiShellSetEnv(L"debuglasterror", LeString, TRUE););\r
2810 InternalEfiShellSetEnv(L"lasterror", LeString, TRUE);\r
b6b22b13 2811\r
2812 ShellCommandRegisterExit(FALSE, 0);\r
a405b86d 2813 Status = EFI_SUCCESS;\r
2814 break;\r
2815 }\r
c6a7fef8
ED
2816 if (ShellGetExecutionBreakFlag()) {\r
2817 break;\r
2818 }\r
a405b86d 2819 if (EFI_ERROR(Status)) {\r
2820 break;\r
2821 }\r
2822 if (ShellCommandGetExit()) {\r
2823 break;\r
2824 }\r
2825 }\r
2826 //\r
2827 // If that commend did not update the CurrentCommand then we need to advance it...\r
2828 //\r
2829 if (LastCommand == NewScriptFile->CurrentCommand) {\r
2830 NewScriptFile->CurrentCommand = (SCRIPT_COMMAND_LIST *)GetNextNode(&NewScriptFile->CommandList, &NewScriptFile->CurrentCommand->Link);\r
2831 if (!IsNull(&NewScriptFile->CommandList, &NewScriptFile->CurrentCommand->Link)) {\r
2832 NewScriptFile->CurrentCommand->Reset = TRUE;\r
2833 }\r
2834 }\r
2835 } else {\r
2836 NewScriptFile->CurrentCommand = (SCRIPT_COMMAND_LIST *)GetNextNode(&NewScriptFile->CommandList, &NewScriptFile->CurrentCommand->Link);\r
2837 if (!IsNull(&NewScriptFile->CommandList, &NewScriptFile->CurrentCommand->Link)) {\r
2838 NewScriptFile->CurrentCommand->Reset = TRUE;\r
2839 }\r
2840 }\r
2841 }\r
2842\r
a405b86d 2843\r
2844 FreePool(CommandLine);\r
2845 FreePool(CommandLine2);\r
2846 ShellCommandSetNewScript (NULL);\r
733f138d 2847\r
2848 //\r
2849 // Only if this was the last script reset the state.\r
2850 //\r
2851 if (ShellCommandGetCurrentScriptFile()==NULL) {\r
2852 ShellCommandSetEchoState(PreScriptEchoState);\r
2853 }\r
a405b86d 2854 return (EFI_SUCCESS);\r
2855}\r
2856\r
2857/**\r
2858 Function to process a NSH script file.\r
2859\r
2860 @param[in] ScriptPath Pointer to the script file name (including file system path).\r
e958b946
JC
2861 @param[in] Handle the handle of the script file already opened.\r
2862 @param[in] CmdLine the command line to run.\r
2863 @param[in] ParamProtocol the shell parameters protocol pointer\r
a405b86d 2864\r
2865 @retval EFI_SUCCESS the script completed sucessfully\r
2866**/\r
2867EFI_STATUS\r
2868EFIAPI\r
2869RunScriptFile (\r
a308e058
RN
2870 IN CONST CHAR16 *ScriptPath,\r
2871 IN SHELL_FILE_HANDLE Handle OPTIONAL,\r
2872 IN CONST CHAR16 *CmdLine,\r
2873 IN EFI_SHELL_PARAMETERS_PROTOCOL *ParamProtocol\r
a405b86d 2874 )\r
2875{\r
2876 EFI_STATUS Status;\r
2877 SHELL_FILE_HANDLE FileHandle;\r
e958b946
JC
2878 UINTN Argc;\r
2879 CHAR16 **Argv;\r
a405b86d 2880\r
2881 if (ShellIsFile(ScriptPath) != EFI_SUCCESS) {\r
2882 return (EFI_INVALID_PARAMETER);\r
2883 }\r
2884\r
e958b946
JC
2885 //\r
2886 // get the argc and argv updated for scripts\r
2887 //\r
2888 Status = UpdateArgcArgv(ParamProtocol, CmdLine, &Argv, &Argc);\r
2889 if (!EFI_ERROR(Status)) {\r
a405b86d 2890\r
e958b946
JC
2891 if (Handle == NULL) {\r
2892 //\r
2893 // open the file\r
2894 //\r
2895 Status = ShellOpenFileByName(ScriptPath, &FileHandle, EFI_FILE_MODE_READ, 0);\r
2896 if (!EFI_ERROR(Status)) {\r
2897 //\r
2898 // run it\r
2899 //\r
a308e058 2900 Status = RunScriptFileHandle(FileHandle, ScriptPath);\r
a405b86d 2901\r
e958b946
JC
2902 //\r
2903 // now close the file\r
2904 //\r
2905 ShellCloseFile(&FileHandle);\r
2906 }\r
2907 } else {\r
a308e058 2908 Status = RunScriptFileHandle(Handle, ScriptPath);\r
e958b946
JC
2909 }\r
2910 }\r
2911\r
2912 //\r
2913 // This is guarenteed to be called after UpdateArgcArgv no matter what else happened.\r
2914 // This is safe even if the update API failed. In this case, it may be a no-op.\r
2915 //\r
2916 RestoreArgcArgv(ParamProtocol, &Argv, &Argc);\r
a405b86d 2917\r
2918 return (Status);\r
2919}\r