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