]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellLib/BaseShellLib.c
git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@8562 6f19259b...
[mirror_edk2.git] / ShellPkg / Library / UefiShellLib / BaseShellLib.c
1 /** @file
2 Provides interface to shell functionality for shell commands and applications.
3
4 Copyright (c) 2006 - 2009, Intel Corporation
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 <Protocol/EfiShellEnvironment2.h>
26 #include <Protocol/EfiShellInterface.h>
27 #include <Protocol/EfiShell.h>
28 #include <Protocol/EfiShellParameters.h>
29 #include <Protocol/SimpleFileSystem.h>
30
31 #include "BaseShellLib.h"
32
33 #define MAX_FILE_NAME_LEN 522 // (20 * (6+5+2))+1) unicode characters from EFI FAT spec (doubled for bytes)
34 #define FIND_XXXXX_FILE_BUFFER_SIZE (SIZE_OF_EFI_FILE_INFO + MAX_FILE_NAME_LEN)
35
36 //
37 // This is not static since it's extern in the .h file
38 //
39 SHELL_PARAM_ITEM EmptyParamList[] = {
40 {NULL, TypeMax}
41 };
42
43 //
44 // Static file globals for the shell library
45 //
46 STATIC EFI_SHELL_ENVIRONMENT2 *mEfiShellEnvironment2;
47 STATIC EFI_SHELL_INTERFACE *mEfiShellInterface;
48 STATIC EFI_SHELL_PROTOCOL *mEfiShellProtocol;
49 STATIC EFI_SHELL_PARAMETERS_PROTOCOL *mEfiShellParametersProtocol;
50 STATIC EFI_HANDLE mEfiShellEnvironment2Handle;
51 STATIC FILE_HANDLE_FUNCTION_MAP FileFunctionMap;
52
53 /**
54 helper function to find ShellEnvironment2 for constructor
55 **/
56 EFI_STATUS
57 EFIAPI
58 ShellFindSE2 (
59 IN EFI_HANDLE ImageHandle
60 )
61 {
62 EFI_STATUS Status;
63 EFI_HANDLE *Buffer;
64 UINTN BufferSize;
65 UINTN HandleIndex;
66
67 BufferSize = 0;
68 Buffer = NULL;
69 Status = gBS->OpenProtocol(ImageHandle,
70 &gEfiShellEnvironment2Guid,
71 (VOID **)&mEfiShellEnvironment2,
72 ImageHandle,
73 NULL,
74 EFI_OPEN_PROTOCOL_GET_PROTOCOL
75 );
76 //
77 // look for the mEfiShellEnvironment2 protocol at a higher level
78 //
79 if (EFI_ERROR (Status) || !(CompareGuid (&mEfiShellEnvironment2->SESGuid, &gEfiShellEnvironment2ExtGuid) != FALSE &&
80 (mEfiShellEnvironment2->MajorVersion > EFI_SHELL_MAJOR_VER ||
81 (mEfiShellEnvironment2->MajorVersion == EFI_SHELL_MAJOR_VER && mEfiShellEnvironment2->MinorVersion >= EFI_SHELL_MINOR_VER)))) {
82 //
83 // figure out how big of a buffer we need.
84 //
85 Status = gBS->LocateHandle (ByProtocol,
86 &gEfiShellEnvironment2Guid,
87 NULL, // ignored for ByProtocol
88 &BufferSize,
89 Buffer
90 );
91 ASSERT(Status == EFI_BUFFER_TOO_SMALL);
92 Buffer = (EFI_HANDLE*)AllocatePool(BufferSize);
93 ASSERT(Buffer != NULL);
94 Status = gBS->LocateHandle (ByProtocol,
95 &gEfiShellEnvironment2Guid,
96 NULL, // ignored for ByProtocol
97 &BufferSize,
98 Buffer
99 );
100 if (!EFI_ERROR (Status)) {
101 //
102 // now parse the list of returned handles
103 //
104 Status = EFI_NOT_FOUND;
105 for (HandleIndex = 0; HandleIndex < (BufferSize/sizeof(Buffer[0])); HandleIndex++) {
106 Status = gBS->OpenProtocol(Buffer[HandleIndex],
107 &gEfiShellEnvironment2Guid,
108 (VOID **)&mEfiShellEnvironment2,
109 ImageHandle,
110 NULL,
111 EFI_OPEN_PROTOCOL_GET_PROTOCOL
112 );
113 if (CompareGuid (&mEfiShellEnvironment2->SESGuid, &gEfiShellEnvironment2ExtGuid) != FALSE &&
114 (mEfiShellEnvironment2->MajorVersion > EFI_SHELL_MAJOR_VER ||
115 (mEfiShellEnvironment2->MajorVersion == EFI_SHELL_MAJOR_VER && mEfiShellEnvironment2->MinorVersion >= EFI_SHELL_MINOR_VER))) {
116 mEfiShellEnvironment2Handle = Buffer[HandleIndex];
117 Status = EFI_SUCCESS;
118 break;
119 }
120 }
121 }
122 }
123 if (Buffer != NULL) {
124 FreePool (Buffer);
125 }
126 return (Status);
127 }
128
129 EFI_STATUS
130 EFIAPI
131 ShellLibConstructorWorker (
132 IN EFI_HANDLE ImageHandle,
133 IN EFI_SYSTEM_TABLE *SystemTable
134 ){
135 EFI_STATUS Status;
136
137 //
138 // UEFI 2.0 shell interfaces (used preferentially)
139 //
140 Status = gBS->OpenProtocol(ImageHandle,
141 &gEfiShellProtocolGuid,
142 (VOID **)&mEfiShellProtocol,
143 ImageHandle,
144 NULL,
145 EFI_OPEN_PROTOCOL_GET_PROTOCOL
146 );
147 if (EFI_ERROR(Status)) {
148 mEfiShellProtocol = NULL;
149 }
150 Status = gBS->OpenProtocol(ImageHandle,
151 &gEfiShellParametersProtocolGuid,
152 (VOID **)&mEfiShellParametersProtocol,
153 ImageHandle,
154 NULL,
155 EFI_OPEN_PROTOCOL_GET_PROTOCOL
156 );
157 if (EFI_ERROR(Status)) {
158 mEfiShellParametersProtocol = NULL;
159 }
160
161 if (mEfiShellParametersProtocol == NULL || mEfiShellProtocol == NULL) {
162 //
163 // Moved to seperate function due to complexity
164 //
165 Status = ShellFindSE2(ImageHandle);
166
167 if (EFI_ERROR(Status)) {
168 DEBUG((DEBUG_ERROR, "Status: 0x%08x\r\n", Status));
169 mEfiShellEnvironment2 = NULL;
170 }
171 Status = gBS->OpenProtocol(ImageHandle,
172 &gEfiShellInterfaceGuid,
173 (VOID **)&mEfiShellInterface,
174 ImageHandle,
175 NULL,
176 EFI_OPEN_PROTOCOL_GET_PROTOCOL
177 );
178 if (EFI_ERROR(Status)) {
179 mEfiShellInterface = NULL;
180 }
181 }
182 //
183 // only success getting 2 of either the old or new, but no 1/2 and 1/2
184 //
185 if ((mEfiShellEnvironment2 != NULL && mEfiShellInterface != NULL) ||
186 (mEfiShellProtocol != NULL && mEfiShellParametersProtocol != NULL) ) {
187 if (mEfiShellProtocol != NULL) {
188 FileFunctionMap.GetFileInfo = mEfiShellProtocol->GetFileInfo;
189 FileFunctionMap.SetFileInfo = mEfiShellProtocol->SetFileInfo;
190 FileFunctionMap.ReadFile = mEfiShellProtocol->ReadFile;
191 FileFunctionMap.WriteFile = mEfiShellProtocol->WriteFile;
192 FileFunctionMap.CloseFile = mEfiShellProtocol->CloseFile;
193 FileFunctionMap.DeleteFile = mEfiShellProtocol->DeleteFile;
194 FileFunctionMap.GetFilePosition = mEfiShellProtocol->GetFilePosition;
195 FileFunctionMap.SetFilePosition = mEfiShellProtocol->SetFilePosition;
196 FileFunctionMap.FlushFile = mEfiShellProtocol->FlushFile;
197 FileFunctionMap.GetFileSize = mEfiShellProtocol->GetFileSize;
198 } else {
199 FileFunctionMap.GetFileInfo = FileHandleGetInfo;
200 FileFunctionMap.SetFileInfo = FileHandleSetInfo;
201 FileFunctionMap.ReadFile = FileHandleRead;
202 FileFunctionMap.WriteFile = FileHandleWrite;
203 FileFunctionMap.CloseFile = FileHandleClose;
204 FileFunctionMap.DeleteFile = FileHandleDelete;
205 FileFunctionMap.GetFilePosition = FileHandleGetPosition;
206 FileFunctionMap.SetFilePosition = FileHandleSetPosition;
207 FileFunctionMap.FlushFile = FileHandleFlush;
208 FileFunctionMap.GetFileSize = FileHandleGetSize;
209 }
210 return (EFI_SUCCESS);
211 }
212 return (EFI_NOT_FOUND);
213 }
214 /**
215 Constructor for the Shell library.
216
217 Initialize the library and determine if the underlying is a UEFI Shell 2.0 or an EFI shell.
218
219 @param ImageHandle the image handle of the process
220 @param SystemTable the EFI System Table pointer
221
222 @retval EFI_SUCCESS the initialization was complete sucessfully
223 @return others an error ocurred during initialization
224 **/
225 EFI_STATUS
226 EFIAPI
227 ShellLibConstructor (
228 IN EFI_HANDLE ImageHandle,
229 IN EFI_SYSTEM_TABLE *SystemTable
230 )
231 {
232
233
234 mEfiShellEnvironment2 = NULL;
235 mEfiShellProtocol = NULL;
236 mEfiShellParametersProtocol = NULL;
237 mEfiShellInterface = NULL;
238 mEfiShellEnvironment2Handle = NULL;
239
240 ///@todo make a worker constructor so initialize function works
241 //
242 // verify that auto initialize is not set false
243 //
244 if (PcdGetBool(PcdShellLibAutoInitialize) == 0) {
245 return (EFI_SUCCESS);
246 }
247
248 return (ShellLibConstructorWorker(ImageHandle, SystemTable));
249 }
250
251 /**
252 Destructory for the library. free any resources.
253 **/
254 EFI_STATUS
255 EFIAPI
256 ShellLibDestructor (
257 IN EFI_HANDLE ImageHandle,
258 IN EFI_SYSTEM_TABLE *SystemTable
259 )
260 {
261 if (mEfiShellEnvironment2 != NULL) {
262 gBS->CloseProtocol(mEfiShellEnvironment2Handle==NULL?ImageHandle:mEfiShellEnvironment2Handle,
263 &gEfiShellEnvironment2Guid,
264 ImageHandle,
265 NULL);
266 mEfiShellEnvironment2 = NULL;
267 }
268 if (mEfiShellInterface != NULL) {
269 gBS->CloseProtocol(ImageHandle,
270 &gEfiShellInterfaceGuid,
271 ImageHandle,
272 NULL);
273 mEfiShellInterface = NULL;
274 }
275 if (mEfiShellProtocol != NULL) {
276 gBS->CloseProtocol(ImageHandle,
277 &gEfiShellProtocolGuid,
278 ImageHandle,
279 NULL);
280 mEfiShellProtocol = NULL;
281 }
282 if (mEfiShellParametersProtocol != NULL) {
283 gBS->CloseProtocol(ImageHandle,
284 &gEfiShellParametersProtocolGuid,
285 ImageHandle,
286 NULL);
287 mEfiShellParametersProtocol = NULL;
288 }
289 mEfiShellEnvironment2Handle = NULL;
290 return (EFI_SUCCESS);
291 }
292
293 /**
294 This function causes the shell library to initialize itself. If the shell library
295 is already initialized it will de-initialize all the current protocol poitners and
296 re-populate them again.
297
298 When the library is used with PcdShellLibAutoInitialize set to true this function
299 will return EFI_SUCCESS and perform no actions.
300
301 This function is intended for internal access for shell commands only.
302
303 @retval EFI_SUCCESS the initialization was complete sucessfully
304
305 **/
306 EFI_STATUS
307 EFIAPI
308 ShellInitialize (
309 ) {
310 //
311 // if auto initialize is not false then skip
312 //
313 if (PcdGetBool(PcdShellLibAutoInitialize) != 0) {
314 return (EFI_SUCCESS);
315 }
316
317 //
318 // deinit the current stuff
319 //
320 ASSERT_EFI_ERROR(ShellLibDestructor(gImageHandle, gST));
321
322 //
323 // init the new stuff
324 //
325 return (ShellLibConstructorWorker(gImageHandle, gST));
326 }
327
328 /**
329 This function will retrieve the information about the file for the handle
330 specified and store it in allocated pool memory.
331
332 This function allocates a buffer to store the file's information. It is the
333 caller's responsibility to free the buffer
334
335 @param FileHandle The file handle of the file for which information is
336 being requested.
337
338 @retval NULL information could not be retrieved.
339
340 @return the information about the file
341 **/
342 EFI_FILE_INFO*
343 EFIAPI
344 ShellGetFileInfo (
345 IN EFI_FILE_HANDLE FileHandle
346 )
347 {
348 return (FileFunctionMap.GetFileInfo(FileHandle));
349 }
350
351 /**
352 This function will set the information about the file for the opened handle
353 specified.
354
355 @param FileHandle The file handle of the file for which information
356 is being set
357
358 @param FileInfo The infotmation to set.
359
360 @retval EFI_SUCCESS The information was set.
361 @retval EFI_UNSUPPORTED The InformationType is not known.
362 @retval EFI_NO_MEDIA The device has no medium.
363 @retval EFI_DEVICE_ERROR The device reported an error.
364 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
365 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
366 @retval EFI_ACCESS_DENIED The file was opened read only.
367 @retval EFI_VOLUME_FULL The volume is full.
368 **/
369 EFI_STATUS
370 EFIAPI
371 ShellSetFileInfo (
372 IN EFI_FILE_HANDLE FileHandle,
373 IN EFI_FILE_INFO *FileInfo
374 )
375 {
376 return (FileFunctionMap.SetFileInfo(FileHandle, FileInfo));
377 }
378
379 /**
380 This function will open a file or directory referenced by DevicePath.
381
382 This function opens a file with the open mode according to the file path. The
383 Attributes is valid only for EFI_FILE_MODE_CREATE.
384
385 @param FilePath on input the device path to the file. On output
386 the remaining device path.
387 @param DeviceHandle pointer to the system device handle.
388 @param FileHandle pointer to the file handle.
389 @param OpenMode the mode to open the file with.
390 @param Attributes the file's file attributes.
391
392 @retval EFI_SUCCESS The information was set.
393 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
394 @retval EFI_UNSUPPORTED Could not open the file path.
395 @retval EFI_NOT_FOUND The specified file could not be found on the
396 device or the file system could not be found on
397 the device.
398 @retval EFI_NO_MEDIA The device has no medium.
399 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
400 medium is no longer supported.
401 @retval EFI_DEVICE_ERROR The device reported an error.
402 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
403 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
404 @retval EFI_ACCESS_DENIED The file was opened read only.
405 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
406 file.
407 @retval EFI_VOLUME_FULL The volume is full.
408 **/
409 EFI_STATUS
410 EFIAPI
411 ShellOpenFileByDevicePath(
412 IN OUT EFI_DEVICE_PATH_PROTOCOL **FilePath,
413 OUT EFI_HANDLE *DeviceHandle,
414 OUT EFI_FILE_HANDLE *FileHandle,
415 IN UINT64 OpenMode,
416 IN UINT64 Attributes
417 )
418 {
419 CHAR16 *FileName;
420 EFI_STATUS Status;
421 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *EfiSimpleFileSystemProtocol;
422 EFI_FILE_HANDLE LastHandle;
423
424 //
425 // ASERT for FileHandle, FilePath, and DeviceHandle being NULL
426 //
427 ASSERT(FilePath != NULL);
428 ASSERT(FileHandle != NULL);
429 ASSERT(DeviceHandle != NULL);
430 //
431 // which shell interface should we use
432 //
433 if (mEfiShellProtocol != NULL) {
434 //
435 // use UEFI Shell 2.0 method.
436 //
437 FileName = mEfiShellProtocol->GetFilePathFromDevicePath(*FilePath);
438 if (FileName == NULL) {
439 return (EFI_INVALID_PARAMETER);
440 }
441 Status = ShellOpenFileByName(FileName, FileHandle, OpenMode, Attributes);
442 FreePool(FileName);
443 return (Status);
444 }
445
446
447 //
448 // use old shell method.
449 //
450 Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid,
451 FilePath,
452 DeviceHandle);
453 if (EFI_ERROR (Status)) {
454 return Status;
455 }
456 Status = gBS->OpenProtocol(*DeviceHandle,
457 &gEfiSimpleFileSystemProtocolGuid,
458 (VOID**) &EfiSimpleFileSystemProtocol,
459 gImageHandle,
460 NULL,
461 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
462 if (EFI_ERROR (Status)) {
463 return Status;
464 }
465 Status = EfiSimpleFileSystemProtocol->OpenVolume(EfiSimpleFileSystemProtocol, FileHandle);
466 if (EFI_ERROR (Status)) {
467 FileHandle = NULL;
468 return Status;
469 }
470
471 //
472 // go down directories one node at a time.
473 //
474 while (!IsDevicePathEnd (*FilePath)) {
475 //
476 // For file system access each node should be a file path component
477 //
478 if (DevicePathType (*FilePath) != MEDIA_DEVICE_PATH ||
479 DevicePathSubType (*FilePath) != MEDIA_FILEPATH_DP
480 ) {
481 FileHandle = NULL;
482 return (EFI_INVALID_PARAMETER);
483 }
484 //
485 // Open this file path node
486 //
487 LastHandle = *FileHandle;
488 *FileHandle = NULL;
489
490 //
491 // Try to test opening an existing file
492 //
493 Status = LastHandle->Open (
494 LastHandle,
495 FileHandle,
496 ((FILEPATH_DEVICE_PATH*)*FilePath)->PathName,
497 OpenMode &~EFI_FILE_MODE_CREATE,
498 0
499 );
500
501 //
502 // see if the error was that it needs to be created
503 //
504 if ((EFI_ERROR (Status)) && (OpenMode != (OpenMode &~EFI_FILE_MODE_CREATE))) {
505 Status = LastHandle->Open (
506 LastHandle,
507 FileHandle,
508 ((FILEPATH_DEVICE_PATH*)*FilePath)->PathName,
509 OpenMode,
510 Attributes
511 );
512 }
513 //
514 // Close the last node
515 //
516 LastHandle->Close (LastHandle);
517
518 if (EFI_ERROR(Status)) {
519 return (Status);
520 }
521
522 //
523 // Get the next node
524 //
525 *FilePath = NextDevicePathNode (*FilePath);
526 }
527 return (EFI_SUCCESS);
528 }
529
530 /**
531 This function will open a file or directory referenced by filename.
532
533 If return is EFI_SUCCESS, the Filehandle is the opened file's handle;
534 otherwise, the Filehandle is NULL. The Attributes is valid only for
535 EFI_FILE_MODE_CREATE.
536
537 if FileNAme is NULL then ASSERT()
538
539 @param FileName pointer to file name
540 @param FileHandle pointer to the file handle.
541 @param OpenMode the mode to open the file with.
542 @param Attributes the file's file attributes.
543
544 @retval EFI_SUCCESS The information was set.
545 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
546 @retval EFI_UNSUPPORTED Could not open the file path.
547 @retval EFI_NOT_FOUND The specified file could not be found on the
548 device or the file system could not be found
549 on the device.
550 @retval EFI_NO_MEDIA The device has no medium.
551 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
552 medium is no longer supported.
553 @retval EFI_DEVICE_ERROR The device reported an error.
554 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
555 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
556 @retval EFI_ACCESS_DENIED The file was opened read only.
557 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
558 file.
559 @retval EFI_VOLUME_FULL The volume is full.
560 **/
561 EFI_STATUS
562 EFIAPI
563 ShellOpenFileByName(
564 IN CHAR16 *FileName,
565 OUT EFI_FILE_HANDLE *FileHandle,
566 IN UINT64 OpenMode,
567 IN UINT64 Attributes
568 )
569 {
570 EFI_HANDLE DeviceHandle;
571 EFI_DEVICE_PATH_PROTOCOL *FilePath;
572
573 //
574 // ASSERT if FileName is NULL
575 //
576 ASSERT(FileName != NULL);
577
578 if (mEfiShellProtocol != NULL) {
579 //
580 // Use UEFI Shell 2.0 method
581 //
582 return (mEfiShellProtocol->OpenFileByName(FileName,
583 FileHandle,
584 OpenMode));
585
586 ///@todo add the attributes
587 }
588 //
589 // Using EFI Shell version
590 // this means convert name to path and call that function
591 // since this will use EFI method again that will open it.
592 //
593 ASSERT(mEfiShellEnvironment2 != NULL);
594 FilePath = mEfiShellEnvironment2->NameToPath (FileName);
595 if (FileDevicePath != NULL) {
596 return (ShellOpenFileByDevicePath(&FilePath,
597 &DeviceHandle,
598 FileHandle,
599 OpenMode,
600 Attributes ));
601 }
602 return (EFI_DEVICE_ERROR);
603 }
604 /**
605 This function create a directory
606
607 If return is EFI_SUCCESS, the Filehandle is the opened directory's handle;
608 otherwise, the Filehandle is NULL. If the directory already existed, this
609 function opens the existing directory.
610
611 @param DirectoryName pointer to directory name
612 @param FileHandle pointer to the file handle.
613
614 @retval EFI_SUCCESS The information was set.
615 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
616 @retval EFI_UNSUPPORTED Could not open the file path.
617 @retval EFI_NOT_FOUND The specified file could not be found on the
618 device or the file system could not be found
619 on the device.
620 @retval EFI_NO_MEDIA The device has no medium.
621 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
622 medium is no longer supported.
623 @retval EFI_DEVICE_ERROR The device reported an error.
624 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
625 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
626 @retval EFI_ACCESS_DENIED The file was opened read only.
627 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
628 file.
629 @retval EFI_VOLUME_FULL The volume is full.
630 @sa ShellOpenFileByName
631 **/
632 EFI_STATUS
633 EFIAPI
634 ShellCreateDirectory(
635 IN CHAR16 *DirectoryName,
636 OUT EFI_FILE_HANDLE *FileHandle
637 )
638 {
639 //
640 // this is a pass thru to the open file function with sepcific open mode and attributes
641 //
642 return (ShellOpenFileByName(DirectoryName,
643 FileHandle,
644 EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE,
645 EFI_FILE_DIRECTORY
646 ));
647 }
648
649 /**
650 This function reads information from an opened file.
651
652 If FileHandle is not a directory, the function reads the requested number of
653 bytes from the file at the file's current position and returns them in Buffer.
654 If the read goes beyond the end of the file, the read length is truncated to the
655 end of the file. The file's current position is increased by the number of bytes
656 returned. If FileHandle is a directory, the function reads the directory entry
657 at the file's current position and returns the entry in Buffer. If the Buffer
658 is not large enough to hold the current directory entry, then
659 EFI_BUFFER_TOO_SMALL is returned and the current file position is not updated.
660 BufferSize is set to be the size of the buffer needed to read the entry. On
661 success, the current position is updated to the next directory entry. If there
662 are no more directory entries, the read returns a zero-length buffer.
663 EFI_FILE_INFO is the structure returned as the directory entry.
664
665 @param FileHandle the opened file handle
666 @param BufferSize on input the size of buffer in bytes. on return
667 the number of bytes written.
668 @param Buffer the buffer to put read data into.
669
670 @retval EFI_SUCCESS Data was read.
671 @retval EFI_NO_MEDIA The device has no media.
672 @retval EFI_DEVICE_ERROR The device reported an error.
673 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
674 @retval EFI_BUFFER_TO_SMALL Buffer is too small. ReadSize contains required
675 size.
676
677 **/
678 EFI_STATUS
679 EFIAPI
680 ShellReadFile(
681 IN EFI_FILE_HANDLE FileHandle,
682 IN OUT UINTN *BufferSize,
683 OUT VOID *Buffer
684 )
685 {
686 return (FileFunctionMap.ReadFile(FileHandle, BufferSize, Buffer));
687 }
688
689
690 /**
691 Write data to a file.
692
693 This function writes the specified number of bytes to the file at the current
694 file position. The current file position is advanced the actual number of bytes
695 written, which is returned in BufferSize. Partial writes only occur when there
696 has been a data error during the write attempt (such as "volume space full").
697 The file is automatically grown to hold the data if required. Direct writes to
698 opened directories are not supported.
699
700 @param FileHandle The opened file for writing
701 @param BufferSize on input the number of bytes in Buffer. On output
702 the number of bytes written.
703 @param Buffer the buffer containing data to write is stored.
704
705 @retval EFI_SUCCESS Data was written.
706 @retval EFI_UNSUPPORTED Writes to an open directory are not supported.
707 @retval EFI_NO_MEDIA The device has no media.
708 @retval EFI_DEVICE_ERROR The device reported an error.
709 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
710 @retval EFI_WRITE_PROTECTED The device is write-protected.
711 @retval EFI_ACCESS_DENIED The file was open for read only.
712 @retval EFI_VOLUME_FULL The volume is full.
713 **/
714 EFI_STATUS
715 EFIAPI
716 ShellWriteFile(
717 IN EFI_FILE_HANDLE FileHandle,
718 IN OUT UINTN *BufferSize,
719 IN VOID *Buffer
720 )
721 {
722 return (FileFunctionMap.WriteFile(FileHandle, BufferSize, Buffer));
723 }
724
725 /**
726 Close an open file handle.
727
728 This function closes a specified file handle. All "dirty" cached file data is
729 flushed to the device, and the file is closed. In all cases the handle is
730 closed.
731
732 @param FileHandle the file handle to close.
733
734 @retval EFI_SUCCESS the file handle was closed sucessfully.
735 **/
736 EFI_STATUS
737 EFIAPI
738 ShellCloseFile (
739 IN EFI_FILE_HANDLE *FileHandle
740 )
741 {
742 return (FileFunctionMap.CloseFile(*FileHandle));
743 }
744
745 /**
746 Delete a file and close the handle
747
748 This function closes and deletes a file. In all cases the file handle is closed.
749 If the file cannot be deleted, the warning code EFI_WARN_DELETE_FAILURE is
750 returned, but the handle is still closed.
751
752 @param FileHandle the file handle to delete
753
754 @retval EFI_SUCCESS the file was closed sucessfully
755 @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not
756 deleted
757 @retval INVALID_PARAMETER One of the parameters has an invalid value.
758 **/
759 EFI_STATUS
760 EFIAPI
761 ShellDeleteFile (
762 IN EFI_FILE_HANDLE *FileHandle
763 )
764 {
765 return (FileFunctionMap.DeleteFile(*FileHandle));
766 }
767
768 /**
769 Set the current position in a file.
770
771 This function sets the current file position for the handle to the position
772 supplied. With the exception of seeking to position 0xFFFFFFFFFFFFFFFF, only
773 absolute positioning is supported, and seeking past the end of the file is
774 allowed (a subsequent write would grow the file). Seeking to position
775 0xFFFFFFFFFFFFFFFF causes the current position to be set to the end of the file.
776 If FileHandle is a directory, the only position that may be set is zero. This
777 has the effect of starting the read process of the directory entries over.
778
779 @param FileHandle The file handle on which the position is being set
780 @param Position Byte position from begining of file
781
782 @retval EFI_SUCCESS Operation completed sucessfully.
783 @retval EFI_UNSUPPORTED the seek request for non-zero is not valid on
784 directories.
785 @retval INVALID_PARAMETER One of the parameters has an invalid value.
786 **/
787 EFI_STATUS
788 EFIAPI
789 ShellSetFilePosition (
790 IN EFI_FILE_HANDLE FileHandle,
791 IN UINT64 Position
792 )
793 {
794 return (FileFunctionMap.SetFilePosition(FileHandle, Position));
795 }
796
797 /**
798 Gets a file's current position
799
800 This function retrieves the current file position for the file handle. For
801 directories, the current file position has no meaning outside of the file
802 system driver and as such the operation is not supported. An error is returned
803 if FileHandle is a directory.
804
805 @param FileHandle The open file handle on which to get the position.
806 @param Position Byte position from begining of file.
807
808 @retval EFI_SUCCESS the operation completed sucessfully.
809 @retval INVALID_PARAMETER One of the parameters has an invalid value.
810 @retval EFI_UNSUPPORTED the request is not valid on directories.
811 **/
812 EFI_STATUS
813 EFIAPI
814 ShellGetFilePosition (
815 IN EFI_FILE_HANDLE FileHandle,
816 OUT UINT64 *Position
817 )
818 {
819 return (FileFunctionMap.GetFilePosition(FileHandle, Position));
820 }
821 /**
822 Flushes data on a file
823
824 This function flushes all modified data associated with a file to a device.
825
826 @param FileHandle The file handle on which to flush data
827
828 @retval EFI_SUCCESS The data was flushed.
829 @retval EFI_NO_MEDIA The device has no media.
830 @retval EFI_DEVICE_ERROR The device reported an error.
831 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
832 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
833 @retval EFI_ACCESS_DENIED The file was opened for read only.
834 **/
835 EFI_STATUS
836 EFIAPI
837 ShellFlushFile (
838 IN EFI_FILE_HANDLE FileHandle
839 )
840 {
841 return (FileFunctionMap.FlushFile(FileHandle));
842 }
843
844 /**
845 Retrieves the first file from a directory
846
847 This function opens a directory and gets the first file's info in the
848 directory. Caller can use ShellFindNextFile() to get other files. When
849 complete the caller is responsible for calling FreePool() on Buffer.
850
851 @param DirHandle The file handle of the directory to search
852 @param Buffer Pointer to buffer for file's information
853
854 @retval EFI_SUCCESS Found the first file.
855 @retval EFI_NOT_FOUND Cannot find the directory.
856 @retval EFI_NO_MEDIA The device has no media.
857 @retval EFI_DEVICE_ERROR The device reported an error.
858 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
859 @return Others status of ShellGetFileInfo, ShellSetFilePosition,
860 or ShellReadFile
861 **/
862 EFI_STATUS
863 EFIAPI
864 ShellFindFirstFile (
865 IN EFI_FILE_HANDLE DirHandle,
866 OUT EFI_FILE_INFO **Buffer
867 )
868 {
869 //
870 // pass to file handle lib
871 //
872 return (FileHandleFindFirstFile(DirHandle, Buffer));
873 }
874 /**
875 Retrieves the next file in a directory.
876
877 To use this function, caller must call the LibFindFirstFile() to get the
878 first file, and then use this function get other files. This function can be
879 called for several times to get each file's information in the directory. If
880 the call of ShellFindNextFile() got the last file in the directory, the next
881 call of this function has no file to get. *NoFile will be set to TRUE and the
882 Buffer memory will be automatically freed.
883
884 @param DirHandle the file handle of the directory
885 @param Buffer pointer to buffer for file's information
886 @param NoFile pointer to boolean when last file is found
887
888 @retval EFI_SUCCESS Found the next file, or reached last file
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 **/
893 EFI_STATUS
894 EFIAPI
895 ShellFindNextFile(
896 IN EFI_FILE_HANDLE DirHandle,
897 OUT EFI_FILE_INFO *Buffer,
898 OUT BOOLEAN *NoFile
899 )
900 {
901 //
902 // pass to file handle lib
903 //
904 return (FileHandleFindNextFile(DirHandle, Buffer, NoFile));
905 }
906 /**
907 Retrieve the size of a file.
908
909 if FileHandle is NULL then ASSERT()
910 if Size is NULL then ASSERT()
911
912 This function extracts the file size info from the FileHandle's EFI_FILE_INFO
913 data.
914
915 @param FileHandle file handle from which size is retrieved
916 @param Size pointer to size
917
918 @retval EFI_SUCCESS operation was completed sucessfully
919 @retval EFI_DEVICE_ERROR cannot access the file
920 **/
921 EFI_STATUS
922 EFIAPI
923 ShellGetFileSize (
924 IN EFI_FILE_HANDLE FileHandle,
925 OUT UINT64 *Size
926 )
927 {
928 return (FileFunctionMap.GetFileSize(FileHandle, Size));
929 }
930 /**
931 Retrieves the status of the break execution flag
932
933 this function is useful to check whether the application is being asked to halt by the shell.
934
935 @retval TRUE the execution break is enabled
936 @retval FALSE the execution break is not enabled
937 **/
938 BOOLEAN
939 EFIAPI
940 ShellGetExecutionBreakFlag(
941 VOID
942 )
943 {
944 //
945 // Check for UEFI Shell 2.0 protocols
946 //
947 if (mEfiShellProtocol != NULL) {
948
949 //
950 // We are using UEFI Shell 2.0; see if the event has been triggered
951 //
952 if (gBS->CheckEvent(mEfiShellProtocol->ExecutionBreak) != EFI_SUCCESS) {
953 return (FALSE);
954 }
955 return (TRUE);
956 }
957
958 //
959 // using EFI Shell; call the function to check
960 //
961 ASSERT(mEfiShellEnvironment2 != NULL);
962 return (mEfiShellEnvironment2->GetExecutionBreak());
963 }
964 /**
965 return the value of an environment variable
966
967 this function gets the value of the environment variable set by the
968 ShellSetEnvironmentVariable function
969
970 @param EnvKey The key name of the environment variable.
971
972 @retval NULL the named environment variable does not exist.
973 @return != NULL pointer to the value of the environment variable
974 **/
975 CONST CHAR16*
976 EFIAPI
977 ShellGetEnvironmentVariable (
978 IN CHAR16 *EnvKey
979 )
980 {
981 //
982 // Check for UEFI Shell 2.0 protocols
983 //
984 if (mEfiShellProtocol != NULL) {
985 return (mEfiShellProtocol->GetEnv(EnvKey));
986 }
987
988 //
989 // ASSERT that we must have EFI shell
990 //
991 ASSERT(mEfiShellEnvironment2 != NULL);
992
993 //
994 // using EFI Shell
995 //
996 return (mEfiShellEnvironment2->GetEnv(EnvKey));
997 }
998 /**
999 set the value of an environment variable
1000
1001 This function changes the current value of the specified environment variable. If the
1002 environment variable exists and the Value is an empty string, then the environment
1003 variable is deleted. If the environment variable exists and the Value is not an empty
1004 string, then the value of the environment variable is changed. If the environment
1005 variable does not exist and the Value is an empty string, there is no action. If the
1006 environment variable does not exist and the Value is a non-empty string, then the
1007 environment variable is created and assigned the specified value.
1008
1009 This is not supported pre-UEFI Shell 2.0.
1010
1011 @param EnvKey The key name of the environment variable.
1012 @param EnvVal The Value of the environment variable
1013 @param Volatile Indicates whether the variable is non-volatile (FALSE) or volatile (TRUE).
1014
1015 @retval EFI_SUCCESS the operation was completed sucessfully
1016 @retval EFI_UNSUPPORTED This operation is not allowed in pre UEFI 2.0 Shell environments
1017 **/
1018 EFI_STATUS
1019 EFIAPI
1020 ShellSetEnvironmentVariable (
1021 IN CONST CHAR16 *EnvKey,
1022 IN CONST CHAR16 *EnvVal,
1023 IN BOOLEAN Volatile
1024 )
1025 {
1026 //
1027 // Check for UEFI Shell 2.0 protocols
1028 //
1029 if (mEfiShellProtocol != NULL) {
1030 return (mEfiShellProtocol->SetEnv(EnvKey, EnvVal, Volatile));
1031 }
1032
1033 //
1034 // This feature does not exist under EFI shell
1035 //
1036 return (EFI_UNSUPPORTED);
1037 }
1038 /**
1039 cause the shell to parse and execute a command line.
1040
1041 This function creates a nested instance of the shell and executes the specified
1042 command (CommandLine) with the specified environment (Environment). Upon return,
1043 the status code returned by the specified command is placed in StatusCode.
1044 If Environment is NULL, then the current environment is used and all changes made
1045 by the commands executed will be reflected in the current environment. If the
1046 Environment is non-NULL, then the changes made will be discarded.
1047 The CommandLine is executed from the current working directory on the current
1048 device.
1049
1050 EnvironmentVariables and Status are only supported for UEFI Shell 2.0.
1051 Output is only supported for pre-UEFI Shell 2.0
1052
1053 @param ImageHandle Parent image that is starting the operation
1054 @param CommandLine pointer to null terminated command line.
1055 @param Output true to display debug output. false to hide it.
1056 @param EnvironmentVariables optional pointer to array of environment variables
1057 in the form "x=y". if NULL current set is used.
1058 @param Status the status of the run command line.
1059
1060 @retval EFI_SUCCESS the operation completed sucessfully. Status
1061 contains the status code returned.
1062 @retval EFI_INVALID_PARAMETER a parameter contains an invalid value
1063 @retval EFI_OUT_OF_RESOURCES out of resources
1064 @retval EFI_UNSUPPORTED the operation is not allowed.
1065 **/
1066 EFI_STATUS
1067 EFIAPI
1068 ShellExecute (
1069 IN EFI_HANDLE *ParentHandle,
1070 IN CHAR16 *CommandLine OPTIONAL,
1071 IN BOOLEAN Output OPTIONAL,
1072 IN CHAR16 **EnvironmentVariables OPTIONAL,
1073 OUT EFI_STATUS *Status OPTIONAL
1074 )
1075 {
1076 //
1077 // Check for UEFI Shell 2.0 protocols
1078 //
1079 if (mEfiShellProtocol != NULL) {
1080 //
1081 // Call UEFI Shell 2.0 version (not using Output parameter)
1082 //
1083 return (mEfiShellProtocol->Execute(ParentHandle,
1084 CommandLine,
1085 EnvironmentVariables,
1086 Status));
1087 }
1088 //
1089 // ASSERT that we must have EFI shell
1090 //
1091 ASSERT(mEfiShellEnvironment2 != NULL);
1092 //
1093 // Call EFI Shell version (not using EnvironmentVariables or Status parameters)
1094 // Due to oddity in the EFI shell we want to dereference the ParentHandle here
1095 //
1096 return (mEfiShellEnvironment2->Execute(*ParentHandle,
1097 CommandLine,
1098 Output));
1099 }
1100 /**
1101 Retreives the current directory path
1102
1103 If the DeviceName is NULL, it returns the current device's current directory
1104 name. If the DeviceName is not NULL, it returns the current directory name
1105 on specified drive.
1106
1107 @param DeviceName the name of the drive to get directory on
1108
1109 @retval NULL the directory does not exist
1110 @return != NULL the directory
1111 **/
1112 CONST CHAR16*
1113 EFIAPI
1114 ShellGetCurrentDir (
1115 IN CHAR16 *DeviceName OPTIONAL
1116 )
1117 {
1118 //
1119 // Check for UEFI Shell 2.0 protocols
1120 //
1121 if (mEfiShellProtocol != NULL) {
1122 return (mEfiShellProtocol->GetCurDir(DeviceName));
1123 }
1124 //
1125 // ASSERT that we must have EFI shell
1126 //
1127 ASSERT(mEfiShellEnvironment2 != NULL);
1128 return (mEfiShellEnvironment2->CurDir(DeviceName));
1129 }
1130 /**
1131 sets (enabled or disabled) the page break mode
1132
1133 when page break mode is enabled the screen will stop scrolling
1134 and wait for operator input before scrolling a subsequent screen.
1135
1136 @param CurrentState TRUE to enable and FALSE to disable
1137 **/
1138 VOID
1139 EFIAPI
1140 ShellSetPageBreakMode (
1141 IN BOOLEAN CurrentState
1142 )
1143 {
1144 //
1145 // check for enabling
1146 //
1147 if (CurrentState != 0x00) {
1148 //
1149 // check for UEFI Shell 2.0
1150 //
1151 if (mEfiShellProtocol != NULL) {
1152 //
1153 // Enable with UEFI 2.0 Shell
1154 //
1155 mEfiShellProtocol->EnablePageBreak();
1156 return;
1157 } else {
1158 //
1159 // ASSERT that must have EFI Shell
1160 //
1161 ASSERT(mEfiShellEnvironment2 != NULL);
1162 //
1163 // Enable with EFI Shell
1164 //
1165 mEfiShellEnvironment2->EnablePageBreak (DEFAULT_INIT_ROW, DEFAULT_AUTO_LF);
1166 return;
1167 }
1168 } else {
1169 //
1170 // check for UEFI Shell 2.0
1171 //
1172 if (mEfiShellProtocol != NULL) {
1173 //
1174 // Disable with UEFI 2.0 Shell
1175 //
1176 mEfiShellProtocol->DisablePageBreak();
1177 return;
1178 } else {
1179 //
1180 // ASSERT that must have EFI Shell
1181 //
1182 ASSERT(mEfiShellEnvironment2 != NULL);
1183 //
1184 // Disable with EFI Shell
1185 //
1186 mEfiShellEnvironment2->DisablePageBreak ();
1187 return;
1188 }
1189 }
1190 }
1191
1192 ///
1193 /// version of EFI_SHELL_FILE_INFO struct, except has no CONST pointers.
1194 /// This allows for the struct to be populated.
1195 ///
1196 typedef struct {
1197 LIST_ENTRY Link;
1198 EFI_STATUS Status;
1199 CHAR16 *FullName;
1200 CHAR16 *FileName;
1201 EFI_FILE_HANDLE Handle;
1202 EFI_FILE_INFO *Info;
1203 } EFI_SHELL_FILE_INFO_NO_CONST;
1204
1205 /**
1206 Converts a EFI shell list of structures to the coresponding UEFI Shell 2.0 type of list.
1207
1208 if OldStyleFileList is NULL then ASSERT()
1209
1210 this function will convert a SHELL_FILE_ARG based list into a callee allocated
1211 EFI_SHELL_FILE_INFO based list. it is up to the caller to free the memory via
1212 the ShellCloseFileMetaArg function.
1213
1214 @param FileList the EFI shell list type
1215
1216 @retval the resultant head of the double linked new format list;
1217 **/
1218 LIST_ENTRY*
1219 EFIAPI
1220 InternalShellConvertFileListType (
1221 LIST_ENTRY *FileList
1222 )
1223 {
1224 LIST_ENTRY *ListHead;
1225 SHELL_FILE_ARG *OldInfo;
1226 LIST_ENTRY *Link;
1227 EFI_SHELL_FILE_INFO_NO_CONST *NewInfo;
1228
1229 //
1230 // ASSERT that FileList is not NULL
1231 //
1232 ASSERT(FileList != NULL);
1233
1234 //
1235 // Allocate our list head and initialize the list
1236 //
1237 ListHead = AllocateZeroPool(sizeof(LIST_ENTRY));
1238 ASSERT (ListHead != NULL);
1239 ListHead = InitializeListHead (ListHead);
1240
1241 //
1242 // enumerate through each member of the old list and copy
1243 //
1244 for (Link = FileList->ForwardLink; Link != FileList; Link = Link->ForwardLink) {
1245 OldInfo = CR (Link, SHELL_FILE_ARG, Link, SHELL_FILE_ARG_SIGNATURE);
1246
1247 //
1248 // make sure the old list was valid
1249 //
1250 ASSERT(OldInfo != NULL);
1251 ASSERT(OldInfo->Info != NULL);
1252 ASSERT(OldInfo->FullName != NULL);
1253 ASSERT(OldInfo->FileName != NULL);
1254
1255 //
1256 // allocate a new EFI_SHELL_FILE_INFO object
1257 //
1258 NewInfo = AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));
1259
1260 //
1261 // copy the simple items
1262 //
1263 NewInfo->Handle = OldInfo->Handle;
1264 NewInfo->Status = OldInfo->Status;
1265
1266 // old shell checks for 0 not NULL
1267 OldInfo->Handle = 0;
1268
1269 //
1270 // allocate new space to copy strings and structure
1271 //
1272 NewInfo->FullName = AllocateZeroPool(StrSize(OldInfo->FullName));
1273 NewInfo->FileName = AllocateZeroPool(StrSize(OldInfo->FileName));
1274 NewInfo->Info = AllocateZeroPool((UINTN)OldInfo->Info->Size);
1275
1276 //
1277 // make sure all the memory allocations were sucessful
1278 //
1279 ASSERT(NewInfo->FullName != NULL);
1280 ASSERT(NewInfo->FileName != NULL);
1281 ASSERT(NewInfo->Info != NULL);
1282
1283 //
1284 // Copt the strings and structure
1285 //
1286 StrCpy(NewInfo->FullName, OldInfo->FullName);
1287 StrCpy(NewInfo->FileName, OldInfo->FileName);
1288 gBS->CopyMem (NewInfo->Info, OldInfo->Info, (UINTN)OldInfo->Info->Size);
1289
1290 //
1291 // add that to the list
1292 //
1293 InsertTailList(ListHead, (LIST_ENTRY*)NewInfo);
1294 }
1295 return (ListHead);
1296 }
1297 /**
1298 Opens a group of files based on a path.
1299
1300 This function uses the Arg to open all the matching files. Each matched
1301 file has a SHELL_FILE_ARG structure to record the file information. These
1302 structures are placed on the list ListHead. Users can get the SHELL_FILE_ARG
1303 structures from ListHead to access each file. This function supports wildcards
1304 and will process '?' and '*' as such. the list must be freed with a call to
1305 ShellCloseFileMetaArg().
1306
1307 This function will fail if called sequentially without freeing the list in the middle.
1308
1309 @param Arg pointer to path string
1310 @param OpenMode mode to open files with
1311 @param ListHead head of linked list of results
1312
1313 @retval EFI_SUCCESS the operation was sucessful and the list head
1314 contains the list of opened files
1315 #retval EFI_UNSUPPORTED a previous ShellOpenFileMetaArg must be closed first.
1316 *ListHead is set to NULL.
1317 @return != EFI_SUCCESS the operation failed
1318
1319 @sa InternalShellConvertFileListType
1320 **/
1321 EFI_STATUS
1322 EFIAPI
1323 ShellOpenFileMetaArg (
1324 IN CHAR16 *Arg,
1325 IN UINT64 OpenMode,
1326 IN OUT EFI_SHELL_FILE_INFO **ListHead
1327 )
1328 {
1329 EFI_STATUS Status;
1330 LIST_ENTRY *EmptyNode;
1331 LIST_ENTRY *mOldStyleFileList;
1332
1333 //
1334 // ASSERT that Arg and ListHead are not NULL
1335 //
1336 ASSERT(Arg != NULL);
1337 ASSERT(ListHead != NULL);
1338
1339 //
1340 // Check for UEFI Shell 2.0 protocols
1341 //
1342 if (mEfiShellProtocol != NULL) {
1343 return (mEfiShellProtocol->OpenFileList(Arg,
1344 OpenMode,
1345 ListHead));
1346 }
1347
1348 //
1349 // ASSERT that we must have EFI shell
1350 //
1351 ASSERT(mEfiShellEnvironment2 != NULL);
1352
1353 //
1354 // allocate memory for old list head
1355 //
1356 mOldStyleFileList = (LIST_ENTRY*)AllocatePool(sizeof(LIST_ENTRY));
1357 ASSERT(mOldStyleFileList != NULL);
1358
1359 //
1360 // make sure the list head is initialized
1361 //
1362 InitializeListHead((LIST_ENTRY*)mOldStyleFileList);
1363
1364 //
1365 // Get the EFI Shell list of files
1366 //
1367 Status = mEfiShellEnvironment2->FileMetaArg(Arg, mOldStyleFileList);
1368 if (EFI_ERROR(Status)) {
1369 *ListHead = NULL;
1370 return (Status);
1371 }
1372
1373 //
1374 // Convert that to equivalent of UEFI Shell 2.0 structure
1375 //
1376 EmptyNode = InternalShellConvertFileListType(mOldStyleFileList);
1377
1378 //
1379 // Free the EFI Shell version that was converted.
1380 //
1381 ASSERT_EFI_ERROR(mEfiShellEnvironment2->FreeFileList(mOldStyleFileList));
1382 FreePool(mOldStyleFileList);
1383 mOldStyleFileList = NULL;
1384
1385 //
1386 // remove the empty head of the list
1387 //
1388 *ListHead = (EFI_SHELL_FILE_INFO*)RemoveEntryList(EmptyNode);
1389 FreePool(EmptyNode);
1390
1391 return (Status);
1392 }
1393 /**
1394 Free the linked list returned from ShellOpenFileMetaArg
1395
1396 if ListHead is NULL then ASSERT()
1397
1398 @param ListHead the pointer to free
1399
1400 @retval EFI_SUCCESS the operation was sucessful
1401 **/
1402 EFI_STATUS
1403 EFIAPI
1404 ShellCloseFileMetaArg (
1405 IN OUT EFI_SHELL_FILE_INFO **ListHead
1406 )
1407 {
1408 LIST_ENTRY *Node;
1409
1410 //
1411 // ASSERT that ListHead is not NULL
1412 //
1413 ASSERT(ListHead != NULL);
1414
1415 //
1416 // Check for UEFI Shell 2.0 protocols
1417 //
1418 if (mEfiShellProtocol != NULL) {
1419 return (mEfiShellProtocol->FreeFileList(ListHead));
1420 } else {
1421 //
1422 // Since this is EFI Shell version we need to free our internally made copy
1423 // of the list
1424 //
1425 for (Node = GetFirstNode((LIST_ENTRY*)*ListHead) ; IsListEmpty((LIST_ENTRY*)*ListHead) == FALSE ; Node = GetFirstNode((LIST_ENTRY*)*ListHead)) {
1426 RemoveEntryList(Node);
1427 ((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->Handle->Close(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->Handle);
1428 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->FullName);
1429 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->FileName);
1430 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->Info);
1431 FreePool((EFI_SHELL_FILE_INFO_NO_CONST*)Node);
1432 }
1433 return EFI_SUCCESS;
1434 }
1435 }
1436
1437 typedef struct {
1438 LIST_ENTRY List;
1439 CHAR16 *Name;
1440 ParamType Type;
1441 CHAR16 *Value;
1442 UINTN OriginalPosition;
1443 } SHELL_PARAM_PACKAGE;
1444
1445 /**
1446 Checks the list of valid arguments and returns TRUE if the item was found. If the
1447 return value is TRUE then the type parameter is set also.
1448
1449 if CheckList is NULL then ASSERT();
1450 if Name is NULL then ASSERT();
1451 if Type is NULL then ASSERT();
1452
1453 @param Type pointer to type of parameter if it was found
1454 @param Name pointer to Name of parameter found
1455 @param CheckList List to check against
1456
1457 @retval TRUE the Parameter was found. Type is valid.
1458 @retval FALSE the Parameter was not found. Type is not valid.
1459 **/
1460 BOOLEAN
1461 EFIAPI
1462 InternalIsOnCheckList (
1463 IN CONST CHAR16 *Name,
1464 IN CONST SHELL_PARAM_ITEM *CheckList,
1465 OUT ParamType *Type
1466 )
1467 {
1468 SHELL_PARAM_ITEM *TempListItem;
1469
1470 //
1471 // ASSERT that all 3 pointer parameters aren't NULL
1472 //
1473 ASSERT(CheckList != NULL);
1474 ASSERT(Type != NULL);
1475 ASSERT(Name != NULL);
1476
1477 //
1478 // question mark and page break mode are always supported
1479 //
1480 if ((StrCmp(Name, L"-?") == 0) ||
1481 (StrCmp(Name, L"-b") == 0)
1482 ) {
1483 return (TRUE);
1484 }
1485
1486 //
1487 // Enumerate through the list
1488 //
1489 for (TempListItem = (SHELL_PARAM_ITEM*)CheckList ; TempListItem->Name != NULL ; TempListItem++) {
1490 //
1491 // If the Name matches set the type and return TRUE
1492 //
1493 if (StrCmp(Name, TempListItem->Name) == 0) {
1494 *Type = TempListItem->Type;
1495 return (TRUE);
1496 }
1497 }
1498 return (FALSE);
1499 }
1500 /**
1501 Checks the string for indicators of "flag" status. this is a leading '/', '-', or '+'
1502
1503 @param Name pointer to Name of parameter found
1504
1505 @retval TRUE the Parameter is a flag.
1506 @retval FALSE the Parameter not a flag
1507 **/
1508 BOOLEAN
1509 EFIAPI
1510 InternalIsFlag (
1511 IN CONST CHAR16 *Name
1512 )
1513 {
1514 //
1515 // ASSERT that Name isn't NULL
1516 //
1517 ASSERT(Name != NULL);
1518
1519 //
1520 // If the Name has a / or - as the first character return TRUE
1521 //
1522 if ((Name[0] == L'/') ||
1523 (Name[0] == L'-') ||
1524 (Name[0] == L'+')
1525 ) {
1526 return (TRUE);
1527 }
1528 return (FALSE);
1529 }
1530
1531 /**
1532 Checks the command line arguments passed against the list of valid ones.
1533
1534 If no initialization is required, then return RETURN_SUCCESS.
1535
1536 @param CheckList pointer to list of parameters to check
1537 @param CheckPackage pointer to pointer to list checked values
1538 @param ProblemParam optional pointer to pointer to unicode string for
1539 the paramater that caused failure. If used then the
1540 caller is responsible for freeing the memory.
1541 @param AutoPageBreak will automatically set PageBreakEnabled for "b" parameter
1542 @param Argc Count of parameters in Argv
1543 @param Argv pointer to array of parameters
1544
1545 @retval EFI_SUCCESS The operation completed sucessfully.
1546 @retval EFI_OUT_OF_RESOURCES A memory allocation failed
1547 @retval EFI_INVALID_PARAMETER A parameter was invalid
1548 @retval EFI_VOLUME_CORRUPTED the command line was corrupt. an argument was
1549 duplicated. the duplicated command line argument
1550 was returned in ProblemParam if provided.
1551 @retval EFI_NOT_FOUND a argument required a value that was missing.
1552 the invalid command line argument was returned in
1553 ProblemParam if provided.
1554 **/
1555 EFI_STATUS
1556 EFIAPI
1557 InternalCommandLineParse (
1558 IN CONST SHELL_PARAM_ITEM *CheckList,
1559 OUT LIST_ENTRY **CheckPackage,
1560 OUT CHAR16 **ProblemParam OPTIONAL,
1561 IN BOOLEAN AutoPageBreak,
1562 IN CONST CHAR16 **Argv,
1563 IN UINTN Argc
1564 )
1565 {
1566 UINTN LoopCounter;
1567 UINTN Count;
1568 ParamType CurrentItemType;
1569 SHELL_PARAM_PACKAGE *CurrentItemPackage;
1570 BOOLEAN GetItemValue;
1571
1572 CurrentItemPackage = NULL;
1573
1574 //
1575 // ASSERTs
1576 //
1577 ASSERT(CheckList != NULL);
1578 ASSERT(Argv != NULL);
1579
1580 Count = 0;
1581 GetItemValue = FALSE;
1582
1583 //
1584 // If there is only 1 item we dont need to do anything
1585 //
1586 if (Argc <= 1) {
1587 *CheckPackage = NULL;
1588 return (EFI_SUCCESS);
1589 }
1590
1591 //
1592 // initialize the linked list
1593 //
1594 *CheckPackage = (LIST_ENTRY*)AllocateZeroPool(sizeof(LIST_ENTRY));
1595 InitializeListHead(*CheckPackage);
1596
1597 //
1598 // loop through each of the arguments
1599 //
1600 for (LoopCounter = 0 ; LoopCounter < Argc ; ++LoopCounter) {
1601 if (Argv[LoopCounter] == NULL) {
1602 //
1603 // do nothing for NULL argv
1604 //
1605 } else if (GetItemValue == TRUE) {
1606 ASSERT(CurrentItemPackage != NULL);
1607 //
1608 // get the item VALUE for the previous flag
1609 //
1610 GetItemValue = FALSE;
1611 CurrentItemPackage->Value = AllocateZeroPool(StrSize(Argv[LoopCounter]));
1612 ASSERT(CurrentItemPackage->Value != NULL);
1613 StrCpy(CurrentItemPackage->Value, Argv[LoopCounter]);
1614 InsertTailList(*CheckPackage, (LIST_ENTRY*)CurrentItemPackage);
1615 } else if (InternalIsFlag(Argv[LoopCounter]) == FALSE) {
1616 //
1617 // add this one as a non-flag
1618 //
1619 CurrentItemPackage = AllocatePool(sizeof(SHELL_PARAM_PACKAGE));
1620 ASSERT(CurrentItemPackage != NULL);
1621 CurrentItemPackage->Name = NULL;
1622 CurrentItemPackage->Type = TypePosition;
1623 CurrentItemPackage->Value = AllocatePool(StrSize(Argv[LoopCounter]));
1624 ASSERT(CurrentItemPackage->Value != NULL);
1625 StrCpy(CurrentItemPackage->Value, Argv[LoopCounter]);
1626 CurrentItemPackage->OriginalPosition = Count++;
1627 InsertTailList(*CheckPackage, (LIST_ENTRY*)CurrentItemPackage);
1628 } else if (InternalIsOnCheckList(Argv[LoopCounter], CheckList, &CurrentItemType) == TRUE) {
1629 //
1630 // this is a flag
1631 //
1632 CurrentItemPackage = AllocatePool(sizeof(SHELL_PARAM_PACKAGE));
1633 ASSERT(CurrentItemPackage != NULL);
1634 CurrentItemPackage->Name = AllocatePool(StrSize(Argv[LoopCounter]));
1635 ASSERT(CurrentItemPackage->Name != NULL);
1636 StrCpy(CurrentItemPackage->Name, Argv[LoopCounter]);
1637 CurrentItemPackage->Type = CurrentItemType;
1638 CurrentItemPackage->OriginalPosition = (UINTN)(-1);
1639
1640 //
1641 // Does this flag require a value
1642 //
1643 if (CurrentItemPackage->Type == TypeValue) {
1644 //
1645 // trigger the next loop to populate the value of this item
1646 //
1647 GetItemValue = TRUE;
1648 } else {
1649 //
1650 // this item has no value expected; we are done
1651 //
1652 CurrentItemPackage->Value = NULL;
1653 InsertTailList(*CheckPackage, (LIST_ENTRY*)CurrentItemPackage);
1654 }
1655 } else if (ProblemParam) {
1656 //
1657 // this was a non-recognised flag... error!
1658 //
1659 *ProblemParam = AllocatePool(StrSize(Argv[LoopCounter]));
1660 ASSERT(*ProblemParam != NULL);
1661 StrCpy(*ProblemParam, Argv[LoopCounter]);
1662 ShellCommandLineFreeVarList(*CheckPackage);
1663 *CheckPackage = NULL;
1664 return (EFI_VOLUME_CORRUPTED);
1665 } else {
1666 ShellCommandLineFreeVarList(*CheckPackage);
1667 *CheckPackage = NULL;
1668 return (EFI_VOLUME_CORRUPTED);
1669 }
1670 }
1671 //
1672 // support for AutoPageBreak
1673 //
1674 if (AutoPageBreak && ShellCommandLineGetFlag(*CheckPackage, L"-b")) {
1675 ShellSetPageBreakMode(TRUE);
1676 }
1677 return (EFI_SUCCESS);
1678 }
1679
1680 /**
1681 Checks the command line arguments passed against the list of valid ones.
1682 Optionally removes NULL values first.
1683
1684 If no initialization is required, then return RETURN_SUCCESS.
1685
1686 @param CheckList pointer to list of parameters to check
1687 @param CheckPackage pointer to pointer to list checked values
1688 @param ProblemParam optional pointer to pointer to unicode string for
1689 the paramater that caused failure.
1690 @param AutoPageBreak will automatically set PageBreakEnabled for "b" parameter
1691
1692 @retval EFI_SUCCESS The operation completed sucessfully.
1693 @retval EFI_OUT_OF_RESOURCES A memory allocation failed
1694 @retval EFI_INVALID_PARAMETER A parameter was invalid
1695 @retval EFI_VOLUME_CORRUPTED the command line was corrupt. an argument was
1696 duplicated. the duplicated command line argument
1697 was returned in ProblemParam if provided.
1698 @retval EFI_DEVICE_ERROR the commands contained 2 opposing arguments. one
1699 of the command line arguments was returned in
1700 ProblemParam if provided.
1701 @retval EFI_NOT_FOUND a argument required a value that was missing.
1702 the invalid command line argument was returned in
1703 ProblemParam if provided.
1704 **/
1705 EFI_STATUS
1706 EFIAPI
1707 ShellCommandLineParse (
1708 IN CONST SHELL_PARAM_ITEM *CheckList,
1709 OUT LIST_ENTRY **CheckPackage,
1710 OUT CHAR16 **ProblemParam OPTIONAL,
1711 IN BOOLEAN AutoPageBreak
1712 )
1713 {
1714 //
1715 // ASSERT that CheckList and CheckPackage aren't NULL
1716 //
1717 ASSERT(CheckList != NULL);
1718 ASSERT(CheckPackage != NULL);
1719
1720 //
1721 // Check for UEFI Shell 2.0 protocols
1722 //
1723 if (mEfiShellParametersProtocol != NULL) {
1724 return (InternalCommandLineParse(CheckList,
1725 CheckPackage,
1726 ProblemParam,
1727 AutoPageBreak,
1728 (CONST CHAR16**) mEfiShellParametersProtocol->Argv,
1729 mEfiShellParametersProtocol->Argc ));
1730 }
1731
1732 //
1733 // ASSERT That EFI Shell is not required
1734 //
1735 ASSERT (mEfiShellInterface != NULL);
1736 return (InternalCommandLineParse(CheckList,
1737 CheckPackage,
1738 ProblemParam,
1739 AutoPageBreak,
1740 (CONST CHAR16**) mEfiShellInterface->Argv,
1741 mEfiShellInterface->Argc ));
1742 }
1743
1744 /**
1745 Frees shell variable list that was returned from ShellCommandLineParse.
1746
1747 This function will free all the memory that was used for the CheckPackage
1748 list of postprocessed shell arguments.
1749
1750 this function has no return value.
1751
1752 if CheckPackage is NULL, then return
1753
1754 @param CheckPackage the list to de-allocate
1755 **/
1756 VOID
1757 EFIAPI
1758 ShellCommandLineFreeVarList (
1759 IN LIST_ENTRY *CheckPackage
1760 )
1761 {
1762 LIST_ENTRY *Node;
1763
1764 //
1765 // check for CheckPackage == NULL
1766 //
1767 if (CheckPackage == NULL) {
1768 return;
1769 }
1770
1771 //
1772 // for each node in the list
1773 //
1774 for (Node = GetFirstNode(CheckPackage); Node != CheckPackage ; Node = GetFirstNode(CheckPackage)) {
1775 //
1776 // Remove it from the list
1777 //
1778 RemoveEntryList(Node);
1779
1780 //
1781 // if it has a name free the name
1782 //
1783 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {
1784 FreePool(((SHELL_PARAM_PACKAGE*)Node)->Name);
1785 }
1786
1787 //
1788 // if it has a value free the value
1789 //
1790 if (((SHELL_PARAM_PACKAGE*)Node)->Value != NULL) {
1791 FreePool(((SHELL_PARAM_PACKAGE*)Node)->Value);
1792 }
1793
1794 //
1795 // free the node structure
1796 //
1797 FreePool((SHELL_PARAM_PACKAGE*)Node);
1798 }
1799 //
1800 // free the list head node
1801 //
1802 FreePool(CheckPackage);
1803 }
1804 /**
1805 Checks for presence of a flag parameter
1806
1807 flag arguments are in the form of "-<Key>" or "/<Key>", but do not have a value following the key
1808
1809 if CheckPackage is NULL then return FALSE.
1810 if KeyString is NULL then ASSERT()
1811
1812 @param CheckPackage The package of parsed command line arguments
1813 @param KeyString the Key of the command line argument to check for
1814
1815 @retval TRUE the flag is on the command line
1816 @retval FALSE the flag is not on the command line
1817 **/
1818 BOOLEAN
1819 EFIAPI
1820 ShellCommandLineGetFlag (
1821 IN CONST LIST_ENTRY *CheckPackage,
1822 IN CHAR16 *KeyString
1823 )
1824 {
1825 LIST_ENTRY *Node;
1826
1827 //
1828 // ASSERT that both CheckPackage and KeyString aren't NULL
1829 //
1830 ASSERT(KeyString != NULL);
1831
1832 //
1833 // return FALSE for no package
1834 //
1835 if (CheckPackage == NULL) {
1836 return (FALSE);
1837 }
1838
1839 //
1840 // enumerate through the list of parametrs
1841 //
1842 for (Node = GetFirstNode(CheckPackage) ; !IsNull (CheckPackage, Node) ; Node = GetNextNode(CheckPackage, Node) ) {
1843 //
1844 // If the Name matches, return TRUE (and there may be NULL name)
1845 //
1846 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {
1847 if (StrCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {
1848 return (TRUE);
1849 }
1850 }
1851 }
1852 return (FALSE);
1853 }
1854 /**
1855 returns value from command line argument
1856
1857 value parameters are in the form of "-<Key> value" or "/<Key> value"
1858
1859 if CheckPackage is NULL, then return NULL;
1860
1861 @param CheckPackage The package of parsed command line arguments
1862 @param KeyString the Key of the command line argument to check for
1863
1864 @retval NULL the flag is not on the command line
1865 @return !=NULL pointer to unicode string of the value
1866 **/
1867 CONST CHAR16*
1868 EFIAPI
1869 ShellCommandLineGetValue (
1870 IN CONST LIST_ENTRY *CheckPackage,
1871 IN CHAR16 *KeyString
1872 )
1873 {
1874 LIST_ENTRY *Node;
1875
1876 //
1877 // check for CheckPackage == NULL
1878 //
1879 if (CheckPackage == NULL) {
1880 return (NULL);
1881 }
1882
1883 //
1884 // enumerate through the list of parametrs
1885 //
1886 for (Node = GetFirstNode(CheckPackage) ; !IsNull (CheckPackage, Node) ; Node = GetNextNode(CheckPackage, Node) ) {
1887 //
1888 // If the Name matches, return the value (name can be NULL)
1889 //
1890 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {
1891 if (StrCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {
1892 return (((SHELL_PARAM_PACKAGE*)Node)->Value);
1893 }
1894 }
1895 }
1896 return (NULL);
1897 }
1898 /**
1899 returns raw value from command line argument
1900
1901 raw value parameters are in the form of "value" in a specific position in the list
1902
1903 if CheckPackage is NULL, then return NULL;
1904
1905 @param CheckPackage The package of parsed command line arguments
1906 @param Position the position of the value
1907
1908 @retval NULL the flag is not on the command line
1909 @return !=NULL pointer to unicode string of the value
1910 **/
1911 CONST CHAR16*
1912 EFIAPI
1913 ShellCommandLineGetRawValue (
1914 IN CONST LIST_ENTRY *CheckPackage,
1915 IN UINT32 Position
1916 )
1917 {
1918 LIST_ENTRY *Node;
1919
1920 //
1921 // check for CheckPackage == NULL
1922 //
1923 if (CheckPackage == NULL) {
1924 return (NULL);
1925 }
1926
1927 //
1928 // enumerate through the list of parametrs
1929 //
1930 for (Node = GetFirstNode(CheckPackage) ; !IsNull (CheckPackage, Node) ; Node = GetNextNode(CheckPackage, Node) ) {
1931 //
1932 // If the position matches, return the value
1933 //
1934 if (((SHELL_PARAM_PACKAGE*)Node)->OriginalPosition == Position) {
1935 return (((SHELL_PARAM_PACKAGE*)Node)->Value);
1936 }
1937 }
1938 return (NULL);
1939 }