blob: 8db18b3b210fa53ac842a9f09cdbb79912962565 [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);
Vladimir Olovyannikovf9c3b1b2016-10-06 15:02:26 -0700726 if (EFI_ERROR(Status)) {
727 return Status;
728 }
Tapan Shah583448b2016-09-22 12:12:47 -0700729
Tapan Shah2cf9ecd2016-10-05 13:58:05 -0700730 if (mUnicodeCollationProtocol == NULL) {
731 Status = gBS->LocateProtocol (&gEfiUnicodeCollation2ProtocolGuid, NULL, (VOID**)&mUnicodeCollationProtocol);
732 if (EFI_ERROR (Status)) {
733 gEfiShellProtocol->CloseFile (*FileHandle);
734 return Status;
735 }
736 }
737
Tapan Shah583448b2016-09-22 12:12:47 -0700738 if ((mUnicodeCollationProtocol->StriColl (mUnicodeCollationProtocol, (CHAR16*)FileName, L"NUL") != 0) &&
739 (mUnicodeCollationProtocol->StriColl (mUnicodeCollationProtocol, (CHAR16*)FileName, L"NULL") != 0) &&
740 !EFI_ERROR(Status) && ((OpenMode & EFI_FILE_MODE_CREATE) != 0)){
jcarsey2247dde2009-11-09 18:08:58 +0000741 FileInfo = FileFunctionMap.GetFileInfo(*FileHandle);
jcarseyb1f95a02009-06-16 00:23:19 +0000742 ASSERT(FileInfo != NULL);
743 FileInfo->Attribute = Attributes;
Yao Jiewendd17e3f2015-12-25 01:24:16 +0000744 Status2 = FileFunctionMap.SetFileInfo(*FileHandle, FileInfo);
jcarsey2247dde2009-11-09 18:08:58 +0000745 FreePool(FileInfo);
Yao Jiewendd17e3f2015-12-25 01:24:16 +0000746 if (EFI_ERROR (Status2)) {
747 gEfiShellProtocol->CloseFile(*FileHandle);
748 }
749 Status = Status2;
jcarseyb1f95a02009-06-16 00:23:19 +0000750 }
751 return (Status);
jcarsey1e6e84c2010-01-25 20:05:08 +0000752 }
jcarsey94b17fa2009-05-07 18:46:18 +0000753 //
754 // Using EFI Shell version
755 // this means convert name to path and call that function
756 // since this will use EFI method again that will open it.
757 //
758 ASSERT(mEfiShellEnvironment2 != NULL);
jcarseyb82bfcc2009-06-29 16:28:23 +0000759 FilePath = mEfiShellEnvironment2->NameToPath ((CHAR16*)FileName);
xdu290bfa222010-07-19 05:21:27 +0000760 if (FilePath != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +0000761 return (ShellOpenFileByDevicePath(&FilePath,
762 &DeviceHandle,
763 FileHandle,
764 OpenMode,
jcarseya405b862010-09-14 05:18:09 +0000765 Attributes));
jcarsey94b17fa2009-05-07 18:46:18 +0000766 }
767 return (EFI_DEVICE_ERROR);
768}
769/**
770 This function create a directory
771
jcarsey1e6e84c2010-01-25 20:05:08 +0000772 If return is EFI_SUCCESS, the Filehandle is the opened directory's handle;
773 otherwise, the Filehandle is NULL. If the directory already existed, this
jcarsey94b17fa2009-05-07 18:46:18 +0000774 function opens the existing directory.
775
darylm503b0934ac2011-11-12 00:35:11 +0000776 @param DirectoryName pointer to directory name
777 @param FileHandle pointer to the file handle.
jcarsey94b17fa2009-05-07 18:46:18 +0000778
darylm503b0934ac2011-11-12 00:35:11 +0000779 @retval EFI_SUCCESS The information was set.
780 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
781 @retval EFI_UNSUPPORTED Could not open the file path.
782 @retval EFI_NOT_FOUND The specified file could not be found on the
jcarsey1e6e84c2010-01-25 20:05:08 +0000783 device or the file system could not be found
jcarsey94b17fa2009-05-07 18:46:18 +0000784 on the device.
darylm503b0934ac2011-11-12 00:35:11 +0000785 @retval EFI_NO_MEDIA The device has no medium.
786 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
jcarsey94b17fa2009-05-07 18:46:18 +0000787 medium is no longer supported.
darylm503b0934ac2011-11-12 00:35:11 +0000788 @retval EFI_DEVICE_ERROR The device reported an error.
789 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
790 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
791 @retval EFI_ACCESS_DENIED The file was opened read only.
792 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
jcarsey94b17fa2009-05-07 18:46:18 +0000793 file.
darylm503b0934ac2011-11-12 00:35:11 +0000794 @retval EFI_VOLUME_FULL The volume is full.
jcarsey94b17fa2009-05-07 18:46:18 +0000795 @sa ShellOpenFileByName
796**/
797EFI_STATUS
798EFIAPI
799ShellCreateDirectory(
jcarseyb82bfcc2009-06-29 16:28:23 +0000800 IN CONST CHAR16 *DirectoryName,
jcarseya405b862010-09-14 05:18:09 +0000801 OUT SHELL_FILE_HANDLE *FileHandle
802 )
803{
jcarsey366f81a2011-06-27 21:04:22 +0000804 if (gEfiShellProtocol != NULL) {
jcarsey2247dde2009-11-09 18:08:58 +0000805 //
806 // Use UEFI Shell 2.0 method
807 //
jcarsey366f81a2011-06-27 21:04:22 +0000808 return (gEfiShellProtocol->CreateFile(DirectoryName,
jcarsey2247dde2009-11-09 18:08:58 +0000809 EFI_FILE_DIRECTORY,
810 FileHandle
jcarseya405b862010-09-14 05:18:09 +0000811 ));
jcarsey2247dde2009-11-09 18:08:58 +0000812 } else {
813 return (ShellOpenFileByName(DirectoryName,
814 FileHandle,
815 EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE,
816 EFI_FILE_DIRECTORY
jcarseya405b862010-09-14 05:18:09 +0000817 ));
jcarsey2247dde2009-11-09 18:08:58 +0000818 }
jcarsey94b17fa2009-05-07 18:46:18 +0000819}
820
821/**
822 This function reads information from an opened file.
823
jcarsey1e6e84c2010-01-25 20:05:08 +0000824 If FileHandle is not a directory, the function reads the requested number of
825 bytes from the file at the file's current position and returns them in Buffer.
jcarsey94b17fa2009-05-07 18:46:18 +0000826 If the read goes beyond the end of the file, the read length is truncated to the
jcarsey1e6e84c2010-01-25 20:05:08 +0000827 end of the file. The file's current position is increased by the number of bytes
828 returned. If FileHandle is a directory, the function reads the directory entry
829 at the file's current position and returns the entry in Buffer. If the Buffer
830 is not large enough to hold the current directory entry, then
831 EFI_BUFFER_TOO_SMALL is returned and the current file position is not updated.
832 BufferSize is set to be the size of the buffer needed to read the entry. On
833 success, the current position is updated to the next directory entry. If there
834 are no more directory entries, the read returns a zero-length buffer.
jcarsey94b17fa2009-05-07 18:46:18 +0000835 EFI_FILE_INFO is the structure returned as the directory entry.
836
837 @param FileHandle the opened file handle
jcarsey1e6e84c2010-01-25 20:05:08 +0000838 @param BufferSize on input the size of buffer in bytes. on return
jcarsey94b17fa2009-05-07 18:46:18 +0000839 the number of bytes written.
840 @param Buffer the buffer to put read data into.
841
darylm503b0934ac2011-11-12 00:35:11 +0000842 @retval EFI_SUCCESS Data was read.
843 @retval EFI_NO_MEDIA The device has no media.
844 @retval EFI_DEVICE_ERROR The device reported an error.
845 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
846 @retval EFI_BUFFER_TO_SMALL Buffer is too small. ReadSize contains required
jcarsey94b17fa2009-05-07 18:46:18 +0000847 size.
848
849**/
850EFI_STATUS
851EFIAPI
852ShellReadFile(
jcarseya405b862010-09-14 05:18:09 +0000853 IN SHELL_FILE_HANDLE FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000854 IN OUT UINTN *BufferSize,
855 OUT VOID *Buffer
jcarseya405b862010-09-14 05:18:09 +0000856 )
857{
jcarseyd2b45642009-05-11 18:02:16 +0000858 return (FileFunctionMap.ReadFile(FileHandle, BufferSize, Buffer));
jcarsey94b17fa2009-05-07 18:46:18 +0000859}
860
861
862/**
863 Write data to a file.
864
jcarsey1e6e84c2010-01-25 20:05:08 +0000865 This function writes the specified number of bytes to the file at the current
866 file position. The current file position is advanced the actual number of bytes
867 written, which is returned in BufferSize. Partial writes only occur when there
868 has been a data error during the write attempt (such as "volume space full").
869 The file is automatically grown to hold the data if required. Direct writes to
jcarsey94b17fa2009-05-07 18:46:18 +0000870 opened directories are not supported.
871
872 @param FileHandle The opened file for writing
873 @param BufferSize on input the number of bytes in Buffer. On output
874 the number of bytes written.
875 @param Buffer the buffer containing data to write is stored.
876
darylm503b0934ac2011-11-12 00:35:11 +0000877 @retval EFI_SUCCESS Data was written.
878 @retval EFI_UNSUPPORTED Writes to an open directory are not supported.
879 @retval EFI_NO_MEDIA The device has no media.
880 @retval EFI_DEVICE_ERROR The device reported an error.
881 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
882 @retval EFI_WRITE_PROTECTED The device is write-protected.
883 @retval EFI_ACCESS_DENIED The file was open for read only.
884 @retval EFI_VOLUME_FULL The volume is full.
jcarsey94b17fa2009-05-07 18:46:18 +0000885**/
886EFI_STATUS
887EFIAPI
888ShellWriteFile(
jcarsey252d9452011-03-25 20:49:53 +0000889 IN SHELL_FILE_HANDLE FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000890 IN OUT UINTN *BufferSize,
891 IN VOID *Buffer
jcarseya405b862010-09-14 05:18:09 +0000892 )
893{
jcarseyd2b45642009-05-11 18:02:16 +0000894 return (FileFunctionMap.WriteFile(FileHandle, BufferSize, Buffer));
jcarsey94b17fa2009-05-07 18:46:18 +0000895}
896
jcarsey1e6e84c2010-01-25 20:05:08 +0000897/**
jcarsey94b17fa2009-05-07 18:46:18 +0000898 Close an open file handle.
899
jcarsey1e6e84c2010-01-25 20:05:08 +0000900 This function closes a specified file handle. All "dirty" cached file data is
901 flushed to the device, and the file is closed. In all cases the handle is
jcarsey94b17fa2009-05-07 18:46:18 +0000902 closed.
903
904@param FileHandle the file handle to close.
905
906@retval EFI_SUCCESS the file handle was closed sucessfully.
907**/
908EFI_STATUS
909EFIAPI
910ShellCloseFile (
jcarseya405b862010-09-14 05:18:09 +0000911 IN SHELL_FILE_HANDLE *FileHandle
912 )
913{
jcarseyd2b45642009-05-11 18:02:16 +0000914 return (FileFunctionMap.CloseFile(*FileHandle));
jcarsey94b17fa2009-05-07 18:46:18 +0000915}
916
917/**
918 Delete a file and close the handle
919
920 This function closes and deletes a file. In all cases the file handle is closed.
jcarsey1e6e84c2010-01-25 20:05:08 +0000921 If the file cannot be deleted, the warning code EFI_WARN_DELETE_FAILURE is
jcarsey94b17fa2009-05-07 18:46:18 +0000922 returned, but the handle is still closed.
923
924 @param FileHandle the file handle to delete
925
926 @retval EFI_SUCCESS the file was closed sucessfully
jcarsey1e6e84c2010-01-25 20:05:08 +0000927 @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not
jcarsey94b17fa2009-05-07 18:46:18 +0000928 deleted
darylm503b0934ac2011-11-12 00:35:11 +0000929 @retval INVALID_PARAMETER One of the parameters has an invalid value.
jcarsey94b17fa2009-05-07 18:46:18 +0000930**/
931EFI_STATUS
932EFIAPI
933ShellDeleteFile (
darylm503b0934ac2011-11-12 00:35:11 +0000934 IN SHELL_FILE_HANDLE *FileHandle
jcarseya405b862010-09-14 05:18:09 +0000935 )
936{
jcarseyd2b45642009-05-11 18:02:16 +0000937 return (FileFunctionMap.DeleteFile(*FileHandle));
jcarsey94b17fa2009-05-07 18:46:18 +0000938}
939
940/**
941 Set the current position in a file.
942
jcarsey1e6e84c2010-01-25 20:05:08 +0000943 This function sets the current file position for the handle to the position
jcarsey94b17fa2009-05-07 18:46:18 +0000944 supplied. With the exception of seeking to position 0xFFFFFFFFFFFFFFFF, only
jcarsey1e6e84c2010-01-25 20:05:08 +0000945 absolute positioning is supported, and seeking past the end of the file is
946 allowed (a subsequent write would grow the file). Seeking to position
jcarsey94b17fa2009-05-07 18:46:18 +0000947 0xFFFFFFFFFFFFFFFF causes the current position to be set to the end of the file.
jcarsey1e6e84c2010-01-25 20:05:08 +0000948 If FileHandle is a directory, the only position that may be set is zero. This
jcarsey94b17fa2009-05-07 18:46:18 +0000949 has the effect of starting the read process of the directory entries over.
950
951 @param FileHandle The file handle on which the position is being set
952 @param Position Byte position from begining of file
953
954 @retval EFI_SUCCESS Operation completed sucessfully.
jcarsey1e6e84c2010-01-25 20:05:08 +0000955 @retval EFI_UNSUPPORTED the seek request for non-zero is not valid on
jcarsey94b17fa2009-05-07 18:46:18 +0000956 directories.
957 @retval INVALID_PARAMETER One of the parameters has an invalid value.
958**/
959EFI_STATUS
960EFIAPI
961ShellSetFilePosition (
darylm503b0934ac2011-11-12 00:35:11 +0000962 IN SHELL_FILE_HANDLE FileHandle,
963 IN UINT64 Position
jcarseya405b862010-09-14 05:18:09 +0000964 )
965{
jcarseyd2b45642009-05-11 18:02:16 +0000966 return (FileFunctionMap.SetFilePosition(FileHandle, Position));
jcarsey94b17fa2009-05-07 18:46:18 +0000967}
968
jcarsey1e6e84c2010-01-25 20:05:08 +0000969/**
jcarsey94b17fa2009-05-07 18:46:18 +0000970 Gets a file's current position
971
jcarsey1e6e84c2010-01-25 20:05:08 +0000972 This function retrieves the current file position for the file handle. For
973 directories, the current file position has no meaning outside of the file
jcarsey94b17fa2009-05-07 18:46:18 +0000974 system driver and as such the operation is not supported. An error is returned
975 if FileHandle is a directory.
976
977 @param FileHandle The open file handle on which to get the position.
978 @param Position Byte position from begining of file.
979
980 @retval EFI_SUCCESS the operation completed sucessfully.
981 @retval INVALID_PARAMETER One of the parameters has an invalid value.
982 @retval EFI_UNSUPPORTED the request is not valid on directories.
983**/
984EFI_STATUS
985EFIAPI
986ShellGetFilePosition (
jcarseya405b862010-09-14 05:18:09 +0000987 IN SHELL_FILE_HANDLE FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000988 OUT UINT64 *Position
jcarseya405b862010-09-14 05:18:09 +0000989 )
990{
jcarseyd2b45642009-05-11 18:02:16 +0000991 return (FileFunctionMap.GetFilePosition(FileHandle, Position));
jcarsey94b17fa2009-05-07 18:46:18 +0000992}
993/**
994 Flushes data on a file
jcarsey1e6e84c2010-01-25 20:05:08 +0000995
jcarsey94b17fa2009-05-07 18:46:18 +0000996 This function flushes all modified data associated with a file to a device.
997
998 @param FileHandle The file handle on which to flush data
999
1000 @retval EFI_SUCCESS The data was flushed.
1001 @retval EFI_NO_MEDIA The device has no media.
1002 @retval EFI_DEVICE_ERROR The device reported an error.
1003 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
1004 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
1005 @retval EFI_ACCESS_DENIED The file was opened for read only.
1006**/
1007EFI_STATUS
1008EFIAPI
1009ShellFlushFile (
jcarseya405b862010-09-14 05:18:09 +00001010 IN SHELL_FILE_HANDLE FileHandle
1011 )
1012{
jcarseyd2b45642009-05-11 18:02:16 +00001013 return (FileFunctionMap.FlushFile(FileHandle));
jcarsey94b17fa2009-05-07 18:46:18 +00001014}
1015
darylm503b0934ac2011-11-12 00:35:11 +00001016/** Retrieve first entry from a directory.
jcarsey94b17fa2009-05-07 18:46:18 +00001017
darylm503b0934ac2011-11-12 00:35:11 +00001018 This function takes an open directory handle and gets information from the
1019 first entry in the directory. A buffer is allocated to contain
1020 the information and a pointer to the buffer is returned in *Buffer. The
1021 caller can use ShellFindNextFile() to get subsequent directory entries.
jcarsey94b17fa2009-05-07 18:46:18 +00001022
darylm503b0934ac2011-11-12 00:35:11 +00001023 The buffer will be freed by ShellFindNextFile() when the last directory
1024 entry is read. Otherwise, the caller must free the buffer, using FreePool,
1025 when finished with it.
1026
1027 @param[in] DirHandle The file handle of the directory to search.
1028 @param[out] Buffer The pointer to the buffer for the file's information.
jcarsey94b17fa2009-05-07 18:46:18 +00001029
1030 @retval EFI_SUCCESS Found the first file.
1031 @retval EFI_NOT_FOUND Cannot find the directory.
1032 @retval EFI_NO_MEDIA The device has no media.
1033 @retval EFI_DEVICE_ERROR The device reported an error.
1034 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
1035 @return Others status of ShellGetFileInfo, ShellSetFilePosition,
1036 or ShellReadFile
1037**/
1038EFI_STATUS
1039EFIAPI
1040ShellFindFirstFile (
jcarseya405b862010-09-14 05:18:09 +00001041 IN SHELL_FILE_HANDLE DirHandle,
jcarseyd2b45642009-05-11 18:02:16 +00001042 OUT EFI_FILE_INFO **Buffer
jcarseya405b862010-09-14 05:18:09 +00001043 )
1044{
jcarsey94b17fa2009-05-07 18:46:18 +00001045 //
jcarseyd2b45642009-05-11 18:02:16 +00001046 // pass to file handle lib
jcarsey94b17fa2009-05-07 18:46:18 +00001047 //
jcarseyd2b45642009-05-11 18:02:16 +00001048 return (FileHandleFindFirstFile(DirHandle, Buffer));
jcarsey94b17fa2009-05-07 18:46:18 +00001049}
darylm503b0934ac2011-11-12 00:35:11 +00001050/** Retrieve next entries from a directory.
jcarsey94b17fa2009-05-07 18:46:18 +00001051
darylm503b0934ac2011-11-12 00:35:11 +00001052 To use this function, the caller must first call the ShellFindFirstFile()
1053 function to get the first directory entry. Subsequent directory entries are
1054 retrieved by using the ShellFindNextFile() function. This function can
1055 be called several times to get each entry from the directory. If the call of
1056 ShellFindNextFile() retrieved the last directory entry, the next call of
1057 this function will set *NoFile to TRUE and free the buffer.
jcarsey94b17fa2009-05-07 18:46:18 +00001058
darylm503b0934ac2011-11-12 00:35:11 +00001059 @param[in] DirHandle The file handle of the directory.
1060 @param[out] Buffer The pointer to buffer for file's information.
1061 @param[out] NoFile The pointer to boolean when last file is found.
jcarsey94b17fa2009-05-07 18:46:18 +00001062
1063 @retval EFI_SUCCESS Found the next file, or reached last file
1064 @retval EFI_NO_MEDIA The device has no media.
1065 @retval EFI_DEVICE_ERROR The device reported an error.
1066 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
1067**/
1068EFI_STATUS
1069EFIAPI
1070ShellFindNextFile(
jcarseya405b862010-09-14 05:18:09 +00001071 IN SHELL_FILE_HANDLE DirHandle,
jcarsey94b17fa2009-05-07 18:46:18 +00001072 OUT EFI_FILE_INFO *Buffer,
1073 OUT BOOLEAN *NoFile
jcarseya405b862010-09-14 05:18:09 +00001074 )
1075{
jcarsey94b17fa2009-05-07 18:46:18 +00001076 //
jcarseyd2b45642009-05-11 18:02:16 +00001077 // pass to file handle lib
jcarsey94b17fa2009-05-07 18:46:18 +00001078 //
jcarseyd2b45642009-05-11 18:02:16 +00001079 return (FileHandleFindNextFile(DirHandle, Buffer, NoFile));
jcarsey94b17fa2009-05-07 18:46:18 +00001080}
1081/**
1082 Retrieve the size of a file.
1083
1084 if FileHandle is NULL then ASSERT()
1085 if Size is NULL then ASSERT()
1086
jcarsey1e6e84c2010-01-25 20:05:08 +00001087 This function extracts the file size info from the FileHandle's EFI_FILE_INFO
jcarsey94b17fa2009-05-07 18:46:18 +00001088 data.
1089
1090 @param FileHandle file handle from which size is retrieved
1091 @param Size pointer to size
1092
1093 @retval EFI_SUCCESS operation was completed sucessfully
1094 @retval EFI_DEVICE_ERROR cannot access the file
1095**/
1096EFI_STATUS
1097EFIAPI
1098ShellGetFileSize (
jcarseya405b862010-09-14 05:18:09 +00001099 IN SHELL_FILE_HANDLE FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +00001100 OUT UINT64 *Size
jcarseya405b862010-09-14 05:18:09 +00001101 )
1102{
jcarseyd2b45642009-05-11 18:02:16 +00001103 return (FileFunctionMap.GetFileSize(FileHandle, Size));
jcarsey94b17fa2009-05-07 18:46:18 +00001104}
1105/**
1106 Retrieves the status of the break execution flag
1107
1108 this function is useful to check whether the application is being asked to halt by the shell.
1109
1110 @retval TRUE the execution break is enabled
1111 @retval FALSE the execution break is not enabled
1112**/
1113BOOLEAN
1114EFIAPI
1115ShellGetExecutionBreakFlag(
1116 VOID
1117 )
1118{
jcarsey1e6e84c2010-01-25 20:05:08 +00001119 //
jcarsey94b17fa2009-05-07 18:46:18 +00001120 // Check for UEFI Shell 2.0 protocols
1121 //
jcarsey366f81a2011-06-27 21:04:22 +00001122 if (gEfiShellProtocol != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00001123
1124 //
1125 // We are using UEFI Shell 2.0; see if the event has been triggered
1126 //
jcarsey366f81a2011-06-27 21:04:22 +00001127 if (gBS->CheckEvent(gEfiShellProtocol->ExecutionBreak) != EFI_SUCCESS) {
jcarsey94b17fa2009-05-07 18:46:18 +00001128 return (FALSE);
1129 }
1130 return (TRUE);
jcarsey1e6e84c2010-01-25 20:05:08 +00001131 }
jcarsey94b17fa2009-05-07 18:46:18 +00001132
1133 //
1134 // using EFI Shell; call the function to check
1135 //
jcarsey92a54472011-06-27 20:33:13 +00001136 if (mEfiShellEnvironment2 != NULL) {
1137 return (mEfiShellEnvironment2->GetExecutionBreak());
1138 }
1139
1140 return (FALSE);
jcarsey94b17fa2009-05-07 18:46:18 +00001141}
1142/**
1143 return the value of an environment variable
1144
jcarsey1e6e84c2010-01-25 20:05:08 +00001145 this function gets the value of the environment variable set by the
jcarsey94b17fa2009-05-07 18:46:18 +00001146 ShellSetEnvironmentVariable function
1147
1148 @param EnvKey The key name of the environment variable.
1149
1150 @retval NULL the named environment variable does not exist.
1151 @return != NULL pointer to the value of the environment variable
1152**/
1153CONST CHAR16*
1154EFIAPI
1155ShellGetEnvironmentVariable (
jcarsey9b3bf082009-06-23 21:15:07 +00001156 IN CONST CHAR16 *EnvKey
jcarsey94b17fa2009-05-07 18:46:18 +00001157 )
1158{
jcarsey1e6e84c2010-01-25 20:05:08 +00001159 //
jcarsey94b17fa2009-05-07 18:46:18 +00001160 // Check for UEFI Shell 2.0 protocols
1161 //
jcarsey366f81a2011-06-27 21:04:22 +00001162 if (gEfiShellProtocol != NULL) {
1163 return (gEfiShellProtocol->GetEnv(EnvKey));
jcarsey94b17fa2009-05-07 18:46:18 +00001164 }
1165
1166 //
jcarsey92a54472011-06-27 20:33:13 +00001167 // Check for EFI shell
jcarsey94b17fa2009-05-07 18:46:18 +00001168 //
jcarsey92a54472011-06-27 20:33:13 +00001169 if (mEfiShellEnvironment2 != NULL) {
1170 return (mEfiShellEnvironment2->GetEnv((CHAR16*)EnvKey));
1171 }
jcarsey94b17fa2009-05-07 18:46:18 +00001172
jcarsey92a54472011-06-27 20:33:13 +00001173 return NULL;
jcarsey94b17fa2009-05-07 18:46:18 +00001174}
1175/**
1176 set the value of an environment variable
1177
1178This function changes the current value of the specified environment variable. If the
1179environment variable exists and the Value is an empty string, then the environment
1180variable is deleted. If the environment variable exists and the Value is not an empty
1181string, then the value of the environment variable is changed. If the environment
1182variable does not exist and the Value is an empty string, there is no action. If the
1183environment variable does not exist and the Value is a non-empty string, then the
1184environment variable is created and assigned the specified value.
1185
1186 This is not supported pre-UEFI Shell 2.0.
1187
1188 @param EnvKey The key name of the environment variable.
1189 @param EnvVal The Value of the environment variable
1190 @param Volatile Indicates whether the variable is non-volatile (FALSE) or volatile (TRUE).
1191
1192 @retval EFI_SUCCESS the operation was completed sucessfully
1193 @retval EFI_UNSUPPORTED This operation is not allowed in pre UEFI 2.0 Shell environments
1194**/
1195EFI_STATUS
1196EFIAPI
1197ShellSetEnvironmentVariable (
1198 IN CONST CHAR16 *EnvKey,
1199 IN CONST CHAR16 *EnvVal,
1200 IN BOOLEAN Volatile
1201 )
1202{
jcarsey1e6e84c2010-01-25 20:05:08 +00001203 //
jcarsey94b17fa2009-05-07 18:46:18 +00001204 // Check for UEFI Shell 2.0 protocols
1205 //
jcarsey366f81a2011-06-27 21:04:22 +00001206 if (gEfiShellProtocol != NULL) {
1207 return (gEfiShellProtocol->SetEnv(EnvKey, EnvVal, Volatile));
jcarsey1e6e84c2010-01-25 20:05:08 +00001208 }
jcarsey94b17fa2009-05-07 18:46:18 +00001209
1210 //
1211 // This feature does not exist under EFI shell
1212 //
1213 return (EFI_UNSUPPORTED);
1214}
jcarseya405b862010-09-14 05:18:09 +00001215
jcarsey94b17fa2009-05-07 18:46:18 +00001216/**
jcarseya405b862010-09-14 05:18:09 +00001217 Cause the shell to parse and execute a command line.
jcarsey94b17fa2009-05-07 18:46:18 +00001218
1219 This function creates a nested instance of the shell and executes the specified
jcarseya405b862010-09-14 05:18:09 +00001220 command (CommandLine) with the specified environment (Environment). Upon return,
1221 the status code returned by the specified command is placed in StatusCode.
1222 If Environment is NULL, then the current environment is used and all changes made
1223 by the commands executed will be reflected in the current environment. If the
1224 Environment is non-NULL, then the changes made will be discarded.
1225 The CommandLine is executed from the current working directory on the current
1226 device.
jcarsey94b17fa2009-05-07 18:46:18 +00001227
Brendan Jackman3877d0f2014-01-24 22:31:07 +00001228 The EnvironmentVariables pararemeter is ignored in a pre-UEFI Shell 2.0
jcarseya405b862010-09-14 05:18:09 +00001229 environment. The values pointed to by the parameters will be unchanged by the
1230 ShellExecute() function. The Output parameter has no effect in a
1231 UEFI Shell 2.0 environment.
jcarsey94b17fa2009-05-07 18:46:18 +00001232
jcarseya405b862010-09-14 05:18:09 +00001233 @param[in] ParentHandle The parent image starting the operation.
1234 @param[in] CommandLine The pointer to a NULL terminated command line.
1235 @param[in] Output True to display debug output. False to hide it.
1236 @param[in] EnvironmentVariables Optional pointer to array of environment variables
1237 in the form "x=y". If NULL, the current set is used.
1238 @param[out] Status The status of the run command line.
jcarsey94b17fa2009-05-07 18:46:18 +00001239
jcarseya405b862010-09-14 05:18:09 +00001240 @retval EFI_SUCCESS The operation completed sucessfully. Status
1241 contains the status code returned.
1242 @retval EFI_INVALID_PARAMETER A parameter contains an invalid value.
1243 @retval EFI_OUT_OF_RESOURCES Out of resources.
1244 @retval EFI_UNSUPPORTED The operation is not allowed.
jcarsey94b17fa2009-05-07 18:46:18 +00001245**/
1246EFI_STATUS
1247EFIAPI
1248ShellExecute (
1249 IN EFI_HANDLE *ParentHandle,
1250 IN CHAR16 *CommandLine OPTIONAL,
1251 IN BOOLEAN Output OPTIONAL,
1252 IN CHAR16 **EnvironmentVariables OPTIONAL,
1253 OUT EFI_STATUS *Status OPTIONAL
1254 )
1255{
Brendan Jackman3877d0f2014-01-24 22:31:07 +00001256 EFI_STATUS CmdStatus;
jcarsey1e6e84c2010-01-25 20:05:08 +00001257 //
jcarsey94b17fa2009-05-07 18:46:18 +00001258 // Check for UEFI Shell 2.0 protocols
1259 //
jcarsey366f81a2011-06-27 21:04:22 +00001260 if (gEfiShellProtocol != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00001261 //
1262 // Call UEFI Shell 2.0 version (not using Output parameter)
1263 //
jcarsey366f81a2011-06-27 21:04:22 +00001264 return (gEfiShellProtocol->Execute(ParentHandle,
jcarsey94b17fa2009-05-07 18:46:18 +00001265 CommandLine,
1266 EnvironmentVariables,
1267 Status));
jcarsey1e6e84c2010-01-25 20:05:08 +00001268 }
jcarsey92a54472011-06-27 20:33:13 +00001269
jcarsey94b17fa2009-05-07 18:46:18 +00001270 //
jcarsey92a54472011-06-27 20:33:13 +00001271 // Check for EFI shell
jcarsey94b17fa2009-05-07 18:46:18 +00001272 //
jcarsey92a54472011-06-27 20:33:13 +00001273 if (mEfiShellEnvironment2 != NULL) {
1274 //
Brendan Jackman3877d0f2014-01-24 22:31:07 +00001275 // Call EFI Shell version.
jcarsey92a54472011-06-27 20:33:13 +00001276 // Due to oddity in the EFI shell we want to dereference the ParentHandle here
1277 //
Brendan Jackman3877d0f2014-01-24 22:31:07 +00001278 CmdStatus = (mEfiShellEnvironment2->Execute(*ParentHandle,
jcarsey92a54472011-06-27 20:33:13 +00001279 CommandLine,
1280 Output));
Brendan Jackman3877d0f2014-01-24 22:31:07 +00001281 //
1282 // No Status output parameter so just use the returned status
1283 //
1284 if (Status != NULL) {
1285 *Status = CmdStatus;
1286 }
1287 //
1288 // If there was an error, we can't tell if it was from the command or from
1289 // the Execute() function, so we'll just assume the shell ran successfully
1290 // and the error came from the command.
1291 //
1292 return EFI_SUCCESS;
jcarsey92a54472011-06-27 20:33:13 +00001293 }
1294
1295 return (EFI_UNSUPPORTED);
jcarsey94b17fa2009-05-07 18:46:18 +00001296}
Brendan Jackman3877d0f2014-01-24 22:31:07 +00001297
jcarsey94b17fa2009-05-07 18:46:18 +00001298/**
1299 Retreives the current directory path
1300
jcarsey1e6e84c2010-01-25 20:05:08 +00001301 If the DeviceName is NULL, it returns the current device's current directory
1302 name. If the DeviceName is not NULL, it returns the current directory name
jcarsey94b17fa2009-05-07 18:46:18 +00001303 on specified drive.
1304
Qiu Shuminfbd2dfa2015-10-23 02:03:20 +00001305 Note that the current directory string should exclude the tailing backslash character.
1306
jcarsey94b17fa2009-05-07 18:46:18 +00001307 @param DeviceName the name of the drive to get directory on
1308
1309 @retval NULL the directory does not exist
1310 @return != NULL the directory
1311**/
1312CONST CHAR16*
1313EFIAPI
1314ShellGetCurrentDir (
jcarseya405b862010-09-14 05:18:09 +00001315 IN CHAR16 * CONST DeviceName OPTIONAL
jcarsey94b17fa2009-05-07 18:46:18 +00001316 )
1317{
jcarsey1e6e84c2010-01-25 20:05:08 +00001318 //
jcarsey94b17fa2009-05-07 18:46:18 +00001319 // Check for UEFI Shell 2.0 protocols
1320 //
jcarsey366f81a2011-06-27 21:04:22 +00001321 if (gEfiShellProtocol != NULL) {
1322 return (gEfiShellProtocol->GetCurDir(DeviceName));
jcarsey1e6e84c2010-01-25 20:05:08 +00001323 }
jcarsey92a54472011-06-27 20:33:13 +00001324
jcarsey94b17fa2009-05-07 18:46:18 +00001325 //
jcarsey8bd282b2011-06-27 16:45:41 +00001326 // Check for EFI shell
jcarsey94b17fa2009-05-07 18:46:18 +00001327 //
jcarsey8bd282b2011-06-27 16:45:41 +00001328 if (mEfiShellEnvironment2 != NULL) {
1329 return (mEfiShellEnvironment2->CurDir(DeviceName));
1330 }
1331
1332 return (NULL);
jcarsey94b17fa2009-05-07 18:46:18 +00001333}
1334/**
1335 sets (enabled or disabled) the page break mode
1336
jcarsey1e6e84c2010-01-25 20:05:08 +00001337 when page break mode is enabled the screen will stop scrolling
jcarsey94b17fa2009-05-07 18:46:18 +00001338 and wait for operator input before scrolling a subsequent screen.
1339
1340 @param CurrentState TRUE to enable and FALSE to disable
1341**/
jcarsey1e6e84c2010-01-25 20:05:08 +00001342VOID
jcarsey94b17fa2009-05-07 18:46:18 +00001343EFIAPI
1344ShellSetPageBreakMode (
1345 IN BOOLEAN CurrentState
1346 )
1347{
1348 //
1349 // check for enabling
1350 //
1351 if (CurrentState != 0x00) {
jcarsey1e6e84c2010-01-25 20:05:08 +00001352 //
jcarsey94b17fa2009-05-07 18:46:18 +00001353 // check for UEFI Shell 2.0
1354 //
jcarsey366f81a2011-06-27 21:04:22 +00001355 if (gEfiShellProtocol != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00001356 //
1357 // Enable with UEFI 2.0 Shell
1358 //
jcarsey366f81a2011-06-27 21:04:22 +00001359 gEfiShellProtocol->EnablePageBreak();
jcarsey94b17fa2009-05-07 18:46:18 +00001360 return;
1361 } else {
jcarsey1e6e84c2010-01-25 20:05:08 +00001362 //
jcarsey92a54472011-06-27 20:33:13 +00001363 // Check for EFI shell
jcarsey94b17fa2009-05-07 18:46:18 +00001364 //
jcarsey92a54472011-06-27 20:33:13 +00001365 if (mEfiShellEnvironment2 != NULL) {
1366 //
1367 // Enable with EFI Shell
1368 //
1369 mEfiShellEnvironment2->EnablePageBreak (DEFAULT_INIT_ROW, DEFAULT_AUTO_LF);
1370 return;
1371 }
jcarsey94b17fa2009-05-07 18:46:18 +00001372 }
1373 } else {
jcarsey1e6e84c2010-01-25 20:05:08 +00001374 //
jcarsey94b17fa2009-05-07 18:46:18 +00001375 // check for UEFI Shell 2.0
1376 //
jcarsey366f81a2011-06-27 21:04:22 +00001377 if (gEfiShellProtocol != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00001378 //
1379 // Disable with UEFI 2.0 Shell
1380 //
jcarsey366f81a2011-06-27 21:04:22 +00001381 gEfiShellProtocol->DisablePageBreak();
jcarsey94b17fa2009-05-07 18:46:18 +00001382 return;
1383 } else {
jcarsey1e6e84c2010-01-25 20:05:08 +00001384 //
jcarsey92a54472011-06-27 20:33:13 +00001385 // Check for EFI shell
jcarsey94b17fa2009-05-07 18:46:18 +00001386 //
jcarsey92a54472011-06-27 20:33:13 +00001387 if (mEfiShellEnvironment2 != NULL) {
1388 //
1389 // Disable with EFI Shell
1390 //
1391 mEfiShellEnvironment2->DisablePageBreak ();
1392 return;
1393 }
jcarsey94b17fa2009-05-07 18:46:18 +00001394 }
1395 }
1396}
1397
1398///
1399/// version of EFI_SHELL_FILE_INFO struct, except has no CONST pointers.
1400/// This allows for the struct to be populated.
1401///
1402typedef struct {
jcarseyd2b45642009-05-11 18:02:16 +00001403 LIST_ENTRY Link;
jcarsey94b17fa2009-05-07 18:46:18 +00001404 EFI_STATUS Status;
1405 CHAR16 *FullName;
1406 CHAR16 *FileName;
jcarseya405b862010-09-14 05:18:09 +00001407 SHELL_FILE_HANDLE Handle;
jcarsey94b17fa2009-05-07 18:46:18 +00001408 EFI_FILE_INFO *Info;
1409} EFI_SHELL_FILE_INFO_NO_CONST;
1410
1411/**
1412 Converts a EFI shell list of structures to the coresponding UEFI Shell 2.0 type of list.
1413
1414 if OldStyleFileList is NULL then ASSERT()
1415
jcarsey1e6e84c2010-01-25 20:05:08 +00001416 this function will convert a SHELL_FILE_ARG based list into a callee allocated
jcarsey94b17fa2009-05-07 18:46:18 +00001417 EFI_SHELL_FILE_INFO based list. it is up to the caller to free the memory via
1418 the ShellCloseFileMetaArg function.
1419
ydong104ff7e372011-09-02 08:05:34 +00001420 @param[in] FileList the EFI shell list type
1421 @param[in, out] ListHead the list to add to
jcarsey94b17fa2009-05-07 18:46:18 +00001422
1423 @retval the resultant head of the double linked new format list;
1424**/
1425LIST_ENTRY*
1426EFIAPI
1427InternalShellConvertFileListType (
jcarsey9b3bf082009-06-23 21:15:07 +00001428 IN LIST_ENTRY *FileList,
1429 IN OUT LIST_ENTRY *ListHead
jcarsey125c2cf2009-11-18 21:36:50 +00001430 )
1431{
jcarsey94b17fa2009-05-07 18:46:18 +00001432 SHELL_FILE_ARG *OldInfo;
jcarsey9b3bf082009-06-23 21:15:07 +00001433 LIST_ENTRY *Link;
jcarsey94b17fa2009-05-07 18:46:18 +00001434 EFI_SHELL_FILE_INFO_NO_CONST *NewInfo;
1435
1436 //
jcarsey9b3bf082009-06-23 21:15:07 +00001437 // ASSERTs
jcarsey94b17fa2009-05-07 18:46:18 +00001438 //
jcarsey9b3bf082009-06-23 21:15:07 +00001439 ASSERT(FileList != NULL);
1440 ASSERT(ListHead != NULL);
jcarsey94b17fa2009-05-07 18:46:18 +00001441
1442 //
1443 // enumerate through each member of the old list and copy
1444 //
jcarseyd2b45642009-05-11 18:02:16 +00001445 for (Link = FileList->ForwardLink; Link != FileList; Link = Link->ForwardLink) {
jcarsey94b17fa2009-05-07 18:46:18 +00001446 OldInfo = CR (Link, SHELL_FILE_ARG, Link, SHELL_FILE_ARG_SIGNATURE);
jcarseya405b862010-09-14 05:18:09 +00001447 ASSERT(OldInfo != NULL);
1448
1449 //
1450 // Skip ones that failed to open...
1451 //
1452 if (OldInfo->Status != EFI_SUCCESS) {
1453 continue;
1454 }
jcarsey94b17fa2009-05-07 18:46:18 +00001455
1456 //
1457 // make sure the old list was valid
1458 //
jcarsey94b17fa2009-05-07 18:46:18 +00001459 ASSERT(OldInfo->Info != NULL);
1460 ASSERT(OldInfo->FullName != NULL);
1461 ASSERT(OldInfo->FileName != NULL);
1462
1463 //
1464 // allocate a new EFI_SHELL_FILE_INFO object
1465 //
1466 NewInfo = AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));
jcarseyc9d92df2010-02-03 15:37:54 +00001467 if (NewInfo == NULL) {
jcarsey7a95efd2011-10-13 16:08:18 +00001468 ShellCloseFileMetaArg((EFI_SHELL_FILE_INFO**)(&ListHead));
jcarseybeab0fc2011-10-10 17:26:25 +00001469 ListHead = NULL;
jcarseyc9d92df2010-02-03 15:37:54 +00001470 break;
1471 }
jcarsey1e6e84c2010-01-25 20:05:08 +00001472
1473 //
jcarsey94b17fa2009-05-07 18:46:18 +00001474 // copy the simple items
1475 //
1476 NewInfo->Handle = OldInfo->Handle;
1477 NewInfo->Status = OldInfo->Status;
1478
jcarseyd2b45642009-05-11 18:02:16 +00001479 // old shell checks for 0 not NULL
1480 OldInfo->Handle = 0;
1481
jcarsey94b17fa2009-05-07 18:46:18 +00001482 //
1483 // allocate new space to copy strings and structure
1484 //
Jaben Carsey98c16be2014-08-19 21:00:34 +00001485 NewInfo->FullName = AllocateCopyPool(StrSize(OldInfo->FullName), OldInfo->FullName);
1486 NewInfo->FileName = AllocateCopyPool(StrSize(OldInfo->FileName), OldInfo->FileName);
1487 NewInfo->Info = AllocateCopyPool((UINTN)OldInfo->Info->Size, OldInfo->Info);
jcarsey1e6e84c2010-01-25 20:05:08 +00001488
jcarsey94b17fa2009-05-07 18:46:18 +00001489 //
1490 // make sure all the memory allocations were sucessful
1491 //
jcarseybeab0fc2011-10-10 17:26:25 +00001492 if (NULL == NewInfo->FullName || NewInfo->FileName == NULL || NewInfo->Info == NULL) {
Jaben Carsey98c16be2014-08-19 21:00:34 +00001493 //
1494 // Free the partially allocated new node
1495 //
1496 SHELL_FREE_NON_NULL(NewInfo->FullName);
1497 SHELL_FREE_NON_NULL(NewInfo->FileName);
1498 SHELL_FREE_NON_NULL(NewInfo->Info);
1499 SHELL_FREE_NON_NULL(NewInfo);
1500
1501 //
1502 // Free the previously converted stuff
1503 //
jcarsey7a95efd2011-10-13 16:08:18 +00001504 ShellCloseFileMetaArg((EFI_SHELL_FILE_INFO**)(&ListHead));
jcarseybeab0fc2011-10-10 17:26:25 +00001505 ListHead = NULL;
1506 break;
1507 }
jcarsey94b17fa2009-05-07 18:46:18 +00001508
1509 //
jcarsey94b17fa2009-05-07 18:46:18 +00001510 // add that to the list
1511 //
jcarsey9b3bf082009-06-23 21:15:07 +00001512 InsertTailList(ListHead, &NewInfo->Link);
jcarsey94b17fa2009-05-07 18:46:18 +00001513 }
1514 return (ListHead);
1515}
1516/**
1517 Opens a group of files based on a path.
1518
jcarsey1e6e84c2010-01-25 20:05:08 +00001519 This function uses the Arg to open all the matching files. Each matched
Brendan Jackman70879312014-01-24 22:29:53 +00001520 file has a SHELL_FILE_INFO structure to record the file information. These
1521 structures are placed on the list ListHead. Users can get the SHELL_FILE_INFO
jcarsey94b17fa2009-05-07 18:46:18 +00001522 structures from ListHead to access each file. This function supports wildcards
jcarsey1e6e84c2010-01-25 20:05:08 +00001523 and will process '?' and '*' as such. the list must be freed with a call to
jcarsey94b17fa2009-05-07 18:46:18 +00001524 ShellCloseFileMetaArg().
1525
jcarsey1e6e84c2010-01-25 20:05:08 +00001526 If you are NOT appending to an existing list *ListHead must be NULL. If
jcarsey5f7431d2009-07-10 18:06:01 +00001527 *ListHead is NULL then it must be callee freed.
jcarsey94b17fa2009-05-07 18:46:18 +00001528
1529 @param Arg pointer to path string
1530 @param OpenMode mode to open files with
1531 @param ListHead head of linked list of results
1532
jcarsey1e6e84c2010-01-25 20:05:08 +00001533 @retval EFI_SUCCESS the operation was sucessful and the list head
jcarsey94b17fa2009-05-07 18:46:18 +00001534 contains the list of opened files
jcarsey94b17fa2009-05-07 18:46:18 +00001535 @return != EFI_SUCCESS the operation failed
1536
1537 @sa InternalShellConvertFileListType
1538**/
1539EFI_STATUS
1540EFIAPI
1541ShellOpenFileMetaArg (
1542 IN CHAR16 *Arg,
1543 IN UINT64 OpenMode,
1544 IN OUT EFI_SHELL_FILE_INFO **ListHead
1545 )
1546{
1547 EFI_STATUS Status;
jcarsey9b3bf082009-06-23 21:15:07 +00001548 LIST_ENTRY mOldStyleFileList;
Qiu Shumin0960ba12014-09-17 07:58:31 +00001549 CHAR16 *CleanFilePathStr;
jcarsey1e6e84c2010-01-25 20:05:08 +00001550
jcarsey94b17fa2009-05-07 18:46:18 +00001551 //
1552 // ASSERT that Arg and ListHead are not NULL
1553 //
1554 ASSERT(Arg != NULL);
1555 ASSERT(ListHead != NULL);
1556
Qiu Shumin715096c2014-09-19 01:32:05 +00001557 CleanFilePathStr = NULL;
1558
Qiu Shumin0960ba12014-09-17 07:58:31 +00001559 Status = InternalShellStripQuotes (Arg, &CleanFilePathStr);
1560 if (EFI_ERROR (Status)) {
1561 return Status;
1562 }
1563
jcarsey1e6e84c2010-01-25 20:05:08 +00001564 //
jcarsey94b17fa2009-05-07 18:46:18 +00001565 // Check for UEFI Shell 2.0 protocols
1566 //
jcarsey366f81a2011-06-27 21:04:22 +00001567 if (gEfiShellProtocol != NULL) {
jcarsey5f7431d2009-07-10 18:06:01 +00001568 if (*ListHead == NULL) {
1569 *ListHead = (EFI_SHELL_FILE_INFO*)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));
1570 if (*ListHead == NULL) {
Qiu Shumin0960ba12014-09-17 07:58:31 +00001571 FreePool(CleanFilePathStr);
jcarsey5f7431d2009-07-10 18:06:01 +00001572 return (EFI_OUT_OF_RESOURCES);
1573 }
1574 InitializeListHead(&((*ListHead)->Link));
jcarsey1e6e84c2010-01-25 20:05:08 +00001575 }
Qiu Shumin0960ba12014-09-17 07:58:31 +00001576 Status = gEfiShellProtocol->OpenFileList(CleanFilePathStr,
jcarsey1e6e84c2010-01-25 20:05:08 +00001577 OpenMode,
jcarsey2247dde2009-11-09 18:08:58 +00001578 ListHead);
1579 if (EFI_ERROR(Status)) {
jcarsey366f81a2011-06-27 21:04:22 +00001580 gEfiShellProtocol->RemoveDupInFileList(ListHead);
jcarsey2247dde2009-11-09 18:08:58 +00001581 } else {
jcarsey366f81a2011-06-27 21:04:22 +00001582 Status = gEfiShellProtocol->RemoveDupInFileList(ListHead);
jcarsey2247dde2009-11-09 18:08:58 +00001583 }
jcarseya405b862010-09-14 05:18:09 +00001584 if (*ListHead != NULL && IsListEmpty(&(*ListHead)->Link)) {
1585 FreePool(*ListHead);
Qiu Shumin0960ba12014-09-17 07:58:31 +00001586 FreePool(CleanFilePathStr);
jcarseya405b862010-09-14 05:18:09 +00001587 *ListHead = NULL;
1588 return (EFI_NOT_FOUND);
1589 }
Qiu Shumin0960ba12014-09-17 07:58:31 +00001590 FreePool(CleanFilePathStr);
jcarsey2247dde2009-11-09 18:08:58 +00001591 return (Status);
jcarsey1e6e84c2010-01-25 20:05:08 +00001592 }
jcarsey94b17fa2009-05-07 18:46:18 +00001593
1594 //
jcarsey92a54472011-06-27 20:33:13 +00001595 // Check for EFI shell
jcarsey94b17fa2009-05-07 18:46:18 +00001596 //
jcarsey92a54472011-06-27 20:33:13 +00001597 if (mEfiShellEnvironment2 != NULL) {
1598 //
1599 // make sure the list head is initialized
1600 //
1601 InitializeListHead(&mOldStyleFileList);
jcarsey94b17fa2009-05-07 18:46:18 +00001602
jcarsey92a54472011-06-27 20:33:13 +00001603 //
1604 // Get the EFI Shell list of files
1605 //
Qiu Shumin0960ba12014-09-17 07:58:31 +00001606 Status = mEfiShellEnvironment2->FileMetaArg(CleanFilePathStr, &mOldStyleFileList);
jcarsey92a54472011-06-27 20:33:13 +00001607 if (EFI_ERROR(Status)) {
1608 *ListHead = NULL;
Qiu Shumin0960ba12014-09-17 07:58:31 +00001609 FreePool(CleanFilePathStr);
jcarsey92a54472011-06-27 20:33:13 +00001610 return (Status);
1611 }
jcarsey94b17fa2009-05-07 18:46:18 +00001612
jcarsey92a54472011-06-27 20:33:13 +00001613 if (*ListHead == NULL) {
1614 *ListHead = (EFI_SHELL_FILE_INFO *)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));
1615 if (*ListHead == NULL) {
Qiu Shumin0960ba12014-09-17 07:58:31 +00001616 FreePool(CleanFilePathStr);
jcarsey92a54472011-06-27 20:33:13 +00001617 return (EFI_OUT_OF_RESOURCES);
1618 }
1619 InitializeListHead(&((*ListHead)->Link));
1620 }
1621
1622 //
1623 // Convert that to equivalent of UEFI Shell 2.0 structure
1624 //
1625 InternalShellConvertFileListType(&mOldStyleFileList, &(*ListHead)->Link);
1626
1627 //
1628 // Free the EFI Shell version that was converted.
1629 //
1630 mEfiShellEnvironment2->FreeFileList(&mOldStyleFileList);
1631
1632 if ((*ListHead)->Link.ForwardLink == (*ListHead)->Link.BackLink && (*ListHead)->Link.BackLink == &((*ListHead)->Link)) {
1633 FreePool(*ListHead);
1634 *ListHead = NULL;
1635 Status = EFI_NOT_FOUND;
1636 }
Qiu Shumin0960ba12014-09-17 07:58:31 +00001637 FreePool(CleanFilePathStr);
jcarsey94b17fa2009-05-07 18:46:18 +00001638 return (Status);
1639 }
1640
Qiu Shumin0960ba12014-09-17 07:58:31 +00001641 FreePool(CleanFilePathStr);
jcarsey92a54472011-06-27 20:33:13 +00001642 return (EFI_UNSUPPORTED);
jcarsey94b17fa2009-05-07 18:46:18 +00001643}
1644/**
jcarseya405b862010-09-14 05:18:09 +00001645 Free the linked list returned from ShellOpenFileMetaArg.
jcarsey94b17fa2009-05-07 18:46:18 +00001646
jcarseya405b862010-09-14 05:18:09 +00001647 if ListHead is NULL then ASSERT().
jcarsey94b17fa2009-05-07 18:46:18 +00001648
jcarseya405b862010-09-14 05:18:09 +00001649 @param ListHead the pointer to free.
jcarsey94b17fa2009-05-07 18:46:18 +00001650
jcarseya405b862010-09-14 05:18:09 +00001651 @retval EFI_SUCCESS the operation was sucessful.
jcarsey94b17fa2009-05-07 18:46:18 +00001652**/
1653EFI_STATUS
1654EFIAPI
1655ShellCloseFileMetaArg (
1656 IN OUT EFI_SHELL_FILE_INFO **ListHead
1657 )
1658{
1659 LIST_ENTRY *Node;
1660
1661 //
1662 // ASSERT that ListHead is not NULL
1663 //
1664 ASSERT(ListHead != NULL);
1665
jcarsey1e6e84c2010-01-25 20:05:08 +00001666 //
jcarsey94b17fa2009-05-07 18:46:18 +00001667 // Check for UEFI Shell 2.0 protocols
1668 //
jcarsey366f81a2011-06-27 21:04:22 +00001669 if (gEfiShellProtocol != NULL) {
1670 return (gEfiShellProtocol->FreeFileList(ListHead));
jcarsey92a54472011-06-27 20:33:13 +00001671 } else if (mEfiShellEnvironment2 != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00001672 //
jcarsey1e6e84c2010-01-25 20:05:08 +00001673 // Since this is EFI Shell version we need to free our internally made copy
jcarsey94b17fa2009-05-07 18:46:18 +00001674 // of the list
1675 //
jcarsey1e6e84c2010-01-25 20:05:08 +00001676 for ( Node = GetFirstNode(&(*ListHead)->Link)
jcarseya405b862010-09-14 05:18:09 +00001677 ; *ListHead != NULL && !IsListEmpty(&(*ListHead)->Link)
jcarsey9b3bf082009-06-23 21:15:07 +00001678 ; Node = GetFirstNode(&(*ListHead)->Link)) {
jcarsey94b17fa2009-05-07 18:46:18 +00001679 RemoveEntryList(Node);
jcarseya405b862010-09-14 05:18:09 +00001680 ((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 +00001681 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->FullName);
1682 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->FileName);
1683 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->Info);
1684 FreePool((EFI_SHELL_FILE_INFO_NO_CONST*)Node);
1685 }
Jaben Carsey75a5e2e2014-01-10 16:42:45 +00001686 SHELL_FREE_NON_NULL(*ListHead);
jcarsey94b17fa2009-05-07 18:46:18 +00001687 return EFI_SUCCESS;
1688 }
jcarsey92a54472011-06-27 20:33:13 +00001689
1690 return (EFI_UNSUPPORTED);
jcarsey94b17fa2009-05-07 18:46:18 +00001691}
1692
jcarsey125c2cf2009-11-18 21:36:50 +00001693/**
1694 Find a file by searching the CWD and then the path.
1695
jcarseyb3011f42010-01-11 21:49:04 +00001696 If FileName is NULL then ASSERT.
jcarsey125c2cf2009-11-18 21:36:50 +00001697
jcarseyb3011f42010-01-11 21:49:04 +00001698 If the return value is not NULL then the memory must be caller freed.
jcarsey125c2cf2009-11-18 21:36:50 +00001699
1700 @param FileName Filename string.
1701
1702 @retval NULL the file was not found
1703 @return !NULL the full path to the file.
1704**/
1705CHAR16 *
1706EFIAPI
1707ShellFindFilePath (
1708 IN CONST CHAR16 *FileName
1709 )
1710{
1711 CONST CHAR16 *Path;
jcarseya405b862010-09-14 05:18:09 +00001712 SHELL_FILE_HANDLE Handle;
jcarsey125c2cf2009-11-18 21:36:50 +00001713 EFI_STATUS Status;
1714 CHAR16 *RetVal;
1715 CHAR16 *TestPath;
1716 CONST CHAR16 *Walker;
jcarsey36a9d672009-11-20 21:13:41 +00001717 UINTN Size;
jcarsey1cd45e72010-01-29 15:07:44 +00001718 CHAR16 *TempChar;
jcarsey125c2cf2009-11-18 21:36:50 +00001719
1720 RetVal = NULL;
1721
jcarseya405b862010-09-14 05:18:09 +00001722 //
1723 // First make sure its not an absolute path.
1724 //
1725 Status = ShellOpenFileByName(FileName, &Handle, EFI_FILE_MODE_READ, 0);
1726 if (!EFI_ERROR(Status)){
1727 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {
1728 ASSERT(RetVal == NULL);
1729 RetVal = StrnCatGrow(&RetVal, NULL, FileName, 0);
1730 ShellCloseFile(&Handle);
1731 return (RetVal);
1732 } else {
1733 ShellCloseFile(&Handle);
1734 }
1735 }
1736
jcarsey125c2cf2009-11-18 21:36:50 +00001737 Path = ShellGetEnvironmentVariable(L"cwd");
1738 if (Path != NULL) {
Qiu Shuminfbd2dfa2015-10-23 02:03:20 +00001739 Size = StrSize(Path) + sizeof(CHAR16);
jcarsey36a9d672009-11-20 21:13:41 +00001740 Size += StrSize(FileName);
1741 TestPath = AllocateZeroPool(Size);
jcarseyc9d92df2010-02-03 15:37:54 +00001742 if (TestPath == NULL) {
1743 return (NULL);
1744 }
Qiu Shumine75390f2015-06-30 03:18:31 +00001745 StrCpyS(TestPath, Size/sizeof(CHAR16), Path);
Qiu Shuminfbd2dfa2015-10-23 02:03:20 +00001746 StrCatS(TestPath, Size/sizeof(CHAR16), L"\\");
Qiu Shumine75390f2015-06-30 03:18:31 +00001747 StrCatS(TestPath, Size/sizeof(CHAR16), FileName);
jcarsey125c2cf2009-11-18 21:36:50 +00001748 Status = ShellOpenFileByName(TestPath, &Handle, EFI_FILE_MODE_READ, 0);
1749 if (!EFI_ERROR(Status)){
jcarseya405b862010-09-14 05:18:09 +00001750 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {
1751 ASSERT(RetVal == NULL);
1752 RetVal = StrnCatGrow(&RetVal, NULL, TestPath, 0);
1753 ShellCloseFile(&Handle);
1754 FreePool(TestPath);
1755 return (RetVal);
1756 } else {
1757 ShellCloseFile(&Handle);
1758 }
jcarsey125c2cf2009-11-18 21:36:50 +00001759 }
1760 FreePool(TestPath);
1761 }
1762 Path = ShellGetEnvironmentVariable(L"path");
1763 if (Path != NULL) {
jcarseya405b862010-09-14 05:18:09 +00001764 Size = StrSize(Path)+sizeof(CHAR16);
jcarsey36a9d672009-11-20 21:13:41 +00001765 Size += StrSize(FileName);
1766 TestPath = AllocateZeroPool(Size);
jcarsey3e082d52010-10-04 16:44:57 +00001767 if (TestPath == NULL) {
1768 return (NULL);
1769 }
jcarsey1e6e84c2010-01-25 20:05:08 +00001770 Walker = (CHAR16*)Path;
jcarsey125c2cf2009-11-18 21:36:50 +00001771 do {
1772 CopyMem(TestPath, Walker, StrSize(Walker));
jcarsey3e082d52010-10-04 16:44:57 +00001773 if (TestPath != NULL) {
1774 TempChar = StrStr(TestPath, L";");
1775 if (TempChar != NULL) {
1776 *TempChar = CHAR_NULL;
1777 }
1778 if (TestPath[StrLen(TestPath)-1] != L'\\') {
Qiu Shumine75390f2015-06-30 03:18:31 +00001779 StrCatS(TestPath, Size/sizeof(CHAR16), L"\\");
jcarsey3e082d52010-10-04 16:44:57 +00001780 }
jcarsey89e85372011-04-13 23:37:50 +00001781 if (FileName[0] == L'\\') {
1782 FileName++;
1783 }
Qiu Shumine75390f2015-06-30 03:18:31 +00001784 StrCatS(TestPath, Size/sizeof(CHAR16), FileName);
jcarsey3e082d52010-10-04 16:44:57 +00001785 if (StrStr(Walker, L";") != NULL) {
1786 Walker = StrStr(Walker, L";") + 1;
jcarseya405b862010-09-14 05:18:09 +00001787 } else {
jcarsey3e082d52010-10-04 16:44:57 +00001788 Walker = NULL;
1789 }
1790 Status = ShellOpenFileByName(TestPath, &Handle, EFI_FILE_MODE_READ, 0);
1791 if (!EFI_ERROR(Status)){
1792 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {
1793 ASSERT(RetVal == NULL);
1794 RetVal = StrnCatGrow(&RetVal, NULL, TestPath, 0);
1795 ShellCloseFile(&Handle);
1796 break;
1797 } else {
1798 ShellCloseFile(&Handle);
1799 }
jcarseya405b862010-09-14 05:18:09 +00001800 }
jcarsey125c2cf2009-11-18 21:36:50 +00001801 }
1802 } while (Walker != NULL && Walker[0] != CHAR_NULL);
1803 FreePool(TestPath);
1804 }
1805 return (RetVal);
1806}
1807
jcarseyb3011f42010-01-11 21:49:04 +00001808/**
jcarsey1e6e84c2010-01-25 20:05:08 +00001809 Find a file by searching the CWD and then the path with a variable set of file
1810 extensions. If the file is not found it will append each extension in the list
jcarseyb3011f42010-01-11 21:49:04 +00001811 in the order provided and return the first one that is successful.
1812
1813 If FileName is NULL, then ASSERT.
1814 If FileExtension is NULL, then behavior is identical to ShellFindFilePath.
1815
1816 If the return value is not NULL then the memory must be caller freed.
1817
1818 @param[in] FileName Filename string.
1819 @param[in] FileExtension Semi-colon delimeted list of possible extensions.
1820
1821 @retval NULL The file was not found.
1822 @retval !NULL The path to the file.
1823**/
1824CHAR16 *
1825EFIAPI
1826ShellFindFilePathEx (
1827 IN CONST CHAR16 *FileName,
1828 IN CONST CHAR16 *FileExtension
1829 )
1830{
1831 CHAR16 *TestPath;
1832 CHAR16 *RetVal;
1833 CONST CHAR16 *ExtensionWalker;
jcarsey9e926b62010-01-14 20:26:39 +00001834 UINTN Size;
jcarsey1cd45e72010-01-29 15:07:44 +00001835 CHAR16 *TempChar;
jcarseyc9d92df2010-02-03 15:37:54 +00001836 CHAR16 *TempChar2;
jcarsey1cd45e72010-01-29 15:07:44 +00001837
jcarseyb3011f42010-01-11 21:49:04 +00001838 ASSERT(FileName != NULL);
1839 if (FileExtension == NULL) {
1840 return (ShellFindFilePath(FileName));
1841 }
1842 RetVal = ShellFindFilePath(FileName);
1843 if (RetVal != NULL) {
1844 return (RetVal);
1845 }
jcarsey9e926b62010-01-14 20:26:39 +00001846 Size = StrSize(FileName);
1847 Size += StrSize(FileExtension);
1848 TestPath = AllocateZeroPool(Size);
jcarseyc9d92df2010-02-03 15:37:54 +00001849 if (TestPath == NULL) {
1850 return (NULL);
1851 }
jcarseya405b862010-09-14 05:18:09 +00001852 for (ExtensionWalker = FileExtension, TempChar2 = (CHAR16*)FileExtension; TempChar2 != NULL ; ExtensionWalker = TempChar2 + 1){
Qiu Shumine75390f2015-06-30 03:18:31 +00001853 StrCpyS(TestPath, Size/sizeof(CHAR16), FileName);
jcarseya405b862010-09-14 05:18:09 +00001854 if (ExtensionWalker != NULL) {
Qiu Shumine75390f2015-06-30 03:18:31 +00001855 StrCatS(TestPath, Size/sizeof(CHAR16), ExtensionWalker);
jcarseya405b862010-09-14 05:18:09 +00001856 }
jcarsey1cd45e72010-01-29 15:07:44 +00001857 TempChar = StrStr(TestPath, L";");
1858 if (TempChar != NULL) {
1859 *TempChar = CHAR_NULL;
jcarseyb3011f42010-01-11 21:49:04 +00001860 }
1861 RetVal = ShellFindFilePath(TestPath);
1862 if (RetVal != NULL) {
1863 break;
1864 }
jcarseya405b862010-09-14 05:18:09 +00001865 ASSERT(ExtensionWalker != NULL);
jcarseyc9d92df2010-02-03 15:37:54 +00001866 TempChar2 = StrStr(ExtensionWalker, L";");
jcarseyb3011f42010-01-11 21:49:04 +00001867 }
1868 FreePool(TestPath);
1869 return (RetVal);
1870}
1871
jcarsey94b17fa2009-05-07 18:46:18 +00001872typedef struct {
jcarsey9b3bf082009-06-23 21:15:07 +00001873 LIST_ENTRY Link;
jcarsey94b17fa2009-05-07 18:46:18 +00001874 CHAR16 *Name;
jcarseya405b862010-09-14 05:18:09 +00001875 SHELL_PARAM_TYPE Type;
jcarsey94b17fa2009-05-07 18:46:18 +00001876 CHAR16 *Value;
1877 UINTN OriginalPosition;
1878} SHELL_PARAM_PACKAGE;
1879
1880/**
jcarsey1e6e84c2010-01-25 20:05:08 +00001881 Checks the list of valid arguments and returns TRUE if the item was found. If the
jcarsey94b17fa2009-05-07 18:46:18 +00001882 return value is TRUE then the type parameter is set also.
jcarsey1e6e84c2010-01-25 20:05:08 +00001883
jcarsey94b17fa2009-05-07 18:46:18 +00001884 if CheckList is NULL then ASSERT();
1885 if Name is NULL then ASSERT();
1886 if Type is NULL then ASSERT();
1887
jcarsey94b17fa2009-05-07 18:46:18 +00001888 @param Name pointer to Name of parameter found
1889 @param CheckList List to check against
jcarseya405b862010-09-14 05:18:09 +00001890 @param Type pointer to type of parameter if it was found
jcarsey94b17fa2009-05-07 18:46:18 +00001891
1892 @retval TRUE the Parameter was found. Type is valid.
1893 @retval FALSE the Parameter was not found. Type is not valid.
1894**/
1895BOOLEAN
1896EFIAPI
jcarseyd2b45642009-05-11 18:02:16 +00001897InternalIsOnCheckList (
jcarsey94b17fa2009-05-07 18:46:18 +00001898 IN CONST CHAR16 *Name,
1899 IN CONST SHELL_PARAM_ITEM *CheckList,
jcarsey252d9452011-03-25 20:49:53 +00001900 OUT SHELL_PARAM_TYPE *Type
jcarseya405b862010-09-14 05:18:09 +00001901 )
1902{
jcarsey94b17fa2009-05-07 18:46:18 +00001903 SHELL_PARAM_ITEM *TempListItem;
jcarsey252d9452011-03-25 20:49:53 +00001904 CHAR16 *TempString;
jcarsey94b17fa2009-05-07 18:46:18 +00001905
1906 //
1907 // ASSERT that all 3 pointer parameters aren't NULL
1908 //
1909 ASSERT(CheckList != NULL);
1910 ASSERT(Type != NULL);
1911 ASSERT(Name != NULL);
1912
1913 //
jcarseyd2b45642009-05-11 18:02:16 +00001914 // question mark and page break mode are always supported
1915 //
1916 if ((StrCmp(Name, L"-?") == 0) ||
1917 (StrCmp(Name, L"-b") == 0)
jcarseya405b862010-09-14 05:18:09 +00001918 ) {
jcarsey252d9452011-03-25 20:49:53 +00001919 *Type = TypeFlag;
jcarseyd2b45642009-05-11 18:02:16 +00001920 return (TRUE);
1921 }
1922
1923 //
jcarsey94b17fa2009-05-07 18:46:18 +00001924 // Enumerate through the list
1925 //
1926 for (TempListItem = (SHELL_PARAM_ITEM*)CheckList ; TempListItem->Name != NULL ; TempListItem++) {
1927 //
jcarsey9eb53ac2009-07-08 17:26:58 +00001928 // If the Type is TypeStart only check the first characters of the passed in param
1929 // If it matches set the type and return TRUE
jcarsey94b17fa2009-05-07 18:46:18 +00001930 //
darylm503b0934ac2011-11-12 00:35:11 +00001931 if (TempListItem->Type == TypeStart) {
jcarsey252d9452011-03-25 20:49:53 +00001932 if (StrnCmp(Name, TempListItem->Name, StrLen(TempListItem->Name)) == 0) {
1933 *Type = TempListItem->Type;
1934 return (TRUE);
1935 }
1936 TempString = NULL;
1937 TempString = StrnCatGrow(&TempString, NULL, Name, StrLen(TempListItem->Name));
1938 if (TempString != NULL) {
1939 if (StringNoCaseCompare(&TempString, &TempListItem->Name) == 0) {
1940 *Type = TempListItem->Type;
1941 FreePool(TempString);
1942 return (TRUE);
1943 }
1944 FreePool(TempString);
1945 }
1946 } else if (StringNoCaseCompare(&Name, &TempListItem->Name) == 0) {
jcarsey94b17fa2009-05-07 18:46:18 +00001947 *Type = TempListItem->Type;
1948 return (TRUE);
1949 }
1950 }
jcarsey2247dde2009-11-09 18:08:58 +00001951
jcarsey94b17fa2009-05-07 18:46:18 +00001952 return (FALSE);
1953}
1954/**
jcarseyd2b45642009-05-11 18:02:16 +00001955 Checks the string for indicators of "flag" status. this is a leading '/', '-', or '+'
jcarsey94b17fa2009-05-07 18:46:18 +00001956
jcarseya405b862010-09-14 05:18:09 +00001957 @param[in] Name pointer to Name of parameter found
1958 @param[in] AlwaysAllowNumbers TRUE to allow numbers, FALSE to not.
jcarsey658bf432014-11-04 22:33:16 +00001959 @param[in] TimeNumbers TRUE to allow numbers with ":", FALSE otherwise.
jcarsey94b17fa2009-05-07 18:46:18 +00001960
1961 @retval TRUE the Parameter is a flag.
jcarseya405b862010-09-14 05:18:09 +00001962 @retval FALSE the Parameter not a flag.
jcarsey94b17fa2009-05-07 18:46:18 +00001963**/
1964BOOLEAN
1965EFIAPI
jcarseyd2b45642009-05-11 18:02:16 +00001966InternalIsFlag (
jcarsey2247dde2009-11-09 18:08:58 +00001967 IN CONST CHAR16 *Name,
jcarsey658bf432014-11-04 22:33:16 +00001968 IN CONST BOOLEAN AlwaysAllowNumbers,
1969 IN CONST BOOLEAN TimeNumbers
jcarsey94b17fa2009-05-07 18:46:18 +00001970 )
1971{
1972 //
1973 // ASSERT that Name isn't NULL
1974 //
1975 ASSERT(Name != NULL);
1976
1977 //
jcarsey2247dde2009-11-09 18:08:58 +00001978 // If we accept numbers then dont return TRUE. (they will be values)
1979 //
jcarsey658bf432014-11-04 22:33:16 +00001980 if (((Name[0] == L'-' || Name[0] == L'+') && InternalShellIsHexOrDecimalNumber(Name+1, FALSE, FALSE, TimeNumbers)) && AlwaysAllowNumbers) {
jcarsey2247dde2009-11-09 18:08:58 +00001981 return (FALSE);
1982 }
1983
1984 //
jcarseya405b862010-09-14 05:18:09 +00001985 // If the Name has a /, +, or - as the first character return TRUE
jcarsey94b17fa2009-05-07 18:46:18 +00001986 //
jcarsey1e6e84c2010-01-25 20:05:08 +00001987 if ((Name[0] == L'/') ||
jcarseyd2b45642009-05-11 18:02:16 +00001988 (Name[0] == L'-') ||
1989 (Name[0] == L'+')
jcarseya405b862010-09-14 05:18:09 +00001990 ) {
jcarsey94b17fa2009-05-07 18:46:18 +00001991 return (TRUE);
1992 }
1993 return (FALSE);
1994}
1995
1996/**
jcarsey1e6e84c2010-01-25 20:05:08 +00001997 Checks the command line arguments passed against the list of valid ones.
jcarsey94b17fa2009-05-07 18:46:18 +00001998
1999 If no initialization is required, then return RETURN_SUCCESS.
jcarsey1e6e84c2010-01-25 20:05:08 +00002000
jcarseya405b862010-09-14 05:18:09 +00002001 @param[in] CheckList pointer to list of parameters to check
2002 @param[out] CheckPackage pointer to pointer to list checked values
2003 @param[out] ProblemParam optional pointer to pointer to unicode string for
jcarseyd2b45642009-05-11 18:02:16 +00002004 the paramater that caused failure. If used then the
2005 caller is responsible for freeing the memory.
jcarseya405b862010-09-14 05:18:09 +00002006 @param[in] AutoPageBreak will automatically set PageBreakEnabled for "b" parameter
2007 @param[in] Argv pointer to array of parameters
2008 @param[in] Argc Count of parameters in Argv
2009 @param[in] AlwaysAllowNumbers TRUE to allow numbers always, FALSE otherwise.
jcarsey94b17fa2009-05-07 18:46:18 +00002010
2011 @retval EFI_SUCCESS The operation completed sucessfully.
2012 @retval EFI_OUT_OF_RESOURCES A memory allocation failed
2013 @retval EFI_INVALID_PARAMETER A parameter was invalid
jcarsey1e6e84c2010-01-25 20:05:08 +00002014 @retval EFI_VOLUME_CORRUPTED the command line was corrupt. an argument was
2015 duplicated. the duplicated command line argument
jcarsey94b17fa2009-05-07 18:46:18 +00002016 was returned in ProblemParam if provided.
jcarsey1e6e84c2010-01-25 20:05:08 +00002017 @retval EFI_NOT_FOUND a argument required a value that was missing.
jcarsey94b17fa2009-05-07 18:46:18 +00002018 the invalid command line argument was returned in
2019 ProblemParam if provided.
2020**/
2021EFI_STATUS
2022EFIAPI
2023InternalCommandLineParse (
2024 IN CONST SHELL_PARAM_ITEM *CheckList,
2025 OUT LIST_ENTRY **CheckPackage,
2026 OUT CHAR16 **ProblemParam OPTIONAL,
2027 IN BOOLEAN AutoPageBreak,
2028 IN CONST CHAR16 **Argv,
jcarsey2247dde2009-11-09 18:08:58 +00002029 IN UINTN Argc,
2030 IN BOOLEAN AlwaysAllowNumbers
jcarseya405b862010-09-14 05:18:09 +00002031 )
2032{
jcarsey94b17fa2009-05-07 18:46:18 +00002033 UINTN LoopCounter;
jcarsey252d9452011-03-25 20:49:53 +00002034 SHELL_PARAM_TYPE CurrentItemType;
jcarsey94b17fa2009-05-07 18:46:18 +00002035 SHELL_PARAM_PACKAGE *CurrentItemPackage;
jcarsey125c2cf2009-11-18 21:36:50 +00002036 UINTN GetItemValue;
2037 UINTN ValueSize;
jcarseya405b862010-09-14 05:18:09 +00002038 UINTN Count;
jcarsey252d9452011-03-25 20:49:53 +00002039 CONST CHAR16 *TempPointer;
Jaben Carsey98c16be2014-08-19 21:00:34 +00002040 UINTN CurrentValueSize;
Ruiyu Ni2efafab2016-07-13 17:39:37 +08002041 CHAR16 *NewValue;
jcarsey94b17fa2009-05-07 18:46:18 +00002042
2043 CurrentItemPackage = NULL;
jcarsey125c2cf2009-11-18 21:36:50 +00002044 GetItemValue = 0;
2045 ValueSize = 0;
jcarseya405b862010-09-14 05:18:09 +00002046 Count = 0;
jcarsey94b17fa2009-05-07 18:46:18 +00002047
2048 //
2049 // If there is only 1 item we dont need to do anything
2050 //
jcarseya405b862010-09-14 05:18:09 +00002051 if (Argc < 1) {
jcarsey94b17fa2009-05-07 18:46:18 +00002052 *CheckPackage = NULL;
2053 return (EFI_SUCCESS);
2054 }
2055
2056 //
jcarsey2247dde2009-11-09 18:08:58 +00002057 // ASSERTs
2058 //
2059 ASSERT(CheckList != NULL);
2060 ASSERT(Argv != NULL);
2061
2062 //
jcarsey94b17fa2009-05-07 18:46:18 +00002063 // initialize the linked list
2064 //
2065 *CheckPackage = (LIST_ENTRY*)AllocateZeroPool(sizeof(LIST_ENTRY));
sfu502a758c2011-10-08 02:55:30 +00002066 if (*CheckPackage == NULL) {
jcarseybeab0fc2011-10-10 17:26:25 +00002067 return (EFI_OUT_OF_RESOURCES);
sfu502a758c2011-10-08 02:55:30 +00002068 }
jcarseybeab0fc2011-10-10 17:26:25 +00002069
jcarsey94b17fa2009-05-07 18:46:18 +00002070 InitializeListHead(*CheckPackage);
2071
2072 //
2073 // loop through each of the arguments
2074 //
2075 for (LoopCounter = 0 ; LoopCounter < Argc ; ++LoopCounter) {
2076 if (Argv[LoopCounter] == NULL) {
2077 //
2078 // do nothing for NULL argv
2079 //
jcarseya405b862010-09-14 05:18:09 +00002080 } else if (InternalIsOnCheckList(Argv[LoopCounter], CheckList, &CurrentItemType)) {
jcarsey94b17fa2009-05-07 18:46:18 +00002081 //
jcarsey2247dde2009-11-09 18:08:58 +00002082 // We might have leftover if last parameter didnt have optional value
2083 //
jcarsey125c2cf2009-11-18 21:36:50 +00002084 if (GetItemValue != 0) {
2085 GetItemValue = 0;
jcarsey2247dde2009-11-09 18:08:58 +00002086 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
2087 }
2088 //
jcarsey94b17fa2009-05-07 18:46:18 +00002089 // this is a flag
2090 //
jcarsey252d9452011-03-25 20:49:53 +00002091 CurrentItemPackage = AllocateZeroPool(sizeof(SHELL_PARAM_PACKAGE));
jcarseybeab0fc2011-10-10 17:26:25 +00002092 if (CurrentItemPackage == NULL) {
2093 ShellCommandLineFreeVarList(*CheckPackage);
2094 *CheckPackage = NULL;
2095 return (EFI_OUT_OF_RESOURCES);
2096 }
Jaben Carsey98c16be2014-08-19 21:00:34 +00002097 CurrentItemPackage->Name = AllocateCopyPool(StrSize(Argv[LoopCounter]), Argv[LoopCounter]);
jcarseybeab0fc2011-10-10 17:26:25 +00002098 if (CurrentItemPackage->Name == NULL) {
2099 ShellCommandLineFreeVarList(*CheckPackage);
2100 *CheckPackage = NULL;
2101 return (EFI_OUT_OF_RESOURCES);
2102 }
jcarsey94b17fa2009-05-07 18:46:18 +00002103 CurrentItemPackage->Type = CurrentItemType;
2104 CurrentItemPackage->OriginalPosition = (UINTN)(-1);
jcarseyb1f95a02009-06-16 00:23:19 +00002105 CurrentItemPackage->Value = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +00002106
2107 //
2108 // Does this flag require a value
2109 //
jcarsey125c2cf2009-11-18 21:36:50 +00002110 switch (CurrentItemPackage->Type) {
jcarsey94b17fa2009-05-07 18:46:18 +00002111 //
jcarsey125c2cf2009-11-18 21:36:50 +00002112 // possibly trigger the next loop(s) to populate the value of this item
jcarsey1e6e84c2010-01-25 20:05:08 +00002113 //
jcarsey125c2cf2009-11-18 21:36:50 +00002114 case TypeValue:
jcarsey658bf432014-11-04 22:33:16 +00002115 case TypeTimeValue:
jcarsey1e6e84c2010-01-25 20:05:08 +00002116 GetItemValue = 1;
jcarsey125c2cf2009-11-18 21:36:50 +00002117 ValueSize = 0;
2118 break;
2119 case TypeDoubleValue:
2120 GetItemValue = 2;
2121 ValueSize = 0;
2122 break;
2123 case TypeMaxValue:
2124 GetItemValue = (UINTN)(-1);
2125 ValueSize = 0;
2126 break;
2127 default:
2128 //
2129 // this item has no value expected; we are done
2130 //
2131 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
2132 ASSERT(GetItemValue == 0);
2133 break;
jcarsey94b17fa2009-05-07 18:46:18 +00002134 }
Qiu Shuminaf7a3a52015-03-13 02:04:17 +00002135 } else if (GetItemValue != 0 && CurrentItemPackage != NULL && !InternalIsFlag(Argv[LoopCounter], AlwaysAllowNumbers, (BOOLEAN)(CurrentItemPackage->Type == TypeTimeValue))) {
jcarseyb1f95a02009-06-16 00:23:19 +00002136 //
jcarsey125c2cf2009-11-18 21:36:50 +00002137 // get the item VALUE for a previous flag
jcarseyb1f95a02009-06-16 00:23:19 +00002138 //
Qiu Shumin484dd082015-04-01 00:49:05 +00002139 CurrentValueSize = ValueSize + StrSize(Argv[LoopCounter]) + sizeof(CHAR16);
Ruiyu Ni2efafab2016-07-13 17:39:37 +08002140 NewValue = ReallocatePool(ValueSize, CurrentValueSize, CurrentItemPackage->Value);
2141 if (NewValue == NULL) {
2142 SHELL_FREE_NON_NULL (CurrentItemPackage->Value);
2143 SHELL_FREE_NON_NULL (CurrentItemPackage);
2144 ShellCommandLineFreeVarList (*CheckPackage);
2145 *CheckPackage = NULL;
2146 return EFI_OUT_OF_RESOURCES;
2147 }
2148 CurrentItemPackage->Value = NewValue;
Qiu Shumin484dd082015-04-01 00:49:05 +00002149 if (ValueSize == 0) {
Qiu Shumine75390f2015-06-30 03:18:31 +00002150 StrCpyS( CurrentItemPackage->Value,
2151 CurrentValueSize/sizeof(CHAR16),
2152 Argv[LoopCounter]
2153 );
jcarsey125c2cf2009-11-18 21:36:50 +00002154 } else {
Qiu Shumine75390f2015-06-30 03:18:31 +00002155 StrCatS( CurrentItemPackage->Value,
2156 CurrentValueSize/sizeof(CHAR16),
2157 L" "
2158 );
2159 StrCatS( CurrentItemPackage->Value,
2160 CurrentValueSize/sizeof(CHAR16),
2161 Argv[LoopCounter]
2162 );
jcarsey125c2cf2009-11-18 21:36:50 +00002163 }
Qiu Shumin484dd082015-04-01 00:49:05 +00002164 ValueSize += StrSize(Argv[LoopCounter]) + sizeof(CHAR16);
2165
jcarsey125c2cf2009-11-18 21:36:50 +00002166 GetItemValue--;
2167 if (GetItemValue == 0) {
2168 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
2169 }
jcarsey658bf432014-11-04 22:33:16 +00002170 } else if (!InternalIsFlag(Argv[LoopCounter], AlwaysAllowNumbers, FALSE)){
jcarseyb1f95a02009-06-16 00:23:19 +00002171 //
2172 // add this one as a non-flag
2173 //
jcarsey252d9452011-03-25 20:49:53 +00002174
2175 TempPointer = Argv[LoopCounter];
darylm503b0934ac2011-11-12 00:35:11 +00002176 if ((*TempPointer == L'^' && *(TempPointer+1) == L'-')
jcarsey252d9452011-03-25 20:49:53 +00002177 || (*TempPointer == L'^' && *(TempPointer+1) == L'/')
2178 || (*TempPointer == L'^' && *(TempPointer+1) == L'+')
2179 ){
2180 TempPointer++;
2181 }
2182 CurrentItemPackage = AllocateZeroPool(sizeof(SHELL_PARAM_PACKAGE));
jcarseybeab0fc2011-10-10 17:26:25 +00002183 if (CurrentItemPackage == NULL) {
2184 ShellCommandLineFreeVarList(*CheckPackage);
2185 *CheckPackage = NULL;
2186 return (EFI_OUT_OF_RESOURCES);
2187 }
jcarseyb1f95a02009-06-16 00:23:19 +00002188 CurrentItemPackage->Name = NULL;
2189 CurrentItemPackage->Type = TypePosition;
Jaben Carsey98c16be2014-08-19 21:00:34 +00002190 CurrentItemPackage->Value = AllocateCopyPool(StrSize(TempPointer), TempPointer);
jcarseybeab0fc2011-10-10 17:26:25 +00002191 if (CurrentItemPackage->Value == NULL) {
2192 ShellCommandLineFreeVarList(*CheckPackage);
2193 *CheckPackage = NULL;
2194 return (EFI_OUT_OF_RESOURCES);
2195 }
jcarseya405b862010-09-14 05:18:09 +00002196 CurrentItemPackage->OriginalPosition = Count++;
jcarsey9b3bf082009-06-23 21:15:07 +00002197 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
jcarsey252d9452011-03-25 20:49:53 +00002198 } else {
jcarsey94b17fa2009-05-07 18:46:18 +00002199 //
2200 // this was a non-recognised flag... error!
2201 //
jcarsey252d9452011-03-25 20:49:53 +00002202 if (ProblemParam != NULL) {
Jaben Carsey98c16be2014-08-19 21:00:34 +00002203 *ProblemParam = AllocateCopyPool(StrSize(Argv[LoopCounter]), Argv[LoopCounter]);
jcarsey252d9452011-03-25 20:49:53 +00002204 }
jcarsey94b17fa2009-05-07 18:46:18 +00002205 ShellCommandLineFreeVarList(*CheckPackage);
2206 *CheckPackage = NULL;
2207 return (EFI_VOLUME_CORRUPTED);
jcarsey94b17fa2009-05-07 18:46:18 +00002208 }
2209 }
jcarsey125c2cf2009-11-18 21:36:50 +00002210 if (GetItemValue != 0) {
2211 GetItemValue = 0;
2212 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
2213 }
jcarsey94b17fa2009-05-07 18:46:18 +00002214 //
2215 // support for AutoPageBreak
2216 //
2217 if (AutoPageBreak && ShellCommandLineGetFlag(*CheckPackage, L"-b")) {
2218 ShellSetPageBreakMode(TRUE);
2219 }
2220 return (EFI_SUCCESS);
2221}
2222
2223/**
jcarsey1e6e84c2010-01-25 20:05:08 +00002224 Checks the command line arguments passed against the list of valid ones.
jcarsey94b17fa2009-05-07 18:46:18 +00002225 Optionally removes NULL values first.
jcarsey1e6e84c2010-01-25 20:05:08 +00002226
jcarsey94b17fa2009-05-07 18:46:18 +00002227 If no initialization is required, then return RETURN_SUCCESS.
jcarsey1e6e84c2010-01-25 20:05:08 +00002228
jcarseya405b862010-09-14 05:18:09 +00002229 @param[in] CheckList The pointer to list of parameters to check.
2230 @param[out] CheckPackage The package of checked values.
2231 @param[out] ProblemParam Optional pointer to pointer to unicode string for
jcarsey94b17fa2009-05-07 18:46:18 +00002232 the paramater that caused failure.
jcarseya405b862010-09-14 05:18:09 +00002233 @param[in] AutoPageBreak Will automatically set PageBreakEnabled.
2234 @param[in] AlwaysAllowNumbers Will never fail for number based flags.
jcarsey94b17fa2009-05-07 18:46:18 +00002235
2236 @retval EFI_SUCCESS The operation completed sucessfully.
jcarseya405b862010-09-14 05:18:09 +00002237 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
2238 @retval EFI_INVALID_PARAMETER A parameter was invalid.
2239 @retval EFI_VOLUME_CORRUPTED The command line was corrupt.
2240 @retval EFI_DEVICE_ERROR The commands contained 2 opposing arguments. One
jcarsey1e6e84c2010-01-25 20:05:08 +00002241 of the command line arguments was returned in
jcarsey94b17fa2009-05-07 18:46:18 +00002242 ProblemParam if provided.
jcarseya405b862010-09-14 05:18:09 +00002243 @retval EFI_NOT_FOUND A argument required a value that was missing.
2244 The invalid command line argument was returned in
jcarsey94b17fa2009-05-07 18:46:18 +00002245 ProblemParam if provided.
2246**/
2247EFI_STATUS
2248EFIAPI
jcarsey2247dde2009-11-09 18:08:58 +00002249ShellCommandLineParseEx (
jcarsey94b17fa2009-05-07 18:46:18 +00002250 IN CONST SHELL_PARAM_ITEM *CheckList,
2251 OUT LIST_ENTRY **CheckPackage,
2252 OUT CHAR16 **ProblemParam OPTIONAL,
jcarsey2247dde2009-11-09 18:08:58 +00002253 IN BOOLEAN AutoPageBreak,
2254 IN BOOLEAN AlwaysAllowNumbers
jcarseya405b862010-09-14 05:18:09 +00002255 )
2256{
jcarsey1e6e84c2010-01-25 20:05:08 +00002257 //
jcarsey94b17fa2009-05-07 18:46:18 +00002258 // ASSERT that CheckList and CheckPackage aren't NULL
2259 //
2260 ASSERT(CheckList != NULL);
2261 ASSERT(CheckPackage != NULL);
2262
jcarsey1e6e84c2010-01-25 20:05:08 +00002263 //
jcarsey94b17fa2009-05-07 18:46:18 +00002264 // Check for UEFI Shell 2.0 protocols
2265 //
jcarsey366f81a2011-06-27 21:04:22 +00002266 if (gEfiShellParametersProtocol != NULL) {
jcarsey1e6e84c2010-01-25 20:05:08 +00002267 return (InternalCommandLineParse(CheckList,
2268 CheckPackage,
2269 ProblemParam,
2270 AutoPageBreak,
jcarsey366f81a2011-06-27 21:04:22 +00002271 (CONST CHAR16**) gEfiShellParametersProtocol->Argv,
2272 gEfiShellParametersProtocol->Argc,
jcarsey2247dde2009-11-09 18:08:58 +00002273 AlwaysAllowNumbers));
jcarsey94b17fa2009-05-07 18:46:18 +00002274 }
2275
jcarsey1e6e84c2010-01-25 20:05:08 +00002276 //
jcarsey94b17fa2009-05-07 18:46:18 +00002277 // ASSERT That EFI Shell is not required
2278 //
2279 ASSERT (mEfiShellInterface != NULL);
jcarsey1e6e84c2010-01-25 20:05:08 +00002280 return (InternalCommandLineParse(CheckList,
2281 CheckPackage,
2282 ProblemParam,
2283 AutoPageBreak,
jljusten08d7f8e2009-06-15 18:42:13 +00002284 (CONST CHAR16**) mEfiShellInterface->Argv,
jcarsey2247dde2009-11-09 18:08:58 +00002285 mEfiShellInterface->Argc,
2286 AlwaysAllowNumbers));
jcarsey94b17fa2009-05-07 18:46:18 +00002287}
2288
2289/**
2290 Frees shell variable list that was returned from ShellCommandLineParse.
2291
2292 This function will free all the memory that was used for the CheckPackage
2293 list of postprocessed shell arguments.
2294
2295 this function has no return value.
2296
2297 if CheckPackage is NULL, then return
2298
2299 @param CheckPackage the list to de-allocate
2300 **/
2301VOID
2302EFIAPI
2303ShellCommandLineFreeVarList (
2304 IN LIST_ENTRY *CheckPackage
jcarseya405b862010-09-14 05:18:09 +00002305 )
2306{
jcarsey94b17fa2009-05-07 18:46:18 +00002307 LIST_ENTRY *Node;
2308
2309 //
2310 // check for CheckPackage == NULL
2311 //
2312 if (CheckPackage == NULL) {
2313 return;
2314 }
2315
2316 //
2317 // for each node in the list
2318 //
jcarsey9eb53ac2009-07-08 17:26:58 +00002319 for ( Node = GetFirstNode(CheckPackage)
jcarseya405b862010-09-14 05:18:09 +00002320 ; !IsListEmpty(CheckPackage)
jcarsey9eb53ac2009-07-08 17:26:58 +00002321 ; Node = GetFirstNode(CheckPackage)
jcarseya405b862010-09-14 05:18:09 +00002322 ){
jcarsey94b17fa2009-05-07 18:46:18 +00002323 //
2324 // Remove it from the list
2325 //
2326 RemoveEntryList(Node);
2327
2328 //
2329 // if it has a name free the name
2330 //
2331 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {
2332 FreePool(((SHELL_PARAM_PACKAGE*)Node)->Name);
2333 }
2334
2335 //
2336 // if it has a value free the value
2337 //
2338 if (((SHELL_PARAM_PACKAGE*)Node)->Value != NULL) {
2339 FreePool(((SHELL_PARAM_PACKAGE*)Node)->Value);
2340 }
jcarsey1e6e84c2010-01-25 20:05:08 +00002341
jcarsey94b17fa2009-05-07 18:46:18 +00002342 //
2343 // free the node structure
2344 //
2345 FreePool((SHELL_PARAM_PACKAGE*)Node);
2346 }
2347 //
2348 // free the list head node
2349 //
2350 FreePool(CheckPackage);
2351}
2352/**
2353 Checks for presence of a flag parameter
2354
2355 flag arguments are in the form of "-<Key>" or "/<Key>", but do not have a value following the key
2356
2357 if CheckPackage is NULL then return FALSE.
2358 if KeyString is NULL then ASSERT()
jcarsey1e6e84c2010-01-25 20:05:08 +00002359
jcarsey94b17fa2009-05-07 18:46:18 +00002360 @param CheckPackage The package of parsed command line arguments
2361 @param KeyString the Key of the command line argument to check for
2362
2363 @retval TRUE the flag is on the command line
2364 @retval FALSE the flag is not on the command line
2365 **/
2366BOOLEAN
2367EFIAPI
2368ShellCommandLineGetFlag (
jcarseya405b862010-09-14 05:18:09 +00002369 IN CONST LIST_ENTRY * CONST CheckPackage,
2370 IN CONST CHAR16 * CONST KeyString
2371 )
2372{
jcarsey94b17fa2009-05-07 18:46:18 +00002373 LIST_ENTRY *Node;
jcarsey252d9452011-03-25 20:49:53 +00002374 CHAR16 *TempString;
jcarsey94b17fa2009-05-07 18:46:18 +00002375
2376 //
ydong100c1950b2011-10-13 02:37:35 +00002377 // return FALSE for no package or KeyString is NULL
jcarsey94b17fa2009-05-07 18:46:18 +00002378 //
ydong100c1950b2011-10-13 02:37:35 +00002379 if (CheckPackage == NULL || KeyString == NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00002380 return (FALSE);
2381 }
2382
2383 //
2384 // enumerate through the list of parametrs
2385 //
jcarsey1e6e84c2010-01-25 20:05:08 +00002386 for ( Node = GetFirstNode(CheckPackage)
2387 ; !IsNull (CheckPackage, Node)
2388 ; Node = GetNextNode(CheckPackage, Node)
jcarsey252d9452011-03-25 20:49:53 +00002389 ){
jcarsey94b17fa2009-05-07 18:46:18 +00002390 //
2391 // If the Name matches, return TRUE (and there may be NULL name)
2392 //
2393 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {
jcarsey9eb53ac2009-07-08 17:26:58 +00002394 //
2395 // If Type is TypeStart then only compare the begining of the strings
2396 //
jcarsey252d9452011-03-25 20:49:53 +00002397 if (((SHELL_PARAM_PACKAGE*)Node)->Type == TypeStart) {
2398 if (StrnCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name, StrLen(KeyString)) == 0) {
2399 return (TRUE);
2400 }
2401 TempString = NULL;
2402 TempString = StrnCatGrow(&TempString, NULL, KeyString, StrLen(((SHELL_PARAM_PACKAGE*)Node)->Name));
2403 if (TempString != NULL) {
2404 if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {
2405 FreePool(TempString);
2406 return (TRUE);
2407 }
2408 FreePool(TempString);
2409 }
2410 } else if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {
jcarsey94b17fa2009-05-07 18:46:18 +00002411 return (TRUE);
2412 }
2413 }
2414 }
2415 return (FALSE);
2416}
2417/**
jcarseya405b862010-09-14 05:18:09 +00002418 Returns value from command line argument.
jcarsey94b17fa2009-05-07 18:46:18 +00002419
jcarseya405b862010-09-14 05:18:09 +00002420 Value parameters are in the form of "-<Key> value" or "/<Key> value".
jcarsey1e6e84c2010-01-25 20:05:08 +00002421
jcarseya405b862010-09-14 05:18:09 +00002422 If CheckPackage is NULL, then return NULL.
jcarsey94b17fa2009-05-07 18:46:18 +00002423
jcarseya405b862010-09-14 05:18:09 +00002424 @param[in] CheckPackage The package of parsed command line arguments.
2425 @param[in] KeyString The Key of the command line argument to check for.
jcarsey94b17fa2009-05-07 18:46:18 +00002426
jcarseya405b862010-09-14 05:18:09 +00002427 @retval NULL The flag is not on the command line.
2428 @retval !=NULL The pointer to unicode string of the value.
2429**/
jcarsey94b17fa2009-05-07 18:46:18 +00002430CONST CHAR16*
2431EFIAPI
2432ShellCommandLineGetValue (
2433 IN CONST LIST_ENTRY *CheckPackage,
2434 IN CHAR16 *KeyString
jcarseya405b862010-09-14 05:18:09 +00002435 )
2436{
jcarsey94b17fa2009-05-07 18:46:18 +00002437 LIST_ENTRY *Node;
jcarsey252d9452011-03-25 20:49:53 +00002438 CHAR16 *TempString;
jcarsey94b17fa2009-05-07 18:46:18 +00002439
2440 //
ydong100c1950b2011-10-13 02:37:35 +00002441 // return NULL for no package or KeyString is NULL
jcarsey94b17fa2009-05-07 18:46:18 +00002442 //
ydong100c1950b2011-10-13 02:37:35 +00002443 if (CheckPackage == NULL || KeyString == NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00002444 return (NULL);
2445 }
2446
2447 //
2448 // enumerate through the list of parametrs
2449 //
jcarsey1e6e84c2010-01-25 20:05:08 +00002450 for ( Node = GetFirstNode(CheckPackage)
2451 ; !IsNull (CheckPackage, Node)
2452 ; Node = GetNextNode(CheckPackage, Node)
jcarsey252d9452011-03-25 20:49:53 +00002453 ){
jcarsey94b17fa2009-05-07 18:46:18 +00002454 //
jcarsey252d9452011-03-25 20:49:53 +00002455 // If the Name matches, return TRUE (and there may be NULL name)
jcarsey94b17fa2009-05-07 18:46:18 +00002456 //
2457 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {
jcarsey9eb53ac2009-07-08 17:26:58 +00002458 //
2459 // If Type is TypeStart then only compare the begining of the strings
2460 //
jcarsey252d9452011-03-25 20:49:53 +00002461 if (((SHELL_PARAM_PACKAGE*)Node)->Type == TypeStart) {
2462 if (StrnCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name, StrLen(KeyString)) == 0) {
2463 return (((SHELL_PARAM_PACKAGE*)Node)->Name + StrLen(KeyString));
2464 }
2465 TempString = NULL;
2466 TempString = StrnCatGrow(&TempString, NULL, KeyString, StrLen(((SHELL_PARAM_PACKAGE*)Node)->Name));
2467 if (TempString != NULL) {
2468 if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {
2469 FreePool(TempString);
2470 return (((SHELL_PARAM_PACKAGE*)Node)->Name + StrLen(KeyString));
2471 }
2472 FreePool(TempString);
2473 }
2474 } else if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {
jcarsey94b17fa2009-05-07 18:46:18 +00002475 return (((SHELL_PARAM_PACKAGE*)Node)->Value);
2476 }
2477 }
2478 }
2479 return (NULL);
2480}
jcarseya405b862010-09-14 05:18:09 +00002481
jcarsey94b17fa2009-05-07 18:46:18 +00002482/**
jcarseya405b862010-09-14 05:18:09 +00002483 Returns raw value from command line argument.
jcarsey94b17fa2009-05-07 18:46:18 +00002484
jcarseya405b862010-09-14 05:18:09 +00002485 Raw value parameters are in the form of "value" in a specific position in the list.
jcarsey1e6e84c2010-01-25 20:05:08 +00002486
jcarseya405b862010-09-14 05:18:09 +00002487 If CheckPackage is NULL, then return NULL.
jcarsey94b17fa2009-05-07 18:46:18 +00002488
jcarseya405b862010-09-14 05:18:09 +00002489 @param[in] CheckPackage The package of parsed command line arguments.
2490 @param[in] Position The position of the value.
jcarsey94b17fa2009-05-07 18:46:18 +00002491
jcarseya405b862010-09-14 05:18:09 +00002492 @retval NULL The flag is not on the command line.
2493 @retval !=NULL The pointer to unicode string of the value.
jcarsey94b17fa2009-05-07 18:46:18 +00002494 **/
2495CONST CHAR16*
2496EFIAPI
2497ShellCommandLineGetRawValue (
jcarseya405b862010-09-14 05:18:09 +00002498 IN CONST LIST_ENTRY * CONST CheckPackage,
2499 IN UINTN Position
2500 )
2501{
jcarsey94b17fa2009-05-07 18:46:18 +00002502 LIST_ENTRY *Node;
2503
2504 //
2505 // check for CheckPackage == NULL
2506 //
2507 if (CheckPackage == NULL) {
2508 return (NULL);
2509 }
2510
2511 //
2512 // enumerate through the list of parametrs
2513 //
jcarsey1e6e84c2010-01-25 20:05:08 +00002514 for ( Node = GetFirstNode(CheckPackage)
2515 ; !IsNull (CheckPackage, Node)
2516 ; Node = GetNextNode(CheckPackage, Node)
jcarseya405b862010-09-14 05:18:09 +00002517 ){
jcarsey94b17fa2009-05-07 18:46:18 +00002518 //
2519 // If the position matches, return the value
2520 //
2521 if (((SHELL_PARAM_PACKAGE*)Node)->OriginalPosition == Position) {
2522 return (((SHELL_PARAM_PACKAGE*)Node)->Value);
2523 }
2524 }
2525 return (NULL);
jcarseyb1f95a02009-06-16 00:23:19 +00002526}
jcarsey2247dde2009-11-09 18:08:58 +00002527
2528/**
jcarsey1e6e84c2010-01-25 20:05:08 +00002529 returns the number of command line value parameters that were parsed.
2530
jcarsey2247dde2009-11-09 18:08:58 +00002531 this will not include flags.
2532
jcarseya405b862010-09-14 05:18:09 +00002533 @param[in] CheckPackage The package of parsed command line arguments.
2534
jcarsey2247dde2009-11-09 18:08:58 +00002535 @retval (UINTN)-1 No parsing has ocurred
2536 @return other The number of value parameters found
2537**/
2538UINTN
2539EFIAPI
2540ShellCommandLineGetCount(
jcarseya405b862010-09-14 05:18:09 +00002541 IN CONST LIST_ENTRY *CheckPackage
jcarsey125c2cf2009-11-18 21:36:50 +00002542 )
2543{
jcarseya405b862010-09-14 05:18:09 +00002544 LIST_ENTRY *Node1;
2545 UINTN Count;
2546
2547 if (CheckPackage == NULL) {
2548 return (0);
2549 }
2550 for ( Node1 = GetFirstNode(CheckPackage), Count = 0
2551 ; !IsNull (CheckPackage, Node1)
2552 ; Node1 = GetNextNode(CheckPackage, Node1)
2553 ){
2554 if (((SHELL_PARAM_PACKAGE*)Node1)->Name == NULL) {
2555 Count++;
2556 }
2557 }
2558 return (Count);
jcarsey2247dde2009-11-09 18:08:58 +00002559}
2560
jcarsey975136a2009-06-16 19:03:54 +00002561/**
Bruce Crancceb4eb2015-07-08 01:54:46 +00002562 Determines if a parameter is duplicated.
jcarsey36a9d672009-11-20 21:13:41 +00002563
jcarsey1e6e84c2010-01-25 20:05:08 +00002564 If Param is not NULL then it will point to a callee allocated string buffer
jcarsey36a9d672009-11-20 21:13:41 +00002565 with the parameter value if a duplicate is found.
2566
2567 If CheckPackage is NULL, then ASSERT.
2568
2569 @param[in] CheckPackage The package of parsed command line arguments.
2570 @param[out] Param Upon finding one, a pointer to the duplicated parameter.
2571
2572 @retval EFI_SUCCESS No parameters were duplicated.
2573 @retval EFI_DEVICE_ERROR A duplicate was found.
2574 **/
2575EFI_STATUS
2576EFIAPI
2577ShellCommandLineCheckDuplicate (
2578 IN CONST LIST_ENTRY *CheckPackage,
2579 OUT CHAR16 **Param
2580 )
2581{
2582 LIST_ENTRY *Node1;
2583 LIST_ENTRY *Node2;
jcarsey1e6e84c2010-01-25 20:05:08 +00002584
jcarsey36a9d672009-11-20 21:13:41 +00002585 ASSERT(CheckPackage != NULL);
2586
jcarsey1e6e84c2010-01-25 20:05:08 +00002587 for ( Node1 = GetFirstNode(CheckPackage)
2588 ; !IsNull (CheckPackage, Node1)
2589 ; Node1 = GetNextNode(CheckPackage, Node1)
jcarseya405b862010-09-14 05:18:09 +00002590 ){
jcarsey1e6e84c2010-01-25 20:05:08 +00002591 for ( Node2 = GetNextNode(CheckPackage, Node1)
2592 ; !IsNull (CheckPackage, Node2)
2593 ; Node2 = GetNextNode(CheckPackage, Node2)
jcarseya405b862010-09-14 05:18:09 +00002594 ){
2595 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 +00002596 if (Param != NULL) {
2597 *Param = NULL;
2598 *Param = StrnCatGrow(Param, NULL, ((SHELL_PARAM_PACKAGE*)Node1)->Name, 0);
2599 }
2600 return (EFI_DEVICE_ERROR);
2601 }
2602 }
2603 }
2604 return (EFI_SUCCESS);
2605}
2606
2607/**
jcarsey1e6e84c2010-01-25 20:05:08 +00002608 This is a find and replace function. Upon successful return the NewString is a copy of
jcarsey975136a2009-06-16 19:03:54 +00002609 SourceString with each instance of FindTarget replaced with ReplaceWith.
2610
jcarseyb3011f42010-01-11 21:49:04 +00002611 If SourceString and NewString overlap the behavior is undefined.
2612
jcarsey975136a2009-06-16 19:03:54 +00002613 If the string would grow bigger than NewSize it will halt and return error.
2614
ydong104ff7e372011-09-02 08:05:34 +00002615 @param[in] SourceString The string with source buffer.
2616 @param[in, out] NewString The string with resultant buffer.
2617 @param[in] NewSize The size in bytes of NewString.
2618 @param[in] FindTarget The string to look for.
2619 @param[in] ReplaceWith The string to replace FindTarget with.
2620 @param[in] SkipPreCarrot If TRUE will skip a FindTarget that has a '^'
2621 immediately before it.
2622 @param[in] ParameterReplacing If TRUE will add "" around items with spaces.
jcarsey975136a2009-06-16 19:03:54 +00002623
jcarsey969c7832010-01-13 16:46:33 +00002624 @retval EFI_INVALID_PARAMETER SourceString was NULL.
2625 @retval EFI_INVALID_PARAMETER NewString was NULL.
2626 @retval EFI_INVALID_PARAMETER FindTarget was NULL.
2627 @retval EFI_INVALID_PARAMETER ReplaceWith was NULL.
2628 @retval EFI_INVALID_PARAMETER FindTarget had length < 1.
2629 @retval EFI_INVALID_PARAMETER SourceString had length < 1.
jcarsey1e6e84c2010-01-25 20:05:08 +00002630 @retval EFI_BUFFER_TOO_SMALL NewSize was less than the minimum size to hold
jcarsey969c7832010-01-13 16:46:33 +00002631 the new string (truncation occurred).
jcarseya405b862010-09-14 05:18:09 +00002632 @retval EFI_SUCCESS The string was successfully copied with replacement.
jcarsey975136a2009-06-16 19:03:54 +00002633**/
jcarsey975136a2009-06-16 19:03:54 +00002634EFI_STATUS
2635EFIAPI
jcarseya405b862010-09-14 05:18:09 +00002636ShellCopySearchAndReplace(
jcarsey975136a2009-06-16 19:03:54 +00002637 IN CHAR16 CONST *SourceString,
jcarseya405b862010-09-14 05:18:09 +00002638 IN OUT CHAR16 *NewString,
jcarsey975136a2009-06-16 19:03:54 +00002639 IN UINTN NewSize,
2640 IN CONST CHAR16 *FindTarget,
jcarsey969c7832010-01-13 16:46:33 +00002641 IN CONST CHAR16 *ReplaceWith,
jcarseya405b862010-09-14 05:18:09 +00002642 IN CONST BOOLEAN SkipPreCarrot,
2643 IN CONST BOOLEAN ParameterReplacing
jcarsey1e6e84c2010-01-25 20:05:08 +00002644 )
jcarsey2247dde2009-11-09 18:08:58 +00002645{
jcarsey01582942009-07-10 19:46:17 +00002646 UINTN Size;
jcarseya405b862010-09-14 05:18:09 +00002647 CHAR16 *Replace;
2648
jcarsey975136a2009-06-16 19:03:54 +00002649 if ( (SourceString == NULL)
2650 || (NewString == NULL)
2651 || (FindTarget == NULL)
2652 || (ReplaceWith == NULL)
2653 || (StrLen(FindTarget) < 1)
2654 || (StrLen(SourceString) < 1)
jcarseya405b862010-09-14 05:18:09 +00002655 ){
jcarsey975136a2009-06-16 19:03:54 +00002656 return (EFI_INVALID_PARAMETER);
2657 }
jcarseya405b862010-09-14 05:18:09 +00002658 Replace = NULL;
2659 if (StrStr(ReplaceWith, L" ") == NULL || !ParameterReplacing) {
2660 Replace = StrnCatGrow(&Replace, NULL, ReplaceWith, 0);
2661 } else {
2662 Replace = AllocateZeroPool(StrSize(ReplaceWith) + 2*sizeof(CHAR16));
jcarseybeab0fc2011-10-10 17:26:25 +00002663 if (Replace != NULL) {
2664 UnicodeSPrint(Replace, StrSize(ReplaceWith) + 2*sizeof(CHAR16), L"\"%s\"", ReplaceWith);
2665 }
jcarseya405b862010-09-14 05:18:09 +00002666 }
jcarsey3e082d52010-10-04 16:44:57 +00002667 if (Replace == NULL) {
2668 return (EFI_OUT_OF_RESOURCES);
2669 }
Jaben Carsey98c16be2014-08-19 21:00:34 +00002670 NewString = ZeroMem(NewString, NewSize);
jcarsey2247dde2009-11-09 18:08:58 +00002671 while (*SourceString != CHAR_NULL) {
jcarsey969c7832010-01-13 16:46:33 +00002672 //
jcarseya405b862010-09-14 05:18:09 +00002673 // if we find the FindTarget and either Skip == FALSE or Skip and we
jcarsey969c7832010-01-13 16:46:33 +00002674 // dont have a carrot do a replace...
2675 //
jcarsey1e6e84c2010-01-25 20:05:08 +00002676 if (StrnCmp(SourceString, FindTarget, StrLen(FindTarget)) == 0
jcarseya405b862010-09-14 05:18:09 +00002677 && ((SkipPreCarrot && *(SourceString-1) != L'^') || !SkipPreCarrot)
2678 ){
jcarsey975136a2009-06-16 19:03:54 +00002679 SourceString += StrLen(FindTarget);
jcarsey01582942009-07-10 19:46:17 +00002680 Size = StrSize(NewString);
jcarseya405b862010-09-14 05:18:09 +00002681 if ((Size + (StrLen(Replace)*sizeof(CHAR16))) > NewSize) {
2682 FreePool(Replace);
jcarsey975136a2009-06-16 19:03:54 +00002683 return (EFI_BUFFER_TOO_SMALL);
2684 }
Qiu Shumine75390f2015-06-30 03:18:31 +00002685 StrCatS(NewString, NewSize/sizeof(CHAR16), Replace);
jcarsey975136a2009-06-16 19:03:54 +00002686 } else {
jcarsey01582942009-07-10 19:46:17 +00002687 Size = StrSize(NewString);
2688 if (Size + sizeof(CHAR16) > NewSize) {
jcarseya405b862010-09-14 05:18:09 +00002689 FreePool(Replace);
jcarsey975136a2009-06-16 19:03:54 +00002690 return (EFI_BUFFER_TOO_SMALL);
2691 }
Qiu Shumine75390f2015-06-30 03:18:31 +00002692 StrnCatS(NewString, NewSize/sizeof(CHAR16), SourceString, 1);
jcarsey975136a2009-06-16 19:03:54 +00002693 SourceString++;
2694 }
2695 }
jcarseya405b862010-09-14 05:18:09 +00002696 FreePool(Replace);
jcarsey975136a2009-06-16 19:03:54 +00002697 return (EFI_SUCCESS);
2698}
jcarseyb1f95a02009-06-16 00:23:19 +00002699
2700/**
jcarseye2f82972009-12-01 05:40:24 +00002701 Internal worker function to output a string.
2702
2703 This function will output a string to the correct StdOut.
2704
2705 @param[in] String The string to print out.
2706
2707 @retval EFI_SUCCESS The operation was sucessful.
2708 @retval !EFI_SUCCESS The operation failed.
2709**/
2710EFI_STATUS
2711EFIAPI
2712InternalPrintTo (
2713 IN CONST CHAR16 *String
2714 )
2715{
2716 UINTN Size;
2717 Size = StrSize(String) - sizeof(CHAR16);
jcarseya405b862010-09-14 05:18:09 +00002718 if (Size == 0) {
2719 return (EFI_SUCCESS);
2720 }
jcarsey366f81a2011-06-27 21:04:22 +00002721 if (gEfiShellParametersProtocol != NULL) {
2722 return (gEfiShellProtocol->WriteFile(gEfiShellParametersProtocol->StdOut, &Size, (VOID*)String));
jcarseye2f82972009-12-01 05:40:24 +00002723 }
2724 if (mEfiShellInterface != NULL) {
jcarsey06c355b2012-03-26 21:00:39 +00002725 if (mEfiShellInterface->RedirArgc == 0) {
jcarsey49bd4982012-01-27 18:42:43 +00002726 //
2727 // Divide in half for old shell. Must be string length not size.
jcarsey06c355b2012-03-26 21:00:39 +00002728 //
2729 Size /=2; // Divide in half only when no redirection.
2730 }
jcarseya405b862010-09-14 05:18:09 +00002731 return (mEfiShellInterface->StdOut->Write(mEfiShellInterface->StdOut, &Size, (VOID*)String));
jcarseye2f82972009-12-01 05:40:24 +00002732 }
2733 ASSERT(FALSE);
2734 return (EFI_UNSUPPORTED);
2735}
2736
2737/**
jcarseyb1f95a02009-06-16 00:23:19 +00002738 Print at a specific location on the screen.
2739
jcarseyf1b87e72009-06-17 00:52:11 +00002740 This function will move the cursor to a given screen location and print the specified string
jcarsey1e6e84c2010-01-25 20:05:08 +00002741
2742 If -1 is specified for either the Row or Col the current screen location for BOTH
jcarseyf1b87e72009-06-17 00:52:11 +00002743 will be used.
jcarseyb1f95a02009-06-16 00:23:19 +00002744
2745 if either Row or Col is out of range for the current console, then ASSERT
2746 if Format is NULL, then ASSERT
2747
jcarsey1e6e84c2010-01-25 20:05:08 +00002748 In addition to the standard %-based flags as supported by UefiLib Print() this supports
jcarseyb1f95a02009-06-16 00:23:19 +00002749 the following additional flags:
2750 %N - Set output attribute to normal
2751 %H - Set output attribute to highlight
2752 %E - Set output attribute to error
2753 %B - Set output attribute to blue color
2754 %V - Set output attribute to green color
2755
2756 Note: The background color is controlled by the shell command cls.
2757
jcarseyb1f95a02009-06-16 00:23:19 +00002758 @param[in] Col the column to print at
jcarsey252d9452011-03-25 20:49:53 +00002759 @param[in] Row the row to print at
jcarseyb1f95a02009-06-16 00:23:19 +00002760 @param[in] Format the format string
jcarsey2247dde2009-11-09 18:08:58 +00002761 @param[in] Marker the marker for the variable argument list
jcarseyb1f95a02009-06-16 00:23:19 +00002762
jcarseya405b862010-09-14 05:18:09 +00002763 @return EFI_SUCCESS The operation was successful.
2764 @return EFI_DEVICE_ERROR The console device reported an error.
jcarseyb1f95a02009-06-16 00:23:19 +00002765**/
jcarseya405b862010-09-14 05:18:09 +00002766EFI_STATUS
jcarseyb1f95a02009-06-16 00:23:19 +00002767EFIAPI
jcarsey2247dde2009-11-09 18:08:58 +00002768InternalShellPrintWorker(
jcarseyb1f95a02009-06-16 00:23:19 +00002769 IN INT32 Col OPTIONAL,
2770 IN INT32 Row OPTIONAL,
2771 IN CONST CHAR16 *Format,
jcarsey252d9452011-03-25 20:49:53 +00002772 IN VA_LIST Marker
jcarsey1e6e84c2010-01-25 20:05:08 +00002773 )
jcarsey2247dde2009-11-09 18:08:58 +00002774{
jcarseyb1f95a02009-06-16 00:23:19 +00002775 EFI_STATUS Status;
jcarsey975136a2009-06-16 19:03:54 +00002776 CHAR16 *ResumeLocation;
2777 CHAR16 *FormatWalker;
jcarseya405b862010-09-14 05:18:09 +00002778 UINTN OriginalAttribute;
jcarsey89e85372011-04-13 23:37:50 +00002779 CHAR16 *mPostReplaceFormat;
2780 CHAR16 *mPostReplaceFormat2;
2781
2782 mPostReplaceFormat = AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize));
2783 mPostReplaceFormat2 = AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize));
jcarseya405b862010-09-14 05:18:09 +00002784
jcarseyf8d3e682011-04-19 17:54:42 +00002785 if (mPostReplaceFormat == NULL || mPostReplaceFormat2 == NULL) {
2786 SHELL_FREE_NON_NULL(mPostReplaceFormat);
2787 SHELL_FREE_NON_NULL(mPostReplaceFormat2);
2788 return (EFI_OUT_OF_RESOURCES);
2789 }
2790
jcarseya405b862010-09-14 05:18:09 +00002791 Status = EFI_SUCCESS;
2792 OriginalAttribute = gST->ConOut->Mode->Attribute;
jcarsey1e6e84c2010-01-25 20:05:08 +00002793
jcarsey975136a2009-06-16 19:03:54 +00002794 //
2795 // Back and forth each time fixing up 1 of our flags...
2796 //
jcarseya405b862010-09-14 05:18:09 +00002797 Status = ShellCopySearchAndReplace(Format, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%N", L"%%N", FALSE, FALSE);
jcarsey975136a2009-06-16 19:03:54 +00002798 ASSERT_EFI_ERROR(Status);
jcarseya405b862010-09-14 05:18:09 +00002799 Status = ShellCopySearchAndReplace(mPostReplaceFormat, mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), L"%E", L"%%E", FALSE, FALSE);
jcarsey975136a2009-06-16 19:03:54 +00002800 ASSERT_EFI_ERROR(Status);
jcarseya405b862010-09-14 05:18:09 +00002801 Status = ShellCopySearchAndReplace(mPostReplaceFormat2, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%H", L"%%H", FALSE, FALSE);
jcarsey975136a2009-06-16 19:03:54 +00002802 ASSERT_EFI_ERROR(Status);
jcarseya405b862010-09-14 05:18:09 +00002803 Status = ShellCopySearchAndReplace(mPostReplaceFormat, mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), L"%B", L"%%B", FALSE, FALSE);
jcarsey975136a2009-06-16 19:03:54 +00002804 ASSERT_EFI_ERROR(Status);
jcarseya405b862010-09-14 05:18:09 +00002805 Status = ShellCopySearchAndReplace(mPostReplaceFormat2, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%V", L"%%V", FALSE, FALSE);
jcarsey975136a2009-06-16 19:03:54 +00002806 ASSERT_EFI_ERROR(Status);
2807
2808 //
2809 // Use the last buffer from replacing to print from...
2810 //
jcarseya405b862010-09-14 05:18:09 +00002811 UnicodeVSPrint (mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), mPostReplaceFormat, Marker);
jcarseyb1f95a02009-06-16 00:23:19 +00002812
2813 if (Col != -1 && Row != -1) {
jcarseyb1f95a02009-06-16 00:23:19 +00002814 Status = gST->ConOut->SetCursorPosition(gST->ConOut, Col, Row);
jcarsey975136a2009-06-16 19:03:54 +00002815 }
2816
jcarseyecd3d592009-12-07 18:05:00 +00002817 FormatWalker = mPostReplaceFormat2;
jcarsey2247dde2009-11-09 18:08:58 +00002818 while (*FormatWalker != CHAR_NULL) {
jcarsey975136a2009-06-16 19:03:54 +00002819 //
2820 // Find the next attribute change request
2821 //
2822 ResumeLocation = StrStr(FormatWalker, L"%");
2823 if (ResumeLocation != NULL) {
jcarsey2247dde2009-11-09 18:08:58 +00002824 *ResumeLocation = CHAR_NULL;
jcarsey975136a2009-06-16 19:03:54 +00002825 }
2826 //
2827 // print the current FormatWalker string
2828 //
jcarseya405b862010-09-14 05:18:09 +00002829 if (StrLen(FormatWalker)>0) {
2830 Status = InternalPrintTo(FormatWalker);
2831 if (EFI_ERROR(Status)) {
2832 break;
2833 }
2834 }
2835
jcarsey975136a2009-06-16 19:03:54 +00002836 //
2837 // update the attribute
2838 //
2839 if (ResumeLocation != NULL) {
jcarsey5d46f172011-08-23 15:34:23 +00002840 if (*(ResumeLocation-1) == L'^') {
2841 //
jcarsey8bb74412012-01-30 18:44:41 +00002842 // Move cursor back 1 position to overwrite the ^
2843 //
2844 gST->ConOut->SetCursorPosition(gST->ConOut, gST->ConOut->Mode->CursorColumn - 1, gST->ConOut->Mode->CursorRow);
2845
2846 //
jcarsey5d46f172011-08-23 15:34:23 +00002847 // Print a simple '%' symbol
2848 //
2849 Status = InternalPrintTo(L"%");
2850 ResumeLocation = ResumeLocation - 1;
2851 } else {
2852 switch (*(ResumeLocation+1)) {
2853 case (L'N'):
2854 gST->ConOut->SetAttribute(gST->ConOut, OriginalAttribute);
jcarseya405b862010-09-14 05:18:09 +00002855 break;
jcarsey5d46f172011-08-23 15:34:23 +00002856 case (L'E'):
2857 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_YELLOW, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));
2858 break;
2859 case (L'H'):
2860 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_WHITE, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));
2861 break;
2862 case (L'B'):
2863 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_BLUE, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));
2864 break;
2865 case (L'V'):
2866 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_GREEN, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));
2867 break;
2868 default:
2869 //
2870 // Print a simple '%' symbol
2871 //
2872 Status = InternalPrintTo(L"%");
2873 if (EFI_ERROR(Status)) {
2874 break;
2875 }
2876 ResumeLocation = ResumeLocation - 1;
2877 break;
2878 }
jcarsey975136a2009-06-16 19:03:54 +00002879 }
2880 } else {
2881 //
2882 // reset to normal now...
2883 //
jcarsey975136a2009-06-16 19:03:54 +00002884 break;
2885 }
2886
2887 //
2888 // update FormatWalker to Resume + 2 (skip the % and the indicator)
2889 //
2890 FormatWalker = ResumeLocation + 2;
2891 }
jcarseyb1f95a02009-06-16 00:23:19 +00002892
jcarseya405b862010-09-14 05:18:09 +00002893 gST->ConOut->SetAttribute(gST->ConOut, OriginalAttribute);
jcarsey89e85372011-04-13 23:37:50 +00002894
2895 SHELL_FREE_NON_NULL(mPostReplaceFormat);
2896 SHELL_FREE_NON_NULL(mPostReplaceFormat2);
jcarseya405b862010-09-14 05:18:09 +00002897 return (Status);
jcarsey5f7431d2009-07-10 18:06:01 +00002898}
jcarsey2247dde2009-11-09 18:08:58 +00002899
2900/**
2901 Print at a specific location on the screen.
2902
jcarseye2f82972009-12-01 05:40:24 +00002903 This function will move the cursor to a given screen location and print the specified string.
jcarsey1e6e84c2010-01-25 20:05:08 +00002904
2905 If -1 is specified for either the Row or Col the current screen location for BOTH
jcarsey2247dde2009-11-09 18:08:58 +00002906 will be used.
2907
jcarseye2f82972009-12-01 05:40:24 +00002908 If either Row or Col is out of range for the current console, then ASSERT.
2909 If Format is NULL, then ASSERT.
jcarsey2247dde2009-11-09 18:08:58 +00002910
jcarsey1e6e84c2010-01-25 20:05:08 +00002911 In addition to the standard %-based flags as supported by UefiLib Print() this supports
jcarsey2247dde2009-11-09 18:08:58 +00002912 the following additional flags:
2913 %N - Set output attribute to normal
2914 %H - Set output attribute to highlight
2915 %E - Set output attribute to error
2916 %B - Set output attribute to blue color
2917 %V - Set output attribute to green color
2918
2919 Note: The background color is controlled by the shell command cls.
2920
jcarsey2247dde2009-11-09 18:08:58 +00002921 @param[in] Col the column to print at
jcarseya405b862010-09-14 05:18:09 +00002922 @param[in] Row the row to print at
jcarsey2247dde2009-11-09 18:08:58 +00002923 @param[in] Format the format string
jcarseya405b862010-09-14 05:18:09 +00002924 @param[in] ... The variable argument list.
jcarsey2247dde2009-11-09 18:08:58 +00002925
jcarseya405b862010-09-14 05:18:09 +00002926 @return EFI_SUCCESS The printing was successful.
2927 @return EFI_DEVICE_ERROR The console device reported an error.
jcarsey2247dde2009-11-09 18:08:58 +00002928**/
jcarseya405b862010-09-14 05:18:09 +00002929EFI_STATUS
jcarsey2247dde2009-11-09 18:08:58 +00002930EFIAPI
2931ShellPrintEx(
2932 IN INT32 Col OPTIONAL,
2933 IN INT32 Row OPTIONAL,
2934 IN CONST CHAR16 *Format,
2935 ...
jcarsey1e6e84c2010-01-25 20:05:08 +00002936 )
jcarsey2247dde2009-11-09 18:08:58 +00002937{
2938 VA_LIST Marker;
jcarseya405b862010-09-14 05:18:09 +00002939 EFI_STATUS RetVal;
jcarsey3e082d52010-10-04 16:44:57 +00002940 if (Format == NULL) {
2941 return (EFI_INVALID_PARAMETER);
2942 }
jcarsey2247dde2009-11-09 18:08:58 +00002943 VA_START (Marker, Format);
jcarseya405b862010-09-14 05:18:09 +00002944 RetVal = InternalShellPrintWorker(Col, Row, Format, Marker);
jcarseye2f82972009-12-01 05:40:24 +00002945 VA_END(Marker);
jcarseya405b862010-09-14 05:18:09 +00002946 return(RetVal);
jcarsey2247dde2009-11-09 18:08:58 +00002947}
2948
2949/**
2950 Print at a specific location on the screen.
2951
jcarseye2f82972009-12-01 05:40:24 +00002952 This function will move the cursor to a given screen location and print the specified string.
jcarsey1e6e84c2010-01-25 20:05:08 +00002953
2954 If -1 is specified for either the Row or Col the current screen location for BOTH
jcarseye2f82972009-12-01 05:40:24 +00002955 will be used.
jcarsey2247dde2009-11-09 18:08:58 +00002956
jcarseye2f82972009-12-01 05:40:24 +00002957 If either Row or Col is out of range for the current console, then ASSERT.
2958 If Format is NULL, then ASSERT.
jcarsey2247dde2009-11-09 18:08:58 +00002959
jcarsey1e6e84c2010-01-25 20:05:08 +00002960 In addition to the standard %-based flags as supported by UefiLib Print() this supports
jcarsey2247dde2009-11-09 18:08:58 +00002961 the following additional flags:
jcarsey1e6e84c2010-01-25 20:05:08 +00002962 %N - Set output attribute to normal.
2963 %H - Set output attribute to highlight.
2964 %E - Set output attribute to error.
2965 %B - Set output attribute to blue color.
2966 %V - Set output attribute to green color.
jcarsey2247dde2009-11-09 18:08:58 +00002967
2968 Note: The background color is controlled by the shell command cls.
2969
jcarsey1e6e84c2010-01-25 20:05:08 +00002970 @param[in] Col The column to print at.
jcarseya405b862010-09-14 05:18:09 +00002971 @param[in] Row The row to print at.
jcarsey1e6e84c2010-01-25 20:05:08 +00002972 @param[in] Language The language of the string to retrieve. If this parameter
2973 is NULL, then the current platform language is used.
2974 @param[in] HiiFormatStringId The format string Id for getting from Hii.
2975 @param[in] HiiFormatHandle The format string Handle for getting from Hii.
jcarseya405b862010-09-14 05:18:09 +00002976 @param[in] ... The variable argument list.
jcarsey2247dde2009-11-09 18:08:58 +00002977
jcarseya405b862010-09-14 05:18:09 +00002978 @return EFI_SUCCESS The printing was successful.
2979 @return EFI_DEVICE_ERROR The console device reported an error.
jcarsey2247dde2009-11-09 18:08:58 +00002980**/
jcarseya405b862010-09-14 05:18:09 +00002981EFI_STATUS
jcarsey2247dde2009-11-09 18:08:58 +00002982EFIAPI
2983ShellPrintHiiEx(
2984 IN INT32 Col OPTIONAL,
2985 IN INT32 Row OPTIONAL,
jcarsey1e6e84c2010-01-25 20:05:08 +00002986 IN CONST CHAR8 *Language OPTIONAL,
jcarsey2247dde2009-11-09 18:08:58 +00002987 IN CONST EFI_STRING_ID HiiFormatStringId,
2988 IN CONST EFI_HANDLE HiiFormatHandle,
2989 ...
2990 )
2991{
2992 VA_LIST Marker;
2993 CHAR16 *HiiFormatString;
jcarseya405b862010-09-14 05:18:09 +00002994 EFI_STATUS RetVal;
jcarsey2247dde2009-11-09 18:08:58 +00002995
Ruiyu Nieeb97442016-07-14 13:15:34 +08002996 RetVal = EFI_DEVICE_ERROR;
2997
jcarsey2247dde2009-11-09 18:08:58 +00002998 VA_START (Marker, HiiFormatHandle);
jcarsey1e6e84c2010-01-25 20:05:08 +00002999 HiiFormatString = HiiGetString(HiiFormatHandle, HiiFormatStringId, Language);
Ruiyu Nieeb97442016-07-14 13:15:34 +08003000 if (HiiFormatString != NULL) {
3001 RetVal = InternalShellPrintWorker (Col, Row, HiiFormatString, Marker);
3002 SHELL_FREE_NON_NULL (HiiFormatString);
3003 }
jcarseye2f82972009-12-01 05:40:24 +00003004 VA_END(Marker);
jcarsey2247dde2009-11-09 18:08:58 +00003005
3006 return (RetVal);
3007}
3008
3009/**
3010 Function to determine if a given filename represents a file or a directory.
3011
3012 @param[in] DirName Path to directory to test.
3013
jcarseyc8c22592011-10-17 17:49:21 +00003014 @retval EFI_SUCCESS The Path represents a directory
3015 @retval EFI_NOT_FOUND The Path does not represent a directory
3016 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
3017 @return The path failed to open
jcarsey2247dde2009-11-09 18:08:58 +00003018**/
3019EFI_STATUS
3020EFIAPI
3021ShellIsDirectory(
3022 IN CONST CHAR16 *DirName
3023 )
3024{
3025 EFI_STATUS Status;
jcarseya405b862010-09-14 05:18:09 +00003026 SHELL_FILE_HANDLE Handle;
jcarsey3e082d52010-10-04 16:44:57 +00003027 CHAR16 *TempLocation;
3028 CHAR16 *TempLocation2;
jcarsey2247dde2009-11-09 18:08:58 +00003029
jcarseyecd3d592009-12-07 18:05:00 +00003030 ASSERT(DirName != NULL);
3031
jcarseya405b862010-09-14 05:18:09 +00003032 Handle = NULL;
3033 TempLocation = NULL;
jcarsey2247dde2009-11-09 18:08:58 +00003034
3035 Status = ShellOpenFileByName(DirName, &Handle, EFI_FILE_MODE_READ, 0);
3036 if (EFI_ERROR(Status)) {
jcarseya405b862010-09-14 05:18:09 +00003037 //
3038 // try good logic first.
3039 //
jcarsey366f81a2011-06-27 21:04:22 +00003040 if (gEfiShellProtocol != NULL) {
jcarsey3e082d52010-10-04 16:44:57 +00003041 TempLocation = StrnCatGrow(&TempLocation, NULL, DirName, 0);
jcarseyc8c22592011-10-17 17:49:21 +00003042 if (TempLocation == NULL) {
3043 ShellCloseFile(&Handle);
3044 return (EFI_OUT_OF_RESOURCES);
3045 }
jcarsey3e082d52010-10-04 16:44:57 +00003046 TempLocation2 = StrStr(TempLocation, L":");
3047 if (TempLocation2 != NULL && StrLen(StrStr(TempLocation, L":")) == 2) {
3048 *(TempLocation2+1) = CHAR_NULL;
jcarseya405b862010-09-14 05:18:09 +00003049 }
jcarsey366f81a2011-06-27 21:04:22 +00003050 if (gEfiShellProtocol->GetDevicePathFromMap(TempLocation) != NULL) {
jcarseya405b862010-09-14 05:18:09 +00003051 FreePool(TempLocation);
3052 return (EFI_SUCCESS);
3053 }
3054 FreePool(TempLocation);
3055 } else {
3056 //
3057 // probably a map name?!?!!?
3058 //
3059 TempLocation = StrStr(DirName, L"\\");
3060 if (TempLocation != NULL && *(TempLocation+1) == CHAR_NULL) {
3061 return (EFI_SUCCESS);
3062 }
3063 }
jcarsey2247dde2009-11-09 18:08:58 +00003064 return (Status);
3065 }
3066
3067 if (FileHandleIsDirectory(Handle) == EFI_SUCCESS) {
3068 ShellCloseFile(&Handle);
3069 return (EFI_SUCCESS);
3070 }
3071 ShellCloseFile(&Handle);
3072 return (EFI_NOT_FOUND);
3073}
3074
jcarsey125c2cf2009-11-18 21:36:50 +00003075/**
jcarsey36a9d672009-11-20 21:13:41 +00003076 Function to determine if a given filename represents a file.
3077
3078 @param[in] Name Path to file to test.
3079
3080 @retval EFI_SUCCESS The Path represents a file.
3081 @retval EFI_NOT_FOUND The Path does not represent a file.
3082 @retval other The path failed to open.
3083**/
3084EFI_STATUS
3085EFIAPI
3086ShellIsFile(
3087 IN CONST CHAR16 *Name
3088 )
3089{
3090 EFI_STATUS Status;
jcarseya405b862010-09-14 05:18:09 +00003091 SHELL_FILE_HANDLE Handle;
jcarsey36a9d672009-11-20 21:13:41 +00003092
jcarseyecd3d592009-12-07 18:05:00 +00003093 ASSERT(Name != NULL);
3094
jcarsey36a9d672009-11-20 21:13:41 +00003095 Handle = NULL;
3096
3097 Status = ShellOpenFileByName(Name, &Handle, EFI_FILE_MODE_READ, 0);
3098 if (EFI_ERROR(Status)) {
3099 return (Status);
3100 }
3101
3102 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {
3103 ShellCloseFile(&Handle);
3104 return (EFI_SUCCESS);
3105 }
3106 ShellCloseFile(&Handle);
3107 return (EFI_NOT_FOUND);
3108}
3109
3110/**
jcarseyb3011f42010-01-11 21:49:04 +00003111 Function to determine if a given filename represents a file.
3112
3113 This will search the CWD and then the Path.
3114
3115 If Name is NULL, then ASSERT.
3116
3117 @param[in] Name Path to file to test.
3118
3119 @retval EFI_SUCCESS The Path represents a file.
3120 @retval EFI_NOT_FOUND The Path does not represent a file.
3121 @retval other The path failed to open.
3122**/
3123EFI_STATUS
3124EFIAPI
3125ShellIsFileInPath(
3126 IN CONST CHAR16 *Name
jcarseya405b862010-09-14 05:18:09 +00003127 )
3128{
jcarseyb3011f42010-01-11 21:49:04 +00003129 CHAR16 *NewName;
3130 EFI_STATUS Status;
3131
3132 if (!EFI_ERROR(ShellIsFile(Name))) {
jcarseya405b862010-09-14 05:18:09 +00003133 return (EFI_SUCCESS);
jcarseyb3011f42010-01-11 21:49:04 +00003134 }
3135
3136 NewName = ShellFindFilePath(Name);
3137 if (NewName == NULL) {
3138 return (EFI_NOT_FOUND);
3139 }
3140 Status = ShellIsFile(NewName);
3141 FreePool(NewName);
3142 return (Status);
3143}
jcarsey252d9452011-03-25 20:49:53 +00003144
jcarseyb3011f42010-01-11 21:49:04 +00003145/**
Jaben Carsey74b0fb82013-11-22 21:37:34 +00003146 Function return the number converted from a hex representation of a number.
3147
3148 Note: this function cannot be used when (UINTN)(-1), (0xFFFFFFFF) may be a valid
3149 result. Use ShellConvertStringToUint64 instead.
3150
3151 @param[in] String String representation of a number.
3152
3153 @return The unsigned integer result of the conversion.
3154 @retval (UINTN)(-1) An error occured.
3155**/
3156UINTN
3157EFIAPI
3158ShellHexStrToUintn(
3159 IN CONST CHAR16 *String
3160 )
3161{
3162 UINT64 RetVal;
3163
3164 if (!EFI_ERROR(ShellConvertStringToUint64(String, &RetVal, TRUE, TRUE))) {
3165 return ((UINTN)RetVal);
3166 }
3167
3168 return ((UINTN)(-1));
3169}
3170
3171/**
jcarsey1e6e84c2010-01-25 20:05:08 +00003172 Function to determine whether a string is decimal or hex representation of a number
Jaben Carseyd59fc242013-11-14 23:52:43 +00003173 and return the number converted from the string. Spaces are always skipped.
jcarsey125c2cf2009-11-18 21:36:50 +00003174
3175 @param[in] String String representation of a number
3176
jcarsey252d9452011-03-25 20:49:53 +00003177 @return the number
3178 @retval (UINTN)(-1) An error ocurred.
jcarsey125c2cf2009-11-18 21:36:50 +00003179**/
3180UINTN
3181EFIAPI
3182ShellStrToUintn(
3183 IN CONST CHAR16 *String
3184 )
3185{
jcarsey252d9452011-03-25 20:49:53 +00003186 UINT64 RetVal;
3187 BOOLEAN Hex;
3188
3189 Hex = FALSE;
3190
jcarsey658bf432014-11-04 22:33:16 +00003191 if (!InternalShellIsHexOrDecimalNumber(String, Hex, TRUE, FALSE)) {
jcarsey252d9452011-03-25 20:49:53 +00003192 Hex = TRUE;
jcarsey125c2cf2009-11-18 21:36:50 +00003193 }
jcarsey252d9452011-03-25 20:49:53 +00003194
3195 if (!EFI_ERROR(ShellConvertStringToUint64(String, &RetVal, Hex, TRUE))) {
3196 return ((UINTN)RetVal);
3197 }
3198 return ((UINTN)(-1));
jcarsey125c2cf2009-11-18 21:36:50 +00003199}
3200
3201/**
3202 Safely append with automatic string resizing given length of Destination and
3203 desired length of copy from Source.
3204
3205 append the first D characters of Source to the end of Destination, where D is
3206 the lesser of Count and the StrLen() of Source. If appending those D characters
3207 will fit within Destination (whose Size is given as CurrentSize) and
jcarsey1e6e84c2010-01-25 20:05:08 +00003208 still leave room for a NULL terminator, then those characters are appended,
3209 starting at the original terminating NULL of Destination, and a new terminating
3210 NULL is appended.
jcarsey125c2cf2009-11-18 21:36:50 +00003211
3212 If appending D characters onto Destination will result in a overflow of the size
3213 given in CurrentSize the string will be grown such that the copy can be performed
3214 and CurrentSize will be updated to the new size.
3215
3216 If Source is NULL, there is nothing to append, just return the current buffer in
3217 Destination.
3218
3219 if Destination is NULL, then ASSERT()
3220 if Destination's current length (including NULL terminator) is already more then
3221 CurrentSize, then ASSERT()
3222
ydong104ff7e372011-09-02 08:05:34 +00003223 @param[in, out] Destination The String to append onto
3224 @param[in, out] CurrentSize on call the number of bytes in Destination. On
jcarsey125c2cf2009-11-18 21:36:50 +00003225 return possibly the new size (still in bytes). if NULL
3226 then allocate whatever is needed.
3227 @param[in] Source The String to append from
3228 @param[in] Count Maximum number of characters to append. if 0 then
3229 all are appended.
3230
3231 @return Destination return the resultant string.
3232**/
3233CHAR16*
3234EFIAPI
3235StrnCatGrow (
3236 IN OUT CHAR16 **Destination,
3237 IN OUT UINTN *CurrentSize,
3238 IN CONST CHAR16 *Source,
3239 IN UINTN Count
3240 )
3241{
3242 UINTN DestinationStartSize;
3243 UINTN NewSize;
3244
3245 //
3246 // ASSERTs
3247 //
3248 ASSERT(Destination != NULL);
3249
3250 //
3251 // If there's nothing to do then just return Destination
3252 //
3253 if (Source == NULL) {
3254 return (*Destination);
3255 }
3256
3257 //
3258 // allow for un-initialized pointers, based on size being 0
3259 //
3260 if (CurrentSize != NULL && *CurrentSize == 0) {
3261 *Destination = NULL;
3262 }
3263
3264 //
3265 // allow for NULL pointers address as Destination
3266 //
3267 if (*Destination != NULL) {
3268 ASSERT(CurrentSize != 0);
3269 DestinationStartSize = StrSize(*Destination);
3270 ASSERT(DestinationStartSize <= *CurrentSize);
3271 } else {
3272 DestinationStartSize = 0;
3273// ASSERT(*CurrentSize == 0);
3274 }
3275
3276 //
3277 // Append all of Source?
3278 //
3279 if (Count == 0) {
3280 Count = StrLen(Source);
3281 }
3282
3283 //
3284 // Test and grow if required
3285 //
3286 if (CurrentSize != NULL) {
3287 NewSize = *CurrentSize;
ydong10f480fdc2012-09-07 01:55:33 +00003288 if (NewSize < DestinationStartSize + (Count * sizeof(CHAR16))) {
3289 while (NewSize < (DestinationStartSize + (Count*sizeof(CHAR16)))) {
3290 NewSize += 2 * Count * sizeof(CHAR16);
3291 }
3292 *Destination = ReallocatePool(*CurrentSize, NewSize, *Destination);
3293 *CurrentSize = NewSize;
jcarsey125c2cf2009-11-18 21:36:50 +00003294 }
jcarsey125c2cf2009-11-18 21:36:50 +00003295 } else {
Qiu Shuminc587fd32015-07-02 01:12:03 +00003296 NewSize = (Count+1)*sizeof(CHAR16);
3297 *Destination = AllocateZeroPool(NewSize);
jcarsey125c2cf2009-11-18 21:36:50 +00003298 }
3299
3300 //
3301 // Now use standard StrnCat on a big enough buffer
3302 //
jcarseyc9d92df2010-02-03 15:37:54 +00003303 if (*Destination == NULL) {
3304 return (NULL);
3305 }
Qiu Shumine75390f2015-06-30 03:18:31 +00003306
Qiu Shuminc587fd32015-07-02 01:12:03 +00003307 StrnCatS(*Destination, NewSize/sizeof(CHAR16), Source, Count);
Qiu Shumine75390f2015-06-30 03:18:31 +00003308 return *Destination;
jcarsey125c2cf2009-11-18 21:36:50 +00003309}
jcarseyc9d92df2010-02-03 15:37:54 +00003310
3311/**
3312 Prompt the user and return the resultant answer to the requestor.
3313
3314 This function will display the requested question on the shell prompt and then
Mudusuru, Giri P1d322462016-07-07 00:47:45 -07003315 wait for an appropriate answer to be input from the console.
jcarseyc9d92df2010-02-03 15:37:54 +00003316
jcarseya405b862010-09-14 05:18:09 +00003317 if the SHELL_PROMPT_REQUEST_TYPE is SHELL_PROMPT_REQUEST_TYPE_YESNO, ShellPromptResponseTypeQuitContinue
jcarseyc9d92df2010-02-03 15:37:54 +00003318 or SHELL_PROMPT_REQUEST_TYPE_YESNOCANCEL then *Response is of type SHELL_PROMPT_RESPONSE.
3319
jcarseya405b862010-09-14 05:18:09 +00003320 if the SHELL_PROMPT_REQUEST_TYPE is ShellPromptResponseTypeFreeform then *Response is of type
jcarseyc9d92df2010-02-03 15:37:54 +00003321 CHAR16*.
3322
3323 In either case *Response must be callee freed if Response was not NULL;
3324
3325 @param Type What type of question is asked. This is used to filter the input
3326 to prevent invalid answers to question.
3327 @param Prompt Pointer to string prompt to use to request input.
3328 @param Response Pointer to Response which will be populated upon return.
3329
3330 @retval EFI_SUCCESS The operation was sucessful.
3331 @retval EFI_UNSUPPORTED The operation is not supported as requested.
3332 @retval EFI_INVALID_PARAMETER A parameter was invalid.
3333 @return other The operation failed.
3334**/
3335EFI_STATUS
3336EFIAPI
3337ShellPromptForResponse (
3338 IN SHELL_PROMPT_REQUEST_TYPE Type,
3339 IN CHAR16 *Prompt OPTIONAL,
3340 IN OUT VOID **Response OPTIONAL
3341 )
3342{
3343 EFI_STATUS Status;
3344 EFI_INPUT_KEY Key;
3345 UINTN EventIndex;
3346 SHELL_PROMPT_RESPONSE *Resp;
jcarseya405b862010-09-14 05:18:09 +00003347 UINTN Size;
3348 CHAR16 *Buffer;
jcarseyc9d92df2010-02-03 15:37:54 +00003349
jcarseya405b862010-09-14 05:18:09 +00003350 Status = EFI_UNSUPPORTED;
3351 Resp = NULL;
3352 Buffer = NULL;
3353 Size = 0;
3354 if (Type != ShellPromptResponseTypeFreeform) {
jcarsey252d9452011-03-25 20:49:53 +00003355 Resp = (SHELL_PROMPT_RESPONSE*)AllocateZeroPool(sizeof(SHELL_PROMPT_RESPONSE));
jcarsey3e082d52010-10-04 16:44:57 +00003356 if (Resp == NULL) {
3357 return (EFI_OUT_OF_RESOURCES);
3358 }
xdu290bfa222010-07-19 05:21:27 +00003359 }
jcarseyc9d92df2010-02-03 15:37:54 +00003360
3361 switch(Type) {
jcarseya405b862010-09-14 05:18:09 +00003362 case ShellPromptResponseTypeQuitContinue:
jcarseyc9d92df2010-02-03 15:37:54 +00003363 if (Prompt != NULL) {
3364 ShellPrintEx(-1, -1, L"%s", Prompt);
3365 }
3366 //
3367 // wait for valid response
3368 //
3369 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
3370 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
Jaben Carsey31b018a2014-01-16 16:52:39 +00003371 if (EFI_ERROR(Status)) {
3372 break;
3373 }
jcarseyc9d92df2010-02-03 15:37:54 +00003374 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3375 if (Key.UnicodeChar == L'Q' || Key.UnicodeChar ==L'q') {
jcarseya405b862010-09-14 05:18:09 +00003376 *Resp = ShellPromptResponseQuit;
jcarseyc9d92df2010-02-03 15:37:54 +00003377 } else {
jcarseya405b862010-09-14 05:18:09 +00003378 *Resp = ShellPromptResponseContinue;
jcarseyc9d92df2010-02-03 15:37:54 +00003379 }
3380 break;
jcarseya405b862010-09-14 05:18:09 +00003381 case ShellPromptResponseTypeYesNoCancel:
jcarseyc9d92df2010-02-03 15:37:54 +00003382 if (Prompt != NULL) {
3383 ShellPrintEx(-1, -1, L"%s", Prompt);
3384 }
3385 //
3386 // wait for valid response
3387 //
jcarseya405b862010-09-14 05:18:09 +00003388 *Resp = ShellPromptResponseMax;
3389 while (*Resp == ShellPromptResponseMax) {
Jaben Carsey194ae482013-12-09 22:55:13 +00003390 if (ShellGetExecutionBreakFlag()) {
3391 Status = EFI_ABORTED;
3392 break;
3393 }
jcarseyc9d92df2010-02-03 15:37:54 +00003394 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
3395 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
Jaben Carsey31b018a2014-01-16 16:52:39 +00003396 if (EFI_ERROR(Status)) {
3397 break;
3398 }
jcarseyc9d92df2010-02-03 15:37:54 +00003399 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3400 switch (Key.UnicodeChar) {
3401 case L'Y':
3402 case L'y':
jcarseya405b862010-09-14 05:18:09 +00003403 *Resp = ShellPromptResponseYes;
jcarseyc9d92df2010-02-03 15:37:54 +00003404 break;
3405 case L'N':
3406 case L'n':
jcarseya405b862010-09-14 05:18:09 +00003407 *Resp = ShellPromptResponseNo;
jcarseyc9d92df2010-02-03 15:37:54 +00003408 break;
3409 case L'C':
3410 case L'c':
jcarseya405b862010-09-14 05:18:09 +00003411 *Resp = ShellPromptResponseCancel;
3412 break;
3413 }
3414 }
3415 break; case ShellPromptResponseTypeYesNoAllCancel:
3416 if (Prompt != NULL) {
3417 ShellPrintEx(-1, -1, L"%s", Prompt);
3418 }
3419 //
3420 // wait for valid response
3421 //
3422 *Resp = ShellPromptResponseMax;
3423 while (*Resp == ShellPromptResponseMax) {
Jaben Carsey194ae482013-12-09 22:55:13 +00003424 if (ShellGetExecutionBreakFlag()) {
3425 Status = EFI_ABORTED;
3426 break;
3427 }
jcarseya405b862010-09-14 05:18:09 +00003428 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
3429 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
Jaben Carsey31b018a2014-01-16 16:52:39 +00003430 if (EFI_ERROR(Status)) {
3431 break;
3432 }
jcarseya405b862010-09-14 05:18:09 +00003433 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3434 switch (Key.UnicodeChar) {
3435 case L'Y':
3436 case L'y':
3437 *Resp = ShellPromptResponseYes;
3438 break;
3439 case L'N':
3440 case L'n':
3441 *Resp = ShellPromptResponseNo;
3442 break;
3443 case L'A':
3444 case L'a':
3445 *Resp = ShellPromptResponseAll;
3446 break;
3447 case L'C':
3448 case L'c':
3449 *Resp = ShellPromptResponseCancel;
jcarseyc9d92df2010-02-03 15:37:54 +00003450 break;
3451 }
3452 }
3453 break;
jcarseya405b862010-09-14 05:18:09 +00003454 case ShellPromptResponseTypeEnterContinue:
3455 case ShellPromptResponseTypeAnyKeyContinue:
jcarseyc9d92df2010-02-03 15:37:54 +00003456 if (Prompt != NULL) {
3457 ShellPrintEx(-1, -1, L"%s", Prompt);
3458 }
3459 //
3460 // wait for valid response
3461 //
jcarseya405b862010-09-14 05:18:09 +00003462 *Resp = ShellPromptResponseMax;
3463 while (*Resp == ShellPromptResponseMax) {
Jaben Carsey194ae482013-12-09 22:55:13 +00003464 if (ShellGetExecutionBreakFlag()) {
3465 Status = EFI_ABORTED;
3466 break;
3467 }
jcarseyc9d92df2010-02-03 15:37:54 +00003468 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
jcarseya405b862010-09-14 05:18:09 +00003469 if (Type == ShellPromptResponseTypeEnterContinue) {
jcarseyc9d92df2010-02-03 15:37:54 +00003470 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
Jaben Carsey31b018a2014-01-16 16:52:39 +00003471 if (EFI_ERROR(Status)) {
3472 break;
3473 }
jcarseyc9d92df2010-02-03 15:37:54 +00003474 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3475 if (Key.UnicodeChar == CHAR_CARRIAGE_RETURN) {
jcarseya405b862010-09-14 05:18:09 +00003476 *Resp = ShellPromptResponseContinue;
jcarseyc9d92df2010-02-03 15:37:54 +00003477 break;
3478 }
3479 }
jcarseya405b862010-09-14 05:18:09 +00003480 if (Type == ShellPromptResponseTypeAnyKeyContinue) {
3481 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
3482 ASSERT_EFI_ERROR(Status);
3483 *Resp = ShellPromptResponseContinue;
jcarseyc9d92df2010-02-03 15:37:54 +00003484 break;
3485 }
3486 }
3487 break;
jcarseya405b862010-09-14 05:18:09 +00003488 case ShellPromptResponseTypeYesNo:
3489 if (Prompt != NULL) {
3490 ShellPrintEx(-1, -1, L"%s", Prompt);
3491 }
3492 //
3493 // wait for valid response
3494 //
3495 *Resp = ShellPromptResponseMax;
3496 while (*Resp == ShellPromptResponseMax) {
Jaben Carsey194ae482013-12-09 22:55:13 +00003497 if (ShellGetExecutionBreakFlag()) {
3498 Status = EFI_ABORTED;
3499 break;
3500 }
jcarseya405b862010-09-14 05:18:09 +00003501 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
3502 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
Jaben Carsey31b018a2014-01-16 16:52:39 +00003503 if (EFI_ERROR(Status)) {
3504 break;
3505 }
jcarseya405b862010-09-14 05:18:09 +00003506 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3507 switch (Key.UnicodeChar) {
3508 case L'Y':
3509 case L'y':
3510 *Resp = ShellPromptResponseYes;
3511 break;
3512 case L'N':
3513 case L'n':
3514 *Resp = ShellPromptResponseNo;
3515 break;
3516 }
3517 }
3518 break;
3519 case ShellPromptResponseTypeFreeform:
3520 if (Prompt != NULL) {
3521 ShellPrintEx(-1, -1, L"%s", Prompt);
3522 }
3523 while(1) {
Jaben Carsey194ae482013-12-09 22:55:13 +00003524 if (ShellGetExecutionBreakFlag()) {
3525 Status = EFI_ABORTED;
3526 break;
3527 }
jcarseya405b862010-09-14 05:18:09 +00003528 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
3529 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
Jaben Carsey31b018a2014-01-16 16:52:39 +00003530 if (EFI_ERROR(Status)) {
3531 break;
3532 }
jcarseya405b862010-09-14 05:18:09 +00003533 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3534 if (Key.UnicodeChar == CHAR_CARRIAGE_RETURN) {
3535 break;
3536 }
3537 ASSERT((Buffer == NULL && Size == 0) || (Buffer != NULL));
3538 StrnCatGrow(&Buffer, &Size, &Key.UnicodeChar, 1);
3539 }
3540 break;
3541 //
3542 // This is the location to add new prompt types.
Jaben Carsey194ae482013-12-09 22:55:13 +00003543 // If your new type loops remember to add ExecutionBreak support.
jcarseya405b862010-09-14 05:18:09 +00003544 //
jcarseyc9d92df2010-02-03 15:37:54 +00003545 default:
jcarseya405b862010-09-14 05:18:09 +00003546 ASSERT(FALSE);
jcarseyc9d92df2010-02-03 15:37:54 +00003547 }
3548
3549 if (Response != NULL) {
jcarseya405b862010-09-14 05:18:09 +00003550 if (Resp != NULL) {
3551 *Response = Resp;
3552 } else if (Buffer != NULL) {
3553 *Response = Buffer;
3554 }
jcarseyc9d92df2010-02-03 15:37:54 +00003555 } else {
jcarseya405b862010-09-14 05:18:09 +00003556 if (Resp != NULL) {
3557 FreePool(Resp);
3558 }
3559 if (Buffer != NULL) {
3560 FreePool(Buffer);
3561 }
jcarseyc9d92df2010-02-03 15:37:54 +00003562 }
3563
jcarseya405b862010-09-14 05:18:09 +00003564 ShellPrintEx(-1, -1, L"\r\n");
jcarseyc9d92df2010-02-03 15:37:54 +00003565 return (Status);
3566}
3567
3568/**
3569 Prompt the user and return the resultant answer to the requestor.
3570
3571 This function is the same as ShellPromptForResponse, except that the prompt is
3572 automatically pulled from HII.
3573
3574 @param Type What type of question is asked. This is used to filter the input
3575 to prevent invalid answers to question.
jcarseya405b862010-09-14 05:18:09 +00003576 @param[in] HiiFormatStringId The format string Id for getting from Hii.
3577 @param[in] HiiFormatHandle The format string Handle for getting from Hii.
3578 @param Response Pointer to Response which will be populated upon return.
jcarseyc9d92df2010-02-03 15:37:54 +00003579
3580 @retval EFI_SUCCESS the operation was sucessful.
3581 @return other the operation failed.
3582
3583 @sa ShellPromptForResponse
3584**/
3585EFI_STATUS
3586EFIAPI
3587ShellPromptForResponseHii (
3588 IN SHELL_PROMPT_REQUEST_TYPE Type,
3589 IN CONST EFI_STRING_ID HiiFormatStringId,
3590 IN CONST EFI_HANDLE HiiFormatHandle,
3591 IN OUT VOID **Response
3592 )
3593{
3594 CHAR16 *Prompt;
3595 EFI_STATUS Status;
3596
3597 Prompt = HiiGetString(HiiFormatHandle, HiiFormatStringId, NULL);
3598 Status = ShellPromptForResponse(Type, Prompt, Response);
3599 FreePool(Prompt);
3600 return (Status);
3601}
3602
jcarseya405b862010-09-14 05:18:09 +00003603/**
3604 Function to determin if an entire string is a valid number.
jcarseyc9d92df2010-02-03 15:37:54 +00003605
jcarseya405b862010-09-14 05:18:09 +00003606 If Hex it must be preceeded with a 0x or has ForceHex, set TRUE.
3607
3608 @param[in] String The string to evaluate.
3609 @param[in] ForceHex TRUE - always assume hex.
3610 @param[in] StopAtSpace TRUE to halt upon finding a space, FALSE to keep going.
jcarsey658bf432014-11-04 22:33:16 +00003611 @param[in] TimeNumbers TRUE to allow numbers with ":", FALSE otherwise.
jcarseya405b862010-09-14 05:18:09 +00003612
3613 @retval TRUE It is all numeric (dec/hex) characters.
3614 @retval FALSE There is a non-numeric character.
3615**/
3616BOOLEAN
3617EFIAPI
jcarsey252d9452011-03-25 20:49:53 +00003618InternalShellIsHexOrDecimalNumber (
jcarseya405b862010-09-14 05:18:09 +00003619 IN CONST CHAR16 *String,
3620 IN CONST BOOLEAN ForceHex,
jcarsey658bf432014-11-04 22:33:16 +00003621 IN CONST BOOLEAN StopAtSpace,
3622 IN CONST BOOLEAN TimeNumbers
jcarseya405b862010-09-14 05:18:09 +00003623 )
3624{
3625 BOOLEAN Hex;
3626
3627 //
3628 // chop off a single negative sign
3629 //
3630 if (String != NULL && *String == L'-') {
3631 String++;
3632 }
darylm503b0934ac2011-11-12 00:35:11 +00003633
jcarseya405b862010-09-14 05:18:09 +00003634 if (String == NULL) {
3635 return (FALSE);
3636 }
3637
3638 //
3639 // chop leading zeroes
3640 //
3641 while(String != NULL && *String == L'0'){
3642 String++;
3643 }
3644 //
3645 // allow '0x' or '0X', but not 'x' or 'X'
3646 //
3647 if (String != NULL && (*String == L'x' || *String == L'X')) {
3648 if (*(String-1) != L'0') {
3649 //
3650 // we got an x without a preceeding 0
3651 //
3652 return (FALSE);
3653 }
3654 String++;
3655 Hex = TRUE;
3656 } else if (ForceHex) {
3657 Hex = TRUE;
3658 } else {
3659 Hex = FALSE;
3660 }
3661
3662 //
3663 // loop through the remaining characters and use the lib function
3664 //
3665 for ( ; String != NULL && *String != CHAR_NULL && !(StopAtSpace && *String == L' ') ; String++){
jcarsey658bf432014-11-04 22:33:16 +00003666 if (TimeNumbers && (String[0] == L':')) {
3667 continue;
3668 }
jcarseya405b862010-09-14 05:18:09 +00003669 if (Hex) {
3670 if (!ShellIsHexaDecimalDigitCharacter(*String)) {
3671 return (FALSE);
3672 }
3673 } else {
3674 if (!ShellIsDecimalDigitCharacter(*String)) {
3675 return (FALSE);
3676 }
3677 }
3678 }
jcarsey252d9452011-03-25 20:49:53 +00003679
jcarseya405b862010-09-14 05:18:09 +00003680 return (TRUE);
3681}
3682
3683/**
3684 Function to determine if a given filename exists.
3685
3686 @param[in] Name Path to test.
3687
3688 @retval EFI_SUCCESS The Path represents a file.
3689 @retval EFI_NOT_FOUND The Path does not represent a file.
3690 @retval other The path failed to open.
3691**/
3692EFI_STATUS
3693EFIAPI
3694ShellFileExists(
3695 IN CONST CHAR16 *Name
3696 )
3697{
3698 EFI_STATUS Status;
3699 EFI_SHELL_FILE_INFO *List;
3700
3701 ASSERT(Name != NULL);
3702
3703 List = NULL;
3704 Status = ShellOpenFileMetaArg((CHAR16*)Name, EFI_FILE_MODE_READ, &List);
3705 if (EFI_ERROR(Status)) {
3706 return (Status);
3707 }
3708
3709 ShellCloseFileMetaArg(&List);
3710
3711 return (EFI_SUCCESS);
3712}
jcarsey252d9452011-03-25 20:49:53 +00003713
3714/**
darylm503b0934ac2011-11-12 00:35:11 +00003715 Convert a Unicode character to upper case only if
jcarsey252d9452011-03-25 20:49:53 +00003716 it maps to a valid small-case ASCII character.
3717
3718 This internal function only deal with Unicode character
3719 which maps to a valid small-case ASCII character, i.e.
3720 L'a' to L'z'. For other Unicode character, the input character
3721 is returned directly.
3722
3723 @param Char The character to convert.
3724
3725 @retval LowerCharacter If the Char is with range L'a' to L'z'.
3726 @retval Unchanged Otherwise.
3727
3728**/
3729CHAR16
3730EFIAPI
3731InternalShellCharToUpper (
3732 IN CHAR16 Char
3733 )
3734{
3735 if (Char >= L'a' && Char <= L'z') {
3736 return (CHAR16) (Char - (L'a' - L'A'));
3737 }
3738
3739 return Char;
3740}
3741
3742/**
3743 Convert a Unicode character to numerical value.
3744
3745 This internal function only deal with Unicode character
3746 which maps to a valid hexadecimal ASII character, i.e.
darylm503b0934ac2011-11-12 00:35:11 +00003747 L'0' to L'9', L'a' to L'f' or L'A' to L'F'. For other
jcarsey252d9452011-03-25 20:49:53 +00003748 Unicode character, the value returned does not make sense.
3749
3750 @param Char The character to convert.
3751
3752 @return The numerical value converted.
3753
3754**/
3755UINTN
3756EFIAPI
3757InternalShellHexCharToUintn (
3758 IN CHAR16 Char
3759 )
3760{
3761 if (ShellIsDecimalDigitCharacter (Char)) {
3762 return Char - L'0';
3763 }
3764
3765 return (UINTN) (10 + InternalShellCharToUpper (Char) - L'A');
3766}
3767
3768/**
3769 Convert a Null-terminated Unicode hexadecimal string to a value of type UINT64.
3770
Jaben Carsey80f3e342015-07-02 17:26:42 +00003771 This function returns a value of type UINT64 by interpreting the contents
jcarsey252d9452011-03-25 20:49:53 +00003772 of the Unicode string specified by String as a hexadecimal number.
3773 The format of the input Unicode string String is:
3774
3775 [spaces][zeros][x][hexadecimal digits].
3776
3777 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
3778 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix.
3779 If "x" appears in the input string, it must be prefixed with at least one 0.
3780 The function will ignore the pad space, which includes spaces or tab characters,
3781 before [zeros], [x] or [hexadecimal digit]. The running zero before [x] or
3782 [hexadecimal digit] will be ignored. Then, the decoding starts after [x] or the
3783 first valid hexadecimal digit. Then, the function stops at the first character that is
3784 a not a valid hexadecimal character or NULL, whichever one comes first.
3785
3786 If String has only pad spaces, then zero is returned.
3787 If String has no leading pad spaces, leading zeros or valid hexadecimal digits,
3788 then zero is returned.
3789
3790 @param[in] String A pointer to a Null-terminated Unicode string.
3791 @param[out] Value Upon a successful return the value of the conversion.
3792 @param[in] StopAtSpace FALSE to skip spaces.
3793
3794 @retval EFI_SUCCESS The conversion was successful.
3795 @retval EFI_INVALID_PARAMETER A parameter was NULL or invalid.
3796 @retval EFI_DEVICE_ERROR An overflow occured.
3797**/
3798EFI_STATUS
3799EFIAPI
3800InternalShellStrHexToUint64 (
3801 IN CONST CHAR16 *String,
3802 OUT UINT64 *Value,
3803 IN CONST BOOLEAN StopAtSpace
3804 )
3805{
3806 UINT64 Result;
3807
3808 if (String == NULL || StrSize(String) == 0 || Value == NULL) {
3809 return (EFI_INVALID_PARAMETER);
3810 }
darylm503b0934ac2011-11-12 00:35:11 +00003811
jcarsey252d9452011-03-25 20:49:53 +00003812 //
darylm503b0934ac2011-11-12 00:35:11 +00003813 // Ignore the pad spaces (space or tab)
jcarsey252d9452011-03-25 20:49:53 +00003814 //
3815 while ((*String == L' ') || (*String == L'\t')) {
3816 String++;
3817 }
3818
3819 //
3820 // Ignore leading Zeros after the spaces
3821 //
3822 while (*String == L'0') {
3823 String++;
3824 }
3825
3826 if (InternalShellCharToUpper (*String) == L'X') {
3827 if (*(String - 1) != L'0') {
3828 return 0;
3829 }
3830 //
3831 // Skip the 'X'
3832 //
3833 String++;
3834 }
3835
3836 Result = 0;
darylm503b0934ac2011-11-12 00:35:11 +00003837
jcarsey252d9452011-03-25 20:49:53 +00003838 //
Jaben Carsey80f3e342015-07-02 17:26:42 +00003839 // there is a space where there should't be
jcarsey252d9452011-03-25 20:49:53 +00003840 //
Jaben Carsey80f3e342015-07-02 17:26:42 +00003841 if (*String == L' ') {
3842 return (EFI_INVALID_PARAMETER);
jcarsey252d9452011-03-25 20:49:53 +00003843 }
darylm503b0934ac2011-11-12 00:35:11 +00003844
jcarsey252d9452011-03-25 20:49:53 +00003845 while (ShellIsHexaDecimalDigitCharacter (*String)) {
3846 //
darylm503b0934ac2011-11-12 00:35:11 +00003847 // If the Hex Number represented by String overflows according
Jaben Carsey80f3e342015-07-02 17:26:42 +00003848 // to the range defined by UINT64, then return EFI_DEVICE_ERROR.
jcarsey252d9452011-03-25 20:49:53 +00003849 //
3850 if (!(Result <= (RShiftU64((((UINT64) ~0) - InternalShellHexCharToUintn (*String)), 4)))) {
3851// if (!(Result <= ((((UINT64) ~0) - InternalShellHexCharToUintn (*String)) >> 4))) {
3852 return (EFI_DEVICE_ERROR);
3853 }
3854
3855 Result = (LShiftU64(Result, 4));
3856 Result += InternalShellHexCharToUintn (*String);
3857 String++;
3858
3859 //
jcarsey49bd4982012-01-27 18:42:43 +00003860 // stop at spaces if requested
jcarsey252d9452011-03-25 20:49:53 +00003861 //
jcarsey49bd4982012-01-27 18:42:43 +00003862 if (StopAtSpace && *String == L' ') {
3863 break;
jcarsey252d9452011-03-25 20:49:53 +00003864 }
3865 }
3866
3867 *Value = Result;
3868 return (EFI_SUCCESS);
3869}
3870
3871/**
3872 Convert a Null-terminated Unicode decimal string to a value of
3873 type UINT64.
3874
3875 This function returns a value of type UINT64 by interpreting the contents
3876 of the Unicode string specified by String as a decimal number. The format
3877 of the input Unicode string String is:
3878
3879 [spaces] [decimal digits].
3880
3881 The valid decimal digit character is in the range [0-9]. The
3882 function will ignore the pad space, which includes spaces or
3883 tab characters, before [decimal digits]. The running zero in the
3884 beginning of [decimal digits] will be ignored. Then, the function
3885 stops at the first character that is a not a valid decimal character
3886 or a Null-terminator, whichever one comes first.
3887
3888 If String has only pad spaces, then 0 is returned.
3889 If String has no pad spaces or valid decimal digits,
3890 then 0 is returned.
3891
3892 @param[in] String A pointer to a Null-terminated Unicode string.
3893 @param[out] Value Upon a successful return the value of the conversion.
3894 @param[in] StopAtSpace FALSE to skip spaces.
3895
3896 @retval EFI_SUCCESS The conversion was successful.
3897 @retval EFI_INVALID_PARAMETER A parameter was NULL or invalid.
3898 @retval EFI_DEVICE_ERROR An overflow occured.
3899**/
3900EFI_STATUS
3901EFIAPI
3902InternalShellStrDecimalToUint64 (
3903 IN CONST CHAR16 *String,
3904 OUT UINT64 *Value,
3905 IN CONST BOOLEAN StopAtSpace
3906 )
3907{
3908 UINT64 Result;
3909
3910 if (String == NULL || StrSize (String) == 0 || Value == NULL) {
3911 return (EFI_INVALID_PARAMETER);
3912 }
3913
3914 //
3915 // Ignore the pad spaces (space or tab)
3916 //
3917 while ((*String == L' ') || (*String == L'\t')) {
3918 String++;
3919 }
3920
3921 //
3922 // Ignore leading Zeros after the spaces
3923 //
3924 while (*String == L'0') {
3925 String++;
3926 }
3927
3928 Result = 0;
3929
3930 //
Jaben Carsey80f3e342015-07-02 17:26:42 +00003931 // Stop upon space if requested
3932 // (if the whole value was 0)
jcarsey252d9452011-03-25 20:49:53 +00003933 //
Jaben Carsey80f3e342015-07-02 17:26:42 +00003934 if (StopAtSpace && *String == L' ') {
3935 *Value = Result;
3936 return (EFI_SUCCESS);
jcarsey252d9452011-03-25 20:49:53 +00003937 }
Jaben Carsey80f3e342015-07-02 17:26:42 +00003938
jcarsey252d9452011-03-25 20:49:53 +00003939 while (ShellIsDecimalDigitCharacter (*String)) {
3940 //
darylm503b0934ac2011-11-12 00:35:11 +00003941 // If the number represented by String overflows according
Jaben Carsey80f3e342015-07-02 17:26:42 +00003942 // to the range defined by UINT64, then return EFI_DEVICE_ERROR.
jcarsey252d9452011-03-25 20:49:53 +00003943 //
darylm503b0934ac2011-11-12 00:35:11 +00003944
jcarsey252d9452011-03-25 20:49:53 +00003945 if (!(Result <= (DivU64x32((((UINT64) ~0) - (*String - L'0')),10)))) {
3946 return (EFI_DEVICE_ERROR);
3947 }
3948
3949 Result = MultU64x32(Result, 10) + (*String - L'0');
3950 String++;
3951
3952 //
3953 // Stop at spaces if requested
3954 //
3955 if (StopAtSpace && *String == L' ') {
3956 break;
3957 }
3958 }
3959
3960 *Value = Result;
darylm503b0934ac2011-11-12 00:35:11 +00003961
jcarsey252d9452011-03-25 20:49:53 +00003962 return (EFI_SUCCESS);
3963}
3964
3965/**
3966 Function to verify and convert a string to its numerical value.
3967
3968 If Hex it must be preceeded with a 0x, 0X, or has ForceHex set TRUE.
3969
3970 @param[in] String The string to evaluate.
3971 @param[out] Value Upon a successful return the value of the conversion.
3972 @param[in] ForceHex TRUE - always assume hex.
3973 @param[in] StopAtSpace FALSE to skip spaces.
darylm503b0934ac2011-11-12 00:35:11 +00003974
jcarsey252d9452011-03-25 20:49:53 +00003975 @retval EFI_SUCCESS The conversion was successful.
3976 @retval EFI_INVALID_PARAMETER String contained an invalid character.
3977 @retval EFI_NOT_FOUND String was a number, but Value was NULL.
3978**/
3979EFI_STATUS
3980EFIAPI
3981ShellConvertStringToUint64(
3982 IN CONST CHAR16 *String,
3983 OUT UINT64 *Value,
3984 IN CONST BOOLEAN ForceHex,
3985 IN CONST BOOLEAN StopAtSpace
3986 )
3987{
3988 UINT64 RetVal;
3989 CONST CHAR16 *Walker;
3990 EFI_STATUS Status;
3991 BOOLEAN Hex;
3992
3993 Hex = ForceHex;
3994
jcarsey658bf432014-11-04 22:33:16 +00003995 if (!InternalShellIsHexOrDecimalNumber(String, Hex, StopAtSpace, FALSE)) {
jcarsey252d9452011-03-25 20:49:53 +00003996 if (!Hex) {
3997 Hex = TRUE;
jcarsey658bf432014-11-04 22:33:16 +00003998 if (!InternalShellIsHexOrDecimalNumber(String, Hex, StopAtSpace, FALSE)) {
jcarsey252d9452011-03-25 20:49:53 +00003999 return (EFI_INVALID_PARAMETER);
4000 }
4001 } else {
4002 return (EFI_INVALID_PARAMETER);
4003 }
4004 }
4005
4006 //
4007 // Chop off leading spaces
4008 //
4009 for (Walker = String; Walker != NULL && *Walker != CHAR_NULL && *Walker == L' '; Walker++);
4010
4011 //
4012 // make sure we have something left that is numeric.
4013 //
jcarsey658bf432014-11-04 22:33:16 +00004014 if (Walker == NULL || *Walker == CHAR_NULL || !InternalShellIsHexOrDecimalNumber(Walker, Hex, StopAtSpace, FALSE)) {
jcarsey252d9452011-03-25 20:49:53 +00004015 return (EFI_INVALID_PARAMETER);
darylm503b0934ac2011-11-12 00:35:11 +00004016 }
jcarsey252d9452011-03-25 20:49:53 +00004017
4018 //
4019 // do the conversion.
4020 //
4021 if (Hex || StrnCmp(Walker, L"0x", 2) == 0 || StrnCmp(Walker, L"0X", 2) == 0){
4022 Status = InternalShellStrHexToUint64(Walker, &RetVal, StopAtSpace);
4023 } else {
4024 Status = InternalShellStrDecimalToUint64(Walker, &RetVal, StopAtSpace);
4025 }
4026
4027 if (Value == NULL && !EFI_ERROR(Status)) {
4028 return (EFI_NOT_FOUND);
4029 }
4030
4031 if (Value != NULL) {
4032 *Value = RetVal;
4033 }
4034
4035 return (Status);
4036}
4037
4038/**
4039 Function to determin if an entire string is a valid number.
4040
4041 If Hex it must be preceeded with a 0x or has ForceHex, set TRUE.
4042
4043 @param[in] String The string to evaluate.
4044 @param[in] ForceHex TRUE - always assume hex.
4045 @param[in] StopAtSpace TRUE to halt upon finding a space, FALSE to keep going.
4046
4047 @retval TRUE It is all numeric (dec/hex) characters.
4048 @retval FALSE There is a non-numeric character.
4049**/
4050BOOLEAN
4051EFIAPI
4052ShellIsHexOrDecimalNumber (
4053 IN CONST CHAR16 *String,
4054 IN CONST BOOLEAN ForceHex,
4055 IN CONST BOOLEAN StopAtSpace
4056 )
4057{
4058 if (ShellConvertStringToUint64(String, NULL, ForceHex, StopAtSpace) == EFI_NOT_FOUND) {
4059 return (TRUE);
4060 }
4061 return (FALSE);
4062}
jcarsey4d0a4fc2011-07-06 22:28:36 +00004063
4064/**
4065 Function to read a single line from a SHELL_FILE_HANDLE. The \n is not included in the returned
4066 buffer. The returned buffer must be callee freed.
4067
4068 If the position upon start is 0, then the Ascii Boolean will be set. This should be
4069 maintained and not changed for all operations with the same file.
4070
ydong104ff7e372011-09-02 08:05:34 +00004071 @param[in] Handle SHELL_FILE_HANDLE to read from.
4072 @param[in, out] Ascii Boolean value for indicating whether the file is
4073 Ascii (TRUE) or UCS2 (FALSE).
jcarsey4d0a4fc2011-07-06 22:28:36 +00004074
jcarseybeab0fc2011-10-10 17:26:25 +00004075 @return The line of text from the file.
4076 @retval NULL There was not enough memory available.
jcarsey4d0a4fc2011-07-06 22:28:36 +00004077
4078 @sa ShellFileHandleReadLine
4079**/
4080CHAR16*
4081EFIAPI
4082ShellFileHandleReturnLine(
4083 IN SHELL_FILE_HANDLE Handle,
4084 IN OUT BOOLEAN *Ascii
4085 )
4086{
4087 CHAR16 *RetVal;
4088 UINTN Size;
4089 EFI_STATUS Status;
4090
4091 Size = 0;
4092 RetVal = NULL;
4093
4094 Status = ShellFileHandleReadLine(Handle, RetVal, &Size, FALSE, Ascii);
4095 if (Status == EFI_BUFFER_TOO_SMALL) {
4096 RetVal = AllocateZeroPool(Size);
jcarseybeab0fc2011-10-10 17:26:25 +00004097 if (RetVal == NULL) {
4098 return (NULL);
4099 }
jcarsey4d0a4fc2011-07-06 22:28:36 +00004100 Status = ShellFileHandleReadLine(Handle, RetVal, &Size, FALSE, Ascii);
darylm503b0934ac2011-11-12 00:35:11 +00004101
jcarsey4d0a4fc2011-07-06 22:28:36 +00004102 }
Qiu Shumine3b76f32016-02-22 10:21:38 +08004103 if (Status == EFI_END_OF_FILE && RetVal != NULL && *RetVal != CHAR_NULL) {
Qiu Shuminf79d7b62016-02-18 13:12:58 +08004104 Status = EFI_SUCCESS;
4105 }
jcarsey4d0a4fc2011-07-06 22:28:36 +00004106 if (EFI_ERROR(Status) && (RetVal != NULL)) {
4107 FreePool(RetVal);
4108 RetVal = NULL;
4109 }
4110 return (RetVal);
4111}
4112
4113/**
4114 Function to read a single line (up to but not including the \n) from a SHELL_FILE_HANDLE.
4115
4116 If the position upon start is 0, then the Ascii Boolean will be set. This should be
4117 maintained and not changed for all operations with the same file.
4118
Jim Dailey2dda8a12016-02-10 08:53:00 -08004119 NOTE: LINES THAT ARE RETURNED BY THIS FUNCTION ARE UCS2, EVEN IF THE FILE BEING READ
4120 IS IN ASCII FORMAT.
4121
ydong104ff7e372011-09-02 08:05:34 +00004122 @param[in] Handle SHELL_FILE_HANDLE to read from.
Jim Dailey2dda8a12016-02-10 08:53:00 -08004123 @param[in, out] Buffer The pointer to buffer to read into. If this function
4124 returns EFI_SUCCESS, then on output Buffer will
4125 contain a UCS2 string, even if the file being
4126 read is ASCII.
4127 @param[in, out] Size On input, pointer to number of bytes in Buffer.
4128 On output, unchanged unless Buffer is too small
4129 to contain the next line of the file. In that
4130 case Size is set to the number of bytes needed
4131 to hold the next line of the file (as a UCS2
4132 string, even if it is an ASCII file).
ydong104ff7e372011-09-02 08:05:34 +00004133 @param[in] Truncate If the buffer is large enough, this has no effect.
4134 If the buffer is is too small and Truncate is TRUE,
4135 the line will be truncated.
4136 If the buffer is is too small and Truncate is FALSE,
4137 then no read will occur.
jcarsey4d0a4fc2011-07-06 22:28:36 +00004138
ydong104ff7e372011-09-02 08:05:34 +00004139 @param[in, out] Ascii Boolean value for indicating whether the file is
4140 Ascii (TRUE) or UCS2 (FALSE).
jcarsey4d0a4fc2011-07-06 22:28:36 +00004141
4142 @retval EFI_SUCCESS The operation was successful. The line is stored in
4143 Buffer.
jaben carsey9ed21942016-02-08 15:59:04 -08004144 @retval EFI_END_OF_FILE There are no more lines in the file.
jcarsey4d0a4fc2011-07-06 22:28:36 +00004145 @retval EFI_INVALID_PARAMETER Handle was NULL.
4146 @retval EFI_INVALID_PARAMETER Size was NULL.
4147 @retval EFI_BUFFER_TOO_SMALL Size was not large enough to store the line.
4148 Size was updated to the minimum space required.
4149**/
4150EFI_STATUS
4151EFIAPI
4152ShellFileHandleReadLine(
4153 IN SHELL_FILE_HANDLE Handle,
4154 IN OUT CHAR16 *Buffer,
4155 IN OUT UINTN *Size,
4156 IN BOOLEAN Truncate,
4157 IN OUT BOOLEAN *Ascii
4158 )
4159{
4160 EFI_STATUS Status;
4161 CHAR16 CharBuffer;
4162 UINTN CharSize;
4163 UINTN CountSoFar;
4164 UINT64 OriginalFilePosition;
4165
4166
4167 if (Handle == NULL
4168 ||Size == NULL
4169 ){
4170 return (EFI_INVALID_PARAMETER);
4171 }
4172 if (Buffer == NULL) {
4173 ASSERT(*Size == 0);
4174 } else {
4175 *Buffer = CHAR_NULL;
4176 }
4177 gEfiShellProtocol->GetFilePosition(Handle, &OriginalFilePosition);
4178 if (OriginalFilePosition == 0) {
4179 CharSize = sizeof(CHAR16);
4180 Status = gEfiShellProtocol->ReadFile(Handle, &CharSize, &CharBuffer);
4181 ASSERT_EFI_ERROR(Status);
4182 if (CharBuffer == gUnicodeFileTag) {
4183 *Ascii = FALSE;
4184 } else {
4185 *Ascii = TRUE;
4186 gEfiShellProtocol->SetFilePosition(Handle, OriginalFilePosition);
4187 }
4188 }
4189
jaben carsey9ed21942016-02-08 15:59:04 -08004190 if (*Ascii) {
4191 CharSize = sizeof(CHAR8);
4192 } else {
4193 CharSize = sizeof(CHAR16);
4194 }
jcarsey4d0a4fc2011-07-06 22:28:36 +00004195 for (CountSoFar = 0;;CountSoFar++){
4196 CharBuffer = 0;
jcarsey4d0a4fc2011-07-06 22:28:36 +00004197 Status = gEfiShellProtocol->ReadFile(Handle, &CharSize, &CharBuffer);
4198 if ( EFI_ERROR(Status)
4199 || CharSize == 0
4200 || (CharBuffer == L'\n' && !(*Ascii))
4201 || (CharBuffer == '\n' && *Ascii)
4202 ){
jaben carsey9ed21942016-02-08 15:59:04 -08004203 if (CharSize == 0) {
4204 Status = EFI_END_OF_FILE;
4205 }
jcarsey4d0a4fc2011-07-06 22:28:36 +00004206 break;
4207 }
4208 //
4209 // if we have space save it...
4210 //
Jim Dailey1095b432016-02-10 13:17:56 -08004211 if ((CountSoFar+1)*sizeof(CHAR16) < *Size){
jcarsey4d0a4fc2011-07-06 22:28:36 +00004212 ASSERT(Buffer != NULL);
Jim Dailey1095b432016-02-10 13:17:56 -08004213 ((CHAR16*)Buffer)[CountSoFar] = CharBuffer;
4214 ((CHAR16*)Buffer)[CountSoFar+1] = CHAR_NULL;
jcarsey4d0a4fc2011-07-06 22:28:36 +00004215 }
4216 }
4217
4218 //
4219 // if we ran out of space tell when...
4220 //
Jim Dailey1095b432016-02-10 13:17:56 -08004221 if ((CountSoFar+1)*sizeof(CHAR16) > *Size){
4222 *Size = (CountSoFar+1)*sizeof(CHAR16);
4223 if (!Truncate) {
4224 gEfiShellProtocol->SetFilePosition(Handle, OriginalFilePosition);
4225 } else {
4226 DEBUG((DEBUG_WARN, "The line was truncated in ShellFileHandleReadLine"));
jcarsey4d0a4fc2011-07-06 22:28:36 +00004227 }
Jim Dailey1095b432016-02-10 13:17:56 -08004228 return (EFI_BUFFER_TOO_SMALL);
4229 }
4230 while(Buffer[StrLen(Buffer)-1] == L'\r') {
4231 Buffer[StrLen(Buffer)-1] = CHAR_NULL;
jcarsey4d0a4fc2011-07-06 22:28:36 +00004232 }
4233
4234 return (Status);
4235}
jcarseyfb5278e2013-02-20 18:21:14 +00004236
4237/**
jcarsey365aa982013-03-04 21:54:02 +00004238 Function to print help file / man page content in the spec from the UEFI Shell protocol GetHelpText function.
4239
4240 @param[in] CommandToGetHelpOn Pointer to a string containing the command name of help file to be printed.
4241 @param[in] SectionToGetHelpOn Pointer to the section specifier(s).
4242 @param[in] PrintCommandText If TRUE, prints the command followed by the help content, otherwise prints
4243 the help content only.
4244 @retval EFI_DEVICE_ERROR The help data format was incorrect.
4245 @retval EFI_NOT_FOUND The help data could not be found.
4246 @retval EFI_SUCCESS The operation was successful.
4247**/
4248EFI_STATUS
4249EFIAPI
4250ShellPrintHelp (
4251 IN CONST CHAR16 *CommandToGetHelpOn,
4252 IN CONST CHAR16 *SectionToGetHelpOn,
4253 IN BOOLEAN PrintCommandText
4254 )
4255{
4256 EFI_STATUS Status;
4257 CHAR16 *OutText;
4258
4259 OutText = NULL;
4260
4261 //
4262 // Get the string to print based
4263 //
4264 Status = gEfiShellProtocol->GetHelpText (CommandToGetHelpOn, SectionToGetHelpOn, &OutText);
4265
4266 //
4267 // make sure we got a valid string
4268 //
4269 if (EFI_ERROR(Status)){
4270 return Status;
4271 }
4272 if (OutText == NULL || StrLen(OutText) == 0) {
4273 return EFI_NOT_FOUND;
4274 }
4275
4276 //
4277 // Chop off trailing stuff we dont need
4278 //
4279 while (OutText[StrLen(OutText)-1] == L'\r' || OutText[StrLen(OutText)-1] == L'\n' || OutText[StrLen(OutText)-1] == L' ') {
4280 OutText[StrLen(OutText)-1] = CHAR_NULL;
4281 }
4282
4283 //
4284 // Print this out to the console
4285 //
4286 if (PrintCommandText) {
4287 ShellPrintEx(-1, -1, L"%H%-14s%N- %s\r\n", CommandToGetHelpOn, OutText);
4288 } else {
4289 ShellPrintEx(-1, -1, L"%N%s\r\n", OutText);
4290 }
4291
4292 SHELL_FREE_NON_NULL(OutText);
4293
4294 return EFI_SUCCESS;
4295}
4296
4297/**
jcarseyfb5278e2013-02-20 18:21:14 +00004298 Function to delete a file by name
4299
4300 @param[in] FileName Pointer to file name to delete.
4301
4302 @retval EFI_SUCCESS the file was deleted sucessfully
4303 @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not
4304 deleted
4305 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
4306 @retval EFI_NOT_FOUND The specified file could not be found on the
4307 device or the file system could not be found
4308 on the device.
4309 @retval EFI_NO_MEDIA The device has no medium.
4310 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
4311 medium is no longer supported.
4312 @retval EFI_DEVICE_ERROR The device reported an error.
4313 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
4314 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
4315 @retval EFI_ACCESS_DENIED The file was opened read only.
4316 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
4317 file.
4318 @retval other The file failed to open
4319**/
4320EFI_STATUS
4321EFIAPI
4322ShellDeleteFileByName(
4323 IN CONST CHAR16 *FileName
4324 )
4325{
4326 EFI_STATUS Status;
4327 SHELL_FILE_HANDLE FileHandle;
4328
4329 Status = ShellFileExists(FileName);
4330
4331 if (Status == EFI_SUCCESS){
4332 Status = ShellOpenFileByName(FileName, &FileHandle, EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE, 0x0);
4333 if (Status == EFI_SUCCESS){
4334 Status = ShellDeleteFile(&FileHandle);
4335 }
4336 }
4337
4338 return(Status);
4339
4340}
Qiu Shumin0960ba12014-09-17 07:58:31 +00004341
4342/**
4343 Cleans off all the quotes in the string.
4344
4345 @param[in] OriginalString pointer to the string to be cleaned.
4346 @param[out] CleanString The new string with all quotes removed.
4347 Memory allocated in the function and free
4348 by caller.
4349
4350 @retval EFI_SUCCESS The operation was successful.
4351**/
4352EFI_STATUS
4353EFIAPI
4354InternalShellStripQuotes (
4355 IN CONST CHAR16 *OriginalString,
4356 OUT CHAR16 **CleanString
4357 )
4358{
4359 CHAR16 *Walker;
4360
4361 if (OriginalString == NULL || CleanString == NULL) {
4362 return EFI_INVALID_PARAMETER;
4363 }
4364
4365 *CleanString = AllocateCopyPool (StrSize (OriginalString), OriginalString);
4366 if (*CleanString == NULL) {
4367 return EFI_OUT_OF_RESOURCES;
4368 }
4369
4370 for (Walker = *CleanString; Walker != NULL && *Walker != CHAR_NULL ; Walker++) {
4371 if (*Walker == L'\"') {
4372 CopyMem(Walker, Walker+1, StrSize(Walker) - sizeof(Walker[0]));
4373 }
4374 }
4375
4376 return EFI_SUCCESS;
4377}
4378