blob: 1694f463789a7b11b0ed0cb5220023ff2678e72e [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"
jcarseya405b862010-09-14 05:18:09 +000016#include <ShellBase.h>
jcarseyd2b45642009-05-11 18:02:16 +000017
jcarsey94b17fa2009-05-07 18:46:18 +000018#define FIND_XXXXX_FILE_BUFFER_SIZE (SIZE_OF_EFI_FILE_INFO + MAX_FILE_NAME_LEN)
19
jcarseyd2b45642009-05-11 18:02:16 +000020//
jcarseya405b862010-09-14 05:18:09 +000021// globals...
jcarseyd2b45642009-05-11 18:02:16 +000022//
23SHELL_PARAM_ITEM EmptyParamList[] = {
24 {NULL, TypeMax}
25 };
jcarseya405b862010-09-14 05:18:09 +000026SHELL_PARAM_ITEM SfoParamList[] = {
27 {L"-sfo", TypeFlag},
28 {NULL, TypeMax}
29 };
30EFI_SHELL_ENVIRONMENT2 *mEfiShellEnvironment2;
31EFI_SHELL_INTERFACE *mEfiShellInterface;
32EFI_SHELL_PROTOCOL *mEfiShellProtocol;
33EFI_SHELL_PARAMETERS_PROTOCOL *mEfiShellParametersProtocol;
34EFI_HANDLE mEfiShellEnvironment2Handle;
35FILE_HANDLE_FUNCTION_MAP FileFunctionMap;
36CHAR16 *mPostReplaceFormat;
37CHAR16 *mPostReplaceFormat2;
jcarseyb3011f42010-01-11 21:49:04 +000038
jcarsey2247dde2009-11-09 18:08:58 +000039/**
40 Check if a Unicode character is a hexadecimal character.
41
jcarsey1e6e84c2010-01-25 20:05:08 +000042 This internal function checks if a Unicode character is a
jcarseya405b862010-09-14 05:18:09 +000043 numeric character. The valid hexadecimal characters are
jcarsey2247dde2009-11-09 18:08:58 +000044 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.
45
jcarsey2247dde2009-11-09 18:08:58 +000046 @param Char The character to check against.
47
48 @retval TRUE If the Char is a hexadecmial character.
49 @retval FALSE If the Char is not a hexadecmial character.
50
51**/
52BOOLEAN
53EFIAPI
jcarsey969c7832010-01-13 16:46:33 +000054ShellIsHexaDecimalDigitCharacter (
jcarsey2247dde2009-11-09 18:08:58 +000055 IN CHAR16 Char
jcarseya405b862010-09-14 05:18:09 +000056 )
57{
jcarsey2247dde2009-11-09 18:08:58 +000058 return (BOOLEAN) ((Char >= L'0' && Char <= L'9') || (Char >= L'A' && Char <= L'F') || (Char >= L'a' && Char <= L'f'));
59}
jcarsey94b17fa2009-05-07 18:46:18 +000060
61/**
jcarseya405b862010-09-14 05:18:09 +000062 Check if a Unicode character is a decimal character.
63
64 This internal function checks if a Unicode character is a
65 decimal character. The valid characters are
66 L'0' to L'9'.
67
68
69 @param Char The character to check against.
70
71 @retval TRUE If the Char is a hexadecmial character.
72 @retval FALSE If the Char is not a hexadecmial character.
73
74**/
75BOOLEAN
76EFIAPI
77ShellIsDecimalDigitCharacter (
78 IN CHAR16 Char
79 )
80{
81 return (BOOLEAN) (Char >= L'0' && Char <= L'9');
82}
83
84/**
85 Helper function to find ShellEnvironment2 for constructor.
86
87 @param[in] ImageHandle A copy of the calling image's handle.
jcarsey94b17fa2009-05-07 18:46:18 +000088**/
89EFI_STATUS
90EFIAPI
91ShellFindSE2 (
92 IN EFI_HANDLE ImageHandle
jcarseya405b862010-09-14 05:18:09 +000093 )
94{
jcarsey94b17fa2009-05-07 18:46:18 +000095 EFI_STATUS Status;
96 EFI_HANDLE *Buffer;
97 UINTN BufferSize;
98 UINTN HandleIndex;
99
100 BufferSize = 0;
101 Buffer = NULL;
jcarsey1e6e84c2010-01-25 20:05:08 +0000102 Status = gBS->OpenProtocol(ImageHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000103 &gEfiShellEnvironment2Guid,
104 (VOID **)&mEfiShellEnvironment2,
105 ImageHandle,
106 NULL,
107 EFI_OPEN_PROTOCOL_GET_PROTOCOL
jcarseya405b862010-09-14 05:18:09 +0000108 );
jcarsey94b17fa2009-05-07 18:46:18 +0000109 //
110 // look for the mEfiShellEnvironment2 protocol at a higher level
111 //
jcarseya405b862010-09-14 05:18:09 +0000112 if (EFI_ERROR (Status) || !(CompareGuid (&mEfiShellEnvironment2->SESGuid, &gEfiShellEnvironment2ExtGuid))){
jcarsey94b17fa2009-05-07 18:46:18 +0000113 //
114 // figure out how big of a buffer we need.
115 //
116 Status = gBS->LocateHandle (ByProtocol,
117 &gEfiShellEnvironment2Guid,
118 NULL, // ignored for ByProtocol
119 &BufferSize,
120 Buffer
jcarseya405b862010-09-14 05:18:09 +0000121 );
jcarsey2247dde2009-11-09 18:08:58 +0000122 //
123 // maybe it's not there???
124 //
125 if (Status == EFI_BUFFER_TOO_SMALL) {
126 Buffer = (EFI_HANDLE*)AllocatePool(BufferSize);
127 ASSERT(Buffer != NULL);
128 Status = gBS->LocateHandle (ByProtocol,
129 &gEfiShellEnvironment2Guid,
130 NULL, // ignored for ByProtocol
131 &BufferSize,
132 Buffer
jcarseya405b862010-09-14 05:18:09 +0000133 );
jcarsey2247dde2009-11-09 18:08:58 +0000134 }
jcarsey1cd45e72010-01-29 15:07:44 +0000135 if (!EFI_ERROR (Status) && Buffer != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +0000136 //
137 // now parse the list of returned handles
138 //
139 Status = EFI_NOT_FOUND;
140 for (HandleIndex = 0; HandleIndex < (BufferSize/sizeof(Buffer[0])); HandleIndex++) {
jcarsey1e6e84c2010-01-25 20:05:08 +0000141 Status = gBS->OpenProtocol(Buffer[HandleIndex],
jcarsey94b17fa2009-05-07 18:46:18 +0000142 &gEfiShellEnvironment2Guid,
143 (VOID **)&mEfiShellEnvironment2,
144 ImageHandle,
145 NULL,
146 EFI_OPEN_PROTOCOL_GET_PROTOCOL
jcarseya405b862010-09-14 05:18:09 +0000147 );
148 if (CompareGuid (&mEfiShellEnvironment2->SESGuid, &gEfiShellEnvironment2ExtGuid)) {
jcarsey94b17fa2009-05-07 18:46:18 +0000149 mEfiShellEnvironment2Handle = Buffer[HandleIndex];
150 Status = EFI_SUCCESS;
151 break;
152 }
153 }
154 }
155 }
156 if (Buffer != NULL) {
157 FreePool (Buffer);
158 }
159 return (Status);
160}
161
jcarseya405b862010-09-14 05:18:09 +0000162/*/
163 Function to do most of the work of the constructor. Allows for calling
164 multiple times without complete re-initialization.
165
166 @param[in] ImageHandle A copy of the ImageHandle.
167 @param[in] SystemTable A pointer to the SystemTable for the application.
168**/
jcarsey94b17fa2009-05-07 18:46:18 +0000169EFI_STATUS
170EFIAPI
jcarseyd2b45642009-05-11 18:02:16 +0000171ShellLibConstructorWorker (
jcarsey94b17fa2009-05-07 18:46:18 +0000172 IN EFI_HANDLE ImageHandle,
173 IN EFI_SYSTEM_TABLE *SystemTable
jcarseya405b862010-09-14 05:18:09 +0000174 )
175{
176 EFI_STATUS Status;
jcarseyb3011f42010-01-11 21:49:04 +0000177 mPostReplaceFormat = AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize));
jcarseyecd3d592009-12-07 18:05:00 +0000178 ASSERT (mPostReplaceFormat != NULL);
jcarseyb3011f42010-01-11 21:49:04 +0000179 mPostReplaceFormat2 = AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize));
jcarseyecd3d592009-12-07 18:05:00 +0000180 ASSERT (mPostReplaceFormat2 != NULL);
181
jcarsey94b17fa2009-05-07 18:46:18 +0000182 //
183 // UEFI 2.0 shell interfaces (used preferentially)
184 //
jcarseya405b862010-09-14 05:18:09 +0000185 Status = gBS->OpenProtocol(
186 ImageHandle,
187 &gEfiShellProtocolGuid,
188 (VOID **)&mEfiShellProtocol,
189 ImageHandle,
190 NULL,
191 EFI_OPEN_PROTOCOL_GET_PROTOCOL
192 );
jcarsey94b17fa2009-05-07 18:46:18 +0000193 if (EFI_ERROR(Status)) {
jcarseya405b862010-09-14 05:18:09 +0000194 //
195 // Search for the shell protocol
196 //
197 Status = gBS->LocateProtocol(
198 &gEfiShellProtocolGuid,
199 NULL,
200 (VOID **)&mEfiShellProtocol
201 );
202 if (EFI_ERROR(Status)) {
203 mEfiShellProtocol = NULL;
204 }
jcarsey94b17fa2009-05-07 18:46:18 +0000205 }
jcarseya405b862010-09-14 05:18:09 +0000206 Status = gBS->OpenProtocol(
207 ImageHandle,
208 &gEfiShellParametersProtocolGuid,
209 (VOID **)&mEfiShellParametersProtocol,
210 ImageHandle,
211 NULL,
212 EFI_OPEN_PROTOCOL_GET_PROTOCOL
213 );
jcarsey94b17fa2009-05-07 18:46:18 +0000214 if (EFI_ERROR(Status)) {
215 mEfiShellParametersProtocol = NULL;
216 }
217
218 if (mEfiShellParametersProtocol == NULL || mEfiShellProtocol == NULL) {
219 //
220 // Moved to seperate function due to complexity
221 //
222 Status = ShellFindSE2(ImageHandle);
223
224 if (EFI_ERROR(Status)) {
225 DEBUG((DEBUG_ERROR, "Status: 0x%08x\r\n", Status));
226 mEfiShellEnvironment2 = NULL;
227 }
jcarsey1e6e84c2010-01-25 20:05:08 +0000228 Status = gBS->OpenProtocol(ImageHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000229 &gEfiShellInterfaceGuid,
230 (VOID **)&mEfiShellInterface,
231 ImageHandle,
232 NULL,
233 EFI_OPEN_PROTOCOL_GET_PROTOCOL
jcarseya405b862010-09-14 05:18:09 +0000234 );
jcarsey94b17fa2009-05-07 18:46:18 +0000235 if (EFI_ERROR(Status)) {
236 mEfiShellInterface = NULL;
237 }
238 }
jcarseyc9d92df2010-02-03 15:37:54 +0000239
jcarsey94b17fa2009-05-07 18:46:18 +0000240 //
241 // only success getting 2 of either the old or new, but no 1/2 and 1/2
242 //
jcarsey1e6e84c2010-01-25 20:05:08 +0000243 if ((mEfiShellEnvironment2 != NULL && mEfiShellInterface != NULL) ||
jcarseya405b862010-09-14 05:18:09 +0000244 (mEfiShellProtocol != NULL && mEfiShellParametersProtocol != NULL) ) {
jcarseyd2b45642009-05-11 18:02:16 +0000245 if (mEfiShellProtocol != NULL) {
246 FileFunctionMap.GetFileInfo = mEfiShellProtocol->GetFileInfo;
247 FileFunctionMap.SetFileInfo = mEfiShellProtocol->SetFileInfo;
248 FileFunctionMap.ReadFile = mEfiShellProtocol->ReadFile;
249 FileFunctionMap.WriteFile = mEfiShellProtocol->WriteFile;
250 FileFunctionMap.CloseFile = mEfiShellProtocol->CloseFile;
251 FileFunctionMap.DeleteFile = mEfiShellProtocol->DeleteFile;
252 FileFunctionMap.GetFilePosition = mEfiShellProtocol->GetFilePosition;
253 FileFunctionMap.SetFilePosition = mEfiShellProtocol->SetFilePosition;
254 FileFunctionMap.FlushFile = mEfiShellProtocol->FlushFile;
255 FileFunctionMap.GetFileSize = mEfiShellProtocol->GetFileSize;
256 } else {
jcarseya405b862010-09-14 05:18:09 +0000257 FileFunctionMap.GetFileInfo = (EFI_SHELL_GET_FILE_INFO)FileHandleGetInfo;
258 FileFunctionMap.SetFileInfo = (EFI_SHELL_SET_FILE_INFO)FileHandleSetInfo;
259 FileFunctionMap.ReadFile = (EFI_SHELL_READ_FILE)FileHandleRead;
260 FileFunctionMap.WriteFile = (EFI_SHELL_WRITE_FILE)FileHandleWrite;
261 FileFunctionMap.CloseFile = (EFI_SHELL_CLOSE_FILE)FileHandleClose;
262 FileFunctionMap.DeleteFile = (EFI_SHELL_DELETE_FILE)FileHandleDelete;
263 FileFunctionMap.GetFilePosition = (EFI_SHELL_GET_FILE_POSITION)FileHandleGetPosition;
264 FileFunctionMap.SetFilePosition = (EFI_SHELL_SET_FILE_POSITION)FileHandleSetPosition;
265 FileFunctionMap.FlushFile = (EFI_SHELL_FLUSH_FILE)FileHandleFlush;
266 FileFunctionMap.GetFileSize = (EFI_SHELL_GET_FILE_SIZE)FileHandleGetSize;
jcarseyd2b45642009-05-11 18:02:16 +0000267 }
jcarsey94b17fa2009-05-07 18:46:18 +0000268 return (EFI_SUCCESS);
269 }
270 return (EFI_NOT_FOUND);
271}
jcarseyd2b45642009-05-11 18:02:16 +0000272/**
273 Constructor for the Shell library.
274
275 Initialize the library and determine if the underlying is a UEFI Shell 2.0 or an EFI shell.
276
277 @param ImageHandle the image handle of the process
278 @param SystemTable the EFI System Table pointer
279
280 @retval EFI_SUCCESS the initialization was complete sucessfully
281 @return others an error ocurred during initialization
282**/
283EFI_STATUS
284EFIAPI
285ShellLibConstructor (
286 IN EFI_HANDLE ImageHandle,
287 IN EFI_SYSTEM_TABLE *SystemTable
jcarseya405b862010-09-14 05:18:09 +0000288 )
289{
jcarseyd2b45642009-05-11 18:02:16 +0000290 mEfiShellEnvironment2 = NULL;
291 mEfiShellProtocol = NULL;
292 mEfiShellParametersProtocol = NULL;
293 mEfiShellInterface = NULL;
294 mEfiShellEnvironment2Handle = NULL;
jcarseyecd3d592009-12-07 18:05:00 +0000295 mPostReplaceFormat = NULL;
296 mPostReplaceFormat2 = NULL;
jcarseyd2b45642009-05-11 18:02:16 +0000297
jcarseyd2b45642009-05-11 18:02:16 +0000298 //
299 // verify that auto initialize is not set false
jcarsey1e6e84c2010-01-25 20:05:08 +0000300 //
jcarseyd2b45642009-05-11 18:02:16 +0000301 if (PcdGetBool(PcdShellLibAutoInitialize) == 0) {
302 return (EFI_SUCCESS);
303 }
jcarsey1e6e84c2010-01-25 20:05:08 +0000304
jcarseyd2b45642009-05-11 18:02:16 +0000305 return (ShellLibConstructorWorker(ImageHandle, SystemTable));
306}
jcarsey94b17fa2009-05-07 18:46:18 +0000307
308/**
jcarseya405b862010-09-14 05:18:09 +0000309 Destructor for the library. free any resources.
310
311 @param[in] ImageHandle A copy of the ImageHandle.
312 @param[in] SystemTable A pointer to the SystemTable for the application.
313
314 @retval EFI_SUCCESS The operation was successful.
315 @return An error from the CloseProtocol function.
jcarsey94b17fa2009-05-07 18:46:18 +0000316**/
317EFI_STATUS
318EFIAPI
319ShellLibDestructor (
320 IN EFI_HANDLE ImageHandle,
321 IN EFI_SYSTEM_TABLE *SystemTable
jcarseya405b862010-09-14 05:18:09 +0000322 )
323{
jcarsey94b17fa2009-05-07 18:46:18 +0000324 if (mEfiShellEnvironment2 != NULL) {
325 gBS->CloseProtocol(mEfiShellEnvironment2Handle==NULL?ImageHandle:mEfiShellEnvironment2Handle,
326 &gEfiShellEnvironment2Guid,
327 ImageHandle,
328 NULL);
jcarseyd2b45642009-05-11 18:02:16 +0000329 mEfiShellEnvironment2 = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +0000330 }
331 if (mEfiShellInterface != NULL) {
332 gBS->CloseProtocol(ImageHandle,
333 &gEfiShellInterfaceGuid,
334 ImageHandle,
jcarsey1e6e84c2010-01-25 20:05:08 +0000335 NULL);
jcarseyd2b45642009-05-11 18:02:16 +0000336 mEfiShellInterface = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +0000337 }
338 if (mEfiShellProtocol != NULL) {
339 gBS->CloseProtocol(ImageHandle,
340 &gEfiShellProtocolGuid,
341 ImageHandle,
jcarsey1e6e84c2010-01-25 20:05:08 +0000342 NULL);
jcarseyd2b45642009-05-11 18:02:16 +0000343 mEfiShellProtocol = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +0000344 }
345 if (mEfiShellParametersProtocol != NULL) {
346 gBS->CloseProtocol(ImageHandle,
347 &gEfiShellParametersProtocolGuid,
348 ImageHandle,
jcarsey1e6e84c2010-01-25 20:05:08 +0000349 NULL);
jcarseyd2b45642009-05-11 18:02:16 +0000350 mEfiShellParametersProtocol = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +0000351 }
jcarseyd2b45642009-05-11 18:02:16 +0000352 mEfiShellEnvironment2Handle = NULL;
jcarseyecd3d592009-12-07 18:05:00 +0000353
354 if (mPostReplaceFormat != NULL) {
355 FreePool(mPostReplaceFormat);
356 }
357 if (mPostReplaceFormat2 != NULL) {
358 FreePool(mPostReplaceFormat2);
359 }
360 mPostReplaceFormat = NULL;
361 mPostReplaceFormat2 = NULL;
362
jcarsey94b17fa2009-05-07 18:46:18 +0000363 return (EFI_SUCCESS);
364}
jcarseyd2b45642009-05-11 18:02:16 +0000365
366/**
367 This function causes the shell library to initialize itself. If the shell library
368 is already initialized it will de-initialize all the current protocol poitners and
369 re-populate them again.
370
371 When the library is used with PcdShellLibAutoInitialize set to true this function
372 will return EFI_SUCCESS and perform no actions.
373
374 This function is intended for internal access for shell commands only.
375
376 @retval EFI_SUCCESS the initialization was complete sucessfully
377
378**/
379EFI_STATUS
380EFIAPI
381ShellInitialize (
jcarseya405b862010-09-14 05:18:09 +0000382 )
383{
jcarseyd2b45642009-05-11 18:02:16 +0000384 //
385 // if auto initialize is not false then skip
386 //
387 if (PcdGetBool(PcdShellLibAutoInitialize) != 0) {
388 return (EFI_SUCCESS);
389 }
390
391 //
392 // deinit the current stuff
393 //
394 ASSERT_EFI_ERROR(ShellLibDestructor(gImageHandle, gST));
395
396 //
397 // init the new stuff
398 //
399 return (ShellLibConstructorWorker(gImageHandle, gST));
400}
401
jcarsey94b17fa2009-05-07 18:46:18 +0000402/**
jcarsey1e6e84c2010-01-25 20:05:08 +0000403 This function will retrieve the information about the file for the handle
jcarsey94b17fa2009-05-07 18:46:18 +0000404 specified and store it in allocated pool memory.
405
jcarsey1e6e84c2010-01-25 20:05:08 +0000406 This function allocates a buffer to store the file's information. It is the
qhuang869817bf2009-05-20 14:42:48 +0000407 caller's responsibility to free the buffer
jcarsey94b17fa2009-05-07 18:46:18 +0000408
jcarsey1e6e84c2010-01-25 20:05:08 +0000409 @param FileHandle The file handle of the file for which information is
jcarsey94b17fa2009-05-07 18:46:18 +0000410 being requested.
411
412 @retval NULL information could not be retrieved.
413
414 @return the information about the file
415**/
416EFI_FILE_INFO*
417EFIAPI
418ShellGetFileInfo (
jcarseya405b862010-09-14 05:18:09 +0000419 IN SHELL_FILE_HANDLE FileHandle
420 )
421{
jcarseyd2b45642009-05-11 18:02:16 +0000422 return (FileFunctionMap.GetFileInfo(FileHandle));
jcarsey94b17fa2009-05-07 18:46:18 +0000423}
424
425/**
jcarseya405b862010-09-14 05:18:09 +0000426 This function sets the information about the file for the opened handle
jcarsey94b17fa2009-05-07 18:46:18 +0000427 specified.
428
jcarseya405b862010-09-14 05:18:09 +0000429 @param[in] FileHandle The file handle of the file for which information
430 is being set.
jcarsey94b17fa2009-05-07 18:46:18 +0000431
jcarseya405b862010-09-14 05:18:09 +0000432 @param[in] FileInfo The information to set.
jcarsey94b17fa2009-05-07 18:46:18 +0000433
jcarseya405b862010-09-14 05:18:09 +0000434 @retval EFI_SUCCESS The information was set.
435 @retval EFI_INVALID_PARAMETER A parameter was out of range or invalid.
436 @retval EFI_UNSUPPORTED The FileHandle does not support FileInfo.
437 @retval EFI_NO_MEDIA The device has no medium.
438 @retval EFI_DEVICE_ERROR The device reported an error.
jcarsey94b17fa2009-05-07 18:46:18 +0000439 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
jcarseya405b862010-09-14 05:18:09 +0000440 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
441 @retval EFI_ACCESS_DENIED The file was opened read only.
442 @retval EFI_VOLUME_FULL The volume is full.
jcarsey94b17fa2009-05-07 18:46:18 +0000443**/
444EFI_STATUS
445EFIAPI
446ShellSetFileInfo (
jcarseya405b862010-09-14 05:18:09 +0000447 IN SHELL_FILE_HANDLE FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000448 IN EFI_FILE_INFO *FileInfo
jcarseya405b862010-09-14 05:18:09 +0000449 )
450{
jcarseyd2b45642009-05-11 18:02:16 +0000451 return (FileFunctionMap.SetFileInfo(FileHandle, FileInfo));
jcarsey1e6e84c2010-01-25 20:05:08 +0000452}
453
jcarsey94b17fa2009-05-07 18:46:18 +0000454 /**
455 This function will open a file or directory referenced by DevicePath.
456
jcarsey1e6e84c2010-01-25 20:05:08 +0000457 This function opens a file with the open mode according to the file path. The
jcarsey94b17fa2009-05-07 18:46:18 +0000458 Attributes is valid only for EFI_FILE_MODE_CREATE.
459
jcarsey1e6e84c2010-01-25 20:05:08 +0000460 @param FilePath on input the device path to the file. On output
jcarsey94b17fa2009-05-07 18:46:18 +0000461 the remaining device path.
462 @param DeviceHandle pointer to the system device handle.
463 @param FileHandle pointer to the file handle.
464 @param OpenMode the mode to open the file with.
465 @param Attributes the file's file attributes.
466
467 @retval EFI_SUCCESS The information was set.
468 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
jcarsey1e6e84c2010-01-25 20:05:08 +0000469 @retval EFI_UNSUPPORTED Could not open the file path.
470 @retval EFI_NOT_FOUND The specified file could not be found on the
471 device or the file system could not be found on
jcarsey94b17fa2009-05-07 18:46:18 +0000472 the device.
473 @retval EFI_NO_MEDIA The device has no medium.
jcarsey1e6e84c2010-01-25 20:05:08 +0000474 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
jcarsey94b17fa2009-05-07 18:46:18 +0000475 medium is no longer supported.
476 @retval EFI_DEVICE_ERROR The device reported an error.
477 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
478 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
jcarseya405b862010-09-14 05:18:09 +0000479 @retval EFI_ACCESS_DENIED The file was opened read only.
jcarsey1e6e84c2010-01-25 20:05:08 +0000480 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
jcarsey94b17fa2009-05-07 18:46:18 +0000481 file.
482 @retval EFI_VOLUME_FULL The volume is full.
483**/
484EFI_STATUS
485EFIAPI
486ShellOpenFileByDevicePath(
487 IN OUT EFI_DEVICE_PATH_PROTOCOL **FilePath,
488 OUT EFI_HANDLE *DeviceHandle,
jcarseya405b862010-09-14 05:18:09 +0000489 OUT SHELL_FILE_HANDLE *FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000490 IN UINT64 OpenMode,
491 IN UINT64 Attributes
jcarseya405b862010-09-14 05:18:09 +0000492 )
493{
494 CHAR16 *FileName;
495 EFI_STATUS Status;
jcarsey94b17fa2009-05-07 18:46:18 +0000496 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *EfiSimpleFileSystemProtocol;
jcarseya405b862010-09-14 05:18:09 +0000497 EFI_FILE_PROTOCOL *Handle1;
498 EFI_FILE_PROTOCOL *Handle2;
jcarsey94b17fa2009-05-07 18:46:18 +0000499
500 //
501 // ASERT for FileHandle, FilePath, and DeviceHandle being NULL
502 //
503 ASSERT(FilePath != NULL);
504 ASSERT(FileHandle != NULL);
505 ASSERT(DeviceHandle != NULL);
jcarsey1e6e84c2010-01-25 20:05:08 +0000506 //
jcarsey94b17fa2009-05-07 18:46:18 +0000507 // which shell interface should we use
508 //
509 if (mEfiShellProtocol != NULL) {
510 //
511 // use UEFI Shell 2.0 method.
512 //
513 FileName = mEfiShellProtocol->GetFilePathFromDevicePath(*FilePath);
514 if (FileName == NULL) {
515 return (EFI_INVALID_PARAMETER);
516 }
517 Status = ShellOpenFileByName(FileName, FileHandle, OpenMode, Attributes);
518 FreePool(FileName);
519 return (Status);
jcarsey1e6e84c2010-01-25 20:05:08 +0000520 }
jcarseyd2b45642009-05-11 18:02:16 +0000521
522
523 //
524 // use old shell method.
525 //
jcarsey1e6e84c2010-01-25 20:05:08 +0000526 Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid,
527 FilePath,
jcarseyd2b45642009-05-11 18:02:16 +0000528 DeviceHandle);
529 if (EFI_ERROR (Status)) {
530 return Status;
531 }
532 Status = gBS->OpenProtocol(*DeviceHandle,
533 &gEfiSimpleFileSystemProtocolGuid,
jcarseyb1f95a02009-06-16 00:23:19 +0000534 (VOID**)&EfiSimpleFileSystemProtocol,
jcarseyd2b45642009-05-11 18:02:16 +0000535 gImageHandle,
536 NULL,
537 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
538 if (EFI_ERROR (Status)) {
539 return Status;
540 }
jcarseya405b862010-09-14 05:18:09 +0000541 Status = EfiSimpleFileSystemProtocol->OpenVolume(EfiSimpleFileSystemProtocol, &Handle1);
jcarseyd2b45642009-05-11 18:02:16 +0000542 if (EFI_ERROR (Status)) {
543 FileHandle = NULL;
544 return Status;
545 }
546
547 //
548 // go down directories one node at a time.
549 //
550 while (!IsDevicePathEnd (*FilePath)) {
jcarsey94b17fa2009-05-07 18:46:18 +0000551 //
jcarseyd2b45642009-05-11 18:02:16 +0000552 // For file system access each node should be a file path component
jcarsey94b17fa2009-05-07 18:46:18 +0000553 //
jcarseyd2b45642009-05-11 18:02:16 +0000554 if (DevicePathType (*FilePath) != MEDIA_DEVICE_PATH ||
555 DevicePathSubType (*FilePath) != MEDIA_FILEPATH_DP
jcarseya405b862010-09-14 05:18:09 +0000556 ) {
jcarsey94b17fa2009-05-07 18:46:18 +0000557 FileHandle = NULL;
jcarseyd2b45642009-05-11 18:02:16 +0000558 return (EFI_INVALID_PARAMETER);
jcarsey94b17fa2009-05-07 18:46:18 +0000559 }
jcarseyd2b45642009-05-11 18:02:16 +0000560 //
561 // Open this file path node
562 //
jcarseya405b862010-09-14 05:18:09 +0000563 Handle2 = Handle1;
564 Handle1 = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +0000565
566 //
jcarseyd2b45642009-05-11 18:02:16 +0000567 // Try to test opening an existing file
jcarsey94b17fa2009-05-07 18:46:18 +0000568 //
jcarseya405b862010-09-14 05:18:09 +0000569 Status = Handle2->Open (
570 Handle2,
571 &Handle1,
jcarseyd2b45642009-05-11 18:02:16 +0000572 ((FILEPATH_DEVICE_PATH*)*FilePath)->PathName,
573 OpenMode &~EFI_FILE_MODE_CREATE,
574 0
jcarseya405b862010-09-14 05:18:09 +0000575 );
jcarsey94b17fa2009-05-07 18:46:18 +0000576
jcarseyd2b45642009-05-11 18:02:16 +0000577 //
578 // see if the error was that it needs to be created
579 //
580 if ((EFI_ERROR (Status)) && (OpenMode != (OpenMode &~EFI_FILE_MODE_CREATE))) {
jcarseya405b862010-09-14 05:18:09 +0000581 Status = Handle2->Open (
582 Handle2,
583 &Handle1,
jcarsey94b17fa2009-05-07 18:46:18 +0000584 ((FILEPATH_DEVICE_PATH*)*FilePath)->PathName,
jcarseyd2b45642009-05-11 18:02:16 +0000585 OpenMode,
586 Attributes
jcarseya405b862010-09-14 05:18:09 +0000587 );
jcarsey94b17fa2009-05-07 18:46:18 +0000588 }
jcarseyd2b45642009-05-11 18:02:16 +0000589 //
590 // Close the last node
591 //
jcarseya405b862010-09-14 05:18:09 +0000592 Handle2->Close (Handle2);
jcarseyd2b45642009-05-11 18:02:16 +0000593
594 if (EFI_ERROR(Status)) {
595 return (Status);
596 }
597
598 //
599 // Get the next node
600 //
601 *FilePath = NextDevicePathNode (*FilePath);
jcarsey94b17fa2009-05-07 18:46:18 +0000602 }
jcarseya405b862010-09-14 05:18:09 +0000603
604 //
605 // This is a weak spot since if the undefined SHELL_FILE_HANDLE format changes this must change also!
606 //
607 *FileHandle = (VOID*)Handle1;
jcarseyd2b45642009-05-11 18:02:16 +0000608 return (EFI_SUCCESS);
jcarsey94b17fa2009-05-07 18:46:18 +0000609}
610
611/**
612 This function will open a file or directory referenced by filename.
613
jcarsey1e6e84c2010-01-25 20:05:08 +0000614 If return is EFI_SUCCESS, the Filehandle is the opened file's handle;
615 otherwise, the Filehandle is NULL. The Attributes is valid only for
jcarsey94b17fa2009-05-07 18:46:18 +0000616 EFI_FILE_MODE_CREATE.
617
618 if FileNAme is NULL then ASSERT()
619
620 @param FileName pointer to file name
621 @param FileHandle pointer to the file handle.
622 @param OpenMode the mode to open the file with.
623 @param Attributes the file's file attributes.
624
625 @retval EFI_SUCCESS The information was set.
626 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
jcarsey1e6e84c2010-01-25 20:05:08 +0000627 @retval EFI_UNSUPPORTED Could not open the file path.
628 @retval EFI_NOT_FOUND The specified file could not be found on the
629 device or the file system could not be found
jcarsey94b17fa2009-05-07 18:46:18 +0000630 on the device.
631 @retval EFI_NO_MEDIA The device has no medium.
jcarsey1e6e84c2010-01-25 20:05:08 +0000632 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
jcarsey94b17fa2009-05-07 18:46:18 +0000633 medium is no longer supported.
634 @retval EFI_DEVICE_ERROR The device reported an error.
635 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
636 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
637 @retval EFI_ACCESS_DENIED The file was opened read only.
jcarsey1e6e84c2010-01-25 20:05:08 +0000638 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
jcarsey94b17fa2009-05-07 18:46:18 +0000639 file.
640 @retval EFI_VOLUME_FULL The volume is full.
641**/
642EFI_STATUS
643EFIAPI
644ShellOpenFileByName(
jcarseyb82bfcc2009-06-29 16:28:23 +0000645 IN CONST CHAR16 *FileName,
jcarseya405b862010-09-14 05:18:09 +0000646 OUT SHELL_FILE_HANDLE *FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000647 IN UINT64 OpenMode,
648 IN UINT64 Attributes
jcarseya405b862010-09-14 05:18:09 +0000649 )
650{
jcarsey94b17fa2009-05-07 18:46:18 +0000651 EFI_HANDLE DeviceHandle;
652 EFI_DEVICE_PATH_PROTOCOL *FilePath;
jcarseyb1f95a02009-06-16 00:23:19 +0000653 EFI_STATUS Status;
654 EFI_FILE_INFO *FileInfo;
jcarsey94b17fa2009-05-07 18:46:18 +0000655
656 //
657 // ASSERT if FileName is NULL
658 //
659 ASSERT(FileName != NULL);
660
jcarseya405b862010-09-14 05:18:09 +0000661 if (FileName == NULL) {
662 return (EFI_INVALID_PARAMETER);
663 }
664
jcarsey94b17fa2009-05-07 18:46:18 +0000665 if (mEfiShellProtocol != NULL) {
jcarseya405b862010-09-14 05:18:09 +0000666 if ((OpenMode & EFI_FILE_MODE_CREATE) == EFI_FILE_MODE_CREATE && (Attributes & EFI_FILE_DIRECTORY) == EFI_FILE_DIRECTORY) {
667 return ShellCreateDirectory(FileName, FileHandle);
668 }
jcarsey94b17fa2009-05-07 18:46:18 +0000669 //
670 // Use UEFI Shell 2.0 method
671 //
jcarseyb1f95a02009-06-16 00:23:19 +0000672 Status = mEfiShellProtocol->OpenFileByName(FileName,
673 FileHandle,
674 OpenMode);
jcarseya405b862010-09-14 05:18:09 +0000675 if (StrCmp(FileName, L"NUL") != 0 && !EFI_ERROR(Status) && ((OpenMode & EFI_FILE_MODE_CREATE) != 0)){
jcarsey2247dde2009-11-09 18:08:58 +0000676 FileInfo = FileFunctionMap.GetFileInfo(*FileHandle);
jcarseyb1f95a02009-06-16 00:23:19 +0000677 ASSERT(FileInfo != NULL);
678 FileInfo->Attribute = Attributes;
jcarsey2247dde2009-11-09 18:08:58 +0000679 Status = FileFunctionMap.SetFileInfo(*FileHandle, FileInfo);
680 FreePool(FileInfo);
jcarseyb1f95a02009-06-16 00:23:19 +0000681 }
682 return (Status);
jcarsey1e6e84c2010-01-25 20:05:08 +0000683 }
jcarsey94b17fa2009-05-07 18:46:18 +0000684 //
685 // Using EFI Shell version
686 // this means convert name to path and call that function
687 // since this will use EFI method again that will open it.
688 //
689 ASSERT(mEfiShellEnvironment2 != NULL);
jcarseyb82bfcc2009-06-29 16:28:23 +0000690 FilePath = mEfiShellEnvironment2->NameToPath ((CHAR16*)FileName);
xdu290bfa222010-07-19 05:21:27 +0000691 if (FilePath != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +0000692 return (ShellOpenFileByDevicePath(&FilePath,
693 &DeviceHandle,
694 FileHandle,
695 OpenMode,
jcarseya405b862010-09-14 05:18:09 +0000696 Attributes));
jcarsey94b17fa2009-05-07 18:46:18 +0000697 }
698 return (EFI_DEVICE_ERROR);
699}
700/**
701 This function create a directory
702
jcarsey1e6e84c2010-01-25 20:05:08 +0000703 If return is EFI_SUCCESS, the Filehandle is the opened directory's handle;
704 otherwise, the Filehandle is NULL. If the directory already existed, this
jcarsey94b17fa2009-05-07 18:46:18 +0000705 function opens the existing directory.
706
707 @param DirectoryName pointer to directory name
708 @param FileHandle pointer to the file handle.
709
710 @retval EFI_SUCCESS The information was set.
711 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
jcarsey1e6e84c2010-01-25 20:05:08 +0000712 @retval EFI_UNSUPPORTED Could not open the file path.
713 @retval EFI_NOT_FOUND The specified file could not be found on the
714 device or the file system could not be found
jcarsey94b17fa2009-05-07 18:46:18 +0000715 on the device.
716 @retval EFI_NO_MEDIA The device has no medium.
jcarsey1e6e84c2010-01-25 20:05:08 +0000717 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
jcarsey94b17fa2009-05-07 18:46:18 +0000718 medium is no longer supported.
719 @retval EFI_DEVICE_ERROR The device reported an error.
720 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
721 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
722 @retval EFI_ACCESS_DENIED The file was opened read only.
jcarsey1e6e84c2010-01-25 20:05:08 +0000723 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
jcarsey94b17fa2009-05-07 18:46:18 +0000724 file.
725 @retval EFI_VOLUME_FULL The volume is full.
726 @sa ShellOpenFileByName
727**/
728EFI_STATUS
729EFIAPI
730ShellCreateDirectory(
jcarseyb82bfcc2009-06-29 16:28:23 +0000731 IN CONST CHAR16 *DirectoryName,
jcarseya405b862010-09-14 05:18:09 +0000732 OUT SHELL_FILE_HANDLE *FileHandle
733 )
734{
jcarsey2247dde2009-11-09 18:08:58 +0000735 if (mEfiShellProtocol != NULL) {
736 //
737 // Use UEFI Shell 2.0 method
738 //
739 return (mEfiShellProtocol->CreateFile(DirectoryName,
740 EFI_FILE_DIRECTORY,
741 FileHandle
jcarseya405b862010-09-14 05:18:09 +0000742 ));
jcarsey2247dde2009-11-09 18:08:58 +0000743 } else {
744 return (ShellOpenFileByName(DirectoryName,
745 FileHandle,
746 EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE,
747 EFI_FILE_DIRECTORY
jcarseya405b862010-09-14 05:18:09 +0000748 ));
jcarsey2247dde2009-11-09 18:08:58 +0000749 }
jcarsey94b17fa2009-05-07 18:46:18 +0000750}
751
752/**
753 This function reads information from an opened file.
754
jcarsey1e6e84c2010-01-25 20:05:08 +0000755 If FileHandle is not a directory, the function reads the requested number of
756 bytes from the file at the file's current position and returns them in Buffer.
jcarsey94b17fa2009-05-07 18:46:18 +0000757 If the read goes beyond the end of the file, the read length is truncated to the
jcarsey1e6e84c2010-01-25 20:05:08 +0000758 end of the file. The file's current position is increased by the number of bytes
759 returned. If FileHandle is a directory, the function reads the directory entry
760 at the file's current position and returns the entry in Buffer. If the Buffer
761 is not large enough to hold the current directory entry, then
762 EFI_BUFFER_TOO_SMALL is returned and the current file position is not updated.
763 BufferSize is set to be the size of the buffer needed to read the entry. On
764 success, the current position is updated to the next directory entry. If there
765 are no more directory entries, the read returns a zero-length buffer.
jcarsey94b17fa2009-05-07 18:46:18 +0000766 EFI_FILE_INFO is the structure returned as the directory entry.
767
768 @param FileHandle the opened file handle
jcarsey1e6e84c2010-01-25 20:05:08 +0000769 @param BufferSize on input the size of buffer in bytes. on return
jcarsey94b17fa2009-05-07 18:46:18 +0000770 the number of bytes written.
771 @param Buffer the buffer to put read data into.
772
773 @retval EFI_SUCCESS Data was read.
774 @retval EFI_NO_MEDIA The device has no media.
775 @retval EFI_DEVICE_ERROR The device reported an error.
776 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
jcarsey1e6e84c2010-01-25 20:05:08 +0000777 @retval EFI_BUFFER_TO_SMALL Buffer is too small. ReadSize contains required
jcarsey94b17fa2009-05-07 18:46:18 +0000778 size.
779
780**/
781EFI_STATUS
782EFIAPI
783ShellReadFile(
jcarseya405b862010-09-14 05:18:09 +0000784 IN SHELL_FILE_HANDLE FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000785 IN OUT UINTN *BufferSize,
786 OUT VOID *Buffer
jcarseya405b862010-09-14 05:18:09 +0000787 )
788{
jcarseyd2b45642009-05-11 18:02:16 +0000789 return (FileFunctionMap.ReadFile(FileHandle, BufferSize, Buffer));
jcarsey94b17fa2009-05-07 18:46:18 +0000790}
791
792
793/**
794 Write data to a file.
795
jcarsey1e6e84c2010-01-25 20:05:08 +0000796 This function writes the specified number of bytes to the file at the current
797 file position. The current file position is advanced the actual number of bytes
798 written, which is returned in BufferSize. Partial writes only occur when there
799 has been a data error during the write attempt (such as "volume space full").
800 The file is automatically grown to hold the data if required. Direct writes to
jcarsey94b17fa2009-05-07 18:46:18 +0000801 opened directories are not supported.
802
803 @param FileHandle The opened file for writing
804 @param BufferSize on input the number of bytes in Buffer. On output
805 the number of bytes written.
806 @param Buffer the buffer containing data to write is stored.
807
808 @retval EFI_SUCCESS Data was written.
809 @retval EFI_UNSUPPORTED Writes to an open directory are not supported.
810 @retval EFI_NO_MEDIA The device has no media.
811 @retval EFI_DEVICE_ERROR The device reported an error.
812 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
813 @retval EFI_WRITE_PROTECTED The device is write-protected.
814 @retval EFI_ACCESS_DENIED The file was open for read only.
815 @retval EFI_VOLUME_FULL The volume is full.
816**/
817EFI_STATUS
818EFIAPI
819ShellWriteFile(
jcarseya405b862010-09-14 05:18:09 +0000820 IN SHELL_FILE_HANDLE FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000821 IN OUT UINTN *BufferSize,
822 IN VOID *Buffer
jcarseya405b862010-09-14 05:18:09 +0000823 )
824{
jcarseyd2b45642009-05-11 18:02:16 +0000825 return (FileFunctionMap.WriteFile(FileHandle, BufferSize, Buffer));
jcarsey94b17fa2009-05-07 18:46:18 +0000826}
827
jcarsey1e6e84c2010-01-25 20:05:08 +0000828/**
jcarsey94b17fa2009-05-07 18:46:18 +0000829 Close an open file handle.
830
jcarsey1e6e84c2010-01-25 20:05:08 +0000831 This function closes a specified file handle. All "dirty" cached file data is
832 flushed to the device, and the file is closed. In all cases the handle is
jcarsey94b17fa2009-05-07 18:46:18 +0000833 closed.
834
835@param FileHandle the file handle to close.
836
837@retval EFI_SUCCESS the file handle was closed sucessfully.
838**/
839EFI_STATUS
840EFIAPI
841ShellCloseFile (
jcarseya405b862010-09-14 05:18:09 +0000842 IN SHELL_FILE_HANDLE *FileHandle
843 )
844{
jcarseyd2b45642009-05-11 18:02:16 +0000845 return (FileFunctionMap.CloseFile(*FileHandle));
jcarsey94b17fa2009-05-07 18:46:18 +0000846}
847
848/**
849 Delete a file and close the handle
850
851 This function closes and deletes a file. In all cases the file handle is closed.
jcarsey1e6e84c2010-01-25 20:05:08 +0000852 If the file cannot be deleted, the warning code EFI_WARN_DELETE_FAILURE is
jcarsey94b17fa2009-05-07 18:46:18 +0000853 returned, but the handle is still closed.
854
855 @param FileHandle the file handle to delete
856
857 @retval EFI_SUCCESS the file was closed sucessfully
jcarsey1e6e84c2010-01-25 20:05:08 +0000858 @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not
jcarsey94b17fa2009-05-07 18:46:18 +0000859 deleted
860 @retval INVALID_PARAMETER One of the parameters has an invalid value.
861**/
862EFI_STATUS
863EFIAPI
864ShellDeleteFile (
jcarseya405b862010-09-14 05:18:09 +0000865 IN SHELL_FILE_HANDLE *FileHandle
866 )
867{
jcarseyd2b45642009-05-11 18:02:16 +0000868 return (FileFunctionMap.DeleteFile(*FileHandle));
jcarsey94b17fa2009-05-07 18:46:18 +0000869}
870
871/**
872 Set the current position in a file.
873
jcarsey1e6e84c2010-01-25 20:05:08 +0000874 This function sets the current file position for the handle to the position
jcarsey94b17fa2009-05-07 18:46:18 +0000875 supplied. With the exception of seeking to position 0xFFFFFFFFFFFFFFFF, only
jcarsey1e6e84c2010-01-25 20:05:08 +0000876 absolute positioning is supported, and seeking past the end of the file is
877 allowed (a subsequent write would grow the file). Seeking to position
jcarsey94b17fa2009-05-07 18:46:18 +0000878 0xFFFFFFFFFFFFFFFF causes the current position to be set to the end of the file.
jcarsey1e6e84c2010-01-25 20:05:08 +0000879 If FileHandle is a directory, the only position that may be set is zero. This
jcarsey94b17fa2009-05-07 18:46:18 +0000880 has the effect of starting the read process of the directory entries over.
881
882 @param FileHandle The file handle on which the position is being set
883 @param Position Byte position from begining of file
884
885 @retval EFI_SUCCESS Operation completed sucessfully.
jcarsey1e6e84c2010-01-25 20:05:08 +0000886 @retval EFI_UNSUPPORTED the seek request for non-zero is not valid on
jcarsey94b17fa2009-05-07 18:46:18 +0000887 directories.
888 @retval INVALID_PARAMETER One of the parameters has an invalid value.
889**/
890EFI_STATUS
891EFIAPI
892ShellSetFilePosition (
jcarseya405b862010-09-14 05:18:09 +0000893 IN SHELL_FILE_HANDLE FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000894 IN UINT64 Position
jcarseya405b862010-09-14 05:18:09 +0000895 )
896{
jcarseyd2b45642009-05-11 18:02:16 +0000897 return (FileFunctionMap.SetFilePosition(FileHandle, Position));
jcarsey94b17fa2009-05-07 18:46:18 +0000898}
899
jcarsey1e6e84c2010-01-25 20:05:08 +0000900/**
jcarsey94b17fa2009-05-07 18:46:18 +0000901 Gets a file's current position
902
jcarsey1e6e84c2010-01-25 20:05:08 +0000903 This function retrieves the current file position for the file handle. For
904 directories, the current file position has no meaning outside of the file
jcarsey94b17fa2009-05-07 18:46:18 +0000905 system driver and as such the operation is not supported. An error is returned
906 if FileHandle is a directory.
907
908 @param FileHandle The open file handle on which to get the position.
909 @param Position Byte position from begining of file.
910
911 @retval EFI_SUCCESS the operation completed sucessfully.
912 @retval INVALID_PARAMETER One of the parameters has an invalid value.
913 @retval EFI_UNSUPPORTED the request is not valid on directories.
914**/
915EFI_STATUS
916EFIAPI
917ShellGetFilePosition (
jcarseya405b862010-09-14 05:18:09 +0000918 IN SHELL_FILE_HANDLE FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000919 OUT UINT64 *Position
jcarseya405b862010-09-14 05:18:09 +0000920 )
921{
jcarseyd2b45642009-05-11 18:02:16 +0000922 return (FileFunctionMap.GetFilePosition(FileHandle, Position));
jcarsey94b17fa2009-05-07 18:46:18 +0000923}
924/**
925 Flushes data on a file
jcarsey1e6e84c2010-01-25 20:05:08 +0000926
jcarsey94b17fa2009-05-07 18:46:18 +0000927 This function flushes all modified data associated with a file to a device.
928
929 @param FileHandle The file handle on which to flush data
930
931 @retval EFI_SUCCESS The data was flushed.
932 @retval EFI_NO_MEDIA The device has no media.
933 @retval EFI_DEVICE_ERROR The device reported an error.
934 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
935 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
936 @retval EFI_ACCESS_DENIED The file was opened for read only.
937**/
938EFI_STATUS
939EFIAPI
940ShellFlushFile (
jcarseya405b862010-09-14 05:18:09 +0000941 IN SHELL_FILE_HANDLE FileHandle
942 )
943{
jcarseyd2b45642009-05-11 18:02:16 +0000944 return (FileFunctionMap.FlushFile(FileHandle));
jcarsey94b17fa2009-05-07 18:46:18 +0000945}
946
947/**
948 Retrieves the first file from a directory
949
jcarsey1e6e84c2010-01-25 20:05:08 +0000950 This function opens a directory and gets the first file's info in the
951 directory. Caller can use ShellFindNextFile() to get other files. When
jcarsey94b17fa2009-05-07 18:46:18 +0000952 complete the caller is responsible for calling FreePool() on Buffer.
953
954 @param DirHandle The file handle of the directory to search
955 @param Buffer Pointer to buffer for file's information
956
957 @retval EFI_SUCCESS Found the first file.
958 @retval EFI_NOT_FOUND Cannot find the directory.
959 @retval EFI_NO_MEDIA The device has no media.
960 @retval EFI_DEVICE_ERROR The device reported an error.
961 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
962 @return Others status of ShellGetFileInfo, ShellSetFilePosition,
963 or ShellReadFile
964**/
965EFI_STATUS
966EFIAPI
967ShellFindFirstFile (
jcarseya405b862010-09-14 05:18:09 +0000968 IN SHELL_FILE_HANDLE DirHandle,
jcarseyd2b45642009-05-11 18:02:16 +0000969 OUT EFI_FILE_INFO **Buffer
jcarseya405b862010-09-14 05:18:09 +0000970 )
971{
jcarsey94b17fa2009-05-07 18:46:18 +0000972 //
jcarseyd2b45642009-05-11 18:02:16 +0000973 // pass to file handle lib
jcarsey94b17fa2009-05-07 18:46:18 +0000974 //
jcarseyd2b45642009-05-11 18:02:16 +0000975 return (FileHandleFindFirstFile(DirHandle, Buffer));
jcarsey94b17fa2009-05-07 18:46:18 +0000976}
977/**
978 Retrieves the next file in a directory.
979
jcarseya405b862010-09-14 05:18:09 +0000980 To use this function, caller must call the ShellFindFirstFile() to get the
jcarsey1e6e84c2010-01-25 20:05:08 +0000981 first file, and then use this function get other files. This function can be
982 called for several times to get each file's information in the directory. If
983 the call of ShellFindNextFile() got the last file in the directory, the next
984 call of this function has no file to get. *NoFile will be set to TRUE and the
985 Buffer memory will be automatically freed.
jcarsey94b17fa2009-05-07 18:46:18 +0000986
987 @param DirHandle the file handle of the directory
988 @param Buffer pointer to buffer for file's information
989 @param NoFile pointer to boolean when last file is found
990
991 @retval EFI_SUCCESS Found the next file, or reached last file
992 @retval EFI_NO_MEDIA The device has no media.
993 @retval EFI_DEVICE_ERROR The device reported an error.
994 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
995**/
996EFI_STATUS
997EFIAPI
998ShellFindNextFile(
jcarseya405b862010-09-14 05:18:09 +0000999 IN SHELL_FILE_HANDLE DirHandle,
jcarsey94b17fa2009-05-07 18:46:18 +00001000 OUT EFI_FILE_INFO *Buffer,
1001 OUT BOOLEAN *NoFile
jcarseya405b862010-09-14 05:18:09 +00001002 )
1003{
jcarsey94b17fa2009-05-07 18:46:18 +00001004 //
jcarseyd2b45642009-05-11 18:02:16 +00001005 // pass to file handle lib
jcarsey94b17fa2009-05-07 18:46:18 +00001006 //
jcarseyd2b45642009-05-11 18:02:16 +00001007 return (FileHandleFindNextFile(DirHandle, Buffer, NoFile));
jcarsey94b17fa2009-05-07 18:46:18 +00001008}
1009/**
1010 Retrieve the size of a file.
1011
1012 if FileHandle is NULL then ASSERT()
1013 if Size is NULL then ASSERT()
1014
jcarsey1e6e84c2010-01-25 20:05:08 +00001015 This function extracts the file size info from the FileHandle's EFI_FILE_INFO
jcarsey94b17fa2009-05-07 18:46:18 +00001016 data.
1017
1018 @param FileHandle file handle from which size is retrieved
1019 @param Size pointer to size
1020
1021 @retval EFI_SUCCESS operation was completed sucessfully
1022 @retval EFI_DEVICE_ERROR cannot access the file
1023**/
1024EFI_STATUS
1025EFIAPI
1026ShellGetFileSize (
jcarseya405b862010-09-14 05:18:09 +00001027 IN SHELL_FILE_HANDLE FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +00001028 OUT UINT64 *Size
jcarseya405b862010-09-14 05:18:09 +00001029 )
1030{
jcarseyd2b45642009-05-11 18:02:16 +00001031 return (FileFunctionMap.GetFileSize(FileHandle, Size));
jcarsey94b17fa2009-05-07 18:46:18 +00001032}
1033/**
1034 Retrieves the status of the break execution flag
1035
1036 this function is useful to check whether the application is being asked to halt by the shell.
1037
1038 @retval TRUE the execution break is enabled
1039 @retval FALSE the execution break is not enabled
1040**/
1041BOOLEAN
1042EFIAPI
1043ShellGetExecutionBreakFlag(
1044 VOID
1045 )
1046{
jcarsey1e6e84c2010-01-25 20:05:08 +00001047 //
jcarsey94b17fa2009-05-07 18:46:18 +00001048 // Check for UEFI Shell 2.0 protocols
1049 //
1050 if (mEfiShellProtocol != NULL) {
1051
1052 //
1053 // We are using UEFI Shell 2.0; see if the event has been triggered
1054 //
1055 if (gBS->CheckEvent(mEfiShellProtocol->ExecutionBreak) != EFI_SUCCESS) {
1056 return (FALSE);
1057 }
1058 return (TRUE);
jcarsey1e6e84c2010-01-25 20:05:08 +00001059 }
jcarsey94b17fa2009-05-07 18:46:18 +00001060
1061 //
1062 // using EFI Shell; call the function to check
1063 //
1064 ASSERT(mEfiShellEnvironment2 != NULL);
1065 return (mEfiShellEnvironment2->GetExecutionBreak());
1066}
1067/**
1068 return the value of an environment variable
1069
jcarsey1e6e84c2010-01-25 20:05:08 +00001070 this function gets the value of the environment variable set by the
jcarsey94b17fa2009-05-07 18:46:18 +00001071 ShellSetEnvironmentVariable function
1072
1073 @param EnvKey The key name of the environment variable.
1074
1075 @retval NULL the named environment variable does not exist.
1076 @return != NULL pointer to the value of the environment variable
1077**/
1078CONST CHAR16*
1079EFIAPI
1080ShellGetEnvironmentVariable (
jcarsey9b3bf082009-06-23 21:15:07 +00001081 IN CONST CHAR16 *EnvKey
jcarsey94b17fa2009-05-07 18:46:18 +00001082 )
1083{
jcarsey1e6e84c2010-01-25 20:05:08 +00001084 //
jcarsey94b17fa2009-05-07 18:46:18 +00001085 // Check for UEFI Shell 2.0 protocols
1086 //
1087 if (mEfiShellProtocol != NULL) {
1088 return (mEfiShellProtocol->GetEnv(EnvKey));
1089 }
1090
1091 //
1092 // ASSERT that we must have EFI shell
1093 //
1094 ASSERT(mEfiShellEnvironment2 != NULL);
1095
1096 //
1097 // using EFI Shell
1098 //
jcarsey9b3bf082009-06-23 21:15:07 +00001099 return (mEfiShellEnvironment2->GetEnv((CHAR16*)EnvKey));
jcarsey94b17fa2009-05-07 18:46:18 +00001100}
1101/**
1102 set the value of an environment variable
1103
1104This function changes the current value of the specified environment variable. If the
1105environment variable exists and the Value is an empty string, then the environment
1106variable is deleted. If the environment variable exists and the Value is not an empty
1107string, then the value of the environment variable is changed. If the environment
1108variable does not exist and the Value is an empty string, there is no action. If the
1109environment variable does not exist and the Value is a non-empty string, then the
1110environment variable is created and assigned the specified value.
1111
1112 This is not supported pre-UEFI Shell 2.0.
1113
1114 @param EnvKey The key name of the environment variable.
1115 @param EnvVal The Value of the environment variable
1116 @param Volatile Indicates whether the variable is non-volatile (FALSE) or volatile (TRUE).
1117
1118 @retval EFI_SUCCESS the operation was completed sucessfully
1119 @retval EFI_UNSUPPORTED This operation is not allowed in pre UEFI 2.0 Shell environments
1120**/
1121EFI_STATUS
1122EFIAPI
1123ShellSetEnvironmentVariable (
1124 IN CONST CHAR16 *EnvKey,
1125 IN CONST CHAR16 *EnvVal,
1126 IN BOOLEAN Volatile
1127 )
1128{
jcarsey1e6e84c2010-01-25 20:05:08 +00001129 //
jcarsey94b17fa2009-05-07 18:46:18 +00001130 // Check for UEFI Shell 2.0 protocols
1131 //
1132 if (mEfiShellProtocol != NULL) {
1133 return (mEfiShellProtocol->SetEnv(EnvKey, EnvVal, Volatile));
jcarsey1e6e84c2010-01-25 20:05:08 +00001134 }
jcarsey94b17fa2009-05-07 18:46:18 +00001135
1136 //
1137 // This feature does not exist under EFI shell
1138 //
1139 return (EFI_UNSUPPORTED);
1140}
jcarseya405b862010-09-14 05:18:09 +00001141
jcarsey94b17fa2009-05-07 18:46:18 +00001142/**
jcarseya405b862010-09-14 05:18:09 +00001143 Cause the shell to parse and execute a command line.
jcarsey94b17fa2009-05-07 18:46:18 +00001144
1145 This function creates a nested instance of the shell and executes the specified
jcarseya405b862010-09-14 05:18:09 +00001146 command (CommandLine) with the specified environment (Environment). Upon return,
1147 the status code returned by the specified command is placed in StatusCode.
1148 If Environment is NULL, then the current environment is used and all changes made
1149 by the commands executed will be reflected in the current environment. If the
1150 Environment is non-NULL, then the changes made will be discarded.
1151 The CommandLine is executed from the current working directory on the current
1152 device.
jcarsey94b17fa2009-05-07 18:46:18 +00001153
jcarseya405b862010-09-14 05:18:09 +00001154 The EnvironmentVariables and Status parameters are ignored in a pre-UEFI Shell 2.0
1155 environment. The values pointed to by the parameters will be unchanged by the
1156 ShellExecute() function. The Output parameter has no effect in a
1157 UEFI Shell 2.0 environment.
jcarsey94b17fa2009-05-07 18:46:18 +00001158
jcarseya405b862010-09-14 05:18:09 +00001159 @param[in] ParentHandle The parent image starting the operation.
1160 @param[in] CommandLine The pointer to a NULL terminated command line.
1161 @param[in] Output True to display debug output. False to hide it.
1162 @param[in] EnvironmentVariables Optional pointer to array of environment variables
1163 in the form "x=y". If NULL, the current set is used.
1164 @param[out] Status The status of the run command line.
jcarsey94b17fa2009-05-07 18:46:18 +00001165
jcarseya405b862010-09-14 05:18:09 +00001166 @retval EFI_SUCCESS The operation completed sucessfully. Status
1167 contains the status code returned.
1168 @retval EFI_INVALID_PARAMETER A parameter contains an invalid value.
1169 @retval EFI_OUT_OF_RESOURCES Out of resources.
1170 @retval EFI_UNSUPPORTED The operation is not allowed.
jcarsey94b17fa2009-05-07 18:46:18 +00001171**/
jcarseya405b862010-09-14 05:18:09 +00001172
jcarsey94b17fa2009-05-07 18:46:18 +00001173EFI_STATUS
1174EFIAPI
1175ShellExecute (
1176 IN EFI_HANDLE *ParentHandle,
1177 IN CHAR16 *CommandLine OPTIONAL,
1178 IN BOOLEAN Output OPTIONAL,
1179 IN CHAR16 **EnvironmentVariables OPTIONAL,
1180 OUT EFI_STATUS *Status OPTIONAL
1181 )
1182{
jcarsey1e6e84c2010-01-25 20:05:08 +00001183 //
jcarsey94b17fa2009-05-07 18:46:18 +00001184 // Check for UEFI Shell 2.0 protocols
1185 //
1186 if (mEfiShellProtocol != NULL) {
1187 //
1188 // Call UEFI Shell 2.0 version (not using Output parameter)
1189 //
1190 return (mEfiShellProtocol->Execute(ParentHandle,
1191 CommandLine,
1192 EnvironmentVariables,
1193 Status));
jcarsey1e6e84c2010-01-25 20:05:08 +00001194 }
jcarsey94b17fa2009-05-07 18:46:18 +00001195 //
1196 // ASSERT that we must have EFI shell
1197 //
1198 ASSERT(mEfiShellEnvironment2 != NULL);
1199 //
1200 // Call EFI Shell version (not using EnvironmentVariables or Status parameters)
1201 // Due to oddity in the EFI shell we want to dereference the ParentHandle here
1202 //
jcarsey1e6e84c2010-01-25 20:05:08 +00001203 return (mEfiShellEnvironment2->Execute(*ParentHandle,
1204 CommandLine,
jcarsey94b17fa2009-05-07 18:46:18 +00001205 Output));
1206}
1207/**
1208 Retreives the current directory path
1209
jcarsey1e6e84c2010-01-25 20:05:08 +00001210 If the DeviceName is NULL, it returns the current device's current directory
1211 name. If the DeviceName is not NULL, it returns the current directory name
jcarsey94b17fa2009-05-07 18:46:18 +00001212 on specified drive.
1213
1214 @param DeviceName the name of the drive to get directory on
1215
1216 @retval NULL the directory does not exist
1217 @return != NULL the directory
1218**/
1219CONST CHAR16*
1220EFIAPI
1221ShellGetCurrentDir (
jcarseya405b862010-09-14 05:18:09 +00001222 IN CHAR16 * CONST DeviceName OPTIONAL
jcarsey94b17fa2009-05-07 18:46:18 +00001223 )
1224{
jcarsey1e6e84c2010-01-25 20:05:08 +00001225 //
jcarsey94b17fa2009-05-07 18:46:18 +00001226 // Check for UEFI Shell 2.0 protocols
1227 //
1228 if (mEfiShellProtocol != NULL) {
1229 return (mEfiShellProtocol->GetCurDir(DeviceName));
jcarsey1e6e84c2010-01-25 20:05:08 +00001230 }
jcarsey94b17fa2009-05-07 18:46:18 +00001231 //
1232 // ASSERT that we must have EFI shell
1233 //
1234 ASSERT(mEfiShellEnvironment2 != NULL);
1235 return (mEfiShellEnvironment2->CurDir(DeviceName));
1236}
1237/**
1238 sets (enabled or disabled) the page break mode
1239
jcarsey1e6e84c2010-01-25 20:05:08 +00001240 when page break mode is enabled the screen will stop scrolling
jcarsey94b17fa2009-05-07 18:46:18 +00001241 and wait for operator input before scrolling a subsequent screen.
1242
1243 @param CurrentState TRUE to enable and FALSE to disable
1244**/
jcarsey1e6e84c2010-01-25 20:05:08 +00001245VOID
jcarsey94b17fa2009-05-07 18:46:18 +00001246EFIAPI
1247ShellSetPageBreakMode (
1248 IN BOOLEAN CurrentState
1249 )
1250{
1251 //
1252 // check for enabling
1253 //
1254 if (CurrentState != 0x00) {
jcarsey1e6e84c2010-01-25 20:05:08 +00001255 //
jcarsey94b17fa2009-05-07 18:46:18 +00001256 // check for UEFI Shell 2.0
1257 //
1258 if (mEfiShellProtocol != NULL) {
1259 //
1260 // Enable with UEFI 2.0 Shell
1261 //
1262 mEfiShellProtocol->EnablePageBreak();
1263 return;
1264 } else {
jcarsey1e6e84c2010-01-25 20:05:08 +00001265 //
jcarsey94b17fa2009-05-07 18:46:18 +00001266 // ASSERT that must have EFI Shell
1267 //
1268 ASSERT(mEfiShellEnvironment2 != NULL);
1269 //
1270 // Enable with EFI Shell
1271 //
1272 mEfiShellEnvironment2->EnablePageBreak (DEFAULT_INIT_ROW, DEFAULT_AUTO_LF);
1273 return;
1274 }
1275 } else {
jcarsey1e6e84c2010-01-25 20:05:08 +00001276 //
jcarsey94b17fa2009-05-07 18:46:18 +00001277 // check for UEFI Shell 2.0
1278 //
1279 if (mEfiShellProtocol != NULL) {
1280 //
1281 // Disable with UEFI 2.0 Shell
1282 //
1283 mEfiShellProtocol->DisablePageBreak();
1284 return;
1285 } else {
jcarsey1e6e84c2010-01-25 20:05:08 +00001286 //
jcarsey94b17fa2009-05-07 18:46:18 +00001287 // ASSERT that must have EFI Shell
1288 //
1289 ASSERT(mEfiShellEnvironment2 != NULL);
1290 //
1291 // Disable with EFI Shell
1292 //
1293 mEfiShellEnvironment2->DisablePageBreak ();
1294 return;
1295 }
1296 }
1297}
1298
1299///
1300/// version of EFI_SHELL_FILE_INFO struct, except has no CONST pointers.
1301/// This allows for the struct to be populated.
1302///
1303typedef struct {
jcarseyd2b45642009-05-11 18:02:16 +00001304 LIST_ENTRY Link;
jcarsey94b17fa2009-05-07 18:46:18 +00001305 EFI_STATUS Status;
1306 CHAR16 *FullName;
1307 CHAR16 *FileName;
jcarseya405b862010-09-14 05:18:09 +00001308 SHELL_FILE_HANDLE Handle;
jcarsey94b17fa2009-05-07 18:46:18 +00001309 EFI_FILE_INFO *Info;
1310} EFI_SHELL_FILE_INFO_NO_CONST;
1311
1312/**
1313 Converts a EFI shell list of structures to the coresponding UEFI Shell 2.0 type of list.
1314
1315 if OldStyleFileList is NULL then ASSERT()
1316
jcarsey1e6e84c2010-01-25 20:05:08 +00001317 this function will convert a SHELL_FILE_ARG based list into a callee allocated
jcarsey94b17fa2009-05-07 18:46:18 +00001318 EFI_SHELL_FILE_INFO based list. it is up to the caller to free the memory via
1319 the ShellCloseFileMetaArg function.
1320
jcarseya405b862010-09-14 05:18:09 +00001321 @param[in] FileList the EFI shell list type
jcarseyb82bfcc2009-06-29 16:28:23 +00001322 @param[in,out] ListHead the list to add to
jcarsey94b17fa2009-05-07 18:46:18 +00001323
1324 @retval the resultant head of the double linked new format list;
1325**/
1326LIST_ENTRY*
1327EFIAPI
1328InternalShellConvertFileListType (
jcarsey9b3bf082009-06-23 21:15:07 +00001329 IN LIST_ENTRY *FileList,
1330 IN OUT LIST_ENTRY *ListHead
jcarsey125c2cf2009-11-18 21:36:50 +00001331 )
1332{
jcarsey94b17fa2009-05-07 18:46:18 +00001333 SHELL_FILE_ARG *OldInfo;
jcarsey9b3bf082009-06-23 21:15:07 +00001334 LIST_ENTRY *Link;
jcarsey94b17fa2009-05-07 18:46:18 +00001335 EFI_SHELL_FILE_INFO_NO_CONST *NewInfo;
1336
1337 //
jcarsey9b3bf082009-06-23 21:15:07 +00001338 // ASSERTs
jcarsey94b17fa2009-05-07 18:46:18 +00001339 //
jcarsey9b3bf082009-06-23 21:15:07 +00001340 ASSERT(FileList != NULL);
1341 ASSERT(ListHead != NULL);
jcarsey94b17fa2009-05-07 18:46:18 +00001342
1343 //
1344 // enumerate through each member of the old list and copy
1345 //
jcarseyd2b45642009-05-11 18:02:16 +00001346 for (Link = FileList->ForwardLink; Link != FileList; Link = Link->ForwardLink) {
jcarsey94b17fa2009-05-07 18:46:18 +00001347 OldInfo = CR (Link, SHELL_FILE_ARG, Link, SHELL_FILE_ARG_SIGNATURE);
jcarseya405b862010-09-14 05:18:09 +00001348 ASSERT(OldInfo != NULL);
1349
1350 //
1351 // Skip ones that failed to open...
1352 //
1353 if (OldInfo->Status != EFI_SUCCESS) {
1354 continue;
1355 }
jcarsey94b17fa2009-05-07 18:46:18 +00001356
1357 //
1358 // make sure the old list was valid
1359 //
jcarsey94b17fa2009-05-07 18:46:18 +00001360 ASSERT(OldInfo->Info != NULL);
1361 ASSERT(OldInfo->FullName != NULL);
1362 ASSERT(OldInfo->FileName != NULL);
1363
1364 //
1365 // allocate a new EFI_SHELL_FILE_INFO object
1366 //
1367 NewInfo = AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));
jcarseyc9d92df2010-02-03 15:37:54 +00001368 ASSERT(NewInfo != NULL);
1369 if (NewInfo == NULL) {
1370 break;
1371 }
jcarsey1e6e84c2010-01-25 20:05:08 +00001372
1373 //
jcarsey94b17fa2009-05-07 18:46:18 +00001374 // copy the simple items
1375 //
1376 NewInfo->Handle = OldInfo->Handle;
1377 NewInfo->Status = OldInfo->Status;
1378
jcarseyd2b45642009-05-11 18:02:16 +00001379 // old shell checks for 0 not NULL
1380 OldInfo->Handle = 0;
1381
jcarsey94b17fa2009-05-07 18:46:18 +00001382 //
1383 // allocate new space to copy strings and structure
1384 //
1385 NewInfo->FullName = AllocateZeroPool(StrSize(OldInfo->FullName));
1386 NewInfo->FileName = AllocateZeroPool(StrSize(OldInfo->FileName));
1387 NewInfo->Info = AllocateZeroPool((UINTN)OldInfo->Info->Size);
jcarsey1e6e84c2010-01-25 20:05:08 +00001388
jcarsey94b17fa2009-05-07 18:46:18 +00001389 //
1390 // make sure all the memory allocations were sucessful
1391 //
1392 ASSERT(NewInfo->FullName != NULL);
1393 ASSERT(NewInfo->FileName != NULL);
1394 ASSERT(NewInfo->Info != NULL);
1395
1396 //
1397 // Copt the strings and structure
1398 //
1399 StrCpy(NewInfo->FullName, OldInfo->FullName);
1400 StrCpy(NewInfo->FileName, OldInfo->FileName);
1401 gBS->CopyMem (NewInfo->Info, OldInfo->Info, (UINTN)OldInfo->Info->Size);
1402
1403 //
1404 // add that to the list
1405 //
jcarsey9b3bf082009-06-23 21:15:07 +00001406 InsertTailList(ListHead, &NewInfo->Link);
jcarsey94b17fa2009-05-07 18:46:18 +00001407 }
1408 return (ListHead);
1409}
1410/**
1411 Opens a group of files based on a path.
1412
jcarsey1e6e84c2010-01-25 20:05:08 +00001413 This function uses the Arg to open all the matching files. Each matched
1414 file has a SHELL_FILE_ARG structure to record the file information. These
1415 structures are placed on the list ListHead. Users can get the SHELL_FILE_ARG
jcarsey94b17fa2009-05-07 18:46:18 +00001416 structures from ListHead to access each file. This function supports wildcards
jcarsey1e6e84c2010-01-25 20:05:08 +00001417 and will process '?' and '*' as such. the list must be freed with a call to
jcarsey94b17fa2009-05-07 18:46:18 +00001418 ShellCloseFileMetaArg().
1419
jcarsey1e6e84c2010-01-25 20:05:08 +00001420 If you are NOT appending to an existing list *ListHead must be NULL. If
jcarsey5f7431d2009-07-10 18:06:01 +00001421 *ListHead is NULL then it must be callee freed.
jcarsey94b17fa2009-05-07 18:46:18 +00001422
1423 @param Arg pointer to path string
1424 @param OpenMode mode to open files with
1425 @param ListHead head of linked list of results
1426
jcarsey1e6e84c2010-01-25 20:05:08 +00001427 @retval EFI_SUCCESS the operation was sucessful and the list head
jcarsey94b17fa2009-05-07 18:46:18 +00001428 contains the list of opened files
jcarsey94b17fa2009-05-07 18:46:18 +00001429 @return != EFI_SUCCESS the operation failed
1430
1431 @sa InternalShellConvertFileListType
1432**/
1433EFI_STATUS
1434EFIAPI
1435ShellOpenFileMetaArg (
1436 IN CHAR16 *Arg,
1437 IN UINT64 OpenMode,
1438 IN OUT EFI_SHELL_FILE_INFO **ListHead
1439 )
1440{
1441 EFI_STATUS Status;
jcarsey9b3bf082009-06-23 21:15:07 +00001442 LIST_ENTRY mOldStyleFileList;
jcarsey1e6e84c2010-01-25 20:05:08 +00001443
jcarsey94b17fa2009-05-07 18:46:18 +00001444 //
1445 // ASSERT that Arg and ListHead are not NULL
1446 //
1447 ASSERT(Arg != NULL);
1448 ASSERT(ListHead != NULL);
1449
jcarsey1e6e84c2010-01-25 20:05:08 +00001450 //
jcarsey94b17fa2009-05-07 18:46:18 +00001451 // Check for UEFI Shell 2.0 protocols
1452 //
1453 if (mEfiShellProtocol != NULL) {
jcarsey5f7431d2009-07-10 18:06:01 +00001454 if (*ListHead == NULL) {
1455 *ListHead = (EFI_SHELL_FILE_INFO*)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));
1456 if (*ListHead == NULL) {
1457 return (EFI_OUT_OF_RESOURCES);
1458 }
1459 InitializeListHead(&((*ListHead)->Link));
jcarsey1e6e84c2010-01-25 20:05:08 +00001460 }
1461 Status = mEfiShellProtocol->OpenFileList(Arg,
1462 OpenMode,
jcarsey2247dde2009-11-09 18:08:58 +00001463 ListHead);
1464 if (EFI_ERROR(Status)) {
1465 mEfiShellProtocol->RemoveDupInFileList(ListHead);
1466 } else {
1467 Status = mEfiShellProtocol->RemoveDupInFileList(ListHead);
1468 }
jcarseya405b862010-09-14 05:18:09 +00001469 if (*ListHead != NULL && IsListEmpty(&(*ListHead)->Link)) {
1470 FreePool(*ListHead);
1471 *ListHead = NULL;
1472 return (EFI_NOT_FOUND);
1473 }
jcarsey2247dde2009-11-09 18:08:58 +00001474 return (Status);
jcarsey1e6e84c2010-01-25 20:05:08 +00001475 }
jcarsey94b17fa2009-05-07 18:46:18 +00001476
1477 //
1478 // ASSERT that we must have EFI shell
1479 //
1480 ASSERT(mEfiShellEnvironment2 != NULL);
1481
1482 //
jcarsey94b17fa2009-05-07 18:46:18 +00001483 // make sure the list head is initialized
1484 //
jcarsey9b3bf082009-06-23 21:15:07 +00001485 InitializeListHead(&mOldStyleFileList);
jcarsey94b17fa2009-05-07 18:46:18 +00001486
1487 //
1488 // Get the EFI Shell list of files
1489 //
jcarsey9b3bf082009-06-23 21:15:07 +00001490 Status = mEfiShellEnvironment2->FileMetaArg(Arg, &mOldStyleFileList);
jcarsey94b17fa2009-05-07 18:46:18 +00001491 if (EFI_ERROR(Status)) {
1492 *ListHead = NULL;
1493 return (Status);
1494 }
1495
jcarsey9b3bf082009-06-23 21:15:07 +00001496 if (*ListHead == NULL) {
1497 *ListHead = (EFI_SHELL_FILE_INFO *)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));
1498 if (*ListHead == NULL) {
1499 return (EFI_OUT_OF_RESOURCES);
1500 }
jcarseya405b862010-09-14 05:18:09 +00001501 InitializeListHead(&((*ListHead)->Link));
jcarsey9b3bf082009-06-23 21:15:07 +00001502 }
1503
jcarsey94b17fa2009-05-07 18:46:18 +00001504 //
1505 // Convert that to equivalent of UEFI Shell 2.0 structure
1506 //
jcarsey9b3bf082009-06-23 21:15:07 +00001507 InternalShellConvertFileListType(&mOldStyleFileList, &(*ListHead)->Link);
jcarsey94b17fa2009-05-07 18:46:18 +00001508
1509 //
jcarseyd2b45642009-05-11 18:02:16 +00001510 // Free the EFI Shell version that was converted.
1511 //
jcarsey9b3bf082009-06-23 21:15:07 +00001512 mEfiShellEnvironment2->FreeFileList(&mOldStyleFileList);
jcarsey94b17fa2009-05-07 18:46:18 +00001513
jcarseya405b862010-09-14 05:18:09 +00001514 if ((*ListHead)->Link.ForwardLink == (*ListHead)->Link.BackLink && (*ListHead)->Link.BackLink == &((*ListHead)->Link)) {
1515 FreePool(*ListHead);
1516 *ListHead = NULL;
1517 Status = EFI_NOT_FOUND;
1518 }
1519
jcarsey94b17fa2009-05-07 18:46:18 +00001520 return (Status);
1521}
1522/**
jcarseya405b862010-09-14 05:18:09 +00001523 Free the linked list returned from ShellOpenFileMetaArg.
jcarsey94b17fa2009-05-07 18:46:18 +00001524
jcarseya405b862010-09-14 05:18:09 +00001525 if ListHead is NULL then ASSERT().
jcarsey94b17fa2009-05-07 18:46:18 +00001526
jcarseya405b862010-09-14 05:18:09 +00001527 @param ListHead the pointer to free.
jcarsey94b17fa2009-05-07 18:46:18 +00001528
jcarseya405b862010-09-14 05:18:09 +00001529 @retval EFI_SUCCESS the operation was sucessful.
jcarsey94b17fa2009-05-07 18:46:18 +00001530**/
1531EFI_STATUS
1532EFIAPI
1533ShellCloseFileMetaArg (
1534 IN OUT EFI_SHELL_FILE_INFO **ListHead
1535 )
1536{
1537 LIST_ENTRY *Node;
1538
1539 //
1540 // ASSERT that ListHead is not NULL
1541 //
1542 ASSERT(ListHead != NULL);
1543
jcarsey1e6e84c2010-01-25 20:05:08 +00001544 //
jcarsey94b17fa2009-05-07 18:46:18 +00001545 // Check for UEFI Shell 2.0 protocols
1546 //
1547 if (mEfiShellProtocol != NULL) {
1548 return (mEfiShellProtocol->FreeFileList(ListHead));
1549 } else {
1550 //
jcarsey1e6e84c2010-01-25 20:05:08 +00001551 // Since this is EFI Shell version we need to free our internally made copy
jcarsey94b17fa2009-05-07 18:46:18 +00001552 // of the list
1553 //
jcarsey1e6e84c2010-01-25 20:05:08 +00001554 for ( Node = GetFirstNode(&(*ListHead)->Link)
jcarseya405b862010-09-14 05:18:09 +00001555 ; *ListHead != NULL && !IsListEmpty(&(*ListHead)->Link)
jcarsey9b3bf082009-06-23 21:15:07 +00001556 ; Node = GetFirstNode(&(*ListHead)->Link)) {
jcarsey94b17fa2009-05-07 18:46:18 +00001557 RemoveEntryList(Node);
jcarseya405b862010-09-14 05:18:09 +00001558 ((EFI_FILE_PROTOCOL*)((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->Handle)->Close(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->Handle);
jcarsey94b17fa2009-05-07 18:46:18 +00001559 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->FullName);
1560 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->FileName);
1561 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->Info);
1562 FreePool((EFI_SHELL_FILE_INFO_NO_CONST*)Node);
1563 }
1564 return EFI_SUCCESS;
1565 }
1566}
1567
jcarsey125c2cf2009-11-18 21:36:50 +00001568/**
1569 Find a file by searching the CWD and then the path.
1570
jcarseyb3011f42010-01-11 21:49:04 +00001571 If FileName is NULL then ASSERT.
jcarsey125c2cf2009-11-18 21:36:50 +00001572
jcarseyb3011f42010-01-11 21:49:04 +00001573 If the return value is not NULL then the memory must be caller freed.
jcarsey125c2cf2009-11-18 21:36:50 +00001574
1575 @param FileName Filename string.
1576
1577 @retval NULL the file was not found
1578 @return !NULL the full path to the file.
1579**/
1580CHAR16 *
1581EFIAPI
1582ShellFindFilePath (
1583 IN CONST CHAR16 *FileName
1584 )
1585{
1586 CONST CHAR16 *Path;
jcarseya405b862010-09-14 05:18:09 +00001587 SHELL_FILE_HANDLE Handle;
jcarsey125c2cf2009-11-18 21:36:50 +00001588 EFI_STATUS Status;
1589 CHAR16 *RetVal;
1590 CHAR16 *TestPath;
1591 CONST CHAR16 *Walker;
jcarsey36a9d672009-11-20 21:13:41 +00001592 UINTN Size;
jcarsey1cd45e72010-01-29 15:07:44 +00001593 CHAR16 *TempChar;
jcarsey125c2cf2009-11-18 21:36:50 +00001594
1595 RetVal = NULL;
1596
jcarseya405b862010-09-14 05:18:09 +00001597 //
1598 // First make sure its not an absolute path.
1599 //
1600 Status = ShellOpenFileByName(FileName, &Handle, EFI_FILE_MODE_READ, 0);
1601 if (!EFI_ERROR(Status)){
1602 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {
1603 ASSERT(RetVal == NULL);
1604 RetVal = StrnCatGrow(&RetVal, NULL, FileName, 0);
1605 ShellCloseFile(&Handle);
1606 return (RetVal);
1607 } else {
1608 ShellCloseFile(&Handle);
1609 }
1610 }
1611
jcarsey125c2cf2009-11-18 21:36:50 +00001612 Path = ShellGetEnvironmentVariable(L"cwd");
1613 if (Path != NULL) {
jcarsey36a9d672009-11-20 21:13:41 +00001614 Size = StrSize(Path);
1615 Size += StrSize(FileName);
1616 TestPath = AllocateZeroPool(Size);
jcarseyc9d92df2010-02-03 15:37:54 +00001617 ASSERT(TestPath != NULL);
1618 if (TestPath == NULL) {
1619 return (NULL);
1620 }
jcarsey125c2cf2009-11-18 21:36:50 +00001621 StrCpy(TestPath, Path);
1622 StrCat(TestPath, FileName);
1623 Status = ShellOpenFileByName(TestPath, &Handle, EFI_FILE_MODE_READ, 0);
1624 if (!EFI_ERROR(Status)){
jcarseya405b862010-09-14 05:18:09 +00001625 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {
1626 ASSERT(RetVal == NULL);
1627 RetVal = StrnCatGrow(&RetVal, NULL, TestPath, 0);
1628 ShellCloseFile(&Handle);
1629 FreePool(TestPath);
1630 return (RetVal);
1631 } else {
1632 ShellCloseFile(&Handle);
1633 }
jcarsey125c2cf2009-11-18 21:36:50 +00001634 }
1635 FreePool(TestPath);
1636 }
1637 Path = ShellGetEnvironmentVariable(L"path");
1638 if (Path != NULL) {
jcarseya405b862010-09-14 05:18:09 +00001639 Size = StrSize(Path)+sizeof(CHAR16);
jcarsey36a9d672009-11-20 21:13:41 +00001640 Size += StrSize(FileName);
1641 TestPath = AllocateZeroPool(Size);
jcarsey1e6e84c2010-01-25 20:05:08 +00001642 Walker = (CHAR16*)Path;
jcarsey125c2cf2009-11-18 21:36:50 +00001643 do {
1644 CopyMem(TestPath, Walker, StrSize(Walker));
jcarsey1cd45e72010-01-29 15:07:44 +00001645 TempChar = StrStr(TestPath, L";");
1646 if (TempChar != NULL) {
1647 *TempChar = CHAR_NULL;
jcarsey125c2cf2009-11-18 21:36:50 +00001648 }
jcarseya405b862010-09-14 05:18:09 +00001649 if (TestPath[StrLen(TestPath)-1] != L'\\') {
1650 StrCat(TestPath, L"\\");
1651 }
jcarsey125c2cf2009-11-18 21:36:50 +00001652 StrCat(TestPath, FileName);
1653 if (StrStr(Walker, L";") != NULL) {
1654 Walker = StrStr(Walker, L";") + 1;
1655 } else {
1656 Walker = NULL;
1657 }
1658 Status = ShellOpenFileByName(TestPath, &Handle, EFI_FILE_MODE_READ, 0);
1659 if (!EFI_ERROR(Status)){
jcarseya405b862010-09-14 05:18:09 +00001660 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {
1661 ASSERT(RetVal == NULL);
1662 RetVal = StrnCatGrow(&RetVal, NULL, TestPath, 0);
1663 ShellCloseFile(&Handle);
1664 break;
1665 } else {
1666 ShellCloseFile(&Handle);
1667 }
jcarsey125c2cf2009-11-18 21:36:50 +00001668 }
1669 } while (Walker != NULL && Walker[0] != CHAR_NULL);
1670 FreePool(TestPath);
1671 }
1672 return (RetVal);
1673}
1674
jcarseyb3011f42010-01-11 21:49:04 +00001675/**
jcarsey1e6e84c2010-01-25 20:05:08 +00001676 Find a file by searching the CWD and then the path with a variable set of file
1677 extensions. If the file is not found it will append each extension in the list
jcarseyb3011f42010-01-11 21:49:04 +00001678 in the order provided and return the first one that is successful.
1679
1680 If FileName is NULL, then ASSERT.
1681 If FileExtension is NULL, then behavior is identical to ShellFindFilePath.
1682
1683 If the return value is not NULL then the memory must be caller freed.
1684
1685 @param[in] FileName Filename string.
1686 @param[in] FileExtension Semi-colon delimeted list of possible extensions.
1687
1688 @retval NULL The file was not found.
1689 @retval !NULL The path to the file.
1690**/
1691CHAR16 *
1692EFIAPI
1693ShellFindFilePathEx (
1694 IN CONST CHAR16 *FileName,
1695 IN CONST CHAR16 *FileExtension
1696 )
1697{
1698 CHAR16 *TestPath;
1699 CHAR16 *RetVal;
1700 CONST CHAR16 *ExtensionWalker;
jcarsey9e926b62010-01-14 20:26:39 +00001701 UINTN Size;
jcarsey1cd45e72010-01-29 15:07:44 +00001702 CHAR16 *TempChar;
jcarseyc9d92df2010-02-03 15:37:54 +00001703 CHAR16 *TempChar2;
jcarsey1cd45e72010-01-29 15:07:44 +00001704
jcarseyb3011f42010-01-11 21:49:04 +00001705 ASSERT(FileName != NULL);
1706 if (FileExtension == NULL) {
1707 return (ShellFindFilePath(FileName));
1708 }
1709 RetVal = ShellFindFilePath(FileName);
1710 if (RetVal != NULL) {
1711 return (RetVal);
1712 }
jcarsey9e926b62010-01-14 20:26:39 +00001713 Size = StrSize(FileName);
1714 Size += StrSize(FileExtension);
1715 TestPath = AllocateZeroPool(Size);
jcarseyc9d92df2010-02-03 15:37:54 +00001716 ASSERT(TestPath != NULL);
1717 if (TestPath == NULL) {
1718 return (NULL);
1719 }
jcarseya405b862010-09-14 05:18:09 +00001720 for (ExtensionWalker = FileExtension, TempChar2 = (CHAR16*)FileExtension; TempChar2 != NULL ; ExtensionWalker = TempChar2 + 1){
jcarseyb3011f42010-01-11 21:49:04 +00001721 StrCpy(TestPath, FileName);
jcarseya405b862010-09-14 05:18:09 +00001722 if (ExtensionWalker != NULL) {
1723 StrCat(TestPath, ExtensionWalker);
1724 }
jcarsey1cd45e72010-01-29 15:07:44 +00001725 TempChar = StrStr(TestPath, L";");
1726 if (TempChar != NULL) {
1727 *TempChar = CHAR_NULL;
jcarseyb3011f42010-01-11 21:49:04 +00001728 }
1729 RetVal = ShellFindFilePath(TestPath);
1730 if (RetVal != NULL) {
1731 break;
1732 }
jcarseya405b862010-09-14 05:18:09 +00001733 ASSERT(ExtensionWalker != NULL);
jcarseyc9d92df2010-02-03 15:37:54 +00001734 TempChar2 = StrStr(ExtensionWalker, L";");
jcarseyb3011f42010-01-11 21:49:04 +00001735 }
1736 FreePool(TestPath);
1737 return (RetVal);
1738}
1739
jcarsey94b17fa2009-05-07 18:46:18 +00001740typedef struct {
jcarsey9b3bf082009-06-23 21:15:07 +00001741 LIST_ENTRY Link;
jcarsey94b17fa2009-05-07 18:46:18 +00001742 CHAR16 *Name;
jcarseya405b862010-09-14 05:18:09 +00001743 SHELL_PARAM_TYPE Type;
jcarsey94b17fa2009-05-07 18:46:18 +00001744 CHAR16 *Value;
1745 UINTN OriginalPosition;
1746} SHELL_PARAM_PACKAGE;
1747
1748/**
jcarsey1e6e84c2010-01-25 20:05:08 +00001749 Checks the list of valid arguments and returns TRUE if the item was found. If the
jcarsey94b17fa2009-05-07 18:46:18 +00001750 return value is TRUE then the type parameter is set also.
jcarsey1e6e84c2010-01-25 20:05:08 +00001751
jcarsey94b17fa2009-05-07 18:46:18 +00001752 if CheckList is NULL then ASSERT();
1753 if Name is NULL then ASSERT();
1754 if Type is NULL then ASSERT();
1755
jcarsey94b17fa2009-05-07 18:46:18 +00001756 @param Name pointer to Name of parameter found
1757 @param CheckList List to check against
jcarseya405b862010-09-14 05:18:09 +00001758 @param Type pointer to type of parameter if it was found
jcarsey94b17fa2009-05-07 18:46:18 +00001759
1760 @retval TRUE the Parameter was found. Type is valid.
1761 @retval FALSE the Parameter was not found. Type is not valid.
1762**/
1763BOOLEAN
1764EFIAPI
jcarseyd2b45642009-05-11 18:02:16 +00001765InternalIsOnCheckList (
jcarsey94b17fa2009-05-07 18:46:18 +00001766 IN CONST CHAR16 *Name,
1767 IN CONST SHELL_PARAM_ITEM *CheckList,
jcarseya405b862010-09-14 05:18:09 +00001768 OUT SHELL_PARAM_TYPE *Type
1769 )
1770{
jcarsey94b17fa2009-05-07 18:46:18 +00001771 SHELL_PARAM_ITEM *TempListItem;
1772
1773 //
1774 // ASSERT that all 3 pointer parameters aren't NULL
1775 //
1776 ASSERT(CheckList != NULL);
1777 ASSERT(Type != NULL);
1778 ASSERT(Name != NULL);
1779
1780 //
jcarseyd2b45642009-05-11 18:02:16 +00001781 // question mark and page break mode are always supported
1782 //
1783 if ((StrCmp(Name, L"-?") == 0) ||
1784 (StrCmp(Name, L"-b") == 0)
jcarseya405b862010-09-14 05:18:09 +00001785 ) {
jcarseyd2b45642009-05-11 18:02:16 +00001786 return (TRUE);
1787 }
1788
1789 //
jcarsey94b17fa2009-05-07 18:46:18 +00001790 // Enumerate through the list
1791 //
1792 for (TempListItem = (SHELL_PARAM_ITEM*)CheckList ; TempListItem->Name != NULL ; TempListItem++) {
1793 //
jcarsey9eb53ac2009-07-08 17:26:58 +00001794 // If the Type is TypeStart only check the first characters of the passed in param
1795 // If it matches set the type and return TRUE
jcarsey94b17fa2009-05-07 18:46:18 +00001796 //
jcarsey9eb53ac2009-07-08 17:26:58 +00001797 if (TempListItem->Type == TypeStart && StrnCmp(Name, TempListItem->Name, StrLen(TempListItem->Name)) == 0) {
1798 *Type = TempListItem->Type;
1799 return (TRUE);
1800 } else if (StrCmp(Name, TempListItem->Name) == 0) {
jcarsey94b17fa2009-05-07 18:46:18 +00001801 *Type = TempListItem->Type;
1802 return (TRUE);
1803 }
1804 }
jcarsey2247dde2009-11-09 18:08:58 +00001805
jcarsey94b17fa2009-05-07 18:46:18 +00001806 return (FALSE);
1807}
1808/**
jcarseyd2b45642009-05-11 18:02:16 +00001809 Checks the string for indicators of "flag" status. this is a leading '/', '-', or '+'
jcarsey94b17fa2009-05-07 18:46:18 +00001810
jcarseya405b862010-09-14 05:18:09 +00001811 @param[in] Name pointer to Name of parameter found
1812 @param[in] AlwaysAllowNumbers TRUE to allow numbers, FALSE to not.
jcarsey94b17fa2009-05-07 18:46:18 +00001813
1814 @retval TRUE the Parameter is a flag.
jcarseya405b862010-09-14 05:18:09 +00001815 @retval FALSE the Parameter not a flag.
jcarsey94b17fa2009-05-07 18:46:18 +00001816**/
1817BOOLEAN
1818EFIAPI
jcarseyd2b45642009-05-11 18:02:16 +00001819InternalIsFlag (
jcarsey2247dde2009-11-09 18:08:58 +00001820 IN CONST CHAR16 *Name,
1821 IN BOOLEAN AlwaysAllowNumbers
jcarsey94b17fa2009-05-07 18:46:18 +00001822 )
1823{
1824 //
1825 // ASSERT that Name isn't NULL
1826 //
1827 ASSERT(Name != NULL);
1828
1829 //
jcarsey2247dde2009-11-09 18:08:58 +00001830 // If we accept numbers then dont return TRUE. (they will be values)
1831 //
jcarseya405b862010-09-14 05:18:09 +00001832 if (((Name[0] == L'-' || Name[0] == L'+') && ShellIsHexaDecimalDigitCharacter(Name[1])) && AlwaysAllowNumbers) {
jcarsey2247dde2009-11-09 18:08:58 +00001833 return (FALSE);
1834 }
1835
1836 //
jcarseya405b862010-09-14 05:18:09 +00001837 // If the Name has a /, +, or - as the first character return TRUE
jcarsey94b17fa2009-05-07 18:46:18 +00001838 //
jcarsey1e6e84c2010-01-25 20:05:08 +00001839 if ((Name[0] == L'/') ||
jcarseyd2b45642009-05-11 18:02:16 +00001840 (Name[0] == L'-') ||
1841 (Name[0] == L'+')
jcarseya405b862010-09-14 05:18:09 +00001842 ) {
jcarsey94b17fa2009-05-07 18:46:18 +00001843 return (TRUE);
1844 }
1845 return (FALSE);
1846}
1847
1848/**
jcarsey1e6e84c2010-01-25 20:05:08 +00001849 Checks the command line arguments passed against the list of valid ones.
jcarsey94b17fa2009-05-07 18:46:18 +00001850
1851 If no initialization is required, then return RETURN_SUCCESS.
jcarsey1e6e84c2010-01-25 20:05:08 +00001852
jcarseya405b862010-09-14 05:18:09 +00001853 @param[in] CheckList pointer to list of parameters to check
1854 @param[out] CheckPackage pointer to pointer to list checked values
1855 @param[out] ProblemParam optional pointer to pointer to unicode string for
jcarseyd2b45642009-05-11 18:02:16 +00001856 the paramater that caused failure. If used then the
1857 caller is responsible for freeing the memory.
jcarseya405b862010-09-14 05:18:09 +00001858 @param[in] AutoPageBreak will automatically set PageBreakEnabled for "b" parameter
1859 @param[in] Argv pointer to array of parameters
1860 @param[in] Argc Count of parameters in Argv
1861 @param[in] AlwaysAllowNumbers TRUE to allow numbers always, FALSE otherwise.
jcarsey94b17fa2009-05-07 18:46:18 +00001862
1863 @retval EFI_SUCCESS The operation completed sucessfully.
1864 @retval EFI_OUT_OF_RESOURCES A memory allocation failed
1865 @retval EFI_INVALID_PARAMETER A parameter was invalid
jcarsey1e6e84c2010-01-25 20:05:08 +00001866 @retval EFI_VOLUME_CORRUPTED the command line was corrupt. an argument was
1867 duplicated. the duplicated command line argument
jcarsey94b17fa2009-05-07 18:46:18 +00001868 was returned in ProblemParam if provided.
jcarsey1e6e84c2010-01-25 20:05:08 +00001869 @retval EFI_NOT_FOUND a argument required a value that was missing.
jcarsey94b17fa2009-05-07 18:46:18 +00001870 the invalid command line argument was returned in
1871 ProblemParam if provided.
1872**/
1873EFI_STATUS
1874EFIAPI
1875InternalCommandLineParse (
1876 IN CONST SHELL_PARAM_ITEM *CheckList,
1877 OUT LIST_ENTRY **CheckPackage,
1878 OUT CHAR16 **ProblemParam OPTIONAL,
1879 IN BOOLEAN AutoPageBreak,
1880 IN CONST CHAR16 **Argv,
jcarsey2247dde2009-11-09 18:08:58 +00001881 IN UINTN Argc,
1882 IN BOOLEAN AlwaysAllowNumbers
jcarseya405b862010-09-14 05:18:09 +00001883 )
1884{
jcarsey94b17fa2009-05-07 18:46:18 +00001885 UINTN LoopCounter;
jcarseya405b862010-09-14 05:18:09 +00001886 SHELL_PARAM_TYPE CurrentItemType;
jcarsey94b17fa2009-05-07 18:46:18 +00001887 SHELL_PARAM_PACKAGE *CurrentItemPackage;
jcarsey125c2cf2009-11-18 21:36:50 +00001888 UINTN GetItemValue;
1889 UINTN ValueSize;
jcarseya405b862010-09-14 05:18:09 +00001890 UINTN Count;
jcarsey94b17fa2009-05-07 18:46:18 +00001891
1892 CurrentItemPackage = NULL;
jcarsey125c2cf2009-11-18 21:36:50 +00001893 GetItemValue = 0;
1894 ValueSize = 0;
jcarseya405b862010-09-14 05:18:09 +00001895 Count = 0;
jcarsey94b17fa2009-05-07 18:46:18 +00001896
1897 //
1898 // If there is only 1 item we dont need to do anything
1899 //
jcarseya405b862010-09-14 05:18:09 +00001900 if (Argc < 1) {
jcarsey94b17fa2009-05-07 18:46:18 +00001901 *CheckPackage = NULL;
1902 return (EFI_SUCCESS);
1903 }
1904
1905 //
jcarsey2247dde2009-11-09 18:08:58 +00001906 // ASSERTs
1907 //
1908 ASSERT(CheckList != NULL);
1909 ASSERT(Argv != NULL);
1910
1911 //
jcarsey94b17fa2009-05-07 18:46:18 +00001912 // initialize the linked list
1913 //
1914 *CheckPackage = (LIST_ENTRY*)AllocateZeroPool(sizeof(LIST_ENTRY));
1915 InitializeListHead(*CheckPackage);
1916
1917 //
1918 // loop through each of the arguments
1919 //
1920 for (LoopCounter = 0 ; LoopCounter < Argc ; ++LoopCounter) {
1921 if (Argv[LoopCounter] == NULL) {
1922 //
1923 // do nothing for NULL argv
1924 //
jcarseya405b862010-09-14 05:18:09 +00001925 } else if (InternalIsOnCheckList(Argv[LoopCounter], CheckList, &CurrentItemType)) {
jcarsey94b17fa2009-05-07 18:46:18 +00001926 //
jcarsey2247dde2009-11-09 18:08:58 +00001927 // We might have leftover if last parameter didnt have optional value
1928 //
jcarsey125c2cf2009-11-18 21:36:50 +00001929 if (GetItemValue != 0) {
1930 GetItemValue = 0;
jcarsey2247dde2009-11-09 18:08:58 +00001931 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
1932 }
1933 //
jcarsey94b17fa2009-05-07 18:46:18 +00001934 // this is a flag
1935 //
1936 CurrentItemPackage = AllocatePool(sizeof(SHELL_PARAM_PACKAGE));
1937 ASSERT(CurrentItemPackage != NULL);
1938 CurrentItemPackage->Name = AllocatePool(StrSize(Argv[LoopCounter]));
1939 ASSERT(CurrentItemPackage->Name != NULL);
1940 StrCpy(CurrentItemPackage->Name, Argv[LoopCounter]);
1941 CurrentItemPackage->Type = CurrentItemType;
1942 CurrentItemPackage->OriginalPosition = (UINTN)(-1);
jcarseyb1f95a02009-06-16 00:23:19 +00001943 CurrentItemPackage->Value = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +00001944
1945 //
1946 // Does this flag require a value
1947 //
jcarsey125c2cf2009-11-18 21:36:50 +00001948 switch (CurrentItemPackage->Type) {
jcarsey94b17fa2009-05-07 18:46:18 +00001949 //
jcarsey125c2cf2009-11-18 21:36:50 +00001950 // possibly trigger the next loop(s) to populate the value of this item
jcarsey1e6e84c2010-01-25 20:05:08 +00001951 //
jcarsey125c2cf2009-11-18 21:36:50 +00001952 case TypeValue:
jcarsey1e6e84c2010-01-25 20:05:08 +00001953 GetItemValue = 1;
jcarsey125c2cf2009-11-18 21:36:50 +00001954 ValueSize = 0;
1955 break;
1956 case TypeDoubleValue:
1957 GetItemValue = 2;
1958 ValueSize = 0;
1959 break;
1960 case TypeMaxValue:
1961 GetItemValue = (UINTN)(-1);
1962 ValueSize = 0;
1963 break;
1964 default:
1965 //
1966 // this item has no value expected; we are done
1967 //
1968 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
1969 ASSERT(GetItemValue == 0);
1970 break;
jcarsey94b17fa2009-05-07 18:46:18 +00001971 }
jcarseya405b862010-09-14 05:18:09 +00001972 } else if (GetItemValue != 0 && !InternalIsFlag(Argv[LoopCounter], AlwaysAllowNumbers)) {
jcarseyb1f95a02009-06-16 00:23:19 +00001973 ASSERT(CurrentItemPackage != NULL);
1974 //
jcarsey125c2cf2009-11-18 21:36:50 +00001975 // get the item VALUE for a previous flag
jcarseyb1f95a02009-06-16 00:23:19 +00001976 //
jcarsey125c2cf2009-11-18 21:36:50 +00001977 CurrentItemPackage->Value = ReallocatePool(ValueSize, ValueSize + StrSize(Argv[LoopCounter]) + sizeof(CHAR16), CurrentItemPackage->Value);
jcarseyb1f95a02009-06-16 00:23:19 +00001978 ASSERT(CurrentItemPackage->Value != NULL);
jcarsey125c2cf2009-11-18 21:36:50 +00001979 if (ValueSize == 0) {
1980 StrCpy(CurrentItemPackage->Value, Argv[LoopCounter]);
1981 } else {
1982 StrCat(CurrentItemPackage->Value, L" ");
1983 StrCat(CurrentItemPackage->Value, Argv[LoopCounter]);
1984 }
1985 ValueSize += StrSize(Argv[LoopCounter]) + sizeof(CHAR16);
1986 GetItemValue--;
1987 if (GetItemValue == 0) {
1988 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
1989 }
jcarseya405b862010-09-14 05:18:09 +00001990 } else if (!InternalIsFlag(Argv[LoopCounter], AlwaysAllowNumbers) ){ //|| ProblemParam == NULL) {
jcarseyb1f95a02009-06-16 00:23:19 +00001991 //
1992 // add this one as a non-flag
1993 //
1994 CurrentItemPackage = AllocatePool(sizeof(SHELL_PARAM_PACKAGE));
1995 ASSERT(CurrentItemPackage != NULL);
1996 CurrentItemPackage->Name = NULL;
1997 CurrentItemPackage->Type = TypePosition;
1998 CurrentItemPackage->Value = AllocatePool(StrSize(Argv[LoopCounter]));
1999 ASSERT(CurrentItemPackage->Value != NULL);
2000 StrCpy(CurrentItemPackage->Value, Argv[LoopCounter]);
jcarseya405b862010-09-14 05:18:09 +00002001 CurrentItemPackage->OriginalPosition = Count++;
jcarsey9b3bf082009-06-23 21:15:07 +00002002 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
jcarseya405b862010-09-14 05:18:09 +00002003 } else if (ProblemParam != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00002004 //
2005 // this was a non-recognised flag... error!
2006 //
jcarseyd2b45642009-05-11 18:02:16 +00002007 *ProblemParam = AllocatePool(StrSize(Argv[LoopCounter]));
2008 ASSERT(*ProblemParam != NULL);
2009 StrCpy(*ProblemParam, Argv[LoopCounter]);
jcarsey94b17fa2009-05-07 18:46:18 +00002010 ShellCommandLineFreeVarList(*CheckPackage);
2011 *CheckPackage = NULL;
2012 return (EFI_VOLUME_CORRUPTED);
2013 } else {
jcarseya405b862010-09-14 05:18:09 +00002014 //ASSERT(FALSE);
jcarsey94b17fa2009-05-07 18:46:18 +00002015 }
2016 }
jcarsey125c2cf2009-11-18 21:36:50 +00002017 if (GetItemValue != 0) {
2018 GetItemValue = 0;
2019 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
2020 }
jcarsey94b17fa2009-05-07 18:46:18 +00002021 //
2022 // support for AutoPageBreak
2023 //
2024 if (AutoPageBreak && ShellCommandLineGetFlag(*CheckPackage, L"-b")) {
2025 ShellSetPageBreakMode(TRUE);
2026 }
2027 return (EFI_SUCCESS);
2028}
2029
2030/**
jcarsey1e6e84c2010-01-25 20:05:08 +00002031 Checks the command line arguments passed against the list of valid ones.
jcarsey94b17fa2009-05-07 18:46:18 +00002032 Optionally removes NULL values first.
jcarsey1e6e84c2010-01-25 20:05:08 +00002033
jcarsey94b17fa2009-05-07 18:46:18 +00002034 If no initialization is required, then return RETURN_SUCCESS.
jcarsey1e6e84c2010-01-25 20:05:08 +00002035
jcarseya405b862010-09-14 05:18:09 +00002036 @param[in] CheckList The pointer to list of parameters to check.
2037 @param[out] CheckPackage The package of checked values.
2038 @param[out] ProblemParam Optional pointer to pointer to unicode string for
jcarsey94b17fa2009-05-07 18:46:18 +00002039 the paramater that caused failure.
jcarseya405b862010-09-14 05:18:09 +00002040 @param[in] AutoPageBreak Will automatically set PageBreakEnabled.
2041 @param[in] AlwaysAllowNumbers Will never fail for number based flags.
jcarsey94b17fa2009-05-07 18:46:18 +00002042
2043 @retval EFI_SUCCESS The operation completed sucessfully.
jcarseya405b862010-09-14 05:18:09 +00002044 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
2045 @retval EFI_INVALID_PARAMETER A parameter was invalid.
2046 @retval EFI_VOLUME_CORRUPTED The command line was corrupt.
2047 @retval EFI_DEVICE_ERROR The commands contained 2 opposing arguments. One
jcarsey1e6e84c2010-01-25 20:05:08 +00002048 of the command line arguments was returned in
jcarsey94b17fa2009-05-07 18:46:18 +00002049 ProblemParam if provided.
jcarseya405b862010-09-14 05:18:09 +00002050 @retval EFI_NOT_FOUND A argument required a value that was missing.
2051 The invalid command line argument was returned in
jcarsey94b17fa2009-05-07 18:46:18 +00002052 ProblemParam if provided.
2053**/
2054EFI_STATUS
2055EFIAPI
jcarsey2247dde2009-11-09 18:08:58 +00002056ShellCommandLineParseEx (
jcarsey94b17fa2009-05-07 18:46:18 +00002057 IN CONST SHELL_PARAM_ITEM *CheckList,
2058 OUT LIST_ENTRY **CheckPackage,
2059 OUT CHAR16 **ProblemParam OPTIONAL,
jcarsey2247dde2009-11-09 18:08:58 +00002060 IN BOOLEAN AutoPageBreak,
2061 IN BOOLEAN AlwaysAllowNumbers
jcarseya405b862010-09-14 05:18:09 +00002062 )
2063{
jcarsey1e6e84c2010-01-25 20:05:08 +00002064 //
jcarsey94b17fa2009-05-07 18:46:18 +00002065 // ASSERT that CheckList and CheckPackage aren't NULL
2066 //
2067 ASSERT(CheckList != NULL);
2068 ASSERT(CheckPackage != NULL);
2069
jcarsey1e6e84c2010-01-25 20:05:08 +00002070 //
jcarsey94b17fa2009-05-07 18:46:18 +00002071 // Check for UEFI Shell 2.0 protocols
2072 //
2073 if (mEfiShellParametersProtocol != NULL) {
jcarsey1e6e84c2010-01-25 20:05:08 +00002074 return (InternalCommandLineParse(CheckList,
2075 CheckPackage,
2076 ProblemParam,
2077 AutoPageBreak,
jljusten08d7f8e2009-06-15 18:42:13 +00002078 (CONST CHAR16**) mEfiShellParametersProtocol->Argv,
jcarsey2247dde2009-11-09 18:08:58 +00002079 mEfiShellParametersProtocol->Argc,
2080 AlwaysAllowNumbers));
jcarsey94b17fa2009-05-07 18:46:18 +00002081 }
2082
jcarsey1e6e84c2010-01-25 20:05:08 +00002083 //
jcarsey94b17fa2009-05-07 18:46:18 +00002084 // ASSERT That EFI Shell is not required
2085 //
2086 ASSERT (mEfiShellInterface != NULL);
jcarsey1e6e84c2010-01-25 20:05:08 +00002087 return (InternalCommandLineParse(CheckList,
2088 CheckPackage,
2089 ProblemParam,
2090 AutoPageBreak,
jljusten08d7f8e2009-06-15 18:42:13 +00002091 (CONST CHAR16**) mEfiShellInterface->Argv,
jcarsey2247dde2009-11-09 18:08:58 +00002092 mEfiShellInterface->Argc,
2093 AlwaysAllowNumbers));
jcarsey94b17fa2009-05-07 18:46:18 +00002094}
2095
2096/**
2097 Frees shell variable list that was returned from ShellCommandLineParse.
2098
2099 This function will free all the memory that was used for the CheckPackage
2100 list of postprocessed shell arguments.
2101
2102 this function has no return value.
2103
2104 if CheckPackage is NULL, then return
2105
2106 @param CheckPackage the list to de-allocate
2107 **/
2108VOID
2109EFIAPI
2110ShellCommandLineFreeVarList (
2111 IN LIST_ENTRY *CheckPackage
jcarseya405b862010-09-14 05:18:09 +00002112 )
2113{
jcarsey94b17fa2009-05-07 18:46:18 +00002114 LIST_ENTRY *Node;
2115
2116 //
2117 // check for CheckPackage == NULL
2118 //
2119 if (CheckPackage == NULL) {
2120 return;
2121 }
2122
2123 //
2124 // for each node in the list
2125 //
jcarsey9eb53ac2009-07-08 17:26:58 +00002126 for ( Node = GetFirstNode(CheckPackage)
jcarseya405b862010-09-14 05:18:09 +00002127 ; !IsListEmpty(CheckPackage)
jcarsey9eb53ac2009-07-08 17:26:58 +00002128 ; Node = GetFirstNode(CheckPackage)
jcarseya405b862010-09-14 05:18:09 +00002129 ){
jcarsey94b17fa2009-05-07 18:46:18 +00002130 //
2131 // Remove it from the list
2132 //
2133 RemoveEntryList(Node);
2134
2135 //
2136 // if it has a name free the name
2137 //
2138 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {
2139 FreePool(((SHELL_PARAM_PACKAGE*)Node)->Name);
2140 }
2141
2142 //
2143 // if it has a value free the value
2144 //
2145 if (((SHELL_PARAM_PACKAGE*)Node)->Value != NULL) {
2146 FreePool(((SHELL_PARAM_PACKAGE*)Node)->Value);
2147 }
jcarsey1e6e84c2010-01-25 20:05:08 +00002148
jcarsey94b17fa2009-05-07 18:46:18 +00002149 //
2150 // free the node structure
2151 //
2152 FreePool((SHELL_PARAM_PACKAGE*)Node);
2153 }
2154 //
2155 // free the list head node
2156 //
2157 FreePool(CheckPackage);
2158}
2159/**
2160 Checks for presence of a flag parameter
2161
2162 flag arguments are in the form of "-<Key>" or "/<Key>", but do not have a value following the key
2163
2164 if CheckPackage is NULL then return FALSE.
2165 if KeyString is NULL then ASSERT()
jcarsey1e6e84c2010-01-25 20:05:08 +00002166
jcarsey94b17fa2009-05-07 18:46:18 +00002167 @param CheckPackage The package of parsed command line arguments
2168 @param KeyString the Key of the command line argument to check for
2169
2170 @retval TRUE the flag is on the command line
2171 @retval FALSE the flag is not on the command line
2172 **/
2173BOOLEAN
2174EFIAPI
2175ShellCommandLineGetFlag (
jcarseya405b862010-09-14 05:18:09 +00002176 IN CONST LIST_ENTRY * CONST CheckPackage,
2177 IN CONST CHAR16 * CONST KeyString
2178 )
2179{
jcarsey94b17fa2009-05-07 18:46:18 +00002180 LIST_ENTRY *Node;
2181
2182 //
2183 // ASSERT that both CheckPackage and KeyString aren't NULL
2184 //
2185 ASSERT(KeyString != NULL);
2186
2187 //
2188 // return FALSE for no package
2189 //
2190 if (CheckPackage == NULL) {
2191 return (FALSE);
2192 }
2193
2194 //
2195 // enumerate through the list of parametrs
2196 //
jcarsey1e6e84c2010-01-25 20:05:08 +00002197 for ( Node = GetFirstNode(CheckPackage)
2198 ; !IsNull (CheckPackage, Node)
2199 ; Node = GetNextNode(CheckPackage, Node)
jcarseya405b862010-09-14 05:18:09 +00002200 ){
jcarsey94b17fa2009-05-07 18:46:18 +00002201 //
2202 // If the Name matches, return TRUE (and there may be NULL name)
2203 //
2204 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {
jcarsey9eb53ac2009-07-08 17:26:58 +00002205 //
2206 // If Type is TypeStart then only compare the begining of the strings
2207 //
jcarsey1e6e84c2010-01-25 20:05:08 +00002208 if ( ((SHELL_PARAM_PACKAGE*)Node)->Type == TypeStart
jcarsey9eb53ac2009-07-08 17:26:58 +00002209 && StrnCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name, StrLen(KeyString)) == 0
jcarseya405b862010-09-14 05:18:09 +00002210 ){
jcarsey9eb53ac2009-07-08 17:26:58 +00002211 return (TRUE);
2212 } else if (StrCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {
jcarsey94b17fa2009-05-07 18:46:18 +00002213 return (TRUE);
2214 }
2215 }
2216 }
2217 return (FALSE);
2218}
2219/**
jcarseya405b862010-09-14 05:18:09 +00002220 Returns value from command line argument.
jcarsey94b17fa2009-05-07 18:46:18 +00002221
jcarseya405b862010-09-14 05:18:09 +00002222 Value parameters are in the form of "-<Key> value" or "/<Key> value".
jcarsey1e6e84c2010-01-25 20:05:08 +00002223
jcarseya405b862010-09-14 05:18:09 +00002224 If CheckPackage is NULL, then return NULL.
jcarsey94b17fa2009-05-07 18:46:18 +00002225
jcarseya405b862010-09-14 05:18:09 +00002226 @param[in] CheckPackage The package of parsed command line arguments.
2227 @param[in] KeyString The Key of the command line argument to check for.
jcarsey94b17fa2009-05-07 18:46:18 +00002228
jcarseya405b862010-09-14 05:18:09 +00002229 @retval NULL The flag is not on the command line.
2230 @retval !=NULL The pointer to unicode string of the value.
2231**/
jcarsey94b17fa2009-05-07 18:46:18 +00002232CONST CHAR16*
2233EFIAPI
2234ShellCommandLineGetValue (
2235 IN CONST LIST_ENTRY *CheckPackage,
2236 IN CHAR16 *KeyString
jcarseya405b862010-09-14 05:18:09 +00002237 )
2238{
jcarsey94b17fa2009-05-07 18:46:18 +00002239 LIST_ENTRY *Node;
2240
2241 //
2242 // check for CheckPackage == NULL
2243 //
2244 if (CheckPackage == NULL) {
2245 return (NULL);
2246 }
2247
2248 //
2249 // enumerate through the list of parametrs
2250 //
jcarsey1e6e84c2010-01-25 20:05:08 +00002251 for ( Node = GetFirstNode(CheckPackage)
2252 ; !IsNull (CheckPackage, Node)
2253 ; Node = GetNextNode(CheckPackage, Node)
jcarseya405b862010-09-14 05:18:09 +00002254 ){
jcarsey94b17fa2009-05-07 18:46:18 +00002255 //
2256 // If the Name matches, return the value (name can be NULL)
2257 //
2258 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {
jcarsey9eb53ac2009-07-08 17:26:58 +00002259 //
2260 // If Type is TypeStart then only compare the begining of the strings
2261 //
jcarsey1e6e84c2010-01-25 20:05:08 +00002262 if ( ((SHELL_PARAM_PACKAGE*)Node)->Type == TypeStart
jcarsey9eb53ac2009-07-08 17:26:58 +00002263 && StrnCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name, StrLen(KeyString)) == 0
jcarseya405b862010-09-14 05:18:09 +00002264 ){
jcarsey9eb53ac2009-07-08 17:26:58 +00002265 //
2266 // return the string part after the flag
2267 //
2268 return (((SHELL_PARAM_PACKAGE*)Node)->Name + StrLen(KeyString));
2269 } else if (StrCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {
2270 //
2271 // return the value
2272 //
jcarsey94b17fa2009-05-07 18:46:18 +00002273 return (((SHELL_PARAM_PACKAGE*)Node)->Value);
2274 }
2275 }
2276 }
2277 return (NULL);
2278}
jcarseya405b862010-09-14 05:18:09 +00002279
jcarsey94b17fa2009-05-07 18:46:18 +00002280/**
jcarseya405b862010-09-14 05:18:09 +00002281 Returns raw value from command line argument.
jcarsey94b17fa2009-05-07 18:46:18 +00002282
jcarseya405b862010-09-14 05:18:09 +00002283 Raw value parameters are in the form of "value" in a specific position in the list.
jcarsey1e6e84c2010-01-25 20:05:08 +00002284
jcarseya405b862010-09-14 05:18:09 +00002285 If CheckPackage is NULL, then return NULL.
jcarsey94b17fa2009-05-07 18:46:18 +00002286
jcarseya405b862010-09-14 05:18:09 +00002287 @param[in] CheckPackage The package of parsed command line arguments.
2288 @param[in] Position The position of the value.
jcarsey94b17fa2009-05-07 18:46:18 +00002289
jcarseya405b862010-09-14 05:18:09 +00002290 @retval NULL The flag is not on the command line.
2291 @retval !=NULL The pointer to unicode string of the value.
jcarsey94b17fa2009-05-07 18:46:18 +00002292 **/
2293CONST CHAR16*
2294EFIAPI
2295ShellCommandLineGetRawValue (
jcarseya405b862010-09-14 05:18:09 +00002296 IN CONST LIST_ENTRY * CONST CheckPackage,
2297 IN UINTN Position
2298 )
2299{
jcarsey94b17fa2009-05-07 18:46:18 +00002300 LIST_ENTRY *Node;
2301
2302 //
2303 // check for CheckPackage == NULL
2304 //
2305 if (CheckPackage == NULL) {
2306 return (NULL);
2307 }
2308
2309 //
2310 // enumerate through the list of parametrs
2311 //
jcarsey1e6e84c2010-01-25 20:05:08 +00002312 for ( Node = GetFirstNode(CheckPackage)
2313 ; !IsNull (CheckPackage, Node)
2314 ; Node = GetNextNode(CheckPackage, Node)
jcarseya405b862010-09-14 05:18:09 +00002315 ){
jcarsey94b17fa2009-05-07 18:46:18 +00002316 //
2317 // If the position matches, return the value
2318 //
2319 if (((SHELL_PARAM_PACKAGE*)Node)->OriginalPosition == Position) {
2320 return (((SHELL_PARAM_PACKAGE*)Node)->Value);
2321 }
2322 }
2323 return (NULL);
jcarseyb1f95a02009-06-16 00:23:19 +00002324}
jcarsey2247dde2009-11-09 18:08:58 +00002325
2326/**
jcarsey1e6e84c2010-01-25 20:05:08 +00002327 returns the number of command line value parameters that were parsed.
2328
jcarsey2247dde2009-11-09 18:08:58 +00002329 this will not include flags.
2330
jcarseya405b862010-09-14 05:18:09 +00002331 @param[in] CheckPackage The package of parsed command line arguments.
2332
jcarsey2247dde2009-11-09 18:08:58 +00002333 @retval (UINTN)-1 No parsing has ocurred
2334 @return other The number of value parameters found
2335**/
2336UINTN
2337EFIAPI
2338ShellCommandLineGetCount(
jcarseya405b862010-09-14 05:18:09 +00002339 IN CONST LIST_ENTRY *CheckPackage
jcarsey125c2cf2009-11-18 21:36:50 +00002340 )
2341{
jcarseya405b862010-09-14 05:18:09 +00002342 LIST_ENTRY *Node1;
2343 UINTN Count;
2344
2345 if (CheckPackage == NULL) {
2346 return (0);
2347 }
2348 for ( Node1 = GetFirstNode(CheckPackage), Count = 0
2349 ; !IsNull (CheckPackage, Node1)
2350 ; Node1 = GetNextNode(CheckPackage, Node1)
2351 ){
2352 if (((SHELL_PARAM_PACKAGE*)Node1)->Name == NULL) {
2353 Count++;
2354 }
2355 }
2356 return (Count);
jcarsey2247dde2009-11-09 18:08:58 +00002357}
2358
jcarsey975136a2009-06-16 19:03:54 +00002359/**
jcarsey36a9d672009-11-20 21:13:41 +00002360 Determins if a parameter is duplicated.
2361
jcarsey1e6e84c2010-01-25 20:05:08 +00002362 If Param is not NULL then it will point to a callee allocated string buffer
jcarsey36a9d672009-11-20 21:13:41 +00002363 with the parameter value if a duplicate is found.
2364
2365 If CheckPackage is NULL, then ASSERT.
2366
2367 @param[in] CheckPackage The package of parsed command line arguments.
2368 @param[out] Param Upon finding one, a pointer to the duplicated parameter.
2369
2370 @retval EFI_SUCCESS No parameters were duplicated.
2371 @retval EFI_DEVICE_ERROR A duplicate was found.
2372 **/
2373EFI_STATUS
2374EFIAPI
2375ShellCommandLineCheckDuplicate (
2376 IN CONST LIST_ENTRY *CheckPackage,
2377 OUT CHAR16 **Param
2378 )
2379{
2380 LIST_ENTRY *Node1;
2381 LIST_ENTRY *Node2;
jcarsey1e6e84c2010-01-25 20:05:08 +00002382
jcarsey36a9d672009-11-20 21:13:41 +00002383 ASSERT(CheckPackage != NULL);
2384
jcarsey1e6e84c2010-01-25 20:05:08 +00002385 for ( Node1 = GetFirstNode(CheckPackage)
2386 ; !IsNull (CheckPackage, Node1)
2387 ; Node1 = GetNextNode(CheckPackage, Node1)
jcarseya405b862010-09-14 05:18:09 +00002388 ){
jcarsey1e6e84c2010-01-25 20:05:08 +00002389 for ( Node2 = GetNextNode(CheckPackage, Node1)
2390 ; !IsNull (CheckPackage, Node2)
2391 ; Node2 = GetNextNode(CheckPackage, Node2)
jcarseya405b862010-09-14 05:18:09 +00002392 ){
2393 if ((((SHELL_PARAM_PACKAGE*)Node1)->Name != NULL) && (((SHELL_PARAM_PACKAGE*)Node2)->Name != NULL) && StrCmp(((SHELL_PARAM_PACKAGE*)Node1)->Name, ((SHELL_PARAM_PACKAGE*)Node2)->Name) == 0) {
jcarsey36a9d672009-11-20 21:13:41 +00002394 if (Param != NULL) {
2395 *Param = NULL;
2396 *Param = StrnCatGrow(Param, NULL, ((SHELL_PARAM_PACKAGE*)Node1)->Name, 0);
2397 }
2398 return (EFI_DEVICE_ERROR);
2399 }
2400 }
2401 }
2402 return (EFI_SUCCESS);
2403}
2404
2405/**
jcarsey1e6e84c2010-01-25 20:05:08 +00002406 This is a find and replace function. Upon successful return the NewString is a copy of
jcarsey975136a2009-06-16 19:03:54 +00002407 SourceString with each instance of FindTarget replaced with ReplaceWith.
2408
jcarseyb3011f42010-01-11 21:49:04 +00002409 If SourceString and NewString overlap the behavior is undefined.
2410
jcarsey975136a2009-06-16 19:03:54 +00002411 If the string would grow bigger than NewSize it will halt and return error.
2412
jcarseya405b862010-09-14 05:18:09 +00002413 @param[in] SourceString The string with source buffer.
2414 @param[in,out] NewString The string with resultant buffer.
2415 @param[in] NewSize The size in bytes of NewString.
2416 @param[in] FindTarget The string to look for.
2417 @param[in] ReplaceWith The string to replace FindTarget with.
jcarsey969c7832010-01-13 16:46:33 +00002418 @param[in] SkipPreCarrot If TRUE will skip a FindTarget that has a '^'
2419 immediately before it.
jcarseya405b862010-09-14 05:18:09 +00002420 @param[in] ParameterReplacing If TRUE will add "" around items with spaces.
jcarsey975136a2009-06-16 19:03:54 +00002421
jcarsey969c7832010-01-13 16:46:33 +00002422 @retval EFI_INVALID_PARAMETER SourceString was NULL.
2423 @retval EFI_INVALID_PARAMETER NewString was NULL.
2424 @retval EFI_INVALID_PARAMETER FindTarget was NULL.
2425 @retval EFI_INVALID_PARAMETER ReplaceWith was NULL.
2426 @retval EFI_INVALID_PARAMETER FindTarget had length < 1.
2427 @retval EFI_INVALID_PARAMETER SourceString had length < 1.
jcarsey1e6e84c2010-01-25 20:05:08 +00002428 @retval EFI_BUFFER_TOO_SMALL NewSize was less than the minimum size to hold
jcarsey969c7832010-01-13 16:46:33 +00002429 the new string (truncation occurred).
jcarseya405b862010-09-14 05:18:09 +00002430 @retval EFI_SUCCESS The string was successfully copied with replacement.
jcarsey975136a2009-06-16 19:03:54 +00002431**/
jcarsey975136a2009-06-16 19:03:54 +00002432EFI_STATUS
2433EFIAPI
jcarseya405b862010-09-14 05:18:09 +00002434ShellCopySearchAndReplace(
jcarsey975136a2009-06-16 19:03:54 +00002435 IN CHAR16 CONST *SourceString,
jcarseya405b862010-09-14 05:18:09 +00002436 IN OUT CHAR16 *NewString,
jcarsey975136a2009-06-16 19:03:54 +00002437 IN UINTN NewSize,
2438 IN CONST CHAR16 *FindTarget,
jcarsey969c7832010-01-13 16:46:33 +00002439 IN CONST CHAR16 *ReplaceWith,
jcarseya405b862010-09-14 05:18:09 +00002440 IN CONST BOOLEAN SkipPreCarrot,
2441 IN CONST BOOLEAN ParameterReplacing
jcarsey1e6e84c2010-01-25 20:05:08 +00002442 )
jcarsey2247dde2009-11-09 18:08:58 +00002443{
jcarsey01582942009-07-10 19:46:17 +00002444 UINTN Size;
jcarseya405b862010-09-14 05:18:09 +00002445 CHAR16 *Replace;
2446
jcarsey975136a2009-06-16 19:03:54 +00002447 if ( (SourceString == NULL)
2448 || (NewString == NULL)
2449 || (FindTarget == NULL)
2450 || (ReplaceWith == NULL)
2451 || (StrLen(FindTarget) < 1)
2452 || (StrLen(SourceString) < 1)
jcarseya405b862010-09-14 05:18:09 +00002453 ){
jcarsey975136a2009-06-16 19:03:54 +00002454 return (EFI_INVALID_PARAMETER);
2455 }
jcarseya405b862010-09-14 05:18:09 +00002456 Replace = NULL;
2457 if (StrStr(ReplaceWith, L" ") == NULL || !ParameterReplacing) {
2458 Replace = StrnCatGrow(&Replace, NULL, ReplaceWith, 0);
2459 } else {
2460 Replace = AllocateZeroPool(StrSize(ReplaceWith) + 2*sizeof(CHAR16));
2461 UnicodeSPrint(Replace, StrSize(ReplaceWith) + 2*sizeof(CHAR16), L"\"%s\"", ReplaceWith);
2462 }
jcarsey2247dde2009-11-09 18:08:58 +00002463 NewString = SetMem16(NewString, NewSize, CHAR_NULL);
2464 while (*SourceString != CHAR_NULL) {
jcarsey969c7832010-01-13 16:46:33 +00002465 //
jcarseya405b862010-09-14 05:18:09 +00002466 // if we find the FindTarget and either Skip == FALSE or Skip and we
jcarsey969c7832010-01-13 16:46:33 +00002467 // dont have a carrot do a replace...
2468 //
jcarsey1e6e84c2010-01-25 20:05:08 +00002469 if (StrnCmp(SourceString, FindTarget, StrLen(FindTarget)) == 0
jcarseya405b862010-09-14 05:18:09 +00002470 && ((SkipPreCarrot && *(SourceString-1) != L'^') || !SkipPreCarrot)
2471 ){
jcarsey975136a2009-06-16 19:03:54 +00002472 SourceString += StrLen(FindTarget);
jcarsey01582942009-07-10 19:46:17 +00002473 Size = StrSize(NewString);
jcarseya405b862010-09-14 05:18:09 +00002474 if ((Size + (StrLen(Replace)*sizeof(CHAR16))) > NewSize) {
2475 FreePool(Replace);
jcarsey975136a2009-06-16 19:03:54 +00002476 return (EFI_BUFFER_TOO_SMALL);
2477 }
jcarseya405b862010-09-14 05:18:09 +00002478 StrCat(NewString, Replace);
jcarsey975136a2009-06-16 19:03:54 +00002479 } else {
jcarsey01582942009-07-10 19:46:17 +00002480 Size = StrSize(NewString);
2481 if (Size + sizeof(CHAR16) > NewSize) {
jcarseya405b862010-09-14 05:18:09 +00002482 FreePool(Replace);
jcarsey975136a2009-06-16 19:03:54 +00002483 return (EFI_BUFFER_TOO_SMALL);
2484 }
2485 StrnCat(NewString, SourceString, 1);
2486 SourceString++;
2487 }
2488 }
jcarseya405b862010-09-14 05:18:09 +00002489 FreePool(Replace);
jcarsey975136a2009-06-16 19:03:54 +00002490 return (EFI_SUCCESS);
2491}
jcarseyb1f95a02009-06-16 00:23:19 +00002492
2493/**
jcarseye2f82972009-12-01 05:40:24 +00002494 Internal worker function to output a string.
2495
2496 This function will output a string to the correct StdOut.
2497
2498 @param[in] String The string to print out.
2499
2500 @retval EFI_SUCCESS The operation was sucessful.
2501 @retval !EFI_SUCCESS The operation failed.
2502**/
2503EFI_STATUS
2504EFIAPI
2505InternalPrintTo (
2506 IN CONST CHAR16 *String
2507 )
2508{
2509 UINTN Size;
2510 Size = StrSize(String) - sizeof(CHAR16);
jcarseya405b862010-09-14 05:18:09 +00002511 if (Size == 0) {
2512 return (EFI_SUCCESS);
2513 }
jcarseye2f82972009-12-01 05:40:24 +00002514 if (mEfiShellParametersProtocol != NULL) {
jcarseya405b862010-09-14 05:18:09 +00002515 return (mEfiShellProtocol->WriteFile(mEfiShellParametersProtocol->StdOut, &Size, (VOID*)String));
jcarseye2f82972009-12-01 05:40:24 +00002516 }
2517 if (mEfiShellInterface != NULL) {
jcarseyecd3d592009-12-07 18:05:00 +00002518 //
2519 // Divide in half for old shell. Must be string length not size.
2520 //
2521 Size /= 2;
jcarseya405b862010-09-14 05:18:09 +00002522 return (mEfiShellInterface->StdOut->Write(mEfiShellInterface->StdOut, &Size, (VOID*)String));
jcarseye2f82972009-12-01 05:40:24 +00002523 }
2524 ASSERT(FALSE);
2525 return (EFI_UNSUPPORTED);
2526}
2527
2528/**
jcarseyb1f95a02009-06-16 00:23:19 +00002529 Print at a specific location on the screen.
2530
jcarseyf1b87e72009-06-17 00:52:11 +00002531 This function will move the cursor to a given screen location and print the specified string
jcarsey1e6e84c2010-01-25 20:05:08 +00002532
2533 If -1 is specified for either the Row or Col the current screen location for BOTH
jcarseyf1b87e72009-06-17 00:52:11 +00002534 will be used.
jcarseyb1f95a02009-06-16 00:23:19 +00002535
2536 if either Row or Col is out of range for the current console, then ASSERT
2537 if Format is NULL, then ASSERT
2538
jcarsey1e6e84c2010-01-25 20:05:08 +00002539 In addition to the standard %-based flags as supported by UefiLib Print() this supports
jcarseyb1f95a02009-06-16 00:23:19 +00002540 the following additional flags:
2541 %N - Set output attribute to normal
2542 %H - Set output attribute to highlight
2543 %E - Set output attribute to error
2544 %B - Set output attribute to blue color
2545 %V - Set output attribute to green color
2546
2547 Note: The background color is controlled by the shell command cls.
2548
2549 @param[in] Row the row to print at
2550 @param[in] Col the column to print at
2551 @param[in] Format the format string
jcarsey2247dde2009-11-09 18:08:58 +00002552 @param[in] Marker the marker for the variable argument list
jcarseyb1f95a02009-06-16 00:23:19 +00002553
jcarseya405b862010-09-14 05:18:09 +00002554 @return EFI_SUCCESS The operation was successful.
2555 @return EFI_DEVICE_ERROR The console device reported an error.
jcarseyb1f95a02009-06-16 00:23:19 +00002556**/
jcarseya405b862010-09-14 05:18:09 +00002557EFI_STATUS
jcarseyb1f95a02009-06-16 00:23:19 +00002558EFIAPI
jcarsey2247dde2009-11-09 18:08:58 +00002559InternalShellPrintWorker(
jcarseyb1f95a02009-06-16 00:23:19 +00002560 IN INT32 Col OPTIONAL,
2561 IN INT32 Row OPTIONAL,
2562 IN CONST CHAR16 *Format,
jcarsey2247dde2009-11-09 18:08:58 +00002563 VA_LIST Marker
jcarsey1e6e84c2010-01-25 20:05:08 +00002564 )
jcarsey2247dde2009-11-09 18:08:58 +00002565{
jcarseyb1f95a02009-06-16 00:23:19 +00002566 EFI_STATUS Status;
jcarsey975136a2009-06-16 19:03:54 +00002567 CHAR16 *ResumeLocation;
2568 CHAR16 *FormatWalker;
jcarseya405b862010-09-14 05:18:09 +00002569 UINTN OriginalAttribute;
2570
2571 Status = EFI_SUCCESS;
2572 OriginalAttribute = gST->ConOut->Mode->Attribute;
jcarsey1e6e84c2010-01-25 20:05:08 +00002573
jcarsey975136a2009-06-16 19:03:54 +00002574 //
2575 // Back and forth each time fixing up 1 of our flags...
2576 //
jcarseya405b862010-09-14 05:18:09 +00002577 Status = ShellCopySearchAndReplace(Format, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%N", L"%%N", FALSE, FALSE);
jcarsey975136a2009-06-16 19:03:54 +00002578 ASSERT_EFI_ERROR(Status);
jcarseya405b862010-09-14 05:18:09 +00002579 Status = ShellCopySearchAndReplace(mPostReplaceFormat, mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), L"%E", L"%%E", FALSE, FALSE);
jcarsey975136a2009-06-16 19:03:54 +00002580 ASSERT_EFI_ERROR(Status);
jcarseya405b862010-09-14 05:18:09 +00002581 Status = ShellCopySearchAndReplace(mPostReplaceFormat2, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%H", L"%%H", FALSE, FALSE);
jcarsey975136a2009-06-16 19:03:54 +00002582 ASSERT_EFI_ERROR(Status);
jcarseya405b862010-09-14 05:18:09 +00002583 Status = ShellCopySearchAndReplace(mPostReplaceFormat, mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), L"%B", L"%%B", FALSE, FALSE);
jcarsey975136a2009-06-16 19:03:54 +00002584 ASSERT_EFI_ERROR(Status);
jcarseya405b862010-09-14 05:18:09 +00002585 Status = ShellCopySearchAndReplace(mPostReplaceFormat2, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%V", L"%%V", FALSE, FALSE);
jcarsey975136a2009-06-16 19:03:54 +00002586 ASSERT_EFI_ERROR(Status);
2587
2588 //
2589 // Use the last buffer from replacing to print from...
2590 //
jcarseya405b862010-09-14 05:18:09 +00002591 UnicodeVSPrint (mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), mPostReplaceFormat, Marker);
jcarseyb1f95a02009-06-16 00:23:19 +00002592
2593 if (Col != -1 && Row != -1) {
jcarseyb1f95a02009-06-16 00:23:19 +00002594 Status = gST->ConOut->SetCursorPosition(gST->ConOut, Col, Row);
2595 ASSERT_EFI_ERROR(Status);
jcarsey975136a2009-06-16 19:03:54 +00002596 }
2597
jcarseyecd3d592009-12-07 18:05:00 +00002598 FormatWalker = mPostReplaceFormat2;
jcarsey2247dde2009-11-09 18:08:58 +00002599 while (*FormatWalker != CHAR_NULL) {
jcarsey975136a2009-06-16 19:03:54 +00002600 //
2601 // Find the next attribute change request
2602 //
2603 ResumeLocation = StrStr(FormatWalker, L"%");
2604 if (ResumeLocation != NULL) {
jcarsey2247dde2009-11-09 18:08:58 +00002605 *ResumeLocation = CHAR_NULL;
jcarsey975136a2009-06-16 19:03:54 +00002606 }
2607 //
2608 // print the current FormatWalker string
2609 //
jcarseya405b862010-09-14 05:18:09 +00002610 if (StrLen(FormatWalker)>0) {
2611 Status = InternalPrintTo(FormatWalker);
2612 if (EFI_ERROR(Status)) {
2613 break;
2614 }
2615 }
2616
jcarsey975136a2009-06-16 19:03:54 +00002617 //
2618 // update the attribute
2619 //
2620 if (ResumeLocation != NULL) {
2621 switch (*(ResumeLocation+1)) {
2622 case (L'N'):
jcarseya405b862010-09-14 05:18:09 +00002623 gST->ConOut->SetAttribute(gST->ConOut, OriginalAttribute);
jcarsey975136a2009-06-16 19:03:54 +00002624 break;
2625 case (L'E'):
jcarseya405b862010-09-14 05:18:09 +00002626 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_YELLOW, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));
jcarsey975136a2009-06-16 19:03:54 +00002627 break;
2628 case (L'H'):
jcarseya405b862010-09-14 05:18:09 +00002629 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_WHITE, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));
jcarsey975136a2009-06-16 19:03:54 +00002630 break;
2631 case (L'B'):
jcarseya405b862010-09-14 05:18:09 +00002632 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_BLUE, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));
jcarsey975136a2009-06-16 19:03:54 +00002633 break;
2634 case (L'V'):
jcarseya405b862010-09-14 05:18:09 +00002635 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_GREEN, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));
jcarsey975136a2009-06-16 19:03:54 +00002636 break;
2637 default:
jcarseye2f82972009-12-01 05:40:24 +00002638 //
2639 // Print a simple '%' symbol
2640 //
2641 Status = InternalPrintTo(L"%");
jcarseya405b862010-09-14 05:18:09 +00002642 if (EFI_ERROR(Status)) {
2643 break;
2644 }
jcarseye2f82972009-12-01 05:40:24 +00002645 ResumeLocation = ResumeLocation - 1;
jcarsey975136a2009-06-16 19:03:54 +00002646 break;
2647 }
2648 } else {
2649 //
2650 // reset to normal now...
2651 //
jcarsey975136a2009-06-16 19:03:54 +00002652 break;
2653 }
2654
2655 //
2656 // update FormatWalker to Resume + 2 (skip the % and the indicator)
2657 //
2658 FormatWalker = ResumeLocation + 2;
2659 }
jcarseyb1f95a02009-06-16 00:23:19 +00002660
jcarseya405b862010-09-14 05:18:09 +00002661 gST->ConOut->SetAttribute(gST->ConOut, OriginalAttribute);
2662 return (Status);
jcarsey5f7431d2009-07-10 18:06:01 +00002663}
jcarsey2247dde2009-11-09 18:08:58 +00002664
2665/**
2666 Print at a specific location on the screen.
2667
jcarseye2f82972009-12-01 05:40:24 +00002668 This function will move the cursor to a given screen location and print the specified string.
jcarsey1e6e84c2010-01-25 20:05:08 +00002669
2670 If -1 is specified for either the Row or Col the current screen location for BOTH
jcarsey2247dde2009-11-09 18:08:58 +00002671 will be used.
2672
jcarseye2f82972009-12-01 05:40:24 +00002673 If either Row or Col is out of range for the current console, then ASSERT.
2674 If Format is NULL, then ASSERT.
jcarsey2247dde2009-11-09 18:08:58 +00002675
jcarsey1e6e84c2010-01-25 20:05:08 +00002676 In addition to the standard %-based flags as supported by UefiLib Print() this supports
jcarsey2247dde2009-11-09 18:08:58 +00002677 the following additional flags:
2678 %N - Set output attribute to normal
2679 %H - Set output attribute to highlight
2680 %E - Set output attribute to error
2681 %B - Set output attribute to blue color
2682 %V - Set output attribute to green color
2683
2684 Note: The background color is controlled by the shell command cls.
2685
jcarsey2247dde2009-11-09 18:08:58 +00002686 @param[in] Col the column to print at
jcarseya405b862010-09-14 05:18:09 +00002687 @param[in] Row the row to print at
jcarsey2247dde2009-11-09 18:08:58 +00002688 @param[in] Format the format string
jcarseya405b862010-09-14 05:18:09 +00002689 @param[in] ... The variable argument list.
jcarsey2247dde2009-11-09 18:08:58 +00002690
jcarseya405b862010-09-14 05:18:09 +00002691 @return EFI_SUCCESS The printing was successful.
2692 @return EFI_DEVICE_ERROR The console device reported an error.
jcarsey2247dde2009-11-09 18:08:58 +00002693**/
jcarseya405b862010-09-14 05:18:09 +00002694EFI_STATUS
jcarsey2247dde2009-11-09 18:08:58 +00002695EFIAPI
2696ShellPrintEx(
2697 IN INT32 Col OPTIONAL,
2698 IN INT32 Row OPTIONAL,
2699 IN CONST CHAR16 *Format,
2700 ...
jcarsey1e6e84c2010-01-25 20:05:08 +00002701 )
jcarsey2247dde2009-11-09 18:08:58 +00002702{
2703 VA_LIST Marker;
jcarseya405b862010-09-14 05:18:09 +00002704 EFI_STATUS RetVal;
jcarsey2247dde2009-11-09 18:08:58 +00002705 VA_START (Marker, Format);
jcarseya405b862010-09-14 05:18:09 +00002706 RetVal = InternalShellPrintWorker(Col, Row, Format, Marker);
jcarseye2f82972009-12-01 05:40:24 +00002707 VA_END(Marker);
jcarseya405b862010-09-14 05:18:09 +00002708 return(RetVal);
jcarsey2247dde2009-11-09 18:08:58 +00002709}
2710
2711/**
2712 Print at a specific location on the screen.
2713
jcarseye2f82972009-12-01 05:40:24 +00002714 This function will move the cursor to a given screen location and print the specified string.
jcarsey1e6e84c2010-01-25 20:05:08 +00002715
2716 If -1 is specified for either the Row or Col the current screen location for BOTH
jcarseye2f82972009-12-01 05:40:24 +00002717 will be used.
jcarsey2247dde2009-11-09 18:08:58 +00002718
jcarseye2f82972009-12-01 05:40:24 +00002719 If either Row or Col is out of range for the current console, then ASSERT.
2720 If Format is NULL, then ASSERT.
jcarsey2247dde2009-11-09 18:08:58 +00002721
jcarsey1e6e84c2010-01-25 20:05:08 +00002722 In addition to the standard %-based flags as supported by UefiLib Print() this supports
jcarsey2247dde2009-11-09 18:08:58 +00002723 the following additional flags:
jcarsey1e6e84c2010-01-25 20:05:08 +00002724 %N - Set output attribute to normal.
2725 %H - Set output attribute to highlight.
2726 %E - Set output attribute to error.
2727 %B - Set output attribute to blue color.
2728 %V - Set output attribute to green color.
jcarsey2247dde2009-11-09 18:08:58 +00002729
2730 Note: The background color is controlled by the shell command cls.
2731
jcarsey1e6e84c2010-01-25 20:05:08 +00002732 @param[in] Col The column to print at.
jcarseya405b862010-09-14 05:18:09 +00002733 @param[in] Row The row to print at.
jcarsey1e6e84c2010-01-25 20:05:08 +00002734 @param[in] Language The language of the string to retrieve. If this parameter
2735 is NULL, then the current platform language is used.
2736 @param[in] HiiFormatStringId The format string Id for getting from Hii.
2737 @param[in] HiiFormatHandle The format string Handle for getting from Hii.
jcarseya405b862010-09-14 05:18:09 +00002738 @param[in] ... The variable argument list.
jcarsey2247dde2009-11-09 18:08:58 +00002739
jcarseya405b862010-09-14 05:18:09 +00002740 @return EFI_SUCCESS The printing was successful.
2741 @return EFI_DEVICE_ERROR The console device reported an error.
jcarsey2247dde2009-11-09 18:08:58 +00002742**/
jcarseya405b862010-09-14 05:18:09 +00002743EFI_STATUS
jcarsey2247dde2009-11-09 18:08:58 +00002744EFIAPI
2745ShellPrintHiiEx(
2746 IN INT32 Col OPTIONAL,
2747 IN INT32 Row OPTIONAL,
jcarsey1e6e84c2010-01-25 20:05:08 +00002748 IN CONST CHAR8 *Language OPTIONAL,
jcarsey2247dde2009-11-09 18:08:58 +00002749 IN CONST EFI_STRING_ID HiiFormatStringId,
2750 IN CONST EFI_HANDLE HiiFormatHandle,
2751 ...
2752 )
2753{
2754 VA_LIST Marker;
2755 CHAR16 *HiiFormatString;
jcarseya405b862010-09-14 05:18:09 +00002756 EFI_STATUS RetVal;
jcarsey2247dde2009-11-09 18:08:58 +00002757
2758 VA_START (Marker, HiiFormatHandle);
jcarsey1e6e84c2010-01-25 20:05:08 +00002759 HiiFormatString = HiiGetString(HiiFormatHandle, HiiFormatStringId, Language);
jcarsey2247dde2009-11-09 18:08:58 +00002760 ASSERT(HiiFormatString != NULL);
2761
2762 RetVal = InternalShellPrintWorker(Col, Row, HiiFormatString, Marker);
2763
jcarseya405b862010-09-14 05:18:09 +00002764 SHELL_FREE_NON_NULL(HiiFormatString);
jcarseye2f82972009-12-01 05:40:24 +00002765 VA_END(Marker);
jcarsey2247dde2009-11-09 18:08:58 +00002766
2767 return (RetVal);
2768}
2769
2770/**
2771 Function to determine if a given filename represents a file or a directory.
2772
2773 @param[in] DirName Path to directory to test.
2774
2775 @retval EFI_SUCCESS The Path represents a directory
2776 @retval EFI_NOT_FOUND The Path does not represent a directory
2777 @return other The path failed to open
2778**/
2779EFI_STATUS
2780EFIAPI
2781ShellIsDirectory(
2782 IN CONST CHAR16 *DirName
2783 )
2784{
2785 EFI_STATUS Status;
jcarseya405b862010-09-14 05:18:09 +00002786 SHELL_FILE_HANDLE Handle;
2787 CHAR16 *TempLocation;
jcarsey2247dde2009-11-09 18:08:58 +00002788
jcarseyecd3d592009-12-07 18:05:00 +00002789 ASSERT(DirName != NULL);
2790
jcarseya405b862010-09-14 05:18:09 +00002791 Handle = NULL;
2792 TempLocation = NULL;
jcarsey2247dde2009-11-09 18:08:58 +00002793
2794 Status = ShellOpenFileByName(DirName, &Handle, EFI_FILE_MODE_READ, 0);
2795 if (EFI_ERROR(Status)) {
jcarseya405b862010-09-14 05:18:09 +00002796 //
2797 // try good logic first.
2798 //
2799 if (mEfiShellProtocol != NULL) {
2800 TempLocation = StrnCatGrow(&TempLocation, NULL, DirName, 0);
2801 if (StrStr(TempLocation, L":") != NULL && StrLen(StrStr(TempLocation, L":")) == 2) {
2802 *(StrStr(TempLocation, L":")+1) = CHAR_NULL;
2803 }
2804 if (mEfiShellProtocol->GetDevicePathFromMap(TempLocation) != NULL) {
2805 FreePool(TempLocation);
2806 return (EFI_SUCCESS);
2807 }
2808 FreePool(TempLocation);
2809 } else {
2810 //
2811 // probably a map name?!?!!?
2812 //
2813 TempLocation = StrStr(DirName, L"\\");
2814 if (TempLocation != NULL && *(TempLocation+1) == CHAR_NULL) {
2815 return (EFI_SUCCESS);
2816 }
2817 }
jcarsey2247dde2009-11-09 18:08:58 +00002818 return (Status);
2819 }
2820
2821 if (FileHandleIsDirectory(Handle) == EFI_SUCCESS) {
2822 ShellCloseFile(&Handle);
2823 return (EFI_SUCCESS);
2824 }
2825 ShellCloseFile(&Handle);
2826 return (EFI_NOT_FOUND);
2827}
2828
jcarsey125c2cf2009-11-18 21:36:50 +00002829/**
jcarsey36a9d672009-11-20 21:13:41 +00002830 Function to determine if a given filename represents a file.
2831
2832 @param[in] Name Path to file to test.
2833
2834 @retval EFI_SUCCESS The Path represents a file.
2835 @retval EFI_NOT_FOUND The Path does not represent a file.
2836 @retval other The path failed to open.
2837**/
2838EFI_STATUS
2839EFIAPI
2840ShellIsFile(
2841 IN CONST CHAR16 *Name
2842 )
2843{
2844 EFI_STATUS Status;
jcarseya405b862010-09-14 05:18:09 +00002845 SHELL_FILE_HANDLE Handle;
jcarsey36a9d672009-11-20 21:13:41 +00002846
jcarseyecd3d592009-12-07 18:05:00 +00002847 ASSERT(Name != NULL);
2848
jcarsey36a9d672009-11-20 21:13:41 +00002849 Handle = NULL;
2850
2851 Status = ShellOpenFileByName(Name, &Handle, EFI_FILE_MODE_READ, 0);
2852 if (EFI_ERROR(Status)) {
2853 return (Status);
2854 }
2855
2856 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {
2857 ShellCloseFile(&Handle);
2858 return (EFI_SUCCESS);
2859 }
2860 ShellCloseFile(&Handle);
2861 return (EFI_NOT_FOUND);
2862}
2863
2864/**
jcarseyb3011f42010-01-11 21:49:04 +00002865 Function to determine if a given filename represents a file.
2866
2867 This will search the CWD and then the Path.
2868
2869 If Name is NULL, then ASSERT.
2870
2871 @param[in] Name Path to file to test.
2872
2873 @retval EFI_SUCCESS The Path represents a file.
2874 @retval EFI_NOT_FOUND The Path does not represent a file.
2875 @retval other The path failed to open.
2876**/
2877EFI_STATUS
2878EFIAPI
2879ShellIsFileInPath(
2880 IN CONST CHAR16 *Name
jcarseya405b862010-09-14 05:18:09 +00002881 )
2882{
jcarseyb3011f42010-01-11 21:49:04 +00002883 CHAR16 *NewName;
2884 EFI_STATUS Status;
2885
2886 if (!EFI_ERROR(ShellIsFile(Name))) {
jcarseya405b862010-09-14 05:18:09 +00002887 return (EFI_SUCCESS);
jcarseyb3011f42010-01-11 21:49:04 +00002888 }
2889
2890 NewName = ShellFindFilePath(Name);
2891 if (NewName == NULL) {
2892 return (EFI_NOT_FOUND);
2893 }
2894 Status = ShellIsFile(NewName);
2895 FreePool(NewName);
2896 return (Status);
2897}
2898/**
jcarsey1e6e84c2010-01-25 20:05:08 +00002899 Function to determine whether a string is decimal or hex representation of a number
jcarsey125c2cf2009-11-18 21:36:50 +00002900 and return the number converted from the string.
2901
2902 @param[in] String String representation of a number
2903
2904 @retval all the number
2905**/
2906UINTN
2907EFIAPI
2908ShellStrToUintn(
2909 IN CONST CHAR16 *String
2910 )
2911{
2912 CONST CHAR16 *Walker;
jcarseyb3011f42010-01-11 21:49:04 +00002913 for (Walker = String; Walker != NULL && *Walker != CHAR_NULL && *Walker == L' '; Walker++);
jcarsey1cd45e72010-01-29 15:07:44 +00002914 if (Walker == NULL || *Walker == CHAR_NULL) {
2915 ASSERT(FALSE);
2916 return ((UINTN)(-1));
2917 } else {
2918 if (StrnCmp(Walker, L"0x", 2) == 0 || StrnCmp(Walker, L"0X", 2) == 0){
2919 return (StrHexToUintn(Walker));
2920 }
2921 return (StrDecimalToUintn(Walker));
jcarsey125c2cf2009-11-18 21:36:50 +00002922 }
jcarsey125c2cf2009-11-18 21:36:50 +00002923}
2924
2925/**
2926 Safely append with automatic string resizing given length of Destination and
2927 desired length of copy from Source.
2928
2929 append the first D characters of Source to the end of Destination, where D is
2930 the lesser of Count and the StrLen() of Source. If appending those D characters
2931 will fit within Destination (whose Size is given as CurrentSize) and
jcarsey1e6e84c2010-01-25 20:05:08 +00002932 still leave room for a NULL terminator, then those characters are appended,
2933 starting at the original terminating NULL of Destination, and a new terminating
2934 NULL is appended.
jcarsey125c2cf2009-11-18 21:36:50 +00002935
2936 If appending D characters onto Destination will result in a overflow of the size
2937 given in CurrentSize the string will be grown such that the copy can be performed
2938 and CurrentSize will be updated to the new size.
2939
2940 If Source is NULL, there is nothing to append, just return the current buffer in
2941 Destination.
2942
2943 if Destination is NULL, then ASSERT()
2944 if Destination's current length (including NULL terminator) is already more then
2945 CurrentSize, then ASSERT()
2946
2947 @param[in,out] Destination The String to append onto
2948 @param[in,out] CurrentSize on call the number of bytes in Destination. On
2949 return possibly the new size (still in bytes). if NULL
2950 then allocate whatever is needed.
2951 @param[in] Source The String to append from
2952 @param[in] Count Maximum number of characters to append. if 0 then
2953 all are appended.
2954
2955 @return Destination return the resultant string.
2956**/
2957CHAR16*
2958EFIAPI
2959StrnCatGrow (
2960 IN OUT CHAR16 **Destination,
2961 IN OUT UINTN *CurrentSize,
2962 IN CONST CHAR16 *Source,
2963 IN UINTN Count
2964 )
2965{
2966 UINTN DestinationStartSize;
2967 UINTN NewSize;
2968
2969 //
2970 // ASSERTs
2971 //
2972 ASSERT(Destination != NULL);
2973
2974 //
2975 // If there's nothing to do then just return Destination
2976 //
2977 if (Source == NULL) {
2978 return (*Destination);
2979 }
2980
2981 //
2982 // allow for un-initialized pointers, based on size being 0
2983 //
2984 if (CurrentSize != NULL && *CurrentSize == 0) {
2985 *Destination = NULL;
2986 }
2987
2988 //
2989 // allow for NULL pointers address as Destination
2990 //
2991 if (*Destination != NULL) {
2992 ASSERT(CurrentSize != 0);
2993 DestinationStartSize = StrSize(*Destination);
2994 ASSERT(DestinationStartSize <= *CurrentSize);
2995 } else {
2996 DestinationStartSize = 0;
2997// ASSERT(*CurrentSize == 0);
2998 }
2999
3000 //
3001 // Append all of Source?
3002 //
3003 if (Count == 0) {
3004 Count = StrLen(Source);
3005 }
3006
3007 //
3008 // Test and grow if required
3009 //
3010 if (CurrentSize != NULL) {
3011 NewSize = *CurrentSize;
3012 while (NewSize < (DestinationStartSize + (Count*sizeof(CHAR16)))) {
3013 NewSize += 2 * Count * sizeof(CHAR16);
3014 }
3015 *Destination = ReallocatePool(*CurrentSize, NewSize, *Destination);
jcarseyc9d92df2010-02-03 15:37:54 +00003016 ASSERT(*Destination != NULL);
jcarsey125c2cf2009-11-18 21:36:50 +00003017 *CurrentSize = NewSize;
3018 } else {
3019 *Destination = AllocateZeroPool((Count+1)*sizeof(CHAR16));
jcarseyc9d92df2010-02-03 15:37:54 +00003020 ASSERT(*Destination != NULL);
jcarsey125c2cf2009-11-18 21:36:50 +00003021 }
3022
3023 //
3024 // Now use standard StrnCat on a big enough buffer
3025 //
jcarseyc9d92df2010-02-03 15:37:54 +00003026 if (*Destination == NULL) {
3027 return (NULL);
3028 }
jcarsey125c2cf2009-11-18 21:36:50 +00003029 return StrnCat(*Destination, Source, Count);
3030}
jcarseyc9d92df2010-02-03 15:37:54 +00003031
3032/**
3033 Prompt the user and return the resultant answer to the requestor.
3034
3035 This function will display the requested question on the shell prompt and then
3036 wait for an apropriate answer to be input from the console.
3037
jcarseya405b862010-09-14 05:18:09 +00003038 if the SHELL_PROMPT_REQUEST_TYPE is SHELL_PROMPT_REQUEST_TYPE_YESNO, ShellPromptResponseTypeQuitContinue
jcarseyc9d92df2010-02-03 15:37:54 +00003039 or SHELL_PROMPT_REQUEST_TYPE_YESNOCANCEL then *Response is of type SHELL_PROMPT_RESPONSE.
3040
jcarseya405b862010-09-14 05:18:09 +00003041 if the SHELL_PROMPT_REQUEST_TYPE is ShellPromptResponseTypeFreeform then *Response is of type
jcarseyc9d92df2010-02-03 15:37:54 +00003042 CHAR16*.
3043
3044 In either case *Response must be callee freed if Response was not NULL;
3045
3046 @param Type What type of question is asked. This is used to filter the input
3047 to prevent invalid answers to question.
3048 @param Prompt Pointer to string prompt to use to request input.
3049 @param Response Pointer to Response which will be populated upon return.
3050
3051 @retval EFI_SUCCESS The operation was sucessful.
3052 @retval EFI_UNSUPPORTED The operation is not supported as requested.
3053 @retval EFI_INVALID_PARAMETER A parameter was invalid.
3054 @return other The operation failed.
3055**/
3056EFI_STATUS
3057EFIAPI
3058ShellPromptForResponse (
3059 IN SHELL_PROMPT_REQUEST_TYPE Type,
3060 IN CHAR16 *Prompt OPTIONAL,
3061 IN OUT VOID **Response OPTIONAL
3062 )
3063{
3064 EFI_STATUS Status;
3065 EFI_INPUT_KEY Key;
3066 UINTN EventIndex;
3067 SHELL_PROMPT_RESPONSE *Resp;
jcarseya405b862010-09-14 05:18:09 +00003068 UINTN Size;
3069 CHAR16 *Buffer;
jcarseyc9d92df2010-02-03 15:37:54 +00003070
jcarseya405b862010-09-14 05:18:09 +00003071 Status = EFI_UNSUPPORTED;
3072 Resp = NULL;
3073 Buffer = NULL;
3074 Size = 0;
3075 if (Type != ShellPromptResponseTypeFreeform) {
3076 Resp = (SHELL_PROMPT_RESPONSE*)AllocatePool(sizeof(SHELL_PROMPT_RESPONSE));
xdu290bfa222010-07-19 05:21:27 +00003077 }
jcarseyc9d92df2010-02-03 15:37:54 +00003078
3079 switch(Type) {
jcarseya405b862010-09-14 05:18:09 +00003080 case ShellPromptResponseTypeQuitContinue:
jcarseyc9d92df2010-02-03 15:37:54 +00003081 if (Prompt != NULL) {
3082 ShellPrintEx(-1, -1, L"%s", Prompt);
3083 }
3084 //
3085 // wait for valid response
3086 //
3087 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
3088 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
3089 ASSERT_EFI_ERROR(Status);
3090 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3091 if (Key.UnicodeChar == L'Q' || Key.UnicodeChar ==L'q') {
jcarseya405b862010-09-14 05:18:09 +00003092 *Resp = ShellPromptResponseQuit;
jcarseyc9d92df2010-02-03 15:37:54 +00003093 } else {
jcarseya405b862010-09-14 05:18:09 +00003094 *Resp = ShellPromptResponseContinue;
jcarseyc9d92df2010-02-03 15:37:54 +00003095 }
3096 break;
jcarseya405b862010-09-14 05:18:09 +00003097 case ShellPromptResponseTypeYesNoCancel:
jcarseyc9d92df2010-02-03 15:37:54 +00003098 if (Prompt != NULL) {
3099 ShellPrintEx(-1, -1, L"%s", Prompt);
3100 }
3101 //
3102 // wait for valid response
3103 //
jcarseya405b862010-09-14 05:18:09 +00003104 *Resp = ShellPromptResponseMax;
3105 while (*Resp == ShellPromptResponseMax) {
jcarseyc9d92df2010-02-03 15:37:54 +00003106 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
3107 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
3108 ASSERT_EFI_ERROR(Status);
3109 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3110 switch (Key.UnicodeChar) {
3111 case L'Y':
3112 case L'y':
jcarseya405b862010-09-14 05:18:09 +00003113 *Resp = ShellPromptResponseYes;
jcarseyc9d92df2010-02-03 15:37:54 +00003114 break;
3115 case L'N':
3116 case L'n':
jcarseya405b862010-09-14 05:18:09 +00003117 *Resp = ShellPromptResponseNo;
jcarseyc9d92df2010-02-03 15:37:54 +00003118 break;
3119 case L'C':
3120 case L'c':
jcarseya405b862010-09-14 05:18:09 +00003121 *Resp = ShellPromptResponseCancel;
3122 break;
3123 }
3124 }
3125 break; case ShellPromptResponseTypeYesNoAllCancel:
3126 if (Prompt != NULL) {
3127 ShellPrintEx(-1, -1, L"%s", Prompt);
3128 }
3129 //
3130 // wait for valid response
3131 //
3132 *Resp = ShellPromptResponseMax;
3133 while (*Resp == ShellPromptResponseMax) {
3134 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
3135 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
3136 ASSERT_EFI_ERROR(Status);
3137 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3138 switch (Key.UnicodeChar) {
3139 case L'Y':
3140 case L'y':
3141 *Resp = ShellPromptResponseYes;
3142 break;
3143 case L'N':
3144 case L'n':
3145 *Resp = ShellPromptResponseNo;
3146 break;
3147 case L'A':
3148 case L'a':
3149 *Resp = ShellPromptResponseAll;
3150 break;
3151 case L'C':
3152 case L'c':
3153 *Resp = ShellPromptResponseCancel;
jcarseyc9d92df2010-02-03 15:37:54 +00003154 break;
3155 }
3156 }
3157 break;
jcarseya405b862010-09-14 05:18:09 +00003158 case ShellPromptResponseTypeEnterContinue:
3159 case ShellPromptResponseTypeAnyKeyContinue:
jcarseyc9d92df2010-02-03 15:37:54 +00003160 if (Prompt != NULL) {
3161 ShellPrintEx(-1, -1, L"%s", Prompt);
3162 }
3163 //
3164 // wait for valid response
3165 //
jcarseya405b862010-09-14 05:18:09 +00003166 *Resp = ShellPromptResponseMax;
3167 while (*Resp == ShellPromptResponseMax) {
jcarseyc9d92df2010-02-03 15:37:54 +00003168 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
jcarseya405b862010-09-14 05:18:09 +00003169 if (Type == ShellPromptResponseTypeEnterContinue) {
jcarseyc9d92df2010-02-03 15:37:54 +00003170 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
3171 ASSERT_EFI_ERROR(Status);
3172 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3173 if (Key.UnicodeChar == CHAR_CARRIAGE_RETURN) {
jcarseya405b862010-09-14 05:18:09 +00003174 *Resp = ShellPromptResponseContinue;
jcarseyc9d92df2010-02-03 15:37:54 +00003175 break;
3176 }
3177 }
jcarseya405b862010-09-14 05:18:09 +00003178 if (Type == ShellPromptResponseTypeAnyKeyContinue) {
3179 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
3180 ASSERT_EFI_ERROR(Status);
3181 *Resp = ShellPromptResponseContinue;
jcarseyc9d92df2010-02-03 15:37:54 +00003182 break;
3183 }
3184 }
3185 break;
jcarseya405b862010-09-14 05:18:09 +00003186 case ShellPromptResponseTypeYesNo:
3187 if (Prompt != NULL) {
3188 ShellPrintEx(-1, -1, L"%s", Prompt);
3189 }
3190 //
3191 // wait for valid response
3192 //
3193 *Resp = ShellPromptResponseMax;
3194 while (*Resp == ShellPromptResponseMax) {
3195 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
3196 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
3197 ASSERT_EFI_ERROR(Status);
3198 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3199 switch (Key.UnicodeChar) {
3200 case L'Y':
3201 case L'y':
3202 *Resp = ShellPromptResponseYes;
3203 break;
3204 case L'N':
3205 case L'n':
3206 *Resp = ShellPromptResponseNo;
3207 break;
3208 }
3209 }
3210 break;
3211 case ShellPromptResponseTypeFreeform:
3212 if (Prompt != NULL) {
3213 ShellPrintEx(-1, -1, L"%s", Prompt);
3214 }
3215 while(1) {
3216 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
3217 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
3218 ASSERT_EFI_ERROR(Status);
3219 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3220 if (Key.UnicodeChar == CHAR_CARRIAGE_RETURN) {
3221 break;
3222 }
3223 ASSERT((Buffer == NULL && Size == 0) || (Buffer != NULL));
3224 StrnCatGrow(&Buffer, &Size, &Key.UnicodeChar, 1);
3225 }
3226 break;
3227 //
3228 // This is the location to add new prompt types.
3229 //
jcarseyc9d92df2010-02-03 15:37:54 +00003230 default:
jcarseya405b862010-09-14 05:18:09 +00003231 ASSERT(FALSE);
jcarseyc9d92df2010-02-03 15:37:54 +00003232 }
3233
3234 if (Response != NULL) {
jcarseya405b862010-09-14 05:18:09 +00003235 if (Resp != NULL) {
3236 *Response = Resp;
3237 } else if (Buffer != NULL) {
3238 *Response = Buffer;
3239 }
jcarseyc9d92df2010-02-03 15:37:54 +00003240 } else {
jcarseya405b862010-09-14 05:18:09 +00003241 if (Resp != NULL) {
3242 FreePool(Resp);
3243 }
3244 if (Buffer != NULL) {
3245 FreePool(Buffer);
3246 }
jcarseyc9d92df2010-02-03 15:37:54 +00003247 }
3248
jcarseya405b862010-09-14 05:18:09 +00003249 ShellPrintEx(-1, -1, L"\r\n");
jcarseyc9d92df2010-02-03 15:37:54 +00003250 return (Status);
3251}
3252
3253/**
3254 Prompt the user and return the resultant answer to the requestor.
3255
3256 This function is the same as ShellPromptForResponse, except that the prompt is
3257 automatically pulled from HII.
3258
3259 @param Type What type of question is asked. This is used to filter the input
3260 to prevent invalid answers to question.
jcarseya405b862010-09-14 05:18:09 +00003261 @param[in] HiiFormatStringId The format string Id for getting from Hii.
3262 @param[in] HiiFormatHandle The format string Handle for getting from Hii.
3263 @param Response Pointer to Response which will be populated upon return.
jcarseyc9d92df2010-02-03 15:37:54 +00003264
3265 @retval EFI_SUCCESS the operation was sucessful.
3266 @return other the operation failed.
3267
3268 @sa ShellPromptForResponse
3269**/
3270EFI_STATUS
3271EFIAPI
3272ShellPromptForResponseHii (
3273 IN SHELL_PROMPT_REQUEST_TYPE Type,
3274 IN CONST EFI_STRING_ID HiiFormatStringId,
3275 IN CONST EFI_HANDLE HiiFormatHandle,
3276 IN OUT VOID **Response
3277 )
3278{
3279 CHAR16 *Prompt;
3280 EFI_STATUS Status;
3281
3282 Prompt = HiiGetString(HiiFormatHandle, HiiFormatStringId, NULL);
3283 Status = ShellPromptForResponse(Type, Prompt, Response);
3284 FreePool(Prompt);
3285 return (Status);
3286}
3287
jcarseya405b862010-09-14 05:18:09 +00003288/**
3289 Function to determin if an entire string is a valid number.
jcarseyc9d92df2010-02-03 15:37:54 +00003290
jcarseya405b862010-09-14 05:18:09 +00003291 If Hex it must be preceeded with a 0x or has ForceHex, set TRUE.
3292
3293 @param[in] String The string to evaluate.
3294 @param[in] ForceHex TRUE - always assume hex.
3295 @param[in] StopAtSpace TRUE to halt upon finding a space, FALSE to keep going.
3296
3297 @retval TRUE It is all numeric (dec/hex) characters.
3298 @retval FALSE There is a non-numeric character.
3299**/
3300BOOLEAN
3301EFIAPI
3302ShellIsHexOrDecimalNumber (
3303 IN CONST CHAR16 *String,
3304 IN CONST BOOLEAN ForceHex,
3305 IN CONST BOOLEAN StopAtSpace
3306 )
3307{
3308 BOOLEAN Hex;
3309
3310 //
3311 // chop off a single negative sign
3312 //
3313 if (String != NULL && *String == L'-') {
3314 String++;
3315 }
3316
3317 if (String == NULL) {
3318 return (FALSE);
3319 }
3320
3321 //
3322 // chop leading zeroes
3323 //
3324 while(String != NULL && *String == L'0'){
3325 String++;
3326 }
3327 //
3328 // allow '0x' or '0X', but not 'x' or 'X'
3329 //
3330 if (String != NULL && (*String == L'x' || *String == L'X')) {
3331 if (*(String-1) != L'0') {
3332 //
3333 // we got an x without a preceeding 0
3334 //
3335 return (FALSE);
3336 }
3337 String++;
3338 Hex = TRUE;
3339 } else if (ForceHex) {
3340 Hex = TRUE;
3341 } else {
3342 Hex = FALSE;
3343 }
3344
3345 //
3346 // loop through the remaining characters and use the lib function
3347 //
3348 for ( ; String != NULL && *String != CHAR_NULL && !(StopAtSpace && *String == L' ') ; String++){
3349 if (Hex) {
3350 if (!ShellIsHexaDecimalDigitCharacter(*String)) {
3351 return (FALSE);
3352 }
3353 } else {
3354 if (!ShellIsDecimalDigitCharacter(*String)) {
3355 return (FALSE);
3356 }
3357 }
3358 }
3359 return (TRUE);
3360}
3361
3362/**
3363 Function to determine if a given filename exists.
3364
3365 @param[in] Name Path to test.
3366
3367 @retval EFI_SUCCESS The Path represents a file.
3368 @retval EFI_NOT_FOUND The Path does not represent a file.
3369 @retval other The path failed to open.
3370**/
3371EFI_STATUS
3372EFIAPI
3373ShellFileExists(
3374 IN CONST CHAR16 *Name
3375 )
3376{
3377 EFI_STATUS Status;
3378 EFI_SHELL_FILE_INFO *List;
3379
3380 ASSERT(Name != NULL);
3381
3382 List = NULL;
3383 Status = ShellOpenFileMetaArg((CHAR16*)Name, EFI_FILE_MODE_READ, &List);
3384 if (EFI_ERROR(Status)) {
3385 return (Status);
3386 }
3387
3388 ShellCloseFileMetaArg(&List);
3389
3390 return (EFI_SUCCESS);
3391}