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