]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellCommandLib/UefiShellCommandLib.c
935e8397fed3550655a1f54d3371a098fe0e6167
[mirror_edk2.git] / ShellPkg / Library / UefiShellCommandLib / UefiShellCommandLib.c
1 /** @file
2 Provides interface to shell internal functions for shell commands.
3
4 Copyright (c) 2009 - 2013, 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 "UefiShellCommandLib.h"
16
17 // STATIC local variables
18 STATIC SHELL_COMMAND_INTERNAL_LIST_ENTRY mCommandList;
19 STATIC SCRIPT_FILE_LIST mScriptList;
20 STATIC ALIAS_LIST mAliasList;
21 STATIC BOOLEAN mEchoState;
22 STATIC BOOLEAN mExitRequested;
23 STATIC UINT64 mExitCode;
24 STATIC BOOLEAN mExitScript;
25 STATIC CHAR16 *mProfileList;
26 STATIC UINTN mProfileListSize;
27 STATIC UINTN mFsMaxCount = 0;
28 STATIC UINTN mBlkMaxCount = 0;
29 STATIC BUFFER_LIST mFileHandleList;
30
31 // global variables required by library class.
32 EFI_UNICODE_COLLATION_PROTOCOL *gUnicodeCollation = NULL;
33 SHELL_MAP_LIST gShellMapList;
34 SHELL_MAP_LIST *gShellCurDir = NULL;
35
36 CONST CHAR16* SupportLevel[] = {
37 L"Minimal",
38 L"Scripting",
39 L"Basic",
40 L"Interactive"
41 };
42
43 /**
44 Function to make sure that the global protocol pointers are valid.
45 must be called after constructor before accessing the pointers.
46 **/
47 EFI_STATUS
48 EFIAPI
49 CommandInit(
50 VOID
51 )
52 {
53 EFI_STATUS Status;
54 if (gUnicodeCollation == NULL) {
55 Status = gBS->LocateProtocol(&gEfiUnicodeCollation2ProtocolGuid, NULL, (VOID**)&gUnicodeCollation);
56 if (EFI_ERROR(Status)) {
57 return (EFI_DEVICE_ERROR);
58 }
59 }
60 return (EFI_SUCCESS);
61 }
62
63 /**
64 Constructor for the Shell Command library.
65
66 Initialize the library and determine if the underlying is a UEFI Shell 2.0 or an EFI shell.
67
68 @param ImageHandle the image handle of the process
69 @param SystemTable the EFI System Table pointer
70
71 @retval EFI_SUCCESS the initialization was complete sucessfully
72 **/
73 RETURN_STATUS
74 EFIAPI
75 ShellCommandLibConstructor (
76 IN EFI_HANDLE ImageHandle,
77 IN EFI_SYSTEM_TABLE *SystemTable
78 )
79 {
80 EFI_STATUS Status;
81 InitializeListHead(&gShellMapList.Link);
82 InitializeListHead(&mCommandList.Link);
83 InitializeListHead(&mAliasList.Link);
84 InitializeListHead(&mScriptList.Link);
85 InitializeListHead(&mFileHandleList.Link);
86 mEchoState = TRUE;
87
88 mExitRequested = FALSE;
89 mExitScript = FALSE;
90 mProfileListSize = 0;
91 mProfileList = NULL;
92
93 if (gUnicodeCollation == NULL) {
94 Status = gBS->LocateProtocol(&gEfiUnicodeCollation2ProtocolGuid, NULL, (VOID**)&gUnicodeCollation);
95 if (EFI_ERROR(Status)) {
96 return (EFI_DEVICE_ERROR);
97 }
98 }
99
100 return (RETURN_SUCCESS);
101 }
102
103 /**
104 Destructor for the library. free any resources.
105
106 @param ImageHandle the image handle of the process
107 @param SystemTable the EFI System Table pointer
108
109 @retval RETURN_SUCCESS this function always returns success
110 **/
111 RETURN_STATUS
112 EFIAPI
113 ShellCommandLibDestructor (
114 IN EFI_HANDLE ImageHandle,
115 IN EFI_SYSTEM_TABLE *SystemTable
116 )
117 {
118 SHELL_COMMAND_INTERNAL_LIST_ENTRY *Node;
119 ALIAS_LIST *Node2;
120 SCRIPT_FILE_LIST *Node3;
121 SHELL_MAP_LIST *MapNode;
122 //
123 // enumerate throught the list and free all the memory
124 //
125 while (!IsListEmpty (&mCommandList.Link)) {
126 Node = (SHELL_COMMAND_INTERNAL_LIST_ENTRY *)GetFirstNode(&mCommandList.Link);
127 RemoveEntryList(&Node->Link);
128 SHELL_FREE_NON_NULL(Node->CommandString);
129 FreePool(Node);
130 DEBUG_CODE(Node = NULL;);
131 }
132
133 //
134 // enumerate through the alias list and free all memory
135 //
136 while (!IsListEmpty (&mAliasList.Link)) {
137 Node2 = (ALIAS_LIST *)GetFirstNode(&mAliasList.Link);
138 RemoveEntryList(&Node2->Link);
139 SHELL_FREE_NON_NULL(Node2->CommandString);
140 SHELL_FREE_NON_NULL(Node2->Alias);
141 SHELL_FREE_NON_NULL(Node2);
142 DEBUG_CODE(Node2 = NULL;);
143 }
144
145 //
146 // enumerate throught the list and free all the memory
147 //
148 while (!IsListEmpty (&mScriptList.Link)) {
149 Node3 = (SCRIPT_FILE_LIST *)GetFirstNode(&mScriptList.Link);
150 RemoveEntryList(&Node3->Link);
151 DeleteScriptFileStruct(Node3->Data);
152 FreePool(Node3);
153 }
154
155 //
156 // enumerate throught the mappings list and free all the memory
157 //
158 if (!IsListEmpty(&gShellMapList.Link)) {
159 for (MapNode = (SHELL_MAP_LIST *)GetFirstNode(&gShellMapList.Link)
160 ; !IsListEmpty (&gShellMapList.Link)
161 ; MapNode = (SHELL_MAP_LIST *)GetFirstNode(&gShellMapList.Link)
162 ){
163 ASSERT(MapNode != NULL);
164 RemoveEntryList(&MapNode->Link);
165 SHELL_FREE_NON_NULL(MapNode->DevicePath);
166 SHELL_FREE_NON_NULL(MapNode->MapName);
167 SHELL_FREE_NON_NULL(MapNode->CurrentDirectoryPath);
168 FreePool(MapNode);
169 }
170 }
171 if (!IsListEmpty(&mFileHandleList.Link)){
172 FreeBufferList(&mFileHandleList);
173 }
174
175 if (mProfileList != NULL) {
176 FreePool(mProfileList);
177 }
178
179 gUnicodeCollation = NULL;
180 gShellCurDir = NULL;
181
182 return (RETURN_SUCCESS);
183 }
184
185 /**
186 Checks if a command is already on the list.
187
188 @param[in] CommandString The command string to check for on the list.
189 **/
190 BOOLEAN
191 EFIAPI
192 ShellCommandIsCommandOnList (
193 IN CONST CHAR16 *CommandString
194 )
195 {
196 SHELL_COMMAND_INTERNAL_LIST_ENTRY *Node;
197
198 //
199 // assert for NULL parameter
200 //
201 ASSERT(CommandString != NULL);
202
203 //
204 // check for the command
205 //
206 for ( Node = (SHELL_COMMAND_INTERNAL_LIST_ENTRY *)GetFirstNode(&mCommandList.Link)
207 ; !IsNull(&mCommandList.Link, &Node->Link)
208 ; Node = (SHELL_COMMAND_INTERNAL_LIST_ENTRY *)GetNextNode(&mCommandList.Link, &Node->Link)
209 ){
210 ASSERT(Node->CommandString != NULL);
211 if (gUnicodeCollation->StriColl(
212 gUnicodeCollation,
213 (CHAR16*)CommandString,
214 Node->CommandString) == 0
215 ){
216 return (TRUE);
217 }
218 }
219 return (FALSE);
220 }
221
222 /**
223 Get the help text for a command.
224
225 @param[in] CommandString The command name.
226
227 @retval NULL No help text was found.
228 @return String of help text. Caller reuiqred to free.
229 **/
230 CHAR16*
231 EFIAPI
232 ShellCommandGetCommandHelp (
233 IN CONST CHAR16 *CommandString
234 )
235 {
236 SHELL_COMMAND_INTERNAL_LIST_ENTRY *Node;
237
238 //
239 // assert for NULL parameter
240 //
241 ASSERT(CommandString != NULL);
242
243 //
244 // check for the command
245 //
246 for ( Node = (SHELL_COMMAND_INTERNAL_LIST_ENTRY *)GetFirstNode(&mCommandList.Link)
247 ; !IsNull(&mCommandList.Link, &Node->Link)
248 ; Node = (SHELL_COMMAND_INTERNAL_LIST_ENTRY *)GetNextNode(&mCommandList.Link, &Node->Link)
249 ){
250 ASSERT(Node->CommandString != NULL);
251 if (gUnicodeCollation->StriColl(
252 gUnicodeCollation,
253 (CHAR16*)CommandString,
254 Node->CommandString) == 0
255 ){
256 return (HiiGetString(Node->HiiHandle, Node->ManFormatHelp, NULL));
257 }
258 }
259 return (NULL);
260 }
261
262 /**
263 Registers handlers of type SHELL_RUN_COMMAND and
264 SHELL_GET_MAN_FILENAME for each shell command.
265
266 If the ShellSupportLevel is greater than the value of the
267 PcdShellSupportLevel then return RETURN_UNSUPPORTED.
268
269 Registers the handlers specified by GetHelpInfoHandler and CommandHandler
270 with the command specified by CommandString. If the command named by
271 CommandString has already been registered, then return
272 RETURN_ALREADY_STARTED.
273
274 If there are not enough resources available to register the handlers then
275 RETURN_OUT_OF_RESOURCES is returned.
276
277 If CommandString is NULL, then ASSERT().
278 If GetHelpInfoHandler is NULL, then ASSERT().
279 If CommandHandler is NULL, then ASSERT().
280 If ProfileName is NULL, then ASSERT().
281
282 @param[in] CommandString Pointer to the command name. This is the
283 name to look for on the command line in
284 the shell.
285 @param[in] CommandHandler Pointer to a function that runs the
286 specified command.
287 @param[in] GetManFileName Pointer to a function that provides man
288 filename.
289 @param[in] ShellMinSupportLevel minimum Shell Support Level which has this
290 function.
291 @param[in] ProfileName profile name to require for support of this
292 function.
293 @param[in] CanAffectLE indicates whether this command's return value
294 can change the LASTERROR environment variable.
295 @param[in] HiiHandle Handle of this command's HII entry.
296 @param[in] ManFormatHelp HII locator for the help text.
297
298 @retval RETURN_SUCCESS The handlers were registered.
299 @retval RETURN_OUT_OF_RESOURCES There are not enough resources available to
300 register the shell command.
301 @retval RETURN_UNSUPPORTED the ShellMinSupportLevel was higher than the
302 currently allowed support level.
303 @retval RETURN_ALREADY_STARTED The CommandString represents a command that
304 is already registered. Only 1 handler set for
305 a given command is allowed.
306 @sa SHELL_GET_MAN_FILENAME
307 @sa SHELL_RUN_COMMAND
308 **/
309 RETURN_STATUS
310 EFIAPI
311 ShellCommandRegisterCommandName (
312 IN CONST CHAR16 *CommandString,
313 IN SHELL_RUN_COMMAND CommandHandler,
314 IN SHELL_GET_MAN_FILENAME GetManFileName,
315 IN UINT32 ShellMinSupportLevel,
316 IN CONST CHAR16 *ProfileName,
317 IN CONST BOOLEAN CanAffectLE,
318 IN CONST EFI_HANDLE HiiHandle,
319 IN CONST EFI_STRING_ID ManFormatHelp
320 )
321 {
322 SHELL_COMMAND_INTERNAL_LIST_ENTRY *Node;
323 SHELL_COMMAND_INTERNAL_LIST_ENTRY *Command;
324 SHELL_COMMAND_INTERNAL_LIST_ENTRY *PrevCommand;
325 INTN LexicalMatchValue;
326
327 //
328 // Initialize local variables.
329 //
330 Command = NULL;
331 PrevCommand = NULL;
332 LexicalMatchValue = 0;
333
334 //
335 // ASSERTs for NULL parameters
336 //
337 ASSERT(CommandString != NULL);
338 ASSERT(GetManFileName != NULL);
339 ASSERT(CommandHandler != NULL);
340 ASSERT(ProfileName != NULL);
341
342 //
343 // check for shell support level
344 //
345 if (PcdGet8(PcdShellSupportLevel) < ShellMinSupportLevel) {
346 return (RETURN_UNSUPPORTED);
347 }
348
349 //
350 // check for already on the list
351 //
352 if (ShellCommandIsCommandOnList(CommandString)) {
353 return (RETURN_ALREADY_STARTED);
354 }
355
356 //
357 // allocate memory for new struct
358 //
359 Node = AllocateZeroPool(sizeof(SHELL_COMMAND_INTERNAL_LIST_ENTRY));
360 ASSERT(Node != NULL);
361 Node->CommandString = AllocateZeroPool(StrSize(CommandString));
362 ASSERT(Node->CommandString != NULL);
363
364 //
365 // populate the new struct
366 //
367 StrCpy(Node->CommandString, CommandString);
368
369 Node->GetManFileName = GetManFileName;
370 Node->CommandHandler = CommandHandler;
371 Node->LastError = CanAffectLE;
372 Node->HiiHandle = HiiHandle;
373 Node->ManFormatHelp = ManFormatHelp;
374
375 if ( StrLen(ProfileName)>0
376 && ((mProfileList != NULL
377 && StrStr(mProfileList, ProfileName) == NULL) || mProfileList == NULL)
378 ){
379 ASSERT((mProfileList == NULL && mProfileListSize == 0) || (mProfileList != NULL));
380 if (mProfileList == NULL) {
381 //
382 // If this is the first make a leading ';'
383 //
384 StrnCatGrow(&mProfileList, &mProfileListSize, L";", 0);
385 }
386 StrnCatGrow(&mProfileList, &mProfileListSize, ProfileName, 0);
387 StrnCatGrow(&mProfileList, &mProfileListSize, L";", 0);
388 }
389
390 //
391 // Insert a new entry on top of the list
392 //
393 InsertHeadList (&mCommandList.Link, &Node->Link);
394
395 //
396 // Move a new registered command to its sorted ordered location in the list
397 //
398 for (Command = (SHELL_COMMAND_INTERNAL_LIST_ENTRY *)GetFirstNode (&mCommandList.Link),
399 PrevCommand = (SHELL_COMMAND_INTERNAL_LIST_ENTRY *)GetFirstNode (&mCommandList.Link)
400 ; !IsNull (&mCommandList.Link, &Command->Link)
401 ; Command = (SHELL_COMMAND_INTERNAL_LIST_ENTRY *)GetNextNode (&mCommandList.Link, &Command->Link)) {
402
403 //
404 // Get Lexical Comparison Value between PrevCommand and Command list entry
405 //
406 LexicalMatchValue = gUnicodeCollation->StriColl (
407 gUnicodeCollation,
408 PrevCommand->CommandString,
409 Command->CommandString
410 );
411
412 //
413 // Swap PrevCommand and Command list entry if PrevCommand list entry
414 // is alphabetically greater than Command list entry
415 //
416 if (LexicalMatchValue > 0){
417 Command = (SHELL_COMMAND_INTERNAL_LIST_ENTRY *) SwapListEntries (&PrevCommand->Link, &Command->Link);
418 } else if (LexicalMatchValue < 0) {
419 //
420 // PrevCommand entry is lexically lower than Command entry
421 //
422 break;
423 }
424 }
425
426 return (RETURN_SUCCESS);
427 }
428
429 /**
430 Function to get the current Profile string.
431
432 @retval NULL There are no installed profiles.
433 @return A semi-colon delimited list of profiles.
434 **/
435 CONST CHAR16 *
436 EFIAPI
437 ShellCommandGetProfileList (
438 VOID
439 )
440 {
441 return (mProfileList);
442 }
443
444 /**
445 Checks if a command string has been registered for CommandString and if so it runs
446 the previously registered handler for that command with the command line.
447
448 If CommandString is NULL, then ASSERT().
449
450 If Sections is specified, then each section name listed will be compared in a casesensitive
451 manner, to the section names described in Appendix B UEFI Shell 2.0 spec. If the section exists,
452 it will be appended to the returned help text. If the section does not exist, no
453 information will be returned. If Sections is NULL, then all help text information
454 available will be returned.
455
456 @param[in] CommandString Pointer to the command name. This is the name
457 found on the command line in the shell.
458 @param[in, out] RetVal Pointer to the return vaule from the command handler.
459
460 @param[in, out] CanAffectLE indicates whether this command's return value
461 needs to be placed into LASTERROR environment variable.
462
463 @retval RETURN_SUCCESS The handler was run.
464 @retval RETURN_NOT_FOUND The CommandString did not match a registered
465 command name.
466 @sa SHELL_RUN_COMMAND
467 **/
468 RETURN_STATUS
469 EFIAPI
470 ShellCommandRunCommandHandler (
471 IN CONST CHAR16 *CommandString,
472 IN OUT SHELL_STATUS *RetVal,
473 IN OUT BOOLEAN *CanAffectLE OPTIONAL
474 )
475 {
476 SHELL_COMMAND_INTERNAL_LIST_ENTRY *Node;
477
478 //
479 // assert for NULL parameters
480 //
481 ASSERT(CommandString != NULL);
482
483 //
484 // check for the command
485 //
486 for ( Node = (SHELL_COMMAND_INTERNAL_LIST_ENTRY *)GetFirstNode(&mCommandList.Link)
487 ; !IsNull(&mCommandList.Link, &Node->Link)
488 ; Node = (SHELL_COMMAND_INTERNAL_LIST_ENTRY *)GetNextNode(&mCommandList.Link, &Node->Link)
489 ){
490 ASSERT(Node->CommandString != NULL);
491 if (gUnicodeCollation->StriColl(
492 gUnicodeCollation,
493 (CHAR16*)CommandString,
494 Node->CommandString) == 0
495 ){
496 if (CanAffectLE != NULL) {
497 *CanAffectLE = Node->LastError;
498 }
499 if (RetVal != NULL) {
500 *RetVal = Node->CommandHandler(NULL, gST);
501 } else {
502 Node->CommandHandler(NULL, gST);
503 }
504 return (RETURN_SUCCESS);
505 }
506 }
507 return (RETURN_NOT_FOUND);
508 }
509
510 /**
511 Checks if a command string has been registered for CommandString and if so it
512 returns the MAN filename specified for that command.
513
514 If CommandString is NULL, then ASSERT().
515
516 @param[in] CommandString Pointer to the command name. This is the name
517 found on the command line in the shell.\
518
519 @retval NULL the commandString was not a registered command.
520 @return other the name of the MAN file.
521 @sa SHELL_GET_MAN_FILENAME
522 **/
523 CONST CHAR16*
524 EFIAPI
525 ShellCommandGetManFileNameHandler (
526 IN CONST CHAR16 *CommandString
527 )
528 {
529 SHELL_COMMAND_INTERNAL_LIST_ENTRY *Node;
530
531 //
532 // assert for NULL parameters
533 //
534 ASSERT(CommandString != NULL);
535
536 //
537 // check for the command
538 //
539 for ( Node = (SHELL_COMMAND_INTERNAL_LIST_ENTRY *)GetFirstNode(&mCommandList.Link)
540 ; !IsNull(&mCommandList.Link, &Node->Link)
541 ; Node = (SHELL_COMMAND_INTERNAL_LIST_ENTRY *)GetNextNode(&mCommandList.Link, &Node->Link)
542 ){
543 ASSERT(Node->CommandString != NULL);
544 if (gUnicodeCollation->StriColl(
545 gUnicodeCollation,
546 (CHAR16*)CommandString,
547 Node->CommandString) == 0
548 ){
549 return (Node->GetManFileName());
550 }
551 }
552 return (NULL);
553 }
554
555 /**
556 Get the list of all available shell internal commands. This is a linked list
557 (via LIST_ENTRY structure). enumerate through it using the BaseLib linked
558 list functions. do not modify the values.
559
560 @param[in] Sort TRUE to alphabetically sort the values first. FALSE otherwise.
561
562 @return a Linked list of all available shell commands.
563 **/
564 CONST COMMAND_LIST*
565 EFIAPI
566 ShellCommandGetCommandList (
567 IN CONST BOOLEAN Sort
568 )
569 {
570 // if (!Sort) {
571 // return ((COMMAND_LIST*)(&mCommandList));
572 // }
573 return ((COMMAND_LIST*)(&mCommandList));
574 }
575
576 /**
577 Registers aliases to be set as part of the initialization of the shell application.
578
579 If Command is NULL, then ASSERT().
580 If Alias is NULL, then ASSERT().
581
582 @param[in] Command Pointer to the Command
583 @param[in] Alias Pointer to Alias
584
585 @retval RETURN_SUCCESS The handlers were registered.
586 @retval RETURN_OUT_OF_RESOURCES There are not enough resources available to
587 register the shell command.
588 **/
589 RETURN_STATUS
590 EFIAPI
591 ShellCommandRegisterAlias (
592 IN CONST CHAR16 *Command,
593 IN CONST CHAR16 *Alias
594 )
595 {
596 ALIAS_LIST *Node;
597
598 //
599 // Asserts for NULL
600 //
601 ASSERT(Command != NULL);
602 ASSERT(Alias != NULL);
603
604 //
605 // allocate memory for new struct
606 //
607 Node = AllocateZeroPool(sizeof(ALIAS_LIST));
608 ASSERT(Node != NULL);
609 Node->CommandString = AllocateZeroPool(StrSize(Command));
610 Node->Alias = AllocateZeroPool(StrSize(Alias));
611 ASSERT(Node->CommandString != NULL);
612 ASSERT(Node->Alias != NULL);
613
614 //
615 // populate the new struct
616 //
617 StrCpy(Node->CommandString, Command);
618 StrCpy(Node->Alias , Alias );
619
620 //
621 // add the new struct to the list
622 //
623 InsertTailList (&mAliasList.Link, &Node->Link);
624
625 return (RETURN_SUCCESS);
626 }
627
628 /**
629 Get the list of all shell alias commands. This is a linked list
630 (via LIST_ENTRY structure). enumerate through it using the BaseLib linked
631 list functions. do not modify the values.
632
633 @return a Linked list of all requested shell alias'.
634 **/
635 CONST ALIAS_LIST*
636 EFIAPI
637 ShellCommandGetInitAliasList (
638 VOID
639 )
640 {
641 return (&mAliasList);
642 }
643
644 /**
645 Determine if a given alias is on the list of built in alias'.
646
647 @param[in] Alias The alias to test for
648
649 @retval TRUE The alias is a built in alias
650 @retval FALSE The alias is not a built in alias
651 **/
652 BOOLEAN
653 EFIAPI
654 ShellCommandIsOnAliasList(
655 IN CONST CHAR16 *Alias
656 )
657 {
658 ALIAS_LIST *Node;
659
660 //
661 // assert for NULL parameter
662 //
663 ASSERT(Alias != NULL);
664
665 //
666 // check for the Alias
667 //
668 for ( Node = (ALIAS_LIST *)GetFirstNode(&mAliasList.Link)
669 ; !IsNull(&mAliasList.Link, &Node->Link)
670 ; Node = (ALIAS_LIST *)GetNextNode(&mAliasList.Link, &Node->Link)
671 ){
672 ASSERT(Node->CommandString != NULL);
673 ASSERT(Node->Alias != NULL);
674 if (gUnicodeCollation->StriColl(
675 gUnicodeCollation,
676 (CHAR16*)Alias,
677 Node->CommandString) == 0
678 ){
679 return (TRUE);
680 }
681 if (gUnicodeCollation->StriColl(
682 gUnicodeCollation,
683 (CHAR16*)Alias,
684 Node->Alias) == 0
685 ){
686 return (TRUE);
687 }
688 }
689 return (FALSE);
690 }
691
692 /**
693 Function to determine current state of ECHO. Echo determins if lines from scripts
694 and ECHO commands are enabled.
695
696 @retval TRUE Echo is currently enabled
697 @retval FALSE Echo is currently disabled
698 **/
699 BOOLEAN
700 EFIAPI
701 ShellCommandGetEchoState(
702 VOID
703 )
704 {
705 return (mEchoState);
706 }
707
708 /**
709 Function to set current state of ECHO. Echo determins if lines from scripts
710 and ECHO commands are enabled.
711
712 If State is TRUE, Echo will be enabled.
713 If State is FALSE, Echo will be disabled.
714
715 @param[in] State How to set echo.
716 **/
717 VOID
718 EFIAPI
719 ShellCommandSetEchoState(
720 IN BOOLEAN State
721 )
722 {
723 mEchoState = State;
724 }
725
726 /**
727 Indicate that the current shell or script should exit.
728
729 @param[in] ScriptOnly TRUE if exiting a script; FALSE otherwise.
730 @param[in] ErrorCode The 64 bit error code to return.
731 **/
732 VOID
733 EFIAPI
734 ShellCommandRegisterExit (
735 IN BOOLEAN ScriptOnly,
736 IN CONST UINT64 ErrorCode
737 )
738 {
739 mExitRequested = (BOOLEAN)(!mExitRequested);
740 if (mExitRequested) {
741 mExitScript = ScriptOnly;
742 } else {
743 mExitScript = FALSE;
744 }
745 mExitCode = ErrorCode;
746 }
747
748 /**
749 Retrieve the Exit indicator.
750
751 @retval TRUE Exit was indicated.
752 @retval FALSE Exis was not indicated.
753 **/
754 BOOLEAN
755 EFIAPI
756 ShellCommandGetExit (
757 VOID
758 )
759 {
760 return (mExitRequested);
761 }
762
763 /**
764 Retrieve the Exit code.
765
766 If ShellCommandGetExit returns FALSE than the return from this is undefined.
767
768 @return the value passed into RegisterExit.
769 **/
770 UINT64
771 EFIAPI
772 ShellCommandGetExitCode (
773 VOID
774 )
775 {
776 return (mExitCode);
777 }
778 /**
779 Retrieve the Exit script indicator.
780
781 If ShellCommandGetExit returns FALSE than the return from this is undefined.
782
783 @retval TRUE ScriptOnly was indicated.
784 @retval FALSE ScriptOnly was not indicated.
785 **/
786 BOOLEAN
787 EFIAPI
788 ShellCommandGetScriptExit (
789 VOID
790 )
791 {
792 return (mExitScript);
793 }
794
795 /**
796 Function to cleanup all memory from a SCRIPT_FILE structure.
797
798 @param[in] Script The pointer to the structure to cleanup.
799 **/
800 VOID
801 EFIAPI
802 DeleteScriptFileStruct (
803 IN SCRIPT_FILE *Script
804 )
805 {
806 UINT8 LoopVar;
807
808 if (Script == NULL) {
809 return;
810 }
811
812 for (LoopVar = 0 ; LoopVar < Script->Argc ; LoopVar++) {
813 SHELL_FREE_NON_NULL(Script->Argv[LoopVar]);
814 }
815 if (Script->Argv != NULL) {
816 SHELL_FREE_NON_NULL(Script->Argv);
817 }
818 Script->CurrentCommand = NULL;
819 while (!IsListEmpty (&Script->CommandList)) {
820 Script->CurrentCommand = (SCRIPT_COMMAND_LIST *)GetFirstNode(&Script->CommandList);
821 if (Script->CurrentCommand != NULL) {
822 RemoveEntryList(&Script->CurrentCommand->Link);
823 if (Script->CurrentCommand->Cl != NULL) {
824 SHELL_FREE_NON_NULL(Script->CurrentCommand->Cl);
825 }
826 if (Script->CurrentCommand->Data != NULL) {
827 SHELL_FREE_NON_NULL(Script->CurrentCommand->Data);
828 }
829 SHELL_FREE_NON_NULL(Script->CurrentCommand);
830 }
831 }
832 SHELL_FREE_NON_NULL(Script->ScriptName);
833 SHELL_FREE_NON_NULL(Script);
834 }
835
836 /**
837 Function to return a pointer to the currently running script file object.
838
839 @retval NULL A script file is not currently running.
840 @return A pointer to the current script file object.
841 **/
842 SCRIPT_FILE*
843 EFIAPI
844 ShellCommandGetCurrentScriptFile (
845 VOID
846 )
847 {
848 SCRIPT_FILE_LIST *List;
849 if (IsListEmpty (&mScriptList.Link)) {
850 return (NULL);
851 }
852 List = ((SCRIPT_FILE_LIST*)GetFirstNode(&mScriptList.Link));
853 return (List->Data);
854 }
855
856 /**
857 Function to set a new script as the currently running one.
858
859 This function will correctly stack and unstack nested scripts.
860
861 @param[in] Script Pointer to new script information structure. if NULL
862 will remove and de-allocate the top-most Script structure.
863
864 @return A pointer to the current running script file after this
865 change. NULL if removing the final script.
866 **/
867 SCRIPT_FILE*
868 EFIAPI
869 ShellCommandSetNewScript (
870 IN SCRIPT_FILE *Script OPTIONAL
871 )
872 {
873 SCRIPT_FILE_LIST *Node;
874 if (Script == NULL) {
875 if (IsListEmpty (&mScriptList.Link)) {
876 return (NULL);
877 }
878 Node = (SCRIPT_FILE_LIST *)GetFirstNode(&mScriptList.Link);
879 RemoveEntryList(&Node->Link);
880 DeleteScriptFileStruct(Node->Data);
881 FreePool(Node);
882 } else {
883 Node = AllocateZeroPool(sizeof(SCRIPT_FILE_LIST));
884 if (Node == NULL) {
885 return (NULL);
886 }
887 Node->Data = Script;
888 InsertHeadList(&mScriptList.Link, &Node->Link);
889 }
890 return (ShellCommandGetCurrentScriptFile());
891 }
892
893 /**
894 Function to generate the next default mapping name.
895
896 If the return value is not NULL then it must be callee freed.
897
898 @param Type What kind of mapping name to make.
899
900 @retval NULL a memory allocation failed.
901 @return a new map name string
902 **/
903 CHAR16*
904 EFIAPI
905 ShellCommandCreateNewMappingName(
906 IN CONST SHELL_MAPPING_TYPE Type
907 )
908 {
909 CHAR16 *String;
910 ASSERT(Type < MappingTypeMax);
911
912 String = NULL;
913
914 String = AllocateZeroPool(PcdGet8(PcdShellMapNameLength) * sizeof(String[0]));
915 UnicodeSPrint(
916 String,
917 PcdGet8(PcdShellMapNameLength) * sizeof(String[0]),
918 Type == MappingTypeFileSystem?L"FS%d:":L"BLK%d:",
919 Type == MappingTypeFileSystem?mFsMaxCount++:mBlkMaxCount++);
920
921 return (String);
922 }
923
924 /**
925 Function to add a map node to the list of map items and update the "path" environment variable (optionally).
926
927 If Path is TRUE (during initialization only), the path environment variable will also be updated to include
928 default paths on the new map name...
929
930 Path should be FALSE when this function is called from the protocol SetMap function.
931
932 @param[in] Name The human readable mapped name.
933 @param[in] DevicePath The Device Path for this map.
934 @param[in] Flags The Flags attribute for this map item.
935 @param[in] Path TRUE to update path, FALSE to skip this step (should only be TRUE during initialization).
936
937 @retval EFI_SUCCESS The addition was sucessful.
938 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
939 @retval EFI_INVALID_PARAMETER A parameter was invalid.
940 **/
941 EFI_STATUS
942 EFIAPI
943 ShellCommandAddMapItemAndUpdatePath(
944 IN CONST CHAR16 *Name,
945 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath,
946 IN CONST UINT64 Flags,
947 IN CONST BOOLEAN Path
948 )
949 {
950 EFI_STATUS Status;
951 SHELL_MAP_LIST *MapListNode;
952 CONST CHAR16 *OriginalPath;
953 CHAR16 *NewPath;
954 UINTN NewPathSize;
955
956 NewPathSize = 0;
957 NewPath = NULL;
958 OriginalPath = NULL;
959 Status = EFI_SUCCESS;
960
961 MapListNode = AllocateZeroPool(sizeof(SHELL_MAP_LIST));
962 if (MapListNode == NULL) {
963 Status = EFI_OUT_OF_RESOURCES;
964 } else {
965 MapListNode->Flags = Flags;
966 MapListNode->MapName = AllocateZeroPool(StrSize(Name));
967 MapListNode->DevicePath = DuplicateDevicePath(DevicePath);
968 if ((MapListNode->MapName == NULL) || (MapListNode->DevicePath == NULL)){
969 Status = EFI_OUT_OF_RESOURCES;
970 } else {
971 StrCpy(MapListNode->MapName, Name);
972 InsertTailList(&gShellMapList.Link, &MapListNode->Link);
973 }
974 }
975 if (EFI_ERROR(Status)) {
976 if (MapListNode != NULL) {
977 if (MapListNode->DevicePath != NULL) {
978 FreePool(MapListNode->DevicePath);
979 }
980 if (MapListNode->MapName != NULL) {
981 FreePool(MapListNode->MapName);
982 }
983 FreePool(MapListNode);
984 }
985 } else if (Path) {
986 //
987 // Since there was no error and Path was TRUE
988 // Now add the correct path for that mapping
989 //
990 OriginalPath = gEfiShellProtocol->GetEnv(L"path");
991 ASSERT((NewPath == NULL && NewPathSize == 0) || (NewPath != NULL));
992 if (OriginalPath != NULL) {
993 StrnCatGrow(&NewPath, &NewPathSize, OriginalPath, 0);
994 } else {
995 StrnCatGrow(&NewPath, &NewPathSize, L".\\", 0);
996 }
997 StrnCatGrow(&NewPath, &NewPathSize, L";", 0);
998 StrnCatGrow(&NewPath, &NewPathSize, Name, 0);
999 StrnCatGrow(&NewPath, &NewPathSize, L"\\efi\\tools\\;", 0);
1000 StrnCatGrow(&NewPath, &NewPathSize, Name, 0);
1001 StrnCatGrow(&NewPath, &NewPathSize, L"\\efi\\boot\\;", 0);
1002 StrnCatGrow(&NewPath, &NewPathSize, Name, 0);
1003 StrnCatGrow(&NewPath, &NewPathSize, L"\\", 0);
1004
1005 Status = gEfiShellProtocol->SetEnv(L"path", NewPath, TRUE);
1006 ASSERT_EFI_ERROR(Status);
1007 FreePool(NewPath);
1008 }
1009 return (Status);
1010 }
1011
1012 /**
1013 Creates the default map names for each device path in the system with
1014 a protocol depending on the Type.
1015
1016 Creates the consistent map names for each device path in the system with
1017 a protocol depending on the Type.
1018
1019 Note: This will reset all mappings in the system("map -r").
1020
1021 Also sets up the default path environment variable if Type is FileSystem.
1022
1023 @retval EFI_SUCCESS All map names were created sucessfully.
1024 @retval EFI_NOT_FOUND No protocols were found in the system.
1025 @return Error returned from gBS->LocateHandle().
1026
1027 @sa LocateHandle
1028 **/
1029 EFI_STATUS
1030 EFIAPI
1031 ShellCommandCreateInitialMappingsAndPaths(
1032 VOID
1033 )
1034 {
1035 EFI_STATUS Status;
1036 EFI_HANDLE *HandleList;
1037 UINTN Count;
1038 EFI_DEVICE_PATH_PROTOCOL **DevicePathList;
1039 CHAR16 *NewDefaultName;
1040 CHAR16 *NewConsistName;
1041 EFI_DEVICE_PATH_PROTOCOL **ConsistMappingTable;
1042 SHELL_MAP_LIST *MapListNode;
1043
1044 HandleList = NULL;
1045
1046 //
1047 // Reset the static members back to zero
1048 //
1049 mFsMaxCount = 0;
1050 mBlkMaxCount = 0;
1051
1052 gEfiShellProtocol->SetEnv(L"path", L"", TRUE);
1053
1054 //
1055 // First empty out the existing list.
1056 //
1057 if (!IsListEmpty(&gShellMapList.Link)) {
1058 for ( MapListNode = (SHELL_MAP_LIST *)GetFirstNode(&gShellMapList.Link)
1059 ; !IsListEmpty(&gShellMapList.Link)
1060 ; MapListNode = (SHELL_MAP_LIST *)GetFirstNode(&gShellMapList.Link)
1061 ){
1062 RemoveEntryList(&MapListNode->Link);
1063 FreePool(MapListNode);
1064 } // for loop
1065 }
1066
1067 //
1068 // Find each handle with Simple File System
1069 //
1070 HandleList = GetHandleListByProtocol(&gEfiSimpleFileSystemProtocolGuid);
1071 if (HandleList != NULL) {
1072 //
1073 // Do a count of the handles
1074 //
1075 for (Count = 0 ; HandleList[Count] != NULL ; Count++);
1076
1077 //
1078 // Get all Device Paths
1079 //
1080 DevicePathList = AllocateZeroPool(sizeof(EFI_DEVICE_PATH_PROTOCOL*) * Count);
1081 ASSERT(DevicePathList != NULL);
1082
1083 for (Count = 0 ; HandleList[Count] != NULL ; Count++) {
1084 DevicePathList[Count] = DevicePathFromHandle(HandleList[Count]);
1085 }
1086
1087 //
1088 // Sort all DevicePaths
1089 //
1090 PerformQuickSort(DevicePathList, Count, sizeof(EFI_DEVICE_PATH_PROTOCOL*), DevicePathCompare);
1091
1092 ShellCommandConsistMappingInitialize(&ConsistMappingTable);
1093 //
1094 // Assign new Mappings to all...
1095 //
1096 for (Count = 0 ; HandleList[Count] != NULL ; Count++) {
1097 //
1098 // Get default name first
1099 //
1100 NewDefaultName = ShellCommandCreateNewMappingName(MappingTypeFileSystem);
1101 ASSERT(NewDefaultName != NULL);
1102 Status = ShellCommandAddMapItemAndUpdatePath(NewDefaultName, DevicePathList[Count], 0, TRUE);
1103 ASSERT_EFI_ERROR(Status);
1104 FreePool(NewDefaultName);
1105
1106 //
1107 // Now do consistent name
1108 //
1109 NewConsistName = ShellCommandConsistMappingGenMappingName(DevicePathList[Count], ConsistMappingTable);
1110 if (NewConsistName != NULL) {
1111 Status = ShellCommandAddMapItemAndUpdatePath(NewConsistName, DevicePathList[Count], 0, FALSE);
1112 ASSERT_EFI_ERROR(Status);
1113 FreePool(NewConsistName);
1114 }
1115 }
1116
1117 ShellCommandConsistMappingUnInitialize(ConsistMappingTable);
1118
1119 SHELL_FREE_NON_NULL(HandleList);
1120 SHELL_FREE_NON_NULL(DevicePathList);
1121
1122 HandleList = NULL;
1123 } else {
1124 Count = (UINTN)-1;
1125 }
1126
1127 //
1128 // Find each handle with Block Io
1129 //
1130 HandleList = GetHandleListByProtocol(&gEfiBlockIoProtocolGuid);
1131 if (HandleList != NULL) {
1132 for (Count = 0 ; HandleList[Count] != NULL ; Count++);
1133
1134 //
1135 // Get all Device Paths
1136 //
1137 DevicePathList = AllocateZeroPool(sizeof(EFI_DEVICE_PATH_PROTOCOL*) * Count);
1138 ASSERT(DevicePathList != NULL);
1139
1140 for (Count = 0 ; HandleList[Count] != NULL ; Count++) {
1141 DevicePathList[Count] = DevicePathFromHandle(HandleList[Count]);
1142 }
1143
1144 //
1145 // Sort all DevicePaths
1146 //
1147 PerformQuickSort(DevicePathList, Count, sizeof(EFI_DEVICE_PATH_PROTOCOL*), DevicePathCompare);
1148
1149 //
1150 // Assign new Mappings to all...
1151 //
1152 for (Count = 0 ; HandleList[Count] != NULL ; Count++) {
1153 //
1154 // Get default name first
1155 //
1156 NewDefaultName = ShellCommandCreateNewMappingName(MappingTypeBlockIo);
1157 ASSERT(NewDefaultName != NULL);
1158 Status = ShellCommandAddMapItemAndUpdatePath(NewDefaultName, DevicePathList[Count], 0, FALSE);
1159 ASSERT_EFI_ERROR(Status);
1160 FreePool(NewDefaultName);
1161 }
1162
1163 SHELL_FREE_NON_NULL(HandleList);
1164 SHELL_FREE_NON_NULL(DevicePathList);
1165 } else if (Count == (UINTN)-1) {
1166 return (EFI_NOT_FOUND);
1167 }
1168
1169 return (EFI_SUCCESS);
1170 }
1171
1172 /**
1173 Converts a SHELL_FILE_HANDLE to an EFI_FILE_PROTOCOL*.
1174
1175 @param[in] Handle The SHELL_FILE_HANDLE to convert.
1176
1177 @return a EFI_FILE_PROTOCOL* representing the same file.
1178 **/
1179 EFI_FILE_PROTOCOL*
1180 EFIAPI
1181 ConvertShellHandleToEfiFileProtocol(
1182 IN CONST SHELL_FILE_HANDLE Handle
1183 )
1184 {
1185 return ((EFI_FILE_PROTOCOL*)(Handle));
1186 }
1187
1188 /**
1189 Converts a EFI_FILE_PROTOCOL* to an SHELL_FILE_HANDLE.
1190
1191 @param[in] Handle The pointer to EFI_FILE_PROTOCOL to convert.
1192 @param[in] Path The path to the file for verification.
1193
1194 @return A SHELL_FILE_HANDLE representing the same file.
1195 @retval NULL There was not enough memory.
1196 **/
1197 SHELL_FILE_HANDLE
1198 EFIAPI
1199 ConvertEfiFileProtocolToShellHandle(
1200 IN CONST EFI_FILE_PROTOCOL *Handle,
1201 IN CONST CHAR16 *Path
1202 )
1203 {
1204 SHELL_COMMAND_FILE_HANDLE *Buffer;
1205 BUFFER_LIST *NewNode;
1206
1207 if (Path != NULL) {
1208 Buffer = AllocateZeroPool(sizeof(SHELL_COMMAND_FILE_HANDLE));
1209 if (Buffer == NULL) {
1210 return (NULL);
1211 }
1212 NewNode = AllocateZeroPool(sizeof(BUFFER_LIST));
1213 if (NewNode == NULL) {
1214 return (NULL);
1215 }
1216 Buffer->FileHandle = (EFI_FILE_PROTOCOL*)Handle;
1217 Buffer->Path = StrnCatGrow(&Buffer->Path, NULL, Path, 0);
1218 if (Buffer->Path == NULL) {
1219 return (NULL);
1220 }
1221 NewNode->Buffer = Buffer;
1222
1223 InsertHeadList(&mFileHandleList.Link, &NewNode->Link);
1224 }
1225 return ((SHELL_FILE_HANDLE)(Handle));
1226 }
1227
1228 /**
1229 Find the path that was logged with the specified SHELL_FILE_HANDLE.
1230
1231 @param[in] Handle The SHELL_FILE_HANDLE to query on.
1232
1233 @return A pointer to the path for the file.
1234 **/
1235 CONST CHAR16*
1236 EFIAPI
1237 ShellFileHandleGetPath(
1238 IN CONST SHELL_FILE_HANDLE Handle
1239 )
1240 {
1241 BUFFER_LIST *Node;
1242
1243 for (Node = (BUFFER_LIST*)GetFirstNode(&mFileHandleList.Link)
1244 ; !IsNull(&mFileHandleList.Link, &Node->Link)
1245 ; Node = (BUFFER_LIST*)GetNextNode(&mFileHandleList.Link, &Node->Link)
1246 ){
1247 if ((Node->Buffer) && (((SHELL_COMMAND_FILE_HANDLE *)Node->Buffer)->FileHandle == Handle)){
1248 return (((SHELL_COMMAND_FILE_HANDLE *)Node->Buffer)->Path);
1249 }
1250 }
1251 return (NULL);
1252 }
1253
1254 /**
1255 Remove a SHELL_FILE_HANDLE from the list of SHELL_FILE_HANDLES.
1256
1257 @param[in] Handle The SHELL_FILE_HANDLE to remove.
1258
1259 @retval TRUE The item was removed.
1260 @retval FALSE The item was not found.
1261 **/
1262 BOOLEAN
1263 EFIAPI
1264 ShellFileHandleRemove(
1265 IN CONST SHELL_FILE_HANDLE Handle
1266 )
1267 {
1268 BUFFER_LIST *Node;
1269
1270 for (Node = (BUFFER_LIST*)GetFirstNode(&mFileHandleList.Link)
1271 ; !IsNull(&mFileHandleList.Link, &Node->Link)
1272 ; Node = (BUFFER_LIST*)GetNextNode(&mFileHandleList.Link, &Node->Link)
1273 ){
1274 if ((Node->Buffer) && (((SHELL_COMMAND_FILE_HANDLE *)Node->Buffer)->FileHandle == Handle)){
1275 RemoveEntryList(&Node->Link);
1276 SHELL_FREE_NON_NULL(((SHELL_COMMAND_FILE_HANDLE *)Node->Buffer)->Path);
1277 SHELL_FREE_NON_NULL(Node->Buffer);
1278 SHELL_FREE_NON_NULL(Node);
1279 return (TRUE);
1280 }
1281 }
1282 return (FALSE);
1283 }
1284
1285 /**
1286 Function to determine if a SHELL_FILE_HANDLE is at the end of the file.
1287
1288 This will NOT work on directories.
1289
1290 If Handle is NULL, then ASSERT.
1291
1292 @param[in] Handle the file handle
1293
1294 @retval TRUE the position is at the end of the file
1295 @retval FALSE the position is not at the end of the file
1296 **/
1297 BOOLEAN
1298 EFIAPI
1299 ShellFileHandleEof(
1300 IN SHELL_FILE_HANDLE Handle
1301 )
1302 {
1303 EFI_FILE_INFO *Info;
1304 UINT64 Pos;
1305 BOOLEAN RetVal;
1306
1307 //
1308 // ASSERT if Handle is NULL
1309 //
1310 ASSERT(Handle != NULL);
1311
1312 gEfiShellProtocol->GetFilePosition(Handle, &Pos);
1313 Info = gEfiShellProtocol->GetFileInfo (Handle);
1314 ASSERT(Info != NULL);
1315 gEfiShellProtocol->SetFilePosition(Handle, Pos);
1316
1317 if (Info == NULL) {
1318 return (FALSE);
1319 }
1320
1321 if (Pos == Info->FileSize) {
1322 RetVal = TRUE;
1323 } else {
1324 RetVal = FALSE;
1325 }
1326
1327 FreePool (Info);
1328
1329 return (RetVal);
1330 }
1331
1332 /**
1333 Frees any BUFFER_LIST defined type.
1334
1335 @param[in] List The BUFFER_LIST object to free.
1336 **/
1337 VOID
1338 EFIAPI
1339 FreeBufferList (
1340 IN BUFFER_LIST *List
1341 )
1342 {
1343 BUFFER_LIST *BufferListEntry;
1344
1345 if (List == NULL){
1346 return;
1347 }
1348 //
1349 // enumerate through the buffer list and free all memory
1350 //
1351 for ( BufferListEntry = ( BUFFER_LIST *)GetFirstNode(&List->Link)
1352 ; !IsListEmpty (&List->Link)
1353 ; BufferListEntry = (BUFFER_LIST *)GetFirstNode(&List->Link)
1354 ){
1355 RemoveEntryList(&BufferListEntry->Link);
1356 ASSERT(BufferListEntry->Buffer != NULL);
1357 if (BufferListEntry->Buffer != NULL) {
1358 FreePool(BufferListEntry->Buffer);
1359 }
1360 FreePool(BufferListEntry);
1361 }
1362 }
1363