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