]> git.proxmox.com Git - mirror_edk2.git/blame - ShellPkg/Library/UefiShellLib/UefiShellLib.c
clean up comments
[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
a31bd33c 4Copyright (c) 2006 - 2009, Intel Corporation<BR>\r
94b17fa1 5All rights reserved. This program and the accompanying materials\r
6are licensed and made available under the terms and conditions of the BSD License\r
7which accompanies this distribution. The full text of the license may be found at\r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include <Uefi.h>\r
16#include <Library/ShellLib.h>\r
17#include <Library/UefiBootServicesTableLib.h>\r
18#include <Library/BaseLib.h>\r
19#include <Library/BaseMemoryLib.h>\r
20#include <Library/DebugLib.h>\r
21#include <Library/MemoryAllocationLib.h>\r
22#include <Library/DevicePathLib.h>\r
d2b4564b 23#include <Library/PcdLib.h>\r
24#include <Library/FileHandleLib.h>\r
b1f95a06 25#include <Library/PrintLib.h>\r
26#include <Library/UefiLib.h>\r
2247dde4 27#include <Library/HiiLib.h>\r
b1f95a06 28\r
94b17fa1 29#include <Protocol/EfiShellEnvironment2.h>\r
30#include <Protocol/EfiShellInterface.h>\r
31#include <Protocol/EfiShell.h>\r
32#include <Protocol/EfiShellParameters.h>\r
33#include <Protocol/SimpleFileSystem.h>\r
34\r
b1f95a06 35#include "UefiShellLib.h"\r
d2b4564b 36\r
94b17fa1 37#define MAX_FILE_NAME_LEN 522 // (20 * (6+5+2))+1) unicode characters from EFI FAT spec (doubled for bytes)\r
38#define FIND_XXXXX_FILE_BUFFER_SIZE (SIZE_OF_EFI_FILE_INFO + MAX_FILE_NAME_LEN)\r
39\r
d2b4564b 40//\r
41// This is not static since it's extern in the .h file\r
42//\r
43SHELL_PARAM_ITEM EmptyParamList[] = {\r
44 {NULL, TypeMax}\r
45 };\r
46\r
47//\r
48// Static file globals for the shell library\r
49//\r
50STATIC EFI_SHELL_ENVIRONMENT2 *mEfiShellEnvironment2;\r
51STATIC EFI_SHELL_INTERFACE *mEfiShellInterface;\r
52STATIC EFI_SHELL_PROTOCOL *mEfiShellProtocol;\r
53STATIC EFI_SHELL_PARAMETERS_PROTOCOL *mEfiShellParametersProtocol;\r
54STATIC EFI_HANDLE mEfiShellEnvironment2Handle;\r
55STATIC FILE_HANDLE_FUNCTION_MAP FileFunctionMap;\r
2247dde4 56STATIC UINTN mTotalParameterCount;\r
57\r
58/**\r
59 Check if a Unicode character is a hexadecimal character.\r
60\r
61 This internal function checks if a Unicode character is a \r
62 decimal character. The valid hexadecimal character is \r
63 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.\r
64\r
65\r
66 @param Char The character to check against.\r
67\r
68 @retval TRUE If the Char is a hexadecmial character.\r
69 @retval FALSE If the Char is not a hexadecmial character.\r
70\r
71**/\r
72BOOLEAN\r
73EFIAPI\r
74ShellInternalIsHexaDecimalDigitCharacter (\r
75 IN CHAR16 Char\r
76 ) {\r
77 return (BOOLEAN) ((Char >= L'0' && Char <= L'9') || (Char >= L'A' && Char <= L'F') || (Char >= L'a' && Char <= L'f'));\r
78}\r
94b17fa1 79\r
80/**\r
81 helper function to find ShellEnvironment2 for constructor\r
82**/\r
83EFI_STATUS\r
84EFIAPI\r
85ShellFindSE2 (\r
86 IN EFI_HANDLE ImageHandle\r
2247dde4 87 ) {\r
94b17fa1 88 EFI_STATUS Status;\r
89 EFI_HANDLE *Buffer;\r
90 UINTN BufferSize;\r
91 UINTN HandleIndex;\r
92\r
93 BufferSize = 0;\r
94 Buffer = NULL;\r
95 Status = gBS->OpenProtocol(ImageHandle, \r
96 &gEfiShellEnvironment2Guid,\r
97 (VOID **)&mEfiShellEnvironment2,\r
98 ImageHandle,\r
99 NULL,\r
100 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
101 );\r
102 //\r
103 // look for the mEfiShellEnvironment2 protocol at a higher level\r
104 //\r
9eb53ac3 105 if (EFI_ERROR (Status) || !(CompareGuid (&mEfiShellEnvironment2->SESGuid, &gEfiShellEnvironment2ExtGuid) != FALSE)){\r
94b17fa1 106 //\r
107 // figure out how big of a buffer we need.\r
108 //\r
109 Status = gBS->LocateHandle (ByProtocol,\r
110 &gEfiShellEnvironment2Guid,\r
111 NULL, // ignored for ByProtocol\r
112 &BufferSize,\r
113 Buffer\r
114 );\r
2247dde4 115 //\r
116 // maybe it's not there???\r
117 //\r
118 if (Status == EFI_BUFFER_TOO_SMALL) {\r
119 Buffer = (EFI_HANDLE*)AllocatePool(BufferSize);\r
120 ASSERT(Buffer != NULL);\r
121 Status = gBS->LocateHandle (ByProtocol,\r
122 &gEfiShellEnvironment2Guid,\r
123 NULL, // ignored for ByProtocol\r
124 &BufferSize,\r
125 Buffer\r
126 );\r
127 }\r
94b17fa1 128 if (!EFI_ERROR (Status)) {\r
129 //\r
130 // now parse the list of returned handles\r
131 //\r
132 Status = EFI_NOT_FOUND;\r
133 for (HandleIndex = 0; HandleIndex < (BufferSize/sizeof(Buffer[0])); HandleIndex++) {\r
134 Status = gBS->OpenProtocol(Buffer[HandleIndex], \r
135 &gEfiShellEnvironment2Guid,\r
136 (VOID **)&mEfiShellEnvironment2,\r
137 ImageHandle,\r
138 NULL,\r
139 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
140 );\r
9eb53ac3 141 if (CompareGuid (&mEfiShellEnvironment2->SESGuid, &gEfiShellEnvironment2ExtGuid) != FALSE) {\r
94b17fa1 142 mEfiShellEnvironment2Handle = Buffer[HandleIndex];\r
143 Status = EFI_SUCCESS;\r
144 break;\r
145 }\r
146 }\r
147 }\r
148 }\r
149 if (Buffer != NULL) {\r
150 FreePool (Buffer);\r
151 }\r
152 return (Status);\r
153}\r
154\r
94b17fa1 155EFI_STATUS\r
156EFIAPI\r
d2b4564b 157ShellLibConstructorWorker (\r
94b17fa1 158 IN EFI_HANDLE ImageHandle,\r
159 IN EFI_SYSTEM_TABLE *SystemTable\r
2247dde4 160 ) {\r
94b17fa1 161 EFI_STATUS Status;\r
162\r
2247dde4 163 //\r
164 // Set the parameter count to an invalid number\r
165 //\r
166 mTotalParameterCount = (UINTN)(-1);\r
167\r
94b17fa1 168 //\r
169 // UEFI 2.0 shell interfaces (used preferentially)\r
170 //\r
171 Status = gBS->OpenProtocol(ImageHandle, \r
172 &gEfiShellProtocolGuid,\r
173 (VOID **)&mEfiShellProtocol,\r
174 ImageHandle,\r
175 NULL,\r
176 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
177 );\r
178 if (EFI_ERROR(Status)) {\r
179 mEfiShellProtocol = NULL;\r
180 }\r
181 Status = gBS->OpenProtocol(ImageHandle, \r
182 &gEfiShellParametersProtocolGuid,\r
183 (VOID **)&mEfiShellParametersProtocol,\r
184 ImageHandle,\r
185 NULL,\r
186 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
187 );\r
188 if (EFI_ERROR(Status)) {\r
189 mEfiShellParametersProtocol = NULL;\r
190 }\r
191\r
192 if (mEfiShellParametersProtocol == NULL || mEfiShellProtocol == NULL) {\r
193 //\r
194 // Moved to seperate function due to complexity\r
195 //\r
196 Status = ShellFindSE2(ImageHandle);\r
197\r
198 if (EFI_ERROR(Status)) {\r
199 DEBUG((DEBUG_ERROR, "Status: 0x%08x\r\n", Status));\r
200 mEfiShellEnvironment2 = NULL;\r
201 }\r
202 Status = gBS->OpenProtocol(ImageHandle, \r
203 &gEfiShellInterfaceGuid,\r
204 (VOID **)&mEfiShellInterface,\r
205 ImageHandle,\r
206 NULL,\r
207 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
208 );\r
209 if (EFI_ERROR(Status)) {\r
210 mEfiShellInterface = NULL;\r
211 }\r
212 }\r
213 //\r
214 // only success getting 2 of either the old or new, but no 1/2 and 1/2\r
215 //\r
216 if ((mEfiShellEnvironment2 != NULL && mEfiShellInterface != NULL) || \r
217 (mEfiShellProtocol != NULL && mEfiShellParametersProtocol != NULL) ) {\r
d2b4564b 218 if (mEfiShellProtocol != NULL) {\r
219 FileFunctionMap.GetFileInfo = mEfiShellProtocol->GetFileInfo;\r
220 FileFunctionMap.SetFileInfo = mEfiShellProtocol->SetFileInfo;\r
221 FileFunctionMap.ReadFile = mEfiShellProtocol->ReadFile;\r
222 FileFunctionMap.WriteFile = mEfiShellProtocol->WriteFile;\r
223 FileFunctionMap.CloseFile = mEfiShellProtocol->CloseFile;\r
224 FileFunctionMap.DeleteFile = mEfiShellProtocol->DeleteFile;\r
225 FileFunctionMap.GetFilePosition = mEfiShellProtocol->GetFilePosition;\r
226 FileFunctionMap.SetFilePosition = mEfiShellProtocol->SetFilePosition;\r
227 FileFunctionMap.FlushFile = mEfiShellProtocol->FlushFile;\r
228 FileFunctionMap.GetFileSize = mEfiShellProtocol->GetFileSize;\r
229 } else {\r
230 FileFunctionMap.GetFileInfo = FileHandleGetInfo;\r
231 FileFunctionMap.SetFileInfo = FileHandleSetInfo;\r
232 FileFunctionMap.ReadFile = FileHandleRead;\r
233 FileFunctionMap.WriteFile = FileHandleWrite;\r
234 FileFunctionMap.CloseFile = FileHandleClose;\r
235 FileFunctionMap.DeleteFile = FileHandleDelete;\r
236 FileFunctionMap.GetFilePosition = FileHandleGetPosition;\r
237 FileFunctionMap.SetFilePosition = FileHandleSetPosition;\r
238 FileFunctionMap.FlushFile = FileHandleFlush;\r
239 FileFunctionMap.GetFileSize = FileHandleGetSize;\r
240 }\r
94b17fa1 241 return (EFI_SUCCESS);\r
242 }\r
243 return (EFI_NOT_FOUND);\r
244}\r
d2b4564b 245/**\r
246 Constructor for the Shell library.\r
247\r
248 Initialize the library and determine if the underlying is a UEFI Shell 2.0 or an EFI shell.\r
249\r
250 @param ImageHandle the image handle of the process\r
251 @param SystemTable the EFI System Table pointer\r
252\r
253 @retval EFI_SUCCESS the initialization was complete sucessfully\r
254 @return others an error ocurred during initialization\r
255**/\r
256EFI_STATUS\r
257EFIAPI\r
258ShellLibConstructor (\r
259 IN EFI_HANDLE ImageHandle,\r
260 IN EFI_SYSTEM_TABLE *SystemTable\r
2247dde4 261 ) {\r
d2b4564b 262\r
263\r
264 mEfiShellEnvironment2 = NULL;\r
265 mEfiShellProtocol = NULL;\r
266 mEfiShellParametersProtocol = NULL;\r
267 mEfiShellInterface = NULL;\r
268 mEfiShellEnvironment2Handle = NULL;\r
269\r
d2b4564b 270 //\r
271 // verify that auto initialize is not set false\r
272 // \r
273 if (PcdGetBool(PcdShellLibAutoInitialize) == 0) {\r
274 return (EFI_SUCCESS);\r
275 }\r
276 \r
277 return (ShellLibConstructorWorker(ImageHandle, SystemTable));\r
278}\r
94b17fa1 279\r
280/**\r
281 Destructory for the library. free any resources.\r
282**/\r
283EFI_STATUS\r
284EFIAPI\r
285ShellLibDestructor (\r
286 IN EFI_HANDLE ImageHandle,\r
287 IN EFI_SYSTEM_TABLE *SystemTable\r
2247dde4 288 ) {\r
94b17fa1 289 if (mEfiShellEnvironment2 != NULL) {\r
290 gBS->CloseProtocol(mEfiShellEnvironment2Handle==NULL?ImageHandle:mEfiShellEnvironment2Handle,\r
291 &gEfiShellEnvironment2Guid,\r
292 ImageHandle,\r
293 NULL);\r
d2b4564b 294 mEfiShellEnvironment2 = NULL;\r
94b17fa1 295 }\r
296 if (mEfiShellInterface != NULL) {\r
297 gBS->CloseProtocol(ImageHandle,\r
298 &gEfiShellInterfaceGuid,\r
299 ImageHandle,\r
300 NULL); \r
d2b4564b 301 mEfiShellInterface = NULL;\r
94b17fa1 302 }\r
303 if (mEfiShellProtocol != NULL) {\r
304 gBS->CloseProtocol(ImageHandle,\r
305 &gEfiShellProtocolGuid,\r
306 ImageHandle,\r
d2b4564b 307 NULL); \r
308 mEfiShellProtocol = NULL;\r
94b17fa1 309 }\r
310 if (mEfiShellParametersProtocol != NULL) {\r
311 gBS->CloseProtocol(ImageHandle,\r
312 &gEfiShellParametersProtocolGuid,\r
313 ImageHandle,\r
314 NULL); \r
d2b4564b 315 mEfiShellParametersProtocol = NULL;\r
94b17fa1 316 }\r
d2b4564b 317 mEfiShellEnvironment2Handle = NULL;\r
94b17fa1 318 return (EFI_SUCCESS);\r
319}\r
d2b4564b 320\r
321/**\r
322 This function causes the shell library to initialize itself. If the shell library\r
323 is already initialized it will de-initialize all the current protocol poitners and\r
324 re-populate them again.\r
325\r
326 When the library is used with PcdShellLibAutoInitialize set to true this function\r
327 will return EFI_SUCCESS and perform no actions.\r
328\r
329 This function is intended for internal access for shell commands only.\r
330\r
331 @retval EFI_SUCCESS the initialization was complete sucessfully\r
332\r
333**/\r
334EFI_STATUS\r
335EFIAPI\r
336ShellInitialize (\r
337 ) {\r
338 //\r
339 // if auto initialize is not false then skip\r
340 //\r
341 if (PcdGetBool(PcdShellLibAutoInitialize) != 0) {\r
342 return (EFI_SUCCESS);\r
343 }\r
344\r
345 //\r
346 // deinit the current stuff\r
347 //\r
348 ASSERT_EFI_ERROR(ShellLibDestructor(gImageHandle, gST));\r
349\r
350 //\r
351 // init the new stuff\r
352 //\r
353 return (ShellLibConstructorWorker(gImageHandle, gST));\r
354}\r
355\r
94b17fa1 356/**\r
357 This function will retrieve the information about the file for the handle \r
358 specified and store it in allocated pool memory.\r
359\r
69817bf8 360 This function allocates a buffer to store the file's information. It is the \r
361 caller's responsibility to free the buffer\r
94b17fa1 362\r
363 @param FileHandle The file handle of the file for which information is \r
364 being requested.\r
365\r
366 @retval NULL information could not be retrieved.\r
367\r
368 @return the information about the file\r
369**/\r
370EFI_FILE_INFO*\r
371EFIAPI\r
372ShellGetFileInfo (\r
373 IN EFI_FILE_HANDLE FileHandle\r
2247dde4 374 ) {\r
d2b4564b 375 return (FileFunctionMap.GetFileInfo(FileHandle));\r
94b17fa1 376}\r
377\r
378/**\r
379 This function will set the information about the file for the opened handle \r
380 specified.\r
381\r
382 @param FileHandle The file handle of the file for which information \r
383 is being set\r
384\r
385 @param FileInfo The infotmation to set.\r
386\r
387 @retval EFI_SUCCESS The information was set.\r
388 @retval EFI_UNSUPPORTED The InformationType is not known.\r
389 @retval EFI_NO_MEDIA The device has no medium.\r
390 @retval EFI_DEVICE_ERROR The device reported an error.\r
391 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
392 @retval EFI_WRITE_PROTECTED The file or medium is write protected.\r
393 @retval EFI_ACCESS_DENIED The file was opened read only.\r
394 @retval EFI_VOLUME_FULL The volume is full.\r
395**/\r
396EFI_STATUS\r
397EFIAPI\r
398ShellSetFileInfo (\r
399 IN EFI_FILE_HANDLE FileHandle,\r
400 IN EFI_FILE_INFO *FileInfo\r
2247dde4 401 ) {\r
d2b4564b 402 return (FileFunctionMap.SetFileInfo(FileHandle, FileInfo));\r
94b17fa1 403} \r
404 \r
405 /**\r
406 This function will open a file or directory referenced by DevicePath.\r
407\r
408 This function opens a file with the open mode according to the file path. The \r
409 Attributes is valid only for EFI_FILE_MODE_CREATE.\r
410\r
411 @param FilePath on input the device path to the file. On output \r
412 the remaining device path.\r
413 @param DeviceHandle pointer to the system device handle.\r
414 @param FileHandle pointer to the file handle.\r
415 @param OpenMode the mode to open the file with.\r
416 @param Attributes the file's file attributes.\r
417\r
418 @retval EFI_SUCCESS The information was set.\r
419 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.\r
420 @retval EFI_UNSUPPORTED Could not open the file path. \r
421 @retval EFI_NOT_FOUND The specified file could not be found on the \r
422 device or the file system could not be found on \r
423 the device.\r
424 @retval EFI_NO_MEDIA The device has no medium.\r
425 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the \r
426 medium is no longer supported.\r
427 @retval EFI_DEVICE_ERROR The device reported an error.\r
428 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
429 @retval EFI_WRITE_PROTECTED The file or medium is write protected.\r
430 @retval EFI_ACCESS_DENIED The file was opened read only.\r
431 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the \r
432 file.\r
433 @retval EFI_VOLUME_FULL The volume is full.\r
434**/\r
435EFI_STATUS\r
436EFIAPI\r
437ShellOpenFileByDevicePath(\r
438 IN OUT EFI_DEVICE_PATH_PROTOCOL **FilePath,\r
439 OUT EFI_HANDLE *DeviceHandle,\r
440 OUT EFI_FILE_HANDLE *FileHandle,\r
441 IN UINT64 OpenMode,\r
442 IN UINT64 Attributes\r
2247dde4 443 ) {\r
94b17fa1 444 CHAR16 *FileName;\r
445 EFI_STATUS Status;\r
446 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *EfiSimpleFileSystemProtocol;\r
447 EFI_FILE_HANDLE LastHandle;\r
448\r
449 //\r
450 // ASERT for FileHandle, FilePath, and DeviceHandle being NULL\r
451 //\r
452 ASSERT(FilePath != NULL);\r
453 ASSERT(FileHandle != NULL);\r
454 ASSERT(DeviceHandle != NULL);\r
455 // \r
456 // which shell interface should we use\r
457 //\r
458 if (mEfiShellProtocol != NULL) {\r
459 //\r
460 // use UEFI Shell 2.0 method.\r
461 //\r
462 FileName = mEfiShellProtocol->GetFilePathFromDevicePath(*FilePath);\r
463 if (FileName == NULL) {\r
464 return (EFI_INVALID_PARAMETER);\r
465 }\r
466 Status = ShellOpenFileByName(FileName, FileHandle, OpenMode, Attributes);\r
467 FreePool(FileName);\r
468 return (Status);\r
d2b4564b 469 } \r
470\r
471\r
472 //\r
473 // use old shell method.\r
474 //\r
475 Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid, \r
476 FilePath, \r
477 DeviceHandle);\r
478 if (EFI_ERROR (Status)) {\r
479 return Status;\r
480 }\r
481 Status = gBS->OpenProtocol(*DeviceHandle,\r
482 &gEfiSimpleFileSystemProtocolGuid,\r
b1f95a06 483 (VOID**)&EfiSimpleFileSystemProtocol,\r
d2b4564b 484 gImageHandle,\r
485 NULL,\r
486 EFI_OPEN_PROTOCOL_GET_PROTOCOL);\r
487 if (EFI_ERROR (Status)) {\r
488 return Status;\r
489 }\r
490 Status = EfiSimpleFileSystemProtocol->OpenVolume(EfiSimpleFileSystemProtocol, FileHandle);\r
491 if (EFI_ERROR (Status)) {\r
492 FileHandle = NULL;\r
493 return Status;\r
494 }\r
495\r
496 //\r
497 // go down directories one node at a time.\r
498 //\r
499 while (!IsDevicePathEnd (*FilePath)) {\r
94b17fa1 500 //\r
d2b4564b 501 // For file system access each node should be a file path component\r
94b17fa1 502 //\r
d2b4564b 503 if (DevicePathType (*FilePath) != MEDIA_DEVICE_PATH ||\r
504 DevicePathSubType (*FilePath) != MEDIA_FILEPATH_DP\r
505 ) {\r
94b17fa1 506 FileHandle = NULL;\r
d2b4564b 507 return (EFI_INVALID_PARAMETER);\r
94b17fa1 508 }\r
d2b4564b 509 //\r
510 // Open this file path node\r
511 //\r
512 LastHandle = *FileHandle;\r
513 *FileHandle = NULL;\r
94b17fa1 514\r
515 //\r
d2b4564b 516 // Try to test opening an existing file\r
94b17fa1 517 //\r
d2b4564b 518 Status = LastHandle->Open (\r
519 LastHandle,\r
520 FileHandle,\r
521 ((FILEPATH_DEVICE_PATH*)*FilePath)->PathName,\r
522 OpenMode &~EFI_FILE_MODE_CREATE,\r
523 0\r
524 );\r
94b17fa1 525\r
d2b4564b 526 //\r
527 // see if the error was that it needs to be created\r
528 //\r
529 if ((EFI_ERROR (Status)) && (OpenMode != (OpenMode &~EFI_FILE_MODE_CREATE))) {\r
94b17fa1 530 Status = LastHandle->Open (\r
531 LastHandle,\r
532 FileHandle,\r
533 ((FILEPATH_DEVICE_PATH*)*FilePath)->PathName,\r
d2b4564b 534 OpenMode,\r
535 Attributes\r
94b17fa1 536 );\r
d2b4564b 537 }\r
538 //\r
539 // Close the last node\r
540 //\r
541 LastHandle->Close (LastHandle);\r
94b17fa1 542\r
d2b4564b 543 if (EFI_ERROR(Status)) {\r
544 return (Status);\r
94b17fa1 545 }\r
d2b4564b 546\r
547 //\r
548 // Get the next node\r
549 //\r
550 *FilePath = NextDevicePathNode (*FilePath);\r
94b17fa1 551 }\r
d2b4564b 552 return (EFI_SUCCESS);\r
94b17fa1 553}\r
554\r
555/**\r
556 This function will open a file or directory referenced by filename.\r
557\r
69817bf8 558 If return is EFI_SUCCESS, the Filehandle is the opened file's handle; \r
94b17fa1 559 otherwise, the Filehandle is NULL. The Attributes is valid only for \r
560 EFI_FILE_MODE_CREATE.\r
561\r
562 if FileNAme is NULL then ASSERT()\r
563\r
564 @param FileName pointer to file name\r
565 @param FileHandle pointer to the file handle.\r
566 @param OpenMode the mode to open the file with.\r
567 @param Attributes the file's file attributes.\r
568\r
569 @retval EFI_SUCCESS The information was set.\r
570 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.\r
571 @retval EFI_UNSUPPORTED Could not open the file path. \r
572 @retval EFI_NOT_FOUND The specified file could not be found on the \r
573 device or the file system could not be found \r
574 on the device.\r
575 @retval EFI_NO_MEDIA The device has no medium.\r
576 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the \r
577 medium is no longer supported.\r
578 @retval EFI_DEVICE_ERROR The device reported an error.\r
579 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
580 @retval EFI_WRITE_PROTECTED The file or medium is write protected.\r
581 @retval EFI_ACCESS_DENIED The file was opened read only.\r
582 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the \r
583 file.\r
584 @retval EFI_VOLUME_FULL The volume is full.\r
585**/\r
586EFI_STATUS\r
587EFIAPI\r
588ShellOpenFileByName(\r
b82bfcc1 589 IN CONST CHAR16 *FileName,\r
94b17fa1 590 OUT EFI_FILE_HANDLE *FileHandle,\r
591 IN UINT64 OpenMode,\r
592 IN UINT64 Attributes\r
2247dde4 593 ) {\r
94b17fa1 594 EFI_HANDLE DeviceHandle;\r
595 EFI_DEVICE_PATH_PROTOCOL *FilePath;\r
b1f95a06 596 EFI_STATUS Status;\r
597 EFI_FILE_INFO *FileInfo;\r
94b17fa1 598\r
599 //\r
600 // ASSERT if FileName is NULL\r
601 //\r
602 ASSERT(FileName != NULL);\r
603\r
604 if (mEfiShellProtocol != NULL) {\r
605 //\r
606 // Use UEFI Shell 2.0 method\r
607 //\r
b1f95a06 608 Status = mEfiShellProtocol->OpenFileByName(FileName,\r
609 FileHandle,\r
610 OpenMode);\r
2247dde4 611 if (!EFI_ERROR(Status) && ((OpenMode & EFI_FILE_MODE_CREATE) != 0)){\r
612 FileInfo = FileFunctionMap.GetFileInfo(*FileHandle);\r
b1f95a06 613 ASSERT(FileInfo != NULL);\r
614 FileInfo->Attribute = Attributes;\r
2247dde4 615 Status = FileFunctionMap.SetFileInfo(*FileHandle, FileInfo);\r
616 FreePool(FileInfo);\r
b1f95a06 617 }\r
618 return (Status);\r
94b17fa1 619 } \r
620 //\r
621 // Using EFI Shell version\r
622 // this means convert name to path and call that function\r
623 // since this will use EFI method again that will open it.\r
624 //\r
625 ASSERT(mEfiShellEnvironment2 != NULL);\r
b82bfcc1 626 FilePath = mEfiShellEnvironment2->NameToPath ((CHAR16*)FileName);\r
94b17fa1 627 if (FileDevicePath != NULL) {\r
628 return (ShellOpenFileByDevicePath(&FilePath,\r
629 &DeviceHandle,\r
630 FileHandle,\r
631 OpenMode,\r
632 Attributes ));\r
633 }\r
634 return (EFI_DEVICE_ERROR);\r
635}\r
636/**\r
637 This function create a directory\r
638\r
639 If return is EFI_SUCCESS, the Filehandle is the opened directory's handle; \r
640 otherwise, the Filehandle is NULL. If the directory already existed, this \r
641 function opens the existing directory.\r
642\r
643 @param DirectoryName pointer to directory name\r
644 @param FileHandle pointer to the file handle.\r
645\r
646 @retval EFI_SUCCESS The information was set.\r
647 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.\r
648 @retval EFI_UNSUPPORTED Could not open the file path. \r
649 @retval EFI_NOT_FOUND The specified file could not be found on the \r
650 device or the file system could not be found \r
651 on the device.\r
652 @retval EFI_NO_MEDIA The device has no medium.\r
653 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the \r
654 medium is no longer supported.\r
655 @retval EFI_DEVICE_ERROR The device reported an error.\r
656 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
657 @retval EFI_WRITE_PROTECTED The file or medium is write protected.\r
658 @retval EFI_ACCESS_DENIED The file was opened read only.\r
659 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the \r
660 file.\r
661 @retval EFI_VOLUME_FULL The volume is full.\r
662 @sa ShellOpenFileByName\r
663**/\r
664EFI_STATUS\r
665EFIAPI\r
666ShellCreateDirectory(\r
b82bfcc1 667 IN CONST CHAR16 *DirectoryName,\r
94b17fa1 668 OUT EFI_FILE_HANDLE *FileHandle\r
2247dde4 669 ) {\r
670 if (mEfiShellProtocol != NULL) {\r
671 //\r
672 // Use UEFI Shell 2.0 method\r
673 //\r
674 return (mEfiShellProtocol->CreateFile(DirectoryName,\r
675 EFI_FILE_DIRECTORY,\r
676 FileHandle\r
677 ));\r
678 } else {\r
679 return (ShellOpenFileByName(DirectoryName,\r
680 FileHandle,\r
681 EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE,\r
682 EFI_FILE_DIRECTORY\r
683 ));\r
684 }\r
94b17fa1 685}\r
686\r
687/**\r
688 This function reads information from an opened file.\r
689\r
690 If FileHandle is not a directory, the function reads the requested number of \r
69817bf8 691 bytes from the file at the file's current position and returns them in Buffer. \r
94b17fa1 692 If the read goes beyond the end of the file, the read length is truncated to the\r
69817bf8 693 end of the file. The file's current position is increased by the number of bytes \r
94b17fa1 694 returned. If FileHandle is a directory, the function reads the directory entry \r
69817bf8 695 at the file's current position and returns the entry in Buffer. If the Buffer \r
94b17fa1 696 is not large enough to hold the current directory entry, then \r
697 EFI_BUFFER_TOO_SMALL is returned and the current file position is not updated. \r
698 BufferSize is set to be the size of the buffer needed to read the entry. On \r
699 success, the current position is updated to the next directory entry. If there \r
700 are no more directory entries, the read returns a zero-length buffer. \r
701 EFI_FILE_INFO is the structure returned as the directory entry.\r
702\r
703 @param FileHandle the opened file handle\r
704 @param BufferSize on input the size of buffer in bytes. on return \r
705 the number of bytes written.\r
706 @param Buffer the buffer to put read data into.\r
707\r
708 @retval EFI_SUCCESS Data was read.\r
709 @retval EFI_NO_MEDIA The device has no media.\r
710 @retval EFI_DEVICE_ERROR The device reported an error.\r
711 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
712 @retval EFI_BUFFER_TO_SMALL Buffer is too small. ReadSize contains required \r
713 size.\r
714\r
715**/\r
716EFI_STATUS\r
717EFIAPI\r
718ShellReadFile(\r
719 IN EFI_FILE_HANDLE FileHandle,\r
720 IN OUT UINTN *BufferSize,\r
721 OUT VOID *Buffer\r
2247dde4 722 ) {\r
d2b4564b 723 return (FileFunctionMap.ReadFile(FileHandle, BufferSize, Buffer));\r
94b17fa1 724}\r
725\r
726\r
727/**\r
728 Write data to a file.\r
729\r
730 This function writes the specified number of bytes to the file at the current \r
731 file position. The current file position is advanced the actual number of bytes \r
732 written, which is returned in BufferSize. Partial writes only occur when there \r
69817bf8 733 has been a data error during the write attempt (such as "volume space full"). \r
94b17fa1 734 The file is automatically grown to hold the data if required. Direct writes to \r
735 opened directories are not supported.\r
736\r
737 @param FileHandle The opened file for writing\r
738 @param BufferSize on input the number of bytes in Buffer. On output\r
739 the number of bytes written.\r
740 @param Buffer the buffer containing data to write is stored.\r
741\r
742 @retval EFI_SUCCESS Data was written.\r
743 @retval EFI_UNSUPPORTED Writes to an open directory are not supported.\r
744 @retval EFI_NO_MEDIA The device has no media.\r
745 @retval EFI_DEVICE_ERROR The device reported an error.\r
746 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
747 @retval EFI_WRITE_PROTECTED The device is write-protected.\r
748 @retval EFI_ACCESS_DENIED The file was open for read only.\r
749 @retval EFI_VOLUME_FULL The volume is full.\r
750**/\r
751EFI_STATUS\r
752EFIAPI\r
753ShellWriteFile(\r
754 IN EFI_FILE_HANDLE FileHandle,\r
755 IN OUT UINTN *BufferSize,\r
756 IN VOID *Buffer\r
2247dde4 757 ) {\r
d2b4564b 758 return (FileFunctionMap.WriteFile(FileHandle, BufferSize, Buffer));\r
94b17fa1 759}\r
760\r
761/** \r
762 Close an open file handle.\r
763\r
69817bf8 764 This function closes a specified file handle. All "dirty" cached file data is \r
94b17fa1 765 flushed to the device, and the file is closed. In all cases the handle is \r
766 closed.\r
767\r
768@param FileHandle the file handle to close.\r
769\r
770@retval EFI_SUCCESS the file handle was closed sucessfully.\r
771**/\r
772EFI_STATUS\r
773EFIAPI\r
774ShellCloseFile (\r
775 IN EFI_FILE_HANDLE *FileHandle\r
2247dde4 776 ) {\r
d2b4564b 777 return (FileFunctionMap.CloseFile(*FileHandle));\r
94b17fa1 778}\r
779\r
780/**\r
781 Delete a file and close the handle\r
782\r
783 This function closes and deletes a file. In all cases the file handle is closed.\r
784 If the file cannot be deleted, the warning code EFI_WARN_DELETE_FAILURE is \r
785 returned, but the handle is still closed.\r
786\r
787 @param FileHandle the file handle to delete\r
788\r
789 @retval EFI_SUCCESS the file was closed sucessfully\r
790 @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not \r
791 deleted\r
792 @retval INVALID_PARAMETER One of the parameters has an invalid value.\r
793**/\r
794EFI_STATUS\r
795EFIAPI\r
796ShellDeleteFile (\r
797 IN EFI_FILE_HANDLE *FileHandle\r
2247dde4 798 ) {\r
d2b4564b 799 return (FileFunctionMap.DeleteFile(*FileHandle));\r
94b17fa1 800}\r
801\r
802/**\r
803 Set the current position in a file.\r
804\r
805 This function sets the current file position for the handle to the position \r
806 supplied. With the exception of seeking to position 0xFFFFFFFFFFFFFFFF, only\r
807 absolute positioning is supported, and seeking past the end of the file is \r
808 allowed (a subsequent write would grow the file). Seeking to position \r
809 0xFFFFFFFFFFFFFFFF causes the current position to be set to the end of the file.\r
810 If FileHandle is a directory, the only position that may be set is zero. This \r
811 has the effect of starting the read process of the directory entries over.\r
812\r
813 @param FileHandle The file handle on which the position is being set\r
814 @param Position Byte position from begining of file\r
815\r
816 @retval EFI_SUCCESS Operation completed sucessfully.\r
817 @retval EFI_UNSUPPORTED the seek request for non-zero is not valid on \r
818 directories.\r
819 @retval INVALID_PARAMETER One of the parameters has an invalid value.\r
820**/\r
821EFI_STATUS\r
822EFIAPI\r
823ShellSetFilePosition (\r
824 IN EFI_FILE_HANDLE FileHandle,\r
825 IN UINT64 Position\r
2247dde4 826 ) {\r
d2b4564b 827 return (FileFunctionMap.SetFilePosition(FileHandle, Position));\r
94b17fa1 828}\r
829\r
830/** \r
831 Gets a file's current position\r
832\r
833 This function retrieves the current file position for the file handle. For \r
834 directories, the current file position has no meaning outside of the file \r
835 system driver and as such the operation is not supported. An error is returned\r
836 if FileHandle is a directory.\r
837\r
838 @param FileHandle The open file handle on which to get the position.\r
839 @param Position Byte position from begining of file.\r
840\r
841 @retval EFI_SUCCESS the operation completed sucessfully.\r
842 @retval INVALID_PARAMETER One of the parameters has an invalid value.\r
843 @retval EFI_UNSUPPORTED the request is not valid on directories.\r
844**/\r
845EFI_STATUS\r
846EFIAPI\r
847ShellGetFilePosition (\r
848 IN EFI_FILE_HANDLE FileHandle,\r
849 OUT UINT64 *Position\r
2247dde4 850 ) {\r
d2b4564b 851 return (FileFunctionMap.GetFilePosition(FileHandle, Position));\r
94b17fa1 852}\r
853/**\r
854 Flushes data on a file\r
855 \r
856 This function flushes all modified data associated with a file to a device.\r
857\r
858 @param FileHandle The file handle on which to flush data\r
859\r
860 @retval EFI_SUCCESS The data was flushed.\r
861 @retval EFI_NO_MEDIA The device has no media.\r
862 @retval EFI_DEVICE_ERROR The device reported an error.\r
863 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
864 @retval EFI_WRITE_PROTECTED The file or medium is write protected.\r
865 @retval EFI_ACCESS_DENIED The file was opened for read only.\r
866**/\r
867EFI_STATUS\r
868EFIAPI\r
869ShellFlushFile (\r
870 IN EFI_FILE_HANDLE FileHandle\r
2247dde4 871 ) {\r
d2b4564b 872 return (FileFunctionMap.FlushFile(FileHandle));\r
94b17fa1 873}\r
874\r
875/**\r
876 Retrieves the first file from a directory\r
877\r
69817bf8 878 This function opens a directory and gets the first file's info in the \r
94b17fa1 879 directory. Caller can use ShellFindNextFile() to get other files. When \r
880 complete the caller is responsible for calling FreePool() on Buffer.\r
881\r
882 @param DirHandle The file handle of the directory to search\r
883 @param Buffer Pointer to buffer for file's information\r
884\r
885 @retval EFI_SUCCESS Found the first file.\r
886 @retval EFI_NOT_FOUND Cannot find the directory.\r
887 @retval EFI_NO_MEDIA The device has no media.\r
888 @retval EFI_DEVICE_ERROR The device reported an error.\r
889 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
890 @return Others status of ShellGetFileInfo, ShellSetFilePosition,\r
891 or ShellReadFile\r
892**/\r
893EFI_STATUS\r
894EFIAPI\r
895ShellFindFirstFile (\r
896 IN EFI_FILE_HANDLE DirHandle,\r
d2b4564b 897 OUT EFI_FILE_INFO **Buffer\r
2247dde4 898 ) {\r
94b17fa1 899 //\r
d2b4564b 900 // pass to file handle lib\r
94b17fa1 901 //\r
d2b4564b 902 return (FileHandleFindFirstFile(DirHandle, Buffer));\r
94b17fa1 903}\r
904/**\r
905 Retrieves the next file in a directory.\r
906\r
907 To use this function, caller must call the LibFindFirstFile() to get the \r
908 first file, and then use this function get other files. This function can be \r
909 called for several times to get each file's information in the directory. If \r
910 the call of ShellFindNextFile() got the last file in the directory, the next \r
911 call of this function has no file to get. *NoFile will be set to TRUE and the \r
912 Buffer memory will be automatically freed. \r
913\r
914 @param DirHandle the file handle of the directory\r
915 @param Buffer pointer to buffer for file's information\r
916 @param NoFile pointer to boolean when last file is found\r
917\r
918 @retval EFI_SUCCESS Found the next file, or reached last file\r
919 @retval EFI_NO_MEDIA The device has no media.\r
920 @retval EFI_DEVICE_ERROR The device reported an error.\r
921 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
922**/\r
923EFI_STATUS\r
924EFIAPI\r
925ShellFindNextFile(\r
926 IN EFI_FILE_HANDLE DirHandle,\r
927 OUT EFI_FILE_INFO *Buffer,\r
928 OUT BOOLEAN *NoFile\r
2247dde4 929 ) {\r
94b17fa1 930 //\r
d2b4564b 931 // pass to file handle lib\r
94b17fa1 932 //\r
d2b4564b 933 return (FileHandleFindNextFile(DirHandle, Buffer, NoFile));\r
94b17fa1 934}\r
935/**\r
936 Retrieve the size of a file.\r
937\r
938 if FileHandle is NULL then ASSERT()\r
939 if Size is NULL then ASSERT()\r
940\r
69817bf8 941 This function extracts the file size info from the FileHandle's EFI_FILE_INFO \r
94b17fa1 942 data.\r
943\r
944 @param FileHandle file handle from which size is retrieved\r
945 @param Size pointer to size\r
946\r
947 @retval EFI_SUCCESS operation was completed sucessfully\r
948 @retval EFI_DEVICE_ERROR cannot access the file\r
949**/\r
950EFI_STATUS\r
951EFIAPI\r
952ShellGetFileSize (\r
953 IN EFI_FILE_HANDLE FileHandle,\r
954 OUT UINT64 *Size\r
2247dde4 955 ) {\r
d2b4564b 956 return (FileFunctionMap.GetFileSize(FileHandle, Size));\r
94b17fa1 957}\r
958/**\r
959 Retrieves the status of the break execution flag\r
960\r
961 this function is useful to check whether the application is being asked to halt by the shell.\r
962\r
963 @retval TRUE the execution break is enabled\r
964 @retval FALSE the execution break is not enabled\r
965**/\r
966BOOLEAN\r
967EFIAPI\r
968ShellGetExecutionBreakFlag(\r
969 VOID\r
970 )\r
971{\r
972 // \r
973 // Check for UEFI Shell 2.0 protocols\r
974 //\r
975 if (mEfiShellProtocol != NULL) {\r
976\r
977 //\r
978 // We are using UEFI Shell 2.0; see if the event has been triggered\r
979 //\r
980 if (gBS->CheckEvent(mEfiShellProtocol->ExecutionBreak) != EFI_SUCCESS) {\r
981 return (FALSE);\r
982 }\r
983 return (TRUE);\r
984 } \r
985\r
986 //\r
987 // using EFI Shell; call the function to check\r
988 //\r
989 ASSERT(mEfiShellEnvironment2 != NULL);\r
990 return (mEfiShellEnvironment2->GetExecutionBreak());\r
991}\r
992/**\r
993 return the value of an environment variable\r
994\r
995 this function gets the value of the environment variable set by the \r
996 ShellSetEnvironmentVariable function\r
997\r
998 @param EnvKey The key name of the environment variable.\r
999\r
1000 @retval NULL the named environment variable does not exist.\r
1001 @return != NULL pointer to the value of the environment variable\r
1002**/\r
1003CONST CHAR16*\r
1004EFIAPI\r
1005ShellGetEnvironmentVariable (\r
9b3bf083 1006 IN CONST CHAR16 *EnvKey\r
94b17fa1 1007 )\r
1008{\r
1009 // \r
1010 // Check for UEFI Shell 2.0 protocols\r
1011 //\r
1012 if (mEfiShellProtocol != NULL) {\r
1013 return (mEfiShellProtocol->GetEnv(EnvKey));\r
1014 }\r
1015\r
1016 //\r
1017 // ASSERT that we must have EFI shell\r
1018 //\r
1019 ASSERT(mEfiShellEnvironment2 != NULL);\r
1020\r
1021 //\r
1022 // using EFI Shell\r
1023 //\r
9b3bf083 1024 return (mEfiShellEnvironment2->GetEnv((CHAR16*)EnvKey));\r
94b17fa1 1025}\r
1026/**\r
1027 set the value of an environment variable\r
1028\r
1029This function changes the current value of the specified environment variable. If the\r
1030environment variable exists and the Value is an empty string, then the environment\r
1031variable is deleted. If the environment variable exists and the Value is not an empty\r
1032string, then the value of the environment variable is changed. If the environment\r
1033variable does not exist and the Value is an empty string, there is no action. If the\r
1034environment variable does not exist and the Value is a non-empty string, then the\r
1035environment variable is created and assigned the specified value.\r
1036\r
1037 This is not supported pre-UEFI Shell 2.0.\r
1038\r
1039 @param EnvKey The key name of the environment variable.\r
1040 @param EnvVal The Value of the environment variable\r
1041 @param Volatile Indicates whether the variable is non-volatile (FALSE) or volatile (TRUE).\r
1042\r
1043 @retval EFI_SUCCESS the operation was completed sucessfully\r
1044 @retval EFI_UNSUPPORTED This operation is not allowed in pre UEFI 2.0 Shell environments\r
1045**/\r
1046EFI_STATUS\r
1047EFIAPI\r
1048ShellSetEnvironmentVariable (\r
1049 IN CONST CHAR16 *EnvKey,\r
1050 IN CONST CHAR16 *EnvVal,\r
1051 IN BOOLEAN Volatile\r
1052 )\r
1053{\r
1054 // \r
1055 // Check for UEFI Shell 2.0 protocols\r
1056 //\r
1057 if (mEfiShellProtocol != NULL) {\r
1058 return (mEfiShellProtocol->SetEnv(EnvKey, EnvVal, Volatile));\r
1059 } \r
1060\r
1061 //\r
1062 // This feature does not exist under EFI shell\r
1063 //\r
1064 return (EFI_UNSUPPORTED);\r
1065}\r
1066/**\r
1067 cause the shell to parse and execute a command line.\r
1068\r
1069 This function creates a nested instance of the shell and executes the specified\r
1070command (CommandLine) with the specified environment (Environment). Upon return,\r
1071the status code returned by the specified command is placed in StatusCode.\r
1072If Environment is NULL, then the current environment is used and all changes made\r
1073by the commands executed will be reflected in the current environment. If the\r
1074Environment is non-NULL, then the changes made will be discarded.\r
1075The CommandLine is executed from the current working directory on the current\r
1076device.\r
1077\r
1078EnvironmentVariables and Status are only supported for UEFI Shell 2.0.\r
1079Output is only supported for pre-UEFI Shell 2.0\r
1080\r
1081 @param ImageHandle Parent image that is starting the operation\r
1082 @param CommandLine pointer to null terminated command line.\r
1083 @param Output true to display debug output. false to hide it.\r
1084 @param EnvironmentVariables optional pointer to array of environment variables\r
1085 in the form "x=y". if NULL current set is used.\r
1086 @param Status the status of the run command line.\r
1087\r
1088 @retval EFI_SUCCESS the operation completed sucessfully. Status\r
1089 contains the status code returned.\r
1090 @retval EFI_INVALID_PARAMETER a parameter contains an invalid value\r
1091 @retval EFI_OUT_OF_RESOURCES out of resources\r
1092 @retval EFI_UNSUPPORTED the operation is not allowed.\r
1093**/\r
1094EFI_STATUS\r
1095EFIAPI\r
1096ShellExecute (\r
1097 IN EFI_HANDLE *ParentHandle,\r
1098 IN CHAR16 *CommandLine OPTIONAL,\r
1099 IN BOOLEAN Output OPTIONAL,\r
1100 IN CHAR16 **EnvironmentVariables OPTIONAL,\r
1101 OUT EFI_STATUS *Status OPTIONAL\r
1102 )\r
1103{\r
1104 // \r
1105 // Check for UEFI Shell 2.0 protocols\r
1106 //\r
1107 if (mEfiShellProtocol != NULL) {\r
1108 //\r
1109 // Call UEFI Shell 2.0 version (not using Output parameter)\r
1110 //\r
1111 return (mEfiShellProtocol->Execute(ParentHandle,\r
1112 CommandLine,\r
1113 EnvironmentVariables,\r
1114 Status));\r
1115 } \r
1116 //\r
1117 // ASSERT that we must have EFI shell\r
1118 //\r
1119 ASSERT(mEfiShellEnvironment2 != NULL);\r
1120 //\r
1121 // Call EFI Shell version (not using EnvironmentVariables or Status parameters)\r
1122 // Due to oddity in the EFI shell we want to dereference the ParentHandle here\r
1123 //\r
1124 return (mEfiShellEnvironment2->Execute(*ParentHandle, \r
1125 CommandLine, \r
1126 Output));\r
1127}\r
1128/**\r
1129 Retreives the current directory path\r
1130\r
69817bf8 1131 If the DeviceName is NULL, it returns the current device's current directory \r
94b17fa1 1132 name. If the DeviceName is not NULL, it returns the current directory name \r
1133 on specified drive.\r
1134\r
1135 @param DeviceName the name of the drive to get directory on\r
1136\r
1137 @retval NULL the directory does not exist\r
1138 @return != NULL the directory\r
1139**/\r
1140CONST CHAR16*\r
1141EFIAPI\r
1142ShellGetCurrentDir (\r
1143 IN CHAR16 *DeviceName OPTIONAL\r
1144 )\r
1145{\r
1146 // \r
1147 // Check for UEFI Shell 2.0 protocols\r
1148 //\r
1149 if (mEfiShellProtocol != NULL) {\r
1150 return (mEfiShellProtocol->GetCurDir(DeviceName));\r
1151 } \r
1152 //\r
1153 // ASSERT that we must have EFI shell\r
1154 //\r
1155 ASSERT(mEfiShellEnvironment2 != NULL);\r
1156 return (mEfiShellEnvironment2->CurDir(DeviceName));\r
1157}\r
1158/**\r
1159 sets (enabled or disabled) the page break mode\r
1160\r
1161 when page break mode is enabled the screen will stop scrolling \r
1162 and wait for operator input before scrolling a subsequent screen.\r
1163\r
1164 @param CurrentState TRUE to enable and FALSE to disable\r
1165**/\r
1166VOID \r
1167EFIAPI\r
1168ShellSetPageBreakMode (\r
1169 IN BOOLEAN CurrentState\r
1170 )\r
1171{\r
1172 //\r
1173 // check for enabling\r
1174 //\r
1175 if (CurrentState != 0x00) {\r
1176 // \r
1177 // check for UEFI Shell 2.0\r
1178 //\r
1179 if (mEfiShellProtocol != NULL) {\r
1180 //\r
1181 // Enable with UEFI 2.0 Shell\r
1182 //\r
1183 mEfiShellProtocol->EnablePageBreak();\r
1184 return;\r
1185 } else {\r
1186 // \r
1187 // ASSERT that must have EFI Shell\r
1188 //\r
1189 ASSERT(mEfiShellEnvironment2 != NULL);\r
1190 //\r
1191 // Enable with EFI Shell\r
1192 //\r
1193 mEfiShellEnvironment2->EnablePageBreak (DEFAULT_INIT_ROW, DEFAULT_AUTO_LF);\r
1194 return;\r
1195 }\r
1196 } else {\r
1197 // \r
1198 // check for UEFI Shell 2.0\r
1199 //\r
1200 if (mEfiShellProtocol != NULL) {\r
1201 //\r
1202 // Disable with UEFI 2.0 Shell\r
1203 //\r
1204 mEfiShellProtocol->DisablePageBreak();\r
1205 return;\r
1206 } else {\r
1207 // \r
1208 // ASSERT that must have EFI Shell\r
1209 //\r
1210 ASSERT(mEfiShellEnvironment2 != NULL);\r
1211 //\r
1212 // Disable with EFI Shell\r
1213 //\r
1214 mEfiShellEnvironment2->DisablePageBreak ();\r
1215 return;\r
1216 }\r
1217 }\r
1218}\r
1219\r
1220///\r
1221/// version of EFI_SHELL_FILE_INFO struct, except has no CONST pointers.\r
1222/// This allows for the struct to be populated.\r
1223///\r
1224typedef struct {\r
d2b4564b 1225 LIST_ENTRY Link;\r
94b17fa1 1226 EFI_STATUS Status;\r
1227 CHAR16 *FullName;\r
1228 CHAR16 *FileName;\r
1229 EFI_FILE_HANDLE Handle;\r
1230 EFI_FILE_INFO *Info;\r
1231} EFI_SHELL_FILE_INFO_NO_CONST;\r
1232\r
1233/**\r
1234 Converts a EFI shell list of structures to the coresponding UEFI Shell 2.0 type of list.\r
1235\r
1236 if OldStyleFileList is NULL then ASSERT()\r
1237\r
1238 this function will convert a SHELL_FILE_ARG based list into a callee allocated \r
1239 EFI_SHELL_FILE_INFO based list. it is up to the caller to free the memory via\r
1240 the ShellCloseFileMetaArg function.\r
1241\r
9b3bf083 1242 @param[in] FileList the EFI shell list type\r
b82bfcc1 1243 @param[in,out] ListHead the list to add to\r
94b17fa1 1244\r
1245 @retval the resultant head of the double linked new format list;\r
1246**/\r
1247LIST_ENTRY*\r
1248EFIAPI\r
1249InternalShellConvertFileListType (\r
9b3bf083 1250 IN LIST_ENTRY *FileList,\r
1251 IN OUT LIST_ENTRY *ListHead\r
125c2cf4 1252 )\r
1253{\r
94b17fa1 1254 SHELL_FILE_ARG *OldInfo;\r
9b3bf083 1255 LIST_ENTRY *Link;\r
94b17fa1 1256 EFI_SHELL_FILE_INFO_NO_CONST *NewInfo;\r
1257\r
1258 //\r
9b3bf083 1259 // ASSERTs\r
94b17fa1 1260 //\r
9b3bf083 1261 ASSERT(FileList != NULL);\r
1262 ASSERT(ListHead != NULL);\r
94b17fa1 1263\r
1264 //\r
1265 // enumerate through each member of the old list and copy\r
1266 //\r
d2b4564b 1267 for (Link = FileList->ForwardLink; Link != FileList; Link = Link->ForwardLink) {\r
94b17fa1 1268 OldInfo = CR (Link, SHELL_FILE_ARG, Link, SHELL_FILE_ARG_SIGNATURE);\r
1269\r
1270 //\r
1271 // make sure the old list was valid\r
1272 //\r
1273 ASSERT(OldInfo != NULL); \r
1274 ASSERT(OldInfo->Info != NULL);\r
1275 ASSERT(OldInfo->FullName != NULL);\r
1276 ASSERT(OldInfo->FileName != NULL);\r
1277\r
1278 //\r
1279 // allocate a new EFI_SHELL_FILE_INFO object\r
1280 //\r
1281 NewInfo = AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));\r
1282 \r
1283 // \r
1284 // copy the simple items\r
1285 //\r
1286 NewInfo->Handle = OldInfo->Handle;\r
1287 NewInfo->Status = OldInfo->Status;\r
1288\r
d2b4564b 1289 // old shell checks for 0 not NULL\r
1290 OldInfo->Handle = 0;\r
1291\r
94b17fa1 1292 //\r
1293 // allocate new space to copy strings and structure\r
1294 //\r
1295 NewInfo->FullName = AllocateZeroPool(StrSize(OldInfo->FullName));\r
1296 NewInfo->FileName = AllocateZeroPool(StrSize(OldInfo->FileName));\r
1297 NewInfo->Info = AllocateZeroPool((UINTN)OldInfo->Info->Size);\r
1298 \r
1299 //\r
1300 // make sure all the memory allocations were sucessful\r
1301 //\r
1302 ASSERT(NewInfo->FullName != NULL);\r
1303 ASSERT(NewInfo->FileName != NULL);\r
1304 ASSERT(NewInfo->Info != NULL);\r
1305\r
1306 //\r
1307 // Copt the strings and structure\r
1308 //\r
1309 StrCpy(NewInfo->FullName, OldInfo->FullName);\r
1310 StrCpy(NewInfo->FileName, OldInfo->FileName);\r
1311 gBS->CopyMem (NewInfo->Info, OldInfo->Info, (UINTN)OldInfo->Info->Size);\r
1312\r
1313 //\r
1314 // add that to the list\r
1315 //\r
9b3bf083 1316 InsertTailList(ListHead, &NewInfo->Link);\r
94b17fa1 1317 }\r
1318 return (ListHead);\r
1319}\r
1320/**\r
1321 Opens a group of files based on a path.\r
1322\r
1323 This function uses the Arg to open all the matching files. Each matched \r
1324 file has a SHELL_FILE_ARG structure to record the file information. These \r
1325 structures are placed on the list ListHead. Users can get the SHELL_FILE_ARG \r
1326 structures from ListHead to access each file. This function supports wildcards\r
1327 and will process '?' and '*' as such. the list must be freed with a call to \r
1328 ShellCloseFileMetaArg().\r
1329\r
5f7431d0 1330 If you are NOT appending to an existing list *ListHead must be NULL. If \r
1331 *ListHead is NULL then it must be callee freed.\r
94b17fa1 1332\r
1333 @param Arg pointer to path string\r
1334 @param OpenMode mode to open files with\r
1335 @param ListHead head of linked list of results\r
1336\r
1337 @retval EFI_SUCCESS the operation was sucessful and the list head \r
1338 contains the list of opened files\r
1339 #retval EFI_UNSUPPORTED a previous ShellOpenFileMetaArg must be closed first.\r
1340 *ListHead is set to NULL.\r
1341 @return != EFI_SUCCESS the operation failed\r
1342\r
1343 @sa InternalShellConvertFileListType\r
1344**/\r
1345EFI_STATUS\r
1346EFIAPI\r
1347ShellOpenFileMetaArg (\r
1348 IN CHAR16 *Arg,\r
1349 IN UINT64 OpenMode,\r
1350 IN OUT EFI_SHELL_FILE_INFO **ListHead\r
1351 )\r
1352{\r
1353 EFI_STATUS Status;\r
9b3bf083 1354 LIST_ENTRY mOldStyleFileList;\r
d2b4564b 1355 \r
94b17fa1 1356 //\r
1357 // ASSERT that Arg and ListHead are not NULL\r
1358 //\r
1359 ASSERT(Arg != NULL);\r
1360 ASSERT(ListHead != NULL);\r
1361\r
1362 // \r
1363 // Check for UEFI Shell 2.0 protocols\r
1364 //\r
1365 if (mEfiShellProtocol != NULL) {\r
5f7431d0 1366 if (*ListHead == NULL) {\r
1367 *ListHead = (EFI_SHELL_FILE_INFO*)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));\r
1368 if (*ListHead == NULL) {\r
1369 return (EFI_OUT_OF_RESOURCES);\r
1370 }\r
1371 InitializeListHead(&((*ListHead)->Link));\r
1372 } \r
2247dde4 1373 Status = mEfiShellProtocol->OpenFileList(Arg, \r
94b17fa1 1374 OpenMode, \r
2247dde4 1375 ListHead);\r
1376 if (EFI_ERROR(Status)) {\r
1377 mEfiShellProtocol->RemoveDupInFileList(ListHead);\r
1378 } else {\r
1379 Status = mEfiShellProtocol->RemoveDupInFileList(ListHead);\r
1380 }\r
1381 return (Status);\r
94b17fa1 1382 } \r
1383\r
1384 //\r
1385 // ASSERT that we must have EFI shell\r
1386 //\r
1387 ASSERT(mEfiShellEnvironment2 != NULL);\r
1388\r
94b17fa1 1389 //\r
1390 // make sure the list head is initialized\r
1391 //\r
9b3bf083 1392 InitializeListHead(&mOldStyleFileList);\r
94b17fa1 1393\r
1394 //\r
1395 // Get the EFI Shell list of files\r
1396 //\r
9b3bf083 1397 Status = mEfiShellEnvironment2->FileMetaArg(Arg, &mOldStyleFileList);\r
94b17fa1 1398 if (EFI_ERROR(Status)) {\r
1399 *ListHead = NULL;\r
1400 return (Status);\r
1401 }\r
1402\r
9b3bf083 1403 if (*ListHead == NULL) {\r
1404 *ListHead = (EFI_SHELL_FILE_INFO *)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));\r
1405 if (*ListHead == NULL) {\r
1406 return (EFI_OUT_OF_RESOURCES);\r
1407 }\r
1408 }\r
1409\r
94b17fa1 1410 //\r
1411 // Convert that to equivalent of UEFI Shell 2.0 structure\r
1412 //\r
9b3bf083 1413 InternalShellConvertFileListType(&mOldStyleFileList, &(*ListHead)->Link);\r
94b17fa1 1414\r
d2b4564b 1415 //\r
1416 // Free the EFI Shell version that was converted.\r
1417 //\r
9b3bf083 1418 mEfiShellEnvironment2->FreeFileList(&mOldStyleFileList);\r
94b17fa1 1419\r
1420 return (Status);\r
1421}\r
1422/**\r
1423 Free the linked list returned from ShellOpenFileMetaArg\r
1424\r
1425 if ListHead is NULL then ASSERT()\r
1426\r
1427 @param ListHead the pointer to free\r
1428\r
1429 @retval EFI_SUCCESS the operation was sucessful\r
1430**/\r
1431EFI_STATUS\r
1432EFIAPI\r
1433ShellCloseFileMetaArg (\r
1434 IN OUT EFI_SHELL_FILE_INFO **ListHead\r
1435 )\r
1436{\r
1437 LIST_ENTRY *Node;\r
1438\r
1439 //\r
1440 // ASSERT that ListHead is not NULL\r
1441 //\r
1442 ASSERT(ListHead != NULL);\r
1443\r
1444 // \r
1445 // Check for UEFI Shell 2.0 protocols\r
1446 //\r
1447 if (mEfiShellProtocol != NULL) {\r
1448 return (mEfiShellProtocol->FreeFileList(ListHead));\r
1449 } else {\r
94b17fa1 1450 //\r
1451 // Since this is EFI Shell version we need to free our internally made copy \r
1452 // of the list\r
1453 //\r
9b3bf083 1454 for ( Node = GetFirstNode(&(*ListHead)->Link) \r
1455 ; IsListEmpty(&(*ListHead)->Link) == FALSE \r
1456 ; Node = GetFirstNode(&(*ListHead)->Link)) {\r
94b17fa1 1457 RemoveEntryList(Node);\r
d2b4564b 1458 ((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->Handle->Close(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->Handle);\r
94b17fa1 1459 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->FullName);\r
1460 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->FileName);\r
1461 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->Info);\r
1462 FreePool((EFI_SHELL_FILE_INFO_NO_CONST*)Node);\r
1463 }\r
1464 return EFI_SUCCESS;\r
1465 }\r
1466}\r
1467\r
125c2cf4 1468/**\r
1469 Find a file by searching the CWD and then the path.\r
1470\r
1471 if FileName is NULL then ASSERT.\r
1472\r
1473 if the return value is not NULL then the memory must be caller freed.\r
1474\r
1475 @param FileName Filename string.\r
1476\r
1477 @retval NULL the file was not found\r
1478 @return !NULL the full path to the file.\r
1479**/\r
1480CHAR16 *\r
1481EFIAPI\r
1482ShellFindFilePath (\r
1483 IN CONST CHAR16 *FileName\r
1484 )\r
1485{\r
1486 CONST CHAR16 *Path;\r
1487 EFI_FILE_HANDLE Handle;\r
1488 EFI_STATUS Status;\r
1489 CHAR16 *RetVal;\r
1490 CHAR16 *TestPath;\r
1491 CONST CHAR16 *Walker;\r
36a9d672 1492 UINTN Size;\r
125c2cf4 1493\r
1494 RetVal = NULL;\r
1495\r
1496 Path = ShellGetEnvironmentVariable(L"cwd");\r
1497 if (Path != NULL) {\r
36a9d672 1498 Size = StrSize(Path);\r
1499 Size += StrSize(FileName);\r
1500 TestPath = AllocateZeroPool(Size);\r
125c2cf4 1501 StrCpy(TestPath, Path);\r
1502 StrCat(TestPath, FileName);\r
1503 Status = ShellOpenFileByName(TestPath, &Handle, EFI_FILE_MODE_READ, 0);\r
1504 if (!EFI_ERROR(Status)){\r
1505 RetVal = StrnCatGrow(&RetVal, NULL, TestPath, 0);\r
1506 ShellCloseFile(&Handle);\r
1507 FreePool(TestPath);\r
1508 return (RetVal);\r
1509 }\r
1510 FreePool(TestPath);\r
1511 }\r
1512 Path = ShellGetEnvironmentVariable(L"path");\r
1513 if (Path != NULL) {\r
36a9d672 1514 Size = StrSize(Path);\r
1515 Size += StrSize(FileName);\r
1516 TestPath = AllocateZeroPool(Size);\r
125c2cf4 1517 Walker = (CHAR16*)Path; \r
1518 do {\r
1519 CopyMem(TestPath, Walker, StrSize(Walker));\r
1520 if (StrStr(TestPath, L";") != NULL) {\r
1521 *(StrStr(TestPath, L";")) = CHAR_NULL;\r
1522 }\r
1523 StrCat(TestPath, FileName);\r
1524 if (StrStr(Walker, L";") != NULL) {\r
1525 Walker = StrStr(Walker, L";") + 1;\r
1526 } else {\r
1527 Walker = NULL;\r
1528 }\r
1529 Status = ShellOpenFileByName(TestPath, &Handle, EFI_FILE_MODE_READ, 0);\r
1530 if (!EFI_ERROR(Status)){\r
1531 RetVal = StrnCatGrow(&RetVal, NULL, TestPath, 0);\r
1532 ShellCloseFile(&Handle);\r
1533 break;\r
1534 }\r
1535 } while (Walker != NULL && Walker[0] != CHAR_NULL);\r
1536 FreePool(TestPath);\r
1537 }\r
1538 return (RetVal);\r
1539}\r
1540\r
94b17fa1 1541typedef struct {\r
9b3bf083 1542 LIST_ENTRY Link;\r
94b17fa1 1543 CHAR16 *Name;\r
1544 ParamType Type;\r
1545 CHAR16 *Value;\r
1546 UINTN OriginalPosition;\r
1547} SHELL_PARAM_PACKAGE;\r
1548\r
1549/**\r
1550 Checks the list of valid arguments and returns TRUE if the item was found. If the \r
1551 return value is TRUE then the type parameter is set also.\r
1552 \r
1553 if CheckList is NULL then ASSERT();\r
1554 if Name is NULL then ASSERT();\r
1555 if Type is NULL then ASSERT();\r
1556\r
1557 @param Type pointer to type of parameter if it was found\r
1558 @param Name pointer to Name of parameter found\r
1559 @param CheckList List to check against\r
1560\r
1561 @retval TRUE the Parameter was found. Type is valid.\r
1562 @retval FALSE the Parameter was not found. Type is not valid.\r
1563**/\r
1564BOOLEAN\r
1565EFIAPI\r
d2b4564b 1566InternalIsOnCheckList (\r
94b17fa1 1567 IN CONST CHAR16 *Name,\r
1568 IN CONST SHELL_PARAM_ITEM *CheckList,\r
1569 OUT ParamType *Type\r
2247dde4 1570 ) {\r
94b17fa1 1571 SHELL_PARAM_ITEM *TempListItem;\r
1572\r
1573 //\r
1574 // ASSERT that all 3 pointer parameters aren't NULL\r
1575 //\r
1576 ASSERT(CheckList != NULL);\r
1577 ASSERT(Type != NULL);\r
1578 ASSERT(Name != NULL);\r
1579\r
d2b4564b 1580 //\r
1581 // question mark and page break mode are always supported\r
1582 //\r
1583 if ((StrCmp(Name, L"-?") == 0) ||\r
1584 (StrCmp(Name, L"-b") == 0)\r
1585 ) {\r
1586 return (TRUE);\r
1587 }\r
1588\r
94b17fa1 1589 //\r
1590 // Enumerate through the list\r
1591 //\r
1592 for (TempListItem = (SHELL_PARAM_ITEM*)CheckList ; TempListItem->Name != NULL ; TempListItem++) {\r
1593 //\r
9eb53ac3 1594 // If the Type is TypeStart only check the first characters of the passed in param\r
1595 // If it matches set the type and return TRUE\r
94b17fa1 1596 //\r
9eb53ac3 1597 if (TempListItem->Type == TypeStart && StrnCmp(Name, TempListItem->Name, StrLen(TempListItem->Name)) == 0) {\r
1598 *Type = TempListItem->Type;\r
1599 return (TRUE);\r
1600 } else if (StrCmp(Name, TempListItem->Name) == 0) {\r
94b17fa1 1601 *Type = TempListItem->Type;\r
1602 return (TRUE);\r
1603 }\r
1604 }\r
2247dde4 1605\r
94b17fa1 1606 return (FALSE);\r
1607}\r
1608/**\r
d2b4564b 1609 Checks the string for indicators of "flag" status. this is a leading '/', '-', or '+'\r
94b17fa1 1610\r
1611 @param Name pointer to Name of parameter found\r
1612\r
1613 @retval TRUE the Parameter is a flag.\r
1614 @retval FALSE the Parameter not a flag\r
1615**/\r
1616BOOLEAN\r
1617EFIAPI\r
d2b4564b 1618InternalIsFlag (\r
2247dde4 1619 IN CONST CHAR16 *Name,\r
1620 IN BOOLEAN AlwaysAllowNumbers\r
94b17fa1 1621 )\r
1622{\r
1623 //\r
1624 // ASSERT that Name isn't NULL\r
1625 //\r
1626 ASSERT(Name != NULL);\r
1627\r
2247dde4 1628 //\r
1629 // If we accept numbers then dont return TRUE. (they will be values)\r
1630 //\r
1631 if (((Name[0] == L'-' || Name[0] == L'+') && ShellInternalIsHexaDecimalDigitCharacter(Name[1])) && AlwaysAllowNumbers == TRUE) {\r
1632 return (FALSE);\r
1633 }\r
1634\r
94b17fa1 1635 //\r
1636 // If the Name has a / or - as the first character return TRUE\r
1637 //\r
d2b4564b 1638 if ((Name[0] == L'/') || \r
1639 (Name[0] == L'-') ||\r
1640 (Name[0] == L'+')\r
1641 ) {\r
94b17fa1 1642 return (TRUE);\r
1643 }\r
1644 return (FALSE);\r
1645}\r
1646\r
1647/**\r
1648 Checks the command line arguments passed against the list of valid ones. \r
1649\r
1650 If no initialization is required, then return RETURN_SUCCESS.\r
1651 \r
1652 @param CheckList pointer to list of parameters to check\r
1653 @param CheckPackage pointer to pointer to list checked values\r
1654 @param ProblemParam optional pointer to pointer to unicode string for \r
d2b4564b 1655 the paramater that caused failure. If used then the\r
1656 caller is responsible for freeing the memory.\r
94b17fa1 1657 @param AutoPageBreak will automatically set PageBreakEnabled for "b" parameter\r
1658 @param Argc Count of parameters in Argv\r
1659 @param Argv pointer to array of parameters\r
1660\r
1661 @retval EFI_SUCCESS The operation completed sucessfully.\r
1662 @retval EFI_OUT_OF_RESOURCES A memory allocation failed\r
1663 @retval EFI_INVALID_PARAMETER A parameter was invalid\r
1664 @retval EFI_VOLUME_CORRUPTED the command line was corrupt. an argument was \r
1665 duplicated. the duplicated command line argument \r
1666 was returned in ProblemParam if provided.\r
1667 @retval EFI_NOT_FOUND a argument required a value that was missing. \r
1668 the invalid command line argument was returned in\r
1669 ProblemParam if provided.\r
1670**/\r
2247dde4 1671STATIC\r
94b17fa1 1672EFI_STATUS\r
1673EFIAPI\r
1674InternalCommandLineParse (\r
1675 IN CONST SHELL_PARAM_ITEM *CheckList,\r
1676 OUT LIST_ENTRY **CheckPackage,\r
1677 OUT CHAR16 **ProblemParam OPTIONAL,\r
1678 IN BOOLEAN AutoPageBreak,\r
1679 IN CONST CHAR16 **Argv,\r
2247dde4 1680 IN UINTN Argc,\r
1681 IN BOOLEAN AlwaysAllowNumbers\r
1682 ) {\r
94b17fa1 1683 UINTN LoopCounter;\r
94b17fa1 1684 ParamType CurrentItemType;\r
1685 SHELL_PARAM_PACKAGE *CurrentItemPackage;\r
125c2cf4 1686 UINTN GetItemValue;\r
1687 UINTN ValueSize;\r
94b17fa1 1688\r
1689 CurrentItemPackage = NULL;\r
2247dde4 1690 mTotalParameterCount = 0;\r
125c2cf4 1691 GetItemValue = 0;\r
1692 ValueSize = 0;\r
94b17fa1 1693\r
1694 //\r
1695 // If there is only 1 item we dont need to do anything\r
1696 //\r
1697 if (Argc <= 1) {\r
1698 *CheckPackage = NULL;\r
1699 return (EFI_SUCCESS);\r
1700 }\r
1701\r
2247dde4 1702 //\r
1703 // ASSERTs\r
1704 //\r
1705 ASSERT(CheckList != NULL);\r
1706 ASSERT(Argv != NULL);\r
1707\r
94b17fa1 1708 //\r
1709 // initialize the linked list\r
1710 //\r
1711 *CheckPackage = (LIST_ENTRY*)AllocateZeroPool(sizeof(LIST_ENTRY));\r
1712 InitializeListHead(*CheckPackage);\r
1713\r
1714 //\r
1715 // loop through each of the arguments\r
1716 //\r
1717 for (LoopCounter = 0 ; LoopCounter < Argc ; ++LoopCounter) {\r
1718 if (Argv[LoopCounter] == NULL) {\r
1719 //\r
1720 // do nothing for NULL argv\r
1721 //\r
d2b4564b 1722 } else if (InternalIsOnCheckList(Argv[LoopCounter], CheckList, &CurrentItemType) == TRUE) {\r
2247dde4 1723 //\r
1724 // We might have leftover if last parameter didnt have optional value\r
1725 //\r
125c2cf4 1726 if (GetItemValue != 0) {\r
1727 GetItemValue = 0;\r
2247dde4 1728 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);\r
1729 }\r
94b17fa1 1730 //\r
1731 // this is a flag\r
1732 //\r
1733 CurrentItemPackage = AllocatePool(sizeof(SHELL_PARAM_PACKAGE));\r
1734 ASSERT(CurrentItemPackage != NULL);\r
1735 CurrentItemPackage->Name = AllocatePool(StrSize(Argv[LoopCounter]));\r
1736 ASSERT(CurrentItemPackage->Name != NULL);\r
1737 StrCpy(CurrentItemPackage->Name, Argv[LoopCounter]);\r
1738 CurrentItemPackage->Type = CurrentItemType;\r
1739 CurrentItemPackage->OriginalPosition = (UINTN)(-1);\r
b1f95a06 1740 CurrentItemPackage->Value = NULL;\r
94b17fa1 1741\r
1742 //\r
1743 // Does this flag require a value\r
1744 //\r
125c2cf4 1745 switch (CurrentItemPackage->Type) {\r
94b17fa1 1746 //\r
125c2cf4 1747 // possibly trigger the next loop(s) to populate the value of this item\r
1748 // \r
1749 case TypeValue:\r
1750 GetItemValue = 1; \r
1751 ValueSize = 0;\r
1752 break;\r
1753 case TypeDoubleValue:\r
1754 GetItemValue = 2;\r
1755 ValueSize = 0;\r
1756 break;\r
1757 case TypeMaxValue:\r
1758 GetItemValue = (UINTN)(-1);\r
1759 ValueSize = 0;\r
1760 break;\r
1761 default:\r
1762 //\r
1763 // this item has no value expected; we are done\r
1764 //\r
1765 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);\r
1766 ASSERT(GetItemValue == 0);\r
1767 break;\r
94b17fa1 1768 }\r
125c2cf4 1769 } else if (GetItemValue != 0 && InternalIsFlag(Argv[LoopCounter], AlwaysAllowNumbers) == FALSE) {\r
b1f95a06 1770 ASSERT(CurrentItemPackage != NULL);\r
1771 //\r
125c2cf4 1772 // get the item VALUE for a previous flag\r
b1f95a06 1773 //\r
125c2cf4 1774 CurrentItemPackage->Value = ReallocatePool(ValueSize, ValueSize + StrSize(Argv[LoopCounter]) + sizeof(CHAR16), CurrentItemPackage->Value);\r
b1f95a06 1775 ASSERT(CurrentItemPackage->Value != NULL);\r
125c2cf4 1776 if (ValueSize == 0) {\r
1777 StrCpy(CurrentItemPackage->Value, Argv[LoopCounter]);\r
1778 } else {\r
1779 StrCat(CurrentItemPackage->Value, L" ");\r
1780 StrCat(CurrentItemPackage->Value, Argv[LoopCounter]);\r
1781 }\r
1782 ValueSize += StrSize(Argv[LoopCounter]) + sizeof(CHAR16);\r
1783 GetItemValue--;\r
1784 if (GetItemValue == 0) {\r
1785 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);\r
1786 }\r
2247dde4 1787 } else if (InternalIsFlag(Argv[LoopCounter], AlwaysAllowNumbers) == FALSE) {\r
b1f95a06 1788 //\r
1789 // add this one as a non-flag\r
1790 //\r
1791 CurrentItemPackage = AllocatePool(sizeof(SHELL_PARAM_PACKAGE));\r
1792 ASSERT(CurrentItemPackage != NULL);\r
1793 CurrentItemPackage->Name = NULL;\r
1794 CurrentItemPackage->Type = TypePosition;\r
1795 CurrentItemPackage->Value = AllocatePool(StrSize(Argv[LoopCounter]));\r
1796 ASSERT(CurrentItemPackage->Value != NULL);\r
1797 StrCpy(CurrentItemPackage->Value, Argv[LoopCounter]);\r
2247dde4 1798 CurrentItemPackage->OriginalPosition = mTotalParameterCount++;\r
9b3bf083 1799 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);\r
94b17fa1 1800 } else if (ProblemParam) {\r
1801 //\r
1802 // this was a non-recognised flag... error!\r
1803 //\r
d2b4564b 1804 *ProblemParam = AllocatePool(StrSize(Argv[LoopCounter]));\r
1805 ASSERT(*ProblemParam != NULL);\r
1806 StrCpy(*ProblemParam, Argv[LoopCounter]);\r
94b17fa1 1807 ShellCommandLineFreeVarList(*CheckPackage);\r
1808 *CheckPackage = NULL;\r
1809 return (EFI_VOLUME_CORRUPTED);\r
1810 } else {\r
1811 ShellCommandLineFreeVarList(*CheckPackage);\r
1812 *CheckPackage = NULL;\r
1813 return (EFI_VOLUME_CORRUPTED);\r
1814 }\r
1815 }\r
125c2cf4 1816 if (GetItemValue != 0) {\r
1817 GetItemValue = 0;\r
1818 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);\r
1819 }\r
94b17fa1 1820 //\r
1821 // support for AutoPageBreak\r
1822 //\r
1823 if (AutoPageBreak && ShellCommandLineGetFlag(*CheckPackage, L"-b")) {\r
1824 ShellSetPageBreakMode(TRUE);\r
1825 }\r
1826 return (EFI_SUCCESS);\r
1827}\r
1828\r
1829/**\r
1830 Checks the command line arguments passed against the list of valid ones. \r
1831 Optionally removes NULL values first.\r
1832 \r
1833 If no initialization is required, then return RETURN_SUCCESS.\r
1834 \r
1835 @param CheckList pointer to list of parameters to check\r
1836 @param CheckPackage pointer to pointer to list checked values\r
1837 @param ProblemParam optional pointer to pointer to unicode string for \r
1838 the paramater that caused failure.\r
1839 @param AutoPageBreak will automatically set PageBreakEnabled for "b" parameter\r
1840\r
1841 @retval EFI_SUCCESS The operation completed sucessfully.\r
1842 @retval EFI_OUT_OF_RESOURCES A memory allocation failed\r
1843 @retval EFI_INVALID_PARAMETER A parameter was invalid\r
1844 @retval EFI_VOLUME_CORRUPTED the command line was corrupt. an argument was \r
1845 duplicated. the duplicated command line argument \r
1846 was returned in ProblemParam if provided.\r
1847 @retval EFI_DEVICE_ERROR the commands contained 2 opposing arguments. one\r
1848 of the command line arguments was returned in \r
1849 ProblemParam if provided.\r
1850 @retval EFI_NOT_FOUND a argument required a value that was missing. \r
1851 the invalid command line argument was returned in\r
1852 ProblemParam if provided.\r
1853**/\r
1854EFI_STATUS\r
1855EFIAPI\r
2247dde4 1856ShellCommandLineParseEx (\r
94b17fa1 1857 IN CONST SHELL_PARAM_ITEM *CheckList,\r
1858 OUT LIST_ENTRY **CheckPackage,\r
1859 OUT CHAR16 **ProblemParam OPTIONAL,\r
2247dde4 1860 IN BOOLEAN AutoPageBreak,\r
1861 IN BOOLEAN AlwaysAllowNumbers\r
1862 ) {\r
94b17fa1 1863 // \r
1864 // ASSERT that CheckList and CheckPackage aren't NULL\r
1865 //\r
1866 ASSERT(CheckList != NULL);\r
1867 ASSERT(CheckPackage != NULL);\r
1868\r
1869 // \r
1870 // Check for UEFI Shell 2.0 protocols\r
1871 //\r
1872 if (mEfiShellParametersProtocol != NULL) {\r
1873 return (InternalCommandLineParse(CheckList, \r
1874 CheckPackage, \r
1875 ProblemParam, \r
1876 AutoPageBreak, \r
08d7f8e8 1877 (CONST CHAR16**) mEfiShellParametersProtocol->Argv,\r
2247dde4 1878 mEfiShellParametersProtocol->Argc,\r
1879 AlwaysAllowNumbers));\r
94b17fa1 1880 }\r
1881\r
1882 // \r
1883 // ASSERT That EFI Shell is not required\r
1884 //\r
1885 ASSERT (mEfiShellInterface != NULL);\r
1886 return (InternalCommandLineParse(CheckList, \r
1887 CheckPackage, \r
1888 ProblemParam, \r
1889 AutoPageBreak, \r
08d7f8e8 1890 (CONST CHAR16**) mEfiShellInterface->Argv,\r
2247dde4 1891 mEfiShellInterface->Argc,\r
1892 AlwaysAllowNumbers));\r
94b17fa1 1893}\r
1894\r
1895/**\r
1896 Frees shell variable list that was returned from ShellCommandLineParse.\r
1897\r
1898 This function will free all the memory that was used for the CheckPackage\r
1899 list of postprocessed shell arguments.\r
1900\r
1901 this function has no return value.\r
1902\r
1903 if CheckPackage is NULL, then return\r
1904\r
1905 @param CheckPackage the list to de-allocate\r
1906 **/\r
1907VOID\r
1908EFIAPI\r
1909ShellCommandLineFreeVarList (\r
1910 IN LIST_ENTRY *CheckPackage\r
2247dde4 1911 ) {\r
94b17fa1 1912 LIST_ENTRY *Node;\r
1913\r
1914 //\r
1915 // check for CheckPackage == NULL\r
1916 //\r
1917 if (CheckPackage == NULL) {\r
1918 return;\r
1919 }\r
1920\r
1921 //\r
1922 // for each node in the list\r
1923 //\r
9eb53ac3 1924 for ( Node = GetFirstNode(CheckPackage)\r
2247dde4 1925 ; IsListEmpty(CheckPackage) == FALSE\r
9eb53ac3 1926 ; Node = GetFirstNode(CheckPackage)\r
1927 ){\r
94b17fa1 1928 //\r
1929 // Remove it from the list\r
1930 //\r
1931 RemoveEntryList(Node);\r
1932\r
1933 //\r
1934 // if it has a name free the name\r
1935 //\r
1936 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {\r
1937 FreePool(((SHELL_PARAM_PACKAGE*)Node)->Name);\r
1938 }\r
1939\r
1940 //\r
1941 // if it has a value free the value\r
1942 //\r
1943 if (((SHELL_PARAM_PACKAGE*)Node)->Value != NULL) {\r
1944 FreePool(((SHELL_PARAM_PACKAGE*)Node)->Value);\r
1945 }\r
1946 \r
1947 //\r
1948 // free the node structure\r
1949 //\r
1950 FreePool((SHELL_PARAM_PACKAGE*)Node);\r
1951 }\r
1952 //\r
1953 // free the list head node\r
1954 //\r
1955 FreePool(CheckPackage);\r
1956}\r
1957/**\r
1958 Checks for presence of a flag parameter\r
1959\r
1960 flag arguments are in the form of "-<Key>" or "/<Key>", but do not have a value following the key\r
1961\r
1962 if CheckPackage is NULL then return FALSE.\r
1963 if KeyString is NULL then ASSERT()\r
1964 \r
1965 @param CheckPackage The package of parsed command line arguments\r
1966 @param KeyString the Key of the command line argument to check for\r
1967\r
1968 @retval TRUE the flag is on the command line\r
1969 @retval FALSE the flag is not on the command line\r
1970 **/\r
1971BOOLEAN\r
1972EFIAPI\r
1973ShellCommandLineGetFlag (\r
1974 IN CONST LIST_ENTRY *CheckPackage,\r
1975 IN CHAR16 *KeyString\r
2247dde4 1976 ) {\r
94b17fa1 1977 LIST_ENTRY *Node;\r
1978\r
1979 //\r
1980 // ASSERT that both CheckPackage and KeyString aren't NULL\r
1981 //\r
1982 ASSERT(KeyString != NULL);\r
1983\r
1984 //\r
1985 // return FALSE for no package\r
1986 //\r
1987 if (CheckPackage == NULL) {\r
1988 return (FALSE);\r
1989 }\r
1990\r
1991 //\r
1992 // enumerate through the list of parametrs\r
1993 //\r
9eb53ac3 1994 for ( Node = GetFirstNode(CheckPackage) \r
1995 ; !IsNull (CheckPackage, Node) \r
1996 ; Node = GetNextNode(CheckPackage, Node) \r
1997 ){\r
94b17fa1 1998 //\r
1999 // If the Name matches, return TRUE (and there may be NULL name)\r
2000 //\r
2001 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {\r
9eb53ac3 2002 //\r
2003 // If Type is TypeStart then only compare the begining of the strings\r
2004 //\r
2005 if ( ((SHELL_PARAM_PACKAGE*)Node)->Type == TypeStart \r
2006 && StrnCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name, StrLen(KeyString)) == 0\r
2007 ){\r
2008 return (TRUE);\r
2009 } else if (StrCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {\r
94b17fa1 2010 return (TRUE);\r
2011 }\r
2012 }\r
2013 }\r
2014 return (FALSE);\r
2015}\r
2016/**\r
2017 returns value from command line argument\r
2018\r
2019 value parameters are in the form of "-<Key> value" or "/<Key> value"\r
2020 \r
2021 if CheckPackage is NULL, then return NULL;\r
2022\r
2023 @param CheckPackage The package of parsed command line arguments\r
2024 @param KeyString the Key of the command line argument to check for\r
2025\r
2026 @retval NULL the flag is not on the command line\r
2027 @return !=NULL pointer to unicode string of the value\r
2028 **/\r
2029CONST CHAR16*\r
2030EFIAPI\r
2031ShellCommandLineGetValue (\r
2032 IN CONST LIST_ENTRY *CheckPackage,\r
2033 IN CHAR16 *KeyString\r
2247dde4 2034 ) {\r
94b17fa1 2035 LIST_ENTRY *Node;\r
2036\r
2037 //\r
2038 // check for CheckPackage == NULL\r
2039 //\r
2040 if (CheckPackage == NULL) {\r
2041 return (NULL);\r
2042 }\r
2043\r
2044 //\r
2045 // enumerate through the list of parametrs\r
2046 //\r
9eb53ac3 2047 for ( Node = GetFirstNode(CheckPackage) \r
2048 ; !IsNull (CheckPackage, Node) \r
2049 ; Node = GetNextNode(CheckPackage, Node) \r
2050 ){\r
94b17fa1 2051 //\r
2052 // If the Name matches, return the value (name can be NULL)\r
2053 //\r
2054 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {\r
9eb53ac3 2055 //\r
2056 // If Type is TypeStart then only compare the begining of the strings\r
2057 //\r
2058 if ( ((SHELL_PARAM_PACKAGE*)Node)->Type == TypeStart \r
2059 && StrnCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name, StrLen(KeyString)) == 0\r
2060 ){\r
2061 //\r
2062 // return the string part after the flag\r
2063 //\r
2064 return (((SHELL_PARAM_PACKAGE*)Node)->Name + StrLen(KeyString));\r
2065 } else if (StrCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {\r
2066 //\r
2067 // return the value\r
2068 //\r
94b17fa1 2069 return (((SHELL_PARAM_PACKAGE*)Node)->Value);\r
2070 }\r
2071 }\r
2072 }\r
2073 return (NULL);\r
2074}\r
2075/**\r
2076 returns raw value from command line argument\r
2077\r
2078 raw value parameters are in the form of "value" in a specific position in the list\r
2079 \r
2080 if CheckPackage is NULL, then return NULL;\r
2081\r
2082 @param CheckPackage The package of parsed command line arguments\r
2083 @param Position the position of the value \r
2084\r
2085 @retval NULL the flag is not on the command line\r
2086 @return !=NULL pointer to unicode string of the value\r
2087 **/\r
2088CONST CHAR16*\r
2089EFIAPI\r
2090ShellCommandLineGetRawValue (\r
2091 IN CONST LIST_ENTRY *CheckPackage,\r
2092 IN UINT32 Position\r
2247dde4 2093 ) {\r
94b17fa1 2094 LIST_ENTRY *Node;\r
2095\r
2096 //\r
2097 // check for CheckPackage == NULL\r
2098 //\r
2099 if (CheckPackage == NULL) {\r
2100 return (NULL);\r
2101 }\r
2102\r
2103 //\r
2104 // enumerate through the list of parametrs\r
2105 //\r
b82bfcc1 2106 for ( Node = GetFirstNode(CheckPackage) \r
2107 ; !IsNull (CheckPackage, Node) \r
2108 ; Node = GetNextNode(CheckPackage, Node) \r
2109 ){\r
94b17fa1 2110 //\r
2111 // If the position matches, return the value\r
2112 //\r
2113 if (((SHELL_PARAM_PACKAGE*)Node)->OriginalPosition == Position) {\r
2114 return (((SHELL_PARAM_PACKAGE*)Node)->Value);\r
2115 }\r
2116 }\r
2117 return (NULL);\r
b1f95a06 2118}\r
2247dde4 2119\r
2120/**\r
2121 returns the number of command line value parameters that were parsed. \r
2122 \r
2123 this will not include flags.\r
2124\r
2125 @retval (UINTN)-1 No parsing has ocurred\r
2126 @return other The number of value parameters found\r
2127**/\r
2128UINTN\r
2129EFIAPI\r
2130ShellCommandLineGetCount(\r
2131 VOID\r
125c2cf4 2132 )\r
2133{\r
2247dde4 2134 return (mTotalParameterCount);\r
2135}\r
2136\r
36a9d672 2137/**\r
2138 Determins if a parameter is duplicated.\r
2139\r
2140 If Param is not NULL then it will point to a callee allocated string buffer \r
2141 with the parameter value if a duplicate is found.\r
2142\r
2143 If CheckPackage is NULL, then ASSERT.\r
2144\r
2145 @param[in] CheckPackage The package of parsed command line arguments.\r
2146 @param[out] Param Upon finding one, a pointer to the duplicated parameter.\r
2147\r
2148 @retval EFI_SUCCESS No parameters were duplicated.\r
2149 @retval EFI_DEVICE_ERROR A duplicate was found.\r
2150 **/\r
2151EFI_STATUS\r
2152EFIAPI\r
2153ShellCommandLineCheckDuplicate (\r
2154 IN CONST LIST_ENTRY *CheckPackage,\r
2155 OUT CHAR16 **Param\r
2156 )\r
2157{\r
2158 LIST_ENTRY *Node1;\r
2159 LIST_ENTRY *Node2;\r
2160 \r
2161 ASSERT(CheckPackage != NULL);\r
2162\r
2163 for ( Node1 = GetFirstNode(CheckPackage) \r
2164 ; !IsNull (CheckPackage, Node1) \r
2165 ; Node1 = GetNextNode(CheckPackage, Node1) \r
2166 ){\r
2167 for ( Node2 = GetNextNode(CheckPackage, Node1) \r
2168 ; !IsNull (CheckPackage, Node2) \r
2169 ; Node2 = GetNextNode(CheckPackage, Node2) \r
2170 ){\r
2171 if (StrCmp(((SHELL_PARAM_PACKAGE*)Node1)->Name, ((SHELL_PARAM_PACKAGE*)Node2)->Name) == 0) {\r
2172 if (Param != NULL) {\r
2173 *Param = NULL;\r
2174 *Param = StrnCatGrow(Param, NULL, ((SHELL_PARAM_PACKAGE*)Node1)->Name, 0);\r
2175 }\r
2176 return (EFI_DEVICE_ERROR);\r
2177 }\r
2178 }\r
2179 }\r
2180 return (EFI_SUCCESS);\r
2181}\r
2182\r
975136ab 2183/**\r
2184 This is a find and replace function. it will return the NewString as a copy of \r
2185 SourceString with each instance of FindTarget replaced with ReplaceWith.\r
2186\r
2187 If the string would grow bigger than NewSize it will halt and return error.\r
2188\r
2189 @param[in] SourceString String with source buffer\r
b82bfcc1 2190 @param[in,out] NewString String with resultant buffer\r
975136ab 2191 @param[in] NewSize Size in bytes of NewString\r
2192 @param[in] FindTarget String to look for\r
2193 @param[in[ ReplaceWith String to replace FindTarget with\r
2194\r
2195 @retval EFI_INVALID_PARAMETER SourceString was NULL\r
2196 @retval EFI_INVALID_PARAMETER NewString was NULL\r
2197 @retval EFI_INVALID_PARAMETER FindTarget was NULL\r
2198 @retval EFI_INVALID_PARAMETER ReplaceWith was NULL\r
2199 @retval EFI_INVALID_PARAMETER FindTarget had length < 1\r
2200 @retval EFI_INVALID_PARAMETER SourceString had length < 1\r
2201 @retval EFI_BUFFER_TOO_SMALL NewSize was less than the minimum size to hold \r
2202 the new string (truncation occurred)\r
2203 @retval EFI_SUCCESS the string was sucessfully copied with replacement\r
2204**/\r
2205\r
2206EFI_STATUS\r
2207EFIAPI\r
2208CopyReplace(\r
2209 IN CHAR16 CONST *SourceString,\r
2210 IN CHAR16 *NewString,\r
2211 IN UINTN NewSize,\r
2212 IN CONST CHAR16 *FindTarget,\r
2213 IN CONST CHAR16 *ReplaceWith\r
2247dde4 2214 ) \r
2215{\r
0158294b 2216 UINTN Size;\r
975136ab 2217 if ( (SourceString == NULL)\r
2218 || (NewString == NULL)\r
2219 || (FindTarget == NULL)\r
2220 || (ReplaceWith == NULL)\r
2221 || (StrLen(FindTarget) < 1)\r
2222 || (StrLen(SourceString) < 1)\r
2223 ){\r
2224 return (EFI_INVALID_PARAMETER);\r
2225 }\r
2247dde4 2226 NewString = SetMem16(NewString, NewSize, CHAR_NULL);\r
2227 while (*SourceString != CHAR_NULL) {\r
975136ab 2228 if (StrnCmp(SourceString, FindTarget, StrLen(FindTarget)) == 0) {\r
2229 SourceString += StrLen(FindTarget);\r
0158294b 2230 Size = StrSize(NewString);\r
2231 if ((Size + (StrLen(ReplaceWith)*sizeof(CHAR16))) > NewSize) {\r
975136ab 2232 return (EFI_BUFFER_TOO_SMALL);\r
2233 }\r
2234 StrCat(NewString, ReplaceWith);\r
2235 } else {\r
0158294b 2236 Size = StrSize(NewString);\r
2237 if (Size + sizeof(CHAR16) > NewSize) {\r
975136ab 2238 return (EFI_BUFFER_TOO_SMALL);\r
2239 }\r
2240 StrnCat(NewString, SourceString, 1);\r
2241 SourceString++;\r
2242 }\r
2243 }\r
2244 return (EFI_SUCCESS);\r
2245}\r
b1f95a06 2246\r
e2f8297f 2247/**\r
2248 Internal worker function to output a string.\r
2249\r
2250 This function will output a string to the correct StdOut.\r
2251\r
2252 @param[in] String The string to print out.\r
2253\r
2254 @retval EFI_SUCCESS The operation was sucessful.\r
2255 @retval !EFI_SUCCESS The operation failed.\r
2256**/\r
2257EFI_STATUS\r
2258EFIAPI\r
2259InternalPrintTo (\r
2260 IN CONST CHAR16 *String\r
2261 )\r
2262{\r
2263 UINTN Size;\r
2264 Size = StrSize(String) - sizeof(CHAR16);\r
2265 if (mEfiShellParametersProtocol != NULL) {\r
2266 return (mEfiShellParametersProtocol->StdOut->Write(mEfiShellParametersProtocol->StdOut, &Size, (VOID*)String));\r
2267 }\r
2268 if (mEfiShellInterface != NULL) {\r
2269 return ( mEfiShellInterface->StdOut->Write(mEfiShellInterface->StdOut, &Size, (VOID*)String));\r
2270 }\r
2271 ASSERT(FALSE);\r
2272 return (EFI_UNSUPPORTED);\r
2273}\r
2274\r
b1f95a06 2275/**\r
2276 Print at a specific location on the screen.\r
2277\r
f1b87e7a 2278 This function will move the cursor to a given screen location and print the specified string\r
b1f95a06 2279 \r
2280 If -1 is specified for either the Row or Col the current screen location for BOTH \r
f1b87e7a 2281 will be used.\r
b1f95a06 2282\r
2283 if either Row or Col is out of range for the current console, then ASSERT\r
2284 if Format is NULL, then ASSERT\r
2285\r
2286 In addition to the standard %-based flags as supported by UefiLib Print() this supports \r
2287 the following additional flags:\r
2288 %N - Set output attribute to normal\r
2289 %H - Set output attribute to highlight\r
2290 %E - Set output attribute to error\r
2291 %B - Set output attribute to blue color\r
2292 %V - Set output attribute to green color\r
2293\r
2294 Note: The background color is controlled by the shell command cls.\r
2295\r
2296 @param[in] Row the row to print at\r
2297 @param[in] Col the column to print at\r
2298 @param[in] Format the format string\r
2247dde4 2299 @param[in] Marker the marker for the variable argument list\r
b1f95a06 2300\r
2301 @return the number of characters printed to the screen\r
2302**/\r
2303\r
2304UINTN\r
2305EFIAPI\r
2247dde4 2306InternalShellPrintWorker(\r
b1f95a06 2307 IN INT32 Col OPTIONAL,\r
2308 IN INT32 Row OPTIONAL,\r
2309 IN CONST CHAR16 *Format,\r
2247dde4 2310 VA_LIST Marker\r
2311 ) \r
2312{\r
b1f95a06 2313 UINTN BufferSize;\r
975136ab 2314 CHAR16 *PostReplaceFormat;\r
2315 CHAR16 *PostReplaceFormat2;\r
b1f95a06 2316 UINTN Return;\r
b1f95a06 2317 EFI_STATUS Status;\r
975136ab 2318 UINTN NormalAttribute;\r
2319 CHAR16 *ResumeLocation;\r
2320 CHAR16 *FormatWalker;\r
b1f95a06 2321 \r
e2f8297f 2322 BufferSize = PcdGet16 (PcdShellLibMaxPrintBufferSize);\r
2323 ASSERT(PcdGet16 (PcdShellLibMaxPrintBufferSize) < PcdGet32 (PcdMaximumUnicodeStringLength));\r
975136ab 2324 PostReplaceFormat = AllocateZeroPool (BufferSize);\r
2325 ASSERT (PostReplaceFormat != NULL);\r
2326 PostReplaceFormat2 = AllocateZeroPool (BufferSize);\r
2327 ASSERT (PostReplaceFormat2 != NULL);\r
b1f95a06 2328\r
975136ab 2329 //\r
2330 // Back and forth each time fixing up 1 of our flags...\r
2331 //\r
2332 Status = CopyReplace(Format, PostReplaceFormat, BufferSize, L"%N", L"%%N");\r
2333 ASSERT_EFI_ERROR(Status);\r
2334 Status = CopyReplace(PostReplaceFormat, PostReplaceFormat2, BufferSize, L"%E", L"%%E");\r
2335 ASSERT_EFI_ERROR(Status);\r
2336 Status = CopyReplace(PostReplaceFormat2, PostReplaceFormat, BufferSize, L"%H", L"%%H");\r
2337 ASSERT_EFI_ERROR(Status);\r
2338 Status = CopyReplace(PostReplaceFormat, PostReplaceFormat2, BufferSize, L"%B", L"%%B");\r
2339 ASSERT_EFI_ERROR(Status);\r
2340 Status = CopyReplace(PostReplaceFormat2, PostReplaceFormat, BufferSize, L"%V", L"%%V");\r
2341 ASSERT_EFI_ERROR(Status);\r
2342\r
2343 //\r
2344 // Use the last buffer from replacing to print from...\r
2345 //\r
2346 Return = UnicodeVSPrint (PostReplaceFormat2, BufferSize, PostReplaceFormat, Marker);\r
2347\r
2348 FreePool(PostReplaceFormat);\r
b1f95a06 2349\r
2350 if (Col != -1 && Row != -1) {\r
b1f95a06 2351 Status = gST->ConOut->SetCursorPosition(gST->ConOut, Col, Row);\r
2352 ASSERT_EFI_ERROR(Status);\r
975136ab 2353 }\r
2354\r
2355 NormalAttribute = gST->ConOut->Mode->Attribute;\r
2356 FormatWalker = PostReplaceFormat2;\r
2247dde4 2357 while (*FormatWalker != CHAR_NULL) {\r
975136ab 2358 //\r
2359 // Find the next attribute change request\r
2360 //\r
2361 ResumeLocation = StrStr(FormatWalker, L"%");\r
2362 if (ResumeLocation != NULL) {\r
2247dde4 2363 *ResumeLocation = CHAR_NULL;\r
975136ab 2364 }\r
2365 //\r
2366 // print the current FormatWalker string\r
2367 //\r
e2f8297f 2368 Status = InternalPrintTo(FormatWalker);\r
b1f95a06 2369 ASSERT_EFI_ERROR(Status);\r
975136ab 2370 //\r
2371 // update the attribute\r
2372 //\r
2373 if (ResumeLocation != NULL) {\r
2374 switch (*(ResumeLocation+1)) {\r
2375 case (L'N'):\r
2376 gST->ConOut->SetAttribute(gST->ConOut, NormalAttribute);\r
2377 break;\r
2378 case (L'E'):\r
2379 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_YELLOW, ((NormalAttribute&(BIT4|BIT5|BIT6))>>4)));\r
2380 break;\r
2381 case (L'H'):\r
2382 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_WHITE, ((NormalAttribute&(BIT4|BIT5|BIT6))>>4)));\r
2383 break;\r
2384 case (L'B'):\r
2385 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_BLUE, ((NormalAttribute&(BIT4|BIT5|BIT6))>>4)));\r
2386 break;\r
2387 case (L'V'):\r
2388 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_GREEN, ((NormalAttribute&(BIT4|BIT5|BIT6))>>4)));\r
2389 break;\r
2390 default:\r
e2f8297f 2391 //\r
2392 // Print a simple '%' symbol\r
2393 //\r
2394 Status = InternalPrintTo(L"%");\r
2395 ASSERT_EFI_ERROR(Status);\r
2396 ResumeLocation = ResumeLocation - 1;\r
975136ab 2397 break;\r
2398 }\r
2399 } else {\r
2400 //\r
2401 // reset to normal now...\r
2402 //\r
2403 gST->ConOut->SetAttribute(gST->ConOut, NormalAttribute);\r
2404 break;\r
2405 }\r
2406\r
2407 //\r
2408 // update FormatWalker to Resume + 2 (skip the % and the indicator)\r
2409 //\r
2410 FormatWalker = ResumeLocation + 2;\r
2411 }\r
b1f95a06 2412\r
975136ab 2413 FreePool(PostReplaceFormat2);\r
2414\r
b1f95a06 2415 return (Return);\r
5f7431d0 2416}\r
2247dde4 2417\r
2418/**\r
2419 Print at a specific location on the screen.\r
2420\r
e2f8297f 2421 This function will move the cursor to a given screen location and print the specified string.\r
2247dde4 2422 \r
2423 If -1 is specified for either the Row or Col the current screen location for BOTH \r
2424 will be used.\r
2425\r
e2f8297f 2426 If either Row or Col is out of range for the current console, then ASSERT.\r
2427 If Format is NULL, then ASSERT.\r
2247dde4 2428\r
2429 In addition to the standard %-based flags as supported by UefiLib Print() this supports \r
2430 the following additional flags:\r
2431 %N - Set output attribute to normal\r
2432 %H - Set output attribute to highlight\r
2433 %E - Set output attribute to error\r
2434 %B - Set output attribute to blue color\r
2435 %V - Set output attribute to green color\r
2436\r
2437 Note: The background color is controlled by the shell command cls.\r
2438\r
2439 @param[in] Row the row to print at\r
2440 @param[in] Col the column to print at\r
2441 @param[in] Format the format string\r
2442\r
2443 @return the number of characters printed to the screen\r
2444**/\r
2445\r
2446UINTN\r
2447EFIAPI\r
2448ShellPrintEx(\r
2449 IN INT32 Col OPTIONAL,\r
2450 IN INT32 Row OPTIONAL,\r
2451 IN CONST CHAR16 *Format,\r
2452 ...\r
2453 ) \r
2454{\r
2455 VA_LIST Marker;\r
e2f8297f 2456 EFI_STATUS Status;\r
2247dde4 2457 VA_START (Marker, Format);\r
e2f8297f 2458 Status = InternalShellPrintWorker(Col, Row, Format, Marker);\r
2459 VA_END(Marker);\r
2460 return(Status);\r
2247dde4 2461}\r
2462\r
2463/**\r
2464 Print at a specific location on the screen.\r
2465\r
e2f8297f 2466 This function will move the cursor to a given screen location and print the specified string.\r
2247dde4 2467 \r
2468 If -1 is specified for either the Row or Col the current screen location for BOTH \r
e2f8297f 2469 will be used.\r
2247dde4 2470\r
e2f8297f 2471 If either Row or Col is out of range for the current console, then ASSERT.\r
2472 If Format is NULL, then ASSERT.\r
2247dde4 2473\r
2474 In addition to the standard %-based flags as supported by UefiLib Print() this supports \r
2475 the following additional flags:\r
2476 %N - Set output attribute to normal\r
2477 %H - Set output attribute to highlight\r
2478 %E - Set output attribute to error\r
2479 %B - Set output attribute to blue color\r
2480 %V - Set output attribute to green color\r
2481\r
2482 Note: The background color is controlled by the shell command cls.\r
2483\r
2484 @param[in] Row the row to print at\r
2485 @param[in] Col the column to print at\r
2486 @param[in] HiiFormatStringId the format string Id for getting from Hii\r
2487 @param[in] HiiFormatHandle the format string Handle for getting from Hii\r
2488\r
2489 @return the number of characters printed to the screen\r
2490**/\r
2491UINTN\r
2492EFIAPI\r
2493ShellPrintHiiEx(\r
2494 IN INT32 Col OPTIONAL,\r
2495 IN INT32 Row OPTIONAL,\r
2496 IN CONST EFI_STRING_ID HiiFormatStringId,\r
2497 IN CONST EFI_HANDLE HiiFormatHandle,\r
2498 ...\r
2499 )\r
2500{\r
2501 VA_LIST Marker;\r
2502 CHAR16 *HiiFormatString;\r
2503 UINTN RetVal;\r
2504\r
2505 VA_START (Marker, HiiFormatHandle);\r
2506 HiiFormatString = HiiGetString(HiiFormatHandle, HiiFormatStringId, NULL);\r
2507 ASSERT(HiiFormatString != NULL);\r
2508\r
2509 RetVal = InternalShellPrintWorker(Col, Row, HiiFormatString, Marker);\r
2510\r
2511 FreePool(HiiFormatString);\r
e2f8297f 2512 VA_END(Marker);\r
2247dde4 2513\r
2514 return (RetVal);\r
2515}\r
2516\r
2517/**\r
2518 Function to determine if a given filename represents a file or a directory.\r
2519\r
2520 @param[in] DirName Path to directory to test.\r
2521\r
2522 @retval EFI_SUCCESS The Path represents a directory\r
2523 @retval EFI_NOT_FOUND The Path does not represent a directory\r
2524 @return other The path failed to open\r
2525**/\r
2526EFI_STATUS\r
2527EFIAPI\r
2528ShellIsDirectory(\r
2529 IN CONST CHAR16 *DirName\r
2530 )\r
2531{\r
2532 EFI_STATUS Status;\r
2533 EFI_FILE_HANDLE Handle;\r
2534\r
2535 Handle = NULL;\r
2536\r
2537 Status = ShellOpenFileByName(DirName, &Handle, EFI_FILE_MODE_READ, 0);\r
2538 if (EFI_ERROR(Status)) {\r
2539 return (Status);\r
2540 }\r
2541\r
2542 if (FileHandleIsDirectory(Handle) == EFI_SUCCESS) {\r
2543 ShellCloseFile(&Handle);\r
2544 return (EFI_SUCCESS);\r
2545 }\r
2546 ShellCloseFile(&Handle);\r
2547 return (EFI_NOT_FOUND);\r
2548}\r
2549\r
36a9d672 2550/**\r
2551 Function to determine if a given filename represents a file.\r
2552\r
2553 @param[in] Name Path to file to test.\r
2554\r
2555 @retval EFI_SUCCESS The Path represents a file.\r
2556 @retval EFI_NOT_FOUND The Path does not represent a file.\r
2557 @retval other The path failed to open.\r
2558**/\r
2559EFI_STATUS\r
2560EFIAPI\r
2561ShellIsFile(\r
2562 IN CONST CHAR16 *Name\r
2563 )\r
2564{\r
2565 EFI_STATUS Status;\r
2566 EFI_FILE_HANDLE Handle;\r
2567\r
2568 Handle = NULL;\r
2569\r
2570 Status = ShellOpenFileByName(Name, &Handle, EFI_FILE_MODE_READ, 0);\r
2571 if (EFI_ERROR(Status)) {\r
2572 return (Status);\r
2573 }\r
2574\r
2575 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {\r
2576 ShellCloseFile(&Handle);\r
2577 return (EFI_SUCCESS);\r
2578 }\r
2579 ShellCloseFile(&Handle);\r
2580 return (EFI_NOT_FOUND);\r
2581}\r
2582\r
125c2cf4 2583/**\r
2584 Function to determine whether a string is decimal or hex representation of a number \r
2585 and return the number converted from the string.\r
2586\r
2587 @param[in] String String representation of a number\r
2588\r
2589 @retval all the number\r
2590**/\r
2591UINTN\r
2592EFIAPI\r
2593ShellStrToUintn(\r
2594 IN CONST CHAR16 *String\r
2595 )\r
2596{\r
2597 CONST CHAR16 *Walker;\r
2598 for (Walker = String; Walker != NULL && *Walker != CHAR_NULL && *Walker == L' '; Walker = Walker + 1);\r
2599 if (StrnCmp(Walker, L"0x", 2) == 0 || StrnCmp(Walker, L"0X", 2) == 0){\r
2600 return (StrHexToUintn(Walker));\r
2601 }\r
2602 return (StrDecimalToUintn(Walker));\r
2603}\r
2604\r
2605/**\r
2606 Safely append with automatic string resizing given length of Destination and\r
2607 desired length of copy from Source.\r
2608\r
2609 append the first D characters of Source to the end of Destination, where D is\r
2610 the lesser of Count and the StrLen() of Source. If appending those D characters\r
2611 will fit within Destination (whose Size is given as CurrentSize) and\r
2612 still leave room for a null terminator, then those characters are appended,\r
2613 starting at the original terminating null of Destination, and a new terminating\r
2614 null is appended.\r
2615\r
2616 If appending D characters onto Destination will result in a overflow of the size\r
2617 given in CurrentSize the string will be grown such that the copy can be performed\r
2618 and CurrentSize will be updated to the new size.\r
2619\r
2620 If Source is NULL, there is nothing to append, just return the current buffer in\r
2621 Destination.\r
2622\r
2623 if Destination is NULL, then ASSERT()\r
2624 if Destination's current length (including NULL terminator) is already more then\r
2625 CurrentSize, then ASSERT()\r
2626\r
2627 @param[in,out] Destination The String to append onto\r
2628 @param[in,out] CurrentSize on call the number of bytes in Destination. On\r
2629 return possibly the new size (still in bytes). if NULL\r
2630 then allocate whatever is needed.\r
2631 @param[in] Source The String to append from\r
2632 @param[in] Count Maximum number of characters to append. if 0 then\r
2633 all are appended.\r
2634\r
2635 @return Destination return the resultant string.\r
2636**/\r
2637CHAR16*\r
2638EFIAPI\r
2639StrnCatGrow (\r
2640 IN OUT CHAR16 **Destination,\r
2641 IN OUT UINTN *CurrentSize,\r
2642 IN CONST CHAR16 *Source,\r
2643 IN UINTN Count\r
2644 )\r
2645{\r
2646 UINTN DestinationStartSize;\r
2647 UINTN NewSize;\r
2648\r
2649 //\r
2650 // ASSERTs\r
2651 //\r
2652 ASSERT(Destination != NULL);\r
2653\r
2654 //\r
2655 // If there's nothing to do then just return Destination\r
2656 //\r
2657 if (Source == NULL) {\r
2658 return (*Destination);\r
2659 }\r
2660\r
2661 //\r
2662 // allow for un-initialized pointers, based on size being 0\r
2663 //\r
2664 if (CurrentSize != NULL && *CurrentSize == 0) {\r
2665 *Destination = NULL;\r
2666 }\r
2667\r
2668 //\r
2669 // allow for NULL pointers address as Destination\r
2670 //\r
2671 if (*Destination != NULL) {\r
2672 ASSERT(CurrentSize != 0);\r
2673 DestinationStartSize = StrSize(*Destination);\r
2674 ASSERT(DestinationStartSize <= *CurrentSize);\r
2675 } else {\r
2676 DestinationStartSize = 0;\r
2677// ASSERT(*CurrentSize == 0);\r
2678 }\r
2679\r
2680 //\r
2681 // Append all of Source?\r
2682 //\r
2683 if (Count == 0) {\r
2684 Count = StrLen(Source);\r
2685 }\r
2686\r
2687 //\r
2688 // Test and grow if required\r
2689 //\r
2690 if (CurrentSize != NULL) {\r
2691 NewSize = *CurrentSize;\r
2692 while (NewSize < (DestinationStartSize + (Count*sizeof(CHAR16)))) {\r
2693 NewSize += 2 * Count * sizeof(CHAR16);\r
2694 }\r
2695 *Destination = ReallocatePool(*CurrentSize, NewSize, *Destination);\r
2696 *CurrentSize = NewSize;\r
2697 } else {\r
2698 *Destination = AllocateZeroPool((Count+1)*sizeof(CHAR16));\r
2699 }\r
2700\r
2701 //\r
2702 // Now use standard StrnCat on a big enough buffer\r
2703 //\r
2704 return StrnCat(*Destination, Source, Count);\r
2705}\r