blob: 5e03400aebae112815e339a7499f49594219a49a [file] [log] [blame]
jcarsey94b17fa2009-05-07 18:46:18 +00001/** @file
2 Provides interface to shell functionality for shell commands and applications.
3
Jaben Carsey75a5e2e2014-01-10 16:42:45 +00004 Copyright (c) 2006 - 2014, 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.
jcarseybeab0fc2011-10-10 17:26:25 +000087
88 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.
jcarsey94b17fa2009-05-07 18:46:18 +000089**/
90EFI_STATUS
91EFIAPI
92ShellFindSE2 (
93 IN EFI_HANDLE ImageHandle
jcarseya405b862010-09-14 05:18:09 +000094 )
95{
jcarsey94b17fa2009-05-07 18:46:18 +000096 EFI_STATUS Status;
97 EFI_HANDLE *Buffer;
98 UINTN BufferSize;
99 UINTN HandleIndex;
100
101 BufferSize = 0;
102 Buffer = NULL;
jcarsey1e6e84c2010-01-25 20:05:08 +0000103 Status = gBS->OpenProtocol(ImageHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000104 &gEfiShellEnvironment2Guid,
105 (VOID **)&mEfiShellEnvironment2,
106 ImageHandle,
107 NULL,
108 EFI_OPEN_PROTOCOL_GET_PROTOCOL
jcarseya405b862010-09-14 05:18:09 +0000109 );
jcarsey94b17fa2009-05-07 18:46:18 +0000110 //
111 // look for the mEfiShellEnvironment2 protocol at a higher level
112 //
jcarseya405b862010-09-14 05:18:09 +0000113 if (EFI_ERROR (Status) || !(CompareGuid (&mEfiShellEnvironment2->SESGuid, &gEfiShellEnvironment2ExtGuid))){
jcarsey94b17fa2009-05-07 18:46:18 +0000114 //
115 // figure out how big of a buffer we need.
116 //
117 Status = gBS->LocateHandle (ByProtocol,
118 &gEfiShellEnvironment2Guid,
119 NULL, // ignored for ByProtocol
120 &BufferSize,
121 Buffer
jcarseya405b862010-09-14 05:18:09 +0000122 );
jcarsey2247dde2009-11-09 18:08:58 +0000123 //
124 // maybe it's not there???
125 //
126 if (Status == EFI_BUFFER_TOO_SMALL) {
jcarsey252d9452011-03-25 20:49:53 +0000127 Buffer = (EFI_HANDLE*)AllocateZeroPool(BufferSize);
jcarseybeab0fc2011-10-10 17:26:25 +0000128 if (Buffer == NULL) {
129 return (EFI_OUT_OF_RESOURCES);
130 }
jcarsey2247dde2009-11-09 18:08:58 +0000131 Status = gBS->LocateHandle (ByProtocol,
132 &gEfiShellEnvironment2Guid,
133 NULL, // ignored for ByProtocol
134 &BufferSize,
135 Buffer
jcarseya405b862010-09-14 05:18:09 +0000136 );
jcarsey2247dde2009-11-09 18:08:58 +0000137 }
jcarsey1cd45e72010-01-29 15:07:44 +0000138 if (!EFI_ERROR (Status) && Buffer != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +0000139 //
140 // now parse the list of returned handles
141 //
142 Status = EFI_NOT_FOUND;
143 for (HandleIndex = 0; HandleIndex < (BufferSize/sizeof(Buffer[0])); HandleIndex++) {
jcarsey1e6e84c2010-01-25 20:05:08 +0000144 Status = gBS->OpenProtocol(Buffer[HandleIndex],
jcarsey94b17fa2009-05-07 18:46:18 +0000145 &gEfiShellEnvironment2Guid,
146 (VOID **)&mEfiShellEnvironment2,
147 ImageHandle,
148 NULL,
149 EFI_OPEN_PROTOCOL_GET_PROTOCOL
jcarseya405b862010-09-14 05:18:09 +0000150 );
151 if (CompareGuid (&mEfiShellEnvironment2->SESGuid, &gEfiShellEnvironment2ExtGuid)) {
jcarsey94b17fa2009-05-07 18:46:18 +0000152 mEfiShellEnvironment2Handle = Buffer[HandleIndex];
153 Status = EFI_SUCCESS;
154 break;
155 }
156 }
157 }
158 }
159 if (Buffer != NULL) {
160 FreePool (Buffer);
161 }
162 return (Status);
163}
164
jcarsey252d9452011-03-25 20:49:53 +0000165/**
darylm503b0934ac2011-11-12 00:35:11 +0000166 Function to do most of the work of the constructor. Allows for calling
jcarseya405b862010-09-14 05:18:09 +0000167 multiple times without complete re-initialization.
168
169 @param[in] ImageHandle A copy of the ImageHandle.
170 @param[in] SystemTable A pointer to the SystemTable for the application.
jcarsey252d9452011-03-25 20:49:53 +0000171
172 @retval EFI_SUCCESS The operationw as successful.
jcarseya405b862010-09-14 05:18:09 +0000173**/
jcarsey94b17fa2009-05-07 18:46:18 +0000174EFI_STATUS
175EFIAPI
jcarseyd2b45642009-05-11 18:02:16 +0000176ShellLibConstructorWorker (
jcarsey94b17fa2009-05-07 18:46:18 +0000177 IN EFI_HANDLE ImageHandle,
178 IN EFI_SYSTEM_TABLE *SystemTable
jcarseya405b862010-09-14 05:18:09 +0000179 )
180{
181 EFI_STATUS Status;
jcarseyecd3d592009-12-07 18:05:00 +0000182
jcarsey94b17fa2009-05-07 18:46:18 +0000183 //
184 // UEFI 2.0 shell interfaces (used preferentially)
185 //
jcarseya405b862010-09-14 05:18:09 +0000186 Status = gBS->OpenProtocol(
187 ImageHandle,
188 &gEfiShellProtocolGuid,
jcarsey366f81a2011-06-27 21:04:22 +0000189 (VOID **)&gEfiShellProtocol,
jcarseya405b862010-09-14 05:18:09 +0000190 ImageHandle,
191 NULL,
192 EFI_OPEN_PROTOCOL_GET_PROTOCOL
193 );
jcarsey94b17fa2009-05-07 18:46:18 +0000194 if (EFI_ERROR(Status)) {
jcarseya405b862010-09-14 05:18:09 +0000195 //
196 // Search for the shell protocol
197 //
198 Status = gBS->LocateProtocol(
199 &gEfiShellProtocolGuid,
200 NULL,
jcarsey366f81a2011-06-27 21:04:22 +0000201 (VOID **)&gEfiShellProtocol
jcarseya405b862010-09-14 05:18:09 +0000202 );
203 if (EFI_ERROR(Status)) {
jcarsey366f81a2011-06-27 21:04:22 +0000204 gEfiShellProtocol = NULL;
jcarseya405b862010-09-14 05:18:09 +0000205 }
jcarsey94b17fa2009-05-07 18:46:18 +0000206 }
jcarseya405b862010-09-14 05:18:09 +0000207 Status = gBS->OpenProtocol(
208 ImageHandle,
209 &gEfiShellParametersProtocolGuid,
jcarsey366f81a2011-06-27 21:04:22 +0000210 (VOID **)&gEfiShellParametersProtocol,
jcarseya405b862010-09-14 05:18:09 +0000211 ImageHandle,
212 NULL,
213 EFI_OPEN_PROTOCOL_GET_PROTOCOL
214 );
jcarsey94b17fa2009-05-07 18:46:18 +0000215 if (EFI_ERROR(Status)) {
jcarsey366f81a2011-06-27 21:04:22 +0000216 gEfiShellParametersProtocol = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +0000217 }
218
jcarsey366f81a2011-06-27 21:04:22 +0000219 if (gEfiShellParametersProtocol == NULL || gEfiShellProtocol == NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +0000220 //
221 // Moved to seperate function due to complexity
222 //
223 Status = ShellFindSE2(ImageHandle);
224
225 if (EFI_ERROR(Status)) {
226 DEBUG((DEBUG_ERROR, "Status: 0x%08x\r\n", Status));
227 mEfiShellEnvironment2 = NULL;
228 }
jcarsey1e6e84c2010-01-25 20:05:08 +0000229 Status = gBS->OpenProtocol(ImageHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000230 &gEfiShellInterfaceGuid,
231 (VOID **)&mEfiShellInterface,
232 ImageHandle,
233 NULL,
234 EFI_OPEN_PROTOCOL_GET_PROTOCOL
jcarseya405b862010-09-14 05:18:09 +0000235 );
jcarsey94b17fa2009-05-07 18:46:18 +0000236 if (EFI_ERROR(Status)) {
237 mEfiShellInterface = NULL;
238 }
239 }
jcarseyc9d92df2010-02-03 15:37:54 +0000240
jcarsey94b17fa2009-05-07 18:46:18 +0000241 //
242 // only success getting 2 of either the old or new, but no 1/2 and 1/2
243 //
jcarsey1e6e84c2010-01-25 20:05:08 +0000244 if ((mEfiShellEnvironment2 != NULL && mEfiShellInterface != NULL) ||
jcarsey366f81a2011-06-27 21:04:22 +0000245 (gEfiShellProtocol != NULL && gEfiShellParametersProtocol != NULL) ) {
246 if (gEfiShellProtocol != NULL) {
247 FileFunctionMap.GetFileInfo = gEfiShellProtocol->GetFileInfo;
248 FileFunctionMap.SetFileInfo = gEfiShellProtocol->SetFileInfo;
249 FileFunctionMap.ReadFile = gEfiShellProtocol->ReadFile;
250 FileFunctionMap.WriteFile = gEfiShellProtocol->WriteFile;
251 FileFunctionMap.CloseFile = gEfiShellProtocol->CloseFile;
252 FileFunctionMap.DeleteFile = gEfiShellProtocol->DeleteFile;
253 FileFunctionMap.GetFilePosition = gEfiShellProtocol->GetFilePosition;
254 FileFunctionMap.SetFilePosition = gEfiShellProtocol->SetFilePosition;
255 FileFunctionMap.FlushFile = gEfiShellProtocol->FlushFile;
256 FileFunctionMap.GetFileSize = gEfiShellProtocol->GetFileSize;
jcarseyd2b45642009-05-11 18:02:16 +0000257 } else {
jcarseya405b862010-09-14 05:18:09 +0000258 FileFunctionMap.GetFileInfo = (EFI_SHELL_GET_FILE_INFO)FileHandleGetInfo;
259 FileFunctionMap.SetFileInfo = (EFI_SHELL_SET_FILE_INFO)FileHandleSetInfo;
260 FileFunctionMap.ReadFile = (EFI_SHELL_READ_FILE)FileHandleRead;
261 FileFunctionMap.WriteFile = (EFI_SHELL_WRITE_FILE)FileHandleWrite;
262 FileFunctionMap.CloseFile = (EFI_SHELL_CLOSE_FILE)FileHandleClose;
263 FileFunctionMap.DeleteFile = (EFI_SHELL_DELETE_FILE)FileHandleDelete;
264 FileFunctionMap.GetFilePosition = (EFI_SHELL_GET_FILE_POSITION)FileHandleGetPosition;
265 FileFunctionMap.SetFilePosition = (EFI_SHELL_SET_FILE_POSITION)FileHandleSetPosition;
266 FileFunctionMap.FlushFile = (EFI_SHELL_FLUSH_FILE)FileHandleFlush;
267 FileFunctionMap.GetFileSize = (EFI_SHELL_GET_FILE_SIZE)FileHandleGetSize;
jcarseyd2b45642009-05-11 18:02:16 +0000268 }
jcarsey94b17fa2009-05-07 18:46:18 +0000269 return (EFI_SUCCESS);
270 }
271 return (EFI_NOT_FOUND);
272}
jcarseyd2b45642009-05-11 18:02:16 +0000273/**
274 Constructor for the Shell library.
275
276 Initialize the library and determine if the underlying is a UEFI Shell 2.0 or an EFI shell.
277
278 @param ImageHandle the image handle of the process
279 @param SystemTable the EFI System Table pointer
280
281 @retval EFI_SUCCESS the initialization was complete sucessfully
282 @return others an error ocurred during initialization
283**/
284EFI_STATUS
285EFIAPI
286ShellLibConstructor (
287 IN EFI_HANDLE ImageHandle,
288 IN EFI_SYSTEM_TABLE *SystemTable
jcarseya405b862010-09-14 05:18:09 +0000289 )
290{
jcarseyd2b45642009-05-11 18:02:16 +0000291 mEfiShellEnvironment2 = NULL;
jcarsey366f81a2011-06-27 21:04:22 +0000292 gEfiShellProtocol = NULL;
293 gEfiShellParametersProtocol = NULL;
jcarseyd2b45642009-05-11 18:02:16 +0000294 mEfiShellInterface = NULL;
295 mEfiShellEnvironment2Handle = NULL;
296
jcarseyd2b45642009-05-11 18:02:16 +0000297 //
298 // verify that auto initialize is not set false
jcarsey1e6e84c2010-01-25 20:05:08 +0000299 //
jcarseyd2b45642009-05-11 18:02:16 +0000300 if (PcdGetBool(PcdShellLibAutoInitialize) == 0) {
301 return (EFI_SUCCESS);
302 }
jcarsey1e6e84c2010-01-25 20:05:08 +0000303
jcarseyd2b45642009-05-11 18:02:16 +0000304 return (ShellLibConstructorWorker(ImageHandle, SystemTable));
305}
jcarsey94b17fa2009-05-07 18:46:18 +0000306
307/**
jcarseya405b862010-09-14 05:18:09 +0000308 Destructor for the library. free any resources.
309
310 @param[in] ImageHandle A copy of the ImageHandle.
311 @param[in] SystemTable A pointer to the SystemTable for the application.
312
313 @retval EFI_SUCCESS The operation was successful.
314 @return An error from the CloseProtocol function.
jcarsey94b17fa2009-05-07 18:46:18 +0000315**/
316EFI_STATUS
317EFIAPI
318ShellLibDestructor (
319 IN EFI_HANDLE ImageHandle,
320 IN EFI_SYSTEM_TABLE *SystemTable
jcarseya405b862010-09-14 05:18:09 +0000321 )
322{
jcarsey94b17fa2009-05-07 18:46:18 +0000323 if (mEfiShellEnvironment2 != NULL) {
324 gBS->CloseProtocol(mEfiShellEnvironment2Handle==NULL?ImageHandle:mEfiShellEnvironment2Handle,
325 &gEfiShellEnvironment2Guid,
326 ImageHandle,
327 NULL);
jcarseyd2b45642009-05-11 18:02:16 +0000328 mEfiShellEnvironment2 = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +0000329 }
330 if (mEfiShellInterface != NULL) {
331 gBS->CloseProtocol(ImageHandle,
332 &gEfiShellInterfaceGuid,
333 ImageHandle,
jcarsey1e6e84c2010-01-25 20:05:08 +0000334 NULL);
jcarseyd2b45642009-05-11 18:02:16 +0000335 mEfiShellInterface = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +0000336 }
jcarsey366f81a2011-06-27 21:04:22 +0000337 if (gEfiShellProtocol != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +0000338 gBS->CloseProtocol(ImageHandle,
339 &gEfiShellProtocolGuid,
340 ImageHandle,
jcarsey1e6e84c2010-01-25 20:05:08 +0000341 NULL);
jcarsey366f81a2011-06-27 21:04:22 +0000342 gEfiShellProtocol = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +0000343 }
jcarsey366f81a2011-06-27 21:04:22 +0000344 if (gEfiShellParametersProtocol != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +0000345 gBS->CloseProtocol(ImageHandle,
346 &gEfiShellParametersProtocolGuid,
347 ImageHandle,
jcarsey1e6e84c2010-01-25 20:05:08 +0000348 NULL);
jcarsey366f81a2011-06-27 21:04:22 +0000349 gEfiShellParametersProtocol = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +0000350 }
jcarseyd2b45642009-05-11 18:02:16 +0000351 mEfiShellEnvironment2Handle = NULL;
jcarseyecd3d592009-12-07 18:05:00 +0000352
jcarsey94b17fa2009-05-07 18:46:18 +0000353 return (EFI_SUCCESS);
354}
jcarseyd2b45642009-05-11 18:02:16 +0000355
356/**
357 This function causes the shell library to initialize itself. If the shell library
358 is already initialized it will de-initialize all the current protocol poitners and
359 re-populate them again.
360
361 When the library is used with PcdShellLibAutoInitialize set to true this function
362 will return EFI_SUCCESS and perform no actions.
363
364 This function is intended for internal access for shell commands only.
365
366 @retval EFI_SUCCESS the initialization was complete sucessfully
367
368**/
369EFI_STATUS
370EFIAPI
371ShellInitialize (
jcarseya405b862010-09-14 05:18:09 +0000372 )
373{
jcarseyd2b45642009-05-11 18:02:16 +0000374 //
375 // if auto initialize is not false then skip
376 //
377 if (PcdGetBool(PcdShellLibAutoInitialize) != 0) {
378 return (EFI_SUCCESS);
379 }
380
381 //
382 // deinit the current stuff
383 //
384 ASSERT_EFI_ERROR(ShellLibDestructor(gImageHandle, gST));
385
386 //
387 // init the new stuff
388 //
389 return (ShellLibConstructorWorker(gImageHandle, gST));
390}
391
jcarsey94b17fa2009-05-07 18:46:18 +0000392/**
jcarsey1e6e84c2010-01-25 20:05:08 +0000393 This function will retrieve the information about the file for the handle
jcarsey94b17fa2009-05-07 18:46:18 +0000394 specified and store it in allocated pool memory.
395
jcarsey1e6e84c2010-01-25 20:05:08 +0000396 This function allocates a buffer to store the file's information. It is the
qhuang869817bf2009-05-20 14:42:48 +0000397 caller's responsibility to free the buffer
jcarsey94b17fa2009-05-07 18:46:18 +0000398
jcarsey1e6e84c2010-01-25 20:05:08 +0000399 @param FileHandle The file handle of the file for which information is
jcarsey94b17fa2009-05-07 18:46:18 +0000400 being requested.
401
402 @retval NULL information could not be retrieved.
403
404 @return the information about the file
405**/
406EFI_FILE_INFO*
407EFIAPI
408ShellGetFileInfo (
jcarseya405b862010-09-14 05:18:09 +0000409 IN SHELL_FILE_HANDLE FileHandle
410 )
411{
jcarseyd2b45642009-05-11 18:02:16 +0000412 return (FileFunctionMap.GetFileInfo(FileHandle));
jcarsey94b17fa2009-05-07 18:46:18 +0000413}
414
415/**
jcarseya405b862010-09-14 05:18:09 +0000416 This function sets the information about the file for the opened handle
jcarsey94b17fa2009-05-07 18:46:18 +0000417 specified.
418
jcarseya405b862010-09-14 05:18:09 +0000419 @param[in] FileHandle The file handle of the file for which information
420 is being set.
jcarsey94b17fa2009-05-07 18:46:18 +0000421
jcarseya405b862010-09-14 05:18:09 +0000422 @param[in] FileInfo The information to set.
jcarsey94b17fa2009-05-07 18:46:18 +0000423
darylm503b0934ac2011-11-12 00:35:11 +0000424 @retval EFI_SUCCESS The information was set.
jcarseya405b862010-09-14 05:18:09 +0000425 @retval EFI_INVALID_PARAMETER A parameter was out of range or invalid.
426 @retval EFI_UNSUPPORTED The FileHandle does not support FileInfo.
darylm503b0934ac2011-11-12 00:35:11 +0000427 @retval EFI_NO_MEDIA The device has no medium.
428 @retval EFI_DEVICE_ERROR The device reported an error.
429 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
430 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
jcarseya405b862010-09-14 05:18:09 +0000431 @retval EFI_ACCESS_DENIED The file was opened read only.
432 @retval EFI_VOLUME_FULL The volume is full.
jcarsey94b17fa2009-05-07 18:46:18 +0000433**/
434EFI_STATUS
435EFIAPI
436ShellSetFileInfo (
darylm503b0934ac2011-11-12 00:35:11 +0000437 IN SHELL_FILE_HANDLE FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000438 IN EFI_FILE_INFO *FileInfo
jcarseya405b862010-09-14 05:18:09 +0000439 )
440{
jcarseyd2b45642009-05-11 18:02:16 +0000441 return (FileFunctionMap.SetFileInfo(FileHandle, FileInfo));
jcarsey1e6e84c2010-01-25 20:05:08 +0000442}
443
jcarsey94b17fa2009-05-07 18:46:18 +0000444 /**
445 This function will open a file or directory referenced by DevicePath.
446
jcarsey1e6e84c2010-01-25 20:05:08 +0000447 This function opens a file with the open mode according to the file path. The
jcarsey94b17fa2009-05-07 18:46:18 +0000448 Attributes is valid only for EFI_FILE_MODE_CREATE.
449
darylm503b0934ac2011-11-12 00:35:11 +0000450 @param FilePath on input the device path to the file. On output
jcarsey94b17fa2009-05-07 18:46:18 +0000451 the remaining device path.
darylm503b0934ac2011-11-12 00:35:11 +0000452 @param DeviceHandle pointer to the system device handle.
453 @param FileHandle pointer to the file handle.
454 @param OpenMode the mode to open the file with.
455 @param Attributes the file's file attributes.
jcarsey94b17fa2009-05-07 18:46:18 +0000456
darylm503b0934ac2011-11-12 00:35:11 +0000457 @retval EFI_SUCCESS The information was set.
458 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
459 @retval EFI_UNSUPPORTED Could not open the file path.
460 @retval EFI_NOT_FOUND The specified file could not be found on the
jcarsey1e6e84c2010-01-25 20:05:08 +0000461 device or the file system could not be found on
jcarsey94b17fa2009-05-07 18:46:18 +0000462 the device.
darylm503b0934ac2011-11-12 00:35:11 +0000463 @retval EFI_NO_MEDIA The device has no medium.
464 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
jcarsey94b17fa2009-05-07 18:46:18 +0000465 medium is no longer supported.
darylm503b0934ac2011-11-12 00:35:11 +0000466 @retval EFI_DEVICE_ERROR The device reported an error.
467 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
468 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
469 @retval EFI_ACCESS_DENIED The file was opened read only.
470 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
jcarsey94b17fa2009-05-07 18:46:18 +0000471 file.
darylm503b0934ac2011-11-12 00:35:11 +0000472 @retval EFI_VOLUME_FULL The volume is full.
jcarsey94b17fa2009-05-07 18:46:18 +0000473**/
474EFI_STATUS
475EFIAPI
476ShellOpenFileByDevicePath(
darylm503b0934ac2011-11-12 00:35:11 +0000477 IN OUT EFI_DEVICE_PATH_PROTOCOL **FilePath,
478 OUT EFI_HANDLE *DeviceHandle,
jcarseya405b862010-09-14 05:18:09 +0000479 OUT SHELL_FILE_HANDLE *FileHandle,
darylm503b0934ac2011-11-12 00:35:11 +0000480 IN UINT64 OpenMode,
481 IN UINT64 Attributes
jcarseya405b862010-09-14 05:18:09 +0000482 )
483{
484 CHAR16 *FileName;
485 EFI_STATUS Status;
jcarsey94b17fa2009-05-07 18:46:18 +0000486 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *EfiSimpleFileSystemProtocol;
jcarseya405b862010-09-14 05:18:09 +0000487 EFI_FILE_PROTOCOL *Handle1;
488 EFI_FILE_PROTOCOL *Handle2;
ydong100b6cb332013-01-25 02:00:22 +0000489 CHAR16 *FnafPathName;
490 UINTN PathLen;
jcarsey94b17fa2009-05-07 18:46:18 +0000491
jcarsey92a54472011-06-27 20:33:13 +0000492 if (FilePath == NULL || FileHandle == NULL || DeviceHandle == NULL) {
493 return (EFI_INVALID_PARAMETER);
494 }
495
jcarsey1e6e84c2010-01-25 20:05:08 +0000496 //
jcarsey94b17fa2009-05-07 18:46:18 +0000497 // which shell interface should we use
498 //
jcarsey366f81a2011-06-27 21:04:22 +0000499 if (gEfiShellProtocol != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +0000500 //
501 // use UEFI Shell 2.0 method.
502 //
jcarsey366f81a2011-06-27 21:04:22 +0000503 FileName = gEfiShellProtocol->GetFilePathFromDevicePath(*FilePath);
jcarsey94b17fa2009-05-07 18:46:18 +0000504 if (FileName == NULL) {
505 return (EFI_INVALID_PARAMETER);
506 }
507 Status = ShellOpenFileByName(FileName, FileHandle, OpenMode, Attributes);
508 FreePool(FileName);
509 return (Status);
jcarsey1e6e84c2010-01-25 20:05:08 +0000510 }
jcarseyd2b45642009-05-11 18:02:16 +0000511
512
513 //
514 // use old shell method.
515 //
jcarsey1e6e84c2010-01-25 20:05:08 +0000516 Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid,
517 FilePath,
jcarseyd2b45642009-05-11 18:02:16 +0000518 DeviceHandle);
519 if (EFI_ERROR (Status)) {
520 return Status;
521 }
522 Status = gBS->OpenProtocol(*DeviceHandle,
523 &gEfiSimpleFileSystemProtocolGuid,
jcarseyb1f95a02009-06-16 00:23:19 +0000524 (VOID**)&EfiSimpleFileSystemProtocol,
jcarseyd2b45642009-05-11 18:02:16 +0000525 gImageHandle,
526 NULL,
527 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
528 if (EFI_ERROR (Status)) {
529 return Status;
530 }
jcarseya405b862010-09-14 05:18:09 +0000531 Status = EfiSimpleFileSystemProtocol->OpenVolume(EfiSimpleFileSystemProtocol, &Handle1);
jcarseyd2b45642009-05-11 18:02:16 +0000532 if (EFI_ERROR (Status)) {
533 FileHandle = NULL;
534 return Status;
535 }
536
537 //
538 // go down directories one node at a time.
539 //
540 while (!IsDevicePathEnd (*FilePath)) {
jcarsey94b17fa2009-05-07 18:46:18 +0000541 //
jcarseyd2b45642009-05-11 18:02:16 +0000542 // For file system access each node should be a file path component
jcarsey94b17fa2009-05-07 18:46:18 +0000543 //
jcarseyd2b45642009-05-11 18:02:16 +0000544 if (DevicePathType (*FilePath) != MEDIA_DEVICE_PATH ||
545 DevicePathSubType (*FilePath) != MEDIA_FILEPATH_DP
jcarseya405b862010-09-14 05:18:09 +0000546 ) {
jcarsey94b17fa2009-05-07 18:46:18 +0000547 FileHandle = NULL;
jcarseyd2b45642009-05-11 18:02:16 +0000548 return (EFI_INVALID_PARAMETER);
jcarsey94b17fa2009-05-07 18:46:18 +0000549 }
jcarseyd2b45642009-05-11 18:02:16 +0000550 //
551 // Open this file path node
552 //
jcarseya405b862010-09-14 05:18:09 +0000553 Handle2 = Handle1;
554 Handle1 = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +0000555
556 //
ydong100b6cb332013-01-25 02:00:22 +0000557 // File Name Alignment Fix (FNAF)
558 // Handle2->Open may be incapable of handling a unaligned CHAR16 data.
559 // The structure pointed to by FilePath may be not CHAR16 aligned.
560 // This code copies the potentially unaligned PathName data from the
561 // FilePath structure to the aligned FnafPathName for use in the
562 // calls to Handl2->Open.
563 //
564
565 //
566 // Determine length of PathName, in bytes.
567 //
568 PathLen = DevicePathNodeLength (*FilePath) - SIZE_OF_FILEPATH_DEVICE_PATH;
569
570 //
571 // Allocate memory for the aligned copy of the string Extra allocation is to allow for forced alignment
572 // Copy bytes from possibly unaligned location to aligned location
573 //
574 FnafPathName = AllocateCopyPool(PathLen, (UINT8 *)((FILEPATH_DEVICE_PATH*)*FilePath)->PathName);
575 if (FnafPathName == NULL) {
576 return EFI_OUT_OF_RESOURCES;
577 }
578
579 //
jcarseyd2b45642009-05-11 18:02:16 +0000580 // Try to test opening an existing file
jcarsey94b17fa2009-05-07 18:46:18 +0000581 //
jcarseya405b862010-09-14 05:18:09 +0000582 Status = Handle2->Open (
583 Handle2,
584 &Handle1,
ydong100b6cb332013-01-25 02:00:22 +0000585 FnafPathName,
jcarseyd2b45642009-05-11 18:02:16 +0000586 OpenMode &~EFI_FILE_MODE_CREATE,
587 0
jcarseya405b862010-09-14 05:18:09 +0000588 );
jcarsey94b17fa2009-05-07 18:46:18 +0000589
jcarseyd2b45642009-05-11 18:02:16 +0000590 //
591 // see if the error was that it needs to be created
592 //
593 if ((EFI_ERROR (Status)) && (OpenMode != (OpenMode &~EFI_FILE_MODE_CREATE))) {
jcarseya405b862010-09-14 05:18:09 +0000594 Status = Handle2->Open (
595 Handle2,
596 &Handle1,
ydong100b6cb332013-01-25 02:00:22 +0000597 FnafPathName,
jcarseyd2b45642009-05-11 18:02:16 +0000598 OpenMode,
599 Attributes
jcarseya405b862010-09-14 05:18:09 +0000600 );
jcarsey94b17fa2009-05-07 18:46:18 +0000601 }
ydong100b6cb332013-01-25 02:00:22 +0000602
603 //
604 // Free the alignment buffer
605 //
606 FreePool(FnafPathName);
607
jcarseyd2b45642009-05-11 18:02:16 +0000608 //
609 // Close the last node
610 //
jcarseya405b862010-09-14 05:18:09 +0000611 Handle2->Close (Handle2);
jcarseyd2b45642009-05-11 18:02:16 +0000612
613 if (EFI_ERROR(Status)) {
614 return (Status);
615 }
616
617 //
618 // Get the next node
619 //
620 *FilePath = NextDevicePathNode (*FilePath);
jcarsey94b17fa2009-05-07 18:46:18 +0000621 }
jcarseya405b862010-09-14 05:18:09 +0000622
623 //
624 // This is a weak spot since if the undefined SHELL_FILE_HANDLE format changes this must change also!
625 //
626 *FileHandle = (VOID*)Handle1;
jcarseyd2b45642009-05-11 18:02:16 +0000627 return (EFI_SUCCESS);
jcarsey94b17fa2009-05-07 18:46:18 +0000628}
629
630/**
631 This function will open a file or directory referenced by filename.
632
jcarsey1e6e84c2010-01-25 20:05:08 +0000633 If return is EFI_SUCCESS, the Filehandle is the opened file's handle;
634 otherwise, the Filehandle is NULL. The Attributes is valid only for
jcarsey94b17fa2009-05-07 18:46:18 +0000635 EFI_FILE_MODE_CREATE.
636
jcarsey92a54472011-06-27 20:33:13 +0000637 if FileName is NULL then ASSERT()
jcarsey94b17fa2009-05-07 18:46:18 +0000638
darylm503b0934ac2011-11-12 00:35:11 +0000639 @param FileName pointer to file name
640 @param FileHandle pointer to the file handle.
641 @param OpenMode the mode to open the file with.
642 @param Attributes the file's file attributes.
jcarsey94b17fa2009-05-07 18:46:18 +0000643
darylm503b0934ac2011-11-12 00:35:11 +0000644 @retval EFI_SUCCESS The information was set.
645 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
646 @retval EFI_UNSUPPORTED Could not open the file path.
647 @retval EFI_NOT_FOUND The specified file could not be found on the
jcarsey1e6e84c2010-01-25 20:05:08 +0000648 device or the file system could not be found
jcarsey94b17fa2009-05-07 18:46:18 +0000649 on the device.
darylm503b0934ac2011-11-12 00:35:11 +0000650 @retval EFI_NO_MEDIA The device has no medium.
651 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
jcarsey94b17fa2009-05-07 18:46:18 +0000652 medium is no longer supported.
darylm503b0934ac2011-11-12 00:35:11 +0000653 @retval EFI_DEVICE_ERROR The device reported an error.
654 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
655 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
656 @retval EFI_ACCESS_DENIED The file was opened read only.
657 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
jcarsey94b17fa2009-05-07 18:46:18 +0000658 file.
darylm503b0934ac2011-11-12 00:35:11 +0000659 @retval EFI_VOLUME_FULL The volume is full.
jcarsey94b17fa2009-05-07 18:46:18 +0000660**/
661EFI_STATUS
662EFIAPI
663ShellOpenFileByName(
darylm503b0934ac2011-11-12 00:35:11 +0000664 IN CONST CHAR16 *FileName,
jcarseya405b862010-09-14 05:18:09 +0000665 OUT SHELL_FILE_HANDLE *FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000666 IN UINT64 OpenMode,
darylm503b0934ac2011-11-12 00:35:11 +0000667 IN UINT64 Attributes
jcarseya405b862010-09-14 05:18:09 +0000668 )
669{
jcarsey94b17fa2009-05-07 18:46:18 +0000670 EFI_HANDLE DeviceHandle;
671 EFI_DEVICE_PATH_PROTOCOL *FilePath;
jcarseyb1f95a02009-06-16 00:23:19 +0000672 EFI_STATUS Status;
673 EFI_FILE_INFO *FileInfo;
jcarsey94b17fa2009-05-07 18:46:18 +0000674
675 //
676 // ASSERT if FileName is NULL
677 //
678 ASSERT(FileName != NULL);
679
jcarseya405b862010-09-14 05:18:09 +0000680 if (FileName == NULL) {
681 return (EFI_INVALID_PARAMETER);
682 }
683
jcarsey366f81a2011-06-27 21:04:22 +0000684 if (gEfiShellProtocol != NULL) {
jcarseya405b862010-09-14 05:18:09 +0000685 if ((OpenMode & EFI_FILE_MODE_CREATE) == EFI_FILE_MODE_CREATE && (Attributes & EFI_FILE_DIRECTORY) == EFI_FILE_DIRECTORY) {
686 return ShellCreateDirectory(FileName, FileHandle);
687 }
jcarsey94b17fa2009-05-07 18:46:18 +0000688 //
689 // Use UEFI Shell 2.0 method
690 //
jcarsey366f81a2011-06-27 21:04:22 +0000691 Status = gEfiShellProtocol->OpenFileByName(FileName,
jcarseyb1f95a02009-06-16 00:23:19 +0000692 FileHandle,
693 OpenMode);
jcarseya405b862010-09-14 05:18:09 +0000694 if (StrCmp(FileName, L"NUL") != 0 && !EFI_ERROR(Status) && ((OpenMode & EFI_FILE_MODE_CREATE) != 0)){
jcarsey2247dde2009-11-09 18:08:58 +0000695 FileInfo = FileFunctionMap.GetFileInfo(*FileHandle);
jcarseyb1f95a02009-06-16 00:23:19 +0000696 ASSERT(FileInfo != NULL);
697 FileInfo->Attribute = Attributes;
jcarsey2247dde2009-11-09 18:08:58 +0000698 Status = FileFunctionMap.SetFileInfo(*FileHandle, FileInfo);
699 FreePool(FileInfo);
jcarseyb1f95a02009-06-16 00:23:19 +0000700 }
701 return (Status);
jcarsey1e6e84c2010-01-25 20:05:08 +0000702 }
jcarsey94b17fa2009-05-07 18:46:18 +0000703 //
704 // Using EFI Shell version
705 // this means convert name to path and call that function
706 // since this will use EFI method again that will open it.
707 //
708 ASSERT(mEfiShellEnvironment2 != NULL);
jcarseyb82bfcc2009-06-29 16:28:23 +0000709 FilePath = mEfiShellEnvironment2->NameToPath ((CHAR16*)FileName);
xdu290bfa222010-07-19 05:21:27 +0000710 if (FilePath != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +0000711 return (ShellOpenFileByDevicePath(&FilePath,
712 &DeviceHandle,
713 FileHandle,
714 OpenMode,
jcarseya405b862010-09-14 05:18:09 +0000715 Attributes));
jcarsey94b17fa2009-05-07 18:46:18 +0000716 }
717 return (EFI_DEVICE_ERROR);
718}
719/**
720 This function create a directory
721
jcarsey1e6e84c2010-01-25 20:05:08 +0000722 If return is EFI_SUCCESS, the Filehandle is the opened directory's handle;
723 otherwise, the Filehandle is NULL. If the directory already existed, this
jcarsey94b17fa2009-05-07 18:46:18 +0000724 function opens the existing directory.
725
darylm503b0934ac2011-11-12 00:35:11 +0000726 @param DirectoryName pointer to directory name
727 @param FileHandle pointer to the file handle.
jcarsey94b17fa2009-05-07 18:46:18 +0000728
darylm503b0934ac2011-11-12 00:35:11 +0000729 @retval EFI_SUCCESS The information was set.
730 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
731 @retval EFI_UNSUPPORTED Could not open the file path.
732 @retval EFI_NOT_FOUND The specified file could not be found on the
jcarsey1e6e84c2010-01-25 20:05:08 +0000733 device or the file system could not be found
jcarsey94b17fa2009-05-07 18:46:18 +0000734 on the device.
darylm503b0934ac2011-11-12 00:35:11 +0000735 @retval EFI_NO_MEDIA The device has no medium.
736 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
jcarsey94b17fa2009-05-07 18:46:18 +0000737 medium is no longer supported.
darylm503b0934ac2011-11-12 00:35:11 +0000738 @retval EFI_DEVICE_ERROR The device reported an error.
739 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
740 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
741 @retval EFI_ACCESS_DENIED The file was opened read only.
742 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
jcarsey94b17fa2009-05-07 18:46:18 +0000743 file.
darylm503b0934ac2011-11-12 00:35:11 +0000744 @retval EFI_VOLUME_FULL The volume is full.
jcarsey94b17fa2009-05-07 18:46:18 +0000745 @sa ShellOpenFileByName
746**/
747EFI_STATUS
748EFIAPI
749ShellCreateDirectory(
jcarseyb82bfcc2009-06-29 16:28:23 +0000750 IN CONST CHAR16 *DirectoryName,
jcarseya405b862010-09-14 05:18:09 +0000751 OUT SHELL_FILE_HANDLE *FileHandle
752 )
753{
jcarsey366f81a2011-06-27 21:04:22 +0000754 if (gEfiShellProtocol != NULL) {
jcarsey2247dde2009-11-09 18:08:58 +0000755 //
756 // Use UEFI Shell 2.0 method
757 //
jcarsey366f81a2011-06-27 21:04:22 +0000758 return (gEfiShellProtocol->CreateFile(DirectoryName,
jcarsey2247dde2009-11-09 18:08:58 +0000759 EFI_FILE_DIRECTORY,
760 FileHandle
jcarseya405b862010-09-14 05:18:09 +0000761 ));
jcarsey2247dde2009-11-09 18:08:58 +0000762 } else {
763 return (ShellOpenFileByName(DirectoryName,
764 FileHandle,
765 EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE,
766 EFI_FILE_DIRECTORY
jcarseya405b862010-09-14 05:18:09 +0000767 ));
jcarsey2247dde2009-11-09 18:08:58 +0000768 }
jcarsey94b17fa2009-05-07 18:46:18 +0000769}
770
771/**
772 This function reads information from an opened file.
773
jcarsey1e6e84c2010-01-25 20:05:08 +0000774 If FileHandle is not a directory, the function reads the requested number of
775 bytes from the file at the file's current position and returns them in Buffer.
jcarsey94b17fa2009-05-07 18:46:18 +0000776 If the read goes beyond the end of the file, the read length is truncated to the
jcarsey1e6e84c2010-01-25 20:05:08 +0000777 end of the file. The file's current position is increased by the number of bytes
778 returned. If FileHandle is a directory, the function reads the directory entry
779 at the file's current position and returns the entry in Buffer. If the Buffer
780 is not large enough to hold the current directory entry, then
781 EFI_BUFFER_TOO_SMALL is returned and the current file position is not updated.
782 BufferSize is set to be the size of the buffer needed to read the entry. On
783 success, the current position is updated to the next directory entry. If there
784 are no more directory entries, the read returns a zero-length buffer.
jcarsey94b17fa2009-05-07 18:46:18 +0000785 EFI_FILE_INFO is the structure returned as the directory entry.
786
787 @param FileHandle the opened file handle
jcarsey1e6e84c2010-01-25 20:05:08 +0000788 @param BufferSize on input the size of buffer in bytes. on return
jcarsey94b17fa2009-05-07 18:46:18 +0000789 the number of bytes written.
790 @param Buffer the buffer to put read data into.
791
darylm503b0934ac2011-11-12 00:35:11 +0000792 @retval EFI_SUCCESS Data was read.
793 @retval EFI_NO_MEDIA The device has no media.
794 @retval EFI_DEVICE_ERROR The device reported an error.
795 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
796 @retval EFI_BUFFER_TO_SMALL Buffer is too small. ReadSize contains required
jcarsey94b17fa2009-05-07 18:46:18 +0000797 size.
798
799**/
800EFI_STATUS
801EFIAPI
802ShellReadFile(
jcarseya405b862010-09-14 05:18:09 +0000803 IN SHELL_FILE_HANDLE FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000804 IN OUT UINTN *BufferSize,
805 OUT VOID *Buffer
jcarseya405b862010-09-14 05:18:09 +0000806 )
807{
jcarseyd2b45642009-05-11 18:02:16 +0000808 return (FileFunctionMap.ReadFile(FileHandle, BufferSize, Buffer));
jcarsey94b17fa2009-05-07 18:46:18 +0000809}
810
811
812/**
813 Write data to a file.
814
jcarsey1e6e84c2010-01-25 20:05:08 +0000815 This function writes the specified number of bytes to the file at the current
816 file position. The current file position is advanced the actual number of bytes
817 written, which is returned in BufferSize. Partial writes only occur when there
818 has been a data error during the write attempt (such as "volume space full").
819 The file is automatically grown to hold the data if required. Direct writes to
jcarsey94b17fa2009-05-07 18:46:18 +0000820 opened directories are not supported.
821
822 @param FileHandle The opened file for writing
823 @param BufferSize on input the number of bytes in Buffer. On output
824 the number of bytes written.
825 @param Buffer the buffer containing data to write is stored.
826
darylm503b0934ac2011-11-12 00:35:11 +0000827 @retval EFI_SUCCESS Data was written.
828 @retval EFI_UNSUPPORTED Writes to an open directory are not supported.
829 @retval EFI_NO_MEDIA The device has no media.
830 @retval EFI_DEVICE_ERROR The device reported an error.
831 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
832 @retval EFI_WRITE_PROTECTED The device is write-protected.
833 @retval EFI_ACCESS_DENIED The file was open for read only.
834 @retval EFI_VOLUME_FULL The volume is full.
jcarsey94b17fa2009-05-07 18:46:18 +0000835**/
836EFI_STATUS
837EFIAPI
838ShellWriteFile(
jcarsey252d9452011-03-25 20:49:53 +0000839 IN SHELL_FILE_HANDLE FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000840 IN OUT UINTN *BufferSize,
841 IN VOID *Buffer
jcarseya405b862010-09-14 05:18:09 +0000842 )
843{
jcarseyd2b45642009-05-11 18:02:16 +0000844 return (FileFunctionMap.WriteFile(FileHandle, BufferSize, Buffer));
jcarsey94b17fa2009-05-07 18:46:18 +0000845}
846
jcarsey1e6e84c2010-01-25 20:05:08 +0000847/**
jcarsey94b17fa2009-05-07 18:46:18 +0000848 Close an open file handle.
849
jcarsey1e6e84c2010-01-25 20:05:08 +0000850 This function closes a specified file handle. All "dirty" cached file data is
851 flushed to the device, and the file is closed. In all cases the handle is
jcarsey94b17fa2009-05-07 18:46:18 +0000852 closed.
853
854@param FileHandle the file handle to close.
855
856@retval EFI_SUCCESS the file handle was closed sucessfully.
857**/
858EFI_STATUS
859EFIAPI
860ShellCloseFile (
jcarseya405b862010-09-14 05:18:09 +0000861 IN SHELL_FILE_HANDLE *FileHandle
862 )
863{
jcarseyd2b45642009-05-11 18:02:16 +0000864 return (FileFunctionMap.CloseFile(*FileHandle));
jcarsey94b17fa2009-05-07 18:46:18 +0000865}
866
867/**
868 Delete a file and close the handle
869
870 This function closes and deletes a file. In all cases the file handle is closed.
jcarsey1e6e84c2010-01-25 20:05:08 +0000871 If the file cannot be deleted, the warning code EFI_WARN_DELETE_FAILURE is
jcarsey94b17fa2009-05-07 18:46:18 +0000872 returned, but the handle is still closed.
873
874 @param FileHandle the file handle to delete
875
876 @retval EFI_SUCCESS the file was closed sucessfully
jcarsey1e6e84c2010-01-25 20:05:08 +0000877 @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not
jcarsey94b17fa2009-05-07 18:46:18 +0000878 deleted
darylm503b0934ac2011-11-12 00:35:11 +0000879 @retval INVALID_PARAMETER One of the parameters has an invalid value.
jcarsey94b17fa2009-05-07 18:46:18 +0000880**/
881EFI_STATUS
882EFIAPI
883ShellDeleteFile (
darylm503b0934ac2011-11-12 00:35:11 +0000884 IN SHELL_FILE_HANDLE *FileHandle
jcarseya405b862010-09-14 05:18:09 +0000885 )
886{
jcarseyd2b45642009-05-11 18:02:16 +0000887 return (FileFunctionMap.DeleteFile(*FileHandle));
jcarsey94b17fa2009-05-07 18:46:18 +0000888}
889
890/**
891 Set the current position in a file.
892
jcarsey1e6e84c2010-01-25 20:05:08 +0000893 This function sets the current file position for the handle to the position
jcarsey94b17fa2009-05-07 18:46:18 +0000894 supplied. With the exception of seeking to position 0xFFFFFFFFFFFFFFFF, only
jcarsey1e6e84c2010-01-25 20:05:08 +0000895 absolute positioning is supported, and seeking past the end of the file is
896 allowed (a subsequent write would grow the file). Seeking to position
jcarsey94b17fa2009-05-07 18:46:18 +0000897 0xFFFFFFFFFFFFFFFF causes the current position to be set to the end of the file.
jcarsey1e6e84c2010-01-25 20:05:08 +0000898 If FileHandle is a directory, the only position that may be set is zero. This
jcarsey94b17fa2009-05-07 18:46:18 +0000899 has the effect of starting the read process of the directory entries over.
900
901 @param FileHandle The file handle on which the position is being set
902 @param Position Byte position from begining of file
903
904 @retval EFI_SUCCESS Operation completed sucessfully.
jcarsey1e6e84c2010-01-25 20:05:08 +0000905 @retval EFI_UNSUPPORTED the seek request for non-zero is not valid on
jcarsey94b17fa2009-05-07 18:46:18 +0000906 directories.
907 @retval INVALID_PARAMETER One of the parameters has an invalid value.
908**/
909EFI_STATUS
910EFIAPI
911ShellSetFilePosition (
darylm503b0934ac2011-11-12 00:35:11 +0000912 IN SHELL_FILE_HANDLE FileHandle,
913 IN UINT64 Position
jcarseya405b862010-09-14 05:18:09 +0000914 )
915{
jcarseyd2b45642009-05-11 18:02:16 +0000916 return (FileFunctionMap.SetFilePosition(FileHandle, Position));
jcarsey94b17fa2009-05-07 18:46:18 +0000917}
918
jcarsey1e6e84c2010-01-25 20:05:08 +0000919/**
jcarsey94b17fa2009-05-07 18:46:18 +0000920 Gets a file's current position
921
jcarsey1e6e84c2010-01-25 20:05:08 +0000922 This function retrieves the current file position for the file handle. For
923 directories, the current file position has no meaning outside of the file
jcarsey94b17fa2009-05-07 18:46:18 +0000924 system driver and as such the operation is not supported. An error is returned
925 if FileHandle is a directory.
926
927 @param FileHandle The open file handle on which to get the position.
928 @param Position Byte position from begining of file.
929
930 @retval EFI_SUCCESS the operation completed sucessfully.
931 @retval INVALID_PARAMETER One of the parameters has an invalid value.
932 @retval EFI_UNSUPPORTED the request is not valid on directories.
933**/
934EFI_STATUS
935EFIAPI
936ShellGetFilePosition (
jcarseya405b862010-09-14 05:18:09 +0000937 IN SHELL_FILE_HANDLE FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000938 OUT UINT64 *Position
jcarseya405b862010-09-14 05:18:09 +0000939 )
940{
jcarseyd2b45642009-05-11 18:02:16 +0000941 return (FileFunctionMap.GetFilePosition(FileHandle, Position));
jcarsey94b17fa2009-05-07 18:46:18 +0000942}
943/**
944 Flushes data on a file
jcarsey1e6e84c2010-01-25 20:05:08 +0000945
jcarsey94b17fa2009-05-07 18:46:18 +0000946 This function flushes all modified data associated with a file to a device.
947
948 @param FileHandle The file handle on which to flush data
949
950 @retval EFI_SUCCESS The data was flushed.
951 @retval EFI_NO_MEDIA The device has no media.
952 @retval EFI_DEVICE_ERROR The device reported an error.
953 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
954 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
955 @retval EFI_ACCESS_DENIED The file was opened for read only.
956**/
957EFI_STATUS
958EFIAPI
959ShellFlushFile (
jcarseya405b862010-09-14 05:18:09 +0000960 IN SHELL_FILE_HANDLE FileHandle
961 )
962{
jcarseyd2b45642009-05-11 18:02:16 +0000963 return (FileFunctionMap.FlushFile(FileHandle));
jcarsey94b17fa2009-05-07 18:46:18 +0000964}
965
darylm503b0934ac2011-11-12 00:35:11 +0000966/** Retrieve first entry from a directory.
jcarsey94b17fa2009-05-07 18:46:18 +0000967
darylm503b0934ac2011-11-12 00:35:11 +0000968 This function takes an open directory handle and gets information from the
969 first entry in the directory. A buffer is allocated to contain
970 the information and a pointer to the buffer is returned in *Buffer. The
971 caller can use ShellFindNextFile() to get subsequent directory entries.
jcarsey94b17fa2009-05-07 18:46:18 +0000972
darylm503b0934ac2011-11-12 00:35:11 +0000973 The buffer will be freed by ShellFindNextFile() when the last directory
974 entry is read. Otherwise, the caller must free the buffer, using FreePool,
975 when finished with it.
976
977 @param[in] DirHandle The file handle of the directory to search.
978 @param[out] Buffer The pointer to the buffer for the file's information.
jcarsey94b17fa2009-05-07 18:46:18 +0000979
980 @retval EFI_SUCCESS Found the first file.
981 @retval EFI_NOT_FOUND Cannot find the directory.
982 @retval EFI_NO_MEDIA The device has no media.
983 @retval EFI_DEVICE_ERROR The device reported an error.
984 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
985 @return Others status of ShellGetFileInfo, ShellSetFilePosition,
986 or ShellReadFile
987**/
988EFI_STATUS
989EFIAPI
990ShellFindFirstFile (
jcarseya405b862010-09-14 05:18:09 +0000991 IN SHELL_FILE_HANDLE DirHandle,
jcarseyd2b45642009-05-11 18:02:16 +0000992 OUT EFI_FILE_INFO **Buffer
jcarseya405b862010-09-14 05:18:09 +0000993 )
994{
jcarsey94b17fa2009-05-07 18:46:18 +0000995 //
jcarseyd2b45642009-05-11 18:02:16 +0000996 // pass to file handle lib
jcarsey94b17fa2009-05-07 18:46:18 +0000997 //
jcarseyd2b45642009-05-11 18:02:16 +0000998 return (FileHandleFindFirstFile(DirHandle, Buffer));
jcarsey94b17fa2009-05-07 18:46:18 +0000999}
darylm503b0934ac2011-11-12 00:35:11 +00001000/** Retrieve next entries from a directory.
jcarsey94b17fa2009-05-07 18:46:18 +00001001
darylm503b0934ac2011-11-12 00:35:11 +00001002 To use this function, the caller must first call the ShellFindFirstFile()
1003 function to get the first directory entry. Subsequent directory entries are
1004 retrieved by using the ShellFindNextFile() function. This function can
1005 be called several times to get each entry from the directory. If the call of
1006 ShellFindNextFile() retrieved the last directory entry, the next call of
1007 this function will set *NoFile to TRUE and free the buffer.
jcarsey94b17fa2009-05-07 18:46:18 +00001008
darylm503b0934ac2011-11-12 00:35:11 +00001009 @param[in] DirHandle The file handle of the directory.
1010 @param[out] Buffer The pointer to buffer for file's information.
1011 @param[out] NoFile The pointer to boolean when last file is found.
jcarsey94b17fa2009-05-07 18:46:18 +00001012
1013 @retval EFI_SUCCESS Found the next file, or reached last file
1014 @retval EFI_NO_MEDIA The device has no media.
1015 @retval EFI_DEVICE_ERROR The device reported an error.
1016 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
1017**/
1018EFI_STATUS
1019EFIAPI
1020ShellFindNextFile(
jcarseya405b862010-09-14 05:18:09 +00001021 IN SHELL_FILE_HANDLE DirHandle,
jcarsey94b17fa2009-05-07 18:46:18 +00001022 OUT EFI_FILE_INFO *Buffer,
1023 OUT BOOLEAN *NoFile
jcarseya405b862010-09-14 05:18:09 +00001024 )
1025{
jcarsey94b17fa2009-05-07 18:46:18 +00001026 //
jcarseyd2b45642009-05-11 18:02:16 +00001027 // pass to file handle lib
jcarsey94b17fa2009-05-07 18:46:18 +00001028 //
jcarseyd2b45642009-05-11 18:02:16 +00001029 return (FileHandleFindNextFile(DirHandle, Buffer, NoFile));
jcarsey94b17fa2009-05-07 18:46:18 +00001030}
1031/**
1032 Retrieve the size of a file.
1033
1034 if FileHandle is NULL then ASSERT()
1035 if Size is NULL then ASSERT()
1036
jcarsey1e6e84c2010-01-25 20:05:08 +00001037 This function extracts the file size info from the FileHandle's EFI_FILE_INFO
jcarsey94b17fa2009-05-07 18:46:18 +00001038 data.
1039
1040 @param FileHandle file handle from which size is retrieved
1041 @param Size pointer to size
1042
1043 @retval EFI_SUCCESS operation was completed sucessfully
1044 @retval EFI_DEVICE_ERROR cannot access the file
1045**/
1046EFI_STATUS
1047EFIAPI
1048ShellGetFileSize (
jcarseya405b862010-09-14 05:18:09 +00001049 IN SHELL_FILE_HANDLE FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +00001050 OUT UINT64 *Size
jcarseya405b862010-09-14 05:18:09 +00001051 )
1052{
jcarseyd2b45642009-05-11 18:02:16 +00001053 return (FileFunctionMap.GetFileSize(FileHandle, Size));
jcarsey94b17fa2009-05-07 18:46:18 +00001054}
1055/**
1056 Retrieves the status of the break execution flag
1057
1058 this function is useful to check whether the application is being asked to halt by the shell.
1059
1060 @retval TRUE the execution break is enabled
1061 @retval FALSE the execution break is not enabled
1062**/
1063BOOLEAN
1064EFIAPI
1065ShellGetExecutionBreakFlag(
1066 VOID
1067 )
1068{
jcarsey1e6e84c2010-01-25 20:05:08 +00001069 //
jcarsey94b17fa2009-05-07 18:46:18 +00001070 // Check for UEFI Shell 2.0 protocols
1071 //
jcarsey366f81a2011-06-27 21:04:22 +00001072 if (gEfiShellProtocol != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00001073
1074 //
1075 // We are using UEFI Shell 2.0; see if the event has been triggered
1076 //
jcarsey366f81a2011-06-27 21:04:22 +00001077 if (gBS->CheckEvent(gEfiShellProtocol->ExecutionBreak) != EFI_SUCCESS) {
jcarsey94b17fa2009-05-07 18:46:18 +00001078 return (FALSE);
1079 }
1080 return (TRUE);
jcarsey1e6e84c2010-01-25 20:05:08 +00001081 }
jcarsey94b17fa2009-05-07 18:46:18 +00001082
1083 //
1084 // using EFI Shell; call the function to check
1085 //
jcarsey92a54472011-06-27 20:33:13 +00001086 if (mEfiShellEnvironment2 != NULL) {
1087 return (mEfiShellEnvironment2->GetExecutionBreak());
1088 }
1089
1090 return (FALSE);
jcarsey94b17fa2009-05-07 18:46:18 +00001091}
1092/**
1093 return the value of an environment variable
1094
jcarsey1e6e84c2010-01-25 20:05:08 +00001095 this function gets the value of the environment variable set by the
jcarsey94b17fa2009-05-07 18:46:18 +00001096 ShellSetEnvironmentVariable function
1097
1098 @param EnvKey The key name of the environment variable.
1099
1100 @retval NULL the named environment variable does not exist.
1101 @return != NULL pointer to the value of the environment variable
1102**/
1103CONST CHAR16*
1104EFIAPI
1105ShellGetEnvironmentVariable (
jcarsey9b3bf082009-06-23 21:15:07 +00001106 IN CONST CHAR16 *EnvKey
jcarsey94b17fa2009-05-07 18:46:18 +00001107 )
1108{
jcarsey1e6e84c2010-01-25 20:05:08 +00001109 //
jcarsey94b17fa2009-05-07 18:46:18 +00001110 // Check for UEFI Shell 2.0 protocols
1111 //
jcarsey366f81a2011-06-27 21:04:22 +00001112 if (gEfiShellProtocol != NULL) {
1113 return (gEfiShellProtocol->GetEnv(EnvKey));
jcarsey94b17fa2009-05-07 18:46:18 +00001114 }
1115
1116 //
jcarsey92a54472011-06-27 20:33:13 +00001117 // Check for EFI shell
jcarsey94b17fa2009-05-07 18:46:18 +00001118 //
jcarsey92a54472011-06-27 20:33:13 +00001119 if (mEfiShellEnvironment2 != NULL) {
1120 return (mEfiShellEnvironment2->GetEnv((CHAR16*)EnvKey));
1121 }
jcarsey94b17fa2009-05-07 18:46:18 +00001122
jcarsey92a54472011-06-27 20:33:13 +00001123 return NULL;
jcarsey94b17fa2009-05-07 18:46:18 +00001124}
1125/**
1126 set the value of an environment variable
1127
1128This function changes the current value of the specified environment variable. If the
1129environment variable exists and the Value is an empty string, then the environment
1130variable is deleted. If the environment variable exists and the Value is not an empty
1131string, then the value of the environment variable is changed. If the environment
1132variable does not exist and the Value is an empty string, there is no action. If the
1133environment variable does not exist and the Value is a non-empty string, then the
1134environment variable is created and assigned the specified value.
1135
1136 This is not supported pre-UEFI Shell 2.0.
1137
1138 @param EnvKey The key name of the environment variable.
1139 @param EnvVal The Value of the environment variable
1140 @param Volatile Indicates whether the variable is non-volatile (FALSE) or volatile (TRUE).
1141
1142 @retval EFI_SUCCESS the operation was completed sucessfully
1143 @retval EFI_UNSUPPORTED This operation is not allowed in pre UEFI 2.0 Shell environments
1144**/
1145EFI_STATUS
1146EFIAPI
1147ShellSetEnvironmentVariable (
1148 IN CONST CHAR16 *EnvKey,
1149 IN CONST CHAR16 *EnvVal,
1150 IN BOOLEAN Volatile
1151 )
1152{
jcarsey1e6e84c2010-01-25 20:05:08 +00001153 //
jcarsey94b17fa2009-05-07 18:46:18 +00001154 // Check for UEFI Shell 2.0 protocols
1155 //
jcarsey366f81a2011-06-27 21:04:22 +00001156 if (gEfiShellProtocol != NULL) {
1157 return (gEfiShellProtocol->SetEnv(EnvKey, EnvVal, Volatile));
jcarsey1e6e84c2010-01-25 20:05:08 +00001158 }
jcarsey94b17fa2009-05-07 18:46:18 +00001159
1160 //
1161 // This feature does not exist under EFI shell
1162 //
1163 return (EFI_UNSUPPORTED);
1164}
jcarseya405b862010-09-14 05:18:09 +00001165
jcarsey94b17fa2009-05-07 18:46:18 +00001166/**
jcarseya405b862010-09-14 05:18:09 +00001167 Cause the shell to parse and execute a command line.
jcarsey94b17fa2009-05-07 18:46:18 +00001168
1169 This function creates a nested instance of the shell and executes the specified
jcarseya405b862010-09-14 05:18:09 +00001170 command (CommandLine) with the specified environment (Environment). Upon return,
1171 the status code returned by the specified command is placed in StatusCode.
1172 If Environment is NULL, then the current environment is used and all changes made
1173 by the commands executed will be reflected in the current environment. If the
1174 Environment is non-NULL, then the changes made will be discarded.
1175 The CommandLine is executed from the current working directory on the current
1176 device.
jcarsey94b17fa2009-05-07 18:46:18 +00001177
Brendan Jackman3877d0f2014-01-24 22:31:07 +00001178 The EnvironmentVariables pararemeter is ignored in a pre-UEFI Shell 2.0
jcarseya405b862010-09-14 05:18:09 +00001179 environment. The values pointed to by the parameters will be unchanged by the
1180 ShellExecute() function. The Output parameter has no effect in a
1181 UEFI Shell 2.0 environment.
jcarsey94b17fa2009-05-07 18:46:18 +00001182
jcarseya405b862010-09-14 05:18:09 +00001183 @param[in] ParentHandle The parent image starting the operation.
1184 @param[in] CommandLine The pointer to a NULL terminated command line.
1185 @param[in] Output True to display debug output. False to hide it.
1186 @param[in] EnvironmentVariables Optional pointer to array of environment variables
1187 in the form "x=y". If NULL, the current set is used.
1188 @param[out] Status The status of the run command line.
jcarsey94b17fa2009-05-07 18:46:18 +00001189
jcarseya405b862010-09-14 05:18:09 +00001190 @retval EFI_SUCCESS The operation completed sucessfully. Status
1191 contains the status code returned.
1192 @retval EFI_INVALID_PARAMETER A parameter contains an invalid value.
1193 @retval EFI_OUT_OF_RESOURCES Out of resources.
1194 @retval EFI_UNSUPPORTED The operation is not allowed.
jcarsey94b17fa2009-05-07 18:46:18 +00001195**/
1196EFI_STATUS
1197EFIAPI
1198ShellExecute (
1199 IN EFI_HANDLE *ParentHandle,
1200 IN CHAR16 *CommandLine OPTIONAL,
1201 IN BOOLEAN Output OPTIONAL,
1202 IN CHAR16 **EnvironmentVariables OPTIONAL,
1203 OUT EFI_STATUS *Status OPTIONAL
1204 )
1205{
Brendan Jackman3877d0f2014-01-24 22:31:07 +00001206 EFI_STATUS CmdStatus;
jcarsey1e6e84c2010-01-25 20:05:08 +00001207 //
jcarsey94b17fa2009-05-07 18:46:18 +00001208 // Check for UEFI Shell 2.0 protocols
1209 //
jcarsey366f81a2011-06-27 21:04:22 +00001210 if (gEfiShellProtocol != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00001211 //
1212 // Call UEFI Shell 2.0 version (not using Output parameter)
1213 //
jcarsey366f81a2011-06-27 21:04:22 +00001214 return (gEfiShellProtocol->Execute(ParentHandle,
jcarsey94b17fa2009-05-07 18:46:18 +00001215 CommandLine,
1216 EnvironmentVariables,
1217 Status));
jcarsey1e6e84c2010-01-25 20:05:08 +00001218 }
jcarsey92a54472011-06-27 20:33:13 +00001219
jcarsey94b17fa2009-05-07 18:46:18 +00001220 //
jcarsey92a54472011-06-27 20:33:13 +00001221 // Check for EFI shell
jcarsey94b17fa2009-05-07 18:46:18 +00001222 //
jcarsey92a54472011-06-27 20:33:13 +00001223 if (mEfiShellEnvironment2 != NULL) {
1224 //
Brendan Jackman3877d0f2014-01-24 22:31:07 +00001225 // Call EFI Shell version.
jcarsey92a54472011-06-27 20:33:13 +00001226 // Due to oddity in the EFI shell we want to dereference the ParentHandle here
1227 //
Brendan Jackman3877d0f2014-01-24 22:31:07 +00001228 CmdStatus = (mEfiShellEnvironment2->Execute(*ParentHandle,
jcarsey92a54472011-06-27 20:33:13 +00001229 CommandLine,
1230 Output));
Brendan Jackman3877d0f2014-01-24 22:31:07 +00001231 //
1232 // No Status output parameter so just use the returned status
1233 //
1234 if (Status != NULL) {
1235 *Status = CmdStatus;
1236 }
1237 //
1238 // If there was an error, we can't tell if it was from the command or from
1239 // the Execute() function, so we'll just assume the shell ran successfully
1240 // and the error came from the command.
1241 //
1242 return EFI_SUCCESS;
jcarsey92a54472011-06-27 20:33:13 +00001243 }
1244
1245 return (EFI_UNSUPPORTED);
jcarsey94b17fa2009-05-07 18:46:18 +00001246}
Brendan Jackman3877d0f2014-01-24 22:31:07 +00001247
jcarsey94b17fa2009-05-07 18:46:18 +00001248/**
1249 Retreives the current directory path
1250
jcarsey1e6e84c2010-01-25 20:05:08 +00001251 If the DeviceName is NULL, it returns the current device's current directory
1252 name. If the DeviceName is not NULL, it returns the current directory name
jcarsey94b17fa2009-05-07 18:46:18 +00001253 on specified drive.
1254
1255 @param DeviceName the name of the drive to get directory on
1256
1257 @retval NULL the directory does not exist
1258 @return != NULL the directory
1259**/
1260CONST CHAR16*
1261EFIAPI
1262ShellGetCurrentDir (
jcarseya405b862010-09-14 05:18:09 +00001263 IN CHAR16 * CONST DeviceName OPTIONAL
jcarsey94b17fa2009-05-07 18:46:18 +00001264 )
1265{
jcarsey1e6e84c2010-01-25 20:05:08 +00001266 //
jcarsey94b17fa2009-05-07 18:46:18 +00001267 // Check for UEFI Shell 2.0 protocols
1268 //
jcarsey366f81a2011-06-27 21:04:22 +00001269 if (gEfiShellProtocol != NULL) {
1270 return (gEfiShellProtocol->GetCurDir(DeviceName));
jcarsey1e6e84c2010-01-25 20:05:08 +00001271 }
jcarsey92a54472011-06-27 20:33:13 +00001272
jcarsey94b17fa2009-05-07 18:46:18 +00001273 //
jcarsey8bd282b2011-06-27 16:45:41 +00001274 // Check for EFI shell
jcarsey94b17fa2009-05-07 18:46:18 +00001275 //
jcarsey8bd282b2011-06-27 16:45:41 +00001276 if (mEfiShellEnvironment2 != NULL) {
1277 return (mEfiShellEnvironment2->CurDir(DeviceName));
1278 }
1279
1280 return (NULL);
jcarsey94b17fa2009-05-07 18:46:18 +00001281}
1282/**
1283 sets (enabled or disabled) the page break mode
1284
jcarsey1e6e84c2010-01-25 20:05:08 +00001285 when page break mode is enabled the screen will stop scrolling
jcarsey94b17fa2009-05-07 18:46:18 +00001286 and wait for operator input before scrolling a subsequent screen.
1287
1288 @param CurrentState TRUE to enable and FALSE to disable
1289**/
jcarsey1e6e84c2010-01-25 20:05:08 +00001290VOID
jcarsey94b17fa2009-05-07 18:46:18 +00001291EFIAPI
1292ShellSetPageBreakMode (
1293 IN BOOLEAN CurrentState
1294 )
1295{
1296 //
1297 // check for enabling
1298 //
1299 if (CurrentState != 0x00) {
jcarsey1e6e84c2010-01-25 20:05:08 +00001300 //
jcarsey94b17fa2009-05-07 18:46:18 +00001301 // check for UEFI Shell 2.0
1302 //
jcarsey366f81a2011-06-27 21:04:22 +00001303 if (gEfiShellProtocol != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00001304 //
1305 // Enable with UEFI 2.0 Shell
1306 //
jcarsey366f81a2011-06-27 21:04:22 +00001307 gEfiShellProtocol->EnablePageBreak();
jcarsey94b17fa2009-05-07 18:46:18 +00001308 return;
1309 } else {
jcarsey1e6e84c2010-01-25 20:05:08 +00001310 //
jcarsey92a54472011-06-27 20:33:13 +00001311 // Check for EFI shell
jcarsey94b17fa2009-05-07 18:46:18 +00001312 //
jcarsey92a54472011-06-27 20:33:13 +00001313 if (mEfiShellEnvironment2 != NULL) {
1314 //
1315 // Enable with EFI Shell
1316 //
1317 mEfiShellEnvironment2->EnablePageBreak (DEFAULT_INIT_ROW, DEFAULT_AUTO_LF);
1318 return;
1319 }
jcarsey94b17fa2009-05-07 18:46:18 +00001320 }
1321 } else {
jcarsey1e6e84c2010-01-25 20:05:08 +00001322 //
jcarsey94b17fa2009-05-07 18:46:18 +00001323 // check for UEFI Shell 2.0
1324 //
jcarsey366f81a2011-06-27 21:04:22 +00001325 if (gEfiShellProtocol != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00001326 //
1327 // Disable with UEFI 2.0 Shell
1328 //
jcarsey366f81a2011-06-27 21:04:22 +00001329 gEfiShellProtocol->DisablePageBreak();
jcarsey94b17fa2009-05-07 18:46:18 +00001330 return;
1331 } else {
jcarsey1e6e84c2010-01-25 20:05:08 +00001332 //
jcarsey92a54472011-06-27 20:33:13 +00001333 // Check for EFI shell
jcarsey94b17fa2009-05-07 18:46:18 +00001334 //
jcarsey92a54472011-06-27 20:33:13 +00001335 if (mEfiShellEnvironment2 != NULL) {
1336 //
1337 // Disable with EFI Shell
1338 //
1339 mEfiShellEnvironment2->DisablePageBreak ();
1340 return;
1341 }
jcarsey94b17fa2009-05-07 18:46:18 +00001342 }
1343 }
1344}
1345
1346///
1347/// version of EFI_SHELL_FILE_INFO struct, except has no CONST pointers.
1348/// This allows for the struct to be populated.
1349///
1350typedef struct {
jcarseyd2b45642009-05-11 18:02:16 +00001351 LIST_ENTRY Link;
jcarsey94b17fa2009-05-07 18:46:18 +00001352 EFI_STATUS Status;
1353 CHAR16 *FullName;
1354 CHAR16 *FileName;
jcarseya405b862010-09-14 05:18:09 +00001355 SHELL_FILE_HANDLE Handle;
jcarsey94b17fa2009-05-07 18:46:18 +00001356 EFI_FILE_INFO *Info;
1357} EFI_SHELL_FILE_INFO_NO_CONST;
1358
1359/**
1360 Converts a EFI shell list of structures to the coresponding UEFI Shell 2.0 type of list.
1361
1362 if OldStyleFileList is NULL then ASSERT()
1363
jcarsey1e6e84c2010-01-25 20:05:08 +00001364 this function will convert a SHELL_FILE_ARG based list into a callee allocated
jcarsey94b17fa2009-05-07 18:46:18 +00001365 EFI_SHELL_FILE_INFO based list. it is up to the caller to free the memory via
1366 the ShellCloseFileMetaArg function.
1367
ydong104ff7e372011-09-02 08:05:34 +00001368 @param[in] FileList the EFI shell list type
1369 @param[in, out] ListHead the list to add to
jcarsey94b17fa2009-05-07 18:46:18 +00001370
1371 @retval the resultant head of the double linked new format list;
1372**/
1373LIST_ENTRY*
1374EFIAPI
1375InternalShellConvertFileListType (
jcarsey9b3bf082009-06-23 21:15:07 +00001376 IN LIST_ENTRY *FileList,
1377 IN OUT LIST_ENTRY *ListHead
jcarsey125c2cf2009-11-18 21:36:50 +00001378 )
1379{
jcarsey94b17fa2009-05-07 18:46:18 +00001380 SHELL_FILE_ARG *OldInfo;
jcarsey9b3bf082009-06-23 21:15:07 +00001381 LIST_ENTRY *Link;
jcarsey94b17fa2009-05-07 18:46:18 +00001382 EFI_SHELL_FILE_INFO_NO_CONST *NewInfo;
1383
1384 //
jcarsey9b3bf082009-06-23 21:15:07 +00001385 // ASSERTs
jcarsey94b17fa2009-05-07 18:46:18 +00001386 //
jcarsey9b3bf082009-06-23 21:15:07 +00001387 ASSERT(FileList != NULL);
1388 ASSERT(ListHead != NULL);
jcarsey94b17fa2009-05-07 18:46:18 +00001389
1390 //
1391 // enumerate through each member of the old list and copy
1392 //
jcarseyd2b45642009-05-11 18:02:16 +00001393 for (Link = FileList->ForwardLink; Link != FileList; Link = Link->ForwardLink) {
jcarsey94b17fa2009-05-07 18:46:18 +00001394 OldInfo = CR (Link, SHELL_FILE_ARG, Link, SHELL_FILE_ARG_SIGNATURE);
jcarseya405b862010-09-14 05:18:09 +00001395 ASSERT(OldInfo != NULL);
1396
1397 //
1398 // Skip ones that failed to open...
1399 //
1400 if (OldInfo->Status != EFI_SUCCESS) {
1401 continue;
1402 }
jcarsey94b17fa2009-05-07 18:46:18 +00001403
1404 //
1405 // make sure the old list was valid
1406 //
jcarsey94b17fa2009-05-07 18:46:18 +00001407 ASSERT(OldInfo->Info != NULL);
1408 ASSERT(OldInfo->FullName != NULL);
1409 ASSERT(OldInfo->FileName != NULL);
1410
1411 //
1412 // allocate a new EFI_SHELL_FILE_INFO object
1413 //
1414 NewInfo = AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));
jcarseyc9d92df2010-02-03 15:37:54 +00001415 if (NewInfo == NULL) {
jcarsey7a95efd2011-10-13 16:08:18 +00001416 ShellCloseFileMetaArg((EFI_SHELL_FILE_INFO**)(&ListHead));
jcarseybeab0fc2011-10-10 17:26:25 +00001417 ListHead = NULL;
jcarseyc9d92df2010-02-03 15:37:54 +00001418 break;
1419 }
jcarsey1e6e84c2010-01-25 20:05:08 +00001420
1421 //
jcarsey94b17fa2009-05-07 18:46:18 +00001422 // copy the simple items
1423 //
1424 NewInfo->Handle = OldInfo->Handle;
1425 NewInfo->Status = OldInfo->Status;
1426
jcarseyd2b45642009-05-11 18:02:16 +00001427 // old shell checks for 0 not NULL
1428 OldInfo->Handle = 0;
1429
jcarsey94b17fa2009-05-07 18:46:18 +00001430 //
1431 // allocate new space to copy strings and structure
1432 //
Jaben Carsey98c16be2014-08-19 21:00:34 +00001433 NewInfo->FullName = AllocateCopyPool(StrSize(OldInfo->FullName), OldInfo->FullName);
1434 NewInfo->FileName = AllocateCopyPool(StrSize(OldInfo->FileName), OldInfo->FileName);
1435 NewInfo->Info = AllocateCopyPool((UINTN)OldInfo->Info->Size, OldInfo->Info);
jcarsey1e6e84c2010-01-25 20:05:08 +00001436
jcarsey94b17fa2009-05-07 18:46:18 +00001437 //
1438 // make sure all the memory allocations were sucessful
1439 //
jcarseybeab0fc2011-10-10 17:26:25 +00001440 if (NULL == NewInfo->FullName || NewInfo->FileName == NULL || NewInfo->Info == NULL) {
Jaben Carsey98c16be2014-08-19 21:00:34 +00001441 //
1442 // Free the partially allocated new node
1443 //
1444 SHELL_FREE_NON_NULL(NewInfo->FullName);
1445 SHELL_FREE_NON_NULL(NewInfo->FileName);
1446 SHELL_FREE_NON_NULL(NewInfo->Info);
1447 SHELL_FREE_NON_NULL(NewInfo);
1448
1449 //
1450 // Free the previously converted stuff
1451 //
jcarsey7a95efd2011-10-13 16:08:18 +00001452 ShellCloseFileMetaArg((EFI_SHELL_FILE_INFO**)(&ListHead));
jcarseybeab0fc2011-10-10 17:26:25 +00001453 ListHead = NULL;
1454 break;
1455 }
jcarsey94b17fa2009-05-07 18:46:18 +00001456
1457 //
jcarsey94b17fa2009-05-07 18:46:18 +00001458 // add that to the list
1459 //
jcarsey9b3bf082009-06-23 21:15:07 +00001460 InsertTailList(ListHead, &NewInfo->Link);
jcarsey94b17fa2009-05-07 18:46:18 +00001461 }
1462 return (ListHead);
1463}
1464/**
1465 Opens a group of files based on a path.
1466
jcarsey1e6e84c2010-01-25 20:05:08 +00001467 This function uses the Arg to open all the matching files. Each matched
Brendan Jackman70879312014-01-24 22:29:53 +00001468 file has a SHELL_FILE_INFO structure to record the file information. These
1469 structures are placed on the list ListHead. Users can get the SHELL_FILE_INFO
jcarsey94b17fa2009-05-07 18:46:18 +00001470 structures from ListHead to access each file. This function supports wildcards
jcarsey1e6e84c2010-01-25 20:05:08 +00001471 and will process '?' and '*' as such. the list must be freed with a call to
jcarsey94b17fa2009-05-07 18:46:18 +00001472 ShellCloseFileMetaArg().
1473
jcarsey1e6e84c2010-01-25 20:05:08 +00001474 If you are NOT appending to an existing list *ListHead must be NULL. If
jcarsey5f7431d2009-07-10 18:06:01 +00001475 *ListHead is NULL then it must be callee freed.
jcarsey94b17fa2009-05-07 18:46:18 +00001476
1477 @param Arg pointer to path string
1478 @param OpenMode mode to open files with
1479 @param ListHead head of linked list of results
1480
jcarsey1e6e84c2010-01-25 20:05:08 +00001481 @retval EFI_SUCCESS the operation was sucessful and the list head
jcarsey94b17fa2009-05-07 18:46:18 +00001482 contains the list of opened files
jcarsey94b17fa2009-05-07 18:46:18 +00001483 @return != EFI_SUCCESS the operation failed
1484
1485 @sa InternalShellConvertFileListType
1486**/
1487EFI_STATUS
1488EFIAPI
1489ShellOpenFileMetaArg (
1490 IN CHAR16 *Arg,
1491 IN UINT64 OpenMode,
1492 IN OUT EFI_SHELL_FILE_INFO **ListHead
1493 )
1494{
1495 EFI_STATUS Status;
jcarsey9b3bf082009-06-23 21:15:07 +00001496 LIST_ENTRY mOldStyleFileList;
Qiu Shumin0960ba12014-09-17 07:58:31 +00001497 CHAR16 *CleanFilePathStr;
jcarsey1e6e84c2010-01-25 20:05:08 +00001498
jcarsey94b17fa2009-05-07 18:46:18 +00001499 //
1500 // ASSERT that Arg and ListHead are not NULL
1501 //
1502 ASSERT(Arg != NULL);
1503 ASSERT(ListHead != NULL);
1504
Qiu Shumin715096c2014-09-19 01:32:05 +00001505 CleanFilePathStr = NULL;
1506
Qiu Shumin0960ba12014-09-17 07:58:31 +00001507 Status = InternalShellStripQuotes (Arg, &CleanFilePathStr);
1508 if (EFI_ERROR (Status)) {
1509 return Status;
1510 }
1511
jcarsey1e6e84c2010-01-25 20:05:08 +00001512 //
jcarsey94b17fa2009-05-07 18:46:18 +00001513 // Check for UEFI Shell 2.0 protocols
1514 //
jcarsey366f81a2011-06-27 21:04:22 +00001515 if (gEfiShellProtocol != NULL) {
jcarsey5f7431d2009-07-10 18:06:01 +00001516 if (*ListHead == NULL) {
1517 *ListHead = (EFI_SHELL_FILE_INFO*)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));
1518 if (*ListHead == NULL) {
Qiu Shumin0960ba12014-09-17 07:58:31 +00001519 FreePool(CleanFilePathStr);
jcarsey5f7431d2009-07-10 18:06:01 +00001520 return (EFI_OUT_OF_RESOURCES);
1521 }
1522 InitializeListHead(&((*ListHead)->Link));
jcarsey1e6e84c2010-01-25 20:05:08 +00001523 }
Qiu Shumin0960ba12014-09-17 07:58:31 +00001524 Status = gEfiShellProtocol->OpenFileList(CleanFilePathStr,
jcarsey1e6e84c2010-01-25 20:05:08 +00001525 OpenMode,
jcarsey2247dde2009-11-09 18:08:58 +00001526 ListHead);
1527 if (EFI_ERROR(Status)) {
jcarsey366f81a2011-06-27 21:04:22 +00001528 gEfiShellProtocol->RemoveDupInFileList(ListHead);
jcarsey2247dde2009-11-09 18:08:58 +00001529 } else {
jcarsey366f81a2011-06-27 21:04:22 +00001530 Status = gEfiShellProtocol->RemoveDupInFileList(ListHead);
jcarsey2247dde2009-11-09 18:08:58 +00001531 }
jcarseya405b862010-09-14 05:18:09 +00001532 if (*ListHead != NULL && IsListEmpty(&(*ListHead)->Link)) {
1533 FreePool(*ListHead);
Qiu Shumin0960ba12014-09-17 07:58:31 +00001534 FreePool(CleanFilePathStr);
jcarseya405b862010-09-14 05:18:09 +00001535 *ListHead = NULL;
1536 return (EFI_NOT_FOUND);
1537 }
Qiu Shumin0960ba12014-09-17 07:58:31 +00001538 FreePool(CleanFilePathStr);
jcarsey2247dde2009-11-09 18:08:58 +00001539 return (Status);
jcarsey1e6e84c2010-01-25 20:05:08 +00001540 }
jcarsey94b17fa2009-05-07 18:46:18 +00001541
1542 //
jcarsey92a54472011-06-27 20:33:13 +00001543 // Check for EFI shell
jcarsey94b17fa2009-05-07 18:46:18 +00001544 //
jcarsey92a54472011-06-27 20:33:13 +00001545 if (mEfiShellEnvironment2 != NULL) {
1546 //
1547 // make sure the list head is initialized
1548 //
1549 InitializeListHead(&mOldStyleFileList);
jcarsey94b17fa2009-05-07 18:46:18 +00001550
jcarsey92a54472011-06-27 20:33:13 +00001551 //
1552 // Get the EFI Shell list of files
1553 //
Qiu Shumin0960ba12014-09-17 07:58:31 +00001554 Status = mEfiShellEnvironment2->FileMetaArg(CleanFilePathStr, &mOldStyleFileList);
jcarsey92a54472011-06-27 20:33:13 +00001555 if (EFI_ERROR(Status)) {
1556 *ListHead = NULL;
Qiu Shumin0960ba12014-09-17 07:58:31 +00001557 FreePool(CleanFilePathStr);
jcarsey92a54472011-06-27 20:33:13 +00001558 return (Status);
1559 }
jcarsey94b17fa2009-05-07 18:46:18 +00001560
jcarsey92a54472011-06-27 20:33:13 +00001561 if (*ListHead == NULL) {
1562 *ListHead = (EFI_SHELL_FILE_INFO *)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));
1563 if (*ListHead == NULL) {
Qiu Shumin0960ba12014-09-17 07:58:31 +00001564 FreePool(CleanFilePathStr);
jcarsey92a54472011-06-27 20:33:13 +00001565 return (EFI_OUT_OF_RESOURCES);
1566 }
1567 InitializeListHead(&((*ListHead)->Link));
1568 }
1569
1570 //
1571 // Convert that to equivalent of UEFI Shell 2.0 structure
1572 //
1573 InternalShellConvertFileListType(&mOldStyleFileList, &(*ListHead)->Link);
1574
1575 //
1576 // Free the EFI Shell version that was converted.
1577 //
1578 mEfiShellEnvironment2->FreeFileList(&mOldStyleFileList);
1579
1580 if ((*ListHead)->Link.ForwardLink == (*ListHead)->Link.BackLink && (*ListHead)->Link.BackLink == &((*ListHead)->Link)) {
1581 FreePool(*ListHead);
1582 *ListHead = NULL;
1583 Status = EFI_NOT_FOUND;
1584 }
Qiu Shumin0960ba12014-09-17 07:58:31 +00001585 FreePool(CleanFilePathStr);
jcarsey94b17fa2009-05-07 18:46:18 +00001586 return (Status);
1587 }
1588
Qiu Shumin0960ba12014-09-17 07:58:31 +00001589 FreePool(CleanFilePathStr);
jcarsey92a54472011-06-27 20:33:13 +00001590 return (EFI_UNSUPPORTED);
jcarsey94b17fa2009-05-07 18:46:18 +00001591}
1592/**
jcarseya405b862010-09-14 05:18:09 +00001593 Free the linked list returned from ShellOpenFileMetaArg.
jcarsey94b17fa2009-05-07 18:46:18 +00001594
jcarseya405b862010-09-14 05:18:09 +00001595 if ListHead is NULL then ASSERT().
jcarsey94b17fa2009-05-07 18:46:18 +00001596
jcarseya405b862010-09-14 05:18:09 +00001597 @param ListHead the pointer to free.
jcarsey94b17fa2009-05-07 18:46:18 +00001598
jcarseya405b862010-09-14 05:18:09 +00001599 @retval EFI_SUCCESS the operation was sucessful.
jcarsey94b17fa2009-05-07 18:46:18 +00001600**/
1601EFI_STATUS
1602EFIAPI
1603ShellCloseFileMetaArg (
1604 IN OUT EFI_SHELL_FILE_INFO **ListHead
1605 )
1606{
1607 LIST_ENTRY *Node;
1608
1609 //
1610 // ASSERT that ListHead is not NULL
1611 //
1612 ASSERT(ListHead != NULL);
1613
jcarsey1e6e84c2010-01-25 20:05:08 +00001614 //
jcarsey94b17fa2009-05-07 18:46:18 +00001615 // Check for UEFI Shell 2.0 protocols
1616 //
jcarsey366f81a2011-06-27 21:04:22 +00001617 if (gEfiShellProtocol != NULL) {
1618 return (gEfiShellProtocol->FreeFileList(ListHead));
jcarsey92a54472011-06-27 20:33:13 +00001619 } else if (mEfiShellEnvironment2 != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00001620 //
jcarsey1e6e84c2010-01-25 20:05:08 +00001621 // Since this is EFI Shell version we need to free our internally made copy
jcarsey94b17fa2009-05-07 18:46:18 +00001622 // of the list
1623 //
jcarsey1e6e84c2010-01-25 20:05:08 +00001624 for ( Node = GetFirstNode(&(*ListHead)->Link)
jcarseya405b862010-09-14 05:18:09 +00001625 ; *ListHead != NULL && !IsListEmpty(&(*ListHead)->Link)
jcarsey9b3bf082009-06-23 21:15:07 +00001626 ; Node = GetFirstNode(&(*ListHead)->Link)) {
jcarsey94b17fa2009-05-07 18:46:18 +00001627 RemoveEntryList(Node);
jcarseya405b862010-09-14 05:18:09 +00001628 ((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 +00001629 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->FullName);
1630 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->FileName);
1631 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->Info);
1632 FreePool((EFI_SHELL_FILE_INFO_NO_CONST*)Node);
1633 }
Jaben Carsey75a5e2e2014-01-10 16:42:45 +00001634 SHELL_FREE_NON_NULL(*ListHead);
jcarsey94b17fa2009-05-07 18:46:18 +00001635 return EFI_SUCCESS;
1636 }
jcarsey92a54472011-06-27 20:33:13 +00001637
1638 return (EFI_UNSUPPORTED);
jcarsey94b17fa2009-05-07 18:46:18 +00001639}
1640
jcarsey125c2cf2009-11-18 21:36:50 +00001641/**
1642 Find a file by searching the CWD and then the path.
1643
jcarseyb3011f42010-01-11 21:49:04 +00001644 If FileName is NULL then ASSERT.
jcarsey125c2cf2009-11-18 21:36:50 +00001645
jcarseyb3011f42010-01-11 21:49:04 +00001646 If the return value is not NULL then the memory must be caller freed.
jcarsey125c2cf2009-11-18 21:36:50 +00001647
1648 @param FileName Filename string.
1649
1650 @retval NULL the file was not found
1651 @return !NULL the full path to the file.
1652**/
1653CHAR16 *
1654EFIAPI
1655ShellFindFilePath (
1656 IN CONST CHAR16 *FileName
1657 )
1658{
1659 CONST CHAR16 *Path;
jcarseya405b862010-09-14 05:18:09 +00001660 SHELL_FILE_HANDLE Handle;
jcarsey125c2cf2009-11-18 21:36:50 +00001661 EFI_STATUS Status;
1662 CHAR16 *RetVal;
1663 CHAR16 *TestPath;
1664 CONST CHAR16 *Walker;
jcarsey36a9d672009-11-20 21:13:41 +00001665 UINTN Size;
jcarsey1cd45e72010-01-29 15:07:44 +00001666 CHAR16 *TempChar;
jcarsey125c2cf2009-11-18 21:36:50 +00001667
1668 RetVal = NULL;
1669
jcarseya405b862010-09-14 05:18:09 +00001670 //
1671 // First make sure its not an absolute path.
1672 //
1673 Status = ShellOpenFileByName(FileName, &Handle, EFI_FILE_MODE_READ, 0);
1674 if (!EFI_ERROR(Status)){
1675 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {
1676 ASSERT(RetVal == NULL);
1677 RetVal = StrnCatGrow(&RetVal, NULL, FileName, 0);
1678 ShellCloseFile(&Handle);
1679 return (RetVal);
1680 } else {
1681 ShellCloseFile(&Handle);
1682 }
1683 }
1684
jcarsey125c2cf2009-11-18 21:36:50 +00001685 Path = ShellGetEnvironmentVariable(L"cwd");
1686 if (Path != NULL) {
jcarsey36a9d672009-11-20 21:13:41 +00001687 Size = StrSize(Path);
1688 Size += StrSize(FileName);
1689 TestPath = AllocateZeroPool(Size);
jcarseyc9d92df2010-02-03 15:37:54 +00001690 if (TestPath == NULL) {
1691 return (NULL);
1692 }
Jaben Carsey98c16be2014-08-19 21:00:34 +00001693 StrnCpy(TestPath, Path, Size/sizeof(CHAR16) - 1);
1694 StrnCat(TestPath, FileName, Size/sizeof(CHAR16) - 1 - StrLen(TestPath));
jcarsey125c2cf2009-11-18 21:36:50 +00001695 Status = ShellOpenFileByName(TestPath, &Handle, EFI_FILE_MODE_READ, 0);
1696 if (!EFI_ERROR(Status)){
jcarseya405b862010-09-14 05:18:09 +00001697 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {
1698 ASSERT(RetVal == NULL);
1699 RetVal = StrnCatGrow(&RetVal, NULL, TestPath, 0);
1700 ShellCloseFile(&Handle);
1701 FreePool(TestPath);
1702 return (RetVal);
1703 } else {
1704 ShellCloseFile(&Handle);
1705 }
jcarsey125c2cf2009-11-18 21:36:50 +00001706 }
1707 FreePool(TestPath);
1708 }
1709 Path = ShellGetEnvironmentVariable(L"path");
1710 if (Path != NULL) {
jcarseya405b862010-09-14 05:18:09 +00001711 Size = StrSize(Path)+sizeof(CHAR16);
jcarsey36a9d672009-11-20 21:13:41 +00001712 Size += StrSize(FileName);
1713 TestPath = AllocateZeroPool(Size);
jcarsey3e082d52010-10-04 16:44:57 +00001714 if (TestPath == NULL) {
1715 return (NULL);
1716 }
jcarsey1e6e84c2010-01-25 20:05:08 +00001717 Walker = (CHAR16*)Path;
jcarsey125c2cf2009-11-18 21:36:50 +00001718 do {
1719 CopyMem(TestPath, Walker, StrSize(Walker));
jcarsey3e082d52010-10-04 16:44:57 +00001720 if (TestPath != NULL) {
1721 TempChar = StrStr(TestPath, L";");
1722 if (TempChar != NULL) {
1723 *TempChar = CHAR_NULL;
1724 }
1725 if (TestPath[StrLen(TestPath)-1] != L'\\') {
Jaben Carsey98c16be2014-08-19 21:00:34 +00001726 StrnCat(TestPath, L"\\", Size/sizeof(CHAR16) - 1 - StrLen(TestPath));
jcarsey3e082d52010-10-04 16:44:57 +00001727 }
jcarsey89e85372011-04-13 23:37:50 +00001728 if (FileName[0] == L'\\') {
1729 FileName++;
1730 }
Jaben Carsey98c16be2014-08-19 21:00:34 +00001731 StrnCat(TestPath, FileName, Size/sizeof(CHAR16) - 1 - StrLen(TestPath));
jcarsey3e082d52010-10-04 16:44:57 +00001732 if (StrStr(Walker, L";") != NULL) {
1733 Walker = StrStr(Walker, L";") + 1;
jcarseya405b862010-09-14 05:18:09 +00001734 } else {
jcarsey3e082d52010-10-04 16:44:57 +00001735 Walker = NULL;
1736 }
1737 Status = ShellOpenFileByName(TestPath, &Handle, EFI_FILE_MODE_READ, 0);
1738 if (!EFI_ERROR(Status)){
1739 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {
1740 ASSERT(RetVal == NULL);
1741 RetVal = StrnCatGrow(&RetVal, NULL, TestPath, 0);
1742 ShellCloseFile(&Handle);
1743 break;
1744 } else {
1745 ShellCloseFile(&Handle);
1746 }
jcarseya405b862010-09-14 05:18:09 +00001747 }
jcarsey125c2cf2009-11-18 21:36:50 +00001748 }
1749 } while (Walker != NULL && Walker[0] != CHAR_NULL);
1750 FreePool(TestPath);
1751 }
1752 return (RetVal);
1753}
1754
jcarseyb3011f42010-01-11 21:49:04 +00001755/**
jcarsey1e6e84c2010-01-25 20:05:08 +00001756 Find a file by searching the CWD and then the path with a variable set of file
1757 extensions. If the file is not found it will append each extension in the list
jcarseyb3011f42010-01-11 21:49:04 +00001758 in the order provided and return the first one that is successful.
1759
1760 If FileName is NULL, then ASSERT.
1761 If FileExtension is NULL, then behavior is identical to ShellFindFilePath.
1762
1763 If the return value is not NULL then the memory must be caller freed.
1764
1765 @param[in] FileName Filename string.
1766 @param[in] FileExtension Semi-colon delimeted list of possible extensions.
1767
1768 @retval NULL The file was not found.
1769 @retval !NULL The path to the file.
1770**/
1771CHAR16 *
1772EFIAPI
1773ShellFindFilePathEx (
1774 IN CONST CHAR16 *FileName,
1775 IN CONST CHAR16 *FileExtension
1776 )
1777{
1778 CHAR16 *TestPath;
1779 CHAR16 *RetVal;
1780 CONST CHAR16 *ExtensionWalker;
jcarsey9e926b62010-01-14 20:26:39 +00001781 UINTN Size;
jcarsey1cd45e72010-01-29 15:07:44 +00001782 CHAR16 *TempChar;
jcarseyc9d92df2010-02-03 15:37:54 +00001783 CHAR16 *TempChar2;
jcarsey1cd45e72010-01-29 15:07:44 +00001784
jcarseyb3011f42010-01-11 21:49:04 +00001785 ASSERT(FileName != NULL);
1786 if (FileExtension == NULL) {
1787 return (ShellFindFilePath(FileName));
1788 }
1789 RetVal = ShellFindFilePath(FileName);
1790 if (RetVal != NULL) {
1791 return (RetVal);
1792 }
jcarsey9e926b62010-01-14 20:26:39 +00001793 Size = StrSize(FileName);
1794 Size += StrSize(FileExtension);
1795 TestPath = AllocateZeroPool(Size);
jcarseyc9d92df2010-02-03 15:37:54 +00001796 if (TestPath == NULL) {
1797 return (NULL);
1798 }
jcarseya405b862010-09-14 05:18:09 +00001799 for (ExtensionWalker = FileExtension, TempChar2 = (CHAR16*)FileExtension; TempChar2 != NULL ; ExtensionWalker = TempChar2 + 1){
Jaben Carsey98c16be2014-08-19 21:00:34 +00001800 StrnCpy(TestPath, FileName, Size/sizeof(CHAR16) - 1);
jcarseya405b862010-09-14 05:18:09 +00001801 if (ExtensionWalker != NULL) {
Jaben Carsey98c16be2014-08-19 21:00:34 +00001802 StrnCat(TestPath, ExtensionWalker, Size/sizeof(CHAR16) - 1 - StrLen(TestPath));
jcarseya405b862010-09-14 05:18:09 +00001803 }
jcarsey1cd45e72010-01-29 15:07:44 +00001804 TempChar = StrStr(TestPath, L";");
1805 if (TempChar != NULL) {
1806 *TempChar = CHAR_NULL;
jcarseyb3011f42010-01-11 21:49:04 +00001807 }
1808 RetVal = ShellFindFilePath(TestPath);
1809 if (RetVal != NULL) {
1810 break;
1811 }
jcarseya405b862010-09-14 05:18:09 +00001812 ASSERT(ExtensionWalker != NULL);
jcarseyc9d92df2010-02-03 15:37:54 +00001813 TempChar2 = StrStr(ExtensionWalker, L";");
jcarseyb3011f42010-01-11 21:49:04 +00001814 }
1815 FreePool(TestPath);
1816 return (RetVal);
1817}
1818
jcarsey94b17fa2009-05-07 18:46:18 +00001819typedef struct {
jcarsey9b3bf082009-06-23 21:15:07 +00001820 LIST_ENTRY Link;
jcarsey94b17fa2009-05-07 18:46:18 +00001821 CHAR16 *Name;
jcarseya405b862010-09-14 05:18:09 +00001822 SHELL_PARAM_TYPE Type;
jcarsey94b17fa2009-05-07 18:46:18 +00001823 CHAR16 *Value;
1824 UINTN OriginalPosition;
1825} SHELL_PARAM_PACKAGE;
1826
1827/**
jcarsey1e6e84c2010-01-25 20:05:08 +00001828 Checks the list of valid arguments and returns TRUE if the item was found. If the
jcarsey94b17fa2009-05-07 18:46:18 +00001829 return value is TRUE then the type parameter is set also.
jcarsey1e6e84c2010-01-25 20:05:08 +00001830
jcarsey94b17fa2009-05-07 18:46:18 +00001831 if CheckList is NULL then ASSERT();
1832 if Name is NULL then ASSERT();
1833 if Type is NULL then ASSERT();
1834
jcarsey94b17fa2009-05-07 18:46:18 +00001835 @param Name pointer to Name of parameter found
1836 @param CheckList List to check against
jcarseya405b862010-09-14 05:18:09 +00001837 @param Type pointer to type of parameter if it was found
jcarsey94b17fa2009-05-07 18:46:18 +00001838
1839 @retval TRUE the Parameter was found. Type is valid.
1840 @retval FALSE the Parameter was not found. Type is not valid.
1841**/
1842BOOLEAN
1843EFIAPI
jcarseyd2b45642009-05-11 18:02:16 +00001844InternalIsOnCheckList (
jcarsey94b17fa2009-05-07 18:46:18 +00001845 IN CONST CHAR16 *Name,
1846 IN CONST SHELL_PARAM_ITEM *CheckList,
jcarsey252d9452011-03-25 20:49:53 +00001847 OUT SHELL_PARAM_TYPE *Type
jcarseya405b862010-09-14 05:18:09 +00001848 )
1849{
jcarsey94b17fa2009-05-07 18:46:18 +00001850 SHELL_PARAM_ITEM *TempListItem;
jcarsey252d9452011-03-25 20:49:53 +00001851 CHAR16 *TempString;
jcarsey94b17fa2009-05-07 18:46:18 +00001852
1853 //
1854 // ASSERT that all 3 pointer parameters aren't NULL
1855 //
1856 ASSERT(CheckList != NULL);
1857 ASSERT(Type != NULL);
1858 ASSERT(Name != NULL);
1859
1860 //
jcarseyd2b45642009-05-11 18:02:16 +00001861 // question mark and page break mode are always supported
1862 //
1863 if ((StrCmp(Name, L"-?") == 0) ||
1864 (StrCmp(Name, L"-b") == 0)
jcarseya405b862010-09-14 05:18:09 +00001865 ) {
jcarsey252d9452011-03-25 20:49:53 +00001866 *Type = TypeFlag;
jcarseyd2b45642009-05-11 18:02:16 +00001867 return (TRUE);
1868 }
1869
1870 //
jcarsey94b17fa2009-05-07 18:46:18 +00001871 // Enumerate through the list
1872 //
1873 for (TempListItem = (SHELL_PARAM_ITEM*)CheckList ; TempListItem->Name != NULL ; TempListItem++) {
1874 //
jcarsey9eb53ac2009-07-08 17:26:58 +00001875 // If the Type is TypeStart only check the first characters of the passed in param
1876 // If it matches set the type and return TRUE
jcarsey94b17fa2009-05-07 18:46:18 +00001877 //
darylm503b0934ac2011-11-12 00:35:11 +00001878 if (TempListItem->Type == TypeStart) {
jcarsey252d9452011-03-25 20:49:53 +00001879 if (StrnCmp(Name, TempListItem->Name, StrLen(TempListItem->Name)) == 0) {
1880 *Type = TempListItem->Type;
1881 return (TRUE);
1882 }
1883 TempString = NULL;
1884 TempString = StrnCatGrow(&TempString, NULL, Name, StrLen(TempListItem->Name));
1885 if (TempString != NULL) {
1886 if (StringNoCaseCompare(&TempString, &TempListItem->Name) == 0) {
1887 *Type = TempListItem->Type;
1888 FreePool(TempString);
1889 return (TRUE);
1890 }
1891 FreePool(TempString);
1892 }
1893 } else if (StringNoCaseCompare(&Name, &TempListItem->Name) == 0) {
jcarsey94b17fa2009-05-07 18:46:18 +00001894 *Type = TempListItem->Type;
1895 return (TRUE);
1896 }
1897 }
jcarsey2247dde2009-11-09 18:08:58 +00001898
jcarsey94b17fa2009-05-07 18:46:18 +00001899 return (FALSE);
1900}
1901/**
jcarseyd2b45642009-05-11 18:02:16 +00001902 Checks the string for indicators of "flag" status. this is a leading '/', '-', or '+'
jcarsey94b17fa2009-05-07 18:46:18 +00001903
jcarseya405b862010-09-14 05:18:09 +00001904 @param[in] Name pointer to Name of parameter found
1905 @param[in] AlwaysAllowNumbers TRUE to allow numbers, FALSE to not.
jcarsey94b17fa2009-05-07 18:46:18 +00001906
1907 @retval TRUE the Parameter is a flag.
jcarseya405b862010-09-14 05:18:09 +00001908 @retval FALSE the Parameter not a flag.
jcarsey94b17fa2009-05-07 18:46:18 +00001909**/
1910BOOLEAN
1911EFIAPI
jcarseyd2b45642009-05-11 18:02:16 +00001912InternalIsFlag (
jcarsey2247dde2009-11-09 18:08:58 +00001913 IN CONST CHAR16 *Name,
1914 IN BOOLEAN AlwaysAllowNumbers
jcarsey94b17fa2009-05-07 18:46:18 +00001915 )
1916{
1917 //
1918 // ASSERT that Name isn't NULL
1919 //
1920 ASSERT(Name != NULL);
1921
1922 //
jcarsey2247dde2009-11-09 18:08:58 +00001923 // If we accept numbers then dont return TRUE. (they will be values)
1924 //
Jaben Carsey8af89da2014-08-19 20:58:03 +00001925 if (((Name[0] == L'-' || Name[0] == L'+') && InternalShellIsHexOrDecimalNumber(Name+1, FALSE, FALSE)) && AlwaysAllowNumbers) {
jcarsey2247dde2009-11-09 18:08:58 +00001926 return (FALSE);
1927 }
1928
1929 //
jcarseya405b862010-09-14 05:18:09 +00001930 // If the Name has a /, +, or - as the first character return TRUE
jcarsey94b17fa2009-05-07 18:46:18 +00001931 //
jcarsey1e6e84c2010-01-25 20:05:08 +00001932 if ((Name[0] == L'/') ||
jcarseyd2b45642009-05-11 18:02:16 +00001933 (Name[0] == L'-') ||
1934 (Name[0] == L'+')
jcarseya405b862010-09-14 05:18:09 +00001935 ) {
jcarsey94b17fa2009-05-07 18:46:18 +00001936 return (TRUE);
1937 }
1938 return (FALSE);
1939}
1940
1941/**
jcarsey1e6e84c2010-01-25 20:05:08 +00001942 Checks the command line arguments passed against the list of valid ones.
jcarsey94b17fa2009-05-07 18:46:18 +00001943
1944 If no initialization is required, then return RETURN_SUCCESS.
jcarsey1e6e84c2010-01-25 20:05:08 +00001945
jcarseya405b862010-09-14 05:18:09 +00001946 @param[in] CheckList pointer to list of parameters to check
1947 @param[out] CheckPackage pointer to pointer to list checked values
1948 @param[out] ProblemParam optional pointer to pointer to unicode string for
jcarseyd2b45642009-05-11 18:02:16 +00001949 the paramater that caused failure. If used then the
1950 caller is responsible for freeing the memory.
jcarseya405b862010-09-14 05:18:09 +00001951 @param[in] AutoPageBreak will automatically set PageBreakEnabled for "b" parameter
1952 @param[in] Argv pointer to array of parameters
1953 @param[in] Argc Count of parameters in Argv
1954 @param[in] AlwaysAllowNumbers TRUE to allow numbers always, FALSE otherwise.
jcarsey94b17fa2009-05-07 18:46:18 +00001955
1956 @retval EFI_SUCCESS The operation completed sucessfully.
1957 @retval EFI_OUT_OF_RESOURCES A memory allocation failed
1958 @retval EFI_INVALID_PARAMETER A parameter was invalid
jcarsey1e6e84c2010-01-25 20:05:08 +00001959 @retval EFI_VOLUME_CORRUPTED the command line was corrupt. an argument was
1960 duplicated. the duplicated command line argument
jcarsey94b17fa2009-05-07 18:46:18 +00001961 was returned in ProblemParam if provided.
jcarsey1e6e84c2010-01-25 20:05:08 +00001962 @retval EFI_NOT_FOUND a argument required a value that was missing.
jcarsey94b17fa2009-05-07 18:46:18 +00001963 the invalid command line argument was returned in
1964 ProblemParam if provided.
1965**/
1966EFI_STATUS
1967EFIAPI
1968InternalCommandLineParse (
1969 IN CONST SHELL_PARAM_ITEM *CheckList,
1970 OUT LIST_ENTRY **CheckPackage,
1971 OUT CHAR16 **ProblemParam OPTIONAL,
1972 IN BOOLEAN AutoPageBreak,
1973 IN CONST CHAR16 **Argv,
jcarsey2247dde2009-11-09 18:08:58 +00001974 IN UINTN Argc,
1975 IN BOOLEAN AlwaysAllowNumbers
jcarseya405b862010-09-14 05:18:09 +00001976 )
1977{
jcarsey94b17fa2009-05-07 18:46:18 +00001978 UINTN LoopCounter;
jcarsey252d9452011-03-25 20:49:53 +00001979 SHELL_PARAM_TYPE CurrentItemType;
jcarsey94b17fa2009-05-07 18:46:18 +00001980 SHELL_PARAM_PACKAGE *CurrentItemPackage;
jcarsey125c2cf2009-11-18 21:36:50 +00001981 UINTN GetItemValue;
1982 UINTN ValueSize;
jcarseya405b862010-09-14 05:18:09 +00001983 UINTN Count;
jcarsey252d9452011-03-25 20:49:53 +00001984 CONST CHAR16 *TempPointer;
Jaben Carsey98c16be2014-08-19 21:00:34 +00001985 UINTN CurrentValueSize;
jcarsey94b17fa2009-05-07 18:46:18 +00001986
1987 CurrentItemPackage = NULL;
jcarsey125c2cf2009-11-18 21:36:50 +00001988 GetItemValue = 0;
1989 ValueSize = 0;
jcarseya405b862010-09-14 05:18:09 +00001990 Count = 0;
jcarsey94b17fa2009-05-07 18:46:18 +00001991
1992 //
1993 // If there is only 1 item we dont need to do anything
1994 //
jcarseya405b862010-09-14 05:18:09 +00001995 if (Argc < 1) {
jcarsey94b17fa2009-05-07 18:46:18 +00001996 *CheckPackage = NULL;
1997 return (EFI_SUCCESS);
1998 }
1999
2000 //
jcarsey2247dde2009-11-09 18:08:58 +00002001 // ASSERTs
2002 //
2003 ASSERT(CheckList != NULL);
2004 ASSERT(Argv != NULL);
2005
2006 //
jcarsey94b17fa2009-05-07 18:46:18 +00002007 // initialize the linked list
2008 //
2009 *CheckPackage = (LIST_ENTRY*)AllocateZeroPool(sizeof(LIST_ENTRY));
sfu502a758c2011-10-08 02:55:30 +00002010 if (*CheckPackage == NULL) {
jcarseybeab0fc2011-10-10 17:26:25 +00002011 return (EFI_OUT_OF_RESOURCES);
sfu502a758c2011-10-08 02:55:30 +00002012 }
jcarseybeab0fc2011-10-10 17:26:25 +00002013
jcarsey94b17fa2009-05-07 18:46:18 +00002014 InitializeListHead(*CheckPackage);
2015
2016 //
2017 // loop through each of the arguments
2018 //
2019 for (LoopCounter = 0 ; LoopCounter < Argc ; ++LoopCounter) {
2020 if (Argv[LoopCounter] == NULL) {
2021 //
2022 // do nothing for NULL argv
2023 //
jcarseya405b862010-09-14 05:18:09 +00002024 } else if (InternalIsOnCheckList(Argv[LoopCounter], CheckList, &CurrentItemType)) {
jcarsey94b17fa2009-05-07 18:46:18 +00002025 //
jcarsey2247dde2009-11-09 18:08:58 +00002026 // We might have leftover if last parameter didnt have optional value
2027 //
jcarsey125c2cf2009-11-18 21:36:50 +00002028 if (GetItemValue != 0) {
2029 GetItemValue = 0;
jcarsey2247dde2009-11-09 18:08:58 +00002030 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
2031 }
2032 //
jcarsey94b17fa2009-05-07 18:46:18 +00002033 // this is a flag
2034 //
jcarsey252d9452011-03-25 20:49:53 +00002035 CurrentItemPackage = AllocateZeroPool(sizeof(SHELL_PARAM_PACKAGE));
jcarseybeab0fc2011-10-10 17:26:25 +00002036 if (CurrentItemPackage == NULL) {
2037 ShellCommandLineFreeVarList(*CheckPackage);
2038 *CheckPackage = NULL;
2039 return (EFI_OUT_OF_RESOURCES);
2040 }
Jaben Carsey98c16be2014-08-19 21:00:34 +00002041 CurrentItemPackage->Name = AllocateCopyPool(StrSize(Argv[LoopCounter]), Argv[LoopCounter]);
jcarseybeab0fc2011-10-10 17:26:25 +00002042 if (CurrentItemPackage->Name == NULL) {
2043 ShellCommandLineFreeVarList(*CheckPackage);
2044 *CheckPackage = NULL;
2045 return (EFI_OUT_OF_RESOURCES);
2046 }
jcarsey94b17fa2009-05-07 18:46:18 +00002047 CurrentItemPackage->Type = CurrentItemType;
2048 CurrentItemPackage->OriginalPosition = (UINTN)(-1);
jcarseyb1f95a02009-06-16 00:23:19 +00002049 CurrentItemPackage->Value = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +00002050
2051 //
2052 // Does this flag require a value
2053 //
jcarsey125c2cf2009-11-18 21:36:50 +00002054 switch (CurrentItemPackage->Type) {
jcarsey94b17fa2009-05-07 18:46:18 +00002055 //
jcarsey125c2cf2009-11-18 21:36:50 +00002056 // possibly trigger the next loop(s) to populate the value of this item
jcarsey1e6e84c2010-01-25 20:05:08 +00002057 //
jcarsey125c2cf2009-11-18 21:36:50 +00002058 case TypeValue:
jcarsey1e6e84c2010-01-25 20:05:08 +00002059 GetItemValue = 1;
jcarsey125c2cf2009-11-18 21:36:50 +00002060 ValueSize = 0;
2061 break;
2062 case TypeDoubleValue:
2063 GetItemValue = 2;
2064 ValueSize = 0;
2065 break;
2066 case TypeMaxValue:
2067 GetItemValue = (UINTN)(-1);
2068 ValueSize = 0;
2069 break;
2070 default:
2071 //
2072 // this item has no value expected; we are done
2073 //
2074 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
2075 ASSERT(GetItemValue == 0);
2076 break;
jcarsey94b17fa2009-05-07 18:46:18 +00002077 }
jcarseya405b862010-09-14 05:18:09 +00002078 } else if (GetItemValue != 0 && !InternalIsFlag(Argv[LoopCounter], AlwaysAllowNumbers)) {
jcarseyb1f95a02009-06-16 00:23:19 +00002079 ASSERT(CurrentItemPackage != NULL);
2080 //
jcarsey125c2cf2009-11-18 21:36:50 +00002081 // get the item VALUE for a previous flag
jcarseyb1f95a02009-06-16 00:23:19 +00002082 //
jcarsey49bd4982012-01-27 18:42:43 +00002083 if (StrStr(Argv[LoopCounter], L" ") == NULL) {
Jaben Carsey98c16be2014-08-19 21:00:34 +00002084 CurrentValueSize = ValueSize + StrSize(Argv[LoopCounter]) + sizeof(CHAR16);
2085 CurrentItemPackage->Value = ReallocatePool(ValueSize, CurrentValueSize, CurrentItemPackage->Value);
jcarsey49bd4982012-01-27 18:42:43 +00002086 ASSERT(CurrentItemPackage->Value != NULL);
2087 if (ValueSize == 0) {
Jaben Carsey98c16be2014-08-19 21:00:34 +00002088 StrnCpy(CurrentItemPackage->Value, Argv[LoopCounter], CurrentValueSize/sizeof(CHAR16) - 1);
jcarsey49bd4982012-01-27 18:42:43 +00002089 } else {
Jaben Carsey98c16be2014-08-19 21:00:34 +00002090 StrnCat(CurrentItemPackage->Value, L" ", CurrentValueSize/sizeof(CHAR16) - 1 - StrLen(CurrentItemPackage->Value));
2091 StrnCat(CurrentItemPackage->Value, Argv[LoopCounter], CurrentValueSize/sizeof(CHAR16) - 1 - StrLen(CurrentItemPackage->Value));
jcarsey49bd4982012-01-27 18:42:43 +00002092 }
2093 ValueSize += StrSize(Argv[LoopCounter]) + sizeof(CHAR16);
jcarsey125c2cf2009-11-18 21:36:50 +00002094 } else {
jcarsey49bd4982012-01-27 18:42:43 +00002095 //
2096 // the parameter has spaces. must be quoted.
2097 //
Jaben Carsey98c16be2014-08-19 21:00:34 +00002098 CurrentValueSize = ValueSize + StrSize(Argv[LoopCounter]) + sizeof(CHAR16) + sizeof(CHAR16) + sizeof(CHAR16);
2099 CurrentItemPackage->Value = ReallocatePool(ValueSize, CurrentValueSize, CurrentItemPackage->Value);
jcarsey49bd4982012-01-27 18:42:43 +00002100 ASSERT(CurrentItemPackage->Value != NULL);
2101 if (ValueSize == 0) {
Jaben Carsey98c16be2014-08-19 21:00:34 +00002102 StrnCpy(CurrentItemPackage->Value, L"\"", CurrentValueSize/sizeof(CHAR16) - 1);
2103 StrnCat(CurrentItemPackage->Value, Argv[LoopCounter], CurrentValueSize/sizeof(CHAR16) - 1 - StrLen(CurrentItemPackage->Value));
2104 StrnCat(CurrentItemPackage->Value, L"\"", CurrentValueSize/sizeof(CHAR16) - 1 - StrLen(CurrentItemPackage->Value));
jcarsey49bd4982012-01-27 18:42:43 +00002105 } else {
Jaben Carsey98c16be2014-08-19 21:00:34 +00002106 StrnCat(CurrentItemPackage->Value, L" ", CurrentValueSize/sizeof(CHAR16) - 1 - StrLen(CurrentItemPackage->Value));
2107 StrnCat(CurrentItemPackage->Value, L"\"", CurrentValueSize/sizeof(CHAR16) - 1 - StrLen(CurrentItemPackage->Value));
2108 StrnCat(CurrentItemPackage->Value, Argv[LoopCounter], CurrentValueSize/sizeof(CHAR16) - 1 - StrLen(CurrentItemPackage->Value));
2109 StrnCat(CurrentItemPackage->Value, L"\"", CurrentValueSize/sizeof(CHAR16) - 1 - StrLen(CurrentItemPackage->Value));
jcarsey49bd4982012-01-27 18:42:43 +00002110 }
2111 ValueSize += StrSize(Argv[LoopCounter]) + sizeof(CHAR16);
jcarsey125c2cf2009-11-18 21:36:50 +00002112 }
jcarsey125c2cf2009-11-18 21:36:50 +00002113 GetItemValue--;
2114 if (GetItemValue == 0) {
2115 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
2116 }
jcarseya405b862010-09-14 05:18:09 +00002117 } else if (!InternalIsFlag(Argv[LoopCounter], AlwaysAllowNumbers) ){ //|| ProblemParam == NULL) {
jcarseyb1f95a02009-06-16 00:23:19 +00002118 //
2119 // add this one as a non-flag
2120 //
jcarsey252d9452011-03-25 20:49:53 +00002121
2122 TempPointer = Argv[LoopCounter];
darylm503b0934ac2011-11-12 00:35:11 +00002123 if ((*TempPointer == L'^' && *(TempPointer+1) == L'-')
jcarsey252d9452011-03-25 20:49:53 +00002124 || (*TempPointer == L'^' && *(TempPointer+1) == L'/')
2125 || (*TempPointer == L'^' && *(TempPointer+1) == L'+')
2126 ){
2127 TempPointer++;
2128 }
2129 CurrentItemPackage = AllocateZeroPool(sizeof(SHELL_PARAM_PACKAGE));
jcarseybeab0fc2011-10-10 17:26:25 +00002130 if (CurrentItemPackage == NULL) {
2131 ShellCommandLineFreeVarList(*CheckPackage);
2132 *CheckPackage = NULL;
2133 return (EFI_OUT_OF_RESOURCES);
2134 }
jcarseyb1f95a02009-06-16 00:23:19 +00002135 CurrentItemPackage->Name = NULL;
2136 CurrentItemPackage->Type = TypePosition;
Jaben Carsey98c16be2014-08-19 21:00:34 +00002137 CurrentItemPackage->Value = AllocateCopyPool(StrSize(TempPointer), TempPointer);
jcarseybeab0fc2011-10-10 17:26:25 +00002138 if (CurrentItemPackage->Value == NULL) {
2139 ShellCommandLineFreeVarList(*CheckPackage);
2140 *CheckPackage = NULL;
2141 return (EFI_OUT_OF_RESOURCES);
2142 }
jcarseya405b862010-09-14 05:18:09 +00002143 CurrentItemPackage->OriginalPosition = Count++;
jcarsey9b3bf082009-06-23 21:15:07 +00002144 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
jcarsey252d9452011-03-25 20:49:53 +00002145 } else {
jcarsey94b17fa2009-05-07 18:46:18 +00002146 //
2147 // this was a non-recognised flag... error!
2148 //
jcarsey252d9452011-03-25 20:49:53 +00002149 if (ProblemParam != NULL) {
Jaben Carsey98c16be2014-08-19 21:00:34 +00002150 *ProblemParam = AllocateCopyPool(StrSize(Argv[LoopCounter]), Argv[LoopCounter]);
jcarsey252d9452011-03-25 20:49:53 +00002151 }
jcarsey94b17fa2009-05-07 18:46:18 +00002152 ShellCommandLineFreeVarList(*CheckPackage);
2153 *CheckPackage = NULL;
2154 return (EFI_VOLUME_CORRUPTED);
jcarsey94b17fa2009-05-07 18:46:18 +00002155 }
2156 }
jcarsey125c2cf2009-11-18 21:36:50 +00002157 if (GetItemValue != 0) {
2158 GetItemValue = 0;
2159 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
2160 }
jcarsey94b17fa2009-05-07 18:46:18 +00002161 //
2162 // support for AutoPageBreak
2163 //
2164 if (AutoPageBreak && ShellCommandLineGetFlag(*CheckPackage, L"-b")) {
2165 ShellSetPageBreakMode(TRUE);
2166 }
2167 return (EFI_SUCCESS);
2168}
2169
2170/**
jcarsey1e6e84c2010-01-25 20:05:08 +00002171 Checks the command line arguments passed against the list of valid ones.
jcarsey94b17fa2009-05-07 18:46:18 +00002172 Optionally removes NULL values first.
jcarsey1e6e84c2010-01-25 20:05:08 +00002173
jcarsey94b17fa2009-05-07 18:46:18 +00002174 If no initialization is required, then return RETURN_SUCCESS.
jcarsey1e6e84c2010-01-25 20:05:08 +00002175
jcarseya405b862010-09-14 05:18:09 +00002176 @param[in] CheckList The pointer to list of parameters to check.
2177 @param[out] CheckPackage The package of checked values.
2178 @param[out] ProblemParam Optional pointer to pointer to unicode string for
jcarsey94b17fa2009-05-07 18:46:18 +00002179 the paramater that caused failure.
jcarseya405b862010-09-14 05:18:09 +00002180 @param[in] AutoPageBreak Will automatically set PageBreakEnabled.
2181 @param[in] AlwaysAllowNumbers Will never fail for number based flags.
jcarsey94b17fa2009-05-07 18:46:18 +00002182
2183 @retval EFI_SUCCESS The operation completed sucessfully.
jcarseya405b862010-09-14 05:18:09 +00002184 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
2185 @retval EFI_INVALID_PARAMETER A parameter was invalid.
2186 @retval EFI_VOLUME_CORRUPTED The command line was corrupt.
2187 @retval EFI_DEVICE_ERROR The commands contained 2 opposing arguments. One
jcarsey1e6e84c2010-01-25 20:05:08 +00002188 of the command line arguments was returned in
jcarsey94b17fa2009-05-07 18:46:18 +00002189 ProblemParam if provided.
jcarseya405b862010-09-14 05:18:09 +00002190 @retval EFI_NOT_FOUND A argument required a value that was missing.
2191 The invalid command line argument was returned in
jcarsey94b17fa2009-05-07 18:46:18 +00002192 ProblemParam if provided.
2193**/
2194EFI_STATUS
2195EFIAPI
jcarsey2247dde2009-11-09 18:08:58 +00002196ShellCommandLineParseEx (
jcarsey94b17fa2009-05-07 18:46:18 +00002197 IN CONST SHELL_PARAM_ITEM *CheckList,
2198 OUT LIST_ENTRY **CheckPackage,
2199 OUT CHAR16 **ProblemParam OPTIONAL,
jcarsey2247dde2009-11-09 18:08:58 +00002200 IN BOOLEAN AutoPageBreak,
2201 IN BOOLEAN AlwaysAllowNumbers
jcarseya405b862010-09-14 05:18:09 +00002202 )
2203{
jcarsey1e6e84c2010-01-25 20:05:08 +00002204 //
jcarsey94b17fa2009-05-07 18:46:18 +00002205 // ASSERT that CheckList and CheckPackage aren't NULL
2206 //
2207 ASSERT(CheckList != NULL);
2208 ASSERT(CheckPackage != NULL);
2209
jcarsey1e6e84c2010-01-25 20:05:08 +00002210 //
jcarsey94b17fa2009-05-07 18:46:18 +00002211 // Check for UEFI Shell 2.0 protocols
2212 //
jcarsey366f81a2011-06-27 21:04:22 +00002213 if (gEfiShellParametersProtocol != NULL) {
jcarsey1e6e84c2010-01-25 20:05:08 +00002214 return (InternalCommandLineParse(CheckList,
2215 CheckPackage,
2216 ProblemParam,
2217 AutoPageBreak,
jcarsey366f81a2011-06-27 21:04:22 +00002218 (CONST CHAR16**) gEfiShellParametersProtocol->Argv,
2219 gEfiShellParametersProtocol->Argc,
jcarsey2247dde2009-11-09 18:08:58 +00002220 AlwaysAllowNumbers));
jcarsey94b17fa2009-05-07 18:46:18 +00002221 }
2222
jcarsey1e6e84c2010-01-25 20:05:08 +00002223 //
jcarsey94b17fa2009-05-07 18:46:18 +00002224 // ASSERT That EFI Shell is not required
2225 //
2226 ASSERT (mEfiShellInterface != NULL);
jcarsey1e6e84c2010-01-25 20:05:08 +00002227 return (InternalCommandLineParse(CheckList,
2228 CheckPackage,
2229 ProblemParam,
2230 AutoPageBreak,
jljusten08d7f8e2009-06-15 18:42:13 +00002231 (CONST CHAR16**) mEfiShellInterface->Argv,
jcarsey2247dde2009-11-09 18:08:58 +00002232 mEfiShellInterface->Argc,
2233 AlwaysAllowNumbers));
jcarsey94b17fa2009-05-07 18:46:18 +00002234}
2235
2236/**
2237 Frees shell variable list that was returned from ShellCommandLineParse.
2238
2239 This function will free all the memory that was used for the CheckPackage
2240 list of postprocessed shell arguments.
2241
2242 this function has no return value.
2243
2244 if CheckPackage is NULL, then return
2245
2246 @param CheckPackage the list to de-allocate
2247 **/
2248VOID
2249EFIAPI
2250ShellCommandLineFreeVarList (
2251 IN LIST_ENTRY *CheckPackage
jcarseya405b862010-09-14 05:18:09 +00002252 )
2253{
jcarsey94b17fa2009-05-07 18:46:18 +00002254 LIST_ENTRY *Node;
2255
2256 //
2257 // check for CheckPackage == NULL
2258 //
2259 if (CheckPackage == NULL) {
2260 return;
2261 }
2262
2263 //
2264 // for each node in the list
2265 //
jcarsey9eb53ac2009-07-08 17:26:58 +00002266 for ( Node = GetFirstNode(CheckPackage)
jcarseya405b862010-09-14 05:18:09 +00002267 ; !IsListEmpty(CheckPackage)
jcarsey9eb53ac2009-07-08 17:26:58 +00002268 ; Node = GetFirstNode(CheckPackage)
jcarseya405b862010-09-14 05:18:09 +00002269 ){
jcarsey94b17fa2009-05-07 18:46:18 +00002270 //
2271 // Remove it from the list
2272 //
2273 RemoveEntryList(Node);
2274
2275 //
2276 // if it has a name free the name
2277 //
2278 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {
2279 FreePool(((SHELL_PARAM_PACKAGE*)Node)->Name);
2280 }
2281
2282 //
2283 // if it has a value free the value
2284 //
2285 if (((SHELL_PARAM_PACKAGE*)Node)->Value != NULL) {
2286 FreePool(((SHELL_PARAM_PACKAGE*)Node)->Value);
2287 }
jcarsey1e6e84c2010-01-25 20:05:08 +00002288
jcarsey94b17fa2009-05-07 18:46:18 +00002289 //
2290 // free the node structure
2291 //
2292 FreePool((SHELL_PARAM_PACKAGE*)Node);
2293 }
2294 //
2295 // free the list head node
2296 //
2297 FreePool(CheckPackage);
2298}
2299/**
2300 Checks for presence of a flag parameter
2301
2302 flag arguments are in the form of "-<Key>" or "/<Key>", but do not have a value following the key
2303
2304 if CheckPackage is NULL then return FALSE.
2305 if KeyString is NULL then ASSERT()
jcarsey1e6e84c2010-01-25 20:05:08 +00002306
jcarsey94b17fa2009-05-07 18:46:18 +00002307 @param CheckPackage The package of parsed command line arguments
2308 @param KeyString the Key of the command line argument to check for
2309
2310 @retval TRUE the flag is on the command line
2311 @retval FALSE the flag is not on the command line
2312 **/
2313BOOLEAN
2314EFIAPI
2315ShellCommandLineGetFlag (
jcarseya405b862010-09-14 05:18:09 +00002316 IN CONST LIST_ENTRY * CONST CheckPackage,
2317 IN CONST CHAR16 * CONST KeyString
2318 )
2319{
jcarsey94b17fa2009-05-07 18:46:18 +00002320 LIST_ENTRY *Node;
jcarsey252d9452011-03-25 20:49:53 +00002321 CHAR16 *TempString;
jcarsey94b17fa2009-05-07 18:46:18 +00002322
2323 //
ydong100c1950b2011-10-13 02:37:35 +00002324 // return FALSE for no package or KeyString is NULL
jcarsey94b17fa2009-05-07 18:46:18 +00002325 //
ydong100c1950b2011-10-13 02:37:35 +00002326 if (CheckPackage == NULL || KeyString == NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00002327 return (FALSE);
2328 }
2329
2330 //
2331 // enumerate through the list of parametrs
2332 //
jcarsey1e6e84c2010-01-25 20:05:08 +00002333 for ( Node = GetFirstNode(CheckPackage)
2334 ; !IsNull (CheckPackage, Node)
2335 ; Node = GetNextNode(CheckPackage, Node)
jcarsey252d9452011-03-25 20:49:53 +00002336 ){
jcarsey94b17fa2009-05-07 18:46:18 +00002337 //
2338 // If the Name matches, return TRUE (and there may be NULL name)
2339 //
2340 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {
jcarsey9eb53ac2009-07-08 17:26:58 +00002341 //
2342 // If Type is TypeStart then only compare the begining of the strings
2343 //
jcarsey252d9452011-03-25 20:49:53 +00002344 if (((SHELL_PARAM_PACKAGE*)Node)->Type == TypeStart) {
2345 if (StrnCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name, StrLen(KeyString)) == 0) {
2346 return (TRUE);
2347 }
2348 TempString = NULL;
2349 TempString = StrnCatGrow(&TempString, NULL, KeyString, StrLen(((SHELL_PARAM_PACKAGE*)Node)->Name));
2350 if (TempString != NULL) {
2351 if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {
2352 FreePool(TempString);
2353 return (TRUE);
2354 }
2355 FreePool(TempString);
2356 }
2357 } else if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {
jcarsey94b17fa2009-05-07 18:46:18 +00002358 return (TRUE);
2359 }
2360 }
2361 }
2362 return (FALSE);
2363}
2364/**
jcarseya405b862010-09-14 05:18:09 +00002365 Returns value from command line argument.
jcarsey94b17fa2009-05-07 18:46:18 +00002366
jcarseya405b862010-09-14 05:18:09 +00002367 Value parameters are in the form of "-<Key> value" or "/<Key> value".
jcarsey1e6e84c2010-01-25 20:05:08 +00002368
jcarseya405b862010-09-14 05:18:09 +00002369 If CheckPackage is NULL, then return NULL.
jcarsey94b17fa2009-05-07 18:46:18 +00002370
jcarseya405b862010-09-14 05:18:09 +00002371 @param[in] CheckPackage The package of parsed command line arguments.
2372 @param[in] KeyString The Key of the command line argument to check for.
jcarsey94b17fa2009-05-07 18:46:18 +00002373
jcarseya405b862010-09-14 05:18:09 +00002374 @retval NULL The flag is not on the command line.
2375 @retval !=NULL The pointer to unicode string of the value.
2376**/
jcarsey94b17fa2009-05-07 18:46:18 +00002377CONST CHAR16*
2378EFIAPI
2379ShellCommandLineGetValue (
2380 IN CONST LIST_ENTRY *CheckPackage,
2381 IN CHAR16 *KeyString
jcarseya405b862010-09-14 05:18:09 +00002382 )
2383{
jcarsey94b17fa2009-05-07 18:46:18 +00002384 LIST_ENTRY *Node;
jcarsey252d9452011-03-25 20:49:53 +00002385 CHAR16 *TempString;
jcarsey94b17fa2009-05-07 18:46:18 +00002386
2387 //
ydong100c1950b2011-10-13 02:37:35 +00002388 // return NULL for no package or KeyString is NULL
jcarsey94b17fa2009-05-07 18:46:18 +00002389 //
ydong100c1950b2011-10-13 02:37:35 +00002390 if (CheckPackage == NULL || KeyString == NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00002391 return (NULL);
2392 }
2393
2394 //
2395 // enumerate through the list of parametrs
2396 //
jcarsey1e6e84c2010-01-25 20:05:08 +00002397 for ( Node = GetFirstNode(CheckPackage)
2398 ; !IsNull (CheckPackage, Node)
2399 ; Node = GetNextNode(CheckPackage, Node)
jcarsey252d9452011-03-25 20:49:53 +00002400 ){
jcarsey94b17fa2009-05-07 18:46:18 +00002401 //
jcarsey252d9452011-03-25 20:49:53 +00002402 // If the Name matches, return TRUE (and there may be NULL name)
jcarsey94b17fa2009-05-07 18:46:18 +00002403 //
2404 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {
jcarsey9eb53ac2009-07-08 17:26:58 +00002405 //
2406 // If Type is TypeStart then only compare the begining of the strings
2407 //
jcarsey252d9452011-03-25 20:49:53 +00002408 if (((SHELL_PARAM_PACKAGE*)Node)->Type == TypeStart) {
2409 if (StrnCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name, StrLen(KeyString)) == 0) {
2410 return (((SHELL_PARAM_PACKAGE*)Node)->Name + StrLen(KeyString));
2411 }
2412 TempString = NULL;
2413 TempString = StrnCatGrow(&TempString, NULL, KeyString, StrLen(((SHELL_PARAM_PACKAGE*)Node)->Name));
2414 if (TempString != NULL) {
2415 if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {
2416 FreePool(TempString);
2417 return (((SHELL_PARAM_PACKAGE*)Node)->Name + StrLen(KeyString));
2418 }
2419 FreePool(TempString);
2420 }
2421 } else if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {
jcarsey94b17fa2009-05-07 18:46:18 +00002422 return (((SHELL_PARAM_PACKAGE*)Node)->Value);
2423 }
2424 }
2425 }
2426 return (NULL);
2427}
jcarseya405b862010-09-14 05:18:09 +00002428
jcarsey94b17fa2009-05-07 18:46:18 +00002429/**
jcarseya405b862010-09-14 05:18:09 +00002430 Returns raw value from command line argument.
jcarsey94b17fa2009-05-07 18:46:18 +00002431
jcarseya405b862010-09-14 05:18:09 +00002432 Raw value parameters are in the form of "value" in a specific position in the list.
jcarsey1e6e84c2010-01-25 20:05:08 +00002433
jcarseya405b862010-09-14 05:18:09 +00002434 If CheckPackage is NULL, then return NULL.
jcarsey94b17fa2009-05-07 18:46:18 +00002435
jcarseya405b862010-09-14 05:18:09 +00002436 @param[in] CheckPackage The package of parsed command line arguments.
2437 @param[in] Position The position of the value.
jcarsey94b17fa2009-05-07 18:46:18 +00002438
jcarseya405b862010-09-14 05:18:09 +00002439 @retval NULL The flag is not on the command line.
2440 @retval !=NULL The pointer to unicode string of the value.
jcarsey94b17fa2009-05-07 18:46:18 +00002441 **/
2442CONST CHAR16*
2443EFIAPI
2444ShellCommandLineGetRawValue (
jcarseya405b862010-09-14 05:18:09 +00002445 IN CONST LIST_ENTRY * CONST CheckPackage,
2446 IN UINTN Position
2447 )
2448{
jcarsey94b17fa2009-05-07 18:46:18 +00002449 LIST_ENTRY *Node;
2450
2451 //
2452 // check for CheckPackage == NULL
2453 //
2454 if (CheckPackage == NULL) {
2455 return (NULL);
2456 }
2457
2458 //
2459 // enumerate through the list of parametrs
2460 //
jcarsey1e6e84c2010-01-25 20:05:08 +00002461 for ( Node = GetFirstNode(CheckPackage)
2462 ; !IsNull (CheckPackage, Node)
2463 ; Node = GetNextNode(CheckPackage, Node)
jcarseya405b862010-09-14 05:18:09 +00002464 ){
jcarsey94b17fa2009-05-07 18:46:18 +00002465 //
2466 // If the position matches, return the value
2467 //
2468 if (((SHELL_PARAM_PACKAGE*)Node)->OriginalPosition == Position) {
2469 return (((SHELL_PARAM_PACKAGE*)Node)->Value);
2470 }
2471 }
2472 return (NULL);
jcarseyb1f95a02009-06-16 00:23:19 +00002473}
jcarsey2247dde2009-11-09 18:08:58 +00002474
2475/**
jcarsey1e6e84c2010-01-25 20:05:08 +00002476 returns the number of command line value parameters that were parsed.
2477
jcarsey2247dde2009-11-09 18:08:58 +00002478 this will not include flags.
2479
jcarseya405b862010-09-14 05:18:09 +00002480 @param[in] CheckPackage The package of parsed command line arguments.
2481
jcarsey2247dde2009-11-09 18:08:58 +00002482 @retval (UINTN)-1 No parsing has ocurred
2483 @return other The number of value parameters found
2484**/
2485UINTN
2486EFIAPI
2487ShellCommandLineGetCount(
jcarseya405b862010-09-14 05:18:09 +00002488 IN CONST LIST_ENTRY *CheckPackage
jcarsey125c2cf2009-11-18 21:36:50 +00002489 )
2490{
jcarseya405b862010-09-14 05:18:09 +00002491 LIST_ENTRY *Node1;
2492 UINTN Count;
2493
2494 if (CheckPackage == NULL) {
2495 return (0);
2496 }
2497 for ( Node1 = GetFirstNode(CheckPackage), Count = 0
2498 ; !IsNull (CheckPackage, Node1)
2499 ; Node1 = GetNextNode(CheckPackage, Node1)
2500 ){
2501 if (((SHELL_PARAM_PACKAGE*)Node1)->Name == NULL) {
2502 Count++;
2503 }
2504 }
2505 return (Count);
jcarsey2247dde2009-11-09 18:08:58 +00002506}
2507
jcarsey975136a2009-06-16 19:03:54 +00002508/**
jcarsey36a9d672009-11-20 21:13:41 +00002509 Determins if a parameter is duplicated.
2510
jcarsey1e6e84c2010-01-25 20:05:08 +00002511 If Param is not NULL then it will point to a callee allocated string buffer
jcarsey36a9d672009-11-20 21:13:41 +00002512 with the parameter value if a duplicate is found.
2513
2514 If CheckPackage is NULL, then ASSERT.
2515
2516 @param[in] CheckPackage The package of parsed command line arguments.
2517 @param[out] Param Upon finding one, a pointer to the duplicated parameter.
2518
2519 @retval EFI_SUCCESS No parameters were duplicated.
2520 @retval EFI_DEVICE_ERROR A duplicate was found.
2521 **/
2522EFI_STATUS
2523EFIAPI
2524ShellCommandLineCheckDuplicate (
2525 IN CONST LIST_ENTRY *CheckPackage,
2526 OUT CHAR16 **Param
2527 )
2528{
2529 LIST_ENTRY *Node1;
2530 LIST_ENTRY *Node2;
jcarsey1e6e84c2010-01-25 20:05:08 +00002531
jcarsey36a9d672009-11-20 21:13:41 +00002532 ASSERT(CheckPackage != NULL);
2533
jcarsey1e6e84c2010-01-25 20:05:08 +00002534 for ( Node1 = GetFirstNode(CheckPackage)
2535 ; !IsNull (CheckPackage, Node1)
2536 ; Node1 = GetNextNode(CheckPackage, Node1)
jcarseya405b862010-09-14 05:18:09 +00002537 ){
jcarsey1e6e84c2010-01-25 20:05:08 +00002538 for ( Node2 = GetNextNode(CheckPackage, Node1)
2539 ; !IsNull (CheckPackage, Node2)
2540 ; Node2 = GetNextNode(CheckPackage, Node2)
jcarseya405b862010-09-14 05:18:09 +00002541 ){
2542 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 +00002543 if (Param != NULL) {
2544 *Param = NULL;
2545 *Param = StrnCatGrow(Param, NULL, ((SHELL_PARAM_PACKAGE*)Node1)->Name, 0);
2546 }
2547 return (EFI_DEVICE_ERROR);
2548 }
2549 }
2550 }
2551 return (EFI_SUCCESS);
2552}
2553
2554/**
jcarsey1e6e84c2010-01-25 20:05:08 +00002555 This is a find and replace function. Upon successful return the NewString is a copy of
jcarsey975136a2009-06-16 19:03:54 +00002556 SourceString with each instance of FindTarget replaced with ReplaceWith.
2557
jcarseyb3011f42010-01-11 21:49:04 +00002558 If SourceString and NewString overlap the behavior is undefined.
2559
jcarsey975136a2009-06-16 19:03:54 +00002560 If the string would grow bigger than NewSize it will halt and return error.
2561
ydong104ff7e372011-09-02 08:05:34 +00002562 @param[in] SourceString The string with source buffer.
2563 @param[in, out] NewString The string with resultant buffer.
2564 @param[in] NewSize The size in bytes of NewString.
2565 @param[in] FindTarget The string to look for.
2566 @param[in] ReplaceWith The string to replace FindTarget with.
2567 @param[in] SkipPreCarrot If TRUE will skip a FindTarget that has a '^'
2568 immediately before it.
2569 @param[in] ParameterReplacing If TRUE will add "" around items with spaces.
jcarsey975136a2009-06-16 19:03:54 +00002570
jcarsey969c7832010-01-13 16:46:33 +00002571 @retval EFI_INVALID_PARAMETER SourceString was NULL.
2572 @retval EFI_INVALID_PARAMETER NewString was NULL.
2573 @retval EFI_INVALID_PARAMETER FindTarget was NULL.
2574 @retval EFI_INVALID_PARAMETER ReplaceWith was NULL.
2575 @retval EFI_INVALID_PARAMETER FindTarget had length < 1.
2576 @retval EFI_INVALID_PARAMETER SourceString had length < 1.
jcarsey1e6e84c2010-01-25 20:05:08 +00002577 @retval EFI_BUFFER_TOO_SMALL NewSize was less than the minimum size to hold
jcarsey969c7832010-01-13 16:46:33 +00002578 the new string (truncation occurred).
jcarseya405b862010-09-14 05:18:09 +00002579 @retval EFI_SUCCESS The string was successfully copied with replacement.
jcarsey975136a2009-06-16 19:03:54 +00002580**/
jcarsey975136a2009-06-16 19:03:54 +00002581EFI_STATUS
2582EFIAPI
jcarseya405b862010-09-14 05:18:09 +00002583ShellCopySearchAndReplace(
jcarsey975136a2009-06-16 19:03:54 +00002584 IN CHAR16 CONST *SourceString,
jcarseya405b862010-09-14 05:18:09 +00002585 IN OUT CHAR16 *NewString,
jcarsey975136a2009-06-16 19:03:54 +00002586 IN UINTN NewSize,
2587 IN CONST CHAR16 *FindTarget,
jcarsey969c7832010-01-13 16:46:33 +00002588 IN CONST CHAR16 *ReplaceWith,
jcarseya405b862010-09-14 05:18:09 +00002589 IN CONST BOOLEAN SkipPreCarrot,
2590 IN CONST BOOLEAN ParameterReplacing
jcarsey1e6e84c2010-01-25 20:05:08 +00002591 )
jcarsey2247dde2009-11-09 18:08:58 +00002592{
jcarsey01582942009-07-10 19:46:17 +00002593 UINTN Size;
jcarseya405b862010-09-14 05:18:09 +00002594 CHAR16 *Replace;
2595
jcarsey975136a2009-06-16 19:03:54 +00002596 if ( (SourceString == NULL)
2597 || (NewString == NULL)
2598 || (FindTarget == NULL)
2599 || (ReplaceWith == NULL)
2600 || (StrLen(FindTarget) < 1)
2601 || (StrLen(SourceString) < 1)
jcarseya405b862010-09-14 05:18:09 +00002602 ){
jcarsey975136a2009-06-16 19:03:54 +00002603 return (EFI_INVALID_PARAMETER);
2604 }
jcarseya405b862010-09-14 05:18:09 +00002605 Replace = NULL;
2606 if (StrStr(ReplaceWith, L" ") == NULL || !ParameterReplacing) {
2607 Replace = StrnCatGrow(&Replace, NULL, ReplaceWith, 0);
2608 } else {
2609 Replace = AllocateZeroPool(StrSize(ReplaceWith) + 2*sizeof(CHAR16));
jcarseybeab0fc2011-10-10 17:26:25 +00002610 if (Replace != NULL) {
2611 UnicodeSPrint(Replace, StrSize(ReplaceWith) + 2*sizeof(CHAR16), L"\"%s\"", ReplaceWith);
2612 }
jcarseya405b862010-09-14 05:18:09 +00002613 }
jcarsey3e082d52010-10-04 16:44:57 +00002614 if (Replace == NULL) {
2615 return (EFI_OUT_OF_RESOURCES);
2616 }
Jaben Carsey98c16be2014-08-19 21:00:34 +00002617 NewString = ZeroMem(NewString, NewSize);
jcarsey2247dde2009-11-09 18:08:58 +00002618 while (*SourceString != CHAR_NULL) {
jcarsey969c7832010-01-13 16:46:33 +00002619 //
jcarseya405b862010-09-14 05:18:09 +00002620 // if we find the FindTarget and either Skip == FALSE or Skip and we
jcarsey969c7832010-01-13 16:46:33 +00002621 // dont have a carrot do a replace...
2622 //
jcarsey1e6e84c2010-01-25 20:05:08 +00002623 if (StrnCmp(SourceString, FindTarget, StrLen(FindTarget)) == 0
jcarseya405b862010-09-14 05:18:09 +00002624 && ((SkipPreCarrot && *(SourceString-1) != L'^') || !SkipPreCarrot)
2625 ){
jcarsey975136a2009-06-16 19:03:54 +00002626 SourceString += StrLen(FindTarget);
jcarsey01582942009-07-10 19:46:17 +00002627 Size = StrSize(NewString);
jcarseya405b862010-09-14 05:18:09 +00002628 if ((Size + (StrLen(Replace)*sizeof(CHAR16))) > NewSize) {
2629 FreePool(Replace);
jcarsey975136a2009-06-16 19:03:54 +00002630 return (EFI_BUFFER_TOO_SMALL);
2631 }
Jaben Carsey98c16be2014-08-19 21:00:34 +00002632 StrnCat(NewString, Replace, NewSize/sizeof(CHAR16) - 1 - StrLen(NewString));
jcarsey975136a2009-06-16 19:03:54 +00002633 } else {
jcarsey01582942009-07-10 19:46:17 +00002634 Size = StrSize(NewString);
2635 if (Size + sizeof(CHAR16) > NewSize) {
jcarseya405b862010-09-14 05:18:09 +00002636 FreePool(Replace);
jcarsey975136a2009-06-16 19:03:54 +00002637 return (EFI_BUFFER_TOO_SMALL);
2638 }
2639 StrnCat(NewString, SourceString, 1);
2640 SourceString++;
2641 }
2642 }
jcarseya405b862010-09-14 05:18:09 +00002643 FreePool(Replace);
jcarsey975136a2009-06-16 19:03:54 +00002644 return (EFI_SUCCESS);
2645}
jcarseyb1f95a02009-06-16 00:23:19 +00002646
2647/**
jcarseye2f82972009-12-01 05:40:24 +00002648 Internal worker function to output a string.
2649
2650 This function will output a string to the correct StdOut.
2651
2652 @param[in] String The string to print out.
2653
2654 @retval EFI_SUCCESS The operation was sucessful.
2655 @retval !EFI_SUCCESS The operation failed.
2656**/
2657EFI_STATUS
2658EFIAPI
2659InternalPrintTo (
2660 IN CONST CHAR16 *String
2661 )
2662{
2663 UINTN Size;
2664 Size = StrSize(String) - sizeof(CHAR16);
jcarseya405b862010-09-14 05:18:09 +00002665 if (Size == 0) {
2666 return (EFI_SUCCESS);
2667 }
jcarsey366f81a2011-06-27 21:04:22 +00002668 if (gEfiShellParametersProtocol != NULL) {
2669 return (gEfiShellProtocol->WriteFile(gEfiShellParametersProtocol->StdOut, &Size, (VOID*)String));
jcarseye2f82972009-12-01 05:40:24 +00002670 }
2671 if (mEfiShellInterface != NULL) {
jcarsey06c355b2012-03-26 21:00:39 +00002672 if (mEfiShellInterface->RedirArgc == 0) {
jcarsey49bd4982012-01-27 18:42:43 +00002673 //
2674 // Divide in half for old shell. Must be string length not size.
jcarsey06c355b2012-03-26 21:00:39 +00002675 //
2676 Size /=2; // Divide in half only when no redirection.
2677 }
jcarseya405b862010-09-14 05:18:09 +00002678 return (mEfiShellInterface->StdOut->Write(mEfiShellInterface->StdOut, &Size, (VOID*)String));
jcarseye2f82972009-12-01 05:40:24 +00002679 }
2680 ASSERT(FALSE);
2681 return (EFI_UNSUPPORTED);
2682}
2683
2684/**
jcarseyb1f95a02009-06-16 00:23:19 +00002685 Print at a specific location on the screen.
2686
jcarseyf1b87e72009-06-17 00:52:11 +00002687 This function will move the cursor to a given screen location and print the specified string
jcarsey1e6e84c2010-01-25 20:05:08 +00002688
2689 If -1 is specified for either the Row or Col the current screen location for BOTH
jcarseyf1b87e72009-06-17 00:52:11 +00002690 will be used.
jcarseyb1f95a02009-06-16 00:23:19 +00002691
2692 if either Row or Col is out of range for the current console, then ASSERT
2693 if Format is NULL, then ASSERT
2694
jcarsey1e6e84c2010-01-25 20:05:08 +00002695 In addition to the standard %-based flags as supported by UefiLib Print() this supports
jcarseyb1f95a02009-06-16 00:23:19 +00002696 the following additional flags:
2697 %N - Set output attribute to normal
2698 %H - Set output attribute to highlight
2699 %E - Set output attribute to error
2700 %B - Set output attribute to blue color
2701 %V - Set output attribute to green color
2702
2703 Note: The background color is controlled by the shell command cls.
2704
jcarseyb1f95a02009-06-16 00:23:19 +00002705 @param[in] Col the column to print at
jcarsey252d9452011-03-25 20:49:53 +00002706 @param[in] Row the row to print at
jcarseyb1f95a02009-06-16 00:23:19 +00002707 @param[in] Format the format string
jcarsey2247dde2009-11-09 18:08:58 +00002708 @param[in] Marker the marker for the variable argument list
jcarseyb1f95a02009-06-16 00:23:19 +00002709
jcarseya405b862010-09-14 05:18:09 +00002710 @return EFI_SUCCESS The operation was successful.
2711 @return EFI_DEVICE_ERROR The console device reported an error.
jcarseyb1f95a02009-06-16 00:23:19 +00002712**/
jcarseya405b862010-09-14 05:18:09 +00002713EFI_STATUS
jcarseyb1f95a02009-06-16 00:23:19 +00002714EFIAPI
jcarsey2247dde2009-11-09 18:08:58 +00002715InternalShellPrintWorker(
jcarseyb1f95a02009-06-16 00:23:19 +00002716 IN INT32 Col OPTIONAL,
2717 IN INT32 Row OPTIONAL,
2718 IN CONST CHAR16 *Format,
jcarsey252d9452011-03-25 20:49:53 +00002719 IN VA_LIST Marker
jcarsey1e6e84c2010-01-25 20:05:08 +00002720 )
jcarsey2247dde2009-11-09 18:08:58 +00002721{
jcarseyb1f95a02009-06-16 00:23:19 +00002722 EFI_STATUS Status;
jcarsey975136a2009-06-16 19:03:54 +00002723 CHAR16 *ResumeLocation;
2724 CHAR16 *FormatWalker;
jcarseya405b862010-09-14 05:18:09 +00002725 UINTN OriginalAttribute;
jcarsey89e85372011-04-13 23:37:50 +00002726 CHAR16 *mPostReplaceFormat;
2727 CHAR16 *mPostReplaceFormat2;
2728
2729 mPostReplaceFormat = AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize));
2730 mPostReplaceFormat2 = AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize));
jcarseya405b862010-09-14 05:18:09 +00002731
jcarseyf8d3e682011-04-19 17:54:42 +00002732 if (mPostReplaceFormat == NULL || mPostReplaceFormat2 == NULL) {
2733 SHELL_FREE_NON_NULL(mPostReplaceFormat);
2734 SHELL_FREE_NON_NULL(mPostReplaceFormat2);
2735 return (EFI_OUT_OF_RESOURCES);
2736 }
2737
jcarseya405b862010-09-14 05:18:09 +00002738 Status = EFI_SUCCESS;
2739 OriginalAttribute = gST->ConOut->Mode->Attribute;
jcarsey1e6e84c2010-01-25 20:05:08 +00002740
jcarsey975136a2009-06-16 19:03:54 +00002741 //
2742 // Back and forth each time fixing up 1 of our flags...
2743 //
jcarseya405b862010-09-14 05:18:09 +00002744 Status = ShellCopySearchAndReplace(Format, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%N", L"%%N", FALSE, FALSE);
jcarsey975136a2009-06-16 19:03:54 +00002745 ASSERT_EFI_ERROR(Status);
jcarseya405b862010-09-14 05:18:09 +00002746 Status = ShellCopySearchAndReplace(mPostReplaceFormat, mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), L"%E", L"%%E", FALSE, FALSE);
jcarsey975136a2009-06-16 19:03:54 +00002747 ASSERT_EFI_ERROR(Status);
jcarseya405b862010-09-14 05:18:09 +00002748 Status = ShellCopySearchAndReplace(mPostReplaceFormat2, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%H", L"%%H", FALSE, FALSE);
jcarsey975136a2009-06-16 19:03:54 +00002749 ASSERT_EFI_ERROR(Status);
jcarseya405b862010-09-14 05:18:09 +00002750 Status = ShellCopySearchAndReplace(mPostReplaceFormat, mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), L"%B", L"%%B", FALSE, FALSE);
jcarsey975136a2009-06-16 19:03:54 +00002751 ASSERT_EFI_ERROR(Status);
jcarseya405b862010-09-14 05:18:09 +00002752 Status = ShellCopySearchAndReplace(mPostReplaceFormat2, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%V", L"%%V", FALSE, FALSE);
jcarsey975136a2009-06-16 19:03:54 +00002753 ASSERT_EFI_ERROR(Status);
2754
2755 //
2756 // Use the last buffer from replacing to print from...
2757 //
jcarseya405b862010-09-14 05:18:09 +00002758 UnicodeVSPrint (mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), mPostReplaceFormat, Marker);
jcarseyb1f95a02009-06-16 00:23:19 +00002759
2760 if (Col != -1 && Row != -1) {
jcarseyb1f95a02009-06-16 00:23:19 +00002761 Status = gST->ConOut->SetCursorPosition(gST->ConOut, Col, Row);
jcarsey975136a2009-06-16 19:03:54 +00002762 }
2763
jcarseyecd3d592009-12-07 18:05:00 +00002764 FormatWalker = mPostReplaceFormat2;
jcarsey2247dde2009-11-09 18:08:58 +00002765 while (*FormatWalker != CHAR_NULL) {
jcarsey975136a2009-06-16 19:03:54 +00002766 //
2767 // Find the next attribute change request
2768 //
2769 ResumeLocation = StrStr(FormatWalker, L"%");
2770 if (ResumeLocation != NULL) {
jcarsey2247dde2009-11-09 18:08:58 +00002771 *ResumeLocation = CHAR_NULL;
jcarsey975136a2009-06-16 19:03:54 +00002772 }
2773 //
2774 // print the current FormatWalker string
2775 //
jcarseya405b862010-09-14 05:18:09 +00002776 if (StrLen(FormatWalker)>0) {
2777 Status = InternalPrintTo(FormatWalker);
2778 if (EFI_ERROR(Status)) {
2779 break;
2780 }
2781 }
2782
jcarsey975136a2009-06-16 19:03:54 +00002783 //
2784 // update the attribute
2785 //
2786 if (ResumeLocation != NULL) {
jcarsey5d46f172011-08-23 15:34:23 +00002787 if (*(ResumeLocation-1) == L'^') {
2788 //
jcarsey8bb74412012-01-30 18:44:41 +00002789 // Move cursor back 1 position to overwrite the ^
2790 //
2791 gST->ConOut->SetCursorPosition(gST->ConOut, gST->ConOut->Mode->CursorColumn - 1, gST->ConOut->Mode->CursorRow);
2792
2793 //
jcarsey5d46f172011-08-23 15:34:23 +00002794 // Print a simple '%' symbol
2795 //
2796 Status = InternalPrintTo(L"%");
2797 ResumeLocation = ResumeLocation - 1;
2798 } else {
2799 switch (*(ResumeLocation+1)) {
2800 case (L'N'):
2801 gST->ConOut->SetAttribute(gST->ConOut, OriginalAttribute);
jcarseya405b862010-09-14 05:18:09 +00002802 break;
jcarsey5d46f172011-08-23 15:34:23 +00002803 case (L'E'):
2804 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_YELLOW, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));
2805 break;
2806 case (L'H'):
2807 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_WHITE, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));
2808 break;
2809 case (L'B'):
2810 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_BLUE, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));
2811 break;
2812 case (L'V'):
2813 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_GREEN, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));
2814 break;
2815 default:
2816 //
2817 // Print a simple '%' symbol
2818 //
2819 Status = InternalPrintTo(L"%");
2820 if (EFI_ERROR(Status)) {
2821 break;
2822 }
2823 ResumeLocation = ResumeLocation - 1;
2824 break;
2825 }
jcarsey975136a2009-06-16 19:03:54 +00002826 }
2827 } else {
2828 //
2829 // reset to normal now...
2830 //
jcarsey975136a2009-06-16 19:03:54 +00002831 break;
2832 }
2833
2834 //
2835 // update FormatWalker to Resume + 2 (skip the % and the indicator)
2836 //
2837 FormatWalker = ResumeLocation + 2;
2838 }
jcarseyb1f95a02009-06-16 00:23:19 +00002839
jcarseya405b862010-09-14 05:18:09 +00002840 gST->ConOut->SetAttribute(gST->ConOut, OriginalAttribute);
jcarsey89e85372011-04-13 23:37:50 +00002841
2842 SHELL_FREE_NON_NULL(mPostReplaceFormat);
2843 SHELL_FREE_NON_NULL(mPostReplaceFormat2);
jcarseya405b862010-09-14 05:18:09 +00002844 return (Status);
jcarsey5f7431d2009-07-10 18:06:01 +00002845}
jcarsey2247dde2009-11-09 18:08:58 +00002846
2847/**
2848 Print at a specific location on the screen.
2849
jcarseye2f82972009-12-01 05:40:24 +00002850 This function will move the cursor to a given screen location and print the specified string.
jcarsey1e6e84c2010-01-25 20:05:08 +00002851
2852 If -1 is specified for either the Row or Col the current screen location for BOTH
jcarsey2247dde2009-11-09 18:08:58 +00002853 will be used.
2854
jcarseye2f82972009-12-01 05:40:24 +00002855 If either Row or Col is out of range for the current console, then ASSERT.
2856 If Format is NULL, then ASSERT.
jcarsey2247dde2009-11-09 18:08:58 +00002857
jcarsey1e6e84c2010-01-25 20:05:08 +00002858 In addition to the standard %-based flags as supported by UefiLib Print() this supports
jcarsey2247dde2009-11-09 18:08:58 +00002859 the following additional flags:
2860 %N - Set output attribute to normal
2861 %H - Set output attribute to highlight
2862 %E - Set output attribute to error
2863 %B - Set output attribute to blue color
2864 %V - Set output attribute to green color
2865
2866 Note: The background color is controlled by the shell command cls.
2867
jcarsey2247dde2009-11-09 18:08:58 +00002868 @param[in] Col the column to print at
jcarseya405b862010-09-14 05:18:09 +00002869 @param[in] Row the row to print at
jcarsey2247dde2009-11-09 18:08:58 +00002870 @param[in] Format the format string
jcarseya405b862010-09-14 05:18:09 +00002871 @param[in] ... The variable argument list.
jcarsey2247dde2009-11-09 18:08:58 +00002872
jcarseya405b862010-09-14 05:18:09 +00002873 @return EFI_SUCCESS The printing was successful.
2874 @return EFI_DEVICE_ERROR The console device reported an error.
jcarsey2247dde2009-11-09 18:08:58 +00002875**/
jcarseya405b862010-09-14 05:18:09 +00002876EFI_STATUS
jcarsey2247dde2009-11-09 18:08:58 +00002877EFIAPI
2878ShellPrintEx(
2879 IN INT32 Col OPTIONAL,
2880 IN INT32 Row OPTIONAL,
2881 IN CONST CHAR16 *Format,
2882 ...
jcarsey1e6e84c2010-01-25 20:05:08 +00002883 )
jcarsey2247dde2009-11-09 18:08:58 +00002884{
2885 VA_LIST Marker;
jcarseya405b862010-09-14 05:18:09 +00002886 EFI_STATUS RetVal;
jcarsey3e082d52010-10-04 16:44:57 +00002887 if (Format == NULL) {
2888 return (EFI_INVALID_PARAMETER);
2889 }
jcarsey2247dde2009-11-09 18:08:58 +00002890 VA_START (Marker, Format);
jcarseya405b862010-09-14 05:18:09 +00002891 RetVal = InternalShellPrintWorker(Col, Row, Format, Marker);
jcarseye2f82972009-12-01 05:40:24 +00002892 VA_END(Marker);
jcarseya405b862010-09-14 05:18:09 +00002893 return(RetVal);
jcarsey2247dde2009-11-09 18:08:58 +00002894}
2895
2896/**
2897 Print at a specific location on the screen.
2898
jcarseye2f82972009-12-01 05:40:24 +00002899 This function will move the cursor to a given screen location and print the specified string.
jcarsey1e6e84c2010-01-25 20:05:08 +00002900
2901 If -1 is specified for either the Row or Col the current screen location for BOTH
jcarseye2f82972009-12-01 05:40:24 +00002902 will be used.
jcarsey2247dde2009-11-09 18:08:58 +00002903
jcarseye2f82972009-12-01 05:40:24 +00002904 If either Row or Col is out of range for the current console, then ASSERT.
2905 If Format is NULL, then ASSERT.
jcarsey2247dde2009-11-09 18:08:58 +00002906
jcarsey1e6e84c2010-01-25 20:05:08 +00002907 In addition to the standard %-based flags as supported by UefiLib Print() this supports
jcarsey2247dde2009-11-09 18:08:58 +00002908 the following additional flags:
jcarsey1e6e84c2010-01-25 20:05:08 +00002909 %N - Set output attribute to normal.
2910 %H - Set output attribute to highlight.
2911 %E - Set output attribute to error.
2912 %B - Set output attribute to blue color.
2913 %V - Set output attribute to green color.
jcarsey2247dde2009-11-09 18:08:58 +00002914
2915 Note: The background color is controlled by the shell command cls.
2916
jcarsey1e6e84c2010-01-25 20:05:08 +00002917 @param[in] Col The column to print at.
jcarseya405b862010-09-14 05:18:09 +00002918 @param[in] Row The row to print at.
jcarsey1e6e84c2010-01-25 20:05:08 +00002919 @param[in] Language The language of the string to retrieve. If this parameter
2920 is NULL, then the current platform language is used.
2921 @param[in] HiiFormatStringId The format string Id for getting from Hii.
2922 @param[in] HiiFormatHandle The format string Handle for getting from Hii.
jcarseya405b862010-09-14 05:18:09 +00002923 @param[in] ... The variable argument list.
jcarsey2247dde2009-11-09 18:08:58 +00002924
jcarseya405b862010-09-14 05:18:09 +00002925 @return EFI_SUCCESS The printing was successful.
2926 @return EFI_DEVICE_ERROR The console device reported an error.
jcarsey2247dde2009-11-09 18:08:58 +00002927**/
jcarseya405b862010-09-14 05:18:09 +00002928EFI_STATUS
jcarsey2247dde2009-11-09 18:08:58 +00002929EFIAPI
2930ShellPrintHiiEx(
2931 IN INT32 Col OPTIONAL,
2932 IN INT32 Row OPTIONAL,
jcarsey1e6e84c2010-01-25 20:05:08 +00002933 IN CONST CHAR8 *Language OPTIONAL,
jcarsey2247dde2009-11-09 18:08:58 +00002934 IN CONST EFI_STRING_ID HiiFormatStringId,
2935 IN CONST EFI_HANDLE HiiFormatHandle,
2936 ...
2937 )
2938{
2939 VA_LIST Marker;
2940 CHAR16 *HiiFormatString;
jcarseya405b862010-09-14 05:18:09 +00002941 EFI_STATUS RetVal;
jcarsey2247dde2009-11-09 18:08:58 +00002942
2943 VA_START (Marker, HiiFormatHandle);
jcarsey1e6e84c2010-01-25 20:05:08 +00002944 HiiFormatString = HiiGetString(HiiFormatHandle, HiiFormatStringId, Language);
jcarsey2247dde2009-11-09 18:08:58 +00002945 ASSERT(HiiFormatString != NULL);
2946
2947 RetVal = InternalShellPrintWorker(Col, Row, HiiFormatString, Marker);
2948
jcarseya405b862010-09-14 05:18:09 +00002949 SHELL_FREE_NON_NULL(HiiFormatString);
jcarseye2f82972009-12-01 05:40:24 +00002950 VA_END(Marker);
jcarsey2247dde2009-11-09 18:08:58 +00002951
2952 return (RetVal);
2953}
2954
2955/**
2956 Function to determine if a given filename represents a file or a directory.
2957
2958 @param[in] DirName Path to directory to test.
2959
jcarseyc8c22592011-10-17 17:49:21 +00002960 @retval EFI_SUCCESS The Path represents a directory
2961 @retval EFI_NOT_FOUND The Path does not represent a directory
2962 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
2963 @return The path failed to open
jcarsey2247dde2009-11-09 18:08:58 +00002964**/
2965EFI_STATUS
2966EFIAPI
2967ShellIsDirectory(
2968 IN CONST CHAR16 *DirName
2969 )
2970{
2971 EFI_STATUS Status;
jcarseya405b862010-09-14 05:18:09 +00002972 SHELL_FILE_HANDLE Handle;
jcarsey3e082d52010-10-04 16:44:57 +00002973 CHAR16 *TempLocation;
2974 CHAR16 *TempLocation2;
jcarsey2247dde2009-11-09 18:08:58 +00002975
jcarseyecd3d592009-12-07 18:05:00 +00002976 ASSERT(DirName != NULL);
2977
jcarseya405b862010-09-14 05:18:09 +00002978 Handle = NULL;
2979 TempLocation = NULL;
jcarsey2247dde2009-11-09 18:08:58 +00002980
2981 Status = ShellOpenFileByName(DirName, &Handle, EFI_FILE_MODE_READ, 0);
2982 if (EFI_ERROR(Status)) {
jcarseya405b862010-09-14 05:18:09 +00002983 //
2984 // try good logic first.
2985 //
jcarsey366f81a2011-06-27 21:04:22 +00002986 if (gEfiShellProtocol != NULL) {
jcarsey3e082d52010-10-04 16:44:57 +00002987 TempLocation = StrnCatGrow(&TempLocation, NULL, DirName, 0);
jcarseyc8c22592011-10-17 17:49:21 +00002988 if (TempLocation == NULL) {
2989 ShellCloseFile(&Handle);
2990 return (EFI_OUT_OF_RESOURCES);
2991 }
jcarsey3e082d52010-10-04 16:44:57 +00002992 TempLocation2 = StrStr(TempLocation, L":");
2993 if (TempLocation2 != NULL && StrLen(StrStr(TempLocation, L":")) == 2) {
2994 *(TempLocation2+1) = CHAR_NULL;
jcarseya405b862010-09-14 05:18:09 +00002995 }
jcarsey366f81a2011-06-27 21:04:22 +00002996 if (gEfiShellProtocol->GetDevicePathFromMap(TempLocation) != NULL) {
jcarseya405b862010-09-14 05:18:09 +00002997 FreePool(TempLocation);
2998 return (EFI_SUCCESS);
2999 }
3000 FreePool(TempLocation);
3001 } else {
3002 //
3003 // probably a map name?!?!!?
3004 //
3005 TempLocation = StrStr(DirName, L"\\");
3006 if (TempLocation != NULL && *(TempLocation+1) == CHAR_NULL) {
3007 return (EFI_SUCCESS);
3008 }
3009 }
jcarsey2247dde2009-11-09 18:08:58 +00003010 return (Status);
3011 }
3012
3013 if (FileHandleIsDirectory(Handle) == EFI_SUCCESS) {
3014 ShellCloseFile(&Handle);
3015 return (EFI_SUCCESS);
3016 }
3017 ShellCloseFile(&Handle);
3018 return (EFI_NOT_FOUND);
3019}
3020
jcarsey125c2cf2009-11-18 21:36:50 +00003021/**
jcarsey36a9d672009-11-20 21:13:41 +00003022 Function to determine if a given filename represents a file.
3023
3024 @param[in] Name Path to file to test.
3025
3026 @retval EFI_SUCCESS The Path represents a file.
3027 @retval EFI_NOT_FOUND The Path does not represent a file.
3028 @retval other The path failed to open.
3029**/
3030EFI_STATUS
3031EFIAPI
3032ShellIsFile(
3033 IN CONST CHAR16 *Name
3034 )
3035{
3036 EFI_STATUS Status;
jcarseya405b862010-09-14 05:18:09 +00003037 SHELL_FILE_HANDLE Handle;
jcarsey36a9d672009-11-20 21:13:41 +00003038
jcarseyecd3d592009-12-07 18:05:00 +00003039 ASSERT(Name != NULL);
3040
jcarsey36a9d672009-11-20 21:13:41 +00003041 Handle = NULL;
3042
3043 Status = ShellOpenFileByName(Name, &Handle, EFI_FILE_MODE_READ, 0);
3044 if (EFI_ERROR(Status)) {
3045 return (Status);
3046 }
3047
3048 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {
3049 ShellCloseFile(&Handle);
3050 return (EFI_SUCCESS);
3051 }
3052 ShellCloseFile(&Handle);
3053 return (EFI_NOT_FOUND);
3054}
3055
3056/**
jcarseyb3011f42010-01-11 21:49:04 +00003057 Function to determine if a given filename represents a file.
3058
3059 This will search the CWD and then the Path.
3060
3061 If Name is NULL, then ASSERT.
3062
3063 @param[in] Name Path to file to test.
3064
3065 @retval EFI_SUCCESS The Path represents a file.
3066 @retval EFI_NOT_FOUND The Path does not represent a file.
3067 @retval other The path failed to open.
3068**/
3069EFI_STATUS
3070EFIAPI
3071ShellIsFileInPath(
3072 IN CONST CHAR16 *Name
jcarseya405b862010-09-14 05:18:09 +00003073 )
3074{
jcarseyb3011f42010-01-11 21:49:04 +00003075 CHAR16 *NewName;
3076 EFI_STATUS Status;
3077
3078 if (!EFI_ERROR(ShellIsFile(Name))) {
jcarseya405b862010-09-14 05:18:09 +00003079 return (EFI_SUCCESS);
jcarseyb3011f42010-01-11 21:49:04 +00003080 }
3081
3082 NewName = ShellFindFilePath(Name);
3083 if (NewName == NULL) {
3084 return (EFI_NOT_FOUND);
3085 }
3086 Status = ShellIsFile(NewName);
3087 FreePool(NewName);
3088 return (Status);
3089}
jcarsey252d9452011-03-25 20:49:53 +00003090
jcarseyb3011f42010-01-11 21:49:04 +00003091/**
Jaben Carsey74b0fb82013-11-22 21:37:34 +00003092 Function return the number converted from a hex representation of a number.
3093
3094 Note: this function cannot be used when (UINTN)(-1), (0xFFFFFFFF) may be a valid
3095 result. Use ShellConvertStringToUint64 instead.
3096
3097 @param[in] String String representation of a number.
3098
3099 @return The unsigned integer result of the conversion.
3100 @retval (UINTN)(-1) An error occured.
3101**/
3102UINTN
3103EFIAPI
3104ShellHexStrToUintn(
3105 IN CONST CHAR16 *String
3106 )
3107{
3108 UINT64 RetVal;
3109
3110 if (!EFI_ERROR(ShellConvertStringToUint64(String, &RetVal, TRUE, TRUE))) {
3111 return ((UINTN)RetVal);
3112 }
3113
3114 return ((UINTN)(-1));
3115}
3116
3117/**
jcarsey1e6e84c2010-01-25 20:05:08 +00003118 Function to determine whether a string is decimal or hex representation of a number
Jaben Carseyd59fc242013-11-14 23:52:43 +00003119 and return the number converted from the string. Spaces are always skipped.
jcarsey125c2cf2009-11-18 21:36:50 +00003120
3121 @param[in] String String representation of a number
3122
jcarsey252d9452011-03-25 20:49:53 +00003123 @return the number
3124 @retval (UINTN)(-1) An error ocurred.
jcarsey125c2cf2009-11-18 21:36:50 +00003125**/
3126UINTN
3127EFIAPI
3128ShellStrToUintn(
3129 IN CONST CHAR16 *String
3130 )
3131{
jcarsey252d9452011-03-25 20:49:53 +00003132 UINT64 RetVal;
3133 BOOLEAN Hex;
3134
3135 Hex = FALSE;
3136
3137 if (!InternalShellIsHexOrDecimalNumber(String, Hex, TRUE)) {
3138 Hex = TRUE;
jcarsey125c2cf2009-11-18 21:36:50 +00003139 }
jcarsey252d9452011-03-25 20:49:53 +00003140
3141 if (!EFI_ERROR(ShellConvertStringToUint64(String, &RetVal, Hex, TRUE))) {
3142 return ((UINTN)RetVal);
3143 }
3144 return ((UINTN)(-1));
jcarsey125c2cf2009-11-18 21:36:50 +00003145}
3146
3147/**
3148 Safely append with automatic string resizing given length of Destination and
3149 desired length of copy from Source.
3150
3151 append the first D characters of Source to the end of Destination, where D is
3152 the lesser of Count and the StrLen() of Source. If appending those D characters
3153 will fit within Destination (whose Size is given as CurrentSize) and
jcarsey1e6e84c2010-01-25 20:05:08 +00003154 still leave room for a NULL terminator, then those characters are appended,
3155 starting at the original terminating NULL of Destination, and a new terminating
3156 NULL is appended.
jcarsey125c2cf2009-11-18 21:36:50 +00003157
3158 If appending D characters onto Destination will result in a overflow of the size
3159 given in CurrentSize the string will be grown such that the copy can be performed
3160 and CurrentSize will be updated to the new size.
3161
3162 If Source is NULL, there is nothing to append, just return the current buffer in
3163 Destination.
3164
3165 if Destination is NULL, then ASSERT()
3166 if Destination's current length (including NULL terminator) is already more then
3167 CurrentSize, then ASSERT()
3168
ydong104ff7e372011-09-02 08:05:34 +00003169 @param[in, out] Destination The String to append onto
3170 @param[in, out] CurrentSize on call the number of bytes in Destination. On
jcarsey125c2cf2009-11-18 21:36:50 +00003171 return possibly the new size (still in bytes). if NULL
3172 then allocate whatever is needed.
3173 @param[in] Source The String to append from
3174 @param[in] Count Maximum number of characters to append. if 0 then
3175 all are appended.
3176
3177 @return Destination return the resultant string.
3178**/
3179CHAR16*
3180EFIAPI
3181StrnCatGrow (
3182 IN OUT CHAR16 **Destination,
3183 IN OUT UINTN *CurrentSize,
3184 IN CONST CHAR16 *Source,
3185 IN UINTN Count
3186 )
3187{
3188 UINTN DestinationStartSize;
3189 UINTN NewSize;
3190
3191 //
3192 // ASSERTs
3193 //
3194 ASSERT(Destination != NULL);
3195
3196 //
3197 // If there's nothing to do then just return Destination
3198 //
3199 if (Source == NULL) {
3200 return (*Destination);
3201 }
3202
3203 //
3204 // allow for un-initialized pointers, based on size being 0
3205 //
3206 if (CurrentSize != NULL && *CurrentSize == 0) {
3207 *Destination = NULL;
3208 }
3209
3210 //
3211 // allow for NULL pointers address as Destination
3212 //
3213 if (*Destination != NULL) {
3214 ASSERT(CurrentSize != 0);
3215 DestinationStartSize = StrSize(*Destination);
3216 ASSERT(DestinationStartSize <= *CurrentSize);
3217 } else {
3218 DestinationStartSize = 0;
3219// ASSERT(*CurrentSize == 0);
3220 }
3221
3222 //
3223 // Append all of Source?
3224 //
3225 if (Count == 0) {
3226 Count = StrLen(Source);
3227 }
3228
3229 //
3230 // Test and grow if required
3231 //
3232 if (CurrentSize != NULL) {
3233 NewSize = *CurrentSize;
ydong10f480fdc2012-09-07 01:55:33 +00003234 if (NewSize < DestinationStartSize + (Count * sizeof(CHAR16))) {
3235 while (NewSize < (DestinationStartSize + (Count*sizeof(CHAR16)))) {
3236 NewSize += 2 * Count * sizeof(CHAR16);
3237 }
3238 *Destination = ReallocatePool(*CurrentSize, NewSize, *Destination);
3239 *CurrentSize = NewSize;
jcarsey125c2cf2009-11-18 21:36:50 +00003240 }
jcarsey125c2cf2009-11-18 21:36:50 +00003241 } else {
3242 *Destination = AllocateZeroPool((Count+1)*sizeof(CHAR16));
3243 }
3244
3245 //
3246 // Now use standard StrnCat on a big enough buffer
3247 //
jcarseyc9d92df2010-02-03 15:37:54 +00003248 if (*Destination == NULL) {
3249 return (NULL);
3250 }
jcarsey125c2cf2009-11-18 21:36:50 +00003251 return StrnCat(*Destination, Source, Count);
3252}
jcarseyc9d92df2010-02-03 15:37:54 +00003253
3254/**
3255 Prompt the user and return the resultant answer to the requestor.
3256
3257 This function will display the requested question on the shell prompt and then
3258 wait for an apropriate answer to be input from the console.
3259
jcarseya405b862010-09-14 05:18:09 +00003260 if the SHELL_PROMPT_REQUEST_TYPE is SHELL_PROMPT_REQUEST_TYPE_YESNO, ShellPromptResponseTypeQuitContinue
jcarseyc9d92df2010-02-03 15:37:54 +00003261 or SHELL_PROMPT_REQUEST_TYPE_YESNOCANCEL then *Response is of type SHELL_PROMPT_RESPONSE.
3262
jcarseya405b862010-09-14 05:18:09 +00003263 if the SHELL_PROMPT_REQUEST_TYPE is ShellPromptResponseTypeFreeform then *Response is of type
jcarseyc9d92df2010-02-03 15:37:54 +00003264 CHAR16*.
3265
3266 In either case *Response must be callee freed if Response was not NULL;
3267
3268 @param Type What type of question is asked. This is used to filter the input
3269 to prevent invalid answers to question.
3270 @param Prompt Pointer to string prompt to use to request input.
3271 @param Response Pointer to Response which will be populated upon return.
3272
3273 @retval EFI_SUCCESS The operation was sucessful.
3274 @retval EFI_UNSUPPORTED The operation is not supported as requested.
3275 @retval EFI_INVALID_PARAMETER A parameter was invalid.
3276 @return other The operation failed.
3277**/
3278EFI_STATUS
3279EFIAPI
3280ShellPromptForResponse (
3281 IN SHELL_PROMPT_REQUEST_TYPE Type,
3282 IN CHAR16 *Prompt OPTIONAL,
3283 IN OUT VOID **Response OPTIONAL
3284 )
3285{
3286 EFI_STATUS Status;
3287 EFI_INPUT_KEY Key;
3288 UINTN EventIndex;
3289 SHELL_PROMPT_RESPONSE *Resp;
jcarseya405b862010-09-14 05:18:09 +00003290 UINTN Size;
3291 CHAR16 *Buffer;
jcarseyc9d92df2010-02-03 15:37:54 +00003292
jcarseya405b862010-09-14 05:18:09 +00003293 Status = EFI_UNSUPPORTED;
3294 Resp = NULL;
3295 Buffer = NULL;
3296 Size = 0;
3297 if (Type != ShellPromptResponseTypeFreeform) {
jcarsey252d9452011-03-25 20:49:53 +00003298 Resp = (SHELL_PROMPT_RESPONSE*)AllocateZeroPool(sizeof(SHELL_PROMPT_RESPONSE));
jcarsey3e082d52010-10-04 16:44:57 +00003299 if (Resp == NULL) {
3300 return (EFI_OUT_OF_RESOURCES);
3301 }
xdu290bfa222010-07-19 05:21:27 +00003302 }
jcarseyc9d92df2010-02-03 15:37:54 +00003303
3304 switch(Type) {
jcarseya405b862010-09-14 05:18:09 +00003305 case ShellPromptResponseTypeQuitContinue:
jcarseyc9d92df2010-02-03 15:37:54 +00003306 if (Prompt != NULL) {
3307 ShellPrintEx(-1, -1, L"%s", Prompt);
3308 }
3309 //
3310 // wait for valid response
3311 //
3312 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
3313 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
Jaben Carsey31b018a2014-01-16 16:52:39 +00003314 if (EFI_ERROR(Status)) {
3315 break;
3316 }
jcarseyc9d92df2010-02-03 15:37:54 +00003317 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3318 if (Key.UnicodeChar == L'Q' || Key.UnicodeChar ==L'q') {
jcarseya405b862010-09-14 05:18:09 +00003319 *Resp = ShellPromptResponseQuit;
jcarseyc9d92df2010-02-03 15:37:54 +00003320 } else {
jcarseya405b862010-09-14 05:18:09 +00003321 *Resp = ShellPromptResponseContinue;
jcarseyc9d92df2010-02-03 15:37:54 +00003322 }
3323 break;
jcarseya405b862010-09-14 05:18:09 +00003324 case ShellPromptResponseTypeYesNoCancel:
jcarseyc9d92df2010-02-03 15:37:54 +00003325 if (Prompt != NULL) {
3326 ShellPrintEx(-1, -1, L"%s", Prompt);
3327 }
3328 //
3329 // wait for valid response
3330 //
jcarseya405b862010-09-14 05:18:09 +00003331 *Resp = ShellPromptResponseMax;
3332 while (*Resp == ShellPromptResponseMax) {
Jaben Carsey194ae482013-12-09 22:55:13 +00003333 if (ShellGetExecutionBreakFlag()) {
3334 Status = EFI_ABORTED;
3335 break;
3336 }
jcarseyc9d92df2010-02-03 15:37:54 +00003337 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
3338 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
Jaben Carsey31b018a2014-01-16 16:52:39 +00003339 if (EFI_ERROR(Status)) {
3340 break;
3341 }
jcarseyc9d92df2010-02-03 15:37:54 +00003342 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3343 switch (Key.UnicodeChar) {
3344 case L'Y':
3345 case L'y':
jcarseya405b862010-09-14 05:18:09 +00003346 *Resp = ShellPromptResponseYes;
jcarseyc9d92df2010-02-03 15:37:54 +00003347 break;
3348 case L'N':
3349 case L'n':
jcarseya405b862010-09-14 05:18:09 +00003350 *Resp = ShellPromptResponseNo;
jcarseyc9d92df2010-02-03 15:37:54 +00003351 break;
3352 case L'C':
3353 case L'c':
jcarseya405b862010-09-14 05:18:09 +00003354 *Resp = ShellPromptResponseCancel;
3355 break;
3356 }
3357 }
3358 break; case ShellPromptResponseTypeYesNoAllCancel:
3359 if (Prompt != NULL) {
3360 ShellPrintEx(-1, -1, L"%s", Prompt);
3361 }
3362 //
3363 // wait for valid response
3364 //
3365 *Resp = ShellPromptResponseMax;
3366 while (*Resp == ShellPromptResponseMax) {
Jaben Carsey194ae482013-12-09 22:55:13 +00003367 if (ShellGetExecutionBreakFlag()) {
3368 Status = EFI_ABORTED;
3369 break;
3370 }
jcarseya405b862010-09-14 05:18:09 +00003371 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
3372 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
Jaben Carsey31b018a2014-01-16 16:52:39 +00003373 if (EFI_ERROR(Status)) {
3374 break;
3375 }
jcarseya405b862010-09-14 05:18:09 +00003376 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3377 switch (Key.UnicodeChar) {
3378 case L'Y':
3379 case L'y':
3380 *Resp = ShellPromptResponseYes;
3381 break;
3382 case L'N':
3383 case L'n':
3384 *Resp = ShellPromptResponseNo;
3385 break;
3386 case L'A':
3387 case L'a':
3388 *Resp = ShellPromptResponseAll;
3389 break;
3390 case L'C':
3391 case L'c':
3392 *Resp = ShellPromptResponseCancel;
jcarseyc9d92df2010-02-03 15:37:54 +00003393 break;
3394 }
3395 }
3396 break;
jcarseya405b862010-09-14 05:18:09 +00003397 case ShellPromptResponseTypeEnterContinue:
3398 case ShellPromptResponseTypeAnyKeyContinue:
jcarseyc9d92df2010-02-03 15:37:54 +00003399 if (Prompt != NULL) {
3400 ShellPrintEx(-1, -1, L"%s", Prompt);
3401 }
3402 //
3403 // wait for valid response
3404 //
jcarseya405b862010-09-14 05:18:09 +00003405 *Resp = ShellPromptResponseMax;
3406 while (*Resp == ShellPromptResponseMax) {
Jaben Carsey194ae482013-12-09 22:55:13 +00003407 if (ShellGetExecutionBreakFlag()) {
3408 Status = EFI_ABORTED;
3409 break;
3410 }
jcarseyc9d92df2010-02-03 15:37:54 +00003411 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
jcarseya405b862010-09-14 05:18:09 +00003412 if (Type == ShellPromptResponseTypeEnterContinue) {
jcarseyc9d92df2010-02-03 15:37:54 +00003413 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
Jaben Carsey31b018a2014-01-16 16:52:39 +00003414 if (EFI_ERROR(Status)) {
3415 break;
3416 }
jcarseyc9d92df2010-02-03 15:37:54 +00003417 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3418 if (Key.UnicodeChar == CHAR_CARRIAGE_RETURN) {
jcarseya405b862010-09-14 05:18:09 +00003419 *Resp = ShellPromptResponseContinue;
jcarseyc9d92df2010-02-03 15:37:54 +00003420 break;
3421 }
3422 }
jcarseya405b862010-09-14 05:18:09 +00003423 if (Type == ShellPromptResponseTypeAnyKeyContinue) {
3424 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
3425 ASSERT_EFI_ERROR(Status);
3426 *Resp = ShellPromptResponseContinue;
jcarseyc9d92df2010-02-03 15:37:54 +00003427 break;
3428 }
3429 }
3430 break;
jcarseya405b862010-09-14 05:18:09 +00003431 case ShellPromptResponseTypeYesNo:
3432 if (Prompt != NULL) {
3433 ShellPrintEx(-1, -1, L"%s", Prompt);
3434 }
3435 //
3436 // wait for valid response
3437 //
3438 *Resp = ShellPromptResponseMax;
3439 while (*Resp == ShellPromptResponseMax) {
Jaben Carsey194ae482013-12-09 22:55:13 +00003440 if (ShellGetExecutionBreakFlag()) {
3441 Status = EFI_ABORTED;
3442 break;
3443 }
jcarseya405b862010-09-14 05:18:09 +00003444 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
3445 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
Jaben Carsey31b018a2014-01-16 16:52:39 +00003446 if (EFI_ERROR(Status)) {
3447 break;
3448 }
jcarseya405b862010-09-14 05:18:09 +00003449 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3450 switch (Key.UnicodeChar) {
3451 case L'Y':
3452 case L'y':
3453 *Resp = ShellPromptResponseYes;
3454 break;
3455 case L'N':
3456 case L'n':
3457 *Resp = ShellPromptResponseNo;
3458 break;
3459 }
3460 }
3461 break;
3462 case ShellPromptResponseTypeFreeform:
3463 if (Prompt != NULL) {
3464 ShellPrintEx(-1, -1, L"%s", Prompt);
3465 }
3466 while(1) {
Jaben Carsey194ae482013-12-09 22:55:13 +00003467 if (ShellGetExecutionBreakFlag()) {
3468 Status = EFI_ABORTED;
3469 break;
3470 }
jcarseya405b862010-09-14 05:18:09 +00003471 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
3472 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
Jaben Carsey31b018a2014-01-16 16:52:39 +00003473 if (EFI_ERROR(Status)) {
3474 break;
3475 }
jcarseya405b862010-09-14 05:18:09 +00003476 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3477 if (Key.UnicodeChar == CHAR_CARRIAGE_RETURN) {
3478 break;
3479 }
3480 ASSERT((Buffer == NULL && Size == 0) || (Buffer != NULL));
3481 StrnCatGrow(&Buffer, &Size, &Key.UnicodeChar, 1);
3482 }
3483 break;
3484 //
3485 // This is the location to add new prompt types.
Jaben Carsey194ae482013-12-09 22:55:13 +00003486 // If your new type loops remember to add ExecutionBreak support.
jcarseya405b862010-09-14 05:18:09 +00003487 //
jcarseyc9d92df2010-02-03 15:37:54 +00003488 default:
jcarseya405b862010-09-14 05:18:09 +00003489 ASSERT(FALSE);
jcarseyc9d92df2010-02-03 15:37:54 +00003490 }
3491
3492 if (Response != NULL) {
jcarseya405b862010-09-14 05:18:09 +00003493 if (Resp != NULL) {
3494 *Response = Resp;
3495 } else if (Buffer != NULL) {
3496 *Response = Buffer;
3497 }
jcarseyc9d92df2010-02-03 15:37:54 +00003498 } else {
jcarseya405b862010-09-14 05:18:09 +00003499 if (Resp != NULL) {
3500 FreePool(Resp);
3501 }
3502 if (Buffer != NULL) {
3503 FreePool(Buffer);
3504 }
jcarseyc9d92df2010-02-03 15:37:54 +00003505 }
3506
jcarseya405b862010-09-14 05:18:09 +00003507 ShellPrintEx(-1, -1, L"\r\n");
jcarseyc9d92df2010-02-03 15:37:54 +00003508 return (Status);
3509}
3510
3511/**
3512 Prompt the user and return the resultant answer to the requestor.
3513
3514 This function is the same as ShellPromptForResponse, except that the prompt is
3515 automatically pulled from HII.
3516
3517 @param Type What type of question is asked. This is used to filter the input
3518 to prevent invalid answers to question.
jcarseya405b862010-09-14 05:18:09 +00003519 @param[in] HiiFormatStringId The format string Id for getting from Hii.
3520 @param[in] HiiFormatHandle The format string Handle for getting from Hii.
3521 @param Response Pointer to Response which will be populated upon return.
jcarseyc9d92df2010-02-03 15:37:54 +00003522
3523 @retval EFI_SUCCESS the operation was sucessful.
3524 @return other the operation failed.
3525
3526 @sa ShellPromptForResponse
3527**/
3528EFI_STATUS
3529EFIAPI
3530ShellPromptForResponseHii (
3531 IN SHELL_PROMPT_REQUEST_TYPE Type,
3532 IN CONST EFI_STRING_ID HiiFormatStringId,
3533 IN CONST EFI_HANDLE HiiFormatHandle,
3534 IN OUT VOID **Response
3535 )
3536{
3537 CHAR16 *Prompt;
3538 EFI_STATUS Status;
3539
3540 Prompt = HiiGetString(HiiFormatHandle, HiiFormatStringId, NULL);
3541 Status = ShellPromptForResponse(Type, Prompt, Response);
3542 FreePool(Prompt);
3543 return (Status);
3544}
3545
jcarseya405b862010-09-14 05:18:09 +00003546/**
3547 Function to determin if an entire string is a valid number.
jcarseyc9d92df2010-02-03 15:37:54 +00003548
jcarseya405b862010-09-14 05:18:09 +00003549 If Hex it must be preceeded with a 0x or has ForceHex, set TRUE.
3550
3551 @param[in] String The string to evaluate.
3552 @param[in] ForceHex TRUE - always assume hex.
3553 @param[in] StopAtSpace TRUE to halt upon finding a space, FALSE to keep going.
3554
3555 @retval TRUE It is all numeric (dec/hex) characters.
3556 @retval FALSE There is a non-numeric character.
3557**/
3558BOOLEAN
3559EFIAPI
jcarsey252d9452011-03-25 20:49:53 +00003560InternalShellIsHexOrDecimalNumber (
jcarseya405b862010-09-14 05:18:09 +00003561 IN CONST CHAR16 *String,
3562 IN CONST BOOLEAN ForceHex,
3563 IN CONST BOOLEAN StopAtSpace
3564 )
3565{
3566 BOOLEAN Hex;
3567
3568 //
3569 // chop off a single negative sign
3570 //
3571 if (String != NULL && *String == L'-') {
3572 String++;
3573 }
darylm503b0934ac2011-11-12 00:35:11 +00003574
jcarseya405b862010-09-14 05:18:09 +00003575 if (String == NULL) {
3576 return (FALSE);
3577 }
3578
3579 //
3580 // chop leading zeroes
3581 //
3582 while(String != NULL && *String == L'0'){
3583 String++;
3584 }
3585 //
3586 // allow '0x' or '0X', but not 'x' or 'X'
3587 //
3588 if (String != NULL && (*String == L'x' || *String == L'X')) {
3589 if (*(String-1) != L'0') {
3590 //
3591 // we got an x without a preceeding 0
3592 //
3593 return (FALSE);
3594 }
3595 String++;
3596 Hex = TRUE;
3597 } else if (ForceHex) {
3598 Hex = TRUE;
3599 } else {
3600 Hex = FALSE;
3601 }
3602
3603 //
3604 // loop through the remaining characters and use the lib function
3605 //
3606 for ( ; String != NULL && *String != CHAR_NULL && !(StopAtSpace && *String == L' ') ; String++){
3607 if (Hex) {
3608 if (!ShellIsHexaDecimalDigitCharacter(*String)) {
3609 return (FALSE);
3610 }
3611 } else {
3612 if (!ShellIsDecimalDigitCharacter(*String)) {
3613 return (FALSE);
3614 }
3615 }
3616 }
jcarsey252d9452011-03-25 20:49:53 +00003617
jcarseya405b862010-09-14 05:18:09 +00003618 return (TRUE);
3619}
3620
3621/**
3622 Function to determine if a given filename exists.
3623
3624 @param[in] Name Path to test.
3625
3626 @retval EFI_SUCCESS The Path represents a file.
3627 @retval EFI_NOT_FOUND The Path does not represent a file.
3628 @retval other The path failed to open.
3629**/
3630EFI_STATUS
3631EFIAPI
3632ShellFileExists(
3633 IN CONST CHAR16 *Name
3634 )
3635{
3636 EFI_STATUS Status;
3637 EFI_SHELL_FILE_INFO *List;
3638
3639 ASSERT(Name != NULL);
3640
3641 List = NULL;
3642 Status = ShellOpenFileMetaArg((CHAR16*)Name, EFI_FILE_MODE_READ, &List);
3643 if (EFI_ERROR(Status)) {
3644 return (Status);
3645 }
3646
3647 ShellCloseFileMetaArg(&List);
3648
3649 return (EFI_SUCCESS);
3650}
jcarsey252d9452011-03-25 20:49:53 +00003651
3652/**
darylm503b0934ac2011-11-12 00:35:11 +00003653 Convert a Unicode character to upper case only if
jcarsey252d9452011-03-25 20:49:53 +00003654 it maps to a valid small-case ASCII character.
3655
3656 This internal function only deal with Unicode character
3657 which maps to a valid small-case ASCII character, i.e.
3658 L'a' to L'z'. For other Unicode character, the input character
3659 is returned directly.
3660
3661 @param Char The character to convert.
3662
3663 @retval LowerCharacter If the Char is with range L'a' to L'z'.
3664 @retval Unchanged Otherwise.
3665
3666**/
3667CHAR16
3668EFIAPI
3669InternalShellCharToUpper (
3670 IN CHAR16 Char
3671 )
3672{
3673 if (Char >= L'a' && Char <= L'z') {
3674 return (CHAR16) (Char - (L'a' - L'A'));
3675 }
3676
3677 return Char;
3678}
3679
3680/**
3681 Convert a Unicode character to numerical value.
3682
3683 This internal function only deal with Unicode character
3684 which maps to a valid hexadecimal ASII character, i.e.
darylm503b0934ac2011-11-12 00:35:11 +00003685 L'0' to L'9', L'a' to L'f' or L'A' to L'F'. For other
jcarsey252d9452011-03-25 20:49:53 +00003686 Unicode character, the value returned does not make sense.
3687
3688 @param Char The character to convert.
3689
3690 @return The numerical value converted.
3691
3692**/
3693UINTN
3694EFIAPI
3695InternalShellHexCharToUintn (
3696 IN CHAR16 Char
3697 )
3698{
3699 if (ShellIsDecimalDigitCharacter (Char)) {
3700 return Char - L'0';
3701 }
3702
3703 return (UINTN) (10 + InternalShellCharToUpper (Char) - L'A');
3704}
3705
3706/**
3707 Convert a Null-terminated Unicode hexadecimal string to a value of type UINT64.
3708
3709 This function returns a value of type UINTN by interpreting the contents
3710 of the Unicode string specified by String as a hexadecimal number.
3711 The format of the input Unicode string String is:
3712
3713 [spaces][zeros][x][hexadecimal digits].
3714
3715 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
3716 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix.
3717 If "x" appears in the input string, it must be prefixed with at least one 0.
3718 The function will ignore the pad space, which includes spaces or tab characters,
3719 before [zeros], [x] or [hexadecimal digit]. The running zero before [x] or
3720 [hexadecimal digit] will be ignored. Then, the decoding starts after [x] or the
3721 first valid hexadecimal digit. Then, the function stops at the first character that is
3722 a not a valid hexadecimal character or NULL, whichever one comes first.
3723
3724 If String has only pad spaces, then zero is returned.
3725 If String has no leading pad spaces, leading zeros or valid hexadecimal digits,
3726 then zero is returned.
3727
3728 @param[in] String A pointer to a Null-terminated Unicode string.
3729 @param[out] Value Upon a successful return the value of the conversion.
3730 @param[in] StopAtSpace FALSE to skip spaces.
3731
3732 @retval EFI_SUCCESS The conversion was successful.
3733 @retval EFI_INVALID_PARAMETER A parameter was NULL or invalid.
3734 @retval EFI_DEVICE_ERROR An overflow occured.
3735**/
3736EFI_STATUS
3737EFIAPI
3738InternalShellStrHexToUint64 (
3739 IN CONST CHAR16 *String,
3740 OUT UINT64 *Value,
3741 IN CONST BOOLEAN StopAtSpace
3742 )
3743{
3744 UINT64 Result;
3745
3746 if (String == NULL || StrSize(String) == 0 || Value == NULL) {
3747 return (EFI_INVALID_PARAMETER);
3748 }
darylm503b0934ac2011-11-12 00:35:11 +00003749
jcarsey252d9452011-03-25 20:49:53 +00003750 //
darylm503b0934ac2011-11-12 00:35:11 +00003751 // Ignore the pad spaces (space or tab)
jcarsey252d9452011-03-25 20:49:53 +00003752 //
3753 while ((*String == L' ') || (*String == L'\t')) {
3754 String++;
3755 }
3756
3757 //
3758 // Ignore leading Zeros after the spaces
3759 //
3760 while (*String == L'0') {
3761 String++;
3762 }
3763
3764 if (InternalShellCharToUpper (*String) == L'X') {
3765 if (*(String - 1) != L'0') {
3766 return 0;
3767 }
3768 //
3769 // Skip the 'X'
3770 //
3771 String++;
3772 }
3773
3774 Result = 0;
darylm503b0934ac2011-11-12 00:35:11 +00003775
jcarsey252d9452011-03-25 20:49:53 +00003776 //
ydong1012ea4692012-07-26 05:42:43 +00003777 // Skip spaces if requested
jcarsey252d9452011-03-25 20:49:53 +00003778 //
ydong1012ea4692012-07-26 05:42:43 +00003779 while (StopAtSpace && *String == L' ') {
3780 String++;
jcarsey252d9452011-03-25 20:49:53 +00003781 }
darylm503b0934ac2011-11-12 00:35:11 +00003782
jcarsey252d9452011-03-25 20:49:53 +00003783 while (ShellIsHexaDecimalDigitCharacter (*String)) {
3784 //
darylm503b0934ac2011-11-12 00:35:11 +00003785 // If the Hex Number represented by String overflows according
jcarsey252d9452011-03-25 20:49:53 +00003786 // to the range defined by UINTN, then ASSERT().
3787 //
3788 if (!(Result <= (RShiftU64((((UINT64) ~0) - InternalShellHexCharToUintn (*String)), 4)))) {
3789// if (!(Result <= ((((UINT64) ~0) - InternalShellHexCharToUintn (*String)) >> 4))) {
3790 return (EFI_DEVICE_ERROR);
3791 }
3792
3793 Result = (LShiftU64(Result, 4));
3794 Result += InternalShellHexCharToUintn (*String);
3795 String++;
3796
3797 //
jcarsey49bd4982012-01-27 18:42:43 +00003798 // stop at spaces if requested
jcarsey252d9452011-03-25 20:49:53 +00003799 //
jcarsey49bd4982012-01-27 18:42:43 +00003800 if (StopAtSpace && *String == L' ') {
3801 break;
jcarsey252d9452011-03-25 20:49:53 +00003802 }
3803 }
3804
3805 *Value = Result;
3806 return (EFI_SUCCESS);
3807}
3808
3809/**
3810 Convert a Null-terminated Unicode decimal string to a value of
3811 type UINT64.
3812
3813 This function returns a value of type UINT64 by interpreting the contents
3814 of the Unicode string specified by String as a decimal number. The format
3815 of the input Unicode string String is:
3816
3817 [spaces] [decimal digits].
3818
3819 The valid decimal digit character is in the range [0-9]. The
3820 function will ignore the pad space, which includes spaces or
3821 tab characters, before [decimal digits]. The running zero in the
3822 beginning of [decimal digits] will be ignored. Then, the function
3823 stops at the first character that is a not a valid decimal character
3824 or a Null-terminator, whichever one comes first.
3825
3826 If String has only pad spaces, then 0 is returned.
3827 If String has no pad spaces or valid decimal digits,
3828 then 0 is returned.
3829
3830 @param[in] String A pointer to a Null-terminated Unicode string.
3831 @param[out] Value Upon a successful return the value of the conversion.
3832 @param[in] StopAtSpace FALSE to skip spaces.
3833
3834 @retval EFI_SUCCESS The conversion was successful.
3835 @retval EFI_INVALID_PARAMETER A parameter was NULL or invalid.
3836 @retval EFI_DEVICE_ERROR An overflow occured.
3837**/
3838EFI_STATUS
3839EFIAPI
3840InternalShellStrDecimalToUint64 (
3841 IN CONST CHAR16 *String,
3842 OUT UINT64 *Value,
3843 IN CONST BOOLEAN StopAtSpace
3844 )
3845{
3846 UINT64 Result;
3847
3848 if (String == NULL || StrSize (String) == 0 || Value == NULL) {
3849 return (EFI_INVALID_PARAMETER);
3850 }
3851
3852 //
3853 // Ignore the pad spaces (space or tab)
3854 //
3855 while ((*String == L' ') || (*String == L'\t')) {
3856 String++;
3857 }
3858
3859 //
3860 // Ignore leading Zeros after the spaces
3861 //
3862 while (*String == L'0') {
3863 String++;
3864 }
3865
3866 Result = 0;
3867
3868 //
3869 // Skip spaces if requested
3870 //
3871 while (StopAtSpace && *String == L' ') {
3872 String++;
3873 }
3874 while (ShellIsDecimalDigitCharacter (*String)) {
3875 //
darylm503b0934ac2011-11-12 00:35:11 +00003876 // If the number represented by String overflows according
jcarsey252d9452011-03-25 20:49:53 +00003877 // to the range defined by UINT64, then ASSERT().
3878 //
darylm503b0934ac2011-11-12 00:35:11 +00003879
jcarsey252d9452011-03-25 20:49:53 +00003880 if (!(Result <= (DivU64x32((((UINT64) ~0) - (*String - L'0')),10)))) {
3881 return (EFI_DEVICE_ERROR);
3882 }
3883
3884 Result = MultU64x32(Result, 10) + (*String - L'0');
3885 String++;
3886
3887 //
3888 // Stop at spaces if requested
3889 //
3890 if (StopAtSpace && *String == L' ') {
3891 break;
3892 }
3893 }
3894
3895 *Value = Result;
darylm503b0934ac2011-11-12 00:35:11 +00003896
jcarsey252d9452011-03-25 20:49:53 +00003897 return (EFI_SUCCESS);
3898}
3899
3900/**
3901 Function to verify and convert a string to its numerical value.
3902
3903 If Hex it must be preceeded with a 0x, 0X, or has ForceHex set TRUE.
3904
3905 @param[in] String The string to evaluate.
3906 @param[out] Value Upon a successful return the value of the conversion.
3907 @param[in] ForceHex TRUE - always assume hex.
3908 @param[in] StopAtSpace FALSE to skip spaces.
darylm503b0934ac2011-11-12 00:35:11 +00003909
jcarsey252d9452011-03-25 20:49:53 +00003910 @retval EFI_SUCCESS The conversion was successful.
3911 @retval EFI_INVALID_PARAMETER String contained an invalid character.
3912 @retval EFI_NOT_FOUND String was a number, but Value was NULL.
3913**/
3914EFI_STATUS
3915EFIAPI
3916ShellConvertStringToUint64(
3917 IN CONST CHAR16 *String,
3918 OUT UINT64 *Value,
3919 IN CONST BOOLEAN ForceHex,
3920 IN CONST BOOLEAN StopAtSpace
3921 )
3922{
3923 UINT64 RetVal;
3924 CONST CHAR16 *Walker;
3925 EFI_STATUS Status;
3926 BOOLEAN Hex;
3927
3928 Hex = ForceHex;
3929
3930 if (!InternalShellIsHexOrDecimalNumber(String, Hex, StopAtSpace)) {
3931 if (!Hex) {
3932 Hex = TRUE;
3933 if (!InternalShellIsHexOrDecimalNumber(String, Hex, StopAtSpace)) {
3934 return (EFI_INVALID_PARAMETER);
3935 }
3936 } else {
3937 return (EFI_INVALID_PARAMETER);
3938 }
3939 }
3940
3941 //
3942 // Chop off leading spaces
3943 //
3944 for (Walker = String; Walker != NULL && *Walker != CHAR_NULL && *Walker == L' '; Walker++);
3945
3946 //
3947 // make sure we have something left that is numeric.
3948 //
3949 if (Walker == NULL || *Walker == CHAR_NULL || !InternalShellIsHexOrDecimalNumber(Walker, Hex, StopAtSpace)) {
3950 return (EFI_INVALID_PARAMETER);
darylm503b0934ac2011-11-12 00:35:11 +00003951 }
jcarsey252d9452011-03-25 20:49:53 +00003952
3953 //
3954 // do the conversion.
3955 //
3956 if (Hex || StrnCmp(Walker, L"0x", 2) == 0 || StrnCmp(Walker, L"0X", 2) == 0){
3957 Status = InternalShellStrHexToUint64(Walker, &RetVal, StopAtSpace);
3958 } else {
3959 Status = InternalShellStrDecimalToUint64(Walker, &RetVal, StopAtSpace);
3960 }
3961
3962 if (Value == NULL && !EFI_ERROR(Status)) {
3963 return (EFI_NOT_FOUND);
3964 }
3965
3966 if (Value != NULL) {
3967 *Value = RetVal;
3968 }
3969
3970 return (Status);
3971}
3972
3973/**
3974 Function to determin if an entire string is a valid number.
3975
3976 If Hex it must be preceeded with a 0x or has ForceHex, set TRUE.
3977
3978 @param[in] String The string to evaluate.
3979 @param[in] ForceHex TRUE - always assume hex.
3980 @param[in] StopAtSpace TRUE to halt upon finding a space, FALSE to keep going.
3981
3982 @retval TRUE It is all numeric (dec/hex) characters.
3983 @retval FALSE There is a non-numeric character.
3984**/
3985BOOLEAN
3986EFIAPI
3987ShellIsHexOrDecimalNumber (
3988 IN CONST CHAR16 *String,
3989 IN CONST BOOLEAN ForceHex,
3990 IN CONST BOOLEAN StopAtSpace
3991 )
3992{
3993 if (ShellConvertStringToUint64(String, NULL, ForceHex, StopAtSpace) == EFI_NOT_FOUND) {
3994 return (TRUE);
3995 }
3996 return (FALSE);
3997}
jcarsey4d0a4fc2011-07-06 22:28:36 +00003998
3999/**
4000 Function to read a single line from a SHELL_FILE_HANDLE. The \n is not included in the returned
4001 buffer. The returned buffer must be callee freed.
4002
4003 If the position upon start is 0, then the Ascii Boolean will be set. This should be
4004 maintained and not changed for all operations with the same file.
4005
ydong104ff7e372011-09-02 08:05:34 +00004006 @param[in] Handle SHELL_FILE_HANDLE to read from.
4007 @param[in, out] Ascii Boolean value for indicating whether the file is
4008 Ascii (TRUE) or UCS2 (FALSE).
jcarsey4d0a4fc2011-07-06 22:28:36 +00004009
jcarseybeab0fc2011-10-10 17:26:25 +00004010 @return The line of text from the file.
4011 @retval NULL There was not enough memory available.
jcarsey4d0a4fc2011-07-06 22:28:36 +00004012
4013 @sa ShellFileHandleReadLine
4014**/
4015CHAR16*
4016EFIAPI
4017ShellFileHandleReturnLine(
4018 IN SHELL_FILE_HANDLE Handle,
4019 IN OUT BOOLEAN *Ascii
4020 )
4021{
4022 CHAR16 *RetVal;
4023 UINTN Size;
4024 EFI_STATUS Status;
4025
4026 Size = 0;
4027 RetVal = NULL;
4028
4029 Status = ShellFileHandleReadLine(Handle, RetVal, &Size, FALSE, Ascii);
4030 if (Status == EFI_BUFFER_TOO_SMALL) {
4031 RetVal = AllocateZeroPool(Size);
jcarseybeab0fc2011-10-10 17:26:25 +00004032 if (RetVal == NULL) {
4033 return (NULL);
4034 }
jcarsey4d0a4fc2011-07-06 22:28:36 +00004035 Status = ShellFileHandleReadLine(Handle, RetVal, &Size, FALSE, Ascii);
darylm503b0934ac2011-11-12 00:35:11 +00004036
jcarsey4d0a4fc2011-07-06 22:28:36 +00004037 }
jcarsey4d0a4fc2011-07-06 22:28:36 +00004038 if (EFI_ERROR(Status) && (RetVal != NULL)) {
4039 FreePool(RetVal);
4040 RetVal = NULL;
4041 }
4042 return (RetVal);
4043}
4044
4045/**
4046 Function to read a single line (up to but not including the \n) from a SHELL_FILE_HANDLE.
4047
4048 If the position upon start is 0, then the Ascii Boolean will be set. This should be
4049 maintained and not changed for all operations with the same file.
4050
ydong104ff7e372011-09-02 08:05:34 +00004051 @param[in] Handle SHELL_FILE_HANDLE to read from.
4052 @param[in, out] Buffer The pointer to buffer to read into.
4053 @param[in, out] Size The pointer to number of bytes in Buffer.
4054 @param[in] Truncate If the buffer is large enough, this has no effect.
4055 If the buffer is is too small and Truncate is TRUE,
4056 the line will be truncated.
4057 If the buffer is is too small and Truncate is FALSE,
4058 then no read will occur.
jcarsey4d0a4fc2011-07-06 22:28:36 +00004059
ydong104ff7e372011-09-02 08:05:34 +00004060 @param[in, out] Ascii Boolean value for indicating whether the file is
4061 Ascii (TRUE) or UCS2 (FALSE).
jcarsey4d0a4fc2011-07-06 22:28:36 +00004062
4063 @retval EFI_SUCCESS The operation was successful. The line is stored in
4064 Buffer.
4065 @retval EFI_INVALID_PARAMETER Handle was NULL.
4066 @retval EFI_INVALID_PARAMETER Size was NULL.
4067 @retval EFI_BUFFER_TOO_SMALL Size was not large enough to store the line.
4068 Size was updated to the minimum space required.
4069**/
4070EFI_STATUS
4071EFIAPI
4072ShellFileHandleReadLine(
4073 IN SHELL_FILE_HANDLE Handle,
4074 IN OUT CHAR16 *Buffer,
4075 IN OUT UINTN *Size,
4076 IN BOOLEAN Truncate,
4077 IN OUT BOOLEAN *Ascii
4078 )
4079{
4080 EFI_STATUS Status;
4081 CHAR16 CharBuffer;
4082 UINTN CharSize;
4083 UINTN CountSoFar;
4084 UINT64 OriginalFilePosition;
4085
4086
4087 if (Handle == NULL
4088 ||Size == NULL
4089 ){
4090 return (EFI_INVALID_PARAMETER);
4091 }
4092 if (Buffer == NULL) {
4093 ASSERT(*Size == 0);
4094 } else {
4095 *Buffer = CHAR_NULL;
4096 }
4097 gEfiShellProtocol->GetFilePosition(Handle, &OriginalFilePosition);
4098 if (OriginalFilePosition == 0) {
4099 CharSize = sizeof(CHAR16);
4100 Status = gEfiShellProtocol->ReadFile(Handle, &CharSize, &CharBuffer);
4101 ASSERT_EFI_ERROR(Status);
4102 if (CharBuffer == gUnicodeFileTag) {
4103 *Ascii = FALSE;
4104 } else {
4105 *Ascii = TRUE;
4106 gEfiShellProtocol->SetFilePosition(Handle, OriginalFilePosition);
4107 }
4108 }
4109
4110 for (CountSoFar = 0;;CountSoFar++){
4111 CharBuffer = 0;
4112 if (*Ascii) {
4113 CharSize = sizeof(CHAR8);
4114 } else {
4115 CharSize = sizeof(CHAR16);
4116 }
4117 Status = gEfiShellProtocol->ReadFile(Handle, &CharSize, &CharBuffer);
4118 if ( EFI_ERROR(Status)
4119 || CharSize == 0
4120 || (CharBuffer == L'\n' && !(*Ascii))
4121 || (CharBuffer == '\n' && *Ascii)
4122 ){
4123 break;
4124 }
4125 //
4126 // if we have space save it...
4127 //
4128 if ((CountSoFar+1)*sizeof(CHAR16) < *Size){
4129 ASSERT(Buffer != NULL);
4130 ((CHAR16*)Buffer)[CountSoFar] = CharBuffer;
4131 ((CHAR16*)Buffer)[CountSoFar+1] = CHAR_NULL;
4132 }
4133 }
4134
4135 //
4136 // if we ran out of space tell when...
4137 //
4138 if ((CountSoFar+1)*sizeof(CHAR16) > *Size){
4139 *Size = (CountSoFar+1)*sizeof(CHAR16);
4140 if (!Truncate) {
4141 gEfiShellProtocol->SetFilePosition(Handle, OriginalFilePosition);
4142 } else {
4143 DEBUG((DEBUG_WARN, "The line was truncated in ShellFileHandleReadLine"));
4144 }
4145 return (EFI_BUFFER_TOO_SMALL);
4146 }
4147 while(Buffer[StrLen(Buffer)-1] == L'\r') {
4148 Buffer[StrLen(Buffer)-1] = CHAR_NULL;
4149 }
4150
4151 return (Status);
4152}
jcarseyfb5278e2013-02-20 18:21:14 +00004153
4154/**
jcarsey365aa982013-03-04 21:54:02 +00004155 Function to print help file / man page content in the spec from the UEFI Shell protocol GetHelpText function.
4156
4157 @param[in] CommandToGetHelpOn Pointer to a string containing the command name of help file to be printed.
4158 @param[in] SectionToGetHelpOn Pointer to the section specifier(s).
4159 @param[in] PrintCommandText If TRUE, prints the command followed by the help content, otherwise prints
4160 the help content only.
4161 @retval EFI_DEVICE_ERROR The help data format was incorrect.
4162 @retval EFI_NOT_FOUND The help data could not be found.
4163 @retval EFI_SUCCESS The operation was successful.
4164**/
4165EFI_STATUS
4166EFIAPI
4167ShellPrintHelp (
4168 IN CONST CHAR16 *CommandToGetHelpOn,
4169 IN CONST CHAR16 *SectionToGetHelpOn,
4170 IN BOOLEAN PrintCommandText
4171 )
4172{
4173 EFI_STATUS Status;
4174 CHAR16 *OutText;
4175
4176 OutText = NULL;
4177
4178 //
4179 // Get the string to print based
4180 //
4181 Status = gEfiShellProtocol->GetHelpText (CommandToGetHelpOn, SectionToGetHelpOn, &OutText);
4182
4183 //
4184 // make sure we got a valid string
4185 //
4186 if (EFI_ERROR(Status)){
4187 return Status;
4188 }
4189 if (OutText == NULL || StrLen(OutText) == 0) {
4190 return EFI_NOT_FOUND;
4191 }
4192
4193 //
4194 // Chop off trailing stuff we dont need
4195 //
4196 while (OutText[StrLen(OutText)-1] == L'\r' || OutText[StrLen(OutText)-1] == L'\n' || OutText[StrLen(OutText)-1] == L' ') {
4197 OutText[StrLen(OutText)-1] = CHAR_NULL;
4198 }
4199
4200 //
4201 // Print this out to the console
4202 //
4203 if (PrintCommandText) {
4204 ShellPrintEx(-1, -1, L"%H%-14s%N- %s\r\n", CommandToGetHelpOn, OutText);
4205 } else {
4206 ShellPrintEx(-1, -1, L"%N%s\r\n", OutText);
4207 }
4208
4209 SHELL_FREE_NON_NULL(OutText);
4210
4211 return EFI_SUCCESS;
4212}
4213
4214/**
jcarseyfb5278e2013-02-20 18:21:14 +00004215 Function to delete a file by name
4216
4217 @param[in] FileName Pointer to file name to delete.
4218
4219 @retval EFI_SUCCESS the file was deleted sucessfully
4220 @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not
4221 deleted
4222 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
4223 @retval EFI_NOT_FOUND The specified file could not be found on the
4224 device or the file system could not be found
4225 on the device.
4226 @retval EFI_NO_MEDIA The device has no medium.
4227 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
4228 medium is no longer supported.
4229 @retval EFI_DEVICE_ERROR The device reported an error.
4230 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
4231 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
4232 @retval EFI_ACCESS_DENIED The file was opened read only.
4233 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
4234 file.
4235 @retval other The file failed to open
4236**/
4237EFI_STATUS
4238EFIAPI
4239ShellDeleteFileByName(
4240 IN CONST CHAR16 *FileName
4241 )
4242{
4243 EFI_STATUS Status;
4244 SHELL_FILE_HANDLE FileHandle;
4245
4246 Status = ShellFileExists(FileName);
4247
4248 if (Status == EFI_SUCCESS){
4249 Status = ShellOpenFileByName(FileName, &FileHandle, EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE, 0x0);
4250 if (Status == EFI_SUCCESS){
4251 Status = ShellDeleteFile(&FileHandle);
4252 }
4253 }
4254
4255 return(Status);
4256
4257}
Qiu Shumin0960ba12014-09-17 07:58:31 +00004258
4259/**
4260 Cleans off all the quotes in the string.
4261
4262 @param[in] OriginalString pointer to the string to be cleaned.
4263 @param[out] CleanString The new string with all quotes removed.
4264 Memory allocated in the function and free
4265 by caller.
4266
4267 @retval EFI_SUCCESS The operation was successful.
4268**/
4269EFI_STATUS
4270EFIAPI
4271InternalShellStripQuotes (
4272 IN CONST CHAR16 *OriginalString,
4273 OUT CHAR16 **CleanString
4274 )
4275{
4276 CHAR16 *Walker;
4277
4278 if (OriginalString == NULL || CleanString == NULL) {
4279 return EFI_INVALID_PARAMETER;
4280 }
4281
4282 *CleanString = AllocateCopyPool (StrSize (OriginalString), OriginalString);
4283 if (*CleanString == NULL) {
4284 return EFI_OUT_OF_RESOURCES;
4285 }
4286
4287 for (Walker = *CleanString; Walker != NULL && *Walker != CHAR_NULL ; Walker++) {
4288 if (*Walker == L'\"') {
4289 CopyMem(Walker, Walker+1, StrSize(Walker) - sizeof(Walker[0]));
4290 }
4291 }
4292
4293 return EFI_SUCCESS;
4294}
4295