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