]> git.proxmox.com Git - mirror_edk2.git/blame - ShellPkg/Library/UefiShellCommandLib/UefiShellCommandLib.c
ShellPkg/ShellCommandLib: add ShellSortFileList()
[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
d65f2cea 4 Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>\r
cf041fd7
RN
5 (C) Copyright 2013-2015 Hewlett-Packard Development Company, L.P.<BR>\r
6 (C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>\r
7\r
56ba3746 8 SPDX-License-Identifier: BSD-2-Clause-Patent\r
a405b86d 9\r
10**/\r
11\r
12#include "UefiShellCommandLib.h"\r
13\r
a405b86d 14// STATIC local variables\r
15STATIC SHELL_COMMAND_INTERNAL_LIST_ENTRY mCommandList;\r
16STATIC SCRIPT_FILE_LIST mScriptList;\r
17STATIC ALIAS_LIST mAliasList;\r
18STATIC BOOLEAN mEchoState;\r
19STATIC BOOLEAN mExitRequested;\r
b6b22b13 20STATIC UINT64 mExitCode;\r
a405b86d 21STATIC BOOLEAN mExitScript;\r
22STATIC CHAR16 *mProfileList;\r
23STATIC UINTN mProfileListSize;\r
24STATIC UINTN mFsMaxCount = 0;\r
25STATIC UINTN mBlkMaxCount = 0;\r
26STATIC BUFFER_LIST mFileHandleList;\r
27\r
3bd89603
LE
28STATIC CONST CHAR8 Hex[] = {\r
29 '0',\r
30 '1',\r
31 '2',\r
32 '3',\r
33 '4',\r
34 '5',\r
35 '6',\r
36 '7',\r
37 '8',\r
38 '9',\r
39 'A',\r
40 'B',\r
41 'C',\r
42 'D',\r
43 'E',\r
44 'F'\r
45};\r
46\r
a405b86d 47// global variables required by library class.\r
a405b86d 48EFI_UNICODE_COLLATION_PROTOCOL *gUnicodeCollation = NULL;\r
a405b86d 49SHELL_MAP_LIST gShellMapList;\r
9cf45187 50SHELL_MAP_LIST *gShellCurMapping = NULL;\r
a405b86d 51\r
52CONST CHAR16* SupportLevel[] = {\r
53 L"Minimal",\r
54 L"Scripting",\r
55 L"Basic",\r
56 L"Interactive"\r
57};\r
58\r
59/**\r
60 Function to make sure that the global protocol pointers are valid.\r
61 must be called after constructor before accessing the pointers.\r
62**/\r
63EFI_STATUS\r
64EFIAPI\r
65CommandInit(\r
66 VOID\r
67 )\r
68{\r
d65f2cea
RN
69 UINTN NumHandles;\r
70 EFI_HANDLE *Handles;\r
71 EFI_UNICODE_COLLATION_PROTOCOL *Uc;\r
72 CHAR8 *BestLanguage;\r
73 UINTN Index;\r
74 EFI_STATUS Status;\r
75 CHAR8 *PlatformLang;\r
ba0014b9 76\r
a405b86d 77 if (gUnicodeCollation == NULL) {\r
8b8e9158
HS
78\r
79 GetEfiGlobalVariable2 (EFI_PLATFORM_LANG_VARIABLE_NAME, (VOID**)&PlatformLang, NULL);\r
80\r
d65f2cea
RN
81 Status = gBS->LocateHandleBuffer (\r
82 ByProtocol,\r
83 &gEfiUnicodeCollation2ProtocolGuid,\r
84 NULL,\r
85 &NumHandles,\r
86 &Handles\r
87 );\r
88 if (EFI_ERROR (Status)) {\r
89 NumHandles = 0;\r
90 Handles = NULL;\r
91 }\r
92 for (Index = 0; Index < NumHandles; Index++) {\r
93 //\r
94 // Open Unicode Collation Protocol\r
95 //\r
96 Status = gBS->OpenProtocol (\r
97 Handles[Index],\r
98 &gEfiUnicodeCollation2ProtocolGuid,\r
99 (VOID **) &Uc,\r
100 gImageHandle,\r
101 NULL,\r
102 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
103 );\r
104 if (EFI_ERROR (Status)) {\r
105 continue;\r
106 }\r
107\r
8b8e9158
HS
108 //\r
109 // Without clue provided use the first Unicode Collation2 protocol.\r
9e9f0be3
MH
110 // This may happen when PlatformLang is NULL or when no installed Unicode\r
111 // Collation2 protocol instance supports PlatformLang.\r
8b8e9158 112 //\r
9e9f0be3 113 if (gUnicodeCollation == NULL) {\r
8b8e9158 114 gUnicodeCollation = Uc;\r
9e9f0be3
MH
115 }\r
116 if (PlatformLang == NULL) {\r
8b8e9158
HS
117 break;\r
118 }\r
119\r
d65f2cea
RN
120 //\r
121 // Find the best matching matching language from the supported languages\r
ba0014b9 122 // of Unicode Collation2 protocol.\r
d65f2cea
RN
123 //\r
124 BestLanguage = GetBestLanguage (\r
125 Uc->SupportedLanguages,\r
126 FALSE,\r
127 PlatformLang,\r
128 NULL\r
129 );\r
130 if (BestLanguage != NULL) {\r
131 FreePool (BestLanguage);\r
132 gUnicodeCollation = Uc;\r
133 break;\r
134 }\r
135 }\r
136 if (Handles != NULL) {\r
137 FreePool (Handles);\r
a405b86d 138 }\r
8b8e9158
HS
139 if (PlatformLang != NULL) {\r
140 FreePool (PlatformLang);\r
141 }\r
a405b86d 142 }\r
d65f2cea
RN
143\r
144 return (gUnicodeCollation == NULL) ? EFI_UNSUPPORTED : EFI_SUCCESS;\r
a405b86d 145}\r
146\r
147/**\r
148 Constructor for the Shell Command library.\r
149\r
150 Initialize the library and determine if the underlying is a UEFI Shell 2.0 or an EFI shell.\r
151\r
152 @param ImageHandle the image handle of the process\r
153 @param SystemTable the EFI System Table pointer\r
154\r
155 @retval EFI_SUCCESS the initialization was complete sucessfully\r
156**/\r
157RETURN_STATUS\r
158EFIAPI\r
159ShellCommandLibConstructor (\r
160 IN EFI_HANDLE ImageHandle,\r
161 IN EFI_SYSTEM_TABLE *SystemTable\r
162 )\r
163{\r
164 EFI_STATUS Status;\r
165 InitializeListHead(&gShellMapList.Link);\r
166 InitializeListHead(&mCommandList.Link);\r
167 InitializeListHead(&mAliasList.Link);\r
168 InitializeListHead(&mScriptList.Link);\r
169 InitializeListHead(&mFileHandleList.Link);\r
170 mEchoState = TRUE;\r
171\r
172 mExitRequested = FALSE;\r
173 mExitScript = FALSE;\r
174 mProfileListSize = 0;\r
175 mProfileList = NULL;\r
176\r
d65f2cea
RN
177 Status = CommandInit ();\r
178 if (EFI_ERROR (Status)) {\r
179 return EFI_DEVICE_ERROR;\r
a405b86d 180 }\r
181\r
182 return (RETURN_SUCCESS);\r
183}\r
184\r
4f67c7ff
JC
185/**\r
186 Frees list of file handles.\r
187\r
188 @param[in] List The list to free.\r
189**/\r
190VOID\r
4f67c7ff
JC
191FreeFileHandleList (\r
192 IN BUFFER_LIST *List\r
193 )\r
194{\r
195 BUFFER_LIST *BufferListEntry;\r
196\r
197 if (List == NULL){\r
198 return;\r
199 }\r
200 //\r
201 // enumerate through the buffer list and free all memory\r
202 //\r
203 for ( BufferListEntry = ( BUFFER_LIST *)GetFirstNode(&List->Link)\r
204 ; !IsListEmpty (&List->Link)\r
205 ; BufferListEntry = (BUFFER_LIST *)GetFirstNode(&List->Link)\r
206 ){\r
207 RemoveEntryList(&BufferListEntry->Link);\r
208 ASSERT(BufferListEntry->Buffer != NULL);\r
209 SHELL_FREE_NON_NULL(((SHELL_COMMAND_FILE_HANDLE*)(BufferListEntry->Buffer))->Path);\r
210 SHELL_FREE_NON_NULL(BufferListEntry->Buffer);\r
211 SHELL_FREE_NON_NULL(BufferListEntry);\r
212 }\r
213}\r
214\r
a405b86d 215/**\r
216 Destructor for the library. free any resources.\r
217\r
218 @param ImageHandle the image handle of the process\r
219 @param SystemTable the EFI System Table pointer\r
220\r
221 @retval RETURN_SUCCESS this function always returns success\r
222**/\r
223RETURN_STATUS\r
224EFIAPI\r
225ShellCommandLibDestructor (\r
226 IN EFI_HANDLE ImageHandle,\r
227 IN EFI_SYSTEM_TABLE *SystemTable\r
228 )\r
229{\r
230 SHELL_COMMAND_INTERNAL_LIST_ENTRY *Node;\r
3f869579 231 ALIAS_LIST *Node2;\r
a405b86d 232 SCRIPT_FILE_LIST *Node3;\r
233 SHELL_MAP_LIST *MapNode;\r
234 //\r
235 // enumerate throught the list and free all the memory\r
236 //\r
237 while (!IsListEmpty (&mCommandList.Link)) {\r
238 Node = (SHELL_COMMAND_INTERNAL_LIST_ENTRY *)GetFirstNode(&mCommandList.Link);\r
239 RemoveEntryList(&Node->Link);\r
240 SHELL_FREE_NON_NULL(Node->CommandString);\r
241 FreePool(Node);\r
242 DEBUG_CODE(Node = NULL;);\r
243 }\r
244\r
245 //\r
3f869579 246 // enumerate through the alias list and free all memory\r
a405b86d 247 //\r
248 while (!IsListEmpty (&mAliasList.Link)) {\r
3f869579 249 Node2 = (ALIAS_LIST *)GetFirstNode(&mAliasList.Link);\r
a405b86d 250 RemoveEntryList(&Node2->Link);\r
251 SHELL_FREE_NON_NULL(Node2->CommandString);\r
3f869579 252 SHELL_FREE_NON_NULL(Node2->Alias);\r
253 SHELL_FREE_NON_NULL(Node2);\r
a405b86d 254 DEBUG_CODE(Node2 = NULL;);\r
255 }\r
256\r
257 //\r
258 // enumerate throught the list and free all the memory\r
259 //\r
260 while (!IsListEmpty (&mScriptList.Link)) {\r
261 Node3 = (SCRIPT_FILE_LIST *)GetFirstNode(&mScriptList.Link);\r
262 RemoveEntryList(&Node3->Link);\r
263 DeleteScriptFileStruct(Node3->Data);\r
264 FreePool(Node3);\r
265 }\r
266\r
267 //\r
268 // enumerate throught the mappings list and free all the memory\r
269 //\r
270 if (!IsListEmpty(&gShellMapList.Link)) {\r
271 for (MapNode = (SHELL_MAP_LIST *)GetFirstNode(&gShellMapList.Link)\r
272 ; !IsListEmpty (&gShellMapList.Link)\r
273 ; MapNode = (SHELL_MAP_LIST *)GetFirstNode(&gShellMapList.Link)\r
274 ){\r
275 ASSERT(MapNode != NULL);\r
276 RemoveEntryList(&MapNode->Link);\r
277 SHELL_FREE_NON_NULL(MapNode->DevicePath);\r
278 SHELL_FREE_NON_NULL(MapNode->MapName);\r
279 SHELL_FREE_NON_NULL(MapNode->CurrentDirectoryPath);\r
280 FreePool(MapNode);\r
281 }\r
282 }\r
283 if (!IsListEmpty(&mFileHandleList.Link)){\r
4f67c7ff 284 FreeFileHandleList(&mFileHandleList);\r
a405b86d 285 }\r
286\r
287 if (mProfileList != NULL) {\r
288 FreePool(mProfileList);\r
289 }\r
290\r
1a63ec8f 291 gUnicodeCollation = NULL;\r
9cf45187 292 gShellCurMapping = NULL;\r
1a63ec8f 293\r
a405b86d 294 return (RETURN_SUCCESS);\r
295}\r
296\r
297/**\r
f5ba4007 298 Find a dynamic command protocol instance given a command name string.\r
cf812a20
JC
299\r
300 @param CommandString the command name string\r
301\r
302 @return instance the command protocol instance, if dynamic command instance found\r
303 @retval NULL no dynamic command protocol instance found for name\r
304**/\r
305CONST EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL *\r
cf812a20
JC
306ShellCommandFindDynamicCommand (\r
307 IN CONST CHAR16 *CommandString\r
308 )\r
309{\r
310 EFI_STATUS Status;\r
311 EFI_HANDLE *CommandHandleList;\r
312 EFI_HANDLE *NextCommand;\r
313 EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL *DynamicCommand;\r
314\r
315 CommandHandleList = GetHandleListByProtocol(&gEfiShellDynamicCommandProtocolGuid);\r
316 if (CommandHandleList == NULL) {\r
317 //\r
318 // not found or out of resources\r
319 //\r
ba0014b9 320 return NULL;\r
cf812a20
JC
321 }\r
322\r
323 for (NextCommand = CommandHandleList; *NextCommand != NULL; NextCommand++) {\r
324 Status = gBS->HandleProtocol(\r
325 *NextCommand,\r
326 &gEfiShellDynamicCommandProtocolGuid,\r
327 (VOID **)&DynamicCommand\r
328 );\r
329\r
330 if (EFI_ERROR(Status)) {\r
331 continue;\r
332 }\r
333\r
334 if (gUnicodeCollation->StriColl(\r
335 gUnicodeCollation,\r
336 (CHAR16*)CommandString,\r
ba0014b9 337 (CHAR16*)DynamicCommand->CommandName) == 0\r
cf812a20
JC
338 ){\r
339 FreePool(CommandHandleList);\r
340 return (DynamicCommand);\r
341 }\r
342 }\r
343\r
344 FreePool(CommandHandleList);\r
345 return (NULL);\r
346}\r
347\r
348/**\r
349 Checks if a command exists as a dynamic command protocol instance\r
a405b86d 350\r
351 @param[in] CommandString The command string to check for on the list.\r
352**/\r
353BOOLEAN\r
cf812a20
JC
354ShellCommandDynamicCommandExists (\r
355 IN CONST CHAR16 *CommandString\r
356 )\r
357{\r
f5ba4007 358 return (BOOLEAN) ((ShellCommandFindDynamicCommand(CommandString) != NULL));\r
cf812a20
JC
359}\r
360\r
361/**\r
362 Checks if a command is already on the internal command list.\r
363\r
364 @param[in] CommandString The command string to check for on the list.\r
365**/\r
366BOOLEAN\r
cf812a20
JC
367ShellCommandIsCommandOnInternalList(\r
368 IN CONST CHAR16 *CommandString\r
a405b86d 369 )\r
370{\r
371 SHELL_COMMAND_INTERNAL_LIST_ENTRY *Node;\r
372\r
373 //\r
374 // assert for NULL parameter\r
375 //\r
376 ASSERT(CommandString != NULL);\r
377\r
378 //\r
379 // check for the command\r
380 //\r
381 for ( Node = (SHELL_COMMAND_INTERNAL_LIST_ENTRY *)GetFirstNode(&mCommandList.Link)\r
382 ; !IsNull(&mCommandList.Link, &Node->Link)\r
383 ; Node = (SHELL_COMMAND_INTERNAL_LIST_ENTRY *)GetNextNode(&mCommandList.Link, &Node->Link)\r
384 ){\r
385 ASSERT(Node->CommandString != NULL);\r
386 if (gUnicodeCollation->StriColl(\r
387 gUnicodeCollation,\r
388 (CHAR16*)CommandString,\r
389 Node->CommandString) == 0\r
390 ){\r
391 return (TRUE);\r
392 }\r
393 }\r
394 return (FALSE);\r
395}\r
396\r
397/**\r
cf812a20
JC
398 Checks if a command exists, either internally or through the dynamic command protocol.\r
399\r
400 @param[in] CommandString The command string to check for on the list.\r
401**/\r
402BOOLEAN\r
403EFIAPI\r
404ShellCommandIsCommandOnList(\r
405 IN CONST CHAR16 *CommandString\r
406 )\r
407{\r
408 if (ShellCommandIsCommandOnInternalList(CommandString)) {\r
409 return TRUE;\r
410 }\r
411\r
412 return ShellCommandDynamicCommandExists(CommandString);\r
413}\r
414\r
415/**\r
416 Get the help text for a dynamic command.\r
417\r
418 @param[in] CommandString The command name.\r
419\r
420 @retval NULL No help text was found.\r
421 @return String of help text. Caller required to free.\r
422**/\r
423CHAR16*\r
cf812a20
JC
424ShellCommandGetDynamicCommandHelp(\r
425 IN CONST CHAR16 *CommandString\r
426 )\r
427{\r
428 EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL *DynamicCommand;\r
429\r
430 DynamicCommand = (EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL *)ShellCommandFindDynamicCommand(CommandString);\r
431 if (DynamicCommand == NULL) {\r
432 return (NULL);\r
433 }\r
434\r
435 //\r
436 // TODO: how to get proper language?\r
437 //\r
ba0014b9 438 return DynamicCommand->GetHelp(DynamicCommand, "en");\r
cf812a20
JC
439}\r
440\r
441/**\r
442 Get the help text for an internal command.\r
a405b86d 443\r
444 @param[in] CommandString The command name.\r
445\r
446 @retval NULL No help text was found.\r
447 @return String of help text. Caller reuiqred to free.\r
448**/\r
449CHAR16*\r
cf812a20 450ShellCommandGetInternalCommandHelp(\r
a405b86d 451 IN CONST CHAR16 *CommandString\r
452 )\r
453{\r
454 SHELL_COMMAND_INTERNAL_LIST_ENTRY *Node;\r
455\r
456 //\r
457 // assert for NULL parameter\r
458 //\r
459 ASSERT(CommandString != NULL);\r
460\r
461 //\r
462 // check for the command\r
463 //\r
464 for ( Node = (SHELL_COMMAND_INTERNAL_LIST_ENTRY *)GetFirstNode(&mCommandList.Link)\r
465 ; !IsNull(&mCommandList.Link, &Node->Link)\r
466 ; Node = (SHELL_COMMAND_INTERNAL_LIST_ENTRY *)GetNextNode(&mCommandList.Link, &Node->Link)\r
467 ){\r
468 ASSERT(Node->CommandString != NULL);\r
469 if (gUnicodeCollation->StriColl(\r
470 gUnicodeCollation,\r
471 (CHAR16*)CommandString,\r
472 Node->CommandString) == 0\r
473 ){\r
474 return (HiiGetString(Node->HiiHandle, Node->ManFormatHelp, NULL));\r
475 }\r
476 }\r
477 return (NULL);\r
478}\r
479\r
cf812a20
JC
480/**\r
481 Get the help text for a command.\r
482\r
483 @param[in] CommandString The command name.\r
484\r
485 @retval NULL No help text was found.\r
486 @return String of help text.Caller reuiqred to free.\r
487**/\r
488CHAR16*\r
489EFIAPI\r
490ShellCommandGetCommandHelp (\r
491 IN CONST CHAR16 *CommandString\r
492 )\r
493{\r
494 CHAR16 *HelpStr;\r
495 HelpStr = ShellCommandGetInternalCommandHelp(CommandString);\r
496\r
497 if (HelpStr == NULL) {\r
498 HelpStr = ShellCommandGetDynamicCommandHelp(CommandString);\r
499 }\r
500\r
501 return HelpStr;\r
502}\r
503\r
504\r
a405b86d 505/**\r
506 Registers handlers of type SHELL_RUN_COMMAND and\r
507 SHELL_GET_MAN_FILENAME for each shell command.\r
508\r
509 If the ShellSupportLevel is greater than the value of the\r
510 PcdShellSupportLevel then return RETURN_UNSUPPORTED.\r
511\r
512 Registers the handlers specified by GetHelpInfoHandler and CommandHandler\r
513 with the command specified by CommandString. If the command named by\r
514 CommandString has already been registered, then return\r
515 RETURN_ALREADY_STARTED.\r
516\r
517 If there are not enough resources available to register the handlers then\r
518 RETURN_OUT_OF_RESOURCES is returned.\r
519\r
520 If CommandString is NULL, then ASSERT().\r
521 If GetHelpInfoHandler is NULL, then ASSERT().\r
522 If CommandHandler is NULL, then ASSERT().\r
523 If ProfileName is NULL, then ASSERT().\r
524\r
525 @param[in] CommandString Pointer to the command name. This is the\r
526 name to look for on the command line in\r
527 the shell.\r
528 @param[in] CommandHandler Pointer to a function that runs the\r
529 specified command.\r
530 @param[in] GetManFileName Pointer to a function that provides man\r
531 filename.\r
532 @param[in] ShellMinSupportLevel minimum Shell Support Level which has this\r
533 function.\r
534 @param[in] ProfileName profile name to require for support of this\r
535 function.\r
536 @param[in] CanAffectLE indicates whether this command's return value\r
537 can change the LASTERROR environment variable.\r
538 @param[in] HiiHandle Handle of this command's HII entry.\r
539 @param[in] ManFormatHelp HII locator for the help text.\r
540\r
541 @retval RETURN_SUCCESS The handlers were registered.\r
542 @retval RETURN_OUT_OF_RESOURCES There are not enough resources available to\r
543 register the shell command.\r
544 @retval RETURN_UNSUPPORTED the ShellMinSupportLevel was higher than the\r
545 currently allowed support level.\r
546 @retval RETURN_ALREADY_STARTED The CommandString represents a command that\r
547 is already registered. Only 1 handler set for\r
548 a given command is allowed.\r
549 @sa SHELL_GET_MAN_FILENAME\r
550 @sa SHELL_RUN_COMMAND\r
551**/\r
552RETURN_STATUS\r
553EFIAPI\r
554ShellCommandRegisterCommandName (\r
555 IN CONST CHAR16 *CommandString,\r
556 IN SHELL_RUN_COMMAND CommandHandler,\r
557 IN SHELL_GET_MAN_FILENAME GetManFileName,\r
558 IN UINT32 ShellMinSupportLevel,\r
559 IN CONST CHAR16 *ProfileName,\r
560 IN CONST BOOLEAN CanAffectLE,\r
c44501b3 561 IN CONST EFI_HII_HANDLE HiiHandle,\r
a405b86d 562 IN CONST EFI_STRING_ID ManFormatHelp\r
563 )\r
564{\r
565 SHELL_COMMAND_INTERNAL_LIST_ENTRY *Node;\r
d51088b7 566 SHELL_COMMAND_INTERNAL_LIST_ENTRY *Command;\r
567 SHELL_COMMAND_INTERNAL_LIST_ENTRY *PrevCommand;\r
568 INTN LexicalMatchValue;\r
569\r
570 //\r
571 // Initialize local variables.\r
572 //\r
573 Command = NULL;\r
574 PrevCommand = NULL;\r
575 LexicalMatchValue = 0;\r
a405b86d 576\r
577 //\r
578 // ASSERTs for NULL parameters\r
579 //\r
580 ASSERT(CommandString != NULL);\r
581 ASSERT(GetManFileName != NULL);\r
582 ASSERT(CommandHandler != NULL);\r
583 ASSERT(ProfileName != NULL);\r
584\r
585 //\r
586 // check for shell support level\r
587 //\r
588 if (PcdGet8(PcdShellSupportLevel) < ShellMinSupportLevel) {\r
589 return (RETURN_UNSUPPORTED);\r
590 }\r
591\r
592 //\r
593 // check for already on the list\r
594 //\r
595 if (ShellCommandIsCommandOnList(CommandString)) {\r
596 return (RETURN_ALREADY_STARTED);\r
597 }\r
598\r
599 //\r
600 // allocate memory for new struct\r
601 //\r
1a63ec8f 602 Node = AllocateZeroPool(sizeof(SHELL_COMMAND_INTERNAL_LIST_ENTRY));\r
107d05a4
RN
603 if (Node == NULL) {\r
604 return RETURN_OUT_OF_RESOURCES;\r
605 }\r
53173337 606 Node->CommandString = AllocateCopyPool(StrSize(CommandString), CommandString);\r
107d05a4
RN
607 if (Node->CommandString == NULL) {\r
608 FreePool (Node);\r
609 return RETURN_OUT_OF_RESOURCES;\r
610 }\r
a405b86d 611\r
a405b86d 612 Node->GetManFileName = GetManFileName;\r
613 Node->CommandHandler = CommandHandler;\r
614 Node->LastError = CanAffectLE;\r
615 Node->HiiHandle = HiiHandle;\r
616 Node->ManFormatHelp = ManFormatHelp;\r
617\r
618 if ( StrLen(ProfileName)>0\r
619 && ((mProfileList != NULL\r
620 && StrStr(mProfileList, ProfileName) == NULL) || mProfileList == NULL)\r
621 ){\r
622 ASSERT((mProfileList == NULL && mProfileListSize == 0) || (mProfileList != NULL));\r
623 if (mProfileList == NULL) {\r
624 //\r
625 // If this is the first make a leading ';'\r
626 //\r
627 StrnCatGrow(&mProfileList, &mProfileListSize, L";", 0);\r
628 }\r
629 StrnCatGrow(&mProfileList, &mProfileListSize, ProfileName, 0);\r
630 StrnCatGrow(&mProfileList, &mProfileListSize, L";", 0);\r
631 }\r
632\r
633 //\r
d51088b7 634 // Insert a new entry on top of the list\r
635 //\r
636 InsertHeadList (&mCommandList.Link, &Node->Link);\r
637\r
638 //\r
639 // Move a new registered command to its sorted ordered location in the list\r
a405b86d 640 //\r
d51088b7 641 for (Command = (SHELL_COMMAND_INTERNAL_LIST_ENTRY *)GetFirstNode (&mCommandList.Link),\r
642 PrevCommand = (SHELL_COMMAND_INTERNAL_LIST_ENTRY *)GetFirstNode (&mCommandList.Link)\r
643 ; !IsNull (&mCommandList.Link, &Command->Link)\r
644 ; Command = (SHELL_COMMAND_INTERNAL_LIST_ENTRY *)GetNextNode (&mCommandList.Link, &Command->Link)) {\r
645\r
646 //\r
647 // Get Lexical Comparison Value between PrevCommand and Command list entry\r
648 //\r
649 LexicalMatchValue = gUnicodeCollation->StriColl (\r
650 gUnicodeCollation,\r
651 PrevCommand->CommandString,\r
652 Command->CommandString\r
653 );\r
654\r
655 //\r
656 // Swap PrevCommand and Command list entry if PrevCommand list entry\r
657 // is alphabetically greater than Command list entry\r
658 //\r
659 if (LexicalMatchValue > 0){\r
660 Command = (SHELL_COMMAND_INTERNAL_LIST_ENTRY *) SwapListEntries (&PrevCommand->Link, &Command->Link);\r
661 } else if (LexicalMatchValue < 0) {\r
662 //\r
663 // PrevCommand entry is lexically lower than Command entry\r
664 //\r
665 break;\r
666 }\r
667 }\r
a405b86d 668\r
669 return (RETURN_SUCCESS);\r
670}\r
671\r
672/**\r
673 Function to get the current Profile string.\r
674\r
675 @retval NULL There are no installed profiles.\r
676 @return A semi-colon delimited list of profiles.\r
677**/\r
678CONST CHAR16 *\r
679EFIAPI\r
680ShellCommandGetProfileList (\r
681 VOID\r
682 )\r
683{\r
684 return (mProfileList);\r
685}\r
686\r
687/**\r
688 Checks if a command string has been registered for CommandString and if so it runs\r
689 the previously registered handler for that command with the command line.\r
690\r
691 If CommandString is NULL, then ASSERT().\r
692\r
693 If Sections is specified, then each section name listed will be compared in a casesensitive\r
694 manner, to the section names described in Appendix B UEFI Shell 2.0 spec. If the section exists,\r
695 it will be appended to the returned help text. If the section does not exist, no\r
696 information will be returned. If Sections is NULL, then all help text information\r
697 available will be returned.\r
698\r
4ff7e37b
ED
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 @param[in, out] RetVal Pointer to the return vaule from the command handler.\r
a405b86d 702\r
4ff7e37b
ED
703 @param[in, out] CanAffectLE indicates whether this command's return value\r
704 needs to be placed into LASTERROR environment variable.\r
a405b86d 705\r
706 @retval RETURN_SUCCESS The handler was run.\r
707 @retval RETURN_NOT_FOUND The CommandString did not match a registered\r
708 command name.\r
709 @sa SHELL_RUN_COMMAND\r
710**/\r
711RETURN_STATUS\r
712EFIAPI\r
713ShellCommandRunCommandHandler (\r
714 IN CONST CHAR16 *CommandString,\r
715 IN OUT SHELL_STATUS *RetVal,\r
716 IN OUT BOOLEAN *CanAffectLE OPTIONAL\r
717 )\r
718{\r
cf812a20
JC
719 SHELL_COMMAND_INTERNAL_LIST_ENTRY *Node;\r
720 EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL *DynamicCommand;\r
a405b86d 721\r
722 //\r
723 // assert for NULL parameters\r
724 //\r
725 ASSERT(CommandString != NULL);\r
726\r
727 //\r
728 // check for the command\r
729 //\r
730 for ( Node = (SHELL_COMMAND_INTERNAL_LIST_ENTRY *)GetFirstNode(&mCommandList.Link)\r
731 ; !IsNull(&mCommandList.Link, &Node->Link)\r
732 ; Node = (SHELL_COMMAND_INTERNAL_LIST_ENTRY *)GetNextNode(&mCommandList.Link, &Node->Link)\r
733 ){\r
734 ASSERT(Node->CommandString != NULL);\r
735 if (gUnicodeCollation->StriColl(\r
736 gUnicodeCollation,\r
737 (CHAR16*)CommandString,\r
738 Node->CommandString) == 0\r
cf812a20 739 ){\r
a405b86d 740 if (CanAffectLE != NULL) {\r
741 *CanAffectLE = Node->LastError;\r
742 }\r
743 if (RetVal != NULL) {\r
744 *RetVal = Node->CommandHandler(NULL, gST);\r
745 } else {\r
746 Node->CommandHandler(NULL, gST);\r
747 }\r
748 return (RETURN_SUCCESS);\r
749 }\r
750 }\r
cf812a20
JC
751\r
752 //\r
753 // An internal command was not found, try to find a dynamic command\r
754 //\r
755 DynamicCommand = (EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL *)ShellCommandFindDynamicCommand(CommandString);\r
756 if (DynamicCommand != NULL) {\r
757 if (RetVal != NULL) {\r
758 *RetVal = DynamicCommand->Handler(DynamicCommand, gST, gEfiShellParametersProtocol, gEfiShellProtocol);\r
759 } else {\r
760 DynamicCommand->Handler(DynamicCommand, gST, gEfiShellParametersProtocol, gEfiShellProtocol);\r
761 }\r
762 return (RETURN_SUCCESS);\r
763 }\r
764\r
a405b86d 765 return (RETURN_NOT_FOUND);\r
766}\r
767\r
768/**\r
769 Checks if a command string has been registered for CommandString and if so it\r
770 returns the MAN filename specified for that command.\r
771\r
772 If CommandString is NULL, then ASSERT().\r
773\r
774 @param[in] CommandString Pointer to the command name. This is the name\r
775 found on the command line in the shell.\\r
776\r
777 @retval NULL the commandString was not a registered command.\r
778 @return other the name of the MAN file.\r
779 @sa SHELL_GET_MAN_FILENAME\r
780**/\r
781CONST CHAR16*\r
782EFIAPI\r
783ShellCommandGetManFileNameHandler (\r
784 IN CONST CHAR16 *CommandString\r
785 )\r
786{\r
787 SHELL_COMMAND_INTERNAL_LIST_ENTRY *Node;\r
788\r
789 //\r
790 // assert for NULL parameters\r
791 //\r
792 ASSERT(CommandString != NULL);\r
793\r
794 //\r
795 // check for the command\r
796 //\r
797 for ( Node = (SHELL_COMMAND_INTERNAL_LIST_ENTRY *)GetFirstNode(&mCommandList.Link)\r
798 ; !IsNull(&mCommandList.Link, &Node->Link)\r
799 ; Node = (SHELL_COMMAND_INTERNAL_LIST_ENTRY *)GetNextNode(&mCommandList.Link, &Node->Link)\r
800 ){\r
801 ASSERT(Node->CommandString != NULL);\r
802 if (gUnicodeCollation->StriColl(\r
803 gUnicodeCollation,\r
804 (CHAR16*)CommandString,\r
805 Node->CommandString) == 0\r
806 ){\r
807 return (Node->GetManFileName());\r
808 }\r
809 }\r
810 return (NULL);\r
811}\r
812\r
813/**\r
814 Get the list of all available shell internal commands. This is a linked list\r
815 (via LIST_ENTRY structure). enumerate through it using the BaseLib linked\r
816 list functions. do not modify the values.\r
817\r
1a63ec8f 818 @param[in] Sort TRUE to alphabetically sort the values first. FALSE otherwise.\r
819\r
a405b86d 820 @return a Linked list of all available shell commands.\r
821**/\r
822CONST COMMAND_LIST*\r
823EFIAPI\r
824ShellCommandGetCommandList (\r
1a63ec8f 825 IN CONST BOOLEAN Sort\r
a405b86d 826 )\r
827{\r
1a63ec8f 828// if (!Sort) {\r
829// return ((COMMAND_LIST*)(&mCommandList));\r
830// }\r
a405b86d 831 return ((COMMAND_LIST*)(&mCommandList));\r
832}\r
833\r
834/**\r
835 Registers aliases to be set as part of the initialization of the shell application.\r
836\r
837 If Command is NULL, then ASSERT().\r
838 If Alias is NULL, then ASSERT().\r
839\r
840 @param[in] Command Pointer to the Command\r
841 @param[in] Alias Pointer to Alias\r
842\r
843 @retval RETURN_SUCCESS The handlers were registered.\r
844 @retval RETURN_OUT_OF_RESOURCES There are not enough resources available to\r
845 register the shell command.\r
846**/\r
847RETURN_STATUS\r
848EFIAPI\r
849ShellCommandRegisterAlias (\r
850 IN CONST CHAR16 *Command,\r
851 IN CONST CHAR16 *Alias\r
852 )\r
853{\r
854 ALIAS_LIST *Node;\r
4ba9b812 855 ALIAS_LIST *CommandAlias;\r
ba0014b9 856 ALIAS_LIST *PrevCommandAlias;\r
4ba9b812 857 INTN LexicalMatchValue;\r
a405b86d 858\r
859 //\r
860 // Asserts for NULL\r
861 //\r
862 ASSERT(Command != NULL);\r
863 ASSERT(Alias != NULL);\r
864\r
865 //\r
866 // allocate memory for new struct\r
867 //\r
1a63ec8f 868 Node = AllocateZeroPool(sizeof(ALIAS_LIST));\r
107d05a4
RN
869 if (Node == NULL) {\r
870 return RETURN_OUT_OF_RESOURCES;\r
871 }\r
53173337 872 Node->CommandString = AllocateCopyPool(StrSize(Command), Command);\r
107d05a4
RN
873 if (Node->CommandString == NULL) {\r
874 FreePool (Node);\r
875 return RETURN_OUT_OF_RESOURCES;\r
876 }\r
53173337 877 Node->Alias = AllocateCopyPool(StrSize(Alias), Alias);\r
107d05a4
RN
878 if (Node->Alias == NULL) {\r
879 FreePool (Node->CommandString);\r
880 FreePool (Node);\r
881 return RETURN_OUT_OF_RESOURCES;\r
882 }\r
a405b86d 883\r
4ba9b812
TS
884 InsertHeadList (&mAliasList.Link, &Node->Link);\r
885\r
a405b86d 886 //\r
4ba9b812 887 // Move a new pre-defined registered alias to its sorted ordered location in the list\r
a405b86d 888 //\r
4ba9b812
TS
889 for ( CommandAlias = (ALIAS_LIST *)GetFirstNode (&mAliasList.Link),\r
890 PrevCommandAlias = (ALIAS_LIST *)GetFirstNode (&mAliasList.Link)\r
891 ; !IsNull (&mAliasList.Link, &CommandAlias->Link)\r
892 ; CommandAlias = (ALIAS_LIST *) GetNextNode (&mAliasList.Link, &CommandAlias->Link) ) {\r
893 //\r
894 // Get Lexical comparison value between PrevCommandAlias and CommandAlias List Entry\r
895 //\r
896 LexicalMatchValue = gUnicodeCollation->StriColl (\r
897 gUnicodeCollation,\r
898 PrevCommandAlias->Alias,\r
899 CommandAlias->Alias\r
900 );\r
901\r
902 //\r
903 // Swap PrevCommandAlias and CommandAlias list entry if PrevCommandAlias list entry\r
904 // is alphabetically greater than CommandAlias list entry\r
ba0014b9 905 //\r
4ba9b812
TS
906 if (LexicalMatchValue > 0) {\r
907 CommandAlias = (ALIAS_LIST *) SwapListEntries (&PrevCommandAlias->Link, &CommandAlias->Link);\r
908 } else if (LexicalMatchValue < 0) {\r
909 //\r
910 // PrevCommandAlias entry is lexically lower than CommandAlias entry\r
911 //\r
912 break;\r
913 }\r
914 }\r
a405b86d 915\r
916 return (RETURN_SUCCESS);\r
917}\r
918\r
919/**\r
920 Get the list of all shell alias commands. This is a linked list\r
921 (via LIST_ENTRY structure). enumerate through it using the BaseLib linked\r
922 list functions. do not modify the values.\r
923\r
924 @return a Linked list of all requested shell alias'.\r
925**/\r
926CONST ALIAS_LIST*\r
927EFIAPI\r
928ShellCommandGetInitAliasList (\r
929 VOID\r
930 )\r
931{\r
932 return (&mAliasList);\r
933}\r
934\r
935/**\r
1a63ec8f 936 Determine if a given alias is on the list of built in alias'.\r
a405b86d 937\r
938 @param[in] Alias The alias to test for\r
939\r
940 @retval TRUE The alias is a built in alias\r
941 @retval FALSE The alias is not a built in alias\r
942**/\r
943BOOLEAN\r
944EFIAPI\r
945ShellCommandIsOnAliasList(\r
946 IN CONST CHAR16 *Alias\r
947 )\r
948{\r
949 ALIAS_LIST *Node;\r
950\r
951 //\r
952 // assert for NULL parameter\r
953 //\r
954 ASSERT(Alias != NULL);\r
955\r
956 //\r
957 // check for the Alias\r
958 //\r
959 for ( Node = (ALIAS_LIST *)GetFirstNode(&mAliasList.Link)\r
960 ; !IsNull(&mAliasList.Link, &Node->Link)\r
961 ; Node = (ALIAS_LIST *)GetNextNode(&mAliasList.Link, &Node->Link)\r
962 ){\r
963 ASSERT(Node->CommandString != NULL);\r
964 ASSERT(Node->Alias != NULL);\r
965 if (gUnicodeCollation->StriColl(\r
966 gUnicodeCollation,\r
967 (CHAR16*)Alias,\r
968 Node->CommandString) == 0\r
969 ){\r
970 return (TRUE);\r
971 }\r
972 if (gUnicodeCollation->StriColl(\r
973 gUnicodeCollation,\r
974 (CHAR16*)Alias,\r
975 Node->Alias) == 0\r
976 ){\r
977 return (TRUE);\r
978 }\r
979 }\r
980 return (FALSE);\r
981}\r
982\r
983/**\r
cceb4ebd 984 Function to determine current state of ECHO. Echo determines if lines from scripts\r
a405b86d 985 and ECHO commands are enabled.\r
986\r
987 @retval TRUE Echo is currently enabled\r
988 @retval FALSE Echo is currently disabled\r
989**/\r
990BOOLEAN\r
991EFIAPI\r
992ShellCommandGetEchoState(\r
993 VOID\r
994 )\r
995{\r
996 return (mEchoState);\r
997}\r
998\r
999/**\r
cceb4ebd 1000 Function to set current state of ECHO. Echo determines if lines from scripts\r
a405b86d 1001 and ECHO commands are enabled.\r
1002\r
1003 If State is TRUE, Echo will be enabled.\r
1004 If State is FALSE, Echo will be disabled.\r
1a63ec8f 1005\r
1006 @param[in] State How to set echo.\r
a405b86d 1007**/\r
1008VOID\r
1009EFIAPI\r
1010ShellCommandSetEchoState(\r
1011 IN BOOLEAN State\r
1012 )\r
1013{\r
1014 mEchoState = State;\r
1015}\r
1016\r
1017/**\r
1018 Indicate that the current shell or script should exit.\r
1019\r
b6b22b13 1020 @param[in] ScriptOnly TRUE if exiting a script; FALSE otherwise.\r
1021 @param[in] ErrorCode The 64 bit error code to return.\r
a405b86d 1022**/\r
1023VOID\r
1024EFIAPI\r
1025ShellCommandRegisterExit (\r
b6b22b13 1026 IN BOOLEAN ScriptOnly,\r
1027 IN CONST UINT64 ErrorCode\r
a405b86d 1028 )\r
1029{\r
1030 mExitRequested = (BOOLEAN)(!mExitRequested);\r
1031 if (mExitRequested) {\r
1032 mExitScript = ScriptOnly;\r
1033 } else {\r
1034 mExitScript = FALSE;\r
1035 }\r
b6b22b13 1036 mExitCode = ErrorCode;\r
a405b86d 1037}\r
1038\r
1039/**\r
1040 Retrieve the Exit indicator.\r
1041\r
1042 @retval TRUE Exit was indicated.\r
1043 @retval FALSE Exis was not indicated.\r
1044**/\r
1045BOOLEAN\r
1046EFIAPI\r
1047ShellCommandGetExit (\r
1048 VOID\r
1049 )\r
1050{\r
1051 return (mExitRequested);\r
1052}\r
1053\r
b6b22b13 1054/**\r
1055 Retrieve the Exit code.\r
1056\r
1057 If ShellCommandGetExit returns FALSE than the return from this is undefined.\r
1058\r
1059 @return the value passed into RegisterExit.\r
1060**/\r
1061UINT64\r
1062EFIAPI\r
1063ShellCommandGetExitCode (\r
1064 VOID\r
1065 )\r
1066{\r
1067 return (mExitCode);\r
1068}\r
a405b86d 1069/**\r
1070 Retrieve the Exit script indicator.\r
1071\r
1072 If ShellCommandGetExit returns FALSE than the return from this is undefined.\r
1073\r
1074 @retval TRUE ScriptOnly was indicated.\r
1075 @retval FALSE ScriptOnly was not indicated.\r
1076**/\r
1077BOOLEAN\r
1078EFIAPI\r
1079ShellCommandGetScriptExit (\r
1080 VOID\r
1081 )\r
1082{\r
1083 return (mExitScript);\r
1084}\r
1085\r
1086/**\r
1087 Function to cleanup all memory from a SCRIPT_FILE structure.\r
1088\r
1089 @param[in] Script The pointer to the structure to cleanup.\r
1090**/\r
1091VOID\r
1092EFIAPI\r
1093DeleteScriptFileStruct (\r
1094 IN SCRIPT_FILE *Script\r
1095 )\r
1096{\r
1097 UINT8 LoopVar;\r
0ab85bef 1098\r
1099 if (Script == NULL) {\r
1100 return;\r
1101 }\r
1102\r
a405b86d 1103 for (LoopVar = 0 ; LoopVar < Script->Argc ; LoopVar++) {\r
0ab85bef 1104 SHELL_FREE_NON_NULL(Script->Argv[LoopVar]);\r
a405b86d 1105 }\r
1106 if (Script->Argv != NULL) {\r
0ab85bef 1107 SHELL_FREE_NON_NULL(Script->Argv);\r
a405b86d 1108 }\r
1109 Script->CurrentCommand = NULL;\r
1110 while (!IsListEmpty (&Script->CommandList)) {\r
1111 Script->CurrentCommand = (SCRIPT_COMMAND_LIST *)GetFirstNode(&Script->CommandList);\r
1112 if (Script->CurrentCommand != NULL) {\r
1113 RemoveEntryList(&Script->CurrentCommand->Link);\r
1114 if (Script->CurrentCommand->Cl != NULL) {\r
0ab85bef 1115 SHELL_FREE_NON_NULL(Script->CurrentCommand->Cl);\r
a405b86d 1116 }\r
1117 if (Script->CurrentCommand->Data != NULL) {\r
0ab85bef 1118 SHELL_FREE_NON_NULL(Script->CurrentCommand->Data);\r
a405b86d 1119 }\r
0ab85bef 1120 SHELL_FREE_NON_NULL(Script->CurrentCommand);\r
a405b86d 1121 }\r
1122 }\r
0ab85bef 1123 SHELL_FREE_NON_NULL(Script->ScriptName);\r
1124 SHELL_FREE_NON_NULL(Script);\r
a405b86d 1125}\r
1126\r
1127/**\r
1128 Function to return a pointer to the currently running script file object.\r
1129\r
1130 @retval NULL A script file is not currently running.\r
1131 @return A pointer to the current script file object.\r
1132**/\r
1133SCRIPT_FILE*\r
1134EFIAPI\r
1135ShellCommandGetCurrentScriptFile (\r
1136 VOID\r
1137 )\r
1138{\r
1139 SCRIPT_FILE_LIST *List;\r
1140 if (IsListEmpty (&mScriptList.Link)) {\r
1141 return (NULL);\r
1142 }\r
1143 List = ((SCRIPT_FILE_LIST*)GetFirstNode(&mScriptList.Link));\r
1144 return (List->Data);\r
1145}\r
1146\r
1147/**\r
1148 Function to set a new script as the currently running one.\r
1149\r
1150 This function will correctly stack and unstack nested scripts.\r
1151\r
1152 @param[in] Script Pointer to new script information structure. if NULL\r
1153 will remove and de-allocate the top-most Script structure.\r
1154\r
1155 @return A pointer to the current running script file after this\r
1156 change. NULL if removing the final script.\r
1157**/\r
1158SCRIPT_FILE*\r
1159EFIAPI\r
1160ShellCommandSetNewScript (\r
1161 IN SCRIPT_FILE *Script OPTIONAL\r
1162 )\r
1163{\r
1164 SCRIPT_FILE_LIST *Node;\r
1165 if (Script == NULL) {\r
1166 if (IsListEmpty (&mScriptList.Link)) {\r
a405b86d 1167 return (NULL);\r
1168 }\r
1169 Node = (SCRIPT_FILE_LIST *)GetFirstNode(&mScriptList.Link);\r
1170 RemoveEntryList(&Node->Link);\r
1171 DeleteScriptFileStruct(Node->Data);\r
1172 FreePool(Node);\r
1173 } else {\r
1174 Node = AllocateZeroPool(sizeof(SCRIPT_FILE_LIST));\r
0ab85bef 1175 if (Node == NULL) {\r
1176 return (NULL);\r
1177 }\r
a405b86d 1178 Node->Data = Script;\r
1179 InsertHeadList(&mScriptList.Link, &Node->Link);\r
1180 }\r
1181 return (ShellCommandGetCurrentScriptFile());\r
1182}\r
1183\r
1184/**\r
1185 Function to generate the next default mapping name.\r
1186\r
1187 If the return value is not NULL then it must be callee freed.\r
1188\r
1189 @param Type What kind of mapping name to make.\r
1190\r
1191 @retval NULL a memory allocation failed.\r
1192 @return a new map name string\r
1193**/\r
1194CHAR16*\r
1195EFIAPI\r
1196ShellCommandCreateNewMappingName(\r
1197 IN CONST SHELL_MAPPING_TYPE Type\r
1198 )\r
1199{\r
1200 CHAR16 *String;\r
1201 ASSERT(Type < MappingTypeMax);\r
1202\r
1203 String = NULL;\r
1204\r
1205 String = AllocateZeroPool(PcdGet8(PcdShellMapNameLength) * sizeof(String[0]));\r
1206 UnicodeSPrint(\r
1207 String,\r
1208 PcdGet8(PcdShellMapNameLength) * sizeof(String[0]),\r
1209 Type == MappingTypeFileSystem?L"FS%d:":L"BLK%d:",\r
1210 Type == MappingTypeFileSystem?mFsMaxCount++:mBlkMaxCount++);\r
1211\r
1212 return (String);\r
1213}\r
1214\r
1215/**\r
1216 Function to add a map node to the list of map items and update the "path" environment variable (optionally).\r
1217\r
1218 If Path is TRUE (during initialization only), the path environment variable will also be updated to include\r
1219 default paths on the new map name...\r
1220\r
1221 Path should be FALSE when this function is called from the protocol SetMap function.\r
1222\r
1223 @param[in] Name The human readable mapped name.\r
1224 @param[in] DevicePath The Device Path for this map.\r
1225 @param[in] Flags The Flags attribute for this map item.\r
1226 @param[in] Path TRUE to update path, FALSE to skip this step (should only be TRUE during initialization).\r
1227\r
1228 @retval EFI_SUCCESS The addition was sucessful.\r
1229 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.\r
1230 @retval EFI_INVALID_PARAMETER A parameter was invalid.\r
1231**/\r
1232EFI_STATUS\r
1233EFIAPI\r
1234ShellCommandAddMapItemAndUpdatePath(\r
1235 IN CONST CHAR16 *Name,\r
1236 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath,\r
1237 IN CONST UINT64 Flags,\r
1238 IN CONST BOOLEAN Path\r
1239 )\r
1240{\r
1241 EFI_STATUS Status;\r
1242 SHELL_MAP_LIST *MapListNode;\r
1243 CONST CHAR16 *OriginalPath;\r
1244 CHAR16 *NewPath;\r
1245 UINTN NewPathSize;\r
1246\r
1247 NewPathSize = 0;\r
1248 NewPath = NULL;\r
1249 OriginalPath = NULL;\r
1250 Status = EFI_SUCCESS;\r
1251\r
1252 MapListNode = AllocateZeroPool(sizeof(SHELL_MAP_LIST));\r
1253 if (MapListNode == NULL) {\r
1254 Status = EFI_OUT_OF_RESOURCES;\r
1255 } else {\r
1256 MapListNode->Flags = Flags;\r
3957a5a5 1257 MapListNode->MapName = AllocateCopyPool(StrSize(Name), Name);\r
a405b86d 1258 MapListNode->DevicePath = DuplicateDevicePath(DevicePath);\r
1259 if ((MapListNode->MapName == NULL) || (MapListNode->DevicePath == NULL)){\r
1260 Status = EFI_OUT_OF_RESOURCES;\r
1261 } else {\r
a405b86d 1262 InsertTailList(&gShellMapList.Link, &MapListNode->Link);\r
1263 }\r
1264 }\r
1265 if (EFI_ERROR(Status)) {\r
1266 if (MapListNode != NULL) {\r
1267 if (MapListNode->DevicePath != NULL) {\r
1268 FreePool(MapListNode->DevicePath);\r
1269 }\r
1270 if (MapListNode->MapName != NULL) {\r
1271 FreePool(MapListNode->MapName);\r
1272 }\r
1273 FreePool(MapListNode);\r
1274 }\r
1275 } else if (Path) {\r
1276 //\r
1277 // Since there was no error and Path was TRUE\r
1278 // Now add the correct path for that mapping\r
1279 //\r
1280 OriginalPath = gEfiShellProtocol->GetEnv(L"path");\r
1281 ASSERT((NewPath == NULL && NewPathSize == 0) || (NewPath != NULL));\r
1282 if (OriginalPath != NULL) {\r
1283 StrnCatGrow(&NewPath, &NewPathSize, OriginalPath, 0);\r
85b88deb 1284 StrnCatGrow(&NewPath, &NewPathSize, L";", 0);\r
a405b86d 1285 }\r
a405b86d 1286 StrnCatGrow(&NewPath, &NewPathSize, Name, 0);\r
1287 StrnCatGrow(&NewPath, &NewPathSize, L"\\efi\\tools\\;", 0);\r
1288 StrnCatGrow(&NewPath, &NewPathSize, Name, 0);\r
1289 StrnCatGrow(&NewPath, &NewPathSize, L"\\efi\\boot\\;", 0);\r
1290 StrnCatGrow(&NewPath, &NewPathSize, Name, 0);\r
1291 StrnCatGrow(&NewPath, &NewPathSize, L"\\", 0);\r
1292\r
1293 Status = gEfiShellProtocol->SetEnv(L"path", NewPath, TRUE);\r
1294 ASSERT_EFI_ERROR(Status);\r
1295 FreePool(NewPath);\r
1296 }\r
1297 return (Status);\r
1298}\r
1299\r
1300/**\r
1301 Creates the default map names for each device path in the system with\r
1302 a protocol depending on the Type.\r
1303\r
1304 Creates the consistent map names for each device path in the system with\r
1305 a protocol depending on the Type.\r
1306\r
1307 Note: This will reset all mappings in the system("map -r").\r
1308\r
1309 Also sets up the default path environment variable if Type is FileSystem.\r
1310\r
1311 @retval EFI_SUCCESS All map names were created sucessfully.\r
1312 @retval EFI_NOT_FOUND No protocols were found in the system.\r
1313 @return Error returned from gBS->LocateHandle().\r
1314\r
1315 @sa LocateHandle\r
1316**/\r
1317EFI_STATUS\r
1318EFIAPI\r
1319ShellCommandCreateInitialMappingsAndPaths(\r
1320 VOID\r
1321 )\r
1322{\r
1323 EFI_STATUS Status;\r
1324 EFI_HANDLE *HandleList;\r
1325 UINTN Count;\r
1326 EFI_DEVICE_PATH_PROTOCOL **DevicePathList;\r
1327 CHAR16 *NewDefaultName;\r
1328 CHAR16 *NewConsistName;\r
1329 EFI_DEVICE_PATH_PROTOCOL **ConsistMappingTable;\r
1330 SHELL_MAP_LIST *MapListNode;\r
055fafe1
HL
1331 CONST CHAR16 *CurDir;\r
1332 CHAR16 *SplitCurDir;\r
1333 CHAR16 *MapName;\r
1334 SHELL_MAP_LIST *MapListItem;\r
1335\r
1336 SplitCurDir = NULL;\r
1337 MapName = NULL;\r
1338 MapListItem = NULL;\r
a405b86d 1339 HandleList = NULL;\r
1340\r
1341 //\r
1342 // Reset the static members back to zero\r
1343 //\r
1344 mFsMaxCount = 0;\r
1345 mBlkMaxCount = 0;\r
1346\r
1347 gEfiShellProtocol->SetEnv(L"path", L"", TRUE);\r
1348\r
1349 //\r
1350 // First empty out the existing list.\r
1351 //\r
1352 if (!IsListEmpty(&gShellMapList.Link)) {\r
1353 for ( MapListNode = (SHELL_MAP_LIST *)GetFirstNode(&gShellMapList.Link)\r
1354 ; !IsListEmpty(&gShellMapList.Link)\r
1355 ; MapListNode = (SHELL_MAP_LIST *)GetFirstNode(&gShellMapList.Link)\r
1356 ){\r
1357 RemoveEntryList(&MapListNode->Link);\r
c8cfd83a
JY
1358 SHELL_FREE_NON_NULL(MapListNode->DevicePath);\r
1359 SHELL_FREE_NON_NULL(MapListNode->MapName);\r
1360 SHELL_FREE_NON_NULL(MapListNode->CurrentDirectoryPath);\r
a405b86d 1361 FreePool(MapListNode);\r
1362 } // for loop\r
1363 }\r
1364\r
1365 //\r
1366 // Find each handle with Simple File System\r
1367 //\r
0ab85bef 1368 HandleList = GetHandleListByProtocol(&gEfiSimpleFileSystemProtocolGuid);\r
a405b86d 1369 if (HandleList != NULL) {\r
1370 //\r
1371 // Do a count of the handles\r
1372 //\r
1373 for (Count = 0 ; HandleList[Count] != NULL ; Count++);\r
1374\r
1375 //\r
1376 // Get all Device Paths\r
1377 //\r
1a63ec8f 1378 DevicePathList = AllocateZeroPool(sizeof(EFI_DEVICE_PATH_PROTOCOL*) * Count);\r
107d05a4
RN
1379 if (DevicePathList == NULL) {\r
1380 SHELL_FREE_NON_NULL (HandleList);\r
1381 return EFI_OUT_OF_RESOURCES;\r
1382 }\r
a405b86d 1383\r
1384 for (Count = 0 ; HandleList[Count] != NULL ; Count++) {\r
1385 DevicePathList[Count] = DevicePathFromHandle(HandleList[Count]);\r
1386 }\r
1387\r
1388 //\r
1389 // Sort all DevicePaths\r
1390 //\r
1391 PerformQuickSort(DevicePathList, Count, sizeof(EFI_DEVICE_PATH_PROTOCOL*), DevicePathCompare);\r
1392\r
1393 ShellCommandConsistMappingInitialize(&ConsistMappingTable);\r
1394 //\r
1395 // Assign new Mappings to all...\r
1396 //\r
1397 for (Count = 0 ; HandleList[Count] != NULL ; Count++) {\r
1398 //\r
1399 // Get default name first\r
1400 //\r
1401 NewDefaultName = ShellCommandCreateNewMappingName(MappingTypeFileSystem);\r
1402 ASSERT(NewDefaultName != NULL);\r
1403 Status = ShellCommandAddMapItemAndUpdatePath(NewDefaultName, DevicePathList[Count], 0, TRUE);\r
1404 ASSERT_EFI_ERROR(Status);\r
1405 FreePool(NewDefaultName);\r
1406\r
1407 //\r
1408 // Now do consistent name\r
1409 //\r
1410 NewConsistName = ShellCommandConsistMappingGenMappingName(DevicePathList[Count], ConsistMappingTable);\r
1411 if (NewConsistName != NULL) {\r
1412 Status = ShellCommandAddMapItemAndUpdatePath(NewConsistName, DevicePathList[Count], 0, FALSE);\r
1413 ASSERT_EFI_ERROR(Status);\r
1414 FreePool(NewConsistName);\r
1415 }\r
1416 }\r
1417\r
1418 ShellCommandConsistMappingUnInitialize(ConsistMappingTable);\r
1419\r
1420 SHELL_FREE_NON_NULL(HandleList);\r
1421 SHELL_FREE_NON_NULL(DevicePathList);\r
1422\r
1423 HandleList = NULL;\r
055fafe1
HL
1424\r
1425 //\r
1426 //gShellCurMapping point to node of current file system in the gShellMapList. When reset all mappings,\r
1427 //all nodes in the gShellMapList will be free. Then gShellCurMapping will be a dangling pointer, So,\r
1428 //after created new mappings, we should reset the gShellCurMapping pointer back to node of current file system.\r
1429 //\r
1430 if (gShellCurMapping != NULL) {\r
1431 gShellCurMapping = NULL;\r
1432 CurDir = gEfiShellProtocol->GetEnv(L"cwd");\r
1433 if (CurDir != NULL) {\r
1434 MapName = AllocateCopyPool (StrSize(CurDir), CurDir);\r
1435 if (MapName == NULL) {\r
1436 return EFI_OUT_OF_RESOURCES;\r
1437 }\r
1438 SplitCurDir = StrStr (MapName, L":");\r
1439 if (SplitCurDir == NULL) {\r
1440 SHELL_FREE_NON_NULL (MapName);\r
1441 return EFI_UNSUPPORTED;\r
1442 }\r
1443 *(SplitCurDir + 1) = CHAR_NULL;\r
1444 MapListItem = ShellCommandFindMapItem (MapName);\r
1445 if (MapListItem != NULL) {\r
1446 gShellCurMapping = MapListItem;\r
1447 }\r
1448 SHELL_FREE_NON_NULL (MapName);\r
1449 }\r
1450 }\r
a405b86d 1451 } else {\r
1452 Count = (UINTN)-1;\r
1453 }\r
1454\r
1455 //\r
1456 // Find each handle with Block Io\r
1457 //\r
0ab85bef 1458 HandleList = GetHandleListByProtocol(&gEfiBlockIoProtocolGuid);\r
a405b86d 1459 if (HandleList != NULL) {\r
1460 for (Count = 0 ; HandleList[Count] != NULL ; Count++);\r
1461\r
1462 //\r
1463 // Get all Device Paths\r
1464 //\r
1a63ec8f 1465 DevicePathList = AllocateZeroPool(sizeof(EFI_DEVICE_PATH_PROTOCOL*) * Count);\r
107d05a4
RN
1466 if (DevicePathList == NULL) {\r
1467 SHELL_FREE_NON_NULL (HandleList);\r
1468 return EFI_OUT_OF_RESOURCES;\r
1469 }\r
a405b86d 1470\r
1471 for (Count = 0 ; HandleList[Count] != NULL ; Count++) {\r
1472 DevicePathList[Count] = DevicePathFromHandle(HandleList[Count]);\r
1473 }\r
1474\r
1475 //\r
1476 // Sort all DevicePaths\r
1477 //\r
1478 PerformQuickSort(DevicePathList, Count, sizeof(EFI_DEVICE_PATH_PROTOCOL*), DevicePathCompare);\r
1479\r
1480 //\r
1481 // Assign new Mappings to all...\r
1482 //\r
1483 for (Count = 0 ; HandleList[Count] != NULL ; Count++) {\r
1484 //\r
1485 // Get default name first\r
1486 //\r
1487 NewDefaultName = ShellCommandCreateNewMappingName(MappingTypeBlockIo);\r
1488 ASSERT(NewDefaultName != NULL);\r
1489 Status = ShellCommandAddMapItemAndUpdatePath(NewDefaultName, DevicePathList[Count], 0, FALSE);\r
1490 ASSERT_EFI_ERROR(Status);\r
1491 FreePool(NewDefaultName);\r
1492 }\r
1493\r
1494 SHELL_FREE_NON_NULL(HandleList);\r
1495 SHELL_FREE_NON_NULL(DevicePathList);\r
1496 } else if (Count == (UINTN)-1) {\r
1497 return (EFI_NOT_FOUND);\r
1498 }\r
1499\r
1500 return (EFI_SUCCESS);\r
cf812a20
JC
1501}\r
1502\r
1503/**\r
1504 Add mappings for any devices without one. Do not change any existing maps.\r
1505\r
1506 @retval EFI_SUCCESS The operation was successful.\r
1507**/\r
1508EFI_STATUS\r
1509EFIAPI\r
1510ShellCommandUpdateMapping (\r
1511 VOID\r
1512 )\r
1513{\r
1514 EFI_STATUS Status;\r
1515 EFI_HANDLE *HandleList;\r
1516 UINTN Count;\r
1517 EFI_DEVICE_PATH_PROTOCOL **DevicePathList;\r
1518 CHAR16 *NewDefaultName;\r
1519 CHAR16 *NewConsistName;\r
1520 EFI_DEVICE_PATH_PROTOCOL **ConsistMappingTable;\r
1521\r
1522 HandleList = NULL;\r
1523 Status = EFI_SUCCESS;\r
1524\r
1525 //\r
1526 // remove mappings that represent removed devices.\r
1527 //\r
1528\r
1529 //\r
1530 // Find each handle with Simple File System\r
1531 //\r
1532 HandleList = GetHandleListByProtocol(&gEfiSimpleFileSystemProtocolGuid);\r
1533 if (HandleList != NULL) {\r
1534 //\r
1535 // Do a count of the handles\r
1536 //\r
1537 for (Count = 0 ; HandleList[Count] != NULL ; Count++);\r
1538\r
1539 //\r
1540 // Get all Device Paths\r
1541 //\r
1542 DevicePathList = AllocateZeroPool(sizeof(EFI_DEVICE_PATH_PROTOCOL*) * Count);\r
ae315cc2
JC
1543 if (DevicePathList == NULL) {\r
1544 return (EFI_OUT_OF_RESOURCES);\r
1545 }\r
cf812a20
JC
1546\r
1547 for (Count = 0 ; HandleList[Count] != NULL ; Count++) {\r
1548 DevicePathList[Count] = DevicePathFromHandle(HandleList[Count]);\r
1549 }\r
1550\r
1551 //\r
1552 // Sort all DevicePaths\r
1553 //\r
1554 PerformQuickSort(DevicePathList, Count, sizeof(EFI_DEVICE_PATH_PROTOCOL*), DevicePathCompare);\r
1555\r
1556 ShellCommandConsistMappingInitialize(&ConsistMappingTable);\r
1557\r
1558 //\r
1559 // Assign new Mappings to remainders\r
1560 //\r
ae315cc2 1561 for (Count = 0 ; !EFI_ERROR(Status) && HandleList[Count] != NULL && !EFI_ERROR(Status); Count++) {\r
cf812a20
JC
1562 //\r
1563 // Skip ones that already have\r
1564 //\r
1565 if (gEfiShellProtocol->GetMapFromDevicePath(&DevicePathList[Count]) != NULL) {\r
1566 continue;\r
1567 }\r
1568 //\r
1569 // Get default name\r
1570 //\r
1571 NewDefaultName = ShellCommandCreateNewMappingName(MappingTypeFileSystem);\r
ae315cc2
JC
1572 if (NewDefaultName == NULL) {\r
1573 Status = EFI_OUT_OF_RESOURCES;\r
1574 break;\r
1575 }\r
cf812a20
JC
1576\r
1577 //\r
1578 // Call shell protocol SetMap function now...\r
1579 //\r
1580 Status = gEfiShellProtocol->SetMap(DevicePathList[Count], NewDefaultName);\r
1581\r
1582 if (!EFI_ERROR(Status)) {\r
1583 //\r
1584 // Now do consistent name\r
1585 //\r
1586 NewConsistName = ShellCommandConsistMappingGenMappingName(DevicePathList[Count], ConsistMappingTable);\r
1587 if (NewConsistName != NULL) {\r
1588 Status = gEfiShellProtocol->SetMap(DevicePathList[Count], NewConsistName);\r
1589 FreePool(NewConsistName);\r
1590 }\r
1591 }\r
1592\r
1593 FreePool(NewDefaultName);\r
1594 }\r
1595 ShellCommandConsistMappingUnInitialize(ConsistMappingTable);\r
1596 SHELL_FREE_NON_NULL(HandleList);\r
1597 SHELL_FREE_NON_NULL(DevicePathList);\r
1598\r
1599 HandleList = NULL;\r
1600 } else {\r
1601 Count = (UINTN)-1;\r
1602 }\r
1603 //\r
1604 // Do it all over again for gEfiBlockIoProtocolGuid\r
1605 //\r
1606\r
1607 return (Status);\r
1608}\r
1609\r
1610/**\r
1611 Converts a SHELL_FILE_HANDLE to an EFI_FILE_PROTOCOL*.\r
1612\r
1613 @param[in] Handle The SHELL_FILE_HANDLE to convert.\r
a405b86d 1614\r
1615 @return a EFI_FILE_PROTOCOL* representing the same file.\r
1616**/\r
1617EFI_FILE_PROTOCOL*\r
1618EFIAPI\r
1619ConvertShellHandleToEfiFileProtocol(\r
1620 IN CONST SHELL_FILE_HANDLE Handle\r
1621 )\r
1622{\r
1623 return ((EFI_FILE_PROTOCOL*)(Handle));\r
1624}\r
1625\r
1626/**\r
1627 Converts a EFI_FILE_PROTOCOL* to an SHELL_FILE_HANDLE.\r
1628\r
1629 @param[in] Handle The pointer to EFI_FILE_PROTOCOL to convert.\r
1630 @param[in] Path The path to the file for verification.\r
1631\r
ff51746b 1632 @return A SHELL_FILE_HANDLE representing the same file.\r
1633 @retval NULL There was not enough memory.\r
a405b86d 1634**/\r
1635SHELL_FILE_HANDLE\r
1636EFIAPI\r
1637ConvertEfiFileProtocolToShellHandle(\r
1638 IN CONST EFI_FILE_PROTOCOL *Handle,\r
1639 IN CONST CHAR16 *Path\r
1640 )\r
1641{\r
1642 SHELL_COMMAND_FILE_HANDLE *Buffer;\r
1643 BUFFER_LIST *NewNode;\r
1644\r
1645 if (Path != NULL) {\r
1646 Buffer = AllocateZeroPool(sizeof(SHELL_COMMAND_FILE_HANDLE));\r
ff51746b 1647 if (Buffer == NULL) {\r
1648 return (NULL);\r
1649 }\r
1a63ec8f 1650 NewNode = AllocateZeroPool(sizeof(BUFFER_LIST));\r
ff51746b 1651 if (NewNode == NULL) {\r
ae315cc2 1652 SHELL_FREE_NON_NULL(Buffer);\r
ff51746b 1653 return (NULL);\r
1654 }\r
a405b86d 1655 Buffer->FileHandle = (EFI_FILE_PROTOCOL*)Handle;\r
1656 Buffer->Path = StrnCatGrow(&Buffer->Path, NULL, Path, 0);\r
ff51746b 1657 if (Buffer->Path == NULL) {\r
ae315cc2
JC
1658 SHELL_FREE_NON_NULL(NewNode);\r
1659 SHELL_FREE_NON_NULL(Buffer);\r
ff51746b 1660 return (NULL);\r
1661 }\r
a405b86d 1662 NewNode->Buffer = Buffer;\r
1663\r
1664 InsertHeadList(&mFileHandleList.Link, &NewNode->Link);\r
1665 }\r
1666 return ((SHELL_FILE_HANDLE)(Handle));\r
1667}\r
1668\r
1669/**\r
1670 Find the path that was logged with the specified SHELL_FILE_HANDLE.\r
1671\r
1672 @param[in] Handle The SHELL_FILE_HANDLE to query on.\r
1673\r
1674 @return A pointer to the path for the file.\r
1675**/\r
1676CONST CHAR16*\r
1677EFIAPI\r
1678ShellFileHandleGetPath(\r
1679 IN CONST SHELL_FILE_HANDLE Handle\r
1680 )\r
1681{\r
1682 BUFFER_LIST *Node;\r
1683\r
1684 for (Node = (BUFFER_LIST*)GetFirstNode(&mFileHandleList.Link)\r
1685 ; !IsNull(&mFileHandleList.Link, &Node->Link)\r
1686 ; Node = (BUFFER_LIST*)GetNextNode(&mFileHandleList.Link, &Node->Link)\r
1687 ){\r
1688 if ((Node->Buffer) && (((SHELL_COMMAND_FILE_HANDLE *)Node->Buffer)->FileHandle == Handle)){\r
1689 return (((SHELL_COMMAND_FILE_HANDLE *)Node->Buffer)->Path);\r
1690 }\r
1691 }\r
1692 return (NULL);\r
1693}\r
1694\r
1695/**\r
1a63ec8f 1696 Remove a SHELL_FILE_HANDLE from the list of SHELL_FILE_HANDLES.\r
a405b86d 1697\r
1698 @param[in] Handle The SHELL_FILE_HANDLE to remove.\r
1699\r
1700 @retval TRUE The item was removed.\r
1701 @retval FALSE The item was not found.\r
1702**/\r
1703BOOLEAN\r
1704EFIAPI\r
1705ShellFileHandleRemove(\r
1706 IN CONST SHELL_FILE_HANDLE Handle\r
1707 )\r
1708{\r
1709 BUFFER_LIST *Node;\r
1710\r
1711 for (Node = (BUFFER_LIST*)GetFirstNode(&mFileHandleList.Link)\r
1712 ; !IsNull(&mFileHandleList.Link, &Node->Link)\r
1713 ; Node = (BUFFER_LIST*)GetNextNode(&mFileHandleList.Link, &Node->Link)\r
1714 ){\r
1715 if ((Node->Buffer) && (((SHELL_COMMAND_FILE_HANDLE *)Node->Buffer)->FileHandle == Handle)){\r
a405b86d 1716 RemoveEntryList(&Node->Link);\r
ff51746b 1717 SHELL_FREE_NON_NULL(((SHELL_COMMAND_FILE_HANDLE *)Node->Buffer)->Path);\r
1718 SHELL_FREE_NON_NULL(Node->Buffer);\r
1719 SHELL_FREE_NON_NULL(Node);\r
a405b86d 1720 return (TRUE);\r
1721 }\r
1722 }\r
1723 return (FALSE);\r
1724}\r
1725\r
1726/**\r
1727 Function to determine if a SHELL_FILE_HANDLE is at the end of the file.\r
1728\r
1729 This will NOT work on directories.\r
1730\r
1731 If Handle is NULL, then ASSERT.\r
1732\r
1733 @param[in] Handle the file handle\r
1734\r
1735 @retval TRUE the position is at the end of the file\r
1736 @retval FALSE the position is not at the end of the file\r
1737**/\r
1738BOOLEAN\r
1739EFIAPI\r
1740ShellFileHandleEof(\r
1741 IN SHELL_FILE_HANDLE Handle\r
1742 )\r
1743{\r
1744 EFI_FILE_INFO *Info;\r
1745 UINT64 Pos;\r
1746 BOOLEAN RetVal;\r
1747\r
1748 //\r
1749 // ASSERT if Handle is NULL\r
1750 //\r
1751 ASSERT(Handle != NULL);\r
1752\r
1753 gEfiShellProtocol->GetFilePosition(Handle, &Pos);\r
1754 Info = gEfiShellProtocol->GetFileInfo (Handle);\r
a405b86d 1755 gEfiShellProtocol->SetFilePosition(Handle, Pos);\r
1756\r
1757 if (Info == NULL) {\r
1758 return (FALSE);\r
1759 }\r
1760\r
1761 if (Pos == Info->FileSize) {\r
1762 RetVal = TRUE;\r
1763 } else {\r
1764 RetVal = FALSE;\r
1765 }\r
1766\r
1767 FreePool (Info);\r
1768\r
1769 return (RetVal);\r
1770}\r
1771\r
a405b86d 1772/**\r
1773 Frees any BUFFER_LIST defined type.\r
1a63ec8f 1774\r
1775 @param[in] List The BUFFER_LIST object to free.\r
a405b86d 1776**/\r
1777VOID\r
1778EFIAPI\r
1779FreeBufferList (\r
1780 IN BUFFER_LIST *List\r
1781 )\r
1782{\r
1783 BUFFER_LIST *BufferListEntry;\r
1784\r
1785 if (List == NULL){\r
1786 return;\r
1787 }\r
1788 //\r
1789 // enumerate through the buffer list and free all memory\r
1790 //\r
1791 for ( BufferListEntry = ( BUFFER_LIST *)GetFirstNode(&List->Link)\r
1792 ; !IsListEmpty (&List->Link)\r
1793 ; BufferListEntry = (BUFFER_LIST *)GetFirstNode(&List->Link)\r
1794 ){\r
1795 RemoveEntryList(&BufferListEntry->Link);\r
a405b86d 1796 if (BufferListEntry->Buffer != NULL) {\r
1797 FreePool(BufferListEntry->Buffer);\r
1798 }\r
1799 FreePool(BufferListEntry);\r
1800 }\r
1801}\r
1802\r
3bd89603
LE
1803/**\r
1804 Dump some hexadecimal data to the screen.\r
1805\r
1806 @param[in] Indent How many spaces to indent the output.\r
1807 @param[in] Offset The offset of the printing.\r
1808 @param[in] DataSize The size in bytes of UserData.\r
1809 @param[in] UserData The data to print out.\r
1810**/\r
1811VOID\r
667d7146 1812EFIAPI\r
3bd89603
LE
1813DumpHex (\r
1814 IN UINTN Indent,\r
1815 IN UINTN Offset,\r
1816 IN UINTN DataSize,\r
1817 IN VOID *UserData\r
1818 )\r
1819{\r
1820 UINT8 *Data;\r
1821\r
1822 CHAR8 Val[50];\r
1823\r
1824 CHAR8 Str[20];\r
1825\r
1826 UINT8 TempByte;\r
1827 UINTN Size;\r
1828 UINTN Index;\r
1829\r
1830 Data = UserData;\r
1831 while (DataSize != 0) {\r
1832 Size = 16;\r
1833 if (Size > DataSize) {\r
1834 Size = DataSize;\r
1835 }\r
1836\r
1837 for (Index = 0; Index < Size; Index += 1) {\r
1838 TempByte = Data[Index];\r
1839 Val[Index * 3 + 0] = Hex[TempByte >> 4];\r
1840 Val[Index * 3 + 1] = Hex[TempByte & 0xF];\r
1841 Val[Index * 3 + 2] = (CHAR8) ((Index == 7) ? '-' : ' ');\r
4bf3b994 1842 Str[Index] = (CHAR8) ((TempByte < ' ' || TempByte > '~') ? '.' : TempByte);\r
3bd89603
LE
1843 }\r
1844\r
1845 Val[Index * 3] = 0;\r
1846 Str[Index] = 0;\r
1847 ShellPrintEx(-1, -1, L"%*a%08X: %-48a *%a*\r\n", Indent, "", Offset, Val, Str);\r
1848\r
1849 Data += Size;\r
1850 Offset += Size;\r
1851 DataSize -= Size;\r
1852 }\r
1853}\r
cf041fd7
RN
1854\r
1855/**\r
1856 Dump HEX data into buffer.\r
1857\r
1858 @param[in] Buffer HEX data to be dumped in Buffer.\r
1859 @param[in] Indent How many spaces to indent the output.\r
1860 @param[in] Offset The offset of the printing.\r
1861 @param[in] DataSize The size in bytes of UserData.\r
1862 @param[in] UserData The data to print out.\r
1863**/\r
1864CHAR16*\r
667d7146 1865EFIAPI\r
cf041fd7
RN
1866CatSDumpHex (\r
1867 IN CHAR16 *Buffer,\r
1868 IN UINTN Indent,\r
1869 IN UINTN Offset,\r
1870 IN UINTN DataSize,\r
1871 IN VOID *UserData\r
1872 )\r
1873{\r
1874 UINT8 *Data;\r
1875 UINT8 TempByte;\r
1876 UINTN Size;\r
1877 UINTN Index;\r
1878 CHAR8 Val[50];\r
1879 CHAR8 Str[20];\r
1880 CHAR16 *RetVal;\r
1881 CHAR16 *TempRetVal;\r
1882\r
1883 Data = UserData;\r
1884 RetVal = Buffer;\r
1885 while (DataSize != 0) {\r
1886 Size = 16;\r
1887 if (Size > DataSize) {\r
1888 Size = DataSize;\r
1889 }\r
1890\r
1891 for (Index = 0; Index < Size; Index += 1) {\r
1892 TempByte = Data[Index];\r
1893 Val[Index * 3 + 0] = Hex[TempByte >> 4];\r
1894 Val[Index * 3 + 1] = Hex[TempByte & 0xF];\r
1895 Val[Index * 3 + 2] = (CHAR8) ((Index == 7) ? '-' : ' ');\r
1896 Str[Index] = (CHAR8) ((TempByte < ' ' || TempByte > 'z') ? '.' : TempByte);\r
1897 }\r
1898\r
1899 Val[Index * 3] = 0;\r
1900 Str[Index] = 0;\r
1901 TempRetVal = CatSPrint (RetVal, L"%*a%08X: %-48a *%a*\r\n", Indent, "", Offset, Val, Str);\r
1902 SHELL_FREE_NON_NULL (RetVal);\r
1903 RetVal = TempRetVal;\r
1904\r
1905 Data += Size;\r
1906 Offset += Size;\r
1907 DataSize -= Size;\r
1908 }\r
1909\r
1910 return RetVal;\r
1911}\r
101c55ac
LE
1912\r
1913/**\r
1914 ORDERED_COLLECTION_USER_COMPARE function for SHELL_SORT_UNIQUE_NAME objects.\r
1915\r
1916 @param[in] Unique1AsVoid The first SHELL_SORT_UNIQUE_NAME object (Unique1),\r
1917 passed in as a pointer-to-VOID.\r
1918\r
1919 @param[in] Unique2AsVoid The second SHELL_SORT_UNIQUE_NAME object (Unique2),\r
1920 passed in as a pointer-to-VOID.\r
1921\r
1922 @retval <0 If Unique1 compares less than Unique2.\r
1923\r
1924 @retval 0 If Unique1 compares equal to Unique2.\r
1925\r
1926 @retval >0 If Unique1 compares greater than Unique2.\r
1927**/\r
1928STATIC\r
1929INTN\r
1930EFIAPI\r
1931UniqueNameCompare (\r
1932 IN CONST VOID *Unique1AsVoid,\r
1933 IN CONST VOID *Unique2AsVoid\r
1934 )\r
1935{\r
1936 CONST SHELL_SORT_UNIQUE_NAME *Unique1;\r
1937 CONST SHELL_SORT_UNIQUE_NAME *Unique2;\r
1938\r
1939 Unique1 = Unique1AsVoid;\r
1940 Unique2 = Unique2AsVoid;\r
1941\r
1942 //\r
1943 // We need to cast away CONST for EFI_UNICODE_COLLATION_STRICOLL.\r
1944 //\r
1945 return gUnicodeCollation->StriColl (\r
1946 gUnicodeCollation,\r
1947 (CHAR16 *)Unique1->Alias,\r
1948 (CHAR16 *)Unique2->Alias\r
1949 );\r
1950}\r
1951\r
1952/**\r
1953 ORDERED_COLLECTION_KEY_COMPARE function for SHELL_SORT_UNIQUE_NAME objects.\r
1954\r
1955 @param[in] UniqueAliasAsVoid The CHAR16 string UniqueAlias, passed in as a\r
1956 pointer-to-VOID.\r
1957\r
1958 @param[in] UniqueAsVoid The SHELL_SORT_UNIQUE_NAME object (Unique),\r
1959 passed in as a pointer-to-VOID.\r
1960\r
1961 @retval <0 If UniqueAlias compares less than Unique->Alias.\r
1962\r
1963 @retval 0 If UniqueAlias compares equal to Unique->Alias.\r
1964\r
1965 @retval >0 If UniqueAlias compares greater than Unique->Alias.\r
1966**/\r
1967STATIC\r
1968INTN\r
1969EFIAPI\r
1970UniqueNameAliasCompare (\r
1971 IN CONST VOID *UniqueAliasAsVoid,\r
1972 IN CONST VOID *UniqueAsVoid\r
1973 )\r
1974{\r
1975 CONST CHAR16 *UniqueAlias;\r
1976 CONST SHELL_SORT_UNIQUE_NAME *Unique;\r
1977\r
1978 UniqueAlias = UniqueAliasAsVoid;\r
1979 Unique = UniqueAsVoid;\r
1980\r
1981 //\r
1982 // We need to cast away CONST for EFI_UNICODE_COLLATION_STRICOLL.\r
1983 //\r
1984 return gUnicodeCollation->StriColl (\r
1985 gUnicodeCollation,\r
1986 (CHAR16 *)UniqueAlias,\r
1987 (CHAR16 *)Unique->Alias\r
1988 );\r
1989}\r
1990\r
1991/**\r
1992 Sort an EFI_SHELL_FILE_INFO list, optionally moving duplicates to a separate\r
1993 list.\r
1994\r
1995 @param[in,out] FileList The list of EFI_SHELL_FILE_INFO objects to sort.\r
1996\r
1997 If FileList is NULL on input, then FileList is\r
1998 considered an empty, hence already sorted, list.\r
1999\r
2000 Otherwise, if (*FileList) is NULL on input, then\r
2001 EFI_INVALID_PARAMETER is returned.\r
2002\r
2003 Otherwise, the caller is responsible for having\r
2004 initialized (*FileList)->Link with\r
2005 InitializeListHead(). No other fields in the\r
2006 (**FileList) head element are accessed by this\r
2007 function.\r
2008\r
2009 On output, (*FileList) is sorted according to Order.\r
2010 If Duplicates is NULL on input, then duplicate\r
2011 elements are preserved, sorted stably, on\r
2012 (*FileList). If Duplicates is not NULL on input,\r
2013 then duplicates are moved (stably sorted) to the\r
2014 new, dynamically allocated (*Duplicates) list.\r
2015\r
2016 @param[out] Duplicates If Duplicates is NULL on input, (*FileList) will be\r
2017 a monotonically ordered list on output, with\r
2018 duplicates stably sorted.\r
2019\r
2020 If Duplicates is not NULL on input, (*FileList) will\r
2021 be a strictly monotonically oredered list on output,\r
2022 with duplicates separated (stably sorted) to\r
2023 (*Duplicates). All fields except Link will be\r
2024 zero-initialized in the (**Duplicates) head element.\r
2025 If no duplicates exist, then (*Duplicates) is set to\r
2026 NULL on output.\r
2027\r
2028 @param[in] Order Determines the comparison operation between\r
2029 EFI_SHELL_FILE_INFO objects.\r
2030\r
2031 @retval EFI_INVALID_PARAMETER (UINTN)Order is greater than or equal to\r
2032 (UINTN)ShellSortFileListMax. Neither the\r
2033 (*FileList) nor the (*Duplicates) list has\r
2034 been modified.\r
2035\r
2036 @retval EFI_INVALID_PARAMETER (*FileList) was NULL on input. Neither the\r
2037 (*FileList) nor the (*Duplicates) list has\r
2038 been modified.\r
2039\r
2040 @retval EFI_OUT_OF_RESOURCES Memory allocation failed. Neither the\r
2041 (*FileList) nor the (*Duplicates) list has\r
2042 been modified.\r
2043\r
2044 @retval EFI_SUCCESS Sorting successful, including the case when\r
2045 FileList is NULL on input.\r
2046**/\r
2047EFI_STATUS\r
2048EFIAPI\r
2049ShellSortFileList (\r
2050 IN OUT EFI_SHELL_FILE_INFO **FileList,\r
2051 OUT EFI_SHELL_FILE_INFO **Duplicates OPTIONAL,\r
2052 IN SHELL_SORT_FILE_LIST Order\r
2053 )\r
2054{\r
2055 LIST_ENTRY *FilesHead;\r
2056 ORDERED_COLLECTION *Sort;\r
2057 LIST_ENTRY *FileEntry;\r
2058 EFI_SHELL_FILE_INFO *FileInfo;\r
2059 SHELL_SORT_UNIQUE_NAME *Unique;\r
2060 EFI_STATUS Status;\r
2061 EFI_SHELL_FILE_INFO *Dupes;\r
2062 LIST_ENTRY *NextFileEntry;\r
2063 CONST CHAR16 *Alias;\r
2064 ORDERED_COLLECTION_ENTRY *SortEntry;\r
2065 LIST_ENTRY *TargetFileList;\r
2066 ORDERED_COLLECTION_ENTRY *NextSortEntry;\r
2067 VOID *UniqueAsVoid;\r
2068\r
2069 if ((UINTN)Order >= (UINTN)ShellSortFileListMax) {\r
2070 return EFI_INVALID_PARAMETER;\r
2071 }\r
2072\r
2073 if (FileList == NULL) {\r
2074 //\r
2075 // FileList is considered empty, hence already sorted, with no duplicates.\r
2076 //\r
2077 if (Duplicates != NULL) {\r
2078 *Duplicates = NULL;\r
2079 }\r
2080 return EFI_SUCCESS;\r
2081 }\r
2082\r
2083 if (*FileList == NULL) {\r
2084 return EFI_INVALID_PARAMETER;\r
2085 }\r
2086 FilesHead = &(*FileList)->Link;\r
2087\r
2088 //\r
2089 // Collect all the unique names.\r
2090 //\r
2091 Sort = OrderedCollectionInit (UniqueNameCompare, UniqueNameAliasCompare);\r
2092 if (Sort == NULL) {\r
2093 return EFI_OUT_OF_RESOURCES;\r
2094 }\r
2095\r
2096 BASE_LIST_FOR_EACH (FileEntry, FilesHead) {\r
2097 FileInfo = (EFI_SHELL_FILE_INFO *)FileEntry;\r
2098\r
2099 //\r
2100 // Try to record the name of this file as a unique name.\r
2101 //\r
2102 Unique = AllocatePool (sizeof (*Unique));\r
2103 if (Unique == NULL) {\r
2104 Status = EFI_OUT_OF_RESOURCES;\r
2105 goto UninitSort;\r
2106 }\r
2107 Unique->Alias = ((Order == ShellSortFileListByFileName) ?\r
2108 FileInfo->FileName :\r
2109 FileInfo->FullName);\r
2110 InitializeListHead (&Unique->SameNameList);\r
2111\r
2112 Status = OrderedCollectionInsert (Sort, NULL, Unique);\r
2113 if (EFI_ERROR (Status)) {\r
2114 //\r
2115 // Only two errors are possible: memory allocation failed, or this name\r
2116 // has been encountered before. In either case, the\r
2117 // SHELL_SORT_UNIQUE_NAME object being constructed has to be released.\r
2118 //\r
2119 FreePool (Unique);\r
2120 //\r
2121 // Memory allocation failure is fatal, while having seen the same name\r
2122 // before is normal.\r
2123 //\r
2124 if (Status == EFI_OUT_OF_RESOURCES) {\r
2125 goto UninitSort;\r
2126 }\r
2127 ASSERT (Status == EFI_ALREADY_STARTED);\r
2128 }\r
2129 }\r
2130\r
2131 //\r
2132 // If separation of duplicates has been requested, allocate the list for\r
2133 // them.\r
2134 //\r
2135 if (Duplicates != NULL) {\r
2136 Dupes = AllocateZeroPool (sizeof (*Dupes));\r
2137 if (Dupes == NULL) {\r
2138 Status = EFI_OUT_OF_RESOURCES;\r
2139 goto UninitSort;\r
2140 }\r
2141 InitializeListHead (&Dupes->Link);\r
2142 }\r
2143\r
2144 //\r
2145 // No memory allocation beyond this point; thus, no chance to fail. We can\r
2146 // now migrate the EFI_SHELL_FILE_INFO objects from (*FileList) to Sort.\r
2147 //\r
2148 BASE_LIST_FOR_EACH_SAFE (FileEntry, NextFileEntry, FilesHead) {\r
2149 FileInfo = (EFI_SHELL_FILE_INFO *)FileEntry;\r
2150 //\r
2151 // Look up the SHELL_SORT_UNIQUE_NAME that matches FileInfo's name.\r
2152 //\r
2153 Alias = ((Order == ShellSortFileListByFileName) ?\r
2154 FileInfo->FileName :\r
2155 FileInfo->FullName);\r
2156 SortEntry = OrderedCollectionFind (Sort, Alias);\r
2157 ASSERT (SortEntry != NULL);\r
2158 Unique = OrderedCollectionUserStruct (SortEntry);\r
2159 //\r
2160 // Move FileInfo from (*FileList) to the end of the list of files whose\r
2161 // names all compare identical to FileInfo's name.\r
2162 //\r
2163 RemoveEntryList (&FileInfo->Link);\r
2164 InsertTailList (&Unique->SameNameList, &FileInfo->Link);\r
2165 }\r
2166\r
2167 //\r
2168 // All EFI_SHELL_FILE_INFO objects originally in (*FileList) have been\r
2169 // distributed to Sort. Now migrate them back to (*FileList), advancing in\r
2170 // unique name order.\r
2171 //\r
2172 for (SortEntry = OrderedCollectionMin (Sort);\r
2173 SortEntry != NULL;\r
2174 SortEntry = OrderedCollectionNext (SortEntry)) {\r
2175 Unique = OrderedCollectionUserStruct (SortEntry);\r
2176 //\r
2177 // The first FileInfo encountered for each unique name goes back on\r
2178 // (*FileList) unconditionally. Further FileInfo instances for the same\r
2179 // unique name -- that is, duplicates -- are either returned to (*FileList)\r
2180 // or separated, dependent on the caller's request.\r
2181 //\r
2182 TargetFileList = FilesHead;\r
2183 BASE_LIST_FOR_EACH_SAFE (FileEntry, NextFileEntry, &Unique->SameNameList) {\r
2184 RemoveEntryList (FileEntry);\r
2185 InsertTailList (TargetFileList, FileEntry);\r
2186 if (Duplicates != NULL) {\r
2187 TargetFileList = &Dupes->Link;\r
2188 }\r
2189 }\r
2190 }\r
2191\r
2192 //\r
2193 // We're done. If separation of duplicates has been requested, output the\r
2194 // list of duplicates -- and free that list at once, if it's empty (i.e., if\r
2195 // no duplicates have been found).\r
2196 //\r
2197 if (Duplicates != NULL) {\r
2198 if (IsListEmpty (&Dupes->Link)) {\r
2199 FreePool (Dupes);\r
2200 *Duplicates = NULL;\r
2201 } else {\r
2202 *Duplicates = Dupes;\r
2203 }\r
2204 }\r
2205 Status = EFI_SUCCESS;\r
2206\r
2207 //\r
2208 // Fall through.\r
2209 //\r
2210UninitSort:\r
2211 for (SortEntry = OrderedCollectionMin (Sort);\r
2212 SortEntry != NULL;\r
2213 SortEntry = NextSortEntry) {\r
2214 NextSortEntry = OrderedCollectionNext (SortEntry);\r
2215 OrderedCollectionDelete (Sort, SortEntry, &UniqueAsVoid);\r
2216 Unique = UniqueAsVoid;\r
2217 ASSERT (IsListEmpty (&Unique->SameNameList));\r
2218 FreePool (Unique);\r
2219 }\r
2220 OrderedCollectionUninit (Sort);\r
2221\r
2222 return Status;\r
2223}\r