]> git.proxmox.com Git - mirror_edk2.git/blame - ShellPkg/Library/UefiShellCommandLib/UefiShellCommandLib.c
update error handling to use less ASSERT.
[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
0ab85bef 770\r
771 if (Script == NULL) {\r
772 return;\r
773 }\r
774\r
a405b86d 775 for (LoopVar = 0 ; LoopVar < Script->Argc ; LoopVar++) {\r
0ab85bef 776 SHELL_FREE_NON_NULL(Script->Argv[LoopVar]);\r
a405b86d 777 }\r
778 if (Script->Argv != NULL) {\r
0ab85bef 779 SHELL_FREE_NON_NULL(Script->Argv);\r
a405b86d 780 }\r
781 Script->CurrentCommand = NULL;\r
782 while (!IsListEmpty (&Script->CommandList)) {\r
783 Script->CurrentCommand = (SCRIPT_COMMAND_LIST *)GetFirstNode(&Script->CommandList);\r
784 if (Script->CurrentCommand != NULL) {\r
785 RemoveEntryList(&Script->CurrentCommand->Link);\r
786 if (Script->CurrentCommand->Cl != NULL) {\r
0ab85bef 787 SHELL_FREE_NON_NULL(Script->CurrentCommand->Cl);\r
a405b86d 788 }\r
789 if (Script->CurrentCommand->Data != NULL) {\r
0ab85bef 790 SHELL_FREE_NON_NULL(Script->CurrentCommand->Data);\r
a405b86d 791 }\r
0ab85bef 792 SHELL_FREE_NON_NULL(Script->CurrentCommand);\r
a405b86d 793 }\r
794 }\r
0ab85bef 795 SHELL_FREE_NON_NULL(Script->ScriptName);\r
796 SHELL_FREE_NON_NULL(Script);\r
a405b86d 797}\r
798\r
799/**\r
800 Function to return a pointer to the currently running script file object.\r
801\r
802 @retval NULL A script file is not currently running.\r
803 @return A pointer to the current script file object.\r
804**/\r
805SCRIPT_FILE*\r
806EFIAPI\r
807ShellCommandGetCurrentScriptFile (\r
808 VOID\r
809 )\r
810{\r
811 SCRIPT_FILE_LIST *List;\r
812 if (IsListEmpty (&mScriptList.Link)) {\r
813 return (NULL);\r
814 }\r
815 List = ((SCRIPT_FILE_LIST*)GetFirstNode(&mScriptList.Link));\r
816 return (List->Data);\r
817}\r
818\r
819/**\r
820 Function to set a new script as the currently running one.\r
821\r
822 This function will correctly stack and unstack nested scripts.\r
823\r
824 @param[in] Script Pointer to new script information structure. if NULL\r
825 will remove and de-allocate the top-most Script structure.\r
826\r
827 @return A pointer to the current running script file after this\r
828 change. NULL if removing the final script.\r
829**/\r
830SCRIPT_FILE*\r
831EFIAPI\r
832ShellCommandSetNewScript (\r
833 IN SCRIPT_FILE *Script OPTIONAL\r
834 )\r
835{\r
836 SCRIPT_FILE_LIST *Node;\r
837 if (Script == NULL) {\r
838 if (IsListEmpty (&mScriptList.Link)) {\r
a405b86d 839 return (NULL);\r
840 }\r
841 Node = (SCRIPT_FILE_LIST *)GetFirstNode(&mScriptList.Link);\r
842 RemoveEntryList(&Node->Link);\r
843 DeleteScriptFileStruct(Node->Data);\r
844 FreePool(Node);\r
845 } else {\r
846 Node = AllocateZeroPool(sizeof(SCRIPT_FILE_LIST));\r
0ab85bef 847 if (Node == NULL) {\r
848 return (NULL);\r
849 }\r
a405b86d 850 Node->Data = Script;\r
851 InsertHeadList(&mScriptList.Link, &Node->Link);\r
852 }\r
853 return (ShellCommandGetCurrentScriptFile());\r
854}\r
855\r
856/**\r
857 Function to generate the next default mapping name.\r
858\r
859 If the return value is not NULL then it must be callee freed.\r
860\r
861 @param Type What kind of mapping name to make.\r
862\r
863 @retval NULL a memory allocation failed.\r
864 @return a new map name string\r
865**/\r
866CHAR16*\r
867EFIAPI\r
868ShellCommandCreateNewMappingName(\r
869 IN CONST SHELL_MAPPING_TYPE Type\r
870 )\r
871{\r
872 CHAR16 *String;\r
873 ASSERT(Type < MappingTypeMax);\r
874\r
875 String = NULL;\r
876\r
877 String = AllocateZeroPool(PcdGet8(PcdShellMapNameLength) * sizeof(String[0]));\r
878 UnicodeSPrint(\r
879 String,\r
880 PcdGet8(PcdShellMapNameLength) * sizeof(String[0]),\r
881 Type == MappingTypeFileSystem?L"FS%d:":L"BLK%d:",\r
882 Type == MappingTypeFileSystem?mFsMaxCount++:mBlkMaxCount++);\r
883\r
884 return (String);\r
885}\r
886\r
887/**\r
888 Function to add a map node to the list of map items and update the "path" environment variable (optionally).\r
889\r
890 If Path is TRUE (during initialization only), the path environment variable will also be updated to include\r
891 default paths on the new map name...\r
892\r
893 Path should be FALSE when this function is called from the protocol SetMap function.\r
894\r
895 @param[in] Name The human readable mapped name.\r
896 @param[in] DevicePath The Device Path for this map.\r
897 @param[in] Flags The Flags attribute for this map item.\r
898 @param[in] Path TRUE to update path, FALSE to skip this step (should only be TRUE during initialization).\r
899\r
900 @retval EFI_SUCCESS The addition was sucessful.\r
901 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.\r
902 @retval EFI_INVALID_PARAMETER A parameter was invalid.\r
903**/\r
904EFI_STATUS\r
905EFIAPI\r
906ShellCommandAddMapItemAndUpdatePath(\r
907 IN CONST CHAR16 *Name,\r
908 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath,\r
909 IN CONST UINT64 Flags,\r
910 IN CONST BOOLEAN Path\r
911 )\r
912{\r
913 EFI_STATUS Status;\r
914 SHELL_MAP_LIST *MapListNode;\r
915 CONST CHAR16 *OriginalPath;\r
916 CHAR16 *NewPath;\r
917 UINTN NewPathSize;\r
918\r
919 NewPathSize = 0;\r
920 NewPath = NULL;\r
921 OriginalPath = NULL;\r
922 Status = EFI_SUCCESS;\r
923\r
924 MapListNode = AllocateZeroPool(sizeof(SHELL_MAP_LIST));\r
925 if (MapListNode == NULL) {\r
926 Status = EFI_OUT_OF_RESOURCES;\r
927 } else {\r
928 MapListNode->Flags = Flags;\r
929 MapListNode->MapName = AllocateZeroPool(StrSize(Name));\r
930 MapListNode->DevicePath = DuplicateDevicePath(DevicePath);\r
931 if ((MapListNode->MapName == NULL) || (MapListNode->DevicePath == NULL)){\r
932 Status = EFI_OUT_OF_RESOURCES;\r
933 } else {\r
934 StrCpy(MapListNode->MapName, Name);\r
935 InsertTailList(&gShellMapList.Link, &MapListNode->Link);\r
936 }\r
937 }\r
938 if (EFI_ERROR(Status)) {\r
939 if (MapListNode != NULL) {\r
940 if (MapListNode->DevicePath != NULL) {\r
941 FreePool(MapListNode->DevicePath);\r
942 }\r
943 if (MapListNode->MapName != NULL) {\r
944 FreePool(MapListNode->MapName);\r
945 }\r
946 FreePool(MapListNode);\r
947 }\r
948 } else if (Path) {\r
949 //\r
950 // Since there was no error and Path was TRUE\r
951 // Now add the correct path for that mapping\r
952 //\r
953 OriginalPath = gEfiShellProtocol->GetEnv(L"path");\r
954 ASSERT((NewPath == NULL && NewPathSize == 0) || (NewPath != NULL));\r
955 if (OriginalPath != NULL) {\r
956 StrnCatGrow(&NewPath, &NewPathSize, OriginalPath, 0);\r
957 } else {\r
958 StrnCatGrow(&NewPath, &NewPathSize, L".\\", 0);\r
959 }\r
960 StrnCatGrow(&NewPath, &NewPathSize, L";", 0);\r
961 StrnCatGrow(&NewPath, &NewPathSize, Name, 0);\r
962 StrnCatGrow(&NewPath, &NewPathSize, L"\\efi\\tools\\;", 0);\r
963 StrnCatGrow(&NewPath, &NewPathSize, Name, 0);\r
964 StrnCatGrow(&NewPath, &NewPathSize, L"\\efi\\boot\\;", 0);\r
965 StrnCatGrow(&NewPath, &NewPathSize, Name, 0);\r
966 StrnCatGrow(&NewPath, &NewPathSize, L"\\", 0);\r
967\r
968 Status = gEfiShellProtocol->SetEnv(L"path", NewPath, TRUE);\r
969 ASSERT_EFI_ERROR(Status);\r
970 FreePool(NewPath);\r
971 }\r
972 return (Status);\r
973}\r
974\r
975/**\r
976 Creates the default map names for each device path in the system with\r
977 a protocol depending on the Type.\r
978\r
979 Creates the consistent map names for each device path in the system with\r
980 a protocol depending on the Type.\r
981\r
982 Note: This will reset all mappings in the system("map -r").\r
983\r
984 Also sets up the default path environment variable if Type is FileSystem.\r
985\r
986 @retval EFI_SUCCESS All map names were created sucessfully.\r
987 @retval EFI_NOT_FOUND No protocols were found in the system.\r
988 @return Error returned from gBS->LocateHandle().\r
989\r
990 @sa LocateHandle\r
991**/\r
992EFI_STATUS\r
993EFIAPI\r
994ShellCommandCreateInitialMappingsAndPaths(\r
995 VOID\r
996 )\r
997{\r
998 EFI_STATUS Status;\r
999 EFI_HANDLE *HandleList;\r
1000 UINTN Count;\r
1001 EFI_DEVICE_PATH_PROTOCOL **DevicePathList;\r
1002 CHAR16 *NewDefaultName;\r
1003 CHAR16 *NewConsistName;\r
1004 EFI_DEVICE_PATH_PROTOCOL **ConsistMappingTable;\r
1005 SHELL_MAP_LIST *MapListNode;\r
1006\r
1007 HandleList = NULL;\r
1008\r
1009 //\r
1010 // Reset the static members back to zero\r
1011 //\r
1012 mFsMaxCount = 0;\r
1013 mBlkMaxCount = 0;\r
1014\r
1015 gEfiShellProtocol->SetEnv(L"path", L"", TRUE);\r
1016\r
1017 //\r
1018 // First empty out the existing list.\r
1019 //\r
1020 if (!IsListEmpty(&gShellMapList.Link)) {\r
1021 for ( MapListNode = (SHELL_MAP_LIST *)GetFirstNode(&gShellMapList.Link)\r
1022 ; !IsListEmpty(&gShellMapList.Link)\r
1023 ; MapListNode = (SHELL_MAP_LIST *)GetFirstNode(&gShellMapList.Link)\r
1024 ){\r
1025 RemoveEntryList(&MapListNode->Link);\r
1026 FreePool(MapListNode);\r
1027 } // for loop\r
1028 }\r
1029\r
1030 //\r
1031 // Find each handle with Simple File System\r
1032 //\r
0ab85bef 1033 HandleList = GetHandleListByProtocol(&gEfiSimpleFileSystemProtocolGuid);\r
a405b86d 1034 if (HandleList != NULL) {\r
1035 //\r
1036 // Do a count of the handles\r
1037 //\r
1038 for (Count = 0 ; HandleList[Count] != NULL ; Count++);\r
1039\r
1040 //\r
1041 // Get all Device Paths\r
1042 //\r
1043 DevicePathList = AllocatePool(sizeof(EFI_DEVICE_PATH_PROTOCOL*) * Count);\r
1044 ASSERT(DevicePathList != NULL);\r
1045\r
1046 for (Count = 0 ; HandleList[Count] != NULL ; Count++) {\r
1047 DevicePathList[Count] = DevicePathFromHandle(HandleList[Count]);\r
1048 }\r
1049\r
1050 //\r
1051 // Sort all DevicePaths\r
1052 //\r
1053 PerformQuickSort(DevicePathList, Count, sizeof(EFI_DEVICE_PATH_PROTOCOL*), DevicePathCompare);\r
1054\r
1055 ShellCommandConsistMappingInitialize(&ConsistMappingTable);\r
1056 //\r
1057 // Assign new Mappings to all...\r
1058 //\r
1059 for (Count = 0 ; HandleList[Count] != NULL ; Count++) {\r
1060 //\r
1061 // Get default name first\r
1062 //\r
1063 NewDefaultName = ShellCommandCreateNewMappingName(MappingTypeFileSystem);\r
1064 ASSERT(NewDefaultName != NULL);\r
1065 Status = ShellCommandAddMapItemAndUpdatePath(NewDefaultName, DevicePathList[Count], 0, TRUE);\r
1066 ASSERT_EFI_ERROR(Status);\r
1067 FreePool(NewDefaultName);\r
1068\r
1069 //\r
1070 // Now do consistent name\r
1071 //\r
1072 NewConsistName = ShellCommandConsistMappingGenMappingName(DevicePathList[Count], ConsistMappingTable);\r
1073 if (NewConsistName != NULL) {\r
1074 Status = ShellCommandAddMapItemAndUpdatePath(NewConsistName, DevicePathList[Count], 0, FALSE);\r
1075 ASSERT_EFI_ERROR(Status);\r
1076 FreePool(NewConsistName);\r
1077 }\r
1078 }\r
1079\r
1080 ShellCommandConsistMappingUnInitialize(ConsistMappingTable);\r
1081\r
1082 SHELL_FREE_NON_NULL(HandleList);\r
1083 SHELL_FREE_NON_NULL(DevicePathList);\r
1084\r
1085 HandleList = NULL;\r
1086 } else {\r
1087 Count = (UINTN)-1;\r
1088 }\r
1089\r
1090 //\r
1091 // Find each handle with Block Io\r
1092 //\r
0ab85bef 1093 HandleList = GetHandleListByProtocol(&gEfiBlockIoProtocolGuid);\r
a405b86d 1094 if (HandleList != NULL) {\r
1095 for (Count = 0 ; HandleList[Count] != NULL ; Count++);\r
1096\r
1097 //\r
1098 // Get all Device Paths\r
1099 //\r
1100 DevicePathList = AllocatePool(sizeof(EFI_DEVICE_PATH_PROTOCOL*) * Count);\r
1101 ASSERT(DevicePathList != NULL);\r
1102\r
1103 for (Count = 0 ; HandleList[Count] != NULL ; Count++) {\r
1104 DevicePathList[Count] = DevicePathFromHandle(HandleList[Count]);\r
1105 }\r
1106\r
1107 //\r
1108 // Sort all DevicePaths\r
1109 //\r
1110 PerformQuickSort(DevicePathList, Count, sizeof(EFI_DEVICE_PATH_PROTOCOL*), DevicePathCompare);\r
1111\r
1112 //\r
1113 // Assign new Mappings to all...\r
1114 //\r
1115 for (Count = 0 ; HandleList[Count] != NULL ; Count++) {\r
1116 //\r
1117 // Get default name first\r
1118 //\r
1119 NewDefaultName = ShellCommandCreateNewMappingName(MappingTypeBlockIo);\r
1120 ASSERT(NewDefaultName != NULL);\r
1121 Status = ShellCommandAddMapItemAndUpdatePath(NewDefaultName, DevicePathList[Count], 0, FALSE);\r
1122 ASSERT_EFI_ERROR(Status);\r
1123 FreePool(NewDefaultName);\r
1124 }\r
1125\r
1126 SHELL_FREE_NON_NULL(HandleList);\r
1127 SHELL_FREE_NON_NULL(DevicePathList);\r
1128 } else if (Count == (UINTN)-1) {\r
1129 return (EFI_NOT_FOUND);\r
1130 }\r
1131\r
1132 return (EFI_SUCCESS);\r
1133}\r
1134\r
1135CHAR16*\r
1136EFIAPI\r
1137ShellCommandCleanPath (\r
1138 IN OUT CHAR16 *Path\r
1139 )\r
1140{\r
1141 CHAR16 *Path2;\r
1142\r
1143 for (Path2 = Path ; Path2 != NULL && *Path2 != CHAR_NULL ; Path2++) {\r
1144 if (*Path2 == L'/') {\r
1145 *Path2 = L'\\';\r
1146 }\r
1147 }\r
1148\r
1149 return (Path);\r
1150}\r
1151\r
1152/**\r
1153 Converts a SHELL_FILE_HANDLE to an EFI_FILE_PROTOCOL*.\r
1154\r
1155 @param[in] Handle The SHELL_FILE_HANDLE to convert.\r
1156\r
1157 @return a EFI_FILE_PROTOCOL* representing the same file.\r
1158**/\r
1159EFI_FILE_PROTOCOL*\r
1160EFIAPI\r
1161ConvertShellHandleToEfiFileProtocol(\r
1162 IN CONST SHELL_FILE_HANDLE Handle\r
1163 )\r
1164{\r
1165 return ((EFI_FILE_PROTOCOL*)(Handle));\r
1166}\r
1167\r
1168/**\r
1169 Converts a EFI_FILE_PROTOCOL* to an SHELL_FILE_HANDLE.\r
1170\r
1171 @param[in] Handle The pointer to EFI_FILE_PROTOCOL to convert.\r
1172 @param[in] Path The path to the file for verification.\r
1173\r
ff51746b 1174 @return A SHELL_FILE_HANDLE representing the same file.\r
1175 @retval NULL There was not enough memory.\r
a405b86d 1176**/\r
1177SHELL_FILE_HANDLE\r
1178EFIAPI\r
1179ConvertEfiFileProtocolToShellHandle(\r
1180 IN CONST EFI_FILE_PROTOCOL *Handle,\r
1181 IN CONST CHAR16 *Path\r
1182 )\r
1183{\r
1184 SHELL_COMMAND_FILE_HANDLE *Buffer;\r
1185 BUFFER_LIST *NewNode;\r
1186\r
1187 if (Path != NULL) {\r
1188 Buffer = AllocateZeroPool(sizeof(SHELL_COMMAND_FILE_HANDLE));\r
ff51746b 1189 if (Buffer == NULL) {\r
1190 return (NULL);\r
1191 }\r
a405b86d 1192 NewNode = AllocatePool(sizeof(BUFFER_LIST));\r
ff51746b 1193 if (NewNode == NULL) {\r
1194 return (NULL);\r
1195 }\r
a405b86d 1196 Buffer->FileHandle = (EFI_FILE_PROTOCOL*)Handle;\r
1197 Buffer->Path = StrnCatGrow(&Buffer->Path, NULL, Path, 0);\r
ff51746b 1198 if (Buffer->Path == NULL) {\r
1199 return (NULL);\r
1200 }\r
a405b86d 1201 NewNode->Buffer = Buffer;\r
1202\r
1203 InsertHeadList(&mFileHandleList.Link, &NewNode->Link);\r
1204 }\r
1205 return ((SHELL_FILE_HANDLE)(Handle));\r
1206}\r
1207\r
1208/**\r
1209 Find the path that was logged with the specified SHELL_FILE_HANDLE.\r
1210\r
1211 @param[in] Handle The SHELL_FILE_HANDLE to query on.\r
1212\r
1213 @return A pointer to the path for the file.\r
1214**/\r
1215CONST CHAR16*\r
1216EFIAPI\r
1217ShellFileHandleGetPath(\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
1228 return (((SHELL_COMMAND_FILE_HANDLE *)Node->Buffer)->Path);\r
1229 }\r
1230 }\r
1231 return (NULL);\r
1232}\r
1233\r
1234/**\r
1235 Remove a SHELL_FILE_HANDLE frmo the list of SHELL_FILE_HANDLES.\r
1236\r
1237 @param[in] Handle The SHELL_FILE_HANDLE to remove.\r
1238\r
1239 @retval TRUE The item was removed.\r
1240 @retval FALSE The item was not found.\r
1241**/\r
1242BOOLEAN\r
1243EFIAPI\r
1244ShellFileHandleRemove(\r
1245 IN CONST SHELL_FILE_HANDLE Handle\r
1246 )\r
1247{\r
1248 BUFFER_LIST *Node;\r
1249\r
1250 for (Node = (BUFFER_LIST*)GetFirstNode(&mFileHandleList.Link)\r
1251 ; !IsNull(&mFileHandleList.Link, &Node->Link)\r
1252 ; Node = (BUFFER_LIST*)GetNextNode(&mFileHandleList.Link, &Node->Link)\r
1253 ){\r
1254 if ((Node->Buffer) && (((SHELL_COMMAND_FILE_HANDLE *)Node->Buffer)->FileHandle == Handle)){\r
a405b86d 1255 RemoveEntryList(&Node->Link);\r
ff51746b 1256 SHELL_FREE_NON_NULL(((SHELL_COMMAND_FILE_HANDLE *)Node->Buffer)->Path);\r
1257 SHELL_FREE_NON_NULL(Node->Buffer);\r
1258 SHELL_FREE_NON_NULL(Node);\r
a405b86d 1259 return (TRUE);\r
1260 }\r
1261 }\r
1262 return (FALSE);\r
1263}\r
1264\r
1265/**\r
1266 Function to determine if a SHELL_FILE_HANDLE is at the end of the file.\r
1267\r
1268 This will NOT work on directories.\r
1269\r
1270 If Handle is NULL, then ASSERT.\r
1271\r
1272 @param[in] Handle the file handle\r
1273\r
1274 @retval TRUE the position is at the end of the file\r
1275 @retval FALSE the position is not at the end of the file\r
1276**/\r
1277BOOLEAN\r
1278EFIAPI\r
1279ShellFileHandleEof(\r
1280 IN SHELL_FILE_HANDLE Handle\r
1281 )\r
1282{\r
1283 EFI_FILE_INFO *Info;\r
1284 UINT64 Pos;\r
1285 BOOLEAN RetVal;\r
1286\r
1287 //\r
1288 // ASSERT if Handle is NULL\r
1289 //\r
1290 ASSERT(Handle != NULL);\r
1291\r
1292 gEfiShellProtocol->GetFilePosition(Handle, &Pos);\r
1293 Info = gEfiShellProtocol->GetFileInfo (Handle);\r
1294 ASSERT(Info != NULL);\r
1295 gEfiShellProtocol->SetFilePosition(Handle, Pos);\r
1296\r
1297 if (Info == NULL) {\r
1298 return (FALSE);\r
1299 }\r
1300\r
1301 if (Pos == Info->FileSize) {\r
1302 RetVal = TRUE;\r
1303 } else {\r
1304 RetVal = FALSE;\r
1305 }\r
1306\r
1307 FreePool (Info);\r
1308\r
1309 return (RetVal);\r
1310}\r
1311\r
1312/**\r
1313 Function to read a single line from a SHELL_FILE_HANDLE. The \n is not included in the returned\r
1314 buffer. The returned buffer must be callee freed.\r
1315\r
1316 If the position upon start is 0, then the Ascii Boolean will be set. This should be\r
1317 maintained and not changed for all operations with the same file.\r
1318\r
1319 @param[in] Handle SHELL_FILE_HANDLE to read from.\r
1320 @param[in,out] Ascii Boolean value for indicating whether the file is\r
1321 Ascii (TRUE) or UCS2 (FALSE).\r
1322\r
1323 @return The line of text from the file.\r
1324\r
1325 @sa ShellFileHandleReadLine\r
1326**/\r
1327CHAR16*\r
1328EFIAPI\r
1329ShellFileHandleReturnLine(\r
1330 IN SHELL_FILE_HANDLE Handle,\r
1331 IN OUT BOOLEAN *Ascii\r
1332 )\r
1333{\r
1334 CHAR16 *RetVal;\r
1335 UINTN Size;\r
1336 EFI_STATUS Status;\r
1337\r
1338 Size = 0;\r
1339 RetVal = NULL;\r
1340\r
1341 Status = ShellFileHandleReadLine(Handle, RetVal, &Size, FALSE, Ascii);\r
1342 if (Status == EFI_BUFFER_TOO_SMALL) {\r
1343 RetVal = AllocatePool(Size);\r
1344 Status = ShellFileHandleReadLine(Handle, RetVal, &Size, FALSE, Ascii);\r
1345 }\r
1346 ASSERT_EFI_ERROR(Status);\r
1347 if (EFI_ERROR(Status) && (RetVal != NULL)) {\r
1348 FreePool(RetVal);\r
1349 RetVal = NULL;\r
1350 }\r
1351 return (RetVal);\r
1352}\r
1353\r
1354/**\r
1355 Function to read a single line (up to but not including the \n) from a SHELL_FILE_HANDLE.\r
1356\r
1357 If the position upon start is 0, then the Ascii Boolean will be set. This should be\r
1358 maintained and not changed for all operations with the same file.\r
1359\r
1360 @param[in] Handle SHELL_FILE_HANDLE to read from.\r
1361 @param[in,out] Buffer The pointer to buffer to read into.\r
1362 @param[in,out] Size The pointer to number of bytes in Buffer.\r
1363 @param[in] Truncate If the buffer is large enough, this has no effect.\r
1364 If the buffer is is too small and Truncate is TRUE,\r
1365 the line will be truncated.\r
1366 If the buffer is is too small and Truncate is FALSE,\r
1367 then no read will occur.\r
1368\r
1369 @param[in,out] Ascii Boolean value for indicating whether the file is\r
1370 Ascii (TRUE) or UCS2 (FALSE).\r
1371\r
1372 @retval EFI_SUCCESS The operation was successful. The line is stored in\r
1373 Buffer.\r
1374 @retval EFI_INVALID_PARAMETER Handle was NULL.\r
1375 @retval EFI_INVALID_PARAMETER Size was NULL.\r
1376 @retval EFI_BUFFER_TOO_SMALL Size was not large enough to store the line.\r
1377 Size was updated to the minimum space required.\r
1378 @sa ShellFileHandleRead\r
1379**/\r
1380EFI_STATUS\r
1381EFIAPI\r
1382ShellFileHandleReadLine(\r
1383 IN SHELL_FILE_HANDLE Handle,\r
1384 IN OUT CHAR16 *Buffer,\r
1385 IN OUT UINTN *Size,\r
1386 IN BOOLEAN Truncate,\r
1387 IN OUT BOOLEAN *Ascii\r
1388 )\r
1389{\r
1390 EFI_STATUS Status;\r
1391 CHAR16 CharBuffer;\r
1392 UINTN CharSize;\r
1393 UINTN CountSoFar;\r
1394 UINT64 OriginalFilePosition;\r
1395\r
1396\r
1397 if (Handle == NULL\r
1398 ||Size == NULL\r
1399 ){\r
1400 return (EFI_INVALID_PARAMETER);\r
1401 }\r
1402 if (Buffer == NULL) {\r
1403 ASSERT(*Size == 0);\r
1404 } else {\r
1405 *Buffer = CHAR_NULL;\r
1406 }\r
1407 gEfiShellProtocol->GetFilePosition(Handle, &OriginalFilePosition);\r
1408 if (OriginalFilePosition == 0) {\r
1409 CharSize = sizeof(CHAR16);\r
1410 Status = gEfiShellProtocol->ReadFile(Handle, &CharSize, &CharBuffer);\r
1411 ASSERT_EFI_ERROR(Status);\r
1412 if (CharBuffer == UnicodeFileTag) {\r
1413 *Ascii = FALSE;\r
1414 } else {\r
1415 *Ascii = TRUE;\r
1416 gEfiShellProtocol->SetFilePosition(Handle, OriginalFilePosition);\r
1417 }\r
1418 }\r
1419\r
1420 for (CountSoFar = 0;;CountSoFar++){\r
1421 CharBuffer = 0;\r
1422 if (*Ascii) {\r
1423 CharSize = sizeof(CHAR8);\r
1424 } else {\r
1425 CharSize = sizeof(CHAR16);\r
1426 }\r
1427 Status = gEfiShellProtocol->ReadFile(Handle, &CharSize, &CharBuffer);\r
1428 if ( EFI_ERROR(Status)\r
1429 || CharSize == 0\r
1430 || (CharBuffer == L'\n' && !(*Ascii))\r
1431 || (CharBuffer == '\n' && *Ascii)\r
1432 ){\r
1433 break;\r
1434 }\r
1435 //\r
1436 // if we have space save it...\r
1437 //\r
1438 if ((CountSoFar+1)*sizeof(CHAR16) < *Size){\r
1439 ASSERT(Buffer != NULL);\r
1440 ((CHAR16*)Buffer)[CountSoFar] = CharBuffer;\r
1441 ((CHAR16*)Buffer)[CountSoFar+1] = CHAR_NULL;\r
1442 }\r
1443 }\r
1444\r
1445 //\r
1446 // if we ran out of space tell when...\r
1447 //\r
1448 if ((CountSoFar+1)*sizeof(CHAR16) > *Size){\r
1449 *Size = (CountSoFar+1)*sizeof(CHAR16);\r
1450 if (!Truncate) {\r
1451 gEfiShellProtocol->SetFilePosition(Handle, OriginalFilePosition);\r
1452 } else {\r
1453 DEBUG((DEBUG_WARN, "The line was truncated in ShellFileHandleReadLine"));\r
1454 }\r
1455 return (EFI_BUFFER_TOO_SMALL);\r
1456 }\r
1457 while(Buffer[StrLen(Buffer)-1] == L'\r') {\r
1458 Buffer[StrLen(Buffer)-1] = CHAR_NULL;\r
1459 }\r
1460\r
1461 return (Status);\r
1462}\r
1463/**\r
1464 Frees any BUFFER_LIST defined type.\r
1465**/\r
1466VOID\r
1467EFIAPI\r
1468FreeBufferList (\r
1469 IN BUFFER_LIST *List\r
1470 )\r
1471{\r
1472 BUFFER_LIST *BufferListEntry;\r
1473\r
1474 if (List == NULL){\r
1475 return;\r
1476 }\r
1477 //\r
1478 // enumerate through the buffer list and free all memory\r
1479 //\r
1480 for ( BufferListEntry = ( BUFFER_LIST *)GetFirstNode(&List->Link)\r
1481 ; !IsListEmpty (&List->Link)\r
1482 ; BufferListEntry = (BUFFER_LIST *)GetFirstNode(&List->Link)\r
1483 ){\r
1484 RemoveEntryList(&BufferListEntry->Link);\r
1485 ASSERT(BufferListEntry->Buffer != NULL);\r
1486 if (BufferListEntry->Buffer != NULL) {\r
1487 FreePool(BufferListEntry->Buffer);\r
1488 }\r
1489 FreePool(BufferListEntry);\r
1490 }\r
1491}\r
1492\r
1493/**\r
1494 Chops off last directory or file entry in a path leaving the trailing slash\r
1495\r
1496 @param[in,out] Path\r
1497\r
1498 @retval FALSE No directory was found to chop off.\r
1499 @retval TRUE A directory was chopped off.\r
1500**/\r
1501BOOLEAN\r
1502EFIAPI\r
1503ChopLastSlash(\r
1504 IN OUT CHAR16 *PathToReturn\r
1505 )\r
1506{\r
1507 CHAR16 *Walker;\r
1508 CHAR16 *LastSlash = NULL;\r
1509 //\r
1510 // get directory name from path... ('chop' off extra)\r
1511 //\r
1512 for ( Walker = PathToReturn\r
1513 ; Walker != NULL && *Walker != CHAR_NULL\r
1514 ; Walker++\r
1515 ){\r
1516 if (*Walker == L'\\' && *(Walker + 1) != CHAR_NULL) {\r
1517 LastSlash = Walker+1;\r
1518 }\r
1519 }\r
1520 if (LastSlash != NULL) {\r
1521 *LastSlash = CHAR_NULL;\r
1522 return (TRUE);\r
1523 }\r
1524 return (FALSE);\r
1525}\r
1526\r