blob: 6d885f4681f51df49d83dcb1986ac1997c698cc1 [file] [log] [blame]
jcarsey94b17fa2009-05-07 18:46:18 +00001/** @file
2 Provides interface to shell functionality for shell commands and applications.
3
jcarsey252d9452011-03-25 20:49:53 +00004 Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>
jcarsey1e6e84c2010-01-25 20:05:08 +00005 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>
jcarsey252d9452011-03-25 20:49:53 +000017#include <Library/SortLib.h>
jcarseyd2b45642009-05-11 18:02:16 +000018
jcarsey94b17fa2009-05-07 18:46:18 +000019#define FIND_XXXXX_FILE_BUFFER_SIZE (SIZE_OF_EFI_FILE_INFO + MAX_FILE_NAME_LEN)
20
jcarseyd2b45642009-05-11 18:02:16 +000021//
jcarseya405b862010-09-14 05:18:09 +000022// globals...
jcarseyd2b45642009-05-11 18:02:16 +000023//
24SHELL_PARAM_ITEM EmptyParamList[] = {
25 {NULL, TypeMax}
26 };
jcarseya405b862010-09-14 05:18:09 +000027SHELL_PARAM_ITEM SfoParamList[] = {
28 {L"-sfo", TypeFlag},
29 {NULL, TypeMax}
30 };
31EFI_SHELL_ENVIRONMENT2 *mEfiShellEnvironment2;
32EFI_SHELL_INTERFACE *mEfiShellInterface;
33EFI_SHELL_PROTOCOL *mEfiShellProtocol;
34EFI_SHELL_PARAMETERS_PROTOCOL *mEfiShellParametersProtocol;
35EFI_HANDLE mEfiShellEnvironment2Handle;
36FILE_HANDLE_FUNCTION_MAP FileFunctionMap;
jcarseyb3011f42010-01-11 21:49:04 +000037
jcarsey2247dde2009-11-09 18:08:58 +000038/**
39 Check if a Unicode character is a hexadecimal character.
40
jcarsey1e6e84c2010-01-25 20:05:08 +000041 This internal function checks if a Unicode character is a
jcarseya405b862010-09-14 05:18:09 +000042 numeric character. The valid hexadecimal characters are
jcarsey2247dde2009-11-09 18:08:58 +000043 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.
44
jcarsey2247dde2009-11-09 18:08:58 +000045 @param Char The character to check against.
46
47 @retval TRUE If the Char is a hexadecmial character.
48 @retval FALSE If the Char is not a hexadecmial character.
49
50**/
51BOOLEAN
52EFIAPI
jcarsey969c7832010-01-13 16:46:33 +000053ShellIsHexaDecimalDigitCharacter (
jcarsey2247dde2009-11-09 18:08:58 +000054 IN CHAR16 Char
jcarseya405b862010-09-14 05:18:09 +000055 )
56{
jcarsey2247dde2009-11-09 18:08:58 +000057 return (BOOLEAN) ((Char >= L'0' && Char <= L'9') || (Char >= L'A' && Char <= L'F') || (Char >= L'a' && Char <= L'f'));
58}
jcarsey94b17fa2009-05-07 18:46:18 +000059
60/**
jcarseya405b862010-09-14 05:18:09 +000061 Check if a Unicode character is a decimal character.
62
63 This internal function checks if a Unicode character is a
64 decimal character. The valid characters are
65 L'0' to L'9'.
66
67
68 @param Char The character to check against.
69
70 @retval TRUE If the Char is a hexadecmial character.
71 @retval FALSE If the Char is not a hexadecmial character.
72
73**/
74BOOLEAN
75EFIAPI
76ShellIsDecimalDigitCharacter (
77 IN CHAR16 Char
78 )
79{
80 return (BOOLEAN) (Char >= L'0' && Char <= L'9');
81}
82
83/**
84 Helper function to find ShellEnvironment2 for constructor.
85
86 @param[in] ImageHandle A copy of the calling image's handle.
jcarsey94b17fa2009-05-07 18:46:18 +000087**/
88EFI_STATUS
89EFIAPI
90ShellFindSE2 (
91 IN EFI_HANDLE ImageHandle
jcarseya405b862010-09-14 05:18:09 +000092 )
93{
jcarsey94b17fa2009-05-07 18:46:18 +000094 EFI_STATUS Status;
95 EFI_HANDLE *Buffer;
96 UINTN BufferSize;
97 UINTN HandleIndex;
98
99 BufferSize = 0;
100 Buffer = NULL;
jcarsey1e6e84c2010-01-25 20:05:08 +0000101 Status = gBS->OpenProtocol(ImageHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000102 &gEfiShellEnvironment2Guid,
103 (VOID **)&mEfiShellEnvironment2,
104 ImageHandle,
105 NULL,
106 EFI_OPEN_PROTOCOL_GET_PROTOCOL
jcarseya405b862010-09-14 05:18:09 +0000107 );
jcarsey94b17fa2009-05-07 18:46:18 +0000108 //
109 // look for the mEfiShellEnvironment2 protocol at a higher level
110 //
jcarseya405b862010-09-14 05:18:09 +0000111 if (EFI_ERROR (Status) || !(CompareGuid (&mEfiShellEnvironment2->SESGuid, &gEfiShellEnvironment2ExtGuid))){
jcarsey94b17fa2009-05-07 18:46:18 +0000112 //
113 // figure out how big of a buffer we need.
114 //
115 Status = gBS->LocateHandle (ByProtocol,
116 &gEfiShellEnvironment2Guid,
117 NULL, // ignored for ByProtocol
118 &BufferSize,
119 Buffer
jcarseya405b862010-09-14 05:18:09 +0000120 );
jcarsey2247dde2009-11-09 18:08:58 +0000121 //
122 // maybe it's not there???
123 //
124 if (Status == EFI_BUFFER_TOO_SMALL) {
jcarsey252d9452011-03-25 20:49:53 +0000125 Buffer = (EFI_HANDLE*)AllocateZeroPool(BufferSize);
jcarsey2247dde2009-11-09 18:08:58 +0000126 ASSERT(Buffer != NULL);
127 Status = gBS->LocateHandle (ByProtocol,
128 &gEfiShellEnvironment2Guid,
129 NULL, // ignored for ByProtocol
130 &BufferSize,
131 Buffer
jcarseya405b862010-09-14 05:18:09 +0000132 );
jcarsey2247dde2009-11-09 18:08:58 +0000133 }
jcarsey1cd45e72010-01-29 15:07:44 +0000134 if (!EFI_ERROR (Status) && Buffer != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +0000135 //
136 // now parse the list of returned handles
137 //
138 Status = EFI_NOT_FOUND;
139 for (HandleIndex = 0; HandleIndex < (BufferSize/sizeof(Buffer[0])); HandleIndex++) {
jcarsey1e6e84c2010-01-25 20:05:08 +0000140 Status = gBS->OpenProtocol(Buffer[HandleIndex],
jcarsey94b17fa2009-05-07 18:46:18 +0000141 &gEfiShellEnvironment2Guid,
142 (VOID **)&mEfiShellEnvironment2,
143 ImageHandle,
144 NULL,
145 EFI_OPEN_PROTOCOL_GET_PROTOCOL
jcarseya405b862010-09-14 05:18:09 +0000146 );
147 if (CompareGuid (&mEfiShellEnvironment2->SESGuid, &gEfiShellEnvironment2ExtGuid)) {
jcarsey94b17fa2009-05-07 18:46:18 +0000148 mEfiShellEnvironment2Handle = Buffer[HandleIndex];
149 Status = EFI_SUCCESS;
150 break;
151 }
152 }
153 }
154 }
155 if (Buffer != NULL) {
156 FreePool (Buffer);
157 }
158 return (Status);
159}
160
jcarsey252d9452011-03-25 20:49:53 +0000161/**
jcarseya405b862010-09-14 05:18:09 +0000162 Function to do most of the work of the constructor. Allows for calling
163 multiple times without complete re-initialization.
164
165 @param[in] ImageHandle A copy of the ImageHandle.
166 @param[in] SystemTable A pointer to the SystemTable for the application.
jcarsey252d9452011-03-25 20:49:53 +0000167
168 @retval EFI_SUCCESS The operationw as successful.
jcarseya405b862010-09-14 05:18:09 +0000169**/
jcarsey94b17fa2009-05-07 18:46:18 +0000170EFI_STATUS
171EFIAPI
jcarseyd2b45642009-05-11 18:02:16 +0000172ShellLibConstructorWorker (
jcarsey94b17fa2009-05-07 18:46:18 +0000173 IN EFI_HANDLE ImageHandle,
174 IN EFI_SYSTEM_TABLE *SystemTable
jcarseya405b862010-09-14 05:18:09 +0000175 )
176{
177 EFI_STATUS Status;
jcarseyecd3d592009-12-07 18:05:00 +0000178
jcarsey94b17fa2009-05-07 18:46:18 +0000179 //
180 // UEFI 2.0 shell interfaces (used preferentially)
181 //
jcarseya405b862010-09-14 05:18:09 +0000182 Status = gBS->OpenProtocol(
183 ImageHandle,
184 &gEfiShellProtocolGuid,
185 (VOID **)&mEfiShellProtocol,
186 ImageHandle,
187 NULL,
188 EFI_OPEN_PROTOCOL_GET_PROTOCOL
189 );
jcarsey94b17fa2009-05-07 18:46:18 +0000190 if (EFI_ERROR(Status)) {
jcarseya405b862010-09-14 05:18:09 +0000191 //
192 // Search for the shell protocol
193 //
194 Status = gBS->LocateProtocol(
195 &gEfiShellProtocolGuid,
196 NULL,
197 (VOID **)&mEfiShellProtocol
198 );
199 if (EFI_ERROR(Status)) {
200 mEfiShellProtocol = NULL;
201 }
jcarsey94b17fa2009-05-07 18:46:18 +0000202 }
jcarseya405b862010-09-14 05:18:09 +0000203 Status = gBS->OpenProtocol(
204 ImageHandle,
205 &gEfiShellParametersProtocolGuid,
206 (VOID **)&mEfiShellParametersProtocol,
207 ImageHandle,
208 NULL,
209 EFI_OPEN_PROTOCOL_GET_PROTOCOL
210 );
jcarsey94b17fa2009-05-07 18:46:18 +0000211 if (EFI_ERROR(Status)) {
212 mEfiShellParametersProtocol = NULL;
213 }
214
215 if (mEfiShellParametersProtocol == NULL || mEfiShellProtocol == NULL) {
216 //
217 // Moved to seperate function due to complexity
218 //
219 Status = ShellFindSE2(ImageHandle);
220
221 if (EFI_ERROR(Status)) {
222 DEBUG((DEBUG_ERROR, "Status: 0x%08x\r\n", Status));
223 mEfiShellEnvironment2 = NULL;
224 }
jcarsey1e6e84c2010-01-25 20:05:08 +0000225 Status = gBS->OpenProtocol(ImageHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000226 &gEfiShellInterfaceGuid,
227 (VOID **)&mEfiShellInterface,
228 ImageHandle,
229 NULL,
230 EFI_OPEN_PROTOCOL_GET_PROTOCOL
jcarseya405b862010-09-14 05:18:09 +0000231 );
jcarsey94b17fa2009-05-07 18:46:18 +0000232 if (EFI_ERROR(Status)) {
233 mEfiShellInterface = NULL;
234 }
235 }
jcarseyc9d92df2010-02-03 15:37:54 +0000236
jcarsey94b17fa2009-05-07 18:46:18 +0000237 //
238 // only success getting 2 of either the old or new, but no 1/2 and 1/2
239 //
jcarsey1e6e84c2010-01-25 20:05:08 +0000240 if ((mEfiShellEnvironment2 != NULL && mEfiShellInterface != NULL) ||
jcarseya405b862010-09-14 05:18:09 +0000241 (mEfiShellProtocol != NULL && mEfiShellParametersProtocol != NULL) ) {
jcarseyd2b45642009-05-11 18:02:16 +0000242 if (mEfiShellProtocol != NULL) {
243 FileFunctionMap.GetFileInfo = mEfiShellProtocol->GetFileInfo;
244 FileFunctionMap.SetFileInfo = mEfiShellProtocol->SetFileInfo;
245 FileFunctionMap.ReadFile = mEfiShellProtocol->ReadFile;
246 FileFunctionMap.WriteFile = mEfiShellProtocol->WriteFile;
247 FileFunctionMap.CloseFile = mEfiShellProtocol->CloseFile;
248 FileFunctionMap.DeleteFile = mEfiShellProtocol->DeleteFile;
249 FileFunctionMap.GetFilePosition = mEfiShellProtocol->GetFilePosition;
250 FileFunctionMap.SetFilePosition = mEfiShellProtocol->SetFilePosition;
251 FileFunctionMap.FlushFile = mEfiShellProtocol->FlushFile;
252 FileFunctionMap.GetFileSize = mEfiShellProtocol->GetFileSize;
253 } else {
jcarseya405b862010-09-14 05:18:09 +0000254 FileFunctionMap.GetFileInfo = (EFI_SHELL_GET_FILE_INFO)FileHandleGetInfo;
255 FileFunctionMap.SetFileInfo = (EFI_SHELL_SET_FILE_INFO)FileHandleSetInfo;
256 FileFunctionMap.ReadFile = (EFI_SHELL_READ_FILE)FileHandleRead;
257 FileFunctionMap.WriteFile = (EFI_SHELL_WRITE_FILE)FileHandleWrite;
258 FileFunctionMap.CloseFile = (EFI_SHELL_CLOSE_FILE)FileHandleClose;
259 FileFunctionMap.DeleteFile = (EFI_SHELL_DELETE_FILE)FileHandleDelete;
260 FileFunctionMap.GetFilePosition = (EFI_SHELL_GET_FILE_POSITION)FileHandleGetPosition;
261 FileFunctionMap.SetFilePosition = (EFI_SHELL_SET_FILE_POSITION)FileHandleSetPosition;
262 FileFunctionMap.FlushFile = (EFI_SHELL_FLUSH_FILE)FileHandleFlush;
263 FileFunctionMap.GetFileSize = (EFI_SHELL_GET_FILE_SIZE)FileHandleGetSize;
jcarseyd2b45642009-05-11 18:02:16 +0000264 }
jcarsey94b17fa2009-05-07 18:46:18 +0000265 return (EFI_SUCCESS);
266 }
267 return (EFI_NOT_FOUND);
268}
jcarseyd2b45642009-05-11 18:02:16 +0000269/**
270 Constructor for the Shell library.
271
272 Initialize the library and determine if the underlying is a UEFI Shell 2.0 or an EFI shell.
273
274 @param ImageHandle the image handle of the process
275 @param SystemTable the EFI System Table pointer
276
277 @retval EFI_SUCCESS the initialization was complete sucessfully
278 @return others an error ocurred during initialization
279**/
280EFI_STATUS
281EFIAPI
282ShellLibConstructor (
283 IN EFI_HANDLE ImageHandle,
284 IN EFI_SYSTEM_TABLE *SystemTable
jcarseya405b862010-09-14 05:18:09 +0000285 )
286{
jcarseyd2b45642009-05-11 18:02:16 +0000287 mEfiShellEnvironment2 = NULL;
288 mEfiShellProtocol = NULL;
289 mEfiShellParametersProtocol = NULL;
290 mEfiShellInterface = NULL;
291 mEfiShellEnvironment2Handle = NULL;
292
jcarseyd2b45642009-05-11 18:02:16 +0000293 //
294 // verify that auto initialize is not set false
jcarsey1e6e84c2010-01-25 20:05:08 +0000295 //
jcarseyd2b45642009-05-11 18:02:16 +0000296 if (PcdGetBool(PcdShellLibAutoInitialize) == 0) {
297 return (EFI_SUCCESS);
298 }
jcarsey1e6e84c2010-01-25 20:05:08 +0000299
jcarseyd2b45642009-05-11 18:02:16 +0000300 return (ShellLibConstructorWorker(ImageHandle, SystemTable));
301}
jcarsey94b17fa2009-05-07 18:46:18 +0000302
303/**
jcarseya405b862010-09-14 05:18:09 +0000304 Destructor for the library. free any resources.
305
306 @param[in] ImageHandle A copy of the ImageHandle.
307 @param[in] SystemTable A pointer to the SystemTable for the application.
308
309 @retval EFI_SUCCESS The operation was successful.
310 @return An error from the CloseProtocol function.
jcarsey94b17fa2009-05-07 18:46:18 +0000311**/
312EFI_STATUS
313EFIAPI
314ShellLibDestructor (
315 IN EFI_HANDLE ImageHandle,
316 IN EFI_SYSTEM_TABLE *SystemTable
jcarseya405b862010-09-14 05:18:09 +0000317 )
318{
jcarsey94b17fa2009-05-07 18:46:18 +0000319 if (mEfiShellEnvironment2 != NULL) {
320 gBS->CloseProtocol(mEfiShellEnvironment2Handle==NULL?ImageHandle:mEfiShellEnvironment2Handle,
321 &gEfiShellEnvironment2Guid,
322 ImageHandle,
323 NULL);
jcarseyd2b45642009-05-11 18:02:16 +0000324 mEfiShellEnvironment2 = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +0000325 }
326 if (mEfiShellInterface != NULL) {
327 gBS->CloseProtocol(ImageHandle,
328 &gEfiShellInterfaceGuid,
329 ImageHandle,
jcarsey1e6e84c2010-01-25 20:05:08 +0000330 NULL);
jcarseyd2b45642009-05-11 18:02:16 +0000331 mEfiShellInterface = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +0000332 }
333 if (mEfiShellProtocol != NULL) {
334 gBS->CloseProtocol(ImageHandle,
335 &gEfiShellProtocolGuid,
336 ImageHandle,
jcarsey1e6e84c2010-01-25 20:05:08 +0000337 NULL);
jcarseyd2b45642009-05-11 18:02:16 +0000338 mEfiShellProtocol = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +0000339 }
340 if (mEfiShellParametersProtocol != NULL) {
341 gBS->CloseProtocol(ImageHandle,
342 &gEfiShellParametersProtocolGuid,
343 ImageHandle,
jcarsey1e6e84c2010-01-25 20:05:08 +0000344 NULL);
jcarseyd2b45642009-05-11 18:02:16 +0000345 mEfiShellParametersProtocol = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +0000346 }
jcarseyd2b45642009-05-11 18:02:16 +0000347 mEfiShellEnvironment2Handle = NULL;
jcarseyecd3d592009-12-07 18:05:00 +0000348
jcarsey94b17fa2009-05-07 18:46:18 +0000349 return (EFI_SUCCESS);
350}
jcarseyd2b45642009-05-11 18:02:16 +0000351
352/**
353 This function causes the shell library to initialize itself. If the shell library
354 is already initialized it will de-initialize all the current protocol poitners and
355 re-populate them again.
356
357 When the library is used with PcdShellLibAutoInitialize set to true this function
358 will return EFI_SUCCESS and perform no actions.
359
360 This function is intended for internal access for shell commands only.
361
362 @retval EFI_SUCCESS the initialization was complete sucessfully
363
364**/
365EFI_STATUS
366EFIAPI
367ShellInitialize (
jcarseya405b862010-09-14 05:18:09 +0000368 )
369{
jcarseyd2b45642009-05-11 18:02:16 +0000370 //
371 // if auto initialize is not false then skip
372 //
373 if (PcdGetBool(PcdShellLibAutoInitialize) != 0) {
374 return (EFI_SUCCESS);
375 }
376
377 //
378 // deinit the current stuff
379 //
380 ASSERT_EFI_ERROR(ShellLibDestructor(gImageHandle, gST));
381
382 //
383 // init the new stuff
384 //
385 return (ShellLibConstructorWorker(gImageHandle, gST));
386}
387
jcarsey94b17fa2009-05-07 18:46:18 +0000388/**
jcarsey1e6e84c2010-01-25 20:05:08 +0000389 This function will retrieve the information about the file for the handle
jcarsey94b17fa2009-05-07 18:46:18 +0000390 specified and store it in allocated pool memory.
391
jcarsey1e6e84c2010-01-25 20:05:08 +0000392 This function allocates a buffer to store the file's information. It is the
qhuang869817bf2009-05-20 14:42:48 +0000393 caller's responsibility to free the buffer
jcarsey94b17fa2009-05-07 18:46:18 +0000394
jcarsey1e6e84c2010-01-25 20:05:08 +0000395 @param FileHandle The file handle of the file for which information is
jcarsey94b17fa2009-05-07 18:46:18 +0000396 being requested.
397
398 @retval NULL information could not be retrieved.
399
400 @return the information about the file
401**/
402EFI_FILE_INFO*
403EFIAPI
404ShellGetFileInfo (
jcarseya405b862010-09-14 05:18:09 +0000405 IN SHELL_FILE_HANDLE FileHandle
406 )
407{
jcarseyd2b45642009-05-11 18:02:16 +0000408 return (FileFunctionMap.GetFileInfo(FileHandle));
jcarsey94b17fa2009-05-07 18:46:18 +0000409}
410
411/**
jcarseya405b862010-09-14 05:18:09 +0000412 This function sets the information about the file for the opened handle
jcarsey94b17fa2009-05-07 18:46:18 +0000413 specified.
414
jcarseya405b862010-09-14 05:18:09 +0000415 @param[in] FileHandle The file handle of the file for which information
416 is being set.
jcarsey94b17fa2009-05-07 18:46:18 +0000417
jcarseya405b862010-09-14 05:18:09 +0000418 @param[in] FileInfo The information to set.
jcarsey94b17fa2009-05-07 18:46:18 +0000419
jcarseya405b862010-09-14 05:18:09 +0000420 @retval EFI_SUCCESS The information was set.
421 @retval EFI_INVALID_PARAMETER A parameter was out of range or invalid.
422 @retval EFI_UNSUPPORTED The FileHandle does not support FileInfo.
423 @retval EFI_NO_MEDIA The device has no medium.
424 @retval EFI_DEVICE_ERROR The device reported an error.
jcarsey94b17fa2009-05-07 18:46:18 +0000425 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
jcarseya405b862010-09-14 05:18:09 +0000426 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
427 @retval EFI_ACCESS_DENIED The file was opened read only.
428 @retval EFI_VOLUME_FULL The volume is full.
jcarsey94b17fa2009-05-07 18:46:18 +0000429**/
430EFI_STATUS
431EFIAPI
432ShellSetFileInfo (
jcarseya405b862010-09-14 05:18:09 +0000433 IN SHELL_FILE_HANDLE FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000434 IN EFI_FILE_INFO *FileInfo
jcarseya405b862010-09-14 05:18:09 +0000435 )
436{
jcarseyd2b45642009-05-11 18:02:16 +0000437 return (FileFunctionMap.SetFileInfo(FileHandle, FileInfo));
jcarsey1e6e84c2010-01-25 20:05:08 +0000438}
439
jcarsey94b17fa2009-05-07 18:46:18 +0000440 /**
441 This function will open a file or directory referenced by DevicePath.
442
jcarsey1e6e84c2010-01-25 20:05:08 +0000443 This function opens a file with the open mode according to the file path. The
jcarsey94b17fa2009-05-07 18:46:18 +0000444 Attributes is valid only for EFI_FILE_MODE_CREATE.
445
jcarsey1e6e84c2010-01-25 20:05:08 +0000446 @param FilePath on input the device path to the file. On output
jcarsey94b17fa2009-05-07 18:46:18 +0000447 the remaining device path.
448 @param DeviceHandle pointer to the system device handle.
449 @param FileHandle pointer to the file handle.
450 @param OpenMode the mode to open the file with.
451 @param Attributes the file's file attributes.
452
453 @retval EFI_SUCCESS The information was set.
454 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
jcarsey1e6e84c2010-01-25 20:05:08 +0000455 @retval EFI_UNSUPPORTED Could not open the file path.
456 @retval EFI_NOT_FOUND The specified file could not be found on the
457 device or the file system could not be found on
jcarsey94b17fa2009-05-07 18:46:18 +0000458 the device.
459 @retval EFI_NO_MEDIA The device has no medium.
jcarsey1e6e84c2010-01-25 20:05:08 +0000460 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
jcarsey94b17fa2009-05-07 18:46:18 +0000461 medium is no longer supported.
462 @retval EFI_DEVICE_ERROR The device reported an error.
463 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
464 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
jcarseya405b862010-09-14 05:18:09 +0000465 @retval EFI_ACCESS_DENIED The file was opened read only.
jcarsey1e6e84c2010-01-25 20:05:08 +0000466 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
jcarsey94b17fa2009-05-07 18:46:18 +0000467 file.
468 @retval EFI_VOLUME_FULL The volume is full.
469**/
470EFI_STATUS
471EFIAPI
472ShellOpenFileByDevicePath(
473 IN OUT EFI_DEVICE_PATH_PROTOCOL **FilePath,
474 OUT EFI_HANDLE *DeviceHandle,
jcarseya405b862010-09-14 05:18:09 +0000475 OUT SHELL_FILE_HANDLE *FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000476 IN UINT64 OpenMode,
477 IN UINT64 Attributes
jcarseya405b862010-09-14 05:18:09 +0000478 )
479{
480 CHAR16 *FileName;
481 EFI_STATUS Status;
jcarsey94b17fa2009-05-07 18:46:18 +0000482 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *EfiSimpleFileSystemProtocol;
jcarseya405b862010-09-14 05:18:09 +0000483 EFI_FILE_PROTOCOL *Handle1;
484 EFI_FILE_PROTOCOL *Handle2;
jcarsey94b17fa2009-05-07 18:46:18 +0000485
486 //
487 // ASERT for FileHandle, FilePath, and DeviceHandle being NULL
488 //
489 ASSERT(FilePath != NULL);
490 ASSERT(FileHandle != NULL);
491 ASSERT(DeviceHandle != NULL);
jcarsey1e6e84c2010-01-25 20:05:08 +0000492 //
jcarsey94b17fa2009-05-07 18:46:18 +0000493 // which shell interface should we use
494 //
495 if (mEfiShellProtocol != NULL) {
496 //
497 // use UEFI Shell 2.0 method.
498 //
499 FileName = mEfiShellProtocol->GetFilePathFromDevicePath(*FilePath);
500 if (FileName == NULL) {
501 return (EFI_INVALID_PARAMETER);
502 }
503 Status = ShellOpenFileByName(FileName, FileHandle, OpenMode, Attributes);
504 FreePool(FileName);
505 return (Status);
jcarsey1e6e84c2010-01-25 20:05:08 +0000506 }
jcarseyd2b45642009-05-11 18:02:16 +0000507
508
509 //
510 // use old shell method.
511 //
jcarsey1e6e84c2010-01-25 20:05:08 +0000512 Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid,
513 FilePath,
jcarseyd2b45642009-05-11 18:02:16 +0000514 DeviceHandle);
515 if (EFI_ERROR (Status)) {
516 return Status;
517 }
518 Status = gBS->OpenProtocol(*DeviceHandle,
519 &gEfiSimpleFileSystemProtocolGuid,
jcarseyb1f95a02009-06-16 00:23:19 +0000520 (VOID**)&EfiSimpleFileSystemProtocol,
jcarseyd2b45642009-05-11 18:02:16 +0000521 gImageHandle,
522 NULL,
523 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
524 if (EFI_ERROR (Status)) {
525 return Status;
526 }
jcarseya405b862010-09-14 05:18:09 +0000527 Status = EfiSimpleFileSystemProtocol->OpenVolume(EfiSimpleFileSystemProtocol, &Handle1);
jcarseyd2b45642009-05-11 18:02:16 +0000528 if (EFI_ERROR (Status)) {
529 FileHandle = NULL;
530 return Status;
531 }
532
533 //
534 // go down directories one node at a time.
535 //
536 while (!IsDevicePathEnd (*FilePath)) {
jcarsey94b17fa2009-05-07 18:46:18 +0000537 //
jcarseyd2b45642009-05-11 18:02:16 +0000538 // For file system access each node should be a file path component
jcarsey94b17fa2009-05-07 18:46:18 +0000539 //
jcarseyd2b45642009-05-11 18:02:16 +0000540 if (DevicePathType (*FilePath) != MEDIA_DEVICE_PATH ||
541 DevicePathSubType (*FilePath) != MEDIA_FILEPATH_DP
jcarseya405b862010-09-14 05:18:09 +0000542 ) {
jcarsey94b17fa2009-05-07 18:46:18 +0000543 FileHandle = NULL;
jcarseyd2b45642009-05-11 18:02:16 +0000544 return (EFI_INVALID_PARAMETER);
jcarsey94b17fa2009-05-07 18:46:18 +0000545 }
jcarseyd2b45642009-05-11 18:02:16 +0000546 //
547 // Open this file path node
548 //
jcarseya405b862010-09-14 05:18:09 +0000549 Handle2 = Handle1;
550 Handle1 = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +0000551
552 //
jcarseyd2b45642009-05-11 18:02:16 +0000553 // Try to test opening an existing file
jcarsey94b17fa2009-05-07 18:46:18 +0000554 //
jcarseya405b862010-09-14 05:18:09 +0000555 Status = Handle2->Open (
556 Handle2,
557 &Handle1,
jcarseyd2b45642009-05-11 18:02:16 +0000558 ((FILEPATH_DEVICE_PATH*)*FilePath)->PathName,
559 OpenMode &~EFI_FILE_MODE_CREATE,
560 0
jcarseya405b862010-09-14 05:18:09 +0000561 );
jcarsey94b17fa2009-05-07 18:46:18 +0000562
jcarseyd2b45642009-05-11 18:02:16 +0000563 //
564 // see if the error was that it needs to be created
565 //
566 if ((EFI_ERROR (Status)) && (OpenMode != (OpenMode &~EFI_FILE_MODE_CREATE))) {
jcarseya405b862010-09-14 05:18:09 +0000567 Status = Handle2->Open (
568 Handle2,
569 &Handle1,
jcarsey94b17fa2009-05-07 18:46:18 +0000570 ((FILEPATH_DEVICE_PATH*)*FilePath)->PathName,
jcarseyd2b45642009-05-11 18:02:16 +0000571 OpenMode,
572 Attributes
jcarseya405b862010-09-14 05:18:09 +0000573 );
jcarsey94b17fa2009-05-07 18:46:18 +0000574 }
jcarseyd2b45642009-05-11 18:02:16 +0000575 //
576 // Close the last node
577 //
jcarseya405b862010-09-14 05:18:09 +0000578 Handle2->Close (Handle2);
jcarseyd2b45642009-05-11 18:02:16 +0000579
580 if (EFI_ERROR(Status)) {
581 return (Status);
582 }
583
584 //
585 // Get the next node
586 //
587 *FilePath = NextDevicePathNode (*FilePath);
jcarsey94b17fa2009-05-07 18:46:18 +0000588 }
jcarseya405b862010-09-14 05:18:09 +0000589
590 //
591 // This is a weak spot since if the undefined SHELL_FILE_HANDLE format changes this must change also!
592 //
593 *FileHandle = (VOID*)Handle1;
jcarseyd2b45642009-05-11 18:02:16 +0000594 return (EFI_SUCCESS);
jcarsey94b17fa2009-05-07 18:46:18 +0000595}
596
597/**
598 This function will open a file or directory referenced by filename.
599
jcarsey1e6e84c2010-01-25 20:05:08 +0000600 If return is EFI_SUCCESS, the Filehandle is the opened file's handle;
601 otherwise, the Filehandle is NULL. The Attributes is valid only for
jcarsey94b17fa2009-05-07 18:46:18 +0000602 EFI_FILE_MODE_CREATE.
603
604 if FileNAme is NULL then ASSERT()
605
606 @param FileName pointer to file name
607 @param FileHandle pointer to the file handle.
608 @param OpenMode the mode to open the file with.
609 @param Attributes the file's file attributes.
610
611 @retval EFI_SUCCESS The information was set.
612 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
jcarsey1e6e84c2010-01-25 20:05:08 +0000613 @retval EFI_UNSUPPORTED Could not open the file path.
614 @retval EFI_NOT_FOUND The specified file could not be found on the
615 device or the file system could not be found
jcarsey94b17fa2009-05-07 18:46:18 +0000616 on the device.
617 @retval EFI_NO_MEDIA The device has no medium.
jcarsey1e6e84c2010-01-25 20:05:08 +0000618 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
jcarsey94b17fa2009-05-07 18:46:18 +0000619 medium is no longer supported.
620 @retval EFI_DEVICE_ERROR The device reported an error.
621 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
622 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
623 @retval EFI_ACCESS_DENIED The file was opened read only.
jcarsey1e6e84c2010-01-25 20:05:08 +0000624 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
jcarsey94b17fa2009-05-07 18:46:18 +0000625 file.
626 @retval EFI_VOLUME_FULL The volume is full.
627**/
628EFI_STATUS
629EFIAPI
630ShellOpenFileByName(
jcarseyb82bfcc2009-06-29 16:28:23 +0000631 IN CONST CHAR16 *FileName,
jcarseya405b862010-09-14 05:18:09 +0000632 OUT SHELL_FILE_HANDLE *FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000633 IN UINT64 OpenMode,
634 IN UINT64 Attributes
jcarseya405b862010-09-14 05:18:09 +0000635 )
636{
jcarsey94b17fa2009-05-07 18:46:18 +0000637 EFI_HANDLE DeviceHandle;
638 EFI_DEVICE_PATH_PROTOCOL *FilePath;
jcarseyb1f95a02009-06-16 00:23:19 +0000639 EFI_STATUS Status;
640 EFI_FILE_INFO *FileInfo;
jcarsey94b17fa2009-05-07 18:46:18 +0000641
642 //
643 // ASSERT if FileName is NULL
644 //
645 ASSERT(FileName != NULL);
646
jcarseya405b862010-09-14 05:18:09 +0000647 if (FileName == NULL) {
648 return (EFI_INVALID_PARAMETER);
649 }
650
jcarsey94b17fa2009-05-07 18:46:18 +0000651 if (mEfiShellProtocol != NULL) {
jcarseya405b862010-09-14 05:18:09 +0000652 if ((OpenMode & EFI_FILE_MODE_CREATE) == EFI_FILE_MODE_CREATE && (Attributes & EFI_FILE_DIRECTORY) == EFI_FILE_DIRECTORY) {
653 return ShellCreateDirectory(FileName, FileHandle);
654 }
jcarsey94b17fa2009-05-07 18:46:18 +0000655 //
656 // Use UEFI Shell 2.0 method
657 //
jcarseyb1f95a02009-06-16 00:23:19 +0000658 Status = mEfiShellProtocol->OpenFileByName(FileName,
659 FileHandle,
660 OpenMode);
jcarseya405b862010-09-14 05:18:09 +0000661 if (StrCmp(FileName, L"NUL") != 0 && !EFI_ERROR(Status) && ((OpenMode & EFI_FILE_MODE_CREATE) != 0)){
jcarsey2247dde2009-11-09 18:08:58 +0000662 FileInfo = FileFunctionMap.GetFileInfo(*FileHandle);
jcarseyb1f95a02009-06-16 00:23:19 +0000663 ASSERT(FileInfo != NULL);
664 FileInfo->Attribute = Attributes;
jcarsey2247dde2009-11-09 18:08:58 +0000665 Status = FileFunctionMap.SetFileInfo(*FileHandle, FileInfo);
666 FreePool(FileInfo);
jcarseyb1f95a02009-06-16 00:23:19 +0000667 }
668 return (Status);
jcarsey1e6e84c2010-01-25 20:05:08 +0000669 }
jcarsey94b17fa2009-05-07 18:46:18 +0000670 //
671 // Using EFI Shell version
672 // this means convert name to path and call that function
673 // since this will use EFI method again that will open it.
674 //
675 ASSERT(mEfiShellEnvironment2 != NULL);
jcarseyb82bfcc2009-06-29 16:28:23 +0000676 FilePath = mEfiShellEnvironment2->NameToPath ((CHAR16*)FileName);
xdu290bfa222010-07-19 05:21:27 +0000677 if (FilePath != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +0000678 return (ShellOpenFileByDevicePath(&FilePath,
679 &DeviceHandle,
680 FileHandle,
681 OpenMode,
jcarseya405b862010-09-14 05:18:09 +0000682 Attributes));
jcarsey94b17fa2009-05-07 18:46:18 +0000683 }
684 return (EFI_DEVICE_ERROR);
685}
686/**
687 This function create a directory
688
jcarsey1e6e84c2010-01-25 20:05:08 +0000689 If return is EFI_SUCCESS, the Filehandle is the opened directory's handle;
690 otherwise, the Filehandle is NULL. If the directory already existed, this
jcarsey94b17fa2009-05-07 18:46:18 +0000691 function opens the existing directory.
692
693 @param DirectoryName pointer to directory name
694 @param FileHandle pointer to the file handle.
695
696 @retval EFI_SUCCESS The information was set.
697 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
jcarsey1e6e84c2010-01-25 20:05:08 +0000698 @retval EFI_UNSUPPORTED Could not open the file path.
699 @retval EFI_NOT_FOUND The specified file could not be found on the
700 device or the file system could not be found
jcarsey94b17fa2009-05-07 18:46:18 +0000701 on the device.
702 @retval EFI_NO_MEDIA The device has no medium.
jcarsey1e6e84c2010-01-25 20:05:08 +0000703 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
jcarsey94b17fa2009-05-07 18:46:18 +0000704 medium is no longer supported.
705 @retval EFI_DEVICE_ERROR The device reported an error.
706 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
707 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
708 @retval EFI_ACCESS_DENIED The file was opened read only.
jcarsey1e6e84c2010-01-25 20:05:08 +0000709 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
jcarsey94b17fa2009-05-07 18:46:18 +0000710 file.
711 @retval EFI_VOLUME_FULL The volume is full.
712 @sa ShellOpenFileByName
713**/
714EFI_STATUS
715EFIAPI
716ShellCreateDirectory(
jcarseyb82bfcc2009-06-29 16:28:23 +0000717 IN CONST CHAR16 *DirectoryName,
jcarseya405b862010-09-14 05:18:09 +0000718 OUT SHELL_FILE_HANDLE *FileHandle
719 )
720{
jcarsey2247dde2009-11-09 18:08:58 +0000721 if (mEfiShellProtocol != NULL) {
722 //
723 // Use UEFI Shell 2.0 method
724 //
725 return (mEfiShellProtocol->CreateFile(DirectoryName,
726 EFI_FILE_DIRECTORY,
727 FileHandle
jcarseya405b862010-09-14 05:18:09 +0000728 ));
jcarsey2247dde2009-11-09 18:08:58 +0000729 } else {
730 return (ShellOpenFileByName(DirectoryName,
731 FileHandle,
732 EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE,
733 EFI_FILE_DIRECTORY
jcarseya405b862010-09-14 05:18:09 +0000734 ));
jcarsey2247dde2009-11-09 18:08:58 +0000735 }
jcarsey94b17fa2009-05-07 18:46:18 +0000736}
737
738/**
739 This function reads information from an opened file.
740
jcarsey1e6e84c2010-01-25 20:05:08 +0000741 If FileHandle is not a directory, the function reads the requested number of
742 bytes from the file at the file's current position and returns them in Buffer.
jcarsey94b17fa2009-05-07 18:46:18 +0000743 If the read goes beyond the end of the file, the read length is truncated to the
jcarsey1e6e84c2010-01-25 20:05:08 +0000744 end of the file. The file's current position is increased by the number of bytes
745 returned. If FileHandle is a directory, the function reads the directory entry
746 at the file's current position and returns the entry in Buffer. If the Buffer
747 is not large enough to hold the current directory entry, then
748 EFI_BUFFER_TOO_SMALL is returned and the current file position is not updated.
749 BufferSize is set to be the size of the buffer needed to read the entry. On
750 success, the current position is updated to the next directory entry. If there
751 are no more directory entries, the read returns a zero-length buffer.
jcarsey94b17fa2009-05-07 18:46:18 +0000752 EFI_FILE_INFO is the structure returned as the directory entry.
753
754 @param FileHandle the opened file handle
jcarsey1e6e84c2010-01-25 20:05:08 +0000755 @param BufferSize on input the size of buffer in bytes. on return
jcarsey94b17fa2009-05-07 18:46:18 +0000756 the number of bytes written.
757 @param Buffer the buffer to put read data into.
758
759 @retval EFI_SUCCESS Data was read.
760 @retval EFI_NO_MEDIA The device has no media.
761 @retval EFI_DEVICE_ERROR The device reported an error.
762 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
jcarsey1e6e84c2010-01-25 20:05:08 +0000763 @retval EFI_BUFFER_TO_SMALL Buffer is too small. ReadSize contains required
jcarsey94b17fa2009-05-07 18:46:18 +0000764 size.
765
766**/
767EFI_STATUS
768EFIAPI
769ShellReadFile(
jcarseya405b862010-09-14 05:18:09 +0000770 IN SHELL_FILE_HANDLE FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000771 IN OUT UINTN *BufferSize,
772 OUT VOID *Buffer
jcarseya405b862010-09-14 05:18:09 +0000773 )
774{
jcarseyd2b45642009-05-11 18:02:16 +0000775 return (FileFunctionMap.ReadFile(FileHandle, BufferSize, Buffer));
jcarsey94b17fa2009-05-07 18:46:18 +0000776}
777
778
779/**
780 Write data to a file.
781
jcarsey1e6e84c2010-01-25 20:05:08 +0000782 This function writes the specified number of bytes to the file at the current
783 file position. The current file position is advanced the actual number of bytes
784 written, which is returned in BufferSize. Partial writes only occur when there
785 has been a data error during the write attempt (such as "volume space full").
786 The file is automatically grown to hold the data if required. Direct writes to
jcarsey94b17fa2009-05-07 18:46:18 +0000787 opened directories are not supported.
788
789 @param FileHandle The opened file for writing
790 @param BufferSize on input the number of bytes in Buffer. On output
791 the number of bytes written.
792 @param Buffer the buffer containing data to write is stored.
793
794 @retval EFI_SUCCESS Data was written.
795 @retval EFI_UNSUPPORTED Writes to an open directory are not supported.
796 @retval EFI_NO_MEDIA The device has no media.
797 @retval EFI_DEVICE_ERROR The device reported an error.
798 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
799 @retval EFI_WRITE_PROTECTED The device is write-protected.
800 @retval EFI_ACCESS_DENIED The file was open for read only.
801 @retval EFI_VOLUME_FULL The volume is full.
802**/
803EFI_STATUS
804EFIAPI
805ShellWriteFile(
jcarsey252d9452011-03-25 20:49:53 +0000806 IN SHELL_FILE_HANDLE FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000807 IN OUT UINTN *BufferSize,
808 IN VOID *Buffer
jcarseya405b862010-09-14 05:18:09 +0000809 )
810{
jcarseyd2b45642009-05-11 18:02:16 +0000811 return (FileFunctionMap.WriteFile(FileHandle, BufferSize, Buffer));
jcarsey94b17fa2009-05-07 18:46:18 +0000812}
813
jcarsey1e6e84c2010-01-25 20:05:08 +0000814/**
jcarsey94b17fa2009-05-07 18:46:18 +0000815 Close an open file handle.
816
jcarsey1e6e84c2010-01-25 20:05:08 +0000817 This function closes a specified file handle. All "dirty" cached file data is
818 flushed to the device, and the file is closed. In all cases the handle is
jcarsey94b17fa2009-05-07 18:46:18 +0000819 closed.
820
821@param FileHandle the file handle to close.
822
823@retval EFI_SUCCESS the file handle was closed sucessfully.
824**/
825EFI_STATUS
826EFIAPI
827ShellCloseFile (
jcarseya405b862010-09-14 05:18:09 +0000828 IN SHELL_FILE_HANDLE *FileHandle
829 )
830{
jcarseyd2b45642009-05-11 18:02:16 +0000831 return (FileFunctionMap.CloseFile(*FileHandle));
jcarsey94b17fa2009-05-07 18:46:18 +0000832}
833
834/**
835 Delete a file and close the handle
836
837 This function closes and deletes a file. In all cases the file handle is closed.
jcarsey1e6e84c2010-01-25 20:05:08 +0000838 If the file cannot be deleted, the warning code EFI_WARN_DELETE_FAILURE is
jcarsey94b17fa2009-05-07 18:46:18 +0000839 returned, but the handle is still closed.
840
841 @param FileHandle the file handle to delete
842
843 @retval EFI_SUCCESS the file was closed sucessfully
jcarsey1e6e84c2010-01-25 20:05:08 +0000844 @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not
jcarsey94b17fa2009-05-07 18:46:18 +0000845 deleted
846 @retval INVALID_PARAMETER One of the parameters has an invalid value.
847**/
848EFI_STATUS
849EFIAPI
850ShellDeleteFile (
jcarseya405b862010-09-14 05:18:09 +0000851 IN SHELL_FILE_HANDLE *FileHandle
852 )
853{
jcarseyd2b45642009-05-11 18:02:16 +0000854 return (FileFunctionMap.DeleteFile(*FileHandle));
jcarsey94b17fa2009-05-07 18:46:18 +0000855}
856
857/**
858 Set the current position in a file.
859
jcarsey1e6e84c2010-01-25 20:05:08 +0000860 This function sets the current file position for the handle to the position
jcarsey94b17fa2009-05-07 18:46:18 +0000861 supplied. With the exception of seeking to position 0xFFFFFFFFFFFFFFFF, only
jcarsey1e6e84c2010-01-25 20:05:08 +0000862 absolute positioning is supported, and seeking past the end of the file is
863 allowed (a subsequent write would grow the file). Seeking to position
jcarsey94b17fa2009-05-07 18:46:18 +0000864 0xFFFFFFFFFFFFFFFF causes the current position to be set to the end of the file.
jcarsey1e6e84c2010-01-25 20:05:08 +0000865 If FileHandle is a directory, the only position that may be set is zero. This
jcarsey94b17fa2009-05-07 18:46:18 +0000866 has the effect of starting the read process of the directory entries over.
867
868 @param FileHandle The file handle on which the position is being set
869 @param Position Byte position from begining of file
870
871 @retval EFI_SUCCESS Operation completed sucessfully.
jcarsey1e6e84c2010-01-25 20:05:08 +0000872 @retval EFI_UNSUPPORTED the seek request for non-zero is not valid on
jcarsey94b17fa2009-05-07 18:46:18 +0000873 directories.
874 @retval INVALID_PARAMETER One of the parameters has an invalid value.
875**/
876EFI_STATUS
877EFIAPI
878ShellSetFilePosition (
jcarseya405b862010-09-14 05:18:09 +0000879 IN SHELL_FILE_HANDLE FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000880 IN UINT64 Position
jcarseya405b862010-09-14 05:18:09 +0000881 )
882{
jcarseyd2b45642009-05-11 18:02:16 +0000883 return (FileFunctionMap.SetFilePosition(FileHandle, Position));
jcarsey94b17fa2009-05-07 18:46:18 +0000884}
885
jcarsey1e6e84c2010-01-25 20:05:08 +0000886/**
jcarsey94b17fa2009-05-07 18:46:18 +0000887 Gets a file's current position
888
jcarsey1e6e84c2010-01-25 20:05:08 +0000889 This function retrieves the current file position for the file handle. For
890 directories, the current file position has no meaning outside of the file
jcarsey94b17fa2009-05-07 18:46:18 +0000891 system driver and as such the operation is not supported. An error is returned
892 if FileHandle is a directory.
893
894 @param FileHandle The open file handle on which to get the position.
895 @param Position Byte position from begining of file.
896
897 @retval EFI_SUCCESS the operation completed sucessfully.
898 @retval INVALID_PARAMETER One of the parameters has an invalid value.
899 @retval EFI_UNSUPPORTED the request is not valid on directories.
900**/
901EFI_STATUS
902EFIAPI
903ShellGetFilePosition (
jcarseya405b862010-09-14 05:18:09 +0000904 IN SHELL_FILE_HANDLE FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000905 OUT UINT64 *Position
jcarseya405b862010-09-14 05:18:09 +0000906 )
907{
jcarseyd2b45642009-05-11 18:02:16 +0000908 return (FileFunctionMap.GetFilePosition(FileHandle, Position));
jcarsey94b17fa2009-05-07 18:46:18 +0000909}
910/**
911 Flushes data on a file
jcarsey1e6e84c2010-01-25 20:05:08 +0000912
jcarsey94b17fa2009-05-07 18:46:18 +0000913 This function flushes all modified data associated with a file to a device.
914
915 @param FileHandle The file handle on which to flush data
916
917 @retval EFI_SUCCESS The data was flushed.
918 @retval EFI_NO_MEDIA The device has no media.
919 @retval EFI_DEVICE_ERROR The device reported an error.
920 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
921 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
922 @retval EFI_ACCESS_DENIED The file was opened for read only.
923**/
924EFI_STATUS
925EFIAPI
926ShellFlushFile (
jcarseya405b862010-09-14 05:18:09 +0000927 IN SHELL_FILE_HANDLE FileHandle
928 )
929{
jcarseyd2b45642009-05-11 18:02:16 +0000930 return (FileFunctionMap.FlushFile(FileHandle));
jcarsey94b17fa2009-05-07 18:46:18 +0000931}
932
933/**
934 Retrieves the first file from a directory
935
jcarsey1e6e84c2010-01-25 20:05:08 +0000936 This function opens a directory and gets the first file's info in the
937 directory. Caller can use ShellFindNextFile() to get other files. When
jcarsey94b17fa2009-05-07 18:46:18 +0000938 complete the caller is responsible for calling FreePool() on Buffer.
939
940 @param DirHandle The file handle of the directory to search
941 @param Buffer Pointer to buffer for file's information
942
943 @retval EFI_SUCCESS Found the first file.
944 @retval EFI_NOT_FOUND Cannot find the directory.
945 @retval EFI_NO_MEDIA The device has no media.
946 @retval EFI_DEVICE_ERROR The device reported an error.
947 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
948 @return Others status of ShellGetFileInfo, ShellSetFilePosition,
949 or ShellReadFile
950**/
951EFI_STATUS
952EFIAPI
953ShellFindFirstFile (
jcarseya405b862010-09-14 05:18:09 +0000954 IN SHELL_FILE_HANDLE DirHandle,
jcarseyd2b45642009-05-11 18:02:16 +0000955 OUT EFI_FILE_INFO **Buffer
jcarseya405b862010-09-14 05:18:09 +0000956 )
957{
jcarsey94b17fa2009-05-07 18:46:18 +0000958 //
jcarseyd2b45642009-05-11 18:02:16 +0000959 // pass to file handle lib
jcarsey94b17fa2009-05-07 18:46:18 +0000960 //
jcarseyd2b45642009-05-11 18:02:16 +0000961 return (FileHandleFindFirstFile(DirHandle, Buffer));
jcarsey94b17fa2009-05-07 18:46:18 +0000962}
963/**
964 Retrieves the next file in a directory.
965
jcarseya405b862010-09-14 05:18:09 +0000966 To use this function, caller must call the ShellFindFirstFile() to get the
jcarsey1e6e84c2010-01-25 20:05:08 +0000967 first file, and then use this function get other files. This function can be
968 called for several times to get each file's information in the directory. If
969 the call of ShellFindNextFile() got the last file in the directory, the next
970 call of this function has no file to get. *NoFile will be set to TRUE and the
971 Buffer memory will be automatically freed.
jcarsey94b17fa2009-05-07 18:46:18 +0000972
973 @param DirHandle the file handle of the directory
974 @param Buffer pointer to buffer for file's information
975 @param NoFile pointer to boolean when last file is found
976
977 @retval EFI_SUCCESS Found the next file, or reached last file
978 @retval EFI_NO_MEDIA The device has no media.
979 @retval EFI_DEVICE_ERROR The device reported an error.
980 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
981**/
982EFI_STATUS
983EFIAPI
984ShellFindNextFile(
jcarseya405b862010-09-14 05:18:09 +0000985 IN SHELL_FILE_HANDLE DirHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000986 OUT EFI_FILE_INFO *Buffer,
987 OUT BOOLEAN *NoFile
jcarseya405b862010-09-14 05:18:09 +0000988 )
989{
jcarsey94b17fa2009-05-07 18:46:18 +0000990 //
jcarseyd2b45642009-05-11 18:02:16 +0000991 // pass to file handle lib
jcarsey94b17fa2009-05-07 18:46:18 +0000992 //
jcarseyd2b45642009-05-11 18:02:16 +0000993 return (FileHandleFindNextFile(DirHandle, Buffer, NoFile));
jcarsey94b17fa2009-05-07 18:46:18 +0000994}
995/**
996 Retrieve the size of a file.
997
998 if FileHandle is NULL then ASSERT()
999 if Size is NULL then ASSERT()
1000
jcarsey1e6e84c2010-01-25 20:05:08 +00001001 This function extracts the file size info from the FileHandle's EFI_FILE_INFO
jcarsey94b17fa2009-05-07 18:46:18 +00001002 data.
1003
1004 @param FileHandle file handle from which size is retrieved
1005 @param Size pointer to size
1006
1007 @retval EFI_SUCCESS operation was completed sucessfully
1008 @retval EFI_DEVICE_ERROR cannot access the file
1009**/
1010EFI_STATUS
1011EFIAPI
1012ShellGetFileSize (
jcarseya405b862010-09-14 05:18:09 +00001013 IN SHELL_FILE_HANDLE FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +00001014 OUT UINT64 *Size
jcarseya405b862010-09-14 05:18:09 +00001015 )
1016{
jcarseyd2b45642009-05-11 18:02:16 +00001017 return (FileFunctionMap.GetFileSize(FileHandle, Size));
jcarsey94b17fa2009-05-07 18:46:18 +00001018}
1019/**
1020 Retrieves the status of the break execution flag
1021
1022 this function is useful to check whether the application is being asked to halt by the shell.
1023
1024 @retval TRUE the execution break is enabled
1025 @retval FALSE the execution break is not enabled
1026**/
1027BOOLEAN
1028EFIAPI
1029ShellGetExecutionBreakFlag(
1030 VOID
1031 )
1032{
jcarsey1e6e84c2010-01-25 20:05:08 +00001033 //
jcarsey94b17fa2009-05-07 18:46:18 +00001034 // Check for UEFI Shell 2.0 protocols
1035 //
1036 if (mEfiShellProtocol != NULL) {
1037
1038 //
1039 // We are using UEFI Shell 2.0; see if the event has been triggered
1040 //
1041 if (gBS->CheckEvent(mEfiShellProtocol->ExecutionBreak) != EFI_SUCCESS) {
1042 return (FALSE);
1043 }
1044 return (TRUE);
jcarsey1e6e84c2010-01-25 20:05:08 +00001045 }
jcarsey94b17fa2009-05-07 18:46:18 +00001046
1047 //
1048 // using EFI Shell; call the function to check
1049 //
1050 ASSERT(mEfiShellEnvironment2 != NULL);
1051 return (mEfiShellEnvironment2->GetExecutionBreak());
1052}
1053/**
1054 return the value of an environment variable
1055
jcarsey1e6e84c2010-01-25 20:05:08 +00001056 this function gets the value of the environment variable set by the
jcarsey94b17fa2009-05-07 18:46:18 +00001057 ShellSetEnvironmentVariable function
1058
1059 @param EnvKey The key name of the environment variable.
1060
1061 @retval NULL the named environment variable does not exist.
1062 @return != NULL pointer to the value of the environment variable
1063**/
1064CONST CHAR16*
1065EFIAPI
1066ShellGetEnvironmentVariable (
jcarsey9b3bf082009-06-23 21:15:07 +00001067 IN CONST CHAR16 *EnvKey
jcarsey94b17fa2009-05-07 18:46:18 +00001068 )
1069{
jcarsey1e6e84c2010-01-25 20:05:08 +00001070 //
jcarsey94b17fa2009-05-07 18:46:18 +00001071 // Check for UEFI Shell 2.0 protocols
1072 //
1073 if (mEfiShellProtocol != NULL) {
1074 return (mEfiShellProtocol->GetEnv(EnvKey));
1075 }
1076
1077 //
1078 // ASSERT that we must have EFI shell
1079 //
1080 ASSERT(mEfiShellEnvironment2 != NULL);
1081
1082 //
1083 // using EFI Shell
1084 //
jcarsey9b3bf082009-06-23 21:15:07 +00001085 return (mEfiShellEnvironment2->GetEnv((CHAR16*)EnvKey));
jcarsey94b17fa2009-05-07 18:46:18 +00001086}
1087/**
1088 set the value of an environment variable
1089
1090This function changes the current value of the specified environment variable. If the
1091environment variable exists and the Value is an empty string, then the environment
1092variable is deleted. If the environment variable exists and the Value is not an empty
1093string, then the value of the environment variable is changed. If the environment
1094variable does not exist and the Value is an empty string, there is no action. If the
1095environment variable does not exist and the Value is a non-empty string, then the
1096environment variable is created and assigned the specified value.
1097
1098 This is not supported pre-UEFI Shell 2.0.
1099
1100 @param EnvKey The key name of the environment variable.
1101 @param EnvVal The Value of the environment variable
1102 @param Volatile Indicates whether the variable is non-volatile (FALSE) or volatile (TRUE).
1103
1104 @retval EFI_SUCCESS the operation was completed sucessfully
1105 @retval EFI_UNSUPPORTED This operation is not allowed in pre UEFI 2.0 Shell environments
1106**/
1107EFI_STATUS
1108EFIAPI
1109ShellSetEnvironmentVariable (
1110 IN CONST CHAR16 *EnvKey,
1111 IN CONST CHAR16 *EnvVal,
1112 IN BOOLEAN Volatile
1113 )
1114{
jcarsey1e6e84c2010-01-25 20:05:08 +00001115 //
jcarsey94b17fa2009-05-07 18:46:18 +00001116 // Check for UEFI Shell 2.0 protocols
1117 //
1118 if (mEfiShellProtocol != NULL) {
1119 return (mEfiShellProtocol->SetEnv(EnvKey, EnvVal, Volatile));
jcarsey1e6e84c2010-01-25 20:05:08 +00001120 }
jcarsey94b17fa2009-05-07 18:46:18 +00001121
1122 //
1123 // This feature does not exist under EFI shell
1124 //
1125 return (EFI_UNSUPPORTED);
1126}
jcarseya405b862010-09-14 05:18:09 +00001127
jcarsey94b17fa2009-05-07 18:46:18 +00001128/**
jcarseya405b862010-09-14 05:18:09 +00001129 Cause the shell to parse and execute a command line.
jcarsey94b17fa2009-05-07 18:46:18 +00001130
1131 This function creates a nested instance of the shell and executes the specified
jcarseya405b862010-09-14 05:18:09 +00001132 command (CommandLine) with the specified environment (Environment). Upon return,
1133 the status code returned by the specified command is placed in StatusCode.
1134 If Environment is NULL, then the current environment is used and all changes made
1135 by the commands executed will be reflected in the current environment. If the
1136 Environment is non-NULL, then the changes made will be discarded.
1137 The CommandLine is executed from the current working directory on the current
1138 device.
jcarsey94b17fa2009-05-07 18:46:18 +00001139
jcarseya405b862010-09-14 05:18:09 +00001140 The EnvironmentVariables and Status parameters are ignored in a pre-UEFI Shell 2.0
1141 environment. The values pointed to by the parameters will be unchanged by the
1142 ShellExecute() function. The Output parameter has no effect in a
1143 UEFI Shell 2.0 environment.
jcarsey94b17fa2009-05-07 18:46:18 +00001144
jcarseya405b862010-09-14 05:18:09 +00001145 @param[in] ParentHandle The parent image starting the operation.
1146 @param[in] CommandLine The pointer to a NULL terminated command line.
1147 @param[in] Output True to display debug output. False to hide it.
1148 @param[in] EnvironmentVariables Optional pointer to array of environment variables
1149 in the form "x=y". If NULL, the current set is used.
1150 @param[out] Status The status of the run command line.
jcarsey94b17fa2009-05-07 18:46:18 +00001151
jcarseya405b862010-09-14 05:18:09 +00001152 @retval EFI_SUCCESS The operation completed sucessfully. Status
1153 contains the status code returned.
1154 @retval EFI_INVALID_PARAMETER A parameter contains an invalid value.
1155 @retval EFI_OUT_OF_RESOURCES Out of resources.
1156 @retval EFI_UNSUPPORTED The operation is not allowed.
jcarsey94b17fa2009-05-07 18:46:18 +00001157**/
1158EFI_STATUS
1159EFIAPI
1160ShellExecute (
1161 IN EFI_HANDLE *ParentHandle,
1162 IN CHAR16 *CommandLine OPTIONAL,
1163 IN BOOLEAN Output OPTIONAL,
1164 IN CHAR16 **EnvironmentVariables OPTIONAL,
1165 OUT EFI_STATUS *Status OPTIONAL
1166 )
1167{
jcarsey1e6e84c2010-01-25 20:05:08 +00001168 //
jcarsey94b17fa2009-05-07 18:46:18 +00001169 // Check for UEFI Shell 2.0 protocols
1170 //
1171 if (mEfiShellProtocol != NULL) {
1172 //
1173 // Call UEFI Shell 2.0 version (not using Output parameter)
1174 //
1175 return (mEfiShellProtocol->Execute(ParentHandle,
1176 CommandLine,
1177 EnvironmentVariables,
1178 Status));
jcarsey1e6e84c2010-01-25 20:05:08 +00001179 }
jcarsey94b17fa2009-05-07 18:46:18 +00001180 //
1181 // ASSERT that we must have EFI shell
1182 //
1183 ASSERT(mEfiShellEnvironment2 != NULL);
1184 //
1185 // Call EFI Shell version (not using EnvironmentVariables or Status parameters)
1186 // Due to oddity in the EFI shell we want to dereference the ParentHandle here
1187 //
jcarsey1e6e84c2010-01-25 20:05:08 +00001188 return (mEfiShellEnvironment2->Execute(*ParentHandle,
1189 CommandLine,
jcarsey94b17fa2009-05-07 18:46:18 +00001190 Output));
1191}
1192/**
1193 Retreives the current directory path
1194
jcarsey1e6e84c2010-01-25 20:05:08 +00001195 If the DeviceName is NULL, it returns the current device's current directory
1196 name. If the DeviceName is not NULL, it returns the current directory name
jcarsey94b17fa2009-05-07 18:46:18 +00001197 on specified drive.
1198
1199 @param DeviceName the name of the drive to get directory on
1200
1201 @retval NULL the directory does not exist
1202 @return != NULL the directory
1203**/
1204CONST CHAR16*
1205EFIAPI
1206ShellGetCurrentDir (
jcarseya405b862010-09-14 05:18:09 +00001207 IN CHAR16 * CONST DeviceName OPTIONAL
jcarsey94b17fa2009-05-07 18:46:18 +00001208 )
1209{
jcarsey1e6e84c2010-01-25 20:05:08 +00001210 //
jcarsey94b17fa2009-05-07 18:46:18 +00001211 // Check for UEFI Shell 2.0 protocols
1212 //
1213 if (mEfiShellProtocol != NULL) {
1214 return (mEfiShellProtocol->GetCurDir(DeviceName));
jcarsey1e6e84c2010-01-25 20:05:08 +00001215 }
jcarsey94b17fa2009-05-07 18:46:18 +00001216 //
jcarsey8bd282b2011-06-27 16:45:41 +00001217 // Check for EFI shell
jcarsey94b17fa2009-05-07 18:46:18 +00001218 //
jcarsey8bd282b2011-06-27 16:45:41 +00001219 if (mEfiShellEnvironment2 != NULL) {
1220 return (mEfiShellEnvironment2->CurDir(DeviceName));
1221 }
1222
1223 return (NULL);
jcarsey94b17fa2009-05-07 18:46:18 +00001224}
1225/**
1226 sets (enabled or disabled) the page break mode
1227
jcarsey1e6e84c2010-01-25 20:05:08 +00001228 when page break mode is enabled the screen will stop scrolling
jcarsey94b17fa2009-05-07 18:46:18 +00001229 and wait for operator input before scrolling a subsequent screen.
1230
1231 @param CurrentState TRUE to enable and FALSE to disable
1232**/
jcarsey1e6e84c2010-01-25 20:05:08 +00001233VOID
jcarsey94b17fa2009-05-07 18:46:18 +00001234EFIAPI
1235ShellSetPageBreakMode (
1236 IN BOOLEAN CurrentState
1237 )
1238{
1239 //
1240 // check for enabling
1241 //
1242 if (CurrentState != 0x00) {
jcarsey1e6e84c2010-01-25 20:05:08 +00001243 //
jcarsey94b17fa2009-05-07 18:46:18 +00001244 // check for UEFI Shell 2.0
1245 //
1246 if (mEfiShellProtocol != NULL) {
1247 //
1248 // Enable with UEFI 2.0 Shell
1249 //
1250 mEfiShellProtocol->EnablePageBreak();
1251 return;
1252 } else {
jcarsey1e6e84c2010-01-25 20:05:08 +00001253 //
jcarsey94b17fa2009-05-07 18:46:18 +00001254 // ASSERT that must have EFI Shell
1255 //
1256 ASSERT(mEfiShellEnvironment2 != NULL);
1257 //
1258 // Enable with EFI Shell
1259 //
1260 mEfiShellEnvironment2->EnablePageBreak (DEFAULT_INIT_ROW, DEFAULT_AUTO_LF);
1261 return;
1262 }
1263 } else {
jcarsey1e6e84c2010-01-25 20:05:08 +00001264 //
jcarsey94b17fa2009-05-07 18:46:18 +00001265 // check for UEFI Shell 2.0
1266 //
1267 if (mEfiShellProtocol != NULL) {
1268 //
1269 // Disable with UEFI 2.0 Shell
1270 //
1271 mEfiShellProtocol->DisablePageBreak();
1272 return;
1273 } else {
jcarsey1e6e84c2010-01-25 20:05:08 +00001274 //
jcarsey94b17fa2009-05-07 18:46:18 +00001275 // ASSERT that must have EFI Shell
1276 //
1277 ASSERT(mEfiShellEnvironment2 != NULL);
1278 //
1279 // Disable with EFI Shell
1280 //
1281 mEfiShellEnvironment2->DisablePageBreak ();
1282 return;
1283 }
1284 }
1285}
1286
1287///
1288/// version of EFI_SHELL_FILE_INFO struct, except has no CONST pointers.
1289/// This allows for the struct to be populated.
1290///
1291typedef struct {
jcarseyd2b45642009-05-11 18:02:16 +00001292 LIST_ENTRY Link;
jcarsey94b17fa2009-05-07 18:46:18 +00001293 EFI_STATUS Status;
1294 CHAR16 *FullName;
1295 CHAR16 *FileName;
jcarseya405b862010-09-14 05:18:09 +00001296 SHELL_FILE_HANDLE Handle;
jcarsey94b17fa2009-05-07 18:46:18 +00001297 EFI_FILE_INFO *Info;
1298} EFI_SHELL_FILE_INFO_NO_CONST;
1299
1300/**
1301 Converts a EFI shell list of structures to the coresponding UEFI Shell 2.0 type of list.
1302
1303 if OldStyleFileList is NULL then ASSERT()
1304
jcarsey1e6e84c2010-01-25 20:05:08 +00001305 this function will convert a SHELL_FILE_ARG based list into a callee allocated
jcarsey94b17fa2009-05-07 18:46:18 +00001306 EFI_SHELL_FILE_INFO based list. it is up to the caller to free the memory via
1307 the ShellCloseFileMetaArg function.
1308
jcarseya405b862010-09-14 05:18:09 +00001309 @param[in] FileList the EFI shell list type
jcarseyb82bfcc2009-06-29 16:28:23 +00001310 @param[in,out] ListHead the list to add to
jcarsey94b17fa2009-05-07 18:46:18 +00001311
1312 @retval the resultant head of the double linked new format list;
1313**/
1314LIST_ENTRY*
1315EFIAPI
1316InternalShellConvertFileListType (
jcarsey9b3bf082009-06-23 21:15:07 +00001317 IN LIST_ENTRY *FileList,
1318 IN OUT LIST_ENTRY *ListHead
jcarsey125c2cf2009-11-18 21:36:50 +00001319 )
1320{
jcarsey94b17fa2009-05-07 18:46:18 +00001321 SHELL_FILE_ARG *OldInfo;
jcarsey9b3bf082009-06-23 21:15:07 +00001322 LIST_ENTRY *Link;
jcarsey94b17fa2009-05-07 18:46:18 +00001323 EFI_SHELL_FILE_INFO_NO_CONST *NewInfo;
1324
1325 //
jcarsey9b3bf082009-06-23 21:15:07 +00001326 // ASSERTs
jcarsey94b17fa2009-05-07 18:46:18 +00001327 //
jcarsey9b3bf082009-06-23 21:15:07 +00001328 ASSERT(FileList != NULL);
1329 ASSERT(ListHead != NULL);
jcarsey94b17fa2009-05-07 18:46:18 +00001330
1331 //
1332 // enumerate through each member of the old list and copy
1333 //
jcarseyd2b45642009-05-11 18:02:16 +00001334 for (Link = FileList->ForwardLink; Link != FileList; Link = Link->ForwardLink) {
jcarsey94b17fa2009-05-07 18:46:18 +00001335 OldInfo = CR (Link, SHELL_FILE_ARG, Link, SHELL_FILE_ARG_SIGNATURE);
jcarseya405b862010-09-14 05:18:09 +00001336 ASSERT(OldInfo != NULL);
1337
1338 //
1339 // Skip ones that failed to open...
1340 //
1341 if (OldInfo->Status != EFI_SUCCESS) {
1342 continue;
1343 }
jcarsey94b17fa2009-05-07 18:46:18 +00001344
1345 //
1346 // make sure the old list was valid
1347 //
jcarsey94b17fa2009-05-07 18:46:18 +00001348 ASSERT(OldInfo->Info != NULL);
1349 ASSERT(OldInfo->FullName != NULL);
1350 ASSERT(OldInfo->FileName != NULL);
1351
1352 //
1353 // allocate a new EFI_SHELL_FILE_INFO object
1354 //
1355 NewInfo = AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));
jcarseyc9d92df2010-02-03 15:37:54 +00001356 ASSERT(NewInfo != NULL);
1357 if (NewInfo == NULL) {
1358 break;
1359 }
jcarsey1e6e84c2010-01-25 20:05:08 +00001360
1361 //
jcarsey94b17fa2009-05-07 18:46:18 +00001362 // copy the simple items
1363 //
1364 NewInfo->Handle = OldInfo->Handle;
1365 NewInfo->Status = OldInfo->Status;
1366
jcarseyd2b45642009-05-11 18:02:16 +00001367 // old shell checks for 0 not NULL
1368 OldInfo->Handle = 0;
1369
jcarsey94b17fa2009-05-07 18:46:18 +00001370 //
1371 // allocate new space to copy strings and structure
1372 //
1373 NewInfo->FullName = AllocateZeroPool(StrSize(OldInfo->FullName));
1374 NewInfo->FileName = AllocateZeroPool(StrSize(OldInfo->FileName));
1375 NewInfo->Info = AllocateZeroPool((UINTN)OldInfo->Info->Size);
jcarsey1e6e84c2010-01-25 20:05:08 +00001376
jcarsey94b17fa2009-05-07 18:46:18 +00001377 //
1378 // make sure all the memory allocations were sucessful
1379 //
1380 ASSERT(NewInfo->FullName != NULL);
1381 ASSERT(NewInfo->FileName != NULL);
1382 ASSERT(NewInfo->Info != NULL);
1383
1384 //
1385 // Copt the strings and structure
1386 //
1387 StrCpy(NewInfo->FullName, OldInfo->FullName);
1388 StrCpy(NewInfo->FileName, OldInfo->FileName);
1389 gBS->CopyMem (NewInfo->Info, OldInfo->Info, (UINTN)OldInfo->Info->Size);
1390
1391 //
1392 // add that to the list
1393 //
jcarsey9b3bf082009-06-23 21:15:07 +00001394 InsertTailList(ListHead, &NewInfo->Link);
jcarsey94b17fa2009-05-07 18:46:18 +00001395 }
1396 return (ListHead);
1397}
1398/**
1399 Opens a group of files based on a path.
1400
jcarsey1e6e84c2010-01-25 20:05:08 +00001401 This function uses the Arg to open all the matching files. Each matched
1402 file has a SHELL_FILE_ARG structure to record the file information. These
1403 structures are placed on the list ListHead. Users can get the SHELL_FILE_ARG
jcarsey94b17fa2009-05-07 18:46:18 +00001404 structures from ListHead to access each file. This function supports wildcards
jcarsey1e6e84c2010-01-25 20:05:08 +00001405 and will process '?' and '*' as such. the list must be freed with a call to
jcarsey94b17fa2009-05-07 18:46:18 +00001406 ShellCloseFileMetaArg().
1407
jcarsey1e6e84c2010-01-25 20:05:08 +00001408 If you are NOT appending to an existing list *ListHead must be NULL. If
jcarsey5f7431d2009-07-10 18:06:01 +00001409 *ListHead is NULL then it must be callee freed.
jcarsey94b17fa2009-05-07 18:46:18 +00001410
1411 @param Arg pointer to path string
1412 @param OpenMode mode to open files with
1413 @param ListHead head of linked list of results
1414
jcarsey1e6e84c2010-01-25 20:05:08 +00001415 @retval EFI_SUCCESS the operation was sucessful and the list head
jcarsey94b17fa2009-05-07 18:46:18 +00001416 contains the list of opened files
jcarsey94b17fa2009-05-07 18:46:18 +00001417 @return != EFI_SUCCESS the operation failed
1418
1419 @sa InternalShellConvertFileListType
1420**/
1421EFI_STATUS
1422EFIAPI
1423ShellOpenFileMetaArg (
1424 IN CHAR16 *Arg,
1425 IN UINT64 OpenMode,
1426 IN OUT EFI_SHELL_FILE_INFO **ListHead
1427 )
1428{
1429 EFI_STATUS Status;
jcarsey9b3bf082009-06-23 21:15:07 +00001430 LIST_ENTRY mOldStyleFileList;
jcarsey1e6e84c2010-01-25 20:05:08 +00001431
jcarsey94b17fa2009-05-07 18:46:18 +00001432 //
1433 // ASSERT that Arg and ListHead are not NULL
1434 //
1435 ASSERT(Arg != NULL);
1436 ASSERT(ListHead != NULL);
1437
jcarsey1e6e84c2010-01-25 20:05:08 +00001438 //
jcarsey94b17fa2009-05-07 18:46:18 +00001439 // Check for UEFI Shell 2.0 protocols
1440 //
1441 if (mEfiShellProtocol != NULL) {
jcarsey5f7431d2009-07-10 18:06:01 +00001442 if (*ListHead == NULL) {
1443 *ListHead = (EFI_SHELL_FILE_INFO*)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));
1444 if (*ListHead == NULL) {
1445 return (EFI_OUT_OF_RESOURCES);
1446 }
1447 InitializeListHead(&((*ListHead)->Link));
jcarsey1e6e84c2010-01-25 20:05:08 +00001448 }
1449 Status = mEfiShellProtocol->OpenFileList(Arg,
1450 OpenMode,
jcarsey2247dde2009-11-09 18:08:58 +00001451 ListHead);
1452 if (EFI_ERROR(Status)) {
1453 mEfiShellProtocol->RemoveDupInFileList(ListHead);
1454 } else {
1455 Status = mEfiShellProtocol->RemoveDupInFileList(ListHead);
1456 }
jcarseya405b862010-09-14 05:18:09 +00001457 if (*ListHead != NULL && IsListEmpty(&(*ListHead)->Link)) {
1458 FreePool(*ListHead);
1459 *ListHead = NULL;
1460 return (EFI_NOT_FOUND);
1461 }
jcarsey2247dde2009-11-09 18:08:58 +00001462 return (Status);
jcarsey1e6e84c2010-01-25 20:05:08 +00001463 }
jcarsey94b17fa2009-05-07 18:46:18 +00001464
1465 //
1466 // ASSERT that we must have EFI shell
1467 //
1468 ASSERT(mEfiShellEnvironment2 != NULL);
1469
1470 //
jcarsey94b17fa2009-05-07 18:46:18 +00001471 // make sure the list head is initialized
1472 //
jcarsey9b3bf082009-06-23 21:15:07 +00001473 InitializeListHead(&mOldStyleFileList);
jcarsey94b17fa2009-05-07 18:46:18 +00001474
1475 //
1476 // Get the EFI Shell list of files
1477 //
jcarsey9b3bf082009-06-23 21:15:07 +00001478 Status = mEfiShellEnvironment2->FileMetaArg(Arg, &mOldStyleFileList);
jcarsey94b17fa2009-05-07 18:46:18 +00001479 if (EFI_ERROR(Status)) {
1480 *ListHead = NULL;
1481 return (Status);
1482 }
1483
jcarsey9b3bf082009-06-23 21:15:07 +00001484 if (*ListHead == NULL) {
1485 *ListHead = (EFI_SHELL_FILE_INFO *)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));
1486 if (*ListHead == NULL) {
1487 return (EFI_OUT_OF_RESOURCES);
1488 }
jcarseya405b862010-09-14 05:18:09 +00001489 InitializeListHead(&((*ListHead)->Link));
jcarsey9b3bf082009-06-23 21:15:07 +00001490 }
1491
jcarsey94b17fa2009-05-07 18:46:18 +00001492 //
1493 // Convert that to equivalent of UEFI Shell 2.0 structure
1494 //
jcarsey9b3bf082009-06-23 21:15:07 +00001495 InternalShellConvertFileListType(&mOldStyleFileList, &(*ListHead)->Link);
jcarsey94b17fa2009-05-07 18:46:18 +00001496
1497 //
jcarseyd2b45642009-05-11 18:02:16 +00001498 // Free the EFI Shell version that was converted.
1499 //
jcarsey9b3bf082009-06-23 21:15:07 +00001500 mEfiShellEnvironment2->FreeFileList(&mOldStyleFileList);
jcarsey94b17fa2009-05-07 18:46:18 +00001501
jcarseya405b862010-09-14 05:18:09 +00001502 if ((*ListHead)->Link.ForwardLink == (*ListHead)->Link.BackLink && (*ListHead)->Link.BackLink == &((*ListHead)->Link)) {
1503 FreePool(*ListHead);
1504 *ListHead = NULL;
1505 Status = EFI_NOT_FOUND;
1506 }
1507
jcarsey94b17fa2009-05-07 18:46:18 +00001508 return (Status);
1509}
1510/**
jcarseya405b862010-09-14 05:18:09 +00001511 Free the linked list returned from ShellOpenFileMetaArg.
jcarsey94b17fa2009-05-07 18:46:18 +00001512
jcarseya405b862010-09-14 05:18:09 +00001513 if ListHead is NULL then ASSERT().
jcarsey94b17fa2009-05-07 18:46:18 +00001514
jcarseya405b862010-09-14 05:18:09 +00001515 @param ListHead the pointer to free.
jcarsey94b17fa2009-05-07 18:46:18 +00001516
jcarseya405b862010-09-14 05:18:09 +00001517 @retval EFI_SUCCESS the operation was sucessful.
jcarsey94b17fa2009-05-07 18:46:18 +00001518**/
1519EFI_STATUS
1520EFIAPI
1521ShellCloseFileMetaArg (
1522 IN OUT EFI_SHELL_FILE_INFO **ListHead
1523 )
1524{
1525 LIST_ENTRY *Node;
1526
1527 //
1528 // ASSERT that ListHead is not NULL
1529 //
1530 ASSERT(ListHead != NULL);
1531
jcarsey1e6e84c2010-01-25 20:05:08 +00001532 //
jcarsey94b17fa2009-05-07 18:46:18 +00001533 // Check for UEFI Shell 2.0 protocols
1534 //
1535 if (mEfiShellProtocol != NULL) {
1536 return (mEfiShellProtocol->FreeFileList(ListHead));
1537 } else {
1538 //
jcarsey1e6e84c2010-01-25 20:05:08 +00001539 // Since this is EFI Shell version we need to free our internally made copy
jcarsey94b17fa2009-05-07 18:46:18 +00001540 // of the list
1541 //
jcarsey1e6e84c2010-01-25 20:05:08 +00001542 for ( Node = GetFirstNode(&(*ListHead)->Link)
jcarseya405b862010-09-14 05:18:09 +00001543 ; *ListHead != NULL && !IsListEmpty(&(*ListHead)->Link)
jcarsey9b3bf082009-06-23 21:15:07 +00001544 ; Node = GetFirstNode(&(*ListHead)->Link)) {
jcarsey94b17fa2009-05-07 18:46:18 +00001545 RemoveEntryList(Node);
jcarseya405b862010-09-14 05:18:09 +00001546 ((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 +00001547 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->FullName);
1548 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->FileName);
1549 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->Info);
1550 FreePool((EFI_SHELL_FILE_INFO_NO_CONST*)Node);
1551 }
1552 return EFI_SUCCESS;
1553 }
1554}
1555
jcarsey125c2cf2009-11-18 21:36:50 +00001556/**
1557 Find a file by searching the CWD and then the path.
1558
jcarseyb3011f42010-01-11 21:49:04 +00001559 If FileName is NULL then ASSERT.
jcarsey125c2cf2009-11-18 21:36:50 +00001560
jcarseyb3011f42010-01-11 21:49:04 +00001561 If the return value is not NULL then the memory must be caller freed.
jcarsey125c2cf2009-11-18 21:36:50 +00001562
1563 @param FileName Filename string.
1564
1565 @retval NULL the file was not found
1566 @return !NULL the full path to the file.
1567**/
1568CHAR16 *
1569EFIAPI
1570ShellFindFilePath (
1571 IN CONST CHAR16 *FileName
1572 )
1573{
1574 CONST CHAR16 *Path;
jcarseya405b862010-09-14 05:18:09 +00001575 SHELL_FILE_HANDLE Handle;
jcarsey125c2cf2009-11-18 21:36:50 +00001576 EFI_STATUS Status;
1577 CHAR16 *RetVal;
1578 CHAR16 *TestPath;
1579 CONST CHAR16 *Walker;
jcarsey36a9d672009-11-20 21:13:41 +00001580 UINTN Size;
jcarsey1cd45e72010-01-29 15:07:44 +00001581 CHAR16 *TempChar;
jcarsey125c2cf2009-11-18 21:36:50 +00001582
1583 RetVal = NULL;
1584
jcarseya405b862010-09-14 05:18:09 +00001585 //
1586 // First make sure its not an absolute path.
1587 //
1588 Status = ShellOpenFileByName(FileName, &Handle, EFI_FILE_MODE_READ, 0);
1589 if (!EFI_ERROR(Status)){
1590 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {
1591 ASSERT(RetVal == NULL);
1592 RetVal = StrnCatGrow(&RetVal, NULL, FileName, 0);
1593 ShellCloseFile(&Handle);
1594 return (RetVal);
1595 } else {
1596 ShellCloseFile(&Handle);
1597 }
1598 }
1599
jcarsey125c2cf2009-11-18 21:36:50 +00001600 Path = ShellGetEnvironmentVariable(L"cwd");
1601 if (Path != NULL) {
jcarsey36a9d672009-11-20 21:13:41 +00001602 Size = StrSize(Path);
1603 Size += StrSize(FileName);
1604 TestPath = AllocateZeroPool(Size);
jcarseyc9d92df2010-02-03 15:37:54 +00001605 ASSERT(TestPath != NULL);
1606 if (TestPath == NULL) {
1607 return (NULL);
1608 }
jcarsey125c2cf2009-11-18 21:36:50 +00001609 StrCpy(TestPath, Path);
1610 StrCat(TestPath, FileName);
1611 Status = ShellOpenFileByName(TestPath, &Handle, EFI_FILE_MODE_READ, 0);
1612 if (!EFI_ERROR(Status)){
jcarseya405b862010-09-14 05:18:09 +00001613 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {
1614 ASSERT(RetVal == NULL);
1615 RetVal = StrnCatGrow(&RetVal, NULL, TestPath, 0);
1616 ShellCloseFile(&Handle);
1617 FreePool(TestPath);
1618 return (RetVal);
1619 } else {
1620 ShellCloseFile(&Handle);
1621 }
jcarsey125c2cf2009-11-18 21:36:50 +00001622 }
1623 FreePool(TestPath);
1624 }
1625 Path = ShellGetEnvironmentVariable(L"path");
1626 if (Path != NULL) {
jcarseya405b862010-09-14 05:18:09 +00001627 Size = StrSize(Path)+sizeof(CHAR16);
jcarsey36a9d672009-11-20 21:13:41 +00001628 Size += StrSize(FileName);
1629 TestPath = AllocateZeroPool(Size);
jcarsey3e082d52010-10-04 16:44:57 +00001630 if (TestPath == NULL) {
1631 return (NULL);
1632 }
jcarsey1e6e84c2010-01-25 20:05:08 +00001633 Walker = (CHAR16*)Path;
jcarsey125c2cf2009-11-18 21:36:50 +00001634 do {
1635 CopyMem(TestPath, Walker, StrSize(Walker));
jcarsey3e082d52010-10-04 16:44:57 +00001636 if (TestPath != NULL) {
1637 TempChar = StrStr(TestPath, L";");
1638 if (TempChar != NULL) {
1639 *TempChar = CHAR_NULL;
1640 }
1641 if (TestPath[StrLen(TestPath)-1] != L'\\') {
1642 StrCat(TestPath, L"\\");
1643 }
jcarsey89e85372011-04-13 23:37:50 +00001644 if (FileName[0] == L'\\') {
1645 FileName++;
1646 }
jcarsey3e082d52010-10-04 16:44:57 +00001647 StrCat(TestPath, FileName);
1648 if (StrStr(Walker, L";") != NULL) {
1649 Walker = StrStr(Walker, L";") + 1;
jcarseya405b862010-09-14 05:18:09 +00001650 } else {
jcarsey3e082d52010-10-04 16:44:57 +00001651 Walker = NULL;
1652 }
1653 Status = ShellOpenFileByName(TestPath, &Handle, EFI_FILE_MODE_READ, 0);
1654 if (!EFI_ERROR(Status)){
1655 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {
1656 ASSERT(RetVal == NULL);
1657 RetVal = StrnCatGrow(&RetVal, NULL, TestPath, 0);
1658 ShellCloseFile(&Handle);
1659 break;
1660 } else {
1661 ShellCloseFile(&Handle);
1662 }
jcarseya405b862010-09-14 05:18:09 +00001663 }
jcarsey125c2cf2009-11-18 21:36:50 +00001664 }
1665 } while (Walker != NULL && Walker[0] != CHAR_NULL);
1666 FreePool(TestPath);
1667 }
1668 return (RetVal);
1669}
1670
jcarseyb3011f42010-01-11 21:49:04 +00001671/**
jcarsey1e6e84c2010-01-25 20:05:08 +00001672 Find a file by searching the CWD and then the path with a variable set of file
1673 extensions. If the file is not found it will append each extension in the list
jcarseyb3011f42010-01-11 21:49:04 +00001674 in the order provided and return the first one that is successful.
1675
1676 If FileName is NULL, then ASSERT.
1677 If FileExtension is NULL, then behavior is identical to ShellFindFilePath.
1678
1679 If the return value is not NULL then the memory must be caller freed.
1680
1681 @param[in] FileName Filename string.
1682 @param[in] FileExtension Semi-colon delimeted list of possible extensions.
1683
1684 @retval NULL The file was not found.
1685 @retval !NULL The path to the file.
1686**/
1687CHAR16 *
1688EFIAPI
1689ShellFindFilePathEx (
1690 IN CONST CHAR16 *FileName,
1691 IN CONST CHAR16 *FileExtension
1692 )
1693{
1694 CHAR16 *TestPath;
1695 CHAR16 *RetVal;
1696 CONST CHAR16 *ExtensionWalker;
jcarsey9e926b62010-01-14 20:26:39 +00001697 UINTN Size;
jcarsey1cd45e72010-01-29 15:07:44 +00001698 CHAR16 *TempChar;
jcarseyc9d92df2010-02-03 15:37:54 +00001699 CHAR16 *TempChar2;
jcarsey1cd45e72010-01-29 15:07:44 +00001700
jcarseyb3011f42010-01-11 21:49:04 +00001701 ASSERT(FileName != NULL);
1702 if (FileExtension == NULL) {
1703 return (ShellFindFilePath(FileName));
1704 }
1705 RetVal = ShellFindFilePath(FileName);
1706 if (RetVal != NULL) {
1707 return (RetVal);
1708 }
jcarsey9e926b62010-01-14 20:26:39 +00001709 Size = StrSize(FileName);
1710 Size += StrSize(FileExtension);
1711 TestPath = AllocateZeroPool(Size);
jcarseyc9d92df2010-02-03 15:37:54 +00001712 ASSERT(TestPath != NULL);
1713 if (TestPath == NULL) {
1714 return (NULL);
1715 }
jcarseya405b862010-09-14 05:18:09 +00001716 for (ExtensionWalker = FileExtension, TempChar2 = (CHAR16*)FileExtension; TempChar2 != NULL ; ExtensionWalker = TempChar2 + 1){
jcarseyb3011f42010-01-11 21:49:04 +00001717 StrCpy(TestPath, FileName);
jcarseya405b862010-09-14 05:18:09 +00001718 if (ExtensionWalker != NULL) {
1719 StrCat(TestPath, ExtensionWalker);
1720 }
jcarsey1cd45e72010-01-29 15:07:44 +00001721 TempChar = StrStr(TestPath, L";");
1722 if (TempChar != NULL) {
1723 *TempChar = CHAR_NULL;
jcarseyb3011f42010-01-11 21:49:04 +00001724 }
1725 RetVal = ShellFindFilePath(TestPath);
1726 if (RetVal != NULL) {
1727 break;
1728 }
jcarseya405b862010-09-14 05:18:09 +00001729 ASSERT(ExtensionWalker != NULL);
jcarseyc9d92df2010-02-03 15:37:54 +00001730 TempChar2 = StrStr(ExtensionWalker, L";");
jcarseyb3011f42010-01-11 21:49:04 +00001731 }
1732 FreePool(TestPath);
1733 return (RetVal);
1734}
1735
jcarsey94b17fa2009-05-07 18:46:18 +00001736typedef struct {
jcarsey9b3bf082009-06-23 21:15:07 +00001737 LIST_ENTRY Link;
jcarsey94b17fa2009-05-07 18:46:18 +00001738 CHAR16 *Name;
jcarseya405b862010-09-14 05:18:09 +00001739 SHELL_PARAM_TYPE Type;
jcarsey94b17fa2009-05-07 18:46:18 +00001740 CHAR16 *Value;
1741 UINTN OriginalPosition;
1742} SHELL_PARAM_PACKAGE;
1743
1744/**
jcarsey1e6e84c2010-01-25 20:05:08 +00001745 Checks the list of valid arguments and returns TRUE if the item was found. If the
jcarsey94b17fa2009-05-07 18:46:18 +00001746 return value is TRUE then the type parameter is set also.
jcarsey1e6e84c2010-01-25 20:05:08 +00001747
jcarsey94b17fa2009-05-07 18:46:18 +00001748 if CheckList is NULL then ASSERT();
1749 if Name is NULL then ASSERT();
1750 if Type is NULL then ASSERT();
1751
jcarsey94b17fa2009-05-07 18:46:18 +00001752 @param Name pointer to Name of parameter found
1753 @param CheckList List to check against
jcarseya405b862010-09-14 05:18:09 +00001754 @param Type pointer to type of parameter if it was found
jcarsey94b17fa2009-05-07 18:46:18 +00001755
1756 @retval TRUE the Parameter was found. Type is valid.
1757 @retval FALSE the Parameter was not found. Type is not valid.
1758**/
1759BOOLEAN
1760EFIAPI
jcarseyd2b45642009-05-11 18:02:16 +00001761InternalIsOnCheckList (
jcarsey94b17fa2009-05-07 18:46:18 +00001762 IN CONST CHAR16 *Name,
1763 IN CONST SHELL_PARAM_ITEM *CheckList,
jcarsey252d9452011-03-25 20:49:53 +00001764 OUT SHELL_PARAM_TYPE *Type
jcarseya405b862010-09-14 05:18:09 +00001765 )
1766{
jcarsey94b17fa2009-05-07 18:46:18 +00001767 SHELL_PARAM_ITEM *TempListItem;
jcarsey252d9452011-03-25 20:49:53 +00001768 CHAR16 *TempString;
jcarsey94b17fa2009-05-07 18:46:18 +00001769
1770 //
1771 // ASSERT that all 3 pointer parameters aren't NULL
1772 //
1773 ASSERT(CheckList != NULL);
1774 ASSERT(Type != NULL);
1775 ASSERT(Name != NULL);
1776
1777 //
jcarseyd2b45642009-05-11 18:02:16 +00001778 // question mark and page break mode are always supported
1779 //
1780 if ((StrCmp(Name, L"-?") == 0) ||
1781 (StrCmp(Name, L"-b") == 0)
jcarseya405b862010-09-14 05:18:09 +00001782 ) {
jcarsey252d9452011-03-25 20:49:53 +00001783 *Type = TypeFlag;
jcarseyd2b45642009-05-11 18:02:16 +00001784 return (TRUE);
1785 }
1786
1787 //
jcarsey94b17fa2009-05-07 18:46:18 +00001788 // Enumerate through the list
1789 //
1790 for (TempListItem = (SHELL_PARAM_ITEM*)CheckList ; TempListItem->Name != NULL ; TempListItem++) {
1791 //
jcarsey9eb53ac2009-07-08 17:26:58 +00001792 // If the Type is TypeStart only check the first characters of the passed in param
1793 // If it matches set the type and return TRUE
jcarsey94b17fa2009-05-07 18:46:18 +00001794 //
jcarsey252d9452011-03-25 20:49:53 +00001795 if (TempListItem->Type == TypeStart) {
1796 if (StrnCmp(Name, TempListItem->Name, StrLen(TempListItem->Name)) == 0) {
1797 *Type = TempListItem->Type;
1798 return (TRUE);
1799 }
1800 TempString = NULL;
1801 TempString = StrnCatGrow(&TempString, NULL, Name, StrLen(TempListItem->Name));
1802 if (TempString != NULL) {
1803 if (StringNoCaseCompare(&TempString, &TempListItem->Name) == 0) {
1804 *Type = TempListItem->Type;
1805 FreePool(TempString);
1806 return (TRUE);
1807 }
1808 FreePool(TempString);
1809 }
1810 } else if (StringNoCaseCompare(&Name, &TempListItem->Name) == 0) {
jcarsey94b17fa2009-05-07 18:46:18 +00001811 *Type = TempListItem->Type;
1812 return (TRUE);
1813 }
1814 }
jcarsey2247dde2009-11-09 18:08:58 +00001815
jcarsey94b17fa2009-05-07 18:46:18 +00001816 return (FALSE);
1817}
1818/**
jcarseyd2b45642009-05-11 18:02:16 +00001819 Checks the string for indicators of "flag" status. this is a leading '/', '-', or '+'
jcarsey94b17fa2009-05-07 18:46:18 +00001820
jcarseya405b862010-09-14 05:18:09 +00001821 @param[in] Name pointer to Name of parameter found
1822 @param[in] AlwaysAllowNumbers TRUE to allow numbers, FALSE to not.
jcarsey94b17fa2009-05-07 18:46:18 +00001823
1824 @retval TRUE the Parameter is a flag.
jcarseya405b862010-09-14 05:18:09 +00001825 @retval FALSE the Parameter not a flag.
jcarsey94b17fa2009-05-07 18:46:18 +00001826**/
1827BOOLEAN
1828EFIAPI
jcarseyd2b45642009-05-11 18:02:16 +00001829InternalIsFlag (
jcarsey2247dde2009-11-09 18:08:58 +00001830 IN CONST CHAR16 *Name,
1831 IN BOOLEAN AlwaysAllowNumbers
jcarsey94b17fa2009-05-07 18:46:18 +00001832 )
1833{
1834 //
1835 // ASSERT that Name isn't NULL
1836 //
1837 ASSERT(Name != NULL);
1838
1839 //
jcarsey2247dde2009-11-09 18:08:58 +00001840 // If we accept numbers then dont return TRUE. (they will be values)
1841 //
jcarseya405b862010-09-14 05:18:09 +00001842 if (((Name[0] == L'-' || Name[0] == L'+') && ShellIsHexaDecimalDigitCharacter(Name[1])) && AlwaysAllowNumbers) {
jcarsey2247dde2009-11-09 18:08:58 +00001843 return (FALSE);
1844 }
1845
1846 //
jcarseya405b862010-09-14 05:18:09 +00001847 // If the Name has a /, +, or - as the first character return TRUE
jcarsey94b17fa2009-05-07 18:46:18 +00001848 //
jcarsey1e6e84c2010-01-25 20:05:08 +00001849 if ((Name[0] == L'/') ||
jcarseyd2b45642009-05-11 18:02:16 +00001850 (Name[0] == L'-') ||
1851 (Name[0] == L'+')
jcarseya405b862010-09-14 05:18:09 +00001852 ) {
jcarsey94b17fa2009-05-07 18:46:18 +00001853 return (TRUE);
1854 }
1855 return (FALSE);
1856}
1857
1858/**
jcarsey1e6e84c2010-01-25 20:05:08 +00001859 Checks the command line arguments passed against the list of valid ones.
jcarsey94b17fa2009-05-07 18:46:18 +00001860
1861 If no initialization is required, then return RETURN_SUCCESS.
jcarsey1e6e84c2010-01-25 20:05:08 +00001862
jcarseya405b862010-09-14 05:18:09 +00001863 @param[in] CheckList pointer to list of parameters to check
1864 @param[out] CheckPackage pointer to pointer to list checked values
1865 @param[out] ProblemParam optional pointer to pointer to unicode string for
jcarseyd2b45642009-05-11 18:02:16 +00001866 the paramater that caused failure. If used then the
1867 caller is responsible for freeing the memory.
jcarseya405b862010-09-14 05:18:09 +00001868 @param[in] AutoPageBreak will automatically set PageBreakEnabled for "b" parameter
1869 @param[in] Argv pointer to array of parameters
1870 @param[in] Argc Count of parameters in Argv
1871 @param[in] AlwaysAllowNumbers TRUE to allow numbers always, FALSE otherwise.
jcarsey94b17fa2009-05-07 18:46:18 +00001872
1873 @retval EFI_SUCCESS The operation completed sucessfully.
1874 @retval EFI_OUT_OF_RESOURCES A memory allocation failed
1875 @retval EFI_INVALID_PARAMETER A parameter was invalid
jcarsey1e6e84c2010-01-25 20:05:08 +00001876 @retval EFI_VOLUME_CORRUPTED the command line was corrupt. an argument was
1877 duplicated. the duplicated command line argument
jcarsey94b17fa2009-05-07 18:46:18 +00001878 was returned in ProblemParam if provided.
jcarsey1e6e84c2010-01-25 20:05:08 +00001879 @retval EFI_NOT_FOUND a argument required a value that was missing.
jcarsey94b17fa2009-05-07 18:46:18 +00001880 the invalid command line argument was returned in
1881 ProblemParam if provided.
1882**/
1883EFI_STATUS
1884EFIAPI
1885InternalCommandLineParse (
1886 IN CONST SHELL_PARAM_ITEM *CheckList,
1887 OUT LIST_ENTRY **CheckPackage,
1888 OUT CHAR16 **ProblemParam OPTIONAL,
1889 IN BOOLEAN AutoPageBreak,
1890 IN CONST CHAR16 **Argv,
jcarsey2247dde2009-11-09 18:08:58 +00001891 IN UINTN Argc,
1892 IN BOOLEAN AlwaysAllowNumbers
jcarseya405b862010-09-14 05:18:09 +00001893 )
1894{
jcarsey94b17fa2009-05-07 18:46:18 +00001895 UINTN LoopCounter;
jcarsey252d9452011-03-25 20:49:53 +00001896 SHELL_PARAM_TYPE CurrentItemType;
jcarsey94b17fa2009-05-07 18:46:18 +00001897 SHELL_PARAM_PACKAGE *CurrentItemPackage;
jcarsey125c2cf2009-11-18 21:36:50 +00001898 UINTN GetItemValue;
1899 UINTN ValueSize;
jcarseya405b862010-09-14 05:18:09 +00001900 UINTN Count;
jcarsey252d9452011-03-25 20:49:53 +00001901 CONST CHAR16 *TempPointer;
jcarsey94b17fa2009-05-07 18:46:18 +00001902
1903 CurrentItemPackage = NULL;
jcarsey125c2cf2009-11-18 21:36:50 +00001904 GetItemValue = 0;
1905 ValueSize = 0;
jcarseya405b862010-09-14 05:18:09 +00001906 Count = 0;
jcarsey94b17fa2009-05-07 18:46:18 +00001907
1908 //
1909 // If there is only 1 item we dont need to do anything
1910 //
jcarseya405b862010-09-14 05:18:09 +00001911 if (Argc < 1) {
jcarsey94b17fa2009-05-07 18:46:18 +00001912 *CheckPackage = NULL;
1913 return (EFI_SUCCESS);
1914 }
1915
1916 //
jcarsey2247dde2009-11-09 18:08:58 +00001917 // ASSERTs
1918 //
1919 ASSERT(CheckList != NULL);
1920 ASSERT(Argv != NULL);
1921
1922 //
jcarsey94b17fa2009-05-07 18:46:18 +00001923 // initialize the linked list
1924 //
1925 *CheckPackage = (LIST_ENTRY*)AllocateZeroPool(sizeof(LIST_ENTRY));
1926 InitializeListHead(*CheckPackage);
1927
1928 //
1929 // loop through each of the arguments
1930 //
1931 for (LoopCounter = 0 ; LoopCounter < Argc ; ++LoopCounter) {
1932 if (Argv[LoopCounter] == NULL) {
1933 //
1934 // do nothing for NULL argv
1935 //
jcarseya405b862010-09-14 05:18:09 +00001936 } else if (InternalIsOnCheckList(Argv[LoopCounter], CheckList, &CurrentItemType)) {
jcarsey94b17fa2009-05-07 18:46:18 +00001937 //
jcarsey2247dde2009-11-09 18:08:58 +00001938 // We might have leftover if last parameter didnt have optional value
1939 //
jcarsey125c2cf2009-11-18 21:36:50 +00001940 if (GetItemValue != 0) {
1941 GetItemValue = 0;
jcarsey2247dde2009-11-09 18:08:58 +00001942 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
1943 }
1944 //
jcarsey94b17fa2009-05-07 18:46:18 +00001945 // this is a flag
1946 //
jcarsey252d9452011-03-25 20:49:53 +00001947 CurrentItemPackage = AllocateZeroPool(sizeof(SHELL_PARAM_PACKAGE));
jcarsey94b17fa2009-05-07 18:46:18 +00001948 ASSERT(CurrentItemPackage != NULL);
jcarsey252d9452011-03-25 20:49:53 +00001949 CurrentItemPackage->Name = AllocateZeroPool(StrSize(Argv[LoopCounter]));
jcarsey94b17fa2009-05-07 18:46:18 +00001950 ASSERT(CurrentItemPackage->Name != NULL);
1951 StrCpy(CurrentItemPackage->Name, Argv[LoopCounter]);
1952 CurrentItemPackage->Type = CurrentItemType;
1953 CurrentItemPackage->OriginalPosition = (UINTN)(-1);
jcarseyb1f95a02009-06-16 00:23:19 +00001954 CurrentItemPackage->Value = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +00001955
1956 //
1957 // Does this flag require a value
1958 //
jcarsey125c2cf2009-11-18 21:36:50 +00001959 switch (CurrentItemPackage->Type) {
jcarsey94b17fa2009-05-07 18:46:18 +00001960 //
jcarsey125c2cf2009-11-18 21:36:50 +00001961 // possibly trigger the next loop(s) to populate the value of this item
jcarsey1e6e84c2010-01-25 20:05:08 +00001962 //
jcarsey125c2cf2009-11-18 21:36:50 +00001963 case TypeValue:
jcarsey1e6e84c2010-01-25 20:05:08 +00001964 GetItemValue = 1;
jcarsey125c2cf2009-11-18 21:36:50 +00001965 ValueSize = 0;
1966 break;
1967 case TypeDoubleValue:
1968 GetItemValue = 2;
1969 ValueSize = 0;
1970 break;
1971 case TypeMaxValue:
1972 GetItemValue = (UINTN)(-1);
1973 ValueSize = 0;
1974 break;
1975 default:
1976 //
1977 // this item has no value expected; we are done
1978 //
1979 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
1980 ASSERT(GetItemValue == 0);
1981 break;
jcarsey94b17fa2009-05-07 18:46:18 +00001982 }
jcarseya405b862010-09-14 05:18:09 +00001983 } else if (GetItemValue != 0 && !InternalIsFlag(Argv[LoopCounter], AlwaysAllowNumbers)) {
jcarseyb1f95a02009-06-16 00:23:19 +00001984 ASSERT(CurrentItemPackage != NULL);
1985 //
jcarsey125c2cf2009-11-18 21:36:50 +00001986 // get the item VALUE for a previous flag
jcarseyb1f95a02009-06-16 00:23:19 +00001987 //
jcarsey125c2cf2009-11-18 21:36:50 +00001988 CurrentItemPackage->Value = ReallocatePool(ValueSize, ValueSize + StrSize(Argv[LoopCounter]) + sizeof(CHAR16), CurrentItemPackage->Value);
jcarseyb1f95a02009-06-16 00:23:19 +00001989 ASSERT(CurrentItemPackage->Value != NULL);
jcarsey125c2cf2009-11-18 21:36:50 +00001990 if (ValueSize == 0) {
1991 StrCpy(CurrentItemPackage->Value, Argv[LoopCounter]);
1992 } else {
1993 StrCat(CurrentItemPackage->Value, L" ");
1994 StrCat(CurrentItemPackage->Value, Argv[LoopCounter]);
1995 }
1996 ValueSize += StrSize(Argv[LoopCounter]) + sizeof(CHAR16);
1997 GetItemValue--;
1998 if (GetItemValue == 0) {
1999 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
2000 }
jcarseya405b862010-09-14 05:18:09 +00002001 } else if (!InternalIsFlag(Argv[LoopCounter], AlwaysAllowNumbers) ){ //|| ProblemParam == NULL) {
jcarseyb1f95a02009-06-16 00:23:19 +00002002 //
2003 // add this one as a non-flag
2004 //
jcarsey252d9452011-03-25 20:49:53 +00002005
2006 TempPointer = Argv[LoopCounter];
2007 if ((*TempPointer == L'^' && *(TempPointer+1) == L'-')
2008 || (*TempPointer == L'^' && *(TempPointer+1) == L'/')
2009 || (*TempPointer == L'^' && *(TempPointer+1) == L'+')
2010 ){
2011 TempPointer++;
2012 }
2013 CurrentItemPackage = AllocateZeroPool(sizeof(SHELL_PARAM_PACKAGE));
jcarseyb1f95a02009-06-16 00:23:19 +00002014 ASSERT(CurrentItemPackage != NULL);
2015 CurrentItemPackage->Name = NULL;
2016 CurrentItemPackage->Type = TypePosition;
jcarsey252d9452011-03-25 20:49:53 +00002017 CurrentItemPackage->Value = AllocateZeroPool(StrSize(TempPointer));
jcarseyb1f95a02009-06-16 00:23:19 +00002018 ASSERT(CurrentItemPackage->Value != NULL);
jcarsey252d9452011-03-25 20:49:53 +00002019 StrCpy(CurrentItemPackage->Value, TempPointer);
jcarseya405b862010-09-14 05:18:09 +00002020 CurrentItemPackage->OriginalPosition = Count++;
jcarsey9b3bf082009-06-23 21:15:07 +00002021 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
jcarsey252d9452011-03-25 20:49:53 +00002022 } else {
jcarsey94b17fa2009-05-07 18:46:18 +00002023 //
2024 // this was a non-recognised flag... error!
2025 //
jcarsey252d9452011-03-25 20:49:53 +00002026 if (ProblemParam != NULL) {
2027 *ProblemParam = AllocateZeroPool(StrSize(Argv[LoopCounter]));
2028 ASSERT(*ProblemParam != NULL);
2029 StrCpy(*ProblemParam, Argv[LoopCounter]);
2030 }
jcarsey94b17fa2009-05-07 18:46:18 +00002031 ShellCommandLineFreeVarList(*CheckPackage);
2032 *CheckPackage = NULL;
2033 return (EFI_VOLUME_CORRUPTED);
jcarsey94b17fa2009-05-07 18:46:18 +00002034 }
2035 }
jcarsey125c2cf2009-11-18 21:36:50 +00002036 if (GetItemValue != 0) {
2037 GetItemValue = 0;
2038 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
2039 }
jcarsey94b17fa2009-05-07 18:46:18 +00002040 //
2041 // support for AutoPageBreak
2042 //
2043 if (AutoPageBreak && ShellCommandLineGetFlag(*CheckPackage, L"-b")) {
2044 ShellSetPageBreakMode(TRUE);
2045 }
2046 return (EFI_SUCCESS);
2047}
2048
2049/**
jcarsey1e6e84c2010-01-25 20:05:08 +00002050 Checks the command line arguments passed against the list of valid ones.
jcarsey94b17fa2009-05-07 18:46:18 +00002051 Optionally removes NULL values first.
jcarsey1e6e84c2010-01-25 20:05:08 +00002052
jcarsey94b17fa2009-05-07 18:46:18 +00002053 If no initialization is required, then return RETURN_SUCCESS.
jcarsey1e6e84c2010-01-25 20:05:08 +00002054
jcarseya405b862010-09-14 05:18:09 +00002055 @param[in] CheckList The pointer to list of parameters to check.
2056 @param[out] CheckPackage The package of checked values.
2057 @param[out] ProblemParam Optional pointer to pointer to unicode string for
jcarsey94b17fa2009-05-07 18:46:18 +00002058 the paramater that caused failure.
jcarseya405b862010-09-14 05:18:09 +00002059 @param[in] AutoPageBreak Will automatically set PageBreakEnabled.
2060 @param[in] AlwaysAllowNumbers Will never fail for number based flags.
jcarsey94b17fa2009-05-07 18:46:18 +00002061
2062 @retval EFI_SUCCESS The operation completed sucessfully.
jcarseya405b862010-09-14 05:18:09 +00002063 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
2064 @retval EFI_INVALID_PARAMETER A parameter was invalid.
2065 @retval EFI_VOLUME_CORRUPTED The command line was corrupt.
2066 @retval EFI_DEVICE_ERROR The commands contained 2 opposing arguments. One
jcarsey1e6e84c2010-01-25 20:05:08 +00002067 of the command line arguments was returned in
jcarsey94b17fa2009-05-07 18:46:18 +00002068 ProblemParam if provided.
jcarseya405b862010-09-14 05:18:09 +00002069 @retval EFI_NOT_FOUND A argument required a value that was missing.
2070 The invalid command line argument was returned in
jcarsey94b17fa2009-05-07 18:46:18 +00002071 ProblemParam if provided.
2072**/
2073EFI_STATUS
2074EFIAPI
jcarsey2247dde2009-11-09 18:08:58 +00002075ShellCommandLineParseEx (
jcarsey94b17fa2009-05-07 18:46:18 +00002076 IN CONST SHELL_PARAM_ITEM *CheckList,
2077 OUT LIST_ENTRY **CheckPackage,
2078 OUT CHAR16 **ProblemParam OPTIONAL,
jcarsey2247dde2009-11-09 18:08:58 +00002079 IN BOOLEAN AutoPageBreak,
2080 IN BOOLEAN AlwaysAllowNumbers
jcarseya405b862010-09-14 05:18:09 +00002081 )
2082{
jcarsey1e6e84c2010-01-25 20:05:08 +00002083 //
jcarsey94b17fa2009-05-07 18:46:18 +00002084 // ASSERT that CheckList and CheckPackage aren't NULL
2085 //
2086 ASSERT(CheckList != NULL);
2087 ASSERT(CheckPackage != NULL);
2088
jcarsey1e6e84c2010-01-25 20:05:08 +00002089 //
jcarsey94b17fa2009-05-07 18:46:18 +00002090 // Check for UEFI Shell 2.0 protocols
2091 //
2092 if (mEfiShellParametersProtocol != NULL) {
jcarsey1e6e84c2010-01-25 20:05:08 +00002093 return (InternalCommandLineParse(CheckList,
2094 CheckPackage,
2095 ProblemParam,
2096 AutoPageBreak,
jljusten08d7f8e2009-06-15 18:42:13 +00002097 (CONST CHAR16**) mEfiShellParametersProtocol->Argv,
jcarsey2247dde2009-11-09 18:08:58 +00002098 mEfiShellParametersProtocol->Argc,
2099 AlwaysAllowNumbers));
jcarsey94b17fa2009-05-07 18:46:18 +00002100 }
2101
jcarsey1e6e84c2010-01-25 20:05:08 +00002102 //
jcarsey94b17fa2009-05-07 18:46:18 +00002103 // ASSERT That EFI Shell is not required
2104 //
2105 ASSERT (mEfiShellInterface != NULL);
jcarsey1e6e84c2010-01-25 20:05:08 +00002106 return (InternalCommandLineParse(CheckList,
2107 CheckPackage,
2108 ProblemParam,
2109 AutoPageBreak,
jljusten08d7f8e2009-06-15 18:42:13 +00002110 (CONST CHAR16**) mEfiShellInterface->Argv,
jcarsey2247dde2009-11-09 18:08:58 +00002111 mEfiShellInterface->Argc,
2112 AlwaysAllowNumbers));
jcarsey94b17fa2009-05-07 18:46:18 +00002113}
2114
2115/**
2116 Frees shell variable list that was returned from ShellCommandLineParse.
2117
2118 This function will free all the memory that was used for the CheckPackage
2119 list of postprocessed shell arguments.
2120
2121 this function has no return value.
2122
2123 if CheckPackage is NULL, then return
2124
2125 @param CheckPackage the list to de-allocate
2126 **/
2127VOID
2128EFIAPI
2129ShellCommandLineFreeVarList (
2130 IN LIST_ENTRY *CheckPackage
jcarseya405b862010-09-14 05:18:09 +00002131 )
2132{
jcarsey94b17fa2009-05-07 18:46:18 +00002133 LIST_ENTRY *Node;
2134
2135 //
2136 // check for CheckPackage == NULL
2137 //
2138 if (CheckPackage == NULL) {
2139 return;
2140 }
2141
2142 //
2143 // for each node in the list
2144 //
jcarsey9eb53ac2009-07-08 17:26:58 +00002145 for ( Node = GetFirstNode(CheckPackage)
jcarseya405b862010-09-14 05:18:09 +00002146 ; !IsListEmpty(CheckPackage)
jcarsey9eb53ac2009-07-08 17:26:58 +00002147 ; Node = GetFirstNode(CheckPackage)
jcarseya405b862010-09-14 05:18:09 +00002148 ){
jcarsey94b17fa2009-05-07 18:46:18 +00002149 //
2150 // Remove it from the list
2151 //
2152 RemoveEntryList(Node);
2153
2154 //
2155 // if it has a name free the name
2156 //
2157 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {
2158 FreePool(((SHELL_PARAM_PACKAGE*)Node)->Name);
2159 }
2160
2161 //
2162 // if it has a value free the value
2163 //
2164 if (((SHELL_PARAM_PACKAGE*)Node)->Value != NULL) {
2165 FreePool(((SHELL_PARAM_PACKAGE*)Node)->Value);
2166 }
jcarsey1e6e84c2010-01-25 20:05:08 +00002167
jcarsey94b17fa2009-05-07 18:46:18 +00002168 //
2169 // free the node structure
2170 //
2171 FreePool((SHELL_PARAM_PACKAGE*)Node);
2172 }
2173 //
2174 // free the list head node
2175 //
2176 FreePool(CheckPackage);
2177}
2178/**
2179 Checks for presence of a flag parameter
2180
2181 flag arguments are in the form of "-<Key>" or "/<Key>", but do not have a value following the key
2182
2183 if CheckPackage is NULL then return FALSE.
2184 if KeyString is NULL then ASSERT()
jcarsey1e6e84c2010-01-25 20:05:08 +00002185
jcarsey94b17fa2009-05-07 18:46:18 +00002186 @param CheckPackage The package of parsed command line arguments
2187 @param KeyString the Key of the command line argument to check for
2188
2189 @retval TRUE the flag is on the command line
2190 @retval FALSE the flag is not on the command line
2191 **/
2192BOOLEAN
2193EFIAPI
2194ShellCommandLineGetFlag (
jcarseya405b862010-09-14 05:18:09 +00002195 IN CONST LIST_ENTRY * CONST CheckPackage,
2196 IN CONST CHAR16 * CONST KeyString
2197 )
2198{
jcarsey94b17fa2009-05-07 18:46:18 +00002199 LIST_ENTRY *Node;
jcarsey252d9452011-03-25 20:49:53 +00002200 CHAR16 *TempString;
jcarsey94b17fa2009-05-07 18:46:18 +00002201
2202 //
2203 // ASSERT that both CheckPackage and KeyString aren't NULL
2204 //
2205 ASSERT(KeyString != NULL);
2206
2207 //
2208 // return FALSE for no package
2209 //
2210 if (CheckPackage == NULL) {
2211 return (FALSE);
2212 }
2213
2214 //
2215 // enumerate through the list of parametrs
2216 //
jcarsey1e6e84c2010-01-25 20:05:08 +00002217 for ( Node = GetFirstNode(CheckPackage)
2218 ; !IsNull (CheckPackage, Node)
2219 ; Node = GetNextNode(CheckPackage, Node)
jcarsey252d9452011-03-25 20:49:53 +00002220 ){
jcarsey94b17fa2009-05-07 18:46:18 +00002221 //
2222 // If the Name matches, return TRUE (and there may be NULL name)
2223 //
2224 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {
jcarsey9eb53ac2009-07-08 17:26:58 +00002225 //
2226 // If Type is TypeStart then only compare the begining of the strings
2227 //
jcarsey252d9452011-03-25 20:49:53 +00002228 if (((SHELL_PARAM_PACKAGE*)Node)->Type == TypeStart) {
2229 if (StrnCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name, StrLen(KeyString)) == 0) {
2230 return (TRUE);
2231 }
2232 TempString = NULL;
2233 TempString = StrnCatGrow(&TempString, NULL, KeyString, StrLen(((SHELL_PARAM_PACKAGE*)Node)->Name));
2234 if (TempString != NULL) {
2235 if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {
2236 FreePool(TempString);
2237 return (TRUE);
2238 }
2239 FreePool(TempString);
2240 }
2241 } else if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {
jcarsey94b17fa2009-05-07 18:46:18 +00002242 return (TRUE);
2243 }
2244 }
2245 }
2246 return (FALSE);
2247}
2248/**
jcarseya405b862010-09-14 05:18:09 +00002249 Returns value from command line argument.
jcarsey94b17fa2009-05-07 18:46:18 +00002250
jcarseya405b862010-09-14 05:18:09 +00002251 Value parameters are in the form of "-<Key> value" or "/<Key> value".
jcarsey1e6e84c2010-01-25 20:05:08 +00002252
jcarseya405b862010-09-14 05:18:09 +00002253 If CheckPackage is NULL, then return NULL.
jcarsey94b17fa2009-05-07 18:46:18 +00002254
jcarseya405b862010-09-14 05:18:09 +00002255 @param[in] CheckPackage The package of parsed command line arguments.
2256 @param[in] KeyString The Key of the command line argument to check for.
jcarsey94b17fa2009-05-07 18:46:18 +00002257
jcarseya405b862010-09-14 05:18:09 +00002258 @retval NULL The flag is not on the command line.
2259 @retval !=NULL The pointer to unicode string of the value.
2260**/
jcarsey94b17fa2009-05-07 18:46:18 +00002261CONST CHAR16*
2262EFIAPI
2263ShellCommandLineGetValue (
2264 IN CONST LIST_ENTRY *CheckPackage,
2265 IN CHAR16 *KeyString
jcarseya405b862010-09-14 05:18:09 +00002266 )
2267{
jcarsey94b17fa2009-05-07 18:46:18 +00002268 LIST_ENTRY *Node;
jcarsey252d9452011-03-25 20:49:53 +00002269 CHAR16 *TempString;
jcarsey94b17fa2009-05-07 18:46:18 +00002270
2271 //
2272 // check for CheckPackage == NULL
2273 //
2274 if (CheckPackage == NULL) {
2275 return (NULL);
2276 }
2277
2278 //
2279 // enumerate through the list of parametrs
2280 //
jcarsey1e6e84c2010-01-25 20:05:08 +00002281 for ( Node = GetFirstNode(CheckPackage)
2282 ; !IsNull (CheckPackage, Node)
2283 ; Node = GetNextNode(CheckPackage, Node)
jcarsey252d9452011-03-25 20:49:53 +00002284 ){
jcarsey94b17fa2009-05-07 18:46:18 +00002285 //
jcarsey252d9452011-03-25 20:49:53 +00002286 // If the Name matches, return TRUE (and there may be NULL name)
jcarsey94b17fa2009-05-07 18:46:18 +00002287 //
2288 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {
jcarsey9eb53ac2009-07-08 17:26:58 +00002289 //
2290 // If Type is TypeStart then only compare the begining of the strings
2291 //
jcarsey252d9452011-03-25 20:49:53 +00002292 if (((SHELL_PARAM_PACKAGE*)Node)->Type == TypeStart) {
2293 if (StrnCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name, StrLen(KeyString)) == 0) {
2294 return (((SHELL_PARAM_PACKAGE*)Node)->Name + StrLen(KeyString));
2295 }
2296 TempString = NULL;
2297 TempString = StrnCatGrow(&TempString, NULL, KeyString, StrLen(((SHELL_PARAM_PACKAGE*)Node)->Name));
2298 if (TempString != NULL) {
2299 if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {
2300 FreePool(TempString);
2301 return (((SHELL_PARAM_PACKAGE*)Node)->Name + StrLen(KeyString));
2302 }
2303 FreePool(TempString);
2304 }
2305 } else if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {
jcarsey94b17fa2009-05-07 18:46:18 +00002306 return (((SHELL_PARAM_PACKAGE*)Node)->Value);
2307 }
2308 }
2309 }
2310 return (NULL);
2311}
jcarseya405b862010-09-14 05:18:09 +00002312
jcarsey94b17fa2009-05-07 18:46:18 +00002313/**
jcarseya405b862010-09-14 05:18:09 +00002314 Returns raw value from command line argument.
jcarsey94b17fa2009-05-07 18:46:18 +00002315
jcarseya405b862010-09-14 05:18:09 +00002316 Raw value parameters are in the form of "value" in a specific position in the list.
jcarsey1e6e84c2010-01-25 20:05:08 +00002317
jcarseya405b862010-09-14 05:18:09 +00002318 If CheckPackage is NULL, then return NULL.
jcarsey94b17fa2009-05-07 18:46:18 +00002319
jcarseya405b862010-09-14 05:18:09 +00002320 @param[in] CheckPackage The package of parsed command line arguments.
2321 @param[in] Position The position of the value.
jcarsey94b17fa2009-05-07 18:46:18 +00002322
jcarseya405b862010-09-14 05:18:09 +00002323 @retval NULL The flag is not on the command line.
2324 @retval !=NULL The pointer to unicode string of the value.
jcarsey94b17fa2009-05-07 18:46:18 +00002325 **/
2326CONST CHAR16*
2327EFIAPI
2328ShellCommandLineGetRawValue (
jcarseya405b862010-09-14 05:18:09 +00002329 IN CONST LIST_ENTRY * CONST CheckPackage,
2330 IN UINTN Position
2331 )
2332{
jcarsey94b17fa2009-05-07 18:46:18 +00002333 LIST_ENTRY *Node;
2334
2335 //
2336 // check for CheckPackage == NULL
2337 //
2338 if (CheckPackage == NULL) {
2339 return (NULL);
2340 }
2341
2342 //
2343 // enumerate through the list of parametrs
2344 //
jcarsey1e6e84c2010-01-25 20:05:08 +00002345 for ( Node = GetFirstNode(CheckPackage)
2346 ; !IsNull (CheckPackage, Node)
2347 ; Node = GetNextNode(CheckPackage, Node)
jcarseya405b862010-09-14 05:18:09 +00002348 ){
jcarsey94b17fa2009-05-07 18:46:18 +00002349 //
2350 // If the position matches, return the value
2351 //
2352 if (((SHELL_PARAM_PACKAGE*)Node)->OriginalPosition == Position) {
2353 return (((SHELL_PARAM_PACKAGE*)Node)->Value);
2354 }
2355 }
2356 return (NULL);
jcarseyb1f95a02009-06-16 00:23:19 +00002357}
jcarsey2247dde2009-11-09 18:08:58 +00002358
2359/**
jcarsey1e6e84c2010-01-25 20:05:08 +00002360 returns the number of command line value parameters that were parsed.
2361
jcarsey2247dde2009-11-09 18:08:58 +00002362 this will not include flags.
2363
jcarseya405b862010-09-14 05:18:09 +00002364 @param[in] CheckPackage The package of parsed command line arguments.
2365
jcarsey2247dde2009-11-09 18:08:58 +00002366 @retval (UINTN)-1 No parsing has ocurred
2367 @return other The number of value parameters found
2368**/
2369UINTN
2370EFIAPI
2371ShellCommandLineGetCount(
jcarseya405b862010-09-14 05:18:09 +00002372 IN CONST LIST_ENTRY *CheckPackage
jcarsey125c2cf2009-11-18 21:36:50 +00002373 )
2374{
jcarseya405b862010-09-14 05:18:09 +00002375 LIST_ENTRY *Node1;
2376 UINTN Count;
2377
2378 if (CheckPackage == NULL) {
2379 return (0);
2380 }
2381 for ( Node1 = GetFirstNode(CheckPackage), Count = 0
2382 ; !IsNull (CheckPackage, Node1)
2383 ; Node1 = GetNextNode(CheckPackage, Node1)
2384 ){
2385 if (((SHELL_PARAM_PACKAGE*)Node1)->Name == NULL) {
2386 Count++;
2387 }
2388 }
2389 return (Count);
jcarsey2247dde2009-11-09 18:08:58 +00002390}
2391
jcarsey975136a2009-06-16 19:03:54 +00002392/**
jcarsey36a9d672009-11-20 21:13:41 +00002393 Determins if a parameter is duplicated.
2394
jcarsey1e6e84c2010-01-25 20:05:08 +00002395 If Param is not NULL then it will point to a callee allocated string buffer
jcarsey36a9d672009-11-20 21:13:41 +00002396 with the parameter value if a duplicate is found.
2397
2398 If CheckPackage is NULL, then ASSERT.
2399
2400 @param[in] CheckPackage The package of parsed command line arguments.
2401 @param[out] Param Upon finding one, a pointer to the duplicated parameter.
2402
2403 @retval EFI_SUCCESS No parameters were duplicated.
2404 @retval EFI_DEVICE_ERROR A duplicate was found.
2405 **/
2406EFI_STATUS
2407EFIAPI
2408ShellCommandLineCheckDuplicate (
2409 IN CONST LIST_ENTRY *CheckPackage,
2410 OUT CHAR16 **Param
2411 )
2412{
2413 LIST_ENTRY *Node1;
2414 LIST_ENTRY *Node2;
jcarsey1e6e84c2010-01-25 20:05:08 +00002415
jcarsey36a9d672009-11-20 21:13:41 +00002416 ASSERT(CheckPackage != NULL);
2417
jcarsey1e6e84c2010-01-25 20:05:08 +00002418 for ( Node1 = GetFirstNode(CheckPackage)
2419 ; !IsNull (CheckPackage, Node1)
2420 ; Node1 = GetNextNode(CheckPackage, Node1)
jcarseya405b862010-09-14 05:18:09 +00002421 ){
jcarsey1e6e84c2010-01-25 20:05:08 +00002422 for ( Node2 = GetNextNode(CheckPackage, Node1)
2423 ; !IsNull (CheckPackage, Node2)
2424 ; Node2 = GetNextNode(CheckPackage, Node2)
jcarseya405b862010-09-14 05:18:09 +00002425 ){
2426 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 +00002427 if (Param != NULL) {
2428 *Param = NULL;
2429 *Param = StrnCatGrow(Param, NULL, ((SHELL_PARAM_PACKAGE*)Node1)->Name, 0);
2430 }
2431 return (EFI_DEVICE_ERROR);
2432 }
2433 }
2434 }
2435 return (EFI_SUCCESS);
2436}
2437
2438/**
jcarsey1e6e84c2010-01-25 20:05:08 +00002439 This is a find and replace function. Upon successful return the NewString is a copy of
jcarsey975136a2009-06-16 19:03:54 +00002440 SourceString with each instance of FindTarget replaced with ReplaceWith.
2441
jcarseyb3011f42010-01-11 21:49:04 +00002442 If SourceString and NewString overlap the behavior is undefined.
2443
jcarsey975136a2009-06-16 19:03:54 +00002444 If the string would grow bigger than NewSize it will halt and return error.
2445
jcarseya405b862010-09-14 05:18:09 +00002446 @param[in] SourceString The string with source buffer.
2447 @param[in,out] NewString The string with resultant buffer.
2448 @param[in] NewSize The size in bytes of NewString.
2449 @param[in] FindTarget The string to look for.
2450 @param[in] ReplaceWith The string to replace FindTarget with.
jcarsey969c7832010-01-13 16:46:33 +00002451 @param[in] SkipPreCarrot If TRUE will skip a FindTarget that has a '^'
2452 immediately before it.
jcarseya405b862010-09-14 05:18:09 +00002453 @param[in] ParameterReplacing If TRUE will add "" around items with spaces.
jcarsey975136a2009-06-16 19:03:54 +00002454
jcarsey969c7832010-01-13 16:46:33 +00002455 @retval EFI_INVALID_PARAMETER SourceString was NULL.
2456 @retval EFI_INVALID_PARAMETER NewString was NULL.
2457 @retval EFI_INVALID_PARAMETER FindTarget was NULL.
2458 @retval EFI_INVALID_PARAMETER ReplaceWith was NULL.
2459 @retval EFI_INVALID_PARAMETER FindTarget had length < 1.
2460 @retval EFI_INVALID_PARAMETER SourceString had length < 1.
jcarsey1e6e84c2010-01-25 20:05:08 +00002461 @retval EFI_BUFFER_TOO_SMALL NewSize was less than the minimum size to hold
jcarsey969c7832010-01-13 16:46:33 +00002462 the new string (truncation occurred).
jcarseya405b862010-09-14 05:18:09 +00002463 @retval EFI_SUCCESS The string was successfully copied with replacement.
jcarsey975136a2009-06-16 19:03:54 +00002464**/
jcarsey975136a2009-06-16 19:03:54 +00002465EFI_STATUS
2466EFIAPI
jcarseya405b862010-09-14 05:18:09 +00002467ShellCopySearchAndReplace(
jcarsey975136a2009-06-16 19:03:54 +00002468 IN CHAR16 CONST *SourceString,
jcarseya405b862010-09-14 05:18:09 +00002469 IN OUT CHAR16 *NewString,
jcarsey975136a2009-06-16 19:03:54 +00002470 IN UINTN NewSize,
2471 IN CONST CHAR16 *FindTarget,
jcarsey969c7832010-01-13 16:46:33 +00002472 IN CONST CHAR16 *ReplaceWith,
jcarseya405b862010-09-14 05:18:09 +00002473 IN CONST BOOLEAN SkipPreCarrot,
2474 IN CONST BOOLEAN ParameterReplacing
jcarsey1e6e84c2010-01-25 20:05:08 +00002475 )
jcarsey2247dde2009-11-09 18:08:58 +00002476{
jcarsey01582942009-07-10 19:46:17 +00002477 UINTN Size;
jcarseya405b862010-09-14 05:18:09 +00002478 CHAR16 *Replace;
2479
jcarsey975136a2009-06-16 19:03:54 +00002480 if ( (SourceString == NULL)
2481 || (NewString == NULL)
2482 || (FindTarget == NULL)
2483 || (ReplaceWith == NULL)
2484 || (StrLen(FindTarget) < 1)
2485 || (StrLen(SourceString) < 1)
jcarseya405b862010-09-14 05:18:09 +00002486 ){
jcarsey975136a2009-06-16 19:03:54 +00002487 return (EFI_INVALID_PARAMETER);
2488 }
jcarseya405b862010-09-14 05:18:09 +00002489 Replace = NULL;
2490 if (StrStr(ReplaceWith, L" ") == NULL || !ParameterReplacing) {
2491 Replace = StrnCatGrow(&Replace, NULL, ReplaceWith, 0);
2492 } else {
2493 Replace = AllocateZeroPool(StrSize(ReplaceWith) + 2*sizeof(CHAR16));
2494 UnicodeSPrint(Replace, StrSize(ReplaceWith) + 2*sizeof(CHAR16), L"\"%s\"", ReplaceWith);
2495 }
jcarsey3e082d52010-10-04 16:44:57 +00002496 if (Replace == NULL) {
2497 return (EFI_OUT_OF_RESOURCES);
2498 }
jcarsey2247dde2009-11-09 18:08:58 +00002499 NewString = SetMem16(NewString, NewSize, CHAR_NULL);
2500 while (*SourceString != CHAR_NULL) {
jcarsey969c7832010-01-13 16:46:33 +00002501 //
jcarseya405b862010-09-14 05:18:09 +00002502 // if we find the FindTarget and either Skip == FALSE or Skip and we
jcarsey969c7832010-01-13 16:46:33 +00002503 // dont have a carrot do a replace...
2504 //
jcarsey1e6e84c2010-01-25 20:05:08 +00002505 if (StrnCmp(SourceString, FindTarget, StrLen(FindTarget)) == 0
jcarseya405b862010-09-14 05:18:09 +00002506 && ((SkipPreCarrot && *(SourceString-1) != L'^') || !SkipPreCarrot)
2507 ){
jcarsey975136a2009-06-16 19:03:54 +00002508 SourceString += StrLen(FindTarget);
jcarsey01582942009-07-10 19:46:17 +00002509 Size = StrSize(NewString);
jcarseya405b862010-09-14 05:18:09 +00002510 if ((Size + (StrLen(Replace)*sizeof(CHAR16))) > NewSize) {
2511 FreePool(Replace);
jcarsey975136a2009-06-16 19:03:54 +00002512 return (EFI_BUFFER_TOO_SMALL);
2513 }
jcarseya405b862010-09-14 05:18:09 +00002514 StrCat(NewString, Replace);
jcarsey975136a2009-06-16 19:03:54 +00002515 } else {
jcarsey01582942009-07-10 19:46:17 +00002516 Size = StrSize(NewString);
2517 if (Size + sizeof(CHAR16) > NewSize) {
jcarseya405b862010-09-14 05:18:09 +00002518 FreePool(Replace);
jcarsey975136a2009-06-16 19:03:54 +00002519 return (EFI_BUFFER_TOO_SMALL);
2520 }
2521 StrnCat(NewString, SourceString, 1);
2522 SourceString++;
2523 }
2524 }
jcarseya405b862010-09-14 05:18:09 +00002525 FreePool(Replace);
jcarsey975136a2009-06-16 19:03:54 +00002526 return (EFI_SUCCESS);
2527}
jcarseyb1f95a02009-06-16 00:23:19 +00002528
2529/**
jcarseye2f82972009-12-01 05:40:24 +00002530 Internal worker function to output a string.
2531
2532 This function will output a string to the correct StdOut.
2533
2534 @param[in] String The string to print out.
2535
2536 @retval EFI_SUCCESS The operation was sucessful.
2537 @retval !EFI_SUCCESS The operation failed.
2538**/
2539EFI_STATUS
2540EFIAPI
2541InternalPrintTo (
2542 IN CONST CHAR16 *String
2543 )
2544{
2545 UINTN Size;
2546 Size = StrSize(String) - sizeof(CHAR16);
jcarseya405b862010-09-14 05:18:09 +00002547 if (Size == 0) {
2548 return (EFI_SUCCESS);
2549 }
jcarseye2f82972009-12-01 05:40:24 +00002550 if (mEfiShellParametersProtocol != NULL) {
jcarseya405b862010-09-14 05:18:09 +00002551 return (mEfiShellProtocol->WriteFile(mEfiShellParametersProtocol->StdOut, &Size, (VOID*)String));
jcarseye2f82972009-12-01 05:40:24 +00002552 }
2553 if (mEfiShellInterface != NULL) {
jcarseyecd3d592009-12-07 18:05:00 +00002554 //
2555 // Divide in half for old shell. Must be string length not size.
2556 //
2557 Size /= 2;
jcarseya405b862010-09-14 05:18:09 +00002558 return (mEfiShellInterface->StdOut->Write(mEfiShellInterface->StdOut, &Size, (VOID*)String));
jcarseye2f82972009-12-01 05:40:24 +00002559 }
2560 ASSERT(FALSE);
2561 return (EFI_UNSUPPORTED);
2562}
2563
2564/**
jcarseyb1f95a02009-06-16 00:23:19 +00002565 Print at a specific location on the screen.
2566
jcarseyf1b87e72009-06-17 00:52:11 +00002567 This function will move the cursor to a given screen location and print the specified string
jcarsey1e6e84c2010-01-25 20:05:08 +00002568
2569 If -1 is specified for either the Row or Col the current screen location for BOTH
jcarseyf1b87e72009-06-17 00:52:11 +00002570 will be used.
jcarseyb1f95a02009-06-16 00:23:19 +00002571
2572 if either Row or Col is out of range for the current console, then ASSERT
2573 if Format is NULL, then ASSERT
2574
jcarsey1e6e84c2010-01-25 20:05:08 +00002575 In addition to the standard %-based flags as supported by UefiLib Print() this supports
jcarseyb1f95a02009-06-16 00:23:19 +00002576 the following additional flags:
2577 %N - Set output attribute to normal
2578 %H - Set output attribute to highlight
2579 %E - Set output attribute to error
2580 %B - Set output attribute to blue color
2581 %V - Set output attribute to green color
2582
2583 Note: The background color is controlled by the shell command cls.
2584
jcarseyb1f95a02009-06-16 00:23:19 +00002585 @param[in] Col the column to print at
jcarsey252d9452011-03-25 20:49:53 +00002586 @param[in] Row the row to print at
jcarseyb1f95a02009-06-16 00:23:19 +00002587 @param[in] Format the format string
jcarsey2247dde2009-11-09 18:08:58 +00002588 @param[in] Marker the marker for the variable argument list
jcarseyb1f95a02009-06-16 00:23:19 +00002589
jcarseya405b862010-09-14 05:18:09 +00002590 @return EFI_SUCCESS The operation was successful.
2591 @return EFI_DEVICE_ERROR The console device reported an error.
jcarseyb1f95a02009-06-16 00:23:19 +00002592**/
jcarseya405b862010-09-14 05:18:09 +00002593EFI_STATUS
jcarseyb1f95a02009-06-16 00:23:19 +00002594EFIAPI
jcarsey2247dde2009-11-09 18:08:58 +00002595InternalShellPrintWorker(
jcarseyb1f95a02009-06-16 00:23:19 +00002596 IN INT32 Col OPTIONAL,
2597 IN INT32 Row OPTIONAL,
2598 IN CONST CHAR16 *Format,
jcarsey252d9452011-03-25 20:49:53 +00002599 IN VA_LIST Marker
jcarsey1e6e84c2010-01-25 20:05:08 +00002600 )
jcarsey2247dde2009-11-09 18:08:58 +00002601{
jcarseyb1f95a02009-06-16 00:23:19 +00002602 EFI_STATUS Status;
jcarsey975136a2009-06-16 19:03:54 +00002603 CHAR16 *ResumeLocation;
2604 CHAR16 *FormatWalker;
jcarseya405b862010-09-14 05:18:09 +00002605 UINTN OriginalAttribute;
jcarsey89e85372011-04-13 23:37:50 +00002606 CHAR16 *mPostReplaceFormat;
2607 CHAR16 *mPostReplaceFormat2;
2608
2609 mPostReplaceFormat = AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize));
2610 mPostReplaceFormat2 = AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize));
jcarseya405b862010-09-14 05:18:09 +00002611
jcarseyf8d3e682011-04-19 17:54:42 +00002612 if (mPostReplaceFormat == NULL || mPostReplaceFormat2 == NULL) {
2613 SHELL_FREE_NON_NULL(mPostReplaceFormat);
2614 SHELL_FREE_NON_NULL(mPostReplaceFormat2);
2615 return (EFI_OUT_OF_RESOURCES);
2616 }
2617
jcarseya405b862010-09-14 05:18:09 +00002618 Status = EFI_SUCCESS;
2619 OriginalAttribute = gST->ConOut->Mode->Attribute;
jcarsey1e6e84c2010-01-25 20:05:08 +00002620
jcarsey975136a2009-06-16 19:03:54 +00002621 //
2622 // Back and forth each time fixing up 1 of our flags...
2623 //
jcarseya405b862010-09-14 05:18:09 +00002624 Status = ShellCopySearchAndReplace(Format, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%N", L"%%N", FALSE, FALSE);
jcarsey975136a2009-06-16 19:03:54 +00002625 ASSERT_EFI_ERROR(Status);
jcarseya405b862010-09-14 05:18:09 +00002626 Status = ShellCopySearchAndReplace(mPostReplaceFormat, mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), L"%E", L"%%E", FALSE, FALSE);
jcarsey975136a2009-06-16 19:03:54 +00002627 ASSERT_EFI_ERROR(Status);
jcarseya405b862010-09-14 05:18:09 +00002628 Status = ShellCopySearchAndReplace(mPostReplaceFormat2, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%H", L"%%H", FALSE, FALSE);
jcarsey975136a2009-06-16 19:03:54 +00002629 ASSERT_EFI_ERROR(Status);
jcarseya405b862010-09-14 05:18:09 +00002630 Status = ShellCopySearchAndReplace(mPostReplaceFormat, mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), L"%B", L"%%B", FALSE, FALSE);
jcarsey975136a2009-06-16 19:03:54 +00002631 ASSERT_EFI_ERROR(Status);
jcarseya405b862010-09-14 05:18:09 +00002632 Status = ShellCopySearchAndReplace(mPostReplaceFormat2, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%V", L"%%V", FALSE, FALSE);
jcarsey975136a2009-06-16 19:03:54 +00002633 ASSERT_EFI_ERROR(Status);
2634
2635 //
2636 // Use the last buffer from replacing to print from...
2637 //
jcarseya405b862010-09-14 05:18:09 +00002638 UnicodeVSPrint (mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), mPostReplaceFormat, Marker);
jcarseyb1f95a02009-06-16 00:23:19 +00002639
2640 if (Col != -1 && Row != -1) {
jcarseyb1f95a02009-06-16 00:23:19 +00002641 Status = gST->ConOut->SetCursorPosition(gST->ConOut, Col, Row);
jcarsey975136a2009-06-16 19:03:54 +00002642 }
2643
jcarseyecd3d592009-12-07 18:05:00 +00002644 FormatWalker = mPostReplaceFormat2;
jcarsey2247dde2009-11-09 18:08:58 +00002645 while (*FormatWalker != CHAR_NULL) {
jcarsey975136a2009-06-16 19:03:54 +00002646 //
2647 // Find the next attribute change request
2648 //
2649 ResumeLocation = StrStr(FormatWalker, L"%");
2650 if (ResumeLocation != NULL) {
jcarsey2247dde2009-11-09 18:08:58 +00002651 *ResumeLocation = CHAR_NULL;
jcarsey975136a2009-06-16 19:03:54 +00002652 }
2653 //
2654 // print the current FormatWalker string
2655 //
jcarseya405b862010-09-14 05:18:09 +00002656 if (StrLen(FormatWalker)>0) {
2657 Status = InternalPrintTo(FormatWalker);
2658 if (EFI_ERROR(Status)) {
2659 break;
2660 }
2661 }
2662
jcarsey975136a2009-06-16 19:03:54 +00002663 //
2664 // update the attribute
2665 //
2666 if (ResumeLocation != NULL) {
2667 switch (*(ResumeLocation+1)) {
2668 case (L'N'):
jcarseya405b862010-09-14 05:18:09 +00002669 gST->ConOut->SetAttribute(gST->ConOut, OriginalAttribute);
jcarsey975136a2009-06-16 19:03:54 +00002670 break;
2671 case (L'E'):
jcarseya405b862010-09-14 05:18:09 +00002672 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_YELLOW, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));
jcarsey975136a2009-06-16 19:03:54 +00002673 break;
2674 case (L'H'):
jcarseya405b862010-09-14 05:18:09 +00002675 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_WHITE, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));
jcarsey975136a2009-06-16 19:03:54 +00002676 break;
2677 case (L'B'):
jcarseya405b862010-09-14 05:18:09 +00002678 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_BLUE, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));
jcarsey975136a2009-06-16 19:03:54 +00002679 break;
2680 case (L'V'):
jcarseya405b862010-09-14 05:18:09 +00002681 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_GREEN, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));
jcarsey975136a2009-06-16 19:03:54 +00002682 break;
2683 default:
jcarseye2f82972009-12-01 05:40:24 +00002684 //
2685 // Print a simple '%' symbol
2686 //
2687 Status = InternalPrintTo(L"%");
jcarseya405b862010-09-14 05:18:09 +00002688 if (EFI_ERROR(Status)) {
2689 break;
2690 }
jcarseye2f82972009-12-01 05:40:24 +00002691 ResumeLocation = ResumeLocation - 1;
jcarsey975136a2009-06-16 19:03:54 +00002692 break;
2693 }
2694 } else {
2695 //
2696 // reset to normal now...
2697 //
jcarsey975136a2009-06-16 19:03:54 +00002698 break;
2699 }
2700
2701 //
2702 // update FormatWalker to Resume + 2 (skip the % and the indicator)
2703 //
2704 FormatWalker = ResumeLocation + 2;
2705 }
jcarseyb1f95a02009-06-16 00:23:19 +00002706
jcarseya405b862010-09-14 05:18:09 +00002707 gST->ConOut->SetAttribute(gST->ConOut, OriginalAttribute);
jcarsey89e85372011-04-13 23:37:50 +00002708
2709 SHELL_FREE_NON_NULL(mPostReplaceFormat);
2710 SHELL_FREE_NON_NULL(mPostReplaceFormat2);
jcarseya405b862010-09-14 05:18:09 +00002711 return (Status);
jcarsey5f7431d2009-07-10 18:06:01 +00002712}
jcarsey2247dde2009-11-09 18:08:58 +00002713
2714/**
2715 Print at a specific location on the screen.
2716
jcarseye2f82972009-12-01 05:40:24 +00002717 This function will move the cursor to a given screen location and print the specified string.
jcarsey1e6e84c2010-01-25 20:05:08 +00002718
2719 If -1 is specified for either the Row or Col the current screen location for BOTH
jcarsey2247dde2009-11-09 18:08:58 +00002720 will be used.
2721
jcarseye2f82972009-12-01 05:40:24 +00002722 If either Row or Col is out of range for the current console, then ASSERT.
2723 If Format is NULL, then ASSERT.
jcarsey2247dde2009-11-09 18:08:58 +00002724
jcarsey1e6e84c2010-01-25 20:05:08 +00002725 In addition to the standard %-based flags as supported by UefiLib Print() this supports
jcarsey2247dde2009-11-09 18:08:58 +00002726 the following additional flags:
2727 %N - Set output attribute to normal
2728 %H - Set output attribute to highlight
2729 %E - Set output attribute to error
2730 %B - Set output attribute to blue color
2731 %V - Set output attribute to green color
2732
2733 Note: The background color is controlled by the shell command cls.
2734
jcarsey2247dde2009-11-09 18:08:58 +00002735 @param[in] Col the column to print at
jcarseya405b862010-09-14 05:18:09 +00002736 @param[in] Row the row to print at
jcarsey2247dde2009-11-09 18:08:58 +00002737 @param[in] Format the format string
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
2745ShellPrintEx(
2746 IN INT32 Col OPTIONAL,
2747 IN INT32 Row OPTIONAL,
2748 IN CONST CHAR16 *Format,
2749 ...
jcarsey1e6e84c2010-01-25 20:05:08 +00002750 )
jcarsey2247dde2009-11-09 18:08:58 +00002751{
2752 VA_LIST Marker;
jcarseya405b862010-09-14 05:18:09 +00002753 EFI_STATUS RetVal;
jcarsey3e082d52010-10-04 16:44:57 +00002754 if (Format == NULL) {
2755 return (EFI_INVALID_PARAMETER);
2756 }
jcarsey2247dde2009-11-09 18:08:58 +00002757 VA_START (Marker, Format);
jcarseya405b862010-09-14 05:18:09 +00002758 RetVal = InternalShellPrintWorker(Col, Row, Format, Marker);
jcarseye2f82972009-12-01 05:40:24 +00002759 VA_END(Marker);
jcarseya405b862010-09-14 05:18:09 +00002760 return(RetVal);
jcarsey2247dde2009-11-09 18:08:58 +00002761}
2762
2763/**
2764 Print at a specific location on the screen.
2765
jcarseye2f82972009-12-01 05:40:24 +00002766 This function will move the cursor to a given screen location and print the specified string.
jcarsey1e6e84c2010-01-25 20:05:08 +00002767
2768 If -1 is specified for either the Row or Col the current screen location for BOTH
jcarseye2f82972009-12-01 05:40:24 +00002769 will be used.
jcarsey2247dde2009-11-09 18:08:58 +00002770
jcarseye2f82972009-12-01 05:40:24 +00002771 If either Row or Col is out of range for the current console, then ASSERT.
2772 If Format is NULL, then ASSERT.
jcarsey2247dde2009-11-09 18:08:58 +00002773
jcarsey1e6e84c2010-01-25 20:05:08 +00002774 In addition to the standard %-based flags as supported by UefiLib Print() this supports
jcarsey2247dde2009-11-09 18:08:58 +00002775 the following additional flags:
jcarsey1e6e84c2010-01-25 20:05:08 +00002776 %N - Set output attribute to normal.
2777 %H - Set output attribute to highlight.
2778 %E - Set output attribute to error.
2779 %B - Set output attribute to blue color.
2780 %V - Set output attribute to green color.
jcarsey2247dde2009-11-09 18:08:58 +00002781
2782 Note: The background color is controlled by the shell command cls.
2783
jcarsey1e6e84c2010-01-25 20:05:08 +00002784 @param[in] Col The column to print at.
jcarseya405b862010-09-14 05:18:09 +00002785 @param[in] Row The row to print at.
jcarsey1e6e84c2010-01-25 20:05:08 +00002786 @param[in] Language The language of the string to retrieve. If this parameter
2787 is NULL, then the current platform language is used.
2788 @param[in] HiiFormatStringId The format string Id for getting from Hii.
2789 @param[in] HiiFormatHandle The format string Handle for getting from Hii.
jcarseya405b862010-09-14 05:18:09 +00002790 @param[in] ... The variable argument list.
jcarsey2247dde2009-11-09 18:08:58 +00002791
jcarseya405b862010-09-14 05:18:09 +00002792 @return EFI_SUCCESS The printing was successful.
2793 @return EFI_DEVICE_ERROR The console device reported an error.
jcarsey2247dde2009-11-09 18:08:58 +00002794**/
jcarseya405b862010-09-14 05:18:09 +00002795EFI_STATUS
jcarsey2247dde2009-11-09 18:08:58 +00002796EFIAPI
2797ShellPrintHiiEx(
2798 IN INT32 Col OPTIONAL,
2799 IN INT32 Row OPTIONAL,
jcarsey1e6e84c2010-01-25 20:05:08 +00002800 IN CONST CHAR8 *Language OPTIONAL,
jcarsey2247dde2009-11-09 18:08:58 +00002801 IN CONST EFI_STRING_ID HiiFormatStringId,
2802 IN CONST EFI_HANDLE HiiFormatHandle,
2803 ...
2804 )
2805{
2806 VA_LIST Marker;
2807 CHAR16 *HiiFormatString;
jcarseya405b862010-09-14 05:18:09 +00002808 EFI_STATUS RetVal;
jcarsey2247dde2009-11-09 18:08:58 +00002809
2810 VA_START (Marker, HiiFormatHandle);
jcarsey1e6e84c2010-01-25 20:05:08 +00002811 HiiFormatString = HiiGetString(HiiFormatHandle, HiiFormatStringId, Language);
jcarsey2247dde2009-11-09 18:08:58 +00002812 ASSERT(HiiFormatString != NULL);
2813
2814 RetVal = InternalShellPrintWorker(Col, Row, HiiFormatString, Marker);
2815
jcarseya405b862010-09-14 05:18:09 +00002816 SHELL_FREE_NON_NULL(HiiFormatString);
jcarseye2f82972009-12-01 05:40:24 +00002817 VA_END(Marker);
jcarsey2247dde2009-11-09 18:08:58 +00002818
2819 return (RetVal);
2820}
2821
2822/**
2823 Function to determine if a given filename represents a file or a directory.
2824
2825 @param[in] DirName Path to directory to test.
2826
2827 @retval EFI_SUCCESS The Path represents a directory
2828 @retval EFI_NOT_FOUND The Path does not represent a directory
2829 @return other The path failed to open
2830**/
2831EFI_STATUS
2832EFIAPI
2833ShellIsDirectory(
2834 IN CONST CHAR16 *DirName
2835 )
2836{
2837 EFI_STATUS Status;
jcarseya405b862010-09-14 05:18:09 +00002838 SHELL_FILE_HANDLE Handle;
jcarsey3e082d52010-10-04 16:44:57 +00002839 CHAR16 *TempLocation;
2840 CHAR16 *TempLocation2;
jcarsey2247dde2009-11-09 18:08:58 +00002841
jcarseyecd3d592009-12-07 18:05:00 +00002842 ASSERT(DirName != NULL);
2843
jcarseya405b862010-09-14 05:18:09 +00002844 Handle = NULL;
2845 TempLocation = NULL;
jcarsey2247dde2009-11-09 18:08:58 +00002846
2847 Status = ShellOpenFileByName(DirName, &Handle, EFI_FILE_MODE_READ, 0);
2848 if (EFI_ERROR(Status)) {
jcarseya405b862010-09-14 05:18:09 +00002849 //
2850 // try good logic first.
2851 //
2852 if (mEfiShellProtocol != NULL) {
jcarsey3e082d52010-10-04 16:44:57 +00002853 TempLocation = StrnCatGrow(&TempLocation, NULL, DirName, 0);
2854 TempLocation2 = StrStr(TempLocation, L":");
2855 if (TempLocation2 != NULL && StrLen(StrStr(TempLocation, L":")) == 2) {
2856 *(TempLocation2+1) = CHAR_NULL;
jcarseya405b862010-09-14 05:18:09 +00002857 }
2858 if (mEfiShellProtocol->GetDevicePathFromMap(TempLocation) != NULL) {
2859 FreePool(TempLocation);
2860 return (EFI_SUCCESS);
2861 }
2862 FreePool(TempLocation);
2863 } else {
2864 //
2865 // probably a map name?!?!!?
2866 //
2867 TempLocation = StrStr(DirName, L"\\");
2868 if (TempLocation != NULL && *(TempLocation+1) == CHAR_NULL) {
2869 return (EFI_SUCCESS);
2870 }
2871 }
jcarsey2247dde2009-11-09 18:08:58 +00002872 return (Status);
2873 }
2874
2875 if (FileHandleIsDirectory(Handle) == EFI_SUCCESS) {
2876 ShellCloseFile(&Handle);
2877 return (EFI_SUCCESS);
2878 }
2879 ShellCloseFile(&Handle);
2880 return (EFI_NOT_FOUND);
2881}
2882
jcarsey125c2cf2009-11-18 21:36:50 +00002883/**
jcarsey36a9d672009-11-20 21:13:41 +00002884 Function to determine if a given filename represents a file.
2885
2886 @param[in] Name Path to file to test.
2887
2888 @retval EFI_SUCCESS The Path represents a file.
2889 @retval EFI_NOT_FOUND The Path does not represent a file.
2890 @retval other The path failed to open.
2891**/
2892EFI_STATUS
2893EFIAPI
2894ShellIsFile(
2895 IN CONST CHAR16 *Name
2896 )
2897{
2898 EFI_STATUS Status;
jcarseya405b862010-09-14 05:18:09 +00002899 SHELL_FILE_HANDLE Handle;
jcarsey36a9d672009-11-20 21:13:41 +00002900
jcarseyecd3d592009-12-07 18:05:00 +00002901 ASSERT(Name != NULL);
2902
jcarsey36a9d672009-11-20 21:13:41 +00002903 Handle = NULL;
2904
2905 Status = ShellOpenFileByName(Name, &Handle, EFI_FILE_MODE_READ, 0);
2906 if (EFI_ERROR(Status)) {
2907 return (Status);
2908 }
2909
2910 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {
2911 ShellCloseFile(&Handle);
2912 return (EFI_SUCCESS);
2913 }
2914 ShellCloseFile(&Handle);
2915 return (EFI_NOT_FOUND);
2916}
2917
2918/**
jcarseyb3011f42010-01-11 21:49:04 +00002919 Function to determine if a given filename represents a file.
2920
2921 This will search the CWD and then the Path.
2922
2923 If Name is NULL, then ASSERT.
2924
2925 @param[in] Name Path to file to test.
2926
2927 @retval EFI_SUCCESS The Path represents a file.
2928 @retval EFI_NOT_FOUND The Path does not represent a file.
2929 @retval other The path failed to open.
2930**/
2931EFI_STATUS
2932EFIAPI
2933ShellIsFileInPath(
2934 IN CONST CHAR16 *Name
jcarseya405b862010-09-14 05:18:09 +00002935 )
2936{
jcarseyb3011f42010-01-11 21:49:04 +00002937 CHAR16 *NewName;
2938 EFI_STATUS Status;
2939
2940 if (!EFI_ERROR(ShellIsFile(Name))) {
jcarseya405b862010-09-14 05:18:09 +00002941 return (EFI_SUCCESS);
jcarseyb3011f42010-01-11 21:49:04 +00002942 }
2943
2944 NewName = ShellFindFilePath(Name);
2945 if (NewName == NULL) {
2946 return (EFI_NOT_FOUND);
2947 }
2948 Status = ShellIsFile(NewName);
2949 FreePool(NewName);
2950 return (Status);
2951}
jcarsey252d9452011-03-25 20:49:53 +00002952
jcarseyb3011f42010-01-11 21:49:04 +00002953/**
jcarsey1e6e84c2010-01-25 20:05:08 +00002954 Function to determine whether a string is decimal or hex representation of a number
jcarsey125c2cf2009-11-18 21:36:50 +00002955 and return the number converted from the string.
2956
2957 @param[in] String String representation of a number
2958
jcarsey252d9452011-03-25 20:49:53 +00002959 @return the number
2960 @retval (UINTN)(-1) An error ocurred.
jcarsey125c2cf2009-11-18 21:36:50 +00002961**/
2962UINTN
2963EFIAPI
2964ShellStrToUintn(
2965 IN CONST CHAR16 *String
2966 )
2967{
jcarsey252d9452011-03-25 20:49:53 +00002968 UINT64 RetVal;
2969 BOOLEAN Hex;
2970
2971 Hex = FALSE;
2972
2973 if (!InternalShellIsHexOrDecimalNumber(String, Hex, TRUE)) {
2974 Hex = TRUE;
jcarsey125c2cf2009-11-18 21:36:50 +00002975 }
jcarsey252d9452011-03-25 20:49:53 +00002976
2977 if (!EFI_ERROR(ShellConvertStringToUint64(String, &RetVal, Hex, TRUE))) {
2978 return ((UINTN)RetVal);
2979 }
2980 return ((UINTN)(-1));
jcarsey125c2cf2009-11-18 21:36:50 +00002981}
2982
2983/**
2984 Safely append with automatic string resizing given length of Destination and
2985 desired length of copy from Source.
2986
2987 append the first D characters of Source to the end of Destination, where D is
2988 the lesser of Count and the StrLen() of Source. If appending those D characters
2989 will fit within Destination (whose Size is given as CurrentSize) and
jcarsey1e6e84c2010-01-25 20:05:08 +00002990 still leave room for a NULL terminator, then those characters are appended,
2991 starting at the original terminating NULL of Destination, and a new terminating
2992 NULL is appended.
jcarsey125c2cf2009-11-18 21:36:50 +00002993
2994 If appending D characters onto Destination will result in a overflow of the size
2995 given in CurrentSize the string will be grown such that the copy can be performed
2996 and CurrentSize will be updated to the new size.
2997
2998 If Source is NULL, there is nothing to append, just return the current buffer in
2999 Destination.
3000
3001 if Destination is NULL, then ASSERT()
3002 if Destination's current length (including NULL terminator) is already more then
3003 CurrentSize, then ASSERT()
3004
3005 @param[in,out] Destination The String to append onto
3006 @param[in,out] CurrentSize on call the number of bytes in Destination. On
3007 return possibly the new size (still in bytes). if NULL
3008 then allocate whatever is needed.
3009 @param[in] Source The String to append from
3010 @param[in] Count Maximum number of characters to append. if 0 then
3011 all are appended.
3012
3013 @return Destination return the resultant string.
3014**/
3015CHAR16*
3016EFIAPI
3017StrnCatGrow (
3018 IN OUT CHAR16 **Destination,
3019 IN OUT UINTN *CurrentSize,
3020 IN CONST CHAR16 *Source,
3021 IN UINTN Count
3022 )
3023{
3024 UINTN DestinationStartSize;
3025 UINTN NewSize;
3026
3027 //
3028 // ASSERTs
3029 //
3030 ASSERT(Destination != NULL);
3031
3032 //
3033 // If there's nothing to do then just return Destination
3034 //
3035 if (Source == NULL) {
3036 return (*Destination);
3037 }
3038
3039 //
3040 // allow for un-initialized pointers, based on size being 0
3041 //
3042 if (CurrentSize != NULL && *CurrentSize == 0) {
3043 *Destination = NULL;
3044 }
3045
3046 //
3047 // allow for NULL pointers address as Destination
3048 //
3049 if (*Destination != NULL) {
3050 ASSERT(CurrentSize != 0);
3051 DestinationStartSize = StrSize(*Destination);
3052 ASSERT(DestinationStartSize <= *CurrentSize);
3053 } else {
3054 DestinationStartSize = 0;
3055// ASSERT(*CurrentSize == 0);
3056 }
3057
3058 //
3059 // Append all of Source?
3060 //
3061 if (Count == 0) {
3062 Count = StrLen(Source);
3063 }
3064
3065 //
3066 // Test and grow if required
3067 //
3068 if (CurrentSize != NULL) {
3069 NewSize = *CurrentSize;
3070 while (NewSize < (DestinationStartSize + (Count*sizeof(CHAR16)))) {
3071 NewSize += 2 * Count * sizeof(CHAR16);
3072 }
3073 *Destination = ReallocatePool(*CurrentSize, NewSize, *Destination);
jcarseyc9d92df2010-02-03 15:37:54 +00003074 ASSERT(*Destination != NULL);
jcarsey125c2cf2009-11-18 21:36:50 +00003075 *CurrentSize = NewSize;
3076 } else {
3077 *Destination = AllocateZeroPool((Count+1)*sizeof(CHAR16));
jcarseyc9d92df2010-02-03 15:37:54 +00003078 ASSERT(*Destination != NULL);
jcarsey125c2cf2009-11-18 21:36:50 +00003079 }
3080
3081 //
3082 // Now use standard StrnCat on a big enough buffer
3083 //
jcarseyc9d92df2010-02-03 15:37:54 +00003084 if (*Destination == NULL) {
3085 return (NULL);
3086 }
jcarsey125c2cf2009-11-18 21:36:50 +00003087 return StrnCat(*Destination, Source, Count);
3088}
jcarseyc9d92df2010-02-03 15:37:54 +00003089
3090/**
3091 Prompt the user and return the resultant answer to the requestor.
3092
3093 This function will display the requested question on the shell prompt and then
3094 wait for an apropriate answer to be input from the console.
3095
jcarseya405b862010-09-14 05:18:09 +00003096 if the SHELL_PROMPT_REQUEST_TYPE is SHELL_PROMPT_REQUEST_TYPE_YESNO, ShellPromptResponseTypeQuitContinue
jcarseyc9d92df2010-02-03 15:37:54 +00003097 or SHELL_PROMPT_REQUEST_TYPE_YESNOCANCEL then *Response is of type SHELL_PROMPT_RESPONSE.
3098
jcarseya405b862010-09-14 05:18:09 +00003099 if the SHELL_PROMPT_REQUEST_TYPE is ShellPromptResponseTypeFreeform then *Response is of type
jcarseyc9d92df2010-02-03 15:37:54 +00003100 CHAR16*.
3101
3102 In either case *Response must be callee freed if Response was not NULL;
3103
3104 @param Type What type of question is asked. This is used to filter the input
3105 to prevent invalid answers to question.
3106 @param Prompt Pointer to string prompt to use to request input.
3107 @param Response Pointer to Response which will be populated upon return.
3108
3109 @retval EFI_SUCCESS The operation was sucessful.
3110 @retval EFI_UNSUPPORTED The operation is not supported as requested.
3111 @retval EFI_INVALID_PARAMETER A parameter was invalid.
3112 @return other The operation failed.
3113**/
3114EFI_STATUS
3115EFIAPI
3116ShellPromptForResponse (
3117 IN SHELL_PROMPT_REQUEST_TYPE Type,
3118 IN CHAR16 *Prompt OPTIONAL,
3119 IN OUT VOID **Response OPTIONAL
3120 )
3121{
3122 EFI_STATUS Status;
3123 EFI_INPUT_KEY Key;
3124 UINTN EventIndex;
3125 SHELL_PROMPT_RESPONSE *Resp;
jcarseya405b862010-09-14 05:18:09 +00003126 UINTN Size;
3127 CHAR16 *Buffer;
jcarseyc9d92df2010-02-03 15:37:54 +00003128
jcarseya405b862010-09-14 05:18:09 +00003129 Status = EFI_UNSUPPORTED;
3130 Resp = NULL;
3131 Buffer = NULL;
3132 Size = 0;
3133 if (Type != ShellPromptResponseTypeFreeform) {
jcarsey252d9452011-03-25 20:49:53 +00003134 Resp = (SHELL_PROMPT_RESPONSE*)AllocateZeroPool(sizeof(SHELL_PROMPT_RESPONSE));
jcarsey3e082d52010-10-04 16:44:57 +00003135 if (Resp == NULL) {
3136 return (EFI_OUT_OF_RESOURCES);
3137 }
xdu290bfa222010-07-19 05:21:27 +00003138 }
jcarseyc9d92df2010-02-03 15:37:54 +00003139
3140 switch(Type) {
jcarseya405b862010-09-14 05:18:09 +00003141 case ShellPromptResponseTypeQuitContinue:
jcarseyc9d92df2010-02-03 15:37:54 +00003142 if (Prompt != NULL) {
3143 ShellPrintEx(-1, -1, L"%s", Prompt);
3144 }
3145 //
3146 // wait for valid response
3147 //
3148 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
3149 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
3150 ASSERT_EFI_ERROR(Status);
3151 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3152 if (Key.UnicodeChar == L'Q' || Key.UnicodeChar ==L'q') {
jcarseya405b862010-09-14 05:18:09 +00003153 *Resp = ShellPromptResponseQuit;
jcarseyc9d92df2010-02-03 15:37:54 +00003154 } else {
jcarseya405b862010-09-14 05:18:09 +00003155 *Resp = ShellPromptResponseContinue;
jcarseyc9d92df2010-02-03 15:37:54 +00003156 }
3157 break;
jcarseya405b862010-09-14 05:18:09 +00003158 case ShellPromptResponseTypeYesNoCancel:
jcarseyc9d92df2010-02-03 15:37:54 +00003159 if (Prompt != NULL) {
3160 ShellPrintEx(-1, -1, L"%s", Prompt);
3161 }
3162 //
3163 // wait for valid response
3164 //
jcarseya405b862010-09-14 05:18:09 +00003165 *Resp = ShellPromptResponseMax;
3166 while (*Resp == ShellPromptResponseMax) {
jcarseyc9d92df2010-02-03 15:37:54 +00003167 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
3168 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
3169 ASSERT_EFI_ERROR(Status);
3170 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3171 switch (Key.UnicodeChar) {
3172 case L'Y':
3173 case L'y':
jcarseya405b862010-09-14 05:18:09 +00003174 *Resp = ShellPromptResponseYes;
jcarseyc9d92df2010-02-03 15:37:54 +00003175 break;
3176 case L'N':
3177 case L'n':
jcarseya405b862010-09-14 05:18:09 +00003178 *Resp = ShellPromptResponseNo;
jcarseyc9d92df2010-02-03 15:37:54 +00003179 break;
3180 case L'C':
3181 case L'c':
jcarseya405b862010-09-14 05:18:09 +00003182 *Resp = ShellPromptResponseCancel;
3183 break;
3184 }
3185 }
3186 break; case ShellPromptResponseTypeYesNoAllCancel:
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 case L'A':
3209 case L'a':
3210 *Resp = ShellPromptResponseAll;
3211 break;
3212 case L'C':
3213 case L'c':
3214 *Resp = ShellPromptResponseCancel;
jcarseyc9d92df2010-02-03 15:37:54 +00003215 break;
3216 }
3217 }
3218 break;
jcarseya405b862010-09-14 05:18:09 +00003219 case ShellPromptResponseTypeEnterContinue:
3220 case ShellPromptResponseTypeAnyKeyContinue:
jcarseyc9d92df2010-02-03 15:37:54 +00003221 if (Prompt != NULL) {
3222 ShellPrintEx(-1, -1, L"%s", Prompt);
3223 }
3224 //
3225 // wait for valid response
3226 //
jcarseya405b862010-09-14 05:18:09 +00003227 *Resp = ShellPromptResponseMax;
3228 while (*Resp == ShellPromptResponseMax) {
jcarseyc9d92df2010-02-03 15:37:54 +00003229 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
jcarseya405b862010-09-14 05:18:09 +00003230 if (Type == ShellPromptResponseTypeEnterContinue) {
jcarseyc9d92df2010-02-03 15:37:54 +00003231 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
3232 ASSERT_EFI_ERROR(Status);
3233 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3234 if (Key.UnicodeChar == CHAR_CARRIAGE_RETURN) {
jcarseya405b862010-09-14 05:18:09 +00003235 *Resp = ShellPromptResponseContinue;
jcarseyc9d92df2010-02-03 15:37:54 +00003236 break;
3237 }
3238 }
jcarseya405b862010-09-14 05:18:09 +00003239 if (Type == ShellPromptResponseTypeAnyKeyContinue) {
3240 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
3241 ASSERT_EFI_ERROR(Status);
3242 *Resp = ShellPromptResponseContinue;
jcarseyc9d92df2010-02-03 15:37:54 +00003243 break;
3244 }
3245 }
3246 break;
jcarseya405b862010-09-14 05:18:09 +00003247 case ShellPromptResponseTypeYesNo:
3248 if (Prompt != NULL) {
3249 ShellPrintEx(-1, -1, L"%s", Prompt);
3250 }
3251 //
3252 // wait for valid response
3253 //
3254 *Resp = ShellPromptResponseMax;
3255 while (*Resp == ShellPromptResponseMax) {
3256 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
3257 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
3258 ASSERT_EFI_ERROR(Status);
3259 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3260 switch (Key.UnicodeChar) {
3261 case L'Y':
3262 case L'y':
3263 *Resp = ShellPromptResponseYes;
3264 break;
3265 case L'N':
3266 case L'n':
3267 *Resp = ShellPromptResponseNo;
3268 break;
3269 }
3270 }
3271 break;
3272 case ShellPromptResponseTypeFreeform:
3273 if (Prompt != NULL) {
3274 ShellPrintEx(-1, -1, L"%s", Prompt);
3275 }
3276 while(1) {
3277 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
3278 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
3279 ASSERT_EFI_ERROR(Status);
3280 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3281 if (Key.UnicodeChar == CHAR_CARRIAGE_RETURN) {
3282 break;
3283 }
3284 ASSERT((Buffer == NULL && Size == 0) || (Buffer != NULL));
3285 StrnCatGrow(&Buffer, &Size, &Key.UnicodeChar, 1);
3286 }
3287 break;
3288 //
3289 // This is the location to add new prompt types.
3290 //
jcarseyc9d92df2010-02-03 15:37:54 +00003291 default:
jcarseya405b862010-09-14 05:18:09 +00003292 ASSERT(FALSE);
jcarseyc9d92df2010-02-03 15:37:54 +00003293 }
3294
3295 if (Response != NULL) {
jcarseya405b862010-09-14 05:18:09 +00003296 if (Resp != NULL) {
3297 *Response = Resp;
3298 } else if (Buffer != NULL) {
3299 *Response = Buffer;
3300 }
jcarseyc9d92df2010-02-03 15:37:54 +00003301 } else {
jcarseya405b862010-09-14 05:18:09 +00003302 if (Resp != NULL) {
3303 FreePool(Resp);
3304 }
3305 if (Buffer != NULL) {
3306 FreePool(Buffer);
3307 }
jcarseyc9d92df2010-02-03 15:37:54 +00003308 }
3309
jcarseya405b862010-09-14 05:18:09 +00003310 ShellPrintEx(-1, -1, L"\r\n");
jcarseyc9d92df2010-02-03 15:37:54 +00003311 return (Status);
3312}
3313
3314/**
3315 Prompt the user and return the resultant answer to the requestor.
3316
3317 This function is the same as ShellPromptForResponse, except that the prompt is
3318 automatically pulled from HII.
3319
3320 @param Type What type of question is asked. This is used to filter the input
3321 to prevent invalid answers to question.
jcarseya405b862010-09-14 05:18:09 +00003322 @param[in] HiiFormatStringId The format string Id for getting from Hii.
3323 @param[in] HiiFormatHandle The format string Handle for getting from Hii.
3324 @param Response Pointer to Response which will be populated upon return.
jcarseyc9d92df2010-02-03 15:37:54 +00003325
3326 @retval EFI_SUCCESS the operation was sucessful.
3327 @return other the operation failed.
3328
3329 @sa ShellPromptForResponse
3330**/
3331EFI_STATUS
3332EFIAPI
3333ShellPromptForResponseHii (
3334 IN SHELL_PROMPT_REQUEST_TYPE Type,
3335 IN CONST EFI_STRING_ID HiiFormatStringId,
3336 IN CONST EFI_HANDLE HiiFormatHandle,
3337 IN OUT VOID **Response
3338 )
3339{
3340 CHAR16 *Prompt;
3341 EFI_STATUS Status;
3342
3343 Prompt = HiiGetString(HiiFormatHandle, HiiFormatStringId, NULL);
3344 Status = ShellPromptForResponse(Type, Prompt, Response);
3345 FreePool(Prompt);
3346 return (Status);
3347}
3348
jcarseya405b862010-09-14 05:18:09 +00003349/**
3350 Function to determin if an entire string is a valid number.
jcarseyc9d92df2010-02-03 15:37:54 +00003351
jcarseya405b862010-09-14 05:18:09 +00003352 If Hex it must be preceeded with a 0x or has ForceHex, set TRUE.
3353
3354 @param[in] String The string to evaluate.
3355 @param[in] ForceHex TRUE - always assume hex.
3356 @param[in] StopAtSpace TRUE to halt upon finding a space, FALSE to keep going.
3357
3358 @retval TRUE It is all numeric (dec/hex) characters.
3359 @retval FALSE There is a non-numeric character.
3360**/
3361BOOLEAN
3362EFIAPI
jcarsey252d9452011-03-25 20:49:53 +00003363InternalShellIsHexOrDecimalNumber (
jcarseya405b862010-09-14 05:18:09 +00003364 IN CONST CHAR16 *String,
3365 IN CONST BOOLEAN ForceHex,
3366 IN CONST BOOLEAN StopAtSpace
3367 )
3368{
3369 BOOLEAN Hex;
3370
3371 //
3372 // chop off a single negative sign
3373 //
3374 if (String != NULL && *String == L'-') {
3375 String++;
3376 }
3377
3378 if (String == NULL) {
3379 return (FALSE);
3380 }
3381
3382 //
3383 // chop leading zeroes
3384 //
3385 while(String != NULL && *String == L'0'){
3386 String++;
3387 }
3388 //
3389 // allow '0x' or '0X', but not 'x' or 'X'
3390 //
3391 if (String != NULL && (*String == L'x' || *String == L'X')) {
3392 if (*(String-1) != L'0') {
3393 //
3394 // we got an x without a preceeding 0
3395 //
3396 return (FALSE);
3397 }
3398 String++;
3399 Hex = TRUE;
3400 } else if (ForceHex) {
3401 Hex = TRUE;
3402 } else {
3403 Hex = FALSE;
3404 }
3405
3406 //
3407 // loop through the remaining characters and use the lib function
3408 //
3409 for ( ; String != NULL && *String != CHAR_NULL && !(StopAtSpace && *String == L' ') ; String++){
3410 if (Hex) {
3411 if (!ShellIsHexaDecimalDigitCharacter(*String)) {
3412 return (FALSE);
3413 }
3414 } else {
3415 if (!ShellIsDecimalDigitCharacter(*String)) {
3416 return (FALSE);
3417 }
3418 }
3419 }
jcarsey252d9452011-03-25 20:49:53 +00003420
jcarseya405b862010-09-14 05:18:09 +00003421 return (TRUE);
3422}
3423
3424/**
3425 Function to determine if a given filename exists.
3426
3427 @param[in] Name Path to test.
3428
3429 @retval EFI_SUCCESS The Path represents a file.
3430 @retval EFI_NOT_FOUND The Path does not represent a file.
3431 @retval other The path failed to open.
3432**/
3433EFI_STATUS
3434EFIAPI
3435ShellFileExists(
3436 IN CONST CHAR16 *Name
3437 )
3438{
3439 EFI_STATUS Status;
3440 EFI_SHELL_FILE_INFO *List;
3441
3442 ASSERT(Name != NULL);
3443
3444 List = NULL;
3445 Status = ShellOpenFileMetaArg((CHAR16*)Name, EFI_FILE_MODE_READ, &List);
3446 if (EFI_ERROR(Status)) {
3447 return (Status);
3448 }
3449
3450 ShellCloseFileMetaArg(&List);
3451
3452 return (EFI_SUCCESS);
3453}
jcarsey252d9452011-03-25 20:49:53 +00003454
3455/**
3456 Convert a Unicode character to upper case only if
3457 it maps to a valid small-case ASCII character.
3458
3459 This internal function only deal with Unicode character
3460 which maps to a valid small-case ASCII character, i.e.
3461 L'a' to L'z'. For other Unicode character, the input character
3462 is returned directly.
3463
3464 @param Char The character to convert.
3465
3466 @retval LowerCharacter If the Char is with range L'a' to L'z'.
3467 @retval Unchanged Otherwise.
3468
3469**/
3470CHAR16
3471EFIAPI
3472InternalShellCharToUpper (
3473 IN CHAR16 Char
3474 )
3475{
3476 if (Char >= L'a' && Char <= L'z') {
3477 return (CHAR16) (Char - (L'a' - L'A'));
3478 }
3479
3480 return Char;
3481}
3482
3483/**
3484 Convert a Unicode character to numerical value.
3485
3486 This internal function only deal with Unicode character
3487 which maps to a valid hexadecimal ASII character, i.e.
3488 L'0' to L'9', L'a' to L'f' or L'A' to L'F'. For other
3489 Unicode character, the value returned does not make sense.
3490
3491 @param Char The character to convert.
3492
3493 @return The numerical value converted.
3494
3495**/
3496UINTN
3497EFIAPI
3498InternalShellHexCharToUintn (
3499 IN CHAR16 Char
3500 )
3501{
3502 if (ShellIsDecimalDigitCharacter (Char)) {
3503 return Char - L'0';
3504 }
3505
3506 return (UINTN) (10 + InternalShellCharToUpper (Char) - L'A');
3507}
3508
3509/**
3510 Convert a Null-terminated Unicode hexadecimal string to a value of type UINT64.
3511
3512 This function returns a value of type UINTN by interpreting the contents
3513 of the Unicode string specified by String as a hexadecimal number.
3514 The format of the input Unicode string String is:
3515
3516 [spaces][zeros][x][hexadecimal digits].
3517
3518 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
3519 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix.
3520 If "x" appears in the input string, it must be prefixed with at least one 0.
3521 The function will ignore the pad space, which includes spaces or tab characters,
3522 before [zeros], [x] or [hexadecimal digit]. The running zero before [x] or
3523 [hexadecimal digit] will be ignored. Then, the decoding starts after [x] or the
3524 first valid hexadecimal digit. Then, the function stops at the first character that is
3525 a not a valid hexadecimal character or NULL, whichever one comes first.
3526
3527 If String has only pad spaces, then zero is returned.
3528 If String has no leading pad spaces, leading zeros or valid hexadecimal digits,
3529 then zero is returned.
3530
3531 @param[in] String A pointer to a Null-terminated Unicode string.
3532 @param[out] Value Upon a successful return the value of the conversion.
3533 @param[in] StopAtSpace FALSE to skip spaces.
3534
3535 @retval EFI_SUCCESS The conversion was successful.
3536 @retval EFI_INVALID_PARAMETER A parameter was NULL or invalid.
3537 @retval EFI_DEVICE_ERROR An overflow occured.
3538**/
3539EFI_STATUS
3540EFIAPI
3541InternalShellStrHexToUint64 (
3542 IN CONST CHAR16 *String,
3543 OUT UINT64 *Value,
3544 IN CONST BOOLEAN StopAtSpace
3545 )
3546{
3547 UINT64 Result;
3548
3549 if (String == NULL || StrSize(String) == 0 || Value == NULL) {
3550 return (EFI_INVALID_PARAMETER);
3551 }
3552
3553 //
3554 // Ignore the pad spaces (space or tab)
3555 //
3556 while ((*String == L' ') || (*String == L'\t')) {
3557 String++;
3558 }
3559
3560 //
3561 // Ignore leading Zeros after the spaces
3562 //
3563 while (*String == L'0') {
3564 String++;
3565 }
3566
3567 if (InternalShellCharToUpper (*String) == L'X') {
3568 if (*(String - 1) != L'0') {
3569 return 0;
3570 }
3571 //
3572 // Skip the 'X'
3573 //
3574 String++;
3575 }
3576
3577 Result = 0;
3578
3579 //
3580 // Skip spaces if requested
3581 //
3582 while (StopAtSpace && *String == L' ') {
3583 String++;
3584 }
3585
3586 while (ShellIsHexaDecimalDigitCharacter (*String)) {
3587 //
3588 // If the Hex Number represented by String overflows according
3589 // to the range defined by UINTN, then ASSERT().
3590 //
3591 if (!(Result <= (RShiftU64((((UINT64) ~0) - InternalShellHexCharToUintn (*String)), 4)))) {
3592// if (!(Result <= ((((UINT64) ~0) - InternalShellHexCharToUintn (*String)) >> 4))) {
3593 return (EFI_DEVICE_ERROR);
3594 }
3595
3596 Result = (LShiftU64(Result, 4));
3597 Result += InternalShellHexCharToUintn (*String);
3598 String++;
3599
3600 //
3601 // Skip spaces if requested
3602 //
3603 while (StopAtSpace && *String == L' ') {
3604 String++;
3605 }
3606 }
3607
3608 *Value = Result;
3609 return (EFI_SUCCESS);
3610}
3611
3612/**
3613 Convert a Null-terminated Unicode decimal string to a value of
3614 type UINT64.
3615
3616 This function returns a value of type UINT64 by interpreting the contents
3617 of the Unicode string specified by String as a decimal number. The format
3618 of the input Unicode string String is:
3619
3620 [spaces] [decimal digits].
3621
3622 The valid decimal digit character is in the range [0-9]. The
3623 function will ignore the pad space, which includes spaces or
3624 tab characters, before [decimal digits]. The running zero in the
3625 beginning of [decimal digits] will be ignored. Then, the function
3626 stops at the first character that is a not a valid decimal character
3627 or a Null-terminator, whichever one comes first.
3628
3629 If String has only pad spaces, then 0 is returned.
3630 If String has no pad spaces or valid decimal digits,
3631 then 0 is returned.
3632
3633 @param[in] String A pointer to a Null-terminated Unicode string.
3634 @param[out] Value Upon a successful return the value of the conversion.
3635 @param[in] StopAtSpace FALSE to skip spaces.
3636
3637 @retval EFI_SUCCESS The conversion was successful.
3638 @retval EFI_INVALID_PARAMETER A parameter was NULL or invalid.
3639 @retval EFI_DEVICE_ERROR An overflow occured.
3640**/
3641EFI_STATUS
3642EFIAPI
3643InternalShellStrDecimalToUint64 (
3644 IN CONST CHAR16 *String,
3645 OUT UINT64 *Value,
3646 IN CONST BOOLEAN StopAtSpace
3647 )
3648{
3649 UINT64 Result;
3650
3651 if (String == NULL || StrSize (String) == 0 || Value == NULL) {
3652 return (EFI_INVALID_PARAMETER);
3653 }
3654
3655 //
3656 // Ignore the pad spaces (space or tab)
3657 //
3658 while ((*String == L' ') || (*String == L'\t')) {
3659 String++;
3660 }
3661
3662 //
3663 // Ignore leading Zeros after the spaces
3664 //
3665 while (*String == L'0') {
3666 String++;
3667 }
3668
3669 Result = 0;
3670
3671 //
3672 // Skip spaces if requested
3673 //
3674 while (StopAtSpace && *String == L' ') {
3675 String++;
3676 }
3677 while (ShellIsDecimalDigitCharacter (*String)) {
3678 //
3679 // If the number represented by String overflows according
3680 // to the range defined by UINT64, then ASSERT().
3681 //
3682
3683 if (!(Result <= (DivU64x32((((UINT64) ~0) - (*String - L'0')),10)))) {
3684 return (EFI_DEVICE_ERROR);
3685 }
3686
3687 Result = MultU64x32(Result, 10) + (*String - L'0');
3688 String++;
3689
3690 //
3691 // Stop at spaces if requested
3692 //
3693 if (StopAtSpace && *String == L' ') {
3694 break;
3695 }
3696 }
3697
3698 *Value = Result;
3699
3700 return (EFI_SUCCESS);
3701}
3702
3703/**
3704 Function to verify and convert a string to its numerical value.
3705
3706 If Hex it must be preceeded with a 0x, 0X, or has ForceHex set TRUE.
3707
3708 @param[in] String The string to evaluate.
3709 @param[out] Value Upon a successful return the value of the conversion.
3710 @param[in] ForceHex TRUE - always assume hex.
3711 @param[in] StopAtSpace FALSE to skip spaces.
3712
3713 @retval EFI_SUCCESS The conversion was successful.
3714 @retval EFI_INVALID_PARAMETER String contained an invalid character.
3715 @retval EFI_NOT_FOUND String was a number, but Value was NULL.
3716**/
3717EFI_STATUS
3718EFIAPI
3719ShellConvertStringToUint64(
3720 IN CONST CHAR16 *String,
3721 OUT UINT64 *Value,
3722 IN CONST BOOLEAN ForceHex,
3723 IN CONST BOOLEAN StopAtSpace
3724 )
3725{
3726 UINT64 RetVal;
3727 CONST CHAR16 *Walker;
3728 EFI_STATUS Status;
3729 BOOLEAN Hex;
3730
3731 Hex = ForceHex;
3732
3733 if (!InternalShellIsHexOrDecimalNumber(String, Hex, StopAtSpace)) {
3734 if (!Hex) {
3735 Hex = TRUE;
3736 if (!InternalShellIsHexOrDecimalNumber(String, Hex, StopAtSpace)) {
3737 return (EFI_INVALID_PARAMETER);
3738 }
3739 } else {
3740 return (EFI_INVALID_PARAMETER);
3741 }
3742 }
3743
3744 //
3745 // Chop off leading spaces
3746 //
3747 for (Walker = String; Walker != NULL && *Walker != CHAR_NULL && *Walker == L' '; Walker++);
3748
3749 //
3750 // make sure we have something left that is numeric.
3751 //
3752 if (Walker == NULL || *Walker == CHAR_NULL || !InternalShellIsHexOrDecimalNumber(Walker, Hex, StopAtSpace)) {
3753 return (EFI_INVALID_PARAMETER);
3754 }
3755
3756 //
3757 // do the conversion.
3758 //
3759 if (Hex || StrnCmp(Walker, L"0x", 2) == 0 || StrnCmp(Walker, L"0X", 2) == 0){
3760 Status = InternalShellStrHexToUint64(Walker, &RetVal, StopAtSpace);
3761 } else {
3762 Status = InternalShellStrDecimalToUint64(Walker, &RetVal, StopAtSpace);
3763 }
3764
3765 if (Value == NULL && !EFI_ERROR(Status)) {
3766 return (EFI_NOT_FOUND);
3767 }
3768
3769 if (Value != NULL) {
3770 *Value = RetVal;
3771 }
3772
3773 return (Status);
3774}
3775
3776/**
3777 Function to determin if an entire string is a valid number.
3778
3779 If Hex it must be preceeded with a 0x or has ForceHex, set TRUE.
3780
3781 @param[in] String The string to evaluate.
3782 @param[in] ForceHex TRUE - always assume hex.
3783 @param[in] StopAtSpace TRUE to halt upon finding a space, FALSE to keep going.
3784
3785 @retval TRUE It is all numeric (dec/hex) characters.
3786 @retval FALSE There is a non-numeric character.
3787**/
3788BOOLEAN
3789EFIAPI
3790ShellIsHexOrDecimalNumber (
3791 IN CONST CHAR16 *String,
3792 IN CONST BOOLEAN ForceHex,
3793 IN CONST BOOLEAN StopAtSpace
3794 )
3795{
3796 if (ShellConvertStringToUint64(String, NULL, ForceHex, StopAtSpace) == EFI_NOT_FOUND) {
3797 return (TRUE);
3798 }
3799 return (FALSE);
3800}