blob: fcb7e430ed6ec260217cc4d468fab3030a789396 [file] [log] [blame]
jcarsey94b17fa2009-05-07 18:46:18 +00001/** @file
2 Provides interface to shell functionality for shell commands and applications.
3
jcarsey1e6e84c2010-01-25 20:05:08 +00004 Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
jcarseyb3011f42010-01-11 21:49:04 +00006 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
jcarsey94b17fa2009-05-07 18:46:18 +00009
jcarseyb3011f42010-01-11 21:49:04 +000010 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.
jcarsey94b17fa2009-05-07 18:46:18 +000012
13**/
14
jcarseyb1f95a02009-06-16 00:23:19 +000015#include "UefiShellLib.h"
jcarseyd2b45642009-05-11 18:02:16 +000016
jcarsey94b17fa2009-05-07 18:46:18 +000017#define MAX_FILE_NAME_LEN 522 // (20 * (6+5+2))+1) unicode characters from EFI FAT spec (doubled for bytes)
18#define FIND_XXXXX_FILE_BUFFER_SIZE (SIZE_OF_EFI_FILE_INFO + MAX_FILE_NAME_LEN)
19
jcarseyd2b45642009-05-11 18:02:16 +000020//
21// This is not static since it's extern in the .h file
22//
23SHELL_PARAM_ITEM EmptyParamList[] = {
24 {NULL, TypeMax}
25 };
26
27//
28// Static file globals for the shell library
29//
30STATIC EFI_SHELL_ENVIRONMENT2 *mEfiShellEnvironment2;
31STATIC EFI_SHELL_INTERFACE *mEfiShellInterface;
32STATIC EFI_SHELL_PROTOCOL *mEfiShellProtocol;
33STATIC EFI_SHELL_PARAMETERS_PROTOCOL *mEfiShellParametersProtocol;
34STATIC EFI_HANDLE mEfiShellEnvironment2Handle;
35STATIC FILE_HANDLE_FUNCTION_MAP FileFunctionMap;
jcarsey2247dde2009-11-09 18:08:58 +000036STATIC UINTN mTotalParameterCount;
jcarseyecd3d592009-12-07 18:05:00 +000037STATIC CHAR16 *mPostReplaceFormat;
38STATIC CHAR16 *mPostReplaceFormat2;
jcarseyb3011f42010-01-11 21:49:04 +000039
jcarsey2247dde2009-11-09 18:08:58 +000040/**
41 Check if a Unicode character is a hexadecimal character.
42
jcarsey1e6e84c2010-01-25 20:05:08 +000043 This internal function checks if a Unicode character is a
44 decimal character. The valid hexadecimal character is
jcarsey2247dde2009-11-09 18:08:58 +000045 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.
46
47
48 @param Char The character to check against.
49
50 @retval TRUE If the Char is a hexadecmial character.
51 @retval FALSE If the Char is not a hexadecmial character.
52
53**/
54BOOLEAN
55EFIAPI
jcarsey969c7832010-01-13 16:46:33 +000056ShellIsHexaDecimalDigitCharacter (
jcarsey2247dde2009-11-09 18:08:58 +000057 IN CHAR16 Char
58 ) {
59 return (BOOLEAN) ((Char >= L'0' && Char <= L'9') || (Char >= L'A' && Char <= L'F') || (Char >= L'a' && Char <= L'f'));
60}
jcarsey94b17fa2009-05-07 18:46:18 +000061
62/**
63 helper function to find ShellEnvironment2 for constructor
64**/
65EFI_STATUS
66EFIAPI
67ShellFindSE2 (
68 IN EFI_HANDLE ImageHandle
jcarsey2247dde2009-11-09 18:08:58 +000069 ) {
jcarsey94b17fa2009-05-07 18:46:18 +000070 EFI_STATUS Status;
71 EFI_HANDLE *Buffer;
72 UINTN BufferSize;
73 UINTN HandleIndex;
74
75 BufferSize = 0;
76 Buffer = NULL;
jcarsey1e6e84c2010-01-25 20:05:08 +000077 Status = gBS->OpenProtocol(ImageHandle,
jcarsey94b17fa2009-05-07 18:46:18 +000078 &gEfiShellEnvironment2Guid,
79 (VOID **)&mEfiShellEnvironment2,
80 ImageHandle,
81 NULL,
82 EFI_OPEN_PROTOCOL_GET_PROTOCOL
83 );
84 //
85 // look for the mEfiShellEnvironment2 protocol at a higher level
86 //
jcarsey9eb53ac2009-07-08 17:26:58 +000087 if (EFI_ERROR (Status) || !(CompareGuid (&mEfiShellEnvironment2->SESGuid, &gEfiShellEnvironment2ExtGuid) != FALSE)){
jcarsey94b17fa2009-05-07 18:46:18 +000088 //
89 // figure out how big of a buffer we need.
90 //
91 Status = gBS->LocateHandle (ByProtocol,
92 &gEfiShellEnvironment2Guid,
93 NULL, // ignored for ByProtocol
94 &BufferSize,
95 Buffer
96 );
jcarsey2247dde2009-11-09 18:08:58 +000097 //
98 // maybe it's not there???
99 //
100 if (Status == EFI_BUFFER_TOO_SMALL) {
101 Buffer = (EFI_HANDLE*)AllocatePool(BufferSize);
102 ASSERT(Buffer != NULL);
103 Status = gBS->LocateHandle (ByProtocol,
104 &gEfiShellEnvironment2Guid,
105 NULL, // ignored for ByProtocol
106 &BufferSize,
107 Buffer
108 );
109 }
jcarsey1cd45e72010-01-29 15:07:44 +0000110 if (!EFI_ERROR (Status) && Buffer != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +0000111 //
112 // now parse the list of returned handles
113 //
114 Status = EFI_NOT_FOUND;
115 for (HandleIndex = 0; HandleIndex < (BufferSize/sizeof(Buffer[0])); HandleIndex++) {
jcarsey1e6e84c2010-01-25 20:05:08 +0000116 Status = gBS->OpenProtocol(Buffer[HandleIndex],
jcarsey94b17fa2009-05-07 18:46:18 +0000117 &gEfiShellEnvironment2Guid,
118 (VOID **)&mEfiShellEnvironment2,
119 ImageHandle,
120 NULL,
121 EFI_OPEN_PROTOCOL_GET_PROTOCOL
122 );
jcarsey9eb53ac2009-07-08 17:26:58 +0000123 if (CompareGuid (&mEfiShellEnvironment2->SESGuid, &gEfiShellEnvironment2ExtGuid) != FALSE) {
jcarsey94b17fa2009-05-07 18:46:18 +0000124 mEfiShellEnvironment2Handle = Buffer[HandleIndex];
125 Status = EFI_SUCCESS;
126 break;
127 }
128 }
129 }
130 }
131 if (Buffer != NULL) {
132 FreePool (Buffer);
133 }
134 return (Status);
135}
136
jcarsey94b17fa2009-05-07 18:46:18 +0000137EFI_STATUS
138EFIAPI
jcarseyd2b45642009-05-11 18:02:16 +0000139ShellLibConstructorWorker (
jcarsey94b17fa2009-05-07 18:46:18 +0000140 IN EFI_HANDLE ImageHandle,
141 IN EFI_SYSTEM_TABLE *SystemTable
jcarsey2247dde2009-11-09 18:08:58 +0000142 ) {
jcarsey94b17fa2009-05-07 18:46:18 +0000143 EFI_STATUS Status;
144
jcarseyb3011f42010-01-11 21:49:04 +0000145 mPostReplaceFormat = AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize));
jcarseyecd3d592009-12-07 18:05:00 +0000146 ASSERT (mPostReplaceFormat != NULL);
jcarseyb3011f42010-01-11 21:49:04 +0000147 mPostReplaceFormat2 = AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize));
jcarseyecd3d592009-12-07 18:05:00 +0000148 ASSERT (mPostReplaceFormat2 != NULL);
149
jcarsey94b17fa2009-05-07 18:46:18 +0000150 //
jcarsey2247dde2009-11-09 18:08:58 +0000151 // Set the parameter count to an invalid number
152 //
153 mTotalParameterCount = (UINTN)(-1);
154
155 //
jcarsey94b17fa2009-05-07 18:46:18 +0000156 // UEFI 2.0 shell interfaces (used preferentially)
157 //
jcarsey1e6e84c2010-01-25 20:05:08 +0000158 Status = gBS->OpenProtocol(ImageHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000159 &gEfiShellProtocolGuid,
160 (VOID **)&mEfiShellProtocol,
161 ImageHandle,
162 NULL,
163 EFI_OPEN_PROTOCOL_GET_PROTOCOL
164 );
165 if (EFI_ERROR(Status)) {
166 mEfiShellProtocol = NULL;
167 }
jcarsey1e6e84c2010-01-25 20:05:08 +0000168 Status = gBS->OpenProtocol(ImageHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000169 &gEfiShellParametersProtocolGuid,
170 (VOID **)&mEfiShellParametersProtocol,
171 ImageHandle,
172 NULL,
173 EFI_OPEN_PROTOCOL_GET_PROTOCOL
174 );
175 if (EFI_ERROR(Status)) {
176 mEfiShellParametersProtocol = NULL;
177 }
178
179 if (mEfiShellParametersProtocol == NULL || mEfiShellProtocol == NULL) {
180 //
181 // Moved to seperate function due to complexity
182 //
183 Status = ShellFindSE2(ImageHandle);
184
185 if (EFI_ERROR(Status)) {
186 DEBUG((DEBUG_ERROR, "Status: 0x%08x\r\n", Status));
187 mEfiShellEnvironment2 = NULL;
188 }
jcarsey1e6e84c2010-01-25 20:05:08 +0000189 Status = gBS->OpenProtocol(ImageHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000190 &gEfiShellInterfaceGuid,
191 (VOID **)&mEfiShellInterface,
192 ImageHandle,
193 NULL,
194 EFI_OPEN_PROTOCOL_GET_PROTOCOL
195 );
196 if (EFI_ERROR(Status)) {
197 mEfiShellInterface = NULL;
198 }
199 }
jcarseyc9d92df2010-02-03 15:37:54 +0000200
jcarsey94b17fa2009-05-07 18:46:18 +0000201 //
202 // only success getting 2 of either the old or new, but no 1/2 and 1/2
203 //
jcarsey1e6e84c2010-01-25 20:05:08 +0000204 if ((mEfiShellEnvironment2 != NULL && mEfiShellInterface != NULL) ||
jcarsey94b17fa2009-05-07 18:46:18 +0000205 (mEfiShellProtocol != NULL && mEfiShellParametersProtocol != NULL) ) {
jcarseyd2b45642009-05-11 18:02:16 +0000206 if (mEfiShellProtocol != NULL) {
207 FileFunctionMap.GetFileInfo = mEfiShellProtocol->GetFileInfo;
208 FileFunctionMap.SetFileInfo = mEfiShellProtocol->SetFileInfo;
209 FileFunctionMap.ReadFile = mEfiShellProtocol->ReadFile;
210 FileFunctionMap.WriteFile = mEfiShellProtocol->WriteFile;
211 FileFunctionMap.CloseFile = mEfiShellProtocol->CloseFile;
212 FileFunctionMap.DeleteFile = mEfiShellProtocol->DeleteFile;
213 FileFunctionMap.GetFilePosition = mEfiShellProtocol->GetFilePosition;
214 FileFunctionMap.SetFilePosition = mEfiShellProtocol->SetFilePosition;
215 FileFunctionMap.FlushFile = mEfiShellProtocol->FlushFile;
216 FileFunctionMap.GetFileSize = mEfiShellProtocol->GetFileSize;
217 } else {
218 FileFunctionMap.GetFileInfo = FileHandleGetInfo;
219 FileFunctionMap.SetFileInfo = FileHandleSetInfo;
220 FileFunctionMap.ReadFile = FileHandleRead;
221 FileFunctionMap.WriteFile = FileHandleWrite;
222 FileFunctionMap.CloseFile = FileHandleClose;
223 FileFunctionMap.DeleteFile = FileHandleDelete;
224 FileFunctionMap.GetFilePosition = FileHandleGetPosition;
225 FileFunctionMap.SetFilePosition = FileHandleSetPosition;
226 FileFunctionMap.FlushFile = FileHandleFlush;
227 FileFunctionMap.GetFileSize = FileHandleGetSize;
228 }
jcarsey94b17fa2009-05-07 18:46:18 +0000229 return (EFI_SUCCESS);
230 }
231 return (EFI_NOT_FOUND);
232}
jcarseyd2b45642009-05-11 18:02:16 +0000233/**
234 Constructor for the Shell library.
235
236 Initialize the library and determine if the underlying is a UEFI Shell 2.0 or an EFI shell.
237
238 @param ImageHandle the image handle of the process
239 @param SystemTable the EFI System Table pointer
240
241 @retval EFI_SUCCESS the initialization was complete sucessfully
242 @return others an error ocurred during initialization
243**/
244EFI_STATUS
245EFIAPI
246ShellLibConstructor (
247 IN EFI_HANDLE ImageHandle,
248 IN EFI_SYSTEM_TABLE *SystemTable
jcarsey2247dde2009-11-09 18:08:58 +0000249 ) {
jcarseyd2b45642009-05-11 18:02:16 +0000250
jcarseyd2b45642009-05-11 18:02:16 +0000251 mEfiShellEnvironment2 = NULL;
252 mEfiShellProtocol = NULL;
253 mEfiShellParametersProtocol = NULL;
254 mEfiShellInterface = NULL;
255 mEfiShellEnvironment2Handle = NULL;
jcarseyecd3d592009-12-07 18:05:00 +0000256 mPostReplaceFormat = NULL;
257 mPostReplaceFormat2 = NULL;
jcarseyd2b45642009-05-11 18:02:16 +0000258
jcarseyd2b45642009-05-11 18:02:16 +0000259 //
260 // verify that auto initialize is not set false
jcarsey1e6e84c2010-01-25 20:05:08 +0000261 //
jcarseyd2b45642009-05-11 18:02:16 +0000262 if (PcdGetBool(PcdShellLibAutoInitialize) == 0) {
263 return (EFI_SUCCESS);
264 }
jcarsey1e6e84c2010-01-25 20:05:08 +0000265
jcarseyd2b45642009-05-11 18:02:16 +0000266 return (ShellLibConstructorWorker(ImageHandle, SystemTable));
267}
jcarsey94b17fa2009-05-07 18:46:18 +0000268
269/**
270 Destructory for the library. free any resources.
271**/
272EFI_STATUS
273EFIAPI
274ShellLibDestructor (
275 IN EFI_HANDLE ImageHandle,
276 IN EFI_SYSTEM_TABLE *SystemTable
jcarsey2247dde2009-11-09 18:08:58 +0000277 ) {
jcarsey94b17fa2009-05-07 18:46:18 +0000278 if (mEfiShellEnvironment2 != NULL) {
279 gBS->CloseProtocol(mEfiShellEnvironment2Handle==NULL?ImageHandle:mEfiShellEnvironment2Handle,
280 &gEfiShellEnvironment2Guid,
281 ImageHandle,
282 NULL);
jcarseyd2b45642009-05-11 18:02:16 +0000283 mEfiShellEnvironment2 = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +0000284 }
285 if (mEfiShellInterface != NULL) {
286 gBS->CloseProtocol(ImageHandle,
287 &gEfiShellInterfaceGuid,
288 ImageHandle,
jcarsey1e6e84c2010-01-25 20:05:08 +0000289 NULL);
jcarseyd2b45642009-05-11 18:02:16 +0000290 mEfiShellInterface = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +0000291 }
292 if (mEfiShellProtocol != NULL) {
293 gBS->CloseProtocol(ImageHandle,
294 &gEfiShellProtocolGuid,
295 ImageHandle,
jcarsey1e6e84c2010-01-25 20:05:08 +0000296 NULL);
jcarseyd2b45642009-05-11 18:02:16 +0000297 mEfiShellProtocol = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +0000298 }
299 if (mEfiShellParametersProtocol != NULL) {
300 gBS->CloseProtocol(ImageHandle,
301 &gEfiShellParametersProtocolGuid,
302 ImageHandle,
jcarsey1e6e84c2010-01-25 20:05:08 +0000303 NULL);
jcarseyd2b45642009-05-11 18:02:16 +0000304 mEfiShellParametersProtocol = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +0000305 }
jcarseyd2b45642009-05-11 18:02:16 +0000306 mEfiShellEnvironment2Handle = NULL;
jcarseyecd3d592009-12-07 18:05:00 +0000307
308 if (mPostReplaceFormat != NULL) {
309 FreePool(mPostReplaceFormat);
310 }
311 if (mPostReplaceFormat2 != NULL) {
312 FreePool(mPostReplaceFormat2);
313 }
314 mPostReplaceFormat = NULL;
315 mPostReplaceFormat2 = NULL;
316
jcarsey94b17fa2009-05-07 18:46:18 +0000317 return (EFI_SUCCESS);
318}
jcarseyd2b45642009-05-11 18:02:16 +0000319
320/**
321 This function causes the shell library to initialize itself. If the shell library
322 is already initialized it will de-initialize all the current protocol poitners and
323 re-populate them again.
324
325 When the library is used with PcdShellLibAutoInitialize set to true this function
326 will return EFI_SUCCESS and perform no actions.
327
328 This function is intended for internal access for shell commands only.
329
330 @retval EFI_SUCCESS the initialization was complete sucessfully
331
332**/
333EFI_STATUS
334EFIAPI
335ShellInitialize (
336 ) {
337 //
338 // if auto initialize is not false then skip
339 //
340 if (PcdGetBool(PcdShellLibAutoInitialize) != 0) {
341 return (EFI_SUCCESS);
342 }
343
344 //
345 // deinit the current stuff
346 //
347 ASSERT_EFI_ERROR(ShellLibDestructor(gImageHandle, gST));
348
349 //
350 // init the new stuff
351 //
352 return (ShellLibConstructorWorker(gImageHandle, gST));
353}
354
jcarsey94b17fa2009-05-07 18:46:18 +0000355/**
jcarsey1e6e84c2010-01-25 20:05:08 +0000356 This function will retrieve the information about the file for the handle
jcarsey94b17fa2009-05-07 18:46:18 +0000357 specified and store it in allocated pool memory.
358
jcarsey1e6e84c2010-01-25 20:05:08 +0000359 This function allocates a buffer to store the file's information. It is the
qhuang869817bf2009-05-20 14:42:48 +0000360 caller's responsibility to free the buffer
jcarsey94b17fa2009-05-07 18:46:18 +0000361
jcarsey1e6e84c2010-01-25 20:05:08 +0000362 @param FileHandle The file handle of the file for which information is
jcarsey94b17fa2009-05-07 18:46:18 +0000363 being requested.
364
365 @retval NULL information could not be retrieved.
366
367 @return the information about the file
368**/
369EFI_FILE_INFO*
370EFIAPI
371ShellGetFileInfo (
372 IN EFI_FILE_HANDLE FileHandle
jcarsey2247dde2009-11-09 18:08:58 +0000373 ) {
jcarseyd2b45642009-05-11 18:02:16 +0000374 return (FileFunctionMap.GetFileInfo(FileHandle));
jcarsey94b17fa2009-05-07 18:46:18 +0000375}
376
377/**
jcarsey1e6e84c2010-01-25 20:05:08 +0000378 This function will set the information about the file for the opened handle
jcarsey94b17fa2009-05-07 18:46:18 +0000379 specified.
380
jcarsey1e6e84c2010-01-25 20:05:08 +0000381 @param FileHandle The file handle of the file for which information
jcarsey94b17fa2009-05-07 18:46:18 +0000382 is being set
383
384 @param FileInfo The infotmation to set.
385
386 @retval EFI_SUCCESS The information was set.
387 @retval EFI_UNSUPPORTED The InformationType is not known.
388 @retval EFI_NO_MEDIA The device has no medium.
389 @retval EFI_DEVICE_ERROR The device reported an error.
390 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
391 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
392 @retval EFI_ACCESS_DENIED The file was opened read only.
393 @retval EFI_VOLUME_FULL The volume is full.
394**/
395EFI_STATUS
396EFIAPI
397ShellSetFileInfo (
398 IN EFI_FILE_HANDLE FileHandle,
399 IN EFI_FILE_INFO *FileInfo
jcarsey2247dde2009-11-09 18:08:58 +0000400 ) {
jcarseyd2b45642009-05-11 18:02:16 +0000401 return (FileFunctionMap.SetFileInfo(FileHandle, FileInfo));
jcarsey1e6e84c2010-01-25 20:05:08 +0000402}
403
jcarsey94b17fa2009-05-07 18:46:18 +0000404 /**
405 This function will open a file or directory referenced by DevicePath.
406
jcarsey1e6e84c2010-01-25 20:05:08 +0000407 This function opens a file with the open mode according to the file path. The
jcarsey94b17fa2009-05-07 18:46:18 +0000408 Attributes is valid only for EFI_FILE_MODE_CREATE.
409
jcarsey1e6e84c2010-01-25 20:05:08 +0000410 @param FilePath on input the device path to the file. On output
jcarsey94b17fa2009-05-07 18:46:18 +0000411 the remaining device path.
412 @param DeviceHandle pointer to the system device handle.
413 @param FileHandle pointer to the file handle.
414 @param OpenMode the mode to open the file with.
415 @param Attributes the file's file attributes.
416
417 @retval EFI_SUCCESS The information was set.
418 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
jcarsey1e6e84c2010-01-25 20:05:08 +0000419 @retval EFI_UNSUPPORTED Could not open the file path.
420 @retval EFI_NOT_FOUND The specified file could not be found on the
421 device or the file system could not be found on
jcarsey94b17fa2009-05-07 18:46:18 +0000422 the device.
423 @retval EFI_NO_MEDIA The device has no medium.
jcarsey1e6e84c2010-01-25 20:05:08 +0000424 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
jcarsey94b17fa2009-05-07 18:46:18 +0000425 medium is no longer supported.
426 @retval EFI_DEVICE_ERROR The device reported an error.
427 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
428 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
429 @retval EFI_ACCESS_DENIED The file was opened read only.
jcarsey1e6e84c2010-01-25 20:05:08 +0000430 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
jcarsey94b17fa2009-05-07 18:46:18 +0000431 file.
432 @retval EFI_VOLUME_FULL The volume is full.
433**/
434EFI_STATUS
435EFIAPI
436ShellOpenFileByDevicePath(
437 IN OUT EFI_DEVICE_PATH_PROTOCOL **FilePath,
438 OUT EFI_HANDLE *DeviceHandle,
439 OUT EFI_FILE_HANDLE *FileHandle,
440 IN UINT64 OpenMode,
441 IN UINT64 Attributes
jcarsey2247dde2009-11-09 18:08:58 +0000442 ) {
jcarsey94b17fa2009-05-07 18:46:18 +0000443 CHAR16 *FileName;
444 EFI_STATUS Status;
445 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *EfiSimpleFileSystemProtocol;
446 EFI_FILE_HANDLE LastHandle;
447
448 //
449 // ASERT for FileHandle, FilePath, and DeviceHandle being NULL
450 //
451 ASSERT(FilePath != NULL);
452 ASSERT(FileHandle != NULL);
453 ASSERT(DeviceHandle != NULL);
jcarsey1e6e84c2010-01-25 20:05:08 +0000454 //
jcarsey94b17fa2009-05-07 18:46:18 +0000455 // which shell interface should we use
456 //
457 if (mEfiShellProtocol != NULL) {
458 //
459 // use UEFI Shell 2.0 method.
460 //
461 FileName = mEfiShellProtocol->GetFilePathFromDevicePath(*FilePath);
462 if (FileName == NULL) {
463 return (EFI_INVALID_PARAMETER);
464 }
465 Status = ShellOpenFileByName(FileName, FileHandle, OpenMode, Attributes);
466 FreePool(FileName);
467 return (Status);
jcarsey1e6e84c2010-01-25 20:05:08 +0000468 }
jcarseyd2b45642009-05-11 18:02:16 +0000469
470
471 //
472 // use old shell method.
473 //
jcarsey1e6e84c2010-01-25 20:05:08 +0000474 Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid,
475 FilePath,
jcarseyd2b45642009-05-11 18:02:16 +0000476 DeviceHandle);
477 if (EFI_ERROR (Status)) {
478 return Status;
479 }
480 Status = gBS->OpenProtocol(*DeviceHandle,
481 &gEfiSimpleFileSystemProtocolGuid,
jcarseyb1f95a02009-06-16 00:23:19 +0000482 (VOID**)&EfiSimpleFileSystemProtocol,
jcarseyd2b45642009-05-11 18:02:16 +0000483 gImageHandle,
484 NULL,
485 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
486 if (EFI_ERROR (Status)) {
487 return Status;
488 }
489 Status = EfiSimpleFileSystemProtocol->OpenVolume(EfiSimpleFileSystemProtocol, FileHandle);
490 if (EFI_ERROR (Status)) {
491 FileHandle = NULL;
492 return Status;
493 }
494
495 //
496 // go down directories one node at a time.
497 //
498 while (!IsDevicePathEnd (*FilePath)) {
jcarsey94b17fa2009-05-07 18:46:18 +0000499 //
jcarseyd2b45642009-05-11 18:02:16 +0000500 // For file system access each node should be a file path component
jcarsey94b17fa2009-05-07 18:46:18 +0000501 //
jcarseyd2b45642009-05-11 18:02:16 +0000502 if (DevicePathType (*FilePath) != MEDIA_DEVICE_PATH ||
503 DevicePathSubType (*FilePath) != MEDIA_FILEPATH_DP
504 ) {
jcarsey94b17fa2009-05-07 18:46:18 +0000505 FileHandle = NULL;
jcarseyd2b45642009-05-11 18:02:16 +0000506 return (EFI_INVALID_PARAMETER);
jcarsey94b17fa2009-05-07 18:46:18 +0000507 }
jcarseyd2b45642009-05-11 18:02:16 +0000508 //
509 // Open this file path node
510 //
511 LastHandle = *FileHandle;
512 *FileHandle = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +0000513
514 //
jcarseyd2b45642009-05-11 18:02:16 +0000515 // Try to test opening an existing file
jcarsey94b17fa2009-05-07 18:46:18 +0000516 //
jcarseyd2b45642009-05-11 18:02:16 +0000517 Status = LastHandle->Open (
518 LastHandle,
519 FileHandle,
520 ((FILEPATH_DEVICE_PATH*)*FilePath)->PathName,
521 OpenMode &~EFI_FILE_MODE_CREATE,
522 0
523 );
jcarsey94b17fa2009-05-07 18:46:18 +0000524
jcarseyd2b45642009-05-11 18:02:16 +0000525 //
526 // see if the error was that it needs to be created
527 //
528 if ((EFI_ERROR (Status)) && (OpenMode != (OpenMode &~EFI_FILE_MODE_CREATE))) {
jcarsey94b17fa2009-05-07 18:46:18 +0000529 Status = LastHandle->Open (
530 LastHandle,
531 FileHandle,
532 ((FILEPATH_DEVICE_PATH*)*FilePath)->PathName,
jcarseyd2b45642009-05-11 18:02:16 +0000533 OpenMode,
534 Attributes
jcarsey94b17fa2009-05-07 18:46:18 +0000535 );
jcarsey94b17fa2009-05-07 18:46:18 +0000536 }
jcarseyd2b45642009-05-11 18:02:16 +0000537 //
538 // Close the last node
539 //
540 LastHandle->Close (LastHandle);
541
542 if (EFI_ERROR(Status)) {
543 return (Status);
544 }
545
546 //
547 // Get the next node
548 //
549 *FilePath = NextDevicePathNode (*FilePath);
jcarsey94b17fa2009-05-07 18:46:18 +0000550 }
jcarseyd2b45642009-05-11 18:02:16 +0000551 return (EFI_SUCCESS);
jcarsey94b17fa2009-05-07 18:46:18 +0000552}
553
554/**
555 This function will open a file or directory referenced by filename.
556
jcarsey1e6e84c2010-01-25 20:05:08 +0000557 If return is EFI_SUCCESS, the Filehandle is the opened file's handle;
558 otherwise, the Filehandle is NULL. The Attributes is valid only for
jcarsey94b17fa2009-05-07 18:46:18 +0000559 EFI_FILE_MODE_CREATE.
560
561 if FileNAme is NULL then ASSERT()
562
563 @param FileName pointer to file name
564 @param FileHandle pointer to the file handle.
565 @param OpenMode the mode to open the file with.
566 @param Attributes the file's file attributes.
567
568 @retval EFI_SUCCESS The information was set.
569 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
jcarsey1e6e84c2010-01-25 20:05:08 +0000570 @retval EFI_UNSUPPORTED Could not open the file path.
571 @retval EFI_NOT_FOUND The specified file could not be found on the
572 device or the file system could not be found
jcarsey94b17fa2009-05-07 18:46:18 +0000573 on the device.
574 @retval EFI_NO_MEDIA The device has no medium.
jcarsey1e6e84c2010-01-25 20:05:08 +0000575 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
jcarsey94b17fa2009-05-07 18:46:18 +0000576 medium is no longer supported.
577 @retval EFI_DEVICE_ERROR The device reported an error.
578 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
579 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
580 @retval EFI_ACCESS_DENIED The file was opened read only.
jcarsey1e6e84c2010-01-25 20:05:08 +0000581 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
jcarsey94b17fa2009-05-07 18:46:18 +0000582 file.
583 @retval EFI_VOLUME_FULL The volume is full.
584**/
585EFI_STATUS
586EFIAPI
587ShellOpenFileByName(
jcarseyb82bfcc2009-06-29 16:28:23 +0000588 IN CONST CHAR16 *FileName,
jcarsey94b17fa2009-05-07 18:46:18 +0000589 OUT EFI_FILE_HANDLE *FileHandle,
590 IN UINT64 OpenMode,
591 IN UINT64 Attributes
jcarsey2247dde2009-11-09 18:08:58 +0000592 ) {
jcarsey94b17fa2009-05-07 18:46:18 +0000593 EFI_HANDLE DeviceHandle;
594 EFI_DEVICE_PATH_PROTOCOL *FilePath;
jcarseyb1f95a02009-06-16 00:23:19 +0000595 EFI_STATUS Status;
596 EFI_FILE_INFO *FileInfo;
jcarsey94b17fa2009-05-07 18:46:18 +0000597
598 //
599 // ASSERT if FileName is NULL
600 //
601 ASSERT(FileName != NULL);
602
603 if (mEfiShellProtocol != NULL) {
604 //
605 // Use UEFI Shell 2.0 method
606 //
jcarseyb1f95a02009-06-16 00:23:19 +0000607 Status = mEfiShellProtocol->OpenFileByName(FileName,
608 FileHandle,
609 OpenMode);
jcarsey2247dde2009-11-09 18:08:58 +0000610 if (!EFI_ERROR(Status) && ((OpenMode & EFI_FILE_MODE_CREATE) != 0)){
611 FileInfo = FileFunctionMap.GetFileInfo(*FileHandle);
jcarseyb1f95a02009-06-16 00:23:19 +0000612 ASSERT(FileInfo != NULL);
613 FileInfo->Attribute = Attributes;
jcarsey2247dde2009-11-09 18:08:58 +0000614 Status = FileFunctionMap.SetFileInfo(*FileHandle, FileInfo);
615 FreePool(FileInfo);
jcarseyb1f95a02009-06-16 00:23:19 +0000616 }
617 return (Status);
jcarsey1e6e84c2010-01-25 20:05:08 +0000618 }
jcarsey94b17fa2009-05-07 18:46:18 +0000619 //
620 // Using EFI Shell version
621 // this means convert name to path and call that function
622 // since this will use EFI method again that will open it.
623 //
624 ASSERT(mEfiShellEnvironment2 != NULL);
jcarseyb82bfcc2009-06-29 16:28:23 +0000625 FilePath = mEfiShellEnvironment2->NameToPath ((CHAR16*)FileName);
jcarsey94b17fa2009-05-07 18:46:18 +0000626 if (FileDevicePath != NULL) {
627 return (ShellOpenFileByDevicePath(&FilePath,
628 &DeviceHandle,
629 FileHandle,
630 OpenMode,
631 Attributes ));
632 }
633 return (EFI_DEVICE_ERROR);
634}
635/**
636 This function create a directory
637
jcarsey1e6e84c2010-01-25 20:05:08 +0000638 If return is EFI_SUCCESS, the Filehandle is the opened directory's handle;
639 otherwise, the Filehandle is NULL. If the directory already existed, this
jcarsey94b17fa2009-05-07 18:46:18 +0000640 function opens the existing directory.
641
642 @param DirectoryName pointer to directory name
643 @param FileHandle pointer to the file handle.
644
645 @retval EFI_SUCCESS The information was set.
646 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
jcarsey1e6e84c2010-01-25 20:05:08 +0000647 @retval EFI_UNSUPPORTED Could not open the file path.
648 @retval EFI_NOT_FOUND The specified file could not be found on the
649 device or the file system could not be found
jcarsey94b17fa2009-05-07 18:46:18 +0000650 on the device.
651 @retval EFI_NO_MEDIA The device has no medium.
jcarsey1e6e84c2010-01-25 20:05:08 +0000652 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
jcarsey94b17fa2009-05-07 18:46:18 +0000653 medium is no longer supported.
654 @retval EFI_DEVICE_ERROR The device reported an error.
655 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
656 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
657 @retval EFI_ACCESS_DENIED The file was opened read only.
jcarsey1e6e84c2010-01-25 20:05:08 +0000658 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
jcarsey94b17fa2009-05-07 18:46:18 +0000659 file.
660 @retval EFI_VOLUME_FULL The volume is full.
661 @sa ShellOpenFileByName
662**/
663EFI_STATUS
664EFIAPI
665ShellCreateDirectory(
jcarseyb82bfcc2009-06-29 16:28:23 +0000666 IN CONST CHAR16 *DirectoryName,
jcarsey94b17fa2009-05-07 18:46:18 +0000667 OUT EFI_FILE_HANDLE *FileHandle
jcarsey2247dde2009-11-09 18:08:58 +0000668 ) {
669 if (mEfiShellProtocol != NULL) {
670 //
671 // Use UEFI Shell 2.0 method
672 //
673 return (mEfiShellProtocol->CreateFile(DirectoryName,
674 EFI_FILE_DIRECTORY,
675 FileHandle
676 ));
677 } else {
678 return (ShellOpenFileByName(DirectoryName,
679 FileHandle,
680 EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE,
681 EFI_FILE_DIRECTORY
682 ));
683 }
jcarsey94b17fa2009-05-07 18:46:18 +0000684}
685
686/**
687 This function reads information from an opened file.
688
jcarsey1e6e84c2010-01-25 20:05:08 +0000689 If FileHandle is not a directory, the function reads the requested number of
690 bytes from the file at the file's current position and returns them in Buffer.
jcarsey94b17fa2009-05-07 18:46:18 +0000691 If the read goes beyond the end of the file, the read length is truncated to the
jcarsey1e6e84c2010-01-25 20:05:08 +0000692 end of the file. The file's current position is increased by the number of bytes
693 returned. If FileHandle is a directory, the function reads the directory entry
694 at the file's current position and returns the entry in Buffer. If the Buffer
695 is not large enough to hold the current directory entry, then
696 EFI_BUFFER_TOO_SMALL is returned and the current file position is not updated.
697 BufferSize is set to be the size of the buffer needed to read the entry. On
698 success, the current position is updated to the next directory entry. If there
699 are no more directory entries, the read returns a zero-length buffer.
jcarsey94b17fa2009-05-07 18:46:18 +0000700 EFI_FILE_INFO is the structure returned as the directory entry.
701
702 @param FileHandle the opened file handle
jcarsey1e6e84c2010-01-25 20:05:08 +0000703 @param BufferSize on input the size of buffer in bytes. on return
jcarsey94b17fa2009-05-07 18:46:18 +0000704 the number of bytes written.
705 @param Buffer the buffer to put read data into.
706
707 @retval EFI_SUCCESS Data was read.
708 @retval EFI_NO_MEDIA The device has no media.
709 @retval EFI_DEVICE_ERROR The device reported an error.
710 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
jcarsey1e6e84c2010-01-25 20:05:08 +0000711 @retval EFI_BUFFER_TO_SMALL Buffer is too small. ReadSize contains required
jcarsey94b17fa2009-05-07 18:46:18 +0000712 size.
713
714**/
715EFI_STATUS
716EFIAPI
717ShellReadFile(
718 IN EFI_FILE_HANDLE FileHandle,
719 IN OUT UINTN *BufferSize,
720 OUT VOID *Buffer
jcarsey2247dde2009-11-09 18:08:58 +0000721 ) {
jcarseyd2b45642009-05-11 18:02:16 +0000722 return (FileFunctionMap.ReadFile(FileHandle, BufferSize, Buffer));
jcarsey94b17fa2009-05-07 18:46:18 +0000723}
724
725
726/**
727 Write data to a file.
728
jcarsey1e6e84c2010-01-25 20:05:08 +0000729 This function writes the specified number of bytes to the file at the current
730 file position. The current file position is advanced the actual number of bytes
731 written, which is returned in BufferSize. Partial writes only occur when there
732 has been a data error during the write attempt (such as "volume space full").
733 The file is automatically grown to hold the data if required. Direct writes to
jcarsey94b17fa2009-05-07 18:46:18 +0000734 opened directories are not supported.
735
736 @param FileHandle The opened file for writing
737 @param BufferSize on input the number of bytes in Buffer. On output
738 the number of bytes written.
739 @param Buffer the buffer containing data to write is stored.
740
741 @retval EFI_SUCCESS Data was written.
742 @retval EFI_UNSUPPORTED Writes to an open directory are not supported.
743 @retval EFI_NO_MEDIA The device has no media.
744 @retval EFI_DEVICE_ERROR The device reported an error.
745 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
746 @retval EFI_WRITE_PROTECTED The device is write-protected.
747 @retval EFI_ACCESS_DENIED The file was open for read only.
748 @retval EFI_VOLUME_FULL The volume is full.
749**/
750EFI_STATUS
751EFIAPI
752ShellWriteFile(
753 IN EFI_FILE_HANDLE FileHandle,
754 IN OUT UINTN *BufferSize,
755 IN VOID *Buffer
jcarsey2247dde2009-11-09 18:08:58 +0000756 ) {
jcarseyd2b45642009-05-11 18:02:16 +0000757 return (FileFunctionMap.WriteFile(FileHandle, BufferSize, Buffer));
jcarsey94b17fa2009-05-07 18:46:18 +0000758}
759
jcarsey1e6e84c2010-01-25 20:05:08 +0000760/**
jcarsey94b17fa2009-05-07 18:46:18 +0000761 Close an open file handle.
762
jcarsey1e6e84c2010-01-25 20:05:08 +0000763 This function closes a specified file handle. All "dirty" cached file data is
764 flushed to the device, and the file is closed. In all cases the handle is
jcarsey94b17fa2009-05-07 18:46:18 +0000765 closed.
766
767@param FileHandle the file handle to close.
768
769@retval EFI_SUCCESS the file handle was closed sucessfully.
770**/
771EFI_STATUS
772EFIAPI
773ShellCloseFile (
774 IN EFI_FILE_HANDLE *FileHandle
jcarsey2247dde2009-11-09 18:08:58 +0000775 ) {
jcarseyd2b45642009-05-11 18:02:16 +0000776 return (FileFunctionMap.CloseFile(*FileHandle));
jcarsey94b17fa2009-05-07 18:46:18 +0000777}
778
779/**
780 Delete a file and close the handle
781
782 This function closes and deletes a file. In all cases the file handle is closed.
jcarsey1e6e84c2010-01-25 20:05:08 +0000783 If the file cannot be deleted, the warning code EFI_WARN_DELETE_FAILURE is
jcarsey94b17fa2009-05-07 18:46:18 +0000784 returned, but the handle is still closed.
785
786 @param FileHandle the file handle to delete
787
788 @retval EFI_SUCCESS the file was closed sucessfully
jcarsey1e6e84c2010-01-25 20:05:08 +0000789 @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not
jcarsey94b17fa2009-05-07 18:46:18 +0000790 deleted
791 @retval INVALID_PARAMETER One of the parameters has an invalid value.
792**/
793EFI_STATUS
794EFIAPI
795ShellDeleteFile (
796 IN EFI_FILE_HANDLE *FileHandle
jcarsey2247dde2009-11-09 18:08:58 +0000797 ) {
jcarseyd2b45642009-05-11 18:02:16 +0000798 return (FileFunctionMap.DeleteFile(*FileHandle));
jcarsey94b17fa2009-05-07 18:46:18 +0000799}
800
801/**
802 Set the current position in a file.
803
jcarsey1e6e84c2010-01-25 20:05:08 +0000804 This function sets the current file position for the handle to the position
jcarsey94b17fa2009-05-07 18:46:18 +0000805 supplied. With the exception of seeking to position 0xFFFFFFFFFFFFFFFF, only
jcarsey1e6e84c2010-01-25 20:05:08 +0000806 absolute positioning is supported, and seeking past the end of the file is
807 allowed (a subsequent write would grow the file). Seeking to position
jcarsey94b17fa2009-05-07 18:46:18 +0000808 0xFFFFFFFFFFFFFFFF causes the current position to be set to the end of the file.
jcarsey1e6e84c2010-01-25 20:05:08 +0000809 If FileHandle is a directory, the only position that may be set is zero. This
jcarsey94b17fa2009-05-07 18:46:18 +0000810 has the effect of starting the read process of the directory entries over.
811
812 @param FileHandle The file handle on which the position is being set
813 @param Position Byte position from begining of file
814
815 @retval EFI_SUCCESS Operation completed sucessfully.
jcarsey1e6e84c2010-01-25 20:05:08 +0000816 @retval EFI_UNSUPPORTED the seek request for non-zero is not valid on
jcarsey94b17fa2009-05-07 18:46:18 +0000817 directories.
818 @retval INVALID_PARAMETER One of the parameters has an invalid value.
819**/
820EFI_STATUS
821EFIAPI
822ShellSetFilePosition (
823 IN EFI_FILE_HANDLE FileHandle,
824 IN UINT64 Position
jcarsey2247dde2009-11-09 18:08:58 +0000825 ) {
jcarseyd2b45642009-05-11 18:02:16 +0000826 return (FileFunctionMap.SetFilePosition(FileHandle, Position));
jcarsey94b17fa2009-05-07 18:46:18 +0000827}
828
jcarsey1e6e84c2010-01-25 20:05:08 +0000829/**
jcarsey94b17fa2009-05-07 18:46:18 +0000830 Gets a file's current position
831
jcarsey1e6e84c2010-01-25 20:05:08 +0000832 This function retrieves the current file position for the file handle. For
833 directories, the current file position has no meaning outside of the file
jcarsey94b17fa2009-05-07 18:46:18 +0000834 system driver and as such the operation is not supported. An error is returned
835 if FileHandle is a directory.
836
837 @param FileHandle The open file handle on which to get the position.
838 @param Position Byte position from begining of file.
839
840 @retval EFI_SUCCESS the operation completed sucessfully.
841 @retval INVALID_PARAMETER One of the parameters has an invalid value.
842 @retval EFI_UNSUPPORTED the request is not valid on directories.
843**/
844EFI_STATUS
845EFIAPI
846ShellGetFilePosition (
847 IN EFI_FILE_HANDLE FileHandle,
848 OUT UINT64 *Position
jcarsey2247dde2009-11-09 18:08:58 +0000849 ) {
jcarseyd2b45642009-05-11 18:02:16 +0000850 return (FileFunctionMap.GetFilePosition(FileHandle, Position));
jcarsey94b17fa2009-05-07 18:46:18 +0000851}
852/**
853 Flushes data on a file
jcarsey1e6e84c2010-01-25 20:05:08 +0000854
jcarsey94b17fa2009-05-07 18:46:18 +0000855 This function flushes all modified data associated with a file to a device.
856
857 @param FileHandle The file handle on which to flush data
858
859 @retval EFI_SUCCESS The data was flushed.
860 @retval EFI_NO_MEDIA The device has no media.
861 @retval EFI_DEVICE_ERROR The device reported an error.
862 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
863 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
864 @retval EFI_ACCESS_DENIED The file was opened for read only.
865**/
866EFI_STATUS
867EFIAPI
868ShellFlushFile (
869 IN EFI_FILE_HANDLE FileHandle
jcarsey2247dde2009-11-09 18:08:58 +0000870 ) {
jcarseyd2b45642009-05-11 18:02:16 +0000871 return (FileFunctionMap.FlushFile(FileHandle));
jcarsey94b17fa2009-05-07 18:46:18 +0000872}
873
874/**
875 Retrieves the first file from a directory
876
jcarsey1e6e84c2010-01-25 20:05:08 +0000877 This function opens a directory and gets the first file's info in the
878 directory. Caller can use ShellFindNextFile() to get other files. When
jcarsey94b17fa2009-05-07 18:46:18 +0000879 complete the caller is responsible for calling FreePool() on Buffer.
880
881 @param DirHandle The file handle of the directory to search
882 @param Buffer Pointer to buffer for file's information
883
884 @retval EFI_SUCCESS Found the first file.
885 @retval EFI_NOT_FOUND Cannot find the directory.
886 @retval EFI_NO_MEDIA The device has no media.
887 @retval EFI_DEVICE_ERROR The device reported an error.
888 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
889 @return Others status of ShellGetFileInfo, ShellSetFilePosition,
890 or ShellReadFile
891**/
892EFI_STATUS
893EFIAPI
894ShellFindFirstFile (
895 IN EFI_FILE_HANDLE DirHandle,
jcarseyd2b45642009-05-11 18:02:16 +0000896 OUT EFI_FILE_INFO **Buffer
jcarsey2247dde2009-11-09 18:08:58 +0000897 ) {
jcarsey94b17fa2009-05-07 18:46:18 +0000898 //
jcarseyd2b45642009-05-11 18:02:16 +0000899 // pass to file handle lib
jcarsey94b17fa2009-05-07 18:46:18 +0000900 //
jcarseyd2b45642009-05-11 18:02:16 +0000901 return (FileHandleFindFirstFile(DirHandle, Buffer));
jcarsey94b17fa2009-05-07 18:46:18 +0000902}
903/**
904 Retrieves the next file in a directory.
905
jcarsey1e6e84c2010-01-25 20:05:08 +0000906 To use this function, caller must call the LibFindFirstFile() to get the
907 first file, and then use this function get other files. This function can be
908 called for several times to get each file's information in the directory. If
909 the call of ShellFindNextFile() got the last file in the directory, the next
910 call of this function has no file to get. *NoFile will be set to TRUE and the
911 Buffer memory will be automatically freed.
jcarsey94b17fa2009-05-07 18:46:18 +0000912
913 @param DirHandle the file handle of the directory
914 @param Buffer pointer to buffer for file's information
915 @param NoFile pointer to boolean when last file is found
916
917 @retval EFI_SUCCESS Found the next file, or reached last file
918 @retval EFI_NO_MEDIA The device has no media.
919 @retval EFI_DEVICE_ERROR The device reported an error.
920 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
921**/
922EFI_STATUS
923EFIAPI
924ShellFindNextFile(
925 IN EFI_FILE_HANDLE DirHandle,
926 OUT EFI_FILE_INFO *Buffer,
927 OUT BOOLEAN *NoFile
jcarsey2247dde2009-11-09 18:08:58 +0000928 ) {
jcarsey94b17fa2009-05-07 18:46:18 +0000929 //
jcarseyd2b45642009-05-11 18:02:16 +0000930 // pass to file handle lib
jcarsey94b17fa2009-05-07 18:46:18 +0000931 //
jcarseyd2b45642009-05-11 18:02:16 +0000932 return (FileHandleFindNextFile(DirHandle, Buffer, NoFile));
jcarsey94b17fa2009-05-07 18:46:18 +0000933}
934/**
935 Retrieve the size of a file.
936
937 if FileHandle is NULL then ASSERT()
938 if Size is NULL then ASSERT()
939
jcarsey1e6e84c2010-01-25 20:05:08 +0000940 This function extracts the file size info from the FileHandle's EFI_FILE_INFO
jcarsey94b17fa2009-05-07 18:46:18 +0000941 data.
942
943 @param FileHandle file handle from which size is retrieved
944 @param Size pointer to size
945
946 @retval EFI_SUCCESS operation was completed sucessfully
947 @retval EFI_DEVICE_ERROR cannot access the file
948**/
949EFI_STATUS
950EFIAPI
951ShellGetFileSize (
952 IN EFI_FILE_HANDLE FileHandle,
953 OUT UINT64 *Size
jcarsey2247dde2009-11-09 18:08:58 +0000954 ) {
jcarseyd2b45642009-05-11 18:02:16 +0000955 return (FileFunctionMap.GetFileSize(FileHandle, Size));
jcarsey94b17fa2009-05-07 18:46:18 +0000956}
957/**
958 Retrieves the status of the break execution flag
959
960 this function is useful to check whether the application is being asked to halt by the shell.
961
962 @retval TRUE the execution break is enabled
963 @retval FALSE the execution break is not enabled
964**/
965BOOLEAN
966EFIAPI
967ShellGetExecutionBreakFlag(
968 VOID
969 )
970{
jcarsey1e6e84c2010-01-25 20:05:08 +0000971 //
jcarsey94b17fa2009-05-07 18:46:18 +0000972 // Check for UEFI Shell 2.0 protocols
973 //
974 if (mEfiShellProtocol != NULL) {
975
976 //
977 // We are using UEFI Shell 2.0; see if the event has been triggered
978 //
979 if (gBS->CheckEvent(mEfiShellProtocol->ExecutionBreak) != EFI_SUCCESS) {
980 return (FALSE);
981 }
982 return (TRUE);
jcarsey1e6e84c2010-01-25 20:05:08 +0000983 }
jcarsey94b17fa2009-05-07 18:46:18 +0000984
985 //
986 // using EFI Shell; call the function to check
987 //
988 ASSERT(mEfiShellEnvironment2 != NULL);
989 return (mEfiShellEnvironment2->GetExecutionBreak());
990}
991/**
992 return the value of an environment variable
993
jcarsey1e6e84c2010-01-25 20:05:08 +0000994 this function gets the value of the environment variable set by the
jcarsey94b17fa2009-05-07 18:46:18 +0000995 ShellSetEnvironmentVariable function
996
997 @param EnvKey The key name of the environment variable.
998
999 @retval NULL the named environment variable does not exist.
1000 @return != NULL pointer to the value of the environment variable
1001**/
1002CONST CHAR16*
1003EFIAPI
1004ShellGetEnvironmentVariable (
jcarsey9b3bf082009-06-23 21:15:07 +00001005 IN CONST CHAR16 *EnvKey
jcarsey94b17fa2009-05-07 18:46:18 +00001006 )
1007{
jcarsey1e6e84c2010-01-25 20:05:08 +00001008 //
jcarsey94b17fa2009-05-07 18:46:18 +00001009 // Check for UEFI Shell 2.0 protocols
1010 //
1011 if (mEfiShellProtocol != NULL) {
1012 return (mEfiShellProtocol->GetEnv(EnvKey));
1013 }
1014
1015 //
1016 // ASSERT that we must have EFI shell
1017 //
1018 ASSERT(mEfiShellEnvironment2 != NULL);
1019
1020 //
1021 // using EFI Shell
1022 //
jcarsey9b3bf082009-06-23 21:15:07 +00001023 return (mEfiShellEnvironment2->GetEnv((CHAR16*)EnvKey));
jcarsey94b17fa2009-05-07 18:46:18 +00001024}
1025/**
1026 set the value of an environment variable
1027
1028This function changes the current value of the specified environment variable. If the
1029environment variable exists and the Value is an empty string, then the environment
1030variable is deleted. If the environment variable exists and the Value is not an empty
1031string, then the value of the environment variable is changed. If the environment
1032variable does not exist and the Value is an empty string, there is no action. If the
1033environment variable does not exist and the Value is a non-empty string, then the
1034environment variable is created and assigned the specified value.
1035
1036 This is not supported pre-UEFI Shell 2.0.
1037
1038 @param EnvKey The key name of the environment variable.
1039 @param EnvVal The Value of the environment variable
1040 @param Volatile Indicates whether the variable is non-volatile (FALSE) or volatile (TRUE).
1041
1042 @retval EFI_SUCCESS the operation was completed sucessfully
1043 @retval EFI_UNSUPPORTED This operation is not allowed in pre UEFI 2.0 Shell environments
1044**/
1045EFI_STATUS
1046EFIAPI
1047ShellSetEnvironmentVariable (
1048 IN CONST CHAR16 *EnvKey,
1049 IN CONST CHAR16 *EnvVal,
1050 IN BOOLEAN Volatile
1051 )
1052{
jcarsey1e6e84c2010-01-25 20:05:08 +00001053 //
jcarsey94b17fa2009-05-07 18:46:18 +00001054 // Check for UEFI Shell 2.0 protocols
1055 //
1056 if (mEfiShellProtocol != NULL) {
1057 return (mEfiShellProtocol->SetEnv(EnvKey, EnvVal, Volatile));
jcarsey1e6e84c2010-01-25 20:05:08 +00001058 }
jcarsey94b17fa2009-05-07 18:46:18 +00001059
1060 //
1061 // This feature does not exist under EFI shell
1062 //
1063 return (EFI_UNSUPPORTED);
1064}
1065/**
1066 cause the shell to parse and execute a command line.
1067
1068 This function creates a nested instance of the shell and executes the specified
1069command (CommandLine) with the specified environment (Environment). Upon return,
1070the status code returned by the specified command is placed in StatusCode.
1071If Environment is NULL, then the current environment is used and all changes made
1072by the commands executed will be reflected in the current environment. If the
1073Environment is non-NULL, then the changes made will be discarded.
1074The CommandLine is executed from the current working directory on the current
1075device.
1076
1077EnvironmentVariables and Status are only supported for UEFI Shell 2.0.
1078Output is only supported for pre-UEFI Shell 2.0
1079
1080 @param ImageHandle Parent image that is starting the operation
jcarsey1e6e84c2010-01-25 20:05:08 +00001081 @param CommandLine pointer to NULL terminated command line.
jcarsey94b17fa2009-05-07 18:46:18 +00001082 @param Output true to display debug output. false to hide it.
1083 @param EnvironmentVariables optional pointer to array of environment variables
1084 in the form "x=y". if NULL current set is used.
1085 @param Status the status of the run command line.
1086
1087 @retval EFI_SUCCESS the operation completed sucessfully. Status
1088 contains the status code returned.
1089 @retval EFI_INVALID_PARAMETER a parameter contains an invalid value
1090 @retval EFI_OUT_OF_RESOURCES out of resources
1091 @retval EFI_UNSUPPORTED the operation is not allowed.
1092**/
1093EFI_STATUS
1094EFIAPI
1095ShellExecute (
1096 IN EFI_HANDLE *ParentHandle,
1097 IN CHAR16 *CommandLine OPTIONAL,
1098 IN BOOLEAN Output OPTIONAL,
1099 IN CHAR16 **EnvironmentVariables OPTIONAL,
1100 OUT EFI_STATUS *Status OPTIONAL
1101 )
1102{
jcarsey1e6e84c2010-01-25 20:05:08 +00001103 //
jcarsey94b17fa2009-05-07 18:46:18 +00001104 // Check for UEFI Shell 2.0 protocols
1105 //
1106 if (mEfiShellProtocol != NULL) {
1107 //
1108 // Call UEFI Shell 2.0 version (not using Output parameter)
1109 //
1110 return (mEfiShellProtocol->Execute(ParentHandle,
1111 CommandLine,
1112 EnvironmentVariables,
1113 Status));
jcarsey1e6e84c2010-01-25 20:05:08 +00001114 }
jcarsey94b17fa2009-05-07 18:46:18 +00001115 //
1116 // ASSERT that we must have EFI shell
1117 //
1118 ASSERT(mEfiShellEnvironment2 != NULL);
1119 //
1120 // Call EFI Shell version (not using EnvironmentVariables or Status parameters)
1121 // Due to oddity in the EFI shell we want to dereference the ParentHandle here
1122 //
jcarsey1e6e84c2010-01-25 20:05:08 +00001123 return (mEfiShellEnvironment2->Execute(*ParentHandle,
1124 CommandLine,
jcarsey94b17fa2009-05-07 18:46:18 +00001125 Output));
1126}
1127/**
1128 Retreives the current directory path
1129
jcarsey1e6e84c2010-01-25 20:05:08 +00001130 If the DeviceName is NULL, it returns the current device's current directory
1131 name. If the DeviceName is not NULL, it returns the current directory name
jcarsey94b17fa2009-05-07 18:46:18 +00001132 on specified drive.
1133
1134 @param DeviceName the name of the drive to get directory on
1135
1136 @retval NULL the directory does not exist
1137 @return != NULL the directory
1138**/
1139CONST CHAR16*
1140EFIAPI
1141ShellGetCurrentDir (
1142 IN CHAR16 *DeviceName OPTIONAL
1143 )
1144{
jcarsey1e6e84c2010-01-25 20:05:08 +00001145 //
jcarsey94b17fa2009-05-07 18:46:18 +00001146 // Check for UEFI Shell 2.0 protocols
1147 //
1148 if (mEfiShellProtocol != NULL) {
1149 return (mEfiShellProtocol->GetCurDir(DeviceName));
jcarsey1e6e84c2010-01-25 20:05:08 +00001150 }
jcarsey94b17fa2009-05-07 18:46:18 +00001151 //
1152 // ASSERT that we must have EFI shell
1153 //
1154 ASSERT(mEfiShellEnvironment2 != NULL);
1155 return (mEfiShellEnvironment2->CurDir(DeviceName));
1156}
1157/**
1158 sets (enabled or disabled) the page break mode
1159
jcarsey1e6e84c2010-01-25 20:05:08 +00001160 when page break mode is enabled the screen will stop scrolling
jcarsey94b17fa2009-05-07 18:46:18 +00001161 and wait for operator input before scrolling a subsequent screen.
1162
1163 @param CurrentState TRUE to enable and FALSE to disable
1164**/
jcarsey1e6e84c2010-01-25 20:05:08 +00001165VOID
jcarsey94b17fa2009-05-07 18:46:18 +00001166EFIAPI
1167ShellSetPageBreakMode (
1168 IN BOOLEAN CurrentState
1169 )
1170{
1171 //
1172 // check for enabling
1173 //
1174 if (CurrentState != 0x00) {
jcarsey1e6e84c2010-01-25 20:05:08 +00001175 //
jcarsey94b17fa2009-05-07 18:46:18 +00001176 // check for UEFI Shell 2.0
1177 //
1178 if (mEfiShellProtocol != NULL) {
1179 //
1180 // Enable with UEFI 2.0 Shell
1181 //
1182 mEfiShellProtocol->EnablePageBreak();
1183 return;
1184 } else {
jcarsey1e6e84c2010-01-25 20:05:08 +00001185 //
jcarsey94b17fa2009-05-07 18:46:18 +00001186 // ASSERT that must have EFI Shell
1187 //
1188 ASSERT(mEfiShellEnvironment2 != NULL);
1189 //
1190 // Enable with EFI Shell
1191 //
1192 mEfiShellEnvironment2->EnablePageBreak (DEFAULT_INIT_ROW, DEFAULT_AUTO_LF);
1193 return;
1194 }
1195 } else {
jcarsey1e6e84c2010-01-25 20:05:08 +00001196 //
jcarsey94b17fa2009-05-07 18:46:18 +00001197 // check for UEFI Shell 2.0
1198 //
1199 if (mEfiShellProtocol != NULL) {
1200 //
1201 // Disable with UEFI 2.0 Shell
1202 //
1203 mEfiShellProtocol->DisablePageBreak();
1204 return;
1205 } else {
jcarsey1e6e84c2010-01-25 20:05:08 +00001206 //
jcarsey94b17fa2009-05-07 18:46:18 +00001207 // ASSERT that must have EFI Shell
1208 //
1209 ASSERT(mEfiShellEnvironment2 != NULL);
1210 //
1211 // Disable with EFI Shell
1212 //
1213 mEfiShellEnvironment2->DisablePageBreak ();
1214 return;
1215 }
1216 }
1217}
1218
1219///
1220/// version of EFI_SHELL_FILE_INFO struct, except has no CONST pointers.
1221/// This allows for the struct to be populated.
1222///
1223typedef struct {
jcarseyd2b45642009-05-11 18:02:16 +00001224 LIST_ENTRY Link;
jcarsey94b17fa2009-05-07 18:46:18 +00001225 EFI_STATUS Status;
1226 CHAR16 *FullName;
1227 CHAR16 *FileName;
1228 EFI_FILE_HANDLE Handle;
1229 EFI_FILE_INFO *Info;
1230} EFI_SHELL_FILE_INFO_NO_CONST;
1231
1232/**
1233 Converts a EFI shell list of structures to the coresponding UEFI Shell 2.0 type of list.
1234
1235 if OldStyleFileList is NULL then ASSERT()
1236
jcarsey1e6e84c2010-01-25 20:05:08 +00001237 this function will convert a SHELL_FILE_ARG based list into a callee allocated
jcarsey94b17fa2009-05-07 18:46:18 +00001238 EFI_SHELL_FILE_INFO based list. it is up to the caller to free the memory via
1239 the ShellCloseFileMetaArg function.
1240
jcarsey9b3bf082009-06-23 21:15:07 +00001241 @param[in] FileList the EFI shell list type
jcarseyb82bfcc2009-06-29 16:28:23 +00001242 @param[in,out] ListHead the list to add to
jcarsey94b17fa2009-05-07 18:46:18 +00001243
1244 @retval the resultant head of the double linked new format list;
1245**/
1246LIST_ENTRY*
1247EFIAPI
1248InternalShellConvertFileListType (
jcarsey9b3bf082009-06-23 21:15:07 +00001249 IN LIST_ENTRY *FileList,
1250 IN OUT LIST_ENTRY *ListHead
jcarsey125c2cf2009-11-18 21:36:50 +00001251 )
1252{
jcarsey94b17fa2009-05-07 18:46:18 +00001253 SHELL_FILE_ARG *OldInfo;
jcarsey9b3bf082009-06-23 21:15:07 +00001254 LIST_ENTRY *Link;
jcarsey94b17fa2009-05-07 18:46:18 +00001255 EFI_SHELL_FILE_INFO_NO_CONST *NewInfo;
1256
1257 //
jcarsey9b3bf082009-06-23 21:15:07 +00001258 // ASSERTs
jcarsey94b17fa2009-05-07 18:46:18 +00001259 //
jcarsey9b3bf082009-06-23 21:15:07 +00001260 ASSERT(FileList != NULL);
1261 ASSERT(ListHead != NULL);
jcarsey94b17fa2009-05-07 18:46:18 +00001262
1263 //
1264 // enumerate through each member of the old list and copy
1265 //
jcarseyd2b45642009-05-11 18:02:16 +00001266 for (Link = FileList->ForwardLink; Link != FileList; Link = Link->ForwardLink) {
jcarsey94b17fa2009-05-07 18:46:18 +00001267 OldInfo = CR (Link, SHELL_FILE_ARG, Link, SHELL_FILE_ARG_SIGNATURE);
1268
1269 //
1270 // make sure the old list was valid
1271 //
jcarsey1e6e84c2010-01-25 20:05:08 +00001272 ASSERT(OldInfo != NULL);
jcarsey94b17fa2009-05-07 18:46:18 +00001273 ASSERT(OldInfo->Info != NULL);
1274 ASSERT(OldInfo->FullName != NULL);
1275 ASSERT(OldInfo->FileName != NULL);
1276
1277 //
1278 // allocate a new EFI_SHELL_FILE_INFO object
1279 //
1280 NewInfo = AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));
jcarseyc9d92df2010-02-03 15:37:54 +00001281 ASSERT(NewInfo != NULL);
1282 if (NewInfo == NULL) {
1283 break;
1284 }
jcarsey1e6e84c2010-01-25 20:05:08 +00001285
1286 //
jcarsey94b17fa2009-05-07 18:46:18 +00001287 // copy the simple items
1288 //
1289 NewInfo->Handle = OldInfo->Handle;
1290 NewInfo->Status = OldInfo->Status;
1291
jcarseyd2b45642009-05-11 18:02:16 +00001292 // old shell checks for 0 not NULL
1293 OldInfo->Handle = 0;
1294
jcarsey94b17fa2009-05-07 18:46:18 +00001295 //
1296 // allocate new space to copy strings and structure
1297 //
1298 NewInfo->FullName = AllocateZeroPool(StrSize(OldInfo->FullName));
1299 NewInfo->FileName = AllocateZeroPool(StrSize(OldInfo->FileName));
1300 NewInfo->Info = AllocateZeroPool((UINTN)OldInfo->Info->Size);
jcarsey1e6e84c2010-01-25 20:05:08 +00001301
jcarsey94b17fa2009-05-07 18:46:18 +00001302 //
1303 // make sure all the memory allocations were sucessful
1304 //
1305 ASSERT(NewInfo->FullName != NULL);
1306 ASSERT(NewInfo->FileName != NULL);
1307 ASSERT(NewInfo->Info != NULL);
1308
1309 //
1310 // Copt the strings and structure
1311 //
1312 StrCpy(NewInfo->FullName, OldInfo->FullName);
1313 StrCpy(NewInfo->FileName, OldInfo->FileName);
1314 gBS->CopyMem (NewInfo->Info, OldInfo->Info, (UINTN)OldInfo->Info->Size);
1315
1316 //
1317 // add that to the list
1318 //
jcarsey9b3bf082009-06-23 21:15:07 +00001319 InsertTailList(ListHead, &NewInfo->Link);
jcarsey94b17fa2009-05-07 18:46:18 +00001320 }
1321 return (ListHead);
1322}
1323/**
1324 Opens a group of files based on a path.
1325
jcarsey1e6e84c2010-01-25 20:05:08 +00001326 This function uses the Arg to open all the matching files. Each matched
1327 file has a SHELL_FILE_ARG structure to record the file information. These
1328 structures are placed on the list ListHead. Users can get the SHELL_FILE_ARG
jcarsey94b17fa2009-05-07 18:46:18 +00001329 structures from ListHead to access each file. This function supports wildcards
jcarsey1e6e84c2010-01-25 20:05:08 +00001330 and will process '?' and '*' as such. the list must be freed with a call to
jcarsey94b17fa2009-05-07 18:46:18 +00001331 ShellCloseFileMetaArg().
1332
jcarsey1e6e84c2010-01-25 20:05:08 +00001333 If you are NOT appending to an existing list *ListHead must be NULL. If
jcarsey5f7431d2009-07-10 18:06:01 +00001334 *ListHead is NULL then it must be callee freed.
jcarsey94b17fa2009-05-07 18:46:18 +00001335
1336 @param Arg pointer to path string
1337 @param OpenMode mode to open files with
1338 @param ListHead head of linked list of results
1339
jcarsey1e6e84c2010-01-25 20:05:08 +00001340 @retval EFI_SUCCESS the operation was sucessful and the list head
jcarsey94b17fa2009-05-07 18:46:18 +00001341 contains the list of opened files
1342 #retval EFI_UNSUPPORTED a previous ShellOpenFileMetaArg must be closed first.
1343 *ListHead is set to NULL.
1344 @return != EFI_SUCCESS the operation failed
1345
1346 @sa InternalShellConvertFileListType
1347**/
1348EFI_STATUS
1349EFIAPI
1350ShellOpenFileMetaArg (
1351 IN CHAR16 *Arg,
1352 IN UINT64 OpenMode,
1353 IN OUT EFI_SHELL_FILE_INFO **ListHead
1354 )
1355{
1356 EFI_STATUS Status;
jcarsey9b3bf082009-06-23 21:15:07 +00001357 LIST_ENTRY mOldStyleFileList;
jcarsey1e6e84c2010-01-25 20:05:08 +00001358
jcarsey94b17fa2009-05-07 18:46:18 +00001359 //
1360 // ASSERT that Arg and ListHead are not NULL
1361 //
1362 ASSERT(Arg != NULL);
1363 ASSERT(ListHead != NULL);
1364
jcarsey1e6e84c2010-01-25 20:05:08 +00001365 //
jcarsey94b17fa2009-05-07 18:46:18 +00001366 // Check for UEFI Shell 2.0 protocols
1367 //
1368 if (mEfiShellProtocol != NULL) {
jcarsey5f7431d2009-07-10 18:06:01 +00001369 if (*ListHead == NULL) {
1370 *ListHead = (EFI_SHELL_FILE_INFO*)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));
1371 if (*ListHead == NULL) {
1372 return (EFI_OUT_OF_RESOURCES);
1373 }
1374 InitializeListHead(&((*ListHead)->Link));
jcarsey1e6e84c2010-01-25 20:05:08 +00001375 }
1376 Status = mEfiShellProtocol->OpenFileList(Arg,
1377 OpenMode,
jcarsey2247dde2009-11-09 18:08:58 +00001378 ListHead);
1379 if (EFI_ERROR(Status)) {
1380 mEfiShellProtocol->RemoveDupInFileList(ListHead);
1381 } else {
1382 Status = mEfiShellProtocol->RemoveDupInFileList(ListHead);
1383 }
1384 return (Status);
jcarsey1e6e84c2010-01-25 20:05:08 +00001385 }
jcarsey94b17fa2009-05-07 18:46:18 +00001386
1387 //
1388 // ASSERT that we must have EFI shell
1389 //
1390 ASSERT(mEfiShellEnvironment2 != NULL);
1391
1392 //
jcarsey94b17fa2009-05-07 18:46:18 +00001393 // make sure the list head is initialized
1394 //
jcarsey9b3bf082009-06-23 21:15:07 +00001395 InitializeListHead(&mOldStyleFileList);
jcarsey94b17fa2009-05-07 18:46:18 +00001396
1397 //
1398 // Get the EFI Shell list of files
1399 //
jcarsey9b3bf082009-06-23 21:15:07 +00001400 Status = mEfiShellEnvironment2->FileMetaArg(Arg, &mOldStyleFileList);
jcarsey94b17fa2009-05-07 18:46:18 +00001401 if (EFI_ERROR(Status)) {
1402 *ListHead = NULL;
1403 return (Status);
1404 }
1405
jcarsey9b3bf082009-06-23 21:15:07 +00001406 if (*ListHead == NULL) {
1407 *ListHead = (EFI_SHELL_FILE_INFO *)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));
1408 if (*ListHead == NULL) {
1409 return (EFI_OUT_OF_RESOURCES);
1410 }
1411 }
1412
jcarsey94b17fa2009-05-07 18:46:18 +00001413 //
1414 // Convert that to equivalent of UEFI Shell 2.0 structure
1415 //
jcarsey9b3bf082009-06-23 21:15:07 +00001416 InternalShellConvertFileListType(&mOldStyleFileList, &(*ListHead)->Link);
jcarsey94b17fa2009-05-07 18:46:18 +00001417
1418 //
jcarseyd2b45642009-05-11 18:02:16 +00001419 // Free the EFI Shell version that was converted.
1420 //
jcarsey9b3bf082009-06-23 21:15:07 +00001421 mEfiShellEnvironment2->FreeFileList(&mOldStyleFileList);
jcarsey94b17fa2009-05-07 18:46:18 +00001422
1423 return (Status);
1424}
1425/**
1426 Free the linked list returned from ShellOpenFileMetaArg
1427
1428 if ListHead is NULL then ASSERT()
1429
1430 @param ListHead the pointer to free
1431
1432 @retval EFI_SUCCESS the operation was sucessful
1433**/
1434EFI_STATUS
1435EFIAPI
1436ShellCloseFileMetaArg (
1437 IN OUT EFI_SHELL_FILE_INFO **ListHead
1438 )
1439{
1440 LIST_ENTRY *Node;
1441
1442 //
1443 // ASSERT that ListHead is not NULL
1444 //
1445 ASSERT(ListHead != NULL);
1446
jcarsey1e6e84c2010-01-25 20:05:08 +00001447 //
jcarsey94b17fa2009-05-07 18:46:18 +00001448 // Check for UEFI Shell 2.0 protocols
1449 //
1450 if (mEfiShellProtocol != NULL) {
1451 return (mEfiShellProtocol->FreeFileList(ListHead));
1452 } else {
1453 //
jcarsey1e6e84c2010-01-25 20:05:08 +00001454 // Since this is EFI Shell version we need to free our internally made copy
jcarsey94b17fa2009-05-07 18:46:18 +00001455 // of the list
1456 //
jcarsey1e6e84c2010-01-25 20:05:08 +00001457 for ( Node = GetFirstNode(&(*ListHead)->Link)
1458 ; IsListEmpty(&(*ListHead)->Link) == FALSE
jcarsey9b3bf082009-06-23 21:15:07 +00001459 ; Node = GetFirstNode(&(*ListHead)->Link)) {
jcarsey94b17fa2009-05-07 18:46:18 +00001460 RemoveEntryList(Node);
jcarseyd2b45642009-05-11 18:02:16 +00001461 ((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->Handle->Close(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->Handle);
jcarsey94b17fa2009-05-07 18:46:18 +00001462 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->FullName);
1463 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->FileName);
1464 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->Info);
1465 FreePool((EFI_SHELL_FILE_INFO_NO_CONST*)Node);
1466 }
1467 return EFI_SUCCESS;
1468 }
1469}
1470
jcarsey125c2cf2009-11-18 21:36:50 +00001471/**
1472 Find a file by searching the CWD and then the path.
1473
jcarseyb3011f42010-01-11 21:49:04 +00001474 If FileName is NULL then ASSERT.
jcarsey125c2cf2009-11-18 21:36:50 +00001475
jcarseyb3011f42010-01-11 21:49:04 +00001476 If the return value is not NULL then the memory must be caller freed.
jcarsey125c2cf2009-11-18 21:36:50 +00001477
1478 @param FileName Filename string.
1479
1480 @retval NULL the file was not found
1481 @return !NULL the full path to the file.
1482**/
1483CHAR16 *
1484EFIAPI
1485ShellFindFilePath (
1486 IN CONST CHAR16 *FileName
1487 )
1488{
1489 CONST CHAR16 *Path;
1490 EFI_FILE_HANDLE Handle;
1491 EFI_STATUS Status;
1492 CHAR16 *RetVal;
1493 CHAR16 *TestPath;
1494 CONST CHAR16 *Walker;
jcarsey36a9d672009-11-20 21:13:41 +00001495 UINTN Size;
jcarsey1cd45e72010-01-29 15:07:44 +00001496 CHAR16 *TempChar;
jcarsey125c2cf2009-11-18 21:36:50 +00001497
1498 RetVal = NULL;
1499
1500 Path = ShellGetEnvironmentVariable(L"cwd");
1501 if (Path != NULL) {
jcarsey36a9d672009-11-20 21:13:41 +00001502 Size = StrSize(Path);
1503 Size += StrSize(FileName);
1504 TestPath = AllocateZeroPool(Size);
jcarseyc9d92df2010-02-03 15:37:54 +00001505 ASSERT(TestPath != NULL);
1506 if (TestPath == NULL) {
1507 return (NULL);
1508 }
jcarsey125c2cf2009-11-18 21:36:50 +00001509 StrCpy(TestPath, Path);
1510 StrCat(TestPath, FileName);
1511 Status = ShellOpenFileByName(TestPath, &Handle, EFI_FILE_MODE_READ, 0);
1512 if (!EFI_ERROR(Status)){
1513 RetVal = StrnCatGrow(&RetVal, NULL, TestPath, 0);
1514 ShellCloseFile(&Handle);
1515 FreePool(TestPath);
1516 return (RetVal);
1517 }
1518 FreePool(TestPath);
1519 }
1520 Path = ShellGetEnvironmentVariable(L"path");
1521 if (Path != NULL) {
jcarsey36a9d672009-11-20 21:13:41 +00001522 Size = StrSize(Path);
1523 Size += StrSize(FileName);
1524 TestPath = AllocateZeroPool(Size);
jcarsey1e6e84c2010-01-25 20:05:08 +00001525 Walker = (CHAR16*)Path;
jcarsey125c2cf2009-11-18 21:36:50 +00001526 do {
1527 CopyMem(TestPath, Walker, StrSize(Walker));
jcarsey1cd45e72010-01-29 15:07:44 +00001528 TempChar = StrStr(TestPath, L";");
1529 if (TempChar != NULL) {
1530 *TempChar = CHAR_NULL;
jcarsey125c2cf2009-11-18 21:36:50 +00001531 }
1532 StrCat(TestPath, FileName);
1533 if (StrStr(Walker, L";") != NULL) {
1534 Walker = StrStr(Walker, L";") + 1;
1535 } else {
1536 Walker = NULL;
1537 }
1538 Status = ShellOpenFileByName(TestPath, &Handle, EFI_FILE_MODE_READ, 0);
1539 if (!EFI_ERROR(Status)){
1540 RetVal = StrnCatGrow(&RetVal, NULL, TestPath, 0);
1541 ShellCloseFile(&Handle);
1542 break;
1543 }
1544 } while (Walker != NULL && Walker[0] != CHAR_NULL);
1545 FreePool(TestPath);
1546 }
1547 return (RetVal);
1548}
1549
jcarseyb3011f42010-01-11 21:49:04 +00001550/**
jcarsey1e6e84c2010-01-25 20:05:08 +00001551 Find a file by searching the CWD and then the path with a variable set of file
1552 extensions. If the file is not found it will append each extension in the list
jcarseyb3011f42010-01-11 21:49:04 +00001553 in the order provided and return the first one that is successful.
1554
1555 If FileName is NULL, then ASSERT.
1556 If FileExtension is NULL, then behavior is identical to ShellFindFilePath.
1557
1558 If the return value is not NULL then the memory must be caller freed.
1559
1560 @param[in] FileName Filename string.
1561 @param[in] FileExtension Semi-colon delimeted list of possible extensions.
1562
1563 @retval NULL The file was not found.
1564 @retval !NULL The path to the file.
1565**/
1566CHAR16 *
1567EFIAPI
1568ShellFindFilePathEx (
1569 IN CONST CHAR16 *FileName,
1570 IN CONST CHAR16 *FileExtension
1571 )
1572{
1573 CHAR16 *TestPath;
1574 CHAR16 *RetVal;
1575 CONST CHAR16 *ExtensionWalker;
jcarsey9e926b62010-01-14 20:26:39 +00001576 UINTN Size;
jcarsey1cd45e72010-01-29 15:07:44 +00001577 CHAR16 *TempChar;
jcarseyc9d92df2010-02-03 15:37:54 +00001578 CHAR16 *TempChar2;
jcarsey1cd45e72010-01-29 15:07:44 +00001579
jcarseyb3011f42010-01-11 21:49:04 +00001580 ASSERT(FileName != NULL);
1581 if (FileExtension == NULL) {
1582 return (ShellFindFilePath(FileName));
1583 }
1584 RetVal = ShellFindFilePath(FileName);
1585 if (RetVal != NULL) {
1586 return (RetVal);
1587 }
jcarsey9e926b62010-01-14 20:26:39 +00001588 Size = StrSize(FileName);
1589 Size += StrSize(FileExtension);
1590 TestPath = AllocateZeroPool(Size);
jcarseyc9d92df2010-02-03 15:37:54 +00001591 ASSERT(TestPath != NULL);
1592 if (TestPath == NULL) {
1593 return (NULL);
1594 }
1595 for (ExtensionWalker = FileExtension, TempChar2 = (CHAR16*)FileExtension; TempChar2 != NULL ; ExtensionWalker = TempChar2 + 1 ){
jcarseyb3011f42010-01-11 21:49:04 +00001596 StrCpy(TestPath, FileName);
jcarsey1cd45e72010-01-29 15:07:44 +00001597 if (ExtensionWalker != NULL) {
1598 StrCat(TestPath, ExtensionWalker);
1599 }
1600 TempChar = StrStr(TestPath, L";");
1601 if (TempChar != NULL) {
1602 *TempChar = CHAR_NULL;
jcarseyb3011f42010-01-11 21:49:04 +00001603 }
1604 RetVal = ShellFindFilePath(TestPath);
1605 if (RetVal != NULL) {
1606 break;
1607 }
jcarseyc9d92df2010-02-03 15:37:54 +00001608 TempChar2 = StrStr(ExtensionWalker, L";");
jcarseyb3011f42010-01-11 21:49:04 +00001609 }
1610 FreePool(TestPath);
1611 return (RetVal);
1612}
1613
jcarsey94b17fa2009-05-07 18:46:18 +00001614typedef struct {
jcarsey9b3bf082009-06-23 21:15:07 +00001615 LIST_ENTRY Link;
jcarsey94b17fa2009-05-07 18:46:18 +00001616 CHAR16 *Name;
1617 ParamType Type;
1618 CHAR16 *Value;
1619 UINTN OriginalPosition;
1620} SHELL_PARAM_PACKAGE;
1621
1622/**
jcarsey1e6e84c2010-01-25 20:05:08 +00001623 Checks the list of valid arguments and returns TRUE if the item was found. If the
jcarsey94b17fa2009-05-07 18:46:18 +00001624 return value is TRUE then the type parameter is set also.
jcarsey1e6e84c2010-01-25 20:05:08 +00001625
jcarsey94b17fa2009-05-07 18:46:18 +00001626 if CheckList is NULL then ASSERT();
1627 if Name is NULL then ASSERT();
1628 if Type is NULL then ASSERT();
1629
1630 @param Type pointer to type of parameter if it was found
1631 @param Name pointer to Name of parameter found
1632 @param CheckList List to check against
1633
1634 @retval TRUE the Parameter was found. Type is valid.
1635 @retval FALSE the Parameter was not found. Type is not valid.
1636**/
1637BOOLEAN
1638EFIAPI
jcarseyd2b45642009-05-11 18:02:16 +00001639InternalIsOnCheckList (
jcarsey94b17fa2009-05-07 18:46:18 +00001640 IN CONST CHAR16 *Name,
1641 IN CONST SHELL_PARAM_ITEM *CheckList,
1642 OUT ParamType *Type
jcarsey2247dde2009-11-09 18:08:58 +00001643 ) {
jcarsey94b17fa2009-05-07 18:46:18 +00001644 SHELL_PARAM_ITEM *TempListItem;
1645
1646 //
1647 // ASSERT that all 3 pointer parameters aren't NULL
1648 //
1649 ASSERT(CheckList != NULL);
1650 ASSERT(Type != NULL);
1651 ASSERT(Name != NULL);
1652
1653 //
jcarseyd2b45642009-05-11 18:02:16 +00001654 // question mark and page break mode are always supported
1655 //
1656 if ((StrCmp(Name, L"-?") == 0) ||
1657 (StrCmp(Name, L"-b") == 0)
1658 ) {
1659 return (TRUE);
1660 }
1661
1662 //
jcarsey94b17fa2009-05-07 18:46:18 +00001663 // Enumerate through the list
1664 //
1665 for (TempListItem = (SHELL_PARAM_ITEM*)CheckList ; TempListItem->Name != NULL ; TempListItem++) {
1666 //
jcarsey9eb53ac2009-07-08 17:26:58 +00001667 // If the Type is TypeStart only check the first characters of the passed in param
1668 // If it matches set the type and return TRUE
jcarsey94b17fa2009-05-07 18:46:18 +00001669 //
jcarsey9eb53ac2009-07-08 17:26:58 +00001670 if (TempListItem->Type == TypeStart && StrnCmp(Name, TempListItem->Name, StrLen(TempListItem->Name)) == 0) {
1671 *Type = TempListItem->Type;
1672 return (TRUE);
1673 } else if (StrCmp(Name, TempListItem->Name) == 0) {
jcarsey94b17fa2009-05-07 18:46:18 +00001674 *Type = TempListItem->Type;
1675 return (TRUE);
1676 }
1677 }
jcarsey2247dde2009-11-09 18:08:58 +00001678
jcarsey94b17fa2009-05-07 18:46:18 +00001679 return (FALSE);
1680}
1681/**
jcarseyd2b45642009-05-11 18:02:16 +00001682 Checks the string for indicators of "flag" status. this is a leading '/', '-', or '+'
jcarsey94b17fa2009-05-07 18:46:18 +00001683
1684 @param Name pointer to Name of parameter found
1685
1686 @retval TRUE the Parameter is a flag.
1687 @retval FALSE the Parameter not a flag
1688**/
1689BOOLEAN
1690EFIAPI
jcarseyd2b45642009-05-11 18:02:16 +00001691InternalIsFlag (
jcarsey2247dde2009-11-09 18:08:58 +00001692 IN CONST CHAR16 *Name,
1693 IN BOOLEAN AlwaysAllowNumbers
jcarsey94b17fa2009-05-07 18:46:18 +00001694 )
1695{
1696 //
1697 // ASSERT that Name isn't NULL
1698 //
1699 ASSERT(Name != NULL);
1700
1701 //
jcarsey2247dde2009-11-09 18:08:58 +00001702 // If we accept numbers then dont return TRUE. (they will be values)
1703 //
jcarsey969c7832010-01-13 16:46:33 +00001704 if (((Name[0] == L'-' || Name[0] == L'+') && ShellIsHexaDecimalDigitCharacter(Name[1])) && AlwaysAllowNumbers != FALSE) {
jcarsey2247dde2009-11-09 18:08:58 +00001705 return (FALSE);
1706 }
1707
1708 //
jcarsey94b17fa2009-05-07 18:46:18 +00001709 // If the Name has a / or - as the first character return TRUE
1710 //
jcarsey1e6e84c2010-01-25 20:05:08 +00001711 if ((Name[0] == L'/') ||
jcarseyd2b45642009-05-11 18:02:16 +00001712 (Name[0] == L'-') ||
1713 (Name[0] == L'+')
1714 ) {
jcarsey94b17fa2009-05-07 18:46:18 +00001715 return (TRUE);
1716 }
1717 return (FALSE);
1718}
1719
1720/**
jcarsey1e6e84c2010-01-25 20:05:08 +00001721 Checks the command line arguments passed against the list of valid ones.
jcarsey94b17fa2009-05-07 18:46:18 +00001722
1723 If no initialization is required, then return RETURN_SUCCESS.
jcarsey1e6e84c2010-01-25 20:05:08 +00001724
jcarsey94b17fa2009-05-07 18:46:18 +00001725 @param CheckList pointer to list of parameters to check
1726 @param CheckPackage pointer to pointer to list checked values
jcarsey1e6e84c2010-01-25 20:05:08 +00001727 @param ProblemParam optional pointer to pointer to unicode string for
jcarseyd2b45642009-05-11 18:02:16 +00001728 the paramater that caused failure. If used then the
1729 caller is responsible for freeing the memory.
jcarsey94b17fa2009-05-07 18:46:18 +00001730 @param AutoPageBreak will automatically set PageBreakEnabled for "b" parameter
1731 @param Argc Count of parameters in Argv
1732 @param Argv pointer to array of parameters
1733
1734 @retval EFI_SUCCESS The operation completed sucessfully.
1735 @retval EFI_OUT_OF_RESOURCES A memory allocation failed
1736 @retval EFI_INVALID_PARAMETER A parameter was invalid
jcarsey1e6e84c2010-01-25 20:05:08 +00001737 @retval EFI_VOLUME_CORRUPTED the command line was corrupt. an argument was
1738 duplicated. the duplicated command line argument
jcarsey94b17fa2009-05-07 18:46:18 +00001739 was returned in ProblemParam if provided.
jcarsey1e6e84c2010-01-25 20:05:08 +00001740 @retval EFI_NOT_FOUND a argument required a value that was missing.
jcarsey94b17fa2009-05-07 18:46:18 +00001741 the invalid command line argument was returned in
1742 ProblemParam if provided.
1743**/
jcarsey2247dde2009-11-09 18:08:58 +00001744STATIC
jcarsey94b17fa2009-05-07 18:46:18 +00001745EFI_STATUS
1746EFIAPI
1747InternalCommandLineParse (
1748 IN CONST SHELL_PARAM_ITEM *CheckList,
1749 OUT LIST_ENTRY **CheckPackage,
1750 OUT CHAR16 **ProblemParam OPTIONAL,
1751 IN BOOLEAN AutoPageBreak,
1752 IN CONST CHAR16 **Argv,
jcarsey2247dde2009-11-09 18:08:58 +00001753 IN UINTN Argc,
1754 IN BOOLEAN AlwaysAllowNumbers
1755 ) {
jcarsey94b17fa2009-05-07 18:46:18 +00001756 UINTN LoopCounter;
jcarsey94b17fa2009-05-07 18:46:18 +00001757 ParamType CurrentItemType;
1758 SHELL_PARAM_PACKAGE *CurrentItemPackage;
jcarsey125c2cf2009-11-18 21:36:50 +00001759 UINTN GetItemValue;
1760 UINTN ValueSize;
jcarsey94b17fa2009-05-07 18:46:18 +00001761
1762 CurrentItemPackage = NULL;
jcarsey2247dde2009-11-09 18:08:58 +00001763 mTotalParameterCount = 0;
jcarsey125c2cf2009-11-18 21:36:50 +00001764 GetItemValue = 0;
1765 ValueSize = 0;
jcarsey94b17fa2009-05-07 18:46:18 +00001766
1767 //
1768 // If there is only 1 item we dont need to do anything
1769 //
1770 if (Argc <= 1) {
1771 *CheckPackage = NULL;
1772 return (EFI_SUCCESS);
1773 }
1774
1775 //
jcarsey2247dde2009-11-09 18:08:58 +00001776 // ASSERTs
1777 //
1778 ASSERT(CheckList != NULL);
1779 ASSERT(Argv != NULL);
1780
1781 //
jcarsey94b17fa2009-05-07 18:46:18 +00001782 // initialize the linked list
1783 //
1784 *CheckPackage = (LIST_ENTRY*)AllocateZeroPool(sizeof(LIST_ENTRY));
1785 InitializeListHead(*CheckPackage);
1786
1787 //
1788 // loop through each of the arguments
1789 //
1790 for (LoopCounter = 0 ; LoopCounter < Argc ; ++LoopCounter) {
1791 if (Argv[LoopCounter] == NULL) {
1792 //
1793 // do nothing for NULL argv
1794 //
jcarseyb3011f42010-01-11 21:49:04 +00001795 } else if (InternalIsOnCheckList(Argv[LoopCounter], CheckList, &CurrentItemType) != FALSE) {
jcarsey94b17fa2009-05-07 18:46:18 +00001796 //
jcarsey2247dde2009-11-09 18:08:58 +00001797 // We might have leftover if last parameter didnt have optional value
1798 //
jcarsey125c2cf2009-11-18 21:36:50 +00001799 if (GetItemValue != 0) {
1800 GetItemValue = 0;
jcarsey2247dde2009-11-09 18:08:58 +00001801 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
1802 }
1803 //
jcarsey94b17fa2009-05-07 18:46:18 +00001804 // this is a flag
1805 //
1806 CurrentItemPackage = AllocatePool(sizeof(SHELL_PARAM_PACKAGE));
1807 ASSERT(CurrentItemPackage != NULL);
1808 CurrentItemPackage->Name = AllocatePool(StrSize(Argv[LoopCounter]));
1809 ASSERT(CurrentItemPackage->Name != NULL);
1810 StrCpy(CurrentItemPackage->Name, Argv[LoopCounter]);
1811 CurrentItemPackage->Type = CurrentItemType;
1812 CurrentItemPackage->OriginalPosition = (UINTN)(-1);
jcarseyb1f95a02009-06-16 00:23:19 +00001813 CurrentItemPackage->Value = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +00001814
1815 //
1816 // Does this flag require a value
1817 //
jcarsey125c2cf2009-11-18 21:36:50 +00001818 switch (CurrentItemPackage->Type) {
jcarsey94b17fa2009-05-07 18:46:18 +00001819 //
jcarsey125c2cf2009-11-18 21:36:50 +00001820 // possibly trigger the next loop(s) to populate the value of this item
jcarsey1e6e84c2010-01-25 20:05:08 +00001821 //
jcarsey125c2cf2009-11-18 21:36:50 +00001822 case TypeValue:
jcarsey1e6e84c2010-01-25 20:05:08 +00001823 GetItemValue = 1;
jcarsey125c2cf2009-11-18 21:36:50 +00001824 ValueSize = 0;
1825 break;
1826 case TypeDoubleValue:
1827 GetItemValue = 2;
1828 ValueSize = 0;
1829 break;
1830 case TypeMaxValue:
1831 GetItemValue = (UINTN)(-1);
1832 ValueSize = 0;
1833 break;
1834 default:
1835 //
1836 // this item has no value expected; we are done
1837 //
1838 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
1839 ASSERT(GetItemValue == 0);
1840 break;
jcarsey94b17fa2009-05-07 18:46:18 +00001841 }
jcarsey125c2cf2009-11-18 21:36:50 +00001842 } else if (GetItemValue != 0 && InternalIsFlag(Argv[LoopCounter], AlwaysAllowNumbers) == FALSE) {
jcarseyb1f95a02009-06-16 00:23:19 +00001843 ASSERT(CurrentItemPackage != NULL);
1844 //
jcarsey125c2cf2009-11-18 21:36:50 +00001845 // get the item VALUE for a previous flag
jcarseyb1f95a02009-06-16 00:23:19 +00001846 //
jcarsey125c2cf2009-11-18 21:36:50 +00001847 CurrentItemPackage->Value = ReallocatePool(ValueSize, ValueSize + StrSize(Argv[LoopCounter]) + sizeof(CHAR16), CurrentItemPackage->Value);
jcarseyb1f95a02009-06-16 00:23:19 +00001848 ASSERT(CurrentItemPackage->Value != NULL);
jcarsey125c2cf2009-11-18 21:36:50 +00001849 if (ValueSize == 0) {
1850 StrCpy(CurrentItemPackage->Value, Argv[LoopCounter]);
1851 } else {
1852 StrCat(CurrentItemPackage->Value, L" ");
1853 StrCat(CurrentItemPackage->Value, Argv[LoopCounter]);
1854 }
1855 ValueSize += StrSize(Argv[LoopCounter]) + sizeof(CHAR16);
1856 GetItemValue--;
1857 if (GetItemValue == 0) {
1858 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
1859 }
jcarsey2247dde2009-11-09 18:08:58 +00001860 } else if (InternalIsFlag(Argv[LoopCounter], AlwaysAllowNumbers) == FALSE) {
jcarseyb1f95a02009-06-16 00:23:19 +00001861 //
1862 // add this one as a non-flag
1863 //
1864 CurrentItemPackage = AllocatePool(sizeof(SHELL_PARAM_PACKAGE));
1865 ASSERT(CurrentItemPackage != NULL);
1866 CurrentItemPackage->Name = NULL;
1867 CurrentItemPackage->Type = TypePosition;
1868 CurrentItemPackage->Value = AllocatePool(StrSize(Argv[LoopCounter]));
1869 ASSERT(CurrentItemPackage->Value != NULL);
1870 StrCpy(CurrentItemPackage->Value, Argv[LoopCounter]);
jcarsey2247dde2009-11-09 18:08:58 +00001871 CurrentItemPackage->OriginalPosition = mTotalParameterCount++;
jcarsey9b3bf082009-06-23 21:15:07 +00001872 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
jcarsey94b17fa2009-05-07 18:46:18 +00001873 } else if (ProblemParam) {
1874 //
1875 // this was a non-recognised flag... error!
1876 //
jcarseyd2b45642009-05-11 18:02:16 +00001877 *ProblemParam = AllocatePool(StrSize(Argv[LoopCounter]));
1878 ASSERT(*ProblemParam != NULL);
1879 StrCpy(*ProblemParam, Argv[LoopCounter]);
jcarsey94b17fa2009-05-07 18:46:18 +00001880 ShellCommandLineFreeVarList(*CheckPackage);
1881 *CheckPackage = NULL;
1882 return (EFI_VOLUME_CORRUPTED);
1883 } else {
1884 ShellCommandLineFreeVarList(*CheckPackage);
1885 *CheckPackage = NULL;
1886 return (EFI_VOLUME_CORRUPTED);
1887 }
1888 }
jcarsey125c2cf2009-11-18 21:36:50 +00001889 if (GetItemValue != 0) {
1890 GetItemValue = 0;
1891 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
1892 }
jcarsey94b17fa2009-05-07 18:46:18 +00001893 //
1894 // support for AutoPageBreak
1895 //
1896 if (AutoPageBreak && ShellCommandLineGetFlag(*CheckPackage, L"-b")) {
1897 ShellSetPageBreakMode(TRUE);
1898 }
1899 return (EFI_SUCCESS);
1900}
1901
1902/**
jcarsey1e6e84c2010-01-25 20:05:08 +00001903 Checks the command line arguments passed against the list of valid ones.
jcarsey94b17fa2009-05-07 18:46:18 +00001904 Optionally removes NULL values first.
jcarsey1e6e84c2010-01-25 20:05:08 +00001905
jcarsey94b17fa2009-05-07 18:46:18 +00001906 If no initialization is required, then return RETURN_SUCCESS.
jcarsey1e6e84c2010-01-25 20:05:08 +00001907
jcarsey94b17fa2009-05-07 18:46:18 +00001908 @param CheckList pointer to list of parameters to check
1909 @param CheckPackage pointer to pointer to list checked values
jcarsey1e6e84c2010-01-25 20:05:08 +00001910 @param ProblemParam optional pointer to pointer to unicode string for
jcarsey94b17fa2009-05-07 18:46:18 +00001911 the paramater that caused failure.
1912 @param AutoPageBreak will automatically set PageBreakEnabled for "b" parameter
1913
1914 @retval EFI_SUCCESS The operation completed sucessfully.
1915 @retval EFI_OUT_OF_RESOURCES A memory allocation failed
1916 @retval EFI_INVALID_PARAMETER A parameter was invalid
jcarsey1e6e84c2010-01-25 20:05:08 +00001917 @retval EFI_VOLUME_CORRUPTED the command line was corrupt. an argument was
1918 duplicated. the duplicated command line argument
jcarsey94b17fa2009-05-07 18:46:18 +00001919 was returned in ProblemParam if provided.
1920 @retval EFI_DEVICE_ERROR the commands contained 2 opposing arguments. one
jcarsey1e6e84c2010-01-25 20:05:08 +00001921 of the command line arguments was returned in
jcarsey94b17fa2009-05-07 18:46:18 +00001922 ProblemParam if provided.
jcarsey1e6e84c2010-01-25 20:05:08 +00001923 @retval EFI_NOT_FOUND a argument required a value that was missing.
jcarsey94b17fa2009-05-07 18:46:18 +00001924 the invalid command line argument was returned in
1925 ProblemParam if provided.
1926**/
1927EFI_STATUS
1928EFIAPI
jcarsey2247dde2009-11-09 18:08:58 +00001929ShellCommandLineParseEx (
jcarsey94b17fa2009-05-07 18:46:18 +00001930 IN CONST SHELL_PARAM_ITEM *CheckList,
1931 OUT LIST_ENTRY **CheckPackage,
1932 OUT CHAR16 **ProblemParam OPTIONAL,
jcarsey2247dde2009-11-09 18:08:58 +00001933 IN BOOLEAN AutoPageBreak,
1934 IN BOOLEAN AlwaysAllowNumbers
1935 ) {
jcarsey1e6e84c2010-01-25 20:05:08 +00001936 //
jcarsey94b17fa2009-05-07 18:46:18 +00001937 // ASSERT that CheckList and CheckPackage aren't NULL
1938 //
1939 ASSERT(CheckList != NULL);
1940 ASSERT(CheckPackage != NULL);
1941
jcarsey1e6e84c2010-01-25 20:05:08 +00001942 //
jcarsey94b17fa2009-05-07 18:46:18 +00001943 // Check for UEFI Shell 2.0 protocols
1944 //
1945 if (mEfiShellParametersProtocol != NULL) {
jcarsey1e6e84c2010-01-25 20:05:08 +00001946 return (InternalCommandLineParse(CheckList,
1947 CheckPackage,
1948 ProblemParam,
1949 AutoPageBreak,
jljusten08d7f8e2009-06-15 18:42:13 +00001950 (CONST CHAR16**) mEfiShellParametersProtocol->Argv,
jcarsey2247dde2009-11-09 18:08:58 +00001951 mEfiShellParametersProtocol->Argc,
1952 AlwaysAllowNumbers));
jcarsey94b17fa2009-05-07 18:46:18 +00001953 }
1954
jcarsey1e6e84c2010-01-25 20:05:08 +00001955 //
jcarsey94b17fa2009-05-07 18:46:18 +00001956 // ASSERT That EFI Shell is not required
1957 //
1958 ASSERT (mEfiShellInterface != NULL);
jcarsey1e6e84c2010-01-25 20:05:08 +00001959 return (InternalCommandLineParse(CheckList,
1960 CheckPackage,
1961 ProblemParam,
1962 AutoPageBreak,
jljusten08d7f8e2009-06-15 18:42:13 +00001963 (CONST CHAR16**) mEfiShellInterface->Argv,
jcarsey2247dde2009-11-09 18:08:58 +00001964 mEfiShellInterface->Argc,
1965 AlwaysAllowNumbers));
jcarsey94b17fa2009-05-07 18:46:18 +00001966}
1967
1968/**
1969 Frees shell variable list that was returned from ShellCommandLineParse.
1970
1971 This function will free all the memory that was used for the CheckPackage
1972 list of postprocessed shell arguments.
1973
1974 this function has no return value.
1975
1976 if CheckPackage is NULL, then return
1977
1978 @param CheckPackage the list to de-allocate
1979 **/
1980VOID
1981EFIAPI
1982ShellCommandLineFreeVarList (
1983 IN LIST_ENTRY *CheckPackage
jcarsey2247dde2009-11-09 18:08:58 +00001984 ) {
jcarsey94b17fa2009-05-07 18:46:18 +00001985 LIST_ENTRY *Node;
1986
1987 //
1988 // check for CheckPackage == NULL
1989 //
1990 if (CheckPackage == NULL) {
1991 return;
1992 }
1993
1994 //
1995 // for each node in the list
1996 //
jcarsey9eb53ac2009-07-08 17:26:58 +00001997 for ( Node = GetFirstNode(CheckPackage)
jcarsey2247dde2009-11-09 18:08:58 +00001998 ; IsListEmpty(CheckPackage) == FALSE
jcarsey9eb53ac2009-07-08 17:26:58 +00001999 ; Node = GetFirstNode(CheckPackage)
2000 ){
jcarsey94b17fa2009-05-07 18:46:18 +00002001 //
2002 // Remove it from the list
2003 //
2004 RemoveEntryList(Node);
2005
2006 //
2007 // if it has a name free the name
2008 //
2009 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {
2010 FreePool(((SHELL_PARAM_PACKAGE*)Node)->Name);
2011 }
2012
2013 //
2014 // if it has a value free the value
2015 //
2016 if (((SHELL_PARAM_PACKAGE*)Node)->Value != NULL) {
2017 FreePool(((SHELL_PARAM_PACKAGE*)Node)->Value);
2018 }
jcarsey1e6e84c2010-01-25 20:05:08 +00002019
jcarsey94b17fa2009-05-07 18:46:18 +00002020 //
2021 // free the node structure
2022 //
2023 FreePool((SHELL_PARAM_PACKAGE*)Node);
2024 }
2025 //
2026 // free the list head node
2027 //
2028 FreePool(CheckPackage);
2029}
2030/**
2031 Checks for presence of a flag parameter
2032
2033 flag arguments are in the form of "-<Key>" or "/<Key>", but do not have a value following the key
2034
2035 if CheckPackage is NULL then return FALSE.
2036 if KeyString is NULL then ASSERT()
jcarsey1e6e84c2010-01-25 20:05:08 +00002037
jcarsey94b17fa2009-05-07 18:46:18 +00002038 @param CheckPackage The package of parsed command line arguments
2039 @param KeyString the Key of the command line argument to check for
2040
2041 @retval TRUE the flag is on the command line
2042 @retval FALSE the flag is not on the command line
2043 **/
2044BOOLEAN
2045EFIAPI
2046ShellCommandLineGetFlag (
2047 IN CONST LIST_ENTRY *CheckPackage,
2048 IN CHAR16 *KeyString
jcarsey2247dde2009-11-09 18:08:58 +00002049 ) {
jcarsey94b17fa2009-05-07 18:46:18 +00002050 LIST_ENTRY *Node;
2051
2052 //
2053 // ASSERT that both CheckPackage and KeyString aren't NULL
2054 //
2055 ASSERT(KeyString != NULL);
2056
2057 //
2058 // return FALSE for no package
2059 //
2060 if (CheckPackage == NULL) {
2061 return (FALSE);
2062 }
2063
2064 //
2065 // enumerate through the list of parametrs
2066 //
jcarsey1e6e84c2010-01-25 20:05:08 +00002067 for ( Node = GetFirstNode(CheckPackage)
2068 ; !IsNull (CheckPackage, Node)
2069 ; Node = GetNextNode(CheckPackage, Node)
jcarsey9eb53ac2009-07-08 17:26:58 +00002070 ){
jcarsey94b17fa2009-05-07 18:46:18 +00002071 //
2072 // If the Name matches, return TRUE (and there may be NULL name)
2073 //
2074 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {
jcarsey9eb53ac2009-07-08 17:26:58 +00002075 //
2076 // If Type is TypeStart then only compare the begining of the strings
2077 //
jcarsey1e6e84c2010-01-25 20:05:08 +00002078 if ( ((SHELL_PARAM_PACKAGE*)Node)->Type == TypeStart
jcarsey9eb53ac2009-07-08 17:26:58 +00002079 && StrnCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name, StrLen(KeyString)) == 0
2080 ){
2081 return (TRUE);
2082 } else if (StrCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {
jcarsey94b17fa2009-05-07 18:46:18 +00002083 return (TRUE);
2084 }
2085 }
2086 }
2087 return (FALSE);
2088}
2089/**
2090 returns value from command line argument
2091
2092 value parameters are in the form of "-<Key> value" or "/<Key> value"
jcarsey1e6e84c2010-01-25 20:05:08 +00002093
jcarsey94b17fa2009-05-07 18:46:18 +00002094 if CheckPackage is NULL, then return NULL;
2095
2096 @param CheckPackage The package of parsed command line arguments
2097 @param KeyString the Key of the command line argument to check for
2098
2099 @retval NULL the flag is not on the command line
2100 @return !=NULL pointer to unicode string of the value
2101 **/
2102CONST CHAR16*
2103EFIAPI
2104ShellCommandLineGetValue (
2105 IN CONST LIST_ENTRY *CheckPackage,
2106 IN CHAR16 *KeyString
jcarsey2247dde2009-11-09 18:08:58 +00002107 ) {
jcarsey94b17fa2009-05-07 18:46:18 +00002108 LIST_ENTRY *Node;
2109
2110 //
2111 // check for CheckPackage == NULL
2112 //
2113 if (CheckPackage == NULL) {
2114 return (NULL);
2115 }
2116
2117 //
2118 // enumerate through the list of parametrs
2119 //
jcarsey1e6e84c2010-01-25 20:05:08 +00002120 for ( Node = GetFirstNode(CheckPackage)
2121 ; !IsNull (CheckPackage, Node)
2122 ; Node = GetNextNode(CheckPackage, Node)
jcarsey9eb53ac2009-07-08 17:26:58 +00002123 ){
jcarsey94b17fa2009-05-07 18:46:18 +00002124 //
2125 // If the Name matches, return the value (name can be NULL)
2126 //
2127 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {
jcarsey9eb53ac2009-07-08 17:26:58 +00002128 //
2129 // If Type is TypeStart then only compare the begining of the strings
2130 //
jcarsey1e6e84c2010-01-25 20:05:08 +00002131 if ( ((SHELL_PARAM_PACKAGE*)Node)->Type == TypeStart
jcarsey9eb53ac2009-07-08 17:26:58 +00002132 && StrnCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name, StrLen(KeyString)) == 0
2133 ){
2134 //
2135 // return the string part after the flag
2136 //
2137 return (((SHELL_PARAM_PACKAGE*)Node)->Name + StrLen(KeyString));
2138 } else if (StrCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {
2139 //
2140 // return the value
2141 //
jcarsey94b17fa2009-05-07 18:46:18 +00002142 return (((SHELL_PARAM_PACKAGE*)Node)->Value);
2143 }
2144 }
2145 }
2146 return (NULL);
2147}
2148/**
2149 returns raw value from command line argument
2150
2151 raw value parameters are in the form of "value" in a specific position in the list
jcarsey1e6e84c2010-01-25 20:05:08 +00002152
jcarsey94b17fa2009-05-07 18:46:18 +00002153 if CheckPackage is NULL, then return NULL;
2154
2155 @param CheckPackage The package of parsed command line arguments
jcarsey1e6e84c2010-01-25 20:05:08 +00002156 @param Position the position of the value
jcarsey94b17fa2009-05-07 18:46:18 +00002157
2158 @retval NULL the flag is not on the command line
2159 @return !=NULL pointer to unicode string of the value
2160 **/
2161CONST CHAR16*
2162EFIAPI
2163ShellCommandLineGetRawValue (
2164 IN CONST LIST_ENTRY *CheckPackage,
2165 IN UINT32 Position
jcarsey2247dde2009-11-09 18:08:58 +00002166 ) {
jcarsey94b17fa2009-05-07 18:46:18 +00002167 LIST_ENTRY *Node;
2168
2169 //
2170 // check for CheckPackage == NULL
2171 //
2172 if (CheckPackage == NULL) {
2173 return (NULL);
2174 }
2175
2176 //
2177 // enumerate through the list of parametrs
2178 //
jcarsey1e6e84c2010-01-25 20:05:08 +00002179 for ( Node = GetFirstNode(CheckPackage)
2180 ; !IsNull (CheckPackage, Node)
2181 ; Node = GetNextNode(CheckPackage, Node)
jcarseyb82bfcc2009-06-29 16:28:23 +00002182 ){
jcarsey94b17fa2009-05-07 18:46:18 +00002183 //
2184 // If the position matches, return the value
2185 //
2186 if (((SHELL_PARAM_PACKAGE*)Node)->OriginalPosition == Position) {
2187 return (((SHELL_PARAM_PACKAGE*)Node)->Value);
2188 }
2189 }
2190 return (NULL);
jcarseyb1f95a02009-06-16 00:23:19 +00002191}
jcarsey2247dde2009-11-09 18:08:58 +00002192
2193/**
jcarsey1e6e84c2010-01-25 20:05:08 +00002194 returns the number of command line value parameters that were parsed.
2195
jcarsey2247dde2009-11-09 18:08:58 +00002196 this will not include flags.
2197
2198 @retval (UINTN)-1 No parsing has ocurred
2199 @return other The number of value parameters found
2200**/
2201UINTN
2202EFIAPI
2203ShellCommandLineGetCount(
2204 VOID
jcarsey125c2cf2009-11-18 21:36:50 +00002205 )
2206{
jcarsey2247dde2009-11-09 18:08:58 +00002207 return (mTotalParameterCount);
2208}
2209
jcarsey975136a2009-06-16 19:03:54 +00002210/**
jcarsey36a9d672009-11-20 21:13:41 +00002211 Determins if a parameter is duplicated.
2212
jcarsey1e6e84c2010-01-25 20:05:08 +00002213 If Param is not NULL then it will point to a callee allocated string buffer
jcarsey36a9d672009-11-20 21:13:41 +00002214 with the parameter value if a duplicate is found.
2215
2216 If CheckPackage is NULL, then ASSERT.
2217
2218 @param[in] CheckPackage The package of parsed command line arguments.
2219 @param[out] Param Upon finding one, a pointer to the duplicated parameter.
2220
2221 @retval EFI_SUCCESS No parameters were duplicated.
2222 @retval EFI_DEVICE_ERROR A duplicate was found.
2223 **/
2224EFI_STATUS
2225EFIAPI
2226ShellCommandLineCheckDuplicate (
2227 IN CONST LIST_ENTRY *CheckPackage,
2228 OUT CHAR16 **Param
2229 )
2230{
2231 LIST_ENTRY *Node1;
2232 LIST_ENTRY *Node2;
jcarsey1e6e84c2010-01-25 20:05:08 +00002233
jcarsey36a9d672009-11-20 21:13:41 +00002234 ASSERT(CheckPackage != NULL);
2235
jcarsey1e6e84c2010-01-25 20:05:08 +00002236 for ( Node1 = GetFirstNode(CheckPackage)
2237 ; !IsNull (CheckPackage, Node1)
2238 ; Node1 = GetNextNode(CheckPackage, Node1)
jcarsey36a9d672009-11-20 21:13:41 +00002239 ){
jcarsey1e6e84c2010-01-25 20:05:08 +00002240 for ( Node2 = GetNextNode(CheckPackage, Node1)
2241 ; !IsNull (CheckPackage, Node2)
2242 ; Node2 = GetNextNode(CheckPackage, Node2)
jcarsey36a9d672009-11-20 21:13:41 +00002243 ){
2244 if (StrCmp(((SHELL_PARAM_PACKAGE*)Node1)->Name, ((SHELL_PARAM_PACKAGE*)Node2)->Name) == 0) {
2245 if (Param != NULL) {
2246 *Param = NULL;
2247 *Param = StrnCatGrow(Param, NULL, ((SHELL_PARAM_PACKAGE*)Node1)->Name, 0);
2248 }
2249 return (EFI_DEVICE_ERROR);
2250 }
2251 }
2252 }
2253 return (EFI_SUCCESS);
2254}
2255
2256/**
jcarsey1e6e84c2010-01-25 20:05:08 +00002257 This is a find and replace function. Upon successful return the NewString is a copy of
jcarsey975136a2009-06-16 19:03:54 +00002258 SourceString with each instance of FindTarget replaced with ReplaceWith.
2259
jcarseyb3011f42010-01-11 21:49:04 +00002260 If SourceString and NewString overlap the behavior is undefined.
2261
jcarsey975136a2009-06-16 19:03:54 +00002262 If the string would grow bigger than NewSize it will halt and return error.
2263
2264 @param[in] SourceString String with source buffer
jcarseyb82bfcc2009-06-29 16:28:23 +00002265 @param[in,out] NewString String with resultant buffer
jcarsey975136a2009-06-16 19:03:54 +00002266 @param[in] NewSize Size in bytes of NewString
2267 @param[in] FindTarget String to look for
2268 @param[in[ ReplaceWith String to replace FindTarget with
jcarsey969c7832010-01-13 16:46:33 +00002269 @param[in] SkipPreCarrot If TRUE will skip a FindTarget that has a '^'
2270 immediately before it.
jcarsey975136a2009-06-16 19:03:54 +00002271
jcarsey969c7832010-01-13 16:46:33 +00002272 @retval EFI_INVALID_PARAMETER SourceString was NULL.
2273 @retval EFI_INVALID_PARAMETER NewString was NULL.
2274 @retval EFI_INVALID_PARAMETER FindTarget was NULL.
2275 @retval EFI_INVALID_PARAMETER ReplaceWith was NULL.
2276 @retval EFI_INVALID_PARAMETER FindTarget had length < 1.
2277 @retval EFI_INVALID_PARAMETER SourceString had length < 1.
jcarsey1e6e84c2010-01-25 20:05:08 +00002278 @retval EFI_BUFFER_TOO_SMALL NewSize was less than the minimum size to hold
jcarsey969c7832010-01-13 16:46:33 +00002279 the new string (truncation occurred).
2280 @retval EFI_SUCCESS the string was sucessfully copied with replacement.
jcarsey975136a2009-06-16 19:03:54 +00002281**/
2282
2283EFI_STATUS
2284EFIAPI
jcarsey969c7832010-01-13 16:46:33 +00002285ShellCopySearchAndReplace2(
jcarsey975136a2009-06-16 19:03:54 +00002286 IN CHAR16 CONST *SourceString,
2287 IN CHAR16 *NewString,
2288 IN UINTN NewSize,
2289 IN CONST CHAR16 *FindTarget,
jcarsey969c7832010-01-13 16:46:33 +00002290 IN CONST CHAR16 *ReplaceWith,
2291 IN CONST BOOLEAN SkipPreCarrot
jcarsey1e6e84c2010-01-25 20:05:08 +00002292 )
jcarsey2247dde2009-11-09 18:08:58 +00002293{
jcarsey01582942009-07-10 19:46:17 +00002294 UINTN Size;
jcarsey975136a2009-06-16 19:03:54 +00002295 if ( (SourceString == NULL)
2296 || (NewString == NULL)
2297 || (FindTarget == NULL)
2298 || (ReplaceWith == NULL)
2299 || (StrLen(FindTarget) < 1)
2300 || (StrLen(SourceString) < 1)
2301 ){
2302 return (EFI_INVALID_PARAMETER);
2303 }
jcarsey2247dde2009-11-09 18:08:58 +00002304 NewString = SetMem16(NewString, NewSize, CHAR_NULL);
2305 while (*SourceString != CHAR_NULL) {
jcarsey969c7832010-01-13 16:46:33 +00002306 //
jcarsey1e6e84c2010-01-25 20:05:08 +00002307 // if we find the FindTarget and either Skip == FALSE or Skip == TRUE and we
jcarsey969c7832010-01-13 16:46:33 +00002308 // dont have a carrot do a replace...
2309 //
jcarsey1e6e84c2010-01-25 20:05:08 +00002310 if (StrnCmp(SourceString, FindTarget, StrLen(FindTarget)) == 0
jcarsey969c7832010-01-13 16:46:33 +00002311 && ((SkipPreCarrot && *(SourceString-1) != L'^') || SkipPreCarrot == FALSE)
2312 ){
jcarsey975136a2009-06-16 19:03:54 +00002313 SourceString += StrLen(FindTarget);
jcarsey01582942009-07-10 19:46:17 +00002314 Size = StrSize(NewString);
2315 if ((Size + (StrLen(ReplaceWith)*sizeof(CHAR16))) > NewSize) {
jcarsey975136a2009-06-16 19:03:54 +00002316 return (EFI_BUFFER_TOO_SMALL);
2317 }
2318 StrCat(NewString, ReplaceWith);
2319 } else {
jcarsey01582942009-07-10 19:46:17 +00002320 Size = StrSize(NewString);
2321 if (Size + sizeof(CHAR16) > NewSize) {
jcarsey975136a2009-06-16 19:03:54 +00002322 return (EFI_BUFFER_TOO_SMALL);
2323 }
2324 StrnCat(NewString, SourceString, 1);
2325 SourceString++;
2326 }
2327 }
2328 return (EFI_SUCCESS);
2329}
jcarseyb1f95a02009-06-16 00:23:19 +00002330
2331/**
jcarseye2f82972009-12-01 05:40:24 +00002332 Internal worker function to output a string.
2333
2334 This function will output a string to the correct StdOut.
2335
2336 @param[in] String The string to print out.
2337
2338 @retval EFI_SUCCESS The operation was sucessful.
2339 @retval !EFI_SUCCESS The operation failed.
2340**/
2341EFI_STATUS
2342EFIAPI
2343InternalPrintTo (
2344 IN CONST CHAR16 *String
2345 )
2346{
2347 UINTN Size;
2348 Size = StrSize(String) - sizeof(CHAR16);
2349 if (mEfiShellParametersProtocol != NULL) {
2350 return (mEfiShellParametersProtocol->StdOut->Write(mEfiShellParametersProtocol->StdOut, &Size, (VOID*)String));
2351 }
2352 if (mEfiShellInterface != NULL) {
jcarseyecd3d592009-12-07 18:05:00 +00002353 //
2354 // Divide in half for old shell. Must be string length not size.
2355 //
2356 Size /= 2;
jcarseye2f82972009-12-01 05:40:24 +00002357 return ( mEfiShellInterface->StdOut->Write(mEfiShellInterface->StdOut, &Size, (VOID*)String));
2358 }
2359 ASSERT(FALSE);
2360 return (EFI_UNSUPPORTED);
2361}
2362
2363/**
jcarseyb1f95a02009-06-16 00:23:19 +00002364 Print at a specific location on the screen.
2365
jcarseyf1b87e72009-06-17 00:52:11 +00002366 This function will move the cursor to a given screen location and print the specified string
jcarsey1e6e84c2010-01-25 20:05:08 +00002367
2368 If -1 is specified for either the Row or Col the current screen location for BOTH
jcarseyf1b87e72009-06-17 00:52:11 +00002369 will be used.
jcarseyb1f95a02009-06-16 00:23:19 +00002370
2371 if either Row or Col is out of range for the current console, then ASSERT
2372 if Format is NULL, then ASSERT
2373
jcarsey1e6e84c2010-01-25 20:05:08 +00002374 In addition to the standard %-based flags as supported by UefiLib Print() this supports
jcarseyb1f95a02009-06-16 00:23:19 +00002375 the following additional flags:
2376 %N - Set output attribute to normal
2377 %H - Set output attribute to highlight
2378 %E - Set output attribute to error
2379 %B - Set output attribute to blue color
2380 %V - Set output attribute to green color
2381
2382 Note: The background color is controlled by the shell command cls.
2383
2384 @param[in] Row the row to print at
2385 @param[in] Col the column to print at
2386 @param[in] Format the format string
jcarsey2247dde2009-11-09 18:08:58 +00002387 @param[in] Marker the marker for the variable argument list
jcarseyb1f95a02009-06-16 00:23:19 +00002388
2389 @return the number of characters printed to the screen
2390**/
2391
2392UINTN
2393EFIAPI
jcarsey2247dde2009-11-09 18:08:58 +00002394InternalShellPrintWorker(
jcarseyb1f95a02009-06-16 00:23:19 +00002395 IN INT32 Col OPTIONAL,
2396 IN INT32 Row OPTIONAL,
2397 IN CONST CHAR16 *Format,
jcarsey2247dde2009-11-09 18:08:58 +00002398 VA_LIST Marker
jcarsey1e6e84c2010-01-25 20:05:08 +00002399 )
jcarsey2247dde2009-11-09 18:08:58 +00002400{
jcarseyb1f95a02009-06-16 00:23:19 +00002401 UINTN Return;
jcarseyb1f95a02009-06-16 00:23:19 +00002402 EFI_STATUS Status;
jcarsey975136a2009-06-16 19:03:54 +00002403 UINTN NormalAttribute;
2404 CHAR16 *ResumeLocation;
2405 CHAR16 *FormatWalker;
jcarsey1e6e84c2010-01-25 20:05:08 +00002406
jcarsey975136a2009-06-16 19:03:54 +00002407 //
2408 // Back and forth each time fixing up 1 of our flags...
2409 //
jcarseyb3011f42010-01-11 21:49:04 +00002410 Status = ShellLibCopySearchAndReplace(Format, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%N", L"%%N");
jcarsey975136a2009-06-16 19:03:54 +00002411 ASSERT_EFI_ERROR(Status);
jcarseyb3011f42010-01-11 21:49:04 +00002412 Status = ShellLibCopySearchAndReplace(mPostReplaceFormat, mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), L"%E", L"%%E");
jcarsey975136a2009-06-16 19:03:54 +00002413 ASSERT_EFI_ERROR(Status);
jcarseyb3011f42010-01-11 21:49:04 +00002414 Status = ShellLibCopySearchAndReplace(mPostReplaceFormat2, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%H", L"%%H");
jcarsey975136a2009-06-16 19:03:54 +00002415 ASSERT_EFI_ERROR(Status);
jcarseyb3011f42010-01-11 21:49:04 +00002416 Status = ShellLibCopySearchAndReplace(mPostReplaceFormat, mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), L"%B", L"%%B");
jcarsey975136a2009-06-16 19:03:54 +00002417 ASSERT_EFI_ERROR(Status);
jcarseyb3011f42010-01-11 21:49:04 +00002418 Status = ShellLibCopySearchAndReplace(mPostReplaceFormat2, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%V", L"%%V");
jcarsey975136a2009-06-16 19:03:54 +00002419 ASSERT_EFI_ERROR(Status);
2420
2421 //
2422 // Use the last buffer from replacing to print from...
2423 //
jcarseyb3011f42010-01-11 21:49:04 +00002424 Return = UnicodeVSPrint (mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), mPostReplaceFormat, Marker);
jcarseyb1f95a02009-06-16 00:23:19 +00002425
2426 if (Col != -1 && Row != -1) {
jcarseyb1f95a02009-06-16 00:23:19 +00002427 Status = gST->ConOut->SetCursorPosition(gST->ConOut, Col, Row);
2428 ASSERT_EFI_ERROR(Status);
jcarsey975136a2009-06-16 19:03:54 +00002429 }
2430
2431 NormalAttribute = gST->ConOut->Mode->Attribute;
jcarseyecd3d592009-12-07 18:05:00 +00002432 FormatWalker = mPostReplaceFormat2;
jcarsey2247dde2009-11-09 18:08:58 +00002433 while (*FormatWalker != CHAR_NULL) {
jcarsey975136a2009-06-16 19:03:54 +00002434 //
2435 // Find the next attribute change request
2436 //
2437 ResumeLocation = StrStr(FormatWalker, L"%");
2438 if (ResumeLocation != NULL) {
jcarsey2247dde2009-11-09 18:08:58 +00002439 *ResumeLocation = CHAR_NULL;
jcarsey975136a2009-06-16 19:03:54 +00002440 }
2441 //
2442 // print the current FormatWalker string
2443 //
jcarseye2f82972009-12-01 05:40:24 +00002444 Status = InternalPrintTo(FormatWalker);
jcarsey975136a2009-06-16 19:03:54 +00002445 ASSERT_EFI_ERROR(Status);
2446 //
2447 // update the attribute
2448 //
2449 if (ResumeLocation != NULL) {
2450 switch (*(ResumeLocation+1)) {
2451 case (L'N'):
2452 gST->ConOut->SetAttribute(gST->ConOut, NormalAttribute);
2453 break;
2454 case (L'E'):
2455 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_YELLOW, ((NormalAttribute&(BIT4|BIT5|BIT6))>>4)));
2456 break;
2457 case (L'H'):
2458 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_WHITE, ((NormalAttribute&(BIT4|BIT5|BIT6))>>4)));
2459 break;
2460 case (L'B'):
2461 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_BLUE, ((NormalAttribute&(BIT4|BIT5|BIT6))>>4)));
2462 break;
2463 case (L'V'):
2464 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_GREEN, ((NormalAttribute&(BIT4|BIT5|BIT6))>>4)));
2465 break;
2466 default:
jcarseye2f82972009-12-01 05:40:24 +00002467 //
2468 // Print a simple '%' symbol
2469 //
2470 Status = InternalPrintTo(L"%");
2471 ASSERT_EFI_ERROR(Status);
2472 ResumeLocation = ResumeLocation - 1;
jcarsey975136a2009-06-16 19:03:54 +00002473 break;
2474 }
2475 } else {
2476 //
2477 // reset to normal now...
2478 //
2479 gST->ConOut->SetAttribute(gST->ConOut, NormalAttribute);
2480 break;
2481 }
2482
2483 //
2484 // update FormatWalker to Resume + 2 (skip the % and the indicator)
2485 //
2486 FormatWalker = ResumeLocation + 2;
2487 }
jcarseyb1f95a02009-06-16 00:23:19 +00002488
jcarseyb1f95a02009-06-16 00:23:19 +00002489 return (Return);
jcarsey5f7431d2009-07-10 18:06:01 +00002490}
jcarsey2247dde2009-11-09 18:08:58 +00002491
2492/**
2493 Print at a specific location on the screen.
2494
jcarseye2f82972009-12-01 05:40:24 +00002495 This function will move the cursor to a given screen location and print the specified string.
jcarsey1e6e84c2010-01-25 20:05:08 +00002496
2497 If -1 is specified for either the Row or Col the current screen location for BOTH
jcarsey2247dde2009-11-09 18:08:58 +00002498 will be used.
2499
jcarseye2f82972009-12-01 05:40:24 +00002500 If either Row or Col is out of range for the current console, then ASSERT.
2501 If Format is NULL, then ASSERT.
jcarsey2247dde2009-11-09 18:08:58 +00002502
jcarsey1e6e84c2010-01-25 20:05:08 +00002503 In addition to the standard %-based flags as supported by UefiLib Print() this supports
jcarsey2247dde2009-11-09 18:08:58 +00002504 the following additional flags:
2505 %N - Set output attribute to normal
2506 %H - Set output attribute to highlight
2507 %E - Set output attribute to error
2508 %B - Set output attribute to blue color
2509 %V - Set output attribute to green color
2510
2511 Note: The background color is controlled by the shell command cls.
2512
2513 @param[in] Row the row to print at
2514 @param[in] Col the column to print at
2515 @param[in] Format the format string
2516
2517 @return the number of characters printed to the screen
2518**/
2519
2520UINTN
2521EFIAPI
2522ShellPrintEx(
2523 IN INT32 Col OPTIONAL,
2524 IN INT32 Row OPTIONAL,
2525 IN CONST CHAR16 *Format,
2526 ...
jcarsey1e6e84c2010-01-25 20:05:08 +00002527 )
jcarsey2247dde2009-11-09 18:08:58 +00002528{
2529 VA_LIST Marker;
jcarseye2f82972009-12-01 05:40:24 +00002530 EFI_STATUS Status;
jcarsey2247dde2009-11-09 18:08:58 +00002531 VA_START (Marker, Format);
jcarseye2f82972009-12-01 05:40:24 +00002532 Status = InternalShellPrintWorker(Col, Row, Format, Marker);
2533 VA_END(Marker);
2534 return(Status);
jcarsey2247dde2009-11-09 18:08:58 +00002535}
2536
2537/**
2538 Print at a specific location on the screen.
2539
jcarseye2f82972009-12-01 05:40:24 +00002540 This function will move the cursor to a given screen location and print the specified string.
jcarsey1e6e84c2010-01-25 20:05:08 +00002541
2542 If -1 is specified for either the Row or Col the current screen location for BOTH
jcarseye2f82972009-12-01 05:40:24 +00002543 will be used.
jcarsey2247dde2009-11-09 18:08:58 +00002544
jcarseye2f82972009-12-01 05:40:24 +00002545 If either Row or Col is out of range for the current console, then ASSERT.
2546 If Format is NULL, then ASSERT.
jcarsey2247dde2009-11-09 18:08:58 +00002547
jcarsey1e6e84c2010-01-25 20:05:08 +00002548 In addition to the standard %-based flags as supported by UefiLib Print() this supports
jcarsey2247dde2009-11-09 18:08:58 +00002549 the following additional flags:
jcarsey1e6e84c2010-01-25 20:05:08 +00002550 %N - Set output attribute to normal.
2551 %H - Set output attribute to highlight.
2552 %E - Set output attribute to error.
2553 %B - Set output attribute to blue color.
2554 %V - Set output attribute to green color.
jcarsey2247dde2009-11-09 18:08:58 +00002555
2556 Note: The background color is controlled by the shell command cls.
2557
jcarsey1e6e84c2010-01-25 20:05:08 +00002558 @param[in] Row The row to print at.
2559 @param[in] Col The column to print at.
2560 @param[in] Language The language of the string to retrieve. If this parameter
2561 is NULL, then the current platform language is used.
2562 @param[in] HiiFormatStringId The format string Id for getting from Hii.
2563 @param[in] HiiFormatHandle The format string Handle for getting from Hii.
jcarsey2247dde2009-11-09 18:08:58 +00002564
jcarsey1e6e84c2010-01-25 20:05:08 +00002565 @return the number of characters printed to the screen.
jcarsey2247dde2009-11-09 18:08:58 +00002566**/
2567UINTN
2568EFIAPI
2569ShellPrintHiiEx(
2570 IN INT32 Col OPTIONAL,
2571 IN INT32 Row OPTIONAL,
jcarsey1e6e84c2010-01-25 20:05:08 +00002572 IN CONST CHAR8 *Language OPTIONAL,
jcarsey2247dde2009-11-09 18:08:58 +00002573 IN CONST EFI_STRING_ID HiiFormatStringId,
2574 IN CONST EFI_HANDLE HiiFormatHandle,
2575 ...
2576 )
2577{
2578 VA_LIST Marker;
2579 CHAR16 *HiiFormatString;
2580 UINTN RetVal;
2581
2582 VA_START (Marker, HiiFormatHandle);
jcarsey1e6e84c2010-01-25 20:05:08 +00002583 HiiFormatString = HiiGetString(HiiFormatHandle, HiiFormatStringId, Language);
jcarsey2247dde2009-11-09 18:08:58 +00002584 ASSERT(HiiFormatString != NULL);
2585
2586 RetVal = InternalShellPrintWorker(Col, Row, HiiFormatString, Marker);
2587
2588 FreePool(HiiFormatString);
jcarseye2f82972009-12-01 05:40:24 +00002589 VA_END(Marker);
jcarsey2247dde2009-11-09 18:08:58 +00002590
2591 return (RetVal);
2592}
2593
2594/**
2595 Function to determine if a given filename represents a file or a directory.
2596
2597 @param[in] DirName Path to directory to test.
2598
2599 @retval EFI_SUCCESS The Path represents a directory
2600 @retval EFI_NOT_FOUND The Path does not represent a directory
2601 @return other The path failed to open
2602**/
2603EFI_STATUS
2604EFIAPI
2605ShellIsDirectory(
2606 IN CONST CHAR16 *DirName
2607 )
2608{
2609 EFI_STATUS Status;
2610 EFI_FILE_HANDLE Handle;
2611
jcarseyecd3d592009-12-07 18:05:00 +00002612 ASSERT(DirName != NULL);
2613
jcarsey2247dde2009-11-09 18:08:58 +00002614 Handle = NULL;
2615
2616 Status = ShellOpenFileByName(DirName, &Handle, EFI_FILE_MODE_READ, 0);
2617 if (EFI_ERROR(Status)) {
2618 return (Status);
2619 }
2620
2621 if (FileHandleIsDirectory(Handle) == EFI_SUCCESS) {
2622 ShellCloseFile(&Handle);
2623 return (EFI_SUCCESS);
2624 }
2625 ShellCloseFile(&Handle);
2626 return (EFI_NOT_FOUND);
2627}
2628
jcarsey125c2cf2009-11-18 21:36:50 +00002629/**
jcarsey36a9d672009-11-20 21:13:41 +00002630 Function to determine if a given filename represents a file.
2631
2632 @param[in] Name Path to file to test.
2633
2634 @retval EFI_SUCCESS The Path represents a file.
2635 @retval EFI_NOT_FOUND The Path does not represent a file.
2636 @retval other The path failed to open.
2637**/
2638EFI_STATUS
2639EFIAPI
2640ShellIsFile(
2641 IN CONST CHAR16 *Name
2642 )
2643{
2644 EFI_STATUS Status;
2645 EFI_FILE_HANDLE Handle;
2646
jcarseyecd3d592009-12-07 18:05:00 +00002647 ASSERT(Name != NULL);
2648
jcarsey36a9d672009-11-20 21:13:41 +00002649 Handle = NULL;
2650
2651 Status = ShellOpenFileByName(Name, &Handle, EFI_FILE_MODE_READ, 0);
2652 if (EFI_ERROR(Status)) {
2653 return (Status);
2654 }
2655
2656 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {
2657 ShellCloseFile(&Handle);
2658 return (EFI_SUCCESS);
2659 }
2660 ShellCloseFile(&Handle);
2661 return (EFI_NOT_FOUND);
2662}
2663
2664/**
jcarseyb3011f42010-01-11 21:49:04 +00002665 Function to determine if a given filename represents a file.
2666
2667 This will search the CWD and then the Path.
2668
2669 If Name is NULL, then ASSERT.
2670
2671 @param[in] Name Path to file to test.
2672
2673 @retval EFI_SUCCESS The Path represents a file.
2674 @retval EFI_NOT_FOUND The Path does not represent a file.
2675 @retval other The path failed to open.
2676**/
2677EFI_STATUS
2678EFIAPI
2679ShellIsFileInPath(
2680 IN CONST CHAR16 *Name
2681 ) {
2682 CHAR16 *NewName;
2683 EFI_STATUS Status;
2684
2685 if (!EFI_ERROR(ShellIsFile(Name))) {
2686 return (TRUE);
2687 }
2688
2689 NewName = ShellFindFilePath(Name);
2690 if (NewName == NULL) {
2691 return (EFI_NOT_FOUND);
2692 }
2693 Status = ShellIsFile(NewName);
2694 FreePool(NewName);
2695 return (Status);
2696}
2697/**
jcarsey1e6e84c2010-01-25 20:05:08 +00002698 Function to determine whether a string is decimal or hex representation of a number
jcarsey125c2cf2009-11-18 21:36:50 +00002699 and return the number converted from the string.
2700
2701 @param[in] String String representation of a number
2702
2703 @retval all the number
2704**/
2705UINTN
2706EFIAPI
2707ShellStrToUintn(
2708 IN CONST CHAR16 *String
2709 )
2710{
2711 CONST CHAR16 *Walker;
jcarseyb3011f42010-01-11 21:49:04 +00002712 for (Walker = String; Walker != NULL && *Walker != CHAR_NULL && *Walker == L' '; Walker++);
jcarsey1cd45e72010-01-29 15:07:44 +00002713 if (Walker == NULL || *Walker == CHAR_NULL) {
2714 ASSERT(FALSE);
2715 return ((UINTN)(-1));
2716 } else {
2717 if (StrnCmp(Walker, L"0x", 2) == 0 || StrnCmp(Walker, L"0X", 2) == 0){
2718 return (StrHexToUintn(Walker));
2719 }
2720 return (StrDecimalToUintn(Walker));
jcarsey125c2cf2009-11-18 21:36:50 +00002721 }
jcarsey125c2cf2009-11-18 21:36:50 +00002722}
2723
2724/**
2725 Safely append with automatic string resizing given length of Destination and
2726 desired length of copy from Source.
2727
2728 append the first D characters of Source to the end of Destination, where D is
2729 the lesser of Count and the StrLen() of Source. If appending those D characters
2730 will fit within Destination (whose Size is given as CurrentSize) and
jcarsey1e6e84c2010-01-25 20:05:08 +00002731 still leave room for a NULL terminator, then those characters are appended,
2732 starting at the original terminating NULL of Destination, and a new terminating
2733 NULL is appended.
jcarsey125c2cf2009-11-18 21:36:50 +00002734
2735 If appending D characters onto Destination will result in a overflow of the size
2736 given in CurrentSize the string will be grown such that the copy can be performed
2737 and CurrentSize will be updated to the new size.
2738
2739 If Source is NULL, there is nothing to append, just return the current buffer in
2740 Destination.
2741
2742 if Destination is NULL, then ASSERT()
2743 if Destination's current length (including NULL terminator) is already more then
2744 CurrentSize, then ASSERT()
2745
2746 @param[in,out] Destination The String to append onto
2747 @param[in,out] CurrentSize on call the number of bytes in Destination. On
2748 return possibly the new size (still in bytes). if NULL
2749 then allocate whatever is needed.
2750 @param[in] Source The String to append from
2751 @param[in] Count Maximum number of characters to append. if 0 then
2752 all are appended.
2753
2754 @return Destination return the resultant string.
2755**/
2756CHAR16*
2757EFIAPI
2758StrnCatGrow (
2759 IN OUT CHAR16 **Destination,
2760 IN OUT UINTN *CurrentSize,
2761 IN CONST CHAR16 *Source,
2762 IN UINTN Count
2763 )
2764{
2765 UINTN DestinationStartSize;
2766 UINTN NewSize;
2767
2768 //
2769 // ASSERTs
2770 //
2771 ASSERT(Destination != NULL);
2772
2773 //
2774 // If there's nothing to do then just return Destination
2775 //
2776 if (Source == NULL) {
2777 return (*Destination);
2778 }
2779
2780 //
2781 // allow for un-initialized pointers, based on size being 0
2782 //
2783 if (CurrentSize != NULL && *CurrentSize == 0) {
2784 *Destination = NULL;
2785 }
2786
2787 //
2788 // allow for NULL pointers address as Destination
2789 //
2790 if (*Destination != NULL) {
2791 ASSERT(CurrentSize != 0);
2792 DestinationStartSize = StrSize(*Destination);
2793 ASSERT(DestinationStartSize <= *CurrentSize);
2794 } else {
2795 DestinationStartSize = 0;
2796// ASSERT(*CurrentSize == 0);
2797 }
2798
2799 //
2800 // Append all of Source?
2801 //
2802 if (Count == 0) {
2803 Count = StrLen(Source);
2804 }
2805
2806 //
2807 // Test and grow if required
2808 //
2809 if (CurrentSize != NULL) {
2810 NewSize = *CurrentSize;
2811 while (NewSize < (DestinationStartSize + (Count*sizeof(CHAR16)))) {
2812 NewSize += 2 * Count * sizeof(CHAR16);
2813 }
2814 *Destination = ReallocatePool(*CurrentSize, NewSize, *Destination);
jcarseyc9d92df2010-02-03 15:37:54 +00002815 ASSERT(*Destination != NULL);
jcarsey125c2cf2009-11-18 21:36:50 +00002816 *CurrentSize = NewSize;
2817 } else {
2818 *Destination = AllocateZeroPool((Count+1)*sizeof(CHAR16));
jcarseyc9d92df2010-02-03 15:37:54 +00002819 ASSERT(*Destination != NULL);
jcarsey125c2cf2009-11-18 21:36:50 +00002820 }
2821
2822 //
2823 // Now use standard StrnCat on a big enough buffer
2824 //
jcarseyc9d92df2010-02-03 15:37:54 +00002825 if (*Destination == NULL) {
2826 return (NULL);
2827 }
jcarsey125c2cf2009-11-18 21:36:50 +00002828 return StrnCat(*Destination, Source, Count);
2829}
jcarseyc9d92df2010-02-03 15:37:54 +00002830
2831/**
2832 Prompt the user and return the resultant answer to the requestor.
2833
2834 This function will display the requested question on the shell prompt and then
2835 wait for an apropriate answer to be input from the console.
2836
2837 if the SHELL_PROMPT_REQUEST_TYPE is SHELL_PROMPT_REQUEST_TYPE_YESNO, SHELL_PROMPT_REQUEST_TYPE_QUIT_CONTINUE
2838 or SHELL_PROMPT_REQUEST_TYPE_YESNOCANCEL then *Response is of type SHELL_PROMPT_RESPONSE.
2839
2840 if the SHELL_PROMPT_REQUEST_TYPE is SHELL_PROMPT_REQUEST_TYPE_FREEFORM then *Response is of type
2841 CHAR16*.
2842
2843 In either case *Response must be callee freed if Response was not NULL;
2844
2845 @param Type What type of question is asked. This is used to filter the input
2846 to prevent invalid answers to question.
2847 @param Prompt Pointer to string prompt to use to request input.
2848 @param Response Pointer to Response which will be populated upon return.
2849
2850 @retval EFI_SUCCESS The operation was sucessful.
2851 @retval EFI_UNSUPPORTED The operation is not supported as requested.
2852 @retval EFI_INVALID_PARAMETER A parameter was invalid.
2853 @return other The operation failed.
2854**/
2855EFI_STATUS
2856EFIAPI
2857ShellPromptForResponse (
2858 IN SHELL_PROMPT_REQUEST_TYPE Type,
2859 IN CHAR16 *Prompt OPTIONAL,
2860 IN OUT VOID **Response OPTIONAL
2861 )
2862{
2863 EFI_STATUS Status;
2864 EFI_INPUT_KEY Key;
2865 UINTN EventIndex;
2866 SHELL_PROMPT_RESPONSE *Resp;
2867
2868 Status = EFI_SUCCESS;
2869 Resp = (SHELL_PROMPT_RESPONSE*)AllocatePool(sizeof(SHELL_PROMPT_RESPONSE));
2870
2871 switch(Type) {
2872 case SHELL_PROMPT_REQUEST_TYPE_QUIT_CONTINUE:
2873 if (Prompt != NULL) {
2874 ShellPrintEx(-1, -1, L"%s", Prompt);
2875 }
2876 //
2877 // wait for valid response
2878 //
2879 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
2880 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
2881 ASSERT_EFI_ERROR(Status);
2882 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
2883 if (Key.UnicodeChar == L'Q' || Key.UnicodeChar ==L'q') {
2884 *Resp = SHELL_PROMPT_RESPONSE_QUIT;
2885 } else {
2886 *Resp = SHELL_PROMPT_RESPONSE_CONTINUE;
2887 }
2888 break;
2889 case SHELL_PROMPT_REQUEST_TYPE_YES_NO_ALL_CANCEL:
2890 if (Prompt != NULL) {
2891 ShellPrintEx(-1, -1, L"%s", Prompt);
2892 }
2893 //
2894 // wait for valid response
2895 //
2896 *Resp = SHELL_PROMPT_RESPONSE_MAX;
2897 while (*Resp == SHELL_PROMPT_RESPONSE_MAX) {
2898 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
2899 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
2900 ASSERT_EFI_ERROR(Status);
2901 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
2902 switch (Key.UnicodeChar) {
2903 case L'Y':
2904 case L'y':
2905 *Resp = SHELL_PROMPT_RESPONSE_YES;
2906 break;
2907 case L'N':
2908 case L'n':
2909 *Resp = SHELL_PROMPT_RESPONSE_NO;
2910 break;
2911 case L'A':
2912 case L'a':
2913 *Resp = SHELL_PROMPT_RESPONSE_ALL;
2914 break;
2915 case L'C':
2916 case L'c':
2917 *Resp = SHELL_PROMPT_RESPONSE_CANCEL;
2918 break;
2919 }
2920 }
2921 break;
2922 case SHELL_PROMPT_REQUEST_TYPE_ENTER_TO_COMTINUE:
2923 case SHELL_PROMPT_REQUEST_TYPE_ANYKEY_TO_COMTINUE:
2924 if (Prompt != NULL) {
2925 ShellPrintEx(-1, -1, L"%s", Prompt);
2926 }
2927 //
2928 // wait for valid response
2929 //
2930 *Resp = SHELL_PROMPT_RESPONSE_MAX;
2931 while (*Resp == SHELL_PROMPT_RESPONSE_MAX) {
2932 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
2933 if (Type == SHELL_PROMPT_REQUEST_TYPE_ENTER_TO_COMTINUE) {
2934 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
2935 ASSERT_EFI_ERROR(Status);
2936 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
2937 if (Key.UnicodeChar == CHAR_CARRIAGE_RETURN) {
2938 *Resp = SHELL_PROMPT_RESPONSE_CONTINUE;
2939 break;
2940 }
2941 }
2942 if (Type == SHELL_PROMPT_REQUEST_TYPE_ANYKEY_TO_COMTINUE) {
2943 *Resp = SHELL_PROMPT_RESPONSE_CONTINUE;
2944 break;
2945 }
2946 }
2947 break;
2948 ///@todo add more request types here!
2949 default:
2950 Status = EFI_UNSUPPORTED;
2951 }
2952
2953 if (Response != NULL) {
2954 *Response = Resp;
2955 } else {
2956 FreePool(Resp);
2957 }
2958
2959 return (Status);
2960}
2961
2962/**
2963 Prompt the user and return the resultant answer to the requestor.
2964
2965 This function is the same as ShellPromptForResponse, except that the prompt is
2966 automatically pulled from HII.
2967
2968 @param Type What type of question is asked. This is used to filter the input
2969 to prevent invalid answers to question.
2970 @param Prompt Pointer to string prompt to use to request input.
2971 @param Response Pointer to Response which will be populated upon return.
2972
2973 @retval EFI_SUCCESS the operation was sucessful.
2974 @return other the operation failed.
2975
2976 @sa ShellPromptForResponse
2977**/
2978EFI_STATUS
2979EFIAPI
2980ShellPromptForResponseHii (
2981 IN SHELL_PROMPT_REQUEST_TYPE Type,
2982 IN CONST EFI_STRING_ID HiiFormatStringId,
2983 IN CONST EFI_HANDLE HiiFormatHandle,
2984 IN OUT VOID **Response
2985 )
2986{
2987 CHAR16 *Prompt;
2988 EFI_STATUS Status;
2989
2990 Prompt = HiiGetString(HiiFormatHandle, HiiFormatStringId, NULL);
2991 Status = ShellPromptForResponse(Type, Prompt, Response);
2992 FreePool(Prompt);
2993 return (Status);
2994}
2995
2996