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