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