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