blob: c3a67c3be7685379c7a39d4f7120d0f9db7264cb [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.
Hao Wu22454f12017-02-17 11:00:45 +08006 Copyright (c) 2006 - 2017, 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"
jcarsey252d9452011-03-25 20:49:53 +000018#include <Library/SortLib.h>
Laszlo Ersek3fe23dc2015-01-14 16:25:48 +000019#include <Library/BaseLib.h>
jcarseyd2b45642009-05-11 18:02:16 +000020
jcarsey94b17fa2009-05-07 18:46:18 +000021#define FIND_XXXXX_FILE_BUFFER_SIZE (SIZE_OF_EFI_FILE_INFO + MAX_FILE_NAME_LEN)
22
jcarseyd2b45642009-05-11 18:02:16 +000023//
jcarseya405b862010-09-14 05:18:09 +000024// globals...
jcarseyd2b45642009-05-11 18:02:16 +000025//
26SHELL_PARAM_ITEM EmptyParamList[] = {
27 {NULL, TypeMax}
28 };
jcarseya405b862010-09-14 05:18:09 +000029SHELL_PARAM_ITEM SfoParamList[] = {
30 {L"-sfo", TypeFlag},
31 {NULL, TypeMax}
32 };
33EFI_SHELL_ENVIRONMENT2 *mEfiShellEnvironment2;
34EFI_SHELL_INTERFACE *mEfiShellInterface;
jcarsey366f81a2011-06-27 21:04:22 +000035EFI_SHELL_PROTOCOL *gEfiShellProtocol;
36EFI_SHELL_PARAMETERS_PROTOCOL *gEfiShellParametersProtocol;
jcarseya405b862010-09-14 05:18:09 +000037EFI_HANDLE mEfiShellEnvironment2Handle;
38FILE_HANDLE_FUNCTION_MAP FileFunctionMap;
Tapan Shah583448b2016-09-22 12:12:47 -070039EFI_UNICODE_COLLATION_PROTOCOL *mUnicodeCollationProtocol;
jcarseyb3011f42010-01-11 21:49:04 +000040
jcarsey2247dde2009-11-09 18:08:58 +000041/**
42 Check if a Unicode character is a hexadecimal character.
43
jcarsey1e6e84c2010-01-25 20:05:08 +000044 This internal function checks if a Unicode character is a
jcarseya405b862010-09-14 05:18:09 +000045 numeric character. The valid hexadecimal characters are
jcarsey2247dde2009-11-09 18:08:58 +000046 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.
47
jcarsey2247dde2009-11-09 18:08:58 +000048 @param Char The character to check against.
49
50 @retval TRUE If the Char is a hexadecmial character.
51 @retval FALSE If the Char is not a hexadecmial character.
52
53**/
54BOOLEAN
55EFIAPI
jcarsey969c7832010-01-13 16:46:33 +000056ShellIsHexaDecimalDigitCharacter (
jcarsey2247dde2009-11-09 18:08:58 +000057 IN CHAR16 Char
jcarseya405b862010-09-14 05:18:09 +000058 )
59{
jcarsey2247dde2009-11-09 18:08:58 +000060 return (BOOLEAN) ((Char >= L'0' && Char <= L'9') || (Char >= L'A' && Char <= L'F') || (Char >= L'a' && Char <= L'f'));
61}
jcarsey94b17fa2009-05-07 18:46:18 +000062
63/**
jcarseya405b862010-09-14 05:18:09 +000064 Check if a Unicode character is a decimal character.
65
66 This internal function checks if a Unicode character is a
67 decimal character. The valid characters are
68 L'0' to L'9'.
69
70
71 @param Char The character to check against.
72
73 @retval TRUE If the Char is a hexadecmial character.
74 @retval FALSE If the Char is not a hexadecmial character.
75
76**/
77BOOLEAN
78EFIAPI
79ShellIsDecimalDigitCharacter (
80 IN CHAR16 Char
81 )
82{
83 return (BOOLEAN) (Char >= L'0' && Char <= L'9');
84}
85
86/**
87 Helper function to find ShellEnvironment2 for constructor.
88
89 @param[in] ImageHandle A copy of the calling image's handle.
jcarseybeab0fc2011-10-10 17:26:25 +000090
91 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.
jcarsey94b17fa2009-05-07 18:46:18 +000092**/
93EFI_STATUS
jcarsey94b17fa2009-05-07 18:46:18 +000094ShellFindSE2 (
95 IN EFI_HANDLE ImageHandle
jcarseya405b862010-09-14 05:18:09 +000096 )
97{
jcarsey94b17fa2009-05-07 18:46:18 +000098 EFI_STATUS Status;
99 EFI_HANDLE *Buffer;
100 UINTN BufferSize;
101 UINTN HandleIndex;
102
103 BufferSize = 0;
104 Buffer = NULL;
jcarsey1e6e84c2010-01-25 20:05:08 +0000105 Status = gBS->OpenProtocol(ImageHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000106 &gEfiShellEnvironment2Guid,
107 (VOID **)&mEfiShellEnvironment2,
108 ImageHandle,
109 NULL,
110 EFI_OPEN_PROTOCOL_GET_PROTOCOL
jcarseya405b862010-09-14 05:18:09 +0000111 );
jcarsey94b17fa2009-05-07 18:46:18 +0000112 //
113 // look for the mEfiShellEnvironment2 protocol at a higher level
114 //
jcarseya405b862010-09-14 05:18:09 +0000115 if (EFI_ERROR (Status) || !(CompareGuid (&mEfiShellEnvironment2->SESGuid, &gEfiShellEnvironment2ExtGuid))){
jcarsey94b17fa2009-05-07 18:46:18 +0000116 //
117 // figure out how big of a buffer we need.
118 //
119 Status = gBS->LocateHandle (ByProtocol,
120 &gEfiShellEnvironment2Guid,
121 NULL, // ignored for ByProtocol
122 &BufferSize,
123 Buffer
jcarseya405b862010-09-14 05:18:09 +0000124 );
jcarsey2247dde2009-11-09 18:08:58 +0000125 //
126 // maybe it's not there???
127 //
128 if (Status == EFI_BUFFER_TOO_SMALL) {
jcarsey252d9452011-03-25 20:49:53 +0000129 Buffer = (EFI_HANDLE*)AllocateZeroPool(BufferSize);
jcarseybeab0fc2011-10-10 17:26:25 +0000130 if (Buffer == NULL) {
131 return (EFI_OUT_OF_RESOURCES);
132 }
jcarsey2247dde2009-11-09 18:08:58 +0000133 Status = gBS->LocateHandle (ByProtocol,
134 &gEfiShellEnvironment2Guid,
135 NULL, // ignored for ByProtocol
136 &BufferSize,
137 Buffer
jcarseya405b862010-09-14 05:18:09 +0000138 );
jcarsey2247dde2009-11-09 18:08:58 +0000139 }
jcarsey1cd45e72010-01-29 15:07:44 +0000140 if (!EFI_ERROR (Status) && Buffer != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +0000141 //
142 // now parse the list of returned handles
143 //
144 Status = EFI_NOT_FOUND;
145 for (HandleIndex = 0; HandleIndex < (BufferSize/sizeof(Buffer[0])); HandleIndex++) {
jcarsey1e6e84c2010-01-25 20:05:08 +0000146 Status = gBS->OpenProtocol(Buffer[HandleIndex],
jcarsey94b17fa2009-05-07 18:46:18 +0000147 &gEfiShellEnvironment2Guid,
148 (VOID **)&mEfiShellEnvironment2,
149 ImageHandle,
150 NULL,
151 EFI_OPEN_PROTOCOL_GET_PROTOCOL
jcarseya405b862010-09-14 05:18:09 +0000152 );
153 if (CompareGuid (&mEfiShellEnvironment2->SESGuid, &gEfiShellEnvironment2ExtGuid)) {
jcarsey94b17fa2009-05-07 18:46:18 +0000154 mEfiShellEnvironment2Handle = Buffer[HandleIndex];
155 Status = EFI_SUCCESS;
156 break;
157 }
158 }
159 }
160 }
161 if (Buffer != NULL) {
162 FreePool (Buffer);
163 }
164 return (Status);
165}
166
jcarsey252d9452011-03-25 20:49:53 +0000167/**
darylm503b0934ac2011-11-12 00:35:11 +0000168 Function to do most of the work of the constructor. Allows for calling
jcarseya405b862010-09-14 05:18:09 +0000169 multiple times without complete re-initialization.
170
171 @param[in] ImageHandle A copy of the ImageHandle.
172 @param[in] SystemTable A pointer to the SystemTable for the application.
jcarsey252d9452011-03-25 20:49:53 +0000173
174 @retval EFI_SUCCESS The operationw as successful.
jcarseya405b862010-09-14 05:18:09 +0000175**/
jcarsey94b17fa2009-05-07 18:46:18 +0000176EFI_STATUS
jcarseyd2b45642009-05-11 18:02:16 +0000177ShellLibConstructorWorker (
jcarsey94b17fa2009-05-07 18:46:18 +0000178 IN EFI_HANDLE ImageHandle,
179 IN EFI_SYSTEM_TABLE *SystemTable
jcarseya405b862010-09-14 05:18:09 +0000180 )
181{
182 EFI_STATUS Status;
jcarseyecd3d592009-12-07 18:05:00 +0000183
jcarsey94b17fa2009-05-07 18:46:18 +0000184 //
185 // UEFI 2.0 shell interfaces (used preferentially)
186 //
jcarseya405b862010-09-14 05:18:09 +0000187 Status = gBS->OpenProtocol(
188 ImageHandle,
189 &gEfiShellProtocolGuid,
jcarsey366f81a2011-06-27 21:04:22 +0000190 (VOID **)&gEfiShellProtocol,
jcarseya405b862010-09-14 05:18:09 +0000191 ImageHandle,
192 NULL,
193 EFI_OPEN_PROTOCOL_GET_PROTOCOL
194 );
jcarsey94b17fa2009-05-07 18:46:18 +0000195 if (EFI_ERROR(Status)) {
jcarseya405b862010-09-14 05:18:09 +0000196 //
197 // Search for the shell protocol
198 //
199 Status = gBS->LocateProtocol(
200 &gEfiShellProtocolGuid,
201 NULL,
jcarsey366f81a2011-06-27 21:04:22 +0000202 (VOID **)&gEfiShellProtocol
jcarseya405b862010-09-14 05:18:09 +0000203 );
204 if (EFI_ERROR(Status)) {
jcarsey366f81a2011-06-27 21:04:22 +0000205 gEfiShellProtocol = NULL;
jcarseya405b862010-09-14 05:18:09 +0000206 }
jcarsey94b17fa2009-05-07 18:46:18 +0000207 }
jcarseya405b862010-09-14 05:18:09 +0000208 Status = gBS->OpenProtocol(
209 ImageHandle,
210 &gEfiShellParametersProtocolGuid,
jcarsey366f81a2011-06-27 21:04:22 +0000211 (VOID **)&gEfiShellParametersProtocol,
jcarseya405b862010-09-14 05:18:09 +0000212 ImageHandle,
213 NULL,
214 EFI_OPEN_PROTOCOL_GET_PROTOCOL
215 );
jcarsey94b17fa2009-05-07 18:46:18 +0000216 if (EFI_ERROR(Status)) {
jcarsey366f81a2011-06-27 21:04:22 +0000217 gEfiShellParametersProtocol = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +0000218 }
219
jcarsey366f81a2011-06-27 21:04:22 +0000220 if (gEfiShellParametersProtocol == NULL || gEfiShellProtocol == NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +0000221 //
222 // Moved to seperate function due to complexity
223 //
224 Status = ShellFindSE2(ImageHandle);
225
226 if (EFI_ERROR(Status)) {
227 DEBUG((DEBUG_ERROR, "Status: 0x%08x\r\n", Status));
228 mEfiShellEnvironment2 = NULL;
229 }
jcarsey1e6e84c2010-01-25 20:05:08 +0000230 Status = gBS->OpenProtocol(ImageHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000231 &gEfiShellInterfaceGuid,
232 (VOID **)&mEfiShellInterface,
233 ImageHandle,
234 NULL,
235 EFI_OPEN_PROTOCOL_GET_PROTOCOL
jcarseya405b862010-09-14 05:18:09 +0000236 );
jcarsey94b17fa2009-05-07 18:46:18 +0000237 if (EFI_ERROR(Status)) {
238 mEfiShellInterface = NULL;
239 }
240 }
jcarseyc9d92df2010-02-03 15:37:54 +0000241
jcarsey94b17fa2009-05-07 18:46:18 +0000242 //
243 // only success getting 2 of either the old or new, but no 1/2 and 1/2
244 //
jcarsey1e6e84c2010-01-25 20:05:08 +0000245 if ((mEfiShellEnvironment2 != NULL && mEfiShellInterface != NULL) ||
jcarsey366f81a2011-06-27 21:04:22 +0000246 (gEfiShellProtocol != NULL && gEfiShellParametersProtocol != NULL) ) {
247 if (gEfiShellProtocol != NULL) {
248 FileFunctionMap.GetFileInfo = gEfiShellProtocol->GetFileInfo;
249 FileFunctionMap.SetFileInfo = gEfiShellProtocol->SetFileInfo;
250 FileFunctionMap.ReadFile = gEfiShellProtocol->ReadFile;
251 FileFunctionMap.WriteFile = gEfiShellProtocol->WriteFile;
252 FileFunctionMap.CloseFile = gEfiShellProtocol->CloseFile;
253 FileFunctionMap.DeleteFile = gEfiShellProtocol->DeleteFile;
254 FileFunctionMap.GetFilePosition = gEfiShellProtocol->GetFilePosition;
255 FileFunctionMap.SetFilePosition = gEfiShellProtocol->SetFilePosition;
256 FileFunctionMap.FlushFile = gEfiShellProtocol->FlushFile;
257 FileFunctionMap.GetFileSize = gEfiShellProtocol->GetFileSize;
jcarseyd2b45642009-05-11 18:02:16 +0000258 } else {
jcarseya405b862010-09-14 05:18:09 +0000259 FileFunctionMap.GetFileInfo = (EFI_SHELL_GET_FILE_INFO)FileHandleGetInfo;
260 FileFunctionMap.SetFileInfo = (EFI_SHELL_SET_FILE_INFO)FileHandleSetInfo;
261 FileFunctionMap.ReadFile = (EFI_SHELL_READ_FILE)FileHandleRead;
262 FileFunctionMap.WriteFile = (EFI_SHELL_WRITE_FILE)FileHandleWrite;
263 FileFunctionMap.CloseFile = (EFI_SHELL_CLOSE_FILE)FileHandleClose;
264 FileFunctionMap.DeleteFile = (EFI_SHELL_DELETE_FILE)FileHandleDelete;
265 FileFunctionMap.GetFilePosition = (EFI_SHELL_GET_FILE_POSITION)FileHandleGetPosition;
266 FileFunctionMap.SetFilePosition = (EFI_SHELL_SET_FILE_POSITION)FileHandleSetPosition;
267 FileFunctionMap.FlushFile = (EFI_SHELL_FLUSH_FILE)FileHandleFlush;
268 FileFunctionMap.GetFileSize = (EFI_SHELL_GET_FILE_SIZE)FileHandleGetSize;
jcarseyd2b45642009-05-11 18:02:16 +0000269 }
jcarsey94b17fa2009-05-07 18:46:18 +0000270 return (EFI_SUCCESS);
271 }
272 return (EFI_NOT_FOUND);
273}
jcarseyd2b45642009-05-11 18:02:16 +0000274/**
275 Constructor for the Shell library.
276
277 Initialize the library and determine if the underlying is a UEFI Shell 2.0 or an EFI shell.
278
279 @param ImageHandle the image handle of the process
280 @param SystemTable the EFI System Table pointer
281
282 @retval EFI_SUCCESS the initialization was complete sucessfully
283 @return others an error ocurred during initialization
284**/
285EFI_STATUS
286EFIAPI
287ShellLibConstructor (
288 IN EFI_HANDLE ImageHandle,
289 IN EFI_SYSTEM_TABLE *SystemTable
jcarseya405b862010-09-14 05:18:09 +0000290 )
291{
jcarseyd2b45642009-05-11 18:02:16 +0000292 mEfiShellEnvironment2 = NULL;
jcarsey366f81a2011-06-27 21:04:22 +0000293 gEfiShellProtocol = NULL;
294 gEfiShellParametersProtocol = NULL;
jcarseyd2b45642009-05-11 18:02:16 +0000295 mEfiShellInterface = NULL;
296 mEfiShellEnvironment2Handle = NULL;
Tapan Shah2cf9ecd2016-10-05 13:58:05 -0700297 mUnicodeCollationProtocol = NULL;
Tapan Shah583448b2016-09-22 12:12:47 -0700298
jcarseyd2b45642009-05-11 18:02:16 +0000299 //
300 // verify that auto initialize is not set false
jcarsey1e6e84c2010-01-25 20:05:08 +0000301 //
jcarseyd2b45642009-05-11 18:02:16 +0000302 if (PcdGetBool(PcdShellLibAutoInitialize) == 0) {
303 return (EFI_SUCCESS);
304 }
jcarsey1e6e84c2010-01-25 20:05:08 +0000305
jcarseyd2b45642009-05-11 18:02:16 +0000306 return (ShellLibConstructorWorker(ImageHandle, SystemTable));
307}
jcarsey94b17fa2009-05-07 18:46:18 +0000308
309/**
jcarseya405b862010-09-14 05:18:09 +0000310 Destructor for the library. free any resources.
311
312 @param[in] ImageHandle A copy of the ImageHandle.
313 @param[in] SystemTable A pointer to the SystemTable for the application.
314
315 @retval EFI_SUCCESS The operation was successful.
316 @return An error from the CloseProtocol function.
jcarsey94b17fa2009-05-07 18:46:18 +0000317**/
318EFI_STATUS
319EFIAPI
320ShellLibDestructor (
321 IN EFI_HANDLE ImageHandle,
322 IN EFI_SYSTEM_TABLE *SystemTable
jcarseya405b862010-09-14 05:18:09 +0000323 )
324{
jcarsey94b17fa2009-05-07 18:46:18 +0000325 if (mEfiShellEnvironment2 != NULL) {
326 gBS->CloseProtocol(mEfiShellEnvironment2Handle==NULL?ImageHandle:mEfiShellEnvironment2Handle,
327 &gEfiShellEnvironment2Guid,
328 ImageHandle,
329 NULL);
jcarseyd2b45642009-05-11 18:02:16 +0000330 mEfiShellEnvironment2 = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +0000331 }
332 if (mEfiShellInterface != NULL) {
333 gBS->CloseProtocol(ImageHandle,
334 &gEfiShellInterfaceGuid,
335 ImageHandle,
jcarsey1e6e84c2010-01-25 20:05:08 +0000336 NULL);
jcarseyd2b45642009-05-11 18:02:16 +0000337 mEfiShellInterface = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +0000338 }
jcarsey366f81a2011-06-27 21:04:22 +0000339 if (gEfiShellProtocol != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +0000340 gBS->CloseProtocol(ImageHandle,
341 &gEfiShellProtocolGuid,
342 ImageHandle,
jcarsey1e6e84c2010-01-25 20:05:08 +0000343 NULL);
jcarsey366f81a2011-06-27 21:04:22 +0000344 gEfiShellProtocol = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +0000345 }
jcarsey366f81a2011-06-27 21:04:22 +0000346 if (gEfiShellParametersProtocol != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +0000347 gBS->CloseProtocol(ImageHandle,
348 &gEfiShellParametersProtocolGuid,
349 ImageHandle,
jcarsey1e6e84c2010-01-25 20:05:08 +0000350 NULL);
jcarsey366f81a2011-06-27 21:04:22 +0000351 gEfiShellParametersProtocol = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +0000352 }
jcarseyd2b45642009-05-11 18:02:16 +0000353 mEfiShellEnvironment2Handle = NULL;
jcarseyecd3d592009-12-07 18:05:00 +0000354
jcarsey94b17fa2009-05-07 18:46:18 +0000355 return (EFI_SUCCESS);
356}
jcarseyd2b45642009-05-11 18:02:16 +0000357
358/**
359 This function causes the shell library to initialize itself. If the shell library
360 is already initialized it will de-initialize all the current protocol poitners and
361 re-populate them again.
362
363 When the library is used with PcdShellLibAutoInitialize set to true this function
364 will return EFI_SUCCESS and perform no actions.
365
366 This function is intended for internal access for shell commands only.
367
368 @retval EFI_SUCCESS the initialization was complete sucessfully
369
370**/
371EFI_STATUS
372EFIAPI
373ShellInitialize (
jcarseya405b862010-09-14 05:18:09 +0000374 )
375{
Laszlo Ersek4a7518d2016-06-28 13:52:11 +0200376 EFI_STATUS Status;
377
jcarseyd2b45642009-05-11 18:02:16 +0000378 //
379 // if auto initialize is not false then skip
380 //
381 if (PcdGetBool(PcdShellLibAutoInitialize) != 0) {
382 return (EFI_SUCCESS);
383 }
384
385 //
386 // deinit the current stuff
387 //
Laszlo Ersek4a7518d2016-06-28 13:52:11 +0200388 Status = ShellLibDestructor (gImageHandle, gST);
389 ASSERT_EFI_ERROR (Status);
jcarseyd2b45642009-05-11 18:02:16 +0000390
391 //
392 // init the new stuff
393 //
394 return (ShellLibConstructorWorker(gImageHandle, gST));
395}
396
jcarsey94b17fa2009-05-07 18:46:18 +0000397/**
jcarsey1e6e84c2010-01-25 20:05:08 +0000398 This function will retrieve the information about the file for the handle
jcarsey94b17fa2009-05-07 18:46:18 +0000399 specified and store it in allocated pool memory.
400
jcarsey1e6e84c2010-01-25 20:05:08 +0000401 This function allocates a buffer to store the file's information. It is the
qhuang869817bf2009-05-20 14:42:48 +0000402 caller's responsibility to free the buffer
jcarsey94b17fa2009-05-07 18:46:18 +0000403
jcarsey1e6e84c2010-01-25 20:05:08 +0000404 @param FileHandle The file handle of the file for which information is
jcarsey94b17fa2009-05-07 18:46:18 +0000405 being requested.
406
407 @retval NULL information could not be retrieved.
408
409 @return the information about the file
410**/
411EFI_FILE_INFO*
412EFIAPI
413ShellGetFileInfo (
jcarseya405b862010-09-14 05:18:09 +0000414 IN SHELL_FILE_HANDLE FileHandle
415 )
416{
jcarseyd2b45642009-05-11 18:02:16 +0000417 return (FileFunctionMap.GetFileInfo(FileHandle));
jcarsey94b17fa2009-05-07 18:46:18 +0000418}
419
420/**
jcarseya405b862010-09-14 05:18:09 +0000421 This function sets the information about the file for the opened handle
jcarsey94b17fa2009-05-07 18:46:18 +0000422 specified.
423
jcarseya405b862010-09-14 05:18:09 +0000424 @param[in] FileHandle The file handle of the file for which information
425 is being set.
jcarsey94b17fa2009-05-07 18:46:18 +0000426
jcarseya405b862010-09-14 05:18:09 +0000427 @param[in] FileInfo The information to set.
jcarsey94b17fa2009-05-07 18:46:18 +0000428
darylm503b0934ac2011-11-12 00:35:11 +0000429 @retval EFI_SUCCESS The information was set.
jcarseya405b862010-09-14 05:18:09 +0000430 @retval EFI_INVALID_PARAMETER A parameter was out of range or invalid.
431 @retval EFI_UNSUPPORTED The FileHandle does not support FileInfo.
darylm503b0934ac2011-11-12 00:35:11 +0000432 @retval EFI_NO_MEDIA The device has no medium.
433 @retval EFI_DEVICE_ERROR The device reported an error.
434 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
435 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
jcarseya405b862010-09-14 05:18:09 +0000436 @retval EFI_ACCESS_DENIED The file was opened read only.
437 @retval EFI_VOLUME_FULL The volume is full.
jcarsey94b17fa2009-05-07 18:46:18 +0000438**/
439EFI_STATUS
440EFIAPI
441ShellSetFileInfo (
darylm503b0934ac2011-11-12 00:35:11 +0000442 IN SHELL_FILE_HANDLE FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000443 IN EFI_FILE_INFO *FileInfo
jcarseya405b862010-09-14 05:18:09 +0000444 )
445{
jcarseyd2b45642009-05-11 18:02:16 +0000446 return (FileFunctionMap.SetFileInfo(FileHandle, FileInfo));
jcarsey1e6e84c2010-01-25 20:05:08 +0000447}
448
jcarsey94b17fa2009-05-07 18:46:18 +0000449 /**
450 This function will open a file or directory referenced by DevicePath.
451
jcarsey1e6e84c2010-01-25 20:05:08 +0000452 This function opens a file with the open mode according to the file path. The
jcarsey94b17fa2009-05-07 18:46:18 +0000453 Attributes is valid only for EFI_FILE_MODE_CREATE.
454
darylm503b0934ac2011-11-12 00:35:11 +0000455 @param FilePath on input the device path to the file. On output
jcarsey94b17fa2009-05-07 18:46:18 +0000456 the remaining device path.
darylm503b0934ac2011-11-12 00:35:11 +0000457 @param DeviceHandle pointer to the system device handle.
458 @param FileHandle pointer to the file handle.
459 @param OpenMode the mode to open the file with.
460 @param Attributes the file's file attributes.
jcarsey94b17fa2009-05-07 18:46:18 +0000461
darylm503b0934ac2011-11-12 00:35:11 +0000462 @retval EFI_SUCCESS The information was set.
463 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
464 @retval EFI_UNSUPPORTED Could not open the file path.
465 @retval EFI_NOT_FOUND The specified file could not be found on the
jcarsey1e6e84c2010-01-25 20:05:08 +0000466 device or the file system could not be found on
jcarsey94b17fa2009-05-07 18:46:18 +0000467 the device.
darylm503b0934ac2011-11-12 00:35:11 +0000468 @retval EFI_NO_MEDIA The device has no medium.
469 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
jcarsey94b17fa2009-05-07 18:46:18 +0000470 medium is no longer supported.
darylm503b0934ac2011-11-12 00:35:11 +0000471 @retval EFI_DEVICE_ERROR The device reported an error.
472 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
473 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
474 @retval EFI_ACCESS_DENIED The file was opened read only.
475 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
jcarsey94b17fa2009-05-07 18:46:18 +0000476 file.
darylm503b0934ac2011-11-12 00:35:11 +0000477 @retval EFI_VOLUME_FULL The volume is full.
jcarsey94b17fa2009-05-07 18:46:18 +0000478**/
479EFI_STATUS
480EFIAPI
481ShellOpenFileByDevicePath(
darylm503b0934ac2011-11-12 00:35:11 +0000482 IN OUT EFI_DEVICE_PATH_PROTOCOL **FilePath,
483 OUT EFI_HANDLE *DeviceHandle,
jcarseya405b862010-09-14 05:18:09 +0000484 OUT SHELL_FILE_HANDLE *FileHandle,
darylm503b0934ac2011-11-12 00:35:11 +0000485 IN UINT64 OpenMode,
486 IN UINT64 Attributes
jcarseya405b862010-09-14 05:18:09 +0000487 )
488{
489 CHAR16 *FileName;
490 EFI_STATUS Status;
jcarsey94b17fa2009-05-07 18:46:18 +0000491 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *EfiSimpleFileSystemProtocol;
jcarseya405b862010-09-14 05:18:09 +0000492 EFI_FILE_PROTOCOL *Handle1;
493 EFI_FILE_PROTOCOL *Handle2;
ydong100b6cb332013-01-25 02:00:22 +0000494 CHAR16 *FnafPathName;
495 UINTN PathLen;
jcarsey94b17fa2009-05-07 18:46:18 +0000496
jcarsey92a54472011-06-27 20:33:13 +0000497 if (FilePath == NULL || FileHandle == NULL || DeviceHandle == NULL) {
498 return (EFI_INVALID_PARAMETER);
499 }
500
jcarsey1e6e84c2010-01-25 20:05:08 +0000501 //
jcarsey94b17fa2009-05-07 18:46:18 +0000502 // which shell interface should we use
503 //
jcarsey366f81a2011-06-27 21:04:22 +0000504 if (gEfiShellProtocol != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +0000505 //
506 // use UEFI Shell 2.0 method.
507 //
jcarsey366f81a2011-06-27 21:04:22 +0000508 FileName = gEfiShellProtocol->GetFilePathFromDevicePath(*FilePath);
jcarsey94b17fa2009-05-07 18:46:18 +0000509 if (FileName == NULL) {
510 return (EFI_INVALID_PARAMETER);
511 }
512 Status = ShellOpenFileByName(FileName, FileHandle, OpenMode, Attributes);
513 FreePool(FileName);
514 return (Status);
jcarsey1e6e84c2010-01-25 20:05:08 +0000515 }
jcarseyd2b45642009-05-11 18:02:16 +0000516
517
518 //
519 // use old shell method.
520 //
jcarsey1e6e84c2010-01-25 20:05:08 +0000521 Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid,
522 FilePath,
jcarseyd2b45642009-05-11 18:02:16 +0000523 DeviceHandle);
524 if (EFI_ERROR (Status)) {
525 return Status;
526 }
527 Status = gBS->OpenProtocol(*DeviceHandle,
528 &gEfiSimpleFileSystemProtocolGuid,
jcarseyb1f95a02009-06-16 00:23:19 +0000529 (VOID**)&EfiSimpleFileSystemProtocol,
jcarseyd2b45642009-05-11 18:02:16 +0000530 gImageHandle,
531 NULL,
532 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
533 if (EFI_ERROR (Status)) {
534 return Status;
535 }
jcarseya405b862010-09-14 05:18:09 +0000536 Status = EfiSimpleFileSystemProtocol->OpenVolume(EfiSimpleFileSystemProtocol, &Handle1);
jcarseyd2b45642009-05-11 18:02:16 +0000537 if (EFI_ERROR (Status)) {
538 FileHandle = NULL;
539 return Status;
540 }
541
542 //
543 // go down directories one node at a time.
544 //
545 while (!IsDevicePathEnd (*FilePath)) {
jcarsey94b17fa2009-05-07 18:46:18 +0000546 //
jcarseyd2b45642009-05-11 18:02:16 +0000547 // For file system access each node should be a file path component
jcarsey94b17fa2009-05-07 18:46:18 +0000548 //
jcarseyd2b45642009-05-11 18:02:16 +0000549 if (DevicePathType (*FilePath) != MEDIA_DEVICE_PATH ||
550 DevicePathSubType (*FilePath) != MEDIA_FILEPATH_DP
jcarseya405b862010-09-14 05:18:09 +0000551 ) {
jcarsey94b17fa2009-05-07 18:46:18 +0000552 FileHandle = NULL;
jcarseyd2b45642009-05-11 18:02:16 +0000553 return (EFI_INVALID_PARAMETER);
jcarsey94b17fa2009-05-07 18:46:18 +0000554 }
jcarseyd2b45642009-05-11 18:02:16 +0000555 //
556 // Open this file path node
557 //
jcarseya405b862010-09-14 05:18:09 +0000558 Handle2 = Handle1;
559 Handle1 = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +0000560
561 //
ydong100b6cb332013-01-25 02:00:22 +0000562 // File Name Alignment Fix (FNAF)
563 // Handle2->Open may be incapable of handling a unaligned CHAR16 data.
564 // The structure pointed to by FilePath may be not CHAR16 aligned.
565 // This code copies the potentially unaligned PathName data from the
566 // FilePath structure to the aligned FnafPathName for use in the
567 // calls to Handl2->Open.
568 //
569
570 //
571 // Determine length of PathName, in bytes.
572 //
573 PathLen = DevicePathNodeLength (*FilePath) - SIZE_OF_FILEPATH_DEVICE_PATH;
574
575 //
576 // Allocate memory for the aligned copy of the string Extra allocation is to allow for forced alignment
577 // Copy bytes from possibly unaligned location to aligned location
578 //
579 FnafPathName = AllocateCopyPool(PathLen, (UINT8 *)((FILEPATH_DEVICE_PATH*)*FilePath)->PathName);
580 if (FnafPathName == NULL) {
581 return EFI_OUT_OF_RESOURCES;
582 }
583
584 //
jcarseyd2b45642009-05-11 18:02:16 +0000585 // Try to test opening an existing file
jcarsey94b17fa2009-05-07 18:46:18 +0000586 //
jcarseya405b862010-09-14 05:18:09 +0000587 Status = Handle2->Open (
588 Handle2,
589 &Handle1,
ydong100b6cb332013-01-25 02:00:22 +0000590 FnafPathName,
jcarseyd2b45642009-05-11 18:02:16 +0000591 OpenMode &~EFI_FILE_MODE_CREATE,
592 0
jcarseya405b862010-09-14 05:18:09 +0000593 );
jcarsey94b17fa2009-05-07 18:46:18 +0000594
jcarseyd2b45642009-05-11 18:02:16 +0000595 //
596 // see if the error was that it needs to be created
597 //
598 if ((EFI_ERROR (Status)) && (OpenMode != (OpenMode &~EFI_FILE_MODE_CREATE))) {
jcarseya405b862010-09-14 05:18:09 +0000599 Status = Handle2->Open (
600 Handle2,
601 &Handle1,
ydong100b6cb332013-01-25 02:00:22 +0000602 FnafPathName,
jcarseyd2b45642009-05-11 18:02:16 +0000603 OpenMode,
604 Attributes
jcarseya405b862010-09-14 05:18:09 +0000605 );
jcarsey94b17fa2009-05-07 18:46:18 +0000606 }
ydong100b6cb332013-01-25 02:00:22 +0000607
608 //
609 // Free the alignment buffer
610 //
611 FreePool(FnafPathName);
612
jcarseyd2b45642009-05-11 18:02:16 +0000613 //
614 // Close the last node
615 //
jcarseya405b862010-09-14 05:18:09 +0000616 Handle2->Close (Handle2);
jcarseyd2b45642009-05-11 18:02:16 +0000617
618 if (EFI_ERROR(Status)) {
619 return (Status);
620 }
621
622 //
623 // Get the next node
624 //
625 *FilePath = NextDevicePathNode (*FilePath);
jcarsey94b17fa2009-05-07 18:46:18 +0000626 }
jcarseya405b862010-09-14 05:18:09 +0000627
628 //
629 // This is a weak spot since if the undefined SHELL_FILE_HANDLE format changes this must change also!
630 //
631 *FileHandle = (VOID*)Handle1;
jcarseyd2b45642009-05-11 18:02:16 +0000632 return (EFI_SUCCESS);
jcarsey94b17fa2009-05-07 18:46:18 +0000633}
634
635/**
636 This function will open a file or directory referenced by filename.
637
jcarsey1e6e84c2010-01-25 20:05:08 +0000638 If return is EFI_SUCCESS, the Filehandle is the opened file's handle;
639 otherwise, the Filehandle is NULL. The Attributes is valid only for
jcarsey94b17fa2009-05-07 18:46:18 +0000640 EFI_FILE_MODE_CREATE.
641
jcarsey92a54472011-06-27 20:33:13 +0000642 if FileName is NULL then ASSERT()
jcarsey94b17fa2009-05-07 18:46:18 +0000643
darylm503b0934ac2011-11-12 00:35:11 +0000644 @param FileName pointer to file name
645 @param FileHandle pointer to the file handle.
646 @param OpenMode the mode to open the file with.
647 @param Attributes the file's file attributes.
jcarsey94b17fa2009-05-07 18:46:18 +0000648
darylm503b0934ac2011-11-12 00:35:11 +0000649 @retval EFI_SUCCESS The information was set.
650 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
651 @retval EFI_UNSUPPORTED Could not open the file path.
652 @retval EFI_NOT_FOUND The specified file could not be found on the
jcarsey1e6e84c2010-01-25 20:05:08 +0000653 device or the file system could not be found
jcarsey94b17fa2009-05-07 18:46:18 +0000654 on the device.
darylm503b0934ac2011-11-12 00:35:11 +0000655 @retval EFI_NO_MEDIA The device has no medium.
656 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
jcarsey94b17fa2009-05-07 18:46:18 +0000657 medium is no longer supported.
darylm503b0934ac2011-11-12 00:35:11 +0000658 @retval EFI_DEVICE_ERROR The device reported an error.
659 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
660 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
661 @retval EFI_ACCESS_DENIED The file was opened read only.
662 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
jcarsey94b17fa2009-05-07 18:46:18 +0000663 file.
darylm503b0934ac2011-11-12 00:35:11 +0000664 @retval EFI_VOLUME_FULL The volume is full.
jcarsey94b17fa2009-05-07 18:46:18 +0000665**/
666EFI_STATUS
667EFIAPI
668ShellOpenFileByName(
darylm503b0934ac2011-11-12 00:35:11 +0000669 IN CONST CHAR16 *FileName,
jcarseya405b862010-09-14 05:18:09 +0000670 OUT SHELL_FILE_HANDLE *FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000671 IN UINT64 OpenMode,
darylm503b0934ac2011-11-12 00:35:11 +0000672 IN UINT64 Attributes
jcarseya405b862010-09-14 05:18:09 +0000673 )
674{
jcarsey94b17fa2009-05-07 18:46:18 +0000675 EFI_HANDLE DeviceHandle;
676 EFI_DEVICE_PATH_PROTOCOL *FilePath;
jcarseyb1f95a02009-06-16 00:23:19 +0000677 EFI_STATUS Status;
678 EFI_FILE_INFO *FileInfo;
jaben carsey21a86a72015-01-13 22:16:41 +0000679 CHAR16 *FileNameCopy;
Yao Jiewendd17e3f2015-12-25 01:24:16 +0000680 EFI_STATUS Status2;
jcarsey94b17fa2009-05-07 18:46:18 +0000681
682 //
683 // ASSERT if FileName is NULL
684 //
685 ASSERT(FileName != NULL);
686
jcarseya405b862010-09-14 05:18:09 +0000687 if (FileName == NULL) {
688 return (EFI_INVALID_PARAMETER);
689 }
690
jcarsey366f81a2011-06-27 21:04:22 +0000691 if (gEfiShellProtocol != NULL) {
jaben carsey21a86a72015-01-13 22:16:41 +0000692 if ((OpenMode & EFI_FILE_MODE_CREATE) == EFI_FILE_MODE_CREATE) {
693
694 //
695 // Create only a directory
696 //
697 if ((Attributes & EFI_FILE_DIRECTORY) == EFI_FILE_DIRECTORY) {
698 return ShellCreateDirectory(FileName, FileHandle);
699 }
700
701 //
702 // Create the directory to create the file in
703 //
704 FileNameCopy = AllocateCopyPool (StrSize (FileName), FileName);
Jeff Westfahl121e15a2017-05-13 04:41:40 +0800705 if (FileNameCopy == NULL) {
jaben carsey21a86a72015-01-13 22:16:41 +0000706 return (EFI_OUT_OF_RESOURCES);
707 }
708 PathCleanUpDirectories (FileNameCopy);
709 if (PathRemoveLastItem (FileNameCopy)) {
Jaben Carsey983fffd2015-07-28 20:22:26 +0000710 if (!EFI_ERROR(ShellCreateDirectory (FileNameCopy, FileHandle))) {
711 ShellCloseFile (FileHandle);
712 }
jaben carsey21a86a72015-01-13 22:16:41 +0000713 }
714 SHELL_FREE_NON_NULL (FileNameCopy);
jcarseya405b862010-09-14 05:18:09 +0000715 }
jaben carsey21a86a72015-01-13 22:16:41 +0000716
jcarsey94b17fa2009-05-07 18:46:18 +0000717 //
jaben carsey21a86a72015-01-13 22:16:41 +0000718 // Use UEFI Shell 2.0 method to create the file
jcarsey94b17fa2009-05-07 18:46:18 +0000719 //
jcarsey366f81a2011-06-27 21:04:22 +0000720 Status = gEfiShellProtocol->OpenFileByName(FileName,
jcarseyb1f95a02009-06-16 00:23:19 +0000721 FileHandle,
722 OpenMode);
Vladimir Olovyannikovf9c3b1b2016-10-06 15:02:26 -0700723 if (EFI_ERROR(Status)) {
724 return Status;
725 }
Tapan Shah583448b2016-09-22 12:12:47 -0700726
Tapan Shah2cf9ecd2016-10-05 13:58:05 -0700727 if (mUnicodeCollationProtocol == NULL) {
728 Status = gBS->LocateProtocol (&gEfiUnicodeCollation2ProtocolGuid, NULL, (VOID**)&mUnicodeCollationProtocol);
729 if (EFI_ERROR (Status)) {
730 gEfiShellProtocol->CloseFile (*FileHandle);
731 return Status;
732 }
733 }
734
Tapan Shah583448b2016-09-22 12:12:47 -0700735 if ((mUnicodeCollationProtocol->StriColl (mUnicodeCollationProtocol, (CHAR16*)FileName, L"NUL") != 0) &&
736 (mUnicodeCollationProtocol->StriColl (mUnicodeCollationProtocol, (CHAR16*)FileName, L"NULL") != 0) &&
737 !EFI_ERROR(Status) && ((OpenMode & EFI_FILE_MODE_CREATE) != 0)){
jcarsey2247dde2009-11-09 18:08:58 +0000738 FileInfo = FileFunctionMap.GetFileInfo(*FileHandle);
jcarseyb1f95a02009-06-16 00:23:19 +0000739 ASSERT(FileInfo != NULL);
740 FileInfo->Attribute = Attributes;
Yao Jiewendd17e3f2015-12-25 01:24:16 +0000741 Status2 = FileFunctionMap.SetFileInfo(*FileHandle, FileInfo);
jcarsey2247dde2009-11-09 18:08:58 +0000742 FreePool(FileInfo);
Yao Jiewendd17e3f2015-12-25 01:24:16 +0000743 if (EFI_ERROR (Status2)) {
744 gEfiShellProtocol->CloseFile(*FileHandle);
745 }
746 Status = Status2;
jcarseyb1f95a02009-06-16 00:23:19 +0000747 }
748 return (Status);
jcarsey1e6e84c2010-01-25 20:05:08 +0000749 }
jcarsey94b17fa2009-05-07 18:46:18 +0000750 //
751 // Using EFI Shell version
752 // this means convert name to path and call that function
753 // since this will use EFI method again that will open it.
754 //
755 ASSERT(mEfiShellEnvironment2 != NULL);
jcarseyb82bfcc2009-06-29 16:28:23 +0000756 FilePath = mEfiShellEnvironment2->NameToPath ((CHAR16*)FileName);
xdu290bfa222010-07-19 05:21:27 +0000757 if (FilePath != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +0000758 return (ShellOpenFileByDevicePath(&FilePath,
759 &DeviceHandle,
760 FileHandle,
761 OpenMode,
jcarseya405b862010-09-14 05:18:09 +0000762 Attributes));
jcarsey94b17fa2009-05-07 18:46:18 +0000763 }
764 return (EFI_DEVICE_ERROR);
765}
766/**
767 This function create a directory
768
jcarsey1e6e84c2010-01-25 20:05:08 +0000769 If return is EFI_SUCCESS, the Filehandle is the opened directory's handle;
770 otherwise, the Filehandle is NULL. If the directory already existed, this
jcarsey94b17fa2009-05-07 18:46:18 +0000771 function opens the existing directory.
772
darylm503b0934ac2011-11-12 00:35:11 +0000773 @param DirectoryName pointer to directory name
774 @param FileHandle pointer to the file handle.
jcarsey94b17fa2009-05-07 18:46:18 +0000775
darylm503b0934ac2011-11-12 00:35:11 +0000776 @retval EFI_SUCCESS The information was set.
777 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
778 @retval EFI_UNSUPPORTED Could not open the file path.
779 @retval EFI_NOT_FOUND The specified file could not be found on the
jcarsey1e6e84c2010-01-25 20:05:08 +0000780 device or the file system could not be found
jcarsey94b17fa2009-05-07 18:46:18 +0000781 on the device.
darylm503b0934ac2011-11-12 00:35:11 +0000782 @retval EFI_NO_MEDIA The device has no medium.
783 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
jcarsey94b17fa2009-05-07 18:46:18 +0000784 medium is no longer supported.
darylm503b0934ac2011-11-12 00:35:11 +0000785 @retval EFI_DEVICE_ERROR The device reported an error.
786 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
787 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
788 @retval EFI_ACCESS_DENIED The file was opened read only.
789 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
jcarsey94b17fa2009-05-07 18:46:18 +0000790 file.
darylm503b0934ac2011-11-12 00:35:11 +0000791 @retval EFI_VOLUME_FULL The volume is full.
jcarsey94b17fa2009-05-07 18:46:18 +0000792 @sa ShellOpenFileByName
793**/
794EFI_STATUS
795EFIAPI
796ShellCreateDirectory(
jcarseyb82bfcc2009-06-29 16:28:23 +0000797 IN CONST CHAR16 *DirectoryName,
jcarseya405b862010-09-14 05:18:09 +0000798 OUT SHELL_FILE_HANDLE *FileHandle
799 )
800{
jcarsey366f81a2011-06-27 21:04:22 +0000801 if (gEfiShellProtocol != NULL) {
jcarsey2247dde2009-11-09 18:08:58 +0000802 //
803 // Use UEFI Shell 2.0 method
804 //
jcarsey366f81a2011-06-27 21:04:22 +0000805 return (gEfiShellProtocol->CreateFile(DirectoryName,
jcarsey2247dde2009-11-09 18:08:58 +0000806 EFI_FILE_DIRECTORY,
807 FileHandle
jcarseya405b862010-09-14 05:18:09 +0000808 ));
jcarsey2247dde2009-11-09 18:08:58 +0000809 } else {
810 return (ShellOpenFileByName(DirectoryName,
811 FileHandle,
812 EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE,
813 EFI_FILE_DIRECTORY
jcarseya405b862010-09-14 05:18:09 +0000814 ));
jcarsey2247dde2009-11-09 18:08:58 +0000815 }
jcarsey94b17fa2009-05-07 18:46:18 +0000816}
817
818/**
819 This function reads information from an opened file.
820
jcarsey1e6e84c2010-01-25 20:05:08 +0000821 If FileHandle is not a directory, the function reads the requested number of
822 bytes from the file at the file's current position and returns them in Buffer.
jcarsey94b17fa2009-05-07 18:46:18 +0000823 If the read goes beyond the end of the file, the read length is truncated to the
jcarsey1e6e84c2010-01-25 20:05:08 +0000824 end of the file. The file's current position is increased by the number of bytes
825 returned. If FileHandle is a directory, the function reads the directory entry
826 at the file's current position and returns the entry in Buffer. If the Buffer
827 is not large enough to hold the current directory entry, then
828 EFI_BUFFER_TOO_SMALL is returned and the current file position is not updated.
829 BufferSize is set to be the size of the buffer needed to read the entry. On
830 success, the current position is updated to the next directory entry. If there
831 are no more directory entries, the read returns a zero-length buffer.
jcarsey94b17fa2009-05-07 18:46:18 +0000832 EFI_FILE_INFO is the structure returned as the directory entry.
833
834 @param FileHandle the opened file handle
jcarsey1e6e84c2010-01-25 20:05:08 +0000835 @param BufferSize on input the size of buffer in bytes. on return
jcarsey94b17fa2009-05-07 18:46:18 +0000836 the number of bytes written.
837 @param Buffer the buffer to put read data into.
838
darylm503b0934ac2011-11-12 00:35:11 +0000839 @retval EFI_SUCCESS Data was read.
840 @retval EFI_NO_MEDIA The device has no media.
841 @retval EFI_DEVICE_ERROR The device reported an error.
842 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
843 @retval EFI_BUFFER_TO_SMALL Buffer is too small. ReadSize contains required
jcarsey94b17fa2009-05-07 18:46:18 +0000844 size.
845
846**/
847EFI_STATUS
848EFIAPI
849ShellReadFile(
jcarseya405b862010-09-14 05:18:09 +0000850 IN SHELL_FILE_HANDLE FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000851 IN OUT UINTN *BufferSize,
852 OUT VOID *Buffer
jcarseya405b862010-09-14 05:18:09 +0000853 )
854{
jcarseyd2b45642009-05-11 18:02:16 +0000855 return (FileFunctionMap.ReadFile(FileHandle, BufferSize, Buffer));
jcarsey94b17fa2009-05-07 18:46:18 +0000856}
857
858
859/**
860 Write data to a file.
861
jcarsey1e6e84c2010-01-25 20:05:08 +0000862 This function writes the specified number of bytes to the file at the current
863 file position. The current file position is advanced the actual number of bytes
864 written, which is returned in BufferSize. Partial writes only occur when there
865 has been a data error during the write attempt (such as "volume space full").
866 The file is automatically grown to hold the data if required. Direct writes to
jcarsey94b17fa2009-05-07 18:46:18 +0000867 opened directories are not supported.
868
869 @param FileHandle The opened file for writing
870 @param BufferSize on input the number of bytes in Buffer. On output
871 the number of bytes written.
872 @param Buffer the buffer containing data to write is stored.
873
darylm503b0934ac2011-11-12 00:35:11 +0000874 @retval EFI_SUCCESS Data was written.
875 @retval EFI_UNSUPPORTED Writes to an open directory are not supported.
876 @retval EFI_NO_MEDIA The device has no media.
877 @retval EFI_DEVICE_ERROR The device reported an error.
878 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
879 @retval EFI_WRITE_PROTECTED The device is write-protected.
880 @retval EFI_ACCESS_DENIED The file was open for read only.
881 @retval EFI_VOLUME_FULL The volume is full.
jcarsey94b17fa2009-05-07 18:46:18 +0000882**/
883EFI_STATUS
884EFIAPI
885ShellWriteFile(
jcarsey252d9452011-03-25 20:49:53 +0000886 IN SHELL_FILE_HANDLE FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000887 IN OUT UINTN *BufferSize,
888 IN VOID *Buffer
jcarseya405b862010-09-14 05:18:09 +0000889 )
890{
jcarseyd2b45642009-05-11 18:02:16 +0000891 return (FileFunctionMap.WriteFile(FileHandle, BufferSize, Buffer));
jcarsey94b17fa2009-05-07 18:46:18 +0000892}
893
jcarsey1e6e84c2010-01-25 20:05:08 +0000894/**
jcarsey94b17fa2009-05-07 18:46:18 +0000895 Close an open file handle.
896
jcarsey1e6e84c2010-01-25 20:05:08 +0000897 This function closes a specified file handle. All "dirty" cached file data is
898 flushed to the device, and the file is closed. In all cases the handle is
jcarsey94b17fa2009-05-07 18:46:18 +0000899 closed.
900
901@param FileHandle the file handle to close.
902
903@retval EFI_SUCCESS the file handle was closed sucessfully.
904**/
905EFI_STATUS
906EFIAPI
907ShellCloseFile (
jcarseya405b862010-09-14 05:18:09 +0000908 IN SHELL_FILE_HANDLE *FileHandle
909 )
910{
jcarseyd2b45642009-05-11 18:02:16 +0000911 return (FileFunctionMap.CloseFile(*FileHandle));
jcarsey94b17fa2009-05-07 18:46:18 +0000912}
913
914/**
915 Delete a file and close the handle
916
917 This function closes and deletes a file. In all cases the file handle is closed.
jcarsey1e6e84c2010-01-25 20:05:08 +0000918 If the file cannot be deleted, the warning code EFI_WARN_DELETE_FAILURE is
jcarsey94b17fa2009-05-07 18:46:18 +0000919 returned, but the handle is still closed.
920
921 @param FileHandle the file handle to delete
922
923 @retval EFI_SUCCESS the file was closed sucessfully
jcarsey1e6e84c2010-01-25 20:05:08 +0000924 @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not
jcarsey94b17fa2009-05-07 18:46:18 +0000925 deleted
darylm503b0934ac2011-11-12 00:35:11 +0000926 @retval INVALID_PARAMETER One of the parameters has an invalid value.
jcarsey94b17fa2009-05-07 18:46:18 +0000927**/
928EFI_STATUS
929EFIAPI
930ShellDeleteFile (
darylm503b0934ac2011-11-12 00:35:11 +0000931 IN SHELL_FILE_HANDLE *FileHandle
jcarseya405b862010-09-14 05:18:09 +0000932 )
933{
jcarseyd2b45642009-05-11 18:02:16 +0000934 return (FileFunctionMap.DeleteFile(*FileHandle));
jcarsey94b17fa2009-05-07 18:46:18 +0000935}
936
937/**
938 Set the current position in a file.
939
jcarsey1e6e84c2010-01-25 20:05:08 +0000940 This function sets the current file position for the handle to the position
jcarsey94b17fa2009-05-07 18:46:18 +0000941 supplied. With the exception of seeking to position 0xFFFFFFFFFFFFFFFF, only
jcarsey1e6e84c2010-01-25 20:05:08 +0000942 absolute positioning is supported, and seeking past the end of the file is
943 allowed (a subsequent write would grow the file). Seeking to position
jcarsey94b17fa2009-05-07 18:46:18 +0000944 0xFFFFFFFFFFFFFFFF causes the current position to be set to the end of the file.
jcarsey1e6e84c2010-01-25 20:05:08 +0000945 If FileHandle is a directory, the only position that may be set is zero. This
jcarsey94b17fa2009-05-07 18:46:18 +0000946 has the effect of starting the read process of the directory entries over.
947
948 @param FileHandle The file handle on which the position is being set
949 @param Position Byte position from begining of file
950
951 @retval EFI_SUCCESS Operation completed sucessfully.
jcarsey1e6e84c2010-01-25 20:05:08 +0000952 @retval EFI_UNSUPPORTED the seek request for non-zero is not valid on
jcarsey94b17fa2009-05-07 18:46:18 +0000953 directories.
954 @retval INVALID_PARAMETER One of the parameters has an invalid value.
955**/
956EFI_STATUS
957EFIAPI
958ShellSetFilePosition (
darylm503b0934ac2011-11-12 00:35:11 +0000959 IN SHELL_FILE_HANDLE FileHandle,
960 IN UINT64 Position
jcarseya405b862010-09-14 05:18:09 +0000961 )
962{
jcarseyd2b45642009-05-11 18:02:16 +0000963 return (FileFunctionMap.SetFilePosition(FileHandle, Position));
jcarsey94b17fa2009-05-07 18:46:18 +0000964}
965
jcarsey1e6e84c2010-01-25 20:05:08 +0000966/**
jcarsey94b17fa2009-05-07 18:46:18 +0000967 Gets a file's current position
968
jcarsey1e6e84c2010-01-25 20:05:08 +0000969 This function retrieves the current file position for the file handle. For
970 directories, the current file position has no meaning outside of the file
jcarsey94b17fa2009-05-07 18:46:18 +0000971 system driver and as such the operation is not supported. An error is returned
972 if FileHandle is a directory.
973
974 @param FileHandle The open file handle on which to get the position.
975 @param Position Byte position from begining of file.
976
977 @retval EFI_SUCCESS the operation completed sucessfully.
978 @retval INVALID_PARAMETER One of the parameters has an invalid value.
979 @retval EFI_UNSUPPORTED the request is not valid on directories.
980**/
981EFI_STATUS
982EFIAPI
983ShellGetFilePosition (
jcarseya405b862010-09-14 05:18:09 +0000984 IN SHELL_FILE_HANDLE FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000985 OUT UINT64 *Position
jcarseya405b862010-09-14 05:18:09 +0000986 )
987{
jcarseyd2b45642009-05-11 18:02:16 +0000988 return (FileFunctionMap.GetFilePosition(FileHandle, Position));
jcarsey94b17fa2009-05-07 18:46:18 +0000989}
990/**
991 Flushes data on a file
jcarsey1e6e84c2010-01-25 20:05:08 +0000992
jcarsey94b17fa2009-05-07 18:46:18 +0000993 This function flushes all modified data associated with a file to a device.
994
995 @param FileHandle The file handle on which to flush data
996
997 @retval EFI_SUCCESS The data was flushed.
998 @retval EFI_NO_MEDIA The device has no media.
999 @retval EFI_DEVICE_ERROR The device reported an error.
1000 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
1001 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
1002 @retval EFI_ACCESS_DENIED The file was opened for read only.
1003**/
1004EFI_STATUS
1005EFIAPI
1006ShellFlushFile (
jcarseya405b862010-09-14 05:18:09 +00001007 IN SHELL_FILE_HANDLE FileHandle
1008 )
1009{
jcarseyd2b45642009-05-11 18:02:16 +00001010 return (FileFunctionMap.FlushFile(FileHandle));
jcarsey94b17fa2009-05-07 18:46:18 +00001011}
1012
darylm503b0934ac2011-11-12 00:35:11 +00001013/** Retrieve first entry from a directory.
jcarsey94b17fa2009-05-07 18:46:18 +00001014
darylm503b0934ac2011-11-12 00:35:11 +00001015 This function takes an open directory handle and gets information from the
1016 first entry in the directory. A buffer is allocated to contain
1017 the information and a pointer to the buffer is returned in *Buffer. The
1018 caller can use ShellFindNextFile() to get subsequent directory entries.
jcarsey94b17fa2009-05-07 18:46:18 +00001019
darylm503b0934ac2011-11-12 00:35:11 +00001020 The buffer will be freed by ShellFindNextFile() when the last directory
1021 entry is read. Otherwise, the caller must free the buffer, using FreePool,
1022 when finished with it.
1023
1024 @param[in] DirHandle The file handle of the directory to search.
1025 @param[out] Buffer The pointer to the buffer for the file's information.
jcarsey94b17fa2009-05-07 18:46:18 +00001026
1027 @retval EFI_SUCCESS Found the first file.
1028 @retval EFI_NOT_FOUND Cannot find the directory.
1029 @retval EFI_NO_MEDIA The device has no media.
1030 @retval EFI_DEVICE_ERROR The device reported an error.
1031 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
1032 @return Others status of ShellGetFileInfo, ShellSetFilePosition,
1033 or ShellReadFile
1034**/
1035EFI_STATUS
1036EFIAPI
1037ShellFindFirstFile (
jcarseya405b862010-09-14 05:18:09 +00001038 IN SHELL_FILE_HANDLE DirHandle,
jcarseyd2b45642009-05-11 18:02:16 +00001039 OUT EFI_FILE_INFO **Buffer
jcarseya405b862010-09-14 05:18:09 +00001040 )
1041{
jcarsey94b17fa2009-05-07 18:46:18 +00001042 //
jcarseyd2b45642009-05-11 18:02:16 +00001043 // pass to file handle lib
jcarsey94b17fa2009-05-07 18:46:18 +00001044 //
jcarseyd2b45642009-05-11 18:02:16 +00001045 return (FileHandleFindFirstFile(DirHandle, Buffer));
jcarsey94b17fa2009-05-07 18:46:18 +00001046}
darylm503b0934ac2011-11-12 00:35:11 +00001047/** Retrieve next entries from a directory.
jcarsey94b17fa2009-05-07 18:46:18 +00001048
darylm503b0934ac2011-11-12 00:35:11 +00001049 To use this function, the caller must first call the ShellFindFirstFile()
1050 function to get the first directory entry. Subsequent directory entries are
1051 retrieved by using the ShellFindNextFile() function. This function can
1052 be called several times to get each entry from the directory. If the call of
1053 ShellFindNextFile() retrieved the last directory entry, the next call of
1054 this function will set *NoFile to TRUE and free the buffer.
jcarsey94b17fa2009-05-07 18:46:18 +00001055
darylm503b0934ac2011-11-12 00:35:11 +00001056 @param[in] DirHandle The file handle of the directory.
1057 @param[out] Buffer The pointer to buffer for file's information.
1058 @param[out] NoFile The pointer to boolean when last file is found.
jcarsey94b17fa2009-05-07 18:46:18 +00001059
1060 @retval EFI_SUCCESS Found the next file, or reached last file
1061 @retval EFI_NO_MEDIA The device has no media.
1062 @retval EFI_DEVICE_ERROR The device reported an error.
1063 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
1064**/
1065EFI_STATUS
1066EFIAPI
1067ShellFindNextFile(
jcarseya405b862010-09-14 05:18:09 +00001068 IN SHELL_FILE_HANDLE DirHandle,
jcarsey94b17fa2009-05-07 18:46:18 +00001069 OUT EFI_FILE_INFO *Buffer,
1070 OUT BOOLEAN *NoFile
jcarseya405b862010-09-14 05:18:09 +00001071 )
1072{
jcarsey94b17fa2009-05-07 18:46:18 +00001073 //
jcarseyd2b45642009-05-11 18:02:16 +00001074 // pass to file handle lib
jcarsey94b17fa2009-05-07 18:46:18 +00001075 //
jcarseyd2b45642009-05-11 18:02:16 +00001076 return (FileHandleFindNextFile(DirHandle, Buffer, NoFile));
jcarsey94b17fa2009-05-07 18:46:18 +00001077}
1078/**
1079 Retrieve the size of a file.
1080
1081 if FileHandle is NULL then ASSERT()
1082 if Size is NULL then ASSERT()
1083
jcarsey1e6e84c2010-01-25 20:05:08 +00001084 This function extracts the file size info from the FileHandle's EFI_FILE_INFO
jcarsey94b17fa2009-05-07 18:46:18 +00001085 data.
1086
1087 @param FileHandle file handle from which size is retrieved
1088 @param Size pointer to size
1089
1090 @retval EFI_SUCCESS operation was completed sucessfully
1091 @retval EFI_DEVICE_ERROR cannot access the file
1092**/
1093EFI_STATUS
1094EFIAPI
1095ShellGetFileSize (
jcarseya405b862010-09-14 05:18:09 +00001096 IN SHELL_FILE_HANDLE FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +00001097 OUT UINT64 *Size
jcarseya405b862010-09-14 05:18:09 +00001098 )
1099{
jcarseyd2b45642009-05-11 18:02:16 +00001100 return (FileFunctionMap.GetFileSize(FileHandle, Size));
jcarsey94b17fa2009-05-07 18:46:18 +00001101}
1102/**
1103 Retrieves the status of the break execution flag
1104
1105 this function is useful to check whether the application is being asked to halt by the shell.
1106
1107 @retval TRUE the execution break is enabled
1108 @retval FALSE the execution break is not enabled
1109**/
1110BOOLEAN
1111EFIAPI
1112ShellGetExecutionBreakFlag(
1113 VOID
1114 )
1115{
jcarsey1e6e84c2010-01-25 20:05:08 +00001116 //
jcarsey94b17fa2009-05-07 18:46:18 +00001117 // Check for UEFI Shell 2.0 protocols
1118 //
jcarsey366f81a2011-06-27 21:04:22 +00001119 if (gEfiShellProtocol != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00001120
1121 //
1122 // We are using UEFI Shell 2.0; see if the event has been triggered
1123 //
jcarsey366f81a2011-06-27 21:04:22 +00001124 if (gBS->CheckEvent(gEfiShellProtocol->ExecutionBreak) != EFI_SUCCESS) {
jcarsey94b17fa2009-05-07 18:46:18 +00001125 return (FALSE);
1126 }
1127 return (TRUE);
jcarsey1e6e84c2010-01-25 20:05:08 +00001128 }
jcarsey94b17fa2009-05-07 18:46:18 +00001129
1130 //
1131 // using EFI Shell; call the function to check
1132 //
jcarsey92a54472011-06-27 20:33:13 +00001133 if (mEfiShellEnvironment2 != NULL) {
1134 return (mEfiShellEnvironment2->GetExecutionBreak());
1135 }
1136
1137 return (FALSE);
jcarsey94b17fa2009-05-07 18:46:18 +00001138}
1139/**
1140 return the value of an environment variable
1141
jcarsey1e6e84c2010-01-25 20:05:08 +00001142 this function gets the value of the environment variable set by the
jcarsey94b17fa2009-05-07 18:46:18 +00001143 ShellSetEnvironmentVariable function
1144
1145 @param EnvKey The key name of the environment variable.
1146
1147 @retval NULL the named environment variable does not exist.
1148 @return != NULL pointer to the value of the environment variable
1149**/
1150CONST CHAR16*
1151EFIAPI
1152ShellGetEnvironmentVariable (
jcarsey9b3bf082009-06-23 21:15:07 +00001153 IN CONST CHAR16 *EnvKey
jcarsey94b17fa2009-05-07 18:46:18 +00001154 )
1155{
jcarsey1e6e84c2010-01-25 20:05:08 +00001156 //
jcarsey94b17fa2009-05-07 18:46:18 +00001157 // Check for UEFI Shell 2.0 protocols
1158 //
jcarsey366f81a2011-06-27 21:04:22 +00001159 if (gEfiShellProtocol != NULL) {
1160 return (gEfiShellProtocol->GetEnv(EnvKey));
jcarsey94b17fa2009-05-07 18:46:18 +00001161 }
1162
1163 //
jcarsey92a54472011-06-27 20:33:13 +00001164 // Check for EFI shell
jcarsey94b17fa2009-05-07 18:46:18 +00001165 //
jcarsey92a54472011-06-27 20:33:13 +00001166 if (mEfiShellEnvironment2 != NULL) {
1167 return (mEfiShellEnvironment2->GetEnv((CHAR16*)EnvKey));
1168 }
jcarsey94b17fa2009-05-07 18:46:18 +00001169
jcarsey92a54472011-06-27 20:33:13 +00001170 return NULL;
jcarsey94b17fa2009-05-07 18:46:18 +00001171}
1172/**
1173 set the value of an environment variable
1174
1175This function changes the current value of the specified environment variable. If the
1176environment variable exists and the Value is an empty string, then the environment
1177variable is deleted. If the environment variable exists and the Value is not an empty
1178string, then the value of the environment variable is changed. If the environment
1179variable does not exist and the Value is an empty string, there is no action. If the
1180environment variable does not exist and the Value is a non-empty string, then the
1181environment variable is created and assigned the specified value.
1182
1183 This is not supported pre-UEFI Shell 2.0.
1184
1185 @param EnvKey The key name of the environment variable.
1186 @param EnvVal The Value of the environment variable
1187 @param Volatile Indicates whether the variable is non-volatile (FALSE) or volatile (TRUE).
1188
1189 @retval EFI_SUCCESS the operation was completed sucessfully
1190 @retval EFI_UNSUPPORTED This operation is not allowed in pre UEFI 2.0 Shell environments
1191**/
1192EFI_STATUS
1193EFIAPI
1194ShellSetEnvironmentVariable (
1195 IN CONST CHAR16 *EnvKey,
1196 IN CONST CHAR16 *EnvVal,
1197 IN BOOLEAN Volatile
1198 )
1199{
jcarsey1e6e84c2010-01-25 20:05:08 +00001200 //
jcarsey94b17fa2009-05-07 18:46:18 +00001201 // Check for UEFI Shell 2.0 protocols
1202 //
jcarsey366f81a2011-06-27 21:04:22 +00001203 if (gEfiShellProtocol != NULL) {
1204 return (gEfiShellProtocol->SetEnv(EnvKey, EnvVal, Volatile));
jcarsey1e6e84c2010-01-25 20:05:08 +00001205 }
jcarsey94b17fa2009-05-07 18:46:18 +00001206
1207 //
1208 // This feature does not exist under EFI shell
1209 //
1210 return (EFI_UNSUPPORTED);
1211}
jcarseya405b862010-09-14 05:18:09 +00001212
jcarsey94b17fa2009-05-07 18:46:18 +00001213/**
jcarseya405b862010-09-14 05:18:09 +00001214 Cause the shell to parse and execute a command line.
jcarsey94b17fa2009-05-07 18:46:18 +00001215
1216 This function creates a nested instance of the shell and executes the specified
jcarseya405b862010-09-14 05:18:09 +00001217 command (CommandLine) with the specified environment (Environment). Upon return,
1218 the status code returned by the specified command is placed in StatusCode.
1219 If Environment is NULL, then the current environment is used and all changes made
1220 by the commands executed will be reflected in the current environment. If the
1221 Environment is non-NULL, then the changes made will be discarded.
1222 The CommandLine is executed from the current working directory on the current
1223 device.
jcarsey94b17fa2009-05-07 18:46:18 +00001224
Brendan Jackman3877d0f2014-01-24 22:31:07 +00001225 The EnvironmentVariables pararemeter is ignored in a pre-UEFI Shell 2.0
jcarseya405b862010-09-14 05:18:09 +00001226 environment. The values pointed to by the parameters will be unchanged by the
1227 ShellExecute() function. The Output parameter has no effect in a
1228 UEFI Shell 2.0 environment.
jcarsey94b17fa2009-05-07 18:46:18 +00001229
jcarseya405b862010-09-14 05:18:09 +00001230 @param[in] ParentHandle The parent image starting the operation.
1231 @param[in] CommandLine The pointer to a NULL terminated command line.
1232 @param[in] Output True to display debug output. False to hide it.
1233 @param[in] EnvironmentVariables Optional pointer to array of environment variables
1234 in the form "x=y". If NULL, the current set is used.
1235 @param[out] Status The status of the run command line.
jcarsey94b17fa2009-05-07 18:46:18 +00001236
jcarseya405b862010-09-14 05:18:09 +00001237 @retval EFI_SUCCESS The operation completed sucessfully. Status
1238 contains the status code returned.
1239 @retval EFI_INVALID_PARAMETER A parameter contains an invalid value.
1240 @retval EFI_OUT_OF_RESOURCES Out of resources.
1241 @retval EFI_UNSUPPORTED The operation is not allowed.
jcarsey94b17fa2009-05-07 18:46:18 +00001242**/
1243EFI_STATUS
1244EFIAPI
1245ShellExecute (
1246 IN EFI_HANDLE *ParentHandle,
1247 IN CHAR16 *CommandLine OPTIONAL,
1248 IN BOOLEAN Output OPTIONAL,
1249 IN CHAR16 **EnvironmentVariables OPTIONAL,
1250 OUT EFI_STATUS *Status OPTIONAL
1251 )
1252{
Brendan Jackman3877d0f2014-01-24 22:31:07 +00001253 EFI_STATUS CmdStatus;
jcarsey1e6e84c2010-01-25 20:05:08 +00001254 //
jcarsey94b17fa2009-05-07 18:46:18 +00001255 // Check for UEFI Shell 2.0 protocols
1256 //
jcarsey366f81a2011-06-27 21:04:22 +00001257 if (gEfiShellProtocol != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00001258 //
1259 // Call UEFI Shell 2.0 version (not using Output parameter)
1260 //
jcarsey366f81a2011-06-27 21:04:22 +00001261 return (gEfiShellProtocol->Execute(ParentHandle,
jcarsey94b17fa2009-05-07 18:46:18 +00001262 CommandLine,
1263 EnvironmentVariables,
1264 Status));
jcarsey1e6e84c2010-01-25 20:05:08 +00001265 }
jcarsey92a54472011-06-27 20:33:13 +00001266
jcarsey94b17fa2009-05-07 18:46:18 +00001267 //
jcarsey92a54472011-06-27 20:33:13 +00001268 // Check for EFI shell
jcarsey94b17fa2009-05-07 18:46:18 +00001269 //
jcarsey92a54472011-06-27 20:33:13 +00001270 if (mEfiShellEnvironment2 != NULL) {
1271 //
Brendan Jackman3877d0f2014-01-24 22:31:07 +00001272 // Call EFI Shell version.
jcarsey92a54472011-06-27 20:33:13 +00001273 // Due to oddity in the EFI shell we want to dereference the ParentHandle here
1274 //
Brendan Jackman3877d0f2014-01-24 22:31:07 +00001275 CmdStatus = (mEfiShellEnvironment2->Execute(*ParentHandle,
jcarsey92a54472011-06-27 20:33:13 +00001276 CommandLine,
1277 Output));
Brendan Jackman3877d0f2014-01-24 22:31:07 +00001278 //
1279 // No Status output parameter so just use the returned status
1280 //
1281 if (Status != NULL) {
1282 *Status = CmdStatus;
1283 }
1284 //
1285 // If there was an error, we can't tell if it was from the command or from
1286 // the Execute() function, so we'll just assume the shell ran successfully
1287 // and the error came from the command.
1288 //
1289 return EFI_SUCCESS;
jcarsey92a54472011-06-27 20:33:13 +00001290 }
1291
1292 return (EFI_UNSUPPORTED);
jcarsey94b17fa2009-05-07 18:46:18 +00001293}
Brendan Jackman3877d0f2014-01-24 22:31:07 +00001294
jcarsey94b17fa2009-05-07 18:46:18 +00001295/**
1296 Retreives the current directory path
1297
jcarsey1e6e84c2010-01-25 20:05:08 +00001298 If the DeviceName is NULL, it returns the current device's current directory
1299 name. If the DeviceName is not NULL, it returns the current directory name
jcarsey94b17fa2009-05-07 18:46:18 +00001300 on specified drive.
1301
Qiu Shuminfbd2dfa2015-10-23 02:03:20 +00001302 Note that the current directory string should exclude the tailing backslash character.
1303
jcarsey94b17fa2009-05-07 18:46:18 +00001304 @param DeviceName the name of the drive to get directory on
1305
1306 @retval NULL the directory does not exist
1307 @return != NULL the directory
1308**/
1309CONST CHAR16*
1310EFIAPI
1311ShellGetCurrentDir (
jcarseya405b862010-09-14 05:18:09 +00001312 IN CHAR16 * CONST DeviceName OPTIONAL
jcarsey94b17fa2009-05-07 18:46:18 +00001313 )
1314{
jcarsey1e6e84c2010-01-25 20:05:08 +00001315 //
jcarsey94b17fa2009-05-07 18:46:18 +00001316 // Check for UEFI Shell 2.0 protocols
1317 //
jcarsey366f81a2011-06-27 21:04:22 +00001318 if (gEfiShellProtocol != NULL) {
1319 return (gEfiShellProtocol->GetCurDir(DeviceName));
jcarsey1e6e84c2010-01-25 20:05:08 +00001320 }
jcarsey92a54472011-06-27 20:33:13 +00001321
jcarsey94b17fa2009-05-07 18:46:18 +00001322 //
jcarsey8bd282b2011-06-27 16:45:41 +00001323 // Check for EFI shell
jcarsey94b17fa2009-05-07 18:46:18 +00001324 //
jcarsey8bd282b2011-06-27 16:45:41 +00001325 if (mEfiShellEnvironment2 != NULL) {
1326 return (mEfiShellEnvironment2->CurDir(DeviceName));
1327 }
1328
1329 return (NULL);
jcarsey94b17fa2009-05-07 18:46:18 +00001330}
1331/**
1332 sets (enabled or disabled) the page break mode
1333
jcarsey1e6e84c2010-01-25 20:05:08 +00001334 when page break mode is enabled the screen will stop scrolling
jcarsey94b17fa2009-05-07 18:46:18 +00001335 and wait for operator input before scrolling a subsequent screen.
1336
1337 @param CurrentState TRUE to enable and FALSE to disable
1338**/
jcarsey1e6e84c2010-01-25 20:05:08 +00001339VOID
jcarsey94b17fa2009-05-07 18:46:18 +00001340EFIAPI
1341ShellSetPageBreakMode (
1342 IN BOOLEAN CurrentState
1343 )
1344{
1345 //
1346 // check for enabling
1347 //
1348 if (CurrentState != 0x00) {
jcarsey1e6e84c2010-01-25 20:05:08 +00001349 //
jcarsey94b17fa2009-05-07 18:46:18 +00001350 // check for UEFI Shell 2.0
1351 //
jcarsey366f81a2011-06-27 21:04:22 +00001352 if (gEfiShellProtocol != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00001353 //
1354 // Enable with UEFI 2.0 Shell
1355 //
jcarsey366f81a2011-06-27 21:04:22 +00001356 gEfiShellProtocol->EnablePageBreak();
jcarsey94b17fa2009-05-07 18:46:18 +00001357 return;
1358 } else {
jcarsey1e6e84c2010-01-25 20:05:08 +00001359 //
jcarsey92a54472011-06-27 20:33:13 +00001360 // Check for EFI shell
jcarsey94b17fa2009-05-07 18:46:18 +00001361 //
jcarsey92a54472011-06-27 20:33:13 +00001362 if (mEfiShellEnvironment2 != NULL) {
1363 //
1364 // Enable with EFI Shell
1365 //
1366 mEfiShellEnvironment2->EnablePageBreak (DEFAULT_INIT_ROW, DEFAULT_AUTO_LF);
1367 return;
1368 }
jcarsey94b17fa2009-05-07 18:46:18 +00001369 }
1370 } else {
jcarsey1e6e84c2010-01-25 20:05:08 +00001371 //
jcarsey94b17fa2009-05-07 18:46:18 +00001372 // check for UEFI Shell 2.0
1373 //
jcarsey366f81a2011-06-27 21:04:22 +00001374 if (gEfiShellProtocol != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00001375 //
1376 // Disable with UEFI 2.0 Shell
1377 //
jcarsey366f81a2011-06-27 21:04:22 +00001378 gEfiShellProtocol->DisablePageBreak();
jcarsey94b17fa2009-05-07 18:46:18 +00001379 return;
1380 } else {
jcarsey1e6e84c2010-01-25 20:05:08 +00001381 //
jcarsey92a54472011-06-27 20:33:13 +00001382 // Check for EFI shell
jcarsey94b17fa2009-05-07 18:46:18 +00001383 //
jcarsey92a54472011-06-27 20:33:13 +00001384 if (mEfiShellEnvironment2 != NULL) {
1385 //
1386 // Disable with EFI Shell
1387 //
1388 mEfiShellEnvironment2->DisablePageBreak ();
1389 return;
1390 }
jcarsey94b17fa2009-05-07 18:46:18 +00001391 }
1392 }
1393}
1394
1395///
1396/// version of EFI_SHELL_FILE_INFO struct, except has no CONST pointers.
1397/// This allows for the struct to be populated.
1398///
1399typedef struct {
jcarseyd2b45642009-05-11 18:02:16 +00001400 LIST_ENTRY Link;
jcarsey94b17fa2009-05-07 18:46:18 +00001401 EFI_STATUS Status;
1402 CHAR16 *FullName;
1403 CHAR16 *FileName;
jcarseya405b862010-09-14 05:18:09 +00001404 SHELL_FILE_HANDLE Handle;
jcarsey94b17fa2009-05-07 18:46:18 +00001405 EFI_FILE_INFO *Info;
1406} EFI_SHELL_FILE_INFO_NO_CONST;
1407
1408/**
1409 Converts a EFI shell list of structures to the coresponding UEFI Shell 2.0 type of list.
1410
1411 if OldStyleFileList is NULL then ASSERT()
1412
jcarsey1e6e84c2010-01-25 20:05:08 +00001413 this function will convert a SHELL_FILE_ARG based list into a callee allocated
jcarsey94b17fa2009-05-07 18:46:18 +00001414 EFI_SHELL_FILE_INFO based list. it is up to the caller to free the memory via
1415 the ShellCloseFileMetaArg function.
1416
ydong104ff7e372011-09-02 08:05:34 +00001417 @param[in] FileList the EFI shell list type
1418 @param[in, out] ListHead the list to add to
jcarsey94b17fa2009-05-07 18:46:18 +00001419
1420 @retval the resultant head of the double linked new format list;
1421**/
1422LIST_ENTRY*
jcarsey94b17fa2009-05-07 18:46:18 +00001423InternalShellConvertFileListType (
jcarsey9b3bf082009-06-23 21:15:07 +00001424 IN LIST_ENTRY *FileList,
1425 IN OUT LIST_ENTRY *ListHead
jcarsey125c2cf2009-11-18 21:36:50 +00001426 )
1427{
jcarsey94b17fa2009-05-07 18:46:18 +00001428 SHELL_FILE_ARG *OldInfo;
jcarsey9b3bf082009-06-23 21:15:07 +00001429 LIST_ENTRY *Link;
jcarsey94b17fa2009-05-07 18:46:18 +00001430 EFI_SHELL_FILE_INFO_NO_CONST *NewInfo;
1431
1432 //
jcarsey9b3bf082009-06-23 21:15:07 +00001433 // ASSERTs
jcarsey94b17fa2009-05-07 18:46:18 +00001434 //
jcarsey9b3bf082009-06-23 21:15:07 +00001435 ASSERT(FileList != NULL);
1436 ASSERT(ListHead != NULL);
jcarsey94b17fa2009-05-07 18:46:18 +00001437
1438 //
1439 // enumerate through each member of the old list and copy
1440 //
jcarseyd2b45642009-05-11 18:02:16 +00001441 for (Link = FileList->ForwardLink; Link != FileList; Link = Link->ForwardLink) {
jcarsey94b17fa2009-05-07 18:46:18 +00001442 OldInfo = CR (Link, SHELL_FILE_ARG, Link, SHELL_FILE_ARG_SIGNATURE);
jcarseya405b862010-09-14 05:18:09 +00001443 ASSERT(OldInfo != NULL);
1444
1445 //
1446 // Skip ones that failed to open...
1447 //
1448 if (OldInfo->Status != EFI_SUCCESS) {
1449 continue;
1450 }
jcarsey94b17fa2009-05-07 18:46:18 +00001451
1452 //
1453 // make sure the old list was valid
1454 //
jcarsey94b17fa2009-05-07 18:46:18 +00001455 ASSERT(OldInfo->Info != NULL);
1456 ASSERT(OldInfo->FullName != NULL);
1457 ASSERT(OldInfo->FileName != NULL);
1458
1459 //
1460 // allocate a new EFI_SHELL_FILE_INFO object
1461 //
1462 NewInfo = AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));
jcarseyc9d92df2010-02-03 15:37:54 +00001463 if (NewInfo == NULL) {
jcarsey7a95efd2011-10-13 16:08:18 +00001464 ShellCloseFileMetaArg((EFI_SHELL_FILE_INFO**)(&ListHead));
jcarseybeab0fc2011-10-10 17:26:25 +00001465 ListHead = NULL;
jcarseyc9d92df2010-02-03 15:37:54 +00001466 break;
1467 }
jcarsey1e6e84c2010-01-25 20:05:08 +00001468
1469 //
jcarsey94b17fa2009-05-07 18:46:18 +00001470 // copy the simple items
1471 //
1472 NewInfo->Handle = OldInfo->Handle;
1473 NewInfo->Status = OldInfo->Status;
1474
jcarseyd2b45642009-05-11 18:02:16 +00001475 // old shell checks for 0 not NULL
1476 OldInfo->Handle = 0;
1477
jcarsey94b17fa2009-05-07 18:46:18 +00001478 //
1479 // allocate new space to copy strings and structure
1480 //
Jaben Carsey98c16be2014-08-19 21:00:34 +00001481 NewInfo->FullName = AllocateCopyPool(StrSize(OldInfo->FullName), OldInfo->FullName);
1482 NewInfo->FileName = AllocateCopyPool(StrSize(OldInfo->FileName), OldInfo->FileName);
1483 NewInfo->Info = AllocateCopyPool((UINTN)OldInfo->Info->Size, OldInfo->Info);
jcarsey1e6e84c2010-01-25 20:05:08 +00001484
jcarsey94b17fa2009-05-07 18:46:18 +00001485 //
1486 // make sure all the memory allocations were sucessful
1487 //
jcarseybeab0fc2011-10-10 17:26:25 +00001488 if (NULL == NewInfo->FullName || NewInfo->FileName == NULL || NewInfo->Info == NULL) {
Jaben Carsey98c16be2014-08-19 21:00:34 +00001489 //
1490 // Free the partially allocated new node
1491 //
1492 SHELL_FREE_NON_NULL(NewInfo->FullName);
1493 SHELL_FREE_NON_NULL(NewInfo->FileName);
1494 SHELL_FREE_NON_NULL(NewInfo->Info);
1495 SHELL_FREE_NON_NULL(NewInfo);
1496
1497 //
1498 // Free the previously converted stuff
1499 //
jcarsey7a95efd2011-10-13 16:08:18 +00001500 ShellCloseFileMetaArg((EFI_SHELL_FILE_INFO**)(&ListHead));
jcarseybeab0fc2011-10-10 17:26:25 +00001501 ListHead = NULL;
1502 break;
1503 }
jcarsey94b17fa2009-05-07 18:46:18 +00001504
1505 //
jcarsey94b17fa2009-05-07 18:46:18 +00001506 // add that to the list
1507 //
jcarsey9b3bf082009-06-23 21:15:07 +00001508 InsertTailList(ListHead, &NewInfo->Link);
jcarsey94b17fa2009-05-07 18:46:18 +00001509 }
1510 return (ListHead);
1511}
1512/**
1513 Opens a group of files based on a path.
1514
jcarsey1e6e84c2010-01-25 20:05:08 +00001515 This function uses the Arg to open all the matching files. Each matched
Brendan Jackman70879312014-01-24 22:29:53 +00001516 file has a SHELL_FILE_INFO structure to record the file information. These
1517 structures are placed on the list ListHead. Users can get the SHELL_FILE_INFO
jcarsey94b17fa2009-05-07 18:46:18 +00001518 structures from ListHead to access each file. This function supports wildcards
jcarsey1e6e84c2010-01-25 20:05:08 +00001519 and will process '?' and '*' as such. the list must be freed with a call to
jcarsey94b17fa2009-05-07 18:46:18 +00001520 ShellCloseFileMetaArg().
1521
jcarsey1e6e84c2010-01-25 20:05:08 +00001522 If you are NOT appending to an existing list *ListHead must be NULL. If
jcarsey5f7431d2009-07-10 18:06:01 +00001523 *ListHead is NULL then it must be callee freed.
jcarsey94b17fa2009-05-07 18:46:18 +00001524
1525 @param Arg pointer to path string
1526 @param OpenMode mode to open files with
1527 @param ListHead head of linked list of results
1528
jcarsey1e6e84c2010-01-25 20:05:08 +00001529 @retval EFI_SUCCESS the operation was sucessful and the list head
jcarsey94b17fa2009-05-07 18:46:18 +00001530 contains the list of opened files
jcarsey94b17fa2009-05-07 18:46:18 +00001531 @return != EFI_SUCCESS the operation failed
1532
1533 @sa InternalShellConvertFileListType
1534**/
1535EFI_STATUS
1536EFIAPI
1537ShellOpenFileMetaArg (
1538 IN CHAR16 *Arg,
1539 IN UINT64 OpenMode,
1540 IN OUT EFI_SHELL_FILE_INFO **ListHead
1541 )
1542{
1543 EFI_STATUS Status;
jcarsey9b3bf082009-06-23 21:15:07 +00001544 LIST_ENTRY mOldStyleFileList;
Qiu Shumin0960ba12014-09-17 07:58:31 +00001545 CHAR16 *CleanFilePathStr;
jcarsey1e6e84c2010-01-25 20:05:08 +00001546
jcarsey94b17fa2009-05-07 18:46:18 +00001547 //
1548 // ASSERT that Arg and ListHead are not NULL
1549 //
1550 ASSERT(Arg != NULL);
1551 ASSERT(ListHead != NULL);
1552
Qiu Shumin715096c2014-09-19 01:32:05 +00001553 CleanFilePathStr = NULL;
1554
Qiu Shumin0960ba12014-09-17 07:58:31 +00001555 Status = InternalShellStripQuotes (Arg, &CleanFilePathStr);
1556 if (EFI_ERROR (Status)) {
1557 return Status;
1558 }
1559
jcarsey1e6e84c2010-01-25 20:05:08 +00001560 //
jcarsey94b17fa2009-05-07 18:46:18 +00001561 // Check for UEFI Shell 2.0 protocols
1562 //
jcarsey366f81a2011-06-27 21:04:22 +00001563 if (gEfiShellProtocol != NULL) {
jcarsey5f7431d2009-07-10 18:06:01 +00001564 if (*ListHead == NULL) {
1565 *ListHead = (EFI_SHELL_FILE_INFO*)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));
1566 if (*ListHead == NULL) {
Qiu Shumin0960ba12014-09-17 07:58:31 +00001567 FreePool(CleanFilePathStr);
jcarsey5f7431d2009-07-10 18:06:01 +00001568 return (EFI_OUT_OF_RESOURCES);
1569 }
1570 InitializeListHead(&((*ListHead)->Link));
jcarsey1e6e84c2010-01-25 20:05:08 +00001571 }
Qiu Shumin0960ba12014-09-17 07:58:31 +00001572 Status = gEfiShellProtocol->OpenFileList(CleanFilePathStr,
jcarsey1e6e84c2010-01-25 20:05:08 +00001573 OpenMode,
jcarsey2247dde2009-11-09 18:08:58 +00001574 ListHead);
1575 if (EFI_ERROR(Status)) {
jcarsey366f81a2011-06-27 21:04:22 +00001576 gEfiShellProtocol->RemoveDupInFileList(ListHead);
jcarsey2247dde2009-11-09 18:08:58 +00001577 } else {
jcarsey366f81a2011-06-27 21:04:22 +00001578 Status = gEfiShellProtocol->RemoveDupInFileList(ListHead);
jcarsey2247dde2009-11-09 18:08:58 +00001579 }
jcarseya405b862010-09-14 05:18:09 +00001580 if (*ListHead != NULL && IsListEmpty(&(*ListHead)->Link)) {
1581 FreePool(*ListHead);
Qiu Shumin0960ba12014-09-17 07:58:31 +00001582 FreePool(CleanFilePathStr);
jcarseya405b862010-09-14 05:18:09 +00001583 *ListHead = NULL;
1584 return (EFI_NOT_FOUND);
1585 }
Qiu Shumin0960ba12014-09-17 07:58:31 +00001586 FreePool(CleanFilePathStr);
jcarsey2247dde2009-11-09 18:08:58 +00001587 return (Status);
jcarsey1e6e84c2010-01-25 20:05:08 +00001588 }
jcarsey94b17fa2009-05-07 18:46:18 +00001589
1590 //
jcarsey92a54472011-06-27 20:33:13 +00001591 // Check for EFI shell
jcarsey94b17fa2009-05-07 18:46:18 +00001592 //
jcarsey92a54472011-06-27 20:33:13 +00001593 if (mEfiShellEnvironment2 != NULL) {
1594 //
1595 // make sure the list head is initialized
1596 //
1597 InitializeListHead(&mOldStyleFileList);
jcarsey94b17fa2009-05-07 18:46:18 +00001598
jcarsey92a54472011-06-27 20:33:13 +00001599 //
1600 // Get the EFI Shell list of files
1601 //
Qiu Shumin0960ba12014-09-17 07:58:31 +00001602 Status = mEfiShellEnvironment2->FileMetaArg(CleanFilePathStr, &mOldStyleFileList);
jcarsey92a54472011-06-27 20:33:13 +00001603 if (EFI_ERROR(Status)) {
1604 *ListHead = NULL;
Qiu Shumin0960ba12014-09-17 07:58:31 +00001605 FreePool(CleanFilePathStr);
jcarsey92a54472011-06-27 20:33:13 +00001606 return (Status);
1607 }
jcarsey94b17fa2009-05-07 18:46:18 +00001608
jcarsey92a54472011-06-27 20:33:13 +00001609 if (*ListHead == NULL) {
1610 *ListHead = (EFI_SHELL_FILE_INFO *)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));
1611 if (*ListHead == NULL) {
Qiu Shumin0960ba12014-09-17 07:58:31 +00001612 FreePool(CleanFilePathStr);
jcarsey92a54472011-06-27 20:33:13 +00001613 return (EFI_OUT_OF_RESOURCES);
1614 }
1615 InitializeListHead(&((*ListHead)->Link));
1616 }
1617
1618 //
1619 // Convert that to equivalent of UEFI Shell 2.0 structure
1620 //
1621 InternalShellConvertFileListType(&mOldStyleFileList, &(*ListHead)->Link);
1622
1623 //
1624 // Free the EFI Shell version that was converted.
1625 //
1626 mEfiShellEnvironment2->FreeFileList(&mOldStyleFileList);
1627
1628 if ((*ListHead)->Link.ForwardLink == (*ListHead)->Link.BackLink && (*ListHead)->Link.BackLink == &((*ListHead)->Link)) {
1629 FreePool(*ListHead);
1630 *ListHead = NULL;
1631 Status = EFI_NOT_FOUND;
1632 }
Qiu Shumin0960ba12014-09-17 07:58:31 +00001633 FreePool(CleanFilePathStr);
jcarsey94b17fa2009-05-07 18:46:18 +00001634 return (Status);
1635 }
1636
Qiu Shumin0960ba12014-09-17 07:58:31 +00001637 FreePool(CleanFilePathStr);
jcarsey92a54472011-06-27 20:33:13 +00001638 return (EFI_UNSUPPORTED);
jcarsey94b17fa2009-05-07 18:46:18 +00001639}
1640/**
jcarseya405b862010-09-14 05:18:09 +00001641 Free the linked list returned from ShellOpenFileMetaArg.
jcarsey94b17fa2009-05-07 18:46:18 +00001642
jcarseya405b862010-09-14 05:18:09 +00001643 if ListHead is NULL then ASSERT().
jcarsey94b17fa2009-05-07 18:46:18 +00001644
jcarseya405b862010-09-14 05:18:09 +00001645 @param ListHead the pointer to free.
jcarsey94b17fa2009-05-07 18:46:18 +00001646
jcarseya405b862010-09-14 05:18:09 +00001647 @retval EFI_SUCCESS the operation was sucessful.
jcarsey94b17fa2009-05-07 18:46:18 +00001648**/
1649EFI_STATUS
1650EFIAPI
1651ShellCloseFileMetaArg (
1652 IN OUT EFI_SHELL_FILE_INFO **ListHead
1653 )
1654{
1655 LIST_ENTRY *Node;
1656
1657 //
1658 // ASSERT that ListHead is not NULL
1659 //
1660 ASSERT(ListHead != NULL);
1661
jcarsey1e6e84c2010-01-25 20:05:08 +00001662 //
jcarsey94b17fa2009-05-07 18:46:18 +00001663 // Check for UEFI Shell 2.0 protocols
1664 //
jcarsey366f81a2011-06-27 21:04:22 +00001665 if (gEfiShellProtocol != NULL) {
1666 return (gEfiShellProtocol->FreeFileList(ListHead));
jcarsey92a54472011-06-27 20:33:13 +00001667 } else if (mEfiShellEnvironment2 != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00001668 //
jcarsey1e6e84c2010-01-25 20:05:08 +00001669 // Since this is EFI Shell version we need to free our internally made copy
jcarsey94b17fa2009-05-07 18:46:18 +00001670 // of the list
1671 //
jcarsey1e6e84c2010-01-25 20:05:08 +00001672 for ( Node = GetFirstNode(&(*ListHead)->Link)
jcarseya405b862010-09-14 05:18:09 +00001673 ; *ListHead != NULL && !IsListEmpty(&(*ListHead)->Link)
jcarsey9b3bf082009-06-23 21:15:07 +00001674 ; Node = GetFirstNode(&(*ListHead)->Link)) {
jcarsey94b17fa2009-05-07 18:46:18 +00001675 RemoveEntryList(Node);
jcarseya405b862010-09-14 05:18:09 +00001676 ((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 +00001677 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->FullName);
1678 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->FileName);
1679 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->Info);
1680 FreePool((EFI_SHELL_FILE_INFO_NO_CONST*)Node);
1681 }
Jaben Carsey75a5e2e2014-01-10 16:42:45 +00001682 SHELL_FREE_NON_NULL(*ListHead);
jcarsey94b17fa2009-05-07 18:46:18 +00001683 return EFI_SUCCESS;
1684 }
jcarsey92a54472011-06-27 20:33:13 +00001685
1686 return (EFI_UNSUPPORTED);
jcarsey94b17fa2009-05-07 18:46:18 +00001687}
1688
jcarsey125c2cf2009-11-18 21:36:50 +00001689/**
1690 Find a file by searching the CWD and then the path.
1691
jcarseyb3011f42010-01-11 21:49:04 +00001692 If FileName is NULL then ASSERT.
jcarsey125c2cf2009-11-18 21:36:50 +00001693
jcarseyb3011f42010-01-11 21:49:04 +00001694 If the return value is not NULL then the memory must be caller freed.
jcarsey125c2cf2009-11-18 21:36:50 +00001695
1696 @param FileName Filename string.
1697
1698 @retval NULL the file was not found
1699 @return !NULL the full path to the file.
1700**/
1701CHAR16 *
1702EFIAPI
1703ShellFindFilePath (
1704 IN CONST CHAR16 *FileName
1705 )
1706{
1707 CONST CHAR16 *Path;
jcarseya405b862010-09-14 05:18:09 +00001708 SHELL_FILE_HANDLE Handle;
jcarsey125c2cf2009-11-18 21:36:50 +00001709 EFI_STATUS Status;
1710 CHAR16 *RetVal;
1711 CHAR16 *TestPath;
1712 CONST CHAR16 *Walker;
jcarsey36a9d672009-11-20 21:13:41 +00001713 UINTN Size;
jcarsey1cd45e72010-01-29 15:07:44 +00001714 CHAR16 *TempChar;
jcarsey125c2cf2009-11-18 21:36:50 +00001715
1716 RetVal = NULL;
1717
jcarseya405b862010-09-14 05:18:09 +00001718 //
1719 // First make sure its not an absolute path.
1720 //
1721 Status = ShellOpenFileByName(FileName, &Handle, EFI_FILE_MODE_READ, 0);
1722 if (!EFI_ERROR(Status)){
1723 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {
1724 ASSERT(RetVal == NULL);
1725 RetVal = StrnCatGrow(&RetVal, NULL, FileName, 0);
1726 ShellCloseFile(&Handle);
1727 return (RetVal);
1728 } else {
1729 ShellCloseFile(&Handle);
1730 }
1731 }
1732
jcarsey125c2cf2009-11-18 21:36:50 +00001733 Path = ShellGetEnvironmentVariable(L"cwd");
1734 if (Path != NULL) {
Qiu Shuminfbd2dfa2015-10-23 02:03:20 +00001735 Size = StrSize(Path) + sizeof(CHAR16);
jcarsey36a9d672009-11-20 21:13:41 +00001736 Size += StrSize(FileName);
1737 TestPath = AllocateZeroPool(Size);
jcarseyc9d92df2010-02-03 15:37:54 +00001738 if (TestPath == NULL) {
1739 return (NULL);
1740 }
Qiu Shumine75390f2015-06-30 03:18:31 +00001741 StrCpyS(TestPath, Size/sizeof(CHAR16), Path);
Qiu Shuminfbd2dfa2015-10-23 02:03:20 +00001742 StrCatS(TestPath, Size/sizeof(CHAR16), L"\\");
Qiu Shumine75390f2015-06-30 03:18:31 +00001743 StrCatS(TestPath, Size/sizeof(CHAR16), FileName);
jcarsey125c2cf2009-11-18 21:36:50 +00001744 Status = ShellOpenFileByName(TestPath, &Handle, EFI_FILE_MODE_READ, 0);
1745 if (!EFI_ERROR(Status)){
jcarseya405b862010-09-14 05:18:09 +00001746 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {
1747 ASSERT(RetVal == NULL);
1748 RetVal = StrnCatGrow(&RetVal, NULL, TestPath, 0);
1749 ShellCloseFile(&Handle);
1750 FreePool(TestPath);
1751 return (RetVal);
1752 } else {
1753 ShellCloseFile(&Handle);
1754 }
jcarsey125c2cf2009-11-18 21:36:50 +00001755 }
1756 FreePool(TestPath);
1757 }
1758 Path = ShellGetEnvironmentVariable(L"path");
1759 if (Path != NULL) {
jcarseya405b862010-09-14 05:18:09 +00001760 Size = StrSize(Path)+sizeof(CHAR16);
jcarsey36a9d672009-11-20 21:13:41 +00001761 Size += StrSize(FileName);
1762 TestPath = AllocateZeroPool(Size);
jcarsey3e082d52010-10-04 16:44:57 +00001763 if (TestPath == NULL) {
1764 return (NULL);
1765 }
jcarsey1e6e84c2010-01-25 20:05:08 +00001766 Walker = (CHAR16*)Path;
jcarsey125c2cf2009-11-18 21:36:50 +00001767 do {
1768 CopyMem(TestPath, Walker, StrSize(Walker));
jcarsey3e082d52010-10-04 16:44:57 +00001769 if (TestPath != NULL) {
1770 TempChar = StrStr(TestPath, L";");
1771 if (TempChar != NULL) {
1772 *TempChar = CHAR_NULL;
1773 }
1774 if (TestPath[StrLen(TestPath)-1] != L'\\') {
Qiu Shumine75390f2015-06-30 03:18:31 +00001775 StrCatS(TestPath, Size/sizeof(CHAR16), L"\\");
jcarsey3e082d52010-10-04 16:44:57 +00001776 }
jcarsey89e85372011-04-13 23:37:50 +00001777 if (FileName[0] == L'\\') {
1778 FileName++;
1779 }
Qiu Shumine75390f2015-06-30 03:18:31 +00001780 StrCatS(TestPath, Size/sizeof(CHAR16), FileName);
jcarsey3e082d52010-10-04 16:44:57 +00001781 if (StrStr(Walker, L";") != NULL) {
1782 Walker = StrStr(Walker, L";") + 1;
jcarseya405b862010-09-14 05:18:09 +00001783 } else {
jcarsey3e082d52010-10-04 16:44:57 +00001784 Walker = NULL;
1785 }
1786 Status = ShellOpenFileByName(TestPath, &Handle, EFI_FILE_MODE_READ, 0);
1787 if (!EFI_ERROR(Status)){
1788 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {
1789 ASSERT(RetVal == NULL);
1790 RetVal = StrnCatGrow(&RetVal, NULL, TestPath, 0);
1791 ShellCloseFile(&Handle);
1792 break;
1793 } else {
1794 ShellCloseFile(&Handle);
1795 }
jcarseya405b862010-09-14 05:18:09 +00001796 }
jcarsey125c2cf2009-11-18 21:36:50 +00001797 }
1798 } while (Walker != NULL && Walker[0] != CHAR_NULL);
1799 FreePool(TestPath);
1800 }
1801 return (RetVal);
1802}
1803
jcarseyb3011f42010-01-11 21:49:04 +00001804/**
jcarsey1e6e84c2010-01-25 20:05:08 +00001805 Find a file by searching the CWD and then the path with a variable set of file
1806 extensions. If the file is not found it will append each extension in the list
jcarseyb3011f42010-01-11 21:49:04 +00001807 in the order provided and return the first one that is successful.
1808
1809 If FileName is NULL, then ASSERT.
1810 If FileExtension is NULL, then behavior is identical to ShellFindFilePath.
1811
1812 If the return value is not NULL then the memory must be caller freed.
1813
1814 @param[in] FileName Filename string.
1815 @param[in] FileExtension Semi-colon delimeted list of possible extensions.
1816
1817 @retval NULL The file was not found.
1818 @retval !NULL The path to the file.
1819**/
1820CHAR16 *
1821EFIAPI
1822ShellFindFilePathEx (
1823 IN CONST CHAR16 *FileName,
1824 IN CONST CHAR16 *FileExtension
1825 )
1826{
1827 CHAR16 *TestPath;
1828 CHAR16 *RetVal;
1829 CONST CHAR16 *ExtensionWalker;
jcarsey9e926b62010-01-14 20:26:39 +00001830 UINTN Size;
jcarsey1cd45e72010-01-29 15:07:44 +00001831 CHAR16 *TempChar;
jcarseyc9d92df2010-02-03 15:37:54 +00001832 CHAR16 *TempChar2;
jcarsey1cd45e72010-01-29 15:07:44 +00001833
jcarseyb3011f42010-01-11 21:49:04 +00001834 ASSERT(FileName != NULL);
1835 if (FileExtension == NULL) {
1836 return (ShellFindFilePath(FileName));
1837 }
1838 RetVal = ShellFindFilePath(FileName);
1839 if (RetVal != NULL) {
1840 return (RetVal);
1841 }
jcarsey9e926b62010-01-14 20:26:39 +00001842 Size = StrSize(FileName);
1843 Size += StrSize(FileExtension);
1844 TestPath = AllocateZeroPool(Size);
jcarseyc9d92df2010-02-03 15:37:54 +00001845 if (TestPath == NULL) {
1846 return (NULL);
1847 }
jcarseya405b862010-09-14 05:18:09 +00001848 for (ExtensionWalker = FileExtension, TempChar2 = (CHAR16*)FileExtension; TempChar2 != NULL ; ExtensionWalker = TempChar2 + 1){
Qiu Shumine75390f2015-06-30 03:18:31 +00001849 StrCpyS(TestPath, Size/sizeof(CHAR16), FileName);
jcarseya405b862010-09-14 05:18:09 +00001850 if (ExtensionWalker != NULL) {
Qiu Shumine75390f2015-06-30 03:18:31 +00001851 StrCatS(TestPath, Size/sizeof(CHAR16), ExtensionWalker);
jcarseya405b862010-09-14 05:18:09 +00001852 }
jcarsey1cd45e72010-01-29 15:07:44 +00001853 TempChar = StrStr(TestPath, L";");
1854 if (TempChar != NULL) {
1855 *TempChar = CHAR_NULL;
jcarseyb3011f42010-01-11 21:49:04 +00001856 }
1857 RetVal = ShellFindFilePath(TestPath);
1858 if (RetVal != NULL) {
1859 break;
1860 }
jcarseya405b862010-09-14 05:18:09 +00001861 ASSERT(ExtensionWalker != NULL);
jcarseyc9d92df2010-02-03 15:37:54 +00001862 TempChar2 = StrStr(ExtensionWalker, L";");
jcarseyb3011f42010-01-11 21:49:04 +00001863 }
1864 FreePool(TestPath);
1865 return (RetVal);
1866}
1867
jcarsey94b17fa2009-05-07 18:46:18 +00001868typedef struct {
jcarsey9b3bf082009-06-23 21:15:07 +00001869 LIST_ENTRY Link;
jcarsey94b17fa2009-05-07 18:46:18 +00001870 CHAR16 *Name;
jcarseya405b862010-09-14 05:18:09 +00001871 SHELL_PARAM_TYPE Type;
jcarsey94b17fa2009-05-07 18:46:18 +00001872 CHAR16 *Value;
1873 UINTN OriginalPosition;
1874} SHELL_PARAM_PACKAGE;
1875
1876/**
jcarsey1e6e84c2010-01-25 20:05:08 +00001877 Checks the list of valid arguments and returns TRUE if the item was found. If the
jcarsey94b17fa2009-05-07 18:46:18 +00001878 return value is TRUE then the type parameter is set also.
jcarsey1e6e84c2010-01-25 20:05:08 +00001879
jcarsey94b17fa2009-05-07 18:46:18 +00001880 if CheckList is NULL then ASSERT();
1881 if Name is NULL then ASSERT();
1882 if Type is NULL then ASSERT();
1883
jcarsey94b17fa2009-05-07 18:46:18 +00001884 @param Name pointer to Name of parameter found
1885 @param CheckList List to check against
jcarseya405b862010-09-14 05:18:09 +00001886 @param Type pointer to type of parameter if it was found
jcarsey94b17fa2009-05-07 18:46:18 +00001887
1888 @retval TRUE the Parameter was found. Type is valid.
1889 @retval FALSE the Parameter was not found. Type is not valid.
1890**/
1891BOOLEAN
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
jcarseyd2b45642009-05-11 18:02:16 +00001960InternalIsFlag (
jcarsey2247dde2009-11-09 18:08:58 +00001961 IN CONST CHAR16 *Name,
jcarsey658bf432014-11-04 22:33:16 +00001962 IN CONST BOOLEAN AlwaysAllowNumbers,
1963 IN CONST BOOLEAN TimeNumbers
jcarsey94b17fa2009-05-07 18:46:18 +00001964 )
1965{
1966 //
1967 // ASSERT that Name isn't NULL
1968 //
1969 ASSERT(Name != NULL);
1970
1971 //
jcarsey2247dde2009-11-09 18:08:58 +00001972 // If we accept numbers then dont return TRUE. (they will be values)
1973 //
jcarsey658bf432014-11-04 22:33:16 +00001974 if (((Name[0] == L'-' || Name[0] == L'+') && InternalShellIsHexOrDecimalNumber(Name+1, FALSE, FALSE, TimeNumbers)) && AlwaysAllowNumbers) {
jcarsey2247dde2009-11-09 18:08:58 +00001975 return (FALSE);
1976 }
1977
1978 //
jcarseya405b862010-09-14 05:18:09 +00001979 // If the Name has a /, +, or - as the first character return TRUE
jcarsey94b17fa2009-05-07 18:46:18 +00001980 //
jcarsey1e6e84c2010-01-25 20:05:08 +00001981 if ((Name[0] == L'/') ||
jcarseyd2b45642009-05-11 18:02:16 +00001982 (Name[0] == L'-') ||
1983 (Name[0] == L'+')
jcarseya405b862010-09-14 05:18:09 +00001984 ) {
jcarsey94b17fa2009-05-07 18:46:18 +00001985 return (TRUE);
1986 }
1987 return (FALSE);
1988}
1989
1990/**
jcarsey1e6e84c2010-01-25 20:05:08 +00001991 Checks the command line arguments passed against the list of valid ones.
jcarsey94b17fa2009-05-07 18:46:18 +00001992
1993 If no initialization is required, then return RETURN_SUCCESS.
jcarsey1e6e84c2010-01-25 20:05:08 +00001994
jcarseya405b862010-09-14 05:18:09 +00001995 @param[in] CheckList pointer to list of parameters to check
1996 @param[out] CheckPackage pointer to pointer to list checked values
1997 @param[out] ProblemParam optional pointer to pointer to unicode string for
jcarseyd2b45642009-05-11 18:02:16 +00001998 the paramater that caused failure. If used then the
1999 caller is responsible for freeing the memory.
jcarseya405b862010-09-14 05:18:09 +00002000 @param[in] AutoPageBreak will automatically set PageBreakEnabled for "b" parameter
2001 @param[in] Argv pointer to array of parameters
2002 @param[in] Argc Count of parameters in Argv
2003 @param[in] AlwaysAllowNumbers TRUE to allow numbers always, FALSE otherwise.
jcarsey94b17fa2009-05-07 18:46:18 +00002004
2005 @retval EFI_SUCCESS The operation completed sucessfully.
2006 @retval EFI_OUT_OF_RESOURCES A memory allocation failed
2007 @retval EFI_INVALID_PARAMETER A parameter was invalid
jcarsey1e6e84c2010-01-25 20:05:08 +00002008 @retval EFI_VOLUME_CORRUPTED the command line was corrupt. an argument was
2009 duplicated. the duplicated command line argument
jcarsey94b17fa2009-05-07 18:46:18 +00002010 was returned in ProblemParam if provided.
jcarsey1e6e84c2010-01-25 20:05:08 +00002011 @retval EFI_NOT_FOUND a argument required a value that was missing.
jcarsey94b17fa2009-05-07 18:46:18 +00002012 the invalid command line argument was returned in
2013 ProblemParam if provided.
2014**/
2015EFI_STATUS
jcarsey94b17fa2009-05-07 18:46:18 +00002016InternalCommandLineParse (
2017 IN CONST SHELL_PARAM_ITEM *CheckList,
2018 OUT LIST_ENTRY **CheckPackage,
2019 OUT CHAR16 **ProblemParam OPTIONAL,
2020 IN BOOLEAN AutoPageBreak,
2021 IN CONST CHAR16 **Argv,
jcarsey2247dde2009-11-09 18:08:58 +00002022 IN UINTN Argc,
2023 IN BOOLEAN AlwaysAllowNumbers
jcarseya405b862010-09-14 05:18:09 +00002024 )
2025{
jcarsey94b17fa2009-05-07 18:46:18 +00002026 UINTN LoopCounter;
jcarsey252d9452011-03-25 20:49:53 +00002027 SHELL_PARAM_TYPE CurrentItemType;
jcarsey94b17fa2009-05-07 18:46:18 +00002028 SHELL_PARAM_PACKAGE *CurrentItemPackage;
jcarsey125c2cf2009-11-18 21:36:50 +00002029 UINTN GetItemValue;
2030 UINTN ValueSize;
jcarseya405b862010-09-14 05:18:09 +00002031 UINTN Count;
jcarsey252d9452011-03-25 20:49:53 +00002032 CONST CHAR16 *TempPointer;
Jaben Carsey98c16be2014-08-19 21:00:34 +00002033 UINTN CurrentValueSize;
Ruiyu Ni2efafab2016-07-13 17:39:37 +08002034 CHAR16 *NewValue;
jcarsey94b17fa2009-05-07 18:46:18 +00002035
2036 CurrentItemPackage = NULL;
jcarsey125c2cf2009-11-18 21:36:50 +00002037 GetItemValue = 0;
2038 ValueSize = 0;
jcarseya405b862010-09-14 05:18:09 +00002039 Count = 0;
jcarsey94b17fa2009-05-07 18:46:18 +00002040
2041 //
2042 // If there is only 1 item we dont need to do anything
2043 //
jcarseya405b862010-09-14 05:18:09 +00002044 if (Argc < 1) {
jcarsey94b17fa2009-05-07 18:46:18 +00002045 *CheckPackage = NULL;
2046 return (EFI_SUCCESS);
2047 }
2048
2049 //
jcarsey2247dde2009-11-09 18:08:58 +00002050 // ASSERTs
2051 //
2052 ASSERT(CheckList != NULL);
2053 ASSERT(Argv != NULL);
2054
2055 //
jcarsey94b17fa2009-05-07 18:46:18 +00002056 // initialize the linked list
2057 //
2058 *CheckPackage = (LIST_ENTRY*)AllocateZeroPool(sizeof(LIST_ENTRY));
sfu502a758c2011-10-08 02:55:30 +00002059 if (*CheckPackage == NULL) {
jcarseybeab0fc2011-10-10 17:26:25 +00002060 return (EFI_OUT_OF_RESOURCES);
sfu502a758c2011-10-08 02:55:30 +00002061 }
jcarseybeab0fc2011-10-10 17:26:25 +00002062
jcarsey94b17fa2009-05-07 18:46:18 +00002063 InitializeListHead(*CheckPackage);
2064
2065 //
2066 // loop through each of the arguments
2067 //
2068 for (LoopCounter = 0 ; LoopCounter < Argc ; ++LoopCounter) {
2069 if (Argv[LoopCounter] == NULL) {
2070 //
2071 // do nothing for NULL argv
2072 //
jcarseya405b862010-09-14 05:18:09 +00002073 } else if (InternalIsOnCheckList(Argv[LoopCounter], CheckList, &CurrentItemType)) {
jcarsey94b17fa2009-05-07 18:46:18 +00002074 //
jcarsey2247dde2009-11-09 18:08:58 +00002075 // We might have leftover if last parameter didnt have optional value
2076 //
jcarsey125c2cf2009-11-18 21:36:50 +00002077 if (GetItemValue != 0) {
2078 GetItemValue = 0;
jcarsey2247dde2009-11-09 18:08:58 +00002079 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
2080 }
2081 //
jcarsey94b17fa2009-05-07 18:46:18 +00002082 // this is a flag
2083 //
jcarsey252d9452011-03-25 20:49:53 +00002084 CurrentItemPackage = AllocateZeroPool(sizeof(SHELL_PARAM_PACKAGE));
jcarseybeab0fc2011-10-10 17:26:25 +00002085 if (CurrentItemPackage == NULL) {
2086 ShellCommandLineFreeVarList(*CheckPackage);
2087 *CheckPackage = NULL;
2088 return (EFI_OUT_OF_RESOURCES);
2089 }
Jaben Carsey98c16be2014-08-19 21:00:34 +00002090 CurrentItemPackage->Name = AllocateCopyPool(StrSize(Argv[LoopCounter]), Argv[LoopCounter]);
jcarseybeab0fc2011-10-10 17:26:25 +00002091 if (CurrentItemPackage->Name == NULL) {
2092 ShellCommandLineFreeVarList(*CheckPackage);
2093 *CheckPackage = NULL;
2094 return (EFI_OUT_OF_RESOURCES);
2095 }
jcarsey94b17fa2009-05-07 18:46:18 +00002096 CurrentItemPackage->Type = CurrentItemType;
2097 CurrentItemPackage->OriginalPosition = (UINTN)(-1);
jcarseyb1f95a02009-06-16 00:23:19 +00002098 CurrentItemPackage->Value = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +00002099
2100 //
2101 // Does this flag require a value
2102 //
jcarsey125c2cf2009-11-18 21:36:50 +00002103 switch (CurrentItemPackage->Type) {
jcarsey94b17fa2009-05-07 18:46:18 +00002104 //
jcarsey125c2cf2009-11-18 21:36:50 +00002105 // possibly trigger the next loop(s) to populate the value of this item
jcarsey1e6e84c2010-01-25 20:05:08 +00002106 //
jcarsey125c2cf2009-11-18 21:36:50 +00002107 case TypeValue:
jcarsey658bf432014-11-04 22:33:16 +00002108 case TypeTimeValue:
jcarsey1e6e84c2010-01-25 20:05:08 +00002109 GetItemValue = 1;
jcarsey125c2cf2009-11-18 21:36:50 +00002110 ValueSize = 0;
2111 break;
2112 case TypeDoubleValue:
2113 GetItemValue = 2;
2114 ValueSize = 0;
2115 break;
2116 case TypeMaxValue:
2117 GetItemValue = (UINTN)(-1);
2118 ValueSize = 0;
2119 break;
2120 default:
2121 //
2122 // this item has no value expected; we are done
2123 //
2124 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
2125 ASSERT(GetItemValue == 0);
2126 break;
jcarsey94b17fa2009-05-07 18:46:18 +00002127 }
Qiu Shuminaf7a3a52015-03-13 02:04:17 +00002128 } else if (GetItemValue != 0 && CurrentItemPackage != NULL && !InternalIsFlag(Argv[LoopCounter], AlwaysAllowNumbers, (BOOLEAN)(CurrentItemPackage->Type == TypeTimeValue))) {
jcarseyb1f95a02009-06-16 00:23:19 +00002129 //
jcarsey125c2cf2009-11-18 21:36:50 +00002130 // get the item VALUE for a previous flag
jcarseyb1f95a02009-06-16 00:23:19 +00002131 //
Qiu Shumin484dd082015-04-01 00:49:05 +00002132 CurrentValueSize = ValueSize + StrSize(Argv[LoopCounter]) + sizeof(CHAR16);
Ruiyu Ni2efafab2016-07-13 17:39:37 +08002133 NewValue = ReallocatePool(ValueSize, CurrentValueSize, CurrentItemPackage->Value);
2134 if (NewValue == NULL) {
2135 SHELL_FREE_NON_NULL (CurrentItemPackage->Value);
2136 SHELL_FREE_NON_NULL (CurrentItemPackage);
2137 ShellCommandLineFreeVarList (*CheckPackage);
2138 *CheckPackage = NULL;
2139 return EFI_OUT_OF_RESOURCES;
2140 }
2141 CurrentItemPackage->Value = NewValue;
Qiu Shumin484dd082015-04-01 00:49:05 +00002142 if (ValueSize == 0) {
Qiu Shumine75390f2015-06-30 03:18:31 +00002143 StrCpyS( CurrentItemPackage->Value,
2144 CurrentValueSize/sizeof(CHAR16),
2145 Argv[LoopCounter]
2146 );
jcarsey125c2cf2009-11-18 21:36:50 +00002147 } else {
Qiu Shumine75390f2015-06-30 03:18:31 +00002148 StrCatS( CurrentItemPackage->Value,
2149 CurrentValueSize/sizeof(CHAR16),
2150 L" "
2151 );
2152 StrCatS( CurrentItemPackage->Value,
2153 CurrentValueSize/sizeof(CHAR16),
2154 Argv[LoopCounter]
2155 );
jcarsey125c2cf2009-11-18 21:36:50 +00002156 }
Qiu Shumin484dd082015-04-01 00:49:05 +00002157 ValueSize += StrSize(Argv[LoopCounter]) + sizeof(CHAR16);
2158
jcarsey125c2cf2009-11-18 21:36:50 +00002159 GetItemValue--;
2160 if (GetItemValue == 0) {
2161 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
2162 }
jcarsey658bf432014-11-04 22:33:16 +00002163 } else if (!InternalIsFlag(Argv[LoopCounter], AlwaysAllowNumbers, FALSE)){
jcarseyb1f95a02009-06-16 00:23:19 +00002164 //
2165 // add this one as a non-flag
2166 //
jcarsey252d9452011-03-25 20:49:53 +00002167
2168 TempPointer = Argv[LoopCounter];
darylm503b0934ac2011-11-12 00:35:11 +00002169 if ((*TempPointer == L'^' && *(TempPointer+1) == L'-')
jcarsey252d9452011-03-25 20:49:53 +00002170 || (*TempPointer == L'^' && *(TempPointer+1) == L'/')
2171 || (*TempPointer == L'^' && *(TempPointer+1) == L'+')
2172 ){
2173 TempPointer++;
2174 }
2175 CurrentItemPackage = AllocateZeroPool(sizeof(SHELL_PARAM_PACKAGE));
jcarseybeab0fc2011-10-10 17:26:25 +00002176 if (CurrentItemPackage == NULL) {
2177 ShellCommandLineFreeVarList(*CheckPackage);
2178 *CheckPackage = NULL;
2179 return (EFI_OUT_OF_RESOURCES);
2180 }
jcarseyb1f95a02009-06-16 00:23:19 +00002181 CurrentItemPackage->Name = NULL;
2182 CurrentItemPackage->Type = TypePosition;
Jaben Carsey98c16be2014-08-19 21:00:34 +00002183 CurrentItemPackage->Value = AllocateCopyPool(StrSize(TempPointer), TempPointer);
jcarseybeab0fc2011-10-10 17:26:25 +00002184 if (CurrentItemPackage->Value == NULL) {
2185 ShellCommandLineFreeVarList(*CheckPackage);
2186 *CheckPackage = NULL;
2187 return (EFI_OUT_OF_RESOURCES);
2188 }
jcarseya405b862010-09-14 05:18:09 +00002189 CurrentItemPackage->OriginalPosition = Count++;
jcarsey9b3bf082009-06-23 21:15:07 +00002190 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
jcarsey252d9452011-03-25 20:49:53 +00002191 } else {
jcarsey94b17fa2009-05-07 18:46:18 +00002192 //
2193 // this was a non-recognised flag... error!
2194 //
jcarsey252d9452011-03-25 20:49:53 +00002195 if (ProblemParam != NULL) {
Jaben Carsey98c16be2014-08-19 21:00:34 +00002196 *ProblemParam = AllocateCopyPool(StrSize(Argv[LoopCounter]), Argv[LoopCounter]);
jcarsey252d9452011-03-25 20:49:53 +00002197 }
jcarsey94b17fa2009-05-07 18:46:18 +00002198 ShellCommandLineFreeVarList(*CheckPackage);
2199 *CheckPackage = NULL;
2200 return (EFI_VOLUME_CORRUPTED);
jcarsey94b17fa2009-05-07 18:46:18 +00002201 }
2202 }
jcarsey125c2cf2009-11-18 21:36:50 +00002203 if (GetItemValue != 0) {
2204 GetItemValue = 0;
2205 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
2206 }
jcarsey94b17fa2009-05-07 18:46:18 +00002207 //
2208 // support for AutoPageBreak
2209 //
2210 if (AutoPageBreak && ShellCommandLineGetFlag(*CheckPackage, L"-b")) {
2211 ShellSetPageBreakMode(TRUE);
2212 }
2213 return (EFI_SUCCESS);
2214}
2215
2216/**
jcarsey1e6e84c2010-01-25 20:05:08 +00002217 Checks the command line arguments passed against the list of valid ones.
jcarsey94b17fa2009-05-07 18:46:18 +00002218 Optionally removes NULL values first.
jcarsey1e6e84c2010-01-25 20:05:08 +00002219
jcarsey94b17fa2009-05-07 18:46:18 +00002220 If no initialization is required, then return RETURN_SUCCESS.
jcarsey1e6e84c2010-01-25 20:05:08 +00002221
jcarseya405b862010-09-14 05:18:09 +00002222 @param[in] CheckList The pointer to list of parameters to check.
2223 @param[out] CheckPackage The package of checked values.
2224 @param[out] ProblemParam Optional pointer to pointer to unicode string for
jcarsey94b17fa2009-05-07 18:46:18 +00002225 the paramater that caused failure.
jcarseya405b862010-09-14 05:18:09 +00002226 @param[in] AutoPageBreak Will automatically set PageBreakEnabled.
2227 @param[in] AlwaysAllowNumbers Will never fail for number based flags.
jcarsey94b17fa2009-05-07 18:46:18 +00002228
2229 @retval EFI_SUCCESS The operation completed sucessfully.
jcarseya405b862010-09-14 05:18:09 +00002230 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
2231 @retval EFI_INVALID_PARAMETER A parameter was invalid.
2232 @retval EFI_VOLUME_CORRUPTED The command line was corrupt.
2233 @retval EFI_DEVICE_ERROR The commands contained 2 opposing arguments. One
jcarsey1e6e84c2010-01-25 20:05:08 +00002234 of the command line arguments was returned in
jcarsey94b17fa2009-05-07 18:46:18 +00002235 ProblemParam if provided.
jcarseya405b862010-09-14 05:18:09 +00002236 @retval EFI_NOT_FOUND A argument required a value that was missing.
2237 The invalid command line argument was returned in
jcarsey94b17fa2009-05-07 18:46:18 +00002238 ProblemParam if provided.
2239**/
2240EFI_STATUS
2241EFIAPI
jcarsey2247dde2009-11-09 18:08:58 +00002242ShellCommandLineParseEx (
jcarsey94b17fa2009-05-07 18:46:18 +00002243 IN CONST SHELL_PARAM_ITEM *CheckList,
2244 OUT LIST_ENTRY **CheckPackage,
2245 OUT CHAR16 **ProblemParam OPTIONAL,
jcarsey2247dde2009-11-09 18:08:58 +00002246 IN BOOLEAN AutoPageBreak,
2247 IN BOOLEAN AlwaysAllowNumbers
jcarseya405b862010-09-14 05:18:09 +00002248 )
2249{
jcarsey1e6e84c2010-01-25 20:05:08 +00002250 //
jcarsey94b17fa2009-05-07 18:46:18 +00002251 // ASSERT that CheckList and CheckPackage aren't NULL
2252 //
2253 ASSERT(CheckList != NULL);
2254 ASSERT(CheckPackage != NULL);
2255
jcarsey1e6e84c2010-01-25 20:05:08 +00002256 //
jcarsey94b17fa2009-05-07 18:46:18 +00002257 // Check for UEFI Shell 2.0 protocols
2258 //
jcarsey366f81a2011-06-27 21:04:22 +00002259 if (gEfiShellParametersProtocol != NULL) {
jcarsey1e6e84c2010-01-25 20:05:08 +00002260 return (InternalCommandLineParse(CheckList,
2261 CheckPackage,
2262 ProblemParam,
2263 AutoPageBreak,
jcarsey366f81a2011-06-27 21:04:22 +00002264 (CONST CHAR16**) gEfiShellParametersProtocol->Argv,
2265 gEfiShellParametersProtocol->Argc,
jcarsey2247dde2009-11-09 18:08:58 +00002266 AlwaysAllowNumbers));
jcarsey94b17fa2009-05-07 18:46:18 +00002267 }
2268
jcarsey1e6e84c2010-01-25 20:05:08 +00002269 //
jcarsey94b17fa2009-05-07 18:46:18 +00002270 // ASSERT That EFI Shell is not required
2271 //
2272 ASSERT (mEfiShellInterface != NULL);
jcarsey1e6e84c2010-01-25 20:05:08 +00002273 return (InternalCommandLineParse(CheckList,
2274 CheckPackage,
2275 ProblemParam,
2276 AutoPageBreak,
jljusten08d7f8e2009-06-15 18:42:13 +00002277 (CONST CHAR16**) mEfiShellInterface->Argv,
jcarsey2247dde2009-11-09 18:08:58 +00002278 mEfiShellInterface->Argc,
2279 AlwaysAllowNumbers));
jcarsey94b17fa2009-05-07 18:46:18 +00002280}
2281
2282/**
2283 Frees shell variable list that was returned from ShellCommandLineParse.
2284
2285 This function will free all the memory that was used for the CheckPackage
2286 list of postprocessed shell arguments.
2287
2288 this function has no return value.
2289
2290 if CheckPackage is NULL, then return
2291
2292 @param CheckPackage the list to de-allocate
2293 **/
2294VOID
2295EFIAPI
2296ShellCommandLineFreeVarList (
2297 IN LIST_ENTRY *CheckPackage
jcarseya405b862010-09-14 05:18:09 +00002298 )
2299{
jcarsey94b17fa2009-05-07 18:46:18 +00002300 LIST_ENTRY *Node;
2301
2302 //
2303 // check for CheckPackage == NULL
2304 //
2305 if (CheckPackage == NULL) {
2306 return;
2307 }
2308
2309 //
2310 // for each node in the list
2311 //
jcarsey9eb53ac2009-07-08 17:26:58 +00002312 for ( Node = GetFirstNode(CheckPackage)
jcarseya405b862010-09-14 05:18:09 +00002313 ; !IsListEmpty(CheckPackage)
jcarsey9eb53ac2009-07-08 17:26:58 +00002314 ; Node = GetFirstNode(CheckPackage)
jcarseya405b862010-09-14 05:18:09 +00002315 ){
jcarsey94b17fa2009-05-07 18:46:18 +00002316 //
2317 // Remove it from the list
2318 //
2319 RemoveEntryList(Node);
2320
2321 //
2322 // if it has a name free the name
2323 //
2324 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {
2325 FreePool(((SHELL_PARAM_PACKAGE*)Node)->Name);
2326 }
2327
2328 //
2329 // if it has a value free the value
2330 //
2331 if (((SHELL_PARAM_PACKAGE*)Node)->Value != NULL) {
2332 FreePool(((SHELL_PARAM_PACKAGE*)Node)->Value);
2333 }
jcarsey1e6e84c2010-01-25 20:05:08 +00002334
jcarsey94b17fa2009-05-07 18:46:18 +00002335 //
2336 // free the node structure
2337 //
2338 FreePool((SHELL_PARAM_PACKAGE*)Node);
2339 }
2340 //
2341 // free the list head node
2342 //
2343 FreePool(CheckPackage);
2344}
2345/**
2346 Checks for presence of a flag parameter
2347
2348 flag arguments are in the form of "-<Key>" or "/<Key>", but do not have a value following the key
2349
2350 if CheckPackage is NULL then return FALSE.
2351 if KeyString is NULL then ASSERT()
jcarsey1e6e84c2010-01-25 20:05:08 +00002352
jcarsey94b17fa2009-05-07 18:46:18 +00002353 @param CheckPackage The package of parsed command line arguments
2354 @param KeyString the Key of the command line argument to check for
2355
2356 @retval TRUE the flag is on the command line
2357 @retval FALSE the flag is not on the command line
2358 **/
2359BOOLEAN
2360EFIAPI
2361ShellCommandLineGetFlag (
jcarseya405b862010-09-14 05:18:09 +00002362 IN CONST LIST_ENTRY * CONST CheckPackage,
2363 IN CONST CHAR16 * CONST KeyString
2364 )
2365{
jcarsey94b17fa2009-05-07 18:46:18 +00002366 LIST_ENTRY *Node;
jcarsey252d9452011-03-25 20:49:53 +00002367 CHAR16 *TempString;
jcarsey94b17fa2009-05-07 18:46:18 +00002368
2369 //
ydong100c1950b2011-10-13 02:37:35 +00002370 // return FALSE for no package or KeyString is NULL
jcarsey94b17fa2009-05-07 18:46:18 +00002371 //
ydong100c1950b2011-10-13 02:37:35 +00002372 if (CheckPackage == NULL || KeyString == NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00002373 return (FALSE);
2374 }
2375
2376 //
2377 // enumerate through the list of parametrs
2378 //
jcarsey1e6e84c2010-01-25 20:05:08 +00002379 for ( Node = GetFirstNode(CheckPackage)
2380 ; !IsNull (CheckPackage, Node)
2381 ; Node = GetNextNode(CheckPackage, Node)
jcarsey252d9452011-03-25 20:49:53 +00002382 ){
jcarsey94b17fa2009-05-07 18:46:18 +00002383 //
2384 // If the Name matches, return TRUE (and there may be NULL name)
2385 //
2386 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {
jcarsey9eb53ac2009-07-08 17:26:58 +00002387 //
2388 // If Type is TypeStart then only compare the begining of the strings
2389 //
jcarsey252d9452011-03-25 20:49:53 +00002390 if (((SHELL_PARAM_PACKAGE*)Node)->Type == TypeStart) {
2391 if (StrnCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name, StrLen(KeyString)) == 0) {
2392 return (TRUE);
2393 }
2394 TempString = NULL;
2395 TempString = StrnCatGrow(&TempString, NULL, KeyString, StrLen(((SHELL_PARAM_PACKAGE*)Node)->Name));
2396 if (TempString != NULL) {
2397 if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {
2398 FreePool(TempString);
2399 return (TRUE);
2400 }
2401 FreePool(TempString);
2402 }
2403 } else if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {
jcarsey94b17fa2009-05-07 18:46:18 +00002404 return (TRUE);
2405 }
2406 }
2407 }
2408 return (FALSE);
2409}
2410/**
jcarseya405b862010-09-14 05:18:09 +00002411 Returns value from command line argument.
jcarsey94b17fa2009-05-07 18:46:18 +00002412
jcarseya405b862010-09-14 05:18:09 +00002413 Value parameters are in the form of "-<Key> value" or "/<Key> value".
jcarsey1e6e84c2010-01-25 20:05:08 +00002414
jcarseya405b862010-09-14 05:18:09 +00002415 If CheckPackage is NULL, then return NULL.
jcarsey94b17fa2009-05-07 18:46:18 +00002416
jcarseya405b862010-09-14 05:18:09 +00002417 @param[in] CheckPackage The package of parsed command line arguments.
2418 @param[in] KeyString The Key of the command line argument to check for.
jcarsey94b17fa2009-05-07 18:46:18 +00002419
jcarseya405b862010-09-14 05:18:09 +00002420 @retval NULL The flag is not on the command line.
2421 @retval !=NULL The pointer to unicode string of the value.
2422**/
jcarsey94b17fa2009-05-07 18:46:18 +00002423CONST CHAR16*
2424EFIAPI
2425ShellCommandLineGetValue (
2426 IN CONST LIST_ENTRY *CheckPackage,
2427 IN CHAR16 *KeyString
jcarseya405b862010-09-14 05:18:09 +00002428 )
2429{
jcarsey94b17fa2009-05-07 18:46:18 +00002430 LIST_ENTRY *Node;
jcarsey252d9452011-03-25 20:49:53 +00002431 CHAR16 *TempString;
jcarsey94b17fa2009-05-07 18:46:18 +00002432
2433 //
ydong100c1950b2011-10-13 02:37:35 +00002434 // return NULL for no package or KeyString is NULL
jcarsey94b17fa2009-05-07 18:46:18 +00002435 //
ydong100c1950b2011-10-13 02:37:35 +00002436 if (CheckPackage == NULL || KeyString == NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00002437 return (NULL);
2438 }
2439
2440 //
2441 // enumerate through the list of parametrs
2442 //
jcarsey1e6e84c2010-01-25 20:05:08 +00002443 for ( Node = GetFirstNode(CheckPackage)
2444 ; !IsNull (CheckPackage, Node)
2445 ; Node = GetNextNode(CheckPackage, Node)
jcarsey252d9452011-03-25 20:49:53 +00002446 ){
jcarsey94b17fa2009-05-07 18:46:18 +00002447 //
jcarsey252d9452011-03-25 20:49:53 +00002448 // If the Name matches, return TRUE (and there may be NULL name)
jcarsey94b17fa2009-05-07 18:46:18 +00002449 //
2450 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {
jcarsey9eb53ac2009-07-08 17:26:58 +00002451 //
2452 // If Type is TypeStart then only compare the begining of the strings
2453 //
jcarsey252d9452011-03-25 20:49:53 +00002454 if (((SHELL_PARAM_PACKAGE*)Node)->Type == TypeStart) {
2455 if (StrnCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name, StrLen(KeyString)) == 0) {
2456 return (((SHELL_PARAM_PACKAGE*)Node)->Name + StrLen(KeyString));
2457 }
2458 TempString = NULL;
2459 TempString = StrnCatGrow(&TempString, NULL, KeyString, StrLen(((SHELL_PARAM_PACKAGE*)Node)->Name));
2460 if (TempString != NULL) {
2461 if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {
2462 FreePool(TempString);
2463 return (((SHELL_PARAM_PACKAGE*)Node)->Name + StrLen(KeyString));
2464 }
2465 FreePool(TempString);
2466 }
2467 } else if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {
jcarsey94b17fa2009-05-07 18:46:18 +00002468 return (((SHELL_PARAM_PACKAGE*)Node)->Value);
2469 }
2470 }
2471 }
2472 return (NULL);
2473}
jcarseya405b862010-09-14 05:18:09 +00002474
jcarsey94b17fa2009-05-07 18:46:18 +00002475/**
jcarseya405b862010-09-14 05:18:09 +00002476 Returns raw value from command line argument.
jcarsey94b17fa2009-05-07 18:46:18 +00002477
jcarseya405b862010-09-14 05:18:09 +00002478 Raw value parameters are in the form of "value" in a specific position in the list.
jcarsey1e6e84c2010-01-25 20:05:08 +00002479
jcarseya405b862010-09-14 05:18:09 +00002480 If CheckPackage is NULL, then return NULL.
jcarsey94b17fa2009-05-07 18:46:18 +00002481
jcarseya405b862010-09-14 05:18:09 +00002482 @param[in] CheckPackage The package of parsed command line arguments.
2483 @param[in] Position The position of the value.
jcarsey94b17fa2009-05-07 18:46:18 +00002484
jcarseya405b862010-09-14 05:18:09 +00002485 @retval NULL The flag is not on the command line.
2486 @retval !=NULL The pointer to unicode string of the value.
jcarsey94b17fa2009-05-07 18:46:18 +00002487 **/
2488CONST CHAR16*
2489EFIAPI
2490ShellCommandLineGetRawValue (
jcarseya405b862010-09-14 05:18:09 +00002491 IN CONST LIST_ENTRY * CONST CheckPackage,
2492 IN UINTN Position
2493 )
2494{
jcarsey94b17fa2009-05-07 18:46:18 +00002495 LIST_ENTRY *Node;
2496
2497 //
2498 // check for CheckPackage == NULL
2499 //
2500 if (CheckPackage == NULL) {
2501 return (NULL);
2502 }
2503
2504 //
2505 // enumerate through the list of parametrs
2506 //
jcarsey1e6e84c2010-01-25 20:05:08 +00002507 for ( Node = GetFirstNode(CheckPackage)
2508 ; !IsNull (CheckPackage, Node)
2509 ; Node = GetNextNode(CheckPackage, Node)
jcarseya405b862010-09-14 05:18:09 +00002510 ){
jcarsey94b17fa2009-05-07 18:46:18 +00002511 //
2512 // If the position matches, return the value
2513 //
2514 if (((SHELL_PARAM_PACKAGE*)Node)->OriginalPosition == Position) {
2515 return (((SHELL_PARAM_PACKAGE*)Node)->Value);
2516 }
2517 }
2518 return (NULL);
jcarseyb1f95a02009-06-16 00:23:19 +00002519}
jcarsey2247dde2009-11-09 18:08:58 +00002520
2521/**
jcarsey1e6e84c2010-01-25 20:05:08 +00002522 returns the number of command line value parameters that were parsed.
2523
jcarsey2247dde2009-11-09 18:08:58 +00002524 this will not include flags.
2525
jcarseya405b862010-09-14 05:18:09 +00002526 @param[in] CheckPackage The package of parsed command line arguments.
2527
jcarsey2247dde2009-11-09 18:08:58 +00002528 @retval (UINTN)-1 No parsing has ocurred
2529 @return other The number of value parameters found
2530**/
2531UINTN
2532EFIAPI
2533ShellCommandLineGetCount(
jcarseya405b862010-09-14 05:18:09 +00002534 IN CONST LIST_ENTRY *CheckPackage
jcarsey125c2cf2009-11-18 21:36:50 +00002535 )
2536{
jcarseya405b862010-09-14 05:18:09 +00002537 LIST_ENTRY *Node1;
2538 UINTN Count;
2539
2540 if (CheckPackage == NULL) {
2541 return (0);
2542 }
2543 for ( Node1 = GetFirstNode(CheckPackage), Count = 0
2544 ; !IsNull (CheckPackage, Node1)
2545 ; Node1 = GetNextNode(CheckPackage, Node1)
2546 ){
2547 if (((SHELL_PARAM_PACKAGE*)Node1)->Name == NULL) {
2548 Count++;
2549 }
2550 }
2551 return (Count);
jcarsey2247dde2009-11-09 18:08:58 +00002552}
2553
jcarsey975136a2009-06-16 19:03:54 +00002554/**
Bruce Crancceb4eb2015-07-08 01:54:46 +00002555 Determines if a parameter is duplicated.
jcarsey36a9d672009-11-20 21:13:41 +00002556
jcarsey1e6e84c2010-01-25 20:05:08 +00002557 If Param is not NULL then it will point to a callee allocated string buffer
jcarsey36a9d672009-11-20 21:13:41 +00002558 with the parameter value if a duplicate is found.
2559
2560 If CheckPackage is NULL, then ASSERT.
2561
2562 @param[in] CheckPackage The package of parsed command line arguments.
2563 @param[out] Param Upon finding one, a pointer to the duplicated parameter.
2564
2565 @retval EFI_SUCCESS No parameters were duplicated.
2566 @retval EFI_DEVICE_ERROR A duplicate was found.
2567 **/
2568EFI_STATUS
2569EFIAPI
2570ShellCommandLineCheckDuplicate (
2571 IN CONST LIST_ENTRY *CheckPackage,
2572 OUT CHAR16 **Param
2573 )
2574{
2575 LIST_ENTRY *Node1;
2576 LIST_ENTRY *Node2;
jcarsey1e6e84c2010-01-25 20:05:08 +00002577
jcarsey36a9d672009-11-20 21:13:41 +00002578 ASSERT(CheckPackage != NULL);
2579
jcarsey1e6e84c2010-01-25 20:05:08 +00002580 for ( Node1 = GetFirstNode(CheckPackage)
2581 ; !IsNull (CheckPackage, Node1)
2582 ; Node1 = GetNextNode(CheckPackage, Node1)
jcarseya405b862010-09-14 05:18:09 +00002583 ){
jcarsey1e6e84c2010-01-25 20:05:08 +00002584 for ( Node2 = GetNextNode(CheckPackage, Node1)
2585 ; !IsNull (CheckPackage, Node2)
2586 ; Node2 = GetNextNode(CheckPackage, Node2)
jcarseya405b862010-09-14 05:18:09 +00002587 ){
2588 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 +00002589 if (Param != NULL) {
2590 *Param = NULL;
2591 *Param = StrnCatGrow(Param, NULL, ((SHELL_PARAM_PACKAGE*)Node1)->Name, 0);
2592 }
2593 return (EFI_DEVICE_ERROR);
2594 }
2595 }
2596 }
2597 return (EFI_SUCCESS);
2598}
2599
2600/**
jcarsey1e6e84c2010-01-25 20:05:08 +00002601 This is a find and replace function. Upon successful return the NewString is a copy of
jcarsey975136a2009-06-16 19:03:54 +00002602 SourceString with each instance of FindTarget replaced with ReplaceWith.
2603
jcarseyb3011f42010-01-11 21:49:04 +00002604 If SourceString and NewString overlap the behavior is undefined.
2605
jcarsey975136a2009-06-16 19:03:54 +00002606 If the string would grow bigger than NewSize it will halt and return error.
2607
ydong104ff7e372011-09-02 08:05:34 +00002608 @param[in] SourceString The string with source buffer.
2609 @param[in, out] NewString The string with resultant buffer.
2610 @param[in] NewSize The size in bytes of NewString.
2611 @param[in] FindTarget The string to look for.
2612 @param[in] ReplaceWith The string to replace FindTarget with.
2613 @param[in] SkipPreCarrot If TRUE will skip a FindTarget that has a '^'
2614 immediately before it.
2615 @param[in] ParameterReplacing If TRUE will add "" around items with spaces.
jcarsey975136a2009-06-16 19:03:54 +00002616
jcarsey969c7832010-01-13 16:46:33 +00002617 @retval EFI_INVALID_PARAMETER SourceString was NULL.
2618 @retval EFI_INVALID_PARAMETER NewString was NULL.
2619 @retval EFI_INVALID_PARAMETER FindTarget was NULL.
2620 @retval EFI_INVALID_PARAMETER ReplaceWith was NULL.
2621 @retval EFI_INVALID_PARAMETER FindTarget had length < 1.
2622 @retval EFI_INVALID_PARAMETER SourceString had length < 1.
jcarsey1e6e84c2010-01-25 20:05:08 +00002623 @retval EFI_BUFFER_TOO_SMALL NewSize was less than the minimum size to hold
jcarsey969c7832010-01-13 16:46:33 +00002624 the new string (truncation occurred).
jcarseya405b862010-09-14 05:18:09 +00002625 @retval EFI_SUCCESS The string was successfully copied with replacement.
jcarsey975136a2009-06-16 19:03:54 +00002626**/
jcarsey975136a2009-06-16 19:03:54 +00002627EFI_STATUS
2628EFIAPI
jcarseya405b862010-09-14 05:18:09 +00002629ShellCopySearchAndReplace(
jcarsey975136a2009-06-16 19:03:54 +00002630 IN CHAR16 CONST *SourceString,
jcarseya405b862010-09-14 05:18:09 +00002631 IN OUT CHAR16 *NewString,
jcarsey975136a2009-06-16 19:03:54 +00002632 IN UINTN NewSize,
2633 IN CONST CHAR16 *FindTarget,
jcarsey969c7832010-01-13 16:46:33 +00002634 IN CONST CHAR16 *ReplaceWith,
jcarseya405b862010-09-14 05:18:09 +00002635 IN CONST BOOLEAN SkipPreCarrot,
2636 IN CONST BOOLEAN ParameterReplacing
jcarsey1e6e84c2010-01-25 20:05:08 +00002637 )
jcarsey2247dde2009-11-09 18:08:58 +00002638{
jcarsey01582942009-07-10 19:46:17 +00002639 UINTN Size;
jcarseya405b862010-09-14 05:18:09 +00002640 CHAR16 *Replace;
2641
jcarsey975136a2009-06-16 19:03:54 +00002642 if ( (SourceString == NULL)
2643 || (NewString == NULL)
2644 || (FindTarget == NULL)
2645 || (ReplaceWith == NULL)
2646 || (StrLen(FindTarget) < 1)
2647 || (StrLen(SourceString) < 1)
jcarseya405b862010-09-14 05:18:09 +00002648 ){
jcarsey975136a2009-06-16 19:03:54 +00002649 return (EFI_INVALID_PARAMETER);
2650 }
jcarseya405b862010-09-14 05:18:09 +00002651 Replace = NULL;
2652 if (StrStr(ReplaceWith, L" ") == NULL || !ParameterReplacing) {
2653 Replace = StrnCatGrow(&Replace, NULL, ReplaceWith, 0);
2654 } else {
2655 Replace = AllocateZeroPool(StrSize(ReplaceWith) + 2*sizeof(CHAR16));
jcarseybeab0fc2011-10-10 17:26:25 +00002656 if (Replace != NULL) {
2657 UnicodeSPrint(Replace, StrSize(ReplaceWith) + 2*sizeof(CHAR16), L"\"%s\"", ReplaceWith);
2658 }
jcarseya405b862010-09-14 05:18:09 +00002659 }
jcarsey3e082d52010-10-04 16:44:57 +00002660 if (Replace == NULL) {
2661 return (EFI_OUT_OF_RESOURCES);
2662 }
Jaben Carsey98c16be2014-08-19 21:00:34 +00002663 NewString = ZeroMem(NewString, NewSize);
jcarsey2247dde2009-11-09 18:08:58 +00002664 while (*SourceString != CHAR_NULL) {
jcarsey969c7832010-01-13 16:46:33 +00002665 //
jcarseya405b862010-09-14 05:18:09 +00002666 // if we find the FindTarget and either Skip == FALSE or Skip and we
jcarsey969c7832010-01-13 16:46:33 +00002667 // dont have a carrot do a replace...
2668 //
jcarsey1e6e84c2010-01-25 20:05:08 +00002669 if (StrnCmp(SourceString, FindTarget, StrLen(FindTarget)) == 0
jcarseya405b862010-09-14 05:18:09 +00002670 && ((SkipPreCarrot && *(SourceString-1) != L'^') || !SkipPreCarrot)
2671 ){
jcarsey975136a2009-06-16 19:03:54 +00002672 SourceString += StrLen(FindTarget);
jcarsey01582942009-07-10 19:46:17 +00002673 Size = StrSize(NewString);
jcarseya405b862010-09-14 05:18:09 +00002674 if ((Size + (StrLen(Replace)*sizeof(CHAR16))) > NewSize) {
2675 FreePool(Replace);
jcarsey975136a2009-06-16 19:03:54 +00002676 return (EFI_BUFFER_TOO_SMALL);
2677 }
Qiu Shumine75390f2015-06-30 03:18:31 +00002678 StrCatS(NewString, NewSize/sizeof(CHAR16), Replace);
jcarsey975136a2009-06-16 19:03:54 +00002679 } else {
jcarsey01582942009-07-10 19:46:17 +00002680 Size = StrSize(NewString);
2681 if (Size + sizeof(CHAR16) > NewSize) {
jcarseya405b862010-09-14 05:18:09 +00002682 FreePool(Replace);
jcarsey975136a2009-06-16 19:03:54 +00002683 return (EFI_BUFFER_TOO_SMALL);
2684 }
Qiu Shumine75390f2015-06-30 03:18:31 +00002685 StrnCatS(NewString, NewSize/sizeof(CHAR16), SourceString, 1);
jcarsey975136a2009-06-16 19:03:54 +00002686 SourceString++;
2687 }
2688 }
jcarseya405b862010-09-14 05:18:09 +00002689 FreePool(Replace);
jcarsey975136a2009-06-16 19:03:54 +00002690 return (EFI_SUCCESS);
2691}
jcarseyb1f95a02009-06-16 00:23:19 +00002692
2693/**
jcarseye2f82972009-12-01 05:40:24 +00002694 Internal worker function to output a string.
2695
2696 This function will output a string to the correct StdOut.
2697
2698 @param[in] String The string to print out.
2699
2700 @retval EFI_SUCCESS The operation was sucessful.
2701 @retval !EFI_SUCCESS The operation failed.
2702**/
2703EFI_STATUS
jcarseye2f82972009-12-01 05:40:24 +00002704InternalPrintTo (
2705 IN CONST CHAR16 *String
2706 )
2707{
2708 UINTN Size;
2709 Size = StrSize(String) - sizeof(CHAR16);
jcarseya405b862010-09-14 05:18:09 +00002710 if (Size == 0) {
2711 return (EFI_SUCCESS);
2712 }
jcarsey366f81a2011-06-27 21:04:22 +00002713 if (gEfiShellParametersProtocol != NULL) {
2714 return (gEfiShellProtocol->WriteFile(gEfiShellParametersProtocol->StdOut, &Size, (VOID*)String));
jcarseye2f82972009-12-01 05:40:24 +00002715 }
2716 if (mEfiShellInterface != NULL) {
jcarsey06c355b2012-03-26 21:00:39 +00002717 if (mEfiShellInterface->RedirArgc == 0) {
jcarsey49bd4982012-01-27 18:42:43 +00002718 //
2719 // Divide in half for old shell. Must be string length not size.
jcarsey06c355b2012-03-26 21:00:39 +00002720 //
2721 Size /=2; // Divide in half only when no redirection.
2722 }
jcarseya405b862010-09-14 05:18:09 +00002723 return (mEfiShellInterface->StdOut->Write(mEfiShellInterface->StdOut, &Size, (VOID*)String));
jcarseye2f82972009-12-01 05:40:24 +00002724 }
2725 ASSERT(FALSE);
2726 return (EFI_UNSUPPORTED);
2727}
2728
2729/**
jcarseyb1f95a02009-06-16 00:23:19 +00002730 Print at a specific location on the screen.
2731
jcarseyf1b87e72009-06-17 00:52:11 +00002732 This function will move the cursor to a given screen location and print the specified string
jcarsey1e6e84c2010-01-25 20:05:08 +00002733
2734 If -1 is specified for either the Row or Col the current screen location for BOTH
jcarseyf1b87e72009-06-17 00:52:11 +00002735 will be used.
jcarseyb1f95a02009-06-16 00:23:19 +00002736
2737 if either Row or Col is out of range for the current console, then ASSERT
2738 if Format is NULL, then ASSERT
2739
jcarsey1e6e84c2010-01-25 20:05:08 +00002740 In addition to the standard %-based flags as supported by UefiLib Print() this supports
jcarseyb1f95a02009-06-16 00:23:19 +00002741 the following additional flags:
2742 %N - Set output attribute to normal
2743 %H - Set output attribute to highlight
2744 %E - Set output attribute to error
2745 %B - Set output attribute to blue color
2746 %V - Set output attribute to green color
2747
2748 Note: The background color is controlled by the shell command cls.
2749
jcarseyb1f95a02009-06-16 00:23:19 +00002750 @param[in] Col the column to print at
jcarsey252d9452011-03-25 20:49:53 +00002751 @param[in] Row the row to print at
jcarseyb1f95a02009-06-16 00:23:19 +00002752 @param[in] Format the format string
jcarsey2247dde2009-11-09 18:08:58 +00002753 @param[in] Marker the marker for the variable argument list
jcarseyb1f95a02009-06-16 00:23:19 +00002754
jcarseya405b862010-09-14 05:18:09 +00002755 @return EFI_SUCCESS The operation was successful.
2756 @return EFI_DEVICE_ERROR The console device reported an error.
jcarseyb1f95a02009-06-16 00:23:19 +00002757**/
jcarseya405b862010-09-14 05:18:09 +00002758EFI_STATUS
jcarsey2247dde2009-11-09 18:08:58 +00002759InternalShellPrintWorker(
jcarseyb1f95a02009-06-16 00:23:19 +00002760 IN INT32 Col OPTIONAL,
2761 IN INT32 Row OPTIONAL,
2762 IN CONST CHAR16 *Format,
jcarsey252d9452011-03-25 20:49:53 +00002763 IN VA_LIST Marker
jcarsey1e6e84c2010-01-25 20:05:08 +00002764 )
jcarsey2247dde2009-11-09 18:08:58 +00002765{
jcarseyb1f95a02009-06-16 00:23:19 +00002766 EFI_STATUS Status;
jcarsey975136a2009-06-16 19:03:54 +00002767 CHAR16 *ResumeLocation;
2768 CHAR16 *FormatWalker;
jcarseya405b862010-09-14 05:18:09 +00002769 UINTN OriginalAttribute;
jcarsey89e85372011-04-13 23:37:50 +00002770 CHAR16 *mPostReplaceFormat;
2771 CHAR16 *mPostReplaceFormat2;
2772
2773 mPostReplaceFormat = AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize));
2774 mPostReplaceFormat2 = AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize));
jcarseya405b862010-09-14 05:18:09 +00002775
jcarseyf8d3e682011-04-19 17:54:42 +00002776 if (mPostReplaceFormat == NULL || mPostReplaceFormat2 == NULL) {
2777 SHELL_FREE_NON_NULL(mPostReplaceFormat);
2778 SHELL_FREE_NON_NULL(mPostReplaceFormat2);
2779 return (EFI_OUT_OF_RESOURCES);
2780 }
2781
jcarseya405b862010-09-14 05:18:09 +00002782 Status = EFI_SUCCESS;
2783 OriginalAttribute = gST->ConOut->Mode->Attribute;
jcarsey1e6e84c2010-01-25 20:05:08 +00002784
jcarsey975136a2009-06-16 19:03:54 +00002785 //
2786 // Back and forth each time fixing up 1 of our flags...
2787 //
jcarseya405b862010-09-14 05:18:09 +00002788 Status = ShellCopySearchAndReplace(Format, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%N", L"%%N", FALSE, FALSE);
jcarsey975136a2009-06-16 19:03:54 +00002789 ASSERT_EFI_ERROR(Status);
jcarseya405b862010-09-14 05:18:09 +00002790 Status = ShellCopySearchAndReplace(mPostReplaceFormat, mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), L"%E", L"%%E", FALSE, FALSE);
jcarsey975136a2009-06-16 19:03:54 +00002791 ASSERT_EFI_ERROR(Status);
jcarseya405b862010-09-14 05:18:09 +00002792 Status = ShellCopySearchAndReplace(mPostReplaceFormat2, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%H", L"%%H", 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"%B", L"%%B", 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"%V", L"%%V", FALSE, FALSE);
jcarsey975136a2009-06-16 19:03:54 +00002797 ASSERT_EFI_ERROR(Status);
2798
2799 //
2800 // Use the last buffer from replacing to print from...
2801 //
jcarseya405b862010-09-14 05:18:09 +00002802 UnicodeVSPrint (mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), mPostReplaceFormat, Marker);
jcarseyb1f95a02009-06-16 00:23:19 +00002803
2804 if (Col != -1 && Row != -1) {
jcarseyb1f95a02009-06-16 00:23:19 +00002805 Status = gST->ConOut->SetCursorPosition(gST->ConOut, Col, Row);
jcarsey975136a2009-06-16 19:03:54 +00002806 }
2807
jcarseyecd3d592009-12-07 18:05:00 +00002808 FormatWalker = mPostReplaceFormat2;
jcarsey2247dde2009-11-09 18:08:58 +00002809 while (*FormatWalker != CHAR_NULL) {
jcarsey975136a2009-06-16 19:03:54 +00002810 //
2811 // Find the next attribute change request
2812 //
2813 ResumeLocation = StrStr(FormatWalker, L"%");
2814 if (ResumeLocation != NULL) {
jcarsey2247dde2009-11-09 18:08:58 +00002815 *ResumeLocation = CHAR_NULL;
jcarsey975136a2009-06-16 19:03:54 +00002816 }
2817 //
2818 // print the current FormatWalker string
2819 //
jcarseya405b862010-09-14 05:18:09 +00002820 if (StrLen(FormatWalker)>0) {
2821 Status = InternalPrintTo(FormatWalker);
2822 if (EFI_ERROR(Status)) {
2823 break;
2824 }
2825 }
2826
jcarsey975136a2009-06-16 19:03:54 +00002827 //
2828 // update the attribute
2829 //
2830 if (ResumeLocation != NULL) {
Hao Wu3e47c012017-05-23 09:47:26 +08002831 if ((ResumeLocation != mPostReplaceFormat2) && (*(ResumeLocation-1) == L'^')) {
jcarsey5d46f172011-08-23 15:34:23 +00002832 //
jcarsey8bb74412012-01-30 18:44:41 +00002833 // Move cursor back 1 position to overwrite the ^
2834 //
2835 gST->ConOut->SetCursorPosition(gST->ConOut, gST->ConOut->Mode->CursorColumn - 1, gST->ConOut->Mode->CursorRow);
2836
2837 //
jcarsey5d46f172011-08-23 15:34:23 +00002838 // Print a simple '%' symbol
2839 //
2840 Status = InternalPrintTo(L"%");
2841 ResumeLocation = ResumeLocation - 1;
2842 } else {
2843 switch (*(ResumeLocation+1)) {
2844 case (L'N'):
2845 gST->ConOut->SetAttribute(gST->ConOut, OriginalAttribute);
jcarseya405b862010-09-14 05:18:09 +00002846 break;
jcarsey5d46f172011-08-23 15:34:23 +00002847 case (L'E'):
2848 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_YELLOW, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));
2849 break;
2850 case (L'H'):
2851 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_WHITE, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));
2852 break;
2853 case (L'B'):
2854 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_BLUE, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));
2855 break;
2856 case (L'V'):
2857 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_GREEN, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));
2858 break;
2859 default:
2860 //
2861 // Print a simple '%' symbol
2862 //
2863 Status = InternalPrintTo(L"%");
2864 if (EFI_ERROR(Status)) {
2865 break;
2866 }
2867 ResumeLocation = ResumeLocation - 1;
2868 break;
2869 }
jcarsey975136a2009-06-16 19:03:54 +00002870 }
2871 } else {
2872 //
2873 // reset to normal now...
2874 //
jcarsey975136a2009-06-16 19:03:54 +00002875 break;
2876 }
2877
2878 //
2879 // update FormatWalker to Resume + 2 (skip the % and the indicator)
2880 //
2881 FormatWalker = ResumeLocation + 2;
2882 }
jcarseyb1f95a02009-06-16 00:23:19 +00002883
jcarseya405b862010-09-14 05:18:09 +00002884 gST->ConOut->SetAttribute(gST->ConOut, OriginalAttribute);
jcarsey89e85372011-04-13 23:37:50 +00002885
2886 SHELL_FREE_NON_NULL(mPostReplaceFormat);
2887 SHELL_FREE_NON_NULL(mPostReplaceFormat2);
jcarseya405b862010-09-14 05:18:09 +00002888 return (Status);
jcarsey5f7431d2009-07-10 18:06:01 +00002889}
jcarsey2247dde2009-11-09 18:08:58 +00002890
2891/**
2892 Print at a specific location on the screen.
2893
jcarseye2f82972009-12-01 05:40:24 +00002894 This function will move the cursor to a given screen location and print the specified string.
jcarsey1e6e84c2010-01-25 20:05:08 +00002895
2896 If -1 is specified for either the Row or Col the current screen location for BOTH
jcarsey2247dde2009-11-09 18:08:58 +00002897 will be used.
2898
jcarseye2f82972009-12-01 05:40:24 +00002899 If either Row or Col is out of range for the current console, then ASSERT.
2900 If Format is NULL, then ASSERT.
jcarsey2247dde2009-11-09 18:08:58 +00002901
jcarsey1e6e84c2010-01-25 20:05:08 +00002902 In addition to the standard %-based flags as supported by UefiLib Print() this supports
jcarsey2247dde2009-11-09 18:08:58 +00002903 the following additional flags:
2904 %N - Set output attribute to normal
2905 %H - Set output attribute to highlight
2906 %E - Set output attribute to error
2907 %B - Set output attribute to blue color
2908 %V - Set output attribute to green color
2909
2910 Note: The background color is controlled by the shell command cls.
2911
jcarsey2247dde2009-11-09 18:08:58 +00002912 @param[in] Col the column to print at
jcarseya405b862010-09-14 05:18:09 +00002913 @param[in] Row the row to print at
jcarsey2247dde2009-11-09 18:08:58 +00002914 @param[in] Format the format string
jcarseya405b862010-09-14 05:18:09 +00002915 @param[in] ... The variable argument list.
jcarsey2247dde2009-11-09 18:08:58 +00002916
jcarseya405b862010-09-14 05:18:09 +00002917 @return EFI_SUCCESS The printing was successful.
2918 @return EFI_DEVICE_ERROR The console device reported an error.
jcarsey2247dde2009-11-09 18:08:58 +00002919**/
jcarseya405b862010-09-14 05:18:09 +00002920EFI_STATUS
jcarsey2247dde2009-11-09 18:08:58 +00002921EFIAPI
2922ShellPrintEx(
2923 IN INT32 Col OPTIONAL,
2924 IN INT32 Row OPTIONAL,
2925 IN CONST CHAR16 *Format,
2926 ...
jcarsey1e6e84c2010-01-25 20:05:08 +00002927 )
jcarsey2247dde2009-11-09 18:08:58 +00002928{
2929 VA_LIST Marker;
jcarseya405b862010-09-14 05:18:09 +00002930 EFI_STATUS RetVal;
jcarsey3e082d52010-10-04 16:44:57 +00002931 if (Format == NULL) {
2932 return (EFI_INVALID_PARAMETER);
2933 }
jcarsey2247dde2009-11-09 18:08:58 +00002934 VA_START (Marker, Format);
jcarseya405b862010-09-14 05:18:09 +00002935 RetVal = InternalShellPrintWorker(Col, Row, Format, Marker);
jcarseye2f82972009-12-01 05:40:24 +00002936 VA_END(Marker);
jcarseya405b862010-09-14 05:18:09 +00002937 return(RetVal);
jcarsey2247dde2009-11-09 18:08:58 +00002938}
2939
2940/**
2941 Print at a specific location on the screen.
2942
jcarseye2f82972009-12-01 05:40:24 +00002943 This function will move the cursor to a given screen location and print the specified string.
jcarsey1e6e84c2010-01-25 20:05:08 +00002944
2945 If -1 is specified for either the Row or Col the current screen location for BOTH
jcarseye2f82972009-12-01 05:40:24 +00002946 will be used.
jcarsey2247dde2009-11-09 18:08:58 +00002947
jcarseye2f82972009-12-01 05:40:24 +00002948 If either Row or Col is out of range for the current console, then ASSERT.
2949 If Format is NULL, then ASSERT.
jcarsey2247dde2009-11-09 18:08:58 +00002950
jcarsey1e6e84c2010-01-25 20:05:08 +00002951 In addition to the standard %-based flags as supported by UefiLib Print() this supports
jcarsey2247dde2009-11-09 18:08:58 +00002952 the following additional flags:
jcarsey1e6e84c2010-01-25 20:05:08 +00002953 %N - Set output attribute to normal.
2954 %H - Set output attribute to highlight.
2955 %E - Set output attribute to error.
2956 %B - Set output attribute to blue color.
2957 %V - Set output attribute to green color.
jcarsey2247dde2009-11-09 18:08:58 +00002958
2959 Note: The background color is controlled by the shell command cls.
2960
jcarsey1e6e84c2010-01-25 20:05:08 +00002961 @param[in] Col The column to print at.
jcarseya405b862010-09-14 05:18:09 +00002962 @param[in] Row The row to print at.
jcarsey1e6e84c2010-01-25 20:05:08 +00002963 @param[in] Language The language of the string to retrieve. If this parameter
2964 is NULL, then the current platform language is used.
2965 @param[in] HiiFormatStringId The format string Id for getting from Hii.
2966 @param[in] HiiFormatHandle The format string Handle for getting from Hii.
jcarseya405b862010-09-14 05:18:09 +00002967 @param[in] ... The variable argument list.
jcarsey2247dde2009-11-09 18:08:58 +00002968
jcarseya405b862010-09-14 05:18:09 +00002969 @return EFI_SUCCESS The printing was successful.
2970 @return EFI_DEVICE_ERROR The console device reported an error.
jcarsey2247dde2009-11-09 18:08:58 +00002971**/
jcarseya405b862010-09-14 05:18:09 +00002972EFI_STATUS
jcarsey2247dde2009-11-09 18:08:58 +00002973EFIAPI
2974ShellPrintHiiEx(
2975 IN INT32 Col OPTIONAL,
2976 IN INT32 Row OPTIONAL,
jcarsey1e6e84c2010-01-25 20:05:08 +00002977 IN CONST CHAR8 *Language OPTIONAL,
jcarsey2247dde2009-11-09 18:08:58 +00002978 IN CONST EFI_STRING_ID HiiFormatStringId,
2979 IN CONST EFI_HANDLE HiiFormatHandle,
2980 ...
2981 )
2982{
2983 VA_LIST Marker;
2984 CHAR16 *HiiFormatString;
jcarseya405b862010-09-14 05:18:09 +00002985 EFI_STATUS RetVal;
jcarsey2247dde2009-11-09 18:08:58 +00002986
Ruiyu Nieeb97442016-07-14 13:15:34 +08002987 RetVal = EFI_DEVICE_ERROR;
2988
jcarsey2247dde2009-11-09 18:08:58 +00002989 VA_START (Marker, HiiFormatHandle);
jcarsey1e6e84c2010-01-25 20:05:08 +00002990 HiiFormatString = HiiGetString(HiiFormatHandle, HiiFormatStringId, Language);
Ruiyu Nieeb97442016-07-14 13:15:34 +08002991 if (HiiFormatString != NULL) {
2992 RetVal = InternalShellPrintWorker (Col, Row, HiiFormatString, Marker);
2993 SHELL_FREE_NON_NULL (HiiFormatString);
2994 }
jcarseye2f82972009-12-01 05:40:24 +00002995 VA_END(Marker);
jcarsey2247dde2009-11-09 18:08:58 +00002996
2997 return (RetVal);
2998}
2999
3000/**
3001 Function to determine if a given filename represents a file or a directory.
3002
3003 @param[in] DirName Path to directory to test.
3004
jcarseyc8c22592011-10-17 17:49:21 +00003005 @retval EFI_SUCCESS The Path represents a directory
3006 @retval EFI_NOT_FOUND The Path does not represent a directory
3007 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
3008 @return The path failed to open
jcarsey2247dde2009-11-09 18:08:58 +00003009**/
3010EFI_STATUS
3011EFIAPI
3012ShellIsDirectory(
3013 IN CONST CHAR16 *DirName
3014 )
3015{
3016 EFI_STATUS Status;
jcarseya405b862010-09-14 05:18:09 +00003017 SHELL_FILE_HANDLE Handle;
jcarsey3e082d52010-10-04 16:44:57 +00003018 CHAR16 *TempLocation;
3019 CHAR16 *TempLocation2;
jcarsey2247dde2009-11-09 18:08:58 +00003020
jcarseyecd3d592009-12-07 18:05:00 +00003021 ASSERT(DirName != NULL);
3022
jcarseya405b862010-09-14 05:18:09 +00003023 Handle = NULL;
3024 TempLocation = NULL;
jcarsey2247dde2009-11-09 18:08:58 +00003025
3026 Status = ShellOpenFileByName(DirName, &Handle, EFI_FILE_MODE_READ, 0);
3027 if (EFI_ERROR(Status)) {
jcarseya405b862010-09-14 05:18:09 +00003028 //
3029 // try good logic first.
3030 //
jcarsey366f81a2011-06-27 21:04:22 +00003031 if (gEfiShellProtocol != NULL) {
jcarsey3e082d52010-10-04 16:44:57 +00003032 TempLocation = StrnCatGrow(&TempLocation, NULL, DirName, 0);
jcarseyc8c22592011-10-17 17:49:21 +00003033 if (TempLocation == NULL) {
3034 ShellCloseFile(&Handle);
3035 return (EFI_OUT_OF_RESOURCES);
3036 }
jcarsey3e082d52010-10-04 16:44:57 +00003037 TempLocation2 = StrStr(TempLocation, L":");
3038 if (TempLocation2 != NULL && StrLen(StrStr(TempLocation, L":")) == 2) {
3039 *(TempLocation2+1) = CHAR_NULL;
jcarseya405b862010-09-14 05:18:09 +00003040 }
jcarsey366f81a2011-06-27 21:04:22 +00003041 if (gEfiShellProtocol->GetDevicePathFromMap(TempLocation) != NULL) {
jcarseya405b862010-09-14 05:18:09 +00003042 FreePool(TempLocation);
3043 return (EFI_SUCCESS);
3044 }
3045 FreePool(TempLocation);
3046 } else {
3047 //
3048 // probably a map name?!?!!?
3049 //
3050 TempLocation = StrStr(DirName, L"\\");
3051 if (TempLocation != NULL && *(TempLocation+1) == CHAR_NULL) {
3052 return (EFI_SUCCESS);
3053 }
3054 }
jcarsey2247dde2009-11-09 18:08:58 +00003055 return (Status);
3056 }
3057
3058 if (FileHandleIsDirectory(Handle) == EFI_SUCCESS) {
3059 ShellCloseFile(&Handle);
3060 return (EFI_SUCCESS);
3061 }
3062 ShellCloseFile(&Handle);
3063 return (EFI_NOT_FOUND);
3064}
3065
jcarsey125c2cf2009-11-18 21:36:50 +00003066/**
jcarsey36a9d672009-11-20 21:13:41 +00003067 Function to determine if a given filename represents a file.
3068
3069 @param[in] Name Path to file to test.
3070
3071 @retval EFI_SUCCESS The Path represents a file.
3072 @retval EFI_NOT_FOUND The Path does not represent a file.
3073 @retval other The path failed to open.
3074**/
3075EFI_STATUS
3076EFIAPI
3077ShellIsFile(
3078 IN CONST CHAR16 *Name
3079 )
3080{
3081 EFI_STATUS Status;
jcarseya405b862010-09-14 05:18:09 +00003082 SHELL_FILE_HANDLE Handle;
jcarsey36a9d672009-11-20 21:13:41 +00003083
jcarseyecd3d592009-12-07 18:05:00 +00003084 ASSERT(Name != NULL);
3085
jcarsey36a9d672009-11-20 21:13:41 +00003086 Handle = NULL;
3087
3088 Status = ShellOpenFileByName(Name, &Handle, EFI_FILE_MODE_READ, 0);
3089 if (EFI_ERROR(Status)) {
3090 return (Status);
3091 }
3092
3093 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {
3094 ShellCloseFile(&Handle);
3095 return (EFI_SUCCESS);
3096 }
3097 ShellCloseFile(&Handle);
3098 return (EFI_NOT_FOUND);
3099}
3100
3101/**
jcarseyb3011f42010-01-11 21:49:04 +00003102 Function to determine if a given filename represents a file.
3103
3104 This will search the CWD and then the Path.
3105
3106 If Name is NULL, then ASSERT.
3107
3108 @param[in] Name Path to file to test.
3109
3110 @retval EFI_SUCCESS The Path represents a file.
3111 @retval EFI_NOT_FOUND The Path does not represent a file.
3112 @retval other The path failed to open.
3113**/
3114EFI_STATUS
3115EFIAPI
3116ShellIsFileInPath(
3117 IN CONST CHAR16 *Name
jcarseya405b862010-09-14 05:18:09 +00003118 )
3119{
jcarseyb3011f42010-01-11 21:49:04 +00003120 CHAR16 *NewName;
3121 EFI_STATUS Status;
3122
3123 if (!EFI_ERROR(ShellIsFile(Name))) {
jcarseya405b862010-09-14 05:18:09 +00003124 return (EFI_SUCCESS);
jcarseyb3011f42010-01-11 21:49:04 +00003125 }
3126
3127 NewName = ShellFindFilePath(Name);
3128 if (NewName == NULL) {
3129 return (EFI_NOT_FOUND);
3130 }
3131 Status = ShellIsFile(NewName);
3132 FreePool(NewName);
3133 return (Status);
3134}
jcarsey252d9452011-03-25 20:49:53 +00003135
jcarseyb3011f42010-01-11 21:49:04 +00003136/**
Jaben Carsey74b0fb82013-11-22 21:37:34 +00003137 Function return the number converted from a hex representation of a number.
3138
3139 Note: this function cannot be used when (UINTN)(-1), (0xFFFFFFFF) may be a valid
3140 result. Use ShellConvertStringToUint64 instead.
3141
3142 @param[in] String String representation of a number.
3143
3144 @return The unsigned integer result of the conversion.
3145 @retval (UINTN)(-1) An error occured.
3146**/
3147UINTN
3148EFIAPI
3149ShellHexStrToUintn(
3150 IN CONST CHAR16 *String
3151 )
3152{
3153 UINT64 RetVal;
3154
3155 if (!EFI_ERROR(ShellConvertStringToUint64(String, &RetVal, TRUE, TRUE))) {
3156 return ((UINTN)RetVal);
3157 }
3158
3159 return ((UINTN)(-1));
3160}
3161
3162/**
jcarsey1e6e84c2010-01-25 20:05:08 +00003163 Function to determine whether a string is decimal or hex representation of a number
Jaben Carseyd59fc242013-11-14 23:52:43 +00003164 and return the number converted from the string. Spaces are always skipped.
jcarsey125c2cf2009-11-18 21:36:50 +00003165
3166 @param[in] String String representation of a number
3167
jcarsey252d9452011-03-25 20:49:53 +00003168 @return the number
3169 @retval (UINTN)(-1) An error ocurred.
jcarsey125c2cf2009-11-18 21:36:50 +00003170**/
3171UINTN
3172EFIAPI
3173ShellStrToUintn(
3174 IN CONST CHAR16 *String
3175 )
3176{
jcarsey252d9452011-03-25 20:49:53 +00003177 UINT64 RetVal;
3178 BOOLEAN Hex;
3179
3180 Hex = FALSE;
3181
jcarsey658bf432014-11-04 22:33:16 +00003182 if (!InternalShellIsHexOrDecimalNumber(String, Hex, TRUE, FALSE)) {
jcarsey252d9452011-03-25 20:49:53 +00003183 Hex = TRUE;
jcarsey125c2cf2009-11-18 21:36:50 +00003184 }
jcarsey252d9452011-03-25 20:49:53 +00003185
3186 if (!EFI_ERROR(ShellConvertStringToUint64(String, &RetVal, Hex, TRUE))) {
3187 return ((UINTN)RetVal);
3188 }
3189 return ((UINTN)(-1));
jcarsey125c2cf2009-11-18 21:36:50 +00003190}
3191
3192/**
3193 Safely append with automatic string resizing given length of Destination and
3194 desired length of copy from Source.
3195
3196 append the first D characters of Source to the end of Destination, where D is
3197 the lesser of Count and the StrLen() of Source. If appending those D characters
3198 will fit within Destination (whose Size is given as CurrentSize) and
jcarsey1e6e84c2010-01-25 20:05:08 +00003199 still leave room for a NULL terminator, then those characters are appended,
3200 starting at the original terminating NULL of Destination, and a new terminating
3201 NULL is appended.
jcarsey125c2cf2009-11-18 21:36:50 +00003202
3203 If appending D characters onto Destination will result in a overflow of the size
3204 given in CurrentSize the string will be grown such that the copy can be performed
3205 and CurrentSize will be updated to the new size.
3206
3207 If Source is NULL, there is nothing to append, just return the current buffer in
3208 Destination.
3209
3210 if Destination is NULL, then ASSERT()
3211 if Destination's current length (including NULL terminator) is already more then
3212 CurrentSize, then ASSERT()
3213
ydong104ff7e372011-09-02 08:05:34 +00003214 @param[in, out] Destination The String to append onto
3215 @param[in, out] CurrentSize on call the number of bytes in Destination. On
jcarsey125c2cf2009-11-18 21:36:50 +00003216 return possibly the new size (still in bytes). if NULL
3217 then allocate whatever is needed.
3218 @param[in] Source The String to append from
3219 @param[in] Count Maximum number of characters to append. if 0 then
3220 all are appended.
3221
3222 @return Destination return the resultant string.
3223**/
3224CHAR16*
3225EFIAPI
3226StrnCatGrow (
3227 IN OUT CHAR16 **Destination,
3228 IN OUT UINTN *CurrentSize,
3229 IN CONST CHAR16 *Source,
3230 IN UINTN Count
3231 )
3232{
3233 UINTN DestinationStartSize;
3234 UINTN NewSize;
3235
3236 //
3237 // ASSERTs
3238 //
3239 ASSERT(Destination != NULL);
3240
3241 //
3242 // If there's nothing to do then just return Destination
3243 //
3244 if (Source == NULL) {
3245 return (*Destination);
3246 }
3247
3248 //
3249 // allow for un-initialized pointers, based on size being 0
3250 //
3251 if (CurrentSize != NULL && *CurrentSize == 0) {
3252 *Destination = NULL;
3253 }
3254
3255 //
3256 // allow for NULL pointers address as Destination
3257 //
3258 if (*Destination != NULL) {
3259 ASSERT(CurrentSize != 0);
3260 DestinationStartSize = StrSize(*Destination);
3261 ASSERT(DestinationStartSize <= *CurrentSize);
3262 } else {
3263 DestinationStartSize = 0;
3264// ASSERT(*CurrentSize == 0);
3265 }
3266
3267 //
3268 // Append all of Source?
3269 //
3270 if (Count == 0) {
3271 Count = StrLen(Source);
3272 }
3273
3274 //
3275 // Test and grow if required
3276 //
3277 if (CurrentSize != NULL) {
3278 NewSize = *CurrentSize;
ydong10f480fdc2012-09-07 01:55:33 +00003279 if (NewSize < DestinationStartSize + (Count * sizeof(CHAR16))) {
3280 while (NewSize < (DestinationStartSize + (Count*sizeof(CHAR16)))) {
3281 NewSize += 2 * Count * sizeof(CHAR16);
3282 }
3283 *Destination = ReallocatePool(*CurrentSize, NewSize, *Destination);
3284 *CurrentSize = NewSize;
jcarsey125c2cf2009-11-18 21:36:50 +00003285 }
jcarsey125c2cf2009-11-18 21:36:50 +00003286 } else {
Qiu Shuminc587fd32015-07-02 01:12:03 +00003287 NewSize = (Count+1)*sizeof(CHAR16);
3288 *Destination = AllocateZeroPool(NewSize);
jcarsey125c2cf2009-11-18 21:36:50 +00003289 }
3290
3291 //
3292 // Now use standard StrnCat on a big enough buffer
3293 //
jcarseyc9d92df2010-02-03 15:37:54 +00003294 if (*Destination == NULL) {
3295 return (NULL);
3296 }
Qiu Shumine75390f2015-06-30 03:18:31 +00003297
Qiu Shuminc587fd32015-07-02 01:12:03 +00003298 StrnCatS(*Destination, NewSize/sizeof(CHAR16), Source, Count);
Qiu Shumine75390f2015-06-30 03:18:31 +00003299 return *Destination;
jcarsey125c2cf2009-11-18 21:36:50 +00003300}
jcarseyc9d92df2010-02-03 15:37:54 +00003301
3302/**
3303 Prompt the user and return the resultant answer to the requestor.
3304
3305 This function will display the requested question on the shell prompt and then
Mudusuru, Giri P1d322462016-07-07 00:47:45 -07003306 wait for an appropriate answer to be input from the console.
jcarseyc9d92df2010-02-03 15:37:54 +00003307
jcarseya405b862010-09-14 05:18:09 +00003308 if the SHELL_PROMPT_REQUEST_TYPE is SHELL_PROMPT_REQUEST_TYPE_YESNO, ShellPromptResponseTypeQuitContinue
jcarseyc9d92df2010-02-03 15:37:54 +00003309 or SHELL_PROMPT_REQUEST_TYPE_YESNOCANCEL then *Response is of type SHELL_PROMPT_RESPONSE.
3310
jcarseya405b862010-09-14 05:18:09 +00003311 if the SHELL_PROMPT_REQUEST_TYPE is ShellPromptResponseTypeFreeform then *Response is of type
jcarseyc9d92df2010-02-03 15:37:54 +00003312 CHAR16*.
3313
3314 In either case *Response must be callee freed if Response was not NULL;
3315
3316 @param Type What type of question is asked. This is used to filter the input
3317 to prevent invalid answers to question.
3318 @param Prompt Pointer to string prompt to use to request input.
3319 @param Response Pointer to Response which will be populated upon return.
3320
3321 @retval EFI_SUCCESS The operation was sucessful.
3322 @retval EFI_UNSUPPORTED The operation is not supported as requested.
3323 @retval EFI_INVALID_PARAMETER A parameter was invalid.
3324 @return other The operation failed.
3325**/
3326EFI_STATUS
3327EFIAPI
3328ShellPromptForResponse (
3329 IN SHELL_PROMPT_REQUEST_TYPE Type,
3330 IN CHAR16 *Prompt OPTIONAL,
3331 IN OUT VOID **Response OPTIONAL
3332 )
3333{
3334 EFI_STATUS Status;
3335 EFI_INPUT_KEY Key;
3336 UINTN EventIndex;
3337 SHELL_PROMPT_RESPONSE *Resp;
jcarseya405b862010-09-14 05:18:09 +00003338 UINTN Size;
3339 CHAR16 *Buffer;
jcarseyc9d92df2010-02-03 15:37:54 +00003340
jcarseya405b862010-09-14 05:18:09 +00003341 Status = EFI_UNSUPPORTED;
3342 Resp = NULL;
3343 Buffer = NULL;
3344 Size = 0;
3345 if (Type != ShellPromptResponseTypeFreeform) {
jcarsey252d9452011-03-25 20:49:53 +00003346 Resp = (SHELL_PROMPT_RESPONSE*)AllocateZeroPool(sizeof(SHELL_PROMPT_RESPONSE));
jcarsey3e082d52010-10-04 16:44:57 +00003347 if (Resp == NULL) {
3348 return (EFI_OUT_OF_RESOURCES);
3349 }
xdu290bfa222010-07-19 05:21:27 +00003350 }
jcarseyc9d92df2010-02-03 15:37:54 +00003351
3352 switch(Type) {
jcarseya405b862010-09-14 05:18:09 +00003353 case ShellPromptResponseTypeQuitContinue:
jcarseyc9d92df2010-02-03 15:37:54 +00003354 if (Prompt != NULL) {
3355 ShellPrintEx(-1, -1, L"%s", Prompt);
3356 }
3357 //
3358 // wait for valid response
3359 //
3360 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
3361 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
Jaben Carsey31b018a2014-01-16 16:52:39 +00003362 if (EFI_ERROR(Status)) {
3363 break;
3364 }
jcarseyc9d92df2010-02-03 15:37:54 +00003365 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3366 if (Key.UnicodeChar == L'Q' || Key.UnicodeChar ==L'q') {
jcarseya405b862010-09-14 05:18:09 +00003367 *Resp = ShellPromptResponseQuit;
jcarseyc9d92df2010-02-03 15:37:54 +00003368 } else {
jcarseya405b862010-09-14 05:18:09 +00003369 *Resp = ShellPromptResponseContinue;
jcarseyc9d92df2010-02-03 15:37:54 +00003370 }
3371 break;
jcarseya405b862010-09-14 05:18:09 +00003372 case ShellPromptResponseTypeYesNoCancel:
jcarseyc9d92df2010-02-03 15:37:54 +00003373 if (Prompt != NULL) {
3374 ShellPrintEx(-1, -1, L"%s", Prompt);
3375 }
3376 //
3377 // wait for valid response
3378 //
jcarseya405b862010-09-14 05:18:09 +00003379 *Resp = ShellPromptResponseMax;
3380 while (*Resp == ShellPromptResponseMax) {
Jaben Carsey194ae482013-12-09 22:55:13 +00003381 if (ShellGetExecutionBreakFlag()) {
3382 Status = EFI_ABORTED;
3383 break;
3384 }
jcarseyc9d92df2010-02-03 15:37:54 +00003385 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
3386 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
Jaben Carsey31b018a2014-01-16 16:52:39 +00003387 if (EFI_ERROR(Status)) {
3388 break;
3389 }
jcarseyc9d92df2010-02-03 15:37:54 +00003390 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3391 switch (Key.UnicodeChar) {
3392 case L'Y':
3393 case L'y':
jcarseya405b862010-09-14 05:18:09 +00003394 *Resp = ShellPromptResponseYes;
jcarseyc9d92df2010-02-03 15:37:54 +00003395 break;
3396 case L'N':
3397 case L'n':
jcarseya405b862010-09-14 05:18:09 +00003398 *Resp = ShellPromptResponseNo;
jcarseyc9d92df2010-02-03 15:37:54 +00003399 break;
3400 case L'C':
3401 case L'c':
jcarseya405b862010-09-14 05:18:09 +00003402 *Resp = ShellPromptResponseCancel;
3403 break;
3404 }
3405 }
Tapan Shahac55b922016-10-28 12:48:59 -07003406 break;
3407 case ShellPromptResponseTypeYesNoAllCancel:
jcarseya405b862010-09-14 05:18:09 +00003408 if (Prompt != NULL) {
3409 ShellPrintEx(-1, -1, L"%s", Prompt);
3410 }
3411 //
3412 // wait for valid response
3413 //
3414 *Resp = ShellPromptResponseMax;
3415 while (*Resp == ShellPromptResponseMax) {
Jaben Carsey194ae482013-12-09 22:55:13 +00003416 if (ShellGetExecutionBreakFlag()) {
3417 Status = EFI_ABORTED;
3418 break;
3419 }
jcarseya405b862010-09-14 05:18:09 +00003420 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
3421 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
Jaben Carsey31b018a2014-01-16 16:52:39 +00003422 if (EFI_ERROR(Status)) {
3423 break;
3424 }
Tapan Shahac55b922016-10-28 12:48:59 -07003425
3426 if (Key.UnicodeChar <= 127 && Key.UnicodeChar >= 32) {
3427 ShellPrintEx (-1, -1, L"%c", Key.UnicodeChar);
3428 }
3429
jcarseya405b862010-09-14 05:18:09 +00003430 switch (Key.UnicodeChar) {
3431 case L'Y':
3432 case L'y':
3433 *Resp = ShellPromptResponseYes;
3434 break;
3435 case L'N':
3436 case L'n':
3437 *Resp = ShellPromptResponseNo;
3438 break;
3439 case L'A':
3440 case L'a':
3441 *Resp = ShellPromptResponseAll;
3442 break;
3443 case L'C':
3444 case L'c':
3445 *Resp = ShellPromptResponseCancel;
jcarseyc9d92df2010-02-03 15:37:54 +00003446 break;
3447 }
3448 }
3449 break;
jcarseya405b862010-09-14 05:18:09 +00003450 case ShellPromptResponseTypeEnterContinue:
3451 case ShellPromptResponseTypeAnyKeyContinue:
jcarseyc9d92df2010-02-03 15:37:54 +00003452 if (Prompt != NULL) {
3453 ShellPrintEx(-1, -1, L"%s", Prompt);
3454 }
3455 //
3456 // wait for valid response
3457 //
jcarseya405b862010-09-14 05:18:09 +00003458 *Resp = ShellPromptResponseMax;
3459 while (*Resp == ShellPromptResponseMax) {
Jaben Carsey194ae482013-12-09 22:55:13 +00003460 if (ShellGetExecutionBreakFlag()) {
3461 Status = EFI_ABORTED;
3462 break;
3463 }
jcarseyc9d92df2010-02-03 15:37:54 +00003464 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
jcarseya405b862010-09-14 05:18:09 +00003465 if (Type == ShellPromptResponseTypeEnterContinue) {
jcarseyc9d92df2010-02-03 15:37:54 +00003466 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
Jaben Carsey31b018a2014-01-16 16:52:39 +00003467 if (EFI_ERROR(Status)) {
3468 break;
3469 }
jcarseyc9d92df2010-02-03 15:37:54 +00003470 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3471 if (Key.UnicodeChar == CHAR_CARRIAGE_RETURN) {
jcarseya405b862010-09-14 05:18:09 +00003472 *Resp = ShellPromptResponseContinue;
jcarseyc9d92df2010-02-03 15:37:54 +00003473 break;
3474 }
3475 }
jcarseya405b862010-09-14 05:18:09 +00003476 if (Type == ShellPromptResponseTypeAnyKeyContinue) {
3477 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
3478 ASSERT_EFI_ERROR(Status);
3479 *Resp = ShellPromptResponseContinue;
jcarseyc9d92df2010-02-03 15:37:54 +00003480 break;
3481 }
3482 }
3483 break;
jcarseya405b862010-09-14 05:18:09 +00003484 case ShellPromptResponseTypeYesNo:
3485 if (Prompt != NULL) {
3486 ShellPrintEx(-1, -1, L"%s", Prompt);
3487 }
3488 //
3489 // wait for valid response
3490 //
3491 *Resp = ShellPromptResponseMax;
3492 while (*Resp == ShellPromptResponseMax) {
Jaben Carsey194ae482013-12-09 22:55:13 +00003493 if (ShellGetExecutionBreakFlag()) {
3494 Status = EFI_ABORTED;
3495 break;
3496 }
jcarseya405b862010-09-14 05:18:09 +00003497 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
3498 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
Jaben Carsey31b018a2014-01-16 16:52:39 +00003499 if (EFI_ERROR(Status)) {
3500 break;
3501 }
jcarseya405b862010-09-14 05:18:09 +00003502 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3503 switch (Key.UnicodeChar) {
3504 case L'Y':
3505 case L'y':
3506 *Resp = ShellPromptResponseYes;
3507 break;
3508 case L'N':
3509 case L'n':
3510 *Resp = ShellPromptResponseNo;
3511 break;
3512 }
3513 }
3514 break;
3515 case ShellPromptResponseTypeFreeform:
3516 if (Prompt != NULL) {
3517 ShellPrintEx(-1, -1, L"%s", Prompt);
3518 }
3519 while(1) {
Jaben Carsey194ae482013-12-09 22:55:13 +00003520 if (ShellGetExecutionBreakFlag()) {
3521 Status = EFI_ABORTED;
3522 break;
3523 }
jcarseya405b862010-09-14 05:18:09 +00003524 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
3525 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
Jaben Carsey31b018a2014-01-16 16:52:39 +00003526 if (EFI_ERROR(Status)) {
3527 break;
3528 }
jcarseya405b862010-09-14 05:18:09 +00003529 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3530 if (Key.UnicodeChar == CHAR_CARRIAGE_RETURN) {
3531 break;
3532 }
3533 ASSERT((Buffer == NULL && Size == 0) || (Buffer != NULL));
3534 StrnCatGrow(&Buffer, &Size, &Key.UnicodeChar, 1);
3535 }
3536 break;
3537 //
3538 // This is the location to add new prompt types.
Jaben Carsey194ae482013-12-09 22:55:13 +00003539 // If your new type loops remember to add ExecutionBreak support.
jcarseya405b862010-09-14 05:18:09 +00003540 //
jcarseyc9d92df2010-02-03 15:37:54 +00003541 default:
jcarseya405b862010-09-14 05:18:09 +00003542 ASSERT(FALSE);
jcarseyc9d92df2010-02-03 15:37:54 +00003543 }
3544
3545 if (Response != NULL) {
jcarseya405b862010-09-14 05:18:09 +00003546 if (Resp != NULL) {
3547 *Response = Resp;
3548 } else if (Buffer != NULL) {
3549 *Response = Buffer;
3550 }
jcarseyc9d92df2010-02-03 15:37:54 +00003551 } else {
jcarseya405b862010-09-14 05:18:09 +00003552 if (Resp != NULL) {
3553 FreePool(Resp);
3554 }
3555 if (Buffer != NULL) {
3556 FreePool(Buffer);
3557 }
jcarseyc9d92df2010-02-03 15:37:54 +00003558 }
3559
jcarseya405b862010-09-14 05:18:09 +00003560 ShellPrintEx(-1, -1, L"\r\n");
jcarseyc9d92df2010-02-03 15:37:54 +00003561 return (Status);
3562}
3563
3564/**
3565 Prompt the user and return the resultant answer to the requestor.
3566
3567 This function is the same as ShellPromptForResponse, except that the prompt is
3568 automatically pulled from HII.
3569
3570 @param Type What type of question is asked. This is used to filter the input
3571 to prevent invalid answers to question.
jcarseya405b862010-09-14 05:18:09 +00003572 @param[in] HiiFormatStringId The format string Id for getting from Hii.
3573 @param[in] HiiFormatHandle The format string Handle for getting from Hii.
3574 @param Response Pointer to Response which will be populated upon return.
jcarseyc9d92df2010-02-03 15:37:54 +00003575
3576 @retval EFI_SUCCESS the operation was sucessful.
3577 @return other the operation failed.
3578
3579 @sa ShellPromptForResponse
3580**/
3581EFI_STATUS
3582EFIAPI
3583ShellPromptForResponseHii (
3584 IN SHELL_PROMPT_REQUEST_TYPE Type,
3585 IN CONST EFI_STRING_ID HiiFormatStringId,
3586 IN CONST EFI_HANDLE HiiFormatHandle,
3587 IN OUT VOID **Response
3588 )
3589{
3590 CHAR16 *Prompt;
3591 EFI_STATUS Status;
3592
3593 Prompt = HiiGetString(HiiFormatHandle, HiiFormatStringId, NULL);
3594 Status = ShellPromptForResponse(Type, Prompt, Response);
3595 FreePool(Prompt);
3596 return (Status);
3597}
3598
jcarseya405b862010-09-14 05:18:09 +00003599/**
3600 Function to determin if an entire string is a valid number.
jcarseyc9d92df2010-02-03 15:37:54 +00003601
jcarseya405b862010-09-14 05:18:09 +00003602 If Hex it must be preceeded with a 0x or has ForceHex, set TRUE.
3603
3604 @param[in] String The string to evaluate.
3605 @param[in] ForceHex TRUE - always assume hex.
3606 @param[in] StopAtSpace TRUE to halt upon finding a space, FALSE to keep going.
jcarsey658bf432014-11-04 22:33:16 +00003607 @param[in] TimeNumbers TRUE to allow numbers with ":", FALSE otherwise.
jcarseya405b862010-09-14 05:18:09 +00003608
3609 @retval TRUE It is all numeric (dec/hex) characters.
3610 @retval FALSE There is a non-numeric character.
3611**/
3612BOOLEAN
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
jcarsey252d9452011-03-25 20:49:53 +00003725InternalShellCharToUpper (
3726 IN CHAR16 Char
3727 )
3728{
3729 if (Char >= L'a' && Char <= L'z') {
3730 return (CHAR16) (Char - (L'a' - L'A'));
3731 }
3732
3733 return Char;
3734}
3735
3736/**
3737 Convert a Unicode character to numerical value.
3738
3739 This internal function only deal with Unicode character
3740 which maps to a valid hexadecimal ASII character, i.e.
darylm503b0934ac2011-11-12 00:35:11 +00003741 L'0' to L'9', L'a' to L'f' or L'A' to L'F'. For other
jcarsey252d9452011-03-25 20:49:53 +00003742 Unicode character, the value returned does not make sense.
3743
3744 @param Char The character to convert.
3745
3746 @return The numerical value converted.
3747
3748**/
3749UINTN
jcarsey252d9452011-03-25 20:49:53 +00003750InternalShellHexCharToUintn (
3751 IN CHAR16 Char
3752 )
3753{
3754 if (ShellIsDecimalDigitCharacter (Char)) {
3755 return Char - L'0';
3756 }
3757
Hao Wu22454f12017-02-17 11:00:45 +08003758 return (10 + InternalShellCharToUpper (Char) - L'A');
jcarsey252d9452011-03-25 20:49:53 +00003759}
3760
3761/**
3762 Convert a Null-terminated Unicode hexadecimal string to a value of type UINT64.
3763
Jaben Carsey80f3e342015-07-02 17:26:42 +00003764 This function returns a value of type UINT64 by interpreting the contents
jcarsey252d9452011-03-25 20:49:53 +00003765 of the Unicode string specified by String as a hexadecimal number.
3766 The format of the input Unicode string String is:
3767
3768 [spaces][zeros][x][hexadecimal digits].
3769
3770 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
3771 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix.
3772 If "x" appears in the input string, it must be prefixed with at least one 0.
3773 The function will ignore the pad space, which includes spaces or tab characters,
3774 before [zeros], [x] or [hexadecimal digit]. The running zero before [x] or
3775 [hexadecimal digit] will be ignored. Then, the decoding starts after [x] or the
3776 first valid hexadecimal digit. Then, the function stops at the first character that is
3777 a not a valid hexadecimal character or NULL, whichever one comes first.
3778
3779 If String has only pad spaces, then zero is returned.
3780 If String has no leading pad spaces, leading zeros or valid hexadecimal digits,
3781 then zero is returned.
3782
3783 @param[in] String A pointer to a Null-terminated Unicode string.
3784 @param[out] Value Upon a successful return the value of the conversion.
3785 @param[in] StopAtSpace FALSE to skip spaces.
3786
3787 @retval EFI_SUCCESS The conversion was successful.
3788 @retval EFI_INVALID_PARAMETER A parameter was NULL or invalid.
3789 @retval EFI_DEVICE_ERROR An overflow occured.
3790**/
3791EFI_STATUS
jcarsey252d9452011-03-25 20:49:53 +00003792InternalShellStrHexToUint64 (
3793 IN CONST CHAR16 *String,
3794 OUT UINT64 *Value,
3795 IN CONST BOOLEAN StopAtSpace
3796 )
3797{
3798 UINT64 Result;
3799
3800 if (String == NULL || StrSize(String) == 0 || Value == NULL) {
3801 return (EFI_INVALID_PARAMETER);
3802 }
darylm503b0934ac2011-11-12 00:35:11 +00003803
jcarsey252d9452011-03-25 20:49:53 +00003804 //
darylm503b0934ac2011-11-12 00:35:11 +00003805 // Ignore the pad spaces (space or tab)
jcarsey252d9452011-03-25 20:49:53 +00003806 //
3807 while ((*String == L' ') || (*String == L'\t')) {
3808 String++;
3809 }
3810
3811 //
3812 // Ignore leading Zeros after the spaces
3813 //
3814 while (*String == L'0') {
3815 String++;
3816 }
3817
3818 if (InternalShellCharToUpper (*String) == L'X') {
3819 if (*(String - 1) != L'0') {
3820 return 0;
3821 }
3822 //
3823 // Skip the 'X'
3824 //
3825 String++;
3826 }
3827
3828 Result = 0;
darylm503b0934ac2011-11-12 00:35:11 +00003829
jcarsey252d9452011-03-25 20:49:53 +00003830 //
Jaben Carsey80f3e342015-07-02 17:26:42 +00003831 // there is a space where there should't be
jcarsey252d9452011-03-25 20:49:53 +00003832 //
Jaben Carsey80f3e342015-07-02 17:26:42 +00003833 if (*String == L' ') {
3834 return (EFI_INVALID_PARAMETER);
jcarsey252d9452011-03-25 20:49:53 +00003835 }
darylm503b0934ac2011-11-12 00:35:11 +00003836
jcarsey252d9452011-03-25 20:49:53 +00003837 while (ShellIsHexaDecimalDigitCharacter (*String)) {
3838 //
darylm503b0934ac2011-11-12 00:35:11 +00003839 // If the Hex Number represented by String overflows according
Jaben Carsey80f3e342015-07-02 17:26:42 +00003840 // to the range defined by UINT64, then return EFI_DEVICE_ERROR.
jcarsey252d9452011-03-25 20:49:53 +00003841 //
3842 if (!(Result <= (RShiftU64((((UINT64) ~0) - InternalShellHexCharToUintn (*String)), 4)))) {
3843// if (!(Result <= ((((UINT64) ~0) - InternalShellHexCharToUintn (*String)) >> 4))) {
3844 return (EFI_DEVICE_ERROR);
3845 }
3846
3847 Result = (LShiftU64(Result, 4));
3848 Result += InternalShellHexCharToUintn (*String);
3849 String++;
3850
3851 //
jcarsey49bd4982012-01-27 18:42:43 +00003852 // stop at spaces if requested
jcarsey252d9452011-03-25 20:49:53 +00003853 //
jcarsey49bd4982012-01-27 18:42:43 +00003854 if (StopAtSpace && *String == L' ') {
3855 break;
jcarsey252d9452011-03-25 20:49:53 +00003856 }
3857 }
3858
3859 *Value = Result;
3860 return (EFI_SUCCESS);
3861}
3862
3863/**
3864 Convert a Null-terminated Unicode decimal string to a value of
3865 type UINT64.
3866
3867 This function returns a value of type UINT64 by interpreting the contents
3868 of the Unicode string specified by String as a decimal number. The format
3869 of the input Unicode string String is:
3870
3871 [spaces] [decimal digits].
3872
3873 The valid decimal digit character is in the range [0-9]. The
3874 function will ignore the pad space, which includes spaces or
3875 tab characters, before [decimal digits]. The running zero in the
3876 beginning of [decimal digits] will be ignored. Then, the function
3877 stops at the first character that is a not a valid decimal character
3878 or a Null-terminator, whichever one comes first.
3879
3880 If String has only pad spaces, then 0 is returned.
3881 If String has no pad spaces or valid decimal digits,
3882 then 0 is returned.
3883
3884 @param[in] String A pointer to a Null-terminated Unicode string.
3885 @param[out] Value Upon a successful return the value of the conversion.
3886 @param[in] StopAtSpace FALSE to skip spaces.
3887
3888 @retval EFI_SUCCESS The conversion was successful.
3889 @retval EFI_INVALID_PARAMETER A parameter was NULL or invalid.
3890 @retval EFI_DEVICE_ERROR An overflow occured.
3891**/
3892EFI_STATUS
jcarsey252d9452011-03-25 20:49:53 +00003893InternalShellStrDecimalToUint64 (
3894 IN CONST CHAR16 *String,
3895 OUT UINT64 *Value,
3896 IN CONST BOOLEAN StopAtSpace
3897 )
3898{
3899 UINT64 Result;
3900
3901 if (String == NULL || StrSize (String) == 0 || Value == NULL) {
3902 return (EFI_INVALID_PARAMETER);
3903 }
3904
3905 //
3906 // Ignore the pad spaces (space or tab)
3907 //
3908 while ((*String == L' ') || (*String == L'\t')) {
3909 String++;
3910 }
3911
3912 //
3913 // Ignore leading Zeros after the spaces
3914 //
3915 while (*String == L'0') {
3916 String++;
3917 }
3918
3919 Result = 0;
3920
3921 //
Jaben Carsey80f3e342015-07-02 17:26:42 +00003922 // Stop upon space if requested
3923 // (if the whole value was 0)
jcarsey252d9452011-03-25 20:49:53 +00003924 //
Jaben Carsey80f3e342015-07-02 17:26:42 +00003925 if (StopAtSpace && *String == L' ') {
3926 *Value = Result;
3927 return (EFI_SUCCESS);
jcarsey252d9452011-03-25 20:49:53 +00003928 }
Jaben Carsey80f3e342015-07-02 17:26:42 +00003929
jcarsey252d9452011-03-25 20:49:53 +00003930 while (ShellIsDecimalDigitCharacter (*String)) {
3931 //
darylm503b0934ac2011-11-12 00:35:11 +00003932 // If the number represented by String overflows according
Jaben Carsey80f3e342015-07-02 17:26:42 +00003933 // to the range defined by UINT64, then return EFI_DEVICE_ERROR.
jcarsey252d9452011-03-25 20:49:53 +00003934 //
darylm503b0934ac2011-11-12 00:35:11 +00003935
jcarsey252d9452011-03-25 20:49:53 +00003936 if (!(Result <= (DivU64x32((((UINT64) ~0) - (*String - L'0')),10)))) {
3937 return (EFI_DEVICE_ERROR);
3938 }
3939
3940 Result = MultU64x32(Result, 10) + (*String - L'0');
3941 String++;
3942
3943 //
3944 // Stop at spaces if requested
3945 //
3946 if (StopAtSpace && *String == L' ') {
3947 break;
3948 }
3949 }
3950
3951 *Value = Result;
darylm503b0934ac2011-11-12 00:35:11 +00003952
jcarsey252d9452011-03-25 20:49:53 +00003953 return (EFI_SUCCESS);
3954}
3955
3956/**
3957 Function to verify and convert a string to its numerical value.
3958
3959 If Hex it must be preceeded with a 0x, 0X, or has ForceHex set TRUE.
3960
3961 @param[in] String The string to evaluate.
3962 @param[out] Value Upon a successful return the value of the conversion.
3963 @param[in] ForceHex TRUE - always assume hex.
3964 @param[in] StopAtSpace FALSE to skip spaces.
darylm503b0934ac2011-11-12 00:35:11 +00003965
jcarsey252d9452011-03-25 20:49:53 +00003966 @retval EFI_SUCCESS The conversion was successful.
3967 @retval EFI_INVALID_PARAMETER String contained an invalid character.
3968 @retval EFI_NOT_FOUND String was a number, but Value was NULL.
3969**/
3970EFI_STATUS
3971EFIAPI
3972ShellConvertStringToUint64(
3973 IN CONST CHAR16 *String,
3974 OUT UINT64 *Value,
3975 IN CONST BOOLEAN ForceHex,
3976 IN CONST BOOLEAN StopAtSpace
3977 )
3978{
3979 UINT64 RetVal;
3980 CONST CHAR16 *Walker;
3981 EFI_STATUS Status;
3982 BOOLEAN Hex;
3983
3984 Hex = ForceHex;
3985
jcarsey658bf432014-11-04 22:33:16 +00003986 if (!InternalShellIsHexOrDecimalNumber(String, Hex, StopAtSpace, FALSE)) {
jcarsey252d9452011-03-25 20:49:53 +00003987 if (!Hex) {
3988 Hex = TRUE;
jcarsey658bf432014-11-04 22:33:16 +00003989 if (!InternalShellIsHexOrDecimalNumber(String, Hex, StopAtSpace, FALSE)) {
jcarsey252d9452011-03-25 20:49:53 +00003990 return (EFI_INVALID_PARAMETER);
3991 }
3992 } else {
3993 return (EFI_INVALID_PARAMETER);
3994 }
3995 }
3996
3997 //
3998 // Chop off leading spaces
3999 //
4000 for (Walker = String; Walker != NULL && *Walker != CHAR_NULL && *Walker == L' '; Walker++);
4001
4002 //
4003 // make sure we have something left that is numeric.
4004 //
jcarsey658bf432014-11-04 22:33:16 +00004005 if (Walker == NULL || *Walker == CHAR_NULL || !InternalShellIsHexOrDecimalNumber(Walker, Hex, StopAtSpace, FALSE)) {
jcarsey252d9452011-03-25 20:49:53 +00004006 return (EFI_INVALID_PARAMETER);
darylm503b0934ac2011-11-12 00:35:11 +00004007 }
jcarsey252d9452011-03-25 20:49:53 +00004008
4009 //
4010 // do the conversion.
4011 //
4012 if (Hex || StrnCmp(Walker, L"0x", 2) == 0 || StrnCmp(Walker, L"0X", 2) == 0){
4013 Status = InternalShellStrHexToUint64(Walker, &RetVal, StopAtSpace);
4014 } else {
4015 Status = InternalShellStrDecimalToUint64(Walker, &RetVal, StopAtSpace);
4016 }
4017
4018 if (Value == NULL && !EFI_ERROR(Status)) {
4019 return (EFI_NOT_FOUND);
4020 }
4021
4022 if (Value != NULL) {
4023 *Value = RetVal;
4024 }
4025
4026 return (Status);
4027}
4028
4029/**
4030 Function to determin if an entire string is a valid number.
4031
4032 If Hex it must be preceeded with a 0x or has ForceHex, set TRUE.
4033
4034 @param[in] String The string to evaluate.
4035 @param[in] ForceHex TRUE - always assume hex.
4036 @param[in] StopAtSpace TRUE to halt upon finding a space, FALSE to keep going.
4037
4038 @retval TRUE It is all numeric (dec/hex) characters.
4039 @retval FALSE There is a non-numeric character.
4040**/
4041BOOLEAN
4042EFIAPI
4043ShellIsHexOrDecimalNumber (
4044 IN CONST CHAR16 *String,
4045 IN CONST BOOLEAN ForceHex,
4046 IN CONST BOOLEAN StopAtSpace
4047 )
4048{
4049 if (ShellConvertStringToUint64(String, NULL, ForceHex, StopAtSpace) == EFI_NOT_FOUND) {
4050 return (TRUE);
4051 }
4052 return (FALSE);
4053}
jcarsey4d0a4fc2011-07-06 22:28:36 +00004054
4055/**
4056 Function to read a single line from a SHELL_FILE_HANDLE. The \n is not included in the returned
4057 buffer. The returned buffer must be callee freed.
4058
4059 If the position upon start is 0, then the Ascii Boolean will be set. This should be
4060 maintained and not changed for all operations with the same file.
4061
ydong104ff7e372011-09-02 08:05:34 +00004062 @param[in] Handle SHELL_FILE_HANDLE to read from.
4063 @param[in, out] Ascii Boolean value for indicating whether the file is
4064 Ascii (TRUE) or UCS2 (FALSE).
jcarsey4d0a4fc2011-07-06 22:28:36 +00004065
jcarseybeab0fc2011-10-10 17:26:25 +00004066 @return The line of text from the file.
4067 @retval NULL There was not enough memory available.
jcarsey4d0a4fc2011-07-06 22:28:36 +00004068
4069 @sa ShellFileHandleReadLine
4070**/
4071CHAR16*
4072EFIAPI
4073ShellFileHandleReturnLine(
4074 IN SHELL_FILE_HANDLE Handle,
4075 IN OUT BOOLEAN *Ascii
4076 )
4077{
4078 CHAR16 *RetVal;
4079 UINTN Size;
4080 EFI_STATUS Status;
4081
4082 Size = 0;
4083 RetVal = NULL;
4084
4085 Status = ShellFileHandleReadLine(Handle, RetVal, &Size, FALSE, Ascii);
4086 if (Status == EFI_BUFFER_TOO_SMALL) {
4087 RetVal = AllocateZeroPool(Size);
jcarseybeab0fc2011-10-10 17:26:25 +00004088 if (RetVal == NULL) {
4089 return (NULL);
4090 }
jcarsey4d0a4fc2011-07-06 22:28:36 +00004091 Status = ShellFileHandleReadLine(Handle, RetVal, &Size, FALSE, Ascii);
darylm503b0934ac2011-11-12 00:35:11 +00004092
jcarsey4d0a4fc2011-07-06 22:28:36 +00004093 }
Qiu Shumine3b76f32016-02-22 10:21:38 +08004094 if (Status == EFI_END_OF_FILE && RetVal != NULL && *RetVal != CHAR_NULL) {
Qiu Shuminf79d7b62016-02-18 13:12:58 +08004095 Status = EFI_SUCCESS;
4096 }
jcarsey4d0a4fc2011-07-06 22:28:36 +00004097 if (EFI_ERROR(Status) && (RetVal != NULL)) {
4098 FreePool(RetVal);
4099 RetVal = NULL;
4100 }
4101 return (RetVal);
4102}
4103
4104/**
4105 Function to read a single line (up to but not including the \n) from a SHELL_FILE_HANDLE.
4106
4107 If the position upon start is 0, then the Ascii Boolean will be set. This should be
4108 maintained and not changed for all operations with the same file.
4109
Jim Dailey2dda8a12016-02-10 08:53:00 -08004110 NOTE: LINES THAT ARE RETURNED BY THIS FUNCTION ARE UCS2, EVEN IF THE FILE BEING READ
4111 IS IN ASCII FORMAT.
4112
ydong104ff7e372011-09-02 08:05:34 +00004113 @param[in] Handle SHELL_FILE_HANDLE to read from.
Jim Dailey2dda8a12016-02-10 08:53:00 -08004114 @param[in, out] Buffer The pointer to buffer to read into. If this function
4115 returns EFI_SUCCESS, then on output Buffer will
4116 contain a UCS2 string, even if the file being
4117 read is ASCII.
4118 @param[in, out] Size On input, pointer to number of bytes in Buffer.
4119 On output, unchanged unless Buffer is too small
4120 to contain the next line of the file. In that
4121 case Size is set to the number of bytes needed
4122 to hold the next line of the file (as a UCS2
4123 string, even if it is an ASCII file).
ydong104ff7e372011-09-02 08:05:34 +00004124 @param[in] Truncate If the buffer is large enough, this has no effect.
4125 If the buffer is is too small and Truncate is TRUE,
4126 the line will be truncated.
4127 If the buffer is is too small and Truncate is FALSE,
4128 then no read will occur.
jcarsey4d0a4fc2011-07-06 22:28:36 +00004129
ydong104ff7e372011-09-02 08:05:34 +00004130 @param[in, out] Ascii Boolean value for indicating whether the file is
4131 Ascii (TRUE) or UCS2 (FALSE).
jcarsey4d0a4fc2011-07-06 22:28:36 +00004132
4133 @retval EFI_SUCCESS The operation was successful. The line is stored in
4134 Buffer.
jaben carsey9ed21942016-02-08 15:59:04 -08004135 @retval EFI_END_OF_FILE There are no more lines in the file.
jcarsey4d0a4fc2011-07-06 22:28:36 +00004136 @retval EFI_INVALID_PARAMETER Handle was NULL.
4137 @retval EFI_INVALID_PARAMETER Size was NULL.
4138 @retval EFI_BUFFER_TOO_SMALL Size was not large enough to store the line.
4139 Size was updated to the minimum space required.
4140**/
4141EFI_STATUS
4142EFIAPI
4143ShellFileHandleReadLine(
4144 IN SHELL_FILE_HANDLE Handle,
4145 IN OUT CHAR16 *Buffer,
4146 IN OUT UINTN *Size,
4147 IN BOOLEAN Truncate,
4148 IN OUT BOOLEAN *Ascii
4149 )
4150{
4151 EFI_STATUS Status;
4152 CHAR16 CharBuffer;
4153 UINTN CharSize;
4154 UINTN CountSoFar;
4155 UINT64 OriginalFilePosition;
4156
4157
4158 if (Handle == NULL
4159 ||Size == NULL
4160 ){
4161 return (EFI_INVALID_PARAMETER);
4162 }
4163 if (Buffer == NULL) {
4164 ASSERT(*Size == 0);
4165 } else {
4166 *Buffer = CHAR_NULL;
4167 }
4168 gEfiShellProtocol->GetFilePosition(Handle, &OriginalFilePosition);
4169 if (OriginalFilePosition == 0) {
4170 CharSize = sizeof(CHAR16);
4171 Status = gEfiShellProtocol->ReadFile(Handle, &CharSize, &CharBuffer);
4172 ASSERT_EFI_ERROR(Status);
4173 if (CharBuffer == gUnicodeFileTag) {
4174 *Ascii = FALSE;
4175 } else {
4176 *Ascii = TRUE;
4177 gEfiShellProtocol->SetFilePosition(Handle, OriginalFilePosition);
4178 }
4179 }
4180
jaben carsey9ed21942016-02-08 15:59:04 -08004181 if (*Ascii) {
4182 CharSize = sizeof(CHAR8);
4183 } else {
4184 CharSize = sizeof(CHAR16);
4185 }
jcarsey4d0a4fc2011-07-06 22:28:36 +00004186 for (CountSoFar = 0;;CountSoFar++){
4187 CharBuffer = 0;
jcarsey4d0a4fc2011-07-06 22:28:36 +00004188 Status = gEfiShellProtocol->ReadFile(Handle, &CharSize, &CharBuffer);
4189 if ( EFI_ERROR(Status)
4190 || CharSize == 0
4191 || (CharBuffer == L'\n' && !(*Ascii))
4192 || (CharBuffer == '\n' && *Ascii)
4193 ){
jaben carsey9ed21942016-02-08 15:59:04 -08004194 if (CharSize == 0) {
4195 Status = EFI_END_OF_FILE;
4196 }
jcarsey4d0a4fc2011-07-06 22:28:36 +00004197 break;
4198 }
4199 //
4200 // if we have space save it...
4201 //
Jim Dailey1095b432016-02-10 13:17:56 -08004202 if ((CountSoFar+1)*sizeof(CHAR16) < *Size){
jcarsey4d0a4fc2011-07-06 22:28:36 +00004203 ASSERT(Buffer != NULL);
Jim Dailey1095b432016-02-10 13:17:56 -08004204 ((CHAR16*)Buffer)[CountSoFar] = CharBuffer;
4205 ((CHAR16*)Buffer)[CountSoFar+1] = CHAR_NULL;
jcarsey4d0a4fc2011-07-06 22:28:36 +00004206 }
4207 }
4208
4209 //
4210 // if we ran out of space tell when...
4211 //
Jim Dailey1095b432016-02-10 13:17:56 -08004212 if ((CountSoFar+1)*sizeof(CHAR16) > *Size){
4213 *Size = (CountSoFar+1)*sizeof(CHAR16);
4214 if (!Truncate) {
4215 gEfiShellProtocol->SetFilePosition(Handle, OriginalFilePosition);
4216 } else {
4217 DEBUG((DEBUG_WARN, "The line was truncated in ShellFileHandleReadLine"));
jcarsey4d0a4fc2011-07-06 22:28:36 +00004218 }
Jim Dailey1095b432016-02-10 13:17:56 -08004219 return (EFI_BUFFER_TOO_SMALL);
4220 }
4221 while(Buffer[StrLen(Buffer)-1] == L'\r') {
4222 Buffer[StrLen(Buffer)-1] = CHAR_NULL;
jcarsey4d0a4fc2011-07-06 22:28:36 +00004223 }
4224
4225 return (Status);
4226}
jcarseyfb5278e2013-02-20 18:21:14 +00004227
4228/**
jcarsey365aa982013-03-04 21:54:02 +00004229 Function to print help file / man page content in the spec from the UEFI Shell protocol GetHelpText function.
4230
4231 @param[in] CommandToGetHelpOn Pointer to a string containing the command name of help file to be printed.
4232 @param[in] SectionToGetHelpOn Pointer to the section specifier(s).
4233 @param[in] PrintCommandText If TRUE, prints the command followed by the help content, otherwise prints
4234 the help content only.
4235 @retval EFI_DEVICE_ERROR The help data format was incorrect.
4236 @retval EFI_NOT_FOUND The help data could not be found.
4237 @retval EFI_SUCCESS The operation was successful.
4238**/
4239EFI_STATUS
4240EFIAPI
4241ShellPrintHelp (
4242 IN CONST CHAR16 *CommandToGetHelpOn,
4243 IN CONST CHAR16 *SectionToGetHelpOn,
4244 IN BOOLEAN PrintCommandText
4245 )
4246{
4247 EFI_STATUS Status;
4248 CHAR16 *OutText;
4249
4250 OutText = NULL;
4251
4252 //
4253 // Get the string to print based
4254 //
4255 Status = gEfiShellProtocol->GetHelpText (CommandToGetHelpOn, SectionToGetHelpOn, &OutText);
4256
4257 //
4258 // make sure we got a valid string
4259 //
4260 if (EFI_ERROR(Status)){
4261 return Status;
4262 }
4263 if (OutText == NULL || StrLen(OutText) == 0) {
4264 return EFI_NOT_FOUND;
4265 }
4266
4267 //
4268 // Chop off trailing stuff we dont need
4269 //
4270 while (OutText[StrLen(OutText)-1] == L'\r' || OutText[StrLen(OutText)-1] == L'\n' || OutText[StrLen(OutText)-1] == L' ') {
4271 OutText[StrLen(OutText)-1] = CHAR_NULL;
4272 }
4273
4274 //
4275 // Print this out to the console
4276 //
4277 if (PrintCommandText) {
4278 ShellPrintEx(-1, -1, L"%H%-14s%N- %s\r\n", CommandToGetHelpOn, OutText);
4279 } else {
4280 ShellPrintEx(-1, -1, L"%N%s\r\n", OutText);
4281 }
4282
4283 SHELL_FREE_NON_NULL(OutText);
4284
4285 return EFI_SUCCESS;
4286}
4287
4288/**
jcarseyfb5278e2013-02-20 18:21:14 +00004289 Function to delete a file by name
4290
4291 @param[in] FileName Pointer to file name to delete.
4292
4293 @retval EFI_SUCCESS the file was deleted sucessfully
4294 @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not
4295 deleted
4296 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
4297 @retval EFI_NOT_FOUND The specified file could not be found on the
4298 device or the file system could not be found
4299 on the device.
4300 @retval EFI_NO_MEDIA The device has no medium.
4301 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
4302 medium is no longer supported.
4303 @retval EFI_DEVICE_ERROR The device reported an error.
4304 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
4305 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
4306 @retval EFI_ACCESS_DENIED The file was opened read only.
4307 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
4308 file.
4309 @retval other The file failed to open
4310**/
4311EFI_STATUS
4312EFIAPI
4313ShellDeleteFileByName(
4314 IN CONST CHAR16 *FileName
4315 )
4316{
4317 EFI_STATUS Status;
4318 SHELL_FILE_HANDLE FileHandle;
4319
4320 Status = ShellFileExists(FileName);
4321
4322 if (Status == EFI_SUCCESS){
4323 Status = ShellOpenFileByName(FileName, &FileHandle, EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE, 0x0);
4324 if (Status == EFI_SUCCESS){
4325 Status = ShellDeleteFile(&FileHandle);
4326 }
4327 }
4328
4329 return(Status);
4330
4331}
Qiu Shumin0960ba12014-09-17 07:58:31 +00004332
4333/**
4334 Cleans off all the quotes in the string.
4335
4336 @param[in] OriginalString pointer to the string to be cleaned.
4337 @param[out] CleanString The new string with all quotes removed.
4338 Memory allocated in the function and free
4339 by caller.
4340
4341 @retval EFI_SUCCESS The operation was successful.
4342**/
4343EFI_STATUS
Qiu Shumin0960ba12014-09-17 07:58:31 +00004344InternalShellStripQuotes (
4345 IN CONST CHAR16 *OriginalString,
4346 OUT CHAR16 **CleanString
4347 )
4348{
4349 CHAR16 *Walker;
4350
4351 if (OriginalString == NULL || CleanString == NULL) {
4352 return EFI_INVALID_PARAMETER;
4353 }
4354
4355 *CleanString = AllocateCopyPool (StrSize (OriginalString), OriginalString);
4356 if (*CleanString == NULL) {
4357 return EFI_OUT_OF_RESOURCES;
4358 }
4359
4360 for (Walker = *CleanString; Walker != NULL && *Walker != CHAR_NULL ; Walker++) {
4361 if (*Walker == L'\"') {
4362 CopyMem(Walker, Walker+1, StrSize(Walker) - sizeof(Walker[0]));
4363 }
4364 }
4365
4366 return EFI_SUCCESS;
4367}
4368