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