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