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