blob: 3c1b983a6028526787ef0f07f91d97f04cf14c57 [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;
jcarsey366f81a2011-06-27 21:04:22 +000033EFI_SHELL_PROTOCOL *gEfiShellProtocol;
34EFI_SHELL_PARAMETERS_PROTOCOL *gEfiShellParametersProtocol;
jcarseya405b862010-09-14 05:18:09 +000035EFI_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,
jcarsey366f81a2011-06-27 21:04:22 +0000185 (VOID **)&gEfiShellProtocol,
jcarseya405b862010-09-14 05:18:09 +0000186 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,
jcarsey366f81a2011-06-27 21:04:22 +0000197 (VOID **)&gEfiShellProtocol
jcarseya405b862010-09-14 05:18:09 +0000198 );
199 if (EFI_ERROR(Status)) {
jcarsey366f81a2011-06-27 21:04:22 +0000200 gEfiShellProtocol = NULL;
jcarseya405b862010-09-14 05:18:09 +0000201 }
jcarsey94b17fa2009-05-07 18:46:18 +0000202 }
jcarseya405b862010-09-14 05:18:09 +0000203 Status = gBS->OpenProtocol(
204 ImageHandle,
205 &gEfiShellParametersProtocolGuid,
jcarsey366f81a2011-06-27 21:04:22 +0000206 (VOID **)&gEfiShellParametersProtocol,
jcarseya405b862010-09-14 05:18:09 +0000207 ImageHandle,
208 NULL,
209 EFI_OPEN_PROTOCOL_GET_PROTOCOL
210 );
jcarsey94b17fa2009-05-07 18:46:18 +0000211 if (EFI_ERROR(Status)) {
jcarsey366f81a2011-06-27 21:04:22 +0000212 gEfiShellParametersProtocol = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +0000213 }
214
jcarsey366f81a2011-06-27 21:04:22 +0000215 if (gEfiShellParametersProtocol == NULL || gEfiShellProtocol == NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +0000216 //
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) ||
jcarsey366f81a2011-06-27 21:04:22 +0000241 (gEfiShellProtocol != NULL && gEfiShellParametersProtocol != NULL) ) {
242 if (gEfiShellProtocol != NULL) {
243 FileFunctionMap.GetFileInfo = gEfiShellProtocol->GetFileInfo;
244 FileFunctionMap.SetFileInfo = gEfiShellProtocol->SetFileInfo;
245 FileFunctionMap.ReadFile = gEfiShellProtocol->ReadFile;
246 FileFunctionMap.WriteFile = gEfiShellProtocol->WriteFile;
247 FileFunctionMap.CloseFile = gEfiShellProtocol->CloseFile;
248 FileFunctionMap.DeleteFile = gEfiShellProtocol->DeleteFile;
249 FileFunctionMap.GetFilePosition = gEfiShellProtocol->GetFilePosition;
250 FileFunctionMap.SetFilePosition = gEfiShellProtocol->SetFilePosition;
251 FileFunctionMap.FlushFile = gEfiShellProtocol->FlushFile;
252 FileFunctionMap.GetFileSize = gEfiShellProtocol->GetFileSize;
jcarseyd2b45642009-05-11 18:02:16 +0000253 } 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;
jcarsey366f81a2011-06-27 21:04:22 +0000288 gEfiShellProtocol = NULL;
289 gEfiShellParametersProtocol = NULL;
jcarseyd2b45642009-05-11 18:02:16 +0000290 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 }
jcarsey366f81a2011-06-27 21:04:22 +0000333 if (gEfiShellProtocol != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +0000334 gBS->CloseProtocol(ImageHandle,
335 &gEfiShellProtocolGuid,
336 ImageHandle,
jcarsey1e6e84c2010-01-25 20:05:08 +0000337 NULL);
jcarsey366f81a2011-06-27 21:04:22 +0000338 gEfiShellProtocol = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +0000339 }
jcarsey366f81a2011-06-27 21:04:22 +0000340 if (gEfiShellParametersProtocol != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +0000341 gBS->CloseProtocol(ImageHandle,
342 &gEfiShellParametersProtocolGuid,
343 ImageHandle,
jcarsey1e6e84c2010-01-25 20:05:08 +0000344 NULL);
jcarsey366f81a2011-06-27 21:04:22 +0000345 gEfiShellParametersProtocol = 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
jcarsey92a54472011-06-27 20:33:13 +0000486 if (FilePath == NULL || FileHandle == NULL || DeviceHandle == NULL) {
487 return (EFI_INVALID_PARAMETER);
488 }
489
jcarsey1e6e84c2010-01-25 20:05:08 +0000490 //
jcarsey94b17fa2009-05-07 18:46:18 +0000491 // which shell interface should we use
492 //
jcarsey366f81a2011-06-27 21:04:22 +0000493 if (gEfiShellProtocol != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +0000494 //
495 // use UEFI Shell 2.0 method.
496 //
jcarsey366f81a2011-06-27 21:04:22 +0000497 FileName = gEfiShellProtocol->GetFilePathFromDevicePath(*FilePath);
jcarsey94b17fa2009-05-07 18:46:18 +0000498 if (FileName == NULL) {
499 return (EFI_INVALID_PARAMETER);
500 }
501 Status = ShellOpenFileByName(FileName, FileHandle, OpenMode, Attributes);
502 FreePool(FileName);
503 return (Status);
jcarsey1e6e84c2010-01-25 20:05:08 +0000504 }
jcarseyd2b45642009-05-11 18:02:16 +0000505
506
507 //
508 // use old shell method.
509 //
jcarsey1e6e84c2010-01-25 20:05:08 +0000510 Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid,
511 FilePath,
jcarseyd2b45642009-05-11 18:02:16 +0000512 DeviceHandle);
513 if (EFI_ERROR (Status)) {
514 return Status;
515 }
516 Status = gBS->OpenProtocol(*DeviceHandle,
517 &gEfiSimpleFileSystemProtocolGuid,
jcarseyb1f95a02009-06-16 00:23:19 +0000518 (VOID**)&EfiSimpleFileSystemProtocol,
jcarseyd2b45642009-05-11 18:02:16 +0000519 gImageHandle,
520 NULL,
521 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
522 if (EFI_ERROR (Status)) {
523 return Status;
524 }
jcarseya405b862010-09-14 05:18:09 +0000525 Status = EfiSimpleFileSystemProtocol->OpenVolume(EfiSimpleFileSystemProtocol, &Handle1);
jcarseyd2b45642009-05-11 18:02:16 +0000526 if (EFI_ERROR (Status)) {
527 FileHandle = NULL;
528 return Status;
529 }
530
531 //
532 // go down directories one node at a time.
533 //
534 while (!IsDevicePathEnd (*FilePath)) {
jcarsey94b17fa2009-05-07 18:46:18 +0000535 //
jcarseyd2b45642009-05-11 18:02:16 +0000536 // For file system access each node should be a file path component
jcarsey94b17fa2009-05-07 18:46:18 +0000537 //
jcarseyd2b45642009-05-11 18:02:16 +0000538 if (DevicePathType (*FilePath) != MEDIA_DEVICE_PATH ||
539 DevicePathSubType (*FilePath) != MEDIA_FILEPATH_DP
jcarseya405b862010-09-14 05:18:09 +0000540 ) {
jcarsey94b17fa2009-05-07 18:46:18 +0000541 FileHandle = NULL;
jcarseyd2b45642009-05-11 18:02:16 +0000542 return (EFI_INVALID_PARAMETER);
jcarsey94b17fa2009-05-07 18:46:18 +0000543 }
jcarseyd2b45642009-05-11 18:02:16 +0000544 //
545 // Open this file path node
546 //
jcarseya405b862010-09-14 05:18:09 +0000547 Handle2 = Handle1;
548 Handle1 = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +0000549
550 //
jcarseyd2b45642009-05-11 18:02:16 +0000551 // Try to test opening an existing file
jcarsey94b17fa2009-05-07 18:46:18 +0000552 //
jcarseya405b862010-09-14 05:18:09 +0000553 Status = Handle2->Open (
554 Handle2,
555 &Handle1,
jcarseyd2b45642009-05-11 18:02:16 +0000556 ((FILEPATH_DEVICE_PATH*)*FilePath)->PathName,
557 OpenMode &~EFI_FILE_MODE_CREATE,
558 0
jcarseya405b862010-09-14 05:18:09 +0000559 );
jcarsey94b17fa2009-05-07 18:46:18 +0000560
jcarseyd2b45642009-05-11 18:02:16 +0000561 //
562 // see if the error was that it needs to be created
563 //
564 if ((EFI_ERROR (Status)) && (OpenMode != (OpenMode &~EFI_FILE_MODE_CREATE))) {
jcarseya405b862010-09-14 05:18:09 +0000565 Status = Handle2->Open (
566 Handle2,
567 &Handle1,
jcarsey94b17fa2009-05-07 18:46:18 +0000568 ((FILEPATH_DEVICE_PATH*)*FilePath)->PathName,
jcarseyd2b45642009-05-11 18:02:16 +0000569 OpenMode,
570 Attributes
jcarseya405b862010-09-14 05:18:09 +0000571 );
jcarsey94b17fa2009-05-07 18:46:18 +0000572 }
jcarseyd2b45642009-05-11 18:02:16 +0000573 //
574 // Close the last node
575 //
jcarseya405b862010-09-14 05:18:09 +0000576 Handle2->Close (Handle2);
jcarseyd2b45642009-05-11 18:02:16 +0000577
578 if (EFI_ERROR(Status)) {
579 return (Status);
580 }
581
582 //
583 // Get the next node
584 //
585 *FilePath = NextDevicePathNode (*FilePath);
jcarsey94b17fa2009-05-07 18:46:18 +0000586 }
jcarseya405b862010-09-14 05:18:09 +0000587
588 //
589 // This is a weak spot since if the undefined SHELL_FILE_HANDLE format changes this must change also!
590 //
591 *FileHandle = (VOID*)Handle1;
jcarseyd2b45642009-05-11 18:02:16 +0000592 return (EFI_SUCCESS);
jcarsey94b17fa2009-05-07 18:46:18 +0000593}
594
595/**
596 This function will open a file or directory referenced by filename.
597
jcarsey1e6e84c2010-01-25 20:05:08 +0000598 If return is EFI_SUCCESS, the Filehandle is the opened file's handle;
599 otherwise, the Filehandle is NULL. The Attributes is valid only for
jcarsey94b17fa2009-05-07 18:46:18 +0000600 EFI_FILE_MODE_CREATE.
601
jcarsey92a54472011-06-27 20:33:13 +0000602 if FileName is NULL then ASSERT()
jcarsey94b17fa2009-05-07 18:46:18 +0000603
604 @param FileName pointer to file name
605 @param FileHandle pointer to the file handle.
606 @param OpenMode the mode to open the file with.
607 @param Attributes the file's file attributes.
608
609 @retval EFI_SUCCESS The information was set.
610 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
jcarsey1e6e84c2010-01-25 20:05:08 +0000611 @retval EFI_UNSUPPORTED Could not open the file path.
612 @retval EFI_NOT_FOUND The specified file could not be found on the
613 device or the file system could not be found
jcarsey94b17fa2009-05-07 18:46:18 +0000614 on the device.
615 @retval EFI_NO_MEDIA The device has no medium.
jcarsey1e6e84c2010-01-25 20:05:08 +0000616 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
jcarsey94b17fa2009-05-07 18:46:18 +0000617 medium is no longer supported.
618 @retval EFI_DEVICE_ERROR The device reported an error.
619 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
620 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
621 @retval EFI_ACCESS_DENIED The file was opened read only.
jcarsey1e6e84c2010-01-25 20:05:08 +0000622 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
jcarsey94b17fa2009-05-07 18:46:18 +0000623 file.
624 @retval EFI_VOLUME_FULL The volume is full.
625**/
626EFI_STATUS
627EFIAPI
628ShellOpenFileByName(
jcarseyb82bfcc2009-06-29 16:28:23 +0000629 IN CONST CHAR16 *FileName,
jcarseya405b862010-09-14 05:18:09 +0000630 OUT SHELL_FILE_HANDLE *FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000631 IN UINT64 OpenMode,
632 IN UINT64 Attributes
jcarseya405b862010-09-14 05:18:09 +0000633 )
634{
jcarsey94b17fa2009-05-07 18:46:18 +0000635 EFI_HANDLE DeviceHandle;
636 EFI_DEVICE_PATH_PROTOCOL *FilePath;
jcarseyb1f95a02009-06-16 00:23:19 +0000637 EFI_STATUS Status;
638 EFI_FILE_INFO *FileInfo;
jcarsey94b17fa2009-05-07 18:46:18 +0000639
640 //
641 // ASSERT if FileName is NULL
642 //
643 ASSERT(FileName != NULL);
644
jcarseya405b862010-09-14 05:18:09 +0000645 if (FileName == NULL) {
646 return (EFI_INVALID_PARAMETER);
647 }
648
jcarsey366f81a2011-06-27 21:04:22 +0000649 if (gEfiShellProtocol != NULL) {
jcarseya405b862010-09-14 05:18:09 +0000650 if ((OpenMode & EFI_FILE_MODE_CREATE) == EFI_FILE_MODE_CREATE && (Attributes & EFI_FILE_DIRECTORY) == EFI_FILE_DIRECTORY) {
651 return ShellCreateDirectory(FileName, FileHandle);
652 }
jcarsey94b17fa2009-05-07 18:46:18 +0000653 //
654 // Use UEFI Shell 2.0 method
655 //
jcarsey366f81a2011-06-27 21:04:22 +0000656 Status = gEfiShellProtocol->OpenFileByName(FileName,
jcarseyb1f95a02009-06-16 00:23:19 +0000657 FileHandle,
658 OpenMode);
jcarseya405b862010-09-14 05:18:09 +0000659 if (StrCmp(FileName, L"NUL") != 0 && !EFI_ERROR(Status) && ((OpenMode & EFI_FILE_MODE_CREATE) != 0)){
jcarsey2247dde2009-11-09 18:08:58 +0000660 FileInfo = FileFunctionMap.GetFileInfo(*FileHandle);
jcarseyb1f95a02009-06-16 00:23:19 +0000661 ASSERT(FileInfo != NULL);
662 FileInfo->Attribute = Attributes;
jcarsey2247dde2009-11-09 18:08:58 +0000663 Status = FileFunctionMap.SetFileInfo(*FileHandle, FileInfo);
664 FreePool(FileInfo);
jcarseyb1f95a02009-06-16 00:23:19 +0000665 }
666 return (Status);
jcarsey1e6e84c2010-01-25 20:05:08 +0000667 }
jcarsey94b17fa2009-05-07 18:46:18 +0000668 //
669 // Using EFI Shell version
670 // this means convert name to path and call that function
671 // since this will use EFI method again that will open it.
672 //
673 ASSERT(mEfiShellEnvironment2 != NULL);
jcarseyb82bfcc2009-06-29 16:28:23 +0000674 FilePath = mEfiShellEnvironment2->NameToPath ((CHAR16*)FileName);
xdu290bfa222010-07-19 05:21:27 +0000675 if (FilePath != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +0000676 return (ShellOpenFileByDevicePath(&FilePath,
677 &DeviceHandle,
678 FileHandle,
679 OpenMode,
jcarseya405b862010-09-14 05:18:09 +0000680 Attributes));
jcarsey94b17fa2009-05-07 18:46:18 +0000681 }
682 return (EFI_DEVICE_ERROR);
683}
684/**
685 This function create a directory
686
jcarsey1e6e84c2010-01-25 20:05:08 +0000687 If return is EFI_SUCCESS, the Filehandle is the opened directory's handle;
688 otherwise, the Filehandle is NULL. If the directory already existed, this
jcarsey94b17fa2009-05-07 18:46:18 +0000689 function opens the existing directory.
690
691 @param DirectoryName pointer to directory name
692 @param FileHandle pointer to the file handle.
693
694 @retval EFI_SUCCESS The information was set.
695 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
jcarsey1e6e84c2010-01-25 20:05:08 +0000696 @retval EFI_UNSUPPORTED Could not open the file path.
697 @retval EFI_NOT_FOUND The specified file could not be found on the
698 device or the file system could not be found
jcarsey94b17fa2009-05-07 18:46:18 +0000699 on the device.
700 @retval EFI_NO_MEDIA The device has no medium.
jcarsey1e6e84c2010-01-25 20:05:08 +0000701 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
jcarsey94b17fa2009-05-07 18:46:18 +0000702 medium is no longer supported.
703 @retval EFI_DEVICE_ERROR The device reported an error.
704 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
705 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
706 @retval EFI_ACCESS_DENIED The file was opened read only.
jcarsey1e6e84c2010-01-25 20:05:08 +0000707 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
jcarsey94b17fa2009-05-07 18:46:18 +0000708 file.
709 @retval EFI_VOLUME_FULL The volume is full.
710 @sa ShellOpenFileByName
711**/
712EFI_STATUS
713EFIAPI
714ShellCreateDirectory(
jcarseyb82bfcc2009-06-29 16:28:23 +0000715 IN CONST CHAR16 *DirectoryName,
jcarseya405b862010-09-14 05:18:09 +0000716 OUT SHELL_FILE_HANDLE *FileHandle
717 )
718{
jcarsey366f81a2011-06-27 21:04:22 +0000719 if (gEfiShellProtocol != NULL) {
jcarsey2247dde2009-11-09 18:08:58 +0000720 //
721 // Use UEFI Shell 2.0 method
722 //
jcarsey366f81a2011-06-27 21:04:22 +0000723 return (gEfiShellProtocol->CreateFile(DirectoryName,
jcarsey2247dde2009-11-09 18:08:58 +0000724 EFI_FILE_DIRECTORY,
725 FileHandle
jcarseya405b862010-09-14 05:18:09 +0000726 ));
jcarsey2247dde2009-11-09 18:08:58 +0000727 } else {
728 return (ShellOpenFileByName(DirectoryName,
729 FileHandle,
730 EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE,
731 EFI_FILE_DIRECTORY
jcarseya405b862010-09-14 05:18:09 +0000732 ));
jcarsey2247dde2009-11-09 18:08:58 +0000733 }
jcarsey94b17fa2009-05-07 18:46:18 +0000734}
735
736/**
737 This function reads information from an opened file.
738
jcarsey1e6e84c2010-01-25 20:05:08 +0000739 If FileHandle is not a directory, the function reads the requested number of
740 bytes from the file at the file's current position and returns them in Buffer.
jcarsey94b17fa2009-05-07 18:46:18 +0000741 If the read goes beyond the end of the file, the read length is truncated to the
jcarsey1e6e84c2010-01-25 20:05:08 +0000742 end of the file. The file's current position is increased by the number of bytes
743 returned. If FileHandle is a directory, the function reads the directory entry
744 at the file's current position and returns the entry in Buffer. If the Buffer
745 is not large enough to hold the current directory entry, then
746 EFI_BUFFER_TOO_SMALL is returned and the current file position is not updated.
747 BufferSize is set to be the size of the buffer needed to read the entry. On
748 success, the current position is updated to the next directory entry. If there
749 are no more directory entries, the read returns a zero-length buffer.
jcarsey94b17fa2009-05-07 18:46:18 +0000750 EFI_FILE_INFO is the structure returned as the directory entry.
751
752 @param FileHandle the opened file handle
jcarsey1e6e84c2010-01-25 20:05:08 +0000753 @param BufferSize on input the size of buffer in bytes. on return
jcarsey94b17fa2009-05-07 18:46:18 +0000754 the number of bytes written.
755 @param Buffer the buffer to put read data into.
756
757 @retval EFI_SUCCESS Data was read.
758 @retval EFI_NO_MEDIA The device has no media.
759 @retval EFI_DEVICE_ERROR The device reported an error.
760 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
jcarsey1e6e84c2010-01-25 20:05:08 +0000761 @retval EFI_BUFFER_TO_SMALL Buffer is too small. ReadSize contains required
jcarsey94b17fa2009-05-07 18:46:18 +0000762 size.
763
764**/
765EFI_STATUS
766EFIAPI
767ShellReadFile(
jcarseya405b862010-09-14 05:18:09 +0000768 IN SHELL_FILE_HANDLE FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000769 IN OUT UINTN *BufferSize,
770 OUT VOID *Buffer
jcarseya405b862010-09-14 05:18:09 +0000771 )
772{
jcarseyd2b45642009-05-11 18:02:16 +0000773 return (FileFunctionMap.ReadFile(FileHandle, BufferSize, Buffer));
jcarsey94b17fa2009-05-07 18:46:18 +0000774}
775
776
777/**
778 Write data to a file.
779
jcarsey1e6e84c2010-01-25 20:05:08 +0000780 This function writes the specified number of bytes to the file at the current
781 file position. The current file position is advanced the actual number of bytes
782 written, which is returned in BufferSize. Partial writes only occur when there
783 has been a data error during the write attempt (such as "volume space full").
784 The file is automatically grown to hold the data if required. Direct writes to
jcarsey94b17fa2009-05-07 18:46:18 +0000785 opened directories are not supported.
786
787 @param FileHandle The opened file for writing
788 @param BufferSize on input the number of bytes in Buffer. On output
789 the number of bytes written.
790 @param Buffer the buffer containing data to write is stored.
791
792 @retval EFI_SUCCESS Data was written.
793 @retval EFI_UNSUPPORTED Writes to an open directory are not supported.
794 @retval EFI_NO_MEDIA The device has no media.
795 @retval EFI_DEVICE_ERROR The device reported an error.
796 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
797 @retval EFI_WRITE_PROTECTED The device is write-protected.
798 @retval EFI_ACCESS_DENIED The file was open for read only.
799 @retval EFI_VOLUME_FULL The volume is full.
800**/
801EFI_STATUS
802EFIAPI
803ShellWriteFile(
jcarsey252d9452011-03-25 20:49:53 +0000804 IN SHELL_FILE_HANDLE FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000805 IN OUT UINTN *BufferSize,
806 IN VOID *Buffer
jcarseya405b862010-09-14 05:18:09 +0000807 )
808{
jcarseyd2b45642009-05-11 18:02:16 +0000809 return (FileFunctionMap.WriteFile(FileHandle, BufferSize, Buffer));
jcarsey94b17fa2009-05-07 18:46:18 +0000810}
811
jcarsey1e6e84c2010-01-25 20:05:08 +0000812/**
jcarsey94b17fa2009-05-07 18:46:18 +0000813 Close an open file handle.
814
jcarsey1e6e84c2010-01-25 20:05:08 +0000815 This function closes a specified file handle. All "dirty" cached file data is
816 flushed to the device, and the file is closed. In all cases the handle is
jcarsey94b17fa2009-05-07 18:46:18 +0000817 closed.
818
819@param FileHandle the file handle to close.
820
821@retval EFI_SUCCESS the file handle was closed sucessfully.
822**/
823EFI_STATUS
824EFIAPI
825ShellCloseFile (
jcarseya405b862010-09-14 05:18:09 +0000826 IN SHELL_FILE_HANDLE *FileHandle
827 )
828{
jcarseyd2b45642009-05-11 18:02:16 +0000829 return (FileFunctionMap.CloseFile(*FileHandle));
jcarsey94b17fa2009-05-07 18:46:18 +0000830}
831
832/**
833 Delete a file and close the handle
834
835 This function closes and deletes a file. In all cases the file handle is closed.
jcarsey1e6e84c2010-01-25 20:05:08 +0000836 If the file cannot be deleted, the warning code EFI_WARN_DELETE_FAILURE is
jcarsey94b17fa2009-05-07 18:46:18 +0000837 returned, but the handle is still closed.
838
839 @param FileHandle the file handle to delete
840
841 @retval EFI_SUCCESS the file was closed sucessfully
jcarsey1e6e84c2010-01-25 20:05:08 +0000842 @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not
jcarsey94b17fa2009-05-07 18:46:18 +0000843 deleted
844 @retval INVALID_PARAMETER One of the parameters has an invalid value.
845**/
846EFI_STATUS
847EFIAPI
848ShellDeleteFile (
jcarseya405b862010-09-14 05:18:09 +0000849 IN SHELL_FILE_HANDLE *FileHandle
850 )
851{
jcarseyd2b45642009-05-11 18:02:16 +0000852 return (FileFunctionMap.DeleteFile(*FileHandle));
jcarsey94b17fa2009-05-07 18:46:18 +0000853}
854
855/**
856 Set the current position in a file.
857
jcarsey1e6e84c2010-01-25 20:05:08 +0000858 This function sets the current file position for the handle to the position
jcarsey94b17fa2009-05-07 18:46:18 +0000859 supplied. With the exception of seeking to position 0xFFFFFFFFFFFFFFFF, only
jcarsey1e6e84c2010-01-25 20:05:08 +0000860 absolute positioning is supported, and seeking past the end of the file is
861 allowed (a subsequent write would grow the file). Seeking to position
jcarsey94b17fa2009-05-07 18:46:18 +0000862 0xFFFFFFFFFFFFFFFF causes the current position to be set to the end of the file.
jcarsey1e6e84c2010-01-25 20:05:08 +0000863 If FileHandle is a directory, the only position that may be set is zero. This
jcarsey94b17fa2009-05-07 18:46:18 +0000864 has the effect of starting the read process of the directory entries over.
865
866 @param FileHandle The file handle on which the position is being set
867 @param Position Byte position from begining of file
868
869 @retval EFI_SUCCESS Operation completed sucessfully.
jcarsey1e6e84c2010-01-25 20:05:08 +0000870 @retval EFI_UNSUPPORTED the seek request for non-zero is not valid on
jcarsey94b17fa2009-05-07 18:46:18 +0000871 directories.
872 @retval INVALID_PARAMETER One of the parameters has an invalid value.
873**/
874EFI_STATUS
875EFIAPI
876ShellSetFilePosition (
jcarseya405b862010-09-14 05:18:09 +0000877 IN SHELL_FILE_HANDLE FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000878 IN UINT64 Position
jcarseya405b862010-09-14 05:18:09 +0000879 )
880{
jcarseyd2b45642009-05-11 18:02:16 +0000881 return (FileFunctionMap.SetFilePosition(FileHandle, Position));
jcarsey94b17fa2009-05-07 18:46:18 +0000882}
883
jcarsey1e6e84c2010-01-25 20:05:08 +0000884/**
jcarsey94b17fa2009-05-07 18:46:18 +0000885 Gets a file's current position
886
jcarsey1e6e84c2010-01-25 20:05:08 +0000887 This function retrieves the current file position for the file handle. For
888 directories, the current file position has no meaning outside of the file
jcarsey94b17fa2009-05-07 18:46:18 +0000889 system driver and as such the operation is not supported. An error is returned
890 if FileHandle is a directory.
891
892 @param FileHandle The open file handle on which to get the position.
893 @param Position Byte position from begining of file.
894
895 @retval EFI_SUCCESS the operation completed sucessfully.
896 @retval INVALID_PARAMETER One of the parameters has an invalid value.
897 @retval EFI_UNSUPPORTED the request is not valid on directories.
898**/
899EFI_STATUS
900EFIAPI
901ShellGetFilePosition (
jcarseya405b862010-09-14 05:18:09 +0000902 IN SHELL_FILE_HANDLE FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000903 OUT UINT64 *Position
jcarseya405b862010-09-14 05:18:09 +0000904 )
905{
jcarseyd2b45642009-05-11 18:02:16 +0000906 return (FileFunctionMap.GetFilePosition(FileHandle, Position));
jcarsey94b17fa2009-05-07 18:46:18 +0000907}
908/**
909 Flushes data on a file
jcarsey1e6e84c2010-01-25 20:05:08 +0000910
jcarsey94b17fa2009-05-07 18:46:18 +0000911 This function flushes all modified data associated with a file to a device.
912
913 @param FileHandle The file handle on which to flush data
914
915 @retval EFI_SUCCESS The data was flushed.
916 @retval EFI_NO_MEDIA The device has no media.
917 @retval EFI_DEVICE_ERROR The device reported an error.
918 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
919 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
920 @retval EFI_ACCESS_DENIED The file was opened for read only.
921**/
922EFI_STATUS
923EFIAPI
924ShellFlushFile (
jcarseya405b862010-09-14 05:18:09 +0000925 IN SHELL_FILE_HANDLE FileHandle
926 )
927{
jcarseyd2b45642009-05-11 18:02:16 +0000928 return (FileFunctionMap.FlushFile(FileHandle));
jcarsey94b17fa2009-05-07 18:46:18 +0000929}
930
931/**
932 Retrieves the first file from a directory
933
jcarsey1e6e84c2010-01-25 20:05:08 +0000934 This function opens a directory and gets the first file's info in the
935 directory. Caller can use ShellFindNextFile() to get other files. When
jcarsey94b17fa2009-05-07 18:46:18 +0000936 complete the caller is responsible for calling FreePool() on Buffer.
937
938 @param DirHandle The file handle of the directory to search
939 @param Buffer Pointer to buffer for file's information
940
941 @retval EFI_SUCCESS Found the first file.
942 @retval EFI_NOT_FOUND Cannot find the directory.
943 @retval EFI_NO_MEDIA The device has no media.
944 @retval EFI_DEVICE_ERROR The device reported an error.
945 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
946 @return Others status of ShellGetFileInfo, ShellSetFilePosition,
947 or ShellReadFile
948**/
949EFI_STATUS
950EFIAPI
951ShellFindFirstFile (
jcarseya405b862010-09-14 05:18:09 +0000952 IN SHELL_FILE_HANDLE DirHandle,
jcarseyd2b45642009-05-11 18:02:16 +0000953 OUT EFI_FILE_INFO **Buffer
jcarseya405b862010-09-14 05:18:09 +0000954 )
955{
jcarsey94b17fa2009-05-07 18:46:18 +0000956 //
jcarseyd2b45642009-05-11 18:02:16 +0000957 // pass to file handle lib
jcarsey94b17fa2009-05-07 18:46:18 +0000958 //
jcarseyd2b45642009-05-11 18:02:16 +0000959 return (FileHandleFindFirstFile(DirHandle, Buffer));
jcarsey94b17fa2009-05-07 18:46:18 +0000960}
961/**
962 Retrieves the next file in a directory.
963
jcarseya405b862010-09-14 05:18:09 +0000964 To use this function, caller must call the ShellFindFirstFile() to get the
jcarsey1e6e84c2010-01-25 20:05:08 +0000965 first file, and then use this function get other files. This function can be
966 called for several times to get each file's information in the directory. If
967 the call of ShellFindNextFile() got the last file in the directory, the next
968 call of this function has no file to get. *NoFile will be set to TRUE and the
969 Buffer memory will be automatically freed.
jcarsey94b17fa2009-05-07 18:46:18 +0000970
971 @param DirHandle the file handle of the directory
972 @param Buffer pointer to buffer for file's information
973 @param NoFile pointer to boolean when last file is found
974
975 @retval EFI_SUCCESS Found the next file, or reached last file
976 @retval EFI_NO_MEDIA The device has no media.
977 @retval EFI_DEVICE_ERROR The device reported an error.
978 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
979**/
980EFI_STATUS
981EFIAPI
982ShellFindNextFile(
jcarseya405b862010-09-14 05:18:09 +0000983 IN SHELL_FILE_HANDLE DirHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000984 OUT EFI_FILE_INFO *Buffer,
985 OUT BOOLEAN *NoFile
jcarseya405b862010-09-14 05:18:09 +0000986 )
987{
jcarsey94b17fa2009-05-07 18:46:18 +0000988 //
jcarseyd2b45642009-05-11 18:02:16 +0000989 // pass to file handle lib
jcarsey94b17fa2009-05-07 18:46:18 +0000990 //
jcarseyd2b45642009-05-11 18:02:16 +0000991 return (FileHandleFindNextFile(DirHandle, Buffer, NoFile));
jcarsey94b17fa2009-05-07 18:46:18 +0000992}
993/**
994 Retrieve the size of a file.
995
996 if FileHandle is NULL then ASSERT()
997 if Size is NULL then ASSERT()
998
jcarsey1e6e84c2010-01-25 20:05:08 +0000999 This function extracts the file size info from the FileHandle's EFI_FILE_INFO
jcarsey94b17fa2009-05-07 18:46:18 +00001000 data.
1001
1002 @param FileHandle file handle from which size is retrieved
1003 @param Size pointer to size
1004
1005 @retval EFI_SUCCESS operation was completed sucessfully
1006 @retval EFI_DEVICE_ERROR cannot access the file
1007**/
1008EFI_STATUS
1009EFIAPI
1010ShellGetFileSize (
jcarseya405b862010-09-14 05:18:09 +00001011 IN SHELL_FILE_HANDLE FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +00001012 OUT UINT64 *Size
jcarseya405b862010-09-14 05:18:09 +00001013 )
1014{
jcarseyd2b45642009-05-11 18:02:16 +00001015 return (FileFunctionMap.GetFileSize(FileHandle, Size));
jcarsey94b17fa2009-05-07 18:46:18 +00001016}
1017/**
1018 Retrieves the status of the break execution flag
1019
1020 this function is useful to check whether the application is being asked to halt by the shell.
1021
1022 @retval TRUE the execution break is enabled
1023 @retval FALSE the execution break is not enabled
1024**/
1025BOOLEAN
1026EFIAPI
1027ShellGetExecutionBreakFlag(
1028 VOID
1029 )
1030{
jcarsey1e6e84c2010-01-25 20:05:08 +00001031 //
jcarsey94b17fa2009-05-07 18:46:18 +00001032 // Check for UEFI Shell 2.0 protocols
1033 //
jcarsey366f81a2011-06-27 21:04:22 +00001034 if (gEfiShellProtocol != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00001035
1036 //
1037 // We are using UEFI Shell 2.0; see if the event has been triggered
1038 //
jcarsey366f81a2011-06-27 21:04:22 +00001039 if (gBS->CheckEvent(gEfiShellProtocol->ExecutionBreak) != EFI_SUCCESS) {
jcarsey94b17fa2009-05-07 18:46:18 +00001040 return (FALSE);
1041 }
1042 return (TRUE);
jcarsey1e6e84c2010-01-25 20:05:08 +00001043 }
jcarsey94b17fa2009-05-07 18:46:18 +00001044
1045 //
1046 // using EFI Shell; call the function to check
1047 //
jcarsey92a54472011-06-27 20:33:13 +00001048 if (mEfiShellEnvironment2 != NULL) {
1049 return (mEfiShellEnvironment2->GetExecutionBreak());
1050 }
1051
1052 return (FALSE);
jcarsey94b17fa2009-05-07 18:46:18 +00001053}
1054/**
1055 return the value of an environment variable
1056
jcarsey1e6e84c2010-01-25 20:05:08 +00001057 this function gets the value of the environment variable set by the
jcarsey94b17fa2009-05-07 18:46:18 +00001058 ShellSetEnvironmentVariable function
1059
1060 @param EnvKey The key name of the environment variable.
1061
1062 @retval NULL the named environment variable does not exist.
1063 @return != NULL pointer to the value of the environment variable
1064**/
1065CONST CHAR16*
1066EFIAPI
1067ShellGetEnvironmentVariable (
jcarsey9b3bf082009-06-23 21:15:07 +00001068 IN CONST CHAR16 *EnvKey
jcarsey94b17fa2009-05-07 18:46:18 +00001069 )
1070{
jcarsey1e6e84c2010-01-25 20:05:08 +00001071 //
jcarsey94b17fa2009-05-07 18:46:18 +00001072 // Check for UEFI Shell 2.0 protocols
1073 //
jcarsey366f81a2011-06-27 21:04:22 +00001074 if (gEfiShellProtocol != NULL) {
1075 return (gEfiShellProtocol->GetEnv(EnvKey));
jcarsey94b17fa2009-05-07 18:46:18 +00001076 }
1077
1078 //
jcarsey92a54472011-06-27 20:33:13 +00001079 // Check for EFI shell
jcarsey94b17fa2009-05-07 18:46:18 +00001080 //
jcarsey92a54472011-06-27 20:33:13 +00001081 if (mEfiShellEnvironment2 != NULL) {
1082 return (mEfiShellEnvironment2->GetEnv((CHAR16*)EnvKey));
1083 }
jcarsey94b17fa2009-05-07 18:46:18 +00001084
jcarsey92a54472011-06-27 20:33:13 +00001085 return NULL;
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 //
jcarsey366f81a2011-06-27 21:04:22 +00001118 if (gEfiShellProtocol != NULL) {
1119 return (gEfiShellProtocol->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 //
jcarsey366f81a2011-06-27 21:04:22 +00001171 if (gEfiShellProtocol != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00001172 //
1173 // Call UEFI Shell 2.0 version (not using Output parameter)
1174 //
jcarsey366f81a2011-06-27 21:04:22 +00001175 return (gEfiShellProtocol->Execute(ParentHandle,
jcarsey94b17fa2009-05-07 18:46:18 +00001176 CommandLine,
1177 EnvironmentVariables,
1178 Status));
jcarsey1e6e84c2010-01-25 20:05:08 +00001179 }
jcarsey92a54472011-06-27 20:33:13 +00001180
jcarsey94b17fa2009-05-07 18:46:18 +00001181 //
jcarsey92a54472011-06-27 20:33:13 +00001182 // Check for EFI shell
jcarsey94b17fa2009-05-07 18:46:18 +00001183 //
jcarsey92a54472011-06-27 20:33:13 +00001184 if (mEfiShellEnvironment2 != NULL) {
1185 //
1186 // Call EFI Shell version (not using EnvironmentVariables or Status parameters)
1187 // Due to oddity in the EFI shell we want to dereference the ParentHandle here
1188 //
1189 return (mEfiShellEnvironment2->Execute(*ParentHandle,
1190 CommandLine,
1191 Output));
1192 }
1193
1194 return (EFI_UNSUPPORTED);
jcarsey94b17fa2009-05-07 18:46:18 +00001195}
1196/**
1197 Retreives the current directory path
1198
jcarsey1e6e84c2010-01-25 20:05:08 +00001199 If the DeviceName is NULL, it returns the current device's current directory
1200 name. If the DeviceName is not NULL, it returns the current directory name
jcarsey94b17fa2009-05-07 18:46:18 +00001201 on specified drive.
1202
1203 @param DeviceName the name of the drive to get directory on
1204
1205 @retval NULL the directory does not exist
1206 @return != NULL the directory
1207**/
1208CONST CHAR16*
1209EFIAPI
1210ShellGetCurrentDir (
jcarseya405b862010-09-14 05:18:09 +00001211 IN CHAR16 * CONST DeviceName OPTIONAL
jcarsey94b17fa2009-05-07 18:46:18 +00001212 )
1213{
jcarsey1e6e84c2010-01-25 20:05:08 +00001214 //
jcarsey94b17fa2009-05-07 18:46:18 +00001215 // Check for UEFI Shell 2.0 protocols
1216 //
jcarsey366f81a2011-06-27 21:04:22 +00001217 if (gEfiShellProtocol != NULL) {
1218 return (gEfiShellProtocol->GetCurDir(DeviceName));
jcarsey1e6e84c2010-01-25 20:05:08 +00001219 }
jcarsey92a54472011-06-27 20:33:13 +00001220
jcarsey94b17fa2009-05-07 18:46:18 +00001221 //
jcarsey8bd282b2011-06-27 16:45:41 +00001222 // Check for EFI shell
jcarsey94b17fa2009-05-07 18:46:18 +00001223 //
jcarsey8bd282b2011-06-27 16:45:41 +00001224 if (mEfiShellEnvironment2 != NULL) {
1225 return (mEfiShellEnvironment2->CurDir(DeviceName));
1226 }
1227
1228 return (NULL);
jcarsey94b17fa2009-05-07 18:46:18 +00001229}
1230/**
1231 sets (enabled or disabled) the page break mode
1232
jcarsey1e6e84c2010-01-25 20:05:08 +00001233 when page break mode is enabled the screen will stop scrolling
jcarsey94b17fa2009-05-07 18:46:18 +00001234 and wait for operator input before scrolling a subsequent screen.
1235
1236 @param CurrentState TRUE to enable and FALSE to disable
1237**/
jcarsey1e6e84c2010-01-25 20:05:08 +00001238VOID
jcarsey94b17fa2009-05-07 18:46:18 +00001239EFIAPI
1240ShellSetPageBreakMode (
1241 IN BOOLEAN CurrentState
1242 )
1243{
1244 //
1245 // check for enabling
1246 //
1247 if (CurrentState != 0x00) {
jcarsey1e6e84c2010-01-25 20:05:08 +00001248 //
jcarsey94b17fa2009-05-07 18:46:18 +00001249 // check for UEFI Shell 2.0
1250 //
jcarsey366f81a2011-06-27 21:04:22 +00001251 if (gEfiShellProtocol != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00001252 //
1253 // Enable with UEFI 2.0 Shell
1254 //
jcarsey366f81a2011-06-27 21:04:22 +00001255 gEfiShellProtocol->EnablePageBreak();
jcarsey94b17fa2009-05-07 18:46:18 +00001256 return;
1257 } else {
jcarsey1e6e84c2010-01-25 20:05:08 +00001258 //
jcarsey92a54472011-06-27 20:33:13 +00001259 // Check for EFI shell
jcarsey94b17fa2009-05-07 18:46:18 +00001260 //
jcarsey92a54472011-06-27 20:33:13 +00001261 if (mEfiShellEnvironment2 != NULL) {
1262 //
1263 // Enable with EFI Shell
1264 //
1265 mEfiShellEnvironment2->EnablePageBreak (DEFAULT_INIT_ROW, DEFAULT_AUTO_LF);
1266 return;
1267 }
jcarsey94b17fa2009-05-07 18:46:18 +00001268 }
1269 } else {
jcarsey1e6e84c2010-01-25 20:05:08 +00001270 //
jcarsey94b17fa2009-05-07 18:46:18 +00001271 // check for UEFI Shell 2.0
1272 //
jcarsey366f81a2011-06-27 21:04:22 +00001273 if (gEfiShellProtocol != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00001274 //
1275 // Disable with UEFI 2.0 Shell
1276 //
jcarsey366f81a2011-06-27 21:04:22 +00001277 gEfiShellProtocol->DisablePageBreak();
jcarsey94b17fa2009-05-07 18:46:18 +00001278 return;
1279 } else {
jcarsey1e6e84c2010-01-25 20:05:08 +00001280 //
jcarsey92a54472011-06-27 20:33:13 +00001281 // Check for EFI shell
jcarsey94b17fa2009-05-07 18:46:18 +00001282 //
jcarsey92a54472011-06-27 20:33:13 +00001283 if (mEfiShellEnvironment2 != NULL) {
1284 //
1285 // Disable with EFI Shell
1286 //
1287 mEfiShellEnvironment2->DisablePageBreak ();
1288 return;
1289 }
jcarsey94b17fa2009-05-07 18:46:18 +00001290 }
1291 }
1292}
1293
1294///
1295/// version of EFI_SHELL_FILE_INFO struct, except has no CONST pointers.
1296/// This allows for the struct to be populated.
1297///
1298typedef struct {
jcarseyd2b45642009-05-11 18:02:16 +00001299 LIST_ENTRY Link;
jcarsey94b17fa2009-05-07 18:46:18 +00001300 EFI_STATUS Status;
1301 CHAR16 *FullName;
1302 CHAR16 *FileName;
jcarseya405b862010-09-14 05:18:09 +00001303 SHELL_FILE_HANDLE Handle;
jcarsey94b17fa2009-05-07 18:46:18 +00001304 EFI_FILE_INFO *Info;
1305} EFI_SHELL_FILE_INFO_NO_CONST;
1306
1307/**
1308 Converts a EFI shell list of structures to the coresponding UEFI Shell 2.0 type of list.
1309
1310 if OldStyleFileList is NULL then ASSERT()
1311
jcarsey1e6e84c2010-01-25 20:05:08 +00001312 this function will convert a SHELL_FILE_ARG based list into a callee allocated
jcarsey94b17fa2009-05-07 18:46:18 +00001313 EFI_SHELL_FILE_INFO based list. it is up to the caller to free the memory via
1314 the ShellCloseFileMetaArg function.
1315
jcarseya405b862010-09-14 05:18:09 +00001316 @param[in] FileList the EFI shell list type
jcarseyb82bfcc2009-06-29 16:28:23 +00001317 @param[in,out] ListHead the list to add to
jcarsey94b17fa2009-05-07 18:46:18 +00001318
1319 @retval the resultant head of the double linked new format list;
1320**/
1321LIST_ENTRY*
1322EFIAPI
1323InternalShellConvertFileListType (
jcarsey9b3bf082009-06-23 21:15:07 +00001324 IN LIST_ENTRY *FileList,
1325 IN OUT LIST_ENTRY *ListHead
jcarsey125c2cf2009-11-18 21:36:50 +00001326 )
1327{
jcarsey94b17fa2009-05-07 18:46:18 +00001328 SHELL_FILE_ARG *OldInfo;
jcarsey9b3bf082009-06-23 21:15:07 +00001329 LIST_ENTRY *Link;
jcarsey94b17fa2009-05-07 18:46:18 +00001330 EFI_SHELL_FILE_INFO_NO_CONST *NewInfo;
1331
1332 //
jcarsey9b3bf082009-06-23 21:15:07 +00001333 // ASSERTs
jcarsey94b17fa2009-05-07 18:46:18 +00001334 //
jcarsey9b3bf082009-06-23 21:15:07 +00001335 ASSERT(FileList != NULL);
1336 ASSERT(ListHead != NULL);
jcarsey94b17fa2009-05-07 18:46:18 +00001337
1338 //
1339 // enumerate through each member of the old list and copy
1340 //
jcarseyd2b45642009-05-11 18:02:16 +00001341 for (Link = FileList->ForwardLink; Link != FileList; Link = Link->ForwardLink) {
jcarsey94b17fa2009-05-07 18:46:18 +00001342 OldInfo = CR (Link, SHELL_FILE_ARG, Link, SHELL_FILE_ARG_SIGNATURE);
jcarseya405b862010-09-14 05:18:09 +00001343 ASSERT(OldInfo != NULL);
1344
1345 //
1346 // Skip ones that failed to open...
1347 //
1348 if (OldInfo->Status != EFI_SUCCESS) {
1349 continue;
1350 }
jcarsey94b17fa2009-05-07 18:46:18 +00001351
1352 //
1353 // make sure the old list was valid
1354 //
jcarsey94b17fa2009-05-07 18:46:18 +00001355 ASSERT(OldInfo->Info != NULL);
1356 ASSERT(OldInfo->FullName != NULL);
1357 ASSERT(OldInfo->FileName != NULL);
1358
1359 //
1360 // allocate a new EFI_SHELL_FILE_INFO object
1361 //
1362 NewInfo = AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));
jcarseyc9d92df2010-02-03 15:37:54 +00001363 ASSERT(NewInfo != NULL);
1364 if (NewInfo == NULL) {
1365 break;
1366 }
jcarsey1e6e84c2010-01-25 20:05:08 +00001367
1368 //
jcarsey94b17fa2009-05-07 18:46:18 +00001369 // copy the simple items
1370 //
1371 NewInfo->Handle = OldInfo->Handle;
1372 NewInfo->Status = OldInfo->Status;
1373
jcarseyd2b45642009-05-11 18:02:16 +00001374 // old shell checks for 0 not NULL
1375 OldInfo->Handle = 0;
1376
jcarsey94b17fa2009-05-07 18:46:18 +00001377 //
1378 // allocate new space to copy strings and structure
1379 //
1380 NewInfo->FullName = AllocateZeroPool(StrSize(OldInfo->FullName));
1381 NewInfo->FileName = AllocateZeroPool(StrSize(OldInfo->FileName));
1382 NewInfo->Info = AllocateZeroPool((UINTN)OldInfo->Info->Size);
jcarsey1e6e84c2010-01-25 20:05:08 +00001383
jcarsey94b17fa2009-05-07 18:46:18 +00001384 //
1385 // make sure all the memory allocations were sucessful
1386 //
1387 ASSERT(NewInfo->FullName != NULL);
1388 ASSERT(NewInfo->FileName != NULL);
1389 ASSERT(NewInfo->Info != NULL);
1390
1391 //
1392 // Copt the strings and structure
1393 //
1394 StrCpy(NewInfo->FullName, OldInfo->FullName);
1395 StrCpy(NewInfo->FileName, OldInfo->FileName);
1396 gBS->CopyMem (NewInfo->Info, OldInfo->Info, (UINTN)OldInfo->Info->Size);
1397
1398 //
1399 // add that to the list
1400 //
jcarsey9b3bf082009-06-23 21:15:07 +00001401 InsertTailList(ListHead, &NewInfo->Link);
jcarsey94b17fa2009-05-07 18:46:18 +00001402 }
1403 return (ListHead);
1404}
1405/**
1406 Opens a group of files based on a path.
1407
jcarsey1e6e84c2010-01-25 20:05:08 +00001408 This function uses the Arg to open all the matching files. Each matched
1409 file has a SHELL_FILE_ARG structure to record the file information. These
1410 structures are placed on the list ListHead. Users can get the SHELL_FILE_ARG
jcarsey94b17fa2009-05-07 18:46:18 +00001411 structures from ListHead to access each file. This function supports wildcards
jcarsey1e6e84c2010-01-25 20:05:08 +00001412 and will process '?' and '*' as such. the list must be freed with a call to
jcarsey94b17fa2009-05-07 18:46:18 +00001413 ShellCloseFileMetaArg().
1414
jcarsey1e6e84c2010-01-25 20:05:08 +00001415 If you are NOT appending to an existing list *ListHead must be NULL. If
jcarsey5f7431d2009-07-10 18:06:01 +00001416 *ListHead is NULL then it must be callee freed.
jcarsey94b17fa2009-05-07 18:46:18 +00001417
1418 @param Arg pointer to path string
1419 @param OpenMode mode to open files with
1420 @param ListHead head of linked list of results
1421
jcarsey1e6e84c2010-01-25 20:05:08 +00001422 @retval EFI_SUCCESS the operation was sucessful and the list head
jcarsey94b17fa2009-05-07 18:46:18 +00001423 contains the list of opened files
jcarsey94b17fa2009-05-07 18:46:18 +00001424 @return != EFI_SUCCESS the operation failed
1425
1426 @sa InternalShellConvertFileListType
1427**/
1428EFI_STATUS
1429EFIAPI
1430ShellOpenFileMetaArg (
1431 IN CHAR16 *Arg,
1432 IN UINT64 OpenMode,
1433 IN OUT EFI_SHELL_FILE_INFO **ListHead
1434 )
1435{
1436 EFI_STATUS Status;
jcarsey9b3bf082009-06-23 21:15:07 +00001437 LIST_ENTRY mOldStyleFileList;
jcarsey1e6e84c2010-01-25 20:05:08 +00001438
jcarsey94b17fa2009-05-07 18:46:18 +00001439 //
1440 // ASSERT that Arg and ListHead are not NULL
1441 //
1442 ASSERT(Arg != NULL);
1443 ASSERT(ListHead != NULL);
1444
jcarsey1e6e84c2010-01-25 20:05:08 +00001445 //
jcarsey94b17fa2009-05-07 18:46:18 +00001446 // Check for UEFI Shell 2.0 protocols
1447 //
jcarsey366f81a2011-06-27 21:04:22 +00001448 if (gEfiShellProtocol != NULL) {
jcarsey5f7431d2009-07-10 18:06:01 +00001449 if (*ListHead == NULL) {
1450 *ListHead = (EFI_SHELL_FILE_INFO*)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));
1451 if (*ListHead == NULL) {
1452 return (EFI_OUT_OF_RESOURCES);
1453 }
1454 InitializeListHead(&((*ListHead)->Link));
jcarsey1e6e84c2010-01-25 20:05:08 +00001455 }
jcarsey366f81a2011-06-27 21:04:22 +00001456 Status = gEfiShellProtocol->OpenFileList(Arg,
jcarsey1e6e84c2010-01-25 20:05:08 +00001457 OpenMode,
jcarsey2247dde2009-11-09 18:08:58 +00001458 ListHead);
1459 if (EFI_ERROR(Status)) {
jcarsey366f81a2011-06-27 21:04:22 +00001460 gEfiShellProtocol->RemoveDupInFileList(ListHead);
jcarsey2247dde2009-11-09 18:08:58 +00001461 } else {
jcarsey366f81a2011-06-27 21:04:22 +00001462 Status = gEfiShellProtocol->RemoveDupInFileList(ListHead);
jcarsey2247dde2009-11-09 18:08:58 +00001463 }
jcarseya405b862010-09-14 05:18:09 +00001464 if (*ListHead != NULL && IsListEmpty(&(*ListHead)->Link)) {
1465 FreePool(*ListHead);
1466 *ListHead = NULL;
1467 return (EFI_NOT_FOUND);
1468 }
jcarsey2247dde2009-11-09 18:08:58 +00001469 return (Status);
jcarsey1e6e84c2010-01-25 20:05:08 +00001470 }
jcarsey94b17fa2009-05-07 18:46:18 +00001471
1472 //
jcarsey92a54472011-06-27 20:33:13 +00001473 // Check for EFI shell
jcarsey94b17fa2009-05-07 18:46:18 +00001474 //
jcarsey92a54472011-06-27 20:33:13 +00001475 if (mEfiShellEnvironment2 != NULL) {
1476 //
1477 // make sure the list head is initialized
1478 //
1479 InitializeListHead(&mOldStyleFileList);
jcarsey94b17fa2009-05-07 18:46:18 +00001480
jcarsey92a54472011-06-27 20:33:13 +00001481 //
1482 // Get the EFI Shell list of files
1483 //
1484 Status = mEfiShellEnvironment2->FileMetaArg(Arg, &mOldStyleFileList);
1485 if (EFI_ERROR(Status)) {
1486 *ListHead = NULL;
1487 return (Status);
1488 }
jcarsey94b17fa2009-05-07 18:46:18 +00001489
jcarsey92a54472011-06-27 20:33:13 +00001490 if (*ListHead == NULL) {
1491 *ListHead = (EFI_SHELL_FILE_INFO *)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));
1492 if (*ListHead == NULL) {
1493 return (EFI_OUT_OF_RESOURCES);
1494 }
1495 InitializeListHead(&((*ListHead)->Link));
1496 }
1497
1498 //
1499 // Convert that to equivalent of UEFI Shell 2.0 structure
1500 //
1501 InternalShellConvertFileListType(&mOldStyleFileList, &(*ListHead)->Link);
1502
1503 //
1504 // Free the EFI Shell version that was converted.
1505 //
1506 mEfiShellEnvironment2->FreeFileList(&mOldStyleFileList);
1507
1508 if ((*ListHead)->Link.ForwardLink == (*ListHead)->Link.BackLink && (*ListHead)->Link.BackLink == &((*ListHead)->Link)) {
1509 FreePool(*ListHead);
1510 *ListHead = NULL;
1511 Status = EFI_NOT_FOUND;
1512 }
jcarsey94b17fa2009-05-07 18:46:18 +00001513 return (Status);
1514 }
1515
jcarsey92a54472011-06-27 20:33:13 +00001516 return (EFI_UNSUPPORTED);
jcarsey94b17fa2009-05-07 18:46:18 +00001517}
1518/**
jcarseya405b862010-09-14 05:18:09 +00001519 Free the linked list returned from ShellOpenFileMetaArg.
jcarsey94b17fa2009-05-07 18:46:18 +00001520
jcarseya405b862010-09-14 05:18:09 +00001521 if ListHead is NULL then ASSERT().
jcarsey94b17fa2009-05-07 18:46:18 +00001522
jcarseya405b862010-09-14 05:18:09 +00001523 @param ListHead the pointer to free.
jcarsey94b17fa2009-05-07 18:46:18 +00001524
jcarseya405b862010-09-14 05:18:09 +00001525 @retval EFI_SUCCESS the operation was sucessful.
jcarsey94b17fa2009-05-07 18:46:18 +00001526**/
1527EFI_STATUS
1528EFIAPI
1529ShellCloseFileMetaArg (
1530 IN OUT EFI_SHELL_FILE_INFO **ListHead
1531 )
1532{
1533 LIST_ENTRY *Node;
1534
1535 //
1536 // ASSERT that ListHead is not NULL
1537 //
1538 ASSERT(ListHead != NULL);
1539
jcarsey1e6e84c2010-01-25 20:05:08 +00001540 //
jcarsey94b17fa2009-05-07 18:46:18 +00001541 // Check for UEFI Shell 2.0 protocols
1542 //
jcarsey366f81a2011-06-27 21:04:22 +00001543 if (gEfiShellProtocol != NULL) {
1544 return (gEfiShellProtocol->FreeFileList(ListHead));
jcarsey92a54472011-06-27 20:33:13 +00001545 } else if (mEfiShellEnvironment2 != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00001546 //
jcarsey1e6e84c2010-01-25 20:05:08 +00001547 // Since this is EFI Shell version we need to free our internally made copy
jcarsey94b17fa2009-05-07 18:46:18 +00001548 // of the list
1549 //
jcarsey1e6e84c2010-01-25 20:05:08 +00001550 for ( Node = GetFirstNode(&(*ListHead)->Link)
jcarseya405b862010-09-14 05:18:09 +00001551 ; *ListHead != NULL && !IsListEmpty(&(*ListHead)->Link)
jcarsey9b3bf082009-06-23 21:15:07 +00001552 ; Node = GetFirstNode(&(*ListHead)->Link)) {
jcarsey94b17fa2009-05-07 18:46:18 +00001553 RemoveEntryList(Node);
jcarseya405b862010-09-14 05:18:09 +00001554 ((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 +00001555 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->FullName);
1556 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->FileName);
1557 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->Info);
1558 FreePool((EFI_SHELL_FILE_INFO_NO_CONST*)Node);
1559 }
1560 return EFI_SUCCESS;
1561 }
jcarsey92a54472011-06-27 20:33:13 +00001562
1563 return (EFI_UNSUPPORTED);
jcarsey94b17fa2009-05-07 18:46:18 +00001564}
1565
jcarsey125c2cf2009-11-18 21:36:50 +00001566/**
1567 Find a file by searching the CWD and then the path.
1568
jcarseyb3011f42010-01-11 21:49:04 +00001569 If FileName is NULL then ASSERT.
jcarsey125c2cf2009-11-18 21:36:50 +00001570
jcarseyb3011f42010-01-11 21:49:04 +00001571 If the return value is not NULL then the memory must be caller freed.
jcarsey125c2cf2009-11-18 21:36:50 +00001572
1573 @param FileName Filename string.
1574
1575 @retval NULL the file was not found
1576 @return !NULL the full path to the file.
1577**/
1578CHAR16 *
1579EFIAPI
1580ShellFindFilePath (
1581 IN CONST CHAR16 *FileName
1582 )
1583{
1584 CONST CHAR16 *Path;
jcarseya405b862010-09-14 05:18:09 +00001585 SHELL_FILE_HANDLE Handle;
jcarsey125c2cf2009-11-18 21:36:50 +00001586 EFI_STATUS Status;
1587 CHAR16 *RetVal;
1588 CHAR16 *TestPath;
1589 CONST CHAR16 *Walker;
jcarsey36a9d672009-11-20 21:13:41 +00001590 UINTN Size;
jcarsey1cd45e72010-01-29 15:07:44 +00001591 CHAR16 *TempChar;
jcarsey125c2cf2009-11-18 21:36:50 +00001592
1593 RetVal = NULL;
1594
jcarseya405b862010-09-14 05:18:09 +00001595 //
1596 // First make sure its not an absolute path.
1597 //
1598 Status = ShellOpenFileByName(FileName, &Handle, EFI_FILE_MODE_READ, 0);
1599 if (!EFI_ERROR(Status)){
1600 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {
1601 ASSERT(RetVal == NULL);
1602 RetVal = StrnCatGrow(&RetVal, NULL, FileName, 0);
1603 ShellCloseFile(&Handle);
1604 return (RetVal);
1605 } else {
1606 ShellCloseFile(&Handle);
1607 }
1608 }
1609
jcarsey125c2cf2009-11-18 21:36:50 +00001610 Path = ShellGetEnvironmentVariable(L"cwd");
1611 if (Path != NULL) {
jcarsey36a9d672009-11-20 21:13:41 +00001612 Size = StrSize(Path);
1613 Size += StrSize(FileName);
1614 TestPath = AllocateZeroPool(Size);
jcarseyc9d92df2010-02-03 15:37:54 +00001615 ASSERT(TestPath != NULL);
1616 if (TestPath == NULL) {
1617 return (NULL);
1618 }
jcarsey125c2cf2009-11-18 21:36:50 +00001619 StrCpy(TestPath, Path);
1620 StrCat(TestPath, FileName);
1621 Status = ShellOpenFileByName(TestPath, &Handle, EFI_FILE_MODE_READ, 0);
1622 if (!EFI_ERROR(Status)){
jcarseya405b862010-09-14 05:18:09 +00001623 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {
1624 ASSERT(RetVal == NULL);
1625 RetVal = StrnCatGrow(&RetVal, NULL, TestPath, 0);
1626 ShellCloseFile(&Handle);
1627 FreePool(TestPath);
1628 return (RetVal);
1629 } else {
1630 ShellCloseFile(&Handle);
1631 }
jcarsey125c2cf2009-11-18 21:36:50 +00001632 }
1633 FreePool(TestPath);
1634 }
1635 Path = ShellGetEnvironmentVariable(L"path");
1636 if (Path != NULL) {
jcarseya405b862010-09-14 05:18:09 +00001637 Size = StrSize(Path)+sizeof(CHAR16);
jcarsey36a9d672009-11-20 21:13:41 +00001638 Size += StrSize(FileName);
1639 TestPath = AllocateZeroPool(Size);
jcarsey3e082d52010-10-04 16:44:57 +00001640 if (TestPath == NULL) {
1641 return (NULL);
1642 }
jcarsey1e6e84c2010-01-25 20:05:08 +00001643 Walker = (CHAR16*)Path;
jcarsey125c2cf2009-11-18 21:36:50 +00001644 do {
1645 CopyMem(TestPath, Walker, StrSize(Walker));
jcarsey3e082d52010-10-04 16:44:57 +00001646 if (TestPath != NULL) {
1647 TempChar = StrStr(TestPath, L";");
1648 if (TempChar != NULL) {
1649 *TempChar = CHAR_NULL;
1650 }
1651 if (TestPath[StrLen(TestPath)-1] != L'\\') {
1652 StrCat(TestPath, L"\\");
1653 }
jcarsey89e85372011-04-13 23:37:50 +00001654 if (FileName[0] == L'\\') {
1655 FileName++;
1656 }
jcarsey3e082d52010-10-04 16:44:57 +00001657 StrCat(TestPath, FileName);
1658 if (StrStr(Walker, L";") != NULL) {
1659 Walker = StrStr(Walker, L";") + 1;
jcarseya405b862010-09-14 05:18:09 +00001660 } else {
jcarsey3e082d52010-10-04 16:44:57 +00001661 Walker = NULL;
1662 }
1663 Status = ShellOpenFileByName(TestPath, &Handle, EFI_FILE_MODE_READ, 0);
1664 if (!EFI_ERROR(Status)){
1665 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {
1666 ASSERT(RetVal == NULL);
1667 RetVal = StrnCatGrow(&RetVal, NULL, TestPath, 0);
1668 ShellCloseFile(&Handle);
1669 break;
1670 } else {
1671 ShellCloseFile(&Handle);
1672 }
jcarseya405b862010-09-14 05:18:09 +00001673 }
jcarsey125c2cf2009-11-18 21:36:50 +00001674 }
1675 } while (Walker != NULL && Walker[0] != CHAR_NULL);
1676 FreePool(TestPath);
1677 }
1678 return (RetVal);
1679}
1680
jcarseyb3011f42010-01-11 21:49:04 +00001681/**
jcarsey1e6e84c2010-01-25 20:05:08 +00001682 Find a file by searching the CWD and then the path with a variable set of file
1683 extensions. If the file is not found it will append each extension in the list
jcarseyb3011f42010-01-11 21:49:04 +00001684 in the order provided and return the first one that is successful.
1685
1686 If FileName is NULL, then ASSERT.
1687 If FileExtension is NULL, then behavior is identical to ShellFindFilePath.
1688
1689 If the return value is not NULL then the memory must be caller freed.
1690
1691 @param[in] FileName Filename string.
1692 @param[in] FileExtension Semi-colon delimeted list of possible extensions.
1693
1694 @retval NULL The file was not found.
1695 @retval !NULL The path to the file.
1696**/
1697CHAR16 *
1698EFIAPI
1699ShellFindFilePathEx (
1700 IN CONST CHAR16 *FileName,
1701 IN CONST CHAR16 *FileExtension
1702 )
1703{
1704 CHAR16 *TestPath;
1705 CHAR16 *RetVal;
1706 CONST CHAR16 *ExtensionWalker;
jcarsey9e926b62010-01-14 20:26:39 +00001707 UINTN Size;
jcarsey1cd45e72010-01-29 15:07:44 +00001708 CHAR16 *TempChar;
jcarseyc9d92df2010-02-03 15:37:54 +00001709 CHAR16 *TempChar2;
jcarsey1cd45e72010-01-29 15:07:44 +00001710
jcarseyb3011f42010-01-11 21:49:04 +00001711 ASSERT(FileName != NULL);
1712 if (FileExtension == NULL) {
1713 return (ShellFindFilePath(FileName));
1714 }
1715 RetVal = ShellFindFilePath(FileName);
1716 if (RetVal != NULL) {
1717 return (RetVal);
1718 }
jcarsey9e926b62010-01-14 20:26:39 +00001719 Size = StrSize(FileName);
1720 Size += StrSize(FileExtension);
1721 TestPath = AllocateZeroPool(Size);
jcarseyc9d92df2010-02-03 15:37:54 +00001722 ASSERT(TestPath != NULL);
1723 if (TestPath == NULL) {
1724 return (NULL);
1725 }
jcarseya405b862010-09-14 05:18:09 +00001726 for (ExtensionWalker = FileExtension, TempChar2 = (CHAR16*)FileExtension; TempChar2 != NULL ; ExtensionWalker = TempChar2 + 1){
jcarseyb3011f42010-01-11 21:49:04 +00001727 StrCpy(TestPath, FileName);
jcarseya405b862010-09-14 05:18:09 +00001728 if (ExtensionWalker != NULL) {
1729 StrCat(TestPath, ExtensionWalker);
1730 }
jcarsey1cd45e72010-01-29 15:07:44 +00001731 TempChar = StrStr(TestPath, L";");
1732 if (TempChar != NULL) {
1733 *TempChar = CHAR_NULL;
jcarseyb3011f42010-01-11 21:49:04 +00001734 }
1735 RetVal = ShellFindFilePath(TestPath);
1736 if (RetVal != NULL) {
1737 break;
1738 }
jcarseya405b862010-09-14 05:18:09 +00001739 ASSERT(ExtensionWalker != NULL);
jcarseyc9d92df2010-02-03 15:37:54 +00001740 TempChar2 = StrStr(ExtensionWalker, L";");
jcarseyb3011f42010-01-11 21:49:04 +00001741 }
1742 FreePool(TestPath);
1743 return (RetVal);
1744}
1745
jcarsey94b17fa2009-05-07 18:46:18 +00001746typedef struct {
jcarsey9b3bf082009-06-23 21:15:07 +00001747 LIST_ENTRY Link;
jcarsey94b17fa2009-05-07 18:46:18 +00001748 CHAR16 *Name;
jcarseya405b862010-09-14 05:18:09 +00001749 SHELL_PARAM_TYPE Type;
jcarsey94b17fa2009-05-07 18:46:18 +00001750 CHAR16 *Value;
1751 UINTN OriginalPosition;
1752} SHELL_PARAM_PACKAGE;
1753
1754/**
jcarsey1e6e84c2010-01-25 20:05:08 +00001755 Checks the list of valid arguments and returns TRUE if the item was found. If the
jcarsey94b17fa2009-05-07 18:46:18 +00001756 return value is TRUE then the type parameter is set also.
jcarsey1e6e84c2010-01-25 20:05:08 +00001757
jcarsey94b17fa2009-05-07 18:46:18 +00001758 if CheckList is NULL then ASSERT();
1759 if Name is NULL then ASSERT();
1760 if Type is NULL then ASSERT();
1761
jcarsey94b17fa2009-05-07 18:46:18 +00001762 @param Name pointer to Name of parameter found
1763 @param CheckList List to check against
jcarseya405b862010-09-14 05:18:09 +00001764 @param Type pointer to type of parameter if it was found
jcarsey94b17fa2009-05-07 18:46:18 +00001765
1766 @retval TRUE the Parameter was found. Type is valid.
1767 @retval FALSE the Parameter was not found. Type is not valid.
1768**/
1769BOOLEAN
1770EFIAPI
jcarseyd2b45642009-05-11 18:02:16 +00001771InternalIsOnCheckList (
jcarsey94b17fa2009-05-07 18:46:18 +00001772 IN CONST CHAR16 *Name,
1773 IN CONST SHELL_PARAM_ITEM *CheckList,
jcarsey252d9452011-03-25 20:49:53 +00001774 OUT SHELL_PARAM_TYPE *Type
jcarseya405b862010-09-14 05:18:09 +00001775 )
1776{
jcarsey94b17fa2009-05-07 18:46:18 +00001777 SHELL_PARAM_ITEM *TempListItem;
jcarsey252d9452011-03-25 20:49:53 +00001778 CHAR16 *TempString;
jcarsey94b17fa2009-05-07 18:46:18 +00001779
1780 //
1781 // ASSERT that all 3 pointer parameters aren't NULL
1782 //
1783 ASSERT(CheckList != NULL);
1784 ASSERT(Type != NULL);
1785 ASSERT(Name != NULL);
1786
1787 //
jcarseyd2b45642009-05-11 18:02:16 +00001788 // question mark and page break mode are always supported
1789 //
1790 if ((StrCmp(Name, L"-?") == 0) ||
1791 (StrCmp(Name, L"-b") == 0)
jcarseya405b862010-09-14 05:18:09 +00001792 ) {
jcarsey252d9452011-03-25 20:49:53 +00001793 *Type = TypeFlag;
jcarseyd2b45642009-05-11 18:02:16 +00001794 return (TRUE);
1795 }
1796
1797 //
jcarsey94b17fa2009-05-07 18:46:18 +00001798 // Enumerate through the list
1799 //
1800 for (TempListItem = (SHELL_PARAM_ITEM*)CheckList ; TempListItem->Name != NULL ; TempListItem++) {
1801 //
jcarsey9eb53ac2009-07-08 17:26:58 +00001802 // If the Type is TypeStart only check the first characters of the passed in param
1803 // If it matches set the type and return TRUE
jcarsey94b17fa2009-05-07 18:46:18 +00001804 //
jcarsey252d9452011-03-25 20:49:53 +00001805 if (TempListItem->Type == TypeStart) {
1806 if (StrnCmp(Name, TempListItem->Name, StrLen(TempListItem->Name)) == 0) {
1807 *Type = TempListItem->Type;
1808 return (TRUE);
1809 }
1810 TempString = NULL;
1811 TempString = StrnCatGrow(&TempString, NULL, Name, StrLen(TempListItem->Name));
1812 if (TempString != NULL) {
1813 if (StringNoCaseCompare(&TempString, &TempListItem->Name) == 0) {
1814 *Type = TempListItem->Type;
1815 FreePool(TempString);
1816 return (TRUE);
1817 }
1818 FreePool(TempString);
1819 }
1820 } else if (StringNoCaseCompare(&Name, &TempListItem->Name) == 0) {
jcarsey94b17fa2009-05-07 18:46:18 +00001821 *Type = TempListItem->Type;
1822 return (TRUE);
1823 }
1824 }
jcarsey2247dde2009-11-09 18:08:58 +00001825
jcarsey94b17fa2009-05-07 18:46:18 +00001826 return (FALSE);
1827}
1828/**
jcarseyd2b45642009-05-11 18:02:16 +00001829 Checks the string for indicators of "flag" status. this is a leading '/', '-', or '+'
jcarsey94b17fa2009-05-07 18:46:18 +00001830
jcarseya405b862010-09-14 05:18:09 +00001831 @param[in] Name pointer to Name of parameter found
1832 @param[in] AlwaysAllowNumbers TRUE to allow numbers, FALSE to not.
jcarsey94b17fa2009-05-07 18:46:18 +00001833
1834 @retval TRUE the Parameter is a flag.
jcarseya405b862010-09-14 05:18:09 +00001835 @retval FALSE the Parameter not a flag.
jcarsey94b17fa2009-05-07 18:46:18 +00001836**/
1837BOOLEAN
1838EFIAPI
jcarseyd2b45642009-05-11 18:02:16 +00001839InternalIsFlag (
jcarsey2247dde2009-11-09 18:08:58 +00001840 IN CONST CHAR16 *Name,
1841 IN BOOLEAN AlwaysAllowNumbers
jcarsey94b17fa2009-05-07 18:46:18 +00001842 )
1843{
1844 //
1845 // ASSERT that Name isn't NULL
1846 //
1847 ASSERT(Name != NULL);
1848
1849 //
jcarsey2247dde2009-11-09 18:08:58 +00001850 // If we accept numbers then dont return TRUE. (they will be values)
1851 //
jcarseya405b862010-09-14 05:18:09 +00001852 if (((Name[0] == L'-' || Name[0] == L'+') && ShellIsHexaDecimalDigitCharacter(Name[1])) && AlwaysAllowNumbers) {
jcarsey2247dde2009-11-09 18:08:58 +00001853 return (FALSE);
1854 }
1855
1856 //
jcarseya405b862010-09-14 05:18:09 +00001857 // If the Name has a /, +, or - as the first character return TRUE
jcarsey94b17fa2009-05-07 18:46:18 +00001858 //
jcarsey1e6e84c2010-01-25 20:05:08 +00001859 if ((Name[0] == L'/') ||
jcarseyd2b45642009-05-11 18:02:16 +00001860 (Name[0] == L'-') ||
1861 (Name[0] == L'+')
jcarseya405b862010-09-14 05:18:09 +00001862 ) {
jcarsey94b17fa2009-05-07 18:46:18 +00001863 return (TRUE);
1864 }
1865 return (FALSE);
1866}
1867
1868/**
jcarsey1e6e84c2010-01-25 20:05:08 +00001869 Checks the command line arguments passed against the list of valid ones.
jcarsey94b17fa2009-05-07 18:46:18 +00001870
1871 If no initialization is required, then return RETURN_SUCCESS.
jcarsey1e6e84c2010-01-25 20:05:08 +00001872
jcarseya405b862010-09-14 05:18:09 +00001873 @param[in] CheckList pointer to list of parameters to check
1874 @param[out] CheckPackage pointer to pointer to list checked values
1875 @param[out] ProblemParam optional pointer to pointer to unicode string for
jcarseyd2b45642009-05-11 18:02:16 +00001876 the paramater that caused failure. If used then the
1877 caller is responsible for freeing the memory.
jcarseya405b862010-09-14 05:18:09 +00001878 @param[in] AutoPageBreak will automatically set PageBreakEnabled for "b" parameter
1879 @param[in] Argv pointer to array of parameters
1880 @param[in] Argc Count of parameters in Argv
1881 @param[in] AlwaysAllowNumbers TRUE to allow numbers always, FALSE otherwise.
jcarsey94b17fa2009-05-07 18:46:18 +00001882
1883 @retval EFI_SUCCESS The operation completed sucessfully.
1884 @retval EFI_OUT_OF_RESOURCES A memory allocation failed
1885 @retval EFI_INVALID_PARAMETER A parameter was invalid
jcarsey1e6e84c2010-01-25 20:05:08 +00001886 @retval EFI_VOLUME_CORRUPTED the command line was corrupt. an argument was
1887 duplicated. the duplicated command line argument
jcarsey94b17fa2009-05-07 18:46:18 +00001888 was returned in ProblemParam if provided.
jcarsey1e6e84c2010-01-25 20:05:08 +00001889 @retval EFI_NOT_FOUND a argument required a value that was missing.
jcarsey94b17fa2009-05-07 18:46:18 +00001890 the invalid command line argument was returned in
1891 ProblemParam if provided.
1892**/
1893EFI_STATUS
1894EFIAPI
1895InternalCommandLineParse (
1896 IN CONST SHELL_PARAM_ITEM *CheckList,
1897 OUT LIST_ENTRY **CheckPackage,
1898 OUT CHAR16 **ProblemParam OPTIONAL,
1899 IN BOOLEAN AutoPageBreak,
1900 IN CONST CHAR16 **Argv,
jcarsey2247dde2009-11-09 18:08:58 +00001901 IN UINTN Argc,
1902 IN BOOLEAN AlwaysAllowNumbers
jcarseya405b862010-09-14 05:18:09 +00001903 )
1904{
jcarsey94b17fa2009-05-07 18:46:18 +00001905 UINTN LoopCounter;
jcarsey252d9452011-03-25 20:49:53 +00001906 SHELL_PARAM_TYPE CurrentItemType;
jcarsey94b17fa2009-05-07 18:46:18 +00001907 SHELL_PARAM_PACKAGE *CurrentItemPackage;
jcarsey125c2cf2009-11-18 21:36:50 +00001908 UINTN GetItemValue;
1909 UINTN ValueSize;
jcarseya405b862010-09-14 05:18:09 +00001910 UINTN Count;
jcarsey252d9452011-03-25 20:49:53 +00001911 CONST CHAR16 *TempPointer;
jcarsey94b17fa2009-05-07 18:46:18 +00001912
1913 CurrentItemPackage = NULL;
jcarsey125c2cf2009-11-18 21:36:50 +00001914 GetItemValue = 0;
1915 ValueSize = 0;
jcarseya405b862010-09-14 05:18:09 +00001916 Count = 0;
jcarsey94b17fa2009-05-07 18:46:18 +00001917
1918 //
1919 // If there is only 1 item we dont need to do anything
1920 //
jcarseya405b862010-09-14 05:18:09 +00001921 if (Argc < 1) {
jcarsey94b17fa2009-05-07 18:46:18 +00001922 *CheckPackage = NULL;
1923 return (EFI_SUCCESS);
1924 }
1925
1926 //
jcarsey2247dde2009-11-09 18:08:58 +00001927 // ASSERTs
1928 //
1929 ASSERT(CheckList != NULL);
1930 ASSERT(Argv != NULL);
1931
1932 //
jcarsey94b17fa2009-05-07 18:46:18 +00001933 // initialize the linked list
1934 //
1935 *CheckPackage = (LIST_ENTRY*)AllocateZeroPool(sizeof(LIST_ENTRY));
1936 InitializeListHead(*CheckPackage);
1937
1938 //
1939 // loop through each of the arguments
1940 //
1941 for (LoopCounter = 0 ; LoopCounter < Argc ; ++LoopCounter) {
1942 if (Argv[LoopCounter] == NULL) {
1943 //
1944 // do nothing for NULL argv
1945 //
jcarseya405b862010-09-14 05:18:09 +00001946 } else if (InternalIsOnCheckList(Argv[LoopCounter], CheckList, &CurrentItemType)) {
jcarsey94b17fa2009-05-07 18:46:18 +00001947 //
jcarsey2247dde2009-11-09 18:08:58 +00001948 // We might have leftover if last parameter didnt have optional value
1949 //
jcarsey125c2cf2009-11-18 21:36:50 +00001950 if (GetItemValue != 0) {
1951 GetItemValue = 0;
jcarsey2247dde2009-11-09 18:08:58 +00001952 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
1953 }
1954 //
jcarsey94b17fa2009-05-07 18:46:18 +00001955 // this is a flag
1956 //
jcarsey252d9452011-03-25 20:49:53 +00001957 CurrentItemPackage = AllocateZeroPool(sizeof(SHELL_PARAM_PACKAGE));
jcarsey94b17fa2009-05-07 18:46:18 +00001958 ASSERT(CurrentItemPackage != NULL);
jcarsey252d9452011-03-25 20:49:53 +00001959 CurrentItemPackage->Name = AllocateZeroPool(StrSize(Argv[LoopCounter]));
jcarsey94b17fa2009-05-07 18:46:18 +00001960 ASSERT(CurrentItemPackage->Name != NULL);
1961 StrCpy(CurrentItemPackage->Name, Argv[LoopCounter]);
1962 CurrentItemPackage->Type = CurrentItemType;
1963 CurrentItemPackage->OriginalPosition = (UINTN)(-1);
jcarseyb1f95a02009-06-16 00:23:19 +00001964 CurrentItemPackage->Value = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +00001965
1966 //
1967 // Does this flag require a value
1968 //
jcarsey125c2cf2009-11-18 21:36:50 +00001969 switch (CurrentItemPackage->Type) {
jcarsey94b17fa2009-05-07 18:46:18 +00001970 //
jcarsey125c2cf2009-11-18 21:36:50 +00001971 // possibly trigger the next loop(s) to populate the value of this item
jcarsey1e6e84c2010-01-25 20:05:08 +00001972 //
jcarsey125c2cf2009-11-18 21:36:50 +00001973 case TypeValue:
jcarsey1e6e84c2010-01-25 20:05:08 +00001974 GetItemValue = 1;
jcarsey125c2cf2009-11-18 21:36:50 +00001975 ValueSize = 0;
1976 break;
1977 case TypeDoubleValue:
1978 GetItemValue = 2;
1979 ValueSize = 0;
1980 break;
1981 case TypeMaxValue:
1982 GetItemValue = (UINTN)(-1);
1983 ValueSize = 0;
1984 break;
1985 default:
1986 //
1987 // this item has no value expected; we are done
1988 //
1989 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
1990 ASSERT(GetItemValue == 0);
1991 break;
jcarsey94b17fa2009-05-07 18:46:18 +00001992 }
jcarseya405b862010-09-14 05:18:09 +00001993 } else if (GetItemValue != 0 && !InternalIsFlag(Argv[LoopCounter], AlwaysAllowNumbers)) {
jcarseyb1f95a02009-06-16 00:23:19 +00001994 ASSERT(CurrentItemPackage != NULL);
1995 //
jcarsey125c2cf2009-11-18 21:36:50 +00001996 // get the item VALUE for a previous flag
jcarseyb1f95a02009-06-16 00:23:19 +00001997 //
jcarsey125c2cf2009-11-18 21:36:50 +00001998 CurrentItemPackage->Value = ReallocatePool(ValueSize, ValueSize + StrSize(Argv[LoopCounter]) + sizeof(CHAR16), CurrentItemPackage->Value);
jcarseyb1f95a02009-06-16 00:23:19 +00001999 ASSERT(CurrentItemPackage->Value != NULL);
jcarsey125c2cf2009-11-18 21:36:50 +00002000 if (ValueSize == 0) {
2001 StrCpy(CurrentItemPackage->Value, Argv[LoopCounter]);
2002 } else {
2003 StrCat(CurrentItemPackage->Value, L" ");
2004 StrCat(CurrentItemPackage->Value, Argv[LoopCounter]);
2005 }
2006 ValueSize += StrSize(Argv[LoopCounter]) + sizeof(CHAR16);
2007 GetItemValue--;
2008 if (GetItemValue == 0) {
2009 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
2010 }
jcarseya405b862010-09-14 05:18:09 +00002011 } else if (!InternalIsFlag(Argv[LoopCounter], AlwaysAllowNumbers) ){ //|| ProblemParam == NULL) {
jcarseyb1f95a02009-06-16 00:23:19 +00002012 //
2013 // add this one as a non-flag
2014 //
jcarsey252d9452011-03-25 20:49:53 +00002015
2016 TempPointer = Argv[LoopCounter];
2017 if ((*TempPointer == L'^' && *(TempPointer+1) == L'-')
2018 || (*TempPointer == L'^' && *(TempPointer+1) == L'/')
2019 || (*TempPointer == L'^' && *(TempPointer+1) == L'+')
2020 ){
2021 TempPointer++;
2022 }
2023 CurrentItemPackage = AllocateZeroPool(sizeof(SHELL_PARAM_PACKAGE));
jcarseyb1f95a02009-06-16 00:23:19 +00002024 ASSERT(CurrentItemPackage != NULL);
2025 CurrentItemPackage->Name = NULL;
2026 CurrentItemPackage->Type = TypePosition;
jcarsey252d9452011-03-25 20:49:53 +00002027 CurrentItemPackage->Value = AllocateZeroPool(StrSize(TempPointer));
jcarseyb1f95a02009-06-16 00:23:19 +00002028 ASSERT(CurrentItemPackage->Value != NULL);
jcarsey252d9452011-03-25 20:49:53 +00002029 StrCpy(CurrentItemPackage->Value, TempPointer);
jcarseya405b862010-09-14 05:18:09 +00002030 CurrentItemPackage->OriginalPosition = Count++;
jcarsey9b3bf082009-06-23 21:15:07 +00002031 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
jcarsey252d9452011-03-25 20:49:53 +00002032 } else {
jcarsey94b17fa2009-05-07 18:46:18 +00002033 //
2034 // this was a non-recognised flag... error!
2035 //
jcarsey252d9452011-03-25 20:49:53 +00002036 if (ProblemParam != NULL) {
2037 *ProblemParam = AllocateZeroPool(StrSize(Argv[LoopCounter]));
2038 ASSERT(*ProblemParam != NULL);
2039 StrCpy(*ProblemParam, Argv[LoopCounter]);
2040 }
jcarsey94b17fa2009-05-07 18:46:18 +00002041 ShellCommandLineFreeVarList(*CheckPackage);
2042 *CheckPackage = NULL;
2043 return (EFI_VOLUME_CORRUPTED);
jcarsey94b17fa2009-05-07 18:46:18 +00002044 }
2045 }
jcarsey125c2cf2009-11-18 21:36:50 +00002046 if (GetItemValue != 0) {
2047 GetItemValue = 0;
2048 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
2049 }
jcarsey94b17fa2009-05-07 18:46:18 +00002050 //
2051 // support for AutoPageBreak
2052 //
2053 if (AutoPageBreak && ShellCommandLineGetFlag(*CheckPackage, L"-b")) {
2054 ShellSetPageBreakMode(TRUE);
2055 }
2056 return (EFI_SUCCESS);
2057}
2058
2059/**
jcarsey1e6e84c2010-01-25 20:05:08 +00002060 Checks the command line arguments passed against the list of valid ones.
jcarsey94b17fa2009-05-07 18:46:18 +00002061 Optionally removes NULL values first.
jcarsey1e6e84c2010-01-25 20:05:08 +00002062
jcarsey94b17fa2009-05-07 18:46:18 +00002063 If no initialization is required, then return RETURN_SUCCESS.
jcarsey1e6e84c2010-01-25 20:05:08 +00002064
jcarseya405b862010-09-14 05:18:09 +00002065 @param[in] CheckList The pointer to list of parameters to check.
2066 @param[out] CheckPackage The package of checked values.
2067 @param[out] ProblemParam Optional pointer to pointer to unicode string for
jcarsey94b17fa2009-05-07 18:46:18 +00002068 the paramater that caused failure.
jcarseya405b862010-09-14 05:18:09 +00002069 @param[in] AutoPageBreak Will automatically set PageBreakEnabled.
2070 @param[in] AlwaysAllowNumbers Will never fail for number based flags.
jcarsey94b17fa2009-05-07 18:46:18 +00002071
2072 @retval EFI_SUCCESS The operation completed sucessfully.
jcarseya405b862010-09-14 05:18:09 +00002073 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
2074 @retval EFI_INVALID_PARAMETER A parameter was invalid.
2075 @retval EFI_VOLUME_CORRUPTED The command line was corrupt.
2076 @retval EFI_DEVICE_ERROR The commands contained 2 opposing arguments. One
jcarsey1e6e84c2010-01-25 20:05:08 +00002077 of the command line arguments was returned in
jcarsey94b17fa2009-05-07 18:46:18 +00002078 ProblemParam if provided.
jcarseya405b862010-09-14 05:18:09 +00002079 @retval EFI_NOT_FOUND A argument required a value that was missing.
2080 The invalid command line argument was returned in
jcarsey94b17fa2009-05-07 18:46:18 +00002081 ProblemParam if provided.
2082**/
2083EFI_STATUS
2084EFIAPI
jcarsey2247dde2009-11-09 18:08:58 +00002085ShellCommandLineParseEx (
jcarsey94b17fa2009-05-07 18:46:18 +00002086 IN CONST SHELL_PARAM_ITEM *CheckList,
2087 OUT LIST_ENTRY **CheckPackage,
2088 OUT CHAR16 **ProblemParam OPTIONAL,
jcarsey2247dde2009-11-09 18:08:58 +00002089 IN BOOLEAN AutoPageBreak,
2090 IN BOOLEAN AlwaysAllowNumbers
jcarseya405b862010-09-14 05:18:09 +00002091 )
2092{
jcarsey1e6e84c2010-01-25 20:05:08 +00002093 //
jcarsey94b17fa2009-05-07 18:46:18 +00002094 // ASSERT that CheckList and CheckPackage aren't NULL
2095 //
2096 ASSERT(CheckList != NULL);
2097 ASSERT(CheckPackage != NULL);
2098
jcarsey1e6e84c2010-01-25 20:05:08 +00002099 //
jcarsey94b17fa2009-05-07 18:46:18 +00002100 // Check for UEFI Shell 2.0 protocols
2101 //
jcarsey366f81a2011-06-27 21:04:22 +00002102 if (gEfiShellParametersProtocol != NULL) {
jcarsey1e6e84c2010-01-25 20:05:08 +00002103 return (InternalCommandLineParse(CheckList,
2104 CheckPackage,
2105 ProblemParam,
2106 AutoPageBreak,
jcarsey366f81a2011-06-27 21:04:22 +00002107 (CONST CHAR16**) gEfiShellParametersProtocol->Argv,
2108 gEfiShellParametersProtocol->Argc,
jcarsey2247dde2009-11-09 18:08:58 +00002109 AlwaysAllowNumbers));
jcarsey94b17fa2009-05-07 18:46:18 +00002110 }
2111
jcarsey1e6e84c2010-01-25 20:05:08 +00002112 //
jcarsey94b17fa2009-05-07 18:46:18 +00002113 // ASSERT That EFI Shell is not required
2114 //
2115 ASSERT (mEfiShellInterface != NULL);
jcarsey1e6e84c2010-01-25 20:05:08 +00002116 return (InternalCommandLineParse(CheckList,
2117 CheckPackage,
2118 ProblemParam,
2119 AutoPageBreak,
jljusten08d7f8e2009-06-15 18:42:13 +00002120 (CONST CHAR16**) mEfiShellInterface->Argv,
jcarsey2247dde2009-11-09 18:08:58 +00002121 mEfiShellInterface->Argc,
2122 AlwaysAllowNumbers));
jcarsey94b17fa2009-05-07 18:46:18 +00002123}
2124
2125/**
2126 Frees shell variable list that was returned from ShellCommandLineParse.
2127
2128 This function will free all the memory that was used for the CheckPackage
2129 list of postprocessed shell arguments.
2130
2131 this function has no return value.
2132
2133 if CheckPackage is NULL, then return
2134
2135 @param CheckPackage the list to de-allocate
2136 **/
2137VOID
2138EFIAPI
2139ShellCommandLineFreeVarList (
2140 IN LIST_ENTRY *CheckPackage
jcarseya405b862010-09-14 05:18:09 +00002141 )
2142{
jcarsey94b17fa2009-05-07 18:46:18 +00002143 LIST_ENTRY *Node;
2144
2145 //
2146 // check for CheckPackage == NULL
2147 //
2148 if (CheckPackage == NULL) {
2149 return;
2150 }
2151
2152 //
2153 // for each node in the list
2154 //
jcarsey9eb53ac2009-07-08 17:26:58 +00002155 for ( Node = GetFirstNode(CheckPackage)
jcarseya405b862010-09-14 05:18:09 +00002156 ; !IsListEmpty(CheckPackage)
jcarsey9eb53ac2009-07-08 17:26:58 +00002157 ; Node = GetFirstNode(CheckPackage)
jcarseya405b862010-09-14 05:18:09 +00002158 ){
jcarsey94b17fa2009-05-07 18:46:18 +00002159 //
2160 // Remove it from the list
2161 //
2162 RemoveEntryList(Node);
2163
2164 //
2165 // if it has a name free the name
2166 //
2167 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {
2168 FreePool(((SHELL_PARAM_PACKAGE*)Node)->Name);
2169 }
2170
2171 //
2172 // if it has a value free the value
2173 //
2174 if (((SHELL_PARAM_PACKAGE*)Node)->Value != NULL) {
2175 FreePool(((SHELL_PARAM_PACKAGE*)Node)->Value);
2176 }
jcarsey1e6e84c2010-01-25 20:05:08 +00002177
jcarsey94b17fa2009-05-07 18:46:18 +00002178 //
2179 // free the node structure
2180 //
2181 FreePool((SHELL_PARAM_PACKAGE*)Node);
2182 }
2183 //
2184 // free the list head node
2185 //
2186 FreePool(CheckPackage);
2187}
2188/**
2189 Checks for presence of a flag parameter
2190
2191 flag arguments are in the form of "-<Key>" or "/<Key>", but do not have a value following the key
2192
2193 if CheckPackage is NULL then return FALSE.
2194 if KeyString is NULL then ASSERT()
jcarsey1e6e84c2010-01-25 20:05:08 +00002195
jcarsey94b17fa2009-05-07 18:46:18 +00002196 @param CheckPackage The package of parsed command line arguments
2197 @param KeyString the Key of the command line argument to check for
2198
2199 @retval TRUE the flag is on the command line
2200 @retval FALSE the flag is not on the command line
2201 **/
2202BOOLEAN
2203EFIAPI
2204ShellCommandLineGetFlag (
jcarseya405b862010-09-14 05:18:09 +00002205 IN CONST LIST_ENTRY * CONST CheckPackage,
2206 IN CONST CHAR16 * CONST KeyString
2207 )
2208{
jcarsey94b17fa2009-05-07 18:46:18 +00002209 LIST_ENTRY *Node;
jcarsey252d9452011-03-25 20:49:53 +00002210 CHAR16 *TempString;
jcarsey94b17fa2009-05-07 18:46:18 +00002211
2212 //
2213 // ASSERT that both CheckPackage and KeyString aren't NULL
2214 //
2215 ASSERT(KeyString != NULL);
2216
2217 //
2218 // return FALSE for no package
2219 //
2220 if (CheckPackage == NULL) {
2221 return (FALSE);
2222 }
2223
2224 //
2225 // enumerate through the list of parametrs
2226 //
jcarsey1e6e84c2010-01-25 20:05:08 +00002227 for ( Node = GetFirstNode(CheckPackage)
2228 ; !IsNull (CheckPackage, Node)
2229 ; Node = GetNextNode(CheckPackage, Node)
jcarsey252d9452011-03-25 20:49:53 +00002230 ){
jcarsey94b17fa2009-05-07 18:46:18 +00002231 //
2232 // If the Name matches, return TRUE (and there may be NULL name)
2233 //
2234 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {
jcarsey9eb53ac2009-07-08 17:26:58 +00002235 //
2236 // If Type is TypeStart then only compare the begining of the strings
2237 //
jcarsey252d9452011-03-25 20:49:53 +00002238 if (((SHELL_PARAM_PACKAGE*)Node)->Type == TypeStart) {
2239 if (StrnCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name, StrLen(KeyString)) == 0) {
2240 return (TRUE);
2241 }
2242 TempString = NULL;
2243 TempString = StrnCatGrow(&TempString, NULL, KeyString, StrLen(((SHELL_PARAM_PACKAGE*)Node)->Name));
2244 if (TempString != NULL) {
2245 if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {
2246 FreePool(TempString);
2247 return (TRUE);
2248 }
2249 FreePool(TempString);
2250 }
2251 } else if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {
jcarsey94b17fa2009-05-07 18:46:18 +00002252 return (TRUE);
2253 }
2254 }
2255 }
2256 return (FALSE);
2257}
2258/**
jcarseya405b862010-09-14 05:18:09 +00002259 Returns value from command line argument.
jcarsey94b17fa2009-05-07 18:46:18 +00002260
jcarseya405b862010-09-14 05:18:09 +00002261 Value parameters are in the form of "-<Key> value" or "/<Key> value".
jcarsey1e6e84c2010-01-25 20:05:08 +00002262
jcarseya405b862010-09-14 05:18:09 +00002263 If CheckPackage is NULL, then return NULL.
jcarsey94b17fa2009-05-07 18:46:18 +00002264
jcarseya405b862010-09-14 05:18:09 +00002265 @param[in] CheckPackage The package of parsed command line arguments.
2266 @param[in] KeyString The Key of the command line argument to check for.
jcarsey94b17fa2009-05-07 18:46:18 +00002267
jcarseya405b862010-09-14 05:18:09 +00002268 @retval NULL The flag is not on the command line.
2269 @retval !=NULL The pointer to unicode string of the value.
2270**/
jcarsey94b17fa2009-05-07 18:46:18 +00002271CONST CHAR16*
2272EFIAPI
2273ShellCommandLineGetValue (
2274 IN CONST LIST_ENTRY *CheckPackage,
2275 IN CHAR16 *KeyString
jcarseya405b862010-09-14 05:18:09 +00002276 )
2277{
jcarsey94b17fa2009-05-07 18:46:18 +00002278 LIST_ENTRY *Node;
jcarsey252d9452011-03-25 20:49:53 +00002279 CHAR16 *TempString;
jcarsey94b17fa2009-05-07 18:46:18 +00002280
2281 //
2282 // check for CheckPackage == NULL
2283 //
2284 if (CheckPackage == NULL) {
2285 return (NULL);
2286 }
2287
2288 //
2289 // enumerate through the list of parametrs
2290 //
jcarsey1e6e84c2010-01-25 20:05:08 +00002291 for ( Node = GetFirstNode(CheckPackage)
2292 ; !IsNull (CheckPackage, Node)
2293 ; Node = GetNextNode(CheckPackage, Node)
jcarsey252d9452011-03-25 20:49:53 +00002294 ){
jcarsey94b17fa2009-05-07 18:46:18 +00002295 //
jcarsey252d9452011-03-25 20:49:53 +00002296 // If the Name matches, return TRUE (and there may be NULL name)
jcarsey94b17fa2009-05-07 18:46:18 +00002297 //
2298 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {
jcarsey9eb53ac2009-07-08 17:26:58 +00002299 //
2300 // If Type is TypeStart then only compare the begining of the strings
2301 //
jcarsey252d9452011-03-25 20:49:53 +00002302 if (((SHELL_PARAM_PACKAGE*)Node)->Type == TypeStart) {
2303 if (StrnCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name, StrLen(KeyString)) == 0) {
2304 return (((SHELL_PARAM_PACKAGE*)Node)->Name + StrLen(KeyString));
2305 }
2306 TempString = NULL;
2307 TempString = StrnCatGrow(&TempString, NULL, KeyString, StrLen(((SHELL_PARAM_PACKAGE*)Node)->Name));
2308 if (TempString != NULL) {
2309 if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {
2310 FreePool(TempString);
2311 return (((SHELL_PARAM_PACKAGE*)Node)->Name + StrLen(KeyString));
2312 }
2313 FreePool(TempString);
2314 }
2315 } else if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {
jcarsey94b17fa2009-05-07 18:46:18 +00002316 return (((SHELL_PARAM_PACKAGE*)Node)->Value);
2317 }
2318 }
2319 }
2320 return (NULL);
2321}
jcarseya405b862010-09-14 05:18:09 +00002322
jcarsey94b17fa2009-05-07 18:46:18 +00002323/**
jcarseya405b862010-09-14 05:18:09 +00002324 Returns raw value from command line argument.
jcarsey94b17fa2009-05-07 18:46:18 +00002325
jcarseya405b862010-09-14 05:18:09 +00002326 Raw value parameters are in the form of "value" in a specific position in the list.
jcarsey1e6e84c2010-01-25 20:05:08 +00002327
jcarseya405b862010-09-14 05:18:09 +00002328 If CheckPackage is NULL, then return NULL.
jcarsey94b17fa2009-05-07 18:46:18 +00002329
jcarseya405b862010-09-14 05:18:09 +00002330 @param[in] CheckPackage The package of parsed command line arguments.
2331 @param[in] Position The position of the value.
jcarsey94b17fa2009-05-07 18:46:18 +00002332
jcarseya405b862010-09-14 05:18:09 +00002333 @retval NULL The flag is not on the command line.
2334 @retval !=NULL The pointer to unicode string of the value.
jcarsey94b17fa2009-05-07 18:46:18 +00002335 **/
2336CONST CHAR16*
2337EFIAPI
2338ShellCommandLineGetRawValue (
jcarseya405b862010-09-14 05:18:09 +00002339 IN CONST LIST_ENTRY * CONST CheckPackage,
2340 IN UINTN Position
2341 )
2342{
jcarsey94b17fa2009-05-07 18:46:18 +00002343 LIST_ENTRY *Node;
2344
2345 //
2346 // check for CheckPackage == NULL
2347 //
2348 if (CheckPackage == NULL) {
2349 return (NULL);
2350 }
2351
2352 //
2353 // enumerate through the list of parametrs
2354 //
jcarsey1e6e84c2010-01-25 20:05:08 +00002355 for ( Node = GetFirstNode(CheckPackage)
2356 ; !IsNull (CheckPackage, Node)
2357 ; Node = GetNextNode(CheckPackage, Node)
jcarseya405b862010-09-14 05:18:09 +00002358 ){
jcarsey94b17fa2009-05-07 18:46:18 +00002359 //
2360 // If the position matches, return the value
2361 //
2362 if (((SHELL_PARAM_PACKAGE*)Node)->OriginalPosition == Position) {
2363 return (((SHELL_PARAM_PACKAGE*)Node)->Value);
2364 }
2365 }
2366 return (NULL);
jcarseyb1f95a02009-06-16 00:23:19 +00002367}
jcarsey2247dde2009-11-09 18:08:58 +00002368
2369/**
jcarsey1e6e84c2010-01-25 20:05:08 +00002370 returns the number of command line value parameters that were parsed.
2371
jcarsey2247dde2009-11-09 18:08:58 +00002372 this will not include flags.
2373
jcarseya405b862010-09-14 05:18:09 +00002374 @param[in] CheckPackage The package of parsed command line arguments.
2375
jcarsey2247dde2009-11-09 18:08:58 +00002376 @retval (UINTN)-1 No parsing has ocurred
2377 @return other The number of value parameters found
2378**/
2379UINTN
2380EFIAPI
2381ShellCommandLineGetCount(
jcarseya405b862010-09-14 05:18:09 +00002382 IN CONST LIST_ENTRY *CheckPackage
jcarsey125c2cf2009-11-18 21:36:50 +00002383 )
2384{
jcarseya405b862010-09-14 05:18:09 +00002385 LIST_ENTRY *Node1;
2386 UINTN Count;
2387
2388 if (CheckPackage == NULL) {
2389 return (0);
2390 }
2391 for ( Node1 = GetFirstNode(CheckPackage), Count = 0
2392 ; !IsNull (CheckPackage, Node1)
2393 ; Node1 = GetNextNode(CheckPackage, Node1)
2394 ){
2395 if (((SHELL_PARAM_PACKAGE*)Node1)->Name == NULL) {
2396 Count++;
2397 }
2398 }
2399 return (Count);
jcarsey2247dde2009-11-09 18:08:58 +00002400}
2401
jcarsey975136a2009-06-16 19:03:54 +00002402/**
jcarsey36a9d672009-11-20 21:13:41 +00002403 Determins if a parameter is duplicated.
2404
jcarsey1e6e84c2010-01-25 20:05:08 +00002405 If Param is not NULL then it will point to a callee allocated string buffer
jcarsey36a9d672009-11-20 21:13:41 +00002406 with the parameter value if a duplicate is found.
2407
2408 If CheckPackage is NULL, then ASSERT.
2409
2410 @param[in] CheckPackage The package of parsed command line arguments.
2411 @param[out] Param Upon finding one, a pointer to the duplicated parameter.
2412
2413 @retval EFI_SUCCESS No parameters were duplicated.
2414 @retval EFI_DEVICE_ERROR A duplicate was found.
2415 **/
2416EFI_STATUS
2417EFIAPI
2418ShellCommandLineCheckDuplicate (
2419 IN CONST LIST_ENTRY *CheckPackage,
2420 OUT CHAR16 **Param
2421 )
2422{
2423 LIST_ENTRY *Node1;
2424 LIST_ENTRY *Node2;
jcarsey1e6e84c2010-01-25 20:05:08 +00002425
jcarsey36a9d672009-11-20 21:13:41 +00002426 ASSERT(CheckPackage != NULL);
2427
jcarsey1e6e84c2010-01-25 20:05:08 +00002428 for ( Node1 = GetFirstNode(CheckPackage)
2429 ; !IsNull (CheckPackage, Node1)
2430 ; Node1 = GetNextNode(CheckPackage, Node1)
jcarseya405b862010-09-14 05:18:09 +00002431 ){
jcarsey1e6e84c2010-01-25 20:05:08 +00002432 for ( Node2 = GetNextNode(CheckPackage, Node1)
2433 ; !IsNull (CheckPackage, Node2)
2434 ; Node2 = GetNextNode(CheckPackage, Node2)
jcarseya405b862010-09-14 05:18:09 +00002435 ){
2436 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 +00002437 if (Param != NULL) {
2438 *Param = NULL;
2439 *Param = StrnCatGrow(Param, NULL, ((SHELL_PARAM_PACKAGE*)Node1)->Name, 0);
2440 }
2441 return (EFI_DEVICE_ERROR);
2442 }
2443 }
2444 }
2445 return (EFI_SUCCESS);
2446}
2447
2448/**
jcarsey1e6e84c2010-01-25 20:05:08 +00002449 This is a find and replace function. Upon successful return the NewString is a copy of
jcarsey975136a2009-06-16 19:03:54 +00002450 SourceString with each instance of FindTarget replaced with ReplaceWith.
2451
jcarseyb3011f42010-01-11 21:49:04 +00002452 If SourceString and NewString overlap the behavior is undefined.
2453
jcarsey975136a2009-06-16 19:03:54 +00002454 If the string would grow bigger than NewSize it will halt and return error.
2455
jcarseya405b862010-09-14 05:18:09 +00002456 @param[in] SourceString The string with source buffer.
2457 @param[in,out] NewString The string with resultant buffer.
2458 @param[in] NewSize The size in bytes of NewString.
2459 @param[in] FindTarget The string to look for.
2460 @param[in] ReplaceWith The string to replace FindTarget with.
jcarsey969c7832010-01-13 16:46:33 +00002461 @param[in] SkipPreCarrot If TRUE will skip a FindTarget that has a '^'
2462 immediately before it.
jcarseya405b862010-09-14 05:18:09 +00002463 @param[in] ParameterReplacing If TRUE will add "" around items with spaces.
jcarsey975136a2009-06-16 19:03:54 +00002464
jcarsey969c7832010-01-13 16:46:33 +00002465 @retval EFI_INVALID_PARAMETER SourceString was NULL.
2466 @retval EFI_INVALID_PARAMETER NewString was NULL.
2467 @retval EFI_INVALID_PARAMETER FindTarget was NULL.
2468 @retval EFI_INVALID_PARAMETER ReplaceWith was NULL.
2469 @retval EFI_INVALID_PARAMETER FindTarget had length < 1.
2470 @retval EFI_INVALID_PARAMETER SourceString had length < 1.
jcarsey1e6e84c2010-01-25 20:05:08 +00002471 @retval EFI_BUFFER_TOO_SMALL NewSize was less than the minimum size to hold
jcarsey969c7832010-01-13 16:46:33 +00002472 the new string (truncation occurred).
jcarseya405b862010-09-14 05:18:09 +00002473 @retval EFI_SUCCESS The string was successfully copied with replacement.
jcarsey975136a2009-06-16 19:03:54 +00002474**/
jcarsey975136a2009-06-16 19:03:54 +00002475EFI_STATUS
2476EFIAPI
jcarseya405b862010-09-14 05:18:09 +00002477ShellCopySearchAndReplace(
jcarsey975136a2009-06-16 19:03:54 +00002478 IN CHAR16 CONST *SourceString,
jcarseya405b862010-09-14 05:18:09 +00002479 IN OUT CHAR16 *NewString,
jcarsey975136a2009-06-16 19:03:54 +00002480 IN UINTN NewSize,
2481 IN CONST CHAR16 *FindTarget,
jcarsey969c7832010-01-13 16:46:33 +00002482 IN CONST CHAR16 *ReplaceWith,
jcarseya405b862010-09-14 05:18:09 +00002483 IN CONST BOOLEAN SkipPreCarrot,
2484 IN CONST BOOLEAN ParameterReplacing
jcarsey1e6e84c2010-01-25 20:05:08 +00002485 )
jcarsey2247dde2009-11-09 18:08:58 +00002486{
jcarsey01582942009-07-10 19:46:17 +00002487 UINTN Size;
jcarseya405b862010-09-14 05:18:09 +00002488 CHAR16 *Replace;
2489
jcarsey975136a2009-06-16 19:03:54 +00002490 if ( (SourceString == NULL)
2491 || (NewString == NULL)
2492 || (FindTarget == NULL)
2493 || (ReplaceWith == NULL)
2494 || (StrLen(FindTarget) < 1)
2495 || (StrLen(SourceString) < 1)
jcarseya405b862010-09-14 05:18:09 +00002496 ){
jcarsey975136a2009-06-16 19:03:54 +00002497 return (EFI_INVALID_PARAMETER);
2498 }
jcarseya405b862010-09-14 05:18:09 +00002499 Replace = NULL;
2500 if (StrStr(ReplaceWith, L" ") == NULL || !ParameterReplacing) {
2501 Replace = StrnCatGrow(&Replace, NULL, ReplaceWith, 0);
2502 } else {
2503 Replace = AllocateZeroPool(StrSize(ReplaceWith) + 2*sizeof(CHAR16));
2504 UnicodeSPrint(Replace, StrSize(ReplaceWith) + 2*sizeof(CHAR16), L"\"%s\"", ReplaceWith);
2505 }
jcarsey3e082d52010-10-04 16:44:57 +00002506 if (Replace == NULL) {
2507 return (EFI_OUT_OF_RESOURCES);
2508 }
jcarsey2247dde2009-11-09 18:08:58 +00002509 NewString = SetMem16(NewString, NewSize, CHAR_NULL);
2510 while (*SourceString != CHAR_NULL) {
jcarsey969c7832010-01-13 16:46:33 +00002511 //
jcarseya405b862010-09-14 05:18:09 +00002512 // if we find the FindTarget and either Skip == FALSE or Skip and we
jcarsey969c7832010-01-13 16:46:33 +00002513 // dont have a carrot do a replace...
2514 //
jcarsey1e6e84c2010-01-25 20:05:08 +00002515 if (StrnCmp(SourceString, FindTarget, StrLen(FindTarget)) == 0
jcarseya405b862010-09-14 05:18:09 +00002516 && ((SkipPreCarrot && *(SourceString-1) != L'^') || !SkipPreCarrot)
2517 ){
jcarsey975136a2009-06-16 19:03:54 +00002518 SourceString += StrLen(FindTarget);
jcarsey01582942009-07-10 19:46:17 +00002519 Size = StrSize(NewString);
jcarseya405b862010-09-14 05:18:09 +00002520 if ((Size + (StrLen(Replace)*sizeof(CHAR16))) > NewSize) {
2521 FreePool(Replace);
jcarsey975136a2009-06-16 19:03:54 +00002522 return (EFI_BUFFER_TOO_SMALL);
2523 }
jcarseya405b862010-09-14 05:18:09 +00002524 StrCat(NewString, Replace);
jcarsey975136a2009-06-16 19:03:54 +00002525 } else {
jcarsey01582942009-07-10 19:46:17 +00002526 Size = StrSize(NewString);
2527 if (Size + sizeof(CHAR16) > NewSize) {
jcarseya405b862010-09-14 05:18:09 +00002528 FreePool(Replace);
jcarsey975136a2009-06-16 19:03:54 +00002529 return (EFI_BUFFER_TOO_SMALL);
2530 }
2531 StrnCat(NewString, SourceString, 1);
2532 SourceString++;
2533 }
2534 }
jcarseya405b862010-09-14 05:18:09 +00002535 FreePool(Replace);
jcarsey975136a2009-06-16 19:03:54 +00002536 return (EFI_SUCCESS);
2537}
jcarseyb1f95a02009-06-16 00:23:19 +00002538
2539/**
jcarseye2f82972009-12-01 05:40:24 +00002540 Internal worker function to output a string.
2541
2542 This function will output a string to the correct StdOut.
2543
2544 @param[in] String The string to print out.
2545
2546 @retval EFI_SUCCESS The operation was sucessful.
2547 @retval !EFI_SUCCESS The operation failed.
2548**/
2549EFI_STATUS
2550EFIAPI
2551InternalPrintTo (
2552 IN CONST CHAR16 *String
2553 )
2554{
2555 UINTN Size;
2556 Size = StrSize(String) - sizeof(CHAR16);
jcarseya405b862010-09-14 05:18:09 +00002557 if (Size == 0) {
2558 return (EFI_SUCCESS);
2559 }
jcarsey366f81a2011-06-27 21:04:22 +00002560 if (gEfiShellParametersProtocol != NULL) {
2561 return (gEfiShellProtocol->WriteFile(gEfiShellParametersProtocol->StdOut, &Size, (VOID*)String));
jcarseye2f82972009-12-01 05:40:24 +00002562 }
2563 if (mEfiShellInterface != NULL) {
jcarseyecd3d592009-12-07 18:05:00 +00002564 //
2565 // Divide in half for old shell. Must be string length not size.
2566 //
2567 Size /= 2;
jcarseya405b862010-09-14 05:18:09 +00002568 return (mEfiShellInterface->StdOut->Write(mEfiShellInterface->StdOut, &Size, (VOID*)String));
jcarseye2f82972009-12-01 05:40:24 +00002569 }
2570 ASSERT(FALSE);
2571 return (EFI_UNSUPPORTED);
2572}
2573
2574/**
jcarseyb1f95a02009-06-16 00:23:19 +00002575 Print at a specific location on the screen.
2576
jcarseyf1b87e72009-06-17 00:52:11 +00002577 This function will move the cursor to a given screen location and print the specified string
jcarsey1e6e84c2010-01-25 20:05:08 +00002578
2579 If -1 is specified for either the Row or Col the current screen location for BOTH
jcarseyf1b87e72009-06-17 00:52:11 +00002580 will be used.
jcarseyb1f95a02009-06-16 00:23:19 +00002581
2582 if either Row or Col is out of range for the current console, then ASSERT
2583 if Format is NULL, then ASSERT
2584
jcarsey1e6e84c2010-01-25 20:05:08 +00002585 In addition to the standard %-based flags as supported by UefiLib Print() this supports
jcarseyb1f95a02009-06-16 00:23:19 +00002586 the following additional flags:
2587 %N - Set output attribute to normal
2588 %H - Set output attribute to highlight
2589 %E - Set output attribute to error
2590 %B - Set output attribute to blue color
2591 %V - Set output attribute to green color
2592
2593 Note: The background color is controlled by the shell command cls.
2594
jcarseyb1f95a02009-06-16 00:23:19 +00002595 @param[in] Col the column to print at
jcarsey252d9452011-03-25 20:49:53 +00002596 @param[in] Row the row to print at
jcarseyb1f95a02009-06-16 00:23:19 +00002597 @param[in] Format the format string
jcarsey2247dde2009-11-09 18:08:58 +00002598 @param[in] Marker the marker for the variable argument list
jcarseyb1f95a02009-06-16 00:23:19 +00002599
jcarseya405b862010-09-14 05:18:09 +00002600 @return EFI_SUCCESS The operation was successful.
2601 @return EFI_DEVICE_ERROR The console device reported an error.
jcarseyb1f95a02009-06-16 00:23:19 +00002602**/
jcarseya405b862010-09-14 05:18:09 +00002603EFI_STATUS
jcarseyb1f95a02009-06-16 00:23:19 +00002604EFIAPI
jcarsey2247dde2009-11-09 18:08:58 +00002605InternalShellPrintWorker(
jcarseyb1f95a02009-06-16 00:23:19 +00002606 IN INT32 Col OPTIONAL,
2607 IN INT32 Row OPTIONAL,
2608 IN CONST CHAR16 *Format,
jcarsey252d9452011-03-25 20:49:53 +00002609 IN VA_LIST Marker
jcarsey1e6e84c2010-01-25 20:05:08 +00002610 )
jcarsey2247dde2009-11-09 18:08:58 +00002611{
jcarseyb1f95a02009-06-16 00:23:19 +00002612 EFI_STATUS Status;
jcarsey975136a2009-06-16 19:03:54 +00002613 CHAR16 *ResumeLocation;
2614 CHAR16 *FormatWalker;
jcarseya405b862010-09-14 05:18:09 +00002615 UINTN OriginalAttribute;
jcarsey89e85372011-04-13 23:37:50 +00002616 CHAR16 *mPostReplaceFormat;
2617 CHAR16 *mPostReplaceFormat2;
2618
2619 mPostReplaceFormat = AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize));
2620 mPostReplaceFormat2 = AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize));
jcarseya405b862010-09-14 05:18:09 +00002621
jcarseyf8d3e682011-04-19 17:54:42 +00002622 if (mPostReplaceFormat == NULL || mPostReplaceFormat2 == NULL) {
2623 SHELL_FREE_NON_NULL(mPostReplaceFormat);
2624 SHELL_FREE_NON_NULL(mPostReplaceFormat2);
2625 return (EFI_OUT_OF_RESOURCES);
2626 }
2627
jcarseya405b862010-09-14 05:18:09 +00002628 Status = EFI_SUCCESS;
2629 OriginalAttribute = gST->ConOut->Mode->Attribute;
jcarsey1e6e84c2010-01-25 20:05:08 +00002630
jcarsey975136a2009-06-16 19:03:54 +00002631 //
2632 // Back and forth each time fixing up 1 of our flags...
2633 //
jcarseya405b862010-09-14 05:18:09 +00002634 Status = ShellCopySearchAndReplace(Format, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%N", L"%%N", FALSE, FALSE);
jcarsey975136a2009-06-16 19:03:54 +00002635 ASSERT_EFI_ERROR(Status);
jcarseya405b862010-09-14 05:18:09 +00002636 Status = ShellCopySearchAndReplace(mPostReplaceFormat, mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), L"%E", L"%%E", FALSE, FALSE);
jcarsey975136a2009-06-16 19:03:54 +00002637 ASSERT_EFI_ERROR(Status);
jcarseya405b862010-09-14 05:18:09 +00002638 Status = ShellCopySearchAndReplace(mPostReplaceFormat2, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%H", L"%%H", FALSE, FALSE);
jcarsey975136a2009-06-16 19:03:54 +00002639 ASSERT_EFI_ERROR(Status);
jcarseya405b862010-09-14 05:18:09 +00002640 Status = ShellCopySearchAndReplace(mPostReplaceFormat, mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), L"%B", L"%%B", FALSE, FALSE);
jcarsey975136a2009-06-16 19:03:54 +00002641 ASSERT_EFI_ERROR(Status);
jcarseya405b862010-09-14 05:18:09 +00002642 Status = ShellCopySearchAndReplace(mPostReplaceFormat2, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%V", L"%%V", FALSE, FALSE);
jcarsey975136a2009-06-16 19:03:54 +00002643 ASSERT_EFI_ERROR(Status);
2644
2645 //
2646 // Use the last buffer from replacing to print from...
2647 //
jcarseya405b862010-09-14 05:18:09 +00002648 UnicodeVSPrint (mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), mPostReplaceFormat, Marker);
jcarseyb1f95a02009-06-16 00:23:19 +00002649
2650 if (Col != -1 && Row != -1) {
jcarseyb1f95a02009-06-16 00:23:19 +00002651 Status = gST->ConOut->SetCursorPosition(gST->ConOut, Col, Row);
jcarsey975136a2009-06-16 19:03:54 +00002652 }
2653
jcarseyecd3d592009-12-07 18:05:00 +00002654 FormatWalker = mPostReplaceFormat2;
jcarsey2247dde2009-11-09 18:08:58 +00002655 while (*FormatWalker != CHAR_NULL) {
jcarsey975136a2009-06-16 19:03:54 +00002656 //
2657 // Find the next attribute change request
2658 //
2659 ResumeLocation = StrStr(FormatWalker, L"%");
2660 if (ResumeLocation != NULL) {
jcarsey2247dde2009-11-09 18:08:58 +00002661 *ResumeLocation = CHAR_NULL;
jcarsey975136a2009-06-16 19:03:54 +00002662 }
2663 //
2664 // print the current FormatWalker string
2665 //
jcarseya405b862010-09-14 05:18:09 +00002666 if (StrLen(FormatWalker)>0) {
2667 Status = InternalPrintTo(FormatWalker);
2668 if (EFI_ERROR(Status)) {
2669 break;
2670 }
2671 }
2672
jcarsey975136a2009-06-16 19:03:54 +00002673 //
2674 // update the attribute
2675 //
2676 if (ResumeLocation != NULL) {
2677 switch (*(ResumeLocation+1)) {
2678 case (L'N'):
jcarseya405b862010-09-14 05:18:09 +00002679 gST->ConOut->SetAttribute(gST->ConOut, OriginalAttribute);
jcarsey975136a2009-06-16 19:03:54 +00002680 break;
2681 case (L'E'):
jcarseya405b862010-09-14 05:18:09 +00002682 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_YELLOW, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));
jcarsey975136a2009-06-16 19:03:54 +00002683 break;
2684 case (L'H'):
jcarseya405b862010-09-14 05:18:09 +00002685 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_WHITE, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));
jcarsey975136a2009-06-16 19:03:54 +00002686 break;
2687 case (L'B'):
jcarseya405b862010-09-14 05:18:09 +00002688 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_BLUE, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));
jcarsey975136a2009-06-16 19:03:54 +00002689 break;
2690 case (L'V'):
jcarseya405b862010-09-14 05:18:09 +00002691 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_GREEN, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));
jcarsey975136a2009-06-16 19:03:54 +00002692 break;
2693 default:
jcarseye2f82972009-12-01 05:40:24 +00002694 //
2695 // Print a simple '%' symbol
2696 //
2697 Status = InternalPrintTo(L"%");
jcarseya405b862010-09-14 05:18:09 +00002698 if (EFI_ERROR(Status)) {
2699 break;
2700 }
jcarseye2f82972009-12-01 05:40:24 +00002701 ResumeLocation = ResumeLocation - 1;
jcarsey975136a2009-06-16 19:03:54 +00002702 break;
2703 }
2704 } else {
2705 //
2706 // reset to normal now...
2707 //
jcarsey975136a2009-06-16 19:03:54 +00002708 break;
2709 }
2710
2711 //
2712 // update FormatWalker to Resume + 2 (skip the % and the indicator)
2713 //
2714 FormatWalker = ResumeLocation + 2;
2715 }
jcarseyb1f95a02009-06-16 00:23:19 +00002716
jcarseya405b862010-09-14 05:18:09 +00002717 gST->ConOut->SetAttribute(gST->ConOut, OriginalAttribute);
jcarsey89e85372011-04-13 23:37:50 +00002718
2719 SHELL_FREE_NON_NULL(mPostReplaceFormat);
2720 SHELL_FREE_NON_NULL(mPostReplaceFormat2);
jcarseya405b862010-09-14 05:18:09 +00002721 return (Status);
jcarsey5f7431d2009-07-10 18:06:01 +00002722}
jcarsey2247dde2009-11-09 18:08:58 +00002723
2724/**
2725 Print at a specific location on the screen.
2726
jcarseye2f82972009-12-01 05:40:24 +00002727 This function will move the cursor to a given screen location and print the specified string.
jcarsey1e6e84c2010-01-25 20:05:08 +00002728
2729 If -1 is specified for either the Row or Col the current screen location for BOTH
jcarsey2247dde2009-11-09 18:08:58 +00002730 will be used.
2731
jcarseye2f82972009-12-01 05:40:24 +00002732 If either Row or Col is out of range for the current console, then ASSERT.
2733 If Format is NULL, then ASSERT.
jcarsey2247dde2009-11-09 18:08:58 +00002734
jcarsey1e6e84c2010-01-25 20:05:08 +00002735 In addition to the standard %-based flags as supported by UefiLib Print() this supports
jcarsey2247dde2009-11-09 18:08:58 +00002736 the following additional flags:
2737 %N - Set output attribute to normal
2738 %H - Set output attribute to highlight
2739 %E - Set output attribute to error
2740 %B - Set output attribute to blue color
2741 %V - Set output attribute to green color
2742
2743 Note: The background color is controlled by the shell command cls.
2744
jcarsey2247dde2009-11-09 18:08:58 +00002745 @param[in] Col the column to print at
jcarseya405b862010-09-14 05:18:09 +00002746 @param[in] Row the row to print at
jcarsey2247dde2009-11-09 18:08:58 +00002747 @param[in] Format the format string
jcarseya405b862010-09-14 05:18:09 +00002748 @param[in] ... The variable argument list.
jcarsey2247dde2009-11-09 18:08:58 +00002749
jcarseya405b862010-09-14 05:18:09 +00002750 @return EFI_SUCCESS The printing was successful.
2751 @return EFI_DEVICE_ERROR The console device reported an error.
jcarsey2247dde2009-11-09 18:08:58 +00002752**/
jcarseya405b862010-09-14 05:18:09 +00002753EFI_STATUS
jcarsey2247dde2009-11-09 18:08:58 +00002754EFIAPI
2755ShellPrintEx(
2756 IN INT32 Col OPTIONAL,
2757 IN INT32 Row OPTIONAL,
2758 IN CONST CHAR16 *Format,
2759 ...
jcarsey1e6e84c2010-01-25 20:05:08 +00002760 )
jcarsey2247dde2009-11-09 18:08:58 +00002761{
2762 VA_LIST Marker;
jcarseya405b862010-09-14 05:18:09 +00002763 EFI_STATUS RetVal;
jcarsey3e082d52010-10-04 16:44:57 +00002764 if (Format == NULL) {
2765 return (EFI_INVALID_PARAMETER);
2766 }
jcarsey2247dde2009-11-09 18:08:58 +00002767 VA_START (Marker, Format);
jcarseya405b862010-09-14 05:18:09 +00002768 RetVal = InternalShellPrintWorker(Col, Row, Format, Marker);
jcarseye2f82972009-12-01 05:40:24 +00002769 VA_END(Marker);
jcarseya405b862010-09-14 05:18:09 +00002770 return(RetVal);
jcarsey2247dde2009-11-09 18:08:58 +00002771}
2772
2773/**
2774 Print at a specific location on the screen.
2775
jcarseye2f82972009-12-01 05:40:24 +00002776 This function will move the cursor to a given screen location and print the specified string.
jcarsey1e6e84c2010-01-25 20:05:08 +00002777
2778 If -1 is specified for either the Row or Col the current screen location for BOTH
jcarseye2f82972009-12-01 05:40:24 +00002779 will be used.
jcarsey2247dde2009-11-09 18:08:58 +00002780
jcarseye2f82972009-12-01 05:40:24 +00002781 If either Row or Col is out of range for the current console, then ASSERT.
2782 If Format is NULL, then ASSERT.
jcarsey2247dde2009-11-09 18:08:58 +00002783
jcarsey1e6e84c2010-01-25 20:05:08 +00002784 In addition to the standard %-based flags as supported by UefiLib Print() this supports
jcarsey2247dde2009-11-09 18:08:58 +00002785 the following additional flags:
jcarsey1e6e84c2010-01-25 20:05:08 +00002786 %N - Set output attribute to normal.
2787 %H - Set output attribute to highlight.
2788 %E - Set output attribute to error.
2789 %B - Set output attribute to blue color.
2790 %V - Set output attribute to green color.
jcarsey2247dde2009-11-09 18:08:58 +00002791
2792 Note: The background color is controlled by the shell command cls.
2793
jcarsey1e6e84c2010-01-25 20:05:08 +00002794 @param[in] Col The column to print at.
jcarseya405b862010-09-14 05:18:09 +00002795 @param[in] Row The row to print at.
jcarsey1e6e84c2010-01-25 20:05:08 +00002796 @param[in] Language The language of the string to retrieve. If this parameter
2797 is NULL, then the current platform language is used.
2798 @param[in] HiiFormatStringId The format string Id for getting from Hii.
2799 @param[in] HiiFormatHandle The format string Handle for getting from Hii.
jcarseya405b862010-09-14 05:18:09 +00002800 @param[in] ... The variable argument list.
jcarsey2247dde2009-11-09 18:08:58 +00002801
jcarseya405b862010-09-14 05:18:09 +00002802 @return EFI_SUCCESS The printing was successful.
2803 @return EFI_DEVICE_ERROR The console device reported an error.
jcarsey2247dde2009-11-09 18:08:58 +00002804**/
jcarseya405b862010-09-14 05:18:09 +00002805EFI_STATUS
jcarsey2247dde2009-11-09 18:08:58 +00002806EFIAPI
2807ShellPrintHiiEx(
2808 IN INT32 Col OPTIONAL,
2809 IN INT32 Row OPTIONAL,
jcarsey1e6e84c2010-01-25 20:05:08 +00002810 IN CONST CHAR8 *Language OPTIONAL,
jcarsey2247dde2009-11-09 18:08:58 +00002811 IN CONST EFI_STRING_ID HiiFormatStringId,
2812 IN CONST EFI_HANDLE HiiFormatHandle,
2813 ...
2814 )
2815{
2816 VA_LIST Marker;
2817 CHAR16 *HiiFormatString;
jcarseya405b862010-09-14 05:18:09 +00002818 EFI_STATUS RetVal;
jcarsey2247dde2009-11-09 18:08:58 +00002819
2820 VA_START (Marker, HiiFormatHandle);
jcarsey1e6e84c2010-01-25 20:05:08 +00002821 HiiFormatString = HiiGetString(HiiFormatHandle, HiiFormatStringId, Language);
jcarsey2247dde2009-11-09 18:08:58 +00002822 ASSERT(HiiFormatString != NULL);
2823
2824 RetVal = InternalShellPrintWorker(Col, Row, HiiFormatString, Marker);
2825
jcarseya405b862010-09-14 05:18:09 +00002826 SHELL_FREE_NON_NULL(HiiFormatString);
jcarseye2f82972009-12-01 05:40:24 +00002827 VA_END(Marker);
jcarsey2247dde2009-11-09 18:08:58 +00002828
2829 return (RetVal);
2830}
2831
2832/**
2833 Function to determine if a given filename represents a file or a directory.
2834
2835 @param[in] DirName Path to directory to test.
2836
2837 @retval EFI_SUCCESS The Path represents a directory
2838 @retval EFI_NOT_FOUND The Path does not represent a directory
2839 @return other The path failed to open
2840**/
2841EFI_STATUS
2842EFIAPI
2843ShellIsDirectory(
2844 IN CONST CHAR16 *DirName
2845 )
2846{
2847 EFI_STATUS Status;
jcarseya405b862010-09-14 05:18:09 +00002848 SHELL_FILE_HANDLE Handle;
jcarsey3e082d52010-10-04 16:44:57 +00002849 CHAR16 *TempLocation;
2850 CHAR16 *TempLocation2;
jcarsey2247dde2009-11-09 18:08:58 +00002851
jcarseyecd3d592009-12-07 18:05:00 +00002852 ASSERT(DirName != NULL);
2853
jcarseya405b862010-09-14 05:18:09 +00002854 Handle = NULL;
2855 TempLocation = NULL;
jcarsey2247dde2009-11-09 18:08:58 +00002856
2857 Status = ShellOpenFileByName(DirName, &Handle, EFI_FILE_MODE_READ, 0);
2858 if (EFI_ERROR(Status)) {
jcarseya405b862010-09-14 05:18:09 +00002859 //
2860 // try good logic first.
2861 //
jcarsey366f81a2011-06-27 21:04:22 +00002862 if (gEfiShellProtocol != NULL) {
jcarsey3e082d52010-10-04 16:44:57 +00002863 TempLocation = StrnCatGrow(&TempLocation, NULL, DirName, 0);
2864 TempLocation2 = StrStr(TempLocation, L":");
2865 if (TempLocation2 != NULL && StrLen(StrStr(TempLocation, L":")) == 2) {
2866 *(TempLocation2+1) = CHAR_NULL;
jcarseya405b862010-09-14 05:18:09 +00002867 }
jcarsey366f81a2011-06-27 21:04:22 +00002868 if (gEfiShellProtocol->GetDevicePathFromMap(TempLocation) != NULL) {
jcarseya405b862010-09-14 05:18:09 +00002869 FreePool(TempLocation);
2870 return (EFI_SUCCESS);
2871 }
2872 FreePool(TempLocation);
2873 } else {
2874 //
2875 // probably a map name?!?!!?
2876 //
2877 TempLocation = StrStr(DirName, L"\\");
2878 if (TempLocation != NULL && *(TempLocation+1) == CHAR_NULL) {
2879 return (EFI_SUCCESS);
2880 }
2881 }
jcarsey2247dde2009-11-09 18:08:58 +00002882 return (Status);
2883 }
2884
2885 if (FileHandleIsDirectory(Handle) == EFI_SUCCESS) {
2886 ShellCloseFile(&Handle);
2887 return (EFI_SUCCESS);
2888 }
2889 ShellCloseFile(&Handle);
2890 return (EFI_NOT_FOUND);
2891}
2892
jcarsey125c2cf2009-11-18 21:36:50 +00002893/**
jcarsey36a9d672009-11-20 21:13:41 +00002894 Function to determine if a given filename represents a file.
2895
2896 @param[in] Name Path to file to test.
2897
2898 @retval EFI_SUCCESS The Path represents a file.
2899 @retval EFI_NOT_FOUND The Path does not represent a file.
2900 @retval other The path failed to open.
2901**/
2902EFI_STATUS
2903EFIAPI
2904ShellIsFile(
2905 IN CONST CHAR16 *Name
2906 )
2907{
2908 EFI_STATUS Status;
jcarseya405b862010-09-14 05:18:09 +00002909 SHELL_FILE_HANDLE Handle;
jcarsey36a9d672009-11-20 21:13:41 +00002910
jcarseyecd3d592009-12-07 18:05:00 +00002911 ASSERT(Name != NULL);
2912
jcarsey36a9d672009-11-20 21:13:41 +00002913 Handle = NULL;
2914
2915 Status = ShellOpenFileByName(Name, &Handle, EFI_FILE_MODE_READ, 0);
2916 if (EFI_ERROR(Status)) {
2917 return (Status);
2918 }
2919
2920 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {
2921 ShellCloseFile(&Handle);
2922 return (EFI_SUCCESS);
2923 }
2924 ShellCloseFile(&Handle);
2925 return (EFI_NOT_FOUND);
2926}
2927
2928/**
jcarseyb3011f42010-01-11 21:49:04 +00002929 Function to determine if a given filename represents a file.
2930
2931 This will search the CWD and then the Path.
2932
2933 If Name is NULL, then ASSERT.
2934
2935 @param[in] Name Path to file to test.
2936
2937 @retval EFI_SUCCESS The Path represents a file.
2938 @retval EFI_NOT_FOUND The Path does not represent a file.
2939 @retval other The path failed to open.
2940**/
2941EFI_STATUS
2942EFIAPI
2943ShellIsFileInPath(
2944 IN CONST CHAR16 *Name
jcarseya405b862010-09-14 05:18:09 +00002945 )
2946{
jcarseyb3011f42010-01-11 21:49:04 +00002947 CHAR16 *NewName;
2948 EFI_STATUS Status;
2949
2950 if (!EFI_ERROR(ShellIsFile(Name))) {
jcarseya405b862010-09-14 05:18:09 +00002951 return (EFI_SUCCESS);
jcarseyb3011f42010-01-11 21:49:04 +00002952 }
2953
2954 NewName = ShellFindFilePath(Name);
2955 if (NewName == NULL) {
2956 return (EFI_NOT_FOUND);
2957 }
2958 Status = ShellIsFile(NewName);
2959 FreePool(NewName);
2960 return (Status);
2961}
jcarsey252d9452011-03-25 20:49:53 +00002962
jcarseyb3011f42010-01-11 21:49:04 +00002963/**
jcarsey1e6e84c2010-01-25 20:05:08 +00002964 Function to determine whether a string is decimal or hex representation of a number
jcarsey125c2cf2009-11-18 21:36:50 +00002965 and return the number converted from the string.
2966
2967 @param[in] String String representation of a number
2968
jcarsey252d9452011-03-25 20:49:53 +00002969 @return the number
2970 @retval (UINTN)(-1) An error ocurred.
jcarsey125c2cf2009-11-18 21:36:50 +00002971**/
2972UINTN
2973EFIAPI
2974ShellStrToUintn(
2975 IN CONST CHAR16 *String
2976 )
2977{
jcarsey252d9452011-03-25 20:49:53 +00002978 UINT64 RetVal;
2979 BOOLEAN Hex;
2980
2981 Hex = FALSE;
2982
2983 if (!InternalShellIsHexOrDecimalNumber(String, Hex, TRUE)) {
2984 Hex = TRUE;
jcarsey125c2cf2009-11-18 21:36:50 +00002985 }
jcarsey252d9452011-03-25 20:49:53 +00002986
2987 if (!EFI_ERROR(ShellConvertStringToUint64(String, &RetVal, Hex, TRUE))) {
2988 return ((UINTN)RetVal);
2989 }
2990 return ((UINTN)(-1));
jcarsey125c2cf2009-11-18 21:36:50 +00002991}
2992
2993/**
2994 Safely append with automatic string resizing given length of Destination and
2995 desired length of copy from Source.
2996
2997 append the first D characters of Source to the end of Destination, where D is
2998 the lesser of Count and the StrLen() of Source. If appending those D characters
2999 will fit within Destination (whose Size is given as CurrentSize) and
jcarsey1e6e84c2010-01-25 20:05:08 +00003000 still leave room for a NULL terminator, then those characters are appended,
3001 starting at the original terminating NULL of Destination, and a new terminating
3002 NULL is appended.
jcarsey125c2cf2009-11-18 21:36:50 +00003003
3004 If appending D characters onto Destination will result in a overflow of the size
3005 given in CurrentSize the string will be grown such that the copy can be performed
3006 and CurrentSize will be updated to the new size.
3007
3008 If Source is NULL, there is nothing to append, just return the current buffer in
3009 Destination.
3010
3011 if Destination is NULL, then ASSERT()
3012 if Destination's current length (including NULL terminator) is already more then
3013 CurrentSize, then ASSERT()
3014
3015 @param[in,out] Destination The String to append onto
3016 @param[in,out] CurrentSize on call the number of bytes in Destination. On
3017 return possibly the new size (still in bytes). if NULL
3018 then allocate whatever is needed.
3019 @param[in] Source The String to append from
3020 @param[in] Count Maximum number of characters to append. if 0 then
3021 all are appended.
3022
3023 @return Destination return the resultant string.
3024**/
3025CHAR16*
3026EFIAPI
3027StrnCatGrow (
3028 IN OUT CHAR16 **Destination,
3029 IN OUT UINTN *CurrentSize,
3030 IN CONST CHAR16 *Source,
3031 IN UINTN Count
3032 )
3033{
3034 UINTN DestinationStartSize;
3035 UINTN NewSize;
3036
3037 //
3038 // ASSERTs
3039 //
3040 ASSERT(Destination != NULL);
3041
3042 //
3043 // If there's nothing to do then just return Destination
3044 //
3045 if (Source == NULL) {
3046 return (*Destination);
3047 }
3048
3049 //
3050 // allow for un-initialized pointers, based on size being 0
3051 //
3052 if (CurrentSize != NULL && *CurrentSize == 0) {
3053 *Destination = NULL;
3054 }
3055
3056 //
3057 // allow for NULL pointers address as Destination
3058 //
3059 if (*Destination != NULL) {
3060 ASSERT(CurrentSize != 0);
3061 DestinationStartSize = StrSize(*Destination);
3062 ASSERT(DestinationStartSize <= *CurrentSize);
3063 } else {
3064 DestinationStartSize = 0;
3065// ASSERT(*CurrentSize == 0);
3066 }
3067
3068 //
3069 // Append all of Source?
3070 //
3071 if (Count == 0) {
3072 Count = StrLen(Source);
3073 }
3074
3075 //
3076 // Test and grow if required
3077 //
3078 if (CurrentSize != NULL) {
3079 NewSize = *CurrentSize;
3080 while (NewSize < (DestinationStartSize + (Count*sizeof(CHAR16)))) {
3081 NewSize += 2 * Count * sizeof(CHAR16);
3082 }
3083 *Destination = ReallocatePool(*CurrentSize, NewSize, *Destination);
jcarseyc9d92df2010-02-03 15:37:54 +00003084 ASSERT(*Destination != NULL);
jcarsey125c2cf2009-11-18 21:36:50 +00003085 *CurrentSize = NewSize;
3086 } else {
3087 *Destination = AllocateZeroPool((Count+1)*sizeof(CHAR16));
jcarseyc9d92df2010-02-03 15:37:54 +00003088 ASSERT(*Destination != NULL);
jcarsey125c2cf2009-11-18 21:36:50 +00003089 }
3090
3091 //
3092 // Now use standard StrnCat on a big enough buffer
3093 //
jcarseyc9d92df2010-02-03 15:37:54 +00003094 if (*Destination == NULL) {
3095 return (NULL);
3096 }
jcarsey125c2cf2009-11-18 21:36:50 +00003097 return StrnCat(*Destination, Source, Count);
3098}
jcarseyc9d92df2010-02-03 15:37:54 +00003099
3100/**
3101 Prompt the user and return the resultant answer to the requestor.
3102
3103 This function will display the requested question on the shell prompt and then
3104 wait for an apropriate answer to be input from the console.
3105
jcarseya405b862010-09-14 05:18:09 +00003106 if the SHELL_PROMPT_REQUEST_TYPE is SHELL_PROMPT_REQUEST_TYPE_YESNO, ShellPromptResponseTypeQuitContinue
jcarseyc9d92df2010-02-03 15:37:54 +00003107 or SHELL_PROMPT_REQUEST_TYPE_YESNOCANCEL then *Response is of type SHELL_PROMPT_RESPONSE.
3108
jcarseya405b862010-09-14 05:18:09 +00003109 if the SHELL_PROMPT_REQUEST_TYPE is ShellPromptResponseTypeFreeform then *Response is of type
jcarseyc9d92df2010-02-03 15:37:54 +00003110 CHAR16*.
3111
3112 In either case *Response must be callee freed if Response was not NULL;
3113
3114 @param Type What type of question is asked. This is used to filter the input
3115 to prevent invalid answers to question.
3116 @param Prompt Pointer to string prompt to use to request input.
3117 @param Response Pointer to Response which will be populated upon return.
3118
3119 @retval EFI_SUCCESS The operation was sucessful.
3120 @retval EFI_UNSUPPORTED The operation is not supported as requested.
3121 @retval EFI_INVALID_PARAMETER A parameter was invalid.
3122 @return other The operation failed.
3123**/
3124EFI_STATUS
3125EFIAPI
3126ShellPromptForResponse (
3127 IN SHELL_PROMPT_REQUEST_TYPE Type,
3128 IN CHAR16 *Prompt OPTIONAL,
3129 IN OUT VOID **Response OPTIONAL
3130 )
3131{
3132 EFI_STATUS Status;
3133 EFI_INPUT_KEY Key;
3134 UINTN EventIndex;
3135 SHELL_PROMPT_RESPONSE *Resp;
jcarseya405b862010-09-14 05:18:09 +00003136 UINTN Size;
3137 CHAR16 *Buffer;
jcarseyc9d92df2010-02-03 15:37:54 +00003138
jcarseya405b862010-09-14 05:18:09 +00003139 Status = EFI_UNSUPPORTED;
3140 Resp = NULL;
3141 Buffer = NULL;
3142 Size = 0;
3143 if (Type != ShellPromptResponseTypeFreeform) {
jcarsey252d9452011-03-25 20:49:53 +00003144 Resp = (SHELL_PROMPT_RESPONSE*)AllocateZeroPool(sizeof(SHELL_PROMPT_RESPONSE));
jcarsey3e082d52010-10-04 16:44:57 +00003145 if (Resp == NULL) {
3146 return (EFI_OUT_OF_RESOURCES);
3147 }
xdu290bfa222010-07-19 05:21:27 +00003148 }
jcarseyc9d92df2010-02-03 15:37:54 +00003149
3150 switch(Type) {
jcarseya405b862010-09-14 05:18:09 +00003151 case ShellPromptResponseTypeQuitContinue:
jcarseyc9d92df2010-02-03 15:37:54 +00003152 if (Prompt != NULL) {
3153 ShellPrintEx(-1, -1, L"%s", Prompt);
3154 }
3155 //
3156 // wait for valid response
3157 //
3158 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
3159 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
3160 ASSERT_EFI_ERROR(Status);
3161 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3162 if (Key.UnicodeChar == L'Q' || Key.UnicodeChar ==L'q') {
jcarseya405b862010-09-14 05:18:09 +00003163 *Resp = ShellPromptResponseQuit;
jcarseyc9d92df2010-02-03 15:37:54 +00003164 } else {
jcarseya405b862010-09-14 05:18:09 +00003165 *Resp = ShellPromptResponseContinue;
jcarseyc9d92df2010-02-03 15:37:54 +00003166 }
3167 break;
jcarseya405b862010-09-14 05:18:09 +00003168 case ShellPromptResponseTypeYesNoCancel:
jcarseyc9d92df2010-02-03 15:37:54 +00003169 if (Prompt != NULL) {
3170 ShellPrintEx(-1, -1, L"%s", Prompt);
3171 }
3172 //
3173 // wait for valid response
3174 //
jcarseya405b862010-09-14 05:18:09 +00003175 *Resp = ShellPromptResponseMax;
3176 while (*Resp == ShellPromptResponseMax) {
jcarseyc9d92df2010-02-03 15:37:54 +00003177 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
3178 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
3179 ASSERT_EFI_ERROR(Status);
3180 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3181 switch (Key.UnicodeChar) {
3182 case L'Y':
3183 case L'y':
jcarseya405b862010-09-14 05:18:09 +00003184 *Resp = ShellPromptResponseYes;
jcarseyc9d92df2010-02-03 15:37:54 +00003185 break;
3186 case L'N':
3187 case L'n':
jcarseya405b862010-09-14 05:18:09 +00003188 *Resp = ShellPromptResponseNo;
jcarseyc9d92df2010-02-03 15:37:54 +00003189 break;
3190 case L'C':
3191 case L'c':
jcarseya405b862010-09-14 05:18:09 +00003192 *Resp = ShellPromptResponseCancel;
3193 break;
3194 }
3195 }
3196 break; case ShellPromptResponseTypeYesNoAllCancel:
3197 if (Prompt != NULL) {
3198 ShellPrintEx(-1, -1, L"%s", Prompt);
3199 }
3200 //
3201 // wait for valid response
3202 //
3203 *Resp = ShellPromptResponseMax;
3204 while (*Resp == ShellPromptResponseMax) {
3205 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
3206 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
3207 ASSERT_EFI_ERROR(Status);
3208 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3209 switch (Key.UnicodeChar) {
3210 case L'Y':
3211 case L'y':
3212 *Resp = ShellPromptResponseYes;
3213 break;
3214 case L'N':
3215 case L'n':
3216 *Resp = ShellPromptResponseNo;
3217 break;
3218 case L'A':
3219 case L'a':
3220 *Resp = ShellPromptResponseAll;
3221 break;
3222 case L'C':
3223 case L'c':
3224 *Resp = ShellPromptResponseCancel;
jcarseyc9d92df2010-02-03 15:37:54 +00003225 break;
3226 }
3227 }
3228 break;
jcarseya405b862010-09-14 05:18:09 +00003229 case ShellPromptResponseTypeEnterContinue:
3230 case ShellPromptResponseTypeAnyKeyContinue:
jcarseyc9d92df2010-02-03 15:37:54 +00003231 if (Prompt != NULL) {
3232 ShellPrintEx(-1, -1, L"%s", Prompt);
3233 }
3234 //
3235 // wait for valid response
3236 //
jcarseya405b862010-09-14 05:18:09 +00003237 *Resp = ShellPromptResponseMax;
3238 while (*Resp == ShellPromptResponseMax) {
jcarseyc9d92df2010-02-03 15:37:54 +00003239 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
jcarseya405b862010-09-14 05:18:09 +00003240 if (Type == ShellPromptResponseTypeEnterContinue) {
jcarseyc9d92df2010-02-03 15:37:54 +00003241 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
3242 ASSERT_EFI_ERROR(Status);
3243 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3244 if (Key.UnicodeChar == CHAR_CARRIAGE_RETURN) {
jcarseya405b862010-09-14 05:18:09 +00003245 *Resp = ShellPromptResponseContinue;
jcarseyc9d92df2010-02-03 15:37:54 +00003246 break;
3247 }
3248 }
jcarseya405b862010-09-14 05:18:09 +00003249 if (Type == ShellPromptResponseTypeAnyKeyContinue) {
3250 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
3251 ASSERT_EFI_ERROR(Status);
3252 *Resp = ShellPromptResponseContinue;
jcarseyc9d92df2010-02-03 15:37:54 +00003253 break;
3254 }
3255 }
3256 break;
jcarseya405b862010-09-14 05:18:09 +00003257 case ShellPromptResponseTypeYesNo:
3258 if (Prompt != NULL) {
3259 ShellPrintEx(-1, -1, L"%s", Prompt);
3260 }
3261 //
3262 // wait for valid response
3263 //
3264 *Resp = ShellPromptResponseMax;
3265 while (*Resp == ShellPromptResponseMax) {
3266 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
3267 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
3268 ASSERT_EFI_ERROR(Status);
3269 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3270 switch (Key.UnicodeChar) {
3271 case L'Y':
3272 case L'y':
3273 *Resp = ShellPromptResponseYes;
3274 break;
3275 case L'N':
3276 case L'n':
3277 *Resp = ShellPromptResponseNo;
3278 break;
3279 }
3280 }
3281 break;
3282 case ShellPromptResponseTypeFreeform:
3283 if (Prompt != NULL) {
3284 ShellPrintEx(-1, -1, L"%s", Prompt);
3285 }
3286 while(1) {
3287 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
3288 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
3289 ASSERT_EFI_ERROR(Status);
3290 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3291 if (Key.UnicodeChar == CHAR_CARRIAGE_RETURN) {
3292 break;
3293 }
3294 ASSERT((Buffer == NULL && Size == 0) || (Buffer != NULL));
3295 StrnCatGrow(&Buffer, &Size, &Key.UnicodeChar, 1);
3296 }
3297 break;
3298 //
3299 // This is the location to add new prompt types.
3300 //
jcarseyc9d92df2010-02-03 15:37:54 +00003301 default:
jcarseya405b862010-09-14 05:18:09 +00003302 ASSERT(FALSE);
jcarseyc9d92df2010-02-03 15:37:54 +00003303 }
3304
3305 if (Response != NULL) {
jcarseya405b862010-09-14 05:18:09 +00003306 if (Resp != NULL) {
3307 *Response = Resp;
3308 } else if (Buffer != NULL) {
3309 *Response = Buffer;
3310 }
jcarseyc9d92df2010-02-03 15:37:54 +00003311 } else {
jcarseya405b862010-09-14 05:18:09 +00003312 if (Resp != NULL) {
3313 FreePool(Resp);
3314 }
3315 if (Buffer != NULL) {
3316 FreePool(Buffer);
3317 }
jcarseyc9d92df2010-02-03 15:37:54 +00003318 }
3319
jcarseya405b862010-09-14 05:18:09 +00003320 ShellPrintEx(-1, -1, L"\r\n");
jcarseyc9d92df2010-02-03 15:37:54 +00003321 return (Status);
3322}
3323
3324/**
3325 Prompt the user and return the resultant answer to the requestor.
3326
3327 This function is the same as ShellPromptForResponse, except that the prompt is
3328 automatically pulled from HII.
3329
3330 @param Type What type of question is asked. This is used to filter the input
3331 to prevent invalid answers to question.
jcarseya405b862010-09-14 05:18:09 +00003332 @param[in] HiiFormatStringId The format string Id for getting from Hii.
3333 @param[in] HiiFormatHandle The format string Handle for getting from Hii.
3334 @param Response Pointer to Response which will be populated upon return.
jcarseyc9d92df2010-02-03 15:37:54 +00003335
3336 @retval EFI_SUCCESS the operation was sucessful.
3337 @return other the operation failed.
3338
3339 @sa ShellPromptForResponse
3340**/
3341EFI_STATUS
3342EFIAPI
3343ShellPromptForResponseHii (
3344 IN SHELL_PROMPT_REQUEST_TYPE Type,
3345 IN CONST EFI_STRING_ID HiiFormatStringId,
3346 IN CONST EFI_HANDLE HiiFormatHandle,
3347 IN OUT VOID **Response
3348 )
3349{
3350 CHAR16 *Prompt;
3351 EFI_STATUS Status;
3352
3353 Prompt = HiiGetString(HiiFormatHandle, HiiFormatStringId, NULL);
3354 Status = ShellPromptForResponse(Type, Prompt, Response);
3355 FreePool(Prompt);
3356 return (Status);
3357}
3358
jcarseya405b862010-09-14 05:18:09 +00003359/**
3360 Function to determin if an entire string is a valid number.
jcarseyc9d92df2010-02-03 15:37:54 +00003361
jcarseya405b862010-09-14 05:18:09 +00003362 If Hex it must be preceeded with a 0x or has ForceHex, set TRUE.
3363
3364 @param[in] String The string to evaluate.
3365 @param[in] ForceHex TRUE - always assume hex.
3366 @param[in] StopAtSpace TRUE to halt upon finding a space, FALSE to keep going.
3367
3368 @retval TRUE It is all numeric (dec/hex) characters.
3369 @retval FALSE There is a non-numeric character.
3370**/
3371BOOLEAN
3372EFIAPI
jcarsey252d9452011-03-25 20:49:53 +00003373InternalShellIsHexOrDecimalNumber (
jcarseya405b862010-09-14 05:18:09 +00003374 IN CONST CHAR16 *String,
3375 IN CONST BOOLEAN ForceHex,
3376 IN CONST BOOLEAN StopAtSpace
3377 )
3378{
3379 BOOLEAN Hex;
3380
3381 //
3382 // chop off a single negative sign
3383 //
3384 if (String != NULL && *String == L'-') {
3385 String++;
3386 }
3387
3388 if (String == NULL) {
3389 return (FALSE);
3390 }
3391
3392 //
3393 // chop leading zeroes
3394 //
3395 while(String != NULL && *String == L'0'){
3396 String++;
3397 }
3398 //
3399 // allow '0x' or '0X', but not 'x' or 'X'
3400 //
3401 if (String != NULL && (*String == L'x' || *String == L'X')) {
3402 if (*(String-1) != L'0') {
3403 //
3404 // we got an x without a preceeding 0
3405 //
3406 return (FALSE);
3407 }
3408 String++;
3409 Hex = TRUE;
3410 } else if (ForceHex) {
3411 Hex = TRUE;
3412 } else {
3413 Hex = FALSE;
3414 }
3415
3416 //
3417 // loop through the remaining characters and use the lib function
3418 //
3419 for ( ; String != NULL && *String != CHAR_NULL && !(StopAtSpace && *String == L' ') ; String++){
3420 if (Hex) {
3421 if (!ShellIsHexaDecimalDigitCharacter(*String)) {
3422 return (FALSE);
3423 }
3424 } else {
3425 if (!ShellIsDecimalDigitCharacter(*String)) {
3426 return (FALSE);
3427 }
3428 }
3429 }
jcarsey252d9452011-03-25 20:49:53 +00003430
jcarseya405b862010-09-14 05:18:09 +00003431 return (TRUE);
3432}
3433
3434/**
3435 Function to determine if a given filename exists.
3436
3437 @param[in] Name Path to test.
3438
3439 @retval EFI_SUCCESS The Path represents a file.
3440 @retval EFI_NOT_FOUND The Path does not represent a file.
3441 @retval other The path failed to open.
3442**/
3443EFI_STATUS
3444EFIAPI
3445ShellFileExists(
3446 IN CONST CHAR16 *Name
3447 )
3448{
3449 EFI_STATUS Status;
3450 EFI_SHELL_FILE_INFO *List;
3451
3452 ASSERT(Name != NULL);
3453
3454 List = NULL;
3455 Status = ShellOpenFileMetaArg((CHAR16*)Name, EFI_FILE_MODE_READ, &List);
3456 if (EFI_ERROR(Status)) {
3457 return (Status);
3458 }
3459
3460 ShellCloseFileMetaArg(&List);
3461
3462 return (EFI_SUCCESS);
3463}
jcarsey252d9452011-03-25 20:49:53 +00003464
3465/**
3466 Convert a Unicode character to upper case only if
3467 it maps to a valid small-case ASCII character.
3468
3469 This internal function only deal with Unicode character
3470 which maps to a valid small-case ASCII character, i.e.
3471 L'a' to L'z'. For other Unicode character, the input character
3472 is returned directly.
3473
3474 @param Char The character to convert.
3475
3476 @retval LowerCharacter If the Char is with range L'a' to L'z'.
3477 @retval Unchanged Otherwise.
3478
3479**/
3480CHAR16
3481EFIAPI
3482InternalShellCharToUpper (
3483 IN CHAR16 Char
3484 )
3485{
3486 if (Char >= L'a' && Char <= L'z') {
3487 return (CHAR16) (Char - (L'a' - L'A'));
3488 }
3489
3490 return Char;
3491}
3492
3493/**
3494 Convert a Unicode character to numerical value.
3495
3496 This internal function only deal with Unicode character
3497 which maps to a valid hexadecimal ASII character, i.e.
3498 L'0' to L'9', L'a' to L'f' or L'A' to L'F'. For other
3499 Unicode character, the value returned does not make sense.
3500
3501 @param Char The character to convert.
3502
3503 @return The numerical value converted.
3504
3505**/
3506UINTN
3507EFIAPI
3508InternalShellHexCharToUintn (
3509 IN CHAR16 Char
3510 )
3511{
3512 if (ShellIsDecimalDigitCharacter (Char)) {
3513 return Char - L'0';
3514 }
3515
3516 return (UINTN) (10 + InternalShellCharToUpper (Char) - L'A');
3517}
3518
3519/**
3520 Convert a Null-terminated Unicode hexadecimal string to a value of type UINT64.
3521
3522 This function returns a value of type UINTN by interpreting the contents
3523 of the Unicode string specified by String as a hexadecimal number.
3524 The format of the input Unicode string String is:
3525
3526 [spaces][zeros][x][hexadecimal digits].
3527
3528 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
3529 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix.
3530 If "x" appears in the input string, it must be prefixed with at least one 0.
3531 The function will ignore the pad space, which includes spaces or tab characters,
3532 before [zeros], [x] or [hexadecimal digit]. The running zero before [x] or
3533 [hexadecimal digit] will be ignored. Then, the decoding starts after [x] or the
3534 first valid hexadecimal digit. Then, the function stops at the first character that is
3535 a not a valid hexadecimal character or NULL, whichever one comes first.
3536
3537 If String has only pad spaces, then zero is returned.
3538 If String has no leading pad spaces, leading zeros or valid hexadecimal digits,
3539 then zero is returned.
3540
3541 @param[in] String A pointer to a Null-terminated Unicode string.
3542 @param[out] Value Upon a successful return the value of the conversion.
3543 @param[in] StopAtSpace FALSE to skip spaces.
3544
3545 @retval EFI_SUCCESS The conversion was successful.
3546 @retval EFI_INVALID_PARAMETER A parameter was NULL or invalid.
3547 @retval EFI_DEVICE_ERROR An overflow occured.
3548**/
3549EFI_STATUS
3550EFIAPI
3551InternalShellStrHexToUint64 (
3552 IN CONST CHAR16 *String,
3553 OUT UINT64 *Value,
3554 IN CONST BOOLEAN StopAtSpace
3555 )
3556{
3557 UINT64 Result;
3558
3559 if (String == NULL || StrSize(String) == 0 || Value == NULL) {
3560 return (EFI_INVALID_PARAMETER);
3561 }
3562
3563 //
3564 // Ignore the pad spaces (space or tab)
3565 //
3566 while ((*String == L' ') || (*String == L'\t')) {
3567 String++;
3568 }
3569
3570 //
3571 // Ignore leading Zeros after the spaces
3572 //
3573 while (*String == L'0') {
3574 String++;
3575 }
3576
3577 if (InternalShellCharToUpper (*String) == L'X') {
3578 if (*(String - 1) != L'0') {
3579 return 0;
3580 }
3581 //
3582 // Skip the 'X'
3583 //
3584 String++;
3585 }
3586
3587 Result = 0;
3588
3589 //
3590 // Skip spaces if requested
3591 //
3592 while (StopAtSpace && *String == L' ') {
3593 String++;
3594 }
3595
3596 while (ShellIsHexaDecimalDigitCharacter (*String)) {
3597 //
3598 // If the Hex Number represented by String overflows according
3599 // to the range defined by UINTN, then ASSERT().
3600 //
3601 if (!(Result <= (RShiftU64((((UINT64) ~0) - InternalShellHexCharToUintn (*String)), 4)))) {
3602// if (!(Result <= ((((UINT64) ~0) - InternalShellHexCharToUintn (*String)) >> 4))) {
3603 return (EFI_DEVICE_ERROR);
3604 }
3605
3606 Result = (LShiftU64(Result, 4));
3607 Result += InternalShellHexCharToUintn (*String);
3608 String++;
3609
3610 //
3611 // Skip spaces if requested
3612 //
3613 while (StopAtSpace && *String == L' ') {
3614 String++;
3615 }
3616 }
3617
3618 *Value = Result;
3619 return (EFI_SUCCESS);
3620}
3621
3622/**
3623 Convert a Null-terminated Unicode decimal string to a value of
3624 type UINT64.
3625
3626 This function returns a value of type UINT64 by interpreting the contents
3627 of the Unicode string specified by String as a decimal number. The format
3628 of the input Unicode string String is:
3629
3630 [spaces] [decimal digits].
3631
3632 The valid decimal digit character is in the range [0-9]. The
3633 function will ignore the pad space, which includes spaces or
3634 tab characters, before [decimal digits]. The running zero in the
3635 beginning of [decimal digits] will be ignored. Then, the function
3636 stops at the first character that is a not a valid decimal character
3637 or a Null-terminator, whichever one comes first.
3638
3639 If String has only pad spaces, then 0 is returned.
3640 If String has no pad spaces or valid decimal digits,
3641 then 0 is returned.
3642
3643 @param[in] String A pointer to a Null-terminated Unicode string.
3644 @param[out] Value Upon a successful return the value of the conversion.
3645 @param[in] StopAtSpace FALSE to skip spaces.
3646
3647 @retval EFI_SUCCESS The conversion was successful.
3648 @retval EFI_INVALID_PARAMETER A parameter was NULL or invalid.
3649 @retval EFI_DEVICE_ERROR An overflow occured.
3650**/
3651EFI_STATUS
3652EFIAPI
3653InternalShellStrDecimalToUint64 (
3654 IN CONST CHAR16 *String,
3655 OUT UINT64 *Value,
3656 IN CONST BOOLEAN StopAtSpace
3657 )
3658{
3659 UINT64 Result;
3660
3661 if (String == NULL || StrSize (String) == 0 || Value == NULL) {
3662 return (EFI_INVALID_PARAMETER);
3663 }
3664
3665 //
3666 // Ignore the pad spaces (space or tab)
3667 //
3668 while ((*String == L' ') || (*String == L'\t')) {
3669 String++;
3670 }
3671
3672 //
3673 // Ignore leading Zeros after the spaces
3674 //
3675 while (*String == L'0') {
3676 String++;
3677 }
3678
3679 Result = 0;
3680
3681 //
3682 // Skip spaces if requested
3683 //
3684 while (StopAtSpace && *String == L' ') {
3685 String++;
3686 }
3687 while (ShellIsDecimalDigitCharacter (*String)) {
3688 //
3689 // If the number represented by String overflows according
3690 // to the range defined by UINT64, then ASSERT().
3691 //
3692
3693 if (!(Result <= (DivU64x32((((UINT64) ~0) - (*String - L'0')),10)))) {
3694 return (EFI_DEVICE_ERROR);
3695 }
3696
3697 Result = MultU64x32(Result, 10) + (*String - L'0');
3698 String++;
3699
3700 //
3701 // Stop at spaces if requested
3702 //
3703 if (StopAtSpace && *String == L' ') {
3704 break;
3705 }
3706 }
3707
3708 *Value = Result;
3709
3710 return (EFI_SUCCESS);
3711}
3712
3713/**
3714 Function to verify and convert a string to its numerical value.
3715
3716 If Hex it must be preceeded with a 0x, 0X, or has ForceHex set TRUE.
3717
3718 @param[in] String The string to evaluate.
3719 @param[out] Value Upon a successful return the value of the conversion.
3720 @param[in] ForceHex TRUE - always assume hex.
3721 @param[in] StopAtSpace FALSE to skip spaces.
3722
3723 @retval EFI_SUCCESS The conversion was successful.
3724 @retval EFI_INVALID_PARAMETER String contained an invalid character.
3725 @retval EFI_NOT_FOUND String was a number, but Value was NULL.
3726**/
3727EFI_STATUS
3728EFIAPI
3729ShellConvertStringToUint64(
3730 IN CONST CHAR16 *String,
3731 OUT UINT64 *Value,
3732 IN CONST BOOLEAN ForceHex,
3733 IN CONST BOOLEAN StopAtSpace
3734 )
3735{
3736 UINT64 RetVal;
3737 CONST CHAR16 *Walker;
3738 EFI_STATUS Status;
3739 BOOLEAN Hex;
3740
3741 Hex = ForceHex;
3742
3743 if (!InternalShellIsHexOrDecimalNumber(String, Hex, StopAtSpace)) {
3744 if (!Hex) {
3745 Hex = TRUE;
3746 if (!InternalShellIsHexOrDecimalNumber(String, Hex, StopAtSpace)) {
3747 return (EFI_INVALID_PARAMETER);
3748 }
3749 } else {
3750 return (EFI_INVALID_PARAMETER);
3751 }
3752 }
3753
3754 //
3755 // Chop off leading spaces
3756 //
3757 for (Walker = String; Walker != NULL && *Walker != CHAR_NULL && *Walker == L' '; Walker++);
3758
3759 //
3760 // make sure we have something left that is numeric.
3761 //
3762 if (Walker == NULL || *Walker == CHAR_NULL || !InternalShellIsHexOrDecimalNumber(Walker, Hex, StopAtSpace)) {
3763 return (EFI_INVALID_PARAMETER);
3764 }
3765
3766 //
3767 // do the conversion.
3768 //
3769 if (Hex || StrnCmp(Walker, L"0x", 2) == 0 || StrnCmp(Walker, L"0X", 2) == 0){
3770 Status = InternalShellStrHexToUint64(Walker, &RetVal, StopAtSpace);
3771 } else {
3772 Status = InternalShellStrDecimalToUint64(Walker, &RetVal, StopAtSpace);
3773 }
3774
3775 if (Value == NULL && !EFI_ERROR(Status)) {
3776 return (EFI_NOT_FOUND);
3777 }
3778
3779 if (Value != NULL) {
3780 *Value = RetVal;
3781 }
3782
3783 return (Status);
3784}
3785
3786/**
3787 Function to determin if an entire string is a valid number.
3788
3789 If Hex it must be preceeded with a 0x or has ForceHex, set TRUE.
3790
3791 @param[in] String The string to evaluate.
3792 @param[in] ForceHex TRUE - always assume hex.
3793 @param[in] StopAtSpace TRUE to halt upon finding a space, FALSE to keep going.
3794
3795 @retval TRUE It is all numeric (dec/hex) characters.
3796 @retval FALSE There is a non-numeric character.
3797**/
3798BOOLEAN
3799EFIAPI
3800ShellIsHexOrDecimalNumber (
3801 IN CONST CHAR16 *String,
3802 IN CONST BOOLEAN ForceHex,
3803 IN CONST BOOLEAN StopAtSpace
3804 )
3805{
3806 if (ShellConvertStringToUint64(String, NULL, ForceHex, StopAtSpace) == EFI_NOT_FOUND) {
3807 return (TRUE);
3808 }
3809 return (FALSE);
3810}
jcarsey4d0a4fc2011-07-06 22:28:36 +00003811
3812/**
3813 Function to read a single line from a SHELL_FILE_HANDLE. The \n is not included in the returned
3814 buffer. The returned buffer must be callee freed.
3815
3816 If the position upon start is 0, then the Ascii Boolean will be set. This should be
3817 maintained and not changed for all operations with the same file.
3818
3819 @param[in] Handle SHELL_FILE_HANDLE to read from.
3820 @param[in,out] Ascii Boolean value for indicating whether the file is
3821 Ascii (TRUE) or UCS2 (FALSE).
3822
3823 @return The line of text from the file.
3824
3825 @sa ShellFileHandleReadLine
3826**/
3827CHAR16*
3828EFIAPI
3829ShellFileHandleReturnLine(
3830 IN SHELL_FILE_HANDLE Handle,
3831 IN OUT BOOLEAN *Ascii
3832 )
3833{
3834 CHAR16 *RetVal;
3835 UINTN Size;
3836 EFI_STATUS Status;
3837
3838 Size = 0;
3839 RetVal = NULL;
3840
3841 Status = ShellFileHandleReadLine(Handle, RetVal, &Size, FALSE, Ascii);
3842 if (Status == EFI_BUFFER_TOO_SMALL) {
3843 RetVal = AllocateZeroPool(Size);
3844 Status = ShellFileHandleReadLine(Handle, RetVal, &Size, FALSE, Ascii);
3845 }
3846 ASSERT_EFI_ERROR(Status);
3847 if (EFI_ERROR(Status) && (RetVal != NULL)) {
3848 FreePool(RetVal);
3849 RetVal = NULL;
3850 }
3851 return (RetVal);
3852}
3853
3854/**
3855 Function to read a single line (up to but not including the \n) from a SHELL_FILE_HANDLE.
3856
3857 If the position upon start is 0, then the Ascii Boolean will be set. This should be
3858 maintained and not changed for all operations with the same file.
3859
3860 @param[in] Handle SHELL_FILE_HANDLE to read from.
3861 @param[in,out] Buffer The pointer to buffer to read into.
3862 @param[in,out] Size The pointer to number of bytes in Buffer.
3863 @param[in] Truncate If the buffer is large enough, this has no effect.
3864 If the buffer is is too small and Truncate is TRUE,
3865 the line will be truncated.
3866 If the buffer is is too small and Truncate is FALSE,
3867 then no read will occur.
3868
3869 @param[in,out] Ascii Boolean value for indicating whether the file is
3870 Ascii (TRUE) or UCS2 (FALSE).
3871
3872 @retval EFI_SUCCESS The operation was successful. The line is stored in
3873 Buffer.
3874 @retval EFI_INVALID_PARAMETER Handle was NULL.
3875 @retval EFI_INVALID_PARAMETER Size was NULL.
3876 @retval EFI_BUFFER_TOO_SMALL Size was not large enough to store the line.
3877 Size was updated to the minimum space required.
3878**/
3879EFI_STATUS
3880EFIAPI
3881ShellFileHandleReadLine(
3882 IN SHELL_FILE_HANDLE Handle,
3883 IN OUT CHAR16 *Buffer,
3884 IN OUT UINTN *Size,
3885 IN BOOLEAN Truncate,
3886 IN OUT BOOLEAN *Ascii
3887 )
3888{
3889 EFI_STATUS Status;
3890 CHAR16 CharBuffer;
3891 UINTN CharSize;
3892 UINTN CountSoFar;
3893 UINT64 OriginalFilePosition;
3894
3895
3896 if (Handle == NULL
3897 ||Size == NULL
3898 ){
3899 return (EFI_INVALID_PARAMETER);
3900 }
3901 if (Buffer == NULL) {
3902 ASSERT(*Size == 0);
3903 } else {
3904 *Buffer = CHAR_NULL;
3905 }
3906 gEfiShellProtocol->GetFilePosition(Handle, &OriginalFilePosition);
3907 if (OriginalFilePosition == 0) {
3908 CharSize = sizeof(CHAR16);
3909 Status = gEfiShellProtocol->ReadFile(Handle, &CharSize, &CharBuffer);
3910 ASSERT_EFI_ERROR(Status);
3911 if (CharBuffer == gUnicodeFileTag) {
3912 *Ascii = FALSE;
3913 } else {
3914 *Ascii = TRUE;
3915 gEfiShellProtocol->SetFilePosition(Handle, OriginalFilePosition);
3916 }
3917 }
3918
3919 for (CountSoFar = 0;;CountSoFar++){
3920 CharBuffer = 0;
3921 if (*Ascii) {
3922 CharSize = sizeof(CHAR8);
3923 } else {
3924 CharSize = sizeof(CHAR16);
3925 }
3926 Status = gEfiShellProtocol->ReadFile(Handle, &CharSize, &CharBuffer);
3927 if ( EFI_ERROR(Status)
3928 || CharSize == 0
3929 || (CharBuffer == L'\n' && !(*Ascii))
3930 || (CharBuffer == '\n' && *Ascii)
3931 ){
3932 break;
3933 }
3934 //
3935 // if we have space save it...
3936 //
3937 if ((CountSoFar+1)*sizeof(CHAR16) < *Size){
3938 ASSERT(Buffer != NULL);
3939 ((CHAR16*)Buffer)[CountSoFar] = CharBuffer;
3940 ((CHAR16*)Buffer)[CountSoFar+1] = CHAR_NULL;
3941 }
3942 }
3943
3944 //
3945 // if we ran out of space tell when...
3946 //
3947 if ((CountSoFar+1)*sizeof(CHAR16) > *Size){
3948 *Size = (CountSoFar+1)*sizeof(CHAR16);
3949 if (!Truncate) {
3950 gEfiShellProtocol->SetFilePosition(Handle, OriginalFilePosition);
3951 } else {
3952 DEBUG((DEBUG_WARN, "The line was truncated in ShellFileHandleReadLine"));
3953 }
3954 return (EFI_BUFFER_TOO_SMALL);
3955 }
3956 while(Buffer[StrLen(Buffer)-1] == L'\r') {
3957 Buffer[StrLen(Buffer)-1] = CHAR_NULL;
3958 }
3959
3960 return (Status);
3961}