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