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