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