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