]> git.proxmox.com Git - mirror_edk2.git/blame - ShellPkg/Library/UefiShellLib/UefiShellLib.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / ShellPkg / Library / UefiShellLib / UefiShellLib.c
CommitLineData
94b17fa1 1/** @file\r
2 Provides interface to shell functionality for shell commands and applications.\r
3\r
583448b4 4 (C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>\r
9ba404d6 5 Copyright 2016-2018 Dell Technologies.<BR>\r
3d086fed 6 Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>\r
56ba3746 7 SPDX-License-Identifier: BSD-2-Clause-Patent\r
94b17fa1 8\r
9**/\r
10\r
b1f95a06 11#include "UefiShellLib.h"\r
252d9457 12#include <Library/SortLib.h>\r
3fe23dc6 13#include <Library/BaseLib.h>\r
d2b4564b 14\r
d2b4564b 15//\r
a405b86d 16// globals...\r
d2b4564b 17//\r
47d20b54
MK
18SHELL_PARAM_ITEM EmptyParamList[] = {\r
19 { NULL, TypeMax }\r
20};\r
21SHELL_PARAM_ITEM SfoParamList[] = {\r
22 { L"-sfo", TypeFlag },\r
23 { NULL, TypeMax }\r
24};\r
25EFI_SHELL_ENVIRONMENT2 *mEfiShellEnvironment2;\r
26EFI_SHELL_INTERFACE *mEfiShellInterface;\r
27EFI_SHELL_PROTOCOL *gEfiShellProtocol;\r
28EFI_SHELL_PARAMETERS_PROTOCOL *gEfiShellParametersProtocol;\r
29EFI_HANDLE mEfiShellEnvironment2Handle;\r
30FILE_HANDLE_FUNCTION_MAP FileFunctionMap;\r
583448b4 31EFI_UNICODE_COLLATION_PROTOCOL *mUnicodeCollationProtocol;\r
b3011f40 32\r
9ba404d6
JD
33/**\r
34 Return a clean, fully-qualified version of an input path. If the return value\r
35 is non-NULL the caller must free the memory when it is no longer needed.\r
36\r
37 If asserts are disabled, and if the input parameter is NULL, NULL is returned.\r
38\r
39 If there is not enough memory available to create the fully-qualified path or\r
40 a copy of the input path, NULL is returned.\r
41\r
42 If there is no working directory, a clean copy of Path is returned.\r
43\r
44 Otherwise, the current file system or working directory (as appropriate) is\r
45 prepended to Path and the resulting path is cleaned and returned.\r
46\r
47 NOTE: If the input path is an empty string, then the current working directory\r
48 (if it exists) is returned. In other words, an empty input path is treated\r
49 exactly the same as ".".\r
50\r
51 @param[in] Path A pointer to some file or directory path.\r
52\r
53 @retval NULL The input path is NULL or out of memory.\r
54\r
55 @retval non-NULL A pointer to a clean, fully-qualified version of Path.\r
56 If there is no working directory, then a pointer to a\r
57 clean, but not necessarily fully-qualified version of\r
58 Path. The caller must free this memory when it is no\r
59 longer needed.\r
60**/\r
47d20b54 61CHAR16 *\r
9ba404d6 62EFIAPI\r
47d20b54
MK
63FullyQualifyPath (\r
64 IN CONST CHAR16 *Path\r
9ba404d6
JD
65 )\r
66{\r
47d20b54
MK
67 CONST CHAR16 *WorkingPath;\r
68 CONST CHAR16 *InputPath;\r
69 CHAR16 *CharPtr;\r
70 CHAR16 *InputFileSystem;\r
71 UINTN FileSystemCharCount;\r
72 CHAR16 *FullyQualifiedPath;\r
73 UINTN Size;\r
9ba404d6
JD
74\r
75 FullyQualifiedPath = NULL;\r
76\r
47d20b54 77 ASSERT (Path != NULL);\r
9ba404d6
JD
78 //\r
79 // Handle erroneous input when asserts are disabled.\r
80 //\r
81 if (Path == NULL) {\r
82 return NULL;\r
83 }\r
47d20b54 84\r
9ba404d6
JD
85 //\r
86 // In paths that contain ":", like fs0:dir/file.ext and fs2:\fqpath\file.ext,\r
87 // we have to consider the file system part separately from the "path" part.\r
88 // If there is a file system in the path, we have to get the current working\r
89 // directory for that file system. Then we need to use the part of the path\r
90 // following the ":". If a path does not contain ":", we use it as given.\r
91 //\r
47d20b54 92 InputPath = StrStr (Path, L":");\r
9ba404d6
JD
93 if (InputPath != NULL) {\r
94 InputPath++;\r
47d20b54
MK
95 FileSystemCharCount = ((UINTN)InputPath - (UINTN)Path + sizeof (CHAR16)) / sizeof (CHAR16);\r
96 InputFileSystem = AllocateCopyPool (FileSystemCharCount * sizeof (CHAR16), Path);\r
9ba404d6
JD
97 if (InputFileSystem != NULL) {\r
98 InputFileSystem[FileSystemCharCount - 1] = CHAR_NULL;\r
99 }\r
47d20b54
MK
100\r
101 WorkingPath = ShellGetCurrentDir (InputFileSystem);\r
102 SHELL_FREE_NON_NULL (InputFileSystem);\r
9ba404d6 103 } else {\r
47d20b54
MK
104 InputPath = Path;\r
105 WorkingPath = ShellGetEnvironmentVariable (L"cwd");\r
9ba404d6
JD
106 }\r
107\r
108 if (WorkingPath == NULL) {\r
109 //\r
110 // With no working directory, all we can do is copy and clean the input path.\r
111 //\r
47d20b54 112 FullyQualifiedPath = AllocateCopyPool (StrSize (Path), Path);\r
9ba404d6
JD
113 } else {\r
114 //\r
115 // Allocate space for both strings plus one more character.\r
116 //\r
47d20b54
MK
117 Size = StrSize (WorkingPath) + StrSize (InputPath);\r
118 FullyQualifiedPath = AllocateZeroPool (Size);\r
9ba404d6
JD
119 if (FullyQualifiedPath == NULL) {\r
120 //\r
121 // Try to copy and clean just the input. No harm if not enough memory.\r
122 //\r
47d20b54 123 FullyQualifiedPath = AllocateCopyPool (StrSize (Path), Path);\r
9ba404d6 124 } else {\r
47d20b54 125 if ((*InputPath == L'\\') || (*InputPath == L'/')) {\r
9ba404d6
JD
126 //\r
127 // Absolute path: start with the current working directory, then\r
128 // truncate the new path after the file system part.\r
129 //\r
47d20b54
MK
130 StrCpyS (FullyQualifiedPath, Size/sizeof (CHAR16), WorkingPath);\r
131 CharPtr = StrStr (FullyQualifiedPath, L":");\r
b46de509
RN
132 if (CharPtr != NULL) {\r
133 *(CharPtr + 1) = CHAR_NULL;\r
134 }\r
9ba404d6
JD
135 } else {\r
136 //\r
137 // Relative path: start with the working directory and append "\".\r
138 //\r
47d20b54
MK
139 StrCpyS (FullyQualifiedPath, Size/sizeof (CHAR16), WorkingPath);\r
140 StrCatS (FullyQualifiedPath, Size/sizeof (CHAR16), L"\\");\r
9ba404d6 141 }\r
47d20b54 142\r
9ba404d6
JD
143 //\r
144 // Now append the absolute or relative path.\r
145 //\r
47d20b54 146 StrCatS (FullyQualifiedPath, Size/sizeof (CHAR16), InputPath);\r
9ba404d6
JD
147 }\r
148 }\r
149\r
47d20b54 150 PathCleanUpDirectories (FullyQualifiedPath);\r
9ba404d6
JD
151\r
152 return FullyQualifiedPath;\r
153}\r
154\r
2247dde4 155/**\r
156 Check if a Unicode character is a hexadecimal character.\r
157\r
1e6e84c7 158 This internal function checks if a Unicode character is a\r
a405b86d 159 numeric character. The valid hexadecimal characters are\r
2247dde4 160 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.\r
161\r
2247dde4 162 @param Char The character to check against.\r
163\r
164 @retval TRUE If the Char is a hexadecmial character.\r
165 @retval FALSE If the Char is not a hexadecmial character.\r
166\r
167**/\r
168BOOLEAN\r
169EFIAPI\r
969c783b 170ShellIsHexaDecimalDigitCharacter (\r
47d20b54 171 IN CHAR16 Char\r
a405b86d 172 )\r
173{\r
47d20b54 174 return (BOOLEAN)((Char >= L'0' && Char <= L'9') || (Char >= L'A' && Char <= L'F') || (Char >= L'a' && Char <= L'f'));\r
2247dde4 175}\r
94b17fa1 176\r
177/**\r
a405b86d 178 Check if a Unicode character is a decimal character.\r
179\r
180 This internal function checks if a Unicode character is a\r
181 decimal character. The valid characters are\r
182 L'0' to L'9'.\r
183\r
184\r
185 @param Char The character to check against.\r
186\r
187 @retval TRUE If the Char is a hexadecmial character.\r
188 @retval FALSE If the Char is not a hexadecmial character.\r
189\r
190**/\r
191BOOLEAN\r
192EFIAPI\r
193ShellIsDecimalDigitCharacter (\r
47d20b54 194 IN CHAR16 Char\r
a405b86d 195 )\r
196{\r
47d20b54 197 return (BOOLEAN)(Char >= L'0' && Char <= L'9');\r
a405b86d 198}\r
199\r
200/**\r
201 Helper function to find ShellEnvironment2 for constructor.\r
202\r
203 @param[in] ImageHandle A copy of the calling image's handle.\r
beab0fc5 204\r
205 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.\r
94b17fa1 206**/\r
207EFI_STATUS\r
94b17fa1 208ShellFindSE2 (\r
47d20b54 209 IN EFI_HANDLE ImageHandle\r
a405b86d 210 )\r
211{\r
94b17fa1 212 EFI_STATUS Status;\r
213 EFI_HANDLE *Buffer;\r
214 UINTN BufferSize;\r
215 UINTN HandleIndex;\r
216\r
217 BufferSize = 0;\r
47d20b54
MK
218 Buffer = NULL;\r
219 Status = gBS->OpenProtocol (\r
220 ImageHandle,\r
221 &gEfiShellEnvironment2Guid,\r
222 (VOID **)&mEfiShellEnvironment2,\r
223 ImageHandle,\r
224 NULL,\r
225 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
226 );\r
94b17fa1 227 //\r
228 // look for the mEfiShellEnvironment2 protocol at a higher level\r
229 //\r
47d20b54 230 if (EFI_ERROR (Status) || !(CompareGuid (&mEfiShellEnvironment2->SESGuid, &gEfiShellEnvironment2ExtGuid))) {\r
94b17fa1 231 //\r
232 // figure out how big of a buffer we need.\r
233 //\r
47d20b54
MK
234 Status = gBS->LocateHandle (\r
235 ByProtocol,\r
236 &gEfiShellEnvironment2Guid,\r
237 NULL, // ignored for ByProtocol\r
238 &BufferSize,\r
239 Buffer\r
240 );\r
2247dde4 241 //\r
242 // maybe it's not there???\r
243 //\r
244 if (Status == EFI_BUFFER_TOO_SMALL) {\r
47d20b54 245 Buffer = (EFI_HANDLE *)AllocateZeroPool (BufferSize);\r
beab0fc5 246 if (Buffer == NULL) {\r
247 return (EFI_OUT_OF_RESOURCES);\r
248 }\r
47d20b54
MK
249\r
250 Status = gBS->LocateHandle (\r
251 ByProtocol,\r
252 &gEfiShellEnvironment2Guid,\r
253 NULL, // ignored for ByProtocol\r
254 &BufferSize,\r
255 Buffer\r
256 );\r
2247dde4 257 }\r
47d20b54
MK
258\r
259 if (!EFI_ERROR (Status) && (Buffer != NULL)) {\r
94b17fa1 260 //\r
261 // now parse the list of returned handles\r
262 //\r
263 Status = EFI_NOT_FOUND;\r
47d20b54
MK
264 for (HandleIndex = 0; HandleIndex < (BufferSize/sizeof (Buffer[0])); HandleIndex++) {\r
265 Status = gBS->OpenProtocol (\r
266 Buffer[HandleIndex],\r
267 &gEfiShellEnvironment2Guid,\r
268 (VOID **)&mEfiShellEnvironment2,\r
269 ImageHandle,\r
270 NULL,\r
271 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
272 );\r
273 if (CompareGuid (&mEfiShellEnvironment2->SESGuid, &gEfiShellEnvironment2ExtGuid)) {\r
94b17fa1 274 mEfiShellEnvironment2Handle = Buffer[HandleIndex];\r
47d20b54 275 Status = EFI_SUCCESS;\r
94b17fa1 276 break;\r
277 }\r
278 }\r
279 }\r
280 }\r
47d20b54 281\r
94b17fa1 282 if (Buffer != NULL) {\r
283 FreePool (Buffer);\r
284 }\r
47d20b54 285\r
94b17fa1 286 return (Status);\r
287}\r
288\r
252d9457 289/**\r
b0934ac4 290 Function to do most of the work of the constructor. Allows for calling\r
a405b86d 291 multiple times without complete re-initialization.\r
292\r
293 @param[in] ImageHandle A copy of the ImageHandle.\r
294 @param[in] SystemTable A pointer to the SystemTable for the application.\r
252d9457 295\r
296 @retval EFI_SUCCESS The operationw as successful.\r
a405b86d 297**/\r
94b17fa1 298EFI_STATUS\r
d2b4564b 299ShellLibConstructorWorker (\r
94b17fa1 300 IN EFI_HANDLE ImageHandle,\r
301 IN EFI_SYSTEM_TABLE *SystemTable\r
a405b86d 302 )\r
303{\r
304 EFI_STATUS Status;\r
ecd3d59f 305\r
68b07eba 306 if (gEfiShellProtocol == NULL) {\r
a405b86d 307 //\r
68b07eba 308 // UEFI 2.0 shell interfaces (used preferentially)\r
a405b86d 309 //\r
68b07eba 310 Status = gBS->OpenProtocol (\r
47d20b54
MK
311 ImageHandle,\r
312 &gEfiShellProtocolGuid,\r
313 (VOID **)&gEfiShellProtocol,\r
314 ImageHandle,\r
315 NULL,\r
316 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
317 );\r
68b07eba
RN
318 if (EFI_ERROR (Status)) {\r
319 //\r
320 // Search for the shell protocol\r
321 //\r
322 Status = gBS->LocateProtocol (\r
47d20b54
MK
323 &gEfiShellProtocolGuid,\r
324 NULL,\r
325 (VOID **)&gEfiShellProtocol\r
326 );\r
68b07eba
RN
327 if (EFI_ERROR (Status)) {\r
328 gEfiShellProtocol = NULL;\r
329 }\r
a405b86d 330 }\r
94b17fa1 331 }\r
68b07eba
RN
332\r
333 if (gEfiShellParametersProtocol == NULL) {\r
334 Status = gBS->OpenProtocol (\r
47d20b54
MK
335 ImageHandle,\r
336 &gEfiShellParametersProtocolGuid,\r
337 (VOID **)&gEfiShellParametersProtocol,\r
338 ImageHandle,\r
339 NULL,\r
340 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
341 );\r
68b07eba
RN
342 if (EFI_ERROR (Status)) {\r
343 gEfiShellParametersProtocol = NULL;\r
344 }\r
94b17fa1 345 }\r
346\r
3d29f8c5 347 if (gEfiShellProtocol == NULL) {\r
94b17fa1 348 //\r
349 // Moved to seperate function due to complexity\r
350 //\r
47d20b54 351 Status = ShellFindSE2 (ImageHandle);\r
94b17fa1 352\r
47d20b54
MK
353 if (EFI_ERROR (Status)) {\r
354 DEBUG ((DEBUG_ERROR, "Status: 0x%08x\r\n", Status));\r
94b17fa1 355 mEfiShellEnvironment2 = NULL;\r
356 }\r
47d20b54
MK
357\r
358 Status = gBS->OpenProtocol (\r
359 ImageHandle,\r
360 &gEfiShellInterfaceGuid,\r
361 (VOID **)&mEfiShellInterface,\r
362 ImageHandle,\r
363 NULL,\r
364 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
365 );\r
366 if (EFI_ERROR (Status)) {\r
94b17fa1 367 mEfiShellInterface = NULL;\r
368 }\r
369 }\r
c9d92df0 370\r
94b17fa1 371 //\r
3d29f8c5
RN
372 // Getting either EDK Shell's ShellEnvironment2 and ShellInterface protocol\r
373 // or UEFI Shell's Shell protocol.\r
374 // When ShellLib is linked to a driver producing DynamicCommand protocol,\r
375 // ShellParameters protocol is set by DynamicCommand.Handler().\r
94b17fa1 376 //\r
47d20b54 377 if (((mEfiShellEnvironment2 != NULL) && (mEfiShellInterface != NULL)) ||\r
3d29f8c5 378 (gEfiShellProtocol != NULL)\r
47d20b54
MK
379 )\r
380 {\r
366f81a0 381 if (gEfiShellProtocol != NULL) {\r
382 FileFunctionMap.GetFileInfo = gEfiShellProtocol->GetFileInfo;\r
383 FileFunctionMap.SetFileInfo = gEfiShellProtocol->SetFileInfo;\r
384 FileFunctionMap.ReadFile = gEfiShellProtocol->ReadFile;\r
385 FileFunctionMap.WriteFile = gEfiShellProtocol->WriteFile;\r
386 FileFunctionMap.CloseFile = gEfiShellProtocol->CloseFile;\r
387 FileFunctionMap.DeleteFile = gEfiShellProtocol->DeleteFile;\r
388 FileFunctionMap.GetFilePosition = gEfiShellProtocol->GetFilePosition;\r
389 FileFunctionMap.SetFilePosition = gEfiShellProtocol->SetFilePosition;\r
390 FileFunctionMap.FlushFile = gEfiShellProtocol->FlushFile;\r
391 FileFunctionMap.GetFileSize = gEfiShellProtocol->GetFileSize;\r
d2b4564b 392 } else {\r
a405b86d 393 FileFunctionMap.GetFileInfo = (EFI_SHELL_GET_FILE_INFO)FileHandleGetInfo;\r
394 FileFunctionMap.SetFileInfo = (EFI_SHELL_SET_FILE_INFO)FileHandleSetInfo;\r
395 FileFunctionMap.ReadFile = (EFI_SHELL_READ_FILE)FileHandleRead;\r
396 FileFunctionMap.WriteFile = (EFI_SHELL_WRITE_FILE)FileHandleWrite;\r
397 FileFunctionMap.CloseFile = (EFI_SHELL_CLOSE_FILE)FileHandleClose;\r
398 FileFunctionMap.DeleteFile = (EFI_SHELL_DELETE_FILE)FileHandleDelete;\r
399 FileFunctionMap.GetFilePosition = (EFI_SHELL_GET_FILE_POSITION)FileHandleGetPosition;\r
400 FileFunctionMap.SetFilePosition = (EFI_SHELL_SET_FILE_POSITION)FileHandleSetPosition;\r
401 FileFunctionMap.FlushFile = (EFI_SHELL_FLUSH_FILE)FileHandleFlush;\r
402 FileFunctionMap.GetFileSize = (EFI_SHELL_GET_FILE_SIZE)FileHandleGetSize;\r
d2b4564b 403 }\r
47d20b54 404\r
94b17fa1 405 return (EFI_SUCCESS);\r
406 }\r
47d20b54 407\r
94b17fa1 408 return (EFI_NOT_FOUND);\r
409}\r
47d20b54 410\r
d2b4564b 411/**\r
412 Constructor for the Shell library.\r
413\r
414 Initialize the library and determine if the underlying is a UEFI Shell 2.0 or an EFI shell.\r
415\r
416 @param ImageHandle the image handle of the process\r
417 @param SystemTable the EFI System Table pointer\r
418\r
419 @retval EFI_SUCCESS the initialization was complete sucessfully\r
420 @return others an error ocurred during initialization\r
421**/\r
422EFI_STATUS\r
423EFIAPI\r
424ShellLibConstructor (\r
425 IN EFI_HANDLE ImageHandle,\r
426 IN EFI_SYSTEM_TABLE *SystemTable\r
a405b86d 427 )\r
428{\r
d2b4564b 429 mEfiShellEnvironment2 = NULL;\r
366f81a0 430 gEfiShellProtocol = NULL;\r
431 gEfiShellParametersProtocol = NULL;\r
d2b4564b 432 mEfiShellInterface = NULL;\r
433 mEfiShellEnvironment2Handle = NULL;\r
2cf9ecd2 434 mUnicodeCollationProtocol = NULL;\r
583448b4 435\r
d2b4564b 436 //\r
437 // verify that auto initialize is not set false\r
1e6e84c7 438 //\r
47d20b54 439 if (PcdGetBool (PcdShellLibAutoInitialize) == 0) {\r
d2b4564b 440 return (EFI_SUCCESS);\r
441 }\r
1e6e84c7 442\r
47d20b54 443 return (ShellLibConstructorWorker (ImageHandle, SystemTable));\r
d2b4564b 444}\r
94b17fa1 445\r
446/**\r
a405b86d 447 Destructor for the library. free any resources.\r
448\r
449 @param[in] ImageHandle A copy of the ImageHandle.\r
450 @param[in] SystemTable A pointer to the SystemTable for the application.\r
451\r
452 @retval EFI_SUCCESS The operation was successful.\r
453 @return An error from the CloseProtocol function.\r
94b17fa1 454**/\r
455EFI_STATUS\r
456EFIAPI\r
457ShellLibDestructor (\r
458 IN EFI_HANDLE ImageHandle,\r
459 IN EFI_SYSTEM_TABLE *SystemTable\r
a405b86d 460 )\r
461{\r
47d20b54 462 EFI_STATUS Status;\r
68b07eba 463\r
94b17fa1 464 if (mEfiShellEnvironment2 != NULL) {\r
47d20b54
MK
465 Status = gBS->CloseProtocol (\r
466 mEfiShellEnvironment2Handle == NULL ? ImageHandle : mEfiShellEnvironment2Handle,\r
467 &gEfiShellEnvironment2Guid,\r
468 ImageHandle,\r
469 NULL\r
470 );\r
68b07eba
RN
471 if (!EFI_ERROR (Status)) {\r
472 mEfiShellEnvironment2 = NULL;\r
473 mEfiShellEnvironment2Handle = NULL;\r
474 }\r
94b17fa1 475 }\r
47d20b54 476\r
94b17fa1 477 if (mEfiShellInterface != NULL) {\r
47d20b54
MK
478 Status = gBS->CloseProtocol (\r
479 ImageHandle,\r
480 &gEfiShellInterfaceGuid,\r
481 ImageHandle,\r
482 NULL\r
483 );\r
68b07eba
RN
484 if (!EFI_ERROR (Status)) {\r
485 mEfiShellInterface = NULL;\r
486 }\r
94b17fa1 487 }\r
47d20b54 488\r
366f81a0 489 if (gEfiShellProtocol != NULL) {\r
47d20b54
MK
490 Status = gBS->CloseProtocol (\r
491 ImageHandle,\r
492 &gEfiShellProtocolGuid,\r
493 ImageHandle,\r
494 NULL\r
495 );\r
68b07eba
RN
496 if (!EFI_ERROR (Status)) {\r
497 gEfiShellProtocol = NULL;\r
498 }\r
94b17fa1 499 }\r
47d20b54 500\r
366f81a0 501 if (gEfiShellParametersProtocol != NULL) {\r
47d20b54
MK
502 Status = gBS->CloseProtocol (\r
503 ImageHandle,\r
504 &gEfiShellParametersProtocolGuid,\r
505 ImageHandle,\r
506 NULL\r
507 );\r
68b07eba
RN
508 if (!EFI_ERROR (Status)) {\r
509 gEfiShellParametersProtocol = NULL;\r
510 }\r
94b17fa1 511 }\r
ecd3d59f 512\r
94b17fa1 513 return (EFI_SUCCESS);\r
514}\r
d2b4564b 515\r
516/**\r
517 This function causes the shell library to initialize itself. If the shell library\r
518 is already initialized it will de-initialize all the current protocol poitners and\r
519 re-populate them again.\r
520\r
521 When the library is used with PcdShellLibAutoInitialize set to true this function\r
522 will return EFI_SUCCESS and perform no actions.\r
523\r
524 This function is intended for internal access for shell commands only.\r
525\r
526 @retval EFI_SUCCESS the initialization was complete sucessfully\r
527\r
528**/\r
529EFI_STATUS\r
530EFIAPI\r
531ShellInitialize (\r
e7275d3f 532 VOID\r
a405b86d 533 )\r
534{\r
47d20b54 535 EFI_STATUS Status;\r
4a7518d3 536\r
d2b4564b 537 //\r
538 // if auto initialize is not false then skip\r
539 //\r
47d20b54 540 if (PcdGetBool (PcdShellLibAutoInitialize) != 0) {\r
d2b4564b 541 return (EFI_SUCCESS);\r
542 }\r
543\r
544 //\r
545 // deinit the current stuff\r
546 //\r
4a7518d3
LE
547 Status = ShellLibDestructor (gImageHandle, gST);\r
548 ASSERT_EFI_ERROR (Status);\r
d2b4564b 549\r
550 //\r
551 // init the new stuff\r
552 //\r
47d20b54 553 return (ShellLibConstructorWorker (gImageHandle, gST));\r
d2b4564b 554}\r
555\r
94b17fa1 556/**\r
1e6e84c7 557 This function will retrieve the information about the file for the handle\r
94b17fa1 558 specified and store it in allocated pool memory.\r
559\r
1e6e84c7 560 This function allocates a buffer to store the file's information. It is the\r
69817bf8 561 caller's responsibility to free the buffer\r
94b17fa1 562\r
1e6e84c7 563 @param FileHandle The file handle of the file for which information is\r
94b17fa1 564 being requested.\r
565\r
566 @retval NULL information could not be retrieved.\r
567\r
568 @return the information about the file\r
569**/\r
47d20b54 570EFI_FILE_INFO *\r
94b17fa1 571EFIAPI\r
572ShellGetFileInfo (\r
47d20b54 573 IN SHELL_FILE_HANDLE FileHandle\r
a405b86d 574 )\r
575{\r
47d20b54 576 return (FileFunctionMap.GetFileInfo (FileHandle));\r
94b17fa1 577}\r
578\r
579/**\r
a405b86d 580 This function sets the information about the file for the opened handle\r
94b17fa1 581 specified.\r
582\r
a405b86d 583 @param[in] FileHandle The file handle of the file for which information\r
584 is being set.\r
94b17fa1 585\r
a405b86d 586 @param[in] FileInfo The information to set.\r
94b17fa1 587\r
b0934ac4 588 @retval EFI_SUCCESS The information was set.\r
a405b86d 589 @retval EFI_INVALID_PARAMETER A parameter was out of range or invalid.\r
590 @retval EFI_UNSUPPORTED The FileHandle does not support FileInfo.\r
b0934ac4 591 @retval EFI_NO_MEDIA The device has no medium.\r
592 @retval EFI_DEVICE_ERROR The device reported an error.\r
593 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
594 @retval EFI_WRITE_PROTECTED The file or medium is write protected.\r
a405b86d 595 @retval EFI_ACCESS_DENIED The file was opened read only.\r
596 @retval EFI_VOLUME_FULL The volume is full.\r
94b17fa1 597**/\r
598EFI_STATUS\r
599EFIAPI\r
600ShellSetFileInfo (\r
47d20b54
MK
601 IN SHELL_FILE_HANDLE FileHandle,\r
602 IN EFI_FILE_INFO *FileInfo\r
a405b86d 603 )\r
604{\r
47d20b54 605 return (FileFunctionMap.SetFileInfo (FileHandle, FileInfo));\r
1e6e84c7 606}\r
607\r
47d20b54
MK
608/**\r
609This function will open a file or directory referenced by DevicePath.\r
610\r
611This function opens a file with the open mode according to the file path. The\r
612Attributes is valid only for EFI_FILE_MODE_CREATE.\r
613\r
614@param FilePath on input the device path to the file. On output\r
615 the remaining device path.\r
616@param FileHandle pointer to the file handle.\r
617@param OpenMode the mode to open the file with.\r
618@param Attributes the file's file attributes.\r
619\r
620@retval EFI_SUCCESS The information was set.\r
621@retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.\r
622@retval EFI_UNSUPPORTED Could not open the file path.\r
623@retval EFI_NOT_FOUND The specified file could not be found on the\r
624 device or the file system could not be found on\r
625 the device.\r
626@retval EFI_NO_MEDIA The device has no medium.\r
627@retval EFI_MEDIA_CHANGED The device has a different medium in it or the\r
628 medium is no longer supported.\r
629@retval EFI_DEVICE_ERROR The device reported an error.\r
630@retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
631@retval EFI_WRITE_PROTECTED The file or medium is write protected.\r
632@retval EFI_ACCESS_DENIED The file was opened read only.\r
633@retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the\r
634 file.\r
635@retval EFI_VOLUME_FULL The volume is full.\r
94b17fa1 636**/\r
637EFI_STATUS\r
638EFIAPI\r
47d20b54
MK
639ShellOpenFileByDevicePath (\r
640 IN OUT EFI_DEVICE_PATH_PROTOCOL **FilePath,\r
641 OUT SHELL_FILE_HANDLE *FileHandle,\r
642 IN UINT64 OpenMode,\r
643 IN UINT64 Attributes\r
a405b86d 644 )\r
645{\r
47d20b54
MK
646 CHAR16 *FileName;\r
647 EFI_STATUS Status;\r
648 EFI_FILE_PROTOCOL *File;\r
94b17fa1 649\r
47d20b54 650 if ((FilePath == NULL) || (FileHandle == NULL)) {\r
92a5447e 651 return (EFI_INVALID_PARAMETER);\r
652 }\r
653\r
1e6e84c7 654 //\r
94b17fa1 655 // which shell interface should we use\r
656 //\r
366f81a0 657 if (gEfiShellProtocol != NULL) {\r
94b17fa1 658 //\r
659 // use UEFI Shell 2.0 method.\r
660 //\r
47d20b54 661 FileName = gEfiShellProtocol->GetFilePathFromDevicePath (*FilePath);\r
94b17fa1 662 if (FileName == NULL) {\r
663 return (EFI_INVALID_PARAMETER);\r
664 }\r
47d20b54
MK
665\r
666 Status = ShellOpenFileByName (FileName, FileHandle, OpenMode, Attributes);\r
667 FreePool (FileName);\r
94b17fa1 668 return (Status);\r
1e6e84c7 669 }\r
d2b4564b 670\r
d2b4564b 671 //\r
672 // use old shell method.\r
673 //\r
9becf2f0 674 Status = EfiOpenFileByDevicePath (FilePath, &File, OpenMode, Attributes);\r
d2b4564b 675 if (EFI_ERROR (Status)) {\r
676 return Status;\r
677 }\r
a405b86d 678\r
679 //\r
680 // This is a weak spot since if the undefined SHELL_FILE_HANDLE format changes this must change also!\r
681 //\r
47d20b54 682 *FileHandle = (VOID *)File;\r
d2b4564b 683 return (EFI_SUCCESS);\r
94b17fa1 684}\r
685\r
686/**\r
687 This function will open a file or directory referenced by filename.\r
688\r
1e6e84c7 689 If return is EFI_SUCCESS, the Filehandle is the opened file's handle;\r
690 otherwise, the Filehandle is NULL. The Attributes is valid only for\r
94b17fa1 691 EFI_FILE_MODE_CREATE.\r
692\r
92a5447e 693 if FileName is NULL then ASSERT()\r
94b17fa1 694\r
b0934ac4 695 @param FileName pointer to file name\r
696 @param FileHandle pointer to the file handle.\r
697 @param OpenMode the mode to open the file with.\r
698 @param Attributes the file's file attributes.\r
94b17fa1 699\r
b0934ac4 700 @retval EFI_SUCCESS The information was set.\r
701 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.\r
702 @retval EFI_UNSUPPORTED Could not open the file path.\r
703 @retval EFI_NOT_FOUND The specified file could not be found on the\r
1e6e84c7 704 device or the file system could not be found\r
94b17fa1 705 on the device.\r
b0934ac4 706 @retval EFI_NO_MEDIA The device has no medium.\r
707 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the\r
94b17fa1 708 medium is no longer supported.\r
b0934ac4 709 @retval EFI_DEVICE_ERROR The device reported an error.\r
710 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
711 @retval EFI_WRITE_PROTECTED The file or medium is write protected.\r
712 @retval EFI_ACCESS_DENIED The file was opened read only.\r
713 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the\r
94b17fa1 714 file.\r
b0934ac4 715 @retval EFI_VOLUME_FULL The volume is full.\r
94b17fa1 716**/\r
717EFI_STATUS\r
718EFIAPI\r
47d20b54
MK
719ShellOpenFileByName (\r
720 IN CONST CHAR16 *FileName,\r
721 OUT SHELL_FILE_HANDLE *FileHandle,\r
722 IN UINT64 OpenMode,\r
723 IN UINT64 Attributes\r
a405b86d 724 )\r
725{\r
47d20b54
MK
726 EFI_DEVICE_PATH_PROTOCOL *FilePath;\r
727 EFI_STATUS Status;\r
728 EFI_FILE_INFO *FileInfo;\r
729 CHAR16 *FileNameCopy;\r
730 EFI_STATUS Status2;\r
94b17fa1 731\r
732 //\r
733 // ASSERT if FileName is NULL\r
734 //\r
47d20b54 735 ASSERT (FileName != NULL);\r
94b17fa1 736\r
a405b86d 737 if (FileName == NULL) {\r
738 return (EFI_INVALID_PARAMETER);\r
739 }\r
740\r
366f81a0 741 if (gEfiShellProtocol != NULL) {\r
21a86a7d 742 if ((OpenMode & EFI_FILE_MODE_CREATE) == EFI_FILE_MODE_CREATE) {\r
21a86a7d 743 //\r
744 // Create only a directory\r
745 //\r
746 if ((Attributes & EFI_FILE_DIRECTORY) == EFI_FILE_DIRECTORY) {\r
47d20b54 747 return ShellCreateDirectory (FileName, FileHandle);\r
21a86a7d 748 }\r
749\r
750 //\r
751 // Create the directory to create the file in\r
752 //\r
753 FileNameCopy = AllocateCopyPool (StrSize (FileName), FileName);\r
52208978 754 if (FileNameCopy == NULL) {\r
21a86a7d 755 return (EFI_OUT_OF_RESOURCES);\r
756 }\r
47d20b54 757\r
21a86a7d 758 PathCleanUpDirectories (FileNameCopy);\r
759 if (PathRemoveLastItem (FileNameCopy)) {\r
47d20b54 760 if (!EFI_ERROR (ShellCreateDirectory (FileNameCopy, FileHandle))) {\r
983fffd4
JC
761 ShellCloseFile (FileHandle);\r
762 }\r
21a86a7d 763 }\r
47d20b54 764\r
21a86a7d 765 SHELL_FREE_NON_NULL (FileNameCopy);\r
a405b86d 766 }\r
21a86a7d 767\r
94b17fa1 768 //\r
21a86a7d 769 // Use UEFI Shell 2.0 method to create the file\r
94b17fa1 770 //\r
47d20b54
MK
771 Status = gEfiShellProtocol->OpenFileByName (\r
772 FileName,\r
773 FileHandle,\r
774 OpenMode\r
775 );\r
776 if (EFI_ERROR (Status)) {\r
f9c3b1b5
VO
777 return Status;\r
778 }\r
583448b4 779\r
2cf9ecd2 780 if (mUnicodeCollationProtocol == NULL) {\r
47d20b54 781 Status = gBS->LocateProtocol (&gEfiUnicodeCollation2ProtocolGuid, NULL, (VOID **)&mUnicodeCollationProtocol);\r
2cf9ecd2
TS
782 if (EFI_ERROR (Status)) {\r
783 gEfiShellProtocol->CloseFile (*FileHandle);\r
784 return Status;\r
785 }\r
786 }\r
787\r
47d20b54
MK
788 if ((mUnicodeCollationProtocol->StriColl (mUnicodeCollationProtocol, (CHAR16 *)FileName, L"NUL") != 0) &&\r
789 (mUnicodeCollationProtocol->StriColl (mUnicodeCollationProtocol, (CHAR16 *)FileName, L"NULL") != 0) &&\r
790 !EFI_ERROR (Status) && ((OpenMode & EFI_FILE_MODE_CREATE) != 0))\r
791 {\r
792 FileInfo = FileFunctionMap.GetFileInfo (*FileHandle);\r
793 ASSERT (FileInfo != NULL);\r
b1f95a06 794 FileInfo->Attribute = Attributes;\r
47d20b54
MK
795 Status2 = FileFunctionMap.SetFileInfo (*FileHandle, FileInfo);\r
796 FreePool (FileInfo);\r
dd17e3fe 797 if (EFI_ERROR (Status2)) {\r
47d20b54 798 gEfiShellProtocol->CloseFile (*FileHandle);\r
dd17e3fe 799 }\r
47d20b54 800\r
dd17e3fe 801 Status = Status2;\r
b1f95a06 802 }\r
47d20b54 803\r
b1f95a06 804 return (Status);\r
1e6e84c7 805 }\r
47d20b54 806\r
94b17fa1 807 //\r
808 // Using EFI Shell version\r
809 // this means convert name to path and call that function\r
810 // since this will use EFI method again that will open it.\r
811 //\r
47d20b54
MK
812 ASSERT (mEfiShellEnvironment2 != NULL);\r
813 FilePath = mEfiShellEnvironment2->NameToPath ((CHAR16 *)FileName);\r
90bfa227 814 if (FilePath != NULL) {\r
47d20b54
MK
815 return (ShellOpenFileByDevicePath (\r
816 &FilePath,\r
817 FileHandle,\r
818 OpenMode,\r
819 Attributes\r
820 ));\r
94b17fa1 821 }\r
47d20b54 822\r
94b17fa1 823 return (EFI_DEVICE_ERROR);\r
824}\r
47d20b54 825\r
94b17fa1 826/**\r
827 This function create a directory\r
828\r
1e6e84c7 829 If return is EFI_SUCCESS, the Filehandle is the opened directory's handle;\r
830 otherwise, the Filehandle is NULL. If the directory already existed, this\r
94b17fa1 831 function opens the existing directory.\r
832\r
b0934ac4 833 @param DirectoryName pointer to directory name\r
834 @param FileHandle pointer to the file handle.\r
94b17fa1 835\r
b0934ac4 836 @retval EFI_SUCCESS The information was set.\r
837 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.\r
838 @retval EFI_UNSUPPORTED Could not open the file path.\r
839 @retval EFI_NOT_FOUND The specified file could not be found on the\r
1e6e84c7 840 device or the file system could not be found\r
94b17fa1 841 on the device.\r
b0934ac4 842 @retval EFI_NO_MEDIA The device has no medium.\r
843 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the\r
94b17fa1 844 medium is no longer supported.\r
b0934ac4 845 @retval EFI_DEVICE_ERROR The device reported an error.\r
846 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
847 @retval EFI_WRITE_PROTECTED The file or medium is write protected.\r
848 @retval EFI_ACCESS_DENIED The file was opened read only.\r
849 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the\r
94b17fa1 850 file.\r
b0934ac4 851 @retval EFI_VOLUME_FULL The volume is full.\r
94b17fa1 852 @sa ShellOpenFileByName\r
853**/\r
854EFI_STATUS\r
855EFIAPI\r
47d20b54
MK
856ShellCreateDirectory (\r
857 IN CONST CHAR16 *DirectoryName,\r
858 OUT SHELL_FILE_HANDLE *FileHandle\r
a405b86d 859 )\r
860{\r
366f81a0 861 if (gEfiShellProtocol != NULL) {\r
2247dde4 862 //\r
863 // Use UEFI Shell 2.0 method\r
864 //\r
47d20b54
MK
865 return (gEfiShellProtocol->CreateFile (\r
866 DirectoryName,\r
867 EFI_FILE_DIRECTORY,\r
868 FileHandle\r
869 ));\r
2247dde4 870 } else {\r
47d20b54
MK
871 return (ShellOpenFileByName (\r
872 DirectoryName,\r
873 FileHandle,\r
874 EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE,\r
875 EFI_FILE_DIRECTORY\r
876 ));\r
2247dde4 877 }\r
94b17fa1 878}\r
879\r
880/**\r
881 This function reads information from an opened file.\r
882\r
1e6e84c7 883 If FileHandle is not a directory, the function reads the requested number of\r
884 bytes from the file at the file's current position and returns them in Buffer.\r
94b17fa1 885 If the read goes beyond the end of the file, the read length is truncated to the\r
1e6e84c7 886 end of the file. The file's current position is increased by the number of bytes\r
887 returned. If FileHandle is a directory, the function reads the directory entry\r
888 at the file's current position and returns the entry in Buffer. If the Buffer\r
889 is not large enough to hold the current directory entry, then\r
890 EFI_BUFFER_TOO_SMALL is returned and the current file position is not updated.\r
891 BufferSize is set to be the size of the buffer needed to read the entry. On\r
892 success, the current position is updated to the next directory entry. If there\r
893 are no more directory entries, the read returns a zero-length buffer.\r
94b17fa1 894 EFI_FILE_INFO is the structure returned as the directory entry.\r
895\r
896 @param FileHandle the opened file handle\r
1e6e84c7 897 @param BufferSize on input the size of buffer in bytes. on return\r
94b17fa1 898 the number of bytes written.\r
899 @param Buffer the buffer to put read data into.\r
900\r
b0934ac4 901 @retval EFI_SUCCESS Data was read.\r
902 @retval EFI_NO_MEDIA The device has no media.\r
903 @retval EFI_DEVICE_ERROR The device reported an error.\r
904 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
905 @retval EFI_BUFFER_TO_SMALL Buffer is too small. ReadSize contains required\r
94b17fa1 906 size.\r
907\r
908**/\r
909EFI_STATUS\r
910EFIAPI\r
47d20b54
MK
911ShellReadFile (\r
912 IN SHELL_FILE_HANDLE FileHandle,\r
913 IN OUT UINTN *BufferSize,\r
914 OUT VOID *Buffer\r
a405b86d 915 )\r
916{\r
47d20b54 917 return (FileFunctionMap.ReadFile (FileHandle, BufferSize, Buffer));\r
94b17fa1 918}\r
919\r
94b17fa1 920/**\r
921 Write data to a file.\r
922\r
1e6e84c7 923 This function writes the specified number of bytes to the file at the current\r
924 file position. The current file position is advanced the actual number of bytes\r
925 written, which is returned in BufferSize. Partial writes only occur when there\r
926 has been a data error during the write attempt (such as "volume space full").\r
927 The file is automatically grown to hold the data if required. Direct writes to\r
94b17fa1 928 opened directories are not supported.\r
929\r
930 @param FileHandle The opened file for writing\r
931 @param BufferSize on input the number of bytes in Buffer. On output\r
932 the number of bytes written.\r
933 @param Buffer the buffer containing data to write is stored.\r
934\r
b0934ac4 935 @retval EFI_SUCCESS Data was written.\r
936 @retval EFI_UNSUPPORTED Writes to an open directory are not supported.\r
937 @retval EFI_NO_MEDIA The device has no media.\r
938 @retval EFI_DEVICE_ERROR The device reported an error.\r
939 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
940 @retval EFI_WRITE_PROTECTED The device is write-protected.\r
941 @retval EFI_ACCESS_DENIED The file was open for read only.\r
942 @retval EFI_VOLUME_FULL The volume is full.\r
94b17fa1 943**/\r
944EFI_STATUS\r
945EFIAPI\r
47d20b54
MK
946ShellWriteFile (\r
947 IN SHELL_FILE_HANDLE FileHandle,\r
948 IN OUT UINTN *BufferSize,\r
949 IN VOID *Buffer\r
a405b86d 950 )\r
951{\r
47d20b54 952 return (FileFunctionMap.WriteFile (FileHandle, BufferSize, Buffer));\r
94b17fa1 953}\r
954\r
1e6e84c7 955/**\r
94b17fa1 956 Close an open file handle.\r
957\r
1e6e84c7 958 This function closes a specified file handle. All "dirty" cached file data is\r
959 flushed to the device, and the file is closed. In all cases the handle is\r
94b17fa1 960 closed.\r
961\r
962@param FileHandle the file handle to close.\r
963\r
964@retval EFI_SUCCESS the file handle was closed sucessfully.\r
965**/\r
966EFI_STATUS\r
967EFIAPI\r
968ShellCloseFile (\r
47d20b54 969 IN SHELL_FILE_HANDLE *FileHandle\r
a405b86d 970 )\r
971{\r
47d20b54 972 return (FileFunctionMap.CloseFile (*FileHandle));\r
94b17fa1 973}\r
974\r
975/**\r
976 Delete a file and close the handle\r
977\r
978 This function closes and deletes a file. In all cases the file handle is closed.\r
1e6e84c7 979 If the file cannot be deleted, the warning code EFI_WARN_DELETE_FAILURE is\r
94b17fa1 980 returned, but the handle is still closed.\r
981\r
982 @param FileHandle the file handle to delete\r
983\r
984 @retval EFI_SUCCESS the file was closed sucessfully\r
1e6e84c7 985 @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not\r
94b17fa1 986 deleted\r
b0934ac4 987 @retval INVALID_PARAMETER One of the parameters has an invalid value.\r
94b17fa1 988**/\r
989EFI_STATUS\r
990EFIAPI\r
991ShellDeleteFile (\r
47d20b54 992 IN SHELL_FILE_HANDLE *FileHandle\r
a405b86d 993 )\r
994{\r
47d20b54 995 return (FileFunctionMap.DeleteFile (*FileHandle));\r
94b17fa1 996}\r
997\r
998/**\r
999 Set the current position in a file.\r
1000\r
1e6e84c7 1001 This function sets the current file position for the handle to the position\r
94b17fa1 1002 supplied. With the exception of seeking to position 0xFFFFFFFFFFFFFFFF, only\r
1e6e84c7 1003 absolute positioning is supported, and seeking past the end of the file is\r
1004 allowed (a subsequent write would grow the file). Seeking to position\r
94b17fa1 1005 0xFFFFFFFFFFFFFFFF causes the current position to be set to the end of the file.\r
1e6e84c7 1006 If FileHandle is a directory, the only position that may be set is zero. This\r
94b17fa1 1007 has the effect of starting the read process of the directory entries over.\r
1008\r
1009 @param FileHandle The file handle on which the position is being set\r
1010 @param Position Byte position from begining of file\r
1011\r
1012 @retval EFI_SUCCESS Operation completed sucessfully.\r
1e6e84c7 1013 @retval EFI_UNSUPPORTED the seek request for non-zero is not valid on\r
94b17fa1 1014 directories.\r
1015 @retval INVALID_PARAMETER One of the parameters has an invalid value.\r
1016**/\r
1017EFI_STATUS\r
1018EFIAPI\r
1019ShellSetFilePosition (\r
47d20b54 1020 IN SHELL_FILE_HANDLE FileHandle,\r
b0934ac4 1021 IN UINT64 Position\r
a405b86d 1022 )\r
1023{\r
47d20b54 1024 return (FileFunctionMap.SetFilePosition (FileHandle, Position));\r
94b17fa1 1025}\r
1026\r
1e6e84c7 1027/**\r
94b17fa1 1028 Gets a file's current position\r
1029\r
1e6e84c7 1030 This function retrieves the current file position for the file handle. For\r
1031 directories, the current file position has no meaning outside of the file\r
94b17fa1 1032 system driver and as such the operation is not supported. An error is returned\r
1033 if FileHandle is a directory.\r
1034\r
1035 @param FileHandle The open file handle on which to get the position.\r
1036 @param Position Byte position from begining of file.\r
1037\r
1038 @retval EFI_SUCCESS the operation completed sucessfully.\r
1039 @retval INVALID_PARAMETER One of the parameters has an invalid value.\r
1040 @retval EFI_UNSUPPORTED the request is not valid on directories.\r
1041**/\r
1042EFI_STATUS\r
1043EFIAPI\r
1044ShellGetFilePosition (\r
47d20b54
MK
1045 IN SHELL_FILE_HANDLE FileHandle,\r
1046 OUT UINT64 *Position\r
a405b86d 1047 )\r
1048{\r
47d20b54 1049 return (FileFunctionMap.GetFilePosition (FileHandle, Position));\r
94b17fa1 1050}\r
47d20b54 1051\r
94b17fa1 1052/**\r
1053 Flushes data on a file\r
1e6e84c7 1054\r
94b17fa1 1055 This function flushes all modified data associated with a file to a device.\r
1056\r
1057 @param FileHandle The file handle on which to flush data\r
1058\r
1059 @retval EFI_SUCCESS The data was flushed.\r
1060 @retval EFI_NO_MEDIA The device has no media.\r
1061 @retval EFI_DEVICE_ERROR The device reported an error.\r
1062 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
1063 @retval EFI_WRITE_PROTECTED The file or medium is write protected.\r
1064 @retval EFI_ACCESS_DENIED The file was opened for read only.\r
1065**/\r
1066EFI_STATUS\r
1067EFIAPI\r
1068ShellFlushFile (\r
47d20b54 1069 IN SHELL_FILE_HANDLE FileHandle\r
a405b86d 1070 )\r
1071{\r
47d20b54 1072 return (FileFunctionMap.FlushFile (FileHandle));\r
94b17fa1 1073}\r
1074\r
b0934ac4 1075/** Retrieve first entry from a directory.\r
94b17fa1 1076\r
b0934ac4 1077 This function takes an open directory handle and gets information from the\r
1078 first entry in the directory. A buffer is allocated to contain\r
1079 the information and a pointer to the buffer is returned in *Buffer. The\r
1080 caller can use ShellFindNextFile() to get subsequent directory entries.\r
94b17fa1 1081\r
b0934ac4 1082 The buffer will be freed by ShellFindNextFile() when the last directory\r
1083 entry is read. Otherwise, the caller must free the buffer, using FreePool,\r
1084 when finished with it.\r
1085\r
1086 @param[in] DirHandle The file handle of the directory to search.\r
1087 @param[out] Buffer The pointer to the buffer for the file's information.\r
94b17fa1 1088\r
1089 @retval EFI_SUCCESS Found the first file.\r
1090 @retval EFI_NOT_FOUND Cannot find the directory.\r
1091 @retval EFI_NO_MEDIA The device has no media.\r
1092 @retval EFI_DEVICE_ERROR The device reported an error.\r
1093 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
1094 @return Others status of ShellGetFileInfo, ShellSetFilePosition,\r
1095 or ShellReadFile\r
1096**/\r
1097EFI_STATUS\r
1098EFIAPI\r
1099ShellFindFirstFile (\r
47d20b54
MK
1100 IN SHELL_FILE_HANDLE DirHandle,\r
1101 OUT EFI_FILE_INFO **Buffer\r
a405b86d 1102 )\r
1103{\r
94b17fa1 1104 //\r
d2b4564b 1105 // pass to file handle lib\r
94b17fa1 1106 //\r
47d20b54 1107 return (FileHandleFindFirstFile (DirHandle, Buffer));\r
94b17fa1 1108}\r
47d20b54 1109\r
b0934ac4 1110/** Retrieve next entries from a directory.\r
94b17fa1 1111\r
b0934ac4 1112 To use this function, the caller must first call the ShellFindFirstFile()\r
1113 function to get the first directory entry. Subsequent directory entries are\r
1114 retrieved by using the ShellFindNextFile() function. This function can\r
1115 be called several times to get each entry from the directory. If the call of\r
1116 ShellFindNextFile() retrieved the last directory entry, the next call of\r
1117 this function will set *NoFile to TRUE and free the buffer.\r
94b17fa1 1118\r
b0934ac4 1119 @param[in] DirHandle The file handle of the directory.\r
1120 @param[out] Buffer The pointer to buffer for file's information.\r
1121 @param[out] NoFile The pointer to boolean when last file is found.\r
94b17fa1 1122\r
1123 @retval EFI_SUCCESS Found the next file, or reached last file\r
1124 @retval EFI_NO_MEDIA The device has no media.\r
1125 @retval EFI_DEVICE_ERROR The device reported an error.\r
1126 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
1127**/\r
1128EFI_STATUS\r
1129EFIAPI\r
47d20b54
MK
1130ShellFindNextFile (\r
1131 IN SHELL_FILE_HANDLE DirHandle,\r
1132 OUT EFI_FILE_INFO *Buffer,\r
1133 OUT BOOLEAN *NoFile\r
a405b86d 1134 )\r
1135{\r
94b17fa1 1136 //\r
d2b4564b 1137 // pass to file handle lib\r
94b17fa1 1138 //\r
47d20b54 1139 return (FileHandleFindNextFile (DirHandle, Buffer, NoFile));\r
94b17fa1 1140}\r
47d20b54 1141\r
94b17fa1 1142/**\r
1143 Retrieve the size of a file.\r
1144\r
1145 if FileHandle is NULL then ASSERT()\r
1146 if Size is NULL then ASSERT()\r
1147\r
1e6e84c7 1148 This function extracts the file size info from the FileHandle's EFI_FILE_INFO\r
94b17fa1 1149 data.\r
1150\r
1151 @param FileHandle file handle from which size is retrieved\r
1152 @param Size pointer to size\r
1153\r
1154 @retval EFI_SUCCESS operation was completed sucessfully\r
1155 @retval EFI_DEVICE_ERROR cannot access the file\r
1156**/\r
1157EFI_STATUS\r
1158EFIAPI\r
1159ShellGetFileSize (\r
47d20b54
MK
1160 IN SHELL_FILE_HANDLE FileHandle,\r
1161 OUT UINT64 *Size\r
a405b86d 1162 )\r
1163{\r
47d20b54 1164 return (FileFunctionMap.GetFileSize (FileHandle, Size));\r
94b17fa1 1165}\r
47d20b54 1166\r
94b17fa1 1167/**\r
1168 Retrieves the status of the break execution flag\r
1169\r
1170 this function is useful to check whether the application is being asked to halt by the shell.\r
1171\r
1172 @retval TRUE the execution break is enabled\r
1173 @retval FALSE the execution break is not enabled\r
1174**/\r
1175BOOLEAN\r
1176EFIAPI\r
47d20b54 1177ShellGetExecutionBreakFlag (\r
94b17fa1 1178 VOID\r
1179 )\r
1180{\r
1e6e84c7 1181 //\r
94b17fa1 1182 // Check for UEFI Shell 2.0 protocols\r
1183 //\r
366f81a0 1184 if (gEfiShellProtocol != NULL) {\r
94b17fa1 1185 //\r
1186 // We are using UEFI Shell 2.0; see if the event has been triggered\r
1187 //\r
47d20b54 1188 if (gBS->CheckEvent (gEfiShellProtocol->ExecutionBreak) != EFI_SUCCESS) {\r
94b17fa1 1189 return (FALSE);\r
1190 }\r
47d20b54 1191\r
94b17fa1 1192 return (TRUE);\r
1e6e84c7 1193 }\r
94b17fa1 1194\r
1195 //\r
1196 // using EFI Shell; call the function to check\r
1197 //\r
92a5447e 1198 if (mEfiShellEnvironment2 != NULL) {\r
47d20b54 1199 return (mEfiShellEnvironment2->GetExecutionBreak ());\r
92a5447e 1200 }\r
1201\r
1202 return (FALSE);\r
94b17fa1 1203}\r
47d20b54 1204\r
94b17fa1 1205/**\r
1206 return the value of an environment variable\r
1207\r
1e6e84c7 1208 this function gets the value of the environment variable set by the\r
94b17fa1 1209 ShellSetEnvironmentVariable function\r
1210\r
1211 @param EnvKey The key name of the environment variable.\r
1212\r
1213 @retval NULL the named environment variable does not exist.\r
1214 @return != NULL pointer to the value of the environment variable\r
1215**/\r
47d20b54 1216CONST CHAR16 *\r
94b17fa1 1217EFIAPI\r
1218ShellGetEnvironmentVariable (\r
47d20b54 1219 IN CONST CHAR16 *EnvKey\r
94b17fa1 1220 )\r
1221{\r
1e6e84c7 1222 //\r
94b17fa1 1223 // Check for UEFI Shell 2.0 protocols\r
1224 //\r
366f81a0 1225 if (gEfiShellProtocol != NULL) {\r
47d20b54 1226 return (gEfiShellProtocol->GetEnv (EnvKey));\r
94b17fa1 1227 }\r
1228\r
1229 //\r
92a5447e 1230 // Check for EFI shell\r
94b17fa1 1231 //\r
92a5447e 1232 if (mEfiShellEnvironment2 != NULL) {\r
47d20b54 1233 return (mEfiShellEnvironment2->GetEnv ((CHAR16 *)EnvKey));\r
92a5447e 1234 }\r
94b17fa1 1235\r
92a5447e 1236 return NULL;\r
94b17fa1 1237}\r
47d20b54 1238\r
94b17fa1 1239/**\r
1240 set the value of an environment variable\r
1241\r
1242This function changes the current value of the specified environment variable. If the\r
1243environment variable exists and the Value is an empty string, then the environment\r
1244variable is deleted. If the environment variable exists and the Value is not an empty\r
1245string, then the value of the environment variable is changed. If the environment\r
1246variable does not exist and the Value is an empty string, there is no action. If the\r
1247environment variable does not exist and the Value is a non-empty string, then the\r
1248environment variable is created and assigned the specified value.\r
1249\r
1250 This is not supported pre-UEFI Shell 2.0.\r
1251\r
1252 @param EnvKey The key name of the environment variable.\r
1253 @param EnvVal The Value of the environment variable\r
1254 @param Volatile Indicates whether the variable is non-volatile (FALSE) or volatile (TRUE).\r
1255\r
1256 @retval EFI_SUCCESS the operation was completed sucessfully\r
1257 @retval EFI_UNSUPPORTED This operation is not allowed in pre UEFI 2.0 Shell environments\r
1258**/\r
1259EFI_STATUS\r
1260EFIAPI\r
1261ShellSetEnvironmentVariable (\r
47d20b54
MK
1262 IN CONST CHAR16 *EnvKey,\r
1263 IN CONST CHAR16 *EnvVal,\r
1264 IN BOOLEAN Volatile\r
94b17fa1 1265 )\r
1266{\r
1e6e84c7 1267 //\r
94b17fa1 1268 // Check for UEFI Shell 2.0 protocols\r
1269 //\r
366f81a0 1270 if (gEfiShellProtocol != NULL) {\r
47d20b54 1271 return (gEfiShellProtocol->SetEnv (EnvKey, EnvVal, Volatile));\r
1e6e84c7 1272 }\r
94b17fa1 1273\r
1274 //\r
1275 // This feature does not exist under EFI shell\r
1276 //\r
1277 return (EFI_UNSUPPORTED);\r
1278}\r
a405b86d 1279\r
94b17fa1 1280/**\r
a405b86d 1281 Cause the shell to parse and execute a command line.\r
94b17fa1 1282\r
1283 This function creates a nested instance of the shell and executes the specified\r
a405b86d 1284 command (CommandLine) with the specified environment (Environment). Upon return,\r
1285 the status code returned by the specified command is placed in StatusCode.\r
1286 If Environment is NULL, then the current environment is used and all changes made\r
1287 by the commands executed will be reflected in the current environment. If the\r
1288 Environment is non-NULL, then the changes made will be discarded.\r
1289 The CommandLine is executed from the current working directory on the current\r
1290 device.\r
1291\r
3877d0f5 1292 The EnvironmentVariables pararemeter is ignored in a pre-UEFI Shell 2.0\r
a405b86d 1293 environment. The values pointed to by the parameters will be unchanged by the\r
1294 ShellExecute() function. The Output parameter has no effect in a\r
1295 UEFI Shell 2.0 environment.\r
1296\r
1297 @param[in] ParentHandle The parent image starting the operation.\r
1298 @param[in] CommandLine The pointer to a NULL terminated command line.\r
1299 @param[in] Output True to display debug output. False to hide it.\r
1300 @param[in] EnvironmentVariables Optional pointer to array of environment variables\r
1301 in the form "x=y". If NULL, the current set is used.\r
1302 @param[out] Status The status of the run command line.\r
1303\r
1304 @retval EFI_SUCCESS The operation completed sucessfully. Status\r
1305 contains the status code returned.\r
1306 @retval EFI_INVALID_PARAMETER A parameter contains an invalid value.\r
1307 @retval EFI_OUT_OF_RESOURCES Out of resources.\r
1308 @retval EFI_UNSUPPORTED The operation is not allowed.\r
94b17fa1 1309**/\r
1310EFI_STATUS\r
1311EFIAPI\r
1312ShellExecute (\r
47d20b54
MK
1313 IN EFI_HANDLE *ParentHandle,\r
1314 IN CHAR16 *CommandLine OPTIONAL,\r
1315 IN BOOLEAN Output OPTIONAL,\r
1316 IN CHAR16 **EnvironmentVariables OPTIONAL,\r
1317 OUT EFI_STATUS *Status OPTIONAL\r
94b17fa1 1318 )\r
1319{\r
47d20b54
MK
1320 EFI_STATUS CmdStatus;\r
1321\r
1e6e84c7 1322 //\r
94b17fa1 1323 // Check for UEFI Shell 2.0 protocols\r
1324 //\r
366f81a0 1325 if (gEfiShellProtocol != NULL) {\r
94b17fa1 1326 //\r
1327 // Call UEFI Shell 2.0 version (not using Output parameter)\r
1328 //\r
47d20b54
MK
1329 return (gEfiShellProtocol->Execute (\r
1330 ParentHandle,\r
1331 CommandLine,\r
1332 EnvironmentVariables,\r
1333 Status\r
1334 ));\r
1e6e84c7 1335 }\r
92a5447e 1336\r
94b17fa1 1337 //\r
92a5447e 1338 // Check for EFI shell\r
94b17fa1 1339 //\r
92a5447e 1340 if (mEfiShellEnvironment2 != NULL) {\r
1341 //\r
3877d0f5 1342 // Call EFI Shell version.\r
92a5447e 1343 //\r
7eb6160d
LE
1344 // Due to an unfixable bug in the EdkShell implementation, we must\r
1345 // dereference "ParentHandle" here:\r
1346 //\r
1347 // 1. The EFI shell installs the EFI_SHELL_ENVIRONMENT2 protocol,\r
1348 // identified by gEfiShellEnvironment2Guid.\r
1349 // 2. The Execute() member function takes "ParentImageHandle" as first\r
1350 // parameter, with type (EFI_HANDLE*).\r
1351 // 3. In the EdkShell implementation, SEnvExecute() implements the\r
1352 // Execute() member function. It passes "ParentImageHandle" correctly to\r
1353 // SEnvDoExecute().\r
1354 // 4. SEnvDoExecute() takes the (EFI_HANDLE*), and passes it directly --\r
1355 // without de-referencing -- to the HandleProtocol() boot service.\r
1356 // 5. But HandleProtocol() takes an EFI_HANDLE.\r
1357 //\r
1358 // Therefore we must\r
1359 // - de-reference "ParentHandle" here, to mask the bug in\r
1360 // SEnvDoExecute(), and\r
1361 // - pass the resultant EFI_HANDLE as an (EFI_HANDLE*).\r
1362 //\r
47d20b54
MK
1363 CmdStatus = (mEfiShellEnvironment2->Execute (\r
1364 (EFI_HANDLE *)*ParentHandle,\r
92a5447e 1365 CommandLine,\r
47d20b54
MK
1366 Output\r
1367 ));\r
3877d0f5
BJ
1368 //\r
1369 // No Status output parameter so just use the returned status\r
1370 //\r
1371 if (Status != NULL) {\r
1372 *Status = CmdStatus;\r
1373 }\r
47d20b54 1374\r
3877d0f5
BJ
1375 //\r
1376 // If there was an error, we can't tell if it was from the command or from\r
1377 // the Execute() function, so we'll just assume the shell ran successfully\r
1378 // and the error came from the command.\r
1379 //\r
1380 return EFI_SUCCESS;\r
92a5447e 1381 }\r
1382\r
1383 return (EFI_UNSUPPORTED);\r
94b17fa1 1384}\r
3877d0f5 1385\r
94b17fa1 1386/**\r
1387 Retreives the current directory path\r
1388\r
1e6e84c7 1389 If the DeviceName is NULL, it returns the current device's current directory\r
1390 name. If the DeviceName is not NULL, it returns the current directory name\r
94b17fa1 1391 on specified drive.\r
1392\r
fbd2dfad
QS
1393 Note that the current directory string should exclude the tailing backslash character.\r
1394\r
94b17fa1 1395 @param DeviceName the name of the drive to get directory on\r
1396\r
1397 @retval NULL the directory does not exist\r
1398 @return != NULL the directory\r
1399**/\r
47d20b54 1400CONST CHAR16 *\r
94b17fa1 1401EFIAPI\r
1402ShellGetCurrentDir (\r
47d20b54 1403 IN CHAR16 *CONST DeviceName OPTIONAL\r
94b17fa1 1404 )\r
1405{\r
1e6e84c7 1406 //\r
94b17fa1 1407 // Check for UEFI Shell 2.0 protocols\r
1408 //\r
366f81a0 1409 if (gEfiShellProtocol != NULL) {\r
47d20b54 1410 return (gEfiShellProtocol->GetCurDir (DeviceName));\r
1e6e84c7 1411 }\r
92a5447e 1412\r
94b17fa1 1413 //\r
8bd282be 1414 // Check for EFI shell\r
94b17fa1 1415 //\r
8bd282be 1416 if (mEfiShellEnvironment2 != NULL) {\r
47d20b54 1417 return (mEfiShellEnvironment2->CurDir (DeviceName));\r
8bd282be 1418 }\r
1419\r
1420 return (NULL);\r
94b17fa1 1421}\r
47d20b54 1422\r
94b17fa1 1423/**\r
1424 sets (enabled or disabled) the page break mode\r
1425\r
1e6e84c7 1426 when page break mode is enabled the screen will stop scrolling\r
94b17fa1 1427 and wait for operator input before scrolling a subsequent screen.\r
1428\r
1429 @param CurrentState TRUE to enable and FALSE to disable\r
1430**/\r
1e6e84c7 1431VOID\r
94b17fa1 1432EFIAPI\r
1433ShellSetPageBreakMode (\r
47d20b54 1434 IN BOOLEAN CurrentState\r
94b17fa1 1435 )\r
1436{\r
1437 //\r
1438 // check for enabling\r
1439 //\r
1440 if (CurrentState != 0x00) {\r
1e6e84c7 1441 //\r
94b17fa1 1442 // check for UEFI Shell 2.0\r
1443 //\r
366f81a0 1444 if (gEfiShellProtocol != NULL) {\r
94b17fa1 1445 //\r
1446 // Enable with UEFI 2.0 Shell\r
1447 //\r
47d20b54 1448 gEfiShellProtocol->EnablePageBreak ();\r
94b17fa1 1449 return;\r
1450 } else {\r
1e6e84c7 1451 //\r
92a5447e 1452 // Check for EFI shell\r
94b17fa1 1453 //\r
92a5447e 1454 if (mEfiShellEnvironment2 != NULL) {\r
1455 //\r
1456 // Enable with EFI Shell\r
1457 //\r
1458 mEfiShellEnvironment2->EnablePageBreak (DEFAULT_INIT_ROW, DEFAULT_AUTO_LF);\r
1459 return;\r
1460 }\r
94b17fa1 1461 }\r
1462 } else {\r
1e6e84c7 1463 //\r
94b17fa1 1464 // check for UEFI Shell 2.0\r
1465 //\r
366f81a0 1466 if (gEfiShellProtocol != NULL) {\r
94b17fa1 1467 //\r
1468 // Disable with UEFI 2.0 Shell\r
1469 //\r
47d20b54 1470 gEfiShellProtocol->DisablePageBreak ();\r
94b17fa1 1471 return;\r
1472 } else {\r
1e6e84c7 1473 //\r
92a5447e 1474 // Check for EFI shell\r
94b17fa1 1475 //\r
92a5447e 1476 if (mEfiShellEnvironment2 != NULL) {\r
1477 //\r
1478 // Disable with EFI Shell\r
1479 //\r
1480 mEfiShellEnvironment2->DisablePageBreak ();\r
1481 return;\r
1482 }\r
94b17fa1 1483 }\r
1484 }\r
1485}\r
1486\r
1487///\r
1488/// version of EFI_SHELL_FILE_INFO struct, except has no CONST pointers.\r
1489/// This allows for the struct to be populated.\r
1490///\r
1491typedef struct {\r
47d20b54
MK
1492 LIST_ENTRY Link;\r
1493 EFI_STATUS Status;\r
1494 CHAR16 *FullName;\r
1495 CHAR16 *FileName;\r
1496 SHELL_FILE_HANDLE Handle;\r
1497 EFI_FILE_INFO *Info;\r
94b17fa1 1498} EFI_SHELL_FILE_INFO_NO_CONST;\r
1499\r
1500/**\r
1501 Converts a EFI shell list of structures to the coresponding UEFI Shell 2.0 type of list.\r
1502\r
1503 if OldStyleFileList is NULL then ASSERT()\r
1504\r
1e6e84c7 1505 this function will convert a SHELL_FILE_ARG based list into a callee allocated\r
94b17fa1 1506 EFI_SHELL_FILE_INFO based list. it is up to the caller to free the memory via\r
1507 the ShellCloseFileMetaArg function.\r
1508\r
4ff7e37b
ED
1509 @param[in] FileList the EFI shell list type\r
1510 @param[in, out] ListHead the list to add to\r
94b17fa1 1511\r
1512 @retval the resultant head of the double linked new format list;\r
1513**/\r
47d20b54 1514LIST_ENTRY *\r
94b17fa1 1515InternalShellConvertFileListType (\r
47d20b54
MK
1516 IN LIST_ENTRY *FileList,\r
1517 IN OUT LIST_ENTRY *ListHead\r
125c2cf4 1518 )\r
1519{\r
94b17fa1 1520 SHELL_FILE_ARG *OldInfo;\r
9b3bf083 1521 LIST_ENTRY *Link;\r
94b17fa1 1522 EFI_SHELL_FILE_INFO_NO_CONST *NewInfo;\r
1523\r
1524 //\r
9b3bf083 1525 // ASSERTs\r
94b17fa1 1526 //\r
47d20b54
MK
1527 ASSERT (FileList != NULL);\r
1528 ASSERT (ListHead != NULL);\r
94b17fa1 1529\r
1530 //\r
1531 // enumerate through each member of the old list and copy\r
1532 //\r
d2b4564b 1533 for (Link = FileList->ForwardLink; Link != FileList; Link = Link->ForwardLink) {\r
94b17fa1 1534 OldInfo = CR (Link, SHELL_FILE_ARG, Link, SHELL_FILE_ARG_SIGNATURE);\r
47d20b54 1535 ASSERT (OldInfo != NULL);\r
a405b86d 1536\r
1537 //\r
1538 // Skip ones that failed to open...\r
1539 //\r
1540 if (OldInfo->Status != EFI_SUCCESS) {\r
1541 continue;\r
1542 }\r
94b17fa1 1543\r
1544 //\r
1545 // make sure the old list was valid\r
1546 //\r
47d20b54
MK
1547 ASSERT (OldInfo->Info != NULL);\r
1548 ASSERT (OldInfo->FullName != NULL);\r
1549 ASSERT (OldInfo->FileName != NULL);\r
94b17fa1 1550\r
1551 //\r
1552 // allocate a new EFI_SHELL_FILE_INFO object\r
1553 //\r
47d20b54 1554 NewInfo = AllocateZeroPool (sizeof (EFI_SHELL_FILE_INFO));\r
c9d92df0 1555 if (NewInfo == NULL) {\r
47d20b54 1556 ShellCloseFileMetaArg ((EFI_SHELL_FILE_INFO **)(&ListHead));\r
beab0fc5 1557 ListHead = NULL;\r
c9d92df0 1558 break;\r
1559 }\r
1e6e84c7 1560\r
1561 //\r
94b17fa1 1562 // copy the simple items\r
1563 //\r
47d20b54
MK
1564 NewInfo->Handle = OldInfo->Handle;\r
1565 NewInfo->Status = OldInfo->Status;\r
94b17fa1 1566\r
d2b4564b 1567 // old shell checks for 0 not NULL\r
1568 OldInfo->Handle = 0;\r
1569\r
94b17fa1 1570 //\r
1571 // allocate new space to copy strings and structure\r
1572 //\r
47d20b54
MK
1573 NewInfo->FullName = AllocateCopyPool (StrSize (OldInfo->FullName), OldInfo->FullName);\r
1574 NewInfo->FileName = AllocateCopyPool (StrSize (OldInfo->FileName), OldInfo->FileName);\r
1575 NewInfo->Info = AllocateCopyPool ((UINTN)OldInfo->Info->Size, OldInfo->Info);\r
1e6e84c7 1576\r
94b17fa1 1577 //\r
1578 // make sure all the memory allocations were sucessful\r
1579 //\r
47d20b54 1580 if ((NULL == NewInfo->FullName) || (NewInfo->FileName == NULL) || (NewInfo->Info == NULL)) {\r
98c16be5
JC
1581 //\r
1582 // Free the partially allocated new node\r
1583 //\r
47d20b54
MK
1584 SHELL_FREE_NON_NULL (NewInfo->FullName);\r
1585 SHELL_FREE_NON_NULL (NewInfo->FileName);\r
1586 SHELL_FREE_NON_NULL (NewInfo->Info);\r
1587 SHELL_FREE_NON_NULL (NewInfo);\r
98c16be5
JC
1588\r
1589 //\r
1590 // Free the previously converted stuff\r
1591 //\r
47d20b54 1592 ShellCloseFileMetaArg ((EFI_SHELL_FILE_INFO **)(&ListHead));\r
beab0fc5 1593 ListHead = NULL;\r
1594 break;\r
1595 }\r
94b17fa1 1596\r
94b17fa1 1597 //\r
1598 // add that to the list\r
1599 //\r
47d20b54 1600 InsertTailList (ListHead, &NewInfo->Link);\r
94b17fa1 1601 }\r
47d20b54 1602\r
94b17fa1 1603 return (ListHead);\r
1604}\r
47d20b54 1605\r
94b17fa1 1606/**\r
1607 Opens a group of files based on a path.\r
1608\r
1e6e84c7 1609 This function uses the Arg to open all the matching files. Each matched\r
70879314
BJ
1610 file has a SHELL_FILE_INFO structure to record the file information. These\r
1611 structures are placed on the list ListHead. Users can get the SHELL_FILE_INFO\r
94b17fa1 1612 structures from ListHead to access each file. This function supports wildcards\r
1e6e84c7 1613 and will process '?' and '*' as such. the list must be freed with a call to\r
94b17fa1 1614 ShellCloseFileMetaArg().\r
1615\r
1e6e84c7 1616 If you are NOT appending to an existing list *ListHead must be NULL. If\r
5f7431d0 1617 *ListHead is NULL then it must be callee freed.\r
94b17fa1 1618\r
1619 @param Arg pointer to path string\r
1620 @param OpenMode mode to open files with\r
1621 @param ListHead head of linked list of results\r
1622\r
1e6e84c7 1623 @retval EFI_SUCCESS the operation was sucessful and the list head\r
94b17fa1 1624 contains the list of opened files\r
94b17fa1 1625 @return != EFI_SUCCESS the operation failed\r
1626\r
1627 @sa InternalShellConvertFileListType\r
1628**/\r
1629EFI_STATUS\r
1630EFIAPI\r
1631ShellOpenFileMetaArg (\r
47d20b54
MK
1632 IN CHAR16 *Arg,\r
1633 IN UINT64 OpenMode,\r
1634 IN OUT EFI_SHELL_FILE_INFO **ListHead\r
94b17fa1 1635 )\r
1636{\r
47d20b54
MK
1637 EFI_STATUS Status;\r
1638 LIST_ENTRY mOldStyleFileList;\r
1639 CHAR16 *CleanFilePathStr;\r
1e6e84c7 1640\r
94b17fa1 1641 //\r
1642 // ASSERT that Arg and ListHead are not NULL\r
1643 //\r
47d20b54
MK
1644 ASSERT (Arg != NULL);\r
1645 ASSERT (ListHead != NULL);\r
94b17fa1 1646\r
715096c2
QS
1647 CleanFilePathStr = NULL;\r
1648\r
0960ba17
QS
1649 Status = InternalShellStripQuotes (Arg, &CleanFilePathStr);\r
1650 if (EFI_ERROR (Status)) {\r
1651 return Status;\r
1652 }\r
1653\r
1e6e84c7 1654 //\r
94b17fa1 1655 // Check for UEFI Shell 2.0 protocols\r
1656 //\r
366f81a0 1657 if (gEfiShellProtocol != NULL) {\r
5f7431d0 1658 if (*ListHead == NULL) {\r
47d20b54 1659 *ListHead = (EFI_SHELL_FILE_INFO *)AllocateZeroPool (sizeof (EFI_SHELL_FILE_INFO));\r
5f7431d0 1660 if (*ListHead == NULL) {\r
47d20b54 1661 FreePool (CleanFilePathStr);\r
5f7431d0 1662 return (EFI_OUT_OF_RESOURCES);\r
1663 }\r
47d20b54
MK
1664\r
1665 InitializeListHead (&((*ListHead)->Link));\r
1e6e84c7 1666 }\r
47d20b54
MK
1667\r
1668 Status = gEfiShellProtocol->OpenFileList (\r
1669 CleanFilePathStr,\r
1670 OpenMode,\r
1671 ListHead\r
1672 );\r
1673 if (EFI_ERROR (Status)) {\r
1674 gEfiShellProtocol->RemoveDupInFileList (ListHead);\r
2247dde4 1675 } else {\r
47d20b54 1676 Status = gEfiShellProtocol->RemoveDupInFileList (ListHead);\r
2247dde4 1677 }\r
47d20b54
MK
1678\r
1679 if ((*ListHead != NULL) && IsListEmpty (&(*ListHead)->Link)) {\r
1680 FreePool (*ListHead);\r
1681 FreePool (CleanFilePathStr);\r
a405b86d 1682 *ListHead = NULL;\r
1683 return (EFI_NOT_FOUND);\r
1684 }\r
47d20b54
MK
1685\r
1686 FreePool (CleanFilePathStr);\r
2247dde4 1687 return (Status);\r
1e6e84c7 1688 }\r
94b17fa1 1689\r
1690 //\r
92a5447e 1691 // Check for EFI shell\r
94b17fa1 1692 //\r
92a5447e 1693 if (mEfiShellEnvironment2 != NULL) {\r
1694 //\r
1695 // make sure the list head is initialized\r
1696 //\r
47d20b54 1697 InitializeListHead (&mOldStyleFileList);\r
94b17fa1 1698\r
92a5447e 1699 //\r
1700 // Get the EFI Shell list of files\r
1701 //\r
47d20b54
MK
1702 Status = mEfiShellEnvironment2->FileMetaArg (CleanFilePathStr, &mOldStyleFileList);\r
1703 if (EFI_ERROR (Status)) {\r
92a5447e 1704 *ListHead = NULL;\r
47d20b54 1705 FreePool (CleanFilePathStr);\r
92a5447e 1706 return (Status);\r
1707 }\r
94b17fa1 1708\r
9b3bf083 1709 if (*ListHead == NULL) {\r
47d20b54 1710 *ListHead = (EFI_SHELL_FILE_INFO *)AllocateZeroPool (sizeof (EFI_SHELL_FILE_INFO));\r
92a5447e 1711 if (*ListHead == NULL) {\r
47d20b54 1712 FreePool (CleanFilePathStr);\r
92a5447e 1713 return (EFI_OUT_OF_RESOURCES);\r
1714 }\r
47d20b54
MK
1715\r
1716 InitializeListHead (&((*ListHead)->Link));\r
9b3bf083 1717 }\r
9b3bf083 1718\r
92a5447e 1719 //\r
1720 // Convert that to equivalent of UEFI Shell 2.0 structure\r
1721 //\r
47d20b54 1722 InternalShellConvertFileListType (&mOldStyleFileList, &(*ListHead)->Link);\r
94b17fa1 1723\r
92a5447e 1724 //\r
1725 // Free the EFI Shell version that was converted.\r
1726 //\r
47d20b54 1727 mEfiShellEnvironment2->FreeFileList (&mOldStyleFileList);\r
94b17fa1 1728\r
47d20b54
MK
1729 if (((*ListHead)->Link.ForwardLink == (*ListHead)->Link.BackLink) && ((*ListHead)->Link.BackLink == &((*ListHead)->Link))) {\r
1730 FreePool (*ListHead);\r
92a5447e 1731 *ListHead = NULL;\r
47d20b54 1732 Status = EFI_NOT_FOUND;\r
92a5447e 1733 }\r
47d20b54
MK
1734\r
1735 FreePool (CleanFilePathStr);\r
92a5447e 1736 return (Status);\r
a405b86d 1737 }\r
1738\r
47d20b54 1739 FreePool (CleanFilePathStr);\r
92a5447e 1740 return (EFI_UNSUPPORTED);\r
94b17fa1 1741}\r
47d20b54 1742\r
94b17fa1 1743/**\r
a405b86d 1744 Free the linked list returned from ShellOpenFileMetaArg.\r
94b17fa1 1745\r
a405b86d 1746 if ListHead is NULL then ASSERT().\r
94b17fa1 1747\r
a405b86d 1748 @param ListHead the pointer to free.\r
94b17fa1 1749\r
a405b86d 1750 @retval EFI_SUCCESS the operation was sucessful.\r
94b17fa1 1751**/\r
1752EFI_STATUS\r
1753EFIAPI\r
1754ShellCloseFileMetaArg (\r
47d20b54 1755 IN OUT EFI_SHELL_FILE_INFO **ListHead\r
94b17fa1 1756 )\r
1757{\r
47d20b54 1758 LIST_ENTRY *Node;\r
94b17fa1 1759\r
1760 //\r
1761 // ASSERT that ListHead is not NULL\r
1762 //\r
47d20b54 1763 ASSERT (ListHead != NULL);\r
94b17fa1 1764\r
1e6e84c7 1765 //\r
94b17fa1 1766 // Check for UEFI Shell 2.0 protocols\r
1767 //\r
366f81a0 1768 if (gEfiShellProtocol != NULL) {\r
47d20b54 1769 return (gEfiShellProtocol->FreeFileList (ListHead));\r
92a5447e 1770 } else if (mEfiShellEnvironment2 != NULL) {\r
94b17fa1 1771 //\r
1e6e84c7 1772 // Since this is EFI Shell version we need to free our internally made copy\r
94b17fa1 1773 // of the list\r
1774 //\r
47d20b54
MK
1775 for ( Node = GetFirstNode (&(*ListHead)->Link)\r
1776 ; *ListHead != NULL && !IsListEmpty (&(*ListHead)->Link)\r
1777 ; Node = GetFirstNode (&(*ListHead)->Link))\r
1778 {\r
1779 RemoveEntryList (Node);\r
1780 ((EFI_FILE_PROTOCOL *)((EFI_SHELL_FILE_INFO_NO_CONST *)Node)->Handle)->Close (((EFI_SHELL_FILE_INFO_NO_CONST *)Node)->Handle);\r
1781 FreePool (((EFI_SHELL_FILE_INFO_NO_CONST *)Node)->FullName);\r
1782 FreePool (((EFI_SHELL_FILE_INFO_NO_CONST *)Node)->FileName);\r
1783 FreePool (((EFI_SHELL_FILE_INFO_NO_CONST *)Node)->Info);\r
1784 FreePool ((EFI_SHELL_FILE_INFO_NO_CONST *)Node);\r
94b17fa1 1785 }\r
47d20b54
MK
1786\r
1787 SHELL_FREE_NON_NULL (*ListHead);\r
94b17fa1 1788 return EFI_SUCCESS;\r
1789 }\r
92a5447e 1790\r
1791 return (EFI_UNSUPPORTED);\r
94b17fa1 1792}\r
1793\r
125c2cf4 1794/**\r
1795 Find a file by searching the CWD and then the path.\r
1796\r
b3011f40 1797 If FileName is NULL then ASSERT.\r
125c2cf4 1798\r
b3011f40 1799 If the return value is not NULL then the memory must be caller freed.\r
125c2cf4 1800\r
1801 @param FileName Filename string.\r
1802\r
1803 @retval NULL the file was not found\r
1804 @return !NULL the full path to the file.\r
1805**/\r
1806CHAR16 *\r
1807EFIAPI\r
1808ShellFindFilePath (\r
47d20b54 1809 IN CONST CHAR16 *FileName\r
125c2cf4 1810 )\r
1811{\r
47d20b54
MK
1812 CONST CHAR16 *Path;\r
1813 SHELL_FILE_HANDLE Handle;\r
1814 EFI_STATUS Status;\r
1815 CHAR16 *RetVal;\r
1816 CHAR16 *TestPath;\r
1817 CONST CHAR16 *Walker;\r
1818 UINTN Size;\r
1819 CHAR16 *TempChar;\r
125c2cf4 1820\r
1821 RetVal = NULL;\r
1822\r
a405b86d 1823 //\r
1824 // First make sure its not an absolute path.\r
1825 //\r
47d20b54
MK
1826 Status = ShellOpenFileByName (FileName, &Handle, EFI_FILE_MODE_READ, 0);\r
1827 if (!EFI_ERROR (Status)) {\r
1828 if (FileHandleIsDirectory (Handle) != EFI_SUCCESS) {\r
1829 ASSERT (RetVal == NULL);\r
1830 RetVal = StrnCatGrow (&RetVal, NULL, FileName, 0);\r
1831 ShellCloseFile (&Handle);\r
a405b86d 1832 return (RetVal);\r
1833 } else {\r
47d20b54 1834 ShellCloseFile (&Handle);\r
a405b86d 1835 }\r
1836 }\r
1837\r
47d20b54 1838 Path = ShellGetEnvironmentVariable (L"cwd");\r
125c2cf4 1839 if (Path != NULL) {\r
47d20b54
MK
1840 Size = StrSize (Path) + sizeof (CHAR16);\r
1841 Size += StrSize (FileName);\r
1842 TestPath = AllocateZeroPool (Size);\r
c9d92df0 1843 if (TestPath == NULL) {\r
1844 return (NULL);\r
1845 }\r
47d20b54
MK
1846\r
1847 StrCpyS (TestPath, Size/sizeof (CHAR16), Path);\r
1848 StrCatS (TestPath, Size/sizeof (CHAR16), L"\\");\r
1849 StrCatS (TestPath, Size/sizeof (CHAR16), FileName);\r
1850 Status = ShellOpenFileByName (TestPath, &Handle, EFI_FILE_MODE_READ, 0);\r
1851 if (!EFI_ERROR (Status)) {\r
1852 if (FileHandleIsDirectory (Handle) != EFI_SUCCESS) {\r
1853 ASSERT (RetVal == NULL);\r
1854 RetVal = StrnCatGrow (&RetVal, NULL, TestPath, 0);\r
1855 ShellCloseFile (&Handle);\r
1856 FreePool (TestPath);\r
a405b86d 1857 return (RetVal);\r
1858 } else {\r
47d20b54 1859 ShellCloseFile (&Handle);\r
a405b86d 1860 }\r
125c2cf4 1861 }\r
47d20b54
MK
1862\r
1863 FreePool (TestPath);\r
125c2cf4 1864 }\r
47d20b54
MK
1865\r
1866 Path = ShellGetEnvironmentVariable (L"path");\r
125c2cf4 1867 if (Path != NULL) {\r
47d20b54
MK
1868 Size = StrSize (Path)+sizeof (CHAR16);\r
1869 Size += StrSize (FileName);\r
1870 TestPath = AllocateZeroPool (Size);\r
3e082d58 1871 if (TestPath == NULL) {\r
1872 return (NULL);\r
1873 }\r
47d20b54
MK
1874\r
1875 Walker = (CHAR16 *)Path;\r
125c2cf4 1876 do {\r
47d20b54 1877 CopyMem (TestPath, Walker, StrSize (Walker));\r
3e082d58 1878 if (TestPath != NULL) {\r
47d20b54 1879 TempChar = StrStr (TestPath, L";");\r
3e082d58 1880 if (TempChar != NULL) {\r
1881 *TempChar = CHAR_NULL;\r
1882 }\r
47d20b54
MK
1883\r
1884 if (TestPath[StrLen (TestPath)-1] != L'\\') {\r
1885 StrCatS (TestPath, Size/sizeof (CHAR16), L"\\");\r
3e082d58 1886 }\r
47d20b54 1887\r
89e8537a 1888 if (FileName[0] == L'\\') {\r
1889 FileName++;\r
1890 }\r
47d20b54
MK
1891\r
1892 StrCatS (TestPath, Size/sizeof (CHAR16), FileName);\r
1893 if (StrStr (Walker, L";") != NULL) {\r
1894 Walker = StrStr (Walker, L";") + 1;\r
a405b86d 1895 } else {\r
3e082d58 1896 Walker = NULL;\r
1897 }\r
47d20b54
MK
1898\r
1899 Status = ShellOpenFileByName (TestPath, &Handle, EFI_FILE_MODE_READ, 0);\r
1900 if (!EFI_ERROR (Status)) {\r
1901 if (FileHandleIsDirectory (Handle) != EFI_SUCCESS) {\r
1902 ASSERT (RetVal == NULL);\r
1903 RetVal = StrnCatGrow (&RetVal, NULL, TestPath, 0);\r
1904 ShellCloseFile (&Handle);\r
3e082d58 1905 break;\r
1906 } else {\r
47d20b54 1907 ShellCloseFile (&Handle);\r
3e082d58 1908 }\r
a405b86d 1909 }\r
125c2cf4 1910 }\r
1911 } while (Walker != NULL && Walker[0] != CHAR_NULL);\r
47d20b54
MK
1912\r
1913 FreePool (TestPath);\r
125c2cf4 1914 }\r
47d20b54 1915\r
125c2cf4 1916 return (RetVal);\r
1917}\r
1918\r
b3011f40 1919/**\r
1e6e84c7 1920 Find a file by searching the CWD and then the path with a variable set of file\r
1921 extensions. If the file is not found it will append each extension in the list\r
b3011f40 1922 in the order provided and return the first one that is successful.\r
1923\r
1924 If FileName is NULL, then ASSERT.\r
1925 If FileExtension is NULL, then behavior is identical to ShellFindFilePath.\r
1926\r
1927 If the return value is not NULL then the memory must be caller freed.\r
1928\r
1929 @param[in] FileName Filename string.\r
1930 @param[in] FileExtension Semi-colon delimeted list of possible extensions.\r
1931\r
1932 @retval NULL The file was not found.\r
1933 @retval !NULL The path to the file.\r
1934**/\r
1935CHAR16 *\r
1936EFIAPI\r
1937ShellFindFilePathEx (\r
47d20b54
MK
1938 IN CONST CHAR16 *FileName,\r
1939 IN CONST CHAR16 *FileExtension\r
b3011f40 1940 )\r
1941{\r
47d20b54
MK
1942 CHAR16 *TestPath;\r
1943 CHAR16 *RetVal;\r
1944 CONST CHAR16 *ExtensionWalker;\r
1945 UINTN Size;\r
1946 CHAR16 *TempChar;\r
1947 CHAR16 *TempChar2;\r
1948\r
1949 ASSERT (FileName != NULL);\r
b3011f40 1950 if (FileExtension == NULL) {\r
47d20b54 1951 return (ShellFindFilePath (FileName));\r
b3011f40 1952 }\r
47d20b54
MK
1953\r
1954 RetVal = ShellFindFilePath (FileName);\r
b3011f40 1955 if (RetVal != NULL) {\r
1956 return (RetVal);\r
1957 }\r
47d20b54
MK
1958\r
1959 Size = StrSize (FileName);\r
1960 Size += StrSize (FileExtension);\r
1961 TestPath = AllocateZeroPool (Size);\r
c9d92df0 1962 if (TestPath == NULL) {\r
1963 return (NULL);\r
1964 }\r
47d20b54
MK
1965\r
1966 for (ExtensionWalker = FileExtension, TempChar2 = (CHAR16 *)FileExtension; TempChar2 != NULL; ExtensionWalker = TempChar2 + 1) {\r
1967 StrCpyS (TestPath, Size/sizeof (CHAR16), FileName);\r
a405b86d 1968 if (ExtensionWalker != NULL) {\r
47d20b54 1969 StrCatS (TestPath, Size/sizeof (CHAR16), ExtensionWalker);\r
a405b86d 1970 }\r
47d20b54
MK
1971\r
1972 TempChar = StrStr (TestPath, L";");\r
1cd45e78 1973 if (TempChar != NULL) {\r
1974 *TempChar = CHAR_NULL;\r
b3011f40 1975 }\r
47d20b54
MK
1976\r
1977 RetVal = ShellFindFilePath (TestPath);\r
b3011f40 1978 if (RetVal != NULL) {\r
1979 break;\r
1980 }\r
47d20b54
MK
1981\r
1982 ASSERT (ExtensionWalker != NULL);\r
1983 TempChar2 = StrStr (ExtensionWalker, L";");\r
b3011f40 1984 }\r
47d20b54
MK
1985\r
1986 FreePool (TestPath);\r
b3011f40 1987 return (RetVal);\r
1988}\r
1989\r
94b17fa1 1990typedef struct {\r
47d20b54
MK
1991 LIST_ENTRY Link;\r
1992 CHAR16 *Name;\r
1993 SHELL_PARAM_TYPE Type;\r
1994 CHAR16 *Value;\r
1995 UINTN OriginalPosition;\r
94b17fa1 1996} SHELL_PARAM_PACKAGE;\r
1997\r
1998/**\r
1e6e84c7 1999 Checks the list of valid arguments and returns TRUE if the item was found. If the\r
94b17fa1 2000 return value is TRUE then the type parameter is set also.\r
1e6e84c7 2001\r
94b17fa1 2002 if CheckList is NULL then ASSERT();\r
2003 if Name is NULL then ASSERT();\r
2004 if Type is NULL then ASSERT();\r
2005\r
94b17fa1 2006 @param Name pointer to Name of parameter found\r
2007 @param CheckList List to check against\r
a405b86d 2008 @param Type pointer to type of parameter if it was found\r
94b17fa1 2009\r
2010 @retval TRUE the Parameter was found. Type is valid.\r
2011 @retval FALSE the Parameter was not found. Type is not valid.\r
2012**/\r
2013BOOLEAN\r
d2b4564b 2014InternalIsOnCheckList (\r
47d20b54
MK
2015 IN CONST CHAR16 *Name,\r
2016 IN CONST SHELL_PARAM_ITEM *CheckList,\r
2017 OUT SHELL_PARAM_TYPE *Type\r
a405b86d 2018 )\r
2019{\r
47d20b54
MK
2020 SHELL_PARAM_ITEM *TempListItem;\r
2021 CHAR16 *TempString;\r
94b17fa1 2022\r
2023 //\r
2024 // ASSERT that all 3 pointer parameters aren't NULL\r
2025 //\r
47d20b54
MK
2026 ASSERT (CheckList != NULL);\r
2027 ASSERT (Type != NULL);\r
2028 ASSERT (Name != NULL);\r
94b17fa1 2029\r
d2b4564b 2030 //\r
2031 // question mark and page break mode are always supported\r
2032 //\r
47d20b54
MK
2033 if ((StrCmp (Name, L"-?") == 0) ||\r
2034 (StrCmp (Name, L"-b") == 0)\r
2035 )\r
2036 {\r
2037 *Type = TypeFlag;\r
2038 return (TRUE);\r
d2b4564b 2039 }\r
2040\r
94b17fa1 2041 //\r
2042 // Enumerate through the list\r
2043 //\r
47d20b54 2044 for (TempListItem = (SHELL_PARAM_ITEM *)CheckList; TempListItem->Name != NULL; TempListItem++) {\r
94b17fa1 2045 //\r
9eb53ac3 2046 // If the Type is TypeStart only check the first characters of the passed in param\r
2047 // If it matches set the type and return TRUE\r
94b17fa1 2048 //\r
b0934ac4 2049 if (TempListItem->Type == TypeStart) {\r
47d20b54 2050 if (StrnCmp (Name, TempListItem->Name, StrLen (TempListItem->Name)) == 0) {\r
252d9457 2051 *Type = TempListItem->Type;\r
2052 return (TRUE);\r
2053 }\r
47d20b54 2054\r
252d9457 2055 TempString = NULL;\r
47d20b54 2056 TempString = StrnCatGrow (&TempString, NULL, Name, StrLen (TempListItem->Name));\r
252d9457 2057 if (TempString != NULL) {\r
47d20b54 2058 if (StringNoCaseCompare (&TempString, &TempListItem->Name) == 0) {\r
252d9457 2059 *Type = TempListItem->Type;\r
47d20b54 2060 FreePool (TempString);\r
252d9457 2061 return (TRUE);\r
2062 }\r
47d20b54
MK
2063\r
2064 FreePool (TempString);\r
252d9457 2065 }\r
47d20b54 2066 } else if (StringNoCaseCompare (&Name, &TempListItem->Name) == 0) {\r
94b17fa1 2067 *Type = TempListItem->Type;\r
2068 return (TRUE);\r
2069 }\r
2070 }\r
2247dde4 2071\r
94b17fa1 2072 return (FALSE);\r
2073}\r
47d20b54 2074\r
94b17fa1 2075/**\r
d2b4564b 2076 Checks the string for indicators of "flag" status. this is a leading '/', '-', or '+'\r
94b17fa1 2077\r
a405b86d 2078 @param[in] Name pointer to Name of parameter found\r
2079 @param[in] AlwaysAllowNumbers TRUE to allow numbers, FALSE to not.\r
658bf43e 2080 @param[in] TimeNumbers TRUE to allow numbers with ":", FALSE otherwise.\r
94b17fa1 2081\r
2082 @retval TRUE the Parameter is a flag.\r
a405b86d 2083 @retval FALSE the Parameter not a flag.\r
94b17fa1 2084**/\r
2085BOOLEAN\r
d2b4564b 2086InternalIsFlag (\r
47d20b54
MK
2087 IN CONST CHAR16 *Name,\r
2088 IN CONST BOOLEAN AlwaysAllowNumbers,\r
2089 IN CONST BOOLEAN TimeNumbers\r
94b17fa1 2090 )\r
2091{\r
2092 //\r
2093 // ASSERT that Name isn't NULL\r
2094 //\r
47d20b54 2095 ASSERT (Name != NULL);\r
94b17fa1 2096\r
2247dde4 2097 //\r
2098 // If we accept numbers then dont return TRUE. (they will be values)\r
2099 //\r
47d20b54 2100 if ((((Name[0] == L'-') || (Name[0] == L'+')) && InternalShellIsHexOrDecimalNumber (Name+1, FALSE, FALSE, TimeNumbers)) && AlwaysAllowNumbers) {\r
2247dde4 2101 return (FALSE);\r
2102 }\r
2103\r
94b17fa1 2104 //\r
a405b86d 2105 // If the Name has a /, +, or - as the first character return TRUE\r
94b17fa1 2106 //\r
1e6e84c7 2107 if ((Name[0] == L'/') ||\r
d2b4564b 2108 (Name[0] == L'-') ||\r
2109 (Name[0] == L'+')\r
47d20b54
MK
2110 )\r
2111 {\r
2112 return (TRUE);\r
94b17fa1 2113 }\r
47d20b54 2114\r
94b17fa1 2115 return (FALSE);\r
2116}\r
2117\r
2118/**\r
1e6e84c7 2119 Checks the command line arguments passed against the list of valid ones.\r
94b17fa1 2120\r
2121 If no initialization is required, then return RETURN_SUCCESS.\r
1e6e84c7 2122\r
a405b86d 2123 @param[in] CheckList pointer to list of parameters to check\r
2124 @param[out] CheckPackage pointer to pointer to list checked values\r
2125 @param[out] ProblemParam optional pointer to pointer to unicode string for\r
d2b4564b 2126 the paramater that caused failure. If used then the\r
2127 caller is responsible for freeing the memory.\r
a405b86d 2128 @param[in] AutoPageBreak will automatically set PageBreakEnabled for "b" parameter\r
2129 @param[in] Argv pointer to array of parameters\r
2130 @param[in] Argc Count of parameters in Argv\r
2131 @param[in] AlwaysAllowNumbers TRUE to allow numbers always, FALSE otherwise.\r
94b17fa1 2132\r
2133 @retval EFI_SUCCESS The operation completed sucessfully.\r
2134 @retval EFI_OUT_OF_RESOURCES A memory allocation failed\r
2135 @retval EFI_INVALID_PARAMETER A parameter was invalid\r
1e6e84c7 2136 @retval EFI_VOLUME_CORRUPTED the command line was corrupt. an argument was\r
2137 duplicated. the duplicated command line argument\r
94b17fa1 2138 was returned in ProblemParam if provided.\r
1e6e84c7 2139 @retval EFI_NOT_FOUND a argument required a value that was missing.\r
94b17fa1 2140 the invalid command line argument was returned in\r
2141 ProblemParam if provided.\r
2142**/\r
2143EFI_STATUS\r
94b17fa1 2144InternalCommandLineParse (\r
47d20b54
MK
2145 IN CONST SHELL_PARAM_ITEM *CheckList,\r
2146 OUT LIST_ENTRY **CheckPackage,\r
2147 OUT CHAR16 **ProblemParam OPTIONAL,\r
2148 IN BOOLEAN AutoPageBreak,\r
2149 IN CONST CHAR16 **Argv,\r
2150 IN UINTN Argc,\r
2151 IN BOOLEAN AlwaysAllowNumbers\r
a405b86d 2152 )\r
2153{\r
47d20b54
MK
2154 UINTN LoopCounter;\r
2155 SHELL_PARAM_TYPE CurrentItemType;\r
2156 SHELL_PARAM_PACKAGE *CurrentItemPackage;\r
2157 UINTN GetItemValue;\r
2158 UINTN ValueSize;\r
2159 UINTN Count;\r
2160 CONST CHAR16 *TempPointer;\r
2161 UINTN CurrentValueSize;\r
2162 CHAR16 *NewValue;\r
94b17fa1 2163\r
2164 CurrentItemPackage = NULL;\r
47d20b54
MK
2165 GetItemValue = 0;\r
2166 ValueSize = 0;\r
2167 Count = 0;\r
94b17fa1 2168\r
2169 //\r
2170 // If there is only 1 item we dont need to do anything\r
2171 //\r
a405b86d 2172 if (Argc < 1) {\r
94b17fa1 2173 *CheckPackage = NULL;\r
2174 return (EFI_SUCCESS);\r
2175 }\r
2176\r
2247dde4 2177 //\r
2178 // ASSERTs\r
2179 //\r
47d20b54
MK
2180 ASSERT (CheckList != NULL);\r
2181 ASSERT (Argv != NULL);\r
2247dde4 2182\r
94b17fa1 2183 //\r
2184 // initialize the linked list\r
2185 //\r
47d20b54 2186 *CheckPackage = (LIST_ENTRY *)AllocateZeroPool (sizeof (LIST_ENTRY));\r
02a758cb 2187 if (*CheckPackage == NULL) {\r
beab0fc5 2188 return (EFI_OUT_OF_RESOURCES);\r
02a758cb 2189 }\r
beab0fc5 2190\r
47d20b54 2191 InitializeListHead (*CheckPackage);\r
94b17fa1 2192\r
2193 //\r
2194 // loop through each of the arguments\r
2195 //\r
47d20b54 2196 for (LoopCounter = 0; LoopCounter < Argc; ++LoopCounter) {\r
94b17fa1 2197 if (Argv[LoopCounter] == NULL) {\r
2198 //\r
2199 // do nothing for NULL argv\r
2200 //\r
47d20b54 2201 } else if (InternalIsOnCheckList (Argv[LoopCounter], CheckList, &CurrentItemType)) {\r
2247dde4 2202 //\r
2203 // We might have leftover if last parameter didnt have optional value\r
2204 //\r
125c2cf4 2205 if (GetItemValue != 0) {\r
2206 GetItemValue = 0;\r
47d20b54 2207 InsertHeadList (*CheckPackage, &CurrentItemPackage->Link);\r
2247dde4 2208 }\r
47d20b54 2209\r
94b17fa1 2210 //\r
2211 // this is a flag\r
2212 //\r
47d20b54 2213 CurrentItemPackage = AllocateZeroPool (sizeof (SHELL_PARAM_PACKAGE));\r
beab0fc5 2214 if (CurrentItemPackage == NULL) {\r
47d20b54 2215 ShellCommandLineFreeVarList (*CheckPackage);\r
beab0fc5 2216 *CheckPackage = NULL;\r
2217 return (EFI_OUT_OF_RESOURCES);\r
2218 }\r
47d20b54
MK
2219\r
2220 CurrentItemPackage->Name = AllocateCopyPool (StrSize (Argv[LoopCounter]), Argv[LoopCounter]);\r
beab0fc5 2221 if (CurrentItemPackage->Name == NULL) {\r
47d20b54 2222 ShellCommandLineFreeVarList (*CheckPackage);\r
beab0fc5 2223 *CheckPackage = NULL;\r
2224 return (EFI_OUT_OF_RESOURCES);\r
2225 }\r
47d20b54
MK
2226\r
2227 CurrentItemPackage->Type = CurrentItemType;\r
94b17fa1 2228 CurrentItemPackage->OriginalPosition = (UINTN)(-1);\r
47d20b54 2229 CurrentItemPackage->Value = NULL;\r
94b17fa1 2230\r
2231 //\r
2232 // Does this flag require a value\r
2233 //\r
125c2cf4 2234 switch (CurrentItemPackage->Type) {\r
94b17fa1 2235 //\r
125c2cf4 2236 // possibly trigger the next loop(s) to populate the value of this item\r
1e6e84c7 2237 //\r
125c2cf4 2238 case TypeValue:\r
658bf43e 2239 case TypeTimeValue:\r
1e6e84c7 2240 GetItemValue = 1;\r
47d20b54 2241 ValueSize = 0;\r
125c2cf4 2242 break;\r
2243 case TypeDoubleValue:\r
2244 GetItemValue = 2;\r
47d20b54 2245 ValueSize = 0;\r
125c2cf4 2246 break;\r
2247 case TypeMaxValue:\r
2248 GetItemValue = (UINTN)(-1);\r
47d20b54 2249 ValueSize = 0;\r
125c2cf4 2250 break;\r
2251 default:\r
2252 //\r
2253 // this item has no value expected; we are done\r
2254 //\r
47d20b54
MK
2255 InsertHeadList (*CheckPackage, &CurrentItemPackage->Link);\r
2256 ASSERT (GetItemValue == 0);\r
125c2cf4 2257 break;\r
94b17fa1 2258 }\r
47d20b54 2259 } else if ((GetItemValue != 0) && (CurrentItemPackage != NULL) && !InternalIsFlag (Argv[LoopCounter], AlwaysAllowNumbers, (BOOLEAN)(CurrentItemPackage->Type == TypeTimeValue))) {\r
b1f95a06 2260 //\r
125c2cf4 2261 // get the item VALUE for a previous flag\r
b1f95a06 2262 //\r
47d20b54
MK
2263 CurrentValueSize = ValueSize + StrSize (Argv[LoopCounter]) + sizeof (CHAR16);\r
2264 NewValue = ReallocatePool (ValueSize, CurrentValueSize, CurrentItemPackage->Value);\r
2efafabf
RN
2265 if (NewValue == NULL) {\r
2266 SHELL_FREE_NON_NULL (CurrentItemPackage->Value);\r
2267 SHELL_FREE_NON_NULL (CurrentItemPackage);\r
2268 ShellCommandLineFreeVarList (*CheckPackage);\r
2269 *CheckPackage = NULL;\r
2270 return EFI_OUT_OF_RESOURCES;\r
2271 }\r
47d20b54 2272\r
2efafabf 2273 CurrentItemPackage->Value = NewValue;\r
484dd08c 2274 if (ValueSize == 0) {\r
47d20b54
MK
2275 StrCpyS (\r
2276 CurrentItemPackage->Value,\r
2277 CurrentValueSize/sizeof (CHAR16),\r
2278 Argv[LoopCounter]\r
2279 );\r
125c2cf4 2280 } else {\r
47d20b54
MK
2281 StrCatS (\r
2282 CurrentItemPackage->Value,\r
2283 CurrentValueSize/sizeof (CHAR16),\r
2284 L" "\r
2285 );\r
2286 StrCatS (\r
2287 CurrentItemPackage->Value,\r
2288 CurrentValueSize/sizeof (CHAR16),\r
2289 Argv[LoopCounter]\r
2290 );\r
125c2cf4 2291 }\r
47d20b54
MK
2292\r
2293 ValueSize += StrSize (Argv[LoopCounter]) + sizeof (CHAR16);\r
ba0014b9 2294\r
125c2cf4 2295 GetItemValue--;\r
2296 if (GetItemValue == 0) {\r
47d20b54 2297 InsertHeadList (*CheckPackage, &CurrentItemPackage->Link);\r
125c2cf4 2298 }\r
47d20b54 2299 } else if (!InternalIsFlag (Argv[LoopCounter], AlwaysAllowNumbers, FALSE)) {\r
b1f95a06 2300 //\r
2301 // add this one as a non-flag\r
2302 //\r
252d9457 2303\r
2304 TempPointer = Argv[LoopCounter];\r
47d20b54
MK
2305 if ( ((*TempPointer == L'^') && (*(TempPointer+1) == L'-'))\r
2306 || ((*TempPointer == L'^') && (*(TempPointer+1) == L'/'))\r
2307 || ((*TempPointer == L'^') && (*(TempPointer+1) == L'+'))\r
2308 )\r
2309 {\r
252d9457 2310 TempPointer++;\r
2311 }\r
47d20b54
MK
2312\r
2313 CurrentItemPackage = AllocateZeroPool (sizeof (SHELL_PARAM_PACKAGE));\r
beab0fc5 2314 if (CurrentItemPackage == NULL) {\r
47d20b54 2315 ShellCommandLineFreeVarList (*CheckPackage);\r
beab0fc5 2316 *CheckPackage = NULL;\r
2317 return (EFI_OUT_OF_RESOURCES);\r
2318 }\r
47d20b54 2319\r
b1f95a06 2320 CurrentItemPackage->Name = NULL;\r
2321 CurrentItemPackage->Type = TypePosition;\r
47d20b54 2322 CurrentItemPackage->Value = AllocateCopyPool (StrSize (TempPointer), TempPointer);\r
beab0fc5 2323 if (CurrentItemPackage->Value == NULL) {\r
47d20b54 2324 ShellCommandLineFreeVarList (*CheckPackage);\r
beab0fc5 2325 *CheckPackage = NULL;\r
2326 return (EFI_OUT_OF_RESOURCES);\r
2327 }\r
47d20b54 2328\r
a405b86d 2329 CurrentItemPackage->OriginalPosition = Count++;\r
47d20b54 2330 InsertHeadList (*CheckPackage, &CurrentItemPackage->Link);\r
252d9457 2331 } else {\r
94b17fa1 2332 //\r
2333 // this was a non-recognised flag... error!\r
2334 //\r
252d9457 2335 if (ProblemParam != NULL) {\r
47d20b54 2336 *ProblemParam = AllocateCopyPool (StrSize (Argv[LoopCounter]), Argv[LoopCounter]);\r
252d9457 2337 }\r
47d20b54
MK
2338\r
2339 ShellCommandLineFreeVarList (*CheckPackage);\r
94b17fa1 2340 *CheckPackage = NULL;\r
2341 return (EFI_VOLUME_CORRUPTED);\r
94b17fa1 2342 }\r
2343 }\r
47d20b54 2344\r
125c2cf4 2345 if (GetItemValue != 0) {\r
2346 GetItemValue = 0;\r
47d20b54 2347 InsertHeadList (*CheckPackage, &CurrentItemPackage->Link);\r
125c2cf4 2348 }\r
47d20b54 2349\r
94b17fa1 2350 //\r
2351 // support for AutoPageBreak\r
2352 //\r
47d20b54
MK
2353 if (AutoPageBreak && ShellCommandLineGetFlag (*CheckPackage, L"-b")) {\r
2354 ShellSetPageBreakMode (TRUE);\r
94b17fa1 2355 }\r
47d20b54 2356\r
94b17fa1 2357 return (EFI_SUCCESS);\r
2358}\r
2359\r
2360/**\r
1e6e84c7 2361 Checks the command line arguments passed against the list of valid ones.\r
94b17fa1 2362 Optionally removes NULL values first.\r
1e6e84c7 2363\r
94b17fa1 2364 If no initialization is required, then return RETURN_SUCCESS.\r
1e6e84c7 2365\r
a405b86d 2366 @param[in] CheckList The pointer to list of parameters to check.\r
2367 @param[out] CheckPackage The package of checked values.\r
2368 @param[out] ProblemParam Optional pointer to pointer to unicode string for\r
94b17fa1 2369 the paramater that caused failure.\r
a405b86d 2370 @param[in] AutoPageBreak Will automatically set PageBreakEnabled.\r
2371 @param[in] AlwaysAllowNumbers Will never fail for number based flags.\r
94b17fa1 2372\r
2373 @retval EFI_SUCCESS The operation completed sucessfully.\r
a405b86d 2374 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.\r
2375 @retval EFI_INVALID_PARAMETER A parameter was invalid.\r
2376 @retval EFI_VOLUME_CORRUPTED The command line was corrupt.\r
2377 @retval EFI_DEVICE_ERROR The commands contained 2 opposing arguments. One\r
1e6e84c7 2378 of the command line arguments was returned in\r
94b17fa1 2379 ProblemParam if provided.\r
a405b86d 2380 @retval EFI_NOT_FOUND A argument required a value that was missing.\r
2381 The invalid command line argument was returned in\r
94b17fa1 2382 ProblemParam if provided.\r
2383**/\r
2384EFI_STATUS\r
2385EFIAPI\r
2247dde4 2386ShellCommandLineParseEx (\r
47d20b54
MK
2387 IN CONST SHELL_PARAM_ITEM *CheckList,\r
2388 OUT LIST_ENTRY **CheckPackage,\r
2389 OUT CHAR16 **ProblemParam OPTIONAL,\r
2390 IN BOOLEAN AutoPageBreak,\r
2391 IN BOOLEAN AlwaysAllowNumbers\r
a405b86d 2392 )\r
2393{\r
1e6e84c7 2394 //\r
94b17fa1 2395 // ASSERT that CheckList and CheckPackage aren't NULL\r
2396 //\r
47d20b54
MK
2397 ASSERT (CheckList != NULL);\r
2398 ASSERT (CheckPackage != NULL);\r
94b17fa1 2399\r
1e6e84c7 2400 //\r
94b17fa1 2401 // Check for UEFI Shell 2.0 protocols\r
2402 //\r
366f81a0 2403 if (gEfiShellParametersProtocol != NULL) {\r
47d20b54
MK
2404 return (InternalCommandLineParse (\r
2405 CheckList,\r
2406 CheckPackage,\r
2407 ProblemParam,\r
2408 AutoPageBreak,\r
2409 (CONST CHAR16 **)gEfiShellParametersProtocol->Argv,\r
2410 gEfiShellParametersProtocol->Argc,\r
2411 AlwaysAllowNumbers\r
2412 ));\r
94b17fa1 2413 }\r
2414\r
1e6e84c7 2415 //\r
94b17fa1 2416 // ASSERT That EFI Shell is not required\r
2417 //\r
2418 ASSERT (mEfiShellInterface != NULL);\r
47d20b54
MK
2419 return (InternalCommandLineParse (\r
2420 CheckList,\r
2421 CheckPackage,\r
2422 ProblemParam,\r
2423 AutoPageBreak,\r
2424 (CONST CHAR16 **)mEfiShellInterface->Argv,\r
2425 mEfiShellInterface->Argc,\r
2426 AlwaysAllowNumbers\r
2427 ));\r
94b17fa1 2428}\r
2429\r
2430/**\r
2431 Frees shell variable list that was returned from ShellCommandLineParse.\r
2432\r
2433 This function will free all the memory that was used for the CheckPackage\r
2434 list of postprocessed shell arguments.\r
2435\r
2436 this function has no return value.\r
2437\r
2438 if CheckPackage is NULL, then return\r
2439\r
2440 @param CheckPackage the list to de-allocate\r
2441 **/\r
2442VOID\r
2443EFIAPI\r
2444ShellCommandLineFreeVarList (\r
47d20b54 2445 IN LIST_ENTRY *CheckPackage\r
a405b86d 2446 )\r
2447{\r
47d20b54 2448 LIST_ENTRY *Node;\r
94b17fa1 2449\r
2450 //\r
2451 // check for CheckPackage == NULL\r
2452 //\r
2453 if (CheckPackage == NULL) {\r
2454 return;\r
2455 }\r
2456\r
2457 //\r
2458 // for each node in the list\r
2459 //\r
47d20b54
MK
2460 for ( Node = GetFirstNode (CheckPackage)\r
2461 ; !IsListEmpty (CheckPackage)\r
2462 ; Node = GetFirstNode (CheckPackage)\r
2463 )\r
2464 {\r
94b17fa1 2465 //\r
2466 // Remove it from the list\r
2467 //\r
47d20b54 2468 RemoveEntryList (Node);\r
94b17fa1 2469\r
2470 //\r
2471 // if it has a name free the name\r
2472 //\r
47d20b54
MK
2473 if (((SHELL_PARAM_PACKAGE *)Node)->Name != NULL) {\r
2474 FreePool (((SHELL_PARAM_PACKAGE *)Node)->Name);\r
94b17fa1 2475 }\r
2476\r
2477 //\r
2478 // if it has a value free the value\r
2479 //\r
47d20b54
MK
2480 if (((SHELL_PARAM_PACKAGE *)Node)->Value != NULL) {\r
2481 FreePool (((SHELL_PARAM_PACKAGE *)Node)->Value);\r
94b17fa1 2482 }\r
1e6e84c7 2483\r
94b17fa1 2484 //\r
2485 // free the node structure\r
2486 //\r
47d20b54 2487 FreePool ((SHELL_PARAM_PACKAGE *)Node);\r
94b17fa1 2488 }\r
47d20b54 2489\r
94b17fa1 2490 //\r
2491 // free the list head node\r
2492 //\r
47d20b54 2493 FreePool (CheckPackage);\r
94b17fa1 2494}\r
47d20b54 2495\r
94b17fa1 2496/**\r
2497 Checks for presence of a flag parameter\r
2498\r
2499 flag arguments are in the form of "-<Key>" or "/<Key>", but do not have a value following the key\r
2500\r
2501 if CheckPackage is NULL then return FALSE.\r
2502 if KeyString is NULL then ASSERT()\r
1e6e84c7 2503\r
94b17fa1 2504 @param CheckPackage The package of parsed command line arguments\r
2505 @param KeyString the Key of the command line argument to check for\r
2506\r
2507 @retval TRUE the flag is on the command line\r
2508 @retval FALSE the flag is not on the command line\r
2509 **/\r
2510BOOLEAN\r
2511EFIAPI\r
2512ShellCommandLineGetFlag (\r
47d20b54
MK
2513 IN CONST LIST_ENTRY *CONST CheckPackage,\r
2514 IN CONST CHAR16 *CONST KeyString\r
a405b86d 2515 )\r
2516{\r
47d20b54
MK
2517 LIST_ENTRY *Node;\r
2518 CHAR16 *TempString;\r
94b17fa1 2519\r
2520 //\r
0c1950ba 2521 // return FALSE for no package or KeyString is NULL\r
94b17fa1 2522 //\r
47d20b54 2523 if ((CheckPackage == NULL) || (KeyString == NULL)) {\r
94b17fa1 2524 return (FALSE);\r
2525 }\r
2526\r
2527 //\r
2528 // enumerate through the list of parametrs\r
2529 //\r
47d20b54
MK
2530 for ( Node = GetFirstNode (CheckPackage)\r
2531 ; !IsNull (CheckPackage, Node)\r
2532 ; Node = GetNextNode (CheckPackage, Node)\r
2533 )\r
2534 {\r
94b17fa1 2535 //\r
2536 // If the Name matches, return TRUE (and there may be NULL name)\r
2537 //\r
47d20b54 2538 if (((SHELL_PARAM_PACKAGE *)Node)->Name != NULL) {\r
9eb53ac3 2539 //\r
2540 // If Type is TypeStart then only compare the begining of the strings\r
2541 //\r
47d20b54
MK
2542 if (((SHELL_PARAM_PACKAGE *)Node)->Type == TypeStart) {\r
2543 if (StrnCmp (KeyString, ((SHELL_PARAM_PACKAGE *)Node)->Name, StrLen (KeyString)) == 0) {\r
252d9457 2544 return (TRUE);\r
2545 }\r
47d20b54 2546\r
252d9457 2547 TempString = NULL;\r
47d20b54 2548 TempString = StrnCatGrow (&TempString, NULL, KeyString, StrLen (((SHELL_PARAM_PACKAGE *)Node)->Name));\r
252d9457 2549 if (TempString != NULL) {\r
47d20b54
MK
2550 if (StringNoCaseCompare (&KeyString, &((SHELL_PARAM_PACKAGE *)Node)->Name) == 0) {\r
2551 FreePool (TempString);\r
252d9457 2552 return (TRUE);\r
2553 }\r
47d20b54
MK
2554\r
2555 FreePool (TempString);\r
252d9457 2556 }\r
47d20b54 2557 } else if (StringNoCaseCompare (&KeyString, &((SHELL_PARAM_PACKAGE *)Node)->Name) == 0) {\r
94b17fa1 2558 return (TRUE);\r
2559 }\r
2560 }\r
2561 }\r
47d20b54 2562\r
94b17fa1 2563 return (FALSE);\r
2564}\r
47d20b54 2565\r
94b17fa1 2566/**\r
a405b86d 2567 Returns value from command line argument.\r
94b17fa1 2568\r
a405b86d 2569 Value parameters are in the form of "-<Key> value" or "/<Key> value".\r
1e6e84c7 2570\r
a405b86d 2571 If CheckPackage is NULL, then return NULL.\r
94b17fa1 2572\r
a405b86d 2573 @param[in] CheckPackage The package of parsed command line arguments.\r
2574 @param[in] KeyString The Key of the command line argument to check for.\r
94b17fa1 2575\r
a405b86d 2576 @retval NULL The flag is not on the command line.\r
2577 @retval !=NULL The pointer to unicode string of the value.\r
2578**/\r
47d20b54 2579CONST CHAR16 *\r
94b17fa1 2580EFIAPI\r
2581ShellCommandLineGetValue (\r
47d20b54
MK
2582 IN CONST LIST_ENTRY *CheckPackage,\r
2583 IN CHAR16 *KeyString\r
a405b86d 2584 )\r
2585{\r
47d20b54
MK
2586 LIST_ENTRY *Node;\r
2587 CHAR16 *TempString;\r
94b17fa1 2588\r
2589 //\r
0c1950ba 2590 // return NULL for no package or KeyString is NULL\r
94b17fa1 2591 //\r
47d20b54 2592 if ((CheckPackage == NULL) || (KeyString == NULL)) {\r
94b17fa1 2593 return (NULL);\r
2594 }\r
2595\r
2596 //\r
2597 // enumerate through the list of parametrs\r
2598 //\r
47d20b54
MK
2599 for ( Node = GetFirstNode (CheckPackage)\r
2600 ; !IsNull (CheckPackage, Node)\r
2601 ; Node = GetNextNode (CheckPackage, Node)\r
2602 )\r
2603 {\r
94b17fa1 2604 //\r
252d9457 2605 // If the Name matches, return TRUE (and there may be NULL name)\r
94b17fa1 2606 //\r
47d20b54 2607 if (((SHELL_PARAM_PACKAGE *)Node)->Name != NULL) {\r
9eb53ac3 2608 //\r
2609 // If Type is TypeStart then only compare the begining of the strings\r
2610 //\r
47d20b54
MK
2611 if (((SHELL_PARAM_PACKAGE *)Node)->Type == TypeStart) {\r
2612 if (StrnCmp (KeyString, ((SHELL_PARAM_PACKAGE *)Node)->Name, StrLen (KeyString)) == 0) {\r
2613 return (((SHELL_PARAM_PACKAGE *)Node)->Name + StrLen (KeyString));\r
252d9457 2614 }\r
47d20b54 2615\r
252d9457 2616 TempString = NULL;\r
47d20b54 2617 TempString = StrnCatGrow (&TempString, NULL, KeyString, StrLen (((SHELL_PARAM_PACKAGE *)Node)->Name));\r
252d9457 2618 if (TempString != NULL) {\r
47d20b54
MK
2619 if (StringNoCaseCompare (&KeyString, &((SHELL_PARAM_PACKAGE *)Node)->Name) == 0) {\r
2620 FreePool (TempString);\r
2621 return (((SHELL_PARAM_PACKAGE *)Node)->Name + StrLen (KeyString));\r
252d9457 2622 }\r
47d20b54
MK
2623\r
2624 FreePool (TempString);\r
252d9457 2625 }\r
47d20b54
MK
2626 } else if (StringNoCaseCompare (&KeyString, &((SHELL_PARAM_PACKAGE *)Node)->Name) == 0) {\r
2627 return (((SHELL_PARAM_PACKAGE *)Node)->Value);\r
94b17fa1 2628 }\r
2629 }\r
2630 }\r
47d20b54 2631\r
94b17fa1 2632 return (NULL);\r
2633}\r
a405b86d 2634\r
94b17fa1 2635/**\r
a405b86d 2636 Returns raw value from command line argument.\r
94b17fa1 2637\r
a405b86d 2638 Raw value parameters are in the form of "value" in a specific position in the list.\r
1e6e84c7 2639\r
a405b86d 2640 If CheckPackage is NULL, then return NULL.\r
94b17fa1 2641\r
a405b86d 2642 @param[in] CheckPackage The package of parsed command line arguments.\r
2643 @param[in] Position The position of the value.\r
94b17fa1 2644\r
a405b86d 2645 @retval NULL The flag is not on the command line.\r
2646 @retval !=NULL The pointer to unicode string of the value.\r
94b17fa1 2647 **/\r
47d20b54 2648CONST CHAR16 *\r
94b17fa1 2649EFIAPI\r
2650ShellCommandLineGetRawValue (\r
47d20b54
MK
2651 IN CONST LIST_ENTRY *CONST CheckPackage,\r
2652 IN UINTN Position\r
a405b86d 2653 )\r
2654{\r
47d20b54 2655 LIST_ENTRY *Node;\r
94b17fa1 2656\r
2657 //\r
2658 // check for CheckPackage == NULL\r
2659 //\r
2660 if (CheckPackage == NULL) {\r
2661 return (NULL);\r
2662 }\r
2663\r
2664 //\r
2665 // enumerate through the list of parametrs\r
2666 //\r
47d20b54
MK
2667 for ( Node = GetFirstNode (CheckPackage)\r
2668 ; !IsNull (CheckPackage, Node)\r
2669 ; Node = GetNextNode (CheckPackage, Node)\r
2670 )\r
2671 {\r
94b17fa1 2672 //\r
2673 // If the position matches, return the value\r
2674 //\r
47d20b54
MK
2675 if (((SHELL_PARAM_PACKAGE *)Node)->OriginalPosition == Position) {\r
2676 return (((SHELL_PARAM_PACKAGE *)Node)->Value);\r
94b17fa1 2677 }\r
2678 }\r
47d20b54 2679\r
94b17fa1 2680 return (NULL);\r
b1f95a06 2681}\r
2247dde4 2682\r
2683/**\r
1e6e84c7 2684 returns the number of command line value parameters that were parsed.\r
2685\r
2247dde4 2686 this will not include flags.\r
2687\r
a405b86d 2688 @param[in] CheckPackage The package of parsed command line arguments.\r
2689\r
2247dde4 2690 @retval (UINTN)-1 No parsing has ocurred\r
2691 @return other The number of value parameters found\r
2692**/\r
2693UINTN\r
2694EFIAPI\r
47d20b54
MK
2695ShellCommandLineGetCount (\r
2696 IN CONST LIST_ENTRY *CheckPackage\r
125c2cf4 2697 )\r
2698{\r
a405b86d 2699 LIST_ENTRY *Node1;\r
2700 UINTN Count;\r
2701\r
2702 if (CheckPackage == NULL) {\r
2703 return (0);\r
2704 }\r
47d20b54
MK
2705\r
2706 for ( Node1 = GetFirstNode (CheckPackage), Count = 0\r
2707 ; !IsNull (CheckPackage, Node1)\r
2708 ; Node1 = GetNextNode (CheckPackage, Node1)\r
2709 )\r
2710 {\r
2711 if (((SHELL_PARAM_PACKAGE *)Node1)->Name == NULL) {\r
a405b86d 2712 Count++;\r
2713 }\r
2714 }\r
47d20b54 2715\r
a405b86d 2716 return (Count);\r
2247dde4 2717}\r
2718\r
36a9d672 2719/**\r
cceb4ebd 2720 Determines if a parameter is duplicated.\r
36a9d672 2721\r
1e6e84c7 2722 If Param is not NULL then it will point to a callee allocated string buffer\r
36a9d672 2723 with the parameter value if a duplicate is found.\r
2724\r
2725 If CheckPackage is NULL, then ASSERT.\r
2726\r
2727 @param[in] CheckPackage The package of parsed command line arguments.\r
2728 @param[out] Param Upon finding one, a pointer to the duplicated parameter.\r
2729\r
2730 @retval EFI_SUCCESS No parameters were duplicated.\r
2731 @retval EFI_DEVICE_ERROR A duplicate was found.\r
2732 **/\r
2733EFI_STATUS\r
2734EFIAPI\r
2735ShellCommandLineCheckDuplicate (\r
47d20b54
MK
2736 IN CONST LIST_ENTRY *CheckPackage,\r
2737 OUT CHAR16 **Param\r
36a9d672 2738 )\r
2739{\r
47d20b54
MK
2740 LIST_ENTRY *Node1;\r
2741 LIST_ENTRY *Node2;\r
2742\r
2743 ASSERT (CheckPackage != NULL);\r
2744\r
2745 for ( Node1 = GetFirstNode (CheckPackage)\r
2746 ; !IsNull (CheckPackage, Node1)\r
2747 ; Node1 = GetNextNode (CheckPackage, Node1)\r
2748 )\r
2749 {\r
2750 for ( Node2 = GetNextNode (CheckPackage, Node1)\r
2751 ; !IsNull (CheckPackage, Node2)\r
2752 ; Node2 = GetNextNode (CheckPackage, Node2)\r
2753 )\r
2754 {\r
2755 if ((((SHELL_PARAM_PACKAGE *)Node1)->Name != NULL) && (((SHELL_PARAM_PACKAGE *)Node2)->Name != NULL) && (StrCmp (((SHELL_PARAM_PACKAGE *)Node1)->Name, ((SHELL_PARAM_PACKAGE *)Node2)->Name) == 0)) {\r
36a9d672 2756 if (Param != NULL) {\r
2757 *Param = NULL;\r
47d20b54 2758 *Param = StrnCatGrow (Param, NULL, ((SHELL_PARAM_PACKAGE *)Node1)->Name, 0);\r
36a9d672 2759 }\r
47d20b54 2760\r
36a9d672 2761 return (EFI_DEVICE_ERROR);\r
2762 }\r
2763 }\r
2764 }\r
47d20b54 2765\r
36a9d672 2766 return (EFI_SUCCESS);\r
2767}\r
2768\r
975136ab 2769/**\r
1e6e84c7 2770 This is a find and replace function. Upon successful return the NewString is a copy of\r
975136ab 2771 SourceString with each instance of FindTarget replaced with ReplaceWith.\r
2772\r
b3011f40 2773 If SourceString and NewString overlap the behavior is undefined.\r
2774\r
975136ab 2775 If the string would grow bigger than NewSize it will halt and return error.\r
2776\r
4ff7e37b
ED
2777 @param[in] SourceString The string with source buffer.\r
2778 @param[in, out] NewString The string with resultant buffer.\r
2779 @param[in] NewSize The size in bytes of NewString.\r
2780 @param[in] FindTarget The string to look for.\r
2781 @param[in] ReplaceWith The string to replace FindTarget with.\r
2782 @param[in] SkipPreCarrot If TRUE will skip a FindTarget that has a '^'\r
2783 immediately before it.\r
2784 @param[in] ParameterReplacing If TRUE will add "" around items with spaces.\r
969c783b 2785\r
2786 @retval EFI_INVALID_PARAMETER SourceString was NULL.\r
2787 @retval EFI_INVALID_PARAMETER NewString was NULL.\r
2788 @retval EFI_INVALID_PARAMETER FindTarget was NULL.\r
2789 @retval EFI_INVALID_PARAMETER ReplaceWith was NULL.\r
2790 @retval EFI_INVALID_PARAMETER FindTarget had length < 1.\r
2791 @retval EFI_INVALID_PARAMETER SourceString had length < 1.\r
1e6e84c7 2792 @retval EFI_BUFFER_TOO_SMALL NewSize was less than the minimum size to hold\r
969c783b 2793 the new string (truncation occurred).\r
a405b86d 2794 @retval EFI_SUCCESS The string was successfully copied with replacement.\r
975136ab 2795**/\r
975136ab 2796EFI_STATUS\r
2797EFIAPI\r
47d20b54
MK
2798ShellCopySearchAndReplace (\r
2799 IN CHAR16 CONST *SourceString,\r
2800 IN OUT CHAR16 *NewString,\r
2801 IN UINTN NewSize,\r
2802 IN CONST CHAR16 *FindTarget,\r
2803 IN CONST CHAR16 *ReplaceWith,\r
2804 IN CONST BOOLEAN SkipPreCarrot,\r
2805 IN CONST BOOLEAN ParameterReplacing\r
1e6e84c7 2806 )\r
2247dde4 2807{\r
47d20b54
MK
2808 UINTN Size;\r
2809 CHAR16 *Replace;\r
2810\r
2811 if ( (SourceString == NULL)\r
2812 || (NewString == NULL)\r
2813 || (FindTarget == NULL)\r
2814 || (ReplaceWith == NULL)\r
2815 || (StrLen (FindTarget) < 1)\r
2816 || (StrLen (SourceString) < 1)\r
2817 )\r
2818 {\r
975136ab 2819 return (EFI_INVALID_PARAMETER);\r
2820 }\r
47d20b54 2821\r
a405b86d 2822 Replace = NULL;\r
47d20b54
MK
2823 if ((StrStr (ReplaceWith, L" ") == NULL) || !ParameterReplacing) {\r
2824 Replace = StrnCatGrow (&Replace, NULL, ReplaceWith, 0);\r
a405b86d 2825 } else {\r
47d20b54 2826 Replace = AllocateZeroPool (StrSize (ReplaceWith) + 2*sizeof (CHAR16));\r
beab0fc5 2827 if (Replace != NULL) {\r
47d20b54 2828 UnicodeSPrint (Replace, StrSize (ReplaceWith) + 2*sizeof (CHAR16), L"\"%s\"", ReplaceWith);\r
beab0fc5 2829 }\r
a405b86d 2830 }\r
47d20b54 2831\r
3e082d58 2832 if (Replace == NULL) {\r
2833 return (EFI_OUT_OF_RESOURCES);\r
2834 }\r
47d20b54
MK
2835\r
2836 NewString = ZeroMem (NewString, NewSize);\r
2247dde4 2837 while (*SourceString != CHAR_NULL) {\r
969c783b 2838 //\r
a405b86d 2839 // if we find the FindTarget and either Skip == FALSE or Skip and we\r
969c783b 2840 // dont have a carrot do a replace...\r
2841 //\r
47d20b54
MK
2842 if ( (StrnCmp (SourceString, FindTarget, StrLen (FindTarget)) == 0)\r
2843 && ((SkipPreCarrot && (*(SourceString-1) != L'^')) || !SkipPreCarrot)\r
2844 )\r
2845 {\r
2846 SourceString += StrLen (FindTarget);\r
2847 Size = StrSize (NewString);\r
2848 if ((Size + (StrLen (Replace)*sizeof (CHAR16))) > NewSize) {\r
2849 FreePool (Replace);\r
975136ab 2850 return (EFI_BUFFER_TOO_SMALL);\r
2851 }\r
47d20b54
MK
2852\r
2853 StrCatS (NewString, NewSize/sizeof (CHAR16), Replace);\r
975136ab 2854 } else {\r
47d20b54
MK
2855 Size = StrSize (NewString);\r
2856 if (Size + sizeof (CHAR16) > NewSize) {\r
2857 FreePool (Replace);\r
975136ab 2858 return (EFI_BUFFER_TOO_SMALL);\r
2859 }\r
47d20b54
MK
2860\r
2861 StrnCatS (NewString, NewSize/sizeof (CHAR16), SourceString, 1);\r
975136ab 2862 SourceString++;\r
2863 }\r
2864 }\r
47d20b54
MK
2865\r
2866 FreePool (Replace);\r
975136ab 2867 return (EFI_SUCCESS);\r
2868}\r
b1f95a06 2869\r
e2f8297f 2870/**\r
2871 Internal worker function to output a string.\r
2872\r
2873 This function will output a string to the correct StdOut.\r
2874\r
2875 @param[in] String The string to print out.\r
2876\r
2877 @retval EFI_SUCCESS The operation was sucessful.\r
2878 @retval !EFI_SUCCESS The operation failed.\r
2879**/\r
2880EFI_STATUS\r
e2f8297f 2881InternalPrintTo (\r
47d20b54 2882 IN CONST CHAR16 *String\r
e2f8297f 2883 )\r
2884{\r
47d20b54
MK
2885 UINTN Size;\r
2886\r
2887 Size = StrSize (String) - sizeof (CHAR16);\r
a405b86d 2888 if (Size == 0) {\r
2889 return (EFI_SUCCESS);\r
2890 }\r
47d20b54 2891\r
366f81a0 2892 if (gEfiShellParametersProtocol != NULL) {\r
47d20b54 2893 return (gEfiShellProtocol->WriteFile (gEfiShellParametersProtocol->StdOut, &Size, (VOID *)String));\r
e2f8297f 2894 }\r
47d20b54 2895\r
e2f8297f 2896 if (mEfiShellInterface != NULL) {\r
ba0014b9 2897 if (mEfiShellInterface->RedirArgc == 0) {\r
ba0014b9 2898 //\r
47d20b54
MK
2899 // Divide in half for old shell. Must be string length not size.\r
2900 //\r
2901 Size /= 2; // Divide in half only when no redirection.\r
06c355b4 2902 }\r
47d20b54
MK
2903\r
2904 return (mEfiShellInterface->StdOut->Write (mEfiShellInterface->StdOut, &Size, (VOID *)String));\r
e2f8297f 2905 }\r
47d20b54
MK
2906\r
2907 ASSERT (FALSE);\r
e2f8297f 2908 return (EFI_UNSUPPORTED);\r
2909}\r
2910\r
b1f95a06 2911/**\r
2912 Print at a specific location on the screen.\r
2913\r
f1b87e7a 2914 This function will move the cursor to a given screen location and print the specified string\r
1e6e84c7 2915\r
2916 If -1 is specified for either the Row or Col the current screen location for BOTH\r
f1b87e7a 2917 will be used.\r
b1f95a06 2918\r
2919 if either Row or Col is out of range for the current console, then ASSERT\r
2920 if Format is NULL, then ASSERT\r
2921\r
1e6e84c7 2922 In addition to the standard %-based flags as supported by UefiLib Print() this supports\r
b1f95a06 2923 the following additional flags:\r
2924 %N - Set output attribute to normal\r
2925 %H - Set output attribute to highlight\r
2926 %E - Set output attribute to error\r
2927 %B - Set output attribute to blue color\r
2928 %V - Set output attribute to green color\r
2929\r
2930 Note: The background color is controlled by the shell command cls.\r
2931\r
b1f95a06 2932 @param[in] Col the column to print at\r
252d9457 2933 @param[in] Row the row to print at\r
b1f95a06 2934 @param[in] Format the format string\r
2247dde4 2935 @param[in] Marker the marker for the variable argument list\r
b1f95a06 2936\r
a405b86d 2937 @return EFI_SUCCESS The operation was successful.\r
2938 @return EFI_DEVICE_ERROR The console device reported an error.\r
b1f95a06 2939**/\r
a405b86d 2940EFI_STATUS\r
47d20b54
MK
2941InternalShellPrintWorker (\r
2942 IN INT32 Col OPTIONAL,\r
2943 IN INT32 Row OPTIONAL,\r
2944 IN CONST CHAR16 *Format,\r
2945 IN VA_LIST Marker\r
1e6e84c7 2946 )\r
2247dde4 2947{\r
47d20b54
MK
2948 EFI_STATUS Status;\r
2949 CHAR16 *ResumeLocation;\r
2950 CHAR16 *FormatWalker;\r
2951 UINTN OriginalAttribute;\r
2952 CHAR16 *mPostReplaceFormat;\r
2953 CHAR16 *mPostReplaceFormat2;\r
2954\r
2955 mPostReplaceFormat = AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize));\r
89e8537a 2956 mPostReplaceFormat2 = AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize));\r
a405b86d 2957\r
47d20b54
MK
2958 if ((mPostReplaceFormat == NULL) || (mPostReplaceFormat2 == NULL)) {\r
2959 SHELL_FREE_NON_NULL (mPostReplaceFormat);\r
2960 SHELL_FREE_NON_NULL (mPostReplaceFormat2);\r
f8d3e689 2961 return (EFI_OUT_OF_RESOURCES);\r
2962 }\r
2963\r
a405b86d 2964 Status = EFI_SUCCESS;\r
2965 OriginalAttribute = gST->ConOut->Mode->Attribute;\r
1e6e84c7 2966\r
975136ab 2967 //\r
2968 // Back and forth each time fixing up 1 of our flags...\r
2969 //\r
47d20b54
MK
2970 Status = ShellCopySearchAndReplace (Format, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%N", L"%%N", FALSE, FALSE);\r
2971 ASSERT_EFI_ERROR (Status);\r
2972 Status = ShellCopySearchAndReplace (mPostReplaceFormat, mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), L"%E", L"%%E", FALSE, FALSE);\r
2973 ASSERT_EFI_ERROR (Status);\r
2974 Status = ShellCopySearchAndReplace (mPostReplaceFormat2, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%H", L"%%H", FALSE, FALSE);\r
2975 ASSERT_EFI_ERROR (Status);\r
2976 Status = ShellCopySearchAndReplace (mPostReplaceFormat, mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), L"%B", L"%%B", FALSE, FALSE);\r
2977 ASSERT_EFI_ERROR (Status);\r
2978 Status = ShellCopySearchAndReplace (mPostReplaceFormat2, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%V", L"%%V", FALSE, FALSE);\r
2979 ASSERT_EFI_ERROR (Status);\r
975136ab 2980\r
2981 //\r
2982 // Use the last buffer from replacing to print from...\r
2983 //\r
a405b86d 2984 UnicodeVSPrint (mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), mPostReplaceFormat, Marker);\r
b1f95a06 2985\r
47d20b54
MK
2986 if ((Col != -1) && (Row != -1)) {\r
2987 Status = gST->ConOut->SetCursorPosition (gST->ConOut, Col, Row);\r
975136ab 2988 }\r
2989\r
ecd3d59f 2990 FormatWalker = mPostReplaceFormat2;\r
2247dde4 2991 while (*FormatWalker != CHAR_NULL) {\r
975136ab 2992 //\r
2993 // Find the next attribute change request\r
2994 //\r
47d20b54 2995 ResumeLocation = StrStr (FormatWalker, L"%");\r
975136ab 2996 if (ResumeLocation != NULL) {\r
2247dde4 2997 *ResumeLocation = CHAR_NULL;\r
975136ab 2998 }\r
47d20b54 2999\r
975136ab 3000 //\r
3001 // print the current FormatWalker string\r
3002 //\r
47d20b54
MK
3003 if (StrLen (FormatWalker) > 0) {\r
3004 Status = InternalPrintTo (FormatWalker);\r
3005 if (EFI_ERROR (Status)) {\r
a405b86d 3006 break;\r
3007 }\r
3008 }\r
3009\r
975136ab 3010 //\r
3011 // update the attribute\r
3012 //\r
3013 if (ResumeLocation != NULL) {\r
d727614c 3014 if ((ResumeLocation != mPostReplaceFormat2) && (*(ResumeLocation-1) == L'^')) {\r
8bb7441e 3015 //\r
3016 // Move cursor back 1 position to overwrite the ^\r
3017 //\r
47d20b54 3018 gST->ConOut->SetCursorPosition (gST->ConOut, gST->ConOut->Mode->CursorColumn - 1, gST->ConOut->Mode->CursorRow);\r
8bb7441e 3019\r
5d46f17b 3020 //\r
3021 // Print a simple '%' symbol\r
3022 //\r
47d20b54 3023 Status = InternalPrintTo (L"%");\r
5d46f17b 3024 ResumeLocation = ResumeLocation - 1;\r
3025 } else {\r
3026 switch (*(ResumeLocation+1)) {\r
3027 case (L'N'):\r
47d20b54 3028 gST->ConOut->SetAttribute (gST->ConOut, OriginalAttribute);\r
a405b86d 3029 break;\r
5d46f17b 3030 case (L'E'):\r
47d20b54 3031 gST->ConOut->SetAttribute (gST->ConOut, EFI_TEXT_ATTR (EFI_YELLOW, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));\r
5d46f17b 3032 break;\r
3033 case (L'H'):\r
47d20b54 3034 gST->ConOut->SetAttribute (gST->ConOut, EFI_TEXT_ATTR (EFI_WHITE, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));\r
5d46f17b 3035 break;\r
3036 case (L'B'):\r
47d20b54 3037 gST->ConOut->SetAttribute (gST->ConOut, EFI_TEXT_ATTR (EFI_LIGHTBLUE, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));\r
5d46f17b 3038 break;\r
3039 case (L'V'):\r
47d20b54 3040 gST->ConOut->SetAttribute (gST->ConOut, EFI_TEXT_ATTR (EFI_LIGHTGREEN, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));\r
5d46f17b 3041 break;\r
3042 default:\r
3043 //\r
3044 // Print a simple '%' symbol\r
3045 //\r
47d20b54
MK
3046 Status = InternalPrintTo (L"%");\r
3047 if (EFI_ERROR (Status)) {\r
5d46f17b 3048 break;\r
3049 }\r
47d20b54 3050\r
5d46f17b 3051 ResumeLocation = ResumeLocation - 1;\r
3052 break;\r
3053 }\r
975136ab 3054 }\r
3055 } else {\r
3056 //\r
3057 // reset to normal now...\r
3058 //\r
975136ab 3059 break;\r
3060 }\r
3061\r
3062 //\r
3063 // update FormatWalker to Resume + 2 (skip the % and the indicator)\r
3064 //\r
3065 FormatWalker = ResumeLocation + 2;\r
3066 }\r
b1f95a06 3067\r
47d20b54 3068 gST->ConOut->SetAttribute (gST->ConOut, OriginalAttribute);\r
89e8537a 3069\r
47d20b54
MK
3070 SHELL_FREE_NON_NULL (mPostReplaceFormat);\r
3071 SHELL_FREE_NON_NULL (mPostReplaceFormat2);\r
a405b86d 3072 return (Status);\r
5f7431d0 3073}\r
2247dde4 3074\r
3075/**\r
3076 Print at a specific location on the screen.\r
3077\r
e2f8297f 3078 This function will move the cursor to a given screen location and print the specified string.\r
1e6e84c7 3079\r
3080 If -1 is specified for either the Row or Col the current screen location for BOTH\r
2247dde4 3081 will be used.\r
3082\r
e2f8297f 3083 If either Row or Col is out of range for the current console, then ASSERT.\r
3084 If Format is NULL, then ASSERT.\r
2247dde4 3085\r
1e6e84c7 3086 In addition to the standard %-based flags as supported by UefiLib Print() this supports\r
2247dde4 3087 the following additional flags:\r
3088 %N - Set output attribute to normal\r
3089 %H - Set output attribute to highlight\r
3090 %E - Set output attribute to error\r
3091 %B - Set output attribute to blue color\r
3092 %V - Set output attribute to green color\r
3093\r
3094 Note: The background color is controlled by the shell command cls.\r
3095\r
2247dde4 3096 @param[in] Col the column to print at\r
a405b86d 3097 @param[in] Row the row to print at\r
2247dde4 3098 @param[in] Format the format string\r
a405b86d 3099 @param[in] ... The variable argument list.\r
2247dde4 3100\r
a405b86d 3101 @return EFI_SUCCESS The printing was successful.\r
3102 @return EFI_DEVICE_ERROR The console device reported an error.\r
2247dde4 3103**/\r
a405b86d 3104EFI_STATUS\r
2247dde4 3105EFIAPI\r
47d20b54
MK
3106ShellPrintEx (\r
3107 IN INT32 Col OPTIONAL,\r
3108 IN INT32 Row OPTIONAL,\r
3109 IN CONST CHAR16 *Format,\r
2247dde4 3110 ...\r
1e6e84c7 3111 )\r
2247dde4 3112{\r
47d20b54
MK
3113 VA_LIST Marker;\r
3114 EFI_STATUS RetVal;\r
3115\r
3e082d58 3116 if (Format == NULL) {\r
3117 return (EFI_INVALID_PARAMETER);\r
3118 }\r
47d20b54 3119\r
2247dde4 3120 VA_START (Marker, Format);\r
47d20b54
MK
3121 RetVal = InternalShellPrintWorker (Col, Row, Format, Marker);\r
3122 VA_END (Marker);\r
3123 return (RetVal);\r
2247dde4 3124}\r
3125\r
3126/**\r
3127 Print at a specific location on the screen.\r
3128\r
e2f8297f 3129 This function will move the cursor to a given screen location and print the specified string.\r
1e6e84c7 3130\r
3131 If -1 is specified for either the Row or Col the current screen location for BOTH\r
e2f8297f 3132 will be used.\r
2247dde4 3133\r
e2f8297f 3134 If either Row or Col is out of range for the current console, then ASSERT.\r
3135 If Format is NULL, then ASSERT.\r
2247dde4 3136\r
1e6e84c7 3137 In addition to the standard %-based flags as supported by UefiLib Print() this supports\r
2247dde4 3138 the following additional flags:\r
1e6e84c7 3139 %N - Set output attribute to normal.\r
3140 %H - Set output attribute to highlight.\r
3141 %E - Set output attribute to error.\r
3142 %B - Set output attribute to blue color.\r
3143 %V - Set output attribute to green color.\r
2247dde4 3144\r
3145 Note: The background color is controlled by the shell command cls.\r
3146\r
1e6e84c7 3147 @param[in] Col The column to print at.\r
a405b86d 3148 @param[in] Row The row to print at.\r
1e6e84c7 3149 @param[in] Language The language of the string to retrieve. If this parameter\r
3150 is NULL, then the current platform language is used.\r
3151 @param[in] HiiFormatStringId The format string Id for getting from Hii.\r
3152 @param[in] HiiFormatHandle The format string Handle for getting from Hii.\r
a405b86d 3153 @param[in] ... The variable argument list.\r
2247dde4 3154\r
a405b86d 3155 @return EFI_SUCCESS The printing was successful.\r
3156 @return EFI_DEVICE_ERROR The console device reported an error.\r
2247dde4 3157**/\r
a405b86d 3158EFI_STATUS\r
2247dde4 3159EFIAPI\r
47d20b54
MK
3160ShellPrintHiiEx (\r
3161 IN INT32 Col OPTIONAL,\r
3162 IN INT32 Row OPTIONAL,\r
3163 IN CONST CHAR8 *Language OPTIONAL,\r
3164 IN CONST EFI_STRING_ID HiiFormatStringId,\r
3165 IN CONST EFI_HII_HANDLE HiiFormatHandle,\r
2247dde4 3166 ...\r
3167 )\r
3168{\r
47d20b54
MK
3169 VA_LIST Marker;\r
3170 CHAR16 *HiiFormatString;\r
3171 EFI_STATUS RetVal;\r
2247dde4 3172\r
eeb9744e
RN
3173 RetVal = EFI_DEVICE_ERROR;\r
3174\r
2247dde4 3175 VA_START (Marker, HiiFormatHandle);\r
47d20b54 3176 HiiFormatString = HiiGetString (HiiFormatHandle, HiiFormatStringId, Language);\r
eeb9744e
RN
3177 if (HiiFormatString != NULL) {\r
3178 RetVal = InternalShellPrintWorker (Col, Row, HiiFormatString, Marker);\r
3179 SHELL_FREE_NON_NULL (HiiFormatString);\r
3180 }\r
47d20b54
MK
3181\r
3182 VA_END (Marker);\r
2247dde4 3183\r
3184 return (RetVal);\r
3185}\r
3186\r
3187/**\r
3188 Function to determine if a given filename represents a file or a directory.\r
3189\r
3190 @param[in] DirName Path to directory to test.\r
3191\r
c8c22591 3192 @retval EFI_SUCCESS The Path represents a directory\r
3193 @retval EFI_NOT_FOUND The Path does not represent a directory\r
3194 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.\r
3195 @return The path failed to open\r
2247dde4 3196**/\r
3197EFI_STATUS\r
3198EFIAPI\r
47d20b54
MK
3199ShellIsDirectory (\r
3200 IN CONST CHAR16 *DirName\r
2247dde4 3201 )\r
3202{\r
47d20b54
MK
3203 EFI_STATUS Status;\r
3204 SHELL_FILE_HANDLE Handle;\r
3205 CHAR16 *TempLocation;\r
3206 CHAR16 *TempLocation2;\r
2247dde4 3207\r
47d20b54 3208 ASSERT (DirName != NULL);\r
ecd3d59f 3209\r
47d20b54
MK
3210 Handle = NULL;\r
3211 TempLocation = NULL;\r
2247dde4 3212\r
47d20b54
MK
3213 Status = ShellOpenFileByName (DirName, &Handle, EFI_FILE_MODE_READ, 0);\r
3214 if (EFI_ERROR (Status)) {\r
a405b86d 3215 //\r
3216 // try good logic first.\r
3217 //\r
366f81a0 3218 if (gEfiShellProtocol != NULL) {\r
47d20b54 3219 TempLocation = StrnCatGrow (&TempLocation, NULL, DirName, 0);\r
c8c22591 3220 if (TempLocation == NULL) {\r
47d20b54 3221 ShellCloseFile (&Handle);\r
c8c22591 3222 return (EFI_OUT_OF_RESOURCES);\r
3223 }\r
47d20b54
MK
3224\r
3225 TempLocation2 = StrStr (TempLocation, L":");\r
3226 if ((TempLocation2 != NULL) && (StrLen (StrStr (TempLocation, L":")) == 2)) {\r
3e082d58 3227 *(TempLocation2+1) = CHAR_NULL;\r
a405b86d 3228 }\r
47d20b54
MK
3229\r
3230 if (gEfiShellProtocol->GetDevicePathFromMap (TempLocation) != NULL) {\r
3231 FreePool (TempLocation);\r
a405b86d 3232 return (EFI_SUCCESS);\r
3233 }\r
47d20b54
MK
3234\r
3235 FreePool (TempLocation);\r
a405b86d 3236 } else {\r
3237 //\r
3238 // probably a map name?!?!!?\r
3239 //\r
47d20b54
MK
3240 TempLocation = StrStr (DirName, L"\\");\r
3241 if ((TempLocation != NULL) && (*(TempLocation+1) == CHAR_NULL)) {\r
a405b86d 3242 return (EFI_SUCCESS);\r
3243 }\r
3244 }\r
47d20b54 3245\r
2247dde4 3246 return (Status);\r
3247 }\r
3248\r
47d20b54
MK
3249 if (FileHandleIsDirectory (Handle) == EFI_SUCCESS) {\r
3250 ShellCloseFile (&Handle);\r
2247dde4 3251 return (EFI_SUCCESS);\r
3252 }\r
47d20b54
MK
3253\r
3254 ShellCloseFile (&Handle);\r
2247dde4 3255 return (EFI_NOT_FOUND);\r
3256}\r
3257\r
36a9d672 3258/**\r
3259 Function to determine if a given filename represents a file.\r
3260\r
3261 @param[in] Name Path to file to test.\r
3262\r
3263 @retval EFI_SUCCESS The Path represents a file.\r
3264 @retval EFI_NOT_FOUND The Path does not represent a file.\r
3265 @retval other The path failed to open.\r
3266**/\r
3267EFI_STATUS\r
3268EFIAPI\r
47d20b54
MK
3269ShellIsFile (\r
3270 IN CONST CHAR16 *Name\r
36a9d672 3271 )\r
3272{\r
47d20b54
MK
3273 EFI_STATUS Status;\r
3274 SHELL_FILE_HANDLE Handle;\r
36a9d672 3275\r
47d20b54 3276 ASSERT (Name != NULL);\r
ecd3d59f 3277\r
36a9d672 3278 Handle = NULL;\r
3279\r
47d20b54
MK
3280 Status = ShellOpenFileByName (Name, &Handle, EFI_FILE_MODE_READ, 0);\r
3281 if (EFI_ERROR (Status)) {\r
36a9d672 3282 return (Status);\r
3283 }\r
3284\r
47d20b54
MK
3285 if (FileHandleIsDirectory (Handle) != EFI_SUCCESS) {\r
3286 ShellCloseFile (&Handle);\r
36a9d672 3287 return (EFI_SUCCESS);\r
3288 }\r
47d20b54
MK
3289\r
3290 ShellCloseFile (&Handle);\r
36a9d672 3291 return (EFI_NOT_FOUND);\r
3292}\r
3293\r
b3011f40 3294/**\r
3295 Function to determine if a given filename represents a file.\r
3296\r
3297 This will search the CWD and then the Path.\r
3298\r
3299 If Name is NULL, then ASSERT.\r
3300\r
3301 @param[in] Name Path to file to test.\r
3302\r
3303 @retval EFI_SUCCESS The Path represents a file.\r
3304 @retval EFI_NOT_FOUND The Path does not represent a file.\r
3305 @retval other The path failed to open.\r
3306**/\r
3307EFI_STATUS\r
3308EFIAPI\r
47d20b54
MK
3309ShellIsFileInPath (\r
3310 IN CONST CHAR16 *Name\r
a405b86d 3311 )\r
3312{\r
b3011f40 3313 CHAR16 *NewName;\r
3314 EFI_STATUS Status;\r
3315\r
47d20b54 3316 if (!EFI_ERROR (ShellIsFile (Name))) {\r
a405b86d 3317 return (EFI_SUCCESS);\r
b3011f40 3318 }\r
3319\r
47d20b54 3320 NewName = ShellFindFilePath (Name);\r
b3011f40 3321 if (NewName == NULL) {\r
3322 return (EFI_NOT_FOUND);\r
3323 }\r
47d20b54
MK
3324\r
3325 Status = ShellIsFile (NewName);\r
3326 FreePool (NewName);\r
b3011f40 3327 return (Status);\r
3328}\r
252d9457 3329\r
74b0fb8c
JC
3330/**\r
3331 Function return the number converted from a hex representation of a number.\r
3332\r
3333 Note: this function cannot be used when (UINTN)(-1), (0xFFFFFFFF) may be a valid\r
3334 result. Use ShellConvertStringToUint64 instead.\r
3335\r
3336 @param[in] String String representation of a number.\r
3337\r
3338 @return The unsigned integer result of the conversion.\r
a048af3c 3339 @retval (UINTN)(-1) An error occurred.\r
74b0fb8c
JC
3340**/\r
3341UINTN\r
3342EFIAPI\r
47d20b54
MK
3343ShellHexStrToUintn (\r
3344 IN CONST CHAR16 *String\r
74b0fb8c
JC
3345 )\r
3346{\r
47d20b54 3347 UINT64 RetVal;\r
74b0fb8c 3348\r
47d20b54 3349 if (!EFI_ERROR (ShellConvertStringToUint64 (String, &RetVal, TRUE, TRUE))) {\r
74b0fb8c
JC
3350 return ((UINTN)RetVal);\r
3351 }\r
ba0014b9 3352\r
74b0fb8c
JC
3353 return ((UINTN)(-1));\r
3354}\r
3355\r
125c2cf4 3356/**\r
1e6e84c7 3357 Function to determine whether a string is decimal or hex representation of a number\r
d59fc244 3358 and return the number converted from the string. Spaces are always skipped.\r
125c2cf4 3359\r
3360 @param[in] String String representation of a number\r
3361\r
252d9457 3362 @return the number\r
3363 @retval (UINTN)(-1) An error ocurred.\r
125c2cf4 3364**/\r
3365UINTN\r
3366EFIAPI\r
47d20b54
MK
3367ShellStrToUintn (\r
3368 IN CONST CHAR16 *String\r
125c2cf4 3369 )\r
3370{\r
47d20b54
MK
3371 UINT64 RetVal;\r
3372 BOOLEAN Hex;\r
252d9457 3373\r
3374 Hex = FALSE;\r
3375\r
47d20b54 3376 if (!InternalShellIsHexOrDecimalNumber (String, Hex, TRUE, FALSE)) {\r
252d9457 3377 Hex = TRUE;\r
125c2cf4 3378 }\r
252d9457 3379\r
47d20b54 3380 if (!EFI_ERROR (ShellConvertStringToUint64 (String, &RetVal, Hex, TRUE))) {\r
252d9457 3381 return ((UINTN)RetVal);\r
3382 }\r
47d20b54 3383\r
252d9457 3384 return ((UINTN)(-1));\r
125c2cf4 3385}\r
3386\r
3387/**\r
3388 Safely append with automatic string resizing given length of Destination and\r
3389 desired length of copy from Source.\r
3390\r
3391 append the first D characters of Source to the end of Destination, where D is\r
3392 the lesser of Count and the StrLen() of Source. If appending those D characters\r
3393 will fit within Destination (whose Size is given as CurrentSize) and\r
1e6e84c7 3394 still leave room for a NULL terminator, then those characters are appended,\r
3395 starting at the original terminating NULL of Destination, and a new terminating\r
3396 NULL is appended.\r
125c2cf4 3397\r
3398 If appending D characters onto Destination will result in a overflow of the size\r
3399 given in CurrentSize the string will be grown such that the copy can be performed\r
3400 and CurrentSize will be updated to the new size.\r
3401\r
3402 If Source is NULL, there is nothing to append, just return the current buffer in\r
3403 Destination.\r
3404\r
3405 if Destination is NULL, then ASSERT()\r
3406 if Destination's current length (including NULL terminator) is already more then\r
3407 CurrentSize, then ASSERT()\r
3408\r
4ff7e37b
ED
3409 @param[in, out] Destination The String to append onto\r
3410 @param[in, out] CurrentSize on call the number of bytes in Destination. On\r
125c2cf4 3411 return possibly the new size (still in bytes). if NULL\r
3412 then allocate whatever is needed.\r
3413 @param[in] Source The String to append from\r
3414 @param[in] Count Maximum number of characters to append. if 0 then\r
3415 all are appended.\r
3416\r
3417 @return Destination return the resultant string.\r
3418**/\r
47d20b54 3419CHAR16 *\r
125c2cf4 3420EFIAPI\r
3421StrnCatGrow (\r
47d20b54
MK
3422 IN OUT CHAR16 **Destination,\r
3423 IN OUT UINTN *CurrentSize,\r
3424 IN CONST CHAR16 *Source,\r
3425 IN UINTN Count\r
125c2cf4 3426 )\r
3427{\r
47d20b54
MK
3428 UINTN DestinationStartSize;\r
3429 UINTN NewSize;\r
125c2cf4 3430\r
3431 //\r
3432 // ASSERTs\r
3433 //\r
47d20b54 3434 ASSERT (Destination != NULL);\r
125c2cf4 3435\r
3436 //\r
3437 // If there's nothing to do then just return Destination\r
3438 //\r
3439 if (Source == NULL) {\r
3440 return (*Destination);\r
3441 }\r
3442\r
3443 //\r
3444 // allow for un-initialized pointers, based on size being 0\r
3445 //\r
47d20b54 3446 if ((CurrentSize != NULL) && (*CurrentSize == 0)) {\r
125c2cf4 3447 *Destination = NULL;\r
3448 }\r
3449\r
3450 //\r
3451 // allow for NULL pointers address as Destination\r
3452 //\r
3453 if (*Destination != NULL) {\r
47d20b54
MK
3454 ASSERT (CurrentSize != 0);\r
3455 DestinationStartSize = StrSize (*Destination);\r
3456 ASSERT (DestinationStartSize <= *CurrentSize);\r
125c2cf4 3457 } else {\r
3458 DestinationStartSize = 0;\r
47d20b54 3459 // ASSERT(*CurrentSize == 0);\r
125c2cf4 3460 }\r
3461\r
3462 //\r
3463 // Append all of Source?\r
3464 //\r
3465 if (Count == 0) {\r
47d20b54 3466 Count = StrLen (Source);\r
125c2cf4 3467 }\r
3468\r
3469 //\r
3470 // Test and grow if required\r
3471 //\r
3472 if (CurrentSize != NULL) {\r
3473 NewSize = *CurrentSize;\r
47d20b54
MK
3474 if (NewSize < DestinationStartSize + (Count * sizeof (CHAR16))) {\r
3475 while (NewSize < (DestinationStartSize + (Count*sizeof (CHAR16)))) {\r
3476 NewSize += 2 * Count * sizeof (CHAR16);\r
f480fdc0 3477 }\r
47d20b54
MK
3478\r
3479 *Destination = ReallocatePool (*CurrentSize, NewSize, *Destination);\r
f480fdc0 3480 *CurrentSize = NewSize;\r
125c2cf4 3481 }\r
125c2cf4 3482 } else {\r
47d20b54
MK
3483 NewSize = (Count+1)*sizeof (CHAR16);\r
3484 *Destination = AllocateZeroPool (NewSize);\r
125c2cf4 3485 }\r
3486\r
3487 //\r
3488 // Now use standard StrnCat on a big enough buffer\r
3489 //\r
c9d92df0 3490 if (*Destination == NULL) {\r
3491 return (NULL);\r
3492 }\r
ba0014b9 3493\r
47d20b54 3494 StrnCatS (*Destination, NewSize/sizeof (CHAR16), Source, Count);\r
e75390f0 3495 return *Destination;\r
125c2cf4 3496}\r
c9d92df0 3497\r
3498/**\r
3499 Prompt the user and return the resultant answer to the requestor.\r
3500\r
3501 This function will display the requested question on the shell prompt and then\r
1d322461 3502 wait for an appropriate answer to be input from the console.\r
c9d92df0 3503\r
a405b86d 3504 if the SHELL_PROMPT_REQUEST_TYPE is SHELL_PROMPT_REQUEST_TYPE_YESNO, ShellPromptResponseTypeQuitContinue\r
c9d92df0 3505 or SHELL_PROMPT_REQUEST_TYPE_YESNOCANCEL then *Response is of type SHELL_PROMPT_RESPONSE.\r
3506\r
a405b86d 3507 if the SHELL_PROMPT_REQUEST_TYPE is ShellPromptResponseTypeFreeform then *Response is of type\r
c9d92df0 3508 CHAR16*.\r
3509\r
3510 In either case *Response must be callee freed if Response was not NULL;\r
3511\r
3512 @param Type What type of question is asked. This is used to filter the input\r
3513 to prevent invalid answers to question.\r
3514 @param Prompt Pointer to string prompt to use to request input.\r
3515 @param Response Pointer to Response which will be populated upon return.\r
3516\r
3517 @retval EFI_SUCCESS The operation was sucessful.\r
3518 @retval EFI_UNSUPPORTED The operation is not supported as requested.\r
3519 @retval EFI_INVALID_PARAMETER A parameter was invalid.\r
3520 @return other The operation failed.\r
3521**/\r
3522EFI_STATUS\r
3523EFIAPI\r
3524ShellPromptForResponse (\r
47d20b54
MK
3525 IN SHELL_PROMPT_REQUEST_TYPE Type,\r
3526 IN CHAR16 *Prompt OPTIONAL,\r
3527 IN OUT VOID **Response OPTIONAL\r
c9d92df0 3528 )\r
3529{\r
47d20b54
MK
3530 EFI_STATUS Status;\r
3531 EFI_INPUT_KEY Key;\r
3532 UINTN EventIndex;\r
3533 SHELL_PROMPT_RESPONSE *Resp;\r
3534 UINTN Size;\r
3535 CHAR16 *Buffer;\r
3536\r
3537 Status = EFI_UNSUPPORTED;\r
3538 Resp = NULL;\r
3539 Buffer = NULL;\r
3540 Size = 0;\r
a405b86d 3541 if (Type != ShellPromptResponseTypeFreeform) {\r
47d20b54 3542 Resp = (SHELL_PROMPT_RESPONSE *)AllocateZeroPool (sizeof (SHELL_PROMPT_RESPONSE));\r
3e082d58 3543 if (Resp == NULL) {\r
21902410
BB
3544 if (Response != NULL) {\r
3545 *Response = NULL;\r
3546 }\r
47d20b54 3547\r
3e082d58 3548 return (EFI_OUT_OF_RESOURCES);\r
3549 }\r
90bfa227 3550 }\r
c9d92df0 3551\r
47d20b54 3552 switch (Type) {\r
a405b86d 3553 case ShellPromptResponseTypeQuitContinue:\r
c9d92df0 3554 if (Prompt != NULL) {\r
47d20b54 3555 ShellPrintEx (-1, -1, L"%s", Prompt);\r
c9d92df0 3556 }\r
47d20b54 3557\r
c9d92df0 3558 //\r
3559 // wait for valid response\r
3560 //\r
3561 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);\r
3562 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);\r
47d20b54 3563 if (EFI_ERROR (Status)) {\r
31b018a6
JC
3564 break;\r
3565 }\r
47d20b54
MK
3566\r
3567 ShellPrintEx (-1, -1, L"%c", Key.UnicodeChar);\r
3568 if ((Key.UnicodeChar == L'Q') || (Key.UnicodeChar == L'q')) {\r
a405b86d 3569 *Resp = ShellPromptResponseQuit;\r
c9d92df0 3570 } else {\r
a405b86d 3571 *Resp = ShellPromptResponseContinue;\r
c9d92df0 3572 }\r
47d20b54 3573\r
c9d92df0 3574 break;\r
a405b86d 3575 case ShellPromptResponseTypeYesNoCancel:\r
47d20b54
MK
3576 if (Prompt != NULL) {\r
3577 ShellPrintEx (-1, -1, L"%s", Prompt);\r
c9d92df0 3578 }\r
47d20b54 3579\r
c9d92df0 3580 //\r
3581 // wait for valid response\r
3582 //\r
a405b86d 3583 *Resp = ShellPromptResponseMax;\r
3584 while (*Resp == ShellPromptResponseMax) {\r
47d20b54 3585 if (ShellGetExecutionBreakFlag ()) {\r
194ae48d
JC
3586 Status = EFI_ABORTED;\r
3587 break;\r
3588 }\r
47d20b54 3589\r
c9d92df0 3590 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);\r
3591 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);\r
47d20b54 3592 if (EFI_ERROR (Status)) {\r
31b018a6
JC
3593 break;\r
3594 }\r
47d20b54
MK
3595\r
3596 ShellPrintEx (-1, -1, L"%c", Key.UnicodeChar);\r
c9d92df0 3597 switch (Key.UnicodeChar) {\r
3598 case L'Y':\r
3599 case L'y':\r
a405b86d 3600 *Resp = ShellPromptResponseYes;\r
c9d92df0 3601 break;\r
3602 case L'N':\r
3603 case L'n':\r
a405b86d 3604 *Resp = ShellPromptResponseNo;\r
3605 break;\r
3606 case L'C':\r
3607 case L'c':\r
3608 *Resp = ShellPromptResponseCancel;\r
3609 break;\r
3610 }\r
3611 }\r
47d20b54 3612\r
ac55b925 3613 break;\r
47d20b54
MK
3614 case ShellPromptResponseTypeYesNoAllCancel:\r
3615 if (Prompt != NULL) {\r
3616 ShellPrintEx (-1, -1, L"%s", Prompt);\r
a405b86d 3617 }\r
47d20b54 3618\r
a405b86d 3619 //\r
3620 // wait for valid response\r
3621 //\r
3622 *Resp = ShellPromptResponseMax;\r
3623 while (*Resp == ShellPromptResponseMax) {\r
47d20b54 3624 if (ShellGetExecutionBreakFlag ()) {\r
194ae48d
JC
3625 Status = EFI_ABORTED;\r
3626 break;\r
3627 }\r
47d20b54 3628\r
a405b86d 3629 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);\r
3630 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);\r
47d20b54 3631 if (EFI_ERROR (Status)) {\r
31b018a6
JC
3632 break;\r
3633 }\r
ac55b925 3634\r
47d20b54 3635 if ((Key.UnicodeChar <= 127) && (Key.UnicodeChar >= 32)) {\r
ac55b925
TS
3636 ShellPrintEx (-1, -1, L"%c", Key.UnicodeChar);\r
3637 }\r
3638\r
a405b86d 3639 switch (Key.UnicodeChar) {\r
3640 case L'Y':\r
3641 case L'y':\r
3642 *Resp = ShellPromptResponseYes;\r
3643 break;\r
3644 case L'N':\r
3645 case L'n':\r
3646 *Resp = ShellPromptResponseNo;\r
c9d92df0 3647 break;\r
3648 case L'A':\r
3649 case L'a':\r
a405b86d 3650 *Resp = ShellPromptResponseAll;\r
c9d92df0 3651 break;\r
3652 case L'C':\r
3653 case L'c':\r
a405b86d 3654 *Resp = ShellPromptResponseCancel;\r
c9d92df0 3655 break;\r
3656 }\r
3657 }\r
47d20b54 3658\r
c9d92df0 3659 break;\r
a405b86d 3660 case ShellPromptResponseTypeEnterContinue:\r
3661 case ShellPromptResponseTypeAnyKeyContinue:\r
c9d92df0 3662 if (Prompt != NULL) {\r
47d20b54 3663 ShellPrintEx (-1, -1, L"%s", Prompt);\r
c9d92df0 3664 }\r
47d20b54 3665\r
c9d92df0 3666 //\r
3667 // wait for valid response\r
3668 //\r
a405b86d 3669 *Resp = ShellPromptResponseMax;\r
3670 while (*Resp == ShellPromptResponseMax) {\r
47d20b54 3671 if (ShellGetExecutionBreakFlag ()) {\r
194ae48d
JC
3672 Status = EFI_ABORTED;\r
3673 break;\r
3674 }\r
47d20b54 3675\r
c9d92df0 3676 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);\r
a405b86d 3677 if (Type == ShellPromptResponseTypeEnterContinue) {\r
c9d92df0 3678 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);\r
47d20b54 3679 if (EFI_ERROR (Status)) {\r
31b018a6
JC
3680 break;\r
3681 }\r
47d20b54
MK
3682\r
3683 ShellPrintEx (-1, -1, L"%c", Key.UnicodeChar);\r
c9d92df0 3684 if (Key.UnicodeChar == CHAR_CARRIAGE_RETURN) {\r
a405b86d 3685 *Resp = ShellPromptResponseContinue;\r
c9d92df0 3686 break;\r
3687 }\r
3688 }\r
47d20b54 3689\r
a405b86d 3690 if (Type == ShellPromptResponseTypeAnyKeyContinue) {\r
3691 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);\r
47d20b54 3692 ASSERT_EFI_ERROR (Status);\r
a405b86d 3693 *Resp = ShellPromptResponseContinue;\r
3694 break;\r
3695 }\r
3696 }\r
47d20b54 3697\r
a405b86d 3698 break;\r
3699 case ShellPromptResponseTypeYesNo:\r
47d20b54
MK
3700 if (Prompt != NULL) {\r
3701 ShellPrintEx (-1, -1, L"%s", Prompt);\r
a405b86d 3702 }\r
47d20b54 3703\r
a405b86d 3704 //\r
3705 // wait for valid response\r
3706 //\r
3707 *Resp = ShellPromptResponseMax;\r
3708 while (*Resp == ShellPromptResponseMax) {\r
47d20b54 3709 if (ShellGetExecutionBreakFlag ()) {\r
194ae48d
JC
3710 Status = EFI_ABORTED;\r
3711 break;\r
3712 }\r
47d20b54 3713\r
a405b86d 3714 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);\r
3715 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);\r
47d20b54 3716 if (EFI_ERROR (Status)) {\r
31b018a6
JC
3717 break;\r
3718 }\r
47d20b54
MK
3719\r
3720 ShellPrintEx (-1, -1, L"%c", Key.UnicodeChar);\r
a405b86d 3721 switch (Key.UnicodeChar) {\r
3722 case L'Y':\r
3723 case L'y':\r
3724 *Resp = ShellPromptResponseYes;\r
3725 break;\r
3726 case L'N':\r
3727 case L'n':\r
3728 *Resp = ShellPromptResponseNo;\r
3729 break;\r
3730 }\r
3731 }\r
47d20b54 3732\r
a405b86d 3733 break;\r
3734 case ShellPromptResponseTypeFreeform:\r
3735 if (Prompt != NULL) {\r
47d20b54 3736 ShellPrintEx (-1, -1, L"%s", Prompt);\r
a405b86d 3737 }\r
47d20b54
MK
3738\r
3739 while (1) {\r
3740 if (ShellGetExecutionBreakFlag ()) {\r
194ae48d
JC
3741 Status = EFI_ABORTED;\r
3742 break;\r
3743 }\r
47d20b54 3744\r
a405b86d 3745 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);\r
3746 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);\r
47d20b54 3747 if (EFI_ERROR (Status)) {\r
31b018a6
JC
3748 break;\r
3749 }\r
47d20b54
MK
3750\r
3751 ShellPrintEx (-1, -1, L"%c", Key.UnicodeChar);\r
a405b86d 3752 if (Key.UnicodeChar == CHAR_CARRIAGE_RETURN) {\r
c9d92df0 3753 break;\r
3754 }\r
47d20b54
MK
3755\r
3756 ASSERT ((Buffer == NULL && Size == 0) || (Buffer != NULL));\r
3757 StrnCatGrow (&Buffer, &Size, &Key.UnicodeChar, 1);\r
c9d92df0 3758 }\r
47d20b54 3759\r
c9d92df0 3760 break;\r
a405b86d 3761 //\r
3762 // This is the location to add new prompt types.\r
194ae48d 3763 // If your new type loops remember to add ExecutionBreak support.\r
a405b86d 3764 //\r
c9d92df0 3765 default:\r
47d20b54 3766 ASSERT (FALSE);\r
c9d92df0 3767 }\r
3768\r
3769 if (Response != NULL) {\r
a405b86d 3770 if (Resp != NULL) {\r
3771 *Response = Resp;\r
3772 } else if (Buffer != NULL) {\r
3773 *Response = Buffer;\r
21902410
BB
3774 } else {\r
3775 *Response = NULL;\r
a405b86d 3776 }\r
c9d92df0 3777 } else {\r
a405b86d 3778 if (Resp != NULL) {\r
47d20b54 3779 FreePool (Resp);\r
a405b86d 3780 }\r
47d20b54 3781\r
a405b86d 3782 if (Buffer != NULL) {\r
47d20b54 3783 FreePool (Buffer);\r
a405b86d 3784 }\r
c9d92df0 3785 }\r
3786\r
47d20b54 3787 ShellPrintEx (-1, -1, L"\r\n");\r
c9d92df0 3788 return (Status);\r
3789}\r
3790\r
3791/**\r
3792 Prompt the user and return the resultant answer to the requestor.\r
3793\r
3794 This function is the same as ShellPromptForResponse, except that the prompt is\r
3795 automatically pulled from HII.\r
3796\r
3797 @param Type What type of question is asked. This is used to filter the input\r
3798 to prevent invalid answers to question.\r
a405b86d 3799 @param[in] HiiFormatStringId The format string Id for getting from Hii.\r
3800 @param[in] HiiFormatHandle The format string Handle for getting from Hii.\r
3801 @param Response Pointer to Response which will be populated upon return.\r
c9d92df0 3802\r
3803 @retval EFI_SUCCESS the operation was sucessful.\r
3804 @return other the operation failed.\r
3805\r
3806 @sa ShellPromptForResponse\r
3807**/\r
3808EFI_STATUS\r
3809EFIAPI\r
3810ShellPromptForResponseHii (\r
47d20b54
MK
3811 IN SHELL_PROMPT_REQUEST_TYPE Type,\r
3812 IN CONST EFI_STRING_ID HiiFormatStringId,\r
3813 IN CONST EFI_HII_HANDLE HiiFormatHandle,\r
3814 IN OUT VOID **Response\r
c9d92df0 3815 )\r
3816{\r
3817 CHAR16 *Prompt;\r
3818 EFI_STATUS Status;\r
3819\r
47d20b54
MK
3820 Prompt = HiiGetString (HiiFormatHandle, HiiFormatStringId, NULL);\r
3821 Status = ShellPromptForResponse (Type, Prompt, Response);\r
3822 FreePool (Prompt);\r
c9d92df0 3823 return (Status);\r
3824}\r
3825\r
a405b86d 3826/**\r
3827 Function to determin if an entire string is a valid number.\r
3828\r
3829 If Hex it must be preceeded with a 0x or has ForceHex, set TRUE.\r
c9d92df0 3830\r
a405b86d 3831 @param[in] String The string to evaluate.\r
3832 @param[in] ForceHex TRUE - always assume hex.\r
3833 @param[in] StopAtSpace TRUE to halt upon finding a space, FALSE to keep going.\r
658bf43e 3834 @param[in] TimeNumbers TRUE to allow numbers with ":", FALSE otherwise.\r
a405b86d 3835\r
3836 @retval TRUE It is all numeric (dec/hex) characters.\r
3837 @retval FALSE There is a non-numeric character.\r
3838**/\r
3839BOOLEAN\r
252d9457 3840InternalShellIsHexOrDecimalNumber (\r
a405b86d 3841 IN CONST CHAR16 *String,\r
3842 IN CONST BOOLEAN ForceHex,\r
658bf43e 3843 IN CONST BOOLEAN StopAtSpace,\r
3844 IN CONST BOOLEAN TimeNumbers\r
a405b86d 3845 )\r
3846{\r
47d20b54
MK
3847 BOOLEAN Hex;\r
3848 BOOLEAN LeadingZero;\r
ee33344c
RN
3849\r
3850 if (String == NULL) {\r
3851 return FALSE;\r
3852 }\r
a405b86d 3853\r
3854 //\r
3855 // chop off a single negative sign\r
3856 //\r
ee33344c 3857 if (*String == L'-') {\r
a405b86d 3858 String++;\r
3859 }\r
b0934ac4 3860\r
ee33344c
RN
3861 if (*String == CHAR_NULL) {\r
3862 return FALSE;\r
a405b86d 3863 }\r
3864\r
3865 //\r
3866 // chop leading zeroes\r
3867 //\r
ee33344c 3868 LeadingZero = FALSE;\r
47d20b54 3869 while (*String == L'0') {\r
a405b86d 3870 String++;\r
ee33344c 3871 LeadingZero = TRUE;\r
a405b86d 3872 }\r
47d20b54 3873\r
a405b86d 3874 //\r
3875 // allow '0x' or '0X', but not 'x' or 'X'\r
3876 //\r
47d20b54 3877 if ((*String == L'x') || (*String == L'X')) {\r
ee33344c 3878 if (!LeadingZero) {\r
a405b86d 3879 //\r
3880 // we got an x without a preceeding 0\r
3881 //\r
3882 return (FALSE);\r
3883 }\r
47d20b54 3884\r
a405b86d 3885 String++;\r
3886 Hex = TRUE;\r
3887 } else if (ForceHex) {\r
3888 Hex = TRUE;\r
3889 } else {\r
3890 Hex = FALSE;\r
3891 }\r
3892\r
3893 //\r
3894 // loop through the remaining characters and use the lib function\r
3895 //\r
47d20b54 3896 for ( ; *String != CHAR_NULL && !(StopAtSpace && *String == L' '); String++) {\r
658bf43e 3897 if (TimeNumbers && (String[0] == L':')) {\r
3898 continue;\r
3899 }\r
47d20b54 3900\r
a405b86d 3901 if (Hex) {\r
47d20b54 3902 if (!ShellIsHexaDecimalDigitCharacter (*String)) {\r
a405b86d 3903 return (FALSE);\r
3904 }\r
3905 } else {\r
47d20b54 3906 if (!ShellIsDecimalDigitCharacter (*String)) {\r
a405b86d 3907 return (FALSE);\r
3908 }\r
3909 }\r
3910 }\r
252d9457 3911\r
a405b86d 3912 return (TRUE);\r
3913}\r
3914\r
3915/**\r
3916 Function to determine if a given filename exists.\r
3917\r
3918 @param[in] Name Path to test.\r
3919\r
3920 @retval EFI_SUCCESS The Path represents a file.\r
3921 @retval EFI_NOT_FOUND The Path does not represent a file.\r
3922 @retval other The path failed to open.\r
3923**/\r
3924EFI_STATUS\r
3925EFIAPI\r
47d20b54
MK
3926ShellFileExists (\r
3927 IN CONST CHAR16 *Name\r
a405b86d 3928 )\r
3929{\r
47d20b54
MK
3930 EFI_STATUS Status;\r
3931 EFI_SHELL_FILE_INFO *List;\r
a405b86d 3932\r
47d20b54 3933 ASSERT (Name != NULL);\r
a405b86d 3934\r
47d20b54
MK
3935 List = NULL;\r
3936 Status = ShellOpenFileMetaArg ((CHAR16 *)Name, EFI_FILE_MODE_READ, &List);\r
3937 if (EFI_ERROR (Status)) {\r
a405b86d 3938 return (Status);\r
3939 }\r
3940\r
47d20b54 3941 ShellCloseFileMetaArg (&List);\r
a405b86d 3942\r
3943 return (EFI_SUCCESS);\r
3944}\r
252d9457 3945\r
252d9457 3946/**\r
3947 Convert a Unicode character to numerical value.\r
3948\r
3949 This internal function only deal with Unicode character\r
3950 which maps to a valid hexadecimal ASII character, i.e.\r
b0934ac4 3951 L'0' to L'9', L'a' to L'f' or L'A' to L'F'. For other\r
252d9457 3952 Unicode character, the value returned does not make sense.\r
3953\r
3954 @param Char The character to convert.\r
3955\r
3956 @return The numerical value converted.\r
3957\r
3958**/\r
3959UINTN\r
252d9457 3960InternalShellHexCharToUintn (\r
47d20b54 3961 IN CHAR16 Char\r
252d9457 3962 )\r
3963{\r
3964 if (ShellIsDecimalDigitCharacter (Char)) {\r
3965 return Char - L'0';\r
3966 }\r
3967\r
3d086fed 3968 return (10 + CharToUpper (Char) - L'A');\r
252d9457 3969}\r
3970\r
3971/**\r
3972 Convert a Null-terminated Unicode hexadecimal string to a value of type UINT64.\r
3973\r
80f3e34f 3974 This function returns a value of type UINT64 by interpreting the contents\r
252d9457 3975 of the Unicode string specified by String as a hexadecimal number.\r
3976 The format of the input Unicode string String is:\r
3977\r
3978 [spaces][zeros][x][hexadecimal digits].\r
3979\r
3980 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].\r
3981 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix.\r
3982 If "x" appears in the input string, it must be prefixed with at least one 0.\r
3983 The function will ignore the pad space, which includes spaces or tab characters,\r
3984 before [zeros], [x] or [hexadecimal digit]. The running zero before [x] or\r
3985 [hexadecimal digit] will be ignored. Then, the decoding starts after [x] or the\r
3986 first valid hexadecimal digit. Then, the function stops at the first character that is\r
3987 a not a valid hexadecimal character or NULL, whichever one comes first.\r
3988\r
3989 If String has only pad spaces, then zero is returned.\r
3990 If String has no leading pad spaces, leading zeros or valid hexadecimal digits,\r
3991 then zero is returned.\r
3992\r
3993 @param[in] String A pointer to a Null-terminated Unicode string.\r
3994 @param[out] Value Upon a successful return the value of the conversion.\r
3995 @param[in] StopAtSpace FALSE to skip spaces.\r
3996\r
3997 @retval EFI_SUCCESS The conversion was successful.\r
3998 @retval EFI_INVALID_PARAMETER A parameter was NULL or invalid.\r
a048af3c 3999 @retval EFI_DEVICE_ERROR An overflow occurred.\r
252d9457 4000**/\r
4001EFI_STATUS\r
252d9457 4002InternalShellStrHexToUint64 (\r
4003 IN CONST CHAR16 *String,\r
47d20b54 4004 OUT UINT64 *Value,\r
252d9457 4005 IN CONST BOOLEAN StopAtSpace\r
4006 )\r
4007{\r
47d20b54 4008 UINT64 Result;\r
252d9457 4009\r
47d20b54 4010 if ((String == NULL) || (StrSize (String) == 0) || (Value == NULL)) {\r
252d9457 4011 return (EFI_INVALID_PARAMETER);\r
4012 }\r
b0934ac4 4013\r
252d9457 4014 //\r
b0934ac4 4015 // Ignore the pad spaces (space or tab)\r
252d9457 4016 //\r
4017 while ((*String == L' ') || (*String == L'\t')) {\r
4018 String++;\r
4019 }\r
4020\r
4021 //\r
4022 // Ignore leading Zeros after the spaces\r
4023 //\r
4024 while (*String == L'0') {\r
4025 String++;\r
4026 }\r
4027\r
3d086fed 4028 if (CharToUpper (*String) == L'X') {\r
252d9457 4029 if (*(String - 1) != L'0') {\r
4030 return 0;\r
4031 }\r
47d20b54 4032\r
252d9457 4033 //\r
4034 // Skip the 'X'\r
4035 //\r
4036 String++;\r
4037 }\r
4038\r
4039 Result = 0;\r
b0934ac4 4040\r
252d9457 4041 //\r
80f3e34f 4042 // there is a space where there should't be\r
252d9457 4043 //\r
80f3e34f
JC
4044 if (*String == L' ') {\r
4045 return (EFI_INVALID_PARAMETER);\r
252d9457 4046 }\r
b0934ac4 4047\r
252d9457 4048 while (ShellIsHexaDecimalDigitCharacter (*String)) {\r
4049 //\r
b0934ac4 4050 // If the Hex Number represented by String overflows according\r
80f3e34f 4051 // to the range defined by UINT64, then return EFI_DEVICE_ERROR.\r
252d9457 4052 //\r
47d20b54
MK
4053 if (!(Result <= (RShiftU64 ((((UINT64) ~0) - InternalShellHexCharToUintn (*String)), 4)))) {\r
4054 // if (!(Result <= ((((UINT64) ~0) - InternalShellHexCharToUintn (*String)) >> 4))) {\r
252d9457 4055 return (EFI_DEVICE_ERROR);\r
4056 }\r
4057\r
47d20b54 4058 Result = (LShiftU64 (Result, 4));\r
252d9457 4059 Result += InternalShellHexCharToUintn (*String);\r
4060 String++;\r
4061\r
4062 //\r
49bd498d 4063 // stop at spaces if requested\r
252d9457 4064 //\r
47d20b54 4065 if (StopAtSpace && (*String == L' ')) {\r
49bd498d 4066 break;\r
252d9457 4067 }\r
4068 }\r
4069\r
4070 *Value = Result;\r
4071 return (EFI_SUCCESS);\r
4072}\r
4073\r
4074/**\r
4075 Convert a Null-terminated Unicode decimal string to a value of\r
4076 type UINT64.\r
4077\r
4078 This function returns a value of type UINT64 by interpreting the contents\r
4079 of the Unicode string specified by String as a decimal number. The format\r
4080 of the input Unicode string String is:\r
4081\r
4082 [spaces] [decimal digits].\r
4083\r
4084 The valid decimal digit character is in the range [0-9]. The\r
4085 function will ignore the pad space, which includes spaces or\r
4086 tab characters, before [decimal digits]. The running zero in the\r
4087 beginning of [decimal digits] will be ignored. Then, the function\r
4088 stops at the first character that is a not a valid decimal character\r
4089 or a Null-terminator, whichever one comes first.\r
4090\r
4091 If String has only pad spaces, then 0 is returned.\r
4092 If String has no pad spaces or valid decimal digits,\r
4093 then 0 is returned.\r
4094\r
4095 @param[in] String A pointer to a Null-terminated Unicode string.\r
4096 @param[out] Value Upon a successful return the value of the conversion.\r
4097 @param[in] StopAtSpace FALSE to skip spaces.\r
4098\r
4099 @retval EFI_SUCCESS The conversion was successful.\r
4100 @retval EFI_INVALID_PARAMETER A parameter was NULL or invalid.\r
a048af3c 4101 @retval EFI_DEVICE_ERROR An overflow occurred.\r
252d9457 4102**/\r
4103EFI_STATUS\r
252d9457 4104InternalShellStrDecimalToUint64 (\r
47d20b54
MK
4105 IN CONST CHAR16 *String,\r
4106 OUT UINT64 *Value,\r
252d9457 4107 IN CONST BOOLEAN StopAtSpace\r
4108 )\r
4109{\r
47d20b54 4110 UINT64 Result;\r
252d9457 4111\r
47d20b54 4112 if ((String == NULL) || (StrSize (String) == 0) || (Value == NULL)) {\r
252d9457 4113 return (EFI_INVALID_PARAMETER);\r
4114 }\r
4115\r
4116 //\r
4117 // Ignore the pad spaces (space or tab)\r
4118 //\r
4119 while ((*String == L' ') || (*String == L'\t')) {\r
4120 String++;\r
4121 }\r
4122\r
4123 //\r
4124 // Ignore leading Zeros after the spaces\r
4125 //\r
4126 while (*String == L'0') {\r
4127 String++;\r
4128 }\r
4129\r
4130 Result = 0;\r
4131\r
4132 //\r
ba0014b9 4133 // Stop upon space if requested\r
80f3e34f 4134 // (if the whole value was 0)\r
252d9457 4135 //\r
47d20b54 4136 if (StopAtSpace && (*String == L' ')) {\r
80f3e34f
JC
4137 *Value = Result;\r
4138 return (EFI_SUCCESS);\r
252d9457 4139 }\r
80f3e34f 4140\r
252d9457 4141 while (ShellIsDecimalDigitCharacter (*String)) {\r
4142 //\r
b0934ac4 4143 // If the number represented by String overflows according\r
80f3e34f 4144 // to the range defined by UINT64, then return EFI_DEVICE_ERROR.\r
252d9457 4145 //\r
b0934ac4 4146\r
47d20b54 4147 if (!(Result <= (DivU64x32 ((((UINT64) ~0) - (*String - L'0')), 10)))) {\r
252d9457 4148 return (EFI_DEVICE_ERROR);\r
4149 }\r
4150\r
47d20b54 4151 Result = MultU64x32 (Result, 10) + (*String - L'0');\r
252d9457 4152 String++;\r
4153\r
4154 //\r
4155 // Stop at spaces if requested\r
4156 //\r
47d20b54 4157 if (StopAtSpace && (*String == L' ')) {\r
252d9457 4158 break;\r
4159 }\r
4160 }\r
4161\r
4162 *Value = Result;\r
b0934ac4 4163\r
252d9457 4164 return (EFI_SUCCESS);\r
4165}\r
4166\r
4167/**\r
4168 Function to verify and convert a string to its numerical value.\r
4169\r
4170 If Hex it must be preceeded with a 0x, 0X, or has ForceHex set TRUE.\r
4171\r
4172 @param[in] String The string to evaluate.\r
4173 @param[out] Value Upon a successful return the value of the conversion.\r
4174 @param[in] ForceHex TRUE - always assume hex.\r
4175 @param[in] StopAtSpace FALSE to skip spaces.\r
b0934ac4 4176\r
252d9457 4177 @retval EFI_SUCCESS The conversion was successful.\r
4178 @retval EFI_INVALID_PARAMETER String contained an invalid character.\r
4179 @retval EFI_NOT_FOUND String was a number, but Value was NULL.\r
4180**/\r
4181EFI_STATUS\r
4182EFIAPI\r
47d20b54 4183ShellConvertStringToUint64 (\r
252d9457 4184 IN CONST CHAR16 *String,\r
47d20b54 4185 OUT UINT64 *Value,\r
252d9457 4186 IN CONST BOOLEAN ForceHex,\r
4187 IN CONST BOOLEAN StopAtSpace\r
4188 )\r
4189{\r
4190 UINT64 RetVal;\r
4191 CONST CHAR16 *Walker;\r
4192 EFI_STATUS Status;\r
4193 BOOLEAN Hex;\r
4194\r
4195 Hex = ForceHex;\r
4196\r
47d20b54 4197 if (!InternalShellIsHexOrDecimalNumber (String, Hex, StopAtSpace, FALSE)) {\r
252d9457 4198 if (!Hex) {\r
4199 Hex = TRUE;\r
47d20b54 4200 if (!InternalShellIsHexOrDecimalNumber (String, Hex, StopAtSpace, FALSE)) {\r
252d9457 4201 return (EFI_INVALID_PARAMETER);\r
4202 }\r
4203 } else {\r
4204 return (EFI_INVALID_PARAMETER);\r
4205 }\r
4206 }\r
4207\r
4208 //\r
4209 // Chop off leading spaces\r
4210 //\r
47d20b54
MK
4211 for (Walker = String; Walker != NULL && *Walker != CHAR_NULL && *Walker == L' '; Walker++) {\r
4212 }\r
252d9457 4213\r
4214 //\r
4215 // make sure we have something left that is numeric.\r
4216 //\r
47d20b54 4217 if ((Walker == NULL) || (*Walker == CHAR_NULL) || !InternalShellIsHexOrDecimalNumber (Walker, Hex, StopAtSpace, FALSE)) {\r
252d9457 4218 return (EFI_INVALID_PARAMETER);\r
b0934ac4 4219 }\r
252d9457 4220\r
4221 //\r
4222 // do the conversion.\r
4223 //\r
47d20b54
MK
4224 if (Hex || (StrnCmp (Walker, L"0x", 2) == 0) || (StrnCmp (Walker, L"0X", 2) == 0)) {\r
4225 Status = InternalShellStrHexToUint64 (Walker, &RetVal, StopAtSpace);\r
252d9457 4226 } else {\r
47d20b54 4227 Status = InternalShellStrDecimalToUint64 (Walker, &RetVal, StopAtSpace);\r
252d9457 4228 }\r
4229\r
47d20b54 4230 if ((Value == NULL) && !EFI_ERROR (Status)) {\r
252d9457 4231 return (EFI_NOT_FOUND);\r
4232 }\r
4233\r
4234 if (Value != NULL) {\r
4235 *Value = RetVal;\r
4236 }\r
4237\r
4238 return (Status);\r
4239}\r
4240\r
4241/**\r
4242 Function to determin if an entire string is a valid number.\r
4243\r
4244 If Hex it must be preceeded with a 0x or has ForceHex, set TRUE.\r
4245\r
4246 @param[in] String The string to evaluate.\r
4247 @param[in] ForceHex TRUE - always assume hex.\r
4248 @param[in] StopAtSpace TRUE to halt upon finding a space, FALSE to keep going.\r
4249\r
4250 @retval TRUE It is all numeric (dec/hex) characters.\r
4251 @retval FALSE There is a non-numeric character.\r
4252**/\r
4253BOOLEAN\r
4254EFIAPI\r
4255ShellIsHexOrDecimalNumber (\r
4256 IN CONST CHAR16 *String,\r
4257 IN CONST BOOLEAN ForceHex,\r
4258 IN CONST BOOLEAN StopAtSpace\r
4259 )\r
4260{\r
47d20b54 4261 if (ShellConvertStringToUint64 (String, NULL, ForceHex, StopAtSpace) == EFI_NOT_FOUND) {\r
252d9457 4262 return (TRUE);\r
4263 }\r
47d20b54 4264\r
252d9457 4265 return (FALSE);\r
4266}\r
4d0a4fce 4267\r
4268/**\r
4269 Function to read a single line from a SHELL_FILE_HANDLE. The \n is not included in the returned\r
4270 buffer. The returned buffer must be callee freed.\r
4271\r
4272 If the position upon start is 0, then the Ascii Boolean will be set. This should be\r
4273 maintained and not changed for all operations with the same file.\r
4274\r
4ff7e37b
ED
4275 @param[in] Handle SHELL_FILE_HANDLE to read from.\r
4276 @param[in, out] Ascii Boolean value for indicating whether the file is\r
4277 Ascii (TRUE) or UCS2 (FALSE).\r
4d0a4fce 4278\r
beab0fc5 4279 @return The line of text from the file.\r
4280 @retval NULL There was not enough memory available.\r
4d0a4fce 4281\r
4282 @sa ShellFileHandleReadLine\r
4283**/\r
47d20b54 4284CHAR16 *\r
4d0a4fce 4285EFIAPI\r
47d20b54
MK
4286ShellFileHandleReturnLine (\r
4287 IN SHELL_FILE_HANDLE Handle,\r
4288 IN OUT BOOLEAN *Ascii\r
4d0a4fce 4289 )\r
4290{\r
47d20b54
MK
4291 CHAR16 *RetVal;\r
4292 UINTN Size;\r
4293 EFI_STATUS Status;\r
4d0a4fce 4294\r
47d20b54 4295 Size = 0;\r
4d0a4fce 4296 RetVal = NULL;\r
4297\r
47d20b54 4298 Status = ShellFileHandleReadLine (Handle, RetVal, &Size, FALSE, Ascii);\r
4d0a4fce 4299 if (Status == EFI_BUFFER_TOO_SMALL) {\r
47d20b54 4300 RetVal = AllocateZeroPool (Size);\r
beab0fc5 4301 if (RetVal == NULL) {\r
4302 return (NULL);\r
4303 }\r
b0934ac4 4304\r
47d20b54 4305 Status = ShellFileHandleReadLine (Handle, RetVal, &Size, FALSE, Ascii);\r
4d0a4fce 4306 }\r
47d20b54
MK
4307\r
4308 if ((Status == EFI_END_OF_FILE) && (RetVal != NULL) && (*RetVal != CHAR_NULL)) {\r
f79d7b62
QS
4309 Status = EFI_SUCCESS;\r
4310 }\r
47d20b54
MK
4311\r
4312 if (EFI_ERROR (Status) && (RetVal != NULL)) {\r
4313 FreePool (RetVal);\r
4d0a4fce 4314 RetVal = NULL;\r
4315 }\r
47d20b54 4316\r
4d0a4fce 4317 return (RetVal);\r
4318}\r
4319\r
4320/**\r
4321 Function to read a single line (up to but not including the \n) from a SHELL_FILE_HANDLE.\r
4322\r
4323 If the position upon start is 0, then the Ascii Boolean will be set. This should be\r
4324 maintained and not changed for all operations with the same file.\r
4325\r
2dda8a12
JD
4326 NOTE: LINES THAT ARE RETURNED BY THIS FUNCTION ARE UCS2, EVEN IF THE FILE BEING READ\r
4327 IS IN ASCII FORMAT.\r
4328\r
4ff7e37b 4329 @param[in] Handle SHELL_FILE_HANDLE to read from.\r
2dda8a12
JD
4330 @param[in, out] Buffer The pointer to buffer to read into. If this function\r
4331 returns EFI_SUCCESS, then on output Buffer will\r
4332 contain a UCS2 string, even if the file being\r
4333 read is ASCII.\r
4334 @param[in, out] Size On input, pointer to number of bytes in Buffer.\r
4335 On output, unchanged unless Buffer is too small\r
4336 to contain the next line of the file. In that\r
4337 case Size is set to the number of bytes needed\r
4338 to hold the next line of the file (as a UCS2\r
4339 string, even if it is an ASCII file).\r
4ff7e37b
ED
4340 @param[in] Truncate If the buffer is large enough, this has no effect.\r
4341 If the buffer is is too small and Truncate is TRUE,\r
4342 the line will be truncated.\r
4343 If the buffer is is too small and Truncate is FALSE,\r
4344 then no read will occur.\r
4345\r
4346 @param[in, out] Ascii Boolean value for indicating whether the file is\r
4347 Ascii (TRUE) or UCS2 (FALSE).\r
4d0a4fce 4348\r
4349 @retval EFI_SUCCESS The operation was successful. The line is stored in\r
4350 Buffer.\r
9ed21946 4351 @retval EFI_END_OF_FILE There are no more lines in the file.\r
4d0a4fce 4352 @retval EFI_INVALID_PARAMETER Handle was NULL.\r
4353 @retval EFI_INVALID_PARAMETER Size was NULL.\r
4354 @retval EFI_BUFFER_TOO_SMALL Size was not large enough to store the line.\r
4355 Size was updated to the minimum space required.\r
4356**/\r
4357EFI_STATUS\r
4358EFIAPI\r
47d20b54
MK
4359ShellFileHandleReadLine (\r
4360 IN SHELL_FILE_HANDLE Handle,\r
4361 IN OUT CHAR16 *Buffer,\r
4362 IN OUT UINTN *Size,\r
4363 IN BOOLEAN Truncate,\r
4364 IN OUT BOOLEAN *Ascii\r
4d0a4fce 4365 )\r
4366{\r
4367 EFI_STATUS Status;\r
4368 CHAR16 CharBuffer;\r
4369 UINTN CharSize;\r
4370 UINTN CountSoFar;\r
4371 UINT64 OriginalFilePosition;\r
4372\r
47d20b54
MK
4373 if ( (Handle == NULL)\r
4374 || (Size == NULL)\r
4375 )\r
4376 {\r
4d0a4fce 4377 return (EFI_INVALID_PARAMETER);\r
4378 }\r
47d20b54 4379\r
4d0a4fce 4380 if (Buffer == NULL) {\r
47d20b54 4381 ASSERT (*Size == 0);\r
4d0a4fce 4382 } else {\r
4383 *Buffer = CHAR_NULL;\r
4384 }\r
47d20b54
MK
4385\r
4386 gEfiShellProtocol->GetFilePosition (Handle, &OriginalFilePosition);\r
4d0a4fce 4387 if (OriginalFilePosition == 0) {\r
47d20b54
MK
4388 CharSize = sizeof (CHAR16);\r
4389 Status = gEfiShellProtocol->ReadFile (Handle, &CharSize, &CharBuffer);\r
4390 ASSERT_EFI_ERROR (Status);\r
4d0a4fce 4391 if (CharBuffer == gUnicodeFileTag) {\r
4392 *Ascii = FALSE;\r
4393 } else {\r
4394 *Ascii = TRUE;\r
47d20b54 4395 gEfiShellProtocol->SetFilePosition (Handle, OriginalFilePosition);\r
4d0a4fce 4396 }\r
4397 }\r
4398\r
9ed21946 4399 if (*Ascii) {\r
47d20b54 4400 CharSize = sizeof (CHAR8);\r
9ed21946 4401 } else {\r
47d20b54 4402 CharSize = sizeof (CHAR16);\r
9ed21946 4403 }\r
47d20b54
MK
4404\r
4405 for (CountSoFar = 0; ; CountSoFar++) {\r
4d0a4fce 4406 CharBuffer = 0;\r
47d20b54
MK
4407 Status = gEfiShellProtocol->ReadFile (Handle, &CharSize, &CharBuffer);\r
4408 if ( EFI_ERROR (Status)\r
4409 || (CharSize == 0)\r
4410 || ((CharBuffer == L'\n') && !(*Ascii))\r
4411 || ((CharBuffer == '\n') && *Ascii)\r
4412 )\r
4413 {\r
9ed21946 4414 if (CharSize == 0) {\r
4415 Status = EFI_END_OF_FILE;\r
4416 }\r
47d20b54 4417\r
4d0a4fce 4418 break;\r
4419 }\r
47d20b54 4420\r
4d0a4fce 4421 //\r
4422 // if we have space save it...\r
4423 //\r
47d20b54
MK
4424 if ((CountSoFar+1)*sizeof (CHAR16) < *Size) {\r
4425 ASSERT (Buffer != NULL);\r
4426 ((CHAR16 *)Buffer)[CountSoFar] = CharBuffer;\r
4427 ((CHAR16 *)Buffer)[CountSoFar+1] = CHAR_NULL;\r
4d0a4fce 4428 }\r
4429 }\r
4430\r
4431 //\r
4432 // if we ran out of space tell when...\r
4433 //\r
47d20b54
MK
4434 if ((CountSoFar+1)*sizeof (CHAR16) > *Size) {\r
4435 *Size = (CountSoFar+1)*sizeof (CHAR16);\r
1095b432 4436 if (!Truncate) {\r
47d20b54 4437 gEfiShellProtocol->SetFilePosition (Handle, OriginalFilePosition);\r
1095b432 4438 } else {\r
47d20b54 4439 DEBUG ((DEBUG_WARN, "The line was truncated in ShellFileHandleReadLine"));\r
4d0a4fce 4440 }\r
47d20b54 4441\r
1095b432
JD
4442 return (EFI_BUFFER_TOO_SMALL);\r
4443 }\r
47d20b54
MK
4444\r
4445 while (Buffer[StrLen (Buffer)-1] == L'\r') {\r
4446 Buffer[StrLen (Buffer)-1] = CHAR_NULL;\r
4d0a4fce 4447 }\r
4448\r
4449 return (Status);\r
4450}\r
fb5278ef 4451\r
365aa98a 4452/**\r
4453 Function to print help file / man page content in the spec from the UEFI Shell protocol GetHelpText function.\r
4454\r
4455 @param[in] CommandToGetHelpOn Pointer to a string containing the command name of help file to be printed.\r
4456 @param[in] SectionToGetHelpOn Pointer to the section specifier(s).\r
ba0014b9 4457 @param[in] PrintCommandText If TRUE, prints the command followed by the help content, otherwise prints\r
365aa98a 4458 the help content only.\r
4459 @retval EFI_DEVICE_ERROR The help data format was incorrect.\r
4460 @retval EFI_NOT_FOUND The help data could not be found.\r
4461 @retval EFI_SUCCESS The operation was successful.\r
4462**/\r
4463EFI_STATUS\r
4464EFIAPI\r
4465ShellPrintHelp (\r
47d20b54
MK
4466 IN CONST CHAR16 *CommandToGetHelpOn,\r
4467 IN CONST CHAR16 *SectionToGetHelpOn,\r
4468 IN BOOLEAN PrintCommandText\r
365aa98a 4469 )\r
4470{\r
47d20b54
MK
4471 EFI_STATUS Status;\r
4472 CHAR16 *OutText;\r
ba0014b9
LG
4473\r
4474 OutText = NULL;\r
4475\r
365aa98a 4476 //\r
4477 // Get the string to print based\r
4478 //\r
ba0014b9
LG
4479 Status = gEfiShellProtocol->GetHelpText (CommandToGetHelpOn, SectionToGetHelpOn, &OutText);\r
4480\r
365aa98a 4481 //\r
4482 // make sure we got a valid string\r
4483 //\r
47d20b54 4484 if (EFI_ERROR (Status)) {\r
365aa98a 4485 return Status;\r
ba0014b9 4486 }\r
47d20b54
MK
4487\r
4488 if ((OutText == NULL) || (StrLen (OutText) == 0)) {\r
ba0014b9
LG
4489 return EFI_NOT_FOUND;\r
4490 }\r
4491\r
365aa98a 4492 //\r
4493 // Chop off trailing stuff we dont need\r
4494 //\r
47d20b54
MK
4495 while (OutText[StrLen (OutText)-1] == L'\r' || OutText[StrLen (OutText)-1] == L'\n' || OutText[StrLen (OutText)-1] == L' ') {\r
4496 OutText[StrLen (OutText)-1] = CHAR_NULL;\r
365aa98a 4497 }\r
ba0014b9 4498\r
365aa98a 4499 //\r
4500 // Print this out to the console\r
4501 //\r
4502 if (PrintCommandText) {\r
47d20b54 4503 ShellPrintEx (-1, -1, L"%H%-14s%N- %s\r\n", CommandToGetHelpOn, OutText);\r
365aa98a 4504 } else {\r
47d20b54 4505 ShellPrintEx (-1, -1, L"%N%s\r\n", OutText);\r
365aa98a 4506 }\r
ba0014b9 4507\r
47d20b54 4508 SHELL_FREE_NON_NULL (OutText);\r
365aa98a 4509\r
ba0014b9 4510 return EFI_SUCCESS;\r
365aa98a 4511}\r
4512\r
fb5278ef 4513/**\r
4514 Function to delete a file by name\r
ba0014b9 4515\r
fb5278ef 4516 @param[in] FileName Pointer to file name to delete.\r
ba0014b9 4517\r
fb5278ef 4518 @retval EFI_SUCCESS the file was deleted sucessfully\r
4519 @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not\r
4520 deleted\r
4521 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.\r
4522 @retval EFI_NOT_FOUND The specified file could not be found on the\r
4523 device or the file system could not be found\r
4524 on the device.\r
4525 @retval EFI_NO_MEDIA The device has no medium.\r
4526 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the\r
4527 medium is no longer supported.\r
4528 @retval EFI_DEVICE_ERROR The device reported an error.\r
4529 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
4530 @retval EFI_WRITE_PROTECTED The file or medium is write protected.\r
4531 @retval EFI_ACCESS_DENIED The file was opened read only.\r
4532 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the\r
4533 file.\r
4534 @retval other The file failed to open\r
4535**/\r
4536EFI_STATUS\r
4537EFIAPI\r
47d20b54
MK
4538ShellDeleteFileByName (\r
4539 IN CONST CHAR16 *FileName\r
fb5278ef 4540 )\r
4541{\r
47d20b54
MK
4542 EFI_STATUS Status;\r
4543 SHELL_FILE_HANDLE FileHandle;\r
ba0014b9 4544\r
47d20b54 4545 Status = ShellFileExists (FileName);\r
ba0014b9 4546\r
47d20b54
MK
4547 if (Status == EFI_SUCCESS) {\r
4548 Status = ShellOpenFileByName (FileName, &FileHandle, EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE, 0x0);\r
4549 if (Status == EFI_SUCCESS) {\r
4550 Status = ShellDeleteFile (&FileHandle);\r
fb5278ef 4551 }\r
ba0014b9 4552 }\r
fb5278ef 4553\r
47d20b54 4554 return (Status);\r
fb5278ef 4555}\r
0960ba17
QS
4556\r
4557/**\r
4558 Cleans off all the quotes in the string.\r
4559\r
4560 @param[in] OriginalString pointer to the string to be cleaned.\r
ba0014b9
LG
4561 @param[out] CleanString The new string with all quotes removed.\r
4562 Memory allocated in the function and free\r
0960ba17
QS
4563 by caller.\r
4564\r
4565 @retval EFI_SUCCESS The operation was successful.\r
4566**/\r
4567EFI_STATUS\r
0960ba17 4568InternalShellStripQuotes (\r
47d20b54
MK
4569 IN CONST CHAR16 *OriginalString,\r
4570 OUT CHAR16 **CleanString\r
0960ba17
QS
4571 )\r
4572{\r
47d20b54 4573 CHAR16 *Walker;\r
ba0014b9 4574\r
47d20b54 4575 if ((OriginalString == NULL) || (CleanString == NULL)) {\r
0960ba17
QS
4576 return EFI_INVALID_PARAMETER;\r
4577 }\r
4578\r
4579 *CleanString = AllocateCopyPool (StrSize (OriginalString), OriginalString);\r
4580 if (*CleanString == NULL) {\r
4581 return EFI_OUT_OF_RESOURCES;\r
4582 }\r
4583\r
47d20b54 4584 for (Walker = *CleanString; Walker != NULL && *Walker != CHAR_NULL; Walker++) {\r
0960ba17 4585 if (*Walker == L'\"') {\r
47d20b54 4586 CopyMem (Walker, Walker+1, StrSize (Walker) - sizeof (Walker[0]));\r
0960ba17
QS
4587 }\r
4588 }\r
4589\r
4590 return EFI_SUCCESS;\r
4591}\r