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