blob: 6e0f61130ff463f3bc2860245eb0944961770863 [file] [log] [blame]
jcarsey94b17fa2009-05-07 18:46:18 +00001/** @file
2 Provides interface to shell functionality for shell commands and applications.
3
jaben carsey21a86a72015-01-13 22:16:41 +00004 Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.<BR>
jcarsey1e6e84c2010-01-25 20:05:08 +00005 This program and the accompanying materials
jcarseyb3011f42010-01-11 21:49:04 +00006 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
jcarsey94b17fa2009-05-07 18:46:18 +00009
jcarseyb3011f42010-01-11 21:49:04 +000010 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
jcarsey94b17fa2009-05-07 18:46:18 +000012
13**/
14
jcarseyb1f95a02009-06-16 00:23:19 +000015#include "UefiShellLib.h"
jcarseya405b862010-09-14 05:18:09 +000016#include <ShellBase.h>
jcarsey252d9452011-03-25 20:49:53 +000017#include <Library/SortLib.h>
Laszlo Ersek3fe23dc2015-01-14 16:25:48 +000018#include <Library/BaseLib.h>
jcarseyd2b45642009-05-11 18:02:16 +000019
jcarsey94b17fa2009-05-07 18:46:18 +000020#define FIND_XXXXX_FILE_BUFFER_SIZE (SIZE_OF_EFI_FILE_INFO + MAX_FILE_NAME_LEN)
21
jcarseyd2b45642009-05-11 18:02:16 +000022//
jcarseya405b862010-09-14 05:18:09 +000023// globals...
jcarseyd2b45642009-05-11 18:02:16 +000024//
25SHELL_PARAM_ITEM EmptyParamList[] = {
26 {NULL, TypeMax}
27 };
jcarseya405b862010-09-14 05:18:09 +000028SHELL_PARAM_ITEM SfoParamList[] = {
29 {L"-sfo", TypeFlag},
30 {NULL, TypeMax}
31 };
32EFI_SHELL_ENVIRONMENT2 *mEfiShellEnvironment2;
33EFI_SHELL_INTERFACE *mEfiShellInterface;
jcarsey366f81a2011-06-27 21:04:22 +000034EFI_SHELL_PROTOCOL *gEfiShellProtocol;
35EFI_SHELL_PARAMETERS_PROTOCOL *gEfiShellParametersProtocol;
jcarseya405b862010-09-14 05:18:09 +000036EFI_HANDLE mEfiShellEnvironment2Handle;
37FILE_HANDLE_FUNCTION_MAP FileFunctionMap;
jcarseyb3011f42010-01-11 21:49:04 +000038
jcarsey2247dde2009-11-09 18:08:58 +000039/**
40 Check if a Unicode character is a hexadecimal character.
41
jcarsey1e6e84c2010-01-25 20:05:08 +000042 This internal function checks if a Unicode character is a
jcarseya405b862010-09-14 05:18:09 +000043 numeric character. The valid hexadecimal characters are
jcarsey2247dde2009-11-09 18:08:58 +000044 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.
45
jcarsey2247dde2009-11-09 18:08:58 +000046 @param Char The character to check against.
47
48 @retval TRUE If the Char is a hexadecmial character.
49 @retval FALSE If the Char is not a hexadecmial character.
50
51**/
52BOOLEAN
53EFIAPI
jcarsey969c7832010-01-13 16:46:33 +000054ShellIsHexaDecimalDigitCharacter (
jcarsey2247dde2009-11-09 18:08:58 +000055 IN CHAR16 Char
jcarseya405b862010-09-14 05:18:09 +000056 )
57{
jcarsey2247dde2009-11-09 18:08:58 +000058 return (BOOLEAN) ((Char >= L'0' && Char <= L'9') || (Char >= L'A' && Char <= L'F') || (Char >= L'a' && Char <= L'f'));
59}
jcarsey94b17fa2009-05-07 18:46:18 +000060
61/**
jcarseya405b862010-09-14 05:18:09 +000062 Check if a Unicode character is a decimal character.
63
64 This internal function checks if a Unicode character is a
65 decimal character. The valid characters are
66 L'0' to L'9'.
67
68
69 @param Char The character to check against.
70
71 @retval TRUE If the Char is a hexadecmial character.
72 @retval FALSE If the Char is not a hexadecmial character.
73
74**/
75BOOLEAN
76EFIAPI
77ShellIsDecimalDigitCharacter (
78 IN CHAR16 Char
79 )
80{
81 return (BOOLEAN) (Char >= L'0' && Char <= L'9');
82}
83
84/**
85 Helper function to find ShellEnvironment2 for constructor.
86
87 @param[in] ImageHandle A copy of the calling image's handle.
jcarseybeab0fc2011-10-10 17:26:25 +000088
89 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.
jcarsey94b17fa2009-05-07 18:46:18 +000090**/
91EFI_STATUS
92EFIAPI
93ShellFindSE2 (
94 IN EFI_HANDLE ImageHandle
jcarseya405b862010-09-14 05:18:09 +000095 )
96{
jcarsey94b17fa2009-05-07 18:46:18 +000097 EFI_STATUS Status;
98 EFI_HANDLE *Buffer;
99 UINTN BufferSize;
100 UINTN HandleIndex;
101
102 BufferSize = 0;
103 Buffer = NULL;
jcarsey1e6e84c2010-01-25 20:05:08 +0000104 Status = gBS->OpenProtocol(ImageHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000105 &gEfiShellEnvironment2Guid,
106 (VOID **)&mEfiShellEnvironment2,
107 ImageHandle,
108 NULL,
109 EFI_OPEN_PROTOCOL_GET_PROTOCOL
jcarseya405b862010-09-14 05:18:09 +0000110 );
jcarsey94b17fa2009-05-07 18:46:18 +0000111 //
112 // look for the mEfiShellEnvironment2 protocol at a higher level
113 //
jcarseya405b862010-09-14 05:18:09 +0000114 if (EFI_ERROR (Status) || !(CompareGuid (&mEfiShellEnvironment2->SESGuid, &gEfiShellEnvironment2ExtGuid))){
jcarsey94b17fa2009-05-07 18:46:18 +0000115 //
116 // figure out how big of a buffer we need.
117 //
118 Status = gBS->LocateHandle (ByProtocol,
119 &gEfiShellEnvironment2Guid,
120 NULL, // ignored for ByProtocol
121 &BufferSize,
122 Buffer
jcarseya405b862010-09-14 05:18:09 +0000123 );
jcarsey2247dde2009-11-09 18:08:58 +0000124 //
125 // maybe it's not there???
126 //
127 if (Status == EFI_BUFFER_TOO_SMALL) {
jcarsey252d9452011-03-25 20:49:53 +0000128 Buffer = (EFI_HANDLE*)AllocateZeroPool(BufferSize);
jcarseybeab0fc2011-10-10 17:26:25 +0000129 if (Buffer == NULL) {
130 return (EFI_OUT_OF_RESOURCES);
131 }
jcarsey2247dde2009-11-09 18:08:58 +0000132 Status = gBS->LocateHandle (ByProtocol,
133 &gEfiShellEnvironment2Guid,
134 NULL, // ignored for ByProtocol
135 &BufferSize,
136 Buffer
jcarseya405b862010-09-14 05:18:09 +0000137 );
jcarsey2247dde2009-11-09 18:08:58 +0000138 }
jcarsey1cd45e72010-01-29 15:07:44 +0000139 if (!EFI_ERROR (Status) && Buffer != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +0000140 //
141 // now parse the list of returned handles
142 //
143 Status = EFI_NOT_FOUND;
144 for (HandleIndex = 0; HandleIndex < (BufferSize/sizeof(Buffer[0])); HandleIndex++) {
jcarsey1e6e84c2010-01-25 20:05:08 +0000145 Status = gBS->OpenProtocol(Buffer[HandleIndex],
jcarsey94b17fa2009-05-07 18:46:18 +0000146 &gEfiShellEnvironment2Guid,
147 (VOID **)&mEfiShellEnvironment2,
148 ImageHandle,
149 NULL,
150 EFI_OPEN_PROTOCOL_GET_PROTOCOL
jcarseya405b862010-09-14 05:18:09 +0000151 );
152 if (CompareGuid (&mEfiShellEnvironment2->SESGuid, &gEfiShellEnvironment2ExtGuid)) {
jcarsey94b17fa2009-05-07 18:46:18 +0000153 mEfiShellEnvironment2Handle = Buffer[HandleIndex];
154 Status = EFI_SUCCESS;
155 break;
156 }
157 }
158 }
159 }
160 if (Buffer != NULL) {
161 FreePool (Buffer);
162 }
163 return (Status);
164}
165
jcarsey252d9452011-03-25 20:49:53 +0000166/**
darylm503b0934ac2011-11-12 00:35:11 +0000167 Function to do most of the work of the constructor. Allows for calling
jcarseya405b862010-09-14 05:18:09 +0000168 multiple times without complete re-initialization.
169
170 @param[in] ImageHandle A copy of the ImageHandle.
171 @param[in] SystemTable A pointer to the SystemTable for the application.
jcarsey252d9452011-03-25 20:49:53 +0000172
173 @retval EFI_SUCCESS The operationw as successful.
jcarseya405b862010-09-14 05:18:09 +0000174**/
jcarsey94b17fa2009-05-07 18:46:18 +0000175EFI_STATUS
176EFIAPI
jcarseyd2b45642009-05-11 18:02:16 +0000177ShellLibConstructorWorker (
jcarsey94b17fa2009-05-07 18:46:18 +0000178 IN EFI_HANDLE ImageHandle,
179 IN EFI_SYSTEM_TABLE *SystemTable
jcarseya405b862010-09-14 05:18:09 +0000180 )
181{
182 EFI_STATUS Status;
jcarseyecd3d592009-12-07 18:05:00 +0000183
jcarsey94b17fa2009-05-07 18:46:18 +0000184 //
185 // UEFI 2.0 shell interfaces (used preferentially)
186 //
jcarseya405b862010-09-14 05:18:09 +0000187 Status = gBS->OpenProtocol(
188 ImageHandle,
189 &gEfiShellProtocolGuid,
jcarsey366f81a2011-06-27 21:04:22 +0000190 (VOID **)&gEfiShellProtocol,
jcarseya405b862010-09-14 05:18:09 +0000191 ImageHandle,
192 NULL,
193 EFI_OPEN_PROTOCOL_GET_PROTOCOL
194 );
jcarsey94b17fa2009-05-07 18:46:18 +0000195 if (EFI_ERROR(Status)) {
jcarseya405b862010-09-14 05:18:09 +0000196 //
197 // Search for the shell protocol
198 //
199 Status = gBS->LocateProtocol(
200 &gEfiShellProtocolGuid,
201 NULL,
jcarsey366f81a2011-06-27 21:04:22 +0000202 (VOID **)&gEfiShellProtocol
jcarseya405b862010-09-14 05:18:09 +0000203 );
204 if (EFI_ERROR(Status)) {
jcarsey366f81a2011-06-27 21:04:22 +0000205 gEfiShellProtocol = NULL;
jcarseya405b862010-09-14 05:18:09 +0000206 }
jcarsey94b17fa2009-05-07 18:46:18 +0000207 }
jcarseya405b862010-09-14 05:18:09 +0000208 Status = gBS->OpenProtocol(
209 ImageHandle,
210 &gEfiShellParametersProtocolGuid,
jcarsey366f81a2011-06-27 21:04:22 +0000211 (VOID **)&gEfiShellParametersProtocol,
jcarseya405b862010-09-14 05:18:09 +0000212 ImageHandle,
213 NULL,
214 EFI_OPEN_PROTOCOL_GET_PROTOCOL
215 );
jcarsey94b17fa2009-05-07 18:46:18 +0000216 if (EFI_ERROR(Status)) {
jcarsey366f81a2011-06-27 21:04:22 +0000217 gEfiShellParametersProtocol = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +0000218 }
219
jcarsey366f81a2011-06-27 21:04:22 +0000220 if (gEfiShellParametersProtocol == NULL || gEfiShellProtocol == NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +0000221 //
222 // Moved to seperate function due to complexity
223 //
224 Status = ShellFindSE2(ImageHandle);
225
226 if (EFI_ERROR(Status)) {
227 DEBUG((DEBUG_ERROR, "Status: 0x%08x\r\n", Status));
228 mEfiShellEnvironment2 = NULL;
229 }
jcarsey1e6e84c2010-01-25 20:05:08 +0000230 Status = gBS->OpenProtocol(ImageHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000231 &gEfiShellInterfaceGuid,
232 (VOID **)&mEfiShellInterface,
233 ImageHandle,
234 NULL,
235 EFI_OPEN_PROTOCOL_GET_PROTOCOL
jcarseya405b862010-09-14 05:18:09 +0000236 );
jcarsey94b17fa2009-05-07 18:46:18 +0000237 if (EFI_ERROR(Status)) {
238 mEfiShellInterface = NULL;
239 }
240 }
jcarseyc9d92df2010-02-03 15:37:54 +0000241
jcarsey94b17fa2009-05-07 18:46:18 +0000242 //
243 // only success getting 2 of either the old or new, but no 1/2 and 1/2
244 //
jcarsey1e6e84c2010-01-25 20:05:08 +0000245 if ((mEfiShellEnvironment2 != NULL && mEfiShellInterface != NULL) ||
jcarsey366f81a2011-06-27 21:04:22 +0000246 (gEfiShellProtocol != NULL && gEfiShellParametersProtocol != NULL) ) {
247 if (gEfiShellProtocol != NULL) {
248 FileFunctionMap.GetFileInfo = gEfiShellProtocol->GetFileInfo;
249 FileFunctionMap.SetFileInfo = gEfiShellProtocol->SetFileInfo;
250 FileFunctionMap.ReadFile = gEfiShellProtocol->ReadFile;
251 FileFunctionMap.WriteFile = gEfiShellProtocol->WriteFile;
252 FileFunctionMap.CloseFile = gEfiShellProtocol->CloseFile;
253 FileFunctionMap.DeleteFile = gEfiShellProtocol->DeleteFile;
254 FileFunctionMap.GetFilePosition = gEfiShellProtocol->GetFilePosition;
255 FileFunctionMap.SetFilePosition = gEfiShellProtocol->SetFilePosition;
256 FileFunctionMap.FlushFile = gEfiShellProtocol->FlushFile;
257 FileFunctionMap.GetFileSize = gEfiShellProtocol->GetFileSize;
jcarseyd2b45642009-05-11 18:02:16 +0000258 } else {
jcarseya405b862010-09-14 05:18:09 +0000259 FileFunctionMap.GetFileInfo = (EFI_SHELL_GET_FILE_INFO)FileHandleGetInfo;
260 FileFunctionMap.SetFileInfo = (EFI_SHELL_SET_FILE_INFO)FileHandleSetInfo;
261 FileFunctionMap.ReadFile = (EFI_SHELL_READ_FILE)FileHandleRead;
262 FileFunctionMap.WriteFile = (EFI_SHELL_WRITE_FILE)FileHandleWrite;
263 FileFunctionMap.CloseFile = (EFI_SHELL_CLOSE_FILE)FileHandleClose;
264 FileFunctionMap.DeleteFile = (EFI_SHELL_DELETE_FILE)FileHandleDelete;
265 FileFunctionMap.GetFilePosition = (EFI_SHELL_GET_FILE_POSITION)FileHandleGetPosition;
266 FileFunctionMap.SetFilePosition = (EFI_SHELL_SET_FILE_POSITION)FileHandleSetPosition;
267 FileFunctionMap.FlushFile = (EFI_SHELL_FLUSH_FILE)FileHandleFlush;
268 FileFunctionMap.GetFileSize = (EFI_SHELL_GET_FILE_SIZE)FileHandleGetSize;
jcarseyd2b45642009-05-11 18:02:16 +0000269 }
jcarsey94b17fa2009-05-07 18:46:18 +0000270 return (EFI_SUCCESS);
271 }
272 return (EFI_NOT_FOUND);
273}
jcarseyd2b45642009-05-11 18:02:16 +0000274/**
275 Constructor for the Shell library.
276
277 Initialize the library and determine if the underlying is a UEFI Shell 2.0 or an EFI shell.
278
279 @param ImageHandle the image handle of the process
280 @param SystemTable the EFI System Table pointer
281
282 @retval EFI_SUCCESS the initialization was complete sucessfully
283 @return others an error ocurred during initialization
284**/
285EFI_STATUS
286EFIAPI
287ShellLibConstructor (
288 IN EFI_HANDLE ImageHandle,
289 IN EFI_SYSTEM_TABLE *SystemTable
jcarseya405b862010-09-14 05:18:09 +0000290 )
291{
jcarseyd2b45642009-05-11 18:02:16 +0000292 mEfiShellEnvironment2 = NULL;
jcarsey366f81a2011-06-27 21:04:22 +0000293 gEfiShellProtocol = NULL;
294 gEfiShellParametersProtocol = NULL;
jcarseyd2b45642009-05-11 18:02:16 +0000295 mEfiShellInterface = NULL;
296 mEfiShellEnvironment2Handle = NULL;
297
jcarseyd2b45642009-05-11 18:02:16 +0000298 //
299 // verify that auto initialize is not set false
jcarsey1e6e84c2010-01-25 20:05:08 +0000300 //
jcarseyd2b45642009-05-11 18:02:16 +0000301 if (PcdGetBool(PcdShellLibAutoInitialize) == 0) {
302 return (EFI_SUCCESS);
303 }
jcarsey1e6e84c2010-01-25 20:05:08 +0000304
jcarseyd2b45642009-05-11 18:02:16 +0000305 return (ShellLibConstructorWorker(ImageHandle, SystemTable));
306}
jcarsey94b17fa2009-05-07 18:46:18 +0000307
308/**
jcarseya405b862010-09-14 05:18:09 +0000309 Destructor for the library. free any resources.
310
311 @param[in] ImageHandle A copy of the ImageHandle.
312 @param[in] SystemTable A pointer to the SystemTable for the application.
313
314 @retval EFI_SUCCESS The operation was successful.
315 @return An error from the CloseProtocol function.
jcarsey94b17fa2009-05-07 18:46:18 +0000316**/
317EFI_STATUS
318EFIAPI
319ShellLibDestructor (
320 IN EFI_HANDLE ImageHandle,
321 IN EFI_SYSTEM_TABLE *SystemTable
jcarseya405b862010-09-14 05:18:09 +0000322 )
323{
jcarsey94b17fa2009-05-07 18:46:18 +0000324 if (mEfiShellEnvironment2 != NULL) {
325 gBS->CloseProtocol(mEfiShellEnvironment2Handle==NULL?ImageHandle:mEfiShellEnvironment2Handle,
326 &gEfiShellEnvironment2Guid,
327 ImageHandle,
328 NULL);
jcarseyd2b45642009-05-11 18:02:16 +0000329 mEfiShellEnvironment2 = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +0000330 }
331 if (mEfiShellInterface != NULL) {
332 gBS->CloseProtocol(ImageHandle,
333 &gEfiShellInterfaceGuid,
334 ImageHandle,
jcarsey1e6e84c2010-01-25 20:05:08 +0000335 NULL);
jcarseyd2b45642009-05-11 18:02:16 +0000336 mEfiShellInterface = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +0000337 }
jcarsey366f81a2011-06-27 21:04:22 +0000338 if (gEfiShellProtocol != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +0000339 gBS->CloseProtocol(ImageHandle,
340 &gEfiShellProtocolGuid,
341 ImageHandle,
jcarsey1e6e84c2010-01-25 20:05:08 +0000342 NULL);
jcarsey366f81a2011-06-27 21:04:22 +0000343 gEfiShellProtocol = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +0000344 }
jcarsey366f81a2011-06-27 21:04:22 +0000345 if (gEfiShellParametersProtocol != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +0000346 gBS->CloseProtocol(ImageHandle,
347 &gEfiShellParametersProtocolGuid,
348 ImageHandle,
jcarsey1e6e84c2010-01-25 20:05:08 +0000349 NULL);
jcarsey366f81a2011-06-27 21:04:22 +0000350 gEfiShellParametersProtocol = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +0000351 }
jcarseyd2b45642009-05-11 18:02:16 +0000352 mEfiShellEnvironment2Handle = NULL;
jcarseyecd3d592009-12-07 18:05:00 +0000353
jcarsey94b17fa2009-05-07 18:46:18 +0000354 return (EFI_SUCCESS);
355}
jcarseyd2b45642009-05-11 18:02:16 +0000356
357/**
358 This function causes the shell library to initialize itself. If the shell library
359 is already initialized it will de-initialize all the current protocol poitners and
360 re-populate them again.
361
362 When the library is used with PcdShellLibAutoInitialize set to true this function
363 will return EFI_SUCCESS and perform no actions.
364
365 This function is intended for internal access for shell commands only.
366
367 @retval EFI_SUCCESS the initialization was complete sucessfully
368
369**/
370EFI_STATUS
371EFIAPI
372ShellInitialize (
jcarseya405b862010-09-14 05:18:09 +0000373 )
374{
jcarseyd2b45642009-05-11 18:02:16 +0000375 //
376 // if auto initialize is not false then skip
377 //
378 if (PcdGetBool(PcdShellLibAutoInitialize) != 0) {
379 return (EFI_SUCCESS);
380 }
381
382 //
383 // deinit the current stuff
384 //
385 ASSERT_EFI_ERROR(ShellLibDestructor(gImageHandle, gST));
386
387 //
388 // init the new stuff
389 //
390 return (ShellLibConstructorWorker(gImageHandle, gST));
391}
392
jcarsey94b17fa2009-05-07 18:46:18 +0000393/**
jcarsey1e6e84c2010-01-25 20:05:08 +0000394 This function will retrieve the information about the file for the handle
jcarsey94b17fa2009-05-07 18:46:18 +0000395 specified and store it in allocated pool memory.
396
jcarsey1e6e84c2010-01-25 20:05:08 +0000397 This function allocates a buffer to store the file's information. It is the
qhuang869817bf2009-05-20 14:42:48 +0000398 caller's responsibility to free the buffer
jcarsey94b17fa2009-05-07 18:46:18 +0000399
jcarsey1e6e84c2010-01-25 20:05:08 +0000400 @param FileHandle The file handle of the file for which information is
jcarsey94b17fa2009-05-07 18:46:18 +0000401 being requested.
402
403 @retval NULL information could not be retrieved.
404
405 @return the information about the file
406**/
407EFI_FILE_INFO*
408EFIAPI
409ShellGetFileInfo (
jcarseya405b862010-09-14 05:18:09 +0000410 IN SHELL_FILE_HANDLE FileHandle
411 )
412{
jcarseyd2b45642009-05-11 18:02:16 +0000413 return (FileFunctionMap.GetFileInfo(FileHandle));
jcarsey94b17fa2009-05-07 18:46:18 +0000414}
415
416/**
jcarseya405b862010-09-14 05:18:09 +0000417 This function sets the information about the file for the opened handle
jcarsey94b17fa2009-05-07 18:46:18 +0000418 specified.
419
jcarseya405b862010-09-14 05:18:09 +0000420 @param[in] FileHandle The file handle of the file for which information
421 is being set.
jcarsey94b17fa2009-05-07 18:46:18 +0000422
jcarseya405b862010-09-14 05:18:09 +0000423 @param[in] FileInfo The information to set.
jcarsey94b17fa2009-05-07 18:46:18 +0000424
darylm503b0934ac2011-11-12 00:35:11 +0000425 @retval EFI_SUCCESS The information was set.
jcarseya405b862010-09-14 05:18:09 +0000426 @retval EFI_INVALID_PARAMETER A parameter was out of range or invalid.
427 @retval EFI_UNSUPPORTED The FileHandle does not support FileInfo.
darylm503b0934ac2011-11-12 00:35:11 +0000428 @retval EFI_NO_MEDIA The device has no medium.
429 @retval EFI_DEVICE_ERROR The device reported an error.
430 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
431 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
jcarseya405b862010-09-14 05:18:09 +0000432 @retval EFI_ACCESS_DENIED The file was opened read only.
433 @retval EFI_VOLUME_FULL The volume is full.
jcarsey94b17fa2009-05-07 18:46:18 +0000434**/
435EFI_STATUS
436EFIAPI
437ShellSetFileInfo (
darylm503b0934ac2011-11-12 00:35:11 +0000438 IN SHELL_FILE_HANDLE FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000439 IN EFI_FILE_INFO *FileInfo
jcarseya405b862010-09-14 05:18:09 +0000440 )
441{
jcarseyd2b45642009-05-11 18:02:16 +0000442 return (FileFunctionMap.SetFileInfo(FileHandle, FileInfo));
jcarsey1e6e84c2010-01-25 20:05:08 +0000443}
444
jcarsey94b17fa2009-05-07 18:46:18 +0000445 /**
446 This function will open a file or directory referenced by DevicePath.
447
jcarsey1e6e84c2010-01-25 20:05:08 +0000448 This function opens a file with the open mode according to the file path. The
jcarsey94b17fa2009-05-07 18:46:18 +0000449 Attributes is valid only for EFI_FILE_MODE_CREATE.
450
darylm503b0934ac2011-11-12 00:35:11 +0000451 @param FilePath on input the device path to the file. On output
jcarsey94b17fa2009-05-07 18:46:18 +0000452 the remaining device path.
darylm503b0934ac2011-11-12 00:35:11 +0000453 @param DeviceHandle pointer to the system device handle.
454 @param FileHandle pointer to the file handle.
455 @param OpenMode the mode to open the file with.
456 @param Attributes the file's file attributes.
jcarsey94b17fa2009-05-07 18:46:18 +0000457
darylm503b0934ac2011-11-12 00:35:11 +0000458 @retval EFI_SUCCESS The information was set.
459 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
460 @retval EFI_UNSUPPORTED Could not open the file path.
461 @retval EFI_NOT_FOUND The specified file could not be found on the
jcarsey1e6e84c2010-01-25 20:05:08 +0000462 device or the file system could not be found on
jcarsey94b17fa2009-05-07 18:46:18 +0000463 the device.
darylm503b0934ac2011-11-12 00:35:11 +0000464 @retval EFI_NO_MEDIA The device has no medium.
465 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
jcarsey94b17fa2009-05-07 18:46:18 +0000466 medium is no longer supported.
darylm503b0934ac2011-11-12 00:35:11 +0000467 @retval EFI_DEVICE_ERROR The device reported an error.
468 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
469 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
470 @retval EFI_ACCESS_DENIED The file was opened read only.
471 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
jcarsey94b17fa2009-05-07 18:46:18 +0000472 file.
darylm503b0934ac2011-11-12 00:35:11 +0000473 @retval EFI_VOLUME_FULL The volume is full.
jcarsey94b17fa2009-05-07 18:46:18 +0000474**/
475EFI_STATUS
476EFIAPI
477ShellOpenFileByDevicePath(
darylm503b0934ac2011-11-12 00:35:11 +0000478 IN OUT EFI_DEVICE_PATH_PROTOCOL **FilePath,
479 OUT EFI_HANDLE *DeviceHandle,
jcarseya405b862010-09-14 05:18:09 +0000480 OUT SHELL_FILE_HANDLE *FileHandle,
darylm503b0934ac2011-11-12 00:35:11 +0000481 IN UINT64 OpenMode,
482 IN UINT64 Attributes
jcarseya405b862010-09-14 05:18:09 +0000483 )
484{
485 CHAR16 *FileName;
486 EFI_STATUS Status;
jcarsey94b17fa2009-05-07 18:46:18 +0000487 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *EfiSimpleFileSystemProtocol;
jcarseya405b862010-09-14 05:18:09 +0000488 EFI_FILE_PROTOCOL *Handle1;
489 EFI_FILE_PROTOCOL *Handle2;
ydong100b6cb332013-01-25 02:00:22 +0000490 CHAR16 *FnafPathName;
491 UINTN PathLen;
jcarsey94b17fa2009-05-07 18:46:18 +0000492
jcarsey92a54472011-06-27 20:33:13 +0000493 if (FilePath == NULL || FileHandle == NULL || DeviceHandle == NULL) {
494 return (EFI_INVALID_PARAMETER);
495 }
496
jcarsey1e6e84c2010-01-25 20:05:08 +0000497 //
jcarsey94b17fa2009-05-07 18:46:18 +0000498 // which shell interface should we use
499 //
jcarsey366f81a2011-06-27 21:04:22 +0000500 if (gEfiShellProtocol != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +0000501 //
502 // use UEFI Shell 2.0 method.
503 //
jcarsey366f81a2011-06-27 21:04:22 +0000504 FileName = gEfiShellProtocol->GetFilePathFromDevicePath(*FilePath);
jcarsey94b17fa2009-05-07 18:46:18 +0000505 if (FileName == NULL) {
506 return (EFI_INVALID_PARAMETER);
507 }
508 Status = ShellOpenFileByName(FileName, FileHandle, OpenMode, Attributes);
509 FreePool(FileName);
510 return (Status);
jcarsey1e6e84c2010-01-25 20:05:08 +0000511 }
jcarseyd2b45642009-05-11 18:02:16 +0000512
513
514 //
515 // use old shell method.
516 //
jcarsey1e6e84c2010-01-25 20:05:08 +0000517 Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid,
518 FilePath,
jcarseyd2b45642009-05-11 18:02:16 +0000519 DeviceHandle);
520 if (EFI_ERROR (Status)) {
521 return Status;
522 }
523 Status = gBS->OpenProtocol(*DeviceHandle,
524 &gEfiSimpleFileSystemProtocolGuid,
jcarseyb1f95a02009-06-16 00:23:19 +0000525 (VOID**)&EfiSimpleFileSystemProtocol,
jcarseyd2b45642009-05-11 18:02:16 +0000526 gImageHandle,
527 NULL,
528 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
529 if (EFI_ERROR (Status)) {
530 return Status;
531 }
jcarseya405b862010-09-14 05:18:09 +0000532 Status = EfiSimpleFileSystemProtocol->OpenVolume(EfiSimpleFileSystemProtocol, &Handle1);
jcarseyd2b45642009-05-11 18:02:16 +0000533 if (EFI_ERROR (Status)) {
534 FileHandle = NULL;
535 return Status;
536 }
537
538 //
539 // go down directories one node at a time.
540 //
541 while (!IsDevicePathEnd (*FilePath)) {
jcarsey94b17fa2009-05-07 18:46:18 +0000542 //
jcarseyd2b45642009-05-11 18:02:16 +0000543 // For file system access each node should be a file path component
jcarsey94b17fa2009-05-07 18:46:18 +0000544 //
jcarseyd2b45642009-05-11 18:02:16 +0000545 if (DevicePathType (*FilePath) != MEDIA_DEVICE_PATH ||
546 DevicePathSubType (*FilePath) != MEDIA_FILEPATH_DP
jcarseya405b862010-09-14 05:18:09 +0000547 ) {
jcarsey94b17fa2009-05-07 18:46:18 +0000548 FileHandle = NULL;
jcarseyd2b45642009-05-11 18:02:16 +0000549 return (EFI_INVALID_PARAMETER);
jcarsey94b17fa2009-05-07 18:46:18 +0000550 }
jcarseyd2b45642009-05-11 18:02:16 +0000551 //
552 // Open this file path node
553 //
jcarseya405b862010-09-14 05:18:09 +0000554 Handle2 = Handle1;
555 Handle1 = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +0000556
557 //
ydong100b6cb332013-01-25 02:00:22 +0000558 // File Name Alignment Fix (FNAF)
559 // Handle2->Open may be incapable of handling a unaligned CHAR16 data.
560 // The structure pointed to by FilePath may be not CHAR16 aligned.
561 // This code copies the potentially unaligned PathName data from the
562 // FilePath structure to the aligned FnafPathName for use in the
563 // calls to Handl2->Open.
564 //
565
566 //
567 // Determine length of PathName, in bytes.
568 //
569 PathLen = DevicePathNodeLength (*FilePath) - SIZE_OF_FILEPATH_DEVICE_PATH;
570
571 //
572 // Allocate memory for the aligned copy of the string Extra allocation is to allow for forced alignment
573 // Copy bytes from possibly unaligned location to aligned location
574 //
575 FnafPathName = AllocateCopyPool(PathLen, (UINT8 *)((FILEPATH_DEVICE_PATH*)*FilePath)->PathName);
576 if (FnafPathName == NULL) {
577 return EFI_OUT_OF_RESOURCES;
578 }
579
580 //
jcarseyd2b45642009-05-11 18:02:16 +0000581 // Try to test opening an existing file
jcarsey94b17fa2009-05-07 18:46:18 +0000582 //
jcarseya405b862010-09-14 05:18:09 +0000583 Status = Handle2->Open (
584 Handle2,
585 &Handle1,
ydong100b6cb332013-01-25 02:00:22 +0000586 FnafPathName,
jcarseyd2b45642009-05-11 18:02:16 +0000587 OpenMode &~EFI_FILE_MODE_CREATE,
588 0
jcarseya405b862010-09-14 05:18:09 +0000589 );
jcarsey94b17fa2009-05-07 18:46:18 +0000590
jcarseyd2b45642009-05-11 18:02:16 +0000591 //
592 // see if the error was that it needs to be created
593 //
594 if ((EFI_ERROR (Status)) && (OpenMode != (OpenMode &~EFI_FILE_MODE_CREATE))) {
jcarseya405b862010-09-14 05:18:09 +0000595 Status = Handle2->Open (
596 Handle2,
597 &Handle1,
ydong100b6cb332013-01-25 02:00:22 +0000598 FnafPathName,
jcarseyd2b45642009-05-11 18:02:16 +0000599 OpenMode,
600 Attributes
jcarseya405b862010-09-14 05:18:09 +0000601 );
jcarsey94b17fa2009-05-07 18:46:18 +0000602 }
ydong100b6cb332013-01-25 02:00:22 +0000603
604 //
605 // Free the alignment buffer
606 //
607 FreePool(FnafPathName);
608
jcarseyd2b45642009-05-11 18:02:16 +0000609 //
610 // Close the last node
611 //
jcarseya405b862010-09-14 05:18:09 +0000612 Handle2->Close (Handle2);
jcarseyd2b45642009-05-11 18:02:16 +0000613
614 if (EFI_ERROR(Status)) {
615 return (Status);
616 }
617
618 //
619 // Get the next node
620 //
621 *FilePath = NextDevicePathNode (*FilePath);
jcarsey94b17fa2009-05-07 18:46:18 +0000622 }
jcarseya405b862010-09-14 05:18:09 +0000623
624 //
625 // This is a weak spot since if the undefined SHELL_FILE_HANDLE format changes this must change also!
626 //
627 *FileHandle = (VOID*)Handle1;
jcarseyd2b45642009-05-11 18:02:16 +0000628 return (EFI_SUCCESS);
jcarsey94b17fa2009-05-07 18:46:18 +0000629}
630
631/**
632 This function will open a file or directory referenced by filename.
633
jcarsey1e6e84c2010-01-25 20:05:08 +0000634 If return is EFI_SUCCESS, the Filehandle is the opened file's handle;
635 otherwise, the Filehandle is NULL. The Attributes is valid only for
jcarsey94b17fa2009-05-07 18:46:18 +0000636 EFI_FILE_MODE_CREATE.
637
jcarsey92a54472011-06-27 20:33:13 +0000638 if FileName is NULL then ASSERT()
jcarsey94b17fa2009-05-07 18:46:18 +0000639
darylm503b0934ac2011-11-12 00:35:11 +0000640 @param FileName pointer to file name
641 @param FileHandle pointer to the file handle.
642 @param OpenMode the mode to open the file with.
643 @param Attributes the file's file attributes.
jcarsey94b17fa2009-05-07 18:46:18 +0000644
darylm503b0934ac2011-11-12 00:35:11 +0000645 @retval EFI_SUCCESS The information was set.
646 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
647 @retval EFI_UNSUPPORTED Could not open the file path.
648 @retval EFI_NOT_FOUND The specified file could not be found on the
jcarsey1e6e84c2010-01-25 20:05:08 +0000649 device or the file system could not be found
jcarsey94b17fa2009-05-07 18:46:18 +0000650 on the device.
darylm503b0934ac2011-11-12 00:35:11 +0000651 @retval EFI_NO_MEDIA The device has no medium.
652 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
jcarsey94b17fa2009-05-07 18:46:18 +0000653 medium is no longer supported.
darylm503b0934ac2011-11-12 00:35:11 +0000654 @retval EFI_DEVICE_ERROR The device reported an error.
655 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
656 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
657 @retval EFI_ACCESS_DENIED The file was opened read only.
658 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
jcarsey94b17fa2009-05-07 18:46:18 +0000659 file.
darylm503b0934ac2011-11-12 00:35:11 +0000660 @retval EFI_VOLUME_FULL The volume is full.
jcarsey94b17fa2009-05-07 18:46:18 +0000661**/
662EFI_STATUS
663EFIAPI
664ShellOpenFileByName(
darylm503b0934ac2011-11-12 00:35:11 +0000665 IN CONST CHAR16 *FileName,
jcarseya405b862010-09-14 05:18:09 +0000666 OUT SHELL_FILE_HANDLE *FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000667 IN UINT64 OpenMode,
darylm503b0934ac2011-11-12 00:35:11 +0000668 IN UINT64 Attributes
jcarseya405b862010-09-14 05:18:09 +0000669 )
670{
jcarsey94b17fa2009-05-07 18:46:18 +0000671 EFI_HANDLE DeviceHandle;
672 EFI_DEVICE_PATH_PROTOCOL *FilePath;
jcarseyb1f95a02009-06-16 00:23:19 +0000673 EFI_STATUS Status;
674 EFI_FILE_INFO *FileInfo;
jaben carsey21a86a72015-01-13 22:16:41 +0000675 CHAR16 *FileNameCopy;
jcarsey94b17fa2009-05-07 18:46:18 +0000676
677 //
678 // ASSERT if FileName is NULL
679 //
680 ASSERT(FileName != NULL);
681
jcarseya405b862010-09-14 05:18:09 +0000682 if (FileName == NULL) {
683 return (EFI_INVALID_PARAMETER);
684 }
685
jcarsey366f81a2011-06-27 21:04:22 +0000686 if (gEfiShellProtocol != NULL) {
jaben carsey21a86a72015-01-13 22:16:41 +0000687 if ((OpenMode & EFI_FILE_MODE_CREATE) == EFI_FILE_MODE_CREATE) {
688
689 //
690 // Create only a directory
691 //
692 if ((Attributes & EFI_FILE_DIRECTORY) == EFI_FILE_DIRECTORY) {
693 return ShellCreateDirectory(FileName, FileHandle);
694 }
695
696 //
697 // Create the directory to create the file in
698 //
699 FileNameCopy = AllocateCopyPool (StrSize (FileName), FileName);
700 if (FileName == NULL) {
701 return (EFI_OUT_OF_RESOURCES);
702 }
703 PathCleanUpDirectories (FileNameCopy);
704 if (PathRemoveLastItem (FileNameCopy)) {
Jaben Carsey983fffd2015-07-28 20:22:26 +0000705 if (!EFI_ERROR(ShellCreateDirectory (FileNameCopy, FileHandle))) {
706 ShellCloseFile (FileHandle);
707 }
jaben carsey21a86a72015-01-13 22:16:41 +0000708 }
709 SHELL_FREE_NON_NULL (FileNameCopy);
jcarseya405b862010-09-14 05:18:09 +0000710 }
jaben carsey21a86a72015-01-13 22:16:41 +0000711
jcarsey94b17fa2009-05-07 18:46:18 +0000712 //
jaben carsey21a86a72015-01-13 22:16:41 +0000713 // Use UEFI Shell 2.0 method to create the file
jcarsey94b17fa2009-05-07 18:46:18 +0000714 //
jcarsey366f81a2011-06-27 21:04:22 +0000715 Status = gEfiShellProtocol->OpenFileByName(FileName,
jcarseyb1f95a02009-06-16 00:23:19 +0000716 FileHandle,
717 OpenMode);
jcarseya405b862010-09-14 05:18:09 +0000718 if (StrCmp(FileName, L"NUL") != 0 && !EFI_ERROR(Status) && ((OpenMode & EFI_FILE_MODE_CREATE) != 0)){
jcarsey2247dde2009-11-09 18:08:58 +0000719 FileInfo = FileFunctionMap.GetFileInfo(*FileHandle);
jcarseyb1f95a02009-06-16 00:23:19 +0000720 ASSERT(FileInfo != NULL);
721 FileInfo->Attribute = Attributes;
jcarsey2247dde2009-11-09 18:08:58 +0000722 Status = FileFunctionMap.SetFileInfo(*FileHandle, FileInfo);
723 FreePool(FileInfo);
jcarseyb1f95a02009-06-16 00:23:19 +0000724 }
725 return (Status);
jcarsey1e6e84c2010-01-25 20:05:08 +0000726 }
jcarsey94b17fa2009-05-07 18:46:18 +0000727 //
728 // Using EFI Shell version
729 // this means convert name to path and call that function
730 // since this will use EFI method again that will open it.
731 //
732 ASSERT(mEfiShellEnvironment2 != NULL);
jcarseyb82bfcc2009-06-29 16:28:23 +0000733 FilePath = mEfiShellEnvironment2->NameToPath ((CHAR16*)FileName);
xdu290bfa222010-07-19 05:21:27 +0000734 if (FilePath != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +0000735 return (ShellOpenFileByDevicePath(&FilePath,
736 &DeviceHandle,
737 FileHandle,
738 OpenMode,
jcarseya405b862010-09-14 05:18:09 +0000739 Attributes));
jcarsey94b17fa2009-05-07 18:46:18 +0000740 }
741 return (EFI_DEVICE_ERROR);
742}
743/**
744 This function create a directory
745
jcarsey1e6e84c2010-01-25 20:05:08 +0000746 If return is EFI_SUCCESS, the Filehandle is the opened directory's handle;
747 otherwise, the Filehandle is NULL. If the directory already existed, this
jcarsey94b17fa2009-05-07 18:46:18 +0000748 function opens the existing directory.
749
darylm503b0934ac2011-11-12 00:35:11 +0000750 @param DirectoryName pointer to directory name
751 @param FileHandle pointer to the file handle.
jcarsey94b17fa2009-05-07 18:46:18 +0000752
darylm503b0934ac2011-11-12 00:35:11 +0000753 @retval EFI_SUCCESS The information was set.
754 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
755 @retval EFI_UNSUPPORTED Could not open the file path.
756 @retval EFI_NOT_FOUND The specified file could not be found on the
jcarsey1e6e84c2010-01-25 20:05:08 +0000757 device or the file system could not be found
jcarsey94b17fa2009-05-07 18:46:18 +0000758 on the device.
darylm503b0934ac2011-11-12 00:35:11 +0000759 @retval EFI_NO_MEDIA The device has no medium.
760 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
jcarsey94b17fa2009-05-07 18:46:18 +0000761 medium is no longer supported.
darylm503b0934ac2011-11-12 00:35:11 +0000762 @retval EFI_DEVICE_ERROR The device reported an error.
763 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
764 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
765 @retval EFI_ACCESS_DENIED The file was opened read only.
766 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
jcarsey94b17fa2009-05-07 18:46:18 +0000767 file.
darylm503b0934ac2011-11-12 00:35:11 +0000768 @retval EFI_VOLUME_FULL The volume is full.
jcarsey94b17fa2009-05-07 18:46:18 +0000769 @sa ShellOpenFileByName
770**/
771EFI_STATUS
772EFIAPI
773ShellCreateDirectory(
jcarseyb82bfcc2009-06-29 16:28:23 +0000774 IN CONST CHAR16 *DirectoryName,
jcarseya405b862010-09-14 05:18:09 +0000775 OUT SHELL_FILE_HANDLE *FileHandle
776 )
777{
jcarsey366f81a2011-06-27 21:04:22 +0000778 if (gEfiShellProtocol != NULL) {
jcarsey2247dde2009-11-09 18:08:58 +0000779 //
780 // Use UEFI Shell 2.0 method
781 //
jcarsey366f81a2011-06-27 21:04:22 +0000782 return (gEfiShellProtocol->CreateFile(DirectoryName,
jcarsey2247dde2009-11-09 18:08:58 +0000783 EFI_FILE_DIRECTORY,
784 FileHandle
jcarseya405b862010-09-14 05:18:09 +0000785 ));
jcarsey2247dde2009-11-09 18:08:58 +0000786 } else {
787 return (ShellOpenFileByName(DirectoryName,
788 FileHandle,
789 EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE,
790 EFI_FILE_DIRECTORY
jcarseya405b862010-09-14 05:18:09 +0000791 ));
jcarsey2247dde2009-11-09 18:08:58 +0000792 }
jcarsey94b17fa2009-05-07 18:46:18 +0000793}
794
795/**
796 This function reads information from an opened file.
797
jcarsey1e6e84c2010-01-25 20:05:08 +0000798 If FileHandle is not a directory, the function reads the requested number of
799 bytes from the file at the file's current position and returns them in Buffer.
jcarsey94b17fa2009-05-07 18:46:18 +0000800 If the read goes beyond the end of the file, the read length is truncated to the
jcarsey1e6e84c2010-01-25 20:05:08 +0000801 end of the file. The file's current position is increased by the number of bytes
802 returned. If FileHandle is a directory, the function reads the directory entry
803 at the file's current position and returns the entry in Buffer. If the Buffer
804 is not large enough to hold the current directory entry, then
805 EFI_BUFFER_TOO_SMALL is returned and the current file position is not updated.
806 BufferSize is set to be the size of the buffer needed to read the entry. On
807 success, the current position is updated to the next directory entry. If there
808 are no more directory entries, the read returns a zero-length buffer.
jcarsey94b17fa2009-05-07 18:46:18 +0000809 EFI_FILE_INFO is the structure returned as the directory entry.
810
811 @param FileHandle the opened file handle
jcarsey1e6e84c2010-01-25 20:05:08 +0000812 @param BufferSize on input the size of buffer in bytes. on return
jcarsey94b17fa2009-05-07 18:46:18 +0000813 the number of bytes written.
814 @param Buffer the buffer to put read data into.
815
darylm503b0934ac2011-11-12 00:35:11 +0000816 @retval EFI_SUCCESS Data was read.
817 @retval EFI_NO_MEDIA The device has no media.
818 @retval EFI_DEVICE_ERROR The device reported an error.
819 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
820 @retval EFI_BUFFER_TO_SMALL Buffer is too small. ReadSize contains required
jcarsey94b17fa2009-05-07 18:46:18 +0000821 size.
822
823**/
824EFI_STATUS
825EFIAPI
826ShellReadFile(
jcarseya405b862010-09-14 05:18:09 +0000827 IN SHELL_FILE_HANDLE FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000828 IN OUT UINTN *BufferSize,
829 OUT VOID *Buffer
jcarseya405b862010-09-14 05:18:09 +0000830 )
831{
jcarseyd2b45642009-05-11 18:02:16 +0000832 return (FileFunctionMap.ReadFile(FileHandle, BufferSize, Buffer));
jcarsey94b17fa2009-05-07 18:46:18 +0000833}
834
835
836/**
837 Write data to a file.
838
jcarsey1e6e84c2010-01-25 20:05:08 +0000839 This function writes the specified number of bytes to the file at the current
840 file position. The current file position is advanced the actual number of bytes
841 written, which is returned in BufferSize. Partial writes only occur when there
842 has been a data error during the write attempt (such as "volume space full").
843 The file is automatically grown to hold the data if required. Direct writes to
jcarsey94b17fa2009-05-07 18:46:18 +0000844 opened directories are not supported.
845
846 @param FileHandle The opened file for writing
847 @param BufferSize on input the number of bytes in Buffer. On output
848 the number of bytes written.
849 @param Buffer the buffer containing data to write is stored.
850
darylm503b0934ac2011-11-12 00:35:11 +0000851 @retval EFI_SUCCESS Data was written.
852 @retval EFI_UNSUPPORTED Writes to an open directory are not supported.
853 @retval EFI_NO_MEDIA The device has no media.
854 @retval EFI_DEVICE_ERROR The device reported an error.
855 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
856 @retval EFI_WRITE_PROTECTED The device is write-protected.
857 @retval EFI_ACCESS_DENIED The file was open for read only.
858 @retval EFI_VOLUME_FULL The volume is full.
jcarsey94b17fa2009-05-07 18:46:18 +0000859**/
860EFI_STATUS
861EFIAPI
862ShellWriteFile(
jcarsey252d9452011-03-25 20:49:53 +0000863 IN SHELL_FILE_HANDLE FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000864 IN OUT UINTN *BufferSize,
865 IN VOID *Buffer
jcarseya405b862010-09-14 05:18:09 +0000866 )
867{
jcarseyd2b45642009-05-11 18:02:16 +0000868 return (FileFunctionMap.WriteFile(FileHandle, BufferSize, Buffer));
jcarsey94b17fa2009-05-07 18:46:18 +0000869}
870
jcarsey1e6e84c2010-01-25 20:05:08 +0000871/**
jcarsey94b17fa2009-05-07 18:46:18 +0000872 Close an open file handle.
873
jcarsey1e6e84c2010-01-25 20:05:08 +0000874 This function closes a specified file handle. All "dirty" cached file data is
875 flushed to the device, and the file is closed. In all cases the handle is
jcarsey94b17fa2009-05-07 18:46:18 +0000876 closed.
877
878@param FileHandle the file handle to close.
879
880@retval EFI_SUCCESS the file handle was closed sucessfully.
881**/
882EFI_STATUS
883EFIAPI
884ShellCloseFile (
jcarseya405b862010-09-14 05:18:09 +0000885 IN SHELL_FILE_HANDLE *FileHandle
886 )
887{
jcarseyd2b45642009-05-11 18:02:16 +0000888 return (FileFunctionMap.CloseFile(*FileHandle));
jcarsey94b17fa2009-05-07 18:46:18 +0000889}
890
891/**
892 Delete a file and close the handle
893
894 This function closes and deletes a file. In all cases the file handle is closed.
jcarsey1e6e84c2010-01-25 20:05:08 +0000895 If the file cannot be deleted, the warning code EFI_WARN_DELETE_FAILURE is
jcarsey94b17fa2009-05-07 18:46:18 +0000896 returned, but the handle is still closed.
897
898 @param FileHandle the file handle to delete
899
900 @retval EFI_SUCCESS the file was closed sucessfully
jcarsey1e6e84c2010-01-25 20:05:08 +0000901 @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not
jcarsey94b17fa2009-05-07 18:46:18 +0000902 deleted
darylm503b0934ac2011-11-12 00:35:11 +0000903 @retval INVALID_PARAMETER One of the parameters has an invalid value.
jcarsey94b17fa2009-05-07 18:46:18 +0000904**/
905EFI_STATUS
906EFIAPI
907ShellDeleteFile (
darylm503b0934ac2011-11-12 00:35:11 +0000908 IN SHELL_FILE_HANDLE *FileHandle
jcarseya405b862010-09-14 05:18:09 +0000909 )
910{
jcarseyd2b45642009-05-11 18:02:16 +0000911 return (FileFunctionMap.DeleteFile(*FileHandle));
jcarsey94b17fa2009-05-07 18:46:18 +0000912}
913
914/**
915 Set the current position in a file.
916
jcarsey1e6e84c2010-01-25 20:05:08 +0000917 This function sets the current file position for the handle to the position
jcarsey94b17fa2009-05-07 18:46:18 +0000918 supplied. With the exception of seeking to position 0xFFFFFFFFFFFFFFFF, only
jcarsey1e6e84c2010-01-25 20:05:08 +0000919 absolute positioning is supported, and seeking past the end of the file is
920 allowed (a subsequent write would grow the file). Seeking to position
jcarsey94b17fa2009-05-07 18:46:18 +0000921 0xFFFFFFFFFFFFFFFF causes the current position to be set to the end of the file.
jcarsey1e6e84c2010-01-25 20:05:08 +0000922 If FileHandle is a directory, the only position that may be set is zero. This
jcarsey94b17fa2009-05-07 18:46:18 +0000923 has the effect of starting the read process of the directory entries over.
924
925 @param FileHandle The file handle on which the position is being set
926 @param Position Byte position from begining of file
927
928 @retval EFI_SUCCESS Operation completed sucessfully.
jcarsey1e6e84c2010-01-25 20:05:08 +0000929 @retval EFI_UNSUPPORTED the seek request for non-zero is not valid on
jcarsey94b17fa2009-05-07 18:46:18 +0000930 directories.
931 @retval INVALID_PARAMETER One of the parameters has an invalid value.
932**/
933EFI_STATUS
934EFIAPI
935ShellSetFilePosition (
darylm503b0934ac2011-11-12 00:35:11 +0000936 IN SHELL_FILE_HANDLE FileHandle,
937 IN UINT64 Position
jcarseya405b862010-09-14 05:18:09 +0000938 )
939{
jcarseyd2b45642009-05-11 18:02:16 +0000940 return (FileFunctionMap.SetFilePosition(FileHandle, Position));
jcarsey94b17fa2009-05-07 18:46:18 +0000941}
942
jcarsey1e6e84c2010-01-25 20:05:08 +0000943/**
jcarsey94b17fa2009-05-07 18:46:18 +0000944 Gets a file's current position
945
jcarsey1e6e84c2010-01-25 20:05:08 +0000946 This function retrieves the current file position for the file handle. For
947 directories, the current file position has no meaning outside of the file
jcarsey94b17fa2009-05-07 18:46:18 +0000948 system driver and as such the operation is not supported. An error is returned
949 if FileHandle is a directory.
950
951 @param FileHandle The open file handle on which to get the position.
952 @param Position Byte position from begining of file.
953
954 @retval EFI_SUCCESS the operation completed sucessfully.
955 @retval INVALID_PARAMETER One of the parameters has an invalid value.
956 @retval EFI_UNSUPPORTED the request is not valid on directories.
957**/
958EFI_STATUS
959EFIAPI
960ShellGetFilePosition (
jcarseya405b862010-09-14 05:18:09 +0000961 IN SHELL_FILE_HANDLE FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000962 OUT UINT64 *Position
jcarseya405b862010-09-14 05:18:09 +0000963 )
964{
jcarseyd2b45642009-05-11 18:02:16 +0000965 return (FileFunctionMap.GetFilePosition(FileHandle, Position));
jcarsey94b17fa2009-05-07 18:46:18 +0000966}
967/**
968 Flushes data on a file
jcarsey1e6e84c2010-01-25 20:05:08 +0000969
jcarsey94b17fa2009-05-07 18:46:18 +0000970 This function flushes all modified data associated with a file to a device.
971
972 @param FileHandle The file handle on which to flush data
973
974 @retval EFI_SUCCESS The data was flushed.
975 @retval EFI_NO_MEDIA The device has no media.
976 @retval EFI_DEVICE_ERROR The device reported an error.
977 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
978 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
979 @retval EFI_ACCESS_DENIED The file was opened for read only.
980**/
981EFI_STATUS
982EFIAPI
983ShellFlushFile (
jcarseya405b862010-09-14 05:18:09 +0000984 IN SHELL_FILE_HANDLE FileHandle
985 )
986{
jcarseyd2b45642009-05-11 18:02:16 +0000987 return (FileFunctionMap.FlushFile(FileHandle));
jcarsey94b17fa2009-05-07 18:46:18 +0000988}
989
darylm503b0934ac2011-11-12 00:35:11 +0000990/** Retrieve first entry from a directory.
jcarsey94b17fa2009-05-07 18:46:18 +0000991
darylm503b0934ac2011-11-12 00:35:11 +0000992 This function takes an open directory handle and gets information from the
993 first entry in the directory. A buffer is allocated to contain
994 the information and a pointer to the buffer is returned in *Buffer. The
995 caller can use ShellFindNextFile() to get subsequent directory entries.
jcarsey94b17fa2009-05-07 18:46:18 +0000996
darylm503b0934ac2011-11-12 00:35:11 +0000997 The buffer will be freed by ShellFindNextFile() when the last directory
998 entry is read. Otherwise, the caller must free the buffer, using FreePool,
999 when finished with it.
1000
1001 @param[in] DirHandle The file handle of the directory to search.
1002 @param[out] Buffer The pointer to the buffer for the file's information.
jcarsey94b17fa2009-05-07 18:46:18 +00001003
1004 @retval EFI_SUCCESS Found the first file.
1005 @retval EFI_NOT_FOUND Cannot find the directory.
1006 @retval EFI_NO_MEDIA The device has no media.
1007 @retval EFI_DEVICE_ERROR The device reported an error.
1008 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
1009 @return Others status of ShellGetFileInfo, ShellSetFilePosition,
1010 or ShellReadFile
1011**/
1012EFI_STATUS
1013EFIAPI
1014ShellFindFirstFile (
jcarseya405b862010-09-14 05:18:09 +00001015 IN SHELL_FILE_HANDLE DirHandle,
jcarseyd2b45642009-05-11 18:02:16 +00001016 OUT EFI_FILE_INFO **Buffer
jcarseya405b862010-09-14 05:18:09 +00001017 )
1018{
jcarsey94b17fa2009-05-07 18:46:18 +00001019 //
jcarseyd2b45642009-05-11 18:02:16 +00001020 // pass to file handle lib
jcarsey94b17fa2009-05-07 18:46:18 +00001021 //
jcarseyd2b45642009-05-11 18:02:16 +00001022 return (FileHandleFindFirstFile(DirHandle, Buffer));
jcarsey94b17fa2009-05-07 18:46:18 +00001023}
darylm503b0934ac2011-11-12 00:35:11 +00001024/** Retrieve next entries from a directory.
jcarsey94b17fa2009-05-07 18:46:18 +00001025
darylm503b0934ac2011-11-12 00:35:11 +00001026 To use this function, the caller must first call the ShellFindFirstFile()
1027 function to get the first directory entry. Subsequent directory entries are
1028 retrieved by using the ShellFindNextFile() function. This function can
1029 be called several times to get each entry from the directory. If the call of
1030 ShellFindNextFile() retrieved the last directory entry, the next call of
1031 this function will set *NoFile to TRUE and free the buffer.
jcarsey94b17fa2009-05-07 18:46:18 +00001032
darylm503b0934ac2011-11-12 00:35:11 +00001033 @param[in] DirHandle The file handle of the directory.
1034 @param[out] Buffer The pointer to buffer for file's information.
1035 @param[out] NoFile The pointer to boolean when last file is found.
jcarsey94b17fa2009-05-07 18:46:18 +00001036
1037 @retval EFI_SUCCESS Found the next file, or reached last file
1038 @retval EFI_NO_MEDIA The device has no media.
1039 @retval EFI_DEVICE_ERROR The device reported an error.
1040 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
1041**/
1042EFI_STATUS
1043EFIAPI
1044ShellFindNextFile(
jcarseya405b862010-09-14 05:18:09 +00001045 IN SHELL_FILE_HANDLE DirHandle,
jcarsey94b17fa2009-05-07 18:46:18 +00001046 OUT EFI_FILE_INFO *Buffer,
1047 OUT BOOLEAN *NoFile
jcarseya405b862010-09-14 05:18:09 +00001048 )
1049{
jcarsey94b17fa2009-05-07 18:46:18 +00001050 //
jcarseyd2b45642009-05-11 18:02:16 +00001051 // pass to file handle lib
jcarsey94b17fa2009-05-07 18:46:18 +00001052 //
jcarseyd2b45642009-05-11 18:02:16 +00001053 return (FileHandleFindNextFile(DirHandle, Buffer, NoFile));
jcarsey94b17fa2009-05-07 18:46:18 +00001054}
1055/**
1056 Retrieve the size of a file.
1057
1058 if FileHandle is NULL then ASSERT()
1059 if Size is NULL then ASSERT()
1060
jcarsey1e6e84c2010-01-25 20:05:08 +00001061 This function extracts the file size info from the FileHandle's EFI_FILE_INFO
jcarsey94b17fa2009-05-07 18:46:18 +00001062 data.
1063
1064 @param FileHandle file handle from which size is retrieved
1065 @param Size pointer to size
1066
1067 @retval EFI_SUCCESS operation was completed sucessfully
1068 @retval EFI_DEVICE_ERROR cannot access the file
1069**/
1070EFI_STATUS
1071EFIAPI
1072ShellGetFileSize (
jcarseya405b862010-09-14 05:18:09 +00001073 IN SHELL_FILE_HANDLE FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +00001074 OUT UINT64 *Size
jcarseya405b862010-09-14 05:18:09 +00001075 )
1076{
jcarseyd2b45642009-05-11 18:02:16 +00001077 return (FileFunctionMap.GetFileSize(FileHandle, Size));
jcarsey94b17fa2009-05-07 18:46:18 +00001078}
1079/**
1080 Retrieves the status of the break execution flag
1081
1082 this function is useful to check whether the application is being asked to halt by the shell.
1083
1084 @retval TRUE the execution break is enabled
1085 @retval FALSE the execution break is not enabled
1086**/
1087BOOLEAN
1088EFIAPI
1089ShellGetExecutionBreakFlag(
1090 VOID
1091 )
1092{
jcarsey1e6e84c2010-01-25 20:05:08 +00001093 //
jcarsey94b17fa2009-05-07 18:46:18 +00001094 // Check for UEFI Shell 2.0 protocols
1095 //
jcarsey366f81a2011-06-27 21:04:22 +00001096 if (gEfiShellProtocol != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00001097
1098 //
1099 // We are using UEFI Shell 2.0; see if the event has been triggered
1100 //
jcarsey366f81a2011-06-27 21:04:22 +00001101 if (gBS->CheckEvent(gEfiShellProtocol->ExecutionBreak) != EFI_SUCCESS) {
jcarsey94b17fa2009-05-07 18:46:18 +00001102 return (FALSE);
1103 }
1104 return (TRUE);
jcarsey1e6e84c2010-01-25 20:05:08 +00001105 }
jcarsey94b17fa2009-05-07 18:46:18 +00001106
1107 //
1108 // using EFI Shell; call the function to check
1109 //
jcarsey92a54472011-06-27 20:33:13 +00001110 if (mEfiShellEnvironment2 != NULL) {
1111 return (mEfiShellEnvironment2->GetExecutionBreak());
1112 }
1113
1114 return (FALSE);
jcarsey94b17fa2009-05-07 18:46:18 +00001115}
1116/**
1117 return the value of an environment variable
1118
jcarsey1e6e84c2010-01-25 20:05:08 +00001119 this function gets the value of the environment variable set by the
jcarsey94b17fa2009-05-07 18:46:18 +00001120 ShellSetEnvironmentVariable function
1121
1122 @param EnvKey The key name of the environment variable.
1123
1124 @retval NULL the named environment variable does not exist.
1125 @return != NULL pointer to the value of the environment variable
1126**/
1127CONST CHAR16*
1128EFIAPI
1129ShellGetEnvironmentVariable (
jcarsey9b3bf082009-06-23 21:15:07 +00001130 IN CONST CHAR16 *EnvKey
jcarsey94b17fa2009-05-07 18:46:18 +00001131 )
1132{
jcarsey1e6e84c2010-01-25 20:05:08 +00001133 //
jcarsey94b17fa2009-05-07 18:46:18 +00001134 // Check for UEFI Shell 2.0 protocols
1135 //
jcarsey366f81a2011-06-27 21:04:22 +00001136 if (gEfiShellProtocol != NULL) {
1137 return (gEfiShellProtocol->GetEnv(EnvKey));
jcarsey94b17fa2009-05-07 18:46:18 +00001138 }
1139
1140 //
jcarsey92a54472011-06-27 20:33:13 +00001141 // Check for EFI shell
jcarsey94b17fa2009-05-07 18:46:18 +00001142 //
jcarsey92a54472011-06-27 20:33:13 +00001143 if (mEfiShellEnvironment2 != NULL) {
1144 return (mEfiShellEnvironment2->GetEnv((CHAR16*)EnvKey));
1145 }
jcarsey94b17fa2009-05-07 18:46:18 +00001146
jcarsey92a54472011-06-27 20:33:13 +00001147 return NULL;
jcarsey94b17fa2009-05-07 18:46:18 +00001148}
1149/**
1150 set the value of an environment variable
1151
1152This function changes the current value of the specified environment variable. If the
1153environment variable exists and the Value is an empty string, then the environment
1154variable is deleted. If the environment variable exists and the Value is not an empty
1155string, then the value of the environment variable is changed. If the environment
1156variable does not exist and the Value is an empty string, there is no action. If the
1157environment variable does not exist and the Value is a non-empty string, then the
1158environment variable is created and assigned the specified value.
1159
1160 This is not supported pre-UEFI Shell 2.0.
1161
1162 @param EnvKey The key name of the environment variable.
1163 @param EnvVal The Value of the environment variable
1164 @param Volatile Indicates whether the variable is non-volatile (FALSE) or volatile (TRUE).
1165
1166 @retval EFI_SUCCESS the operation was completed sucessfully
1167 @retval EFI_UNSUPPORTED This operation is not allowed in pre UEFI 2.0 Shell environments
1168**/
1169EFI_STATUS
1170EFIAPI
1171ShellSetEnvironmentVariable (
1172 IN CONST CHAR16 *EnvKey,
1173 IN CONST CHAR16 *EnvVal,
1174 IN BOOLEAN Volatile
1175 )
1176{
jcarsey1e6e84c2010-01-25 20:05:08 +00001177 //
jcarsey94b17fa2009-05-07 18:46:18 +00001178 // Check for UEFI Shell 2.0 protocols
1179 //
jcarsey366f81a2011-06-27 21:04:22 +00001180 if (gEfiShellProtocol != NULL) {
1181 return (gEfiShellProtocol->SetEnv(EnvKey, EnvVal, Volatile));
jcarsey1e6e84c2010-01-25 20:05:08 +00001182 }
jcarsey94b17fa2009-05-07 18:46:18 +00001183
1184 //
1185 // This feature does not exist under EFI shell
1186 //
1187 return (EFI_UNSUPPORTED);
1188}
jcarseya405b862010-09-14 05:18:09 +00001189
jcarsey94b17fa2009-05-07 18:46:18 +00001190/**
jcarseya405b862010-09-14 05:18:09 +00001191 Cause the shell to parse and execute a command line.
jcarsey94b17fa2009-05-07 18:46:18 +00001192
1193 This function creates a nested instance of the shell and executes the specified
jcarseya405b862010-09-14 05:18:09 +00001194 command (CommandLine) with the specified environment (Environment). Upon return,
1195 the status code returned by the specified command is placed in StatusCode.
1196 If Environment is NULL, then the current environment is used and all changes made
1197 by the commands executed will be reflected in the current environment. If the
1198 Environment is non-NULL, then the changes made will be discarded.
1199 The CommandLine is executed from the current working directory on the current
1200 device.
jcarsey94b17fa2009-05-07 18:46:18 +00001201
Brendan Jackman3877d0f2014-01-24 22:31:07 +00001202 The EnvironmentVariables pararemeter is ignored in a pre-UEFI Shell 2.0
jcarseya405b862010-09-14 05:18:09 +00001203 environment. The values pointed to by the parameters will be unchanged by the
1204 ShellExecute() function. The Output parameter has no effect in a
1205 UEFI Shell 2.0 environment.
jcarsey94b17fa2009-05-07 18:46:18 +00001206
jcarseya405b862010-09-14 05:18:09 +00001207 @param[in] ParentHandle The parent image starting the operation.
1208 @param[in] CommandLine The pointer to a NULL terminated command line.
1209 @param[in] Output True to display debug output. False to hide it.
1210 @param[in] EnvironmentVariables Optional pointer to array of environment variables
1211 in the form "x=y". If NULL, the current set is used.
1212 @param[out] Status The status of the run command line.
jcarsey94b17fa2009-05-07 18:46:18 +00001213
jcarseya405b862010-09-14 05:18:09 +00001214 @retval EFI_SUCCESS The operation completed sucessfully. Status
1215 contains the status code returned.
1216 @retval EFI_INVALID_PARAMETER A parameter contains an invalid value.
1217 @retval EFI_OUT_OF_RESOURCES Out of resources.
1218 @retval EFI_UNSUPPORTED The operation is not allowed.
jcarsey94b17fa2009-05-07 18:46:18 +00001219**/
1220EFI_STATUS
1221EFIAPI
1222ShellExecute (
1223 IN EFI_HANDLE *ParentHandle,
1224 IN CHAR16 *CommandLine OPTIONAL,
1225 IN BOOLEAN Output OPTIONAL,
1226 IN CHAR16 **EnvironmentVariables OPTIONAL,
1227 OUT EFI_STATUS *Status OPTIONAL
1228 )
1229{
Brendan Jackman3877d0f2014-01-24 22:31:07 +00001230 EFI_STATUS CmdStatus;
jcarsey1e6e84c2010-01-25 20:05:08 +00001231 //
jcarsey94b17fa2009-05-07 18:46:18 +00001232 // Check for UEFI Shell 2.0 protocols
1233 //
jcarsey366f81a2011-06-27 21:04:22 +00001234 if (gEfiShellProtocol != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00001235 //
1236 // Call UEFI Shell 2.0 version (not using Output parameter)
1237 //
jcarsey366f81a2011-06-27 21:04:22 +00001238 return (gEfiShellProtocol->Execute(ParentHandle,
jcarsey94b17fa2009-05-07 18:46:18 +00001239 CommandLine,
1240 EnvironmentVariables,
1241 Status));
jcarsey1e6e84c2010-01-25 20:05:08 +00001242 }
jcarsey92a54472011-06-27 20:33:13 +00001243
jcarsey94b17fa2009-05-07 18:46:18 +00001244 //
jcarsey92a54472011-06-27 20:33:13 +00001245 // Check for EFI shell
jcarsey94b17fa2009-05-07 18:46:18 +00001246 //
jcarsey92a54472011-06-27 20:33:13 +00001247 if (mEfiShellEnvironment2 != NULL) {
1248 //
Brendan Jackman3877d0f2014-01-24 22:31:07 +00001249 // Call EFI Shell version.
jcarsey92a54472011-06-27 20:33:13 +00001250 // Due to oddity in the EFI shell we want to dereference the ParentHandle here
1251 //
Brendan Jackman3877d0f2014-01-24 22:31:07 +00001252 CmdStatus = (mEfiShellEnvironment2->Execute(*ParentHandle,
jcarsey92a54472011-06-27 20:33:13 +00001253 CommandLine,
1254 Output));
Brendan Jackman3877d0f2014-01-24 22:31:07 +00001255 //
1256 // No Status output parameter so just use the returned status
1257 //
1258 if (Status != NULL) {
1259 *Status = CmdStatus;
1260 }
1261 //
1262 // If there was an error, we can't tell if it was from the command or from
1263 // the Execute() function, so we'll just assume the shell ran successfully
1264 // and the error came from the command.
1265 //
1266 return EFI_SUCCESS;
jcarsey92a54472011-06-27 20:33:13 +00001267 }
1268
1269 return (EFI_UNSUPPORTED);
jcarsey94b17fa2009-05-07 18:46:18 +00001270}
Brendan Jackman3877d0f2014-01-24 22:31:07 +00001271
jcarsey94b17fa2009-05-07 18:46:18 +00001272/**
1273 Retreives the current directory path
1274
jcarsey1e6e84c2010-01-25 20:05:08 +00001275 If the DeviceName is NULL, it returns the current device's current directory
1276 name. If the DeviceName is not NULL, it returns the current directory name
jcarsey94b17fa2009-05-07 18:46:18 +00001277 on specified drive.
1278
1279 @param DeviceName the name of the drive to get directory on
1280
1281 @retval NULL the directory does not exist
1282 @return != NULL the directory
1283**/
1284CONST CHAR16*
1285EFIAPI
1286ShellGetCurrentDir (
jcarseya405b862010-09-14 05:18:09 +00001287 IN CHAR16 * CONST DeviceName OPTIONAL
jcarsey94b17fa2009-05-07 18:46:18 +00001288 )
1289{
jcarsey1e6e84c2010-01-25 20:05:08 +00001290 //
jcarsey94b17fa2009-05-07 18:46:18 +00001291 // Check for UEFI Shell 2.0 protocols
1292 //
jcarsey366f81a2011-06-27 21:04:22 +00001293 if (gEfiShellProtocol != NULL) {
1294 return (gEfiShellProtocol->GetCurDir(DeviceName));
jcarsey1e6e84c2010-01-25 20:05:08 +00001295 }
jcarsey92a54472011-06-27 20:33:13 +00001296
jcarsey94b17fa2009-05-07 18:46:18 +00001297 //
jcarsey8bd282b2011-06-27 16:45:41 +00001298 // Check for EFI shell
jcarsey94b17fa2009-05-07 18:46:18 +00001299 //
jcarsey8bd282b2011-06-27 16:45:41 +00001300 if (mEfiShellEnvironment2 != NULL) {
1301 return (mEfiShellEnvironment2->CurDir(DeviceName));
1302 }
1303
1304 return (NULL);
jcarsey94b17fa2009-05-07 18:46:18 +00001305}
1306/**
1307 sets (enabled or disabled) the page break mode
1308
jcarsey1e6e84c2010-01-25 20:05:08 +00001309 when page break mode is enabled the screen will stop scrolling
jcarsey94b17fa2009-05-07 18:46:18 +00001310 and wait for operator input before scrolling a subsequent screen.
1311
1312 @param CurrentState TRUE to enable and FALSE to disable
1313**/
jcarsey1e6e84c2010-01-25 20:05:08 +00001314VOID
jcarsey94b17fa2009-05-07 18:46:18 +00001315EFIAPI
1316ShellSetPageBreakMode (
1317 IN BOOLEAN CurrentState
1318 )
1319{
1320 //
1321 // check for enabling
1322 //
1323 if (CurrentState != 0x00) {
jcarsey1e6e84c2010-01-25 20:05:08 +00001324 //
jcarsey94b17fa2009-05-07 18:46:18 +00001325 // check for UEFI Shell 2.0
1326 //
jcarsey366f81a2011-06-27 21:04:22 +00001327 if (gEfiShellProtocol != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00001328 //
1329 // Enable with UEFI 2.0 Shell
1330 //
jcarsey366f81a2011-06-27 21:04:22 +00001331 gEfiShellProtocol->EnablePageBreak();
jcarsey94b17fa2009-05-07 18:46:18 +00001332 return;
1333 } else {
jcarsey1e6e84c2010-01-25 20:05:08 +00001334 //
jcarsey92a54472011-06-27 20:33:13 +00001335 // Check for EFI shell
jcarsey94b17fa2009-05-07 18:46:18 +00001336 //
jcarsey92a54472011-06-27 20:33:13 +00001337 if (mEfiShellEnvironment2 != NULL) {
1338 //
1339 // Enable with EFI Shell
1340 //
1341 mEfiShellEnvironment2->EnablePageBreak (DEFAULT_INIT_ROW, DEFAULT_AUTO_LF);
1342 return;
1343 }
jcarsey94b17fa2009-05-07 18:46:18 +00001344 }
1345 } else {
jcarsey1e6e84c2010-01-25 20:05:08 +00001346 //
jcarsey94b17fa2009-05-07 18:46:18 +00001347 // check for UEFI Shell 2.0
1348 //
jcarsey366f81a2011-06-27 21:04:22 +00001349 if (gEfiShellProtocol != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00001350 //
1351 // Disable with UEFI 2.0 Shell
1352 //
jcarsey366f81a2011-06-27 21:04:22 +00001353 gEfiShellProtocol->DisablePageBreak();
jcarsey94b17fa2009-05-07 18:46:18 +00001354 return;
1355 } else {
jcarsey1e6e84c2010-01-25 20:05:08 +00001356 //
jcarsey92a54472011-06-27 20:33:13 +00001357 // Check for EFI shell
jcarsey94b17fa2009-05-07 18:46:18 +00001358 //
jcarsey92a54472011-06-27 20:33:13 +00001359 if (mEfiShellEnvironment2 != NULL) {
1360 //
1361 // Disable with EFI Shell
1362 //
1363 mEfiShellEnvironment2->DisablePageBreak ();
1364 return;
1365 }
jcarsey94b17fa2009-05-07 18:46:18 +00001366 }
1367 }
1368}
1369
1370///
1371/// version of EFI_SHELL_FILE_INFO struct, except has no CONST pointers.
1372/// This allows for the struct to be populated.
1373///
1374typedef struct {
jcarseyd2b45642009-05-11 18:02:16 +00001375 LIST_ENTRY Link;
jcarsey94b17fa2009-05-07 18:46:18 +00001376 EFI_STATUS Status;
1377 CHAR16 *FullName;
1378 CHAR16 *FileName;
jcarseya405b862010-09-14 05:18:09 +00001379 SHELL_FILE_HANDLE Handle;
jcarsey94b17fa2009-05-07 18:46:18 +00001380 EFI_FILE_INFO *Info;
1381} EFI_SHELL_FILE_INFO_NO_CONST;
1382
1383/**
1384 Converts a EFI shell list of structures to the coresponding UEFI Shell 2.0 type of list.
1385
1386 if OldStyleFileList is NULL then ASSERT()
1387
jcarsey1e6e84c2010-01-25 20:05:08 +00001388 this function will convert a SHELL_FILE_ARG based list into a callee allocated
jcarsey94b17fa2009-05-07 18:46:18 +00001389 EFI_SHELL_FILE_INFO based list. it is up to the caller to free the memory via
1390 the ShellCloseFileMetaArg function.
1391
ydong104ff7e372011-09-02 08:05:34 +00001392 @param[in] FileList the EFI shell list type
1393 @param[in, out] ListHead the list to add to
jcarsey94b17fa2009-05-07 18:46:18 +00001394
1395 @retval the resultant head of the double linked new format list;
1396**/
1397LIST_ENTRY*
1398EFIAPI
1399InternalShellConvertFileListType (
jcarsey9b3bf082009-06-23 21:15:07 +00001400 IN LIST_ENTRY *FileList,
1401 IN OUT LIST_ENTRY *ListHead
jcarsey125c2cf2009-11-18 21:36:50 +00001402 )
1403{
jcarsey94b17fa2009-05-07 18:46:18 +00001404 SHELL_FILE_ARG *OldInfo;
jcarsey9b3bf082009-06-23 21:15:07 +00001405 LIST_ENTRY *Link;
jcarsey94b17fa2009-05-07 18:46:18 +00001406 EFI_SHELL_FILE_INFO_NO_CONST *NewInfo;
1407
1408 //
jcarsey9b3bf082009-06-23 21:15:07 +00001409 // ASSERTs
jcarsey94b17fa2009-05-07 18:46:18 +00001410 //
jcarsey9b3bf082009-06-23 21:15:07 +00001411 ASSERT(FileList != NULL);
1412 ASSERT(ListHead != NULL);
jcarsey94b17fa2009-05-07 18:46:18 +00001413
1414 //
1415 // enumerate through each member of the old list and copy
1416 //
jcarseyd2b45642009-05-11 18:02:16 +00001417 for (Link = FileList->ForwardLink; Link != FileList; Link = Link->ForwardLink) {
jcarsey94b17fa2009-05-07 18:46:18 +00001418 OldInfo = CR (Link, SHELL_FILE_ARG, Link, SHELL_FILE_ARG_SIGNATURE);
jcarseya405b862010-09-14 05:18:09 +00001419 ASSERT(OldInfo != NULL);
1420
1421 //
1422 // Skip ones that failed to open...
1423 //
1424 if (OldInfo->Status != EFI_SUCCESS) {
1425 continue;
1426 }
jcarsey94b17fa2009-05-07 18:46:18 +00001427
1428 //
1429 // make sure the old list was valid
1430 //
jcarsey94b17fa2009-05-07 18:46:18 +00001431 ASSERT(OldInfo->Info != NULL);
1432 ASSERT(OldInfo->FullName != NULL);
1433 ASSERT(OldInfo->FileName != NULL);
1434
1435 //
1436 // allocate a new EFI_SHELL_FILE_INFO object
1437 //
1438 NewInfo = AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));
jcarseyc9d92df2010-02-03 15:37:54 +00001439 if (NewInfo == NULL) {
jcarsey7a95efd2011-10-13 16:08:18 +00001440 ShellCloseFileMetaArg((EFI_SHELL_FILE_INFO**)(&ListHead));
jcarseybeab0fc2011-10-10 17:26:25 +00001441 ListHead = NULL;
jcarseyc9d92df2010-02-03 15:37:54 +00001442 break;
1443 }
jcarsey1e6e84c2010-01-25 20:05:08 +00001444
1445 //
jcarsey94b17fa2009-05-07 18:46:18 +00001446 // copy the simple items
1447 //
1448 NewInfo->Handle = OldInfo->Handle;
1449 NewInfo->Status = OldInfo->Status;
1450
jcarseyd2b45642009-05-11 18:02:16 +00001451 // old shell checks for 0 not NULL
1452 OldInfo->Handle = 0;
1453
jcarsey94b17fa2009-05-07 18:46:18 +00001454 //
1455 // allocate new space to copy strings and structure
1456 //
Jaben Carsey98c16be2014-08-19 21:00:34 +00001457 NewInfo->FullName = AllocateCopyPool(StrSize(OldInfo->FullName), OldInfo->FullName);
1458 NewInfo->FileName = AllocateCopyPool(StrSize(OldInfo->FileName), OldInfo->FileName);
1459 NewInfo->Info = AllocateCopyPool((UINTN)OldInfo->Info->Size, OldInfo->Info);
jcarsey1e6e84c2010-01-25 20:05:08 +00001460
jcarsey94b17fa2009-05-07 18:46:18 +00001461 //
1462 // make sure all the memory allocations were sucessful
1463 //
jcarseybeab0fc2011-10-10 17:26:25 +00001464 if (NULL == NewInfo->FullName || NewInfo->FileName == NULL || NewInfo->Info == NULL) {
Jaben Carsey98c16be2014-08-19 21:00:34 +00001465 //
1466 // Free the partially allocated new node
1467 //
1468 SHELL_FREE_NON_NULL(NewInfo->FullName);
1469 SHELL_FREE_NON_NULL(NewInfo->FileName);
1470 SHELL_FREE_NON_NULL(NewInfo->Info);
1471 SHELL_FREE_NON_NULL(NewInfo);
1472
1473 //
1474 // Free the previously converted stuff
1475 //
jcarsey7a95efd2011-10-13 16:08:18 +00001476 ShellCloseFileMetaArg((EFI_SHELL_FILE_INFO**)(&ListHead));
jcarseybeab0fc2011-10-10 17:26:25 +00001477 ListHead = NULL;
1478 break;
1479 }
jcarsey94b17fa2009-05-07 18:46:18 +00001480
1481 //
jcarsey94b17fa2009-05-07 18:46:18 +00001482 // add that to the list
1483 //
jcarsey9b3bf082009-06-23 21:15:07 +00001484 InsertTailList(ListHead, &NewInfo->Link);
jcarsey94b17fa2009-05-07 18:46:18 +00001485 }
1486 return (ListHead);
1487}
1488/**
1489 Opens a group of files based on a path.
1490
jcarsey1e6e84c2010-01-25 20:05:08 +00001491 This function uses the Arg to open all the matching files. Each matched
Brendan Jackman70879312014-01-24 22:29:53 +00001492 file has a SHELL_FILE_INFO structure to record the file information. These
1493 structures are placed on the list ListHead. Users can get the SHELL_FILE_INFO
jcarsey94b17fa2009-05-07 18:46:18 +00001494 structures from ListHead to access each file. This function supports wildcards
jcarsey1e6e84c2010-01-25 20:05:08 +00001495 and will process '?' and '*' as such. the list must be freed with a call to
jcarsey94b17fa2009-05-07 18:46:18 +00001496 ShellCloseFileMetaArg().
1497
jcarsey1e6e84c2010-01-25 20:05:08 +00001498 If you are NOT appending to an existing list *ListHead must be NULL. If
jcarsey5f7431d2009-07-10 18:06:01 +00001499 *ListHead is NULL then it must be callee freed.
jcarsey94b17fa2009-05-07 18:46:18 +00001500
1501 @param Arg pointer to path string
1502 @param OpenMode mode to open files with
1503 @param ListHead head of linked list of results
1504
jcarsey1e6e84c2010-01-25 20:05:08 +00001505 @retval EFI_SUCCESS the operation was sucessful and the list head
jcarsey94b17fa2009-05-07 18:46:18 +00001506 contains the list of opened files
jcarsey94b17fa2009-05-07 18:46:18 +00001507 @return != EFI_SUCCESS the operation failed
1508
1509 @sa InternalShellConvertFileListType
1510**/
1511EFI_STATUS
1512EFIAPI
1513ShellOpenFileMetaArg (
1514 IN CHAR16 *Arg,
1515 IN UINT64 OpenMode,
1516 IN OUT EFI_SHELL_FILE_INFO **ListHead
1517 )
1518{
1519 EFI_STATUS Status;
jcarsey9b3bf082009-06-23 21:15:07 +00001520 LIST_ENTRY mOldStyleFileList;
Qiu Shumin0960ba12014-09-17 07:58:31 +00001521 CHAR16 *CleanFilePathStr;
jcarsey1e6e84c2010-01-25 20:05:08 +00001522
jcarsey94b17fa2009-05-07 18:46:18 +00001523 //
1524 // ASSERT that Arg and ListHead are not NULL
1525 //
1526 ASSERT(Arg != NULL);
1527 ASSERT(ListHead != NULL);
1528
Qiu Shumin715096c2014-09-19 01:32:05 +00001529 CleanFilePathStr = NULL;
1530
Qiu Shumin0960ba12014-09-17 07:58:31 +00001531 Status = InternalShellStripQuotes (Arg, &CleanFilePathStr);
1532 if (EFI_ERROR (Status)) {
1533 return Status;
1534 }
1535
jcarsey1e6e84c2010-01-25 20:05:08 +00001536 //
jcarsey94b17fa2009-05-07 18:46:18 +00001537 // Check for UEFI Shell 2.0 protocols
1538 //
jcarsey366f81a2011-06-27 21:04:22 +00001539 if (gEfiShellProtocol != NULL) {
jcarsey5f7431d2009-07-10 18:06:01 +00001540 if (*ListHead == NULL) {
1541 *ListHead = (EFI_SHELL_FILE_INFO*)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));
1542 if (*ListHead == NULL) {
Qiu Shumin0960ba12014-09-17 07:58:31 +00001543 FreePool(CleanFilePathStr);
jcarsey5f7431d2009-07-10 18:06:01 +00001544 return (EFI_OUT_OF_RESOURCES);
1545 }
1546 InitializeListHead(&((*ListHead)->Link));
jcarsey1e6e84c2010-01-25 20:05:08 +00001547 }
Qiu Shumin0960ba12014-09-17 07:58:31 +00001548 Status = gEfiShellProtocol->OpenFileList(CleanFilePathStr,
jcarsey1e6e84c2010-01-25 20:05:08 +00001549 OpenMode,
jcarsey2247dde2009-11-09 18:08:58 +00001550 ListHead);
1551 if (EFI_ERROR(Status)) {
jcarsey366f81a2011-06-27 21:04:22 +00001552 gEfiShellProtocol->RemoveDupInFileList(ListHead);
jcarsey2247dde2009-11-09 18:08:58 +00001553 } else {
jcarsey366f81a2011-06-27 21:04:22 +00001554 Status = gEfiShellProtocol->RemoveDupInFileList(ListHead);
jcarsey2247dde2009-11-09 18:08:58 +00001555 }
jcarseya405b862010-09-14 05:18:09 +00001556 if (*ListHead != NULL && IsListEmpty(&(*ListHead)->Link)) {
1557 FreePool(*ListHead);
Qiu Shumin0960ba12014-09-17 07:58:31 +00001558 FreePool(CleanFilePathStr);
jcarseya405b862010-09-14 05:18:09 +00001559 *ListHead = NULL;
1560 return (EFI_NOT_FOUND);
1561 }
Qiu Shumin0960ba12014-09-17 07:58:31 +00001562 FreePool(CleanFilePathStr);
jcarsey2247dde2009-11-09 18:08:58 +00001563 return (Status);
jcarsey1e6e84c2010-01-25 20:05:08 +00001564 }
jcarsey94b17fa2009-05-07 18:46:18 +00001565
1566 //
jcarsey92a54472011-06-27 20:33:13 +00001567 // Check for EFI shell
jcarsey94b17fa2009-05-07 18:46:18 +00001568 //
jcarsey92a54472011-06-27 20:33:13 +00001569 if (mEfiShellEnvironment2 != NULL) {
1570 //
1571 // make sure the list head is initialized
1572 //
1573 InitializeListHead(&mOldStyleFileList);
jcarsey94b17fa2009-05-07 18:46:18 +00001574
jcarsey92a54472011-06-27 20:33:13 +00001575 //
1576 // Get the EFI Shell list of files
1577 //
Qiu Shumin0960ba12014-09-17 07:58:31 +00001578 Status = mEfiShellEnvironment2->FileMetaArg(CleanFilePathStr, &mOldStyleFileList);
jcarsey92a54472011-06-27 20:33:13 +00001579 if (EFI_ERROR(Status)) {
1580 *ListHead = NULL;
Qiu Shumin0960ba12014-09-17 07:58:31 +00001581 FreePool(CleanFilePathStr);
jcarsey92a54472011-06-27 20:33:13 +00001582 return (Status);
1583 }
jcarsey94b17fa2009-05-07 18:46:18 +00001584
jcarsey92a54472011-06-27 20:33:13 +00001585 if (*ListHead == NULL) {
1586 *ListHead = (EFI_SHELL_FILE_INFO *)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));
1587 if (*ListHead == NULL) {
Qiu Shumin0960ba12014-09-17 07:58:31 +00001588 FreePool(CleanFilePathStr);
jcarsey92a54472011-06-27 20:33:13 +00001589 return (EFI_OUT_OF_RESOURCES);
1590 }
1591 InitializeListHead(&((*ListHead)->Link));
1592 }
1593
1594 //
1595 // Convert that to equivalent of UEFI Shell 2.0 structure
1596 //
1597 InternalShellConvertFileListType(&mOldStyleFileList, &(*ListHead)->Link);
1598
1599 //
1600 // Free the EFI Shell version that was converted.
1601 //
1602 mEfiShellEnvironment2->FreeFileList(&mOldStyleFileList);
1603
1604 if ((*ListHead)->Link.ForwardLink == (*ListHead)->Link.BackLink && (*ListHead)->Link.BackLink == &((*ListHead)->Link)) {
1605 FreePool(*ListHead);
1606 *ListHead = NULL;
1607 Status = EFI_NOT_FOUND;
1608 }
Qiu Shumin0960ba12014-09-17 07:58:31 +00001609 FreePool(CleanFilePathStr);
jcarsey94b17fa2009-05-07 18:46:18 +00001610 return (Status);
1611 }
1612
Qiu Shumin0960ba12014-09-17 07:58:31 +00001613 FreePool(CleanFilePathStr);
jcarsey92a54472011-06-27 20:33:13 +00001614 return (EFI_UNSUPPORTED);
jcarsey94b17fa2009-05-07 18:46:18 +00001615}
1616/**
jcarseya405b862010-09-14 05:18:09 +00001617 Free the linked list returned from ShellOpenFileMetaArg.
jcarsey94b17fa2009-05-07 18:46:18 +00001618
jcarseya405b862010-09-14 05:18:09 +00001619 if ListHead is NULL then ASSERT().
jcarsey94b17fa2009-05-07 18:46:18 +00001620
jcarseya405b862010-09-14 05:18:09 +00001621 @param ListHead the pointer to free.
jcarsey94b17fa2009-05-07 18:46:18 +00001622
jcarseya405b862010-09-14 05:18:09 +00001623 @retval EFI_SUCCESS the operation was sucessful.
jcarsey94b17fa2009-05-07 18:46:18 +00001624**/
1625EFI_STATUS
1626EFIAPI
1627ShellCloseFileMetaArg (
1628 IN OUT EFI_SHELL_FILE_INFO **ListHead
1629 )
1630{
1631 LIST_ENTRY *Node;
1632
1633 //
1634 // ASSERT that ListHead is not NULL
1635 //
1636 ASSERT(ListHead != NULL);
1637
jcarsey1e6e84c2010-01-25 20:05:08 +00001638 //
jcarsey94b17fa2009-05-07 18:46:18 +00001639 // Check for UEFI Shell 2.0 protocols
1640 //
jcarsey366f81a2011-06-27 21:04:22 +00001641 if (gEfiShellProtocol != NULL) {
1642 return (gEfiShellProtocol->FreeFileList(ListHead));
jcarsey92a54472011-06-27 20:33:13 +00001643 } else if (mEfiShellEnvironment2 != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00001644 //
jcarsey1e6e84c2010-01-25 20:05:08 +00001645 // Since this is EFI Shell version we need to free our internally made copy
jcarsey94b17fa2009-05-07 18:46:18 +00001646 // of the list
1647 //
jcarsey1e6e84c2010-01-25 20:05:08 +00001648 for ( Node = GetFirstNode(&(*ListHead)->Link)
jcarseya405b862010-09-14 05:18:09 +00001649 ; *ListHead != NULL && !IsListEmpty(&(*ListHead)->Link)
jcarsey9b3bf082009-06-23 21:15:07 +00001650 ; Node = GetFirstNode(&(*ListHead)->Link)) {
jcarsey94b17fa2009-05-07 18:46:18 +00001651 RemoveEntryList(Node);
jcarseya405b862010-09-14 05:18:09 +00001652 ((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 +00001653 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->FullName);
1654 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->FileName);
1655 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->Info);
1656 FreePool((EFI_SHELL_FILE_INFO_NO_CONST*)Node);
1657 }
Jaben Carsey75a5e2e2014-01-10 16:42:45 +00001658 SHELL_FREE_NON_NULL(*ListHead);
jcarsey94b17fa2009-05-07 18:46:18 +00001659 return EFI_SUCCESS;
1660 }
jcarsey92a54472011-06-27 20:33:13 +00001661
1662 return (EFI_UNSUPPORTED);
jcarsey94b17fa2009-05-07 18:46:18 +00001663}
1664
jcarsey125c2cf2009-11-18 21:36:50 +00001665/**
1666 Find a file by searching the CWD and then the path.
1667
jcarseyb3011f42010-01-11 21:49:04 +00001668 If FileName is NULL then ASSERT.
jcarsey125c2cf2009-11-18 21:36:50 +00001669
jcarseyb3011f42010-01-11 21:49:04 +00001670 If the return value is not NULL then the memory must be caller freed.
jcarsey125c2cf2009-11-18 21:36:50 +00001671
1672 @param FileName Filename string.
1673
1674 @retval NULL the file was not found
1675 @return !NULL the full path to the file.
1676**/
1677CHAR16 *
1678EFIAPI
1679ShellFindFilePath (
1680 IN CONST CHAR16 *FileName
1681 )
1682{
1683 CONST CHAR16 *Path;
jcarseya405b862010-09-14 05:18:09 +00001684 SHELL_FILE_HANDLE Handle;
jcarsey125c2cf2009-11-18 21:36:50 +00001685 EFI_STATUS Status;
1686 CHAR16 *RetVal;
1687 CHAR16 *TestPath;
1688 CONST CHAR16 *Walker;
jcarsey36a9d672009-11-20 21:13:41 +00001689 UINTN Size;
jcarsey1cd45e72010-01-29 15:07:44 +00001690 CHAR16 *TempChar;
jcarsey125c2cf2009-11-18 21:36:50 +00001691
1692 RetVal = NULL;
1693
jcarseya405b862010-09-14 05:18:09 +00001694 //
1695 // First make sure its not an absolute path.
1696 //
1697 Status = ShellOpenFileByName(FileName, &Handle, EFI_FILE_MODE_READ, 0);
1698 if (!EFI_ERROR(Status)){
1699 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {
1700 ASSERT(RetVal == NULL);
1701 RetVal = StrnCatGrow(&RetVal, NULL, FileName, 0);
1702 ShellCloseFile(&Handle);
1703 return (RetVal);
1704 } else {
1705 ShellCloseFile(&Handle);
1706 }
1707 }
1708
jcarsey125c2cf2009-11-18 21:36:50 +00001709 Path = ShellGetEnvironmentVariable(L"cwd");
1710 if (Path != NULL) {
jcarsey36a9d672009-11-20 21:13:41 +00001711 Size = StrSize(Path);
1712 Size += StrSize(FileName);
1713 TestPath = AllocateZeroPool(Size);
jcarseyc9d92df2010-02-03 15:37:54 +00001714 if (TestPath == NULL) {
1715 return (NULL);
1716 }
Qiu Shumine75390f2015-06-30 03:18:31 +00001717 StrCpyS(TestPath, Size/sizeof(CHAR16), Path);
1718 StrCatS(TestPath, Size/sizeof(CHAR16), FileName);
jcarsey125c2cf2009-11-18 21:36:50 +00001719 Status = ShellOpenFileByName(TestPath, &Handle, EFI_FILE_MODE_READ, 0);
1720 if (!EFI_ERROR(Status)){
jcarseya405b862010-09-14 05:18:09 +00001721 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {
1722 ASSERT(RetVal == NULL);
1723 RetVal = StrnCatGrow(&RetVal, NULL, TestPath, 0);
1724 ShellCloseFile(&Handle);
1725 FreePool(TestPath);
1726 return (RetVal);
1727 } else {
1728 ShellCloseFile(&Handle);
1729 }
jcarsey125c2cf2009-11-18 21:36:50 +00001730 }
1731 FreePool(TestPath);
1732 }
1733 Path = ShellGetEnvironmentVariable(L"path");
1734 if (Path != NULL) {
jcarseya405b862010-09-14 05:18:09 +00001735 Size = StrSize(Path)+sizeof(CHAR16);
jcarsey36a9d672009-11-20 21:13:41 +00001736 Size += StrSize(FileName);
1737 TestPath = AllocateZeroPool(Size);
jcarsey3e082d52010-10-04 16:44:57 +00001738 if (TestPath == NULL) {
1739 return (NULL);
1740 }
jcarsey1e6e84c2010-01-25 20:05:08 +00001741 Walker = (CHAR16*)Path;
jcarsey125c2cf2009-11-18 21:36:50 +00001742 do {
1743 CopyMem(TestPath, Walker, StrSize(Walker));
jcarsey3e082d52010-10-04 16:44:57 +00001744 if (TestPath != NULL) {
1745 TempChar = StrStr(TestPath, L";");
1746 if (TempChar != NULL) {
1747 *TempChar = CHAR_NULL;
1748 }
1749 if (TestPath[StrLen(TestPath)-1] != L'\\') {
Qiu Shumine75390f2015-06-30 03:18:31 +00001750 StrCatS(TestPath, Size/sizeof(CHAR16), L"\\");
jcarsey3e082d52010-10-04 16:44:57 +00001751 }
jcarsey89e85372011-04-13 23:37:50 +00001752 if (FileName[0] == L'\\') {
1753 FileName++;
1754 }
Qiu Shumine75390f2015-06-30 03:18:31 +00001755 StrCatS(TestPath, Size/sizeof(CHAR16), FileName);
jcarsey3e082d52010-10-04 16:44:57 +00001756 if (StrStr(Walker, L";") != NULL) {
1757 Walker = StrStr(Walker, L";") + 1;
jcarseya405b862010-09-14 05:18:09 +00001758 } else {
jcarsey3e082d52010-10-04 16:44:57 +00001759 Walker = NULL;
1760 }
1761 Status = ShellOpenFileByName(TestPath, &Handle, EFI_FILE_MODE_READ, 0);
1762 if (!EFI_ERROR(Status)){
1763 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {
1764 ASSERT(RetVal == NULL);
1765 RetVal = StrnCatGrow(&RetVal, NULL, TestPath, 0);
1766 ShellCloseFile(&Handle);
1767 break;
1768 } else {
1769 ShellCloseFile(&Handle);
1770 }
jcarseya405b862010-09-14 05:18:09 +00001771 }
jcarsey125c2cf2009-11-18 21:36:50 +00001772 }
1773 } while (Walker != NULL && Walker[0] != CHAR_NULL);
1774 FreePool(TestPath);
1775 }
1776 return (RetVal);
1777}
1778
jcarseyb3011f42010-01-11 21:49:04 +00001779/**
jcarsey1e6e84c2010-01-25 20:05:08 +00001780 Find a file by searching the CWD and then the path with a variable set of file
1781 extensions. If the file is not found it will append each extension in the list
jcarseyb3011f42010-01-11 21:49:04 +00001782 in the order provided and return the first one that is successful.
1783
1784 If FileName is NULL, then ASSERT.
1785 If FileExtension is NULL, then behavior is identical to ShellFindFilePath.
1786
1787 If the return value is not NULL then the memory must be caller freed.
1788
1789 @param[in] FileName Filename string.
1790 @param[in] FileExtension Semi-colon delimeted list of possible extensions.
1791
1792 @retval NULL The file was not found.
1793 @retval !NULL The path to the file.
1794**/
1795CHAR16 *
1796EFIAPI
1797ShellFindFilePathEx (
1798 IN CONST CHAR16 *FileName,
1799 IN CONST CHAR16 *FileExtension
1800 )
1801{
1802 CHAR16 *TestPath;
1803 CHAR16 *RetVal;
1804 CONST CHAR16 *ExtensionWalker;
jcarsey9e926b62010-01-14 20:26:39 +00001805 UINTN Size;
jcarsey1cd45e72010-01-29 15:07:44 +00001806 CHAR16 *TempChar;
jcarseyc9d92df2010-02-03 15:37:54 +00001807 CHAR16 *TempChar2;
jcarsey1cd45e72010-01-29 15:07:44 +00001808
jcarseyb3011f42010-01-11 21:49:04 +00001809 ASSERT(FileName != NULL);
1810 if (FileExtension == NULL) {
1811 return (ShellFindFilePath(FileName));
1812 }
1813 RetVal = ShellFindFilePath(FileName);
1814 if (RetVal != NULL) {
1815 return (RetVal);
1816 }
jcarsey9e926b62010-01-14 20:26:39 +00001817 Size = StrSize(FileName);
1818 Size += StrSize(FileExtension);
1819 TestPath = AllocateZeroPool(Size);
jcarseyc9d92df2010-02-03 15:37:54 +00001820 if (TestPath == NULL) {
1821 return (NULL);
1822 }
jcarseya405b862010-09-14 05:18:09 +00001823 for (ExtensionWalker = FileExtension, TempChar2 = (CHAR16*)FileExtension; TempChar2 != NULL ; ExtensionWalker = TempChar2 + 1){
Qiu Shumine75390f2015-06-30 03:18:31 +00001824 StrCpyS(TestPath, Size/sizeof(CHAR16), FileName);
jcarseya405b862010-09-14 05:18:09 +00001825 if (ExtensionWalker != NULL) {
Qiu Shumine75390f2015-06-30 03:18:31 +00001826 StrCatS(TestPath, Size/sizeof(CHAR16), ExtensionWalker);
jcarseya405b862010-09-14 05:18:09 +00001827 }
jcarsey1cd45e72010-01-29 15:07:44 +00001828 TempChar = StrStr(TestPath, L";");
1829 if (TempChar != NULL) {
1830 *TempChar = CHAR_NULL;
jcarseyb3011f42010-01-11 21:49:04 +00001831 }
1832 RetVal = ShellFindFilePath(TestPath);
1833 if (RetVal != NULL) {
1834 break;
1835 }
jcarseya405b862010-09-14 05:18:09 +00001836 ASSERT(ExtensionWalker != NULL);
jcarseyc9d92df2010-02-03 15:37:54 +00001837 TempChar2 = StrStr(ExtensionWalker, L";");
jcarseyb3011f42010-01-11 21:49:04 +00001838 }
1839 FreePool(TestPath);
1840 return (RetVal);
1841}
1842
jcarsey94b17fa2009-05-07 18:46:18 +00001843typedef struct {
jcarsey9b3bf082009-06-23 21:15:07 +00001844 LIST_ENTRY Link;
jcarsey94b17fa2009-05-07 18:46:18 +00001845 CHAR16 *Name;
jcarseya405b862010-09-14 05:18:09 +00001846 SHELL_PARAM_TYPE Type;
jcarsey94b17fa2009-05-07 18:46:18 +00001847 CHAR16 *Value;
1848 UINTN OriginalPosition;
1849} SHELL_PARAM_PACKAGE;
1850
1851/**
jcarsey1e6e84c2010-01-25 20:05:08 +00001852 Checks the list of valid arguments and returns TRUE if the item was found. If the
jcarsey94b17fa2009-05-07 18:46:18 +00001853 return value is TRUE then the type parameter is set also.
jcarsey1e6e84c2010-01-25 20:05:08 +00001854
jcarsey94b17fa2009-05-07 18:46:18 +00001855 if CheckList is NULL then ASSERT();
1856 if Name is NULL then ASSERT();
1857 if Type is NULL then ASSERT();
1858
jcarsey94b17fa2009-05-07 18:46:18 +00001859 @param Name pointer to Name of parameter found
1860 @param CheckList List to check against
jcarseya405b862010-09-14 05:18:09 +00001861 @param Type pointer to type of parameter if it was found
jcarsey94b17fa2009-05-07 18:46:18 +00001862
1863 @retval TRUE the Parameter was found. Type is valid.
1864 @retval FALSE the Parameter was not found. Type is not valid.
1865**/
1866BOOLEAN
1867EFIAPI
jcarseyd2b45642009-05-11 18:02:16 +00001868InternalIsOnCheckList (
jcarsey94b17fa2009-05-07 18:46:18 +00001869 IN CONST CHAR16 *Name,
1870 IN CONST SHELL_PARAM_ITEM *CheckList,
jcarsey252d9452011-03-25 20:49:53 +00001871 OUT SHELL_PARAM_TYPE *Type
jcarseya405b862010-09-14 05:18:09 +00001872 )
1873{
jcarsey94b17fa2009-05-07 18:46:18 +00001874 SHELL_PARAM_ITEM *TempListItem;
jcarsey252d9452011-03-25 20:49:53 +00001875 CHAR16 *TempString;
jcarsey94b17fa2009-05-07 18:46:18 +00001876
1877 //
1878 // ASSERT that all 3 pointer parameters aren't NULL
1879 //
1880 ASSERT(CheckList != NULL);
1881 ASSERT(Type != NULL);
1882 ASSERT(Name != NULL);
1883
1884 //
jcarseyd2b45642009-05-11 18:02:16 +00001885 // question mark and page break mode are always supported
1886 //
1887 if ((StrCmp(Name, L"-?") == 0) ||
1888 (StrCmp(Name, L"-b") == 0)
jcarseya405b862010-09-14 05:18:09 +00001889 ) {
jcarsey252d9452011-03-25 20:49:53 +00001890 *Type = TypeFlag;
jcarseyd2b45642009-05-11 18:02:16 +00001891 return (TRUE);
1892 }
1893
1894 //
jcarsey94b17fa2009-05-07 18:46:18 +00001895 // Enumerate through the list
1896 //
1897 for (TempListItem = (SHELL_PARAM_ITEM*)CheckList ; TempListItem->Name != NULL ; TempListItem++) {
1898 //
jcarsey9eb53ac2009-07-08 17:26:58 +00001899 // If the Type is TypeStart only check the first characters of the passed in param
1900 // If it matches set the type and return TRUE
jcarsey94b17fa2009-05-07 18:46:18 +00001901 //
darylm503b0934ac2011-11-12 00:35:11 +00001902 if (TempListItem->Type == TypeStart) {
jcarsey252d9452011-03-25 20:49:53 +00001903 if (StrnCmp(Name, TempListItem->Name, StrLen(TempListItem->Name)) == 0) {
1904 *Type = TempListItem->Type;
1905 return (TRUE);
1906 }
1907 TempString = NULL;
1908 TempString = StrnCatGrow(&TempString, NULL, Name, StrLen(TempListItem->Name));
1909 if (TempString != NULL) {
1910 if (StringNoCaseCompare(&TempString, &TempListItem->Name) == 0) {
1911 *Type = TempListItem->Type;
1912 FreePool(TempString);
1913 return (TRUE);
1914 }
1915 FreePool(TempString);
1916 }
1917 } else if (StringNoCaseCompare(&Name, &TempListItem->Name) == 0) {
jcarsey94b17fa2009-05-07 18:46:18 +00001918 *Type = TempListItem->Type;
1919 return (TRUE);
1920 }
1921 }
jcarsey2247dde2009-11-09 18:08:58 +00001922
jcarsey94b17fa2009-05-07 18:46:18 +00001923 return (FALSE);
1924}
1925/**
jcarseyd2b45642009-05-11 18:02:16 +00001926 Checks the string for indicators of "flag" status. this is a leading '/', '-', or '+'
jcarsey94b17fa2009-05-07 18:46:18 +00001927
jcarseya405b862010-09-14 05:18:09 +00001928 @param[in] Name pointer to Name of parameter found
1929 @param[in] AlwaysAllowNumbers TRUE to allow numbers, FALSE to not.
jcarsey658bf432014-11-04 22:33:16 +00001930 @param[in] TimeNumbers TRUE to allow numbers with ":", FALSE otherwise.
jcarsey94b17fa2009-05-07 18:46:18 +00001931
1932 @retval TRUE the Parameter is a flag.
jcarseya405b862010-09-14 05:18:09 +00001933 @retval FALSE the Parameter not a flag.
jcarsey94b17fa2009-05-07 18:46:18 +00001934**/
1935BOOLEAN
1936EFIAPI
jcarseyd2b45642009-05-11 18:02:16 +00001937InternalIsFlag (
jcarsey2247dde2009-11-09 18:08:58 +00001938 IN CONST CHAR16 *Name,
jcarsey658bf432014-11-04 22:33:16 +00001939 IN CONST BOOLEAN AlwaysAllowNumbers,
1940 IN CONST BOOLEAN TimeNumbers
jcarsey94b17fa2009-05-07 18:46:18 +00001941 )
1942{
1943 //
1944 // ASSERT that Name isn't NULL
1945 //
1946 ASSERT(Name != NULL);
1947
1948 //
jcarsey2247dde2009-11-09 18:08:58 +00001949 // If we accept numbers then dont return TRUE. (they will be values)
1950 //
jcarsey658bf432014-11-04 22:33:16 +00001951 if (((Name[0] == L'-' || Name[0] == L'+') && InternalShellIsHexOrDecimalNumber(Name+1, FALSE, FALSE, TimeNumbers)) && AlwaysAllowNumbers) {
jcarsey2247dde2009-11-09 18:08:58 +00001952 return (FALSE);
1953 }
1954
1955 //
jcarseya405b862010-09-14 05:18:09 +00001956 // If the Name has a /, +, or - as the first character return TRUE
jcarsey94b17fa2009-05-07 18:46:18 +00001957 //
jcarsey1e6e84c2010-01-25 20:05:08 +00001958 if ((Name[0] == L'/') ||
jcarseyd2b45642009-05-11 18:02:16 +00001959 (Name[0] == L'-') ||
1960 (Name[0] == L'+')
jcarseya405b862010-09-14 05:18:09 +00001961 ) {
jcarsey94b17fa2009-05-07 18:46:18 +00001962 return (TRUE);
1963 }
1964 return (FALSE);
1965}
1966
1967/**
jcarsey1e6e84c2010-01-25 20:05:08 +00001968 Checks the command line arguments passed against the list of valid ones.
jcarsey94b17fa2009-05-07 18:46:18 +00001969
1970 If no initialization is required, then return RETURN_SUCCESS.
jcarsey1e6e84c2010-01-25 20:05:08 +00001971
jcarseya405b862010-09-14 05:18:09 +00001972 @param[in] CheckList pointer to list of parameters to check
1973 @param[out] CheckPackage pointer to pointer to list checked values
1974 @param[out] ProblemParam optional pointer to pointer to unicode string for
jcarseyd2b45642009-05-11 18:02:16 +00001975 the paramater that caused failure. If used then the
1976 caller is responsible for freeing the memory.
jcarseya405b862010-09-14 05:18:09 +00001977 @param[in] AutoPageBreak will automatically set PageBreakEnabled for "b" parameter
1978 @param[in] Argv pointer to array of parameters
1979 @param[in] Argc Count of parameters in Argv
1980 @param[in] AlwaysAllowNumbers TRUE to allow numbers always, FALSE otherwise.
jcarsey94b17fa2009-05-07 18:46:18 +00001981
1982 @retval EFI_SUCCESS The operation completed sucessfully.
1983 @retval EFI_OUT_OF_RESOURCES A memory allocation failed
1984 @retval EFI_INVALID_PARAMETER A parameter was invalid
jcarsey1e6e84c2010-01-25 20:05:08 +00001985 @retval EFI_VOLUME_CORRUPTED the command line was corrupt. an argument was
1986 duplicated. the duplicated command line argument
jcarsey94b17fa2009-05-07 18:46:18 +00001987 was returned in ProblemParam if provided.
jcarsey1e6e84c2010-01-25 20:05:08 +00001988 @retval EFI_NOT_FOUND a argument required a value that was missing.
jcarsey94b17fa2009-05-07 18:46:18 +00001989 the invalid command line argument was returned in
1990 ProblemParam if provided.
1991**/
1992EFI_STATUS
1993EFIAPI
1994InternalCommandLineParse (
1995 IN CONST SHELL_PARAM_ITEM *CheckList,
1996 OUT LIST_ENTRY **CheckPackage,
1997 OUT CHAR16 **ProblemParam OPTIONAL,
1998 IN BOOLEAN AutoPageBreak,
1999 IN CONST CHAR16 **Argv,
jcarsey2247dde2009-11-09 18:08:58 +00002000 IN UINTN Argc,
2001 IN BOOLEAN AlwaysAllowNumbers
jcarseya405b862010-09-14 05:18:09 +00002002 )
2003{
jcarsey94b17fa2009-05-07 18:46:18 +00002004 UINTN LoopCounter;
jcarsey252d9452011-03-25 20:49:53 +00002005 SHELL_PARAM_TYPE CurrentItemType;
jcarsey94b17fa2009-05-07 18:46:18 +00002006 SHELL_PARAM_PACKAGE *CurrentItemPackage;
jcarsey125c2cf2009-11-18 21:36:50 +00002007 UINTN GetItemValue;
2008 UINTN ValueSize;
jcarseya405b862010-09-14 05:18:09 +00002009 UINTN Count;
jcarsey252d9452011-03-25 20:49:53 +00002010 CONST CHAR16 *TempPointer;
Jaben Carsey98c16be2014-08-19 21:00:34 +00002011 UINTN CurrentValueSize;
jcarsey94b17fa2009-05-07 18:46:18 +00002012
2013 CurrentItemPackage = NULL;
jcarsey125c2cf2009-11-18 21:36:50 +00002014 GetItemValue = 0;
2015 ValueSize = 0;
jcarseya405b862010-09-14 05:18:09 +00002016 Count = 0;
jcarsey94b17fa2009-05-07 18:46:18 +00002017
2018 //
2019 // If there is only 1 item we dont need to do anything
2020 //
jcarseya405b862010-09-14 05:18:09 +00002021 if (Argc < 1) {
jcarsey94b17fa2009-05-07 18:46:18 +00002022 *CheckPackage = NULL;
2023 return (EFI_SUCCESS);
2024 }
2025
2026 //
jcarsey2247dde2009-11-09 18:08:58 +00002027 // ASSERTs
2028 //
2029 ASSERT(CheckList != NULL);
2030 ASSERT(Argv != NULL);
2031
2032 //
jcarsey94b17fa2009-05-07 18:46:18 +00002033 // initialize the linked list
2034 //
2035 *CheckPackage = (LIST_ENTRY*)AllocateZeroPool(sizeof(LIST_ENTRY));
sfu502a758c2011-10-08 02:55:30 +00002036 if (*CheckPackage == NULL) {
jcarseybeab0fc2011-10-10 17:26:25 +00002037 return (EFI_OUT_OF_RESOURCES);
sfu502a758c2011-10-08 02:55:30 +00002038 }
jcarseybeab0fc2011-10-10 17:26:25 +00002039
jcarsey94b17fa2009-05-07 18:46:18 +00002040 InitializeListHead(*CheckPackage);
2041
2042 //
2043 // loop through each of the arguments
2044 //
2045 for (LoopCounter = 0 ; LoopCounter < Argc ; ++LoopCounter) {
2046 if (Argv[LoopCounter] == NULL) {
2047 //
2048 // do nothing for NULL argv
2049 //
jcarseya405b862010-09-14 05:18:09 +00002050 } else if (InternalIsOnCheckList(Argv[LoopCounter], CheckList, &CurrentItemType)) {
jcarsey94b17fa2009-05-07 18:46:18 +00002051 //
jcarsey2247dde2009-11-09 18:08:58 +00002052 // We might have leftover if last parameter didnt have optional value
2053 //
jcarsey125c2cf2009-11-18 21:36:50 +00002054 if (GetItemValue != 0) {
2055 GetItemValue = 0;
jcarsey2247dde2009-11-09 18:08:58 +00002056 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
2057 }
2058 //
jcarsey94b17fa2009-05-07 18:46:18 +00002059 // this is a flag
2060 //
jcarsey252d9452011-03-25 20:49:53 +00002061 CurrentItemPackage = AllocateZeroPool(sizeof(SHELL_PARAM_PACKAGE));
jcarseybeab0fc2011-10-10 17:26:25 +00002062 if (CurrentItemPackage == NULL) {
2063 ShellCommandLineFreeVarList(*CheckPackage);
2064 *CheckPackage = NULL;
2065 return (EFI_OUT_OF_RESOURCES);
2066 }
Jaben Carsey98c16be2014-08-19 21:00:34 +00002067 CurrentItemPackage->Name = AllocateCopyPool(StrSize(Argv[LoopCounter]), Argv[LoopCounter]);
jcarseybeab0fc2011-10-10 17:26:25 +00002068 if (CurrentItemPackage->Name == NULL) {
2069 ShellCommandLineFreeVarList(*CheckPackage);
2070 *CheckPackage = NULL;
2071 return (EFI_OUT_OF_RESOURCES);
2072 }
jcarsey94b17fa2009-05-07 18:46:18 +00002073 CurrentItemPackage->Type = CurrentItemType;
2074 CurrentItemPackage->OriginalPosition = (UINTN)(-1);
jcarseyb1f95a02009-06-16 00:23:19 +00002075 CurrentItemPackage->Value = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +00002076
2077 //
2078 // Does this flag require a value
2079 //
jcarsey125c2cf2009-11-18 21:36:50 +00002080 switch (CurrentItemPackage->Type) {
jcarsey94b17fa2009-05-07 18:46:18 +00002081 //
jcarsey125c2cf2009-11-18 21:36:50 +00002082 // possibly trigger the next loop(s) to populate the value of this item
jcarsey1e6e84c2010-01-25 20:05:08 +00002083 //
jcarsey125c2cf2009-11-18 21:36:50 +00002084 case TypeValue:
jcarsey658bf432014-11-04 22:33:16 +00002085 case TypeTimeValue:
jcarsey1e6e84c2010-01-25 20:05:08 +00002086 GetItemValue = 1;
jcarsey125c2cf2009-11-18 21:36:50 +00002087 ValueSize = 0;
2088 break;
2089 case TypeDoubleValue:
2090 GetItemValue = 2;
2091 ValueSize = 0;
2092 break;
2093 case TypeMaxValue:
2094 GetItemValue = (UINTN)(-1);
2095 ValueSize = 0;
2096 break;
2097 default:
2098 //
2099 // this item has no value expected; we are done
2100 //
2101 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
2102 ASSERT(GetItemValue == 0);
2103 break;
jcarsey94b17fa2009-05-07 18:46:18 +00002104 }
Qiu Shuminaf7a3a52015-03-13 02:04:17 +00002105 } else if (GetItemValue != 0 && CurrentItemPackage != NULL && !InternalIsFlag(Argv[LoopCounter], AlwaysAllowNumbers, (BOOLEAN)(CurrentItemPackage->Type == TypeTimeValue))) {
jcarseyb1f95a02009-06-16 00:23:19 +00002106 //
jcarsey125c2cf2009-11-18 21:36:50 +00002107 // get the item VALUE for a previous flag
jcarseyb1f95a02009-06-16 00:23:19 +00002108 //
Qiu Shumin484dd082015-04-01 00:49:05 +00002109 CurrentValueSize = ValueSize + StrSize(Argv[LoopCounter]) + sizeof(CHAR16);
2110 CurrentItemPackage->Value = ReallocatePool(ValueSize, CurrentValueSize, CurrentItemPackage->Value);
2111 ASSERT(CurrentItemPackage->Value != NULL);
2112 if (ValueSize == 0) {
Qiu Shumine75390f2015-06-30 03:18:31 +00002113 StrCpyS( CurrentItemPackage->Value,
2114 CurrentValueSize/sizeof(CHAR16),
2115 Argv[LoopCounter]
2116 );
jcarsey125c2cf2009-11-18 21:36:50 +00002117 } else {
Qiu Shumine75390f2015-06-30 03:18:31 +00002118 StrCatS( CurrentItemPackage->Value,
2119 CurrentValueSize/sizeof(CHAR16),
2120 L" "
2121 );
2122 StrCatS( CurrentItemPackage->Value,
2123 CurrentValueSize/sizeof(CHAR16),
2124 Argv[LoopCounter]
2125 );
jcarsey125c2cf2009-11-18 21:36:50 +00002126 }
Qiu Shumin484dd082015-04-01 00:49:05 +00002127 ValueSize += StrSize(Argv[LoopCounter]) + sizeof(CHAR16);
2128
jcarsey125c2cf2009-11-18 21:36:50 +00002129 GetItemValue--;
2130 if (GetItemValue == 0) {
2131 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
2132 }
jcarsey658bf432014-11-04 22:33:16 +00002133 } else if (!InternalIsFlag(Argv[LoopCounter], AlwaysAllowNumbers, FALSE)){
jcarseyb1f95a02009-06-16 00:23:19 +00002134 //
2135 // add this one as a non-flag
2136 //
jcarsey252d9452011-03-25 20:49:53 +00002137
2138 TempPointer = Argv[LoopCounter];
darylm503b0934ac2011-11-12 00:35:11 +00002139 if ((*TempPointer == L'^' && *(TempPointer+1) == L'-')
jcarsey252d9452011-03-25 20:49:53 +00002140 || (*TempPointer == L'^' && *(TempPointer+1) == L'/')
2141 || (*TempPointer == L'^' && *(TempPointer+1) == L'+')
2142 ){
2143 TempPointer++;
2144 }
2145 CurrentItemPackage = AllocateZeroPool(sizeof(SHELL_PARAM_PACKAGE));
jcarseybeab0fc2011-10-10 17:26:25 +00002146 if (CurrentItemPackage == NULL) {
2147 ShellCommandLineFreeVarList(*CheckPackage);
2148 *CheckPackage = NULL;
2149 return (EFI_OUT_OF_RESOURCES);
2150 }
jcarseyb1f95a02009-06-16 00:23:19 +00002151 CurrentItemPackage->Name = NULL;
2152 CurrentItemPackage->Type = TypePosition;
Jaben Carsey98c16be2014-08-19 21:00:34 +00002153 CurrentItemPackage->Value = AllocateCopyPool(StrSize(TempPointer), TempPointer);
jcarseybeab0fc2011-10-10 17:26:25 +00002154 if (CurrentItemPackage->Value == NULL) {
2155 ShellCommandLineFreeVarList(*CheckPackage);
2156 *CheckPackage = NULL;
2157 return (EFI_OUT_OF_RESOURCES);
2158 }
jcarseya405b862010-09-14 05:18:09 +00002159 CurrentItemPackage->OriginalPosition = Count++;
jcarsey9b3bf082009-06-23 21:15:07 +00002160 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
jcarsey252d9452011-03-25 20:49:53 +00002161 } else {
jcarsey94b17fa2009-05-07 18:46:18 +00002162 //
2163 // this was a non-recognised flag... error!
2164 //
jcarsey252d9452011-03-25 20:49:53 +00002165 if (ProblemParam != NULL) {
Jaben Carsey98c16be2014-08-19 21:00:34 +00002166 *ProblemParam = AllocateCopyPool(StrSize(Argv[LoopCounter]), Argv[LoopCounter]);
jcarsey252d9452011-03-25 20:49:53 +00002167 }
jcarsey94b17fa2009-05-07 18:46:18 +00002168 ShellCommandLineFreeVarList(*CheckPackage);
2169 *CheckPackage = NULL;
2170 return (EFI_VOLUME_CORRUPTED);
jcarsey94b17fa2009-05-07 18:46:18 +00002171 }
2172 }
jcarsey125c2cf2009-11-18 21:36:50 +00002173 if (GetItemValue != 0) {
2174 GetItemValue = 0;
2175 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
2176 }
jcarsey94b17fa2009-05-07 18:46:18 +00002177 //
2178 // support for AutoPageBreak
2179 //
2180 if (AutoPageBreak && ShellCommandLineGetFlag(*CheckPackage, L"-b")) {
2181 ShellSetPageBreakMode(TRUE);
2182 }
2183 return (EFI_SUCCESS);
2184}
2185
2186/**
jcarsey1e6e84c2010-01-25 20:05:08 +00002187 Checks the command line arguments passed against the list of valid ones.
jcarsey94b17fa2009-05-07 18:46:18 +00002188 Optionally removes NULL values first.
jcarsey1e6e84c2010-01-25 20:05:08 +00002189
jcarsey94b17fa2009-05-07 18:46:18 +00002190 If no initialization is required, then return RETURN_SUCCESS.
jcarsey1e6e84c2010-01-25 20:05:08 +00002191
jcarseya405b862010-09-14 05:18:09 +00002192 @param[in] CheckList The pointer to list of parameters to check.
2193 @param[out] CheckPackage The package of checked values.
2194 @param[out] ProblemParam Optional pointer to pointer to unicode string for
jcarsey94b17fa2009-05-07 18:46:18 +00002195 the paramater that caused failure.
jcarseya405b862010-09-14 05:18:09 +00002196 @param[in] AutoPageBreak Will automatically set PageBreakEnabled.
2197 @param[in] AlwaysAllowNumbers Will never fail for number based flags.
jcarsey94b17fa2009-05-07 18:46:18 +00002198
2199 @retval EFI_SUCCESS The operation completed sucessfully.
jcarseya405b862010-09-14 05:18:09 +00002200 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
2201 @retval EFI_INVALID_PARAMETER A parameter was invalid.
2202 @retval EFI_VOLUME_CORRUPTED The command line was corrupt.
2203 @retval EFI_DEVICE_ERROR The commands contained 2 opposing arguments. One
jcarsey1e6e84c2010-01-25 20:05:08 +00002204 of the command line arguments was returned in
jcarsey94b17fa2009-05-07 18:46:18 +00002205 ProblemParam if provided.
jcarseya405b862010-09-14 05:18:09 +00002206 @retval EFI_NOT_FOUND A argument required a value that was missing.
2207 The invalid command line argument was returned in
jcarsey94b17fa2009-05-07 18:46:18 +00002208 ProblemParam if provided.
2209**/
2210EFI_STATUS
2211EFIAPI
jcarsey2247dde2009-11-09 18:08:58 +00002212ShellCommandLineParseEx (
jcarsey94b17fa2009-05-07 18:46:18 +00002213 IN CONST SHELL_PARAM_ITEM *CheckList,
2214 OUT LIST_ENTRY **CheckPackage,
2215 OUT CHAR16 **ProblemParam OPTIONAL,
jcarsey2247dde2009-11-09 18:08:58 +00002216 IN BOOLEAN AutoPageBreak,
2217 IN BOOLEAN AlwaysAllowNumbers
jcarseya405b862010-09-14 05:18:09 +00002218 )
2219{
jcarsey1e6e84c2010-01-25 20:05:08 +00002220 //
jcarsey94b17fa2009-05-07 18:46:18 +00002221 // ASSERT that CheckList and CheckPackage aren't NULL
2222 //
2223 ASSERT(CheckList != NULL);
2224 ASSERT(CheckPackage != NULL);
2225
jcarsey1e6e84c2010-01-25 20:05:08 +00002226 //
jcarsey94b17fa2009-05-07 18:46:18 +00002227 // Check for UEFI Shell 2.0 protocols
2228 //
jcarsey366f81a2011-06-27 21:04:22 +00002229 if (gEfiShellParametersProtocol != NULL) {
jcarsey1e6e84c2010-01-25 20:05:08 +00002230 return (InternalCommandLineParse(CheckList,
2231 CheckPackage,
2232 ProblemParam,
2233 AutoPageBreak,
jcarsey366f81a2011-06-27 21:04:22 +00002234 (CONST CHAR16**) gEfiShellParametersProtocol->Argv,
2235 gEfiShellParametersProtocol->Argc,
jcarsey2247dde2009-11-09 18:08:58 +00002236 AlwaysAllowNumbers));
jcarsey94b17fa2009-05-07 18:46:18 +00002237 }
2238
jcarsey1e6e84c2010-01-25 20:05:08 +00002239 //
jcarsey94b17fa2009-05-07 18:46:18 +00002240 // ASSERT That EFI Shell is not required
2241 //
2242 ASSERT (mEfiShellInterface != NULL);
jcarsey1e6e84c2010-01-25 20:05:08 +00002243 return (InternalCommandLineParse(CheckList,
2244 CheckPackage,
2245 ProblemParam,
2246 AutoPageBreak,
jljusten08d7f8e2009-06-15 18:42:13 +00002247 (CONST CHAR16**) mEfiShellInterface->Argv,
jcarsey2247dde2009-11-09 18:08:58 +00002248 mEfiShellInterface->Argc,
2249 AlwaysAllowNumbers));
jcarsey94b17fa2009-05-07 18:46:18 +00002250}
2251
2252/**
2253 Frees shell variable list that was returned from ShellCommandLineParse.
2254
2255 This function will free all the memory that was used for the CheckPackage
2256 list of postprocessed shell arguments.
2257
2258 this function has no return value.
2259
2260 if CheckPackage is NULL, then return
2261
2262 @param CheckPackage the list to de-allocate
2263 **/
2264VOID
2265EFIAPI
2266ShellCommandLineFreeVarList (
2267 IN LIST_ENTRY *CheckPackage
jcarseya405b862010-09-14 05:18:09 +00002268 )
2269{
jcarsey94b17fa2009-05-07 18:46:18 +00002270 LIST_ENTRY *Node;
2271
2272 //
2273 // check for CheckPackage == NULL
2274 //
2275 if (CheckPackage == NULL) {
2276 return;
2277 }
2278
2279 //
2280 // for each node in the list
2281 //
jcarsey9eb53ac2009-07-08 17:26:58 +00002282 for ( Node = GetFirstNode(CheckPackage)
jcarseya405b862010-09-14 05:18:09 +00002283 ; !IsListEmpty(CheckPackage)
jcarsey9eb53ac2009-07-08 17:26:58 +00002284 ; Node = GetFirstNode(CheckPackage)
jcarseya405b862010-09-14 05:18:09 +00002285 ){
jcarsey94b17fa2009-05-07 18:46:18 +00002286 //
2287 // Remove it from the list
2288 //
2289 RemoveEntryList(Node);
2290
2291 //
2292 // if it has a name free the name
2293 //
2294 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {
2295 FreePool(((SHELL_PARAM_PACKAGE*)Node)->Name);
2296 }
2297
2298 //
2299 // if it has a value free the value
2300 //
2301 if (((SHELL_PARAM_PACKAGE*)Node)->Value != NULL) {
2302 FreePool(((SHELL_PARAM_PACKAGE*)Node)->Value);
2303 }
jcarsey1e6e84c2010-01-25 20:05:08 +00002304
jcarsey94b17fa2009-05-07 18:46:18 +00002305 //
2306 // free the node structure
2307 //
2308 FreePool((SHELL_PARAM_PACKAGE*)Node);
2309 }
2310 //
2311 // free the list head node
2312 //
2313 FreePool(CheckPackage);
2314}
2315/**
2316 Checks for presence of a flag parameter
2317
2318 flag arguments are in the form of "-<Key>" or "/<Key>", but do not have a value following the key
2319
2320 if CheckPackage is NULL then return FALSE.
2321 if KeyString is NULL then ASSERT()
jcarsey1e6e84c2010-01-25 20:05:08 +00002322
jcarsey94b17fa2009-05-07 18:46:18 +00002323 @param CheckPackage The package of parsed command line arguments
2324 @param KeyString the Key of the command line argument to check for
2325
2326 @retval TRUE the flag is on the command line
2327 @retval FALSE the flag is not on the command line
2328 **/
2329BOOLEAN
2330EFIAPI
2331ShellCommandLineGetFlag (
jcarseya405b862010-09-14 05:18:09 +00002332 IN CONST LIST_ENTRY * CONST CheckPackage,
2333 IN CONST CHAR16 * CONST KeyString
2334 )
2335{
jcarsey94b17fa2009-05-07 18:46:18 +00002336 LIST_ENTRY *Node;
jcarsey252d9452011-03-25 20:49:53 +00002337 CHAR16 *TempString;
jcarsey94b17fa2009-05-07 18:46:18 +00002338
2339 //
ydong100c1950b2011-10-13 02:37:35 +00002340 // return FALSE for no package or KeyString is NULL
jcarsey94b17fa2009-05-07 18:46:18 +00002341 //
ydong100c1950b2011-10-13 02:37:35 +00002342 if (CheckPackage == NULL || KeyString == NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00002343 return (FALSE);
2344 }
2345
2346 //
2347 // enumerate through the list of parametrs
2348 //
jcarsey1e6e84c2010-01-25 20:05:08 +00002349 for ( Node = GetFirstNode(CheckPackage)
2350 ; !IsNull (CheckPackage, Node)
2351 ; Node = GetNextNode(CheckPackage, Node)
jcarsey252d9452011-03-25 20:49:53 +00002352 ){
jcarsey94b17fa2009-05-07 18:46:18 +00002353 //
2354 // If the Name matches, return TRUE (and there may be NULL name)
2355 //
2356 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {
jcarsey9eb53ac2009-07-08 17:26:58 +00002357 //
2358 // If Type is TypeStart then only compare the begining of the strings
2359 //
jcarsey252d9452011-03-25 20:49:53 +00002360 if (((SHELL_PARAM_PACKAGE*)Node)->Type == TypeStart) {
2361 if (StrnCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name, StrLen(KeyString)) == 0) {
2362 return (TRUE);
2363 }
2364 TempString = NULL;
2365 TempString = StrnCatGrow(&TempString, NULL, KeyString, StrLen(((SHELL_PARAM_PACKAGE*)Node)->Name));
2366 if (TempString != NULL) {
2367 if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {
2368 FreePool(TempString);
2369 return (TRUE);
2370 }
2371 FreePool(TempString);
2372 }
2373 } else if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {
jcarsey94b17fa2009-05-07 18:46:18 +00002374 return (TRUE);
2375 }
2376 }
2377 }
2378 return (FALSE);
2379}
2380/**
jcarseya405b862010-09-14 05:18:09 +00002381 Returns value from command line argument.
jcarsey94b17fa2009-05-07 18:46:18 +00002382
jcarseya405b862010-09-14 05:18:09 +00002383 Value parameters are in the form of "-<Key> value" or "/<Key> value".
jcarsey1e6e84c2010-01-25 20:05:08 +00002384
jcarseya405b862010-09-14 05:18:09 +00002385 If CheckPackage is NULL, then return NULL.
jcarsey94b17fa2009-05-07 18:46:18 +00002386
jcarseya405b862010-09-14 05:18:09 +00002387 @param[in] CheckPackage The package of parsed command line arguments.
2388 @param[in] KeyString The Key of the command line argument to check for.
jcarsey94b17fa2009-05-07 18:46:18 +00002389
jcarseya405b862010-09-14 05:18:09 +00002390 @retval NULL The flag is not on the command line.
2391 @retval !=NULL The pointer to unicode string of the value.
2392**/
jcarsey94b17fa2009-05-07 18:46:18 +00002393CONST CHAR16*
2394EFIAPI
2395ShellCommandLineGetValue (
2396 IN CONST LIST_ENTRY *CheckPackage,
2397 IN CHAR16 *KeyString
jcarseya405b862010-09-14 05:18:09 +00002398 )
2399{
jcarsey94b17fa2009-05-07 18:46:18 +00002400 LIST_ENTRY *Node;
jcarsey252d9452011-03-25 20:49:53 +00002401 CHAR16 *TempString;
jcarsey94b17fa2009-05-07 18:46:18 +00002402
2403 //
ydong100c1950b2011-10-13 02:37:35 +00002404 // return NULL for no package or KeyString is NULL
jcarsey94b17fa2009-05-07 18:46:18 +00002405 //
ydong100c1950b2011-10-13 02:37:35 +00002406 if (CheckPackage == NULL || KeyString == NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00002407 return (NULL);
2408 }
2409
2410 //
2411 // enumerate through the list of parametrs
2412 //
jcarsey1e6e84c2010-01-25 20:05:08 +00002413 for ( Node = GetFirstNode(CheckPackage)
2414 ; !IsNull (CheckPackage, Node)
2415 ; Node = GetNextNode(CheckPackage, Node)
jcarsey252d9452011-03-25 20:49:53 +00002416 ){
jcarsey94b17fa2009-05-07 18:46:18 +00002417 //
jcarsey252d9452011-03-25 20:49:53 +00002418 // If the Name matches, return TRUE (and there may be NULL name)
jcarsey94b17fa2009-05-07 18:46:18 +00002419 //
2420 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {
jcarsey9eb53ac2009-07-08 17:26:58 +00002421 //
2422 // If Type is TypeStart then only compare the begining of the strings
2423 //
jcarsey252d9452011-03-25 20:49:53 +00002424 if (((SHELL_PARAM_PACKAGE*)Node)->Type == TypeStart) {
2425 if (StrnCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name, StrLen(KeyString)) == 0) {
2426 return (((SHELL_PARAM_PACKAGE*)Node)->Name + StrLen(KeyString));
2427 }
2428 TempString = NULL;
2429 TempString = StrnCatGrow(&TempString, NULL, KeyString, StrLen(((SHELL_PARAM_PACKAGE*)Node)->Name));
2430 if (TempString != NULL) {
2431 if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {
2432 FreePool(TempString);
2433 return (((SHELL_PARAM_PACKAGE*)Node)->Name + StrLen(KeyString));
2434 }
2435 FreePool(TempString);
2436 }
2437 } else if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {
jcarsey94b17fa2009-05-07 18:46:18 +00002438 return (((SHELL_PARAM_PACKAGE*)Node)->Value);
2439 }
2440 }
2441 }
2442 return (NULL);
2443}
jcarseya405b862010-09-14 05:18:09 +00002444
jcarsey94b17fa2009-05-07 18:46:18 +00002445/**
jcarseya405b862010-09-14 05:18:09 +00002446 Returns raw value from command line argument.
jcarsey94b17fa2009-05-07 18:46:18 +00002447
jcarseya405b862010-09-14 05:18:09 +00002448 Raw value parameters are in the form of "value" in a specific position in the list.
jcarsey1e6e84c2010-01-25 20:05:08 +00002449
jcarseya405b862010-09-14 05:18:09 +00002450 If CheckPackage is NULL, then return NULL.
jcarsey94b17fa2009-05-07 18:46:18 +00002451
jcarseya405b862010-09-14 05:18:09 +00002452 @param[in] CheckPackage The package of parsed command line arguments.
2453 @param[in] Position The position of the value.
jcarsey94b17fa2009-05-07 18:46:18 +00002454
jcarseya405b862010-09-14 05:18:09 +00002455 @retval NULL The flag is not on the command line.
2456 @retval !=NULL The pointer to unicode string of the value.
jcarsey94b17fa2009-05-07 18:46:18 +00002457 **/
2458CONST CHAR16*
2459EFIAPI
2460ShellCommandLineGetRawValue (
jcarseya405b862010-09-14 05:18:09 +00002461 IN CONST LIST_ENTRY * CONST CheckPackage,
2462 IN UINTN Position
2463 )
2464{
jcarsey94b17fa2009-05-07 18:46:18 +00002465 LIST_ENTRY *Node;
2466
2467 //
2468 // check for CheckPackage == NULL
2469 //
2470 if (CheckPackage == NULL) {
2471 return (NULL);
2472 }
2473
2474 //
2475 // enumerate through the list of parametrs
2476 //
jcarsey1e6e84c2010-01-25 20:05:08 +00002477 for ( Node = GetFirstNode(CheckPackage)
2478 ; !IsNull (CheckPackage, Node)
2479 ; Node = GetNextNode(CheckPackage, Node)
jcarseya405b862010-09-14 05:18:09 +00002480 ){
jcarsey94b17fa2009-05-07 18:46:18 +00002481 //
2482 // If the position matches, return the value
2483 //
2484 if (((SHELL_PARAM_PACKAGE*)Node)->OriginalPosition == Position) {
2485 return (((SHELL_PARAM_PACKAGE*)Node)->Value);
2486 }
2487 }
2488 return (NULL);
jcarseyb1f95a02009-06-16 00:23:19 +00002489}
jcarsey2247dde2009-11-09 18:08:58 +00002490
2491/**
jcarsey1e6e84c2010-01-25 20:05:08 +00002492 returns the number of command line value parameters that were parsed.
2493
jcarsey2247dde2009-11-09 18:08:58 +00002494 this will not include flags.
2495
jcarseya405b862010-09-14 05:18:09 +00002496 @param[in] CheckPackage The package of parsed command line arguments.
2497
jcarsey2247dde2009-11-09 18:08:58 +00002498 @retval (UINTN)-1 No parsing has ocurred
2499 @return other The number of value parameters found
2500**/
2501UINTN
2502EFIAPI
2503ShellCommandLineGetCount(
jcarseya405b862010-09-14 05:18:09 +00002504 IN CONST LIST_ENTRY *CheckPackage
jcarsey125c2cf2009-11-18 21:36:50 +00002505 )
2506{
jcarseya405b862010-09-14 05:18:09 +00002507 LIST_ENTRY *Node1;
2508 UINTN Count;
2509
2510 if (CheckPackage == NULL) {
2511 return (0);
2512 }
2513 for ( Node1 = GetFirstNode(CheckPackage), Count = 0
2514 ; !IsNull (CheckPackage, Node1)
2515 ; Node1 = GetNextNode(CheckPackage, Node1)
2516 ){
2517 if (((SHELL_PARAM_PACKAGE*)Node1)->Name == NULL) {
2518 Count++;
2519 }
2520 }
2521 return (Count);
jcarsey2247dde2009-11-09 18:08:58 +00002522}
2523
jcarsey975136a2009-06-16 19:03:54 +00002524/**
Bruce Crancceb4eb2015-07-08 01:54:46 +00002525 Determines if a parameter is duplicated.
jcarsey36a9d672009-11-20 21:13:41 +00002526
jcarsey1e6e84c2010-01-25 20:05:08 +00002527 If Param is not NULL then it will point to a callee allocated string buffer
jcarsey36a9d672009-11-20 21:13:41 +00002528 with the parameter value if a duplicate is found.
2529
2530 If CheckPackage is NULL, then ASSERT.
2531
2532 @param[in] CheckPackage The package of parsed command line arguments.
2533 @param[out] Param Upon finding one, a pointer to the duplicated parameter.
2534
2535 @retval EFI_SUCCESS No parameters were duplicated.
2536 @retval EFI_DEVICE_ERROR A duplicate was found.
2537 **/
2538EFI_STATUS
2539EFIAPI
2540ShellCommandLineCheckDuplicate (
2541 IN CONST LIST_ENTRY *CheckPackage,
2542 OUT CHAR16 **Param
2543 )
2544{
2545 LIST_ENTRY *Node1;
2546 LIST_ENTRY *Node2;
jcarsey1e6e84c2010-01-25 20:05:08 +00002547
jcarsey36a9d672009-11-20 21:13:41 +00002548 ASSERT(CheckPackage != NULL);
2549
jcarsey1e6e84c2010-01-25 20:05:08 +00002550 for ( Node1 = GetFirstNode(CheckPackage)
2551 ; !IsNull (CheckPackage, Node1)
2552 ; Node1 = GetNextNode(CheckPackage, Node1)
jcarseya405b862010-09-14 05:18:09 +00002553 ){
jcarsey1e6e84c2010-01-25 20:05:08 +00002554 for ( Node2 = GetNextNode(CheckPackage, Node1)
2555 ; !IsNull (CheckPackage, Node2)
2556 ; Node2 = GetNextNode(CheckPackage, Node2)
jcarseya405b862010-09-14 05:18:09 +00002557 ){
2558 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 +00002559 if (Param != NULL) {
2560 *Param = NULL;
2561 *Param = StrnCatGrow(Param, NULL, ((SHELL_PARAM_PACKAGE*)Node1)->Name, 0);
2562 }
2563 return (EFI_DEVICE_ERROR);
2564 }
2565 }
2566 }
2567 return (EFI_SUCCESS);
2568}
2569
2570/**
jcarsey1e6e84c2010-01-25 20:05:08 +00002571 This is a find and replace function. Upon successful return the NewString is a copy of
jcarsey975136a2009-06-16 19:03:54 +00002572 SourceString with each instance of FindTarget replaced with ReplaceWith.
2573
jcarseyb3011f42010-01-11 21:49:04 +00002574 If SourceString and NewString overlap the behavior is undefined.
2575
jcarsey975136a2009-06-16 19:03:54 +00002576 If the string would grow bigger than NewSize it will halt and return error.
2577
ydong104ff7e372011-09-02 08:05:34 +00002578 @param[in] SourceString The string with source buffer.
2579 @param[in, out] NewString The string with resultant buffer.
2580 @param[in] NewSize The size in bytes of NewString.
2581 @param[in] FindTarget The string to look for.
2582 @param[in] ReplaceWith The string to replace FindTarget with.
2583 @param[in] SkipPreCarrot If TRUE will skip a FindTarget that has a '^'
2584 immediately before it.
2585 @param[in] ParameterReplacing If TRUE will add "" around items with spaces.
jcarsey975136a2009-06-16 19:03:54 +00002586
jcarsey969c7832010-01-13 16:46:33 +00002587 @retval EFI_INVALID_PARAMETER SourceString was NULL.
2588 @retval EFI_INVALID_PARAMETER NewString was NULL.
2589 @retval EFI_INVALID_PARAMETER FindTarget was NULL.
2590 @retval EFI_INVALID_PARAMETER ReplaceWith was NULL.
2591 @retval EFI_INVALID_PARAMETER FindTarget had length < 1.
2592 @retval EFI_INVALID_PARAMETER SourceString had length < 1.
jcarsey1e6e84c2010-01-25 20:05:08 +00002593 @retval EFI_BUFFER_TOO_SMALL NewSize was less than the minimum size to hold
jcarsey969c7832010-01-13 16:46:33 +00002594 the new string (truncation occurred).
jcarseya405b862010-09-14 05:18:09 +00002595 @retval EFI_SUCCESS The string was successfully copied with replacement.
jcarsey975136a2009-06-16 19:03:54 +00002596**/
jcarsey975136a2009-06-16 19:03:54 +00002597EFI_STATUS
2598EFIAPI
jcarseya405b862010-09-14 05:18:09 +00002599ShellCopySearchAndReplace(
jcarsey975136a2009-06-16 19:03:54 +00002600 IN CHAR16 CONST *SourceString,
jcarseya405b862010-09-14 05:18:09 +00002601 IN OUT CHAR16 *NewString,
jcarsey975136a2009-06-16 19:03:54 +00002602 IN UINTN NewSize,
2603 IN CONST CHAR16 *FindTarget,
jcarsey969c7832010-01-13 16:46:33 +00002604 IN CONST CHAR16 *ReplaceWith,
jcarseya405b862010-09-14 05:18:09 +00002605 IN CONST BOOLEAN SkipPreCarrot,
2606 IN CONST BOOLEAN ParameterReplacing
jcarsey1e6e84c2010-01-25 20:05:08 +00002607 )
jcarsey2247dde2009-11-09 18:08:58 +00002608{
jcarsey01582942009-07-10 19:46:17 +00002609 UINTN Size;
jcarseya405b862010-09-14 05:18:09 +00002610 CHAR16 *Replace;
2611
jcarsey975136a2009-06-16 19:03:54 +00002612 if ( (SourceString == NULL)
2613 || (NewString == NULL)
2614 || (FindTarget == NULL)
2615 || (ReplaceWith == NULL)
2616 || (StrLen(FindTarget) < 1)
2617 || (StrLen(SourceString) < 1)
jcarseya405b862010-09-14 05:18:09 +00002618 ){
jcarsey975136a2009-06-16 19:03:54 +00002619 return (EFI_INVALID_PARAMETER);
2620 }
jcarseya405b862010-09-14 05:18:09 +00002621 Replace = NULL;
2622 if (StrStr(ReplaceWith, L" ") == NULL || !ParameterReplacing) {
2623 Replace = StrnCatGrow(&Replace, NULL, ReplaceWith, 0);
2624 } else {
2625 Replace = AllocateZeroPool(StrSize(ReplaceWith) + 2*sizeof(CHAR16));
jcarseybeab0fc2011-10-10 17:26:25 +00002626 if (Replace != NULL) {
2627 UnicodeSPrint(Replace, StrSize(ReplaceWith) + 2*sizeof(CHAR16), L"\"%s\"", ReplaceWith);
2628 }
jcarseya405b862010-09-14 05:18:09 +00002629 }
jcarsey3e082d52010-10-04 16:44:57 +00002630 if (Replace == NULL) {
2631 return (EFI_OUT_OF_RESOURCES);
2632 }
Jaben Carsey98c16be2014-08-19 21:00:34 +00002633 NewString = ZeroMem(NewString, NewSize);
jcarsey2247dde2009-11-09 18:08:58 +00002634 while (*SourceString != CHAR_NULL) {
jcarsey969c7832010-01-13 16:46:33 +00002635 //
jcarseya405b862010-09-14 05:18:09 +00002636 // if we find the FindTarget and either Skip == FALSE or Skip and we
jcarsey969c7832010-01-13 16:46:33 +00002637 // dont have a carrot do a replace...
2638 //
jcarsey1e6e84c2010-01-25 20:05:08 +00002639 if (StrnCmp(SourceString, FindTarget, StrLen(FindTarget)) == 0
jcarseya405b862010-09-14 05:18:09 +00002640 && ((SkipPreCarrot && *(SourceString-1) != L'^') || !SkipPreCarrot)
2641 ){
jcarsey975136a2009-06-16 19:03:54 +00002642 SourceString += StrLen(FindTarget);
jcarsey01582942009-07-10 19:46:17 +00002643 Size = StrSize(NewString);
jcarseya405b862010-09-14 05:18:09 +00002644 if ((Size + (StrLen(Replace)*sizeof(CHAR16))) > NewSize) {
2645 FreePool(Replace);
jcarsey975136a2009-06-16 19:03:54 +00002646 return (EFI_BUFFER_TOO_SMALL);
2647 }
Qiu Shumine75390f2015-06-30 03:18:31 +00002648 StrCatS(NewString, NewSize/sizeof(CHAR16), Replace);
jcarsey975136a2009-06-16 19:03:54 +00002649 } else {
jcarsey01582942009-07-10 19:46:17 +00002650 Size = StrSize(NewString);
2651 if (Size + sizeof(CHAR16) > NewSize) {
jcarseya405b862010-09-14 05:18:09 +00002652 FreePool(Replace);
jcarsey975136a2009-06-16 19:03:54 +00002653 return (EFI_BUFFER_TOO_SMALL);
2654 }
Qiu Shumine75390f2015-06-30 03:18:31 +00002655 StrnCatS(NewString, NewSize/sizeof(CHAR16), SourceString, 1);
jcarsey975136a2009-06-16 19:03:54 +00002656 SourceString++;
2657 }
2658 }
jcarseya405b862010-09-14 05:18:09 +00002659 FreePool(Replace);
jcarsey975136a2009-06-16 19:03:54 +00002660 return (EFI_SUCCESS);
2661}
jcarseyb1f95a02009-06-16 00:23:19 +00002662
2663/**
jcarseye2f82972009-12-01 05:40:24 +00002664 Internal worker function to output a string.
2665
2666 This function will output a string to the correct StdOut.
2667
2668 @param[in] String The string to print out.
2669
2670 @retval EFI_SUCCESS The operation was sucessful.
2671 @retval !EFI_SUCCESS The operation failed.
2672**/
2673EFI_STATUS
2674EFIAPI
2675InternalPrintTo (
2676 IN CONST CHAR16 *String
2677 )
2678{
2679 UINTN Size;
2680 Size = StrSize(String) - sizeof(CHAR16);
jcarseya405b862010-09-14 05:18:09 +00002681 if (Size == 0) {
2682 return (EFI_SUCCESS);
2683 }
jcarsey366f81a2011-06-27 21:04:22 +00002684 if (gEfiShellParametersProtocol != NULL) {
2685 return (gEfiShellProtocol->WriteFile(gEfiShellParametersProtocol->StdOut, &Size, (VOID*)String));
jcarseye2f82972009-12-01 05:40:24 +00002686 }
2687 if (mEfiShellInterface != NULL) {
jcarsey06c355b2012-03-26 21:00:39 +00002688 if (mEfiShellInterface->RedirArgc == 0) {
jcarsey49bd4982012-01-27 18:42:43 +00002689 //
2690 // Divide in half for old shell. Must be string length not size.
jcarsey06c355b2012-03-26 21:00:39 +00002691 //
2692 Size /=2; // Divide in half only when no redirection.
2693 }
jcarseya405b862010-09-14 05:18:09 +00002694 return (mEfiShellInterface->StdOut->Write(mEfiShellInterface->StdOut, &Size, (VOID*)String));
jcarseye2f82972009-12-01 05:40:24 +00002695 }
2696 ASSERT(FALSE);
2697 return (EFI_UNSUPPORTED);
2698}
2699
2700/**
jcarseyb1f95a02009-06-16 00:23:19 +00002701 Print at a specific location on the screen.
2702
jcarseyf1b87e72009-06-17 00:52:11 +00002703 This function will move the cursor to a given screen location and print the specified string
jcarsey1e6e84c2010-01-25 20:05:08 +00002704
2705 If -1 is specified for either the Row or Col the current screen location for BOTH
jcarseyf1b87e72009-06-17 00:52:11 +00002706 will be used.
jcarseyb1f95a02009-06-16 00:23:19 +00002707
2708 if either Row or Col is out of range for the current console, then ASSERT
2709 if Format is NULL, then ASSERT
2710
jcarsey1e6e84c2010-01-25 20:05:08 +00002711 In addition to the standard %-based flags as supported by UefiLib Print() this supports
jcarseyb1f95a02009-06-16 00:23:19 +00002712 the following additional flags:
2713 %N - Set output attribute to normal
2714 %H - Set output attribute to highlight
2715 %E - Set output attribute to error
2716 %B - Set output attribute to blue color
2717 %V - Set output attribute to green color
2718
2719 Note: The background color is controlled by the shell command cls.
2720
jcarseyb1f95a02009-06-16 00:23:19 +00002721 @param[in] Col the column to print at
jcarsey252d9452011-03-25 20:49:53 +00002722 @param[in] Row the row to print at
jcarseyb1f95a02009-06-16 00:23:19 +00002723 @param[in] Format the format string
jcarsey2247dde2009-11-09 18:08:58 +00002724 @param[in] Marker the marker for the variable argument list
jcarseyb1f95a02009-06-16 00:23:19 +00002725
jcarseya405b862010-09-14 05:18:09 +00002726 @return EFI_SUCCESS The operation was successful.
2727 @return EFI_DEVICE_ERROR The console device reported an error.
jcarseyb1f95a02009-06-16 00:23:19 +00002728**/
jcarseya405b862010-09-14 05:18:09 +00002729EFI_STATUS
jcarseyb1f95a02009-06-16 00:23:19 +00002730EFIAPI
jcarsey2247dde2009-11-09 18:08:58 +00002731InternalShellPrintWorker(
jcarseyb1f95a02009-06-16 00:23:19 +00002732 IN INT32 Col OPTIONAL,
2733 IN INT32 Row OPTIONAL,
2734 IN CONST CHAR16 *Format,
jcarsey252d9452011-03-25 20:49:53 +00002735 IN VA_LIST Marker
jcarsey1e6e84c2010-01-25 20:05:08 +00002736 )
jcarsey2247dde2009-11-09 18:08:58 +00002737{
jcarseyb1f95a02009-06-16 00:23:19 +00002738 EFI_STATUS Status;
jcarsey975136a2009-06-16 19:03:54 +00002739 CHAR16 *ResumeLocation;
2740 CHAR16 *FormatWalker;
jcarseya405b862010-09-14 05:18:09 +00002741 UINTN OriginalAttribute;
jcarsey89e85372011-04-13 23:37:50 +00002742 CHAR16 *mPostReplaceFormat;
2743 CHAR16 *mPostReplaceFormat2;
2744
2745 mPostReplaceFormat = AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize));
2746 mPostReplaceFormat2 = AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize));
jcarseya405b862010-09-14 05:18:09 +00002747
jcarseyf8d3e682011-04-19 17:54:42 +00002748 if (mPostReplaceFormat == NULL || mPostReplaceFormat2 == NULL) {
2749 SHELL_FREE_NON_NULL(mPostReplaceFormat);
2750 SHELL_FREE_NON_NULL(mPostReplaceFormat2);
2751 return (EFI_OUT_OF_RESOURCES);
2752 }
2753
jcarseya405b862010-09-14 05:18:09 +00002754 Status = EFI_SUCCESS;
2755 OriginalAttribute = gST->ConOut->Mode->Attribute;
jcarsey1e6e84c2010-01-25 20:05:08 +00002756
jcarsey975136a2009-06-16 19:03:54 +00002757 //
2758 // Back and forth each time fixing up 1 of our flags...
2759 //
jcarseya405b862010-09-14 05:18:09 +00002760 Status = ShellCopySearchAndReplace(Format, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%N", L"%%N", FALSE, FALSE);
jcarsey975136a2009-06-16 19:03:54 +00002761 ASSERT_EFI_ERROR(Status);
jcarseya405b862010-09-14 05:18:09 +00002762 Status = ShellCopySearchAndReplace(mPostReplaceFormat, mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), L"%E", L"%%E", FALSE, FALSE);
jcarsey975136a2009-06-16 19:03:54 +00002763 ASSERT_EFI_ERROR(Status);
jcarseya405b862010-09-14 05:18:09 +00002764 Status = ShellCopySearchAndReplace(mPostReplaceFormat2, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%H", L"%%H", FALSE, FALSE);
jcarsey975136a2009-06-16 19:03:54 +00002765 ASSERT_EFI_ERROR(Status);
jcarseya405b862010-09-14 05:18:09 +00002766 Status = ShellCopySearchAndReplace(mPostReplaceFormat, mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), L"%B", L"%%B", FALSE, FALSE);
jcarsey975136a2009-06-16 19:03:54 +00002767 ASSERT_EFI_ERROR(Status);
jcarseya405b862010-09-14 05:18:09 +00002768 Status = ShellCopySearchAndReplace(mPostReplaceFormat2, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%V", L"%%V", FALSE, FALSE);
jcarsey975136a2009-06-16 19:03:54 +00002769 ASSERT_EFI_ERROR(Status);
2770
2771 //
2772 // Use the last buffer from replacing to print from...
2773 //
jcarseya405b862010-09-14 05:18:09 +00002774 UnicodeVSPrint (mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), mPostReplaceFormat, Marker);
jcarseyb1f95a02009-06-16 00:23:19 +00002775
2776 if (Col != -1 && Row != -1) {
jcarseyb1f95a02009-06-16 00:23:19 +00002777 Status = gST->ConOut->SetCursorPosition(gST->ConOut, Col, Row);
jcarsey975136a2009-06-16 19:03:54 +00002778 }
2779
jcarseyecd3d592009-12-07 18:05:00 +00002780 FormatWalker = mPostReplaceFormat2;
jcarsey2247dde2009-11-09 18:08:58 +00002781 while (*FormatWalker != CHAR_NULL) {
jcarsey975136a2009-06-16 19:03:54 +00002782 //
2783 // Find the next attribute change request
2784 //
2785 ResumeLocation = StrStr(FormatWalker, L"%");
2786 if (ResumeLocation != NULL) {
jcarsey2247dde2009-11-09 18:08:58 +00002787 *ResumeLocation = CHAR_NULL;
jcarsey975136a2009-06-16 19:03:54 +00002788 }
2789 //
2790 // print the current FormatWalker string
2791 //
jcarseya405b862010-09-14 05:18:09 +00002792 if (StrLen(FormatWalker)>0) {
2793 Status = InternalPrintTo(FormatWalker);
2794 if (EFI_ERROR(Status)) {
2795 break;
2796 }
2797 }
2798
jcarsey975136a2009-06-16 19:03:54 +00002799 //
2800 // update the attribute
2801 //
2802 if (ResumeLocation != NULL) {
jcarsey5d46f172011-08-23 15:34:23 +00002803 if (*(ResumeLocation-1) == L'^') {
2804 //
jcarsey8bb74412012-01-30 18:44:41 +00002805 // Move cursor back 1 position to overwrite the ^
2806 //
2807 gST->ConOut->SetCursorPosition(gST->ConOut, gST->ConOut->Mode->CursorColumn - 1, gST->ConOut->Mode->CursorRow);
2808
2809 //
jcarsey5d46f172011-08-23 15:34:23 +00002810 // Print a simple '%' symbol
2811 //
2812 Status = InternalPrintTo(L"%");
2813 ResumeLocation = ResumeLocation - 1;
2814 } else {
2815 switch (*(ResumeLocation+1)) {
2816 case (L'N'):
2817 gST->ConOut->SetAttribute(gST->ConOut, OriginalAttribute);
jcarseya405b862010-09-14 05:18:09 +00002818 break;
jcarsey5d46f172011-08-23 15:34:23 +00002819 case (L'E'):
2820 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_YELLOW, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));
2821 break;
2822 case (L'H'):
2823 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_WHITE, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));
2824 break;
2825 case (L'B'):
2826 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_BLUE, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));
2827 break;
2828 case (L'V'):
2829 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_GREEN, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));
2830 break;
2831 default:
2832 //
2833 // Print a simple '%' symbol
2834 //
2835 Status = InternalPrintTo(L"%");
2836 if (EFI_ERROR(Status)) {
2837 break;
2838 }
2839 ResumeLocation = ResumeLocation - 1;
2840 break;
2841 }
jcarsey975136a2009-06-16 19:03:54 +00002842 }
2843 } else {
2844 //
2845 // reset to normal now...
2846 //
jcarsey975136a2009-06-16 19:03:54 +00002847 break;
2848 }
2849
2850 //
2851 // update FormatWalker to Resume + 2 (skip the % and the indicator)
2852 //
2853 FormatWalker = ResumeLocation + 2;
2854 }
jcarseyb1f95a02009-06-16 00:23:19 +00002855
jcarseya405b862010-09-14 05:18:09 +00002856 gST->ConOut->SetAttribute(gST->ConOut, OriginalAttribute);
jcarsey89e85372011-04-13 23:37:50 +00002857
2858 SHELL_FREE_NON_NULL(mPostReplaceFormat);
2859 SHELL_FREE_NON_NULL(mPostReplaceFormat2);
jcarseya405b862010-09-14 05:18:09 +00002860 return (Status);
jcarsey5f7431d2009-07-10 18:06:01 +00002861}
jcarsey2247dde2009-11-09 18:08:58 +00002862
2863/**
2864 Print at a specific location on the screen.
2865
jcarseye2f82972009-12-01 05:40:24 +00002866 This function will move the cursor to a given screen location and print the specified string.
jcarsey1e6e84c2010-01-25 20:05:08 +00002867
2868 If -1 is specified for either the Row or Col the current screen location for BOTH
jcarsey2247dde2009-11-09 18:08:58 +00002869 will be used.
2870
jcarseye2f82972009-12-01 05:40:24 +00002871 If either Row or Col is out of range for the current console, then ASSERT.
2872 If Format is NULL, then ASSERT.
jcarsey2247dde2009-11-09 18:08:58 +00002873
jcarsey1e6e84c2010-01-25 20:05:08 +00002874 In addition to the standard %-based flags as supported by UefiLib Print() this supports
jcarsey2247dde2009-11-09 18:08:58 +00002875 the following additional flags:
2876 %N - Set output attribute to normal
2877 %H - Set output attribute to highlight
2878 %E - Set output attribute to error
2879 %B - Set output attribute to blue color
2880 %V - Set output attribute to green color
2881
2882 Note: The background color is controlled by the shell command cls.
2883
jcarsey2247dde2009-11-09 18:08:58 +00002884 @param[in] Col the column to print at
jcarseya405b862010-09-14 05:18:09 +00002885 @param[in] Row the row to print at
jcarsey2247dde2009-11-09 18:08:58 +00002886 @param[in] Format the format string
jcarseya405b862010-09-14 05:18:09 +00002887 @param[in] ... The variable argument list.
jcarsey2247dde2009-11-09 18:08:58 +00002888
jcarseya405b862010-09-14 05:18:09 +00002889 @return EFI_SUCCESS The printing was successful.
2890 @return EFI_DEVICE_ERROR The console device reported an error.
jcarsey2247dde2009-11-09 18:08:58 +00002891**/
jcarseya405b862010-09-14 05:18:09 +00002892EFI_STATUS
jcarsey2247dde2009-11-09 18:08:58 +00002893EFIAPI
2894ShellPrintEx(
2895 IN INT32 Col OPTIONAL,
2896 IN INT32 Row OPTIONAL,
2897 IN CONST CHAR16 *Format,
2898 ...
jcarsey1e6e84c2010-01-25 20:05:08 +00002899 )
jcarsey2247dde2009-11-09 18:08:58 +00002900{
2901 VA_LIST Marker;
jcarseya405b862010-09-14 05:18:09 +00002902 EFI_STATUS RetVal;
jcarsey3e082d52010-10-04 16:44:57 +00002903 if (Format == NULL) {
2904 return (EFI_INVALID_PARAMETER);
2905 }
jcarsey2247dde2009-11-09 18:08:58 +00002906 VA_START (Marker, Format);
jcarseya405b862010-09-14 05:18:09 +00002907 RetVal = InternalShellPrintWorker(Col, Row, Format, Marker);
jcarseye2f82972009-12-01 05:40:24 +00002908 VA_END(Marker);
jcarseya405b862010-09-14 05:18:09 +00002909 return(RetVal);
jcarsey2247dde2009-11-09 18:08:58 +00002910}
2911
2912/**
2913 Print at a specific location on the screen.
2914
jcarseye2f82972009-12-01 05:40:24 +00002915 This function will move the cursor to a given screen location and print the specified string.
jcarsey1e6e84c2010-01-25 20:05:08 +00002916
2917 If -1 is specified for either the Row or Col the current screen location for BOTH
jcarseye2f82972009-12-01 05:40:24 +00002918 will be used.
jcarsey2247dde2009-11-09 18:08:58 +00002919
jcarseye2f82972009-12-01 05:40:24 +00002920 If either Row or Col is out of range for the current console, then ASSERT.
2921 If Format is NULL, then ASSERT.
jcarsey2247dde2009-11-09 18:08:58 +00002922
jcarsey1e6e84c2010-01-25 20:05:08 +00002923 In addition to the standard %-based flags as supported by UefiLib Print() this supports
jcarsey2247dde2009-11-09 18:08:58 +00002924 the following additional flags:
jcarsey1e6e84c2010-01-25 20:05:08 +00002925 %N - Set output attribute to normal.
2926 %H - Set output attribute to highlight.
2927 %E - Set output attribute to error.
2928 %B - Set output attribute to blue color.
2929 %V - Set output attribute to green color.
jcarsey2247dde2009-11-09 18:08:58 +00002930
2931 Note: The background color is controlled by the shell command cls.
2932
jcarsey1e6e84c2010-01-25 20:05:08 +00002933 @param[in] Col The column to print at.
jcarseya405b862010-09-14 05:18:09 +00002934 @param[in] Row The row to print at.
jcarsey1e6e84c2010-01-25 20:05:08 +00002935 @param[in] Language The language of the string to retrieve. If this parameter
2936 is NULL, then the current platform language is used.
2937 @param[in] HiiFormatStringId The format string Id for getting from Hii.
2938 @param[in] HiiFormatHandle The format string Handle for getting from Hii.
jcarseya405b862010-09-14 05:18:09 +00002939 @param[in] ... The variable argument list.
jcarsey2247dde2009-11-09 18:08:58 +00002940
jcarseya405b862010-09-14 05:18:09 +00002941 @return EFI_SUCCESS The printing was successful.
2942 @return EFI_DEVICE_ERROR The console device reported an error.
jcarsey2247dde2009-11-09 18:08:58 +00002943**/
jcarseya405b862010-09-14 05:18:09 +00002944EFI_STATUS
jcarsey2247dde2009-11-09 18:08:58 +00002945EFIAPI
2946ShellPrintHiiEx(
2947 IN INT32 Col OPTIONAL,
2948 IN INT32 Row OPTIONAL,
jcarsey1e6e84c2010-01-25 20:05:08 +00002949 IN CONST CHAR8 *Language OPTIONAL,
jcarsey2247dde2009-11-09 18:08:58 +00002950 IN CONST EFI_STRING_ID HiiFormatStringId,
2951 IN CONST EFI_HANDLE HiiFormatHandle,
2952 ...
2953 )
2954{
2955 VA_LIST Marker;
2956 CHAR16 *HiiFormatString;
jcarseya405b862010-09-14 05:18:09 +00002957 EFI_STATUS RetVal;
jcarsey2247dde2009-11-09 18:08:58 +00002958
2959 VA_START (Marker, HiiFormatHandle);
jcarsey1e6e84c2010-01-25 20:05:08 +00002960 HiiFormatString = HiiGetString(HiiFormatHandle, HiiFormatStringId, Language);
jcarsey2247dde2009-11-09 18:08:58 +00002961 ASSERT(HiiFormatString != NULL);
2962
2963 RetVal = InternalShellPrintWorker(Col, Row, HiiFormatString, Marker);
2964
jcarseya405b862010-09-14 05:18:09 +00002965 SHELL_FREE_NON_NULL(HiiFormatString);
jcarseye2f82972009-12-01 05:40:24 +00002966 VA_END(Marker);
jcarsey2247dde2009-11-09 18:08:58 +00002967
2968 return (RetVal);
2969}
2970
2971/**
2972 Function to determine if a given filename represents a file or a directory.
2973
2974 @param[in] DirName Path to directory to test.
2975
jcarseyc8c22592011-10-17 17:49:21 +00002976 @retval EFI_SUCCESS The Path represents a directory
2977 @retval EFI_NOT_FOUND The Path does not represent a directory
2978 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
2979 @return The path failed to open
jcarsey2247dde2009-11-09 18:08:58 +00002980**/
2981EFI_STATUS
2982EFIAPI
2983ShellIsDirectory(
2984 IN CONST CHAR16 *DirName
2985 )
2986{
2987 EFI_STATUS Status;
jcarseya405b862010-09-14 05:18:09 +00002988 SHELL_FILE_HANDLE Handle;
jcarsey3e082d52010-10-04 16:44:57 +00002989 CHAR16 *TempLocation;
2990 CHAR16 *TempLocation2;
jcarsey2247dde2009-11-09 18:08:58 +00002991
jcarseyecd3d592009-12-07 18:05:00 +00002992 ASSERT(DirName != NULL);
2993
jcarseya405b862010-09-14 05:18:09 +00002994 Handle = NULL;
2995 TempLocation = NULL;
jcarsey2247dde2009-11-09 18:08:58 +00002996
2997 Status = ShellOpenFileByName(DirName, &Handle, EFI_FILE_MODE_READ, 0);
2998 if (EFI_ERROR(Status)) {
jcarseya405b862010-09-14 05:18:09 +00002999 //
3000 // try good logic first.
3001 //
jcarsey366f81a2011-06-27 21:04:22 +00003002 if (gEfiShellProtocol != NULL) {
jcarsey3e082d52010-10-04 16:44:57 +00003003 TempLocation = StrnCatGrow(&TempLocation, NULL, DirName, 0);
jcarseyc8c22592011-10-17 17:49:21 +00003004 if (TempLocation == NULL) {
3005 ShellCloseFile(&Handle);
3006 return (EFI_OUT_OF_RESOURCES);
3007 }
jcarsey3e082d52010-10-04 16:44:57 +00003008 TempLocation2 = StrStr(TempLocation, L":");
3009 if (TempLocation2 != NULL && StrLen(StrStr(TempLocation, L":")) == 2) {
3010 *(TempLocation2+1) = CHAR_NULL;
jcarseya405b862010-09-14 05:18:09 +00003011 }
jcarsey366f81a2011-06-27 21:04:22 +00003012 if (gEfiShellProtocol->GetDevicePathFromMap(TempLocation) != NULL) {
jcarseya405b862010-09-14 05:18:09 +00003013 FreePool(TempLocation);
3014 return (EFI_SUCCESS);
3015 }
3016 FreePool(TempLocation);
3017 } else {
3018 //
3019 // probably a map name?!?!!?
3020 //
3021 TempLocation = StrStr(DirName, L"\\");
3022 if (TempLocation != NULL && *(TempLocation+1) == CHAR_NULL) {
3023 return (EFI_SUCCESS);
3024 }
3025 }
jcarsey2247dde2009-11-09 18:08:58 +00003026 return (Status);
3027 }
3028
3029 if (FileHandleIsDirectory(Handle) == EFI_SUCCESS) {
3030 ShellCloseFile(&Handle);
3031 return (EFI_SUCCESS);
3032 }
3033 ShellCloseFile(&Handle);
3034 return (EFI_NOT_FOUND);
3035}
3036
jcarsey125c2cf2009-11-18 21:36:50 +00003037/**
jcarsey36a9d672009-11-20 21:13:41 +00003038 Function to determine if a given filename represents a file.
3039
3040 @param[in] Name Path to file to test.
3041
3042 @retval EFI_SUCCESS The Path represents a file.
3043 @retval EFI_NOT_FOUND The Path does not represent a file.
3044 @retval other The path failed to open.
3045**/
3046EFI_STATUS
3047EFIAPI
3048ShellIsFile(
3049 IN CONST CHAR16 *Name
3050 )
3051{
3052 EFI_STATUS Status;
jcarseya405b862010-09-14 05:18:09 +00003053 SHELL_FILE_HANDLE Handle;
jcarsey36a9d672009-11-20 21:13:41 +00003054
jcarseyecd3d592009-12-07 18:05:00 +00003055 ASSERT(Name != NULL);
3056
jcarsey36a9d672009-11-20 21:13:41 +00003057 Handle = NULL;
3058
3059 Status = ShellOpenFileByName(Name, &Handle, EFI_FILE_MODE_READ, 0);
3060 if (EFI_ERROR(Status)) {
3061 return (Status);
3062 }
3063
3064 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {
3065 ShellCloseFile(&Handle);
3066 return (EFI_SUCCESS);
3067 }
3068 ShellCloseFile(&Handle);
3069 return (EFI_NOT_FOUND);
3070}
3071
3072/**
jcarseyb3011f42010-01-11 21:49:04 +00003073 Function to determine if a given filename represents a file.
3074
3075 This will search the CWD and then the Path.
3076
3077 If Name is NULL, then ASSERT.
3078
3079 @param[in] Name Path to file to test.
3080
3081 @retval EFI_SUCCESS The Path represents a file.
3082 @retval EFI_NOT_FOUND The Path does not represent a file.
3083 @retval other The path failed to open.
3084**/
3085EFI_STATUS
3086EFIAPI
3087ShellIsFileInPath(
3088 IN CONST CHAR16 *Name
jcarseya405b862010-09-14 05:18:09 +00003089 )
3090{
jcarseyb3011f42010-01-11 21:49:04 +00003091 CHAR16 *NewName;
3092 EFI_STATUS Status;
3093
3094 if (!EFI_ERROR(ShellIsFile(Name))) {
jcarseya405b862010-09-14 05:18:09 +00003095 return (EFI_SUCCESS);
jcarseyb3011f42010-01-11 21:49:04 +00003096 }
3097
3098 NewName = ShellFindFilePath(Name);
3099 if (NewName == NULL) {
3100 return (EFI_NOT_FOUND);
3101 }
3102 Status = ShellIsFile(NewName);
3103 FreePool(NewName);
3104 return (Status);
3105}
jcarsey252d9452011-03-25 20:49:53 +00003106
jcarseyb3011f42010-01-11 21:49:04 +00003107/**
Jaben Carsey74b0fb82013-11-22 21:37:34 +00003108 Function return the number converted from a hex representation of a number.
3109
3110 Note: this function cannot be used when (UINTN)(-1), (0xFFFFFFFF) may be a valid
3111 result. Use ShellConvertStringToUint64 instead.
3112
3113 @param[in] String String representation of a number.
3114
3115 @return The unsigned integer result of the conversion.
3116 @retval (UINTN)(-1) An error occured.
3117**/
3118UINTN
3119EFIAPI
3120ShellHexStrToUintn(
3121 IN CONST CHAR16 *String
3122 )
3123{
3124 UINT64 RetVal;
3125
3126 if (!EFI_ERROR(ShellConvertStringToUint64(String, &RetVal, TRUE, TRUE))) {
3127 return ((UINTN)RetVal);
3128 }
3129
3130 return ((UINTN)(-1));
3131}
3132
3133/**
jcarsey1e6e84c2010-01-25 20:05:08 +00003134 Function to determine whether a string is decimal or hex representation of a number
Jaben Carseyd59fc242013-11-14 23:52:43 +00003135 and return the number converted from the string. Spaces are always skipped.
jcarsey125c2cf2009-11-18 21:36:50 +00003136
3137 @param[in] String String representation of a number
3138
jcarsey252d9452011-03-25 20:49:53 +00003139 @return the number
3140 @retval (UINTN)(-1) An error ocurred.
jcarsey125c2cf2009-11-18 21:36:50 +00003141**/
3142UINTN
3143EFIAPI
3144ShellStrToUintn(
3145 IN CONST CHAR16 *String
3146 )
3147{
jcarsey252d9452011-03-25 20:49:53 +00003148 UINT64 RetVal;
3149 BOOLEAN Hex;
3150
3151 Hex = FALSE;
3152
jcarsey658bf432014-11-04 22:33:16 +00003153 if (!InternalShellIsHexOrDecimalNumber(String, Hex, TRUE, FALSE)) {
jcarsey252d9452011-03-25 20:49:53 +00003154 Hex = TRUE;
jcarsey125c2cf2009-11-18 21:36:50 +00003155 }
jcarsey252d9452011-03-25 20:49:53 +00003156
3157 if (!EFI_ERROR(ShellConvertStringToUint64(String, &RetVal, Hex, TRUE))) {
3158 return ((UINTN)RetVal);
3159 }
3160 return ((UINTN)(-1));
jcarsey125c2cf2009-11-18 21:36:50 +00003161}
3162
3163/**
3164 Safely append with automatic string resizing given length of Destination and
3165 desired length of copy from Source.
3166
3167 append the first D characters of Source to the end of Destination, where D is
3168 the lesser of Count and the StrLen() of Source. If appending those D characters
3169 will fit within Destination (whose Size is given as CurrentSize) and
jcarsey1e6e84c2010-01-25 20:05:08 +00003170 still leave room for a NULL terminator, then those characters are appended,
3171 starting at the original terminating NULL of Destination, and a new terminating
3172 NULL is appended.
jcarsey125c2cf2009-11-18 21:36:50 +00003173
3174 If appending D characters onto Destination will result in a overflow of the size
3175 given in CurrentSize the string will be grown such that the copy can be performed
3176 and CurrentSize will be updated to the new size.
3177
3178 If Source is NULL, there is nothing to append, just return the current buffer in
3179 Destination.
3180
3181 if Destination is NULL, then ASSERT()
3182 if Destination's current length (including NULL terminator) is already more then
3183 CurrentSize, then ASSERT()
3184
ydong104ff7e372011-09-02 08:05:34 +00003185 @param[in, out] Destination The String to append onto
3186 @param[in, out] CurrentSize on call the number of bytes in Destination. On
jcarsey125c2cf2009-11-18 21:36:50 +00003187 return possibly the new size (still in bytes). if NULL
3188 then allocate whatever is needed.
3189 @param[in] Source The String to append from
3190 @param[in] Count Maximum number of characters to append. if 0 then
3191 all are appended.
3192
3193 @return Destination return the resultant string.
3194**/
3195CHAR16*
3196EFIAPI
3197StrnCatGrow (
3198 IN OUT CHAR16 **Destination,
3199 IN OUT UINTN *CurrentSize,
3200 IN CONST CHAR16 *Source,
3201 IN UINTN Count
3202 )
3203{
3204 UINTN DestinationStartSize;
3205 UINTN NewSize;
3206
3207 //
3208 // ASSERTs
3209 //
3210 ASSERT(Destination != NULL);
3211
3212 //
3213 // If there's nothing to do then just return Destination
3214 //
3215 if (Source == NULL) {
3216 return (*Destination);
3217 }
3218
3219 //
3220 // allow for un-initialized pointers, based on size being 0
3221 //
3222 if (CurrentSize != NULL && *CurrentSize == 0) {
3223 *Destination = NULL;
3224 }
3225
3226 //
3227 // allow for NULL pointers address as Destination
3228 //
3229 if (*Destination != NULL) {
3230 ASSERT(CurrentSize != 0);
3231 DestinationStartSize = StrSize(*Destination);
3232 ASSERT(DestinationStartSize <= *CurrentSize);
3233 } else {
3234 DestinationStartSize = 0;
3235// ASSERT(*CurrentSize == 0);
3236 }
3237
3238 //
3239 // Append all of Source?
3240 //
3241 if (Count == 0) {
3242 Count = StrLen(Source);
3243 }
3244
3245 //
3246 // Test and grow if required
3247 //
3248 if (CurrentSize != NULL) {
3249 NewSize = *CurrentSize;
ydong10f480fdc2012-09-07 01:55:33 +00003250 if (NewSize < DestinationStartSize + (Count * sizeof(CHAR16))) {
3251 while (NewSize < (DestinationStartSize + (Count*sizeof(CHAR16)))) {
3252 NewSize += 2 * Count * sizeof(CHAR16);
3253 }
3254 *Destination = ReallocatePool(*CurrentSize, NewSize, *Destination);
3255 *CurrentSize = NewSize;
jcarsey125c2cf2009-11-18 21:36:50 +00003256 }
jcarsey125c2cf2009-11-18 21:36:50 +00003257 } else {
Qiu Shuminc587fd32015-07-02 01:12:03 +00003258 NewSize = (Count+1)*sizeof(CHAR16);
3259 *Destination = AllocateZeroPool(NewSize);
jcarsey125c2cf2009-11-18 21:36:50 +00003260 }
3261
3262 //
3263 // Now use standard StrnCat on a big enough buffer
3264 //
jcarseyc9d92df2010-02-03 15:37:54 +00003265 if (*Destination == NULL) {
3266 return (NULL);
3267 }
Qiu Shumine75390f2015-06-30 03:18:31 +00003268
Qiu Shuminc587fd32015-07-02 01:12:03 +00003269 StrnCatS(*Destination, NewSize/sizeof(CHAR16), Source, Count);
Qiu Shumine75390f2015-06-30 03:18:31 +00003270 return *Destination;
jcarsey125c2cf2009-11-18 21:36:50 +00003271}
jcarseyc9d92df2010-02-03 15:37:54 +00003272
3273/**
3274 Prompt the user and return the resultant answer to the requestor.
3275
3276 This function will display the requested question on the shell prompt and then
3277 wait for an apropriate answer to be input from the console.
3278
jcarseya405b862010-09-14 05:18:09 +00003279 if the SHELL_PROMPT_REQUEST_TYPE is SHELL_PROMPT_REQUEST_TYPE_YESNO, ShellPromptResponseTypeQuitContinue
jcarseyc9d92df2010-02-03 15:37:54 +00003280 or SHELL_PROMPT_REQUEST_TYPE_YESNOCANCEL then *Response is of type SHELL_PROMPT_RESPONSE.
3281
jcarseya405b862010-09-14 05:18:09 +00003282 if the SHELL_PROMPT_REQUEST_TYPE is ShellPromptResponseTypeFreeform then *Response is of type
jcarseyc9d92df2010-02-03 15:37:54 +00003283 CHAR16*.
3284
3285 In either case *Response must be callee freed if Response was not NULL;
3286
3287 @param Type What type of question is asked. This is used to filter the input
3288 to prevent invalid answers to question.
3289 @param Prompt Pointer to string prompt to use to request input.
3290 @param Response Pointer to Response which will be populated upon return.
3291
3292 @retval EFI_SUCCESS The operation was sucessful.
3293 @retval EFI_UNSUPPORTED The operation is not supported as requested.
3294 @retval EFI_INVALID_PARAMETER A parameter was invalid.
3295 @return other The operation failed.
3296**/
3297EFI_STATUS
3298EFIAPI
3299ShellPromptForResponse (
3300 IN SHELL_PROMPT_REQUEST_TYPE Type,
3301 IN CHAR16 *Prompt OPTIONAL,
3302 IN OUT VOID **Response OPTIONAL
3303 )
3304{
3305 EFI_STATUS Status;
3306 EFI_INPUT_KEY Key;
3307 UINTN EventIndex;
3308 SHELL_PROMPT_RESPONSE *Resp;
jcarseya405b862010-09-14 05:18:09 +00003309 UINTN Size;
3310 CHAR16 *Buffer;
jcarseyc9d92df2010-02-03 15:37:54 +00003311
jcarseya405b862010-09-14 05:18:09 +00003312 Status = EFI_UNSUPPORTED;
3313 Resp = NULL;
3314 Buffer = NULL;
3315 Size = 0;
3316 if (Type != ShellPromptResponseTypeFreeform) {
jcarsey252d9452011-03-25 20:49:53 +00003317 Resp = (SHELL_PROMPT_RESPONSE*)AllocateZeroPool(sizeof(SHELL_PROMPT_RESPONSE));
jcarsey3e082d52010-10-04 16:44:57 +00003318 if (Resp == NULL) {
3319 return (EFI_OUT_OF_RESOURCES);
3320 }
xdu290bfa222010-07-19 05:21:27 +00003321 }
jcarseyc9d92df2010-02-03 15:37:54 +00003322
3323 switch(Type) {
jcarseya405b862010-09-14 05:18:09 +00003324 case ShellPromptResponseTypeQuitContinue:
jcarseyc9d92df2010-02-03 15:37:54 +00003325 if (Prompt != NULL) {
3326 ShellPrintEx(-1, -1, L"%s", Prompt);
3327 }
3328 //
3329 // wait for valid response
3330 //
3331 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
3332 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
Jaben Carsey31b018a2014-01-16 16:52:39 +00003333 if (EFI_ERROR(Status)) {
3334 break;
3335 }
jcarseyc9d92df2010-02-03 15:37:54 +00003336 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3337 if (Key.UnicodeChar == L'Q' || Key.UnicodeChar ==L'q') {
jcarseya405b862010-09-14 05:18:09 +00003338 *Resp = ShellPromptResponseQuit;
jcarseyc9d92df2010-02-03 15:37:54 +00003339 } else {
jcarseya405b862010-09-14 05:18:09 +00003340 *Resp = ShellPromptResponseContinue;
jcarseyc9d92df2010-02-03 15:37:54 +00003341 }
3342 break;
jcarseya405b862010-09-14 05:18:09 +00003343 case ShellPromptResponseTypeYesNoCancel:
jcarseyc9d92df2010-02-03 15:37:54 +00003344 if (Prompt != NULL) {
3345 ShellPrintEx(-1, -1, L"%s", Prompt);
3346 }
3347 //
3348 // wait for valid response
3349 //
jcarseya405b862010-09-14 05:18:09 +00003350 *Resp = ShellPromptResponseMax;
3351 while (*Resp == ShellPromptResponseMax) {
Jaben Carsey194ae482013-12-09 22:55:13 +00003352 if (ShellGetExecutionBreakFlag()) {
3353 Status = EFI_ABORTED;
3354 break;
3355 }
jcarseyc9d92df2010-02-03 15:37:54 +00003356 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
3357 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
Jaben Carsey31b018a2014-01-16 16:52:39 +00003358 if (EFI_ERROR(Status)) {
3359 break;
3360 }
jcarseyc9d92df2010-02-03 15:37:54 +00003361 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3362 switch (Key.UnicodeChar) {
3363 case L'Y':
3364 case L'y':
jcarseya405b862010-09-14 05:18:09 +00003365 *Resp = ShellPromptResponseYes;
jcarseyc9d92df2010-02-03 15:37:54 +00003366 break;
3367 case L'N':
3368 case L'n':
jcarseya405b862010-09-14 05:18:09 +00003369 *Resp = ShellPromptResponseNo;
jcarseyc9d92df2010-02-03 15:37:54 +00003370 break;
3371 case L'C':
3372 case L'c':
jcarseya405b862010-09-14 05:18:09 +00003373 *Resp = ShellPromptResponseCancel;
3374 break;
3375 }
3376 }
3377 break; case ShellPromptResponseTypeYesNoAllCancel:
3378 if (Prompt != NULL) {
3379 ShellPrintEx(-1, -1, L"%s", Prompt);
3380 }
3381 //
3382 // wait for valid response
3383 //
3384 *Resp = ShellPromptResponseMax;
3385 while (*Resp == ShellPromptResponseMax) {
Jaben Carsey194ae482013-12-09 22:55:13 +00003386 if (ShellGetExecutionBreakFlag()) {
3387 Status = EFI_ABORTED;
3388 break;
3389 }
jcarseya405b862010-09-14 05:18:09 +00003390 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
3391 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
Jaben Carsey31b018a2014-01-16 16:52:39 +00003392 if (EFI_ERROR(Status)) {
3393 break;
3394 }
jcarseya405b862010-09-14 05:18:09 +00003395 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3396 switch (Key.UnicodeChar) {
3397 case L'Y':
3398 case L'y':
3399 *Resp = ShellPromptResponseYes;
3400 break;
3401 case L'N':
3402 case L'n':
3403 *Resp = ShellPromptResponseNo;
3404 break;
3405 case L'A':
3406 case L'a':
3407 *Resp = ShellPromptResponseAll;
3408 break;
3409 case L'C':
3410 case L'c':
3411 *Resp = ShellPromptResponseCancel;
jcarseyc9d92df2010-02-03 15:37:54 +00003412 break;
3413 }
3414 }
3415 break;
jcarseya405b862010-09-14 05:18:09 +00003416 case ShellPromptResponseTypeEnterContinue:
3417 case ShellPromptResponseTypeAnyKeyContinue:
jcarseyc9d92df2010-02-03 15:37:54 +00003418 if (Prompt != NULL) {
3419 ShellPrintEx(-1, -1, L"%s", Prompt);
3420 }
3421 //
3422 // wait for valid response
3423 //
jcarseya405b862010-09-14 05:18:09 +00003424 *Resp = ShellPromptResponseMax;
3425 while (*Resp == ShellPromptResponseMax) {
Jaben Carsey194ae482013-12-09 22:55:13 +00003426 if (ShellGetExecutionBreakFlag()) {
3427 Status = EFI_ABORTED;
3428 break;
3429 }
jcarseyc9d92df2010-02-03 15:37:54 +00003430 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
jcarseya405b862010-09-14 05:18:09 +00003431 if (Type == ShellPromptResponseTypeEnterContinue) {
jcarseyc9d92df2010-02-03 15:37:54 +00003432 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
Jaben Carsey31b018a2014-01-16 16:52:39 +00003433 if (EFI_ERROR(Status)) {
3434 break;
3435 }
jcarseyc9d92df2010-02-03 15:37:54 +00003436 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3437 if (Key.UnicodeChar == CHAR_CARRIAGE_RETURN) {
jcarseya405b862010-09-14 05:18:09 +00003438 *Resp = ShellPromptResponseContinue;
jcarseyc9d92df2010-02-03 15:37:54 +00003439 break;
3440 }
3441 }
jcarseya405b862010-09-14 05:18:09 +00003442 if (Type == ShellPromptResponseTypeAnyKeyContinue) {
3443 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
3444 ASSERT_EFI_ERROR(Status);
3445 *Resp = ShellPromptResponseContinue;
jcarseyc9d92df2010-02-03 15:37:54 +00003446 break;
3447 }
3448 }
3449 break;
jcarseya405b862010-09-14 05:18:09 +00003450 case ShellPromptResponseTypeYesNo:
3451 if (Prompt != NULL) {
3452 ShellPrintEx(-1, -1, L"%s", Prompt);
3453 }
3454 //
3455 // wait for valid response
3456 //
3457 *Resp = ShellPromptResponseMax;
3458 while (*Resp == ShellPromptResponseMax) {
Jaben Carsey194ae482013-12-09 22:55:13 +00003459 if (ShellGetExecutionBreakFlag()) {
3460 Status = EFI_ABORTED;
3461 break;
3462 }
jcarseya405b862010-09-14 05:18:09 +00003463 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
3464 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
Jaben Carsey31b018a2014-01-16 16:52:39 +00003465 if (EFI_ERROR(Status)) {
3466 break;
3467 }
jcarseya405b862010-09-14 05:18:09 +00003468 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3469 switch (Key.UnicodeChar) {
3470 case L'Y':
3471 case L'y':
3472 *Resp = ShellPromptResponseYes;
3473 break;
3474 case L'N':
3475 case L'n':
3476 *Resp = ShellPromptResponseNo;
3477 break;
3478 }
3479 }
3480 break;
3481 case ShellPromptResponseTypeFreeform:
3482 if (Prompt != NULL) {
3483 ShellPrintEx(-1, -1, L"%s", Prompt);
3484 }
3485 while(1) {
Jaben Carsey194ae482013-12-09 22:55:13 +00003486 if (ShellGetExecutionBreakFlag()) {
3487 Status = EFI_ABORTED;
3488 break;
3489 }
jcarseya405b862010-09-14 05:18:09 +00003490 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
3491 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
Jaben Carsey31b018a2014-01-16 16:52:39 +00003492 if (EFI_ERROR(Status)) {
3493 break;
3494 }
jcarseya405b862010-09-14 05:18:09 +00003495 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3496 if (Key.UnicodeChar == CHAR_CARRIAGE_RETURN) {
3497 break;
3498 }
3499 ASSERT((Buffer == NULL && Size == 0) || (Buffer != NULL));
3500 StrnCatGrow(&Buffer, &Size, &Key.UnicodeChar, 1);
3501 }
3502 break;
3503 //
3504 // This is the location to add new prompt types.
Jaben Carsey194ae482013-12-09 22:55:13 +00003505 // If your new type loops remember to add ExecutionBreak support.
jcarseya405b862010-09-14 05:18:09 +00003506 //
jcarseyc9d92df2010-02-03 15:37:54 +00003507 default:
jcarseya405b862010-09-14 05:18:09 +00003508 ASSERT(FALSE);
jcarseyc9d92df2010-02-03 15:37:54 +00003509 }
3510
3511 if (Response != NULL) {
jcarseya405b862010-09-14 05:18:09 +00003512 if (Resp != NULL) {
3513 *Response = Resp;
3514 } else if (Buffer != NULL) {
3515 *Response = Buffer;
3516 }
jcarseyc9d92df2010-02-03 15:37:54 +00003517 } else {
jcarseya405b862010-09-14 05:18:09 +00003518 if (Resp != NULL) {
3519 FreePool(Resp);
3520 }
3521 if (Buffer != NULL) {
3522 FreePool(Buffer);
3523 }
jcarseyc9d92df2010-02-03 15:37:54 +00003524 }
3525
jcarseya405b862010-09-14 05:18:09 +00003526 ShellPrintEx(-1, -1, L"\r\n");
jcarseyc9d92df2010-02-03 15:37:54 +00003527 return (Status);
3528}
3529
3530/**
3531 Prompt the user and return the resultant answer to the requestor.
3532
3533 This function is the same as ShellPromptForResponse, except that the prompt is
3534 automatically pulled from HII.
3535
3536 @param Type What type of question is asked. This is used to filter the input
3537 to prevent invalid answers to question.
jcarseya405b862010-09-14 05:18:09 +00003538 @param[in] HiiFormatStringId The format string Id for getting from Hii.
3539 @param[in] HiiFormatHandle The format string Handle for getting from Hii.
3540 @param Response Pointer to Response which will be populated upon return.
jcarseyc9d92df2010-02-03 15:37:54 +00003541
3542 @retval EFI_SUCCESS the operation was sucessful.
3543 @return other the operation failed.
3544
3545 @sa ShellPromptForResponse
3546**/
3547EFI_STATUS
3548EFIAPI
3549ShellPromptForResponseHii (
3550 IN SHELL_PROMPT_REQUEST_TYPE Type,
3551 IN CONST EFI_STRING_ID HiiFormatStringId,
3552 IN CONST EFI_HANDLE HiiFormatHandle,
3553 IN OUT VOID **Response
3554 )
3555{
3556 CHAR16 *Prompt;
3557 EFI_STATUS Status;
3558
3559 Prompt = HiiGetString(HiiFormatHandle, HiiFormatStringId, NULL);
3560 Status = ShellPromptForResponse(Type, Prompt, Response);
3561 FreePool(Prompt);
3562 return (Status);
3563}
3564
jcarseya405b862010-09-14 05:18:09 +00003565/**
3566 Function to determin if an entire string is a valid number.
jcarseyc9d92df2010-02-03 15:37:54 +00003567
jcarseya405b862010-09-14 05:18:09 +00003568 If Hex it must be preceeded with a 0x or has ForceHex, set TRUE.
3569
3570 @param[in] String The string to evaluate.
3571 @param[in] ForceHex TRUE - always assume hex.
3572 @param[in] StopAtSpace TRUE to halt upon finding a space, FALSE to keep going.
jcarsey658bf432014-11-04 22:33:16 +00003573 @param[in] TimeNumbers TRUE to allow numbers with ":", FALSE otherwise.
jcarseya405b862010-09-14 05:18:09 +00003574
3575 @retval TRUE It is all numeric (dec/hex) characters.
3576 @retval FALSE There is a non-numeric character.
3577**/
3578BOOLEAN
3579EFIAPI
jcarsey252d9452011-03-25 20:49:53 +00003580InternalShellIsHexOrDecimalNumber (
jcarseya405b862010-09-14 05:18:09 +00003581 IN CONST CHAR16 *String,
3582 IN CONST BOOLEAN ForceHex,
jcarsey658bf432014-11-04 22:33:16 +00003583 IN CONST BOOLEAN StopAtSpace,
3584 IN CONST BOOLEAN TimeNumbers
jcarseya405b862010-09-14 05:18:09 +00003585 )
3586{
3587 BOOLEAN Hex;
3588
3589 //
3590 // chop off a single negative sign
3591 //
3592 if (String != NULL && *String == L'-') {
3593 String++;
3594 }
darylm503b0934ac2011-11-12 00:35:11 +00003595
jcarseya405b862010-09-14 05:18:09 +00003596 if (String == NULL) {
3597 return (FALSE);
3598 }
3599
3600 //
3601 // chop leading zeroes
3602 //
3603 while(String != NULL && *String == L'0'){
3604 String++;
3605 }
3606 //
3607 // allow '0x' or '0X', but not 'x' or 'X'
3608 //
3609 if (String != NULL && (*String == L'x' || *String == L'X')) {
3610 if (*(String-1) != L'0') {
3611 //
3612 // we got an x without a preceeding 0
3613 //
3614 return (FALSE);
3615 }
3616 String++;
3617 Hex = TRUE;
3618 } else if (ForceHex) {
3619 Hex = TRUE;
3620 } else {
3621 Hex = FALSE;
3622 }
3623
3624 //
3625 // loop through the remaining characters and use the lib function
3626 //
3627 for ( ; String != NULL && *String != CHAR_NULL && !(StopAtSpace && *String == L' ') ; String++){
jcarsey658bf432014-11-04 22:33:16 +00003628 if (TimeNumbers && (String[0] == L':')) {
3629 continue;
3630 }
jcarseya405b862010-09-14 05:18:09 +00003631 if (Hex) {
3632 if (!ShellIsHexaDecimalDigitCharacter(*String)) {
3633 return (FALSE);
3634 }
3635 } else {
3636 if (!ShellIsDecimalDigitCharacter(*String)) {
3637 return (FALSE);
3638 }
3639 }
3640 }
jcarsey252d9452011-03-25 20:49:53 +00003641
jcarseya405b862010-09-14 05:18:09 +00003642 return (TRUE);
3643}
3644
3645/**
3646 Function to determine if a given filename exists.
3647
3648 @param[in] Name Path to test.
3649
3650 @retval EFI_SUCCESS The Path represents a file.
3651 @retval EFI_NOT_FOUND The Path does not represent a file.
3652 @retval other The path failed to open.
3653**/
3654EFI_STATUS
3655EFIAPI
3656ShellFileExists(
3657 IN CONST CHAR16 *Name
3658 )
3659{
3660 EFI_STATUS Status;
3661 EFI_SHELL_FILE_INFO *List;
3662
3663 ASSERT(Name != NULL);
3664
3665 List = NULL;
3666 Status = ShellOpenFileMetaArg((CHAR16*)Name, EFI_FILE_MODE_READ, &List);
3667 if (EFI_ERROR(Status)) {
3668 return (Status);
3669 }
3670
3671 ShellCloseFileMetaArg(&List);
3672
3673 return (EFI_SUCCESS);
3674}
jcarsey252d9452011-03-25 20:49:53 +00003675
3676/**
darylm503b0934ac2011-11-12 00:35:11 +00003677 Convert a Unicode character to upper case only if
jcarsey252d9452011-03-25 20:49:53 +00003678 it maps to a valid small-case ASCII character.
3679
3680 This internal function only deal with Unicode character
3681 which maps to a valid small-case ASCII character, i.e.
3682 L'a' to L'z'. For other Unicode character, the input character
3683 is returned directly.
3684
3685 @param Char The character to convert.
3686
3687 @retval LowerCharacter If the Char is with range L'a' to L'z'.
3688 @retval Unchanged Otherwise.
3689
3690**/
3691CHAR16
3692EFIAPI
3693InternalShellCharToUpper (
3694 IN CHAR16 Char
3695 )
3696{
3697 if (Char >= L'a' && Char <= L'z') {
3698 return (CHAR16) (Char - (L'a' - L'A'));
3699 }
3700
3701 return Char;
3702}
3703
3704/**
3705 Convert a Unicode character to numerical value.
3706
3707 This internal function only deal with Unicode character
3708 which maps to a valid hexadecimal ASII character, i.e.
darylm503b0934ac2011-11-12 00:35:11 +00003709 L'0' to L'9', L'a' to L'f' or L'A' to L'F'. For other
jcarsey252d9452011-03-25 20:49:53 +00003710 Unicode character, the value returned does not make sense.
3711
3712 @param Char The character to convert.
3713
3714 @return The numerical value converted.
3715
3716**/
3717UINTN
3718EFIAPI
3719InternalShellHexCharToUintn (
3720 IN CHAR16 Char
3721 )
3722{
3723 if (ShellIsDecimalDigitCharacter (Char)) {
3724 return Char - L'0';
3725 }
3726
3727 return (UINTN) (10 + InternalShellCharToUpper (Char) - L'A');
3728}
3729
3730/**
3731 Convert a Null-terminated Unicode hexadecimal string to a value of type UINT64.
3732
Jaben Carsey80f3e342015-07-02 17:26:42 +00003733 This function returns a value of type UINT64 by interpreting the contents
jcarsey252d9452011-03-25 20:49:53 +00003734 of the Unicode string specified by String as a hexadecimal number.
3735 The format of the input Unicode string String is:
3736
3737 [spaces][zeros][x][hexadecimal digits].
3738
3739 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
3740 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix.
3741 If "x" appears in the input string, it must be prefixed with at least one 0.
3742 The function will ignore the pad space, which includes spaces or tab characters,
3743 before [zeros], [x] or [hexadecimal digit]. The running zero before [x] or
3744 [hexadecimal digit] will be ignored. Then, the decoding starts after [x] or the
3745 first valid hexadecimal digit. Then, the function stops at the first character that is
3746 a not a valid hexadecimal character or NULL, whichever one comes first.
3747
3748 If String has only pad spaces, then zero is returned.
3749 If String has no leading pad spaces, leading zeros or valid hexadecimal digits,
3750 then zero is returned.
3751
3752 @param[in] String A pointer to a Null-terminated Unicode string.
3753 @param[out] Value Upon a successful return the value of the conversion.
3754 @param[in] StopAtSpace FALSE to skip spaces.
3755
3756 @retval EFI_SUCCESS The conversion was successful.
3757 @retval EFI_INVALID_PARAMETER A parameter was NULL or invalid.
3758 @retval EFI_DEVICE_ERROR An overflow occured.
3759**/
3760EFI_STATUS
3761EFIAPI
3762InternalShellStrHexToUint64 (
3763 IN CONST CHAR16 *String,
3764 OUT UINT64 *Value,
3765 IN CONST BOOLEAN StopAtSpace
3766 )
3767{
3768 UINT64 Result;
3769
3770 if (String == NULL || StrSize(String) == 0 || Value == NULL) {
3771 return (EFI_INVALID_PARAMETER);
3772 }
darylm503b0934ac2011-11-12 00:35:11 +00003773
jcarsey252d9452011-03-25 20:49:53 +00003774 //
darylm503b0934ac2011-11-12 00:35:11 +00003775 // Ignore the pad spaces (space or tab)
jcarsey252d9452011-03-25 20:49:53 +00003776 //
3777 while ((*String == L' ') || (*String == L'\t')) {
3778 String++;
3779 }
3780
3781 //
3782 // Ignore leading Zeros after the spaces
3783 //
3784 while (*String == L'0') {
3785 String++;
3786 }
3787
3788 if (InternalShellCharToUpper (*String) == L'X') {
3789 if (*(String - 1) != L'0') {
3790 return 0;
3791 }
3792 //
3793 // Skip the 'X'
3794 //
3795 String++;
3796 }
3797
3798 Result = 0;
darylm503b0934ac2011-11-12 00:35:11 +00003799
jcarsey252d9452011-03-25 20:49:53 +00003800 //
Jaben Carsey80f3e342015-07-02 17:26:42 +00003801 // there is a space where there should't be
jcarsey252d9452011-03-25 20:49:53 +00003802 //
Jaben Carsey80f3e342015-07-02 17:26:42 +00003803 if (*String == L' ') {
3804 return (EFI_INVALID_PARAMETER);
jcarsey252d9452011-03-25 20:49:53 +00003805 }
darylm503b0934ac2011-11-12 00:35:11 +00003806
jcarsey252d9452011-03-25 20:49:53 +00003807 while (ShellIsHexaDecimalDigitCharacter (*String)) {
3808 //
darylm503b0934ac2011-11-12 00:35:11 +00003809 // If the Hex Number represented by String overflows according
Jaben Carsey80f3e342015-07-02 17:26:42 +00003810 // to the range defined by UINT64, then return EFI_DEVICE_ERROR.
jcarsey252d9452011-03-25 20:49:53 +00003811 //
3812 if (!(Result <= (RShiftU64((((UINT64) ~0) - InternalShellHexCharToUintn (*String)), 4)))) {
3813// if (!(Result <= ((((UINT64) ~0) - InternalShellHexCharToUintn (*String)) >> 4))) {
3814 return (EFI_DEVICE_ERROR);
3815 }
3816
3817 Result = (LShiftU64(Result, 4));
3818 Result += InternalShellHexCharToUintn (*String);
3819 String++;
3820
3821 //
jcarsey49bd4982012-01-27 18:42:43 +00003822 // stop at spaces if requested
jcarsey252d9452011-03-25 20:49:53 +00003823 //
jcarsey49bd4982012-01-27 18:42:43 +00003824 if (StopAtSpace && *String == L' ') {
3825 break;
jcarsey252d9452011-03-25 20:49:53 +00003826 }
3827 }
3828
3829 *Value = Result;
3830 return (EFI_SUCCESS);
3831}
3832
3833/**
3834 Convert a Null-terminated Unicode decimal string to a value of
3835 type UINT64.
3836
3837 This function returns a value of type UINT64 by interpreting the contents
3838 of the Unicode string specified by String as a decimal number. The format
3839 of the input Unicode string String is:
3840
3841 [spaces] [decimal digits].
3842
3843 The valid decimal digit character is in the range [0-9]. The
3844 function will ignore the pad space, which includes spaces or
3845 tab characters, before [decimal digits]. The running zero in the
3846 beginning of [decimal digits] will be ignored. Then, the function
3847 stops at the first character that is a not a valid decimal character
3848 or a Null-terminator, whichever one comes first.
3849
3850 If String has only pad spaces, then 0 is returned.
3851 If String has no pad spaces or valid decimal digits,
3852 then 0 is returned.
3853
3854 @param[in] String A pointer to a Null-terminated Unicode string.
3855 @param[out] Value Upon a successful return the value of the conversion.
3856 @param[in] StopAtSpace FALSE to skip spaces.
3857
3858 @retval EFI_SUCCESS The conversion was successful.
3859 @retval EFI_INVALID_PARAMETER A parameter was NULL or invalid.
3860 @retval EFI_DEVICE_ERROR An overflow occured.
3861**/
3862EFI_STATUS
3863EFIAPI
3864InternalShellStrDecimalToUint64 (
3865 IN CONST CHAR16 *String,
3866 OUT UINT64 *Value,
3867 IN CONST BOOLEAN StopAtSpace
3868 )
3869{
3870 UINT64 Result;
3871
3872 if (String == NULL || StrSize (String) == 0 || Value == NULL) {
3873 return (EFI_INVALID_PARAMETER);
3874 }
3875
3876 //
3877 // Ignore the pad spaces (space or tab)
3878 //
3879 while ((*String == L' ') || (*String == L'\t')) {
3880 String++;
3881 }
3882
3883 //
3884 // Ignore leading Zeros after the spaces
3885 //
3886 while (*String == L'0') {
3887 String++;
3888 }
3889
3890 Result = 0;
3891
3892 //
Jaben Carsey80f3e342015-07-02 17:26:42 +00003893 // Stop upon space if requested
3894 // (if the whole value was 0)
jcarsey252d9452011-03-25 20:49:53 +00003895 //
Jaben Carsey80f3e342015-07-02 17:26:42 +00003896 if (StopAtSpace && *String == L' ') {
3897 *Value = Result;
3898 return (EFI_SUCCESS);
jcarsey252d9452011-03-25 20:49:53 +00003899 }
Jaben Carsey80f3e342015-07-02 17:26:42 +00003900
jcarsey252d9452011-03-25 20:49:53 +00003901 while (ShellIsDecimalDigitCharacter (*String)) {
3902 //
darylm503b0934ac2011-11-12 00:35:11 +00003903 // If the number represented by String overflows according
Jaben Carsey80f3e342015-07-02 17:26:42 +00003904 // to the range defined by UINT64, then return EFI_DEVICE_ERROR.
jcarsey252d9452011-03-25 20:49:53 +00003905 //
darylm503b0934ac2011-11-12 00:35:11 +00003906
jcarsey252d9452011-03-25 20:49:53 +00003907 if (!(Result <= (DivU64x32((((UINT64) ~0) - (*String - L'0')),10)))) {
3908 return (EFI_DEVICE_ERROR);
3909 }
3910
3911 Result = MultU64x32(Result, 10) + (*String - L'0');
3912 String++;
3913
3914 //
3915 // Stop at spaces if requested
3916 //
3917 if (StopAtSpace && *String == L' ') {
3918 break;
3919 }
3920 }
3921
3922 *Value = Result;
darylm503b0934ac2011-11-12 00:35:11 +00003923
jcarsey252d9452011-03-25 20:49:53 +00003924 return (EFI_SUCCESS);
3925}
3926
3927/**
3928 Function to verify and convert a string to its numerical value.
3929
3930 If Hex it must be preceeded with a 0x, 0X, or has ForceHex set TRUE.
3931
3932 @param[in] String The string to evaluate.
3933 @param[out] Value Upon a successful return the value of the conversion.
3934 @param[in] ForceHex TRUE - always assume hex.
3935 @param[in] StopAtSpace FALSE to skip spaces.
darylm503b0934ac2011-11-12 00:35:11 +00003936
jcarsey252d9452011-03-25 20:49:53 +00003937 @retval EFI_SUCCESS The conversion was successful.
3938 @retval EFI_INVALID_PARAMETER String contained an invalid character.
3939 @retval EFI_NOT_FOUND String was a number, but Value was NULL.
3940**/
3941EFI_STATUS
3942EFIAPI
3943ShellConvertStringToUint64(
3944 IN CONST CHAR16 *String,
3945 OUT UINT64 *Value,
3946 IN CONST BOOLEAN ForceHex,
3947 IN CONST BOOLEAN StopAtSpace
3948 )
3949{
3950 UINT64 RetVal;
3951 CONST CHAR16 *Walker;
3952 EFI_STATUS Status;
3953 BOOLEAN Hex;
3954
3955 Hex = ForceHex;
3956
jcarsey658bf432014-11-04 22:33:16 +00003957 if (!InternalShellIsHexOrDecimalNumber(String, Hex, StopAtSpace, FALSE)) {
jcarsey252d9452011-03-25 20:49:53 +00003958 if (!Hex) {
3959 Hex = TRUE;
jcarsey658bf432014-11-04 22:33:16 +00003960 if (!InternalShellIsHexOrDecimalNumber(String, Hex, StopAtSpace, FALSE)) {
jcarsey252d9452011-03-25 20:49:53 +00003961 return (EFI_INVALID_PARAMETER);
3962 }
3963 } else {
3964 return (EFI_INVALID_PARAMETER);
3965 }
3966 }
3967
3968 //
3969 // Chop off leading spaces
3970 //
3971 for (Walker = String; Walker != NULL && *Walker != CHAR_NULL && *Walker == L' '; Walker++);
3972
3973 //
3974 // make sure we have something left that is numeric.
3975 //
jcarsey658bf432014-11-04 22:33:16 +00003976 if (Walker == NULL || *Walker == CHAR_NULL || !InternalShellIsHexOrDecimalNumber(Walker, Hex, StopAtSpace, FALSE)) {
jcarsey252d9452011-03-25 20:49:53 +00003977 return (EFI_INVALID_PARAMETER);
darylm503b0934ac2011-11-12 00:35:11 +00003978 }
jcarsey252d9452011-03-25 20:49:53 +00003979
3980 //
3981 // do the conversion.
3982 //
3983 if (Hex || StrnCmp(Walker, L"0x", 2) == 0 || StrnCmp(Walker, L"0X", 2) == 0){
3984 Status = InternalShellStrHexToUint64(Walker, &RetVal, StopAtSpace);
3985 } else {
3986 Status = InternalShellStrDecimalToUint64(Walker, &RetVal, StopAtSpace);
3987 }
3988
3989 if (Value == NULL && !EFI_ERROR(Status)) {
3990 return (EFI_NOT_FOUND);
3991 }
3992
3993 if (Value != NULL) {
3994 *Value = RetVal;
3995 }
3996
3997 return (Status);
3998}
3999
4000/**
4001 Function to determin if an entire string is a valid number.
4002
4003 If Hex it must be preceeded with a 0x or has ForceHex, set TRUE.
4004
4005 @param[in] String The string to evaluate.
4006 @param[in] ForceHex TRUE - always assume hex.
4007 @param[in] StopAtSpace TRUE to halt upon finding a space, FALSE to keep going.
4008
4009 @retval TRUE It is all numeric (dec/hex) characters.
4010 @retval FALSE There is a non-numeric character.
4011**/
4012BOOLEAN
4013EFIAPI
4014ShellIsHexOrDecimalNumber (
4015 IN CONST CHAR16 *String,
4016 IN CONST BOOLEAN ForceHex,
4017 IN CONST BOOLEAN StopAtSpace
4018 )
4019{
4020 if (ShellConvertStringToUint64(String, NULL, ForceHex, StopAtSpace) == EFI_NOT_FOUND) {
4021 return (TRUE);
4022 }
4023 return (FALSE);
4024}
jcarsey4d0a4fc2011-07-06 22:28:36 +00004025
4026/**
4027 Function to read a single line from a SHELL_FILE_HANDLE. The \n is not included in the returned
4028 buffer. The returned buffer must be callee freed.
4029
4030 If the position upon start is 0, then the Ascii Boolean will be set. This should be
4031 maintained and not changed for all operations with the same file.
4032
ydong104ff7e372011-09-02 08:05:34 +00004033 @param[in] Handle SHELL_FILE_HANDLE to read from.
4034 @param[in, out] Ascii Boolean value for indicating whether the file is
4035 Ascii (TRUE) or UCS2 (FALSE).
jcarsey4d0a4fc2011-07-06 22:28:36 +00004036
jcarseybeab0fc2011-10-10 17:26:25 +00004037 @return The line of text from the file.
4038 @retval NULL There was not enough memory available.
jcarsey4d0a4fc2011-07-06 22:28:36 +00004039
4040 @sa ShellFileHandleReadLine
4041**/
4042CHAR16*
4043EFIAPI
4044ShellFileHandleReturnLine(
4045 IN SHELL_FILE_HANDLE Handle,
4046 IN OUT BOOLEAN *Ascii
4047 )
4048{
4049 CHAR16 *RetVal;
4050 UINTN Size;
4051 EFI_STATUS Status;
4052
4053 Size = 0;
4054 RetVal = NULL;
4055
4056 Status = ShellFileHandleReadLine(Handle, RetVal, &Size, FALSE, Ascii);
4057 if (Status == EFI_BUFFER_TOO_SMALL) {
4058 RetVal = AllocateZeroPool(Size);
jcarseybeab0fc2011-10-10 17:26:25 +00004059 if (RetVal == NULL) {
4060 return (NULL);
4061 }
jcarsey4d0a4fc2011-07-06 22:28:36 +00004062 Status = ShellFileHandleReadLine(Handle, RetVal, &Size, FALSE, Ascii);
darylm503b0934ac2011-11-12 00:35:11 +00004063
jcarsey4d0a4fc2011-07-06 22:28:36 +00004064 }
jcarsey4d0a4fc2011-07-06 22:28:36 +00004065 if (EFI_ERROR(Status) && (RetVal != NULL)) {
4066 FreePool(RetVal);
4067 RetVal = NULL;
4068 }
4069 return (RetVal);
4070}
4071
4072/**
4073 Function to read a single line (up to but not including the \n) from a SHELL_FILE_HANDLE.
4074
4075 If the position upon start is 0, then the Ascii Boolean will be set. This should be
4076 maintained and not changed for all operations with the same file.
4077
ydong104ff7e372011-09-02 08:05:34 +00004078 @param[in] Handle SHELL_FILE_HANDLE to read from.
4079 @param[in, out] Buffer The pointer to buffer to read into.
4080 @param[in, out] Size The pointer to number of bytes in Buffer.
4081 @param[in] Truncate If the buffer is large enough, this has no effect.
4082 If the buffer is is too small and Truncate is TRUE,
4083 the line will be truncated.
4084 If the buffer is is too small and Truncate is FALSE,
4085 then no read will occur.
jcarsey4d0a4fc2011-07-06 22:28:36 +00004086
ydong104ff7e372011-09-02 08:05:34 +00004087 @param[in, out] Ascii Boolean value for indicating whether the file is
4088 Ascii (TRUE) or UCS2 (FALSE).
jcarsey4d0a4fc2011-07-06 22:28:36 +00004089
4090 @retval EFI_SUCCESS The operation was successful. The line is stored in
4091 Buffer.
4092 @retval EFI_INVALID_PARAMETER Handle was NULL.
4093 @retval EFI_INVALID_PARAMETER Size was NULL.
4094 @retval EFI_BUFFER_TOO_SMALL Size was not large enough to store the line.
4095 Size was updated to the minimum space required.
4096**/
4097EFI_STATUS
4098EFIAPI
4099ShellFileHandleReadLine(
4100 IN SHELL_FILE_HANDLE Handle,
4101 IN OUT CHAR16 *Buffer,
4102 IN OUT UINTN *Size,
4103 IN BOOLEAN Truncate,
4104 IN OUT BOOLEAN *Ascii
4105 )
4106{
4107 EFI_STATUS Status;
4108 CHAR16 CharBuffer;
4109 UINTN CharSize;
4110 UINTN CountSoFar;
4111 UINT64 OriginalFilePosition;
4112
4113
4114 if (Handle == NULL
4115 ||Size == NULL
4116 ){
4117 return (EFI_INVALID_PARAMETER);
4118 }
4119 if (Buffer == NULL) {
4120 ASSERT(*Size == 0);
4121 } else {
4122 *Buffer = CHAR_NULL;
4123 }
4124 gEfiShellProtocol->GetFilePosition(Handle, &OriginalFilePosition);
4125 if (OriginalFilePosition == 0) {
4126 CharSize = sizeof(CHAR16);
4127 Status = gEfiShellProtocol->ReadFile(Handle, &CharSize, &CharBuffer);
4128 ASSERT_EFI_ERROR(Status);
4129 if (CharBuffer == gUnicodeFileTag) {
4130 *Ascii = FALSE;
4131 } else {
4132 *Ascii = TRUE;
4133 gEfiShellProtocol->SetFilePosition(Handle, OriginalFilePosition);
4134 }
4135 }
4136
4137 for (CountSoFar = 0;;CountSoFar++){
4138 CharBuffer = 0;
4139 if (*Ascii) {
4140 CharSize = sizeof(CHAR8);
4141 } else {
4142 CharSize = sizeof(CHAR16);
4143 }
4144 Status = gEfiShellProtocol->ReadFile(Handle, &CharSize, &CharBuffer);
4145 if ( EFI_ERROR(Status)
4146 || CharSize == 0
4147 || (CharBuffer == L'\n' && !(*Ascii))
4148 || (CharBuffer == '\n' && *Ascii)
4149 ){
4150 break;
4151 }
4152 //
4153 // if we have space save it...
4154 //
4155 if ((CountSoFar+1)*sizeof(CHAR16) < *Size){
4156 ASSERT(Buffer != NULL);
4157 ((CHAR16*)Buffer)[CountSoFar] = CharBuffer;
4158 ((CHAR16*)Buffer)[CountSoFar+1] = CHAR_NULL;
4159 }
4160 }
4161
4162 //
4163 // if we ran out of space tell when...
4164 //
4165 if ((CountSoFar+1)*sizeof(CHAR16) > *Size){
4166 *Size = (CountSoFar+1)*sizeof(CHAR16);
4167 if (!Truncate) {
4168 gEfiShellProtocol->SetFilePosition(Handle, OriginalFilePosition);
4169 } else {
4170 DEBUG((DEBUG_WARN, "The line was truncated in ShellFileHandleReadLine"));
4171 }
4172 return (EFI_BUFFER_TOO_SMALL);
4173 }
4174 while(Buffer[StrLen(Buffer)-1] == L'\r') {
4175 Buffer[StrLen(Buffer)-1] = CHAR_NULL;
4176 }
4177
4178 return (Status);
4179}
jcarseyfb5278e2013-02-20 18:21:14 +00004180
4181/**
jcarsey365aa982013-03-04 21:54:02 +00004182 Function to print help file / man page content in the spec from the UEFI Shell protocol GetHelpText function.
4183
4184 @param[in] CommandToGetHelpOn Pointer to a string containing the command name of help file to be printed.
4185 @param[in] SectionToGetHelpOn Pointer to the section specifier(s).
4186 @param[in] PrintCommandText If TRUE, prints the command followed by the help content, otherwise prints
4187 the help content only.
4188 @retval EFI_DEVICE_ERROR The help data format was incorrect.
4189 @retval EFI_NOT_FOUND The help data could not be found.
4190 @retval EFI_SUCCESS The operation was successful.
4191**/
4192EFI_STATUS
4193EFIAPI
4194ShellPrintHelp (
4195 IN CONST CHAR16 *CommandToGetHelpOn,
4196 IN CONST CHAR16 *SectionToGetHelpOn,
4197 IN BOOLEAN PrintCommandText
4198 )
4199{
4200 EFI_STATUS Status;
4201 CHAR16 *OutText;
4202
4203 OutText = NULL;
4204
4205 //
4206 // Get the string to print based
4207 //
4208 Status = gEfiShellProtocol->GetHelpText (CommandToGetHelpOn, SectionToGetHelpOn, &OutText);
4209
4210 //
4211 // make sure we got a valid string
4212 //
4213 if (EFI_ERROR(Status)){
4214 return Status;
4215 }
4216 if (OutText == NULL || StrLen(OutText) == 0) {
4217 return EFI_NOT_FOUND;
4218 }
4219
4220 //
4221 // Chop off trailing stuff we dont need
4222 //
4223 while (OutText[StrLen(OutText)-1] == L'\r' || OutText[StrLen(OutText)-1] == L'\n' || OutText[StrLen(OutText)-1] == L' ') {
4224 OutText[StrLen(OutText)-1] = CHAR_NULL;
4225 }
4226
4227 //
4228 // Print this out to the console
4229 //
4230 if (PrintCommandText) {
4231 ShellPrintEx(-1, -1, L"%H%-14s%N- %s\r\n", CommandToGetHelpOn, OutText);
4232 } else {
4233 ShellPrintEx(-1, -1, L"%N%s\r\n", OutText);
4234 }
4235
4236 SHELL_FREE_NON_NULL(OutText);
4237
4238 return EFI_SUCCESS;
4239}
4240
4241/**
jcarseyfb5278e2013-02-20 18:21:14 +00004242 Function to delete a file by name
4243
4244 @param[in] FileName Pointer to file name to delete.
4245
4246 @retval EFI_SUCCESS the file was deleted sucessfully
4247 @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not
4248 deleted
4249 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
4250 @retval EFI_NOT_FOUND The specified file could not be found on the
4251 device or the file system could not be found
4252 on the device.
4253 @retval EFI_NO_MEDIA The device has no medium.
4254 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
4255 medium is no longer supported.
4256 @retval EFI_DEVICE_ERROR The device reported an error.
4257 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
4258 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
4259 @retval EFI_ACCESS_DENIED The file was opened read only.
4260 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
4261 file.
4262 @retval other The file failed to open
4263**/
4264EFI_STATUS
4265EFIAPI
4266ShellDeleteFileByName(
4267 IN CONST CHAR16 *FileName
4268 )
4269{
4270 EFI_STATUS Status;
4271 SHELL_FILE_HANDLE FileHandle;
4272
4273 Status = ShellFileExists(FileName);
4274
4275 if (Status == EFI_SUCCESS){
4276 Status = ShellOpenFileByName(FileName, &FileHandle, EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE, 0x0);
4277 if (Status == EFI_SUCCESS){
4278 Status = ShellDeleteFile(&FileHandle);
4279 }
4280 }
4281
4282 return(Status);
4283
4284}
Qiu Shumin0960ba12014-09-17 07:58:31 +00004285
4286/**
4287 Cleans off all the quotes in the string.
4288
4289 @param[in] OriginalString pointer to the string to be cleaned.
4290 @param[out] CleanString The new string with all quotes removed.
4291 Memory allocated in the function and free
4292 by caller.
4293
4294 @retval EFI_SUCCESS The operation was successful.
4295**/
4296EFI_STATUS
4297EFIAPI
4298InternalShellStripQuotes (
4299 IN CONST CHAR16 *OriginalString,
4300 OUT CHAR16 **CleanString
4301 )
4302{
4303 CHAR16 *Walker;
4304
4305 if (OriginalString == NULL || CleanString == NULL) {
4306 return EFI_INVALID_PARAMETER;
4307 }
4308
4309 *CleanString = AllocateCopyPool (StrSize (OriginalString), OriginalString);
4310 if (*CleanString == NULL) {
4311 return EFI_OUT_OF_RESOURCES;
4312 }
4313
4314 for (Walker = *CleanString; Walker != NULL && *Walker != CHAR_NULL ; Walker++) {
4315 if (*Walker == L'\"') {
4316 CopyMem(Walker, Walker+1, StrSize(Walker) - sizeof(Walker[0]));
4317 }
4318 }
4319
4320 return EFI_SUCCESS;
4321}
4322