]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Application/Shell/Shell.c
ShellPkg: Refactor change to current file system
[mirror_edk2.git] / ShellPkg / Application / Shell / Shell.c
1 /** @file
2 This is THE shell (application)
3
4 Copyright (c) 2009 - 2013, 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 at the end of the (*String).
95 //
96 while ((*String)[StrLen((*String))-1] == L' ') {
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
111 CHAR16*
112 EFIAPI
113 FindSplit(
114 IN CONST CHAR16 *CmdLine
115 )
116 {
117 CONST CHAR16 *TempSpot;
118 TempSpot = NULL;
119 if (StrStr(CmdLine, L"|") != NULL) {
120 for (TempSpot = CmdLine ; TempSpot != NULL && *TempSpot != CHAR_NULL ; TempSpot++) {
121 if (*TempSpot == L'^' && *(TempSpot+1) == L'|') {
122 TempSpot++;
123 } else if (*TempSpot == L'|') {
124 break;
125 }
126 }
127 }
128 return (TempSpot);
129 }
130
131 /**
132 Determine if a command line contains a split operation
133
134 @param[in] CmdLine The command line to parse.
135
136 @retval TRUE CmdLine has a valid split.
137 @retval FALSE CmdLine does not have a valid split.
138 **/
139 BOOLEAN
140 EFIAPI
141 ContainsSplit(
142 IN CONST CHAR16 *CmdLine
143 )
144 {
145 CONST CHAR16 *TempSpot;
146 TempSpot = FindSplit(CmdLine);
147 return (TempSpot != NULL && *TempSpot != CHAR_NULL);
148 }
149
150 /**
151 Function to start monitoring for CTRL-S using SimpleTextInputEx. This
152 feature's enabled state was not known when the shell initially launched.
153
154 @retval EFI_SUCCESS The feature is enabled.
155 @retval EFI_OUT_OF_RESOURCES There is not enough mnemory available.
156 **/
157 EFI_STATUS
158 EFIAPI
159 InternalEfiShellStartCtrlSMonitor(
160 VOID
161 )
162 {
163 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *SimpleEx;
164 EFI_KEY_DATA KeyData;
165 EFI_STATUS Status;
166
167 Status = gBS->OpenProtocol(
168 gST->ConsoleInHandle,
169 &gEfiSimpleTextInputExProtocolGuid,
170 (VOID**)&SimpleEx,
171 gImageHandle,
172 NULL,
173 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
174 if (EFI_ERROR(Status)) {
175 ShellPrintHiiEx(
176 -1,
177 -1,
178 NULL,
179 STRING_TOKEN (STR_SHELL_NO_IN_EX),
180 ShellInfoObject.HiiHandle);
181 return (EFI_SUCCESS);
182 }
183
184 KeyData.KeyState.KeyToggleState = 0;
185 KeyData.Key.ScanCode = 0;
186 KeyData.KeyState.KeyShiftState = EFI_SHIFT_STATE_VALID|EFI_LEFT_CONTROL_PRESSED;
187 KeyData.Key.UnicodeChar = L's';
188
189 Status = SimpleEx->RegisterKeyNotify(
190 SimpleEx,
191 &KeyData,
192 NotificationFunction,
193 &ShellInfoObject.CtrlSNotifyHandle1);
194
195 KeyData.KeyState.KeyShiftState = EFI_SHIFT_STATE_VALID|EFI_RIGHT_CONTROL_PRESSED;
196 if (!EFI_ERROR(Status)) {
197 Status = SimpleEx->RegisterKeyNotify(
198 SimpleEx,
199 &KeyData,
200 NotificationFunction,
201 &ShellInfoObject.CtrlSNotifyHandle2);
202 }
203 KeyData.KeyState.KeyShiftState = EFI_SHIFT_STATE_VALID|EFI_LEFT_CONTROL_PRESSED;
204 KeyData.Key.UnicodeChar = 19;
205
206 if (!EFI_ERROR(Status)) {
207 Status = SimpleEx->RegisterKeyNotify(
208 SimpleEx,
209 &KeyData,
210 NotificationFunction,
211 &ShellInfoObject.CtrlSNotifyHandle3);
212 }
213 KeyData.KeyState.KeyShiftState = EFI_SHIFT_STATE_VALID|EFI_RIGHT_CONTROL_PRESSED;
214 if (!EFI_ERROR(Status)) {
215 Status = SimpleEx->RegisterKeyNotify(
216 SimpleEx,
217 &KeyData,
218 NotificationFunction,
219 &ShellInfoObject.CtrlSNotifyHandle4);
220 }
221 return (Status);
222 }
223
224
225
226 /**
227 The entry point for the application.
228
229 @param[in] ImageHandle The firmware allocated handle for the EFI image.
230 @param[in] SystemTable A pointer to the EFI System Table.
231
232 @retval EFI_SUCCESS The entry point is executed successfully.
233 @retval other Some error occurs when executing this entry point.
234
235 **/
236 EFI_STATUS
237 EFIAPI
238 UefiMain (
239 IN EFI_HANDLE ImageHandle,
240 IN EFI_SYSTEM_TABLE *SystemTable
241 )
242 {
243 EFI_STATUS Status;
244 CHAR16 *TempString;
245 UINTN Size;
246 EFI_HANDLE ConInHandle;
247 EFI_SIMPLE_TEXT_INPUT_PROTOCOL *OldConIn;
248
249 if (PcdGet8(PcdShellSupportLevel) > 3) {
250 return (EFI_UNSUPPORTED);
251 }
252
253 //
254 // Clear the screen
255 //
256 Status = gST->ConOut->ClearScreen(gST->ConOut);
257 if (EFI_ERROR(Status)) {
258 return (Status);
259 }
260
261 //
262 // Populate the global structure from PCDs
263 //
264 ShellInfoObject.ImageDevPath = NULL;
265 ShellInfoObject.FileDevPath = NULL;
266 ShellInfoObject.PageBreakEnabled = PcdGetBool(PcdShellPageBreakDefault);
267 ShellInfoObject.ViewingSettings.InsertMode = PcdGetBool(PcdShellInsertModeDefault);
268 ShellInfoObject.LogScreenCount = PcdGet8 (PcdShellScreenLogCount );
269
270 //
271 // verify we dont allow for spec violation
272 //
273 ASSERT(ShellInfoObject.LogScreenCount >= 3);
274
275 //
276 // Initialize the LIST ENTRY objects...
277 //
278 InitializeListHead(&ShellInfoObject.BufferToFreeList.Link);
279 InitializeListHead(&ShellInfoObject.ViewingSettings.CommandHistory.Link);
280 InitializeListHead(&ShellInfoObject.SplitList.Link);
281
282 //
283 // Check PCDs for optional features that are not implemented yet.
284 //
285 if ( PcdGetBool(PcdShellSupportOldProtocols)
286 || !FeaturePcdGet(PcdShellRequireHiiPlatform)
287 || FeaturePcdGet(PcdShellSupportFrameworkHii)
288 ) {
289 return (EFI_UNSUPPORTED);
290 }
291
292 //
293 // turn off the watchdog timer
294 //
295 gBS->SetWatchdogTimer (0, 0, 0, NULL);
296
297 //
298 // install our console logger. This will keep a log of the output for back-browsing
299 //
300 Status = ConsoleLoggerInstall(ShellInfoObject.LogScreenCount, &ShellInfoObject.ConsoleInfo);
301 if (!EFI_ERROR(Status)) {
302 //
303 // Enable the cursor to be visible
304 //
305 gST->ConOut->EnableCursor (gST->ConOut, TRUE);
306
307 //
308 // If supporting EFI 1.1 we need to install HII protocol
309 // only do this if PcdShellRequireHiiPlatform == FALSE
310 //
311 // remove EFI_UNSUPPORTED check above when complete.
312 ///@todo add support for Framework HII
313
314 //
315 // install our (solitary) HII package
316 //
317 ShellInfoObject.HiiHandle = HiiAddPackages (&gEfiCallerIdGuid, gImageHandle, ShellStrings, NULL);
318 if (ShellInfoObject.HiiHandle == NULL) {
319 if (PcdGetBool(PcdShellSupportFrameworkHii)) {
320 ///@todo Add our package into Framework HII
321 }
322 if (ShellInfoObject.HiiHandle == NULL) {
323 return (EFI_NOT_STARTED);
324 }
325 }
326
327 //
328 // create and install the EfiShellParametersProtocol
329 //
330 Status = CreatePopulateInstallShellParametersProtocol(&ShellInfoObject.NewShellParametersProtocol, &ShellInfoObject.RootShellInstance);
331 ASSERT_EFI_ERROR(Status);
332 ASSERT(ShellInfoObject.NewShellParametersProtocol != NULL);
333
334 //
335 // create and install the EfiShellProtocol
336 //
337 Status = CreatePopulateInstallShellProtocol(&ShellInfoObject.NewEfiShellProtocol);
338 ASSERT_EFI_ERROR(Status);
339 ASSERT(ShellInfoObject.NewEfiShellProtocol != NULL);
340
341 //
342 // Now initialize the shell library (it requires Shell Parameters protocol)
343 //
344 Status = ShellInitialize();
345 ASSERT_EFI_ERROR(Status);
346
347 Status = CommandInit();
348 ASSERT_EFI_ERROR(Status);
349
350 //
351 // Check the command line
352 //
353 Status = ProcessCommandLine();
354
355 //
356 // If shell support level is >= 1 create the mappings and paths
357 //
358 if (PcdGet8(PcdShellSupportLevel) >= 1) {
359 Status = ShellCommandCreateInitialMappingsAndPaths();
360 }
361
362 //
363 // save the device path for the loaded image and the device path for the filepath (under loaded image)
364 // These are where to look for the startup.nsh file
365 //
366 Status = GetDevicePathsForImageAndFile(&ShellInfoObject.ImageDevPath, &ShellInfoObject.FileDevPath);
367 ASSERT_EFI_ERROR(Status);
368
369 //
370 // Display the version
371 //
372 if (!ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoVersion) {
373 ShellPrintHiiEx (
374 0,
375 gST->ConOut->Mode->CursorRow,
376 NULL,
377 STRING_TOKEN (STR_VER_OUTPUT_MAIN_SHELL),
378 ShellInfoObject.HiiHandle,
379 SupportLevel[PcdGet8(PcdShellSupportLevel)],
380 gEfiShellProtocol->MajorVersion,
381 gEfiShellProtocol->MinorVersion
382 );
383
384 ShellPrintHiiEx (
385 -1,
386 -1,
387 NULL,
388 STRING_TOKEN (STR_VER_OUTPUT_MAIN_SUPPLIER),
389 ShellInfoObject.HiiHandle,
390 (CHAR16 *) PcdGetPtr (PcdShellSupplier)
391 );
392
393 ShellPrintHiiEx (
394 -1,
395 -1,
396 NULL,
397 STRING_TOKEN (STR_VER_OUTPUT_MAIN_UEFI),
398 ShellInfoObject.HiiHandle,
399 (gST->Hdr.Revision&0xffff0000)>>16,
400 (gST->Hdr.Revision&0x0000ffff),
401 gST->FirmwareVendor,
402 gST->FirmwareRevision
403 );
404 }
405
406 //
407 // Display the mapping
408 //
409 if (PcdGet8(PcdShellSupportLevel) >= 2 && !ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoMap) {
410 Status = RunCommand(L"map");
411 ASSERT_EFI_ERROR(Status);
412 }
413
414 //
415 // init all the built in alias'
416 //
417 Status = SetBuiltInAlias();
418 ASSERT_EFI_ERROR(Status);
419
420 //
421 // Initialize environment variables
422 //
423 if (ShellCommandGetProfileList() != NULL) {
424 Status = InternalEfiShellSetEnv(L"profiles", ShellCommandGetProfileList(), TRUE);
425 ASSERT_EFI_ERROR(Status);
426 }
427
428 Size = 100;
429 TempString = AllocateZeroPool(Size);
430
431 UnicodeSPrint(TempString, Size, L"%d", PcdGet8(PcdShellSupportLevel));
432 Status = InternalEfiShellSetEnv(L"uefishellsupport", TempString, TRUE);
433 ASSERT_EFI_ERROR(Status);
434
435 UnicodeSPrint(TempString, Size, L"%d.%d", ShellInfoObject.NewEfiShellProtocol->MajorVersion, ShellInfoObject.NewEfiShellProtocol->MinorVersion);
436 Status = InternalEfiShellSetEnv(L"uefishellversion", TempString, TRUE);
437 ASSERT_EFI_ERROR(Status);
438
439 UnicodeSPrint(TempString, Size, L"%d.%d", (gST->Hdr.Revision & 0xFFFF0000) >> 16, gST->Hdr.Revision & 0x0000FFFF);
440 Status = InternalEfiShellSetEnv(L"uefiversion", TempString, TRUE);
441 ASSERT_EFI_ERROR(Status);
442
443 FreePool(TempString);
444
445 if (!EFI_ERROR(Status)) {
446 if (!ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoInterrupt) {
447 //
448 // Set up the event for CTRL-C monitoring...
449 //
450 Status = InernalEfiShellStartMonitor();
451 }
452
453 if (!EFI_ERROR(Status) && !ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoConsoleIn) {
454 //
455 // Set up the event for CTRL-S monitoring...
456 //
457 Status = InternalEfiShellStartCtrlSMonitor();
458 }
459
460 if (!EFI_ERROR(Status) && ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoConsoleIn) {
461 //
462 // close off the gST->ConIn
463 //
464 OldConIn = gST->ConIn;
465 ConInHandle = gST->ConsoleInHandle;
466 gST->ConIn = CreateSimpleTextInOnFile((SHELL_FILE_HANDLE)&FileInterfaceNulFile, &gST->ConsoleInHandle);
467 } else {
468 OldConIn = NULL;
469 ConInHandle = NULL;
470 }
471
472 if (!EFI_ERROR(Status) && PcdGet8(PcdShellSupportLevel) >= 1) {
473 //
474 // process the startup script or launch the called app.
475 //
476 Status = DoStartupScript(ShellInfoObject.ImageDevPath, ShellInfoObject.FileDevPath);
477 }
478
479 if (!ShellInfoObject.ShellInitSettings.BitUnion.Bits.Exit && !ShellCommandGetExit() && (PcdGet8(PcdShellSupportLevel) >= 3 || PcdGetBool(PcdShellForceConsole)) && !EFI_ERROR(Status) && !ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoConsoleIn) {
480 //
481 // begin the UI waiting loop
482 //
483 do {
484 //
485 // clean out all the memory allocated for CONST <something> * return values
486 // between each shell prompt presentation
487 //
488 if (!IsListEmpty(&ShellInfoObject.BufferToFreeList.Link)){
489 FreeBufferList(&ShellInfoObject.BufferToFreeList);
490 }
491
492 //
493 // Reset page break back to default.
494 //
495 ShellInfoObject.PageBreakEnabled = PcdGetBool(PcdShellPageBreakDefault);
496 ShellInfoObject.ConsoleInfo->Enabled = TRUE;
497 ShellInfoObject.ConsoleInfo->RowCounter = 0;
498
499 //
500 // Reset the CTRL-C event (yes we ignore the return values)
501 //
502 Status = gBS->CheckEvent (ShellInfoObject.NewEfiShellProtocol->ExecutionBreak);
503
504 //
505 // Display Prompt
506 //
507 Status = DoShellPrompt();
508 } while (!ShellCommandGetExit());
509 }
510 if (OldConIn != NULL && ConInHandle != NULL) {
511 CloseSimpleTextInOnFile (gST->ConIn);
512 gST->ConIn = OldConIn;
513 gST->ConsoleInHandle = ConInHandle;
514 }
515 }
516 }
517
518 //
519 // uninstall protocols / free memory / etc...
520 //
521 if (ShellInfoObject.UserBreakTimer != NULL) {
522 gBS->CloseEvent(ShellInfoObject.UserBreakTimer);
523 DEBUG_CODE(ShellInfoObject.UserBreakTimer = NULL;);
524 }
525 if (ShellInfoObject.ImageDevPath != NULL) {
526 FreePool(ShellInfoObject.ImageDevPath);
527 DEBUG_CODE(ShellInfoObject.ImageDevPath = NULL;);
528 }
529 if (ShellInfoObject.FileDevPath != NULL) {
530 FreePool(ShellInfoObject.FileDevPath);
531 DEBUG_CODE(ShellInfoObject.FileDevPath = NULL;);
532 }
533 if (ShellInfoObject.NewShellParametersProtocol != NULL) {
534 CleanUpShellParametersProtocol(ShellInfoObject.NewShellParametersProtocol);
535 DEBUG_CODE(ShellInfoObject.NewShellParametersProtocol = NULL;);
536 }
537 if (ShellInfoObject.NewEfiShellProtocol != NULL){
538 if (ShellInfoObject.NewEfiShellProtocol->IsRootShell()){
539 InternalEfiShellSetEnv(L"cwd", NULL, TRUE);
540 }
541 CleanUpShellProtocol(ShellInfoObject.NewEfiShellProtocol);
542 DEBUG_CODE(ShellInfoObject.NewEfiShellProtocol = NULL;);
543 }
544
545 if (!IsListEmpty(&ShellInfoObject.BufferToFreeList.Link)){
546 FreeBufferList(&ShellInfoObject.BufferToFreeList);
547 }
548
549 if (!IsListEmpty(&ShellInfoObject.SplitList.Link)){
550 ASSERT(FALSE); ///@todo finish this de-allocation.
551 }
552
553 if (ShellInfoObject.ShellInitSettings.FileName != NULL) {
554 FreePool(ShellInfoObject.ShellInitSettings.FileName);
555 DEBUG_CODE(ShellInfoObject.ShellInitSettings.FileName = NULL;);
556 }
557
558 if (ShellInfoObject.ShellInitSettings.FileOptions != NULL) {
559 FreePool(ShellInfoObject.ShellInitSettings.FileOptions);
560 DEBUG_CODE(ShellInfoObject.ShellInitSettings.FileOptions = NULL;);
561 }
562
563 if (ShellInfoObject.HiiHandle != NULL) {
564 HiiRemovePackages(ShellInfoObject.HiiHandle);
565 DEBUG_CODE(ShellInfoObject.HiiHandle = NULL;);
566 }
567
568 if (!IsListEmpty(&ShellInfoObject.ViewingSettings.CommandHistory.Link)){
569 FreeBufferList(&ShellInfoObject.ViewingSettings.CommandHistory);
570 }
571
572 ASSERT(ShellInfoObject.ConsoleInfo != NULL);
573 if (ShellInfoObject.ConsoleInfo != NULL) {
574 ConsoleLoggerUninstall(ShellInfoObject.ConsoleInfo);
575 FreePool(ShellInfoObject.ConsoleInfo);
576 DEBUG_CODE(ShellInfoObject.ConsoleInfo = NULL;);
577 }
578
579 if (ShellCommandGetExit()) {
580 return ((EFI_STATUS)ShellCommandGetExitCode());
581 }
582 return (Status);
583 }
584
585 /**
586 Sets all the alias' that were registered with the ShellCommandLib library.
587
588 @retval EFI_SUCCESS all init commands were run sucessfully.
589 **/
590 EFI_STATUS
591 EFIAPI
592 SetBuiltInAlias(
593 )
594 {
595 EFI_STATUS Status;
596 CONST ALIAS_LIST *List;
597 ALIAS_LIST *Node;
598
599 //
600 // Get all the commands we want to run
601 //
602 List = ShellCommandGetInitAliasList();
603
604 //
605 // for each command in the List
606 //
607 for ( Node = (ALIAS_LIST*)GetFirstNode(&List->Link)
608 ; !IsNull (&List->Link, &Node->Link)
609 ; Node = (ALIAS_LIST *)GetNextNode(&List->Link, &Node->Link)
610 ){
611 //
612 // install the alias'
613 //
614 Status = InternalSetAlias(Node->CommandString, Node->Alias, TRUE);
615 ASSERT_EFI_ERROR(Status);
616 }
617 return (EFI_SUCCESS);
618 }
619
620 /**
621 Internal function to determine if 2 command names are really the same.
622
623 @param[in] Command1 The pointer to the first command name.
624 @param[in] Command2 The pointer to the second command name.
625
626 @retval TRUE The 2 command names are the same.
627 @retval FALSE The 2 command names are not the same.
628 **/
629 BOOLEAN
630 EFIAPI
631 IsCommand(
632 IN CONST CHAR16 *Command1,
633 IN CONST CHAR16 *Command2
634 )
635 {
636 if (StringNoCaseCompare(&Command1, &Command2) == 0) {
637 return (TRUE);
638 }
639 return (FALSE);
640 }
641
642 /**
643 Internal function to determine if a command is a script only command.
644
645 @param[in] CommandName The pointer to the command name.
646
647 @retval TRUE The command is a script only command.
648 @retval FALSE The command is not a script only command.
649 **/
650 BOOLEAN
651 EFIAPI
652 IsScriptOnlyCommand(
653 IN CONST CHAR16 *CommandName
654 )
655 {
656 if (IsCommand(CommandName, L"for")
657 ||IsCommand(CommandName, L"endfor")
658 ||IsCommand(CommandName, L"if")
659 ||IsCommand(CommandName, L"else")
660 ||IsCommand(CommandName, L"endif")
661 ||IsCommand(CommandName, L"goto")) {
662 return (TRUE);
663 }
664 return (FALSE);
665 }
666
667 /**
668 This function will populate the 2 device path protocol parameters based on the
669 global gImageHandle. The DevPath will point to the device path for the handle that has
670 loaded image protocol installed on it. The FilePath will point to the device path
671 for the file that was loaded.
672
673 @param[in, out] DevPath On a sucessful return the device path to the loaded image.
674 @param[in, out] FilePath On a sucessful return the device path to the file.
675
676 @retval EFI_SUCCESS The 2 device paths were sucessfully returned.
677 @retval other A error from gBS->HandleProtocol.
678
679 @sa HandleProtocol
680 **/
681 EFI_STATUS
682 EFIAPI
683 GetDevicePathsForImageAndFile (
684 IN OUT EFI_DEVICE_PATH_PROTOCOL **DevPath,
685 IN OUT EFI_DEVICE_PATH_PROTOCOL **FilePath
686 )
687 {
688 EFI_STATUS Status;
689 EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;
690 EFI_DEVICE_PATH_PROTOCOL *ImageDevicePath;
691
692 ASSERT(DevPath != NULL);
693 ASSERT(FilePath != NULL);
694
695 Status = gBS->OpenProtocol (
696 gImageHandle,
697 &gEfiLoadedImageProtocolGuid,
698 (VOID**)&LoadedImage,
699 gImageHandle,
700 NULL,
701 EFI_OPEN_PROTOCOL_GET_PROTOCOL
702 );
703 if (!EFI_ERROR (Status)) {
704 Status = gBS->OpenProtocol (
705 LoadedImage->DeviceHandle,
706 &gEfiDevicePathProtocolGuid,
707 (VOID**)&ImageDevicePath,
708 gImageHandle,
709 NULL,
710 EFI_OPEN_PROTOCOL_GET_PROTOCOL
711 );
712 if (!EFI_ERROR (Status)) {
713 *DevPath = DuplicateDevicePath (ImageDevicePath);
714 *FilePath = DuplicateDevicePath (LoadedImage->FilePath);
715 gBS->CloseProtocol(
716 LoadedImage->DeviceHandle,
717 &gEfiDevicePathProtocolGuid,
718 gImageHandle,
719 NULL);
720 }
721 gBS->CloseProtocol(
722 gImageHandle,
723 &gEfiLoadedImageProtocolGuid,
724 gImageHandle,
725 NULL);
726 }
727 return (Status);
728 }
729
730 STATIC CONST SHELL_PARAM_ITEM mShellParamList[] = {
731 {L"-nostartup", TypeFlag},
732 {L"-startup", TypeFlag},
733 {L"-noconsoleout", TypeFlag},
734 {L"-noconsolein", TypeFlag},
735 {L"-nointerrupt", TypeFlag},
736 {L"-nomap", TypeFlag},
737 {L"-noversion", TypeFlag},
738 {L"-startup", TypeFlag},
739 {L"-delay", TypeValue},
740 {L"-_exit", TypeFlag},
741 {NULL, TypeMax}
742 };
743
744 /**
745 Process all Uefi Shell 2.0 command line options.
746
747 see Uefi Shell 2.0 section 3.2 for full details.
748
749 the command line must resemble the following:
750
751 shell.efi [ShellOpt-options] [options] [file-name [file-name-options]]
752
753 ShellOpt-options Options which control the initialization behavior of the shell.
754 These options are read from the EFI global variable "ShellOpt"
755 and are processed before options or file-name.
756
757 options Options which control the initialization behavior of the shell.
758
759 file-name The name of a UEFI shell application or script to be executed
760 after initialization is complete. By default, if file-name is
761 specified, then -nostartup is implied. Scripts are not supported
762 by level 0.
763
764 file-name-options The command-line options that are passed to file-name when it
765 is invoked.
766
767 This will initialize the ShellInfoObject.ShellInitSettings global variable.
768
769 @retval EFI_SUCCESS The variable is initialized.
770 **/
771 EFI_STATUS
772 EFIAPI
773 ProcessCommandLine(
774 VOID
775 )
776 {
777 EFI_STATUS Status;
778 LIST_ENTRY *Package;
779 UINTN Size;
780 CONST CHAR16 *TempConst;
781 UINTN Count;
782 UINTN LoopVar;
783 CHAR16 *ProblemParam;
784 UINT64 Intermediate;
785
786 Package = NULL;
787 ProblemParam = NULL;
788
789 Status = ShellCommandLineParse (mShellParamList, &Package, NULL, FALSE);
790
791 Count = 1;
792 Size = 0;
793 TempConst = ShellCommandLineGetRawValue(Package, Count++);
794 if (TempConst != NULL && StrLen(TempConst)) {
795 ShellInfoObject.ShellInitSettings.FileName = AllocateZeroPool(StrSize(TempConst));
796 if (ShellInfoObject.ShellInitSettings.FileName == NULL) {
797 return (EFI_OUT_OF_RESOURCES);
798 }
799 StrCpy(ShellInfoObject.ShellInitSettings.FileName, TempConst);
800 ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoStartup = 1;
801 for (LoopVar = 0 ; LoopVar < gEfiShellParametersProtocol->Argc ; LoopVar++) {
802 if (StrCmp(gEfiShellParametersProtocol->Argv[LoopVar], ShellInfoObject.ShellInitSettings.FileName)==0) {
803 LoopVar++;
804 //
805 // We found the file... add the rest of the params...
806 //
807 for ( ; LoopVar < gEfiShellParametersProtocol->Argc ; LoopVar++) {
808 ASSERT((ShellInfoObject.ShellInitSettings.FileOptions == NULL && Size == 0) || (ShellInfoObject.ShellInitSettings.FileOptions != NULL));
809 StrnCatGrow(&ShellInfoObject.ShellInitSettings.FileOptions,
810 &Size,
811 L" ",
812 0);
813 if (ShellInfoObject.ShellInitSettings.FileOptions == NULL) {
814 SHELL_FREE_NON_NULL(ShellInfoObject.ShellInitSettings.FileName);
815 return (EFI_OUT_OF_RESOURCES);
816 }
817 StrnCatGrow(&ShellInfoObject.ShellInitSettings.FileOptions,
818 &Size,
819 gEfiShellParametersProtocol->Argv[LoopVar],
820 0);
821 if (ShellInfoObject.ShellInitSettings.FileOptions == NULL) {
822 SHELL_FREE_NON_NULL(ShellInfoObject.ShellInitSettings.FileName);
823 return (EFI_OUT_OF_RESOURCES);
824 }
825 }
826 }
827 }
828 } else {
829 ShellCommandLineFreeVarList(Package);
830 Package = NULL;
831 Status = ShellCommandLineParse (mShellParamList, &Package, &ProblemParam, FALSE);
832 if (EFI_ERROR(Status)) {
833 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), ShellInfoObject.HiiHandle, ProblemParam);
834 FreePool(ProblemParam);
835 ShellCommandLineFreeVarList(Package);
836 return (EFI_INVALID_PARAMETER);
837 }
838 }
839
840 ShellInfoObject.ShellInitSettings.BitUnion.Bits.Startup = ShellCommandLineGetFlag(Package, L"-startup");
841 ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoStartup = ShellCommandLineGetFlag(Package, L"-nostartup");
842 ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoConsoleOut = ShellCommandLineGetFlag(Package, L"-noconsoleout");
843 ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoConsoleIn = ShellCommandLineGetFlag(Package, L"-noconsolein");
844 ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoInterrupt = ShellCommandLineGetFlag(Package, L"-nointerrupt");
845 ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoMap = ShellCommandLineGetFlag(Package, L"-nomap");
846 ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoVersion = ShellCommandLineGetFlag(Package, L"-noversion");
847 ShellInfoObject.ShellInitSettings.BitUnion.Bits.Delay = ShellCommandLineGetFlag(Package, L"-delay");
848 ShellInfoObject.ShellInitSettings.BitUnion.Bits.Exit = ShellCommandLineGetFlag(Package, L"-_exit");
849
850 ShellInfoObject.ShellInitSettings.Delay = 5;
851
852 if (ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoInterrupt) {
853 ShellInfoObject.ShellInitSettings.Delay = 0;
854 } else if (ShellInfoObject.ShellInitSettings.BitUnion.Bits.Delay) {
855 TempConst = ShellCommandLineGetValue(Package, L"-delay");
856 if (TempConst != NULL && *TempConst == L':') {
857 TempConst++;
858 }
859 if (TempConst != NULL && !EFI_ERROR(ShellConvertStringToUint64(TempConst, &Intermediate, FALSE, FALSE))) {
860 ShellInfoObject.ShellInitSettings.Delay = (UINTN)Intermediate;
861 }
862 }
863 ShellCommandLineFreeVarList(Package);
864
865 return (Status);
866 }
867
868 /**
869 Handles all interaction with the default startup script.
870
871 this will check that the correct command line parameters were passed, handle the delay, and then start running the script.
872
873 @param ImagePath the path to the image for shell. first place to look for the startup script
874 @param FilePath the path to the file for shell. second place to look for the startup script.
875
876 @retval EFI_SUCCESS the variable is initialized.
877 **/
878 EFI_STATUS
879 EFIAPI
880 DoStartupScript(
881 EFI_DEVICE_PATH_PROTOCOL *ImagePath,
882 EFI_DEVICE_PATH_PROTOCOL *FilePath
883 )
884 {
885 EFI_STATUS Status;
886 UINTN Delay;
887 EFI_INPUT_KEY Key;
888 SHELL_FILE_HANDLE FileHandle;
889 EFI_DEVICE_PATH_PROTOCOL *NewPath;
890 EFI_DEVICE_PATH_PROTOCOL *NamePath;
891 CHAR16 *FileStringPath;
892 CHAR16 *TempSpot;
893 UINTN NewSize;
894 CONST CHAR16 *MapName;
895
896 Key.UnicodeChar = CHAR_NULL;
897 Key.ScanCode = 0;
898 FileHandle = NULL;
899
900 if (!ShellInfoObject.ShellInitSettings.BitUnion.Bits.Startup && ShellInfoObject.ShellInitSettings.FileName != NULL) {
901 //
902 // launch something else instead
903 //
904 NewSize = StrSize(ShellInfoObject.ShellInitSettings.FileName);
905 if (ShellInfoObject.ShellInitSettings.FileOptions != NULL) {
906 NewSize += StrSize(ShellInfoObject.ShellInitSettings.FileOptions) + sizeof(CHAR16);
907 }
908 FileStringPath = AllocateZeroPool(NewSize);
909 if (FileStringPath == NULL) {
910 return (EFI_OUT_OF_RESOURCES);
911 }
912 StrCpy(FileStringPath, ShellInfoObject.ShellInitSettings.FileName);
913 if (ShellInfoObject.ShellInitSettings.FileOptions != NULL) {
914 StrCat(FileStringPath, L" ");
915 StrCat(FileStringPath, ShellInfoObject.ShellInitSettings.FileOptions);
916 }
917 Status = RunCommand(FileStringPath);
918 FreePool(FileStringPath);
919 return (Status);
920
921 }
922
923 //
924 // for shell level 0 we do no scripts
925 // Without the Startup bit overriding we allow for nostartup to prevent scripts
926 //
927 if ( (PcdGet8(PcdShellSupportLevel) < 1)
928 || (ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoStartup && !ShellInfoObject.ShellInitSettings.BitUnion.Bits.Startup)
929 ){
930 return (EFI_SUCCESS);
931 }
932
933 gST->ConOut->EnableCursor(gST->ConOut, FALSE);
934 //
935 // print out our warning and see if they press a key
936 //
937 for ( Status = EFI_UNSUPPORTED, Delay = ShellInfoObject.ShellInitSettings.Delay
938 ; Delay != 0 && EFI_ERROR(Status)
939 ; Delay--
940 ){
941 ShellPrintHiiEx(0, gST->ConOut->Mode->CursorRow, NULL, STRING_TOKEN (STR_SHELL_STARTUP_QUESTION), ShellInfoObject.HiiHandle, Delay);
942 gBS->Stall (1000000);
943 if (!ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoConsoleIn) {
944 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
945 }
946 }
947 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_CRLF), ShellInfoObject.HiiHandle);
948 gST->ConOut->EnableCursor(gST->ConOut, TRUE);
949
950 //
951 // ESC was pressed
952 //
953 if (Status == EFI_SUCCESS && Key.UnicodeChar == 0 && Key.ScanCode == SCAN_ESC) {
954 return (EFI_SUCCESS);
955 }
956
957 //
958 // Try the first location (must be file system)
959 //
960 MapName = ShellInfoObject.NewEfiShellProtocol->GetMapFromDevicePath(&ImagePath);
961 if (MapName != NULL) {
962 FileStringPath = NULL;
963 NewSize = 0;
964 FileStringPath = StrnCatGrow(&FileStringPath, &NewSize, MapName, 0);
965 if (FileStringPath == NULL) {
966 Status = EFI_OUT_OF_RESOURCES;
967 } else {
968 TempSpot = StrStr(FileStringPath, L";");
969 if (TempSpot != NULL) {
970 *TempSpot = CHAR_NULL;
971 }
972 FileStringPath = StrnCatGrow(&FileStringPath, &NewSize, ((FILEPATH_DEVICE_PATH*)FilePath)->PathName, 0);
973 PathRemoveLastItem(FileStringPath);
974 FileStringPath = StrnCatGrow(&FileStringPath, &NewSize, mStartupScript, 0);
975 Status = ShellInfoObject.NewEfiShellProtocol->OpenFileByName(FileStringPath, &FileHandle, EFI_FILE_MODE_READ);
976 FreePool(FileStringPath);
977 }
978 }
979 if (EFI_ERROR(Status)) {
980 NamePath = FileDevicePath (NULL, mStartupScript);
981 NewPath = AppendDevicePathNode (ImagePath, NamePath);
982 FreePool(NamePath);
983
984 //
985 // Try the location
986 //
987 Status = InternalOpenFileDevicePath(NewPath, &FileHandle, EFI_FILE_MODE_READ, 0);
988 FreePool(NewPath);
989 }
990 //
991 // If we got a file, run it
992 //
993 if (!EFI_ERROR(Status) && FileHandle != NULL) {
994 Status = RunScriptFileHandle (FileHandle, mStartupScript);
995 ShellInfoObject.NewEfiShellProtocol->CloseFile(FileHandle);
996 } else {
997 FileStringPath = ShellFindFilePath(mStartupScript);
998 if (FileStringPath == NULL) {
999 //
1000 // we return success since we dont need to have a startup script
1001 //
1002 Status = EFI_SUCCESS;
1003 ASSERT(FileHandle == NULL);
1004 } else {
1005 Status = RunScriptFile(FileStringPath);
1006 FreePool(FileStringPath);
1007 }
1008 }
1009
1010
1011 return (Status);
1012 }
1013
1014 /**
1015 Function to perform the shell prompt looping. It will do a single prompt,
1016 dispatch the result, and then return. It is expected that the caller will
1017 call this function in a loop many times.
1018
1019 @retval EFI_SUCCESS
1020 @retval RETURN_ABORTED
1021 **/
1022 EFI_STATUS
1023 EFIAPI
1024 DoShellPrompt (
1025 VOID
1026 )
1027 {
1028 UINTN Column;
1029 UINTN Row;
1030 CHAR16 *CmdLine;
1031 CONST CHAR16 *CurDir;
1032 UINTN BufferSize;
1033 EFI_STATUS Status;
1034
1035 CurDir = NULL;
1036
1037 //
1038 // Get screen setting to decide size of the command line buffer
1039 //
1040 gST->ConOut->QueryMode (gST->ConOut, gST->ConOut->Mode->Mode, &Column, &Row);
1041 BufferSize = Column * Row * sizeof (CHAR16);
1042 CmdLine = AllocateZeroPool (BufferSize);
1043 if (CmdLine == NULL) {
1044 return EFI_OUT_OF_RESOURCES;
1045 }
1046
1047 CurDir = ShellInfoObject.NewEfiShellProtocol->GetEnv(L"cwd");
1048
1049 //
1050 // Prompt for input
1051 //
1052 gST->ConOut->SetCursorPosition (gST->ConOut, 0, gST->ConOut->Mode->CursorRow);
1053
1054 if (CurDir != NULL && StrLen(CurDir) > 1) {
1055 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_CURDIR), ShellInfoObject.HiiHandle, CurDir);
1056 } else {
1057 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_SHELL), ShellInfoObject.HiiHandle);
1058 }
1059
1060 //
1061 // Read a line from the console
1062 //
1063 Status = ShellInfoObject.NewEfiShellProtocol->ReadFile(ShellInfoObject.NewShellParametersProtocol->StdIn, &BufferSize, CmdLine);
1064
1065 //
1066 // Null terminate the string and parse it
1067 //
1068 if (!EFI_ERROR (Status)) {
1069 CmdLine[BufferSize / sizeof (CHAR16)] = CHAR_NULL;
1070 Status = RunCommand(CmdLine);
1071 }
1072
1073 //
1074 // Done with this command
1075 //
1076 FreePool (CmdLine);
1077 return Status;
1078 }
1079
1080 /**
1081 Add a buffer to the Buffer To Free List for safely returning buffers to other
1082 places without risking letting them modify internal shell information.
1083
1084 @param Buffer Something to pass to FreePool when the shell is exiting.
1085 **/
1086 VOID*
1087 EFIAPI
1088 AddBufferToFreeList(
1089 VOID *Buffer
1090 )
1091 {
1092 BUFFER_LIST *BufferListEntry;
1093
1094 if (Buffer == NULL) {
1095 return (NULL);
1096 }
1097
1098 BufferListEntry = AllocateZeroPool(sizeof(BUFFER_LIST));
1099 ASSERT(BufferListEntry != NULL);
1100 BufferListEntry->Buffer = Buffer;
1101 InsertTailList(&ShellInfoObject.BufferToFreeList.Link, &BufferListEntry->Link);
1102 return (Buffer);
1103 }
1104
1105 /**
1106 Add a buffer to the Line History List
1107
1108 @param Buffer The line buffer to add.
1109 **/
1110 VOID
1111 EFIAPI
1112 AddLineToCommandHistory(
1113 IN CONST CHAR16 *Buffer
1114 )
1115 {
1116 BUFFER_LIST *Node;
1117
1118 Node = AllocateZeroPool(sizeof(BUFFER_LIST));
1119 ASSERT(Node != NULL);
1120 Node->Buffer = AllocateZeroPool(StrSize(Buffer));
1121 ASSERT(Node->Buffer != NULL);
1122 StrCpy(Node->Buffer, Buffer);
1123
1124 InsertTailList(&ShellInfoObject.ViewingSettings.CommandHistory.Link, &Node->Link);
1125 }
1126
1127 /**
1128 Checks if a string is an alias for another command. If yes, then it replaces the alias name
1129 with the correct command name.
1130
1131 @param[in, out] CommandString Upon entry the potential alias. Upon return the
1132 command name if it was an alias. If it was not
1133 an alias it will be unchanged. This function may
1134 change the buffer to fit the command name.
1135
1136 @retval EFI_SUCCESS The name was changed.
1137 @retval EFI_SUCCESS The name was not an alias.
1138 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
1139 **/
1140 EFI_STATUS
1141 EFIAPI
1142 ShellConvertAlias(
1143 IN OUT CHAR16 **CommandString
1144 )
1145 {
1146 CONST CHAR16 *NewString;
1147
1148 NewString = ShellInfoObject.NewEfiShellProtocol->GetAlias(*CommandString, NULL);
1149 if (NewString == NULL) {
1150 return (EFI_SUCCESS);
1151 }
1152 FreePool(*CommandString);
1153 *CommandString = AllocateZeroPool(StrSize(NewString));
1154 if (*CommandString == NULL) {
1155 return (EFI_OUT_OF_RESOURCES);
1156 }
1157 StrCpy(*CommandString, NewString);
1158 return (EFI_SUCCESS);
1159 }
1160
1161 /**
1162 Function allocates a new command line and replaces all instances of environment
1163 variable names that are correctly preset to their values.
1164
1165 If the return value is not NULL the memory must be caller freed.
1166
1167 @param[in] OriginalCommandLine The original command line
1168
1169 @retval NULL An error ocurred.
1170 @return The new command line with no environment variables present.
1171 **/
1172 CHAR16*
1173 EFIAPI
1174 ShellConvertVariables (
1175 IN CONST CHAR16 *OriginalCommandLine
1176 )
1177 {
1178 CONST CHAR16 *MasterEnvList;
1179 UINTN NewSize;
1180 CHAR16 *NewCommandLine1;
1181 CHAR16 *NewCommandLine2;
1182 CHAR16 *Temp;
1183 CHAR16 *Temp2;
1184 UINTN ItemSize;
1185 CHAR16 *ItemTemp;
1186 SCRIPT_FILE *CurrentScriptFile;
1187 ALIAS_LIST *AliasListNode;
1188
1189 ASSERT(OriginalCommandLine != NULL);
1190
1191 ItemSize = 0;
1192 NewSize = StrSize(OriginalCommandLine);
1193 CurrentScriptFile = ShellCommandGetCurrentScriptFile();
1194 Temp = NULL;
1195
1196 ///@todo update this to handle the %0 - %9 for scripting only (borrow from line 1256 area) ? ? ?
1197
1198 //
1199 // calculate the size required for the post-conversion string...
1200 //
1201 if (CurrentScriptFile != NULL) {
1202 for (AliasListNode = (ALIAS_LIST*)GetFirstNode(&CurrentScriptFile->SubstList)
1203 ; !IsNull(&CurrentScriptFile->SubstList, &AliasListNode->Link)
1204 ; AliasListNode = (ALIAS_LIST*)GetNextNode(&CurrentScriptFile->SubstList, &AliasListNode->Link)
1205 ){
1206 for (Temp = StrStr(OriginalCommandLine, AliasListNode->Alias)
1207 ; Temp != NULL
1208 ; Temp = StrStr(Temp+1, AliasListNode->Alias)
1209 ){
1210 //
1211 // we need a preceeding and if there is space no ^ preceeding (if no space ignore)
1212 //
1213 if ((((Temp-OriginalCommandLine)>2) && *(Temp-2) != L'^') || ((Temp-OriginalCommandLine)<=2)) {
1214 NewSize += StrSize(AliasListNode->CommandString);
1215 }
1216 }
1217 }
1218 }
1219
1220 for (MasterEnvList = EfiShellGetEnv(NULL)
1221 ; MasterEnvList != NULL && *MasterEnvList != CHAR_NULL //&& *(MasterEnvList+1) != CHAR_NULL
1222 ; MasterEnvList += StrLen(MasterEnvList) + 1
1223 ){
1224 if (StrSize(MasterEnvList) > ItemSize) {
1225 ItemSize = StrSize(MasterEnvList);
1226 }
1227 for (Temp = StrStr(OriginalCommandLine, MasterEnvList)
1228 ; Temp != NULL
1229 ; Temp = StrStr(Temp+1, MasterEnvList)
1230 ){
1231 //
1232 // we need a preceeding and following % and if there is space no ^ preceeding (if no space ignore)
1233 //
1234 if (*(Temp-1) == L'%' && *(Temp+StrLen(MasterEnvList)) == L'%' &&
1235 ((((Temp-OriginalCommandLine)>2) && *(Temp-2) != L'^') || ((Temp-OriginalCommandLine)<=2))) {
1236 NewSize+=StrSize(EfiShellGetEnv(MasterEnvList));
1237 }
1238 }
1239 }
1240
1241 //
1242 // now do the replacements...
1243 //
1244 NewCommandLine1 = AllocateZeroPool(NewSize);
1245 NewCommandLine2 = AllocateZeroPool(NewSize);
1246 ItemTemp = AllocateZeroPool(ItemSize+(2*sizeof(CHAR16)));
1247 if (NewCommandLine1 == NULL || NewCommandLine2 == NULL || ItemTemp == NULL) {
1248 SHELL_FREE_NON_NULL(NewCommandLine1);
1249 SHELL_FREE_NON_NULL(NewCommandLine2);
1250 SHELL_FREE_NON_NULL(ItemTemp);
1251 return (NULL);
1252 }
1253 StrCpy(NewCommandLine1, OriginalCommandLine);
1254 for (MasterEnvList = EfiShellGetEnv(NULL)
1255 ; MasterEnvList != NULL && *MasterEnvList != CHAR_NULL //&& *(MasterEnvList+1) != CHAR_NULL
1256 ; MasterEnvList += StrLen(MasterEnvList) + 1
1257 ){
1258 StrCpy(ItemTemp, L"%");
1259 StrCat(ItemTemp, MasterEnvList);
1260 StrCat(ItemTemp, L"%");
1261 ShellCopySearchAndReplace(NewCommandLine1, NewCommandLine2, NewSize, ItemTemp, EfiShellGetEnv(MasterEnvList), TRUE, FALSE);
1262 StrCpy(NewCommandLine1, NewCommandLine2);
1263 }
1264 if (CurrentScriptFile != NULL) {
1265 for (AliasListNode = (ALIAS_LIST*)GetFirstNode(&CurrentScriptFile->SubstList)
1266 ; !IsNull(&CurrentScriptFile->SubstList, &AliasListNode->Link)
1267 ; AliasListNode = (ALIAS_LIST*)GetNextNode(&CurrentScriptFile->SubstList, &AliasListNode->Link)
1268 ){
1269 ShellCopySearchAndReplace(NewCommandLine1, NewCommandLine2, NewSize, AliasListNode->Alias, AliasListNode->CommandString, TRUE, FALSE);
1270 StrCpy(NewCommandLine1, NewCommandLine2);
1271 }
1272
1273 //
1274 // Remove non-existant environment variables in scripts only
1275 //
1276 for (Temp = NewCommandLine1 ; Temp != NULL ; ) {
1277 Temp = StrStr(Temp, L"%");
1278 if (Temp == NULL) {
1279 break;
1280 }
1281 while (*(Temp - 1) == L'^') {
1282 Temp = StrStr(Temp + 1, L"%");
1283 if (Temp == NULL) {
1284 break;
1285 }
1286 }
1287 if (Temp == NULL) {
1288 break;
1289 }
1290
1291 Temp2 = StrStr(Temp + 1, L"%");
1292 if (Temp2 == NULL) {
1293 break;
1294 }
1295 while (*(Temp2 - 1) == L'^') {
1296 Temp2 = StrStr(Temp2 + 1, L"%");
1297 if (Temp2 == NULL) {
1298 break;
1299 }
1300 }
1301 if (Temp2 == NULL) {
1302 break;
1303 }
1304
1305 Temp2++;
1306 CopyMem(Temp, Temp2, StrSize(Temp2));
1307 }
1308
1309 }
1310
1311 //
1312 // Now cleanup any straggler intentionally ignored "%" characters
1313 //
1314 ShellCopySearchAndReplace(NewCommandLine1, NewCommandLine2, NewSize, L"^%", L"%", TRUE, FALSE);
1315 StrCpy(NewCommandLine1, NewCommandLine2);
1316
1317 FreePool(NewCommandLine2);
1318 FreePool(ItemTemp);
1319
1320 return (NewCommandLine1);
1321 }
1322
1323 /**
1324 Internal function to run a command line with pipe usage.
1325
1326 @param[in] CmdLine The pointer to the command line.
1327 @param[in] StdIn The pointer to the Standard input.
1328 @param[in] StdOut The pointer to the Standard output.
1329
1330 @retval EFI_SUCCESS The split command is executed successfully.
1331 @retval other Some error occurs when executing the split command.
1332 **/
1333 EFI_STATUS
1334 EFIAPI
1335 RunSplitCommand(
1336 IN CONST CHAR16 *CmdLine,
1337 IN SHELL_FILE_HANDLE *StdIn,
1338 IN SHELL_FILE_HANDLE *StdOut
1339 )
1340 {
1341 EFI_STATUS Status;
1342 CHAR16 *NextCommandLine;
1343 CHAR16 *OurCommandLine;
1344 UINTN Size1;
1345 UINTN Size2;
1346 SPLIT_LIST *Split;
1347 SHELL_FILE_HANDLE *TempFileHandle;
1348 BOOLEAN Unicode;
1349
1350 ASSERT(StdOut == NULL);
1351
1352 ASSERT(StrStr(CmdLine, L"|") != NULL);
1353
1354 Status = EFI_SUCCESS;
1355 NextCommandLine = NULL;
1356 OurCommandLine = NULL;
1357 Size1 = 0;
1358 Size2 = 0;
1359
1360 NextCommandLine = StrnCatGrow(&NextCommandLine, &Size1, StrStr(CmdLine, L"|")+1, 0);
1361 OurCommandLine = StrnCatGrow(&OurCommandLine , &Size2, CmdLine , StrStr(CmdLine, L"|") - CmdLine);
1362
1363 if (NextCommandLine == NULL || OurCommandLine == NULL) {
1364 SHELL_FREE_NON_NULL(OurCommandLine);
1365 SHELL_FREE_NON_NULL(NextCommandLine);
1366 return (EFI_OUT_OF_RESOURCES);
1367 } else if (StrStr(OurCommandLine, L"|") != NULL || Size1 == 0 || Size2 == 0) {
1368 SHELL_FREE_NON_NULL(OurCommandLine);
1369 SHELL_FREE_NON_NULL(NextCommandLine);
1370 return (EFI_INVALID_PARAMETER);
1371 } else if (NextCommandLine[0] != CHAR_NULL &&
1372 NextCommandLine[0] == L'a' &&
1373 NextCommandLine[1] == L' '
1374 ){
1375 CopyMem(NextCommandLine, NextCommandLine+1, StrSize(NextCommandLine) - sizeof(NextCommandLine[0]));
1376 Unicode = FALSE;
1377 } else {
1378 Unicode = TRUE;
1379 }
1380
1381
1382 //
1383 // make a SPLIT_LIST item and add to list
1384 //
1385 Split = AllocateZeroPool(sizeof(SPLIT_LIST));
1386 ASSERT(Split != NULL);
1387 Split->SplitStdIn = StdIn;
1388 Split->SplitStdOut = ConvertEfiFileProtocolToShellHandle(CreateFileInterfaceMem(Unicode), NULL);
1389 ASSERT(Split->SplitStdOut != NULL);
1390 InsertHeadList(&ShellInfoObject.SplitList.Link, &Split->Link);
1391
1392 Status = RunCommand(OurCommandLine);
1393
1394 //
1395 // move the output from the first to the in to the second.
1396 //
1397 TempFileHandle = Split->SplitStdOut;
1398 if (Split->SplitStdIn == StdIn) {
1399 Split->SplitStdOut = NULL;
1400 } else {
1401 Split->SplitStdOut = Split->SplitStdIn;
1402 }
1403 Split->SplitStdIn = TempFileHandle;
1404 ShellInfoObject.NewEfiShellProtocol->SetFilePosition(ConvertShellHandleToEfiFileProtocol(Split->SplitStdIn), 0);
1405
1406 if (!EFI_ERROR(Status)) {
1407 Status = RunCommand(NextCommandLine);
1408 }
1409
1410 //
1411 // remove the top level from the ScriptList
1412 //
1413 ASSERT((SPLIT_LIST*)GetFirstNode(&ShellInfoObject.SplitList.Link) == Split);
1414 RemoveEntryList(&Split->Link);
1415
1416 //
1417 // Note that the original StdIn is now the StdOut...
1418 //
1419 if (Split->SplitStdOut != NULL && Split->SplitStdOut != StdIn) {
1420 ShellInfoObject.NewEfiShellProtocol->CloseFile(ConvertShellHandleToEfiFileProtocol(Split->SplitStdOut));
1421 }
1422 if (Split->SplitStdIn != NULL) {
1423 ShellInfoObject.NewEfiShellProtocol->CloseFile(ConvertShellHandleToEfiFileProtocol(Split->SplitStdIn));
1424 }
1425
1426 FreePool(Split);
1427 FreePool(NextCommandLine);
1428 FreePool(OurCommandLine);
1429
1430 return (Status);
1431 }
1432
1433 /**
1434 Take the original command line, substitute any variables, free
1435 the original string, return the modified copy
1436
1437 @param[in] CmdLine pointer to the command line to update
1438 @param[out]CmdName upon successful return the name of the command to be run
1439
1440 @retval EFI_SUCCESS the function was successful
1441 @retval EFI_OUT_OF_RESOURCES a memory allocation failed
1442 **/
1443 EFI_STATUS
1444 EFIAPI
1445 ShellSubstituteVariables(
1446 IN CHAR16 **CmdLine
1447 )
1448 {
1449 CHAR16 *NewCmdLine;
1450 NewCmdLine = ShellConvertVariables(*CmdLine);
1451 SHELL_FREE_NON_NULL(*CmdLine);
1452 if (NewCmdLine == NULL) {
1453 return (EFI_OUT_OF_RESOURCES);
1454 }
1455 *CmdLine = NewCmdLine;
1456 return (EFI_SUCCESS);
1457 }
1458
1459 /**
1460 Take the original command line, substitute any alias in the first group of space delimited characters, free
1461 the original string, return the modified copy
1462
1463 @param[in] CmdLine pointer to the command line to update
1464 @param[out]CmdName upon successful return the name of the command to be run
1465
1466 @retval EFI_SUCCESS the function was successful
1467 @retval EFI_OUT_OF_RESOURCES a memory allocation failed
1468 **/
1469 EFI_STATUS
1470 EFIAPI
1471 ShellSubstituteAliases(
1472 IN CHAR16 **CmdLine
1473 )
1474 {
1475 CHAR16 *NewCmdLine;
1476 CHAR16 *CommandName;
1477 EFI_STATUS Status;
1478 UINTN PostAliasSize;
1479 ASSERT(CmdLine != NULL);
1480 ASSERT(*CmdLine!= NULL);
1481
1482
1483 CommandName = NULL;
1484 if (StrStr((*CmdLine), L" ") == NULL){
1485 StrnCatGrow(&CommandName, NULL, (*CmdLine), 0);
1486 } else {
1487 StrnCatGrow(&CommandName, NULL, (*CmdLine), StrStr((*CmdLine), L" ") - (*CmdLine));
1488 }
1489
1490 //
1491 // This cannot happen 'inline' since the CmdLine can need extra space.
1492 //
1493 NewCmdLine = NULL;
1494 if (!ShellCommandIsCommandOnList(CommandName)) {
1495 //
1496 // Convert via alias
1497 //
1498 Status = ShellConvertAlias(&CommandName);
1499 if (EFI_ERROR(Status)){
1500 return (Status);
1501 }
1502 PostAliasSize = 0;
1503 NewCmdLine = StrnCatGrow(&NewCmdLine, &PostAliasSize, CommandName, 0);
1504 if (NewCmdLine == NULL) {
1505 SHELL_FREE_NON_NULL(CommandName);
1506 SHELL_FREE_NON_NULL(*CmdLine);
1507 return (EFI_OUT_OF_RESOURCES);
1508 }
1509 NewCmdLine = StrnCatGrow(&NewCmdLine, &PostAliasSize, StrStr((*CmdLine), L" "), 0);
1510 if (NewCmdLine == NULL) {
1511 SHELL_FREE_NON_NULL(CommandName);
1512 SHELL_FREE_NON_NULL(*CmdLine);
1513 return (EFI_OUT_OF_RESOURCES);
1514 }
1515 } else {
1516 NewCmdLine = StrnCatGrow(&NewCmdLine, NULL, (*CmdLine), 0);
1517 }
1518
1519 SHELL_FREE_NON_NULL(*CmdLine);
1520 SHELL_FREE_NON_NULL(CommandName);
1521
1522 //
1523 // re-assign the passed in double pointer to point to our newly allocated buffer
1524 //
1525 *CmdLine = NewCmdLine;
1526
1527 return (EFI_SUCCESS);
1528 }
1529
1530 /**
1531 Takes the Argv[0] part of the command line and determine the meaning of it.
1532 **/
1533 SHELL_OPERATION_TYPES
1534 EFIAPI
1535 GetOperationType(
1536 IN CONST CHAR16 *CmdName
1537 )
1538 {
1539 CHAR16* FileWithPath;
1540 CONST CHAR16* TempLocation;
1541 CONST CHAR16* TempLocation2;
1542
1543 FileWithPath = NULL;
1544 //
1545 // test for an internal command.
1546 //
1547 if (ShellCommandIsCommandOnList(CmdName)) {
1548 return (INTERNAL_COMMAND);
1549 }
1550
1551 //
1552 // Test for file system change request. anything ending with : and cant have spaces.
1553 //
1554 if (CmdName[(StrLen(CmdName)-1)] == L':') {
1555 if (StrStr(CmdName, L" ") != NULL) {
1556 return (UNKNOWN_INVALID);
1557 }
1558 return (FILE_SYS_CHANGE);
1559 }
1560
1561 //
1562 // Test for a file
1563 //
1564 if ((FileWithPath = ShellFindFilePathEx(CmdName, mExecutableExtensions)) != NULL) {
1565 //
1566 // See if that file has a script file extension
1567 //
1568 if (StrLen(FileWithPath) > 4) {
1569 TempLocation = FileWithPath+StrLen(FileWithPath)-4;
1570 TempLocation2 = mScriptExtension;
1571 if (StringNoCaseCompare((VOID*)(&TempLocation), (VOID*)(&TempLocation2)) == 0) {
1572 SHELL_FREE_NON_NULL(FileWithPath);
1573 return (SCRIPT_FILE_NAME);
1574 }
1575 }
1576
1577 //
1578 // Was a file, but not a script. we treat this as an application.
1579 //
1580 SHELL_FREE_NON_NULL(FileWithPath);
1581 return (EFI_APPLICATION);
1582 }
1583
1584 SHELL_FREE_NON_NULL(FileWithPath);
1585 //
1586 // No clue what this is... return invalid flag...
1587 //
1588 return (UNKNOWN_INVALID);
1589 }
1590
1591 /**
1592 **/
1593 EFI_STATUS
1594 EFIAPI
1595 ProcessNewSplitCommandLine(
1596 IN CONST CHAR16 *CmdLine
1597 )
1598 {
1599 SPLIT_LIST *Split;
1600 EFI_STATUS Status;
1601
1602 Split = NULL;
1603
1604 //
1605 // are we in an existing split???
1606 //
1607 if (!IsListEmpty(&ShellInfoObject.SplitList.Link)) {
1608 Split = (SPLIT_LIST*)GetFirstNode(&ShellInfoObject.SplitList.Link);
1609 }
1610
1611 if (Split == NULL) {
1612 Status = RunSplitCommand(CmdLine, NULL, NULL);
1613 } else {
1614 Status = RunSplitCommand(CmdLine, Split->SplitStdIn, Split->SplitStdOut);
1615 }
1616 if (EFI_ERROR(Status)) {
1617 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_INVALID_SPLIT), ShellInfoObject.HiiHandle, CmdLine);
1618 }
1619 return (Status);
1620 }
1621
1622 /**
1623 Handle a request to change the current file system
1624
1625 @param[in] CmdLine The passed in command line
1626
1627 @retval EFI_SUCCESS The operation was successful
1628 **/
1629 EFI_STATUS
1630 EFIAPI
1631 ChangeMappedDrive(
1632 IN CONST CHAR16 *CmdLine
1633 )
1634 {
1635 EFI_STATUS Status;
1636 Status = EFI_SUCCESS;
1637
1638 //
1639 // make sure we are the right operation
1640 //
1641 ASSERT(CmdLine[(StrLen(CmdLine)-1)] == L':' && StrStr(CmdLine, L" ") == NULL);
1642
1643 //
1644 // Call the protocol API to do the work
1645 //
1646 Status = ShellInfoObject.NewEfiShellProtocol->SetCurDir(NULL, CmdLine);
1647
1648 //
1649 // Report any errors
1650 //
1651 if (EFI_ERROR(Status)) {
1652 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_INVALID_MAPPING), ShellInfoObject.HiiHandle, CmdLine);
1653 }
1654
1655 return (Status);
1656 }
1657
1658 /**
1659 Function will process and run a command line.
1660
1661 This will determine if the command line represents an internal shell
1662 command or dispatch an external application.
1663
1664 @param[in] CmdLine The command line to parse.
1665
1666 @retval EFI_SUCCESS The command was completed.
1667 @retval EFI_ABORTED The command's operation was aborted.
1668 **/
1669 EFI_STATUS
1670 EFIAPI
1671 RunCommand(
1672 IN CONST CHAR16 *CmdLine
1673 )
1674 {
1675 EFI_STATUS Status;
1676 EFI_STATUS StatusCode;
1677 CHAR16 *CommandName;
1678 SHELL_STATUS ShellStatus;
1679 UINTN Argc;
1680 CHAR16 **Argv;
1681 BOOLEAN LastError;
1682 CHAR16 LeString[19];
1683 CHAR16 *CommandWithPath;
1684 CONST EFI_DEVICE_PATH_PROTOCOL *DevPath;
1685 CONST CHAR16 *TempLocation;
1686 CONST CHAR16 *TempLocation2;
1687 SHELL_FILE_HANDLE OriginalStdIn;
1688 SHELL_FILE_HANDLE OriginalStdOut;
1689 SHELL_FILE_HANDLE OriginalStdErr;
1690 SYSTEM_TABLE_INFO OriginalSystemTableInfo;
1691 UINTN Count;
1692 UINTN Count2;
1693 CHAR16 *CleanOriginal;
1694
1695 ASSERT(CmdLine != NULL);
1696 if (StrLen(CmdLine) == 0) {
1697 return (EFI_SUCCESS);
1698 }
1699
1700 CommandName = NULL;
1701 CommandWithPath = NULL;
1702 DevPath = NULL;
1703 Status = EFI_SUCCESS;
1704 CleanOriginal = NULL;
1705
1706 CleanOriginal = StrnCatGrow(&CleanOriginal, NULL, CmdLine, 0);
1707 if (CleanOriginal == NULL) {
1708 return (EFI_OUT_OF_RESOURCES);
1709 }
1710
1711 TrimSpaces(&CleanOriginal);
1712
1713 //
1714 // Handle case that passed in command line is just 1 or more " " characters.
1715 //
1716 if (StrLen (CleanOriginal) == 0) {
1717 if (CleanOriginal != NULL) {
1718 FreePool(CleanOriginal);
1719 CleanOriginal = NULL;
1720 }
1721 return (EFI_SUCCESS);
1722 }
1723
1724 Status = ShellSubstituteAliases(&CleanOriginal);
1725 if (EFI_ERROR(Status)) {
1726 return (Status);
1727 }
1728
1729 Status = ShellSubstituteVariables(&CleanOriginal);
1730 if (EFI_ERROR(Status)) {
1731 return (Status);
1732 }
1733
1734 TrimSpaces(&CleanOriginal);
1735
1736 //
1737 // We dont do normal processing with a split command line (output from one command input to another)
1738 //
1739 if (ContainsSplit(CleanOriginal)) {
1740 Status = ProcessNewSplitCommandLine(CleanOriginal);
1741 } else {
1742 //
1743 // If this is a mapped drive change handle that...
1744 //
1745 if (CleanOriginal[(StrLen(CleanOriginal)-1)] == L':' && StrStr(CleanOriginal, L" ") == NULL) {
1746 Status = ChangeMappedDrive(CleanOriginal);
1747 SHELL_FREE_NON_NULL(CleanOriginal);
1748 return (Status);
1749 }
1750
1751
1752 ///@todo update this section to divide into 3 ways - run internal command, run split (above), and run an external file...
1753 /// We waste a lot of time doing processing like StdIn,StdOut,Argv,Argc for things that are external files...
1754
1755
1756
1757 Status = UpdateStdInStdOutStdErr(ShellInfoObject.NewShellParametersProtocol, CleanOriginal, &OriginalStdIn, &OriginalStdOut, &OriginalStdErr, &OriginalSystemTableInfo);
1758 if (EFI_ERROR(Status)) {
1759 if (Status == EFI_NOT_FOUND) {
1760 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_REDUNDA_REDIR), ShellInfoObject.HiiHandle);
1761 } else {
1762 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_INVALID_REDIR), ShellInfoObject.HiiHandle);
1763 }
1764 } else {
1765 TrimSpaces(&CleanOriginal);
1766
1767 //
1768 // get the argc and argv updated for internal commands
1769 //
1770 Status = UpdateArgcArgv(ShellInfoObject.NewShellParametersProtocol, CleanOriginal, &Argv, &Argc);
1771 ASSERT_EFI_ERROR(Status);
1772
1773 for (Count = 0 ; Count < ShellInfoObject.NewShellParametersProtocol->Argc ; Count++) {
1774 if (StrStr(ShellInfoObject.NewShellParametersProtocol->Argv[Count], L"-?") == ShellInfoObject.NewShellParametersProtocol->Argv[Count]
1775 || (ShellInfoObject.NewShellParametersProtocol->Argv[0][0] == L'?' && ShellInfoObject.NewShellParametersProtocol->Argv[0][1] == CHAR_NULL)
1776 ) {
1777 //
1778 // We need to redo the arguments since a parameter was -?
1779 // move them all down 1 to the end, then up one then replace the first with help
1780 //
1781 FreePool(ShellInfoObject.NewShellParametersProtocol->Argv[Count]);
1782 ShellInfoObject.NewShellParametersProtocol->Argv[Count] = NULL;
1783 for (Count2 = Count ; (Count2 + 1) < ShellInfoObject.NewShellParametersProtocol->Argc ; Count2++) {
1784 ShellInfoObject.NewShellParametersProtocol->Argv[Count2] = ShellInfoObject.NewShellParametersProtocol->Argv[Count2+1];
1785 }
1786 ShellInfoObject.NewShellParametersProtocol->Argv[Count2] = NULL;
1787 for (Count2 = ShellInfoObject.NewShellParametersProtocol->Argc -1 ; Count2 > 0 ; Count2--) {
1788 ShellInfoObject.NewShellParametersProtocol->Argv[Count2] = ShellInfoObject.NewShellParametersProtocol->Argv[Count2-1];
1789 }
1790 ShellInfoObject.NewShellParametersProtocol->Argv[0] = NULL;
1791 ShellInfoObject.NewShellParametersProtocol->Argv[0] = StrnCatGrow(&ShellInfoObject.NewShellParametersProtocol->Argv[0], NULL, L"help", 0);
1792 break;
1793 }
1794 }
1795
1796 //
1797 // command or file?
1798 //
1799 if (ShellCommandIsCommandOnList(ShellInfoObject.NewShellParametersProtocol->Argv[0])) {
1800 //
1801 // Run the command (which was converted if it was an alias)
1802 //
1803 if (!EFI_ERROR(Status)) {
1804 Status = ShellCommandRunCommandHandler(ShellInfoObject.NewShellParametersProtocol->Argv[0], &ShellStatus, &LastError);
1805 ASSERT_EFI_ERROR(Status);
1806
1807 if (sizeof(EFI_STATUS) == sizeof(UINT64)) {
1808 UnicodeSPrint(LeString, sizeof(LeString), L"0x%Lx", ShellStatus);
1809 } else {
1810 UnicodeSPrint(LeString, sizeof(LeString), L"0x%x", ShellStatus);
1811 }
1812 DEBUG_CODE(InternalEfiShellSetEnv(L"debuglasterror", LeString, TRUE););
1813 if (LastError) {
1814 InternalEfiShellSetEnv(L"lasterror", LeString, TRUE);
1815 }
1816 //
1817 // Pass thru the exitcode from the app.
1818 //
1819 if (ShellCommandGetExit()) {
1820 Status = ShellStatus;
1821 } else if (ShellStatus != 0 && IsScriptOnlyCommand(ShellInfoObject.NewShellParametersProtocol->Argv[0])) {
1822 Status = EFI_ABORTED;
1823 }
1824 }
1825 } else {
1826 //
1827 // run an external file (or script)
1828 //
1829 if (StrStr(ShellInfoObject.NewShellParametersProtocol->Argv[0], L":") != NULL) {
1830 ASSERT (CommandWithPath == NULL);
1831 if (ShellIsFile(ShellInfoObject.NewShellParametersProtocol->Argv[0]) == EFI_SUCCESS) {
1832 CommandWithPath = StrnCatGrow(&CommandWithPath, NULL, ShellInfoObject.NewShellParametersProtocol->Argv[0], 0);
1833 }
1834 }
1835 if (CommandWithPath == NULL) {
1836 CommandWithPath = ShellFindFilePathEx(ShellInfoObject.NewShellParametersProtocol->Argv[0], mExecutableExtensions);
1837 }
1838 if (CommandWithPath == NULL || ShellIsDirectory(CommandWithPath) == EFI_SUCCESS) {
1839 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_NOT_FOUND), ShellInfoObject.HiiHandle, ShellInfoObject.NewShellParametersProtocol->Argv[0]);
1840
1841 if (sizeof(EFI_STATUS) == sizeof(UINT64)) {
1842 UnicodeSPrint(LeString, sizeof(LeString), L"0x%Lx", EFI_NOT_FOUND);
1843 } else {
1844 UnicodeSPrint(LeString, sizeof(LeString), L"0x%x", EFI_NOT_FOUND);
1845 }
1846 DEBUG_CODE(InternalEfiShellSetEnv(L"debuglasterror", LeString, TRUE););
1847 InternalEfiShellSetEnv(L"lasterror", LeString, TRUE);
1848 } else {
1849 //
1850 // Check if it's a NSH (script) file.
1851 //
1852 TempLocation = CommandWithPath+StrLen(CommandWithPath)-4;
1853 TempLocation2 = mScriptExtension;
1854 if ((StrLen(CommandWithPath) > 4) && (StringNoCaseCompare((VOID*)(&TempLocation), (VOID*)(&TempLocation2)) == 0)) {
1855 Status = RunScriptFile (CommandWithPath);
1856 } else {
1857 DevPath = ShellInfoObject.NewEfiShellProtocol->GetDevicePathFromFilePath(CommandWithPath);
1858 ASSERT(DevPath != NULL);
1859 Status = InternalShellExecuteDevicePath(
1860 &gImageHandle,
1861 DevPath,
1862 CleanOriginal,
1863 NULL,
1864 &StatusCode
1865 );
1866
1867 //
1868 // Update last error status.
1869 //
1870 if (sizeof(EFI_STATUS) == sizeof(UINT64)) {
1871 UnicodeSPrint(LeString, sizeof(LeString), L"0x%Lx", StatusCode);
1872 } else {
1873 UnicodeSPrint(LeString, sizeof(LeString), L"0x%x", StatusCode);
1874 }
1875 DEBUG_CODE(InternalEfiShellSetEnv(L"debuglasterror", LeString, TRUE););
1876 InternalEfiShellSetEnv(L"lasterror", LeString, TRUE);
1877 }
1878 }
1879 }
1880
1881 //
1882 // Print some error info.
1883 //
1884 if (EFI_ERROR(Status)) {
1885 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_ERROR), ShellInfoObject.HiiHandle, (VOID*)(Status));
1886 }
1887
1888 CommandName = StrnCatGrow(&CommandName, NULL, ShellInfoObject.NewShellParametersProtocol->Argv[0], 0);
1889
1890 RestoreArgcArgv(ShellInfoObject.NewShellParametersProtocol, &Argv, &Argc);
1891
1892 RestoreStdInStdOutStdErr(ShellInfoObject.NewShellParametersProtocol, &OriginalStdIn, &OriginalStdOut, &OriginalStdErr, &OriginalSystemTableInfo);
1893 }
1894 if (CommandName != NULL) {
1895 if (ShellCommandGetCurrentScriptFile() != NULL && !IsScriptOnlyCommand(CommandName)) {
1896 //
1897 // if this is NOT a scipt only command return success so the script won't quit.
1898 // prevent killing the script - this is the only place where we know the actual command name (after alias and variable replacement...)
1899 //
1900 Status = EFI_SUCCESS;
1901 }
1902 }
1903 }
1904
1905 SHELL_FREE_NON_NULL(CommandName);
1906 SHELL_FREE_NON_NULL(CommandWithPath);
1907 SHELL_FREE_NON_NULL(CleanOriginal);
1908
1909 return (Status);
1910 }
1911
1912 STATIC CONST UINT16 InvalidChars[] = {L'*', L'?', L'<', L'>', L'\\', L'/', L'\"', 0x0001, 0x0002};
1913 /**
1914 Function determins if the CommandName COULD be a valid command. It does not determine whether
1915 this is a valid command. It only checks for invalid characters.
1916
1917 @param[in] CommandName The name to check
1918
1919 @retval TRUE CommandName could be a command name
1920 @retval FALSE CommandName could not be a valid command name
1921 **/
1922 BOOLEAN
1923 EFIAPI
1924 IsValidCommandName(
1925 IN CONST CHAR16 *CommandName
1926 )
1927 {
1928 UINTN Count;
1929 if (CommandName == NULL) {
1930 ASSERT(FALSE);
1931 return (FALSE);
1932 }
1933 for ( Count = 0
1934 ; Count < sizeof(InvalidChars) / sizeof(InvalidChars[0])
1935 ; Count++
1936 ){
1937 if (ScanMem16(CommandName, StrSize(CommandName), InvalidChars[Count]) != NULL) {
1938 return (FALSE);
1939 }
1940 }
1941 return (TRUE);
1942 }
1943
1944 /**
1945 Function to process a NSH script file via SHELL_FILE_HANDLE.
1946
1947 @param[in] Handle The handle to the already opened file.
1948 @param[in] Name The name of the script file.
1949
1950 @retval EFI_SUCCESS the script completed sucessfully
1951 **/
1952 EFI_STATUS
1953 EFIAPI
1954 RunScriptFileHandle (
1955 IN SHELL_FILE_HANDLE Handle,
1956 IN CONST CHAR16 *Name
1957 )
1958 {
1959 EFI_STATUS Status;
1960 SCRIPT_FILE *NewScriptFile;
1961 UINTN LoopVar;
1962 CHAR16 *CommandLine;
1963 CHAR16 *CommandLine2;
1964 CHAR16 *CommandLine3;
1965 SCRIPT_COMMAND_LIST *LastCommand;
1966 BOOLEAN Ascii;
1967 BOOLEAN PreScriptEchoState;
1968 BOOLEAN PreCommandEchoState;
1969 CONST CHAR16 *CurDir;
1970 UINTN LineCount;
1971 CHAR16 LeString[50];
1972
1973 ASSERT(!ShellCommandGetScriptExit());
1974
1975 PreScriptEchoState = ShellCommandGetEchoState();
1976
1977 NewScriptFile = (SCRIPT_FILE*)AllocateZeroPool(sizeof(SCRIPT_FILE));
1978 if (NewScriptFile == NULL) {
1979 return (EFI_OUT_OF_RESOURCES);
1980 }
1981
1982 //
1983 // Set up the name
1984 //
1985 ASSERT(NewScriptFile->ScriptName == NULL);
1986 NewScriptFile->ScriptName = StrnCatGrow(&NewScriptFile->ScriptName, NULL, Name, 0);
1987 if (NewScriptFile->ScriptName == NULL) {
1988 DeleteScriptFileStruct(NewScriptFile);
1989 return (EFI_OUT_OF_RESOURCES);
1990 }
1991
1992 //
1993 // Save the parameters (used to replace %0 to %9 later on)
1994 //
1995 NewScriptFile->Argc = ShellInfoObject.NewShellParametersProtocol->Argc;
1996 if (NewScriptFile->Argc != 0) {
1997 NewScriptFile->Argv = (CHAR16**)AllocateZeroPool(NewScriptFile->Argc * sizeof(CHAR16*));
1998 if (NewScriptFile->Argv == NULL) {
1999 DeleteScriptFileStruct(NewScriptFile);
2000 return (EFI_OUT_OF_RESOURCES);
2001 }
2002 for (LoopVar = 0 ; LoopVar < 10 && LoopVar < NewScriptFile->Argc; LoopVar++) {
2003 ASSERT(NewScriptFile->Argv[LoopVar] == NULL);
2004 NewScriptFile->Argv[LoopVar] = StrnCatGrow(&NewScriptFile->Argv[LoopVar], NULL, ShellInfoObject.NewShellParametersProtocol->Argv[LoopVar], 0);
2005 if (NewScriptFile->Argv[LoopVar] == NULL) {
2006 DeleteScriptFileStruct(NewScriptFile);
2007 return (EFI_OUT_OF_RESOURCES);
2008 }
2009 }
2010 } else {
2011 NewScriptFile->Argv = NULL;
2012 }
2013
2014 InitializeListHead(&NewScriptFile->CommandList);
2015 InitializeListHead(&NewScriptFile->SubstList);
2016
2017 //
2018 // Now build the list of all script commands.
2019 //
2020 LineCount = 0;
2021 while(!ShellFileHandleEof(Handle)) {
2022 CommandLine = ShellFileHandleReturnLine(Handle, &Ascii);
2023 LineCount++;
2024 if (CommandLine == NULL || StrLen(CommandLine) == 0) {
2025 continue;
2026 }
2027 NewScriptFile->CurrentCommand = AllocateZeroPool(sizeof(SCRIPT_COMMAND_LIST));
2028 if (NewScriptFile->CurrentCommand == NULL) {
2029 DeleteScriptFileStruct(NewScriptFile);
2030 return (EFI_OUT_OF_RESOURCES);
2031 }
2032
2033 NewScriptFile->CurrentCommand->Cl = CommandLine;
2034 NewScriptFile->CurrentCommand->Data = NULL;
2035 NewScriptFile->CurrentCommand->Line = LineCount;
2036
2037 InsertTailList(&NewScriptFile->CommandList, &NewScriptFile->CurrentCommand->Link);
2038 }
2039
2040 //
2041 // Add this as the topmost script file
2042 //
2043 ShellCommandSetNewScript (NewScriptFile);
2044
2045 //
2046 // Now enumerate through the commands and run each one.
2047 //
2048 CommandLine = AllocateZeroPool(PcdGet16(PcdShellPrintBufferSize));
2049 if (CommandLine == NULL) {
2050 DeleteScriptFileStruct(NewScriptFile);
2051 return (EFI_OUT_OF_RESOURCES);
2052 }
2053 CommandLine2 = AllocateZeroPool(PcdGet16(PcdShellPrintBufferSize));
2054 if (CommandLine2 == NULL) {
2055 FreePool(CommandLine);
2056 DeleteScriptFileStruct(NewScriptFile);
2057 return (EFI_OUT_OF_RESOURCES);
2058 }
2059
2060 for ( NewScriptFile->CurrentCommand = (SCRIPT_COMMAND_LIST *)GetFirstNode(&NewScriptFile->CommandList)
2061 ; !IsNull(&NewScriptFile->CommandList, &NewScriptFile->CurrentCommand->Link)
2062 ; // conditional increment in the body of the loop
2063 ){
2064 ASSERT(CommandLine2 != NULL);
2065 StrCpy(CommandLine2, NewScriptFile->CurrentCommand->Cl);
2066
2067 //
2068 // NULL out comments
2069 //
2070 for (CommandLine3 = CommandLine2 ; CommandLine3 != NULL && *CommandLine3 != CHAR_NULL ; CommandLine3++) {
2071 if (*CommandLine3 == L'^') {
2072 if (*(CommandLine3+1) == L'#' || *(CommandLine3+1) == L':') {
2073 CopyMem(CommandLine3, CommandLine3+1, StrSize(CommandLine3) - sizeof(CommandLine3[0]));
2074 }
2075 } else if (*CommandLine3 == L'#') {
2076 *CommandLine3 = CHAR_NULL;
2077 }
2078 }
2079
2080 if (CommandLine2 != NULL && StrLen(CommandLine2) >= 1) {
2081 //
2082 // Due to variability in starting the find and replace action we need to have both buffers the same.
2083 //
2084 StrCpy(CommandLine, CommandLine2);
2085
2086 //
2087 // Remove the %0 to %9 from the command line (if we have some arguments)
2088 //
2089 if (NewScriptFile->Argv != NULL) {
2090 switch (NewScriptFile->Argc) {
2091 default:
2092 Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PcdGet16 (PcdShellPrintBufferSize), L"%9", NewScriptFile->Argv[9], FALSE, TRUE);
2093 ASSERT_EFI_ERROR(Status);
2094 case 9:
2095 Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PcdGet16 (PcdShellPrintBufferSize), L"%8", NewScriptFile->Argv[8], FALSE, TRUE);
2096 ASSERT_EFI_ERROR(Status);
2097 case 8:
2098 Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PcdGet16 (PcdShellPrintBufferSize), L"%7", NewScriptFile->Argv[7], FALSE, TRUE);
2099 ASSERT_EFI_ERROR(Status);
2100 case 7:
2101 Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PcdGet16 (PcdShellPrintBufferSize), L"%6", NewScriptFile->Argv[6], FALSE, TRUE);
2102 ASSERT_EFI_ERROR(Status);
2103 case 6:
2104 Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PcdGet16 (PcdShellPrintBufferSize), L"%5", NewScriptFile->Argv[5], FALSE, TRUE);
2105 ASSERT_EFI_ERROR(Status);
2106 case 5:
2107 Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PcdGet16 (PcdShellPrintBufferSize), L"%4", NewScriptFile->Argv[4], FALSE, TRUE);
2108 ASSERT_EFI_ERROR(Status);
2109 case 4:
2110 Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PcdGet16 (PcdShellPrintBufferSize), L"%3", NewScriptFile->Argv[3], FALSE, TRUE);
2111 ASSERT_EFI_ERROR(Status);
2112 case 3:
2113 Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PcdGet16 (PcdShellPrintBufferSize), L"%2", NewScriptFile->Argv[2], FALSE, TRUE);
2114 ASSERT_EFI_ERROR(Status);
2115 case 2:
2116 Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PcdGet16 (PcdShellPrintBufferSize), L"%1", NewScriptFile->Argv[1], FALSE, TRUE);
2117 ASSERT_EFI_ERROR(Status);
2118 case 1:
2119 Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PcdGet16 (PcdShellPrintBufferSize), L"%0", NewScriptFile->Argv[0], FALSE, TRUE);
2120 ASSERT_EFI_ERROR(Status);
2121 break;
2122 case 0:
2123 break;
2124 }
2125 }
2126 Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PcdGet16 (PcdShellPrintBufferSize), L"%1", L"\"\"", FALSE, FALSE);
2127 Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PcdGet16 (PcdShellPrintBufferSize), L"%2", L"\"\"", FALSE, FALSE);
2128 Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PcdGet16 (PcdShellPrintBufferSize), L"%3", L"\"\"", FALSE, FALSE);
2129 Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PcdGet16 (PcdShellPrintBufferSize), L"%4", L"\"\"", FALSE, FALSE);
2130 Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PcdGet16 (PcdShellPrintBufferSize), L"%5", L"\"\"", FALSE, FALSE);
2131 Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PcdGet16 (PcdShellPrintBufferSize), L"%6", L"\"\"", FALSE, FALSE);
2132 Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PcdGet16 (PcdShellPrintBufferSize), L"%7", L"\"\"", FALSE, FALSE);
2133 Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PcdGet16 (PcdShellPrintBufferSize), L"%8", L"\"\"", FALSE, FALSE);
2134 Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PcdGet16 (PcdShellPrintBufferSize), L"%9", L"\"\"", FALSE, FALSE);
2135
2136 StrCpy(CommandLine2, CommandLine);
2137
2138 LastCommand = NewScriptFile->CurrentCommand;
2139
2140 for (CommandLine3 = CommandLine2 ; CommandLine3[0] == L' ' ; CommandLine3++);
2141
2142 if (CommandLine3 != NULL && CommandLine3[0] == L':' ) {
2143 //
2144 // This line is a goto target / label
2145 //
2146 } else {
2147 if (CommandLine3 != NULL && StrLen(CommandLine3) > 0) {
2148 if (CommandLine3[0] == L'@') {
2149 //
2150 // We need to save the current echo state
2151 // and disable echo for just this command.
2152 //
2153 PreCommandEchoState = ShellCommandGetEchoState();
2154 ShellCommandSetEchoState(FALSE);
2155 Status = RunCommand(CommandLine3+1);
2156
2157 //
2158 // If command was "@echo -off" or "@echo -on" then don't restore echo state
2159 //
2160 if (StrCmp (L"@echo -off", CommandLine3) != 0 &&
2161 StrCmp (L"@echo -on", CommandLine3) != 0) {
2162 //
2163 // Now restore the pre-'@' echo state.
2164 //
2165 ShellCommandSetEchoState(PreCommandEchoState);
2166 }
2167 } else {
2168 if (ShellCommandGetEchoState()) {
2169 CurDir = ShellInfoObject.NewEfiShellProtocol->GetEnv(L"cwd");
2170 if (CurDir != NULL && StrLen(CurDir) > 1) {
2171 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_CURDIR), ShellInfoObject.HiiHandle, CurDir);
2172 } else {
2173 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_SHELL), ShellInfoObject.HiiHandle);
2174 }
2175 ShellPrintEx(-1, -1, L"%s\r\n", CommandLine2);
2176 }
2177 Status = RunCommand(CommandLine3);
2178 }
2179 }
2180
2181 if (ShellCommandGetScriptExit()) {
2182 //
2183 // ShellCommandGetExitCode() always returns a UINT64
2184 //
2185 UnicodeSPrint(LeString, sizeof(LeString), L"0x%Lx", ShellCommandGetExitCode());
2186 DEBUG_CODE(InternalEfiShellSetEnv(L"debuglasterror", LeString, TRUE););
2187 InternalEfiShellSetEnv(L"lasterror", LeString, TRUE);
2188
2189 ShellCommandRegisterExit(FALSE, 0);
2190 Status = EFI_SUCCESS;
2191 break;
2192 }
2193 if (ShellGetExecutionBreakFlag()) {
2194 break;
2195 }
2196 if (EFI_ERROR(Status)) {
2197 break;
2198 }
2199 if (ShellCommandGetExit()) {
2200 break;
2201 }
2202 }
2203 //
2204 // If that commend did not update the CurrentCommand then we need to advance it...
2205 //
2206 if (LastCommand == NewScriptFile->CurrentCommand) {
2207 NewScriptFile->CurrentCommand = (SCRIPT_COMMAND_LIST *)GetNextNode(&NewScriptFile->CommandList, &NewScriptFile->CurrentCommand->Link);
2208 if (!IsNull(&NewScriptFile->CommandList, &NewScriptFile->CurrentCommand->Link)) {
2209 NewScriptFile->CurrentCommand->Reset = TRUE;
2210 }
2211 }
2212 } else {
2213 NewScriptFile->CurrentCommand = (SCRIPT_COMMAND_LIST *)GetNextNode(&NewScriptFile->CommandList, &NewScriptFile->CurrentCommand->Link);
2214 if (!IsNull(&NewScriptFile->CommandList, &NewScriptFile->CurrentCommand->Link)) {
2215 NewScriptFile->CurrentCommand->Reset = TRUE;
2216 }
2217 }
2218 }
2219
2220
2221 FreePool(CommandLine);
2222 FreePool(CommandLine2);
2223 ShellCommandSetNewScript (NULL);
2224
2225 //
2226 // Only if this was the last script reset the state.
2227 //
2228 if (ShellCommandGetCurrentScriptFile()==NULL) {
2229 ShellCommandSetEchoState(PreScriptEchoState);
2230 }
2231 return (EFI_SUCCESS);
2232 }
2233
2234 /**
2235 Function to process a NSH script file.
2236
2237 @param[in] ScriptPath Pointer to the script file name (including file system path).
2238
2239 @retval EFI_SUCCESS the script completed sucessfully
2240 **/
2241 EFI_STATUS
2242 EFIAPI
2243 RunScriptFile (
2244 IN CONST CHAR16 *ScriptPath
2245 )
2246 {
2247 EFI_STATUS Status;
2248 SHELL_FILE_HANDLE FileHandle;
2249
2250 if (ShellIsFile(ScriptPath) != EFI_SUCCESS) {
2251 return (EFI_INVALID_PARAMETER);
2252 }
2253
2254 Status = ShellOpenFileByName(ScriptPath, &FileHandle, EFI_FILE_MODE_READ, 0);
2255 if (EFI_ERROR(Status)) {
2256 return (Status);
2257 }
2258
2259 Status = RunScriptFileHandle(FileHandle, ScriptPath);
2260
2261 ShellCloseFile(&FileHandle);
2262
2263 return (Status);
2264 }