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