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