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