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