blob: 77cf5e77950065706729a6b5cd4cbffaed5d1216 [file] [log] [blame]
jcarsey94b17fa2009-05-07 18:46:18 +00001/** @file
2 Provides interface to shell functionality for shell commands and applications.
3
jcarsey252d9452011-03-25 20:49:53 +00004 Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>
jcarsey1e6e84c2010-01-25 20:05:08 +00005 This program and the accompanying materials
jcarseyb3011f42010-01-11 21:49:04 +00006 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
jcarsey94b17fa2009-05-07 18:46:18 +00009
jcarseyb3011f42010-01-11 21:49:04 +000010 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
jcarsey94b17fa2009-05-07 18:46:18 +000012
13**/
14
jcarseyb1f95a02009-06-16 00:23:19 +000015#include "UefiShellLib.h"
jcarseya405b862010-09-14 05:18:09 +000016#include <ShellBase.h>
jcarsey252d9452011-03-25 20:49:53 +000017#include <Library/SortLib.h>
jcarseyd2b45642009-05-11 18:02:16 +000018
jcarsey94b17fa2009-05-07 18:46:18 +000019#define FIND_XXXXX_FILE_BUFFER_SIZE (SIZE_OF_EFI_FILE_INFO + MAX_FILE_NAME_LEN)
20
jcarseyd2b45642009-05-11 18:02:16 +000021//
jcarseya405b862010-09-14 05:18:09 +000022// globals...
jcarseyd2b45642009-05-11 18:02:16 +000023//
24SHELL_PARAM_ITEM EmptyParamList[] = {
25 {NULL, TypeMax}
26 };
jcarseya405b862010-09-14 05:18:09 +000027SHELL_PARAM_ITEM SfoParamList[] = {
28 {L"-sfo", TypeFlag},
29 {NULL, TypeMax}
30 };
31EFI_SHELL_ENVIRONMENT2 *mEfiShellEnvironment2;
32EFI_SHELL_INTERFACE *mEfiShellInterface;
jcarsey366f81a2011-06-27 21:04:22 +000033EFI_SHELL_PROTOCOL *gEfiShellProtocol;
34EFI_SHELL_PARAMETERS_PROTOCOL *gEfiShellParametersProtocol;
jcarseya405b862010-09-14 05:18:09 +000035EFI_HANDLE mEfiShellEnvironment2Handle;
36FILE_HANDLE_FUNCTION_MAP FileFunctionMap;
jcarseyb3011f42010-01-11 21:49:04 +000037
jcarsey2247dde2009-11-09 18:08:58 +000038/**
39 Check if a Unicode character is a hexadecimal character.
40
jcarsey1e6e84c2010-01-25 20:05:08 +000041 This internal function checks if a Unicode character is a
jcarseya405b862010-09-14 05:18:09 +000042 numeric character. The valid hexadecimal characters are
jcarsey2247dde2009-11-09 18:08:58 +000043 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.
44
jcarsey2247dde2009-11-09 18:08:58 +000045 @param Char The character to check against.
46
47 @retval TRUE If the Char is a hexadecmial character.
48 @retval FALSE If the Char is not a hexadecmial character.
49
50**/
51BOOLEAN
52EFIAPI
jcarsey969c7832010-01-13 16:46:33 +000053ShellIsHexaDecimalDigitCharacter (
jcarsey2247dde2009-11-09 18:08:58 +000054 IN CHAR16 Char
jcarseya405b862010-09-14 05:18:09 +000055 )
56{
jcarsey2247dde2009-11-09 18:08:58 +000057 return (BOOLEAN) ((Char >= L'0' && Char <= L'9') || (Char >= L'A' && Char <= L'F') || (Char >= L'a' && Char <= L'f'));
58}
jcarsey94b17fa2009-05-07 18:46:18 +000059
60/**
jcarseya405b862010-09-14 05:18:09 +000061 Check if a Unicode character is a decimal character.
62
63 This internal function checks if a Unicode character is a
64 decimal character. The valid characters are
65 L'0' to L'9'.
66
67
68 @param Char The character to check against.
69
70 @retval TRUE If the Char is a hexadecmial character.
71 @retval FALSE If the Char is not a hexadecmial character.
72
73**/
74BOOLEAN
75EFIAPI
76ShellIsDecimalDigitCharacter (
77 IN CHAR16 Char
78 )
79{
80 return (BOOLEAN) (Char >= L'0' && Char <= L'9');
81}
82
83/**
84 Helper function to find ShellEnvironment2 for constructor.
85
86 @param[in] ImageHandle A copy of the calling image's handle.
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;
jcarsey94b17fa2009-05-07 18:46:18 +0000489
jcarsey92a54472011-06-27 20:33:13 +0000490 if (FilePath == NULL || FileHandle == NULL || DeviceHandle == NULL) {
491 return (EFI_INVALID_PARAMETER);
492 }
493
jcarsey1e6e84c2010-01-25 20:05:08 +0000494 //
jcarsey94b17fa2009-05-07 18:46:18 +0000495 // which shell interface should we use
496 //
jcarsey366f81a2011-06-27 21:04:22 +0000497 if (gEfiShellProtocol != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +0000498 //
499 // use UEFI Shell 2.0 method.
500 //
jcarsey366f81a2011-06-27 21:04:22 +0000501 FileName = gEfiShellProtocol->GetFilePathFromDevicePath(*FilePath);
jcarsey94b17fa2009-05-07 18:46:18 +0000502 if (FileName == NULL) {
503 return (EFI_INVALID_PARAMETER);
504 }
505 Status = ShellOpenFileByName(FileName, FileHandle, OpenMode, Attributes);
506 FreePool(FileName);
507 return (Status);
jcarsey1e6e84c2010-01-25 20:05:08 +0000508 }
jcarseyd2b45642009-05-11 18:02:16 +0000509
510
511 //
512 // use old shell method.
513 //
jcarsey1e6e84c2010-01-25 20:05:08 +0000514 Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid,
515 FilePath,
jcarseyd2b45642009-05-11 18:02:16 +0000516 DeviceHandle);
517 if (EFI_ERROR (Status)) {
518 return Status;
519 }
520 Status = gBS->OpenProtocol(*DeviceHandle,
521 &gEfiSimpleFileSystemProtocolGuid,
jcarseyb1f95a02009-06-16 00:23:19 +0000522 (VOID**)&EfiSimpleFileSystemProtocol,
jcarseyd2b45642009-05-11 18:02:16 +0000523 gImageHandle,
524 NULL,
525 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
526 if (EFI_ERROR (Status)) {
527 return Status;
528 }
jcarseya405b862010-09-14 05:18:09 +0000529 Status = EfiSimpleFileSystemProtocol->OpenVolume(EfiSimpleFileSystemProtocol, &Handle1);
jcarseyd2b45642009-05-11 18:02:16 +0000530 if (EFI_ERROR (Status)) {
531 FileHandle = NULL;
532 return Status;
533 }
534
535 //
536 // go down directories one node at a time.
537 //
538 while (!IsDevicePathEnd (*FilePath)) {
jcarsey94b17fa2009-05-07 18:46:18 +0000539 //
jcarseyd2b45642009-05-11 18:02:16 +0000540 // For file system access each node should be a file path component
jcarsey94b17fa2009-05-07 18:46:18 +0000541 //
jcarseyd2b45642009-05-11 18:02:16 +0000542 if (DevicePathType (*FilePath) != MEDIA_DEVICE_PATH ||
543 DevicePathSubType (*FilePath) != MEDIA_FILEPATH_DP
jcarseya405b862010-09-14 05:18:09 +0000544 ) {
jcarsey94b17fa2009-05-07 18:46:18 +0000545 FileHandle = NULL;
jcarseyd2b45642009-05-11 18:02:16 +0000546 return (EFI_INVALID_PARAMETER);
jcarsey94b17fa2009-05-07 18:46:18 +0000547 }
jcarseyd2b45642009-05-11 18:02:16 +0000548 //
549 // Open this file path node
550 //
jcarseya405b862010-09-14 05:18:09 +0000551 Handle2 = Handle1;
552 Handle1 = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +0000553
554 //
jcarseyd2b45642009-05-11 18:02:16 +0000555 // Try to test opening an existing file
jcarsey94b17fa2009-05-07 18:46:18 +0000556 //
jcarseya405b862010-09-14 05:18:09 +0000557 Status = Handle2->Open (
558 Handle2,
559 &Handle1,
jcarseyd2b45642009-05-11 18:02:16 +0000560 ((FILEPATH_DEVICE_PATH*)*FilePath)->PathName,
561 OpenMode &~EFI_FILE_MODE_CREATE,
562 0
jcarseya405b862010-09-14 05:18:09 +0000563 );
jcarsey94b17fa2009-05-07 18:46:18 +0000564
jcarseyd2b45642009-05-11 18:02:16 +0000565 //
566 // see if the error was that it needs to be created
567 //
568 if ((EFI_ERROR (Status)) && (OpenMode != (OpenMode &~EFI_FILE_MODE_CREATE))) {
jcarseya405b862010-09-14 05:18:09 +0000569 Status = Handle2->Open (
570 Handle2,
571 &Handle1,
jcarsey94b17fa2009-05-07 18:46:18 +0000572 ((FILEPATH_DEVICE_PATH*)*FilePath)->PathName,
jcarseyd2b45642009-05-11 18:02:16 +0000573 OpenMode,
574 Attributes
jcarseya405b862010-09-14 05:18:09 +0000575 );
jcarsey94b17fa2009-05-07 18:46:18 +0000576 }
jcarseyd2b45642009-05-11 18:02:16 +0000577 //
578 // Close the last node
579 //
jcarseya405b862010-09-14 05:18:09 +0000580 Handle2->Close (Handle2);
jcarseyd2b45642009-05-11 18:02:16 +0000581
582 if (EFI_ERROR(Status)) {
583 return (Status);
584 }
585
586 //
587 // Get the next node
588 //
589 *FilePath = NextDevicePathNode (*FilePath);
jcarsey94b17fa2009-05-07 18:46:18 +0000590 }
jcarseya405b862010-09-14 05:18:09 +0000591
592 //
593 // This is a weak spot since if the undefined SHELL_FILE_HANDLE format changes this must change also!
594 //
595 *FileHandle = (VOID*)Handle1;
jcarseyd2b45642009-05-11 18:02:16 +0000596 return (EFI_SUCCESS);
jcarsey94b17fa2009-05-07 18:46:18 +0000597}
598
599/**
600 This function will open a file or directory referenced by filename.
601
jcarsey1e6e84c2010-01-25 20:05:08 +0000602 If return is EFI_SUCCESS, the Filehandle is the opened file's handle;
603 otherwise, the Filehandle is NULL. The Attributes is valid only for
jcarsey94b17fa2009-05-07 18:46:18 +0000604 EFI_FILE_MODE_CREATE.
605
jcarsey92a54472011-06-27 20:33:13 +0000606 if FileName is NULL then ASSERT()
jcarsey94b17fa2009-05-07 18:46:18 +0000607
darylm503b0934ac2011-11-12 00:35:11 +0000608 @param FileName pointer to file name
609 @param FileHandle pointer to the file handle.
610 @param OpenMode the mode to open the file with.
611 @param Attributes the file's file attributes.
jcarsey94b17fa2009-05-07 18:46:18 +0000612
darylm503b0934ac2011-11-12 00:35:11 +0000613 @retval EFI_SUCCESS The information was set.
614 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
615 @retval EFI_UNSUPPORTED Could not open the file path.
616 @retval EFI_NOT_FOUND The specified file could not be found on the
jcarsey1e6e84c2010-01-25 20:05:08 +0000617 device or the file system could not be found
jcarsey94b17fa2009-05-07 18:46:18 +0000618 on the device.
darylm503b0934ac2011-11-12 00:35:11 +0000619 @retval EFI_NO_MEDIA The device has no medium.
620 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
jcarsey94b17fa2009-05-07 18:46:18 +0000621 medium is no longer supported.
darylm503b0934ac2011-11-12 00:35:11 +0000622 @retval EFI_DEVICE_ERROR The device reported an error.
623 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
624 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
625 @retval EFI_ACCESS_DENIED The file was opened read only.
626 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
jcarsey94b17fa2009-05-07 18:46:18 +0000627 file.
darylm503b0934ac2011-11-12 00:35:11 +0000628 @retval EFI_VOLUME_FULL The volume is full.
jcarsey94b17fa2009-05-07 18:46:18 +0000629**/
630EFI_STATUS
631EFIAPI
632ShellOpenFileByName(
darylm503b0934ac2011-11-12 00:35:11 +0000633 IN CONST CHAR16 *FileName,
jcarseya405b862010-09-14 05:18:09 +0000634 OUT SHELL_FILE_HANDLE *FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000635 IN UINT64 OpenMode,
darylm503b0934ac2011-11-12 00:35:11 +0000636 IN UINT64 Attributes
jcarseya405b862010-09-14 05:18:09 +0000637 )
638{
jcarsey94b17fa2009-05-07 18:46:18 +0000639 EFI_HANDLE DeviceHandle;
640 EFI_DEVICE_PATH_PROTOCOL *FilePath;
jcarseyb1f95a02009-06-16 00:23:19 +0000641 EFI_STATUS Status;
642 EFI_FILE_INFO *FileInfo;
jcarsey94b17fa2009-05-07 18:46:18 +0000643
644 //
645 // ASSERT if FileName is NULL
646 //
647 ASSERT(FileName != NULL);
648
jcarseya405b862010-09-14 05:18:09 +0000649 if (FileName == NULL) {
650 return (EFI_INVALID_PARAMETER);
651 }
652
jcarsey366f81a2011-06-27 21:04:22 +0000653 if (gEfiShellProtocol != NULL) {
jcarseya405b862010-09-14 05:18:09 +0000654 if ((OpenMode & EFI_FILE_MODE_CREATE) == EFI_FILE_MODE_CREATE && (Attributes & EFI_FILE_DIRECTORY) == EFI_FILE_DIRECTORY) {
655 return ShellCreateDirectory(FileName, FileHandle);
656 }
jcarsey94b17fa2009-05-07 18:46:18 +0000657 //
658 // Use UEFI Shell 2.0 method
659 //
jcarsey366f81a2011-06-27 21:04:22 +0000660 Status = gEfiShellProtocol->OpenFileByName(FileName,
jcarseyb1f95a02009-06-16 00:23:19 +0000661 FileHandle,
662 OpenMode);
jcarseya405b862010-09-14 05:18:09 +0000663 if (StrCmp(FileName, L"NUL") != 0 && !EFI_ERROR(Status) && ((OpenMode & EFI_FILE_MODE_CREATE) != 0)){
jcarsey2247dde2009-11-09 18:08:58 +0000664 FileInfo = FileFunctionMap.GetFileInfo(*FileHandle);
jcarseyb1f95a02009-06-16 00:23:19 +0000665 ASSERT(FileInfo != NULL);
666 FileInfo->Attribute = Attributes;
jcarsey2247dde2009-11-09 18:08:58 +0000667 Status = FileFunctionMap.SetFileInfo(*FileHandle, FileInfo);
668 FreePool(FileInfo);
jcarseyb1f95a02009-06-16 00:23:19 +0000669 }
670 return (Status);
jcarsey1e6e84c2010-01-25 20:05:08 +0000671 }
jcarsey94b17fa2009-05-07 18:46:18 +0000672 //
673 // Using EFI Shell version
674 // this means convert name to path and call that function
675 // since this will use EFI method again that will open it.
676 //
677 ASSERT(mEfiShellEnvironment2 != NULL);
jcarseyb82bfcc2009-06-29 16:28:23 +0000678 FilePath = mEfiShellEnvironment2->NameToPath ((CHAR16*)FileName);
xdu290bfa222010-07-19 05:21:27 +0000679 if (FilePath != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +0000680 return (ShellOpenFileByDevicePath(&FilePath,
681 &DeviceHandle,
682 FileHandle,
683 OpenMode,
jcarseya405b862010-09-14 05:18:09 +0000684 Attributes));
jcarsey94b17fa2009-05-07 18:46:18 +0000685 }
686 return (EFI_DEVICE_ERROR);
687}
688/**
689 This function create a directory
690
jcarsey1e6e84c2010-01-25 20:05:08 +0000691 If return is EFI_SUCCESS, the Filehandle is the opened directory's handle;
692 otherwise, the Filehandle is NULL. If the directory already existed, this
jcarsey94b17fa2009-05-07 18:46:18 +0000693 function opens the existing directory.
694
darylm503b0934ac2011-11-12 00:35:11 +0000695 @param DirectoryName pointer to directory name
696 @param FileHandle pointer to the file handle.
jcarsey94b17fa2009-05-07 18:46:18 +0000697
darylm503b0934ac2011-11-12 00:35:11 +0000698 @retval EFI_SUCCESS The information was set.
699 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
700 @retval EFI_UNSUPPORTED Could not open the file path.
701 @retval EFI_NOT_FOUND The specified file could not be found on the
jcarsey1e6e84c2010-01-25 20:05:08 +0000702 device or the file system could not be found
jcarsey94b17fa2009-05-07 18:46:18 +0000703 on the device.
darylm503b0934ac2011-11-12 00:35:11 +0000704 @retval EFI_NO_MEDIA The device has no medium.
705 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
jcarsey94b17fa2009-05-07 18:46:18 +0000706 medium is no longer supported.
darylm503b0934ac2011-11-12 00:35:11 +0000707 @retval EFI_DEVICE_ERROR The device reported an error.
708 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
709 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
710 @retval EFI_ACCESS_DENIED The file was opened read only.
711 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
jcarsey94b17fa2009-05-07 18:46:18 +0000712 file.
darylm503b0934ac2011-11-12 00:35:11 +0000713 @retval EFI_VOLUME_FULL The volume is full.
jcarsey94b17fa2009-05-07 18:46:18 +0000714 @sa ShellOpenFileByName
715**/
716EFI_STATUS
717EFIAPI
718ShellCreateDirectory(
jcarseyb82bfcc2009-06-29 16:28:23 +0000719 IN CONST CHAR16 *DirectoryName,
jcarseya405b862010-09-14 05:18:09 +0000720 OUT SHELL_FILE_HANDLE *FileHandle
721 )
722{
jcarsey366f81a2011-06-27 21:04:22 +0000723 if (gEfiShellProtocol != NULL) {
jcarsey2247dde2009-11-09 18:08:58 +0000724 //
725 // Use UEFI Shell 2.0 method
726 //
jcarsey366f81a2011-06-27 21:04:22 +0000727 return (gEfiShellProtocol->CreateFile(DirectoryName,
jcarsey2247dde2009-11-09 18:08:58 +0000728 EFI_FILE_DIRECTORY,
729 FileHandle
jcarseya405b862010-09-14 05:18:09 +0000730 ));
jcarsey2247dde2009-11-09 18:08:58 +0000731 } else {
732 return (ShellOpenFileByName(DirectoryName,
733 FileHandle,
734 EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE,
735 EFI_FILE_DIRECTORY
jcarseya405b862010-09-14 05:18:09 +0000736 ));
jcarsey2247dde2009-11-09 18:08:58 +0000737 }
jcarsey94b17fa2009-05-07 18:46:18 +0000738}
739
740/**
741 This function reads information from an opened file.
742
jcarsey1e6e84c2010-01-25 20:05:08 +0000743 If FileHandle is not a directory, the function reads the requested number of
744 bytes from the file at the file's current position and returns them in Buffer.
jcarsey94b17fa2009-05-07 18:46:18 +0000745 If the read goes beyond the end of the file, the read length is truncated to the
jcarsey1e6e84c2010-01-25 20:05:08 +0000746 end of the file. The file's current position is increased by the number of bytes
747 returned. If FileHandle is a directory, the function reads the directory entry
748 at the file's current position and returns the entry in Buffer. If the Buffer
749 is not large enough to hold the current directory entry, then
750 EFI_BUFFER_TOO_SMALL is returned and the current file position is not updated.
751 BufferSize is set to be the size of the buffer needed to read the entry. On
752 success, the current position is updated to the next directory entry. If there
753 are no more directory entries, the read returns a zero-length buffer.
jcarsey94b17fa2009-05-07 18:46:18 +0000754 EFI_FILE_INFO is the structure returned as the directory entry.
755
756 @param FileHandle the opened file handle
jcarsey1e6e84c2010-01-25 20:05:08 +0000757 @param BufferSize on input the size of buffer in bytes. on return
jcarsey94b17fa2009-05-07 18:46:18 +0000758 the number of bytes written.
759 @param Buffer the buffer to put read data into.
760
darylm503b0934ac2011-11-12 00:35:11 +0000761 @retval EFI_SUCCESS Data was read.
762 @retval EFI_NO_MEDIA The device has no media.
763 @retval EFI_DEVICE_ERROR The device reported an error.
764 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
765 @retval EFI_BUFFER_TO_SMALL Buffer is too small. ReadSize contains required
jcarsey94b17fa2009-05-07 18:46:18 +0000766 size.
767
768**/
769EFI_STATUS
770EFIAPI
771ShellReadFile(
jcarseya405b862010-09-14 05:18:09 +0000772 IN SHELL_FILE_HANDLE FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000773 IN OUT UINTN *BufferSize,
774 OUT VOID *Buffer
jcarseya405b862010-09-14 05:18:09 +0000775 )
776{
jcarseyd2b45642009-05-11 18:02:16 +0000777 return (FileFunctionMap.ReadFile(FileHandle, BufferSize, Buffer));
jcarsey94b17fa2009-05-07 18:46:18 +0000778}
779
780
781/**
782 Write data to a file.
783
jcarsey1e6e84c2010-01-25 20:05:08 +0000784 This function writes the specified number of bytes to the file at the current
785 file position. The current file position is advanced the actual number of bytes
786 written, which is returned in BufferSize. Partial writes only occur when there
787 has been a data error during the write attempt (such as "volume space full").
788 The file is automatically grown to hold the data if required. Direct writes to
jcarsey94b17fa2009-05-07 18:46:18 +0000789 opened directories are not supported.
790
791 @param FileHandle The opened file for writing
792 @param BufferSize on input the number of bytes in Buffer. On output
793 the number of bytes written.
794 @param Buffer the buffer containing data to write is stored.
795
darylm503b0934ac2011-11-12 00:35:11 +0000796 @retval EFI_SUCCESS Data was written.
797 @retval EFI_UNSUPPORTED Writes to an open directory are not supported.
798 @retval EFI_NO_MEDIA The device has no media.
799 @retval EFI_DEVICE_ERROR The device reported an error.
800 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
801 @retval EFI_WRITE_PROTECTED The device is write-protected.
802 @retval EFI_ACCESS_DENIED The file was open for read only.
803 @retval EFI_VOLUME_FULL The volume is full.
jcarsey94b17fa2009-05-07 18:46:18 +0000804**/
805EFI_STATUS
806EFIAPI
807ShellWriteFile(
jcarsey252d9452011-03-25 20:49:53 +0000808 IN SHELL_FILE_HANDLE FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000809 IN OUT UINTN *BufferSize,
810 IN VOID *Buffer
jcarseya405b862010-09-14 05:18:09 +0000811 )
812{
jcarseyd2b45642009-05-11 18:02:16 +0000813 return (FileFunctionMap.WriteFile(FileHandle, BufferSize, Buffer));
jcarsey94b17fa2009-05-07 18:46:18 +0000814}
815
jcarsey1e6e84c2010-01-25 20:05:08 +0000816/**
jcarsey94b17fa2009-05-07 18:46:18 +0000817 Close an open file handle.
818
jcarsey1e6e84c2010-01-25 20:05:08 +0000819 This function closes a specified file handle. All "dirty" cached file data is
820 flushed to the device, and the file is closed. In all cases the handle is
jcarsey94b17fa2009-05-07 18:46:18 +0000821 closed.
822
823@param FileHandle the file handle to close.
824
825@retval EFI_SUCCESS the file handle was closed sucessfully.
826**/
827EFI_STATUS
828EFIAPI
829ShellCloseFile (
jcarseya405b862010-09-14 05:18:09 +0000830 IN SHELL_FILE_HANDLE *FileHandle
831 )
832{
jcarseyd2b45642009-05-11 18:02:16 +0000833 return (FileFunctionMap.CloseFile(*FileHandle));
jcarsey94b17fa2009-05-07 18:46:18 +0000834}
835
836/**
837 Delete a file and close the handle
838
839 This function closes and deletes a file. In all cases the file handle is closed.
jcarsey1e6e84c2010-01-25 20:05:08 +0000840 If the file cannot be deleted, the warning code EFI_WARN_DELETE_FAILURE is
jcarsey94b17fa2009-05-07 18:46:18 +0000841 returned, but the handle is still closed.
842
843 @param FileHandle the file handle to delete
844
845 @retval EFI_SUCCESS the file was closed sucessfully
jcarsey1e6e84c2010-01-25 20:05:08 +0000846 @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not
jcarsey94b17fa2009-05-07 18:46:18 +0000847 deleted
darylm503b0934ac2011-11-12 00:35:11 +0000848 @retval INVALID_PARAMETER One of the parameters has an invalid value.
jcarsey94b17fa2009-05-07 18:46:18 +0000849**/
850EFI_STATUS
851EFIAPI
852ShellDeleteFile (
darylm503b0934ac2011-11-12 00:35:11 +0000853 IN SHELL_FILE_HANDLE *FileHandle
jcarseya405b862010-09-14 05:18:09 +0000854 )
855{
jcarseyd2b45642009-05-11 18:02:16 +0000856 return (FileFunctionMap.DeleteFile(*FileHandle));
jcarsey94b17fa2009-05-07 18:46:18 +0000857}
858
859/**
860 Set the current position in a file.
861
jcarsey1e6e84c2010-01-25 20:05:08 +0000862 This function sets the current file position for the handle to the position
jcarsey94b17fa2009-05-07 18:46:18 +0000863 supplied. With the exception of seeking to position 0xFFFFFFFFFFFFFFFF, only
jcarsey1e6e84c2010-01-25 20:05:08 +0000864 absolute positioning is supported, and seeking past the end of the file is
865 allowed (a subsequent write would grow the file). Seeking to position
jcarsey94b17fa2009-05-07 18:46:18 +0000866 0xFFFFFFFFFFFFFFFF causes the current position to be set to the end of the file.
jcarsey1e6e84c2010-01-25 20:05:08 +0000867 If FileHandle is a directory, the only position that may be set is zero. This
jcarsey94b17fa2009-05-07 18:46:18 +0000868 has the effect of starting the read process of the directory entries over.
869
870 @param FileHandle The file handle on which the position is being set
871 @param Position Byte position from begining of file
872
873 @retval EFI_SUCCESS Operation completed sucessfully.
jcarsey1e6e84c2010-01-25 20:05:08 +0000874 @retval EFI_UNSUPPORTED the seek request for non-zero is not valid on
jcarsey94b17fa2009-05-07 18:46:18 +0000875 directories.
876 @retval INVALID_PARAMETER One of the parameters has an invalid value.
877**/
878EFI_STATUS
879EFIAPI
880ShellSetFilePosition (
darylm503b0934ac2011-11-12 00:35:11 +0000881 IN SHELL_FILE_HANDLE FileHandle,
882 IN UINT64 Position
jcarseya405b862010-09-14 05:18:09 +0000883 )
884{
jcarseyd2b45642009-05-11 18:02:16 +0000885 return (FileFunctionMap.SetFilePosition(FileHandle, Position));
jcarsey94b17fa2009-05-07 18:46:18 +0000886}
887
jcarsey1e6e84c2010-01-25 20:05:08 +0000888/**
jcarsey94b17fa2009-05-07 18:46:18 +0000889 Gets a file's current position
890
jcarsey1e6e84c2010-01-25 20:05:08 +0000891 This function retrieves the current file position for the file handle. For
892 directories, the current file position has no meaning outside of the file
jcarsey94b17fa2009-05-07 18:46:18 +0000893 system driver and as such the operation is not supported. An error is returned
894 if FileHandle is a directory.
895
896 @param FileHandle The open file handle on which to get the position.
897 @param Position Byte position from begining of file.
898
899 @retval EFI_SUCCESS the operation completed sucessfully.
900 @retval INVALID_PARAMETER One of the parameters has an invalid value.
901 @retval EFI_UNSUPPORTED the request is not valid on directories.
902**/
903EFI_STATUS
904EFIAPI
905ShellGetFilePosition (
jcarseya405b862010-09-14 05:18:09 +0000906 IN SHELL_FILE_HANDLE FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000907 OUT UINT64 *Position
jcarseya405b862010-09-14 05:18:09 +0000908 )
909{
jcarseyd2b45642009-05-11 18:02:16 +0000910 return (FileFunctionMap.GetFilePosition(FileHandle, Position));
jcarsey94b17fa2009-05-07 18:46:18 +0000911}
912/**
913 Flushes data on a file
jcarsey1e6e84c2010-01-25 20:05:08 +0000914
jcarsey94b17fa2009-05-07 18:46:18 +0000915 This function flushes all modified data associated with a file to a device.
916
917 @param FileHandle The file handle on which to flush data
918
919 @retval EFI_SUCCESS The data was flushed.
920 @retval EFI_NO_MEDIA The device has no media.
921 @retval EFI_DEVICE_ERROR The device reported an error.
922 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
923 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
924 @retval EFI_ACCESS_DENIED The file was opened for read only.
925**/
926EFI_STATUS
927EFIAPI
928ShellFlushFile (
jcarseya405b862010-09-14 05:18:09 +0000929 IN SHELL_FILE_HANDLE FileHandle
930 )
931{
jcarseyd2b45642009-05-11 18:02:16 +0000932 return (FileFunctionMap.FlushFile(FileHandle));
jcarsey94b17fa2009-05-07 18:46:18 +0000933}
934
darylm503b0934ac2011-11-12 00:35:11 +0000935/** Retrieve first entry from a directory.
jcarsey94b17fa2009-05-07 18:46:18 +0000936
darylm503b0934ac2011-11-12 00:35:11 +0000937 This function takes an open directory handle and gets information from the
938 first entry in the directory. A buffer is allocated to contain
939 the information and a pointer to the buffer is returned in *Buffer. The
940 caller can use ShellFindNextFile() to get subsequent directory entries.
jcarsey94b17fa2009-05-07 18:46:18 +0000941
darylm503b0934ac2011-11-12 00:35:11 +0000942 The buffer will be freed by ShellFindNextFile() when the last directory
943 entry is read. Otherwise, the caller must free the buffer, using FreePool,
944 when finished with it.
945
946 @param[in] DirHandle The file handle of the directory to search.
947 @param[out] Buffer The pointer to the buffer for the file's information.
jcarsey94b17fa2009-05-07 18:46:18 +0000948
949 @retval EFI_SUCCESS Found the first file.
950 @retval EFI_NOT_FOUND Cannot find the directory.
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 @return Others status of ShellGetFileInfo, ShellSetFilePosition,
955 or ShellReadFile
956**/
957EFI_STATUS
958EFIAPI
959ShellFindFirstFile (
jcarseya405b862010-09-14 05:18:09 +0000960 IN SHELL_FILE_HANDLE DirHandle,
jcarseyd2b45642009-05-11 18:02:16 +0000961 OUT EFI_FILE_INFO **Buffer
jcarseya405b862010-09-14 05:18:09 +0000962 )
963{
jcarsey94b17fa2009-05-07 18:46:18 +0000964 //
jcarseyd2b45642009-05-11 18:02:16 +0000965 // pass to file handle lib
jcarsey94b17fa2009-05-07 18:46:18 +0000966 //
jcarseyd2b45642009-05-11 18:02:16 +0000967 return (FileHandleFindFirstFile(DirHandle, Buffer));
jcarsey94b17fa2009-05-07 18:46:18 +0000968}
darylm503b0934ac2011-11-12 00:35:11 +0000969/** Retrieve next entries from a directory.
jcarsey94b17fa2009-05-07 18:46:18 +0000970
darylm503b0934ac2011-11-12 00:35:11 +0000971 To use this function, the caller must first call the ShellFindFirstFile()
972 function to get the first directory entry. Subsequent directory entries are
973 retrieved by using the ShellFindNextFile() function. This function can
974 be called several times to get each entry from the directory. If the call of
975 ShellFindNextFile() retrieved the last directory entry, the next call of
976 this function will set *NoFile to TRUE and free the buffer.
jcarsey94b17fa2009-05-07 18:46:18 +0000977
darylm503b0934ac2011-11-12 00:35:11 +0000978 @param[in] DirHandle The file handle of the directory.
979 @param[out] Buffer The pointer to buffer for file's information.
980 @param[out] NoFile The pointer to boolean when last file is found.
jcarsey94b17fa2009-05-07 18:46:18 +0000981
982 @retval EFI_SUCCESS Found the next file, or reached last file
983 @retval EFI_NO_MEDIA The device has no media.
984 @retval EFI_DEVICE_ERROR The device reported an error.
985 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
986**/
987EFI_STATUS
988EFIAPI
989ShellFindNextFile(
jcarseya405b862010-09-14 05:18:09 +0000990 IN SHELL_FILE_HANDLE DirHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000991 OUT EFI_FILE_INFO *Buffer,
992 OUT BOOLEAN *NoFile
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 (FileHandleFindNextFile(DirHandle, Buffer, NoFile));
jcarsey94b17fa2009-05-07 18:46:18 +0000999}
1000/**
1001 Retrieve the size of a file.
1002
1003 if FileHandle is NULL then ASSERT()
1004 if Size is NULL then ASSERT()
1005
jcarsey1e6e84c2010-01-25 20:05:08 +00001006 This function extracts the file size info from the FileHandle's EFI_FILE_INFO
jcarsey94b17fa2009-05-07 18:46:18 +00001007 data.
1008
1009 @param FileHandle file handle from which size is retrieved
1010 @param Size pointer to size
1011
1012 @retval EFI_SUCCESS operation was completed sucessfully
1013 @retval EFI_DEVICE_ERROR cannot access the file
1014**/
1015EFI_STATUS
1016EFIAPI
1017ShellGetFileSize (
jcarseya405b862010-09-14 05:18:09 +00001018 IN SHELL_FILE_HANDLE FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +00001019 OUT UINT64 *Size
jcarseya405b862010-09-14 05:18:09 +00001020 )
1021{
jcarseyd2b45642009-05-11 18:02:16 +00001022 return (FileFunctionMap.GetFileSize(FileHandle, Size));
jcarsey94b17fa2009-05-07 18:46:18 +00001023}
1024/**
1025 Retrieves the status of the break execution flag
1026
1027 this function is useful to check whether the application is being asked to halt by the shell.
1028
1029 @retval TRUE the execution break is enabled
1030 @retval FALSE the execution break is not enabled
1031**/
1032BOOLEAN
1033EFIAPI
1034ShellGetExecutionBreakFlag(
1035 VOID
1036 )
1037{
jcarsey1e6e84c2010-01-25 20:05:08 +00001038 //
jcarsey94b17fa2009-05-07 18:46:18 +00001039 // Check for UEFI Shell 2.0 protocols
1040 //
jcarsey366f81a2011-06-27 21:04:22 +00001041 if (gEfiShellProtocol != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00001042
1043 //
1044 // We are using UEFI Shell 2.0; see if the event has been triggered
1045 //
jcarsey366f81a2011-06-27 21:04:22 +00001046 if (gBS->CheckEvent(gEfiShellProtocol->ExecutionBreak) != EFI_SUCCESS) {
jcarsey94b17fa2009-05-07 18:46:18 +00001047 return (FALSE);
1048 }
1049 return (TRUE);
jcarsey1e6e84c2010-01-25 20:05:08 +00001050 }
jcarsey94b17fa2009-05-07 18:46:18 +00001051
1052 //
1053 // using EFI Shell; call the function to check
1054 //
jcarsey92a54472011-06-27 20:33:13 +00001055 if (mEfiShellEnvironment2 != NULL) {
1056 return (mEfiShellEnvironment2->GetExecutionBreak());
1057 }
1058
1059 return (FALSE);
jcarsey94b17fa2009-05-07 18:46:18 +00001060}
1061/**
1062 return the value of an environment variable
1063
jcarsey1e6e84c2010-01-25 20:05:08 +00001064 this function gets the value of the environment variable set by the
jcarsey94b17fa2009-05-07 18:46:18 +00001065 ShellSetEnvironmentVariable function
1066
1067 @param EnvKey The key name of the environment variable.
1068
1069 @retval NULL the named environment variable does not exist.
1070 @return != NULL pointer to the value of the environment variable
1071**/
1072CONST CHAR16*
1073EFIAPI
1074ShellGetEnvironmentVariable (
jcarsey9b3bf082009-06-23 21:15:07 +00001075 IN CONST CHAR16 *EnvKey
jcarsey94b17fa2009-05-07 18:46:18 +00001076 )
1077{
jcarsey1e6e84c2010-01-25 20:05:08 +00001078 //
jcarsey94b17fa2009-05-07 18:46:18 +00001079 // Check for UEFI Shell 2.0 protocols
1080 //
jcarsey366f81a2011-06-27 21:04:22 +00001081 if (gEfiShellProtocol != NULL) {
1082 return (gEfiShellProtocol->GetEnv(EnvKey));
jcarsey94b17fa2009-05-07 18:46:18 +00001083 }
1084
1085 //
jcarsey92a54472011-06-27 20:33:13 +00001086 // Check for EFI shell
jcarsey94b17fa2009-05-07 18:46:18 +00001087 //
jcarsey92a54472011-06-27 20:33:13 +00001088 if (mEfiShellEnvironment2 != NULL) {
1089 return (mEfiShellEnvironment2->GetEnv((CHAR16*)EnvKey));
1090 }
jcarsey94b17fa2009-05-07 18:46:18 +00001091
jcarsey92a54472011-06-27 20:33:13 +00001092 return NULL;
jcarsey94b17fa2009-05-07 18:46:18 +00001093}
1094/**
1095 set the value of an environment variable
1096
1097This function changes the current value of the specified environment variable. If the
1098environment variable exists and the Value is an empty string, then the environment
1099variable is deleted. If the environment variable exists and the Value is not an empty
1100string, then the value of the environment variable is changed. If the environment
1101variable does not exist and the Value is an empty string, there is no action. If the
1102environment variable does not exist and the Value is a non-empty string, then the
1103environment variable is created and assigned the specified value.
1104
1105 This is not supported pre-UEFI Shell 2.0.
1106
1107 @param EnvKey The key name of the environment variable.
1108 @param EnvVal The Value of the environment variable
1109 @param Volatile Indicates whether the variable is non-volatile (FALSE) or volatile (TRUE).
1110
1111 @retval EFI_SUCCESS the operation was completed sucessfully
1112 @retval EFI_UNSUPPORTED This operation is not allowed in pre UEFI 2.0 Shell environments
1113**/
1114EFI_STATUS
1115EFIAPI
1116ShellSetEnvironmentVariable (
1117 IN CONST CHAR16 *EnvKey,
1118 IN CONST CHAR16 *EnvVal,
1119 IN BOOLEAN Volatile
1120 )
1121{
jcarsey1e6e84c2010-01-25 20:05:08 +00001122 //
jcarsey94b17fa2009-05-07 18:46:18 +00001123 // Check for UEFI Shell 2.0 protocols
1124 //
jcarsey366f81a2011-06-27 21:04:22 +00001125 if (gEfiShellProtocol != NULL) {
1126 return (gEfiShellProtocol->SetEnv(EnvKey, EnvVal, Volatile));
jcarsey1e6e84c2010-01-25 20:05:08 +00001127 }
jcarsey94b17fa2009-05-07 18:46:18 +00001128
1129 //
1130 // This feature does not exist under EFI shell
1131 //
1132 return (EFI_UNSUPPORTED);
1133}
jcarseya405b862010-09-14 05:18:09 +00001134
jcarsey94b17fa2009-05-07 18:46:18 +00001135/**
jcarseya405b862010-09-14 05:18:09 +00001136 Cause the shell to parse and execute a command line.
jcarsey94b17fa2009-05-07 18:46:18 +00001137
1138 This function creates a nested instance of the shell and executes the specified
jcarseya405b862010-09-14 05:18:09 +00001139 command (CommandLine) with the specified environment (Environment). Upon return,
1140 the status code returned by the specified command is placed in StatusCode.
1141 If Environment is NULL, then the current environment is used and all changes made
1142 by the commands executed will be reflected in the current environment. If the
1143 Environment is non-NULL, then the changes made will be discarded.
1144 The CommandLine is executed from the current working directory on the current
1145 device.
jcarsey94b17fa2009-05-07 18:46:18 +00001146
jcarseya405b862010-09-14 05:18:09 +00001147 The EnvironmentVariables and Status parameters are ignored in a pre-UEFI Shell 2.0
1148 environment. The values pointed to by the parameters will be unchanged by the
1149 ShellExecute() function. The Output parameter has no effect in a
1150 UEFI Shell 2.0 environment.
jcarsey94b17fa2009-05-07 18:46:18 +00001151
jcarseya405b862010-09-14 05:18:09 +00001152 @param[in] ParentHandle The parent image starting the operation.
1153 @param[in] CommandLine The pointer to a NULL terminated command line.
1154 @param[in] Output True to display debug output. False to hide it.
1155 @param[in] EnvironmentVariables Optional pointer to array of environment variables
1156 in the form "x=y". If NULL, the current set is used.
1157 @param[out] Status The status of the run command line.
jcarsey94b17fa2009-05-07 18:46:18 +00001158
jcarseya405b862010-09-14 05:18:09 +00001159 @retval EFI_SUCCESS The operation completed sucessfully. Status
1160 contains the status code returned.
1161 @retval EFI_INVALID_PARAMETER A parameter contains an invalid value.
1162 @retval EFI_OUT_OF_RESOURCES Out of resources.
1163 @retval EFI_UNSUPPORTED The operation is not allowed.
jcarsey94b17fa2009-05-07 18:46:18 +00001164**/
1165EFI_STATUS
1166EFIAPI
1167ShellExecute (
1168 IN EFI_HANDLE *ParentHandle,
1169 IN CHAR16 *CommandLine OPTIONAL,
1170 IN BOOLEAN Output OPTIONAL,
1171 IN CHAR16 **EnvironmentVariables OPTIONAL,
1172 OUT EFI_STATUS *Status OPTIONAL
1173 )
1174{
jcarsey1e6e84c2010-01-25 20:05:08 +00001175 //
jcarsey94b17fa2009-05-07 18:46:18 +00001176 // Check for UEFI Shell 2.0 protocols
1177 //
jcarsey366f81a2011-06-27 21:04:22 +00001178 if (gEfiShellProtocol != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00001179 //
1180 // Call UEFI Shell 2.0 version (not using Output parameter)
1181 //
jcarsey366f81a2011-06-27 21:04:22 +00001182 return (gEfiShellProtocol->Execute(ParentHandle,
jcarsey94b17fa2009-05-07 18:46:18 +00001183 CommandLine,
1184 EnvironmentVariables,
1185 Status));
jcarsey1e6e84c2010-01-25 20:05:08 +00001186 }
jcarsey92a54472011-06-27 20:33:13 +00001187
jcarsey94b17fa2009-05-07 18:46:18 +00001188 //
jcarsey92a54472011-06-27 20:33:13 +00001189 // Check for EFI shell
jcarsey94b17fa2009-05-07 18:46:18 +00001190 //
jcarsey92a54472011-06-27 20:33:13 +00001191 if (mEfiShellEnvironment2 != NULL) {
1192 //
1193 // Call EFI Shell version (not using EnvironmentVariables or Status parameters)
1194 // Due to oddity in the EFI shell we want to dereference the ParentHandle here
1195 //
1196 return (mEfiShellEnvironment2->Execute(*ParentHandle,
1197 CommandLine,
1198 Output));
1199 }
1200
1201 return (EFI_UNSUPPORTED);
jcarsey94b17fa2009-05-07 18:46:18 +00001202}
1203/**
1204 Retreives the current directory path
1205
jcarsey1e6e84c2010-01-25 20:05:08 +00001206 If the DeviceName is NULL, it returns the current device's current directory
1207 name. If the DeviceName is not NULL, it returns the current directory name
jcarsey94b17fa2009-05-07 18:46:18 +00001208 on specified drive.
1209
1210 @param DeviceName the name of the drive to get directory on
1211
1212 @retval NULL the directory does not exist
1213 @return != NULL the directory
1214**/
1215CONST CHAR16*
1216EFIAPI
1217ShellGetCurrentDir (
jcarseya405b862010-09-14 05:18:09 +00001218 IN CHAR16 * CONST DeviceName OPTIONAL
jcarsey94b17fa2009-05-07 18:46:18 +00001219 )
1220{
jcarsey1e6e84c2010-01-25 20:05:08 +00001221 //
jcarsey94b17fa2009-05-07 18:46:18 +00001222 // Check for UEFI Shell 2.0 protocols
1223 //
jcarsey366f81a2011-06-27 21:04:22 +00001224 if (gEfiShellProtocol != NULL) {
1225 return (gEfiShellProtocol->GetCurDir(DeviceName));
jcarsey1e6e84c2010-01-25 20:05:08 +00001226 }
jcarsey92a54472011-06-27 20:33:13 +00001227
jcarsey94b17fa2009-05-07 18:46:18 +00001228 //
jcarsey8bd282b2011-06-27 16:45:41 +00001229 // Check for EFI shell
jcarsey94b17fa2009-05-07 18:46:18 +00001230 //
jcarsey8bd282b2011-06-27 16:45:41 +00001231 if (mEfiShellEnvironment2 != NULL) {
1232 return (mEfiShellEnvironment2->CurDir(DeviceName));
1233 }
1234
1235 return (NULL);
jcarsey94b17fa2009-05-07 18:46:18 +00001236}
1237/**
1238 sets (enabled or disabled) the page break mode
1239
jcarsey1e6e84c2010-01-25 20:05:08 +00001240 when page break mode is enabled the screen will stop scrolling
jcarsey94b17fa2009-05-07 18:46:18 +00001241 and wait for operator input before scrolling a subsequent screen.
1242
1243 @param CurrentState TRUE to enable and FALSE to disable
1244**/
jcarsey1e6e84c2010-01-25 20:05:08 +00001245VOID
jcarsey94b17fa2009-05-07 18:46:18 +00001246EFIAPI
1247ShellSetPageBreakMode (
1248 IN BOOLEAN CurrentState
1249 )
1250{
1251 //
1252 // check for enabling
1253 //
1254 if (CurrentState != 0x00) {
jcarsey1e6e84c2010-01-25 20:05:08 +00001255 //
jcarsey94b17fa2009-05-07 18:46:18 +00001256 // check for UEFI Shell 2.0
1257 //
jcarsey366f81a2011-06-27 21:04:22 +00001258 if (gEfiShellProtocol != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00001259 //
1260 // Enable with UEFI 2.0 Shell
1261 //
jcarsey366f81a2011-06-27 21:04:22 +00001262 gEfiShellProtocol->EnablePageBreak();
jcarsey94b17fa2009-05-07 18:46:18 +00001263 return;
1264 } else {
jcarsey1e6e84c2010-01-25 20:05:08 +00001265 //
jcarsey92a54472011-06-27 20:33:13 +00001266 // Check for EFI shell
jcarsey94b17fa2009-05-07 18:46:18 +00001267 //
jcarsey92a54472011-06-27 20:33:13 +00001268 if (mEfiShellEnvironment2 != NULL) {
1269 //
1270 // Enable with EFI Shell
1271 //
1272 mEfiShellEnvironment2->EnablePageBreak (DEFAULT_INIT_ROW, DEFAULT_AUTO_LF);
1273 return;
1274 }
jcarsey94b17fa2009-05-07 18:46:18 +00001275 }
1276 } else {
jcarsey1e6e84c2010-01-25 20:05:08 +00001277 //
jcarsey94b17fa2009-05-07 18:46:18 +00001278 // check for UEFI Shell 2.0
1279 //
jcarsey366f81a2011-06-27 21:04:22 +00001280 if (gEfiShellProtocol != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00001281 //
1282 // Disable with UEFI 2.0 Shell
1283 //
jcarsey366f81a2011-06-27 21:04:22 +00001284 gEfiShellProtocol->DisablePageBreak();
jcarsey94b17fa2009-05-07 18:46:18 +00001285 return;
1286 } else {
jcarsey1e6e84c2010-01-25 20:05:08 +00001287 //
jcarsey92a54472011-06-27 20:33:13 +00001288 // Check for EFI shell
jcarsey94b17fa2009-05-07 18:46:18 +00001289 //
jcarsey92a54472011-06-27 20:33:13 +00001290 if (mEfiShellEnvironment2 != NULL) {
1291 //
1292 // Disable with EFI Shell
1293 //
1294 mEfiShellEnvironment2->DisablePageBreak ();
1295 return;
1296 }
jcarsey94b17fa2009-05-07 18:46:18 +00001297 }
1298 }
1299}
1300
1301///
1302/// version of EFI_SHELL_FILE_INFO struct, except has no CONST pointers.
1303/// This allows for the struct to be populated.
1304///
1305typedef struct {
jcarseyd2b45642009-05-11 18:02:16 +00001306 LIST_ENTRY Link;
jcarsey94b17fa2009-05-07 18:46:18 +00001307 EFI_STATUS Status;
1308 CHAR16 *FullName;
1309 CHAR16 *FileName;
jcarseya405b862010-09-14 05:18:09 +00001310 SHELL_FILE_HANDLE Handle;
jcarsey94b17fa2009-05-07 18:46:18 +00001311 EFI_FILE_INFO *Info;
1312} EFI_SHELL_FILE_INFO_NO_CONST;
1313
1314/**
1315 Converts a EFI shell list of structures to the coresponding UEFI Shell 2.0 type of list.
1316
1317 if OldStyleFileList is NULL then ASSERT()
1318
jcarsey1e6e84c2010-01-25 20:05:08 +00001319 this function will convert a SHELL_FILE_ARG based list into a callee allocated
jcarsey94b17fa2009-05-07 18:46:18 +00001320 EFI_SHELL_FILE_INFO based list. it is up to the caller to free the memory via
1321 the ShellCloseFileMetaArg function.
1322
ydong104ff7e372011-09-02 08:05:34 +00001323 @param[in] FileList the EFI shell list type
1324 @param[in, out] ListHead the list to add to
jcarsey94b17fa2009-05-07 18:46:18 +00001325
1326 @retval the resultant head of the double linked new format list;
1327**/
1328LIST_ENTRY*
1329EFIAPI
1330InternalShellConvertFileListType (
jcarsey9b3bf082009-06-23 21:15:07 +00001331 IN LIST_ENTRY *FileList,
1332 IN OUT LIST_ENTRY *ListHead
jcarsey125c2cf2009-11-18 21:36:50 +00001333 )
1334{
jcarsey94b17fa2009-05-07 18:46:18 +00001335 SHELL_FILE_ARG *OldInfo;
jcarsey9b3bf082009-06-23 21:15:07 +00001336 LIST_ENTRY *Link;
jcarsey94b17fa2009-05-07 18:46:18 +00001337 EFI_SHELL_FILE_INFO_NO_CONST *NewInfo;
1338
1339 //
jcarsey9b3bf082009-06-23 21:15:07 +00001340 // ASSERTs
jcarsey94b17fa2009-05-07 18:46:18 +00001341 //
jcarsey9b3bf082009-06-23 21:15:07 +00001342 ASSERT(FileList != NULL);
1343 ASSERT(ListHead != NULL);
jcarsey94b17fa2009-05-07 18:46:18 +00001344
1345 //
1346 // enumerate through each member of the old list and copy
1347 //
jcarseyd2b45642009-05-11 18:02:16 +00001348 for (Link = FileList->ForwardLink; Link != FileList; Link = Link->ForwardLink) {
jcarsey94b17fa2009-05-07 18:46:18 +00001349 OldInfo = CR (Link, SHELL_FILE_ARG, Link, SHELL_FILE_ARG_SIGNATURE);
jcarseya405b862010-09-14 05:18:09 +00001350 ASSERT(OldInfo != NULL);
1351
1352 //
1353 // Skip ones that failed to open...
1354 //
1355 if (OldInfo->Status != EFI_SUCCESS) {
1356 continue;
1357 }
jcarsey94b17fa2009-05-07 18:46:18 +00001358
1359 //
1360 // make sure the old list was valid
1361 //
jcarsey94b17fa2009-05-07 18:46:18 +00001362 ASSERT(OldInfo->Info != NULL);
1363 ASSERT(OldInfo->FullName != NULL);
1364 ASSERT(OldInfo->FileName != NULL);
1365
1366 //
1367 // allocate a new EFI_SHELL_FILE_INFO object
1368 //
1369 NewInfo = AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));
jcarseyc9d92df2010-02-03 15:37:54 +00001370 if (NewInfo == NULL) {
jcarsey7a95efd2011-10-13 16:08:18 +00001371 ShellCloseFileMetaArg((EFI_SHELL_FILE_INFO**)(&ListHead));
jcarseybeab0fc2011-10-10 17:26:25 +00001372 ListHead = NULL;
jcarseyc9d92df2010-02-03 15:37:54 +00001373 break;
1374 }
jcarsey1e6e84c2010-01-25 20:05:08 +00001375
1376 //
jcarsey94b17fa2009-05-07 18:46:18 +00001377 // copy the simple items
1378 //
1379 NewInfo->Handle = OldInfo->Handle;
1380 NewInfo->Status = OldInfo->Status;
1381
jcarseyd2b45642009-05-11 18:02:16 +00001382 // old shell checks for 0 not NULL
1383 OldInfo->Handle = 0;
1384
jcarsey94b17fa2009-05-07 18:46:18 +00001385 //
1386 // allocate new space to copy strings and structure
1387 //
1388 NewInfo->FullName = AllocateZeroPool(StrSize(OldInfo->FullName));
1389 NewInfo->FileName = AllocateZeroPool(StrSize(OldInfo->FileName));
1390 NewInfo->Info = AllocateZeroPool((UINTN)OldInfo->Info->Size);
jcarsey1e6e84c2010-01-25 20:05:08 +00001391
jcarsey94b17fa2009-05-07 18:46:18 +00001392 //
1393 // make sure all the memory allocations were sucessful
1394 //
jcarseybeab0fc2011-10-10 17:26:25 +00001395 if (NULL == NewInfo->FullName || NewInfo->FileName == NULL || NewInfo->Info == NULL) {
jcarsey7a95efd2011-10-13 16:08:18 +00001396 ShellCloseFileMetaArg((EFI_SHELL_FILE_INFO**)(&ListHead));
jcarseybeab0fc2011-10-10 17:26:25 +00001397 ListHead = NULL;
1398 break;
1399 }
jcarsey94b17fa2009-05-07 18:46:18 +00001400
1401 //
1402 // Copt the strings and structure
1403 //
1404 StrCpy(NewInfo->FullName, OldInfo->FullName);
1405 StrCpy(NewInfo->FileName, OldInfo->FileName);
1406 gBS->CopyMem (NewInfo->Info, OldInfo->Info, (UINTN)OldInfo->Info->Size);
1407
1408 //
1409 // add that to the list
1410 //
jcarsey9b3bf082009-06-23 21:15:07 +00001411 InsertTailList(ListHead, &NewInfo->Link);
jcarsey94b17fa2009-05-07 18:46:18 +00001412 }
1413 return (ListHead);
1414}
1415/**
1416 Opens a group of files based on a path.
1417
jcarsey1e6e84c2010-01-25 20:05:08 +00001418 This function uses the Arg to open all the matching files. Each matched
1419 file has a SHELL_FILE_ARG structure to record the file information. These
1420 structures are placed on the list ListHead. Users can get the SHELL_FILE_ARG
jcarsey94b17fa2009-05-07 18:46:18 +00001421 structures from ListHead to access each file. This function supports wildcards
jcarsey1e6e84c2010-01-25 20:05:08 +00001422 and will process '?' and '*' as such. the list must be freed with a call to
jcarsey94b17fa2009-05-07 18:46:18 +00001423 ShellCloseFileMetaArg().
1424
jcarsey1e6e84c2010-01-25 20:05:08 +00001425 If you are NOT appending to an existing list *ListHead must be NULL. If
jcarsey5f7431d2009-07-10 18:06:01 +00001426 *ListHead is NULL then it must be callee freed.
jcarsey94b17fa2009-05-07 18:46:18 +00001427
1428 @param Arg pointer to path string
1429 @param OpenMode mode to open files with
1430 @param ListHead head of linked list of results
1431
jcarsey1e6e84c2010-01-25 20:05:08 +00001432 @retval EFI_SUCCESS the operation was sucessful and the list head
jcarsey94b17fa2009-05-07 18:46:18 +00001433 contains the list of opened files
jcarsey94b17fa2009-05-07 18:46:18 +00001434 @return != EFI_SUCCESS the operation failed
1435
1436 @sa InternalShellConvertFileListType
1437**/
1438EFI_STATUS
1439EFIAPI
1440ShellOpenFileMetaArg (
1441 IN CHAR16 *Arg,
1442 IN UINT64 OpenMode,
1443 IN OUT EFI_SHELL_FILE_INFO **ListHead
1444 )
1445{
1446 EFI_STATUS Status;
jcarsey9b3bf082009-06-23 21:15:07 +00001447 LIST_ENTRY mOldStyleFileList;
jcarsey1e6e84c2010-01-25 20:05:08 +00001448
jcarsey94b17fa2009-05-07 18:46:18 +00001449 //
1450 // ASSERT that Arg and ListHead are not NULL
1451 //
1452 ASSERT(Arg != NULL);
1453 ASSERT(ListHead != NULL);
1454
jcarsey1e6e84c2010-01-25 20:05:08 +00001455 //
jcarsey94b17fa2009-05-07 18:46:18 +00001456 // Check for UEFI Shell 2.0 protocols
1457 //
jcarsey366f81a2011-06-27 21:04:22 +00001458 if (gEfiShellProtocol != NULL) {
jcarsey5f7431d2009-07-10 18:06:01 +00001459 if (*ListHead == NULL) {
1460 *ListHead = (EFI_SHELL_FILE_INFO*)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));
1461 if (*ListHead == NULL) {
1462 return (EFI_OUT_OF_RESOURCES);
1463 }
1464 InitializeListHead(&((*ListHead)->Link));
jcarsey1e6e84c2010-01-25 20:05:08 +00001465 }
jcarsey366f81a2011-06-27 21:04:22 +00001466 Status = gEfiShellProtocol->OpenFileList(Arg,
jcarsey1e6e84c2010-01-25 20:05:08 +00001467 OpenMode,
jcarsey2247dde2009-11-09 18:08:58 +00001468 ListHead);
1469 if (EFI_ERROR(Status)) {
jcarsey366f81a2011-06-27 21:04:22 +00001470 gEfiShellProtocol->RemoveDupInFileList(ListHead);
jcarsey2247dde2009-11-09 18:08:58 +00001471 } else {
jcarsey366f81a2011-06-27 21:04:22 +00001472 Status = gEfiShellProtocol->RemoveDupInFileList(ListHead);
jcarsey2247dde2009-11-09 18:08:58 +00001473 }
jcarseya405b862010-09-14 05:18:09 +00001474 if (*ListHead != NULL && IsListEmpty(&(*ListHead)->Link)) {
1475 FreePool(*ListHead);
1476 *ListHead = NULL;
1477 return (EFI_NOT_FOUND);
1478 }
jcarsey2247dde2009-11-09 18:08:58 +00001479 return (Status);
jcarsey1e6e84c2010-01-25 20:05:08 +00001480 }
jcarsey94b17fa2009-05-07 18:46:18 +00001481
1482 //
jcarsey92a54472011-06-27 20:33:13 +00001483 // Check for EFI shell
jcarsey94b17fa2009-05-07 18:46:18 +00001484 //
jcarsey92a54472011-06-27 20:33:13 +00001485 if (mEfiShellEnvironment2 != NULL) {
1486 //
1487 // make sure the list head is initialized
1488 //
1489 InitializeListHead(&mOldStyleFileList);
jcarsey94b17fa2009-05-07 18:46:18 +00001490
jcarsey92a54472011-06-27 20:33:13 +00001491 //
1492 // Get the EFI Shell list of files
1493 //
1494 Status = mEfiShellEnvironment2->FileMetaArg(Arg, &mOldStyleFileList);
1495 if (EFI_ERROR(Status)) {
1496 *ListHead = NULL;
1497 return (Status);
1498 }
jcarsey94b17fa2009-05-07 18:46:18 +00001499
jcarsey92a54472011-06-27 20:33:13 +00001500 if (*ListHead == NULL) {
1501 *ListHead = (EFI_SHELL_FILE_INFO *)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));
1502 if (*ListHead == NULL) {
1503 return (EFI_OUT_OF_RESOURCES);
1504 }
1505 InitializeListHead(&((*ListHead)->Link));
1506 }
1507
1508 //
1509 // Convert that to equivalent of UEFI Shell 2.0 structure
1510 //
1511 InternalShellConvertFileListType(&mOldStyleFileList, &(*ListHead)->Link);
1512
1513 //
1514 // Free the EFI Shell version that was converted.
1515 //
1516 mEfiShellEnvironment2->FreeFileList(&mOldStyleFileList);
1517
1518 if ((*ListHead)->Link.ForwardLink == (*ListHead)->Link.BackLink && (*ListHead)->Link.BackLink == &((*ListHead)->Link)) {
1519 FreePool(*ListHead);
1520 *ListHead = NULL;
1521 Status = EFI_NOT_FOUND;
1522 }
jcarsey94b17fa2009-05-07 18:46:18 +00001523 return (Status);
1524 }
1525
jcarsey92a54472011-06-27 20:33:13 +00001526 return (EFI_UNSUPPORTED);
jcarsey94b17fa2009-05-07 18:46:18 +00001527}
1528/**
jcarseya405b862010-09-14 05:18:09 +00001529 Free the linked list returned from ShellOpenFileMetaArg.
jcarsey94b17fa2009-05-07 18:46:18 +00001530
jcarseya405b862010-09-14 05:18:09 +00001531 if ListHead is NULL then ASSERT().
jcarsey94b17fa2009-05-07 18:46:18 +00001532
jcarseya405b862010-09-14 05:18:09 +00001533 @param ListHead the pointer to free.
jcarsey94b17fa2009-05-07 18:46:18 +00001534
jcarseya405b862010-09-14 05:18:09 +00001535 @retval EFI_SUCCESS the operation was sucessful.
jcarsey94b17fa2009-05-07 18:46:18 +00001536**/
1537EFI_STATUS
1538EFIAPI
1539ShellCloseFileMetaArg (
1540 IN OUT EFI_SHELL_FILE_INFO **ListHead
1541 )
1542{
1543 LIST_ENTRY *Node;
1544
1545 //
1546 // ASSERT that ListHead is not NULL
1547 //
1548 ASSERT(ListHead != NULL);
1549
jcarsey1e6e84c2010-01-25 20:05:08 +00001550 //
jcarsey94b17fa2009-05-07 18:46:18 +00001551 // Check for UEFI Shell 2.0 protocols
1552 //
jcarsey366f81a2011-06-27 21:04:22 +00001553 if (gEfiShellProtocol != NULL) {
1554 return (gEfiShellProtocol->FreeFileList(ListHead));
jcarsey92a54472011-06-27 20:33:13 +00001555 } else if (mEfiShellEnvironment2 != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00001556 //
jcarsey1e6e84c2010-01-25 20:05:08 +00001557 // Since this is EFI Shell version we need to free our internally made copy
jcarsey94b17fa2009-05-07 18:46:18 +00001558 // of the list
1559 //
jcarsey1e6e84c2010-01-25 20:05:08 +00001560 for ( Node = GetFirstNode(&(*ListHead)->Link)
jcarseya405b862010-09-14 05:18:09 +00001561 ; *ListHead != NULL && !IsListEmpty(&(*ListHead)->Link)
jcarsey9b3bf082009-06-23 21:15:07 +00001562 ; Node = GetFirstNode(&(*ListHead)->Link)) {
jcarsey94b17fa2009-05-07 18:46:18 +00001563 RemoveEntryList(Node);
jcarseya405b862010-09-14 05:18:09 +00001564 ((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 +00001565 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->FullName);
1566 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->FileName);
1567 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->Info);
1568 FreePool((EFI_SHELL_FILE_INFO_NO_CONST*)Node);
1569 }
1570 return EFI_SUCCESS;
1571 }
jcarsey92a54472011-06-27 20:33:13 +00001572
1573 return (EFI_UNSUPPORTED);
jcarsey94b17fa2009-05-07 18:46:18 +00001574}
1575
jcarsey125c2cf2009-11-18 21:36:50 +00001576/**
1577 Find a file by searching the CWD and then the path.
1578
jcarseyb3011f42010-01-11 21:49:04 +00001579 If FileName is NULL then ASSERT.
jcarsey125c2cf2009-11-18 21:36:50 +00001580
jcarseyb3011f42010-01-11 21:49:04 +00001581 If the return value is not NULL then the memory must be caller freed.
jcarsey125c2cf2009-11-18 21:36:50 +00001582
1583 @param FileName Filename string.
1584
1585 @retval NULL the file was not found
1586 @return !NULL the full path to the file.
1587**/
1588CHAR16 *
1589EFIAPI
1590ShellFindFilePath (
1591 IN CONST CHAR16 *FileName
1592 )
1593{
1594 CONST CHAR16 *Path;
jcarseya405b862010-09-14 05:18:09 +00001595 SHELL_FILE_HANDLE Handle;
jcarsey125c2cf2009-11-18 21:36:50 +00001596 EFI_STATUS Status;
1597 CHAR16 *RetVal;
1598 CHAR16 *TestPath;
1599 CONST CHAR16 *Walker;
jcarsey36a9d672009-11-20 21:13:41 +00001600 UINTN Size;
jcarsey1cd45e72010-01-29 15:07:44 +00001601 CHAR16 *TempChar;
jcarsey125c2cf2009-11-18 21:36:50 +00001602
1603 RetVal = NULL;
1604
jcarseya405b862010-09-14 05:18:09 +00001605 //
1606 // First make sure its not an absolute path.
1607 //
1608 Status = ShellOpenFileByName(FileName, &Handle, EFI_FILE_MODE_READ, 0);
1609 if (!EFI_ERROR(Status)){
1610 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {
1611 ASSERT(RetVal == NULL);
1612 RetVal = StrnCatGrow(&RetVal, NULL, FileName, 0);
1613 ShellCloseFile(&Handle);
1614 return (RetVal);
1615 } else {
1616 ShellCloseFile(&Handle);
1617 }
1618 }
1619
jcarsey125c2cf2009-11-18 21:36:50 +00001620 Path = ShellGetEnvironmentVariable(L"cwd");
1621 if (Path != NULL) {
jcarsey36a9d672009-11-20 21:13:41 +00001622 Size = StrSize(Path);
1623 Size += StrSize(FileName);
1624 TestPath = AllocateZeroPool(Size);
jcarseyc9d92df2010-02-03 15:37:54 +00001625 if (TestPath == NULL) {
1626 return (NULL);
1627 }
jcarsey125c2cf2009-11-18 21:36:50 +00001628 StrCpy(TestPath, Path);
1629 StrCat(TestPath, FileName);
1630 Status = ShellOpenFileByName(TestPath, &Handle, EFI_FILE_MODE_READ, 0);
1631 if (!EFI_ERROR(Status)){
jcarseya405b862010-09-14 05:18:09 +00001632 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {
1633 ASSERT(RetVal == NULL);
1634 RetVal = StrnCatGrow(&RetVal, NULL, TestPath, 0);
1635 ShellCloseFile(&Handle);
1636 FreePool(TestPath);
1637 return (RetVal);
1638 } else {
1639 ShellCloseFile(&Handle);
1640 }
jcarsey125c2cf2009-11-18 21:36:50 +00001641 }
1642 FreePool(TestPath);
1643 }
1644 Path = ShellGetEnvironmentVariable(L"path");
1645 if (Path != NULL) {
jcarseya405b862010-09-14 05:18:09 +00001646 Size = StrSize(Path)+sizeof(CHAR16);
jcarsey36a9d672009-11-20 21:13:41 +00001647 Size += StrSize(FileName);
1648 TestPath = AllocateZeroPool(Size);
jcarsey3e082d52010-10-04 16:44:57 +00001649 if (TestPath == NULL) {
1650 return (NULL);
1651 }
jcarsey1e6e84c2010-01-25 20:05:08 +00001652 Walker = (CHAR16*)Path;
jcarsey125c2cf2009-11-18 21:36:50 +00001653 do {
1654 CopyMem(TestPath, Walker, StrSize(Walker));
jcarsey3e082d52010-10-04 16:44:57 +00001655 if (TestPath != NULL) {
1656 TempChar = StrStr(TestPath, L";");
1657 if (TempChar != NULL) {
1658 *TempChar = CHAR_NULL;
1659 }
1660 if (TestPath[StrLen(TestPath)-1] != L'\\') {
1661 StrCat(TestPath, L"\\");
1662 }
jcarsey89e85372011-04-13 23:37:50 +00001663 if (FileName[0] == L'\\') {
1664 FileName++;
1665 }
jcarsey3e082d52010-10-04 16:44:57 +00001666 StrCat(TestPath, FileName);
1667 if (StrStr(Walker, L";") != NULL) {
1668 Walker = StrStr(Walker, L";") + 1;
jcarseya405b862010-09-14 05:18:09 +00001669 } else {
jcarsey3e082d52010-10-04 16:44:57 +00001670 Walker = NULL;
1671 }
1672 Status = ShellOpenFileByName(TestPath, &Handle, EFI_FILE_MODE_READ, 0);
1673 if (!EFI_ERROR(Status)){
1674 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {
1675 ASSERT(RetVal == NULL);
1676 RetVal = StrnCatGrow(&RetVal, NULL, TestPath, 0);
1677 ShellCloseFile(&Handle);
1678 break;
1679 } else {
1680 ShellCloseFile(&Handle);
1681 }
jcarseya405b862010-09-14 05:18:09 +00001682 }
jcarsey125c2cf2009-11-18 21:36:50 +00001683 }
1684 } while (Walker != NULL && Walker[0] != CHAR_NULL);
1685 FreePool(TestPath);
1686 }
1687 return (RetVal);
1688}
1689
jcarseyb3011f42010-01-11 21:49:04 +00001690/**
jcarsey1e6e84c2010-01-25 20:05:08 +00001691 Find a file by searching the CWD and then the path with a variable set of file
1692 extensions. If the file is not found it will append each extension in the list
jcarseyb3011f42010-01-11 21:49:04 +00001693 in the order provided and return the first one that is successful.
1694
1695 If FileName is NULL, then ASSERT.
1696 If FileExtension is NULL, then behavior is identical to ShellFindFilePath.
1697
1698 If the return value is not NULL then the memory must be caller freed.
1699
1700 @param[in] FileName Filename string.
1701 @param[in] FileExtension Semi-colon delimeted list of possible extensions.
1702
1703 @retval NULL The file was not found.
1704 @retval !NULL The path to the file.
1705**/
1706CHAR16 *
1707EFIAPI
1708ShellFindFilePathEx (
1709 IN CONST CHAR16 *FileName,
1710 IN CONST CHAR16 *FileExtension
1711 )
1712{
1713 CHAR16 *TestPath;
1714 CHAR16 *RetVal;
1715 CONST CHAR16 *ExtensionWalker;
jcarsey9e926b62010-01-14 20:26:39 +00001716 UINTN Size;
jcarsey1cd45e72010-01-29 15:07:44 +00001717 CHAR16 *TempChar;
jcarseyc9d92df2010-02-03 15:37:54 +00001718 CHAR16 *TempChar2;
jcarsey1cd45e72010-01-29 15:07:44 +00001719
jcarseyb3011f42010-01-11 21:49:04 +00001720 ASSERT(FileName != NULL);
1721 if (FileExtension == NULL) {
1722 return (ShellFindFilePath(FileName));
1723 }
1724 RetVal = ShellFindFilePath(FileName);
1725 if (RetVal != NULL) {
1726 return (RetVal);
1727 }
jcarsey9e926b62010-01-14 20:26:39 +00001728 Size = StrSize(FileName);
1729 Size += StrSize(FileExtension);
1730 TestPath = AllocateZeroPool(Size);
jcarseyc9d92df2010-02-03 15:37:54 +00001731 if (TestPath == NULL) {
1732 return (NULL);
1733 }
jcarseya405b862010-09-14 05:18:09 +00001734 for (ExtensionWalker = FileExtension, TempChar2 = (CHAR16*)FileExtension; TempChar2 != NULL ; ExtensionWalker = TempChar2 + 1){
jcarseyb3011f42010-01-11 21:49:04 +00001735 StrCpy(TestPath, FileName);
jcarseya405b862010-09-14 05:18:09 +00001736 if (ExtensionWalker != NULL) {
1737 StrCat(TestPath, ExtensionWalker);
1738 }
jcarsey1cd45e72010-01-29 15:07:44 +00001739 TempChar = StrStr(TestPath, L";");
1740 if (TempChar != NULL) {
1741 *TempChar = CHAR_NULL;
jcarseyb3011f42010-01-11 21:49:04 +00001742 }
1743 RetVal = ShellFindFilePath(TestPath);
1744 if (RetVal != NULL) {
1745 break;
1746 }
jcarseya405b862010-09-14 05:18:09 +00001747 ASSERT(ExtensionWalker != NULL);
jcarseyc9d92df2010-02-03 15:37:54 +00001748 TempChar2 = StrStr(ExtensionWalker, L";");
jcarseyb3011f42010-01-11 21:49:04 +00001749 }
1750 FreePool(TestPath);
1751 return (RetVal);
1752}
1753
jcarsey94b17fa2009-05-07 18:46:18 +00001754typedef struct {
jcarsey9b3bf082009-06-23 21:15:07 +00001755 LIST_ENTRY Link;
jcarsey94b17fa2009-05-07 18:46:18 +00001756 CHAR16 *Name;
jcarseya405b862010-09-14 05:18:09 +00001757 SHELL_PARAM_TYPE Type;
jcarsey94b17fa2009-05-07 18:46:18 +00001758 CHAR16 *Value;
1759 UINTN OriginalPosition;
1760} SHELL_PARAM_PACKAGE;
1761
1762/**
jcarsey1e6e84c2010-01-25 20:05:08 +00001763 Checks the list of valid arguments and returns TRUE if the item was found. If the
jcarsey94b17fa2009-05-07 18:46:18 +00001764 return value is TRUE then the type parameter is set also.
jcarsey1e6e84c2010-01-25 20:05:08 +00001765
jcarsey94b17fa2009-05-07 18:46:18 +00001766 if CheckList is NULL then ASSERT();
1767 if Name is NULL then ASSERT();
1768 if Type is NULL then ASSERT();
1769
jcarsey94b17fa2009-05-07 18:46:18 +00001770 @param Name pointer to Name of parameter found
1771 @param CheckList List to check against
jcarseya405b862010-09-14 05:18:09 +00001772 @param Type pointer to type of parameter if it was found
jcarsey94b17fa2009-05-07 18:46:18 +00001773
1774 @retval TRUE the Parameter was found. Type is valid.
1775 @retval FALSE the Parameter was not found. Type is not valid.
1776**/
1777BOOLEAN
1778EFIAPI
jcarseyd2b45642009-05-11 18:02:16 +00001779InternalIsOnCheckList (
jcarsey94b17fa2009-05-07 18:46:18 +00001780 IN CONST CHAR16 *Name,
1781 IN CONST SHELL_PARAM_ITEM *CheckList,
jcarsey252d9452011-03-25 20:49:53 +00001782 OUT SHELL_PARAM_TYPE *Type
jcarseya405b862010-09-14 05:18:09 +00001783 )
1784{
jcarsey94b17fa2009-05-07 18:46:18 +00001785 SHELL_PARAM_ITEM *TempListItem;
jcarsey252d9452011-03-25 20:49:53 +00001786 CHAR16 *TempString;
jcarsey94b17fa2009-05-07 18:46:18 +00001787
1788 //
1789 // ASSERT that all 3 pointer parameters aren't NULL
1790 //
1791 ASSERT(CheckList != NULL);
1792 ASSERT(Type != NULL);
1793 ASSERT(Name != NULL);
1794
1795 //
jcarseyd2b45642009-05-11 18:02:16 +00001796 // question mark and page break mode are always supported
1797 //
1798 if ((StrCmp(Name, L"-?") == 0) ||
1799 (StrCmp(Name, L"-b") == 0)
jcarseya405b862010-09-14 05:18:09 +00001800 ) {
jcarsey252d9452011-03-25 20:49:53 +00001801 *Type = TypeFlag;
jcarseyd2b45642009-05-11 18:02:16 +00001802 return (TRUE);
1803 }
1804
1805 //
jcarsey94b17fa2009-05-07 18:46:18 +00001806 // Enumerate through the list
1807 //
1808 for (TempListItem = (SHELL_PARAM_ITEM*)CheckList ; TempListItem->Name != NULL ; TempListItem++) {
1809 //
jcarsey9eb53ac2009-07-08 17:26:58 +00001810 // If the Type is TypeStart only check the first characters of the passed in param
1811 // If it matches set the type and return TRUE
jcarsey94b17fa2009-05-07 18:46:18 +00001812 //
darylm503b0934ac2011-11-12 00:35:11 +00001813 if (TempListItem->Type == TypeStart) {
jcarsey252d9452011-03-25 20:49:53 +00001814 if (StrnCmp(Name, TempListItem->Name, StrLen(TempListItem->Name)) == 0) {
1815 *Type = TempListItem->Type;
1816 return (TRUE);
1817 }
1818 TempString = NULL;
1819 TempString = StrnCatGrow(&TempString, NULL, Name, StrLen(TempListItem->Name));
1820 if (TempString != NULL) {
1821 if (StringNoCaseCompare(&TempString, &TempListItem->Name) == 0) {
1822 *Type = TempListItem->Type;
1823 FreePool(TempString);
1824 return (TRUE);
1825 }
1826 FreePool(TempString);
1827 }
1828 } else if (StringNoCaseCompare(&Name, &TempListItem->Name) == 0) {
jcarsey94b17fa2009-05-07 18:46:18 +00001829 *Type = TempListItem->Type;
1830 return (TRUE);
1831 }
1832 }
jcarsey2247dde2009-11-09 18:08:58 +00001833
jcarsey94b17fa2009-05-07 18:46:18 +00001834 return (FALSE);
1835}
1836/**
jcarseyd2b45642009-05-11 18:02:16 +00001837 Checks the string for indicators of "flag" status. this is a leading '/', '-', or '+'
jcarsey94b17fa2009-05-07 18:46:18 +00001838
jcarseya405b862010-09-14 05:18:09 +00001839 @param[in] Name pointer to Name of parameter found
1840 @param[in] AlwaysAllowNumbers TRUE to allow numbers, FALSE to not.
jcarsey94b17fa2009-05-07 18:46:18 +00001841
1842 @retval TRUE the Parameter is a flag.
jcarseya405b862010-09-14 05:18:09 +00001843 @retval FALSE the Parameter not a flag.
jcarsey94b17fa2009-05-07 18:46:18 +00001844**/
1845BOOLEAN
1846EFIAPI
jcarseyd2b45642009-05-11 18:02:16 +00001847InternalIsFlag (
jcarsey2247dde2009-11-09 18:08:58 +00001848 IN CONST CHAR16 *Name,
1849 IN BOOLEAN AlwaysAllowNumbers
jcarsey94b17fa2009-05-07 18:46:18 +00001850 )
1851{
1852 //
1853 // ASSERT that Name isn't NULL
1854 //
1855 ASSERT(Name != NULL);
1856
1857 //
jcarsey2247dde2009-11-09 18:08:58 +00001858 // If we accept numbers then dont return TRUE. (they will be values)
1859 //
jcarseya405b862010-09-14 05:18:09 +00001860 if (((Name[0] == L'-' || Name[0] == L'+') && ShellIsHexaDecimalDigitCharacter(Name[1])) && AlwaysAllowNumbers) {
jcarsey2247dde2009-11-09 18:08:58 +00001861 return (FALSE);
1862 }
1863
1864 //
jcarseya405b862010-09-14 05:18:09 +00001865 // If the Name has a /, +, or - as the first character return TRUE
jcarsey94b17fa2009-05-07 18:46:18 +00001866 //
jcarsey1e6e84c2010-01-25 20:05:08 +00001867 if ((Name[0] == L'/') ||
jcarseyd2b45642009-05-11 18:02:16 +00001868 (Name[0] == L'-') ||
1869 (Name[0] == L'+')
jcarseya405b862010-09-14 05:18:09 +00001870 ) {
jcarsey94b17fa2009-05-07 18:46:18 +00001871 return (TRUE);
1872 }
1873 return (FALSE);
1874}
1875
1876/**
jcarsey1e6e84c2010-01-25 20:05:08 +00001877 Checks the command line arguments passed against the list of valid ones.
jcarsey94b17fa2009-05-07 18:46:18 +00001878
1879 If no initialization is required, then return RETURN_SUCCESS.
jcarsey1e6e84c2010-01-25 20:05:08 +00001880
jcarseya405b862010-09-14 05:18:09 +00001881 @param[in] CheckList pointer to list of parameters to check
1882 @param[out] CheckPackage pointer to pointer to list checked values
1883 @param[out] ProblemParam optional pointer to pointer to unicode string for
jcarseyd2b45642009-05-11 18:02:16 +00001884 the paramater that caused failure. If used then the
1885 caller is responsible for freeing the memory.
jcarseya405b862010-09-14 05:18:09 +00001886 @param[in] AutoPageBreak will automatically set PageBreakEnabled for "b" parameter
1887 @param[in] Argv pointer to array of parameters
1888 @param[in] Argc Count of parameters in Argv
1889 @param[in] AlwaysAllowNumbers TRUE to allow numbers always, FALSE otherwise.
jcarsey94b17fa2009-05-07 18:46:18 +00001890
1891 @retval EFI_SUCCESS The operation completed sucessfully.
1892 @retval EFI_OUT_OF_RESOURCES A memory allocation failed
1893 @retval EFI_INVALID_PARAMETER A parameter was invalid
jcarsey1e6e84c2010-01-25 20:05:08 +00001894 @retval EFI_VOLUME_CORRUPTED the command line was corrupt. an argument was
1895 duplicated. the duplicated command line argument
jcarsey94b17fa2009-05-07 18:46:18 +00001896 was returned in ProblemParam if provided.
jcarsey1e6e84c2010-01-25 20:05:08 +00001897 @retval EFI_NOT_FOUND a argument required a value that was missing.
jcarsey94b17fa2009-05-07 18:46:18 +00001898 the invalid command line argument was returned in
1899 ProblemParam if provided.
1900**/
1901EFI_STATUS
1902EFIAPI
1903InternalCommandLineParse (
1904 IN CONST SHELL_PARAM_ITEM *CheckList,
1905 OUT LIST_ENTRY **CheckPackage,
1906 OUT CHAR16 **ProblemParam OPTIONAL,
1907 IN BOOLEAN AutoPageBreak,
1908 IN CONST CHAR16 **Argv,
jcarsey2247dde2009-11-09 18:08:58 +00001909 IN UINTN Argc,
1910 IN BOOLEAN AlwaysAllowNumbers
jcarseya405b862010-09-14 05:18:09 +00001911 )
1912{
jcarsey94b17fa2009-05-07 18:46:18 +00001913 UINTN LoopCounter;
jcarsey252d9452011-03-25 20:49:53 +00001914 SHELL_PARAM_TYPE CurrentItemType;
jcarsey94b17fa2009-05-07 18:46:18 +00001915 SHELL_PARAM_PACKAGE *CurrentItemPackage;
jcarsey125c2cf2009-11-18 21:36:50 +00001916 UINTN GetItemValue;
1917 UINTN ValueSize;
jcarseya405b862010-09-14 05:18:09 +00001918 UINTN Count;
jcarsey252d9452011-03-25 20:49:53 +00001919 CONST CHAR16 *TempPointer;
jcarsey94b17fa2009-05-07 18:46:18 +00001920
1921 CurrentItemPackage = NULL;
jcarsey125c2cf2009-11-18 21:36:50 +00001922 GetItemValue = 0;
1923 ValueSize = 0;
jcarseya405b862010-09-14 05:18:09 +00001924 Count = 0;
jcarsey94b17fa2009-05-07 18:46:18 +00001925
1926 //
1927 // If there is only 1 item we dont need to do anything
1928 //
jcarseya405b862010-09-14 05:18:09 +00001929 if (Argc < 1) {
jcarsey94b17fa2009-05-07 18:46:18 +00001930 *CheckPackage = NULL;
1931 return (EFI_SUCCESS);
1932 }
1933
1934 //
jcarsey2247dde2009-11-09 18:08:58 +00001935 // ASSERTs
1936 //
1937 ASSERT(CheckList != NULL);
1938 ASSERT(Argv != NULL);
1939
1940 //
jcarsey94b17fa2009-05-07 18:46:18 +00001941 // initialize the linked list
1942 //
1943 *CheckPackage = (LIST_ENTRY*)AllocateZeroPool(sizeof(LIST_ENTRY));
sfu502a758c2011-10-08 02:55:30 +00001944 if (*CheckPackage == NULL) {
jcarseybeab0fc2011-10-10 17:26:25 +00001945 return (EFI_OUT_OF_RESOURCES);
sfu502a758c2011-10-08 02:55:30 +00001946 }
jcarseybeab0fc2011-10-10 17:26:25 +00001947
jcarsey94b17fa2009-05-07 18:46:18 +00001948 InitializeListHead(*CheckPackage);
1949
1950 //
1951 // loop through each of the arguments
1952 //
1953 for (LoopCounter = 0 ; LoopCounter < Argc ; ++LoopCounter) {
1954 if (Argv[LoopCounter] == NULL) {
1955 //
1956 // do nothing for NULL argv
1957 //
jcarseya405b862010-09-14 05:18:09 +00001958 } else if (InternalIsOnCheckList(Argv[LoopCounter], CheckList, &CurrentItemType)) {
jcarsey94b17fa2009-05-07 18:46:18 +00001959 //
jcarsey2247dde2009-11-09 18:08:58 +00001960 // We might have leftover if last parameter didnt have optional value
1961 //
jcarsey125c2cf2009-11-18 21:36:50 +00001962 if (GetItemValue != 0) {
1963 GetItemValue = 0;
jcarsey2247dde2009-11-09 18:08:58 +00001964 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
1965 }
1966 //
jcarsey94b17fa2009-05-07 18:46:18 +00001967 // this is a flag
1968 //
jcarsey252d9452011-03-25 20:49:53 +00001969 CurrentItemPackage = AllocateZeroPool(sizeof(SHELL_PARAM_PACKAGE));
jcarseybeab0fc2011-10-10 17:26:25 +00001970 if (CurrentItemPackage == NULL) {
1971 ShellCommandLineFreeVarList(*CheckPackage);
1972 *CheckPackage = NULL;
1973 return (EFI_OUT_OF_RESOURCES);
1974 }
jcarsey252d9452011-03-25 20:49:53 +00001975 CurrentItemPackage->Name = AllocateZeroPool(StrSize(Argv[LoopCounter]));
jcarseybeab0fc2011-10-10 17:26:25 +00001976 if (CurrentItemPackage->Name == NULL) {
1977 ShellCommandLineFreeVarList(*CheckPackage);
1978 *CheckPackage = NULL;
1979 return (EFI_OUT_OF_RESOURCES);
1980 }
jcarsey94b17fa2009-05-07 18:46:18 +00001981 StrCpy(CurrentItemPackage->Name, Argv[LoopCounter]);
1982 CurrentItemPackage->Type = CurrentItemType;
1983 CurrentItemPackage->OriginalPosition = (UINTN)(-1);
jcarseyb1f95a02009-06-16 00:23:19 +00001984 CurrentItemPackage->Value = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +00001985
1986 //
1987 // Does this flag require a value
1988 //
jcarsey125c2cf2009-11-18 21:36:50 +00001989 switch (CurrentItemPackage->Type) {
jcarsey94b17fa2009-05-07 18:46:18 +00001990 //
jcarsey125c2cf2009-11-18 21:36:50 +00001991 // possibly trigger the next loop(s) to populate the value of this item
jcarsey1e6e84c2010-01-25 20:05:08 +00001992 //
jcarsey125c2cf2009-11-18 21:36:50 +00001993 case TypeValue:
jcarsey1e6e84c2010-01-25 20:05:08 +00001994 GetItemValue = 1;
jcarsey125c2cf2009-11-18 21:36:50 +00001995 ValueSize = 0;
1996 break;
1997 case TypeDoubleValue:
1998 GetItemValue = 2;
1999 ValueSize = 0;
2000 break;
2001 case TypeMaxValue:
2002 GetItemValue = (UINTN)(-1);
2003 ValueSize = 0;
2004 break;
2005 default:
2006 //
2007 // this item has no value expected; we are done
2008 //
2009 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
2010 ASSERT(GetItemValue == 0);
2011 break;
jcarsey94b17fa2009-05-07 18:46:18 +00002012 }
jcarseya405b862010-09-14 05:18:09 +00002013 } else if (GetItemValue != 0 && !InternalIsFlag(Argv[LoopCounter], AlwaysAllowNumbers)) {
jcarseyb1f95a02009-06-16 00:23:19 +00002014 ASSERT(CurrentItemPackage != NULL);
2015 //
jcarsey125c2cf2009-11-18 21:36:50 +00002016 // get the item VALUE for a previous flag
jcarseyb1f95a02009-06-16 00:23:19 +00002017 //
jcarsey125c2cf2009-11-18 21:36:50 +00002018 CurrentItemPackage->Value = ReallocatePool(ValueSize, ValueSize + StrSize(Argv[LoopCounter]) + sizeof(CHAR16), CurrentItemPackage->Value);
jcarseyb1f95a02009-06-16 00:23:19 +00002019 ASSERT(CurrentItemPackage->Value != NULL);
jcarsey125c2cf2009-11-18 21:36:50 +00002020 if (ValueSize == 0) {
2021 StrCpy(CurrentItemPackage->Value, Argv[LoopCounter]);
2022 } else {
2023 StrCat(CurrentItemPackage->Value, L" ");
2024 StrCat(CurrentItemPackage->Value, Argv[LoopCounter]);
2025 }
2026 ValueSize += StrSize(Argv[LoopCounter]) + sizeof(CHAR16);
2027 GetItemValue--;
2028 if (GetItemValue == 0) {
2029 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
2030 }
jcarseya405b862010-09-14 05:18:09 +00002031 } else if (!InternalIsFlag(Argv[LoopCounter], AlwaysAllowNumbers) ){ //|| ProblemParam == NULL) {
jcarseyb1f95a02009-06-16 00:23:19 +00002032 //
2033 // add this one as a non-flag
2034 //
jcarsey252d9452011-03-25 20:49:53 +00002035
2036 TempPointer = Argv[LoopCounter];
darylm503b0934ac2011-11-12 00:35:11 +00002037 if ((*TempPointer == L'^' && *(TempPointer+1) == L'-')
jcarsey252d9452011-03-25 20:49:53 +00002038 || (*TempPointer == L'^' && *(TempPointer+1) == L'/')
2039 || (*TempPointer == L'^' && *(TempPointer+1) == L'+')
2040 ){
2041 TempPointer++;
2042 }
2043 CurrentItemPackage = AllocateZeroPool(sizeof(SHELL_PARAM_PACKAGE));
jcarseybeab0fc2011-10-10 17:26:25 +00002044 if (CurrentItemPackage == NULL) {
2045 ShellCommandLineFreeVarList(*CheckPackage);
2046 *CheckPackage = NULL;
2047 return (EFI_OUT_OF_RESOURCES);
2048 }
jcarseyb1f95a02009-06-16 00:23:19 +00002049 CurrentItemPackage->Name = NULL;
2050 CurrentItemPackage->Type = TypePosition;
jcarsey252d9452011-03-25 20:49:53 +00002051 CurrentItemPackage->Value = AllocateZeroPool(StrSize(TempPointer));
jcarseybeab0fc2011-10-10 17:26:25 +00002052 if (CurrentItemPackage->Value == NULL) {
2053 ShellCommandLineFreeVarList(*CheckPackage);
2054 *CheckPackage = NULL;
2055 return (EFI_OUT_OF_RESOURCES);
2056 }
jcarsey252d9452011-03-25 20:49:53 +00002057 StrCpy(CurrentItemPackage->Value, TempPointer);
jcarseya405b862010-09-14 05:18:09 +00002058 CurrentItemPackage->OriginalPosition = Count++;
jcarsey9b3bf082009-06-23 21:15:07 +00002059 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
jcarsey252d9452011-03-25 20:49:53 +00002060 } else {
jcarsey94b17fa2009-05-07 18:46:18 +00002061 //
2062 // this was a non-recognised flag... error!
2063 //
jcarsey252d9452011-03-25 20:49:53 +00002064 if (ProblemParam != NULL) {
2065 *ProblemParam = AllocateZeroPool(StrSize(Argv[LoopCounter]));
jcarseybeab0fc2011-10-10 17:26:25 +00002066 if (*ProblemParam != NULL) {
darylm503b0934ac2011-11-12 00:35:11 +00002067 StrCpy(*ProblemParam, Argv[LoopCounter]);
jcarseybeab0fc2011-10-10 17:26:25 +00002068 }
jcarsey252d9452011-03-25 20:49:53 +00002069 }
jcarsey94b17fa2009-05-07 18:46:18 +00002070 ShellCommandLineFreeVarList(*CheckPackage);
2071 *CheckPackage = NULL;
2072 return (EFI_VOLUME_CORRUPTED);
jcarsey94b17fa2009-05-07 18:46:18 +00002073 }
2074 }
jcarsey125c2cf2009-11-18 21:36:50 +00002075 if (GetItemValue != 0) {
2076 GetItemValue = 0;
2077 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
2078 }
jcarsey94b17fa2009-05-07 18:46:18 +00002079 //
2080 // support for AutoPageBreak
2081 //
2082 if (AutoPageBreak && ShellCommandLineGetFlag(*CheckPackage, L"-b")) {
2083 ShellSetPageBreakMode(TRUE);
2084 }
2085 return (EFI_SUCCESS);
2086}
2087
2088/**
jcarsey1e6e84c2010-01-25 20:05:08 +00002089 Checks the command line arguments passed against the list of valid ones.
jcarsey94b17fa2009-05-07 18:46:18 +00002090 Optionally removes NULL values first.
jcarsey1e6e84c2010-01-25 20:05:08 +00002091
jcarsey94b17fa2009-05-07 18:46:18 +00002092 If no initialization is required, then return RETURN_SUCCESS.
jcarsey1e6e84c2010-01-25 20:05:08 +00002093
jcarseya405b862010-09-14 05:18:09 +00002094 @param[in] CheckList The pointer to list of parameters to check.
2095 @param[out] CheckPackage The package of checked values.
2096 @param[out] ProblemParam Optional pointer to pointer to unicode string for
jcarsey94b17fa2009-05-07 18:46:18 +00002097 the paramater that caused failure.
jcarseya405b862010-09-14 05:18:09 +00002098 @param[in] AutoPageBreak Will automatically set PageBreakEnabled.
2099 @param[in] AlwaysAllowNumbers Will never fail for number based flags.
jcarsey94b17fa2009-05-07 18:46:18 +00002100
2101 @retval EFI_SUCCESS The operation completed sucessfully.
jcarseya405b862010-09-14 05:18:09 +00002102 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
2103 @retval EFI_INVALID_PARAMETER A parameter was invalid.
2104 @retval EFI_VOLUME_CORRUPTED The command line was corrupt.
2105 @retval EFI_DEVICE_ERROR The commands contained 2 opposing arguments. One
jcarsey1e6e84c2010-01-25 20:05:08 +00002106 of the command line arguments was returned in
jcarsey94b17fa2009-05-07 18:46:18 +00002107 ProblemParam if provided.
jcarseya405b862010-09-14 05:18:09 +00002108 @retval EFI_NOT_FOUND A argument required a value that was missing.
2109 The invalid command line argument was returned in
jcarsey94b17fa2009-05-07 18:46:18 +00002110 ProblemParam if provided.
2111**/
2112EFI_STATUS
2113EFIAPI
jcarsey2247dde2009-11-09 18:08:58 +00002114ShellCommandLineParseEx (
jcarsey94b17fa2009-05-07 18:46:18 +00002115 IN CONST SHELL_PARAM_ITEM *CheckList,
2116 OUT LIST_ENTRY **CheckPackage,
2117 OUT CHAR16 **ProblemParam OPTIONAL,
jcarsey2247dde2009-11-09 18:08:58 +00002118 IN BOOLEAN AutoPageBreak,
2119 IN BOOLEAN AlwaysAllowNumbers
jcarseya405b862010-09-14 05:18:09 +00002120 )
2121{
jcarsey1e6e84c2010-01-25 20:05:08 +00002122 //
jcarsey94b17fa2009-05-07 18:46:18 +00002123 // ASSERT that CheckList and CheckPackage aren't NULL
2124 //
2125 ASSERT(CheckList != NULL);
2126 ASSERT(CheckPackage != NULL);
2127
jcarsey1e6e84c2010-01-25 20:05:08 +00002128 //
jcarsey94b17fa2009-05-07 18:46:18 +00002129 // Check for UEFI Shell 2.0 protocols
2130 //
jcarsey366f81a2011-06-27 21:04:22 +00002131 if (gEfiShellParametersProtocol != NULL) {
jcarsey1e6e84c2010-01-25 20:05:08 +00002132 return (InternalCommandLineParse(CheckList,
2133 CheckPackage,
2134 ProblemParam,
2135 AutoPageBreak,
jcarsey366f81a2011-06-27 21:04:22 +00002136 (CONST CHAR16**) gEfiShellParametersProtocol->Argv,
2137 gEfiShellParametersProtocol->Argc,
jcarsey2247dde2009-11-09 18:08:58 +00002138 AlwaysAllowNumbers));
jcarsey94b17fa2009-05-07 18:46:18 +00002139 }
2140
jcarsey1e6e84c2010-01-25 20:05:08 +00002141 //
jcarsey94b17fa2009-05-07 18:46:18 +00002142 // ASSERT That EFI Shell is not required
2143 //
2144 ASSERT (mEfiShellInterface != NULL);
jcarsey1e6e84c2010-01-25 20:05:08 +00002145 return (InternalCommandLineParse(CheckList,
2146 CheckPackage,
2147 ProblemParam,
2148 AutoPageBreak,
jljusten08d7f8e2009-06-15 18:42:13 +00002149 (CONST CHAR16**) mEfiShellInterface->Argv,
jcarsey2247dde2009-11-09 18:08:58 +00002150 mEfiShellInterface->Argc,
2151 AlwaysAllowNumbers));
jcarsey94b17fa2009-05-07 18:46:18 +00002152}
2153
2154/**
2155 Frees shell variable list that was returned from ShellCommandLineParse.
2156
2157 This function will free all the memory that was used for the CheckPackage
2158 list of postprocessed shell arguments.
2159
2160 this function has no return value.
2161
2162 if CheckPackage is NULL, then return
2163
2164 @param CheckPackage the list to de-allocate
2165 **/
2166VOID
2167EFIAPI
2168ShellCommandLineFreeVarList (
2169 IN LIST_ENTRY *CheckPackage
jcarseya405b862010-09-14 05:18:09 +00002170 )
2171{
jcarsey94b17fa2009-05-07 18:46:18 +00002172 LIST_ENTRY *Node;
2173
2174 //
2175 // check for CheckPackage == NULL
2176 //
2177 if (CheckPackage == NULL) {
2178 return;
2179 }
2180
2181 //
2182 // for each node in the list
2183 //
jcarsey9eb53ac2009-07-08 17:26:58 +00002184 for ( Node = GetFirstNode(CheckPackage)
jcarseya405b862010-09-14 05:18:09 +00002185 ; !IsListEmpty(CheckPackage)
jcarsey9eb53ac2009-07-08 17:26:58 +00002186 ; Node = GetFirstNode(CheckPackage)
jcarseya405b862010-09-14 05:18:09 +00002187 ){
jcarsey94b17fa2009-05-07 18:46:18 +00002188 //
2189 // Remove it from the list
2190 //
2191 RemoveEntryList(Node);
2192
2193 //
2194 // if it has a name free the name
2195 //
2196 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {
2197 FreePool(((SHELL_PARAM_PACKAGE*)Node)->Name);
2198 }
2199
2200 //
2201 // if it has a value free the value
2202 //
2203 if (((SHELL_PARAM_PACKAGE*)Node)->Value != NULL) {
2204 FreePool(((SHELL_PARAM_PACKAGE*)Node)->Value);
2205 }
jcarsey1e6e84c2010-01-25 20:05:08 +00002206
jcarsey94b17fa2009-05-07 18:46:18 +00002207 //
2208 // free the node structure
2209 //
2210 FreePool((SHELL_PARAM_PACKAGE*)Node);
2211 }
2212 //
2213 // free the list head node
2214 //
2215 FreePool(CheckPackage);
2216}
2217/**
2218 Checks for presence of a flag parameter
2219
2220 flag arguments are in the form of "-<Key>" or "/<Key>", but do not have a value following the key
2221
2222 if CheckPackage is NULL then return FALSE.
2223 if KeyString is NULL then ASSERT()
jcarsey1e6e84c2010-01-25 20:05:08 +00002224
jcarsey94b17fa2009-05-07 18:46:18 +00002225 @param CheckPackage The package of parsed command line arguments
2226 @param KeyString the Key of the command line argument to check for
2227
2228 @retval TRUE the flag is on the command line
2229 @retval FALSE the flag is not on the command line
2230 **/
2231BOOLEAN
2232EFIAPI
2233ShellCommandLineGetFlag (
jcarseya405b862010-09-14 05:18:09 +00002234 IN CONST LIST_ENTRY * CONST CheckPackage,
2235 IN CONST CHAR16 * CONST KeyString
2236 )
2237{
jcarsey94b17fa2009-05-07 18:46:18 +00002238 LIST_ENTRY *Node;
jcarsey252d9452011-03-25 20:49:53 +00002239 CHAR16 *TempString;
jcarsey94b17fa2009-05-07 18:46:18 +00002240
2241 //
ydong100c1950b2011-10-13 02:37:35 +00002242 // return FALSE for no package or KeyString is NULL
jcarsey94b17fa2009-05-07 18:46:18 +00002243 //
ydong100c1950b2011-10-13 02:37:35 +00002244 if (CheckPackage == NULL || KeyString == NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00002245 return (FALSE);
2246 }
2247
2248 //
2249 // enumerate through the list of parametrs
2250 //
jcarsey1e6e84c2010-01-25 20:05:08 +00002251 for ( Node = GetFirstNode(CheckPackage)
2252 ; !IsNull (CheckPackage, Node)
2253 ; Node = GetNextNode(CheckPackage, Node)
jcarsey252d9452011-03-25 20:49:53 +00002254 ){
jcarsey94b17fa2009-05-07 18:46:18 +00002255 //
2256 // If the Name matches, return TRUE (and there may be NULL name)
2257 //
2258 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {
jcarsey9eb53ac2009-07-08 17:26:58 +00002259 //
2260 // If Type is TypeStart then only compare the begining of the strings
2261 //
jcarsey252d9452011-03-25 20:49:53 +00002262 if (((SHELL_PARAM_PACKAGE*)Node)->Type == TypeStart) {
2263 if (StrnCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name, StrLen(KeyString)) == 0) {
2264 return (TRUE);
2265 }
2266 TempString = NULL;
2267 TempString = StrnCatGrow(&TempString, NULL, KeyString, StrLen(((SHELL_PARAM_PACKAGE*)Node)->Name));
2268 if (TempString != NULL) {
2269 if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {
2270 FreePool(TempString);
2271 return (TRUE);
2272 }
2273 FreePool(TempString);
2274 }
2275 } else if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {
jcarsey94b17fa2009-05-07 18:46:18 +00002276 return (TRUE);
2277 }
2278 }
2279 }
2280 return (FALSE);
2281}
2282/**
jcarseya405b862010-09-14 05:18:09 +00002283 Returns value from command line argument.
jcarsey94b17fa2009-05-07 18:46:18 +00002284
jcarseya405b862010-09-14 05:18:09 +00002285 Value parameters are in the form of "-<Key> value" or "/<Key> value".
jcarsey1e6e84c2010-01-25 20:05:08 +00002286
jcarseya405b862010-09-14 05:18:09 +00002287 If CheckPackage is NULL, then return NULL.
jcarsey94b17fa2009-05-07 18:46:18 +00002288
jcarseya405b862010-09-14 05:18:09 +00002289 @param[in] CheckPackage The package of parsed command line arguments.
2290 @param[in] KeyString The Key of the command line argument to check for.
jcarsey94b17fa2009-05-07 18:46:18 +00002291
jcarseya405b862010-09-14 05:18:09 +00002292 @retval NULL The flag is not on the command line.
2293 @retval !=NULL The pointer to unicode string of the value.
2294**/
jcarsey94b17fa2009-05-07 18:46:18 +00002295CONST CHAR16*
2296EFIAPI
2297ShellCommandLineGetValue (
2298 IN CONST LIST_ENTRY *CheckPackage,
2299 IN CHAR16 *KeyString
jcarseya405b862010-09-14 05:18:09 +00002300 )
2301{
jcarsey94b17fa2009-05-07 18:46:18 +00002302 LIST_ENTRY *Node;
jcarsey252d9452011-03-25 20:49:53 +00002303 CHAR16 *TempString;
jcarsey94b17fa2009-05-07 18:46:18 +00002304
2305 //
ydong100c1950b2011-10-13 02:37:35 +00002306 // return NULL for no package or KeyString is NULL
jcarsey94b17fa2009-05-07 18:46:18 +00002307 //
ydong100c1950b2011-10-13 02:37:35 +00002308 if (CheckPackage == NULL || KeyString == NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00002309 return (NULL);
2310 }
2311
2312 //
2313 // enumerate through the list of parametrs
2314 //
jcarsey1e6e84c2010-01-25 20:05:08 +00002315 for ( Node = GetFirstNode(CheckPackage)
2316 ; !IsNull (CheckPackage, Node)
2317 ; Node = GetNextNode(CheckPackage, Node)
jcarsey252d9452011-03-25 20:49:53 +00002318 ){
jcarsey94b17fa2009-05-07 18:46:18 +00002319 //
jcarsey252d9452011-03-25 20:49:53 +00002320 // If the Name matches, return TRUE (and there may be NULL name)
jcarsey94b17fa2009-05-07 18:46:18 +00002321 //
2322 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {
jcarsey9eb53ac2009-07-08 17:26:58 +00002323 //
2324 // If Type is TypeStart then only compare the begining of the strings
2325 //
jcarsey252d9452011-03-25 20:49:53 +00002326 if (((SHELL_PARAM_PACKAGE*)Node)->Type == TypeStart) {
2327 if (StrnCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name, StrLen(KeyString)) == 0) {
2328 return (((SHELL_PARAM_PACKAGE*)Node)->Name + StrLen(KeyString));
2329 }
2330 TempString = NULL;
2331 TempString = StrnCatGrow(&TempString, NULL, KeyString, StrLen(((SHELL_PARAM_PACKAGE*)Node)->Name));
2332 if (TempString != NULL) {
2333 if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {
2334 FreePool(TempString);
2335 return (((SHELL_PARAM_PACKAGE*)Node)->Name + StrLen(KeyString));
2336 }
2337 FreePool(TempString);
2338 }
2339 } else if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {
jcarsey94b17fa2009-05-07 18:46:18 +00002340 return (((SHELL_PARAM_PACKAGE*)Node)->Value);
2341 }
2342 }
2343 }
2344 return (NULL);
2345}
jcarseya405b862010-09-14 05:18:09 +00002346
jcarsey94b17fa2009-05-07 18:46:18 +00002347/**
jcarseya405b862010-09-14 05:18:09 +00002348 Returns raw value from command line argument.
jcarsey94b17fa2009-05-07 18:46:18 +00002349
jcarseya405b862010-09-14 05:18:09 +00002350 Raw value parameters are in the form of "value" in a specific position in the list.
jcarsey1e6e84c2010-01-25 20:05:08 +00002351
jcarseya405b862010-09-14 05:18:09 +00002352 If CheckPackage is NULL, then return NULL.
jcarsey94b17fa2009-05-07 18:46:18 +00002353
jcarseya405b862010-09-14 05:18:09 +00002354 @param[in] CheckPackage The package of parsed command line arguments.
2355 @param[in] Position The position of the value.
jcarsey94b17fa2009-05-07 18:46:18 +00002356
jcarseya405b862010-09-14 05:18:09 +00002357 @retval NULL The flag is not on the command line.
2358 @retval !=NULL The pointer to unicode string of the value.
jcarsey94b17fa2009-05-07 18:46:18 +00002359 **/
2360CONST CHAR16*
2361EFIAPI
2362ShellCommandLineGetRawValue (
jcarseya405b862010-09-14 05:18:09 +00002363 IN CONST LIST_ENTRY * CONST CheckPackage,
2364 IN UINTN Position
2365 )
2366{
jcarsey94b17fa2009-05-07 18:46:18 +00002367 LIST_ENTRY *Node;
2368
2369 //
2370 // check for CheckPackage == NULL
2371 //
2372 if (CheckPackage == NULL) {
2373 return (NULL);
2374 }
2375
2376 //
2377 // enumerate through the list of parametrs
2378 //
jcarsey1e6e84c2010-01-25 20:05:08 +00002379 for ( Node = GetFirstNode(CheckPackage)
2380 ; !IsNull (CheckPackage, Node)
2381 ; Node = GetNextNode(CheckPackage, Node)
jcarseya405b862010-09-14 05:18:09 +00002382 ){
jcarsey94b17fa2009-05-07 18:46:18 +00002383 //
2384 // If the position matches, return the value
2385 //
2386 if (((SHELL_PARAM_PACKAGE*)Node)->OriginalPosition == Position) {
2387 return (((SHELL_PARAM_PACKAGE*)Node)->Value);
2388 }
2389 }
2390 return (NULL);
jcarseyb1f95a02009-06-16 00:23:19 +00002391}
jcarsey2247dde2009-11-09 18:08:58 +00002392
2393/**
jcarsey1e6e84c2010-01-25 20:05:08 +00002394 returns the number of command line value parameters that were parsed.
2395
jcarsey2247dde2009-11-09 18:08:58 +00002396 this will not include flags.
2397
jcarseya405b862010-09-14 05:18:09 +00002398 @param[in] CheckPackage The package of parsed command line arguments.
2399
jcarsey2247dde2009-11-09 18:08:58 +00002400 @retval (UINTN)-1 No parsing has ocurred
2401 @return other The number of value parameters found
2402**/
2403UINTN
2404EFIAPI
2405ShellCommandLineGetCount(
jcarseya405b862010-09-14 05:18:09 +00002406 IN CONST LIST_ENTRY *CheckPackage
jcarsey125c2cf2009-11-18 21:36:50 +00002407 )
2408{
jcarseya405b862010-09-14 05:18:09 +00002409 LIST_ENTRY *Node1;
2410 UINTN Count;
2411
2412 if (CheckPackage == NULL) {
2413 return (0);
2414 }
2415 for ( Node1 = GetFirstNode(CheckPackage), Count = 0
2416 ; !IsNull (CheckPackage, Node1)
2417 ; Node1 = GetNextNode(CheckPackage, Node1)
2418 ){
2419 if (((SHELL_PARAM_PACKAGE*)Node1)->Name == NULL) {
2420 Count++;
2421 }
2422 }
2423 return (Count);
jcarsey2247dde2009-11-09 18:08:58 +00002424}
2425
jcarsey975136a2009-06-16 19:03:54 +00002426/**
jcarsey36a9d672009-11-20 21:13:41 +00002427 Determins if a parameter is duplicated.
2428
jcarsey1e6e84c2010-01-25 20:05:08 +00002429 If Param is not NULL then it will point to a callee allocated string buffer
jcarsey36a9d672009-11-20 21:13:41 +00002430 with the parameter value if a duplicate is found.
2431
2432 If CheckPackage is NULL, then ASSERT.
2433
2434 @param[in] CheckPackage The package of parsed command line arguments.
2435 @param[out] Param Upon finding one, a pointer to the duplicated parameter.
2436
2437 @retval EFI_SUCCESS No parameters were duplicated.
2438 @retval EFI_DEVICE_ERROR A duplicate was found.
2439 **/
2440EFI_STATUS
2441EFIAPI
2442ShellCommandLineCheckDuplicate (
2443 IN CONST LIST_ENTRY *CheckPackage,
2444 OUT CHAR16 **Param
2445 )
2446{
2447 LIST_ENTRY *Node1;
2448 LIST_ENTRY *Node2;
jcarsey1e6e84c2010-01-25 20:05:08 +00002449
jcarsey36a9d672009-11-20 21:13:41 +00002450 ASSERT(CheckPackage != NULL);
2451
jcarsey1e6e84c2010-01-25 20:05:08 +00002452 for ( Node1 = GetFirstNode(CheckPackage)
2453 ; !IsNull (CheckPackage, Node1)
2454 ; Node1 = GetNextNode(CheckPackage, Node1)
jcarseya405b862010-09-14 05:18:09 +00002455 ){
jcarsey1e6e84c2010-01-25 20:05:08 +00002456 for ( Node2 = GetNextNode(CheckPackage, Node1)
2457 ; !IsNull (CheckPackage, Node2)
2458 ; Node2 = GetNextNode(CheckPackage, Node2)
jcarseya405b862010-09-14 05:18:09 +00002459 ){
2460 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 +00002461 if (Param != NULL) {
2462 *Param = NULL;
2463 *Param = StrnCatGrow(Param, NULL, ((SHELL_PARAM_PACKAGE*)Node1)->Name, 0);
2464 }
2465 return (EFI_DEVICE_ERROR);
2466 }
2467 }
2468 }
2469 return (EFI_SUCCESS);
2470}
2471
2472/**
jcarsey1e6e84c2010-01-25 20:05:08 +00002473 This is a find and replace function. Upon successful return the NewString is a copy of
jcarsey975136a2009-06-16 19:03:54 +00002474 SourceString with each instance of FindTarget replaced with ReplaceWith.
2475
jcarseyb3011f42010-01-11 21:49:04 +00002476 If SourceString and NewString overlap the behavior is undefined.
2477
jcarsey975136a2009-06-16 19:03:54 +00002478 If the string would grow bigger than NewSize it will halt and return error.
2479
ydong104ff7e372011-09-02 08:05:34 +00002480 @param[in] SourceString The string with source buffer.
2481 @param[in, out] NewString The string with resultant buffer.
2482 @param[in] NewSize The size in bytes of NewString.
2483 @param[in] FindTarget The string to look for.
2484 @param[in] ReplaceWith The string to replace FindTarget with.
2485 @param[in] SkipPreCarrot If TRUE will skip a FindTarget that has a '^'
2486 immediately before it.
2487 @param[in] ParameterReplacing If TRUE will add "" around items with spaces.
jcarsey975136a2009-06-16 19:03:54 +00002488
jcarsey969c7832010-01-13 16:46:33 +00002489 @retval EFI_INVALID_PARAMETER SourceString was NULL.
2490 @retval EFI_INVALID_PARAMETER NewString was NULL.
2491 @retval EFI_INVALID_PARAMETER FindTarget was NULL.
2492 @retval EFI_INVALID_PARAMETER ReplaceWith was NULL.
2493 @retval EFI_INVALID_PARAMETER FindTarget had length < 1.
2494 @retval EFI_INVALID_PARAMETER SourceString had length < 1.
jcarsey1e6e84c2010-01-25 20:05:08 +00002495 @retval EFI_BUFFER_TOO_SMALL NewSize was less than the minimum size to hold
jcarsey969c7832010-01-13 16:46:33 +00002496 the new string (truncation occurred).
jcarseya405b862010-09-14 05:18:09 +00002497 @retval EFI_SUCCESS The string was successfully copied with replacement.
jcarsey975136a2009-06-16 19:03:54 +00002498**/
jcarsey975136a2009-06-16 19:03:54 +00002499EFI_STATUS
2500EFIAPI
jcarseya405b862010-09-14 05:18:09 +00002501ShellCopySearchAndReplace(
jcarsey975136a2009-06-16 19:03:54 +00002502 IN CHAR16 CONST *SourceString,
jcarseya405b862010-09-14 05:18:09 +00002503 IN OUT CHAR16 *NewString,
jcarsey975136a2009-06-16 19:03:54 +00002504 IN UINTN NewSize,
2505 IN CONST CHAR16 *FindTarget,
jcarsey969c7832010-01-13 16:46:33 +00002506 IN CONST CHAR16 *ReplaceWith,
jcarseya405b862010-09-14 05:18:09 +00002507 IN CONST BOOLEAN SkipPreCarrot,
2508 IN CONST BOOLEAN ParameterReplacing
jcarsey1e6e84c2010-01-25 20:05:08 +00002509 )
jcarsey2247dde2009-11-09 18:08:58 +00002510{
jcarsey01582942009-07-10 19:46:17 +00002511 UINTN Size;
jcarseya405b862010-09-14 05:18:09 +00002512 CHAR16 *Replace;
2513
jcarsey975136a2009-06-16 19:03:54 +00002514 if ( (SourceString == NULL)
2515 || (NewString == NULL)
2516 || (FindTarget == NULL)
2517 || (ReplaceWith == NULL)
2518 || (StrLen(FindTarget) < 1)
2519 || (StrLen(SourceString) < 1)
jcarseya405b862010-09-14 05:18:09 +00002520 ){
jcarsey975136a2009-06-16 19:03:54 +00002521 return (EFI_INVALID_PARAMETER);
2522 }
jcarseya405b862010-09-14 05:18:09 +00002523 Replace = NULL;
2524 if (StrStr(ReplaceWith, L" ") == NULL || !ParameterReplacing) {
2525 Replace = StrnCatGrow(&Replace, NULL, ReplaceWith, 0);
2526 } else {
2527 Replace = AllocateZeroPool(StrSize(ReplaceWith) + 2*sizeof(CHAR16));
jcarseybeab0fc2011-10-10 17:26:25 +00002528 if (Replace != NULL) {
2529 UnicodeSPrint(Replace, StrSize(ReplaceWith) + 2*sizeof(CHAR16), L"\"%s\"", ReplaceWith);
2530 }
jcarseya405b862010-09-14 05:18:09 +00002531 }
jcarsey3e082d52010-10-04 16:44:57 +00002532 if (Replace == NULL) {
2533 return (EFI_OUT_OF_RESOURCES);
2534 }
jcarsey2247dde2009-11-09 18:08:58 +00002535 NewString = SetMem16(NewString, NewSize, CHAR_NULL);
2536 while (*SourceString != CHAR_NULL) {
jcarsey969c7832010-01-13 16:46:33 +00002537 //
jcarseya405b862010-09-14 05:18:09 +00002538 // if we find the FindTarget and either Skip == FALSE or Skip and we
jcarsey969c7832010-01-13 16:46:33 +00002539 // dont have a carrot do a replace...
2540 //
jcarsey1e6e84c2010-01-25 20:05:08 +00002541 if (StrnCmp(SourceString, FindTarget, StrLen(FindTarget)) == 0
jcarseya405b862010-09-14 05:18:09 +00002542 && ((SkipPreCarrot && *(SourceString-1) != L'^') || !SkipPreCarrot)
2543 ){
jcarsey975136a2009-06-16 19:03:54 +00002544 SourceString += StrLen(FindTarget);
jcarsey01582942009-07-10 19:46:17 +00002545 Size = StrSize(NewString);
jcarseya405b862010-09-14 05:18:09 +00002546 if ((Size + (StrLen(Replace)*sizeof(CHAR16))) > NewSize) {
2547 FreePool(Replace);
jcarsey975136a2009-06-16 19:03:54 +00002548 return (EFI_BUFFER_TOO_SMALL);
2549 }
jcarseya405b862010-09-14 05:18:09 +00002550 StrCat(NewString, Replace);
jcarsey975136a2009-06-16 19:03:54 +00002551 } else {
jcarsey01582942009-07-10 19:46:17 +00002552 Size = StrSize(NewString);
2553 if (Size + sizeof(CHAR16) > NewSize) {
jcarseya405b862010-09-14 05:18:09 +00002554 FreePool(Replace);
jcarsey975136a2009-06-16 19:03:54 +00002555 return (EFI_BUFFER_TOO_SMALL);
2556 }
2557 StrnCat(NewString, SourceString, 1);
2558 SourceString++;
2559 }
2560 }
jcarseya405b862010-09-14 05:18:09 +00002561 FreePool(Replace);
jcarsey975136a2009-06-16 19:03:54 +00002562 return (EFI_SUCCESS);
2563}
jcarseyb1f95a02009-06-16 00:23:19 +00002564
2565/**
jcarseye2f82972009-12-01 05:40:24 +00002566 Internal worker function to output a string.
2567
2568 This function will output a string to the correct StdOut.
2569
2570 @param[in] String The string to print out.
2571
2572 @retval EFI_SUCCESS The operation was sucessful.
2573 @retval !EFI_SUCCESS The operation failed.
2574**/
2575EFI_STATUS
2576EFIAPI
2577InternalPrintTo (
2578 IN CONST CHAR16 *String
2579 )
2580{
2581 UINTN Size;
2582 Size = StrSize(String) - sizeof(CHAR16);
jcarseya405b862010-09-14 05:18:09 +00002583 if (Size == 0) {
2584 return (EFI_SUCCESS);
2585 }
jcarsey366f81a2011-06-27 21:04:22 +00002586 if (gEfiShellParametersProtocol != NULL) {
2587 return (gEfiShellProtocol->WriteFile(gEfiShellParametersProtocol->StdOut, &Size, (VOID*)String));
jcarseye2f82972009-12-01 05:40:24 +00002588 }
2589 if (mEfiShellInterface != NULL) {
jcarseyecd3d592009-12-07 18:05:00 +00002590 //
2591 // Divide in half for old shell. Must be string length not size.
2592 //
2593 Size /= 2;
jcarseya405b862010-09-14 05:18:09 +00002594 return (mEfiShellInterface->StdOut->Write(mEfiShellInterface->StdOut, &Size, (VOID*)String));
jcarseye2f82972009-12-01 05:40:24 +00002595 }
2596 ASSERT(FALSE);
2597 return (EFI_UNSUPPORTED);
2598}
2599
2600/**
jcarseyb1f95a02009-06-16 00:23:19 +00002601 Print at a specific location on the screen.
2602
jcarseyf1b87e72009-06-17 00:52:11 +00002603 This function will move the cursor to a given screen location and print the specified string
jcarsey1e6e84c2010-01-25 20:05:08 +00002604
2605 If -1 is specified for either the Row or Col the current screen location for BOTH
jcarseyf1b87e72009-06-17 00:52:11 +00002606 will be used.
jcarseyb1f95a02009-06-16 00:23:19 +00002607
2608 if either Row or Col is out of range for the current console, then ASSERT
2609 if Format is NULL, then ASSERT
2610
jcarsey1e6e84c2010-01-25 20:05:08 +00002611 In addition to the standard %-based flags as supported by UefiLib Print() this supports
jcarseyb1f95a02009-06-16 00:23:19 +00002612 the following additional flags:
2613 %N - Set output attribute to normal
2614 %H - Set output attribute to highlight
2615 %E - Set output attribute to error
2616 %B - Set output attribute to blue color
2617 %V - Set output attribute to green color
2618
2619 Note: The background color is controlled by the shell command cls.
2620
jcarseyb1f95a02009-06-16 00:23:19 +00002621 @param[in] Col the column to print at
jcarsey252d9452011-03-25 20:49:53 +00002622 @param[in] Row the row to print at
jcarseyb1f95a02009-06-16 00:23:19 +00002623 @param[in] Format the format string
jcarsey2247dde2009-11-09 18:08:58 +00002624 @param[in] Marker the marker for the variable argument list
jcarseyb1f95a02009-06-16 00:23:19 +00002625
jcarseya405b862010-09-14 05:18:09 +00002626 @return EFI_SUCCESS The operation was successful.
2627 @return EFI_DEVICE_ERROR The console device reported an error.
jcarseyb1f95a02009-06-16 00:23:19 +00002628**/
jcarseya405b862010-09-14 05:18:09 +00002629EFI_STATUS
jcarseyb1f95a02009-06-16 00:23:19 +00002630EFIAPI
jcarsey2247dde2009-11-09 18:08:58 +00002631InternalShellPrintWorker(
jcarseyb1f95a02009-06-16 00:23:19 +00002632 IN INT32 Col OPTIONAL,
2633 IN INT32 Row OPTIONAL,
2634 IN CONST CHAR16 *Format,
jcarsey252d9452011-03-25 20:49:53 +00002635 IN VA_LIST Marker
jcarsey1e6e84c2010-01-25 20:05:08 +00002636 )
jcarsey2247dde2009-11-09 18:08:58 +00002637{
jcarseyb1f95a02009-06-16 00:23:19 +00002638 EFI_STATUS Status;
jcarsey975136a2009-06-16 19:03:54 +00002639 CHAR16 *ResumeLocation;
2640 CHAR16 *FormatWalker;
jcarseya405b862010-09-14 05:18:09 +00002641 UINTN OriginalAttribute;
jcarsey89e85372011-04-13 23:37:50 +00002642 CHAR16 *mPostReplaceFormat;
2643 CHAR16 *mPostReplaceFormat2;
2644
2645 mPostReplaceFormat = AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize));
2646 mPostReplaceFormat2 = AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize));
jcarseya405b862010-09-14 05:18:09 +00002647
jcarseyf8d3e682011-04-19 17:54:42 +00002648 if (mPostReplaceFormat == NULL || mPostReplaceFormat2 == NULL) {
2649 SHELL_FREE_NON_NULL(mPostReplaceFormat);
2650 SHELL_FREE_NON_NULL(mPostReplaceFormat2);
2651 return (EFI_OUT_OF_RESOURCES);
2652 }
2653
jcarseya405b862010-09-14 05:18:09 +00002654 Status = EFI_SUCCESS;
2655 OriginalAttribute = gST->ConOut->Mode->Attribute;
jcarsey1e6e84c2010-01-25 20:05:08 +00002656
jcarsey975136a2009-06-16 19:03:54 +00002657 //
2658 // Back and forth each time fixing up 1 of our flags...
2659 //
jcarseya405b862010-09-14 05:18:09 +00002660 Status = ShellCopySearchAndReplace(Format, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%N", L"%%N", FALSE, FALSE);
jcarsey975136a2009-06-16 19:03:54 +00002661 ASSERT_EFI_ERROR(Status);
jcarseya405b862010-09-14 05:18:09 +00002662 Status = ShellCopySearchAndReplace(mPostReplaceFormat, mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), L"%E", L"%%E", FALSE, FALSE);
jcarsey975136a2009-06-16 19:03:54 +00002663 ASSERT_EFI_ERROR(Status);
jcarseya405b862010-09-14 05:18:09 +00002664 Status = ShellCopySearchAndReplace(mPostReplaceFormat2, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%H", L"%%H", FALSE, FALSE);
jcarsey975136a2009-06-16 19:03:54 +00002665 ASSERT_EFI_ERROR(Status);
jcarseya405b862010-09-14 05:18:09 +00002666 Status = ShellCopySearchAndReplace(mPostReplaceFormat, mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), L"%B", L"%%B", FALSE, FALSE);
jcarsey975136a2009-06-16 19:03:54 +00002667 ASSERT_EFI_ERROR(Status);
jcarseya405b862010-09-14 05:18:09 +00002668 Status = ShellCopySearchAndReplace(mPostReplaceFormat2, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%V", L"%%V", FALSE, FALSE);
jcarsey975136a2009-06-16 19:03:54 +00002669 ASSERT_EFI_ERROR(Status);
2670
2671 //
2672 // Use the last buffer from replacing to print from...
2673 //
jcarseya405b862010-09-14 05:18:09 +00002674 UnicodeVSPrint (mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), mPostReplaceFormat, Marker);
jcarseyb1f95a02009-06-16 00:23:19 +00002675
2676 if (Col != -1 && Row != -1) {
jcarseyb1f95a02009-06-16 00:23:19 +00002677 Status = gST->ConOut->SetCursorPosition(gST->ConOut, Col, Row);
jcarsey975136a2009-06-16 19:03:54 +00002678 }
2679
jcarseyecd3d592009-12-07 18:05:00 +00002680 FormatWalker = mPostReplaceFormat2;
jcarsey2247dde2009-11-09 18:08:58 +00002681 while (*FormatWalker != CHAR_NULL) {
jcarsey975136a2009-06-16 19:03:54 +00002682 //
2683 // Find the next attribute change request
2684 //
2685 ResumeLocation = StrStr(FormatWalker, L"%");
2686 if (ResumeLocation != NULL) {
jcarsey2247dde2009-11-09 18:08:58 +00002687 *ResumeLocation = CHAR_NULL;
jcarsey975136a2009-06-16 19:03:54 +00002688 }
2689 //
2690 // print the current FormatWalker string
2691 //
jcarseya405b862010-09-14 05:18:09 +00002692 if (StrLen(FormatWalker)>0) {
2693 Status = InternalPrintTo(FormatWalker);
2694 if (EFI_ERROR(Status)) {
2695 break;
2696 }
2697 }
2698
jcarsey975136a2009-06-16 19:03:54 +00002699 //
2700 // update the attribute
2701 //
2702 if (ResumeLocation != NULL) {
jcarsey5d46f172011-08-23 15:34:23 +00002703 if (*(ResumeLocation-1) == L'^') {
2704 //
2705 // Print a simple '%' symbol
2706 //
2707 Status = InternalPrintTo(L"%");
2708 ResumeLocation = ResumeLocation - 1;
2709 } else {
2710 switch (*(ResumeLocation+1)) {
2711 case (L'N'):
2712 gST->ConOut->SetAttribute(gST->ConOut, OriginalAttribute);
jcarseya405b862010-09-14 05:18:09 +00002713 break;
jcarsey5d46f172011-08-23 15:34:23 +00002714 case (L'E'):
2715 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_YELLOW, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));
2716 break;
2717 case (L'H'):
2718 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_WHITE, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));
2719 break;
2720 case (L'B'):
2721 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_BLUE, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));
2722 break;
2723 case (L'V'):
2724 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_GREEN, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));
2725 break;
2726 default:
2727 //
2728 // Print a simple '%' symbol
2729 //
2730 Status = InternalPrintTo(L"%");
2731 if (EFI_ERROR(Status)) {
2732 break;
2733 }
2734 ResumeLocation = ResumeLocation - 1;
2735 break;
2736 }
jcarsey975136a2009-06-16 19:03:54 +00002737 }
2738 } else {
2739 //
2740 // reset to normal now...
2741 //
jcarsey975136a2009-06-16 19:03:54 +00002742 break;
2743 }
2744
2745 //
2746 // update FormatWalker to Resume + 2 (skip the % and the indicator)
2747 //
2748 FormatWalker = ResumeLocation + 2;
2749 }
jcarseyb1f95a02009-06-16 00:23:19 +00002750
jcarseya405b862010-09-14 05:18:09 +00002751 gST->ConOut->SetAttribute(gST->ConOut, OriginalAttribute);
jcarsey89e85372011-04-13 23:37:50 +00002752
2753 SHELL_FREE_NON_NULL(mPostReplaceFormat);
2754 SHELL_FREE_NON_NULL(mPostReplaceFormat2);
jcarseya405b862010-09-14 05:18:09 +00002755 return (Status);
jcarsey5f7431d2009-07-10 18:06:01 +00002756}
jcarsey2247dde2009-11-09 18:08:58 +00002757
2758/**
2759 Print at a specific location on the screen.
2760
jcarseye2f82972009-12-01 05:40:24 +00002761 This function will move the cursor to a given screen location and print the specified string.
jcarsey1e6e84c2010-01-25 20:05:08 +00002762
2763 If -1 is specified for either the Row or Col the current screen location for BOTH
jcarsey2247dde2009-11-09 18:08:58 +00002764 will be used.
2765
jcarseye2f82972009-12-01 05:40:24 +00002766 If either Row or Col is out of range for the current console, then ASSERT.
2767 If Format is NULL, then ASSERT.
jcarsey2247dde2009-11-09 18:08:58 +00002768
jcarsey1e6e84c2010-01-25 20:05:08 +00002769 In addition to the standard %-based flags as supported by UefiLib Print() this supports
jcarsey2247dde2009-11-09 18:08:58 +00002770 the following additional flags:
2771 %N - Set output attribute to normal
2772 %H - Set output attribute to highlight
2773 %E - Set output attribute to error
2774 %B - Set output attribute to blue color
2775 %V - Set output attribute to green color
2776
2777 Note: The background color is controlled by the shell command cls.
2778
jcarsey2247dde2009-11-09 18:08:58 +00002779 @param[in] Col the column to print at
jcarseya405b862010-09-14 05:18:09 +00002780 @param[in] Row the row to print at
jcarsey2247dde2009-11-09 18:08:58 +00002781 @param[in] Format the format string
jcarseya405b862010-09-14 05:18:09 +00002782 @param[in] ... The variable argument list.
jcarsey2247dde2009-11-09 18:08:58 +00002783
jcarseya405b862010-09-14 05:18:09 +00002784 @return EFI_SUCCESS The printing was successful.
2785 @return EFI_DEVICE_ERROR The console device reported an error.
jcarsey2247dde2009-11-09 18:08:58 +00002786**/
jcarseya405b862010-09-14 05:18:09 +00002787EFI_STATUS
jcarsey2247dde2009-11-09 18:08:58 +00002788EFIAPI
2789ShellPrintEx(
2790 IN INT32 Col OPTIONAL,
2791 IN INT32 Row OPTIONAL,
2792 IN CONST CHAR16 *Format,
2793 ...
jcarsey1e6e84c2010-01-25 20:05:08 +00002794 )
jcarsey2247dde2009-11-09 18:08:58 +00002795{
2796 VA_LIST Marker;
jcarseya405b862010-09-14 05:18:09 +00002797 EFI_STATUS RetVal;
jcarsey3e082d52010-10-04 16:44:57 +00002798 if (Format == NULL) {
2799 return (EFI_INVALID_PARAMETER);
2800 }
jcarsey2247dde2009-11-09 18:08:58 +00002801 VA_START (Marker, Format);
jcarseya405b862010-09-14 05:18:09 +00002802 RetVal = InternalShellPrintWorker(Col, Row, Format, Marker);
jcarseye2f82972009-12-01 05:40:24 +00002803 VA_END(Marker);
jcarseya405b862010-09-14 05:18:09 +00002804 return(RetVal);
jcarsey2247dde2009-11-09 18:08:58 +00002805}
2806
2807/**
2808 Print at a specific location on the screen.
2809
jcarseye2f82972009-12-01 05:40:24 +00002810 This function will move the cursor to a given screen location and print the specified string.
jcarsey1e6e84c2010-01-25 20:05:08 +00002811
2812 If -1 is specified for either the Row or Col the current screen location for BOTH
jcarseye2f82972009-12-01 05:40:24 +00002813 will be used.
jcarsey2247dde2009-11-09 18:08:58 +00002814
jcarseye2f82972009-12-01 05:40:24 +00002815 If either Row or Col is out of range for the current console, then ASSERT.
2816 If Format is NULL, then ASSERT.
jcarsey2247dde2009-11-09 18:08:58 +00002817
jcarsey1e6e84c2010-01-25 20:05:08 +00002818 In addition to the standard %-based flags as supported by UefiLib Print() this supports
jcarsey2247dde2009-11-09 18:08:58 +00002819 the following additional flags:
jcarsey1e6e84c2010-01-25 20:05:08 +00002820 %N - Set output attribute to normal.
2821 %H - Set output attribute to highlight.
2822 %E - Set output attribute to error.
2823 %B - Set output attribute to blue color.
2824 %V - Set output attribute to green color.
jcarsey2247dde2009-11-09 18:08:58 +00002825
2826 Note: The background color is controlled by the shell command cls.
2827
jcarsey1e6e84c2010-01-25 20:05:08 +00002828 @param[in] Col The column to print at.
jcarseya405b862010-09-14 05:18:09 +00002829 @param[in] Row The row to print at.
jcarsey1e6e84c2010-01-25 20:05:08 +00002830 @param[in] Language The language of the string to retrieve. If this parameter
2831 is NULL, then the current platform language is used.
2832 @param[in] HiiFormatStringId The format string Id for getting from Hii.
2833 @param[in] HiiFormatHandle The format string Handle for getting from Hii.
jcarseya405b862010-09-14 05:18:09 +00002834 @param[in] ... The variable argument list.
jcarsey2247dde2009-11-09 18:08:58 +00002835
jcarseya405b862010-09-14 05:18:09 +00002836 @return EFI_SUCCESS The printing was successful.
2837 @return EFI_DEVICE_ERROR The console device reported an error.
jcarsey2247dde2009-11-09 18:08:58 +00002838**/
jcarseya405b862010-09-14 05:18:09 +00002839EFI_STATUS
jcarsey2247dde2009-11-09 18:08:58 +00002840EFIAPI
2841ShellPrintHiiEx(
2842 IN INT32 Col OPTIONAL,
2843 IN INT32 Row OPTIONAL,
jcarsey1e6e84c2010-01-25 20:05:08 +00002844 IN CONST CHAR8 *Language OPTIONAL,
jcarsey2247dde2009-11-09 18:08:58 +00002845 IN CONST EFI_STRING_ID HiiFormatStringId,
2846 IN CONST EFI_HANDLE HiiFormatHandle,
2847 ...
2848 )
2849{
2850 VA_LIST Marker;
2851 CHAR16 *HiiFormatString;
jcarseya405b862010-09-14 05:18:09 +00002852 EFI_STATUS RetVal;
jcarsey2247dde2009-11-09 18:08:58 +00002853
2854 VA_START (Marker, HiiFormatHandle);
jcarsey1e6e84c2010-01-25 20:05:08 +00002855 HiiFormatString = HiiGetString(HiiFormatHandle, HiiFormatStringId, Language);
jcarsey2247dde2009-11-09 18:08:58 +00002856 ASSERT(HiiFormatString != NULL);
2857
2858 RetVal = InternalShellPrintWorker(Col, Row, HiiFormatString, Marker);
2859
jcarseya405b862010-09-14 05:18:09 +00002860 SHELL_FREE_NON_NULL(HiiFormatString);
jcarseye2f82972009-12-01 05:40:24 +00002861 VA_END(Marker);
jcarsey2247dde2009-11-09 18:08:58 +00002862
2863 return (RetVal);
2864}
2865
2866/**
2867 Function to determine if a given filename represents a file or a directory.
2868
2869 @param[in] DirName Path to directory to test.
2870
jcarseyc8c22592011-10-17 17:49:21 +00002871 @retval EFI_SUCCESS The Path represents a directory
2872 @retval EFI_NOT_FOUND The Path does not represent a directory
2873 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
2874 @return The path failed to open
jcarsey2247dde2009-11-09 18:08:58 +00002875**/
2876EFI_STATUS
2877EFIAPI
2878ShellIsDirectory(
2879 IN CONST CHAR16 *DirName
2880 )
2881{
2882 EFI_STATUS Status;
jcarseya405b862010-09-14 05:18:09 +00002883 SHELL_FILE_HANDLE Handle;
jcarsey3e082d52010-10-04 16:44:57 +00002884 CHAR16 *TempLocation;
2885 CHAR16 *TempLocation2;
jcarsey2247dde2009-11-09 18:08:58 +00002886
jcarseyecd3d592009-12-07 18:05:00 +00002887 ASSERT(DirName != NULL);
2888
jcarseya405b862010-09-14 05:18:09 +00002889 Handle = NULL;
2890 TempLocation = NULL;
jcarsey2247dde2009-11-09 18:08:58 +00002891
2892 Status = ShellOpenFileByName(DirName, &Handle, EFI_FILE_MODE_READ, 0);
2893 if (EFI_ERROR(Status)) {
jcarseya405b862010-09-14 05:18:09 +00002894 //
2895 // try good logic first.
2896 //
jcarsey366f81a2011-06-27 21:04:22 +00002897 if (gEfiShellProtocol != NULL) {
jcarsey3e082d52010-10-04 16:44:57 +00002898 TempLocation = StrnCatGrow(&TempLocation, NULL, DirName, 0);
jcarseyc8c22592011-10-17 17:49:21 +00002899 if (TempLocation == NULL) {
2900 ShellCloseFile(&Handle);
2901 return (EFI_OUT_OF_RESOURCES);
2902 }
jcarsey3e082d52010-10-04 16:44:57 +00002903 TempLocation2 = StrStr(TempLocation, L":");
2904 if (TempLocation2 != NULL && StrLen(StrStr(TempLocation, L":")) == 2) {
2905 *(TempLocation2+1) = CHAR_NULL;
jcarseya405b862010-09-14 05:18:09 +00002906 }
jcarsey366f81a2011-06-27 21:04:22 +00002907 if (gEfiShellProtocol->GetDevicePathFromMap(TempLocation) != NULL) {
jcarseya405b862010-09-14 05:18:09 +00002908 FreePool(TempLocation);
2909 return (EFI_SUCCESS);
2910 }
2911 FreePool(TempLocation);
2912 } else {
2913 //
2914 // probably a map name?!?!!?
2915 //
2916 TempLocation = StrStr(DirName, L"\\");
2917 if (TempLocation != NULL && *(TempLocation+1) == CHAR_NULL) {
2918 return (EFI_SUCCESS);
2919 }
2920 }
jcarsey2247dde2009-11-09 18:08:58 +00002921 return (Status);
2922 }
2923
2924 if (FileHandleIsDirectory(Handle) == EFI_SUCCESS) {
2925 ShellCloseFile(&Handle);
2926 return (EFI_SUCCESS);
2927 }
2928 ShellCloseFile(&Handle);
2929 return (EFI_NOT_FOUND);
2930}
2931
jcarsey125c2cf2009-11-18 21:36:50 +00002932/**
jcarsey36a9d672009-11-20 21:13:41 +00002933 Function to determine if a given filename represents a file.
2934
2935 @param[in] Name Path to file to test.
2936
2937 @retval EFI_SUCCESS The Path represents a file.
2938 @retval EFI_NOT_FOUND The Path does not represent a file.
2939 @retval other The path failed to open.
2940**/
2941EFI_STATUS
2942EFIAPI
2943ShellIsFile(
2944 IN CONST CHAR16 *Name
2945 )
2946{
2947 EFI_STATUS Status;
jcarseya405b862010-09-14 05:18:09 +00002948 SHELL_FILE_HANDLE Handle;
jcarsey36a9d672009-11-20 21:13:41 +00002949
jcarseyecd3d592009-12-07 18:05:00 +00002950 ASSERT(Name != NULL);
2951
jcarsey36a9d672009-11-20 21:13:41 +00002952 Handle = NULL;
2953
2954 Status = ShellOpenFileByName(Name, &Handle, EFI_FILE_MODE_READ, 0);
2955 if (EFI_ERROR(Status)) {
2956 return (Status);
2957 }
2958
2959 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {
2960 ShellCloseFile(&Handle);
2961 return (EFI_SUCCESS);
2962 }
2963 ShellCloseFile(&Handle);
2964 return (EFI_NOT_FOUND);
2965}
2966
2967/**
jcarseyb3011f42010-01-11 21:49:04 +00002968 Function to determine if a given filename represents a file.
2969
2970 This will search the CWD and then the Path.
2971
2972 If Name is NULL, then ASSERT.
2973
2974 @param[in] Name Path to file to test.
2975
2976 @retval EFI_SUCCESS The Path represents a file.
2977 @retval EFI_NOT_FOUND The Path does not represent a file.
2978 @retval other The path failed to open.
2979**/
2980EFI_STATUS
2981EFIAPI
2982ShellIsFileInPath(
2983 IN CONST CHAR16 *Name
jcarseya405b862010-09-14 05:18:09 +00002984 )
2985{
jcarseyb3011f42010-01-11 21:49:04 +00002986 CHAR16 *NewName;
2987 EFI_STATUS Status;
2988
2989 if (!EFI_ERROR(ShellIsFile(Name))) {
jcarseya405b862010-09-14 05:18:09 +00002990 return (EFI_SUCCESS);
jcarseyb3011f42010-01-11 21:49:04 +00002991 }
2992
2993 NewName = ShellFindFilePath(Name);
2994 if (NewName == NULL) {
2995 return (EFI_NOT_FOUND);
2996 }
2997 Status = ShellIsFile(NewName);
2998 FreePool(NewName);
2999 return (Status);
3000}
jcarsey252d9452011-03-25 20:49:53 +00003001
jcarseyb3011f42010-01-11 21:49:04 +00003002/**
jcarsey1e6e84c2010-01-25 20:05:08 +00003003 Function to determine whether a string is decimal or hex representation of a number
jcarsey125c2cf2009-11-18 21:36:50 +00003004 and return the number converted from the string.
3005
3006 @param[in] String String representation of a number
3007
jcarsey252d9452011-03-25 20:49:53 +00003008 @return the number
3009 @retval (UINTN)(-1) An error ocurred.
jcarsey125c2cf2009-11-18 21:36:50 +00003010**/
3011UINTN
3012EFIAPI
3013ShellStrToUintn(
3014 IN CONST CHAR16 *String
3015 )
3016{
jcarsey252d9452011-03-25 20:49:53 +00003017 UINT64 RetVal;
3018 BOOLEAN Hex;
3019
3020 Hex = FALSE;
3021
3022 if (!InternalShellIsHexOrDecimalNumber(String, Hex, TRUE)) {
3023 Hex = TRUE;
jcarsey125c2cf2009-11-18 21:36:50 +00003024 }
jcarsey252d9452011-03-25 20:49:53 +00003025
3026 if (!EFI_ERROR(ShellConvertStringToUint64(String, &RetVal, Hex, TRUE))) {
3027 return ((UINTN)RetVal);
3028 }
3029 return ((UINTN)(-1));
jcarsey125c2cf2009-11-18 21:36:50 +00003030}
3031
3032/**
3033 Safely append with automatic string resizing given length of Destination and
3034 desired length of copy from Source.
3035
3036 append the first D characters of Source to the end of Destination, where D is
3037 the lesser of Count and the StrLen() of Source. If appending those D characters
3038 will fit within Destination (whose Size is given as CurrentSize) and
jcarsey1e6e84c2010-01-25 20:05:08 +00003039 still leave room for a NULL terminator, then those characters are appended,
3040 starting at the original terminating NULL of Destination, and a new terminating
3041 NULL is appended.
jcarsey125c2cf2009-11-18 21:36:50 +00003042
3043 If appending D characters onto Destination will result in a overflow of the size
3044 given in CurrentSize the string will be grown such that the copy can be performed
3045 and CurrentSize will be updated to the new size.
3046
3047 If Source is NULL, there is nothing to append, just return the current buffer in
3048 Destination.
3049
3050 if Destination is NULL, then ASSERT()
3051 if Destination's current length (including NULL terminator) is already more then
3052 CurrentSize, then ASSERT()
3053
ydong104ff7e372011-09-02 08:05:34 +00003054 @param[in, out] Destination The String to append onto
3055 @param[in, out] CurrentSize on call the number of bytes in Destination. On
jcarsey125c2cf2009-11-18 21:36:50 +00003056 return possibly the new size (still in bytes). if NULL
3057 then allocate whatever is needed.
3058 @param[in] Source The String to append from
3059 @param[in] Count Maximum number of characters to append. if 0 then
3060 all are appended.
3061
3062 @return Destination return the resultant string.
3063**/
3064CHAR16*
3065EFIAPI
3066StrnCatGrow (
3067 IN OUT CHAR16 **Destination,
3068 IN OUT UINTN *CurrentSize,
3069 IN CONST CHAR16 *Source,
3070 IN UINTN Count
3071 )
3072{
3073 UINTN DestinationStartSize;
3074 UINTN NewSize;
3075
3076 //
3077 // ASSERTs
3078 //
3079 ASSERT(Destination != NULL);
3080
3081 //
3082 // If there's nothing to do then just return Destination
3083 //
3084 if (Source == NULL) {
3085 return (*Destination);
3086 }
3087
3088 //
3089 // allow for un-initialized pointers, based on size being 0
3090 //
3091 if (CurrentSize != NULL && *CurrentSize == 0) {
3092 *Destination = NULL;
3093 }
3094
3095 //
3096 // allow for NULL pointers address as Destination
3097 //
3098 if (*Destination != NULL) {
3099 ASSERT(CurrentSize != 0);
3100 DestinationStartSize = StrSize(*Destination);
3101 ASSERT(DestinationStartSize <= *CurrentSize);
3102 } else {
3103 DestinationStartSize = 0;
3104// ASSERT(*CurrentSize == 0);
3105 }
3106
3107 //
3108 // Append all of Source?
3109 //
3110 if (Count == 0) {
3111 Count = StrLen(Source);
3112 }
3113
3114 //
3115 // Test and grow if required
3116 //
3117 if (CurrentSize != NULL) {
3118 NewSize = *CurrentSize;
3119 while (NewSize < (DestinationStartSize + (Count*sizeof(CHAR16)))) {
3120 NewSize += 2 * Count * sizeof(CHAR16);
3121 }
3122 *Destination = ReallocatePool(*CurrentSize, NewSize, *Destination);
3123 *CurrentSize = NewSize;
3124 } else {
3125 *Destination = AllocateZeroPool((Count+1)*sizeof(CHAR16));
3126 }
3127
3128 //
3129 // Now use standard StrnCat on a big enough buffer
3130 //
jcarseyc9d92df2010-02-03 15:37:54 +00003131 if (*Destination == NULL) {
3132 return (NULL);
3133 }
jcarsey125c2cf2009-11-18 21:36:50 +00003134 return StrnCat(*Destination, Source, Count);
3135}
jcarseyc9d92df2010-02-03 15:37:54 +00003136
3137/**
3138 Prompt the user and return the resultant answer to the requestor.
3139
3140 This function will display the requested question on the shell prompt and then
3141 wait for an apropriate answer to be input from the console.
3142
jcarseya405b862010-09-14 05:18:09 +00003143 if the SHELL_PROMPT_REQUEST_TYPE is SHELL_PROMPT_REQUEST_TYPE_YESNO, ShellPromptResponseTypeQuitContinue
jcarseyc9d92df2010-02-03 15:37:54 +00003144 or SHELL_PROMPT_REQUEST_TYPE_YESNOCANCEL then *Response is of type SHELL_PROMPT_RESPONSE.
3145
jcarseya405b862010-09-14 05:18:09 +00003146 if the SHELL_PROMPT_REQUEST_TYPE is ShellPromptResponseTypeFreeform then *Response is of type
jcarseyc9d92df2010-02-03 15:37:54 +00003147 CHAR16*.
3148
3149 In either case *Response must be callee freed if Response was not NULL;
3150
3151 @param Type What type of question is asked. This is used to filter the input
3152 to prevent invalid answers to question.
3153 @param Prompt Pointer to string prompt to use to request input.
3154 @param Response Pointer to Response which will be populated upon return.
3155
3156 @retval EFI_SUCCESS The operation was sucessful.
3157 @retval EFI_UNSUPPORTED The operation is not supported as requested.
3158 @retval EFI_INVALID_PARAMETER A parameter was invalid.
3159 @return other The operation failed.
3160**/
3161EFI_STATUS
3162EFIAPI
3163ShellPromptForResponse (
3164 IN SHELL_PROMPT_REQUEST_TYPE Type,
3165 IN CHAR16 *Prompt OPTIONAL,
3166 IN OUT VOID **Response OPTIONAL
3167 )
3168{
3169 EFI_STATUS Status;
3170 EFI_INPUT_KEY Key;
3171 UINTN EventIndex;
3172 SHELL_PROMPT_RESPONSE *Resp;
jcarseya405b862010-09-14 05:18:09 +00003173 UINTN Size;
3174 CHAR16 *Buffer;
jcarseyc9d92df2010-02-03 15:37:54 +00003175
jcarseya405b862010-09-14 05:18:09 +00003176 Status = EFI_UNSUPPORTED;
3177 Resp = NULL;
3178 Buffer = NULL;
3179 Size = 0;
3180 if (Type != ShellPromptResponseTypeFreeform) {
jcarsey252d9452011-03-25 20:49:53 +00003181 Resp = (SHELL_PROMPT_RESPONSE*)AllocateZeroPool(sizeof(SHELL_PROMPT_RESPONSE));
jcarsey3e082d52010-10-04 16:44:57 +00003182 if (Resp == NULL) {
3183 return (EFI_OUT_OF_RESOURCES);
3184 }
xdu290bfa222010-07-19 05:21:27 +00003185 }
jcarseyc9d92df2010-02-03 15:37:54 +00003186
3187 switch(Type) {
jcarseya405b862010-09-14 05:18:09 +00003188 case ShellPromptResponseTypeQuitContinue:
jcarseyc9d92df2010-02-03 15:37:54 +00003189 if (Prompt != NULL) {
3190 ShellPrintEx(-1, -1, L"%s", Prompt);
3191 }
3192 //
3193 // wait for valid response
3194 //
3195 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
3196 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
3197 ASSERT_EFI_ERROR(Status);
3198 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3199 if (Key.UnicodeChar == L'Q' || Key.UnicodeChar ==L'q') {
jcarseya405b862010-09-14 05:18:09 +00003200 *Resp = ShellPromptResponseQuit;
jcarseyc9d92df2010-02-03 15:37:54 +00003201 } else {
jcarseya405b862010-09-14 05:18:09 +00003202 *Resp = ShellPromptResponseContinue;
jcarseyc9d92df2010-02-03 15:37:54 +00003203 }
3204 break;
jcarseya405b862010-09-14 05:18:09 +00003205 case ShellPromptResponseTypeYesNoCancel:
jcarseyc9d92df2010-02-03 15:37:54 +00003206 if (Prompt != NULL) {
3207 ShellPrintEx(-1, -1, L"%s", Prompt);
3208 }
3209 //
3210 // wait for valid response
3211 //
jcarseya405b862010-09-14 05:18:09 +00003212 *Resp = ShellPromptResponseMax;
3213 while (*Resp == ShellPromptResponseMax) {
jcarseyc9d92df2010-02-03 15:37:54 +00003214 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
3215 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
3216 ASSERT_EFI_ERROR(Status);
3217 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3218 switch (Key.UnicodeChar) {
3219 case L'Y':
3220 case L'y':
jcarseya405b862010-09-14 05:18:09 +00003221 *Resp = ShellPromptResponseYes;
jcarseyc9d92df2010-02-03 15:37:54 +00003222 break;
3223 case L'N':
3224 case L'n':
jcarseya405b862010-09-14 05:18:09 +00003225 *Resp = ShellPromptResponseNo;
jcarseyc9d92df2010-02-03 15:37:54 +00003226 break;
3227 case L'C':
3228 case L'c':
jcarseya405b862010-09-14 05:18:09 +00003229 *Resp = ShellPromptResponseCancel;
3230 break;
3231 }
3232 }
3233 break; case ShellPromptResponseTypeYesNoAllCancel:
3234 if (Prompt != NULL) {
3235 ShellPrintEx(-1, -1, L"%s", Prompt);
3236 }
3237 //
3238 // wait for valid response
3239 //
3240 *Resp = ShellPromptResponseMax;
3241 while (*Resp == ShellPromptResponseMax) {
3242 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
3243 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
3244 ASSERT_EFI_ERROR(Status);
3245 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3246 switch (Key.UnicodeChar) {
3247 case L'Y':
3248 case L'y':
3249 *Resp = ShellPromptResponseYes;
3250 break;
3251 case L'N':
3252 case L'n':
3253 *Resp = ShellPromptResponseNo;
3254 break;
3255 case L'A':
3256 case L'a':
3257 *Resp = ShellPromptResponseAll;
3258 break;
3259 case L'C':
3260 case L'c':
3261 *Resp = ShellPromptResponseCancel;
jcarseyc9d92df2010-02-03 15:37:54 +00003262 break;
3263 }
3264 }
3265 break;
jcarseya405b862010-09-14 05:18:09 +00003266 case ShellPromptResponseTypeEnterContinue:
3267 case ShellPromptResponseTypeAnyKeyContinue:
jcarseyc9d92df2010-02-03 15:37:54 +00003268 if (Prompt != NULL) {
3269 ShellPrintEx(-1, -1, L"%s", Prompt);
3270 }
3271 //
3272 // wait for valid response
3273 //
jcarseya405b862010-09-14 05:18:09 +00003274 *Resp = ShellPromptResponseMax;
3275 while (*Resp == ShellPromptResponseMax) {
jcarseyc9d92df2010-02-03 15:37:54 +00003276 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
jcarseya405b862010-09-14 05:18:09 +00003277 if (Type == ShellPromptResponseTypeEnterContinue) {
jcarseyc9d92df2010-02-03 15:37:54 +00003278 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
3279 ASSERT_EFI_ERROR(Status);
3280 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3281 if (Key.UnicodeChar == CHAR_CARRIAGE_RETURN) {
jcarseya405b862010-09-14 05:18:09 +00003282 *Resp = ShellPromptResponseContinue;
jcarseyc9d92df2010-02-03 15:37:54 +00003283 break;
3284 }
3285 }
jcarseya405b862010-09-14 05:18:09 +00003286 if (Type == ShellPromptResponseTypeAnyKeyContinue) {
3287 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
3288 ASSERT_EFI_ERROR(Status);
3289 *Resp = ShellPromptResponseContinue;
jcarseyc9d92df2010-02-03 15:37:54 +00003290 break;
3291 }
3292 }
3293 break;
jcarseya405b862010-09-14 05:18:09 +00003294 case ShellPromptResponseTypeYesNo:
3295 if (Prompt != NULL) {
3296 ShellPrintEx(-1, -1, L"%s", Prompt);
3297 }
3298 //
3299 // wait for valid response
3300 //
3301 *Resp = ShellPromptResponseMax;
3302 while (*Resp == ShellPromptResponseMax) {
3303 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
3304 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
3305 ASSERT_EFI_ERROR(Status);
3306 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3307 switch (Key.UnicodeChar) {
3308 case L'Y':
3309 case L'y':
3310 *Resp = ShellPromptResponseYes;
3311 break;
3312 case L'N':
3313 case L'n':
3314 *Resp = ShellPromptResponseNo;
3315 break;
3316 }
3317 }
3318 break;
3319 case ShellPromptResponseTypeFreeform:
3320 if (Prompt != NULL) {
3321 ShellPrintEx(-1, -1, L"%s", Prompt);
3322 }
3323 while(1) {
3324 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
3325 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
3326 ASSERT_EFI_ERROR(Status);
3327 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3328 if (Key.UnicodeChar == CHAR_CARRIAGE_RETURN) {
3329 break;
3330 }
3331 ASSERT((Buffer == NULL && Size == 0) || (Buffer != NULL));
3332 StrnCatGrow(&Buffer, &Size, &Key.UnicodeChar, 1);
3333 }
3334 break;
3335 //
3336 // This is the location to add new prompt types.
3337 //
jcarseyc9d92df2010-02-03 15:37:54 +00003338 default:
jcarseya405b862010-09-14 05:18:09 +00003339 ASSERT(FALSE);
jcarseyc9d92df2010-02-03 15:37:54 +00003340 }
3341
3342 if (Response != NULL) {
jcarseya405b862010-09-14 05:18:09 +00003343 if (Resp != NULL) {
3344 *Response = Resp;
3345 } else if (Buffer != NULL) {
3346 *Response = Buffer;
3347 }
jcarseyc9d92df2010-02-03 15:37:54 +00003348 } else {
jcarseya405b862010-09-14 05:18:09 +00003349 if (Resp != NULL) {
3350 FreePool(Resp);
3351 }
3352 if (Buffer != NULL) {
3353 FreePool(Buffer);
3354 }
jcarseyc9d92df2010-02-03 15:37:54 +00003355 }
3356
jcarseya405b862010-09-14 05:18:09 +00003357 ShellPrintEx(-1, -1, L"\r\n");
jcarseyc9d92df2010-02-03 15:37:54 +00003358 return (Status);
3359}
3360
3361/**
3362 Prompt the user and return the resultant answer to the requestor.
3363
3364 This function is the same as ShellPromptForResponse, except that the prompt is
3365 automatically pulled from HII.
3366
3367 @param Type What type of question is asked. This is used to filter the input
3368 to prevent invalid answers to question.
jcarseya405b862010-09-14 05:18:09 +00003369 @param[in] HiiFormatStringId The format string Id for getting from Hii.
3370 @param[in] HiiFormatHandle The format string Handle for getting from Hii.
3371 @param Response Pointer to Response which will be populated upon return.
jcarseyc9d92df2010-02-03 15:37:54 +00003372
3373 @retval EFI_SUCCESS the operation was sucessful.
3374 @return other the operation failed.
3375
3376 @sa ShellPromptForResponse
3377**/
3378EFI_STATUS
3379EFIAPI
3380ShellPromptForResponseHii (
3381 IN SHELL_PROMPT_REQUEST_TYPE Type,
3382 IN CONST EFI_STRING_ID HiiFormatStringId,
3383 IN CONST EFI_HANDLE HiiFormatHandle,
3384 IN OUT VOID **Response
3385 )
3386{
3387 CHAR16 *Prompt;
3388 EFI_STATUS Status;
3389
3390 Prompt = HiiGetString(HiiFormatHandle, HiiFormatStringId, NULL);
3391 Status = ShellPromptForResponse(Type, Prompt, Response);
3392 FreePool(Prompt);
3393 return (Status);
3394}
3395
jcarseya405b862010-09-14 05:18:09 +00003396/**
3397 Function to determin if an entire string is a valid number.
jcarseyc9d92df2010-02-03 15:37:54 +00003398
jcarseya405b862010-09-14 05:18:09 +00003399 If Hex it must be preceeded with a 0x or has ForceHex, set TRUE.
3400
3401 @param[in] String The string to evaluate.
3402 @param[in] ForceHex TRUE - always assume hex.
3403 @param[in] StopAtSpace TRUE to halt upon finding a space, FALSE to keep going.
3404
3405 @retval TRUE It is all numeric (dec/hex) characters.
3406 @retval FALSE There is a non-numeric character.
3407**/
3408BOOLEAN
3409EFIAPI
jcarsey252d9452011-03-25 20:49:53 +00003410InternalShellIsHexOrDecimalNumber (
jcarseya405b862010-09-14 05:18:09 +00003411 IN CONST CHAR16 *String,
3412 IN CONST BOOLEAN ForceHex,
3413 IN CONST BOOLEAN StopAtSpace
3414 )
3415{
3416 BOOLEAN Hex;
3417
3418 //
3419 // chop off a single negative sign
3420 //
3421 if (String != NULL && *String == L'-') {
3422 String++;
3423 }
darylm503b0934ac2011-11-12 00:35:11 +00003424
jcarseya405b862010-09-14 05:18:09 +00003425 if (String == NULL) {
3426 return (FALSE);
3427 }
3428
3429 //
3430 // chop leading zeroes
3431 //
3432 while(String != NULL && *String == L'0'){
3433 String++;
3434 }
3435 //
3436 // allow '0x' or '0X', but not 'x' or 'X'
3437 //
3438 if (String != NULL && (*String == L'x' || *String == L'X')) {
3439 if (*(String-1) != L'0') {
3440 //
3441 // we got an x without a preceeding 0
3442 //
3443 return (FALSE);
3444 }
3445 String++;
3446 Hex = TRUE;
3447 } else if (ForceHex) {
3448 Hex = TRUE;
3449 } else {
3450 Hex = FALSE;
3451 }
3452
3453 //
3454 // loop through the remaining characters and use the lib function
3455 //
3456 for ( ; String != NULL && *String != CHAR_NULL && !(StopAtSpace && *String == L' ') ; String++){
3457 if (Hex) {
3458 if (!ShellIsHexaDecimalDigitCharacter(*String)) {
3459 return (FALSE);
3460 }
3461 } else {
3462 if (!ShellIsDecimalDigitCharacter(*String)) {
3463 return (FALSE);
3464 }
3465 }
3466 }
jcarsey252d9452011-03-25 20:49:53 +00003467
jcarseya405b862010-09-14 05:18:09 +00003468 return (TRUE);
3469}
3470
3471/**
3472 Function to determine if a given filename exists.
3473
3474 @param[in] Name Path to test.
3475
3476 @retval EFI_SUCCESS The Path represents a file.
3477 @retval EFI_NOT_FOUND The Path does not represent a file.
3478 @retval other The path failed to open.
3479**/
3480EFI_STATUS
3481EFIAPI
3482ShellFileExists(
3483 IN CONST CHAR16 *Name
3484 )
3485{
3486 EFI_STATUS Status;
3487 EFI_SHELL_FILE_INFO *List;
3488
3489 ASSERT(Name != NULL);
3490
3491 List = NULL;
3492 Status = ShellOpenFileMetaArg((CHAR16*)Name, EFI_FILE_MODE_READ, &List);
3493 if (EFI_ERROR(Status)) {
3494 return (Status);
3495 }
3496
3497 ShellCloseFileMetaArg(&List);
3498
3499 return (EFI_SUCCESS);
3500}
jcarsey252d9452011-03-25 20:49:53 +00003501
3502/**
darylm503b0934ac2011-11-12 00:35:11 +00003503 Convert a Unicode character to upper case only if
jcarsey252d9452011-03-25 20:49:53 +00003504 it maps to a valid small-case ASCII character.
3505
3506 This internal function only deal with Unicode character
3507 which maps to a valid small-case ASCII character, i.e.
3508 L'a' to L'z'. For other Unicode character, the input character
3509 is returned directly.
3510
3511 @param Char The character to convert.
3512
3513 @retval LowerCharacter If the Char is with range L'a' to L'z'.
3514 @retval Unchanged Otherwise.
3515
3516**/
3517CHAR16
3518EFIAPI
3519InternalShellCharToUpper (
3520 IN CHAR16 Char
3521 )
3522{
3523 if (Char >= L'a' && Char <= L'z') {
3524 return (CHAR16) (Char - (L'a' - L'A'));
3525 }
3526
3527 return Char;
3528}
3529
3530/**
3531 Convert a Unicode character to numerical value.
3532
3533 This internal function only deal with Unicode character
3534 which maps to a valid hexadecimal ASII character, i.e.
darylm503b0934ac2011-11-12 00:35:11 +00003535 L'0' to L'9', L'a' to L'f' or L'A' to L'F'. For other
jcarsey252d9452011-03-25 20:49:53 +00003536 Unicode character, the value returned does not make sense.
3537
3538 @param Char The character to convert.
3539
3540 @return The numerical value converted.
3541
3542**/
3543UINTN
3544EFIAPI
3545InternalShellHexCharToUintn (
3546 IN CHAR16 Char
3547 )
3548{
3549 if (ShellIsDecimalDigitCharacter (Char)) {
3550 return Char - L'0';
3551 }
3552
3553 return (UINTN) (10 + InternalShellCharToUpper (Char) - L'A');
3554}
3555
3556/**
3557 Convert a Null-terminated Unicode hexadecimal string to a value of type UINT64.
3558
3559 This function returns a value of type UINTN by interpreting the contents
3560 of the Unicode string specified by String as a hexadecimal number.
3561 The format of the input Unicode string String is:
3562
3563 [spaces][zeros][x][hexadecimal digits].
3564
3565 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
3566 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix.
3567 If "x" appears in the input string, it must be prefixed with at least one 0.
3568 The function will ignore the pad space, which includes spaces or tab characters,
3569 before [zeros], [x] or [hexadecimal digit]. The running zero before [x] or
3570 [hexadecimal digit] will be ignored. Then, the decoding starts after [x] or the
3571 first valid hexadecimal digit. Then, the function stops at the first character that is
3572 a not a valid hexadecimal character or NULL, whichever one comes first.
3573
3574 If String has only pad spaces, then zero is returned.
3575 If String has no leading pad spaces, leading zeros or valid hexadecimal digits,
3576 then zero is returned.
3577
3578 @param[in] String A pointer to a Null-terminated Unicode string.
3579 @param[out] Value Upon a successful return the value of the conversion.
3580 @param[in] StopAtSpace FALSE to skip spaces.
3581
3582 @retval EFI_SUCCESS The conversion was successful.
3583 @retval EFI_INVALID_PARAMETER A parameter was NULL or invalid.
3584 @retval EFI_DEVICE_ERROR An overflow occured.
3585**/
3586EFI_STATUS
3587EFIAPI
3588InternalShellStrHexToUint64 (
3589 IN CONST CHAR16 *String,
3590 OUT UINT64 *Value,
3591 IN CONST BOOLEAN StopAtSpace
3592 )
3593{
3594 UINT64 Result;
3595
3596 if (String == NULL || StrSize(String) == 0 || Value == NULL) {
3597 return (EFI_INVALID_PARAMETER);
3598 }
darylm503b0934ac2011-11-12 00:35:11 +00003599
jcarsey252d9452011-03-25 20:49:53 +00003600 //
darylm503b0934ac2011-11-12 00:35:11 +00003601 // Ignore the pad spaces (space or tab)
jcarsey252d9452011-03-25 20:49:53 +00003602 //
3603 while ((*String == L' ') || (*String == L'\t')) {
3604 String++;
3605 }
3606
3607 //
3608 // Ignore leading Zeros after the spaces
3609 //
3610 while (*String == L'0') {
3611 String++;
3612 }
3613
3614 if (InternalShellCharToUpper (*String) == L'X') {
3615 if (*(String - 1) != L'0') {
3616 return 0;
3617 }
3618 //
3619 // Skip the 'X'
3620 //
3621 String++;
3622 }
3623
3624 Result = 0;
darylm503b0934ac2011-11-12 00:35:11 +00003625
jcarsey252d9452011-03-25 20:49:53 +00003626 //
3627 // Skip spaces if requested
3628 //
3629 while (StopAtSpace && *String == L' ') {
3630 String++;
3631 }
darylm503b0934ac2011-11-12 00:35:11 +00003632
jcarsey252d9452011-03-25 20:49:53 +00003633 while (ShellIsHexaDecimalDigitCharacter (*String)) {
3634 //
darylm503b0934ac2011-11-12 00:35:11 +00003635 // If the Hex Number represented by String overflows according
jcarsey252d9452011-03-25 20:49:53 +00003636 // to the range defined by UINTN, then ASSERT().
3637 //
3638 if (!(Result <= (RShiftU64((((UINT64) ~0) - InternalShellHexCharToUintn (*String)), 4)))) {
3639// if (!(Result <= ((((UINT64) ~0) - InternalShellHexCharToUintn (*String)) >> 4))) {
3640 return (EFI_DEVICE_ERROR);
3641 }
3642
3643 Result = (LShiftU64(Result, 4));
3644 Result += InternalShellHexCharToUintn (*String);
3645 String++;
3646
3647 //
3648 // Skip spaces if requested
3649 //
3650 while (StopAtSpace && *String == L' ') {
3651 String++;
3652 }
3653 }
3654
3655 *Value = Result;
3656 return (EFI_SUCCESS);
3657}
3658
3659/**
3660 Convert a Null-terminated Unicode decimal string to a value of
3661 type UINT64.
3662
3663 This function returns a value of type UINT64 by interpreting the contents
3664 of the Unicode string specified by String as a decimal number. The format
3665 of the input Unicode string String is:
3666
3667 [spaces] [decimal digits].
3668
3669 The valid decimal digit character is in the range [0-9]. The
3670 function will ignore the pad space, which includes spaces or
3671 tab characters, before [decimal digits]. The running zero in the
3672 beginning of [decimal digits] will be ignored. Then, the function
3673 stops at the first character that is a not a valid decimal character
3674 or a Null-terminator, whichever one comes first.
3675
3676 If String has only pad spaces, then 0 is returned.
3677 If String has no pad spaces or valid decimal digits,
3678 then 0 is returned.
3679
3680 @param[in] String A pointer to a Null-terminated Unicode string.
3681 @param[out] Value Upon a successful return the value of the conversion.
3682 @param[in] StopAtSpace FALSE to skip spaces.
3683
3684 @retval EFI_SUCCESS The conversion was successful.
3685 @retval EFI_INVALID_PARAMETER A parameter was NULL or invalid.
3686 @retval EFI_DEVICE_ERROR An overflow occured.
3687**/
3688EFI_STATUS
3689EFIAPI
3690InternalShellStrDecimalToUint64 (
3691 IN CONST CHAR16 *String,
3692 OUT UINT64 *Value,
3693 IN CONST BOOLEAN StopAtSpace
3694 )
3695{
3696 UINT64 Result;
3697
3698 if (String == NULL || StrSize (String) == 0 || Value == NULL) {
3699 return (EFI_INVALID_PARAMETER);
3700 }
3701
3702 //
3703 // Ignore the pad spaces (space or tab)
3704 //
3705 while ((*String == L' ') || (*String == L'\t')) {
3706 String++;
3707 }
3708
3709 //
3710 // Ignore leading Zeros after the spaces
3711 //
3712 while (*String == L'0') {
3713 String++;
3714 }
3715
3716 Result = 0;
3717
3718 //
3719 // Skip spaces if requested
3720 //
3721 while (StopAtSpace && *String == L' ') {
3722 String++;
3723 }
3724 while (ShellIsDecimalDigitCharacter (*String)) {
3725 //
darylm503b0934ac2011-11-12 00:35:11 +00003726 // If the number represented by String overflows according
jcarsey252d9452011-03-25 20:49:53 +00003727 // to the range defined by UINT64, then ASSERT().
3728 //
darylm503b0934ac2011-11-12 00:35:11 +00003729
jcarsey252d9452011-03-25 20:49:53 +00003730 if (!(Result <= (DivU64x32((((UINT64) ~0) - (*String - L'0')),10)))) {
3731 return (EFI_DEVICE_ERROR);
3732 }
3733
3734 Result = MultU64x32(Result, 10) + (*String - L'0');
3735 String++;
3736
3737 //
3738 // Stop at spaces if requested
3739 //
3740 if (StopAtSpace && *String == L' ') {
3741 break;
3742 }
3743 }
3744
3745 *Value = Result;
darylm503b0934ac2011-11-12 00:35:11 +00003746
jcarsey252d9452011-03-25 20:49:53 +00003747 return (EFI_SUCCESS);
3748}
3749
3750/**
3751 Function to verify and convert a string to its numerical value.
3752
3753 If Hex it must be preceeded with a 0x, 0X, or has ForceHex set TRUE.
3754
3755 @param[in] String The string to evaluate.
3756 @param[out] Value Upon a successful return the value of the conversion.
3757 @param[in] ForceHex TRUE - always assume hex.
3758 @param[in] StopAtSpace FALSE to skip spaces.
darylm503b0934ac2011-11-12 00:35:11 +00003759
jcarsey252d9452011-03-25 20:49:53 +00003760 @retval EFI_SUCCESS The conversion was successful.
3761 @retval EFI_INVALID_PARAMETER String contained an invalid character.
3762 @retval EFI_NOT_FOUND String was a number, but Value was NULL.
3763**/
3764EFI_STATUS
3765EFIAPI
3766ShellConvertStringToUint64(
3767 IN CONST CHAR16 *String,
3768 OUT UINT64 *Value,
3769 IN CONST BOOLEAN ForceHex,
3770 IN CONST BOOLEAN StopAtSpace
3771 )
3772{
3773 UINT64 RetVal;
3774 CONST CHAR16 *Walker;
3775 EFI_STATUS Status;
3776 BOOLEAN Hex;
3777
3778 Hex = ForceHex;
3779
3780 if (!InternalShellIsHexOrDecimalNumber(String, Hex, StopAtSpace)) {
3781 if (!Hex) {
3782 Hex = TRUE;
3783 if (!InternalShellIsHexOrDecimalNumber(String, Hex, StopAtSpace)) {
3784 return (EFI_INVALID_PARAMETER);
3785 }
3786 } else {
3787 return (EFI_INVALID_PARAMETER);
3788 }
3789 }
3790
3791 //
3792 // Chop off leading spaces
3793 //
3794 for (Walker = String; Walker != NULL && *Walker != CHAR_NULL && *Walker == L' '; Walker++);
3795
3796 //
3797 // make sure we have something left that is numeric.
3798 //
3799 if (Walker == NULL || *Walker == CHAR_NULL || !InternalShellIsHexOrDecimalNumber(Walker, Hex, StopAtSpace)) {
3800 return (EFI_INVALID_PARAMETER);
darylm503b0934ac2011-11-12 00:35:11 +00003801 }
jcarsey252d9452011-03-25 20:49:53 +00003802
3803 //
3804 // do the conversion.
3805 //
3806 if (Hex || StrnCmp(Walker, L"0x", 2) == 0 || StrnCmp(Walker, L"0X", 2) == 0){
3807 Status = InternalShellStrHexToUint64(Walker, &RetVal, StopAtSpace);
3808 } else {
3809 Status = InternalShellStrDecimalToUint64(Walker, &RetVal, StopAtSpace);
3810 }
3811
3812 if (Value == NULL && !EFI_ERROR(Status)) {
3813 return (EFI_NOT_FOUND);
3814 }
3815
3816 if (Value != NULL) {
3817 *Value = RetVal;
3818 }
3819
3820 return (Status);
3821}
3822
3823/**
3824 Function to determin if an entire string is a valid number.
3825
3826 If Hex it must be preceeded with a 0x or has ForceHex, set TRUE.
3827
3828 @param[in] String The string to evaluate.
3829 @param[in] ForceHex TRUE - always assume hex.
3830 @param[in] StopAtSpace TRUE to halt upon finding a space, FALSE to keep going.
3831
3832 @retval TRUE It is all numeric (dec/hex) characters.
3833 @retval FALSE There is a non-numeric character.
3834**/
3835BOOLEAN
3836EFIAPI
3837ShellIsHexOrDecimalNumber (
3838 IN CONST CHAR16 *String,
3839 IN CONST BOOLEAN ForceHex,
3840 IN CONST BOOLEAN StopAtSpace
3841 )
3842{
3843 if (ShellConvertStringToUint64(String, NULL, ForceHex, StopAtSpace) == EFI_NOT_FOUND) {
3844 return (TRUE);
3845 }
3846 return (FALSE);
3847}
jcarsey4d0a4fc2011-07-06 22:28:36 +00003848
3849/**
3850 Function to read a single line from a SHELL_FILE_HANDLE. The \n is not included in the returned
3851 buffer. The returned buffer must be callee freed.
3852
3853 If the position upon start is 0, then the Ascii Boolean will be set. This should be
3854 maintained and not changed for all operations with the same file.
3855
ydong104ff7e372011-09-02 08:05:34 +00003856 @param[in] Handle SHELL_FILE_HANDLE to read from.
3857 @param[in, out] Ascii Boolean value for indicating whether the file is
3858 Ascii (TRUE) or UCS2 (FALSE).
jcarsey4d0a4fc2011-07-06 22:28:36 +00003859
jcarseybeab0fc2011-10-10 17:26:25 +00003860 @return The line of text from the file.
3861 @retval NULL There was not enough memory available.
jcarsey4d0a4fc2011-07-06 22:28:36 +00003862
3863 @sa ShellFileHandleReadLine
3864**/
3865CHAR16*
3866EFIAPI
3867ShellFileHandleReturnLine(
3868 IN SHELL_FILE_HANDLE Handle,
3869 IN OUT BOOLEAN *Ascii
3870 )
3871{
3872 CHAR16 *RetVal;
3873 UINTN Size;
3874 EFI_STATUS Status;
3875
3876 Size = 0;
3877 RetVal = NULL;
3878
3879 Status = ShellFileHandleReadLine(Handle, RetVal, &Size, FALSE, Ascii);
3880 if (Status == EFI_BUFFER_TOO_SMALL) {
3881 RetVal = AllocateZeroPool(Size);
jcarseybeab0fc2011-10-10 17:26:25 +00003882 if (RetVal == NULL) {
3883 return (NULL);
3884 }
jcarsey4d0a4fc2011-07-06 22:28:36 +00003885 Status = ShellFileHandleReadLine(Handle, RetVal, &Size, FALSE, Ascii);
darylm503b0934ac2011-11-12 00:35:11 +00003886
jcarsey4d0a4fc2011-07-06 22:28:36 +00003887 }
jcarsey4d0a4fc2011-07-06 22:28:36 +00003888 if (EFI_ERROR(Status) && (RetVal != NULL)) {
3889 FreePool(RetVal);
3890 RetVal = NULL;
3891 }
3892 return (RetVal);
3893}
3894
3895/**
3896 Function to read a single line (up to but not including the \n) from a SHELL_FILE_HANDLE.
3897
3898 If the position upon start is 0, then the Ascii Boolean will be set. This should be
3899 maintained and not changed for all operations with the same file.
3900
ydong104ff7e372011-09-02 08:05:34 +00003901 @param[in] Handle SHELL_FILE_HANDLE to read from.
3902 @param[in, out] Buffer The pointer to buffer to read into.
3903 @param[in, out] Size The pointer to number of bytes in Buffer.
3904 @param[in] Truncate If the buffer is large enough, this has no effect.
3905 If the buffer is is too small and Truncate is TRUE,
3906 the line will be truncated.
3907 If the buffer is is too small and Truncate is FALSE,
3908 then no read will occur.
jcarsey4d0a4fc2011-07-06 22:28:36 +00003909
ydong104ff7e372011-09-02 08:05:34 +00003910 @param[in, out] Ascii Boolean value for indicating whether the file is
3911 Ascii (TRUE) or UCS2 (FALSE).
jcarsey4d0a4fc2011-07-06 22:28:36 +00003912
3913 @retval EFI_SUCCESS The operation was successful. The line is stored in
3914 Buffer.
3915 @retval EFI_INVALID_PARAMETER Handle was NULL.
3916 @retval EFI_INVALID_PARAMETER Size was NULL.
3917 @retval EFI_BUFFER_TOO_SMALL Size was not large enough to store the line.
3918 Size was updated to the minimum space required.
3919**/
3920EFI_STATUS
3921EFIAPI
3922ShellFileHandleReadLine(
3923 IN SHELL_FILE_HANDLE Handle,
3924 IN OUT CHAR16 *Buffer,
3925 IN OUT UINTN *Size,
3926 IN BOOLEAN Truncate,
3927 IN OUT BOOLEAN *Ascii
3928 )
3929{
3930 EFI_STATUS Status;
3931 CHAR16 CharBuffer;
3932 UINTN CharSize;
3933 UINTN CountSoFar;
3934 UINT64 OriginalFilePosition;
3935
3936
3937 if (Handle == NULL
3938 ||Size == NULL
3939 ){
3940 return (EFI_INVALID_PARAMETER);
3941 }
3942 if (Buffer == NULL) {
3943 ASSERT(*Size == 0);
3944 } else {
3945 *Buffer = CHAR_NULL;
3946 }
3947 gEfiShellProtocol->GetFilePosition(Handle, &OriginalFilePosition);
3948 if (OriginalFilePosition == 0) {
3949 CharSize = sizeof(CHAR16);
3950 Status = gEfiShellProtocol->ReadFile(Handle, &CharSize, &CharBuffer);
3951 ASSERT_EFI_ERROR(Status);
3952 if (CharBuffer == gUnicodeFileTag) {
3953 *Ascii = FALSE;
3954 } else {
3955 *Ascii = TRUE;
3956 gEfiShellProtocol->SetFilePosition(Handle, OriginalFilePosition);
3957 }
3958 }
3959
3960 for (CountSoFar = 0;;CountSoFar++){
3961 CharBuffer = 0;
3962 if (*Ascii) {
3963 CharSize = sizeof(CHAR8);
3964 } else {
3965 CharSize = sizeof(CHAR16);
3966 }
3967 Status = gEfiShellProtocol->ReadFile(Handle, &CharSize, &CharBuffer);
3968 if ( EFI_ERROR(Status)
3969 || CharSize == 0
3970 || (CharBuffer == L'\n' && !(*Ascii))
3971 || (CharBuffer == '\n' && *Ascii)
3972 ){
3973 break;
3974 }
3975 //
3976 // if we have space save it...
3977 //
3978 if ((CountSoFar+1)*sizeof(CHAR16) < *Size){
3979 ASSERT(Buffer != NULL);
3980 ((CHAR16*)Buffer)[CountSoFar] = CharBuffer;
3981 ((CHAR16*)Buffer)[CountSoFar+1] = CHAR_NULL;
3982 }
3983 }
3984
3985 //
3986 // if we ran out of space tell when...
3987 //
3988 if ((CountSoFar+1)*sizeof(CHAR16) > *Size){
3989 *Size = (CountSoFar+1)*sizeof(CHAR16);
3990 if (!Truncate) {
3991 gEfiShellProtocol->SetFilePosition(Handle, OriginalFilePosition);
3992 } else {
3993 DEBUG((DEBUG_WARN, "The line was truncated in ShellFileHandleReadLine"));
3994 }
3995 return (EFI_BUFFER_TOO_SMALL);
3996 }
3997 while(Buffer[StrLen(Buffer)-1] == L'\r') {
3998 Buffer[StrLen(Buffer)-1] = CHAR_NULL;
3999 }
4000
4001 return (Status);
4002}