]> git.proxmox.com Git - mirror_edk2.git/blame - ShellPkg/Library/UefiShellCommandLib/UefiShellCommandLib.c
ShellPkg: Standardized HP Copyright Message String
[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
c011b6c9 4 (C) Copyright 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
53173337 529 Node->CommandString = AllocateCopyPool(StrSize(CommandString), CommandString);\r
a405b86d 530 ASSERT(Node->CommandString != NULL);\r
531\r
a405b86d 532 Node->GetManFileName = GetManFileName;\r
533 Node->CommandHandler = CommandHandler;\r
534 Node->LastError = CanAffectLE;\r
535 Node->HiiHandle = HiiHandle;\r
536 Node->ManFormatHelp = ManFormatHelp;\r
537\r
538 if ( StrLen(ProfileName)>0\r
539 && ((mProfileList != NULL\r
540 && StrStr(mProfileList, ProfileName) == NULL) || mProfileList == NULL)\r
541 ){\r
542 ASSERT((mProfileList == NULL && mProfileListSize == 0) || (mProfileList != NULL));\r
543 if (mProfileList == NULL) {\r
544 //\r
545 // If this is the first make a leading ';'\r
546 //\r
547 StrnCatGrow(&mProfileList, &mProfileListSize, L";", 0);\r
548 }\r
549 StrnCatGrow(&mProfileList, &mProfileListSize, ProfileName, 0);\r
550 StrnCatGrow(&mProfileList, &mProfileListSize, L";", 0);\r
551 }\r
552\r
553 //\r
d51088b7 554 // Insert a new entry on top of the list\r
555 //\r
556 InsertHeadList (&mCommandList.Link, &Node->Link);\r
557\r
558 //\r
559 // Move a new registered command to its sorted ordered location in the list\r
a405b86d 560 //\r
d51088b7 561 for (Command = (SHELL_COMMAND_INTERNAL_LIST_ENTRY *)GetFirstNode (&mCommandList.Link),\r
562 PrevCommand = (SHELL_COMMAND_INTERNAL_LIST_ENTRY *)GetFirstNode (&mCommandList.Link)\r
563 ; !IsNull (&mCommandList.Link, &Command->Link)\r
564 ; Command = (SHELL_COMMAND_INTERNAL_LIST_ENTRY *)GetNextNode (&mCommandList.Link, &Command->Link)) {\r
565\r
566 //\r
567 // Get Lexical Comparison Value between PrevCommand and Command list entry\r
568 //\r
569 LexicalMatchValue = gUnicodeCollation->StriColl (\r
570 gUnicodeCollation,\r
571 PrevCommand->CommandString,\r
572 Command->CommandString\r
573 );\r
574\r
575 //\r
576 // Swap PrevCommand and Command list entry if PrevCommand list entry\r
577 // is alphabetically greater than Command list entry\r
578 //\r
579 if (LexicalMatchValue > 0){\r
580 Command = (SHELL_COMMAND_INTERNAL_LIST_ENTRY *) SwapListEntries (&PrevCommand->Link, &Command->Link);\r
581 } else if (LexicalMatchValue < 0) {\r
582 //\r
583 // PrevCommand entry is lexically lower than Command entry\r
584 //\r
585 break;\r
586 }\r
587 }\r
a405b86d 588\r
589 return (RETURN_SUCCESS);\r
590}\r
591\r
592/**\r
593 Function to get the current Profile string.\r
594\r
595 @retval NULL There are no installed profiles.\r
596 @return A semi-colon delimited list of profiles.\r
597**/\r
598CONST CHAR16 *\r
599EFIAPI\r
600ShellCommandGetProfileList (\r
601 VOID\r
602 )\r
603{\r
604 return (mProfileList);\r
605}\r
606\r
607/**\r
608 Checks if a command string has been registered for CommandString and if so it runs\r
609 the previously registered handler for that command with the command line.\r
610\r
611 If CommandString is NULL, then ASSERT().\r
612\r
613 If Sections is specified, then each section name listed will be compared in a casesensitive\r
614 manner, to the section names described in Appendix B UEFI Shell 2.0 spec. If the section exists,\r
615 it will be appended to the returned help text. If the section does not exist, no\r
616 information will be returned. If Sections is NULL, then all help text information\r
617 available will be returned.\r
618\r
4ff7e37b
ED
619 @param[in] CommandString Pointer to the command name. This is the name\r
620 found on the command line in the shell.\r
621 @param[in, out] RetVal Pointer to the return vaule from the command handler.\r
a405b86d 622\r
4ff7e37b
ED
623 @param[in, out] CanAffectLE indicates whether this command's return value\r
624 needs to be placed into LASTERROR environment variable.\r
a405b86d 625\r
626 @retval RETURN_SUCCESS The handler was run.\r
627 @retval RETURN_NOT_FOUND The CommandString did not match a registered\r
628 command name.\r
629 @sa SHELL_RUN_COMMAND\r
630**/\r
631RETURN_STATUS\r
632EFIAPI\r
633ShellCommandRunCommandHandler (\r
634 IN CONST CHAR16 *CommandString,\r
635 IN OUT SHELL_STATUS *RetVal,\r
636 IN OUT BOOLEAN *CanAffectLE OPTIONAL\r
637 )\r
638{\r
cf812a20
JC
639 SHELL_COMMAND_INTERNAL_LIST_ENTRY *Node;\r
640 EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL *DynamicCommand;\r
a405b86d 641\r
642 //\r
643 // assert for NULL parameters\r
644 //\r
645 ASSERT(CommandString != NULL);\r
646\r
647 //\r
648 // check for the command\r
649 //\r
650 for ( Node = (SHELL_COMMAND_INTERNAL_LIST_ENTRY *)GetFirstNode(&mCommandList.Link)\r
651 ; !IsNull(&mCommandList.Link, &Node->Link)\r
652 ; Node = (SHELL_COMMAND_INTERNAL_LIST_ENTRY *)GetNextNode(&mCommandList.Link, &Node->Link)\r
653 ){\r
654 ASSERT(Node->CommandString != NULL);\r
655 if (gUnicodeCollation->StriColl(\r
656 gUnicodeCollation,\r
657 (CHAR16*)CommandString,\r
658 Node->CommandString) == 0\r
cf812a20 659 ){\r
a405b86d 660 if (CanAffectLE != NULL) {\r
661 *CanAffectLE = Node->LastError;\r
662 }\r
663 if (RetVal != NULL) {\r
664 *RetVal = Node->CommandHandler(NULL, gST);\r
665 } else {\r
666 Node->CommandHandler(NULL, gST);\r
667 }\r
668 return (RETURN_SUCCESS);\r
669 }\r
670 }\r
cf812a20
JC
671\r
672 //\r
673 // An internal command was not found, try to find a dynamic command\r
674 //\r
675 DynamicCommand = (EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL *)ShellCommandFindDynamicCommand(CommandString);\r
676 if (DynamicCommand != NULL) {\r
677 if (RetVal != NULL) {\r
678 *RetVal = DynamicCommand->Handler(DynamicCommand, gST, gEfiShellParametersProtocol, gEfiShellProtocol);\r
679 } else {\r
680 DynamicCommand->Handler(DynamicCommand, gST, gEfiShellParametersProtocol, gEfiShellProtocol);\r
681 }\r
682 return (RETURN_SUCCESS);\r
683 }\r
684\r
a405b86d 685 return (RETURN_NOT_FOUND);\r
686}\r
687\r
688/**\r
689 Checks if a command string has been registered for CommandString and if so it\r
690 returns the MAN filename specified for that command.\r
691\r
692 If CommandString is NULL, then ASSERT().\r
693\r
694 @param[in] CommandString Pointer to the command name. This is the name\r
695 found on the command line in the shell.\\r
696\r
697 @retval NULL the commandString was not a registered command.\r
698 @return other the name of the MAN file.\r
699 @sa SHELL_GET_MAN_FILENAME\r
700**/\r
701CONST CHAR16*\r
702EFIAPI\r
703ShellCommandGetManFileNameHandler (\r
704 IN CONST CHAR16 *CommandString\r
705 )\r
706{\r
707 SHELL_COMMAND_INTERNAL_LIST_ENTRY *Node;\r
708\r
709 //\r
710 // assert for NULL parameters\r
711 //\r
712 ASSERT(CommandString != NULL);\r
713\r
714 //\r
715 // check for the command\r
716 //\r
717 for ( Node = (SHELL_COMMAND_INTERNAL_LIST_ENTRY *)GetFirstNode(&mCommandList.Link)\r
718 ; !IsNull(&mCommandList.Link, &Node->Link)\r
719 ; Node = (SHELL_COMMAND_INTERNAL_LIST_ENTRY *)GetNextNode(&mCommandList.Link, &Node->Link)\r
720 ){\r
721 ASSERT(Node->CommandString != NULL);\r
722 if (gUnicodeCollation->StriColl(\r
723 gUnicodeCollation,\r
724 (CHAR16*)CommandString,\r
725 Node->CommandString) == 0\r
726 ){\r
727 return (Node->GetManFileName());\r
728 }\r
729 }\r
730 return (NULL);\r
731}\r
732\r
733/**\r
734 Get the list of all available shell internal commands. This is a linked list\r
735 (via LIST_ENTRY structure). enumerate through it using the BaseLib linked\r
736 list functions. do not modify the values.\r
737\r
1a63ec8f 738 @param[in] Sort TRUE to alphabetically sort the values first. FALSE otherwise.\r
739\r
a405b86d 740 @return a Linked list of all available shell commands.\r
741**/\r
742CONST COMMAND_LIST*\r
743EFIAPI\r
744ShellCommandGetCommandList (\r
1a63ec8f 745 IN CONST BOOLEAN Sort\r
a405b86d 746 )\r
747{\r
1a63ec8f 748// if (!Sort) {\r
749// return ((COMMAND_LIST*)(&mCommandList));\r
750// }\r
a405b86d 751 return ((COMMAND_LIST*)(&mCommandList));\r
752}\r
753\r
754/**\r
755 Registers aliases to be set as part of the initialization of the shell application.\r
756\r
757 If Command is NULL, then ASSERT().\r
758 If Alias is NULL, then ASSERT().\r
759\r
760 @param[in] Command Pointer to the Command\r
761 @param[in] Alias Pointer to Alias\r
762\r
763 @retval RETURN_SUCCESS The handlers were registered.\r
764 @retval RETURN_OUT_OF_RESOURCES There are not enough resources available to\r
765 register the shell command.\r
766**/\r
767RETURN_STATUS\r
768EFIAPI\r
769ShellCommandRegisterAlias (\r
770 IN CONST CHAR16 *Command,\r
771 IN CONST CHAR16 *Alias\r
772 )\r
773{\r
774 ALIAS_LIST *Node;\r
4ba9b812
TS
775 ALIAS_LIST *CommandAlias;\r
776 ALIAS_LIST *PrevCommandAlias; \r
777 INTN LexicalMatchValue;\r
a405b86d 778\r
779 //\r
780 // Asserts for NULL\r
781 //\r
782 ASSERT(Command != NULL);\r
783 ASSERT(Alias != NULL);\r
784\r
785 //\r
786 // allocate memory for new struct\r
787 //\r
1a63ec8f 788 Node = AllocateZeroPool(sizeof(ALIAS_LIST));\r
a405b86d 789 ASSERT(Node != NULL);\r
53173337
JC
790 Node->CommandString = AllocateCopyPool(StrSize(Command), Command);\r
791 Node->Alias = AllocateCopyPool(StrSize(Alias), Alias);\r
a405b86d 792 ASSERT(Node->CommandString != NULL);\r
793 ASSERT(Node->Alias != NULL);\r
794\r
4ba9b812
TS
795 InsertHeadList (&mAliasList.Link, &Node->Link);\r
796\r
a405b86d 797 //\r
4ba9b812 798 // Move a new pre-defined registered alias to its sorted ordered location in the list\r
a405b86d 799 //\r
4ba9b812
TS
800 for ( CommandAlias = (ALIAS_LIST *)GetFirstNode (&mAliasList.Link),\r
801 PrevCommandAlias = (ALIAS_LIST *)GetFirstNode (&mAliasList.Link)\r
802 ; !IsNull (&mAliasList.Link, &CommandAlias->Link)\r
803 ; CommandAlias = (ALIAS_LIST *) GetNextNode (&mAliasList.Link, &CommandAlias->Link) ) {\r
804 //\r
805 // Get Lexical comparison value between PrevCommandAlias and CommandAlias List Entry\r
806 //\r
807 LexicalMatchValue = gUnicodeCollation->StriColl (\r
808 gUnicodeCollation,\r
809 PrevCommandAlias->Alias,\r
810 CommandAlias->Alias\r
811 );\r
812\r
813 //\r
814 // Swap PrevCommandAlias and CommandAlias list entry if PrevCommandAlias list entry\r
815 // is alphabetically greater than CommandAlias list entry\r
816 // \r
817 if (LexicalMatchValue > 0) {\r
818 CommandAlias = (ALIAS_LIST *) SwapListEntries (&PrevCommandAlias->Link, &CommandAlias->Link);\r
819 } else if (LexicalMatchValue < 0) {\r
820 //\r
821 // PrevCommandAlias entry is lexically lower than CommandAlias entry\r
822 //\r
823 break;\r
824 }\r
825 }\r
a405b86d 826\r
827 return (RETURN_SUCCESS);\r
828}\r
829\r
830/**\r
831 Get the list of all shell alias commands. This is a linked list\r
832 (via LIST_ENTRY structure). enumerate through it using the BaseLib linked\r
833 list functions. do not modify the values.\r
834\r
835 @return a Linked list of all requested shell alias'.\r
836**/\r
837CONST ALIAS_LIST*\r
838EFIAPI\r
839ShellCommandGetInitAliasList (\r
840 VOID\r
841 )\r
842{\r
843 return (&mAliasList);\r
844}\r
845\r
846/**\r
1a63ec8f 847 Determine if a given alias is on the list of built in alias'.\r
a405b86d 848\r
849 @param[in] Alias The alias to test for\r
850\r
851 @retval TRUE The alias is a built in alias\r
852 @retval FALSE The alias is not a built in alias\r
853**/\r
854BOOLEAN\r
855EFIAPI\r
856ShellCommandIsOnAliasList(\r
857 IN CONST CHAR16 *Alias\r
858 )\r
859{\r
860 ALIAS_LIST *Node;\r
861\r
862 //\r
863 // assert for NULL parameter\r
864 //\r
865 ASSERT(Alias != NULL);\r
866\r
867 //\r
868 // check for the Alias\r
869 //\r
870 for ( Node = (ALIAS_LIST *)GetFirstNode(&mAliasList.Link)\r
871 ; !IsNull(&mAliasList.Link, &Node->Link)\r
872 ; Node = (ALIAS_LIST *)GetNextNode(&mAliasList.Link, &Node->Link)\r
873 ){\r
874 ASSERT(Node->CommandString != NULL);\r
875 ASSERT(Node->Alias != NULL);\r
876 if (gUnicodeCollation->StriColl(\r
877 gUnicodeCollation,\r
878 (CHAR16*)Alias,\r
879 Node->CommandString) == 0\r
880 ){\r
881 return (TRUE);\r
882 }\r
883 if (gUnicodeCollation->StriColl(\r
884 gUnicodeCollation,\r
885 (CHAR16*)Alias,\r
886 Node->Alias) == 0\r
887 ){\r
888 return (TRUE);\r
889 }\r
890 }\r
891 return (FALSE);\r
892}\r
893\r
894/**\r
895 Function to determine current state of ECHO. Echo determins if lines from scripts\r
896 and ECHO commands are enabled.\r
897\r
898 @retval TRUE Echo is currently enabled\r
899 @retval FALSE Echo is currently disabled\r
900**/\r
901BOOLEAN\r
902EFIAPI\r
903ShellCommandGetEchoState(\r
904 VOID\r
905 )\r
906{\r
907 return (mEchoState);\r
908}\r
909\r
910/**\r
911 Function to set current state of ECHO. Echo determins if lines from scripts\r
912 and ECHO commands are enabled.\r
913\r
914 If State is TRUE, Echo will be enabled.\r
915 If State is FALSE, Echo will be disabled.\r
1a63ec8f 916\r
917 @param[in] State How to set echo.\r
a405b86d 918**/\r
919VOID\r
920EFIAPI\r
921ShellCommandSetEchoState(\r
922 IN BOOLEAN State\r
923 )\r
924{\r
925 mEchoState = State;\r
926}\r
927\r
928/**\r
929 Indicate that the current shell or script should exit.\r
930\r
b6b22b13 931 @param[in] ScriptOnly TRUE if exiting a script; FALSE otherwise.\r
932 @param[in] ErrorCode The 64 bit error code to return.\r
a405b86d 933**/\r
934VOID\r
935EFIAPI\r
936ShellCommandRegisterExit (\r
b6b22b13 937 IN BOOLEAN ScriptOnly,\r
938 IN CONST UINT64 ErrorCode\r
a405b86d 939 )\r
940{\r
941 mExitRequested = (BOOLEAN)(!mExitRequested);\r
942 if (mExitRequested) {\r
943 mExitScript = ScriptOnly;\r
944 } else {\r
945 mExitScript = FALSE;\r
946 }\r
b6b22b13 947 mExitCode = ErrorCode;\r
a405b86d 948}\r
949\r
950/**\r
951 Retrieve the Exit indicator.\r
952\r
953 @retval TRUE Exit was indicated.\r
954 @retval FALSE Exis was not indicated.\r
955**/\r
956BOOLEAN\r
957EFIAPI\r
958ShellCommandGetExit (\r
959 VOID\r
960 )\r
961{\r
962 return (mExitRequested);\r
963}\r
964\r
b6b22b13 965/**\r
966 Retrieve the Exit code.\r
967\r
968 If ShellCommandGetExit returns FALSE than the return from this is undefined.\r
969\r
970 @return the value passed into RegisterExit.\r
971**/\r
972UINT64\r
973EFIAPI\r
974ShellCommandGetExitCode (\r
975 VOID\r
976 )\r
977{\r
978 return (mExitCode);\r
979}\r
a405b86d 980/**\r
981 Retrieve the Exit script indicator.\r
982\r
983 If ShellCommandGetExit returns FALSE than the return from this is undefined.\r
984\r
985 @retval TRUE ScriptOnly was indicated.\r
986 @retval FALSE ScriptOnly was not indicated.\r
987**/\r
988BOOLEAN\r
989EFIAPI\r
990ShellCommandGetScriptExit (\r
991 VOID\r
992 )\r
993{\r
994 return (mExitScript);\r
995}\r
996\r
997/**\r
998 Function to cleanup all memory from a SCRIPT_FILE structure.\r
999\r
1000 @param[in] Script The pointer to the structure to cleanup.\r
1001**/\r
1002VOID\r
1003EFIAPI\r
1004DeleteScriptFileStruct (\r
1005 IN SCRIPT_FILE *Script\r
1006 )\r
1007{\r
1008 UINT8 LoopVar;\r
0ab85bef 1009\r
1010 if (Script == NULL) {\r
1011 return;\r
1012 }\r
1013\r
a405b86d 1014 for (LoopVar = 0 ; LoopVar < Script->Argc ; LoopVar++) {\r
0ab85bef 1015 SHELL_FREE_NON_NULL(Script->Argv[LoopVar]);\r
a405b86d 1016 }\r
1017 if (Script->Argv != NULL) {\r
0ab85bef 1018 SHELL_FREE_NON_NULL(Script->Argv);\r
a405b86d 1019 }\r
1020 Script->CurrentCommand = NULL;\r
1021 while (!IsListEmpty (&Script->CommandList)) {\r
1022 Script->CurrentCommand = (SCRIPT_COMMAND_LIST *)GetFirstNode(&Script->CommandList);\r
1023 if (Script->CurrentCommand != NULL) {\r
1024 RemoveEntryList(&Script->CurrentCommand->Link);\r
1025 if (Script->CurrentCommand->Cl != NULL) {\r
0ab85bef 1026 SHELL_FREE_NON_NULL(Script->CurrentCommand->Cl);\r
a405b86d 1027 }\r
1028 if (Script->CurrentCommand->Data != NULL) {\r
0ab85bef 1029 SHELL_FREE_NON_NULL(Script->CurrentCommand->Data);\r
a405b86d 1030 }\r
0ab85bef 1031 SHELL_FREE_NON_NULL(Script->CurrentCommand);\r
a405b86d 1032 }\r
1033 }\r
0ab85bef 1034 SHELL_FREE_NON_NULL(Script->ScriptName);\r
1035 SHELL_FREE_NON_NULL(Script);\r
a405b86d 1036}\r
1037\r
1038/**\r
1039 Function to return a pointer to the currently running script file object.\r
1040\r
1041 @retval NULL A script file is not currently running.\r
1042 @return A pointer to the current script file object.\r
1043**/\r
1044SCRIPT_FILE*\r
1045EFIAPI\r
1046ShellCommandGetCurrentScriptFile (\r
1047 VOID\r
1048 )\r
1049{\r
1050 SCRIPT_FILE_LIST *List;\r
1051 if (IsListEmpty (&mScriptList.Link)) {\r
1052 return (NULL);\r
1053 }\r
1054 List = ((SCRIPT_FILE_LIST*)GetFirstNode(&mScriptList.Link));\r
1055 return (List->Data);\r
1056}\r
1057\r
1058/**\r
1059 Function to set a new script as the currently running one.\r
1060\r
1061 This function will correctly stack and unstack nested scripts.\r
1062\r
1063 @param[in] Script Pointer to new script information structure. if NULL\r
1064 will remove and de-allocate the top-most Script structure.\r
1065\r
1066 @return A pointer to the current running script file after this\r
1067 change. NULL if removing the final script.\r
1068**/\r
1069SCRIPT_FILE*\r
1070EFIAPI\r
1071ShellCommandSetNewScript (\r
1072 IN SCRIPT_FILE *Script OPTIONAL\r
1073 )\r
1074{\r
1075 SCRIPT_FILE_LIST *Node;\r
1076 if (Script == NULL) {\r
1077 if (IsListEmpty (&mScriptList.Link)) {\r
a405b86d 1078 return (NULL);\r
1079 }\r
1080 Node = (SCRIPT_FILE_LIST *)GetFirstNode(&mScriptList.Link);\r
1081 RemoveEntryList(&Node->Link);\r
1082 DeleteScriptFileStruct(Node->Data);\r
1083 FreePool(Node);\r
1084 } else {\r
1085 Node = AllocateZeroPool(sizeof(SCRIPT_FILE_LIST));\r
0ab85bef 1086 if (Node == NULL) {\r
1087 return (NULL);\r
1088 }\r
a405b86d 1089 Node->Data = Script;\r
1090 InsertHeadList(&mScriptList.Link, &Node->Link);\r
1091 }\r
1092 return (ShellCommandGetCurrentScriptFile());\r
1093}\r
1094\r
1095/**\r
1096 Function to generate the next default mapping name.\r
1097\r
1098 If the return value is not NULL then it must be callee freed.\r
1099\r
1100 @param Type What kind of mapping name to make.\r
1101\r
1102 @retval NULL a memory allocation failed.\r
1103 @return a new map name string\r
1104**/\r
1105CHAR16*\r
1106EFIAPI\r
1107ShellCommandCreateNewMappingName(\r
1108 IN CONST SHELL_MAPPING_TYPE Type\r
1109 )\r
1110{\r
1111 CHAR16 *String;\r
1112 ASSERT(Type < MappingTypeMax);\r
1113\r
1114 String = NULL;\r
1115\r
1116 String = AllocateZeroPool(PcdGet8(PcdShellMapNameLength) * sizeof(String[0]));\r
1117 UnicodeSPrint(\r
1118 String,\r
1119 PcdGet8(PcdShellMapNameLength) * sizeof(String[0]),\r
1120 Type == MappingTypeFileSystem?L"FS%d:":L"BLK%d:",\r
1121 Type == MappingTypeFileSystem?mFsMaxCount++:mBlkMaxCount++);\r
1122\r
1123 return (String);\r
1124}\r
1125\r
1126/**\r
1127 Function to add a map node to the list of map items and update the "path" environment variable (optionally).\r
1128\r
1129 If Path is TRUE (during initialization only), the path environment variable will also be updated to include\r
1130 default paths on the new map name...\r
1131\r
1132 Path should be FALSE when this function is called from the protocol SetMap function.\r
1133\r
1134 @param[in] Name The human readable mapped name.\r
1135 @param[in] DevicePath The Device Path for this map.\r
1136 @param[in] Flags The Flags attribute for this map item.\r
1137 @param[in] Path TRUE to update path, FALSE to skip this step (should only be TRUE during initialization).\r
1138\r
1139 @retval EFI_SUCCESS The addition was sucessful.\r
1140 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.\r
1141 @retval EFI_INVALID_PARAMETER A parameter was invalid.\r
1142**/\r
1143EFI_STATUS\r
1144EFIAPI\r
1145ShellCommandAddMapItemAndUpdatePath(\r
1146 IN CONST CHAR16 *Name,\r
1147 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath,\r
1148 IN CONST UINT64 Flags,\r
1149 IN CONST BOOLEAN Path\r
1150 )\r
1151{\r
1152 EFI_STATUS Status;\r
1153 SHELL_MAP_LIST *MapListNode;\r
1154 CONST CHAR16 *OriginalPath;\r
1155 CHAR16 *NewPath;\r
1156 UINTN NewPathSize;\r
1157\r
1158 NewPathSize = 0;\r
1159 NewPath = NULL;\r
1160 OriginalPath = NULL;\r
1161 Status = EFI_SUCCESS;\r
1162\r
1163 MapListNode = AllocateZeroPool(sizeof(SHELL_MAP_LIST));\r
1164 if (MapListNode == NULL) {\r
1165 Status = EFI_OUT_OF_RESOURCES;\r
1166 } else {\r
1167 MapListNode->Flags = Flags;\r
3957a5a5 1168 MapListNode->MapName = AllocateCopyPool(StrSize(Name), Name);\r
a405b86d 1169 MapListNode->DevicePath = DuplicateDevicePath(DevicePath);\r
1170 if ((MapListNode->MapName == NULL) || (MapListNode->DevicePath == NULL)){\r
1171 Status = EFI_OUT_OF_RESOURCES;\r
1172 } else {\r
a405b86d 1173 InsertTailList(&gShellMapList.Link, &MapListNode->Link);\r
1174 }\r
1175 }\r
1176 if (EFI_ERROR(Status)) {\r
1177 if (MapListNode != NULL) {\r
1178 if (MapListNode->DevicePath != NULL) {\r
1179 FreePool(MapListNode->DevicePath);\r
1180 }\r
1181 if (MapListNode->MapName != NULL) {\r
1182 FreePool(MapListNode->MapName);\r
1183 }\r
1184 FreePool(MapListNode);\r
1185 }\r
1186 } else if (Path) {\r
1187 //\r
1188 // Since there was no error and Path was TRUE\r
1189 // Now add the correct path for that mapping\r
1190 //\r
1191 OriginalPath = gEfiShellProtocol->GetEnv(L"path");\r
1192 ASSERT((NewPath == NULL && NewPathSize == 0) || (NewPath != NULL));\r
1193 if (OriginalPath != NULL) {\r
1194 StrnCatGrow(&NewPath, &NewPathSize, OriginalPath, 0);\r
1195 } else {\r
1196 StrnCatGrow(&NewPath, &NewPathSize, L".\\", 0);\r
1197 }\r
1198 StrnCatGrow(&NewPath, &NewPathSize, L";", 0);\r
1199 StrnCatGrow(&NewPath, &NewPathSize, Name, 0);\r
1200 StrnCatGrow(&NewPath, &NewPathSize, L"\\efi\\tools\\;", 0);\r
1201 StrnCatGrow(&NewPath, &NewPathSize, Name, 0);\r
1202 StrnCatGrow(&NewPath, &NewPathSize, L"\\efi\\boot\\;", 0);\r
1203 StrnCatGrow(&NewPath, &NewPathSize, Name, 0);\r
1204 StrnCatGrow(&NewPath, &NewPathSize, L"\\", 0);\r
1205\r
1206 Status = gEfiShellProtocol->SetEnv(L"path", NewPath, TRUE);\r
1207 ASSERT_EFI_ERROR(Status);\r
1208 FreePool(NewPath);\r
1209 }\r
1210 return (Status);\r
1211}\r
1212\r
1213/**\r
1214 Creates the default map names for each device path in the system with\r
1215 a protocol depending on the Type.\r
1216\r
1217 Creates the consistent map names for each device path in the system with\r
1218 a protocol depending on the Type.\r
1219\r
1220 Note: This will reset all mappings in the system("map -r").\r
1221\r
1222 Also sets up the default path environment variable if Type is FileSystem.\r
1223\r
1224 @retval EFI_SUCCESS All map names were created sucessfully.\r
1225 @retval EFI_NOT_FOUND No protocols were found in the system.\r
1226 @return Error returned from gBS->LocateHandle().\r
1227\r
1228 @sa LocateHandle\r
1229**/\r
1230EFI_STATUS\r
1231EFIAPI\r
1232ShellCommandCreateInitialMappingsAndPaths(\r
1233 VOID\r
1234 )\r
1235{\r
1236 EFI_STATUS Status;\r
1237 EFI_HANDLE *HandleList;\r
1238 UINTN Count;\r
1239 EFI_DEVICE_PATH_PROTOCOL **DevicePathList;\r
1240 CHAR16 *NewDefaultName;\r
1241 CHAR16 *NewConsistName;\r
1242 EFI_DEVICE_PATH_PROTOCOL **ConsistMappingTable;\r
1243 SHELL_MAP_LIST *MapListNode;\r
1244\r
1245 HandleList = NULL;\r
1246\r
1247 //\r
1248 // Reset the static members back to zero\r
1249 //\r
1250 mFsMaxCount = 0;\r
1251 mBlkMaxCount = 0;\r
1252\r
1253 gEfiShellProtocol->SetEnv(L"path", L"", TRUE);\r
1254\r
1255 //\r
1256 // First empty out the existing list.\r
1257 //\r
1258 if (!IsListEmpty(&gShellMapList.Link)) {\r
1259 for ( MapListNode = (SHELL_MAP_LIST *)GetFirstNode(&gShellMapList.Link)\r
1260 ; !IsListEmpty(&gShellMapList.Link)\r
1261 ; MapListNode = (SHELL_MAP_LIST *)GetFirstNode(&gShellMapList.Link)\r
1262 ){\r
1263 RemoveEntryList(&MapListNode->Link);\r
1264 FreePool(MapListNode);\r
1265 } // for loop\r
1266 }\r
1267\r
1268 //\r
1269 // Find each handle with Simple File System\r
1270 //\r
0ab85bef 1271 HandleList = GetHandleListByProtocol(&gEfiSimpleFileSystemProtocolGuid);\r
a405b86d 1272 if (HandleList != NULL) {\r
1273 //\r
1274 // Do a count of the handles\r
1275 //\r
1276 for (Count = 0 ; HandleList[Count] != NULL ; Count++);\r
1277\r
1278 //\r
1279 // Get all Device Paths\r
1280 //\r
1a63ec8f 1281 DevicePathList = AllocateZeroPool(sizeof(EFI_DEVICE_PATH_PROTOCOL*) * Count);\r
a405b86d 1282 ASSERT(DevicePathList != NULL);\r
1283\r
1284 for (Count = 0 ; HandleList[Count] != NULL ; Count++) {\r
1285 DevicePathList[Count] = DevicePathFromHandle(HandleList[Count]);\r
1286 }\r
1287\r
1288 //\r
1289 // Sort all DevicePaths\r
1290 //\r
1291 PerformQuickSort(DevicePathList, Count, sizeof(EFI_DEVICE_PATH_PROTOCOL*), DevicePathCompare);\r
1292\r
1293 ShellCommandConsistMappingInitialize(&ConsistMappingTable);\r
1294 //\r
1295 // Assign new Mappings to all...\r
1296 //\r
1297 for (Count = 0 ; HandleList[Count] != NULL ; Count++) {\r
1298 //\r
1299 // Get default name first\r
1300 //\r
1301 NewDefaultName = ShellCommandCreateNewMappingName(MappingTypeFileSystem);\r
1302 ASSERT(NewDefaultName != NULL);\r
1303 Status = ShellCommandAddMapItemAndUpdatePath(NewDefaultName, DevicePathList[Count], 0, TRUE);\r
1304 ASSERT_EFI_ERROR(Status);\r
1305 FreePool(NewDefaultName);\r
1306\r
1307 //\r
1308 // Now do consistent name\r
1309 //\r
1310 NewConsistName = ShellCommandConsistMappingGenMappingName(DevicePathList[Count], ConsistMappingTable);\r
1311 if (NewConsistName != NULL) {\r
1312 Status = ShellCommandAddMapItemAndUpdatePath(NewConsistName, DevicePathList[Count], 0, FALSE);\r
1313 ASSERT_EFI_ERROR(Status);\r
1314 FreePool(NewConsistName);\r
1315 }\r
1316 }\r
1317\r
1318 ShellCommandConsistMappingUnInitialize(ConsistMappingTable);\r
1319\r
1320 SHELL_FREE_NON_NULL(HandleList);\r
1321 SHELL_FREE_NON_NULL(DevicePathList);\r
1322\r
1323 HandleList = NULL;\r
1324 } else {\r
1325 Count = (UINTN)-1;\r
1326 }\r
1327\r
1328 //\r
1329 // Find each handle with Block Io\r
1330 //\r
0ab85bef 1331 HandleList = GetHandleListByProtocol(&gEfiBlockIoProtocolGuid);\r
a405b86d 1332 if (HandleList != NULL) {\r
1333 for (Count = 0 ; HandleList[Count] != NULL ; Count++);\r
1334\r
1335 //\r
1336 // Get all Device Paths\r
1337 //\r
1a63ec8f 1338 DevicePathList = AllocateZeroPool(sizeof(EFI_DEVICE_PATH_PROTOCOL*) * Count);\r
a405b86d 1339 ASSERT(DevicePathList != NULL);\r
1340\r
1341 for (Count = 0 ; HandleList[Count] != NULL ; Count++) {\r
1342 DevicePathList[Count] = DevicePathFromHandle(HandleList[Count]);\r
1343 }\r
1344\r
1345 //\r
1346 // Sort all DevicePaths\r
1347 //\r
1348 PerformQuickSort(DevicePathList, Count, sizeof(EFI_DEVICE_PATH_PROTOCOL*), DevicePathCompare);\r
1349\r
1350 //\r
1351 // Assign new Mappings to all...\r
1352 //\r
1353 for (Count = 0 ; HandleList[Count] != NULL ; Count++) {\r
1354 //\r
1355 // Get default name first\r
1356 //\r
1357 NewDefaultName = ShellCommandCreateNewMappingName(MappingTypeBlockIo);\r
1358 ASSERT(NewDefaultName != NULL);\r
1359 Status = ShellCommandAddMapItemAndUpdatePath(NewDefaultName, DevicePathList[Count], 0, FALSE);\r
1360 ASSERT_EFI_ERROR(Status);\r
1361 FreePool(NewDefaultName);\r
1362 }\r
1363\r
1364 SHELL_FREE_NON_NULL(HandleList);\r
1365 SHELL_FREE_NON_NULL(DevicePathList);\r
1366 } else if (Count == (UINTN)-1) {\r
1367 return (EFI_NOT_FOUND);\r
1368 }\r
1369\r
1370 return (EFI_SUCCESS);\r
cf812a20
JC
1371}\r
1372\r
1373/**\r
1374 Add mappings for any devices without one. Do not change any existing maps.\r
1375\r
1376 @retval EFI_SUCCESS The operation was successful.\r
1377**/\r
1378EFI_STATUS\r
1379EFIAPI\r
1380ShellCommandUpdateMapping (\r
1381 VOID\r
1382 )\r
1383{\r
1384 EFI_STATUS Status;\r
1385 EFI_HANDLE *HandleList;\r
1386 UINTN Count;\r
1387 EFI_DEVICE_PATH_PROTOCOL **DevicePathList;\r
1388 CHAR16 *NewDefaultName;\r
1389 CHAR16 *NewConsistName;\r
1390 EFI_DEVICE_PATH_PROTOCOL **ConsistMappingTable;\r
1391\r
1392 HandleList = NULL;\r
1393 Status = EFI_SUCCESS;\r
1394\r
1395 //\r
1396 // remove mappings that represent removed devices.\r
1397 //\r
1398\r
1399 //\r
1400 // Find each handle with Simple File System\r
1401 //\r
1402 HandleList = GetHandleListByProtocol(&gEfiSimpleFileSystemProtocolGuid);\r
1403 if (HandleList != NULL) {\r
1404 //\r
1405 // Do a count of the handles\r
1406 //\r
1407 for (Count = 0 ; HandleList[Count] != NULL ; Count++);\r
1408\r
1409 //\r
1410 // Get all Device Paths\r
1411 //\r
1412 DevicePathList = AllocateZeroPool(sizeof(EFI_DEVICE_PATH_PROTOCOL*) * Count);\r
ae315cc2
JC
1413 if (DevicePathList == NULL) {\r
1414 return (EFI_OUT_OF_RESOURCES);\r
1415 }\r
cf812a20
JC
1416\r
1417 for (Count = 0 ; HandleList[Count] != NULL ; Count++) {\r
1418 DevicePathList[Count] = DevicePathFromHandle(HandleList[Count]);\r
1419 }\r
1420\r
1421 //\r
1422 // Sort all DevicePaths\r
1423 //\r
1424 PerformQuickSort(DevicePathList, Count, sizeof(EFI_DEVICE_PATH_PROTOCOL*), DevicePathCompare);\r
1425\r
1426 ShellCommandConsistMappingInitialize(&ConsistMappingTable);\r
1427\r
1428 //\r
1429 // Assign new Mappings to remainders\r
1430 //\r
ae315cc2 1431 for (Count = 0 ; !EFI_ERROR(Status) && HandleList[Count] != NULL && !EFI_ERROR(Status); Count++) {\r
cf812a20
JC
1432 //\r
1433 // Skip ones that already have\r
1434 //\r
1435 if (gEfiShellProtocol->GetMapFromDevicePath(&DevicePathList[Count]) != NULL) {\r
1436 continue;\r
1437 }\r
1438 //\r
1439 // Get default name\r
1440 //\r
1441 NewDefaultName = ShellCommandCreateNewMappingName(MappingTypeFileSystem);\r
ae315cc2
JC
1442 if (NewDefaultName == NULL) {\r
1443 Status = EFI_OUT_OF_RESOURCES;\r
1444 break;\r
1445 }\r
cf812a20
JC
1446\r
1447 //\r
1448 // Call shell protocol SetMap function now...\r
1449 //\r
1450 Status = gEfiShellProtocol->SetMap(DevicePathList[Count], NewDefaultName);\r
1451\r
1452 if (!EFI_ERROR(Status)) {\r
1453 //\r
1454 // Now do consistent name\r
1455 //\r
1456 NewConsistName = ShellCommandConsistMappingGenMappingName(DevicePathList[Count], ConsistMappingTable);\r
1457 if (NewConsistName != NULL) {\r
1458 Status = gEfiShellProtocol->SetMap(DevicePathList[Count], NewConsistName);\r
1459 FreePool(NewConsistName);\r
1460 }\r
1461 }\r
1462\r
1463 FreePool(NewDefaultName);\r
1464 }\r
1465 ShellCommandConsistMappingUnInitialize(ConsistMappingTable);\r
1466 SHELL_FREE_NON_NULL(HandleList);\r
1467 SHELL_FREE_NON_NULL(DevicePathList);\r
1468\r
1469 HandleList = NULL;\r
1470 } else {\r
1471 Count = (UINTN)-1;\r
1472 }\r
1473 //\r
1474 // Do it all over again for gEfiBlockIoProtocolGuid\r
1475 //\r
1476\r
1477 return (Status);\r
1478}\r
1479\r
1480/**\r
1481 Converts a SHELL_FILE_HANDLE to an EFI_FILE_PROTOCOL*.\r
1482\r
1483 @param[in] Handle The SHELL_FILE_HANDLE to convert.\r
a405b86d 1484\r
1485 @return a EFI_FILE_PROTOCOL* representing the same file.\r
1486**/\r
1487EFI_FILE_PROTOCOL*\r
1488EFIAPI\r
1489ConvertShellHandleToEfiFileProtocol(\r
1490 IN CONST SHELL_FILE_HANDLE Handle\r
1491 )\r
1492{\r
1493 return ((EFI_FILE_PROTOCOL*)(Handle));\r
1494}\r
1495\r
1496/**\r
1497 Converts a EFI_FILE_PROTOCOL* to an SHELL_FILE_HANDLE.\r
1498\r
1499 @param[in] Handle The pointer to EFI_FILE_PROTOCOL to convert.\r
1500 @param[in] Path The path to the file for verification.\r
1501\r
ff51746b 1502 @return A SHELL_FILE_HANDLE representing the same file.\r
1503 @retval NULL There was not enough memory.\r
a405b86d 1504**/\r
1505SHELL_FILE_HANDLE\r
1506EFIAPI\r
1507ConvertEfiFileProtocolToShellHandle(\r
1508 IN CONST EFI_FILE_PROTOCOL *Handle,\r
1509 IN CONST CHAR16 *Path\r
1510 )\r
1511{\r
1512 SHELL_COMMAND_FILE_HANDLE *Buffer;\r
1513 BUFFER_LIST *NewNode;\r
1514\r
1515 if (Path != NULL) {\r
1516 Buffer = AllocateZeroPool(sizeof(SHELL_COMMAND_FILE_HANDLE));\r
ff51746b 1517 if (Buffer == NULL) {\r
1518 return (NULL);\r
1519 }\r
1a63ec8f 1520 NewNode = AllocateZeroPool(sizeof(BUFFER_LIST));\r
ff51746b 1521 if (NewNode == NULL) {\r
ae315cc2 1522 SHELL_FREE_NON_NULL(Buffer);\r
ff51746b 1523 return (NULL);\r
1524 }\r
a405b86d 1525 Buffer->FileHandle = (EFI_FILE_PROTOCOL*)Handle;\r
1526 Buffer->Path = StrnCatGrow(&Buffer->Path, NULL, Path, 0);\r
ff51746b 1527 if (Buffer->Path == NULL) {\r
ae315cc2
JC
1528 SHELL_FREE_NON_NULL(NewNode);\r
1529 SHELL_FREE_NON_NULL(Buffer);\r
ff51746b 1530 return (NULL);\r
1531 }\r
a405b86d 1532 NewNode->Buffer = Buffer;\r
1533\r
1534 InsertHeadList(&mFileHandleList.Link, &NewNode->Link);\r
1535 }\r
1536 return ((SHELL_FILE_HANDLE)(Handle));\r
1537}\r
1538\r
1539/**\r
1540 Find the path that was logged with the specified SHELL_FILE_HANDLE.\r
1541\r
1542 @param[in] Handle The SHELL_FILE_HANDLE to query on.\r
1543\r
1544 @return A pointer to the path for the file.\r
1545**/\r
1546CONST CHAR16*\r
1547EFIAPI\r
1548ShellFileHandleGetPath(\r
1549 IN CONST SHELL_FILE_HANDLE Handle\r
1550 )\r
1551{\r
1552 BUFFER_LIST *Node;\r
1553\r
1554 for (Node = (BUFFER_LIST*)GetFirstNode(&mFileHandleList.Link)\r
1555 ; !IsNull(&mFileHandleList.Link, &Node->Link)\r
1556 ; Node = (BUFFER_LIST*)GetNextNode(&mFileHandleList.Link, &Node->Link)\r
1557 ){\r
1558 if ((Node->Buffer) && (((SHELL_COMMAND_FILE_HANDLE *)Node->Buffer)->FileHandle == Handle)){\r
1559 return (((SHELL_COMMAND_FILE_HANDLE *)Node->Buffer)->Path);\r
1560 }\r
1561 }\r
1562 return (NULL);\r
1563}\r
1564\r
1565/**\r
1a63ec8f 1566 Remove a SHELL_FILE_HANDLE from the list of SHELL_FILE_HANDLES.\r
a405b86d 1567\r
1568 @param[in] Handle The SHELL_FILE_HANDLE to remove.\r
1569\r
1570 @retval TRUE The item was removed.\r
1571 @retval FALSE The item was not found.\r
1572**/\r
1573BOOLEAN\r
1574EFIAPI\r
1575ShellFileHandleRemove(\r
1576 IN CONST SHELL_FILE_HANDLE Handle\r
1577 )\r
1578{\r
1579 BUFFER_LIST *Node;\r
1580\r
1581 for (Node = (BUFFER_LIST*)GetFirstNode(&mFileHandleList.Link)\r
1582 ; !IsNull(&mFileHandleList.Link, &Node->Link)\r
1583 ; Node = (BUFFER_LIST*)GetNextNode(&mFileHandleList.Link, &Node->Link)\r
1584 ){\r
1585 if ((Node->Buffer) && (((SHELL_COMMAND_FILE_HANDLE *)Node->Buffer)->FileHandle == Handle)){\r
a405b86d 1586 RemoveEntryList(&Node->Link);\r
ff51746b 1587 SHELL_FREE_NON_NULL(((SHELL_COMMAND_FILE_HANDLE *)Node->Buffer)->Path);\r
1588 SHELL_FREE_NON_NULL(Node->Buffer);\r
1589 SHELL_FREE_NON_NULL(Node);\r
a405b86d 1590 return (TRUE);\r
1591 }\r
1592 }\r
1593 return (FALSE);\r
1594}\r
1595\r
1596/**\r
1597 Function to determine if a SHELL_FILE_HANDLE is at the end of the file.\r
1598\r
1599 This will NOT work on directories.\r
1600\r
1601 If Handle is NULL, then ASSERT.\r
1602\r
1603 @param[in] Handle the file handle\r
1604\r
1605 @retval TRUE the position is at the end of the file\r
1606 @retval FALSE the position is not at the end of the file\r
1607**/\r
1608BOOLEAN\r
1609EFIAPI\r
1610ShellFileHandleEof(\r
1611 IN SHELL_FILE_HANDLE Handle\r
1612 )\r
1613{\r
1614 EFI_FILE_INFO *Info;\r
1615 UINT64 Pos;\r
1616 BOOLEAN RetVal;\r
1617\r
1618 //\r
1619 // ASSERT if Handle is NULL\r
1620 //\r
1621 ASSERT(Handle != NULL);\r
1622\r
1623 gEfiShellProtocol->GetFilePosition(Handle, &Pos);\r
1624 Info = gEfiShellProtocol->GetFileInfo (Handle);\r
1625 ASSERT(Info != NULL);\r
1626 gEfiShellProtocol->SetFilePosition(Handle, Pos);\r
1627\r
1628 if (Info == NULL) {\r
1629 return (FALSE);\r
1630 }\r
1631\r
1632 if (Pos == Info->FileSize) {\r
1633 RetVal = TRUE;\r
1634 } else {\r
1635 RetVal = FALSE;\r
1636 }\r
1637\r
1638 FreePool (Info);\r
1639\r
1640 return (RetVal);\r
1641}\r
1642\r
a405b86d 1643/**\r
1644 Frees any BUFFER_LIST defined type.\r
1a63ec8f 1645\r
1646 @param[in] List The BUFFER_LIST object to free.\r
a405b86d 1647**/\r
1648VOID\r
1649EFIAPI\r
1650FreeBufferList (\r
1651 IN BUFFER_LIST *List\r
1652 )\r
1653{\r
1654 BUFFER_LIST *BufferListEntry;\r
1655\r
1656 if (List == NULL){\r
1657 return;\r
1658 }\r
1659 //\r
1660 // enumerate through the buffer list and free all memory\r
1661 //\r
1662 for ( BufferListEntry = ( BUFFER_LIST *)GetFirstNode(&List->Link)\r
1663 ; !IsListEmpty (&List->Link)\r
1664 ; BufferListEntry = (BUFFER_LIST *)GetFirstNode(&List->Link)\r
1665 ){\r
1666 RemoveEntryList(&BufferListEntry->Link);\r
a405b86d 1667 if (BufferListEntry->Buffer != NULL) {\r
1668 FreePool(BufferListEntry->Buffer);\r
1669 }\r
1670 FreePool(BufferListEntry);\r
1671 }\r
1672}\r
1673\r