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