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