blob: 53f54e1746d48bb1e41f06c288477f227e1f08be [file] [log] [blame]
jcarsey94b17fa2009-05-07 18:46:18 +00001/** @file
2 Provides interface to shell functionality for shell commands and applications.
3
Tapan Shah583448b2016-09-22 12:12:47 -07004 (C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>
jaben carsey9ed21942016-02-08 15:59:04 -08005 Copyright 2016 Dell Inc.
Qiu Shuminf79d7b62016-02-18 13:12:58 +08006 Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>
jcarsey1e6e84c2010-01-25 20:05:08 +00007 This program and the accompanying materials
jcarseyb3011f42010-01-11 21:49:04 +00008 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
jcarsey94b17fa2009-05-07 18:46:18 +000011
jcarseyb3011f42010-01-11 21:49:04 +000012 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
jcarsey94b17fa2009-05-07 18:46:18 +000014
15**/
16
jcarseyb1f95a02009-06-16 00:23:19 +000017#include "UefiShellLib.h"
jcarseya405b862010-09-14 05:18:09 +000018#include <ShellBase.h>
jcarsey252d9452011-03-25 20:49:53 +000019#include <Library/SortLib.h>
Laszlo Ersek3fe23dc2015-01-14 16:25:48 +000020#include <Library/BaseLib.h>
jcarseyd2b45642009-05-11 18:02:16 +000021
jcarsey94b17fa2009-05-07 18:46:18 +000022#define FIND_XXXXX_FILE_BUFFER_SIZE (SIZE_OF_EFI_FILE_INFO + MAX_FILE_NAME_LEN)
23
jcarseyd2b45642009-05-11 18:02:16 +000024//
jcarseya405b862010-09-14 05:18:09 +000025// globals...
jcarseyd2b45642009-05-11 18:02:16 +000026//
27SHELL_PARAM_ITEM EmptyParamList[] = {
28 {NULL, TypeMax}
29 };
jcarseya405b862010-09-14 05:18:09 +000030SHELL_PARAM_ITEM SfoParamList[] = {
31 {L"-sfo", TypeFlag},
32 {NULL, TypeMax}
33 };
34EFI_SHELL_ENVIRONMENT2 *mEfiShellEnvironment2;
35EFI_SHELL_INTERFACE *mEfiShellInterface;
jcarsey366f81a2011-06-27 21:04:22 +000036EFI_SHELL_PROTOCOL *gEfiShellProtocol;
37EFI_SHELL_PARAMETERS_PROTOCOL *gEfiShellParametersProtocol;
jcarseya405b862010-09-14 05:18:09 +000038EFI_HANDLE mEfiShellEnvironment2Handle;
39FILE_HANDLE_FUNCTION_MAP FileFunctionMap;
Tapan Shah583448b2016-09-22 12:12:47 -070040EFI_UNICODE_COLLATION_PROTOCOL *mUnicodeCollationProtocol;
jcarseyb3011f42010-01-11 21:49:04 +000041
jcarsey2247dde2009-11-09 18:08:58 +000042/**
43 Check if a Unicode character is a hexadecimal character.
44
jcarsey1e6e84c2010-01-25 20:05:08 +000045 This internal function checks if a Unicode character is a
jcarseya405b862010-09-14 05:18:09 +000046 numeric character. The valid hexadecimal characters are
jcarsey2247dde2009-11-09 18:08:58 +000047 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.
48
jcarsey2247dde2009-11-09 18:08:58 +000049 @param Char The character to check against.
50
51 @retval TRUE If the Char is a hexadecmial character.
52 @retval FALSE If the Char is not a hexadecmial character.
53
54**/
55BOOLEAN
56EFIAPI
jcarsey969c7832010-01-13 16:46:33 +000057ShellIsHexaDecimalDigitCharacter (
jcarsey2247dde2009-11-09 18:08:58 +000058 IN CHAR16 Char
jcarseya405b862010-09-14 05:18:09 +000059 )
60{
jcarsey2247dde2009-11-09 18:08:58 +000061 return (BOOLEAN) ((Char >= L'0' && Char <= L'9') || (Char >= L'A' && Char <= L'F') || (Char >= L'a' && Char <= L'f'));
62}
jcarsey94b17fa2009-05-07 18:46:18 +000063
64/**
jcarseya405b862010-09-14 05:18:09 +000065 Check if a Unicode character is a decimal character.
66
67 This internal function checks if a Unicode character is a
68 decimal character. The valid characters are
69 L'0' to L'9'.
70
71
72 @param Char The character to check against.
73
74 @retval TRUE If the Char is a hexadecmial character.
75 @retval FALSE If the Char is not a hexadecmial character.
76
77**/
78BOOLEAN
79EFIAPI
80ShellIsDecimalDigitCharacter (
81 IN CHAR16 Char
82 )
83{
84 return (BOOLEAN) (Char >= L'0' && Char <= L'9');
85}
86
87/**
88 Helper function to find ShellEnvironment2 for constructor.
89
90 @param[in] ImageHandle A copy of the calling image's handle.
jcarseybeab0fc2011-10-10 17:26:25 +000091
92 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.
jcarsey94b17fa2009-05-07 18:46:18 +000093**/
94EFI_STATUS
95EFIAPI
96ShellFindSE2 (
97 IN EFI_HANDLE ImageHandle
jcarseya405b862010-09-14 05:18:09 +000098 )
99{
jcarsey94b17fa2009-05-07 18:46:18 +0000100 EFI_STATUS Status;
101 EFI_HANDLE *Buffer;
102 UINTN BufferSize;
103 UINTN HandleIndex;
104
105 BufferSize = 0;
106 Buffer = NULL;
jcarsey1e6e84c2010-01-25 20:05:08 +0000107 Status = gBS->OpenProtocol(ImageHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000108 &gEfiShellEnvironment2Guid,
109 (VOID **)&mEfiShellEnvironment2,
110 ImageHandle,
111 NULL,
112 EFI_OPEN_PROTOCOL_GET_PROTOCOL
jcarseya405b862010-09-14 05:18:09 +0000113 );
jcarsey94b17fa2009-05-07 18:46:18 +0000114 //
115 // look for the mEfiShellEnvironment2 protocol at a higher level
116 //
jcarseya405b862010-09-14 05:18:09 +0000117 if (EFI_ERROR (Status) || !(CompareGuid (&mEfiShellEnvironment2->SESGuid, &gEfiShellEnvironment2ExtGuid))){
jcarsey94b17fa2009-05-07 18:46:18 +0000118 //
119 // figure out how big of a buffer we need.
120 //
121 Status = gBS->LocateHandle (ByProtocol,
122 &gEfiShellEnvironment2Guid,
123 NULL, // ignored for ByProtocol
124 &BufferSize,
125 Buffer
jcarseya405b862010-09-14 05:18:09 +0000126 );
jcarsey2247dde2009-11-09 18:08:58 +0000127 //
128 // maybe it's not there???
129 //
130 if (Status == EFI_BUFFER_TOO_SMALL) {
jcarsey252d9452011-03-25 20:49:53 +0000131 Buffer = (EFI_HANDLE*)AllocateZeroPool(BufferSize);
jcarseybeab0fc2011-10-10 17:26:25 +0000132 if (Buffer == NULL) {
133 return (EFI_OUT_OF_RESOURCES);
134 }
jcarsey2247dde2009-11-09 18:08:58 +0000135 Status = gBS->LocateHandle (ByProtocol,
136 &gEfiShellEnvironment2Guid,
137 NULL, // ignored for ByProtocol
138 &BufferSize,
139 Buffer
jcarseya405b862010-09-14 05:18:09 +0000140 );
jcarsey2247dde2009-11-09 18:08:58 +0000141 }
jcarsey1cd45e72010-01-29 15:07:44 +0000142 if (!EFI_ERROR (Status) && Buffer != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +0000143 //
144 // now parse the list of returned handles
145 //
146 Status = EFI_NOT_FOUND;
147 for (HandleIndex = 0; HandleIndex < (BufferSize/sizeof(Buffer[0])); HandleIndex++) {
jcarsey1e6e84c2010-01-25 20:05:08 +0000148 Status = gBS->OpenProtocol(Buffer[HandleIndex],
jcarsey94b17fa2009-05-07 18:46:18 +0000149 &gEfiShellEnvironment2Guid,
150 (VOID **)&mEfiShellEnvironment2,
151 ImageHandle,
152 NULL,
153 EFI_OPEN_PROTOCOL_GET_PROTOCOL
jcarseya405b862010-09-14 05:18:09 +0000154 );
155 if (CompareGuid (&mEfiShellEnvironment2->SESGuid, &gEfiShellEnvironment2ExtGuid)) {
jcarsey94b17fa2009-05-07 18:46:18 +0000156 mEfiShellEnvironment2Handle = Buffer[HandleIndex];
157 Status = EFI_SUCCESS;
158 break;
159 }
160 }
161 }
162 }
163 if (Buffer != NULL) {
164 FreePool (Buffer);
165 }
166 return (Status);
167}
168
jcarsey252d9452011-03-25 20:49:53 +0000169/**
darylm503b0934ac2011-11-12 00:35:11 +0000170 Function to do most of the work of the constructor. Allows for calling
jcarseya405b862010-09-14 05:18:09 +0000171 multiple times without complete re-initialization.
172
173 @param[in] ImageHandle A copy of the ImageHandle.
174 @param[in] SystemTable A pointer to the SystemTable for the application.
jcarsey252d9452011-03-25 20:49:53 +0000175
176 @retval EFI_SUCCESS The operationw as successful.
jcarseya405b862010-09-14 05:18:09 +0000177**/
jcarsey94b17fa2009-05-07 18:46:18 +0000178EFI_STATUS
179EFIAPI
jcarseyd2b45642009-05-11 18:02:16 +0000180ShellLibConstructorWorker (
jcarsey94b17fa2009-05-07 18:46:18 +0000181 IN EFI_HANDLE ImageHandle,
182 IN EFI_SYSTEM_TABLE *SystemTable
jcarseya405b862010-09-14 05:18:09 +0000183 )
184{
185 EFI_STATUS Status;
jcarseyecd3d592009-12-07 18:05:00 +0000186
jcarsey94b17fa2009-05-07 18:46:18 +0000187 //
188 // UEFI 2.0 shell interfaces (used preferentially)
189 //
jcarseya405b862010-09-14 05:18:09 +0000190 Status = gBS->OpenProtocol(
191 ImageHandle,
192 &gEfiShellProtocolGuid,
jcarsey366f81a2011-06-27 21:04:22 +0000193 (VOID **)&gEfiShellProtocol,
jcarseya405b862010-09-14 05:18:09 +0000194 ImageHandle,
195 NULL,
196 EFI_OPEN_PROTOCOL_GET_PROTOCOL
197 );
jcarsey94b17fa2009-05-07 18:46:18 +0000198 if (EFI_ERROR(Status)) {
jcarseya405b862010-09-14 05:18:09 +0000199 //
200 // Search for the shell protocol
201 //
202 Status = gBS->LocateProtocol(
203 &gEfiShellProtocolGuid,
204 NULL,
jcarsey366f81a2011-06-27 21:04:22 +0000205 (VOID **)&gEfiShellProtocol
jcarseya405b862010-09-14 05:18:09 +0000206 );
207 if (EFI_ERROR(Status)) {
jcarsey366f81a2011-06-27 21:04:22 +0000208 gEfiShellProtocol = NULL;
jcarseya405b862010-09-14 05:18:09 +0000209 }
jcarsey94b17fa2009-05-07 18:46:18 +0000210 }
jcarseya405b862010-09-14 05:18:09 +0000211 Status = gBS->OpenProtocol(
212 ImageHandle,
213 &gEfiShellParametersProtocolGuid,
jcarsey366f81a2011-06-27 21:04:22 +0000214 (VOID **)&gEfiShellParametersProtocol,
jcarseya405b862010-09-14 05:18:09 +0000215 ImageHandle,
216 NULL,
217 EFI_OPEN_PROTOCOL_GET_PROTOCOL
218 );
jcarsey94b17fa2009-05-07 18:46:18 +0000219 if (EFI_ERROR(Status)) {
jcarsey366f81a2011-06-27 21:04:22 +0000220 gEfiShellParametersProtocol = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +0000221 }
222
jcarsey366f81a2011-06-27 21:04:22 +0000223 if (gEfiShellParametersProtocol == NULL || gEfiShellProtocol == NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +0000224 //
225 // Moved to seperate function due to complexity
226 //
227 Status = ShellFindSE2(ImageHandle);
228
229 if (EFI_ERROR(Status)) {
230 DEBUG((DEBUG_ERROR, "Status: 0x%08x\r\n", Status));
231 mEfiShellEnvironment2 = NULL;
232 }
jcarsey1e6e84c2010-01-25 20:05:08 +0000233 Status = gBS->OpenProtocol(ImageHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000234 &gEfiShellInterfaceGuid,
235 (VOID **)&mEfiShellInterface,
236 ImageHandle,
237 NULL,
238 EFI_OPEN_PROTOCOL_GET_PROTOCOL
jcarseya405b862010-09-14 05:18:09 +0000239 );
jcarsey94b17fa2009-05-07 18:46:18 +0000240 if (EFI_ERROR(Status)) {
241 mEfiShellInterface = NULL;
242 }
243 }
jcarseyc9d92df2010-02-03 15:37:54 +0000244
jcarsey94b17fa2009-05-07 18:46:18 +0000245 //
246 // only success getting 2 of either the old or new, but no 1/2 and 1/2
247 //
jcarsey1e6e84c2010-01-25 20:05:08 +0000248 if ((mEfiShellEnvironment2 != NULL && mEfiShellInterface != NULL) ||
jcarsey366f81a2011-06-27 21:04:22 +0000249 (gEfiShellProtocol != NULL && gEfiShellParametersProtocol != NULL) ) {
250 if (gEfiShellProtocol != NULL) {
251 FileFunctionMap.GetFileInfo = gEfiShellProtocol->GetFileInfo;
252 FileFunctionMap.SetFileInfo = gEfiShellProtocol->SetFileInfo;
253 FileFunctionMap.ReadFile = gEfiShellProtocol->ReadFile;
254 FileFunctionMap.WriteFile = gEfiShellProtocol->WriteFile;
255 FileFunctionMap.CloseFile = gEfiShellProtocol->CloseFile;
256 FileFunctionMap.DeleteFile = gEfiShellProtocol->DeleteFile;
257 FileFunctionMap.GetFilePosition = gEfiShellProtocol->GetFilePosition;
258 FileFunctionMap.SetFilePosition = gEfiShellProtocol->SetFilePosition;
259 FileFunctionMap.FlushFile = gEfiShellProtocol->FlushFile;
260 FileFunctionMap.GetFileSize = gEfiShellProtocol->GetFileSize;
jcarseyd2b45642009-05-11 18:02:16 +0000261 } else {
jcarseya405b862010-09-14 05:18:09 +0000262 FileFunctionMap.GetFileInfo = (EFI_SHELL_GET_FILE_INFO)FileHandleGetInfo;
263 FileFunctionMap.SetFileInfo = (EFI_SHELL_SET_FILE_INFO)FileHandleSetInfo;
264 FileFunctionMap.ReadFile = (EFI_SHELL_READ_FILE)FileHandleRead;
265 FileFunctionMap.WriteFile = (EFI_SHELL_WRITE_FILE)FileHandleWrite;
266 FileFunctionMap.CloseFile = (EFI_SHELL_CLOSE_FILE)FileHandleClose;
267 FileFunctionMap.DeleteFile = (EFI_SHELL_DELETE_FILE)FileHandleDelete;
268 FileFunctionMap.GetFilePosition = (EFI_SHELL_GET_FILE_POSITION)FileHandleGetPosition;
269 FileFunctionMap.SetFilePosition = (EFI_SHELL_SET_FILE_POSITION)FileHandleSetPosition;
270 FileFunctionMap.FlushFile = (EFI_SHELL_FLUSH_FILE)FileHandleFlush;
271 FileFunctionMap.GetFileSize = (EFI_SHELL_GET_FILE_SIZE)FileHandleGetSize;
jcarseyd2b45642009-05-11 18:02:16 +0000272 }
jcarsey94b17fa2009-05-07 18:46:18 +0000273 return (EFI_SUCCESS);
274 }
275 return (EFI_NOT_FOUND);
276}
jcarseyd2b45642009-05-11 18:02:16 +0000277/**
278 Constructor for the Shell library.
279
280 Initialize the library and determine if the underlying is a UEFI Shell 2.0 or an EFI shell.
281
282 @param ImageHandle the image handle of the process
283 @param SystemTable the EFI System Table pointer
284
285 @retval EFI_SUCCESS the initialization was complete sucessfully
286 @return others an error ocurred during initialization
287**/
288EFI_STATUS
289EFIAPI
290ShellLibConstructor (
291 IN EFI_HANDLE ImageHandle,
292 IN EFI_SYSTEM_TABLE *SystemTable
jcarseya405b862010-09-14 05:18:09 +0000293 )
294{
jcarseyd2b45642009-05-11 18:02:16 +0000295 mEfiShellEnvironment2 = NULL;
jcarsey366f81a2011-06-27 21:04:22 +0000296 gEfiShellProtocol = NULL;
297 gEfiShellParametersProtocol = NULL;
jcarseyd2b45642009-05-11 18:02:16 +0000298 mEfiShellInterface = NULL;
299 mEfiShellEnvironment2Handle = NULL;
Tapan Shah2cf9ecd2016-10-05 13:58:05 -0700300 mUnicodeCollationProtocol = NULL;
Tapan Shah583448b2016-09-22 12:12:47 -0700301
jcarseyd2b45642009-05-11 18:02:16 +0000302 //
303 // verify that auto initialize is not set false
jcarsey1e6e84c2010-01-25 20:05:08 +0000304 //
jcarseyd2b45642009-05-11 18:02:16 +0000305 if (PcdGetBool(PcdShellLibAutoInitialize) == 0) {
306 return (EFI_SUCCESS);
307 }
jcarsey1e6e84c2010-01-25 20:05:08 +0000308
jcarseyd2b45642009-05-11 18:02:16 +0000309 return (ShellLibConstructorWorker(ImageHandle, SystemTable));
310}
jcarsey94b17fa2009-05-07 18:46:18 +0000311
312/**
jcarseya405b862010-09-14 05:18:09 +0000313 Destructor for the library. free any resources.
314
315 @param[in] ImageHandle A copy of the ImageHandle.
316 @param[in] SystemTable A pointer to the SystemTable for the application.
317
318 @retval EFI_SUCCESS The operation was successful.
319 @return An error from the CloseProtocol function.
jcarsey94b17fa2009-05-07 18:46:18 +0000320**/
321EFI_STATUS
322EFIAPI
323ShellLibDestructor (
324 IN EFI_HANDLE ImageHandle,
325 IN EFI_SYSTEM_TABLE *SystemTable
jcarseya405b862010-09-14 05:18:09 +0000326 )
327{
jcarsey94b17fa2009-05-07 18:46:18 +0000328 if (mEfiShellEnvironment2 != NULL) {
329 gBS->CloseProtocol(mEfiShellEnvironment2Handle==NULL?ImageHandle:mEfiShellEnvironment2Handle,
330 &gEfiShellEnvironment2Guid,
331 ImageHandle,
332 NULL);
jcarseyd2b45642009-05-11 18:02:16 +0000333 mEfiShellEnvironment2 = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +0000334 }
335 if (mEfiShellInterface != NULL) {
336 gBS->CloseProtocol(ImageHandle,
337 &gEfiShellInterfaceGuid,
338 ImageHandle,
jcarsey1e6e84c2010-01-25 20:05:08 +0000339 NULL);
jcarseyd2b45642009-05-11 18:02:16 +0000340 mEfiShellInterface = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +0000341 }
jcarsey366f81a2011-06-27 21:04:22 +0000342 if (gEfiShellProtocol != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +0000343 gBS->CloseProtocol(ImageHandle,
344 &gEfiShellProtocolGuid,
345 ImageHandle,
jcarsey1e6e84c2010-01-25 20:05:08 +0000346 NULL);
jcarsey366f81a2011-06-27 21:04:22 +0000347 gEfiShellProtocol = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +0000348 }
jcarsey366f81a2011-06-27 21:04:22 +0000349 if (gEfiShellParametersProtocol != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +0000350 gBS->CloseProtocol(ImageHandle,
351 &gEfiShellParametersProtocolGuid,
352 ImageHandle,
jcarsey1e6e84c2010-01-25 20:05:08 +0000353 NULL);
jcarsey366f81a2011-06-27 21:04:22 +0000354 gEfiShellParametersProtocol = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +0000355 }
jcarseyd2b45642009-05-11 18:02:16 +0000356 mEfiShellEnvironment2Handle = NULL;
jcarseyecd3d592009-12-07 18:05:00 +0000357
jcarsey94b17fa2009-05-07 18:46:18 +0000358 return (EFI_SUCCESS);
359}
jcarseyd2b45642009-05-11 18:02:16 +0000360
361/**
362 This function causes the shell library to initialize itself. If the shell library
363 is already initialized it will de-initialize all the current protocol poitners and
364 re-populate them again.
365
366 When the library is used with PcdShellLibAutoInitialize set to true this function
367 will return EFI_SUCCESS and perform no actions.
368
369 This function is intended for internal access for shell commands only.
370
371 @retval EFI_SUCCESS the initialization was complete sucessfully
372
373**/
374EFI_STATUS
375EFIAPI
376ShellInitialize (
jcarseya405b862010-09-14 05:18:09 +0000377 )
378{
Laszlo Ersek4a7518d2016-06-28 13:52:11 +0200379 EFI_STATUS Status;
380
jcarseyd2b45642009-05-11 18:02:16 +0000381 //
382 // if auto initialize is not false then skip
383 //
384 if (PcdGetBool(PcdShellLibAutoInitialize) != 0) {
385 return (EFI_SUCCESS);
386 }
387
388 //
389 // deinit the current stuff
390 //
Laszlo Ersek4a7518d2016-06-28 13:52:11 +0200391 Status = ShellLibDestructor (gImageHandle, gST);
392 ASSERT_EFI_ERROR (Status);
jcarseyd2b45642009-05-11 18:02:16 +0000393
394 //
395 // init the new stuff
396 //
397 return (ShellLibConstructorWorker(gImageHandle, gST));
398}
399
jcarsey94b17fa2009-05-07 18:46:18 +0000400/**
jcarsey1e6e84c2010-01-25 20:05:08 +0000401 This function will retrieve the information about the file for the handle
jcarsey94b17fa2009-05-07 18:46:18 +0000402 specified and store it in allocated pool memory.
403
jcarsey1e6e84c2010-01-25 20:05:08 +0000404 This function allocates a buffer to store the file's information. It is the
qhuang869817bf2009-05-20 14:42:48 +0000405 caller's responsibility to free the buffer
jcarsey94b17fa2009-05-07 18:46:18 +0000406
jcarsey1e6e84c2010-01-25 20:05:08 +0000407 @param FileHandle The file handle of the file for which information is
jcarsey94b17fa2009-05-07 18:46:18 +0000408 being requested.
409
410 @retval NULL information could not be retrieved.
411
412 @return the information about the file
413**/
414EFI_FILE_INFO*
415EFIAPI
416ShellGetFileInfo (
jcarseya405b862010-09-14 05:18:09 +0000417 IN SHELL_FILE_HANDLE FileHandle
418 )
419{
jcarseyd2b45642009-05-11 18:02:16 +0000420 return (FileFunctionMap.GetFileInfo(FileHandle));
jcarsey94b17fa2009-05-07 18:46:18 +0000421}
422
423/**
jcarseya405b862010-09-14 05:18:09 +0000424 This function sets the information about the file for the opened handle
jcarsey94b17fa2009-05-07 18:46:18 +0000425 specified.
426
jcarseya405b862010-09-14 05:18:09 +0000427 @param[in] FileHandle The file handle of the file for which information
428 is being set.
jcarsey94b17fa2009-05-07 18:46:18 +0000429
jcarseya405b862010-09-14 05:18:09 +0000430 @param[in] FileInfo The information to set.
jcarsey94b17fa2009-05-07 18:46:18 +0000431
darylm503b0934ac2011-11-12 00:35:11 +0000432 @retval EFI_SUCCESS The information was set.
jcarseya405b862010-09-14 05:18:09 +0000433 @retval EFI_INVALID_PARAMETER A parameter was out of range or invalid.
434 @retval EFI_UNSUPPORTED The FileHandle does not support FileInfo.
darylm503b0934ac2011-11-12 00:35:11 +0000435 @retval EFI_NO_MEDIA The device has no medium.
436 @retval EFI_DEVICE_ERROR The device reported an error.
437 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
438 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
jcarseya405b862010-09-14 05:18:09 +0000439 @retval EFI_ACCESS_DENIED The file was opened read only.
440 @retval EFI_VOLUME_FULL The volume is full.
jcarsey94b17fa2009-05-07 18:46:18 +0000441**/
442EFI_STATUS
443EFIAPI
444ShellSetFileInfo (
darylm503b0934ac2011-11-12 00:35:11 +0000445 IN SHELL_FILE_HANDLE FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000446 IN EFI_FILE_INFO *FileInfo
jcarseya405b862010-09-14 05:18:09 +0000447 )
448{
jcarseyd2b45642009-05-11 18:02:16 +0000449 return (FileFunctionMap.SetFileInfo(FileHandle, FileInfo));
jcarsey1e6e84c2010-01-25 20:05:08 +0000450}
451
jcarsey94b17fa2009-05-07 18:46:18 +0000452 /**
453 This function will open a file or directory referenced by DevicePath.
454
jcarsey1e6e84c2010-01-25 20:05:08 +0000455 This function opens a file with the open mode according to the file path. The
jcarsey94b17fa2009-05-07 18:46:18 +0000456 Attributes is valid only for EFI_FILE_MODE_CREATE.
457
darylm503b0934ac2011-11-12 00:35:11 +0000458 @param FilePath on input the device path to the file. On output
jcarsey94b17fa2009-05-07 18:46:18 +0000459 the remaining device path.
darylm503b0934ac2011-11-12 00:35:11 +0000460 @param DeviceHandle pointer to the system device handle.
461 @param FileHandle pointer to the file handle.
462 @param OpenMode the mode to open the file with.
463 @param Attributes the file's file attributes.
jcarsey94b17fa2009-05-07 18:46:18 +0000464
darylm503b0934ac2011-11-12 00:35:11 +0000465 @retval EFI_SUCCESS The information was set.
466 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
467 @retval EFI_UNSUPPORTED Could not open the file path.
468 @retval EFI_NOT_FOUND The specified file could not be found on the
jcarsey1e6e84c2010-01-25 20:05:08 +0000469 device or the file system could not be found on
jcarsey94b17fa2009-05-07 18:46:18 +0000470 the device.
darylm503b0934ac2011-11-12 00:35:11 +0000471 @retval EFI_NO_MEDIA The device has no medium.
472 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
jcarsey94b17fa2009-05-07 18:46:18 +0000473 medium is no longer supported.
darylm503b0934ac2011-11-12 00:35:11 +0000474 @retval EFI_DEVICE_ERROR The device reported an error.
475 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
476 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
477 @retval EFI_ACCESS_DENIED The file was opened read only.
478 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
jcarsey94b17fa2009-05-07 18:46:18 +0000479 file.
darylm503b0934ac2011-11-12 00:35:11 +0000480 @retval EFI_VOLUME_FULL The volume is full.
jcarsey94b17fa2009-05-07 18:46:18 +0000481**/
482EFI_STATUS
483EFIAPI
484ShellOpenFileByDevicePath(
darylm503b0934ac2011-11-12 00:35:11 +0000485 IN OUT EFI_DEVICE_PATH_PROTOCOL **FilePath,
486 OUT EFI_HANDLE *DeviceHandle,
jcarseya405b862010-09-14 05:18:09 +0000487 OUT SHELL_FILE_HANDLE *FileHandle,
darylm503b0934ac2011-11-12 00:35:11 +0000488 IN UINT64 OpenMode,
489 IN UINT64 Attributes
jcarseya405b862010-09-14 05:18:09 +0000490 )
491{
492 CHAR16 *FileName;
493 EFI_STATUS Status;
jcarsey94b17fa2009-05-07 18:46:18 +0000494 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *EfiSimpleFileSystemProtocol;
jcarseya405b862010-09-14 05:18:09 +0000495 EFI_FILE_PROTOCOL *Handle1;
496 EFI_FILE_PROTOCOL *Handle2;
ydong100b6cb332013-01-25 02:00:22 +0000497 CHAR16 *FnafPathName;
498 UINTN PathLen;
jcarsey94b17fa2009-05-07 18:46:18 +0000499
jcarsey92a54472011-06-27 20:33:13 +0000500 if (FilePath == NULL || FileHandle == NULL || DeviceHandle == NULL) {
501 return (EFI_INVALID_PARAMETER);
502 }
503
jcarsey1e6e84c2010-01-25 20:05:08 +0000504 //
jcarsey94b17fa2009-05-07 18:46:18 +0000505 // which shell interface should we use
506 //
jcarsey366f81a2011-06-27 21:04:22 +0000507 if (gEfiShellProtocol != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +0000508 //
509 // use UEFI Shell 2.0 method.
510 //
jcarsey366f81a2011-06-27 21:04:22 +0000511 FileName = gEfiShellProtocol->GetFilePathFromDevicePath(*FilePath);
jcarsey94b17fa2009-05-07 18:46:18 +0000512 if (FileName == NULL) {
513 return (EFI_INVALID_PARAMETER);
514 }
515 Status = ShellOpenFileByName(FileName, FileHandle, OpenMode, Attributes);
516 FreePool(FileName);
517 return (Status);
jcarsey1e6e84c2010-01-25 20:05:08 +0000518 }
jcarseyd2b45642009-05-11 18:02:16 +0000519
520
521 //
522 // use old shell method.
523 //
jcarsey1e6e84c2010-01-25 20:05:08 +0000524 Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid,
525 FilePath,
jcarseyd2b45642009-05-11 18:02:16 +0000526 DeviceHandle);
527 if (EFI_ERROR (Status)) {
528 return Status;
529 }
530 Status = gBS->OpenProtocol(*DeviceHandle,
531 &gEfiSimpleFileSystemProtocolGuid,
jcarseyb1f95a02009-06-16 00:23:19 +0000532 (VOID**)&EfiSimpleFileSystemProtocol,
jcarseyd2b45642009-05-11 18:02:16 +0000533 gImageHandle,
534 NULL,
535 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
536 if (EFI_ERROR (Status)) {
537 return Status;
538 }
jcarseya405b862010-09-14 05:18:09 +0000539 Status = EfiSimpleFileSystemProtocol->OpenVolume(EfiSimpleFileSystemProtocol, &Handle1);
jcarseyd2b45642009-05-11 18:02:16 +0000540 if (EFI_ERROR (Status)) {
541 FileHandle = NULL;
542 return Status;
543 }
544
545 //
546 // go down directories one node at a time.
547 //
548 while (!IsDevicePathEnd (*FilePath)) {
jcarsey94b17fa2009-05-07 18:46:18 +0000549 //
jcarseyd2b45642009-05-11 18:02:16 +0000550 // For file system access each node should be a file path component
jcarsey94b17fa2009-05-07 18:46:18 +0000551 //
jcarseyd2b45642009-05-11 18:02:16 +0000552 if (DevicePathType (*FilePath) != MEDIA_DEVICE_PATH ||
553 DevicePathSubType (*FilePath) != MEDIA_FILEPATH_DP
jcarseya405b862010-09-14 05:18:09 +0000554 ) {
jcarsey94b17fa2009-05-07 18:46:18 +0000555 FileHandle = NULL;
jcarseyd2b45642009-05-11 18:02:16 +0000556 return (EFI_INVALID_PARAMETER);
jcarsey94b17fa2009-05-07 18:46:18 +0000557 }
jcarseyd2b45642009-05-11 18:02:16 +0000558 //
559 // Open this file path node
560 //
jcarseya405b862010-09-14 05:18:09 +0000561 Handle2 = Handle1;
562 Handle1 = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +0000563
564 //
ydong100b6cb332013-01-25 02:00:22 +0000565 // File Name Alignment Fix (FNAF)
566 // Handle2->Open may be incapable of handling a unaligned CHAR16 data.
567 // The structure pointed to by FilePath may be not CHAR16 aligned.
568 // This code copies the potentially unaligned PathName data from the
569 // FilePath structure to the aligned FnafPathName for use in the
570 // calls to Handl2->Open.
571 //
572
573 //
574 // Determine length of PathName, in bytes.
575 //
576 PathLen = DevicePathNodeLength (*FilePath) - SIZE_OF_FILEPATH_DEVICE_PATH;
577
578 //
579 // Allocate memory for the aligned copy of the string Extra allocation is to allow for forced alignment
580 // Copy bytes from possibly unaligned location to aligned location
581 //
582 FnafPathName = AllocateCopyPool(PathLen, (UINT8 *)((FILEPATH_DEVICE_PATH*)*FilePath)->PathName);
583 if (FnafPathName == NULL) {
584 return EFI_OUT_OF_RESOURCES;
585 }
586
587 //
jcarseyd2b45642009-05-11 18:02:16 +0000588 // Try to test opening an existing file
jcarsey94b17fa2009-05-07 18:46:18 +0000589 //
jcarseya405b862010-09-14 05:18:09 +0000590 Status = Handle2->Open (
591 Handle2,
592 &Handle1,
ydong100b6cb332013-01-25 02:00:22 +0000593 FnafPathName,
jcarseyd2b45642009-05-11 18:02:16 +0000594 OpenMode &~EFI_FILE_MODE_CREATE,
595 0
jcarseya405b862010-09-14 05:18:09 +0000596 );
jcarsey94b17fa2009-05-07 18:46:18 +0000597
jcarseyd2b45642009-05-11 18:02:16 +0000598 //
599 // see if the error was that it needs to be created
600 //
601 if ((EFI_ERROR (Status)) && (OpenMode != (OpenMode &~EFI_FILE_MODE_CREATE))) {
jcarseya405b862010-09-14 05:18:09 +0000602 Status = Handle2->Open (
603 Handle2,
604 &Handle1,
ydong100b6cb332013-01-25 02:00:22 +0000605 FnafPathName,
jcarseyd2b45642009-05-11 18:02:16 +0000606 OpenMode,
607 Attributes
jcarseya405b862010-09-14 05:18:09 +0000608 );
jcarsey94b17fa2009-05-07 18:46:18 +0000609 }
ydong100b6cb332013-01-25 02:00:22 +0000610
611 //
612 // Free the alignment buffer
613 //
614 FreePool(FnafPathName);
615
jcarseyd2b45642009-05-11 18:02:16 +0000616 //
617 // Close the last node
618 //
jcarseya405b862010-09-14 05:18:09 +0000619 Handle2->Close (Handle2);
jcarseyd2b45642009-05-11 18:02:16 +0000620
621 if (EFI_ERROR(Status)) {
622 return (Status);
623 }
624
625 //
626 // Get the next node
627 //
628 *FilePath = NextDevicePathNode (*FilePath);
jcarsey94b17fa2009-05-07 18:46:18 +0000629 }
jcarseya405b862010-09-14 05:18:09 +0000630
631 //
632 // This is a weak spot since if the undefined SHELL_FILE_HANDLE format changes this must change also!
633 //
634 *FileHandle = (VOID*)Handle1;
jcarseyd2b45642009-05-11 18:02:16 +0000635 return (EFI_SUCCESS);
jcarsey94b17fa2009-05-07 18:46:18 +0000636}
637
638/**
639 This function will open a file or directory referenced by filename.
640
jcarsey1e6e84c2010-01-25 20:05:08 +0000641 If return is EFI_SUCCESS, the Filehandle is the opened file's handle;
642 otherwise, the Filehandle is NULL. The Attributes is valid only for
jcarsey94b17fa2009-05-07 18:46:18 +0000643 EFI_FILE_MODE_CREATE.
644
jcarsey92a54472011-06-27 20:33:13 +0000645 if FileName is NULL then ASSERT()
jcarsey94b17fa2009-05-07 18:46:18 +0000646
darylm503b0934ac2011-11-12 00:35:11 +0000647 @param FileName pointer to file name
648 @param FileHandle pointer to the file handle.
649 @param OpenMode the mode to open the file with.
650 @param Attributes the file's file attributes.
jcarsey94b17fa2009-05-07 18:46:18 +0000651
darylm503b0934ac2011-11-12 00:35:11 +0000652 @retval EFI_SUCCESS The information was set.
653 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
654 @retval EFI_UNSUPPORTED Could not open the file path.
655 @retval EFI_NOT_FOUND The specified file could not be found on the
jcarsey1e6e84c2010-01-25 20:05:08 +0000656 device or the file system could not be found
jcarsey94b17fa2009-05-07 18:46:18 +0000657 on the device.
darylm503b0934ac2011-11-12 00:35:11 +0000658 @retval EFI_NO_MEDIA The device has no medium.
659 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
jcarsey94b17fa2009-05-07 18:46:18 +0000660 medium is no longer supported.
darylm503b0934ac2011-11-12 00:35:11 +0000661 @retval EFI_DEVICE_ERROR The device reported an error.
662 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
663 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
664 @retval EFI_ACCESS_DENIED The file was opened read only.
665 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
jcarsey94b17fa2009-05-07 18:46:18 +0000666 file.
darylm503b0934ac2011-11-12 00:35:11 +0000667 @retval EFI_VOLUME_FULL The volume is full.
jcarsey94b17fa2009-05-07 18:46:18 +0000668**/
669EFI_STATUS
670EFIAPI
671ShellOpenFileByName(
darylm503b0934ac2011-11-12 00:35:11 +0000672 IN CONST CHAR16 *FileName,
jcarseya405b862010-09-14 05:18:09 +0000673 OUT SHELL_FILE_HANDLE *FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000674 IN UINT64 OpenMode,
darylm503b0934ac2011-11-12 00:35:11 +0000675 IN UINT64 Attributes
jcarseya405b862010-09-14 05:18:09 +0000676 )
677{
jcarsey94b17fa2009-05-07 18:46:18 +0000678 EFI_HANDLE DeviceHandle;
679 EFI_DEVICE_PATH_PROTOCOL *FilePath;
jcarseyb1f95a02009-06-16 00:23:19 +0000680 EFI_STATUS Status;
681 EFI_FILE_INFO *FileInfo;
jaben carsey21a86a72015-01-13 22:16:41 +0000682 CHAR16 *FileNameCopy;
Yao Jiewendd17e3f2015-12-25 01:24:16 +0000683 EFI_STATUS Status2;
jcarsey94b17fa2009-05-07 18:46:18 +0000684
685 //
686 // ASSERT if FileName is NULL
687 //
688 ASSERT(FileName != NULL);
689
jcarseya405b862010-09-14 05:18:09 +0000690 if (FileName == NULL) {
691 return (EFI_INVALID_PARAMETER);
692 }
693
jcarsey366f81a2011-06-27 21:04:22 +0000694 if (gEfiShellProtocol != NULL) {
jaben carsey21a86a72015-01-13 22:16:41 +0000695 if ((OpenMode & EFI_FILE_MODE_CREATE) == EFI_FILE_MODE_CREATE) {
696
697 //
698 // Create only a directory
699 //
700 if ((Attributes & EFI_FILE_DIRECTORY) == EFI_FILE_DIRECTORY) {
701 return ShellCreateDirectory(FileName, FileHandle);
702 }
703
704 //
705 // Create the directory to create the file in
706 //
707 FileNameCopy = AllocateCopyPool (StrSize (FileName), FileName);
708 if (FileName == NULL) {
709 return (EFI_OUT_OF_RESOURCES);
710 }
711 PathCleanUpDirectories (FileNameCopy);
712 if (PathRemoveLastItem (FileNameCopy)) {
Jaben Carsey983fffd2015-07-28 20:22:26 +0000713 if (!EFI_ERROR(ShellCreateDirectory (FileNameCopy, FileHandle))) {
714 ShellCloseFile (FileHandle);
715 }
jaben carsey21a86a72015-01-13 22:16:41 +0000716 }
717 SHELL_FREE_NON_NULL (FileNameCopy);
jcarseya405b862010-09-14 05:18:09 +0000718 }
jaben carsey21a86a72015-01-13 22:16:41 +0000719
jcarsey94b17fa2009-05-07 18:46:18 +0000720 //
jaben carsey21a86a72015-01-13 22:16:41 +0000721 // Use UEFI Shell 2.0 method to create the file
jcarsey94b17fa2009-05-07 18:46:18 +0000722 //
jcarsey366f81a2011-06-27 21:04:22 +0000723 Status = gEfiShellProtocol->OpenFileByName(FileName,
jcarseyb1f95a02009-06-16 00:23:19 +0000724 FileHandle,
725 OpenMode);
Tapan Shah583448b2016-09-22 12:12:47 -0700726
Tapan Shah2cf9ecd2016-10-05 13:58:05 -0700727 if (mUnicodeCollationProtocol == NULL) {
728 Status = gBS->LocateProtocol (&gEfiUnicodeCollation2ProtocolGuid, NULL, (VOID**)&mUnicodeCollationProtocol);
729 if (EFI_ERROR (Status)) {
730 gEfiShellProtocol->CloseFile (*FileHandle);
731 return Status;
732 }
733 }
734
Tapan Shah583448b2016-09-22 12:12:47 -0700735 if ((mUnicodeCollationProtocol->StriColl (mUnicodeCollationProtocol, (CHAR16*)FileName, L"NUL") != 0) &&
736 (mUnicodeCollationProtocol->StriColl (mUnicodeCollationProtocol, (CHAR16*)FileName, L"NULL") != 0) &&
737 !EFI_ERROR(Status) && ((OpenMode & EFI_FILE_MODE_CREATE) != 0)){
jcarsey2247dde2009-11-09 18:08:58 +0000738 FileInfo = FileFunctionMap.GetFileInfo(*FileHandle);
jcarseyb1f95a02009-06-16 00:23:19 +0000739 ASSERT(FileInfo != NULL);
740 FileInfo->Attribute = Attributes;
Yao Jiewendd17e3f2015-12-25 01:24:16 +0000741 Status2 = FileFunctionMap.SetFileInfo(*FileHandle, FileInfo);
jcarsey2247dde2009-11-09 18:08:58 +0000742 FreePool(FileInfo);
Yao Jiewendd17e3f2015-12-25 01:24:16 +0000743 if (EFI_ERROR (Status2)) {
744 gEfiShellProtocol->CloseFile(*FileHandle);
745 }
746 Status = Status2;
jcarseyb1f95a02009-06-16 00:23:19 +0000747 }
748 return (Status);
jcarsey1e6e84c2010-01-25 20:05:08 +0000749 }
jcarsey94b17fa2009-05-07 18:46:18 +0000750 //
751 // Using EFI Shell version
752 // this means convert name to path and call that function
753 // since this will use EFI method again that will open it.
754 //
755 ASSERT(mEfiShellEnvironment2 != NULL);
jcarseyb82bfcc2009-06-29 16:28:23 +0000756 FilePath = mEfiShellEnvironment2->NameToPath ((CHAR16*)FileName);
xdu290bfa222010-07-19 05:21:27 +0000757 if (FilePath != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +0000758 return (ShellOpenFileByDevicePath(&FilePath,
759 &DeviceHandle,
760 FileHandle,
761 OpenMode,
jcarseya405b862010-09-14 05:18:09 +0000762 Attributes));
jcarsey94b17fa2009-05-07 18:46:18 +0000763 }
764 return (EFI_DEVICE_ERROR);
765}
766/**
767 This function create a directory
768
jcarsey1e6e84c2010-01-25 20:05:08 +0000769 If return is EFI_SUCCESS, the Filehandle is the opened directory's handle;
770 otherwise, the Filehandle is NULL. If the directory already existed, this
jcarsey94b17fa2009-05-07 18:46:18 +0000771 function opens the existing directory.
772
darylm503b0934ac2011-11-12 00:35:11 +0000773 @param DirectoryName pointer to directory name
774 @param FileHandle pointer to the file handle.
jcarsey94b17fa2009-05-07 18:46:18 +0000775
darylm503b0934ac2011-11-12 00:35:11 +0000776 @retval EFI_SUCCESS The information was set.
777 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
778 @retval EFI_UNSUPPORTED Could not open the file path.
779 @retval EFI_NOT_FOUND The specified file could not be found on the
jcarsey1e6e84c2010-01-25 20:05:08 +0000780 device or the file system could not be found
jcarsey94b17fa2009-05-07 18:46:18 +0000781 on the device.
darylm503b0934ac2011-11-12 00:35:11 +0000782 @retval EFI_NO_MEDIA The device has no medium.
783 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
jcarsey94b17fa2009-05-07 18:46:18 +0000784 medium is no longer supported.
darylm503b0934ac2011-11-12 00:35:11 +0000785 @retval EFI_DEVICE_ERROR The device reported an error.
786 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
787 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
788 @retval EFI_ACCESS_DENIED The file was opened read only.
789 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
jcarsey94b17fa2009-05-07 18:46:18 +0000790 file.
darylm503b0934ac2011-11-12 00:35:11 +0000791 @retval EFI_VOLUME_FULL The volume is full.
jcarsey94b17fa2009-05-07 18:46:18 +0000792 @sa ShellOpenFileByName
793**/
794EFI_STATUS
795EFIAPI
796ShellCreateDirectory(
jcarseyb82bfcc2009-06-29 16:28:23 +0000797 IN CONST CHAR16 *DirectoryName,
jcarseya405b862010-09-14 05:18:09 +0000798 OUT SHELL_FILE_HANDLE *FileHandle
799 )
800{
jcarsey366f81a2011-06-27 21:04:22 +0000801 if (gEfiShellProtocol != NULL) {
jcarsey2247dde2009-11-09 18:08:58 +0000802 //
803 // Use UEFI Shell 2.0 method
804 //
jcarsey366f81a2011-06-27 21:04:22 +0000805 return (gEfiShellProtocol->CreateFile(DirectoryName,
jcarsey2247dde2009-11-09 18:08:58 +0000806 EFI_FILE_DIRECTORY,
807 FileHandle
jcarseya405b862010-09-14 05:18:09 +0000808 ));
jcarsey2247dde2009-11-09 18:08:58 +0000809 } else {
810 return (ShellOpenFileByName(DirectoryName,
811 FileHandle,
812 EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE,
813 EFI_FILE_DIRECTORY
jcarseya405b862010-09-14 05:18:09 +0000814 ));
jcarsey2247dde2009-11-09 18:08:58 +0000815 }
jcarsey94b17fa2009-05-07 18:46:18 +0000816}
817
818/**
819 This function reads information from an opened file.
820
jcarsey1e6e84c2010-01-25 20:05:08 +0000821 If FileHandle is not a directory, the function reads the requested number of
822 bytes from the file at the file's current position and returns them in Buffer.
jcarsey94b17fa2009-05-07 18:46:18 +0000823 If the read goes beyond the end of the file, the read length is truncated to the
jcarsey1e6e84c2010-01-25 20:05:08 +0000824 end of the file. The file's current position is increased by the number of bytes
825 returned. If FileHandle is a directory, the function reads the directory entry
826 at the file's current position and returns the entry in Buffer. If the Buffer
827 is not large enough to hold the current directory entry, then
828 EFI_BUFFER_TOO_SMALL is returned and the current file position is not updated.
829 BufferSize is set to be the size of the buffer needed to read the entry. On
830 success, the current position is updated to the next directory entry. If there
831 are no more directory entries, the read returns a zero-length buffer.
jcarsey94b17fa2009-05-07 18:46:18 +0000832 EFI_FILE_INFO is the structure returned as the directory entry.
833
834 @param FileHandle the opened file handle
jcarsey1e6e84c2010-01-25 20:05:08 +0000835 @param BufferSize on input the size of buffer in bytes. on return
jcarsey94b17fa2009-05-07 18:46:18 +0000836 the number of bytes written.
837 @param Buffer the buffer to put read data into.
838
darylm503b0934ac2011-11-12 00:35:11 +0000839 @retval EFI_SUCCESS Data was read.
840 @retval EFI_NO_MEDIA The device has no media.
841 @retval EFI_DEVICE_ERROR The device reported an error.
842 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
843 @retval EFI_BUFFER_TO_SMALL Buffer is too small. ReadSize contains required
jcarsey94b17fa2009-05-07 18:46:18 +0000844 size.
845
846**/
847EFI_STATUS
848EFIAPI
849ShellReadFile(
jcarseya405b862010-09-14 05:18:09 +0000850 IN SHELL_FILE_HANDLE FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000851 IN OUT UINTN *BufferSize,
852 OUT VOID *Buffer
jcarseya405b862010-09-14 05:18:09 +0000853 )
854{
jcarseyd2b45642009-05-11 18:02:16 +0000855 return (FileFunctionMap.ReadFile(FileHandle, BufferSize, Buffer));
jcarsey94b17fa2009-05-07 18:46:18 +0000856}
857
858
859/**
860 Write data to a file.
861
jcarsey1e6e84c2010-01-25 20:05:08 +0000862 This function writes the specified number of bytes to the file at the current
863 file position. The current file position is advanced the actual number of bytes
864 written, which is returned in BufferSize. Partial writes only occur when there
865 has been a data error during the write attempt (such as "volume space full").
866 The file is automatically grown to hold the data if required. Direct writes to
jcarsey94b17fa2009-05-07 18:46:18 +0000867 opened directories are not supported.
868
869 @param FileHandle The opened file for writing
870 @param BufferSize on input the number of bytes in Buffer. On output
871 the number of bytes written.
872 @param Buffer the buffer containing data to write is stored.
873
darylm503b0934ac2011-11-12 00:35:11 +0000874 @retval EFI_SUCCESS Data was written.
875 @retval EFI_UNSUPPORTED Writes to an open directory are not supported.
876 @retval EFI_NO_MEDIA The device has no media.
877 @retval EFI_DEVICE_ERROR The device reported an error.
878 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
879 @retval EFI_WRITE_PROTECTED The device is write-protected.
880 @retval EFI_ACCESS_DENIED The file was open for read only.
881 @retval EFI_VOLUME_FULL The volume is full.
jcarsey94b17fa2009-05-07 18:46:18 +0000882**/
883EFI_STATUS
884EFIAPI
885ShellWriteFile(
jcarsey252d9452011-03-25 20:49:53 +0000886 IN SHELL_FILE_HANDLE FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000887 IN OUT UINTN *BufferSize,
888 IN VOID *Buffer
jcarseya405b862010-09-14 05:18:09 +0000889 )
890{
jcarseyd2b45642009-05-11 18:02:16 +0000891 return (FileFunctionMap.WriteFile(FileHandle, BufferSize, Buffer));
jcarsey94b17fa2009-05-07 18:46:18 +0000892}
893
jcarsey1e6e84c2010-01-25 20:05:08 +0000894/**
jcarsey94b17fa2009-05-07 18:46:18 +0000895 Close an open file handle.
896
jcarsey1e6e84c2010-01-25 20:05:08 +0000897 This function closes a specified file handle. All "dirty" cached file data is
898 flushed to the device, and the file is closed. In all cases the handle is
jcarsey94b17fa2009-05-07 18:46:18 +0000899 closed.
900
901@param FileHandle the file handle to close.
902
903@retval EFI_SUCCESS the file handle was closed sucessfully.
904**/
905EFI_STATUS
906EFIAPI
907ShellCloseFile (
jcarseya405b862010-09-14 05:18:09 +0000908 IN SHELL_FILE_HANDLE *FileHandle
909 )
910{
jcarseyd2b45642009-05-11 18:02:16 +0000911 return (FileFunctionMap.CloseFile(*FileHandle));
jcarsey94b17fa2009-05-07 18:46:18 +0000912}
913
914/**
915 Delete a file and close the handle
916
917 This function closes and deletes a file. In all cases the file handle is closed.
jcarsey1e6e84c2010-01-25 20:05:08 +0000918 If the file cannot be deleted, the warning code EFI_WARN_DELETE_FAILURE is
jcarsey94b17fa2009-05-07 18:46:18 +0000919 returned, but the handle is still closed.
920
921 @param FileHandle the file handle to delete
922
923 @retval EFI_SUCCESS the file was closed sucessfully
jcarsey1e6e84c2010-01-25 20:05:08 +0000924 @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not
jcarsey94b17fa2009-05-07 18:46:18 +0000925 deleted
darylm503b0934ac2011-11-12 00:35:11 +0000926 @retval INVALID_PARAMETER One of the parameters has an invalid value.
jcarsey94b17fa2009-05-07 18:46:18 +0000927**/
928EFI_STATUS
929EFIAPI
930ShellDeleteFile (
darylm503b0934ac2011-11-12 00:35:11 +0000931 IN SHELL_FILE_HANDLE *FileHandle
jcarseya405b862010-09-14 05:18:09 +0000932 )
933{
jcarseyd2b45642009-05-11 18:02:16 +0000934 return (FileFunctionMap.DeleteFile(*FileHandle));
jcarsey94b17fa2009-05-07 18:46:18 +0000935}
936
937/**
938 Set the current position in a file.
939
jcarsey1e6e84c2010-01-25 20:05:08 +0000940 This function sets the current file position for the handle to the position
jcarsey94b17fa2009-05-07 18:46:18 +0000941 supplied. With the exception of seeking to position 0xFFFFFFFFFFFFFFFF, only
jcarsey1e6e84c2010-01-25 20:05:08 +0000942 absolute positioning is supported, and seeking past the end of the file is
943 allowed (a subsequent write would grow the file). Seeking to position
jcarsey94b17fa2009-05-07 18:46:18 +0000944 0xFFFFFFFFFFFFFFFF causes the current position to be set to the end of the file.
jcarsey1e6e84c2010-01-25 20:05:08 +0000945 If FileHandle is a directory, the only position that may be set is zero. This
jcarsey94b17fa2009-05-07 18:46:18 +0000946 has the effect of starting the read process of the directory entries over.
947
948 @param FileHandle The file handle on which the position is being set
949 @param Position Byte position from begining of file
950
951 @retval EFI_SUCCESS Operation completed sucessfully.
jcarsey1e6e84c2010-01-25 20:05:08 +0000952 @retval EFI_UNSUPPORTED the seek request for non-zero is not valid on
jcarsey94b17fa2009-05-07 18:46:18 +0000953 directories.
954 @retval INVALID_PARAMETER One of the parameters has an invalid value.
955**/
956EFI_STATUS
957EFIAPI
958ShellSetFilePosition (
darylm503b0934ac2011-11-12 00:35:11 +0000959 IN SHELL_FILE_HANDLE FileHandle,
960 IN UINT64 Position
jcarseya405b862010-09-14 05:18:09 +0000961 )
962{
jcarseyd2b45642009-05-11 18:02:16 +0000963 return (FileFunctionMap.SetFilePosition(FileHandle, Position));
jcarsey94b17fa2009-05-07 18:46:18 +0000964}
965
jcarsey1e6e84c2010-01-25 20:05:08 +0000966/**
jcarsey94b17fa2009-05-07 18:46:18 +0000967 Gets a file's current position
968
jcarsey1e6e84c2010-01-25 20:05:08 +0000969 This function retrieves the current file position for the file handle. For
970 directories, the current file position has no meaning outside of the file
jcarsey94b17fa2009-05-07 18:46:18 +0000971 system driver and as such the operation is not supported. An error is returned
972 if FileHandle is a directory.
973
974 @param FileHandle The open file handle on which to get the position.
975 @param Position Byte position from begining of file.
976
977 @retval EFI_SUCCESS the operation completed sucessfully.
978 @retval INVALID_PARAMETER One of the parameters has an invalid value.
979 @retval EFI_UNSUPPORTED the request is not valid on directories.
980**/
981EFI_STATUS
982EFIAPI
983ShellGetFilePosition (
jcarseya405b862010-09-14 05:18:09 +0000984 IN SHELL_FILE_HANDLE FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000985 OUT UINT64 *Position
jcarseya405b862010-09-14 05:18:09 +0000986 )
987{
jcarseyd2b45642009-05-11 18:02:16 +0000988 return (FileFunctionMap.GetFilePosition(FileHandle, Position));
jcarsey94b17fa2009-05-07 18:46:18 +0000989}
990/**
991 Flushes data on a file
jcarsey1e6e84c2010-01-25 20:05:08 +0000992
jcarsey94b17fa2009-05-07 18:46:18 +0000993 This function flushes all modified data associated with a file to a device.
994
995 @param FileHandle The file handle on which to flush data
996
997 @retval EFI_SUCCESS The data was flushed.
998 @retval EFI_NO_MEDIA The device has no media.
999 @retval EFI_DEVICE_ERROR The device reported an error.
1000 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
1001 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
1002 @retval EFI_ACCESS_DENIED The file was opened for read only.
1003**/
1004EFI_STATUS
1005EFIAPI
1006ShellFlushFile (
jcarseya405b862010-09-14 05:18:09 +00001007 IN SHELL_FILE_HANDLE FileHandle
1008 )
1009{
jcarseyd2b45642009-05-11 18:02:16 +00001010 return (FileFunctionMap.FlushFile(FileHandle));
jcarsey94b17fa2009-05-07 18:46:18 +00001011}
1012
darylm503b0934ac2011-11-12 00:35:11 +00001013/** Retrieve first entry from a directory.
jcarsey94b17fa2009-05-07 18:46:18 +00001014
darylm503b0934ac2011-11-12 00:35:11 +00001015 This function takes an open directory handle and gets information from the
1016 first entry in the directory. A buffer is allocated to contain
1017 the information and a pointer to the buffer is returned in *Buffer. The
1018 caller can use ShellFindNextFile() to get subsequent directory entries.
jcarsey94b17fa2009-05-07 18:46:18 +00001019
darylm503b0934ac2011-11-12 00:35:11 +00001020 The buffer will be freed by ShellFindNextFile() when the last directory
1021 entry is read. Otherwise, the caller must free the buffer, using FreePool,
1022 when finished with it.
1023
1024 @param[in] DirHandle The file handle of the directory to search.
1025 @param[out] Buffer The pointer to the buffer for the file's information.
jcarsey94b17fa2009-05-07 18:46:18 +00001026
1027 @retval EFI_SUCCESS Found the first file.
1028 @retval EFI_NOT_FOUND Cannot find the directory.
1029 @retval EFI_NO_MEDIA The device has no media.
1030 @retval EFI_DEVICE_ERROR The device reported an error.
1031 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
1032 @return Others status of ShellGetFileInfo, ShellSetFilePosition,
1033 or ShellReadFile
1034**/
1035EFI_STATUS
1036EFIAPI
1037ShellFindFirstFile (
jcarseya405b862010-09-14 05:18:09 +00001038 IN SHELL_FILE_HANDLE DirHandle,
jcarseyd2b45642009-05-11 18:02:16 +00001039 OUT EFI_FILE_INFO **Buffer
jcarseya405b862010-09-14 05:18:09 +00001040 )
1041{
jcarsey94b17fa2009-05-07 18:46:18 +00001042 //
jcarseyd2b45642009-05-11 18:02:16 +00001043 // pass to file handle lib
jcarsey94b17fa2009-05-07 18:46:18 +00001044 //
jcarseyd2b45642009-05-11 18:02:16 +00001045 return (FileHandleFindFirstFile(DirHandle, Buffer));
jcarsey94b17fa2009-05-07 18:46:18 +00001046}
darylm503b0934ac2011-11-12 00:35:11 +00001047/** Retrieve next entries from a directory.
jcarsey94b17fa2009-05-07 18:46:18 +00001048
darylm503b0934ac2011-11-12 00:35:11 +00001049 To use this function, the caller must first call the ShellFindFirstFile()
1050 function to get the first directory entry. Subsequent directory entries are
1051 retrieved by using the ShellFindNextFile() function. This function can
1052 be called several times to get each entry from the directory. If the call of
1053 ShellFindNextFile() retrieved the last directory entry, the next call of
1054 this function will set *NoFile to TRUE and free the buffer.
jcarsey94b17fa2009-05-07 18:46:18 +00001055
darylm503b0934ac2011-11-12 00:35:11 +00001056 @param[in] DirHandle The file handle of the directory.
1057 @param[out] Buffer The pointer to buffer for file's information.
1058 @param[out] NoFile The pointer to boolean when last file is found.
jcarsey94b17fa2009-05-07 18:46:18 +00001059
1060 @retval EFI_SUCCESS Found the next file, or reached last file
1061 @retval EFI_NO_MEDIA The device has no media.
1062 @retval EFI_DEVICE_ERROR The device reported an error.
1063 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
1064**/
1065EFI_STATUS
1066EFIAPI
1067ShellFindNextFile(
jcarseya405b862010-09-14 05:18:09 +00001068 IN SHELL_FILE_HANDLE DirHandle,
jcarsey94b17fa2009-05-07 18:46:18 +00001069 OUT EFI_FILE_INFO *Buffer,
1070 OUT BOOLEAN *NoFile
jcarseya405b862010-09-14 05:18:09 +00001071 )
1072{
jcarsey94b17fa2009-05-07 18:46:18 +00001073 //
jcarseyd2b45642009-05-11 18:02:16 +00001074 // pass to file handle lib
jcarsey94b17fa2009-05-07 18:46:18 +00001075 //
jcarseyd2b45642009-05-11 18:02:16 +00001076 return (FileHandleFindNextFile(DirHandle, Buffer, NoFile));
jcarsey94b17fa2009-05-07 18:46:18 +00001077}
1078/**
1079 Retrieve the size of a file.
1080
1081 if FileHandle is NULL then ASSERT()
1082 if Size is NULL then ASSERT()
1083
jcarsey1e6e84c2010-01-25 20:05:08 +00001084 This function extracts the file size info from the FileHandle's EFI_FILE_INFO
jcarsey94b17fa2009-05-07 18:46:18 +00001085 data.
1086
1087 @param FileHandle file handle from which size is retrieved
1088 @param Size pointer to size
1089
1090 @retval EFI_SUCCESS operation was completed sucessfully
1091 @retval EFI_DEVICE_ERROR cannot access the file
1092**/
1093EFI_STATUS
1094EFIAPI
1095ShellGetFileSize (
jcarseya405b862010-09-14 05:18:09 +00001096 IN SHELL_FILE_HANDLE FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +00001097 OUT UINT64 *Size
jcarseya405b862010-09-14 05:18:09 +00001098 )
1099{
jcarseyd2b45642009-05-11 18:02:16 +00001100 return (FileFunctionMap.GetFileSize(FileHandle, Size));
jcarsey94b17fa2009-05-07 18:46:18 +00001101}
1102/**
1103 Retrieves the status of the break execution flag
1104
1105 this function is useful to check whether the application is being asked to halt by the shell.
1106
1107 @retval TRUE the execution break is enabled
1108 @retval FALSE the execution break is not enabled
1109**/
1110BOOLEAN
1111EFIAPI
1112ShellGetExecutionBreakFlag(
1113 VOID
1114 )
1115{
jcarsey1e6e84c2010-01-25 20:05:08 +00001116 //
jcarsey94b17fa2009-05-07 18:46:18 +00001117 // Check for UEFI Shell 2.0 protocols
1118 //
jcarsey366f81a2011-06-27 21:04:22 +00001119 if (gEfiShellProtocol != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00001120
1121 //
1122 // We are using UEFI Shell 2.0; see if the event has been triggered
1123 //
jcarsey366f81a2011-06-27 21:04:22 +00001124 if (gBS->CheckEvent(gEfiShellProtocol->ExecutionBreak) != EFI_SUCCESS) {
jcarsey94b17fa2009-05-07 18:46:18 +00001125 return (FALSE);
1126 }
1127 return (TRUE);
jcarsey1e6e84c2010-01-25 20:05:08 +00001128 }
jcarsey94b17fa2009-05-07 18:46:18 +00001129
1130 //
1131 // using EFI Shell; call the function to check
1132 //
jcarsey92a54472011-06-27 20:33:13 +00001133 if (mEfiShellEnvironment2 != NULL) {
1134 return (mEfiShellEnvironment2->GetExecutionBreak());
1135 }
1136
1137 return (FALSE);
jcarsey94b17fa2009-05-07 18:46:18 +00001138}
1139/**
1140 return the value of an environment variable
1141
jcarsey1e6e84c2010-01-25 20:05:08 +00001142 this function gets the value of the environment variable set by the
jcarsey94b17fa2009-05-07 18:46:18 +00001143 ShellSetEnvironmentVariable function
1144
1145 @param EnvKey The key name of the environment variable.
1146
1147 @retval NULL the named environment variable does not exist.
1148 @return != NULL pointer to the value of the environment variable
1149**/
1150CONST CHAR16*
1151EFIAPI
1152ShellGetEnvironmentVariable (
jcarsey9b3bf082009-06-23 21:15:07 +00001153 IN CONST CHAR16 *EnvKey
jcarsey94b17fa2009-05-07 18:46:18 +00001154 )
1155{
jcarsey1e6e84c2010-01-25 20:05:08 +00001156 //
jcarsey94b17fa2009-05-07 18:46:18 +00001157 // Check for UEFI Shell 2.0 protocols
1158 //
jcarsey366f81a2011-06-27 21:04:22 +00001159 if (gEfiShellProtocol != NULL) {
1160 return (gEfiShellProtocol->GetEnv(EnvKey));
jcarsey94b17fa2009-05-07 18:46:18 +00001161 }
1162
1163 //
jcarsey92a54472011-06-27 20:33:13 +00001164 // Check for EFI shell
jcarsey94b17fa2009-05-07 18:46:18 +00001165 //
jcarsey92a54472011-06-27 20:33:13 +00001166 if (mEfiShellEnvironment2 != NULL) {
1167 return (mEfiShellEnvironment2->GetEnv((CHAR16*)EnvKey));
1168 }
jcarsey94b17fa2009-05-07 18:46:18 +00001169
jcarsey92a54472011-06-27 20:33:13 +00001170 return NULL;
jcarsey94b17fa2009-05-07 18:46:18 +00001171}
1172/**
1173 set the value of an environment variable
1174
1175This function changes the current value of the specified environment variable. If the
1176environment variable exists and the Value is an empty string, then the environment
1177variable is deleted. If the environment variable exists and the Value is not an empty
1178string, then the value of the environment variable is changed. If the environment
1179variable does not exist and the Value is an empty string, there is no action. If the
1180environment variable does not exist and the Value is a non-empty string, then the
1181environment variable is created and assigned the specified value.
1182
1183 This is not supported pre-UEFI Shell 2.0.
1184
1185 @param EnvKey The key name of the environment variable.
1186 @param EnvVal The Value of the environment variable
1187 @param Volatile Indicates whether the variable is non-volatile (FALSE) or volatile (TRUE).
1188
1189 @retval EFI_SUCCESS the operation was completed sucessfully
1190 @retval EFI_UNSUPPORTED This operation is not allowed in pre UEFI 2.0 Shell environments
1191**/
1192EFI_STATUS
1193EFIAPI
1194ShellSetEnvironmentVariable (
1195 IN CONST CHAR16 *EnvKey,
1196 IN CONST CHAR16 *EnvVal,
1197 IN BOOLEAN Volatile
1198 )
1199{
jcarsey1e6e84c2010-01-25 20:05:08 +00001200 //
jcarsey94b17fa2009-05-07 18:46:18 +00001201 // Check for UEFI Shell 2.0 protocols
1202 //
jcarsey366f81a2011-06-27 21:04:22 +00001203 if (gEfiShellProtocol != NULL) {
1204 return (gEfiShellProtocol->SetEnv(EnvKey, EnvVal, Volatile));
jcarsey1e6e84c2010-01-25 20:05:08 +00001205 }
jcarsey94b17fa2009-05-07 18:46:18 +00001206
1207 //
1208 // This feature does not exist under EFI shell
1209 //
1210 return (EFI_UNSUPPORTED);
1211}
jcarseya405b862010-09-14 05:18:09 +00001212
jcarsey94b17fa2009-05-07 18:46:18 +00001213/**
jcarseya405b862010-09-14 05:18:09 +00001214 Cause the shell to parse and execute a command line.
jcarsey94b17fa2009-05-07 18:46:18 +00001215
1216 This function creates a nested instance of the shell and executes the specified
jcarseya405b862010-09-14 05:18:09 +00001217 command (CommandLine) with the specified environment (Environment). Upon return,
1218 the status code returned by the specified command is placed in StatusCode.
1219 If Environment is NULL, then the current environment is used and all changes made
1220 by the commands executed will be reflected in the current environment. If the
1221 Environment is non-NULL, then the changes made will be discarded.
1222 The CommandLine is executed from the current working directory on the current
1223 device.
jcarsey94b17fa2009-05-07 18:46:18 +00001224
Brendan Jackman3877d0f2014-01-24 22:31:07 +00001225 The EnvironmentVariables pararemeter is ignored in a pre-UEFI Shell 2.0
jcarseya405b862010-09-14 05:18:09 +00001226 environment. The values pointed to by the parameters will be unchanged by the
1227 ShellExecute() function. The Output parameter has no effect in a
1228 UEFI Shell 2.0 environment.
jcarsey94b17fa2009-05-07 18:46:18 +00001229
jcarseya405b862010-09-14 05:18:09 +00001230 @param[in] ParentHandle The parent image starting the operation.
1231 @param[in] CommandLine The pointer to a NULL terminated command line.
1232 @param[in] Output True to display debug output. False to hide it.
1233 @param[in] EnvironmentVariables Optional pointer to array of environment variables
1234 in the form "x=y". If NULL, the current set is used.
1235 @param[out] Status The status of the run command line.
jcarsey94b17fa2009-05-07 18:46:18 +00001236
jcarseya405b862010-09-14 05:18:09 +00001237 @retval EFI_SUCCESS The operation completed sucessfully. Status
1238 contains the status code returned.
1239 @retval EFI_INVALID_PARAMETER A parameter contains an invalid value.
1240 @retval EFI_OUT_OF_RESOURCES Out of resources.
1241 @retval EFI_UNSUPPORTED The operation is not allowed.
jcarsey94b17fa2009-05-07 18:46:18 +00001242**/
1243EFI_STATUS
1244EFIAPI
1245ShellExecute (
1246 IN EFI_HANDLE *ParentHandle,
1247 IN CHAR16 *CommandLine OPTIONAL,
1248 IN BOOLEAN Output OPTIONAL,
1249 IN CHAR16 **EnvironmentVariables OPTIONAL,
1250 OUT EFI_STATUS *Status OPTIONAL
1251 )
1252{
Brendan Jackman3877d0f2014-01-24 22:31:07 +00001253 EFI_STATUS CmdStatus;
jcarsey1e6e84c2010-01-25 20:05:08 +00001254 //
jcarsey94b17fa2009-05-07 18:46:18 +00001255 // Check for UEFI Shell 2.0 protocols
1256 //
jcarsey366f81a2011-06-27 21:04:22 +00001257 if (gEfiShellProtocol != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00001258 //
1259 // Call UEFI Shell 2.0 version (not using Output parameter)
1260 //
jcarsey366f81a2011-06-27 21:04:22 +00001261 return (gEfiShellProtocol->Execute(ParentHandle,
jcarsey94b17fa2009-05-07 18:46:18 +00001262 CommandLine,
1263 EnvironmentVariables,
1264 Status));
jcarsey1e6e84c2010-01-25 20:05:08 +00001265 }
jcarsey92a54472011-06-27 20:33:13 +00001266
jcarsey94b17fa2009-05-07 18:46:18 +00001267 //
jcarsey92a54472011-06-27 20:33:13 +00001268 // Check for EFI shell
jcarsey94b17fa2009-05-07 18:46:18 +00001269 //
jcarsey92a54472011-06-27 20:33:13 +00001270 if (mEfiShellEnvironment2 != NULL) {
1271 //
Brendan Jackman3877d0f2014-01-24 22:31:07 +00001272 // Call EFI Shell version.
jcarsey92a54472011-06-27 20:33:13 +00001273 // Due to oddity in the EFI shell we want to dereference the ParentHandle here
1274 //
Brendan Jackman3877d0f2014-01-24 22:31:07 +00001275 CmdStatus = (mEfiShellEnvironment2->Execute(*ParentHandle,
jcarsey92a54472011-06-27 20:33:13 +00001276 CommandLine,
1277 Output));
Brendan Jackman3877d0f2014-01-24 22:31:07 +00001278 //
1279 // No Status output parameter so just use the returned status
1280 //
1281 if (Status != NULL) {
1282 *Status = CmdStatus;
1283 }
1284 //
1285 // If there was an error, we can't tell if it was from the command or from
1286 // the Execute() function, so we'll just assume the shell ran successfully
1287 // and the error came from the command.
1288 //
1289 return EFI_SUCCESS;
jcarsey92a54472011-06-27 20:33:13 +00001290 }
1291
1292 return (EFI_UNSUPPORTED);
jcarsey94b17fa2009-05-07 18:46:18 +00001293}
Brendan Jackman3877d0f2014-01-24 22:31:07 +00001294
jcarsey94b17fa2009-05-07 18:46:18 +00001295/**
1296 Retreives the current directory path
1297
jcarsey1e6e84c2010-01-25 20:05:08 +00001298 If the DeviceName is NULL, it returns the current device's current directory
1299 name. If the DeviceName is not NULL, it returns the current directory name
jcarsey94b17fa2009-05-07 18:46:18 +00001300 on specified drive.
1301
Qiu Shuminfbd2dfa2015-10-23 02:03:20 +00001302 Note that the current directory string should exclude the tailing backslash character.
1303
jcarsey94b17fa2009-05-07 18:46:18 +00001304 @param DeviceName the name of the drive to get directory on
1305
1306 @retval NULL the directory does not exist
1307 @return != NULL the directory
1308**/
1309CONST CHAR16*
1310EFIAPI
1311ShellGetCurrentDir (
jcarseya405b862010-09-14 05:18:09 +00001312 IN CHAR16 * CONST DeviceName OPTIONAL
jcarsey94b17fa2009-05-07 18:46:18 +00001313 )
1314{
jcarsey1e6e84c2010-01-25 20:05:08 +00001315 //
jcarsey94b17fa2009-05-07 18:46:18 +00001316 // Check for UEFI Shell 2.0 protocols
1317 //
jcarsey366f81a2011-06-27 21:04:22 +00001318 if (gEfiShellProtocol != NULL) {
1319 return (gEfiShellProtocol->GetCurDir(DeviceName));
jcarsey1e6e84c2010-01-25 20:05:08 +00001320 }
jcarsey92a54472011-06-27 20:33:13 +00001321
jcarsey94b17fa2009-05-07 18:46:18 +00001322 //
jcarsey8bd282b2011-06-27 16:45:41 +00001323 // Check for EFI shell
jcarsey94b17fa2009-05-07 18:46:18 +00001324 //
jcarsey8bd282b2011-06-27 16:45:41 +00001325 if (mEfiShellEnvironment2 != NULL) {
1326 return (mEfiShellEnvironment2->CurDir(DeviceName));
1327 }
1328
1329 return (NULL);
jcarsey94b17fa2009-05-07 18:46:18 +00001330}
1331/**
1332 sets (enabled or disabled) the page break mode
1333
jcarsey1e6e84c2010-01-25 20:05:08 +00001334 when page break mode is enabled the screen will stop scrolling
jcarsey94b17fa2009-05-07 18:46:18 +00001335 and wait for operator input before scrolling a subsequent screen.
1336
1337 @param CurrentState TRUE to enable and FALSE to disable
1338**/
jcarsey1e6e84c2010-01-25 20:05:08 +00001339VOID
jcarsey94b17fa2009-05-07 18:46:18 +00001340EFIAPI
1341ShellSetPageBreakMode (
1342 IN BOOLEAN CurrentState
1343 )
1344{
1345 //
1346 // check for enabling
1347 //
1348 if (CurrentState != 0x00) {
jcarsey1e6e84c2010-01-25 20:05:08 +00001349 //
jcarsey94b17fa2009-05-07 18:46:18 +00001350 // check for UEFI Shell 2.0
1351 //
jcarsey366f81a2011-06-27 21:04:22 +00001352 if (gEfiShellProtocol != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00001353 //
1354 // Enable with UEFI 2.0 Shell
1355 //
jcarsey366f81a2011-06-27 21:04:22 +00001356 gEfiShellProtocol->EnablePageBreak();
jcarsey94b17fa2009-05-07 18:46:18 +00001357 return;
1358 } else {
jcarsey1e6e84c2010-01-25 20:05:08 +00001359 //
jcarsey92a54472011-06-27 20:33:13 +00001360 // Check for EFI shell
jcarsey94b17fa2009-05-07 18:46:18 +00001361 //
jcarsey92a54472011-06-27 20:33:13 +00001362 if (mEfiShellEnvironment2 != NULL) {
1363 //
1364 // Enable with EFI Shell
1365 //
1366 mEfiShellEnvironment2->EnablePageBreak (DEFAULT_INIT_ROW, DEFAULT_AUTO_LF);
1367 return;
1368 }
jcarsey94b17fa2009-05-07 18:46:18 +00001369 }
1370 } else {
jcarsey1e6e84c2010-01-25 20:05:08 +00001371 //
jcarsey94b17fa2009-05-07 18:46:18 +00001372 // check for UEFI Shell 2.0
1373 //
jcarsey366f81a2011-06-27 21:04:22 +00001374 if (gEfiShellProtocol != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00001375 //
1376 // Disable with UEFI 2.0 Shell
1377 //
jcarsey366f81a2011-06-27 21:04:22 +00001378 gEfiShellProtocol->DisablePageBreak();
jcarsey94b17fa2009-05-07 18:46:18 +00001379 return;
1380 } else {
jcarsey1e6e84c2010-01-25 20:05:08 +00001381 //
jcarsey92a54472011-06-27 20:33:13 +00001382 // Check for EFI shell
jcarsey94b17fa2009-05-07 18:46:18 +00001383 //
jcarsey92a54472011-06-27 20:33:13 +00001384 if (mEfiShellEnvironment2 != NULL) {
1385 //
1386 // Disable with EFI Shell
1387 //
1388 mEfiShellEnvironment2->DisablePageBreak ();
1389 return;
1390 }
jcarsey94b17fa2009-05-07 18:46:18 +00001391 }
1392 }
1393}
1394
1395///
1396/// version of EFI_SHELL_FILE_INFO struct, except has no CONST pointers.
1397/// This allows for the struct to be populated.
1398///
1399typedef struct {
jcarseyd2b45642009-05-11 18:02:16 +00001400 LIST_ENTRY Link;
jcarsey94b17fa2009-05-07 18:46:18 +00001401 EFI_STATUS Status;
1402 CHAR16 *FullName;
1403 CHAR16 *FileName;
jcarseya405b862010-09-14 05:18:09 +00001404 SHELL_FILE_HANDLE Handle;
jcarsey94b17fa2009-05-07 18:46:18 +00001405 EFI_FILE_INFO *Info;
1406} EFI_SHELL_FILE_INFO_NO_CONST;
1407
1408/**
1409 Converts a EFI shell list of structures to the coresponding UEFI Shell 2.0 type of list.
1410
1411 if OldStyleFileList is NULL then ASSERT()
1412
jcarsey1e6e84c2010-01-25 20:05:08 +00001413 this function will convert a SHELL_FILE_ARG based list into a callee allocated
jcarsey94b17fa2009-05-07 18:46:18 +00001414 EFI_SHELL_FILE_INFO based list. it is up to the caller to free the memory via
1415 the ShellCloseFileMetaArg function.
1416
ydong104ff7e372011-09-02 08:05:34 +00001417 @param[in] FileList the EFI shell list type
1418 @param[in, out] ListHead the list to add to
jcarsey94b17fa2009-05-07 18:46:18 +00001419
1420 @retval the resultant head of the double linked new format list;
1421**/
1422LIST_ENTRY*
1423EFIAPI
1424InternalShellConvertFileListType (
jcarsey9b3bf082009-06-23 21:15:07 +00001425 IN LIST_ENTRY *FileList,
1426 IN OUT LIST_ENTRY *ListHead
jcarsey125c2cf2009-11-18 21:36:50 +00001427 )
1428{
jcarsey94b17fa2009-05-07 18:46:18 +00001429 SHELL_FILE_ARG *OldInfo;
jcarsey9b3bf082009-06-23 21:15:07 +00001430 LIST_ENTRY *Link;
jcarsey94b17fa2009-05-07 18:46:18 +00001431 EFI_SHELL_FILE_INFO_NO_CONST *NewInfo;
1432
1433 //
jcarsey9b3bf082009-06-23 21:15:07 +00001434 // ASSERTs
jcarsey94b17fa2009-05-07 18:46:18 +00001435 //
jcarsey9b3bf082009-06-23 21:15:07 +00001436 ASSERT(FileList != NULL);
1437 ASSERT(ListHead != NULL);
jcarsey94b17fa2009-05-07 18:46:18 +00001438
1439 //
1440 // enumerate through each member of the old list and copy
1441 //
jcarseyd2b45642009-05-11 18:02:16 +00001442 for (Link = FileList->ForwardLink; Link != FileList; Link = Link->ForwardLink) {
jcarsey94b17fa2009-05-07 18:46:18 +00001443 OldInfo = CR (Link, SHELL_FILE_ARG, Link, SHELL_FILE_ARG_SIGNATURE);
jcarseya405b862010-09-14 05:18:09 +00001444 ASSERT(OldInfo != NULL);
1445
1446 //
1447 // Skip ones that failed to open...
1448 //
1449 if (OldInfo->Status != EFI_SUCCESS) {
1450 continue;
1451 }
jcarsey94b17fa2009-05-07 18:46:18 +00001452
1453 //
1454 // make sure the old list was valid
1455 //
jcarsey94b17fa2009-05-07 18:46:18 +00001456 ASSERT(OldInfo->Info != NULL);
1457 ASSERT(OldInfo->FullName != NULL);
1458 ASSERT(OldInfo->FileName != NULL);
1459
1460 //
1461 // allocate a new EFI_SHELL_FILE_INFO object
1462 //
1463 NewInfo = AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));
jcarseyc9d92df2010-02-03 15:37:54 +00001464 if (NewInfo == NULL) {
jcarsey7a95efd2011-10-13 16:08:18 +00001465 ShellCloseFileMetaArg((EFI_SHELL_FILE_INFO**)(&ListHead));
jcarseybeab0fc2011-10-10 17:26:25 +00001466 ListHead = NULL;
jcarseyc9d92df2010-02-03 15:37:54 +00001467 break;
1468 }
jcarsey1e6e84c2010-01-25 20:05:08 +00001469
1470 //
jcarsey94b17fa2009-05-07 18:46:18 +00001471 // copy the simple items
1472 //
1473 NewInfo->Handle = OldInfo->Handle;
1474 NewInfo->Status = OldInfo->Status;
1475
jcarseyd2b45642009-05-11 18:02:16 +00001476 // old shell checks for 0 not NULL
1477 OldInfo->Handle = 0;
1478
jcarsey94b17fa2009-05-07 18:46:18 +00001479 //
1480 // allocate new space to copy strings and structure
1481 //
Jaben Carsey98c16be2014-08-19 21:00:34 +00001482 NewInfo->FullName = AllocateCopyPool(StrSize(OldInfo->FullName), OldInfo->FullName);
1483 NewInfo->FileName = AllocateCopyPool(StrSize(OldInfo->FileName), OldInfo->FileName);
1484 NewInfo->Info = AllocateCopyPool((UINTN)OldInfo->Info->Size, OldInfo->Info);
jcarsey1e6e84c2010-01-25 20:05:08 +00001485
jcarsey94b17fa2009-05-07 18:46:18 +00001486 //
1487 // make sure all the memory allocations were sucessful
1488 //
jcarseybeab0fc2011-10-10 17:26:25 +00001489 if (NULL == NewInfo->FullName || NewInfo->FileName == NULL || NewInfo->Info == NULL) {
Jaben Carsey98c16be2014-08-19 21:00:34 +00001490 //
1491 // Free the partially allocated new node
1492 //
1493 SHELL_FREE_NON_NULL(NewInfo->FullName);
1494 SHELL_FREE_NON_NULL(NewInfo->FileName);
1495 SHELL_FREE_NON_NULL(NewInfo->Info);
1496 SHELL_FREE_NON_NULL(NewInfo);
1497
1498 //
1499 // Free the previously converted stuff
1500 //
jcarsey7a95efd2011-10-13 16:08:18 +00001501 ShellCloseFileMetaArg((EFI_SHELL_FILE_INFO**)(&ListHead));
jcarseybeab0fc2011-10-10 17:26:25 +00001502 ListHead = NULL;
1503 break;
1504 }
jcarsey94b17fa2009-05-07 18:46:18 +00001505
1506 //
jcarsey94b17fa2009-05-07 18:46:18 +00001507 // add that to the list
1508 //
jcarsey9b3bf082009-06-23 21:15:07 +00001509 InsertTailList(ListHead, &NewInfo->Link);
jcarsey94b17fa2009-05-07 18:46:18 +00001510 }
1511 return (ListHead);
1512}
1513/**
1514 Opens a group of files based on a path.
1515
jcarsey1e6e84c2010-01-25 20:05:08 +00001516 This function uses the Arg to open all the matching files. Each matched
Brendan Jackman70879312014-01-24 22:29:53 +00001517 file has a SHELL_FILE_INFO structure to record the file information. These
1518 structures are placed on the list ListHead. Users can get the SHELL_FILE_INFO
jcarsey94b17fa2009-05-07 18:46:18 +00001519 structures from ListHead to access each file. This function supports wildcards
jcarsey1e6e84c2010-01-25 20:05:08 +00001520 and will process '?' and '*' as such. the list must be freed with a call to
jcarsey94b17fa2009-05-07 18:46:18 +00001521 ShellCloseFileMetaArg().
1522
jcarsey1e6e84c2010-01-25 20:05:08 +00001523 If you are NOT appending to an existing list *ListHead must be NULL. If
jcarsey5f7431d2009-07-10 18:06:01 +00001524 *ListHead is NULL then it must be callee freed.
jcarsey94b17fa2009-05-07 18:46:18 +00001525
1526 @param Arg pointer to path string
1527 @param OpenMode mode to open files with
1528 @param ListHead head of linked list of results
1529
jcarsey1e6e84c2010-01-25 20:05:08 +00001530 @retval EFI_SUCCESS the operation was sucessful and the list head
jcarsey94b17fa2009-05-07 18:46:18 +00001531 contains the list of opened files
jcarsey94b17fa2009-05-07 18:46:18 +00001532 @return != EFI_SUCCESS the operation failed
1533
1534 @sa InternalShellConvertFileListType
1535**/
1536EFI_STATUS
1537EFIAPI
1538ShellOpenFileMetaArg (
1539 IN CHAR16 *Arg,
1540 IN UINT64 OpenMode,
1541 IN OUT EFI_SHELL_FILE_INFO **ListHead
1542 )
1543{
1544 EFI_STATUS Status;
jcarsey9b3bf082009-06-23 21:15:07 +00001545 LIST_ENTRY mOldStyleFileList;
Qiu Shumin0960ba12014-09-17 07:58:31 +00001546 CHAR16 *CleanFilePathStr;
jcarsey1e6e84c2010-01-25 20:05:08 +00001547
jcarsey94b17fa2009-05-07 18:46:18 +00001548 //
1549 // ASSERT that Arg and ListHead are not NULL
1550 //
1551 ASSERT(Arg != NULL);
1552 ASSERT(ListHead != NULL);
1553
Qiu Shumin715096c2014-09-19 01:32:05 +00001554 CleanFilePathStr = NULL;
1555
Qiu Shumin0960ba12014-09-17 07:58:31 +00001556 Status = InternalShellStripQuotes (Arg, &CleanFilePathStr);
1557 if (EFI_ERROR (Status)) {
1558 return Status;
1559 }
1560
jcarsey1e6e84c2010-01-25 20:05:08 +00001561 //
jcarsey94b17fa2009-05-07 18:46:18 +00001562 // Check for UEFI Shell 2.0 protocols
1563 //
jcarsey366f81a2011-06-27 21:04:22 +00001564 if (gEfiShellProtocol != NULL) {
jcarsey5f7431d2009-07-10 18:06:01 +00001565 if (*ListHead == NULL) {
1566 *ListHead = (EFI_SHELL_FILE_INFO*)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));
1567 if (*ListHead == NULL) {
Qiu Shumin0960ba12014-09-17 07:58:31 +00001568 FreePool(CleanFilePathStr);
jcarsey5f7431d2009-07-10 18:06:01 +00001569 return (EFI_OUT_OF_RESOURCES);
1570 }
1571 InitializeListHead(&((*ListHead)->Link));
jcarsey1e6e84c2010-01-25 20:05:08 +00001572 }
Qiu Shumin0960ba12014-09-17 07:58:31 +00001573 Status = gEfiShellProtocol->OpenFileList(CleanFilePathStr,
jcarsey1e6e84c2010-01-25 20:05:08 +00001574 OpenMode,
jcarsey2247dde2009-11-09 18:08:58 +00001575 ListHead);
1576 if (EFI_ERROR(Status)) {
jcarsey366f81a2011-06-27 21:04:22 +00001577 gEfiShellProtocol->RemoveDupInFileList(ListHead);
jcarsey2247dde2009-11-09 18:08:58 +00001578 } else {
jcarsey366f81a2011-06-27 21:04:22 +00001579 Status = gEfiShellProtocol->RemoveDupInFileList(ListHead);
jcarsey2247dde2009-11-09 18:08:58 +00001580 }
jcarseya405b862010-09-14 05:18:09 +00001581 if (*ListHead != NULL && IsListEmpty(&(*ListHead)->Link)) {
1582 FreePool(*ListHead);
Qiu Shumin0960ba12014-09-17 07:58:31 +00001583 FreePool(CleanFilePathStr);
jcarseya405b862010-09-14 05:18:09 +00001584 *ListHead = NULL;
1585 return (EFI_NOT_FOUND);
1586 }
Qiu Shumin0960ba12014-09-17 07:58:31 +00001587 FreePool(CleanFilePathStr);
jcarsey2247dde2009-11-09 18:08:58 +00001588 return (Status);
jcarsey1e6e84c2010-01-25 20:05:08 +00001589 }
jcarsey94b17fa2009-05-07 18:46:18 +00001590
1591 //
jcarsey92a54472011-06-27 20:33:13 +00001592 // Check for EFI shell
jcarsey94b17fa2009-05-07 18:46:18 +00001593 //
jcarsey92a54472011-06-27 20:33:13 +00001594 if (mEfiShellEnvironment2 != NULL) {
1595 //
1596 // make sure the list head is initialized
1597 //
1598 InitializeListHead(&mOldStyleFileList);
jcarsey94b17fa2009-05-07 18:46:18 +00001599
jcarsey92a54472011-06-27 20:33:13 +00001600 //
1601 // Get the EFI Shell list of files
1602 //
Qiu Shumin0960ba12014-09-17 07:58:31 +00001603 Status = mEfiShellEnvironment2->FileMetaArg(CleanFilePathStr, &mOldStyleFileList);
jcarsey92a54472011-06-27 20:33:13 +00001604 if (EFI_ERROR(Status)) {
1605 *ListHead = NULL;
Qiu Shumin0960ba12014-09-17 07:58:31 +00001606 FreePool(CleanFilePathStr);
jcarsey92a54472011-06-27 20:33:13 +00001607 return (Status);
1608 }
jcarsey94b17fa2009-05-07 18:46:18 +00001609
jcarsey92a54472011-06-27 20:33:13 +00001610 if (*ListHead == NULL) {
1611 *ListHead = (EFI_SHELL_FILE_INFO *)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));
1612 if (*ListHead == NULL) {
Qiu Shumin0960ba12014-09-17 07:58:31 +00001613 FreePool(CleanFilePathStr);
jcarsey92a54472011-06-27 20:33:13 +00001614 return (EFI_OUT_OF_RESOURCES);
1615 }
1616 InitializeListHead(&((*ListHead)->Link));
1617 }
1618
1619 //
1620 // Convert that to equivalent of UEFI Shell 2.0 structure
1621 //
1622 InternalShellConvertFileListType(&mOldStyleFileList, &(*ListHead)->Link);
1623
1624 //
1625 // Free the EFI Shell version that was converted.
1626 //
1627 mEfiShellEnvironment2->FreeFileList(&mOldStyleFileList);
1628
1629 if ((*ListHead)->Link.ForwardLink == (*ListHead)->Link.BackLink && (*ListHead)->Link.BackLink == &((*ListHead)->Link)) {
1630 FreePool(*ListHead);
1631 *ListHead = NULL;
1632 Status = EFI_NOT_FOUND;
1633 }
Qiu Shumin0960ba12014-09-17 07:58:31 +00001634 FreePool(CleanFilePathStr);
jcarsey94b17fa2009-05-07 18:46:18 +00001635 return (Status);
1636 }
1637
Qiu Shumin0960ba12014-09-17 07:58:31 +00001638 FreePool(CleanFilePathStr);
jcarsey92a54472011-06-27 20:33:13 +00001639 return (EFI_UNSUPPORTED);
jcarsey94b17fa2009-05-07 18:46:18 +00001640}
1641/**
jcarseya405b862010-09-14 05:18:09 +00001642 Free the linked list returned from ShellOpenFileMetaArg.
jcarsey94b17fa2009-05-07 18:46:18 +00001643
jcarseya405b862010-09-14 05:18:09 +00001644 if ListHead is NULL then ASSERT().
jcarsey94b17fa2009-05-07 18:46:18 +00001645
jcarseya405b862010-09-14 05:18:09 +00001646 @param ListHead the pointer to free.
jcarsey94b17fa2009-05-07 18:46:18 +00001647
jcarseya405b862010-09-14 05:18:09 +00001648 @retval EFI_SUCCESS the operation was sucessful.
jcarsey94b17fa2009-05-07 18:46:18 +00001649**/
1650EFI_STATUS
1651EFIAPI
1652ShellCloseFileMetaArg (
1653 IN OUT EFI_SHELL_FILE_INFO **ListHead
1654 )
1655{
1656 LIST_ENTRY *Node;
1657
1658 //
1659 // ASSERT that ListHead is not NULL
1660 //
1661 ASSERT(ListHead != NULL);
1662
jcarsey1e6e84c2010-01-25 20:05:08 +00001663 //
jcarsey94b17fa2009-05-07 18:46:18 +00001664 // Check for UEFI Shell 2.0 protocols
1665 //
jcarsey366f81a2011-06-27 21:04:22 +00001666 if (gEfiShellProtocol != NULL) {
1667 return (gEfiShellProtocol->FreeFileList(ListHead));
jcarsey92a54472011-06-27 20:33:13 +00001668 } else if (mEfiShellEnvironment2 != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00001669 //
jcarsey1e6e84c2010-01-25 20:05:08 +00001670 // Since this is EFI Shell version we need to free our internally made copy
jcarsey94b17fa2009-05-07 18:46:18 +00001671 // of the list
1672 //
jcarsey1e6e84c2010-01-25 20:05:08 +00001673 for ( Node = GetFirstNode(&(*ListHead)->Link)
jcarseya405b862010-09-14 05:18:09 +00001674 ; *ListHead != NULL && !IsListEmpty(&(*ListHead)->Link)
jcarsey9b3bf082009-06-23 21:15:07 +00001675 ; Node = GetFirstNode(&(*ListHead)->Link)) {
jcarsey94b17fa2009-05-07 18:46:18 +00001676 RemoveEntryList(Node);
jcarseya405b862010-09-14 05:18:09 +00001677 ((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 +00001678 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->FullName);
1679 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->FileName);
1680 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->Info);
1681 FreePool((EFI_SHELL_FILE_INFO_NO_CONST*)Node);
1682 }
Jaben Carsey75a5e2e2014-01-10 16:42:45 +00001683 SHELL_FREE_NON_NULL(*ListHead);
jcarsey94b17fa2009-05-07 18:46:18 +00001684 return EFI_SUCCESS;
1685 }
jcarsey92a54472011-06-27 20:33:13 +00001686
1687 return (EFI_UNSUPPORTED);
jcarsey94b17fa2009-05-07 18:46:18 +00001688}
1689
jcarsey125c2cf2009-11-18 21:36:50 +00001690/**
1691 Find a file by searching the CWD and then the path.
1692
jcarseyb3011f42010-01-11 21:49:04 +00001693 If FileName is NULL then ASSERT.
jcarsey125c2cf2009-11-18 21:36:50 +00001694
jcarseyb3011f42010-01-11 21:49:04 +00001695 If the return value is not NULL then the memory must be caller freed.
jcarsey125c2cf2009-11-18 21:36:50 +00001696
1697 @param FileName Filename string.
1698
1699 @retval NULL the file was not found
1700 @return !NULL the full path to the file.
1701**/
1702CHAR16 *
1703EFIAPI
1704ShellFindFilePath (
1705 IN CONST CHAR16 *FileName
1706 )
1707{
1708 CONST CHAR16 *Path;
jcarseya405b862010-09-14 05:18:09 +00001709 SHELL_FILE_HANDLE Handle;
jcarsey125c2cf2009-11-18 21:36:50 +00001710 EFI_STATUS Status;
1711 CHAR16 *RetVal;
1712 CHAR16 *TestPath;
1713 CONST CHAR16 *Walker;
jcarsey36a9d672009-11-20 21:13:41 +00001714 UINTN Size;
jcarsey1cd45e72010-01-29 15:07:44 +00001715 CHAR16 *TempChar;
jcarsey125c2cf2009-11-18 21:36:50 +00001716
1717 RetVal = NULL;
1718
jcarseya405b862010-09-14 05:18:09 +00001719 //
1720 // First make sure its not an absolute path.
1721 //
1722 Status = ShellOpenFileByName(FileName, &Handle, EFI_FILE_MODE_READ, 0);
1723 if (!EFI_ERROR(Status)){
1724 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {
1725 ASSERT(RetVal == NULL);
1726 RetVal = StrnCatGrow(&RetVal, NULL, FileName, 0);
1727 ShellCloseFile(&Handle);
1728 return (RetVal);
1729 } else {
1730 ShellCloseFile(&Handle);
1731 }
1732 }
1733
jcarsey125c2cf2009-11-18 21:36:50 +00001734 Path = ShellGetEnvironmentVariable(L"cwd");
1735 if (Path != NULL) {
Qiu Shuminfbd2dfa2015-10-23 02:03:20 +00001736 Size = StrSize(Path) + sizeof(CHAR16);
jcarsey36a9d672009-11-20 21:13:41 +00001737 Size += StrSize(FileName);
1738 TestPath = AllocateZeroPool(Size);
jcarseyc9d92df2010-02-03 15:37:54 +00001739 if (TestPath == NULL) {
1740 return (NULL);
1741 }
Qiu Shumine75390f2015-06-30 03:18:31 +00001742 StrCpyS(TestPath, Size/sizeof(CHAR16), Path);
Qiu Shuminfbd2dfa2015-10-23 02:03:20 +00001743 StrCatS(TestPath, Size/sizeof(CHAR16), L"\\");
Qiu Shumine75390f2015-06-30 03:18:31 +00001744 StrCatS(TestPath, Size/sizeof(CHAR16), FileName);
jcarsey125c2cf2009-11-18 21:36:50 +00001745 Status = ShellOpenFileByName(TestPath, &Handle, EFI_FILE_MODE_READ, 0);
1746 if (!EFI_ERROR(Status)){
jcarseya405b862010-09-14 05:18:09 +00001747 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {
1748 ASSERT(RetVal == NULL);
1749 RetVal = StrnCatGrow(&RetVal, NULL, TestPath, 0);
1750 ShellCloseFile(&Handle);
1751 FreePool(TestPath);
1752 return (RetVal);
1753 } else {
1754 ShellCloseFile(&Handle);
1755 }
jcarsey125c2cf2009-11-18 21:36:50 +00001756 }
1757 FreePool(TestPath);
1758 }
1759 Path = ShellGetEnvironmentVariable(L"path");
1760 if (Path != NULL) {
jcarseya405b862010-09-14 05:18:09 +00001761 Size = StrSize(Path)+sizeof(CHAR16);
jcarsey36a9d672009-11-20 21:13:41 +00001762 Size += StrSize(FileName);
1763 TestPath = AllocateZeroPool(Size);
jcarsey3e082d52010-10-04 16:44:57 +00001764 if (TestPath == NULL) {
1765 return (NULL);
1766 }
jcarsey1e6e84c2010-01-25 20:05:08 +00001767 Walker = (CHAR16*)Path;
jcarsey125c2cf2009-11-18 21:36:50 +00001768 do {
1769 CopyMem(TestPath, Walker, StrSize(Walker));
jcarsey3e082d52010-10-04 16:44:57 +00001770 if (TestPath != NULL) {
1771 TempChar = StrStr(TestPath, L";");
1772 if (TempChar != NULL) {
1773 *TempChar = CHAR_NULL;
1774 }
1775 if (TestPath[StrLen(TestPath)-1] != L'\\') {
Qiu Shumine75390f2015-06-30 03:18:31 +00001776 StrCatS(TestPath, Size/sizeof(CHAR16), L"\\");
jcarsey3e082d52010-10-04 16:44:57 +00001777 }
jcarsey89e85372011-04-13 23:37:50 +00001778 if (FileName[0] == L'\\') {
1779 FileName++;
1780 }
Qiu Shumine75390f2015-06-30 03:18:31 +00001781 StrCatS(TestPath, Size/sizeof(CHAR16), FileName);
jcarsey3e082d52010-10-04 16:44:57 +00001782 if (StrStr(Walker, L";") != NULL) {
1783 Walker = StrStr(Walker, L";") + 1;
jcarseya405b862010-09-14 05:18:09 +00001784 } else {
jcarsey3e082d52010-10-04 16:44:57 +00001785 Walker = NULL;
1786 }
1787 Status = ShellOpenFileByName(TestPath, &Handle, EFI_FILE_MODE_READ, 0);
1788 if (!EFI_ERROR(Status)){
1789 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {
1790 ASSERT(RetVal == NULL);
1791 RetVal = StrnCatGrow(&RetVal, NULL, TestPath, 0);
1792 ShellCloseFile(&Handle);
1793 break;
1794 } else {
1795 ShellCloseFile(&Handle);
1796 }
jcarseya405b862010-09-14 05:18:09 +00001797 }
jcarsey125c2cf2009-11-18 21:36:50 +00001798 }
1799 } while (Walker != NULL && Walker[0] != CHAR_NULL);
1800 FreePool(TestPath);
1801 }
1802 return (RetVal);
1803}
1804
jcarseyb3011f42010-01-11 21:49:04 +00001805/**
jcarsey1e6e84c2010-01-25 20:05:08 +00001806 Find a file by searching the CWD and then the path with a variable set of file
1807 extensions. If the file is not found it will append each extension in the list
jcarseyb3011f42010-01-11 21:49:04 +00001808 in the order provided and return the first one that is successful.
1809
1810 If FileName is NULL, then ASSERT.
1811 If FileExtension is NULL, then behavior is identical to ShellFindFilePath.
1812
1813 If the return value is not NULL then the memory must be caller freed.
1814
1815 @param[in] FileName Filename string.
1816 @param[in] FileExtension Semi-colon delimeted list of possible extensions.
1817
1818 @retval NULL The file was not found.
1819 @retval !NULL The path to the file.
1820**/
1821CHAR16 *
1822EFIAPI
1823ShellFindFilePathEx (
1824 IN CONST CHAR16 *FileName,
1825 IN CONST CHAR16 *FileExtension
1826 )
1827{
1828 CHAR16 *TestPath;
1829 CHAR16 *RetVal;
1830 CONST CHAR16 *ExtensionWalker;
jcarsey9e926b62010-01-14 20:26:39 +00001831 UINTN Size;
jcarsey1cd45e72010-01-29 15:07:44 +00001832 CHAR16 *TempChar;
jcarseyc9d92df2010-02-03 15:37:54 +00001833 CHAR16 *TempChar2;
jcarsey1cd45e72010-01-29 15:07:44 +00001834
jcarseyb3011f42010-01-11 21:49:04 +00001835 ASSERT(FileName != NULL);
1836 if (FileExtension == NULL) {
1837 return (ShellFindFilePath(FileName));
1838 }
1839 RetVal = ShellFindFilePath(FileName);
1840 if (RetVal != NULL) {
1841 return (RetVal);
1842 }
jcarsey9e926b62010-01-14 20:26:39 +00001843 Size = StrSize(FileName);
1844 Size += StrSize(FileExtension);
1845 TestPath = AllocateZeroPool(Size);
jcarseyc9d92df2010-02-03 15:37:54 +00001846 if (TestPath == NULL) {
1847 return (NULL);
1848 }
jcarseya405b862010-09-14 05:18:09 +00001849 for (ExtensionWalker = FileExtension, TempChar2 = (CHAR16*)FileExtension; TempChar2 != NULL ; ExtensionWalker = TempChar2 + 1){
Qiu Shumine75390f2015-06-30 03:18:31 +00001850 StrCpyS(TestPath, Size/sizeof(CHAR16), FileName);
jcarseya405b862010-09-14 05:18:09 +00001851 if (ExtensionWalker != NULL) {
Qiu Shumine75390f2015-06-30 03:18:31 +00001852 StrCatS(TestPath, Size/sizeof(CHAR16), ExtensionWalker);
jcarseya405b862010-09-14 05:18:09 +00001853 }
jcarsey1cd45e72010-01-29 15:07:44 +00001854 TempChar = StrStr(TestPath, L";");
1855 if (TempChar != NULL) {
1856 *TempChar = CHAR_NULL;
jcarseyb3011f42010-01-11 21:49:04 +00001857 }
1858 RetVal = ShellFindFilePath(TestPath);
1859 if (RetVal != NULL) {
1860 break;
1861 }
jcarseya405b862010-09-14 05:18:09 +00001862 ASSERT(ExtensionWalker != NULL);
jcarseyc9d92df2010-02-03 15:37:54 +00001863 TempChar2 = StrStr(ExtensionWalker, L";");
jcarseyb3011f42010-01-11 21:49:04 +00001864 }
1865 FreePool(TestPath);
1866 return (RetVal);
1867}
1868
jcarsey94b17fa2009-05-07 18:46:18 +00001869typedef struct {
jcarsey9b3bf082009-06-23 21:15:07 +00001870 LIST_ENTRY Link;
jcarsey94b17fa2009-05-07 18:46:18 +00001871 CHAR16 *Name;
jcarseya405b862010-09-14 05:18:09 +00001872 SHELL_PARAM_TYPE Type;
jcarsey94b17fa2009-05-07 18:46:18 +00001873 CHAR16 *Value;
1874 UINTN OriginalPosition;
1875} SHELL_PARAM_PACKAGE;
1876
1877/**
jcarsey1e6e84c2010-01-25 20:05:08 +00001878 Checks the list of valid arguments and returns TRUE if the item was found. If the
jcarsey94b17fa2009-05-07 18:46:18 +00001879 return value is TRUE then the type parameter is set also.
jcarsey1e6e84c2010-01-25 20:05:08 +00001880
jcarsey94b17fa2009-05-07 18:46:18 +00001881 if CheckList is NULL then ASSERT();
1882 if Name is NULL then ASSERT();
1883 if Type is NULL then ASSERT();
1884
jcarsey94b17fa2009-05-07 18:46:18 +00001885 @param Name pointer to Name of parameter found
1886 @param CheckList List to check against
jcarseya405b862010-09-14 05:18:09 +00001887 @param Type pointer to type of parameter if it was found
jcarsey94b17fa2009-05-07 18:46:18 +00001888
1889 @retval TRUE the Parameter was found. Type is valid.
1890 @retval FALSE the Parameter was not found. Type is not valid.
1891**/
1892BOOLEAN
1893EFIAPI
jcarseyd2b45642009-05-11 18:02:16 +00001894InternalIsOnCheckList (
jcarsey94b17fa2009-05-07 18:46:18 +00001895 IN CONST CHAR16 *Name,
1896 IN CONST SHELL_PARAM_ITEM *CheckList,
jcarsey252d9452011-03-25 20:49:53 +00001897 OUT SHELL_PARAM_TYPE *Type
jcarseya405b862010-09-14 05:18:09 +00001898 )
1899{
jcarsey94b17fa2009-05-07 18:46:18 +00001900 SHELL_PARAM_ITEM *TempListItem;
jcarsey252d9452011-03-25 20:49:53 +00001901 CHAR16 *TempString;
jcarsey94b17fa2009-05-07 18:46:18 +00001902
1903 //
1904 // ASSERT that all 3 pointer parameters aren't NULL
1905 //
1906 ASSERT(CheckList != NULL);
1907 ASSERT(Type != NULL);
1908 ASSERT(Name != NULL);
1909
1910 //
jcarseyd2b45642009-05-11 18:02:16 +00001911 // question mark and page break mode are always supported
1912 //
1913 if ((StrCmp(Name, L"-?") == 0) ||
1914 (StrCmp(Name, L"-b") == 0)
jcarseya405b862010-09-14 05:18:09 +00001915 ) {
jcarsey252d9452011-03-25 20:49:53 +00001916 *Type = TypeFlag;
jcarseyd2b45642009-05-11 18:02:16 +00001917 return (TRUE);
1918 }
1919
1920 //
jcarsey94b17fa2009-05-07 18:46:18 +00001921 // Enumerate through the list
1922 //
1923 for (TempListItem = (SHELL_PARAM_ITEM*)CheckList ; TempListItem->Name != NULL ; TempListItem++) {
1924 //
jcarsey9eb53ac2009-07-08 17:26:58 +00001925 // If the Type is TypeStart only check the first characters of the passed in param
1926 // If it matches set the type and return TRUE
jcarsey94b17fa2009-05-07 18:46:18 +00001927 //
darylm503b0934ac2011-11-12 00:35:11 +00001928 if (TempListItem->Type == TypeStart) {
jcarsey252d9452011-03-25 20:49:53 +00001929 if (StrnCmp(Name, TempListItem->Name, StrLen(TempListItem->Name)) == 0) {
1930 *Type = TempListItem->Type;
1931 return (TRUE);
1932 }
1933 TempString = NULL;
1934 TempString = StrnCatGrow(&TempString, NULL, Name, StrLen(TempListItem->Name));
1935 if (TempString != NULL) {
1936 if (StringNoCaseCompare(&TempString, &TempListItem->Name) == 0) {
1937 *Type = TempListItem->Type;
1938 FreePool(TempString);
1939 return (TRUE);
1940 }
1941 FreePool(TempString);
1942 }
1943 } else if (StringNoCaseCompare(&Name, &TempListItem->Name) == 0) {
jcarsey94b17fa2009-05-07 18:46:18 +00001944 *Type = TempListItem->Type;
1945 return (TRUE);
1946 }
1947 }
jcarsey2247dde2009-11-09 18:08:58 +00001948
jcarsey94b17fa2009-05-07 18:46:18 +00001949 return (FALSE);
1950}
1951/**
jcarseyd2b45642009-05-11 18:02:16 +00001952 Checks the string for indicators of "flag" status. this is a leading '/', '-', or '+'
jcarsey94b17fa2009-05-07 18:46:18 +00001953
jcarseya405b862010-09-14 05:18:09 +00001954 @param[in] Name pointer to Name of parameter found
1955 @param[in] AlwaysAllowNumbers TRUE to allow numbers, FALSE to not.
jcarsey658bf432014-11-04 22:33:16 +00001956 @param[in] TimeNumbers TRUE to allow numbers with ":", FALSE otherwise.
jcarsey94b17fa2009-05-07 18:46:18 +00001957
1958 @retval TRUE the Parameter is a flag.
jcarseya405b862010-09-14 05:18:09 +00001959 @retval FALSE the Parameter not a flag.
jcarsey94b17fa2009-05-07 18:46:18 +00001960**/
1961BOOLEAN
1962EFIAPI
jcarseyd2b45642009-05-11 18:02:16 +00001963InternalIsFlag (
jcarsey2247dde2009-11-09 18:08:58 +00001964 IN CONST CHAR16 *Name,
jcarsey658bf432014-11-04 22:33:16 +00001965 IN CONST BOOLEAN AlwaysAllowNumbers,
1966 IN CONST BOOLEAN TimeNumbers
jcarsey94b17fa2009-05-07 18:46:18 +00001967 )
1968{
1969 //
1970 // ASSERT that Name isn't NULL
1971 //
1972 ASSERT(Name != NULL);
1973
1974 //
jcarsey2247dde2009-11-09 18:08:58 +00001975 // If we accept numbers then dont return TRUE. (they will be values)
1976 //
jcarsey658bf432014-11-04 22:33:16 +00001977 if (((Name[0] == L'-' || Name[0] == L'+') && InternalShellIsHexOrDecimalNumber(Name+1, FALSE, FALSE, TimeNumbers)) && AlwaysAllowNumbers) {
jcarsey2247dde2009-11-09 18:08:58 +00001978 return (FALSE);
1979 }
1980
1981 //
jcarseya405b862010-09-14 05:18:09 +00001982 // If the Name has a /, +, or - as the first character return TRUE
jcarsey94b17fa2009-05-07 18:46:18 +00001983 //
jcarsey1e6e84c2010-01-25 20:05:08 +00001984 if ((Name[0] == L'/') ||
jcarseyd2b45642009-05-11 18:02:16 +00001985 (Name[0] == L'-') ||
1986 (Name[0] == L'+')
jcarseya405b862010-09-14 05:18:09 +00001987 ) {
jcarsey94b17fa2009-05-07 18:46:18 +00001988 return (TRUE);
1989 }
1990 return (FALSE);
1991}
1992
1993/**
jcarsey1e6e84c2010-01-25 20:05:08 +00001994 Checks the command line arguments passed against the list of valid ones.
jcarsey94b17fa2009-05-07 18:46:18 +00001995
1996 If no initialization is required, then return RETURN_SUCCESS.
jcarsey1e6e84c2010-01-25 20:05:08 +00001997
jcarseya405b862010-09-14 05:18:09 +00001998 @param[in] CheckList pointer to list of parameters to check
1999 @param[out] CheckPackage pointer to pointer to list checked values
2000 @param[out] ProblemParam optional pointer to pointer to unicode string for
jcarseyd2b45642009-05-11 18:02:16 +00002001 the paramater that caused failure. If used then the
2002 caller is responsible for freeing the memory.
jcarseya405b862010-09-14 05:18:09 +00002003 @param[in] AutoPageBreak will automatically set PageBreakEnabled for "b" parameter
2004 @param[in] Argv pointer to array of parameters
2005 @param[in] Argc Count of parameters in Argv
2006 @param[in] AlwaysAllowNumbers TRUE to allow numbers always, FALSE otherwise.
jcarsey94b17fa2009-05-07 18:46:18 +00002007
2008 @retval EFI_SUCCESS The operation completed sucessfully.
2009 @retval EFI_OUT_OF_RESOURCES A memory allocation failed
2010 @retval EFI_INVALID_PARAMETER A parameter was invalid
jcarsey1e6e84c2010-01-25 20:05:08 +00002011 @retval EFI_VOLUME_CORRUPTED the command line was corrupt. an argument was
2012 duplicated. the duplicated command line argument
jcarsey94b17fa2009-05-07 18:46:18 +00002013 was returned in ProblemParam if provided.
jcarsey1e6e84c2010-01-25 20:05:08 +00002014 @retval EFI_NOT_FOUND a argument required a value that was missing.
jcarsey94b17fa2009-05-07 18:46:18 +00002015 the invalid command line argument was returned in
2016 ProblemParam if provided.
2017**/
2018EFI_STATUS
2019EFIAPI
2020InternalCommandLineParse (
2021 IN CONST SHELL_PARAM_ITEM *CheckList,
2022 OUT LIST_ENTRY **CheckPackage,
2023 OUT CHAR16 **ProblemParam OPTIONAL,
2024 IN BOOLEAN AutoPageBreak,
2025 IN CONST CHAR16 **Argv,
jcarsey2247dde2009-11-09 18:08:58 +00002026 IN UINTN Argc,
2027 IN BOOLEAN AlwaysAllowNumbers
jcarseya405b862010-09-14 05:18:09 +00002028 )
2029{
jcarsey94b17fa2009-05-07 18:46:18 +00002030 UINTN LoopCounter;
jcarsey252d9452011-03-25 20:49:53 +00002031 SHELL_PARAM_TYPE CurrentItemType;
jcarsey94b17fa2009-05-07 18:46:18 +00002032 SHELL_PARAM_PACKAGE *CurrentItemPackage;
jcarsey125c2cf2009-11-18 21:36:50 +00002033 UINTN GetItemValue;
2034 UINTN ValueSize;
jcarseya405b862010-09-14 05:18:09 +00002035 UINTN Count;
jcarsey252d9452011-03-25 20:49:53 +00002036 CONST CHAR16 *TempPointer;
Jaben Carsey98c16be2014-08-19 21:00:34 +00002037 UINTN CurrentValueSize;
Ruiyu Ni2efafab2016-07-13 17:39:37 +08002038 CHAR16 *NewValue;
jcarsey94b17fa2009-05-07 18:46:18 +00002039
2040 CurrentItemPackage = NULL;
jcarsey125c2cf2009-11-18 21:36:50 +00002041 GetItemValue = 0;
2042 ValueSize = 0;
jcarseya405b862010-09-14 05:18:09 +00002043 Count = 0;
jcarsey94b17fa2009-05-07 18:46:18 +00002044
2045 //
2046 // If there is only 1 item we dont need to do anything
2047 //
jcarseya405b862010-09-14 05:18:09 +00002048 if (Argc < 1) {
jcarsey94b17fa2009-05-07 18:46:18 +00002049 *CheckPackage = NULL;
2050 return (EFI_SUCCESS);
2051 }
2052
2053 //
jcarsey2247dde2009-11-09 18:08:58 +00002054 // ASSERTs
2055 //
2056 ASSERT(CheckList != NULL);
2057 ASSERT(Argv != NULL);
2058
2059 //
jcarsey94b17fa2009-05-07 18:46:18 +00002060 // initialize the linked list
2061 //
2062 *CheckPackage = (LIST_ENTRY*)AllocateZeroPool(sizeof(LIST_ENTRY));
sfu502a758c2011-10-08 02:55:30 +00002063 if (*CheckPackage == NULL) {
jcarseybeab0fc2011-10-10 17:26:25 +00002064 return (EFI_OUT_OF_RESOURCES);
sfu502a758c2011-10-08 02:55:30 +00002065 }
jcarseybeab0fc2011-10-10 17:26:25 +00002066
jcarsey94b17fa2009-05-07 18:46:18 +00002067 InitializeListHead(*CheckPackage);
2068
2069 //
2070 // loop through each of the arguments
2071 //
2072 for (LoopCounter = 0 ; LoopCounter < Argc ; ++LoopCounter) {
2073 if (Argv[LoopCounter] == NULL) {
2074 //
2075 // do nothing for NULL argv
2076 //
jcarseya405b862010-09-14 05:18:09 +00002077 } else if (InternalIsOnCheckList(Argv[LoopCounter], CheckList, &CurrentItemType)) {
jcarsey94b17fa2009-05-07 18:46:18 +00002078 //
jcarsey2247dde2009-11-09 18:08:58 +00002079 // We might have leftover if last parameter didnt have optional value
2080 //
jcarsey125c2cf2009-11-18 21:36:50 +00002081 if (GetItemValue != 0) {
2082 GetItemValue = 0;
jcarsey2247dde2009-11-09 18:08:58 +00002083 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
2084 }
2085 //
jcarsey94b17fa2009-05-07 18:46:18 +00002086 // this is a flag
2087 //
jcarsey252d9452011-03-25 20:49:53 +00002088 CurrentItemPackage = AllocateZeroPool(sizeof(SHELL_PARAM_PACKAGE));
jcarseybeab0fc2011-10-10 17:26:25 +00002089 if (CurrentItemPackage == NULL) {
2090 ShellCommandLineFreeVarList(*CheckPackage);
2091 *CheckPackage = NULL;
2092 return (EFI_OUT_OF_RESOURCES);
2093 }
Jaben Carsey98c16be2014-08-19 21:00:34 +00002094 CurrentItemPackage->Name = AllocateCopyPool(StrSize(Argv[LoopCounter]), Argv[LoopCounter]);
jcarseybeab0fc2011-10-10 17:26:25 +00002095 if (CurrentItemPackage->Name == NULL) {
2096 ShellCommandLineFreeVarList(*CheckPackage);
2097 *CheckPackage = NULL;
2098 return (EFI_OUT_OF_RESOURCES);
2099 }
jcarsey94b17fa2009-05-07 18:46:18 +00002100 CurrentItemPackage->Type = CurrentItemType;
2101 CurrentItemPackage->OriginalPosition = (UINTN)(-1);
jcarseyb1f95a02009-06-16 00:23:19 +00002102 CurrentItemPackage->Value = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +00002103
2104 //
2105 // Does this flag require a value
2106 //
jcarsey125c2cf2009-11-18 21:36:50 +00002107 switch (CurrentItemPackage->Type) {
jcarsey94b17fa2009-05-07 18:46:18 +00002108 //
jcarsey125c2cf2009-11-18 21:36:50 +00002109 // possibly trigger the next loop(s) to populate the value of this item
jcarsey1e6e84c2010-01-25 20:05:08 +00002110 //
jcarsey125c2cf2009-11-18 21:36:50 +00002111 case TypeValue:
jcarsey658bf432014-11-04 22:33:16 +00002112 case TypeTimeValue:
jcarsey1e6e84c2010-01-25 20:05:08 +00002113 GetItemValue = 1;
jcarsey125c2cf2009-11-18 21:36:50 +00002114 ValueSize = 0;
2115 break;
2116 case TypeDoubleValue:
2117 GetItemValue = 2;
2118 ValueSize = 0;
2119 break;
2120 case TypeMaxValue:
2121 GetItemValue = (UINTN)(-1);
2122 ValueSize = 0;
2123 break;
2124 default:
2125 //
2126 // this item has no value expected; we are done
2127 //
2128 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
2129 ASSERT(GetItemValue == 0);
2130 break;
jcarsey94b17fa2009-05-07 18:46:18 +00002131 }
Qiu Shuminaf7a3a52015-03-13 02:04:17 +00002132 } else if (GetItemValue != 0 && CurrentItemPackage != NULL && !InternalIsFlag(Argv[LoopCounter], AlwaysAllowNumbers, (BOOLEAN)(CurrentItemPackage->Type == TypeTimeValue))) {
jcarseyb1f95a02009-06-16 00:23:19 +00002133 //
jcarsey125c2cf2009-11-18 21:36:50 +00002134 // get the item VALUE for a previous flag
jcarseyb1f95a02009-06-16 00:23:19 +00002135 //
Qiu Shumin484dd082015-04-01 00:49:05 +00002136 CurrentValueSize = ValueSize + StrSize(Argv[LoopCounter]) + sizeof(CHAR16);
Ruiyu Ni2efafab2016-07-13 17:39:37 +08002137 NewValue = ReallocatePool(ValueSize, CurrentValueSize, CurrentItemPackage->Value);
2138 if (NewValue == NULL) {
2139 SHELL_FREE_NON_NULL (CurrentItemPackage->Value);
2140 SHELL_FREE_NON_NULL (CurrentItemPackage);
2141 ShellCommandLineFreeVarList (*CheckPackage);
2142 *CheckPackage = NULL;
2143 return EFI_OUT_OF_RESOURCES;
2144 }
2145 CurrentItemPackage->Value = NewValue;
Qiu Shumin484dd082015-04-01 00:49:05 +00002146 if (ValueSize == 0) {
Qiu Shumine75390f2015-06-30 03:18:31 +00002147 StrCpyS( CurrentItemPackage->Value,
2148 CurrentValueSize/sizeof(CHAR16),
2149 Argv[LoopCounter]
2150 );
jcarsey125c2cf2009-11-18 21:36:50 +00002151 } else {
Qiu Shumine75390f2015-06-30 03:18:31 +00002152 StrCatS( CurrentItemPackage->Value,
2153 CurrentValueSize/sizeof(CHAR16),
2154 L" "
2155 );
2156 StrCatS( CurrentItemPackage->Value,
2157 CurrentValueSize/sizeof(CHAR16),
2158 Argv[LoopCounter]
2159 );
jcarsey125c2cf2009-11-18 21:36:50 +00002160 }
Qiu Shumin484dd082015-04-01 00:49:05 +00002161 ValueSize += StrSize(Argv[LoopCounter]) + sizeof(CHAR16);
2162
jcarsey125c2cf2009-11-18 21:36:50 +00002163 GetItemValue--;
2164 if (GetItemValue == 0) {
2165 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
2166 }
jcarsey658bf432014-11-04 22:33:16 +00002167 } else if (!InternalIsFlag(Argv[LoopCounter], AlwaysAllowNumbers, FALSE)){
jcarseyb1f95a02009-06-16 00:23:19 +00002168 //
2169 // add this one as a non-flag
2170 //
jcarsey252d9452011-03-25 20:49:53 +00002171
2172 TempPointer = Argv[LoopCounter];
darylm503b0934ac2011-11-12 00:35:11 +00002173 if ((*TempPointer == L'^' && *(TempPointer+1) == L'-')
jcarsey252d9452011-03-25 20:49:53 +00002174 || (*TempPointer == L'^' && *(TempPointer+1) == L'/')
2175 || (*TempPointer == L'^' && *(TempPointer+1) == L'+')
2176 ){
2177 TempPointer++;
2178 }
2179 CurrentItemPackage = AllocateZeroPool(sizeof(SHELL_PARAM_PACKAGE));
jcarseybeab0fc2011-10-10 17:26:25 +00002180 if (CurrentItemPackage == NULL) {
2181 ShellCommandLineFreeVarList(*CheckPackage);
2182 *CheckPackage = NULL;
2183 return (EFI_OUT_OF_RESOURCES);
2184 }
jcarseyb1f95a02009-06-16 00:23:19 +00002185 CurrentItemPackage->Name = NULL;
2186 CurrentItemPackage->Type = TypePosition;
Jaben Carsey98c16be2014-08-19 21:00:34 +00002187 CurrentItemPackage->Value = AllocateCopyPool(StrSize(TempPointer), TempPointer);
jcarseybeab0fc2011-10-10 17:26:25 +00002188 if (CurrentItemPackage->Value == NULL) {
2189 ShellCommandLineFreeVarList(*CheckPackage);
2190 *CheckPackage = NULL;
2191 return (EFI_OUT_OF_RESOURCES);
2192 }
jcarseya405b862010-09-14 05:18:09 +00002193 CurrentItemPackage->OriginalPosition = Count++;
jcarsey9b3bf082009-06-23 21:15:07 +00002194 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
jcarsey252d9452011-03-25 20:49:53 +00002195 } else {
jcarsey94b17fa2009-05-07 18:46:18 +00002196 //
2197 // this was a non-recognised flag... error!
2198 //
jcarsey252d9452011-03-25 20:49:53 +00002199 if (ProblemParam != NULL) {
Jaben Carsey98c16be2014-08-19 21:00:34 +00002200 *ProblemParam = AllocateCopyPool(StrSize(Argv[LoopCounter]), Argv[LoopCounter]);
jcarsey252d9452011-03-25 20:49:53 +00002201 }
jcarsey94b17fa2009-05-07 18:46:18 +00002202 ShellCommandLineFreeVarList(*CheckPackage);
2203 *CheckPackage = NULL;
2204 return (EFI_VOLUME_CORRUPTED);
jcarsey94b17fa2009-05-07 18:46:18 +00002205 }
2206 }
jcarsey125c2cf2009-11-18 21:36:50 +00002207 if (GetItemValue != 0) {
2208 GetItemValue = 0;
2209 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
2210 }
jcarsey94b17fa2009-05-07 18:46:18 +00002211 //
2212 // support for AutoPageBreak
2213 //
2214 if (AutoPageBreak && ShellCommandLineGetFlag(*CheckPackage, L"-b")) {
2215 ShellSetPageBreakMode(TRUE);
2216 }
2217 return (EFI_SUCCESS);
2218}
2219
2220/**
jcarsey1e6e84c2010-01-25 20:05:08 +00002221 Checks the command line arguments passed against the list of valid ones.
jcarsey94b17fa2009-05-07 18:46:18 +00002222 Optionally removes NULL values first.
jcarsey1e6e84c2010-01-25 20:05:08 +00002223
jcarsey94b17fa2009-05-07 18:46:18 +00002224 If no initialization is required, then return RETURN_SUCCESS.
jcarsey1e6e84c2010-01-25 20:05:08 +00002225
jcarseya405b862010-09-14 05:18:09 +00002226 @param[in] CheckList The pointer to list of parameters to check.
2227 @param[out] CheckPackage The package of checked values.
2228 @param[out] ProblemParam Optional pointer to pointer to unicode string for
jcarsey94b17fa2009-05-07 18:46:18 +00002229 the paramater that caused failure.
jcarseya405b862010-09-14 05:18:09 +00002230 @param[in] AutoPageBreak Will automatically set PageBreakEnabled.
2231 @param[in] AlwaysAllowNumbers Will never fail for number based flags.
jcarsey94b17fa2009-05-07 18:46:18 +00002232
2233 @retval EFI_SUCCESS The operation completed sucessfully.
jcarseya405b862010-09-14 05:18:09 +00002234 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
2235 @retval EFI_INVALID_PARAMETER A parameter was invalid.
2236 @retval EFI_VOLUME_CORRUPTED The command line was corrupt.
2237 @retval EFI_DEVICE_ERROR The commands contained 2 opposing arguments. One
jcarsey1e6e84c2010-01-25 20:05:08 +00002238 of the command line arguments was returned in
jcarsey94b17fa2009-05-07 18:46:18 +00002239 ProblemParam if provided.
jcarseya405b862010-09-14 05:18:09 +00002240 @retval EFI_NOT_FOUND A argument required a value that was missing.
2241 The invalid command line argument was returned in
jcarsey94b17fa2009-05-07 18:46:18 +00002242 ProblemParam if provided.
2243**/
2244EFI_STATUS
2245EFIAPI
jcarsey2247dde2009-11-09 18:08:58 +00002246ShellCommandLineParseEx (
jcarsey94b17fa2009-05-07 18:46:18 +00002247 IN CONST SHELL_PARAM_ITEM *CheckList,
2248 OUT LIST_ENTRY **CheckPackage,
2249 OUT CHAR16 **ProblemParam OPTIONAL,
jcarsey2247dde2009-11-09 18:08:58 +00002250 IN BOOLEAN AutoPageBreak,
2251 IN BOOLEAN AlwaysAllowNumbers
jcarseya405b862010-09-14 05:18:09 +00002252 )
2253{
jcarsey1e6e84c2010-01-25 20:05:08 +00002254 //
jcarsey94b17fa2009-05-07 18:46:18 +00002255 // ASSERT that CheckList and CheckPackage aren't NULL
2256 //
2257 ASSERT(CheckList != NULL);
2258 ASSERT(CheckPackage != NULL);
2259
jcarsey1e6e84c2010-01-25 20:05:08 +00002260 //
jcarsey94b17fa2009-05-07 18:46:18 +00002261 // Check for UEFI Shell 2.0 protocols
2262 //
jcarsey366f81a2011-06-27 21:04:22 +00002263 if (gEfiShellParametersProtocol != NULL) {
jcarsey1e6e84c2010-01-25 20:05:08 +00002264 return (InternalCommandLineParse(CheckList,
2265 CheckPackage,
2266 ProblemParam,
2267 AutoPageBreak,
jcarsey366f81a2011-06-27 21:04:22 +00002268 (CONST CHAR16**) gEfiShellParametersProtocol->Argv,
2269 gEfiShellParametersProtocol->Argc,
jcarsey2247dde2009-11-09 18:08:58 +00002270 AlwaysAllowNumbers));
jcarsey94b17fa2009-05-07 18:46:18 +00002271 }
2272
jcarsey1e6e84c2010-01-25 20:05:08 +00002273 //
jcarsey94b17fa2009-05-07 18:46:18 +00002274 // ASSERT That EFI Shell is not required
2275 //
2276 ASSERT (mEfiShellInterface != NULL);
jcarsey1e6e84c2010-01-25 20:05:08 +00002277 return (InternalCommandLineParse(CheckList,
2278 CheckPackage,
2279 ProblemParam,
2280 AutoPageBreak,
jljusten08d7f8e2009-06-15 18:42:13 +00002281 (CONST CHAR16**) mEfiShellInterface->Argv,
jcarsey2247dde2009-11-09 18:08:58 +00002282 mEfiShellInterface->Argc,
2283 AlwaysAllowNumbers));
jcarsey94b17fa2009-05-07 18:46:18 +00002284}
2285
2286/**
2287 Frees shell variable list that was returned from ShellCommandLineParse.
2288
2289 This function will free all the memory that was used for the CheckPackage
2290 list of postprocessed shell arguments.
2291
2292 this function has no return value.
2293
2294 if CheckPackage is NULL, then return
2295
2296 @param CheckPackage the list to de-allocate
2297 **/
2298VOID
2299EFIAPI
2300ShellCommandLineFreeVarList (
2301 IN LIST_ENTRY *CheckPackage
jcarseya405b862010-09-14 05:18:09 +00002302 )
2303{
jcarsey94b17fa2009-05-07 18:46:18 +00002304 LIST_ENTRY *Node;
2305
2306 //
2307 // check for CheckPackage == NULL
2308 //
2309 if (CheckPackage == NULL) {
2310 return;
2311 }
2312
2313 //
2314 // for each node in the list
2315 //
jcarsey9eb53ac2009-07-08 17:26:58 +00002316 for ( Node = GetFirstNode(CheckPackage)
jcarseya405b862010-09-14 05:18:09 +00002317 ; !IsListEmpty(CheckPackage)
jcarsey9eb53ac2009-07-08 17:26:58 +00002318 ; Node = GetFirstNode(CheckPackage)
jcarseya405b862010-09-14 05:18:09 +00002319 ){
jcarsey94b17fa2009-05-07 18:46:18 +00002320 //
2321 // Remove it from the list
2322 //
2323 RemoveEntryList(Node);
2324
2325 //
2326 // if it has a name free the name
2327 //
2328 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {
2329 FreePool(((SHELL_PARAM_PACKAGE*)Node)->Name);
2330 }
2331
2332 //
2333 // if it has a value free the value
2334 //
2335 if (((SHELL_PARAM_PACKAGE*)Node)->Value != NULL) {
2336 FreePool(((SHELL_PARAM_PACKAGE*)Node)->Value);
2337 }
jcarsey1e6e84c2010-01-25 20:05:08 +00002338
jcarsey94b17fa2009-05-07 18:46:18 +00002339 //
2340 // free the node structure
2341 //
2342 FreePool((SHELL_PARAM_PACKAGE*)Node);
2343 }
2344 //
2345 // free the list head node
2346 //
2347 FreePool(CheckPackage);
2348}
2349/**
2350 Checks for presence of a flag parameter
2351
2352 flag arguments are in the form of "-<Key>" or "/<Key>", but do not have a value following the key
2353
2354 if CheckPackage is NULL then return FALSE.
2355 if KeyString is NULL then ASSERT()
jcarsey1e6e84c2010-01-25 20:05:08 +00002356
jcarsey94b17fa2009-05-07 18:46:18 +00002357 @param CheckPackage The package of parsed command line arguments
2358 @param KeyString the Key of the command line argument to check for
2359
2360 @retval TRUE the flag is on the command line
2361 @retval FALSE the flag is not on the command line
2362 **/
2363BOOLEAN
2364EFIAPI
2365ShellCommandLineGetFlag (
jcarseya405b862010-09-14 05:18:09 +00002366 IN CONST LIST_ENTRY * CONST CheckPackage,
2367 IN CONST CHAR16 * CONST KeyString
2368 )
2369{
jcarsey94b17fa2009-05-07 18:46:18 +00002370 LIST_ENTRY *Node;
jcarsey252d9452011-03-25 20:49:53 +00002371 CHAR16 *TempString;
jcarsey94b17fa2009-05-07 18:46:18 +00002372
2373 //
ydong100c1950b2011-10-13 02:37:35 +00002374 // return FALSE for no package or KeyString is NULL
jcarsey94b17fa2009-05-07 18:46:18 +00002375 //
ydong100c1950b2011-10-13 02:37:35 +00002376 if (CheckPackage == NULL || KeyString == NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00002377 return (FALSE);
2378 }
2379
2380 //
2381 // enumerate through the list of parametrs
2382 //
jcarsey1e6e84c2010-01-25 20:05:08 +00002383 for ( Node = GetFirstNode(CheckPackage)
2384 ; !IsNull (CheckPackage, Node)
2385 ; Node = GetNextNode(CheckPackage, Node)
jcarsey252d9452011-03-25 20:49:53 +00002386 ){
jcarsey94b17fa2009-05-07 18:46:18 +00002387 //
2388 // If the Name matches, return TRUE (and there may be NULL name)
2389 //
2390 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {
jcarsey9eb53ac2009-07-08 17:26:58 +00002391 //
2392 // If Type is TypeStart then only compare the begining of the strings
2393 //
jcarsey252d9452011-03-25 20:49:53 +00002394 if (((SHELL_PARAM_PACKAGE*)Node)->Type == TypeStart) {
2395 if (StrnCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name, StrLen(KeyString)) == 0) {
2396 return (TRUE);
2397 }
2398 TempString = NULL;
2399 TempString = StrnCatGrow(&TempString, NULL, KeyString, StrLen(((SHELL_PARAM_PACKAGE*)Node)->Name));
2400 if (TempString != NULL) {
2401 if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {
2402 FreePool(TempString);
2403 return (TRUE);
2404 }
2405 FreePool(TempString);
2406 }
2407 } else if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {
jcarsey94b17fa2009-05-07 18:46:18 +00002408 return (TRUE);
2409 }
2410 }
2411 }
2412 return (FALSE);
2413}
2414/**
jcarseya405b862010-09-14 05:18:09 +00002415 Returns value from command line argument.
jcarsey94b17fa2009-05-07 18:46:18 +00002416
jcarseya405b862010-09-14 05:18:09 +00002417 Value parameters are in the form of "-<Key> value" or "/<Key> value".
jcarsey1e6e84c2010-01-25 20:05:08 +00002418
jcarseya405b862010-09-14 05:18:09 +00002419 If CheckPackage is NULL, then return NULL.
jcarsey94b17fa2009-05-07 18:46:18 +00002420
jcarseya405b862010-09-14 05:18:09 +00002421 @param[in] CheckPackage The package of parsed command line arguments.
2422 @param[in] KeyString The Key of the command line argument to check for.
jcarsey94b17fa2009-05-07 18:46:18 +00002423
jcarseya405b862010-09-14 05:18:09 +00002424 @retval NULL The flag is not on the command line.
2425 @retval !=NULL The pointer to unicode string of the value.
2426**/
jcarsey94b17fa2009-05-07 18:46:18 +00002427CONST CHAR16*
2428EFIAPI
2429ShellCommandLineGetValue (
2430 IN CONST LIST_ENTRY *CheckPackage,
2431 IN CHAR16 *KeyString
jcarseya405b862010-09-14 05:18:09 +00002432 )
2433{
jcarsey94b17fa2009-05-07 18:46:18 +00002434 LIST_ENTRY *Node;
jcarsey252d9452011-03-25 20:49:53 +00002435 CHAR16 *TempString;
jcarsey94b17fa2009-05-07 18:46:18 +00002436
2437 //
ydong100c1950b2011-10-13 02:37:35 +00002438 // return NULL for no package or KeyString is NULL
jcarsey94b17fa2009-05-07 18:46:18 +00002439 //
ydong100c1950b2011-10-13 02:37:35 +00002440 if (CheckPackage == NULL || KeyString == NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00002441 return (NULL);
2442 }
2443
2444 //
2445 // enumerate through the list of parametrs
2446 //
jcarsey1e6e84c2010-01-25 20:05:08 +00002447 for ( Node = GetFirstNode(CheckPackage)
2448 ; !IsNull (CheckPackage, Node)
2449 ; Node = GetNextNode(CheckPackage, Node)
jcarsey252d9452011-03-25 20:49:53 +00002450 ){
jcarsey94b17fa2009-05-07 18:46:18 +00002451 //
jcarsey252d9452011-03-25 20:49:53 +00002452 // If the Name matches, return TRUE (and there may be NULL name)
jcarsey94b17fa2009-05-07 18:46:18 +00002453 //
2454 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {
jcarsey9eb53ac2009-07-08 17:26:58 +00002455 //
2456 // If Type is TypeStart then only compare the begining of the strings
2457 //
jcarsey252d9452011-03-25 20:49:53 +00002458 if (((SHELL_PARAM_PACKAGE*)Node)->Type == TypeStart) {
2459 if (StrnCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name, StrLen(KeyString)) == 0) {
2460 return (((SHELL_PARAM_PACKAGE*)Node)->Name + StrLen(KeyString));
2461 }
2462 TempString = NULL;
2463 TempString = StrnCatGrow(&TempString, NULL, KeyString, StrLen(((SHELL_PARAM_PACKAGE*)Node)->Name));
2464 if (TempString != NULL) {
2465 if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {
2466 FreePool(TempString);
2467 return (((SHELL_PARAM_PACKAGE*)Node)->Name + StrLen(KeyString));
2468 }
2469 FreePool(TempString);
2470 }
2471 } else if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {
jcarsey94b17fa2009-05-07 18:46:18 +00002472 return (((SHELL_PARAM_PACKAGE*)Node)->Value);
2473 }
2474 }
2475 }
2476 return (NULL);
2477}
jcarseya405b862010-09-14 05:18:09 +00002478
jcarsey94b17fa2009-05-07 18:46:18 +00002479/**
jcarseya405b862010-09-14 05:18:09 +00002480 Returns raw value from command line argument.
jcarsey94b17fa2009-05-07 18:46:18 +00002481
jcarseya405b862010-09-14 05:18:09 +00002482 Raw value parameters are in the form of "value" in a specific position in the list.
jcarsey1e6e84c2010-01-25 20:05:08 +00002483
jcarseya405b862010-09-14 05:18:09 +00002484 If CheckPackage is NULL, then return NULL.
jcarsey94b17fa2009-05-07 18:46:18 +00002485
jcarseya405b862010-09-14 05:18:09 +00002486 @param[in] CheckPackage The package of parsed command line arguments.
2487 @param[in] Position The position of the value.
jcarsey94b17fa2009-05-07 18:46:18 +00002488
jcarseya405b862010-09-14 05:18:09 +00002489 @retval NULL The flag is not on the command line.
2490 @retval !=NULL The pointer to unicode string of the value.
jcarsey94b17fa2009-05-07 18:46:18 +00002491 **/
2492CONST CHAR16*
2493EFIAPI
2494ShellCommandLineGetRawValue (
jcarseya405b862010-09-14 05:18:09 +00002495 IN CONST LIST_ENTRY * CONST CheckPackage,
2496 IN UINTN Position
2497 )
2498{
jcarsey94b17fa2009-05-07 18:46:18 +00002499 LIST_ENTRY *Node;
2500
2501 //
2502 // check for CheckPackage == NULL
2503 //
2504 if (CheckPackage == NULL) {
2505 return (NULL);
2506 }
2507
2508 //
2509 // enumerate through the list of parametrs
2510 //
jcarsey1e6e84c2010-01-25 20:05:08 +00002511 for ( Node = GetFirstNode(CheckPackage)
2512 ; !IsNull (CheckPackage, Node)
2513 ; Node = GetNextNode(CheckPackage, Node)
jcarseya405b862010-09-14 05:18:09 +00002514 ){
jcarsey94b17fa2009-05-07 18:46:18 +00002515 //
2516 // If the position matches, return the value
2517 //
2518 if (((SHELL_PARAM_PACKAGE*)Node)->OriginalPosition == Position) {
2519 return (((SHELL_PARAM_PACKAGE*)Node)->Value);
2520 }
2521 }
2522 return (NULL);
jcarseyb1f95a02009-06-16 00:23:19 +00002523}
jcarsey2247dde2009-11-09 18:08:58 +00002524
2525/**
jcarsey1e6e84c2010-01-25 20:05:08 +00002526 returns the number of command line value parameters that were parsed.
2527
jcarsey2247dde2009-11-09 18:08:58 +00002528 this will not include flags.
2529
jcarseya405b862010-09-14 05:18:09 +00002530 @param[in] CheckPackage The package of parsed command line arguments.
2531
jcarsey2247dde2009-11-09 18:08:58 +00002532 @retval (UINTN)-1 No parsing has ocurred
2533 @return other The number of value parameters found
2534**/
2535UINTN
2536EFIAPI
2537ShellCommandLineGetCount(
jcarseya405b862010-09-14 05:18:09 +00002538 IN CONST LIST_ENTRY *CheckPackage
jcarsey125c2cf2009-11-18 21:36:50 +00002539 )
2540{
jcarseya405b862010-09-14 05:18:09 +00002541 LIST_ENTRY *Node1;
2542 UINTN Count;
2543
2544 if (CheckPackage == NULL) {
2545 return (0);
2546 }
2547 for ( Node1 = GetFirstNode(CheckPackage), Count = 0
2548 ; !IsNull (CheckPackage, Node1)
2549 ; Node1 = GetNextNode(CheckPackage, Node1)
2550 ){
2551 if (((SHELL_PARAM_PACKAGE*)Node1)->Name == NULL) {
2552 Count++;
2553 }
2554 }
2555 return (Count);
jcarsey2247dde2009-11-09 18:08:58 +00002556}
2557
jcarsey975136a2009-06-16 19:03:54 +00002558/**
Bruce Crancceb4eb2015-07-08 01:54:46 +00002559 Determines if a parameter is duplicated.
jcarsey36a9d672009-11-20 21:13:41 +00002560
jcarsey1e6e84c2010-01-25 20:05:08 +00002561 If Param is not NULL then it will point to a callee allocated string buffer
jcarsey36a9d672009-11-20 21:13:41 +00002562 with the parameter value if a duplicate is found.
2563
2564 If CheckPackage is NULL, then ASSERT.
2565
2566 @param[in] CheckPackage The package of parsed command line arguments.
2567 @param[out] Param Upon finding one, a pointer to the duplicated parameter.
2568
2569 @retval EFI_SUCCESS No parameters were duplicated.
2570 @retval EFI_DEVICE_ERROR A duplicate was found.
2571 **/
2572EFI_STATUS
2573EFIAPI
2574ShellCommandLineCheckDuplicate (
2575 IN CONST LIST_ENTRY *CheckPackage,
2576 OUT CHAR16 **Param
2577 )
2578{
2579 LIST_ENTRY *Node1;
2580 LIST_ENTRY *Node2;
jcarsey1e6e84c2010-01-25 20:05:08 +00002581
jcarsey36a9d672009-11-20 21:13:41 +00002582 ASSERT(CheckPackage != NULL);
2583
jcarsey1e6e84c2010-01-25 20:05:08 +00002584 for ( Node1 = GetFirstNode(CheckPackage)
2585 ; !IsNull (CheckPackage, Node1)
2586 ; Node1 = GetNextNode(CheckPackage, Node1)
jcarseya405b862010-09-14 05:18:09 +00002587 ){
jcarsey1e6e84c2010-01-25 20:05:08 +00002588 for ( Node2 = GetNextNode(CheckPackage, Node1)
2589 ; !IsNull (CheckPackage, Node2)
2590 ; Node2 = GetNextNode(CheckPackage, Node2)
jcarseya405b862010-09-14 05:18:09 +00002591 ){
2592 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 +00002593 if (Param != NULL) {
2594 *Param = NULL;
2595 *Param = StrnCatGrow(Param, NULL, ((SHELL_PARAM_PACKAGE*)Node1)->Name, 0);
2596 }
2597 return (EFI_DEVICE_ERROR);
2598 }
2599 }
2600 }
2601 return (EFI_SUCCESS);
2602}
2603
2604/**
jcarsey1e6e84c2010-01-25 20:05:08 +00002605 This is a find and replace function. Upon successful return the NewString is a copy of
jcarsey975136a2009-06-16 19:03:54 +00002606 SourceString with each instance of FindTarget replaced with ReplaceWith.
2607
jcarseyb3011f42010-01-11 21:49:04 +00002608 If SourceString and NewString overlap the behavior is undefined.
2609
jcarsey975136a2009-06-16 19:03:54 +00002610 If the string would grow bigger than NewSize it will halt and return error.
2611
ydong104ff7e372011-09-02 08:05:34 +00002612 @param[in] SourceString The string with source buffer.
2613 @param[in, out] NewString The string with resultant buffer.
2614 @param[in] NewSize The size in bytes of NewString.
2615 @param[in] FindTarget The string to look for.
2616 @param[in] ReplaceWith The string to replace FindTarget with.
2617 @param[in] SkipPreCarrot If TRUE will skip a FindTarget that has a '^'
2618 immediately before it.
2619 @param[in] ParameterReplacing If TRUE will add "" around items with spaces.
jcarsey975136a2009-06-16 19:03:54 +00002620
jcarsey969c7832010-01-13 16:46:33 +00002621 @retval EFI_INVALID_PARAMETER SourceString was NULL.
2622 @retval EFI_INVALID_PARAMETER NewString was NULL.
2623 @retval EFI_INVALID_PARAMETER FindTarget was NULL.
2624 @retval EFI_INVALID_PARAMETER ReplaceWith was NULL.
2625 @retval EFI_INVALID_PARAMETER FindTarget had length < 1.
2626 @retval EFI_INVALID_PARAMETER SourceString had length < 1.
jcarsey1e6e84c2010-01-25 20:05:08 +00002627 @retval EFI_BUFFER_TOO_SMALL NewSize was less than the minimum size to hold
jcarsey969c7832010-01-13 16:46:33 +00002628 the new string (truncation occurred).
jcarseya405b862010-09-14 05:18:09 +00002629 @retval EFI_SUCCESS The string was successfully copied with replacement.
jcarsey975136a2009-06-16 19:03:54 +00002630**/
jcarsey975136a2009-06-16 19:03:54 +00002631EFI_STATUS
2632EFIAPI
jcarseya405b862010-09-14 05:18:09 +00002633ShellCopySearchAndReplace(
jcarsey975136a2009-06-16 19:03:54 +00002634 IN CHAR16 CONST *SourceString,
jcarseya405b862010-09-14 05:18:09 +00002635 IN OUT CHAR16 *NewString,
jcarsey975136a2009-06-16 19:03:54 +00002636 IN UINTN NewSize,
2637 IN CONST CHAR16 *FindTarget,
jcarsey969c7832010-01-13 16:46:33 +00002638 IN CONST CHAR16 *ReplaceWith,
jcarseya405b862010-09-14 05:18:09 +00002639 IN CONST BOOLEAN SkipPreCarrot,
2640 IN CONST BOOLEAN ParameterReplacing
jcarsey1e6e84c2010-01-25 20:05:08 +00002641 )
jcarsey2247dde2009-11-09 18:08:58 +00002642{
jcarsey01582942009-07-10 19:46:17 +00002643 UINTN Size;
jcarseya405b862010-09-14 05:18:09 +00002644 CHAR16 *Replace;
2645
jcarsey975136a2009-06-16 19:03:54 +00002646 if ( (SourceString == NULL)
2647 || (NewString == NULL)
2648 || (FindTarget == NULL)
2649 || (ReplaceWith == NULL)
2650 || (StrLen(FindTarget) < 1)
2651 || (StrLen(SourceString) < 1)
jcarseya405b862010-09-14 05:18:09 +00002652 ){
jcarsey975136a2009-06-16 19:03:54 +00002653 return (EFI_INVALID_PARAMETER);
2654 }
jcarseya405b862010-09-14 05:18:09 +00002655 Replace = NULL;
2656 if (StrStr(ReplaceWith, L" ") == NULL || !ParameterReplacing) {
2657 Replace = StrnCatGrow(&Replace, NULL, ReplaceWith, 0);
2658 } else {
2659 Replace = AllocateZeroPool(StrSize(ReplaceWith) + 2*sizeof(CHAR16));
jcarseybeab0fc2011-10-10 17:26:25 +00002660 if (Replace != NULL) {
2661 UnicodeSPrint(Replace, StrSize(ReplaceWith) + 2*sizeof(CHAR16), L"\"%s\"", ReplaceWith);
2662 }
jcarseya405b862010-09-14 05:18:09 +00002663 }
jcarsey3e082d52010-10-04 16:44:57 +00002664 if (Replace == NULL) {
2665 return (EFI_OUT_OF_RESOURCES);
2666 }
Jaben Carsey98c16be2014-08-19 21:00:34 +00002667 NewString = ZeroMem(NewString, NewSize);
jcarsey2247dde2009-11-09 18:08:58 +00002668 while (*SourceString != CHAR_NULL) {
jcarsey969c7832010-01-13 16:46:33 +00002669 //
jcarseya405b862010-09-14 05:18:09 +00002670 // if we find the FindTarget and either Skip == FALSE or Skip and we
jcarsey969c7832010-01-13 16:46:33 +00002671 // dont have a carrot do a replace...
2672 //
jcarsey1e6e84c2010-01-25 20:05:08 +00002673 if (StrnCmp(SourceString, FindTarget, StrLen(FindTarget)) == 0
jcarseya405b862010-09-14 05:18:09 +00002674 && ((SkipPreCarrot && *(SourceString-1) != L'^') || !SkipPreCarrot)
2675 ){
jcarsey975136a2009-06-16 19:03:54 +00002676 SourceString += StrLen(FindTarget);
jcarsey01582942009-07-10 19:46:17 +00002677 Size = StrSize(NewString);
jcarseya405b862010-09-14 05:18:09 +00002678 if ((Size + (StrLen(Replace)*sizeof(CHAR16))) > NewSize) {
2679 FreePool(Replace);
jcarsey975136a2009-06-16 19:03:54 +00002680 return (EFI_BUFFER_TOO_SMALL);
2681 }
Qiu Shumine75390f2015-06-30 03:18:31 +00002682 StrCatS(NewString, NewSize/sizeof(CHAR16), Replace);
jcarsey975136a2009-06-16 19:03:54 +00002683 } else {
jcarsey01582942009-07-10 19:46:17 +00002684 Size = StrSize(NewString);
2685 if (Size + sizeof(CHAR16) > NewSize) {
jcarseya405b862010-09-14 05:18:09 +00002686 FreePool(Replace);
jcarsey975136a2009-06-16 19:03:54 +00002687 return (EFI_BUFFER_TOO_SMALL);
2688 }
Qiu Shumine75390f2015-06-30 03:18:31 +00002689 StrnCatS(NewString, NewSize/sizeof(CHAR16), SourceString, 1);
jcarsey975136a2009-06-16 19:03:54 +00002690 SourceString++;
2691 }
2692 }
jcarseya405b862010-09-14 05:18:09 +00002693 FreePool(Replace);
jcarsey975136a2009-06-16 19:03:54 +00002694 return (EFI_SUCCESS);
2695}
jcarseyb1f95a02009-06-16 00:23:19 +00002696
2697/**
jcarseye2f82972009-12-01 05:40:24 +00002698 Internal worker function to output a string.
2699
2700 This function will output a string to the correct StdOut.
2701
2702 @param[in] String The string to print out.
2703
2704 @retval EFI_SUCCESS The operation was sucessful.
2705 @retval !EFI_SUCCESS The operation failed.
2706**/
2707EFI_STATUS
2708EFIAPI
2709InternalPrintTo (
2710 IN CONST CHAR16 *String
2711 )
2712{
2713 UINTN Size;
2714 Size = StrSize(String) - sizeof(CHAR16);
jcarseya405b862010-09-14 05:18:09 +00002715 if (Size == 0) {
2716 return (EFI_SUCCESS);
2717 }
jcarsey366f81a2011-06-27 21:04:22 +00002718 if (gEfiShellParametersProtocol != NULL) {
2719 return (gEfiShellProtocol->WriteFile(gEfiShellParametersProtocol->StdOut, &Size, (VOID*)String));
jcarseye2f82972009-12-01 05:40:24 +00002720 }
2721 if (mEfiShellInterface != NULL) {
jcarsey06c355b2012-03-26 21:00:39 +00002722 if (mEfiShellInterface->RedirArgc == 0) {
jcarsey49bd4982012-01-27 18:42:43 +00002723 //
2724 // Divide in half for old shell. Must be string length not size.
jcarsey06c355b2012-03-26 21:00:39 +00002725 //
2726 Size /=2; // Divide in half only when no redirection.
2727 }
jcarseya405b862010-09-14 05:18:09 +00002728 return (mEfiShellInterface->StdOut->Write(mEfiShellInterface->StdOut, &Size, (VOID*)String));
jcarseye2f82972009-12-01 05:40:24 +00002729 }
2730 ASSERT(FALSE);
2731 return (EFI_UNSUPPORTED);
2732}
2733
2734/**
jcarseyb1f95a02009-06-16 00:23:19 +00002735 Print at a specific location on the screen.
2736
jcarseyf1b87e72009-06-17 00:52:11 +00002737 This function will move the cursor to a given screen location and print the specified string
jcarsey1e6e84c2010-01-25 20:05:08 +00002738
2739 If -1 is specified for either the Row or Col the current screen location for BOTH
jcarseyf1b87e72009-06-17 00:52:11 +00002740 will be used.
jcarseyb1f95a02009-06-16 00:23:19 +00002741
2742 if either Row or Col is out of range for the current console, then ASSERT
2743 if Format is NULL, then ASSERT
2744
jcarsey1e6e84c2010-01-25 20:05:08 +00002745 In addition to the standard %-based flags as supported by UefiLib Print() this supports
jcarseyb1f95a02009-06-16 00:23:19 +00002746 the following additional flags:
2747 %N - Set output attribute to normal
2748 %H - Set output attribute to highlight
2749 %E - Set output attribute to error
2750 %B - Set output attribute to blue color
2751 %V - Set output attribute to green color
2752
2753 Note: The background color is controlled by the shell command cls.
2754
jcarseyb1f95a02009-06-16 00:23:19 +00002755 @param[in] Col the column to print at
jcarsey252d9452011-03-25 20:49:53 +00002756 @param[in] Row the row to print at
jcarseyb1f95a02009-06-16 00:23:19 +00002757 @param[in] Format the format string
jcarsey2247dde2009-11-09 18:08:58 +00002758 @param[in] Marker the marker for the variable argument list
jcarseyb1f95a02009-06-16 00:23:19 +00002759
jcarseya405b862010-09-14 05:18:09 +00002760 @return EFI_SUCCESS The operation was successful.
2761 @return EFI_DEVICE_ERROR The console device reported an error.
jcarseyb1f95a02009-06-16 00:23:19 +00002762**/
jcarseya405b862010-09-14 05:18:09 +00002763EFI_STATUS
jcarseyb1f95a02009-06-16 00:23:19 +00002764EFIAPI
jcarsey2247dde2009-11-09 18:08:58 +00002765InternalShellPrintWorker(
jcarseyb1f95a02009-06-16 00:23:19 +00002766 IN INT32 Col OPTIONAL,
2767 IN INT32 Row OPTIONAL,
2768 IN CONST CHAR16 *Format,
jcarsey252d9452011-03-25 20:49:53 +00002769 IN VA_LIST Marker
jcarsey1e6e84c2010-01-25 20:05:08 +00002770 )
jcarsey2247dde2009-11-09 18:08:58 +00002771{
jcarseyb1f95a02009-06-16 00:23:19 +00002772 EFI_STATUS Status;
jcarsey975136a2009-06-16 19:03:54 +00002773 CHAR16 *ResumeLocation;
2774 CHAR16 *FormatWalker;
jcarseya405b862010-09-14 05:18:09 +00002775 UINTN OriginalAttribute;
jcarsey89e85372011-04-13 23:37:50 +00002776 CHAR16 *mPostReplaceFormat;
2777 CHAR16 *mPostReplaceFormat2;
2778
2779 mPostReplaceFormat = AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize));
2780 mPostReplaceFormat2 = AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize));
jcarseya405b862010-09-14 05:18:09 +00002781
jcarseyf8d3e682011-04-19 17:54:42 +00002782 if (mPostReplaceFormat == NULL || mPostReplaceFormat2 == NULL) {
2783 SHELL_FREE_NON_NULL(mPostReplaceFormat);
2784 SHELL_FREE_NON_NULL(mPostReplaceFormat2);
2785 return (EFI_OUT_OF_RESOURCES);
2786 }
2787
jcarseya405b862010-09-14 05:18:09 +00002788 Status = EFI_SUCCESS;
2789 OriginalAttribute = gST->ConOut->Mode->Attribute;
jcarsey1e6e84c2010-01-25 20:05:08 +00002790
jcarsey975136a2009-06-16 19:03:54 +00002791 //
2792 // Back and forth each time fixing up 1 of our flags...
2793 //
jcarseya405b862010-09-14 05:18:09 +00002794 Status = ShellCopySearchAndReplace(Format, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%N", L"%%N", FALSE, FALSE);
jcarsey975136a2009-06-16 19:03:54 +00002795 ASSERT_EFI_ERROR(Status);
jcarseya405b862010-09-14 05:18:09 +00002796 Status = ShellCopySearchAndReplace(mPostReplaceFormat, mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), L"%E", L"%%E", FALSE, FALSE);
jcarsey975136a2009-06-16 19:03:54 +00002797 ASSERT_EFI_ERROR(Status);
jcarseya405b862010-09-14 05:18:09 +00002798 Status = ShellCopySearchAndReplace(mPostReplaceFormat2, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%H", L"%%H", FALSE, FALSE);
jcarsey975136a2009-06-16 19:03:54 +00002799 ASSERT_EFI_ERROR(Status);
jcarseya405b862010-09-14 05:18:09 +00002800 Status = ShellCopySearchAndReplace(mPostReplaceFormat, mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), L"%B", L"%%B", FALSE, FALSE);
jcarsey975136a2009-06-16 19:03:54 +00002801 ASSERT_EFI_ERROR(Status);
jcarseya405b862010-09-14 05:18:09 +00002802 Status = ShellCopySearchAndReplace(mPostReplaceFormat2, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%V", L"%%V", FALSE, FALSE);
jcarsey975136a2009-06-16 19:03:54 +00002803 ASSERT_EFI_ERROR(Status);
2804
2805 //
2806 // Use the last buffer from replacing to print from...
2807 //
jcarseya405b862010-09-14 05:18:09 +00002808 UnicodeVSPrint (mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), mPostReplaceFormat, Marker);
jcarseyb1f95a02009-06-16 00:23:19 +00002809
2810 if (Col != -1 && Row != -1) {
jcarseyb1f95a02009-06-16 00:23:19 +00002811 Status = gST->ConOut->SetCursorPosition(gST->ConOut, Col, Row);
jcarsey975136a2009-06-16 19:03:54 +00002812 }
2813
jcarseyecd3d592009-12-07 18:05:00 +00002814 FormatWalker = mPostReplaceFormat2;
jcarsey2247dde2009-11-09 18:08:58 +00002815 while (*FormatWalker != CHAR_NULL) {
jcarsey975136a2009-06-16 19:03:54 +00002816 //
2817 // Find the next attribute change request
2818 //
2819 ResumeLocation = StrStr(FormatWalker, L"%");
2820 if (ResumeLocation != NULL) {
jcarsey2247dde2009-11-09 18:08:58 +00002821 *ResumeLocation = CHAR_NULL;
jcarsey975136a2009-06-16 19:03:54 +00002822 }
2823 //
2824 // print the current FormatWalker string
2825 //
jcarseya405b862010-09-14 05:18:09 +00002826 if (StrLen(FormatWalker)>0) {
2827 Status = InternalPrintTo(FormatWalker);
2828 if (EFI_ERROR(Status)) {
2829 break;
2830 }
2831 }
2832
jcarsey975136a2009-06-16 19:03:54 +00002833 //
2834 // update the attribute
2835 //
2836 if (ResumeLocation != NULL) {
jcarsey5d46f172011-08-23 15:34:23 +00002837 if (*(ResumeLocation-1) == L'^') {
2838 //
jcarsey8bb74412012-01-30 18:44:41 +00002839 // Move cursor back 1 position to overwrite the ^
2840 //
2841 gST->ConOut->SetCursorPosition(gST->ConOut, gST->ConOut->Mode->CursorColumn - 1, gST->ConOut->Mode->CursorRow);
2842
2843 //
jcarsey5d46f172011-08-23 15:34:23 +00002844 // Print a simple '%' symbol
2845 //
2846 Status = InternalPrintTo(L"%");
2847 ResumeLocation = ResumeLocation - 1;
2848 } else {
2849 switch (*(ResumeLocation+1)) {
2850 case (L'N'):
2851 gST->ConOut->SetAttribute(gST->ConOut, OriginalAttribute);
jcarseya405b862010-09-14 05:18:09 +00002852 break;
jcarsey5d46f172011-08-23 15:34:23 +00002853 case (L'E'):
2854 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_YELLOW, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));
2855 break;
2856 case (L'H'):
2857 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_WHITE, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));
2858 break;
2859 case (L'B'):
2860 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_BLUE, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));
2861 break;
2862 case (L'V'):
2863 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_GREEN, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));
2864 break;
2865 default:
2866 //
2867 // Print a simple '%' symbol
2868 //
2869 Status = InternalPrintTo(L"%");
2870 if (EFI_ERROR(Status)) {
2871 break;
2872 }
2873 ResumeLocation = ResumeLocation - 1;
2874 break;
2875 }
jcarsey975136a2009-06-16 19:03:54 +00002876 }
2877 } else {
2878 //
2879 // reset to normal now...
2880 //
jcarsey975136a2009-06-16 19:03:54 +00002881 break;
2882 }
2883
2884 //
2885 // update FormatWalker to Resume + 2 (skip the % and the indicator)
2886 //
2887 FormatWalker = ResumeLocation + 2;
2888 }
jcarseyb1f95a02009-06-16 00:23:19 +00002889
jcarseya405b862010-09-14 05:18:09 +00002890 gST->ConOut->SetAttribute(gST->ConOut, OriginalAttribute);
jcarsey89e85372011-04-13 23:37:50 +00002891
2892 SHELL_FREE_NON_NULL(mPostReplaceFormat);
2893 SHELL_FREE_NON_NULL(mPostReplaceFormat2);
jcarseya405b862010-09-14 05:18:09 +00002894 return (Status);
jcarsey5f7431d2009-07-10 18:06:01 +00002895}
jcarsey2247dde2009-11-09 18:08:58 +00002896
2897/**
2898 Print at a specific location on the screen.
2899
jcarseye2f82972009-12-01 05:40:24 +00002900 This function will move the cursor to a given screen location and print the specified string.
jcarsey1e6e84c2010-01-25 20:05:08 +00002901
2902 If -1 is specified for either the Row or Col the current screen location for BOTH
jcarsey2247dde2009-11-09 18:08:58 +00002903 will be used.
2904
jcarseye2f82972009-12-01 05:40:24 +00002905 If either Row or Col is out of range for the current console, then ASSERT.
2906 If Format is NULL, then ASSERT.
jcarsey2247dde2009-11-09 18:08:58 +00002907
jcarsey1e6e84c2010-01-25 20:05:08 +00002908 In addition to the standard %-based flags as supported by UefiLib Print() this supports
jcarsey2247dde2009-11-09 18:08:58 +00002909 the following additional flags:
2910 %N - Set output attribute to normal
2911 %H - Set output attribute to highlight
2912 %E - Set output attribute to error
2913 %B - Set output attribute to blue color
2914 %V - Set output attribute to green color
2915
2916 Note: The background color is controlled by the shell command cls.
2917
jcarsey2247dde2009-11-09 18:08:58 +00002918 @param[in] Col the column to print at
jcarseya405b862010-09-14 05:18:09 +00002919 @param[in] Row the row to print at
jcarsey2247dde2009-11-09 18:08:58 +00002920 @param[in] Format the format string
jcarseya405b862010-09-14 05:18:09 +00002921 @param[in] ... The variable argument list.
jcarsey2247dde2009-11-09 18:08:58 +00002922
jcarseya405b862010-09-14 05:18:09 +00002923 @return EFI_SUCCESS The printing was successful.
2924 @return EFI_DEVICE_ERROR The console device reported an error.
jcarsey2247dde2009-11-09 18:08:58 +00002925**/
jcarseya405b862010-09-14 05:18:09 +00002926EFI_STATUS
jcarsey2247dde2009-11-09 18:08:58 +00002927EFIAPI
2928ShellPrintEx(
2929 IN INT32 Col OPTIONAL,
2930 IN INT32 Row OPTIONAL,
2931 IN CONST CHAR16 *Format,
2932 ...
jcarsey1e6e84c2010-01-25 20:05:08 +00002933 )
jcarsey2247dde2009-11-09 18:08:58 +00002934{
2935 VA_LIST Marker;
jcarseya405b862010-09-14 05:18:09 +00002936 EFI_STATUS RetVal;
jcarsey3e082d52010-10-04 16:44:57 +00002937 if (Format == NULL) {
2938 return (EFI_INVALID_PARAMETER);
2939 }
jcarsey2247dde2009-11-09 18:08:58 +00002940 VA_START (Marker, Format);
jcarseya405b862010-09-14 05:18:09 +00002941 RetVal = InternalShellPrintWorker(Col, Row, Format, Marker);
jcarseye2f82972009-12-01 05:40:24 +00002942 VA_END(Marker);
jcarseya405b862010-09-14 05:18:09 +00002943 return(RetVal);
jcarsey2247dde2009-11-09 18:08:58 +00002944}
2945
2946/**
2947 Print at a specific location on the screen.
2948
jcarseye2f82972009-12-01 05:40:24 +00002949 This function will move the cursor to a given screen location and print the specified string.
jcarsey1e6e84c2010-01-25 20:05:08 +00002950
2951 If -1 is specified for either the Row or Col the current screen location for BOTH
jcarseye2f82972009-12-01 05:40:24 +00002952 will be used.
jcarsey2247dde2009-11-09 18:08:58 +00002953
jcarseye2f82972009-12-01 05:40:24 +00002954 If either Row or Col is out of range for the current console, then ASSERT.
2955 If Format is NULL, then ASSERT.
jcarsey2247dde2009-11-09 18:08:58 +00002956
jcarsey1e6e84c2010-01-25 20:05:08 +00002957 In addition to the standard %-based flags as supported by UefiLib Print() this supports
jcarsey2247dde2009-11-09 18:08:58 +00002958 the following additional flags:
jcarsey1e6e84c2010-01-25 20:05:08 +00002959 %N - Set output attribute to normal.
2960 %H - Set output attribute to highlight.
2961 %E - Set output attribute to error.
2962 %B - Set output attribute to blue color.
2963 %V - Set output attribute to green color.
jcarsey2247dde2009-11-09 18:08:58 +00002964
2965 Note: The background color is controlled by the shell command cls.
2966
jcarsey1e6e84c2010-01-25 20:05:08 +00002967 @param[in] Col The column to print at.
jcarseya405b862010-09-14 05:18:09 +00002968 @param[in] Row The row to print at.
jcarsey1e6e84c2010-01-25 20:05:08 +00002969 @param[in] Language The language of the string to retrieve. If this parameter
2970 is NULL, then the current platform language is used.
2971 @param[in] HiiFormatStringId The format string Id for getting from Hii.
2972 @param[in] HiiFormatHandle The format string Handle for getting from Hii.
jcarseya405b862010-09-14 05:18:09 +00002973 @param[in] ... The variable argument list.
jcarsey2247dde2009-11-09 18:08:58 +00002974
jcarseya405b862010-09-14 05:18:09 +00002975 @return EFI_SUCCESS The printing was successful.
2976 @return EFI_DEVICE_ERROR The console device reported an error.
jcarsey2247dde2009-11-09 18:08:58 +00002977**/
jcarseya405b862010-09-14 05:18:09 +00002978EFI_STATUS
jcarsey2247dde2009-11-09 18:08:58 +00002979EFIAPI
2980ShellPrintHiiEx(
2981 IN INT32 Col OPTIONAL,
2982 IN INT32 Row OPTIONAL,
jcarsey1e6e84c2010-01-25 20:05:08 +00002983 IN CONST CHAR8 *Language OPTIONAL,
jcarsey2247dde2009-11-09 18:08:58 +00002984 IN CONST EFI_STRING_ID HiiFormatStringId,
2985 IN CONST EFI_HANDLE HiiFormatHandle,
2986 ...
2987 )
2988{
2989 VA_LIST Marker;
2990 CHAR16 *HiiFormatString;
jcarseya405b862010-09-14 05:18:09 +00002991 EFI_STATUS RetVal;
jcarsey2247dde2009-11-09 18:08:58 +00002992
Ruiyu Nieeb97442016-07-14 13:15:34 +08002993 RetVal = EFI_DEVICE_ERROR;
2994
jcarsey2247dde2009-11-09 18:08:58 +00002995 VA_START (Marker, HiiFormatHandle);
jcarsey1e6e84c2010-01-25 20:05:08 +00002996 HiiFormatString = HiiGetString(HiiFormatHandle, HiiFormatStringId, Language);
Ruiyu Nieeb97442016-07-14 13:15:34 +08002997 if (HiiFormatString != NULL) {
2998 RetVal = InternalShellPrintWorker (Col, Row, HiiFormatString, Marker);
2999 SHELL_FREE_NON_NULL (HiiFormatString);
3000 }
jcarseye2f82972009-12-01 05:40:24 +00003001 VA_END(Marker);
jcarsey2247dde2009-11-09 18:08:58 +00003002
3003 return (RetVal);
3004}
3005
3006/**
3007 Function to determine if a given filename represents a file or a directory.
3008
3009 @param[in] DirName Path to directory to test.
3010
jcarseyc8c22592011-10-17 17:49:21 +00003011 @retval EFI_SUCCESS The Path represents a directory
3012 @retval EFI_NOT_FOUND The Path does not represent a directory
3013 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
3014 @return The path failed to open
jcarsey2247dde2009-11-09 18:08:58 +00003015**/
3016EFI_STATUS
3017EFIAPI
3018ShellIsDirectory(
3019 IN CONST CHAR16 *DirName
3020 )
3021{
3022 EFI_STATUS Status;
jcarseya405b862010-09-14 05:18:09 +00003023 SHELL_FILE_HANDLE Handle;
jcarsey3e082d52010-10-04 16:44:57 +00003024 CHAR16 *TempLocation;
3025 CHAR16 *TempLocation2;
jcarsey2247dde2009-11-09 18:08:58 +00003026
jcarseyecd3d592009-12-07 18:05:00 +00003027 ASSERT(DirName != NULL);
3028
jcarseya405b862010-09-14 05:18:09 +00003029 Handle = NULL;
3030 TempLocation = NULL;
jcarsey2247dde2009-11-09 18:08:58 +00003031
3032 Status = ShellOpenFileByName(DirName, &Handle, EFI_FILE_MODE_READ, 0);
3033 if (EFI_ERROR(Status)) {
jcarseya405b862010-09-14 05:18:09 +00003034 //
3035 // try good logic first.
3036 //
jcarsey366f81a2011-06-27 21:04:22 +00003037 if (gEfiShellProtocol != NULL) {
jcarsey3e082d52010-10-04 16:44:57 +00003038 TempLocation = StrnCatGrow(&TempLocation, NULL, DirName, 0);
jcarseyc8c22592011-10-17 17:49:21 +00003039 if (TempLocation == NULL) {
3040 ShellCloseFile(&Handle);
3041 return (EFI_OUT_OF_RESOURCES);
3042 }
jcarsey3e082d52010-10-04 16:44:57 +00003043 TempLocation2 = StrStr(TempLocation, L":");
3044 if (TempLocation2 != NULL && StrLen(StrStr(TempLocation, L":")) == 2) {
3045 *(TempLocation2+1) = CHAR_NULL;
jcarseya405b862010-09-14 05:18:09 +00003046 }
jcarsey366f81a2011-06-27 21:04:22 +00003047 if (gEfiShellProtocol->GetDevicePathFromMap(TempLocation) != NULL) {
jcarseya405b862010-09-14 05:18:09 +00003048 FreePool(TempLocation);
3049 return (EFI_SUCCESS);
3050 }
3051 FreePool(TempLocation);
3052 } else {
3053 //
3054 // probably a map name?!?!!?
3055 //
3056 TempLocation = StrStr(DirName, L"\\");
3057 if (TempLocation != NULL && *(TempLocation+1) == CHAR_NULL) {
3058 return (EFI_SUCCESS);
3059 }
3060 }
jcarsey2247dde2009-11-09 18:08:58 +00003061 return (Status);
3062 }
3063
3064 if (FileHandleIsDirectory(Handle) == EFI_SUCCESS) {
3065 ShellCloseFile(&Handle);
3066 return (EFI_SUCCESS);
3067 }
3068 ShellCloseFile(&Handle);
3069 return (EFI_NOT_FOUND);
3070}
3071
jcarsey125c2cf2009-11-18 21:36:50 +00003072/**
jcarsey36a9d672009-11-20 21:13:41 +00003073 Function to determine if a given filename represents a file.
3074
3075 @param[in] Name Path to file to test.
3076
3077 @retval EFI_SUCCESS The Path represents a file.
3078 @retval EFI_NOT_FOUND The Path does not represent a file.
3079 @retval other The path failed to open.
3080**/
3081EFI_STATUS
3082EFIAPI
3083ShellIsFile(
3084 IN CONST CHAR16 *Name
3085 )
3086{
3087 EFI_STATUS Status;
jcarseya405b862010-09-14 05:18:09 +00003088 SHELL_FILE_HANDLE Handle;
jcarsey36a9d672009-11-20 21:13:41 +00003089
jcarseyecd3d592009-12-07 18:05:00 +00003090 ASSERT(Name != NULL);
3091
jcarsey36a9d672009-11-20 21:13:41 +00003092 Handle = NULL;
3093
3094 Status = ShellOpenFileByName(Name, &Handle, EFI_FILE_MODE_READ, 0);
3095 if (EFI_ERROR(Status)) {
3096 return (Status);
3097 }
3098
3099 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {
3100 ShellCloseFile(&Handle);
3101 return (EFI_SUCCESS);
3102 }
3103 ShellCloseFile(&Handle);
3104 return (EFI_NOT_FOUND);
3105}
3106
3107/**
jcarseyb3011f42010-01-11 21:49:04 +00003108 Function to determine if a given filename represents a file.
3109
3110 This will search the CWD and then the Path.
3111
3112 If Name is NULL, then ASSERT.
3113
3114 @param[in] Name Path to file to test.
3115
3116 @retval EFI_SUCCESS The Path represents a file.
3117 @retval EFI_NOT_FOUND The Path does not represent a file.
3118 @retval other The path failed to open.
3119**/
3120EFI_STATUS
3121EFIAPI
3122ShellIsFileInPath(
3123 IN CONST CHAR16 *Name
jcarseya405b862010-09-14 05:18:09 +00003124 )
3125{
jcarseyb3011f42010-01-11 21:49:04 +00003126 CHAR16 *NewName;
3127 EFI_STATUS Status;
3128
3129 if (!EFI_ERROR(ShellIsFile(Name))) {
jcarseya405b862010-09-14 05:18:09 +00003130 return (EFI_SUCCESS);
jcarseyb3011f42010-01-11 21:49:04 +00003131 }
3132
3133 NewName = ShellFindFilePath(Name);
3134 if (NewName == NULL) {
3135 return (EFI_NOT_FOUND);
3136 }
3137 Status = ShellIsFile(NewName);
3138 FreePool(NewName);
3139 return (Status);
3140}
jcarsey252d9452011-03-25 20:49:53 +00003141
jcarseyb3011f42010-01-11 21:49:04 +00003142/**
Jaben Carsey74b0fb82013-11-22 21:37:34 +00003143 Function return the number converted from a hex representation of a number.
3144
3145 Note: this function cannot be used when (UINTN)(-1), (0xFFFFFFFF) may be a valid
3146 result. Use ShellConvertStringToUint64 instead.
3147
3148 @param[in] String String representation of a number.
3149
3150 @return The unsigned integer result of the conversion.
3151 @retval (UINTN)(-1) An error occured.
3152**/
3153UINTN
3154EFIAPI
3155ShellHexStrToUintn(
3156 IN CONST CHAR16 *String
3157 )
3158{
3159 UINT64 RetVal;
3160
3161 if (!EFI_ERROR(ShellConvertStringToUint64(String, &RetVal, TRUE, TRUE))) {
3162 return ((UINTN)RetVal);
3163 }
3164
3165 return ((UINTN)(-1));
3166}
3167
3168/**
jcarsey1e6e84c2010-01-25 20:05:08 +00003169 Function to determine whether a string is decimal or hex representation of a number
Jaben Carseyd59fc242013-11-14 23:52:43 +00003170 and return the number converted from the string. Spaces are always skipped.
jcarsey125c2cf2009-11-18 21:36:50 +00003171
3172 @param[in] String String representation of a number
3173
jcarsey252d9452011-03-25 20:49:53 +00003174 @return the number
3175 @retval (UINTN)(-1) An error ocurred.
jcarsey125c2cf2009-11-18 21:36:50 +00003176**/
3177UINTN
3178EFIAPI
3179ShellStrToUintn(
3180 IN CONST CHAR16 *String
3181 )
3182{
jcarsey252d9452011-03-25 20:49:53 +00003183 UINT64 RetVal;
3184 BOOLEAN Hex;
3185
3186 Hex = FALSE;
3187
jcarsey658bf432014-11-04 22:33:16 +00003188 if (!InternalShellIsHexOrDecimalNumber(String, Hex, TRUE, FALSE)) {
jcarsey252d9452011-03-25 20:49:53 +00003189 Hex = TRUE;
jcarsey125c2cf2009-11-18 21:36:50 +00003190 }
jcarsey252d9452011-03-25 20:49:53 +00003191
3192 if (!EFI_ERROR(ShellConvertStringToUint64(String, &RetVal, Hex, TRUE))) {
3193 return ((UINTN)RetVal);
3194 }
3195 return ((UINTN)(-1));
jcarsey125c2cf2009-11-18 21:36:50 +00003196}
3197
3198/**
3199 Safely append with automatic string resizing given length of Destination and
3200 desired length of copy from Source.
3201
3202 append the first D characters of Source to the end of Destination, where D is
3203 the lesser of Count and the StrLen() of Source. If appending those D characters
3204 will fit within Destination (whose Size is given as CurrentSize) and
jcarsey1e6e84c2010-01-25 20:05:08 +00003205 still leave room for a NULL terminator, then those characters are appended,
3206 starting at the original terminating NULL of Destination, and a new terminating
3207 NULL is appended.
jcarsey125c2cf2009-11-18 21:36:50 +00003208
3209 If appending D characters onto Destination will result in a overflow of the size
3210 given in CurrentSize the string will be grown such that the copy can be performed
3211 and CurrentSize will be updated to the new size.
3212
3213 If Source is NULL, there is nothing to append, just return the current buffer in
3214 Destination.
3215
3216 if Destination is NULL, then ASSERT()
3217 if Destination's current length (including NULL terminator) is already more then
3218 CurrentSize, then ASSERT()
3219
ydong104ff7e372011-09-02 08:05:34 +00003220 @param[in, out] Destination The String to append onto
3221 @param[in, out] CurrentSize on call the number of bytes in Destination. On
jcarsey125c2cf2009-11-18 21:36:50 +00003222 return possibly the new size (still in bytes). if NULL
3223 then allocate whatever is needed.
3224 @param[in] Source The String to append from
3225 @param[in] Count Maximum number of characters to append. if 0 then
3226 all are appended.
3227
3228 @return Destination return the resultant string.
3229**/
3230CHAR16*
3231EFIAPI
3232StrnCatGrow (
3233 IN OUT CHAR16 **Destination,
3234 IN OUT UINTN *CurrentSize,
3235 IN CONST CHAR16 *Source,
3236 IN UINTN Count
3237 )
3238{
3239 UINTN DestinationStartSize;
3240 UINTN NewSize;
3241
3242 //
3243 // ASSERTs
3244 //
3245 ASSERT(Destination != NULL);
3246
3247 //
3248 // If there's nothing to do then just return Destination
3249 //
3250 if (Source == NULL) {
3251 return (*Destination);
3252 }
3253
3254 //
3255 // allow for un-initialized pointers, based on size being 0
3256 //
3257 if (CurrentSize != NULL && *CurrentSize == 0) {
3258 *Destination = NULL;
3259 }
3260
3261 //
3262 // allow for NULL pointers address as Destination
3263 //
3264 if (*Destination != NULL) {
3265 ASSERT(CurrentSize != 0);
3266 DestinationStartSize = StrSize(*Destination);
3267 ASSERT(DestinationStartSize <= *CurrentSize);
3268 } else {
3269 DestinationStartSize = 0;
3270// ASSERT(*CurrentSize == 0);
3271 }
3272
3273 //
3274 // Append all of Source?
3275 //
3276 if (Count == 0) {
3277 Count = StrLen(Source);
3278 }
3279
3280 //
3281 // Test and grow if required
3282 //
3283 if (CurrentSize != NULL) {
3284 NewSize = *CurrentSize;
ydong10f480fdc2012-09-07 01:55:33 +00003285 if (NewSize < DestinationStartSize + (Count * sizeof(CHAR16))) {
3286 while (NewSize < (DestinationStartSize + (Count*sizeof(CHAR16)))) {
3287 NewSize += 2 * Count * sizeof(CHAR16);
3288 }
3289 *Destination = ReallocatePool(*CurrentSize, NewSize, *Destination);
3290 *CurrentSize = NewSize;
jcarsey125c2cf2009-11-18 21:36:50 +00003291 }
jcarsey125c2cf2009-11-18 21:36:50 +00003292 } else {
Qiu Shuminc587fd32015-07-02 01:12:03 +00003293 NewSize = (Count+1)*sizeof(CHAR16);
3294 *Destination = AllocateZeroPool(NewSize);
jcarsey125c2cf2009-11-18 21:36:50 +00003295 }
3296
3297 //
3298 // Now use standard StrnCat on a big enough buffer
3299 //
jcarseyc9d92df2010-02-03 15:37:54 +00003300 if (*Destination == NULL) {
3301 return (NULL);
3302 }
Qiu Shumine75390f2015-06-30 03:18:31 +00003303
Qiu Shuminc587fd32015-07-02 01:12:03 +00003304 StrnCatS(*Destination, NewSize/sizeof(CHAR16), Source, Count);
Qiu Shumine75390f2015-06-30 03:18:31 +00003305 return *Destination;
jcarsey125c2cf2009-11-18 21:36:50 +00003306}
jcarseyc9d92df2010-02-03 15:37:54 +00003307
3308/**
3309 Prompt the user and return the resultant answer to the requestor.
3310
3311 This function will display the requested question on the shell prompt and then
Mudusuru, Giri P1d322462016-07-07 00:47:45 -07003312 wait for an appropriate answer to be input from the console.
jcarseyc9d92df2010-02-03 15:37:54 +00003313
jcarseya405b862010-09-14 05:18:09 +00003314 if the SHELL_PROMPT_REQUEST_TYPE is SHELL_PROMPT_REQUEST_TYPE_YESNO, ShellPromptResponseTypeQuitContinue
jcarseyc9d92df2010-02-03 15:37:54 +00003315 or SHELL_PROMPT_REQUEST_TYPE_YESNOCANCEL then *Response is of type SHELL_PROMPT_RESPONSE.
3316
jcarseya405b862010-09-14 05:18:09 +00003317 if the SHELL_PROMPT_REQUEST_TYPE is ShellPromptResponseTypeFreeform then *Response is of type
jcarseyc9d92df2010-02-03 15:37:54 +00003318 CHAR16*.
3319
3320 In either case *Response must be callee freed if Response was not NULL;
3321
3322 @param Type What type of question is asked. This is used to filter the input
3323 to prevent invalid answers to question.
3324 @param Prompt Pointer to string prompt to use to request input.
3325 @param Response Pointer to Response which will be populated upon return.
3326
3327 @retval EFI_SUCCESS The operation was sucessful.
3328 @retval EFI_UNSUPPORTED The operation is not supported as requested.
3329 @retval EFI_INVALID_PARAMETER A parameter was invalid.
3330 @return other The operation failed.
3331**/
3332EFI_STATUS
3333EFIAPI
3334ShellPromptForResponse (
3335 IN SHELL_PROMPT_REQUEST_TYPE Type,
3336 IN CHAR16 *Prompt OPTIONAL,
3337 IN OUT VOID **Response OPTIONAL
3338 )
3339{
3340 EFI_STATUS Status;
3341 EFI_INPUT_KEY Key;
3342 UINTN EventIndex;
3343 SHELL_PROMPT_RESPONSE *Resp;
jcarseya405b862010-09-14 05:18:09 +00003344 UINTN Size;
3345 CHAR16 *Buffer;
jcarseyc9d92df2010-02-03 15:37:54 +00003346
jcarseya405b862010-09-14 05:18:09 +00003347 Status = EFI_UNSUPPORTED;
3348 Resp = NULL;
3349 Buffer = NULL;
3350 Size = 0;
3351 if (Type != ShellPromptResponseTypeFreeform) {
jcarsey252d9452011-03-25 20:49:53 +00003352 Resp = (SHELL_PROMPT_RESPONSE*)AllocateZeroPool(sizeof(SHELL_PROMPT_RESPONSE));
jcarsey3e082d52010-10-04 16:44:57 +00003353 if (Resp == NULL) {
3354 return (EFI_OUT_OF_RESOURCES);
3355 }
xdu290bfa222010-07-19 05:21:27 +00003356 }
jcarseyc9d92df2010-02-03 15:37:54 +00003357
3358 switch(Type) {
jcarseya405b862010-09-14 05:18:09 +00003359 case ShellPromptResponseTypeQuitContinue:
jcarseyc9d92df2010-02-03 15:37:54 +00003360 if (Prompt != NULL) {
3361 ShellPrintEx(-1, -1, L"%s", Prompt);
3362 }
3363 //
3364 // wait for valid response
3365 //
3366 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
3367 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
Jaben Carsey31b018a2014-01-16 16:52:39 +00003368 if (EFI_ERROR(Status)) {
3369 break;
3370 }
jcarseyc9d92df2010-02-03 15:37:54 +00003371 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3372 if (Key.UnicodeChar == L'Q' || Key.UnicodeChar ==L'q') {
jcarseya405b862010-09-14 05:18:09 +00003373 *Resp = ShellPromptResponseQuit;
jcarseyc9d92df2010-02-03 15:37:54 +00003374 } else {
jcarseya405b862010-09-14 05:18:09 +00003375 *Resp = ShellPromptResponseContinue;
jcarseyc9d92df2010-02-03 15:37:54 +00003376 }
3377 break;
jcarseya405b862010-09-14 05:18:09 +00003378 case ShellPromptResponseTypeYesNoCancel:
jcarseyc9d92df2010-02-03 15:37:54 +00003379 if (Prompt != NULL) {
3380 ShellPrintEx(-1, -1, L"%s", Prompt);
3381 }
3382 //
3383 // wait for valid response
3384 //
jcarseya405b862010-09-14 05:18:09 +00003385 *Resp = ShellPromptResponseMax;
3386 while (*Resp == ShellPromptResponseMax) {
Jaben Carsey194ae482013-12-09 22:55:13 +00003387 if (ShellGetExecutionBreakFlag()) {
3388 Status = EFI_ABORTED;
3389 break;
3390 }
jcarseyc9d92df2010-02-03 15:37:54 +00003391 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
3392 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
Jaben Carsey31b018a2014-01-16 16:52:39 +00003393 if (EFI_ERROR(Status)) {
3394 break;
3395 }
jcarseyc9d92df2010-02-03 15:37:54 +00003396 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3397 switch (Key.UnicodeChar) {
3398 case L'Y':
3399 case L'y':
jcarseya405b862010-09-14 05:18:09 +00003400 *Resp = ShellPromptResponseYes;
jcarseyc9d92df2010-02-03 15:37:54 +00003401 break;
3402 case L'N':
3403 case L'n':
jcarseya405b862010-09-14 05:18:09 +00003404 *Resp = ShellPromptResponseNo;
jcarseyc9d92df2010-02-03 15:37:54 +00003405 break;
3406 case L'C':
3407 case L'c':
jcarseya405b862010-09-14 05:18:09 +00003408 *Resp = ShellPromptResponseCancel;
3409 break;
3410 }
3411 }
3412 break; case ShellPromptResponseTypeYesNoAllCancel:
3413 if (Prompt != NULL) {
3414 ShellPrintEx(-1, -1, L"%s", Prompt);
3415 }
3416 //
3417 // wait for valid response
3418 //
3419 *Resp = ShellPromptResponseMax;
3420 while (*Resp == ShellPromptResponseMax) {
Jaben Carsey194ae482013-12-09 22:55:13 +00003421 if (ShellGetExecutionBreakFlag()) {
3422 Status = EFI_ABORTED;
3423 break;
3424 }
jcarseya405b862010-09-14 05:18:09 +00003425 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
3426 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
Jaben Carsey31b018a2014-01-16 16:52:39 +00003427 if (EFI_ERROR(Status)) {
3428 break;
3429 }
jcarseya405b862010-09-14 05:18:09 +00003430 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3431 switch (Key.UnicodeChar) {
3432 case L'Y':
3433 case L'y':
3434 *Resp = ShellPromptResponseYes;
3435 break;
3436 case L'N':
3437 case L'n':
3438 *Resp = ShellPromptResponseNo;
3439 break;
3440 case L'A':
3441 case L'a':
3442 *Resp = ShellPromptResponseAll;
3443 break;
3444 case L'C':
3445 case L'c':
3446 *Resp = ShellPromptResponseCancel;
jcarseyc9d92df2010-02-03 15:37:54 +00003447 break;
3448 }
3449 }
3450 break;
jcarseya405b862010-09-14 05:18:09 +00003451 case ShellPromptResponseTypeEnterContinue:
3452 case ShellPromptResponseTypeAnyKeyContinue:
jcarseyc9d92df2010-02-03 15:37:54 +00003453 if (Prompt != NULL) {
3454 ShellPrintEx(-1, -1, L"%s", Prompt);
3455 }
3456 //
3457 // wait for valid response
3458 //
jcarseya405b862010-09-14 05:18:09 +00003459 *Resp = ShellPromptResponseMax;
3460 while (*Resp == ShellPromptResponseMax) {
Jaben Carsey194ae482013-12-09 22:55:13 +00003461 if (ShellGetExecutionBreakFlag()) {
3462 Status = EFI_ABORTED;
3463 break;
3464 }
jcarseyc9d92df2010-02-03 15:37:54 +00003465 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
jcarseya405b862010-09-14 05:18:09 +00003466 if (Type == ShellPromptResponseTypeEnterContinue) {
jcarseyc9d92df2010-02-03 15:37:54 +00003467 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
Jaben Carsey31b018a2014-01-16 16:52:39 +00003468 if (EFI_ERROR(Status)) {
3469 break;
3470 }
jcarseyc9d92df2010-02-03 15:37:54 +00003471 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3472 if (Key.UnicodeChar == CHAR_CARRIAGE_RETURN) {
jcarseya405b862010-09-14 05:18:09 +00003473 *Resp = ShellPromptResponseContinue;
jcarseyc9d92df2010-02-03 15:37:54 +00003474 break;
3475 }
3476 }
jcarseya405b862010-09-14 05:18:09 +00003477 if (Type == ShellPromptResponseTypeAnyKeyContinue) {
3478 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
3479 ASSERT_EFI_ERROR(Status);
3480 *Resp = ShellPromptResponseContinue;
jcarseyc9d92df2010-02-03 15:37:54 +00003481 break;
3482 }
3483 }
3484 break;
jcarseya405b862010-09-14 05:18:09 +00003485 case ShellPromptResponseTypeYesNo:
3486 if (Prompt != NULL) {
3487 ShellPrintEx(-1, -1, L"%s", Prompt);
3488 }
3489 //
3490 // wait for valid response
3491 //
3492 *Resp = ShellPromptResponseMax;
3493 while (*Resp == ShellPromptResponseMax) {
Jaben Carsey194ae482013-12-09 22:55:13 +00003494 if (ShellGetExecutionBreakFlag()) {
3495 Status = EFI_ABORTED;
3496 break;
3497 }
jcarseya405b862010-09-14 05:18:09 +00003498 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
3499 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
Jaben Carsey31b018a2014-01-16 16:52:39 +00003500 if (EFI_ERROR(Status)) {
3501 break;
3502 }
jcarseya405b862010-09-14 05:18:09 +00003503 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3504 switch (Key.UnicodeChar) {
3505 case L'Y':
3506 case L'y':
3507 *Resp = ShellPromptResponseYes;
3508 break;
3509 case L'N':
3510 case L'n':
3511 *Resp = ShellPromptResponseNo;
3512 break;
3513 }
3514 }
3515 break;
3516 case ShellPromptResponseTypeFreeform:
3517 if (Prompt != NULL) {
3518 ShellPrintEx(-1, -1, L"%s", Prompt);
3519 }
3520 while(1) {
Jaben Carsey194ae482013-12-09 22:55:13 +00003521 if (ShellGetExecutionBreakFlag()) {
3522 Status = EFI_ABORTED;
3523 break;
3524 }
jcarseya405b862010-09-14 05:18:09 +00003525 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
3526 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
Jaben Carsey31b018a2014-01-16 16:52:39 +00003527 if (EFI_ERROR(Status)) {
3528 break;
3529 }
jcarseya405b862010-09-14 05:18:09 +00003530 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3531 if (Key.UnicodeChar == CHAR_CARRIAGE_RETURN) {
3532 break;
3533 }
3534 ASSERT((Buffer == NULL && Size == 0) || (Buffer != NULL));
3535 StrnCatGrow(&Buffer, &Size, &Key.UnicodeChar, 1);
3536 }
3537 break;
3538 //
3539 // This is the location to add new prompt types.
Jaben Carsey194ae482013-12-09 22:55:13 +00003540 // If your new type loops remember to add ExecutionBreak support.
jcarseya405b862010-09-14 05:18:09 +00003541 //
jcarseyc9d92df2010-02-03 15:37:54 +00003542 default:
jcarseya405b862010-09-14 05:18:09 +00003543 ASSERT(FALSE);
jcarseyc9d92df2010-02-03 15:37:54 +00003544 }
3545
3546 if (Response != NULL) {
jcarseya405b862010-09-14 05:18:09 +00003547 if (Resp != NULL) {
3548 *Response = Resp;
3549 } else if (Buffer != NULL) {
3550 *Response = Buffer;
3551 }
jcarseyc9d92df2010-02-03 15:37:54 +00003552 } else {
jcarseya405b862010-09-14 05:18:09 +00003553 if (Resp != NULL) {
3554 FreePool(Resp);
3555 }
3556 if (Buffer != NULL) {
3557 FreePool(Buffer);
3558 }
jcarseyc9d92df2010-02-03 15:37:54 +00003559 }
3560
jcarseya405b862010-09-14 05:18:09 +00003561 ShellPrintEx(-1, -1, L"\r\n");
jcarseyc9d92df2010-02-03 15:37:54 +00003562 return (Status);
3563}
3564
3565/**
3566 Prompt the user and return the resultant answer to the requestor.
3567
3568 This function is the same as ShellPromptForResponse, except that the prompt is
3569 automatically pulled from HII.
3570
3571 @param Type What type of question is asked. This is used to filter the input
3572 to prevent invalid answers to question.
jcarseya405b862010-09-14 05:18:09 +00003573 @param[in] HiiFormatStringId The format string Id for getting from Hii.
3574 @param[in] HiiFormatHandle The format string Handle for getting from Hii.
3575 @param Response Pointer to Response which will be populated upon return.
jcarseyc9d92df2010-02-03 15:37:54 +00003576
3577 @retval EFI_SUCCESS the operation was sucessful.
3578 @return other the operation failed.
3579
3580 @sa ShellPromptForResponse
3581**/
3582EFI_STATUS
3583EFIAPI
3584ShellPromptForResponseHii (
3585 IN SHELL_PROMPT_REQUEST_TYPE Type,
3586 IN CONST EFI_STRING_ID HiiFormatStringId,
3587 IN CONST EFI_HANDLE HiiFormatHandle,
3588 IN OUT VOID **Response
3589 )
3590{
3591 CHAR16 *Prompt;
3592 EFI_STATUS Status;
3593
3594 Prompt = HiiGetString(HiiFormatHandle, HiiFormatStringId, NULL);
3595 Status = ShellPromptForResponse(Type, Prompt, Response);
3596 FreePool(Prompt);
3597 return (Status);
3598}
3599
jcarseya405b862010-09-14 05:18:09 +00003600/**
3601 Function to determin if an entire string is a valid number.
jcarseyc9d92df2010-02-03 15:37:54 +00003602
jcarseya405b862010-09-14 05:18:09 +00003603 If Hex it must be preceeded with a 0x or has ForceHex, set TRUE.
3604
3605 @param[in] String The string to evaluate.
3606 @param[in] ForceHex TRUE - always assume hex.
3607 @param[in] StopAtSpace TRUE to halt upon finding a space, FALSE to keep going.
jcarsey658bf432014-11-04 22:33:16 +00003608 @param[in] TimeNumbers TRUE to allow numbers with ":", FALSE otherwise.
jcarseya405b862010-09-14 05:18:09 +00003609
3610 @retval TRUE It is all numeric (dec/hex) characters.
3611 @retval FALSE There is a non-numeric character.
3612**/
3613BOOLEAN
3614EFIAPI
jcarsey252d9452011-03-25 20:49:53 +00003615InternalShellIsHexOrDecimalNumber (
jcarseya405b862010-09-14 05:18:09 +00003616 IN CONST CHAR16 *String,
3617 IN CONST BOOLEAN ForceHex,
jcarsey658bf432014-11-04 22:33:16 +00003618 IN CONST BOOLEAN StopAtSpace,
3619 IN CONST BOOLEAN TimeNumbers
jcarseya405b862010-09-14 05:18:09 +00003620 )
3621{
3622 BOOLEAN Hex;
3623
3624 //
3625 // chop off a single negative sign
3626 //
3627 if (String != NULL && *String == L'-') {
3628 String++;
3629 }
darylm503b0934ac2011-11-12 00:35:11 +00003630
jcarseya405b862010-09-14 05:18:09 +00003631 if (String == NULL) {
3632 return (FALSE);
3633 }
3634
3635 //
3636 // chop leading zeroes
3637 //
3638 while(String != NULL && *String == L'0'){
3639 String++;
3640 }
3641 //
3642 // allow '0x' or '0X', but not 'x' or 'X'
3643 //
3644 if (String != NULL && (*String == L'x' || *String == L'X')) {
3645 if (*(String-1) != L'0') {
3646 //
3647 // we got an x without a preceeding 0
3648 //
3649 return (FALSE);
3650 }
3651 String++;
3652 Hex = TRUE;
3653 } else if (ForceHex) {
3654 Hex = TRUE;
3655 } else {
3656 Hex = FALSE;
3657 }
3658
3659 //
3660 // loop through the remaining characters and use the lib function
3661 //
3662 for ( ; String != NULL && *String != CHAR_NULL && !(StopAtSpace && *String == L' ') ; String++){
jcarsey658bf432014-11-04 22:33:16 +00003663 if (TimeNumbers && (String[0] == L':')) {
3664 continue;
3665 }
jcarseya405b862010-09-14 05:18:09 +00003666 if (Hex) {
3667 if (!ShellIsHexaDecimalDigitCharacter(*String)) {
3668 return (FALSE);
3669 }
3670 } else {
3671 if (!ShellIsDecimalDigitCharacter(*String)) {
3672 return (FALSE);
3673 }
3674 }
3675 }
jcarsey252d9452011-03-25 20:49:53 +00003676
jcarseya405b862010-09-14 05:18:09 +00003677 return (TRUE);
3678}
3679
3680/**
3681 Function to determine if a given filename exists.
3682
3683 @param[in] Name Path to test.
3684
3685 @retval EFI_SUCCESS The Path represents a file.
3686 @retval EFI_NOT_FOUND The Path does not represent a file.
3687 @retval other The path failed to open.
3688**/
3689EFI_STATUS
3690EFIAPI
3691ShellFileExists(
3692 IN CONST CHAR16 *Name
3693 )
3694{
3695 EFI_STATUS Status;
3696 EFI_SHELL_FILE_INFO *List;
3697
3698 ASSERT(Name != NULL);
3699
3700 List = NULL;
3701 Status = ShellOpenFileMetaArg((CHAR16*)Name, EFI_FILE_MODE_READ, &List);
3702 if (EFI_ERROR(Status)) {
3703 return (Status);
3704 }
3705
3706 ShellCloseFileMetaArg(&List);
3707
3708 return (EFI_SUCCESS);
3709}
jcarsey252d9452011-03-25 20:49:53 +00003710
3711/**
darylm503b0934ac2011-11-12 00:35:11 +00003712 Convert a Unicode character to upper case only if
jcarsey252d9452011-03-25 20:49:53 +00003713 it maps to a valid small-case ASCII character.
3714
3715 This internal function only deal with Unicode character
3716 which maps to a valid small-case ASCII character, i.e.
3717 L'a' to L'z'. For other Unicode character, the input character
3718 is returned directly.
3719
3720 @param Char The character to convert.
3721
3722 @retval LowerCharacter If the Char is with range L'a' to L'z'.
3723 @retval Unchanged Otherwise.
3724
3725**/
3726CHAR16
3727EFIAPI
3728InternalShellCharToUpper (
3729 IN CHAR16 Char
3730 )
3731{
3732 if (Char >= L'a' && Char <= L'z') {
3733 return (CHAR16) (Char - (L'a' - L'A'));
3734 }
3735
3736 return Char;
3737}
3738
3739/**
3740 Convert a Unicode character to numerical value.
3741
3742 This internal function only deal with Unicode character
3743 which maps to a valid hexadecimal ASII character, i.e.
darylm503b0934ac2011-11-12 00:35:11 +00003744 L'0' to L'9', L'a' to L'f' or L'A' to L'F'. For other
jcarsey252d9452011-03-25 20:49:53 +00003745 Unicode character, the value returned does not make sense.
3746
3747 @param Char The character to convert.
3748
3749 @return The numerical value converted.
3750
3751**/
3752UINTN
3753EFIAPI
3754InternalShellHexCharToUintn (
3755 IN CHAR16 Char
3756 )
3757{
3758 if (ShellIsDecimalDigitCharacter (Char)) {
3759 return Char - L'0';
3760 }
3761
3762 return (UINTN) (10 + InternalShellCharToUpper (Char) - L'A');
3763}
3764
3765/**
3766 Convert a Null-terminated Unicode hexadecimal string to a value of type UINT64.
3767
Jaben Carsey80f3e342015-07-02 17:26:42 +00003768 This function returns a value of type UINT64 by interpreting the contents
jcarsey252d9452011-03-25 20:49:53 +00003769 of the Unicode string specified by String as a hexadecimal number.
3770 The format of the input Unicode string String is:
3771
3772 [spaces][zeros][x][hexadecimal digits].
3773
3774 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
3775 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix.
3776 If "x" appears in the input string, it must be prefixed with at least one 0.
3777 The function will ignore the pad space, which includes spaces or tab characters,
3778 before [zeros], [x] or [hexadecimal digit]. The running zero before [x] or
3779 [hexadecimal digit] will be ignored. Then, the decoding starts after [x] or the
3780 first valid hexadecimal digit. Then, the function stops at the first character that is
3781 a not a valid hexadecimal character or NULL, whichever one comes first.
3782
3783 If String has only pad spaces, then zero is returned.
3784 If String has no leading pad spaces, leading zeros or valid hexadecimal digits,
3785 then zero is returned.
3786
3787 @param[in] String A pointer to a Null-terminated Unicode string.
3788 @param[out] Value Upon a successful return the value of the conversion.
3789 @param[in] StopAtSpace FALSE to skip spaces.
3790
3791 @retval EFI_SUCCESS The conversion was successful.
3792 @retval EFI_INVALID_PARAMETER A parameter was NULL or invalid.
3793 @retval EFI_DEVICE_ERROR An overflow occured.
3794**/
3795EFI_STATUS
3796EFIAPI
3797InternalShellStrHexToUint64 (
3798 IN CONST CHAR16 *String,
3799 OUT UINT64 *Value,
3800 IN CONST BOOLEAN StopAtSpace
3801 )
3802{
3803 UINT64 Result;
3804
3805 if (String == NULL || StrSize(String) == 0 || Value == NULL) {
3806 return (EFI_INVALID_PARAMETER);
3807 }
darylm503b0934ac2011-11-12 00:35:11 +00003808
jcarsey252d9452011-03-25 20:49:53 +00003809 //
darylm503b0934ac2011-11-12 00:35:11 +00003810 // Ignore the pad spaces (space or tab)
jcarsey252d9452011-03-25 20:49:53 +00003811 //
3812 while ((*String == L' ') || (*String == L'\t')) {
3813 String++;
3814 }
3815
3816 //
3817 // Ignore leading Zeros after the spaces
3818 //
3819 while (*String == L'0') {
3820 String++;
3821 }
3822
3823 if (InternalShellCharToUpper (*String) == L'X') {
3824 if (*(String - 1) != L'0') {
3825 return 0;
3826 }
3827 //
3828 // Skip the 'X'
3829 //
3830 String++;
3831 }
3832
3833 Result = 0;
darylm503b0934ac2011-11-12 00:35:11 +00003834
jcarsey252d9452011-03-25 20:49:53 +00003835 //
Jaben Carsey80f3e342015-07-02 17:26:42 +00003836 // there is a space where there should't be
jcarsey252d9452011-03-25 20:49:53 +00003837 //
Jaben Carsey80f3e342015-07-02 17:26:42 +00003838 if (*String == L' ') {
3839 return (EFI_INVALID_PARAMETER);
jcarsey252d9452011-03-25 20:49:53 +00003840 }
darylm503b0934ac2011-11-12 00:35:11 +00003841
jcarsey252d9452011-03-25 20:49:53 +00003842 while (ShellIsHexaDecimalDigitCharacter (*String)) {
3843 //
darylm503b0934ac2011-11-12 00:35:11 +00003844 // If the Hex Number represented by String overflows according
Jaben Carsey80f3e342015-07-02 17:26:42 +00003845 // to the range defined by UINT64, then return EFI_DEVICE_ERROR.
jcarsey252d9452011-03-25 20:49:53 +00003846 //
3847 if (!(Result <= (RShiftU64((((UINT64) ~0) - InternalShellHexCharToUintn (*String)), 4)))) {
3848// if (!(Result <= ((((UINT64) ~0) - InternalShellHexCharToUintn (*String)) >> 4))) {
3849 return (EFI_DEVICE_ERROR);
3850 }
3851
3852 Result = (LShiftU64(Result, 4));
3853 Result += InternalShellHexCharToUintn (*String);
3854 String++;
3855
3856 //
jcarsey49bd4982012-01-27 18:42:43 +00003857 // stop at spaces if requested
jcarsey252d9452011-03-25 20:49:53 +00003858 //
jcarsey49bd4982012-01-27 18:42:43 +00003859 if (StopAtSpace && *String == L' ') {
3860 break;
jcarsey252d9452011-03-25 20:49:53 +00003861 }
3862 }
3863
3864 *Value = Result;
3865 return (EFI_SUCCESS);
3866}
3867
3868/**
3869 Convert a Null-terminated Unicode decimal string to a value of
3870 type UINT64.
3871
3872 This function returns a value of type UINT64 by interpreting the contents
3873 of the Unicode string specified by String as a decimal number. The format
3874 of the input Unicode string String is:
3875
3876 [spaces] [decimal digits].
3877
3878 The valid decimal digit character is in the range [0-9]. The
3879 function will ignore the pad space, which includes spaces or
3880 tab characters, before [decimal digits]. The running zero in the
3881 beginning of [decimal digits] will be ignored. Then, the function
3882 stops at the first character that is a not a valid decimal character
3883 or a Null-terminator, whichever one comes first.
3884
3885 If String has only pad spaces, then 0 is returned.
3886 If String has no pad spaces or valid decimal digits,
3887 then 0 is returned.
3888
3889 @param[in] String A pointer to a Null-terminated Unicode string.
3890 @param[out] Value Upon a successful return the value of the conversion.
3891 @param[in] StopAtSpace FALSE to skip spaces.
3892
3893 @retval EFI_SUCCESS The conversion was successful.
3894 @retval EFI_INVALID_PARAMETER A parameter was NULL or invalid.
3895 @retval EFI_DEVICE_ERROR An overflow occured.
3896**/
3897EFI_STATUS
3898EFIAPI
3899InternalShellStrDecimalToUint64 (
3900 IN CONST CHAR16 *String,
3901 OUT UINT64 *Value,
3902 IN CONST BOOLEAN StopAtSpace
3903 )
3904{
3905 UINT64 Result;
3906
3907 if (String == NULL || StrSize (String) == 0 || Value == NULL) {
3908 return (EFI_INVALID_PARAMETER);
3909 }
3910
3911 //
3912 // Ignore the pad spaces (space or tab)
3913 //
3914 while ((*String == L' ') || (*String == L'\t')) {
3915 String++;
3916 }
3917
3918 //
3919 // Ignore leading Zeros after the spaces
3920 //
3921 while (*String == L'0') {
3922 String++;
3923 }
3924
3925 Result = 0;
3926
3927 //
Jaben Carsey80f3e342015-07-02 17:26:42 +00003928 // Stop upon space if requested
3929 // (if the whole value was 0)
jcarsey252d9452011-03-25 20:49:53 +00003930 //
Jaben Carsey80f3e342015-07-02 17:26:42 +00003931 if (StopAtSpace && *String == L' ') {
3932 *Value = Result;
3933 return (EFI_SUCCESS);
jcarsey252d9452011-03-25 20:49:53 +00003934 }
Jaben Carsey80f3e342015-07-02 17:26:42 +00003935
jcarsey252d9452011-03-25 20:49:53 +00003936 while (ShellIsDecimalDigitCharacter (*String)) {
3937 //
darylm503b0934ac2011-11-12 00:35:11 +00003938 // If the number represented by String overflows according
Jaben Carsey80f3e342015-07-02 17:26:42 +00003939 // to the range defined by UINT64, then return EFI_DEVICE_ERROR.
jcarsey252d9452011-03-25 20:49:53 +00003940 //
darylm503b0934ac2011-11-12 00:35:11 +00003941
jcarsey252d9452011-03-25 20:49:53 +00003942 if (!(Result <= (DivU64x32((((UINT64) ~0) - (*String - L'0')),10)))) {
3943 return (EFI_DEVICE_ERROR);
3944 }
3945
3946 Result = MultU64x32(Result, 10) + (*String - L'0');
3947 String++;
3948
3949 //
3950 // Stop at spaces if requested
3951 //
3952 if (StopAtSpace && *String == L' ') {
3953 break;
3954 }
3955 }
3956
3957 *Value = Result;
darylm503b0934ac2011-11-12 00:35:11 +00003958
jcarsey252d9452011-03-25 20:49:53 +00003959 return (EFI_SUCCESS);
3960}
3961
3962/**
3963 Function to verify and convert a string to its numerical value.
3964
3965 If Hex it must be preceeded with a 0x, 0X, or has ForceHex set TRUE.
3966
3967 @param[in] String The string to evaluate.
3968 @param[out] Value Upon a successful return the value of the conversion.
3969 @param[in] ForceHex TRUE - always assume hex.
3970 @param[in] StopAtSpace FALSE to skip spaces.
darylm503b0934ac2011-11-12 00:35:11 +00003971
jcarsey252d9452011-03-25 20:49:53 +00003972 @retval EFI_SUCCESS The conversion was successful.
3973 @retval EFI_INVALID_PARAMETER String contained an invalid character.
3974 @retval EFI_NOT_FOUND String was a number, but Value was NULL.
3975**/
3976EFI_STATUS
3977EFIAPI
3978ShellConvertStringToUint64(
3979 IN CONST CHAR16 *String,
3980 OUT UINT64 *Value,
3981 IN CONST BOOLEAN ForceHex,
3982 IN CONST BOOLEAN StopAtSpace
3983 )
3984{
3985 UINT64 RetVal;
3986 CONST CHAR16 *Walker;
3987 EFI_STATUS Status;
3988 BOOLEAN Hex;
3989
3990 Hex = ForceHex;
3991
jcarsey658bf432014-11-04 22:33:16 +00003992 if (!InternalShellIsHexOrDecimalNumber(String, Hex, StopAtSpace, FALSE)) {
jcarsey252d9452011-03-25 20:49:53 +00003993 if (!Hex) {
3994 Hex = TRUE;
jcarsey658bf432014-11-04 22:33:16 +00003995 if (!InternalShellIsHexOrDecimalNumber(String, Hex, StopAtSpace, FALSE)) {
jcarsey252d9452011-03-25 20:49:53 +00003996 return (EFI_INVALID_PARAMETER);
3997 }
3998 } else {
3999 return (EFI_INVALID_PARAMETER);
4000 }
4001 }
4002
4003 //
4004 // Chop off leading spaces
4005 //
4006 for (Walker = String; Walker != NULL && *Walker != CHAR_NULL && *Walker == L' '; Walker++);
4007
4008 //
4009 // make sure we have something left that is numeric.
4010 //
jcarsey658bf432014-11-04 22:33:16 +00004011 if (Walker == NULL || *Walker == CHAR_NULL || !InternalShellIsHexOrDecimalNumber(Walker, Hex, StopAtSpace, FALSE)) {
jcarsey252d9452011-03-25 20:49:53 +00004012 return (EFI_INVALID_PARAMETER);
darylm503b0934ac2011-11-12 00:35:11 +00004013 }
jcarsey252d9452011-03-25 20:49:53 +00004014
4015 //
4016 // do the conversion.
4017 //
4018 if (Hex || StrnCmp(Walker, L"0x", 2) == 0 || StrnCmp(Walker, L"0X", 2) == 0){
4019 Status = InternalShellStrHexToUint64(Walker, &RetVal, StopAtSpace);
4020 } else {
4021 Status = InternalShellStrDecimalToUint64(Walker, &RetVal, StopAtSpace);
4022 }
4023
4024 if (Value == NULL && !EFI_ERROR(Status)) {
4025 return (EFI_NOT_FOUND);
4026 }
4027
4028 if (Value != NULL) {
4029 *Value = RetVal;
4030 }
4031
4032 return (Status);
4033}
4034
4035/**
4036 Function to determin if an entire string is a valid number.
4037
4038 If Hex it must be preceeded with a 0x or has ForceHex, set TRUE.
4039
4040 @param[in] String The string to evaluate.
4041 @param[in] ForceHex TRUE - always assume hex.
4042 @param[in] StopAtSpace TRUE to halt upon finding a space, FALSE to keep going.
4043
4044 @retval TRUE It is all numeric (dec/hex) characters.
4045 @retval FALSE There is a non-numeric character.
4046**/
4047BOOLEAN
4048EFIAPI
4049ShellIsHexOrDecimalNumber (
4050 IN CONST CHAR16 *String,
4051 IN CONST BOOLEAN ForceHex,
4052 IN CONST BOOLEAN StopAtSpace
4053 )
4054{
4055 if (ShellConvertStringToUint64(String, NULL, ForceHex, StopAtSpace) == EFI_NOT_FOUND) {
4056 return (TRUE);
4057 }
4058 return (FALSE);
4059}
jcarsey4d0a4fc2011-07-06 22:28:36 +00004060
4061/**
4062 Function to read a single line from a SHELL_FILE_HANDLE. The \n is not included in the returned
4063 buffer. The returned buffer must be callee freed.
4064
4065 If the position upon start is 0, then the Ascii Boolean will be set. This should be
4066 maintained and not changed for all operations with the same file.
4067
ydong104ff7e372011-09-02 08:05:34 +00004068 @param[in] Handle SHELL_FILE_HANDLE to read from.
4069 @param[in, out] Ascii Boolean value for indicating whether the file is
4070 Ascii (TRUE) or UCS2 (FALSE).
jcarsey4d0a4fc2011-07-06 22:28:36 +00004071
jcarseybeab0fc2011-10-10 17:26:25 +00004072 @return The line of text from the file.
4073 @retval NULL There was not enough memory available.
jcarsey4d0a4fc2011-07-06 22:28:36 +00004074
4075 @sa ShellFileHandleReadLine
4076**/
4077CHAR16*
4078EFIAPI
4079ShellFileHandleReturnLine(
4080 IN SHELL_FILE_HANDLE Handle,
4081 IN OUT BOOLEAN *Ascii
4082 )
4083{
4084 CHAR16 *RetVal;
4085 UINTN Size;
4086 EFI_STATUS Status;
4087
4088 Size = 0;
4089 RetVal = NULL;
4090
4091 Status = ShellFileHandleReadLine(Handle, RetVal, &Size, FALSE, Ascii);
4092 if (Status == EFI_BUFFER_TOO_SMALL) {
4093 RetVal = AllocateZeroPool(Size);
jcarseybeab0fc2011-10-10 17:26:25 +00004094 if (RetVal == NULL) {
4095 return (NULL);
4096 }
jcarsey4d0a4fc2011-07-06 22:28:36 +00004097 Status = ShellFileHandleReadLine(Handle, RetVal, &Size, FALSE, Ascii);
darylm503b0934ac2011-11-12 00:35:11 +00004098
jcarsey4d0a4fc2011-07-06 22:28:36 +00004099 }
Qiu Shumine3b76f32016-02-22 10:21:38 +08004100 if (Status == EFI_END_OF_FILE && RetVal != NULL && *RetVal != CHAR_NULL) {
Qiu Shuminf79d7b62016-02-18 13:12:58 +08004101 Status = EFI_SUCCESS;
4102 }
jcarsey4d0a4fc2011-07-06 22:28:36 +00004103 if (EFI_ERROR(Status) && (RetVal != NULL)) {
4104 FreePool(RetVal);
4105 RetVal = NULL;
4106 }
4107 return (RetVal);
4108}
4109
4110/**
4111 Function to read a single line (up to but not including the \n) from a SHELL_FILE_HANDLE.
4112
4113 If the position upon start is 0, then the Ascii Boolean will be set. This should be
4114 maintained and not changed for all operations with the same file.
4115
Jim Dailey2dda8a12016-02-10 08:53:00 -08004116 NOTE: LINES THAT ARE RETURNED BY THIS FUNCTION ARE UCS2, EVEN IF THE FILE BEING READ
4117 IS IN ASCII FORMAT.
4118
ydong104ff7e372011-09-02 08:05:34 +00004119 @param[in] Handle SHELL_FILE_HANDLE to read from.
Jim Dailey2dda8a12016-02-10 08:53:00 -08004120 @param[in, out] Buffer The pointer to buffer to read into. If this function
4121 returns EFI_SUCCESS, then on output Buffer will
4122 contain a UCS2 string, even if the file being
4123 read is ASCII.
4124 @param[in, out] Size On input, pointer to number of bytes in Buffer.
4125 On output, unchanged unless Buffer is too small
4126 to contain the next line of the file. In that
4127 case Size is set to the number of bytes needed
4128 to hold the next line of the file (as a UCS2
4129 string, even if it is an ASCII file).
ydong104ff7e372011-09-02 08:05:34 +00004130 @param[in] Truncate If the buffer is large enough, this has no effect.
4131 If the buffer is is too small and Truncate is TRUE,
4132 the line will be truncated.
4133 If the buffer is is too small and Truncate is FALSE,
4134 then no read will occur.
jcarsey4d0a4fc2011-07-06 22:28:36 +00004135
ydong104ff7e372011-09-02 08:05:34 +00004136 @param[in, out] Ascii Boolean value for indicating whether the file is
4137 Ascii (TRUE) or UCS2 (FALSE).
jcarsey4d0a4fc2011-07-06 22:28:36 +00004138
4139 @retval EFI_SUCCESS The operation was successful. The line is stored in
4140 Buffer.
jaben carsey9ed21942016-02-08 15:59:04 -08004141 @retval EFI_END_OF_FILE There are no more lines in the file.
jcarsey4d0a4fc2011-07-06 22:28:36 +00004142 @retval EFI_INVALID_PARAMETER Handle was NULL.
4143 @retval EFI_INVALID_PARAMETER Size was NULL.
4144 @retval EFI_BUFFER_TOO_SMALL Size was not large enough to store the line.
4145 Size was updated to the minimum space required.
4146**/
4147EFI_STATUS
4148EFIAPI
4149ShellFileHandleReadLine(
4150 IN SHELL_FILE_HANDLE Handle,
4151 IN OUT CHAR16 *Buffer,
4152 IN OUT UINTN *Size,
4153 IN BOOLEAN Truncate,
4154 IN OUT BOOLEAN *Ascii
4155 )
4156{
4157 EFI_STATUS Status;
4158 CHAR16 CharBuffer;
4159 UINTN CharSize;
4160 UINTN CountSoFar;
4161 UINT64 OriginalFilePosition;
4162
4163
4164 if (Handle == NULL
4165 ||Size == NULL
4166 ){
4167 return (EFI_INVALID_PARAMETER);
4168 }
4169 if (Buffer == NULL) {
4170 ASSERT(*Size == 0);
4171 } else {
4172 *Buffer = CHAR_NULL;
4173 }
4174 gEfiShellProtocol->GetFilePosition(Handle, &OriginalFilePosition);
4175 if (OriginalFilePosition == 0) {
4176 CharSize = sizeof(CHAR16);
4177 Status = gEfiShellProtocol->ReadFile(Handle, &CharSize, &CharBuffer);
4178 ASSERT_EFI_ERROR(Status);
4179 if (CharBuffer == gUnicodeFileTag) {
4180 *Ascii = FALSE;
4181 } else {
4182 *Ascii = TRUE;
4183 gEfiShellProtocol->SetFilePosition(Handle, OriginalFilePosition);
4184 }
4185 }
4186
jaben carsey9ed21942016-02-08 15:59:04 -08004187 if (*Ascii) {
4188 CharSize = sizeof(CHAR8);
4189 } else {
4190 CharSize = sizeof(CHAR16);
4191 }
jcarsey4d0a4fc2011-07-06 22:28:36 +00004192 for (CountSoFar = 0;;CountSoFar++){
4193 CharBuffer = 0;
jcarsey4d0a4fc2011-07-06 22:28:36 +00004194 Status = gEfiShellProtocol->ReadFile(Handle, &CharSize, &CharBuffer);
4195 if ( EFI_ERROR(Status)
4196 || CharSize == 0
4197 || (CharBuffer == L'\n' && !(*Ascii))
4198 || (CharBuffer == '\n' && *Ascii)
4199 ){
jaben carsey9ed21942016-02-08 15:59:04 -08004200 if (CharSize == 0) {
4201 Status = EFI_END_OF_FILE;
4202 }
jcarsey4d0a4fc2011-07-06 22:28:36 +00004203 break;
4204 }
4205 //
4206 // if we have space save it...
4207 //
Jim Dailey1095b432016-02-10 13:17:56 -08004208 if ((CountSoFar+1)*sizeof(CHAR16) < *Size){
jcarsey4d0a4fc2011-07-06 22:28:36 +00004209 ASSERT(Buffer != NULL);
Jim Dailey1095b432016-02-10 13:17:56 -08004210 ((CHAR16*)Buffer)[CountSoFar] = CharBuffer;
4211 ((CHAR16*)Buffer)[CountSoFar+1] = CHAR_NULL;
jcarsey4d0a4fc2011-07-06 22:28:36 +00004212 }
4213 }
4214
4215 //
4216 // if we ran out of space tell when...
4217 //
Jim Dailey1095b432016-02-10 13:17:56 -08004218 if ((CountSoFar+1)*sizeof(CHAR16) > *Size){
4219 *Size = (CountSoFar+1)*sizeof(CHAR16);
4220 if (!Truncate) {
4221 gEfiShellProtocol->SetFilePosition(Handle, OriginalFilePosition);
4222 } else {
4223 DEBUG((DEBUG_WARN, "The line was truncated in ShellFileHandleReadLine"));
jcarsey4d0a4fc2011-07-06 22:28:36 +00004224 }
Jim Dailey1095b432016-02-10 13:17:56 -08004225 return (EFI_BUFFER_TOO_SMALL);
4226 }
4227 while(Buffer[StrLen(Buffer)-1] == L'\r') {
4228 Buffer[StrLen(Buffer)-1] = CHAR_NULL;
jcarsey4d0a4fc2011-07-06 22:28:36 +00004229 }
4230
4231 return (Status);
4232}
jcarseyfb5278e2013-02-20 18:21:14 +00004233
4234/**
jcarsey365aa982013-03-04 21:54:02 +00004235 Function to print help file / man page content in the spec from the UEFI Shell protocol GetHelpText function.
4236
4237 @param[in] CommandToGetHelpOn Pointer to a string containing the command name of help file to be printed.
4238 @param[in] SectionToGetHelpOn Pointer to the section specifier(s).
4239 @param[in] PrintCommandText If TRUE, prints the command followed by the help content, otherwise prints
4240 the help content only.
4241 @retval EFI_DEVICE_ERROR The help data format was incorrect.
4242 @retval EFI_NOT_FOUND The help data could not be found.
4243 @retval EFI_SUCCESS The operation was successful.
4244**/
4245EFI_STATUS
4246EFIAPI
4247ShellPrintHelp (
4248 IN CONST CHAR16 *CommandToGetHelpOn,
4249 IN CONST CHAR16 *SectionToGetHelpOn,
4250 IN BOOLEAN PrintCommandText
4251 )
4252{
4253 EFI_STATUS Status;
4254 CHAR16 *OutText;
4255
4256 OutText = NULL;
4257
4258 //
4259 // Get the string to print based
4260 //
4261 Status = gEfiShellProtocol->GetHelpText (CommandToGetHelpOn, SectionToGetHelpOn, &OutText);
4262
4263 //
4264 // make sure we got a valid string
4265 //
4266 if (EFI_ERROR(Status)){
4267 return Status;
4268 }
4269 if (OutText == NULL || StrLen(OutText) == 0) {
4270 return EFI_NOT_FOUND;
4271 }
4272
4273 //
4274 // Chop off trailing stuff we dont need
4275 //
4276 while (OutText[StrLen(OutText)-1] == L'\r' || OutText[StrLen(OutText)-1] == L'\n' || OutText[StrLen(OutText)-1] == L' ') {
4277 OutText[StrLen(OutText)-1] = CHAR_NULL;
4278 }
4279
4280 //
4281 // Print this out to the console
4282 //
4283 if (PrintCommandText) {
4284 ShellPrintEx(-1, -1, L"%H%-14s%N- %s\r\n", CommandToGetHelpOn, OutText);
4285 } else {
4286 ShellPrintEx(-1, -1, L"%N%s\r\n", OutText);
4287 }
4288
4289 SHELL_FREE_NON_NULL(OutText);
4290
4291 return EFI_SUCCESS;
4292}
4293
4294/**
jcarseyfb5278e2013-02-20 18:21:14 +00004295 Function to delete a file by name
4296
4297 @param[in] FileName Pointer to file name to delete.
4298
4299 @retval EFI_SUCCESS the file was deleted sucessfully
4300 @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not
4301 deleted
4302 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
4303 @retval EFI_NOT_FOUND The specified file could not be found on the
4304 device or the file system could not be found
4305 on the device.
4306 @retval EFI_NO_MEDIA The device has no medium.
4307 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
4308 medium is no longer supported.
4309 @retval EFI_DEVICE_ERROR The device reported an error.
4310 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
4311 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
4312 @retval EFI_ACCESS_DENIED The file was opened read only.
4313 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
4314 file.
4315 @retval other The file failed to open
4316**/
4317EFI_STATUS
4318EFIAPI
4319ShellDeleteFileByName(
4320 IN CONST CHAR16 *FileName
4321 )
4322{
4323 EFI_STATUS Status;
4324 SHELL_FILE_HANDLE FileHandle;
4325
4326 Status = ShellFileExists(FileName);
4327
4328 if (Status == EFI_SUCCESS){
4329 Status = ShellOpenFileByName(FileName, &FileHandle, EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE, 0x0);
4330 if (Status == EFI_SUCCESS){
4331 Status = ShellDeleteFile(&FileHandle);
4332 }
4333 }
4334
4335 return(Status);
4336
4337}
Qiu Shumin0960ba12014-09-17 07:58:31 +00004338
4339/**
4340 Cleans off all the quotes in the string.
4341
4342 @param[in] OriginalString pointer to the string to be cleaned.
4343 @param[out] CleanString The new string with all quotes removed.
4344 Memory allocated in the function and free
4345 by caller.
4346
4347 @retval EFI_SUCCESS The operation was successful.
4348**/
4349EFI_STATUS
4350EFIAPI
4351InternalShellStripQuotes (
4352 IN CONST CHAR16 *OriginalString,
4353 OUT CHAR16 **CleanString
4354 )
4355{
4356 CHAR16 *Walker;
4357
4358 if (OriginalString == NULL || CleanString == NULL) {
4359 return EFI_INVALID_PARAMETER;
4360 }
4361
4362 *CleanString = AllocateCopyPool (StrSize (OriginalString), OriginalString);
4363 if (*CleanString == NULL) {
4364 return EFI_OUT_OF_RESOURCES;
4365 }
4366
4367 for (Walker = *CleanString; Walker != NULL && *Walker != CHAR_NULL ; Walker++) {
4368 if (*Walker == L'\"') {
4369 CopyMem(Walker, Walker+1, StrSize(Walker) - sizeof(Walker[0]));
4370 }
4371 }
4372
4373 return EFI_SUCCESS;
4374}
4375