blob: e4e7e3d8296dab184cee91d114a6131253183f8f [file] [log] [blame]
jcarsey94b17fa2009-05-07 18:46:18 +00001/** @file
2 Provides interface to shell functionality for shell commands and applications.
3
jaben carsey9ed21942016-02-08 15:59:04 -08004 Copyright 2016 Dell Inc.
Qiu Shuminf79d7b62016-02-18 13:12:58 +08005 Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>
jcarsey1e6e84c2010-01-25 20:05:08 +00006 This program and the accompanying materials
jcarseyb3011f42010-01-11 21:49:04 +00007 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
jcarsey94b17fa2009-05-07 18:46:18 +000010
jcarseyb3011f42010-01-11 21:49:04 +000011 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
jcarsey94b17fa2009-05-07 18:46:18 +000013
14**/
15
jcarseyb1f95a02009-06-16 00:23:19 +000016#include "UefiShellLib.h"
jcarseya405b862010-09-14 05:18:09 +000017#include <ShellBase.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;
jcarseyb3011f42010-01-11 21:49:04 +000039
jcarsey2247dde2009-11-09 18:08:58 +000040/**
41 Check if a Unicode character is a hexadecimal character.
42
jcarsey1e6e84c2010-01-25 20:05:08 +000043 This internal function checks if a Unicode character is a
jcarseya405b862010-09-14 05:18:09 +000044 numeric character. The valid hexadecimal characters are
jcarsey2247dde2009-11-09 18:08:58 +000045 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.
46
jcarsey2247dde2009-11-09 18:08:58 +000047 @param Char The character to check against.
48
49 @retval TRUE If the Char is a hexadecmial character.
50 @retval FALSE If the Char is not a hexadecmial character.
51
52**/
53BOOLEAN
54EFIAPI
jcarsey969c7832010-01-13 16:46:33 +000055ShellIsHexaDecimalDigitCharacter (
jcarsey2247dde2009-11-09 18:08:58 +000056 IN CHAR16 Char
jcarseya405b862010-09-14 05:18:09 +000057 )
58{
jcarsey2247dde2009-11-09 18:08:58 +000059 return (BOOLEAN) ((Char >= L'0' && Char <= L'9') || (Char >= L'A' && Char <= L'F') || (Char >= L'a' && Char <= L'f'));
60}
jcarsey94b17fa2009-05-07 18:46:18 +000061
62/**
jcarseya405b862010-09-14 05:18:09 +000063 Check if a Unicode character is a decimal character.
64
65 This internal function checks if a Unicode character is a
66 decimal character. The valid characters are
67 L'0' to L'9'.
68
69
70 @param Char The character to check against.
71
72 @retval TRUE If the Char is a hexadecmial character.
73 @retval FALSE If the Char is not a hexadecmial character.
74
75**/
76BOOLEAN
77EFIAPI
78ShellIsDecimalDigitCharacter (
79 IN CHAR16 Char
80 )
81{
82 return (BOOLEAN) (Char >= L'0' && Char <= L'9');
83}
84
85/**
86 Helper function to find ShellEnvironment2 for constructor.
87
88 @param[in] ImageHandle A copy of the calling image's handle.
jcarseybeab0fc2011-10-10 17:26:25 +000089
90 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.
jcarsey94b17fa2009-05-07 18:46:18 +000091**/
92EFI_STATUS
93EFIAPI
94ShellFindSE2 (
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
177EFIAPI
jcarseyd2b45642009-05-11 18:02:16 +0000178ShellLibConstructorWorker (
jcarsey94b17fa2009-05-07 18:46:18 +0000179 IN EFI_HANDLE ImageHandle,
180 IN EFI_SYSTEM_TABLE *SystemTable
jcarseya405b862010-09-14 05:18:09 +0000181 )
182{
183 EFI_STATUS Status;
jcarseyecd3d592009-12-07 18:05:00 +0000184
jcarsey94b17fa2009-05-07 18:46:18 +0000185 //
186 // UEFI 2.0 shell interfaces (used preferentially)
187 //
jcarseya405b862010-09-14 05:18:09 +0000188 Status = gBS->OpenProtocol(
189 ImageHandle,
190 &gEfiShellProtocolGuid,
jcarsey366f81a2011-06-27 21:04:22 +0000191 (VOID **)&gEfiShellProtocol,
jcarseya405b862010-09-14 05:18:09 +0000192 ImageHandle,
193 NULL,
194 EFI_OPEN_PROTOCOL_GET_PROTOCOL
195 );
jcarsey94b17fa2009-05-07 18:46:18 +0000196 if (EFI_ERROR(Status)) {
jcarseya405b862010-09-14 05:18:09 +0000197 //
198 // Search for the shell protocol
199 //
200 Status = gBS->LocateProtocol(
201 &gEfiShellProtocolGuid,
202 NULL,
jcarsey366f81a2011-06-27 21:04:22 +0000203 (VOID **)&gEfiShellProtocol
jcarseya405b862010-09-14 05:18:09 +0000204 );
205 if (EFI_ERROR(Status)) {
jcarsey366f81a2011-06-27 21:04:22 +0000206 gEfiShellProtocol = NULL;
jcarseya405b862010-09-14 05:18:09 +0000207 }
jcarsey94b17fa2009-05-07 18:46:18 +0000208 }
jcarseya405b862010-09-14 05:18:09 +0000209 Status = gBS->OpenProtocol(
210 ImageHandle,
211 &gEfiShellParametersProtocolGuid,
jcarsey366f81a2011-06-27 21:04:22 +0000212 (VOID **)&gEfiShellParametersProtocol,
jcarseya405b862010-09-14 05:18:09 +0000213 ImageHandle,
214 NULL,
215 EFI_OPEN_PROTOCOL_GET_PROTOCOL
216 );
jcarsey94b17fa2009-05-07 18:46:18 +0000217 if (EFI_ERROR(Status)) {
jcarsey366f81a2011-06-27 21:04:22 +0000218 gEfiShellParametersProtocol = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +0000219 }
220
jcarsey366f81a2011-06-27 21:04:22 +0000221 if (gEfiShellParametersProtocol == NULL || gEfiShellProtocol == NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +0000222 //
223 // Moved to seperate function due to complexity
224 //
225 Status = ShellFindSE2(ImageHandle);
226
227 if (EFI_ERROR(Status)) {
228 DEBUG((DEBUG_ERROR, "Status: 0x%08x\r\n", Status));
229 mEfiShellEnvironment2 = NULL;
230 }
jcarsey1e6e84c2010-01-25 20:05:08 +0000231 Status = gBS->OpenProtocol(ImageHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000232 &gEfiShellInterfaceGuid,
233 (VOID **)&mEfiShellInterface,
234 ImageHandle,
235 NULL,
236 EFI_OPEN_PROTOCOL_GET_PROTOCOL
jcarseya405b862010-09-14 05:18:09 +0000237 );
jcarsey94b17fa2009-05-07 18:46:18 +0000238 if (EFI_ERROR(Status)) {
239 mEfiShellInterface = NULL;
240 }
241 }
jcarseyc9d92df2010-02-03 15:37:54 +0000242
jcarsey94b17fa2009-05-07 18:46:18 +0000243 //
244 // only success getting 2 of either the old or new, but no 1/2 and 1/2
245 //
jcarsey1e6e84c2010-01-25 20:05:08 +0000246 if ((mEfiShellEnvironment2 != NULL && mEfiShellInterface != NULL) ||
jcarsey366f81a2011-06-27 21:04:22 +0000247 (gEfiShellProtocol != NULL && gEfiShellParametersProtocol != NULL) ) {
248 if (gEfiShellProtocol != NULL) {
249 FileFunctionMap.GetFileInfo = gEfiShellProtocol->GetFileInfo;
250 FileFunctionMap.SetFileInfo = gEfiShellProtocol->SetFileInfo;
251 FileFunctionMap.ReadFile = gEfiShellProtocol->ReadFile;
252 FileFunctionMap.WriteFile = gEfiShellProtocol->WriteFile;
253 FileFunctionMap.CloseFile = gEfiShellProtocol->CloseFile;
254 FileFunctionMap.DeleteFile = gEfiShellProtocol->DeleteFile;
255 FileFunctionMap.GetFilePosition = gEfiShellProtocol->GetFilePosition;
256 FileFunctionMap.SetFilePosition = gEfiShellProtocol->SetFilePosition;
257 FileFunctionMap.FlushFile = gEfiShellProtocol->FlushFile;
258 FileFunctionMap.GetFileSize = gEfiShellProtocol->GetFileSize;
jcarseyd2b45642009-05-11 18:02:16 +0000259 } else {
jcarseya405b862010-09-14 05:18:09 +0000260 FileFunctionMap.GetFileInfo = (EFI_SHELL_GET_FILE_INFO)FileHandleGetInfo;
261 FileFunctionMap.SetFileInfo = (EFI_SHELL_SET_FILE_INFO)FileHandleSetInfo;
262 FileFunctionMap.ReadFile = (EFI_SHELL_READ_FILE)FileHandleRead;
263 FileFunctionMap.WriteFile = (EFI_SHELL_WRITE_FILE)FileHandleWrite;
264 FileFunctionMap.CloseFile = (EFI_SHELL_CLOSE_FILE)FileHandleClose;
265 FileFunctionMap.DeleteFile = (EFI_SHELL_DELETE_FILE)FileHandleDelete;
266 FileFunctionMap.GetFilePosition = (EFI_SHELL_GET_FILE_POSITION)FileHandleGetPosition;
267 FileFunctionMap.SetFilePosition = (EFI_SHELL_SET_FILE_POSITION)FileHandleSetPosition;
268 FileFunctionMap.FlushFile = (EFI_SHELL_FLUSH_FILE)FileHandleFlush;
269 FileFunctionMap.GetFileSize = (EFI_SHELL_GET_FILE_SIZE)FileHandleGetSize;
jcarseyd2b45642009-05-11 18:02:16 +0000270 }
jcarsey94b17fa2009-05-07 18:46:18 +0000271 return (EFI_SUCCESS);
272 }
273 return (EFI_NOT_FOUND);
274}
jcarseyd2b45642009-05-11 18:02:16 +0000275/**
276 Constructor for the Shell library.
277
278 Initialize the library and determine if the underlying is a UEFI Shell 2.0 or an EFI shell.
279
280 @param ImageHandle the image handle of the process
281 @param SystemTable the EFI System Table pointer
282
283 @retval EFI_SUCCESS the initialization was complete sucessfully
284 @return others an error ocurred during initialization
285**/
286EFI_STATUS
287EFIAPI
288ShellLibConstructor (
289 IN EFI_HANDLE ImageHandle,
290 IN EFI_SYSTEM_TABLE *SystemTable
jcarseya405b862010-09-14 05:18:09 +0000291 )
292{
jcarseyd2b45642009-05-11 18:02:16 +0000293 mEfiShellEnvironment2 = NULL;
jcarsey366f81a2011-06-27 21:04:22 +0000294 gEfiShellProtocol = NULL;
295 gEfiShellParametersProtocol = NULL;
jcarseyd2b45642009-05-11 18:02:16 +0000296 mEfiShellInterface = NULL;
297 mEfiShellEnvironment2Handle = NULL;
298
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);
705 if (FileName == NULL) {
706 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);
jcarseya405b862010-09-14 05:18:09 +0000723 if (StrCmp(FileName, L"NUL") != 0 && !EFI_ERROR(Status) && ((OpenMode & EFI_FILE_MODE_CREATE) != 0)){
jcarsey2247dde2009-11-09 18:08:58 +0000724 FileInfo = FileFunctionMap.GetFileInfo(*FileHandle);
jcarseyb1f95a02009-06-16 00:23:19 +0000725 ASSERT(FileInfo != NULL);
726 FileInfo->Attribute = Attributes;
Yao Jiewendd17e3f2015-12-25 01:24:16 +0000727 Status2 = FileFunctionMap.SetFileInfo(*FileHandle, FileInfo);
jcarsey2247dde2009-11-09 18:08:58 +0000728 FreePool(FileInfo);
Yao Jiewendd17e3f2015-12-25 01:24:16 +0000729 if (EFI_ERROR (Status2)) {
730 gEfiShellProtocol->CloseFile(*FileHandle);
731 }
732 Status = Status2;
jcarseyb1f95a02009-06-16 00:23:19 +0000733 }
734 return (Status);
jcarsey1e6e84c2010-01-25 20:05:08 +0000735 }
jcarsey94b17fa2009-05-07 18:46:18 +0000736 //
737 // Using EFI Shell version
738 // this means convert name to path and call that function
739 // since this will use EFI method again that will open it.
740 //
741 ASSERT(mEfiShellEnvironment2 != NULL);
jcarseyb82bfcc2009-06-29 16:28:23 +0000742 FilePath = mEfiShellEnvironment2->NameToPath ((CHAR16*)FileName);
xdu290bfa222010-07-19 05:21:27 +0000743 if (FilePath != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +0000744 return (ShellOpenFileByDevicePath(&FilePath,
745 &DeviceHandle,
746 FileHandle,
747 OpenMode,
jcarseya405b862010-09-14 05:18:09 +0000748 Attributes));
jcarsey94b17fa2009-05-07 18:46:18 +0000749 }
750 return (EFI_DEVICE_ERROR);
751}
752/**
753 This function create a directory
754
jcarsey1e6e84c2010-01-25 20:05:08 +0000755 If return is EFI_SUCCESS, the Filehandle is the opened directory's handle;
756 otherwise, the Filehandle is NULL. If the directory already existed, this
jcarsey94b17fa2009-05-07 18:46:18 +0000757 function opens the existing directory.
758
darylm503b0934ac2011-11-12 00:35:11 +0000759 @param DirectoryName pointer to directory name
760 @param FileHandle pointer to the file handle.
jcarsey94b17fa2009-05-07 18:46:18 +0000761
darylm503b0934ac2011-11-12 00:35:11 +0000762 @retval EFI_SUCCESS The information was set.
763 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
764 @retval EFI_UNSUPPORTED Could not open the file path.
765 @retval EFI_NOT_FOUND The specified file could not be found on the
jcarsey1e6e84c2010-01-25 20:05:08 +0000766 device or the file system could not be found
jcarsey94b17fa2009-05-07 18:46:18 +0000767 on the device.
darylm503b0934ac2011-11-12 00:35:11 +0000768 @retval EFI_NO_MEDIA The device has no medium.
769 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
jcarsey94b17fa2009-05-07 18:46:18 +0000770 medium is no longer supported.
darylm503b0934ac2011-11-12 00:35:11 +0000771 @retval EFI_DEVICE_ERROR The device reported an error.
772 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
773 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
774 @retval EFI_ACCESS_DENIED The file was opened read only.
775 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
jcarsey94b17fa2009-05-07 18:46:18 +0000776 file.
darylm503b0934ac2011-11-12 00:35:11 +0000777 @retval EFI_VOLUME_FULL The volume is full.
jcarsey94b17fa2009-05-07 18:46:18 +0000778 @sa ShellOpenFileByName
779**/
780EFI_STATUS
781EFIAPI
782ShellCreateDirectory(
jcarseyb82bfcc2009-06-29 16:28:23 +0000783 IN CONST CHAR16 *DirectoryName,
jcarseya405b862010-09-14 05:18:09 +0000784 OUT SHELL_FILE_HANDLE *FileHandle
785 )
786{
jcarsey366f81a2011-06-27 21:04:22 +0000787 if (gEfiShellProtocol != NULL) {
jcarsey2247dde2009-11-09 18:08:58 +0000788 //
789 // Use UEFI Shell 2.0 method
790 //
jcarsey366f81a2011-06-27 21:04:22 +0000791 return (gEfiShellProtocol->CreateFile(DirectoryName,
jcarsey2247dde2009-11-09 18:08:58 +0000792 EFI_FILE_DIRECTORY,
793 FileHandle
jcarseya405b862010-09-14 05:18:09 +0000794 ));
jcarsey2247dde2009-11-09 18:08:58 +0000795 } else {
796 return (ShellOpenFileByName(DirectoryName,
797 FileHandle,
798 EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE,
799 EFI_FILE_DIRECTORY
jcarseya405b862010-09-14 05:18:09 +0000800 ));
jcarsey2247dde2009-11-09 18:08:58 +0000801 }
jcarsey94b17fa2009-05-07 18:46:18 +0000802}
803
804/**
805 This function reads information from an opened file.
806
jcarsey1e6e84c2010-01-25 20:05:08 +0000807 If FileHandle is not a directory, the function reads the requested number of
808 bytes from the file at the file's current position and returns them in Buffer.
jcarsey94b17fa2009-05-07 18:46:18 +0000809 If the read goes beyond the end of the file, the read length is truncated to the
jcarsey1e6e84c2010-01-25 20:05:08 +0000810 end of the file. The file's current position is increased by the number of bytes
811 returned. If FileHandle is a directory, the function reads the directory entry
812 at the file's current position and returns the entry in Buffer. If the Buffer
813 is not large enough to hold the current directory entry, then
814 EFI_BUFFER_TOO_SMALL is returned and the current file position is not updated.
815 BufferSize is set to be the size of the buffer needed to read the entry. On
816 success, the current position is updated to the next directory entry. If there
817 are no more directory entries, the read returns a zero-length buffer.
jcarsey94b17fa2009-05-07 18:46:18 +0000818 EFI_FILE_INFO is the structure returned as the directory entry.
819
820 @param FileHandle the opened file handle
jcarsey1e6e84c2010-01-25 20:05:08 +0000821 @param BufferSize on input the size of buffer in bytes. on return
jcarsey94b17fa2009-05-07 18:46:18 +0000822 the number of bytes written.
823 @param Buffer the buffer to put read data into.
824
darylm503b0934ac2011-11-12 00:35:11 +0000825 @retval EFI_SUCCESS Data was read.
826 @retval EFI_NO_MEDIA The device has no media.
827 @retval EFI_DEVICE_ERROR The device reported an error.
828 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
829 @retval EFI_BUFFER_TO_SMALL Buffer is too small. ReadSize contains required
jcarsey94b17fa2009-05-07 18:46:18 +0000830 size.
831
832**/
833EFI_STATUS
834EFIAPI
835ShellReadFile(
jcarseya405b862010-09-14 05:18:09 +0000836 IN SHELL_FILE_HANDLE FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000837 IN OUT UINTN *BufferSize,
838 OUT VOID *Buffer
jcarseya405b862010-09-14 05:18:09 +0000839 )
840{
jcarseyd2b45642009-05-11 18:02:16 +0000841 return (FileFunctionMap.ReadFile(FileHandle, BufferSize, Buffer));
jcarsey94b17fa2009-05-07 18:46:18 +0000842}
843
844
845/**
846 Write data to a file.
847
jcarsey1e6e84c2010-01-25 20:05:08 +0000848 This function writes the specified number of bytes to the file at the current
849 file position. The current file position is advanced the actual number of bytes
850 written, which is returned in BufferSize. Partial writes only occur when there
851 has been a data error during the write attempt (such as "volume space full").
852 The file is automatically grown to hold the data if required. Direct writes to
jcarsey94b17fa2009-05-07 18:46:18 +0000853 opened directories are not supported.
854
855 @param FileHandle The opened file for writing
856 @param BufferSize on input the number of bytes in Buffer. On output
857 the number of bytes written.
858 @param Buffer the buffer containing data to write is stored.
859
darylm503b0934ac2011-11-12 00:35:11 +0000860 @retval EFI_SUCCESS Data was written.
861 @retval EFI_UNSUPPORTED Writes to an open directory are not supported.
862 @retval EFI_NO_MEDIA The device has no media.
863 @retval EFI_DEVICE_ERROR The device reported an error.
864 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
865 @retval EFI_WRITE_PROTECTED The device is write-protected.
866 @retval EFI_ACCESS_DENIED The file was open for read only.
867 @retval EFI_VOLUME_FULL The volume is full.
jcarsey94b17fa2009-05-07 18:46:18 +0000868**/
869EFI_STATUS
870EFIAPI
871ShellWriteFile(
jcarsey252d9452011-03-25 20:49:53 +0000872 IN SHELL_FILE_HANDLE FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000873 IN OUT UINTN *BufferSize,
874 IN VOID *Buffer
jcarseya405b862010-09-14 05:18:09 +0000875 )
876{
jcarseyd2b45642009-05-11 18:02:16 +0000877 return (FileFunctionMap.WriteFile(FileHandle, BufferSize, Buffer));
jcarsey94b17fa2009-05-07 18:46:18 +0000878}
879
jcarsey1e6e84c2010-01-25 20:05:08 +0000880/**
jcarsey94b17fa2009-05-07 18:46:18 +0000881 Close an open file handle.
882
jcarsey1e6e84c2010-01-25 20:05:08 +0000883 This function closes a specified file handle. All "dirty" cached file data is
884 flushed to the device, and the file is closed. In all cases the handle is
jcarsey94b17fa2009-05-07 18:46:18 +0000885 closed.
886
887@param FileHandle the file handle to close.
888
889@retval EFI_SUCCESS the file handle was closed sucessfully.
890**/
891EFI_STATUS
892EFIAPI
893ShellCloseFile (
jcarseya405b862010-09-14 05:18:09 +0000894 IN SHELL_FILE_HANDLE *FileHandle
895 )
896{
jcarseyd2b45642009-05-11 18:02:16 +0000897 return (FileFunctionMap.CloseFile(*FileHandle));
jcarsey94b17fa2009-05-07 18:46:18 +0000898}
899
900/**
901 Delete a file and close the handle
902
903 This function closes and deletes a file. In all cases the file handle is closed.
jcarsey1e6e84c2010-01-25 20:05:08 +0000904 If the file cannot be deleted, the warning code EFI_WARN_DELETE_FAILURE is
jcarsey94b17fa2009-05-07 18:46:18 +0000905 returned, but the handle is still closed.
906
907 @param FileHandle the file handle to delete
908
909 @retval EFI_SUCCESS the file was closed sucessfully
jcarsey1e6e84c2010-01-25 20:05:08 +0000910 @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not
jcarsey94b17fa2009-05-07 18:46:18 +0000911 deleted
darylm503b0934ac2011-11-12 00:35:11 +0000912 @retval INVALID_PARAMETER One of the parameters has an invalid value.
jcarsey94b17fa2009-05-07 18:46:18 +0000913**/
914EFI_STATUS
915EFIAPI
916ShellDeleteFile (
darylm503b0934ac2011-11-12 00:35:11 +0000917 IN SHELL_FILE_HANDLE *FileHandle
jcarseya405b862010-09-14 05:18:09 +0000918 )
919{
jcarseyd2b45642009-05-11 18:02:16 +0000920 return (FileFunctionMap.DeleteFile(*FileHandle));
jcarsey94b17fa2009-05-07 18:46:18 +0000921}
922
923/**
924 Set the current position in a file.
925
jcarsey1e6e84c2010-01-25 20:05:08 +0000926 This function sets the current file position for the handle to the position
jcarsey94b17fa2009-05-07 18:46:18 +0000927 supplied. With the exception of seeking to position 0xFFFFFFFFFFFFFFFF, only
jcarsey1e6e84c2010-01-25 20:05:08 +0000928 absolute positioning is supported, and seeking past the end of the file is
929 allowed (a subsequent write would grow the file). Seeking to position
jcarsey94b17fa2009-05-07 18:46:18 +0000930 0xFFFFFFFFFFFFFFFF causes the current position to be set to the end of the file.
jcarsey1e6e84c2010-01-25 20:05:08 +0000931 If FileHandle is a directory, the only position that may be set is zero. This
jcarsey94b17fa2009-05-07 18:46:18 +0000932 has the effect of starting the read process of the directory entries over.
933
934 @param FileHandle The file handle on which the position is being set
935 @param Position Byte position from begining of file
936
937 @retval EFI_SUCCESS Operation completed sucessfully.
jcarsey1e6e84c2010-01-25 20:05:08 +0000938 @retval EFI_UNSUPPORTED the seek request for non-zero is not valid on
jcarsey94b17fa2009-05-07 18:46:18 +0000939 directories.
940 @retval INVALID_PARAMETER One of the parameters has an invalid value.
941**/
942EFI_STATUS
943EFIAPI
944ShellSetFilePosition (
darylm503b0934ac2011-11-12 00:35:11 +0000945 IN SHELL_FILE_HANDLE FileHandle,
946 IN UINT64 Position
jcarseya405b862010-09-14 05:18:09 +0000947 )
948{
jcarseyd2b45642009-05-11 18:02:16 +0000949 return (FileFunctionMap.SetFilePosition(FileHandle, Position));
jcarsey94b17fa2009-05-07 18:46:18 +0000950}
951
jcarsey1e6e84c2010-01-25 20:05:08 +0000952/**
jcarsey94b17fa2009-05-07 18:46:18 +0000953 Gets a file's current position
954
jcarsey1e6e84c2010-01-25 20:05:08 +0000955 This function retrieves the current file position for the file handle. For
956 directories, the current file position has no meaning outside of the file
jcarsey94b17fa2009-05-07 18:46:18 +0000957 system driver and as such the operation is not supported. An error is returned
958 if FileHandle is a directory.
959
960 @param FileHandle The open file handle on which to get the position.
961 @param Position Byte position from begining of file.
962
963 @retval EFI_SUCCESS the operation completed sucessfully.
964 @retval INVALID_PARAMETER One of the parameters has an invalid value.
965 @retval EFI_UNSUPPORTED the request is not valid on directories.
966**/
967EFI_STATUS
968EFIAPI
969ShellGetFilePosition (
jcarseya405b862010-09-14 05:18:09 +0000970 IN SHELL_FILE_HANDLE FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000971 OUT UINT64 *Position
jcarseya405b862010-09-14 05:18:09 +0000972 )
973{
jcarseyd2b45642009-05-11 18:02:16 +0000974 return (FileFunctionMap.GetFilePosition(FileHandle, Position));
jcarsey94b17fa2009-05-07 18:46:18 +0000975}
976/**
977 Flushes data on a file
jcarsey1e6e84c2010-01-25 20:05:08 +0000978
jcarsey94b17fa2009-05-07 18:46:18 +0000979 This function flushes all modified data associated with a file to a device.
980
981 @param FileHandle The file handle on which to flush data
982
983 @retval EFI_SUCCESS The data was flushed.
984 @retval EFI_NO_MEDIA The device has no media.
985 @retval EFI_DEVICE_ERROR The device reported an error.
986 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
987 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
988 @retval EFI_ACCESS_DENIED The file was opened for read only.
989**/
990EFI_STATUS
991EFIAPI
992ShellFlushFile (
jcarseya405b862010-09-14 05:18:09 +0000993 IN SHELL_FILE_HANDLE FileHandle
994 )
995{
jcarseyd2b45642009-05-11 18:02:16 +0000996 return (FileFunctionMap.FlushFile(FileHandle));
jcarsey94b17fa2009-05-07 18:46:18 +0000997}
998
darylm503b0934ac2011-11-12 00:35:11 +0000999/** Retrieve first entry from a directory.
jcarsey94b17fa2009-05-07 18:46:18 +00001000
darylm503b0934ac2011-11-12 00:35:11 +00001001 This function takes an open directory handle and gets information from the
1002 first entry in the directory. A buffer is allocated to contain
1003 the information and a pointer to the buffer is returned in *Buffer. The
1004 caller can use ShellFindNextFile() to get subsequent directory entries.
jcarsey94b17fa2009-05-07 18:46:18 +00001005
darylm503b0934ac2011-11-12 00:35:11 +00001006 The buffer will be freed by ShellFindNextFile() when the last directory
1007 entry is read. Otherwise, the caller must free the buffer, using FreePool,
1008 when finished with it.
1009
1010 @param[in] DirHandle The file handle of the directory to search.
1011 @param[out] Buffer The pointer to the buffer for the file's information.
jcarsey94b17fa2009-05-07 18:46:18 +00001012
1013 @retval EFI_SUCCESS Found the first file.
1014 @retval EFI_NOT_FOUND Cannot find the directory.
1015 @retval EFI_NO_MEDIA The device has no media.
1016 @retval EFI_DEVICE_ERROR The device reported an error.
1017 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
1018 @return Others status of ShellGetFileInfo, ShellSetFilePosition,
1019 or ShellReadFile
1020**/
1021EFI_STATUS
1022EFIAPI
1023ShellFindFirstFile (
jcarseya405b862010-09-14 05:18:09 +00001024 IN SHELL_FILE_HANDLE DirHandle,
jcarseyd2b45642009-05-11 18:02:16 +00001025 OUT EFI_FILE_INFO **Buffer
jcarseya405b862010-09-14 05:18:09 +00001026 )
1027{
jcarsey94b17fa2009-05-07 18:46:18 +00001028 //
jcarseyd2b45642009-05-11 18:02:16 +00001029 // pass to file handle lib
jcarsey94b17fa2009-05-07 18:46:18 +00001030 //
jcarseyd2b45642009-05-11 18:02:16 +00001031 return (FileHandleFindFirstFile(DirHandle, Buffer));
jcarsey94b17fa2009-05-07 18:46:18 +00001032}
darylm503b0934ac2011-11-12 00:35:11 +00001033/** Retrieve next entries from a directory.
jcarsey94b17fa2009-05-07 18:46:18 +00001034
darylm503b0934ac2011-11-12 00:35:11 +00001035 To use this function, the caller must first call the ShellFindFirstFile()
1036 function to get the first directory entry. Subsequent directory entries are
1037 retrieved by using the ShellFindNextFile() function. This function can
1038 be called several times to get each entry from the directory. If the call of
1039 ShellFindNextFile() retrieved the last directory entry, the next call of
1040 this function will set *NoFile to TRUE and free the buffer.
jcarsey94b17fa2009-05-07 18:46:18 +00001041
darylm503b0934ac2011-11-12 00:35:11 +00001042 @param[in] DirHandle The file handle of the directory.
1043 @param[out] Buffer The pointer to buffer for file's information.
1044 @param[out] NoFile The pointer to boolean when last file is found.
jcarsey94b17fa2009-05-07 18:46:18 +00001045
1046 @retval EFI_SUCCESS Found the next file, or reached last file
1047 @retval EFI_NO_MEDIA The device has no media.
1048 @retval EFI_DEVICE_ERROR The device reported an error.
1049 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
1050**/
1051EFI_STATUS
1052EFIAPI
1053ShellFindNextFile(
jcarseya405b862010-09-14 05:18:09 +00001054 IN SHELL_FILE_HANDLE DirHandle,
jcarsey94b17fa2009-05-07 18:46:18 +00001055 OUT EFI_FILE_INFO *Buffer,
1056 OUT BOOLEAN *NoFile
jcarseya405b862010-09-14 05:18:09 +00001057 )
1058{
jcarsey94b17fa2009-05-07 18:46:18 +00001059 //
jcarseyd2b45642009-05-11 18:02:16 +00001060 // pass to file handle lib
jcarsey94b17fa2009-05-07 18:46:18 +00001061 //
jcarseyd2b45642009-05-11 18:02:16 +00001062 return (FileHandleFindNextFile(DirHandle, Buffer, NoFile));
jcarsey94b17fa2009-05-07 18:46:18 +00001063}
1064/**
1065 Retrieve the size of a file.
1066
1067 if FileHandle is NULL then ASSERT()
1068 if Size is NULL then ASSERT()
1069
jcarsey1e6e84c2010-01-25 20:05:08 +00001070 This function extracts the file size info from the FileHandle's EFI_FILE_INFO
jcarsey94b17fa2009-05-07 18:46:18 +00001071 data.
1072
1073 @param FileHandle file handle from which size is retrieved
1074 @param Size pointer to size
1075
1076 @retval EFI_SUCCESS operation was completed sucessfully
1077 @retval EFI_DEVICE_ERROR cannot access the file
1078**/
1079EFI_STATUS
1080EFIAPI
1081ShellGetFileSize (
jcarseya405b862010-09-14 05:18:09 +00001082 IN SHELL_FILE_HANDLE FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +00001083 OUT UINT64 *Size
jcarseya405b862010-09-14 05:18:09 +00001084 )
1085{
jcarseyd2b45642009-05-11 18:02:16 +00001086 return (FileFunctionMap.GetFileSize(FileHandle, Size));
jcarsey94b17fa2009-05-07 18:46:18 +00001087}
1088/**
1089 Retrieves the status of the break execution flag
1090
1091 this function is useful to check whether the application is being asked to halt by the shell.
1092
1093 @retval TRUE the execution break is enabled
1094 @retval FALSE the execution break is not enabled
1095**/
1096BOOLEAN
1097EFIAPI
1098ShellGetExecutionBreakFlag(
1099 VOID
1100 )
1101{
jcarsey1e6e84c2010-01-25 20:05:08 +00001102 //
jcarsey94b17fa2009-05-07 18:46:18 +00001103 // Check for UEFI Shell 2.0 protocols
1104 //
jcarsey366f81a2011-06-27 21:04:22 +00001105 if (gEfiShellProtocol != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00001106
1107 //
1108 // We are using UEFI Shell 2.0; see if the event has been triggered
1109 //
jcarsey366f81a2011-06-27 21:04:22 +00001110 if (gBS->CheckEvent(gEfiShellProtocol->ExecutionBreak) != EFI_SUCCESS) {
jcarsey94b17fa2009-05-07 18:46:18 +00001111 return (FALSE);
1112 }
1113 return (TRUE);
jcarsey1e6e84c2010-01-25 20:05:08 +00001114 }
jcarsey94b17fa2009-05-07 18:46:18 +00001115
1116 //
1117 // using EFI Shell; call the function to check
1118 //
jcarsey92a54472011-06-27 20:33:13 +00001119 if (mEfiShellEnvironment2 != NULL) {
1120 return (mEfiShellEnvironment2->GetExecutionBreak());
1121 }
1122
1123 return (FALSE);
jcarsey94b17fa2009-05-07 18:46:18 +00001124}
1125/**
1126 return the value of an environment variable
1127
jcarsey1e6e84c2010-01-25 20:05:08 +00001128 this function gets the value of the environment variable set by the
jcarsey94b17fa2009-05-07 18:46:18 +00001129 ShellSetEnvironmentVariable function
1130
1131 @param EnvKey The key name of the environment variable.
1132
1133 @retval NULL the named environment variable does not exist.
1134 @return != NULL pointer to the value of the environment variable
1135**/
1136CONST CHAR16*
1137EFIAPI
1138ShellGetEnvironmentVariable (
jcarsey9b3bf082009-06-23 21:15:07 +00001139 IN CONST CHAR16 *EnvKey
jcarsey94b17fa2009-05-07 18:46:18 +00001140 )
1141{
jcarsey1e6e84c2010-01-25 20:05:08 +00001142 //
jcarsey94b17fa2009-05-07 18:46:18 +00001143 // Check for UEFI Shell 2.0 protocols
1144 //
jcarsey366f81a2011-06-27 21:04:22 +00001145 if (gEfiShellProtocol != NULL) {
1146 return (gEfiShellProtocol->GetEnv(EnvKey));
jcarsey94b17fa2009-05-07 18:46:18 +00001147 }
1148
1149 //
jcarsey92a54472011-06-27 20:33:13 +00001150 // Check for EFI shell
jcarsey94b17fa2009-05-07 18:46:18 +00001151 //
jcarsey92a54472011-06-27 20:33:13 +00001152 if (mEfiShellEnvironment2 != NULL) {
1153 return (mEfiShellEnvironment2->GetEnv((CHAR16*)EnvKey));
1154 }
jcarsey94b17fa2009-05-07 18:46:18 +00001155
jcarsey92a54472011-06-27 20:33:13 +00001156 return NULL;
jcarsey94b17fa2009-05-07 18:46:18 +00001157}
1158/**
1159 set the value of an environment variable
1160
1161This function changes the current value of the specified environment variable. If the
1162environment variable exists and the Value is an empty string, then the environment
1163variable is deleted. If the environment variable exists and the Value is not an empty
1164string, then the value of the environment variable is changed. If the environment
1165variable does not exist and the Value is an empty string, there is no action. If the
1166environment variable does not exist and the Value is a non-empty string, then the
1167environment variable is created and assigned the specified value.
1168
1169 This is not supported pre-UEFI Shell 2.0.
1170
1171 @param EnvKey The key name of the environment variable.
1172 @param EnvVal The Value of the environment variable
1173 @param Volatile Indicates whether the variable is non-volatile (FALSE) or volatile (TRUE).
1174
1175 @retval EFI_SUCCESS the operation was completed sucessfully
1176 @retval EFI_UNSUPPORTED This operation is not allowed in pre UEFI 2.0 Shell environments
1177**/
1178EFI_STATUS
1179EFIAPI
1180ShellSetEnvironmentVariable (
1181 IN CONST CHAR16 *EnvKey,
1182 IN CONST CHAR16 *EnvVal,
1183 IN BOOLEAN Volatile
1184 )
1185{
jcarsey1e6e84c2010-01-25 20:05:08 +00001186 //
jcarsey94b17fa2009-05-07 18:46:18 +00001187 // Check for UEFI Shell 2.0 protocols
1188 //
jcarsey366f81a2011-06-27 21:04:22 +00001189 if (gEfiShellProtocol != NULL) {
1190 return (gEfiShellProtocol->SetEnv(EnvKey, EnvVal, Volatile));
jcarsey1e6e84c2010-01-25 20:05:08 +00001191 }
jcarsey94b17fa2009-05-07 18:46:18 +00001192
1193 //
1194 // This feature does not exist under EFI shell
1195 //
1196 return (EFI_UNSUPPORTED);
1197}
jcarseya405b862010-09-14 05:18:09 +00001198
jcarsey94b17fa2009-05-07 18:46:18 +00001199/**
jcarseya405b862010-09-14 05:18:09 +00001200 Cause the shell to parse and execute a command line.
jcarsey94b17fa2009-05-07 18:46:18 +00001201
1202 This function creates a nested instance of the shell and executes the specified
jcarseya405b862010-09-14 05:18:09 +00001203 command (CommandLine) with the specified environment (Environment). Upon return,
1204 the status code returned by the specified command is placed in StatusCode.
1205 If Environment is NULL, then the current environment is used and all changes made
1206 by the commands executed will be reflected in the current environment. If the
1207 Environment is non-NULL, then the changes made will be discarded.
1208 The CommandLine is executed from the current working directory on the current
1209 device.
jcarsey94b17fa2009-05-07 18:46:18 +00001210
Brendan Jackman3877d0f2014-01-24 22:31:07 +00001211 The EnvironmentVariables pararemeter is ignored in a pre-UEFI Shell 2.0
jcarseya405b862010-09-14 05:18:09 +00001212 environment. The values pointed to by the parameters will be unchanged by the
1213 ShellExecute() function. The Output parameter has no effect in a
1214 UEFI Shell 2.0 environment.
jcarsey94b17fa2009-05-07 18:46:18 +00001215
jcarseya405b862010-09-14 05:18:09 +00001216 @param[in] ParentHandle The parent image starting the operation.
1217 @param[in] CommandLine The pointer to a NULL terminated command line.
1218 @param[in] Output True to display debug output. False to hide it.
1219 @param[in] EnvironmentVariables Optional pointer to array of environment variables
1220 in the form "x=y". If NULL, the current set is used.
1221 @param[out] Status The status of the run command line.
jcarsey94b17fa2009-05-07 18:46:18 +00001222
jcarseya405b862010-09-14 05:18:09 +00001223 @retval EFI_SUCCESS The operation completed sucessfully. Status
1224 contains the status code returned.
1225 @retval EFI_INVALID_PARAMETER A parameter contains an invalid value.
1226 @retval EFI_OUT_OF_RESOURCES Out of resources.
1227 @retval EFI_UNSUPPORTED The operation is not allowed.
jcarsey94b17fa2009-05-07 18:46:18 +00001228**/
1229EFI_STATUS
1230EFIAPI
1231ShellExecute (
1232 IN EFI_HANDLE *ParentHandle,
1233 IN CHAR16 *CommandLine OPTIONAL,
1234 IN BOOLEAN Output OPTIONAL,
1235 IN CHAR16 **EnvironmentVariables OPTIONAL,
1236 OUT EFI_STATUS *Status OPTIONAL
1237 )
1238{
Brendan Jackman3877d0f2014-01-24 22:31:07 +00001239 EFI_STATUS CmdStatus;
jcarsey1e6e84c2010-01-25 20:05:08 +00001240 //
jcarsey94b17fa2009-05-07 18:46:18 +00001241 // Check for UEFI Shell 2.0 protocols
1242 //
jcarsey366f81a2011-06-27 21:04:22 +00001243 if (gEfiShellProtocol != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00001244 //
1245 // Call UEFI Shell 2.0 version (not using Output parameter)
1246 //
jcarsey366f81a2011-06-27 21:04:22 +00001247 return (gEfiShellProtocol->Execute(ParentHandle,
jcarsey94b17fa2009-05-07 18:46:18 +00001248 CommandLine,
1249 EnvironmentVariables,
1250 Status));
jcarsey1e6e84c2010-01-25 20:05:08 +00001251 }
jcarsey92a54472011-06-27 20:33:13 +00001252
jcarsey94b17fa2009-05-07 18:46:18 +00001253 //
jcarsey92a54472011-06-27 20:33:13 +00001254 // Check for EFI shell
jcarsey94b17fa2009-05-07 18:46:18 +00001255 //
jcarsey92a54472011-06-27 20:33:13 +00001256 if (mEfiShellEnvironment2 != NULL) {
1257 //
Brendan Jackman3877d0f2014-01-24 22:31:07 +00001258 // Call EFI Shell version.
jcarsey92a54472011-06-27 20:33:13 +00001259 // Due to oddity in the EFI shell we want to dereference the ParentHandle here
1260 //
Brendan Jackman3877d0f2014-01-24 22:31:07 +00001261 CmdStatus = (mEfiShellEnvironment2->Execute(*ParentHandle,
jcarsey92a54472011-06-27 20:33:13 +00001262 CommandLine,
1263 Output));
Brendan Jackman3877d0f2014-01-24 22:31:07 +00001264 //
1265 // No Status output parameter so just use the returned status
1266 //
1267 if (Status != NULL) {
1268 *Status = CmdStatus;
1269 }
1270 //
1271 // If there was an error, we can't tell if it was from the command or from
1272 // the Execute() function, so we'll just assume the shell ran successfully
1273 // and the error came from the command.
1274 //
1275 return EFI_SUCCESS;
jcarsey92a54472011-06-27 20:33:13 +00001276 }
1277
1278 return (EFI_UNSUPPORTED);
jcarsey94b17fa2009-05-07 18:46:18 +00001279}
Brendan Jackman3877d0f2014-01-24 22:31:07 +00001280
jcarsey94b17fa2009-05-07 18:46:18 +00001281/**
1282 Retreives the current directory path
1283
jcarsey1e6e84c2010-01-25 20:05:08 +00001284 If the DeviceName is NULL, it returns the current device's current directory
1285 name. If the DeviceName is not NULL, it returns the current directory name
jcarsey94b17fa2009-05-07 18:46:18 +00001286 on specified drive.
1287
Qiu Shuminfbd2dfa2015-10-23 02:03:20 +00001288 Note that the current directory string should exclude the tailing backslash character.
1289
jcarsey94b17fa2009-05-07 18:46:18 +00001290 @param DeviceName the name of the drive to get directory on
1291
1292 @retval NULL the directory does not exist
1293 @return != NULL the directory
1294**/
1295CONST CHAR16*
1296EFIAPI
1297ShellGetCurrentDir (
jcarseya405b862010-09-14 05:18:09 +00001298 IN CHAR16 * CONST DeviceName OPTIONAL
jcarsey94b17fa2009-05-07 18:46:18 +00001299 )
1300{
jcarsey1e6e84c2010-01-25 20:05:08 +00001301 //
jcarsey94b17fa2009-05-07 18:46:18 +00001302 // Check for UEFI Shell 2.0 protocols
1303 //
jcarsey366f81a2011-06-27 21:04:22 +00001304 if (gEfiShellProtocol != NULL) {
1305 return (gEfiShellProtocol->GetCurDir(DeviceName));
jcarsey1e6e84c2010-01-25 20:05:08 +00001306 }
jcarsey92a54472011-06-27 20:33:13 +00001307
jcarsey94b17fa2009-05-07 18:46:18 +00001308 //
jcarsey8bd282b2011-06-27 16:45:41 +00001309 // Check for EFI shell
jcarsey94b17fa2009-05-07 18:46:18 +00001310 //
jcarsey8bd282b2011-06-27 16:45:41 +00001311 if (mEfiShellEnvironment2 != NULL) {
1312 return (mEfiShellEnvironment2->CurDir(DeviceName));
1313 }
1314
1315 return (NULL);
jcarsey94b17fa2009-05-07 18:46:18 +00001316}
1317/**
1318 sets (enabled or disabled) the page break mode
1319
jcarsey1e6e84c2010-01-25 20:05:08 +00001320 when page break mode is enabled the screen will stop scrolling
jcarsey94b17fa2009-05-07 18:46:18 +00001321 and wait for operator input before scrolling a subsequent screen.
1322
1323 @param CurrentState TRUE to enable and FALSE to disable
1324**/
jcarsey1e6e84c2010-01-25 20:05:08 +00001325VOID
jcarsey94b17fa2009-05-07 18:46:18 +00001326EFIAPI
1327ShellSetPageBreakMode (
1328 IN BOOLEAN CurrentState
1329 )
1330{
1331 //
1332 // check for enabling
1333 //
1334 if (CurrentState != 0x00) {
jcarsey1e6e84c2010-01-25 20:05:08 +00001335 //
jcarsey94b17fa2009-05-07 18:46:18 +00001336 // check for UEFI Shell 2.0
1337 //
jcarsey366f81a2011-06-27 21:04:22 +00001338 if (gEfiShellProtocol != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00001339 //
1340 // Enable with UEFI 2.0 Shell
1341 //
jcarsey366f81a2011-06-27 21:04:22 +00001342 gEfiShellProtocol->EnablePageBreak();
jcarsey94b17fa2009-05-07 18:46:18 +00001343 return;
1344 } else {
jcarsey1e6e84c2010-01-25 20:05:08 +00001345 //
jcarsey92a54472011-06-27 20:33:13 +00001346 // Check for EFI shell
jcarsey94b17fa2009-05-07 18:46:18 +00001347 //
jcarsey92a54472011-06-27 20:33:13 +00001348 if (mEfiShellEnvironment2 != NULL) {
1349 //
1350 // Enable with EFI Shell
1351 //
1352 mEfiShellEnvironment2->EnablePageBreak (DEFAULT_INIT_ROW, DEFAULT_AUTO_LF);
1353 return;
1354 }
jcarsey94b17fa2009-05-07 18:46:18 +00001355 }
1356 } else {
jcarsey1e6e84c2010-01-25 20:05:08 +00001357 //
jcarsey94b17fa2009-05-07 18:46:18 +00001358 // check for UEFI Shell 2.0
1359 //
jcarsey366f81a2011-06-27 21:04:22 +00001360 if (gEfiShellProtocol != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00001361 //
1362 // Disable with UEFI 2.0 Shell
1363 //
jcarsey366f81a2011-06-27 21:04:22 +00001364 gEfiShellProtocol->DisablePageBreak();
jcarsey94b17fa2009-05-07 18:46:18 +00001365 return;
1366 } else {
jcarsey1e6e84c2010-01-25 20:05:08 +00001367 //
jcarsey92a54472011-06-27 20:33:13 +00001368 // Check for EFI shell
jcarsey94b17fa2009-05-07 18:46:18 +00001369 //
jcarsey92a54472011-06-27 20:33:13 +00001370 if (mEfiShellEnvironment2 != NULL) {
1371 //
1372 // Disable with EFI Shell
1373 //
1374 mEfiShellEnvironment2->DisablePageBreak ();
1375 return;
1376 }
jcarsey94b17fa2009-05-07 18:46:18 +00001377 }
1378 }
1379}
1380
1381///
1382/// version of EFI_SHELL_FILE_INFO struct, except has no CONST pointers.
1383/// This allows for the struct to be populated.
1384///
1385typedef struct {
jcarseyd2b45642009-05-11 18:02:16 +00001386 LIST_ENTRY Link;
jcarsey94b17fa2009-05-07 18:46:18 +00001387 EFI_STATUS Status;
1388 CHAR16 *FullName;
1389 CHAR16 *FileName;
jcarseya405b862010-09-14 05:18:09 +00001390 SHELL_FILE_HANDLE Handle;
jcarsey94b17fa2009-05-07 18:46:18 +00001391 EFI_FILE_INFO *Info;
1392} EFI_SHELL_FILE_INFO_NO_CONST;
1393
1394/**
1395 Converts a EFI shell list of structures to the coresponding UEFI Shell 2.0 type of list.
1396
1397 if OldStyleFileList is NULL then ASSERT()
1398
jcarsey1e6e84c2010-01-25 20:05:08 +00001399 this function will convert a SHELL_FILE_ARG based list into a callee allocated
jcarsey94b17fa2009-05-07 18:46:18 +00001400 EFI_SHELL_FILE_INFO based list. it is up to the caller to free the memory via
1401 the ShellCloseFileMetaArg function.
1402
ydong104ff7e372011-09-02 08:05:34 +00001403 @param[in] FileList the EFI shell list type
1404 @param[in, out] ListHead the list to add to
jcarsey94b17fa2009-05-07 18:46:18 +00001405
1406 @retval the resultant head of the double linked new format list;
1407**/
1408LIST_ENTRY*
1409EFIAPI
1410InternalShellConvertFileListType (
jcarsey9b3bf082009-06-23 21:15:07 +00001411 IN LIST_ENTRY *FileList,
1412 IN OUT LIST_ENTRY *ListHead
jcarsey125c2cf2009-11-18 21:36:50 +00001413 )
1414{
jcarsey94b17fa2009-05-07 18:46:18 +00001415 SHELL_FILE_ARG *OldInfo;
jcarsey9b3bf082009-06-23 21:15:07 +00001416 LIST_ENTRY *Link;
jcarsey94b17fa2009-05-07 18:46:18 +00001417 EFI_SHELL_FILE_INFO_NO_CONST *NewInfo;
1418
1419 //
jcarsey9b3bf082009-06-23 21:15:07 +00001420 // ASSERTs
jcarsey94b17fa2009-05-07 18:46:18 +00001421 //
jcarsey9b3bf082009-06-23 21:15:07 +00001422 ASSERT(FileList != NULL);
1423 ASSERT(ListHead != NULL);
jcarsey94b17fa2009-05-07 18:46:18 +00001424
1425 //
1426 // enumerate through each member of the old list and copy
1427 //
jcarseyd2b45642009-05-11 18:02:16 +00001428 for (Link = FileList->ForwardLink; Link != FileList; Link = Link->ForwardLink) {
jcarsey94b17fa2009-05-07 18:46:18 +00001429 OldInfo = CR (Link, SHELL_FILE_ARG, Link, SHELL_FILE_ARG_SIGNATURE);
jcarseya405b862010-09-14 05:18:09 +00001430 ASSERT(OldInfo != NULL);
1431
1432 //
1433 // Skip ones that failed to open...
1434 //
1435 if (OldInfo->Status != EFI_SUCCESS) {
1436 continue;
1437 }
jcarsey94b17fa2009-05-07 18:46:18 +00001438
1439 //
1440 // make sure the old list was valid
1441 //
jcarsey94b17fa2009-05-07 18:46:18 +00001442 ASSERT(OldInfo->Info != NULL);
1443 ASSERT(OldInfo->FullName != NULL);
1444 ASSERT(OldInfo->FileName != NULL);
1445
1446 //
1447 // allocate a new EFI_SHELL_FILE_INFO object
1448 //
1449 NewInfo = AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));
jcarseyc9d92df2010-02-03 15:37:54 +00001450 if (NewInfo == NULL) {
jcarsey7a95efd2011-10-13 16:08:18 +00001451 ShellCloseFileMetaArg((EFI_SHELL_FILE_INFO**)(&ListHead));
jcarseybeab0fc2011-10-10 17:26:25 +00001452 ListHead = NULL;
jcarseyc9d92df2010-02-03 15:37:54 +00001453 break;
1454 }
jcarsey1e6e84c2010-01-25 20:05:08 +00001455
1456 //
jcarsey94b17fa2009-05-07 18:46:18 +00001457 // copy the simple items
1458 //
1459 NewInfo->Handle = OldInfo->Handle;
1460 NewInfo->Status = OldInfo->Status;
1461
jcarseyd2b45642009-05-11 18:02:16 +00001462 // old shell checks for 0 not NULL
1463 OldInfo->Handle = 0;
1464
jcarsey94b17fa2009-05-07 18:46:18 +00001465 //
1466 // allocate new space to copy strings and structure
1467 //
Jaben Carsey98c16be2014-08-19 21:00:34 +00001468 NewInfo->FullName = AllocateCopyPool(StrSize(OldInfo->FullName), OldInfo->FullName);
1469 NewInfo->FileName = AllocateCopyPool(StrSize(OldInfo->FileName), OldInfo->FileName);
1470 NewInfo->Info = AllocateCopyPool((UINTN)OldInfo->Info->Size, OldInfo->Info);
jcarsey1e6e84c2010-01-25 20:05:08 +00001471
jcarsey94b17fa2009-05-07 18:46:18 +00001472 //
1473 // make sure all the memory allocations were sucessful
1474 //
jcarseybeab0fc2011-10-10 17:26:25 +00001475 if (NULL == NewInfo->FullName || NewInfo->FileName == NULL || NewInfo->Info == NULL) {
Jaben Carsey98c16be2014-08-19 21:00:34 +00001476 //
1477 // Free the partially allocated new node
1478 //
1479 SHELL_FREE_NON_NULL(NewInfo->FullName);
1480 SHELL_FREE_NON_NULL(NewInfo->FileName);
1481 SHELL_FREE_NON_NULL(NewInfo->Info);
1482 SHELL_FREE_NON_NULL(NewInfo);
1483
1484 //
1485 // Free the previously converted stuff
1486 //
jcarsey7a95efd2011-10-13 16:08:18 +00001487 ShellCloseFileMetaArg((EFI_SHELL_FILE_INFO**)(&ListHead));
jcarseybeab0fc2011-10-10 17:26:25 +00001488 ListHead = NULL;
1489 break;
1490 }
jcarsey94b17fa2009-05-07 18:46:18 +00001491
1492 //
jcarsey94b17fa2009-05-07 18:46:18 +00001493 // add that to the list
1494 //
jcarsey9b3bf082009-06-23 21:15:07 +00001495 InsertTailList(ListHead, &NewInfo->Link);
jcarsey94b17fa2009-05-07 18:46:18 +00001496 }
1497 return (ListHead);
1498}
1499/**
1500 Opens a group of files based on a path.
1501
jcarsey1e6e84c2010-01-25 20:05:08 +00001502 This function uses the Arg to open all the matching files. Each matched
Brendan Jackman70879312014-01-24 22:29:53 +00001503 file has a SHELL_FILE_INFO structure to record the file information. These
1504 structures are placed on the list ListHead. Users can get the SHELL_FILE_INFO
jcarsey94b17fa2009-05-07 18:46:18 +00001505 structures from ListHead to access each file. This function supports wildcards
jcarsey1e6e84c2010-01-25 20:05:08 +00001506 and will process '?' and '*' as such. the list must be freed with a call to
jcarsey94b17fa2009-05-07 18:46:18 +00001507 ShellCloseFileMetaArg().
1508
jcarsey1e6e84c2010-01-25 20:05:08 +00001509 If you are NOT appending to an existing list *ListHead must be NULL. If
jcarsey5f7431d2009-07-10 18:06:01 +00001510 *ListHead is NULL then it must be callee freed.
jcarsey94b17fa2009-05-07 18:46:18 +00001511
1512 @param Arg pointer to path string
1513 @param OpenMode mode to open files with
1514 @param ListHead head of linked list of results
1515
jcarsey1e6e84c2010-01-25 20:05:08 +00001516 @retval EFI_SUCCESS the operation was sucessful and the list head
jcarsey94b17fa2009-05-07 18:46:18 +00001517 contains the list of opened files
jcarsey94b17fa2009-05-07 18:46:18 +00001518 @return != EFI_SUCCESS the operation failed
1519
1520 @sa InternalShellConvertFileListType
1521**/
1522EFI_STATUS
1523EFIAPI
1524ShellOpenFileMetaArg (
1525 IN CHAR16 *Arg,
1526 IN UINT64 OpenMode,
1527 IN OUT EFI_SHELL_FILE_INFO **ListHead
1528 )
1529{
1530 EFI_STATUS Status;
jcarsey9b3bf082009-06-23 21:15:07 +00001531 LIST_ENTRY mOldStyleFileList;
Qiu Shumin0960ba12014-09-17 07:58:31 +00001532 CHAR16 *CleanFilePathStr;
jcarsey1e6e84c2010-01-25 20:05:08 +00001533
jcarsey94b17fa2009-05-07 18:46:18 +00001534 //
1535 // ASSERT that Arg and ListHead are not NULL
1536 //
1537 ASSERT(Arg != NULL);
1538 ASSERT(ListHead != NULL);
1539
Qiu Shumin715096c2014-09-19 01:32:05 +00001540 CleanFilePathStr = NULL;
1541
Qiu Shumin0960ba12014-09-17 07:58:31 +00001542 Status = InternalShellStripQuotes (Arg, &CleanFilePathStr);
1543 if (EFI_ERROR (Status)) {
1544 return Status;
1545 }
1546
jcarsey1e6e84c2010-01-25 20:05:08 +00001547 //
jcarsey94b17fa2009-05-07 18:46:18 +00001548 // Check for UEFI Shell 2.0 protocols
1549 //
jcarsey366f81a2011-06-27 21:04:22 +00001550 if (gEfiShellProtocol != NULL) {
jcarsey5f7431d2009-07-10 18:06:01 +00001551 if (*ListHead == NULL) {
1552 *ListHead = (EFI_SHELL_FILE_INFO*)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));
1553 if (*ListHead == NULL) {
Qiu Shumin0960ba12014-09-17 07:58:31 +00001554 FreePool(CleanFilePathStr);
jcarsey5f7431d2009-07-10 18:06:01 +00001555 return (EFI_OUT_OF_RESOURCES);
1556 }
1557 InitializeListHead(&((*ListHead)->Link));
jcarsey1e6e84c2010-01-25 20:05:08 +00001558 }
Qiu Shumin0960ba12014-09-17 07:58:31 +00001559 Status = gEfiShellProtocol->OpenFileList(CleanFilePathStr,
jcarsey1e6e84c2010-01-25 20:05:08 +00001560 OpenMode,
jcarsey2247dde2009-11-09 18:08:58 +00001561 ListHead);
1562 if (EFI_ERROR(Status)) {
jcarsey366f81a2011-06-27 21:04:22 +00001563 gEfiShellProtocol->RemoveDupInFileList(ListHead);
jcarsey2247dde2009-11-09 18:08:58 +00001564 } else {
jcarsey366f81a2011-06-27 21:04:22 +00001565 Status = gEfiShellProtocol->RemoveDupInFileList(ListHead);
jcarsey2247dde2009-11-09 18:08:58 +00001566 }
jcarseya405b862010-09-14 05:18:09 +00001567 if (*ListHead != NULL && IsListEmpty(&(*ListHead)->Link)) {
1568 FreePool(*ListHead);
Qiu Shumin0960ba12014-09-17 07:58:31 +00001569 FreePool(CleanFilePathStr);
jcarseya405b862010-09-14 05:18:09 +00001570 *ListHead = NULL;
1571 return (EFI_NOT_FOUND);
1572 }
Qiu Shumin0960ba12014-09-17 07:58:31 +00001573 FreePool(CleanFilePathStr);
jcarsey2247dde2009-11-09 18:08:58 +00001574 return (Status);
jcarsey1e6e84c2010-01-25 20:05:08 +00001575 }
jcarsey94b17fa2009-05-07 18:46:18 +00001576
1577 //
jcarsey92a54472011-06-27 20:33:13 +00001578 // Check for EFI shell
jcarsey94b17fa2009-05-07 18:46:18 +00001579 //
jcarsey92a54472011-06-27 20:33:13 +00001580 if (mEfiShellEnvironment2 != NULL) {
1581 //
1582 // make sure the list head is initialized
1583 //
1584 InitializeListHead(&mOldStyleFileList);
jcarsey94b17fa2009-05-07 18:46:18 +00001585
jcarsey92a54472011-06-27 20:33:13 +00001586 //
1587 // Get the EFI Shell list of files
1588 //
Qiu Shumin0960ba12014-09-17 07:58:31 +00001589 Status = mEfiShellEnvironment2->FileMetaArg(CleanFilePathStr, &mOldStyleFileList);
jcarsey92a54472011-06-27 20:33:13 +00001590 if (EFI_ERROR(Status)) {
1591 *ListHead = NULL;
Qiu Shumin0960ba12014-09-17 07:58:31 +00001592 FreePool(CleanFilePathStr);
jcarsey92a54472011-06-27 20:33:13 +00001593 return (Status);
1594 }
jcarsey94b17fa2009-05-07 18:46:18 +00001595
jcarsey92a54472011-06-27 20:33:13 +00001596 if (*ListHead == NULL) {
1597 *ListHead = (EFI_SHELL_FILE_INFO *)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));
1598 if (*ListHead == NULL) {
Qiu Shumin0960ba12014-09-17 07:58:31 +00001599 FreePool(CleanFilePathStr);
jcarsey92a54472011-06-27 20:33:13 +00001600 return (EFI_OUT_OF_RESOURCES);
1601 }
1602 InitializeListHead(&((*ListHead)->Link));
1603 }
1604
1605 //
1606 // Convert that to equivalent of UEFI Shell 2.0 structure
1607 //
1608 InternalShellConvertFileListType(&mOldStyleFileList, &(*ListHead)->Link);
1609
1610 //
1611 // Free the EFI Shell version that was converted.
1612 //
1613 mEfiShellEnvironment2->FreeFileList(&mOldStyleFileList);
1614
1615 if ((*ListHead)->Link.ForwardLink == (*ListHead)->Link.BackLink && (*ListHead)->Link.BackLink == &((*ListHead)->Link)) {
1616 FreePool(*ListHead);
1617 *ListHead = NULL;
1618 Status = EFI_NOT_FOUND;
1619 }
Qiu Shumin0960ba12014-09-17 07:58:31 +00001620 FreePool(CleanFilePathStr);
jcarsey94b17fa2009-05-07 18:46:18 +00001621 return (Status);
1622 }
1623
Qiu Shumin0960ba12014-09-17 07:58:31 +00001624 FreePool(CleanFilePathStr);
jcarsey92a54472011-06-27 20:33:13 +00001625 return (EFI_UNSUPPORTED);
jcarsey94b17fa2009-05-07 18:46:18 +00001626}
1627/**
jcarseya405b862010-09-14 05:18:09 +00001628 Free the linked list returned from ShellOpenFileMetaArg.
jcarsey94b17fa2009-05-07 18:46:18 +00001629
jcarseya405b862010-09-14 05:18:09 +00001630 if ListHead is NULL then ASSERT().
jcarsey94b17fa2009-05-07 18:46:18 +00001631
jcarseya405b862010-09-14 05:18:09 +00001632 @param ListHead the pointer to free.
jcarsey94b17fa2009-05-07 18:46:18 +00001633
jcarseya405b862010-09-14 05:18:09 +00001634 @retval EFI_SUCCESS the operation was sucessful.
jcarsey94b17fa2009-05-07 18:46:18 +00001635**/
1636EFI_STATUS
1637EFIAPI
1638ShellCloseFileMetaArg (
1639 IN OUT EFI_SHELL_FILE_INFO **ListHead
1640 )
1641{
1642 LIST_ENTRY *Node;
1643
1644 //
1645 // ASSERT that ListHead is not NULL
1646 //
1647 ASSERT(ListHead != NULL);
1648
jcarsey1e6e84c2010-01-25 20:05:08 +00001649 //
jcarsey94b17fa2009-05-07 18:46:18 +00001650 // Check for UEFI Shell 2.0 protocols
1651 //
jcarsey366f81a2011-06-27 21:04:22 +00001652 if (gEfiShellProtocol != NULL) {
1653 return (gEfiShellProtocol->FreeFileList(ListHead));
jcarsey92a54472011-06-27 20:33:13 +00001654 } else if (mEfiShellEnvironment2 != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00001655 //
jcarsey1e6e84c2010-01-25 20:05:08 +00001656 // Since this is EFI Shell version we need to free our internally made copy
jcarsey94b17fa2009-05-07 18:46:18 +00001657 // of the list
1658 //
jcarsey1e6e84c2010-01-25 20:05:08 +00001659 for ( Node = GetFirstNode(&(*ListHead)->Link)
jcarseya405b862010-09-14 05:18:09 +00001660 ; *ListHead != NULL && !IsListEmpty(&(*ListHead)->Link)
jcarsey9b3bf082009-06-23 21:15:07 +00001661 ; Node = GetFirstNode(&(*ListHead)->Link)) {
jcarsey94b17fa2009-05-07 18:46:18 +00001662 RemoveEntryList(Node);
jcarseya405b862010-09-14 05:18:09 +00001663 ((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 +00001664 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->FullName);
1665 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->FileName);
1666 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->Info);
1667 FreePool((EFI_SHELL_FILE_INFO_NO_CONST*)Node);
1668 }
Jaben Carsey75a5e2e2014-01-10 16:42:45 +00001669 SHELL_FREE_NON_NULL(*ListHead);
jcarsey94b17fa2009-05-07 18:46:18 +00001670 return EFI_SUCCESS;
1671 }
jcarsey92a54472011-06-27 20:33:13 +00001672
1673 return (EFI_UNSUPPORTED);
jcarsey94b17fa2009-05-07 18:46:18 +00001674}
1675
jcarsey125c2cf2009-11-18 21:36:50 +00001676/**
1677 Find a file by searching the CWD and then the path.
1678
jcarseyb3011f42010-01-11 21:49:04 +00001679 If FileName is NULL then ASSERT.
jcarsey125c2cf2009-11-18 21:36:50 +00001680
jcarseyb3011f42010-01-11 21:49:04 +00001681 If the return value is not NULL then the memory must be caller freed.
jcarsey125c2cf2009-11-18 21:36:50 +00001682
1683 @param FileName Filename string.
1684
1685 @retval NULL the file was not found
1686 @return !NULL the full path to the file.
1687**/
1688CHAR16 *
1689EFIAPI
1690ShellFindFilePath (
1691 IN CONST CHAR16 *FileName
1692 )
1693{
1694 CONST CHAR16 *Path;
jcarseya405b862010-09-14 05:18:09 +00001695 SHELL_FILE_HANDLE Handle;
jcarsey125c2cf2009-11-18 21:36:50 +00001696 EFI_STATUS Status;
1697 CHAR16 *RetVal;
1698 CHAR16 *TestPath;
1699 CONST CHAR16 *Walker;
jcarsey36a9d672009-11-20 21:13:41 +00001700 UINTN Size;
jcarsey1cd45e72010-01-29 15:07:44 +00001701 CHAR16 *TempChar;
jcarsey125c2cf2009-11-18 21:36:50 +00001702
1703 RetVal = NULL;
1704
jcarseya405b862010-09-14 05:18:09 +00001705 //
1706 // First make sure its not an absolute path.
1707 //
1708 Status = ShellOpenFileByName(FileName, &Handle, EFI_FILE_MODE_READ, 0);
1709 if (!EFI_ERROR(Status)){
1710 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {
1711 ASSERT(RetVal == NULL);
1712 RetVal = StrnCatGrow(&RetVal, NULL, FileName, 0);
1713 ShellCloseFile(&Handle);
1714 return (RetVal);
1715 } else {
1716 ShellCloseFile(&Handle);
1717 }
1718 }
1719
jcarsey125c2cf2009-11-18 21:36:50 +00001720 Path = ShellGetEnvironmentVariable(L"cwd");
1721 if (Path != NULL) {
Qiu Shuminfbd2dfa2015-10-23 02:03:20 +00001722 Size = StrSize(Path) + sizeof(CHAR16);
jcarsey36a9d672009-11-20 21:13:41 +00001723 Size += StrSize(FileName);
1724 TestPath = AllocateZeroPool(Size);
jcarseyc9d92df2010-02-03 15:37:54 +00001725 if (TestPath == NULL) {
1726 return (NULL);
1727 }
Qiu Shumine75390f2015-06-30 03:18:31 +00001728 StrCpyS(TestPath, Size/sizeof(CHAR16), Path);
Qiu Shuminfbd2dfa2015-10-23 02:03:20 +00001729 StrCatS(TestPath, Size/sizeof(CHAR16), L"\\");
Qiu Shumine75390f2015-06-30 03:18:31 +00001730 StrCatS(TestPath, Size/sizeof(CHAR16), FileName);
jcarsey125c2cf2009-11-18 21:36:50 +00001731 Status = ShellOpenFileByName(TestPath, &Handle, EFI_FILE_MODE_READ, 0);
1732 if (!EFI_ERROR(Status)){
jcarseya405b862010-09-14 05:18:09 +00001733 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {
1734 ASSERT(RetVal == NULL);
1735 RetVal = StrnCatGrow(&RetVal, NULL, TestPath, 0);
1736 ShellCloseFile(&Handle);
1737 FreePool(TestPath);
1738 return (RetVal);
1739 } else {
1740 ShellCloseFile(&Handle);
1741 }
jcarsey125c2cf2009-11-18 21:36:50 +00001742 }
1743 FreePool(TestPath);
1744 }
1745 Path = ShellGetEnvironmentVariable(L"path");
1746 if (Path != NULL) {
jcarseya405b862010-09-14 05:18:09 +00001747 Size = StrSize(Path)+sizeof(CHAR16);
jcarsey36a9d672009-11-20 21:13:41 +00001748 Size += StrSize(FileName);
1749 TestPath = AllocateZeroPool(Size);
jcarsey3e082d52010-10-04 16:44:57 +00001750 if (TestPath == NULL) {
1751 return (NULL);
1752 }
jcarsey1e6e84c2010-01-25 20:05:08 +00001753 Walker = (CHAR16*)Path;
jcarsey125c2cf2009-11-18 21:36:50 +00001754 do {
1755 CopyMem(TestPath, Walker, StrSize(Walker));
jcarsey3e082d52010-10-04 16:44:57 +00001756 if (TestPath != NULL) {
1757 TempChar = StrStr(TestPath, L";");
1758 if (TempChar != NULL) {
1759 *TempChar = CHAR_NULL;
1760 }
1761 if (TestPath[StrLen(TestPath)-1] != L'\\') {
Qiu Shumine75390f2015-06-30 03:18:31 +00001762 StrCatS(TestPath, Size/sizeof(CHAR16), L"\\");
jcarsey3e082d52010-10-04 16:44:57 +00001763 }
jcarsey89e85372011-04-13 23:37:50 +00001764 if (FileName[0] == L'\\') {
1765 FileName++;
1766 }
Qiu Shumine75390f2015-06-30 03:18:31 +00001767 StrCatS(TestPath, Size/sizeof(CHAR16), FileName);
jcarsey3e082d52010-10-04 16:44:57 +00001768 if (StrStr(Walker, L";") != NULL) {
1769 Walker = StrStr(Walker, L";") + 1;
jcarseya405b862010-09-14 05:18:09 +00001770 } else {
jcarsey3e082d52010-10-04 16:44:57 +00001771 Walker = NULL;
1772 }
1773 Status = ShellOpenFileByName(TestPath, &Handle, EFI_FILE_MODE_READ, 0);
1774 if (!EFI_ERROR(Status)){
1775 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {
1776 ASSERT(RetVal == NULL);
1777 RetVal = StrnCatGrow(&RetVal, NULL, TestPath, 0);
1778 ShellCloseFile(&Handle);
1779 break;
1780 } else {
1781 ShellCloseFile(&Handle);
1782 }
jcarseya405b862010-09-14 05:18:09 +00001783 }
jcarsey125c2cf2009-11-18 21:36:50 +00001784 }
1785 } while (Walker != NULL && Walker[0] != CHAR_NULL);
1786 FreePool(TestPath);
1787 }
1788 return (RetVal);
1789}
1790
jcarseyb3011f42010-01-11 21:49:04 +00001791/**
jcarsey1e6e84c2010-01-25 20:05:08 +00001792 Find a file by searching the CWD and then the path with a variable set of file
1793 extensions. If the file is not found it will append each extension in the list
jcarseyb3011f42010-01-11 21:49:04 +00001794 in the order provided and return the first one that is successful.
1795
1796 If FileName is NULL, then ASSERT.
1797 If FileExtension is NULL, then behavior is identical to ShellFindFilePath.
1798
1799 If the return value is not NULL then the memory must be caller freed.
1800
1801 @param[in] FileName Filename string.
1802 @param[in] FileExtension Semi-colon delimeted list of possible extensions.
1803
1804 @retval NULL The file was not found.
1805 @retval !NULL The path to the file.
1806**/
1807CHAR16 *
1808EFIAPI
1809ShellFindFilePathEx (
1810 IN CONST CHAR16 *FileName,
1811 IN CONST CHAR16 *FileExtension
1812 )
1813{
1814 CHAR16 *TestPath;
1815 CHAR16 *RetVal;
1816 CONST CHAR16 *ExtensionWalker;
jcarsey9e926b62010-01-14 20:26:39 +00001817 UINTN Size;
jcarsey1cd45e72010-01-29 15:07:44 +00001818 CHAR16 *TempChar;
jcarseyc9d92df2010-02-03 15:37:54 +00001819 CHAR16 *TempChar2;
jcarsey1cd45e72010-01-29 15:07:44 +00001820
jcarseyb3011f42010-01-11 21:49:04 +00001821 ASSERT(FileName != NULL);
1822 if (FileExtension == NULL) {
1823 return (ShellFindFilePath(FileName));
1824 }
1825 RetVal = ShellFindFilePath(FileName);
1826 if (RetVal != NULL) {
1827 return (RetVal);
1828 }
jcarsey9e926b62010-01-14 20:26:39 +00001829 Size = StrSize(FileName);
1830 Size += StrSize(FileExtension);
1831 TestPath = AllocateZeroPool(Size);
jcarseyc9d92df2010-02-03 15:37:54 +00001832 if (TestPath == NULL) {
1833 return (NULL);
1834 }
jcarseya405b862010-09-14 05:18:09 +00001835 for (ExtensionWalker = FileExtension, TempChar2 = (CHAR16*)FileExtension; TempChar2 != NULL ; ExtensionWalker = TempChar2 + 1){
Qiu Shumine75390f2015-06-30 03:18:31 +00001836 StrCpyS(TestPath, Size/sizeof(CHAR16), FileName);
jcarseya405b862010-09-14 05:18:09 +00001837 if (ExtensionWalker != NULL) {
Qiu Shumine75390f2015-06-30 03:18:31 +00001838 StrCatS(TestPath, Size/sizeof(CHAR16), ExtensionWalker);
jcarseya405b862010-09-14 05:18:09 +00001839 }
jcarsey1cd45e72010-01-29 15:07:44 +00001840 TempChar = StrStr(TestPath, L";");
1841 if (TempChar != NULL) {
1842 *TempChar = CHAR_NULL;
jcarseyb3011f42010-01-11 21:49:04 +00001843 }
1844 RetVal = ShellFindFilePath(TestPath);
1845 if (RetVal != NULL) {
1846 break;
1847 }
jcarseya405b862010-09-14 05:18:09 +00001848 ASSERT(ExtensionWalker != NULL);
jcarseyc9d92df2010-02-03 15:37:54 +00001849 TempChar2 = StrStr(ExtensionWalker, L";");
jcarseyb3011f42010-01-11 21:49:04 +00001850 }
1851 FreePool(TestPath);
1852 return (RetVal);
1853}
1854
jcarsey94b17fa2009-05-07 18:46:18 +00001855typedef struct {
jcarsey9b3bf082009-06-23 21:15:07 +00001856 LIST_ENTRY Link;
jcarsey94b17fa2009-05-07 18:46:18 +00001857 CHAR16 *Name;
jcarseya405b862010-09-14 05:18:09 +00001858 SHELL_PARAM_TYPE Type;
jcarsey94b17fa2009-05-07 18:46:18 +00001859 CHAR16 *Value;
1860 UINTN OriginalPosition;
1861} SHELL_PARAM_PACKAGE;
1862
1863/**
jcarsey1e6e84c2010-01-25 20:05:08 +00001864 Checks the list of valid arguments and returns TRUE if the item was found. If the
jcarsey94b17fa2009-05-07 18:46:18 +00001865 return value is TRUE then the type parameter is set also.
jcarsey1e6e84c2010-01-25 20:05:08 +00001866
jcarsey94b17fa2009-05-07 18:46:18 +00001867 if CheckList is NULL then ASSERT();
1868 if Name is NULL then ASSERT();
1869 if Type is NULL then ASSERT();
1870
jcarsey94b17fa2009-05-07 18:46:18 +00001871 @param Name pointer to Name of parameter found
1872 @param CheckList List to check against
jcarseya405b862010-09-14 05:18:09 +00001873 @param Type pointer to type of parameter if it was found
jcarsey94b17fa2009-05-07 18:46:18 +00001874
1875 @retval TRUE the Parameter was found. Type is valid.
1876 @retval FALSE the Parameter was not found. Type is not valid.
1877**/
1878BOOLEAN
1879EFIAPI
jcarseyd2b45642009-05-11 18:02:16 +00001880InternalIsOnCheckList (
jcarsey94b17fa2009-05-07 18:46:18 +00001881 IN CONST CHAR16 *Name,
1882 IN CONST SHELL_PARAM_ITEM *CheckList,
jcarsey252d9452011-03-25 20:49:53 +00001883 OUT SHELL_PARAM_TYPE *Type
jcarseya405b862010-09-14 05:18:09 +00001884 )
1885{
jcarsey94b17fa2009-05-07 18:46:18 +00001886 SHELL_PARAM_ITEM *TempListItem;
jcarsey252d9452011-03-25 20:49:53 +00001887 CHAR16 *TempString;
jcarsey94b17fa2009-05-07 18:46:18 +00001888
1889 //
1890 // ASSERT that all 3 pointer parameters aren't NULL
1891 //
1892 ASSERT(CheckList != NULL);
1893 ASSERT(Type != NULL);
1894 ASSERT(Name != NULL);
1895
1896 //
jcarseyd2b45642009-05-11 18:02:16 +00001897 // question mark and page break mode are always supported
1898 //
1899 if ((StrCmp(Name, L"-?") == 0) ||
1900 (StrCmp(Name, L"-b") == 0)
jcarseya405b862010-09-14 05:18:09 +00001901 ) {
jcarsey252d9452011-03-25 20:49:53 +00001902 *Type = TypeFlag;
jcarseyd2b45642009-05-11 18:02:16 +00001903 return (TRUE);
1904 }
1905
1906 //
jcarsey94b17fa2009-05-07 18:46:18 +00001907 // Enumerate through the list
1908 //
1909 for (TempListItem = (SHELL_PARAM_ITEM*)CheckList ; TempListItem->Name != NULL ; TempListItem++) {
1910 //
jcarsey9eb53ac2009-07-08 17:26:58 +00001911 // If the Type is TypeStart only check the first characters of the passed in param
1912 // If it matches set the type and return TRUE
jcarsey94b17fa2009-05-07 18:46:18 +00001913 //
darylm503b0934ac2011-11-12 00:35:11 +00001914 if (TempListItem->Type == TypeStart) {
jcarsey252d9452011-03-25 20:49:53 +00001915 if (StrnCmp(Name, TempListItem->Name, StrLen(TempListItem->Name)) == 0) {
1916 *Type = TempListItem->Type;
1917 return (TRUE);
1918 }
1919 TempString = NULL;
1920 TempString = StrnCatGrow(&TempString, NULL, Name, StrLen(TempListItem->Name));
1921 if (TempString != NULL) {
1922 if (StringNoCaseCompare(&TempString, &TempListItem->Name) == 0) {
1923 *Type = TempListItem->Type;
1924 FreePool(TempString);
1925 return (TRUE);
1926 }
1927 FreePool(TempString);
1928 }
1929 } else if (StringNoCaseCompare(&Name, &TempListItem->Name) == 0) {
jcarsey94b17fa2009-05-07 18:46:18 +00001930 *Type = TempListItem->Type;
1931 return (TRUE);
1932 }
1933 }
jcarsey2247dde2009-11-09 18:08:58 +00001934
jcarsey94b17fa2009-05-07 18:46:18 +00001935 return (FALSE);
1936}
1937/**
jcarseyd2b45642009-05-11 18:02:16 +00001938 Checks the string for indicators of "flag" status. this is a leading '/', '-', or '+'
jcarsey94b17fa2009-05-07 18:46:18 +00001939
jcarseya405b862010-09-14 05:18:09 +00001940 @param[in] Name pointer to Name of parameter found
1941 @param[in] AlwaysAllowNumbers TRUE to allow numbers, FALSE to not.
jcarsey658bf432014-11-04 22:33:16 +00001942 @param[in] TimeNumbers TRUE to allow numbers with ":", FALSE otherwise.
jcarsey94b17fa2009-05-07 18:46:18 +00001943
1944 @retval TRUE the Parameter is a flag.
jcarseya405b862010-09-14 05:18:09 +00001945 @retval FALSE the Parameter not a flag.
jcarsey94b17fa2009-05-07 18:46:18 +00001946**/
1947BOOLEAN
1948EFIAPI
jcarseyd2b45642009-05-11 18:02:16 +00001949InternalIsFlag (
jcarsey2247dde2009-11-09 18:08:58 +00001950 IN CONST CHAR16 *Name,
jcarsey658bf432014-11-04 22:33:16 +00001951 IN CONST BOOLEAN AlwaysAllowNumbers,
1952 IN CONST BOOLEAN TimeNumbers
jcarsey94b17fa2009-05-07 18:46:18 +00001953 )
1954{
1955 //
1956 // ASSERT that Name isn't NULL
1957 //
1958 ASSERT(Name != NULL);
1959
1960 //
jcarsey2247dde2009-11-09 18:08:58 +00001961 // If we accept numbers then dont return TRUE. (they will be values)
1962 //
jcarsey658bf432014-11-04 22:33:16 +00001963 if (((Name[0] == L'-' || Name[0] == L'+') && InternalShellIsHexOrDecimalNumber(Name+1, FALSE, FALSE, TimeNumbers)) && AlwaysAllowNumbers) {
jcarsey2247dde2009-11-09 18:08:58 +00001964 return (FALSE);
1965 }
1966
1967 //
jcarseya405b862010-09-14 05:18:09 +00001968 // If the Name has a /, +, or - as the first character return TRUE
jcarsey94b17fa2009-05-07 18:46:18 +00001969 //
jcarsey1e6e84c2010-01-25 20:05:08 +00001970 if ((Name[0] == L'/') ||
jcarseyd2b45642009-05-11 18:02:16 +00001971 (Name[0] == L'-') ||
1972 (Name[0] == L'+')
jcarseya405b862010-09-14 05:18:09 +00001973 ) {
jcarsey94b17fa2009-05-07 18:46:18 +00001974 return (TRUE);
1975 }
1976 return (FALSE);
1977}
1978
1979/**
jcarsey1e6e84c2010-01-25 20:05:08 +00001980 Checks the command line arguments passed against the list of valid ones.
jcarsey94b17fa2009-05-07 18:46:18 +00001981
1982 If no initialization is required, then return RETURN_SUCCESS.
jcarsey1e6e84c2010-01-25 20:05:08 +00001983
jcarseya405b862010-09-14 05:18:09 +00001984 @param[in] CheckList pointer to list of parameters to check
1985 @param[out] CheckPackage pointer to pointer to list checked values
1986 @param[out] ProblemParam optional pointer to pointer to unicode string for
jcarseyd2b45642009-05-11 18:02:16 +00001987 the paramater that caused failure. If used then the
1988 caller is responsible for freeing the memory.
jcarseya405b862010-09-14 05:18:09 +00001989 @param[in] AutoPageBreak will automatically set PageBreakEnabled for "b" parameter
1990 @param[in] Argv pointer to array of parameters
1991 @param[in] Argc Count of parameters in Argv
1992 @param[in] AlwaysAllowNumbers TRUE to allow numbers always, FALSE otherwise.
jcarsey94b17fa2009-05-07 18:46:18 +00001993
1994 @retval EFI_SUCCESS The operation completed sucessfully.
1995 @retval EFI_OUT_OF_RESOURCES A memory allocation failed
1996 @retval EFI_INVALID_PARAMETER A parameter was invalid
jcarsey1e6e84c2010-01-25 20:05:08 +00001997 @retval EFI_VOLUME_CORRUPTED the command line was corrupt. an argument was
1998 duplicated. the duplicated command line argument
jcarsey94b17fa2009-05-07 18:46:18 +00001999 was returned in ProblemParam if provided.
jcarsey1e6e84c2010-01-25 20:05:08 +00002000 @retval EFI_NOT_FOUND a argument required a value that was missing.
jcarsey94b17fa2009-05-07 18:46:18 +00002001 the invalid command line argument was returned in
2002 ProblemParam if provided.
2003**/
2004EFI_STATUS
2005EFIAPI
2006InternalCommandLineParse (
2007 IN CONST SHELL_PARAM_ITEM *CheckList,
2008 OUT LIST_ENTRY **CheckPackage,
2009 OUT CHAR16 **ProblemParam OPTIONAL,
2010 IN BOOLEAN AutoPageBreak,
2011 IN CONST CHAR16 **Argv,
jcarsey2247dde2009-11-09 18:08:58 +00002012 IN UINTN Argc,
2013 IN BOOLEAN AlwaysAllowNumbers
jcarseya405b862010-09-14 05:18:09 +00002014 )
2015{
jcarsey94b17fa2009-05-07 18:46:18 +00002016 UINTN LoopCounter;
jcarsey252d9452011-03-25 20:49:53 +00002017 SHELL_PARAM_TYPE CurrentItemType;
jcarsey94b17fa2009-05-07 18:46:18 +00002018 SHELL_PARAM_PACKAGE *CurrentItemPackage;
jcarsey125c2cf2009-11-18 21:36:50 +00002019 UINTN GetItemValue;
2020 UINTN ValueSize;
jcarseya405b862010-09-14 05:18:09 +00002021 UINTN Count;
jcarsey252d9452011-03-25 20:49:53 +00002022 CONST CHAR16 *TempPointer;
Jaben Carsey98c16be2014-08-19 21:00:34 +00002023 UINTN CurrentValueSize;
Ruiyu Ni2efafab2016-07-13 17:39:37 +08002024 CHAR16 *NewValue;
jcarsey94b17fa2009-05-07 18:46:18 +00002025
2026 CurrentItemPackage = NULL;
jcarsey125c2cf2009-11-18 21:36:50 +00002027 GetItemValue = 0;
2028 ValueSize = 0;
jcarseya405b862010-09-14 05:18:09 +00002029 Count = 0;
jcarsey94b17fa2009-05-07 18:46:18 +00002030
2031 //
2032 // If there is only 1 item we dont need to do anything
2033 //
jcarseya405b862010-09-14 05:18:09 +00002034 if (Argc < 1) {
jcarsey94b17fa2009-05-07 18:46:18 +00002035 *CheckPackage = NULL;
2036 return (EFI_SUCCESS);
2037 }
2038
2039 //
jcarsey2247dde2009-11-09 18:08:58 +00002040 // ASSERTs
2041 //
2042 ASSERT(CheckList != NULL);
2043 ASSERT(Argv != NULL);
2044
2045 //
jcarsey94b17fa2009-05-07 18:46:18 +00002046 // initialize the linked list
2047 //
2048 *CheckPackage = (LIST_ENTRY*)AllocateZeroPool(sizeof(LIST_ENTRY));
sfu502a758c2011-10-08 02:55:30 +00002049 if (*CheckPackage == NULL) {
jcarseybeab0fc2011-10-10 17:26:25 +00002050 return (EFI_OUT_OF_RESOURCES);
sfu502a758c2011-10-08 02:55:30 +00002051 }
jcarseybeab0fc2011-10-10 17:26:25 +00002052
jcarsey94b17fa2009-05-07 18:46:18 +00002053 InitializeListHead(*CheckPackage);
2054
2055 //
2056 // loop through each of the arguments
2057 //
2058 for (LoopCounter = 0 ; LoopCounter < Argc ; ++LoopCounter) {
2059 if (Argv[LoopCounter] == NULL) {
2060 //
2061 // do nothing for NULL argv
2062 //
jcarseya405b862010-09-14 05:18:09 +00002063 } else if (InternalIsOnCheckList(Argv[LoopCounter], CheckList, &CurrentItemType)) {
jcarsey94b17fa2009-05-07 18:46:18 +00002064 //
jcarsey2247dde2009-11-09 18:08:58 +00002065 // We might have leftover if last parameter didnt have optional value
2066 //
jcarsey125c2cf2009-11-18 21:36:50 +00002067 if (GetItemValue != 0) {
2068 GetItemValue = 0;
jcarsey2247dde2009-11-09 18:08:58 +00002069 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
2070 }
2071 //
jcarsey94b17fa2009-05-07 18:46:18 +00002072 // this is a flag
2073 //
jcarsey252d9452011-03-25 20:49:53 +00002074 CurrentItemPackage = AllocateZeroPool(sizeof(SHELL_PARAM_PACKAGE));
jcarseybeab0fc2011-10-10 17:26:25 +00002075 if (CurrentItemPackage == NULL) {
2076 ShellCommandLineFreeVarList(*CheckPackage);
2077 *CheckPackage = NULL;
2078 return (EFI_OUT_OF_RESOURCES);
2079 }
Jaben Carsey98c16be2014-08-19 21:00:34 +00002080 CurrentItemPackage->Name = AllocateCopyPool(StrSize(Argv[LoopCounter]), Argv[LoopCounter]);
jcarseybeab0fc2011-10-10 17:26:25 +00002081 if (CurrentItemPackage->Name == NULL) {
2082 ShellCommandLineFreeVarList(*CheckPackage);
2083 *CheckPackage = NULL;
2084 return (EFI_OUT_OF_RESOURCES);
2085 }
jcarsey94b17fa2009-05-07 18:46:18 +00002086 CurrentItemPackage->Type = CurrentItemType;
2087 CurrentItemPackage->OriginalPosition = (UINTN)(-1);
jcarseyb1f95a02009-06-16 00:23:19 +00002088 CurrentItemPackage->Value = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +00002089
2090 //
2091 // Does this flag require a value
2092 //
jcarsey125c2cf2009-11-18 21:36:50 +00002093 switch (CurrentItemPackage->Type) {
jcarsey94b17fa2009-05-07 18:46:18 +00002094 //
jcarsey125c2cf2009-11-18 21:36:50 +00002095 // possibly trigger the next loop(s) to populate the value of this item
jcarsey1e6e84c2010-01-25 20:05:08 +00002096 //
jcarsey125c2cf2009-11-18 21:36:50 +00002097 case TypeValue:
jcarsey658bf432014-11-04 22:33:16 +00002098 case TypeTimeValue:
jcarsey1e6e84c2010-01-25 20:05:08 +00002099 GetItemValue = 1;
jcarsey125c2cf2009-11-18 21:36:50 +00002100 ValueSize = 0;
2101 break;
2102 case TypeDoubleValue:
2103 GetItemValue = 2;
2104 ValueSize = 0;
2105 break;
2106 case TypeMaxValue:
2107 GetItemValue = (UINTN)(-1);
2108 ValueSize = 0;
2109 break;
2110 default:
2111 //
2112 // this item has no value expected; we are done
2113 //
2114 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
2115 ASSERT(GetItemValue == 0);
2116 break;
jcarsey94b17fa2009-05-07 18:46:18 +00002117 }
Qiu Shuminaf7a3a52015-03-13 02:04:17 +00002118 } else if (GetItemValue != 0 && CurrentItemPackage != NULL && !InternalIsFlag(Argv[LoopCounter], AlwaysAllowNumbers, (BOOLEAN)(CurrentItemPackage->Type == TypeTimeValue))) {
jcarseyb1f95a02009-06-16 00:23:19 +00002119 //
jcarsey125c2cf2009-11-18 21:36:50 +00002120 // get the item VALUE for a previous flag
jcarseyb1f95a02009-06-16 00:23:19 +00002121 //
Qiu Shumin484dd082015-04-01 00:49:05 +00002122 CurrentValueSize = ValueSize + StrSize(Argv[LoopCounter]) + sizeof(CHAR16);
Ruiyu Ni2efafab2016-07-13 17:39:37 +08002123 NewValue = ReallocatePool(ValueSize, CurrentValueSize, CurrentItemPackage->Value);
2124 if (NewValue == NULL) {
2125 SHELL_FREE_NON_NULL (CurrentItemPackage->Value);
2126 SHELL_FREE_NON_NULL (CurrentItemPackage);
2127 ShellCommandLineFreeVarList (*CheckPackage);
2128 *CheckPackage = NULL;
2129 return EFI_OUT_OF_RESOURCES;
2130 }
2131 CurrentItemPackage->Value = NewValue;
Qiu Shumin484dd082015-04-01 00:49:05 +00002132 if (ValueSize == 0) {
Qiu Shumine75390f2015-06-30 03:18:31 +00002133 StrCpyS( CurrentItemPackage->Value,
2134 CurrentValueSize/sizeof(CHAR16),
2135 Argv[LoopCounter]
2136 );
jcarsey125c2cf2009-11-18 21:36:50 +00002137 } else {
Qiu Shumine75390f2015-06-30 03:18:31 +00002138 StrCatS( CurrentItemPackage->Value,
2139 CurrentValueSize/sizeof(CHAR16),
2140 L" "
2141 );
2142 StrCatS( CurrentItemPackage->Value,
2143 CurrentValueSize/sizeof(CHAR16),
2144 Argv[LoopCounter]
2145 );
jcarsey125c2cf2009-11-18 21:36:50 +00002146 }
Qiu Shumin484dd082015-04-01 00:49:05 +00002147 ValueSize += StrSize(Argv[LoopCounter]) + sizeof(CHAR16);
2148
jcarsey125c2cf2009-11-18 21:36:50 +00002149 GetItemValue--;
2150 if (GetItemValue == 0) {
2151 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
2152 }
jcarsey658bf432014-11-04 22:33:16 +00002153 } else if (!InternalIsFlag(Argv[LoopCounter], AlwaysAllowNumbers, FALSE)){
jcarseyb1f95a02009-06-16 00:23:19 +00002154 //
2155 // add this one as a non-flag
2156 //
jcarsey252d9452011-03-25 20:49:53 +00002157
2158 TempPointer = Argv[LoopCounter];
darylm503b0934ac2011-11-12 00:35:11 +00002159 if ((*TempPointer == L'^' && *(TempPointer+1) == L'-')
jcarsey252d9452011-03-25 20:49:53 +00002160 || (*TempPointer == L'^' && *(TempPointer+1) == L'/')
2161 || (*TempPointer == L'^' && *(TempPointer+1) == L'+')
2162 ){
2163 TempPointer++;
2164 }
2165 CurrentItemPackage = AllocateZeroPool(sizeof(SHELL_PARAM_PACKAGE));
jcarseybeab0fc2011-10-10 17:26:25 +00002166 if (CurrentItemPackage == NULL) {
2167 ShellCommandLineFreeVarList(*CheckPackage);
2168 *CheckPackage = NULL;
2169 return (EFI_OUT_OF_RESOURCES);
2170 }
jcarseyb1f95a02009-06-16 00:23:19 +00002171 CurrentItemPackage->Name = NULL;
2172 CurrentItemPackage->Type = TypePosition;
Jaben Carsey98c16be2014-08-19 21:00:34 +00002173 CurrentItemPackage->Value = AllocateCopyPool(StrSize(TempPointer), TempPointer);
jcarseybeab0fc2011-10-10 17:26:25 +00002174 if (CurrentItemPackage->Value == NULL) {
2175 ShellCommandLineFreeVarList(*CheckPackage);
2176 *CheckPackage = NULL;
2177 return (EFI_OUT_OF_RESOURCES);
2178 }
jcarseya405b862010-09-14 05:18:09 +00002179 CurrentItemPackage->OriginalPosition = Count++;
jcarsey9b3bf082009-06-23 21:15:07 +00002180 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
jcarsey252d9452011-03-25 20:49:53 +00002181 } else {
jcarsey94b17fa2009-05-07 18:46:18 +00002182 //
2183 // this was a non-recognised flag... error!
2184 //
jcarsey252d9452011-03-25 20:49:53 +00002185 if (ProblemParam != NULL) {
Jaben Carsey98c16be2014-08-19 21:00:34 +00002186 *ProblemParam = AllocateCopyPool(StrSize(Argv[LoopCounter]), Argv[LoopCounter]);
jcarsey252d9452011-03-25 20:49:53 +00002187 }
jcarsey94b17fa2009-05-07 18:46:18 +00002188 ShellCommandLineFreeVarList(*CheckPackage);
2189 *CheckPackage = NULL;
2190 return (EFI_VOLUME_CORRUPTED);
jcarsey94b17fa2009-05-07 18:46:18 +00002191 }
2192 }
jcarsey125c2cf2009-11-18 21:36:50 +00002193 if (GetItemValue != 0) {
2194 GetItemValue = 0;
2195 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
2196 }
jcarsey94b17fa2009-05-07 18:46:18 +00002197 //
2198 // support for AutoPageBreak
2199 //
2200 if (AutoPageBreak && ShellCommandLineGetFlag(*CheckPackage, L"-b")) {
2201 ShellSetPageBreakMode(TRUE);
2202 }
2203 return (EFI_SUCCESS);
2204}
2205
2206/**
jcarsey1e6e84c2010-01-25 20:05:08 +00002207 Checks the command line arguments passed against the list of valid ones.
jcarsey94b17fa2009-05-07 18:46:18 +00002208 Optionally removes NULL values first.
jcarsey1e6e84c2010-01-25 20:05:08 +00002209
jcarsey94b17fa2009-05-07 18:46:18 +00002210 If no initialization is required, then return RETURN_SUCCESS.
jcarsey1e6e84c2010-01-25 20:05:08 +00002211
jcarseya405b862010-09-14 05:18:09 +00002212 @param[in] CheckList The pointer to list of parameters to check.
2213 @param[out] CheckPackage The package of checked values.
2214 @param[out] ProblemParam Optional pointer to pointer to unicode string for
jcarsey94b17fa2009-05-07 18:46:18 +00002215 the paramater that caused failure.
jcarseya405b862010-09-14 05:18:09 +00002216 @param[in] AutoPageBreak Will automatically set PageBreakEnabled.
2217 @param[in] AlwaysAllowNumbers Will never fail for number based flags.
jcarsey94b17fa2009-05-07 18:46:18 +00002218
2219 @retval EFI_SUCCESS The operation completed sucessfully.
jcarseya405b862010-09-14 05:18:09 +00002220 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
2221 @retval EFI_INVALID_PARAMETER A parameter was invalid.
2222 @retval EFI_VOLUME_CORRUPTED The command line was corrupt.
2223 @retval EFI_DEVICE_ERROR The commands contained 2 opposing arguments. One
jcarsey1e6e84c2010-01-25 20:05:08 +00002224 of the command line arguments was returned in
jcarsey94b17fa2009-05-07 18:46:18 +00002225 ProblemParam if provided.
jcarseya405b862010-09-14 05:18:09 +00002226 @retval EFI_NOT_FOUND A argument required a value that was missing.
2227 The invalid command line argument was returned in
jcarsey94b17fa2009-05-07 18:46:18 +00002228 ProblemParam if provided.
2229**/
2230EFI_STATUS
2231EFIAPI
jcarsey2247dde2009-11-09 18:08:58 +00002232ShellCommandLineParseEx (
jcarsey94b17fa2009-05-07 18:46:18 +00002233 IN CONST SHELL_PARAM_ITEM *CheckList,
2234 OUT LIST_ENTRY **CheckPackage,
2235 OUT CHAR16 **ProblemParam OPTIONAL,
jcarsey2247dde2009-11-09 18:08:58 +00002236 IN BOOLEAN AutoPageBreak,
2237 IN BOOLEAN AlwaysAllowNumbers
jcarseya405b862010-09-14 05:18:09 +00002238 )
2239{
jcarsey1e6e84c2010-01-25 20:05:08 +00002240 //
jcarsey94b17fa2009-05-07 18:46:18 +00002241 // ASSERT that CheckList and CheckPackage aren't NULL
2242 //
2243 ASSERT(CheckList != NULL);
2244 ASSERT(CheckPackage != NULL);
2245
jcarsey1e6e84c2010-01-25 20:05:08 +00002246 //
jcarsey94b17fa2009-05-07 18:46:18 +00002247 // Check for UEFI Shell 2.0 protocols
2248 //
jcarsey366f81a2011-06-27 21:04:22 +00002249 if (gEfiShellParametersProtocol != NULL) {
jcarsey1e6e84c2010-01-25 20:05:08 +00002250 return (InternalCommandLineParse(CheckList,
2251 CheckPackage,
2252 ProblemParam,
2253 AutoPageBreak,
jcarsey366f81a2011-06-27 21:04:22 +00002254 (CONST CHAR16**) gEfiShellParametersProtocol->Argv,
2255 gEfiShellParametersProtocol->Argc,
jcarsey2247dde2009-11-09 18:08:58 +00002256 AlwaysAllowNumbers));
jcarsey94b17fa2009-05-07 18:46:18 +00002257 }
2258
jcarsey1e6e84c2010-01-25 20:05:08 +00002259 //
jcarsey94b17fa2009-05-07 18:46:18 +00002260 // ASSERT That EFI Shell is not required
2261 //
2262 ASSERT (mEfiShellInterface != NULL);
jcarsey1e6e84c2010-01-25 20:05:08 +00002263 return (InternalCommandLineParse(CheckList,
2264 CheckPackage,
2265 ProblemParam,
2266 AutoPageBreak,
jljusten08d7f8e2009-06-15 18:42:13 +00002267 (CONST CHAR16**) mEfiShellInterface->Argv,
jcarsey2247dde2009-11-09 18:08:58 +00002268 mEfiShellInterface->Argc,
2269 AlwaysAllowNumbers));
jcarsey94b17fa2009-05-07 18:46:18 +00002270}
2271
2272/**
2273 Frees shell variable list that was returned from ShellCommandLineParse.
2274
2275 This function will free all the memory that was used for the CheckPackage
2276 list of postprocessed shell arguments.
2277
2278 this function has no return value.
2279
2280 if CheckPackage is NULL, then return
2281
2282 @param CheckPackage the list to de-allocate
2283 **/
2284VOID
2285EFIAPI
2286ShellCommandLineFreeVarList (
2287 IN LIST_ENTRY *CheckPackage
jcarseya405b862010-09-14 05:18:09 +00002288 )
2289{
jcarsey94b17fa2009-05-07 18:46:18 +00002290 LIST_ENTRY *Node;
2291
2292 //
2293 // check for CheckPackage == NULL
2294 //
2295 if (CheckPackage == NULL) {
2296 return;
2297 }
2298
2299 //
2300 // for each node in the list
2301 //
jcarsey9eb53ac2009-07-08 17:26:58 +00002302 for ( Node = GetFirstNode(CheckPackage)
jcarseya405b862010-09-14 05:18:09 +00002303 ; !IsListEmpty(CheckPackage)
jcarsey9eb53ac2009-07-08 17:26:58 +00002304 ; Node = GetFirstNode(CheckPackage)
jcarseya405b862010-09-14 05:18:09 +00002305 ){
jcarsey94b17fa2009-05-07 18:46:18 +00002306 //
2307 // Remove it from the list
2308 //
2309 RemoveEntryList(Node);
2310
2311 //
2312 // if it has a name free the name
2313 //
2314 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {
2315 FreePool(((SHELL_PARAM_PACKAGE*)Node)->Name);
2316 }
2317
2318 //
2319 // if it has a value free the value
2320 //
2321 if (((SHELL_PARAM_PACKAGE*)Node)->Value != NULL) {
2322 FreePool(((SHELL_PARAM_PACKAGE*)Node)->Value);
2323 }
jcarsey1e6e84c2010-01-25 20:05:08 +00002324
jcarsey94b17fa2009-05-07 18:46:18 +00002325 //
2326 // free the node structure
2327 //
2328 FreePool((SHELL_PARAM_PACKAGE*)Node);
2329 }
2330 //
2331 // free the list head node
2332 //
2333 FreePool(CheckPackage);
2334}
2335/**
2336 Checks for presence of a flag parameter
2337
2338 flag arguments are in the form of "-<Key>" or "/<Key>", but do not have a value following the key
2339
2340 if CheckPackage is NULL then return FALSE.
2341 if KeyString is NULL then ASSERT()
jcarsey1e6e84c2010-01-25 20:05:08 +00002342
jcarsey94b17fa2009-05-07 18:46:18 +00002343 @param CheckPackage The package of parsed command line arguments
2344 @param KeyString the Key of the command line argument to check for
2345
2346 @retval TRUE the flag is on the command line
2347 @retval FALSE the flag is not on the command line
2348 **/
2349BOOLEAN
2350EFIAPI
2351ShellCommandLineGetFlag (
jcarseya405b862010-09-14 05:18:09 +00002352 IN CONST LIST_ENTRY * CONST CheckPackage,
2353 IN CONST CHAR16 * CONST KeyString
2354 )
2355{
jcarsey94b17fa2009-05-07 18:46:18 +00002356 LIST_ENTRY *Node;
jcarsey252d9452011-03-25 20:49:53 +00002357 CHAR16 *TempString;
jcarsey94b17fa2009-05-07 18:46:18 +00002358
2359 //
ydong100c1950b2011-10-13 02:37:35 +00002360 // return FALSE for no package or KeyString is NULL
jcarsey94b17fa2009-05-07 18:46:18 +00002361 //
ydong100c1950b2011-10-13 02:37:35 +00002362 if (CheckPackage == NULL || KeyString == NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00002363 return (FALSE);
2364 }
2365
2366 //
2367 // enumerate through the list of parametrs
2368 //
jcarsey1e6e84c2010-01-25 20:05:08 +00002369 for ( Node = GetFirstNode(CheckPackage)
2370 ; !IsNull (CheckPackage, Node)
2371 ; Node = GetNextNode(CheckPackage, Node)
jcarsey252d9452011-03-25 20:49:53 +00002372 ){
jcarsey94b17fa2009-05-07 18:46:18 +00002373 //
2374 // If the Name matches, return TRUE (and there may be NULL name)
2375 //
2376 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {
jcarsey9eb53ac2009-07-08 17:26:58 +00002377 //
2378 // If Type is TypeStart then only compare the begining of the strings
2379 //
jcarsey252d9452011-03-25 20:49:53 +00002380 if (((SHELL_PARAM_PACKAGE*)Node)->Type == TypeStart) {
2381 if (StrnCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name, StrLen(KeyString)) == 0) {
2382 return (TRUE);
2383 }
2384 TempString = NULL;
2385 TempString = StrnCatGrow(&TempString, NULL, KeyString, StrLen(((SHELL_PARAM_PACKAGE*)Node)->Name));
2386 if (TempString != NULL) {
2387 if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {
2388 FreePool(TempString);
2389 return (TRUE);
2390 }
2391 FreePool(TempString);
2392 }
2393 } else if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {
jcarsey94b17fa2009-05-07 18:46:18 +00002394 return (TRUE);
2395 }
2396 }
2397 }
2398 return (FALSE);
2399}
2400/**
jcarseya405b862010-09-14 05:18:09 +00002401 Returns value from command line argument.
jcarsey94b17fa2009-05-07 18:46:18 +00002402
jcarseya405b862010-09-14 05:18:09 +00002403 Value parameters are in the form of "-<Key> value" or "/<Key> value".
jcarsey1e6e84c2010-01-25 20:05:08 +00002404
jcarseya405b862010-09-14 05:18:09 +00002405 If CheckPackage is NULL, then return NULL.
jcarsey94b17fa2009-05-07 18:46:18 +00002406
jcarseya405b862010-09-14 05:18:09 +00002407 @param[in] CheckPackage The package of parsed command line arguments.
2408 @param[in] KeyString The Key of the command line argument to check for.
jcarsey94b17fa2009-05-07 18:46:18 +00002409
jcarseya405b862010-09-14 05:18:09 +00002410 @retval NULL The flag is not on the command line.
2411 @retval !=NULL The pointer to unicode string of the value.
2412**/
jcarsey94b17fa2009-05-07 18:46:18 +00002413CONST CHAR16*
2414EFIAPI
2415ShellCommandLineGetValue (
2416 IN CONST LIST_ENTRY *CheckPackage,
2417 IN CHAR16 *KeyString
jcarseya405b862010-09-14 05:18:09 +00002418 )
2419{
jcarsey94b17fa2009-05-07 18:46:18 +00002420 LIST_ENTRY *Node;
jcarsey252d9452011-03-25 20:49:53 +00002421 CHAR16 *TempString;
jcarsey94b17fa2009-05-07 18:46:18 +00002422
2423 //
ydong100c1950b2011-10-13 02:37:35 +00002424 // return NULL for no package or KeyString is NULL
jcarsey94b17fa2009-05-07 18:46:18 +00002425 //
ydong100c1950b2011-10-13 02:37:35 +00002426 if (CheckPackage == NULL || KeyString == NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00002427 return (NULL);
2428 }
2429
2430 //
2431 // enumerate through the list of parametrs
2432 //
jcarsey1e6e84c2010-01-25 20:05:08 +00002433 for ( Node = GetFirstNode(CheckPackage)
2434 ; !IsNull (CheckPackage, Node)
2435 ; Node = GetNextNode(CheckPackage, Node)
jcarsey252d9452011-03-25 20:49:53 +00002436 ){
jcarsey94b17fa2009-05-07 18:46:18 +00002437 //
jcarsey252d9452011-03-25 20:49:53 +00002438 // If the Name matches, return TRUE (and there may be NULL name)
jcarsey94b17fa2009-05-07 18:46:18 +00002439 //
2440 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {
jcarsey9eb53ac2009-07-08 17:26:58 +00002441 //
2442 // If Type is TypeStart then only compare the begining of the strings
2443 //
jcarsey252d9452011-03-25 20:49:53 +00002444 if (((SHELL_PARAM_PACKAGE*)Node)->Type == TypeStart) {
2445 if (StrnCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name, StrLen(KeyString)) == 0) {
2446 return (((SHELL_PARAM_PACKAGE*)Node)->Name + StrLen(KeyString));
2447 }
2448 TempString = NULL;
2449 TempString = StrnCatGrow(&TempString, NULL, KeyString, StrLen(((SHELL_PARAM_PACKAGE*)Node)->Name));
2450 if (TempString != NULL) {
2451 if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {
2452 FreePool(TempString);
2453 return (((SHELL_PARAM_PACKAGE*)Node)->Name + StrLen(KeyString));
2454 }
2455 FreePool(TempString);
2456 }
2457 } else if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {
jcarsey94b17fa2009-05-07 18:46:18 +00002458 return (((SHELL_PARAM_PACKAGE*)Node)->Value);
2459 }
2460 }
2461 }
2462 return (NULL);
2463}
jcarseya405b862010-09-14 05:18:09 +00002464
jcarsey94b17fa2009-05-07 18:46:18 +00002465/**
jcarseya405b862010-09-14 05:18:09 +00002466 Returns raw value from command line argument.
jcarsey94b17fa2009-05-07 18:46:18 +00002467
jcarseya405b862010-09-14 05:18:09 +00002468 Raw value parameters are in the form of "value" in a specific position in the list.
jcarsey1e6e84c2010-01-25 20:05:08 +00002469
jcarseya405b862010-09-14 05:18:09 +00002470 If CheckPackage is NULL, then return NULL.
jcarsey94b17fa2009-05-07 18:46:18 +00002471
jcarseya405b862010-09-14 05:18:09 +00002472 @param[in] CheckPackage The package of parsed command line arguments.
2473 @param[in] Position The position of the value.
jcarsey94b17fa2009-05-07 18:46:18 +00002474
jcarseya405b862010-09-14 05:18:09 +00002475 @retval NULL The flag is not on the command line.
2476 @retval !=NULL The pointer to unicode string of the value.
jcarsey94b17fa2009-05-07 18:46:18 +00002477 **/
2478CONST CHAR16*
2479EFIAPI
2480ShellCommandLineGetRawValue (
jcarseya405b862010-09-14 05:18:09 +00002481 IN CONST LIST_ENTRY * CONST CheckPackage,
2482 IN UINTN Position
2483 )
2484{
jcarsey94b17fa2009-05-07 18:46:18 +00002485 LIST_ENTRY *Node;
2486
2487 //
2488 // check for CheckPackage == NULL
2489 //
2490 if (CheckPackage == NULL) {
2491 return (NULL);
2492 }
2493
2494 //
2495 // enumerate through the list of parametrs
2496 //
jcarsey1e6e84c2010-01-25 20:05:08 +00002497 for ( Node = GetFirstNode(CheckPackage)
2498 ; !IsNull (CheckPackage, Node)
2499 ; Node = GetNextNode(CheckPackage, Node)
jcarseya405b862010-09-14 05:18:09 +00002500 ){
jcarsey94b17fa2009-05-07 18:46:18 +00002501 //
2502 // If the position matches, return the value
2503 //
2504 if (((SHELL_PARAM_PACKAGE*)Node)->OriginalPosition == Position) {
2505 return (((SHELL_PARAM_PACKAGE*)Node)->Value);
2506 }
2507 }
2508 return (NULL);
jcarseyb1f95a02009-06-16 00:23:19 +00002509}
jcarsey2247dde2009-11-09 18:08:58 +00002510
2511/**
jcarsey1e6e84c2010-01-25 20:05:08 +00002512 returns the number of command line value parameters that were parsed.
2513
jcarsey2247dde2009-11-09 18:08:58 +00002514 this will not include flags.
2515
jcarseya405b862010-09-14 05:18:09 +00002516 @param[in] CheckPackage The package of parsed command line arguments.
2517
jcarsey2247dde2009-11-09 18:08:58 +00002518 @retval (UINTN)-1 No parsing has ocurred
2519 @return other The number of value parameters found
2520**/
2521UINTN
2522EFIAPI
2523ShellCommandLineGetCount(
jcarseya405b862010-09-14 05:18:09 +00002524 IN CONST LIST_ENTRY *CheckPackage
jcarsey125c2cf2009-11-18 21:36:50 +00002525 )
2526{
jcarseya405b862010-09-14 05:18:09 +00002527 LIST_ENTRY *Node1;
2528 UINTN Count;
2529
2530 if (CheckPackage == NULL) {
2531 return (0);
2532 }
2533 for ( Node1 = GetFirstNode(CheckPackage), Count = 0
2534 ; !IsNull (CheckPackage, Node1)
2535 ; Node1 = GetNextNode(CheckPackage, Node1)
2536 ){
2537 if (((SHELL_PARAM_PACKAGE*)Node1)->Name == NULL) {
2538 Count++;
2539 }
2540 }
2541 return (Count);
jcarsey2247dde2009-11-09 18:08:58 +00002542}
2543
jcarsey975136a2009-06-16 19:03:54 +00002544/**
Bruce Crancceb4eb2015-07-08 01:54:46 +00002545 Determines if a parameter is duplicated.
jcarsey36a9d672009-11-20 21:13:41 +00002546
jcarsey1e6e84c2010-01-25 20:05:08 +00002547 If Param is not NULL then it will point to a callee allocated string buffer
jcarsey36a9d672009-11-20 21:13:41 +00002548 with the parameter value if a duplicate is found.
2549
2550 If CheckPackage is NULL, then ASSERT.
2551
2552 @param[in] CheckPackage The package of parsed command line arguments.
2553 @param[out] Param Upon finding one, a pointer to the duplicated parameter.
2554
2555 @retval EFI_SUCCESS No parameters were duplicated.
2556 @retval EFI_DEVICE_ERROR A duplicate was found.
2557 **/
2558EFI_STATUS
2559EFIAPI
2560ShellCommandLineCheckDuplicate (
2561 IN CONST LIST_ENTRY *CheckPackage,
2562 OUT CHAR16 **Param
2563 )
2564{
2565 LIST_ENTRY *Node1;
2566 LIST_ENTRY *Node2;
jcarsey1e6e84c2010-01-25 20:05:08 +00002567
jcarsey36a9d672009-11-20 21:13:41 +00002568 ASSERT(CheckPackage != NULL);
2569
jcarsey1e6e84c2010-01-25 20:05:08 +00002570 for ( Node1 = GetFirstNode(CheckPackage)
2571 ; !IsNull (CheckPackage, Node1)
2572 ; Node1 = GetNextNode(CheckPackage, Node1)
jcarseya405b862010-09-14 05:18:09 +00002573 ){
jcarsey1e6e84c2010-01-25 20:05:08 +00002574 for ( Node2 = GetNextNode(CheckPackage, Node1)
2575 ; !IsNull (CheckPackage, Node2)
2576 ; Node2 = GetNextNode(CheckPackage, Node2)
jcarseya405b862010-09-14 05:18:09 +00002577 ){
2578 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 +00002579 if (Param != NULL) {
2580 *Param = NULL;
2581 *Param = StrnCatGrow(Param, NULL, ((SHELL_PARAM_PACKAGE*)Node1)->Name, 0);
2582 }
2583 return (EFI_DEVICE_ERROR);
2584 }
2585 }
2586 }
2587 return (EFI_SUCCESS);
2588}
2589
2590/**
jcarsey1e6e84c2010-01-25 20:05:08 +00002591 This is a find and replace function. Upon successful return the NewString is a copy of
jcarsey975136a2009-06-16 19:03:54 +00002592 SourceString with each instance of FindTarget replaced with ReplaceWith.
2593
jcarseyb3011f42010-01-11 21:49:04 +00002594 If SourceString and NewString overlap the behavior is undefined.
2595
jcarsey975136a2009-06-16 19:03:54 +00002596 If the string would grow bigger than NewSize it will halt and return error.
2597
ydong104ff7e372011-09-02 08:05:34 +00002598 @param[in] SourceString The string with source buffer.
2599 @param[in, out] NewString The string with resultant buffer.
2600 @param[in] NewSize The size in bytes of NewString.
2601 @param[in] FindTarget The string to look for.
2602 @param[in] ReplaceWith The string to replace FindTarget with.
2603 @param[in] SkipPreCarrot If TRUE will skip a FindTarget that has a '^'
2604 immediately before it.
2605 @param[in] ParameterReplacing If TRUE will add "" around items with spaces.
jcarsey975136a2009-06-16 19:03:54 +00002606
jcarsey969c7832010-01-13 16:46:33 +00002607 @retval EFI_INVALID_PARAMETER SourceString was NULL.
2608 @retval EFI_INVALID_PARAMETER NewString was NULL.
2609 @retval EFI_INVALID_PARAMETER FindTarget was NULL.
2610 @retval EFI_INVALID_PARAMETER ReplaceWith was NULL.
2611 @retval EFI_INVALID_PARAMETER FindTarget had length < 1.
2612 @retval EFI_INVALID_PARAMETER SourceString had length < 1.
jcarsey1e6e84c2010-01-25 20:05:08 +00002613 @retval EFI_BUFFER_TOO_SMALL NewSize was less than the minimum size to hold
jcarsey969c7832010-01-13 16:46:33 +00002614 the new string (truncation occurred).
jcarseya405b862010-09-14 05:18:09 +00002615 @retval EFI_SUCCESS The string was successfully copied with replacement.
jcarsey975136a2009-06-16 19:03:54 +00002616**/
jcarsey975136a2009-06-16 19:03:54 +00002617EFI_STATUS
2618EFIAPI
jcarseya405b862010-09-14 05:18:09 +00002619ShellCopySearchAndReplace(
jcarsey975136a2009-06-16 19:03:54 +00002620 IN CHAR16 CONST *SourceString,
jcarseya405b862010-09-14 05:18:09 +00002621 IN OUT CHAR16 *NewString,
jcarsey975136a2009-06-16 19:03:54 +00002622 IN UINTN NewSize,
2623 IN CONST CHAR16 *FindTarget,
jcarsey969c7832010-01-13 16:46:33 +00002624 IN CONST CHAR16 *ReplaceWith,
jcarseya405b862010-09-14 05:18:09 +00002625 IN CONST BOOLEAN SkipPreCarrot,
2626 IN CONST BOOLEAN ParameterReplacing
jcarsey1e6e84c2010-01-25 20:05:08 +00002627 )
jcarsey2247dde2009-11-09 18:08:58 +00002628{
jcarsey01582942009-07-10 19:46:17 +00002629 UINTN Size;
jcarseya405b862010-09-14 05:18:09 +00002630 CHAR16 *Replace;
2631
jcarsey975136a2009-06-16 19:03:54 +00002632 if ( (SourceString == NULL)
2633 || (NewString == NULL)
2634 || (FindTarget == NULL)
2635 || (ReplaceWith == NULL)
2636 || (StrLen(FindTarget) < 1)
2637 || (StrLen(SourceString) < 1)
jcarseya405b862010-09-14 05:18:09 +00002638 ){
jcarsey975136a2009-06-16 19:03:54 +00002639 return (EFI_INVALID_PARAMETER);
2640 }
jcarseya405b862010-09-14 05:18:09 +00002641 Replace = NULL;
2642 if (StrStr(ReplaceWith, L" ") == NULL || !ParameterReplacing) {
2643 Replace = StrnCatGrow(&Replace, NULL, ReplaceWith, 0);
2644 } else {
2645 Replace = AllocateZeroPool(StrSize(ReplaceWith) + 2*sizeof(CHAR16));
jcarseybeab0fc2011-10-10 17:26:25 +00002646 if (Replace != NULL) {
2647 UnicodeSPrint(Replace, StrSize(ReplaceWith) + 2*sizeof(CHAR16), L"\"%s\"", ReplaceWith);
2648 }
jcarseya405b862010-09-14 05:18:09 +00002649 }
jcarsey3e082d52010-10-04 16:44:57 +00002650 if (Replace == NULL) {
2651 return (EFI_OUT_OF_RESOURCES);
2652 }
Jaben Carsey98c16be2014-08-19 21:00:34 +00002653 NewString = ZeroMem(NewString, NewSize);
jcarsey2247dde2009-11-09 18:08:58 +00002654 while (*SourceString != CHAR_NULL) {
jcarsey969c7832010-01-13 16:46:33 +00002655 //
jcarseya405b862010-09-14 05:18:09 +00002656 // if we find the FindTarget and either Skip == FALSE or Skip and we
jcarsey969c7832010-01-13 16:46:33 +00002657 // dont have a carrot do a replace...
2658 //
jcarsey1e6e84c2010-01-25 20:05:08 +00002659 if (StrnCmp(SourceString, FindTarget, StrLen(FindTarget)) == 0
jcarseya405b862010-09-14 05:18:09 +00002660 && ((SkipPreCarrot && *(SourceString-1) != L'^') || !SkipPreCarrot)
2661 ){
jcarsey975136a2009-06-16 19:03:54 +00002662 SourceString += StrLen(FindTarget);
jcarsey01582942009-07-10 19:46:17 +00002663 Size = StrSize(NewString);
jcarseya405b862010-09-14 05:18:09 +00002664 if ((Size + (StrLen(Replace)*sizeof(CHAR16))) > NewSize) {
2665 FreePool(Replace);
jcarsey975136a2009-06-16 19:03:54 +00002666 return (EFI_BUFFER_TOO_SMALL);
2667 }
Qiu Shumine75390f2015-06-30 03:18:31 +00002668 StrCatS(NewString, NewSize/sizeof(CHAR16), Replace);
jcarsey975136a2009-06-16 19:03:54 +00002669 } else {
jcarsey01582942009-07-10 19:46:17 +00002670 Size = StrSize(NewString);
2671 if (Size + sizeof(CHAR16) > NewSize) {
jcarseya405b862010-09-14 05:18:09 +00002672 FreePool(Replace);
jcarsey975136a2009-06-16 19:03:54 +00002673 return (EFI_BUFFER_TOO_SMALL);
2674 }
Qiu Shumine75390f2015-06-30 03:18:31 +00002675 StrnCatS(NewString, NewSize/sizeof(CHAR16), SourceString, 1);
jcarsey975136a2009-06-16 19:03:54 +00002676 SourceString++;
2677 }
2678 }
jcarseya405b862010-09-14 05:18:09 +00002679 FreePool(Replace);
jcarsey975136a2009-06-16 19:03:54 +00002680 return (EFI_SUCCESS);
2681}
jcarseyb1f95a02009-06-16 00:23:19 +00002682
2683/**
jcarseye2f82972009-12-01 05:40:24 +00002684 Internal worker function to output a string.
2685
2686 This function will output a string to the correct StdOut.
2687
2688 @param[in] String The string to print out.
2689
2690 @retval EFI_SUCCESS The operation was sucessful.
2691 @retval !EFI_SUCCESS The operation failed.
2692**/
2693EFI_STATUS
2694EFIAPI
2695InternalPrintTo (
2696 IN CONST CHAR16 *String
2697 )
2698{
2699 UINTN Size;
2700 Size = StrSize(String) - sizeof(CHAR16);
jcarseya405b862010-09-14 05:18:09 +00002701 if (Size == 0) {
2702 return (EFI_SUCCESS);
2703 }
jcarsey366f81a2011-06-27 21:04:22 +00002704 if (gEfiShellParametersProtocol != NULL) {
2705 return (gEfiShellProtocol->WriteFile(gEfiShellParametersProtocol->StdOut, &Size, (VOID*)String));
jcarseye2f82972009-12-01 05:40:24 +00002706 }
2707 if (mEfiShellInterface != NULL) {
jcarsey06c355b2012-03-26 21:00:39 +00002708 if (mEfiShellInterface->RedirArgc == 0) {
jcarsey49bd4982012-01-27 18:42:43 +00002709 //
2710 // Divide in half for old shell. Must be string length not size.
jcarsey06c355b2012-03-26 21:00:39 +00002711 //
2712 Size /=2; // Divide in half only when no redirection.
2713 }
jcarseya405b862010-09-14 05:18:09 +00002714 return (mEfiShellInterface->StdOut->Write(mEfiShellInterface->StdOut, &Size, (VOID*)String));
jcarseye2f82972009-12-01 05:40:24 +00002715 }
2716 ASSERT(FALSE);
2717 return (EFI_UNSUPPORTED);
2718}
2719
2720/**
jcarseyb1f95a02009-06-16 00:23:19 +00002721 Print at a specific location on the screen.
2722
jcarseyf1b87e72009-06-17 00:52:11 +00002723 This function will move the cursor to a given screen location and print the specified string
jcarsey1e6e84c2010-01-25 20:05:08 +00002724
2725 If -1 is specified for either the Row or Col the current screen location for BOTH
jcarseyf1b87e72009-06-17 00:52:11 +00002726 will be used.
jcarseyb1f95a02009-06-16 00:23:19 +00002727
2728 if either Row or Col is out of range for the current console, then ASSERT
2729 if Format is NULL, then ASSERT
2730
jcarsey1e6e84c2010-01-25 20:05:08 +00002731 In addition to the standard %-based flags as supported by UefiLib Print() this supports
jcarseyb1f95a02009-06-16 00:23:19 +00002732 the following additional flags:
2733 %N - Set output attribute to normal
2734 %H - Set output attribute to highlight
2735 %E - Set output attribute to error
2736 %B - Set output attribute to blue color
2737 %V - Set output attribute to green color
2738
2739 Note: The background color is controlled by the shell command cls.
2740
jcarseyb1f95a02009-06-16 00:23:19 +00002741 @param[in] Col the column to print at
jcarsey252d9452011-03-25 20:49:53 +00002742 @param[in] Row the row to print at
jcarseyb1f95a02009-06-16 00:23:19 +00002743 @param[in] Format the format string
jcarsey2247dde2009-11-09 18:08:58 +00002744 @param[in] Marker the marker for the variable argument list
jcarseyb1f95a02009-06-16 00:23:19 +00002745
jcarseya405b862010-09-14 05:18:09 +00002746 @return EFI_SUCCESS The operation was successful.
2747 @return EFI_DEVICE_ERROR The console device reported an error.
jcarseyb1f95a02009-06-16 00:23:19 +00002748**/
jcarseya405b862010-09-14 05:18:09 +00002749EFI_STATUS
jcarseyb1f95a02009-06-16 00:23:19 +00002750EFIAPI
jcarsey2247dde2009-11-09 18:08:58 +00002751InternalShellPrintWorker(
jcarseyb1f95a02009-06-16 00:23:19 +00002752 IN INT32 Col OPTIONAL,
2753 IN INT32 Row OPTIONAL,
2754 IN CONST CHAR16 *Format,
jcarsey252d9452011-03-25 20:49:53 +00002755 IN VA_LIST Marker
jcarsey1e6e84c2010-01-25 20:05:08 +00002756 )
jcarsey2247dde2009-11-09 18:08:58 +00002757{
jcarseyb1f95a02009-06-16 00:23:19 +00002758 EFI_STATUS Status;
jcarsey975136a2009-06-16 19:03:54 +00002759 CHAR16 *ResumeLocation;
2760 CHAR16 *FormatWalker;
jcarseya405b862010-09-14 05:18:09 +00002761 UINTN OriginalAttribute;
jcarsey89e85372011-04-13 23:37:50 +00002762 CHAR16 *mPostReplaceFormat;
2763 CHAR16 *mPostReplaceFormat2;
2764
2765 mPostReplaceFormat = AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize));
2766 mPostReplaceFormat2 = AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize));
jcarseya405b862010-09-14 05:18:09 +00002767
jcarseyf8d3e682011-04-19 17:54:42 +00002768 if (mPostReplaceFormat == NULL || mPostReplaceFormat2 == NULL) {
2769 SHELL_FREE_NON_NULL(mPostReplaceFormat);
2770 SHELL_FREE_NON_NULL(mPostReplaceFormat2);
2771 return (EFI_OUT_OF_RESOURCES);
2772 }
2773
jcarseya405b862010-09-14 05:18:09 +00002774 Status = EFI_SUCCESS;
2775 OriginalAttribute = gST->ConOut->Mode->Attribute;
jcarsey1e6e84c2010-01-25 20:05:08 +00002776
jcarsey975136a2009-06-16 19:03:54 +00002777 //
2778 // Back and forth each time fixing up 1 of our flags...
2779 //
jcarseya405b862010-09-14 05:18:09 +00002780 Status = ShellCopySearchAndReplace(Format, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%N", L"%%N", FALSE, FALSE);
jcarsey975136a2009-06-16 19:03:54 +00002781 ASSERT_EFI_ERROR(Status);
jcarseya405b862010-09-14 05:18:09 +00002782 Status = ShellCopySearchAndReplace(mPostReplaceFormat, mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), L"%E", L"%%E", FALSE, FALSE);
jcarsey975136a2009-06-16 19:03:54 +00002783 ASSERT_EFI_ERROR(Status);
jcarseya405b862010-09-14 05:18:09 +00002784 Status = ShellCopySearchAndReplace(mPostReplaceFormat2, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%H", L"%%H", FALSE, FALSE);
jcarsey975136a2009-06-16 19:03:54 +00002785 ASSERT_EFI_ERROR(Status);
jcarseya405b862010-09-14 05:18:09 +00002786 Status = ShellCopySearchAndReplace(mPostReplaceFormat, mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), L"%B", L"%%B", FALSE, FALSE);
jcarsey975136a2009-06-16 19:03:54 +00002787 ASSERT_EFI_ERROR(Status);
jcarseya405b862010-09-14 05:18:09 +00002788 Status = ShellCopySearchAndReplace(mPostReplaceFormat2, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%V", L"%%V", FALSE, FALSE);
jcarsey975136a2009-06-16 19:03:54 +00002789 ASSERT_EFI_ERROR(Status);
2790
2791 //
2792 // Use the last buffer from replacing to print from...
2793 //
jcarseya405b862010-09-14 05:18:09 +00002794 UnicodeVSPrint (mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), mPostReplaceFormat, Marker);
jcarseyb1f95a02009-06-16 00:23:19 +00002795
2796 if (Col != -1 && Row != -1) {
jcarseyb1f95a02009-06-16 00:23:19 +00002797 Status = gST->ConOut->SetCursorPosition(gST->ConOut, Col, Row);
jcarsey975136a2009-06-16 19:03:54 +00002798 }
2799
jcarseyecd3d592009-12-07 18:05:00 +00002800 FormatWalker = mPostReplaceFormat2;
jcarsey2247dde2009-11-09 18:08:58 +00002801 while (*FormatWalker != CHAR_NULL) {
jcarsey975136a2009-06-16 19:03:54 +00002802 //
2803 // Find the next attribute change request
2804 //
2805 ResumeLocation = StrStr(FormatWalker, L"%");
2806 if (ResumeLocation != NULL) {
jcarsey2247dde2009-11-09 18:08:58 +00002807 *ResumeLocation = CHAR_NULL;
jcarsey975136a2009-06-16 19:03:54 +00002808 }
2809 //
2810 // print the current FormatWalker string
2811 //
jcarseya405b862010-09-14 05:18:09 +00002812 if (StrLen(FormatWalker)>0) {
2813 Status = InternalPrintTo(FormatWalker);
2814 if (EFI_ERROR(Status)) {
2815 break;
2816 }
2817 }
2818
jcarsey975136a2009-06-16 19:03:54 +00002819 //
2820 // update the attribute
2821 //
2822 if (ResumeLocation != NULL) {
jcarsey5d46f172011-08-23 15:34:23 +00002823 if (*(ResumeLocation-1) == L'^') {
2824 //
jcarsey8bb74412012-01-30 18:44:41 +00002825 // Move cursor back 1 position to overwrite the ^
2826 //
2827 gST->ConOut->SetCursorPosition(gST->ConOut, gST->ConOut->Mode->CursorColumn - 1, gST->ConOut->Mode->CursorRow);
2828
2829 //
jcarsey5d46f172011-08-23 15:34:23 +00002830 // Print a simple '%' symbol
2831 //
2832 Status = InternalPrintTo(L"%");
2833 ResumeLocation = ResumeLocation - 1;
2834 } else {
2835 switch (*(ResumeLocation+1)) {
2836 case (L'N'):
2837 gST->ConOut->SetAttribute(gST->ConOut, OriginalAttribute);
jcarseya405b862010-09-14 05:18:09 +00002838 break;
jcarsey5d46f172011-08-23 15:34:23 +00002839 case (L'E'):
2840 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_YELLOW, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));
2841 break;
2842 case (L'H'):
2843 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_WHITE, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));
2844 break;
2845 case (L'B'):
2846 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_BLUE, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));
2847 break;
2848 case (L'V'):
2849 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_GREEN, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));
2850 break;
2851 default:
2852 //
2853 // Print a simple '%' symbol
2854 //
2855 Status = InternalPrintTo(L"%");
2856 if (EFI_ERROR(Status)) {
2857 break;
2858 }
2859 ResumeLocation = ResumeLocation - 1;
2860 break;
2861 }
jcarsey975136a2009-06-16 19:03:54 +00002862 }
2863 } else {
2864 //
2865 // reset to normal now...
2866 //
jcarsey975136a2009-06-16 19:03:54 +00002867 break;
2868 }
2869
2870 //
2871 // update FormatWalker to Resume + 2 (skip the % and the indicator)
2872 //
2873 FormatWalker = ResumeLocation + 2;
2874 }
jcarseyb1f95a02009-06-16 00:23:19 +00002875
jcarseya405b862010-09-14 05:18:09 +00002876 gST->ConOut->SetAttribute(gST->ConOut, OriginalAttribute);
jcarsey89e85372011-04-13 23:37:50 +00002877
2878 SHELL_FREE_NON_NULL(mPostReplaceFormat);
2879 SHELL_FREE_NON_NULL(mPostReplaceFormat2);
jcarseya405b862010-09-14 05:18:09 +00002880 return (Status);
jcarsey5f7431d2009-07-10 18:06:01 +00002881}
jcarsey2247dde2009-11-09 18:08:58 +00002882
2883/**
2884 Print at a specific location on the screen.
2885
jcarseye2f82972009-12-01 05:40:24 +00002886 This function will move the cursor to a given screen location and print the specified string.
jcarsey1e6e84c2010-01-25 20:05:08 +00002887
2888 If -1 is specified for either the Row or Col the current screen location for BOTH
jcarsey2247dde2009-11-09 18:08:58 +00002889 will be used.
2890
jcarseye2f82972009-12-01 05:40:24 +00002891 If either Row or Col is out of range for the current console, then ASSERT.
2892 If Format is NULL, then ASSERT.
jcarsey2247dde2009-11-09 18:08:58 +00002893
jcarsey1e6e84c2010-01-25 20:05:08 +00002894 In addition to the standard %-based flags as supported by UefiLib Print() this supports
jcarsey2247dde2009-11-09 18:08:58 +00002895 the following additional flags:
2896 %N - Set output attribute to normal
2897 %H - Set output attribute to highlight
2898 %E - Set output attribute to error
2899 %B - Set output attribute to blue color
2900 %V - Set output attribute to green color
2901
2902 Note: The background color is controlled by the shell command cls.
2903
jcarsey2247dde2009-11-09 18:08:58 +00002904 @param[in] Col the column to print at
jcarseya405b862010-09-14 05:18:09 +00002905 @param[in] Row the row to print at
jcarsey2247dde2009-11-09 18:08:58 +00002906 @param[in] Format the format string
jcarseya405b862010-09-14 05:18:09 +00002907 @param[in] ... The variable argument list.
jcarsey2247dde2009-11-09 18:08:58 +00002908
jcarseya405b862010-09-14 05:18:09 +00002909 @return EFI_SUCCESS The printing was successful.
2910 @return EFI_DEVICE_ERROR The console device reported an error.
jcarsey2247dde2009-11-09 18:08:58 +00002911**/
jcarseya405b862010-09-14 05:18:09 +00002912EFI_STATUS
jcarsey2247dde2009-11-09 18:08:58 +00002913EFIAPI
2914ShellPrintEx(
2915 IN INT32 Col OPTIONAL,
2916 IN INT32 Row OPTIONAL,
2917 IN CONST CHAR16 *Format,
2918 ...
jcarsey1e6e84c2010-01-25 20:05:08 +00002919 )
jcarsey2247dde2009-11-09 18:08:58 +00002920{
2921 VA_LIST Marker;
jcarseya405b862010-09-14 05:18:09 +00002922 EFI_STATUS RetVal;
jcarsey3e082d52010-10-04 16:44:57 +00002923 if (Format == NULL) {
2924 return (EFI_INVALID_PARAMETER);
2925 }
jcarsey2247dde2009-11-09 18:08:58 +00002926 VA_START (Marker, Format);
jcarseya405b862010-09-14 05:18:09 +00002927 RetVal = InternalShellPrintWorker(Col, Row, Format, Marker);
jcarseye2f82972009-12-01 05:40:24 +00002928 VA_END(Marker);
jcarseya405b862010-09-14 05:18:09 +00002929 return(RetVal);
jcarsey2247dde2009-11-09 18:08:58 +00002930}
2931
2932/**
2933 Print at a specific location on the screen.
2934
jcarseye2f82972009-12-01 05:40:24 +00002935 This function will move the cursor to a given screen location and print the specified string.
jcarsey1e6e84c2010-01-25 20:05:08 +00002936
2937 If -1 is specified for either the Row or Col the current screen location for BOTH
jcarseye2f82972009-12-01 05:40:24 +00002938 will be used.
jcarsey2247dde2009-11-09 18:08:58 +00002939
jcarseye2f82972009-12-01 05:40:24 +00002940 If either Row or Col is out of range for the current console, then ASSERT.
2941 If Format is NULL, then ASSERT.
jcarsey2247dde2009-11-09 18:08:58 +00002942
jcarsey1e6e84c2010-01-25 20:05:08 +00002943 In addition to the standard %-based flags as supported by UefiLib Print() this supports
jcarsey2247dde2009-11-09 18:08:58 +00002944 the following additional flags:
jcarsey1e6e84c2010-01-25 20:05:08 +00002945 %N - Set output attribute to normal.
2946 %H - Set output attribute to highlight.
2947 %E - Set output attribute to error.
2948 %B - Set output attribute to blue color.
2949 %V - Set output attribute to green color.
jcarsey2247dde2009-11-09 18:08:58 +00002950
2951 Note: The background color is controlled by the shell command cls.
2952
jcarsey1e6e84c2010-01-25 20:05:08 +00002953 @param[in] Col The column to print at.
jcarseya405b862010-09-14 05:18:09 +00002954 @param[in] Row The row to print at.
jcarsey1e6e84c2010-01-25 20:05:08 +00002955 @param[in] Language The language of the string to retrieve. If this parameter
2956 is NULL, then the current platform language is used.
2957 @param[in] HiiFormatStringId The format string Id for getting from Hii.
2958 @param[in] HiiFormatHandle The format string Handle for getting from Hii.
jcarseya405b862010-09-14 05:18:09 +00002959 @param[in] ... The variable argument list.
jcarsey2247dde2009-11-09 18:08:58 +00002960
jcarseya405b862010-09-14 05:18:09 +00002961 @return EFI_SUCCESS The printing was successful.
2962 @return EFI_DEVICE_ERROR The console device reported an error.
jcarsey2247dde2009-11-09 18:08:58 +00002963**/
jcarseya405b862010-09-14 05:18:09 +00002964EFI_STATUS
jcarsey2247dde2009-11-09 18:08:58 +00002965EFIAPI
2966ShellPrintHiiEx(
2967 IN INT32 Col OPTIONAL,
2968 IN INT32 Row OPTIONAL,
jcarsey1e6e84c2010-01-25 20:05:08 +00002969 IN CONST CHAR8 *Language OPTIONAL,
jcarsey2247dde2009-11-09 18:08:58 +00002970 IN CONST EFI_STRING_ID HiiFormatStringId,
2971 IN CONST EFI_HANDLE HiiFormatHandle,
2972 ...
2973 )
2974{
2975 VA_LIST Marker;
2976 CHAR16 *HiiFormatString;
jcarseya405b862010-09-14 05:18:09 +00002977 EFI_STATUS RetVal;
jcarsey2247dde2009-11-09 18:08:58 +00002978
2979 VA_START (Marker, HiiFormatHandle);
jcarsey1e6e84c2010-01-25 20:05:08 +00002980 HiiFormatString = HiiGetString(HiiFormatHandle, HiiFormatStringId, Language);
jcarsey2247dde2009-11-09 18:08:58 +00002981 ASSERT(HiiFormatString != NULL);
2982
2983 RetVal = InternalShellPrintWorker(Col, Row, HiiFormatString, Marker);
2984
jcarseya405b862010-09-14 05:18:09 +00002985 SHELL_FREE_NON_NULL(HiiFormatString);
jcarseye2f82972009-12-01 05:40:24 +00002986 VA_END(Marker);
jcarsey2247dde2009-11-09 18:08:58 +00002987
2988 return (RetVal);
2989}
2990
2991/**
2992 Function to determine if a given filename represents a file or a directory.
2993
2994 @param[in] DirName Path to directory to test.
2995
jcarseyc8c22592011-10-17 17:49:21 +00002996 @retval EFI_SUCCESS The Path represents a directory
2997 @retval EFI_NOT_FOUND The Path does not represent a directory
2998 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
2999 @return The path failed to open
jcarsey2247dde2009-11-09 18:08:58 +00003000**/
3001EFI_STATUS
3002EFIAPI
3003ShellIsDirectory(
3004 IN CONST CHAR16 *DirName
3005 )
3006{
3007 EFI_STATUS Status;
jcarseya405b862010-09-14 05:18:09 +00003008 SHELL_FILE_HANDLE Handle;
jcarsey3e082d52010-10-04 16:44:57 +00003009 CHAR16 *TempLocation;
3010 CHAR16 *TempLocation2;
jcarsey2247dde2009-11-09 18:08:58 +00003011
jcarseyecd3d592009-12-07 18:05:00 +00003012 ASSERT(DirName != NULL);
3013
jcarseya405b862010-09-14 05:18:09 +00003014 Handle = NULL;
3015 TempLocation = NULL;
jcarsey2247dde2009-11-09 18:08:58 +00003016
3017 Status = ShellOpenFileByName(DirName, &Handle, EFI_FILE_MODE_READ, 0);
3018 if (EFI_ERROR(Status)) {
jcarseya405b862010-09-14 05:18:09 +00003019 //
3020 // try good logic first.
3021 //
jcarsey366f81a2011-06-27 21:04:22 +00003022 if (gEfiShellProtocol != NULL) {
jcarsey3e082d52010-10-04 16:44:57 +00003023 TempLocation = StrnCatGrow(&TempLocation, NULL, DirName, 0);
jcarseyc8c22592011-10-17 17:49:21 +00003024 if (TempLocation == NULL) {
3025 ShellCloseFile(&Handle);
3026 return (EFI_OUT_OF_RESOURCES);
3027 }
jcarsey3e082d52010-10-04 16:44:57 +00003028 TempLocation2 = StrStr(TempLocation, L":");
3029 if (TempLocation2 != NULL && StrLen(StrStr(TempLocation, L":")) == 2) {
3030 *(TempLocation2+1) = CHAR_NULL;
jcarseya405b862010-09-14 05:18:09 +00003031 }
jcarsey366f81a2011-06-27 21:04:22 +00003032 if (gEfiShellProtocol->GetDevicePathFromMap(TempLocation) != NULL) {
jcarseya405b862010-09-14 05:18:09 +00003033 FreePool(TempLocation);
3034 return (EFI_SUCCESS);
3035 }
3036 FreePool(TempLocation);
3037 } else {
3038 //
3039 // probably a map name?!?!!?
3040 //
3041 TempLocation = StrStr(DirName, L"\\");
3042 if (TempLocation != NULL && *(TempLocation+1) == CHAR_NULL) {
3043 return (EFI_SUCCESS);
3044 }
3045 }
jcarsey2247dde2009-11-09 18:08:58 +00003046 return (Status);
3047 }
3048
3049 if (FileHandleIsDirectory(Handle) == EFI_SUCCESS) {
3050 ShellCloseFile(&Handle);
3051 return (EFI_SUCCESS);
3052 }
3053 ShellCloseFile(&Handle);
3054 return (EFI_NOT_FOUND);
3055}
3056
jcarsey125c2cf2009-11-18 21:36:50 +00003057/**
jcarsey36a9d672009-11-20 21:13:41 +00003058 Function to determine if a given filename represents a file.
3059
3060 @param[in] Name Path to file to test.
3061
3062 @retval EFI_SUCCESS The Path represents a file.
3063 @retval EFI_NOT_FOUND The Path does not represent a file.
3064 @retval other The path failed to open.
3065**/
3066EFI_STATUS
3067EFIAPI
3068ShellIsFile(
3069 IN CONST CHAR16 *Name
3070 )
3071{
3072 EFI_STATUS Status;
jcarseya405b862010-09-14 05:18:09 +00003073 SHELL_FILE_HANDLE Handle;
jcarsey36a9d672009-11-20 21:13:41 +00003074
jcarseyecd3d592009-12-07 18:05:00 +00003075 ASSERT(Name != NULL);
3076
jcarsey36a9d672009-11-20 21:13:41 +00003077 Handle = NULL;
3078
3079 Status = ShellOpenFileByName(Name, &Handle, EFI_FILE_MODE_READ, 0);
3080 if (EFI_ERROR(Status)) {
3081 return (Status);
3082 }
3083
3084 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {
3085 ShellCloseFile(&Handle);
3086 return (EFI_SUCCESS);
3087 }
3088 ShellCloseFile(&Handle);
3089 return (EFI_NOT_FOUND);
3090}
3091
3092/**
jcarseyb3011f42010-01-11 21:49:04 +00003093 Function to determine if a given filename represents a file.
3094
3095 This will search the CWD and then the Path.
3096
3097 If Name is NULL, then ASSERT.
3098
3099 @param[in] Name Path to file to test.
3100
3101 @retval EFI_SUCCESS The Path represents a file.
3102 @retval EFI_NOT_FOUND The Path does not represent a file.
3103 @retval other The path failed to open.
3104**/
3105EFI_STATUS
3106EFIAPI
3107ShellIsFileInPath(
3108 IN CONST CHAR16 *Name
jcarseya405b862010-09-14 05:18:09 +00003109 )
3110{
jcarseyb3011f42010-01-11 21:49:04 +00003111 CHAR16 *NewName;
3112 EFI_STATUS Status;
3113
3114 if (!EFI_ERROR(ShellIsFile(Name))) {
jcarseya405b862010-09-14 05:18:09 +00003115 return (EFI_SUCCESS);
jcarseyb3011f42010-01-11 21:49:04 +00003116 }
3117
3118 NewName = ShellFindFilePath(Name);
3119 if (NewName == NULL) {
3120 return (EFI_NOT_FOUND);
3121 }
3122 Status = ShellIsFile(NewName);
3123 FreePool(NewName);
3124 return (Status);
3125}
jcarsey252d9452011-03-25 20:49:53 +00003126
jcarseyb3011f42010-01-11 21:49:04 +00003127/**
Jaben Carsey74b0fb82013-11-22 21:37:34 +00003128 Function return the number converted from a hex representation of a number.
3129
3130 Note: this function cannot be used when (UINTN)(-1), (0xFFFFFFFF) may be a valid
3131 result. Use ShellConvertStringToUint64 instead.
3132
3133 @param[in] String String representation of a number.
3134
3135 @return The unsigned integer result of the conversion.
3136 @retval (UINTN)(-1) An error occured.
3137**/
3138UINTN
3139EFIAPI
3140ShellHexStrToUintn(
3141 IN CONST CHAR16 *String
3142 )
3143{
3144 UINT64 RetVal;
3145
3146 if (!EFI_ERROR(ShellConvertStringToUint64(String, &RetVal, TRUE, TRUE))) {
3147 return ((UINTN)RetVal);
3148 }
3149
3150 return ((UINTN)(-1));
3151}
3152
3153/**
jcarsey1e6e84c2010-01-25 20:05:08 +00003154 Function to determine whether a string is decimal or hex representation of a number
Jaben Carseyd59fc242013-11-14 23:52:43 +00003155 and return the number converted from the string. Spaces are always skipped.
jcarsey125c2cf2009-11-18 21:36:50 +00003156
3157 @param[in] String String representation of a number
3158
jcarsey252d9452011-03-25 20:49:53 +00003159 @return the number
3160 @retval (UINTN)(-1) An error ocurred.
jcarsey125c2cf2009-11-18 21:36:50 +00003161**/
3162UINTN
3163EFIAPI
3164ShellStrToUintn(
3165 IN CONST CHAR16 *String
3166 )
3167{
jcarsey252d9452011-03-25 20:49:53 +00003168 UINT64 RetVal;
3169 BOOLEAN Hex;
3170
3171 Hex = FALSE;
3172
jcarsey658bf432014-11-04 22:33:16 +00003173 if (!InternalShellIsHexOrDecimalNumber(String, Hex, TRUE, FALSE)) {
jcarsey252d9452011-03-25 20:49:53 +00003174 Hex = TRUE;
jcarsey125c2cf2009-11-18 21:36:50 +00003175 }
jcarsey252d9452011-03-25 20:49:53 +00003176
3177 if (!EFI_ERROR(ShellConvertStringToUint64(String, &RetVal, Hex, TRUE))) {
3178 return ((UINTN)RetVal);
3179 }
3180 return ((UINTN)(-1));
jcarsey125c2cf2009-11-18 21:36:50 +00003181}
3182
3183/**
3184 Safely append with automatic string resizing given length of Destination and
3185 desired length of copy from Source.
3186
3187 append the first D characters of Source to the end of Destination, where D is
3188 the lesser of Count and the StrLen() of Source. If appending those D characters
3189 will fit within Destination (whose Size is given as CurrentSize) and
jcarsey1e6e84c2010-01-25 20:05:08 +00003190 still leave room for a NULL terminator, then those characters are appended,
3191 starting at the original terminating NULL of Destination, and a new terminating
3192 NULL is appended.
jcarsey125c2cf2009-11-18 21:36:50 +00003193
3194 If appending D characters onto Destination will result in a overflow of the size
3195 given in CurrentSize the string will be grown such that the copy can be performed
3196 and CurrentSize will be updated to the new size.
3197
3198 If Source is NULL, there is nothing to append, just return the current buffer in
3199 Destination.
3200
3201 if Destination is NULL, then ASSERT()
3202 if Destination's current length (including NULL terminator) is already more then
3203 CurrentSize, then ASSERT()
3204
ydong104ff7e372011-09-02 08:05:34 +00003205 @param[in, out] Destination The String to append onto
3206 @param[in, out] CurrentSize on call the number of bytes in Destination. On
jcarsey125c2cf2009-11-18 21:36:50 +00003207 return possibly the new size (still in bytes). if NULL
3208 then allocate whatever is needed.
3209 @param[in] Source The String to append from
3210 @param[in] Count Maximum number of characters to append. if 0 then
3211 all are appended.
3212
3213 @return Destination return the resultant string.
3214**/
3215CHAR16*
3216EFIAPI
3217StrnCatGrow (
3218 IN OUT CHAR16 **Destination,
3219 IN OUT UINTN *CurrentSize,
3220 IN CONST CHAR16 *Source,
3221 IN UINTN Count
3222 )
3223{
3224 UINTN DestinationStartSize;
3225 UINTN NewSize;
3226
3227 //
3228 // ASSERTs
3229 //
3230 ASSERT(Destination != NULL);
3231
3232 //
3233 // If there's nothing to do then just return Destination
3234 //
3235 if (Source == NULL) {
3236 return (*Destination);
3237 }
3238
3239 //
3240 // allow for un-initialized pointers, based on size being 0
3241 //
3242 if (CurrentSize != NULL && *CurrentSize == 0) {
3243 *Destination = NULL;
3244 }
3245
3246 //
3247 // allow for NULL pointers address as Destination
3248 //
3249 if (*Destination != NULL) {
3250 ASSERT(CurrentSize != 0);
3251 DestinationStartSize = StrSize(*Destination);
3252 ASSERT(DestinationStartSize <= *CurrentSize);
3253 } else {
3254 DestinationStartSize = 0;
3255// ASSERT(*CurrentSize == 0);
3256 }
3257
3258 //
3259 // Append all of Source?
3260 //
3261 if (Count == 0) {
3262 Count = StrLen(Source);
3263 }
3264
3265 //
3266 // Test and grow if required
3267 //
3268 if (CurrentSize != NULL) {
3269 NewSize = *CurrentSize;
ydong10f480fdc2012-09-07 01:55:33 +00003270 if (NewSize < DestinationStartSize + (Count * sizeof(CHAR16))) {
3271 while (NewSize < (DestinationStartSize + (Count*sizeof(CHAR16)))) {
3272 NewSize += 2 * Count * sizeof(CHAR16);
3273 }
3274 *Destination = ReallocatePool(*CurrentSize, NewSize, *Destination);
3275 *CurrentSize = NewSize;
jcarsey125c2cf2009-11-18 21:36:50 +00003276 }
jcarsey125c2cf2009-11-18 21:36:50 +00003277 } else {
Qiu Shuminc587fd32015-07-02 01:12:03 +00003278 NewSize = (Count+1)*sizeof(CHAR16);
3279 *Destination = AllocateZeroPool(NewSize);
jcarsey125c2cf2009-11-18 21:36:50 +00003280 }
3281
3282 //
3283 // Now use standard StrnCat on a big enough buffer
3284 //
jcarseyc9d92df2010-02-03 15:37:54 +00003285 if (*Destination == NULL) {
3286 return (NULL);
3287 }
Qiu Shumine75390f2015-06-30 03:18:31 +00003288
Qiu Shuminc587fd32015-07-02 01:12:03 +00003289 StrnCatS(*Destination, NewSize/sizeof(CHAR16), Source, Count);
Qiu Shumine75390f2015-06-30 03:18:31 +00003290 return *Destination;
jcarsey125c2cf2009-11-18 21:36:50 +00003291}
jcarseyc9d92df2010-02-03 15:37:54 +00003292
3293/**
3294 Prompt the user and return the resultant answer to the requestor.
3295
3296 This function will display the requested question on the shell prompt and then
Mudusuru, Giri P1d322462016-07-07 00:47:45 -07003297 wait for an appropriate answer to be input from the console.
jcarseyc9d92df2010-02-03 15:37:54 +00003298
jcarseya405b862010-09-14 05:18:09 +00003299 if the SHELL_PROMPT_REQUEST_TYPE is SHELL_PROMPT_REQUEST_TYPE_YESNO, ShellPromptResponseTypeQuitContinue
jcarseyc9d92df2010-02-03 15:37:54 +00003300 or SHELL_PROMPT_REQUEST_TYPE_YESNOCANCEL then *Response is of type SHELL_PROMPT_RESPONSE.
3301
jcarseya405b862010-09-14 05:18:09 +00003302 if the SHELL_PROMPT_REQUEST_TYPE is ShellPromptResponseTypeFreeform then *Response is of type
jcarseyc9d92df2010-02-03 15:37:54 +00003303 CHAR16*.
3304
3305 In either case *Response must be callee freed if Response was not NULL;
3306
3307 @param Type What type of question is asked. This is used to filter the input
3308 to prevent invalid answers to question.
3309 @param Prompt Pointer to string prompt to use to request input.
3310 @param Response Pointer to Response which will be populated upon return.
3311
3312 @retval EFI_SUCCESS The operation was sucessful.
3313 @retval EFI_UNSUPPORTED The operation is not supported as requested.
3314 @retval EFI_INVALID_PARAMETER A parameter was invalid.
3315 @return other The operation failed.
3316**/
3317EFI_STATUS
3318EFIAPI
3319ShellPromptForResponse (
3320 IN SHELL_PROMPT_REQUEST_TYPE Type,
3321 IN CHAR16 *Prompt OPTIONAL,
3322 IN OUT VOID **Response OPTIONAL
3323 )
3324{
3325 EFI_STATUS Status;
3326 EFI_INPUT_KEY Key;
3327 UINTN EventIndex;
3328 SHELL_PROMPT_RESPONSE *Resp;
jcarseya405b862010-09-14 05:18:09 +00003329 UINTN Size;
3330 CHAR16 *Buffer;
jcarseyc9d92df2010-02-03 15:37:54 +00003331
jcarseya405b862010-09-14 05:18:09 +00003332 Status = EFI_UNSUPPORTED;
3333 Resp = NULL;
3334 Buffer = NULL;
3335 Size = 0;
3336 if (Type != ShellPromptResponseTypeFreeform) {
jcarsey252d9452011-03-25 20:49:53 +00003337 Resp = (SHELL_PROMPT_RESPONSE*)AllocateZeroPool(sizeof(SHELL_PROMPT_RESPONSE));
jcarsey3e082d52010-10-04 16:44:57 +00003338 if (Resp == NULL) {
3339 return (EFI_OUT_OF_RESOURCES);
3340 }
xdu290bfa222010-07-19 05:21:27 +00003341 }
jcarseyc9d92df2010-02-03 15:37:54 +00003342
3343 switch(Type) {
jcarseya405b862010-09-14 05:18:09 +00003344 case ShellPromptResponseTypeQuitContinue:
jcarseyc9d92df2010-02-03 15:37:54 +00003345 if (Prompt != NULL) {
3346 ShellPrintEx(-1, -1, L"%s", Prompt);
3347 }
3348 //
3349 // wait for valid response
3350 //
3351 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
3352 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
Jaben Carsey31b018a2014-01-16 16:52:39 +00003353 if (EFI_ERROR(Status)) {
3354 break;
3355 }
jcarseyc9d92df2010-02-03 15:37:54 +00003356 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3357 if (Key.UnicodeChar == L'Q' || Key.UnicodeChar ==L'q') {
jcarseya405b862010-09-14 05:18:09 +00003358 *Resp = ShellPromptResponseQuit;
jcarseyc9d92df2010-02-03 15:37:54 +00003359 } else {
jcarseya405b862010-09-14 05:18:09 +00003360 *Resp = ShellPromptResponseContinue;
jcarseyc9d92df2010-02-03 15:37:54 +00003361 }
3362 break;
jcarseya405b862010-09-14 05:18:09 +00003363 case ShellPromptResponseTypeYesNoCancel:
jcarseyc9d92df2010-02-03 15:37:54 +00003364 if (Prompt != NULL) {
3365 ShellPrintEx(-1, -1, L"%s", Prompt);
3366 }
3367 //
3368 // wait for valid response
3369 //
jcarseya405b862010-09-14 05:18:09 +00003370 *Resp = ShellPromptResponseMax;
3371 while (*Resp == ShellPromptResponseMax) {
Jaben Carsey194ae482013-12-09 22:55:13 +00003372 if (ShellGetExecutionBreakFlag()) {
3373 Status = EFI_ABORTED;
3374 break;
3375 }
jcarseyc9d92df2010-02-03 15:37:54 +00003376 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
3377 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
Jaben Carsey31b018a2014-01-16 16:52:39 +00003378 if (EFI_ERROR(Status)) {
3379 break;
3380 }
jcarseyc9d92df2010-02-03 15:37:54 +00003381 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3382 switch (Key.UnicodeChar) {
3383 case L'Y':
3384 case L'y':
jcarseya405b862010-09-14 05:18:09 +00003385 *Resp = ShellPromptResponseYes;
jcarseyc9d92df2010-02-03 15:37:54 +00003386 break;
3387 case L'N':
3388 case L'n':
jcarseya405b862010-09-14 05:18:09 +00003389 *Resp = ShellPromptResponseNo;
jcarseyc9d92df2010-02-03 15:37:54 +00003390 break;
3391 case L'C':
3392 case L'c':
jcarseya405b862010-09-14 05:18:09 +00003393 *Resp = ShellPromptResponseCancel;
3394 break;
3395 }
3396 }
3397 break; case ShellPromptResponseTypeYesNoAllCancel:
3398 if (Prompt != NULL) {
3399 ShellPrintEx(-1, -1, L"%s", Prompt);
3400 }
3401 //
3402 // wait for valid response
3403 //
3404 *Resp = ShellPromptResponseMax;
3405 while (*Resp == ShellPromptResponseMax) {
Jaben Carsey194ae482013-12-09 22:55:13 +00003406 if (ShellGetExecutionBreakFlag()) {
3407 Status = EFI_ABORTED;
3408 break;
3409 }
jcarseya405b862010-09-14 05:18:09 +00003410 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
3411 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
Jaben Carsey31b018a2014-01-16 16:52:39 +00003412 if (EFI_ERROR(Status)) {
3413 break;
3414 }
jcarseya405b862010-09-14 05:18:09 +00003415 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3416 switch (Key.UnicodeChar) {
3417 case L'Y':
3418 case L'y':
3419 *Resp = ShellPromptResponseYes;
3420 break;
3421 case L'N':
3422 case L'n':
3423 *Resp = ShellPromptResponseNo;
3424 break;
3425 case L'A':
3426 case L'a':
3427 *Resp = ShellPromptResponseAll;
3428 break;
3429 case L'C':
3430 case L'c':
3431 *Resp = ShellPromptResponseCancel;
jcarseyc9d92df2010-02-03 15:37:54 +00003432 break;
3433 }
3434 }
3435 break;
jcarseya405b862010-09-14 05:18:09 +00003436 case ShellPromptResponseTypeEnterContinue:
3437 case ShellPromptResponseTypeAnyKeyContinue:
jcarseyc9d92df2010-02-03 15:37:54 +00003438 if (Prompt != NULL) {
3439 ShellPrintEx(-1, -1, L"%s", Prompt);
3440 }
3441 //
3442 // wait for valid response
3443 //
jcarseya405b862010-09-14 05:18:09 +00003444 *Resp = ShellPromptResponseMax;
3445 while (*Resp == ShellPromptResponseMax) {
Jaben Carsey194ae482013-12-09 22:55:13 +00003446 if (ShellGetExecutionBreakFlag()) {
3447 Status = EFI_ABORTED;
3448 break;
3449 }
jcarseyc9d92df2010-02-03 15:37:54 +00003450 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
jcarseya405b862010-09-14 05:18:09 +00003451 if (Type == ShellPromptResponseTypeEnterContinue) {
jcarseyc9d92df2010-02-03 15:37:54 +00003452 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
Jaben Carsey31b018a2014-01-16 16:52:39 +00003453 if (EFI_ERROR(Status)) {
3454 break;
3455 }
jcarseyc9d92df2010-02-03 15:37:54 +00003456 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3457 if (Key.UnicodeChar == CHAR_CARRIAGE_RETURN) {
jcarseya405b862010-09-14 05:18:09 +00003458 *Resp = ShellPromptResponseContinue;
jcarseyc9d92df2010-02-03 15:37:54 +00003459 break;
3460 }
3461 }
jcarseya405b862010-09-14 05:18:09 +00003462 if (Type == ShellPromptResponseTypeAnyKeyContinue) {
3463 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
3464 ASSERT_EFI_ERROR(Status);
3465 *Resp = ShellPromptResponseContinue;
jcarseyc9d92df2010-02-03 15:37:54 +00003466 break;
3467 }
3468 }
3469 break;
jcarseya405b862010-09-14 05:18:09 +00003470 case ShellPromptResponseTypeYesNo:
3471 if (Prompt != NULL) {
3472 ShellPrintEx(-1, -1, L"%s", Prompt);
3473 }
3474 //
3475 // wait for valid response
3476 //
3477 *Resp = ShellPromptResponseMax;
3478 while (*Resp == ShellPromptResponseMax) {
Jaben Carsey194ae482013-12-09 22:55:13 +00003479 if (ShellGetExecutionBreakFlag()) {
3480 Status = EFI_ABORTED;
3481 break;
3482 }
jcarseya405b862010-09-14 05:18:09 +00003483 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
3484 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
Jaben Carsey31b018a2014-01-16 16:52:39 +00003485 if (EFI_ERROR(Status)) {
3486 break;
3487 }
jcarseya405b862010-09-14 05:18:09 +00003488 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3489 switch (Key.UnicodeChar) {
3490 case L'Y':
3491 case L'y':
3492 *Resp = ShellPromptResponseYes;
3493 break;
3494 case L'N':
3495 case L'n':
3496 *Resp = ShellPromptResponseNo;
3497 break;
3498 }
3499 }
3500 break;
3501 case ShellPromptResponseTypeFreeform:
3502 if (Prompt != NULL) {
3503 ShellPrintEx(-1, -1, L"%s", Prompt);
3504 }
3505 while(1) {
Jaben Carsey194ae482013-12-09 22:55:13 +00003506 if (ShellGetExecutionBreakFlag()) {
3507 Status = EFI_ABORTED;
3508 break;
3509 }
jcarseya405b862010-09-14 05:18:09 +00003510 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
3511 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
Jaben Carsey31b018a2014-01-16 16:52:39 +00003512 if (EFI_ERROR(Status)) {
3513 break;
3514 }
jcarseya405b862010-09-14 05:18:09 +00003515 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3516 if (Key.UnicodeChar == CHAR_CARRIAGE_RETURN) {
3517 break;
3518 }
3519 ASSERT((Buffer == NULL && Size == 0) || (Buffer != NULL));
3520 StrnCatGrow(&Buffer, &Size, &Key.UnicodeChar, 1);
3521 }
3522 break;
3523 //
3524 // This is the location to add new prompt types.
Jaben Carsey194ae482013-12-09 22:55:13 +00003525 // If your new type loops remember to add ExecutionBreak support.
jcarseya405b862010-09-14 05:18:09 +00003526 //
jcarseyc9d92df2010-02-03 15:37:54 +00003527 default:
jcarseya405b862010-09-14 05:18:09 +00003528 ASSERT(FALSE);
jcarseyc9d92df2010-02-03 15:37:54 +00003529 }
3530
3531 if (Response != NULL) {
jcarseya405b862010-09-14 05:18:09 +00003532 if (Resp != NULL) {
3533 *Response = Resp;
3534 } else if (Buffer != NULL) {
3535 *Response = Buffer;
3536 }
jcarseyc9d92df2010-02-03 15:37:54 +00003537 } else {
jcarseya405b862010-09-14 05:18:09 +00003538 if (Resp != NULL) {
3539 FreePool(Resp);
3540 }
3541 if (Buffer != NULL) {
3542 FreePool(Buffer);
3543 }
jcarseyc9d92df2010-02-03 15:37:54 +00003544 }
3545
jcarseya405b862010-09-14 05:18:09 +00003546 ShellPrintEx(-1, -1, L"\r\n");
jcarseyc9d92df2010-02-03 15:37:54 +00003547 return (Status);
3548}
3549
3550/**
3551 Prompt the user and return the resultant answer to the requestor.
3552
3553 This function is the same as ShellPromptForResponse, except that the prompt is
3554 automatically pulled from HII.
3555
3556 @param Type What type of question is asked. This is used to filter the input
3557 to prevent invalid answers to question.
jcarseya405b862010-09-14 05:18:09 +00003558 @param[in] HiiFormatStringId The format string Id for getting from Hii.
3559 @param[in] HiiFormatHandle The format string Handle for getting from Hii.
3560 @param Response Pointer to Response which will be populated upon return.
jcarseyc9d92df2010-02-03 15:37:54 +00003561
3562 @retval EFI_SUCCESS the operation was sucessful.
3563 @return other the operation failed.
3564
3565 @sa ShellPromptForResponse
3566**/
3567EFI_STATUS
3568EFIAPI
3569ShellPromptForResponseHii (
3570 IN SHELL_PROMPT_REQUEST_TYPE Type,
3571 IN CONST EFI_STRING_ID HiiFormatStringId,
3572 IN CONST EFI_HANDLE HiiFormatHandle,
3573 IN OUT VOID **Response
3574 )
3575{
3576 CHAR16 *Prompt;
3577 EFI_STATUS Status;
3578
3579 Prompt = HiiGetString(HiiFormatHandle, HiiFormatStringId, NULL);
3580 Status = ShellPromptForResponse(Type, Prompt, Response);
3581 FreePool(Prompt);
3582 return (Status);
3583}
3584
jcarseya405b862010-09-14 05:18:09 +00003585/**
3586 Function to determin if an entire string is a valid number.
jcarseyc9d92df2010-02-03 15:37:54 +00003587
jcarseya405b862010-09-14 05:18:09 +00003588 If Hex it must be preceeded with a 0x or has ForceHex, set TRUE.
3589
3590 @param[in] String The string to evaluate.
3591 @param[in] ForceHex TRUE - always assume hex.
3592 @param[in] StopAtSpace TRUE to halt upon finding a space, FALSE to keep going.
jcarsey658bf432014-11-04 22:33:16 +00003593 @param[in] TimeNumbers TRUE to allow numbers with ":", FALSE otherwise.
jcarseya405b862010-09-14 05:18:09 +00003594
3595 @retval TRUE It is all numeric (dec/hex) characters.
3596 @retval FALSE There is a non-numeric character.
3597**/
3598BOOLEAN
3599EFIAPI
jcarsey252d9452011-03-25 20:49:53 +00003600InternalShellIsHexOrDecimalNumber (
jcarseya405b862010-09-14 05:18:09 +00003601 IN CONST CHAR16 *String,
3602 IN CONST BOOLEAN ForceHex,
jcarsey658bf432014-11-04 22:33:16 +00003603 IN CONST BOOLEAN StopAtSpace,
3604 IN CONST BOOLEAN TimeNumbers
jcarseya405b862010-09-14 05:18:09 +00003605 )
3606{
3607 BOOLEAN Hex;
3608
3609 //
3610 // chop off a single negative sign
3611 //
3612 if (String != NULL && *String == L'-') {
3613 String++;
3614 }
darylm503b0934ac2011-11-12 00:35:11 +00003615
jcarseya405b862010-09-14 05:18:09 +00003616 if (String == NULL) {
3617 return (FALSE);
3618 }
3619
3620 //
3621 // chop leading zeroes
3622 //
3623 while(String != NULL && *String == L'0'){
3624 String++;
3625 }
3626 //
3627 // allow '0x' or '0X', but not 'x' or 'X'
3628 //
3629 if (String != NULL && (*String == L'x' || *String == L'X')) {
3630 if (*(String-1) != L'0') {
3631 //
3632 // we got an x without a preceeding 0
3633 //
3634 return (FALSE);
3635 }
3636 String++;
3637 Hex = TRUE;
3638 } else if (ForceHex) {
3639 Hex = TRUE;
3640 } else {
3641 Hex = FALSE;
3642 }
3643
3644 //
3645 // loop through the remaining characters and use the lib function
3646 //
3647 for ( ; String != NULL && *String != CHAR_NULL && !(StopAtSpace && *String == L' ') ; String++){
jcarsey658bf432014-11-04 22:33:16 +00003648 if (TimeNumbers && (String[0] == L':')) {
3649 continue;
3650 }
jcarseya405b862010-09-14 05:18:09 +00003651 if (Hex) {
3652 if (!ShellIsHexaDecimalDigitCharacter(*String)) {
3653 return (FALSE);
3654 }
3655 } else {
3656 if (!ShellIsDecimalDigitCharacter(*String)) {
3657 return (FALSE);
3658 }
3659 }
3660 }
jcarsey252d9452011-03-25 20:49:53 +00003661
jcarseya405b862010-09-14 05:18:09 +00003662 return (TRUE);
3663}
3664
3665/**
3666 Function to determine if a given filename exists.
3667
3668 @param[in] Name Path to test.
3669
3670 @retval EFI_SUCCESS The Path represents a file.
3671 @retval EFI_NOT_FOUND The Path does not represent a file.
3672 @retval other The path failed to open.
3673**/
3674EFI_STATUS
3675EFIAPI
3676ShellFileExists(
3677 IN CONST CHAR16 *Name
3678 )
3679{
3680 EFI_STATUS Status;
3681 EFI_SHELL_FILE_INFO *List;
3682
3683 ASSERT(Name != NULL);
3684
3685 List = NULL;
3686 Status = ShellOpenFileMetaArg((CHAR16*)Name, EFI_FILE_MODE_READ, &List);
3687 if (EFI_ERROR(Status)) {
3688 return (Status);
3689 }
3690
3691 ShellCloseFileMetaArg(&List);
3692
3693 return (EFI_SUCCESS);
3694}
jcarsey252d9452011-03-25 20:49:53 +00003695
3696/**
darylm503b0934ac2011-11-12 00:35:11 +00003697 Convert a Unicode character to upper case only if
jcarsey252d9452011-03-25 20:49:53 +00003698 it maps to a valid small-case ASCII character.
3699
3700 This internal function only deal with Unicode character
3701 which maps to a valid small-case ASCII character, i.e.
3702 L'a' to L'z'. For other Unicode character, the input character
3703 is returned directly.
3704
3705 @param Char The character to convert.
3706
3707 @retval LowerCharacter If the Char is with range L'a' to L'z'.
3708 @retval Unchanged Otherwise.
3709
3710**/
3711CHAR16
3712EFIAPI
3713InternalShellCharToUpper (
3714 IN CHAR16 Char
3715 )
3716{
3717 if (Char >= L'a' && Char <= L'z') {
3718 return (CHAR16) (Char - (L'a' - L'A'));
3719 }
3720
3721 return Char;
3722}
3723
3724/**
3725 Convert a Unicode character to numerical value.
3726
3727 This internal function only deal with Unicode character
3728 which maps to a valid hexadecimal ASII character, i.e.
darylm503b0934ac2011-11-12 00:35:11 +00003729 L'0' to L'9', L'a' to L'f' or L'A' to L'F'. For other
jcarsey252d9452011-03-25 20:49:53 +00003730 Unicode character, the value returned does not make sense.
3731
3732 @param Char The character to convert.
3733
3734 @return The numerical value converted.
3735
3736**/
3737UINTN
3738EFIAPI
3739InternalShellHexCharToUintn (
3740 IN CHAR16 Char
3741 )
3742{
3743 if (ShellIsDecimalDigitCharacter (Char)) {
3744 return Char - L'0';
3745 }
3746
3747 return (UINTN) (10 + InternalShellCharToUpper (Char) - L'A');
3748}
3749
3750/**
3751 Convert a Null-terminated Unicode hexadecimal string to a value of type UINT64.
3752
Jaben Carsey80f3e342015-07-02 17:26:42 +00003753 This function returns a value of type UINT64 by interpreting the contents
jcarsey252d9452011-03-25 20:49:53 +00003754 of the Unicode string specified by String as a hexadecimal number.
3755 The format of the input Unicode string String is:
3756
3757 [spaces][zeros][x][hexadecimal digits].
3758
3759 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
3760 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix.
3761 If "x" appears in the input string, it must be prefixed with at least one 0.
3762 The function will ignore the pad space, which includes spaces or tab characters,
3763 before [zeros], [x] or [hexadecimal digit]. The running zero before [x] or
3764 [hexadecimal digit] will be ignored. Then, the decoding starts after [x] or the
3765 first valid hexadecimal digit. Then, the function stops at the first character that is
3766 a not a valid hexadecimal character or NULL, whichever one comes first.
3767
3768 If String has only pad spaces, then zero is returned.
3769 If String has no leading pad spaces, leading zeros or valid hexadecimal digits,
3770 then zero is returned.
3771
3772 @param[in] String A pointer to a Null-terminated Unicode string.
3773 @param[out] Value Upon a successful return the value of the conversion.
3774 @param[in] StopAtSpace FALSE to skip spaces.
3775
3776 @retval EFI_SUCCESS The conversion was successful.
3777 @retval EFI_INVALID_PARAMETER A parameter was NULL or invalid.
3778 @retval EFI_DEVICE_ERROR An overflow occured.
3779**/
3780EFI_STATUS
3781EFIAPI
3782InternalShellStrHexToUint64 (
3783 IN CONST CHAR16 *String,
3784 OUT UINT64 *Value,
3785 IN CONST BOOLEAN StopAtSpace
3786 )
3787{
3788 UINT64 Result;
3789
3790 if (String == NULL || StrSize(String) == 0 || Value == NULL) {
3791 return (EFI_INVALID_PARAMETER);
3792 }
darylm503b0934ac2011-11-12 00:35:11 +00003793
jcarsey252d9452011-03-25 20:49:53 +00003794 //
darylm503b0934ac2011-11-12 00:35:11 +00003795 // Ignore the pad spaces (space or tab)
jcarsey252d9452011-03-25 20:49:53 +00003796 //
3797 while ((*String == L' ') || (*String == L'\t')) {
3798 String++;
3799 }
3800
3801 //
3802 // Ignore leading Zeros after the spaces
3803 //
3804 while (*String == L'0') {
3805 String++;
3806 }
3807
3808 if (InternalShellCharToUpper (*String) == L'X') {
3809 if (*(String - 1) != L'0') {
3810 return 0;
3811 }
3812 //
3813 // Skip the 'X'
3814 //
3815 String++;
3816 }
3817
3818 Result = 0;
darylm503b0934ac2011-11-12 00:35:11 +00003819
jcarsey252d9452011-03-25 20:49:53 +00003820 //
Jaben Carsey80f3e342015-07-02 17:26:42 +00003821 // there is a space where there should't be
jcarsey252d9452011-03-25 20:49:53 +00003822 //
Jaben Carsey80f3e342015-07-02 17:26:42 +00003823 if (*String == L' ') {
3824 return (EFI_INVALID_PARAMETER);
jcarsey252d9452011-03-25 20:49:53 +00003825 }
darylm503b0934ac2011-11-12 00:35:11 +00003826
jcarsey252d9452011-03-25 20:49:53 +00003827 while (ShellIsHexaDecimalDigitCharacter (*String)) {
3828 //
darylm503b0934ac2011-11-12 00:35:11 +00003829 // If the Hex Number represented by String overflows according
Jaben Carsey80f3e342015-07-02 17:26:42 +00003830 // to the range defined by UINT64, then return EFI_DEVICE_ERROR.
jcarsey252d9452011-03-25 20:49:53 +00003831 //
3832 if (!(Result <= (RShiftU64((((UINT64) ~0) - InternalShellHexCharToUintn (*String)), 4)))) {
3833// if (!(Result <= ((((UINT64) ~0) - InternalShellHexCharToUintn (*String)) >> 4))) {
3834 return (EFI_DEVICE_ERROR);
3835 }
3836
3837 Result = (LShiftU64(Result, 4));
3838 Result += InternalShellHexCharToUintn (*String);
3839 String++;
3840
3841 //
jcarsey49bd4982012-01-27 18:42:43 +00003842 // stop at spaces if requested
jcarsey252d9452011-03-25 20:49:53 +00003843 //
jcarsey49bd4982012-01-27 18:42:43 +00003844 if (StopAtSpace && *String == L' ') {
3845 break;
jcarsey252d9452011-03-25 20:49:53 +00003846 }
3847 }
3848
3849 *Value = Result;
3850 return (EFI_SUCCESS);
3851}
3852
3853/**
3854 Convert a Null-terminated Unicode decimal string to a value of
3855 type UINT64.
3856
3857 This function returns a value of type UINT64 by interpreting the contents
3858 of the Unicode string specified by String as a decimal number. The format
3859 of the input Unicode string String is:
3860
3861 [spaces] [decimal digits].
3862
3863 The valid decimal digit character is in the range [0-9]. The
3864 function will ignore the pad space, which includes spaces or
3865 tab characters, before [decimal digits]. The running zero in the
3866 beginning of [decimal digits] will be ignored. Then, the function
3867 stops at the first character that is a not a valid decimal character
3868 or a Null-terminator, whichever one comes first.
3869
3870 If String has only pad spaces, then 0 is returned.
3871 If String has no pad spaces or valid decimal digits,
3872 then 0 is returned.
3873
3874 @param[in] String A pointer to a Null-terminated Unicode string.
3875 @param[out] Value Upon a successful return the value of the conversion.
3876 @param[in] StopAtSpace FALSE to skip spaces.
3877
3878 @retval EFI_SUCCESS The conversion was successful.
3879 @retval EFI_INVALID_PARAMETER A parameter was NULL or invalid.
3880 @retval EFI_DEVICE_ERROR An overflow occured.
3881**/
3882EFI_STATUS
3883EFIAPI
3884InternalShellStrDecimalToUint64 (
3885 IN CONST CHAR16 *String,
3886 OUT UINT64 *Value,
3887 IN CONST BOOLEAN StopAtSpace
3888 )
3889{
3890 UINT64 Result;
3891
3892 if (String == NULL || StrSize (String) == 0 || Value == NULL) {
3893 return (EFI_INVALID_PARAMETER);
3894 }
3895
3896 //
3897 // Ignore the pad spaces (space or tab)
3898 //
3899 while ((*String == L' ') || (*String == L'\t')) {
3900 String++;
3901 }
3902
3903 //
3904 // Ignore leading Zeros after the spaces
3905 //
3906 while (*String == L'0') {
3907 String++;
3908 }
3909
3910 Result = 0;
3911
3912 //
Jaben Carsey80f3e342015-07-02 17:26:42 +00003913 // Stop upon space if requested
3914 // (if the whole value was 0)
jcarsey252d9452011-03-25 20:49:53 +00003915 //
Jaben Carsey80f3e342015-07-02 17:26:42 +00003916 if (StopAtSpace && *String == L' ') {
3917 *Value = Result;
3918 return (EFI_SUCCESS);
jcarsey252d9452011-03-25 20:49:53 +00003919 }
Jaben Carsey80f3e342015-07-02 17:26:42 +00003920
jcarsey252d9452011-03-25 20:49:53 +00003921 while (ShellIsDecimalDigitCharacter (*String)) {
3922 //
darylm503b0934ac2011-11-12 00:35:11 +00003923 // If the number represented by String overflows according
Jaben Carsey80f3e342015-07-02 17:26:42 +00003924 // to the range defined by UINT64, then return EFI_DEVICE_ERROR.
jcarsey252d9452011-03-25 20:49:53 +00003925 //
darylm503b0934ac2011-11-12 00:35:11 +00003926
jcarsey252d9452011-03-25 20:49:53 +00003927 if (!(Result <= (DivU64x32((((UINT64) ~0) - (*String - L'0')),10)))) {
3928 return (EFI_DEVICE_ERROR);
3929 }
3930
3931 Result = MultU64x32(Result, 10) + (*String - L'0');
3932 String++;
3933
3934 //
3935 // Stop at spaces if requested
3936 //
3937 if (StopAtSpace && *String == L' ') {
3938 break;
3939 }
3940 }
3941
3942 *Value = Result;
darylm503b0934ac2011-11-12 00:35:11 +00003943
jcarsey252d9452011-03-25 20:49:53 +00003944 return (EFI_SUCCESS);
3945}
3946
3947/**
3948 Function to verify and convert a string to its numerical value.
3949
3950 If Hex it must be preceeded with a 0x, 0X, or has ForceHex set TRUE.
3951
3952 @param[in] String The string to evaluate.
3953 @param[out] Value Upon a successful return the value of the conversion.
3954 @param[in] ForceHex TRUE - always assume hex.
3955 @param[in] StopAtSpace FALSE to skip spaces.
darylm503b0934ac2011-11-12 00:35:11 +00003956
jcarsey252d9452011-03-25 20:49:53 +00003957 @retval EFI_SUCCESS The conversion was successful.
3958 @retval EFI_INVALID_PARAMETER String contained an invalid character.
3959 @retval EFI_NOT_FOUND String was a number, but Value was NULL.
3960**/
3961EFI_STATUS
3962EFIAPI
3963ShellConvertStringToUint64(
3964 IN CONST CHAR16 *String,
3965 OUT UINT64 *Value,
3966 IN CONST BOOLEAN ForceHex,
3967 IN CONST BOOLEAN StopAtSpace
3968 )
3969{
3970 UINT64 RetVal;
3971 CONST CHAR16 *Walker;
3972 EFI_STATUS Status;
3973 BOOLEAN Hex;
3974
3975 Hex = ForceHex;
3976
jcarsey658bf432014-11-04 22:33:16 +00003977 if (!InternalShellIsHexOrDecimalNumber(String, Hex, StopAtSpace, FALSE)) {
jcarsey252d9452011-03-25 20:49:53 +00003978 if (!Hex) {
3979 Hex = TRUE;
jcarsey658bf432014-11-04 22:33:16 +00003980 if (!InternalShellIsHexOrDecimalNumber(String, Hex, StopAtSpace, FALSE)) {
jcarsey252d9452011-03-25 20:49:53 +00003981 return (EFI_INVALID_PARAMETER);
3982 }
3983 } else {
3984 return (EFI_INVALID_PARAMETER);
3985 }
3986 }
3987
3988 //
3989 // Chop off leading spaces
3990 //
3991 for (Walker = String; Walker != NULL && *Walker != CHAR_NULL && *Walker == L' '; Walker++);
3992
3993 //
3994 // make sure we have something left that is numeric.
3995 //
jcarsey658bf432014-11-04 22:33:16 +00003996 if (Walker == NULL || *Walker == CHAR_NULL || !InternalShellIsHexOrDecimalNumber(Walker, Hex, StopAtSpace, FALSE)) {
jcarsey252d9452011-03-25 20:49:53 +00003997 return (EFI_INVALID_PARAMETER);
darylm503b0934ac2011-11-12 00:35:11 +00003998 }
jcarsey252d9452011-03-25 20:49:53 +00003999
4000 //
4001 // do the conversion.
4002 //
4003 if (Hex || StrnCmp(Walker, L"0x", 2) == 0 || StrnCmp(Walker, L"0X", 2) == 0){
4004 Status = InternalShellStrHexToUint64(Walker, &RetVal, StopAtSpace);
4005 } else {
4006 Status = InternalShellStrDecimalToUint64(Walker, &RetVal, StopAtSpace);
4007 }
4008
4009 if (Value == NULL && !EFI_ERROR(Status)) {
4010 return (EFI_NOT_FOUND);
4011 }
4012
4013 if (Value != NULL) {
4014 *Value = RetVal;
4015 }
4016
4017 return (Status);
4018}
4019
4020/**
4021 Function to determin if an entire string is a valid number.
4022
4023 If Hex it must be preceeded with a 0x or has ForceHex, set TRUE.
4024
4025 @param[in] String The string to evaluate.
4026 @param[in] ForceHex TRUE - always assume hex.
4027 @param[in] StopAtSpace TRUE to halt upon finding a space, FALSE to keep going.
4028
4029 @retval TRUE It is all numeric (dec/hex) characters.
4030 @retval FALSE There is a non-numeric character.
4031**/
4032BOOLEAN
4033EFIAPI
4034ShellIsHexOrDecimalNumber (
4035 IN CONST CHAR16 *String,
4036 IN CONST BOOLEAN ForceHex,
4037 IN CONST BOOLEAN StopAtSpace
4038 )
4039{
4040 if (ShellConvertStringToUint64(String, NULL, ForceHex, StopAtSpace) == EFI_NOT_FOUND) {
4041 return (TRUE);
4042 }
4043 return (FALSE);
4044}
jcarsey4d0a4fc2011-07-06 22:28:36 +00004045
4046/**
4047 Function to read a single line from a SHELL_FILE_HANDLE. The \n is not included in the returned
4048 buffer. The returned buffer must be callee freed.
4049
4050 If the position upon start is 0, then the Ascii Boolean will be set. This should be
4051 maintained and not changed for all operations with the same file.
4052
ydong104ff7e372011-09-02 08:05:34 +00004053 @param[in] Handle SHELL_FILE_HANDLE to read from.
4054 @param[in, out] Ascii Boolean value for indicating whether the file is
4055 Ascii (TRUE) or UCS2 (FALSE).
jcarsey4d0a4fc2011-07-06 22:28:36 +00004056
jcarseybeab0fc2011-10-10 17:26:25 +00004057 @return The line of text from the file.
4058 @retval NULL There was not enough memory available.
jcarsey4d0a4fc2011-07-06 22:28:36 +00004059
4060 @sa ShellFileHandleReadLine
4061**/
4062CHAR16*
4063EFIAPI
4064ShellFileHandleReturnLine(
4065 IN SHELL_FILE_HANDLE Handle,
4066 IN OUT BOOLEAN *Ascii
4067 )
4068{
4069 CHAR16 *RetVal;
4070 UINTN Size;
4071 EFI_STATUS Status;
4072
4073 Size = 0;
4074 RetVal = NULL;
4075
4076 Status = ShellFileHandleReadLine(Handle, RetVal, &Size, FALSE, Ascii);
4077 if (Status == EFI_BUFFER_TOO_SMALL) {
4078 RetVal = AllocateZeroPool(Size);
jcarseybeab0fc2011-10-10 17:26:25 +00004079 if (RetVal == NULL) {
4080 return (NULL);
4081 }
jcarsey4d0a4fc2011-07-06 22:28:36 +00004082 Status = ShellFileHandleReadLine(Handle, RetVal, &Size, FALSE, Ascii);
darylm503b0934ac2011-11-12 00:35:11 +00004083
jcarsey4d0a4fc2011-07-06 22:28:36 +00004084 }
Qiu Shumine3b76f32016-02-22 10:21:38 +08004085 if (Status == EFI_END_OF_FILE && RetVal != NULL && *RetVal != CHAR_NULL) {
Qiu Shuminf79d7b62016-02-18 13:12:58 +08004086 Status = EFI_SUCCESS;
4087 }
jcarsey4d0a4fc2011-07-06 22:28:36 +00004088 if (EFI_ERROR(Status) && (RetVal != NULL)) {
4089 FreePool(RetVal);
4090 RetVal = NULL;
4091 }
4092 return (RetVal);
4093}
4094
4095/**
4096 Function to read a single line (up to but not including the \n) from a SHELL_FILE_HANDLE.
4097
4098 If the position upon start is 0, then the Ascii Boolean will be set. This should be
4099 maintained and not changed for all operations with the same file.
4100
Jim Dailey2dda8a12016-02-10 08:53:00 -08004101 NOTE: LINES THAT ARE RETURNED BY THIS FUNCTION ARE UCS2, EVEN IF THE FILE BEING READ
4102 IS IN ASCII FORMAT.
4103
ydong104ff7e372011-09-02 08:05:34 +00004104 @param[in] Handle SHELL_FILE_HANDLE to read from.
Jim Dailey2dda8a12016-02-10 08:53:00 -08004105 @param[in, out] Buffer The pointer to buffer to read into. If this function
4106 returns EFI_SUCCESS, then on output Buffer will
4107 contain a UCS2 string, even if the file being
4108 read is ASCII.
4109 @param[in, out] Size On input, pointer to number of bytes in Buffer.
4110 On output, unchanged unless Buffer is too small
4111 to contain the next line of the file. In that
4112 case Size is set to the number of bytes needed
4113 to hold the next line of the file (as a UCS2
4114 string, even if it is an ASCII file).
ydong104ff7e372011-09-02 08:05:34 +00004115 @param[in] Truncate If the buffer is large enough, this has no effect.
4116 If the buffer is is too small and Truncate is TRUE,
4117 the line will be truncated.
4118 If the buffer is is too small and Truncate is FALSE,
4119 then no read will occur.
jcarsey4d0a4fc2011-07-06 22:28:36 +00004120
ydong104ff7e372011-09-02 08:05:34 +00004121 @param[in, out] Ascii Boolean value for indicating whether the file is
4122 Ascii (TRUE) or UCS2 (FALSE).
jcarsey4d0a4fc2011-07-06 22:28:36 +00004123
4124 @retval EFI_SUCCESS The operation was successful. The line is stored in
4125 Buffer.
jaben carsey9ed21942016-02-08 15:59:04 -08004126 @retval EFI_END_OF_FILE There are no more lines in the file.
jcarsey4d0a4fc2011-07-06 22:28:36 +00004127 @retval EFI_INVALID_PARAMETER Handle was NULL.
4128 @retval EFI_INVALID_PARAMETER Size was NULL.
4129 @retval EFI_BUFFER_TOO_SMALL Size was not large enough to store the line.
4130 Size was updated to the minimum space required.
4131**/
4132EFI_STATUS
4133EFIAPI
4134ShellFileHandleReadLine(
4135 IN SHELL_FILE_HANDLE Handle,
4136 IN OUT CHAR16 *Buffer,
4137 IN OUT UINTN *Size,
4138 IN BOOLEAN Truncate,
4139 IN OUT BOOLEAN *Ascii
4140 )
4141{
4142 EFI_STATUS Status;
4143 CHAR16 CharBuffer;
4144 UINTN CharSize;
4145 UINTN CountSoFar;
4146 UINT64 OriginalFilePosition;
4147
4148
4149 if (Handle == NULL
4150 ||Size == NULL
4151 ){
4152 return (EFI_INVALID_PARAMETER);
4153 }
4154 if (Buffer == NULL) {
4155 ASSERT(*Size == 0);
4156 } else {
4157 *Buffer = CHAR_NULL;
4158 }
4159 gEfiShellProtocol->GetFilePosition(Handle, &OriginalFilePosition);
4160 if (OriginalFilePosition == 0) {
4161 CharSize = sizeof(CHAR16);
4162 Status = gEfiShellProtocol->ReadFile(Handle, &CharSize, &CharBuffer);
4163 ASSERT_EFI_ERROR(Status);
4164 if (CharBuffer == gUnicodeFileTag) {
4165 *Ascii = FALSE;
4166 } else {
4167 *Ascii = TRUE;
4168 gEfiShellProtocol->SetFilePosition(Handle, OriginalFilePosition);
4169 }
4170 }
4171
jaben carsey9ed21942016-02-08 15:59:04 -08004172 if (*Ascii) {
4173 CharSize = sizeof(CHAR8);
4174 } else {
4175 CharSize = sizeof(CHAR16);
4176 }
jcarsey4d0a4fc2011-07-06 22:28:36 +00004177 for (CountSoFar = 0;;CountSoFar++){
4178 CharBuffer = 0;
jcarsey4d0a4fc2011-07-06 22:28:36 +00004179 Status = gEfiShellProtocol->ReadFile(Handle, &CharSize, &CharBuffer);
4180 if ( EFI_ERROR(Status)
4181 || CharSize == 0
4182 || (CharBuffer == L'\n' && !(*Ascii))
4183 || (CharBuffer == '\n' && *Ascii)
4184 ){
jaben carsey9ed21942016-02-08 15:59:04 -08004185 if (CharSize == 0) {
4186 Status = EFI_END_OF_FILE;
4187 }
jcarsey4d0a4fc2011-07-06 22:28:36 +00004188 break;
4189 }
4190 //
4191 // if we have space save it...
4192 //
Jim Dailey1095b432016-02-10 13:17:56 -08004193 if ((CountSoFar+1)*sizeof(CHAR16) < *Size){
jcarsey4d0a4fc2011-07-06 22:28:36 +00004194 ASSERT(Buffer != NULL);
Jim Dailey1095b432016-02-10 13:17:56 -08004195 ((CHAR16*)Buffer)[CountSoFar] = CharBuffer;
4196 ((CHAR16*)Buffer)[CountSoFar+1] = CHAR_NULL;
jcarsey4d0a4fc2011-07-06 22:28:36 +00004197 }
4198 }
4199
4200 //
4201 // if we ran out of space tell when...
4202 //
Jim Dailey1095b432016-02-10 13:17:56 -08004203 if ((CountSoFar+1)*sizeof(CHAR16) > *Size){
4204 *Size = (CountSoFar+1)*sizeof(CHAR16);
4205 if (!Truncate) {
4206 gEfiShellProtocol->SetFilePosition(Handle, OriginalFilePosition);
4207 } else {
4208 DEBUG((DEBUG_WARN, "The line was truncated in ShellFileHandleReadLine"));
jcarsey4d0a4fc2011-07-06 22:28:36 +00004209 }
Jim Dailey1095b432016-02-10 13:17:56 -08004210 return (EFI_BUFFER_TOO_SMALL);
4211 }
4212 while(Buffer[StrLen(Buffer)-1] == L'\r') {
4213 Buffer[StrLen(Buffer)-1] = CHAR_NULL;
jcarsey4d0a4fc2011-07-06 22:28:36 +00004214 }
4215
4216 return (Status);
4217}
jcarseyfb5278e2013-02-20 18:21:14 +00004218
4219/**
jcarsey365aa982013-03-04 21:54:02 +00004220 Function to print help file / man page content in the spec from the UEFI Shell protocol GetHelpText function.
4221
4222 @param[in] CommandToGetHelpOn Pointer to a string containing the command name of help file to be printed.
4223 @param[in] SectionToGetHelpOn Pointer to the section specifier(s).
4224 @param[in] PrintCommandText If TRUE, prints the command followed by the help content, otherwise prints
4225 the help content only.
4226 @retval EFI_DEVICE_ERROR The help data format was incorrect.
4227 @retval EFI_NOT_FOUND The help data could not be found.
4228 @retval EFI_SUCCESS The operation was successful.
4229**/
4230EFI_STATUS
4231EFIAPI
4232ShellPrintHelp (
4233 IN CONST CHAR16 *CommandToGetHelpOn,
4234 IN CONST CHAR16 *SectionToGetHelpOn,
4235 IN BOOLEAN PrintCommandText
4236 )
4237{
4238 EFI_STATUS Status;
4239 CHAR16 *OutText;
4240
4241 OutText = NULL;
4242
4243 //
4244 // Get the string to print based
4245 //
4246 Status = gEfiShellProtocol->GetHelpText (CommandToGetHelpOn, SectionToGetHelpOn, &OutText);
4247
4248 //
4249 // make sure we got a valid string
4250 //
4251 if (EFI_ERROR(Status)){
4252 return Status;
4253 }
4254 if (OutText == NULL || StrLen(OutText) == 0) {
4255 return EFI_NOT_FOUND;
4256 }
4257
4258 //
4259 // Chop off trailing stuff we dont need
4260 //
4261 while (OutText[StrLen(OutText)-1] == L'\r' || OutText[StrLen(OutText)-1] == L'\n' || OutText[StrLen(OutText)-1] == L' ') {
4262 OutText[StrLen(OutText)-1] = CHAR_NULL;
4263 }
4264
4265 //
4266 // Print this out to the console
4267 //
4268 if (PrintCommandText) {
4269 ShellPrintEx(-1, -1, L"%H%-14s%N- %s\r\n", CommandToGetHelpOn, OutText);
4270 } else {
4271 ShellPrintEx(-1, -1, L"%N%s\r\n", OutText);
4272 }
4273
4274 SHELL_FREE_NON_NULL(OutText);
4275
4276 return EFI_SUCCESS;
4277}
4278
4279/**
jcarseyfb5278e2013-02-20 18:21:14 +00004280 Function to delete a file by name
4281
4282 @param[in] FileName Pointer to file name to delete.
4283
4284 @retval EFI_SUCCESS the file was deleted sucessfully
4285 @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not
4286 deleted
4287 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
4288 @retval EFI_NOT_FOUND The specified file could not be found on the
4289 device or the file system could not be found
4290 on the device.
4291 @retval EFI_NO_MEDIA The device has no medium.
4292 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
4293 medium is no longer supported.
4294 @retval EFI_DEVICE_ERROR The device reported an error.
4295 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
4296 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
4297 @retval EFI_ACCESS_DENIED The file was opened read only.
4298 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
4299 file.
4300 @retval other The file failed to open
4301**/
4302EFI_STATUS
4303EFIAPI
4304ShellDeleteFileByName(
4305 IN CONST CHAR16 *FileName
4306 )
4307{
4308 EFI_STATUS Status;
4309 SHELL_FILE_HANDLE FileHandle;
4310
4311 Status = ShellFileExists(FileName);
4312
4313 if (Status == EFI_SUCCESS){
4314 Status = ShellOpenFileByName(FileName, &FileHandle, EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE, 0x0);
4315 if (Status == EFI_SUCCESS){
4316 Status = ShellDeleteFile(&FileHandle);
4317 }
4318 }
4319
4320 return(Status);
4321
4322}
Qiu Shumin0960ba12014-09-17 07:58:31 +00004323
4324/**
4325 Cleans off all the quotes in the string.
4326
4327 @param[in] OriginalString pointer to the string to be cleaned.
4328 @param[out] CleanString The new string with all quotes removed.
4329 Memory allocated in the function and free
4330 by caller.
4331
4332 @retval EFI_SUCCESS The operation was successful.
4333**/
4334EFI_STATUS
4335EFIAPI
4336InternalShellStripQuotes (
4337 IN CONST CHAR16 *OriginalString,
4338 OUT CHAR16 **CleanString
4339 )
4340{
4341 CHAR16 *Walker;
4342
4343 if (OriginalString == NULL || CleanString == NULL) {
4344 return EFI_INVALID_PARAMETER;
4345 }
4346
4347 *CleanString = AllocateCopyPool (StrSize (OriginalString), OriginalString);
4348 if (*CleanString == NULL) {
4349 return EFI_OUT_OF_RESOURCES;
4350 }
4351
4352 for (Walker = *CleanString; Walker != NULL && *Walker != CHAR_NULL ; Walker++) {
4353 if (*Walker == L'\"') {
4354 CopyMem(Walker, Walker+1, StrSize(Walker) - sizeof(Walker[0]));
4355 }
4356 }
4357
4358 return EFI_SUCCESS;
4359}
4360