]> git.proxmox.com Git - mirror_edk2.git/blame - ShellPkg/Library/UefiShellCommandLib/UefiShellCommandLib.c
Fix the use of ASSERT and other fixes to memory allocation failures (like free before...
[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
e71cb452 4 (C) Copyright 2013-2014, Hewlett-Packard Development Company, L.P.\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
cf812a20
JC
218 Find a dynamic command protocol instance given a command name string\r
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
280 return (ShellCommandFindDynamicCommand(CommandString) != NULL);\r
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
780\r
781 //\r
782 // Asserts for NULL\r
783 //\r
784 ASSERT(Command != NULL);\r
785 ASSERT(Alias != NULL);\r
786\r
787 //\r
788 // allocate memory for new struct\r
789 //\r
1a63ec8f 790 Node = AllocateZeroPool(sizeof(ALIAS_LIST));\r
a405b86d 791 ASSERT(Node != NULL);\r
1a63ec8f 792 Node->CommandString = AllocateZeroPool(StrSize(Command));\r
793 Node->Alias = AllocateZeroPool(StrSize(Alias));\r
a405b86d 794 ASSERT(Node->CommandString != NULL);\r
795 ASSERT(Node->Alias != NULL);\r
796\r
797 //\r
798 // populate the new struct\r
799 //\r
800 StrCpy(Node->CommandString, Command);\r
801 StrCpy(Node->Alias , Alias );\r
802\r
803 //\r
804 // add the new struct to the list\r
805 //\r
806 InsertTailList (&mAliasList.Link, &Node->Link);\r
807\r
808 return (RETURN_SUCCESS);\r
809}\r
810\r
811/**\r
812 Get the list of all shell alias commands. This is a linked list\r
813 (via LIST_ENTRY structure). enumerate through it using the BaseLib linked\r
814 list functions. do not modify the values.\r
815\r
816 @return a Linked list of all requested shell alias'.\r
817**/\r
818CONST ALIAS_LIST*\r
819EFIAPI\r
820ShellCommandGetInitAliasList (\r
821 VOID\r
822 )\r
823{\r
824 return (&mAliasList);\r
825}\r
826\r
827/**\r
1a63ec8f 828 Determine if a given alias is on the list of built in alias'.\r
a405b86d 829\r
830 @param[in] Alias The alias to test for\r
831\r
832 @retval TRUE The alias is a built in alias\r
833 @retval FALSE The alias is not a built in alias\r
834**/\r
835BOOLEAN\r
836EFIAPI\r
837ShellCommandIsOnAliasList(\r
838 IN CONST CHAR16 *Alias\r
839 )\r
840{\r
841 ALIAS_LIST *Node;\r
842\r
843 //\r
844 // assert for NULL parameter\r
845 //\r
846 ASSERT(Alias != NULL);\r
847\r
848 //\r
849 // check for the Alias\r
850 //\r
851 for ( Node = (ALIAS_LIST *)GetFirstNode(&mAliasList.Link)\r
852 ; !IsNull(&mAliasList.Link, &Node->Link)\r
853 ; Node = (ALIAS_LIST *)GetNextNode(&mAliasList.Link, &Node->Link)\r
854 ){\r
855 ASSERT(Node->CommandString != NULL);\r
856 ASSERT(Node->Alias != NULL);\r
857 if (gUnicodeCollation->StriColl(\r
858 gUnicodeCollation,\r
859 (CHAR16*)Alias,\r
860 Node->CommandString) == 0\r
861 ){\r
862 return (TRUE);\r
863 }\r
864 if (gUnicodeCollation->StriColl(\r
865 gUnicodeCollation,\r
866 (CHAR16*)Alias,\r
867 Node->Alias) == 0\r
868 ){\r
869 return (TRUE);\r
870 }\r
871 }\r
872 return (FALSE);\r
873}\r
874\r
875/**\r
876 Function to determine current state of ECHO. Echo determins if lines from scripts\r
877 and ECHO commands are enabled.\r
878\r
879 @retval TRUE Echo is currently enabled\r
880 @retval FALSE Echo is currently disabled\r
881**/\r
882BOOLEAN\r
883EFIAPI\r
884ShellCommandGetEchoState(\r
885 VOID\r
886 )\r
887{\r
888 return (mEchoState);\r
889}\r
890\r
891/**\r
892 Function to set current state of ECHO. Echo determins if lines from scripts\r
893 and ECHO commands are enabled.\r
894\r
895 If State is TRUE, Echo will be enabled.\r
896 If State is FALSE, Echo will be disabled.\r
1a63ec8f 897\r
898 @param[in] State How to set echo.\r
a405b86d 899**/\r
900VOID\r
901EFIAPI\r
902ShellCommandSetEchoState(\r
903 IN BOOLEAN State\r
904 )\r
905{\r
906 mEchoState = State;\r
907}\r
908\r
909/**\r
910 Indicate that the current shell or script should exit.\r
911\r
b6b22b13 912 @param[in] ScriptOnly TRUE if exiting a script; FALSE otherwise.\r
913 @param[in] ErrorCode The 64 bit error code to return.\r
a405b86d 914**/\r
915VOID\r
916EFIAPI\r
917ShellCommandRegisterExit (\r
b6b22b13 918 IN BOOLEAN ScriptOnly,\r
919 IN CONST UINT64 ErrorCode\r
a405b86d 920 )\r
921{\r
922 mExitRequested = (BOOLEAN)(!mExitRequested);\r
923 if (mExitRequested) {\r
924 mExitScript = ScriptOnly;\r
925 } else {\r
926 mExitScript = FALSE;\r
927 }\r
b6b22b13 928 mExitCode = ErrorCode;\r
a405b86d 929}\r
930\r
931/**\r
932 Retrieve the Exit indicator.\r
933\r
934 @retval TRUE Exit was indicated.\r
935 @retval FALSE Exis was not indicated.\r
936**/\r
937BOOLEAN\r
938EFIAPI\r
939ShellCommandGetExit (\r
940 VOID\r
941 )\r
942{\r
943 return (mExitRequested);\r
944}\r
945\r
b6b22b13 946/**\r
947 Retrieve the Exit code.\r
948\r
949 If ShellCommandGetExit returns FALSE than the return from this is undefined.\r
950\r
951 @return the value passed into RegisterExit.\r
952**/\r
953UINT64\r
954EFIAPI\r
955ShellCommandGetExitCode (\r
956 VOID\r
957 )\r
958{\r
959 return (mExitCode);\r
960}\r
a405b86d 961/**\r
962 Retrieve the Exit script indicator.\r
963\r
964 If ShellCommandGetExit returns FALSE than the return from this is undefined.\r
965\r
966 @retval TRUE ScriptOnly was indicated.\r
967 @retval FALSE ScriptOnly was not indicated.\r
968**/\r
969BOOLEAN\r
970EFIAPI\r
971ShellCommandGetScriptExit (\r
972 VOID\r
973 )\r
974{\r
975 return (mExitScript);\r
976}\r
977\r
978/**\r
979 Function to cleanup all memory from a SCRIPT_FILE structure.\r
980\r
981 @param[in] Script The pointer to the structure to cleanup.\r
982**/\r
983VOID\r
984EFIAPI\r
985DeleteScriptFileStruct (\r
986 IN SCRIPT_FILE *Script\r
987 )\r
988{\r
989 UINT8 LoopVar;\r
0ab85bef 990\r
991 if (Script == NULL) {\r
992 return;\r
993 }\r
994\r
a405b86d 995 for (LoopVar = 0 ; LoopVar < Script->Argc ; LoopVar++) {\r
0ab85bef 996 SHELL_FREE_NON_NULL(Script->Argv[LoopVar]);\r
a405b86d 997 }\r
998 if (Script->Argv != NULL) {\r
0ab85bef 999 SHELL_FREE_NON_NULL(Script->Argv);\r
a405b86d 1000 }\r
1001 Script->CurrentCommand = NULL;\r
1002 while (!IsListEmpty (&Script->CommandList)) {\r
1003 Script->CurrentCommand = (SCRIPT_COMMAND_LIST *)GetFirstNode(&Script->CommandList);\r
1004 if (Script->CurrentCommand != NULL) {\r
1005 RemoveEntryList(&Script->CurrentCommand->Link);\r
1006 if (Script->CurrentCommand->Cl != NULL) {\r
0ab85bef 1007 SHELL_FREE_NON_NULL(Script->CurrentCommand->Cl);\r
a405b86d 1008 }\r
1009 if (Script->CurrentCommand->Data != NULL) {\r
0ab85bef 1010 SHELL_FREE_NON_NULL(Script->CurrentCommand->Data);\r
a405b86d 1011 }\r
0ab85bef 1012 SHELL_FREE_NON_NULL(Script->CurrentCommand);\r
a405b86d 1013 }\r
1014 }\r
0ab85bef 1015 SHELL_FREE_NON_NULL(Script->ScriptName);\r
1016 SHELL_FREE_NON_NULL(Script);\r
a405b86d 1017}\r
1018\r
1019/**\r
1020 Function to return a pointer to the currently running script file object.\r
1021\r
1022 @retval NULL A script file is not currently running.\r
1023 @return A pointer to the current script file object.\r
1024**/\r
1025SCRIPT_FILE*\r
1026EFIAPI\r
1027ShellCommandGetCurrentScriptFile (\r
1028 VOID\r
1029 )\r
1030{\r
1031 SCRIPT_FILE_LIST *List;\r
1032 if (IsListEmpty (&mScriptList.Link)) {\r
1033 return (NULL);\r
1034 }\r
1035 List = ((SCRIPT_FILE_LIST*)GetFirstNode(&mScriptList.Link));\r
1036 return (List->Data);\r
1037}\r
1038\r
1039/**\r
1040 Function to set a new script as the currently running one.\r
1041\r
1042 This function will correctly stack and unstack nested scripts.\r
1043\r
1044 @param[in] Script Pointer to new script information structure. if NULL\r
1045 will remove and de-allocate the top-most Script structure.\r
1046\r
1047 @return A pointer to the current running script file after this\r
1048 change. NULL if removing the final script.\r
1049**/\r
1050SCRIPT_FILE*\r
1051EFIAPI\r
1052ShellCommandSetNewScript (\r
1053 IN SCRIPT_FILE *Script OPTIONAL\r
1054 )\r
1055{\r
1056 SCRIPT_FILE_LIST *Node;\r
1057 if (Script == NULL) {\r
1058 if (IsListEmpty (&mScriptList.Link)) {\r
a405b86d 1059 return (NULL);\r
1060 }\r
1061 Node = (SCRIPT_FILE_LIST *)GetFirstNode(&mScriptList.Link);\r
1062 RemoveEntryList(&Node->Link);\r
1063 DeleteScriptFileStruct(Node->Data);\r
1064 FreePool(Node);\r
1065 } else {\r
1066 Node = AllocateZeroPool(sizeof(SCRIPT_FILE_LIST));\r
0ab85bef 1067 if (Node == NULL) {\r
1068 return (NULL);\r
1069 }\r
a405b86d 1070 Node->Data = Script;\r
1071 InsertHeadList(&mScriptList.Link, &Node->Link);\r
1072 }\r
1073 return (ShellCommandGetCurrentScriptFile());\r
1074}\r
1075\r
1076/**\r
1077 Function to generate the next default mapping name.\r
1078\r
1079 If the return value is not NULL then it must be callee freed.\r
1080\r
1081 @param Type What kind of mapping name to make.\r
1082\r
1083 @retval NULL a memory allocation failed.\r
1084 @return a new map name string\r
1085**/\r
1086CHAR16*\r
1087EFIAPI\r
1088ShellCommandCreateNewMappingName(\r
1089 IN CONST SHELL_MAPPING_TYPE Type\r
1090 )\r
1091{\r
1092 CHAR16 *String;\r
1093 ASSERT(Type < MappingTypeMax);\r
1094\r
1095 String = NULL;\r
1096\r
1097 String = AllocateZeroPool(PcdGet8(PcdShellMapNameLength) * sizeof(String[0]));\r
1098 UnicodeSPrint(\r
1099 String,\r
1100 PcdGet8(PcdShellMapNameLength) * sizeof(String[0]),\r
1101 Type == MappingTypeFileSystem?L"FS%d:":L"BLK%d:",\r
1102 Type == MappingTypeFileSystem?mFsMaxCount++:mBlkMaxCount++);\r
1103\r
1104 return (String);\r
1105}\r
1106\r
1107/**\r
1108 Function to add a map node to the list of map items and update the "path" environment variable (optionally).\r
1109\r
1110 If Path is TRUE (during initialization only), the path environment variable will also be updated to include\r
1111 default paths on the new map name...\r
1112\r
1113 Path should be FALSE when this function is called from the protocol SetMap function.\r
1114\r
1115 @param[in] Name The human readable mapped name.\r
1116 @param[in] DevicePath The Device Path for this map.\r
1117 @param[in] Flags The Flags attribute for this map item.\r
1118 @param[in] Path TRUE to update path, FALSE to skip this step (should only be TRUE during initialization).\r
1119\r
1120 @retval EFI_SUCCESS The addition was sucessful.\r
1121 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.\r
1122 @retval EFI_INVALID_PARAMETER A parameter was invalid.\r
1123**/\r
1124EFI_STATUS\r
1125EFIAPI\r
1126ShellCommandAddMapItemAndUpdatePath(\r
1127 IN CONST CHAR16 *Name,\r
1128 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath,\r
1129 IN CONST UINT64 Flags,\r
1130 IN CONST BOOLEAN Path\r
1131 )\r
1132{\r
1133 EFI_STATUS Status;\r
1134 SHELL_MAP_LIST *MapListNode;\r
1135 CONST CHAR16 *OriginalPath;\r
1136 CHAR16 *NewPath;\r
1137 UINTN NewPathSize;\r
1138\r
1139 NewPathSize = 0;\r
1140 NewPath = NULL;\r
1141 OriginalPath = NULL;\r
1142 Status = EFI_SUCCESS;\r
1143\r
1144 MapListNode = AllocateZeroPool(sizeof(SHELL_MAP_LIST));\r
1145 if (MapListNode == NULL) {\r
1146 Status = EFI_OUT_OF_RESOURCES;\r
1147 } else {\r
1148 MapListNode->Flags = Flags;\r
1149 MapListNode->MapName = AllocateZeroPool(StrSize(Name));\r
1150 MapListNode->DevicePath = DuplicateDevicePath(DevicePath);\r
1151 if ((MapListNode->MapName == NULL) || (MapListNode->DevicePath == NULL)){\r
1152 Status = EFI_OUT_OF_RESOURCES;\r
1153 } else {\r
1154 StrCpy(MapListNode->MapName, Name);\r
1155 InsertTailList(&gShellMapList.Link, &MapListNode->Link);\r
1156 }\r
1157 }\r
1158 if (EFI_ERROR(Status)) {\r
1159 if (MapListNode != NULL) {\r
1160 if (MapListNode->DevicePath != NULL) {\r
1161 FreePool(MapListNode->DevicePath);\r
1162 }\r
1163 if (MapListNode->MapName != NULL) {\r
1164 FreePool(MapListNode->MapName);\r
1165 }\r
1166 FreePool(MapListNode);\r
1167 }\r
1168 } else if (Path) {\r
1169 //\r
1170 // Since there was no error and Path was TRUE\r
1171 // Now add the correct path for that mapping\r
1172 //\r
1173 OriginalPath = gEfiShellProtocol->GetEnv(L"path");\r
1174 ASSERT((NewPath == NULL && NewPathSize == 0) || (NewPath != NULL));\r
1175 if (OriginalPath != NULL) {\r
1176 StrnCatGrow(&NewPath, &NewPathSize, OriginalPath, 0);\r
1177 } else {\r
1178 StrnCatGrow(&NewPath, &NewPathSize, L".\\", 0);\r
1179 }\r
1180 StrnCatGrow(&NewPath, &NewPathSize, L";", 0);\r
1181 StrnCatGrow(&NewPath, &NewPathSize, Name, 0);\r
1182 StrnCatGrow(&NewPath, &NewPathSize, L"\\efi\\tools\\;", 0);\r
1183 StrnCatGrow(&NewPath, &NewPathSize, Name, 0);\r
1184 StrnCatGrow(&NewPath, &NewPathSize, L"\\efi\\boot\\;", 0);\r
1185 StrnCatGrow(&NewPath, &NewPathSize, Name, 0);\r
1186 StrnCatGrow(&NewPath, &NewPathSize, L"\\", 0);\r
1187\r
1188 Status = gEfiShellProtocol->SetEnv(L"path", NewPath, TRUE);\r
1189 ASSERT_EFI_ERROR(Status);\r
1190 FreePool(NewPath);\r
1191 }\r
1192 return (Status);\r
1193}\r
1194\r
1195/**\r
1196 Creates the default map names for each device path in the system with\r
1197 a protocol depending on the Type.\r
1198\r
1199 Creates the consistent map names for each device path in the system with\r
1200 a protocol depending on the Type.\r
1201\r
1202 Note: This will reset all mappings in the system("map -r").\r
1203\r
1204 Also sets up the default path environment variable if Type is FileSystem.\r
1205\r
1206 @retval EFI_SUCCESS All map names were created sucessfully.\r
1207 @retval EFI_NOT_FOUND No protocols were found in the system.\r
1208 @return Error returned from gBS->LocateHandle().\r
1209\r
1210 @sa LocateHandle\r
1211**/\r
1212EFI_STATUS\r
1213EFIAPI\r
1214ShellCommandCreateInitialMappingsAndPaths(\r
1215 VOID\r
1216 )\r
1217{\r
1218 EFI_STATUS Status;\r
1219 EFI_HANDLE *HandleList;\r
1220 UINTN Count;\r
1221 EFI_DEVICE_PATH_PROTOCOL **DevicePathList;\r
1222 CHAR16 *NewDefaultName;\r
1223 CHAR16 *NewConsistName;\r
1224 EFI_DEVICE_PATH_PROTOCOL **ConsistMappingTable;\r
1225 SHELL_MAP_LIST *MapListNode;\r
1226\r
1227 HandleList = NULL;\r
1228\r
1229 //\r
1230 // Reset the static members back to zero\r
1231 //\r
1232 mFsMaxCount = 0;\r
1233 mBlkMaxCount = 0;\r
1234\r
1235 gEfiShellProtocol->SetEnv(L"path", L"", TRUE);\r
1236\r
1237 //\r
1238 // First empty out the existing list.\r
1239 //\r
1240 if (!IsListEmpty(&gShellMapList.Link)) {\r
1241 for ( MapListNode = (SHELL_MAP_LIST *)GetFirstNode(&gShellMapList.Link)\r
1242 ; !IsListEmpty(&gShellMapList.Link)\r
1243 ; MapListNode = (SHELL_MAP_LIST *)GetFirstNode(&gShellMapList.Link)\r
1244 ){\r
1245 RemoveEntryList(&MapListNode->Link);\r
1246 FreePool(MapListNode);\r
1247 } // for loop\r
1248 }\r
1249\r
1250 //\r
1251 // Find each handle with Simple File System\r
1252 //\r
0ab85bef 1253 HandleList = GetHandleListByProtocol(&gEfiSimpleFileSystemProtocolGuid);\r
a405b86d 1254 if (HandleList != NULL) {\r
1255 //\r
1256 // Do a count of the handles\r
1257 //\r
1258 for (Count = 0 ; HandleList[Count] != NULL ; Count++);\r
1259\r
1260 //\r
1261 // Get all Device Paths\r
1262 //\r
1a63ec8f 1263 DevicePathList = AllocateZeroPool(sizeof(EFI_DEVICE_PATH_PROTOCOL*) * Count);\r
a405b86d 1264 ASSERT(DevicePathList != NULL);\r
1265\r
1266 for (Count = 0 ; HandleList[Count] != NULL ; Count++) {\r
1267 DevicePathList[Count] = DevicePathFromHandle(HandleList[Count]);\r
1268 }\r
1269\r
1270 //\r
1271 // Sort all DevicePaths\r
1272 //\r
1273 PerformQuickSort(DevicePathList, Count, sizeof(EFI_DEVICE_PATH_PROTOCOL*), DevicePathCompare);\r
1274\r
1275 ShellCommandConsistMappingInitialize(&ConsistMappingTable);\r
1276 //\r
1277 // Assign new Mappings to all...\r
1278 //\r
1279 for (Count = 0 ; HandleList[Count] != NULL ; Count++) {\r
1280 //\r
1281 // Get default name first\r
1282 //\r
1283 NewDefaultName = ShellCommandCreateNewMappingName(MappingTypeFileSystem);\r
1284 ASSERT(NewDefaultName != NULL);\r
1285 Status = ShellCommandAddMapItemAndUpdatePath(NewDefaultName, DevicePathList[Count], 0, TRUE);\r
1286 ASSERT_EFI_ERROR(Status);\r
1287 FreePool(NewDefaultName);\r
1288\r
1289 //\r
1290 // Now do consistent name\r
1291 //\r
1292 NewConsistName = ShellCommandConsistMappingGenMappingName(DevicePathList[Count], ConsistMappingTable);\r
1293 if (NewConsistName != NULL) {\r
1294 Status = ShellCommandAddMapItemAndUpdatePath(NewConsistName, DevicePathList[Count], 0, FALSE);\r
1295 ASSERT_EFI_ERROR(Status);\r
1296 FreePool(NewConsistName);\r
1297 }\r
1298 }\r
1299\r
1300 ShellCommandConsistMappingUnInitialize(ConsistMappingTable);\r
1301\r
1302 SHELL_FREE_NON_NULL(HandleList);\r
1303 SHELL_FREE_NON_NULL(DevicePathList);\r
1304\r
1305 HandleList = NULL;\r
1306 } else {\r
1307 Count = (UINTN)-1;\r
1308 }\r
1309\r
1310 //\r
1311 // Find each handle with Block Io\r
1312 //\r
0ab85bef 1313 HandleList = GetHandleListByProtocol(&gEfiBlockIoProtocolGuid);\r
a405b86d 1314 if (HandleList != NULL) {\r
1315 for (Count = 0 ; HandleList[Count] != NULL ; Count++);\r
1316\r
1317 //\r
1318 // Get all Device Paths\r
1319 //\r
1a63ec8f 1320 DevicePathList = AllocateZeroPool(sizeof(EFI_DEVICE_PATH_PROTOCOL*) * Count);\r
a405b86d 1321 ASSERT(DevicePathList != NULL);\r
1322\r
1323 for (Count = 0 ; HandleList[Count] != NULL ; Count++) {\r
1324 DevicePathList[Count] = DevicePathFromHandle(HandleList[Count]);\r
1325 }\r
1326\r
1327 //\r
1328 // Sort all DevicePaths\r
1329 //\r
1330 PerformQuickSort(DevicePathList, Count, sizeof(EFI_DEVICE_PATH_PROTOCOL*), DevicePathCompare);\r
1331\r
1332 //\r
1333 // Assign new Mappings to all...\r
1334 //\r
1335 for (Count = 0 ; HandleList[Count] != NULL ; Count++) {\r
1336 //\r
1337 // Get default name first\r
1338 //\r
1339 NewDefaultName = ShellCommandCreateNewMappingName(MappingTypeBlockIo);\r
1340 ASSERT(NewDefaultName != NULL);\r
1341 Status = ShellCommandAddMapItemAndUpdatePath(NewDefaultName, DevicePathList[Count], 0, FALSE);\r
1342 ASSERT_EFI_ERROR(Status);\r
1343 FreePool(NewDefaultName);\r
1344 }\r
1345\r
1346 SHELL_FREE_NON_NULL(HandleList);\r
1347 SHELL_FREE_NON_NULL(DevicePathList);\r
1348 } else if (Count == (UINTN)-1) {\r
1349 return (EFI_NOT_FOUND);\r
1350 }\r
1351\r
1352 return (EFI_SUCCESS);\r
cf812a20
JC
1353}\r
1354\r
1355/**\r
1356 Add mappings for any devices without one. Do not change any existing maps.\r
1357\r
1358 @retval EFI_SUCCESS The operation was successful.\r
1359**/\r
1360EFI_STATUS\r
1361EFIAPI\r
1362ShellCommandUpdateMapping (\r
1363 VOID\r
1364 )\r
1365{\r
1366 EFI_STATUS Status;\r
1367 EFI_HANDLE *HandleList;\r
1368 UINTN Count;\r
1369 EFI_DEVICE_PATH_PROTOCOL **DevicePathList;\r
1370 CHAR16 *NewDefaultName;\r
1371 CHAR16 *NewConsistName;\r
1372 EFI_DEVICE_PATH_PROTOCOL **ConsistMappingTable;\r
1373\r
1374 HandleList = NULL;\r
1375 Status = EFI_SUCCESS;\r
1376\r
1377 //\r
1378 // remove mappings that represent removed devices.\r
1379 //\r
1380\r
1381 //\r
1382 // Find each handle with Simple File System\r
1383 //\r
1384 HandleList = GetHandleListByProtocol(&gEfiSimpleFileSystemProtocolGuid);\r
1385 if (HandleList != NULL) {\r
1386 //\r
1387 // Do a count of the handles\r
1388 //\r
1389 for (Count = 0 ; HandleList[Count] != NULL ; Count++);\r
1390\r
1391 //\r
1392 // Get all Device Paths\r
1393 //\r
1394 DevicePathList = AllocateZeroPool(sizeof(EFI_DEVICE_PATH_PROTOCOL*) * Count);\r
ae315cc2
JC
1395 if (DevicePathList == NULL) {\r
1396 return (EFI_OUT_OF_RESOURCES);\r
1397 }\r
cf812a20
JC
1398\r
1399 for (Count = 0 ; HandleList[Count] != NULL ; Count++) {\r
1400 DevicePathList[Count] = DevicePathFromHandle(HandleList[Count]);\r
1401 }\r
1402\r
1403 //\r
1404 // Sort all DevicePaths\r
1405 //\r
1406 PerformQuickSort(DevicePathList, Count, sizeof(EFI_DEVICE_PATH_PROTOCOL*), DevicePathCompare);\r
1407\r
1408 ShellCommandConsistMappingInitialize(&ConsistMappingTable);\r
1409\r
1410 //\r
1411 // Assign new Mappings to remainders\r
1412 //\r
ae315cc2 1413 for (Count = 0 ; !EFI_ERROR(Status) && HandleList[Count] != NULL && !EFI_ERROR(Status); Count++) {\r
cf812a20
JC
1414 //\r
1415 // Skip ones that already have\r
1416 //\r
1417 if (gEfiShellProtocol->GetMapFromDevicePath(&DevicePathList[Count]) != NULL) {\r
1418 continue;\r
1419 }\r
1420 //\r
1421 // Get default name\r
1422 //\r
1423 NewDefaultName = ShellCommandCreateNewMappingName(MappingTypeFileSystem);\r
ae315cc2
JC
1424 if (NewDefaultName == NULL) {\r
1425 Status = EFI_OUT_OF_RESOURCES;\r
1426 break;\r
1427 }\r
cf812a20
JC
1428\r
1429 //\r
1430 // Call shell protocol SetMap function now...\r
1431 //\r
1432 Status = gEfiShellProtocol->SetMap(DevicePathList[Count], NewDefaultName);\r
1433\r
1434 if (!EFI_ERROR(Status)) {\r
1435 //\r
1436 // Now do consistent name\r
1437 //\r
1438 NewConsistName = ShellCommandConsistMappingGenMappingName(DevicePathList[Count], ConsistMappingTable);\r
1439 if (NewConsistName != NULL) {\r
1440 Status = gEfiShellProtocol->SetMap(DevicePathList[Count], NewConsistName);\r
1441 FreePool(NewConsistName);\r
1442 }\r
1443 }\r
1444\r
1445 FreePool(NewDefaultName);\r
1446 }\r
1447 ShellCommandConsistMappingUnInitialize(ConsistMappingTable);\r
1448 SHELL_FREE_NON_NULL(HandleList);\r
1449 SHELL_FREE_NON_NULL(DevicePathList);\r
1450\r
1451 HandleList = NULL;\r
1452 } else {\r
1453 Count = (UINTN)-1;\r
1454 }\r
1455 //\r
1456 // Do it all over again for gEfiBlockIoProtocolGuid\r
1457 //\r
1458\r
1459 return (Status);\r
1460}\r
1461\r
1462/**\r
1463 Converts a SHELL_FILE_HANDLE to an EFI_FILE_PROTOCOL*.\r
1464\r
1465 @param[in] Handle The SHELL_FILE_HANDLE to convert.\r
a405b86d 1466\r
1467 @return a EFI_FILE_PROTOCOL* representing the same file.\r
1468**/\r
1469EFI_FILE_PROTOCOL*\r
1470EFIAPI\r
1471ConvertShellHandleToEfiFileProtocol(\r
1472 IN CONST SHELL_FILE_HANDLE Handle\r
1473 )\r
1474{\r
1475 return ((EFI_FILE_PROTOCOL*)(Handle));\r
1476}\r
1477\r
1478/**\r
1479 Converts a EFI_FILE_PROTOCOL* to an SHELL_FILE_HANDLE.\r
1480\r
1481 @param[in] Handle The pointer to EFI_FILE_PROTOCOL to convert.\r
1482 @param[in] Path The path to the file for verification.\r
1483\r
ff51746b 1484 @return A SHELL_FILE_HANDLE representing the same file.\r
1485 @retval NULL There was not enough memory.\r
a405b86d 1486**/\r
1487SHELL_FILE_HANDLE\r
1488EFIAPI\r
1489ConvertEfiFileProtocolToShellHandle(\r
1490 IN CONST EFI_FILE_PROTOCOL *Handle,\r
1491 IN CONST CHAR16 *Path\r
1492 )\r
1493{\r
1494 SHELL_COMMAND_FILE_HANDLE *Buffer;\r
1495 BUFFER_LIST *NewNode;\r
1496\r
1497 if (Path != NULL) {\r
1498 Buffer = AllocateZeroPool(sizeof(SHELL_COMMAND_FILE_HANDLE));\r
ff51746b 1499 if (Buffer == NULL) {\r
1500 return (NULL);\r
1501 }\r
1a63ec8f 1502 NewNode = AllocateZeroPool(sizeof(BUFFER_LIST));\r
ff51746b 1503 if (NewNode == NULL) {\r
ae315cc2 1504 SHELL_FREE_NON_NULL(Buffer);\r
ff51746b 1505 return (NULL);\r
1506 }\r
a405b86d 1507 Buffer->FileHandle = (EFI_FILE_PROTOCOL*)Handle;\r
1508 Buffer->Path = StrnCatGrow(&Buffer->Path, NULL, Path, 0);\r
ff51746b 1509 if (Buffer->Path == NULL) {\r
ae315cc2
JC
1510 SHELL_FREE_NON_NULL(NewNode);\r
1511 SHELL_FREE_NON_NULL(Buffer);\r
ff51746b 1512 return (NULL);\r
1513 }\r
a405b86d 1514 NewNode->Buffer = Buffer;\r
1515\r
1516 InsertHeadList(&mFileHandleList.Link, &NewNode->Link);\r
1517 }\r
1518 return ((SHELL_FILE_HANDLE)(Handle));\r
1519}\r
1520\r
1521/**\r
1522 Find the path that was logged with the specified SHELL_FILE_HANDLE.\r
1523\r
1524 @param[in] Handle The SHELL_FILE_HANDLE to query on.\r
1525\r
1526 @return A pointer to the path for the file.\r
1527**/\r
1528CONST CHAR16*\r
1529EFIAPI\r
1530ShellFileHandleGetPath(\r
1531 IN CONST SHELL_FILE_HANDLE Handle\r
1532 )\r
1533{\r
1534 BUFFER_LIST *Node;\r
1535\r
1536 for (Node = (BUFFER_LIST*)GetFirstNode(&mFileHandleList.Link)\r
1537 ; !IsNull(&mFileHandleList.Link, &Node->Link)\r
1538 ; Node = (BUFFER_LIST*)GetNextNode(&mFileHandleList.Link, &Node->Link)\r
1539 ){\r
1540 if ((Node->Buffer) && (((SHELL_COMMAND_FILE_HANDLE *)Node->Buffer)->FileHandle == Handle)){\r
1541 return (((SHELL_COMMAND_FILE_HANDLE *)Node->Buffer)->Path);\r
1542 }\r
1543 }\r
1544 return (NULL);\r
1545}\r
1546\r
1547/**\r
1a63ec8f 1548 Remove a SHELL_FILE_HANDLE from the list of SHELL_FILE_HANDLES.\r
a405b86d 1549\r
1550 @param[in] Handle The SHELL_FILE_HANDLE to remove.\r
1551\r
1552 @retval TRUE The item was removed.\r
1553 @retval FALSE The item was not found.\r
1554**/\r
1555BOOLEAN\r
1556EFIAPI\r
1557ShellFileHandleRemove(\r
1558 IN CONST SHELL_FILE_HANDLE Handle\r
1559 )\r
1560{\r
1561 BUFFER_LIST *Node;\r
1562\r
1563 for (Node = (BUFFER_LIST*)GetFirstNode(&mFileHandleList.Link)\r
1564 ; !IsNull(&mFileHandleList.Link, &Node->Link)\r
1565 ; Node = (BUFFER_LIST*)GetNextNode(&mFileHandleList.Link, &Node->Link)\r
1566 ){\r
1567 if ((Node->Buffer) && (((SHELL_COMMAND_FILE_HANDLE *)Node->Buffer)->FileHandle == Handle)){\r
a405b86d 1568 RemoveEntryList(&Node->Link);\r
ff51746b 1569 SHELL_FREE_NON_NULL(((SHELL_COMMAND_FILE_HANDLE *)Node->Buffer)->Path);\r
1570 SHELL_FREE_NON_NULL(Node->Buffer);\r
1571 SHELL_FREE_NON_NULL(Node);\r
a405b86d 1572 return (TRUE);\r
1573 }\r
1574 }\r
1575 return (FALSE);\r
1576}\r
1577\r
1578/**\r
1579 Function to determine if a SHELL_FILE_HANDLE is at the end of the file.\r
1580\r
1581 This will NOT work on directories.\r
1582\r
1583 If Handle is NULL, then ASSERT.\r
1584\r
1585 @param[in] Handle the file handle\r
1586\r
1587 @retval TRUE the position is at the end of the file\r
1588 @retval FALSE the position is not at the end of the file\r
1589**/\r
1590BOOLEAN\r
1591EFIAPI\r
1592ShellFileHandleEof(\r
1593 IN SHELL_FILE_HANDLE Handle\r
1594 )\r
1595{\r
1596 EFI_FILE_INFO *Info;\r
1597 UINT64 Pos;\r
1598 BOOLEAN RetVal;\r
1599\r
1600 //\r
1601 // ASSERT if Handle is NULL\r
1602 //\r
1603 ASSERT(Handle != NULL);\r
1604\r
1605 gEfiShellProtocol->GetFilePosition(Handle, &Pos);\r
1606 Info = gEfiShellProtocol->GetFileInfo (Handle);\r
1607 ASSERT(Info != NULL);\r
1608 gEfiShellProtocol->SetFilePosition(Handle, Pos);\r
1609\r
1610 if (Info == NULL) {\r
1611 return (FALSE);\r
1612 }\r
1613\r
1614 if (Pos == Info->FileSize) {\r
1615 RetVal = TRUE;\r
1616 } else {\r
1617 RetVal = FALSE;\r
1618 }\r
1619\r
1620 FreePool (Info);\r
1621\r
1622 return (RetVal);\r
1623}\r
1624\r
a405b86d 1625/**\r
1626 Frees any BUFFER_LIST defined type.\r
1a63ec8f 1627\r
1628 @param[in] List The BUFFER_LIST object to free.\r
a405b86d 1629**/\r
1630VOID\r
1631EFIAPI\r
1632FreeBufferList (\r
1633 IN BUFFER_LIST *List\r
1634 )\r
1635{\r
1636 BUFFER_LIST *BufferListEntry;\r
1637\r
1638 if (List == NULL){\r
1639 return;\r
1640 }\r
1641 //\r
1642 // enumerate through the buffer list and free all memory\r
1643 //\r
1644 for ( BufferListEntry = ( BUFFER_LIST *)GetFirstNode(&List->Link)\r
1645 ; !IsListEmpty (&List->Link)\r
1646 ; BufferListEntry = (BUFFER_LIST *)GetFirstNode(&List->Link)\r
1647 ){\r
1648 RemoveEntryList(&BufferListEntry->Link);\r
a405b86d 1649 if (BufferListEntry->Buffer != NULL) {\r
1650 FreePool(BufferListEntry->Buffer);\r
1651 }\r
1652 FreePool(BufferListEntry);\r
1653 }\r
1654}\r
1655\r