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