]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - ShellPkg/Library/UefiShellLib/UefiShellLib.c
fix K8 coding issues.
[mirror_edk2.git] / ShellPkg / Library / UefiShellLib / UefiShellLib.c
... / ...
CommitLineData
1/** @file\r
2 Provides interface to shell functionality for shell commands and applications.\r
3\r
4 Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>\r
5 This program and the accompanying materials\r
6 are licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution. The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php\r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include "UefiShellLib.h"\r
16\r
17#define MAX_FILE_NAME_LEN 522 // (20 * (6+5+2))+1) unicode characters from EFI FAT spec (doubled for bytes)\r
18#define FIND_XXXXX_FILE_BUFFER_SIZE (SIZE_OF_EFI_FILE_INFO + MAX_FILE_NAME_LEN)\r
19\r
20//\r
21// This is not static since it's extern in the .h file\r
22//\r
23SHELL_PARAM_ITEM EmptyParamList[] = {\r
24 {NULL, TypeMax}\r
25 };\r
26\r
27//\r
28// Static file globals for the shell library\r
29//\r
30STATIC EFI_SHELL_ENVIRONMENT2 *mEfiShellEnvironment2;\r
31STATIC EFI_SHELL_INTERFACE *mEfiShellInterface;\r
32STATIC EFI_SHELL_PROTOCOL *mEfiShellProtocol;\r
33STATIC EFI_SHELL_PARAMETERS_PROTOCOL *mEfiShellParametersProtocol;\r
34STATIC EFI_HANDLE mEfiShellEnvironment2Handle;\r
35STATIC FILE_HANDLE_FUNCTION_MAP FileFunctionMap;\r
36STATIC UINTN mTotalParameterCount;\r
37STATIC CHAR16 *mPostReplaceFormat;\r
38STATIC CHAR16 *mPostReplaceFormat2;\r
39\r
40/**\r
41 Check if a Unicode character is a hexadecimal character.\r
42\r
43 This internal function checks if a Unicode character is a\r
44 decimal character. The valid hexadecimal character is\r
45 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.\r
46\r
47\r
48 @param Char The character to check against.\r
49\r
50 @retval TRUE If the Char is a hexadecmial character.\r
51 @retval FALSE If the Char is not a hexadecmial character.\r
52\r
53**/\r
54BOOLEAN\r
55EFIAPI\r
56ShellIsHexaDecimalDigitCharacter (\r
57 IN CHAR16 Char\r
58 ) {\r
59 return (BOOLEAN) ((Char >= L'0' && Char <= L'9') || (Char >= L'A' && Char <= L'F') || (Char >= L'a' && Char <= L'f'));\r
60}\r
61\r
62/**\r
63 helper function to find ShellEnvironment2 for constructor\r
64**/\r
65EFI_STATUS\r
66EFIAPI\r
67ShellFindSE2 (\r
68 IN EFI_HANDLE ImageHandle\r
69 ) {\r
70 EFI_STATUS Status;\r
71 EFI_HANDLE *Buffer;\r
72 UINTN BufferSize;\r
73 UINTN HandleIndex;\r
74\r
75 BufferSize = 0;\r
76 Buffer = NULL;\r
77 Status = gBS->OpenProtocol(ImageHandle,\r
78 &gEfiShellEnvironment2Guid,\r
79 (VOID **)&mEfiShellEnvironment2,\r
80 ImageHandle,\r
81 NULL,\r
82 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
83 );\r
84 //\r
85 // look for the mEfiShellEnvironment2 protocol at a higher level\r
86 //\r
87 if (EFI_ERROR (Status) || !(CompareGuid (&mEfiShellEnvironment2->SESGuid, &gEfiShellEnvironment2ExtGuid) != FALSE)){\r
88 //\r
89 // figure out how big of a buffer we need.\r
90 //\r
91 Status = gBS->LocateHandle (ByProtocol,\r
92 &gEfiShellEnvironment2Guid,\r
93 NULL, // ignored for ByProtocol\r
94 &BufferSize,\r
95 Buffer\r
96 );\r
97 //\r
98 // maybe it's not there???\r
99 //\r
100 if (Status == EFI_BUFFER_TOO_SMALL) {\r
101 Buffer = (EFI_HANDLE*)AllocatePool(BufferSize);\r
102 ASSERT(Buffer != NULL);\r
103 Status = gBS->LocateHandle (ByProtocol,\r
104 &gEfiShellEnvironment2Guid,\r
105 NULL, // ignored for ByProtocol\r
106 &BufferSize,\r
107 Buffer\r
108 );\r
109 }\r
110 if (!EFI_ERROR (Status) && Buffer != NULL) {\r
111 //\r
112 // now parse the list of returned handles\r
113 //\r
114 Status = EFI_NOT_FOUND;\r
115 for (HandleIndex = 0; HandleIndex < (BufferSize/sizeof(Buffer[0])); HandleIndex++) {\r
116 Status = gBS->OpenProtocol(Buffer[HandleIndex],\r
117 &gEfiShellEnvironment2Guid,\r
118 (VOID **)&mEfiShellEnvironment2,\r
119 ImageHandle,\r
120 NULL,\r
121 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
122 );\r
123 if (CompareGuid (&mEfiShellEnvironment2->SESGuid, &gEfiShellEnvironment2ExtGuid) != FALSE) {\r
124 mEfiShellEnvironment2Handle = Buffer[HandleIndex];\r
125 Status = EFI_SUCCESS;\r
126 break;\r
127 }\r
128 }\r
129 }\r
130 }\r
131 if (Buffer != NULL) {\r
132 FreePool (Buffer);\r
133 }\r
134 return (Status);\r
135}\r
136\r
137EFI_STATUS\r
138EFIAPI\r
139ShellLibConstructorWorker (\r
140 IN EFI_HANDLE ImageHandle,\r
141 IN EFI_SYSTEM_TABLE *SystemTable\r
142 ) {\r
143 EFI_STATUS Status;\r
144\r
145 ASSERT(PcdGet16 (PcdShellPrintBufferSize) < PcdGet32 (PcdMaximumUnicodeStringLength));\r
146 mPostReplaceFormat = AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize));\r
147 ASSERT (mPostReplaceFormat != NULL);\r
148 mPostReplaceFormat2 = AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize));\r
149 ASSERT (mPostReplaceFormat2 != NULL);\r
150\r
151 //\r
152 // Set the parameter count to an invalid number\r
153 //\r
154 mTotalParameterCount = (UINTN)(-1);\r
155\r
156 //\r
157 // UEFI 2.0 shell interfaces (used preferentially)\r
158 //\r
159 Status = gBS->OpenProtocol(ImageHandle,\r
160 &gEfiShellProtocolGuid,\r
161 (VOID **)&mEfiShellProtocol,\r
162 ImageHandle,\r
163 NULL,\r
164 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
165 );\r
166 if (EFI_ERROR(Status)) {\r
167 mEfiShellProtocol = NULL;\r
168 }\r
169 Status = gBS->OpenProtocol(ImageHandle,\r
170 &gEfiShellParametersProtocolGuid,\r
171 (VOID **)&mEfiShellParametersProtocol,\r
172 ImageHandle,\r
173 NULL,\r
174 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
175 );\r
176 if (EFI_ERROR(Status)) {\r
177 mEfiShellParametersProtocol = NULL;\r
178 }\r
179\r
180 if (mEfiShellParametersProtocol == NULL || mEfiShellProtocol == NULL) {\r
181 //\r
182 // Moved to seperate function due to complexity\r
183 //\r
184 Status = ShellFindSE2(ImageHandle);\r
185\r
186 if (EFI_ERROR(Status)) {\r
187 DEBUG((DEBUG_ERROR, "Status: 0x%08x\r\n", Status));\r
188 mEfiShellEnvironment2 = NULL;\r
189 }\r
190 Status = gBS->OpenProtocol(ImageHandle,\r
191 &gEfiShellInterfaceGuid,\r
192 (VOID **)&mEfiShellInterface,\r
193 ImageHandle,\r
194 NULL,\r
195 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
196 );\r
197 if (EFI_ERROR(Status)) {\r
198 mEfiShellInterface = NULL;\r
199 }\r
200 }\r
201 //\r
202 // only success getting 2 of either the old or new, but no 1/2 and 1/2\r
203 //\r
204 if ((mEfiShellEnvironment2 != NULL && mEfiShellInterface != NULL) ||\r
205 (mEfiShellProtocol != NULL && mEfiShellParametersProtocol != NULL) ) {\r
206 if (mEfiShellProtocol != NULL) {\r
207 FileFunctionMap.GetFileInfo = mEfiShellProtocol->GetFileInfo;\r
208 FileFunctionMap.SetFileInfo = mEfiShellProtocol->SetFileInfo;\r
209 FileFunctionMap.ReadFile = mEfiShellProtocol->ReadFile;\r
210 FileFunctionMap.WriteFile = mEfiShellProtocol->WriteFile;\r
211 FileFunctionMap.CloseFile = mEfiShellProtocol->CloseFile;\r
212 FileFunctionMap.DeleteFile = mEfiShellProtocol->DeleteFile;\r
213 FileFunctionMap.GetFilePosition = mEfiShellProtocol->GetFilePosition;\r
214 FileFunctionMap.SetFilePosition = mEfiShellProtocol->SetFilePosition;\r
215 FileFunctionMap.FlushFile = mEfiShellProtocol->FlushFile;\r
216 FileFunctionMap.GetFileSize = mEfiShellProtocol->GetFileSize;\r
217 } else {\r
218 FileFunctionMap.GetFileInfo = FileHandleGetInfo;\r
219 FileFunctionMap.SetFileInfo = FileHandleSetInfo;\r
220 FileFunctionMap.ReadFile = FileHandleRead;\r
221 FileFunctionMap.WriteFile = FileHandleWrite;\r
222 FileFunctionMap.CloseFile = FileHandleClose;\r
223 FileFunctionMap.DeleteFile = FileHandleDelete;\r
224 FileFunctionMap.GetFilePosition = FileHandleGetPosition;\r
225 FileFunctionMap.SetFilePosition = FileHandleSetPosition;\r
226 FileFunctionMap.FlushFile = FileHandleFlush;\r
227 FileFunctionMap.GetFileSize = FileHandleGetSize;\r
228 }\r
229 return (EFI_SUCCESS);\r
230 }\r
231 return (EFI_NOT_FOUND);\r
232}\r
233/**\r
234 Constructor for the Shell library.\r
235\r
236 Initialize the library and determine if the underlying is a UEFI Shell 2.0 or an EFI shell.\r
237\r
238 @param ImageHandle the image handle of the process\r
239 @param SystemTable the EFI System Table pointer\r
240\r
241 @retval EFI_SUCCESS the initialization was complete sucessfully\r
242 @return others an error ocurred during initialization\r
243**/\r
244EFI_STATUS\r
245EFIAPI\r
246ShellLibConstructor (\r
247 IN EFI_HANDLE ImageHandle,\r
248 IN EFI_SYSTEM_TABLE *SystemTable\r
249 ) {\r
250\r
251\r
252 mEfiShellEnvironment2 = NULL;\r
253 mEfiShellProtocol = NULL;\r
254 mEfiShellParametersProtocol = NULL;\r
255 mEfiShellInterface = NULL;\r
256 mEfiShellEnvironment2Handle = NULL;\r
257 mPostReplaceFormat = NULL;\r
258 mPostReplaceFormat2 = NULL;\r
259\r
260 //\r
261 // verify that auto initialize is not set false\r
262 //\r
263 if (PcdGetBool(PcdShellLibAutoInitialize) == 0) {\r
264 return (EFI_SUCCESS);\r
265 }\r
266\r
267 return (ShellLibConstructorWorker(ImageHandle, SystemTable));\r
268}\r
269\r
270/**\r
271 Destructory for the library. free any resources.\r
272**/\r
273EFI_STATUS\r
274EFIAPI\r
275ShellLibDestructor (\r
276 IN EFI_HANDLE ImageHandle,\r
277 IN EFI_SYSTEM_TABLE *SystemTable\r
278 ) {\r
279 if (mEfiShellEnvironment2 != NULL) {\r
280 gBS->CloseProtocol(mEfiShellEnvironment2Handle==NULL?ImageHandle:mEfiShellEnvironment2Handle,\r
281 &gEfiShellEnvironment2Guid,\r
282 ImageHandle,\r
283 NULL);\r
284 mEfiShellEnvironment2 = NULL;\r
285 }\r
286 if (mEfiShellInterface != NULL) {\r
287 gBS->CloseProtocol(ImageHandle,\r
288 &gEfiShellInterfaceGuid,\r
289 ImageHandle,\r
290 NULL);\r
291 mEfiShellInterface = NULL;\r
292 }\r
293 if (mEfiShellProtocol != NULL) {\r
294 gBS->CloseProtocol(ImageHandle,\r
295 &gEfiShellProtocolGuid,\r
296 ImageHandle,\r
297 NULL);\r
298 mEfiShellProtocol = NULL;\r
299 }\r
300 if (mEfiShellParametersProtocol != NULL) {\r
301 gBS->CloseProtocol(ImageHandle,\r
302 &gEfiShellParametersProtocolGuid,\r
303 ImageHandle,\r
304 NULL);\r
305 mEfiShellParametersProtocol = NULL;\r
306 }\r
307 mEfiShellEnvironment2Handle = NULL;\r
308\r
309 if (mPostReplaceFormat != NULL) {\r
310 FreePool(mPostReplaceFormat);\r
311 }\r
312 if (mPostReplaceFormat2 != NULL) {\r
313 FreePool(mPostReplaceFormat2);\r
314 }\r
315 mPostReplaceFormat = NULL;\r
316 mPostReplaceFormat2 = NULL;\r
317\r
318 return (EFI_SUCCESS);\r
319}\r
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
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
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
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
374 ) {\r
375 return (FileFunctionMap.GetFileInfo(FileHandle));\r
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
401 ) {\r
402 return (FileFunctionMap.SetFileInfo(FileHandle, FileInfo));\r
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
443 ) {\r
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
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
483 (VOID**)&EfiSimpleFileSystemProtocol,\r
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
500 //\r
501 // For file system access each node should be a file path component\r
502 //\r
503 if (DevicePathType (*FilePath) != MEDIA_DEVICE_PATH ||\r
504 DevicePathSubType (*FilePath) != MEDIA_FILEPATH_DP\r
505 ) {\r
506 FileHandle = NULL;\r
507 return (EFI_INVALID_PARAMETER);\r
508 }\r
509 //\r
510 // Open this file path node\r
511 //\r
512 LastHandle = *FileHandle;\r
513 *FileHandle = NULL;\r
514\r
515 //\r
516 // Try to test opening an existing file\r
517 //\r
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
525\r
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
530 Status = LastHandle->Open (\r
531 LastHandle,\r
532 FileHandle,\r
533 ((FILEPATH_DEVICE_PATH*)*FilePath)->PathName,\r
534 OpenMode,\r
535 Attributes\r
536 );\r
537 }\r
538 //\r
539 // Close the last node\r
540 //\r
541 LastHandle->Close (LastHandle);\r
542\r
543 if (EFI_ERROR(Status)) {\r
544 return (Status);\r
545 }\r
546\r
547 //\r
548 // Get the next node\r
549 //\r
550 *FilePath = NextDevicePathNode (*FilePath);\r
551 }\r
552 return (EFI_SUCCESS);\r
553}\r
554\r
555/**\r
556 This function will open a file or directory referenced by filename.\r
557\r
558 If return is EFI_SUCCESS, the Filehandle is the opened file's handle;\r
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
589 IN CONST CHAR16 *FileName,\r
590 OUT EFI_FILE_HANDLE *FileHandle,\r
591 IN UINT64 OpenMode,\r
592 IN UINT64 Attributes\r
593 ) {\r
594 EFI_HANDLE DeviceHandle;\r
595 EFI_DEVICE_PATH_PROTOCOL *FilePath;\r
596 EFI_STATUS Status;\r
597 EFI_FILE_INFO *FileInfo;\r
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
608 Status = mEfiShellProtocol->OpenFileByName(FileName,\r
609 FileHandle,\r
610 OpenMode);\r
611 if (!EFI_ERROR(Status) && ((OpenMode & EFI_FILE_MODE_CREATE) != 0)){\r
612 FileInfo = FileFunctionMap.GetFileInfo(*FileHandle);\r
613 ASSERT(FileInfo != NULL);\r
614 FileInfo->Attribute = Attributes;\r
615 Status = FileFunctionMap.SetFileInfo(*FileHandle, FileInfo);\r
616 FreePool(FileInfo);\r
617 }\r
618 return (Status);\r
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
626 FilePath = mEfiShellEnvironment2->NameToPath ((CHAR16*)FileName);\r
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
667 IN CONST CHAR16 *DirectoryName,\r
668 OUT EFI_FILE_HANDLE *FileHandle\r
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
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
691 bytes from the file at the file's current position and returns them in Buffer.\r
692 If the read goes beyond the end of the file, the read length is truncated to the\r
693 end of the file. The file's current position is increased by the number of bytes\r
694 returned. If FileHandle is a directory, the function reads the directory entry\r
695 at the file's current position and returns the entry in Buffer. If the Buffer\r
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
722 ) {\r
723 return (FileFunctionMap.ReadFile(FileHandle, BufferSize, Buffer));\r
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
733 has been a data error during the write attempt (such as "volume space full").\r
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
757 ) {\r
758 return (FileFunctionMap.WriteFile(FileHandle, BufferSize, Buffer));\r
759}\r
760\r
761/**\r
762 Close an open file handle.\r
763\r
764 This function closes a specified file handle. All "dirty" cached file data is\r
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
776 ) {\r
777 return (FileFunctionMap.CloseFile(*FileHandle));\r
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
798 ) {\r
799 return (FileFunctionMap.DeleteFile(*FileHandle));\r
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
826 ) {\r
827 return (FileFunctionMap.SetFilePosition(FileHandle, Position));\r
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
850 ) {\r
851 return (FileFunctionMap.GetFilePosition(FileHandle, Position));\r
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
871 ) {\r
872 return (FileFunctionMap.FlushFile(FileHandle));\r
873}\r
874\r
875/**\r
876 Retrieves the first file from a directory\r
877\r
878 This function opens a directory and gets the first file's info in the\r
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
897 OUT EFI_FILE_INFO **Buffer\r
898 ) {\r
899 //\r
900 // pass to file handle lib\r
901 //\r
902 return (FileHandleFindFirstFile(DirHandle, Buffer));\r
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
929 ) {\r
930 //\r
931 // pass to file handle lib\r
932 //\r
933 return (FileHandleFindNextFile(DirHandle, Buffer, NoFile));\r
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
941 This function extracts the file size info from the FileHandle's EFI_FILE_INFO\r
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
955 ) {\r
956 return (FileFunctionMap.GetFileSize(FileHandle, Size));\r
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
1006 IN CONST CHAR16 *EnvKey\r
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
1024 return (mEfiShellEnvironment2->GetEnv((CHAR16*)EnvKey));\r
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
1131 If the DeviceName is NULL, it returns the current device's current directory\r
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
1225 LIST_ENTRY Link;\r
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
1242 @param[in] FileList the EFI shell list type\r
1243 @param[in,out] ListHead the list to add to\r
1244\r
1245 @retval the resultant head of the double linked new format list;\r
1246**/\r
1247LIST_ENTRY*\r
1248EFIAPI\r
1249InternalShellConvertFileListType (\r
1250 IN LIST_ENTRY *FileList,\r
1251 IN OUT LIST_ENTRY *ListHead\r
1252 )\r
1253{\r
1254 SHELL_FILE_ARG *OldInfo;\r
1255 LIST_ENTRY *Link;\r
1256 EFI_SHELL_FILE_INFO_NO_CONST *NewInfo;\r
1257\r
1258 //\r
1259 // ASSERTs\r
1260 //\r
1261 ASSERT(FileList != NULL);\r
1262 ASSERT(ListHead != NULL);\r
1263\r
1264 //\r
1265 // enumerate through each member of the old list and copy\r
1266 //\r
1267 for (Link = FileList->ForwardLink; Link != FileList; Link = Link->ForwardLink) {\r
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
1289 // old shell checks for 0 not NULL\r
1290 OldInfo->Handle = 0;\r
1291\r
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
1316 InsertTailList(ListHead, &NewInfo->Link);\r
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
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
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
1354 LIST_ENTRY mOldStyleFileList;\r
1355\r
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
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
1373 Status = mEfiShellProtocol->OpenFileList(Arg,\r
1374 OpenMode,\r
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
1382 }\r
1383\r
1384 //\r
1385 // ASSERT that we must have EFI shell\r
1386 //\r
1387 ASSERT(mEfiShellEnvironment2 != NULL);\r
1388\r
1389 //\r
1390 // make sure the list head is initialized\r
1391 //\r
1392 InitializeListHead(&mOldStyleFileList);\r
1393\r
1394 //\r
1395 // Get the EFI Shell list of files\r
1396 //\r
1397 Status = mEfiShellEnvironment2->FileMetaArg(Arg, &mOldStyleFileList);\r
1398 if (EFI_ERROR(Status)) {\r
1399 *ListHead = NULL;\r
1400 return (Status);\r
1401 }\r
1402\r
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
1410 //\r
1411 // Convert that to equivalent of UEFI Shell 2.0 structure\r
1412 //\r
1413 InternalShellConvertFileListType(&mOldStyleFileList, &(*ListHead)->Link);\r
1414\r
1415 //\r
1416 // Free the EFI Shell version that was converted.\r
1417 //\r
1418 mEfiShellEnvironment2->FreeFileList(&mOldStyleFileList);\r
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
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
1454 for ( Node = GetFirstNode(&(*ListHead)->Link)\r
1455 ; IsListEmpty(&(*ListHead)->Link) == FALSE\r
1456 ; Node = GetFirstNode(&(*ListHead)->Link)) {\r
1457 RemoveEntryList(Node);\r
1458 ((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->Handle->Close(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->Handle);\r
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
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
1492 UINTN Size;\r
1493 CHAR16 *TempChar;\r
1494\r
1495 RetVal = NULL;\r
1496\r
1497 Path = ShellGetEnvironmentVariable(L"cwd");\r
1498 if (Path != NULL) {\r
1499 Size = StrSize(Path);\r
1500 Size += StrSize(FileName);\r
1501 TestPath = AllocateZeroPool(Size);\r
1502 StrCpy(TestPath, Path);\r
1503 StrCat(TestPath, FileName);\r
1504 Status = ShellOpenFileByName(TestPath, &Handle, EFI_FILE_MODE_READ, 0);\r
1505 if (!EFI_ERROR(Status)){\r
1506 RetVal = StrnCatGrow(&RetVal, NULL, TestPath, 0);\r
1507 ShellCloseFile(&Handle);\r
1508 FreePool(TestPath);\r
1509 return (RetVal);\r
1510 }\r
1511 FreePool(TestPath);\r
1512 }\r
1513 Path = ShellGetEnvironmentVariable(L"path");\r
1514 if (Path != NULL) {\r
1515 Size = StrSize(Path);\r
1516 Size += StrSize(FileName);\r
1517 TestPath = AllocateZeroPool(Size);\r
1518 Walker = (CHAR16*)Path;\r
1519 do {\r
1520 CopyMem(TestPath, Walker, StrSize(Walker));\r
1521 TempChar = StrStr(TestPath, L";");\r
1522 if (TempChar != NULL) {\r
1523 *TempChar = CHAR_NULL;\r
1524 }\r
1525 StrCat(TestPath, FileName);\r
1526 if (StrStr(Walker, L";") != NULL) {\r
1527 Walker = StrStr(Walker, L";") + 1;\r
1528 } else {\r
1529 Walker = NULL;\r
1530 }\r
1531 Status = ShellOpenFileByName(TestPath, &Handle, EFI_FILE_MODE_READ, 0);\r
1532 if (!EFI_ERROR(Status)){\r
1533 RetVal = StrnCatGrow(&RetVal, NULL, TestPath, 0);\r
1534 ShellCloseFile(&Handle);\r
1535 break;\r
1536 }\r
1537 } while (Walker != NULL && Walker[0] != CHAR_NULL);\r
1538 FreePool(TestPath);\r
1539 }\r
1540 return (RetVal);\r
1541}\r
1542\r
1543/**\r
1544 Find a file by searching the CWD and then the path with a variable set of file\r
1545 extensions. If the file is not found it will append each extension in the list\r
1546 in the order provided and return the first one that is successful.\r
1547\r
1548 If FileName is NULL, then ASSERT.\r
1549 If FileExtension is NULL, then behavior is identical to ShellFindFilePath.\r
1550\r
1551 If the return value is not NULL then the memory must be caller freed.\r
1552\r
1553 @param[in] FileName Filename string.\r
1554 @param[in] FileExtension Semi-colon delimeted list of possible extensions.\r
1555\r
1556 @retval NULL The file was not found.\r
1557 @retval !NULL The path to the file.\r
1558**/\r
1559CHAR16 *\r
1560EFIAPI\r
1561ShellFindFilePathEx (\r
1562 IN CONST CHAR16 *FileName,\r
1563 IN CONST CHAR16 *FileExtension\r
1564 )\r
1565{\r
1566 CHAR16 *TestPath;\r
1567 CHAR16 *RetVal;\r
1568 CONST CHAR16 *ExtensionWalker;\r
1569 UINTN Size;\r
1570 CHAR16 *TempChar;\r
1571\r
1572 ASSERT(FileName != NULL);\r
1573 if (FileExtension == NULL) {\r
1574 return (ShellFindFilePath(FileName));\r
1575 }\r
1576 RetVal = ShellFindFilePath(FileName);\r
1577 if (RetVal != NULL) {\r
1578 return (RetVal);\r
1579 }\r
1580 Size = StrSize(FileName);\r
1581 Size += StrSize(FileExtension);\r
1582 TestPath = AllocateZeroPool(Size);\r
1583 for (ExtensionWalker = FileExtension ; ; ExtensionWalker = StrStr(ExtensionWalker, L";") + 1 ){\r
1584 StrCpy(TestPath, FileName);\r
1585 if (ExtensionWalker != NULL) {\r
1586 StrCat(TestPath, ExtensionWalker);\r
1587 }\r
1588 TempChar = StrStr(TestPath, L";");\r
1589 if (TempChar != NULL) {\r
1590 *TempChar = CHAR_NULL;\r
1591 }\r
1592 RetVal = ShellFindFilePath(TestPath);\r
1593 if (RetVal != NULL) {\r
1594 break;\r
1595 }\r
1596 //\r
1597 // Must be after first loop...\r
1598 //\r
1599 if (StrStr(ExtensionWalker, L";") == NULL) {\r
1600 break;\r
1601 }\r
1602 }\r
1603 FreePool(TestPath);\r
1604 return (RetVal);\r
1605}\r
1606\r
1607typedef struct {\r
1608 LIST_ENTRY Link;\r
1609 CHAR16 *Name;\r
1610 ParamType Type;\r
1611 CHAR16 *Value;\r
1612 UINTN OriginalPosition;\r
1613} SHELL_PARAM_PACKAGE;\r
1614\r
1615/**\r
1616 Checks the list of valid arguments and returns TRUE if the item was found. If the\r
1617 return value is TRUE then the type parameter is set also.\r
1618\r
1619 if CheckList is NULL then ASSERT();\r
1620 if Name is NULL then ASSERT();\r
1621 if Type is NULL then ASSERT();\r
1622\r
1623 @param Type pointer to type of parameter if it was found\r
1624 @param Name pointer to Name of parameter found\r
1625 @param CheckList List to check against\r
1626\r
1627 @retval TRUE the Parameter was found. Type is valid.\r
1628 @retval FALSE the Parameter was not found. Type is not valid.\r
1629**/\r
1630BOOLEAN\r
1631EFIAPI\r
1632InternalIsOnCheckList (\r
1633 IN CONST CHAR16 *Name,\r
1634 IN CONST SHELL_PARAM_ITEM *CheckList,\r
1635 OUT ParamType *Type\r
1636 ) {\r
1637 SHELL_PARAM_ITEM *TempListItem;\r
1638\r
1639 //\r
1640 // ASSERT that all 3 pointer parameters aren't NULL\r
1641 //\r
1642 ASSERT(CheckList != NULL);\r
1643 ASSERT(Type != NULL);\r
1644 ASSERT(Name != NULL);\r
1645\r
1646 //\r
1647 // question mark and page break mode are always supported\r
1648 //\r
1649 if ((StrCmp(Name, L"-?") == 0) ||\r
1650 (StrCmp(Name, L"-b") == 0)\r
1651 ) {\r
1652 return (TRUE);\r
1653 }\r
1654\r
1655 //\r
1656 // Enumerate through the list\r
1657 //\r
1658 for (TempListItem = (SHELL_PARAM_ITEM*)CheckList ; TempListItem->Name != NULL ; TempListItem++) {\r
1659 //\r
1660 // If the Type is TypeStart only check the first characters of the passed in param\r
1661 // If it matches set the type and return TRUE\r
1662 //\r
1663 if (TempListItem->Type == TypeStart && StrnCmp(Name, TempListItem->Name, StrLen(TempListItem->Name)) == 0) {\r
1664 *Type = TempListItem->Type;\r
1665 return (TRUE);\r
1666 } else if (StrCmp(Name, TempListItem->Name) == 0) {\r
1667 *Type = TempListItem->Type;\r
1668 return (TRUE);\r
1669 }\r
1670 }\r
1671\r
1672 return (FALSE);\r
1673}\r
1674/**\r
1675 Checks the string for indicators of "flag" status. this is a leading '/', '-', or '+'\r
1676\r
1677 @param Name pointer to Name of parameter found\r
1678\r
1679 @retval TRUE the Parameter is a flag.\r
1680 @retval FALSE the Parameter not a flag\r
1681**/\r
1682BOOLEAN\r
1683EFIAPI\r
1684InternalIsFlag (\r
1685 IN CONST CHAR16 *Name,\r
1686 IN BOOLEAN AlwaysAllowNumbers\r
1687 )\r
1688{\r
1689 //\r
1690 // ASSERT that Name isn't NULL\r
1691 //\r
1692 ASSERT(Name != NULL);\r
1693\r
1694 //\r
1695 // If we accept numbers then dont return TRUE. (they will be values)\r
1696 //\r
1697 if (((Name[0] == L'-' || Name[0] == L'+') && ShellIsHexaDecimalDigitCharacter(Name[1])) && AlwaysAllowNumbers != FALSE) {\r
1698 return (FALSE);\r
1699 }\r
1700\r
1701 //\r
1702 // If the Name has a / or - as the first character return TRUE\r
1703 //\r
1704 if ((Name[0] == L'/') ||\r
1705 (Name[0] == L'-') ||\r
1706 (Name[0] == L'+')\r
1707 ) {\r
1708 return (TRUE);\r
1709 }\r
1710 return (FALSE);\r
1711}\r
1712\r
1713/**\r
1714 Checks the command line arguments passed against the list of valid ones.\r
1715\r
1716 If no initialization is required, then return RETURN_SUCCESS.\r
1717\r
1718 @param CheckList pointer to list of parameters to check\r
1719 @param CheckPackage pointer to pointer to list checked values\r
1720 @param ProblemParam optional pointer to pointer to unicode string for\r
1721 the paramater that caused failure. If used then the\r
1722 caller is responsible for freeing the memory.\r
1723 @param AutoPageBreak will automatically set PageBreakEnabled for "b" parameter\r
1724 @param Argc Count of parameters in Argv\r
1725 @param Argv pointer to array of parameters\r
1726\r
1727 @retval EFI_SUCCESS The operation completed sucessfully.\r
1728 @retval EFI_OUT_OF_RESOURCES A memory allocation failed\r
1729 @retval EFI_INVALID_PARAMETER A parameter was invalid\r
1730 @retval EFI_VOLUME_CORRUPTED the command line was corrupt. an argument was\r
1731 duplicated. the duplicated command line argument\r
1732 was returned in ProblemParam if provided.\r
1733 @retval EFI_NOT_FOUND a argument required a value that was missing.\r
1734 the invalid command line argument was returned in\r
1735 ProblemParam if provided.\r
1736**/\r
1737STATIC\r
1738EFI_STATUS\r
1739EFIAPI\r
1740InternalCommandLineParse (\r
1741 IN CONST SHELL_PARAM_ITEM *CheckList,\r
1742 OUT LIST_ENTRY **CheckPackage,\r
1743 OUT CHAR16 **ProblemParam OPTIONAL,\r
1744 IN BOOLEAN AutoPageBreak,\r
1745 IN CONST CHAR16 **Argv,\r
1746 IN UINTN Argc,\r
1747 IN BOOLEAN AlwaysAllowNumbers\r
1748 ) {\r
1749 UINTN LoopCounter;\r
1750 ParamType CurrentItemType;\r
1751 SHELL_PARAM_PACKAGE *CurrentItemPackage;\r
1752 UINTN GetItemValue;\r
1753 UINTN ValueSize;\r
1754\r
1755 CurrentItemPackage = NULL;\r
1756 mTotalParameterCount = 0;\r
1757 GetItemValue = 0;\r
1758 ValueSize = 0;\r
1759\r
1760 //\r
1761 // If there is only 1 item we dont need to do anything\r
1762 //\r
1763 if (Argc <= 1) {\r
1764 *CheckPackage = NULL;\r
1765 return (EFI_SUCCESS);\r
1766 }\r
1767\r
1768 //\r
1769 // ASSERTs\r
1770 //\r
1771 ASSERT(CheckList != NULL);\r
1772 ASSERT(Argv != NULL);\r
1773\r
1774 //\r
1775 // initialize the linked list\r
1776 //\r
1777 *CheckPackage = (LIST_ENTRY*)AllocateZeroPool(sizeof(LIST_ENTRY));\r
1778 InitializeListHead(*CheckPackage);\r
1779\r
1780 //\r
1781 // loop through each of the arguments\r
1782 //\r
1783 for (LoopCounter = 0 ; LoopCounter < Argc ; ++LoopCounter) {\r
1784 if (Argv[LoopCounter] == NULL) {\r
1785 //\r
1786 // do nothing for NULL argv\r
1787 //\r
1788 } else if (InternalIsOnCheckList(Argv[LoopCounter], CheckList, &CurrentItemType) != FALSE) {\r
1789 //\r
1790 // We might have leftover if last parameter didnt have optional value\r
1791 //\r
1792 if (GetItemValue != 0) {\r
1793 GetItemValue = 0;\r
1794 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);\r
1795 }\r
1796 //\r
1797 // this is a flag\r
1798 //\r
1799 CurrentItemPackage = AllocatePool(sizeof(SHELL_PARAM_PACKAGE));\r
1800 ASSERT(CurrentItemPackage != NULL);\r
1801 CurrentItemPackage->Name = AllocatePool(StrSize(Argv[LoopCounter]));\r
1802 ASSERT(CurrentItemPackage->Name != NULL);\r
1803 StrCpy(CurrentItemPackage->Name, Argv[LoopCounter]);\r
1804 CurrentItemPackage->Type = CurrentItemType;\r
1805 CurrentItemPackage->OriginalPosition = (UINTN)(-1);\r
1806 CurrentItemPackage->Value = NULL;\r
1807\r
1808 //\r
1809 // Does this flag require a value\r
1810 //\r
1811 switch (CurrentItemPackage->Type) {\r
1812 //\r
1813 // possibly trigger the next loop(s) to populate the value of this item\r
1814 //\r
1815 case TypeValue:\r
1816 GetItemValue = 1;\r
1817 ValueSize = 0;\r
1818 break;\r
1819 case TypeDoubleValue:\r
1820 GetItemValue = 2;\r
1821 ValueSize = 0;\r
1822 break;\r
1823 case TypeMaxValue:\r
1824 GetItemValue = (UINTN)(-1);\r
1825 ValueSize = 0;\r
1826 break;\r
1827 default:\r
1828 //\r
1829 // this item has no value expected; we are done\r
1830 //\r
1831 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);\r
1832 ASSERT(GetItemValue == 0);\r
1833 break;\r
1834 }\r
1835 } else if (GetItemValue != 0 && InternalIsFlag(Argv[LoopCounter], AlwaysAllowNumbers) == FALSE) {\r
1836 ASSERT(CurrentItemPackage != NULL);\r
1837 //\r
1838 // get the item VALUE for a previous flag\r
1839 //\r
1840 CurrentItemPackage->Value = ReallocatePool(ValueSize, ValueSize + StrSize(Argv[LoopCounter]) + sizeof(CHAR16), CurrentItemPackage->Value);\r
1841 ASSERT(CurrentItemPackage->Value != NULL);\r
1842 if (ValueSize == 0) {\r
1843 StrCpy(CurrentItemPackage->Value, Argv[LoopCounter]);\r
1844 } else {\r
1845 StrCat(CurrentItemPackage->Value, L" ");\r
1846 StrCat(CurrentItemPackage->Value, Argv[LoopCounter]);\r
1847 }\r
1848 ValueSize += StrSize(Argv[LoopCounter]) + sizeof(CHAR16);\r
1849 GetItemValue--;\r
1850 if (GetItemValue == 0) {\r
1851 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);\r
1852 }\r
1853 } else if (InternalIsFlag(Argv[LoopCounter], AlwaysAllowNumbers) == FALSE) {\r
1854 //\r
1855 // add this one as a non-flag\r
1856 //\r
1857 CurrentItemPackage = AllocatePool(sizeof(SHELL_PARAM_PACKAGE));\r
1858 ASSERT(CurrentItemPackage != NULL);\r
1859 CurrentItemPackage->Name = NULL;\r
1860 CurrentItemPackage->Type = TypePosition;\r
1861 CurrentItemPackage->Value = AllocatePool(StrSize(Argv[LoopCounter]));\r
1862 ASSERT(CurrentItemPackage->Value != NULL);\r
1863 StrCpy(CurrentItemPackage->Value, Argv[LoopCounter]);\r
1864 CurrentItemPackage->OriginalPosition = mTotalParameterCount++;\r
1865 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);\r
1866 } else if (ProblemParam) {\r
1867 //\r
1868 // this was a non-recognised flag... error!\r
1869 //\r
1870 *ProblemParam = AllocatePool(StrSize(Argv[LoopCounter]));\r
1871 ASSERT(*ProblemParam != NULL);\r
1872 StrCpy(*ProblemParam, Argv[LoopCounter]);\r
1873 ShellCommandLineFreeVarList(*CheckPackage);\r
1874 *CheckPackage = NULL;\r
1875 return (EFI_VOLUME_CORRUPTED);\r
1876 } else {\r
1877 ShellCommandLineFreeVarList(*CheckPackage);\r
1878 *CheckPackage = NULL;\r
1879 return (EFI_VOLUME_CORRUPTED);\r
1880 }\r
1881 }\r
1882 if (GetItemValue != 0) {\r
1883 GetItemValue = 0;\r
1884 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);\r
1885 }\r
1886 //\r
1887 // support for AutoPageBreak\r
1888 //\r
1889 if (AutoPageBreak && ShellCommandLineGetFlag(*CheckPackage, L"-b")) {\r
1890 ShellSetPageBreakMode(TRUE);\r
1891 }\r
1892 return (EFI_SUCCESS);\r
1893}\r
1894\r
1895/**\r
1896 Checks the command line arguments passed against the list of valid ones.\r
1897 Optionally removes NULL values first.\r
1898\r
1899 If no initialization is required, then return RETURN_SUCCESS.\r
1900\r
1901 @param CheckList pointer to list of parameters to check\r
1902 @param CheckPackage pointer to pointer to list checked values\r
1903 @param ProblemParam optional pointer to pointer to unicode string for\r
1904 the paramater that caused failure.\r
1905 @param AutoPageBreak will automatically set PageBreakEnabled for "b" parameter\r
1906\r
1907 @retval EFI_SUCCESS The operation completed sucessfully.\r
1908 @retval EFI_OUT_OF_RESOURCES A memory allocation failed\r
1909 @retval EFI_INVALID_PARAMETER A parameter was invalid\r
1910 @retval EFI_VOLUME_CORRUPTED the command line was corrupt. an argument was\r
1911 duplicated. the duplicated command line argument\r
1912 was returned in ProblemParam if provided.\r
1913 @retval EFI_DEVICE_ERROR the commands contained 2 opposing arguments. one\r
1914 of the command line arguments was returned in\r
1915 ProblemParam if provided.\r
1916 @retval EFI_NOT_FOUND a argument required a value that was missing.\r
1917 the invalid command line argument was returned in\r
1918 ProblemParam if provided.\r
1919**/\r
1920EFI_STATUS\r
1921EFIAPI\r
1922ShellCommandLineParseEx (\r
1923 IN CONST SHELL_PARAM_ITEM *CheckList,\r
1924 OUT LIST_ENTRY **CheckPackage,\r
1925 OUT CHAR16 **ProblemParam OPTIONAL,\r
1926 IN BOOLEAN AutoPageBreak,\r
1927 IN BOOLEAN AlwaysAllowNumbers\r
1928 ) {\r
1929 //\r
1930 // ASSERT that CheckList and CheckPackage aren't NULL\r
1931 //\r
1932 ASSERT(CheckList != NULL);\r
1933 ASSERT(CheckPackage != NULL);\r
1934\r
1935 //\r
1936 // Check for UEFI Shell 2.0 protocols\r
1937 //\r
1938 if (mEfiShellParametersProtocol != NULL) {\r
1939 return (InternalCommandLineParse(CheckList,\r
1940 CheckPackage,\r
1941 ProblemParam,\r
1942 AutoPageBreak,\r
1943 (CONST CHAR16**) mEfiShellParametersProtocol->Argv,\r
1944 mEfiShellParametersProtocol->Argc,\r
1945 AlwaysAllowNumbers));\r
1946 }\r
1947\r
1948 //\r
1949 // ASSERT That EFI Shell is not required\r
1950 //\r
1951 ASSERT (mEfiShellInterface != NULL);\r
1952 return (InternalCommandLineParse(CheckList,\r
1953 CheckPackage,\r
1954 ProblemParam,\r
1955 AutoPageBreak,\r
1956 (CONST CHAR16**) mEfiShellInterface->Argv,\r
1957 mEfiShellInterface->Argc,\r
1958 AlwaysAllowNumbers));\r
1959}\r
1960\r
1961/**\r
1962 Frees shell variable list that was returned from ShellCommandLineParse.\r
1963\r
1964 This function will free all the memory that was used for the CheckPackage\r
1965 list of postprocessed shell arguments.\r
1966\r
1967 this function has no return value.\r
1968\r
1969 if CheckPackage is NULL, then return\r
1970\r
1971 @param CheckPackage the list to de-allocate\r
1972 **/\r
1973VOID\r
1974EFIAPI\r
1975ShellCommandLineFreeVarList (\r
1976 IN LIST_ENTRY *CheckPackage\r
1977 ) {\r
1978 LIST_ENTRY *Node;\r
1979\r
1980 //\r
1981 // check for CheckPackage == NULL\r
1982 //\r
1983 if (CheckPackage == NULL) {\r
1984 return;\r
1985 }\r
1986\r
1987 //\r
1988 // for each node in the list\r
1989 //\r
1990 for ( Node = GetFirstNode(CheckPackage)\r
1991 ; IsListEmpty(CheckPackage) == FALSE\r
1992 ; Node = GetFirstNode(CheckPackage)\r
1993 ){\r
1994 //\r
1995 // Remove it from the list\r
1996 //\r
1997 RemoveEntryList(Node);\r
1998\r
1999 //\r
2000 // if it has a name free the name\r
2001 //\r
2002 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {\r
2003 FreePool(((SHELL_PARAM_PACKAGE*)Node)->Name);\r
2004 }\r
2005\r
2006 //\r
2007 // if it has a value free the value\r
2008 //\r
2009 if (((SHELL_PARAM_PACKAGE*)Node)->Value != NULL) {\r
2010 FreePool(((SHELL_PARAM_PACKAGE*)Node)->Value);\r
2011 }\r
2012\r
2013 //\r
2014 // free the node structure\r
2015 //\r
2016 FreePool((SHELL_PARAM_PACKAGE*)Node);\r
2017 }\r
2018 //\r
2019 // free the list head node\r
2020 //\r
2021 FreePool(CheckPackage);\r
2022}\r
2023/**\r
2024 Checks for presence of a flag parameter\r
2025\r
2026 flag arguments are in the form of "-<Key>" or "/<Key>", but do not have a value following the key\r
2027\r
2028 if CheckPackage is NULL then return FALSE.\r
2029 if KeyString is NULL then ASSERT()\r
2030\r
2031 @param CheckPackage The package of parsed command line arguments\r
2032 @param KeyString the Key of the command line argument to check for\r
2033\r
2034 @retval TRUE the flag is on the command line\r
2035 @retval FALSE the flag is not on the command line\r
2036 **/\r
2037BOOLEAN\r
2038EFIAPI\r
2039ShellCommandLineGetFlag (\r
2040 IN CONST LIST_ENTRY *CheckPackage,\r
2041 IN CHAR16 *KeyString\r
2042 ) {\r
2043 LIST_ENTRY *Node;\r
2044\r
2045 //\r
2046 // ASSERT that both CheckPackage and KeyString aren't NULL\r
2047 //\r
2048 ASSERT(KeyString != NULL);\r
2049\r
2050 //\r
2051 // return FALSE for no package\r
2052 //\r
2053 if (CheckPackage == NULL) {\r
2054 return (FALSE);\r
2055 }\r
2056\r
2057 //\r
2058 // enumerate through the list of parametrs\r
2059 //\r
2060 for ( Node = GetFirstNode(CheckPackage)\r
2061 ; !IsNull (CheckPackage, Node)\r
2062 ; Node = GetNextNode(CheckPackage, Node)\r
2063 ){\r
2064 //\r
2065 // If the Name matches, return TRUE (and there may be NULL name)\r
2066 //\r
2067 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {\r
2068 //\r
2069 // If Type is TypeStart then only compare the begining of the strings\r
2070 //\r
2071 if ( ((SHELL_PARAM_PACKAGE*)Node)->Type == TypeStart\r
2072 && StrnCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name, StrLen(KeyString)) == 0\r
2073 ){\r
2074 return (TRUE);\r
2075 } else if (StrCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {\r
2076 return (TRUE);\r
2077 }\r
2078 }\r
2079 }\r
2080 return (FALSE);\r
2081}\r
2082/**\r
2083 returns value from command line argument\r
2084\r
2085 value parameters are in the form of "-<Key> value" or "/<Key> value"\r
2086\r
2087 if CheckPackage is NULL, then return NULL;\r
2088\r
2089 @param CheckPackage The package of parsed command line arguments\r
2090 @param KeyString the Key of the command line argument to check for\r
2091\r
2092 @retval NULL the flag is not on the command line\r
2093 @return !=NULL pointer to unicode string of the value\r
2094 **/\r
2095CONST CHAR16*\r
2096EFIAPI\r
2097ShellCommandLineGetValue (\r
2098 IN CONST LIST_ENTRY *CheckPackage,\r
2099 IN CHAR16 *KeyString\r
2100 ) {\r
2101 LIST_ENTRY *Node;\r
2102\r
2103 //\r
2104 // check for CheckPackage == NULL\r
2105 //\r
2106 if (CheckPackage == NULL) {\r
2107 return (NULL);\r
2108 }\r
2109\r
2110 //\r
2111 // enumerate through the list of parametrs\r
2112 //\r
2113 for ( Node = GetFirstNode(CheckPackage)\r
2114 ; !IsNull (CheckPackage, Node)\r
2115 ; Node = GetNextNode(CheckPackage, Node)\r
2116 ){\r
2117 //\r
2118 // If the Name matches, return the value (name can be NULL)\r
2119 //\r
2120 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {\r
2121 //\r
2122 // If Type is TypeStart then only compare the begining of the strings\r
2123 //\r
2124 if ( ((SHELL_PARAM_PACKAGE*)Node)->Type == TypeStart\r
2125 && StrnCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name, StrLen(KeyString)) == 0\r
2126 ){\r
2127 //\r
2128 // return the string part after the flag\r
2129 //\r
2130 return (((SHELL_PARAM_PACKAGE*)Node)->Name + StrLen(KeyString));\r
2131 } else if (StrCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {\r
2132 //\r
2133 // return the value\r
2134 //\r
2135 return (((SHELL_PARAM_PACKAGE*)Node)->Value);\r
2136 }\r
2137 }\r
2138 }\r
2139 return (NULL);\r
2140}\r
2141/**\r
2142 returns raw value from command line argument\r
2143\r
2144 raw value parameters are in the form of "value" in a specific position in the list\r
2145\r
2146 if CheckPackage is NULL, then return NULL;\r
2147\r
2148 @param CheckPackage The package of parsed command line arguments\r
2149 @param Position the position of the value\r
2150\r
2151 @retval NULL the flag is not on the command line\r
2152 @return !=NULL pointer to unicode string of the value\r
2153 **/\r
2154CONST CHAR16*\r
2155EFIAPI\r
2156ShellCommandLineGetRawValue (\r
2157 IN CONST LIST_ENTRY *CheckPackage,\r
2158 IN UINT32 Position\r
2159 ) {\r
2160 LIST_ENTRY *Node;\r
2161\r
2162 //\r
2163 // check for CheckPackage == NULL\r
2164 //\r
2165 if (CheckPackage == NULL) {\r
2166 return (NULL);\r
2167 }\r
2168\r
2169 //\r
2170 // enumerate through the list of parametrs\r
2171 //\r
2172 for ( Node = GetFirstNode(CheckPackage)\r
2173 ; !IsNull (CheckPackage, Node)\r
2174 ; Node = GetNextNode(CheckPackage, Node)\r
2175 ){\r
2176 //\r
2177 // If the position matches, return the value\r
2178 //\r
2179 if (((SHELL_PARAM_PACKAGE*)Node)->OriginalPosition == Position) {\r
2180 return (((SHELL_PARAM_PACKAGE*)Node)->Value);\r
2181 }\r
2182 }\r
2183 return (NULL);\r
2184}\r
2185\r
2186/**\r
2187 returns the number of command line value parameters that were parsed.\r
2188\r
2189 this will not include flags.\r
2190\r
2191 @retval (UINTN)-1 No parsing has ocurred\r
2192 @return other The number of value parameters found\r
2193**/\r
2194UINTN\r
2195EFIAPI\r
2196ShellCommandLineGetCount(\r
2197 VOID\r
2198 )\r
2199{\r
2200 return (mTotalParameterCount);\r
2201}\r
2202\r
2203/**\r
2204 Determins if a parameter is duplicated.\r
2205\r
2206 If Param is not NULL then it will point to a callee allocated string buffer\r
2207 with the parameter value if a duplicate is found.\r
2208\r
2209 If CheckPackage is NULL, then ASSERT.\r
2210\r
2211 @param[in] CheckPackage The package of parsed command line arguments.\r
2212 @param[out] Param Upon finding one, a pointer to the duplicated parameter.\r
2213\r
2214 @retval EFI_SUCCESS No parameters were duplicated.\r
2215 @retval EFI_DEVICE_ERROR A duplicate was found.\r
2216 **/\r
2217EFI_STATUS\r
2218EFIAPI\r
2219ShellCommandLineCheckDuplicate (\r
2220 IN CONST LIST_ENTRY *CheckPackage,\r
2221 OUT CHAR16 **Param\r
2222 )\r
2223{\r
2224 LIST_ENTRY *Node1;\r
2225 LIST_ENTRY *Node2;\r
2226\r
2227 ASSERT(CheckPackage != NULL);\r
2228\r
2229 for ( Node1 = GetFirstNode(CheckPackage)\r
2230 ; !IsNull (CheckPackage, Node1)\r
2231 ; Node1 = GetNextNode(CheckPackage, Node1)\r
2232 ){\r
2233 for ( Node2 = GetNextNode(CheckPackage, Node1)\r
2234 ; !IsNull (CheckPackage, Node2)\r
2235 ; Node2 = GetNextNode(CheckPackage, Node2)\r
2236 ){\r
2237 if (StrCmp(((SHELL_PARAM_PACKAGE*)Node1)->Name, ((SHELL_PARAM_PACKAGE*)Node2)->Name) == 0) {\r
2238 if (Param != NULL) {\r
2239 *Param = NULL;\r
2240 *Param = StrnCatGrow(Param, NULL, ((SHELL_PARAM_PACKAGE*)Node1)->Name, 0);\r
2241 }\r
2242 return (EFI_DEVICE_ERROR);\r
2243 }\r
2244 }\r
2245 }\r
2246 return (EFI_SUCCESS);\r
2247}\r
2248\r
2249/**\r
2250 This is a find and replace function. Upon successful return the NewString is a copy of\r
2251 SourceString with each instance of FindTarget replaced with ReplaceWith.\r
2252\r
2253 If SourceString and NewString overlap the behavior is undefined.\r
2254\r
2255 If the string would grow bigger than NewSize it will halt and return error.\r
2256\r
2257 @param[in] SourceString String with source buffer\r
2258 @param[in,out] NewString String with resultant buffer\r
2259 @param[in] NewSize Size in bytes of NewString\r
2260 @param[in] FindTarget String to look for\r
2261 @param[in[ ReplaceWith String to replace FindTarget with\r
2262 @param[in] SkipPreCarrot If TRUE will skip a FindTarget that has a '^'\r
2263 immediately before it.\r
2264\r
2265 @retval EFI_INVALID_PARAMETER SourceString was NULL.\r
2266 @retval EFI_INVALID_PARAMETER NewString was NULL.\r
2267 @retval EFI_INVALID_PARAMETER FindTarget was NULL.\r
2268 @retval EFI_INVALID_PARAMETER ReplaceWith was NULL.\r
2269 @retval EFI_INVALID_PARAMETER FindTarget had length < 1.\r
2270 @retval EFI_INVALID_PARAMETER SourceString had length < 1.\r
2271 @retval EFI_BUFFER_TOO_SMALL NewSize was less than the minimum size to hold\r
2272 the new string (truncation occurred).\r
2273 @retval EFI_SUCCESS the string was sucessfully copied with replacement.\r
2274**/\r
2275\r
2276EFI_STATUS\r
2277EFIAPI\r
2278ShellCopySearchAndReplace2(\r
2279 IN CHAR16 CONST *SourceString,\r
2280 IN CHAR16 *NewString,\r
2281 IN UINTN NewSize,\r
2282 IN CONST CHAR16 *FindTarget,\r
2283 IN CONST CHAR16 *ReplaceWith,\r
2284 IN CONST BOOLEAN SkipPreCarrot\r
2285 )\r
2286{\r
2287 UINTN Size;\r
2288 if ( (SourceString == NULL)\r
2289 || (NewString == NULL)\r
2290 || (FindTarget == NULL)\r
2291 || (ReplaceWith == NULL)\r
2292 || (StrLen(FindTarget) < 1)\r
2293 || (StrLen(SourceString) < 1)\r
2294 ){\r
2295 return (EFI_INVALID_PARAMETER);\r
2296 }\r
2297 NewString = SetMem16(NewString, NewSize, CHAR_NULL);\r
2298 while (*SourceString != CHAR_NULL) {\r
2299 //\r
2300 // if we find the FindTarget and either Skip == FALSE or Skip == TRUE and we\r
2301 // dont have a carrot do a replace...\r
2302 //\r
2303 if (StrnCmp(SourceString, FindTarget, StrLen(FindTarget)) == 0\r
2304 && ((SkipPreCarrot && *(SourceString-1) != L'^') || SkipPreCarrot == FALSE)\r
2305 ){\r
2306 SourceString += StrLen(FindTarget);\r
2307 Size = StrSize(NewString);\r
2308 if ((Size + (StrLen(ReplaceWith)*sizeof(CHAR16))) > NewSize) {\r
2309 return (EFI_BUFFER_TOO_SMALL);\r
2310 }\r
2311 StrCat(NewString, ReplaceWith);\r
2312 } else {\r
2313 Size = StrSize(NewString);\r
2314 if (Size + sizeof(CHAR16) > NewSize) {\r
2315 return (EFI_BUFFER_TOO_SMALL);\r
2316 }\r
2317 StrnCat(NewString, SourceString, 1);\r
2318 SourceString++;\r
2319 }\r
2320 }\r
2321 return (EFI_SUCCESS);\r
2322}\r
2323\r
2324/**\r
2325 Internal worker function to output a string.\r
2326\r
2327 This function will output a string to the correct StdOut.\r
2328\r
2329 @param[in] String The string to print out.\r
2330\r
2331 @retval EFI_SUCCESS The operation was sucessful.\r
2332 @retval !EFI_SUCCESS The operation failed.\r
2333**/\r
2334EFI_STATUS\r
2335EFIAPI\r
2336InternalPrintTo (\r
2337 IN CONST CHAR16 *String\r
2338 )\r
2339{\r
2340 UINTN Size;\r
2341 Size = StrSize(String) - sizeof(CHAR16);\r
2342 if (mEfiShellParametersProtocol != NULL) {\r
2343 return (mEfiShellParametersProtocol->StdOut->Write(mEfiShellParametersProtocol->StdOut, &Size, (VOID*)String));\r
2344 }\r
2345 if (mEfiShellInterface != NULL) {\r
2346 //\r
2347 // Divide in half for old shell. Must be string length not size.\r
2348 //\r
2349 Size /= 2;\r
2350 return ( mEfiShellInterface->StdOut->Write(mEfiShellInterface->StdOut, &Size, (VOID*)String));\r
2351 }\r
2352 ASSERT(FALSE);\r
2353 return (EFI_UNSUPPORTED);\r
2354}\r
2355\r
2356/**\r
2357 Print at a specific location on the screen.\r
2358\r
2359 This function will move the cursor to a given screen location and print the specified string\r
2360\r
2361 If -1 is specified for either the Row or Col the current screen location for BOTH\r
2362 will be used.\r
2363\r
2364 if either Row or Col is out of range for the current console, then ASSERT\r
2365 if Format is NULL, then ASSERT\r
2366\r
2367 In addition to the standard %-based flags as supported by UefiLib Print() this supports\r
2368 the following additional flags:\r
2369 %N - Set output attribute to normal\r
2370 %H - Set output attribute to highlight\r
2371 %E - Set output attribute to error\r
2372 %B - Set output attribute to blue color\r
2373 %V - Set output attribute to green color\r
2374\r
2375 Note: The background color is controlled by the shell command cls.\r
2376\r
2377 @param[in] Row the row to print at\r
2378 @param[in] Col the column to print at\r
2379 @param[in] Format the format string\r
2380 @param[in] Marker the marker for the variable argument list\r
2381\r
2382 @return the number of characters printed to the screen\r
2383**/\r
2384\r
2385UINTN\r
2386EFIAPI\r
2387InternalShellPrintWorker(\r
2388 IN INT32 Col OPTIONAL,\r
2389 IN INT32 Row OPTIONAL,\r
2390 IN CONST CHAR16 *Format,\r
2391 VA_LIST Marker\r
2392 )\r
2393{\r
2394 UINTN Return;\r
2395 EFI_STATUS Status;\r
2396 UINTN NormalAttribute;\r
2397 CHAR16 *ResumeLocation;\r
2398 CHAR16 *FormatWalker;\r
2399\r
2400 //\r
2401 // Back and forth each time fixing up 1 of our flags...\r
2402 //\r
2403 Status = ShellLibCopySearchAndReplace(Format, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%N", L"%%N");\r
2404 ASSERT_EFI_ERROR(Status);\r
2405 Status = ShellLibCopySearchAndReplace(mPostReplaceFormat, mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), L"%E", L"%%E");\r
2406 ASSERT_EFI_ERROR(Status);\r
2407 Status = ShellLibCopySearchAndReplace(mPostReplaceFormat2, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%H", L"%%H");\r
2408 ASSERT_EFI_ERROR(Status);\r
2409 Status = ShellLibCopySearchAndReplace(mPostReplaceFormat, mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), L"%B", L"%%B");\r
2410 ASSERT_EFI_ERROR(Status);\r
2411 Status = ShellLibCopySearchAndReplace(mPostReplaceFormat2, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%V", L"%%V");\r
2412 ASSERT_EFI_ERROR(Status);\r
2413\r
2414 //\r
2415 // Use the last buffer from replacing to print from...\r
2416 //\r
2417 Return = UnicodeVSPrint (mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), mPostReplaceFormat, Marker);\r
2418\r
2419 if (Col != -1 && Row != -1) {\r
2420 Status = gST->ConOut->SetCursorPosition(gST->ConOut, Col, Row);\r
2421 ASSERT_EFI_ERROR(Status);\r
2422 }\r
2423\r
2424 NormalAttribute = gST->ConOut->Mode->Attribute;\r
2425 FormatWalker = mPostReplaceFormat2;\r
2426 while (*FormatWalker != CHAR_NULL) {\r
2427 //\r
2428 // Find the next attribute change request\r
2429 //\r
2430 ResumeLocation = StrStr(FormatWalker, L"%");\r
2431 if (ResumeLocation != NULL) {\r
2432 *ResumeLocation = CHAR_NULL;\r
2433 }\r
2434 //\r
2435 // print the current FormatWalker string\r
2436 //\r
2437 Status = InternalPrintTo(FormatWalker);\r
2438 ASSERT_EFI_ERROR(Status);\r
2439 //\r
2440 // update the attribute\r
2441 //\r
2442 if (ResumeLocation != NULL) {\r
2443 switch (*(ResumeLocation+1)) {\r
2444 case (L'N'):\r
2445 gST->ConOut->SetAttribute(gST->ConOut, NormalAttribute);\r
2446 break;\r
2447 case (L'E'):\r
2448 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_YELLOW, ((NormalAttribute&(BIT4|BIT5|BIT6))>>4)));\r
2449 break;\r
2450 case (L'H'):\r
2451 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_WHITE, ((NormalAttribute&(BIT4|BIT5|BIT6))>>4)));\r
2452 break;\r
2453 case (L'B'):\r
2454 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_BLUE, ((NormalAttribute&(BIT4|BIT5|BIT6))>>4)));\r
2455 break;\r
2456 case (L'V'):\r
2457 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_GREEN, ((NormalAttribute&(BIT4|BIT5|BIT6))>>4)));\r
2458 break;\r
2459 default:\r
2460 //\r
2461 // Print a simple '%' symbol\r
2462 //\r
2463 Status = InternalPrintTo(L"%");\r
2464 ASSERT_EFI_ERROR(Status);\r
2465 ResumeLocation = ResumeLocation - 1;\r
2466 break;\r
2467 }\r
2468 } else {\r
2469 //\r
2470 // reset to normal now...\r
2471 //\r
2472 gST->ConOut->SetAttribute(gST->ConOut, NormalAttribute);\r
2473 break;\r
2474 }\r
2475\r
2476 //\r
2477 // update FormatWalker to Resume + 2 (skip the % and the indicator)\r
2478 //\r
2479 FormatWalker = ResumeLocation + 2;\r
2480 }\r
2481\r
2482 return (Return);\r
2483}\r
2484\r
2485/**\r
2486 Print at a specific location on the screen.\r
2487\r
2488 This function will move the cursor to a given screen location and print the specified string.\r
2489\r
2490 If -1 is specified for either the Row or Col the current screen location for BOTH\r
2491 will be used.\r
2492\r
2493 If either Row or Col is out of range for the current console, then ASSERT.\r
2494 If Format is NULL, then ASSERT.\r
2495\r
2496 In addition to the standard %-based flags as supported by UefiLib Print() this supports\r
2497 the following additional flags:\r
2498 %N - Set output attribute to normal\r
2499 %H - Set output attribute to highlight\r
2500 %E - Set output attribute to error\r
2501 %B - Set output attribute to blue color\r
2502 %V - Set output attribute to green color\r
2503\r
2504 Note: The background color is controlled by the shell command cls.\r
2505\r
2506 @param[in] Row the row to print at\r
2507 @param[in] Col the column to print at\r
2508 @param[in] Format the format string\r
2509\r
2510 @return the number of characters printed to the screen\r
2511**/\r
2512\r
2513UINTN\r
2514EFIAPI\r
2515ShellPrintEx(\r
2516 IN INT32 Col OPTIONAL,\r
2517 IN INT32 Row OPTIONAL,\r
2518 IN CONST CHAR16 *Format,\r
2519 ...\r
2520 )\r
2521{\r
2522 VA_LIST Marker;\r
2523 EFI_STATUS Status;\r
2524 VA_START (Marker, Format);\r
2525 Status = InternalShellPrintWorker(Col, Row, Format, Marker);\r
2526 VA_END(Marker);\r
2527 return(Status);\r
2528}\r
2529\r
2530/**\r
2531 Print at a specific location on the screen.\r
2532\r
2533 This function will move the cursor to a given screen location and print the specified string.\r
2534\r
2535 If -1 is specified for either the Row or Col the current screen location for BOTH\r
2536 will be used.\r
2537\r
2538 If either Row or Col is out of range for the current console, then ASSERT.\r
2539 If Format is NULL, then ASSERT.\r
2540\r
2541 In addition to the standard %-based flags as supported by UefiLib Print() this supports\r
2542 the following additional flags:\r
2543 %N - Set output attribute to normal.\r
2544 %H - Set output attribute to highlight.\r
2545 %E - Set output attribute to error.\r
2546 %B - Set output attribute to blue color.\r
2547 %V - Set output attribute to green color.\r
2548\r
2549 Note: The background color is controlled by the shell command cls.\r
2550\r
2551 @param[in] Row The row to print at.\r
2552 @param[in] Col The column to print at.\r
2553 @param[in] Language The language of the string to retrieve. If this parameter\r
2554 is NULL, then the current platform language is used.\r
2555 @param[in] HiiFormatStringId The format string Id for getting from Hii.\r
2556 @param[in] HiiFormatHandle The format string Handle for getting from Hii.\r
2557\r
2558 @return the number of characters printed to the screen.\r
2559**/\r
2560UINTN\r
2561EFIAPI\r
2562ShellPrintHiiEx(\r
2563 IN INT32 Col OPTIONAL,\r
2564 IN INT32 Row OPTIONAL,\r
2565 IN CONST CHAR8 *Language OPTIONAL,\r
2566 IN CONST EFI_STRING_ID HiiFormatStringId,\r
2567 IN CONST EFI_HANDLE HiiFormatHandle,\r
2568 ...\r
2569 )\r
2570{\r
2571 VA_LIST Marker;\r
2572 CHAR16 *HiiFormatString;\r
2573 UINTN RetVal;\r
2574\r
2575 VA_START (Marker, HiiFormatHandle);\r
2576 HiiFormatString = HiiGetString(HiiFormatHandle, HiiFormatStringId, Language);\r
2577 ASSERT(HiiFormatString != NULL);\r
2578\r
2579 RetVal = InternalShellPrintWorker(Col, Row, HiiFormatString, Marker);\r
2580\r
2581 FreePool(HiiFormatString);\r
2582 VA_END(Marker);\r
2583\r
2584 return (RetVal);\r
2585}\r
2586\r
2587/**\r
2588 Function to determine if a given filename represents a file or a directory.\r
2589\r
2590 @param[in] DirName Path to directory to test.\r
2591\r
2592 @retval EFI_SUCCESS The Path represents a directory\r
2593 @retval EFI_NOT_FOUND The Path does not represent a directory\r
2594 @return other The path failed to open\r
2595**/\r
2596EFI_STATUS\r
2597EFIAPI\r
2598ShellIsDirectory(\r
2599 IN CONST CHAR16 *DirName\r
2600 )\r
2601{\r
2602 EFI_STATUS Status;\r
2603 EFI_FILE_HANDLE Handle;\r
2604\r
2605 ASSERT(DirName != NULL);\r
2606\r
2607 Handle = NULL;\r
2608\r
2609 Status = ShellOpenFileByName(DirName, &Handle, EFI_FILE_MODE_READ, 0);\r
2610 if (EFI_ERROR(Status)) {\r
2611 return (Status);\r
2612 }\r
2613\r
2614 if (FileHandleIsDirectory(Handle) == EFI_SUCCESS) {\r
2615 ShellCloseFile(&Handle);\r
2616 return (EFI_SUCCESS);\r
2617 }\r
2618 ShellCloseFile(&Handle);\r
2619 return (EFI_NOT_FOUND);\r
2620}\r
2621\r
2622/**\r
2623 Function to determine if a given filename represents a file.\r
2624\r
2625 @param[in] Name Path to file to test.\r
2626\r
2627 @retval EFI_SUCCESS The Path represents a file.\r
2628 @retval EFI_NOT_FOUND The Path does not represent a file.\r
2629 @retval other The path failed to open.\r
2630**/\r
2631EFI_STATUS\r
2632EFIAPI\r
2633ShellIsFile(\r
2634 IN CONST CHAR16 *Name\r
2635 )\r
2636{\r
2637 EFI_STATUS Status;\r
2638 EFI_FILE_HANDLE Handle;\r
2639\r
2640 ASSERT(Name != NULL);\r
2641\r
2642 Handle = NULL;\r
2643\r
2644 Status = ShellOpenFileByName(Name, &Handle, EFI_FILE_MODE_READ, 0);\r
2645 if (EFI_ERROR(Status)) {\r
2646 return (Status);\r
2647 }\r
2648\r
2649 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {\r
2650 ShellCloseFile(&Handle);\r
2651 return (EFI_SUCCESS);\r
2652 }\r
2653 ShellCloseFile(&Handle);\r
2654 return (EFI_NOT_FOUND);\r
2655}\r
2656\r
2657/**\r
2658 Function to determine if a given filename represents a file.\r
2659\r
2660 This will search the CWD and then the Path.\r
2661\r
2662 If Name is NULL, then ASSERT.\r
2663\r
2664 @param[in] Name Path to file to test.\r
2665\r
2666 @retval EFI_SUCCESS The Path represents a file.\r
2667 @retval EFI_NOT_FOUND The Path does not represent a file.\r
2668 @retval other The path failed to open.\r
2669**/\r
2670EFI_STATUS\r
2671EFIAPI\r
2672ShellIsFileInPath(\r
2673 IN CONST CHAR16 *Name\r
2674 ) {\r
2675 CHAR16 *NewName;\r
2676 EFI_STATUS Status;\r
2677\r
2678 if (!EFI_ERROR(ShellIsFile(Name))) {\r
2679 return (TRUE);\r
2680 }\r
2681\r
2682 NewName = ShellFindFilePath(Name);\r
2683 if (NewName == NULL) {\r
2684 return (EFI_NOT_FOUND);\r
2685 }\r
2686 Status = ShellIsFile(NewName);\r
2687 FreePool(NewName);\r
2688 return (Status);\r
2689}\r
2690/**\r
2691 Function to determine whether a string is decimal or hex representation of a number\r
2692 and return the number converted from the string.\r
2693\r
2694 @param[in] String String representation of a number\r
2695\r
2696 @retval all the number\r
2697**/\r
2698UINTN\r
2699EFIAPI\r
2700ShellStrToUintn(\r
2701 IN CONST CHAR16 *String\r
2702 )\r
2703{\r
2704 CONST CHAR16 *Walker;\r
2705 for (Walker = String; Walker != NULL && *Walker != CHAR_NULL && *Walker == L' '; Walker++);\r
2706 if (Walker == NULL || *Walker == CHAR_NULL) {\r
2707 ASSERT(FALSE);\r
2708 return ((UINTN)(-1));\r
2709 } else {\r
2710 if (StrnCmp(Walker, L"0x", 2) == 0 || StrnCmp(Walker, L"0X", 2) == 0){\r
2711 return (StrHexToUintn(Walker));\r
2712 }\r
2713 return (StrDecimalToUintn(Walker));\r
2714 }\r
2715}\r
2716\r
2717/**\r
2718 Safely append with automatic string resizing given length of Destination and\r
2719 desired length of copy from Source.\r
2720\r
2721 append the first D characters of Source to the end of Destination, where D is\r
2722 the lesser of Count and the StrLen() of Source. If appending those D characters\r
2723 will fit within Destination (whose Size is given as CurrentSize) and\r
2724 still leave room for a NULL terminator, then those characters are appended,\r
2725 starting at the original terminating NULL of Destination, and a new terminating\r
2726 NULL is appended.\r
2727\r
2728 If appending D characters onto Destination will result in a overflow of the size\r
2729 given in CurrentSize the string will be grown such that the copy can be performed\r
2730 and CurrentSize will be updated to the new size.\r
2731\r
2732 If Source is NULL, there is nothing to append, just return the current buffer in\r
2733 Destination.\r
2734\r
2735 if Destination is NULL, then ASSERT()\r
2736 if Destination's current length (including NULL terminator) is already more then\r
2737 CurrentSize, then ASSERT()\r
2738\r
2739 @param[in,out] Destination The String to append onto\r
2740 @param[in,out] CurrentSize on call the number of bytes in Destination. On\r
2741 return possibly the new size (still in bytes). if NULL\r
2742 then allocate whatever is needed.\r
2743 @param[in] Source The String to append from\r
2744 @param[in] Count Maximum number of characters to append. if 0 then\r
2745 all are appended.\r
2746\r
2747 @return Destination return the resultant string.\r
2748**/\r
2749CHAR16*\r
2750EFIAPI\r
2751StrnCatGrow (\r
2752 IN OUT CHAR16 **Destination,\r
2753 IN OUT UINTN *CurrentSize,\r
2754 IN CONST CHAR16 *Source,\r
2755 IN UINTN Count\r
2756 )\r
2757{\r
2758 UINTN DestinationStartSize;\r
2759 UINTN NewSize;\r
2760\r
2761 //\r
2762 // ASSERTs\r
2763 //\r
2764 ASSERT(Destination != NULL);\r
2765\r
2766 //\r
2767 // If there's nothing to do then just return Destination\r
2768 //\r
2769 if (Source == NULL) {\r
2770 return (*Destination);\r
2771 }\r
2772\r
2773 //\r
2774 // allow for un-initialized pointers, based on size being 0\r
2775 //\r
2776 if (CurrentSize != NULL && *CurrentSize == 0) {\r
2777 *Destination = NULL;\r
2778 }\r
2779\r
2780 //\r
2781 // allow for NULL pointers address as Destination\r
2782 //\r
2783 if (*Destination != NULL) {\r
2784 ASSERT(CurrentSize != 0);\r
2785 DestinationStartSize = StrSize(*Destination);\r
2786 ASSERT(DestinationStartSize <= *CurrentSize);\r
2787 } else {\r
2788 DestinationStartSize = 0;\r
2789// ASSERT(*CurrentSize == 0);\r
2790 }\r
2791\r
2792 //\r
2793 // Append all of Source?\r
2794 //\r
2795 if (Count == 0) {\r
2796 Count = StrLen(Source);\r
2797 }\r
2798\r
2799 //\r
2800 // Test and grow if required\r
2801 //\r
2802 if (CurrentSize != NULL) {\r
2803 NewSize = *CurrentSize;\r
2804 while (NewSize < (DestinationStartSize + (Count*sizeof(CHAR16)))) {\r
2805 NewSize += 2 * Count * sizeof(CHAR16);\r
2806 }\r
2807 *Destination = ReallocatePool(*CurrentSize, NewSize, *Destination);\r
2808 *CurrentSize = NewSize;\r
2809 } else {\r
2810 *Destination = AllocateZeroPool((Count+1)*sizeof(CHAR16));\r
2811 }\r
2812\r
2813 //\r
2814 // Now use standard StrnCat on a big enough buffer\r
2815 //\r
2816 return StrnCat(*Destination, Source, Count);\r
2817}\r