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