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