blob: e30e5c7dd14f03cf53b55a7ba97cdd99cae61596 [file] [log] [blame]
jcarsey94b17fa2009-05-07 18:46:18 +00001/** @file
2 Provides interface to shell functionality for shell commands and applications.
3
Jaben Carsey75a5e2e2014-01-10 16:42:45 +00004 Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>
jcarsey1e6e84c2010-01-25 20:05:08 +00005 This program and the accompanying materials
jcarseyb3011f42010-01-11 21:49:04 +00006 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
jcarsey94b17fa2009-05-07 18:46:18 +00009
jcarseyb3011f42010-01-11 21:49:04 +000010 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
jcarsey94b17fa2009-05-07 18:46:18 +000012
13**/
14
jcarseyb1f95a02009-06-16 00:23:19 +000015#include "UefiShellLib.h"
jcarseya405b862010-09-14 05:18:09 +000016#include <ShellBase.h>
jcarsey252d9452011-03-25 20:49:53 +000017#include <Library/SortLib.h>
jcarseyd2b45642009-05-11 18:02:16 +000018
jcarsey94b17fa2009-05-07 18:46:18 +000019#define FIND_XXXXX_FILE_BUFFER_SIZE (SIZE_OF_EFI_FILE_INFO + MAX_FILE_NAME_LEN)
20
jcarseyd2b45642009-05-11 18:02:16 +000021//
jcarseya405b862010-09-14 05:18:09 +000022// globals...
jcarseyd2b45642009-05-11 18:02:16 +000023//
24SHELL_PARAM_ITEM EmptyParamList[] = {
25 {NULL, TypeMax}
26 };
jcarseya405b862010-09-14 05:18:09 +000027SHELL_PARAM_ITEM SfoParamList[] = {
28 {L"-sfo", TypeFlag},
29 {NULL, TypeMax}
30 };
31EFI_SHELL_ENVIRONMENT2 *mEfiShellEnvironment2;
32EFI_SHELL_INTERFACE *mEfiShellInterface;
jcarsey366f81a2011-06-27 21:04:22 +000033EFI_SHELL_PROTOCOL *gEfiShellProtocol;
34EFI_SHELL_PARAMETERS_PROTOCOL *gEfiShellParametersProtocol;
jcarseya405b862010-09-14 05:18:09 +000035EFI_HANDLE mEfiShellEnvironment2Handle;
36FILE_HANDLE_FUNCTION_MAP FileFunctionMap;
jcarseyb3011f42010-01-11 21:49:04 +000037
jcarsey2247dde2009-11-09 18:08:58 +000038/**
39 Check if a Unicode character is a hexadecimal character.
40
jcarsey1e6e84c2010-01-25 20:05:08 +000041 This internal function checks if a Unicode character is a
jcarseya405b862010-09-14 05:18:09 +000042 numeric character. The valid hexadecimal characters are
jcarsey2247dde2009-11-09 18:08:58 +000043 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.
44
jcarsey2247dde2009-11-09 18:08:58 +000045 @param Char The character to check against.
46
47 @retval TRUE If the Char is a hexadecmial character.
48 @retval FALSE If the Char is not a hexadecmial character.
49
50**/
51BOOLEAN
52EFIAPI
jcarsey969c7832010-01-13 16:46:33 +000053ShellIsHexaDecimalDigitCharacter (
jcarsey2247dde2009-11-09 18:08:58 +000054 IN CHAR16 Char
jcarseya405b862010-09-14 05:18:09 +000055 )
56{
jcarsey2247dde2009-11-09 18:08:58 +000057 return (BOOLEAN) ((Char >= L'0' && Char <= L'9') || (Char >= L'A' && Char <= L'F') || (Char >= L'a' && Char <= L'f'));
58}
jcarsey94b17fa2009-05-07 18:46:18 +000059
60/**
jcarseya405b862010-09-14 05:18:09 +000061 Check if a Unicode character is a decimal character.
62
63 This internal function checks if a Unicode character is a
64 decimal character. The valid characters are
65 L'0' to L'9'.
66
67
68 @param Char The character to check against.
69
70 @retval TRUE If the Char is a hexadecmial character.
71 @retval FALSE If the Char is not a hexadecmial character.
72
73**/
74BOOLEAN
75EFIAPI
76ShellIsDecimalDigitCharacter (
77 IN CHAR16 Char
78 )
79{
80 return (BOOLEAN) (Char >= L'0' && Char <= L'9');
81}
82
83/**
84 Helper function to find ShellEnvironment2 for constructor.
85
86 @param[in] ImageHandle A copy of the calling image's handle.
jcarseybeab0fc2011-10-10 17:26:25 +000087
88 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.
jcarsey94b17fa2009-05-07 18:46:18 +000089**/
90EFI_STATUS
91EFIAPI
92ShellFindSE2 (
93 IN EFI_HANDLE ImageHandle
jcarseya405b862010-09-14 05:18:09 +000094 )
95{
jcarsey94b17fa2009-05-07 18:46:18 +000096 EFI_STATUS Status;
97 EFI_HANDLE *Buffer;
98 UINTN BufferSize;
99 UINTN HandleIndex;
100
101 BufferSize = 0;
102 Buffer = NULL;
jcarsey1e6e84c2010-01-25 20:05:08 +0000103 Status = gBS->OpenProtocol(ImageHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000104 &gEfiShellEnvironment2Guid,
105 (VOID **)&mEfiShellEnvironment2,
106 ImageHandle,
107 NULL,
108 EFI_OPEN_PROTOCOL_GET_PROTOCOL
jcarseya405b862010-09-14 05:18:09 +0000109 );
jcarsey94b17fa2009-05-07 18:46:18 +0000110 //
111 // look for the mEfiShellEnvironment2 protocol at a higher level
112 //
jcarseya405b862010-09-14 05:18:09 +0000113 if (EFI_ERROR (Status) || !(CompareGuid (&mEfiShellEnvironment2->SESGuid, &gEfiShellEnvironment2ExtGuid))){
jcarsey94b17fa2009-05-07 18:46:18 +0000114 //
115 // figure out how big of a buffer we need.
116 //
117 Status = gBS->LocateHandle (ByProtocol,
118 &gEfiShellEnvironment2Guid,
119 NULL, // ignored for ByProtocol
120 &BufferSize,
121 Buffer
jcarseya405b862010-09-14 05:18:09 +0000122 );
jcarsey2247dde2009-11-09 18:08:58 +0000123 //
124 // maybe it's not there???
125 //
126 if (Status == EFI_BUFFER_TOO_SMALL) {
jcarsey252d9452011-03-25 20:49:53 +0000127 Buffer = (EFI_HANDLE*)AllocateZeroPool(BufferSize);
jcarseybeab0fc2011-10-10 17:26:25 +0000128 if (Buffer == NULL) {
129 return (EFI_OUT_OF_RESOURCES);
130 }
jcarsey2247dde2009-11-09 18:08:58 +0000131 Status = gBS->LocateHandle (ByProtocol,
132 &gEfiShellEnvironment2Guid,
133 NULL, // ignored for ByProtocol
134 &BufferSize,
135 Buffer
jcarseya405b862010-09-14 05:18:09 +0000136 );
jcarsey2247dde2009-11-09 18:08:58 +0000137 }
jcarsey1cd45e72010-01-29 15:07:44 +0000138 if (!EFI_ERROR (Status) && Buffer != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +0000139 //
140 // now parse the list of returned handles
141 //
142 Status = EFI_NOT_FOUND;
143 for (HandleIndex = 0; HandleIndex < (BufferSize/sizeof(Buffer[0])); HandleIndex++) {
jcarsey1e6e84c2010-01-25 20:05:08 +0000144 Status = gBS->OpenProtocol(Buffer[HandleIndex],
jcarsey94b17fa2009-05-07 18:46:18 +0000145 &gEfiShellEnvironment2Guid,
146 (VOID **)&mEfiShellEnvironment2,
147 ImageHandle,
148 NULL,
149 EFI_OPEN_PROTOCOL_GET_PROTOCOL
jcarseya405b862010-09-14 05:18:09 +0000150 );
151 if (CompareGuid (&mEfiShellEnvironment2->SESGuid, &gEfiShellEnvironment2ExtGuid)) {
jcarsey94b17fa2009-05-07 18:46:18 +0000152 mEfiShellEnvironment2Handle = Buffer[HandleIndex];
153 Status = EFI_SUCCESS;
154 break;
155 }
156 }
157 }
158 }
159 if (Buffer != NULL) {
160 FreePool (Buffer);
161 }
162 return (Status);
163}
164
jcarsey252d9452011-03-25 20:49:53 +0000165/**
darylm503b0934ac2011-11-12 00:35:11 +0000166 Function to do most of the work of the constructor. Allows for calling
jcarseya405b862010-09-14 05:18:09 +0000167 multiple times without complete re-initialization.
168
169 @param[in] ImageHandle A copy of the ImageHandle.
170 @param[in] SystemTable A pointer to the SystemTable for the application.
jcarsey252d9452011-03-25 20:49:53 +0000171
172 @retval EFI_SUCCESS The operationw as successful.
jcarseya405b862010-09-14 05:18:09 +0000173**/
jcarsey94b17fa2009-05-07 18:46:18 +0000174EFI_STATUS
175EFIAPI
jcarseyd2b45642009-05-11 18:02:16 +0000176ShellLibConstructorWorker (
jcarsey94b17fa2009-05-07 18:46:18 +0000177 IN EFI_HANDLE ImageHandle,
178 IN EFI_SYSTEM_TABLE *SystemTable
jcarseya405b862010-09-14 05:18:09 +0000179 )
180{
181 EFI_STATUS Status;
jcarseyecd3d592009-12-07 18:05:00 +0000182
jcarsey94b17fa2009-05-07 18:46:18 +0000183 //
184 // UEFI 2.0 shell interfaces (used preferentially)
185 //
jcarseya405b862010-09-14 05:18:09 +0000186 Status = gBS->OpenProtocol(
187 ImageHandle,
188 &gEfiShellProtocolGuid,
jcarsey366f81a2011-06-27 21:04:22 +0000189 (VOID **)&gEfiShellProtocol,
jcarseya405b862010-09-14 05:18:09 +0000190 ImageHandle,
191 NULL,
192 EFI_OPEN_PROTOCOL_GET_PROTOCOL
193 );
jcarsey94b17fa2009-05-07 18:46:18 +0000194 if (EFI_ERROR(Status)) {
jcarseya405b862010-09-14 05:18:09 +0000195 //
196 // Search for the shell protocol
197 //
198 Status = gBS->LocateProtocol(
199 &gEfiShellProtocolGuid,
200 NULL,
jcarsey366f81a2011-06-27 21:04:22 +0000201 (VOID **)&gEfiShellProtocol
jcarseya405b862010-09-14 05:18:09 +0000202 );
203 if (EFI_ERROR(Status)) {
jcarsey366f81a2011-06-27 21:04:22 +0000204 gEfiShellProtocol = NULL;
jcarseya405b862010-09-14 05:18:09 +0000205 }
jcarsey94b17fa2009-05-07 18:46:18 +0000206 }
jcarseya405b862010-09-14 05:18:09 +0000207 Status = gBS->OpenProtocol(
208 ImageHandle,
209 &gEfiShellParametersProtocolGuid,
jcarsey366f81a2011-06-27 21:04:22 +0000210 (VOID **)&gEfiShellParametersProtocol,
jcarseya405b862010-09-14 05:18:09 +0000211 ImageHandle,
212 NULL,
213 EFI_OPEN_PROTOCOL_GET_PROTOCOL
214 );
jcarsey94b17fa2009-05-07 18:46:18 +0000215 if (EFI_ERROR(Status)) {
jcarsey366f81a2011-06-27 21:04:22 +0000216 gEfiShellParametersProtocol = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +0000217 }
218
jcarsey366f81a2011-06-27 21:04:22 +0000219 if (gEfiShellParametersProtocol == NULL || gEfiShellProtocol == NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +0000220 //
221 // Moved to seperate function due to complexity
222 //
223 Status = ShellFindSE2(ImageHandle);
224
225 if (EFI_ERROR(Status)) {
226 DEBUG((DEBUG_ERROR, "Status: 0x%08x\r\n", Status));
227 mEfiShellEnvironment2 = NULL;
228 }
jcarsey1e6e84c2010-01-25 20:05:08 +0000229 Status = gBS->OpenProtocol(ImageHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000230 &gEfiShellInterfaceGuid,
231 (VOID **)&mEfiShellInterface,
232 ImageHandle,
233 NULL,
234 EFI_OPEN_PROTOCOL_GET_PROTOCOL
jcarseya405b862010-09-14 05:18:09 +0000235 );
jcarsey94b17fa2009-05-07 18:46:18 +0000236 if (EFI_ERROR(Status)) {
237 mEfiShellInterface = NULL;
238 }
239 }
jcarseyc9d92df2010-02-03 15:37:54 +0000240
jcarsey94b17fa2009-05-07 18:46:18 +0000241 //
242 // only success getting 2 of either the old or new, but no 1/2 and 1/2
243 //
jcarsey1e6e84c2010-01-25 20:05:08 +0000244 if ((mEfiShellEnvironment2 != NULL && mEfiShellInterface != NULL) ||
jcarsey366f81a2011-06-27 21:04:22 +0000245 (gEfiShellProtocol != NULL && gEfiShellParametersProtocol != NULL) ) {
246 if (gEfiShellProtocol != NULL) {
247 FileFunctionMap.GetFileInfo = gEfiShellProtocol->GetFileInfo;
248 FileFunctionMap.SetFileInfo = gEfiShellProtocol->SetFileInfo;
249 FileFunctionMap.ReadFile = gEfiShellProtocol->ReadFile;
250 FileFunctionMap.WriteFile = gEfiShellProtocol->WriteFile;
251 FileFunctionMap.CloseFile = gEfiShellProtocol->CloseFile;
252 FileFunctionMap.DeleteFile = gEfiShellProtocol->DeleteFile;
253 FileFunctionMap.GetFilePosition = gEfiShellProtocol->GetFilePosition;
254 FileFunctionMap.SetFilePosition = gEfiShellProtocol->SetFilePosition;
255 FileFunctionMap.FlushFile = gEfiShellProtocol->FlushFile;
256 FileFunctionMap.GetFileSize = gEfiShellProtocol->GetFileSize;
jcarseyd2b45642009-05-11 18:02:16 +0000257 } else {
jcarseya405b862010-09-14 05:18:09 +0000258 FileFunctionMap.GetFileInfo = (EFI_SHELL_GET_FILE_INFO)FileHandleGetInfo;
259 FileFunctionMap.SetFileInfo = (EFI_SHELL_SET_FILE_INFO)FileHandleSetInfo;
260 FileFunctionMap.ReadFile = (EFI_SHELL_READ_FILE)FileHandleRead;
261 FileFunctionMap.WriteFile = (EFI_SHELL_WRITE_FILE)FileHandleWrite;
262 FileFunctionMap.CloseFile = (EFI_SHELL_CLOSE_FILE)FileHandleClose;
263 FileFunctionMap.DeleteFile = (EFI_SHELL_DELETE_FILE)FileHandleDelete;
264 FileFunctionMap.GetFilePosition = (EFI_SHELL_GET_FILE_POSITION)FileHandleGetPosition;
265 FileFunctionMap.SetFilePosition = (EFI_SHELL_SET_FILE_POSITION)FileHandleSetPosition;
266 FileFunctionMap.FlushFile = (EFI_SHELL_FLUSH_FILE)FileHandleFlush;
267 FileFunctionMap.GetFileSize = (EFI_SHELL_GET_FILE_SIZE)FileHandleGetSize;
jcarseyd2b45642009-05-11 18:02:16 +0000268 }
jcarsey94b17fa2009-05-07 18:46:18 +0000269 return (EFI_SUCCESS);
270 }
271 return (EFI_NOT_FOUND);
272}
jcarseyd2b45642009-05-11 18:02:16 +0000273/**
274 Constructor for the Shell library.
275
276 Initialize the library and determine if the underlying is a UEFI Shell 2.0 or an EFI shell.
277
278 @param ImageHandle the image handle of the process
279 @param SystemTable the EFI System Table pointer
280
281 @retval EFI_SUCCESS the initialization was complete sucessfully
282 @return others an error ocurred during initialization
283**/
284EFI_STATUS
285EFIAPI
286ShellLibConstructor (
287 IN EFI_HANDLE ImageHandle,
288 IN EFI_SYSTEM_TABLE *SystemTable
jcarseya405b862010-09-14 05:18:09 +0000289 )
290{
jcarseyd2b45642009-05-11 18:02:16 +0000291 mEfiShellEnvironment2 = NULL;
jcarsey366f81a2011-06-27 21:04:22 +0000292 gEfiShellProtocol = NULL;
293 gEfiShellParametersProtocol = NULL;
jcarseyd2b45642009-05-11 18:02:16 +0000294 mEfiShellInterface = NULL;
295 mEfiShellEnvironment2Handle = NULL;
296
jcarseyd2b45642009-05-11 18:02:16 +0000297 //
298 // verify that auto initialize is not set false
jcarsey1e6e84c2010-01-25 20:05:08 +0000299 //
jcarseyd2b45642009-05-11 18:02:16 +0000300 if (PcdGetBool(PcdShellLibAutoInitialize) == 0) {
301 return (EFI_SUCCESS);
302 }
jcarsey1e6e84c2010-01-25 20:05:08 +0000303
jcarseyd2b45642009-05-11 18:02:16 +0000304 return (ShellLibConstructorWorker(ImageHandle, SystemTable));
305}
jcarsey94b17fa2009-05-07 18:46:18 +0000306
307/**
jcarseya405b862010-09-14 05:18:09 +0000308 Destructor for the library. free any resources.
309
310 @param[in] ImageHandle A copy of the ImageHandle.
311 @param[in] SystemTable A pointer to the SystemTable for the application.
312
313 @retval EFI_SUCCESS The operation was successful.
314 @return An error from the CloseProtocol function.
jcarsey94b17fa2009-05-07 18:46:18 +0000315**/
316EFI_STATUS
317EFIAPI
318ShellLibDestructor (
319 IN EFI_HANDLE ImageHandle,
320 IN EFI_SYSTEM_TABLE *SystemTable
jcarseya405b862010-09-14 05:18:09 +0000321 )
322{
jcarsey94b17fa2009-05-07 18:46:18 +0000323 if (mEfiShellEnvironment2 != NULL) {
324 gBS->CloseProtocol(mEfiShellEnvironment2Handle==NULL?ImageHandle:mEfiShellEnvironment2Handle,
325 &gEfiShellEnvironment2Guid,
326 ImageHandle,
327 NULL);
jcarseyd2b45642009-05-11 18:02:16 +0000328 mEfiShellEnvironment2 = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +0000329 }
330 if (mEfiShellInterface != NULL) {
331 gBS->CloseProtocol(ImageHandle,
332 &gEfiShellInterfaceGuid,
333 ImageHandle,
jcarsey1e6e84c2010-01-25 20:05:08 +0000334 NULL);
jcarseyd2b45642009-05-11 18:02:16 +0000335 mEfiShellInterface = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +0000336 }
jcarsey366f81a2011-06-27 21:04:22 +0000337 if (gEfiShellProtocol != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +0000338 gBS->CloseProtocol(ImageHandle,
339 &gEfiShellProtocolGuid,
340 ImageHandle,
jcarsey1e6e84c2010-01-25 20:05:08 +0000341 NULL);
jcarsey366f81a2011-06-27 21:04:22 +0000342 gEfiShellProtocol = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +0000343 }
jcarsey366f81a2011-06-27 21:04:22 +0000344 if (gEfiShellParametersProtocol != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +0000345 gBS->CloseProtocol(ImageHandle,
346 &gEfiShellParametersProtocolGuid,
347 ImageHandle,
jcarsey1e6e84c2010-01-25 20:05:08 +0000348 NULL);
jcarsey366f81a2011-06-27 21:04:22 +0000349 gEfiShellParametersProtocol = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +0000350 }
jcarseyd2b45642009-05-11 18:02:16 +0000351 mEfiShellEnvironment2Handle = NULL;
jcarseyecd3d592009-12-07 18:05:00 +0000352
jcarsey94b17fa2009-05-07 18:46:18 +0000353 return (EFI_SUCCESS);
354}
jcarseyd2b45642009-05-11 18:02:16 +0000355
356/**
357 This function causes the shell library to initialize itself. If the shell library
358 is already initialized it will de-initialize all the current protocol poitners and
359 re-populate them again.
360
361 When the library is used with PcdShellLibAutoInitialize set to true this function
362 will return EFI_SUCCESS and perform no actions.
363
364 This function is intended for internal access for shell commands only.
365
366 @retval EFI_SUCCESS the initialization was complete sucessfully
367
368**/
369EFI_STATUS
370EFIAPI
371ShellInitialize (
jcarseya405b862010-09-14 05:18:09 +0000372 )
373{
jcarseyd2b45642009-05-11 18:02:16 +0000374 //
375 // if auto initialize is not false then skip
376 //
377 if (PcdGetBool(PcdShellLibAutoInitialize) != 0) {
378 return (EFI_SUCCESS);
379 }
380
381 //
382 // deinit the current stuff
383 //
384 ASSERT_EFI_ERROR(ShellLibDestructor(gImageHandle, gST));
385
386 //
387 // init the new stuff
388 //
389 return (ShellLibConstructorWorker(gImageHandle, gST));
390}
391
jcarsey94b17fa2009-05-07 18:46:18 +0000392/**
jcarsey1e6e84c2010-01-25 20:05:08 +0000393 This function will retrieve the information about the file for the handle
jcarsey94b17fa2009-05-07 18:46:18 +0000394 specified and store it in allocated pool memory.
395
jcarsey1e6e84c2010-01-25 20:05:08 +0000396 This function allocates a buffer to store the file's information. It is the
qhuang869817bf2009-05-20 14:42:48 +0000397 caller's responsibility to free the buffer
jcarsey94b17fa2009-05-07 18:46:18 +0000398
jcarsey1e6e84c2010-01-25 20:05:08 +0000399 @param FileHandle The file handle of the file for which information is
jcarsey94b17fa2009-05-07 18:46:18 +0000400 being requested.
401
402 @retval NULL information could not be retrieved.
403
404 @return the information about the file
405**/
406EFI_FILE_INFO*
407EFIAPI
408ShellGetFileInfo (
jcarseya405b862010-09-14 05:18:09 +0000409 IN SHELL_FILE_HANDLE FileHandle
410 )
411{
jcarseyd2b45642009-05-11 18:02:16 +0000412 return (FileFunctionMap.GetFileInfo(FileHandle));
jcarsey94b17fa2009-05-07 18:46:18 +0000413}
414
415/**
jcarseya405b862010-09-14 05:18:09 +0000416 This function sets the information about the file for the opened handle
jcarsey94b17fa2009-05-07 18:46:18 +0000417 specified.
418
jcarseya405b862010-09-14 05:18:09 +0000419 @param[in] FileHandle The file handle of the file for which information
420 is being set.
jcarsey94b17fa2009-05-07 18:46:18 +0000421
jcarseya405b862010-09-14 05:18:09 +0000422 @param[in] FileInfo The information to set.
jcarsey94b17fa2009-05-07 18:46:18 +0000423
darylm503b0934ac2011-11-12 00:35:11 +0000424 @retval EFI_SUCCESS The information was set.
jcarseya405b862010-09-14 05:18:09 +0000425 @retval EFI_INVALID_PARAMETER A parameter was out of range or invalid.
426 @retval EFI_UNSUPPORTED The FileHandle does not support FileInfo.
darylm503b0934ac2011-11-12 00:35:11 +0000427 @retval EFI_NO_MEDIA The device has no medium.
428 @retval EFI_DEVICE_ERROR The device reported an error.
429 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
430 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
jcarseya405b862010-09-14 05:18:09 +0000431 @retval EFI_ACCESS_DENIED The file was opened read only.
432 @retval EFI_VOLUME_FULL The volume is full.
jcarsey94b17fa2009-05-07 18:46:18 +0000433**/
434EFI_STATUS
435EFIAPI
436ShellSetFileInfo (
darylm503b0934ac2011-11-12 00:35:11 +0000437 IN SHELL_FILE_HANDLE FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000438 IN EFI_FILE_INFO *FileInfo
jcarseya405b862010-09-14 05:18:09 +0000439 )
440{
jcarseyd2b45642009-05-11 18:02:16 +0000441 return (FileFunctionMap.SetFileInfo(FileHandle, FileInfo));
jcarsey1e6e84c2010-01-25 20:05:08 +0000442}
443
jcarsey94b17fa2009-05-07 18:46:18 +0000444 /**
445 This function will open a file or directory referenced by DevicePath.
446
jcarsey1e6e84c2010-01-25 20:05:08 +0000447 This function opens a file with the open mode according to the file path. The
jcarsey94b17fa2009-05-07 18:46:18 +0000448 Attributes is valid only for EFI_FILE_MODE_CREATE.
449
darylm503b0934ac2011-11-12 00:35:11 +0000450 @param FilePath on input the device path to the file. On output
jcarsey94b17fa2009-05-07 18:46:18 +0000451 the remaining device path.
darylm503b0934ac2011-11-12 00:35:11 +0000452 @param DeviceHandle pointer to the system device handle.
453 @param FileHandle pointer to the file handle.
454 @param OpenMode the mode to open the file with.
455 @param Attributes the file's file attributes.
jcarsey94b17fa2009-05-07 18:46:18 +0000456
darylm503b0934ac2011-11-12 00:35:11 +0000457 @retval EFI_SUCCESS The information was set.
458 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
459 @retval EFI_UNSUPPORTED Could not open the file path.
460 @retval EFI_NOT_FOUND The specified file could not be found on the
jcarsey1e6e84c2010-01-25 20:05:08 +0000461 device or the file system could not be found on
jcarsey94b17fa2009-05-07 18:46:18 +0000462 the device.
darylm503b0934ac2011-11-12 00:35:11 +0000463 @retval EFI_NO_MEDIA The device has no medium.
464 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
jcarsey94b17fa2009-05-07 18:46:18 +0000465 medium is no longer supported.
darylm503b0934ac2011-11-12 00:35:11 +0000466 @retval EFI_DEVICE_ERROR The device reported an error.
467 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
468 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
469 @retval EFI_ACCESS_DENIED The file was opened read only.
470 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
jcarsey94b17fa2009-05-07 18:46:18 +0000471 file.
darylm503b0934ac2011-11-12 00:35:11 +0000472 @retval EFI_VOLUME_FULL The volume is full.
jcarsey94b17fa2009-05-07 18:46:18 +0000473**/
474EFI_STATUS
475EFIAPI
476ShellOpenFileByDevicePath(
darylm503b0934ac2011-11-12 00:35:11 +0000477 IN OUT EFI_DEVICE_PATH_PROTOCOL **FilePath,
478 OUT EFI_HANDLE *DeviceHandle,
jcarseya405b862010-09-14 05:18:09 +0000479 OUT SHELL_FILE_HANDLE *FileHandle,
darylm503b0934ac2011-11-12 00:35:11 +0000480 IN UINT64 OpenMode,
481 IN UINT64 Attributes
jcarseya405b862010-09-14 05:18:09 +0000482 )
483{
484 CHAR16 *FileName;
485 EFI_STATUS Status;
jcarsey94b17fa2009-05-07 18:46:18 +0000486 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *EfiSimpleFileSystemProtocol;
jcarseya405b862010-09-14 05:18:09 +0000487 EFI_FILE_PROTOCOL *Handle1;
488 EFI_FILE_PROTOCOL *Handle2;
ydong100b6cb332013-01-25 02:00:22 +0000489 CHAR16 *FnafPathName;
490 UINTN PathLen;
jcarsey94b17fa2009-05-07 18:46:18 +0000491
jcarsey92a54472011-06-27 20:33:13 +0000492 if (FilePath == NULL || FileHandle == NULL || DeviceHandle == NULL) {
493 return (EFI_INVALID_PARAMETER);
494 }
495
jcarsey1e6e84c2010-01-25 20:05:08 +0000496 //
jcarsey94b17fa2009-05-07 18:46:18 +0000497 // which shell interface should we use
498 //
jcarsey366f81a2011-06-27 21:04:22 +0000499 if (gEfiShellProtocol != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +0000500 //
501 // use UEFI Shell 2.0 method.
502 //
jcarsey366f81a2011-06-27 21:04:22 +0000503 FileName = gEfiShellProtocol->GetFilePathFromDevicePath(*FilePath);
jcarsey94b17fa2009-05-07 18:46:18 +0000504 if (FileName == NULL) {
505 return (EFI_INVALID_PARAMETER);
506 }
507 Status = ShellOpenFileByName(FileName, FileHandle, OpenMode, Attributes);
508 FreePool(FileName);
509 return (Status);
jcarsey1e6e84c2010-01-25 20:05:08 +0000510 }
jcarseyd2b45642009-05-11 18:02:16 +0000511
512
513 //
514 // use old shell method.
515 //
jcarsey1e6e84c2010-01-25 20:05:08 +0000516 Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid,
517 FilePath,
jcarseyd2b45642009-05-11 18:02:16 +0000518 DeviceHandle);
519 if (EFI_ERROR (Status)) {
520 return Status;
521 }
522 Status = gBS->OpenProtocol(*DeviceHandle,
523 &gEfiSimpleFileSystemProtocolGuid,
jcarseyb1f95a02009-06-16 00:23:19 +0000524 (VOID**)&EfiSimpleFileSystemProtocol,
jcarseyd2b45642009-05-11 18:02:16 +0000525 gImageHandle,
526 NULL,
527 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
528 if (EFI_ERROR (Status)) {
529 return Status;
530 }
jcarseya405b862010-09-14 05:18:09 +0000531 Status = EfiSimpleFileSystemProtocol->OpenVolume(EfiSimpleFileSystemProtocol, &Handle1);
jcarseyd2b45642009-05-11 18:02:16 +0000532 if (EFI_ERROR (Status)) {
533 FileHandle = NULL;
534 return Status;
535 }
536
537 //
538 // go down directories one node at a time.
539 //
540 while (!IsDevicePathEnd (*FilePath)) {
jcarsey94b17fa2009-05-07 18:46:18 +0000541 //
jcarseyd2b45642009-05-11 18:02:16 +0000542 // For file system access each node should be a file path component
jcarsey94b17fa2009-05-07 18:46:18 +0000543 //
jcarseyd2b45642009-05-11 18:02:16 +0000544 if (DevicePathType (*FilePath) != MEDIA_DEVICE_PATH ||
545 DevicePathSubType (*FilePath) != MEDIA_FILEPATH_DP
jcarseya405b862010-09-14 05:18:09 +0000546 ) {
jcarsey94b17fa2009-05-07 18:46:18 +0000547 FileHandle = NULL;
jcarseyd2b45642009-05-11 18:02:16 +0000548 return (EFI_INVALID_PARAMETER);
jcarsey94b17fa2009-05-07 18:46:18 +0000549 }
jcarseyd2b45642009-05-11 18:02:16 +0000550 //
551 // Open this file path node
552 //
jcarseya405b862010-09-14 05:18:09 +0000553 Handle2 = Handle1;
554 Handle1 = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +0000555
556 //
ydong100b6cb332013-01-25 02:00:22 +0000557 // File Name Alignment Fix (FNAF)
558 // Handle2->Open may be incapable of handling a unaligned CHAR16 data.
559 // The structure pointed to by FilePath may be not CHAR16 aligned.
560 // This code copies the potentially unaligned PathName data from the
561 // FilePath structure to the aligned FnafPathName for use in the
562 // calls to Handl2->Open.
563 //
564
565 //
566 // Determine length of PathName, in bytes.
567 //
568 PathLen = DevicePathNodeLength (*FilePath) - SIZE_OF_FILEPATH_DEVICE_PATH;
569
570 //
571 // Allocate memory for the aligned copy of the string Extra allocation is to allow for forced alignment
572 // Copy bytes from possibly unaligned location to aligned location
573 //
574 FnafPathName = AllocateCopyPool(PathLen, (UINT8 *)((FILEPATH_DEVICE_PATH*)*FilePath)->PathName);
575 if (FnafPathName == NULL) {
576 return EFI_OUT_OF_RESOURCES;
577 }
578
579 //
jcarseyd2b45642009-05-11 18:02:16 +0000580 // Try to test opening an existing file
jcarsey94b17fa2009-05-07 18:46:18 +0000581 //
jcarseya405b862010-09-14 05:18:09 +0000582 Status = Handle2->Open (
583 Handle2,
584 &Handle1,
ydong100b6cb332013-01-25 02:00:22 +0000585 FnafPathName,
jcarseyd2b45642009-05-11 18:02:16 +0000586 OpenMode &~EFI_FILE_MODE_CREATE,
587 0
jcarseya405b862010-09-14 05:18:09 +0000588 );
jcarsey94b17fa2009-05-07 18:46:18 +0000589
jcarseyd2b45642009-05-11 18:02:16 +0000590 //
591 // see if the error was that it needs to be created
592 //
593 if ((EFI_ERROR (Status)) && (OpenMode != (OpenMode &~EFI_FILE_MODE_CREATE))) {
jcarseya405b862010-09-14 05:18:09 +0000594 Status = Handle2->Open (
595 Handle2,
596 &Handle1,
ydong100b6cb332013-01-25 02:00:22 +0000597 FnafPathName,
jcarseyd2b45642009-05-11 18:02:16 +0000598 OpenMode,
599 Attributes
jcarseya405b862010-09-14 05:18:09 +0000600 );
jcarsey94b17fa2009-05-07 18:46:18 +0000601 }
ydong100b6cb332013-01-25 02:00:22 +0000602
603 //
604 // Free the alignment buffer
605 //
606 FreePool(FnafPathName);
607
jcarseyd2b45642009-05-11 18:02:16 +0000608 //
609 // Close the last node
610 //
jcarseya405b862010-09-14 05:18:09 +0000611 Handle2->Close (Handle2);
jcarseyd2b45642009-05-11 18:02:16 +0000612
613 if (EFI_ERROR(Status)) {
614 return (Status);
615 }
616
617 //
618 // Get the next node
619 //
620 *FilePath = NextDevicePathNode (*FilePath);
jcarsey94b17fa2009-05-07 18:46:18 +0000621 }
jcarseya405b862010-09-14 05:18:09 +0000622
623 //
624 // This is a weak spot since if the undefined SHELL_FILE_HANDLE format changes this must change also!
625 //
626 *FileHandle = (VOID*)Handle1;
jcarseyd2b45642009-05-11 18:02:16 +0000627 return (EFI_SUCCESS);
jcarsey94b17fa2009-05-07 18:46:18 +0000628}
629
630/**
631 This function will open a file or directory referenced by filename.
632
jcarsey1e6e84c2010-01-25 20:05:08 +0000633 If return is EFI_SUCCESS, the Filehandle is the opened file's handle;
634 otherwise, the Filehandle is NULL. The Attributes is valid only for
jcarsey94b17fa2009-05-07 18:46:18 +0000635 EFI_FILE_MODE_CREATE.
636
jcarsey92a54472011-06-27 20:33:13 +0000637 if FileName is NULL then ASSERT()
jcarsey94b17fa2009-05-07 18:46:18 +0000638
darylm503b0934ac2011-11-12 00:35:11 +0000639 @param FileName pointer to file name
640 @param FileHandle pointer to the file handle.
641 @param OpenMode the mode to open the file with.
642 @param Attributes the file's file attributes.
jcarsey94b17fa2009-05-07 18:46:18 +0000643
darylm503b0934ac2011-11-12 00:35:11 +0000644 @retval EFI_SUCCESS The information was set.
645 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
646 @retval EFI_UNSUPPORTED Could not open the file path.
647 @retval EFI_NOT_FOUND The specified file could not be found on the
jcarsey1e6e84c2010-01-25 20:05:08 +0000648 device or the file system could not be found
jcarsey94b17fa2009-05-07 18:46:18 +0000649 on the device.
darylm503b0934ac2011-11-12 00:35:11 +0000650 @retval EFI_NO_MEDIA The device has no medium.
651 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
jcarsey94b17fa2009-05-07 18:46:18 +0000652 medium is no longer supported.
darylm503b0934ac2011-11-12 00:35:11 +0000653 @retval EFI_DEVICE_ERROR The device reported an error.
654 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
655 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
656 @retval EFI_ACCESS_DENIED The file was opened read only.
657 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
jcarsey94b17fa2009-05-07 18:46:18 +0000658 file.
darylm503b0934ac2011-11-12 00:35:11 +0000659 @retval EFI_VOLUME_FULL The volume is full.
jcarsey94b17fa2009-05-07 18:46:18 +0000660**/
661EFI_STATUS
662EFIAPI
663ShellOpenFileByName(
darylm503b0934ac2011-11-12 00:35:11 +0000664 IN CONST CHAR16 *FileName,
jcarseya405b862010-09-14 05:18:09 +0000665 OUT SHELL_FILE_HANDLE *FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000666 IN UINT64 OpenMode,
darylm503b0934ac2011-11-12 00:35:11 +0000667 IN UINT64 Attributes
jcarseya405b862010-09-14 05:18:09 +0000668 )
669{
jcarsey94b17fa2009-05-07 18:46:18 +0000670 EFI_HANDLE DeviceHandle;
671 EFI_DEVICE_PATH_PROTOCOL *FilePath;
jcarseyb1f95a02009-06-16 00:23:19 +0000672 EFI_STATUS Status;
673 EFI_FILE_INFO *FileInfo;
jcarsey94b17fa2009-05-07 18:46:18 +0000674
675 //
676 // ASSERT if FileName is NULL
677 //
678 ASSERT(FileName != NULL);
679
jcarseya405b862010-09-14 05:18:09 +0000680 if (FileName == NULL) {
681 return (EFI_INVALID_PARAMETER);
682 }
683
jcarsey366f81a2011-06-27 21:04:22 +0000684 if (gEfiShellProtocol != NULL) {
jcarseya405b862010-09-14 05:18:09 +0000685 if ((OpenMode & EFI_FILE_MODE_CREATE) == EFI_FILE_MODE_CREATE && (Attributes & EFI_FILE_DIRECTORY) == EFI_FILE_DIRECTORY) {
686 return ShellCreateDirectory(FileName, FileHandle);
687 }
jcarsey94b17fa2009-05-07 18:46:18 +0000688 //
689 // Use UEFI Shell 2.0 method
690 //
jcarsey366f81a2011-06-27 21:04:22 +0000691 Status = gEfiShellProtocol->OpenFileByName(FileName,
jcarseyb1f95a02009-06-16 00:23:19 +0000692 FileHandle,
693 OpenMode);
jcarseya405b862010-09-14 05:18:09 +0000694 if (StrCmp(FileName, L"NUL") != 0 && !EFI_ERROR(Status) && ((OpenMode & EFI_FILE_MODE_CREATE) != 0)){
jcarsey2247dde2009-11-09 18:08:58 +0000695 FileInfo = FileFunctionMap.GetFileInfo(*FileHandle);
jcarseyb1f95a02009-06-16 00:23:19 +0000696 ASSERT(FileInfo != NULL);
697 FileInfo->Attribute = Attributes;
jcarsey2247dde2009-11-09 18:08:58 +0000698 Status = FileFunctionMap.SetFileInfo(*FileHandle, FileInfo);
699 FreePool(FileInfo);
jcarseyb1f95a02009-06-16 00:23:19 +0000700 }
701 return (Status);
jcarsey1e6e84c2010-01-25 20:05:08 +0000702 }
jcarsey94b17fa2009-05-07 18:46:18 +0000703 //
704 // Using EFI Shell version
705 // this means convert name to path and call that function
706 // since this will use EFI method again that will open it.
707 //
708 ASSERT(mEfiShellEnvironment2 != NULL);
jcarseyb82bfcc2009-06-29 16:28:23 +0000709 FilePath = mEfiShellEnvironment2->NameToPath ((CHAR16*)FileName);
xdu290bfa222010-07-19 05:21:27 +0000710 if (FilePath != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +0000711 return (ShellOpenFileByDevicePath(&FilePath,
712 &DeviceHandle,
713 FileHandle,
714 OpenMode,
jcarseya405b862010-09-14 05:18:09 +0000715 Attributes));
jcarsey94b17fa2009-05-07 18:46:18 +0000716 }
717 return (EFI_DEVICE_ERROR);
718}
719/**
720 This function create a directory
721
jcarsey1e6e84c2010-01-25 20:05:08 +0000722 If return is EFI_SUCCESS, the Filehandle is the opened directory's handle;
723 otherwise, the Filehandle is NULL. If the directory already existed, this
jcarsey94b17fa2009-05-07 18:46:18 +0000724 function opens the existing directory.
725
darylm503b0934ac2011-11-12 00:35:11 +0000726 @param DirectoryName pointer to directory name
727 @param FileHandle pointer to the file handle.
jcarsey94b17fa2009-05-07 18:46:18 +0000728
darylm503b0934ac2011-11-12 00:35:11 +0000729 @retval EFI_SUCCESS The information was set.
730 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
731 @retval EFI_UNSUPPORTED Could not open the file path.
732 @retval EFI_NOT_FOUND The specified file could not be found on the
jcarsey1e6e84c2010-01-25 20:05:08 +0000733 device or the file system could not be found
jcarsey94b17fa2009-05-07 18:46:18 +0000734 on the device.
darylm503b0934ac2011-11-12 00:35:11 +0000735 @retval EFI_NO_MEDIA The device has no medium.
736 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
jcarsey94b17fa2009-05-07 18:46:18 +0000737 medium is no longer supported.
darylm503b0934ac2011-11-12 00:35:11 +0000738 @retval EFI_DEVICE_ERROR The device reported an error.
739 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
740 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
741 @retval EFI_ACCESS_DENIED The file was opened read only.
742 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
jcarsey94b17fa2009-05-07 18:46:18 +0000743 file.
darylm503b0934ac2011-11-12 00:35:11 +0000744 @retval EFI_VOLUME_FULL The volume is full.
jcarsey94b17fa2009-05-07 18:46:18 +0000745 @sa ShellOpenFileByName
746**/
747EFI_STATUS
748EFIAPI
749ShellCreateDirectory(
jcarseyb82bfcc2009-06-29 16:28:23 +0000750 IN CONST CHAR16 *DirectoryName,
jcarseya405b862010-09-14 05:18:09 +0000751 OUT SHELL_FILE_HANDLE *FileHandle
752 )
753{
jcarsey366f81a2011-06-27 21:04:22 +0000754 if (gEfiShellProtocol != NULL) {
jcarsey2247dde2009-11-09 18:08:58 +0000755 //
756 // Use UEFI Shell 2.0 method
757 //
jcarsey366f81a2011-06-27 21:04:22 +0000758 return (gEfiShellProtocol->CreateFile(DirectoryName,
jcarsey2247dde2009-11-09 18:08:58 +0000759 EFI_FILE_DIRECTORY,
760 FileHandle
jcarseya405b862010-09-14 05:18:09 +0000761 ));
jcarsey2247dde2009-11-09 18:08:58 +0000762 } else {
763 return (ShellOpenFileByName(DirectoryName,
764 FileHandle,
765 EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE,
766 EFI_FILE_DIRECTORY
jcarseya405b862010-09-14 05:18:09 +0000767 ));
jcarsey2247dde2009-11-09 18:08:58 +0000768 }
jcarsey94b17fa2009-05-07 18:46:18 +0000769}
770
771/**
772 This function reads information from an opened file.
773
jcarsey1e6e84c2010-01-25 20:05:08 +0000774 If FileHandle is not a directory, the function reads the requested number of
775 bytes from the file at the file's current position and returns them in Buffer.
jcarsey94b17fa2009-05-07 18:46:18 +0000776 If the read goes beyond the end of the file, the read length is truncated to the
jcarsey1e6e84c2010-01-25 20:05:08 +0000777 end of the file. The file's current position is increased by the number of bytes
778 returned. If FileHandle is a directory, the function reads the directory entry
779 at the file's current position and returns the entry in Buffer. If the Buffer
780 is not large enough to hold the current directory entry, then
781 EFI_BUFFER_TOO_SMALL is returned and the current file position is not updated.
782 BufferSize is set to be the size of the buffer needed to read the entry. On
783 success, the current position is updated to the next directory entry. If there
784 are no more directory entries, the read returns a zero-length buffer.
jcarsey94b17fa2009-05-07 18:46:18 +0000785 EFI_FILE_INFO is the structure returned as the directory entry.
786
787 @param FileHandle the opened file handle
jcarsey1e6e84c2010-01-25 20:05:08 +0000788 @param BufferSize on input the size of buffer in bytes. on return
jcarsey94b17fa2009-05-07 18:46:18 +0000789 the number of bytes written.
790 @param Buffer the buffer to put read data into.
791
darylm503b0934ac2011-11-12 00:35:11 +0000792 @retval EFI_SUCCESS Data was read.
793 @retval EFI_NO_MEDIA The device has no media.
794 @retval EFI_DEVICE_ERROR The device reported an error.
795 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
796 @retval EFI_BUFFER_TO_SMALL Buffer is too small. ReadSize contains required
jcarsey94b17fa2009-05-07 18:46:18 +0000797 size.
798
799**/
800EFI_STATUS
801EFIAPI
802ShellReadFile(
jcarseya405b862010-09-14 05:18:09 +0000803 IN SHELL_FILE_HANDLE FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000804 IN OUT UINTN *BufferSize,
805 OUT VOID *Buffer
jcarseya405b862010-09-14 05:18:09 +0000806 )
807{
jcarseyd2b45642009-05-11 18:02:16 +0000808 return (FileFunctionMap.ReadFile(FileHandle, BufferSize, Buffer));
jcarsey94b17fa2009-05-07 18:46:18 +0000809}
810
811
812/**
813 Write data to a file.
814
jcarsey1e6e84c2010-01-25 20:05:08 +0000815 This function writes the specified number of bytes to the file at the current
816 file position. The current file position is advanced the actual number of bytes
817 written, which is returned in BufferSize. Partial writes only occur when there
818 has been a data error during the write attempt (such as "volume space full").
819 The file is automatically grown to hold the data if required. Direct writes to
jcarsey94b17fa2009-05-07 18:46:18 +0000820 opened directories are not supported.
821
822 @param FileHandle The opened file for writing
823 @param BufferSize on input the number of bytes in Buffer. On output
824 the number of bytes written.
825 @param Buffer the buffer containing data to write is stored.
826
darylm503b0934ac2011-11-12 00:35:11 +0000827 @retval EFI_SUCCESS Data was written.
828 @retval EFI_UNSUPPORTED Writes to an open directory are not supported.
829 @retval EFI_NO_MEDIA The device has no media.
830 @retval EFI_DEVICE_ERROR The device reported an error.
831 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
832 @retval EFI_WRITE_PROTECTED The device is write-protected.
833 @retval EFI_ACCESS_DENIED The file was open for read only.
834 @retval EFI_VOLUME_FULL The volume is full.
jcarsey94b17fa2009-05-07 18:46:18 +0000835**/
836EFI_STATUS
837EFIAPI
838ShellWriteFile(
jcarsey252d9452011-03-25 20:49:53 +0000839 IN SHELL_FILE_HANDLE FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000840 IN OUT UINTN *BufferSize,
841 IN VOID *Buffer
jcarseya405b862010-09-14 05:18:09 +0000842 )
843{
jcarseyd2b45642009-05-11 18:02:16 +0000844 return (FileFunctionMap.WriteFile(FileHandle, BufferSize, Buffer));
jcarsey94b17fa2009-05-07 18:46:18 +0000845}
846
jcarsey1e6e84c2010-01-25 20:05:08 +0000847/**
jcarsey94b17fa2009-05-07 18:46:18 +0000848 Close an open file handle.
849
jcarsey1e6e84c2010-01-25 20:05:08 +0000850 This function closes a specified file handle. All "dirty" cached file data is
851 flushed to the device, and the file is closed. In all cases the handle is
jcarsey94b17fa2009-05-07 18:46:18 +0000852 closed.
853
854@param FileHandle the file handle to close.
855
856@retval EFI_SUCCESS the file handle was closed sucessfully.
857**/
858EFI_STATUS
859EFIAPI
860ShellCloseFile (
jcarseya405b862010-09-14 05:18:09 +0000861 IN SHELL_FILE_HANDLE *FileHandle
862 )
863{
jcarseyd2b45642009-05-11 18:02:16 +0000864 return (FileFunctionMap.CloseFile(*FileHandle));
jcarsey94b17fa2009-05-07 18:46:18 +0000865}
866
867/**
868 Delete a file and close the handle
869
870 This function closes and deletes a file. In all cases the file handle is closed.
jcarsey1e6e84c2010-01-25 20:05:08 +0000871 If the file cannot be deleted, the warning code EFI_WARN_DELETE_FAILURE is
jcarsey94b17fa2009-05-07 18:46:18 +0000872 returned, but the handle is still closed.
873
874 @param FileHandle the file handle to delete
875
876 @retval EFI_SUCCESS the file was closed sucessfully
jcarsey1e6e84c2010-01-25 20:05:08 +0000877 @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not
jcarsey94b17fa2009-05-07 18:46:18 +0000878 deleted
darylm503b0934ac2011-11-12 00:35:11 +0000879 @retval INVALID_PARAMETER One of the parameters has an invalid value.
jcarsey94b17fa2009-05-07 18:46:18 +0000880**/
881EFI_STATUS
882EFIAPI
883ShellDeleteFile (
darylm503b0934ac2011-11-12 00:35:11 +0000884 IN SHELL_FILE_HANDLE *FileHandle
jcarseya405b862010-09-14 05:18:09 +0000885 )
886{
jcarseyd2b45642009-05-11 18:02:16 +0000887 return (FileFunctionMap.DeleteFile(*FileHandle));
jcarsey94b17fa2009-05-07 18:46:18 +0000888}
889
890/**
891 Set the current position in a file.
892
jcarsey1e6e84c2010-01-25 20:05:08 +0000893 This function sets the current file position for the handle to the position
jcarsey94b17fa2009-05-07 18:46:18 +0000894 supplied. With the exception of seeking to position 0xFFFFFFFFFFFFFFFF, only
jcarsey1e6e84c2010-01-25 20:05:08 +0000895 absolute positioning is supported, and seeking past the end of the file is
896 allowed (a subsequent write would grow the file). Seeking to position
jcarsey94b17fa2009-05-07 18:46:18 +0000897 0xFFFFFFFFFFFFFFFF causes the current position to be set to the end of the file.
jcarsey1e6e84c2010-01-25 20:05:08 +0000898 If FileHandle is a directory, the only position that may be set is zero. This
jcarsey94b17fa2009-05-07 18:46:18 +0000899 has the effect of starting the read process of the directory entries over.
900
901 @param FileHandle The file handle on which the position is being set
902 @param Position Byte position from begining of file
903
904 @retval EFI_SUCCESS Operation completed sucessfully.
jcarsey1e6e84c2010-01-25 20:05:08 +0000905 @retval EFI_UNSUPPORTED the seek request for non-zero is not valid on
jcarsey94b17fa2009-05-07 18:46:18 +0000906 directories.
907 @retval INVALID_PARAMETER One of the parameters has an invalid value.
908**/
909EFI_STATUS
910EFIAPI
911ShellSetFilePosition (
darylm503b0934ac2011-11-12 00:35:11 +0000912 IN SHELL_FILE_HANDLE FileHandle,
913 IN UINT64 Position
jcarseya405b862010-09-14 05:18:09 +0000914 )
915{
jcarseyd2b45642009-05-11 18:02:16 +0000916 return (FileFunctionMap.SetFilePosition(FileHandle, Position));
jcarsey94b17fa2009-05-07 18:46:18 +0000917}
918
jcarsey1e6e84c2010-01-25 20:05:08 +0000919/**
jcarsey94b17fa2009-05-07 18:46:18 +0000920 Gets a file's current position
921
jcarsey1e6e84c2010-01-25 20:05:08 +0000922 This function retrieves the current file position for the file handle. For
923 directories, the current file position has no meaning outside of the file
jcarsey94b17fa2009-05-07 18:46:18 +0000924 system driver and as such the operation is not supported. An error is returned
925 if FileHandle is a directory.
926
927 @param FileHandle The open file handle on which to get the position.
928 @param Position Byte position from begining of file.
929
930 @retval EFI_SUCCESS the operation completed sucessfully.
931 @retval INVALID_PARAMETER One of the parameters has an invalid value.
932 @retval EFI_UNSUPPORTED the request is not valid on directories.
933**/
934EFI_STATUS
935EFIAPI
936ShellGetFilePosition (
jcarseya405b862010-09-14 05:18:09 +0000937 IN SHELL_FILE_HANDLE FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000938 OUT UINT64 *Position
jcarseya405b862010-09-14 05:18:09 +0000939 )
940{
jcarseyd2b45642009-05-11 18:02:16 +0000941 return (FileFunctionMap.GetFilePosition(FileHandle, Position));
jcarsey94b17fa2009-05-07 18:46:18 +0000942}
943/**
944 Flushes data on a file
jcarsey1e6e84c2010-01-25 20:05:08 +0000945
jcarsey94b17fa2009-05-07 18:46:18 +0000946 This function flushes all modified data associated with a file to a device.
947
948 @param FileHandle The file handle on which to flush data
949
950 @retval EFI_SUCCESS The data was flushed.
951 @retval EFI_NO_MEDIA The device has no media.
952 @retval EFI_DEVICE_ERROR The device reported an error.
953 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
954 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
955 @retval EFI_ACCESS_DENIED The file was opened for read only.
956**/
957EFI_STATUS
958EFIAPI
959ShellFlushFile (
jcarseya405b862010-09-14 05:18:09 +0000960 IN SHELL_FILE_HANDLE FileHandle
961 )
962{
jcarseyd2b45642009-05-11 18:02:16 +0000963 return (FileFunctionMap.FlushFile(FileHandle));
jcarsey94b17fa2009-05-07 18:46:18 +0000964}
965
darylm503b0934ac2011-11-12 00:35:11 +0000966/** Retrieve first entry from a directory.
jcarsey94b17fa2009-05-07 18:46:18 +0000967
darylm503b0934ac2011-11-12 00:35:11 +0000968 This function takes an open directory handle and gets information from the
969 first entry in the directory. A buffer is allocated to contain
970 the information and a pointer to the buffer is returned in *Buffer. The
971 caller can use ShellFindNextFile() to get subsequent directory entries.
jcarsey94b17fa2009-05-07 18:46:18 +0000972
darylm503b0934ac2011-11-12 00:35:11 +0000973 The buffer will be freed by ShellFindNextFile() when the last directory
974 entry is read. Otherwise, the caller must free the buffer, using FreePool,
975 when finished with it.
976
977 @param[in] DirHandle The file handle of the directory to search.
978 @param[out] Buffer The pointer to the buffer for the file's information.
jcarsey94b17fa2009-05-07 18:46:18 +0000979
980 @retval EFI_SUCCESS Found the first file.
981 @retval EFI_NOT_FOUND Cannot find the directory.
982 @retval EFI_NO_MEDIA The device has no media.
983 @retval EFI_DEVICE_ERROR The device reported an error.
984 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
985 @return Others status of ShellGetFileInfo, ShellSetFilePosition,
986 or ShellReadFile
987**/
988EFI_STATUS
989EFIAPI
990ShellFindFirstFile (
jcarseya405b862010-09-14 05:18:09 +0000991 IN SHELL_FILE_HANDLE DirHandle,
jcarseyd2b45642009-05-11 18:02:16 +0000992 OUT EFI_FILE_INFO **Buffer
jcarseya405b862010-09-14 05:18:09 +0000993 )
994{
jcarsey94b17fa2009-05-07 18:46:18 +0000995 //
jcarseyd2b45642009-05-11 18:02:16 +0000996 // pass to file handle lib
jcarsey94b17fa2009-05-07 18:46:18 +0000997 //
jcarseyd2b45642009-05-11 18:02:16 +0000998 return (FileHandleFindFirstFile(DirHandle, Buffer));
jcarsey94b17fa2009-05-07 18:46:18 +0000999}
darylm503b0934ac2011-11-12 00:35:11 +00001000/** Retrieve next entries from a directory.
jcarsey94b17fa2009-05-07 18:46:18 +00001001
darylm503b0934ac2011-11-12 00:35:11 +00001002 To use this function, the caller must first call the ShellFindFirstFile()
1003 function to get the first directory entry. Subsequent directory entries are
1004 retrieved by using the ShellFindNextFile() function. This function can
1005 be called several times to get each entry from the directory. If the call of
1006 ShellFindNextFile() retrieved the last directory entry, the next call of
1007 this function will set *NoFile to TRUE and free the buffer.
jcarsey94b17fa2009-05-07 18:46:18 +00001008
darylm503b0934ac2011-11-12 00:35:11 +00001009 @param[in] DirHandle The file handle of the directory.
1010 @param[out] Buffer The pointer to buffer for file's information.
1011 @param[out] NoFile The pointer to boolean when last file is found.
jcarsey94b17fa2009-05-07 18:46:18 +00001012
1013 @retval EFI_SUCCESS Found the next file, or reached last file
1014 @retval EFI_NO_MEDIA The device has no media.
1015 @retval EFI_DEVICE_ERROR The device reported an error.
1016 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
1017**/
1018EFI_STATUS
1019EFIAPI
1020ShellFindNextFile(
jcarseya405b862010-09-14 05:18:09 +00001021 IN SHELL_FILE_HANDLE DirHandle,
jcarsey94b17fa2009-05-07 18:46:18 +00001022 OUT EFI_FILE_INFO *Buffer,
1023 OUT BOOLEAN *NoFile
jcarseya405b862010-09-14 05:18:09 +00001024 )
1025{
jcarsey94b17fa2009-05-07 18:46:18 +00001026 //
jcarseyd2b45642009-05-11 18:02:16 +00001027 // pass to file handle lib
jcarsey94b17fa2009-05-07 18:46:18 +00001028 //
jcarseyd2b45642009-05-11 18:02:16 +00001029 return (FileHandleFindNextFile(DirHandle, Buffer, NoFile));
jcarsey94b17fa2009-05-07 18:46:18 +00001030}
1031/**
1032 Retrieve the size of a file.
1033
1034 if FileHandle is NULL then ASSERT()
1035 if Size is NULL then ASSERT()
1036
jcarsey1e6e84c2010-01-25 20:05:08 +00001037 This function extracts the file size info from the FileHandle's EFI_FILE_INFO
jcarsey94b17fa2009-05-07 18:46:18 +00001038 data.
1039
1040 @param FileHandle file handle from which size is retrieved
1041 @param Size pointer to size
1042
1043 @retval EFI_SUCCESS operation was completed sucessfully
1044 @retval EFI_DEVICE_ERROR cannot access the file
1045**/
1046EFI_STATUS
1047EFIAPI
1048ShellGetFileSize (
jcarseya405b862010-09-14 05:18:09 +00001049 IN SHELL_FILE_HANDLE FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +00001050 OUT UINT64 *Size
jcarseya405b862010-09-14 05:18:09 +00001051 )
1052{
jcarseyd2b45642009-05-11 18:02:16 +00001053 return (FileFunctionMap.GetFileSize(FileHandle, Size));
jcarsey94b17fa2009-05-07 18:46:18 +00001054}
1055/**
1056 Retrieves the status of the break execution flag
1057
1058 this function is useful to check whether the application is being asked to halt by the shell.
1059
1060 @retval TRUE the execution break is enabled
1061 @retval FALSE the execution break is not enabled
1062**/
1063BOOLEAN
1064EFIAPI
1065ShellGetExecutionBreakFlag(
1066 VOID
1067 )
1068{
jcarsey1e6e84c2010-01-25 20:05:08 +00001069 //
jcarsey94b17fa2009-05-07 18:46:18 +00001070 // Check for UEFI Shell 2.0 protocols
1071 //
jcarsey366f81a2011-06-27 21:04:22 +00001072 if (gEfiShellProtocol != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00001073
1074 //
1075 // We are using UEFI Shell 2.0; see if the event has been triggered
1076 //
jcarsey366f81a2011-06-27 21:04:22 +00001077 if (gBS->CheckEvent(gEfiShellProtocol->ExecutionBreak) != EFI_SUCCESS) {
jcarsey94b17fa2009-05-07 18:46:18 +00001078 return (FALSE);
1079 }
1080 return (TRUE);
jcarsey1e6e84c2010-01-25 20:05:08 +00001081 }
jcarsey94b17fa2009-05-07 18:46:18 +00001082
1083 //
1084 // using EFI Shell; call the function to check
1085 //
jcarsey92a54472011-06-27 20:33:13 +00001086 if (mEfiShellEnvironment2 != NULL) {
1087 return (mEfiShellEnvironment2->GetExecutionBreak());
1088 }
1089
1090 return (FALSE);
jcarsey94b17fa2009-05-07 18:46:18 +00001091}
1092/**
1093 return the value of an environment variable
1094
jcarsey1e6e84c2010-01-25 20:05:08 +00001095 this function gets the value of the environment variable set by the
jcarsey94b17fa2009-05-07 18:46:18 +00001096 ShellSetEnvironmentVariable function
1097
1098 @param EnvKey The key name of the environment variable.
1099
1100 @retval NULL the named environment variable does not exist.
1101 @return != NULL pointer to the value of the environment variable
1102**/
1103CONST CHAR16*
1104EFIAPI
1105ShellGetEnvironmentVariable (
jcarsey9b3bf082009-06-23 21:15:07 +00001106 IN CONST CHAR16 *EnvKey
jcarsey94b17fa2009-05-07 18:46:18 +00001107 )
1108{
jcarsey1e6e84c2010-01-25 20:05:08 +00001109 //
jcarsey94b17fa2009-05-07 18:46:18 +00001110 // Check for UEFI Shell 2.0 protocols
1111 //
jcarsey366f81a2011-06-27 21:04:22 +00001112 if (gEfiShellProtocol != NULL) {
1113 return (gEfiShellProtocol->GetEnv(EnvKey));
jcarsey94b17fa2009-05-07 18:46:18 +00001114 }
1115
1116 //
jcarsey92a54472011-06-27 20:33:13 +00001117 // Check for EFI shell
jcarsey94b17fa2009-05-07 18:46:18 +00001118 //
jcarsey92a54472011-06-27 20:33:13 +00001119 if (mEfiShellEnvironment2 != NULL) {
1120 return (mEfiShellEnvironment2->GetEnv((CHAR16*)EnvKey));
1121 }
jcarsey94b17fa2009-05-07 18:46:18 +00001122
jcarsey92a54472011-06-27 20:33:13 +00001123 return NULL;
jcarsey94b17fa2009-05-07 18:46:18 +00001124}
1125/**
1126 set the value of an environment variable
1127
1128This function changes the current value of the specified environment variable. If the
1129environment variable exists and the Value is an empty string, then the environment
1130variable is deleted. If the environment variable exists and the Value is not an empty
1131string, then the value of the environment variable is changed. If the environment
1132variable does not exist and the Value is an empty string, there is no action. If the
1133environment variable does not exist and the Value is a non-empty string, then the
1134environment variable is created and assigned the specified value.
1135
1136 This is not supported pre-UEFI Shell 2.0.
1137
1138 @param EnvKey The key name of the environment variable.
1139 @param EnvVal The Value of the environment variable
1140 @param Volatile Indicates whether the variable is non-volatile (FALSE) or volatile (TRUE).
1141
1142 @retval EFI_SUCCESS the operation was completed sucessfully
1143 @retval EFI_UNSUPPORTED This operation is not allowed in pre UEFI 2.0 Shell environments
1144**/
1145EFI_STATUS
1146EFIAPI
1147ShellSetEnvironmentVariable (
1148 IN CONST CHAR16 *EnvKey,
1149 IN CONST CHAR16 *EnvVal,
1150 IN BOOLEAN Volatile
1151 )
1152{
jcarsey1e6e84c2010-01-25 20:05:08 +00001153 //
jcarsey94b17fa2009-05-07 18:46:18 +00001154 // Check for UEFI Shell 2.0 protocols
1155 //
jcarsey366f81a2011-06-27 21:04:22 +00001156 if (gEfiShellProtocol != NULL) {
1157 return (gEfiShellProtocol->SetEnv(EnvKey, EnvVal, Volatile));
jcarsey1e6e84c2010-01-25 20:05:08 +00001158 }
jcarsey94b17fa2009-05-07 18:46:18 +00001159
1160 //
1161 // This feature does not exist under EFI shell
1162 //
1163 return (EFI_UNSUPPORTED);
1164}
jcarseya405b862010-09-14 05:18:09 +00001165
jcarsey94b17fa2009-05-07 18:46:18 +00001166/**
jcarseya405b862010-09-14 05:18:09 +00001167 Cause the shell to parse and execute a command line.
jcarsey94b17fa2009-05-07 18:46:18 +00001168
1169 This function creates a nested instance of the shell and executes the specified
jcarseya405b862010-09-14 05:18:09 +00001170 command (CommandLine) with the specified environment (Environment). Upon return,
1171 the status code returned by the specified command is placed in StatusCode.
1172 If Environment is NULL, then the current environment is used and all changes made
1173 by the commands executed will be reflected in the current environment. If the
1174 Environment is non-NULL, then the changes made will be discarded.
1175 The CommandLine is executed from the current working directory on the current
1176 device.
jcarsey94b17fa2009-05-07 18:46:18 +00001177
Brendan Jackman3877d0f2014-01-24 22:31:07 +00001178 The EnvironmentVariables pararemeter is ignored in a pre-UEFI Shell 2.0
jcarseya405b862010-09-14 05:18:09 +00001179 environment. The values pointed to by the parameters will be unchanged by the
1180 ShellExecute() function. The Output parameter has no effect in a
1181 UEFI Shell 2.0 environment.
jcarsey94b17fa2009-05-07 18:46:18 +00001182
jcarseya405b862010-09-14 05:18:09 +00001183 @param[in] ParentHandle The parent image starting the operation.
1184 @param[in] CommandLine The pointer to a NULL terminated command line.
1185 @param[in] Output True to display debug output. False to hide it.
1186 @param[in] EnvironmentVariables Optional pointer to array of environment variables
1187 in the form "x=y". If NULL, the current set is used.
1188 @param[out] Status The status of the run command line.
jcarsey94b17fa2009-05-07 18:46:18 +00001189
jcarseya405b862010-09-14 05:18:09 +00001190 @retval EFI_SUCCESS The operation completed sucessfully. Status
1191 contains the status code returned.
1192 @retval EFI_INVALID_PARAMETER A parameter contains an invalid value.
1193 @retval EFI_OUT_OF_RESOURCES Out of resources.
1194 @retval EFI_UNSUPPORTED The operation is not allowed.
jcarsey94b17fa2009-05-07 18:46:18 +00001195**/
1196EFI_STATUS
1197EFIAPI
1198ShellExecute (
1199 IN EFI_HANDLE *ParentHandle,
1200 IN CHAR16 *CommandLine OPTIONAL,
1201 IN BOOLEAN Output OPTIONAL,
1202 IN CHAR16 **EnvironmentVariables OPTIONAL,
1203 OUT EFI_STATUS *Status OPTIONAL
1204 )
1205{
Brendan Jackman3877d0f2014-01-24 22:31:07 +00001206 EFI_STATUS CmdStatus;
jcarsey1e6e84c2010-01-25 20:05:08 +00001207 //
jcarsey94b17fa2009-05-07 18:46:18 +00001208 // Check for UEFI Shell 2.0 protocols
1209 //
jcarsey366f81a2011-06-27 21:04:22 +00001210 if (gEfiShellProtocol != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00001211 //
1212 // Call UEFI Shell 2.0 version (not using Output parameter)
1213 //
jcarsey366f81a2011-06-27 21:04:22 +00001214 return (gEfiShellProtocol->Execute(ParentHandle,
jcarsey94b17fa2009-05-07 18:46:18 +00001215 CommandLine,
1216 EnvironmentVariables,
1217 Status));
jcarsey1e6e84c2010-01-25 20:05:08 +00001218 }
jcarsey92a54472011-06-27 20:33:13 +00001219
jcarsey94b17fa2009-05-07 18:46:18 +00001220 //
jcarsey92a54472011-06-27 20:33:13 +00001221 // Check for EFI shell
jcarsey94b17fa2009-05-07 18:46:18 +00001222 //
jcarsey92a54472011-06-27 20:33:13 +00001223 if (mEfiShellEnvironment2 != NULL) {
1224 //
Brendan Jackman3877d0f2014-01-24 22:31:07 +00001225 // Call EFI Shell version.
jcarsey92a54472011-06-27 20:33:13 +00001226 // Due to oddity in the EFI shell we want to dereference the ParentHandle here
1227 //
Brendan Jackman3877d0f2014-01-24 22:31:07 +00001228 CmdStatus = (mEfiShellEnvironment2->Execute(*ParentHandle,
jcarsey92a54472011-06-27 20:33:13 +00001229 CommandLine,
1230 Output));
Brendan Jackman3877d0f2014-01-24 22:31:07 +00001231 //
1232 // No Status output parameter so just use the returned status
1233 //
1234 if (Status != NULL) {
1235 *Status = CmdStatus;
1236 }
1237 //
1238 // If there was an error, we can't tell if it was from the command or from
1239 // the Execute() function, so we'll just assume the shell ran successfully
1240 // and the error came from the command.
1241 //
1242 return EFI_SUCCESS;
jcarsey92a54472011-06-27 20:33:13 +00001243 }
1244
1245 return (EFI_UNSUPPORTED);
jcarsey94b17fa2009-05-07 18:46:18 +00001246}
Brendan Jackman3877d0f2014-01-24 22:31:07 +00001247
jcarsey94b17fa2009-05-07 18:46:18 +00001248/**
1249 Retreives the current directory path
1250
jcarsey1e6e84c2010-01-25 20:05:08 +00001251 If the DeviceName is NULL, it returns the current device's current directory
1252 name. If the DeviceName is not NULL, it returns the current directory name
jcarsey94b17fa2009-05-07 18:46:18 +00001253 on specified drive.
1254
1255 @param DeviceName the name of the drive to get directory on
1256
1257 @retval NULL the directory does not exist
1258 @return != NULL the directory
1259**/
1260CONST CHAR16*
1261EFIAPI
1262ShellGetCurrentDir (
jcarseya405b862010-09-14 05:18:09 +00001263 IN CHAR16 * CONST DeviceName OPTIONAL
jcarsey94b17fa2009-05-07 18:46:18 +00001264 )
1265{
jcarsey1e6e84c2010-01-25 20:05:08 +00001266 //
jcarsey94b17fa2009-05-07 18:46:18 +00001267 // Check for UEFI Shell 2.0 protocols
1268 //
jcarsey366f81a2011-06-27 21:04:22 +00001269 if (gEfiShellProtocol != NULL) {
1270 return (gEfiShellProtocol->GetCurDir(DeviceName));
jcarsey1e6e84c2010-01-25 20:05:08 +00001271 }
jcarsey92a54472011-06-27 20:33:13 +00001272
jcarsey94b17fa2009-05-07 18:46:18 +00001273 //
jcarsey8bd282b2011-06-27 16:45:41 +00001274 // Check for EFI shell
jcarsey94b17fa2009-05-07 18:46:18 +00001275 //
jcarsey8bd282b2011-06-27 16:45:41 +00001276 if (mEfiShellEnvironment2 != NULL) {
1277 return (mEfiShellEnvironment2->CurDir(DeviceName));
1278 }
1279
1280 return (NULL);
jcarsey94b17fa2009-05-07 18:46:18 +00001281}
1282/**
1283 sets (enabled or disabled) the page break mode
1284
jcarsey1e6e84c2010-01-25 20:05:08 +00001285 when page break mode is enabled the screen will stop scrolling
jcarsey94b17fa2009-05-07 18:46:18 +00001286 and wait for operator input before scrolling a subsequent screen.
1287
1288 @param CurrentState TRUE to enable and FALSE to disable
1289**/
jcarsey1e6e84c2010-01-25 20:05:08 +00001290VOID
jcarsey94b17fa2009-05-07 18:46:18 +00001291EFIAPI
1292ShellSetPageBreakMode (
1293 IN BOOLEAN CurrentState
1294 )
1295{
1296 //
1297 // check for enabling
1298 //
1299 if (CurrentState != 0x00) {
jcarsey1e6e84c2010-01-25 20:05:08 +00001300 //
jcarsey94b17fa2009-05-07 18:46:18 +00001301 // check for UEFI Shell 2.0
1302 //
jcarsey366f81a2011-06-27 21:04:22 +00001303 if (gEfiShellProtocol != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00001304 //
1305 // Enable with UEFI 2.0 Shell
1306 //
jcarsey366f81a2011-06-27 21:04:22 +00001307 gEfiShellProtocol->EnablePageBreak();
jcarsey94b17fa2009-05-07 18:46:18 +00001308 return;
1309 } else {
jcarsey1e6e84c2010-01-25 20:05:08 +00001310 //
jcarsey92a54472011-06-27 20:33:13 +00001311 // Check for EFI shell
jcarsey94b17fa2009-05-07 18:46:18 +00001312 //
jcarsey92a54472011-06-27 20:33:13 +00001313 if (mEfiShellEnvironment2 != NULL) {
1314 //
1315 // Enable with EFI Shell
1316 //
1317 mEfiShellEnvironment2->EnablePageBreak (DEFAULT_INIT_ROW, DEFAULT_AUTO_LF);
1318 return;
1319 }
jcarsey94b17fa2009-05-07 18:46:18 +00001320 }
1321 } else {
jcarsey1e6e84c2010-01-25 20:05:08 +00001322 //
jcarsey94b17fa2009-05-07 18:46:18 +00001323 // check for UEFI Shell 2.0
1324 //
jcarsey366f81a2011-06-27 21:04:22 +00001325 if (gEfiShellProtocol != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00001326 //
1327 // Disable with UEFI 2.0 Shell
1328 //
jcarsey366f81a2011-06-27 21:04:22 +00001329 gEfiShellProtocol->DisablePageBreak();
jcarsey94b17fa2009-05-07 18:46:18 +00001330 return;
1331 } else {
jcarsey1e6e84c2010-01-25 20:05:08 +00001332 //
jcarsey92a54472011-06-27 20:33:13 +00001333 // Check for EFI shell
jcarsey94b17fa2009-05-07 18:46:18 +00001334 //
jcarsey92a54472011-06-27 20:33:13 +00001335 if (mEfiShellEnvironment2 != NULL) {
1336 //
1337 // Disable with EFI Shell
1338 //
1339 mEfiShellEnvironment2->DisablePageBreak ();
1340 return;
1341 }
jcarsey94b17fa2009-05-07 18:46:18 +00001342 }
1343 }
1344}
1345
1346///
1347/// version of EFI_SHELL_FILE_INFO struct, except has no CONST pointers.
1348/// This allows for the struct to be populated.
1349///
1350typedef struct {
jcarseyd2b45642009-05-11 18:02:16 +00001351 LIST_ENTRY Link;
jcarsey94b17fa2009-05-07 18:46:18 +00001352 EFI_STATUS Status;
1353 CHAR16 *FullName;
1354 CHAR16 *FileName;
jcarseya405b862010-09-14 05:18:09 +00001355 SHELL_FILE_HANDLE Handle;
jcarsey94b17fa2009-05-07 18:46:18 +00001356 EFI_FILE_INFO *Info;
1357} EFI_SHELL_FILE_INFO_NO_CONST;
1358
1359/**
1360 Converts a EFI shell list of structures to the coresponding UEFI Shell 2.0 type of list.
1361
1362 if OldStyleFileList is NULL then ASSERT()
1363
jcarsey1e6e84c2010-01-25 20:05:08 +00001364 this function will convert a SHELL_FILE_ARG based list into a callee allocated
jcarsey94b17fa2009-05-07 18:46:18 +00001365 EFI_SHELL_FILE_INFO based list. it is up to the caller to free the memory via
1366 the ShellCloseFileMetaArg function.
1367
ydong104ff7e372011-09-02 08:05:34 +00001368 @param[in] FileList the EFI shell list type
1369 @param[in, out] ListHead the list to add to
jcarsey94b17fa2009-05-07 18:46:18 +00001370
1371 @retval the resultant head of the double linked new format list;
1372**/
1373LIST_ENTRY*
1374EFIAPI
1375InternalShellConvertFileListType (
jcarsey9b3bf082009-06-23 21:15:07 +00001376 IN LIST_ENTRY *FileList,
1377 IN OUT LIST_ENTRY *ListHead
jcarsey125c2cf2009-11-18 21:36:50 +00001378 )
1379{
jcarsey94b17fa2009-05-07 18:46:18 +00001380 SHELL_FILE_ARG *OldInfo;
jcarsey9b3bf082009-06-23 21:15:07 +00001381 LIST_ENTRY *Link;
jcarsey94b17fa2009-05-07 18:46:18 +00001382 EFI_SHELL_FILE_INFO_NO_CONST *NewInfo;
1383
1384 //
jcarsey9b3bf082009-06-23 21:15:07 +00001385 // ASSERTs
jcarsey94b17fa2009-05-07 18:46:18 +00001386 //
jcarsey9b3bf082009-06-23 21:15:07 +00001387 ASSERT(FileList != NULL);
1388 ASSERT(ListHead != NULL);
jcarsey94b17fa2009-05-07 18:46:18 +00001389
1390 //
1391 // enumerate through each member of the old list and copy
1392 //
jcarseyd2b45642009-05-11 18:02:16 +00001393 for (Link = FileList->ForwardLink; Link != FileList; Link = Link->ForwardLink) {
jcarsey94b17fa2009-05-07 18:46:18 +00001394 OldInfo = CR (Link, SHELL_FILE_ARG, Link, SHELL_FILE_ARG_SIGNATURE);
jcarseya405b862010-09-14 05:18:09 +00001395 ASSERT(OldInfo != NULL);
1396
1397 //
1398 // Skip ones that failed to open...
1399 //
1400 if (OldInfo->Status != EFI_SUCCESS) {
1401 continue;
1402 }
jcarsey94b17fa2009-05-07 18:46:18 +00001403
1404 //
1405 // make sure the old list was valid
1406 //
jcarsey94b17fa2009-05-07 18:46:18 +00001407 ASSERT(OldInfo->Info != NULL);
1408 ASSERT(OldInfo->FullName != NULL);
1409 ASSERT(OldInfo->FileName != NULL);
1410
1411 //
1412 // allocate a new EFI_SHELL_FILE_INFO object
1413 //
1414 NewInfo = AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));
jcarseyc9d92df2010-02-03 15:37:54 +00001415 if (NewInfo == NULL) {
jcarsey7a95efd2011-10-13 16:08:18 +00001416 ShellCloseFileMetaArg((EFI_SHELL_FILE_INFO**)(&ListHead));
jcarseybeab0fc2011-10-10 17:26:25 +00001417 ListHead = NULL;
jcarseyc9d92df2010-02-03 15:37:54 +00001418 break;
1419 }
jcarsey1e6e84c2010-01-25 20:05:08 +00001420
1421 //
jcarsey94b17fa2009-05-07 18:46:18 +00001422 // copy the simple items
1423 //
1424 NewInfo->Handle = OldInfo->Handle;
1425 NewInfo->Status = OldInfo->Status;
1426
jcarseyd2b45642009-05-11 18:02:16 +00001427 // old shell checks for 0 not NULL
1428 OldInfo->Handle = 0;
1429
jcarsey94b17fa2009-05-07 18:46:18 +00001430 //
1431 // allocate new space to copy strings and structure
1432 //
1433 NewInfo->FullName = AllocateZeroPool(StrSize(OldInfo->FullName));
1434 NewInfo->FileName = AllocateZeroPool(StrSize(OldInfo->FileName));
1435 NewInfo->Info = AllocateZeroPool((UINTN)OldInfo->Info->Size);
jcarsey1e6e84c2010-01-25 20:05:08 +00001436
jcarsey94b17fa2009-05-07 18:46:18 +00001437 //
1438 // make sure all the memory allocations were sucessful
1439 //
jcarseybeab0fc2011-10-10 17:26:25 +00001440 if (NULL == NewInfo->FullName || NewInfo->FileName == NULL || NewInfo->Info == NULL) {
jcarsey7a95efd2011-10-13 16:08:18 +00001441 ShellCloseFileMetaArg((EFI_SHELL_FILE_INFO**)(&ListHead));
jcarseybeab0fc2011-10-10 17:26:25 +00001442 ListHead = NULL;
1443 break;
1444 }
jcarsey94b17fa2009-05-07 18:46:18 +00001445
1446 //
1447 // Copt the strings and structure
1448 //
1449 StrCpy(NewInfo->FullName, OldInfo->FullName);
1450 StrCpy(NewInfo->FileName, OldInfo->FileName);
1451 gBS->CopyMem (NewInfo->Info, OldInfo->Info, (UINTN)OldInfo->Info->Size);
1452
1453 //
1454 // add that to the list
1455 //
jcarsey9b3bf082009-06-23 21:15:07 +00001456 InsertTailList(ListHead, &NewInfo->Link);
jcarsey94b17fa2009-05-07 18:46:18 +00001457 }
1458 return (ListHead);
1459}
1460/**
1461 Opens a group of files based on a path.
1462
jcarsey1e6e84c2010-01-25 20:05:08 +00001463 This function uses the Arg to open all the matching files. Each matched
Brendan Jackman70879312014-01-24 22:29:53 +00001464 file has a SHELL_FILE_INFO structure to record the file information. These
1465 structures are placed on the list ListHead. Users can get the SHELL_FILE_INFO
jcarsey94b17fa2009-05-07 18:46:18 +00001466 structures from ListHead to access each file. This function supports wildcards
jcarsey1e6e84c2010-01-25 20:05:08 +00001467 and will process '?' and '*' as such. the list must be freed with a call to
jcarsey94b17fa2009-05-07 18:46:18 +00001468 ShellCloseFileMetaArg().
1469
jcarsey1e6e84c2010-01-25 20:05:08 +00001470 If you are NOT appending to an existing list *ListHead must be NULL. If
jcarsey5f7431d2009-07-10 18:06:01 +00001471 *ListHead is NULL then it must be callee freed.
jcarsey94b17fa2009-05-07 18:46:18 +00001472
1473 @param Arg pointer to path string
1474 @param OpenMode mode to open files with
1475 @param ListHead head of linked list of results
1476
jcarsey1e6e84c2010-01-25 20:05:08 +00001477 @retval EFI_SUCCESS the operation was sucessful and the list head
jcarsey94b17fa2009-05-07 18:46:18 +00001478 contains the list of opened files
jcarsey94b17fa2009-05-07 18:46:18 +00001479 @return != EFI_SUCCESS the operation failed
1480
1481 @sa InternalShellConvertFileListType
1482**/
1483EFI_STATUS
1484EFIAPI
1485ShellOpenFileMetaArg (
1486 IN CHAR16 *Arg,
1487 IN UINT64 OpenMode,
1488 IN OUT EFI_SHELL_FILE_INFO **ListHead
1489 )
1490{
1491 EFI_STATUS Status;
jcarsey9b3bf082009-06-23 21:15:07 +00001492 LIST_ENTRY mOldStyleFileList;
jcarsey1e6e84c2010-01-25 20:05:08 +00001493
jcarsey94b17fa2009-05-07 18:46:18 +00001494 //
1495 // ASSERT that Arg and ListHead are not NULL
1496 //
1497 ASSERT(Arg != NULL);
1498 ASSERT(ListHead != NULL);
1499
jcarsey1e6e84c2010-01-25 20:05:08 +00001500 //
jcarsey94b17fa2009-05-07 18:46:18 +00001501 // Check for UEFI Shell 2.0 protocols
1502 //
jcarsey366f81a2011-06-27 21:04:22 +00001503 if (gEfiShellProtocol != NULL) {
jcarsey5f7431d2009-07-10 18:06:01 +00001504 if (*ListHead == NULL) {
1505 *ListHead = (EFI_SHELL_FILE_INFO*)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));
1506 if (*ListHead == NULL) {
1507 return (EFI_OUT_OF_RESOURCES);
1508 }
1509 InitializeListHead(&((*ListHead)->Link));
jcarsey1e6e84c2010-01-25 20:05:08 +00001510 }
jcarsey366f81a2011-06-27 21:04:22 +00001511 Status = gEfiShellProtocol->OpenFileList(Arg,
jcarsey1e6e84c2010-01-25 20:05:08 +00001512 OpenMode,
jcarsey2247dde2009-11-09 18:08:58 +00001513 ListHead);
1514 if (EFI_ERROR(Status)) {
jcarsey366f81a2011-06-27 21:04:22 +00001515 gEfiShellProtocol->RemoveDupInFileList(ListHead);
jcarsey2247dde2009-11-09 18:08:58 +00001516 } else {
jcarsey366f81a2011-06-27 21:04:22 +00001517 Status = gEfiShellProtocol->RemoveDupInFileList(ListHead);
jcarsey2247dde2009-11-09 18:08:58 +00001518 }
jcarseya405b862010-09-14 05:18:09 +00001519 if (*ListHead != NULL && IsListEmpty(&(*ListHead)->Link)) {
1520 FreePool(*ListHead);
1521 *ListHead = NULL;
1522 return (EFI_NOT_FOUND);
1523 }
jcarsey2247dde2009-11-09 18:08:58 +00001524 return (Status);
jcarsey1e6e84c2010-01-25 20:05:08 +00001525 }
jcarsey94b17fa2009-05-07 18:46:18 +00001526
1527 //
jcarsey92a54472011-06-27 20:33:13 +00001528 // Check for EFI shell
jcarsey94b17fa2009-05-07 18:46:18 +00001529 //
jcarsey92a54472011-06-27 20:33:13 +00001530 if (mEfiShellEnvironment2 != NULL) {
1531 //
1532 // make sure the list head is initialized
1533 //
1534 InitializeListHead(&mOldStyleFileList);
jcarsey94b17fa2009-05-07 18:46:18 +00001535
jcarsey92a54472011-06-27 20:33:13 +00001536 //
1537 // Get the EFI Shell list of files
1538 //
1539 Status = mEfiShellEnvironment2->FileMetaArg(Arg, &mOldStyleFileList);
1540 if (EFI_ERROR(Status)) {
1541 *ListHead = NULL;
1542 return (Status);
1543 }
jcarsey94b17fa2009-05-07 18:46:18 +00001544
jcarsey92a54472011-06-27 20:33:13 +00001545 if (*ListHead == NULL) {
1546 *ListHead = (EFI_SHELL_FILE_INFO *)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));
1547 if (*ListHead == NULL) {
1548 return (EFI_OUT_OF_RESOURCES);
1549 }
1550 InitializeListHead(&((*ListHead)->Link));
1551 }
1552
1553 //
1554 // Convert that to equivalent of UEFI Shell 2.0 structure
1555 //
1556 InternalShellConvertFileListType(&mOldStyleFileList, &(*ListHead)->Link);
1557
1558 //
1559 // Free the EFI Shell version that was converted.
1560 //
1561 mEfiShellEnvironment2->FreeFileList(&mOldStyleFileList);
1562
1563 if ((*ListHead)->Link.ForwardLink == (*ListHead)->Link.BackLink && (*ListHead)->Link.BackLink == &((*ListHead)->Link)) {
1564 FreePool(*ListHead);
1565 *ListHead = NULL;
1566 Status = EFI_NOT_FOUND;
1567 }
jcarsey94b17fa2009-05-07 18:46:18 +00001568 return (Status);
1569 }
1570
jcarsey92a54472011-06-27 20:33:13 +00001571 return (EFI_UNSUPPORTED);
jcarsey94b17fa2009-05-07 18:46:18 +00001572}
1573/**
jcarseya405b862010-09-14 05:18:09 +00001574 Free the linked list returned from ShellOpenFileMetaArg.
jcarsey94b17fa2009-05-07 18:46:18 +00001575
jcarseya405b862010-09-14 05:18:09 +00001576 if ListHead is NULL then ASSERT().
jcarsey94b17fa2009-05-07 18:46:18 +00001577
jcarseya405b862010-09-14 05:18:09 +00001578 @param ListHead the pointer to free.
jcarsey94b17fa2009-05-07 18:46:18 +00001579
jcarseya405b862010-09-14 05:18:09 +00001580 @retval EFI_SUCCESS the operation was sucessful.
jcarsey94b17fa2009-05-07 18:46:18 +00001581**/
1582EFI_STATUS
1583EFIAPI
1584ShellCloseFileMetaArg (
1585 IN OUT EFI_SHELL_FILE_INFO **ListHead
1586 )
1587{
1588 LIST_ENTRY *Node;
1589
1590 //
1591 // ASSERT that ListHead is not NULL
1592 //
1593 ASSERT(ListHead != NULL);
1594
jcarsey1e6e84c2010-01-25 20:05:08 +00001595 //
jcarsey94b17fa2009-05-07 18:46:18 +00001596 // Check for UEFI Shell 2.0 protocols
1597 //
jcarsey366f81a2011-06-27 21:04:22 +00001598 if (gEfiShellProtocol != NULL) {
1599 return (gEfiShellProtocol->FreeFileList(ListHead));
jcarsey92a54472011-06-27 20:33:13 +00001600 } else if (mEfiShellEnvironment2 != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00001601 //
jcarsey1e6e84c2010-01-25 20:05:08 +00001602 // Since this is EFI Shell version we need to free our internally made copy
jcarsey94b17fa2009-05-07 18:46:18 +00001603 // of the list
1604 //
jcarsey1e6e84c2010-01-25 20:05:08 +00001605 for ( Node = GetFirstNode(&(*ListHead)->Link)
jcarseya405b862010-09-14 05:18:09 +00001606 ; *ListHead != NULL && !IsListEmpty(&(*ListHead)->Link)
jcarsey9b3bf082009-06-23 21:15:07 +00001607 ; Node = GetFirstNode(&(*ListHead)->Link)) {
jcarsey94b17fa2009-05-07 18:46:18 +00001608 RemoveEntryList(Node);
jcarseya405b862010-09-14 05:18:09 +00001609 ((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 +00001610 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->FullName);
1611 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->FileName);
1612 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->Info);
1613 FreePool((EFI_SHELL_FILE_INFO_NO_CONST*)Node);
1614 }
Jaben Carsey75a5e2e2014-01-10 16:42:45 +00001615 SHELL_FREE_NON_NULL(*ListHead);
jcarsey94b17fa2009-05-07 18:46:18 +00001616 return EFI_SUCCESS;
1617 }
jcarsey92a54472011-06-27 20:33:13 +00001618
1619 return (EFI_UNSUPPORTED);
jcarsey94b17fa2009-05-07 18:46:18 +00001620}
1621
jcarsey125c2cf2009-11-18 21:36:50 +00001622/**
1623 Find a file by searching the CWD and then the path.
1624
jcarseyb3011f42010-01-11 21:49:04 +00001625 If FileName is NULL then ASSERT.
jcarsey125c2cf2009-11-18 21:36:50 +00001626
jcarseyb3011f42010-01-11 21:49:04 +00001627 If the return value is not NULL then the memory must be caller freed.
jcarsey125c2cf2009-11-18 21:36:50 +00001628
1629 @param FileName Filename string.
1630
1631 @retval NULL the file was not found
1632 @return !NULL the full path to the file.
1633**/
1634CHAR16 *
1635EFIAPI
1636ShellFindFilePath (
1637 IN CONST CHAR16 *FileName
1638 )
1639{
1640 CONST CHAR16 *Path;
jcarseya405b862010-09-14 05:18:09 +00001641 SHELL_FILE_HANDLE Handle;
jcarsey125c2cf2009-11-18 21:36:50 +00001642 EFI_STATUS Status;
1643 CHAR16 *RetVal;
1644 CHAR16 *TestPath;
1645 CONST CHAR16 *Walker;
jcarsey36a9d672009-11-20 21:13:41 +00001646 UINTN Size;
jcarsey1cd45e72010-01-29 15:07:44 +00001647 CHAR16 *TempChar;
jcarsey125c2cf2009-11-18 21:36:50 +00001648
1649 RetVal = NULL;
1650
jcarseya405b862010-09-14 05:18:09 +00001651 //
1652 // First make sure its not an absolute path.
1653 //
1654 Status = ShellOpenFileByName(FileName, &Handle, EFI_FILE_MODE_READ, 0);
1655 if (!EFI_ERROR(Status)){
1656 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {
1657 ASSERT(RetVal == NULL);
1658 RetVal = StrnCatGrow(&RetVal, NULL, FileName, 0);
1659 ShellCloseFile(&Handle);
1660 return (RetVal);
1661 } else {
1662 ShellCloseFile(&Handle);
1663 }
1664 }
1665
jcarsey125c2cf2009-11-18 21:36:50 +00001666 Path = ShellGetEnvironmentVariable(L"cwd");
1667 if (Path != NULL) {
jcarsey36a9d672009-11-20 21:13:41 +00001668 Size = StrSize(Path);
1669 Size += StrSize(FileName);
1670 TestPath = AllocateZeroPool(Size);
jcarseyc9d92df2010-02-03 15:37:54 +00001671 if (TestPath == NULL) {
1672 return (NULL);
1673 }
jcarsey125c2cf2009-11-18 21:36:50 +00001674 StrCpy(TestPath, Path);
1675 StrCat(TestPath, FileName);
1676 Status = ShellOpenFileByName(TestPath, &Handle, EFI_FILE_MODE_READ, 0);
1677 if (!EFI_ERROR(Status)){
jcarseya405b862010-09-14 05:18:09 +00001678 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {
1679 ASSERT(RetVal == NULL);
1680 RetVal = StrnCatGrow(&RetVal, NULL, TestPath, 0);
1681 ShellCloseFile(&Handle);
1682 FreePool(TestPath);
1683 return (RetVal);
1684 } else {
1685 ShellCloseFile(&Handle);
1686 }
jcarsey125c2cf2009-11-18 21:36:50 +00001687 }
1688 FreePool(TestPath);
1689 }
1690 Path = ShellGetEnvironmentVariable(L"path");
1691 if (Path != NULL) {
jcarseya405b862010-09-14 05:18:09 +00001692 Size = StrSize(Path)+sizeof(CHAR16);
jcarsey36a9d672009-11-20 21:13:41 +00001693 Size += StrSize(FileName);
1694 TestPath = AllocateZeroPool(Size);
jcarsey3e082d52010-10-04 16:44:57 +00001695 if (TestPath == NULL) {
1696 return (NULL);
1697 }
jcarsey1e6e84c2010-01-25 20:05:08 +00001698 Walker = (CHAR16*)Path;
jcarsey125c2cf2009-11-18 21:36:50 +00001699 do {
1700 CopyMem(TestPath, Walker, StrSize(Walker));
jcarsey3e082d52010-10-04 16:44:57 +00001701 if (TestPath != NULL) {
1702 TempChar = StrStr(TestPath, L";");
1703 if (TempChar != NULL) {
1704 *TempChar = CHAR_NULL;
1705 }
1706 if (TestPath[StrLen(TestPath)-1] != L'\\') {
1707 StrCat(TestPath, L"\\");
1708 }
jcarsey89e85372011-04-13 23:37:50 +00001709 if (FileName[0] == L'\\') {
1710 FileName++;
1711 }
jcarsey3e082d52010-10-04 16:44:57 +00001712 StrCat(TestPath, FileName);
1713 if (StrStr(Walker, L";") != NULL) {
1714 Walker = StrStr(Walker, L";") + 1;
jcarseya405b862010-09-14 05:18:09 +00001715 } else {
jcarsey3e082d52010-10-04 16:44:57 +00001716 Walker = NULL;
1717 }
1718 Status = ShellOpenFileByName(TestPath, &Handle, EFI_FILE_MODE_READ, 0);
1719 if (!EFI_ERROR(Status)){
1720 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {
1721 ASSERT(RetVal == NULL);
1722 RetVal = StrnCatGrow(&RetVal, NULL, TestPath, 0);
1723 ShellCloseFile(&Handle);
1724 break;
1725 } else {
1726 ShellCloseFile(&Handle);
1727 }
jcarseya405b862010-09-14 05:18:09 +00001728 }
jcarsey125c2cf2009-11-18 21:36:50 +00001729 }
1730 } while (Walker != NULL && Walker[0] != CHAR_NULL);
1731 FreePool(TestPath);
1732 }
1733 return (RetVal);
1734}
1735
jcarseyb3011f42010-01-11 21:49:04 +00001736/**
jcarsey1e6e84c2010-01-25 20:05:08 +00001737 Find a file by searching the CWD and then the path with a variable set of file
1738 extensions. If the file is not found it will append each extension in the list
jcarseyb3011f42010-01-11 21:49:04 +00001739 in the order provided and return the first one that is successful.
1740
1741 If FileName is NULL, then ASSERT.
1742 If FileExtension is NULL, then behavior is identical to ShellFindFilePath.
1743
1744 If the return value is not NULL then the memory must be caller freed.
1745
1746 @param[in] FileName Filename string.
1747 @param[in] FileExtension Semi-colon delimeted list of possible extensions.
1748
1749 @retval NULL The file was not found.
1750 @retval !NULL The path to the file.
1751**/
1752CHAR16 *
1753EFIAPI
1754ShellFindFilePathEx (
1755 IN CONST CHAR16 *FileName,
1756 IN CONST CHAR16 *FileExtension
1757 )
1758{
1759 CHAR16 *TestPath;
1760 CHAR16 *RetVal;
1761 CONST CHAR16 *ExtensionWalker;
jcarsey9e926b62010-01-14 20:26:39 +00001762 UINTN Size;
jcarsey1cd45e72010-01-29 15:07:44 +00001763 CHAR16 *TempChar;
jcarseyc9d92df2010-02-03 15:37:54 +00001764 CHAR16 *TempChar2;
jcarsey1cd45e72010-01-29 15:07:44 +00001765
jcarseyb3011f42010-01-11 21:49:04 +00001766 ASSERT(FileName != NULL);
1767 if (FileExtension == NULL) {
1768 return (ShellFindFilePath(FileName));
1769 }
1770 RetVal = ShellFindFilePath(FileName);
1771 if (RetVal != NULL) {
1772 return (RetVal);
1773 }
jcarsey9e926b62010-01-14 20:26:39 +00001774 Size = StrSize(FileName);
1775 Size += StrSize(FileExtension);
1776 TestPath = AllocateZeroPool(Size);
jcarseyc9d92df2010-02-03 15:37:54 +00001777 if (TestPath == NULL) {
1778 return (NULL);
1779 }
jcarseya405b862010-09-14 05:18:09 +00001780 for (ExtensionWalker = FileExtension, TempChar2 = (CHAR16*)FileExtension; TempChar2 != NULL ; ExtensionWalker = TempChar2 + 1){
jcarseyb3011f42010-01-11 21:49:04 +00001781 StrCpy(TestPath, FileName);
jcarseya405b862010-09-14 05:18:09 +00001782 if (ExtensionWalker != NULL) {
1783 StrCat(TestPath, ExtensionWalker);
1784 }
jcarsey1cd45e72010-01-29 15:07:44 +00001785 TempChar = StrStr(TestPath, L";");
1786 if (TempChar != NULL) {
1787 *TempChar = CHAR_NULL;
jcarseyb3011f42010-01-11 21:49:04 +00001788 }
1789 RetVal = ShellFindFilePath(TestPath);
1790 if (RetVal != NULL) {
1791 break;
1792 }
jcarseya405b862010-09-14 05:18:09 +00001793 ASSERT(ExtensionWalker != NULL);
jcarseyc9d92df2010-02-03 15:37:54 +00001794 TempChar2 = StrStr(ExtensionWalker, L";");
jcarseyb3011f42010-01-11 21:49:04 +00001795 }
1796 FreePool(TestPath);
1797 return (RetVal);
1798}
1799
jcarsey94b17fa2009-05-07 18:46:18 +00001800typedef struct {
jcarsey9b3bf082009-06-23 21:15:07 +00001801 LIST_ENTRY Link;
jcarsey94b17fa2009-05-07 18:46:18 +00001802 CHAR16 *Name;
jcarseya405b862010-09-14 05:18:09 +00001803 SHELL_PARAM_TYPE Type;
jcarsey94b17fa2009-05-07 18:46:18 +00001804 CHAR16 *Value;
1805 UINTN OriginalPosition;
1806} SHELL_PARAM_PACKAGE;
1807
1808/**
jcarsey1e6e84c2010-01-25 20:05:08 +00001809 Checks the list of valid arguments and returns TRUE if the item was found. If the
jcarsey94b17fa2009-05-07 18:46:18 +00001810 return value is TRUE then the type parameter is set also.
jcarsey1e6e84c2010-01-25 20:05:08 +00001811
jcarsey94b17fa2009-05-07 18:46:18 +00001812 if CheckList is NULL then ASSERT();
1813 if Name is NULL then ASSERT();
1814 if Type is NULL then ASSERT();
1815
jcarsey94b17fa2009-05-07 18:46:18 +00001816 @param Name pointer to Name of parameter found
1817 @param CheckList List to check against
jcarseya405b862010-09-14 05:18:09 +00001818 @param Type pointer to type of parameter if it was found
jcarsey94b17fa2009-05-07 18:46:18 +00001819
1820 @retval TRUE the Parameter was found. Type is valid.
1821 @retval FALSE the Parameter was not found. Type is not valid.
1822**/
1823BOOLEAN
1824EFIAPI
jcarseyd2b45642009-05-11 18:02:16 +00001825InternalIsOnCheckList (
jcarsey94b17fa2009-05-07 18:46:18 +00001826 IN CONST CHAR16 *Name,
1827 IN CONST SHELL_PARAM_ITEM *CheckList,
jcarsey252d9452011-03-25 20:49:53 +00001828 OUT SHELL_PARAM_TYPE *Type
jcarseya405b862010-09-14 05:18:09 +00001829 )
1830{
jcarsey94b17fa2009-05-07 18:46:18 +00001831 SHELL_PARAM_ITEM *TempListItem;
jcarsey252d9452011-03-25 20:49:53 +00001832 CHAR16 *TempString;
jcarsey94b17fa2009-05-07 18:46:18 +00001833
1834 //
1835 // ASSERT that all 3 pointer parameters aren't NULL
1836 //
1837 ASSERT(CheckList != NULL);
1838 ASSERT(Type != NULL);
1839 ASSERT(Name != NULL);
1840
1841 //
jcarseyd2b45642009-05-11 18:02:16 +00001842 // question mark and page break mode are always supported
1843 //
1844 if ((StrCmp(Name, L"-?") == 0) ||
1845 (StrCmp(Name, L"-b") == 0)
jcarseya405b862010-09-14 05:18:09 +00001846 ) {
jcarsey252d9452011-03-25 20:49:53 +00001847 *Type = TypeFlag;
jcarseyd2b45642009-05-11 18:02:16 +00001848 return (TRUE);
1849 }
1850
1851 //
jcarsey94b17fa2009-05-07 18:46:18 +00001852 // Enumerate through the list
1853 //
1854 for (TempListItem = (SHELL_PARAM_ITEM*)CheckList ; TempListItem->Name != NULL ; TempListItem++) {
1855 //
jcarsey9eb53ac2009-07-08 17:26:58 +00001856 // If the Type is TypeStart only check the first characters of the passed in param
1857 // If it matches set the type and return TRUE
jcarsey94b17fa2009-05-07 18:46:18 +00001858 //
darylm503b0934ac2011-11-12 00:35:11 +00001859 if (TempListItem->Type == TypeStart) {
jcarsey252d9452011-03-25 20:49:53 +00001860 if (StrnCmp(Name, TempListItem->Name, StrLen(TempListItem->Name)) == 0) {
1861 *Type = TempListItem->Type;
1862 return (TRUE);
1863 }
1864 TempString = NULL;
1865 TempString = StrnCatGrow(&TempString, NULL, Name, StrLen(TempListItem->Name));
1866 if (TempString != NULL) {
1867 if (StringNoCaseCompare(&TempString, &TempListItem->Name) == 0) {
1868 *Type = TempListItem->Type;
1869 FreePool(TempString);
1870 return (TRUE);
1871 }
1872 FreePool(TempString);
1873 }
1874 } else if (StringNoCaseCompare(&Name, &TempListItem->Name) == 0) {
jcarsey94b17fa2009-05-07 18:46:18 +00001875 *Type = TempListItem->Type;
1876 return (TRUE);
1877 }
1878 }
jcarsey2247dde2009-11-09 18:08:58 +00001879
jcarsey94b17fa2009-05-07 18:46:18 +00001880 return (FALSE);
1881}
1882/**
jcarseyd2b45642009-05-11 18:02:16 +00001883 Checks the string for indicators of "flag" status. this is a leading '/', '-', or '+'
jcarsey94b17fa2009-05-07 18:46:18 +00001884
jcarseya405b862010-09-14 05:18:09 +00001885 @param[in] Name pointer to Name of parameter found
1886 @param[in] AlwaysAllowNumbers TRUE to allow numbers, FALSE to not.
jcarsey94b17fa2009-05-07 18:46:18 +00001887
1888 @retval TRUE the Parameter is a flag.
jcarseya405b862010-09-14 05:18:09 +00001889 @retval FALSE the Parameter not a flag.
jcarsey94b17fa2009-05-07 18:46:18 +00001890**/
1891BOOLEAN
1892EFIAPI
jcarseyd2b45642009-05-11 18:02:16 +00001893InternalIsFlag (
jcarsey2247dde2009-11-09 18:08:58 +00001894 IN CONST CHAR16 *Name,
1895 IN BOOLEAN AlwaysAllowNumbers
jcarsey94b17fa2009-05-07 18:46:18 +00001896 )
1897{
1898 //
1899 // ASSERT that Name isn't NULL
1900 //
1901 ASSERT(Name != NULL);
1902
1903 //
jcarsey2247dde2009-11-09 18:08:58 +00001904 // If we accept numbers then dont return TRUE. (they will be values)
1905 //
jcarseya405b862010-09-14 05:18:09 +00001906 if (((Name[0] == L'-' || Name[0] == L'+') && ShellIsHexaDecimalDigitCharacter(Name[1])) && AlwaysAllowNumbers) {
jcarsey2247dde2009-11-09 18:08:58 +00001907 return (FALSE);
1908 }
1909
1910 //
jcarseya405b862010-09-14 05:18:09 +00001911 // If the Name has a /, +, or - as the first character return TRUE
jcarsey94b17fa2009-05-07 18:46:18 +00001912 //
jcarsey1e6e84c2010-01-25 20:05:08 +00001913 if ((Name[0] == L'/') ||
jcarseyd2b45642009-05-11 18:02:16 +00001914 (Name[0] == L'-') ||
1915 (Name[0] == L'+')
jcarseya405b862010-09-14 05:18:09 +00001916 ) {
jcarsey94b17fa2009-05-07 18:46:18 +00001917 return (TRUE);
1918 }
1919 return (FALSE);
1920}
1921
1922/**
jcarsey1e6e84c2010-01-25 20:05:08 +00001923 Checks the command line arguments passed against the list of valid ones.
jcarsey94b17fa2009-05-07 18:46:18 +00001924
1925 If no initialization is required, then return RETURN_SUCCESS.
jcarsey1e6e84c2010-01-25 20:05:08 +00001926
jcarseya405b862010-09-14 05:18:09 +00001927 @param[in] CheckList pointer to list of parameters to check
1928 @param[out] CheckPackage pointer to pointer to list checked values
1929 @param[out] ProblemParam optional pointer to pointer to unicode string for
jcarseyd2b45642009-05-11 18:02:16 +00001930 the paramater that caused failure. If used then the
1931 caller is responsible for freeing the memory.
jcarseya405b862010-09-14 05:18:09 +00001932 @param[in] AutoPageBreak will automatically set PageBreakEnabled for "b" parameter
1933 @param[in] Argv pointer to array of parameters
1934 @param[in] Argc Count of parameters in Argv
1935 @param[in] AlwaysAllowNumbers TRUE to allow numbers always, FALSE otherwise.
jcarsey94b17fa2009-05-07 18:46:18 +00001936
1937 @retval EFI_SUCCESS The operation completed sucessfully.
1938 @retval EFI_OUT_OF_RESOURCES A memory allocation failed
1939 @retval EFI_INVALID_PARAMETER A parameter was invalid
jcarsey1e6e84c2010-01-25 20:05:08 +00001940 @retval EFI_VOLUME_CORRUPTED the command line was corrupt. an argument was
1941 duplicated. the duplicated command line argument
jcarsey94b17fa2009-05-07 18:46:18 +00001942 was returned in ProblemParam if provided.
jcarsey1e6e84c2010-01-25 20:05:08 +00001943 @retval EFI_NOT_FOUND a argument required a value that was missing.
jcarsey94b17fa2009-05-07 18:46:18 +00001944 the invalid command line argument was returned in
1945 ProblemParam if provided.
1946**/
1947EFI_STATUS
1948EFIAPI
1949InternalCommandLineParse (
1950 IN CONST SHELL_PARAM_ITEM *CheckList,
1951 OUT LIST_ENTRY **CheckPackage,
1952 OUT CHAR16 **ProblemParam OPTIONAL,
1953 IN BOOLEAN AutoPageBreak,
1954 IN CONST CHAR16 **Argv,
jcarsey2247dde2009-11-09 18:08:58 +00001955 IN UINTN Argc,
1956 IN BOOLEAN AlwaysAllowNumbers
jcarseya405b862010-09-14 05:18:09 +00001957 )
1958{
jcarsey94b17fa2009-05-07 18:46:18 +00001959 UINTN LoopCounter;
jcarsey252d9452011-03-25 20:49:53 +00001960 SHELL_PARAM_TYPE CurrentItemType;
jcarsey94b17fa2009-05-07 18:46:18 +00001961 SHELL_PARAM_PACKAGE *CurrentItemPackage;
jcarsey125c2cf2009-11-18 21:36:50 +00001962 UINTN GetItemValue;
1963 UINTN ValueSize;
jcarseya405b862010-09-14 05:18:09 +00001964 UINTN Count;
jcarsey252d9452011-03-25 20:49:53 +00001965 CONST CHAR16 *TempPointer;
jcarsey94b17fa2009-05-07 18:46:18 +00001966
1967 CurrentItemPackage = NULL;
jcarsey125c2cf2009-11-18 21:36:50 +00001968 GetItemValue = 0;
1969 ValueSize = 0;
jcarseya405b862010-09-14 05:18:09 +00001970 Count = 0;
jcarsey94b17fa2009-05-07 18:46:18 +00001971
1972 //
1973 // If there is only 1 item we dont need to do anything
1974 //
jcarseya405b862010-09-14 05:18:09 +00001975 if (Argc < 1) {
jcarsey94b17fa2009-05-07 18:46:18 +00001976 *CheckPackage = NULL;
1977 return (EFI_SUCCESS);
1978 }
1979
1980 //
jcarsey2247dde2009-11-09 18:08:58 +00001981 // ASSERTs
1982 //
1983 ASSERT(CheckList != NULL);
1984 ASSERT(Argv != NULL);
1985
1986 //
jcarsey94b17fa2009-05-07 18:46:18 +00001987 // initialize the linked list
1988 //
1989 *CheckPackage = (LIST_ENTRY*)AllocateZeroPool(sizeof(LIST_ENTRY));
sfu502a758c2011-10-08 02:55:30 +00001990 if (*CheckPackage == NULL) {
jcarseybeab0fc2011-10-10 17:26:25 +00001991 return (EFI_OUT_OF_RESOURCES);
sfu502a758c2011-10-08 02:55:30 +00001992 }
jcarseybeab0fc2011-10-10 17:26:25 +00001993
jcarsey94b17fa2009-05-07 18:46:18 +00001994 InitializeListHead(*CheckPackage);
1995
1996 //
1997 // loop through each of the arguments
1998 //
1999 for (LoopCounter = 0 ; LoopCounter < Argc ; ++LoopCounter) {
2000 if (Argv[LoopCounter] == NULL) {
2001 //
2002 // do nothing for NULL argv
2003 //
jcarseya405b862010-09-14 05:18:09 +00002004 } else if (InternalIsOnCheckList(Argv[LoopCounter], CheckList, &CurrentItemType)) {
jcarsey94b17fa2009-05-07 18:46:18 +00002005 //
jcarsey2247dde2009-11-09 18:08:58 +00002006 // We might have leftover if last parameter didnt have optional value
2007 //
jcarsey125c2cf2009-11-18 21:36:50 +00002008 if (GetItemValue != 0) {
2009 GetItemValue = 0;
jcarsey2247dde2009-11-09 18:08:58 +00002010 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
2011 }
2012 //
jcarsey94b17fa2009-05-07 18:46:18 +00002013 // this is a flag
2014 //
jcarsey252d9452011-03-25 20:49:53 +00002015 CurrentItemPackage = AllocateZeroPool(sizeof(SHELL_PARAM_PACKAGE));
jcarseybeab0fc2011-10-10 17:26:25 +00002016 if (CurrentItemPackage == NULL) {
2017 ShellCommandLineFreeVarList(*CheckPackage);
2018 *CheckPackage = NULL;
2019 return (EFI_OUT_OF_RESOURCES);
2020 }
jcarsey252d9452011-03-25 20:49:53 +00002021 CurrentItemPackage->Name = AllocateZeroPool(StrSize(Argv[LoopCounter]));
jcarseybeab0fc2011-10-10 17:26:25 +00002022 if (CurrentItemPackage->Name == NULL) {
2023 ShellCommandLineFreeVarList(*CheckPackage);
2024 *CheckPackage = NULL;
2025 return (EFI_OUT_OF_RESOURCES);
2026 }
jcarsey94b17fa2009-05-07 18:46:18 +00002027 StrCpy(CurrentItemPackage->Name, Argv[LoopCounter]);
2028 CurrentItemPackage->Type = CurrentItemType;
2029 CurrentItemPackage->OriginalPosition = (UINTN)(-1);
jcarseyb1f95a02009-06-16 00:23:19 +00002030 CurrentItemPackage->Value = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +00002031
2032 //
2033 // Does this flag require a value
2034 //
jcarsey125c2cf2009-11-18 21:36:50 +00002035 switch (CurrentItemPackage->Type) {
jcarsey94b17fa2009-05-07 18:46:18 +00002036 //
jcarsey125c2cf2009-11-18 21:36:50 +00002037 // possibly trigger the next loop(s) to populate the value of this item
jcarsey1e6e84c2010-01-25 20:05:08 +00002038 //
jcarsey125c2cf2009-11-18 21:36:50 +00002039 case TypeValue:
jcarsey1e6e84c2010-01-25 20:05:08 +00002040 GetItemValue = 1;
jcarsey125c2cf2009-11-18 21:36:50 +00002041 ValueSize = 0;
2042 break;
2043 case TypeDoubleValue:
2044 GetItemValue = 2;
2045 ValueSize = 0;
2046 break;
2047 case TypeMaxValue:
2048 GetItemValue = (UINTN)(-1);
2049 ValueSize = 0;
2050 break;
2051 default:
2052 //
2053 // this item has no value expected; we are done
2054 //
2055 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
2056 ASSERT(GetItemValue == 0);
2057 break;
jcarsey94b17fa2009-05-07 18:46:18 +00002058 }
jcarseya405b862010-09-14 05:18:09 +00002059 } else if (GetItemValue != 0 && !InternalIsFlag(Argv[LoopCounter], AlwaysAllowNumbers)) {
jcarseyb1f95a02009-06-16 00:23:19 +00002060 ASSERT(CurrentItemPackage != NULL);
2061 //
jcarsey125c2cf2009-11-18 21:36:50 +00002062 // get the item VALUE for a previous flag
jcarseyb1f95a02009-06-16 00:23:19 +00002063 //
jcarsey49bd4982012-01-27 18:42:43 +00002064 if (StrStr(Argv[LoopCounter], L" ") == NULL) {
2065 CurrentItemPackage->Value = ReallocatePool(ValueSize, ValueSize + StrSize(Argv[LoopCounter]) + sizeof(CHAR16), CurrentItemPackage->Value);
2066 ASSERT(CurrentItemPackage->Value != NULL);
2067 if (ValueSize == 0) {
2068 StrCpy(CurrentItemPackage->Value, Argv[LoopCounter]);
2069 } else {
2070 StrCat(CurrentItemPackage->Value, L" ");
2071 StrCat(CurrentItemPackage->Value, Argv[LoopCounter]);
2072 }
2073 ValueSize += StrSize(Argv[LoopCounter]) + sizeof(CHAR16);
jcarsey125c2cf2009-11-18 21:36:50 +00002074 } else {
jcarsey49bd4982012-01-27 18:42:43 +00002075 //
2076 // the parameter has spaces. must be quoted.
2077 //
2078 CurrentItemPackage->Value = ReallocatePool(ValueSize, ValueSize + StrSize(Argv[LoopCounter]) + sizeof(CHAR16) + sizeof(CHAR16) + sizeof(CHAR16), CurrentItemPackage->Value);
2079 ASSERT(CurrentItemPackage->Value != NULL);
2080 if (ValueSize == 0) {
2081 StrCpy(CurrentItemPackage->Value, L"\"");
2082 StrCat(CurrentItemPackage->Value, Argv[LoopCounter]);
2083 StrCat(CurrentItemPackage->Value, L"\"");
2084 } else {
2085 StrCat(CurrentItemPackage->Value, L" ");
2086 StrCat(CurrentItemPackage->Value, L"\"");
2087 StrCat(CurrentItemPackage->Value, Argv[LoopCounter]);
2088 StrCat(CurrentItemPackage->Value, L"\"");
2089 }
2090 ValueSize += StrSize(Argv[LoopCounter]) + sizeof(CHAR16);
jcarsey125c2cf2009-11-18 21:36:50 +00002091 }
jcarsey125c2cf2009-11-18 21:36:50 +00002092 GetItemValue--;
2093 if (GetItemValue == 0) {
2094 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
2095 }
jcarseya405b862010-09-14 05:18:09 +00002096 } else if (!InternalIsFlag(Argv[LoopCounter], AlwaysAllowNumbers) ){ //|| ProblemParam == NULL) {
jcarseyb1f95a02009-06-16 00:23:19 +00002097 //
2098 // add this one as a non-flag
2099 //
jcarsey252d9452011-03-25 20:49:53 +00002100
2101 TempPointer = Argv[LoopCounter];
darylm503b0934ac2011-11-12 00:35:11 +00002102 if ((*TempPointer == L'^' && *(TempPointer+1) == L'-')
jcarsey252d9452011-03-25 20:49:53 +00002103 || (*TempPointer == L'^' && *(TempPointer+1) == L'/')
2104 || (*TempPointer == L'^' && *(TempPointer+1) == L'+')
2105 ){
2106 TempPointer++;
2107 }
2108 CurrentItemPackage = AllocateZeroPool(sizeof(SHELL_PARAM_PACKAGE));
jcarseybeab0fc2011-10-10 17:26:25 +00002109 if (CurrentItemPackage == NULL) {
2110 ShellCommandLineFreeVarList(*CheckPackage);
2111 *CheckPackage = NULL;
2112 return (EFI_OUT_OF_RESOURCES);
2113 }
jcarseyb1f95a02009-06-16 00:23:19 +00002114 CurrentItemPackage->Name = NULL;
2115 CurrentItemPackage->Type = TypePosition;
jcarsey252d9452011-03-25 20:49:53 +00002116 CurrentItemPackage->Value = AllocateZeroPool(StrSize(TempPointer));
jcarseybeab0fc2011-10-10 17:26:25 +00002117 if (CurrentItemPackage->Value == NULL) {
2118 ShellCommandLineFreeVarList(*CheckPackage);
2119 *CheckPackage = NULL;
2120 return (EFI_OUT_OF_RESOURCES);
2121 }
jcarsey252d9452011-03-25 20:49:53 +00002122 StrCpy(CurrentItemPackage->Value, TempPointer);
jcarseya405b862010-09-14 05:18:09 +00002123 CurrentItemPackage->OriginalPosition = Count++;
jcarsey9b3bf082009-06-23 21:15:07 +00002124 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
jcarsey252d9452011-03-25 20:49:53 +00002125 } else {
jcarsey94b17fa2009-05-07 18:46:18 +00002126 //
2127 // this was a non-recognised flag... error!
2128 //
jcarsey252d9452011-03-25 20:49:53 +00002129 if (ProblemParam != NULL) {
2130 *ProblemParam = AllocateZeroPool(StrSize(Argv[LoopCounter]));
jcarseybeab0fc2011-10-10 17:26:25 +00002131 if (*ProblemParam != NULL) {
darylm503b0934ac2011-11-12 00:35:11 +00002132 StrCpy(*ProblemParam, Argv[LoopCounter]);
jcarseybeab0fc2011-10-10 17:26:25 +00002133 }
jcarsey252d9452011-03-25 20:49:53 +00002134 }
jcarsey94b17fa2009-05-07 18:46:18 +00002135 ShellCommandLineFreeVarList(*CheckPackage);
2136 *CheckPackage = NULL;
2137 return (EFI_VOLUME_CORRUPTED);
jcarsey94b17fa2009-05-07 18:46:18 +00002138 }
2139 }
jcarsey125c2cf2009-11-18 21:36:50 +00002140 if (GetItemValue != 0) {
2141 GetItemValue = 0;
2142 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
2143 }
jcarsey94b17fa2009-05-07 18:46:18 +00002144 //
2145 // support for AutoPageBreak
2146 //
2147 if (AutoPageBreak && ShellCommandLineGetFlag(*CheckPackage, L"-b")) {
2148 ShellSetPageBreakMode(TRUE);
2149 }
2150 return (EFI_SUCCESS);
2151}
2152
2153/**
jcarsey1e6e84c2010-01-25 20:05:08 +00002154 Checks the command line arguments passed against the list of valid ones.
jcarsey94b17fa2009-05-07 18:46:18 +00002155 Optionally removes NULL values first.
jcarsey1e6e84c2010-01-25 20:05:08 +00002156
jcarsey94b17fa2009-05-07 18:46:18 +00002157 If no initialization is required, then return RETURN_SUCCESS.
jcarsey1e6e84c2010-01-25 20:05:08 +00002158
jcarseya405b862010-09-14 05:18:09 +00002159 @param[in] CheckList The pointer to list of parameters to check.
2160 @param[out] CheckPackage The package of checked values.
2161 @param[out] ProblemParam Optional pointer to pointer to unicode string for
jcarsey94b17fa2009-05-07 18:46:18 +00002162 the paramater that caused failure.
jcarseya405b862010-09-14 05:18:09 +00002163 @param[in] AutoPageBreak Will automatically set PageBreakEnabled.
2164 @param[in] AlwaysAllowNumbers Will never fail for number based flags.
jcarsey94b17fa2009-05-07 18:46:18 +00002165
2166 @retval EFI_SUCCESS The operation completed sucessfully.
jcarseya405b862010-09-14 05:18:09 +00002167 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
2168 @retval EFI_INVALID_PARAMETER A parameter was invalid.
2169 @retval EFI_VOLUME_CORRUPTED The command line was corrupt.
2170 @retval EFI_DEVICE_ERROR The commands contained 2 opposing arguments. One
jcarsey1e6e84c2010-01-25 20:05:08 +00002171 of the command line arguments was returned in
jcarsey94b17fa2009-05-07 18:46:18 +00002172 ProblemParam if provided.
jcarseya405b862010-09-14 05:18:09 +00002173 @retval EFI_NOT_FOUND A argument required a value that was missing.
2174 The invalid command line argument was returned in
jcarsey94b17fa2009-05-07 18:46:18 +00002175 ProblemParam if provided.
2176**/
2177EFI_STATUS
2178EFIAPI
jcarsey2247dde2009-11-09 18:08:58 +00002179ShellCommandLineParseEx (
jcarsey94b17fa2009-05-07 18:46:18 +00002180 IN CONST SHELL_PARAM_ITEM *CheckList,
2181 OUT LIST_ENTRY **CheckPackage,
2182 OUT CHAR16 **ProblemParam OPTIONAL,
jcarsey2247dde2009-11-09 18:08:58 +00002183 IN BOOLEAN AutoPageBreak,
2184 IN BOOLEAN AlwaysAllowNumbers
jcarseya405b862010-09-14 05:18:09 +00002185 )
2186{
jcarsey1e6e84c2010-01-25 20:05:08 +00002187 //
jcarsey94b17fa2009-05-07 18:46:18 +00002188 // ASSERT that CheckList and CheckPackage aren't NULL
2189 //
2190 ASSERT(CheckList != NULL);
2191 ASSERT(CheckPackage != NULL);
2192
jcarsey1e6e84c2010-01-25 20:05:08 +00002193 //
jcarsey94b17fa2009-05-07 18:46:18 +00002194 // Check for UEFI Shell 2.0 protocols
2195 //
jcarsey366f81a2011-06-27 21:04:22 +00002196 if (gEfiShellParametersProtocol != NULL) {
jcarsey1e6e84c2010-01-25 20:05:08 +00002197 return (InternalCommandLineParse(CheckList,
2198 CheckPackage,
2199 ProblemParam,
2200 AutoPageBreak,
jcarsey366f81a2011-06-27 21:04:22 +00002201 (CONST CHAR16**) gEfiShellParametersProtocol->Argv,
2202 gEfiShellParametersProtocol->Argc,
jcarsey2247dde2009-11-09 18:08:58 +00002203 AlwaysAllowNumbers));
jcarsey94b17fa2009-05-07 18:46:18 +00002204 }
2205
jcarsey1e6e84c2010-01-25 20:05:08 +00002206 //
jcarsey94b17fa2009-05-07 18:46:18 +00002207 // ASSERT That EFI Shell is not required
2208 //
2209 ASSERT (mEfiShellInterface != NULL);
jcarsey1e6e84c2010-01-25 20:05:08 +00002210 return (InternalCommandLineParse(CheckList,
2211 CheckPackage,
2212 ProblemParam,
2213 AutoPageBreak,
jljusten08d7f8e2009-06-15 18:42:13 +00002214 (CONST CHAR16**) mEfiShellInterface->Argv,
jcarsey2247dde2009-11-09 18:08:58 +00002215 mEfiShellInterface->Argc,
2216 AlwaysAllowNumbers));
jcarsey94b17fa2009-05-07 18:46:18 +00002217}
2218
2219/**
2220 Frees shell variable list that was returned from ShellCommandLineParse.
2221
2222 This function will free all the memory that was used for the CheckPackage
2223 list of postprocessed shell arguments.
2224
2225 this function has no return value.
2226
2227 if CheckPackage is NULL, then return
2228
2229 @param CheckPackage the list to de-allocate
2230 **/
2231VOID
2232EFIAPI
2233ShellCommandLineFreeVarList (
2234 IN LIST_ENTRY *CheckPackage
jcarseya405b862010-09-14 05:18:09 +00002235 )
2236{
jcarsey94b17fa2009-05-07 18:46:18 +00002237 LIST_ENTRY *Node;
2238
2239 //
2240 // check for CheckPackage == NULL
2241 //
2242 if (CheckPackage == NULL) {
2243 return;
2244 }
2245
2246 //
2247 // for each node in the list
2248 //
jcarsey9eb53ac2009-07-08 17:26:58 +00002249 for ( Node = GetFirstNode(CheckPackage)
jcarseya405b862010-09-14 05:18:09 +00002250 ; !IsListEmpty(CheckPackage)
jcarsey9eb53ac2009-07-08 17:26:58 +00002251 ; Node = GetFirstNode(CheckPackage)
jcarseya405b862010-09-14 05:18:09 +00002252 ){
jcarsey94b17fa2009-05-07 18:46:18 +00002253 //
2254 // Remove it from the list
2255 //
2256 RemoveEntryList(Node);
2257
2258 //
2259 // if it has a name free the name
2260 //
2261 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {
2262 FreePool(((SHELL_PARAM_PACKAGE*)Node)->Name);
2263 }
2264
2265 //
2266 // if it has a value free the value
2267 //
2268 if (((SHELL_PARAM_PACKAGE*)Node)->Value != NULL) {
2269 FreePool(((SHELL_PARAM_PACKAGE*)Node)->Value);
2270 }
jcarsey1e6e84c2010-01-25 20:05:08 +00002271
jcarsey94b17fa2009-05-07 18:46:18 +00002272 //
2273 // free the node structure
2274 //
2275 FreePool((SHELL_PARAM_PACKAGE*)Node);
2276 }
2277 //
2278 // free the list head node
2279 //
2280 FreePool(CheckPackage);
2281}
2282/**
2283 Checks for presence of a flag parameter
2284
2285 flag arguments are in the form of "-<Key>" or "/<Key>", but do not have a value following the key
2286
2287 if CheckPackage is NULL then return FALSE.
2288 if KeyString is NULL then ASSERT()
jcarsey1e6e84c2010-01-25 20:05:08 +00002289
jcarsey94b17fa2009-05-07 18:46:18 +00002290 @param CheckPackage The package of parsed command line arguments
2291 @param KeyString the Key of the command line argument to check for
2292
2293 @retval TRUE the flag is on the command line
2294 @retval FALSE the flag is not on the command line
2295 **/
2296BOOLEAN
2297EFIAPI
2298ShellCommandLineGetFlag (
jcarseya405b862010-09-14 05:18:09 +00002299 IN CONST LIST_ENTRY * CONST CheckPackage,
2300 IN CONST CHAR16 * CONST KeyString
2301 )
2302{
jcarsey94b17fa2009-05-07 18:46:18 +00002303 LIST_ENTRY *Node;
jcarsey252d9452011-03-25 20:49:53 +00002304 CHAR16 *TempString;
jcarsey94b17fa2009-05-07 18:46:18 +00002305
2306 //
ydong100c1950b2011-10-13 02:37:35 +00002307 // return FALSE for no package or KeyString is NULL
jcarsey94b17fa2009-05-07 18:46:18 +00002308 //
ydong100c1950b2011-10-13 02:37:35 +00002309 if (CheckPackage == NULL || KeyString == NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00002310 return (FALSE);
2311 }
2312
2313 //
2314 // enumerate through the list of parametrs
2315 //
jcarsey1e6e84c2010-01-25 20:05:08 +00002316 for ( Node = GetFirstNode(CheckPackage)
2317 ; !IsNull (CheckPackage, Node)
2318 ; Node = GetNextNode(CheckPackage, Node)
jcarsey252d9452011-03-25 20:49:53 +00002319 ){
jcarsey94b17fa2009-05-07 18:46:18 +00002320 //
2321 // If the Name matches, return TRUE (and there may be NULL name)
2322 //
2323 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {
jcarsey9eb53ac2009-07-08 17:26:58 +00002324 //
2325 // If Type is TypeStart then only compare the begining of the strings
2326 //
jcarsey252d9452011-03-25 20:49:53 +00002327 if (((SHELL_PARAM_PACKAGE*)Node)->Type == TypeStart) {
2328 if (StrnCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name, StrLen(KeyString)) == 0) {
2329 return (TRUE);
2330 }
2331 TempString = NULL;
2332 TempString = StrnCatGrow(&TempString, NULL, KeyString, StrLen(((SHELL_PARAM_PACKAGE*)Node)->Name));
2333 if (TempString != NULL) {
2334 if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {
2335 FreePool(TempString);
2336 return (TRUE);
2337 }
2338 FreePool(TempString);
2339 }
2340 } else if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {
jcarsey94b17fa2009-05-07 18:46:18 +00002341 return (TRUE);
2342 }
2343 }
2344 }
2345 return (FALSE);
2346}
2347/**
jcarseya405b862010-09-14 05:18:09 +00002348 Returns value from command line argument.
jcarsey94b17fa2009-05-07 18:46:18 +00002349
jcarseya405b862010-09-14 05:18:09 +00002350 Value parameters are in the form of "-<Key> value" or "/<Key> value".
jcarsey1e6e84c2010-01-25 20:05:08 +00002351
jcarseya405b862010-09-14 05:18:09 +00002352 If CheckPackage is NULL, then return NULL.
jcarsey94b17fa2009-05-07 18:46:18 +00002353
jcarseya405b862010-09-14 05:18:09 +00002354 @param[in] CheckPackage The package of parsed command line arguments.
2355 @param[in] KeyString The Key of the command line argument to check for.
jcarsey94b17fa2009-05-07 18:46:18 +00002356
jcarseya405b862010-09-14 05:18:09 +00002357 @retval NULL The flag is not on the command line.
2358 @retval !=NULL The pointer to unicode string of the value.
2359**/
jcarsey94b17fa2009-05-07 18:46:18 +00002360CONST CHAR16*
2361EFIAPI
2362ShellCommandLineGetValue (
2363 IN CONST LIST_ENTRY *CheckPackage,
2364 IN CHAR16 *KeyString
jcarseya405b862010-09-14 05:18:09 +00002365 )
2366{
jcarsey94b17fa2009-05-07 18:46:18 +00002367 LIST_ENTRY *Node;
jcarsey252d9452011-03-25 20:49:53 +00002368 CHAR16 *TempString;
jcarsey94b17fa2009-05-07 18:46:18 +00002369
2370 //
ydong100c1950b2011-10-13 02:37:35 +00002371 // return NULL for no package or KeyString is NULL
jcarsey94b17fa2009-05-07 18:46:18 +00002372 //
ydong100c1950b2011-10-13 02:37:35 +00002373 if (CheckPackage == NULL || KeyString == NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00002374 return (NULL);
2375 }
2376
2377 //
2378 // enumerate through the list of parametrs
2379 //
jcarsey1e6e84c2010-01-25 20:05:08 +00002380 for ( Node = GetFirstNode(CheckPackage)
2381 ; !IsNull (CheckPackage, Node)
2382 ; Node = GetNextNode(CheckPackage, Node)
jcarsey252d9452011-03-25 20:49:53 +00002383 ){
jcarsey94b17fa2009-05-07 18:46:18 +00002384 //
jcarsey252d9452011-03-25 20:49:53 +00002385 // If the Name matches, return TRUE (and there may be NULL name)
jcarsey94b17fa2009-05-07 18:46:18 +00002386 //
2387 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {
jcarsey9eb53ac2009-07-08 17:26:58 +00002388 //
2389 // If Type is TypeStart then only compare the begining of the strings
2390 //
jcarsey252d9452011-03-25 20:49:53 +00002391 if (((SHELL_PARAM_PACKAGE*)Node)->Type == TypeStart) {
2392 if (StrnCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name, StrLen(KeyString)) == 0) {
2393 return (((SHELL_PARAM_PACKAGE*)Node)->Name + StrLen(KeyString));
2394 }
2395 TempString = NULL;
2396 TempString = StrnCatGrow(&TempString, NULL, KeyString, StrLen(((SHELL_PARAM_PACKAGE*)Node)->Name));
2397 if (TempString != NULL) {
2398 if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {
2399 FreePool(TempString);
2400 return (((SHELL_PARAM_PACKAGE*)Node)->Name + StrLen(KeyString));
2401 }
2402 FreePool(TempString);
2403 }
2404 } else if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {
jcarsey94b17fa2009-05-07 18:46:18 +00002405 return (((SHELL_PARAM_PACKAGE*)Node)->Value);
2406 }
2407 }
2408 }
2409 return (NULL);
2410}
jcarseya405b862010-09-14 05:18:09 +00002411
jcarsey94b17fa2009-05-07 18:46:18 +00002412/**
jcarseya405b862010-09-14 05:18:09 +00002413 Returns raw value from command line argument.
jcarsey94b17fa2009-05-07 18:46:18 +00002414
jcarseya405b862010-09-14 05:18:09 +00002415 Raw value parameters are in the form of "value" in a specific position in the list.
jcarsey1e6e84c2010-01-25 20:05:08 +00002416
jcarseya405b862010-09-14 05:18:09 +00002417 If CheckPackage is NULL, then return NULL.
jcarsey94b17fa2009-05-07 18:46:18 +00002418
jcarseya405b862010-09-14 05:18:09 +00002419 @param[in] CheckPackage The package of parsed command line arguments.
2420 @param[in] Position The position of the value.
jcarsey94b17fa2009-05-07 18:46:18 +00002421
jcarseya405b862010-09-14 05:18:09 +00002422 @retval NULL The flag is not on the command line.
2423 @retval !=NULL The pointer to unicode string of the value.
jcarsey94b17fa2009-05-07 18:46:18 +00002424 **/
2425CONST CHAR16*
2426EFIAPI
2427ShellCommandLineGetRawValue (
jcarseya405b862010-09-14 05:18:09 +00002428 IN CONST LIST_ENTRY * CONST CheckPackage,
2429 IN UINTN Position
2430 )
2431{
jcarsey94b17fa2009-05-07 18:46:18 +00002432 LIST_ENTRY *Node;
2433
2434 //
2435 // check for CheckPackage == NULL
2436 //
2437 if (CheckPackage == NULL) {
2438 return (NULL);
2439 }
2440
2441 //
2442 // enumerate through the list of parametrs
2443 //
jcarsey1e6e84c2010-01-25 20:05:08 +00002444 for ( Node = GetFirstNode(CheckPackage)
2445 ; !IsNull (CheckPackage, Node)
2446 ; Node = GetNextNode(CheckPackage, Node)
jcarseya405b862010-09-14 05:18:09 +00002447 ){
jcarsey94b17fa2009-05-07 18:46:18 +00002448 //
2449 // If the position matches, return the value
2450 //
2451 if (((SHELL_PARAM_PACKAGE*)Node)->OriginalPosition == Position) {
2452 return (((SHELL_PARAM_PACKAGE*)Node)->Value);
2453 }
2454 }
2455 return (NULL);
jcarseyb1f95a02009-06-16 00:23:19 +00002456}
jcarsey2247dde2009-11-09 18:08:58 +00002457
2458/**
jcarsey1e6e84c2010-01-25 20:05:08 +00002459 returns the number of command line value parameters that were parsed.
2460
jcarsey2247dde2009-11-09 18:08:58 +00002461 this will not include flags.
2462
jcarseya405b862010-09-14 05:18:09 +00002463 @param[in] CheckPackage The package of parsed command line arguments.
2464
jcarsey2247dde2009-11-09 18:08:58 +00002465 @retval (UINTN)-1 No parsing has ocurred
2466 @return other The number of value parameters found
2467**/
2468UINTN
2469EFIAPI
2470ShellCommandLineGetCount(
jcarseya405b862010-09-14 05:18:09 +00002471 IN CONST LIST_ENTRY *CheckPackage
jcarsey125c2cf2009-11-18 21:36:50 +00002472 )
2473{
jcarseya405b862010-09-14 05:18:09 +00002474 LIST_ENTRY *Node1;
2475 UINTN Count;
2476
2477 if (CheckPackage == NULL) {
2478 return (0);
2479 }
2480 for ( Node1 = GetFirstNode(CheckPackage), Count = 0
2481 ; !IsNull (CheckPackage, Node1)
2482 ; Node1 = GetNextNode(CheckPackage, Node1)
2483 ){
2484 if (((SHELL_PARAM_PACKAGE*)Node1)->Name == NULL) {
2485 Count++;
2486 }
2487 }
2488 return (Count);
jcarsey2247dde2009-11-09 18:08:58 +00002489}
2490
jcarsey975136a2009-06-16 19:03:54 +00002491/**
jcarsey36a9d672009-11-20 21:13:41 +00002492 Determins if a parameter is duplicated.
2493
jcarsey1e6e84c2010-01-25 20:05:08 +00002494 If Param is not NULL then it will point to a callee allocated string buffer
jcarsey36a9d672009-11-20 21:13:41 +00002495 with the parameter value if a duplicate is found.
2496
2497 If CheckPackage is NULL, then ASSERT.
2498
2499 @param[in] CheckPackage The package of parsed command line arguments.
2500 @param[out] Param Upon finding one, a pointer to the duplicated parameter.
2501
2502 @retval EFI_SUCCESS No parameters were duplicated.
2503 @retval EFI_DEVICE_ERROR A duplicate was found.
2504 **/
2505EFI_STATUS
2506EFIAPI
2507ShellCommandLineCheckDuplicate (
2508 IN CONST LIST_ENTRY *CheckPackage,
2509 OUT CHAR16 **Param
2510 )
2511{
2512 LIST_ENTRY *Node1;
2513 LIST_ENTRY *Node2;
jcarsey1e6e84c2010-01-25 20:05:08 +00002514
jcarsey36a9d672009-11-20 21:13:41 +00002515 ASSERT(CheckPackage != NULL);
2516
jcarsey1e6e84c2010-01-25 20:05:08 +00002517 for ( Node1 = GetFirstNode(CheckPackage)
2518 ; !IsNull (CheckPackage, Node1)
2519 ; Node1 = GetNextNode(CheckPackage, Node1)
jcarseya405b862010-09-14 05:18:09 +00002520 ){
jcarsey1e6e84c2010-01-25 20:05:08 +00002521 for ( Node2 = GetNextNode(CheckPackage, Node1)
2522 ; !IsNull (CheckPackage, Node2)
2523 ; Node2 = GetNextNode(CheckPackage, Node2)
jcarseya405b862010-09-14 05:18:09 +00002524 ){
2525 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 +00002526 if (Param != NULL) {
2527 *Param = NULL;
2528 *Param = StrnCatGrow(Param, NULL, ((SHELL_PARAM_PACKAGE*)Node1)->Name, 0);
2529 }
2530 return (EFI_DEVICE_ERROR);
2531 }
2532 }
2533 }
2534 return (EFI_SUCCESS);
2535}
2536
2537/**
jcarsey1e6e84c2010-01-25 20:05:08 +00002538 This is a find and replace function. Upon successful return the NewString is a copy of
jcarsey975136a2009-06-16 19:03:54 +00002539 SourceString with each instance of FindTarget replaced with ReplaceWith.
2540
jcarseyb3011f42010-01-11 21:49:04 +00002541 If SourceString and NewString overlap the behavior is undefined.
2542
jcarsey975136a2009-06-16 19:03:54 +00002543 If the string would grow bigger than NewSize it will halt and return error.
2544
ydong104ff7e372011-09-02 08:05:34 +00002545 @param[in] SourceString The string with source buffer.
2546 @param[in, out] NewString The string with resultant buffer.
2547 @param[in] NewSize The size in bytes of NewString.
2548 @param[in] FindTarget The string to look for.
2549 @param[in] ReplaceWith The string to replace FindTarget with.
2550 @param[in] SkipPreCarrot If TRUE will skip a FindTarget that has a '^'
2551 immediately before it.
2552 @param[in] ParameterReplacing If TRUE will add "" around items with spaces.
jcarsey975136a2009-06-16 19:03:54 +00002553
jcarsey969c7832010-01-13 16:46:33 +00002554 @retval EFI_INVALID_PARAMETER SourceString was NULL.
2555 @retval EFI_INVALID_PARAMETER NewString was NULL.
2556 @retval EFI_INVALID_PARAMETER FindTarget was NULL.
2557 @retval EFI_INVALID_PARAMETER ReplaceWith was NULL.
2558 @retval EFI_INVALID_PARAMETER FindTarget had length < 1.
2559 @retval EFI_INVALID_PARAMETER SourceString had length < 1.
jcarsey1e6e84c2010-01-25 20:05:08 +00002560 @retval EFI_BUFFER_TOO_SMALL NewSize was less than the minimum size to hold
jcarsey969c7832010-01-13 16:46:33 +00002561 the new string (truncation occurred).
jcarseya405b862010-09-14 05:18:09 +00002562 @retval EFI_SUCCESS The string was successfully copied with replacement.
jcarsey975136a2009-06-16 19:03:54 +00002563**/
jcarsey975136a2009-06-16 19:03:54 +00002564EFI_STATUS
2565EFIAPI
jcarseya405b862010-09-14 05:18:09 +00002566ShellCopySearchAndReplace(
jcarsey975136a2009-06-16 19:03:54 +00002567 IN CHAR16 CONST *SourceString,
jcarseya405b862010-09-14 05:18:09 +00002568 IN OUT CHAR16 *NewString,
jcarsey975136a2009-06-16 19:03:54 +00002569 IN UINTN NewSize,
2570 IN CONST CHAR16 *FindTarget,
jcarsey969c7832010-01-13 16:46:33 +00002571 IN CONST CHAR16 *ReplaceWith,
jcarseya405b862010-09-14 05:18:09 +00002572 IN CONST BOOLEAN SkipPreCarrot,
2573 IN CONST BOOLEAN ParameterReplacing
jcarsey1e6e84c2010-01-25 20:05:08 +00002574 )
jcarsey2247dde2009-11-09 18:08:58 +00002575{
jcarsey01582942009-07-10 19:46:17 +00002576 UINTN Size;
jcarseya405b862010-09-14 05:18:09 +00002577 CHAR16 *Replace;
2578
jcarsey975136a2009-06-16 19:03:54 +00002579 if ( (SourceString == NULL)
2580 || (NewString == NULL)
2581 || (FindTarget == NULL)
2582 || (ReplaceWith == NULL)
2583 || (StrLen(FindTarget) < 1)
2584 || (StrLen(SourceString) < 1)
jcarseya405b862010-09-14 05:18:09 +00002585 ){
jcarsey975136a2009-06-16 19:03:54 +00002586 return (EFI_INVALID_PARAMETER);
2587 }
jcarseya405b862010-09-14 05:18:09 +00002588 Replace = NULL;
2589 if (StrStr(ReplaceWith, L" ") == NULL || !ParameterReplacing) {
2590 Replace = StrnCatGrow(&Replace, NULL, ReplaceWith, 0);
2591 } else {
2592 Replace = AllocateZeroPool(StrSize(ReplaceWith) + 2*sizeof(CHAR16));
jcarseybeab0fc2011-10-10 17:26:25 +00002593 if (Replace != NULL) {
2594 UnicodeSPrint(Replace, StrSize(ReplaceWith) + 2*sizeof(CHAR16), L"\"%s\"", ReplaceWith);
2595 }
jcarseya405b862010-09-14 05:18:09 +00002596 }
jcarsey3e082d52010-10-04 16:44:57 +00002597 if (Replace == NULL) {
2598 return (EFI_OUT_OF_RESOURCES);
2599 }
jcarsey2247dde2009-11-09 18:08:58 +00002600 NewString = SetMem16(NewString, NewSize, CHAR_NULL);
2601 while (*SourceString != CHAR_NULL) {
jcarsey969c7832010-01-13 16:46:33 +00002602 //
jcarseya405b862010-09-14 05:18:09 +00002603 // if we find the FindTarget and either Skip == FALSE or Skip and we
jcarsey969c7832010-01-13 16:46:33 +00002604 // dont have a carrot do a replace...
2605 //
jcarsey1e6e84c2010-01-25 20:05:08 +00002606 if (StrnCmp(SourceString, FindTarget, StrLen(FindTarget)) == 0
jcarseya405b862010-09-14 05:18:09 +00002607 && ((SkipPreCarrot && *(SourceString-1) != L'^') || !SkipPreCarrot)
2608 ){
jcarsey975136a2009-06-16 19:03:54 +00002609 SourceString += StrLen(FindTarget);
jcarsey01582942009-07-10 19:46:17 +00002610 Size = StrSize(NewString);
jcarseya405b862010-09-14 05:18:09 +00002611 if ((Size + (StrLen(Replace)*sizeof(CHAR16))) > NewSize) {
2612 FreePool(Replace);
jcarsey975136a2009-06-16 19:03:54 +00002613 return (EFI_BUFFER_TOO_SMALL);
2614 }
jcarseya405b862010-09-14 05:18:09 +00002615 StrCat(NewString, Replace);
jcarsey975136a2009-06-16 19:03:54 +00002616 } else {
jcarsey01582942009-07-10 19:46:17 +00002617 Size = StrSize(NewString);
2618 if (Size + sizeof(CHAR16) > NewSize) {
jcarseya405b862010-09-14 05:18:09 +00002619 FreePool(Replace);
jcarsey975136a2009-06-16 19:03:54 +00002620 return (EFI_BUFFER_TOO_SMALL);
2621 }
2622 StrnCat(NewString, SourceString, 1);
2623 SourceString++;
2624 }
2625 }
jcarseya405b862010-09-14 05:18:09 +00002626 FreePool(Replace);
jcarsey975136a2009-06-16 19:03:54 +00002627 return (EFI_SUCCESS);
2628}
jcarseyb1f95a02009-06-16 00:23:19 +00002629
2630/**
jcarseye2f82972009-12-01 05:40:24 +00002631 Internal worker function to output a string.
2632
2633 This function will output a string to the correct StdOut.
2634
2635 @param[in] String The string to print out.
2636
2637 @retval EFI_SUCCESS The operation was sucessful.
2638 @retval !EFI_SUCCESS The operation failed.
2639**/
2640EFI_STATUS
2641EFIAPI
2642InternalPrintTo (
2643 IN CONST CHAR16 *String
2644 )
2645{
2646 UINTN Size;
2647 Size = StrSize(String) - sizeof(CHAR16);
jcarseya405b862010-09-14 05:18:09 +00002648 if (Size == 0) {
2649 return (EFI_SUCCESS);
2650 }
jcarsey366f81a2011-06-27 21:04:22 +00002651 if (gEfiShellParametersProtocol != NULL) {
2652 return (gEfiShellProtocol->WriteFile(gEfiShellParametersProtocol->StdOut, &Size, (VOID*)String));
jcarseye2f82972009-12-01 05:40:24 +00002653 }
2654 if (mEfiShellInterface != NULL) {
jcarsey06c355b2012-03-26 21:00:39 +00002655 if (mEfiShellInterface->RedirArgc == 0) {
jcarsey49bd4982012-01-27 18:42:43 +00002656 //
2657 // Divide in half for old shell. Must be string length not size.
jcarsey06c355b2012-03-26 21:00:39 +00002658 //
2659 Size /=2; // Divide in half only when no redirection.
2660 }
jcarseya405b862010-09-14 05:18:09 +00002661 return (mEfiShellInterface->StdOut->Write(mEfiShellInterface->StdOut, &Size, (VOID*)String));
jcarseye2f82972009-12-01 05:40:24 +00002662 }
2663 ASSERT(FALSE);
2664 return (EFI_UNSUPPORTED);
2665}
2666
2667/**
jcarseyb1f95a02009-06-16 00:23:19 +00002668 Print at a specific location on the screen.
2669
jcarseyf1b87e72009-06-17 00:52:11 +00002670 This function will move the cursor to a given screen location and print the specified string
jcarsey1e6e84c2010-01-25 20:05:08 +00002671
2672 If -1 is specified for either the Row or Col the current screen location for BOTH
jcarseyf1b87e72009-06-17 00:52:11 +00002673 will be used.
jcarseyb1f95a02009-06-16 00:23:19 +00002674
2675 if either Row or Col is out of range for the current console, then ASSERT
2676 if Format is NULL, then ASSERT
2677
jcarsey1e6e84c2010-01-25 20:05:08 +00002678 In addition to the standard %-based flags as supported by UefiLib Print() this supports
jcarseyb1f95a02009-06-16 00:23:19 +00002679 the following additional flags:
2680 %N - Set output attribute to normal
2681 %H - Set output attribute to highlight
2682 %E - Set output attribute to error
2683 %B - Set output attribute to blue color
2684 %V - Set output attribute to green color
2685
2686 Note: The background color is controlled by the shell command cls.
2687
jcarseyb1f95a02009-06-16 00:23:19 +00002688 @param[in] Col the column to print at
jcarsey252d9452011-03-25 20:49:53 +00002689 @param[in] Row the row to print at
jcarseyb1f95a02009-06-16 00:23:19 +00002690 @param[in] Format the format string
jcarsey2247dde2009-11-09 18:08:58 +00002691 @param[in] Marker the marker for the variable argument list
jcarseyb1f95a02009-06-16 00:23:19 +00002692
jcarseya405b862010-09-14 05:18:09 +00002693 @return EFI_SUCCESS The operation was successful.
2694 @return EFI_DEVICE_ERROR The console device reported an error.
jcarseyb1f95a02009-06-16 00:23:19 +00002695**/
jcarseya405b862010-09-14 05:18:09 +00002696EFI_STATUS
jcarseyb1f95a02009-06-16 00:23:19 +00002697EFIAPI
jcarsey2247dde2009-11-09 18:08:58 +00002698InternalShellPrintWorker(
jcarseyb1f95a02009-06-16 00:23:19 +00002699 IN INT32 Col OPTIONAL,
2700 IN INT32 Row OPTIONAL,
2701 IN CONST CHAR16 *Format,
jcarsey252d9452011-03-25 20:49:53 +00002702 IN VA_LIST Marker
jcarsey1e6e84c2010-01-25 20:05:08 +00002703 )
jcarsey2247dde2009-11-09 18:08:58 +00002704{
jcarseyb1f95a02009-06-16 00:23:19 +00002705 EFI_STATUS Status;
jcarsey975136a2009-06-16 19:03:54 +00002706 CHAR16 *ResumeLocation;
2707 CHAR16 *FormatWalker;
jcarseya405b862010-09-14 05:18:09 +00002708 UINTN OriginalAttribute;
jcarsey89e85372011-04-13 23:37:50 +00002709 CHAR16 *mPostReplaceFormat;
2710 CHAR16 *mPostReplaceFormat2;
2711
2712 mPostReplaceFormat = AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize));
2713 mPostReplaceFormat2 = AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize));
jcarseya405b862010-09-14 05:18:09 +00002714
jcarseyf8d3e682011-04-19 17:54:42 +00002715 if (mPostReplaceFormat == NULL || mPostReplaceFormat2 == NULL) {
2716 SHELL_FREE_NON_NULL(mPostReplaceFormat);
2717 SHELL_FREE_NON_NULL(mPostReplaceFormat2);
2718 return (EFI_OUT_OF_RESOURCES);
2719 }
2720
jcarseya405b862010-09-14 05:18:09 +00002721 Status = EFI_SUCCESS;
2722 OriginalAttribute = gST->ConOut->Mode->Attribute;
jcarsey1e6e84c2010-01-25 20:05:08 +00002723
jcarsey975136a2009-06-16 19:03:54 +00002724 //
2725 // Back and forth each time fixing up 1 of our flags...
2726 //
jcarseya405b862010-09-14 05:18:09 +00002727 Status = ShellCopySearchAndReplace(Format, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%N", L"%%N", FALSE, FALSE);
jcarsey975136a2009-06-16 19:03:54 +00002728 ASSERT_EFI_ERROR(Status);
jcarseya405b862010-09-14 05:18:09 +00002729 Status = ShellCopySearchAndReplace(mPostReplaceFormat, mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), L"%E", L"%%E", FALSE, FALSE);
jcarsey975136a2009-06-16 19:03:54 +00002730 ASSERT_EFI_ERROR(Status);
jcarseya405b862010-09-14 05:18:09 +00002731 Status = ShellCopySearchAndReplace(mPostReplaceFormat2, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%H", L"%%H", FALSE, FALSE);
jcarsey975136a2009-06-16 19:03:54 +00002732 ASSERT_EFI_ERROR(Status);
jcarseya405b862010-09-14 05:18:09 +00002733 Status = ShellCopySearchAndReplace(mPostReplaceFormat, mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), L"%B", L"%%B", FALSE, FALSE);
jcarsey975136a2009-06-16 19:03:54 +00002734 ASSERT_EFI_ERROR(Status);
jcarseya405b862010-09-14 05:18:09 +00002735 Status = ShellCopySearchAndReplace(mPostReplaceFormat2, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%V", L"%%V", FALSE, FALSE);
jcarsey975136a2009-06-16 19:03:54 +00002736 ASSERT_EFI_ERROR(Status);
2737
2738 //
2739 // Use the last buffer from replacing to print from...
2740 //
jcarseya405b862010-09-14 05:18:09 +00002741 UnicodeVSPrint (mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), mPostReplaceFormat, Marker);
jcarseyb1f95a02009-06-16 00:23:19 +00002742
2743 if (Col != -1 && Row != -1) {
jcarseyb1f95a02009-06-16 00:23:19 +00002744 Status = gST->ConOut->SetCursorPosition(gST->ConOut, Col, Row);
jcarsey975136a2009-06-16 19:03:54 +00002745 }
2746
jcarseyecd3d592009-12-07 18:05:00 +00002747 FormatWalker = mPostReplaceFormat2;
jcarsey2247dde2009-11-09 18:08:58 +00002748 while (*FormatWalker != CHAR_NULL) {
jcarsey975136a2009-06-16 19:03:54 +00002749 //
2750 // Find the next attribute change request
2751 //
2752 ResumeLocation = StrStr(FormatWalker, L"%");
2753 if (ResumeLocation != NULL) {
jcarsey2247dde2009-11-09 18:08:58 +00002754 *ResumeLocation = CHAR_NULL;
jcarsey975136a2009-06-16 19:03:54 +00002755 }
2756 //
2757 // print the current FormatWalker string
2758 //
jcarseya405b862010-09-14 05:18:09 +00002759 if (StrLen(FormatWalker)>0) {
2760 Status = InternalPrintTo(FormatWalker);
2761 if (EFI_ERROR(Status)) {
2762 break;
2763 }
2764 }
2765
jcarsey975136a2009-06-16 19:03:54 +00002766 //
2767 // update the attribute
2768 //
2769 if (ResumeLocation != NULL) {
jcarsey5d46f172011-08-23 15:34:23 +00002770 if (*(ResumeLocation-1) == L'^') {
2771 //
jcarsey8bb74412012-01-30 18:44:41 +00002772 // Move cursor back 1 position to overwrite the ^
2773 //
2774 gST->ConOut->SetCursorPosition(gST->ConOut, gST->ConOut->Mode->CursorColumn - 1, gST->ConOut->Mode->CursorRow);
2775
2776 //
jcarsey5d46f172011-08-23 15:34:23 +00002777 // Print a simple '%' symbol
2778 //
2779 Status = InternalPrintTo(L"%");
2780 ResumeLocation = ResumeLocation - 1;
2781 } else {
2782 switch (*(ResumeLocation+1)) {
2783 case (L'N'):
2784 gST->ConOut->SetAttribute(gST->ConOut, OriginalAttribute);
jcarseya405b862010-09-14 05:18:09 +00002785 break;
jcarsey5d46f172011-08-23 15:34:23 +00002786 case (L'E'):
2787 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_YELLOW, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));
2788 break;
2789 case (L'H'):
2790 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_WHITE, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));
2791 break;
2792 case (L'B'):
2793 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_BLUE, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));
2794 break;
2795 case (L'V'):
2796 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_GREEN, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));
2797 break;
2798 default:
2799 //
2800 // Print a simple '%' symbol
2801 //
2802 Status = InternalPrintTo(L"%");
2803 if (EFI_ERROR(Status)) {
2804 break;
2805 }
2806 ResumeLocation = ResumeLocation - 1;
2807 break;
2808 }
jcarsey975136a2009-06-16 19:03:54 +00002809 }
2810 } else {
2811 //
2812 // reset to normal now...
2813 //
jcarsey975136a2009-06-16 19:03:54 +00002814 break;
2815 }
2816
2817 //
2818 // update FormatWalker to Resume + 2 (skip the % and the indicator)
2819 //
2820 FormatWalker = ResumeLocation + 2;
2821 }
jcarseyb1f95a02009-06-16 00:23:19 +00002822
jcarseya405b862010-09-14 05:18:09 +00002823 gST->ConOut->SetAttribute(gST->ConOut, OriginalAttribute);
jcarsey89e85372011-04-13 23:37:50 +00002824
2825 SHELL_FREE_NON_NULL(mPostReplaceFormat);
2826 SHELL_FREE_NON_NULL(mPostReplaceFormat2);
jcarseya405b862010-09-14 05:18:09 +00002827 return (Status);
jcarsey5f7431d2009-07-10 18:06:01 +00002828}
jcarsey2247dde2009-11-09 18:08:58 +00002829
2830/**
2831 Print at a specific location on the screen.
2832
jcarseye2f82972009-12-01 05:40:24 +00002833 This function will move the cursor to a given screen location and print the specified string.
jcarsey1e6e84c2010-01-25 20:05:08 +00002834
2835 If -1 is specified for either the Row or Col the current screen location for BOTH
jcarsey2247dde2009-11-09 18:08:58 +00002836 will be used.
2837
jcarseye2f82972009-12-01 05:40:24 +00002838 If either Row or Col is out of range for the current console, then ASSERT.
2839 If Format is NULL, then ASSERT.
jcarsey2247dde2009-11-09 18:08:58 +00002840
jcarsey1e6e84c2010-01-25 20:05:08 +00002841 In addition to the standard %-based flags as supported by UefiLib Print() this supports
jcarsey2247dde2009-11-09 18:08:58 +00002842 the following additional flags:
2843 %N - Set output attribute to normal
2844 %H - Set output attribute to highlight
2845 %E - Set output attribute to error
2846 %B - Set output attribute to blue color
2847 %V - Set output attribute to green color
2848
2849 Note: The background color is controlled by the shell command cls.
2850
jcarsey2247dde2009-11-09 18:08:58 +00002851 @param[in] Col the column to print at
jcarseya405b862010-09-14 05:18:09 +00002852 @param[in] Row the row to print at
jcarsey2247dde2009-11-09 18:08:58 +00002853 @param[in] Format the format string
jcarseya405b862010-09-14 05:18:09 +00002854 @param[in] ... The variable argument list.
jcarsey2247dde2009-11-09 18:08:58 +00002855
jcarseya405b862010-09-14 05:18:09 +00002856 @return EFI_SUCCESS The printing was successful.
2857 @return EFI_DEVICE_ERROR The console device reported an error.
jcarsey2247dde2009-11-09 18:08:58 +00002858**/
jcarseya405b862010-09-14 05:18:09 +00002859EFI_STATUS
jcarsey2247dde2009-11-09 18:08:58 +00002860EFIAPI
2861ShellPrintEx(
2862 IN INT32 Col OPTIONAL,
2863 IN INT32 Row OPTIONAL,
2864 IN CONST CHAR16 *Format,
2865 ...
jcarsey1e6e84c2010-01-25 20:05:08 +00002866 )
jcarsey2247dde2009-11-09 18:08:58 +00002867{
2868 VA_LIST Marker;
jcarseya405b862010-09-14 05:18:09 +00002869 EFI_STATUS RetVal;
jcarsey3e082d52010-10-04 16:44:57 +00002870 if (Format == NULL) {
2871 return (EFI_INVALID_PARAMETER);
2872 }
jcarsey2247dde2009-11-09 18:08:58 +00002873 VA_START (Marker, Format);
jcarseya405b862010-09-14 05:18:09 +00002874 RetVal = InternalShellPrintWorker(Col, Row, Format, Marker);
jcarseye2f82972009-12-01 05:40:24 +00002875 VA_END(Marker);
jcarseya405b862010-09-14 05:18:09 +00002876 return(RetVal);
jcarsey2247dde2009-11-09 18:08:58 +00002877}
2878
2879/**
2880 Print at a specific location on the screen.
2881
jcarseye2f82972009-12-01 05:40:24 +00002882 This function will move the cursor to a given screen location and print the specified string.
jcarsey1e6e84c2010-01-25 20:05:08 +00002883
2884 If -1 is specified for either the Row or Col the current screen location for BOTH
jcarseye2f82972009-12-01 05:40:24 +00002885 will be used.
jcarsey2247dde2009-11-09 18:08:58 +00002886
jcarseye2f82972009-12-01 05:40:24 +00002887 If either Row or Col is out of range for the current console, then ASSERT.
2888 If Format is NULL, then ASSERT.
jcarsey2247dde2009-11-09 18:08:58 +00002889
jcarsey1e6e84c2010-01-25 20:05:08 +00002890 In addition to the standard %-based flags as supported by UefiLib Print() this supports
jcarsey2247dde2009-11-09 18:08:58 +00002891 the following additional flags:
jcarsey1e6e84c2010-01-25 20:05:08 +00002892 %N - Set output attribute to normal.
2893 %H - Set output attribute to highlight.
2894 %E - Set output attribute to error.
2895 %B - Set output attribute to blue color.
2896 %V - Set output attribute to green color.
jcarsey2247dde2009-11-09 18:08:58 +00002897
2898 Note: The background color is controlled by the shell command cls.
2899
jcarsey1e6e84c2010-01-25 20:05:08 +00002900 @param[in] Col The column to print at.
jcarseya405b862010-09-14 05:18:09 +00002901 @param[in] Row The row to print at.
jcarsey1e6e84c2010-01-25 20:05:08 +00002902 @param[in] Language The language of the string to retrieve. If this parameter
2903 is NULL, then the current platform language is used.
2904 @param[in] HiiFormatStringId The format string Id for getting from Hii.
2905 @param[in] HiiFormatHandle The format string Handle for getting from Hii.
jcarseya405b862010-09-14 05:18:09 +00002906 @param[in] ... The variable argument list.
jcarsey2247dde2009-11-09 18:08:58 +00002907
jcarseya405b862010-09-14 05:18:09 +00002908 @return EFI_SUCCESS The printing was successful.
2909 @return EFI_DEVICE_ERROR The console device reported an error.
jcarsey2247dde2009-11-09 18:08:58 +00002910**/
jcarseya405b862010-09-14 05:18:09 +00002911EFI_STATUS
jcarsey2247dde2009-11-09 18:08:58 +00002912EFIAPI
2913ShellPrintHiiEx(
2914 IN INT32 Col OPTIONAL,
2915 IN INT32 Row OPTIONAL,
jcarsey1e6e84c2010-01-25 20:05:08 +00002916 IN CONST CHAR8 *Language OPTIONAL,
jcarsey2247dde2009-11-09 18:08:58 +00002917 IN CONST EFI_STRING_ID HiiFormatStringId,
2918 IN CONST EFI_HANDLE HiiFormatHandle,
2919 ...
2920 )
2921{
2922 VA_LIST Marker;
2923 CHAR16 *HiiFormatString;
jcarseya405b862010-09-14 05:18:09 +00002924 EFI_STATUS RetVal;
jcarsey2247dde2009-11-09 18:08:58 +00002925
2926 VA_START (Marker, HiiFormatHandle);
jcarsey1e6e84c2010-01-25 20:05:08 +00002927 HiiFormatString = HiiGetString(HiiFormatHandle, HiiFormatStringId, Language);
jcarsey2247dde2009-11-09 18:08:58 +00002928 ASSERT(HiiFormatString != NULL);
2929
2930 RetVal = InternalShellPrintWorker(Col, Row, HiiFormatString, Marker);
2931
jcarseya405b862010-09-14 05:18:09 +00002932 SHELL_FREE_NON_NULL(HiiFormatString);
jcarseye2f82972009-12-01 05:40:24 +00002933 VA_END(Marker);
jcarsey2247dde2009-11-09 18:08:58 +00002934
2935 return (RetVal);
2936}
2937
2938/**
2939 Function to determine if a given filename represents a file or a directory.
2940
2941 @param[in] DirName Path to directory to test.
2942
jcarseyc8c22592011-10-17 17:49:21 +00002943 @retval EFI_SUCCESS The Path represents a directory
2944 @retval EFI_NOT_FOUND The Path does not represent a directory
2945 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
2946 @return The path failed to open
jcarsey2247dde2009-11-09 18:08:58 +00002947**/
2948EFI_STATUS
2949EFIAPI
2950ShellIsDirectory(
2951 IN CONST CHAR16 *DirName
2952 )
2953{
2954 EFI_STATUS Status;
jcarseya405b862010-09-14 05:18:09 +00002955 SHELL_FILE_HANDLE Handle;
jcarsey3e082d52010-10-04 16:44:57 +00002956 CHAR16 *TempLocation;
2957 CHAR16 *TempLocation2;
jcarsey2247dde2009-11-09 18:08:58 +00002958
jcarseyecd3d592009-12-07 18:05:00 +00002959 ASSERT(DirName != NULL);
2960
jcarseya405b862010-09-14 05:18:09 +00002961 Handle = NULL;
2962 TempLocation = NULL;
jcarsey2247dde2009-11-09 18:08:58 +00002963
2964 Status = ShellOpenFileByName(DirName, &Handle, EFI_FILE_MODE_READ, 0);
2965 if (EFI_ERROR(Status)) {
jcarseya405b862010-09-14 05:18:09 +00002966 //
2967 // try good logic first.
2968 //
jcarsey366f81a2011-06-27 21:04:22 +00002969 if (gEfiShellProtocol != NULL) {
jcarsey3e082d52010-10-04 16:44:57 +00002970 TempLocation = StrnCatGrow(&TempLocation, NULL, DirName, 0);
jcarseyc8c22592011-10-17 17:49:21 +00002971 if (TempLocation == NULL) {
2972 ShellCloseFile(&Handle);
2973 return (EFI_OUT_OF_RESOURCES);
2974 }
jcarsey3e082d52010-10-04 16:44:57 +00002975 TempLocation2 = StrStr(TempLocation, L":");
2976 if (TempLocation2 != NULL && StrLen(StrStr(TempLocation, L":")) == 2) {
2977 *(TempLocation2+1) = CHAR_NULL;
jcarseya405b862010-09-14 05:18:09 +00002978 }
jcarsey366f81a2011-06-27 21:04:22 +00002979 if (gEfiShellProtocol->GetDevicePathFromMap(TempLocation) != NULL) {
jcarseya405b862010-09-14 05:18:09 +00002980 FreePool(TempLocation);
2981 return (EFI_SUCCESS);
2982 }
2983 FreePool(TempLocation);
2984 } else {
2985 //
2986 // probably a map name?!?!!?
2987 //
2988 TempLocation = StrStr(DirName, L"\\");
2989 if (TempLocation != NULL && *(TempLocation+1) == CHAR_NULL) {
2990 return (EFI_SUCCESS);
2991 }
2992 }
jcarsey2247dde2009-11-09 18:08:58 +00002993 return (Status);
2994 }
2995
2996 if (FileHandleIsDirectory(Handle) == EFI_SUCCESS) {
2997 ShellCloseFile(&Handle);
2998 return (EFI_SUCCESS);
2999 }
3000 ShellCloseFile(&Handle);
3001 return (EFI_NOT_FOUND);
3002}
3003
jcarsey125c2cf2009-11-18 21:36:50 +00003004/**
jcarsey36a9d672009-11-20 21:13:41 +00003005 Function to determine if a given filename represents a file.
3006
3007 @param[in] Name Path to file to test.
3008
3009 @retval EFI_SUCCESS The Path represents a file.
3010 @retval EFI_NOT_FOUND The Path does not represent a file.
3011 @retval other The path failed to open.
3012**/
3013EFI_STATUS
3014EFIAPI
3015ShellIsFile(
3016 IN CONST CHAR16 *Name
3017 )
3018{
3019 EFI_STATUS Status;
jcarseya405b862010-09-14 05:18:09 +00003020 SHELL_FILE_HANDLE Handle;
jcarsey36a9d672009-11-20 21:13:41 +00003021
jcarseyecd3d592009-12-07 18:05:00 +00003022 ASSERT(Name != NULL);
3023
jcarsey36a9d672009-11-20 21:13:41 +00003024 Handle = NULL;
3025
3026 Status = ShellOpenFileByName(Name, &Handle, EFI_FILE_MODE_READ, 0);
3027 if (EFI_ERROR(Status)) {
3028 return (Status);
3029 }
3030
3031 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {
3032 ShellCloseFile(&Handle);
3033 return (EFI_SUCCESS);
3034 }
3035 ShellCloseFile(&Handle);
3036 return (EFI_NOT_FOUND);
3037}
3038
3039/**
jcarseyb3011f42010-01-11 21:49:04 +00003040 Function to determine if a given filename represents a file.
3041
3042 This will search the CWD and then the Path.
3043
3044 If Name is NULL, then ASSERT.
3045
3046 @param[in] Name Path to file to test.
3047
3048 @retval EFI_SUCCESS The Path represents a file.
3049 @retval EFI_NOT_FOUND The Path does not represent a file.
3050 @retval other The path failed to open.
3051**/
3052EFI_STATUS
3053EFIAPI
3054ShellIsFileInPath(
3055 IN CONST CHAR16 *Name
jcarseya405b862010-09-14 05:18:09 +00003056 )
3057{
jcarseyb3011f42010-01-11 21:49:04 +00003058 CHAR16 *NewName;
3059 EFI_STATUS Status;
3060
3061 if (!EFI_ERROR(ShellIsFile(Name))) {
jcarseya405b862010-09-14 05:18:09 +00003062 return (EFI_SUCCESS);
jcarseyb3011f42010-01-11 21:49:04 +00003063 }
3064
3065 NewName = ShellFindFilePath(Name);
3066 if (NewName == NULL) {
3067 return (EFI_NOT_FOUND);
3068 }
3069 Status = ShellIsFile(NewName);
3070 FreePool(NewName);
3071 return (Status);
3072}
jcarsey252d9452011-03-25 20:49:53 +00003073
jcarseyb3011f42010-01-11 21:49:04 +00003074/**
Jaben Carsey74b0fb82013-11-22 21:37:34 +00003075 Function return the number converted from a hex representation of a number.
3076
3077 Note: this function cannot be used when (UINTN)(-1), (0xFFFFFFFF) may be a valid
3078 result. Use ShellConvertStringToUint64 instead.
3079
3080 @param[in] String String representation of a number.
3081
3082 @return The unsigned integer result of the conversion.
3083 @retval (UINTN)(-1) An error occured.
3084**/
3085UINTN
3086EFIAPI
3087ShellHexStrToUintn(
3088 IN CONST CHAR16 *String
3089 )
3090{
3091 UINT64 RetVal;
3092
3093 if (!EFI_ERROR(ShellConvertStringToUint64(String, &RetVal, TRUE, TRUE))) {
3094 return ((UINTN)RetVal);
3095 }
3096
3097 return ((UINTN)(-1));
3098}
3099
3100/**
jcarsey1e6e84c2010-01-25 20:05:08 +00003101 Function to determine whether a string is decimal or hex representation of a number
Jaben Carseyd59fc242013-11-14 23:52:43 +00003102 and return the number converted from the string. Spaces are always skipped.
jcarsey125c2cf2009-11-18 21:36:50 +00003103
3104 @param[in] String String representation of a number
3105
jcarsey252d9452011-03-25 20:49:53 +00003106 @return the number
3107 @retval (UINTN)(-1) An error ocurred.
jcarsey125c2cf2009-11-18 21:36:50 +00003108**/
3109UINTN
3110EFIAPI
3111ShellStrToUintn(
3112 IN CONST CHAR16 *String
3113 )
3114{
jcarsey252d9452011-03-25 20:49:53 +00003115 UINT64 RetVal;
3116 BOOLEAN Hex;
3117
3118 Hex = FALSE;
3119
3120 if (!InternalShellIsHexOrDecimalNumber(String, Hex, TRUE)) {
3121 Hex = TRUE;
jcarsey125c2cf2009-11-18 21:36:50 +00003122 }
jcarsey252d9452011-03-25 20:49:53 +00003123
3124 if (!EFI_ERROR(ShellConvertStringToUint64(String, &RetVal, Hex, TRUE))) {
3125 return ((UINTN)RetVal);
3126 }
3127 return ((UINTN)(-1));
jcarsey125c2cf2009-11-18 21:36:50 +00003128}
3129
3130/**
3131 Safely append with automatic string resizing given length of Destination and
3132 desired length of copy from Source.
3133
3134 append the first D characters of Source to the end of Destination, where D is
3135 the lesser of Count and the StrLen() of Source. If appending those D characters
3136 will fit within Destination (whose Size is given as CurrentSize) and
jcarsey1e6e84c2010-01-25 20:05:08 +00003137 still leave room for a NULL terminator, then those characters are appended,
3138 starting at the original terminating NULL of Destination, and a new terminating
3139 NULL is appended.
jcarsey125c2cf2009-11-18 21:36:50 +00003140
3141 If appending D characters onto Destination will result in a overflow of the size
3142 given in CurrentSize the string will be grown such that the copy can be performed
3143 and CurrentSize will be updated to the new size.
3144
3145 If Source is NULL, there is nothing to append, just return the current buffer in
3146 Destination.
3147
3148 if Destination is NULL, then ASSERT()
3149 if Destination's current length (including NULL terminator) is already more then
3150 CurrentSize, then ASSERT()
3151
ydong104ff7e372011-09-02 08:05:34 +00003152 @param[in, out] Destination The String to append onto
3153 @param[in, out] CurrentSize on call the number of bytes in Destination. On
jcarsey125c2cf2009-11-18 21:36:50 +00003154 return possibly the new size (still in bytes). if NULL
3155 then allocate whatever is needed.
3156 @param[in] Source The String to append from
3157 @param[in] Count Maximum number of characters to append. if 0 then
3158 all are appended.
3159
3160 @return Destination return the resultant string.
3161**/
3162CHAR16*
3163EFIAPI
3164StrnCatGrow (
3165 IN OUT CHAR16 **Destination,
3166 IN OUT UINTN *CurrentSize,
3167 IN CONST CHAR16 *Source,
3168 IN UINTN Count
3169 )
3170{
3171 UINTN DestinationStartSize;
3172 UINTN NewSize;
3173
3174 //
3175 // ASSERTs
3176 //
3177 ASSERT(Destination != NULL);
3178
3179 //
3180 // If there's nothing to do then just return Destination
3181 //
3182 if (Source == NULL) {
3183 return (*Destination);
3184 }
3185
3186 //
3187 // allow for un-initialized pointers, based on size being 0
3188 //
3189 if (CurrentSize != NULL && *CurrentSize == 0) {
3190 *Destination = NULL;
3191 }
3192
3193 //
3194 // allow for NULL pointers address as Destination
3195 //
3196 if (*Destination != NULL) {
3197 ASSERT(CurrentSize != 0);
3198 DestinationStartSize = StrSize(*Destination);
3199 ASSERT(DestinationStartSize <= *CurrentSize);
3200 } else {
3201 DestinationStartSize = 0;
3202// ASSERT(*CurrentSize == 0);
3203 }
3204
3205 //
3206 // Append all of Source?
3207 //
3208 if (Count == 0) {
3209 Count = StrLen(Source);
3210 }
3211
3212 //
3213 // Test and grow if required
3214 //
3215 if (CurrentSize != NULL) {
3216 NewSize = *CurrentSize;
ydong10f480fdc2012-09-07 01:55:33 +00003217 if (NewSize < DestinationStartSize + (Count * sizeof(CHAR16))) {
3218 while (NewSize < (DestinationStartSize + (Count*sizeof(CHAR16)))) {
3219 NewSize += 2 * Count * sizeof(CHAR16);
3220 }
3221 *Destination = ReallocatePool(*CurrentSize, NewSize, *Destination);
3222 *CurrentSize = NewSize;
jcarsey125c2cf2009-11-18 21:36:50 +00003223 }
jcarsey125c2cf2009-11-18 21:36:50 +00003224 } else {
3225 *Destination = AllocateZeroPool((Count+1)*sizeof(CHAR16));
3226 }
3227
3228 //
3229 // Now use standard StrnCat on a big enough buffer
3230 //
jcarseyc9d92df2010-02-03 15:37:54 +00003231 if (*Destination == NULL) {
3232 return (NULL);
3233 }
jcarsey125c2cf2009-11-18 21:36:50 +00003234 return StrnCat(*Destination, Source, Count);
3235}
jcarseyc9d92df2010-02-03 15:37:54 +00003236
3237/**
3238 Prompt the user and return the resultant answer to the requestor.
3239
3240 This function will display the requested question on the shell prompt and then
3241 wait for an apropriate answer to be input from the console.
3242
jcarseya405b862010-09-14 05:18:09 +00003243 if the SHELL_PROMPT_REQUEST_TYPE is SHELL_PROMPT_REQUEST_TYPE_YESNO, ShellPromptResponseTypeQuitContinue
jcarseyc9d92df2010-02-03 15:37:54 +00003244 or SHELL_PROMPT_REQUEST_TYPE_YESNOCANCEL then *Response is of type SHELL_PROMPT_RESPONSE.
3245
jcarseya405b862010-09-14 05:18:09 +00003246 if the SHELL_PROMPT_REQUEST_TYPE is ShellPromptResponseTypeFreeform then *Response is of type
jcarseyc9d92df2010-02-03 15:37:54 +00003247 CHAR16*.
3248
3249 In either case *Response must be callee freed if Response was not NULL;
3250
3251 @param Type What type of question is asked. This is used to filter the input
3252 to prevent invalid answers to question.
3253 @param Prompt Pointer to string prompt to use to request input.
3254 @param Response Pointer to Response which will be populated upon return.
3255
3256 @retval EFI_SUCCESS The operation was sucessful.
3257 @retval EFI_UNSUPPORTED The operation is not supported as requested.
3258 @retval EFI_INVALID_PARAMETER A parameter was invalid.
3259 @return other The operation failed.
3260**/
3261EFI_STATUS
3262EFIAPI
3263ShellPromptForResponse (
3264 IN SHELL_PROMPT_REQUEST_TYPE Type,
3265 IN CHAR16 *Prompt OPTIONAL,
3266 IN OUT VOID **Response OPTIONAL
3267 )
3268{
3269 EFI_STATUS Status;
3270 EFI_INPUT_KEY Key;
3271 UINTN EventIndex;
3272 SHELL_PROMPT_RESPONSE *Resp;
jcarseya405b862010-09-14 05:18:09 +00003273 UINTN Size;
3274 CHAR16 *Buffer;
jcarseyc9d92df2010-02-03 15:37:54 +00003275
jcarseya405b862010-09-14 05:18:09 +00003276 Status = EFI_UNSUPPORTED;
3277 Resp = NULL;
3278 Buffer = NULL;
3279 Size = 0;
3280 if (Type != ShellPromptResponseTypeFreeform) {
jcarsey252d9452011-03-25 20:49:53 +00003281 Resp = (SHELL_PROMPT_RESPONSE*)AllocateZeroPool(sizeof(SHELL_PROMPT_RESPONSE));
jcarsey3e082d52010-10-04 16:44:57 +00003282 if (Resp == NULL) {
3283 return (EFI_OUT_OF_RESOURCES);
3284 }
xdu290bfa222010-07-19 05:21:27 +00003285 }
jcarseyc9d92df2010-02-03 15:37:54 +00003286
3287 switch(Type) {
jcarseya405b862010-09-14 05:18:09 +00003288 case ShellPromptResponseTypeQuitContinue:
jcarseyc9d92df2010-02-03 15:37:54 +00003289 if (Prompt != NULL) {
3290 ShellPrintEx(-1, -1, L"%s", Prompt);
3291 }
3292 //
3293 // wait for valid response
3294 //
3295 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
3296 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
Jaben Carsey31b018a2014-01-16 16:52:39 +00003297 if (EFI_ERROR(Status)) {
3298 break;
3299 }
jcarseyc9d92df2010-02-03 15:37:54 +00003300 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3301 if (Key.UnicodeChar == L'Q' || Key.UnicodeChar ==L'q') {
jcarseya405b862010-09-14 05:18:09 +00003302 *Resp = ShellPromptResponseQuit;
jcarseyc9d92df2010-02-03 15:37:54 +00003303 } else {
jcarseya405b862010-09-14 05:18:09 +00003304 *Resp = ShellPromptResponseContinue;
jcarseyc9d92df2010-02-03 15:37:54 +00003305 }
3306 break;
jcarseya405b862010-09-14 05:18:09 +00003307 case ShellPromptResponseTypeYesNoCancel:
jcarseyc9d92df2010-02-03 15:37:54 +00003308 if (Prompt != NULL) {
3309 ShellPrintEx(-1, -1, L"%s", Prompt);
3310 }
3311 //
3312 // wait for valid response
3313 //
jcarseya405b862010-09-14 05:18:09 +00003314 *Resp = ShellPromptResponseMax;
3315 while (*Resp == ShellPromptResponseMax) {
Jaben Carsey194ae482013-12-09 22:55:13 +00003316 if (ShellGetExecutionBreakFlag()) {
3317 Status = EFI_ABORTED;
3318 break;
3319 }
jcarseyc9d92df2010-02-03 15:37:54 +00003320 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
3321 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
Jaben Carsey31b018a2014-01-16 16:52:39 +00003322 if (EFI_ERROR(Status)) {
3323 break;
3324 }
jcarseyc9d92df2010-02-03 15:37:54 +00003325 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3326 switch (Key.UnicodeChar) {
3327 case L'Y':
3328 case L'y':
jcarseya405b862010-09-14 05:18:09 +00003329 *Resp = ShellPromptResponseYes;
jcarseyc9d92df2010-02-03 15:37:54 +00003330 break;
3331 case L'N':
3332 case L'n':
jcarseya405b862010-09-14 05:18:09 +00003333 *Resp = ShellPromptResponseNo;
jcarseyc9d92df2010-02-03 15:37:54 +00003334 break;
3335 case L'C':
3336 case L'c':
jcarseya405b862010-09-14 05:18:09 +00003337 *Resp = ShellPromptResponseCancel;
3338 break;
3339 }
3340 }
3341 break; case ShellPromptResponseTypeYesNoAllCancel:
3342 if (Prompt != NULL) {
3343 ShellPrintEx(-1, -1, L"%s", Prompt);
3344 }
3345 //
3346 // wait for valid response
3347 //
3348 *Resp = ShellPromptResponseMax;
3349 while (*Resp == ShellPromptResponseMax) {
Jaben Carsey194ae482013-12-09 22:55:13 +00003350 if (ShellGetExecutionBreakFlag()) {
3351 Status = EFI_ABORTED;
3352 break;
3353 }
jcarseya405b862010-09-14 05:18:09 +00003354 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
3355 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
Jaben Carsey31b018a2014-01-16 16:52:39 +00003356 if (EFI_ERROR(Status)) {
3357 break;
3358 }
jcarseya405b862010-09-14 05:18:09 +00003359 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3360 switch (Key.UnicodeChar) {
3361 case L'Y':
3362 case L'y':
3363 *Resp = ShellPromptResponseYes;
3364 break;
3365 case L'N':
3366 case L'n':
3367 *Resp = ShellPromptResponseNo;
3368 break;
3369 case L'A':
3370 case L'a':
3371 *Resp = ShellPromptResponseAll;
3372 break;
3373 case L'C':
3374 case L'c':
3375 *Resp = ShellPromptResponseCancel;
jcarseyc9d92df2010-02-03 15:37:54 +00003376 break;
3377 }
3378 }
3379 break;
jcarseya405b862010-09-14 05:18:09 +00003380 case ShellPromptResponseTypeEnterContinue:
3381 case ShellPromptResponseTypeAnyKeyContinue:
jcarseyc9d92df2010-02-03 15:37:54 +00003382 if (Prompt != NULL) {
3383 ShellPrintEx(-1, -1, L"%s", Prompt);
3384 }
3385 //
3386 // wait for valid response
3387 //
jcarseya405b862010-09-14 05:18:09 +00003388 *Resp = ShellPromptResponseMax;
3389 while (*Resp == ShellPromptResponseMax) {
Jaben Carsey194ae482013-12-09 22:55:13 +00003390 if (ShellGetExecutionBreakFlag()) {
3391 Status = EFI_ABORTED;
3392 break;
3393 }
jcarseyc9d92df2010-02-03 15:37:54 +00003394 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
jcarseya405b862010-09-14 05:18:09 +00003395 if (Type == ShellPromptResponseTypeEnterContinue) {
jcarseyc9d92df2010-02-03 15:37:54 +00003396 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
Jaben Carsey31b018a2014-01-16 16:52:39 +00003397 if (EFI_ERROR(Status)) {
3398 break;
3399 }
jcarseyc9d92df2010-02-03 15:37:54 +00003400 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3401 if (Key.UnicodeChar == CHAR_CARRIAGE_RETURN) {
jcarseya405b862010-09-14 05:18:09 +00003402 *Resp = ShellPromptResponseContinue;
jcarseyc9d92df2010-02-03 15:37:54 +00003403 break;
3404 }
3405 }
jcarseya405b862010-09-14 05:18:09 +00003406 if (Type == ShellPromptResponseTypeAnyKeyContinue) {
3407 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
3408 ASSERT_EFI_ERROR(Status);
3409 *Resp = ShellPromptResponseContinue;
jcarseyc9d92df2010-02-03 15:37:54 +00003410 break;
3411 }
3412 }
3413 break;
jcarseya405b862010-09-14 05:18:09 +00003414 case ShellPromptResponseTypeYesNo:
3415 if (Prompt != NULL) {
3416 ShellPrintEx(-1, -1, L"%s", Prompt);
3417 }
3418 //
3419 // wait for valid response
3420 //
3421 *Resp = ShellPromptResponseMax;
3422 while (*Resp == ShellPromptResponseMax) {
Jaben Carsey194ae482013-12-09 22:55:13 +00003423 if (ShellGetExecutionBreakFlag()) {
3424 Status = EFI_ABORTED;
3425 break;
3426 }
jcarseya405b862010-09-14 05:18:09 +00003427 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
3428 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
Jaben Carsey31b018a2014-01-16 16:52:39 +00003429 if (EFI_ERROR(Status)) {
3430 break;
3431 }
jcarseya405b862010-09-14 05:18:09 +00003432 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3433 switch (Key.UnicodeChar) {
3434 case L'Y':
3435 case L'y':
3436 *Resp = ShellPromptResponseYes;
3437 break;
3438 case L'N':
3439 case L'n':
3440 *Resp = ShellPromptResponseNo;
3441 break;
3442 }
3443 }
3444 break;
3445 case ShellPromptResponseTypeFreeform:
3446 if (Prompt != NULL) {
3447 ShellPrintEx(-1, -1, L"%s", Prompt);
3448 }
3449 while(1) {
Jaben Carsey194ae482013-12-09 22:55:13 +00003450 if (ShellGetExecutionBreakFlag()) {
3451 Status = EFI_ABORTED;
3452 break;
3453 }
jcarseya405b862010-09-14 05:18:09 +00003454 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
3455 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
Jaben Carsey31b018a2014-01-16 16:52:39 +00003456 if (EFI_ERROR(Status)) {
3457 break;
3458 }
jcarseya405b862010-09-14 05:18:09 +00003459 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3460 if (Key.UnicodeChar == CHAR_CARRIAGE_RETURN) {
3461 break;
3462 }
3463 ASSERT((Buffer == NULL && Size == 0) || (Buffer != NULL));
3464 StrnCatGrow(&Buffer, &Size, &Key.UnicodeChar, 1);
3465 }
3466 break;
3467 //
3468 // This is the location to add new prompt types.
Jaben Carsey194ae482013-12-09 22:55:13 +00003469 // If your new type loops remember to add ExecutionBreak support.
jcarseya405b862010-09-14 05:18:09 +00003470 //
jcarseyc9d92df2010-02-03 15:37:54 +00003471 default:
jcarseya405b862010-09-14 05:18:09 +00003472 ASSERT(FALSE);
jcarseyc9d92df2010-02-03 15:37:54 +00003473 }
3474
3475 if (Response != NULL) {
jcarseya405b862010-09-14 05:18:09 +00003476 if (Resp != NULL) {
3477 *Response = Resp;
3478 } else if (Buffer != NULL) {
3479 *Response = Buffer;
3480 }
jcarseyc9d92df2010-02-03 15:37:54 +00003481 } else {
jcarseya405b862010-09-14 05:18:09 +00003482 if (Resp != NULL) {
3483 FreePool(Resp);
3484 }
3485 if (Buffer != NULL) {
3486 FreePool(Buffer);
3487 }
jcarseyc9d92df2010-02-03 15:37:54 +00003488 }
3489
jcarseya405b862010-09-14 05:18:09 +00003490 ShellPrintEx(-1, -1, L"\r\n");
jcarseyc9d92df2010-02-03 15:37:54 +00003491 return (Status);
3492}
3493
3494/**
3495 Prompt the user and return the resultant answer to the requestor.
3496
3497 This function is the same as ShellPromptForResponse, except that the prompt is
3498 automatically pulled from HII.
3499
3500 @param Type What type of question is asked. This is used to filter the input
3501 to prevent invalid answers to question.
jcarseya405b862010-09-14 05:18:09 +00003502 @param[in] HiiFormatStringId The format string Id for getting from Hii.
3503 @param[in] HiiFormatHandle The format string Handle for getting from Hii.
3504 @param Response Pointer to Response which will be populated upon return.
jcarseyc9d92df2010-02-03 15:37:54 +00003505
3506 @retval EFI_SUCCESS the operation was sucessful.
3507 @return other the operation failed.
3508
3509 @sa ShellPromptForResponse
3510**/
3511EFI_STATUS
3512EFIAPI
3513ShellPromptForResponseHii (
3514 IN SHELL_PROMPT_REQUEST_TYPE Type,
3515 IN CONST EFI_STRING_ID HiiFormatStringId,
3516 IN CONST EFI_HANDLE HiiFormatHandle,
3517 IN OUT VOID **Response
3518 )
3519{
3520 CHAR16 *Prompt;
3521 EFI_STATUS Status;
3522
3523 Prompt = HiiGetString(HiiFormatHandle, HiiFormatStringId, NULL);
3524 Status = ShellPromptForResponse(Type, Prompt, Response);
3525 FreePool(Prompt);
3526 return (Status);
3527}
3528
jcarseya405b862010-09-14 05:18:09 +00003529/**
3530 Function to determin if an entire string is a valid number.
jcarseyc9d92df2010-02-03 15:37:54 +00003531
jcarseya405b862010-09-14 05:18:09 +00003532 If Hex it must be preceeded with a 0x or has ForceHex, set TRUE.
3533
3534 @param[in] String The string to evaluate.
3535 @param[in] ForceHex TRUE - always assume hex.
3536 @param[in] StopAtSpace TRUE to halt upon finding a space, FALSE to keep going.
3537
3538 @retval TRUE It is all numeric (dec/hex) characters.
3539 @retval FALSE There is a non-numeric character.
3540**/
3541BOOLEAN
3542EFIAPI
jcarsey252d9452011-03-25 20:49:53 +00003543InternalShellIsHexOrDecimalNumber (
jcarseya405b862010-09-14 05:18:09 +00003544 IN CONST CHAR16 *String,
3545 IN CONST BOOLEAN ForceHex,
3546 IN CONST BOOLEAN StopAtSpace
3547 )
3548{
3549 BOOLEAN Hex;
3550
3551 //
3552 // chop off a single negative sign
3553 //
3554 if (String != NULL && *String == L'-') {
3555 String++;
3556 }
darylm503b0934ac2011-11-12 00:35:11 +00003557
jcarseya405b862010-09-14 05:18:09 +00003558 if (String == NULL) {
3559 return (FALSE);
3560 }
3561
3562 //
3563 // chop leading zeroes
3564 //
3565 while(String != NULL && *String == L'0'){
3566 String++;
3567 }
3568 //
3569 // allow '0x' or '0X', but not 'x' or 'X'
3570 //
3571 if (String != NULL && (*String == L'x' || *String == L'X')) {
3572 if (*(String-1) != L'0') {
3573 //
3574 // we got an x without a preceeding 0
3575 //
3576 return (FALSE);
3577 }
3578 String++;
3579 Hex = TRUE;
3580 } else if (ForceHex) {
3581 Hex = TRUE;
3582 } else {
3583 Hex = FALSE;
3584 }
3585
3586 //
3587 // loop through the remaining characters and use the lib function
3588 //
3589 for ( ; String != NULL && *String != CHAR_NULL && !(StopAtSpace && *String == L' ') ; String++){
3590 if (Hex) {
3591 if (!ShellIsHexaDecimalDigitCharacter(*String)) {
3592 return (FALSE);
3593 }
3594 } else {
3595 if (!ShellIsDecimalDigitCharacter(*String)) {
3596 return (FALSE);
3597 }
3598 }
3599 }
jcarsey252d9452011-03-25 20:49:53 +00003600
jcarseya405b862010-09-14 05:18:09 +00003601 return (TRUE);
3602}
3603
3604/**
3605 Function to determine if a given filename exists.
3606
3607 @param[in] Name Path to test.
3608
3609 @retval EFI_SUCCESS The Path represents a file.
3610 @retval EFI_NOT_FOUND The Path does not represent a file.
3611 @retval other The path failed to open.
3612**/
3613EFI_STATUS
3614EFIAPI
3615ShellFileExists(
3616 IN CONST CHAR16 *Name
3617 )
3618{
3619 EFI_STATUS Status;
3620 EFI_SHELL_FILE_INFO *List;
3621
3622 ASSERT(Name != NULL);
3623
3624 List = NULL;
3625 Status = ShellOpenFileMetaArg((CHAR16*)Name, EFI_FILE_MODE_READ, &List);
3626 if (EFI_ERROR(Status)) {
3627 return (Status);
3628 }
3629
3630 ShellCloseFileMetaArg(&List);
3631
3632 return (EFI_SUCCESS);
3633}
jcarsey252d9452011-03-25 20:49:53 +00003634
3635/**
darylm503b0934ac2011-11-12 00:35:11 +00003636 Convert a Unicode character to upper case only if
jcarsey252d9452011-03-25 20:49:53 +00003637 it maps to a valid small-case ASCII character.
3638
3639 This internal function only deal with Unicode character
3640 which maps to a valid small-case ASCII character, i.e.
3641 L'a' to L'z'. For other Unicode character, the input character
3642 is returned directly.
3643
3644 @param Char The character to convert.
3645
3646 @retval LowerCharacter If the Char is with range L'a' to L'z'.
3647 @retval Unchanged Otherwise.
3648
3649**/
3650CHAR16
3651EFIAPI
3652InternalShellCharToUpper (
3653 IN CHAR16 Char
3654 )
3655{
3656 if (Char >= L'a' && Char <= L'z') {
3657 return (CHAR16) (Char - (L'a' - L'A'));
3658 }
3659
3660 return Char;
3661}
3662
3663/**
3664 Convert a Unicode character to numerical value.
3665
3666 This internal function only deal with Unicode character
3667 which maps to a valid hexadecimal ASII character, i.e.
darylm503b0934ac2011-11-12 00:35:11 +00003668 L'0' to L'9', L'a' to L'f' or L'A' to L'F'. For other
jcarsey252d9452011-03-25 20:49:53 +00003669 Unicode character, the value returned does not make sense.
3670
3671 @param Char The character to convert.
3672
3673 @return The numerical value converted.
3674
3675**/
3676UINTN
3677EFIAPI
3678InternalShellHexCharToUintn (
3679 IN CHAR16 Char
3680 )
3681{
3682 if (ShellIsDecimalDigitCharacter (Char)) {
3683 return Char - L'0';
3684 }
3685
3686 return (UINTN) (10 + InternalShellCharToUpper (Char) - L'A');
3687}
3688
3689/**
3690 Convert a Null-terminated Unicode hexadecimal string to a value of type UINT64.
3691
3692 This function returns a value of type UINTN by interpreting the contents
3693 of the Unicode string specified by String as a hexadecimal number.
3694 The format of the input Unicode string String is:
3695
3696 [spaces][zeros][x][hexadecimal digits].
3697
3698 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
3699 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix.
3700 If "x" appears in the input string, it must be prefixed with at least one 0.
3701 The function will ignore the pad space, which includes spaces or tab characters,
3702 before [zeros], [x] or [hexadecimal digit]. The running zero before [x] or
3703 [hexadecimal digit] will be ignored. Then, the decoding starts after [x] or the
3704 first valid hexadecimal digit. Then, the function stops at the first character that is
3705 a not a valid hexadecimal character or NULL, whichever one comes first.
3706
3707 If String has only pad spaces, then zero is returned.
3708 If String has no leading pad spaces, leading zeros or valid hexadecimal digits,
3709 then zero is returned.
3710
3711 @param[in] String A pointer to a Null-terminated Unicode string.
3712 @param[out] Value Upon a successful return the value of the conversion.
3713 @param[in] StopAtSpace FALSE to skip spaces.
3714
3715 @retval EFI_SUCCESS The conversion was successful.
3716 @retval EFI_INVALID_PARAMETER A parameter was NULL or invalid.
3717 @retval EFI_DEVICE_ERROR An overflow occured.
3718**/
3719EFI_STATUS
3720EFIAPI
3721InternalShellStrHexToUint64 (
3722 IN CONST CHAR16 *String,
3723 OUT UINT64 *Value,
3724 IN CONST BOOLEAN StopAtSpace
3725 )
3726{
3727 UINT64 Result;
3728
3729 if (String == NULL || StrSize(String) == 0 || Value == NULL) {
3730 return (EFI_INVALID_PARAMETER);
3731 }
darylm503b0934ac2011-11-12 00:35:11 +00003732
jcarsey252d9452011-03-25 20:49:53 +00003733 //
darylm503b0934ac2011-11-12 00:35:11 +00003734 // Ignore the pad spaces (space or tab)
jcarsey252d9452011-03-25 20:49:53 +00003735 //
3736 while ((*String == L' ') || (*String == L'\t')) {
3737 String++;
3738 }
3739
3740 //
3741 // Ignore leading Zeros after the spaces
3742 //
3743 while (*String == L'0') {
3744 String++;
3745 }
3746
3747 if (InternalShellCharToUpper (*String) == L'X') {
3748 if (*(String - 1) != L'0') {
3749 return 0;
3750 }
3751 //
3752 // Skip the 'X'
3753 //
3754 String++;
3755 }
3756
3757 Result = 0;
darylm503b0934ac2011-11-12 00:35:11 +00003758
jcarsey252d9452011-03-25 20:49:53 +00003759 //
ydong1012ea4692012-07-26 05:42:43 +00003760 // Skip spaces if requested
jcarsey252d9452011-03-25 20:49:53 +00003761 //
ydong1012ea4692012-07-26 05:42:43 +00003762 while (StopAtSpace && *String == L' ') {
3763 String++;
jcarsey252d9452011-03-25 20:49:53 +00003764 }
darylm503b0934ac2011-11-12 00:35:11 +00003765
jcarsey252d9452011-03-25 20:49:53 +00003766 while (ShellIsHexaDecimalDigitCharacter (*String)) {
3767 //
darylm503b0934ac2011-11-12 00:35:11 +00003768 // If the Hex Number represented by String overflows according
jcarsey252d9452011-03-25 20:49:53 +00003769 // to the range defined by UINTN, then ASSERT().
3770 //
3771 if (!(Result <= (RShiftU64((((UINT64) ~0) - InternalShellHexCharToUintn (*String)), 4)))) {
3772// if (!(Result <= ((((UINT64) ~0) - InternalShellHexCharToUintn (*String)) >> 4))) {
3773 return (EFI_DEVICE_ERROR);
3774 }
3775
3776 Result = (LShiftU64(Result, 4));
3777 Result += InternalShellHexCharToUintn (*String);
3778 String++;
3779
3780 //
jcarsey49bd4982012-01-27 18:42:43 +00003781 // stop at spaces if requested
jcarsey252d9452011-03-25 20:49:53 +00003782 //
jcarsey49bd4982012-01-27 18:42:43 +00003783 if (StopAtSpace && *String == L' ') {
3784 break;
jcarsey252d9452011-03-25 20:49:53 +00003785 }
3786 }
3787
3788 *Value = Result;
3789 return (EFI_SUCCESS);
3790}
3791
3792/**
3793 Convert a Null-terminated Unicode decimal string to a value of
3794 type UINT64.
3795
3796 This function returns a value of type UINT64 by interpreting the contents
3797 of the Unicode string specified by String as a decimal number. The format
3798 of the input Unicode string String is:
3799
3800 [spaces] [decimal digits].
3801
3802 The valid decimal digit character is in the range [0-9]. The
3803 function will ignore the pad space, which includes spaces or
3804 tab characters, before [decimal digits]. The running zero in the
3805 beginning of [decimal digits] will be ignored. Then, the function
3806 stops at the first character that is a not a valid decimal character
3807 or a Null-terminator, whichever one comes first.
3808
3809 If String has only pad spaces, then 0 is returned.
3810 If String has no pad spaces or valid decimal digits,
3811 then 0 is returned.
3812
3813 @param[in] String A pointer to a Null-terminated Unicode string.
3814 @param[out] Value Upon a successful return the value of the conversion.
3815 @param[in] StopAtSpace FALSE to skip spaces.
3816
3817 @retval EFI_SUCCESS The conversion was successful.
3818 @retval EFI_INVALID_PARAMETER A parameter was NULL or invalid.
3819 @retval EFI_DEVICE_ERROR An overflow occured.
3820**/
3821EFI_STATUS
3822EFIAPI
3823InternalShellStrDecimalToUint64 (
3824 IN CONST CHAR16 *String,
3825 OUT UINT64 *Value,
3826 IN CONST BOOLEAN StopAtSpace
3827 )
3828{
3829 UINT64 Result;
3830
3831 if (String == NULL || StrSize (String) == 0 || Value == NULL) {
3832 return (EFI_INVALID_PARAMETER);
3833 }
3834
3835 //
3836 // Ignore the pad spaces (space or tab)
3837 //
3838 while ((*String == L' ') || (*String == L'\t')) {
3839 String++;
3840 }
3841
3842 //
3843 // Ignore leading Zeros after the spaces
3844 //
3845 while (*String == L'0') {
3846 String++;
3847 }
3848
3849 Result = 0;
3850
3851 //
3852 // Skip spaces if requested
3853 //
3854 while (StopAtSpace && *String == L' ') {
3855 String++;
3856 }
3857 while (ShellIsDecimalDigitCharacter (*String)) {
3858 //
darylm503b0934ac2011-11-12 00:35:11 +00003859 // If the number represented by String overflows according
jcarsey252d9452011-03-25 20:49:53 +00003860 // to the range defined by UINT64, then ASSERT().
3861 //
darylm503b0934ac2011-11-12 00:35:11 +00003862
jcarsey252d9452011-03-25 20:49:53 +00003863 if (!(Result <= (DivU64x32((((UINT64) ~0) - (*String - L'0')),10)))) {
3864 return (EFI_DEVICE_ERROR);
3865 }
3866
3867 Result = MultU64x32(Result, 10) + (*String - L'0');
3868 String++;
3869
3870 //
3871 // Stop at spaces if requested
3872 //
3873 if (StopAtSpace && *String == L' ') {
3874 break;
3875 }
3876 }
3877
3878 *Value = Result;
darylm503b0934ac2011-11-12 00:35:11 +00003879
jcarsey252d9452011-03-25 20:49:53 +00003880 return (EFI_SUCCESS);
3881}
3882
3883/**
3884 Function to verify and convert a string to its numerical value.
3885
3886 If Hex it must be preceeded with a 0x, 0X, or has ForceHex set TRUE.
3887
3888 @param[in] String The string to evaluate.
3889 @param[out] Value Upon a successful return the value of the conversion.
3890 @param[in] ForceHex TRUE - always assume hex.
3891 @param[in] StopAtSpace FALSE to skip spaces.
darylm503b0934ac2011-11-12 00:35:11 +00003892
jcarsey252d9452011-03-25 20:49:53 +00003893 @retval EFI_SUCCESS The conversion was successful.
3894 @retval EFI_INVALID_PARAMETER String contained an invalid character.
3895 @retval EFI_NOT_FOUND String was a number, but Value was NULL.
3896**/
3897EFI_STATUS
3898EFIAPI
3899ShellConvertStringToUint64(
3900 IN CONST CHAR16 *String,
3901 OUT UINT64 *Value,
3902 IN CONST BOOLEAN ForceHex,
3903 IN CONST BOOLEAN StopAtSpace
3904 )
3905{
3906 UINT64 RetVal;
3907 CONST CHAR16 *Walker;
3908 EFI_STATUS Status;
3909 BOOLEAN Hex;
3910
3911 Hex = ForceHex;
3912
3913 if (!InternalShellIsHexOrDecimalNumber(String, Hex, StopAtSpace)) {
3914 if (!Hex) {
3915 Hex = TRUE;
3916 if (!InternalShellIsHexOrDecimalNumber(String, Hex, StopAtSpace)) {
3917 return (EFI_INVALID_PARAMETER);
3918 }
3919 } else {
3920 return (EFI_INVALID_PARAMETER);
3921 }
3922 }
3923
3924 //
3925 // Chop off leading spaces
3926 //
3927 for (Walker = String; Walker != NULL && *Walker != CHAR_NULL && *Walker == L' '; Walker++);
3928
3929 //
3930 // make sure we have something left that is numeric.
3931 //
3932 if (Walker == NULL || *Walker == CHAR_NULL || !InternalShellIsHexOrDecimalNumber(Walker, Hex, StopAtSpace)) {
3933 return (EFI_INVALID_PARAMETER);
darylm503b0934ac2011-11-12 00:35:11 +00003934 }
jcarsey252d9452011-03-25 20:49:53 +00003935
3936 //
3937 // do the conversion.
3938 //
3939 if (Hex || StrnCmp(Walker, L"0x", 2) == 0 || StrnCmp(Walker, L"0X", 2) == 0){
3940 Status = InternalShellStrHexToUint64(Walker, &RetVal, StopAtSpace);
3941 } else {
3942 Status = InternalShellStrDecimalToUint64(Walker, &RetVal, StopAtSpace);
3943 }
3944
3945 if (Value == NULL && !EFI_ERROR(Status)) {
3946 return (EFI_NOT_FOUND);
3947 }
3948
3949 if (Value != NULL) {
3950 *Value = RetVal;
3951 }
3952
3953 return (Status);
3954}
3955
3956/**
3957 Function to determin if an entire string is a valid number.
3958
3959 If Hex it must be preceeded with a 0x or has ForceHex, set TRUE.
3960
3961 @param[in] String The string to evaluate.
3962 @param[in] ForceHex TRUE - always assume hex.
3963 @param[in] StopAtSpace TRUE to halt upon finding a space, FALSE to keep going.
3964
3965 @retval TRUE It is all numeric (dec/hex) characters.
3966 @retval FALSE There is a non-numeric character.
3967**/
3968BOOLEAN
3969EFIAPI
3970ShellIsHexOrDecimalNumber (
3971 IN CONST CHAR16 *String,
3972 IN CONST BOOLEAN ForceHex,
3973 IN CONST BOOLEAN StopAtSpace
3974 )
3975{
3976 if (ShellConvertStringToUint64(String, NULL, ForceHex, StopAtSpace) == EFI_NOT_FOUND) {
3977 return (TRUE);
3978 }
3979 return (FALSE);
3980}
jcarsey4d0a4fc2011-07-06 22:28:36 +00003981
3982/**
3983 Function to read a single line from a SHELL_FILE_HANDLE. The \n is not included in the returned
3984 buffer. The returned buffer must be callee freed.
3985
3986 If the position upon start is 0, then the Ascii Boolean will be set. This should be
3987 maintained and not changed for all operations with the same file.
3988
ydong104ff7e372011-09-02 08:05:34 +00003989 @param[in] Handle SHELL_FILE_HANDLE to read from.
3990 @param[in, out] Ascii Boolean value for indicating whether the file is
3991 Ascii (TRUE) or UCS2 (FALSE).
jcarsey4d0a4fc2011-07-06 22:28:36 +00003992
jcarseybeab0fc2011-10-10 17:26:25 +00003993 @return The line of text from the file.
3994 @retval NULL There was not enough memory available.
jcarsey4d0a4fc2011-07-06 22:28:36 +00003995
3996 @sa ShellFileHandleReadLine
3997**/
3998CHAR16*
3999EFIAPI
4000ShellFileHandleReturnLine(
4001 IN SHELL_FILE_HANDLE Handle,
4002 IN OUT BOOLEAN *Ascii
4003 )
4004{
4005 CHAR16 *RetVal;
4006 UINTN Size;
4007 EFI_STATUS Status;
4008
4009 Size = 0;
4010 RetVal = NULL;
4011
4012 Status = ShellFileHandleReadLine(Handle, RetVal, &Size, FALSE, Ascii);
4013 if (Status == EFI_BUFFER_TOO_SMALL) {
4014 RetVal = AllocateZeroPool(Size);
jcarseybeab0fc2011-10-10 17:26:25 +00004015 if (RetVal == NULL) {
4016 return (NULL);
4017 }
jcarsey4d0a4fc2011-07-06 22:28:36 +00004018 Status = ShellFileHandleReadLine(Handle, RetVal, &Size, FALSE, Ascii);
darylm503b0934ac2011-11-12 00:35:11 +00004019
jcarsey4d0a4fc2011-07-06 22:28:36 +00004020 }
jcarsey4d0a4fc2011-07-06 22:28:36 +00004021 if (EFI_ERROR(Status) && (RetVal != NULL)) {
4022 FreePool(RetVal);
4023 RetVal = NULL;
4024 }
4025 return (RetVal);
4026}
4027
4028/**
4029 Function to read a single line (up to but not including the \n) from a SHELL_FILE_HANDLE.
4030
4031 If the position upon start is 0, then the Ascii Boolean will be set. This should be
4032 maintained and not changed for all operations with the same file.
4033
ydong104ff7e372011-09-02 08:05:34 +00004034 @param[in] Handle SHELL_FILE_HANDLE to read from.
4035 @param[in, out] Buffer The pointer to buffer to read into.
4036 @param[in, out] Size The pointer to number of bytes in Buffer.
4037 @param[in] Truncate If the buffer is large enough, this has no effect.
4038 If the buffer is is too small and Truncate is TRUE,
4039 the line will be truncated.
4040 If the buffer is is too small and Truncate is FALSE,
4041 then no read will occur.
jcarsey4d0a4fc2011-07-06 22:28:36 +00004042
ydong104ff7e372011-09-02 08:05:34 +00004043 @param[in, out] Ascii Boolean value for indicating whether the file is
4044 Ascii (TRUE) or UCS2 (FALSE).
jcarsey4d0a4fc2011-07-06 22:28:36 +00004045
4046 @retval EFI_SUCCESS The operation was successful. The line is stored in
4047 Buffer.
4048 @retval EFI_INVALID_PARAMETER Handle was NULL.
4049 @retval EFI_INVALID_PARAMETER Size was NULL.
4050 @retval EFI_BUFFER_TOO_SMALL Size was not large enough to store the line.
4051 Size was updated to the minimum space required.
4052**/
4053EFI_STATUS
4054EFIAPI
4055ShellFileHandleReadLine(
4056 IN SHELL_FILE_HANDLE Handle,
4057 IN OUT CHAR16 *Buffer,
4058 IN OUT UINTN *Size,
4059 IN BOOLEAN Truncate,
4060 IN OUT BOOLEAN *Ascii
4061 )
4062{
4063 EFI_STATUS Status;
4064 CHAR16 CharBuffer;
4065 UINTN CharSize;
4066 UINTN CountSoFar;
4067 UINT64 OriginalFilePosition;
4068
4069
4070 if (Handle == NULL
4071 ||Size == NULL
4072 ){
4073 return (EFI_INVALID_PARAMETER);
4074 }
4075 if (Buffer == NULL) {
4076 ASSERT(*Size == 0);
4077 } else {
4078 *Buffer = CHAR_NULL;
4079 }
4080 gEfiShellProtocol->GetFilePosition(Handle, &OriginalFilePosition);
4081 if (OriginalFilePosition == 0) {
4082 CharSize = sizeof(CHAR16);
4083 Status = gEfiShellProtocol->ReadFile(Handle, &CharSize, &CharBuffer);
4084 ASSERT_EFI_ERROR(Status);
4085 if (CharBuffer == gUnicodeFileTag) {
4086 *Ascii = FALSE;
4087 } else {
4088 *Ascii = TRUE;
4089 gEfiShellProtocol->SetFilePosition(Handle, OriginalFilePosition);
4090 }
4091 }
4092
4093 for (CountSoFar = 0;;CountSoFar++){
4094 CharBuffer = 0;
4095 if (*Ascii) {
4096 CharSize = sizeof(CHAR8);
4097 } else {
4098 CharSize = sizeof(CHAR16);
4099 }
4100 Status = gEfiShellProtocol->ReadFile(Handle, &CharSize, &CharBuffer);
4101 if ( EFI_ERROR(Status)
4102 || CharSize == 0
4103 || (CharBuffer == L'\n' && !(*Ascii))
4104 || (CharBuffer == '\n' && *Ascii)
4105 ){
4106 break;
4107 }
4108 //
4109 // if we have space save it...
4110 //
4111 if ((CountSoFar+1)*sizeof(CHAR16) < *Size){
4112 ASSERT(Buffer != NULL);
4113 ((CHAR16*)Buffer)[CountSoFar] = CharBuffer;
4114 ((CHAR16*)Buffer)[CountSoFar+1] = CHAR_NULL;
4115 }
4116 }
4117
4118 //
4119 // if we ran out of space tell when...
4120 //
4121 if ((CountSoFar+1)*sizeof(CHAR16) > *Size){
4122 *Size = (CountSoFar+1)*sizeof(CHAR16);
4123 if (!Truncate) {
4124 gEfiShellProtocol->SetFilePosition(Handle, OriginalFilePosition);
4125 } else {
4126 DEBUG((DEBUG_WARN, "The line was truncated in ShellFileHandleReadLine"));
4127 }
4128 return (EFI_BUFFER_TOO_SMALL);
4129 }
4130 while(Buffer[StrLen(Buffer)-1] == L'\r') {
4131 Buffer[StrLen(Buffer)-1] = CHAR_NULL;
4132 }
4133
4134 return (Status);
4135}
jcarseyfb5278e2013-02-20 18:21:14 +00004136
4137/**
jcarsey365aa982013-03-04 21:54:02 +00004138 Function to print help file / man page content in the spec from the UEFI Shell protocol GetHelpText function.
4139
4140 @param[in] CommandToGetHelpOn Pointer to a string containing the command name of help file to be printed.
4141 @param[in] SectionToGetHelpOn Pointer to the section specifier(s).
4142 @param[in] PrintCommandText If TRUE, prints the command followed by the help content, otherwise prints
4143 the help content only.
4144 @retval EFI_DEVICE_ERROR The help data format was incorrect.
4145 @retval EFI_NOT_FOUND The help data could not be found.
4146 @retval EFI_SUCCESS The operation was successful.
4147**/
4148EFI_STATUS
4149EFIAPI
4150ShellPrintHelp (
4151 IN CONST CHAR16 *CommandToGetHelpOn,
4152 IN CONST CHAR16 *SectionToGetHelpOn,
4153 IN BOOLEAN PrintCommandText
4154 )
4155{
4156 EFI_STATUS Status;
4157 CHAR16 *OutText;
4158
4159 OutText = NULL;
4160
4161 //
4162 // Get the string to print based
4163 //
4164 Status = gEfiShellProtocol->GetHelpText (CommandToGetHelpOn, SectionToGetHelpOn, &OutText);
4165
4166 //
4167 // make sure we got a valid string
4168 //
4169 if (EFI_ERROR(Status)){
4170 return Status;
4171 }
4172 if (OutText == NULL || StrLen(OutText) == 0) {
4173 return EFI_NOT_FOUND;
4174 }
4175
4176 //
4177 // Chop off trailing stuff we dont need
4178 //
4179 while (OutText[StrLen(OutText)-1] == L'\r' || OutText[StrLen(OutText)-1] == L'\n' || OutText[StrLen(OutText)-1] == L' ') {
4180 OutText[StrLen(OutText)-1] = CHAR_NULL;
4181 }
4182
4183 //
4184 // Print this out to the console
4185 //
4186 if (PrintCommandText) {
4187 ShellPrintEx(-1, -1, L"%H%-14s%N- %s\r\n", CommandToGetHelpOn, OutText);
4188 } else {
4189 ShellPrintEx(-1, -1, L"%N%s\r\n", OutText);
4190 }
4191
4192 SHELL_FREE_NON_NULL(OutText);
4193
4194 return EFI_SUCCESS;
4195}
4196
4197/**
jcarseyfb5278e2013-02-20 18:21:14 +00004198 Function to delete a file by name
4199
4200 @param[in] FileName Pointer to file name to delete.
4201
4202 @retval EFI_SUCCESS the file was deleted sucessfully
4203 @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not
4204 deleted
4205 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
4206 @retval EFI_NOT_FOUND The specified file could not be found on the
4207 device or the file system could not be found
4208 on the device.
4209 @retval EFI_NO_MEDIA The device has no medium.
4210 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
4211 medium is no longer supported.
4212 @retval EFI_DEVICE_ERROR The device reported an error.
4213 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
4214 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
4215 @retval EFI_ACCESS_DENIED The file was opened read only.
4216 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
4217 file.
4218 @retval other The file failed to open
4219**/
4220EFI_STATUS
4221EFIAPI
4222ShellDeleteFileByName(
4223 IN CONST CHAR16 *FileName
4224 )
4225{
4226 EFI_STATUS Status;
4227 SHELL_FILE_HANDLE FileHandle;
4228
4229 Status = ShellFileExists(FileName);
4230
4231 if (Status == EFI_SUCCESS){
4232 Status = ShellOpenFileByName(FileName, &FileHandle, EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE, 0x0);
4233 if (Status == EFI_SUCCESS){
4234 Status = ShellDeleteFile(&FileHandle);
4235 }
4236 }
4237
4238 return(Status);
4239
4240}