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