]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellCommandLib/UefiShellCommandLib.c
2dd39ef3cf453b091903b3c72d44e0e501c58625
[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
771 if (Script == NULL) {
772 return;
773 }
774
775 for (LoopVar = 0 ; LoopVar < Script->Argc ; LoopVar++) {
776 SHELL_FREE_NON_NULL(Script->Argv[LoopVar]);
777 }
778 if (Script->Argv != NULL) {
779 SHELL_FREE_NON_NULL(Script->Argv);
780 }
781 Script->CurrentCommand = NULL;
782 while (!IsListEmpty (&Script->CommandList)) {
783 Script->CurrentCommand = (SCRIPT_COMMAND_LIST *)GetFirstNode(&Script->CommandList);
784 if (Script->CurrentCommand != NULL) {
785 RemoveEntryList(&Script->CurrentCommand->Link);
786 if (Script->CurrentCommand->Cl != NULL) {
787 SHELL_FREE_NON_NULL(Script->CurrentCommand->Cl);
788 }
789 if (Script->CurrentCommand->Data != NULL) {
790 SHELL_FREE_NON_NULL(Script->CurrentCommand->Data);
791 }
792 SHELL_FREE_NON_NULL(Script->CurrentCommand);
793 }
794 }
795 SHELL_FREE_NON_NULL(Script->ScriptName);
796 SHELL_FREE_NON_NULL(Script);
797 }
798
799 /**
800 Function to return a pointer to the currently running script file object.
801
802 @retval NULL A script file is not currently running.
803 @return A pointer to the current script file object.
804 **/
805 SCRIPT_FILE*
806 EFIAPI
807 ShellCommandGetCurrentScriptFile (
808 VOID
809 )
810 {
811 SCRIPT_FILE_LIST *List;
812 if (IsListEmpty (&mScriptList.Link)) {
813 return (NULL);
814 }
815 List = ((SCRIPT_FILE_LIST*)GetFirstNode(&mScriptList.Link));
816 return (List->Data);
817 }
818
819 /**
820 Function to set a new script as the currently running one.
821
822 This function will correctly stack and unstack nested scripts.
823
824 @param[in] Script Pointer to new script information structure. if NULL
825 will remove and de-allocate the top-most Script structure.
826
827 @return A pointer to the current running script file after this
828 change. NULL if removing the final script.
829 **/
830 SCRIPT_FILE*
831 EFIAPI
832 ShellCommandSetNewScript (
833 IN SCRIPT_FILE *Script OPTIONAL
834 )
835 {
836 SCRIPT_FILE_LIST *Node;
837 if (Script == NULL) {
838 if (IsListEmpty (&mScriptList.Link)) {
839 return (NULL);
840 }
841 Node = (SCRIPT_FILE_LIST *)GetFirstNode(&mScriptList.Link);
842 RemoveEntryList(&Node->Link);
843 DeleteScriptFileStruct(Node->Data);
844 FreePool(Node);
845 } else {
846 Node = AllocateZeroPool(sizeof(SCRIPT_FILE_LIST));
847 if (Node == NULL) {
848 return (NULL);
849 }
850 Node->Data = Script;
851 InsertHeadList(&mScriptList.Link, &Node->Link);
852 }
853 return (ShellCommandGetCurrentScriptFile());
854 }
855
856 /**
857 Function to generate the next default mapping name.
858
859 If the return value is not NULL then it must be callee freed.
860
861 @param Type What kind of mapping name to make.
862
863 @retval NULL a memory allocation failed.
864 @return a new map name string
865 **/
866 CHAR16*
867 EFIAPI
868 ShellCommandCreateNewMappingName(
869 IN CONST SHELL_MAPPING_TYPE Type
870 )
871 {
872 CHAR16 *String;
873 ASSERT(Type < MappingTypeMax);
874
875 String = NULL;
876
877 String = AllocateZeroPool(PcdGet8(PcdShellMapNameLength) * sizeof(String[0]));
878 UnicodeSPrint(
879 String,
880 PcdGet8(PcdShellMapNameLength) * sizeof(String[0]),
881 Type == MappingTypeFileSystem?L"FS%d:":L"BLK%d:",
882 Type == MappingTypeFileSystem?mFsMaxCount++:mBlkMaxCount++);
883
884 return (String);
885 }
886
887 /**
888 Function to add a map node to the list of map items and update the "path" environment variable (optionally).
889
890 If Path is TRUE (during initialization only), the path environment variable will also be updated to include
891 default paths on the new map name...
892
893 Path should be FALSE when this function is called from the protocol SetMap function.
894
895 @param[in] Name The human readable mapped name.
896 @param[in] DevicePath The Device Path for this map.
897 @param[in] Flags The Flags attribute for this map item.
898 @param[in] Path TRUE to update path, FALSE to skip this step (should only be TRUE during initialization).
899
900 @retval EFI_SUCCESS The addition was sucessful.
901 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
902 @retval EFI_INVALID_PARAMETER A parameter was invalid.
903 **/
904 EFI_STATUS
905 EFIAPI
906 ShellCommandAddMapItemAndUpdatePath(
907 IN CONST CHAR16 *Name,
908 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath,
909 IN CONST UINT64 Flags,
910 IN CONST BOOLEAN Path
911 )
912 {
913 EFI_STATUS Status;
914 SHELL_MAP_LIST *MapListNode;
915 CONST CHAR16 *OriginalPath;
916 CHAR16 *NewPath;
917 UINTN NewPathSize;
918
919 NewPathSize = 0;
920 NewPath = NULL;
921 OriginalPath = NULL;
922 Status = EFI_SUCCESS;
923
924 MapListNode = AllocateZeroPool(sizeof(SHELL_MAP_LIST));
925 if (MapListNode == NULL) {
926 Status = EFI_OUT_OF_RESOURCES;
927 } else {
928 MapListNode->Flags = Flags;
929 MapListNode->MapName = AllocateZeroPool(StrSize(Name));
930 MapListNode->DevicePath = DuplicateDevicePath(DevicePath);
931 if ((MapListNode->MapName == NULL) || (MapListNode->DevicePath == NULL)){
932 Status = EFI_OUT_OF_RESOURCES;
933 } else {
934 StrCpy(MapListNode->MapName, Name);
935 InsertTailList(&gShellMapList.Link, &MapListNode->Link);
936 }
937 }
938 if (EFI_ERROR(Status)) {
939 if (MapListNode != NULL) {
940 if (MapListNode->DevicePath != NULL) {
941 FreePool(MapListNode->DevicePath);
942 }
943 if (MapListNode->MapName != NULL) {
944 FreePool(MapListNode->MapName);
945 }
946 FreePool(MapListNode);
947 }
948 } else if (Path) {
949 //
950 // Since there was no error and Path was TRUE
951 // Now add the correct path for that mapping
952 //
953 OriginalPath = gEfiShellProtocol->GetEnv(L"path");
954 ASSERT((NewPath == NULL && NewPathSize == 0) || (NewPath != NULL));
955 if (OriginalPath != NULL) {
956 StrnCatGrow(&NewPath, &NewPathSize, OriginalPath, 0);
957 } else {
958 StrnCatGrow(&NewPath, &NewPathSize, L".\\", 0);
959 }
960 StrnCatGrow(&NewPath, &NewPathSize, L";", 0);
961 StrnCatGrow(&NewPath, &NewPathSize, Name, 0);
962 StrnCatGrow(&NewPath, &NewPathSize, L"\\efi\\tools\\;", 0);
963 StrnCatGrow(&NewPath, &NewPathSize, Name, 0);
964 StrnCatGrow(&NewPath, &NewPathSize, L"\\efi\\boot\\;", 0);
965 StrnCatGrow(&NewPath, &NewPathSize, Name, 0);
966 StrnCatGrow(&NewPath, &NewPathSize, L"\\", 0);
967
968 Status = gEfiShellProtocol->SetEnv(L"path", NewPath, TRUE);
969 ASSERT_EFI_ERROR(Status);
970 FreePool(NewPath);
971 }
972 return (Status);
973 }
974
975 /**
976 Creates the default map names for each device path in the system with
977 a protocol depending on the Type.
978
979 Creates the consistent map names for each device path in the system with
980 a protocol depending on the Type.
981
982 Note: This will reset all mappings in the system("map -r").
983
984 Also sets up the default path environment variable if Type is FileSystem.
985
986 @retval EFI_SUCCESS All map names were created sucessfully.
987 @retval EFI_NOT_FOUND No protocols were found in the system.
988 @return Error returned from gBS->LocateHandle().
989
990 @sa LocateHandle
991 **/
992 EFI_STATUS
993 EFIAPI
994 ShellCommandCreateInitialMappingsAndPaths(
995 VOID
996 )
997 {
998 EFI_STATUS Status;
999 EFI_HANDLE *HandleList;
1000 UINTN Count;
1001 EFI_DEVICE_PATH_PROTOCOL **DevicePathList;
1002 CHAR16 *NewDefaultName;
1003 CHAR16 *NewConsistName;
1004 EFI_DEVICE_PATH_PROTOCOL **ConsistMappingTable;
1005 SHELL_MAP_LIST *MapListNode;
1006
1007 HandleList = NULL;
1008
1009 //
1010 // Reset the static members back to zero
1011 //
1012 mFsMaxCount = 0;
1013 mBlkMaxCount = 0;
1014
1015 gEfiShellProtocol->SetEnv(L"path", L"", TRUE);
1016
1017 //
1018 // First empty out the existing list.
1019 //
1020 if (!IsListEmpty(&gShellMapList.Link)) {
1021 for ( MapListNode = (SHELL_MAP_LIST *)GetFirstNode(&gShellMapList.Link)
1022 ; !IsListEmpty(&gShellMapList.Link)
1023 ; MapListNode = (SHELL_MAP_LIST *)GetFirstNode(&gShellMapList.Link)
1024 ){
1025 RemoveEntryList(&MapListNode->Link);
1026 FreePool(MapListNode);
1027 } // for loop
1028 }
1029
1030 //
1031 // Find each handle with Simple File System
1032 //
1033 HandleList = GetHandleListByProtocol(&gEfiSimpleFileSystemProtocolGuid);
1034 if (HandleList != NULL) {
1035 //
1036 // Do a count of the handles
1037 //
1038 for (Count = 0 ; HandleList[Count] != NULL ; Count++);
1039
1040 //
1041 // Get all Device Paths
1042 //
1043 DevicePathList = AllocatePool(sizeof(EFI_DEVICE_PATH_PROTOCOL*) * Count);
1044 ASSERT(DevicePathList != NULL);
1045
1046 for (Count = 0 ; HandleList[Count] != NULL ; Count++) {
1047 DevicePathList[Count] = DevicePathFromHandle(HandleList[Count]);
1048 }
1049
1050 //
1051 // Sort all DevicePaths
1052 //
1053 PerformQuickSort(DevicePathList, Count, sizeof(EFI_DEVICE_PATH_PROTOCOL*), DevicePathCompare);
1054
1055 ShellCommandConsistMappingInitialize(&ConsistMappingTable);
1056 //
1057 // Assign new Mappings to all...
1058 //
1059 for (Count = 0 ; HandleList[Count] != NULL ; Count++) {
1060 //
1061 // Get default name first
1062 //
1063 NewDefaultName = ShellCommandCreateNewMappingName(MappingTypeFileSystem);
1064 ASSERT(NewDefaultName != NULL);
1065 Status = ShellCommandAddMapItemAndUpdatePath(NewDefaultName, DevicePathList[Count], 0, TRUE);
1066 ASSERT_EFI_ERROR(Status);
1067 FreePool(NewDefaultName);
1068
1069 //
1070 // Now do consistent name
1071 //
1072 NewConsistName = ShellCommandConsistMappingGenMappingName(DevicePathList[Count], ConsistMappingTable);
1073 if (NewConsistName != NULL) {
1074 Status = ShellCommandAddMapItemAndUpdatePath(NewConsistName, DevicePathList[Count], 0, FALSE);
1075 ASSERT_EFI_ERROR(Status);
1076 FreePool(NewConsistName);
1077 }
1078 }
1079
1080 ShellCommandConsistMappingUnInitialize(ConsistMappingTable);
1081
1082 SHELL_FREE_NON_NULL(HandleList);
1083 SHELL_FREE_NON_NULL(DevicePathList);
1084
1085 HandleList = NULL;
1086 } else {
1087 Count = (UINTN)-1;
1088 }
1089
1090 //
1091 // Find each handle with Block Io
1092 //
1093 HandleList = GetHandleListByProtocol(&gEfiBlockIoProtocolGuid);
1094 if (HandleList != NULL) {
1095 for (Count = 0 ; HandleList[Count] != NULL ; Count++);
1096
1097 //
1098 // Get all Device Paths
1099 //
1100 DevicePathList = AllocatePool(sizeof(EFI_DEVICE_PATH_PROTOCOL*) * Count);
1101 ASSERT(DevicePathList != NULL);
1102
1103 for (Count = 0 ; HandleList[Count] != NULL ; Count++) {
1104 DevicePathList[Count] = DevicePathFromHandle(HandleList[Count]);
1105 }
1106
1107 //
1108 // Sort all DevicePaths
1109 //
1110 PerformQuickSort(DevicePathList, Count, sizeof(EFI_DEVICE_PATH_PROTOCOL*), DevicePathCompare);
1111
1112 //
1113 // Assign new Mappings to all...
1114 //
1115 for (Count = 0 ; HandleList[Count] != NULL ; Count++) {
1116 //
1117 // Get default name first
1118 //
1119 NewDefaultName = ShellCommandCreateNewMappingName(MappingTypeBlockIo);
1120 ASSERT(NewDefaultName != NULL);
1121 Status = ShellCommandAddMapItemAndUpdatePath(NewDefaultName, DevicePathList[Count], 0, FALSE);
1122 ASSERT_EFI_ERROR(Status);
1123 FreePool(NewDefaultName);
1124 }
1125
1126 SHELL_FREE_NON_NULL(HandleList);
1127 SHELL_FREE_NON_NULL(DevicePathList);
1128 } else if (Count == (UINTN)-1) {
1129 return (EFI_NOT_FOUND);
1130 }
1131
1132 return (EFI_SUCCESS);
1133 }
1134
1135 CHAR16*
1136 EFIAPI
1137 ShellCommandCleanPath (
1138 IN OUT CHAR16 *Path
1139 )
1140 {
1141 CHAR16 *Path2;
1142
1143 for (Path2 = Path ; Path2 != NULL && *Path2 != CHAR_NULL ; Path2++) {
1144 if (*Path2 == L'/') {
1145 *Path2 = L'\\';
1146 }
1147 }
1148
1149 return (Path);
1150 }
1151
1152 /**
1153 Converts a SHELL_FILE_HANDLE to an EFI_FILE_PROTOCOL*.
1154
1155 @param[in] Handle The SHELL_FILE_HANDLE to convert.
1156
1157 @return a EFI_FILE_PROTOCOL* representing the same file.
1158 **/
1159 EFI_FILE_PROTOCOL*
1160 EFIAPI
1161 ConvertShellHandleToEfiFileProtocol(
1162 IN CONST SHELL_FILE_HANDLE Handle
1163 )
1164 {
1165 return ((EFI_FILE_PROTOCOL*)(Handle));
1166 }
1167
1168 /**
1169 Converts a EFI_FILE_PROTOCOL* to an SHELL_FILE_HANDLE.
1170
1171 @param[in] Handle The pointer to EFI_FILE_PROTOCOL to convert.
1172 @param[in] Path The path to the file for verification.
1173
1174 @return A SHELL_FILE_HANDLE representing the same file.
1175 @retval NULL There was not enough memory.
1176 **/
1177 SHELL_FILE_HANDLE
1178 EFIAPI
1179 ConvertEfiFileProtocolToShellHandle(
1180 IN CONST EFI_FILE_PROTOCOL *Handle,
1181 IN CONST CHAR16 *Path
1182 )
1183 {
1184 SHELL_COMMAND_FILE_HANDLE *Buffer;
1185 BUFFER_LIST *NewNode;
1186
1187 if (Path != NULL) {
1188 Buffer = AllocateZeroPool(sizeof(SHELL_COMMAND_FILE_HANDLE));
1189 if (Buffer == NULL) {
1190 return (NULL);
1191 }
1192 NewNode = AllocatePool(sizeof(BUFFER_LIST));
1193 if (NewNode == NULL) {
1194 return (NULL);
1195 }
1196 Buffer->FileHandle = (EFI_FILE_PROTOCOL*)Handle;
1197 Buffer->Path = StrnCatGrow(&Buffer->Path, NULL, Path, 0);
1198 if (Buffer->Path == NULL) {
1199 return (NULL);
1200 }
1201 NewNode->Buffer = Buffer;
1202
1203 InsertHeadList(&mFileHandleList.Link, &NewNode->Link);
1204 }
1205 return ((SHELL_FILE_HANDLE)(Handle));
1206 }
1207
1208 /**
1209 Find the path that was logged with the specified SHELL_FILE_HANDLE.
1210
1211 @param[in] Handle The SHELL_FILE_HANDLE to query on.
1212
1213 @return A pointer to the path for the file.
1214 **/
1215 CONST CHAR16*
1216 EFIAPI
1217 ShellFileHandleGetPath(
1218 IN CONST SHELL_FILE_HANDLE Handle
1219 )
1220 {
1221 BUFFER_LIST *Node;
1222
1223 for (Node = (BUFFER_LIST*)GetFirstNode(&mFileHandleList.Link)
1224 ; !IsNull(&mFileHandleList.Link, &Node->Link)
1225 ; Node = (BUFFER_LIST*)GetNextNode(&mFileHandleList.Link, &Node->Link)
1226 ){
1227 if ((Node->Buffer) && (((SHELL_COMMAND_FILE_HANDLE *)Node->Buffer)->FileHandle == Handle)){
1228 return (((SHELL_COMMAND_FILE_HANDLE *)Node->Buffer)->Path);
1229 }
1230 }
1231 return (NULL);
1232 }
1233
1234 /**
1235 Remove a SHELL_FILE_HANDLE frmo the list of SHELL_FILE_HANDLES.
1236
1237 @param[in] Handle The SHELL_FILE_HANDLE to remove.
1238
1239 @retval TRUE The item was removed.
1240 @retval FALSE The item was not found.
1241 **/
1242 BOOLEAN
1243 EFIAPI
1244 ShellFileHandleRemove(
1245 IN CONST SHELL_FILE_HANDLE Handle
1246 )
1247 {
1248 BUFFER_LIST *Node;
1249
1250 for (Node = (BUFFER_LIST*)GetFirstNode(&mFileHandleList.Link)
1251 ; !IsNull(&mFileHandleList.Link, &Node->Link)
1252 ; Node = (BUFFER_LIST*)GetNextNode(&mFileHandleList.Link, &Node->Link)
1253 ){
1254 if ((Node->Buffer) && (((SHELL_COMMAND_FILE_HANDLE *)Node->Buffer)->FileHandle == Handle)){
1255 RemoveEntryList(&Node->Link);
1256 SHELL_FREE_NON_NULL(((SHELL_COMMAND_FILE_HANDLE *)Node->Buffer)->Path);
1257 SHELL_FREE_NON_NULL(Node->Buffer);
1258 SHELL_FREE_NON_NULL(Node);
1259 return (TRUE);
1260 }
1261 }
1262 return (FALSE);
1263 }
1264
1265 /**
1266 Function to determine if a SHELL_FILE_HANDLE is at the end of the file.
1267
1268 This will NOT work on directories.
1269
1270 If Handle is NULL, then ASSERT.
1271
1272 @param[in] Handle the file handle
1273
1274 @retval TRUE the position is at the end of the file
1275 @retval FALSE the position is not at the end of the file
1276 **/
1277 BOOLEAN
1278 EFIAPI
1279 ShellFileHandleEof(
1280 IN SHELL_FILE_HANDLE Handle
1281 )
1282 {
1283 EFI_FILE_INFO *Info;
1284 UINT64 Pos;
1285 BOOLEAN RetVal;
1286
1287 //
1288 // ASSERT if Handle is NULL
1289 //
1290 ASSERT(Handle != NULL);
1291
1292 gEfiShellProtocol->GetFilePosition(Handle, &Pos);
1293 Info = gEfiShellProtocol->GetFileInfo (Handle);
1294 ASSERT(Info != NULL);
1295 gEfiShellProtocol->SetFilePosition(Handle, Pos);
1296
1297 if (Info == NULL) {
1298 return (FALSE);
1299 }
1300
1301 if (Pos == Info->FileSize) {
1302 RetVal = TRUE;
1303 } else {
1304 RetVal = FALSE;
1305 }
1306
1307 FreePool (Info);
1308
1309 return (RetVal);
1310 }
1311
1312 /**
1313 Function to read a single line from a SHELL_FILE_HANDLE. The \n is not included in the returned
1314 buffer. The returned buffer must be callee freed.
1315
1316 If the position upon start is 0, then the Ascii Boolean will be set. This should be
1317 maintained and not changed for all operations with the same file.
1318
1319 @param[in] Handle SHELL_FILE_HANDLE to read from.
1320 @param[in,out] Ascii Boolean value for indicating whether the file is
1321 Ascii (TRUE) or UCS2 (FALSE).
1322
1323 @return The line of text from the file.
1324
1325 @sa ShellFileHandleReadLine
1326 **/
1327 CHAR16*
1328 EFIAPI
1329 ShellFileHandleReturnLine(
1330 IN SHELL_FILE_HANDLE Handle,
1331 IN OUT BOOLEAN *Ascii
1332 )
1333 {
1334 CHAR16 *RetVal;
1335 UINTN Size;
1336 EFI_STATUS Status;
1337
1338 Size = 0;
1339 RetVal = NULL;
1340
1341 Status = ShellFileHandleReadLine(Handle, RetVal, &Size, FALSE, Ascii);
1342 if (Status == EFI_BUFFER_TOO_SMALL) {
1343 RetVal = AllocatePool(Size);
1344 Status = ShellFileHandleReadLine(Handle, RetVal, &Size, FALSE, Ascii);
1345 }
1346 ASSERT_EFI_ERROR(Status);
1347 if (EFI_ERROR(Status) && (RetVal != NULL)) {
1348 FreePool(RetVal);
1349 RetVal = NULL;
1350 }
1351 return (RetVal);
1352 }
1353
1354 /**
1355 Function to read a single line (up to but not including the \n) from a SHELL_FILE_HANDLE.
1356
1357 If the position upon start is 0, then the Ascii Boolean will be set. This should be
1358 maintained and not changed for all operations with the same file.
1359
1360 @param[in] Handle SHELL_FILE_HANDLE to read from.
1361 @param[in,out] Buffer The pointer to buffer to read into.
1362 @param[in,out] Size The pointer to number of bytes in Buffer.
1363 @param[in] Truncate If the buffer is large enough, this has no effect.
1364 If the buffer is is too small and Truncate is TRUE,
1365 the line will be truncated.
1366 If the buffer is is too small and Truncate is FALSE,
1367 then no read will occur.
1368
1369 @param[in,out] Ascii Boolean value for indicating whether the file is
1370 Ascii (TRUE) or UCS2 (FALSE).
1371
1372 @retval EFI_SUCCESS The operation was successful. The line is stored in
1373 Buffer.
1374 @retval EFI_INVALID_PARAMETER Handle was NULL.
1375 @retval EFI_INVALID_PARAMETER Size was NULL.
1376 @retval EFI_BUFFER_TOO_SMALL Size was not large enough to store the line.
1377 Size was updated to the minimum space required.
1378 @sa ShellFileHandleRead
1379 **/
1380 EFI_STATUS
1381 EFIAPI
1382 ShellFileHandleReadLine(
1383 IN SHELL_FILE_HANDLE Handle,
1384 IN OUT CHAR16 *Buffer,
1385 IN OUT UINTN *Size,
1386 IN BOOLEAN Truncate,
1387 IN OUT BOOLEAN *Ascii
1388 )
1389 {
1390 EFI_STATUS Status;
1391 CHAR16 CharBuffer;
1392 UINTN CharSize;
1393 UINTN CountSoFar;
1394 UINT64 OriginalFilePosition;
1395
1396
1397 if (Handle == NULL
1398 ||Size == NULL
1399 ){
1400 return (EFI_INVALID_PARAMETER);
1401 }
1402 if (Buffer == NULL) {
1403 ASSERT(*Size == 0);
1404 } else {
1405 *Buffer = CHAR_NULL;
1406 }
1407 gEfiShellProtocol->GetFilePosition(Handle, &OriginalFilePosition);
1408 if (OriginalFilePosition == 0) {
1409 CharSize = sizeof(CHAR16);
1410 Status = gEfiShellProtocol->ReadFile(Handle, &CharSize, &CharBuffer);
1411 ASSERT_EFI_ERROR(Status);
1412 if (CharBuffer == UnicodeFileTag) {
1413 *Ascii = FALSE;
1414 } else {
1415 *Ascii = TRUE;
1416 gEfiShellProtocol->SetFilePosition(Handle, OriginalFilePosition);
1417 }
1418 }
1419
1420 for (CountSoFar = 0;;CountSoFar++){
1421 CharBuffer = 0;
1422 if (*Ascii) {
1423 CharSize = sizeof(CHAR8);
1424 } else {
1425 CharSize = sizeof(CHAR16);
1426 }
1427 Status = gEfiShellProtocol->ReadFile(Handle, &CharSize, &CharBuffer);
1428 if ( EFI_ERROR(Status)
1429 || CharSize == 0
1430 || (CharBuffer == L'\n' && !(*Ascii))
1431 || (CharBuffer == '\n' && *Ascii)
1432 ){
1433 break;
1434 }
1435 //
1436 // if we have space save it...
1437 //
1438 if ((CountSoFar+1)*sizeof(CHAR16) < *Size){
1439 ASSERT(Buffer != NULL);
1440 ((CHAR16*)Buffer)[CountSoFar] = CharBuffer;
1441 ((CHAR16*)Buffer)[CountSoFar+1] = CHAR_NULL;
1442 }
1443 }
1444
1445 //
1446 // if we ran out of space tell when...
1447 //
1448 if ((CountSoFar+1)*sizeof(CHAR16) > *Size){
1449 *Size = (CountSoFar+1)*sizeof(CHAR16);
1450 if (!Truncate) {
1451 gEfiShellProtocol->SetFilePosition(Handle, OriginalFilePosition);
1452 } else {
1453 DEBUG((DEBUG_WARN, "The line was truncated in ShellFileHandleReadLine"));
1454 }
1455 return (EFI_BUFFER_TOO_SMALL);
1456 }
1457 while(Buffer[StrLen(Buffer)-1] == L'\r') {
1458 Buffer[StrLen(Buffer)-1] = CHAR_NULL;
1459 }
1460
1461 return (Status);
1462 }
1463 /**
1464 Frees any BUFFER_LIST defined type.
1465 **/
1466 VOID
1467 EFIAPI
1468 FreeBufferList (
1469 IN BUFFER_LIST *List
1470 )
1471 {
1472 BUFFER_LIST *BufferListEntry;
1473
1474 if (List == NULL){
1475 return;
1476 }
1477 //
1478 // enumerate through the buffer list and free all memory
1479 //
1480 for ( BufferListEntry = ( BUFFER_LIST *)GetFirstNode(&List->Link)
1481 ; !IsListEmpty (&List->Link)
1482 ; BufferListEntry = (BUFFER_LIST *)GetFirstNode(&List->Link)
1483 ){
1484 RemoveEntryList(&BufferListEntry->Link);
1485 ASSERT(BufferListEntry->Buffer != NULL);
1486 if (BufferListEntry->Buffer != NULL) {
1487 FreePool(BufferListEntry->Buffer);
1488 }
1489 FreePool(BufferListEntry);
1490 }
1491 }
1492
1493 /**
1494 Chops off last directory or file entry in a path leaving the trailing slash
1495
1496 @param[in,out] Path
1497
1498 @retval FALSE No directory was found to chop off.
1499 @retval TRUE A directory was chopped off.
1500 **/
1501 BOOLEAN
1502 EFIAPI
1503 ChopLastSlash(
1504 IN OUT CHAR16 *PathToReturn
1505 )
1506 {
1507 CHAR16 *Walker;
1508 CHAR16 *LastSlash = NULL;
1509 //
1510 // get directory name from path... ('chop' off extra)
1511 //
1512 for ( Walker = PathToReturn
1513 ; Walker != NULL && *Walker != CHAR_NULL
1514 ; Walker++
1515 ){
1516 if (*Walker == L'\\' && *(Walker + 1) != CHAR_NULL) {
1517 LastSlash = Walker+1;
1518 }
1519 }
1520 if (LastSlash != NULL) {
1521 *LastSlash = CHAR_NULL;
1522 return (TRUE);
1523 }
1524 return (FALSE);
1525 }
1526