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