blob: e47d5350e0eea8db2b98826f1250a79389e06cbb [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{
Tapan Shah583448b2016-09-22 12:12:47 -0700295 EFI_STATUS Status;
296
jcarseyd2b45642009-05-11 18:02:16 +0000297 mEfiShellEnvironment2 = NULL;
jcarsey366f81a2011-06-27 21:04:22 +0000298 gEfiShellProtocol = NULL;
299 gEfiShellParametersProtocol = NULL;
jcarseyd2b45642009-05-11 18:02:16 +0000300 mEfiShellInterface = NULL;
301 mEfiShellEnvironment2Handle = NULL;
302
Tapan Shah583448b2016-09-22 12:12:47 -0700303 if (mUnicodeCollationProtocol == NULL) {
304 Status = gBS->LocateProtocol (&gEfiUnicodeCollation2ProtocolGuid, NULL, (VOID**)&mUnicodeCollationProtocol);
305 ASSERT_EFI_ERROR (Status);
306 }
307
jcarseyd2b45642009-05-11 18:02:16 +0000308 //
309 // verify that auto initialize is not set false
jcarsey1e6e84c2010-01-25 20:05:08 +0000310 //
jcarseyd2b45642009-05-11 18:02:16 +0000311 if (PcdGetBool(PcdShellLibAutoInitialize) == 0) {
312 return (EFI_SUCCESS);
313 }
jcarsey1e6e84c2010-01-25 20:05:08 +0000314
jcarseyd2b45642009-05-11 18:02:16 +0000315 return (ShellLibConstructorWorker(ImageHandle, SystemTable));
316}
jcarsey94b17fa2009-05-07 18:46:18 +0000317
318/**
jcarseya405b862010-09-14 05:18:09 +0000319 Destructor for the library. free any resources.
320
321 @param[in] ImageHandle A copy of the ImageHandle.
322 @param[in] SystemTable A pointer to the SystemTable for the application.
323
324 @retval EFI_SUCCESS The operation was successful.
325 @return An error from the CloseProtocol function.
jcarsey94b17fa2009-05-07 18:46:18 +0000326**/
327EFI_STATUS
328EFIAPI
329ShellLibDestructor (
330 IN EFI_HANDLE ImageHandle,
331 IN EFI_SYSTEM_TABLE *SystemTable
jcarseya405b862010-09-14 05:18:09 +0000332 )
333{
jcarsey94b17fa2009-05-07 18:46:18 +0000334 if (mEfiShellEnvironment2 != NULL) {
335 gBS->CloseProtocol(mEfiShellEnvironment2Handle==NULL?ImageHandle:mEfiShellEnvironment2Handle,
336 &gEfiShellEnvironment2Guid,
337 ImageHandle,
338 NULL);
jcarseyd2b45642009-05-11 18:02:16 +0000339 mEfiShellEnvironment2 = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +0000340 }
341 if (mEfiShellInterface != NULL) {
342 gBS->CloseProtocol(ImageHandle,
343 &gEfiShellInterfaceGuid,
344 ImageHandle,
jcarsey1e6e84c2010-01-25 20:05:08 +0000345 NULL);
jcarseyd2b45642009-05-11 18:02:16 +0000346 mEfiShellInterface = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +0000347 }
jcarsey366f81a2011-06-27 21:04:22 +0000348 if (gEfiShellProtocol != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +0000349 gBS->CloseProtocol(ImageHandle,
350 &gEfiShellProtocolGuid,
351 ImageHandle,
jcarsey1e6e84c2010-01-25 20:05:08 +0000352 NULL);
jcarsey366f81a2011-06-27 21:04:22 +0000353 gEfiShellProtocol = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +0000354 }
jcarsey366f81a2011-06-27 21:04:22 +0000355 if (gEfiShellParametersProtocol != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +0000356 gBS->CloseProtocol(ImageHandle,
357 &gEfiShellParametersProtocolGuid,
358 ImageHandle,
jcarsey1e6e84c2010-01-25 20:05:08 +0000359 NULL);
jcarsey366f81a2011-06-27 21:04:22 +0000360 gEfiShellParametersProtocol = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +0000361 }
jcarseyd2b45642009-05-11 18:02:16 +0000362 mEfiShellEnvironment2Handle = NULL;
jcarseyecd3d592009-12-07 18:05:00 +0000363
jcarsey94b17fa2009-05-07 18:46:18 +0000364 return (EFI_SUCCESS);
365}
jcarseyd2b45642009-05-11 18:02:16 +0000366
367/**
368 This function causes the shell library to initialize itself. If the shell library
369 is already initialized it will de-initialize all the current protocol poitners and
370 re-populate them again.
371
372 When the library is used with PcdShellLibAutoInitialize set to true this function
373 will return EFI_SUCCESS and perform no actions.
374
375 This function is intended for internal access for shell commands only.
376
377 @retval EFI_SUCCESS the initialization was complete sucessfully
378
379**/
380EFI_STATUS
381EFIAPI
382ShellInitialize (
jcarseya405b862010-09-14 05:18:09 +0000383 )
384{
Laszlo Ersek4a7518d2016-06-28 13:52:11 +0200385 EFI_STATUS Status;
386
jcarseyd2b45642009-05-11 18:02:16 +0000387 //
388 // if auto initialize is not false then skip
389 //
390 if (PcdGetBool(PcdShellLibAutoInitialize) != 0) {
391 return (EFI_SUCCESS);
392 }
393
394 //
395 // deinit the current stuff
396 //
Laszlo Ersek4a7518d2016-06-28 13:52:11 +0200397 Status = ShellLibDestructor (gImageHandle, gST);
398 ASSERT_EFI_ERROR (Status);
jcarseyd2b45642009-05-11 18:02:16 +0000399
400 //
401 // init the new stuff
402 //
403 return (ShellLibConstructorWorker(gImageHandle, gST));
404}
405
jcarsey94b17fa2009-05-07 18:46:18 +0000406/**
jcarsey1e6e84c2010-01-25 20:05:08 +0000407 This function will retrieve the information about the file for the handle
jcarsey94b17fa2009-05-07 18:46:18 +0000408 specified and store it in allocated pool memory.
409
jcarsey1e6e84c2010-01-25 20:05:08 +0000410 This function allocates a buffer to store the file's information. It is the
qhuang869817bf2009-05-20 14:42:48 +0000411 caller's responsibility to free the buffer
jcarsey94b17fa2009-05-07 18:46:18 +0000412
jcarsey1e6e84c2010-01-25 20:05:08 +0000413 @param FileHandle The file handle of the file for which information is
jcarsey94b17fa2009-05-07 18:46:18 +0000414 being requested.
415
416 @retval NULL information could not be retrieved.
417
418 @return the information about the file
419**/
420EFI_FILE_INFO*
421EFIAPI
422ShellGetFileInfo (
jcarseya405b862010-09-14 05:18:09 +0000423 IN SHELL_FILE_HANDLE FileHandle
424 )
425{
jcarseyd2b45642009-05-11 18:02:16 +0000426 return (FileFunctionMap.GetFileInfo(FileHandle));
jcarsey94b17fa2009-05-07 18:46:18 +0000427}
428
429/**
jcarseya405b862010-09-14 05:18:09 +0000430 This function sets the information about the file for the opened handle
jcarsey94b17fa2009-05-07 18:46:18 +0000431 specified.
432
jcarseya405b862010-09-14 05:18:09 +0000433 @param[in] FileHandle The file handle of the file for which information
434 is being set.
jcarsey94b17fa2009-05-07 18:46:18 +0000435
jcarseya405b862010-09-14 05:18:09 +0000436 @param[in] FileInfo The information to set.
jcarsey94b17fa2009-05-07 18:46:18 +0000437
darylm503b0934ac2011-11-12 00:35:11 +0000438 @retval EFI_SUCCESS The information was set.
jcarseya405b862010-09-14 05:18:09 +0000439 @retval EFI_INVALID_PARAMETER A parameter was out of range or invalid.
440 @retval EFI_UNSUPPORTED The FileHandle does not support FileInfo.
darylm503b0934ac2011-11-12 00:35:11 +0000441 @retval EFI_NO_MEDIA The device has no medium.
442 @retval EFI_DEVICE_ERROR The device reported an error.
443 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
444 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
jcarseya405b862010-09-14 05:18:09 +0000445 @retval EFI_ACCESS_DENIED The file was opened read only.
446 @retval EFI_VOLUME_FULL The volume is full.
jcarsey94b17fa2009-05-07 18:46:18 +0000447**/
448EFI_STATUS
449EFIAPI
450ShellSetFileInfo (
darylm503b0934ac2011-11-12 00:35:11 +0000451 IN SHELL_FILE_HANDLE FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000452 IN EFI_FILE_INFO *FileInfo
jcarseya405b862010-09-14 05:18:09 +0000453 )
454{
jcarseyd2b45642009-05-11 18:02:16 +0000455 return (FileFunctionMap.SetFileInfo(FileHandle, FileInfo));
jcarsey1e6e84c2010-01-25 20:05:08 +0000456}
457
jcarsey94b17fa2009-05-07 18:46:18 +0000458 /**
459 This function will open a file or directory referenced by DevicePath.
460
jcarsey1e6e84c2010-01-25 20:05:08 +0000461 This function opens a file with the open mode according to the file path. The
jcarsey94b17fa2009-05-07 18:46:18 +0000462 Attributes is valid only for EFI_FILE_MODE_CREATE.
463
darylm503b0934ac2011-11-12 00:35:11 +0000464 @param FilePath on input the device path to the file. On output
jcarsey94b17fa2009-05-07 18:46:18 +0000465 the remaining device path.
darylm503b0934ac2011-11-12 00:35:11 +0000466 @param DeviceHandle pointer to the system device handle.
467 @param FileHandle pointer to the file handle.
468 @param OpenMode the mode to open the file with.
469 @param Attributes the file's file attributes.
jcarsey94b17fa2009-05-07 18:46:18 +0000470
darylm503b0934ac2011-11-12 00:35:11 +0000471 @retval EFI_SUCCESS The information was set.
472 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
473 @retval EFI_UNSUPPORTED Could not open the file path.
474 @retval EFI_NOT_FOUND The specified file could not be found on the
jcarsey1e6e84c2010-01-25 20:05:08 +0000475 device or the file system could not be found on
jcarsey94b17fa2009-05-07 18:46:18 +0000476 the device.
darylm503b0934ac2011-11-12 00:35:11 +0000477 @retval EFI_NO_MEDIA The device has no medium.
478 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
jcarsey94b17fa2009-05-07 18:46:18 +0000479 medium is no longer supported.
darylm503b0934ac2011-11-12 00:35:11 +0000480 @retval EFI_DEVICE_ERROR The device reported an error.
481 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
482 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
483 @retval EFI_ACCESS_DENIED The file was opened read only.
484 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
jcarsey94b17fa2009-05-07 18:46:18 +0000485 file.
darylm503b0934ac2011-11-12 00:35:11 +0000486 @retval EFI_VOLUME_FULL The volume is full.
jcarsey94b17fa2009-05-07 18:46:18 +0000487**/
488EFI_STATUS
489EFIAPI
490ShellOpenFileByDevicePath(
darylm503b0934ac2011-11-12 00:35:11 +0000491 IN OUT EFI_DEVICE_PATH_PROTOCOL **FilePath,
492 OUT EFI_HANDLE *DeviceHandle,
jcarseya405b862010-09-14 05:18:09 +0000493 OUT SHELL_FILE_HANDLE *FileHandle,
darylm503b0934ac2011-11-12 00:35:11 +0000494 IN UINT64 OpenMode,
495 IN UINT64 Attributes
jcarseya405b862010-09-14 05:18:09 +0000496 )
497{
498 CHAR16 *FileName;
499 EFI_STATUS Status;
jcarsey94b17fa2009-05-07 18:46:18 +0000500 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *EfiSimpleFileSystemProtocol;
jcarseya405b862010-09-14 05:18:09 +0000501 EFI_FILE_PROTOCOL *Handle1;
502 EFI_FILE_PROTOCOL *Handle2;
ydong100b6cb332013-01-25 02:00:22 +0000503 CHAR16 *FnafPathName;
504 UINTN PathLen;
jcarsey94b17fa2009-05-07 18:46:18 +0000505
jcarsey92a54472011-06-27 20:33:13 +0000506 if (FilePath == NULL || FileHandle == NULL || DeviceHandle == NULL) {
507 return (EFI_INVALID_PARAMETER);
508 }
509
jcarsey1e6e84c2010-01-25 20:05:08 +0000510 //
jcarsey94b17fa2009-05-07 18:46:18 +0000511 // which shell interface should we use
512 //
jcarsey366f81a2011-06-27 21:04:22 +0000513 if (gEfiShellProtocol != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +0000514 //
515 // use UEFI Shell 2.0 method.
516 //
jcarsey366f81a2011-06-27 21:04:22 +0000517 FileName = gEfiShellProtocol->GetFilePathFromDevicePath(*FilePath);
jcarsey94b17fa2009-05-07 18:46:18 +0000518 if (FileName == NULL) {
519 return (EFI_INVALID_PARAMETER);
520 }
521 Status = ShellOpenFileByName(FileName, FileHandle, OpenMode, Attributes);
522 FreePool(FileName);
523 return (Status);
jcarsey1e6e84c2010-01-25 20:05:08 +0000524 }
jcarseyd2b45642009-05-11 18:02:16 +0000525
526
527 //
528 // use old shell method.
529 //
jcarsey1e6e84c2010-01-25 20:05:08 +0000530 Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid,
531 FilePath,
jcarseyd2b45642009-05-11 18:02:16 +0000532 DeviceHandle);
533 if (EFI_ERROR (Status)) {
534 return Status;
535 }
536 Status = gBS->OpenProtocol(*DeviceHandle,
537 &gEfiSimpleFileSystemProtocolGuid,
jcarseyb1f95a02009-06-16 00:23:19 +0000538 (VOID**)&EfiSimpleFileSystemProtocol,
jcarseyd2b45642009-05-11 18:02:16 +0000539 gImageHandle,
540 NULL,
541 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
542 if (EFI_ERROR (Status)) {
543 return Status;
544 }
jcarseya405b862010-09-14 05:18:09 +0000545 Status = EfiSimpleFileSystemProtocol->OpenVolume(EfiSimpleFileSystemProtocol, &Handle1);
jcarseyd2b45642009-05-11 18:02:16 +0000546 if (EFI_ERROR (Status)) {
547 FileHandle = NULL;
548 return Status;
549 }
550
551 //
552 // go down directories one node at a time.
553 //
554 while (!IsDevicePathEnd (*FilePath)) {
jcarsey94b17fa2009-05-07 18:46:18 +0000555 //
jcarseyd2b45642009-05-11 18:02:16 +0000556 // For file system access each node should be a file path component
jcarsey94b17fa2009-05-07 18:46:18 +0000557 //
jcarseyd2b45642009-05-11 18:02:16 +0000558 if (DevicePathType (*FilePath) != MEDIA_DEVICE_PATH ||
559 DevicePathSubType (*FilePath) != MEDIA_FILEPATH_DP
jcarseya405b862010-09-14 05:18:09 +0000560 ) {
jcarsey94b17fa2009-05-07 18:46:18 +0000561 FileHandle = NULL;
jcarseyd2b45642009-05-11 18:02:16 +0000562 return (EFI_INVALID_PARAMETER);
jcarsey94b17fa2009-05-07 18:46:18 +0000563 }
jcarseyd2b45642009-05-11 18:02:16 +0000564 //
565 // Open this file path node
566 //
jcarseya405b862010-09-14 05:18:09 +0000567 Handle2 = Handle1;
568 Handle1 = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +0000569
570 //
ydong100b6cb332013-01-25 02:00:22 +0000571 // File Name Alignment Fix (FNAF)
572 // Handle2->Open may be incapable of handling a unaligned CHAR16 data.
573 // The structure pointed to by FilePath may be not CHAR16 aligned.
574 // This code copies the potentially unaligned PathName data from the
575 // FilePath structure to the aligned FnafPathName for use in the
576 // calls to Handl2->Open.
577 //
578
579 //
580 // Determine length of PathName, in bytes.
581 //
582 PathLen = DevicePathNodeLength (*FilePath) - SIZE_OF_FILEPATH_DEVICE_PATH;
583
584 //
585 // Allocate memory for the aligned copy of the string Extra allocation is to allow for forced alignment
586 // Copy bytes from possibly unaligned location to aligned location
587 //
588 FnafPathName = AllocateCopyPool(PathLen, (UINT8 *)((FILEPATH_DEVICE_PATH*)*FilePath)->PathName);
589 if (FnafPathName == NULL) {
590 return EFI_OUT_OF_RESOURCES;
591 }
592
593 //
jcarseyd2b45642009-05-11 18:02:16 +0000594 // Try to test opening an existing file
jcarsey94b17fa2009-05-07 18:46:18 +0000595 //
jcarseya405b862010-09-14 05:18:09 +0000596 Status = Handle2->Open (
597 Handle2,
598 &Handle1,
ydong100b6cb332013-01-25 02:00:22 +0000599 FnafPathName,
jcarseyd2b45642009-05-11 18:02:16 +0000600 OpenMode &~EFI_FILE_MODE_CREATE,
601 0
jcarseya405b862010-09-14 05:18:09 +0000602 );
jcarsey94b17fa2009-05-07 18:46:18 +0000603
jcarseyd2b45642009-05-11 18:02:16 +0000604 //
605 // see if the error was that it needs to be created
606 //
607 if ((EFI_ERROR (Status)) && (OpenMode != (OpenMode &~EFI_FILE_MODE_CREATE))) {
jcarseya405b862010-09-14 05:18:09 +0000608 Status = Handle2->Open (
609 Handle2,
610 &Handle1,
ydong100b6cb332013-01-25 02:00:22 +0000611 FnafPathName,
jcarseyd2b45642009-05-11 18:02:16 +0000612 OpenMode,
613 Attributes
jcarseya405b862010-09-14 05:18:09 +0000614 );
jcarsey94b17fa2009-05-07 18:46:18 +0000615 }
ydong100b6cb332013-01-25 02:00:22 +0000616
617 //
618 // Free the alignment buffer
619 //
620 FreePool(FnafPathName);
621
jcarseyd2b45642009-05-11 18:02:16 +0000622 //
623 // Close the last node
624 //
jcarseya405b862010-09-14 05:18:09 +0000625 Handle2->Close (Handle2);
jcarseyd2b45642009-05-11 18:02:16 +0000626
627 if (EFI_ERROR(Status)) {
628 return (Status);
629 }
630
631 //
632 // Get the next node
633 //
634 *FilePath = NextDevicePathNode (*FilePath);
jcarsey94b17fa2009-05-07 18:46:18 +0000635 }
jcarseya405b862010-09-14 05:18:09 +0000636
637 //
638 // This is a weak spot since if the undefined SHELL_FILE_HANDLE format changes this must change also!
639 //
640 *FileHandle = (VOID*)Handle1;
jcarseyd2b45642009-05-11 18:02:16 +0000641 return (EFI_SUCCESS);
jcarsey94b17fa2009-05-07 18:46:18 +0000642}
643
644/**
645 This function will open a file or directory referenced by filename.
646
jcarsey1e6e84c2010-01-25 20:05:08 +0000647 If return is EFI_SUCCESS, the Filehandle is the opened file's handle;
648 otherwise, the Filehandle is NULL. The Attributes is valid only for
jcarsey94b17fa2009-05-07 18:46:18 +0000649 EFI_FILE_MODE_CREATE.
650
jcarsey92a54472011-06-27 20:33:13 +0000651 if FileName is NULL then ASSERT()
jcarsey94b17fa2009-05-07 18:46:18 +0000652
darylm503b0934ac2011-11-12 00:35:11 +0000653 @param FileName pointer to file name
654 @param FileHandle pointer to the file handle.
655 @param OpenMode the mode to open the file with.
656 @param Attributes the file's file attributes.
jcarsey94b17fa2009-05-07 18:46:18 +0000657
darylm503b0934ac2011-11-12 00:35:11 +0000658 @retval EFI_SUCCESS The information was set.
659 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
660 @retval EFI_UNSUPPORTED Could not open the file path.
661 @retval EFI_NOT_FOUND The specified file could not be found on the
jcarsey1e6e84c2010-01-25 20:05:08 +0000662 device or the file system could not be found
jcarsey94b17fa2009-05-07 18:46:18 +0000663 on the device.
darylm503b0934ac2011-11-12 00:35:11 +0000664 @retval EFI_NO_MEDIA The device has no medium.
665 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
jcarsey94b17fa2009-05-07 18:46:18 +0000666 medium is no longer supported.
darylm503b0934ac2011-11-12 00:35:11 +0000667 @retval EFI_DEVICE_ERROR The device reported an error.
668 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
669 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
670 @retval EFI_ACCESS_DENIED The file was opened read only.
671 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
jcarsey94b17fa2009-05-07 18:46:18 +0000672 file.
darylm503b0934ac2011-11-12 00:35:11 +0000673 @retval EFI_VOLUME_FULL The volume is full.
jcarsey94b17fa2009-05-07 18:46:18 +0000674**/
675EFI_STATUS
676EFIAPI
677ShellOpenFileByName(
darylm503b0934ac2011-11-12 00:35:11 +0000678 IN CONST CHAR16 *FileName,
jcarseya405b862010-09-14 05:18:09 +0000679 OUT SHELL_FILE_HANDLE *FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000680 IN UINT64 OpenMode,
darylm503b0934ac2011-11-12 00:35:11 +0000681 IN UINT64 Attributes
jcarseya405b862010-09-14 05:18:09 +0000682 )
683{
jcarsey94b17fa2009-05-07 18:46:18 +0000684 EFI_HANDLE DeviceHandle;
685 EFI_DEVICE_PATH_PROTOCOL *FilePath;
jcarseyb1f95a02009-06-16 00:23:19 +0000686 EFI_STATUS Status;
687 EFI_FILE_INFO *FileInfo;
jaben carsey21a86a72015-01-13 22:16:41 +0000688 CHAR16 *FileNameCopy;
Yao Jiewendd17e3f2015-12-25 01:24:16 +0000689 EFI_STATUS Status2;
jcarsey94b17fa2009-05-07 18:46:18 +0000690
691 //
692 // ASSERT if FileName is NULL
693 //
694 ASSERT(FileName != NULL);
695
jcarseya405b862010-09-14 05:18:09 +0000696 if (FileName == NULL) {
697 return (EFI_INVALID_PARAMETER);
698 }
699
jcarsey366f81a2011-06-27 21:04:22 +0000700 if (gEfiShellProtocol != NULL) {
jaben carsey21a86a72015-01-13 22:16:41 +0000701 if ((OpenMode & EFI_FILE_MODE_CREATE) == EFI_FILE_MODE_CREATE) {
702
703 //
704 // Create only a directory
705 //
706 if ((Attributes & EFI_FILE_DIRECTORY) == EFI_FILE_DIRECTORY) {
707 return ShellCreateDirectory(FileName, FileHandle);
708 }
709
710 //
711 // Create the directory to create the file in
712 //
713 FileNameCopy = AllocateCopyPool (StrSize (FileName), FileName);
714 if (FileName == NULL) {
715 return (EFI_OUT_OF_RESOURCES);
716 }
717 PathCleanUpDirectories (FileNameCopy);
718 if (PathRemoveLastItem (FileNameCopy)) {
Jaben Carsey983fffd2015-07-28 20:22:26 +0000719 if (!EFI_ERROR(ShellCreateDirectory (FileNameCopy, FileHandle))) {
720 ShellCloseFile (FileHandle);
721 }
jaben carsey21a86a72015-01-13 22:16:41 +0000722 }
723 SHELL_FREE_NON_NULL (FileNameCopy);
jcarseya405b862010-09-14 05:18:09 +0000724 }
jaben carsey21a86a72015-01-13 22:16:41 +0000725
jcarsey94b17fa2009-05-07 18:46:18 +0000726 //
jaben carsey21a86a72015-01-13 22:16:41 +0000727 // Use UEFI Shell 2.0 method to create the file
jcarsey94b17fa2009-05-07 18:46:18 +0000728 //
jcarsey366f81a2011-06-27 21:04:22 +0000729 Status = gEfiShellProtocol->OpenFileByName(FileName,
jcarseyb1f95a02009-06-16 00:23:19 +0000730 FileHandle,
731 OpenMode);
Tapan Shah583448b2016-09-22 12:12:47 -0700732
733 if ((mUnicodeCollationProtocol->StriColl (mUnicodeCollationProtocol, (CHAR16*)FileName, L"NUL") != 0) &&
734 (mUnicodeCollationProtocol->StriColl (mUnicodeCollationProtocol, (CHAR16*)FileName, L"NULL") != 0) &&
735 !EFI_ERROR(Status) && ((OpenMode & EFI_FILE_MODE_CREATE) != 0)){
jcarsey2247dde2009-11-09 18:08:58 +0000736 FileInfo = FileFunctionMap.GetFileInfo(*FileHandle);
jcarseyb1f95a02009-06-16 00:23:19 +0000737 ASSERT(FileInfo != NULL);
738 FileInfo->Attribute = Attributes;
Yao Jiewendd17e3f2015-12-25 01:24:16 +0000739 Status2 = FileFunctionMap.SetFileInfo(*FileHandle, FileInfo);
jcarsey2247dde2009-11-09 18:08:58 +0000740 FreePool(FileInfo);
Yao Jiewendd17e3f2015-12-25 01:24:16 +0000741 if (EFI_ERROR (Status2)) {
742 gEfiShellProtocol->CloseFile(*FileHandle);
743 }
744 Status = Status2;
jcarseyb1f95a02009-06-16 00:23:19 +0000745 }
746 return (Status);
jcarsey1e6e84c2010-01-25 20:05:08 +0000747 }
jcarsey94b17fa2009-05-07 18:46:18 +0000748 //
749 // Using EFI Shell version
750 // this means convert name to path and call that function
751 // since this will use EFI method again that will open it.
752 //
753 ASSERT(mEfiShellEnvironment2 != NULL);
jcarseyb82bfcc2009-06-29 16:28:23 +0000754 FilePath = mEfiShellEnvironment2->NameToPath ((CHAR16*)FileName);
xdu290bfa222010-07-19 05:21:27 +0000755 if (FilePath != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +0000756 return (ShellOpenFileByDevicePath(&FilePath,
757 &DeviceHandle,
758 FileHandle,
759 OpenMode,
jcarseya405b862010-09-14 05:18:09 +0000760 Attributes));
jcarsey94b17fa2009-05-07 18:46:18 +0000761 }
762 return (EFI_DEVICE_ERROR);
763}
764/**
765 This function create a directory
766
jcarsey1e6e84c2010-01-25 20:05:08 +0000767 If return is EFI_SUCCESS, the Filehandle is the opened directory's handle;
768 otherwise, the Filehandle is NULL. If the directory already existed, this
jcarsey94b17fa2009-05-07 18:46:18 +0000769 function opens the existing directory.
770
darylm503b0934ac2011-11-12 00:35:11 +0000771 @param DirectoryName pointer to directory name
772 @param FileHandle pointer to the file handle.
jcarsey94b17fa2009-05-07 18:46:18 +0000773
darylm503b0934ac2011-11-12 00:35:11 +0000774 @retval EFI_SUCCESS The information was set.
775 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
776 @retval EFI_UNSUPPORTED Could not open the file path.
777 @retval EFI_NOT_FOUND The specified file could not be found on the
jcarsey1e6e84c2010-01-25 20:05:08 +0000778 device or the file system could not be found
jcarsey94b17fa2009-05-07 18:46:18 +0000779 on the device.
darylm503b0934ac2011-11-12 00:35:11 +0000780 @retval EFI_NO_MEDIA The device has no medium.
781 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
jcarsey94b17fa2009-05-07 18:46:18 +0000782 medium is no longer supported.
darylm503b0934ac2011-11-12 00:35:11 +0000783 @retval EFI_DEVICE_ERROR The device reported an error.
784 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
785 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
786 @retval EFI_ACCESS_DENIED The file was opened read only.
787 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
jcarsey94b17fa2009-05-07 18:46:18 +0000788 file.
darylm503b0934ac2011-11-12 00:35:11 +0000789 @retval EFI_VOLUME_FULL The volume is full.
jcarsey94b17fa2009-05-07 18:46:18 +0000790 @sa ShellOpenFileByName
791**/
792EFI_STATUS
793EFIAPI
794ShellCreateDirectory(
jcarseyb82bfcc2009-06-29 16:28:23 +0000795 IN CONST CHAR16 *DirectoryName,
jcarseya405b862010-09-14 05:18:09 +0000796 OUT SHELL_FILE_HANDLE *FileHandle
797 )
798{
jcarsey366f81a2011-06-27 21:04:22 +0000799 if (gEfiShellProtocol != NULL) {
jcarsey2247dde2009-11-09 18:08:58 +0000800 //
801 // Use UEFI Shell 2.0 method
802 //
jcarsey366f81a2011-06-27 21:04:22 +0000803 return (gEfiShellProtocol->CreateFile(DirectoryName,
jcarsey2247dde2009-11-09 18:08:58 +0000804 EFI_FILE_DIRECTORY,
805 FileHandle
jcarseya405b862010-09-14 05:18:09 +0000806 ));
jcarsey2247dde2009-11-09 18:08:58 +0000807 } else {
808 return (ShellOpenFileByName(DirectoryName,
809 FileHandle,
810 EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE,
811 EFI_FILE_DIRECTORY
jcarseya405b862010-09-14 05:18:09 +0000812 ));
jcarsey2247dde2009-11-09 18:08:58 +0000813 }
jcarsey94b17fa2009-05-07 18:46:18 +0000814}
815
816/**
817 This function reads information from an opened file.
818
jcarsey1e6e84c2010-01-25 20:05:08 +0000819 If FileHandle is not a directory, the function reads the requested number of
820 bytes from the file at the file's current position and returns them in Buffer.
jcarsey94b17fa2009-05-07 18:46:18 +0000821 If the read goes beyond the end of the file, the read length is truncated to the
jcarsey1e6e84c2010-01-25 20:05:08 +0000822 end of the file. The file's current position is increased by the number of bytes
823 returned. If FileHandle is a directory, the function reads the directory entry
824 at the file's current position and returns the entry in Buffer. If the Buffer
825 is not large enough to hold the current directory entry, then
826 EFI_BUFFER_TOO_SMALL is returned and the current file position is not updated.
827 BufferSize is set to be the size of the buffer needed to read the entry. On
828 success, the current position is updated to the next directory entry. If there
829 are no more directory entries, the read returns a zero-length buffer.
jcarsey94b17fa2009-05-07 18:46:18 +0000830 EFI_FILE_INFO is the structure returned as the directory entry.
831
832 @param FileHandle the opened file handle
jcarsey1e6e84c2010-01-25 20:05:08 +0000833 @param BufferSize on input the size of buffer in bytes. on return
jcarsey94b17fa2009-05-07 18:46:18 +0000834 the number of bytes written.
835 @param Buffer the buffer to put read data into.
836
darylm503b0934ac2011-11-12 00:35:11 +0000837 @retval EFI_SUCCESS Data was read.
838 @retval EFI_NO_MEDIA The device has no media.
839 @retval EFI_DEVICE_ERROR The device reported an error.
840 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
841 @retval EFI_BUFFER_TO_SMALL Buffer is too small. ReadSize contains required
jcarsey94b17fa2009-05-07 18:46:18 +0000842 size.
843
844**/
845EFI_STATUS
846EFIAPI
847ShellReadFile(
jcarseya405b862010-09-14 05:18:09 +0000848 IN SHELL_FILE_HANDLE FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000849 IN OUT UINTN *BufferSize,
850 OUT VOID *Buffer
jcarseya405b862010-09-14 05:18:09 +0000851 )
852{
jcarseyd2b45642009-05-11 18:02:16 +0000853 return (FileFunctionMap.ReadFile(FileHandle, BufferSize, Buffer));
jcarsey94b17fa2009-05-07 18:46:18 +0000854}
855
856
857/**
858 Write data to a file.
859
jcarsey1e6e84c2010-01-25 20:05:08 +0000860 This function writes the specified number of bytes to the file at the current
861 file position. The current file position is advanced the actual number of bytes
862 written, which is returned in BufferSize. Partial writes only occur when there
863 has been a data error during the write attempt (such as "volume space full").
864 The file is automatically grown to hold the data if required. Direct writes to
jcarsey94b17fa2009-05-07 18:46:18 +0000865 opened directories are not supported.
866
867 @param FileHandle The opened file for writing
868 @param BufferSize on input the number of bytes in Buffer. On output
869 the number of bytes written.
870 @param Buffer the buffer containing data to write is stored.
871
darylm503b0934ac2011-11-12 00:35:11 +0000872 @retval EFI_SUCCESS Data was written.
873 @retval EFI_UNSUPPORTED Writes to an open directory are not supported.
874 @retval EFI_NO_MEDIA The device has no media.
875 @retval EFI_DEVICE_ERROR The device reported an error.
876 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
877 @retval EFI_WRITE_PROTECTED The device is write-protected.
878 @retval EFI_ACCESS_DENIED The file was open for read only.
879 @retval EFI_VOLUME_FULL The volume is full.
jcarsey94b17fa2009-05-07 18:46:18 +0000880**/
881EFI_STATUS
882EFIAPI
883ShellWriteFile(
jcarsey252d9452011-03-25 20:49:53 +0000884 IN SHELL_FILE_HANDLE FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000885 IN OUT UINTN *BufferSize,
886 IN VOID *Buffer
jcarseya405b862010-09-14 05:18:09 +0000887 )
888{
jcarseyd2b45642009-05-11 18:02:16 +0000889 return (FileFunctionMap.WriteFile(FileHandle, BufferSize, Buffer));
jcarsey94b17fa2009-05-07 18:46:18 +0000890}
891
jcarsey1e6e84c2010-01-25 20:05:08 +0000892/**
jcarsey94b17fa2009-05-07 18:46:18 +0000893 Close an open file handle.
894
jcarsey1e6e84c2010-01-25 20:05:08 +0000895 This function closes a specified file handle. All "dirty" cached file data is
896 flushed to the device, and the file is closed. In all cases the handle is
jcarsey94b17fa2009-05-07 18:46:18 +0000897 closed.
898
899@param FileHandle the file handle to close.
900
901@retval EFI_SUCCESS the file handle was closed sucessfully.
902**/
903EFI_STATUS
904EFIAPI
905ShellCloseFile (
jcarseya405b862010-09-14 05:18:09 +0000906 IN SHELL_FILE_HANDLE *FileHandle
907 )
908{
jcarseyd2b45642009-05-11 18:02:16 +0000909 return (FileFunctionMap.CloseFile(*FileHandle));
jcarsey94b17fa2009-05-07 18:46:18 +0000910}
911
912/**
913 Delete a file and close the handle
914
915 This function closes and deletes a file. In all cases the file handle is closed.
jcarsey1e6e84c2010-01-25 20:05:08 +0000916 If the file cannot be deleted, the warning code EFI_WARN_DELETE_FAILURE is
jcarsey94b17fa2009-05-07 18:46:18 +0000917 returned, but the handle is still closed.
918
919 @param FileHandle the file handle to delete
920
921 @retval EFI_SUCCESS the file was closed sucessfully
jcarsey1e6e84c2010-01-25 20:05:08 +0000922 @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not
jcarsey94b17fa2009-05-07 18:46:18 +0000923 deleted
darylm503b0934ac2011-11-12 00:35:11 +0000924 @retval INVALID_PARAMETER One of the parameters has an invalid value.
jcarsey94b17fa2009-05-07 18:46:18 +0000925**/
926EFI_STATUS
927EFIAPI
928ShellDeleteFile (
darylm503b0934ac2011-11-12 00:35:11 +0000929 IN SHELL_FILE_HANDLE *FileHandle
jcarseya405b862010-09-14 05:18:09 +0000930 )
931{
jcarseyd2b45642009-05-11 18:02:16 +0000932 return (FileFunctionMap.DeleteFile(*FileHandle));
jcarsey94b17fa2009-05-07 18:46:18 +0000933}
934
935/**
936 Set the current position in a file.
937
jcarsey1e6e84c2010-01-25 20:05:08 +0000938 This function sets the current file position for the handle to the position
jcarsey94b17fa2009-05-07 18:46:18 +0000939 supplied. With the exception of seeking to position 0xFFFFFFFFFFFFFFFF, only
jcarsey1e6e84c2010-01-25 20:05:08 +0000940 absolute positioning is supported, and seeking past the end of the file is
941 allowed (a subsequent write would grow the file). Seeking to position
jcarsey94b17fa2009-05-07 18:46:18 +0000942 0xFFFFFFFFFFFFFFFF causes the current position to be set to the end of the file.
jcarsey1e6e84c2010-01-25 20:05:08 +0000943 If FileHandle is a directory, the only position that may be set is zero. This
jcarsey94b17fa2009-05-07 18:46:18 +0000944 has the effect of starting the read process of the directory entries over.
945
946 @param FileHandle The file handle on which the position is being set
947 @param Position Byte position from begining of file
948
949 @retval EFI_SUCCESS Operation completed sucessfully.
jcarsey1e6e84c2010-01-25 20:05:08 +0000950 @retval EFI_UNSUPPORTED the seek request for non-zero is not valid on
jcarsey94b17fa2009-05-07 18:46:18 +0000951 directories.
952 @retval INVALID_PARAMETER One of the parameters has an invalid value.
953**/
954EFI_STATUS
955EFIAPI
956ShellSetFilePosition (
darylm503b0934ac2011-11-12 00:35:11 +0000957 IN SHELL_FILE_HANDLE FileHandle,
958 IN UINT64 Position
jcarseya405b862010-09-14 05:18:09 +0000959 )
960{
jcarseyd2b45642009-05-11 18:02:16 +0000961 return (FileFunctionMap.SetFilePosition(FileHandle, Position));
jcarsey94b17fa2009-05-07 18:46:18 +0000962}
963
jcarsey1e6e84c2010-01-25 20:05:08 +0000964/**
jcarsey94b17fa2009-05-07 18:46:18 +0000965 Gets a file's current position
966
jcarsey1e6e84c2010-01-25 20:05:08 +0000967 This function retrieves the current file position for the file handle. For
968 directories, the current file position has no meaning outside of the file
jcarsey94b17fa2009-05-07 18:46:18 +0000969 system driver and as such the operation is not supported. An error is returned
970 if FileHandle is a directory.
971
972 @param FileHandle The open file handle on which to get the position.
973 @param Position Byte position from begining of file.
974
975 @retval EFI_SUCCESS the operation completed sucessfully.
976 @retval INVALID_PARAMETER One of the parameters has an invalid value.
977 @retval EFI_UNSUPPORTED the request is not valid on directories.
978**/
979EFI_STATUS
980EFIAPI
981ShellGetFilePosition (
jcarseya405b862010-09-14 05:18:09 +0000982 IN SHELL_FILE_HANDLE FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000983 OUT UINT64 *Position
jcarseya405b862010-09-14 05:18:09 +0000984 )
985{
jcarseyd2b45642009-05-11 18:02:16 +0000986 return (FileFunctionMap.GetFilePosition(FileHandle, Position));
jcarsey94b17fa2009-05-07 18:46:18 +0000987}
988/**
989 Flushes data on a file
jcarsey1e6e84c2010-01-25 20:05:08 +0000990
jcarsey94b17fa2009-05-07 18:46:18 +0000991 This function flushes all modified data associated with a file to a device.
992
993 @param FileHandle The file handle on which to flush data
994
995 @retval EFI_SUCCESS The data was flushed.
996 @retval EFI_NO_MEDIA The device has no media.
997 @retval EFI_DEVICE_ERROR The device reported an error.
998 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
999 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
1000 @retval EFI_ACCESS_DENIED The file was opened for read only.
1001**/
1002EFI_STATUS
1003EFIAPI
1004ShellFlushFile (
jcarseya405b862010-09-14 05:18:09 +00001005 IN SHELL_FILE_HANDLE FileHandle
1006 )
1007{
jcarseyd2b45642009-05-11 18:02:16 +00001008 return (FileFunctionMap.FlushFile(FileHandle));
jcarsey94b17fa2009-05-07 18:46:18 +00001009}
1010
darylm503b0934ac2011-11-12 00:35:11 +00001011/** Retrieve first entry from a directory.
jcarsey94b17fa2009-05-07 18:46:18 +00001012
darylm503b0934ac2011-11-12 00:35:11 +00001013 This function takes an open directory handle and gets information from the
1014 first entry in the directory. A buffer is allocated to contain
1015 the information and a pointer to the buffer is returned in *Buffer. The
1016 caller can use ShellFindNextFile() to get subsequent directory entries.
jcarsey94b17fa2009-05-07 18:46:18 +00001017
darylm503b0934ac2011-11-12 00:35:11 +00001018 The buffer will be freed by ShellFindNextFile() when the last directory
1019 entry is read. Otherwise, the caller must free the buffer, using FreePool,
1020 when finished with it.
1021
1022 @param[in] DirHandle The file handle of the directory to search.
1023 @param[out] Buffer The pointer to the buffer for the file's information.
jcarsey94b17fa2009-05-07 18:46:18 +00001024
1025 @retval EFI_SUCCESS Found the first file.
1026 @retval EFI_NOT_FOUND Cannot find the directory.
1027 @retval EFI_NO_MEDIA The device has no media.
1028 @retval EFI_DEVICE_ERROR The device reported an error.
1029 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
1030 @return Others status of ShellGetFileInfo, ShellSetFilePosition,
1031 or ShellReadFile
1032**/
1033EFI_STATUS
1034EFIAPI
1035ShellFindFirstFile (
jcarseya405b862010-09-14 05:18:09 +00001036 IN SHELL_FILE_HANDLE DirHandle,
jcarseyd2b45642009-05-11 18:02:16 +00001037 OUT EFI_FILE_INFO **Buffer
jcarseya405b862010-09-14 05:18:09 +00001038 )
1039{
jcarsey94b17fa2009-05-07 18:46:18 +00001040 //
jcarseyd2b45642009-05-11 18:02:16 +00001041 // pass to file handle lib
jcarsey94b17fa2009-05-07 18:46:18 +00001042 //
jcarseyd2b45642009-05-11 18:02:16 +00001043 return (FileHandleFindFirstFile(DirHandle, Buffer));
jcarsey94b17fa2009-05-07 18:46:18 +00001044}
darylm503b0934ac2011-11-12 00:35:11 +00001045/** Retrieve next entries from a directory.
jcarsey94b17fa2009-05-07 18:46:18 +00001046
darylm503b0934ac2011-11-12 00:35:11 +00001047 To use this function, the caller must first call the ShellFindFirstFile()
1048 function to get the first directory entry. Subsequent directory entries are
1049 retrieved by using the ShellFindNextFile() function. This function can
1050 be called several times to get each entry from the directory. If the call of
1051 ShellFindNextFile() retrieved the last directory entry, the next call of
1052 this function will set *NoFile to TRUE and free the buffer.
jcarsey94b17fa2009-05-07 18:46:18 +00001053
darylm503b0934ac2011-11-12 00:35:11 +00001054 @param[in] DirHandle The file handle of the directory.
1055 @param[out] Buffer The pointer to buffer for file's information.
1056 @param[out] NoFile The pointer to boolean when last file is found.
jcarsey94b17fa2009-05-07 18:46:18 +00001057
1058 @retval EFI_SUCCESS Found the next file, or reached last file
1059 @retval EFI_NO_MEDIA The device has no media.
1060 @retval EFI_DEVICE_ERROR The device reported an error.
1061 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
1062**/
1063EFI_STATUS
1064EFIAPI
1065ShellFindNextFile(
jcarseya405b862010-09-14 05:18:09 +00001066 IN SHELL_FILE_HANDLE DirHandle,
jcarsey94b17fa2009-05-07 18:46:18 +00001067 OUT EFI_FILE_INFO *Buffer,
1068 OUT BOOLEAN *NoFile
jcarseya405b862010-09-14 05:18:09 +00001069 )
1070{
jcarsey94b17fa2009-05-07 18:46:18 +00001071 //
jcarseyd2b45642009-05-11 18:02:16 +00001072 // pass to file handle lib
jcarsey94b17fa2009-05-07 18:46:18 +00001073 //
jcarseyd2b45642009-05-11 18:02:16 +00001074 return (FileHandleFindNextFile(DirHandle, Buffer, NoFile));
jcarsey94b17fa2009-05-07 18:46:18 +00001075}
1076/**
1077 Retrieve the size of a file.
1078
1079 if FileHandle is NULL then ASSERT()
1080 if Size is NULL then ASSERT()
1081
jcarsey1e6e84c2010-01-25 20:05:08 +00001082 This function extracts the file size info from the FileHandle's EFI_FILE_INFO
jcarsey94b17fa2009-05-07 18:46:18 +00001083 data.
1084
1085 @param FileHandle file handle from which size is retrieved
1086 @param Size pointer to size
1087
1088 @retval EFI_SUCCESS operation was completed sucessfully
1089 @retval EFI_DEVICE_ERROR cannot access the file
1090**/
1091EFI_STATUS
1092EFIAPI
1093ShellGetFileSize (
jcarseya405b862010-09-14 05:18:09 +00001094 IN SHELL_FILE_HANDLE FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +00001095 OUT UINT64 *Size
jcarseya405b862010-09-14 05:18:09 +00001096 )
1097{
jcarseyd2b45642009-05-11 18:02:16 +00001098 return (FileFunctionMap.GetFileSize(FileHandle, Size));
jcarsey94b17fa2009-05-07 18:46:18 +00001099}
1100/**
1101 Retrieves the status of the break execution flag
1102
1103 this function is useful to check whether the application is being asked to halt by the shell.
1104
1105 @retval TRUE the execution break is enabled
1106 @retval FALSE the execution break is not enabled
1107**/
1108BOOLEAN
1109EFIAPI
1110ShellGetExecutionBreakFlag(
1111 VOID
1112 )
1113{
jcarsey1e6e84c2010-01-25 20:05:08 +00001114 //
jcarsey94b17fa2009-05-07 18:46:18 +00001115 // Check for UEFI Shell 2.0 protocols
1116 //
jcarsey366f81a2011-06-27 21:04:22 +00001117 if (gEfiShellProtocol != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00001118
1119 //
1120 // We are using UEFI Shell 2.0; see if the event has been triggered
1121 //
jcarsey366f81a2011-06-27 21:04:22 +00001122 if (gBS->CheckEvent(gEfiShellProtocol->ExecutionBreak) != EFI_SUCCESS) {
jcarsey94b17fa2009-05-07 18:46:18 +00001123 return (FALSE);
1124 }
1125 return (TRUE);
jcarsey1e6e84c2010-01-25 20:05:08 +00001126 }
jcarsey94b17fa2009-05-07 18:46:18 +00001127
1128 //
1129 // using EFI Shell; call the function to check
1130 //
jcarsey92a54472011-06-27 20:33:13 +00001131 if (mEfiShellEnvironment2 != NULL) {
1132 return (mEfiShellEnvironment2->GetExecutionBreak());
1133 }
1134
1135 return (FALSE);
jcarsey94b17fa2009-05-07 18:46:18 +00001136}
1137/**
1138 return the value of an environment variable
1139
jcarsey1e6e84c2010-01-25 20:05:08 +00001140 this function gets the value of the environment variable set by the
jcarsey94b17fa2009-05-07 18:46:18 +00001141 ShellSetEnvironmentVariable function
1142
1143 @param EnvKey The key name of the environment variable.
1144
1145 @retval NULL the named environment variable does not exist.
1146 @return != NULL pointer to the value of the environment variable
1147**/
1148CONST CHAR16*
1149EFIAPI
1150ShellGetEnvironmentVariable (
jcarsey9b3bf082009-06-23 21:15:07 +00001151 IN CONST CHAR16 *EnvKey
jcarsey94b17fa2009-05-07 18:46:18 +00001152 )
1153{
jcarsey1e6e84c2010-01-25 20:05:08 +00001154 //
jcarsey94b17fa2009-05-07 18:46:18 +00001155 // Check for UEFI Shell 2.0 protocols
1156 //
jcarsey366f81a2011-06-27 21:04:22 +00001157 if (gEfiShellProtocol != NULL) {
1158 return (gEfiShellProtocol->GetEnv(EnvKey));
jcarsey94b17fa2009-05-07 18:46:18 +00001159 }
1160
1161 //
jcarsey92a54472011-06-27 20:33:13 +00001162 // Check for EFI shell
jcarsey94b17fa2009-05-07 18:46:18 +00001163 //
jcarsey92a54472011-06-27 20:33:13 +00001164 if (mEfiShellEnvironment2 != NULL) {
1165 return (mEfiShellEnvironment2->GetEnv((CHAR16*)EnvKey));
1166 }
jcarsey94b17fa2009-05-07 18:46:18 +00001167
jcarsey92a54472011-06-27 20:33:13 +00001168 return NULL;
jcarsey94b17fa2009-05-07 18:46:18 +00001169}
1170/**
1171 set the value of an environment variable
1172
1173This function changes the current value of the specified environment variable. If the
1174environment variable exists and the Value is an empty string, then the environment
1175variable is deleted. If the environment variable exists and the Value is not an empty
1176string, then the value of the environment variable is changed. If the environment
1177variable does not exist and the Value is an empty string, there is no action. If the
1178environment variable does not exist and the Value is a non-empty string, then the
1179environment variable is created and assigned the specified value.
1180
1181 This is not supported pre-UEFI Shell 2.0.
1182
1183 @param EnvKey The key name of the environment variable.
1184 @param EnvVal The Value of the environment variable
1185 @param Volatile Indicates whether the variable is non-volatile (FALSE) or volatile (TRUE).
1186
1187 @retval EFI_SUCCESS the operation was completed sucessfully
1188 @retval EFI_UNSUPPORTED This operation is not allowed in pre UEFI 2.0 Shell environments
1189**/
1190EFI_STATUS
1191EFIAPI
1192ShellSetEnvironmentVariable (
1193 IN CONST CHAR16 *EnvKey,
1194 IN CONST CHAR16 *EnvVal,
1195 IN BOOLEAN Volatile
1196 )
1197{
jcarsey1e6e84c2010-01-25 20:05:08 +00001198 //
jcarsey94b17fa2009-05-07 18:46:18 +00001199 // Check for UEFI Shell 2.0 protocols
1200 //
jcarsey366f81a2011-06-27 21:04:22 +00001201 if (gEfiShellProtocol != NULL) {
1202 return (gEfiShellProtocol->SetEnv(EnvKey, EnvVal, Volatile));
jcarsey1e6e84c2010-01-25 20:05:08 +00001203 }
jcarsey94b17fa2009-05-07 18:46:18 +00001204
1205 //
1206 // This feature does not exist under EFI shell
1207 //
1208 return (EFI_UNSUPPORTED);
1209}
jcarseya405b862010-09-14 05:18:09 +00001210
jcarsey94b17fa2009-05-07 18:46:18 +00001211/**
jcarseya405b862010-09-14 05:18:09 +00001212 Cause the shell to parse and execute a command line.
jcarsey94b17fa2009-05-07 18:46:18 +00001213
1214 This function creates a nested instance of the shell and executes the specified
jcarseya405b862010-09-14 05:18:09 +00001215 command (CommandLine) with the specified environment (Environment). Upon return,
1216 the status code returned by the specified command is placed in StatusCode.
1217 If Environment is NULL, then the current environment is used and all changes made
1218 by the commands executed will be reflected in the current environment. If the
1219 Environment is non-NULL, then the changes made will be discarded.
1220 The CommandLine is executed from the current working directory on the current
1221 device.
jcarsey94b17fa2009-05-07 18:46:18 +00001222
Brendan Jackman3877d0f2014-01-24 22:31:07 +00001223 The EnvironmentVariables pararemeter is ignored in a pre-UEFI Shell 2.0
jcarseya405b862010-09-14 05:18:09 +00001224 environment. The values pointed to by the parameters will be unchanged by the
1225 ShellExecute() function. The Output parameter has no effect in a
1226 UEFI Shell 2.0 environment.
jcarsey94b17fa2009-05-07 18:46:18 +00001227
jcarseya405b862010-09-14 05:18:09 +00001228 @param[in] ParentHandle The parent image starting the operation.
1229 @param[in] CommandLine The pointer to a NULL terminated command line.
1230 @param[in] Output True to display debug output. False to hide it.
1231 @param[in] EnvironmentVariables Optional pointer to array of environment variables
1232 in the form "x=y". If NULL, the current set is used.
1233 @param[out] Status The status of the run command line.
jcarsey94b17fa2009-05-07 18:46:18 +00001234
jcarseya405b862010-09-14 05:18:09 +00001235 @retval EFI_SUCCESS The operation completed sucessfully. Status
1236 contains the status code returned.
1237 @retval EFI_INVALID_PARAMETER A parameter contains an invalid value.
1238 @retval EFI_OUT_OF_RESOURCES Out of resources.
1239 @retval EFI_UNSUPPORTED The operation is not allowed.
jcarsey94b17fa2009-05-07 18:46:18 +00001240**/
1241EFI_STATUS
1242EFIAPI
1243ShellExecute (
1244 IN EFI_HANDLE *ParentHandle,
1245 IN CHAR16 *CommandLine OPTIONAL,
1246 IN BOOLEAN Output OPTIONAL,
1247 IN CHAR16 **EnvironmentVariables OPTIONAL,
1248 OUT EFI_STATUS *Status OPTIONAL
1249 )
1250{
Brendan Jackman3877d0f2014-01-24 22:31:07 +00001251 EFI_STATUS CmdStatus;
jcarsey1e6e84c2010-01-25 20:05:08 +00001252 //
jcarsey94b17fa2009-05-07 18:46:18 +00001253 // Check for UEFI Shell 2.0 protocols
1254 //
jcarsey366f81a2011-06-27 21:04:22 +00001255 if (gEfiShellProtocol != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00001256 //
1257 // Call UEFI Shell 2.0 version (not using Output parameter)
1258 //
jcarsey366f81a2011-06-27 21:04:22 +00001259 return (gEfiShellProtocol->Execute(ParentHandle,
jcarsey94b17fa2009-05-07 18:46:18 +00001260 CommandLine,
1261 EnvironmentVariables,
1262 Status));
jcarsey1e6e84c2010-01-25 20:05:08 +00001263 }
jcarsey92a54472011-06-27 20:33:13 +00001264
jcarsey94b17fa2009-05-07 18:46:18 +00001265 //
jcarsey92a54472011-06-27 20:33:13 +00001266 // Check for EFI shell
jcarsey94b17fa2009-05-07 18:46:18 +00001267 //
jcarsey92a54472011-06-27 20:33:13 +00001268 if (mEfiShellEnvironment2 != NULL) {
1269 //
Brendan Jackman3877d0f2014-01-24 22:31:07 +00001270 // Call EFI Shell version.
jcarsey92a54472011-06-27 20:33:13 +00001271 // Due to oddity in the EFI shell we want to dereference the ParentHandle here
1272 //
Brendan Jackman3877d0f2014-01-24 22:31:07 +00001273 CmdStatus = (mEfiShellEnvironment2->Execute(*ParentHandle,
jcarsey92a54472011-06-27 20:33:13 +00001274 CommandLine,
1275 Output));
Brendan Jackman3877d0f2014-01-24 22:31:07 +00001276 //
1277 // No Status output parameter so just use the returned status
1278 //
1279 if (Status != NULL) {
1280 *Status = CmdStatus;
1281 }
1282 //
1283 // If there was an error, we can't tell if it was from the command or from
1284 // the Execute() function, so we'll just assume the shell ran successfully
1285 // and the error came from the command.
1286 //
1287 return EFI_SUCCESS;
jcarsey92a54472011-06-27 20:33:13 +00001288 }
1289
1290 return (EFI_UNSUPPORTED);
jcarsey94b17fa2009-05-07 18:46:18 +00001291}
Brendan Jackman3877d0f2014-01-24 22:31:07 +00001292
jcarsey94b17fa2009-05-07 18:46:18 +00001293/**
1294 Retreives the current directory path
1295
jcarsey1e6e84c2010-01-25 20:05:08 +00001296 If the DeviceName is NULL, it returns the current device's current directory
1297 name. If the DeviceName is not NULL, it returns the current directory name
jcarsey94b17fa2009-05-07 18:46:18 +00001298 on specified drive.
1299
Qiu Shuminfbd2dfa2015-10-23 02:03:20 +00001300 Note that the current directory string should exclude the tailing backslash character.
1301
jcarsey94b17fa2009-05-07 18:46:18 +00001302 @param DeviceName the name of the drive to get directory on
1303
1304 @retval NULL the directory does not exist
1305 @return != NULL the directory
1306**/
1307CONST CHAR16*
1308EFIAPI
1309ShellGetCurrentDir (
jcarseya405b862010-09-14 05:18:09 +00001310 IN CHAR16 * CONST DeviceName OPTIONAL
jcarsey94b17fa2009-05-07 18:46:18 +00001311 )
1312{
jcarsey1e6e84c2010-01-25 20:05:08 +00001313 //
jcarsey94b17fa2009-05-07 18:46:18 +00001314 // Check for UEFI Shell 2.0 protocols
1315 //
jcarsey366f81a2011-06-27 21:04:22 +00001316 if (gEfiShellProtocol != NULL) {
1317 return (gEfiShellProtocol->GetCurDir(DeviceName));
jcarsey1e6e84c2010-01-25 20:05:08 +00001318 }
jcarsey92a54472011-06-27 20:33:13 +00001319
jcarsey94b17fa2009-05-07 18:46:18 +00001320 //
jcarsey8bd282b2011-06-27 16:45:41 +00001321 // Check for EFI shell
jcarsey94b17fa2009-05-07 18:46:18 +00001322 //
jcarsey8bd282b2011-06-27 16:45:41 +00001323 if (mEfiShellEnvironment2 != NULL) {
1324 return (mEfiShellEnvironment2->CurDir(DeviceName));
1325 }
1326
1327 return (NULL);
jcarsey94b17fa2009-05-07 18:46:18 +00001328}
1329/**
1330 sets (enabled or disabled) the page break mode
1331
jcarsey1e6e84c2010-01-25 20:05:08 +00001332 when page break mode is enabled the screen will stop scrolling
jcarsey94b17fa2009-05-07 18:46:18 +00001333 and wait for operator input before scrolling a subsequent screen.
1334
1335 @param CurrentState TRUE to enable and FALSE to disable
1336**/
jcarsey1e6e84c2010-01-25 20:05:08 +00001337VOID
jcarsey94b17fa2009-05-07 18:46:18 +00001338EFIAPI
1339ShellSetPageBreakMode (
1340 IN BOOLEAN CurrentState
1341 )
1342{
1343 //
1344 // check for enabling
1345 //
1346 if (CurrentState != 0x00) {
jcarsey1e6e84c2010-01-25 20:05:08 +00001347 //
jcarsey94b17fa2009-05-07 18:46:18 +00001348 // check for UEFI Shell 2.0
1349 //
jcarsey366f81a2011-06-27 21:04:22 +00001350 if (gEfiShellProtocol != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00001351 //
1352 // Enable with UEFI 2.0 Shell
1353 //
jcarsey366f81a2011-06-27 21:04:22 +00001354 gEfiShellProtocol->EnablePageBreak();
jcarsey94b17fa2009-05-07 18:46:18 +00001355 return;
1356 } else {
jcarsey1e6e84c2010-01-25 20:05:08 +00001357 //
jcarsey92a54472011-06-27 20:33:13 +00001358 // Check for EFI shell
jcarsey94b17fa2009-05-07 18:46:18 +00001359 //
jcarsey92a54472011-06-27 20:33:13 +00001360 if (mEfiShellEnvironment2 != NULL) {
1361 //
1362 // Enable with EFI Shell
1363 //
1364 mEfiShellEnvironment2->EnablePageBreak (DEFAULT_INIT_ROW, DEFAULT_AUTO_LF);
1365 return;
1366 }
jcarsey94b17fa2009-05-07 18:46:18 +00001367 }
1368 } else {
jcarsey1e6e84c2010-01-25 20:05:08 +00001369 //
jcarsey94b17fa2009-05-07 18:46:18 +00001370 // check for UEFI Shell 2.0
1371 //
jcarsey366f81a2011-06-27 21:04:22 +00001372 if (gEfiShellProtocol != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00001373 //
1374 // Disable with UEFI 2.0 Shell
1375 //
jcarsey366f81a2011-06-27 21:04:22 +00001376 gEfiShellProtocol->DisablePageBreak();
jcarsey94b17fa2009-05-07 18:46:18 +00001377 return;
1378 } else {
jcarsey1e6e84c2010-01-25 20:05:08 +00001379 //
jcarsey92a54472011-06-27 20:33:13 +00001380 // Check for EFI shell
jcarsey94b17fa2009-05-07 18:46:18 +00001381 //
jcarsey92a54472011-06-27 20:33:13 +00001382 if (mEfiShellEnvironment2 != NULL) {
1383 //
1384 // Disable with EFI Shell
1385 //
1386 mEfiShellEnvironment2->DisablePageBreak ();
1387 return;
1388 }
jcarsey94b17fa2009-05-07 18:46:18 +00001389 }
1390 }
1391}
1392
1393///
1394/// version of EFI_SHELL_FILE_INFO struct, except has no CONST pointers.
1395/// This allows for the struct to be populated.
1396///
1397typedef struct {
jcarseyd2b45642009-05-11 18:02:16 +00001398 LIST_ENTRY Link;
jcarsey94b17fa2009-05-07 18:46:18 +00001399 EFI_STATUS Status;
1400 CHAR16 *FullName;
1401 CHAR16 *FileName;
jcarseya405b862010-09-14 05:18:09 +00001402 SHELL_FILE_HANDLE Handle;
jcarsey94b17fa2009-05-07 18:46:18 +00001403 EFI_FILE_INFO *Info;
1404} EFI_SHELL_FILE_INFO_NO_CONST;
1405
1406/**
1407 Converts a EFI shell list of structures to the coresponding UEFI Shell 2.0 type of list.
1408
1409 if OldStyleFileList is NULL then ASSERT()
1410
jcarsey1e6e84c2010-01-25 20:05:08 +00001411 this function will convert a SHELL_FILE_ARG based list into a callee allocated
jcarsey94b17fa2009-05-07 18:46:18 +00001412 EFI_SHELL_FILE_INFO based list. it is up to the caller to free the memory via
1413 the ShellCloseFileMetaArg function.
1414
ydong104ff7e372011-09-02 08:05:34 +00001415 @param[in] FileList the EFI shell list type
1416 @param[in, out] ListHead the list to add to
jcarsey94b17fa2009-05-07 18:46:18 +00001417
1418 @retval the resultant head of the double linked new format list;
1419**/
1420LIST_ENTRY*
1421EFIAPI
1422InternalShellConvertFileListType (
jcarsey9b3bf082009-06-23 21:15:07 +00001423 IN LIST_ENTRY *FileList,
1424 IN OUT LIST_ENTRY *ListHead
jcarsey125c2cf2009-11-18 21:36:50 +00001425 )
1426{
jcarsey94b17fa2009-05-07 18:46:18 +00001427 SHELL_FILE_ARG *OldInfo;
jcarsey9b3bf082009-06-23 21:15:07 +00001428 LIST_ENTRY *Link;
jcarsey94b17fa2009-05-07 18:46:18 +00001429 EFI_SHELL_FILE_INFO_NO_CONST *NewInfo;
1430
1431 //
jcarsey9b3bf082009-06-23 21:15:07 +00001432 // ASSERTs
jcarsey94b17fa2009-05-07 18:46:18 +00001433 //
jcarsey9b3bf082009-06-23 21:15:07 +00001434 ASSERT(FileList != NULL);
1435 ASSERT(ListHead != NULL);
jcarsey94b17fa2009-05-07 18:46:18 +00001436
1437 //
1438 // enumerate through each member of the old list and copy
1439 //
jcarseyd2b45642009-05-11 18:02:16 +00001440 for (Link = FileList->ForwardLink; Link != FileList; Link = Link->ForwardLink) {
jcarsey94b17fa2009-05-07 18:46:18 +00001441 OldInfo = CR (Link, SHELL_FILE_ARG, Link, SHELL_FILE_ARG_SIGNATURE);
jcarseya405b862010-09-14 05:18:09 +00001442 ASSERT(OldInfo != NULL);
1443
1444 //
1445 // Skip ones that failed to open...
1446 //
1447 if (OldInfo->Status != EFI_SUCCESS) {
1448 continue;
1449 }
jcarsey94b17fa2009-05-07 18:46:18 +00001450
1451 //
1452 // make sure the old list was valid
1453 //
jcarsey94b17fa2009-05-07 18:46:18 +00001454 ASSERT(OldInfo->Info != NULL);
1455 ASSERT(OldInfo->FullName != NULL);
1456 ASSERT(OldInfo->FileName != NULL);
1457
1458 //
1459 // allocate a new EFI_SHELL_FILE_INFO object
1460 //
1461 NewInfo = AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));
jcarseyc9d92df2010-02-03 15:37:54 +00001462 if (NewInfo == NULL) {
jcarsey7a95efd2011-10-13 16:08:18 +00001463 ShellCloseFileMetaArg((EFI_SHELL_FILE_INFO**)(&ListHead));
jcarseybeab0fc2011-10-10 17:26:25 +00001464 ListHead = NULL;
jcarseyc9d92df2010-02-03 15:37:54 +00001465 break;
1466 }
jcarsey1e6e84c2010-01-25 20:05:08 +00001467
1468 //
jcarsey94b17fa2009-05-07 18:46:18 +00001469 // copy the simple items
1470 //
1471 NewInfo->Handle = OldInfo->Handle;
1472 NewInfo->Status = OldInfo->Status;
1473
jcarseyd2b45642009-05-11 18:02:16 +00001474 // old shell checks for 0 not NULL
1475 OldInfo->Handle = 0;
1476
jcarsey94b17fa2009-05-07 18:46:18 +00001477 //
1478 // allocate new space to copy strings and structure
1479 //
Jaben Carsey98c16be2014-08-19 21:00:34 +00001480 NewInfo->FullName = AllocateCopyPool(StrSize(OldInfo->FullName), OldInfo->FullName);
1481 NewInfo->FileName = AllocateCopyPool(StrSize(OldInfo->FileName), OldInfo->FileName);
1482 NewInfo->Info = AllocateCopyPool((UINTN)OldInfo->Info->Size, OldInfo->Info);
jcarsey1e6e84c2010-01-25 20:05:08 +00001483
jcarsey94b17fa2009-05-07 18:46:18 +00001484 //
1485 // make sure all the memory allocations were sucessful
1486 //
jcarseybeab0fc2011-10-10 17:26:25 +00001487 if (NULL == NewInfo->FullName || NewInfo->FileName == NULL || NewInfo->Info == NULL) {
Jaben Carsey98c16be2014-08-19 21:00:34 +00001488 //
1489 // Free the partially allocated new node
1490 //
1491 SHELL_FREE_NON_NULL(NewInfo->FullName);
1492 SHELL_FREE_NON_NULL(NewInfo->FileName);
1493 SHELL_FREE_NON_NULL(NewInfo->Info);
1494 SHELL_FREE_NON_NULL(NewInfo);
1495
1496 //
1497 // Free the previously converted stuff
1498 //
jcarsey7a95efd2011-10-13 16:08:18 +00001499 ShellCloseFileMetaArg((EFI_SHELL_FILE_INFO**)(&ListHead));
jcarseybeab0fc2011-10-10 17:26:25 +00001500 ListHead = NULL;
1501 break;
1502 }
jcarsey94b17fa2009-05-07 18:46:18 +00001503
1504 //
jcarsey94b17fa2009-05-07 18:46:18 +00001505 // add that to the list
1506 //
jcarsey9b3bf082009-06-23 21:15:07 +00001507 InsertTailList(ListHead, &NewInfo->Link);
jcarsey94b17fa2009-05-07 18:46:18 +00001508 }
1509 return (ListHead);
1510}
1511/**
1512 Opens a group of files based on a path.
1513
jcarsey1e6e84c2010-01-25 20:05:08 +00001514 This function uses the Arg to open all the matching files. Each matched
Brendan Jackman70879312014-01-24 22:29:53 +00001515 file has a SHELL_FILE_INFO structure to record the file information. These
1516 structures are placed on the list ListHead. Users can get the SHELL_FILE_INFO
jcarsey94b17fa2009-05-07 18:46:18 +00001517 structures from ListHead to access each file. This function supports wildcards
jcarsey1e6e84c2010-01-25 20:05:08 +00001518 and will process '?' and '*' as such. the list must be freed with a call to
jcarsey94b17fa2009-05-07 18:46:18 +00001519 ShellCloseFileMetaArg().
1520
jcarsey1e6e84c2010-01-25 20:05:08 +00001521 If you are NOT appending to an existing list *ListHead must be NULL. If
jcarsey5f7431d2009-07-10 18:06:01 +00001522 *ListHead is NULL then it must be callee freed.
jcarsey94b17fa2009-05-07 18:46:18 +00001523
1524 @param Arg pointer to path string
1525 @param OpenMode mode to open files with
1526 @param ListHead head of linked list of results
1527
jcarsey1e6e84c2010-01-25 20:05:08 +00001528 @retval EFI_SUCCESS the operation was sucessful and the list head
jcarsey94b17fa2009-05-07 18:46:18 +00001529 contains the list of opened files
jcarsey94b17fa2009-05-07 18:46:18 +00001530 @return != EFI_SUCCESS the operation failed
1531
1532 @sa InternalShellConvertFileListType
1533**/
1534EFI_STATUS
1535EFIAPI
1536ShellOpenFileMetaArg (
1537 IN CHAR16 *Arg,
1538 IN UINT64 OpenMode,
1539 IN OUT EFI_SHELL_FILE_INFO **ListHead
1540 )
1541{
1542 EFI_STATUS Status;
jcarsey9b3bf082009-06-23 21:15:07 +00001543 LIST_ENTRY mOldStyleFileList;
Qiu Shumin0960ba12014-09-17 07:58:31 +00001544 CHAR16 *CleanFilePathStr;
jcarsey1e6e84c2010-01-25 20:05:08 +00001545
jcarsey94b17fa2009-05-07 18:46:18 +00001546 //
1547 // ASSERT that Arg and ListHead are not NULL
1548 //
1549 ASSERT(Arg != NULL);
1550 ASSERT(ListHead != NULL);
1551
Qiu Shumin715096c2014-09-19 01:32:05 +00001552 CleanFilePathStr = NULL;
1553
Qiu Shumin0960ba12014-09-17 07:58:31 +00001554 Status = InternalShellStripQuotes (Arg, &CleanFilePathStr);
1555 if (EFI_ERROR (Status)) {
1556 return Status;
1557 }
1558
jcarsey1e6e84c2010-01-25 20:05:08 +00001559 //
jcarsey94b17fa2009-05-07 18:46:18 +00001560 // Check for UEFI Shell 2.0 protocols
1561 //
jcarsey366f81a2011-06-27 21:04:22 +00001562 if (gEfiShellProtocol != NULL) {
jcarsey5f7431d2009-07-10 18:06:01 +00001563 if (*ListHead == NULL) {
1564 *ListHead = (EFI_SHELL_FILE_INFO*)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));
1565 if (*ListHead == NULL) {
Qiu Shumin0960ba12014-09-17 07:58:31 +00001566 FreePool(CleanFilePathStr);
jcarsey5f7431d2009-07-10 18:06:01 +00001567 return (EFI_OUT_OF_RESOURCES);
1568 }
1569 InitializeListHead(&((*ListHead)->Link));
jcarsey1e6e84c2010-01-25 20:05:08 +00001570 }
Qiu Shumin0960ba12014-09-17 07:58:31 +00001571 Status = gEfiShellProtocol->OpenFileList(CleanFilePathStr,
jcarsey1e6e84c2010-01-25 20:05:08 +00001572 OpenMode,
jcarsey2247dde2009-11-09 18:08:58 +00001573 ListHead);
1574 if (EFI_ERROR(Status)) {
jcarsey366f81a2011-06-27 21:04:22 +00001575 gEfiShellProtocol->RemoveDupInFileList(ListHead);
jcarsey2247dde2009-11-09 18:08:58 +00001576 } else {
jcarsey366f81a2011-06-27 21:04:22 +00001577 Status = gEfiShellProtocol->RemoveDupInFileList(ListHead);
jcarsey2247dde2009-11-09 18:08:58 +00001578 }
jcarseya405b862010-09-14 05:18:09 +00001579 if (*ListHead != NULL && IsListEmpty(&(*ListHead)->Link)) {
1580 FreePool(*ListHead);
Qiu Shumin0960ba12014-09-17 07:58:31 +00001581 FreePool(CleanFilePathStr);
jcarseya405b862010-09-14 05:18:09 +00001582 *ListHead = NULL;
1583 return (EFI_NOT_FOUND);
1584 }
Qiu Shumin0960ba12014-09-17 07:58:31 +00001585 FreePool(CleanFilePathStr);
jcarsey2247dde2009-11-09 18:08:58 +00001586 return (Status);
jcarsey1e6e84c2010-01-25 20:05:08 +00001587 }
jcarsey94b17fa2009-05-07 18:46:18 +00001588
1589 //
jcarsey92a54472011-06-27 20:33:13 +00001590 // Check for EFI shell
jcarsey94b17fa2009-05-07 18:46:18 +00001591 //
jcarsey92a54472011-06-27 20:33:13 +00001592 if (mEfiShellEnvironment2 != NULL) {
1593 //
1594 // make sure the list head is initialized
1595 //
1596 InitializeListHead(&mOldStyleFileList);
jcarsey94b17fa2009-05-07 18:46:18 +00001597
jcarsey92a54472011-06-27 20:33:13 +00001598 //
1599 // Get the EFI Shell list of files
1600 //
Qiu Shumin0960ba12014-09-17 07:58:31 +00001601 Status = mEfiShellEnvironment2->FileMetaArg(CleanFilePathStr, &mOldStyleFileList);
jcarsey92a54472011-06-27 20:33:13 +00001602 if (EFI_ERROR(Status)) {
1603 *ListHead = NULL;
Qiu Shumin0960ba12014-09-17 07:58:31 +00001604 FreePool(CleanFilePathStr);
jcarsey92a54472011-06-27 20:33:13 +00001605 return (Status);
1606 }
jcarsey94b17fa2009-05-07 18:46:18 +00001607
jcarsey92a54472011-06-27 20:33:13 +00001608 if (*ListHead == NULL) {
1609 *ListHead = (EFI_SHELL_FILE_INFO *)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));
1610 if (*ListHead == NULL) {
Qiu Shumin0960ba12014-09-17 07:58:31 +00001611 FreePool(CleanFilePathStr);
jcarsey92a54472011-06-27 20:33:13 +00001612 return (EFI_OUT_OF_RESOURCES);
1613 }
1614 InitializeListHead(&((*ListHead)->Link));
1615 }
1616
1617 //
1618 // Convert that to equivalent of UEFI Shell 2.0 structure
1619 //
1620 InternalShellConvertFileListType(&mOldStyleFileList, &(*ListHead)->Link);
1621
1622 //
1623 // Free the EFI Shell version that was converted.
1624 //
1625 mEfiShellEnvironment2->FreeFileList(&mOldStyleFileList);
1626
1627 if ((*ListHead)->Link.ForwardLink == (*ListHead)->Link.BackLink && (*ListHead)->Link.BackLink == &((*ListHead)->Link)) {
1628 FreePool(*ListHead);
1629 *ListHead = NULL;
1630 Status = EFI_NOT_FOUND;
1631 }
Qiu Shumin0960ba12014-09-17 07:58:31 +00001632 FreePool(CleanFilePathStr);
jcarsey94b17fa2009-05-07 18:46:18 +00001633 return (Status);
1634 }
1635
Qiu Shumin0960ba12014-09-17 07:58:31 +00001636 FreePool(CleanFilePathStr);
jcarsey92a54472011-06-27 20:33:13 +00001637 return (EFI_UNSUPPORTED);
jcarsey94b17fa2009-05-07 18:46:18 +00001638}
1639/**
jcarseya405b862010-09-14 05:18:09 +00001640 Free the linked list returned from ShellOpenFileMetaArg.
jcarsey94b17fa2009-05-07 18:46:18 +00001641
jcarseya405b862010-09-14 05:18:09 +00001642 if ListHead is NULL then ASSERT().
jcarsey94b17fa2009-05-07 18:46:18 +00001643
jcarseya405b862010-09-14 05:18:09 +00001644 @param ListHead the pointer to free.
jcarsey94b17fa2009-05-07 18:46:18 +00001645
jcarseya405b862010-09-14 05:18:09 +00001646 @retval EFI_SUCCESS the operation was sucessful.
jcarsey94b17fa2009-05-07 18:46:18 +00001647**/
1648EFI_STATUS
1649EFIAPI
1650ShellCloseFileMetaArg (
1651 IN OUT EFI_SHELL_FILE_INFO **ListHead
1652 )
1653{
1654 LIST_ENTRY *Node;
1655
1656 //
1657 // ASSERT that ListHead is not NULL
1658 //
1659 ASSERT(ListHead != NULL);
1660
jcarsey1e6e84c2010-01-25 20:05:08 +00001661 //
jcarsey94b17fa2009-05-07 18:46:18 +00001662 // Check for UEFI Shell 2.0 protocols
1663 //
jcarsey366f81a2011-06-27 21:04:22 +00001664 if (gEfiShellProtocol != NULL) {
1665 return (gEfiShellProtocol->FreeFileList(ListHead));
jcarsey92a54472011-06-27 20:33:13 +00001666 } else if (mEfiShellEnvironment2 != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00001667 //
jcarsey1e6e84c2010-01-25 20:05:08 +00001668 // Since this is EFI Shell version we need to free our internally made copy
jcarsey94b17fa2009-05-07 18:46:18 +00001669 // of the list
1670 //
jcarsey1e6e84c2010-01-25 20:05:08 +00001671 for ( Node = GetFirstNode(&(*ListHead)->Link)
jcarseya405b862010-09-14 05:18:09 +00001672 ; *ListHead != NULL && !IsListEmpty(&(*ListHead)->Link)
jcarsey9b3bf082009-06-23 21:15:07 +00001673 ; Node = GetFirstNode(&(*ListHead)->Link)) {
jcarsey94b17fa2009-05-07 18:46:18 +00001674 RemoveEntryList(Node);
jcarseya405b862010-09-14 05:18:09 +00001675 ((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 +00001676 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->FullName);
1677 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->FileName);
1678 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->Info);
1679 FreePool((EFI_SHELL_FILE_INFO_NO_CONST*)Node);
1680 }
Jaben Carsey75a5e2e2014-01-10 16:42:45 +00001681 SHELL_FREE_NON_NULL(*ListHead);
jcarsey94b17fa2009-05-07 18:46:18 +00001682 return EFI_SUCCESS;
1683 }
jcarsey92a54472011-06-27 20:33:13 +00001684
1685 return (EFI_UNSUPPORTED);
jcarsey94b17fa2009-05-07 18:46:18 +00001686}
1687
jcarsey125c2cf2009-11-18 21:36:50 +00001688/**
1689 Find a file by searching the CWD and then the path.
1690
jcarseyb3011f42010-01-11 21:49:04 +00001691 If FileName is NULL then ASSERT.
jcarsey125c2cf2009-11-18 21:36:50 +00001692
jcarseyb3011f42010-01-11 21:49:04 +00001693 If the return value is not NULL then the memory must be caller freed.
jcarsey125c2cf2009-11-18 21:36:50 +00001694
1695 @param FileName Filename string.
1696
1697 @retval NULL the file was not found
1698 @return !NULL the full path to the file.
1699**/
1700CHAR16 *
1701EFIAPI
1702ShellFindFilePath (
1703 IN CONST CHAR16 *FileName
1704 )
1705{
1706 CONST CHAR16 *Path;
jcarseya405b862010-09-14 05:18:09 +00001707 SHELL_FILE_HANDLE Handle;
jcarsey125c2cf2009-11-18 21:36:50 +00001708 EFI_STATUS Status;
1709 CHAR16 *RetVal;
1710 CHAR16 *TestPath;
1711 CONST CHAR16 *Walker;
jcarsey36a9d672009-11-20 21:13:41 +00001712 UINTN Size;
jcarsey1cd45e72010-01-29 15:07:44 +00001713 CHAR16 *TempChar;
jcarsey125c2cf2009-11-18 21:36:50 +00001714
1715 RetVal = NULL;
1716
jcarseya405b862010-09-14 05:18:09 +00001717 //
1718 // First make sure its not an absolute path.
1719 //
1720 Status = ShellOpenFileByName(FileName, &Handle, EFI_FILE_MODE_READ, 0);
1721 if (!EFI_ERROR(Status)){
1722 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {
1723 ASSERT(RetVal == NULL);
1724 RetVal = StrnCatGrow(&RetVal, NULL, FileName, 0);
1725 ShellCloseFile(&Handle);
1726 return (RetVal);
1727 } else {
1728 ShellCloseFile(&Handle);
1729 }
1730 }
1731
jcarsey125c2cf2009-11-18 21:36:50 +00001732 Path = ShellGetEnvironmentVariable(L"cwd");
1733 if (Path != NULL) {
Qiu Shuminfbd2dfa2015-10-23 02:03:20 +00001734 Size = StrSize(Path) + sizeof(CHAR16);
jcarsey36a9d672009-11-20 21:13:41 +00001735 Size += StrSize(FileName);
1736 TestPath = AllocateZeroPool(Size);
jcarseyc9d92df2010-02-03 15:37:54 +00001737 if (TestPath == NULL) {
1738 return (NULL);
1739 }
Qiu Shumine75390f2015-06-30 03:18:31 +00001740 StrCpyS(TestPath, Size/sizeof(CHAR16), Path);
Qiu Shuminfbd2dfa2015-10-23 02:03:20 +00001741 StrCatS(TestPath, Size/sizeof(CHAR16), L"\\");
Qiu Shumine75390f2015-06-30 03:18:31 +00001742 StrCatS(TestPath, Size/sizeof(CHAR16), FileName);
jcarsey125c2cf2009-11-18 21:36:50 +00001743 Status = ShellOpenFileByName(TestPath, &Handle, EFI_FILE_MODE_READ, 0);
1744 if (!EFI_ERROR(Status)){
jcarseya405b862010-09-14 05:18:09 +00001745 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {
1746 ASSERT(RetVal == NULL);
1747 RetVal = StrnCatGrow(&RetVal, NULL, TestPath, 0);
1748 ShellCloseFile(&Handle);
1749 FreePool(TestPath);
1750 return (RetVal);
1751 } else {
1752 ShellCloseFile(&Handle);
1753 }
jcarsey125c2cf2009-11-18 21:36:50 +00001754 }
1755 FreePool(TestPath);
1756 }
1757 Path = ShellGetEnvironmentVariable(L"path");
1758 if (Path != NULL) {
jcarseya405b862010-09-14 05:18:09 +00001759 Size = StrSize(Path)+sizeof(CHAR16);
jcarsey36a9d672009-11-20 21:13:41 +00001760 Size += StrSize(FileName);
1761 TestPath = AllocateZeroPool(Size);
jcarsey3e082d52010-10-04 16:44:57 +00001762 if (TestPath == NULL) {
1763 return (NULL);
1764 }
jcarsey1e6e84c2010-01-25 20:05:08 +00001765 Walker = (CHAR16*)Path;
jcarsey125c2cf2009-11-18 21:36:50 +00001766 do {
1767 CopyMem(TestPath, Walker, StrSize(Walker));
jcarsey3e082d52010-10-04 16:44:57 +00001768 if (TestPath != NULL) {
1769 TempChar = StrStr(TestPath, L";");
1770 if (TempChar != NULL) {
1771 *TempChar = CHAR_NULL;
1772 }
1773 if (TestPath[StrLen(TestPath)-1] != L'\\') {
Qiu Shumine75390f2015-06-30 03:18:31 +00001774 StrCatS(TestPath, Size/sizeof(CHAR16), L"\\");
jcarsey3e082d52010-10-04 16:44:57 +00001775 }
jcarsey89e85372011-04-13 23:37:50 +00001776 if (FileName[0] == L'\\') {
1777 FileName++;
1778 }
Qiu Shumine75390f2015-06-30 03:18:31 +00001779 StrCatS(TestPath, Size/sizeof(CHAR16), FileName);
jcarsey3e082d52010-10-04 16:44:57 +00001780 if (StrStr(Walker, L";") != NULL) {
1781 Walker = StrStr(Walker, L";") + 1;
jcarseya405b862010-09-14 05:18:09 +00001782 } else {
jcarsey3e082d52010-10-04 16:44:57 +00001783 Walker = NULL;
1784 }
1785 Status = ShellOpenFileByName(TestPath, &Handle, EFI_FILE_MODE_READ, 0);
1786 if (!EFI_ERROR(Status)){
1787 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {
1788 ASSERT(RetVal == NULL);
1789 RetVal = StrnCatGrow(&RetVal, NULL, TestPath, 0);
1790 ShellCloseFile(&Handle);
1791 break;
1792 } else {
1793 ShellCloseFile(&Handle);
1794 }
jcarseya405b862010-09-14 05:18:09 +00001795 }
jcarsey125c2cf2009-11-18 21:36:50 +00001796 }
1797 } while (Walker != NULL && Walker[0] != CHAR_NULL);
1798 FreePool(TestPath);
1799 }
1800 return (RetVal);
1801}
1802
jcarseyb3011f42010-01-11 21:49:04 +00001803/**
jcarsey1e6e84c2010-01-25 20:05:08 +00001804 Find a file by searching the CWD and then the path with a variable set of file
1805 extensions. If the file is not found it will append each extension in the list
jcarseyb3011f42010-01-11 21:49:04 +00001806 in the order provided and return the first one that is successful.
1807
1808 If FileName is NULL, then ASSERT.
1809 If FileExtension is NULL, then behavior is identical to ShellFindFilePath.
1810
1811 If the return value is not NULL then the memory must be caller freed.
1812
1813 @param[in] FileName Filename string.
1814 @param[in] FileExtension Semi-colon delimeted list of possible extensions.
1815
1816 @retval NULL The file was not found.
1817 @retval !NULL The path to the file.
1818**/
1819CHAR16 *
1820EFIAPI
1821ShellFindFilePathEx (
1822 IN CONST CHAR16 *FileName,
1823 IN CONST CHAR16 *FileExtension
1824 )
1825{
1826 CHAR16 *TestPath;
1827 CHAR16 *RetVal;
1828 CONST CHAR16 *ExtensionWalker;
jcarsey9e926b62010-01-14 20:26:39 +00001829 UINTN Size;
jcarsey1cd45e72010-01-29 15:07:44 +00001830 CHAR16 *TempChar;
jcarseyc9d92df2010-02-03 15:37:54 +00001831 CHAR16 *TempChar2;
jcarsey1cd45e72010-01-29 15:07:44 +00001832
jcarseyb3011f42010-01-11 21:49:04 +00001833 ASSERT(FileName != NULL);
1834 if (FileExtension == NULL) {
1835 return (ShellFindFilePath(FileName));
1836 }
1837 RetVal = ShellFindFilePath(FileName);
1838 if (RetVal != NULL) {
1839 return (RetVal);
1840 }
jcarsey9e926b62010-01-14 20:26:39 +00001841 Size = StrSize(FileName);
1842 Size += StrSize(FileExtension);
1843 TestPath = AllocateZeroPool(Size);
jcarseyc9d92df2010-02-03 15:37:54 +00001844 if (TestPath == NULL) {
1845 return (NULL);
1846 }
jcarseya405b862010-09-14 05:18:09 +00001847 for (ExtensionWalker = FileExtension, TempChar2 = (CHAR16*)FileExtension; TempChar2 != NULL ; ExtensionWalker = TempChar2 + 1){
Qiu Shumine75390f2015-06-30 03:18:31 +00001848 StrCpyS(TestPath, Size/sizeof(CHAR16), FileName);
jcarseya405b862010-09-14 05:18:09 +00001849 if (ExtensionWalker != NULL) {
Qiu Shumine75390f2015-06-30 03:18:31 +00001850 StrCatS(TestPath, Size/sizeof(CHAR16), ExtensionWalker);
jcarseya405b862010-09-14 05:18:09 +00001851 }
jcarsey1cd45e72010-01-29 15:07:44 +00001852 TempChar = StrStr(TestPath, L";");
1853 if (TempChar != NULL) {
1854 *TempChar = CHAR_NULL;
jcarseyb3011f42010-01-11 21:49:04 +00001855 }
1856 RetVal = ShellFindFilePath(TestPath);
1857 if (RetVal != NULL) {
1858 break;
1859 }
jcarseya405b862010-09-14 05:18:09 +00001860 ASSERT(ExtensionWalker != NULL);
jcarseyc9d92df2010-02-03 15:37:54 +00001861 TempChar2 = StrStr(ExtensionWalker, L";");
jcarseyb3011f42010-01-11 21:49:04 +00001862 }
1863 FreePool(TestPath);
1864 return (RetVal);
1865}
1866
jcarsey94b17fa2009-05-07 18:46:18 +00001867typedef struct {
jcarsey9b3bf082009-06-23 21:15:07 +00001868 LIST_ENTRY Link;
jcarsey94b17fa2009-05-07 18:46:18 +00001869 CHAR16 *Name;
jcarseya405b862010-09-14 05:18:09 +00001870 SHELL_PARAM_TYPE Type;
jcarsey94b17fa2009-05-07 18:46:18 +00001871 CHAR16 *Value;
1872 UINTN OriginalPosition;
1873} SHELL_PARAM_PACKAGE;
1874
1875/**
jcarsey1e6e84c2010-01-25 20:05:08 +00001876 Checks the list of valid arguments and returns TRUE if the item was found. If the
jcarsey94b17fa2009-05-07 18:46:18 +00001877 return value is TRUE then the type parameter is set also.
jcarsey1e6e84c2010-01-25 20:05:08 +00001878
jcarsey94b17fa2009-05-07 18:46:18 +00001879 if CheckList is NULL then ASSERT();
1880 if Name is NULL then ASSERT();
1881 if Type is NULL then ASSERT();
1882
jcarsey94b17fa2009-05-07 18:46:18 +00001883 @param Name pointer to Name of parameter found
1884 @param CheckList List to check against
jcarseya405b862010-09-14 05:18:09 +00001885 @param Type pointer to type of parameter if it was found
jcarsey94b17fa2009-05-07 18:46:18 +00001886
1887 @retval TRUE the Parameter was found. Type is valid.
1888 @retval FALSE the Parameter was not found. Type is not valid.
1889**/
1890BOOLEAN
1891EFIAPI
jcarseyd2b45642009-05-11 18:02:16 +00001892InternalIsOnCheckList (
jcarsey94b17fa2009-05-07 18:46:18 +00001893 IN CONST CHAR16 *Name,
1894 IN CONST SHELL_PARAM_ITEM *CheckList,
jcarsey252d9452011-03-25 20:49:53 +00001895 OUT SHELL_PARAM_TYPE *Type
jcarseya405b862010-09-14 05:18:09 +00001896 )
1897{
jcarsey94b17fa2009-05-07 18:46:18 +00001898 SHELL_PARAM_ITEM *TempListItem;
jcarsey252d9452011-03-25 20:49:53 +00001899 CHAR16 *TempString;
jcarsey94b17fa2009-05-07 18:46:18 +00001900
1901 //
1902 // ASSERT that all 3 pointer parameters aren't NULL
1903 //
1904 ASSERT(CheckList != NULL);
1905 ASSERT(Type != NULL);
1906 ASSERT(Name != NULL);
1907
1908 //
jcarseyd2b45642009-05-11 18:02:16 +00001909 // question mark and page break mode are always supported
1910 //
1911 if ((StrCmp(Name, L"-?") == 0) ||
1912 (StrCmp(Name, L"-b") == 0)
jcarseya405b862010-09-14 05:18:09 +00001913 ) {
jcarsey252d9452011-03-25 20:49:53 +00001914 *Type = TypeFlag;
jcarseyd2b45642009-05-11 18:02:16 +00001915 return (TRUE);
1916 }
1917
1918 //
jcarsey94b17fa2009-05-07 18:46:18 +00001919 // Enumerate through the list
1920 //
1921 for (TempListItem = (SHELL_PARAM_ITEM*)CheckList ; TempListItem->Name != NULL ; TempListItem++) {
1922 //
jcarsey9eb53ac2009-07-08 17:26:58 +00001923 // If the Type is TypeStart only check the first characters of the passed in param
1924 // If it matches set the type and return TRUE
jcarsey94b17fa2009-05-07 18:46:18 +00001925 //
darylm503b0934ac2011-11-12 00:35:11 +00001926 if (TempListItem->Type == TypeStart) {
jcarsey252d9452011-03-25 20:49:53 +00001927 if (StrnCmp(Name, TempListItem->Name, StrLen(TempListItem->Name)) == 0) {
1928 *Type = TempListItem->Type;
1929 return (TRUE);
1930 }
1931 TempString = NULL;
1932 TempString = StrnCatGrow(&TempString, NULL, Name, StrLen(TempListItem->Name));
1933 if (TempString != NULL) {
1934 if (StringNoCaseCompare(&TempString, &TempListItem->Name) == 0) {
1935 *Type = TempListItem->Type;
1936 FreePool(TempString);
1937 return (TRUE);
1938 }
1939 FreePool(TempString);
1940 }
1941 } else if (StringNoCaseCompare(&Name, &TempListItem->Name) == 0) {
jcarsey94b17fa2009-05-07 18:46:18 +00001942 *Type = TempListItem->Type;
1943 return (TRUE);
1944 }
1945 }
jcarsey2247dde2009-11-09 18:08:58 +00001946
jcarsey94b17fa2009-05-07 18:46:18 +00001947 return (FALSE);
1948}
1949/**
jcarseyd2b45642009-05-11 18:02:16 +00001950 Checks the string for indicators of "flag" status. this is a leading '/', '-', or '+'
jcarsey94b17fa2009-05-07 18:46:18 +00001951
jcarseya405b862010-09-14 05:18:09 +00001952 @param[in] Name pointer to Name of parameter found
1953 @param[in] AlwaysAllowNumbers TRUE to allow numbers, FALSE to not.
jcarsey658bf432014-11-04 22:33:16 +00001954 @param[in] TimeNumbers TRUE to allow numbers with ":", FALSE otherwise.
jcarsey94b17fa2009-05-07 18:46:18 +00001955
1956 @retval TRUE the Parameter is a flag.
jcarseya405b862010-09-14 05:18:09 +00001957 @retval FALSE the Parameter not a flag.
jcarsey94b17fa2009-05-07 18:46:18 +00001958**/
1959BOOLEAN
1960EFIAPI
jcarseyd2b45642009-05-11 18:02:16 +00001961InternalIsFlag (
jcarsey2247dde2009-11-09 18:08:58 +00001962 IN CONST CHAR16 *Name,
jcarsey658bf432014-11-04 22:33:16 +00001963 IN CONST BOOLEAN AlwaysAllowNumbers,
1964 IN CONST BOOLEAN TimeNumbers
jcarsey94b17fa2009-05-07 18:46:18 +00001965 )
1966{
1967 //
1968 // ASSERT that Name isn't NULL
1969 //
1970 ASSERT(Name != NULL);
1971
1972 //
jcarsey2247dde2009-11-09 18:08:58 +00001973 // If we accept numbers then dont return TRUE. (they will be values)
1974 //
jcarsey658bf432014-11-04 22:33:16 +00001975 if (((Name[0] == L'-' || Name[0] == L'+') && InternalShellIsHexOrDecimalNumber(Name+1, FALSE, FALSE, TimeNumbers)) && AlwaysAllowNumbers) {
jcarsey2247dde2009-11-09 18:08:58 +00001976 return (FALSE);
1977 }
1978
1979 //
jcarseya405b862010-09-14 05:18:09 +00001980 // If the Name has a /, +, or - as the first character return TRUE
jcarsey94b17fa2009-05-07 18:46:18 +00001981 //
jcarsey1e6e84c2010-01-25 20:05:08 +00001982 if ((Name[0] == L'/') ||
jcarseyd2b45642009-05-11 18:02:16 +00001983 (Name[0] == L'-') ||
1984 (Name[0] == L'+')
jcarseya405b862010-09-14 05:18:09 +00001985 ) {
jcarsey94b17fa2009-05-07 18:46:18 +00001986 return (TRUE);
1987 }
1988 return (FALSE);
1989}
1990
1991/**
jcarsey1e6e84c2010-01-25 20:05:08 +00001992 Checks the command line arguments passed against the list of valid ones.
jcarsey94b17fa2009-05-07 18:46:18 +00001993
1994 If no initialization is required, then return RETURN_SUCCESS.
jcarsey1e6e84c2010-01-25 20:05:08 +00001995
jcarseya405b862010-09-14 05:18:09 +00001996 @param[in] CheckList pointer to list of parameters to check
1997 @param[out] CheckPackage pointer to pointer to list checked values
1998 @param[out] ProblemParam optional pointer to pointer to unicode string for
jcarseyd2b45642009-05-11 18:02:16 +00001999 the paramater that caused failure. If used then the
2000 caller is responsible for freeing the memory.
jcarseya405b862010-09-14 05:18:09 +00002001 @param[in] AutoPageBreak will automatically set PageBreakEnabled for "b" parameter
2002 @param[in] Argv pointer to array of parameters
2003 @param[in] Argc Count of parameters in Argv
2004 @param[in] AlwaysAllowNumbers TRUE to allow numbers always, FALSE otherwise.
jcarsey94b17fa2009-05-07 18:46:18 +00002005
2006 @retval EFI_SUCCESS The operation completed sucessfully.
2007 @retval EFI_OUT_OF_RESOURCES A memory allocation failed
2008 @retval EFI_INVALID_PARAMETER A parameter was invalid
jcarsey1e6e84c2010-01-25 20:05:08 +00002009 @retval EFI_VOLUME_CORRUPTED the command line was corrupt. an argument was
2010 duplicated. the duplicated command line argument
jcarsey94b17fa2009-05-07 18:46:18 +00002011 was returned in ProblemParam if provided.
jcarsey1e6e84c2010-01-25 20:05:08 +00002012 @retval EFI_NOT_FOUND a argument required a value that was missing.
jcarsey94b17fa2009-05-07 18:46:18 +00002013 the invalid command line argument was returned in
2014 ProblemParam if provided.
2015**/
2016EFI_STATUS
2017EFIAPI
2018InternalCommandLineParse (
2019 IN CONST SHELL_PARAM_ITEM *CheckList,
2020 OUT LIST_ENTRY **CheckPackage,
2021 OUT CHAR16 **ProblemParam OPTIONAL,
2022 IN BOOLEAN AutoPageBreak,
2023 IN CONST CHAR16 **Argv,
jcarsey2247dde2009-11-09 18:08:58 +00002024 IN UINTN Argc,
2025 IN BOOLEAN AlwaysAllowNumbers
jcarseya405b862010-09-14 05:18:09 +00002026 )
2027{
jcarsey94b17fa2009-05-07 18:46:18 +00002028 UINTN LoopCounter;
jcarsey252d9452011-03-25 20:49:53 +00002029 SHELL_PARAM_TYPE CurrentItemType;
jcarsey94b17fa2009-05-07 18:46:18 +00002030 SHELL_PARAM_PACKAGE *CurrentItemPackage;
jcarsey125c2cf2009-11-18 21:36:50 +00002031 UINTN GetItemValue;
2032 UINTN ValueSize;
jcarseya405b862010-09-14 05:18:09 +00002033 UINTN Count;
jcarsey252d9452011-03-25 20:49:53 +00002034 CONST CHAR16 *TempPointer;
Jaben Carsey98c16be2014-08-19 21:00:34 +00002035 UINTN CurrentValueSize;
Ruiyu Ni2efafab2016-07-13 17:39:37 +08002036 CHAR16 *NewValue;
jcarsey94b17fa2009-05-07 18:46:18 +00002037
2038 CurrentItemPackage = NULL;
jcarsey125c2cf2009-11-18 21:36:50 +00002039 GetItemValue = 0;
2040 ValueSize = 0;
jcarseya405b862010-09-14 05:18:09 +00002041 Count = 0;
jcarsey94b17fa2009-05-07 18:46:18 +00002042
2043 //
2044 // If there is only 1 item we dont need to do anything
2045 //
jcarseya405b862010-09-14 05:18:09 +00002046 if (Argc < 1) {
jcarsey94b17fa2009-05-07 18:46:18 +00002047 *CheckPackage = NULL;
2048 return (EFI_SUCCESS);
2049 }
2050
2051 //
jcarsey2247dde2009-11-09 18:08:58 +00002052 // ASSERTs
2053 //
2054 ASSERT(CheckList != NULL);
2055 ASSERT(Argv != NULL);
2056
2057 //
jcarsey94b17fa2009-05-07 18:46:18 +00002058 // initialize the linked list
2059 //
2060 *CheckPackage = (LIST_ENTRY*)AllocateZeroPool(sizeof(LIST_ENTRY));
sfu502a758c2011-10-08 02:55:30 +00002061 if (*CheckPackage == NULL) {
jcarseybeab0fc2011-10-10 17:26:25 +00002062 return (EFI_OUT_OF_RESOURCES);
sfu502a758c2011-10-08 02:55:30 +00002063 }
jcarseybeab0fc2011-10-10 17:26:25 +00002064
jcarsey94b17fa2009-05-07 18:46:18 +00002065 InitializeListHead(*CheckPackage);
2066
2067 //
2068 // loop through each of the arguments
2069 //
2070 for (LoopCounter = 0 ; LoopCounter < Argc ; ++LoopCounter) {
2071 if (Argv[LoopCounter] == NULL) {
2072 //
2073 // do nothing for NULL argv
2074 //
jcarseya405b862010-09-14 05:18:09 +00002075 } else if (InternalIsOnCheckList(Argv[LoopCounter], CheckList, &CurrentItemType)) {
jcarsey94b17fa2009-05-07 18:46:18 +00002076 //
jcarsey2247dde2009-11-09 18:08:58 +00002077 // We might have leftover if last parameter didnt have optional value
2078 //
jcarsey125c2cf2009-11-18 21:36:50 +00002079 if (GetItemValue != 0) {
2080 GetItemValue = 0;
jcarsey2247dde2009-11-09 18:08:58 +00002081 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
2082 }
2083 //
jcarsey94b17fa2009-05-07 18:46:18 +00002084 // this is a flag
2085 //
jcarsey252d9452011-03-25 20:49:53 +00002086 CurrentItemPackage = AllocateZeroPool(sizeof(SHELL_PARAM_PACKAGE));
jcarseybeab0fc2011-10-10 17:26:25 +00002087 if (CurrentItemPackage == NULL) {
2088 ShellCommandLineFreeVarList(*CheckPackage);
2089 *CheckPackage = NULL;
2090 return (EFI_OUT_OF_RESOURCES);
2091 }
Jaben Carsey98c16be2014-08-19 21:00:34 +00002092 CurrentItemPackage->Name = AllocateCopyPool(StrSize(Argv[LoopCounter]), Argv[LoopCounter]);
jcarseybeab0fc2011-10-10 17:26:25 +00002093 if (CurrentItemPackage->Name == NULL) {
2094 ShellCommandLineFreeVarList(*CheckPackage);
2095 *CheckPackage = NULL;
2096 return (EFI_OUT_OF_RESOURCES);
2097 }
jcarsey94b17fa2009-05-07 18:46:18 +00002098 CurrentItemPackage->Type = CurrentItemType;
2099 CurrentItemPackage->OriginalPosition = (UINTN)(-1);
jcarseyb1f95a02009-06-16 00:23:19 +00002100 CurrentItemPackage->Value = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +00002101
2102 //
2103 // Does this flag require a value
2104 //
jcarsey125c2cf2009-11-18 21:36:50 +00002105 switch (CurrentItemPackage->Type) {
jcarsey94b17fa2009-05-07 18:46:18 +00002106 //
jcarsey125c2cf2009-11-18 21:36:50 +00002107 // possibly trigger the next loop(s) to populate the value of this item
jcarsey1e6e84c2010-01-25 20:05:08 +00002108 //
jcarsey125c2cf2009-11-18 21:36:50 +00002109 case TypeValue:
jcarsey658bf432014-11-04 22:33:16 +00002110 case TypeTimeValue:
jcarsey1e6e84c2010-01-25 20:05:08 +00002111 GetItemValue = 1;
jcarsey125c2cf2009-11-18 21:36:50 +00002112 ValueSize = 0;
2113 break;
2114 case TypeDoubleValue:
2115 GetItemValue = 2;
2116 ValueSize = 0;
2117 break;
2118 case TypeMaxValue:
2119 GetItemValue = (UINTN)(-1);
2120 ValueSize = 0;
2121 break;
2122 default:
2123 //
2124 // this item has no value expected; we are done
2125 //
2126 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
2127 ASSERT(GetItemValue == 0);
2128 break;
jcarsey94b17fa2009-05-07 18:46:18 +00002129 }
Qiu Shuminaf7a3a52015-03-13 02:04:17 +00002130 } else if (GetItemValue != 0 && CurrentItemPackage != NULL && !InternalIsFlag(Argv[LoopCounter], AlwaysAllowNumbers, (BOOLEAN)(CurrentItemPackage->Type == TypeTimeValue))) {
jcarseyb1f95a02009-06-16 00:23:19 +00002131 //
jcarsey125c2cf2009-11-18 21:36:50 +00002132 // get the item VALUE for a previous flag
jcarseyb1f95a02009-06-16 00:23:19 +00002133 //
Qiu Shumin484dd082015-04-01 00:49:05 +00002134 CurrentValueSize = ValueSize + StrSize(Argv[LoopCounter]) + sizeof(CHAR16);
Ruiyu Ni2efafab2016-07-13 17:39:37 +08002135 NewValue = ReallocatePool(ValueSize, CurrentValueSize, CurrentItemPackage->Value);
2136 if (NewValue == NULL) {
2137 SHELL_FREE_NON_NULL (CurrentItemPackage->Value);
2138 SHELL_FREE_NON_NULL (CurrentItemPackage);
2139 ShellCommandLineFreeVarList (*CheckPackage);
2140 *CheckPackage = NULL;
2141 return EFI_OUT_OF_RESOURCES;
2142 }
2143 CurrentItemPackage->Value = NewValue;
Qiu Shumin484dd082015-04-01 00:49:05 +00002144 if (ValueSize == 0) {
Qiu Shumine75390f2015-06-30 03:18:31 +00002145 StrCpyS( CurrentItemPackage->Value,
2146 CurrentValueSize/sizeof(CHAR16),
2147 Argv[LoopCounter]
2148 );
jcarsey125c2cf2009-11-18 21:36:50 +00002149 } else {
Qiu Shumine75390f2015-06-30 03:18:31 +00002150 StrCatS( CurrentItemPackage->Value,
2151 CurrentValueSize/sizeof(CHAR16),
2152 L" "
2153 );
2154 StrCatS( CurrentItemPackage->Value,
2155 CurrentValueSize/sizeof(CHAR16),
2156 Argv[LoopCounter]
2157 );
jcarsey125c2cf2009-11-18 21:36:50 +00002158 }
Qiu Shumin484dd082015-04-01 00:49:05 +00002159 ValueSize += StrSize(Argv[LoopCounter]) + sizeof(CHAR16);
2160
jcarsey125c2cf2009-11-18 21:36:50 +00002161 GetItemValue--;
2162 if (GetItemValue == 0) {
2163 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
2164 }
jcarsey658bf432014-11-04 22:33:16 +00002165 } else if (!InternalIsFlag(Argv[LoopCounter], AlwaysAllowNumbers, FALSE)){
jcarseyb1f95a02009-06-16 00:23:19 +00002166 //
2167 // add this one as a non-flag
2168 //
jcarsey252d9452011-03-25 20:49:53 +00002169
2170 TempPointer = Argv[LoopCounter];
darylm503b0934ac2011-11-12 00:35:11 +00002171 if ((*TempPointer == L'^' && *(TempPointer+1) == L'-')
jcarsey252d9452011-03-25 20:49:53 +00002172 || (*TempPointer == L'^' && *(TempPointer+1) == L'/')
2173 || (*TempPointer == L'^' && *(TempPointer+1) == L'+')
2174 ){
2175 TempPointer++;
2176 }
2177 CurrentItemPackage = AllocateZeroPool(sizeof(SHELL_PARAM_PACKAGE));
jcarseybeab0fc2011-10-10 17:26:25 +00002178 if (CurrentItemPackage == NULL) {
2179 ShellCommandLineFreeVarList(*CheckPackage);
2180 *CheckPackage = NULL;
2181 return (EFI_OUT_OF_RESOURCES);
2182 }
jcarseyb1f95a02009-06-16 00:23:19 +00002183 CurrentItemPackage->Name = NULL;
2184 CurrentItemPackage->Type = TypePosition;
Jaben Carsey98c16be2014-08-19 21:00:34 +00002185 CurrentItemPackage->Value = AllocateCopyPool(StrSize(TempPointer), TempPointer);
jcarseybeab0fc2011-10-10 17:26:25 +00002186 if (CurrentItemPackage->Value == NULL) {
2187 ShellCommandLineFreeVarList(*CheckPackage);
2188 *CheckPackage = NULL;
2189 return (EFI_OUT_OF_RESOURCES);
2190 }
jcarseya405b862010-09-14 05:18:09 +00002191 CurrentItemPackage->OriginalPosition = Count++;
jcarsey9b3bf082009-06-23 21:15:07 +00002192 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
jcarsey252d9452011-03-25 20:49:53 +00002193 } else {
jcarsey94b17fa2009-05-07 18:46:18 +00002194 //
2195 // this was a non-recognised flag... error!
2196 //
jcarsey252d9452011-03-25 20:49:53 +00002197 if (ProblemParam != NULL) {
Jaben Carsey98c16be2014-08-19 21:00:34 +00002198 *ProblemParam = AllocateCopyPool(StrSize(Argv[LoopCounter]), Argv[LoopCounter]);
jcarsey252d9452011-03-25 20:49:53 +00002199 }
jcarsey94b17fa2009-05-07 18:46:18 +00002200 ShellCommandLineFreeVarList(*CheckPackage);
2201 *CheckPackage = NULL;
2202 return (EFI_VOLUME_CORRUPTED);
jcarsey94b17fa2009-05-07 18:46:18 +00002203 }
2204 }
jcarsey125c2cf2009-11-18 21:36:50 +00002205 if (GetItemValue != 0) {
2206 GetItemValue = 0;
2207 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
2208 }
jcarsey94b17fa2009-05-07 18:46:18 +00002209 //
2210 // support for AutoPageBreak
2211 //
2212 if (AutoPageBreak && ShellCommandLineGetFlag(*CheckPackage, L"-b")) {
2213 ShellSetPageBreakMode(TRUE);
2214 }
2215 return (EFI_SUCCESS);
2216}
2217
2218/**
jcarsey1e6e84c2010-01-25 20:05:08 +00002219 Checks the command line arguments passed against the list of valid ones.
jcarsey94b17fa2009-05-07 18:46:18 +00002220 Optionally removes NULL values first.
jcarsey1e6e84c2010-01-25 20:05:08 +00002221
jcarsey94b17fa2009-05-07 18:46:18 +00002222 If no initialization is required, then return RETURN_SUCCESS.
jcarsey1e6e84c2010-01-25 20:05:08 +00002223
jcarseya405b862010-09-14 05:18:09 +00002224 @param[in] CheckList The pointer to list of parameters to check.
2225 @param[out] CheckPackage The package of checked values.
2226 @param[out] ProblemParam Optional pointer to pointer to unicode string for
jcarsey94b17fa2009-05-07 18:46:18 +00002227 the paramater that caused failure.
jcarseya405b862010-09-14 05:18:09 +00002228 @param[in] AutoPageBreak Will automatically set PageBreakEnabled.
2229 @param[in] AlwaysAllowNumbers Will never fail for number based flags.
jcarsey94b17fa2009-05-07 18:46:18 +00002230
2231 @retval EFI_SUCCESS The operation completed sucessfully.
jcarseya405b862010-09-14 05:18:09 +00002232 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
2233 @retval EFI_INVALID_PARAMETER A parameter was invalid.
2234 @retval EFI_VOLUME_CORRUPTED The command line was corrupt.
2235 @retval EFI_DEVICE_ERROR The commands contained 2 opposing arguments. One
jcarsey1e6e84c2010-01-25 20:05:08 +00002236 of the command line arguments was returned in
jcarsey94b17fa2009-05-07 18:46:18 +00002237 ProblemParam if provided.
jcarseya405b862010-09-14 05:18:09 +00002238 @retval EFI_NOT_FOUND A argument required a value that was missing.
2239 The invalid command line argument was returned in
jcarsey94b17fa2009-05-07 18:46:18 +00002240 ProblemParam if provided.
2241**/
2242EFI_STATUS
2243EFIAPI
jcarsey2247dde2009-11-09 18:08:58 +00002244ShellCommandLineParseEx (
jcarsey94b17fa2009-05-07 18:46:18 +00002245 IN CONST SHELL_PARAM_ITEM *CheckList,
2246 OUT LIST_ENTRY **CheckPackage,
2247 OUT CHAR16 **ProblemParam OPTIONAL,
jcarsey2247dde2009-11-09 18:08:58 +00002248 IN BOOLEAN AutoPageBreak,
2249 IN BOOLEAN AlwaysAllowNumbers
jcarseya405b862010-09-14 05:18:09 +00002250 )
2251{
jcarsey1e6e84c2010-01-25 20:05:08 +00002252 //
jcarsey94b17fa2009-05-07 18:46:18 +00002253 // ASSERT that CheckList and CheckPackage aren't NULL
2254 //
2255 ASSERT(CheckList != NULL);
2256 ASSERT(CheckPackage != NULL);
2257
jcarsey1e6e84c2010-01-25 20:05:08 +00002258 //
jcarsey94b17fa2009-05-07 18:46:18 +00002259 // Check for UEFI Shell 2.0 protocols
2260 //
jcarsey366f81a2011-06-27 21:04:22 +00002261 if (gEfiShellParametersProtocol != NULL) {
jcarsey1e6e84c2010-01-25 20:05:08 +00002262 return (InternalCommandLineParse(CheckList,
2263 CheckPackage,
2264 ProblemParam,
2265 AutoPageBreak,
jcarsey366f81a2011-06-27 21:04:22 +00002266 (CONST CHAR16**) gEfiShellParametersProtocol->Argv,
2267 gEfiShellParametersProtocol->Argc,
jcarsey2247dde2009-11-09 18:08:58 +00002268 AlwaysAllowNumbers));
jcarsey94b17fa2009-05-07 18:46:18 +00002269 }
2270
jcarsey1e6e84c2010-01-25 20:05:08 +00002271 //
jcarsey94b17fa2009-05-07 18:46:18 +00002272 // ASSERT That EFI Shell is not required
2273 //
2274 ASSERT (mEfiShellInterface != NULL);
jcarsey1e6e84c2010-01-25 20:05:08 +00002275 return (InternalCommandLineParse(CheckList,
2276 CheckPackage,
2277 ProblemParam,
2278 AutoPageBreak,
jljusten08d7f8e2009-06-15 18:42:13 +00002279 (CONST CHAR16**) mEfiShellInterface->Argv,
jcarsey2247dde2009-11-09 18:08:58 +00002280 mEfiShellInterface->Argc,
2281 AlwaysAllowNumbers));
jcarsey94b17fa2009-05-07 18:46:18 +00002282}
2283
2284/**
2285 Frees shell variable list that was returned from ShellCommandLineParse.
2286
2287 This function will free all the memory that was used for the CheckPackage
2288 list of postprocessed shell arguments.
2289
2290 this function has no return value.
2291
2292 if CheckPackage is NULL, then return
2293
2294 @param CheckPackage the list to de-allocate
2295 **/
2296VOID
2297EFIAPI
2298ShellCommandLineFreeVarList (
2299 IN LIST_ENTRY *CheckPackage
jcarseya405b862010-09-14 05:18:09 +00002300 )
2301{
jcarsey94b17fa2009-05-07 18:46:18 +00002302 LIST_ENTRY *Node;
2303
2304 //
2305 // check for CheckPackage == NULL
2306 //
2307 if (CheckPackage == NULL) {
2308 return;
2309 }
2310
2311 //
2312 // for each node in the list
2313 //
jcarsey9eb53ac2009-07-08 17:26:58 +00002314 for ( Node = GetFirstNode(CheckPackage)
jcarseya405b862010-09-14 05:18:09 +00002315 ; !IsListEmpty(CheckPackage)
jcarsey9eb53ac2009-07-08 17:26:58 +00002316 ; Node = GetFirstNode(CheckPackage)
jcarseya405b862010-09-14 05:18:09 +00002317 ){
jcarsey94b17fa2009-05-07 18:46:18 +00002318 //
2319 // Remove it from the list
2320 //
2321 RemoveEntryList(Node);
2322
2323 //
2324 // if it has a name free the name
2325 //
2326 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {
2327 FreePool(((SHELL_PARAM_PACKAGE*)Node)->Name);
2328 }
2329
2330 //
2331 // if it has a value free the value
2332 //
2333 if (((SHELL_PARAM_PACKAGE*)Node)->Value != NULL) {
2334 FreePool(((SHELL_PARAM_PACKAGE*)Node)->Value);
2335 }
jcarsey1e6e84c2010-01-25 20:05:08 +00002336
jcarsey94b17fa2009-05-07 18:46:18 +00002337 //
2338 // free the node structure
2339 //
2340 FreePool((SHELL_PARAM_PACKAGE*)Node);
2341 }
2342 //
2343 // free the list head node
2344 //
2345 FreePool(CheckPackage);
2346}
2347/**
2348 Checks for presence of a flag parameter
2349
2350 flag arguments are in the form of "-<Key>" or "/<Key>", but do not have a value following the key
2351
2352 if CheckPackage is NULL then return FALSE.
2353 if KeyString is NULL then ASSERT()
jcarsey1e6e84c2010-01-25 20:05:08 +00002354
jcarsey94b17fa2009-05-07 18:46:18 +00002355 @param CheckPackage The package of parsed command line arguments
2356 @param KeyString the Key of the command line argument to check for
2357
2358 @retval TRUE the flag is on the command line
2359 @retval FALSE the flag is not on the command line
2360 **/
2361BOOLEAN
2362EFIAPI
2363ShellCommandLineGetFlag (
jcarseya405b862010-09-14 05:18:09 +00002364 IN CONST LIST_ENTRY * CONST CheckPackage,
2365 IN CONST CHAR16 * CONST KeyString
2366 )
2367{
jcarsey94b17fa2009-05-07 18:46:18 +00002368 LIST_ENTRY *Node;
jcarsey252d9452011-03-25 20:49:53 +00002369 CHAR16 *TempString;
jcarsey94b17fa2009-05-07 18:46:18 +00002370
2371 //
ydong100c1950b2011-10-13 02:37:35 +00002372 // return FALSE for no package or KeyString is NULL
jcarsey94b17fa2009-05-07 18:46:18 +00002373 //
ydong100c1950b2011-10-13 02:37:35 +00002374 if (CheckPackage == NULL || KeyString == NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00002375 return (FALSE);
2376 }
2377
2378 //
2379 // enumerate through the list of parametrs
2380 //
jcarsey1e6e84c2010-01-25 20:05:08 +00002381 for ( Node = GetFirstNode(CheckPackage)
2382 ; !IsNull (CheckPackage, Node)
2383 ; Node = GetNextNode(CheckPackage, Node)
jcarsey252d9452011-03-25 20:49:53 +00002384 ){
jcarsey94b17fa2009-05-07 18:46:18 +00002385 //
2386 // If the Name matches, return TRUE (and there may be NULL name)
2387 //
2388 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {
jcarsey9eb53ac2009-07-08 17:26:58 +00002389 //
2390 // If Type is TypeStart then only compare the begining of the strings
2391 //
jcarsey252d9452011-03-25 20:49:53 +00002392 if (((SHELL_PARAM_PACKAGE*)Node)->Type == TypeStart) {
2393 if (StrnCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name, StrLen(KeyString)) == 0) {
2394 return (TRUE);
2395 }
2396 TempString = NULL;
2397 TempString = StrnCatGrow(&TempString, NULL, KeyString, StrLen(((SHELL_PARAM_PACKAGE*)Node)->Name));
2398 if (TempString != NULL) {
2399 if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {
2400 FreePool(TempString);
2401 return (TRUE);
2402 }
2403 FreePool(TempString);
2404 }
2405 } else if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {
jcarsey94b17fa2009-05-07 18:46:18 +00002406 return (TRUE);
2407 }
2408 }
2409 }
2410 return (FALSE);
2411}
2412/**
jcarseya405b862010-09-14 05:18:09 +00002413 Returns value from command line argument.
jcarsey94b17fa2009-05-07 18:46:18 +00002414
jcarseya405b862010-09-14 05:18:09 +00002415 Value parameters are in the form of "-<Key> value" or "/<Key> value".
jcarsey1e6e84c2010-01-25 20:05:08 +00002416
jcarseya405b862010-09-14 05:18:09 +00002417 If CheckPackage is NULL, then return NULL.
jcarsey94b17fa2009-05-07 18:46:18 +00002418
jcarseya405b862010-09-14 05:18:09 +00002419 @param[in] CheckPackage The package of parsed command line arguments.
2420 @param[in] KeyString The Key of the command line argument to check for.
jcarsey94b17fa2009-05-07 18:46:18 +00002421
jcarseya405b862010-09-14 05:18:09 +00002422 @retval NULL The flag is not on the command line.
2423 @retval !=NULL The pointer to unicode string of the value.
2424**/
jcarsey94b17fa2009-05-07 18:46:18 +00002425CONST CHAR16*
2426EFIAPI
2427ShellCommandLineGetValue (
2428 IN CONST LIST_ENTRY *CheckPackage,
2429 IN CHAR16 *KeyString
jcarseya405b862010-09-14 05:18:09 +00002430 )
2431{
jcarsey94b17fa2009-05-07 18:46:18 +00002432 LIST_ENTRY *Node;
jcarsey252d9452011-03-25 20:49:53 +00002433 CHAR16 *TempString;
jcarsey94b17fa2009-05-07 18:46:18 +00002434
2435 //
ydong100c1950b2011-10-13 02:37:35 +00002436 // return NULL for no package or KeyString is NULL
jcarsey94b17fa2009-05-07 18:46:18 +00002437 //
ydong100c1950b2011-10-13 02:37:35 +00002438 if (CheckPackage == NULL || KeyString == NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00002439 return (NULL);
2440 }
2441
2442 //
2443 // enumerate through the list of parametrs
2444 //
jcarsey1e6e84c2010-01-25 20:05:08 +00002445 for ( Node = GetFirstNode(CheckPackage)
2446 ; !IsNull (CheckPackage, Node)
2447 ; Node = GetNextNode(CheckPackage, Node)
jcarsey252d9452011-03-25 20:49:53 +00002448 ){
jcarsey94b17fa2009-05-07 18:46:18 +00002449 //
jcarsey252d9452011-03-25 20:49:53 +00002450 // If the Name matches, return TRUE (and there may be NULL name)
jcarsey94b17fa2009-05-07 18:46:18 +00002451 //
2452 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {
jcarsey9eb53ac2009-07-08 17:26:58 +00002453 //
2454 // If Type is TypeStart then only compare the begining of the strings
2455 //
jcarsey252d9452011-03-25 20:49:53 +00002456 if (((SHELL_PARAM_PACKAGE*)Node)->Type == TypeStart) {
2457 if (StrnCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name, StrLen(KeyString)) == 0) {
2458 return (((SHELL_PARAM_PACKAGE*)Node)->Name + StrLen(KeyString));
2459 }
2460 TempString = NULL;
2461 TempString = StrnCatGrow(&TempString, NULL, KeyString, StrLen(((SHELL_PARAM_PACKAGE*)Node)->Name));
2462 if (TempString != NULL) {
2463 if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {
2464 FreePool(TempString);
2465 return (((SHELL_PARAM_PACKAGE*)Node)->Name + StrLen(KeyString));
2466 }
2467 FreePool(TempString);
2468 }
2469 } else if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {
jcarsey94b17fa2009-05-07 18:46:18 +00002470 return (((SHELL_PARAM_PACKAGE*)Node)->Value);
2471 }
2472 }
2473 }
2474 return (NULL);
2475}
jcarseya405b862010-09-14 05:18:09 +00002476
jcarsey94b17fa2009-05-07 18:46:18 +00002477/**
jcarseya405b862010-09-14 05:18:09 +00002478 Returns raw value from command line argument.
jcarsey94b17fa2009-05-07 18:46:18 +00002479
jcarseya405b862010-09-14 05:18:09 +00002480 Raw value parameters are in the form of "value" in a specific position in the list.
jcarsey1e6e84c2010-01-25 20:05:08 +00002481
jcarseya405b862010-09-14 05:18:09 +00002482 If CheckPackage is NULL, then return NULL.
jcarsey94b17fa2009-05-07 18:46:18 +00002483
jcarseya405b862010-09-14 05:18:09 +00002484 @param[in] CheckPackage The package of parsed command line arguments.
2485 @param[in] Position The position of the value.
jcarsey94b17fa2009-05-07 18:46:18 +00002486
jcarseya405b862010-09-14 05:18:09 +00002487 @retval NULL The flag is not on the command line.
2488 @retval !=NULL The pointer to unicode string of the value.
jcarsey94b17fa2009-05-07 18:46:18 +00002489 **/
2490CONST CHAR16*
2491EFIAPI
2492ShellCommandLineGetRawValue (
jcarseya405b862010-09-14 05:18:09 +00002493 IN CONST LIST_ENTRY * CONST CheckPackage,
2494 IN UINTN Position
2495 )
2496{
jcarsey94b17fa2009-05-07 18:46:18 +00002497 LIST_ENTRY *Node;
2498
2499 //
2500 // check for CheckPackage == NULL
2501 //
2502 if (CheckPackage == NULL) {
2503 return (NULL);
2504 }
2505
2506 //
2507 // enumerate through the list of parametrs
2508 //
jcarsey1e6e84c2010-01-25 20:05:08 +00002509 for ( Node = GetFirstNode(CheckPackage)
2510 ; !IsNull (CheckPackage, Node)
2511 ; Node = GetNextNode(CheckPackage, Node)
jcarseya405b862010-09-14 05:18:09 +00002512 ){
jcarsey94b17fa2009-05-07 18:46:18 +00002513 //
2514 // If the position matches, return the value
2515 //
2516 if (((SHELL_PARAM_PACKAGE*)Node)->OriginalPosition == Position) {
2517 return (((SHELL_PARAM_PACKAGE*)Node)->Value);
2518 }
2519 }
2520 return (NULL);
jcarseyb1f95a02009-06-16 00:23:19 +00002521}
jcarsey2247dde2009-11-09 18:08:58 +00002522
2523/**
jcarsey1e6e84c2010-01-25 20:05:08 +00002524 returns the number of command line value parameters that were parsed.
2525
jcarsey2247dde2009-11-09 18:08:58 +00002526 this will not include flags.
2527
jcarseya405b862010-09-14 05:18:09 +00002528 @param[in] CheckPackage The package of parsed command line arguments.
2529
jcarsey2247dde2009-11-09 18:08:58 +00002530 @retval (UINTN)-1 No parsing has ocurred
2531 @return other The number of value parameters found
2532**/
2533UINTN
2534EFIAPI
2535ShellCommandLineGetCount(
jcarseya405b862010-09-14 05:18:09 +00002536 IN CONST LIST_ENTRY *CheckPackage
jcarsey125c2cf2009-11-18 21:36:50 +00002537 )
2538{
jcarseya405b862010-09-14 05:18:09 +00002539 LIST_ENTRY *Node1;
2540 UINTN Count;
2541
2542 if (CheckPackage == NULL) {
2543 return (0);
2544 }
2545 for ( Node1 = GetFirstNode(CheckPackage), Count = 0
2546 ; !IsNull (CheckPackage, Node1)
2547 ; Node1 = GetNextNode(CheckPackage, Node1)
2548 ){
2549 if (((SHELL_PARAM_PACKAGE*)Node1)->Name == NULL) {
2550 Count++;
2551 }
2552 }
2553 return (Count);
jcarsey2247dde2009-11-09 18:08:58 +00002554}
2555
jcarsey975136a2009-06-16 19:03:54 +00002556/**
Bruce Crancceb4eb2015-07-08 01:54:46 +00002557 Determines if a parameter is duplicated.
jcarsey36a9d672009-11-20 21:13:41 +00002558
jcarsey1e6e84c2010-01-25 20:05:08 +00002559 If Param is not NULL then it will point to a callee allocated string buffer
jcarsey36a9d672009-11-20 21:13:41 +00002560 with the parameter value if a duplicate is found.
2561
2562 If CheckPackage is NULL, then ASSERT.
2563
2564 @param[in] CheckPackage The package of parsed command line arguments.
2565 @param[out] Param Upon finding one, a pointer to the duplicated parameter.
2566
2567 @retval EFI_SUCCESS No parameters were duplicated.
2568 @retval EFI_DEVICE_ERROR A duplicate was found.
2569 **/
2570EFI_STATUS
2571EFIAPI
2572ShellCommandLineCheckDuplicate (
2573 IN CONST LIST_ENTRY *CheckPackage,
2574 OUT CHAR16 **Param
2575 )
2576{
2577 LIST_ENTRY *Node1;
2578 LIST_ENTRY *Node2;
jcarsey1e6e84c2010-01-25 20:05:08 +00002579
jcarsey36a9d672009-11-20 21:13:41 +00002580 ASSERT(CheckPackage != NULL);
2581
jcarsey1e6e84c2010-01-25 20:05:08 +00002582 for ( Node1 = GetFirstNode(CheckPackage)
2583 ; !IsNull (CheckPackage, Node1)
2584 ; Node1 = GetNextNode(CheckPackage, Node1)
jcarseya405b862010-09-14 05:18:09 +00002585 ){
jcarsey1e6e84c2010-01-25 20:05:08 +00002586 for ( Node2 = GetNextNode(CheckPackage, Node1)
2587 ; !IsNull (CheckPackage, Node2)
2588 ; Node2 = GetNextNode(CheckPackage, Node2)
jcarseya405b862010-09-14 05:18:09 +00002589 ){
2590 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 +00002591 if (Param != NULL) {
2592 *Param = NULL;
2593 *Param = StrnCatGrow(Param, NULL, ((SHELL_PARAM_PACKAGE*)Node1)->Name, 0);
2594 }
2595 return (EFI_DEVICE_ERROR);
2596 }
2597 }
2598 }
2599 return (EFI_SUCCESS);
2600}
2601
2602/**
jcarsey1e6e84c2010-01-25 20:05:08 +00002603 This is a find and replace function. Upon successful return the NewString is a copy of
jcarsey975136a2009-06-16 19:03:54 +00002604 SourceString with each instance of FindTarget replaced with ReplaceWith.
2605
jcarseyb3011f42010-01-11 21:49:04 +00002606 If SourceString and NewString overlap the behavior is undefined.
2607
jcarsey975136a2009-06-16 19:03:54 +00002608 If the string would grow bigger than NewSize it will halt and return error.
2609
ydong104ff7e372011-09-02 08:05:34 +00002610 @param[in] SourceString The string with source buffer.
2611 @param[in, out] NewString The string with resultant buffer.
2612 @param[in] NewSize The size in bytes of NewString.
2613 @param[in] FindTarget The string to look for.
2614 @param[in] ReplaceWith The string to replace FindTarget with.
2615 @param[in] SkipPreCarrot If TRUE will skip a FindTarget that has a '^'
2616 immediately before it.
2617 @param[in] ParameterReplacing If TRUE will add "" around items with spaces.
jcarsey975136a2009-06-16 19:03:54 +00002618
jcarsey969c7832010-01-13 16:46:33 +00002619 @retval EFI_INVALID_PARAMETER SourceString was NULL.
2620 @retval EFI_INVALID_PARAMETER NewString was NULL.
2621 @retval EFI_INVALID_PARAMETER FindTarget was NULL.
2622 @retval EFI_INVALID_PARAMETER ReplaceWith was NULL.
2623 @retval EFI_INVALID_PARAMETER FindTarget had length < 1.
2624 @retval EFI_INVALID_PARAMETER SourceString had length < 1.
jcarsey1e6e84c2010-01-25 20:05:08 +00002625 @retval EFI_BUFFER_TOO_SMALL NewSize was less than the minimum size to hold
jcarsey969c7832010-01-13 16:46:33 +00002626 the new string (truncation occurred).
jcarseya405b862010-09-14 05:18:09 +00002627 @retval EFI_SUCCESS The string was successfully copied with replacement.
jcarsey975136a2009-06-16 19:03:54 +00002628**/
jcarsey975136a2009-06-16 19:03:54 +00002629EFI_STATUS
2630EFIAPI
jcarseya405b862010-09-14 05:18:09 +00002631ShellCopySearchAndReplace(
jcarsey975136a2009-06-16 19:03:54 +00002632 IN CHAR16 CONST *SourceString,
jcarseya405b862010-09-14 05:18:09 +00002633 IN OUT CHAR16 *NewString,
jcarsey975136a2009-06-16 19:03:54 +00002634 IN UINTN NewSize,
2635 IN CONST CHAR16 *FindTarget,
jcarsey969c7832010-01-13 16:46:33 +00002636 IN CONST CHAR16 *ReplaceWith,
jcarseya405b862010-09-14 05:18:09 +00002637 IN CONST BOOLEAN SkipPreCarrot,
2638 IN CONST BOOLEAN ParameterReplacing
jcarsey1e6e84c2010-01-25 20:05:08 +00002639 )
jcarsey2247dde2009-11-09 18:08:58 +00002640{
jcarsey01582942009-07-10 19:46:17 +00002641 UINTN Size;
jcarseya405b862010-09-14 05:18:09 +00002642 CHAR16 *Replace;
2643
jcarsey975136a2009-06-16 19:03:54 +00002644 if ( (SourceString == NULL)
2645 || (NewString == NULL)
2646 || (FindTarget == NULL)
2647 || (ReplaceWith == NULL)
2648 || (StrLen(FindTarget) < 1)
2649 || (StrLen(SourceString) < 1)
jcarseya405b862010-09-14 05:18:09 +00002650 ){
jcarsey975136a2009-06-16 19:03:54 +00002651 return (EFI_INVALID_PARAMETER);
2652 }
jcarseya405b862010-09-14 05:18:09 +00002653 Replace = NULL;
2654 if (StrStr(ReplaceWith, L" ") == NULL || !ParameterReplacing) {
2655 Replace = StrnCatGrow(&Replace, NULL, ReplaceWith, 0);
2656 } else {
2657 Replace = AllocateZeroPool(StrSize(ReplaceWith) + 2*sizeof(CHAR16));
jcarseybeab0fc2011-10-10 17:26:25 +00002658 if (Replace != NULL) {
2659 UnicodeSPrint(Replace, StrSize(ReplaceWith) + 2*sizeof(CHAR16), L"\"%s\"", ReplaceWith);
2660 }
jcarseya405b862010-09-14 05:18:09 +00002661 }
jcarsey3e082d52010-10-04 16:44:57 +00002662 if (Replace == NULL) {
2663 return (EFI_OUT_OF_RESOURCES);
2664 }
Jaben Carsey98c16be2014-08-19 21:00:34 +00002665 NewString = ZeroMem(NewString, NewSize);
jcarsey2247dde2009-11-09 18:08:58 +00002666 while (*SourceString != CHAR_NULL) {
jcarsey969c7832010-01-13 16:46:33 +00002667 //
jcarseya405b862010-09-14 05:18:09 +00002668 // if we find the FindTarget and either Skip == FALSE or Skip and we
jcarsey969c7832010-01-13 16:46:33 +00002669 // dont have a carrot do a replace...
2670 //
jcarsey1e6e84c2010-01-25 20:05:08 +00002671 if (StrnCmp(SourceString, FindTarget, StrLen(FindTarget)) == 0
jcarseya405b862010-09-14 05:18:09 +00002672 && ((SkipPreCarrot && *(SourceString-1) != L'^') || !SkipPreCarrot)
2673 ){
jcarsey975136a2009-06-16 19:03:54 +00002674 SourceString += StrLen(FindTarget);
jcarsey01582942009-07-10 19:46:17 +00002675 Size = StrSize(NewString);
jcarseya405b862010-09-14 05:18:09 +00002676 if ((Size + (StrLen(Replace)*sizeof(CHAR16))) > NewSize) {
2677 FreePool(Replace);
jcarsey975136a2009-06-16 19:03:54 +00002678 return (EFI_BUFFER_TOO_SMALL);
2679 }
Qiu Shumine75390f2015-06-30 03:18:31 +00002680 StrCatS(NewString, NewSize/sizeof(CHAR16), Replace);
jcarsey975136a2009-06-16 19:03:54 +00002681 } else {
jcarsey01582942009-07-10 19:46:17 +00002682 Size = StrSize(NewString);
2683 if (Size + sizeof(CHAR16) > NewSize) {
jcarseya405b862010-09-14 05:18:09 +00002684 FreePool(Replace);
jcarsey975136a2009-06-16 19:03:54 +00002685 return (EFI_BUFFER_TOO_SMALL);
2686 }
Qiu Shumine75390f2015-06-30 03:18:31 +00002687 StrnCatS(NewString, NewSize/sizeof(CHAR16), SourceString, 1);
jcarsey975136a2009-06-16 19:03:54 +00002688 SourceString++;
2689 }
2690 }
jcarseya405b862010-09-14 05:18:09 +00002691 FreePool(Replace);
jcarsey975136a2009-06-16 19:03:54 +00002692 return (EFI_SUCCESS);
2693}
jcarseyb1f95a02009-06-16 00:23:19 +00002694
2695/**
jcarseye2f82972009-12-01 05:40:24 +00002696 Internal worker function to output a string.
2697
2698 This function will output a string to the correct StdOut.
2699
2700 @param[in] String The string to print out.
2701
2702 @retval EFI_SUCCESS The operation was sucessful.
2703 @retval !EFI_SUCCESS The operation failed.
2704**/
2705EFI_STATUS
2706EFIAPI
2707InternalPrintTo (
2708 IN CONST CHAR16 *String
2709 )
2710{
2711 UINTN Size;
2712 Size = StrSize(String) - sizeof(CHAR16);
jcarseya405b862010-09-14 05:18:09 +00002713 if (Size == 0) {
2714 return (EFI_SUCCESS);
2715 }
jcarsey366f81a2011-06-27 21:04:22 +00002716 if (gEfiShellParametersProtocol != NULL) {
2717 return (gEfiShellProtocol->WriteFile(gEfiShellParametersProtocol->StdOut, &Size, (VOID*)String));
jcarseye2f82972009-12-01 05:40:24 +00002718 }
2719 if (mEfiShellInterface != NULL) {
jcarsey06c355b2012-03-26 21:00:39 +00002720 if (mEfiShellInterface->RedirArgc == 0) {
jcarsey49bd4982012-01-27 18:42:43 +00002721 //
2722 // Divide in half for old shell. Must be string length not size.
jcarsey06c355b2012-03-26 21:00:39 +00002723 //
2724 Size /=2; // Divide in half only when no redirection.
2725 }
jcarseya405b862010-09-14 05:18:09 +00002726 return (mEfiShellInterface->StdOut->Write(mEfiShellInterface->StdOut, &Size, (VOID*)String));
jcarseye2f82972009-12-01 05:40:24 +00002727 }
2728 ASSERT(FALSE);
2729 return (EFI_UNSUPPORTED);
2730}
2731
2732/**
jcarseyb1f95a02009-06-16 00:23:19 +00002733 Print at a specific location on the screen.
2734
jcarseyf1b87e72009-06-17 00:52:11 +00002735 This function will move the cursor to a given screen location and print the specified string
jcarsey1e6e84c2010-01-25 20:05:08 +00002736
2737 If -1 is specified for either the Row or Col the current screen location for BOTH
jcarseyf1b87e72009-06-17 00:52:11 +00002738 will be used.
jcarseyb1f95a02009-06-16 00:23:19 +00002739
2740 if either Row or Col is out of range for the current console, then ASSERT
2741 if Format is NULL, then ASSERT
2742
jcarsey1e6e84c2010-01-25 20:05:08 +00002743 In addition to the standard %-based flags as supported by UefiLib Print() this supports
jcarseyb1f95a02009-06-16 00:23:19 +00002744 the following additional flags:
2745 %N - Set output attribute to normal
2746 %H - Set output attribute to highlight
2747 %E - Set output attribute to error
2748 %B - Set output attribute to blue color
2749 %V - Set output attribute to green color
2750
2751 Note: The background color is controlled by the shell command cls.
2752
jcarseyb1f95a02009-06-16 00:23:19 +00002753 @param[in] Col the column to print at
jcarsey252d9452011-03-25 20:49:53 +00002754 @param[in] Row the row to print at
jcarseyb1f95a02009-06-16 00:23:19 +00002755 @param[in] Format the format string
jcarsey2247dde2009-11-09 18:08:58 +00002756 @param[in] Marker the marker for the variable argument list
jcarseyb1f95a02009-06-16 00:23:19 +00002757
jcarseya405b862010-09-14 05:18:09 +00002758 @return EFI_SUCCESS The operation was successful.
2759 @return EFI_DEVICE_ERROR The console device reported an error.
jcarseyb1f95a02009-06-16 00:23:19 +00002760**/
jcarseya405b862010-09-14 05:18:09 +00002761EFI_STATUS
jcarseyb1f95a02009-06-16 00:23:19 +00002762EFIAPI
jcarsey2247dde2009-11-09 18:08:58 +00002763InternalShellPrintWorker(
jcarseyb1f95a02009-06-16 00:23:19 +00002764 IN INT32 Col OPTIONAL,
2765 IN INT32 Row OPTIONAL,
2766 IN CONST CHAR16 *Format,
jcarsey252d9452011-03-25 20:49:53 +00002767 IN VA_LIST Marker
jcarsey1e6e84c2010-01-25 20:05:08 +00002768 )
jcarsey2247dde2009-11-09 18:08:58 +00002769{
jcarseyb1f95a02009-06-16 00:23:19 +00002770 EFI_STATUS Status;
jcarsey975136a2009-06-16 19:03:54 +00002771 CHAR16 *ResumeLocation;
2772 CHAR16 *FormatWalker;
jcarseya405b862010-09-14 05:18:09 +00002773 UINTN OriginalAttribute;
jcarsey89e85372011-04-13 23:37:50 +00002774 CHAR16 *mPostReplaceFormat;
2775 CHAR16 *mPostReplaceFormat2;
2776
2777 mPostReplaceFormat = AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize));
2778 mPostReplaceFormat2 = AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize));
jcarseya405b862010-09-14 05:18:09 +00002779
jcarseyf8d3e682011-04-19 17:54:42 +00002780 if (mPostReplaceFormat == NULL || mPostReplaceFormat2 == NULL) {
2781 SHELL_FREE_NON_NULL(mPostReplaceFormat);
2782 SHELL_FREE_NON_NULL(mPostReplaceFormat2);
2783 return (EFI_OUT_OF_RESOURCES);
2784 }
2785
jcarseya405b862010-09-14 05:18:09 +00002786 Status = EFI_SUCCESS;
2787 OriginalAttribute = gST->ConOut->Mode->Attribute;
jcarsey1e6e84c2010-01-25 20:05:08 +00002788
jcarsey975136a2009-06-16 19:03:54 +00002789 //
2790 // Back and forth each time fixing up 1 of our flags...
2791 //
jcarseya405b862010-09-14 05:18:09 +00002792 Status = ShellCopySearchAndReplace(Format, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%N", L"%%N", FALSE, FALSE);
jcarsey975136a2009-06-16 19:03:54 +00002793 ASSERT_EFI_ERROR(Status);
jcarseya405b862010-09-14 05:18:09 +00002794 Status = ShellCopySearchAndReplace(mPostReplaceFormat, mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), L"%E", L"%%E", FALSE, FALSE);
jcarsey975136a2009-06-16 19:03:54 +00002795 ASSERT_EFI_ERROR(Status);
jcarseya405b862010-09-14 05:18:09 +00002796 Status = ShellCopySearchAndReplace(mPostReplaceFormat2, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%H", L"%%H", FALSE, FALSE);
jcarsey975136a2009-06-16 19:03:54 +00002797 ASSERT_EFI_ERROR(Status);
jcarseya405b862010-09-14 05:18:09 +00002798 Status = ShellCopySearchAndReplace(mPostReplaceFormat, mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), L"%B", L"%%B", FALSE, FALSE);
jcarsey975136a2009-06-16 19:03:54 +00002799 ASSERT_EFI_ERROR(Status);
jcarseya405b862010-09-14 05:18:09 +00002800 Status = ShellCopySearchAndReplace(mPostReplaceFormat2, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%V", L"%%V", FALSE, FALSE);
jcarsey975136a2009-06-16 19:03:54 +00002801 ASSERT_EFI_ERROR(Status);
2802
2803 //
2804 // Use the last buffer from replacing to print from...
2805 //
jcarseya405b862010-09-14 05:18:09 +00002806 UnicodeVSPrint (mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), mPostReplaceFormat, Marker);
jcarseyb1f95a02009-06-16 00:23:19 +00002807
2808 if (Col != -1 && Row != -1) {
jcarseyb1f95a02009-06-16 00:23:19 +00002809 Status = gST->ConOut->SetCursorPosition(gST->ConOut, Col, Row);
jcarsey975136a2009-06-16 19:03:54 +00002810 }
2811
jcarseyecd3d592009-12-07 18:05:00 +00002812 FormatWalker = mPostReplaceFormat2;
jcarsey2247dde2009-11-09 18:08:58 +00002813 while (*FormatWalker != CHAR_NULL) {
jcarsey975136a2009-06-16 19:03:54 +00002814 //
2815 // Find the next attribute change request
2816 //
2817 ResumeLocation = StrStr(FormatWalker, L"%");
2818 if (ResumeLocation != NULL) {
jcarsey2247dde2009-11-09 18:08:58 +00002819 *ResumeLocation = CHAR_NULL;
jcarsey975136a2009-06-16 19:03:54 +00002820 }
2821 //
2822 // print the current FormatWalker string
2823 //
jcarseya405b862010-09-14 05:18:09 +00002824 if (StrLen(FormatWalker)>0) {
2825 Status = InternalPrintTo(FormatWalker);
2826 if (EFI_ERROR(Status)) {
2827 break;
2828 }
2829 }
2830
jcarsey975136a2009-06-16 19:03:54 +00002831 //
2832 // update the attribute
2833 //
2834 if (ResumeLocation != NULL) {
jcarsey5d46f172011-08-23 15:34:23 +00002835 if (*(ResumeLocation-1) == L'^') {
2836 //
jcarsey8bb74412012-01-30 18:44:41 +00002837 // Move cursor back 1 position to overwrite the ^
2838 //
2839 gST->ConOut->SetCursorPosition(gST->ConOut, gST->ConOut->Mode->CursorColumn - 1, gST->ConOut->Mode->CursorRow);
2840
2841 //
jcarsey5d46f172011-08-23 15:34:23 +00002842 // Print a simple '%' symbol
2843 //
2844 Status = InternalPrintTo(L"%");
2845 ResumeLocation = ResumeLocation - 1;
2846 } else {
2847 switch (*(ResumeLocation+1)) {
2848 case (L'N'):
2849 gST->ConOut->SetAttribute(gST->ConOut, OriginalAttribute);
jcarseya405b862010-09-14 05:18:09 +00002850 break;
jcarsey5d46f172011-08-23 15:34:23 +00002851 case (L'E'):
2852 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_YELLOW, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));
2853 break;
2854 case (L'H'):
2855 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_WHITE, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));
2856 break;
2857 case (L'B'):
2858 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_BLUE, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));
2859 break;
2860 case (L'V'):
2861 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_GREEN, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));
2862 break;
2863 default:
2864 //
2865 // Print a simple '%' symbol
2866 //
2867 Status = InternalPrintTo(L"%");
2868 if (EFI_ERROR(Status)) {
2869 break;
2870 }
2871 ResumeLocation = ResumeLocation - 1;
2872 break;
2873 }
jcarsey975136a2009-06-16 19:03:54 +00002874 }
2875 } else {
2876 //
2877 // reset to normal now...
2878 //
jcarsey975136a2009-06-16 19:03:54 +00002879 break;
2880 }
2881
2882 //
2883 // update FormatWalker to Resume + 2 (skip the % and the indicator)
2884 //
2885 FormatWalker = ResumeLocation + 2;
2886 }
jcarseyb1f95a02009-06-16 00:23:19 +00002887
jcarseya405b862010-09-14 05:18:09 +00002888 gST->ConOut->SetAttribute(gST->ConOut, OriginalAttribute);
jcarsey89e85372011-04-13 23:37:50 +00002889
2890 SHELL_FREE_NON_NULL(mPostReplaceFormat);
2891 SHELL_FREE_NON_NULL(mPostReplaceFormat2);
jcarseya405b862010-09-14 05:18:09 +00002892 return (Status);
jcarsey5f7431d2009-07-10 18:06:01 +00002893}
jcarsey2247dde2009-11-09 18:08:58 +00002894
2895/**
2896 Print at a specific location on the screen.
2897
jcarseye2f82972009-12-01 05:40:24 +00002898 This function will move the cursor to a given screen location and print the specified string.
jcarsey1e6e84c2010-01-25 20:05:08 +00002899
2900 If -1 is specified for either the Row or Col the current screen location for BOTH
jcarsey2247dde2009-11-09 18:08:58 +00002901 will be used.
2902
jcarseye2f82972009-12-01 05:40:24 +00002903 If either Row or Col is out of range for the current console, then ASSERT.
2904 If Format is NULL, then ASSERT.
jcarsey2247dde2009-11-09 18:08:58 +00002905
jcarsey1e6e84c2010-01-25 20:05:08 +00002906 In addition to the standard %-based flags as supported by UefiLib Print() this supports
jcarsey2247dde2009-11-09 18:08:58 +00002907 the following additional flags:
2908 %N - Set output attribute to normal
2909 %H - Set output attribute to highlight
2910 %E - Set output attribute to error
2911 %B - Set output attribute to blue color
2912 %V - Set output attribute to green color
2913
2914 Note: The background color is controlled by the shell command cls.
2915
jcarsey2247dde2009-11-09 18:08:58 +00002916 @param[in] Col the column to print at
jcarseya405b862010-09-14 05:18:09 +00002917 @param[in] Row the row to print at
jcarsey2247dde2009-11-09 18:08:58 +00002918 @param[in] Format the format string
jcarseya405b862010-09-14 05:18:09 +00002919 @param[in] ... The variable argument list.
jcarsey2247dde2009-11-09 18:08:58 +00002920
jcarseya405b862010-09-14 05:18:09 +00002921 @return EFI_SUCCESS The printing was successful.
2922 @return EFI_DEVICE_ERROR The console device reported an error.
jcarsey2247dde2009-11-09 18:08:58 +00002923**/
jcarseya405b862010-09-14 05:18:09 +00002924EFI_STATUS
jcarsey2247dde2009-11-09 18:08:58 +00002925EFIAPI
2926ShellPrintEx(
2927 IN INT32 Col OPTIONAL,
2928 IN INT32 Row OPTIONAL,
2929 IN CONST CHAR16 *Format,
2930 ...
jcarsey1e6e84c2010-01-25 20:05:08 +00002931 )
jcarsey2247dde2009-11-09 18:08:58 +00002932{
2933 VA_LIST Marker;
jcarseya405b862010-09-14 05:18:09 +00002934 EFI_STATUS RetVal;
jcarsey3e082d52010-10-04 16:44:57 +00002935 if (Format == NULL) {
2936 return (EFI_INVALID_PARAMETER);
2937 }
jcarsey2247dde2009-11-09 18:08:58 +00002938 VA_START (Marker, Format);
jcarseya405b862010-09-14 05:18:09 +00002939 RetVal = InternalShellPrintWorker(Col, Row, Format, Marker);
jcarseye2f82972009-12-01 05:40:24 +00002940 VA_END(Marker);
jcarseya405b862010-09-14 05:18:09 +00002941 return(RetVal);
jcarsey2247dde2009-11-09 18:08:58 +00002942}
2943
2944/**
2945 Print at a specific location on the screen.
2946
jcarseye2f82972009-12-01 05:40:24 +00002947 This function will move the cursor to a given screen location and print the specified string.
jcarsey1e6e84c2010-01-25 20:05:08 +00002948
2949 If -1 is specified for either the Row or Col the current screen location for BOTH
jcarseye2f82972009-12-01 05:40:24 +00002950 will be used.
jcarsey2247dde2009-11-09 18:08:58 +00002951
jcarseye2f82972009-12-01 05:40:24 +00002952 If either Row or Col is out of range for the current console, then ASSERT.
2953 If Format is NULL, then ASSERT.
jcarsey2247dde2009-11-09 18:08:58 +00002954
jcarsey1e6e84c2010-01-25 20:05:08 +00002955 In addition to the standard %-based flags as supported by UefiLib Print() this supports
jcarsey2247dde2009-11-09 18:08:58 +00002956 the following additional flags:
jcarsey1e6e84c2010-01-25 20:05:08 +00002957 %N - Set output attribute to normal.
2958 %H - Set output attribute to highlight.
2959 %E - Set output attribute to error.
2960 %B - Set output attribute to blue color.
2961 %V - Set output attribute to green color.
jcarsey2247dde2009-11-09 18:08:58 +00002962
2963 Note: The background color is controlled by the shell command cls.
2964
jcarsey1e6e84c2010-01-25 20:05:08 +00002965 @param[in] Col The column to print at.
jcarseya405b862010-09-14 05:18:09 +00002966 @param[in] Row The row to print at.
jcarsey1e6e84c2010-01-25 20:05:08 +00002967 @param[in] Language The language of the string to retrieve. If this parameter
2968 is NULL, then the current platform language is used.
2969 @param[in] HiiFormatStringId The format string Id for getting from Hii.
2970 @param[in] HiiFormatHandle The format string Handle for getting from Hii.
jcarseya405b862010-09-14 05:18:09 +00002971 @param[in] ... The variable argument list.
jcarsey2247dde2009-11-09 18:08:58 +00002972
jcarseya405b862010-09-14 05:18:09 +00002973 @return EFI_SUCCESS The printing was successful.
2974 @return EFI_DEVICE_ERROR The console device reported an error.
jcarsey2247dde2009-11-09 18:08:58 +00002975**/
jcarseya405b862010-09-14 05:18:09 +00002976EFI_STATUS
jcarsey2247dde2009-11-09 18:08:58 +00002977EFIAPI
2978ShellPrintHiiEx(
2979 IN INT32 Col OPTIONAL,
2980 IN INT32 Row OPTIONAL,
jcarsey1e6e84c2010-01-25 20:05:08 +00002981 IN CONST CHAR8 *Language OPTIONAL,
jcarsey2247dde2009-11-09 18:08:58 +00002982 IN CONST EFI_STRING_ID HiiFormatStringId,
2983 IN CONST EFI_HANDLE HiiFormatHandle,
2984 ...
2985 )
2986{
2987 VA_LIST Marker;
2988 CHAR16 *HiiFormatString;
jcarseya405b862010-09-14 05:18:09 +00002989 EFI_STATUS RetVal;
jcarsey2247dde2009-11-09 18:08:58 +00002990
Ruiyu Nieeb97442016-07-14 13:15:34 +08002991 RetVal = EFI_DEVICE_ERROR;
2992
jcarsey2247dde2009-11-09 18:08:58 +00002993 VA_START (Marker, HiiFormatHandle);
jcarsey1e6e84c2010-01-25 20:05:08 +00002994 HiiFormatString = HiiGetString(HiiFormatHandle, HiiFormatStringId, Language);
Ruiyu Nieeb97442016-07-14 13:15:34 +08002995 if (HiiFormatString != NULL) {
2996 RetVal = InternalShellPrintWorker (Col, Row, HiiFormatString, Marker);
2997 SHELL_FREE_NON_NULL (HiiFormatString);
2998 }
jcarseye2f82972009-12-01 05:40:24 +00002999 VA_END(Marker);
jcarsey2247dde2009-11-09 18:08:58 +00003000
3001 return (RetVal);
3002}
3003
3004/**
3005 Function to determine if a given filename represents a file or a directory.
3006
3007 @param[in] DirName Path to directory to test.
3008
jcarseyc8c22592011-10-17 17:49:21 +00003009 @retval EFI_SUCCESS The Path represents a directory
3010 @retval EFI_NOT_FOUND The Path does not represent a directory
3011 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
3012 @return The path failed to open
jcarsey2247dde2009-11-09 18:08:58 +00003013**/
3014EFI_STATUS
3015EFIAPI
3016ShellIsDirectory(
3017 IN CONST CHAR16 *DirName
3018 )
3019{
3020 EFI_STATUS Status;
jcarseya405b862010-09-14 05:18:09 +00003021 SHELL_FILE_HANDLE Handle;
jcarsey3e082d52010-10-04 16:44:57 +00003022 CHAR16 *TempLocation;
3023 CHAR16 *TempLocation2;
jcarsey2247dde2009-11-09 18:08:58 +00003024
jcarseyecd3d592009-12-07 18:05:00 +00003025 ASSERT(DirName != NULL);
3026
jcarseya405b862010-09-14 05:18:09 +00003027 Handle = NULL;
3028 TempLocation = NULL;
jcarsey2247dde2009-11-09 18:08:58 +00003029
3030 Status = ShellOpenFileByName(DirName, &Handle, EFI_FILE_MODE_READ, 0);
3031 if (EFI_ERROR(Status)) {
jcarseya405b862010-09-14 05:18:09 +00003032 //
3033 // try good logic first.
3034 //
jcarsey366f81a2011-06-27 21:04:22 +00003035 if (gEfiShellProtocol != NULL) {
jcarsey3e082d52010-10-04 16:44:57 +00003036 TempLocation = StrnCatGrow(&TempLocation, NULL, DirName, 0);
jcarseyc8c22592011-10-17 17:49:21 +00003037 if (TempLocation == NULL) {
3038 ShellCloseFile(&Handle);
3039 return (EFI_OUT_OF_RESOURCES);
3040 }
jcarsey3e082d52010-10-04 16:44:57 +00003041 TempLocation2 = StrStr(TempLocation, L":");
3042 if (TempLocation2 != NULL && StrLen(StrStr(TempLocation, L":")) == 2) {
3043 *(TempLocation2+1) = CHAR_NULL;
jcarseya405b862010-09-14 05:18:09 +00003044 }
jcarsey366f81a2011-06-27 21:04:22 +00003045 if (gEfiShellProtocol->GetDevicePathFromMap(TempLocation) != NULL) {
jcarseya405b862010-09-14 05:18:09 +00003046 FreePool(TempLocation);
3047 return (EFI_SUCCESS);
3048 }
3049 FreePool(TempLocation);
3050 } else {
3051 //
3052 // probably a map name?!?!!?
3053 //
3054 TempLocation = StrStr(DirName, L"\\");
3055 if (TempLocation != NULL && *(TempLocation+1) == CHAR_NULL) {
3056 return (EFI_SUCCESS);
3057 }
3058 }
jcarsey2247dde2009-11-09 18:08:58 +00003059 return (Status);
3060 }
3061
3062 if (FileHandleIsDirectory(Handle) == EFI_SUCCESS) {
3063 ShellCloseFile(&Handle);
3064 return (EFI_SUCCESS);
3065 }
3066 ShellCloseFile(&Handle);
3067 return (EFI_NOT_FOUND);
3068}
3069
jcarsey125c2cf2009-11-18 21:36:50 +00003070/**
jcarsey36a9d672009-11-20 21:13:41 +00003071 Function to determine if a given filename represents a file.
3072
3073 @param[in] Name Path to file to test.
3074
3075 @retval EFI_SUCCESS The Path represents a file.
3076 @retval EFI_NOT_FOUND The Path does not represent a file.
3077 @retval other The path failed to open.
3078**/
3079EFI_STATUS
3080EFIAPI
3081ShellIsFile(
3082 IN CONST CHAR16 *Name
3083 )
3084{
3085 EFI_STATUS Status;
jcarseya405b862010-09-14 05:18:09 +00003086 SHELL_FILE_HANDLE Handle;
jcarsey36a9d672009-11-20 21:13:41 +00003087
jcarseyecd3d592009-12-07 18:05:00 +00003088 ASSERT(Name != NULL);
3089
jcarsey36a9d672009-11-20 21:13:41 +00003090 Handle = NULL;
3091
3092 Status = ShellOpenFileByName(Name, &Handle, EFI_FILE_MODE_READ, 0);
3093 if (EFI_ERROR(Status)) {
3094 return (Status);
3095 }
3096
3097 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {
3098 ShellCloseFile(&Handle);
3099 return (EFI_SUCCESS);
3100 }
3101 ShellCloseFile(&Handle);
3102 return (EFI_NOT_FOUND);
3103}
3104
3105/**
jcarseyb3011f42010-01-11 21:49:04 +00003106 Function to determine if a given filename represents a file.
3107
3108 This will search the CWD and then the Path.
3109
3110 If Name is NULL, then ASSERT.
3111
3112 @param[in] Name Path to file to test.
3113
3114 @retval EFI_SUCCESS The Path represents a file.
3115 @retval EFI_NOT_FOUND The Path does not represent a file.
3116 @retval other The path failed to open.
3117**/
3118EFI_STATUS
3119EFIAPI
3120ShellIsFileInPath(
3121 IN CONST CHAR16 *Name
jcarseya405b862010-09-14 05:18:09 +00003122 )
3123{
jcarseyb3011f42010-01-11 21:49:04 +00003124 CHAR16 *NewName;
3125 EFI_STATUS Status;
3126
3127 if (!EFI_ERROR(ShellIsFile(Name))) {
jcarseya405b862010-09-14 05:18:09 +00003128 return (EFI_SUCCESS);
jcarseyb3011f42010-01-11 21:49:04 +00003129 }
3130
3131 NewName = ShellFindFilePath(Name);
3132 if (NewName == NULL) {
3133 return (EFI_NOT_FOUND);
3134 }
3135 Status = ShellIsFile(NewName);
3136 FreePool(NewName);
3137 return (Status);
3138}
jcarsey252d9452011-03-25 20:49:53 +00003139
jcarseyb3011f42010-01-11 21:49:04 +00003140/**
Jaben Carsey74b0fb82013-11-22 21:37:34 +00003141 Function return the number converted from a hex representation of a number.
3142
3143 Note: this function cannot be used when (UINTN)(-1), (0xFFFFFFFF) may be a valid
3144 result. Use ShellConvertStringToUint64 instead.
3145
3146 @param[in] String String representation of a number.
3147
3148 @return The unsigned integer result of the conversion.
3149 @retval (UINTN)(-1) An error occured.
3150**/
3151UINTN
3152EFIAPI
3153ShellHexStrToUintn(
3154 IN CONST CHAR16 *String
3155 )
3156{
3157 UINT64 RetVal;
3158
3159 if (!EFI_ERROR(ShellConvertStringToUint64(String, &RetVal, TRUE, TRUE))) {
3160 return ((UINTN)RetVal);
3161 }
3162
3163 return ((UINTN)(-1));
3164}
3165
3166/**
jcarsey1e6e84c2010-01-25 20:05:08 +00003167 Function to determine whether a string is decimal or hex representation of a number
Jaben Carseyd59fc242013-11-14 23:52:43 +00003168 and return the number converted from the string. Spaces are always skipped.
jcarsey125c2cf2009-11-18 21:36:50 +00003169
3170 @param[in] String String representation of a number
3171
jcarsey252d9452011-03-25 20:49:53 +00003172 @return the number
3173 @retval (UINTN)(-1) An error ocurred.
jcarsey125c2cf2009-11-18 21:36:50 +00003174**/
3175UINTN
3176EFIAPI
3177ShellStrToUintn(
3178 IN CONST CHAR16 *String
3179 )
3180{
jcarsey252d9452011-03-25 20:49:53 +00003181 UINT64 RetVal;
3182 BOOLEAN Hex;
3183
3184 Hex = FALSE;
3185
jcarsey658bf432014-11-04 22:33:16 +00003186 if (!InternalShellIsHexOrDecimalNumber(String, Hex, TRUE, FALSE)) {
jcarsey252d9452011-03-25 20:49:53 +00003187 Hex = TRUE;
jcarsey125c2cf2009-11-18 21:36:50 +00003188 }
jcarsey252d9452011-03-25 20:49:53 +00003189
3190 if (!EFI_ERROR(ShellConvertStringToUint64(String, &RetVal, Hex, TRUE))) {
3191 return ((UINTN)RetVal);
3192 }
3193 return ((UINTN)(-1));
jcarsey125c2cf2009-11-18 21:36:50 +00003194}
3195
3196/**
3197 Safely append with automatic string resizing given length of Destination and
3198 desired length of copy from Source.
3199
3200 append the first D characters of Source to the end of Destination, where D is
3201 the lesser of Count and the StrLen() of Source. If appending those D characters
3202 will fit within Destination (whose Size is given as CurrentSize) and
jcarsey1e6e84c2010-01-25 20:05:08 +00003203 still leave room for a NULL terminator, then those characters are appended,
3204 starting at the original terminating NULL of Destination, and a new terminating
3205 NULL is appended.
jcarsey125c2cf2009-11-18 21:36:50 +00003206
3207 If appending D characters onto Destination will result in a overflow of the size
3208 given in CurrentSize the string will be grown such that the copy can be performed
3209 and CurrentSize will be updated to the new size.
3210
3211 If Source is NULL, there is nothing to append, just return the current buffer in
3212 Destination.
3213
3214 if Destination is NULL, then ASSERT()
3215 if Destination's current length (including NULL terminator) is already more then
3216 CurrentSize, then ASSERT()
3217
ydong104ff7e372011-09-02 08:05:34 +00003218 @param[in, out] Destination The String to append onto
3219 @param[in, out] CurrentSize on call the number of bytes in Destination. On
jcarsey125c2cf2009-11-18 21:36:50 +00003220 return possibly the new size (still in bytes). if NULL
3221 then allocate whatever is needed.
3222 @param[in] Source The String to append from
3223 @param[in] Count Maximum number of characters to append. if 0 then
3224 all are appended.
3225
3226 @return Destination return the resultant string.
3227**/
3228CHAR16*
3229EFIAPI
3230StrnCatGrow (
3231 IN OUT CHAR16 **Destination,
3232 IN OUT UINTN *CurrentSize,
3233 IN CONST CHAR16 *Source,
3234 IN UINTN Count
3235 )
3236{
3237 UINTN DestinationStartSize;
3238 UINTN NewSize;
3239
3240 //
3241 // ASSERTs
3242 //
3243 ASSERT(Destination != NULL);
3244
3245 //
3246 // If there's nothing to do then just return Destination
3247 //
3248 if (Source == NULL) {
3249 return (*Destination);
3250 }
3251
3252 //
3253 // allow for un-initialized pointers, based on size being 0
3254 //
3255 if (CurrentSize != NULL && *CurrentSize == 0) {
3256 *Destination = NULL;
3257 }
3258
3259 //
3260 // allow for NULL pointers address as Destination
3261 //
3262 if (*Destination != NULL) {
3263 ASSERT(CurrentSize != 0);
3264 DestinationStartSize = StrSize(*Destination);
3265 ASSERT(DestinationStartSize <= *CurrentSize);
3266 } else {
3267 DestinationStartSize = 0;
3268// ASSERT(*CurrentSize == 0);
3269 }
3270
3271 //
3272 // Append all of Source?
3273 //
3274 if (Count == 0) {
3275 Count = StrLen(Source);
3276 }
3277
3278 //
3279 // Test and grow if required
3280 //
3281 if (CurrentSize != NULL) {
3282 NewSize = *CurrentSize;
ydong10f480fdc2012-09-07 01:55:33 +00003283 if (NewSize < DestinationStartSize + (Count * sizeof(CHAR16))) {
3284 while (NewSize < (DestinationStartSize + (Count*sizeof(CHAR16)))) {
3285 NewSize += 2 * Count * sizeof(CHAR16);
3286 }
3287 *Destination = ReallocatePool(*CurrentSize, NewSize, *Destination);
3288 *CurrentSize = NewSize;
jcarsey125c2cf2009-11-18 21:36:50 +00003289 }
jcarsey125c2cf2009-11-18 21:36:50 +00003290 } else {
Qiu Shuminc587fd32015-07-02 01:12:03 +00003291 NewSize = (Count+1)*sizeof(CHAR16);
3292 *Destination = AllocateZeroPool(NewSize);
jcarsey125c2cf2009-11-18 21:36:50 +00003293 }
3294
3295 //
3296 // Now use standard StrnCat on a big enough buffer
3297 //
jcarseyc9d92df2010-02-03 15:37:54 +00003298 if (*Destination == NULL) {
3299 return (NULL);
3300 }
Qiu Shumine75390f2015-06-30 03:18:31 +00003301
Qiu Shuminc587fd32015-07-02 01:12:03 +00003302 StrnCatS(*Destination, NewSize/sizeof(CHAR16), Source, Count);
Qiu Shumine75390f2015-06-30 03:18:31 +00003303 return *Destination;
jcarsey125c2cf2009-11-18 21:36:50 +00003304}
jcarseyc9d92df2010-02-03 15:37:54 +00003305
3306/**
3307 Prompt the user and return the resultant answer to the requestor.
3308
3309 This function will display the requested question on the shell prompt and then
Mudusuru, Giri P1d322462016-07-07 00:47:45 -07003310 wait for an appropriate answer to be input from the console.
jcarseyc9d92df2010-02-03 15:37:54 +00003311
jcarseya405b862010-09-14 05:18:09 +00003312 if the SHELL_PROMPT_REQUEST_TYPE is SHELL_PROMPT_REQUEST_TYPE_YESNO, ShellPromptResponseTypeQuitContinue
jcarseyc9d92df2010-02-03 15:37:54 +00003313 or SHELL_PROMPT_REQUEST_TYPE_YESNOCANCEL then *Response is of type SHELL_PROMPT_RESPONSE.
3314
jcarseya405b862010-09-14 05:18:09 +00003315 if the SHELL_PROMPT_REQUEST_TYPE is ShellPromptResponseTypeFreeform then *Response is of type
jcarseyc9d92df2010-02-03 15:37:54 +00003316 CHAR16*.
3317
3318 In either case *Response must be callee freed if Response was not NULL;
3319
3320 @param Type What type of question is asked. This is used to filter the input
3321 to prevent invalid answers to question.
3322 @param Prompt Pointer to string prompt to use to request input.
3323 @param Response Pointer to Response which will be populated upon return.
3324
3325 @retval EFI_SUCCESS The operation was sucessful.
3326 @retval EFI_UNSUPPORTED The operation is not supported as requested.
3327 @retval EFI_INVALID_PARAMETER A parameter was invalid.
3328 @return other The operation failed.
3329**/
3330EFI_STATUS
3331EFIAPI
3332ShellPromptForResponse (
3333 IN SHELL_PROMPT_REQUEST_TYPE Type,
3334 IN CHAR16 *Prompt OPTIONAL,
3335 IN OUT VOID **Response OPTIONAL
3336 )
3337{
3338 EFI_STATUS Status;
3339 EFI_INPUT_KEY Key;
3340 UINTN EventIndex;
3341 SHELL_PROMPT_RESPONSE *Resp;
jcarseya405b862010-09-14 05:18:09 +00003342 UINTN Size;
3343 CHAR16 *Buffer;
jcarseyc9d92df2010-02-03 15:37:54 +00003344
jcarseya405b862010-09-14 05:18:09 +00003345 Status = EFI_UNSUPPORTED;
3346 Resp = NULL;
3347 Buffer = NULL;
3348 Size = 0;
3349 if (Type != ShellPromptResponseTypeFreeform) {
jcarsey252d9452011-03-25 20:49:53 +00003350 Resp = (SHELL_PROMPT_RESPONSE*)AllocateZeroPool(sizeof(SHELL_PROMPT_RESPONSE));
jcarsey3e082d52010-10-04 16:44:57 +00003351 if (Resp == NULL) {
3352 return (EFI_OUT_OF_RESOURCES);
3353 }
xdu290bfa222010-07-19 05:21:27 +00003354 }
jcarseyc9d92df2010-02-03 15:37:54 +00003355
3356 switch(Type) {
jcarseya405b862010-09-14 05:18:09 +00003357 case ShellPromptResponseTypeQuitContinue:
jcarseyc9d92df2010-02-03 15:37:54 +00003358 if (Prompt != NULL) {
3359 ShellPrintEx(-1, -1, L"%s", Prompt);
3360 }
3361 //
3362 // wait for valid response
3363 //
3364 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
3365 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
Jaben Carsey31b018a2014-01-16 16:52:39 +00003366 if (EFI_ERROR(Status)) {
3367 break;
3368 }
jcarseyc9d92df2010-02-03 15:37:54 +00003369 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3370 if (Key.UnicodeChar == L'Q' || Key.UnicodeChar ==L'q') {
jcarseya405b862010-09-14 05:18:09 +00003371 *Resp = ShellPromptResponseQuit;
jcarseyc9d92df2010-02-03 15:37:54 +00003372 } else {
jcarseya405b862010-09-14 05:18:09 +00003373 *Resp = ShellPromptResponseContinue;
jcarseyc9d92df2010-02-03 15:37:54 +00003374 }
3375 break;
jcarseya405b862010-09-14 05:18:09 +00003376 case ShellPromptResponseTypeYesNoCancel:
jcarseyc9d92df2010-02-03 15:37:54 +00003377 if (Prompt != NULL) {
3378 ShellPrintEx(-1, -1, L"%s", Prompt);
3379 }
3380 //
3381 // wait for valid response
3382 //
jcarseya405b862010-09-14 05:18:09 +00003383 *Resp = ShellPromptResponseMax;
3384 while (*Resp == ShellPromptResponseMax) {
Jaben Carsey194ae482013-12-09 22:55:13 +00003385 if (ShellGetExecutionBreakFlag()) {
3386 Status = EFI_ABORTED;
3387 break;
3388 }
jcarseyc9d92df2010-02-03 15:37:54 +00003389 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
3390 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
Jaben Carsey31b018a2014-01-16 16:52:39 +00003391 if (EFI_ERROR(Status)) {
3392 break;
3393 }
jcarseyc9d92df2010-02-03 15:37:54 +00003394 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3395 switch (Key.UnicodeChar) {
3396 case L'Y':
3397 case L'y':
jcarseya405b862010-09-14 05:18:09 +00003398 *Resp = ShellPromptResponseYes;
jcarseyc9d92df2010-02-03 15:37:54 +00003399 break;
3400 case L'N':
3401 case L'n':
jcarseya405b862010-09-14 05:18:09 +00003402 *Resp = ShellPromptResponseNo;
jcarseyc9d92df2010-02-03 15:37:54 +00003403 break;
3404 case L'C':
3405 case L'c':
jcarseya405b862010-09-14 05:18:09 +00003406 *Resp = ShellPromptResponseCancel;
3407 break;
3408 }
3409 }
3410 break; case ShellPromptResponseTypeYesNoAllCancel:
3411 if (Prompt != NULL) {
3412 ShellPrintEx(-1, -1, L"%s", Prompt);
3413 }
3414 //
3415 // wait for valid response
3416 //
3417 *Resp = ShellPromptResponseMax;
3418 while (*Resp == ShellPromptResponseMax) {
Jaben Carsey194ae482013-12-09 22:55:13 +00003419 if (ShellGetExecutionBreakFlag()) {
3420 Status = EFI_ABORTED;
3421 break;
3422 }
jcarseya405b862010-09-14 05:18:09 +00003423 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
3424 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
Jaben Carsey31b018a2014-01-16 16:52:39 +00003425 if (EFI_ERROR(Status)) {
3426 break;
3427 }
jcarseya405b862010-09-14 05:18:09 +00003428 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3429 switch (Key.UnicodeChar) {
3430 case L'Y':
3431 case L'y':
3432 *Resp = ShellPromptResponseYes;
3433 break;
3434 case L'N':
3435 case L'n':
3436 *Resp = ShellPromptResponseNo;
3437 break;
3438 case L'A':
3439 case L'a':
3440 *Resp = ShellPromptResponseAll;
3441 break;
3442 case L'C':
3443 case L'c':
3444 *Resp = ShellPromptResponseCancel;
jcarseyc9d92df2010-02-03 15:37:54 +00003445 break;
3446 }
3447 }
3448 break;
jcarseya405b862010-09-14 05:18:09 +00003449 case ShellPromptResponseTypeEnterContinue:
3450 case ShellPromptResponseTypeAnyKeyContinue:
jcarseyc9d92df2010-02-03 15:37:54 +00003451 if (Prompt != NULL) {
3452 ShellPrintEx(-1, -1, L"%s", Prompt);
3453 }
3454 //
3455 // wait for valid response
3456 //
jcarseya405b862010-09-14 05:18:09 +00003457 *Resp = ShellPromptResponseMax;
3458 while (*Resp == ShellPromptResponseMax) {
Jaben Carsey194ae482013-12-09 22:55:13 +00003459 if (ShellGetExecutionBreakFlag()) {
3460 Status = EFI_ABORTED;
3461 break;
3462 }
jcarseyc9d92df2010-02-03 15:37:54 +00003463 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
jcarseya405b862010-09-14 05:18:09 +00003464 if (Type == ShellPromptResponseTypeEnterContinue) {
jcarseyc9d92df2010-02-03 15:37:54 +00003465 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
Jaben Carsey31b018a2014-01-16 16:52:39 +00003466 if (EFI_ERROR(Status)) {
3467 break;
3468 }
jcarseyc9d92df2010-02-03 15:37:54 +00003469 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3470 if (Key.UnicodeChar == CHAR_CARRIAGE_RETURN) {
jcarseya405b862010-09-14 05:18:09 +00003471 *Resp = ShellPromptResponseContinue;
jcarseyc9d92df2010-02-03 15:37:54 +00003472 break;
3473 }
3474 }
jcarseya405b862010-09-14 05:18:09 +00003475 if (Type == ShellPromptResponseTypeAnyKeyContinue) {
3476 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
3477 ASSERT_EFI_ERROR(Status);
3478 *Resp = ShellPromptResponseContinue;
jcarseyc9d92df2010-02-03 15:37:54 +00003479 break;
3480 }
3481 }
3482 break;
jcarseya405b862010-09-14 05:18:09 +00003483 case ShellPromptResponseTypeYesNo:
3484 if (Prompt != NULL) {
3485 ShellPrintEx(-1, -1, L"%s", Prompt);
3486 }
3487 //
3488 // wait for valid response
3489 //
3490 *Resp = ShellPromptResponseMax;
3491 while (*Resp == ShellPromptResponseMax) {
Jaben Carsey194ae482013-12-09 22:55:13 +00003492 if (ShellGetExecutionBreakFlag()) {
3493 Status = EFI_ABORTED;
3494 break;
3495 }
jcarseya405b862010-09-14 05:18:09 +00003496 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
3497 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
Jaben Carsey31b018a2014-01-16 16:52:39 +00003498 if (EFI_ERROR(Status)) {
3499 break;
3500 }
jcarseya405b862010-09-14 05:18:09 +00003501 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3502 switch (Key.UnicodeChar) {
3503 case L'Y':
3504 case L'y':
3505 *Resp = ShellPromptResponseYes;
3506 break;
3507 case L'N':
3508 case L'n':
3509 *Resp = ShellPromptResponseNo;
3510 break;
3511 }
3512 }
3513 break;
3514 case ShellPromptResponseTypeFreeform:
3515 if (Prompt != NULL) {
3516 ShellPrintEx(-1, -1, L"%s", Prompt);
3517 }
3518 while(1) {
Jaben Carsey194ae482013-12-09 22:55:13 +00003519 if (ShellGetExecutionBreakFlag()) {
3520 Status = EFI_ABORTED;
3521 break;
3522 }
jcarseya405b862010-09-14 05:18:09 +00003523 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
3524 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
Jaben Carsey31b018a2014-01-16 16:52:39 +00003525 if (EFI_ERROR(Status)) {
3526 break;
3527 }
jcarseya405b862010-09-14 05:18:09 +00003528 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3529 if (Key.UnicodeChar == CHAR_CARRIAGE_RETURN) {
3530 break;
3531 }
3532 ASSERT((Buffer == NULL && Size == 0) || (Buffer != NULL));
3533 StrnCatGrow(&Buffer, &Size, &Key.UnicodeChar, 1);
3534 }
3535 break;
3536 //
3537 // This is the location to add new prompt types.
Jaben Carsey194ae482013-12-09 22:55:13 +00003538 // If your new type loops remember to add ExecutionBreak support.
jcarseya405b862010-09-14 05:18:09 +00003539 //
jcarseyc9d92df2010-02-03 15:37:54 +00003540 default:
jcarseya405b862010-09-14 05:18:09 +00003541 ASSERT(FALSE);
jcarseyc9d92df2010-02-03 15:37:54 +00003542 }
3543
3544 if (Response != NULL) {
jcarseya405b862010-09-14 05:18:09 +00003545 if (Resp != NULL) {
3546 *Response = Resp;
3547 } else if (Buffer != NULL) {
3548 *Response = Buffer;
3549 }
jcarseyc9d92df2010-02-03 15:37:54 +00003550 } else {
jcarseya405b862010-09-14 05:18:09 +00003551 if (Resp != NULL) {
3552 FreePool(Resp);
3553 }
3554 if (Buffer != NULL) {
3555 FreePool(Buffer);
3556 }
jcarseyc9d92df2010-02-03 15:37:54 +00003557 }
3558
jcarseya405b862010-09-14 05:18:09 +00003559 ShellPrintEx(-1, -1, L"\r\n");
jcarseyc9d92df2010-02-03 15:37:54 +00003560 return (Status);
3561}
3562
3563/**
3564 Prompt the user and return the resultant answer to the requestor.
3565
3566 This function is the same as ShellPromptForResponse, except that the prompt is
3567 automatically pulled from HII.
3568
3569 @param Type What type of question is asked. This is used to filter the input
3570 to prevent invalid answers to question.
jcarseya405b862010-09-14 05:18:09 +00003571 @param[in] HiiFormatStringId The format string Id for getting from Hii.
3572 @param[in] HiiFormatHandle The format string Handle for getting from Hii.
3573 @param Response Pointer to Response which will be populated upon return.
jcarseyc9d92df2010-02-03 15:37:54 +00003574
3575 @retval EFI_SUCCESS the operation was sucessful.
3576 @return other the operation failed.
3577
3578 @sa ShellPromptForResponse
3579**/
3580EFI_STATUS
3581EFIAPI
3582ShellPromptForResponseHii (
3583 IN SHELL_PROMPT_REQUEST_TYPE Type,
3584 IN CONST EFI_STRING_ID HiiFormatStringId,
3585 IN CONST EFI_HANDLE HiiFormatHandle,
3586 IN OUT VOID **Response
3587 )
3588{
3589 CHAR16 *Prompt;
3590 EFI_STATUS Status;
3591
3592 Prompt = HiiGetString(HiiFormatHandle, HiiFormatStringId, NULL);
3593 Status = ShellPromptForResponse(Type, Prompt, Response);
3594 FreePool(Prompt);
3595 return (Status);
3596}
3597
jcarseya405b862010-09-14 05:18:09 +00003598/**
3599 Function to determin if an entire string is a valid number.
jcarseyc9d92df2010-02-03 15:37:54 +00003600
jcarseya405b862010-09-14 05:18:09 +00003601 If Hex it must be preceeded with a 0x or has ForceHex, set TRUE.
3602
3603 @param[in] String The string to evaluate.
3604 @param[in] ForceHex TRUE - always assume hex.
3605 @param[in] StopAtSpace TRUE to halt upon finding a space, FALSE to keep going.
jcarsey658bf432014-11-04 22:33:16 +00003606 @param[in] TimeNumbers TRUE to allow numbers with ":", FALSE otherwise.
jcarseya405b862010-09-14 05:18:09 +00003607
3608 @retval TRUE It is all numeric (dec/hex) characters.
3609 @retval FALSE There is a non-numeric character.
3610**/
3611BOOLEAN
3612EFIAPI
jcarsey252d9452011-03-25 20:49:53 +00003613InternalShellIsHexOrDecimalNumber (
jcarseya405b862010-09-14 05:18:09 +00003614 IN CONST CHAR16 *String,
3615 IN CONST BOOLEAN ForceHex,
jcarsey658bf432014-11-04 22:33:16 +00003616 IN CONST BOOLEAN StopAtSpace,
3617 IN CONST BOOLEAN TimeNumbers
jcarseya405b862010-09-14 05:18:09 +00003618 )
3619{
3620 BOOLEAN Hex;
3621
3622 //
3623 // chop off a single negative sign
3624 //
3625 if (String != NULL && *String == L'-') {
3626 String++;
3627 }
darylm503b0934ac2011-11-12 00:35:11 +00003628
jcarseya405b862010-09-14 05:18:09 +00003629 if (String == NULL) {
3630 return (FALSE);
3631 }
3632
3633 //
3634 // chop leading zeroes
3635 //
3636 while(String != NULL && *String == L'0'){
3637 String++;
3638 }
3639 //
3640 // allow '0x' or '0X', but not 'x' or 'X'
3641 //
3642 if (String != NULL && (*String == L'x' || *String == L'X')) {
3643 if (*(String-1) != L'0') {
3644 //
3645 // we got an x without a preceeding 0
3646 //
3647 return (FALSE);
3648 }
3649 String++;
3650 Hex = TRUE;
3651 } else if (ForceHex) {
3652 Hex = TRUE;
3653 } else {
3654 Hex = FALSE;
3655 }
3656
3657 //
3658 // loop through the remaining characters and use the lib function
3659 //
3660 for ( ; String != NULL && *String != CHAR_NULL && !(StopAtSpace && *String == L' ') ; String++){
jcarsey658bf432014-11-04 22:33:16 +00003661 if (TimeNumbers && (String[0] == L':')) {
3662 continue;
3663 }
jcarseya405b862010-09-14 05:18:09 +00003664 if (Hex) {
3665 if (!ShellIsHexaDecimalDigitCharacter(*String)) {
3666 return (FALSE);
3667 }
3668 } else {
3669 if (!ShellIsDecimalDigitCharacter(*String)) {
3670 return (FALSE);
3671 }
3672 }
3673 }
jcarsey252d9452011-03-25 20:49:53 +00003674
jcarseya405b862010-09-14 05:18:09 +00003675 return (TRUE);
3676}
3677
3678/**
3679 Function to determine if a given filename exists.
3680
3681 @param[in] Name Path to test.
3682
3683 @retval EFI_SUCCESS The Path represents a file.
3684 @retval EFI_NOT_FOUND The Path does not represent a file.
3685 @retval other The path failed to open.
3686**/
3687EFI_STATUS
3688EFIAPI
3689ShellFileExists(
3690 IN CONST CHAR16 *Name
3691 )
3692{
3693 EFI_STATUS Status;
3694 EFI_SHELL_FILE_INFO *List;
3695
3696 ASSERT(Name != NULL);
3697
3698 List = NULL;
3699 Status = ShellOpenFileMetaArg((CHAR16*)Name, EFI_FILE_MODE_READ, &List);
3700 if (EFI_ERROR(Status)) {
3701 return (Status);
3702 }
3703
3704 ShellCloseFileMetaArg(&List);
3705
3706 return (EFI_SUCCESS);
3707}
jcarsey252d9452011-03-25 20:49:53 +00003708
3709/**
darylm503b0934ac2011-11-12 00:35:11 +00003710 Convert a Unicode character to upper case only if
jcarsey252d9452011-03-25 20:49:53 +00003711 it maps to a valid small-case ASCII character.
3712
3713 This internal function only deal with Unicode character
3714 which maps to a valid small-case ASCII character, i.e.
3715 L'a' to L'z'. For other Unicode character, the input character
3716 is returned directly.
3717
3718 @param Char The character to convert.
3719
3720 @retval LowerCharacter If the Char is with range L'a' to L'z'.
3721 @retval Unchanged Otherwise.
3722
3723**/
3724CHAR16
3725EFIAPI
3726InternalShellCharToUpper (
3727 IN CHAR16 Char
3728 )
3729{
3730 if (Char >= L'a' && Char <= L'z') {
3731 return (CHAR16) (Char - (L'a' - L'A'));
3732 }
3733
3734 return Char;
3735}
3736
3737/**
3738 Convert a Unicode character to numerical value.
3739
3740 This internal function only deal with Unicode character
3741 which maps to a valid hexadecimal ASII character, i.e.
darylm503b0934ac2011-11-12 00:35:11 +00003742 L'0' to L'9', L'a' to L'f' or L'A' to L'F'. For other
jcarsey252d9452011-03-25 20:49:53 +00003743 Unicode character, the value returned does not make sense.
3744
3745 @param Char The character to convert.
3746
3747 @return The numerical value converted.
3748
3749**/
3750UINTN
3751EFIAPI
3752InternalShellHexCharToUintn (
3753 IN CHAR16 Char
3754 )
3755{
3756 if (ShellIsDecimalDigitCharacter (Char)) {
3757 return Char - L'0';
3758 }
3759
3760 return (UINTN) (10 + InternalShellCharToUpper (Char) - L'A');
3761}
3762
3763/**
3764 Convert a Null-terminated Unicode hexadecimal string to a value of type UINT64.
3765
Jaben Carsey80f3e342015-07-02 17:26:42 +00003766 This function returns a value of type UINT64 by interpreting the contents
jcarsey252d9452011-03-25 20:49:53 +00003767 of the Unicode string specified by String as a hexadecimal number.
3768 The format of the input Unicode string String is:
3769
3770 [spaces][zeros][x][hexadecimal digits].
3771
3772 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
3773 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix.
3774 If "x" appears in the input string, it must be prefixed with at least one 0.
3775 The function will ignore the pad space, which includes spaces or tab characters,
3776 before [zeros], [x] or [hexadecimal digit]. The running zero before [x] or
3777 [hexadecimal digit] will be ignored. Then, the decoding starts after [x] or the
3778 first valid hexadecimal digit. Then, the function stops at the first character that is
3779 a not a valid hexadecimal character or NULL, whichever one comes first.
3780
3781 If String has only pad spaces, then zero is returned.
3782 If String has no leading pad spaces, leading zeros or valid hexadecimal digits,
3783 then zero is returned.
3784
3785 @param[in] String A pointer to a Null-terminated Unicode string.
3786 @param[out] Value Upon a successful return the value of the conversion.
3787 @param[in] StopAtSpace FALSE to skip spaces.
3788
3789 @retval EFI_SUCCESS The conversion was successful.
3790 @retval EFI_INVALID_PARAMETER A parameter was NULL or invalid.
3791 @retval EFI_DEVICE_ERROR An overflow occured.
3792**/
3793EFI_STATUS
3794EFIAPI
3795InternalShellStrHexToUint64 (
3796 IN CONST CHAR16 *String,
3797 OUT UINT64 *Value,
3798 IN CONST BOOLEAN StopAtSpace
3799 )
3800{
3801 UINT64 Result;
3802
3803 if (String == NULL || StrSize(String) == 0 || Value == NULL) {
3804 return (EFI_INVALID_PARAMETER);
3805 }
darylm503b0934ac2011-11-12 00:35:11 +00003806
jcarsey252d9452011-03-25 20:49:53 +00003807 //
darylm503b0934ac2011-11-12 00:35:11 +00003808 // Ignore the pad spaces (space or tab)
jcarsey252d9452011-03-25 20:49:53 +00003809 //
3810 while ((*String == L' ') || (*String == L'\t')) {
3811 String++;
3812 }
3813
3814 //
3815 // Ignore leading Zeros after the spaces
3816 //
3817 while (*String == L'0') {
3818 String++;
3819 }
3820
3821 if (InternalShellCharToUpper (*String) == L'X') {
3822 if (*(String - 1) != L'0') {
3823 return 0;
3824 }
3825 //
3826 // Skip the 'X'
3827 //
3828 String++;
3829 }
3830
3831 Result = 0;
darylm503b0934ac2011-11-12 00:35:11 +00003832
jcarsey252d9452011-03-25 20:49:53 +00003833 //
Jaben Carsey80f3e342015-07-02 17:26:42 +00003834 // there is a space where there should't be
jcarsey252d9452011-03-25 20:49:53 +00003835 //
Jaben Carsey80f3e342015-07-02 17:26:42 +00003836 if (*String == L' ') {
3837 return (EFI_INVALID_PARAMETER);
jcarsey252d9452011-03-25 20:49:53 +00003838 }
darylm503b0934ac2011-11-12 00:35:11 +00003839
jcarsey252d9452011-03-25 20:49:53 +00003840 while (ShellIsHexaDecimalDigitCharacter (*String)) {
3841 //
darylm503b0934ac2011-11-12 00:35:11 +00003842 // If the Hex Number represented by String overflows according
Jaben Carsey80f3e342015-07-02 17:26:42 +00003843 // to the range defined by UINT64, then return EFI_DEVICE_ERROR.
jcarsey252d9452011-03-25 20:49:53 +00003844 //
3845 if (!(Result <= (RShiftU64((((UINT64) ~0) - InternalShellHexCharToUintn (*String)), 4)))) {
3846// if (!(Result <= ((((UINT64) ~0) - InternalShellHexCharToUintn (*String)) >> 4))) {
3847 return (EFI_DEVICE_ERROR);
3848 }
3849
3850 Result = (LShiftU64(Result, 4));
3851 Result += InternalShellHexCharToUintn (*String);
3852 String++;
3853
3854 //
jcarsey49bd4982012-01-27 18:42:43 +00003855 // stop at spaces if requested
jcarsey252d9452011-03-25 20:49:53 +00003856 //
jcarsey49bd4982012-01-27 18:42:43 +00003857 if (StopAtSpace && *String == L' ') {
3858 break;
jcarsey252d9452011-03-25 20:49:53 +00003859 }
3860 }
3861
3862 *Value = Result;
3863 return (EFI_SUCCESS);
3864}
3865
3866/**
3867 Convert a Null-terminated Unicode decimal string to a value of
3868 type UINT64.
3869
3870 This function returns a value of type UINT64 by interpreting the contents
3871 of the Unicode string specified by String as a decimal number. The format
3872 of the input Unicode string String is:
3873
3874 [spaces] [decimal digits].
3875
3876 The valid decimal digit character is in the range [0-9]. The
3877 function will ignore the pad space, which includes spaces or
3878 tab characters, before [decimal digits]. The running zero in the
3879 beginning of [decimal digits] will be ignored. Then, the function
3880 stops at the first character that is a not a valid decimal character
3881 or a Null-terminator, whichever one comes first.
3882
3883 If String has only pad spaces, then 0 is returned.
3884 If String has no pad spaces or valid decimal digits,
3885 then 0 is returned.
3886
3887 @param[in] String A pointer to a Null-terminated Unicode string.
3888 @param[out] Value Upon a successful return the value of the conversion.
3889 @param[in] StopAtSpace FALSE to skip spaces.
3890
3891 @retval EFI_SUCCESS The conversion was successful.
3892 @retval EFI_INVALID_PARAMETER A parameter was NULL or invalid.
3893 @retval EFI_DEVICE_ERROR An overflow occured.
3894**/
3895EFI_STATUS
3896EFIAPI
3897InternalShellStrDecimalToUint64 (
3898 IN CONST CHAR16 *String,
3899 OUT UINT64 *Value,
3900 IN CONST BOOLEAN StopAtSpace
3901 )
3902{
3903 UINT64 Result;
3904
3905 if (String == NULL || StrSize (String) == 0 || Value == NULL) {
3906 return (EFI_INVALID_PARAMETER);
3907 }
3908
3909 //
3910 // Ignore the pad spaces (space or tab)
3911 //
3912 while ((*String == L' ') || (*String == L'\t')) {
3913 String++;
3914 }
3915
3916 //
3917 // Ignore leading Zeros after the spaces
3918 //
3919 while (*String == L'0') {
3920 String++;
3921 }
3922
3923 Result = 0;
3924
3925 //
Jaben Carsey80f3e342015-07-02 17:26:42 +00003926 // Stop upon space if requested
3927 // (if the whole value was 0)
jcarsey252d9452011-03-25 20:49:53 +00003928 //
Jaben Carsey80f3e342015-07-02 17:26:42 +00003929 if (StopAtSpace && *String == L' ') {
3930 *Value = Result;
3931 return (EFI_SUCCESS);
jcarsey252d9452011-03-25 20:49:53 +00003932 }
Jaben Carsey80f3e342015-07-02 17:26:42 +00003933
jcarsey252d9452011-03-25 20:49:53 +00003934 while (ShellIsDecimalDigitCharacter (*String)) {
3935 //
darylm503b0934ac2011-11-12 00:35:11 +00003936 // If the number represented by String overflows according
Jaben Carsey80f3e342015-07-02 17:26:42 +00003937 // to the range defined by UINT64, then return EFI_DEVICE_ERROR.
jcarsey252d9452011-03-25 20:49:53 +00003938 //
darylm503b0934ac2011-11-12 00:35:11 +00003939
jcarsey252d9452011-03-25 20:49:53 +00003940 if (!(Result <= (DivU64x32((((UINT64) ~0) - (*String - L'0')),10)))) {
3941 return (EFI_DEVICE_ERROR);
3942 }
3943
3944 Result = MultU64x32(Result, 10) + (*String - L'0');
3945 String++;
3946
3947 //
3948 // Stop at spaces if requested
3949 //
3950 if (StopAtSpace && *String == L' ') {
3951 break;
3952 }
3953 }
3954
3955 *Value = Result;
darylm503b0934ac2011-11-12 00:35:11 +00003956
jcarsey252d9452011-03-25 20:49:53 +00003957 return (EFI_SUCCESS);
3958}
3959
3960/**
3961 Function to verify and convert a string to its numerical value.
3962
3963 If Hex it must be preceeded with a 0x, 0X, or has ForceHex set TRUE.
3964
3965 @param[in] String The string to evaluate.
3966 @param[out] Value Upon a successful return the value of the conversion.
3967 @param[in] ForceHex TRUE - always assume hex.
3968 @param[in] StopAtSpace FALSE to skip spaces.
darylm503b0934ac2011-11-12 00:35:11 +00003969
jcarsey252d9452011-03-25 20:49:53 +00003970 @retval EFI_SUCCESS The conversion was successful.
3971 @retval EFI_INVALID_PARAMETER String contained an invalid character.
3972 @retval EFI_NOT_FOUND String was a number, but Value was NULL.
3973**/
3974EFI_STATUS
3975EFIAPI
3976ShellConvertStringToUint64(
3977 IN CONST CHAR16 *String,
3978 OUT UINT64 *Value,
3979 IN CONST BOOLEAN ForceHex,
3980 IN CONST BOOLEAN StopAtSpace
3981 )
3982{
3983 UINT64 RetVal;
3984 CONST CHAR16 *Walker;
3985 EFI_STATUS Status;
3986 BOOLEAN Hex;
3987
3988 Hex = ForceHex;
3989
jcarsey658bf432014-11-04 22:33:16 +00003990 if (!InternalShellIsHexOrDecimalNumber(String, Hex, StopAtSpace, FALSE)) {
jcarsey252d9452011-03-25 20:49:53 +00003991 if (!Hex) {
3992 Hex = TRUE;
jcarsey658bf432014-11-04 22:33:16 +00003993 if (!InternalShellIsHexOrDecimalNumber(String, Hex, StopAtSpace, FALSE)) {
jcarsey252d9452011-03-25 20:49:53 +00003994 return (EFI_INVALID_PARAMETER);
3995 }
3996 } else {
3997 return (EFI_INVALID_PARAMETER);
3998 }
3999 }
4000
4001 //
4002 // Chop off leading spaces
4003 //
4004 for (Walker = String; Walker != NULL && *Walker != CHAR_NULL && *Walker == L' '; Walker++);
4005
4006 //
4007 // make sure we have something left that is numeric.
4008 //
jcarsey658bf432014-11-04 22:33:16 +00004009 if (Walker == NULL || *Walker == CHAR_NULL || !InternalShellIsHexOrDecimalNumber(Walker, Hex, StopAtSpace, FALSE)) {
jcarsey252d9452011-03-25 20:49:53 +00004010 return (EFI_INVALID_PARAMETER);
darylm503b0934ac2011-11-12 00:35:11 +00004011 }
jcarsey252d9452011-03-25 20:49:53 +00004012
4013 //
4014 // do the conversion.
4015 //
4016 if (Hex || StrnCmp(Walker, L"0x", 2) == 0 || StrnCmp(Walker, L"0X", 2) == 0){
4017 Status = InternalShellStrHexToUint64(Walker, &RetVal, StopAtSpace);
4018 } else {
4019 Status = InternalShellStrDecimalToUint64(Walker, &RetVal, StopAtSpace);
4020 }
4021
4022 if (Value == NULL && !EFI_ERROR(Status)) {
4023 return (EFI_NOT_FOUND);
4024 }
4025
4026 if (Value != NULL) {
4027 *Value = RetVal;
4028 }
4029
4030 return (Status);
4031}
4032
4033/**
4034 Function to determin if an entire string is a valid number.
4035
4036 If Hex it must be preceeded with a 0x or has ForceHex, set TRUE.
4037
4038 @param[in] String The string to evaluate.
4039 @param[in] ForceHex TRUE - always assume hex.
4040 @param[in] StopAtSpace TRUE to halt upon finding a space, FALSE to keep going.
4041
4042 @retval TRUE It is all numeric (dec/hex) characters.
4043 @retval FALSE There is a non-numeric character.
4044**/
4045BOOLEAN
4046EFIAPI
4047ShellIsHexOrDecimalNumber (
4048 IN CONST CHAR16 *String,
4049 IN CONST BOOLEAN ForceHex,
4050 IN CONST BOOLEAN StopAtSpace
4051 )
4052{
4053 if (ShellConvertStringToUint64(String, NULL, ForceHex, StopAtSpace) == EFI_NOT_FOUND) {
4054 return (TRUE);
4055 }
4056 return (FALSE);
4057}
jcarsey4d0a4fc2011-07-06 22:28:36 +00004058
4059/**
4060 Function to read a single line from a SHELL_FILE_HANDLE. The \n is not included in the returned
4061 buffer. The returned buffer must be callee freed.
4062
4063 If the position upon start is 0, then the Ascii Boolean will be set. This should be
4064 maintained and not changed for all operations with the same file.
4065
ydong104ff7e372011-09-02 08:05:34 +00004066 @param[in] Handle SHELL_FILE_HANDLE to read from.
4067 @param[in, out] Ascii Boolean value for indicating whether the file is
4068 Ascii (TRUE) or UCS2 (FALSE).
jcarsey4d0a4fc2011-07-06 22:28:36 +00004069
jcarseybeab0fc2011-10-10 17:26:25 +00004070 @return The line of text from the file.
4071 @retval NULL There was not enough memory available.
jcarsey4d0a4fc2011-07-06 22:28:36 +00004072
4073 @sa ShellFileHandleReadLine
4074**/
4075CHAR16*
4076EFIAPI
4077ShellFileHandleReturnLine(
4078 IN SHELL_FILE_HANDLE Handle,
4079 IN OUT BOOLEAN *Ascii
4080 )
4081{
4082 CHAR16 *RetVal;
4083 UINTN Size;
4084 EFI_STATUS Status;
4085
4086 Size = 0;
4087 RetVal = NULL;
4088
4089 Status = ShellFileHandleReadLine(Handle, RetVal, &Size, FALSE, Ascii);
4090 if (Status == EFI_BUFFER_TOO_SMALL) {
4091 RetVal = AllocateZeroPool(Size);
jcarseybeab0fc2011-10-10 17:26:25 +00004092 if (RetVal == NULL) {
4093 return (NULL);
4094 }
jcarsey4d0a4fc2011-07-06 22:28:36 +00004095 Status = ShellFileHandleReadLine(Handle, RetVal, &Size, FALSE, Ascii);
darylm503b0934ac2011-11-12 00:35:11 +00004096
jcarsey4d0a4fc2011-07-06 22:28:36 +00004097 }
Qiu Shumine3b76f32016-02-22 10:21:38 +08004098 if (Status == EFI_END_OF_FILE && RetVal != NULL && *RetVal != CHAR_NULL) {
Qiu Shuminf79d7b62016-02-18 13:12:58 +08004099 Status = EFI_SUCCESS;
4100 }
jcarsey4d0a4fc2011-07-06 22:28:36 +00004101 if (EFI_ERROR(Status) && (RetVal != NULL)) {
4102 FreePool(RetVal);
4103 RetVal = NULL;
4104 }
4105 return (RetVal);
4106}
4107
4108/**
4109 Function to read a single line (up to but not including the \n) from a SHELL_FILE_HANDLE.
4110
4111 If the position upon start is 0, then the Ascii Boolean will be set. This should be
4112 maintained and not changed for all operations with the same file.
4113
Jim Dailey2dda8a12016-02-10 08:53:00 -08004114 NOTE: LINES THAT ARE RETURNED BY THIS FUNCTION ARE UCS2, EVEN IF THE FILE BEING READ
4115 IS IN ASCII FORMAT.
4116
ydong104ff7e372011-09-02 08:05:34 +00004117 @param[in] Handle SHELL_FILE_HANDLE to read from.
Jim Dailey2dda8a12016-02-10 08:53:00 -08004118 @param[in, out] Buffer The pointer to buffer to read into. If this function
4119 returns EFI_SUCCESS, then on output Buffer will
4120 contain a UCS2 string, even if the file being
4121 read is ASCII.
4122 @param[in, out] Size On input, pointer to number of bytes in Buffer.
4123 On output, unchanged unless Buffer is too small
4124 to contain the next line of the file. In that
4125 case Size is set to the number of bytes needed
4126 to hold the next line of the file (as a UCS2
4127 string, even if it is an ASCII file).
ydong104ff7e372011-09-02 08:05:34 +00004128 @param[in] Truncate If the buffer is large enough, this has no effect.
4129 If the buffer is is too small and Truncate is TRUE,
4130 the line will be truncated.
4131 If the buffer is is too small and Truncate is FALSE,
4132 then no read will occur.
jcarsey4d0a4fc2011-07-06 22:28:36 +00004133
ydong104ff7e372011-09-02 08:05:34 +00004134 @param[in, out] Ascii Boolean value for indicating whether the file is
4135 Ascii (TRUE) or UCS2 (FALSE).
jcarsey4d0a4fc2011-07-06 22:28:36 +00004136
4137 @retval EFI_SUCCESS The operation was successful. The line is stored in
4138 Buffer.
jaben carsey9ed21942016-02-08 15:59:04 -08004139 @retval EFI_END_OF_FILE There are no more lines in the file.
jcarsey4d0a4fc2011-07-06 22:28:36 +00004140 @retval EFI_INVALID_PARAMETER Handle was NULL.
4141 @retval EFI_INVALID_PARAMETER Size was NULL.
4142 @retval EFI_BUFFER_TOO_SMALL Size was not large enough to store the line.
4143 Size was updated to the minimum space required.
4144**/
4145EFI_STATUS
4146EFIAPI
4147ShellFileHandleReadLine(
4148 IN SHELL_FILE_HANDLE Handle,
4149 IN OUT CHAR16 *Buffer,
4150 IN OUT UINTN *Size,
4151 IN BOOLEAN Truncate,
4152 IN OUT BOOLEAN *Ascii
4153 )
4154{
4155 EFI_STATUS Status;
4156 CHAR16 CharBuffer;
4157 UINTN CharSize;
4158 UINTN CountSoFar;
4159 UINT64 OriginalFilePosition;
4160
4161
4162 if (Handle == NULL
4163 ||Size == NULL
4164 ){
4165 return (EFI_INVALID_PARAMETER);
4166 }
4167 if (Buffer == NULL) {
4168 ASSERT(*Size == 0);
4169 } else {
4170 *Buffer = CHAR_NULL;
4171 }
4172 gEfiShellProtocol->GetFilePosition(Handle, &OriginalFilePosition);
4173 if (OriginalFilePosition == 0) {
4174 CharSize = sizeof(CHAR16);
4175 Status = gEfiShellProtocol->ReadFile(Handle, &CharSize, &CharBuffer);
4176 ASSERT_EFI_ERROR(Status);
4177 if (CharBuffer == gUnicodeFileTag) {
4178 *Ascii = FALSE;
4179 } else {
4180 *Ascii = TRUE;
4181 gEfiShellProtocol->SetFilePosition(Handle, OriginalFilePosition);
4182 }
4183 }
4184
jaben carsey9ed21942016-02-08 15:59:04 -08004185 if (*Ascii) {
4186 CharSize = sizeof(CHAR8);
4187 } else {
4188 CharSize = sizeof(CHAR16);
4189 }
jcarsey4d0a4fc2011-07-06 22:28:36 +00004190 for (CountSoFar = 0;;CountSoFar++){
4191 CharBuffer = 0;
jcarsey4d0a4fc2011-07-06 22:28:36 +00004192 Status = gEfiShellProtocol->ReadFile(Handle, &CharSize, &CharBuffer);
4193 if ( EFI_ERROR(Status)
4194 || CharSize == 0
4195 || (CharBuffer == L'\n' && !(*Ascii))
4196 || (CharBuffer == '\n' && *Ascii)
4197 ){
jaben carsey9ed21942016-02-08 15:59:04 -08004198 if (CharSize == 0) {
4199 Status = EFI_END_OF_FILE;
4200 }
jcarsey4d0a4fc2011-07-06 22:28:36 +00004201 break;
4202 }
4203 //
4204 // if we have space save it...
4205 //
Jim Dailey1095b432016-02-10 13:17:56 -08004206 if ((CountSoFar+1)*sizeof(CHAR16) < *Size){
jcarsey4d0a4fc2011-07-06 22:28:36 +00004207 ASSERT(Buffer != NULL);
Jim Dailey1095b432016-02-10 13:17:56 -08004208 ((CHAR16*)Buffer)[CountSoFar] = CharBuffer;
4209 ((CHAR16*)Buffer)[CountSoFar+1] = CHAR_NULL;
jcarsey4d0a4fc2011-07-06 22:28:36 +00004210 }
4211 }
4212
4213 //
4214 // if we ran out of space tell when...
4215 //
Jim Dailey1095b432016-02-10 13:17:56 -08004216 if ((CountSoFar+1)*sizeof(CHAR16) > *Size){
4217 *Size = (CountSoFar+1)*sizeof(CHAR16);
4218 if (!Truncate) {
4219 gEfiShellProtocol->SetFilePosition(Handle, OriginalFilePosition);
4220 } else {
4221 DEBUG((DEBUG_WARN, "The line was truncated in ShellFileHandleReadLine"));
jcarsey4d0a4fc2011-07-06 22:28:36 +00004222 }
Jim Dailey1095b432016-02-10 13:17:56 -08004223 return (EFI_BUFFER_TOO_SMALL);
4224 }
4225 while(Buffer[StrLen(Buffer)-1] == L'\r') {
4226 Buffer[StrLen(Buffer)-1] = CHAR_NULL;
jcarsey4d0a4fc2011-07-06 22:28:36 +00004227 }
4228
4229 return (Status);
4230}
jcarseyfb5278e2013-02-20 18:21:14 +00004231
4232/**
jcarsey365aa982013-03-04 21:54:02 +00004233 Function to print help file / man page content in the spec from the UEFI Shell protocol GetHelpText function.
4234
4235 @param[in] CommandToGetHelpOn Pointer to a string containing the command name of help file to be printed.
4236 @param[in] SectionToGetHelpOn Pointer to the section specifier(s).
4237 @param[in] PrintCommandText If TRUE, prints the command followed by the help content, otherwise prints
4238 the help content only.
4239 @retval EFI_DEVICE_ERROR The help data format was incorrect.
4240 @retval EFI_NOT_FOUND The help data could not be found.
4241 @retval EFI_SUCCESS The operation was successful.
4242**/
4243EFI_STATUS
4244EFIAPI
4245ShellPrintHelp (
4246 IN CONST CHAR16 *CommandToGetHelpOn,
4247 IN CONST CHAR16 *SectionToGetHelpOn,
4248 IN BOOLEAN PrintCommandText
4249 )
4250{
4251 EFI_STATUS Status;
4252 CHAR16 *OutText;
4253
4254 OutText = NULL;
4255
4256 //
4257 // Get the string to print based
4258 //
4259 Status = gEfiShellProtocol->GetHelpText (CommandToGetHelpOn, SectionToGetHelpOn, &OutText);
4260
4261 //
4262 // make sure we got a valid string
4263 //
4264 if (EFI_ERROR(Status)){
4265 return Status;
4266 }
4267 if (OutText == NULL || StrLen(OutText) == 0) {
4268 return EFI_NOT_FOUND;
4269 }
4270
4271 //
4272 // Chop off trailing stuff we dont need
4273 //
4274 while (OutText[StrLen(OutText)-1] == L'\r' || OutText[StrLen(OutText)-1] == L'\n' || OutText[StrLen(OutText)-1] == L' ') {
4275 OutText[StrLen(OutText)-1] = CHAR_NULL;
4276 }
4277
4278 //
4279 // Print this out to the console
4280 //
4281 if (PrintCommandText) {
4282 ShellPrintEx(-1, -1, L"%H%-14s%N- %s\r\n", CommandToGetHelpOn, OutText);
4283 } else {
4284 ShellPrintEx(-1, -1, L"%N%s\r\n", OutText);
4285 }
4286
4287 SHELL_FREE_NON_NULL(OutText);
4288
4289 return EFI_SUCCESS;
4290}
4291
4292/**
jcarseyfb5278e2013-02-20 18:21:14 +00004293 Function to delete a file by name
4294
4295 @param[in] FileName Pointer to file name to delete.
4296
4297 @retval EFI_SUCCESS the file was deleted sucessfully
4298 @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not
4299 deleted
4300 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
4301 @retval EFI_NOT_FOUND The specified file could not be found on the
4302 device or the file system could not be found
4303 on the device.
4304 @retval EFI_NO_MEDIA The device has no medium.
4305 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
4306 medium is no longer supported.
4307 @retval EFI_DEVICE_ERROR The device reported an error.
4308 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
4309 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
4310 @retval EFI_ACCESS_DENIED The file was opened read only.
4311 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
4312 file.
4313 @retval other The file failed to open
4314**/
4315EFI_STATUS
4316EFIAPI
4317ShellDeleteFileByName(
4318 IN CONST CHAR16 *FileName
4319 )
4320{
4321 EFI_STATUS Status;
4322 SHELL_FILE_HANDLE FileHandle;
4323
4324 Status = ShellFileExists(FileName);
4325
4326 if (Status == EFI_SUCCESS){
4327 Status = ShellOpenFileByName(FileName, &FileHandle, EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE, 0x0);
4328 if (Status == EFI_SUCCESS){
4329 Status = ShellDeleteFile(&FileHandle);
4330 }
4331 }
4332
4333 return(Status);
4334
4335}
Qiu Shumin0960ba12014-09-17 07:58:31 +00004336
4337/**
4338 Cleans off all the quotes in the string.
4339
4340 @param[in] OriginalString pointer to the string to be cleaned.
4341 @param[out] CleanString The new string with all quotes removed.
4342 Memory allocated in the function and free
4343 by caller.
4344
4345 @retval EFI_SUCCESS The operation was successful.
4346**/
4347EFI_STATUS
4348EFIAPI
4349InternalShellStripQuotes (
4350 IN CONST CHAR16 *OriginalString,
4351 OUT CHAR16 **CleanString
4352 )
4353{
4354 CHAR16 *Walker;
4355
4356 if (OriginalString == NULL || CleanString == NULL) {
4357 return EFI_INVALID_PARAMETER;
4358 }
4359
4360 *CleanString = AllocateCopyPool (StrSize (OriginalString), OriginalString);
4361 if (*CleanString == NULL) {
4362 return EFI_OUT_OF_RESOURCES;
4363 }
4364
4365 for (Walker = *CleanString; Walker != NULL && *Walker != CHAR_NULL ; Walker++) {
4366 if (*Walker == L'\"') {
4367 CopyMem(Walker, Walker+1, StrSize(Walker) - sizeof(Walker[0]));
4368 }
4369 }
4370
4371 return EFI_SUCCESS;
4372}
4373