blob: 9b58786756bfb0ab63a45baaa1ca630d9eab8a55 [file] [log] [blame]
jcarsey94b17fa2009-05-07 18:46:18 +00001/** @file
2 Provides interface to shell functionality for shell commands and applications.
3
Jaben Carsey75a5e2e2014-01-10 16:42:45 +00004 Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>
jcarsey1e6e84c2010-01-25 20:05:08 +00005 This program and the accompanying materials
jcarseyb3011f42010-01-11 21:49:04 +00006 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
jcarsey94b17fa2009-05-07 18:46:18 +00009
jcarseyb3011f42010-01-11 21:49:04 +000010 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
jcarsey94b17fa2009-05-07 18:46:18 +000012
13**/
14
jcarseyb1f95a02009-06-16 00:23:19 +000015#include "UefiShellLib.h"
jcarseya405b862010-09-14 05:18:09 +000016#include <ShellBase.h>
jcarsey252d9452011-03-25 20:49:53 +000017#include <Library/SortLib.h>
jcarseyd2b45642009-05-11 18:02:16 +000018
jcarsey94b17fa2009-05-07 18:46:18 +000019#define FIND_XXXXX_FILE_BUFFER_SIZE (SIZE_OF_EFI_FILE_INFO + MAX_FILE_NAME_LEN)
20
jcarseyd2b45642009-05-11 18:02:16 +000021//
jcarseya405b862010-09-14 05:18:09 +000022// globals...
jcarseyd2b45642009-05-11 18:02:16 +000023//
24SHELL_PARAM_ITEM EmptyParamList[] = {
25 {NULL, TypeMax}
26 };
jcarseya405b862010-09-14 05:18:09 +000027SHELL_PARAM_ITEM SfoParamList[] = {
28 {L"-sfo", TypeFlag},
29 {NULL, TypeMax}
30 };
31EFI_SHELL_ENVIRONMENT2 *mEfiShellEnvironment2;
32EFI_SHELL_INTERFACE *mEfiShellInterface;
jcarsey366f81a2011-06-27 21:04:22 +000033EFI_SHELL_PROTOCOL *gEfiShellProtocol;
34EFI_SHELL_PARAMETERS_PROTOCOL *gEfiShellParametersProtocol;
jcarseya405b862010-09-14 05:18:09 +000035EFI_HANDLE mEfiShellEnvironment2Handle;
36FILE_HANDLE_FUNCTION_MAP FileFunctionMap;
jcarseyb3011f42010-01-11 21:49:04 +000037
jcarsey2247dde2009-11-09 18:08:58 +000038/**
39 Check if a Unicode character is a hexadecimal character.
40
jcarsey1e6e84c2010-01-25 20:05:08 +000041 This internal function checks if a Unicode character is a
jcarseya405b862010-09-14 05:18:09 +000042 numeric character. The valid hexadecimal characters are
jcarsey2247dde2009-11-09 18:08:58 +000043 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.
44
jcarsey2247dde2009-11-09 18:08:58 +000045 @param Char The character to check against.
46
47 @retval TRUE If the Char is a hexadecmial character.
48 @retval FALSE If the Char is not a hexadecmial character.
49
50**/
51BOOLEAN
52EFIAPI
jcarsey969c7832010-01-13 16:46:33 +000053ShellIsHexaDecimalDigitCharacter (
jcarsey2247dde2009-11-09 18:08:58 +000054 IN CHAR16 Char
jcarseya405b862010-09-14 05:18:09 +000055 )
56{
jcarsey2247dde2009-11-09 18:08:58 +000057 return (BOOLEAN) ((Char >= L'0' && Char <= L'9') || (Char >= L'A' && Char <= L'F') || (Char >= L'a' && Char <= L'f'));
58}
jcarsey94b17fa2009-05-07 18:46:18 +000059
60/**
jcarseya405b862010-09-14 05:18:09 +000061 Check if a Unicode character is a decimal character.
62
63 This internal function checks if a Unicode character is a
64 decimal character. The valid characters are
65 L'0' to L'9'.
66
67
68 @param Char The character to check against.
69
70 @retval TRUE If the Char is a hexadecmial character.
71 @retval FALSE If the Char is not a hexadecmial character.
72
73**/
74BOOLEAN
75EFIAPI
76ShellIsDecimalDigitCharacter (
77 IN CHAR16 Char
78 )
79{
80 return (BOOLEAN) (Char >= L'0' && Char <= L'9');
81}
82
83/**
84 Helper function to find ShellEnvironment2 for constructor.
85
86 @param[in] ImageHandle A copy of the calling image's handle.
jcarseybeab0fc2011-10-10 17:26:25 +000087
88 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.
jcarsey94b17fa2009-05-07 18:46:18 +000089**/
90EFI_STATUS
91EFIAPI
92ShellFindSE2 (
93 IN EFI_HANDLE ImageHandle
jcarseya405b862010-09-14 05:18:09 +000094 )
95{
jcarsey94b17fa2009-05-07 18:46:18 +000096 EFI_STATUS Status;
97 EFI_HANDLE *Buffer;
98 UINTN BufferSize;
99 UINTN HandleIndex;
100
101 BufferSize = 0;
102 Buffer = NULL;
jcarsey1e6e84c2010-01-25 20:05:08 +0000103 Status = gBS->OpenProtocol(ImageHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000104 &gEfiShellEnvironment2Guid,
105 (VOID **)&mEfiShellEnvironment2,
106 ImageHandle,
107 NULL,
108 EFI_OPEN_PROTOCOL_GET_PROTOCOL
jcarseya405b862010-09-14 05:18:09 +0000109 );
jcarsey94b17fa2009-05-07 18:46:18 +0000110 //
111 // look for the mEfiShellEnvironment2 protocol at a higher level
112 //
jcarseya405b862010-09-14 05:18:09 +0000113 if (EFI_ERROR (Status) || !(CompareGuid (&mEfiShellEnvironment2->SESGuid, &gEfiShellEnvironment2ExtGuid))){
jcarsey94b17fa2009-05-07 18:46:18 +0000114 //
115 // figure out how big of a buffer we need.
116 //
117 Status = gBS->LocateHandle (ByProtocol,
118 &gEfiShellEnvironment2Guid,
119 NULL, // ignored for ByProtocol
120 &BufferSize,
121 Buffer
jcarseya405b862010-09-14 05:18:09 +0000122 );
jcarsey2247dde2009-11-09 18:08:58 +0000123 //
124 // maybe it's not there???
125 //
126 if (Status == EFI_BUFFER_TOO_SMALL) {
jcarsey252d9452011-03-25 20:49:53 +0000127 Buffer = (EFI_HANDLE*)AllocateZeroPool(BufferSize);
jcarseybeab0fc2011-10-10 17:26:25 +0000128 if (Buffer == NULL) {
129 return (EFI_OUT_OF_RESOURCES);
130 }
jcarsey2247dde2009-11-09 18:08:58 +0000131 Status = gBS->LocateHandle (ByProtocol,
132 &gEfiShellEnvironment2Guid,
133 NULL, // ignored for ByProtocol
134 &BufferSize,
135 Buffer
jcarseya405b862010-09-14 05:18:09 +0000136 );
jcarsey2247dde2009-11-09 18:08:58 +0000137 }
jcarsey1cd45e72010-01-29 15:07:44 +0000138 if (!EFI_ERROR (Status) && Buffer != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +0000139 //
140 // now parse the list of returned handles
141 //
142 Status = EFI_NOT_FOUND;
143 for (HandleIndex = 0; HandleIndex < (BufferSize/sizeof(Buffer[0])); HandleIndex++) {
jcarsey1e6e84c2010-01-25 20:05:08 +0000144 Status = gBS->OpenProtocol(Buffer[HandleIndex],
jcarsey94b17fa2009-05-07 18:46:18 +0000145 &gEfiShellEnvironment2Guid,
146 (VOID **)&mEfiShellEnvironment2,
147 ImageHandle,
148 NULL,
149 EFI_OPEN_PROTOCOL_GET_PROTOCOL
jcarseya405b862010-09-14 05:18:09 +0000150 );
151 if (CompareGuid (&mEfiShellEnvironment2->SESGuid, &gEfiShellEnvironment2ExtGuid)) {
jcarsey94b17fa2009-05-07 18:46:18 +0000152 mEfiShellEnvironment2Handle = Buffer[HandleIndex];
153 Status = EFI_SUCCESS;
154 break;
155 }
156 }
157 }
158 }
159 if (Buffer != NULL) {
160 FreePool (Buffer);
161 }
162 return (Status);
163}
164
jcarsey252d9452011-03-25 20:49:53 +0000165/**
darylm503b0934ac2011-11-12 00:35:11 +0000166 Function to do most of the work of the constructor. Allows for calling
jcarseya405b862010-09-14 05:18:09 +0000167 multiple times without complete re-initialization.
168
169 @param[in] ImageHandle A copy of the ImageHandle.
170 @param[in] SystemTable A pointer to the SystemTable for the application.
jcarsey252d9452011-03-25 20:49:53 +0000171
172 @retval EFI_SUCCESS The operationw as successful.
jcarseya405b862010-09-14 05:18:09 +0000173**/
jcarsey94b17fa2009-05-07 18:46:18 +0000174EFI_STATUS
175EFIAPI
jcarseyd2b45642009-05-11 18:02:16 +0000176ShellLibConstructorWorker (
jcarsey94b17fa2009-05-07 18:46:18 +0000177 IN EFI_HANDLE ImageHandle,
178 IN EFI_SYSTEM_TABLE *SystemTable
jcarseya405b862010-09-14 05:18:09 +0000179 )
180{
181 EFI_STATUS Status;
jcarseyecd3d592009-12-07 18:05:00 +0000182
jcarsey94b17fa2009-05-07 18:46:18 +0000183 //
184 // UEFI 2.0 shell interfaces (used preferentially)
185 //
jcarseya405b862010-09-14 05:18:09 +0000186 Status = gBS->OpenProtocol(
187 ImageHandle,
188 &gEfiShellProtocolGuid,
jcarsey366f81a2011-06-27 21:04:22 +0000189 (VOID **)&gEfiShellProtocol,
jcarseya405b862010-09-14 05:18:09 +0000190 ImageHandle,
191 NULL,
192 EFI_OPEN_PROTOCOL_GET_PROTOCOL
193 );
jcarsey94b17fa2009-05-07 18:46:18 +0000194 if (EFI_ERROR(Status)) {
jcarseya405b862010-09-14 05:18:09 +0000195 //
196 // Search for the shell protocol
197 //
198 Status = gBS->LocateProtocol(
199 &gEfiShellProtocolGuid,
200 NULL,
jcarsey366f81a2011-06-27 21:04:22 +0000201 (VOID **)&gEfiShellProtocol
jcarseya405b862010-09-14 05:18:09 +0000202 );
203 if (EFI_ERROR(Status)) {
jcarsey366f81a2011-06-27 21:04:22 +0000204 gEfiShellProtocol = NULL;
jcarseya405b862010-09-14 05:18:09 +0000205 }
jcarsey94b17fa2009-05-07 18:46:18 +0000206 }
jcarseya405b862010-09-14 05:18:09 +0000207 Status = gBS->OpenProtocol(
208 ImageHandle,
209 &gEfiShellParametersProtocolGuid,
jcarsey366f81a2011-06-27 21:04:22 +0000210 (VOID **)&gEfiShellParametersProtocol,
jcarseya405b862010-09-14 05:18:09 +0000211 ImageHandle,
212 NULL,
213 EFI_OPEN_PROTOCOL_GET_PROTOCOL
214 );
jcarsey94b17fa2009-05-07 18:46:18 +0000215 if (EFI_ERROR(Status)) {
jcarsey366f81a2011-06-27 21:04:22 +0000216 gEfiShellParametersProtocol = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +0000217 }
218
jcarsey366f81a2011-06-27 21:04:22 +0000219 if (gEfiShellParametersProtocol == NULL || gEfiShellProtocol == NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +0000220 //
221 // Moved to seperate function due to complexity
222 //
223 Status = ShellFindSE2(ImageHandle);
224
225 if (EFI_ERROR(Status)) {
226 DEBUG((DEBUG_ERROR, "Status: 0x%08x\r\n", Status));
227 mEfiShellEnvironment2 = NULL;
228 }
jcarsey1e6e84c2010-01-25 20:05:08 +0000229 Status = gBS->OpenProtocol(ImageHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000230 &gEfiShellInterfaceGuid,
231 (VOID **)&mEfiShellInterface,
232 ImageHandle,
233 NULL,
234 EFI_OPEN_PROTOCOL_GET_PROTOCOL
jcarseya405b862010-09-14 05:18:09 +0000235 );
jcarsey94b17fa2009-05-07 18:46:18 +0000236 if (EFI_ERROR(Status)) {
237 mEfiShellInterface = NULL;
238 }
239 }
jcarseyc9d92df2010-02-03 15:37:54 +0000240
jcarsey94b17fa2009-05-07 18:46:18 +0000241 //
242 // only success getting 2 of either the old or new, but no 1/2 and 1/2
243 //
jcarsey1e6e84c2010-01-25 20:05:08 +0000244 if ((mEfiShellEnvironment2 != NULL && mEfiShellInterface != NULL) ||
jcarsey366f81a2011-06-27 21:04:22 +0000245 (gEfiShellProtocol != NULL && gEfiShellParametersProtocol != NULL) ) {
246 if (gEfiShellProtocol != NULL) {
247 FileFunctionMap.GetFileInfo = gEfiShellProtocol->GetFileInfo;
248 FileFunctionMap.SetFileInfo = gEfiShellProtocol->SetFileInfo;
249 FileFunctionMap.ReadFile = gEfiShellProtocol->ReadFile;
250 FileFunctionMap.WriteFile = gEfiShellProtocol->WriteFile;
251 FileFunctionMap.CloseFile = gEfiShellProtocol->CloseFile;
252 FileFunctionMap.DeleteFile = gEfiShellProtocol->DeleteFile;
253 FileFunctionMap.GetFilePosition = gEfiShellProtocol->GetFilePosition;
254 FileFunctionMap.SetFilePosition = gEfiShellProtocol->SetFilePosition;
255 FileFunctionMap.FlushFile = gEfiShellProtocol->FlushFile;
256 FileFunctionMap.GetFileSize = gEfiShellProtocol->GetFileSize;
jcarseyd2b45642009-05-11 18:02:16 +0000257 } else {
jcarseya405b862010-09-14 05:18:09 +0000258 FileFunctionMap.GetFileInfo = (EFI_SHELL_GET_FILE_INFO)FileHandleGetInfo;
259 FileFunctionMap.SetFileInfo = (EFI_SHELL_SET_FILE_INFO)FileHandleSetInfo;
260 FileFunctionMap.ReadFile = (EFI_SHELL_READ_FILE)FileHandleRead;
261 FileFunctionMap.WriteFile = (EFI_SHELL_WRITE_FILE)FileHandleWrite;
262 FileFunctionMap.CloseFile = (EFI_SHELL_CLOSE_FILE)FileHandleClose;
263 FileFunctionMap.DeleteFile = (EFI_SHELL_DELETE_FILE)FileHandleDelete;
264 FileFunctionMap.GetFilePosition = (EFI_SHELL_GET_FILE_POSITION)FileHandleGetPosition;
265 FileFunctionMap.SetFilePosition = (EFI_SHELL_SET_FILE_POSITION)FileHandleSetPosition;
266 FileFunctionMap.FlushFile = (EFI_SHELL_FLUSH_FILE)FileHandleFlush;
267 FileFunctionMap.GetFileSize = (EFI_SHELL_GET_FILE_SIZE)FileHandleGetSize;
jcarseyd2b45642009-05-11 18:02:16 +0000268 }
jcarsey94b17fa2009-05-07 18:46:18 +0000269 return (EFI_SUCCESS);
270 }
271 return (EFI_NOT_FOUND);
272}
jcarseyd2b45642009-05-11 18:02:16 +0000273/**
274 Constructor for the Shell library.
275
276 Initialize the library and determine if the underlying is a UEFI Shell 2.0 or an EFI shell.
277
278 @param ImageHandle the image handle of the process
279 @param SystemTable the EFI System Table pointer
280
281 @retval EFI_SUCCESS the initialization was complete sucessfully
282 @return others an error ocurred during initialization
283**/
284EFI_STATUS
285EFIAPI
286ShellLibConstructor (
287 IN EFI_HANDLE ImageHandle,
288 IN EFI_SYSTEM_TABLE *SystemTable
jcarseya405b862010-09-14 05:18:09 +0000289 )
290{
jcarseyd2b45642009-05-11 18:02:16 +0000291 mEfiShellEnvironment2 = NULL;
jcarsey366f81a2011-06-27 21:04:22 +0000292 gEfiShellProtocol = NULL;
293 gEfiShellParametersProtocol = NULL;
jcarseyd2b45642009-05-11 18:02:16 +0000294 mEfiShellInterface = NULL;
295 mEfiShellEnvironment2Handle = NULL;
296
jcarseyd2b45642009-05-11 18:02:16 +0000297 //
298 // verify that auto initialize is not set false
jcarsey1e6e84c2010-01-25 20:05:08 +0000299 //
jcarseyd2b45642009-05-11 18:02:16 +0000300 if (PcdGetBool(PcdShellLibAutoInitialize) == 0) {
301 return (EFI_SUCCESS);
302 }
jcarsey1e6e84c2010-01-25 20:05:08 +0000303
jcarseyd2b45642009-05-11 18:02:16 +0000304 return (ShellLibConstructorWorker(ImageHandle, SystemTable));
305}
jcarsey94b17fa2009-05-07 18:46:18 +0000306
307/**
jcarseya405b862010-09-14 05:18:09 +0000308 Destructor for the library. free any resources.
309
310 @param[in] ImageHandle A copy of the ImageHandle.
311 @param[in] SystemTable A pointer to the SystemTable for the application.
312
313 @retval EFI_SUCCESS The operation was successful.
314 @return An error from the CloseProtocol function.
jcarsey94b17fa2009-05-07 18:46:18 +0000315**/
316EFI_STATUS
317EFIAPI
318ShellLibDestructor (
319 IN EFI_HANDLE ImageHandle,
320 IN EFI_SYSTEM_TABLE *SystemTable
jcarseya405b862010-09-14 05:18:09 +0000321 )
322{
jcarsey94b17fa2009-05-07 18:46:18 +0000323 if (mEfiShellEnvironment2 != NULL) {
324 gBS->CloseProtocol(mEfiShellEnvironment2Handle==NULL?ImageHandle:mEfiShellEnvironment2Handle,
325 &gEfiShellEnvironment2Guid,
326 ImageHandle,
327 NULL);
jcarseyd2b45642009-05-11 18:02:16 +0000328 mEfiShellEnvironment2 = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +0000329 }
330 if (mEfiShellInterface != NULL) {
331 gBS->CloseProtocol(ImageHandle,
332 &gEfiShellInterfaceGuid,
333 ImageHandle,
jcarsey1e6e84c2010-01-25 20:05:08 +0000334 NULL);
jcarseyd2b45642009-05-11 18:02:16 +0000335 mEfiShellInterface = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +0000336 }
jcarsey366f81a2011-06-27 21:04:22 +0000337 if (gEfiShellProtocol != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +0000338 gBS->CloseProtocol(ImageHandle,
339 &gEfiShellProtocolGuid,
340 ImageHandle,
jcarsey1e6e84c2010-01-25 20:05:08 +0000341 NULL);
jcarsey366f81a2011-06-27 21:04:22 +0000342 gEfiShellProtocol = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +0000343 }
jcarsey366f81a2011-06-27 21:04:22 +0000344 if (gEfiShellParametersProtocol != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +0000345 gBS->CloseProtocol(ImageHandle,
346 &gEfiShellParametersProtocolGuid,
347 ImageHandle,
jcarsey1e6e84c2010-01-25 20:05:08 +0000348 NULL);
jcarsey366f81a2011-06-27 21:04:22 +0000349 gEfiShellParametersProtocol = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +0000350 }
jcarseyd2b45642009-05-11 18:02:16 +0000351 mEfiShellEnvironment2Handle = NULL;
jcarseyecd3d592009-12-07 18:05:00 +0000352
jcarsey94b17fa2009-05-07 18:46:18 +0000353 return (EFI_SUCCESS);
354}
jcarseyd2b45642009-05-11 18:02:16 +0000355
356/**
357 This function causes the shell library to initialize itself. If the shell library
358 is already initialized it will de-initialize all the current protocol poitners and
359 re-populate them again.
360
361 When the library is used with PcdShellLibAutoInitialize set to true this function
362 will return EFI_SUCCESS and perform no actions.
363
364 This function is intended for internal access for shell commands only.
365
366 @retval EFI_SUCCESS the initialization was complete sucessfully
367
368**/
369EFI_STATUS
370EFIAPI
371ShellInitialize (
jcarseya405b862010-09-14 05:18:09 +0000372 )
373{
jcarseyd2b45642009-05-11 18:02:16 +0000374 //
375 // if auto initialize is not false then skip
376 //
377 if (PcdGetBool(PcdShellLibAutoInitialize) != 0) {
378 return (EFI_SUCCESS);
379 }
380
381 //
382 // deinit the current stuff
383 //
384 ASSERT_EFI_ERROR(ShellLibDestructor(gImageHandle, gST));
385
386 //
387 // init the new stuff
388 //
389 return (ShellLibConstructorWorker(gImageHandle, gST));
390}
391
jcarsey94b17fa2009-05-07 18:46:18 +0000392/**
jcarsey1e6e84c2010-01-25 20:05:08 +0000393 This function will retrieve the information about the file for the handle
jcarsey94b17fa2009-05-07 18:46:18 +0000394 specified and store it in allocated pool memory.
395
jcarsey1e6e84c2010-01-25 20:05:08 +0000396 This function allocates a buffer to store the file's information. It is the
qhuang869817bf2009-05-20 14:42:48 +0000397 caller's responsibility to free the buffer
jcarsey94b17fa2009-05-07 18:46:18 +0000398
jcarsey1e6e84c2010-01-25 20:05:08 +0000399 @param FileHandle The file handle of the file for which information is
jcarsey94b17fa2009-05-07 18:46:18 +0000400 being requested.
401
402 @retval NULL information could not be retrieved.
403
404 @return the information about the file
405**/
406EFI_FILE_INFO*
407EFIAPI
408ShellGetFileInfo (
jcarseya405b862010-09-14 05:18:09 +0000409 IN SHELL_FILE_HANDLE FileHandle
410 )
411{
jcarseyd2b45642009-05-11 18:02:16 +0000412 return (FileFunctionMap.GetFileInfo(FileHandle));
jcarsey94b17fa2009-05-07 18:46:18 +0000413}
414
415/**
jcarseya405b862010-09-14 05:18:09 +0000416 This function sets the information about the file for the opened handle
jcarsey94b17fa2009-05-07 18:46:18 +0000417 specified.
418
jcarseya405b862010-09-14 05:18:09 +0000419 @param[in] FileHandle The file handle of the file for which information
420 is being set.
jcarsey94b17fa2009-05-07 18:46:18 +0000421
jcarseya405b862010-09-14 05:18:09 +0000422 @param[in] FileInfo The information to set.
jcarsey94b17fa2009-05-07 18:46:18 +0000423
darylm503b0934ac2011-11-12 00:35:11 +0000424 @retval EFI_SUCCESS The information was set.
jcarseya405b862010-09-14 05:18:09 +0000425 @retval EFI_INVALID_PARAMETER A parameter was out of range or invalid.
426 @retval EFI_UNSUPPORTED The FileHandle does not support FileInfo.
darylm503b0934ac2011-11-12 00:35:11 +0000427 @retval EFI_NO_MEDIA The device has no medium.
428 @retval EFI_DEVICE_ERROR The device reported an error.
429 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
430 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
jcarseya405b862010-09-14 05:18:09 +0000431 @retval EFI_ACCESS_DENIED The file was opened read only.
432 @retval EFI_VOLUME_FULL The volume is full.
jcarsey94b17fa2009-05-07 18:46:18 +0000433**/
434EFI_STATUS
435EFIAPI
436ShellSetFileInfo (
darylm503b0934ac2011-11-12 00:35:11 +0000437 IN SHELL_FILE_HANDLE FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000438 IN EFI_FILE_INFO *FileInfo
jcarseya405b862010-09-14 05:18:09 +0000439 )
440{
jcarseyd2b45642009-05-11 18:02:16 +0000441 return (FileFunctionMap.SetFileInfo(FileHandle, FileInfo));
jcarsey1e6e84c2010-01-25 20:05:08 +0000442}
443
jcarsey94b17fa2009-05-07 18:46:18 +0000444 /**
445 This function will open a file or directory referenced by DevicePath.
446
jcarsey1e6e84c2010-01-25 20:05:08 +0000447 This function opens a file with the open mode according to the file path. The
jcarsey94b17fa2009-05-07 18:46:18 +0000448 Attributes is valid only for EFI_FILE_MODE_CREATE.
449
darylm503b0934ac2011-11-12 00:35:11 +0000450 @param FilePath on input the device path to the file. On output
jcarsey94b17fa2009-05-07 18:46:18 +0000451 the remaining device path.
darylm503b0934ac2011-11-12 00:35:11 +0000452 @param DeviceHandle pointer to the system device handle.
453 @param FileHandle pointer to the file handle.
454 @param OpenMode the mode to open the file with.
455 @param Attributes the file's file attributes.
jcarsey94b17fa2009-05-07 18:46:18 +0000456
darylm503b0934ac2011-11-12 00:35:11 +0000457 @retval EFI_SUCCESS The information was set.
458 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
459 @retval EFI_UNSUPPORTED Could not open the file path.
460 @retval EFI_NOT_FOUND The specified file could not be found on the
jcarsey1e6e84c2010-01-25 20:05:08 +0000461 device or the file system could not be found on
jcarsey94b17fa2009-05-07 18:46:18 +0000462 the device.
darylm503b0934ac2011-11-12 00:35:11 +0000463 @retval EFI_NO_MEDIA The device has no medium.
464 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
jcarsey94b17fa2009-05-07 18:46:18 +0000465 medium is no longer supported.
darylm503b0934ac2011-11-12 00:35:11 +0000466 @retval EFI_DEVICE_ERROR The device reported an error.
467 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
468 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
469 @retval EFI_ACCESS_DENIED The file was opened read only.
470 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
jcarsey94b17fa2009-05-07 18:46:18 +0000471 file.
darylm503b0934ac2011-11-12 00:35:11 +0000472 @retval EFI_VOLUME_FULL The volume is full.
jcarsey94b17fa2009-05-07 18:46:18 +0000473**/
474EFI_STATUS
475EFIAPI
476ShellOpenFileByDevicePath(
darylm503b0934ac2011-11-12 00:35:11 +0000477 IN OUT EFI_DEVICE_PATH_PROTOCOL **FilePath,
478 OUT EFI_HANDLE *DeviceHandle,
jcarseya405b862010-09-14 05:18:09 +0000479 OUT SHELL_FILE_HANDLE *FileHandle,
darylm503b0934ac2011-11-12 00:35:11 +0000480 IN UINT64 OpenMode,
481 IN UINT64 Attributes
jcarseya405b862010-09-14 05:18:09 +0000482 )
483{
484 CHAR16 *FileName;
485 EFI_STATUS Status;
jcarsey94b17fa2009-05-07 18:46:18 +0000486 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *EfiSimpleFileSystemProtocol;
jcarseya405b862010-09-14 05:18:09 +0000487 EFI_FILE_PROTOCOL *Handle1;
488 EFI_FILE_PROTOCOL *Handle2;
ydong100b6cb332013-01-25 02:00:22 +0000489 CHAR16 *FnafPathName;
490 UINTN PathLen;
jcarsey94b17fa2009-05-07 18:46:18 +0000491
jcarsey92a54472011-06-27 20:33:13 +0000492 if (FilePath == NULL || FileHandle == NULL || DeviceHandle == NULL) {
493 return (EFI_INVALID_PARAMETER);
494 }
495
jcarsey1e6e84c2010-01-25 20:05:08 +0000496 //
jcarsey94b17fa2009-05-07 18:46:18 +0000497 // which shell interface should we use
498 //
jcarsey366f81a2011-06-27 21:04:22 +0000499 if (gEfiShellProtocol != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +0000500 //
501 // use UEFI Shell 2.0 method.
502 //
jcarsey366f81a2011-06-27 21:04:22 +0000503 FileName = gEfiShellProtocol->GetFilePathFromDevicePath(*FilePath);
jcarsey94b17fa2009-05-07 18:46:18 +0000504 if (FileName == NULL) {
505 return (EFI_INVALID_PARAMETER);
506 }
507 Status = ShellOpenFileByName(FileName, FileHandle, OpenMode, Attributes);
508 FreePool(FileName);
509 return (Status);
jcarsey1e6e84c2010-01-25 20:05:08 +0000510 }
jcarseyd2b45642009-05-11 18:02:16 +0000511
512
513 //
514 // use old shell method.
515 //
jcarsey1e6e84c2010-01-25 20:05:08 +0000516 Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid,
517 FilePath,
jcarseyd2b45642009-05-11 18:02:16 +0000518 DeviceHandle);
519 if (EFI_ERROR (Status)) {
520 return Status;
521 }
522 Status = gBS->OpenProtocol(*DeviceHandle,
523 &gEfiSimpleFileSystemProtocolGuid,
jcarseyb1f95a02009-06-16 00:23:19 +0000524 (VOID**)&EfiSimpleFileSystemProtocol,
jcarseyd2b45642009-05-11 18:02:16 +0000525 gImageHandle,
526 NULL,
527 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
528 if (EFI_ERROR (Status)) {
529 return Status;
530 }
jcarseya405b862010-09-14 05:18:09 +0000531 Status = EfiSimpleFileSystemProtocol->OpenVolume(EfiSimpleFileSystemProtocol, &Handle1);
jcarseyd2b45642009-05-11 18:02:16 +0000532 if (EFI_ERROR (Status)) {
533 FileHandle = NULL;
534 return Status;
535 }
536
537 //
538 // go down directories one node at a time.
539 //
540 while (!IsDevicePathEnd (*FilePath)) {
jcarsey94b17fa2009-05-07 18:46:18 +0000541 //
jcarseyd2b45642009-05-11 18:02:16 +0000542 // For file system access each node should be a file path component
jcarsey94b17fa2009-05-07 18:46:18 +0000543 //
jcarseyd2b45642009-05-11 18:02:16 +0000544 if (DevicePathType (*FilePath) != MEDIA_DEVICE_PATH ||
545 DevicePathSubType (*FilePath) != MEDIA_FILEPATH_DP
jcarseya405b862010-09-14 05:18:09 +0000546 ) {
jcarsey94b17fa2009-05-07 18:46:18 +0000547 FileHandle = NULL;
jcarseyd2b45642009-05-11 18:02:16 +0000548 return (EFI_INVALID_PARAMETER);
jcarsey94b17fa2009-05-07 18:46:18 +0000549 }
jcarseyd2b45642009-05-11 18:02:16 +0000550 //
551 // Open this file path node
552 //
jcarseya405b862010-09-14 05:18:09 +0000553 Handle2 = Handle1;
554 Handle1 = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +0000555
556 //
ydong100b6cb332013-01-25 02:00:22 +0000557 // File Name Alignment Fix (FNAF)
558 // Handle2->Open may be incapable of handling a unaligned CHAR16 data.
559 // The structure pointed to by FilePath may be not CHAR16 aligned.
560 // This code copies the potentially unaligned PathName data from the
561 // FilePath structure to the aligned FnafPathName for use in the
562 // calls to Handl2->Open.
563 //
564
565 //
566 // Determine length of PathName, in bytes.
567 //
568 PathLen = DevicePathNodeLength (*FilePath) - SIZE_OF_FILEPATH_DEVICE_PATH;
569
570 //
571 // Allocate memory for the aligned copy of the string Extra allocation is to allow for forced alignment
572 // Copy bytes from possibly unaligned location to aligned location
573 //
574 FnafPathName = AllocateCopyPool(PathLen, (UINT8 *)((FILEPATH_DEVICE_PATH*)*FilePath)->PathName);
575 if (FnafPathName == NULL) {
576 return EFI_OUT_OF_RESOURCES;
577 }
578
579 //
jcarseyd2b45642009-05-11 18:02:16 +0000580 // Try to test opening an existing file
jcarsey94b17fa2009-05-07 18:46:18 +0000581 //
jcarseya405b862010-09-14 05:18:09 +0000582 Status = Handle2->Open (
583 Handle2,
584 &Handle1,
ydong100b6cb332013-01-25 02:00:22 +0000585 FnafPathName,
jcarseyd2b45642009-05-11 18:02:16 +0000586 OpenMode &~EFI_FILE_MODE_CREATE,
587 0
jcarseya405b862010-09-14 05:18:09 +0000588 );
jcarsey94b17fa2009-05-07 18:46:18 +0000589
jcarseyd2b45642009-05-11 18:02:16 +0000590 //
591 // see if the error was that it needs to be created
592 //
593 if ((EFI_ERROR (Status)) && (OpenMode != (OpenMode &~EFI_FILE_MODE_CREATE))) {
jcarseya405b862010-09-14 05:18:09 +0000594 Status = Handle2->Open (
595 Handle2,
596 &Handle1,
ydong100b6cb332013-01-25 02:00:22 +0000597 FnafPathName,
jcarseyd2b45642009-05-11 18:02:16 +0000598 OpenMode,
599 Attributes
jcarseya405b862010-09-14 05:18:09 +0000600 );
jcarsey94b17fa2009-05-07 18:46:18 +0000601 }
ydong100b6cb332013-01-25 02:00:22 +0000602
603 //
604 // Free the alignment buffer
605 //
606 FreePool(FnafPathName);
607
jcarseyd2b45642009-05-11 18:02:16 +0000608 //
609 // Close the last node
610 //
jcarseya405b862010-09-14 05:18:09 +0000611 Handle2->Close (Handle2);
jcarseyd2b45642009-05-11 18:02:16 +0000612
613 if (EFI_ERROR(Status)) {
614 return (Status);
615 }
616
617 //
618 // Get the next node
619 //
620 *FilePath = NextDevicePathNode (*FilePath);
jcarsey94b17fa2009-05-07 18:46:18 +0000621 }
jcarseya405b862010-09-14 05:18:09 +0000622
623 //
624 // This is a weak spot since if the undefined SHELL_FILE_HANDLE format changes this must change also!
625 //
626 *FileHandle = (VOID*)Handle1;
jcarseyd2b45642009-05-11 18:02:16 +0000627 return (EFI_SUCCESS);
jcarsey94b17fa2009-05-07 18:46:18 +0000628}
629
630/**
631 This function will open a file or directory referenced by filename.
632
jcarsey1e6e84c2010-01-25 20:05:08 +0000633 If return is EFI_SUCCESS, the Filehandle is the opened file's handle;
634 otherwise, the Filehandle is NULL. The Attributes is valid only for
jcarsey94b17fa2009-05-07 18:46:18 +0000635 EFI_FILE_MODE_CREATE.
636
jcarsey92a54472011-06-27 20:33:13 +0000637 if FileName is NULL then ASSERT()
jcarsey94b17fa2009-05-07 18:46:18 +0000638
darylm503b0934ac2011-11-12 00:35:11 +0000639 @param FileName pointer to file name
640 @param FileHandle pointer to the file handle.
641 @param OpenMode the mode to open the file with.
642 @param Attributes the file's file attributes.
jcarsey94b17fa2009-05-07 18:46:18 +0000643
darylm503b0934ac2011-11-12 00:35:11 +0000644 @retval EFI_SUCCESS The information was set.
645 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
646 @retval EFI_UNSUPPORTED Could not open the file path.
647 @retval EFI_NOT_FOUND The specified file could not be found on the
jcarsey1e6e84c2010-01-25 20:05:08 +0000648 device or the file system could not be found
jcarsey94b17fa2009-05-07 18:46:18 +0000649 on the device.
darylm503b0934ac2011-11-12 00:35:11 +0000650 @retval EFI_NO_MEDIA The device has no medium.
651 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
jcarsey94b17fa2009-05-07 18:46:18 +0000652 medium is no longer supported.
darylm503b0934ac2011-11-12 00:35:11 +0000653 @retval EFI_DEVICE_ERROR The device reported an error.
654 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
655 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
656 @retval EFI_ACCESS_DENIED The file was opened read only.
657 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
jcarsey94b17fa2009-05-07 18:46:18 +0000658 file.
darylm503b0934ac2011-11-12 00:35:11 +0000659 @retval EFI_VOLUME_FULL The volume is full.
jcarsey94b17fa2009-05-07 18:46:18 +0000660**/
661EFI_STATUS
662EFIAPI
663ShellOpenFileByName(
darylm503b0934ac2011-11-12 00:35:11 +0000664 IN CONST CHAR16 *FileName,
jcarseya405b862010-09-14 05:18:09 +0000665 OUT SHELL_FILE_HANDLE *FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000666 IN UINT64 OpenMode,
darylm503b0934ac2011-11-12 00:35:11 +0000667 IN UINT64 Attributes
jcarseya405b862010-09-14 05:18:09 +0000668 )
669{
jcarsey94b17fa2009-05-07 18:46:18 +0000670 EFI_HANDLE DeviceHandle;
671 EFI_DEVICE_PATH_PROTOCOL *FilePath;
jcarseyb1f95a02009-06-16 00:23:19 +0000672 EFI_STATUS Status;
673 EFI_FILE_INFO *FileInfo;
jcarsey94b17fa2009-05-07 18:46:18 +0000674
675 //
676 // ASSERT if FileName is NULL
677 //
678 ASSERT(FileName != NULL);
679
jcarseya405b862010-09-14 05:18:09 +0000680 if (FileName == NULL) {
681 return (EFI_INVALID_PARAMETER);
682 }
683
jcarsey366f81a2011-06-27 21:04:22 +0000684 if (gEfiShellProtocol != NULL) {
jcarseya405b862010-09-14 05:18:09 +0000685 if ((OpenMode & EFI_FILE_MODE_CREATE) == EFI_FILE_MODE_CREATE && (Attributes & EFI_FILE_DIRECTORY) == EFI_FILE_DIRECTORY) {
686 return ShellCreateDirectory(FileName, FileHandle);
687 }
jcarsey94b17fa2009-05-07 18:46:18 +0000688 //
689 // Use UEFI Shell 2.0 method
690 //
jcarsey366f81a2011-06-27 21:04:22 +0000691 Status = gEfiShellProtocol->OpenFileByName(FileName,
jcarseyb1f95a02009-06-16 00:23:19 +0000692 FileHandle,
693 OpenMode);
jcarseya405b862010-09-14 05:18:09 +0000694 if (StrCmp(FileName, L"NUL") != 0 && !EFI_ERROR(Status) && ((OpenMode & EFI_FILE_MODE_CREATE) != 0)){
jcarsey2247dde2009-11-09 18:08:58 +0000695 FileInfo = FileFunctionMap.GetFileInfo(*FileHandle);
jcarseyb1f95a02009-06-16 00:23:19 +0000696 ASSERT(FileInfo != NULL);
697 FileInfo->Attribute = Attributes;
jcarsey2247dde2009-11-09 18:08:58 +0000698 Status = FileFunctionMap.SetFileInfo(*FileHandle, FileInfo);
699 FreePool(FileInfo);
jcarseyb1f95a02009-06-16 00:23:19 +0000700 }
701 return (Status);
jcarsey1e6e84c2010-01-25 20:05:08 +0000702 }
jcarsey94b17fa2009-05-07 18:46:18 +0000703 //
704 // Using EFI Shell version
705 // this means convert name to path and call that function
706 // since this will use EFI method again that will open it.
707 //
708 ASSERT(mEfiShellEnvironment2 != NULL);
jcarseyb82bfcc2009-06-29 16:28:23 +0000709 FilePath = mEfiShellEnvironment2->NameToPath ((CHAR16*)FileName);
xdu290bfa222010-07-19 05:21:27 +0000710 if (FilePath != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +0000711 return (ShellOpenFileByDevicePath(&FilePath,
712 &DeviceHandle,
713 FileHandle,
714 OpenMode,
jcarseya405b862010-09-14 05:18:09 +0000715 Attributes));
jcarsey94b17fa2009-05-07 18:46:18 +0000716 }
717 return (EFI_DEVICE_ERROR);
718}
719/**
720 This function create a directory
721
jcarsey1e6e84c2010-01-25 20:05:08 +0000722 If return is EFI_SUCCESS, the Filehandle is the opened directory's handle;
723 otherwise, the Filehandle is NULL. If the directory already existed, this
jcarsey94b17fa2009-05-07 18:46:18 +0000724 function opens the existing directory.
725
darylm503b0934ac2011-11-12 00:35:11 +0000726 @param DirectoryName pointer to directory name
727 @param FileHandle pointer to the file handle.
jcarsey94b17fa2009-05-07 18:46:18 +0000728
darylm503b0934ac2011-11-12 00:35:11 +0000729 @retval EFI_SUCCESS The information was set.
730 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
731 @retval EFI_UNSUPPORTED Could not open the file path.
732 @retval EFI_NOT_FOUND The specified file could not be found on the
jcarsey1e6e84c2010-01-25 20:05:08 +0000733 device or the file system could not be found
jcarsey94b17fa2009-05-07 18:46:18 +0000734 on the device.
darylm503b0934ac2011-11-12 00:35:11 +0000735 @retval EFI_NO_MEDIA The device has no medium.
736 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
jcarsey94b17fa2009-05-07 18:46:18 +0000737 medium is no longer supported.
darylm503b0934ac2011-11-12 00:35:11 +0000738 @retval EFI_DEVICE_ERROR The device reported an error.
739 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
740 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
741 @retval EFI_ACCESS_DENIED The file was opened read only.
742 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
jcarsey94b17fa2009-05-07 18:46:18 +0000743 file.
darylm503b0934ac2011-11-12 00:35:11 +0000744 @retval EFI_VOLUME_FULL The volume is full.
jcarsey94b17fa2009-05-07 18:46:18 +0000745 @sa ShellOpenFileByName
746**/
747EFI_STATUS
748EFIAPI
749ShellCreateDirectory(
jcarseyb82bfcc2009-06-29 16:28:23 +0000750 IN CONST CHAR16 *DirectoryName,
jcarseya405b862010-09-14 05:18:09 +0000751 OUT SHELL_FILE_HANDLE *FileHandle
752 )
753{
jcarsey366f81a2011-06-27 21:04:22 +0000754 if (gEfiShellProtocol != NULL) {
jcarsey2247dde2009-11-09 18:08:58 +0000755 //
756 // Use UEFI Shell 2.0 method
757 //
jcarsey366f81a2011-06-27 21:04:22 +0000758 return (gEfiShellProtocol->CreateFile(DirectoryName,
jcarsey2247dde2009-11-09 18:08:58 +0000759 EFI_FILE_DIRECTORY,
760 FileHandle
jcarseya405b862010-09-14 05:18:09 +0000761 ));
jcarsey2247dde2009-11-09 18:08:58 +0000762 } else {
763 return (ShellOpenFileByName(DirectoryName,
764 FileHandle,
765 EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE,
766 EFI_FILE_DIRECTORY
jcarseya405b862010-09-14 05:18:09 +0000767 ));
jcarsey2247dde2009-11-09 18:08:58 +0000768 }
jcarsey94b17fa2009-05-07 18:46:18 +0000769}
770
771/**
772 This function reads information from an opened file.
773
jcarsey1e6e84c2010-01-25 20:05:08 +0000774 If FileHandle is not a directory, the function reads the requested number of
775 bytes from the file at the file's current position and returns them in Buffer.
jcarsey94b17fa2009-05-07 18:46:18 +0000776 If the read goes beyond the end of the file, the read length is truncated to the
jcarsey1e6e84c2010-01-25 20:05:08 +0000777 end of the file. The file's current position is increased by the number of bytes
778 returned. If FileHandle is a directory, the function reads the directory entry
779 at the file's current position and returns the entry in Buffer. If the Buffer
780 is not large enough to hold the current directory entry, then
781 EFI_BUFFER_TOO_SMALL is returned and the current file position is not updated.
782 BufferSize is set to be the size of the buffer needed to read the entry. On
783 success, the current position is updated to the next directory entry. If there
784 are no more directory entries, the read returns a zero-length buffer.
jcarsey94b17fa2009-05-07 18:46:18 +0000785 EFI_FILE_INFO is the structure returned as the directory entry.
786
787 @param FileHandle the opened file handle
jcarsey1e6e84c2010-01-25 20:05:08 +0000788 @param BufferSize on input the size of buffer in bytes. on return
jcarsey94b17fa2009-05-07 18:46:18 +0000789 the number of bytes written.
790 @param Buffer the buffer to put read data into.
791
darylm503b0934ac2011-11-12 00:35:11 +0000792 @retval EFI_SUCCESS Data was read.
793 @retval EFI_NO_MEDIA The device has no media.
794 @retval EFI_DEVICE_ERROR The device reported an error.
795 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
796 @retval EFI_BUFFER_TO_SMALL Buffer is too small. ReadSize contains required
jcarsey94b17fa2009-05-07 18:46:18 +0000797 size.
798
799**/
800EFI_STATUS
801EFIAPI
802ShellReadFile(
jcarseya405b862010-09-14 05:18:09 +0000803 IN SHELL_FILE_HANDLE FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000804 IN OUT UINTN *BufferSize,
805 OUT VOID *Buffer
jcarseya405b862010-09-14 05:18:09 +0000806 )
807{
jcarseyd2b45642009-05-11 18:02:16 +0000808 return (FileFunctionMap.ReadFile(FileHandle, BufferSize, Buffer));
jcarsey94b17fa2009-05-07 18:46:18 +0000809}
810
811
812/**
813 Write data to a file.
814
jcarsey1e6e84c2010-01-25 20:05:08 +0000815 This function writes the specified number of bytes to the file at the current
816 file position. The current file position is advanced the actual number of bytes
817 written, which is returned in BufferSize. Partial writes only occur when there
818 has been a data error during the write attempt (such as "volume space full").
819 The file is automatically grown to hold the data if required. Direct writes to
jcarsey94b17fa2009-05-07 18:46:18 +0000820 opened directories are not supported.
821
822 @param FileHandle The opened file for writing
823 @param BufferSize on input the number of bytes in Buffer. On output
824 the number of bytes written.
825 @param Buffer the buffer containing data to write is stored.
826
darylm503b0934ac2011-11-12 00:35:11 +0000827 @retval EFI_SUCCESS Data was written.
828 @retval EFI_UNSUPPORTED Writes to an open directory are not supported.
829 @retval EFI_NO_MEDIA The device has no media.
830 @retval EFI_DEVICE_ERROR The device reported an error.
831 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
832 @retval EFI_WRITE_PROTECTED The device is write-protected.
833 @retval EFI_ACCESS_DENIED The file was open for read only.
834 @retval EFI_VOLUME_FULL The volume is full.
jcarsey94b17fa2009-05-07 18:46:18 +0000835**/
836EFI_STATUS
837EFIAPI
838ShellWriteFile(
jcarsey252d9452011-03-25 20:49:53 +0000839 IN SHELL_FILE_HANDLE FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000840 IN OUT UINTN *BufferSize,
841 IN VOID *Buffer
jcarseya405b862010-09-14 05:18:09 +0000842 )
843{
jcarseyd2b45642009-05-11 18:02:16 +0000844 return (FileFunctionMap.WriteFile(FileHandle, BufferSize, Buffer));
jcarsey94b17fa2009-05-07 18:46:18 +0000845}
846
jcarsey1e6e84c2010-01-25 20:05:08 +0000847/**
jcarsey94b17fa2009-05-07 18:46:18 +0000848 Close an open file handle.
849
jcarsey1e6e84c2010-01-25 20:05:08 +0000850 This function closes a specified file handle. All "dirty" cached file data is
851 flushed to the device, and the file is closed. In all cases the handle is
jcarsey94b17fa2009-05-07 18:46:18 +0000852 closed.
853
854@param FileHandle the file handle to close.
855
856@retval EFI_SUCCESS the file handle was closed sucessfully.
857**/
858EFI_STATUS
859EFIAPI
860ShellCloseFile (
jcarseya405b862010-09-14 05:18:09 +0000861 IN SHELL_FILE_HANDLE *FileHandle
862 )
863{
jcarseyd2b45642009-05-11 18:02:16 +0000864 return (FileFunctionMap.CloseFile(*FileHandle));
jcarsey94b17fa2009-05-07 18:46:18 +0000865}
866
867/**
868 Delete a file and close the handle
869
870 This function closes and deletes a file. In all cases the file handle is closed.
jcarsey1e6e84c2010-01-25 20:05:08 +0000871 If the file cannot be deleted, the warning code EFI_WARN_DELETE_FAILURE is
jcarsey94b17fa2009-05-07 18:46:18 +0000872 returned, but the handle is still closed.
873
874 @param FileHandle the file handle to delete
875
876 @retval EFI_SUCCESS the file was closed sucessfully
jcarsey1e6e84c2010-01-25 20:05:08 +0000877 @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not
jcarsey94b17fa2009-05-07 18:46:18 +0000878 deleted
darylm503b0934ac2011-11-12 00:35:11 +0000879 @retval INVALID_PARAMETER One of the parameters has an invalid value.
jcarsey94b17fa2009-05-07 18:46:18 +0000880**/
881EFI_STATUS
882EFIAPI
883ShellDeleteFile (
darylm503b0934ac2011-11-12 00:35:11 +0000884 IN SHELL_FILE_HANDLE *FileHandle
jcarseya405b862010-09-14 05:18:09 +0000885 )
886{
jcarseyd2b45642009-05-11 18:02:16 +0000887 return (FileFunctionMap.DeleteFile(*FileHandle));
jcarsey94b17fa2009-05-07 18:46:18 +0000888}
889
890/**
891 Set the current position in a file.
892
jcarsey1e6e84c2010-01-25 20:05:08 +0000893 This function sets the current file position for the handle to the position
jcarsey94b17fa2009-05-07 18:46:18 +0000894 supplied. With the exception of seeking to position 0xFFFFFFFFFFFFFFFF, only
jcarsey1e6e84c2010-01-25 20:05:08 +0000895 absolute positioning is supported, and seeking past the end of the file is
896 allowed (a subsequent write would grow the file). Seeking to position
jcarsey94b17fa2009-05-07 18:46:18 +0000897 0xFFFFFFFFFFFFFFFF causes the current position to be set to the end of the file.
jcarsey1e6e84c2010-01-25 20:05:08 +0000898 If FileHandle is a directory, the only position that may be set is zero. This
jcarsey94b17fa2009-05-07 18:46:18 +0000899 has the effect of starting the read process of the directory entries over.
900
901 @param FileHandle The file handle on which the position is being set
902 @param Position Byte position from begining of file
903
904 @retval EFI_SUCCESS Operation completed sucessfully.
jcarsey1e6e84c2010-01-25 20:05:08 +0000905 @retval EFI_UNSUPPORTED the seek request for non-zero is not valid on
jcarsey94b17fa2009-05-07 18:46:18 +0000906 directories.
907 @retval INVALID_PARAMETER One of the parameters has an invalid value.
908**/
909EFI_STATUS
910EFIAPI
911ShellSetFilePosition (
darylm503b0934ac2011-11-12 00:35:11 +0000912 IN SHELL_FILE_HANDLE FileHandle,
913 IN UINT64 Position
jcarseya405b862010-09-14 05:18:09 +0000914 )
915{
jcarseyd2b45642009-05-11 18:02:16 +0000916 return (FileFunctionMap.SetFilePosition(FileHandle, Position));
jcarsey94b17fa2009-05-07 18:46:18 +0000917}
918
jcarsey1e6e84c2010-01-25 20:05:08 +0000919/**
jcarsey94b17fa2009-05-07 18:46:18 +0000920 Gets a file's current position
921
jcarsey1e6e84c2010-01-25 20:05:08 +0000922 This function retrieves the current file position for the file handle. For
923 directories, the current file position has no meaning outside of the file
jcarsey94b17fa2009-05-07 18:46:18 +0000924 system driver and as such the operation is not supported. An error is returned
925 if FileHandle is a directory.
926
927 @param FileHandle The open file handle on which to get the position.
928 @param Position Byte position from begining of file.
929
930 @retval EFI_SUCCESS the operation completed sucessfully.
931 @retval INVALID_PARAMETER One of the parameters has an invalid value.
932 @retval EFI_UNSUPPORTED the request is not valid on directories.
933**/
934EFI_STATUS
935EFIAPI
936ShellGetFilePosition (
jcarseya405b862010-09-14 05:18:09 +0000937 IN SHELL_FILE_HANDLE FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000938 OUT UINT64 *Position
jcarseya405b862010-09-14 05:18:09 +0000939 )
940{
jcarseyd2b45642009-05-11 18:02:16 +0000941 return (FileFunctionMap.GetFilePosition(FileHandle, Position));
jcarsey94b17fa2009-05-07 18:46:18 +0000942}
943/**
944 Flushes data on a file
jcarsey1e6e84c2010-01-25 20:05:08 +0000945
jcarsey94b17fa2009-05-07 18:46:18 +0000946 This function flushes all modified data associated with a file to a device.
947
948 @param FileHandle The file handle on which to flush data
949
950 @retval EFI_SUCCESS The data was flushed.
951 @retval EFI_NO_MEDIA The device has no media.
952 @retval EFI_DEVICE_ERROR The device reported an error.
953 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
954 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
955 @retval EFI_ACCESS_DENIED The file was opened for read only.
956**/
957EFI_STATUS
958EFIAPI
959ShellFlushFile (
jcarseya405b862010-09-14 05:18:09 +0000960 IN SHELL_FILE_HANDLE FileHandle
961 )
962{
jcarseyd2b45642009-05-11 18:02:16 +0000963 return (FileFunctionMap.FlushFile(FileHandle));
jcarsey94b17fa2009-05-07 18:46:18 +0000964}
965
darylm503b0934ac2011-11-12 00:35:11 +0000966/** Retrieve first entry from a directory.
jcarsey94b17fa2009-05-07 18:46:18 +0000967
darylm503b0934ac2011-11-12 00:35:11 +0000968 This function takes an open directory handle and gets information from the
969 first entry in the directory. A buffer is allocated to contain
970 the information and a pointer to the buffer is returned in *Buffer. The
971 caller can use ShellFindNextFile() to get subsequent directory entries.
jcarsey94b17fa2009-05-07 18:46:18 +0000972
darylm503b0934ac2011-11-12 00:35:11 +0000973 The buffer will be freed by ShellFindNextFile() when the last directory
974 entry is read. Otherwise, the caller must free the buffer, using FreePool,
975 when finished with it.
976
977 @param[in] DirHandle The file handle of the directory to search.
978 @param[out] Buffer The pointer to the buffer for the file's information.
jcarsey94b17fa2009-05-07 18:46:18 +0000979
980 @retval EFI_SUCCESS Found the first file.
981 @retval EFI_NOT_FOUND Cannot find the directory.
982 @retval EFI_NO_MEDIA The device has no media.
983 @retval EFI_DEVICE_ERROR The device reported an error.
984 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
985 @return Others status of ShellGetFileInfo, ShellSetFilePosition,
986 or ShellReadFile
987**/
988EFI_STATUS
989EFIAPI
990ShellFindFirstFile (
jcarseya405b862010-09-14 05:18:09 +0000991 IN SHELL_FILE_HANDLE DirHandle,
jcarseyd2b45642009-05-11 18:02:16 +0000992 OUT EFI_FILE_INFO **Buffer
jcarseya405b862010-09-14 05:18:09 +0000993 )
994{
jcarsey94b17fa2009-05-07 18:46:18 +0000995 //
jcarseyd2b45642009-05-11 18:02:16 +0000996 // pass to file handle lib
jcarsey94b17fa2009-05-07 18:46:18 +0000997 //
jcarseyd2b45642009-05-11 18:02:16 +0000998 return (FileHandleFindFirstFile(DirHandle, Buffer));
jcarsey94b17fa2009-05-07 18:46:18 +0000999}
darylm503b0934ac2011-11-12 00:35:11 +00001000/** Retrieve next entries from a directory.
jcarsey94b17fa2009-05-07 18:46:18 +00001001
darylm503b0934ac2011-11-12 00:35:11 +00001002 To use this function, the caller must first call the ShellFindFirstFile()
1003 function to get the first directory entry. Subsequent directory entries are
1004 retrieved by using the ShellFindNextFile() function. This function can
1005 be called several times to get each entry from the directory. If the call of
1006 ShellFindNextFile() retrieved the last directory entry, the next call of
1007 this function will set *NoFile to TRUE and free the buffer.
jcarsey94b17fa2009-05-07 18:46:18 +00001008
darylm503b0934ac2011-11-12 00:35:11 +00001009 @param[in] DirHandle The file handle of the directory.
1010 @param[out] Buffer The pointer to buffer for file's information.
1011 @param[out] NoFile The pointer to boolean when last file is found.
jcarsey94b17fa2009-05-07 18:46:18 +00001012
1013 @retval EFI_SUCCESS Found the next file, or reached last file
1014 @retval EFI_NO_MEDIA The device has no media.
1015 @retval EFI_DEVICE_ERROR The device reported an error.
1016 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
1017**/
1018EFI_STATUS
1019EFIAPI
1020ShellFindNextFile(
jcarseya405b862010-09-14 05:18:09 +00001021 IN SHELL_FILE_HANDLE DirHandle,
jcarsey94b17fa2009-05-07 18:46:18 +00001022 OUT EFI_FILE_INFO *Buffer,
1023 OUT BOOLEAN *NoFile
jcarseya405b862010-09-14 05:18:09 +00001024 )
1025{
jcarsey94b17fa2009-05-07 18:46:18 +00001026 //
jcarseyd2b45642009-05-11 18:02:16 +00001027 // pass to file handle lib
jcarsey94b17fa2009-05-07 18:46:18 +00001028 //
jcarseyd2b45642009-05-11 18:02:16 +00001029 return (FileHandleFindNextFile(DirHandle, Buffer, NoFile));
jcarsey94b17fa2009-05-07 18:46:18 +00001030}
1031/**
1032 Retrieve the size of a file.
1033
1034 if FileHandle is NULL then ASSERT()
1035 if Size is NULL then ASSERT()
1036
jcarsey1e6e84c2010-01-25 20:05:08 +00001037 This function extracts the file size info from the FileHandle's EFI_FILE_INFO
jcarsey94b17fa2009-05-07 18:46:18 +00001038 data.
1039
1040 @param FileHandle file handle from which size is retrieved
1041 @param Size pointer to size
1042
1043 @retval EFI_SUCCESS operation was completed sucessfully
1044 @retval EFI_DEVICE_ERROR cannot access the file
1045**/
1046EFI_STATUS
1047EFIAPI
1048ShellGetFileSize (
jcarseya405b862010-09-14 05:18:09 +00001049 IN SHELL_FILE_HANDLE FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +00001050 OUT UINT64 *Size
jcarseya405b862010-09-14 05:18:09 +00001051 )
1052{
jcarseyd2b45642009-05-11 18:02:16 +00001053 return (FileFunctionMap.GetFileSize(FileHandle, Size));
jcarsey94b17fa2009-05-07 18:46:18 +00001054}
1055/**
1056 Retrieves the status of the break execution flag
1057
1058 this function is useful to check whether the application is being asked to halt by the shell.
1059
1060 @retval TRUE the execution break is enabled
1061 @retval FALSE the execution break is not enabled
1062**/
1063BOOLEAN
1064EFIAPI
1065ShellGetExecutionBreakFlag(
1066 VOID
1067 )
1068{
jcarsey1e6e84c2010-01-25 20:05:08 +00001069 //
jcarsey94b17fa2009-05-07 18:46:18 +00001070 // Check for UEFI Shell 2.0 protocols
1071 //
jcarsey366f81a2011-06-27 21:04:22 +00001072 if (gEfiShellProtocol != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00001073
1074 //
1075 // We are using UEFI Shell 2.0; see if the event has been triggered
1076 //
jcarsey366f81a2011-06-27 21:04:22 +00001077 if (gBS->CheckEvent(gEfiShellProtocol->ExecutionBreak) != EFI_SUCCESS) {
jcarsey94b17fa2009-05-07 18:46:18 +00001078 return (FALSE);
1079 }
1080 return (TRUE);
jcarsey1e6e84c2010-01-25 20:05:08 +00001081 }
jcarsey94b17fa2009-05-07 18:46:18 +00001082
1083 //
1084 // using EFI Shell; call the function to check
1085 //
jcarsey92a54472011-06-27 20:33:13 +00001086 if (mEfiShellEnvironment2 != NULL) {
1087 return (mEfiShellEnvironment2->GetExecutionBreak());
1088 }
1089
1090 return (FALSE);
jcarsey94b17fa2009-05-07 18:46:18 +00001091}
1092/**
1093 return the value of an environment variable
1094
jcarsey1e6e84c2010-01-25 20:05:08 +00001095 this function gets the value of the environment variable set by the
jcarsey94b17fa2009-05-07 18:46:18 +00001096 ShellSetEnvironmentVariable function
1097
1098 @param EnvKey The key name of the environment variable.
1099
1100 @retval NULL the named environment variable does not exist.
1101 @return != NULL pointer to the value of the environment variable
1102**/
1103CONST CHAR16*
1104EFIAPI
1105ShellGetEnvironmentVariable (
jcarsey9b3bf082009-06-23 21:15:07 +00001106 IN CONST CHAR16 *EnvKey
jcarsey94b17fa2009-05-07 18:46:18 +00001107 )
1108{
jcarsey1e6e84c2010-01-25 20:05:08 +00001109 //
jcarsey94b17fa2009-05-07 18:46:18 +00001110 // Check for UEFI Shell 2.0 protocols
1111 //
jcarsey366f81a2011-06-27 21:04:22 +00001112 if (gEfiShellProtocol != NULL) {
1113 return (gEfiShellProtocol->GetEnv(EnvKey));
jcarsey94b17fa2009-05-07 18:46:18 +00001114 }
1115
1116 //
jcarsey92a54472011-06-27 20:33:13 +00001117 // Check for EFI shell
jcarsey94b17fa2009-05-07 18:46:18 +00001118 //
jcarsey92a54472011-06-27 20:33:13 +00001119 if (mEfiShellEnvironment2 != NULL) {
1120 return (mEfiShellEnvironment2->GetEnv((CHAR16*)EnvKey));
1121 }
jcarsey94b17fa2009-05-07 18:46:18 +00001122
jcarsey92a54472011-06-27 20:33:13 +00001123 return NULL;
jcarsey94b17fa2009-05-07 18:46:18 +00001124}
1125/**
1126 set the value of an environment variable
1127
1128This function changes the current value of the specified environment variable. If the
1129environment variable exists and the Value is an empty string, then the environment
1130variable is deleted. If the environment variable exists and the Value is not an empty
1131string, then the value of the environment variable is changed. If the environment
1132variable does not exist and the Value is an empty string, there is no action. If the
1133environment variable does not exist and the Value is a non-empty string, then the
1134environment variable is created and assigned the specified value.
1135
1136 This is not supported pre-UEFI Shell 2.0.
1137
1138 @param EnvKey The key name of the environment variable.
1139 @param EnvVal The Value of the environment variable
1140 @param Volatile Indicates whether the variable is non-volatile (FALSE) or volatile (TRUE).
1141
1142 @retval EFI_SUCCESS the operation was completed sucessfully
1143 @retval EFI_UNSUPPORTED This operation is not allowed in pre UEFI 2.0 Shell environments
1144**/
1145EFI_STATUS
1146EFIAPI
1147ShellSetEnvironmentVariable (
1148 IN CONST CHAR16 *EnvKey,
1149 IN CONST CHAR16 *EnvVal,
1150 IN BOOLEAN Volatile
1151 )
1152{
jcarsey1e6e84c2010-01-25 20:05:08 +00001153 //
jcarsey94b17fa2009-05-07 18:46:18 +00001154 // Check for UEFI Shell 2.0 protocols
1155 //
jcarsey366f81a2011-06-27 21:04:22 +00001156 if (gEfiShellProtocol != NULL) {
1157 return (gEfiShellProtocol->SetEnv(EnvKey, EnvVal, Volatile));
jcarsey1e6e84c2010-01-25 20:05:08 +00001158 }
jcarsey94b17fa2009-05-07 18:46:18 +00001159
1160 //
1161 // This feature does not exist under EFI shell
1162 //
1163 return (EFI_UNSUPPORTED);
1164}
jcarseya405b862010-09-14 05:18:09 +00001165
jcarsey94b17fa2009-05-07 18:46:18 +00001166/**
jcarseya405b862010-09-14 05:18:09 +00001167 Cause the shell to parse and execute a command line.
jcarsey94b17fa2009-05-07 18:46:18 +00001168
1169 This function creates a nested instance of the shell and executes the specified
jcarseya405b862010-09-14 05:18:09 +00001170 command (CommandLine) with the specified environment (Environment). Upon return,
1171 the status code returned by the specified command is placed in StatusCode.
1172 If Environment is NULL, then the current environment is used and all changes made
1173 by the commands executed will be reflected in the current environment. If the
1174 Environment is non-NULL, then the changes made will be discarded.
1175 The CommandLine is executed from the current working directory on the current
1176 device.
jcarsey94b17fa2009-05-07 18:46:18 +00001177
jcarseya405b862010-09-14 05:18:09 +00001178 The EnvironmentVariables and Status parameters are ignored in a pre-UEFI Shell 2.0
1179 environment. The values pointed to by the parameters will be unchanged by the
1180 ShellExecute() function. The Output parameter has no effect in a
1181 UEFI Shell 2.0 environment.
jcarsey94b17fa2009-05-07 18:46:18 +00001182
jcarseya405b862010-09-14 05:18:09 +00001183 @param[in] ParentHandle The parent image starting the operation.
1184 @param[in] CommandLine The pointer to a NULL terminated command line.
1185 @param[in] Output True to display debug output. False to hide it.
1186 @param[in] EnvironmentVariables Optional pointer to array of environment variables
1187 in the form "x=y". If NULL, the current set is used.
1188 @param[out] Status The status of the run command line.
jcarsey94b17fa2009-05-07 18:46:18 +00001189
jcarseya405b862010-09-14 05:18:09 +00001190 @retval EFI_SUCCESS The operation completed sucessfully. Status
1191 contains the status code returned.
1192 @retval EFI_INVALID_PARAMETER A parameter contains an invalid value.
1193 @retval EFI_OUT_OF_RESOURCES Out of resources.
1194 @retval EFI_UNSUPPORTED The operation is not allowed.
jcarsey94b17fa2009-05-07 18:46:18 +00001195**/
1196EFI_STATUS
1197EFIAPI
1198ShellExecute (
1199 IN EFI_HANDLE *ParentHandle,
1200 IN CHAR16 *CommandLine OPTIONAL,
1201 IN BOOLEAN Output OPTIONAL,
1202 IN CHAR16 **EnvironmentVariables OPTIONAL,
1203 OUT EFI_STATUS *Status OPTIONAL
1204 )
1205{
jcarsey1e6e84c2010-01-25 20:05:08 +00001206 //
jcarsey94b17fa2009-05-07 18:46:18 +00001207 // Check for UEFI Shell 2.0 protocols
1208 //
jcarsey366f81a2011-06-27 21:04:22 +00001209 if (gEfiShellProtocol != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00001210 //
1211 // Call UEFI Shell 2.0 version (not using Output parameter)
1212 //
jcarsey366f81a2011-06-27 21:04:22 +00001213 return (gEfiShellProtocol->Execute(ParentHandle,
jcarsey94b17fa2009-05-07 18:46:18 +00001214 CommandLine,
1215 EnvironmentVariables,
1216 Status));
jcarsey1e6e84c2010-01-25 20:05:08 +00001217 }
jcarsey92a54472011-06-27 20:33:13 +00001218
jcarsey94b17fa2009-05-07 18:46:18 +00001219 //
jcarsey92a54472011-06-27 20:33:13 +00001220 // Check for EFI shell
jcarsey94b17fa2009-05-07 18:46:18 +00001221 //
jcarsey92a54472011-06-27 20:33:13 +00001222 if (mEfiShellEnvironment2 != NULL) {
1223 //
1224 // Call EFI Shell version (not using EnvironmentVariables or Status parameters)
1225 // Due to oddity in the EFI shell we want to dereference the ParentHandle here
1226 //
1227 return (mEfiShellEnvironment2->Execute(*ParentHandle,
1228 CommandLine,
1229 Output));
1230 }
1231
1232 return (EFI_UNSUPPORTED);
jcarsey94b17fa2009-05-07 18:46:18 +00001233}
1234/**
1235 Retreives the current directory path
1236
jcarsey1e6e84c2010-01-25 20:05:08 +00001237 If the DeviceName is NULL, it returns the current device's current directory
1238 name. If the DeviceName is not NULL, it returns the current directory name
jcarsey94b17fa2009-05-07 18:46:18 +00001239 on specified drive.
1240
1241 @param DeviceName the name of the drive to get directory on
1242
1243 @retval NULL the directory does not exist
1244 @return != NULL the directory
1245**/
1246CONST CHAR16*
1247EFIAPI
1248ShellGetCurrentDir (
jcarseya405b862010-09-14 05:18:09 +00001249 IN CHAR16 * CONST DeviceName OPTIONAL
jcarsey94b17fa2009-05-07 18:46:18 +00001250 )
1251{
jcarsey1e6e84c2010-01-25 20:05:08 +00001252 //
jcarsey94b17fa2009-05-07 18:46:18 +00001253 // Check for UEFI Shell 2.0 protocols
1254 //
jcarsey366f81a2011-06-27 21:04:22 +00001255 if (gEfiShellProtocol != NULL) {
1256 return (gEfiShellProtocol->GetCurDir(DeviceName));
jcarsey1e6e84c2010-01-25 20:05:08 +00001257 }
jcarsey92a54472011-06-27 20:33:13 +00001258
jcarsey94b17fa2009-05-07 18:46:18 +00001259 //
jcarsey8bd282b2011-06-27 16:45:41 +00001260 // Check for EFI shell
jcarsey94b17fa2009-05-07 18:46:18 +00001261 //
jcarsey8bd282b2011-06-27 16:45:41 +00001262 if (mEfiShellEnvironment2 != NULL) {
1263 return (mEfiShellEnvironment2->CurDir(DeviceName));
1264 }
1265
1266 return (NULL);
jcarsey94b17fa2009-05-07 18:46:18 +00001267}
1268/**
1269 sets (enabled or disabled) the page break mode
1270
jcarsey1e6e84c2010-01-25 20:05:08 +00001271 when page break mode is enabled the screen will stop scrolling
jcarsey94b17fa2009-05-07 18:46:18 +00001272 and wait for operator input before scrolling a subsequent screen.
1273
1274 @param CurrentState TRUE to enable and FALSE to disable
1275**/
jcarsey1e6e84c2010-01-25 20:05:08 +00001276VOID
jcarsey94b17fa2009-05-07 18:46:18 +00001277EFIAPI
1278ShellSetPageBreakMode (
1279 IN BOOLEAN CurrentState
1280 )
1281{
1282 //
1283 // check for enabling
1284 //
1285 if (CurrentState != 0x00) {
jcarsey1e6e84c2010-01-25 20:05:08 +00001286 //
jcarsey94b17fa2009-05-07 18:46:18 +00001287 // check for UEFI Shell 2.0
1288 //
jcarsey366f81a2011-06-27 21:04:22 +00001289 if (gEfiShellProtocol != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00001290 //
1291 // Enable with UEFI 2.0 Shell
1292 //
jcarsey366f81a2011-06-27 21:04:22 +00001293 gEfiShellProtocol->EnablePageBreak();
jcarsey94b17fa2009-05-07 18:46:18 +00001294 return;
1295 } else {
jcarsey1e6e84c2010-01-25 20:05:08 +00001296 //
jcarsey92a54472011-06-27 20:33:13 +00001297 // Check for EFI shell
jcarsey94b17fa2009-05-07 18:46:18 +00001298 //
jcarsey92a54472011-06-27 20:33:13 +00001299 if (mEfiShellEnvironment2 != NULL) {
1300 //
1301 // Enable with EFI Shell
1302 //
1303 mEfiShellEnvironment2->EnablePageBreak (DEFAULT_INIT_ROW, DEFAULT_AUTO_LF);
1304 return;
1305 }
jcarsey94b17fa2009-05-07 18:46:18 +00001306 }
1307 } else {
jcarsey1e6e84c2010-01-25 20:05:08 +00001308 //
jcarsey94b17fa2009-05-07 18:46:18 +00001309 // check for UEFI Shell 2.0
1310 //
jcarsey366f81a2011-06-27 21:04:22 +00001311 if (gEfiShellProtocol != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00001312 //
1313 // Disable with UEFI 2.0 Shell
1314 //
jcarsey366f81a2011-06-27 21:04:22 +00001315 gEfiShellProtocol->DisablePageBreak();
jcarsey94b17fa2009-05-07 18:46:18 +00001316 return;
1317 } else {
jcarsey1e6e84c2010-01-25 20:05:08 +00001318 //
jcarsey92a54472011-06-27 20:33:13 +00001319 // Check for EFI shell
jcarsey94b17fa2009-05-07 18:46:18 +00001320 //
jcarsey92a54472011-06-27 20:33:13 +00001321 if (mEfiShellEnvironment2 != NULL) {
1322 //
1323 // Disable with EFI Shell
1324 //
1325 mEfiShellEnvironment2->DisablePageBreak ();
1326 return;
1327 }
jcarsey94b17fa2009-05-07 18:46:18 +00001328 }
1329 }
1330}
1331
1332///
1333/// version of EFI_SHELL_FILE_INFO struct, except has no CONST pointers.
1334/// This allows for the struct to be populated.
1335///
1336typedef struct {
jcarseyd2b45642009-05-11 18:02:16 +00001337 LIST_ENTRY Link;
jcarsey94b17fa2009-05-07 18:46:18 +00001338 EFI_STATUS Status;
1339 CHAR16 *FullName;
1340 CHAR16 *FileName;
jcarseya405b862010-09-14 05:18:09 +00001341 SHELL_FILE_HANDLE Handle;
jcarsey94b17fa2009-05-07 18:46:18 +00001342 EFI_FILE_INFO *Info;
1343} EFI_SHELL_FILE_INFO_NO_CONST;
1344
1345/**
1346 Converts a EFI shell list of structures to the coresponding UEFI Shell 2.0 type of list.
1347
1348 if OldStyleFileList is NULL then ASSERT()
1349
jcarsey1e6e84c2010-01-25 20:05:08 +00001350 this function will convert a SHELL_FILE_ARG based list into a callee allocated
jcarsey94b17fa2009-05-07 18:46:18 +00001351 EFI_SHELL_FILE_INFO based list. it is up to the caller to free the memory via
1352 the ShellCloseFileMetaArg function.
1353
ydong104ff7e372011-09-02 08:05:34 +00001354 @param[in] FileList the EFI shell list type
1355 @param[in, out] ListHead the list to add to
jcarsey94b17fa2009-05-07 18:46:18 +00001356
1357 @retval the resultant head of the double linked new format list;
1358**/
1359LIST_ENTRY*
1360EFIAPI
1361InternalShellConvertFileListType (
jcarsey9b3bf082009-06-23 21:15:07 +00001362 IN LIST_ENTRY *FileList,
1363 IN OUT LIST_ENTRY *ListHead
jcarsey125c2cf2009-11-18 21:36:50 +00001364 )
1365{
jcarsey94b17fa2009-05-07 18:46:18 +00001366 SHELL_FILE_ARG *OldInfo;
jcarsey9b3bf082009-06-23 21:15:07 +00001367 LIST_ENTRY *Link;
jcarsey94b17fa2009-05-07 18:46:18 +00001368 EFI_SHELL_FILE_INFO_NO_CONST *NewInfo;
1369
1370 //
jcarsey9b3bf082009-06-23 21:15:07 +00001371 // ASSERTs
jcarsey94b17fa2009-05-07 18:46:18 +00001372 //
jcarsey9b3bf082009-06-23 21:15:07 +00001373 ASSERT(FileList != NULL);
1374 ASSERT(ListHead != NULL);
jcarsey94b17fa2009-05-07 18:46:18 +00001375
1376 //
1377 // enumerate through each member of the old list and copy
1378 //
jcarseyd2b45642009-05-11 18:02:16 +00001379 for (Link = FileList->ForwardLink; Link != FileList; Link = Link->ForwardLink) {
jcarsey94b17fa2009-05-07 18:46:18 +00001380 OldInfo = CR (Link, SHELL_FILE_ARG, Link, SHELL_FILE_ARG_SIGNATURE);
jcarseya405b862010-09-14 05:18:09 +00001381 ASSERT(OldInfo != NULL);
1382
1383 //
1384 // Skip ones that failed to open...
1385 //
1386 if (OldInfo->Status != EFI_SUCCESS) {
1387 continue;
1388 }
jcarsey94b17fa2009-05-07 18:46:18 +00001389
1390 //
1391 // make sure the old list was valid
1392 //
jcarsey94b17fa2009-05-07 18:46:18 +00001393 ASSERT(OldInfo->Info != NULL);
1394 ASSERT(OldInfo->FullName != NULL);
1395 ASSERT(OldInfo->FileName != NULL);
1396
1397 //
1398 // allocate a new EFI_SHELL_FILE_INFO object
1399 //
1400 NewInfo = AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));
jcarseyc9d92df2010-02-03 15:37:54 +00001401 if (NewInfo == NULL) {
jcarsey7a95efd2011-10-13 16:08:18 +00001402 ShellCloseFileMetaArg((EFI_SHELL_FILE_INFO**)(&ListHead));
jcarseybeab0fc2011-10-10 17:26:25 +00001403 ListHead = NULL;
jcarseyc9d92df2010-02-03 15:37:54 +00001404 break;
1405 }
jcarsey1e6e84c2010-01-25 20:05:08 +00001406
1407 //
jcarsey94b17fa2009-05-07 18:46:18 +00001408 // copy the simple items
1409 //
1410 NewInfo->Handle = OldInfo->Handle;
1411 NewInfo->Status = OldInfo->Status;
1412
jcarseyd2b45642009-05-11 18:02:16 +00001413 // old shell checks for 0 not NULL
1414 OldInfo->Handle = 0;
1415
jcarsey94b17fa2009-05-07 18:46:18 +00001416 //
1417 // allocate new space to copy strings and structure
1418 //
1419 NewInfo->FullName = AllocateZeroPool(StrSize(OldInfo->FullName));
1420 NewInfo->FileName = AllocateZeroPool(StrSize(OldInfo->FileName));
1421 NewInfo->Info = AllocateZeroPool((UINTN)OldInfo->Info->Size);
jcarsey1e6e84c2010-01-25 20:05:08 +00001422
jcarsey94b17fa2009-05-07 18:46:18 +00001423 //
1424 // make sure all the memory allocations were sucessful
1425 //
jcarseybeab0fc2011-10-10 17:26:25 +00001426 if (NULL == NewInfo->FullName || NewInfo->FileName == NULL || NewInfo->Info == NULL) {
jcarsey7a95efd2011-10-13 16:08:18 +00001427 ShellCloseFileMetaArg((EFI_SHELL_FILE_INFO**)(&ListHead));
jcarseybeab0fc2011-10-10 17:26:25 +00001428 ListHead = NULL;
1429 break;
1430 }
jcarsey94b17fa2009-05-07 18:46:18 +00001431
1432 //
1433 // Copt the strings and structure
1434 //
1435 StrCpy(NewInfo->FullName, OldInfo->FullName);
1436 StrCpy(NewInfo->FileName, OldInfo->FileName);
1437 gBS->CopyMem (NewInfo->Info, OldInfo->Info, (UINTN)OldInfo->Info->Size);
1438
1439 //
1440 // add that to the list
1441 //
jcarsey9b3bf082009-06-23 21:15:07 +00001442 InsertTailList(ListHead, &NewInfo->Link);
jcarsey94b17fa2009-05-07 18:46:18 +00001443 }
1444 return (ListHead);
1445}
1446/**
1447 Opens a group of files based on a path.
1448
jcarsey1e6e84c2010-01-25 20:05:08 +00001449 This function uses the Arg to open all the matching files. Each matched
1450 file has a SHELL_FILE_ARG structure to record the file information. These
1451 structures are placed on the list ListHead. Users can get the SHELL_FILE_ARG
jcarsey94b17fa2009-05-07 18:46:18 +00001452 structures from ListHead to access each file. This function supports wildcards
jcarsey1e6e84c2010-01-25 20:05:08 +00001453 and will process '?' and '*' as such. the list must be freed with a call to
jcarsey94b17fa2009-05-07 18:46:18 +00001454 ShellCloseFileMetaArg().
1455
jcarsey1e6e84c2010-01-25 20:05:08 +00001456 If you are NOT appending to an existing list *ListHead must be NULL. If
jcarsey5f7431d2009-07-10 18:06:01 +00001457 *ListHead is NULL then it must be callee freed.
jcarsey94b17fa2009-05-07 18:46:18 +00001458
1459 @param Arg pointer to path string
1460 @param OpenMode mode to open files with
1461 @param ListHead head of linked list of results
1462
jcarsey1e6e84c2010-01-25 20:05:08 +00001463 @retval EFI_SUCCESS the operation was sucessful and the list head
jcarsey94b17fa2009-05-07 18:46:18 +00001464 contains the list of opened files
jcarsey94b17fa2009-05-07 18:46:18 +00001465 @return != EFI_SUCCESS the operation failed
1466
1467 @sa InternalShellConvertFileListType
1468**/
1469EFI_STATUS
1470EFIAPI
1471ShellOpenFileMetaArg (
1472 IN CHAR16 *Arg,
1473 IN UINT64 OpenMode,
1474 IN OUT EFI_SHELL_FILE_INFO **ListHead
1475 )
1476{
1477 EFI_STATUS Status;
jcarsey9b3bf082009-06-23 21:15:07 +00001478 LIST_ENTRY mOldStyleFileList;
jcarsey1e6e84c2010-01-25 20:05:08 +00001479
jcarsey94b17fa2009-05-07 18:46:18 +00001480 //
1481 // ASSERT that Arg and ListHead are not NULL
1482 //
1483 ASSERT(Arg != NULL);
1484 ASSERT(ListHead != NULL);
1485
jcarsey1e6e84c2010-01-25 20:05:08 +00001486 //
jcarsey94b17fa2009-05-07 18:46:18 +00001487 // Check for UEFI Shell 2.0 protocols
1488 //
jcarsey366f81a2011-06-27 21:04:22 +00001489 if (gEfiShellProtocol != NULL) {
jcarsey5f7431d2009-07-10 18:06:01 +00001490 if (*ListHead == NULL) {
1491 *ListHead = (EFI_SHELL_FILE_INFO*)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));
1492 if (*ListHead == NULL) {
1493 return (EFI_OUT_OF_RESOURCES);
1494 }
1495 InitializeListHead(&((*ListHead)->Link));
jcarsey1e6e84c2010-01-25 20:05:08 +00001496 }
jcarsey366f81a2011-06-27 21:04:22 +00001497 Status = gEfiShellProtocol->OpenFileList(Arg,
jcarsey1e6e84c2010-01-25 20:05:08 +00001498 OpenMode,
jcarsey2247dde2009-11-09 18:08:58 +00001499 ListHead);
1500 if (EFI_ERROR(Status)) {
jcarsey366f81a2011-06-27 21:04:22 +00001501 gEfiShellProtocol->RemoveDupInFileList(ListHead);
jcarsey2247dde2009-11-09 18:08:58 +00001502 } else {
jcarsey366f81a2011-06-27 21:04:22 +00001503 Status = gEfiShellProtocol->RemoveDupInFileList(ListHead);
jcarsey2247dde2009-11-09 18:08:58 +00001504 }
jcarseya405b862010-09-14 05:18:09 +00001505 if (*ListHead != NULL && IsListEmpty(&(*ListHead)->Link)) {
1506 FreePool(*ListHead);
1507 *ListHead = NULL;
1508 return (EFI_NOT_FOUND);
1509 }
jcarsey2247dde2009-11-09 18:08:58 +00001510 return (Status);
jcarsey1e6e84c2010-01-25 20:05:08 +00001511 }
jcarsey94b17fa2009-05-07 18:46:18 +00001512
1513 //
jcarsey92a54472011-06-27 20:33:13 +00001514 // Check for EFI shell
jcarsey94b17fa2009-05-07 18:46:18 +00001515 //
jcarsey92a54472011-06-27 20:33:13 +00001516 if (mEfiShellEnvironment2 != NULL) {
1517 //
1518 // make sure the list head is initialized
1519 //
1520 InitializeListHead(&mOldStyleFileList);
jcarsey94b17fa2009-05-07 18:46:18 +00001521
jcarsey92a54472011-06-27 20:33:13 +00001522 //
1523 // Get the EFI Shell list of files
1524 //
1525 Status = mEfiShellEnvironment2->FileMetaArg(Arg, &mOldStyleFileList);
1526 if (EFI_ERROR(Status)) {
1527 *ListHead = NULL;
1528 return (Status);
1529 }
jcarsey94b17fa2009-05-07 18:46:18 +00001530
jcarsey92a54472011-06-27 20:33:13 +00001531 if (*ListHead == NULL) {
1532 *ListHead = (EFI_SHELL_FILE_INFO *)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));
1533 if (*ListHead == NULL) {
1534 return (EFI_OUT_OF_RESOURCES);
1535 }
1536 InitializeListHead(&((*ListHead)->Link));
1537 }
1538
1539 //
1540 // Convert that to equivalent of UEFI Shell 2.0 structure
1541 //
1542 InternalShellConvertFileListType(&mOldStyleFileList, &(*ListHead)->Link);
1543
1544 //
1545 // Free the EFI Shell version that was converted.
1546 //
1547 mEfiShellEnvironment2->FreeFileList(&mOldStyleFileList);
1548
1549 if ((*ListHead)->Link.ForwardLink == (*ListHead)->Link.BackLink && (*ListHead)->Link.BackLink == &((*ListHead)->Link)) {
1550 FreePool(*ListHead);
1551 *ListHead = NULL;
1552 Status = EFI_NOT_FOUND;
1553 }
jcarsey94b17fa2009-05-07 18:46:18 +00001554 return (Status);
1555 }
1556
jcarsey92a54472011-06-27 20:33:13 +00001557 return (EFI_UNSUPPORTED);
jcarsey94b17fa2009-05-07 18:46:18 +00001558}
1559/**
jcarseya405b862010-09-14 05:18:09 +00001560 Free the linked list returned from ShellOpenFileMetaArg.
jcarsey94b17fa2009-05-07 18:46:18 +00001561
jcarseya405b862010-09-14 05:18:09 +00001562 if ListHead is NULL then ASSERT().
jcarsey94b17fa2009-05-07 18:46:18 +00001563
jcarseya405b862010-09-14 05:18:09 +00001564 @param ListHead the pointer to free.
jcarsey94b17fa2009-05-07 18:46:18 +00001565
jcarseya405b862010-09-14 05:18:09 +00001566 @retval EFI_SUCCESS the operation was sucessful.
jcarsey94b17fa2009-05-07 18:46:18 +00001567**/
1568EFI_STATUS
1569EFIAPI
1570ShellCloseFileMetaArg (
1571 IN OUT EFI_SHELL_FILE_INFO **ListHead
1572 )
1573{
1574 LIST_ENTRY *Node;
1575
1576 //
1577 // ASSERT that ListHead is not NULL
1578 //
1579 ASSERT(ListHead != NULL);
1580
jcarsey1e6e84c2010-01-25 20:05:08 +00001581 //
jcarsey94b17fa2009-05-07 18:46:18 +00001582 // Check for UEFI Shell 2.0 protocols
1583 //
jcarsey366f81a2011-06-27 21:04:22 +00001584 if (gEfiShellProtocol != NULL) {
1585 return (gEfiShellProtocol->FreeFileList(ListHead));
jcarsey92a54472011-06-27 20:33:13 +00001586 } else if (mEfiShellEnvironment2 != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00001587 //
jcarsey1e6e84c2010-01-25 20:05:08 +00001588 // Since this is EFI Shell version we need to free our internally made copy
jcarsey94b17fa2009-05-07 18:46:18 +00001589 // of the list
1590 //
jcarsey1e6e84c2010-01-25 20:05:08 +00001591 for ( Node = GetFirstNode(&(*ListHead)->Link)
jcarseya405b862010-09-14 05:18:09 +00001592 ; *ListHead != NULL && !IsListEmpty(&(*ListHead)->Link)
jcarsey9b3bf082009-06-23 21:15:07 +00001593 ; Node = GetFirstNode(&(*ListHead)->Link)) {
jcarsey94b17fa2009-05-07 18:46:18 +00001594 RemoveEntryList(Node);
jcarseya405b862010-09-14 05:18:09 +00001595 ((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 +00001596 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->FullName);
1597 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->FileName);
1598 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->Info);
1599 FreePool((EFI_SHELL_FILE_INFO_NO_CONST*)Node);
1600 }
Jaben Carsey75a5e2e2014-01-10 16:42:45 +00001601 SHELL_FREE_NON_NULL(*ListHead);
jcarsey94b17fa2009-05-07 18:46:18 +00001602 return EFI_SUCCESS;
1603 }
jcarsey92a54472011-06-27 20:33:13 +00001604
1605 return (EFI_UNSUPPORTED);
jcarsey94b17fa2009-05-07 18:46:18 +00001606}
1607
jcarsey125c2cf2009-11-18 21:36:50 +00001608/**
1609 Find a file by searching the CWD and then the path.
1610
jcarseyb3011f42010-01-11 21:49:04 +00001611 If FileName is NULL then ASSERT.
jcarsey125c2cf2009-11-18 21:36:50 +00001612
jcarseyb3011f42010-01-11 21:49:04 +00001613 If the return value is not NULL then the memory must be caller freed.
jcarsey125c2cf2009-11-18 21:36:50 +00001614
1615 @param FileName Filename string.
1616
1617 @retval NULL the file was not found
1618 @return !NULL the full path to the file.
1619**/
1620CHAR16 *
1621EFIAPI
1622ShellFindFilePath (
1623 IN CONST CHAR16 *FileName
1624 )
1625{
1626 CONST CHAR16 *Path;
jcarseya405b862010-09-14 05:18:09 +00001627 SHELL_FILE_HANDLE Handle;
jcarsey125c2cf2009-11-18 21:36:50 +00001628 EFI_STATUS Status;
1629 CHAR16 *RetVal;
1630 CHAR16 *TestPath;
1631 CONST CHAR16 *Walker;
jcarsey36a9d672009-11-20 21:13:41 +00001632 UINTN Size;
jcarsey1cd45e72010-01-29 15:07:44 +00001633 CHAR16 *TempChar;
jcarsey125c2cf2009-11-18 21:36:50 +00001634
1635 RetVal = NULL;
1636
jcarseya405b862010-09-14 05:18:09 +00001637 //
1638 // First make sure its not an absolute path.
1639 //
1640 Status = ShellOpenFileByName(FileName, &Handle, EFI_FILE_MODE_READ, 0);
1641 if (!EFI_ERROR(Status)){
1642 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {
1643 ASSERT(RetVal == NULL);
1644 RetVal = StrnCatGrow(&RetVal, NULL, FileName, 0);
1645 ShellCloseFile(&Handle);
1646 return (RetVal);
1647 } else {
1648 ShellCloseFile(&Handle);
1649 }
1650 }
1651
jcarsey125c2cf2009-11-18 21:36:50 +00001652 Path = ShellGetEnvironmentVariable(L"cwd");
1653 if (Path != NULL) {
jcarsey36a9d672009-11-20 21:13:41 +00001654 Size = StrSize(Path);
1655 Size += StrSize(FileName);
1656 TestPath = AllocateZeroPool(Size);
jcarseyc9d92df2010-02-03 15:37:54 +00001657 if (TestPath == NULL) {
1658 return (NULL);
1659 }
jcarsey125c2cf2009-11-18 21:36:50 +00001660 StrCpy(TestPath, Path);
1661 StrCat(TestPath, FileName);
1662 Status = ShellOpenFileByName(TestPath, &Handle, EFI_FILE_MODE_READ, 0);
1663 if (!EFI_ERROR(Status)){
jcarseya405b862010-09-14 05:18:09 +00001664 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {
1665 ASSERT(RetVal == NULL);
1666 RetVal = StrnCatGrow(&RetVal, NULL, TestPath, 0);
1667 ShellCloseFile(&Handle);
1668 FreePool(TestPath);
1669 return (RetVal);
1670 } else {
1671 ShellCloseFile(&Handle);
1672 }
jcarsey125c2cf2009-11-18 21:36:50 +00001673 }
1674 FreePool(TestPath);
1675 }
1676 Path = ShellGetEnvironmentVariable(L"path");
1677 if (Path != NULL) {
jcarseya405b862010-09-14 05:18:09 +00001678 Size = StrSize(Path)+sizeof(CHAR16);
jcarsey36a9d672009-11-20 21:13:41 +00001679 Size += StrSize(FileName);
1680 TestPath = AllocateZeroPool(Size);
jcarsey3e082d52010-10-04 16:44:57 +00001681 if (TestPath == NULL) {
1682 return (NULL);
1683 }
jcarsey1e6e84c2010-01-25 20:05:08 +00001684 Walker = (CHAR16*)Path;
jcarsey125c2cf2009-11-18 21:36:50 +00001685 do {
1686 CopyMem(TestPath, Walker, StrSize(Walker));
jcarsey3e082d52010-10-04 16:44:57 +00001687 if (TestPath != NULL) {
1688 TempChar = StrStr(TestPath, L";");
1689 if (TempChar != NULL) {
1690 *TempChar = CHAR_NULL;
1691 }
1692 if (TestPath[StrLen(TestPath)-1] != L'\\') {
1693 StrCat(TestPath, L"\\");
1694 }
jcarsey89e85372011-04-13 23:37:50 +00001695 if (FileName[0] == L'\\') {
1696 FileName++;
1697 }
jcarsey3e082d52010-10-04 16:44:57 +00001698 StrCat(TestPath, FileName);
1699 if (StrStr(Walker, L";") != NULL) {
1700 Walker = StrStr(Walker, L";") + 1;
jcarseya405b862010-09-14 05:18:09 +00001701 } else {
jcarsey3e082d52010-10-04 16:44:57 +00001702 Walker = NULL;
1703 }
1704 Status = ShellOpenFileByName(TestPath, &Handle, EFI_FILE_MODE_READ, 0);
1705 if (!EFI_ERROR(Status)){
1706 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {
1707 ASSERT(RetVal == NULL);
1708 RetVal = StrnCatGrow(&RetVal, NULL, TestPath, 0);
1709 ShellCloseFile(&Handle);
1710 break;
1711 } else {
1712 ShellCloseFile(&Handle);
1713 }
jcarseya405b862010-09-14 05:18:09 +00001714 }
jcarsey125c2cf2009-11-18 21:36:50 +00001715 }
1716 } while (Walker != NULL && Walker[0] != CHAR_NULL);
1717 FreePool(TestPath);
1718 }
1719 return (RetVal);
1720}
1721
jcarseyb3011f42010-01-11 21:49:04 +00001722/**
jcarsey1e6e84c2010-01-25 20:05:08 +00001723 Find a file by searching the CWD and then the path with a variable set of file
1724 extensions. If the file is not found it will append each extension in the list
jcarseyb3011f42010-01-11 21:49:04 +00001725 in the order provided and return the first one that is successful.
1726
1727 If FileName is NULL, then ASSERT.
1728 If FileExtension is NULL, then behavior is identical to ShellFindFilePath.
1729
1730 If the return value is not NULL then the memory must be caller freed.
1731
1732 @param[in] FileName Filename string.
1733 @param[in] FileExtension Semi-colon delimeted list of possible extensions.
1734
1735 @retval NULL The file was not found.
1736 @retval !NULL The path to the file.
1737**/
1738CHAR16 *
1739EFIAPI
1740ShellFindFilePathEx (
1741 IN CONST CHAR16 *FileName,
1742 IN CONST CHAR16 *FileExtension
1743 )
1744{
1745 CHAR16 *TestPath;
1746 CHAR16 *RetVal;
1747 CONST CHAR16 *ExtensionWalker;
jcarsey9e926b62010-01-14 20:26:39 +00001748 UINTN Size;
jcarsey1cd45e72010-01-29 15:07:44 +00001749 CHAR16 *TempChar;
jcarseyc9d92df2010-02-03 15:37:54 +00001750 CHAR16 *TempChar2;
jcarsey1cd45e72010-01-29 15:07:44 +00001751
jcarseyb3011f42010-01-11 21:49:04 +00001752 ASSERT(FileName != NULL);
1753 if (FileExtension == NULL) {
1754 return (ShellFindFilePath(FileName));
1755 }
1756 RetVal = ShellFindFilePath(FileName);
1757 if (RetVal != NULL) {
1758 return (RetVal);
1759 }
jcarsey9e926b62010-01-14 20:26:39 +00001760 Size = StrSize(FileName);
1761 Size += StrSize(FileExtension);
1762 TestPath = AllocateZeroPool(Size);
jcarseyc9d92df2010-02-03 15:37:54 +00001763 if (TestPath == NULL) {
1764 return (NULL);
1765 }
jcarseya405b862010-09-14 05:18:09 +00001766 for (ExtensionWalker = FileExtension, TempChar2 = (CHAR16*)FileExtension; TempChar2 != NULL ; ExtensionWalker = TempChar2 + 1){
jcarseyb3011f42010-01-11 21:49:04 +00001767 StrCpy(TestPath, FileName);
jcarseya405b862010-09-14 05:18:09 +00001768 if (ExtensionWalker != NULL) {
1769 StrCat(TestPath, ExtensionWalker);
1770 }
jcarsey1cd45e72010-01-29 15:07:44 +00001771 TempChar = StrStr(TestPath, L";");
1772 if (TempChar != NULL) {
1773 *TempChar = CHAR_NULL;
jcarseyb3011f42010-01-11 21:49:04 +00001774 }
1775 RetVal = ShellFindFilePath(TestPath);
1776 if (RetVal != NULL) {
1777 break;
1778 }
jcarseya405b862010-09-14 05:18:09 +00001779 ASSERT(ExtensionWalker != NULL);
jcarseyc9d92df2010-02-03 15:37:54 +00001780 TempChar2 = StrStr(ExtensionWalker, L";");
jcarseyb3011f42010-01-11 21:49:04 +00001781 }
1782 FreePool(TestPath);
1783 return (RetVal);
1784}
1785
jcarsey94b17fa2009-05-07 18:46:18 +00001786typedef struct {
jcarsey9b3bf082009-06-23 21:15:07 +00001787 LIST_ENTRY Link;
jcarsey94b17fa2009-05-07 18:46:18 +00001788 CHAR16 *Name;
jcarseya405b862010-09-14 05:18:09 +00001789 SHELL_PARAM_TYPE Type;
jcarsey94b17fa2009-05-07 18:46:18 +00001790 CHAR16 *Value;
1791 UINTN OriginalPosition;
1792} SHELL_PARAM_PACKAGE;
1793
1794/**
jcarsey1e6e84c2010-01-25 20:05:08 +00001795 Checks the list of valid arguments and returns TRUE if the item was found. If the
jcarsey94b17fa2009-05-07 18:46:18 +00001796 return value is TRUE then the type parameter is set also.
jcarsey1e6e84c2010-01-25 20:05:08 +00001797
jcarsey94b17fa2009-05-07 18:46:18 +00001798 if CheckList is NULL then ASSERT();
1799 if Name is NULL then ASSERT();
1800 if Type is NULL then ASSERT();
1801
jcarsey94b17fa2009-05-07 18:46:18 +00001802 @param Name pointer to Name of parameter found
1803 @param CheckList List to check against
jcarseya405b862010-09-14 05:18:09 +00001804 @param Type pointer to type of parameter if it was found
jcarsey94b17fa2009-05-07 18:46:18 +00001805
1806 @retval TRUE the Parameter was found. Type is valid.
1807 @retval FALSE the Parameter was not found. Type is not valid.
1808**/
1809BOOLEAN
1810EFIAPI
jcarseyd2b45642009-05-11 18:02:16 +00001811InternalIsOnCheckList (
jcarsey94b17fa2009-05-07 18:46:18 +00001812 IN CONST CHAR16 *Name,
1813 IN CONST SHELL_PARAM_ITEM *CheckList,
jcarsey252d9452011-03-25 20:49:53 +00001814 OUT SHELL_PARAM_TYPE *Type
jcarseya405b862010-09-14 05:18:09 +00001815 )
1816{
jcarsey94b17fa2009-05-07 18:46:18 +00001817 SHELL_PARAM_ITEM *TempListItem;
jcarsey252d9452011-03-25 20:49:53 +00001818 CHAR16 *TempString;
jcarsey94b17fa2009-05-07 18:46:18 +00001819
1820 //
1821 // ASSERT that all 3 pointer parameters aren't NULL
1822 //
1823 ASSERT(CheckList != NULL);
1824 ASSERT(Type != NULL);
1825 ASSERT(Name != NULL);
1826
1827 //
jcarseyd2b45642009-05-11 18:02:16 +00001828 // question mark and page break mode are always supported
1829 //
1830 if ((StrCmp(Name, L"-?") == 0) ||
1831 (StrCmp(Name, L"-b") == 0)
jcarseya405b862010-09-14 05:18:09 +00001832 ) {
jcarsey252d9452011-03-25 20:49:53 +00001833 *Type = TypeFlag;
jcarseyd2b45642009-05-11 18:02:16 +00001834 return (TRUE);
1835 }
1836
1837 //
jcarsey94b17fa2009-05-07 18:46:18 +00001838 // Enumerate through the list
1839 //
1840 for (TempListItem = (SHELL_PARAM_ITEM*)CheckList ; TempListItem->Name != NULL ; TempListItem++) {
1841 //
jcarsey9eb53ac2009-07-08 17:26:58 +00001842 // If the Type is TypeStart only check the first characters of the passed in param
1843 // If it matches set the type and return TRUE
jcarsey94b17fa2009-05-07 18:46:18 +00001844 //
darylm503b0934ac2011-11-12 00:35:11 +00001845 if (TempListItem->Type == TypeStart) {
jcarsey252d9452011-03-25 20:49:53 +00001846 if (StrnCmp(Name, TempListItem->Name, StrLen(TempListItem->Name)) == 0) {
1847 *Type = TempListItem->Type;
1848 return (TRUE);
1849 }
1850 TempString = NULL;
1851 TempString = StrnCatGrow(&TempString, NULL, Name, StrLen(TempListItem->Name));
1852 if (TempString != NULL) {
1853 if (StringNoCaseCompare(&TempString, &TempListItem->Name) == 0) {
1854 *Type = TempListItem->Type;
1855 FreePool(TempString);
1856 return (TRUE);
1857 }
1858 FreePool(TempString);
1859 }
1860 } else if (StringNoCaseCompare(&Name, &TempListItem->Name) == 0) {
jcarsey94b17fa2009-05-07 18:46:18 +00001861 *Type = TempListItem->Type;
1862 return (TRUE);
1863 }
1864 }
jcarsey2247dde2009-11-09 18:08:58 +00001865
jcarsey94b17fa2009-05-07 18:46:18 +00001866 return (FALSE);
1867}
1868/**
jcarseyd2b45642009-05-11 18:02:16 +00001869 Checks the string for indicators of "flag" status. this is a leading '/', '-', or '+'
jcarsey94b17fa2009-05-07 18:46:18 +00001870
jcarseya405b862010-09-14 05:18:09 +00001871 @param[in] Name pointer to Name of parameter found
1872 @param[in] AlwaysAllowNumbers TRUE to allow numbers, FALSE to not.
jcarsey94b17fa2009-05-07 18:46:18 +00001873
1874 @retval TRUE the Parameter is a flag.
jcarseya405b862010-09-14 05:18:09 +00001875 @retval FALSE the Parameter not a flag.
jcarsey94b17fa2009-05-07 18:46:18 +00001876**/
1877BOOLEAN
1878EFIAPI
jcarseyd2b45642009-05-11 18:02:16 +00001879InternalIsFlag (
jcarsey2247dde2009-11-09 18:08:58 +00001880 IN CONST CHAR16 *Name,
1881 IN BOOLEAN AlwaysAllowNumbers
jcarsey94b17fa2009-05-07 18:46:18 +00001882 )
1883{
1884 //
1885 // ASSERT that Name isn't NULL
1886 //
1887 ASSERT(Name != NULL);
1888
1889 //
jcarsey2247dde2009-11-09 18:08:58 +00001890 // If we accept numbers then dont return TRUE. (they will be values)
1891 //
jcarseya405b862010-09-14 05:18:09 +00001892 if (((Name[0] == L'-' || Name[0] == L'+') && ShellIsHexaDecimalDigitCharacter(Name[1])) && AlwaysAllowNumbers) {
jcarsey2247dde2009-11-09 18:08:58 +00001893 return (FALSE);
1894 }
1895
1896 //
jcarseya405b862010-09-14 05:18:09 +00001897 // If the Name has a /, +, or - as the first character return TRUE
jcarsey94b17fa2009-05-07 18:46:18 +00001898 //
jcarsey1e6e84c2010-01-25 20:05:08 +00001899 if ((Name[0] == L'/') ||
jcarseyd2b45642009-05-11 18:02:16 +00001900 (Name[0] == L'-') ||
1901 (Name[0] == L'+')
jcarseya405b862010-09-14 05:18:09 +00001902 ) {
jcarsey94b17fa2009-05-07 18:46:18 +00001903 return (TRUE);
1904 }
1905 return (FALSE);
1906}
1907
1908/**
jcarsey1e6e84c2010-01-25 20:05:08 +00001909 Checks the command line arguments passed against the list of valid ones.
jcarsey94b17fa2009-05-07 18:46:18 +00001910
1911 If no initialization is required, then return RETURN_SUCCESS.
jcarsey1e6e84c2010-01-25 20:05:08 +00001912
jcarseya405b862010-09-14 05:18:09 +00001913 @param[in] CheckList pointer to list of parameters to check
1914 @param[out] CheckPackage pointer to pointer to list checked values
1915 @param[out] ProblemParam optional pointer to pointer to unicode string for
jcarseyd2b45642009-05-11 18:02:16 +00001916 the paramater that caused failure. If used then the
1917 caller is responsible for freeing the memory.
jcarseya405b862010-09-14 05:18:09 +00001918 @param[in] AutoPageBreak will automatically set PageBreakEnabled for "b" parameter
1919 @param[in] Argv pointer to array of parameters
1920 @param[in] Argc Count of parameters in Argv
1921 @param[in] AlwaysAllowNumbers TRUE to allow numbers always, FALSE otherwise.
jcarsey94b17fa2009-05-07 18:46:18 +00001922
1923 @retval EFI_SUCCESS The operation completed sucessfully.
1924 @retval EFI_OUT_OF_RESOURCES A memory allocation failed
1925 @retval EFI_INVALID_PARAMETER A parameter was invalid
jcarsey1e6e84c2010-01-25 20:05:08 +00001926 @retval EFI_VOLUME_CORRUPTED the command line was corrupt. an argument was
1927 duplicated. the duplicated command line argument
jcarsey94b17fa2009-05-07 18:46:18 +00001928 was returned in ProblemParam if provided.
jcarsey1e6e84c2010-01-25 20:05:08 +00001929 @retval EFI_NOT_FOUND a argument required a value that was missing.
jcarsey94b17fa2009-05-07 18:46:18 +00001930 the invalid command line argument was returned in
1931 ProblemParam if provided.
1932**/
1933EFI_STATUS
1934EFIAPI
1935InternalCommandLineParse (
1936 IN CONST SHELL_PARAM_ITEM *CheckList,
1937 OUT LIST_ENTRY **CheckPackage,
1938 OUT CHAR16 **ProblemParam OPTIONAL,
1939 IN BOOLEAN AutoPageBreak,
1940 IN CONST CHAR16 **Argv,
jcarsey2247dde2009-11-09 18:08:58 +00001941 IN UINTN Argc,
1942 IN BOOLEAN AlwaysAllowNumbers
jcarseya405b862010-09-14 05:18:09 +00001943 )
1944{
jcarsey94b17fa2009-05-07 18:46:18 +00001945 UINTN LoopCounter;
jcarsey252d9452011-03-25 20:49:53 +00001946 SHELL_PARAM_TYPE CurrentItemType;
jcarsey94b17fa2009-05-07 18:46:18 +00001947 SHELL_PARAM_PACKAGE *CurrentItemPackage;
jcarsey125c2cf2009-11-18 21:36:50 +00001948 UINTN GetItemValue;
1949 UINTN ValueSize;
jcarseya405b862010-09-14 05:18:09 +00001950 UINTN Count;
jcarsey252d9452011-03-25 20:49:53 +00001951 CONST CHAR16 *TempPointer;
jcarsey94b17fa2009-05-07 18:46:18 +00001952
1953 CurrentItemPackage = NULL;
jcarsey125c2cf2009-11-18 21:36:50 +00001954 GetItemValue = 0;
1955 ValueSize = 0;
jcarseya405b862010-09-14 05:18:09 +00001956 Count = 0;
jcarsey94b17fa2009-05-07 18:46:18 +00001957
1958 //
1959 // If there is only 1 item we dont need to do anything
1960 //
jcarseya405b862010-09-14 05:18:09 +00001961 if (Argc < 1) {
jcarsey94b17fa2009-05-07 18:46:18 +00001962 *CheckPackage = NULL;
1963 return (EFI_SUCCESS);
1964 }
1965
1966 //
jcarsey2247dde2009-11-09 18:08:58 +00001967 // ASSERTs
1968 //
1969 ASSERT(CheckList != NULL);
1970 ASSERT(Argv != NULL);
1971
1972 //
jcarsey94b17fa2009-05-07 18:46:18 +00001973 // initialize the linked list
1974 //
1975 *CheckPackage = (LIST_ENTRY*)AllocateZeroPool(sizeof(LIST_ENTRY));
sfu502a758c2011-10-08 02:55:30 +00001976 if (*CheckPackage == NULL) {
jcarseybeab0fc2011-10-10 17:26:25 +00001977 return (EFI_OUT_OF_RESOURCES);
sfu502a758c2011-10-08 02:55:30 +00001978 }
jcarseybeab0fc2011-10-10 17:26:25 +00001979
jcarsey94b17fa2009-05-07 18:46:18 +00001980 InitializeListHead(*CheckPackage);
1981
1982 //
1983 // loop through each of the arguments
1984 //
1985 for (LoopCounter = 0 ; LoopCounter < Argc ; ++LoopCounter) {
1986 if (Argv[LoopCounter] == NULL) {
1987 //
1988 // do nothing for NULL argv
1989 //
jcarseya405b862010-09-14 05:18:09 +00001990 } else if (InternalIsOnCheckList(Argv[LoopCounter], CheckList, &CurrentItemType)) {
jcarsey94b17fa2009-05-07 18:46:18 +00001991 //
jcarsey2247dde2009-11-09 18:08:58 +00001992 // We might have leftover if last parameter didnt have optional value
1993 //
jcarsey125c2cf2009-11-18 21:36:50 +00001994 if (GetItemValue != 0) {
1995 GetItemValue = 0;
jcarsey2247dde2009-11-09 18:08:58 +00001996 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
1997 }
1998 //
jcarsey94b17fa2009-05-07 18:46:18 +00001999 // this is a flag
2000 //
jcarsey252d9452011-03-25 20:49:53 +00002001 CurrentItemPackage = AllocateZeroPool(sizeof(SHELL_PARAM_PACKAGE));
jcarseybeab0fc2011-10-10 17:26:25 +00002002 if (CurrentItemPackage == NULL) {
2003 ShellCommandLineFreeVarList(*CheckPackage);
2004 *CheckPackage = NULL;
2005 return (EFI_OUT_OF_RESOURCES);
2006 }
jcarsey252d9452011-03-25 20:49:53 +00002007 CurrentItemPackage->Name = AllocateZeroPool(StrSize(Argv[LoopCounter]));
jcarseybeab0fc2011-10-10 17:26:25 +00002008 if (CurrentItemPackage->Name == NULL) {
2009 ShellCommandLineFreeVarList(*CheckPackage);
2010 *CheckPackage = NULL;
2011 return (EFI_OUT_OF_RESOURCES);
2012 }
jcarsey94b17fa2009-05-07 18:46:18 +00002013 StrCpy(CurrentItemPackage->Name, Argv[LoopCounter]);
2014 CurrentItemPackage->Type = CurrentItemType;
2015 CurrentItemPackage->OriginalPosition = (UINTN)(-1);
jcarseyb1f95a02009-06-16 00:23:19 +00002016 CurrentItemPackage->Value = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +00002017
2018 //
2019 // Does this flag require a value
2020 //
jcarsey125c2cf2009-11-18 21:36:50 +00002021 switch (CurrentItemPackage->Type) {
jcarsey94b17fa2009-05-07 18:46:18 +00002022 //
jcarsey125c2cf2009-11-18 21:36:50 +00002023 // possibly trigger the next loop(s) to populate the value of this item
jcarsey1e6e84c2010-01-25 20:05:08 +00002024 //
jcarsey125c2cf2009-11-18 21:36:50 +00002025 case TypeValue:
jcarsey1e6e84c2010-01-25 20:05:08 +00002026 GetItemValue = 1;
jcarsey125c2cf2009-11-18 21:36:50 +00002027 ValueSize = 0;
2028 break;
2029 case TypeDoubleValue:
2030 GetItemValue = 2;
2031 ValueSize = 0;
2032 break;
2033 case TypeMaxValue:
2034 GetItemValue = (UINTN)(-1);
2035 ValueSize = 0;
2036 break;
2037 default:
2038 //
2039 // this item has no value expected; we are done
2040 //
2041 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
2042 ASSERT(GetItemValue == 0);
2043 break;
jcarsey94b17fa2009-05-07 18:46:18 +00002044 }
jcarseya405b862010-09-14 05:18:09 +00002045 } else if (GetItemValue != 0 && !InternalIsFlag(Argv[LoopCounter], AlwaysAllowNumbers)) {
jcarseyb1f95a02009-06-16 00:23:19 +00002046 ASSERT(CurrentItemPackage != NULL);
2047 //
jcarsey125c2cf2009-11-18 21:36:50 +00002048 // get the item VALUE for a previous flag
jcarseyb1f95a02009-06-16 00:23:19 +00002049 //
jcarsey49bd4982012-01-27 18:42:43 +00002050 if (StrStr(Argv[LoopCounter], L" ") == NULL) {
2051 CurrentItemPackage->Value = ReallocatePool(ValueSize, ValueSize + StrSize(Argv[LoopCounter]) + sizeof(CHAR16), CurrentItemPackage->Value);
2052 ASSERT(CurrentItemPackage->Value != NULL);
2053 if (ValueSize == 0) {
2054 StrCpy(CurrentItemPackage->Value, Argv[LoopCounter]);
2055 } else {
2056 StrCat(CurrentItemPackage->Value, L" ");
2057 StrCat(CurrentItemPackage->Value, Argv[LoopCounter]);
2058 }
2059 ValueSize += StrSize(Argv[LoopCounter]) + sizeof(CHAR16);
jcarsey125c2cf2009-11-18 21:36:50 +00002060 } else {
jcarsey49bd4982012-01-27 18:42:43 +00002061 //
2062 // the parameter has spaces. must be quoted.
2063 //
2064 CurrentItemPackage->Value = ReallocatePool(ValueSize, ValueSize + StrSize(Argv[LoopCounter]) + sizeof(CHAR16) + sizeof(CHAR16) + sizeof(CHAR16), CurrentItemPackage->Value);
2065 ASSERT(CurrentItemPackage->Value != NULL);
2066 if (ValueSize == 0) {
2067 StrCpy(CurrentItemPackage->Value, L"\"");
2068 StrCat(CurrentItemPackage->Value, Argv[LoopCounter]);
2069 StrCat(CurrentItemPackage->Value, L"\"");
2070 } else {
2071 StrCat(CurrentItemPackage->Value, L" ");
2072 StrCat(CurrentItemPackage->Value, L"\"");
2073 StrCat(CurrentItemPackage->Value, Argv[LoopCounter]);
2074 StrCat(CurrentItemPackage->Value, L"\"");
2075 }
2076 ValueSize += StrSize(Argv[LoopCounter]) + sizeof(CHAR16);
jcarsey125c2cf2009-11-18 21:36:50 +00002077 }
jcarsey125c2cf2009-11-18 21:36:50 +00002078 GetItemValue--;
2079 if (GetItemValue == 0) {
2080 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
2081 }
jcarseya405b862010-09-14 05:18:09 +00002082 } else if (!InternalIsFlag(Argv[LoopCounter], AlwaysAllowNumbers) ){ //|| ProblemParam == NULL) {
jcarseyb1f95a02009-06-16 00:23:19 +00002083 //
2084 // add this one as a non-flag
2085 //
jcarsey252d9452011-03-25 20:49:53 +00002086
2087 TempPointer = Argv[LoopCounter];
darylm503b0934ac2011-11-12 00:35:11 +00002088 if ((*TempPointer == L'^' && *(TempPointer+1) == L'-')
jcarsey252d9452011-03-25 20:49:53 +00002089 || (*TempPointer == L'^' && *(TempPointer+1) == L'/')
2090 || (*TempPointer == L'^' && *(TempPointer+1) == L'+')
2091 ){
2092 TempPointer++;
2093 }
2094 CurrentItemPackage = AllocateZeroPool(sizeof(SHELL_PARAM_PACKAGE));
jcarseybeab0fc2011-10-10 17:26:25 +00002095 if (CurrentItemPackage == NULL) {
2096 ShellCommandLineFreeVarList(*CheckPackage);
2097 *CheckPackage = NULL;
2098 return (EFI_OUT_OF_RESOURCES);
2099 }
jcarseyb1f95a02009-06-16 00:23:19 +00002100 CurrentItemPackage->Name = NULL;
2101 CurrentItemPackage->Type = TypePosition;
jcarsey252d9452011-03-25 20:49:53 +00002102 CurrentItemPackage->Value = AllocateZeroPool(StrSize(TempPointer));
jcarseybeab0fc2011-10-10 17:26:25 +00002103 if (CurrentItemPackage->Value == NULL) {
2104 ShellCommandLineFreeVarList(*CheckPackage);
2105 *CheckPackage = NULL;
2106 return (EFI_OUT_OF_RESOURCES);
2107 }
jcarsey252d9452011-03-25 20:49:53 +00002108 StrCpy(CurrentItemPackage->Value, TempPointer);
jcarseya405b862010-09-14 05:18:09 +00002109 CurrentItemPackage->OriginalPosition = Count++;
jcarsey9b3bf082009-06-23 21:15:07 +00002110 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
jcarsey252d9452011-03-25 20:49:53 +00002111 } else {
jcarsey94b17fa2009-05-07 18:46:18 +00002112 //
2113 // this was a non-recognised flag... error!
2114 //
jcarsey252d9452011-03-25 20:49:53 +00002115 if (ProblemParam != NULL) {
2116 *ProblemParam = AllocateZeroPool(StrSize(Argv[LoopCounter]));
jcarseybeab0fc2011-10-10 17:26:25 +00002117 if (*ProblemParam != NULL) {
darylm503b0934ac2011-11-12 00:35:11 +00002118 StrCpy(*ProblemParam, Argv[LoopCounter]);
jcarseybeab0fc2011-10-10 17:26:25 +00002119 }
jcarsey252d9452011-03-25 20:49:53 +00002120 }
jcarsey94b17fa2009-05-07 18:46:18 +00002121 ShellCommandLineFreeVarList(*CheckPackage);
2122 *CheckPackage = NULL;
2123 return (EFI_VOLUME_CORRUPTED);
jcarsey94b17fa2009-05-07 18:46:18 +00002124 }
2125 }
jcarsey125c2cf2009-11-18 21:36:50 +00002126 if (GetItemValue != 0) {
2127 GetItemValue = 0;
2128 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
2129 }
jcarsey94b17fa2009-05-07 18:46:18 +00002130 //
2131 // support for AutoPageBreak
2132 //
2133 if (AutoPageBreak && ShellCommandLineGetFlag(*CheckPackage, L"-b")) {
2134 ShellSetPageBreakMode(TRUE);
2135 }
2136 return (EFI_SUCCESS);
2137}
2138
2139/**
jcarsey1e6e84c2010-01-25 20:05:08 +00002140 Checks the command line arguments passed against the list of valid ones.
jcarsey94b17fa2009-05-07 18:46:18 +00002141 Optionally removes NULL values first.
jcarsey1e6e84c2010-01-25 20:05:08 +00002142
jcarsey94b17fa2009-05-07 18:46:18 +00002143 If no initialization is required, then return RETURN_SUCCESS.
jcarsey1e6e84c2010-01-25 20:05:08 +00002144
jcarseya405b862010-09-14 05:18:09 +00002145 @param[in] CheckList The pointer to list of parameters to check.
2146 @param[out] CheckPackage The package of checked values.
2147 @param[out] ProblemParam Optional pointer to pointer to unicode string for
jcarsey94b17fa2009-05-07 18:46:18 +00002148 the paramater that caused failure.
jcarseya405b862010-09-14 05:18:09 +00002149 @param[in] AutoPageBreak Will automatically set PageBreakEnabled.
2150 @param[in] AlwaysAllowNumbers Will never fail for number based flags.
jcarsey94b17fa2009-05-07 18:46:18 +00002151
2152 @retval EFI_SUCCESS The operation completed sucessfully.
jcarseya405b862010-09-14 05:18:09 +00002153 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
2154 @retval EFI_INVALID_PARAMETER A parameter was invalid.
2155 @retval EFI_VOLUME_CORRUPTED The command line was corrupt.
2156 @retval EFI_DEVICE_ERROR The commands contained 2 opposing arguments. One
jcarsey1e6e84c2010-01-25 20:05:08 +00002157 of the command line arguments was returned in
jcarsey94b17fa2009-05-07 18:46:18 +00002158 ProblemParam if provided.
jcarseya405b862010-09-14 05:18:09 +00002159 @retval EFI_NOT_FOUND A argument required a value that was missing.
2160 The invalid command line argument was returned in
jcarsey94b17fa2009-05-07 18:46:18 +00002161 ProblemParam if provided.
2162**/
2163EFI_STATUS
2164EFIAPI
jcarsey2247dde2009-11-09 18:08:58 +00002165ShellCommandLineParseEx (
jcarsey94b17fa2009-05-07 18:46:18 +00002166 IN CONST SHELL_PARAM_ITEM *CheckList,
2167 OUT LIST_ENTRY **CheckPackage,
2168 OUT CHAR16 **ProblemParam OPTIONAL,
jcarsey2247dde2009-11-09 18:08:58 +00002169 IN BOOLEAN AutoPageBreak,
2170 IN BOOLEAN AlwaysAllowNumbers
jcarseya405b862010-09-14 05:18:09 +00002171 )
2172{
jcarsey1e6e84c2010-01-25 20:05:08 +00002173 //
jcarsey94b17fa2009-05-07 18:46:18 +00002174 // ASSERT that CheckList and CheckPackage aren't NULL
2175 //
2176 ASSERT(CheckList != NULL);
2177 ASSERT(CheckPackage != NULL);
2178
jcarsey1e6e84c2010-01-25 20:05:08 +00002179 //
jcarsey94b17fa2009-05-07 18:46:18 +00002180 // Check for UEFI Shell 2.0 protocols
2181 //
jcarsey366f81a2011-06-27 21:04:22 +00002182 if (gEfiShellParametersProtocol != NULL) {
jcarsey1e6e84c2010-01-25 20:05:08 +00002183 return (InternalCommandLineParse(CheckList,
2184 CheckPackage,
2185 ProblemParam,
2186 AutoPageBreak,
jcarsey366f81a2011-06-27 21:04:22 +00002187 (CONST CHAR16**) gEfiShellParametersProtocol->Argv,
2188 gEfiShellParametersProtocol->Argc,
jcarsey2247dde2009-11-09 18:08:58 +00002189 AlwaysAllowNumbers));
jcarsey94b17fa2009-05-07 18:46:18 +00002190 }
2191
jcarsey1e6e84c2010-01-25 20:05:08 +00002192 //
jcarsey94b17fa2009-05-07 18:46:18 +00002193 // ASSERT That EFI Shell is not required
2194 //
2195 ASSERT (mEfiShellInterface != NULL);
jcarsey1e6e84c2010-01-25 20:05:08 +00002196 return (InternalCommandLineParse(CheckList,
2197 CheckPackage,
2198 ProblemParam,
2199 AutoPageBreak,
jljusten08d7f8e2009-06-15 18:42:13 +00002200 (CONST CHAR16**) mEfiShellInterface->Argv,
jcarsey2247dde2009-11-09 18:08:58 +00002201 mEfiShellInterface->Argc,
2202 AlwaysAllowNumbers));
jcarsey94b17fa2009-05-07 18:46:18 +00002203}
2204
2205/**
2206 Frees shell variable list that was returned from ShellCommandLineParse.
2207
2208 This function will free all the memory that was used for the CheckPackage
2209 list of postprocessed shell arguments.
2210
2211 this function has no return value.
2212
2213 if CheckPackage is NULL, then return
2214
2215 @param CheckPackage the list to de-allocate
2216 **/
2217VOID
2218EFIAPI
2219ShellCommandLineFreeVarList (
2220 IN LIST_ENTRY *CheckPackage
jcarseya405b862010-09-14 05:18:09 +00002221 )
2222{
jcarsey94b17fa2009-05-07 18:46:18 +00002223 LIST_ENTRY *Node;
2224
2225 //
2226 // check for CheckPackage == NULL
2227 //
2228 if (CheckPackage == NULL) {
2229 return;
2230 }
2231
2232 //
2233 // for each node in the list
2234 //
jcarsey9eb53ac2009-07-08 17:26:58 +00002235 for ( Node = GetFirstNode(CheckPackage)
jcarseya405b862010-09-14 05:18:09 +00002236 ; !IsListEmpty(CheckPackage)
jcarsey9eb53ac2009-07-08 17:26:58 +00002237 ; Node = GetFirstNode(CheckPackage)
jcarseya405b862010-09-14 05:18:09 +00002238 ){
jcarsey94b17fa2009-05-07 18:46:18 +00002239 //
2240 // Remove it from the list
2241 //
2242 RemoveEntryList(Node);
2243
2244 //
2245 // if it has a name free the name
2246 //
2247 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {
2248 FreePool(((SHELL_PARAM_PACKAGE*)Node)->Name);
2249 }
2250
2251 //
2252 // if it has a value free the value
2253 //
2254 if (((SHELL_PARAM_PACKAGE*)Node)->Value != NULL) {
2255 FreePool(((SHELL_PARAM_PACKAGE*)Node)->Value);
2256 }
jcarsey1e6e84c2010-01-25 20:05:08 +00002257
jcarsey94b17fa2009-05-07 18:46:18 +00002258 //
2259 // free the node structure
2260 //
2261 FreePool((SHELL_PARAM_PACKAGE*)Node);
2262 }
2263 //
2264 // free the list head node
2265 //
2266 FreePool(CheckPackage);
2267}
2268/**
2269 Checks for presence of a flag parameter
2270
2271 flag arguments are in the form of "-<Key>" or "/<Key>", but do not have a value following the key
2272
2273 if CheckPackage is NULL then return FALSE.
2274 if KeyString is NULL then ASSERT()
jcarsey1e6e84c2010-01-25 20:05:08 +00002275
jcarsey94b17fa2009-05-07 18:46:18 +00002276 @param CheckPackage The package of parsed command line arguments
2277 @param KeyString the Key of the command line argument to check for
2278
2279 @retval TRUE the flag is on the command line
2280 @retval FALSE the flag is not on the command line
2281 **/
2282BOOLEAN
2283EFIAPI
2284ShellCommandLineGetFlag (
jcarseya405b862010-09-14 05:18:09 +00002285 IN CONST LIST_ENTRY * CONST CheckPackage,
2286 IN CONST CHAR16 * CONST KeyString
2287 )
2288{
jcarsey94b17fa2009-05-07 18:46:18 +00002289 LIST_ENTRY *Node;
jcarsey252d9452011-03-25 20:49:53 +00002290 CHAR16 *TempString;
jcarsey94b17fa2009-05-07 18:46:18 +00002291
2292 //
ydong100c1950b2011-10-13 02:37:35 +00002293 // return FALSE for no package or KeyString is NULL
jcarsey94b17fa2009-05-07 18:46:18 +00002294 //
ydong100c1950b2011-10-13 02:37:35 +00002295 if (CheckPackage == NULL || KeyString == NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00002296 return (FALSE);
2297 }
2298
2299 //
2300 // enumerate through the list of parametrs
2301 //
jcarsey1e6e84c2010-01-25 20:05:08 +00002302 for ( Node = GetFirstNode(CheckPackage)
2303 ; !IsNull (CheckPackage, Node)
2304 ; Node = GetNextNode(CheckPackage, Node)
jcarsey252d9452011-03-25 20:49:53 +00002305 ){
jcarsey94b17fa2009-05-07 18:46:18 +00002306 //
2307 // If the Name matches, return TRUE (and there may be NULL name)
2308 //
2309 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {
jcarsey9eb53ac2009-07-08 17:26:58 +00002310 //
2311 // If Type is TypeStart then only compare the begining of the strings
2312 //
jcarsey252d9452011-03-25 20:49:53 +00002313 if (((SHELL_PARAM_PACKAGE*)Node)->Type == TypeStart) {
2314 if (StrnCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name, StrLen(KeyString)) == 0) {
2315 return (TRUE);
2316 }
2317 TempString = NULL;
2318 TempString = StrnCatGrow(&TempString, NULL, KeyString, StrLen(((SHELL_PARAM_PACKAGE*)Node)->Name));
2319 if (TempString != NULL) {
2320 if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {
2321 FreePool(TempString);
2322 return (TRUE);
2323 }
2324 FreePool(TempString);
2325 }
2326 } else if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {
jcarsey94b17fa2009-05-07 18:46:18 +00002327 return (TRUE);
2328 }
2329 }
2330 }
2331 return (FALSE);
2332}
2333/**
jcarseya405b862010-09-14 05:18:09 +00002334 Returns value from command line argument.
jcarsey94b17fa2009-05-07 18:46:18 +00002335
jcarseya405b862010-09-14 05:18:09 +00002336 Value parameters are in the form of "-<Key> value" or "/<Key> value".
jcarsey1e6e84c2010-01-25 20:05:08 +00002337
jcarseya405b862010-09-14 05:18:09 +00002338 If CheckPackage is NULL, then return NULL.
jcarsey94b17fa2009-05-07 18:46:18 +00002339
jcarseya405b862010-09-14 05:18:09 +00002340 @param[in] CheckPackage The package of parsed command line arguments.
2341 @param[in] KeyString The Key of the command line argument to check for.
jcarsey94b17fa2009-05-07 18:46:18 +00002342
jcarseya405b862010-09-14 05:18:09 +00002343 @retval NULL The flag is not on the command line.
2344 @retval !=NULL The pointer to unicode string of the value.
2345**/
jcarsey94b17fa2009-05-07 18:46:18 +00002346CONST CHAR16*
2347EFIAPI
2348ShellCommandLineGetValue (
2349 IN CONST LIST_ENTRY *CheckPackage,
2350 IN CHAR16 *KeyString
jcarseya405b862010-09-14 05:18:09 +00002351 )
2352{
jcarsey94b17fa2009-05-07 18:46:18 +00002353 LIST_ENTRY *Node;
jcarsey252d9452011-03-25 20:49:53 +00002354 CHAR16 *TempString;
jcarsey94b17fa2009-05-07 18:46:18 +00002355
2356 //
ydong100c1950b2011-10-13 02:37:35 +00002357 // return NULL for no package or KeyString is NULL
jcarsey94b17fa2009-05-07 18:46:18 +00002358 //
ydong100c1950b2011-10-13 02:37:35 +00002359 if (CheckPackage == NULL || KeyString == NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00002360 return (NULL);
2361 }
2362
2363 //
2364 // enumerate through the list of parametrs
2365 //
jcarsey1e6e84c2010-01-25 20:05:08 +00002366 for ( Node = GetFirstNode(CheckPackage)
2367 ; !IsNull (CheckPackage, Node)
2368 ; Node = GetNextNode(CheckPackage, Node)
jcarsey252d9452011-03-25 20:49:53 +00002369 ){
jcarsey94b17fa2009-05-07 18:46:18 +00002370 //
jcarsey252d9452011-03-25 20:49:53 +00002371 // If the Name matches, return TRUE (and there may be NULL name)
jcarsey94b17fa2009-05-07 18:46:18 +00002372 //
2373 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {
jcarsey9eb53ac2009-07-08 17:26:58 +00002374 //
2375 // If Type is TypeStart then only compare the begining of the strings
2376 //
jcarsey252d9452011-03-25 20:49:53 +00002377 if (((SHELL_PARAM_PACKAGE*)Node)->Type == TypeStart) {
2378 if (StrnCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name, StrLen(KeyString)) == 0) {
2379 return (((SHELL_PARAM_PACKAGE*)Node)->Name + StrLen(KeyString));
2380 }
2381 TempString = NULL;
2382 TempString = StrnCatGrow(&TempString, NULL, KeyString, StrLen(((SHELL_PARAM_PACKAGE*)Node)->Name));
2383 if (TempString != NULL) {
2384 if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {
2385 FreePool(TempString);
2386 return (((SHELL_PARAM_PACKAGE*)Node)->Name + StrLen(KeyString));
2387 }
2388 FreePool(TempString);
2389 }
2390 } else if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {
jcarsey94b17fa2009-05-07 18:46:18 +00002391 return (((SHELL_PARAM_PACKAGE*)Node)->Value);
2392 }
2393 }
2394 }
2395 return (NULL);
2396}
jcarseya405b862010-09-14 05:18:09 +00002397
jcarsey94b17fa2009-05-07 18:46:18 +00002398/**
jcarseya405b862010-09-14 05:18:09 +00002399 Returns raw value from command line argument.
jcarsey94b17fa2009-05-07 18:46:18 +00002400
jcarseya405b862010-09-14 05:18:09 +00002401 Raw value parameters are in the form of "value" in a specific position in the list.
jcarsey1e6e84c2010-01-25 20:05:08 +00002402
jcarseya405b862010-09-14 05:18:09 +00002403 If CheckPackage is NULL, then return NULL.
jcarsey94b17fa2009-05-07 18:46:18 +00002404
jcarseya405b862010-09-14 05:18:09 +00002405 @param[in] CheckPackage The package of parsed command line arguments.
2406 @param[in] Position The position of the value.
jcarsey94b17fa2009-05-07 18:46:18 +00002407
jcarseya405b862010-09-14 05:18:09 +00002408 @retval NULL The flag is not on the command line.
2409 @retval !=NULL The pointer to unicode string of the value.
jcarsey94b17fa2009-05-07 18:46:18 +00002410 **/
2411CONST CHAR16*
2412EFIAPI
2413ShellCommandLineGetRawValue (
jcarseya405b862010-09-14 05:18:09 +00002414 IN CONST LIST_ENTRY * CONST CheckPackage,
2415 IN UINTN Position
2416 )
2417{
jcarsey94b17fa2009-05-07 18:46:18 +00002418 LIST_ENTRY *Node;
2419
2420 //
2421 // check for CheckPackage == NULL
2422 //
2423 if (CheckPackage == NULL) {
2424 return (NULL);
2425 }
2426
2427 //
2428 // enumerate through the list of parametrs
2429 //
jcarsey1e6e84c2010-01-25 20:05:08 +00002430 for ( Node = GetFirstNode(CheckPackage)
2431 ; !IsNull (CheckPackage, Node)
2432 ; Node = GetNextNode(CheckPackage, Node)
jcarseya405b862010-09-14 05:18:09 +00002433 ){
jcarsey94b17fa2009-05-07 18:46:18 +00002434 //
2435 // If the position matches, return the value
2436 //
2437 if (((SHELL_PARAM_PACKAGE*)Node)->OriginalPosition == Position) {
2438 return (((SHELL_PARAM_PACKAGE*)Node)->Value);
2439 }
2440 }
2441 return (NULL);
jcarseyb1f95a02009-06-16 00:23:19 +00002442}
jcarsey2247dde2009-11-09 18:08:58 +00002443
2444/**
jcarsey1e6e84c2010-01-25 20:05:08 +00002445 returns the number of command line value parameters that were parsed.
2446
jcarsey2247dde2009-11-09 18:08:58 +00002447 this will not include flags.
2448
jcarseya405b862010-09-14 05:18:09 +00002449 @param[in] CheckPackage The package of parsed command line arguments.
2450
jcarsey2247dde2009-11-09 18:08:58 +00002451 @retval (UINTN)-1 No parsing has ocurred
2452 @return other The number of value parameters found
2453**/
2454UINTN
2455EFIAPI
2456ShellCommandLineGetCount(
jcarseya405b862010-09-14 05:18:09 +00002457 IN CONST LIST_ENTRY *CheckPackage
jcarsey125c2cf2009-11-18 21:36:50 +00002458 )
2459{
jcarseya405b862010-09-14 05:18:09 +00002460 LIST_ENTRY *Node1;
2461 UINTN Count;
2462
2463 if (CheckPackage == NULL) {
2464 return (0);
2465 }
2466 for ( Node1 = GetFirstNode(CheckPackage), Count = 0
2467 ; !IsNull (CheckPackage, Node1)
2468 ; Node1 = GetNextNode(CheckPackage, Node1)
2469 ){
2470 if (((SHELL_PARAM_PACKAGE*)Node1)->Name == NULL) {
2471 Count++;
2472 }
2473 }
2474 return (Count);
jcarsey2247dde2009-11-09 18:08:58 +00002475}
2476
jcarsey975136a2009-06-16 19:03:54 +00002477/**
jcarsey36a9d672009-11-20 21:13:41 +00002478 Determins if a parameter is duplicated.
2479
jcarsey1e6e84c2010-01-25 20:05:08 +00002480 If Param is not NULL then it will point to a callee allocated string buffer
jcarsey36a9d672009-11-20 21:13:41 +00002481 with the parameter value if a duplicate is found.
2482
2483 If CheckPackage is NULL, then ASSERT.
2484
2485 @param[in] CheckPackage The package of parsed command line arguments.
2486 @param[out] Param Upon finding one, a pointer to the duplicated parameter.
2487
2488 @retval EFI_SUCCESS No parameters were duplicated.
2489 @retval EFI_DEVICE_ERROR A duplicate was found.
2490 **/
2491EFI_STATUS
2492EFIAPI
2493ShellCommandLineCheckDuplicate (
2494 IN CONST LIST_ENTRY *CheckPackage,
2495 OUT CHAR16 **Param
2496 )
2497{
2498 LIST_ENTRY *Node1;
2499 LIST_ENTRY *Node2;
jcarsey1e6e84c2010-01-25 20:05:08 +00002500
jcarsey36a9d672009-11-20 21:13:41 +00002501 ASSERT(CheckPackage != NULL);
2502
jcarsey1e6e84c2010-01-25 20:05:08 +00002503 for ( Node1 = GetFirstNode(CheckPackage)
2504 ; !IsNull (CheckPackage, Node1)
2505 ; Node1 = GetNextNode(CheckPackage, Node1)
jcarseya405b862010-09-14 05:18:09 +00002506 ){
jcarsey1e6e84c2010-01-25 20:05:08 +00002507 for ( Node2 = GetNextNode(CheckPackage, Node1)
2508 ; !IsNull (CheckPackage, Node2)
2509 ; Node2 = GetNextNode(CheckPackage, Node2)
jcarseya405b862010-09-14 05:18:09 +00002510 ){
2511 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 +00002512 if (Param != NULL) {
2513 *Param = NULL;
2514 *Param = StrnCatGrow(Param, NULL, ((SHELL_PARAM_PACKAGE*)Node1)->Name, 0);
2515 }
2516 return (EFI_DEVICE_ERROR);
2517 }
2518 }
2519 }
2520 return (EFI_SUCCESS);
2521}
2522
2523/**
jcarsey1e6e84c2010-01-25 20:05:08 +00002524 This is a find and replace function. Upon successful return the NewString is a copy of
jcarsey975136a2009-06-16 19:03:54 +00002525 SourceString with each instance of FindTarget replaced with ReplaceWith.
2526
jcarseyb3011f42010-01-11 21:49:04 +00002527 If SourceString and NewString overlap the behavior is undefined.
2528
jcarsey975136a2009-06-16 19:03:54 +00002529 If the string would grow bigger than NewSize it will halt and return error.
2530
ydong104ff7e372011-09-02 08:05:34 +00002531 @param[in] SourceString The string with source buffer.
2532 @param[in, out] NewString The string with resultant buffer.
2533 @param[in] NewSize The size in bytes of NewString.
2534 @param[in] FindTarget The string to look for.
2535 @param[in] ReplaceWith The string to replace FindTarget with.
2536 @param[in] SkipPreCarrot If TRUE will skip a FindTarget that has a '^'
2537 immediately before it.
2538 @param[in] ParameterReplacing If TRUE will add "" around items with spaces.
jcarsey975136a2009-06-16 19:03:54 +00002539
jcarsey969c7832010-01-13 16:46:33 +00002540 @retval EFI_INVALID_PARAMETER SourceString was NULL.
2541 @retval EFI_INVALID_PARAMETER NewString was NULL.
2542 @retval EFI_INVALID_PARAMETER FindTarget was NULL.
2543 @retval EFI_INVALID_PARAMETER ReplaceWith was NULL.
2544 @retval EFI_INVALID_PARAMETER FindTarget had length < 1.
2545 @retval EFI_INVALID_PARAMETER SourceString had length < 1.
jcarsey1e6e84c2010-01-25 20:05:08 +00002546 @retval EFI_BUFFER_TOO_SMALL NewSize was less than the minimum size to hold
jcarsey969c7832010-01-13 16:46:33 +00002547 the new string (truncation occurred).
jcarseya405b862010-09-14 05:18:09 +00002548 @retval EFI_SUCCESS The string was successfully copied with replacement.
jcarsey975136a2009-06-16 19:03:54 +00002549**/
jcarsey975136a2009-06-16 19:03:54 +00002550EFI_STATUS
2551EFIAPI
jcarseya405b862010-09-14 05:18:09 +00002552ShellCopySearchAndReplace(
jcarsey975136a2009-06-16 19:03:54 +00002553 IN CHAR16 CONST *SourceString,
jcarseya405b862010-09-14 05:18:09 +00002554 IN OUT CHAR16 *NewString,
jcarsey975136a2009-06-16 19:03:54 +00002555 IN UINTN NewSize,
2556 IN CONST CHAR16 *FindTarget,
jcarsey969c7832010-01-13 16:46:33 +00002557 IN CONST CHAR16 *ReplaceWith,
jcarseya405b862010-09-14 05:18:09 +00002558 IN CONST BOOLEAN SkipPreCarrot,
2559 IN CONST BOOLEAN ParameterReplacing
jcarsey1e6e84c2010-01-25 20:05:08 +00002560 )
jcarsey2247dde2009-11-09 18:08:58 +00002561{
jcarsey01582942009-07-10 19:46:17 +00002562 UINTN Size;
jcarseya405b862010-09-14 05:18:09 +00002563 CHAR16 *Replace;
2564
jcarsey975136a2009-06-16 19:03:54 +00002565 if ( (SourceString == NULL)
2566 || (NewString == NULL)
2567 || (FindTarget == NULL)
2568 || (ReplaceWith == NULL)
2569 || (StrLen(FindTarget) < 1)
2570 || (StrLen(SourceString) < 1)
jcarseya405b862010-09-14 05:18:09 +00002571 ){
jcarsey975136a2009-06-16 19:03:54 +00002572 return (EFI_INVALID_PARAMETER);
2573 }
jcarseya405b862010-09-14 05:18:09 +00002574 Replace = NULL;
2575 if (StrStr(ReplaceWith, L" ") == NULL || !ParameterReplacing) {
2576 Replace = StrnCatGrow(&Replace, NULL, ReplaceWith, 0);
2577 } else {
2578 Replace = AllocateZeroPool(StrSize(ReplaceWith) + 2*sizeof(CHAR16));
jcarseybeab0fc2011-10-10 17:26:25 +00002579 if (Replace != NULL) {
2580 UnicodeSPrint(Replace, StrSize(ReplaceWith) + 2*sizeof(CHAR16), L"\"%s\"", ReplaceWith);
2581 }
jcarseya405b862010-09-14 05:18:09 +00002582 }
jcarsey3e082d52010-10-04 16:44:57 +00002583 if (Replace == NULL) {
2584 return (EFI_OUT_OF_RESOURCES);
2585 }
jcarsey2247dde2009-11-09 18:08:58 +00002586 NewString = SetMem16(NewString, NewSize, CHAR_NULL);
2587 while (*SourceString != CHAR_NULL) {
jcarsey969c7832010-01-13 16:46:33 +00002588 //
jcarseya405b862010-09-14 05:18:09 +00002589 // if we find the FindTarget and either Skip == FALSE or Skip and we
jcarsey969c7832010-01-13 16:46:33 +00002590 // dont have a carrot do a replace...
2591 //
jcarsey1e6e84c2010-01-25 20:05:08 +00002592 if (StrnCmp(SourceString, FindTarget, StrLen(FindTarget)) == 0
jcarseya405b862010-09-14 05:18:09 +00002593 && ((SkipPreCarrot && *(SourceString-1) != L'^') || !SkipPreCarrot)
2594 ){
jcarsey975136a2009-06-16 19:03:54 +00002595 SourceString += StrLen(FindTarget);
jcarsey01582942009-07-10 19:46:17 +00002596 Size = StrSize(NewString);
jcarseya405b862010-09-14 05:18:09 +00002597 if ((Size + (StrLen(Replace)*sizeof(CHAR16))) > NewSize) {
2598 FreePool(Replace);
jcarsey975136a2009-06-16 19:03:54 +00002599 return (EFI_BUFFER_TOO_SMALL);
2600 }
jcarseya405b862010-09-14 05:18:09 +00002601 StrCat(NewString, Replace);
jcarsey975136a2009-06-16 19:03:54 +00002602 } else {
jcarsey01582942009-07-10 19:46:17 +00002603 Size = StrSize(NewString);
2604 if (Size + sizeof(CHAR16) > NewSize) {
jcarseya405b862010-09-14 05:18:09 +00002605 FreePool(Replace);
jcarsey975136a2009-06-16 19:03:54 +00002606 return (EFI_BUFFER_TOO_SMALL);
2607 }
2608 StrnCat(NewString, SourceString, 1);
2609 SourceString++;
2610 }
2611 }
jcarseya405b862010-09-14 05:18:09 +00002612 FreePool(Replace);
jcarsey975136a2009-06-16 19:03:54 +00002613 return (EFI_SUCCESS);
2614}
jcarseyb1f95a02009-06-16 00:23:19 +00002615
2616/**
jcarseye2f82972009-12-01 05:40:24 +00002617 Internal worker function to output a string.
2618
2619 This function will output a string to the correct StdOut.
2620
2621 @param[in] String The string to print out.
2622
2623 @retval EFI_SUCCESS The operation was sucessful.
2624 @retval !EFI_SUCCESS The operation failed.
2625**/
2626EFI_STATUS
2627EFIAPI
2628InternalPrintTo (
2629 IN CONST CHAR16 *String
2630 )
2631{
2632 UINTN Size;
2633 Size = StrSize(String) - sizeof(CHAR16);
jcarseya405b862010-09-14 05:18:09 +00002634 if (Size == 0) {
2635 return (EFI_SUCCESS);
2636 }
jcarsey366f81a2011-06-27 21:04:22 +00002637 if (gEfiShellParametersProtocol != NULL) {
2638 return (gEfiShellProtocol->WriteFile(gEfiShellParametersProtocol->StdOut, &Size, (VOID*)String));
jcarseye2f82972009-12-01 05:40:24 +00002639 }
2640 if (mEfiShellInterface != NULL) {
jcarsey06c355b2012-03-26 21:00:39 +00002641 if (mEfiShellInterface->RedirArgc == 0) {
jcarsey49bd4982012-01-27 18:42:43 +00002642 //
2643 // Divide in half for old shell. Must be string length not size.
jcarsey06c355b2012-03-26 21:00:39 +00002644 //
2645 Size /=2; // Divide in half only when no redirection.
2646 }
jcarseya405b862010-09-14 05:18:09 +00002647 return (mEfiShellInterface->StdOut->Write(mEfiShellInterface->StdOut, &Size, (VOID*)String));
jcarseye2f82972009-12-01 05:40:24 +00002648 }
2649 ASSERT(FALSE);
2650 return (EFI_UNSUPPORTED);
2651}
2652
2653/**
jcarseyb1f95a02009-06-16 00:23:19 +00002654 Print at a specific location on the screen.
2655
jcarseyf1b87e72009-06-17 00:52:11 +00002656 This function will move the cursor to a given screen location and print the specified string
jcarsey1e6e84c2010-01-25 20:05:08 +00002657
2658 If -1 is specified for either the Row or Col the current screen location for BOTH
jcarseyf1b87e72009-06-17 00:52:11 +00002659 will be used.
jcarseyb1f95a02009-06-16 00:23:19 +00002660
2661 if either Row or Col is out of range for the current console, then ASSERT
2662 if Format is NULL, then ASSERT
2663
jcarsey1e6e84c2010-01-25 20:05:08 +00002664 In addition to the standard %-based flags as supported by UefiLib Print() this supports
jcarseyb1f95a02009-06-16 00:23:19 +00002665 the following additional flags:
2666 %N - Set output attribute to normal
2667 %H - Set output attribute to highlight
2668 %E - Set output attribute to error
2669 %B - Set output attribute to blue color
2670 %V - Set output attribute to green color
2671
2672 Note: The background color is controlled by the shell command cls.
2673
jcarseyb1f95a02009-06-16 00:23:19 +00002674 @param[in] Col the column to print at
jcarsey252d9452011-03-25 20:49:53 +00002675 @param[in] Row the row to print at
jcarseyb1f95a02009-06-16 00:23:19 +00002676 @param[in] Format the format string
jcarsey2247dde2009-11-09 18:08:58 +00002677 @param[in] Marker the marker for the variable argument list
jcarseyb1f95a02009-06-16 00:23:19 +00002678
jcarseya405b862010-09-14 05:18:09 +00002679 @return EFI_SUCCESS The operation was successful.
2680 @return EFI_DEVICE_ERROR The console device reported an error.
jcarseyb1f95a02009-06-16 00:23:19 +00002681**/
jcarseya405b862010-09-14 05:18:09 +00002682EFI_STATUS
jcarseyb1f95a02009-06-16 00:23:19 +00002683EFIAPI
jcarsey2247dde2009-11-09 18:08:58 +00002684InternalShellPrintWorker(
jcarseyb1f95a02009-06-16 00:23:19 +00002685 IN INT32 Col OPTIONAL,
2686 IN INT32 Row OPTIONAL,
2687 IN CONST CHAR16 *Format,
jcarsey252d9452011-03-25 20:49:53 +00002688 IN VA_LIST Marker
jcarsey1e6e84c2010-01-25 20:05:08 +00002689 )
jcarsey2247dde2009-11-09 18:08:58 +00002690{
jcarseyb1f95a02009-06-16 00:23:19 +00002691 EFI_STATUS Status;
jcarsey975136a2009-06-16 19:03:54 +00002692 CHAR16 *ResumeLocation;
2693 CHAR16 *FormatWalker;
jcarseya405b862010-09-14 05:18:09 +00002694 UINTN OriginalAttribute;
jcarsey89e85372011-04-13 23:37:50 +00002695 CHAR16 *mPostReplaceFormat;
2696 CHAR16 *mPostReplaceFormat2;
2697
2698 mPostReplaceFormat = AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize));
2699 mPostReplaceFormat2 = AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize));
jcarseya405b862010-09-14 05:18:09 +00002700
jcarseyf8d3e682011-04-19 17:54:42 +00002701 if (mPostReplaceFormat == NULL || mPostReplaceFormat2 == NULL) {
2702 SHELL_FREE_NON_NULL(mPostReplaceFormat);
2703 SHELL_FREE_NON_NULL(mPostReplaceFormat2);
2704 return (EFI_OUT_OF_RESOURCES);
2705 }
2706
jcarseya405b862010-09-14 05:18:09 +00002707 Status = EFI_SUCCESS;
2708 OriginalAttribute = gST->ConOut->Mode->Attribute;
jcarsey1e6e84c2010-01-25 20:05:08 +00002709
jcarsey975136a2009-06-16 19:03:54 +00002710 //
2711 // Back and forth each time fixing up 1 of our flags...
2712 //
jcarseya405b862010-09-14 05:18:09 +00002713 Status = ShellCopySearchAndReplace(Format, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%N", L"%%N", FALSE, FALSE);
jcarsey975136a2009-06-16 19:03:54 +00002714 ASSERT_EFI_ERROR(Status);
jcarseya405b862010-09-14 05:18:09 +00002715 Status = ShellCopySearchAndReplace(mPostReplaceFormat, mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), L"%E", L"%%E", FALSE, FALSE);
jcarsey975136a2009-06-16 19:03:54 +00002716 ASSERT_EFI_ERROR(Status);
jcarseya405b862010-09-14 05:18:09 +00002717 Status = ShellCopySearchAndReplace(mPostReplaceFormat2, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%H", L"%%H", FALSE, FALSE);
jcarsey975136a2009-06-16 19:03:54 +00002718 ASSERT_EFI_ERROR(Status);
jcarseya405b862010-09-14 05:18:09 +00002719 Status = ShellCopySearchAndReplace(mPostReplaceFormat, mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), L"%B", L"%%B", FALSE, FALSE);
jcarsey975136a2009-06-16 19:03:54 +00002720 ASSERT_EFI_ERROR(Status);
jcarseya405b862010-09-14 05:18:09 +00002721 Status = ShellCopySearchAndReplace(mPostReplaceFormat2, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%V", L"%%V", FALSE, FALSE);
jcarsey975136a2009-06-16 19:03:54 +00002722 ASSERT_EFI_ERROR(Status);
2723
2724 //
2725 // Use the last buffer from replacing to print from...
2726 //
jcarseya405b862010-09-14 05:18:09 +00002727 UnicodeVSPrint (mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), mPostReplaceFormat, Marker);
jcarseyb1f95a02009-06-16 00:23:19 +00002728
2729 if (Col != -1 && Row != -1) {
jcarseyb1f95a02009-06-16 00:23:19 +00002730 Status = gST->ConOut->SetCursorPosition(gST->ConOut, Col, Row);
jcarsey975136a2009-06-16 19:03:54 +00002731 }
2732
jcarseyecd3d592009-12-07 18:05:00 +00002733 FormatWalker = mPostReplaceFormat2;
jcarsey2247dde2009-11-09 18:08:58 +00002734 while (*FormatWalker != CHAR_NULL) {
jcarsey975136a2009-06-16 19:03:54 +00002735 //
2736 // Find the next attribute change request
2737 //
2738 ResumeLocation = StrStr(FormatWalker, L"%");
2739 if (ResumeLocation != NULL) {
jcarsey2247dde2009-11-09 18:08:58 +00002740 *ResumeLocation = CHAR_NULL;
jcarsey975136a2009-06-16 19:03:54 +00002741 }
2742 //
2743 // print the current FormatWalker string
2744 //
jcarseya405b862010-09-14 05:18:09 +00002745 if (StrLen(FormatWalker)>0) {
2746 Status = InternalPrintTo(FormatWalker);
2747 if (EFI_ERROR(Status)) {
2748 break;
2749 }
2750 }
2751
jcarsey975136a2009-06-16 19:03:54 +00002752 //
2753 // update the attribute
2754 //
2755 if (ResumeLocation != NULL) {
jcarsey5d46f172011-08-23 15:34:23 +00002756 if (*(ResumeLocation-1) == L'^') {
2757 //
jcarsey8bb74412012-01-30 18:44:41 +00002758 // Move cursor back 1 position to overwrite the ^
2759 //
2760 gST->ConOut->SetCursorPosition(gST->ConOut, gST->ConOut->Mode->CursorColumn - 1, gST->ConOut->Mode->CursorRow);
2761
2762 //
jcarsey5d46f172011-08-23 15:34:23 +00002763 // Print a simple '%' symbol
2764 //
2765 Status = InternalPrintTo(L"%");
2766 ResumeLocation = ResumeLocation - 1;
2767 } else {
2768 switch (*(ResumeLocation+1)) {
2769 case (L'N'):
2770 gST->ConOut->SetAttribute(gST->ConOut, OriginalAttribute);
jcarseya405b862010-09-14 05:18:09 +00002771 break;
jcarsey5d46f172011-08-23 15:34:23 +00002772 case (L'E'):
2773 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_YELLOW, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));
2774 break;
2775 case (L'H'):
2776 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_WHITE, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));
2777 break;
2778 case (L'B'):
2779 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_BLUE, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));
2780 break;
2781 case (L'V'):
2782 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_GREEN, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));
2783 break;
2784 default:
2785 //
2786 // Print a simple '%' symbol
2787 //
2788 Status = InternalPrintTo(L"%");
2789 if (EFI_ERROR(Status)) {
2790 break;
2791 }
2792 ResumeLocation = ResumeLocation - 1;
2793 break;
2794 }
jcarsey975136a2009-06-16 19:03:54 +00002795 }
2796 } else {
2797 //
2798 // reset to normal now...
2799 //
jcarsey975136a2009-06-16 19:03:54 +00002800 break;
2801 }
2802
2803 //
2804 // update FormatWalker to Resume + 2 (skip the % and the indicator)
2805 //
2806 FormatWalker = ResumeLocation + 2;
2807 }
jcarseyb1f95a02009-06-16 00:23:19 +00002808
jcarseya405b862010-09-14 05:18:09 +00002809 gST->ConOut->SetAttribute(gST->ConOut, OriginalAttribute);
jcarsey89e85372011-04-13 23:37:50 +00002810
2811 SHELL_FREE_NON_NULL(mPostReplaceFormat);
2812 SHELL_FREE_NON_NULL(mPostReplaceFormat2);
jcarseya405b862010-09-14 05:18:09 +00002813 return (Status);
jcarsey5f7431d2009-07-10 18:06:01 +00002814}
jcarsey2247dde2009-11-09 18:08:58 +00002815
2816/**
2817 Print at a specific location on the screen.
2818
jcarseye2f82972009-12-01 05:40:24 +00002819 This function will move the cursor to a given screen location and print the specified string.
jcarsey1e6e84c2010-01-25 20:05:08 +00002820
2821 If -1 is specified for either the Row or Col the current screen location for BOTH
jcarsey2247dde2009-11-09 18:08:58 +00002822 will be used.
2823
jcarseye2f82972009-12-01 05:40:24 +00002824 If either Row or Col is out of range for the current console, then ASSERT.
2825 If Format is NULL, then ASSERT.
jcarsey2247dde2009-11-09 18:08:58 +00002826
jcarsey1e6e84c2010-01-25 20:05:08 +00002827 In addition to the standard %-based flags as supported by UefiLib Print() this supports
jcarsey2247dde2009-11-09 18:08:58 +00002828 the following additional flags:
2829 %N - Set output attribute to normal
2830 %H - Set output attribute to highlight
2831 %E - Set output attribute to error
2832 %B - Set output attribute to blue color
2833 %V - Set output attribute to green color
2834
2835 Note: The background color is controlled by the shell command cls.
2836
jcarsey2247dde2009-11-09 18:08:58 +00002837 @param[in] Col the column to print at
jcarseya405b862010-09-14 05:18:09 +00002838 @param[in] Row the row to print at
jcarsey2247dde2009-11-09 18:08:58 +00002839 @param[in] Format the format string
jcarseya405b862010-09-14 05:18:09 +00002840 @param[in] ... The variable argument list.
jcarsey2247dde2009-11-09 18:08:58 +00002841
jcarseya405b862010-09-14 05:18:09 +00002842 @return EFI_SUCCESS The printing was successful.
2843 @return EFI_DEVICE_ERROR The console device reported an error.
jcarsey2247dde2009-11-09 18:08:58 +00002844**/
jcarseya405b862010-09-14 05:18:09 +00002845EFI_STATUS
jcarsey2247dde2009-11-09 18:08:58 +00002846EFIAPI
2847ShellPrintEx(
2848 IN INT32 Col OPTIONAL,
2849 IN INT32 Row OPTIONAL,
2850 IN CONST CHAR16 *Format,
2851 ...
jcarsey1e6e84c2010-01-25 20:05:08 +00002852 )
jcarsey2247dde2009-11-09 18:08:58 +00002853{
2854 VA_LIST Marker;
jcarseya405b862010-09-14 05:18:09 +00002855 EFI_STATUS RetVal;
jcarsey3e082d52010-10-04 16:44:57 +00002856 if (Format == NULL) {
2857 return (EFI_INVALID_PARAMETER);
2858 }
jcarsey2247dde2009-11-09 18:08:58 +00002859 VA_START (Marker, Format);
jcarseya405b862010-09-14 05:18:09 +00002860 RetVal = InternalShellPrintWorker(Col, Row, Format, Marker);
jcarseye2f82972009-12-01 05:40:24 +00002861 VA_END(Marker);
jcarseya405b862010-09-14 05:18:09 +00002862 return(RetVal);
jcarsey2247dde2009-11-09 18:08:58 +00002863}
2864
2865/**
2866 Print at a specific location on the screen.
2867
jcarseye2f82972009-12-01 05:40:24 +00002868 This function will move the cursor to a given screen location and print the specified string.
jcarsey1e6e84c2010-01-25 20:05:08 +00002869
2870 If -1 is specified for either the Row or Col the current screen location for BOTH
jcarseye2f82972009-12-01 05:40:24 +00002871 will be used.
jcarsey2247dde2009-11-09 18:08:58 +00002872
jcarseye2f82972009-12-01 05:40:24 +00002873 If either Row or Col is out of range for the current console, then ASSERT.
2874 If Format is NULL, then ASSERT.
jcarsey2247dde2009-11-09 18:08:58 +00002875
jcarsey1e6e84c2010-01-25 20:05:08 +00002876 In addition to the standard %-based flags as supported by UefiLib Print() this supports
jcarsey2247dde2009-11-09 18:08:58 +00002877 the following additional flags:
jcarsey1e6e84c2010-01-25 20:05:08 +00002878 %N - Set output attribute to normal.
2879 %H - Set output attribute to highlight.
2880 %E - Set output attribute to error.
2881 %B - Set output attribute to blue color.
2882 %V - Set output attribute to green color.
jcarsey2247dde2009-11-09 18:08:58 +00002883
2884 Note: The background color is controlled by the shell command cls.
2885
jcarsey1e6e84c2010-01-25 20:05:08 +00002886 @param[in] Col The column to print at.
jcarseya405b862010-09-14 05:18:09 +00002887 @param[in] Row The row to print at.
jcarsey1e6e84c2010-01-25 20:05:08 +00002888 @param[in] Language The language of the string to retrieve. If this parameter
2889 is NULL, then the current platform language is used.
2890 @param[in] HiiFormatStringId The format string Id for getting from Hii.
2891 @param[in] HiiFormatHandle The format string Handle for getting from Hii.
jcarseya405b862010-09-14 05:18:09 +00002892 @param[in] ... The variable argument list.
jcarsey2247dde2009-11-09 18:08:58 +00002893
jcarseya405b862010-09-14 05:18:09 +00002894 @return EFI_SUCCESS The printing was successful.
2895 @return EFI_DEVICE_ERROR The console device reported an error.
jcarsey2247dde2009-11-09 18:08:58 +00002896**/
jcarseya405b862010-09-14 05:18:09 +00002897EFI_STATUS
jcarsey2247dde2009-11-09 18:08:58 +00002898EFIAPI
2899ShellPrintHiiEx(
2900 IN INT32 Col OPTIONAL,
2901 IN INT32 Row OPTIONAL,
jcarsey1e6e84c2010-01-25 20:05:08 +00002902 IN CONST CHAR8 *Language OPTIONAL,
jcarsey2247dde2009-11-09 18:08:58 +00002903 IN CONST EFI_STRING_ID HiiFormatStringId,
2904 IN CONST EFI_HANDLE HiiFormatHandle,
2905 ...
2906 )
2907{
2908 VA_LIST Marker;
2909 CHAR16 *HiiFormatString;
jcarseya405b862010-09-14 05:18:09 +00002910 EFI_STATUS RetVal;
jcarsey2247dde2009-11-09 18:08:58 +00002911
2912 VA_START (Marker, HiiFormatHandle);
jcarsey1e6e84c2010-01-25 20:05:08 +00002913 HiiFormatString = HiiGetString(HiiFormatHandle, HiiFormatStringId, Language);
jcarsey2247dde2009-11-09 18:08:58 +00002914 ASSERT(HiiFormatString != NULL);
2915
2916 RetVal = InternalShellPrintWorker(Col, Row, HiiFormatString, Marker);
2917
jcarseya405b862010-09-14 05:18:09 +00002918 SHELL_FREE_NON_NULL(HiiFormatString);
jcarseye2f82972009-12-01 05:40:24 +00002919 VA_END(Marker);
jcarsey2247dde2009-11-09 18:08:58 +00002920
2921 return (RetVal);
2922}
2923
2924/**
2925 Function to determine if a given filename represents a file or a directory.
2926
2927 @param[in] DirName Path to directory to test.
2928
jcarseyc8c22592011-10-17 17:49:21 +00002929 @retval EFI_SUCCESS The Path represents a directory
2930 @retval EFI_NOT_FOUND The Path does not represent a directory
2931 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
2932 @return The path failed to open
jcarsey2247dde2009-11-09 18:08:58 +00002933**/
2934EFI_STATUS
2935EFIAPI
2936ShellIsDirectory(
2937 IN CONST CHAR16 *DirName
2938 )
2939{
2940 EFI_STATUS Status;
jcarseya405b862010-09-14 05:18:09 +00002941 SHELL_FILE_HANDLE Handle;
jcarsey3e082d52010-10-04 16:44:57 +00002942 CHAR16 *TempLocation;
2943 CHAR16 *TempLocation2;
jcarsey2247dde2009-11-09 18:08:58 +00002944
jcarseyecd3d592009-12-07 18:05:00 +00002945 ASSERT(DirName != NULL);
2946
jcarseya405b862010-09-14 05:18:09 +00002947 Handle = NULL;
2948 TempLocation = NULL;
jcarsey2247dde2009-11-09 18:08:58 +00002949
2950 Status = ShellOpenFileByName(DirName, &Handle, EFI_FILE_MODE_READ, 0);
2951 if (EFI_ERROR(Status)) {
jcarseya405b862010-09-14 05:18:09 +00002952 //
2953 // try good logic first.
2954 //
jcarsey366f81a2011-06-27 21:04:22 +00002955 if (gEfiShellProtocol != NULL) {
jcarsey3e082d52010-10-04 16:44:57 +00002956 TempLocation = StrnCatGrow(&TempLocation, NULL, DirName, 0);
jcarseyc8c22592011-10-17 17:49:21 +00002957 if (TempLocation == NULL) {
2958 ShellCloseFile(&Handle);
2959 return (EFI_OUT_OF_RESOURCES);
2960 }
jcarsey3e082d52010-10-04 16:44:57 +00002961 TempLocation2 = StrStr(TempLocation, L":");
2962 if (TempLocation2 != NULL && StrLen(StrStr(TempLocation, L":")) == 2) {
2963 *(TempLocation2+1) = CHAR_NULL;
jcarseya405b862010-09-14 05:18:09 +00002964 }
jcarsey366f81a2011-06-27 21:04:22 +00002965 if (gEfiShellProtocol->GetDevicePathFromMap(TempLocation) != NULL) {
jcarseya405b862010-09-14 05:18:09 +00002966 FreePool(TempLocation);
2967 return (EFI_SUCCESS);
2968 }
2969 FreePool(TempLocation);
2970 } else {
2971 //
2972 // probably a map name?!?!!?
2973 //
2974 TempLocation = StrStr(DirName, L"\\");
2975 if (TempLocation != NULL && *(TempLocation+1) == CHAR_NULL) {
2976 return (EFI_SUCCESS);
2977 }
2978 }
jcarsey2247dde2009-11-09 18:08:58 +00002979 return (Status);
2980 }
2981
2982 if (FileHandleIsDirectory(Handle) == EFI_SUCCESS) {
2983 ShellCloseFile(&Handle);
2984 return (EFI_SUCCESS);
2985 }
2986 ShellCloseFile(&Handle);
2987 return (EFI_NOT_FOUND);
2988}
2989
jcarsey125c2cf2009-11-18 21:36:50 +00002990/**
jcarsey36a9d672009-11-20 21:13:41 +00002991 Function to determine if a given filename represents a file.
2992
2993 @param[in] Name Path to file to test.
2994
2995 @retval EFI_SUCCESS The Path represents a file.
2996 @retval EFI_NOT_FOUND The Path does not represent a file.
2997 @retval other The path failed to open.
2998**/
2999EFI_STATUS
3000EFIAPI
3001ShellIsFile(
3002 IN CONST CHAR16 *Name
3003 )
3004{
3005 EFI_STATUS Status;
jcarseya405b862010-09-14 05:18:09 +00003006 SHELL_FILE_HANDLE Handle;
jcarsey36a9d672009-11-20 21:13:41 +00003007
jcarseyecd3d592009-12-07 18:05:00 +00003008 ASSERT(Name != NULL);
3009
jcarsey36a9d672009-11-20 21:13:41 +00003010 Handle = NULL;
3011
3012 Status = ShellOpenFileByName(Name, &Handle, EFI_FILE_MODE_READ, 0);
3013 if (EFI_ERROR(Status)) {
3014 return (Status);
3015 }
3016
3017 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {
3018 ShellCloseFile(&Handle);
3019 return (EFI_SUCCESS);
3020 }
3021 ShellCloseFile(&Handle);
3022 return (EFI_NOT_FOUND);
3023}
3024
3025/**
jcarseyb3011f42010-01-11 21:49:04 +00003026 Function to determine if a given filename represents a file.
3027
3028 This will search the CWD and then the Path.
3029
3030 If Name is NULL, then ASSERT.
3031
3032 @param[in] Name Path to file to test.
3033
3034 @retval EFI_SUCCESS The Path represents a file.
3035 @retval EFI_NOT_FOUND The Path does not represent a file.
3036 @retval other The path failed to open.
3037**/
3038EFI_STATUS
3039EFIAPI
3040ShellIsFileInPath(
3041 IN CONST CHAR16 *Name
jcarseya405b862010-09-14 05:18:09 +00003042 )
3043{
jcarseyb3011f42010-01-11 21:49:04 +00003044 CHAR16 *NewName;
3045 EFI_STATUS Status;
3046
3047 if (!EFI_ERROR(ShellIsFile(Name))) {
jcarseya405b862010-09-14 05:18:09 +00003048 return (EFI_SUCCESS);
jcarseyb3011f42010-01-11 21:49:04 +00003049 }
3050
3051 NewName = ShellFindFilePath(Name);
3052 if (NewName == NULL) {
3053 return (EFI_NOT_FOUND);
3054 }
3055 Status = ShellIsFile(NewName);
3056 FreePool(NewName);
3057 return (Status);
3058}
jcarsey252d9452011-03-25 20:49:53 +00003059
jcarseyb3011f42010-01-11 21:49:04 +00003060/**
Jaben Carsey74b0fb82013-11-22 21:37:34 +00003061 Function return the number converted from a hex representation of a number.
3062
3063 Note: this function cannot be used when (UINTN)(-1), (0xFFFFFFFF) may be a valid
3064 result. Use ShellConvertStringToUint64 instead.
3065
3066 @param[in] String String representation of a number.
3067
3068 @return The unsigned integer result of the conversion.
3069 @retval (UINTN)(-1) An error occured.
3070**/
3071UINTN
3072EFIAPI
3073ShellHexStrToUintn(
3074 IN CONST CHAR16 *String
3075 )
3076{
3077 UINT64 RetVal;
3078
3079 if (!EFI_ERROR(ShellConvertStringToUint64(String, &RetVal, TRUE, TRUE))) {
3080 return ((UINTN)RetVal);
3081 }
3082
3083 return ((UINTN)(-1));
3084}
3085
3086/**
jcarsey1e6e84c2010-01-25 20:05:08 +00003087 Function to determine whether a string is decimal or hex representation of a number
Jaben Carseyd59fc242013-11-14 23:52:43 +00003088 and return the number converted from the string. Spaces are always skipped.
jcarsey125c2cf2009-11-18 21:36:50 +00003089
3090 @param[in] String String representation of a number
3091
jcarsey252d9452011-03-25 20:49:53 +00003092 @return the number
3093 @retval (UINTN)(-1) An error ocurred.
jcarsey125c2cf2009-11-18 21:36:50 +00003094**/
3095UINTN
3096EFIAPI
3097ShellStrToUintn(
3098 IN CONST CHAR16 *String
3099 )
3100{
jcarsey252d9452011-03-25 20:49:53 +00003101 UINT64 RetVal;
3102 BOOLEAN Hex;
3103
3104 Hex = FALSE;
3105
3106 if (!InternalShellIsHexOrDecimalNumber(String, Hex, TRUE)) {
3107 Hex = TRUE;
jcarsey125c2cf2009-11-18 21:36:50 +00003108 }
jcarsey252d9452011-03-25 20:49:53 +00003109
3110 if (!EFI_ERROR(ShellConvertStringToUint64(String, &RetVal, Hex, TRUE))) {
3111 return ((UINTN)RetVal);
3112 }
3113 return ((UINTN)(-1));
jcarsey125c2cf2009-11-18 21:36:50 +00003114}
3115
3116/**
3117 Safely append with automatic string resizing given length of Destination and
3118 desired length of copy from Source.
3119
3120 append the first D characters of Source to the end of Destination, where D is
3121 the lesser of Count and the StrLen() of Source. If appending those D characters
3122 will fit within Destination (whose Size is given as CurrentSize) and
jcarsey1e6e84c2010-01-25 20:05:08 +00003123 still leave room for a NULL terminator, then those characters are appended,
3124 starting at the original terminating NULL of Destination, and a new terminating
3125 NULL is appended.
jcarsey125c2cf2009-11-18 21:36:50 +00003126
3127 If appending D characters onto Destination will result in a overflow of the size
3128 given in CurrentSize the string will be grown such that the copy can be performed
3129 and CurrentSize will be updated to the new size.
3130
3131 If Source is NULL, there is nothing to append, just return the current buffer in
3132 Destination.
3133
3134 if Destination is NULL, then ASSERT()
3135 if Destination's current length (including NULL terminator) is already more then
3136 CurrentSize, then ASSERT()
3137
ydong104ff7e372011-09-02 08:05:34 +00003138 @param[in, out] Destination The String to append onto
3139 @param[in, out] CurrentSize on call the number of bytes in Destination. On
jcarsey125c2cf2009-11-18 21:36:50 +00003140 return possibly the new size (still in bytes). if NULL
3141 then allocate whatever is needed.
3142 @param[in] Source The String to append from
3143 @param[in] Count Maximum number of characters to append. if 0 then
3144 all are appended.
3145
3146 @return Destination return the resultant string.
3147**/
3148CHAR16*
3149EFIAPI
3150StrnCatGrow (
3151 IN OUT CHAR16 **Destination,
3152 IN OUT UINTN *CurrentSize,
3153 IN CONST CHAR16 *Source,
3154 IN UINTN Count
3155 )
3156{
3157 UINTN DestinationStartSize;
3158 UINTN NewSize;
3159
3160 //
3161 // ASSERTs
3162 //
3163 ASSERT(Destination != NULL);
3164
3165 //
3166 // If there's nothing to do then just return Destination
3167 //
3168 if (Source == NULL) {
3169 return (*Destination);
3170 }
3171
3172 //
3173 // allow for un-initialized pointers, based on size being 0
3174 //
3175 if (CurrentSize != NULL && *CurrentSize == 0) {
3176 *Destination = NULL;
3177 }
3178
3179 //
3180 // allow for NULL pointers address as Destination
3181 //
3182 if (*Destination != NULL) {
3183 ASSERT(CurrentSize != 0);
3184 DestinationStartSize = StrSize(*Destination);
3185 ASSERT(DestinationStartSize <= *CurrentSize);
3186 } else {
3187 DestinationStartSize = 0;
3188// ASSERT(*CurrentSize == 0);
3189 }
3190
3191 //
3192 // Append all of Source?
3193 //
3194 if (Count == 0) {
3195 Count = StrLen(Source);
3196 }
3197
3198 //
3199 // Test and grow if required
3200 //
3201 if (CurrentSize != NULL) {
3202 NewSize = *CurrentSize;
ydong10f480fdc2012-09-07 01:55:33 +00003203 if (NewSize < DestinationStartSize + (Count * sizeof(CHAR16))) {
3204 while (NewSize < (DestinationStartSize + (Count*sizeof(CHAR16)))) {
3205 NewSize += 2 * Count * sizeof(CHAR16);
3206 }
3207 *Destination = ReallocatePool(*CurrentSize, NewSize, *Destination);
3208 *CurrentSize = NewSize;
jcarsey125c2cf2009-11-18 21:36:50 +00003209 }
jcarsey125c2cf2009-11-18 21:36:50 +00003210 } else {
3211 *Destination = AllocateZeroPool((Count+1)*sizeof(CHAR16));
3212 }
3213
3214 //
3215 // Now use standard StrnCat on a big enough buffer
3216 //
jcarseyc9d92df2010-02-03 15:37:54 +00003217 if (*Destination == NULL) {
3218 return (NULL);
3219 }
jcarsey125c2cf2009-11-18 21:36:50 +00003220 return StrnCat(*Destination, Source, Count);
3221}
jcarseyc9d92df2010-02-03 15:37:54 +00003222
3223/**
3224 Prompt the user and return the resultant answer to the requestor.
3225
3226 This function will display the requested question on the shell prompt and then
3227 wait for an apropriate answer to be input from the console.
3228
jcarseya405b862010-09-14 05:18:09 +00003229 if the SHELL_PROMPT_REQUEST_TYPE is SHELL_PROMPT_REQUEST_TYPE_YESNO, ShellPromptResponseTypeQuitContinue
jcarseyc9d92df2010-02-03 15:37:54 +00003230 or SHELL_PROMPT_REQUEST_TYPE_YESNOCANCEL then *Response is of type SHELL_PROMPT_RESPONSE.
3231
jcarseya405b862010-09-14 05:18:09 +00003232 if the SHELL_PROMPT_REQUEST_TYPE is ShellPromptResponseTypeFreeform then *Response is of type
jcarseyc9d92df2010-02-03 15:37:54 +00003233 CHAR16*.
3234
3235 In either case *Response must be callee freed if Response was not NULL;
3236
3237 @param Type What type of question is asked. This is used to filter the input
3238 to prevent invalid answers to question.
3239 @param Prompt Pointer to string prompt to use to request input.
3240 @param Response Pointer to Response which will be populated upon return.
3241
3242 @retval EFI_SUCCESS The operation was sucessful.
3243 @retval EFI_UNSUPPORTED The operation is not supported as requested.
3244 @retval EFI_INVALID_PARAMETER A parameter was invalid.
3245 @return other The operation failed.
3246**/
3247EFI_STATUS
3248EFIAPI
3249ShellPromptForResponse (
3250 IN SHELL_PROMPT_REQUEST_TYPE Type,
3251 IN CHAR16 *Prompt OPTIONAL,
3252 IN OUT VOID **Response OPTIONAL
3253 )
3254{
3255 EFI_STATUS Status;
3256 EFI_INPUT_KEY Key;
3257 UINTN EventIndex;
3258 SHELL_PROMPT_RESPONSE *Resp;
jcarseya405b862010-09-14 05:18:09 +00003259 UINTN Size;
3260 CHAR16 *Buffer;
jcarseyc9d92df2010-02-03 15:37:54 +00003261
jcarseya405b862010-09-14 05:18:09 +00003262 Status = EFI_UNSUPPORTED;
3263 Resp = NULL;
3264 Buffer = NULL;
3265 Size = 0;
3266 if (Type != ShellPromptResponseTypeFreeform) {
jcarsey252d9452011-03-25 20:49:53 +00003267 Resp = (SHELL_PROMPT_RESPONSE*)AllocateZeroPool(sizeof(SHELL_PROMPT_RESPONSE));
jcarsey3e082d52010-10-04 16:44:57 +00003268 if (Resp == NULL) {
3269 return (EFI_OUT_OF_RESOURCES);
3270 }
xdu290bfa222010-07-19 05:21:27 +00003271 }
jcarseyc9d92df2010-02-03 15:37:54 +00003272
3273 switch(Type) {
jcarseya405b862010-09-14 05:18:09 +00003274 case ShellPromptResponseTypeQuitContinue:
jcarseyc9d92df2010-02-03 15:37:54 +00003275 if (Prompt != NULL) {
3276 ShellPrintEx(-1, -1, L"%s", Prompt);
3277 }
3278 //
3279 // wait for valid response
3280 //
3281 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
3282 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
3283 ASSERT_EFI_ERROR(Status);
3284 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3285 if (Key.UnicodeChar == L'Q' || Key.UnicodeChar ==L'q') {
jcarseya405b862010-09-14 05:18:09 +00003286 *Resp = ShellPromptResponseQuit;
jcarseyc9d92df2010-02-03 15:37:54 +00003287 } else {
jcarseya405b862010-09-14 05:18:09 +00003288 *Resp = ShellPromptResponseContinue;
jcarseyc9d92df2010-02-03 15:37:54 +00003289 }
3290 break;
jcarseya405b862010-09-14 05:18:09 +00003291 case ShellPromptResponseTypeYesNoCancel:
jcarseyc9d92df2010-02-03 15:37:54 +00003292 if (Prompt != NULL) {
3293 ShellPrintEx(-1, -1, L"%s", Prompt);
3294 }
3295 //
3296 // wait for valid response
3297 //
jcarseya405b862010-09-14 05:18:09 +00003298 *Resp = ShellPromptResponseMax;
3299 while (*Resp == ShellPromptResponseMax) {
Jaben Carsey194ae482013-12-09 22:55:13 +00003300 if (ShellGetExecutionBreakFlag()) {
3301 Status = EFI_ABORTED;
3302 break;
3303 }
jcarseyc9d92df2010-02-03 15:37:54 +00003304 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
3305 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
3306 ASSERT_EFI_ERROR(Status);
3307 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3308 switch (Key.UnicodeChar) {
3309 case L'Y':
3310 case L'y':
jcarseya405b862010-09-14 05:18:09 +00003311 *Resp = ShellPromptResponseYes;
jcarseyc9d92df2010-02-03 15:37:54 +00003312 break;
3313 case L'N':
3314 case L'n':
jcarseya405b862010-09-14 05:18:09 +00003315 *Resp = ShellPromptResponseNo;
jcarseyc9d92df2010-02-03 15:37:54 +00003316 break;
3317 case L'C':
3318 case L'c':
jcarseya405b862010-09-14 05:18:09 +00003319 *Resp = ShellPromptResponseCancel;
3320 break;
3321 }
3322 }
3323 break; case ShellPromptResponseTypeYesNoAllCancel:
3324 if (Prompt != NULL) {
3325 ShellPrintEx(-1, -1, L"%s", Prompt);
3326 }
3327 //
3328 // wait for valid response
3329 //
3330 *Resp = ShellPromptResponseMax;
3331 while (*Resp == ShellPromptResponseMax) {
Jaben Carsey194ae482013-12-09 22:55:13 +00003332 if (ShellGetExecutionBreakFlag()) {
3333 Status = EFI_ABORTED;
3334 break;
3335 }
jcarseya405b862010-09-14 05:18:09 +00003336 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
3337 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
3338 ASSERT_EFI_ERROR(Status);
3339 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3340 switch (Key.UnicodeChar) {
3341 case L'Y':
3342 case L'y':
3343 *Resp = ShellPromptResponseYes;
3344 break;
3345 case L'N':
3346 case L'n':
3347 *Resp = ShellPromptResponseNo;
3348 break;
3349 case L'A':
3350 case L'a':
3351 *Resp = ShellPromptResponseAll;
3352 break;
3353 case L'C':
3354 case L'c':
3355 *Resp = ShellPromptResponseCancel;
jcarseyc9d92df2010-02-03 15:37:54 +00003356 break;
3357 }
3358 }
3359 break;
jcarseya405b862010-09-14 05:18:09 +00003360 case ShellPromptResponseTypeEnterContinue:
3361 case ShellPromptResponseTypeAnyKeyContinue:
jcarseyc9d92df2010-02-03 15:37:54 +00003362 if (Prompt != NULL) {
3363 ShellPrintEx(-1, -1, L"%s", Prompt);
3364 }
3365 //
3366 // wait for valid response
3367 //
jcarseya405b862010-09-14 05:18:09 +00003368 *Resp = ShellPromptResponseMax;
3369 while (*Resp == ShellPromptResponseMax) {
Jaben Carsey194ae482013-12-09 22:55:13 +00003370 if (ShellGetExecutionBreakFlag()) {
3371 Status = EFI_ABORTED;
3372 break;
3373 }
jcarseyc9d92df2010-02-03 15:37:54 +00003374 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
jcarseya405b862010-09-14 05:18:09 +00003375 if (Type == ShellPromptResponseTypeEnterContinue) {
jcarseyc9d92df2010-02-03 15:37:54 +00003376 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
3377 ASSERT_EFI_ERROR(Status);
3378 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3379 if (Key.UnicodeChar == CHAR_CARRIAGE_RETURN) {
jcarseya405b862010-09-14 05:18:09 +00003380 *Resp = ShellPromptResponseContinue;
jcarseyc9d92df2010-02-03 15:37:54 +00003381 break;
3382 }
3383 }
jcarseya405b862010-09-14 05:18:09 +00003384 if (Type == ShellPromptResponseTypeAnyKeyContinue) {
3385 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
3386 ASSERT_EFI_ERROR(Status);
3387 *Resp = ShellPromptResponseContinue;
jcarseyc9d92df2010-02-03 15:37:54 +00003388 break;
3389 }
3390 }
3391 break;
jcarseya405b862010-09-14 05:18:09 +00003392 case ShellPromptResponseTypeYesNo:
3393 if (Prompt != NULL) {
3394 ShellPrintEx(-1, -1, L"%s", Prompt);
3395 }
3396 //
3397 // wait for valid response
3398 //
3399 *Resp = ShellPromptResponseMax;
3400 while (*Resp == ShellPromptResponseMax) {
Jaben Carsey194ae482013-12-09 22:55:13 +00003401 if (ShellGetExecutionBreakFlag()) {
3402 Status = EFI_ABORTED;
3403 break;
3404 }
jcarseya405b862010-09-14 05:18:09 +00003405 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
3406 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
3407 ASSERT_EFI_ERROR(Status);
3408 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3409 switch (Key.UnicodeChar) {
3410 case L'Y':
3411 case L'y':
3412 *Resp = ShellPromptResponseYes;
3413 break;
3414 case L'N':
3415 case L'n':
3416 *Resp = ShellPromptResponseNo;
3417 break;
3418 }
3419 }
3420 break;
3421 case ShellPromptResponseTypeFreeform:
3422 if (Prompt != NULL) {
3423 ShellPrintEx(-1, -1, L"%s", Prompt);
3424 }
3425 while(1) {
Jaben Carsey194ae482013-12-09 22:55:13 +00003426 if (ShellGetExecutionBreakFlag()) {
3427 Status = EFI_ABORTED;
3428 break;
3429 }
jcarseya405b862010-09-14 05:18:09 +00003430 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
3431 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
3432 ASSERT_EFI_ERROR(Status);
3433 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3434 if (Key.UnicodeChar == CHAR_CARRIAGE_RETURN) {
3435 break;
3436 }
3437 ASSERT((Buffer == NULL && Size == 0) || (Buffer != NULL));
3438 StrnCatGrow(&Buffer, &Size, &Key.UnicodeChar, 1);
3439 }
3440 break;
3441 //
3442 // This is the location to add new prompt types.
Jaben Carsey194ae482013-12-09 22:55:13 +00003443 // If your new type loops remember to add ExecutionBreak support.
jcarseya405b862010-09-14 05:18:09 +00003444 //
jcarseyc9d92df2010-02-03 15:37:54 +00003445 default:
jcarseya405b862010-09-14 05:18:09 +00003446 ASSERT(FALSE);
jcarseyc9d92df2010-02-03 15:37:54 +00003447 }
3448
3449 if (Response != NULL) {
jcarseya405b862010-09-14 05:18:09 +00003450 if (Resp != NULL) {
3451 *Response = Resp;
3452 } else if (Buffer != NULL) {
3453 *Response = Buffer;
3454 }
jcarseyc9d92df2010-02-03 15:37:54 +00003455 } else {
jcarseya405b862010-09-14 05:18:09 +00003456 if (Resp != NULL) {
3457 FreePool(Resp);
3458 }
3459 if (Buffer != NULL) {
3460 FreePool(Buffer);
3461 }
jcarseyc9d92df2010-02-03 15:37:54 +00003462 }
3463
jcarseya405b862010-09-14 05:18:09 +00003464 ShellPrintEx(-1, -1, L"\r\n");
jcarseyc9d92df2010-02-03 15:37:54 +00003465 return (Status);
3466}
3467
3468/**
3469 Prompt the user and return the resultant answer to the requestor.
3470
3471 This function is the same as ShellPromptForResponse, except that the prompt is
3472 automatically pulled from HII.
3473
3474 @param Type What type of question is asked. This is used to filter the input
3475 to prevent invalid answers to question.
jcarseya405b862010-09-14 05:18:09 +00003476 @param[in] HiiFormatStringId The format string Id for getting from Hii.
3477 @param[in] HiiFormatHandle The format string Handle for getting from Hii.
3478 @param Response Pointer to Response which will be populated upon return.
jcarseyc9d92df2010-02-03 15:37:54 +00003479
3480 @retval EFI_SUCCESS the operation was sucessful.
3481 @return other the operation failed.
3482
3483 @sa ShellPromptForResponse
3484**/
3485EFI_STATUS
3486EFIAPI
3487ShellPromptForResponseHii (
3488 IN SHELL_PROMPT_REQUEST_TYPE Type,
3489 IN CONST EFI_STRING_ID HiiFormatStringId,
3490 IN CONST EFI_HANDLE HiiFormatHandle,
3491 IN OUT VOID **Response
3492 )
3493{
3494 CHAR16 *Prompt;
3495 EFI_STATUS Status;
3496
3497 Prompt = HiiGetString(HiiFormatHandle, HiiFormatStringId, NULL);
3498 Status = ShellPromptForResponse(Type, Prompt, Response);
3499 FreePool(Prompt);
3500 return (Status);
3501}
3502
jcarseya405b862010-09-14 05:18:09 +00003503/**
3504 Function to determin if an entire string is a valid number.
jcarseyc9d92df2010-02-03 15:37:54 +00003505
jcarseya405b862010-09-14 05:18:09 +00003506 If Hex it must be preceeded with a 0x or has ForceHex, set TRUE.
3507
3508 @param[in] String The string to evaluate.
3509 @param[in] ForceHex TRUE - always assume hex.
3510 @param[in] StopAtSpace TRUE to halt upon finding a space, FALSE to keep going.
3511
3512 @retval TRUE It is all numeric (dec/hex) characters.
3513 @retval FALSE There is a non-numeric character.
3514**/
3515BOOLEAN
3516EFIAPI
jcarsey252d9452011-03-25 20:49:53 +00003517InternalShellIsHexOrDecimalNumber (
jcarseya405b862010-09-14 05:18:09 +00003518 IN CONST CHAR16 *String,
3519 IN CONST BOOLEAN ForceHex,
3520 IN CONST BOOLEAN StopAtSpace
3521 )
3522{
3523 BOOLEAN Hex;
3524
3525 //
3526 // chop off a single negative sign
3527 //
3528 if (String != NULL && *String == L'-') {
3529 String++;
3530 }
darylm503b0934ac2011-11-12 00:35:11 +00003531
jcarseya405b862010-09-14 05:18:09 +00003532 if (String == NULL) {
3533 return (FALSE);
3534 }
3535
3536 //
3537 // chop leading zeroes
3538 //
3539 while(String != NULL && *String == L'0'){
3540 String++;
3541 }
3542 //
3543 // allow '0x' or '0X', but not 'x' or 'X'
3544 //
3545 if (String != NULL && (*String == L'x' || *String == L'X')) {
3546 if (*(String-1) != L'0') {
3547 //
3548 // we got an x without a preceeding 0
3549 //
3550 return (FALSE);
3551 }
3552 String++;
3553 Hex = TRUE;
3554 } else if (ForceHex) {
3555 Hex = TRUE;
3556 } else {
3557 Hex = FALSE;
3558 }
3559
3560 //
3561 // loop through the remaining characters and use the lib function
3562 //
3563 for ( ; String != NULL && *String != CHAR_NULL && !(StopAtSpace && *String == L' ') ; String++){
3564 if (Hex) {
3565 if (!ShellIsHexaDecimalDigitCharacter(*String)) {
3566 return (FALSE);
3567 }
3568 } else {
3569 if (!ShellIsDecimalDigitCharacter(*String)) {
3570 return (FALSE);
3571 }
3572 }
3573 }
jcarsey252d9452011-03-25 20:49:53 +00003574
jcarseya405b862010-09-14 05:18:09 +00003575 return (TRUE);
3576}
3577
3578/**
3579 Function to determine if a given filename exists.
3580
3581 @param[in] Name Path to test.
3582
3583 @retval EFI_SUCCESS The Path represents a file.
3584 @retval EFI_NOT_FOUND The Path does not represent a file.
3585 @retval other The path failed to open.
3586**/
3587EFI_STATUS
3588EFIAPI
3589ShellFileExists(
3590 IN CONST CHAR16 *Name
3591 )
3592{
3593 EFI_STATUS Status;
3594 EFI_SHELL_FILE_INFO *List;
3595
3596 ASSERT(Name != NULL);
3597
3598 List = NULL;
3599 Status = ShellOpenFileMetaArg((CHAR16*)Name, EFI_FILE_MODE_READ, &List);
3600 if (EFI_ERROR(Status)) {
3601 return (Status);
3602 }
3603
3604 ShellCloseFileMetaArg(&List);
3605
3606 return (EFI_SUCCESS);
3607}
jcarsey252d9452011-03-25 20:49:53 +00003608
3609/**
darylm503b0934ac2011-11-12 00:35:11 +00003610 Convert a Unicode character to upper case only if
jcarsey252d9452011-03-25 20:49:53 +00003611 it maps to a valid small-case ASCII character.
3612
3613 This internal function only deal with Unicode character
3614 which maps to a valid small-case ASCII character, i.e.
3615 L'a' to L'z'. For other Unicode character, the input character
3616 is returned directly.
3617
3618 @param Char The character to convert.
3619
3620 @retval LowerCharacter If the Char is with range L'a' to L'z'.
3621 @retval Unchanged Otherwise.
3622
3623**/
3624CHAR16
3625EFIAPI
3626InternalShellCharToUpper (
3627 IN CHAR16 Char
3628 )
3629{
3630 if (Char >= L'a' && Char <= L'z') {
3631 return (CHAR16) (Char - (L'a' - L'A'));
3632 }
3633
3634 return Char;
3635}
3636
3637/**
3638 Convert a Unicode character to numerical value.
3639
3640 This internal function only deal with Unicode character
3641 which maps to a valid hexadecimal ASII character, i.e.
darylm503b0934ac2011-11-12 00:35:11 +00003642 L'0' to L'9', L'a' to L'f' or L'A' to L'F'. For other
jcarsey252d9452011-03-25 20:49:53 +00003643 Unicode character, the value returned does not make sense.
3644
3645 @param Char The character to convert.
3646
3647 @return The numerical value converted.
3648
3649**/
3650UINTN
3651EFIAPI
3652InternalShellHexCharToUintn (
3653 IN CHAR16 Char
3654 )
3655{
3656 if (ShellIsDecimalDigitCharacter (Char)) {
3657 return Char - L'0';
3658 }
3659
3660 return (UINTN) (10 + InternalShellCharToUpper (Char) - L'A');
3661}
3662
3663/**
3664 Convert a Null-terminated Unicode hexadecimal string to a value of type UINT64.
3665
3666 This function returns a value of type UINTN by interpreting the contents
3667 of the Unicode string specified by String as a hexadecimal number.
3668 The format of the input Unicode string String is:
3669
3670 [spaces][zeros][x][hexadecimal digits].
3671
3672 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
3673 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix.
3674 If "x" appears in the input string, it must be prefixed with at least one 0.
3675 The function will ignore the pad space, which includes spaces or tab characters,
3676 before [zeros], [x] or [hexadecimal digit]. The running zero before [x] or
3677 [hexadecimal digit] will be ignored. Then, the decoding starts after [x] or the
3678 first valid hexadecimal digit. Then, the function stops at the first character that is
3679 a not a valid hexadecimal character or NULL, whichever one comes first.
3680
3681 If String has only pad spaces, then zero is returned.
3682 If String has no leading pad spaces, leading zeros or valid hexadecimal digits,
3683 then zero is returned.
3684
3685 @param[in] String A pointer to a Null-terminated Unicode string.
3686 @param[out] Value Upon a successful return the value of the conversion.
3687 @param[in] StopAtSpace FALSE to skip spaces.
3688
3689 @retval EFI_SUCCESS The conversion was successful.
3690 @retval EFI_INVALID_PARAMETER A parameter was NULL or invalid.
3691 @retval EFI_DEVICE_ERROR An overflow occured.
3692**/
3693EFI_STATUS
3694EFIAPI
3695InternalShellStrHexToUint64 (
3696 IN CONST CHAR16 *String,
3697 OUT UINT64 *Value,
3698 IN CONST BOOLEAN StopAtSpace
3699 )
3700{
3701 UINT64 Result;
3702
3703 if (String == NULL || StrSize(String) == 0 || Value == NULL) {
3704 return (EFI_INVALID_PARAMETER);
3705 }
darylm503b0934ac2011-11-12 00:35:11 +00003706
jcarsey252d9452011-03-25 20:49:53 +00003707 //
darylm503b0934ac2011-11-12 00:35:11 +00003708 // Ignore the pad spaces (space or tab)
jcarsey252d9452011-03-25 20:49:53 +00003709 //
3710 while ((*String == L' ') || (*String == L'\t')) {
3711 String++;
3712 }
3713
3714 //
3715 // Ignore leading Zeros after the spaces
3716 //
3717 while (*String == L'0') {
3718 String++;
3719 }
3720
3721 if (InternalShellCharToUpper (*String) == L'X') {
3722 if (*(String - 1) != L'0') {
3723 return 0;
3724 }
3725 //
3726 // Skip the 'X'
3727 //
3728 String++;
3729 }
3730
3731 Result = 0;
darylm503b0934ac2011-11-12 00:35:11 +00003732
jcarsey252d9452011-03-25 20:49:53 +00003733 //
ydong1012ea4692012-07-26 05:42:43 +00003734 // Skip spaces if requested
jcarsey252d9452011-03-25 20:49:53 +00003735 //
ydong1012ea4692012-07-26 05:42:43 +00003736 while (StopAtSpace && *String == L' ') {
3737 String++;
jcarsey252d9452011-03-25 20:49:53 +00003738 }
darylm503b0934ac2011-11-12 00:35:11 +00003739
jcarsey252d9452011-03-25 20:49:53 +00003740 while (ShellIsHexaDecimalDigitCharacter (*String)) {
3741 //
darylm503b0934ac2011-11-12 00:35:11 +00003742 // If the Hex Number represented by String overflows according
jcarsey252d9452011-03-25 20:49:53 +00003743 // to the range defined by UINTN, then ASSERT().
3744 //
3745 if (!(Result <= (RShiftU64((((UINT64) ~0) - InternalShellHexCharToUintn (*String)), 4)))) {
3746// if (!(Result <= ((((UINT64) ~0) - InternalShellHexCharToUintn (*String)) >> 4))) {
3747 return (EFI_DEVICE_ERROR);
3748 }
3749
3750 Result = (LShiftU64(Result, 4));
3751 Result += InternalShellHexCharToUintn (*String);
3752 String++;
3753
3754 //
jcarsey49bd4982012-01-27 18:42:43 +00003755 // stop at spaces if requested
jcarsey252d9452011-03-25 20:49:53 +00003756 //
jcarsey49bd4982012-01-27 18:42:43 +00003757 if (StopAtSpace && *String == L' ') {
3758 break;
jcarsey252d9452011-03-25 20:49:53 +00003759 }
3760 }
3761
3762 *Value = Result;
3763 return (EFI_SUCCESS);
3764}
3765
3766/**
3767 Convert a Null-terminated Unicode decimal string to a value of
3768 type UINT64.
3769
3770 This function returns a value of type UINT64 by interpreting the contents
3771 of the Unicode string specified by String as a decimal number. The format
3772 of the input Unicode string String is:
3773
3774 [spaces] [decimal digits].
3775
3776 The valid decimal digit character is in the range [0-9]. The
3777 function will ignore the pad space, which includes spaces or
3778 tab characters, before [decimal digits]. The running zero in the
3779 beginning of [decimal digits] will be ignored. Then, the function
3780 stops at the first character that is a not a valid decimal character
3781 or a Null-terminator, whichever one comes first.
3782
3783 If String has only pad spaces, then 0 is returned.
3784 If String has no pad spaces or valid decimal digits,
3785 then 0 is returned.
3786
3787 @param[in] String A pointer to a Null-terminated Unicode string.
3788 @param[out] Value Upon a successful return the value of the conversion.
3789 @param[in] StopAtSpace FALSE to skip spaces.
3790
3791 @retval EFI_SUCCESS The conversion was successful.
3792 @retval EFI_INVALID_PARAMETER A parameter was NULL or invalid.
3793 @retval EFI_DEVICE_ERROR An overflow occured.
3794**/
3795EFI_STATUS
3796EFIAPI
3797InternalShellStrDecimalToUint64 (
3798 IN CONST CHAR16 *String,
3799 OUT UINT64 *Value,
3800 IN CONST BOOLEAN StopAtSpace
3801 )
3802{
3803 UINT64 Result;
3804
3805 if (String == NULL || StrSize (String) == 0 || Value == NULL) {
3806 return (EFI_INVALID_PARAMETER);
3807 }
3808
3809 //
3810 // Ignore the pad spaces (space or tab)
3811 //
3812 while ((*String == L' ') || (*String == L'\t')) {
3813 String++;
3814 }
3815
3816 //
3817 // Ignore leading Zeros after the spaces
3818 //
3819 while (*String == L'0') {
3820 String++;
3821 }
3822
3823 Result = 0;
3824
3825 //
3826 // Skip spaces if requested
3827 //
3828 while (StopAtSpace && *String == L' ') {
3829 String++;
3830 }
3831 while (ShellIsDecimalDigitCharacter (*String)) {
3832 //
darylm503b0934ac2011-11-12 00:35:11 +00003833 // If the number represented by String overflows according
jcarsey252d9452011-03-25 20:49:53 +00003834 // to the range defined by UINT64, then ASSERT().
3835 //
darylm503b0934ac2011-11-12 00:35:11 +00003836
jcarsey252d9452011-03-25 20:49:53 +00003837 if (!(Result <= (DivU64x32((((UINT64) ~0) - (*String - L'0')),10)))) {
3838 return (EFI_DEVICE_ERROR);
3839 }
3840
3841 Result = MultU64x32(Result, 10) + (*String - L'0');
3842 String++;
3843
3844 //
3845 // Stop at spaces if requested
3846 //
3847 if (StopAtSpace && *String == L' ') {
3848 break;
3849 }
3850 }
3851
3852 *Value = Result;
darylm503b0934ac2011-11-12 00:35:11 +00003853
jcarsey252d9452011-03-25 20:49:53 +00003854 return (EFI_SUCCESS);
3855}
3856
3857/**
3858 Function to verify and convert a string to its numerical value.
3859
3860 If Hex it must be preceeded with a 0x, 0X, or has ForceHex set TRUE.
3861
3862 @param[in] String The string to evaluate.
3863 @param[out] Value Upon a successful return the value of the conversion.
3864 @param[in] ForceHex TRUE - always assume hex.
3865 @param[in] StopAtSpace FALSE to skip spaces.
darylm503b0934ac2011-11-12 00:35:11 +00003866
jcarsey252d9452011-03-25 20:49:53 +00003867 @retval EFI_SUCCESS The conversion was successful.
3868 @retval EFI_INVALID_PARAMETER String contained an invalid character.
3869 @retval EFI_NOT_FOUND String was a number, but Value was NULL.
3870**/
3871EFI_STATUS
3872EFIAPI
3873ShellConvertStringToUint64(
3874 IN CONST CHAR16 *String,
3875 OUT UINT64 *Value,
3876 IN CONST BOOLEAN ForceHex,
3877 IN CONST BOOLEAN StopAtSpace
3878 )
3879{
3880 UINT64 RetVal;
3881 CONST CHAR16 *Walker;
3882 EFI_STATUS Status;
3883 BOOLEAN Hex;
3884
3885 Hex = ForceHex;
3886
3887 if (!InternalShellIsHexOrDecimalNumber(String, Hex, StopAtSpace)) {
3888 if (!Hex) {
3889 Hex = TRUE;
3890 if (!InternalShellIsHexOrDecimalNumber(String, Hex, StopAtSpace)) {
3891 return (EFI_INVALID_PARAMETER);
3892 }
3893 } else {
3894 return (EFI_INVALID_PARAMETER);
3895 }
3896 }
3897
3898 //
3899 // Chop off leading spaces
3900 //
3901 for (Walker = String; Walker != NULL && *Walker != CHAR_NULL && *Walker == L' '; Walker++);
3902
3903 //
3904 // make sure we have something left that is numeric.
3905 //
3906 if (Walker == NULL || *Walker == CHAR_NULL || !InternalShellIsHexOrDecimalNumber(Walker, Hex, StopAtSpace)) {
3907 return (EFI_INVALID_PARAMETER);
darylm503b0934ac2011-11-12 00:35:11 +00003908 }
jcarsey252d9452011-03-25 20:49:53 +00003909
3910 //
3911 // do the conversion.
3912 //
3913 if (Hex || StrnCmp(Walker, L"0x", 2) == 0 || StrnCmp(Walker, L"0X", 2) == 0){
3914 Status = InternalShellStrHexToUint64(Walker, &RetVal, StopAtSpace);
3915 } else {
3916 Status = InternalShellStrDecimalToUint64(Walker, &RetVal, StopAtSpace);
3917 }
3918
3919 if (Value == NULL && !EFI_ERROR(Status)) {
3920 return (EFI_NOT_FOUND);
3921 }
3922
3923 if (Value != NULL) {
3924 *Value = RetVal;
3925 }
3926
3927 return (Status);
3928}
3929
3930/**
3931 Function to determin if an entire string is a valid number.
3932
3933 If Hex it must be preceeded with a 0x or has ForceHex, set TRUE.
3934
3935 @param[in] String The string to evaluate.
3936 @param[in] ForceHex TRUE - always assume hex.
3937 @param[in] StopAtSpace TRUE to halt upon finding a space, FALSE to keep going.
3938
3939 @retval TRUE It is all numeric (dec/hex) characters.
3940 @retval FALSE There is a non-numeric character.
3941**/
3942BOOLEAN
3943EFIAPI
3944ShellIsHexOrDecimalNumber (
3945 IN CONST CHAR16 *String,
3946 IN CONST BOOLEAN ForceHex,
3947 IN CONST BOOLEAN StopAtSpace
3948 )
3949{
3950 if (ShellConvertStringToUint64(String, NULL, ForceHex, StopAtSpace) == EFI_NOT_FOUND) {
3951 return (TRUE);
3952 }
3953 return (FALSE);
3954}
jcarsey4d0a4fc2011-07-06 22:28:36 +00003955
3956/**
3957 Function to read a single line from a SHELL_FILE_HANDLE. The \n is not included in the returned
3958 buffer. The returned buffer must be callee freed.
3959
3960 If the position upon start is 0, then the Ascii Boolean will be set. This should be
3961 maintained and not changed for all operations with the same file.
3962
ydong104ff7e372011-09-02 08:05:34 +00003963 @param[in] Handle SHELL_FILE_HANDLE to read from.
3964 @param[in, out] Ascii Boolean value for indicating whether the file is
3965 Ascii (TRUE) or UCS2 (FALSE).
jcarsey4d0a4fc2011-07-06 22:28:36 +00003966
jcarseybeab0fc2011-10-10 17:26:25 +00003967 @return The line of text from the file.
3968 @retval NULL There was not enough memory available.
jcarsey4d0a4fc2011-07-06 22:28:36 +00003969
3970 @sa ShellFileHandleReadLine
3971**/
3972CHAR16*
3973EFIAPI
3974ShellFileHandleReturnLine(
3975 IN SHELL_FILE_HANDLE Handle,
3976 IN OUT BOOLEAN *Ascii
3977 )
3978{
3979 CHAR16 *RetVal;
3980 UINTN Size;
3981 EFI_STATUS Status;
3982
3983 Size = 0;
3984 RetVal = NULL;
3985
3986 Status = ShellFileHandleReadLine(Handle, RetVal, &Size, FALSE, Ascii);
3987 if (Status == EFI_BUFFER_TOO_SMALL) {
3988 RetVal = AllocateZeroPool(Size);
jcarseybeab0fc2011-10-10 17:26:25 +00003989 if (RetVal == NULL) {
3990 return (NULL);
3991 }
jcarsey4d0a4fc2011-07-06 22:28:36 +00003992 Status = ShellFileHandleReadLine(Handle, RetVal, &Size, FALSE, Ascii);
darylm503b0934ac2011-11-12 00:35:11 +00003993
jcarsey4d0a4fc2011-07-06 22:28:36 +00003994 }
jcarsey4d0a4fc2011-07-06 22:28:36 +00003995 if (EFI_ERROR(Status) && (RetVal != NULL)) {
3996 FreePool(RetVal);
3997 RetVal = NULL;
3998 }
3999 return (RetVal);
4000}
4001
4002/**
4003 Function to read a single line (up to but not including the \n) from a SHELL_FILE_HANDLE.
4004
4005 If the position upon start is 0, then the Ascii Boolean will be set. This should be
4006 maintained and not changed for all operations with the same file.
4007
ydong104ff7e372011-09-02 08:05:34 +00004008 @param[in] Handle SHELL_FILE_HANDLE to read from.
4009 @param[in, out] Buffer The pointer to buffer to read into.
4010 @param[in, out] Size The pointer to number of bytes in Buffer.
4011 @param[in] Truncate If the buffer is large enough, this has no effect.
4012 If the buffer is is too small and Truncate is TRUE,
4013 the line will be truncated.
4014 If the buffer is is too small and Truncate is FALSE,
4015 then no read will occur.
jcarsey4d0a4fc2011-07-06 22:28:36 +00004016
ydong104ff7e372011-09-02 08:05:34 +00004017 @param[in, out] Ascii Boolean value for indicating whether the file is
4018 Ascii (TRUE) or UCS2 (FALSE).
jcarsey4d0a4fc2011-07-06 22:28:36 +00004019
4020 @retval EFI_SUCCESS The operation was successful. The line is stored in
4021 Buffer.
4022 @retval EFI_INVALID_PARAMETER Handle was NULL.
4023 @retval EFI_INVALID_PARAMETER Size was NULL.
4024 @retval EFI_BUFFER_TOO_SMALL Size was not large enough to store the line.
4025 Size was updated to the minimum space required.
4026**/
4027EFI_STATUS
4028EFIAPI
4029ShellFileHandleReadLine(
4030 IN SHELL_FILE_HANDLE Handle,
4031 IN OUT CHAR16 *Buffer,
4032 IN OUT UINTN *Size,
4033 IN BOOLEAN Truncate,
4034 IN OUT BOOLEAN *Ascii
4035 )
4036{
4037 EFI_STATUS Status;
4038 CHAR16 CharBuffer;
4039 UINTN CharSize;
4040 UINTN CountSoFar;
4041 UINT64 OriginalFilePosition;
4042
4043
4044 if (Handle == NULL
4045 ||Size == NULL
4046 ){
4047 return (EFI_INVALID_PARAMETER);
4048 }
4049 if (Buffer == NULL) {
4050 ASSERT(*Size == 0);
4051 } else {
4052 *Buffer = CHAR_NULL;
4053 }
4054 gEfiShellProtocol->GetFilePosition(Handle, &OriginalFilePosition);
4055 if (OriginalFilePosition == 0) {
4056 CharSize = sizeof(CHAR16);
4057 Status = gEfiShellProtocol->ReadFile(Handle, &CharSize, &CharBuffer);
4058 ASSERT_EFI_ERROR(Status);
4059 if (CharBuffer == gUnicodeFileTag) {
4060 *Ascii = FALSE;
4061 } else {
4062 *Ascii = TRUE;
4063 gEfiShellProtocol->SetFilePosition(Handle, OriginalFilePosition);
4064 }
4065 }
4066
4067 for (CountSoFar = 0;;CountSoFar++){
4068 CharBuffer = 0;
4069 if (*Ascii) {
4070 CharSize = sizeof(CHAR8);
4071 } else {
4072 CharSize = sizeof(CHAR16);
4073 }
4074 Status = gEfiShellProtocol->ReadFile(Handle, &CharSize, &CharBuffer);
4075 if ( EFI_ERROR(Status)
4076 || CharSize == 0
4077 || (CharBuffer == L'\n' && !(*Ascii))
4078 || (CharBuffer == '\n' && *Ascii)
4079 ){
4080 break;
4081 }
4082 //
4083 // if we have space save it...
4084 //
4085 if ((CountSoFar+1)*sizeof(CHAR16) < *Size){
4086 ASSERT(Buffer != NULL);
4087 ((CHAR16*)Buffer)[CountSoFar] = CharBuffer;
4088 ((CHAR16*)Buffer)[CountSoFar+1] = CHAR_NULL;
4089 }
4090 }
4091
4092 //
4093 // if we ran out of space tell when...
4094 //
4095 if ((CountSoFar+1)*sizeof(CHAR16) > *Size){
4096 *Size = (CountSoFar+1)*sizeof(CHAR16);
4097 if (!Truncate) {
4098 gEfiShellProtocol->SetFilePosition(Handle, OriginalFilePosition);
4099 } else {
4100 DEBUG((DEBUG_WARN, "The line was truncated in ShellFileHandleReadLine"));
4101 }
4102 return (EFI_BUFFER_TOO_SMALL);
4103 }
4104 while(Buffer[StrLen(Buffer)-1] == L'\r') {
4105 Buffer[StrLen(Buffer)-1] = CHAR_NULL;
4106 }
4107
4108 return (Status);
4109}
jcarseyfb5278e2013-02-20 18:21:14 +00004110
4111/**
jcarsey365aa982013-03-04 21:54:02 +00004112 Function to print help file / man page content in the spec from the UEFI Shell protocol GetHelpText function.
4113
4114 @param[in] CommandToGetHelpOn Pointer to a string containing the command name of help file to be printed.
4115 @param[in] SectionToGetHelpOn Pointer to the section specifier(s).
4116 @param[in] PrintCommandText If TRUE, prints the command followed by the help content, otherwise prints
4117 the help content only.
4118 @retval EFI_DEVICE_ERROR The help data format was incorrect.
4119 @retval EFI_NOT_FOUND The help data could not be found.
4120 @retval EFI_SUCCESS The operation was successful.
4121**/
4122EFI_STATUS
4123EFIAPI
4124ShellPrintHelp (
4125 IN CONST CHAR16 *CommandToGetHelpOn,
4126 IN CONST CHAR16 *SectionToGetHelpOn,
4127 IN BOOLEAN PrintCommandText
4128 )
4129{
4130 EFI_STATUS Status;
4131 CHAR16 *OutText;
4132
4133 OutText = NULL;
4134
4135 //
4136 // Get the string to print based
4137 //
4138 Status = gEfiShellProtocol->GetHelpText (CommandToGetHelpOn, SectionToGetHelpOn, &OutText);
4139
4140 //
4141 // make sure we got a valid string
4142 //
4143 if (EFI_ERROR(Status)){
4144 return Status;
4145 }
4146 if (OutText == NULL || StrLen(OutText) == 0) {
4147 return EFI_NOT_FOUND;
4148 }
4149
4150 //
4151 // Chop off trailing stuff we dont need
4152 //
4153 while (OutText[StrLen(OutText)-1] == L'\r' || OutText[StrLen(OutText)-1] == L'\n' || OutText[StrLen(OutText)-1] == L' ') {
4154 OutText[StrLen(OutText)-1] = CHAR_NULL;
4155 }
4156
4157 //
4158 // Print this out to the console
4159 //
4160 if (PrintCommandText) {
4161 ShellPrintEx(-1, -1, L"%H%-14s%N- %s\r\n", CommandToGetHelpOn, OutText);
4162 } else {
4163 ShellPrintEx(-1, -1, L"%N%s\r\n", OutText);
4164 }
4165
4166 SHELL_FREE_NON_NULL(OutText);
4167
4168 return EFI_SUCCESS;
4169}
4170
4171/**
jcarseyfb5278e2013-02-20 18:21:14 +00004172 Function to delete a file by name
4173
4174 @param[in] FileName Pointer to file name to delete.
4175
4176 @retval EFI_SUCCESS the file was deleted sucessfully
4177 @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not
4178 deleted
4179 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
4180 @retval EFI_NOT_FOUND The specified file could not be found on the
4181 device or the file system could not be found
4182 on the device.
4183 @retval EFI_NO_MEDIA The device has no medium.
4184 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
4185 medium is no longer supported.
4186 @retval EFI_DEVICE_ERROR The device reported an error.
4187 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
4188 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
4189 @retval EFI_ACCESS_DENIED The file was opened read only.
4190 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
4191 file.
4192 @retval other The file failed to open
4193**/
4194EFI_STATUS
4195EFIAPI
4196ShellDeleteFileByName(
4197 IN CONST CHAR16 *FileName
4198 )
4199{
4200 EFI_STATUS Status;
4201 SHELL_FILE_HANDLE FileHandle;
4202
4203 Status = ShellFileExists(FileName);
4204
4205 if (Status == EFI_SUCCESS){
4206 Status = ShellOpenFileByName(FileName, &FileHandle, EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE, 0x0);
4207 if (Status == EFI_SUCCESS){
4208 Status = ShellDeleteFile(&FileHandle);
4209 }
4210 }
4211
4212 return(Status);
4213
4214}