blob: 844f17abc622b3c7220b3f6707e0fc762d8857d6 [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>
jaben carsey21a86a72015-01-13 22:16:41 +000018#include <Library/PathLib.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)) {
705 ShellCreateDirectory (FileNameCopy, FileHandle);
706 ShellCloseFile (FileHandle);
707 }
708 SHELL_FREE_NON_NULL (FileNameCopy);
jcarseya405b862010-09-14 05:18:09 +0000709 }
jaben carsey21a86a72015-01-13 22:16:41 +0000710
jcarsey94b17fa2009-05-07 18:46:18 +0000711 //
jaben carsey21a86a72015-01-13 22:16:41 +0000712 // Use UEFI Shell 2.0 method to create the file
jcarsey94b17fa2009-05-07 18:46:18 +0000713 //
jcarsey366f81a2011-06-27 21:04:22 +0000714 Status = gEfiShellProtocol->OpenFileByName(FileName,
jcarseyb1f95a02009-06-16 00:23:19 +0000715 FileHandle,
716 OpenMode);
jcarseya405b862010-09-14 05:18:09 +0000717 if (StrCmp(FileName, L"NUL") != 0 && !EFI_ERROR(Status) && ((OpenMode & EFI_FILE_MODE_CREATE) != 0)){
jcarsey2247dde2009-11-09 18:08:58 +0000718 FileInfo = FileFunctionMap.GetFileInfo(*FileHandle);
jcarseyb1f95a02009-06-16 00:23:19 +0000719 ASSERT(FileInfo != NULL);
720 FileInfo->Attribute = Attributes;
jcarsey2247dde2009-11-09 18:08:58 +0000721 Status = FileFunctionMap.SetFileInfo(*FileHandle, FileInfo);
722 FreePool(FileInfo);
jcarseyb1f95a02009-06-16 00:23:19 +0000723 }
724 return (Status);
jcarsey1e6e84c2010-01-25 20:05:08 +0000725 }
jcarsey94b17fa2009-05-07 18:46:18 +0000726 //
727 // Using EFI Shell version
728 // this means convert name to path and call that function
729 // since this will use EFI method again that will open it.
730 //
731 ASSERT(mEfiShellEnvironment2 != NULL);
jcarseyb82bfcc2009-06-29 16:28:23 +0000732 FilePath = mEfiShellEnvironment2->NameToPath ((CHAR16*)FileName);
xdu290bfa222010-07-19 05:21:27 +0000733 if (FilePath != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +0000734 return (ShellOpenFileByDevicePath(&FilePath,
735 &DeviceHandle,
736 FileHandle,
737 OpenMode,
jcarseya405b862010-09-14 05:18:09 +0000738 Attributes));
jcarsey94b17fa2009-05-07 18:46:18 +0000739 }
740 return (EFI_DEVICE_ERROR);
741}
742/**
743 This function create a directory
744
jcarsey1e6e84c2010-01-25 20:05:08 +0000745 If return is EFI_SUCCESS, the Filehandle is the opened directory's handle;
746 otherwise, the Filehandle is NULL. If the directory already existed, this
jcarsey94b17fa2009-05-07 18:46:18 +0000747 function opens the existing directory.
748
darylm503b0934ac2011-11-12 00:35:11 +0000749 @param DirectoryName pointer to directory name
750 @param FileHandle pointer to the file handle.
jcarsey94b17fa2009-05-07 18:46:18 +0000751
darylm503b0934ac2011-11-12 00:35:11 +0000752 @retval EFI_SUCCESS The information was set.
753 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
754 @retval EFI_UNSUPPORTED Could not open the file path.
755 @retval EFI_NOT_FOUND The specified file could not be found on the
jcarsey1e6e84c2010-01-25 20:05:08 +0000756 device or the file system could not be found
jcarsey94b17fa2009-05-07 18:46:18 +0000757 on the device.
darylm503b0934ac2011-11-12 00:35:11 +0000758 @retval EFI_NO_MEDIA The device has no medium.
759 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
jcarsey94b17fa2009-05-07 18:46:18 +0000760 medium is no longer supported.
darylm503b0934ac2011-11-12 00:35:11 +0000761 @retval EFI_DEVICE_ERROR The device reported an error.
762 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
763 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
764 @retval EFI_ACCESS_DENIED The file was opened read only.
765 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
jcarsey94b17fa2009-05-07 18:46:18 +0000766 file.
darylm503b0934ac2011-11-12 00:35:11 +0000767 @retval EFI_VOLUME_FULL The volume is full.
jcarsey94b17fa2009-05-07 18:46:18 +0000768 @sa ShellOpenFileByName
769**/
770EFI_STATUS
771EFIAPI
772ShellCreateDirectory(
jcarseyb82bfcc2009-06-29 16:28:23 +0000773 IN CONST CHAR16 *DirectoryName,
jcarseya405b862010-09-14 05:18:09 +0000774 OUT SHELL_FILE_HANDLE *FileHandle
775 )
776{
jcarsey366f81a2011-06-27 21:04:22 +0000777 if (gEfiShellProtocol != NULL) {
jcarsey2247dde2009-11-09 18:08:58 +0000778 //
779 // Use UEFI Shell 2.0 method
780 //
jcarsey366f81a2011-06-27 21:04:22 +0000781 return (gEfiShellProtocol->CreateFile(DirectoryName,
jcarsey2247dde2009-11-09 18:08:58 +0000782 EFI_FILE_DIRECTORY,
783 FileHandle
jcarseya405b862010-09-14 05:18:09 +0000784 ));
jcarsey2247dde2009-11-09 18:08:58 +0000785 } else {
786 return (ShellOpenFileByName(DirectoryName,
787 FileHandle,
788 EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE,
789 EFI_FILE_DIRECTORY
jcarseya405b862010-09-14 05:18:09 +0000790 ));
jcarsey2247dde2009-11-09 18:08:58 +0000791 }
jcarsey94b17fa2009-05-07 18:46:18 +0000792}
793
794/**
795 This function reads information from an opened file.
796
jcarsey1e6e84c2010-01-25 20:05:08 +0000797 If FileHandle is not a directory, the function reads the requested number of
798 bytes from the file at the file's current position and returns them in Buffer.
jcarsey94b17fa2009-05-07 18:46:18 +0000799 If the read goes beyond the end of the file, the read length is truncated to the
jcarsey1e6e84c2010-01-25 20:05:08 +0000800 end of the file. The file's current position is increased by the number of bytes
801 returned. If FileHandle is a directory, the function reads the directory entry
802 at the file's current position and returns the entry in Buffer. If the Buffer
803 is not large enough to hold the current directory entry, then
804 EFI_BUFFER_TOO_SMALL is returned and the current file position is not updated.
805 BufferSize is set to be the size of the buffer needed to read the entry. On
806 success, the current position is updated to the next directory entry. If there
807 are no more directory entries, the read returns a zero-length buffer.
jcarsey94b17fa2009-05-07 18:46:18 +0000808 EFI_FILE_INFO is the structure returned as the directory entry.
809
810 @param FileHandle the opened file handle
jcarsey1e6e84c2010-01-25 20:05:08 +0000811 @param BufferSize on input the size of buffer in bytes. on return
jcarsey94b17fa2009-05-07 18:46:18 +0000812 the number of bytes written.
813 @param Buffer the buffer to put read data into.
814
darylm503b0934ac2011-11-12 00:35:11 +0000815 @retval EFI_SUCCESS Data was read.
816 @retval EFI_NO_MEDIA The device has no media.
817 @retval EFI_DEVICE_ERROR The device reported an error.
818 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
819 @retval EFI_BUFFER_TO_SMALL Buffer is too small. ReadSize contains required
jcarsey94b17fa2009-05-07 18:46:18 +0000820 size.
821
822**/
823EFI_STATUS
824EFIAPI
825ShellReadFile(
jcarseya405b862010-09-14 05:18:09 +0000826 IN SHELL_FILE_HANDLE FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000827 IN OUT UINTN *BufferSize,
828 OUT VOID *Buffer
jcarseya405b862010-09-14 05:18:09 +0000829 )
830{
jcarseyd2b45642009-05-11 18:02:16 +0000831 return (FileFunctionMap.ReadFile(FileHandle, BufferSize, Buffer));
jcarsey94b17fa2009-05-07 18:46:18 +0000832}
833
834
835/**
836 Write data to a file.
837
jcarsey1e6e84c2010-01-25 20:05:08 +0000838 This function writes the specified number of bytes to the file at the current
839 file position. The current file position is advanced the actual number of bytes
840 written, which is returned in BufferSize. Partial writes only occur when there
841 has been a data error during the write attempt (such as "volume space full").
842 The file is automatically grown to hold the data if required. Direct writes to
jcarsey94b17fa2009-05-07 18:46:18 +0000843 opened directories are not supported.
844
845 @param FileHandle The opened file for writing
846 @param BufferSize on input the number of bytes in Buffer. On output
847 the number of bytes written.
848 @param Buffer the buffer containing data to write is stored.
849
darylm503b0934ac2011-11-12 00:35:11 +0000850 @retval EFI_SUCCESS Data was written.
851 @retval EFI_UNSUPPORTED Writes to an open directory are not supported.
852 @retval EFI_NO_MEDIA The device has no media.
853 @retval EFI_DEVICE_ERROR The device reported an error.
854 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
855 @retval EFI_WRITE_PROTECTED The device is write-protected.
856 @retval EFI_ACCESS_DENIED The file was open for read only.
857 @retval EFI_VOLUME_FULL The volume is full.
jcarsey94b17fa2009-05-07 18:46:18 +0000858**/
859EFI_STATUS
860EFIAPI
861ShellWriteFile(
jcarsey252d9452011-03-25 20:49:53 +0000862 IN SHELL_FILE_HANDLE FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000863 IN OUT UINTN *BufferSize,
864 IN VOID *Buffer
jcarseya405b862010-09-14 05:18:09 +0000865 )
866{
jcarseyd2b45642009-05-11 18:02:16 +0000867 return (FileFunctionMap.WriteFile(FileHandle, BufferSize, Buffer));
jcarsey94b17fa2009-05-07 18:46:18 +0000868}
869
jcarsey1e6e84c2010-01-25 20:05:08 +0000870/**
jcarsey94b17fa2009-05-07 18:46:18 +0000871 Close an open file handle.
872
jcarsey1e6e84c2010-01-25 20:05:08 +0000873 This function closes a specified file handle. All "dirty" cached file data is
874 flushed to the device, and the file is closed. In all cases the handle is
jcarsey94b17fa2009-05-07 18:46:18 +0000875 closed.
876
877@param FileHandle the file handle to close.
878
879@retval EFI_SUCCESS the file handle was closed sucessfully.
880**/
881EFI_STATUS
882EFIAPI
883ShellCloseFile (
jcarseya405b862010-09-14 05:18:09 +0000884 IN SHELL_FILE_HANDLE *FileHandle
885 )
886{
jcarseyd2b45642009-05-11 18:02:16 +0000887 return (FileFunctionMap.CloseFile(*FileHandle));
jcarsey94b17fa2009-05-07 18:46:18 +0000888}
889
890/**
891 Delete a file and close the handle
892
893 This function closes and deletes a file. In all cases the file handle is closed.
jcarsey1e6e84c2010-01-25 20:05:08 +0000894 If the file cannot be deleted, the warning code EFI_WARN_DELETE_FAILURE is
jcarsey94b17fa2009-05-07 18:46:18 +0000895 returned, but the handle is still closed.
896
897 @param FileHandle the file handle to delete
898
899 @retval EFI_SUCCESS the file was closed sucessfully
jcarsey1e6e84c2010-01-25 20:05:08 +0000900 @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not
jcarsey94b17fa2009-05-07 18:46:18 +0000901 deleted
darylm503b0934ac2011-11-12 00:35:11 +0000902 @retval INVALID_PARAMETER One of the parameters has an invalid value.
jcarsey94b17fa2009-05-07 18:46:18 +0000903**/
904EFI_STATUS
905EFIAPI
906ShellDeleteFile (
darylm503b0934ac2011-11-12 00:35:11 +0000907 IN SHELL_FILE_HANDLE *FileHandle
jcarseya405b862010-09-14 05:18:09 +0000908 )
909{
jcarseyd2b45642009-05-11 18:02:16 +0000910 return (FileFunctionMap.DeleteFile(*FileHandle));
jcarsey94b17fa2009-05-07 18:46:18 +0000911}
912
913/**
914 Set the current position in a file.
915
jcarsey1e6e84c2010-01-25 20:05:08 +0000916 This function sets the current file position for the handle to the position
jcarsey94b17fa2009-05-07 18:46:18 +0000917 supplied. With the exception of seeking to position 0xFFFFFFFFFFFFFFFF, only
jcarsey1e6e84c2010-01-25 20:05:08 +0000918 absolute positioning is supported, and seeking past the end of the file is
919 allowed (a subsequent write would grow the file). Seeking to position
jcarsey94b17fa2009-05-07 18:46:18 +0000920 0xFFFFFFFFFFFFFFFF causes the current position to be set to the end of the file.
jcarsey1e6e84c2010-01-25 20:05:08 +0000921 If FileHandle is a directory, the only position that may be set is zero. This
jcarsey94b17fa2009-05-07 18:46:18 +0000922 has the effect of starting the read process of the directory entries over.
923
924 @param FileHandle The file handle on which the position is being set
925 @param Position Byte position from begining of file
926
927 @retval EFI_SUCCESS Operation completed sucessfully.
jcarsey1e6e84c2010-01-25 20:05:08 +0000928 @retval EFI_UNSUPPORTED the seek request for non-zero is not valid on
jcarsey94b17fa2009-05-07 18:46:18 +0000929 directories.
930 @retval INVALID_PARAMETER One of the parameters has an invalid value.
931**/
932EFI_STATUS
933EFIAPI
934ShellSetFilePosition (
darylm503b0934ac2011-11-12 00:35:11 +0000935 IN SHELL_FILE_HANDLE FileHandle,
936 IN UINT64 Position
jcarseya405b862010-09-14 05:18:09 +0000937 )
938{
jcarseyd2b45642009-05-11 18:02:16 +0000939 return (FileFunctionMap.SetFilePosition(FileHandle, Position));
jcarsey94b17fa2009-05-07 18:46:18 +0000940}
941
jcarsey1e6e84c2010-01-25 20:05:08 +0000942/**
jcarsey94b17fa2009-05-07 18:46:18 +0000943 Gets a file's current position
944
jcarsey1e6e84c2010-01-25 20:05:08 +0000945 This function retrieves the current file position for the file handle. For
946 directories, the current file position has no meaning outside of the file
jcarsey94b17fa2009-05-07 18:46:18 +0000947 system driver and as such the operation is not supported. An error is returned
948 if FileHandle is a directory.
949
950 @param FileHandle The open file handle on which to get the position.
951 @param Position Byte position from begining of file.
952
953 @retval EFI_SUCCESS the operation completed sucessfully.
954 @retval INVALID_PARAMETER One of the parameters has an invalid value.
955 @retval EFI_UNSUPPORTED the request is not valid on directories.
956**/
957EFI_STATUS
958EFIAPI
959ShellGetFilePosition (
jcarseya405b862010-09-14 05:18:09 +0000960 IN SHELL_FILE_HANDLE FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +0000961 OUT UINT64 *Position
jcarseya405b862010-09-14 05:18:09 +0000962 )
963{
jcarseyd2b45642009-05-11 18:02:16 +0000964 return (FileFunctionMap.GetFilePosition(FileHandle, Position));
jcarsey94b17fa2009-05-07 18:46:18 +0000965}
966/**
967 Flushes data on a file
jcarsey1e6e84c2010-01-25 20:05:08 +0000968
jcarsey94b17fa2009-05-07 18:46:18 +0000969 This function flushes all modified data associated with a file to a device.
970
971 @param FileHandle The file handle on which to flush data
972
973 @retval EFI_SUCCESS The data was flushed.
974 @retval EFI_NO_MEDIA The device has no media.
975 @retval EFI_DEVICE_ERROR The device reported an error.
976 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
977 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
978 @retval EFI_ACCESS_DENIED The file was opened for read only.
979**/
980EFI_STATUS
981EFIAPI
982ShellFlushFile (
jcarseya405b862010-09-14 05:18:09 +0000983 IN SHELL_FILE_HANDLE FileHandle
984 )
985{
jcarseyd2b45642009-05-11 18:02:16 +0000986 return (FileFunctionMap.FlushFile(FileHandle));
jcarsey94b17fa2009-05-07 18:46:18 +0000987}
988
darylm503b0934ac2011-11-12 00:35:11 +0000989/** Retrieve first entry from a directory.
jcarsey94b17fa2009-05-07 18:46:18 +0000990
darylm503b0934ac2011-11-12 00:35:11 +0000991 This function takes an open directory handle and gets information from the
992 first entry in the directory. A buffer is allocated to contain
993 the information and a pointer to the buffer is returned in *Buffer. The
994 caller can use ShellFindNextFile() to get subsequent directory entries.
jcarsey94b17fa2009-05-07 18:46:18 +0000995
darylm503b0934ac2011-11-12 00:35:11 +0000996 The buffer will be freed by ShellFindNextFile() when the last directory
997 entry is read. Otherwise, the caller must free the buffer, using FreePool,
998 when finished with it.
999
1000 @param[in] DirHandle The file handle of the directory to search.
1001 @param[out] Buffer The pointer to the buffer for the file's information.
jcarsey94b17fa2009-05-07 18:46:18 +00001002
1003 @retval EFI_SUCCESS Found the first file.
1004 @retval EFI_NOT_FOUND Cannot find the directory.
1005 @retval EFI_NO_MEDIA The device has no media.
1006 @retval EFI_DEVICE_ERROR The device reported an error.
1007 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
1008 @return Others status of ShellGetFileInfo, ShellSetFilePosition,
1009 or ShellReadFile
1010**/
1011EFI_STATUS
1012EFIAPI
1013ShellFindFirstFile (
jcarseya405b862010-09-14 05:18:09 +00001014 IN SHELL_FILE_HANDLE DirHandle,
jcarseyd2b45642009-05-11 18:02:16 +00001015 OUT EFI_FILE_INFO **Buffer
jcarseya405b862010-09-14 05:18:09 +00001016 )
1017{
jcarsey94b17fa2009-05-07 18:46:18 +00001018 //
jcarseyd2b45642009-05-11 18:02:16 +00001019 // pass to file handle lib
jcarsey94b17fa2009-05-07 18:46:18 +00001020 //
jcarseyd2b45642009-05-11 18:02:16 +00001021 return (FileHandleFindFirstFile(DirHandle, Buffer));
jcarsey94b17fa2009-05-07 18:46:18 +00001022}
darylm503b0934ac2011-11-12 00:35:11 +00001023/** Retrieve next entries from a directory.
jcarsey94b17fa2009-05-07 18:46:18 +00001024
darylm503b0934ac2011-11-12 00:35:11 +00001025 To use this function, the caller must first call the ShellFindFirstFile()
1026 function to get the first directory entry. Subsequent directory entries are
1027 retrieved by using the ShellFindNextFile() function. This function can
1028 be called several times to get each entry from the directory. If the call of
1029 ShellFindNextFile() retrieved the last directory entry, the next call of
1030 this function will set *NoFile to TRUE and free the buffer.
jcarsey94b17fa2009-05-07 18:46:18 +00001031
darylm503b0934ac2011-11-12 00:35:11 +00001032 @param[in] DirHandle The file handle of the directory.
1033 @param[out] Buffer The pointer to buffer for file's information.
1034 @param[out] NoFile The pointer to boolean when last file is found.
jcarsey94b17fa2009-05-07 18:46:18 +00001035
1036 @retval EFI_SUCCESS Found the next file, or reached last file
1037 @retval EFI_NO_MEDIA The device has no media.
1038 @retval EFI_DEVICE_ERROR The device reported an error.
1039 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
1040**/
1041EFI_STATUS
1042EFIAPI
1043ShellFindNextFile(
jcarseya405b862010-09-14 05:18:09 +00001044 IN SHELL_FILE_HANDLE DirHandle,
jcarsey94b17fa2009-05-07 18:46:18 +00001045 OUT EFI_FILE_INFO *Buffer,
1046 OUT BOOLEAN *NoFile
jcarseya405b862010-09-14 05:18:09 +00001047 )
1048{
jcarsey94b17fa2009-05-07 18:46:18 +00001049 //
jcarseyd2b45642009-05-11 18:02:16 +00001050 // pass to file handle lib
jcarsey94b17fa2009-05-07 18:46:18 +00001051 //
jcarseyd2b45642009-05-11 18:02:16 +00001052 return (FileHandleFindNextFile(DirHandle, Buffer, NoFile));
jcarsey94b17fa2009-05-07 18:46:18 +00001053}
1054/**
1055 Retrieve the size of a file.
1056
1057 if FileHandle is NULL then ASSERT()
1058 if Size is NULL then ASSERT()
1059
jcarsey1e6e84c2010-01-25 20:05:08 +00001060 This function extracts the file size info from the FileHandle's EFI_FILE_INFO
jcarsey94b17fa2009-05-07 18:46:18 +00001061 data.
1062
1063 @param FileHandle file handle from which size is retrieved
1064 @param Size pointer to size
1065
1066 @retval EFI_SUCCESS operation was completed sucessfully
1067 @retval EFI_DEVICE_ERROR cannot access the file
1068**/
1069EFI_STATUS
1070EFIAPI
1071ShellGetFileSize (
jcarseya405b862010-09-14 05:18:09 +00001072 IN SHELL_FILE_HANDLE FileHandle,
jcarsey94b17fa2009-05-07 18:46:18 +00001073 OUT UINT64 *Size
jcarseya405b862010-09-14 05:18:09 +00001074 )
1075{
jcarseyd2b45642009-05-11 18:02:16 +00001076 return (FileFunctionMap.GetFileSize(FileHandle, Size));
jcarsey94b17fa2009-05-07 18:46:18 +00001077}
1078/**
1079 Retrieves the status of the break execution flag
1080
1081 this function is useful to check whether the application is being asked to halt by the shell.
1082
1083 @retval TRUE the execution break is enabled
1084 @retval FALSE the execution break is not enabled
1085**/
1086BOOLEAN
1087EFIAPI
1088ShellGetExecutionBreakFlag(
1089 VOID
1090 )
1091{
jcarsey1e6e84c2010-01-25 20:05:08 +00001092 //
jcarsey94b17fa2009-05-07 18:46:18 +00001093 // Check for UEFI Shell 2.0 protocols
1094 //
jcarsey366f81a2011-06-27 21:04:22 +00001095 if (gEfiShellProtocol != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00001096
1097 //
1098 // We are using UEFI Shell 2.0; see if the event has been triggered
1099 //
jcarsey366f81a2011-06-27 21:04:22 +00001100 if (gBS->CheckEvent(gEfiShellProtocol->ExecutionBreak) != EFI_SUCCESS) {
jcarsey94b17fa2009-05-07 18:46:18 +00001101 return (FALSE);
1102 }
1103 return (TRUE);
jcarsey1e6e84c2010-01-25 20:05:08 +00001104 }
jcarsey94b17fa2009-05-07 18:46:18 +00001105
1106 //
1107 // using EFI Shell; call the function to check
1108 //
jcarsey92a54472011-06-27 20:33:13 +00001109 if (mEfiShellEnvironment2 != NULL) {
1110 return (mEfiShellEnvironment2->GetExecutionBreak());
1111 }
1112
1113 return (FALSE);
jcarsey94b17fa2009-05-07 18:46:18 +00001114}
1115/**
1116 return the value of an environment variable
1117
jcarsey1e6e84c2010-01-25 20:05:08 +00001118 this function gets the value of the environment variable set by the
jcarsey94b17fa2009-05-07 18:46:18 +00001119 ShellSetEnvironmentVariable function
1120
1121 @param EnvKey The key name of the environment variable.
1122
1123 @retval NULL the named environment variable does not exist.
1124 @return != NULL pointer to the value of the environment variable
1125**/
1126CONST CHAR16*
1127EFIAPI
1128ShellGetEnvironmentVariable (
jcarsey9b3bf082009-06-23 21:15:07 +00001129 IN CONST CHAR16 *EnvKey
jcarsey94b17fa2009-05-07 18:46:18 +00001130 )
1131{
jcarsey1e6e84c2010-01-25 20:05:08 +00001132 //
jcarsey94b17fa2009-05-07 18:46:18 +00001133 // Check for UEFI Shell 2.0 protocols
1134 //
jcarsey366f81a2011-06-27 21:04:22 +00001135 if (gEfiShellProtocol != NULL) {
1136 return (gEfiShellProtocol->GetEnv(EnvKey));
jcarsey94b17fa2009-05-07 18:46:18 +00001137 }
1138
1139 //
jcarsey92a54472011-06-27 20:33:13 +00001140 // Check for EFI shell
jcarsey94b17fa2009-05-07 18:46:18 +00001141 //
jcarsey92a54472011-06-27 20:33:13 +00001142 if (mEfiShellEnvironment2 != NULL) {
1143 return (mEfiShellEnvironment2->GetEnv((CHAR16*)EnvKey));
1144 }
jcarsey94b17fa2009-05-07 18:46:18 +00001145
jcarsey92a54472011-06-27 20:33:13 +00001146 return NULL;
jcarsey94b17fa2009-05-07 18:46:18 +00001147}
1148/**
1149 set the value of an environment variable
1150
1151This function changes the current value of the specified environment variable. If the
1152environment variable exists and the Value is an empty string, then the environment
1153variable is deleted. If the environment variable exists and the Value is not an empty
1154string, then the value of the environment variable is changed. If the environment
1155variable does not exist and the Value is an empty string, there is no action. If the
1156environment variable does not exist and the Value is a non-empty string, then the
1157environment variable is created and assigned the specified value.
1158
1159 This is not supported pre-UEFI Shell 2.0.
1160
1161 @param EnvKey The key name of the environment variable.
1162 @param EnvVal The Value of the environment variable
1163 @param Volatile Indicates whether the variable is non-volatile (FALSE) or volatile (TRUE).
1164
1165 @retval EFI_SUCCESS the operation was completed sucessfully
1166 @retval EFI_UNSUPPORTED This operation is not allowed in pre UEFI 2.0 Shell environments
1167**/
1168EFI_STATUS
1169EFIAPI
1170ShellSetEnvironmentVariable (
1171 IN CONST CHAR16 *EnvKey,
1172 IN CONST CHAR16 *EnvVal,
1173 IN BOOLEAN Volatile
1174 )
1175{
jcarsey1e6e84c2010-01-25 20:05:08 +00001176 //
jcarsey94b17fa2009-05-07 18:46:18 +00001177 // Check for UEFI Shell 2.0 protocols
1178 //
jcarsey366f81a2011-06-27 21:04:22 +00001179 if (gEfiShellProtocol != NULL) {
1180 return (gEfiShellProtocol->SetEnv(EnvKey, EnvVal, Volatile));
jcarsey1e6e84c2010-01-25 20:05:08 +00001181 }
jcarsey94b17fa2009-05-07 18:46:18 +00001182
1183 //
1184 // This feature does not exist under EFI shell
1185 //
1186 return (EFI_UNSUPPORTED);
1187}
jcarseya405b862010-09-14 05:18:09 +00001188
jcarsey94b17fa2009-05-07 18:46:18 +00001189/**
jcarseya405b862010-09-14 05:18:09 +00001190 Cause the shell to parse and execute a command line.
jcarsey94b17fa2009-05-07 18:46:18 +00001191
1192 This function creates a nested instance of the shell and executes the specified
jcarseya405b862010-09-14 05:18:09 +00001193 command (CommandLine) with the specified environment (Environment). Upon return,
1194 the status code returned by the specified command is placed in StatusCode.
1195 If Environment is NULL, then the current environment is used and all changes made
1196 by the commands executed will be reflected in the current environment. If the
1197 Environment is non-NULL, then the changes made will be discarded.
1198 The CommandLine is executed from the current working directory on the current
1199 device.
jcarsey94b17fa2009-05-07 18:46:18 +00001200
Brendan Jackman3877d0f2014-01-24 22:31:07 +00001201 The EnvironmentVariables pararemeter is ignored in a pre-UEFI Shell 2.0
jcarseya405b862010-09-14 05:18:09 +00001202 environment. The values pointed to by the parameters will be unchanged by the
1203 ShellExecute() function. The Output parameter has no effect in a
1204 UEFI Shell 2.0 environment.
jcarsey94b17fa2009-05-07 18:46:18 +00001205
jcarseya405b862010-09-14 05:18:09 +00001206 @param[in] ParentHandle The parent image starting the operation.
1207 @param[in] CommandLine The pointer to a NULL terminated command line.
1208 @param[in] Output True to display debug output. False to hide it.
1209 @param[in] EnvironmentVariables Optional pointer to array of environment variables
1210 in the form "x=y". If NULL, the current set is used.
1211 @param[out] Status The status of the run command line.
jcarsey94b17fa2009-05-07 18:46:18 +00001212
jcarseya405b862010-09-14 05:18:09 +00001213 @retval EFI_SUCCESS The operation completed sucessfully. Status
1214 contains the status code returned.
1215 @retval EFI_INVALID_PARAMETER A parameter contains an invalid value.
1216 @retval EFI_OUT_OF_RESOURCES Out of resources.
1217 @retval EFI_UNSUPPORTED The operation is not allowed.
jcarsey94b17fa2009-05-07 18:46:18 +00001218**/
1219EFI_STATUS
1220EFIAPI
1221ShellExecute (
1222 IN EFI_HANDLE *ParentHandle,
1223 IN CHAR16 *CommandLine OPTIONAL,
1224 IN BOOLEAN Output OPTIONAL,
1225 IN CHAR16 **EnvironmentVariables OPTIONAL,
1226 OUT EFI_STATUS *Status OPTIONAL
1227 )
1228{
Brendan Jackman3877d0f2014-01-24 22:31:07 +00001229 EFI_STATUS CmdStatus;
jcarsey1e6e84c2010-01-25 20:05:08 +00001230 //
jcarsey94b17fa2009-05-07 18:46:18 +00001231 // Check for UEFI Shell 2.0 protocols
1232 //
jcarsey366f81a2011-06-27 21:04:22 +00001233 if (gEfiShellProtocol != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00001234 //
1235 // Call UEFI Shell 2.0 version (not using Output parameter)
1236 //
jcarsey366f81a2011-06-27 21:04:22 +00001237 return (gEfiShellProtocol->Execute(ParentHandle,
jcarsey94b17fa2009-05-07 18:46:18 +00001238 CommandLine,
1239 EnvironmentVariables,
1240 Status));
jcarsey1e6e84c2010-01-25 20:05:08 +00001241 }
jcarsey92a54472011-06-27 20:33:13 +00001242
jcarsey94b17fa2009-05-07 18:46:18 +00001243 //
jcarsey92a54472011-06-27 20:33:13 +00001244 // Check for EFI shell
jcarsey94b17fa2009-05-07 18:46:18 +00001245 //
jcarsey92a54472011-06-27 20:33:13 +00001246 if (mEfiShellEnvironment2 != NULL) {
1247 //
Brendan Jackman3877d0f2014-01-24 22:31:07 +00001248 // Call EFI Shell version.
jcarsey92a54472011-06-27 20:33:13 +00001249 // Due to oddity in the EFI shell we want to dereference the ParentHandle here
1250 //
Brendan Jackman3877d0f2014-01-24 22:31:07 +00001251 CmdStatus = (mEfiShellEnvironment2->Execute(*ParentHandle,
jcarsey92a54472011-06-27 20:33:13 +00001252 CommandLine,
1253 Output));
Brendan Jackman3877d0f2014-01-24 22:31:07 +00001254 //
1255 // No Status output parameter so just use the returned status
1256 //
1257 if (Status != NULL) {
1258 *Status = CmdStatus;
1259 }
1260 //
1261 // If there was an error, we can't tell if it was from the command or from
1262 // the Execute() function, so we'll just assume the shell ran successfully
1263 // and the error came from the command.
1264 //
1265 return EFI_SUCCESS;
jcarsey92a54472011-06-27 20:33:13 +00001266 }
1267
1268 return (EFI_UNSUPPORTED);
jcarsey94b17fa2009-05-07 18:46:18 +00001269}
Brendan Jackman3877d0f2014-01-24 22:31:07 +00001270
jcarsey94b17fa2009-05-07 18:46:18 +00001271/**
1272 Retreives the current directory path
1273
jcarsey1e6e84c2010-01-25 20:05:08 +00001274 If the DeviceName is NULL, it returns the current device's current directory
1275 name. If the DeviceName is not NULL, it returns the current directory name
jcarsey94b17fa2009-05-07 18:46:18 +00001276 on specified drive.
1277
1278 @param DeviceName the name of the drive to get directory on
1279
1280 @retval NULL the directory does not exist
1281 @return != NULL the directory
1282**/
1283CONST CHAR16*
1284EFIAPI
1285ShellGetCurrentDir (
jcarseya405b862010-09-14 05:18:09 +00001286 IN CHAR16 * CONST DeviceName OPTIONAL
jcarsey94b17fa2009-05-07 18:46:18 +00001287 )
1288{
jcarsey1e6e84c2010-01-25 20:05:08 +00001289 //
jcarsey94b17fa2009-05-07 18:46:18 +00001290 // Check for UEFI Shell 2.0 protocols
1291 //
jcarsey366f81a2011-06-27 21:04:22 +00001292 if (gEfiShellProtocol != NULL) {
1293 return (gEfiShellProtocol->GetCurDir(DeviceName));
jcarsey1e6e84c2010-01-25 20:05:08 +00001294 }
jcarsey92a54472011-06-27 20:33:13 +00001295
jcarsey94b17fa2009-05-07 18:46:18 +00001296 //
jcarsey8bd282b2011-06-27 16:45:41 +00001297 // Check for EFI shell
jcarsey94b17fa2009-05-07 18:46:18 +00001298 //
jcarsey8bd282b2011-06-27 16:45:41 +00001299 if (mEfiShellEnvironment2 != NULL) {
1300 return (mEfiShellEnvironment2->CurDir(DeviceName));
1301 }
1302
1303 return (NULL);
jcarsey94b17fa2009-05-07 18:46:18 +00001304}
1305/**
1306 sets (enabled or disabled) the page break mode
1307
jcarsey1e6e84c2010-01-25 20:05:08 +00001308 when page break mode is enabled the screen will stop scrolling
jcarsey94b17fa2009-05-07 18:46:18 +00001309 and wait for operator input before scrolling a subsequent screen.
1310
1311 @param CurrentState TRUE to enable and FALSE to disable
1312**/
jcarsey1e6e84c2010-01-25 20:05:08 +00001313VOID
jcarsey94b17fa2009-05-07 18:46:18 +00001314EFIAPI
1315ShellSetPageBreakMode (
1316 IN BOOLEAN CurrentState
1317 )
1318{
1319 //
1320 // check for enabling
1321 //
1322 if (CurrentState != 0x00) {
jcarsey1e6e84c2010-01-25 20:05:08 +00001323 //
jcarsey94b17fa2009-05-07 18:46:18 +00001324 // check for UEFI Shell 2.0
1325 //
jcarsey366f81a2011-06-27 21:04:22 +00001326 if (gEfiShellProtocol != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00001327 //
1328 // Enable with UEFI 2.0 Shell
1329 //
jcarsey366f81a2011-06-27 21:04:22 +00001330 gEfiShellProtocol->EnablePageBreak();
jcarsey94b17fa2009-05-07 18:46:18 +00001331 return;
1332 } else {
jcarsey1e6e84c2010-01-25 20:05:08 +00001333 //
jcarsey92a54472011-06-27 20:33:13 +00001334 // Check for EFI shell
jcarsey94b17fa2009-05-07 18:46:18 +00001335 //
jcarsey92a54472011-06-27 20:33:13 +00001336 if (mEfiShellEnvironment2 != NULL) {
1337 //
1338 // Enable with EFI Shell
1339 //
1340 mEfiShellEnvironment2->EnablePageBreak (DEFAULT_INIT_ROW, DEFAULT_AUTO_LF);
1341 return;
1342 }
jcarsey94b17fa2009-05-07 18:46:18 +00001343 }
1344 } else {
jcarsey1e6e84c2010-01-25 20:05:08 +00001345 //
jcarsey94b17fa2009-05-07 18:46:18 +00001346 // check for UEFI Shell 2.0
1347 //
jcarsey366f81a2011-06-27 21:04:22 +00001348 if (gEfiShellProtocol != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00001349 //
1350 // Disable with UEFI 2.0 Shell
1351 //
jcarsey366f81a2011-06-27 21:04:22 +00001352 gEfiShellProtocol->DisablePageBreak();
jcarsey94b17fa2009-05-07 18:46:18 +00001353 return;
1354 } else {
jcarsey1e6e84c2010-01-25 20:05:08 +00001355 //
jcarsey92a54472011-06-27 20:33:13 +00001356 // Check for EFI shell
jcarsey94b17fa2009-05-07 18:46:18 +00001357 //
jcarsey92a54472011-06-27 20:33:13 +00001358 if (mEfiShellEnvironment2 != NULL) {
1359 //
1360 // Disable with EFI Shell
1361 //
1362 mEfiShellEnvironment2->DisablePageBreak ();
1363 return;
1364 }
jcarsey94b17fa2009-05-07 18:46:18 +00001365 }
1366 }
1367}
1368
1369///
1370/// version of EFI_SHELL_FILE_INFO struct, except has no CONST pointers.
1371/// This allows for the struct to be populated.
1372///
1373typedef struct {
jcarseyd2b45642009-05-11 18:02:16 +00001374 LIST_ENTRY Link;
jcarsey94b17fa2009-05-07 18:46:18 +00001375 EFI_STATUS Status;
1376 CHAR16 *FullName;
1377 CHAR16 *FileName;
jcarseya405b862010-09-14 05:18:09 +00001378 SHELL_FILE_HANDLE Handle;
jcarsey94b17fa2009-05-07 18:46:18 +00001379 EFI_FILE_INFO *Info;
1380} EFI_SHELL_FILE_INFO_NO_CONST;
1381
1382/**
1383 Converts a EFI shell list of structures to the coresponding UEFI Shell 2.0 type of list.
1384
1385 if OldStyleFileList is NULL then ASSERT()
1386
jcarsey1e6e84c2010-01-25 20:05:08 +00001387 this function will convert a SHELL_FILE_ARG based list into a callee allocated
jcarsey94b17fa2009-05-07 18:46:18 +00001388 EFI_SHELL_FILE_INFO based list. it is up to the caller to free the memory via
1389 the ShellCloseFileMetaArg function.
1390
ydong104ff7e372011-09-02 08:05:34 +00001391 @param[in] FileList the EFI shell list type
1392 @param[in, out] ListHead the list to add to
jcarsey94b17fa2009-05-07 18:46:18 +00001393
1394 @retval the resultant head of the double linked new format list;
1395**/
1396LIST_ENTRY*
1397EFIAPI
1398InternalShellConvertFileListType (
jcarsey9b3bf082009-06-23 21:15:07 +00001399 IN LIST_ENTRY *FileList,
1400 IN OUT LIST_ENTRY *ListHead
jcarsey125c2cf2009-11-18 21:36:50 +00001401 )
1402{
jcarsey94b17fa2009-05-07 18:46:18 +00001403 SHELL_FILE_ARG *OldInfo;
jcarsey9b3bf082009-06-23 21:15:07 +00001404 LIST_ENTRY *Link;
jcarsey94b17fa2009-05-07 18:46:18 +00001405 EFI_SHELL_FILE_INFO_NO_CONST *NewInfo;
1406
1407 //
jcarsey9b3bf082009-06-23 21:15:07 +00001408 // ASSERTs
jcarsey94b17fa2009-05-07 18:46:18 +00001409 //
jcarsey9b3bf082009-06-23 21:15:07 +00001410 ASSERT(FileList != NULL);
1411 ASSERT(ListHead != NULL);
jcarsey94b17fa2009-05-07 18:46:18 +00001412
1413 //
1414 // enumerate through each member of the old list and copy
1415 //
jcarseyd2b45642009-05-11 18:02:16 +00001416 for (Link = FileList->ForwardLink; Link != FileList; Link = Link->ForwardLink) {
jcarsey94b17fa2009-05-07 18:46:18 +00001417 OldInfo = CR (Link, SHELL_FILE_ARG, Link, SHELL_FILE_ARG_SIGNATURE);
jcarseya405b862010-09-14 05:18:09 +00001418 ASSERT(OldInfo != NULL);
1419
1420 //
1421 // Skip ones that failed to open...
1422 //
1423 if (OldInfo->Status != EFI_SUCCESS) {
1424 continue;
1425 }
jcarsey94b17fa2009-05-07 18:46:18 +00001426
1427 //
1428 // make sure the old list was valid
1429 //
jcarsey94b17fa2009-05-07 18:46:18 +00001430 ASSERT(OldInfo->Info != NULL);
1431 ASSERT(OldInfo->FullName != NULL);
1432 ASSERT(OldInfo->FileName != NULL);
1433
1434 //
1435 // allocate a new EFI_SHELL_FILE_INFO object
1436 //
1437 NewInfo = AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));
jcarseyc9d92df2010-02-03 15:37:54 +00001438 if (NewInfo == NULL) {
jcarsey7a95efd2011-10-13 16:08:18 +00001439 ShellCloseFileMetaArg((EFI_SHELL_FILE_INFO**)(&ListHead));
jcarseybeab0fc2011-10-10 17:26:25 +00001440 ListHead = NULL;
jcarseyc9d92df2010-02-03 15:37:54 +00001441 break;
1442 }
jcarsey1e6e84c2010-01-25 20:05:08 +00001443
1444 //
jcarsey94b17fa2009-05-07 18:46:18 +00001445 // copy the simple items
1446 //
1447 NewInfo->Handle = OldInfo->Handle;
1448 NewInfo->Status = OldInfo->Status;
1449
jcarseyd2b45642009-05-11 18:02:16 +00001450 // old shell checks for 0 not NULL
1451 OldInfo->Handle = 0;
1452
jcarsey94b17fa2009-05-07 18:46:18 +00001453 //
1454 // allocate new space to copy strings and structure
1455 //
Jaben Carsey98c16be2014-08-19 21:00:34 +00001456 NewInfo->FullName = AllocateCopyPool(StrSize(OldInfo->FullName), OldInfo->FullName);
1457 NewInfo->FileName = AllocateCopyPool(StrSize(OldInfo->FileName), OldInfo->FileName);
1458 NewInfo->Info = AllocateCopyPool((UINTN)OldInfo->Info->Size, OldInfo->Info);
jcarsey1e6e84c2010-01-25 20:05:08 +00001459
jcarsey94b17fa2009-05-07 18:46:18 +00001460 //
1461 // make sure all the memory allocations were sucessful
1462 //
jcarseybeab0fc2011-10-10 17:26:25 +00001463 if (NULL == NewInfo->FullName || NewInfo->FileName == NULL || NewInfo->Info == NULL) {
Jaben Carsey98c16be2014-08-19 21:00:34 +00001464 //
1465 // Free the partially allocated new node
1466 //
1467 SHELL_FREE_NON_NULL(NewInfo->FullName);
1468 SHELL_FREE_NON_NULL(NewInfo->FileName);
1469 SHELL_FREE_NON_NULL(NewInfo->Info);
1470 SHELL_FREE_NON_NULL(NewInfo);
1471
1472 //
1473 // Free the previously converted stuff
1474 //
jcarsey7a95efd2011-10-13 16:08:18 +00001475 ShellCloseFileMetaArg((EFI_SHELL_FILE_INFO**)(&ListHead));
jcarseybeab0fc2011-10-10 17:26:25 +00001476 ListHead = NULL;
1477 break;
1478 }
jcarsey94b17fa2009-05-07 18:46:18 +00001479
1480 //
jcarsey94b17fa2009-05-07 18:46:18 +00001481 // add that to the list
1482 //
jcarsey9b3bf082009-06-23 21:15:07 +00001483 InsertTailList(ListHead, &NewInfo->Link);
jcarsey94b17fa2009-05-07 18:46:18 +00001484 }
1485 return (ListHead);
1486}
1487/**
1488 Opens a group of files based on a path.
1489
jcarsey1e6e84c2010-01-25 20:05:08 +00001490 This function uses the Arg to open all the matching files. Each matched
Brendan Jackman70879312014-01-24 22:29:53 +00001491 file has a SHELL_FILE_INFO structure to record the file information. These
1492 structures are placed on the list ListHead. Users can get the SHELL_FILE_INFO
jcarsey94b17fa2009-05-07 18:46:18 +00001493 structures from ListHead to access each file. This function supports wildcards
jcarsey1e6e84c2010-01-25 20:05:08 +00001494 and will process '?' and '*' as such. the list must be freed with a call to
jcarsey94b17fa2009-05-07 18:46:18 +00001495 ShellCloseFileMetaArg().
1496
jcarsey1e6e84c2010-01-25 20:05:08 +00001497 If you are NOT appending to an existing list *ListHead must be NULL. If
jcarsey5f7431d2009-07-10 18:06:01 +00001498 *ListHead is NULL then it must be callee freed.
jcarsey94b17fa2009-05-07 18:46:18 +00001499
1500 @param Arg pointer to path string
1501 @param OpenMode mode to open files with
1502 @param ListHead head of linked list of results
1503
jcarsey1e6e84c2010-01-25 20:05:08 +00001504 @retval EFI_SUCCESS the operation was sucessful and the list head
jcarsey94b17fa2009-05-07 18:46:18 +00001505 contains the list of opened files
jcarsey94b17fa2009-05-07 18:46:18 +00001506 @return != EFI_SUCCESS the operation failed
1507
1508 @sa InternalShellConvertFileListType
1509**/
1510EFI_STATUS
1511EFIAPI
1512ShellOpenFileMetaArg (
1513 IN CHAR16 *Arg,
1514 IN UINT64 OpenMode,
1515 IN OUT EFI_SHELL_FILE_INFO **ListHead
1516 )
1517{
1518 EFI_STATUS Status;
jcarsey9b3bf082009-06-23 21:15:07 +00001519 LIST_ENTRY mOldStyleFileList;
Qiu Shumin0960ba12014-09-17 07:58:31 +00001520 CHAR16 *CleanFilePathStr;
jcarsey1e6e84c2010-01-25 20:05:08 +00001521
jcarsey94b17fa2009-05-07 18:46:18 +00001522 //
1523 // ASSERT that Arg and ListHead are not NULL
1524 //
1525 ASSERT(Arg != NULL);
1526 ASSERT(ListHead != NULL);
1527
Qiu Shumin715096c2014-09-19 01:32:05 +00001528 CleanFilePathStr = NULL;
1529
Qiu Shumin0960ba12014-09-17 07:58:31 +00001530 Status = InternalShellStripQuotes (Arg, &CleanFilePathStr);
1531 if (EFI_ERROR (Status)) {
1532 return Status;
1533 }
1534
jcarsey1e6e84c2010-01-25 20:05:08 +00001535 //
jcarsey94b17fa2009-05-07 18:46:18 +00001536 // Check for UEFI Shell 2.0 protocols
1537 //
jcarsey366f81a2011-06-27 21:04:22 +00001538 if (gEfiShellProtocol != NULL) {
jcarsey5f7431d2009-07-10 18:06:01 +00001539 if (*ListHead == NULL) {
1540 *ListHead = (EFI_SHELL_FILE_INFO*)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));
1541 if (*ListHead == NULL) {
Qiu Shumin0960ba12014-09-17 07:58:31 +00001542 FreePool(CleanFilePathStr);
jcarsey5f7431d2009-07-10 18:06:01 +00001543 return (EFI_OUT_OF_RESOURCES);
1544 }
1545 InitializeListHead(&((*ListHead)->Link));
jcarsey1e6e84c2010-01-25 20:05:08 +00001546 }
Qiu Shumin0960ba12014-09-17 07:58:31 +00001547 Status = gEfiShellProtocol->OpenFileList(CleanFilePathStr,
jcarsey1e6e84c2010-01-25 20:05:08 +00001548 OpenMode,
jcarsey2247dde2009-11-09 18:08:58 +00001549 ListHead);
1550 if (EFI_ERROR(Status)) {
jcarsey366f81a2011-06-27 21:04:22 +00001551 gEfiShellProtocol->RemoveDupInFileList(ListHead);
jcarsey2247dde2009-11-09 18:08:58 +00001552 } else {
jcarsey366f81a2011-06-27 21:04:22 +00001553 Status = gEfiShellProtocol->RemoveDupInFileList(ListHead);
jcarsey2247dde2009-11-09 18:08:58 +00001554 }
jcarseya405b862010-09-14 05:18:09 +00001555 if (*ListHead != NULL && IsListEmpty(&(*ListHead)->Link)) {
1556 FreePool(*ListHead);
Qiu Shumin0960ba12014-09-17 07:58:31 +00001557 FreePool(CleanFilePathStr);
jcarseya405b862010-09-14 05:18:09 +00001558 *ListHead = NULL;
1559 return (EFI_NOT_FOUND);
1560 }
Qiu Shumin0960ba12014-09-17 07:58:31 +00001561 FreePool(CleanFilePathStr);
jcarsey2247dde2009-11-09 18:08:58 +00001562 return (Status);
jcarsey1e6e84c2010-01-25 20:05:08 +00001563 }
jcarsey94b17fa2009-05-07 18:46:18 +00001564
1565 //
jcarsey92a54472011-06-27 20:33:13 +00001566 // Check for EFI shell
jcarsey94b17fa2009-05-07 18:46:18 +00001567 //
jcarsey92a54472011-06-27 20:33:13 +00001568 if (mEfiShellEnvironment2 != NULL) {
1569 //
1570 // make sure the list head is initialized
1571 //
1572 InitializeListHead(&mOldStyleFileList);
jcarsey94b17fa2009-05-07 18:46:18 +00001573
jcarsey92a54472011-06-27 20:33:13 +00001574 //
1575 // Get the EFI Shell list of files
1576 //
Qiu Shumin0960ba12014-09-17 07:58:31 +00001577 Status = mEfiShellEnvironment2->FileMetaArg(CleanFilePathStr, &mOldStyleFileList);
jcarsey92a54472011-06-27 20:33:13 +00001578 if (EFI_ERROR(Status)) {
1579 *ListHead = NULL;
Qiu Shumin0960ba12014-09-17 07:58:31 +00001580 FreePool(CleanFilePathStr);
jcarsey92a54472011-06-27 20:33:13 +00001581 return (Status);
1582 }
jcarsey94b17fa2009-05-07 18:46:18 +00001583
jcarsey92a54472011-06-27 20:33:13 +00001584 if (*ListHead == NULL) {
1585 *ListHead = (EFI_SHELL_FILE_INFO *)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));
1586 if (*ListHead == NULL) {
Qiu Shumin0960ba12014-09-17 07:58:31 +00001587 FreePool(CleanFilePathStr);
jcarsey92a54472011-06-27 20:33:13 +00001588 return (EFI_OUT_OF_RESOURCES);
1589 }
1590 InitializeListHead(&((*ListHead)->Link));
1591 }
1592
1593 //
1594 // Convert that to equivalent of UEFI Shell 2.0 structure
1595 //
1596 InternalShellConvertFileListType(&mOldStyleFileList, &(*ListHead)->Link);
1597
1598 //
1599 // Free the EFI Shell version that was converted.
1600 //
1601 mEfiShellEnvironment2->FreeFileList(&mOldStyleFileList);
1602
1603 if ((*ListHead)->Link.ForwardLink == (*ListHead)->Link.BackLink && (*ListHead)->Link.BackLink == &((*ListHead)->Link)) {
1604 FreePool(*ListHead);
1605 *ListHead = NULL;
1606 Status = EFI_NOT_FOUND;
1607 }
Qiu Shumin0960ba12014-09-17 07:58:31 +00001608 FreePool(CleanFilePathStr);
jcarsey94b17fa2009-05-07 18:46:18 +00001609 return (Status);
1610 }
1611
Qiu Shumin0960ba12014-09-17 07:58:31 +00001612 FreePool(CleanFilePathStr);
jcarsey92a54472011-06-27 20:33:13 +00001613 return (EFI_UNSUPPORTED);
jcarsey94b17fa2009-05-07 18:46:18 +00001614}
1615/**
jcarseya405b862010-09-14 05:18:09 +00001616 Free the linked list returned from ShellOpenFileMetaArg.
jcarsey94b17fa2009-05-07 18:46:18 +00001617
jcarseya405b862010-09-14 05:18:09 +00001618 if ListHead is NULL then ASSERT().
jcarsey94b17fa2009-05-07 18:46:18 +00001619
jcarseya405b862010-09-14 05:18:09 +00001620 @param ListHead the pointer to free.
jcarsey94b17fa2009-05-07 18:46:18 +00001621
jcarseya405b862010-09-14 05:18:09 +00001622 @retval EFI_SUCCESS the operation was sucessful.
jcarsey94b17fa2009-05-07 18:46:18 +00001623**/
1624EFI_STATUS
1625EFIAPI
1626ShellCloseFileMetaArg (
1627 IN OUT EFI_SHELL_FILE_INFO **ListHead
1628 )
1629{
1630 LIST_ENTRY *Node;
1631
1632 //
1633 // ASSERT that ListHead is not NULL
1634 //
1635 ASSERT(ListHead != NULL);
1636
jcarsey1e6e84c2010-01-25 20:05:08 +00001637 //
jcarsey94b17fa2009-05-07 18:46:18 +00001638 // Check for UEFI Shell 2.0 protocols
1639 //
jcarsey366f81a2011-06-27 21:04:22 +00001640 if (gEfiShellProtocol != NULL) {
1641 return (gEfiShellProtocol->FreeFileList(ListHead));
jcarsey92a54472011-06-27 20:33:13 +00001642 } else if (mEfiShellEnvironment2 != NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00001643 //
jcarsey1e6e84c2010-01-25 20:05:08 +00001644 // Since this is EFI Shell version we need to free our internally made copy
jcarsey94b17fa2009-05-07 18:46:18 +00001645 // of the list
1646 //
jcarsey1e6e84c2010-01-25 20:05:08 +00001647 for ( Node = GetFirstNode(&(*ListHead)->Link)
jcarseya405b862010-09-14 05:18:09 +00001648 ; *ListHead != NULL && !IsListEmpty(&(*ListHead)->Link)
jcarsey9b3bf082009-06-23 21:15:07 +00001649 ; Node = GetFirstNode(&(*ListHead)->Link)) {
jcarsey94b17fa2009-05-07 18:46:18 +00001650 RemoveEntryList(Node);
jcarseya405b862010-09-14 05:18:09 +00001651 ((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 +00001652 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->FullName);
1653 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->FileName);
1654 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->Info);
1655 FreePool((EFI_SHELL_FILE_INFO_NO_CONST*)Node);
1656 }
Jaben Carsey75a5e2e2014-01-10 16:42:45 +00001657 SHELL_FREE_NON_NULL(*ListHead);
jcarsey94b17fa2009-05-07 18:46:18 +00001658 return EFI_SUCCESS;
1659 }
jcarsey92a54472011-06-27 20:33:13 +00001660
1661 return (EFI_UNSUPPORTED);
jcarsey94b17fa2009-05-07 18:46:18 +00001662}
1663
jcarsey125c2cf2009-11-18 21:36:50 +00001664/**
1665 Find a file by searching the CWD and then the path.
1666
jcarseyb3011f42010-01-11 21:49:04 +00001667 If FileName is NULL then ASSERT.
jcarsey125c2cf2009-11-18 21:36:50 +00001668
jcarseyb3011f42010-01-11 21:49:04 +00001669 If the return value is not NULL then the memory must be caller freed.
jcarsey125c2cf2009-11-18 21:36:50 +00001670
1671 @param FileName Filename string.
1672
1673 @retval NULL the file was not found
1674 @return !NULL the full path to the file.
1675**/
1676CHAR16 *
1677EFIAPI
1678ShellFindFilePath (
1679 IN CONST CHAR16 *FileName
1680 )
1681{
1682 CONST CHAR16 *Path;
jcarseya405b862010-09-14 05:18:09 +00001683 SHELL_FILE_HANDLE Handle;
jcarsey125c2cf2009-11-18 21:36:50 +00001684 EFI_STATUS Status;
1685 CHAR16 *RetVal;
1686 CHAR16 *TestPath;
1687 CONST CHAR16 *Walker;
jcarsey36a9d672009-11-20 21:13:41 +00001688 UINTN Size;
jcarsey1cd45e72010-01-29 15:07:44 +00001689 CHAR16 *TempChar;
jcarsey125c2cf2009-11-18 21:36:50 +00001690
1691 RetVal = NULL;
1692
jcarseya405b862010-09-14 05:18:09 +00001693 //
1694 // First make sure its not an absolute path.
1695 //
1696 Status = ShellOpenFileByName(FileName, &Handle, EFI_FILE_MODE_READ, 0);
1697 if (!EFI_ERROR(Status)){
1698 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {
1699 ASSERT(RetVal == NULL);
1700 RetVal = StrnCatGrow(&RetVal, NULL, FileName, 0);
1701 ShellCloseFile(&Handle);
1702 return (RetVal);
1703 } else {
1704 ShellCloseFile(&Handle);
1705 }
1706 }
1707
jcarsey125c2cf2009-11-18 21:36:50 +00001708 Path = ShellGetEnvironmentVariable(L"cwd");
1709 if (Path != NULL) {
jcarsey36a9d672009-11-20 21:13:41 +00001710 Size = StrSize(Path);
1711 Size += StrSize(FileName);
1712 TestPath = AllocateZeroPool(Size);
jcarseyc9d92df2010-02-03 15:37:54 +00001713 if (TestPath == NULL) {
1714 return (NULL);
1715 }
Jaben Carsey98c16be2014-08-19 21:00:34 +00001716 StrnCpy(TestPath, Path, Size/sizeof(CHAR16) - 1);
1717 StrnCat(TestPath, FileName, Size/sizeof(CHAR16) - 1 - StrLen(TestPath));
jcarsey125c2cf2009-11-18 21:36:50 +00001718 Status = ShellOpenFileByName(TestPath, &Handle, EFI_FILE_MODE_READ, 0);
1719 if (!EFI_ERROR(Status)){
jcarseya405b862010-09-14 05:18:09 +00001720 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {
1721 ASSERT(RetVal == NULL);
1722 RetVal = StrnCatGrow(&RetVal, NULL, TestPath, 0);
1723 ShellCloseFile(&Handle);
1724 FreePool(TestPath);
1725 return (RetVal);
1726 } else {
1727 ShellCloseFile(&Handle);
1728 }
jcarsey125c2cf2009-11-18 21:36:50 +00001729 }
1730 FreePool(TestPath);
1731 }
1732 Path = ShellGetEnvironmentVariable(L"path");
1733 if (Path != NULL) {
jcarseya405b862010-09-14 05:18:09 +00001734 Size = StrSize(Path)+sizeof(CHAR16);
jcarsey36a9d672009-11-20 21:13:41 +00001735 Size += StrSize(FileName);
1736 TestPath = AllocateZeroPool(Size);
jcarsey3e082d52010-10-04 16:44:57 +00001737 if (TestPath == NULL) {
1738 return (NULL);
1739 }
jcarsey1e6e84c2010-01-25 20:05:08 +00001740 Walker = (CHAR16*)Path;
jcarsey125c2cf2009-11-18 21:36:50 +00001741 do {
1742 CopyMem(TestPath, Walker, StrSize(Walker));
jcarsey3e082d52010-10-04 16:44:57 +00001743 if (TestPath != NULL) {
1744 TempChar = StrStr(TestPath, L";");
1745 if (TempChar != NULL) {
1746 *TempChar = CHAR_NULL;
1747 }
1748 if (TestPath[StrLen(TestPath)-1] != L'\\') {
Jaben Carsey98c16be2014-08-19 21:00:34 +00001749 StrnCat(TestPath, L"\\", Size/sizeof(CHAR16) - 1 - StrLen(TestPath));
jcarsey3e082d52010-10-04 16:44:57 +00001750 }
jcarsey89e85372011-04-13 23:37:50 +00001751 if (FileName[0] == L'\\') {
1752 FileName++;
1753 }
Jaben Carsey98c16be2014-08-19 21:00:34 +00001754 StrnCat(TestPath, FileName, Size/sizeof(CHAR16) - 1 - StrLen(TestPath));
jcarsey3e082d52010-10-04 16:44:57 +00001755 if (StrStr(Walker, L";") != NULL) {
1756 Walker = StrStr(Walker, L";") + 1;
jcarseya405b862010-09-14 05:18:09 +00001757 } else {
jcarsey3e082d52010-10-04 16:44:57 +00001758 Walker = NULL;
1759 }
1760 Status = ShellOpenFileByName(TestPath, &Handle, EFI_FILE_MODE_READ, 0);
1761 if (!EFI_ERROR(Status)){
1762 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {
1763 ASSERT(RetVal == NULL);
1764 RetVal = StrnCatGrow(&RetVal, NULL, TestPath, 0);
1765 ShellCloseFile(&Handle);
1766 break;
1767 } else {
1768 ShellCloseFile(&Handle);
1769 }
jcarseya405b862010-09-14 05:18:09 +00001770 }
jcarsey125c2cf2009-11-18 21:36:50 +00001771 }
1772 } while (Walker != NULL && Walker[0] != CHAR_NULL);
1773 FreePool(TestPath);
1774 }
1775 return (RetVal);
1776}
1777
jcarseyb3011f42010-01-11 21:49:04 +00001778/**
jcarsey1e6e84c2010-01-25 20:05:08 +00001779 Find a file by searching the CWD and then the path with a variable set of file
1780 extensions. If the file is not found it will append each extension in the list
jcarseyb3011f42010-01-11 21:49:04 +00001781 in the order provided and return the first one that is successful.
1782
1783 If FileName is NULL, then ASSERT.
1784 If FileExtension is NULL, then behavior is identical to ShellFindFilePath.
1785
1786 If the return value is not NULL then the memory must be caller freed.
1787
1788 @param[in] FileName Filename string.
1789 @param[in] FileExtension Semi-colon delimeted list of possible extensions.
1790
1791 @retval NULL The file was not found.
1792 @retval !NULL The path to the file.
1793**/
1794CHAR16 *
1795EFIAPI
1796ShellFindFilePathEx (
1797 IN CONST CHAR16 *FileName,
1798 IN CONST CHAR16 *FileExtension
1799 )
1800{
1801 CHAR16 *TestPath;
1802 CHAR16 *RetVal;
1803 CONST CHAR16 *ExtensionWalker;
jcarsey9e926b62010-01-14 20:26:39 +00001804 UINTN Size;
jcarsey1cd45e72010-01-29 15:07:44 +00001805 CHAR16 *TempChar;
jcarseyc9d92df2010-02-03 15:37:54 +00001806 CHAR16 *TempChar2;
jcarsey1cd45e72010-01-29 15:07:44 +00001807
jcarseyb3011f42010-01-11 21:49:04 +00001808 ASSERT(FileName != NULL);
1809 if (FileExtension == NULL) {
1810 return (ShellFindFilePath(FileName));
1811 }
1812 RetVal = ShellFindFilePath(FileName);
1813 if (RetVal != NULL) {
1814 return (RetVal);
1815 }
jcarsey9e926b62010-01-14 20:26:39 +00001816 Size = StrSize(FileName);
1817 Size += StrSize(FileExtension);
1818 TestPath = AllocateZeroPool(Size);
jcarseyc9d92df2010-02-03 15:37:54 +00001819 if (TestPath == NULL) {
1820 return (NULL);
1821 }
jcarseya405b862010-09-14 05:18:09 +00001822 for (ExtensionWalker = FileExtension, TempChar2 = (CHAR16*)FileExtension; TempChar2 != NULL ; ExtensionWalker = TempChar2 + 1){
Jaben Carsey98c16be2014-08-19 21:00:34 +00001823 StrnCpy(TestPath, FileName, Size/sizeof(CHAR16) - 1);
jcarseya405b862010-09-14 05:18:09 +00001824 if (ExtensionWalker != NULL) {
Jaben Carsey98c16be2014-08-19 21:00:34 +00001825 StrnCat(TestPath, ExtensionWalker, Size/sizeof(CHAR16) - 1 - StrLen(TestPath));
jcarseya405b862010-09-14 05:18:09 +00001826 }
jcarsey1cd45e72010-01-29 15:07:44 +00001827 TempChar = StrStr(TestPath, L";");
1828 if (TempChar != NULL) {
1829 *TempChar = CHAR_NULL;
jcarseyb3011f42010-01-11 21:49:04 +00001830 }
1831 RetVal = ShellFindFilePath(TestPath);
1832 if (RetVal != NULL) {
1833 break;
1834 }
jcarseya405b862010-09-14 05:18:09 +00001835 ASSERT(ExtensionWalker != NULL);
jcarseyc9d92df2010-02-03 15:37:54 +00001836 TempChar2 = StrStr(ExtensionWalker, L";");
jcarseyb3011f42010-01-11 21:49:04 +00001837 }
1838 FreePool(TestPath);
1839 return (RetVal);
1840}
1841
jcarsey94b17fa2009-05-07 18:46:18 +00001842typedef struct {
jcarsey9b3bf082009-06-23 21:15:07 +00001843 LIST_ENTRY Link;
jcarsey94b17fa2009-05-07 18:46:18 +00001844 CHAR16 *Name;
jcarseya405b862010-09-14 05:18:09 +00001845 SHELL_PARAM_TYPE Type;
jcarsey94b17fa2009-05-07 18:46:18 +00001846 CHAR16 *Value;
1847 UINTN OriginalPosition;
1848} SHELL_PARAM_PACKAGE;
1849
1850/**
jcarsey1e6e84c2010-01-25 20:05:08 +00001851 Checks the list of valid arguments and returns TRUE if the item was found. If the
jcarsey94b17fa2009-05-07 18:46:18 +00001852 return value is TRUE then the type parameter is set also.
jcarsey1e6e84c2010-01-25 20:05:08 +00001853
jcarsey94b17fa2009-05-07 18:46:18 +00001854 if CheckList is NULL then ASSERT();
1855 if Name is NULL then ASSERT();
1856 if Type is NULL then ASSERT();
1857
jcarsey94b17fa2009-05-07 18:46:18 +00001858 @param Name pointer to Name of parameter found
1859 @param CheckList List to check against
jcarseya405b862010-09-14 05:18:09 +00001860 @param Type pointer to type of parameter if it was found
jcarsey94b17fa2009-05-07 18:46:18 +00001861
1862 @retval TRUE the Parameter was found. Type is valid.
1863 @retval FALSE the Parameter was not found. Type is not valid.
1864**/
1865BOOLEAN
1866EFIAPI
jcarseyd2b45642009-05-11 18:02:16 +00001867InternalIsOnCheckList (
jcarsey94b17fa2009-05-07 18:46:18 +00001868 IN CONST CHAR16 *Name,
1869 IN CONST SHELL_PARAM_ITEM *CheckList,
jcarsey252d9452011-03-25 20:49:53 +00001870 OUT SHELL_PARAM_TYPE *Type
jcarseya405b862010-09-14 05:18:09 +00001871 )
1872{
jcarsey94b17fa2009-05-07 18:46:18 +00001873 SHELL_PARAM_ITEM *TempListItem;
jcarsey252d9452011-03-25 20:49:53 +00001874 CHAR16 *TempString;
jcarsey94b17fa2009-05-07 18:46:18 +00001875
1876 //
1877 // ASSERT that all 3 pointer parameters aren't NULL
1878 //
1879 ASSERT(CheckList != NULL);
1880 ASSERT(Type != NULL);
1881 ASSERT(Name != NULL);
1882
1883 //
jcarseyd2b45642009-05-11 18:02:16 +00001884 // question mark and page break mode are always supported
1885 //
1886 if ((StrCmp(Name, L"-?") == 0) ||
1887 (StrCmp(Name, L"-b") == 0)
jcarseya405b862010-09-14 05:18:09 +00001888 ) {
jcarsey252d9452011-03-25 20:49:53 +00001889 *Type = TypeFlag;
jcarseyd2b45642009-05-11 18:02:16 +00001890 return (TRUE);
1891 }
1892
1893 //
jcarsey94b17fa2009-05-07 18:46:18 +00001894 // Enumerate through the list
1895 //
1896 for (TempListItem = (SHELL_PARAM_ITEM*)CheckList ; TempListItem->Name != NULL ; TempListItem++) {
1897 //
jcarsey9eb53ac2009-07-08 17:26:58 +00001898 // If the Type is TypeStart only check the first characters of the passed in param
1899 // If it matches set the type and return TRUE
jcarsey94b17fa2009-05-07 18:46:18 +00001900 //
darylm503b0934ac2011-11-12 00:35:11 +00001901 if (TempListItem->Type == TypeStart) {
jcarsey252d9452011-03-25 20:49:53 +00001902 if (StrnCmp(Name, TempListItem->Name, StrLen(TempListItem->Name)) == 0) {
1903 *Type = TempListItem->Type;
1904 return (TRUE);
1905 }
1906 TempString = NULL;
1907 TempString = StrnCatGrow(&TempString, NULL, Name, StrLen(TempListItem->Name));
1908 if (TempString != NULL) {
1909 if (StringNoCaseCompare(&TempString, &TempListItem->Name) == 0) {
1910 *Type = TempListItem->Type;
1911 FreePool(TempString);
1912 return (TRUE);
1913 }
1914 FreePool(TempString);
1915 }
1916 } else if (StringNoCaseCompare(&Name, &TempListItem->Name) == 0) {
jcarsey94b17fa2009-05-07 18:46:18 +00001917 *Type = TempListItem->Type;
1918 return (TRUE);
1919 }
1920 }
jcarsey2247dde2009-11-09 18:08:58 +00001921
jcarsey94b17fa2009-05-07 18:46:18 +00001922 return (FALSE);
1923}
1924/**
jcarseyd2b45642009-05-11 18:02:16 +00001925 Checks the string for indicators of "flag" status. this is a leading '/', '-', or '+'
jcarsey94b17fa2009-05-07 18:46:18 +00001926
jcarseya405b862010-09-14 05:18:09 +00001927 @param[in] Name pointer to Name of parameter found
1928 @param[in] AlwaysAllowNumbers TRUE to allow numbers, FALSE to not.
jcarsey658bf432014-11-04 22:33:16 +00001929 @param[in] TimeNumbers TRUE to allow numbers with ":", FALSE otherwise.
jcarsey94b17fa2009-05-07 18:46:18 +00001930
1931 @retval TRUE the Parameter is a flag.
jcarseya405b862010-09-14 05:18:09 +00001932 @retval FALSE the Parameter not a flag.
jcarsey94b17fa2009-05-07 18:46:18 +00001933**/
1934BOOLEAN
1935EFIAPI
jcarseyd2b45642009-05-11 18:02:16 +00001936InternalIsFlag (
jcarsey2247dde2009-11-09 18:08:58 +00001937 IN CONST CHAR16 *Name,
jcarsey658bf432014-11-04 22:33:16 +00001938 IN CONST BOOLEAN AlwaysAllowNumbers,
1939 IN CONST BOOLEAN TimeNumbers
jcarsey94b17fa2009-05-07 18:46:18 +00001940 )
1941{
1942 //
1943 // ASSERT that Name isn't NULL
1944 //
1945 ASSERT(Name != NULL);
1946
1947 //
jcarsey2247dde2009-11-09 18:08:58 +00001948 // If we accept numbers then dont return TRUE. (they will be values)
1949 //
jcarsey658bf432014-11-04 22:33:16 +00001950 if (((Name[0] == L'-' || Name[0] == L'+') && InternalShellIsHexOrDecimalNumber(Name+1, FALSE, FALSE, TimeNumbers)) && AlwaysAllowNumbers) {
jcarsey2247dde2009-11-09 18:08:58 +00001951 return (FALSE);
1952 }
1953
1954 //
jcarseya405b862010-09-14 05:18:09 +00001955 // If the Name has a /, +, or - as the first character return TRUE
jcarsey94b17fa2009-05-07 18:46:18 +00001956 //
jcarsey1e6e84c2010-01-25 20:05:08 +00001957 if ((Name[0] == L'/') ||
jcarseyd2b45642009-05-11 18:02:16 +00001958 (Name[0] == L'-') ||
1959 (Name[0] == L'+')
jcarseya405b862010-09-14 05:18:09 +00001960 ) {
jcarsey94b17fa2009-05-07 18:46:18 +00001961 return (TRUE);
1962 }
1963 return (FALSE);
1964}
1965
1966/**
jcarsey1e6e84c2010-01-25 20:05:08 +00001967 Checks the command line arguments passed against the list of valid ones.
jcarsey94b17fa2009-05-07 18:46:18 +00001968
1969 If no initialization is required, then return RETURN_SUCCESS.
jcarsey1e6e84c2010-01-25 20:05:08 +00001970
jcarseya405b862010-09-14 05:18:09 +00001971 @param[in] CheckList pointer to list of parameters to check
1972 @param[out] CheckPackage pointer to pointer to list checked values
1973 @param[out] ProblemParam optional pointer to pointer to unicode string for
jcarseyd2b45642009-05-11 18:02:16 +00001974 the paramater that caused failure. If used then the
1975 caller is responsible for freeing the memory.
jcarseya405b862010-09-14 05:18:09 +00001976 @param[in] AutoPageBreak will automatically set PageBreakEnabled for "b" parameter
1977 @param[in] Argv pointer to array of parameters
1978 @param[in] Argc Count of parameters in Argv
1979 @param[in] AlwaysAllowNumbers TRUE to allow numbers always, FALSE otherwise.
jcarsey94b17fa2009-05-07 18:46:18 +00001980
1981 @retval EFI_SUCCESS The operation completed sucessfully.
1982 @retval EFI_OUT_OF_RESOURCES A memory allocation failed
1983 @retval EFI_INVALID_PARAMETER A parameter was invalid
jcarsey1e6e84c2010-01-25 20:05:08 +00001984 @retval EFI_VOLUME_CORRUPTED the command line was corrupt. an argument was
1985 duplicated. the duplicated command line argument
jcarsey94b17fa2009-05-07 18:46:18 +00001986 was returned in ProblemParam if provided.
jcarsey1e6e84c2010-01-25 20:05:08 +00001987 @retval EFI_NOT_FOUND a argument required a value that was missing.
jcarsey94b17fa2009-05-07 18:46:18 +00001988 the invalid command line argument was returned in
1989 ProblemParam if provided.
1990**/
1991EFI_STATUS
1992EFIAPI
1993InternalCommandLineParse (
1994 IN CONST SHELL_PARAM_ITEM *CheckList,
1995 OUT LIST_ENTRY **CheckPackage,
1996 OUT CHAR16 **ProblemParam OPTIONAL,
1997 IN BOOLEAN AutoPageBreak,
1998 IN CONST CHAR16 **Argv,
jcarsey2247dde2009-11-09 18:08:58 +00001999 IN UINTN Argc,
2000 IN BOOLEAN AlwaysAllowNumbers
jcarseya405b862010-09-14 05:18:09 +00002001 )
2002{
jcarsey94b17fa2009-05-07 18:46:18 +00002003 UINTN LoopCounter;
jcarsey252d9452011-03-25 20:49:53 +00002004 SHELL_PARAM_TYPE CurrentItemType;
jcarsey94b17fa2009-05-07 18:46:18 +00002005 SHELL_PARAM_PACKAGE *CurrentItemPackage;
jcarsey125c2cf2009-11-18 21:36:50 +00002006 UINTN GetItemValue;
2007 UINTN ValueSize;
jcarseya405b862010-09-14 05:18:09 +00002008 UINTN Count;
jcarsey252d9452011-03-25 20:49:53 +00002009 CONST CHAR16 *TempPointer;
Jaben Carsey98c16be2014-08-19 21:00:34 +00002010 UINTN CurrentValueSize;
jcarsey94b17fa2009-05-07 18:46:18 +00002011
2012 CurrentItemPackage = NULL;
jcarsey125c2cf2009-11-18 21:36:50 +00002013 GetItemValue = 0;
2014 ValueSize = 0;
jcarseya405b862010-09-14 05:18:09 +00002015 Count = 0;
jcarsey94b17fa2009-05-07 18:46:18 +00002016
2017 //
2018 // If there is only 1 item we dont need to do anything
2019 //
jcarseya405b862010-09-14 05:18:09 +00002020 if (Argc < 1) {
jcarsey94b17fa2009-05-07 18:46:18 +00002021 *CheckPackage = NULL;
2022 return (EFI_SUCCESS);
2023 }
2024
2025 //
jcarsey2247dde2009-11-09 18:08:58 +00002026 // ASSERTs
2027 //
2028 ASSERT(CheckList != NULL);
2029 ASSERT(Argv != NULL);
2030
2031 //
jcarsey94b17fa2009-05-07 18:46:18 +00002032 // initialize the linked list
2033 //
2034 *CheckPackage = (LIST_ENTRY*)AllocateZeroPool(sizeof(LIST_ENTRY));
sfu502a758c2011-10-08 02:55:30 +00002035 if (*CheckPackage == NULL) {
jcarseybeab0fc2011-10-10 17:26:25 +00002036 return (EFI_OUT_OF_RESOURCES);
sfu502a758c2011-10-08 02:55:30 +00002037 }
jcarseybeab0fc2011-10-10 17:26:25 +00002038
jcarsey94b17fa2009-05-07 18:46:18 +00002039 InitializeListHead(*CheckPackage);
2040
2041 //
2042 // loop through each of the arguments
2043 //
2044 for (LoopCounter = 0 ; LoopCounter < Argc ; ++LoopCounter) {
2045 if (Argv[LoopCounter] == NULL) {
2046 //
2047 // do nothing for NULL argv
2048 //
jcarseya405b862010-09-14 05:18:09 +00002049 } else if (InternalIsOnCheckList(Argv[LoopCounter], CheckList, &CurrentItemType)) {
jcarsey94b17fa2009-05-07 18:46:18 +00002050 //
jcarsey2247dde2009-11-09 18:08:58 +00002051 // We might have leftover if last parameter didnt have optional value
2052 //
jcarsey125c2cf2009-11-18 21:36:50 +00002053 if (GetItemValue != 0) {
2054 GetItemValue = 0;
jcarsey2247dde2009-11-09 18:08:58 +00002055 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
2056 }
2057 //
jcarsey94b17fa2009-05-07 18:46:18 +00002058 // this is a flag
2059 //
jcarsey252d9452011-03-25 20:49:53 +00002060 CurrentItemPackage = AllocateZeroPool(sizeof(SHELL_PARAM_PACKAGE));
jcarseybeab0fc2011-10-10 17:26:25 +00002061 if (CurrentItemPackage == NULL) {
2062 ShellCommandLineFreeVarList(*CheckPackage);
2063 *CheckPackage = NULL;
2064 return (EFI_OUT_OF_RESOURCES);
2065 }
Jaben Carsey98c16be2014-08-19 21:00:34 +00002066 CurrentItemPackage->Name = AllocateCopyPool(StrSize(Argv[LoopCounter]), Argv[LoopCounter]);
jcarseybeab0fc2011-10-10 17:26:25 +00002067 if (CurrentItemPackage->Name == NULL) {
2068 ShellCommandLineFreeVarList(*CheckPackage);
2069 *CheckPackage = NULL;
2070 return (EFI_OUT_OF_RESOURCES);
2071 }
jcarsey94b17fa2009-05-07 18:46:18 +00002072 CurrentItemPackage->Type = CurrentItemType;
2073 CurrentItemPackage->OriginalPosition = (UINTN)(-1);
jcarseyb1f95a02009-06-16 00:23:19 +00002074 CurrentItemPackage->Value = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +00002075
2076 //
2077 // Does this flag require a value
2078 //
jcarsey125c2cf2009-11-18 21:36:50 +00002079 switch (CurrentItemPackage->Type) {
jcarsey94b17fa2009-05-07 18:46:18 +00002080 //
jcarsey125c2cf2009-11-18 21:36:50 +00002081 // possibly trigger the next loop(s) to populate the value of this item
jcarsey1e6e84c2010-01-25 20:05:08 +00002082 //
jcarsey125c2cf2009-11-18 21:36:50 +00002083 case TypeValue:
jcarsey658bf432014-11-04 22:33:16 +00002084 case TypeTimeValue:
jcarsey1e6e84c2010-01-25 20:05:08 +00002085 GetItemValue = 1;
jcarsey125c2cf2009-11-18 21:36:50 +00002086 ValueSize = 0;
2087 break;
2088 case TypeDoubleValue:
2089 GetItemValue = 2;
2090 ValueSize = 0;
2091 break;
2092 case TypeMaxValue:
2093 GetItemValue = (UINTN)(-1);
2094 ValueSize = 0;
2095 break;
2096 default:
2097 //
2098 // this item has no value expected; we are done
2099 //
2100 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
2101 ASSERT(GetItemValue == 0);
2102 break;
jcarsey94b17fa2009-05-07 18:46:18 +00002103 }
Qiu Shuminbab9f942014-11-20 01:31:36 +00002104 } else if (GetItemValue != 0 && CurrentItemPackage != NULL && !InternalIsFlag(Argv[LoopCounter], AlwaysAllowNumbers, (CONST BOOLEAN)(CurrentItemPackage->Type == TypeTimeValue))) {
jcarseyb1f95a02009-06-16 00:23:19 +00002105 //
jcarsey125c2cf2009-11-18 21:36:50 +00002106 // get the item VALUE for a previous flag
jcarseyb1f95a02009-06-16 00:23:19 +00002107 //
jcarsey49bd4982012-01-27 18:42:43 +00002108 if (StrStr(Argv[LoopCounter], L" ") == NULL) {
Jaben Carsey98c16be2014-08-19 21:00:34 +00002109 CurrentValueSize = ValueSize + StrSize(Argv[LoopCounter]) + sizeof(CHAR16);
2110 CurrentItemPackage->Value = ReallocatePool(ValueSize, CurrentValueSize, CurrentItemPackage->Value);
jcarsey49bd4982012-01-27 18:42:43 +00002111 ASSERT(CurrentItemPackage->Value != NULL);
2112 if (ValueSize == 0) {
Jaben Carsey98c16be2014-08-19 21:00:34 +00002113 StrnCpy(CurrentItemPackage->Value, Argv[LoopCounter], CurrentValueSize/sizeof(CHAR16) - 1);
jcarsey49bd4982012-01-27 18:42:43 +00002114 } else {
Jaben Carsey98c16be2014-08-19 21:00:34 +00002115 StrnCat(CurrentItemPackage->Value, L" ", CurrentValueSize/sizeof(CHAR16) - 1 - StrLen(CurrentItemPackage->Value));
2116 StrnCat(CurrentItemPackage->Value, Argv[LoopCounter], CurrentValueSize/sizeof(CHAR16) - 1 - StrLen(CurrentItemPackage->Value));
jcarsey49bd4982012-01-27 18:42:43 +00002117 }
2118 ValueSize += StrSize(Argv[LoopCounter]) + sizeof(CHAR16);
jcarsey125c2cf2009-11-18 21:36:50 +00002119 } else {
jcarsey49bd4982012-01-27 18:42:43 +00002120 //
2121 // the parameter has spaces. must be quoted.
2122 //
Jaben Carsey98c16be2014-08-19 21:00:34 +00002123 CurrentValueSize = ValueSize + StrSize(Argv[LoopCounter]) + sizeof(CHAR16) + sizeof(CHAR16) + sizeof(CHAR16);
2124 CurrentItemPackage->Value = ReallocatePool(ValueSize, CurrentValueSize, CurrentItemPackage->Value);
jcarsey49bd4982012-01-27 18:42:43 +00002125 ASSERT(CurrentItemPackage->Value != NULL);
2126 if (ValueSize == 0) {
Jaben Carsey98c16be2014-08-19 21:00:34 +00002127 StrnCpy(CurrentItemPackage->Value, L"\"", CurrentValueSize/sizeof(CHAR16) - 1);
2128 StrnCat(CurrentItemPackage->Value, Argv[LoopCounter], CurrentValueSize/sizeof(CHAR16) - 1 - StrLen(CurrentItemPackage->Value));
2129 StrnCat(CurrentItemPackage->Value, L"\"", CurrentValueSize/sizeof(CHAR16) - 1 - StrLen(CurrentItemPackage->Value));
jcarsey49bd4982012-01-27 18:42:43 +00002130 } else {
Jaben Carsey98c16be2014-08-19 21:00:34 +00002131 StrnCat(CurrentItemPackage->Value, L" ", CurrentValueSize/sizeof(CHAR16) - 1 - StrLen(CurrentItemPackage->Value));
2132 StrnCat(CurrentItemPackage->Value, L"\"", CurrentValueSize/sizeof(CHAR16) - 1 - StrLen(CurrentItemPackage->Value));
2133 StrnCat(CurrentItemPackage->Value, Argv[LoopCounter], CurrentValueSize/sizeof(CHAR16) - 1 - StrLen(CurrentItemPackage->Value));
2134 StrnCat(CurrentItemPackage->Value, L"\"", CurrentValueSize/sizeof(CHAR16) - 1 - StrLen(CurrentItemPackage->Value));
jcarsey49bd4982012-01-27 18:42:43 +00002135 }
2136 ValueSize += StrSize(Argv[LoopCounter]) + sizeof(CHAR16);
jcarsey125c2cf2009-11-18 21:36:50 +00002137 }
jcarsey125c2cf2009-11-18 21:36:50 +00002138 GetItemValue--;
2139 if (GetItemValue == 0) {
2140 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
2141 }
jcarsey658bf432014-11-04 22:33:16 +00002142 } else if (!InternalIsFlag(Argv[LoopCounter], AlwaysAllowNumbers, FALSE)){
jcarseyb1f95a02009-06-16 00:23:19 +00002143 //
2144 // add this one as a non-flag
2145 //
jcarsey252d9452011-03-25 20:49:53 +00002146
2147 TempPointer = Argv[LoopCounter];
darylm503b0934ac2011-11-12 00:35:11 +00002148 if ((*TempPointer == L'^' && *(TempPointer+1) == L'-')
jcarsey252d9452011-03-25 20:49:53 +00002149 || (*TempPointer == L'^' && *(TempPointer+1) == L'/')
2150 || (*TempPointer == L'^' && *(TempPointer+1) == L'+')
2151 ){
2152 TempPointer++;
2153 }
2154 CurrentItemPackage = AllocateZeroPool(sizeof(SHELL_PARAM_PACKAGE));
jcarseybeab0fc2011-10-10 17:26:25 +00002155 if (CurrentItemPackage == NULL) {
2156 ShellCommandLineFreeVarList(*CheckPackage);
2157 *CheckPackage = NULL;
2158 return (EFI_OUT_OF_RESOURCES);
2159 }
jcarseyb1f95a02009-06-16 00:23:19 +00002160 CurrentItemPackage->Name = NULL;
2161 CurrentItemPackage->Type = TypePosition;
Jaben Carsey98c16be2014-08-19 21:00:34 +00002162 CurrentItemPackage->Value = AllocateCopyPool(StrSize(TempPointer), TempPointer);
jcarseybeab0fc2011-10-10 17:26:25 +00002163 if (CurrentItemPackage->Value == NULL) {
2164 ShellCommandLineFreeVarList(*CheckPackage);
2165 *CheckPackage = NULL;
2166 return (EFI_OUT_OF_RESOURCES);
2167 }
jcarseya405b862010-09-14 05:18:09 +00002168 CurrentItemPackage->OriginalPosition = Count++;
jcarsey9b3bf082009-06-23 21:15:07 +00002169 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
jcarsey252d9452011-03-25 20:49:53 +00002170 } else {
jcarsey94b17fa2009-05-07 18:46:18 +00002171 //
2172 // this was a non-recognised flag... error!
2173 //
jcarsey252d9452011-03-25 20:49:53 +00002174 if (ProblemParam != NULL) {
Jaben Carsey98c16be2014-08-19 21:00:34 +00002175 *ProblemParam = AllocateCopyPool(StrSize(Argv[LoopCounter]), Argv[LoopCounter]);
jcarsey252d9452011-03-25 20:49:53 +00002176 }
jcarsey94b17fa2009-05-07 18:46:18 +00002177 ShellCommandLineFreeVarList(*CheckPackage);
2178 *CheckPackage = NULL;
2179 return (EFI_VOLUME_CORRUPTED);
jcarsey94b17fa2009-05-07 18:46:18 +00002180 }
2181 }
jcarsey125c2cf2009-11-18 21:36:50 +00002182 if (GetItemValue != 0) {
2183 GetItemValue = 0;
2184 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
2185 }
jcarsey94b17fa2009-05-07 18:46:18 +00002186 //
2187 // support for AutoPageBreak
2188 //
2189 if (AutoPageBreak && ShellCommandLineGetFlag(*CheckPackage, L"-b")) {
2190 ShellSetPageBreakMode(TRUE);
2191 }
2192 return (EFI_SUCCESS);
2193}
2194
2195/**
jcarsey1e6e84c2010-01-25 20:05:08 +00002196 Checks the command line arguments passed against the list of valid ones.
jcarsey94b17fa2009-05-07 18:46:18 +00002197 Optionally removes NULL values first.
jcarsey1e6e84c2010-01-25 20:05:08 +00002198
jcarsey94b17fa2009-05-07 18:46:18 +00002199 If no initialization is required, then return RETURN_SUCCESS.
jcarsey1e6e84c2010-01-25 20:05:08 +00002200
jcarseya405b862010-09-14 05:18:09 +00002201 @param[in] CheckList The pointer to list of parameters to check.
2202 @param[out] CheckPackage The package of checked values.
2203 @param[out] ProblemParam Optional pointer to pointer to unicode string for
jcarsey94b17fa2009-05-07 18:46:18 +00002204 the paramater that caused failure.
jcarseya405b862010-09-14 05:18:09 +00002205 @param[in] AutoPageBreak Will automatically set PageBreakEnabled.
2206 @param[in] AlwaysAllowNumbers Will never fail for number based flags.
jcarsey94b17fa2009-05-07 18:46:18 +00002207
2208 @retval EFI_SUCCESS The operation completed sucessfully.
jcarseya405b862010-09-14 05:18:09 +00002209 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
2210 @retval EFI_INVALID_PARAMETER A parameter was invalid.
2211 @retval EFI_VOLUME_CORRUPTED The command line was corrupt.
2212 @retval EFI_DEVICE_ERROR The commands contained 2 opposing arguments. One
jcarsey1e6e84c2010-01-25 20:05:08 +00002213 of the command line arguments was returned in
jcarsey94b17fa2009-05-07 18:46:18 +00002214 ProblemParam if provided.
jcarseya405b862010-09-14 05:18:09 +00002215 @retval EFI_NOT_FOUND A argument required a value that was missing.
2216 The invalid command line argument was returned in
jcarsey94b17fa2009-05-07 18:46:18 +00002217 ProblemParam if provided.
2218**/
2219EFI_STATUS
2220EFIAPI
jcarsey2247dde2009-11-09 18:08:58 +00002221ShellCommandLineParseEx (
jcarsey94b17fa2009-05-07 18:46:18 +00002222 IN CONST SHELL_PARAM_ITEM *CheckList,
2223 OUT LIST_ENTRY **CheckPackage,
2224 OUT CHAR16 **ProblemParam OPTIONAL,
jcarsey2247dde2009-11-09 18:08:58 +00002225 IN BOOLEAN AutoPageBreak,
2226 IN BOOLEAN AlwaysAllowNumbers
jcarseya405b862010-09-14 05:18:09 +00002227 )
2228{
jcarsey1e6e84c2010-01-25 20:05:08 +00002229 //
jcarsey94b17fa2009-05-07 18:46:18 +00002230 // ASSERT that CheckList and CheckPackage aren't NULL
2231 //
2232 ASSERT(CheckList != NULL);
2233 ASSERT(CheckPackage != NULL);
2234
jcarsey1e6e84c2010-01-25 20:05:08 +00002235 //
jcarsey94b17fa2009-05-07 18:46:18 +00002236 // Check for UEFI Shell 2.0 protocols
2237 //
jcarsey366f81a2011-06-27 21:04:22 +00002238 if (gEfiShellParametersProtocol != NULL) {
jcarsey1e6e84c2010-01-25 20:05:08 +00002239 return (InternalCommandLineParse(CheckList,
2240 CheckPackage,
2241 ProblemParam,
2242 AutoPageBreak,
jcarsey366f81a2011-06-27 21:04:22 +00002243 (CONST CHAR16**) gEfiShellParametersProtocol->Argv,
2244 gEfiShellParametersProtocol->Argc,
jcarsey2247dde2009-11-09 18:08:58 +00002245 AlwaysAllowNumbers));
jcarsey94b17fa2009-05-07 18:46:18 +00002246 }
2247
jcarsey1e6e84c2010-01-25 20:05:08 +00002248 //
jcarsey94b17fa2009-05-07 18:46:18 +00002249 // ASSERT That EFI Shell is not required
2250 //
2251 ASSERT (mEfiShellInterface != NULL);
jcarsey1e6e84c2010-01-25 20:05:08 +00002252 return (InternalCommandLineParse(CheckList,
2253 CheckPackage,
2254 ProblemParam,
2255 AutoPageBreak,
jljusten08d7f8e2009-06-15 18:42:13 +00002256 (CONST CHAR16**) mEfiShellInterface->Argv,
jcarsey2247dde2009-11-09 18:08:58 +00002257 mEfiShellInterface->Argc,
2258 AlwaysAllowNumbers));
jcarsey94b17fa2009-05-07 18:46:18 +00002259}
2260
2261/**
2262 Frees shell variable list that was returned from ShellCommandLineParse.
2263
2264 This function will free all the memory that was used for the CheckPackage
2265 list of postprocessed shell arguments.
2266
2267 this function has no return value.
2268
2269 if CheckPackage is NULL, then return
2270
2271 @param CheckPackage the list to de-allocate
2272 **/
2273VOID
2274EFIAPI
2275ShellCommandLineFreeVarList (
2276 IN LIST_ENTRY *CheckPackage
jcarseya405b862010-09-14 05:18:09 +00002277 )
2278{
jcarsey94b17fa2009-05-07 18:46:18 +00002279 LIST_ENTRY *Node;
2280
2281 //
2282 // check for CheckPackage == NULL
2283 //
2284 if (CheckPackage == NULL) {
2285 return;
2286 }
2287
2288 //
2289 // for each node in the list
2290 //
jcarsey9eb53ac2009-07-08 17:26:58 +00002291 for ( Node = GetFirstNode(CheckPackage)
jcarseya405b862010-09-14 05:18:09 +00002292 ; !IsListEmpty(CheckPackage)
jcarsey9eb53ac2009-07-08 17:26:58 +00002293 ; Node = GetFirstNode(CheckPackage)
jcarseya405b862010-09-14 05:18:09 +00002294 ){
jcarsey94b17fa2009-05-07 18:46:18 +00002295 //
2296 // Remove it from the list
2297 //
2298 RemoveEntryList(Node);
2299
2300 //
2301 // if it has a name free the name
2302 //
2303 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {
2304 FreePool(((SHELL_PARAM_PACKAGE*)Node)->Name);
2305 }
2306
2307 //
2308 // if it has a value free the value
2309 //
2310 if (((SHELL_PARAM_PACKAGE*)Node)->Value != NULL) {
2311 FreePool(((SHELL_PARAM_PACKAGE*)Node)->Value);
2312 }
jcarsey1e6e84c2010-01-25 20:05:08 +00002313
jcarsey94b17fa2009-05-07 18:46:18 +00002314 //
2315 // free the node structure
2316 //
2317 FreePool((SHELL_PARAM_PACKAGE*)Node);
2318 }
2319 //
2320 // free the list head node
2321 //
2322 FreePool(CheckPackage);
2323}
2324/**
2325 Checks for presence of a flag parameter
2326
2327 flag arguments are in the form of "-<Key>" or "/<Key>", but do not have a value following the key
2328
2329 if CheckPackage is NULL then return FALSE.
2330 if KeyString is NULL then ASSERT()
jcarsey1e6e84c2010-01-25 20:05:08 +00002331
jcarsey94b17fa2009-05-07 18:46:18 +00002332 @param CheckPackage The package of parsed command line arguments
2333 @param KeyString the Key of the command line argument to check for
2334
2335 @retval TRUE the flag is on the command line
2336 @retval FALSE the flag is not on the command line
2337 **/
2338BOOLEAN
2339EFIAPI
2340ShellCommandLineGetFlag (
jcarseya405b862010-09-14 05:18:09 +00002341 IN CONST LIST_ENTRY * CONST CheckPackage,
2342 IN CONST CHAR16 * CONST KeyString
2343 )
2344{
jcarsey94b17fa2009-05-07 18:46:18 +00002345 LIST_ENTRY *Node;
jcarsey252d9452011-03-25 20:49:53 +00002346 CHAR16 *TempString;
jcarsey94b17fa2009-05-07 18:46:18 +00002347
2348 //
ydong100c1950b2011-10-13 02:37:35 +00002349 // return FALSE for no package or KeyString is NULL
jcarsey94b17fa2009-05-07 18:46:18 +00002350 //
ydong100c1950b2011-10-13 02:37:35 +00002351 if (CheckPackage == NULL || KeyString == NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00002352 return (FALSE);
2353 }
2354
2355 //
2356 // enumerate through the list of parametrs
2357 //
jcarsey1e6e84c2010-01-25 20:05:08 +00002358 for ( Node = GetFirstNode(CheckPackage)
2359 ; !IsNull (CheckPackage, Node)
2360 ; Node = GetNextNode(CheckPackage, Node)
jcarsey252d9452011-03-25 20:49:53 +00002361 ){
jcarsey94b17fa2009-05-07 18:46:18 +00002362 //
2363 // If the Name matches, return TRUE (and there may be NULL name)
2364 //
2365 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {
jcarsey9eb53ac2009-07-08 17:26:58 +00002366 //
2367 // If Type is TypeStart then only compare the begining of the strings
2368 //
jcarsey252d9452011-03-25 20:49:53 +00002369 if (((SHELL_PARAM_PACKAGE*)Node)->Type == TypeStart) {
2370 if (StrnCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name, StrLen(KeyString)) == 0) {
2371 return (TRUE);
2372 }
2373 TempString = NULL;
2374 TempString = StrnCatGrow(&TempString, NULL, KeyString, StrLen(((SHELL_PARAM_PACKAGE*)Node)->Name));
2375 if (TempString != NULL) {
2376 if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {
2377 FreePool(TempString);
2378 return (TRUE);
2379 }
2380 FreePool(TempString);
2381 }
2382 } else if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {
jcarsey94b17fa2009-05-07 18:46:18 +00002383 return (TRUE);
2384 }
2385 }
2386 }
2387 return (FALSE);
2388}
2389/**
jcarseya405b862010-09-14 05:18:09 +00002390 Returns value from command line argument.
jcarsey94b17fa2009-05-07 18:46:18 +00002391
jcarseya405b862010-09-14 05:18:09 +00002392 Value parameters are in the form of "-<Key> value" or "/<Key> value".
jcarsey1e6e84c2010-01-25 20:05:08 +00002393
jcarseya405b862010-09-14 05:18:09 +00002394 If CheckPackage is NULL, then return NULL.
jcarsey94b17fa2009-05-07 18:46:18 +00002395
jcarseya405b862010-09-14 05:18:09 +00002396 @param[in] CheckPackage The package of parsed command line arguments.
2397 @param[in] KeyString The Key of the command line argument to check for.
jcarsey94b17fa2009-05-07 18:46:18 +00002398
jcarseya405b862010-09-14 05:18:09 +00002399 @retval NULL The flag is not on the command line.
2400 @retval !=NULL The pointer to unicode string of the value.
2401**/
jcarsey94b17fa2009-05-07 18:46:18 +00002402CONST CHAR16*
2403EFIAPI
2404ShellCommandLineGetValue (
2405 IN CONST LIST_ENTRY *CheckPackage,
2406 IN CHAR16 *KeyString
jcarseya405b862010-09-14 05:18:09 +00002407 )
2408{
jcarsey94b17fa2009-05-07 18:46:18 +00002409 LIST_ENTRY *Node;
jcarsey252d9452011-03-25 20:49:53 +00002410 CHAR16 *TempString;
jcarsey94b17fa2009-05-07 18:46:18 +00002411
2412 //
ydong100c1950b2011-10-13 02:37:35 +00002413 // return NULL for no package or KeyString is NULL
jcarsey94b17fa2009-05-07 18:46:18 +00002414 //
ydong100c1950b2011-10-13 02:37:35 +00002415 if (CheckPackage == NULL || KeyString == NULL) {
jcarsey94b17fa2009-05-07 18:46:18 +00002416 return (NULL);
2417 }
2418
2419 //
2420 // enumerate through the list of parametrs
2421 //
jcarsey1e6e84c2010-01-25 20:05:08 +00002422 for ( Node = GetFirstNode(CheckPackage)
2423 ; !IsNull (CheckPackage, Node)
2424 ; Node = GetNextNode(CheckPackage, Node)
jcarsey252d9452011-03-25 20:49:53 +00002425 ){
jcarsey94b17fa2009-05-07 18:46:18 +00002426 //
jcarsey252d9452011-03-25 20:49:53 +00002427 // If the Name matches, return TRUE (and there may be NULL name)
jcarsey94b17fa2009-05-07 18:46:18 +00002428 //
2429 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {
jcarsey9eb53ac2009-07-08 17:26:58 +00002430 //
2431 // If Type is TypeStart then only compare the begining of the strings
2432 //
jcarsey252d9452011-03-25 20:49:53 +00002433 if (((SHELL_PARAM_PACKAGE*)Node)->Type == TypeStart) {
2434 if (StrnCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name, StrLen(KeyString)) == 0) {
2435 return (((SHELL_PARAM_PACKAGE*)Node)->Name + StrLen(KeyString));
2436 }
2437 TempString = NULL;
2438 TempString = StrnCatGrow(&TempString, NULL, KeyString, StrLen(((SHELL_PARAM_PACKAGE*)Node)->Name));
2439 if (TempString != NULL) {
2440 if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {
2441 FreePool(TempString);
2442 return (((SHELL_PARAM_PACKAGE*)Node)->Name + StrLen(KeyString));
2443 }
2444 FreePool(TempString);
2445 }
2446 } else if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {
jcarsey94b17fa2009-05-07 18:46:18 +00002447 return (((SHELL_PARAM_PACKAGE*)Node)->Value);
2448 }
2449 }
2450 }
2451 return (NULL);
2452}
jcarseya405b862010-09-14 05:18:09 +00002453
jcarsey94b17fa2009-05-07 18:46:18 +00002454/**
jcarseya405b862010-09-14 05:18:09 +00002455 Returns raw value from command line argument.
jcarsey94b17fa2009-05-07 18:46:18 +00002456
jcarseya405b862010-09-14 05:18:09 +00002457 Raw value parameters are in the form of "value" in a specific position in the list.
jcarsey1e6e84c2010-01-25 20:05:08 +00002458
jcarseya405b862010-09-14 05:18:09 +00002459 If CheckPackage is NULL, then return NULL.
jcarsey94b17fa2009-05-07 18:46:18 +00002460
jcarseya405b862010-09-14 05:18:09 +00002461 @param[in] CheckPackage The package of parsed command line arguments.
2462 @param[in] Position The position of the value.
jcarsey94b17fa2009-05-07 18:46:18 +00002463
jcarseya405b862010-09-14 05:18:09 +00002464 @retval NULL The flag is not on the command line.
2465 @retval !=NULL The pointer to unicode string of the value.
jcarsey94b17fa2009-05-07 18:46:18 +00002466 **/
2467CONST CHAR16*
2468EFIAPI
2469ShellCommandLineGetRawValue (
jcarseya405b862010-09-14 05:18:09 +00002470 IN CONST LIST_ENTRY * CONST CheckPackage,
2471 IN UINTN Position
2472 )
2473{
jcarsey94b17fa2009-05-07 18:46:18 +00002474 LIST_ENTRY *Node;
2475
2476 //
2477 // check for CheckPackage == NULL
2478 //
2479 if (CheckPackage == NULL) {
2480 return (NULL);
2481 }
2482
2483 //
2484 // enumerate through the list of parametrs
2485 //
jcarsey1e6e84c2010-01-25 20:05:08 +00002486 for ( Node = GetFirstNode(CheckPackage)
2487 ; !IsNull (CheckPackage, Node)
2488 ; Node = GetNextNode(CheckPackage, Node)
jcarseya405b862010-09-14 05:18:09 +00002489 ){
jcarsey94b17fa2009-05-07 18:46:18 +00002490 //
2491 // If the position matches, return the value
2492 //
2493 if (((SHELL_PARAM_PACKAGE*)Node)->OriginalPosition == Position) {
2494 return (((SHELL_PARAM_PACKAGE*)Node)->Value);
2495 }
2496 }
2497 return (NULL);
jcarseyb1f95a02009-06-16 00:23:19 +00002498}
jcarsey2247dde2009-11-09 18:08:58 +00002499
2500/**
jcarsey1e6e84c2010-01-25 20:05:08 +00002501 returns the number of command line value parameters that were parsed.
2502
jcarsey2247dde2009-11-09 18:08:58 +00002503 this will not include flags.
2504
jcarseya405b862010-09-14 05:18:09 +00002505 @param[in] CheckPackage The package of parsed command line arguments.
2506
jcarsey2247dde2009-11-09 18:08:58 +00002507 @retval (UINTN)-1 No parsing has ocurred
2508 @return other The number of value parameters found
2509**/
2510UINTN
2511EFIAPI
2512ShellCommandLineGetCount(
jcarseya405b862010-09-14 05:18:09 +00002513 IN CONST LIST_ENTRY *CheckPackage
jcarsey125c2cf2009-11-18 21:36:50 +00002514 )
2515{
jcarseya405b862010-09-14 05:18:09 +00002516 LIST_ENTRY *Node1;
2517 UINTN Count;
2518
2519 if (CheckPackage == NULL) {
2520 return (0);
2521 }
2522 for ( Node1 = GetFirstNode(CheckPackage), Count = 0
2523 ; !IsNull (CheckPackage, Node1)
2524 ; Node1 = GetNextNode(CheckPackage, Node1)
2525 ){
2526 if (((SHELL_PARAM_PACKAGE*)Node1)->Name == NULL) {
2527 Count++;
2528 }
2529 }
2530 return (Count);
jcarsey2247dde2009-11-09 18:08:58 +00002531}
2532
jcarsey975136a2009-06-16 19:03:54 +00002533/**
jcarsey36a9d672009-11-20 21:13:41 +00002534 Determins if a parameter is duplicated.
2535
jcarsey1e6e84c2010-01-25 20:05:08 +00002536 If Param is not NULL then it will point to a callee allocated string buffer
jcarsey36a9d672009-11-20 21:13:41 +00002537 with the parameter value if a duplicate is found.
2538
2539 If CheckPackage is NULL, then ASSERT.
2540
2541 @param[in] CheckPackage The package of parsed command line arguments.
2542 @param[out] Param Upon finding one, a pointer to the duplicated parameter.
2543
2544 @retval EFI_SUCCESS No parameters were duplicated.
2545 @retval EFI_DEVICE_ERROR A duplicate was found.
2546 **/
2547EFI_STATUS
2548EFIAPI
2549ShellCommandLineCheckDuplicate (
2550 IN CONST LIST_ENTRY *CheckPackage,
2551 OUT CHAR16 **Param
2552 )
2553{
2554 LIST_ENTRY *Node1;
2555 LIST_ENTRY *Node2;
jcarsey1e6e84c2010-01-25 20:05:08 +00002556
jcarsey36a9d672009-11-20 21:13:41 +00002557 ASSERT(CheckPackage != NULL);
2558
jcarsey1e6e84c2010-01-25 20:05:08 +00002559 for ( Node1 = GetFirstNode(CheckPackage)
2560 ; !IsNull (CheckPackage, Node1)
2561 ; Node1 = GetNextNode(CheckPackage, Node1)
jcarseya405b862010-09-14 05:18:09 +00002562 ){
jcarsey1e6e84c2010-01-25 20:05:08 +00002563 for ( Node2 = GetNextNode(CheckPackage, Node1)
2564 ; !IsNull (CheckPackage, Node2)
2565 ; Node2 = GetNextNode(CheckPackage, Node2)
jcarseya405b862010-09-14 05:18:09 +00002566 ){
2567 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 +00002568 if (Param != NULL) {
2569 *Param = NULL;
2570 *Param = StrnCatGrow(Param, NULL, ((SHELL_PARAM_PACKAGE*)Node1)->Name, 0);
2571 }
2572 return (EFI_DEVICE_ERROR);
2573 }
2574 }
2575 }
2576 return (EFI_SUCCESS);
2577}
2578
2579/**
jcarsey1e6e84c2010-01-25 20:05:08 +00002580 This is a find and replace function. Upon successful return the NewString is a copy of
jcarsey975136a2009-06-16 19:03:54 +00002581 SourceString with each instance of FindTarget replaced with ReplaceWith.
2582
jcarseyb3011f42010-01-11 21:49:04 +00002583 If SourceString and NewString overlap the behavior is undefined.
2584
jcarsey975136a2009-06-16 19:03:54 +00002585 If the string would grow bigger than NewSize it will halt and return error.
2586
ydong104ff7e372011-09-02 08:05:34 +00002587 @param[in] SourceString The string with source buffer.
2588 @param[in, out] NewString The string with resultant buffer.
2589 @param[in] NewSize The size in bytes of NewString.
2590 @param[in] FindTarget The string to look for.
2591 @param[in] ReplaceWith The string to replace FindTarget with.
2592 @param[in] SkipPreCarrot If TRUE will skip a FindTarget that has a '^'
2593 immediately before it.
2594 @param[in] ParameterReplacing If TRUE will add "" around items with spaces.
jcarsey975136a2009-06-16 19:03:54 +00002595
jcarsey969c7832010-01-13 16:46:33 +00002596 @retval EFI_INVALID_PARAMETER SourceString was NULL.
2597 @retval EFI_INVALID_PARAMETER NewString was NULL.
2598 @retval EFI_INVALID_PARAMETER FindTarget was NULL.
2599 @retval EFI_INVALID_PARAMETER ReplaceWith was NULL.
2600 @retval EFI_INVALID_PARAMETER FindTarget had length < 1.
2601 @retval EFI_INVALID_PARAMETER SourceString had length < 1.
jcarsey1e6e84c2010-01-25 20:05:08 +00002602 @retval EFI_BUFFER_TOO_SMALL NewSize was less than the minimum size to hold
jcarsey969c7832010-01-13 16:46:33 +00002603 the new string (truncation occurred).
jcarseya405b862010-09-14 05:18:09 +00002604 @retval EFI_SUCCESS The string was successfully copied with replacement.
jcarsey975136a2009-06-16 19:03:54 +00002605**/
jcarsey975136a2009-06-16 19:03:54 +00002606EFI_STATUS
2607EFIAPI
jcarseya405b862010-09-14 05:18:09 +00002608ShellCopySearchAndReplace(
jcarsey975136a2009-06-16 19:03:54 +00002609 IN CHAR16 CONST *SourceString,
jcarseya405b862010-09-14 05:18:09 +00002610 IN OUT CHAR16 *NewString,
jcarsey975136a2009-06-16 19:03:54 +00002611 IN UINTN NewSize,
2612 IN CONST CHAR16 *FindTarget,
jcarsey969c7832010-01-13 16:46:33 +00002613 IN CONST CHAR16 *ReplaceWith,
jcarseya405b862010-09-14 05:18:09 +00002614 IN CONST BOOLEAN SkipPreCarrot,
2615 IN CONST BOOLEAN ParameterReplacing
jcarsey1e6e84c2010-01-25 20:05:08 +00002616 )
jcarsey2247dde2009-11-09 18:08:58 +00002617{
jcarsey01582942009-07-10 19:46:17 +00002618 UINTN Size;
jcarseya405b862010-09-14 05:18:09 +00002619 CHAR16 *Replace;
2620
jcarsey975136a2009-06-16 19:03:54 +00002621 if ( (SourceString == NULL)
2622 || (NewString == NULL)
2623 || (FindTarget == NULL)
2624 || (ReplaceWith == NULL)
2625 || (StrLen(FindTarget) < 1)
2626 || (StrLen(SourceString) < 1)
jcarseya405b862010-09-14 05:18:09 +00002627 ){
jcarsey975136a2009-06-16 19:03:54 +00002628 return (EFI_INVALID_PARAMETER);
2629 }
jcarseya405b862010-09-14 05:18:09 +00002630 Replace = NULL;
2631 if (StrStr(ReplaceWith, L" ") == NULL || !ParameterReplacing) {
2632 Replace = StrnCatGrow(&Replace, NULL, ReplaceWith, 0);
2633 } else {
2634 Replace = AllocateZeroPool(StrSize(ReplaceWith) + 2*sizeof(CHAR16));
jcarseybeab0fc2011-10-10 17:26:25 +00002635 if (Replace != NULL) {
2636 UnicodeSPrint(Replace, StrSize(ReplaceWith) + 2*sizeof(CHAR16), L"\"%s\"", ReplaceWith);
2637 }
jcarseya405b862010-09-14 05:18:09 +00002638 }
jcarsey3e082d52010-10-04 16:44:57 +00002639 if (Replace == NULL) {
2640 return (EFI_OUT_OF_RESOURCES);
2641 }
Jaben Carsey98c16be2014-08-19 21:00:34 +00002642 NewString = ZeroMem(NewString, NewSize);
jcarsey2247dde2009-11-09 18:08:58 +00002643 while (*SourceString != CHAR_NULL) {
jcarsey969c7832010-01-13 16:46:33 +00002644 //
jcarseya405b862010-09-14 05:18:09 +00002645 // if we find the FindTarget and either Skip == FALSE or Skip and we
jcarsey969c7832010-01-13 16:46:33 +00002646 // dont have a carrot do a replace...
2647 //
jcarsey1e6e84c2010-01-25 20:05:08 +00002648 if (StrnCmp(SourceString, FindTarget, StrLen(FindTarget)) == 0
jcarseya405b862010-09-14 05:18:09 +00002649 && ((SkipPreCarrot && *(SourceString-1) != L'^') || !SkipPreCarrot)
2650 ){
jcarsey975136a2009-06-16 19:03:54 +00002651 SourceString += StrLen(FindTarget);
jcarsey01582942009-07-10 19:46:17 +00002652 Size = StrSize(NewString);
jcarseya405b862010-09-14 05:18:09 +00002653 if ((Size + (StrLen(Replace)*sizeof(CHAR16))) > NewSize) {
2654 FreePool(Replace);
jcarsey975136a2009-06-16 19:03:54 +00002655 return (EFI_BUFFER_TOO_SMALL);
2656 }
Jaben Carsey98c16be2014-08-19 21:00:34 +00002657 StrnCat(NewString, Replace, NewSize/sizeof(CHAR16) - 1 - StrLen(NewString));
jcarsey975136a2009-06-16 19:03:54 +00002658 } else {
jcarsey01582942009-07-10 19:46:17 +00002659 Size = StrSize(NewString);
2660 if (Size + sizeof(CHAR16) > NewSize) {
jcarseya405b862010-09-14 05:18:09 +00002661 FreePool(Replace);
jcarsey975136a2009-06-16 19:03:54 +00002662 return (EFI_BUFFER_TOO_SMALL);
2663 }
2664 StrnCat(NewString, SourceString, 1);
2665 SourceString++;
2666 }
2667 }
jcarseya405b862010-09-14 05:18:09 +00002668 FreePool(Replace);
jcarsey975136a2009-06-16 19:03:54 +00002669 return (EFI_SUCCESS);
2670}
jcarseyb1f95a02009-06-16 00:23:19 +00002671
2672/**
jcarseye2f82972009-12-01 05:40:24 +00002673 Internal worker function to output a string.
2674
2675 This function will output a string to the correct StdOut.
2676
2677 @param[in] String The string to print out.
2678
2679 @retval EFI_SUCCESS The operation was sucessful.
2680 @retval !EFI_SUCCESS The operation failed.
2681**/
2682EFI_STATUS
2683EFIAPI
2684InternalPrintTo (
2685 IN CONST CHAR16 *String
2686 )
2687{
2688 UINTN Size;
2689 Size = StrSize(String) - sizeof(CHAR16);
jcarseya405b862010-09-14 05:18:09 +00002690 if (Size == 0) {
2691 return (EFI_SUCCESS);
2692 }
jcarsey366f81a2011-06-27 21:04:22 +00002693 if (gEfiShellParametersProtocol != NULL) {
2694 return (gEfiShellProtocol->WriteFile(gEfiShellParametersProtocol->StdOut, &Size, (VOID*)String));
jcarseye2f82972009-12-01 05:40:24 +00002695 }
2696 if (mEfiShellInterface != NULL) {
jcarsey06c355b2012-03-26 21:00:39 +00002697 if (mEfiShellInterface->RedirArgc == 0) {
jcarsey49bd4982012-01-27 18:42:43 +00002698 //
2699 // Divide in half for old shell. Must be string length not size.
jcarsey06c355b2012-03-26 21:00:39 +00002700 //
2701 Size /=2; // Divide in half only when no redirection.
2702 }
jcarseya405b862010-09-14 05:18:09 +00002703 return (mEfiShellInterface->StdOut->Write(mEfiShellInterface->StdOut, &Size, (VOID*)String));
jcarseye2f82972009-12-01 05:40:24 +00002704 }
2705 ASSERT(FALSE);
2706 return (EFI_UNSUPPORTED);
2707}
2708
2709/**
jcarseyb1f95a02009-06-16 00:23:19 +00002710 Print at a specific location on the screen.
2711
jcarseyf1b87e72009-06-17 00:52:11 +00002712 This function will move the cursor to a given screen location and print the specified string
jcarsey1e6e84c2010-01-25 20:05:08 +00002713
2714 If -1 is specified for either the Row or Col the current screen location for BOTH
jcarseyf1b87e72009-06-17 00:52:11 +00002715 will be used.
jcarseyb1f95a02009-06-16 00:23:19 +00002716
2717 if either Row or Col is out of range for the current console, then ASSERT
2718 if Format is NULL, then ASSERT
2719
jcarsey1e6e84c2010-01-25 20:05:08 +00002720 In addition to the standard %-based flags as supported by UefiLib Print() this supports
jcarseyb1f95a02009-06-16 00:23:19 +00002721 the following additional flags:
2722 %N - Set output attribute to normal
2723 %H - Set output attribute to highlight
2724 %E - Set output attribute to error
2725 %B - Set output attribute to blue color
2726 %V - Set output attribute to green color
2727
2728 Note: The background color is controlled by the shell command cls.
2729
jcarseyb1f95a02009-06-16 00:23:19 +00002730 @param[in] Col the column to print at
jcarsey252d9452011-03-25 20:49:53 +00002731 @param[in] Row the row to print at
jcarseyb1f95a02009-06-16 00:23:19 +00002732 @param[in] Format the format string
jcarsey2247dde2009-11-09 18:08:58 +00002733 @param[in] Marker the marker for the variable argument list
jcarseyb1f95a02009-06-16 00:23:19 +00002734
jcarseya405b862010-09-14 05:18:09 +00002735 @return EFI_SUCCESS The operation was successful.
2736 @return EFI_DEVICE_ERROR The console device reported an error.
jcarseyb1f95a02009-06-16 00:23:19 +00002737**/
jcarseya405b862010-09-14 05:18:09 +00002738EFI_STATUS
jcarseyb1f95a02009-06-16 00:23:19 +00002739EFIAPI
jcarsey2247dde2009-11-09 18:08:58 +00002740InternalShellPrintWorker(
jcarseyb1f95a02009-06-16 00:23:19 +00002741 IN INT32 Col OPTIONAL,
2742 IN INT32 Row OPTIONAL,
2743 IN CONST CHAR16 *Format,
jcarsey252d9452011-03-25 20:49:53 +00002744 IN VA_LIST Marker
jcarsey1e6e84c2010-01-25 20:05:08 +00002745 )
jcarsey2247dde2009-11-09 18:08:58 +00002746{
jcarseyb1f95a02009-06-16 00:23:19 +00002747 EFI_STATUS Status;
jcarsey975136a2009-06-16 19:03:54 +00002748 CHAR16 *ResumeLocation;
2749 CHAR16 *FormatWalker;
jcarseya405b862010-09-14 05:18:09 +00002750 UINTN OriginalAttribute;
jcarsey89e85372011-04-13 23:37:50 +00002751 CHAR16 *mPostReplaceFormat;
2752 CHAR16 *mPostReplaceFormat2;
2753
2754 mPostReplaceFormat = AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize));
2755 mPostReplaceFormat2 = AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize));
jcarseya405b862010-09-14 05:18:09 +00002756
jcarseyf8d3e682011-04-19 17:54:42 +00002757 if (mPostReplaceFormat == NULL || mPostReplaceFormat2 == NULL) {
2758 SHELL_FREE_NON_NULL(mPostReplaceFormat);
2759 SHELL_FREE_NON_NULL(mPostReplaceFormat2);
2760 return (EFI_OUT_OF_RESOURCES);
2761 }
2762
jcarseya405b862010-09-14 05:18:09 +00002763 Status = EFI_SUCCESS;
2764 OriginalAttribute = gST->ConOut->Mode->Attribute;
jcarsey1e6e84c2010-01-25 20:05:08 +00002765
jcarsey975136a2009-06-16 19:03:54 +00002766 //
2767 // Back and forth each time fixing up 1 of our flags...
2768 //
jcarseya405b862010-09-14 05:18:09 +00002769 Status = ShellCopySearchAndReplace(Format, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%N", L"%%N", FALSE, FALSE);
jcarsey975136a2009-06-16 19:03:54 +00002770 ASSERT_EFI_ERROR(Status);
jcarseya405b862010-09-14 05:18:09 +00002771 Status = ShellCopySearchAndReplace(mPostReplaceFormat, mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), L"%E", L"%%E", FALSE, FALSE);
jcarsey975136a2009-06-16 19:03:54 +00002772 ASSERT_EFI_ERROR(Status);
jcarseya405b862010-09-14 05:18:09 +00002773 Status = ShellCopySearchAndReplace(mPostReplaceFormat2, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%H", L"%%H", FALSE, FALSE);
jcarsey975136a2009-06-16 19:03:54 +00002774 ASSERT_EFI_ERROR(Status);
jcarseya405b862010-09-14 05:18:09 +00002775 Status = ShellCopySearchAndReplace(mPostReplaceFormat, mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), L"%B", L"%%B", FALSE, FALSE);
jcarsey975136a2009-06-16 19:03:54 +00002776 ASSERT_EFI_ERROR(Status);
jcarseya405b862010-09-14 05:18:09 +00002777 Status = ShellCopySearchAndReplace(mPostReplaceFormat2, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%V", L"%%V", FALSE, FALSE);
jcarsey975136a2009-06-16 19:03:54 +00002778 ASSERT_EFI_ERROR(Status);
2779
2780 //
2781 // Use the last buffer from replacing to print from...
2782 //
jcarseya405b862010-09-14 05:18:09 +00002783 UnicodeVSPrint (mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), mPostReplaceFormat, Marker);
jcarseyb1f95a02009-06-16 00:23:19 +00002784
2785 if (Col != -1 && Row != -1) {
jcarseyb1f95a02009-06-16 00:23:19 +00002786 Status = gST->ConOut->SetCursorPosition(gST->ConOut, Col, Row);
jcarsey975136a2009-06-16 19:03:54 +00002787 }
2788
jcarseyecd3d592009-12-07 18:05:00 +00002789 FormatWalker = mPostReplaceFormat2;
jcarsey2247dde2009-11-09 18:08:58 +00002790 while (*FormatWalker != CHAR_NULL) {
jcarsey975136a2009-06-16 19:03:54 +00002791 //
2792 // Find the next attribute change request
2793 //
2794 ResumeLocation = StrStr(FormatWalker, L"%");
2795 if (ResumeLocation != NULL) {
jcarsey2247dde2009-11-09 18:08:58 +00002796 *ResumeLocation = CHAR_NULL;
jcarsey975136a2009-06-16 19:03:54 +00002797 }
2798 //
2799 // print the current FormatWalker string
2800 //
jcarseya405b862010-09-14 05:18:09 +00002801 if (StrLen(FormatWalker)>0) {
2802 Status = InternalPrintTo(FormatWalker);
2803 if (EFI_ERROR(Status)) {
2804 break;
2805 }
2806 }
2807
jcarsey975136a2009-06-16 19:03:54 +00002808 //
2809 // update the attribute
2810 //
2811 if (ResumeLocation != NULL) {
jcarsey5d46f172011-08-23 15:34:23 +00002812 if (*(ResumeLocation-1) == L'^') {
2813 //
jcarsey8bb74412012-01-30 18:44:41 +00002814 // Move cursor back 1 position to overwrite the ^
2815 //
2816 gST->ConOut->SetCursorPosition(gST->ConOut, gST->ConOut->Mode->CursorColumn - 1, gST->ConOut->Mode->CursorRow);
2817
2818 //
jcarsey5d46f172011-08-23 15:34:23 +00002819 // Print a simple '%' symbol
2820 //
2821 Status = InternalPrintTo(L"%");
2822 ResumeLocation = ResumeLocation - 1;
2823 } else {
2824 switch (*(ResumeLocation+1)) {
2825 case (L'N'):
2826 gST->ConOut->SetAttribute(gST->ConOut, OriginalAttribute);
jcarseya405b862010-09-14 05:18:09 +00002827 break;
jcarsey5d46f172011-08-23 15:34:23 +00002828 case (L'E'):
2829 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_YELLOW, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));
2830 break;
2831 case (L'H'):
2832 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_WHITE, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));
2833 break;
2834 case (L'B'):
2835 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_BLUE, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));
2836 break;
2837 case (L'V'):
2838 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_GREEN, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));
2839 break;
2840 default:
2841 //
2842 // Print a simple '%' symbol
2843 //
2844 Status = InternalPrintTo(L"%");
2845 if (EFI_ERROR(Status)) {
2846 break;
2847 }
2848 ResumeLocation = ResumeLocation - 1;
2849 break;
2850 }
jcarsey975136a2009-06-16 19:03:54 +00002851 }
2852 } else {
2853 //
2854 // reset to normal now...
2855 //
jcarsey975136a2009-06-16 19:03:54 +00002856 break;
2857 }
2858
2859 //
2860 // update FormatWalker to Resume + 2 (skip the % and the indicator)
2861 //
2862 FormatWalker = ResumeLocation + 2;
2863 }
jcarseyb1f95a02009-06-16 00:23:19 +00002864
jcarseya405b862010-09-14 05:18:09 +00002865 gST->ConOut->SetAttribute(gST->ConOut, OriginalAttribute);
jcarsey89e85372011-04-13 23:37:50 +00002866
2867 SHELL_FREE_NON_NULL(mPostReplaceFormat);
2868 SHELL_FREE_NON_NULL(mPostReplaceFormat2);
jcarseya405b862010-09-14 05:18:09 +00002869 return (Status);
jcarsey5f7431d2009-07-10 18:06:01 +00002870}
jcarsey2247dde2009-11-09 18:08:58 +00002871
2872/**
2873 Print at a specific location on the screen.
2874
jcarseye2f82972009-12-01 05:40:24 +00002875 This function will move the cursor to a given screen location and print the specified string.
jcarsey1e6e84c2010-01-25 20:05:08 +00002876
2877 If -1 is specified for either the Row or Col the current screen location for BOTH
jcarsey2247dde2009-11-09 18:08:58 +00002878 will be used.
2879
jcarseye2f82972009-12-01 05:40:24 +00002880 If either Row or Col is out of range for the current console, then ASSERT.
2881 If Format is NULL, then ASSERT.
jcarsey2247dde2009-11-09 18:08:58 +00002882
jcarsey1e6e84c2010-01-25 20:05:08 +00002883 In addition to the standard %-based flags as supported by UefiLib Print() this supports
jcarsey2247dde2009-11-09 18:08:58 +00002884 the following additional flags:
2885 %N - Set output attribute to normal
2886 %H - Set output attribute to highlight
2887 %E - Set output attribute to error
2888 %B - Set output attribute to blue color
2889 %V - Set output attribute to green color
2890
2891 Note: The background color is controlled by the shell command cls.
2892
jcarsey2247dde2009-11-09 18:08:58 +00002893 @param[in] Col the column to print at
jcarseya405b862010-09-14 05:18:09 +00002894 @param[in] Row the row to print at
jcarsey2247dde2009-11-09 18:08:58 +00002895 @param[in] Format the format string
jcarseya405b862010-09-14 05:18:09 +00002896 @param[in] ... The variable argument list.
jcarsey2247dde2009-11-09 18:08:58 +00002897
jcarseya405b862010-09-14 05:18:09 +00002898 @return EFI_SUCCESS The printing was successful.
2899 @return EFI_DEVICE_ERROR The console device reported an error.
jcarsey2247dde2009-11-09 18:08:58 +00002900**/
jcarseya405b862010-09-14 05:18:09 +00002901EFI_STATUS
jcarsey2247dde2009-11-09 18:08:58 +00002902EFIAPI
2903ShellPrintEx(
2904 IN INT32 Col OPTIONAL,
2905 IN INT32 Row OPTIONAL,
2906 IN CONST CHAR16 *Format,
2907 ...
jcarsey1e6e84c2010-01-25 20:05:08 +00002908 )
jcarsey2247dde2009-11-09 18:08:58 +00002909{
2910 VA_LIST Marker;
jcarseya405b862010-09-14 05:18:09 +00002911 EFI_STATUS RetVal;
jcarsey3e082d52010-10-04 16:44:57 +00002912 if (Format == NULL) {
2913 return (EFI_INVALID_PARAMETER);
2914 }
jcarsey2247dde2009-11-09 18:08:58 +00002915 VA_START (Marker, Format);
jcarseya405b862010-09-14 05:18:09 +00002916 RetVal = InternalShellPrintWorker(Col, Row, Format, Marker);
jcarseye2f82972009-12-01 05:40:24 +00002917 VA_END(Marker);
jcarseya405b862010-09-14 05:18:09 +00002918 return(RetVal);
jcarsey2247dde2009-11-09 18:08:58 +00002919}
2920
2921/**
2922 Print at a specific location on the screen.
2923
jcarseye2f82972009-12-01 05:40:24 +00002924 This function will move the cursor to a given screen location and print the specified string.
jcarsey1e6e84c2010-01-25 20:05:08 +00002925
2926 If -1 is specified for either the Row or Col the current screen location for BOTH
jcarseye2f82972009-12-01 05:40:24 +00002927 will be used.
jcarsey2247dde2009-11-09 18:08:58 +00002928
jcarseye2f82972009-12-01 05:40:24 +00002929 If either Row or Col is out of range for the current console, then ASSERT.
2930 If Format is NULL, then ASSERT.
jcarsey2247dde2009-11-09 18:08:58 +00002931
jcarsey1e6e84c2010-01-25 20:05:08 +00002932 In addition to the standard %-based flags as supported by UefiLib Print() this supports
jcarsey2247dde2009-11-09 18:08:58 +00002933 the following additional flags:
jcarsey1e6e84c2010-01-25 20:05:08 +00002934 %N - Set output attribute to normal.
2935 %H - Set output attribute to highlight.
2936 %E - Set output attribute to error.
2937 %B - Set output attribute to blue color.
2938 %V - Set output attribute to green color.
jcarsey2247dde2009-11-09 18:08:58 +00002939
2940 Note: The background color is controlled by the shell command cls.
2941
jcarsey1e6e84c2010-01-25 20:05:08 +00002942 @param[in] Col The column to print at.
jcarseya405b862010-09-14 05:18:09 +00002943 @param[in] Row The row to print at.
jcarsey1e6e84c2010-01-25 20:05:08 +00002944 @param[in] Language The language of the string to retrieve. If this parameter
2945 is NULL, then the current platform language is used.
2946 @param[in] HiiFormatStringId The format string Id for getting from Hii.
2947 @param[in] HiiFormatHandle The format string Handle for getting from Hii.
jcarseya405b862010-09-14 05:18:09 +00002948 @param[in] ... The variable argument list.
jcarsey2247dde2009-11-09 18:08:58 +00002949
jcarseya405b862010-09-14 05:18:09 +00002950 @return EFI_SUCCESS The printing was successful.
2951 @return EFI_DEVICE_ERROR The console device reported an error.
jcarsey2247dde2009-11-09 18:08:58 +00002952**/
jcarseya405b862010-09-14 05:18:09 +00002953EFI_STATUS
jcarsey2247dde2009-11-09 18:08:58 +00002954EFIAPI
2955ShellPrintHiiEx(
2956 IN INT32 Col OPTIONAL,
2957 IN INT32 Row OPTIONAL,
jcarsey1e6e84c2010-01-25 20:05:08 +00002958 IN CONST CHAR8 *Language OPTIONAL,
jcarsey2247dde2009-11-09 18:08:58 +00002959 IN CONST EFI_STRING_ID HiiFormatStringId,
2960 IN CONST EFI_HANDLE HiiFormatHandle,
2961 ...
2962 )
2963{
2964 VA_LIST Marker;
2965 CHAR16 *HiiFormatString;
jcarseya405b862010-09-14 05:18:09 +00002966 EFI_STATUS RetVal;
jcarsey2247dde2009-11-09 18:08:58 +00002967
2968 VA_START (Marker, HiiFormatHandle);
jcarsey1e6e84c2010-01-25 20:05:08 +00002969 HiiFormatString = HiiGetString(HiiFormatHandle, HiiFormatStringId, Language);
jcarsey2247dde2009-11-09 18:08:58 +00002970 ASSERT(HiiFormatString != NULL);
2971
2972 RetVal = InternalShellPrintWorker(Col, Row, HiiFormatString, Marker);
2973
jcarseya405b862010-09-14 05:18:09 +00002974 SHELL_FREE_NON_NULL(HiiFormatString);
jcarseye2f82972009-12-01 05:40:24 +00002975 VA_END(Marker);
jcarsey2247dde2009-11-09 18:08:58 +00002976
2977 return (RetVal);
2978}
2979
2980/**
2981 Function to determine if a given filename represents a file or a directory.
2982
2983 @param[in] DirName Path to directory to test.
2984
jcarseyc8c22592011-10-17 17:49:21 +00002985 @retval EFI_SUCCESS The Path represents a directory
2986 @retval EFI_NOT_FOUND The Path does not represent a directory
2987 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
2988 @return The path failed to open
jcarsey2247dde2009-11-09 18:08:58 +00002989**/
2990EFI_STATUS
2991EFIAPI
2992ShellIsDirectory(
2993 IN CONST CHAR16 *DirName
2994 )
2995{
2996 EFI_STATUS Status;
jcarseya405b862010-09-14 05:18:09 +00002997 SHELL_FILE_HANDLE Handle;
jcarsey3e082d52010-10-04 16:44:57 +00002998 CHAR16 *TempLocation;
2999 CHAR16 *TempLocation2;
jcarsey2247dde2009-11-09 18:08:58 +00003000
jcarseyecd3d592009-12-07 18:05:00 +00003001 ASSERT(DirName != NULL);
3002
jcarseya405b862010-09-14 05:18:09 +00003003 Handle = NULL;
3004 TempLocation = NULL;
jcarsey2247dde2009-11-09 18:08:58 +00003005
3006 Status = ShellOpenFileByName(DirName, &Handle, EFI_FILE_MODE_READ, 0);
3007 if (EFI_ERROR(Status)) {
jcarseya405b862010-09-14 05:18:09 +00003008 //
3009 // try good logic first.
3010 //
jcarsey366f81a2011-06-27 21:04:22 +00003011 if (gEfiShellProtocol != NULL) {
jcarsey3e082d52010-10-04 16:44:57 +00003012 TempLocation = StrnCatGrow(&TempLocation, NULL, DirName, 0);
jcarseyc8c22592011-10-17 17:49:21 +00003013 if (TempLocation == NULL) {
3014 ShellCloseFile(&Handle);
3015 return (EFI_OUT_OF_RESOURCES);
3016 }
jcarsey3e082d52010-10-04 16:44:57 +00003017 TempLocation2 = StrStr(TempLocation, L":");
3018 if (TempLocation2 != NULL && StrLen(StrStr(TempLocation, L":")) == 2) {
3019 *(TempLocation2+1) = CHAR_NULL;
jcarseya405b862010-09-14 05:18:09 +00003020 }
jcarsey366f81a2011-06-27 21:04:22 +00003021 if (gEfiShellProtocol->GetDevicePathFromMap(TempLocation) != NULL) {
jcarseya405b862010-09-14 05:18:09 +00003022 FreePool(TempLocation);
3023 return (EFI_SUCCESS);
3024 }
3025 FreePool(TempLocation);
3026 } else {
3027 //
3028 // probably a map name?!?!!?
3029 //
3030 TempLocation = StrStr(DirName, L"\\");
3031 if (TempLocation != NULL && *(TempLocation+1) == CHAR_NULL) {
3032 return (EFI_SUCCESS);
3033 }
3034 }
jcarsey2247dde2009-11-09 18:08:58 +00003035 return (Status);
3036 }
3037
3038 if (FileHandleIsDirectory(Handle) == EFI_SUCCESS) {
3039 ShellCloseFile(&Handle);
3040 return (EFI_SUCCESS);
3041 }
3042 ShellCloseFile(&Handle);
3043 return (EFI_NOT_FOUND);
3044}
3045
jcarsey125c2cf2009-11-18 21:36:50 +00003046/**
jcarsey36a9d672009-11-20 21:13:41 +00003047 Function to determine if a given filename represents a file.
3048
3049 @param[in] Name Path to file to test.
3050
3051 @retval EFI_SUCCESS The Path represents a file.
3052 @retval EFI_NOT_FOUND The Path does not represent a file.
3053 @retval other The path failed to open.
3054**/
3055EFI_STATUS
3056EFIAPI
3057ShellIsFile(
3058 IN CONST CHAR16 *Name
3059 )
3060{
3061 EFI_STATUS Status;
jcarseya405b862010-09-14 05:18:09 +00003062 SHELL_FILE_HANDLE Handle;
jcarsey36a9d672009-11-20 21:13:41 +00003063
jcarseyecd3d592009-12-07 18:05:00 +00003064 ASSERT(Name != NULL);
3065
jcarsey36a9d672009-11-20 21:13:41 +00003066 Handle = NULL;
3067
3068 Status = ShellOpenFileByName(Name, &Handle, EFI_FILE_MODE_READ, 0);
3069 if (EFI_ERROR(Status)) {
3070 return (Status);
3071 }
3072
3073 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {
3074 ShellCloseFile(&Handle);
3075 return (EFI_SUCCESS);
3076 }
3077 ShellCloseFile(&Handle);
3078 return (EFI_NOT_FOUND);
3079}
3080
3081/**
jcarseyb3011f42010-01-11 21:49:04 +00003082 Function to determine if a given filename represents a file.
3083
3084 This will search the CWD and then the Path.
3085
3086 If Name is NULL, then ASSERT.
3087
3088 @param[in] Name Path to file to test.
3089
3090 @retval EFI_SUCCESS The Path represents a file.
3091 @retval EFI_NOT_FOUND The Path does not represent a file.
3092 @retval other The path failed to open.
3093**/
3094EFI_STATUS
3095EFIAPI
3096ShellIsFileInPath(
3097 IN CONST CHAR16 *Name
jcarseya405b862010-09-14 05:18:09 +00003098 )
3099{
jcarseyb3011f42010-01-11 21:49:04 +00003100 CHAR16 *NewName;
3101 EFI_STATUS Status;
3102
3103 if (!EFI_ERROR(ShellIsFile(Name))) {
jcarseya405b862010-09-14 05:18:09 +00003104 return (EFI_SUCCESS);
jcarseyb3011f42010-01-11 21:49:04 +00003105 }
3106
3107 NewName = ShellFindFilePath(Name);
3108 if (NewName == NULL) {
3109 return (EFI_NOT_FOUND);
3110 }
3111 Status = ShellIsFile(NewName);
3112 FreePool(NewName);
3113 return (Status);
3114}
jcarsey252d9452011-03-25 20:49:53 +00003115
jcarseyb3011f42010-01-11 21:49:04 +00003116/**
Jaben Carsey74b0fb82013-11-22 21:37:34 +00003117 Function return the number converted from a hex representation of a number.
3118
3119 Note: this function cannot be used when (UINTN)(-1), (0xFFFFFFFF) may be a valid
3120 result. Use ShellConvertStringToUint64 instead.
3121
3122 @param[in] String String representation of a number.
3123
3124 @return The unsigned integer result of the conversion.
3125 @retval (UINTN)(-1) An error occured.
3126**/
3127UINTN
3128EFIAPI
3129ShellHexStrToUintn(
3130 IN CONST CHAR16 *String
3131 )
3132{
3133 UINT64 RetVal;
3134
3135 if (!EFI_ERROR(ShellConvertStringToUint64(String, &RetVal, TRUE, TRUE))) {
3136 return ((UINTN)RetVal);
3137 }
3138
3139 return ((UINTN)(-1));
3140}
3141
3142/**
jcarsey1e6e84c2010-01-25 20:05:08 +00003143 Function to determine whether a string is decimal or hex representation of a number
Jaben Carseyd59fc242013-11-14 23:52:43 +00003144 and return the number converted from the string. Spaces are always skipped.
jcarsey125c2cf2009-11-18 21:36:50 +00003145
3146 @param[in] String String representation of a number
3147
jcarsey252d9452011-03-25 20:49:53 +00003148 @return the number
3149 @retval (UINTN)(-1) An error ocurred.
jcarsey125c2cf2009-11-18 21:36:50 +00003150**/
3151UINTN
3152EFIAPI
3153ShellStrToUintn(
3154 IN CONST CHAR16 *String
3155 )
3156{
jcarsey252d9452011-03-25 20:49:53 +00003157 UINT64 RetVal;
3158 BOOLEAN Hex;
3159
3160 Hex = FALSE;
3161
jcarsey658bf432014-11-04 22:33:16 +00003162 if (!InternalShellIsHexOrDecimalNumber(String, Hex, TRUE, FALSE)) {
jcarsey252d9452011-03-25 20:49:53 +00003163 Hex = TRUE;
jcarsey125c2cf2009-11-18 21:36:50 +00003164 }
jcarsey252d9452011-03-25 20:49:53 +00003165
3166 if (!EFI_ERROR(ShellConvertStringToUint64(String, &RetVal, Hex, TRUE))) {
3167 return ((UINTN)RetVal);
3168 }
3169 return ((UINTN)(-1));
jcarsey125c2cf2009-11-18 21:36:50 +00003170}
3171
3172/**
3173 Safely append with automatic string resizing given length of Destination and
3174 desired length of copy from Source.
3175
3176 append the first D characters of Source to the end of Destination, where D is
3177 the lesser of Count and the StrLen() of Source. If appending those D characters
3178 will fit within Destination (whose Size is given as CurrentSize) and
jcarsey1e6e84c2010-01-25 20:05:08 +00003179 still leave room for a NULL terminator, then those characters are appended,
3180 starting at the original terminating NULL of Destination, and a new terminating
3181 NULL is appended.
jcarsey125c2cf2009-11-18 21:36:50 +00003182
3183 If appending D characters onto Destination will result in a overflow of the size
3184 given in CurrentSize the string will be grown such that the copy can be performed
3185 and CurrentSize will be updated to the new size.
3186
3187 If Source is NULL, there is nothing to append, just return the current buffer in
3188 Destination.
3189
3190 if Destination is NULL, then ASSERT()
3191 if Destination's current length (including NULL terminator) is already more then
3192 CurrentSize, then ASSERT()
3193
ydong104ff7e372011-09-02 08:05:34 +00003194 @param[in, out] Destination The String to append onto
3195 @param[in, out] CurrentSize on call the number of bytes in Destination. On
jcarsey125c2cf2009-11-18 21:36:50 +00003196 return possibly the new size (still in bytes). if NULL
3197 then allocate whatever is needed.
3198 @param[in] Source The String to append from
3199 @param[in] Count Maximum number of characters to append. if 0 then
3200 all are appended.
3201
3202 @return Destination return the resultant string.
3203**/
3204CHAR16*
3205EFIAPI
3206StrnCatGrow (
3207 IN OUT CHAR16 **Destination,
3208 IN OUT UINTN *CurrentSize,
3209 IN CONST CHAR16 *Source,
3210 IN UINTN Count
3211 )
3212{
3213 UINTN DestinationStartSize;
3214 UINTN NewSize;
3215
3216 //
3217 // ASSERTs
3218 //
3219 ASSERT(Destination != NULL);
3220
3221 //
3222 // If there's nothing to do then just return Destination
3223 //
3224 if (Source == NULL) {
3225 return (*Destination);
3226 }
3227
3228 //
3229 // allow for un-initialized pointers, based on size being 0
3230 //
3231 if (CurrentSize != NULL && *CurrentSize == 0) {
3232 *Destination = NULL;
3233 }
3234
3235 //
3236 // allow for NULL pointers address as Destination
3237 //
3238 if (*Destination != NULL) {
3239 ASSERT(CurrentSize != 0);
3240 DestinationStartSize = StrSize(*Destination);
3241 ASSERT(DestinationStartSize <= *CurrentSize);
3242 } else {
3243 DestinationStartSize = 0;
3244// ASSERT(*CurrentSize == 0);
3245 }
3246
3247 //
3248 // Append all of Source?
3249 //
3250 if (Count == 0) {
3251 Count = StrLen(Source);
3252 }
3253
3254 //
3255 // Test and grow if required
3256 //
3257 if (CurrentSize != NULL) {
3258 NewSize = *CurrentSize;
ydong10f480fdc2012-09-07 01:55:33 +00003259 if (NewSize < DestinationStartSize + (Count * sizeof(CHAR16))) {
3260 while (NewSize < (DestinationStartSize + (Count*sizeof(CHAR16)))) {
3261 NewSize += 2 * Count * sizeof(CHAR16);
3262 }
3263 *Destination = ReallocatePool(*CurrentSize, NewSize, *Destination);
3264 *CurrentSize = NewSize;
jcarsey125c2cf2009-11-18 21:36:50 +00003265 }
jcarsey125c2cf2009-11-18 21:36:50 +00003266 } else {
3267 *Destination = AllocateZeroPool((Count+1)*sizeof(CHAR16));
3268 }
3269
3270 //
3271 // Now use standard StrnCat on a big enough buffer
3272 //
jcarseyc9d92df2010-02-03 15:37:54 +00003273 if (*Destination == NULL) {
3274 return (NULL);
3275 }
jcarsey125c2cf2009-11-18 21:36:50 +00003276 return StrnCat(*Destination, Source, Count);
3277}
jcarseyc9d92df2010-02-03 15:37:54 +00003278
3279/**
3280 Prompt the user and return the resultant answer to the requestor.
3281
3282 This function will display the requested question on the shell prompt and then
3283 wait for an apropriate answer to be input from the console.
3284
jcarseya405b862010-09-14 05:18:09 +00003285 if the SHELL_PROMPT_REQUEST_TYPE is SHELL_PROMPT_REQUEST_TYPE_YESNO, ShellPromptResponseTypeQuitContinue
jcarseyc9d92df2010-02-03 15:37:54 +00003286 or SHELL_PROMPT_REQUEST_TYPE_YESNOCANCEL then *Response is of type SHELL_PROMPT_RESPONSE.
3287
jcarseya405b862010-09-14 05:18:09 +00003288 if the SHELL_PROMPT_REQUEST_TYPE is ShellPromptResponseTypeFreeform then *Response is of type
jcarseyc9d92df2010-02-03 15:37:54 +00003289 CHAR16*.
3290
3291 In either case *Response must be callee freed if Response was not NULL;
3292
3293 @param Type What type of question is asked. This is used to filter the input
3294 to prevent invalid answers to question.
3295 @param Prompt Pointer to string prompt to use to request input.
3296 @param Response Pointer to Response which will be populated upon return.
3297
3298 @retval EFI_SUCCESS The operation was sucessful.
3299 @retval EFI_UNSUPPORTED The operation is not supported as requested.
3300 @retval EFI_INVALID_PARAMETER A parameter was invalid.
3301 @return other The operation failed.
3302**/
3303EFI_STATUS
3304EFIAPI
3305ShellPromptForResponse (
3306 IN SHELL_PROMPT_REQUEST_TYPE Type,
3307 IN CHAR16 *Prompt OPTIONAL,
3308 IN OUT VOID **Response OPTIONAL
3309 )
3310{
3311 EFI_STATUS Status;
3312 EFI_INPUT_KEY Key;
3313 UINTN EventIndex;
3314 SHELL_PROMPT_RESPONSE *Resp;
jcarseya405b862010-09-14 05:18:09 +00003315 UINTN Size;
3316 CHAR16 *Buffer;
jcarseyc9d92df2010-02-03 15:37:54 +00003317
jcarseya405b862010-09-14 05:18:09 +00003318 Status = EFI_UNSUPPORTED;
3319 Resp = NULL;
3320 Buffer = NULL;
3321 Size = 0;
3322 if (Type != ShellPromptResponseTypeFreeform) {
jcarsey252d9452011-03-25 20:49:53 +00003323 Resp = (SHELL_PROMPT_RESPONSE*)AllocateZeroPool(sizeof(SHELL_PROMPT_RESPONSE));
jcarsey3e082d52010-10-04 16:44:57 +00003324 if (Resp == NULL) {
3325 return (EFI_OUT_OF_RESOURCES);
3326 }
xdu290bfa222010-07-19 05:21:27 +00003327 }
jcarseyc9d92df2010-02-03 15:37:54 +00003328
3329 switch(Type) {
jcarseya405b862010-09-14 05:18:09 +00003330 case ShellPromptResponseTypeQuitContinue:
jcarseyc9d92df2010-02-03 15:37:54 +00003331 if (Prompt != NULL) {
3332 ShellPrintEx(-1, -1, L"%s", Prompt);
3333 }
3334 //
3335 // wait for valid response
3336 //
3337 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
3338 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
Jaben Carsey31b018a2014-01-16 16:52:39 +00003339 if (EFI_ERROR(Status)) {
3340 break;
3341 }
jcarseyc9d92df2010-02-03 15:37:54 +00003342 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3343 if (Key.UnicodeChar == L'Q' || Key.UnicodeChar ==L'q') {
jcarseya405b862010-09-14 05:18:09 +00003344 *Resp = ShellPromptResponseQuit;
jcarseyc9d92df2010-02-03 15:37:54 +00003345 } else {
jcarseya405b862010-09-14 05:18:09 +00003346 *Resp = ShellPromptResponseContinue;
jcarseyc9d92df2010-02-03 15:37:54 +00003347 }
3348 break;
jcarseya405b862010-09-14 05:18:09 +00003349 case ShellPromptResponseTypeYesNoCancel:
jcarseyc9d92df2010-02-03 15:37:54 +00003350 if (Prompt != NULL) {
3351 ShellPrintEx(-1, -1, L"%s", Prompt);
3352 }
3353 //
3354 // wait for valid response
3355 //
jcarseya405b862010-09-14 05:18:09 +00003356 *Resp = ShellPromptResponseMax;
3357 while (*Resp == ShellPromptResponseMax) {
Jaben Carsey194ae482013-12-09 22:55:13 +00003358 if (ShellGetExecutionBreakFlag()) {
3359 Status = EFI_ABORTED;
3360 break;
3361 }
jcarseyc9d92df2010-02-03 15:37:54 +00003362 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
3363 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
Jaben Carsey31b018a2014-01-16 16:52:39 +00003364 if (EFI_ERROR(Status)) {
3365 break;
3366 }
jcarseyc9d92df2010-02-03 15:37:54 +00003367 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3368 switch (Key.UnicodeChar) {
3369 case L'Y':
3370 case L'y':
jcarseya405b862010-09-14 05:18:09 +00003371 *Resp = ShellPromptResponseYes;
jcarseyc9d92df2010-02-03 15:37:54 +00003372 break;
3373 case L'N':
3374 case L'n':
jcarseya405b862010-09-14 05:18:09 +00003375 *Resp = ShellPromptResponseNo;
jcarseyc9d92df2010-02-03 15:37:54 +00003376 break;
3377 case L'C':
3378 case L'c':
jcarseya405b862010-09-14 05:18:09 +00003379 *Resp = ShellPromptResponseCancel;
3380 break;
3381 }
3382 }
3383 break; case ShellPromptResponseTypeYesNoAllCancel:
3384 if (Prompt != NULL) {
3385 ShellPrintEx(-1, -1, L"%s", Prompt);
3386 }
3387 //
3388 // wait for valid response
3389 //
3390 *Resp = ShellPromptResponseMax;
3391 while (*Resp == ShellPromptResponseMax) {
Jaben Carsey194ae482013-12-09 22:55:13 +00003392 if (ShellGetExecutionBreakFlag()) {
3393 Status = EFI_ABORTED;
3394 break;
3395 }
jcarseya405b862010-09-14 05:18:09 +00003396 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
3397 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
Jaben Carsey31b018a2014-01-16 16:52:39 +00003398 if (EFI_ERROR(Status)) {
3399 break;
3400 }
jcarseya405b862010-09-14 05:18:09 +00003401 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3402 switch (Key.UnicodeChar) {
3403 case L'Y':
3404 case L'y':
3405 *Resp = ShellPromptResponseYes;
3406 break;
3407 case L'N':
3408 case L'n':
3409 *Resp = ShellPromptResponseNo;
3410 break;
3411 case L'A':
3412 case L'a':
3413 *Resp = ShellPromptResponseAll;
3414 break;
3415 case L'C':
3416 case L'c':
3417 *Resp = ShellPromptResponseCancel;
jcarseyc9d92df2010-02-03 15:37:54 +00003418 break;
3419 }
3420 }
3421 break;
jcarseya405b862010-09-14 05:18:09 +00003422 case ShellPromptResponseTypeEnterContinue:
3423 case ShellPromptResponseTypeAnyKeyContinue:
jcarseyc9d92df2010-02-03 15:37:54 +00003424 if (Prompt != NULL) {
3425 ShellPrintEx(-1, -1, L"%s", Prompt);
3426 }
3427 //
3428 // wait for valid response
3429 //
jcarseya405b862010-09-14 05:18:09 +00003430 *Resp = ShellPromptResponseMax;
3431 while (*Resp == ShellPromptResponseMax) {
Jaben Carsey194ae482013-12-09 22:55:13 +00003432 if (ShellGetExecutionBreakFlag()) {
3433 Status = EFI_ABORTED;
3434 break;
3435 }
jcarseyc9d92df2010-02-03 15:37:54 +00003436 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
jcarseya405b862010-09-14 05:18:09 +00003437 if (Type == ShellPromptResponseTypeEnterContinue) {
jcarseyc9d92df2010-02-03 15:37:54 +00003438 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
Jaben Carsey31b018a2014-01-16 16:52:39 +00003439 if (EFI_ERROR(Status)) {
3440 break;
3441 }
jcarseyc9d92df2010-02-03 15:37:54 +00003442 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3443 if (Key.UnicodeChar == CHAR_CARRIAGE_RETURN) {
jcarseya405b862010-09-14 05:18:09 +00003444 *Resp = ShellPromptResponseContinue;
jcarseyc9d92df2010-02-03 15:37:54 +00003445 break;
3446 }
3447 }
jcarseya405b862010-09-14 05:18:09 +00003448 if (Type == ShellPromptResponseTypeAnyKeyContinue) {
3449 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
3450 ASSERT_EFI_ERROR(Status);
3451 *Resp = ShellPromptResponseContinue;
jcarseyc9d92df2010-02-03 15:37:54 +00003452 break;
3453 }
3454 }
3455 break;
jcarseya405b862010-09-14 05:18:09 +00003456 case ShellPromptResponseTypeYesNo:
3457 if (Prompt != NULL) {
3458 ShellPrintEx(-1, -1, L"%s", Prompt);
3459 }
3460 //
3461 // wait for valid response
3462 //
3463 *Resp = ShellPromptResponseMax;
3464 while (*Resp == ShellPromptResponseMax) {
Jaben Carsey194ae482013-12-09 22:55:13 +00003465 if (ShellGetExecutionBreakFlag()) {
3466 Status = EFI_ABORTED;
3467 break;
3468 }
jcarseya405b862010-09-14 05:18:09 +00003469 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
3470 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
Jaben Carsey31b018a2014-01-16 16:52:39 +00003471 if (EFI_ERROR(Status)) {
3472 break;
3473 }
jcarseya405b862010-09-14 05:18:09 +00003474 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3475 switch (Key.UnicodeChar) {
3476 case L'Y':
3477 case L'y':
3478 *Resp = ShellPromptResponseYes;
3479 break;
3480 case L'N':
3481 case L'n':
3482 *Resp = ShellPromptResponseNo;
3483 break;
3484 }
3485 }
3486 break;
3487 case ShellPromptResponseTypeFreeform:
3488 if (Prompt != NULL) {
3489 ShellPrintEx(-1, -1, L"%s", Prompt);
3490 }
3491 while(1) {
Jaben Carsey194ae482013-12-09 22:55:13 +00003492 if (ShellGetExecutionBreakFlag()) {
3493 Status = EFI_ABORTED;
3494 break;
3495 }
jcarseya405b862010-09-14 05:18:09 +00003496 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
3497 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
Jaben Carsey31b018a2014-01-16 16:52:39 +00003498 if (EFI_ERROR(Status)) {
3499 break;
3500 }
jcarseya405b862010-09-14 05:18:09 +00003501 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);
3502 if (Key.UnicodeChar == CHAR_CARRIAGE_RETURN) {
3503 break;
3504 }
3505 ASSERT((Buffer == NULL && Size == 0) || (Buffer != NULL));
3506 StrnCatGrow(&Buffer, &Size, &Key.UnicodeChar, 1);
3507 }
3508 break;
3509 //
3510 // This is the location to add new prompt types.
Jaben Carsey194ae482013-12-09 22:55:13 +00003511 // If your new type loops remember to add ExecutionBreak support.
jcarseya405b862010-09-14 05:18:09 +00003512 //
jcarseyc9d92df2010-02-03 15:37:54 +00003513 default:
jcarseya405b862010-09-14 05:18:09 +00003514 ASSERT(FALSE);
jcarseyc9d92df2010-02-03 15:37:54 +00003515 }
3516
3517 if (Response != NULL) {
jcarseya405b862010-09-14 05:18:09 +00003518 if (Resp != NULL) {
3519 *Response = Resp;
3520 } else if (Buffer != NULL) {
3521 *Response = Buffer;
3522 }
jcarseyc9d92df2010-02-03 15:37:54 +00003523 } else {
jcarseya405b862010-09-14 05:18:09 +00003524 if (Resp != NULL) {
3525 FreePool(Resp);
3526 }
3527 if (Buffer != NULL) {
3528 FreePool(Buffer);
3529 }
jcarseyc9d92df2010-02-03 15:37:54 +00003530 }
3531
jcarseya405b862010-09-14 05:18:09 +00003532 ShellPrintEx(-1, -1, L"\r\n");
jcarseyc9d92df2010-02-03 15:37:54 +00003533 return (Status);
3534}
3535
3536/**
3537 Prompt the user and return the resultant answer to the requestor.
3538
3539 This function is the same as ShellPromptForResponse, except that the prompt is
3540 automatically pulled from HII.
3541
3542 @param Type What type of question is asked. This is used to filter the input
3543 to prevent invalid answers to question.
jcarseya405b862010-09-14 05:18:09 +00003544 @param[in] HiiFormatStringId The format string Id for getting from Hii.
3545 @param[in] HiiFormatHandle The format string Handle for getting from Hii.
3546 @param Response Pointer to Response which will be populated upon return.
jcarseyc9d92df2010-02-03 15:37:54 +00003547
3548 @retval EFI_SUCCESS the operation was sucessful.
3549 @return other the operation failed.
3550
3551 @sa ShellPromptForResponse
3552**/
3553EFI_STATUS
3554EFIAPI
3555ShellPromptForResponseHii (
3556 IN SHELL_PROMPT_REQUEST_TYPE Type,
3557 IN CONST EFI_STRING_ID HiiFormatStringId,
3558 IN CONST EFI_HANDLE HiiFormatHandle,
3559 IN OUT VOID **Response
3560 )
3561{
3562 CHAR16 *Prompt;
3563 EFI_STATUS Status;
3564
3565 Prompt = HiiGetString(HiiFormatHandle, HiiFormatStringId, NULL);
3566 Status = ShellPromptForResponse(Type, Prompt, Response);
3567 FreePool(Prompt);
3568 return (Status);
3569}
3570
jcarseya405b862010-09-14 05:18:09 +00003571/**
3572 Function to determin if an entire string is a valid number.
jcarseyc9d92df2010-02-03 15:37:54 +00003573
jcarseya405b862010-09-14 05:18:09 +00003574 If Hex it must be preceeded with a 0x or has ForceHex, set TRUE.
3575
3576 @param[in] String The string to evaluate.
3577 @param[in] ForceHex TRUE - always assume hex.
3578 @param[in] StopAtSpace TRUE to halt upon finding a space, FALSE to keep going.
jcarsey658bf432014-11-04 22:33:16 +00003579 @param[in] TimeNumbers TRUE to allow numbers with ":", FALSE otherwise.
jcarseya405b862010-09-14 05:18:09 +00003580
3581 @retval TRUE It is all numeric (dec/hex) characters.
3582 @retval FALSE There is a non-numeric character.
3583**/
3584BOOLEAN
3585EFIAPI
jcarsey252d9452011-03-25 20:49:53 +00003586InternalShellIsHexOrDecimalNumber (
jcarseya405b862010-09-14 05:18:09 +00003587 IN CONST CHAR16 *String,
3588 IN CONST BOOLEAN ForceHex,
jcarsey658bf432014-11-04 22:33:16 +00003589 IN CONST BOOLEAN StopAtSpace,
3590 IN CONST BOOLEAN TimeNumbers
jcarseya405b862010-09-14 05:18:09 +00003591 )
3592{
3593 BOOLEAN Hex;
3594
3595 //
3596 // chop off a single negative sign
3597 //
3598 if (String != NULL && *String == L'-') {
3599 String++;
3600 }
darylm503b0934ac2011-11-12 00:35:11 +00003601
jcarseya405b862010-09-14 05:18:09 +00003602 if (String == NULL) {
3603 return (FALSE);
3604 }
3605
3606 //
3607 // chop leading zeroes
3608 //
3609 while(String != NULL && *String == L'0'){
3610 String++;
3611 }
3612 //
3613 // allow '0x' or '0X', but not 'x' or 'X'
3614 //
3615 if (String != NULL && (*String == L'x' || *String == L'X')) {
3616 if (*(String-1) != L'0') {
3617 //
3618 // we got an x without a preceeding 0
3619 //
3620 return (FALSE);
3621 }
3622 String++;
3623 Hex = TRUE;
3624 } else if (ForceHex) {
3625 Hex = TRUE;
3626 } else {
3627 Hex = FALSE;
3628 }
3629
3630 //
3631 // loop through the remaining characters and use the lib function
3632 //
3633 for ( ; String != NULL && *String != CHAR_NULL && !(StopAtSpace && *String == L' ') ; String++){
jcarsey658bf432014-11-04 22:33:16 +00003634 if (TimeNumbers && (String[0] == L':')) {
3635 continue;
3636 }
jcarseya405b862010-09-14 05:18:09 +00003637 if (Hex) {
3638 if (!ShellIsHexaDecimalDigitCharacter(*String)) {
3639 return (FALSE);
3640 }
3641 } else {
3642 if (!ShellIsDecimalDigitCharacter(*String)) {
3643 return (FALSE);
3644 }
3645 }
3646 }
jcarsey252d9452011-03-25 20:49:53 +00003647
jcarseya405b862010-09-14 05:18:09 +00003648 return (TRUE);
3649}
3650
3651/**
3652 Function to determine if a given filename exists.
3653
3654 @param[in] Name Path to test.
3655
3656 @retval EFI_SUCCESS The Path represents a file.
3657 @retval EFI_NOT_FOUND The Path does not represent a file.
3658 @retval other The path failed to open.
3659**/
3660EFI_STATUS
3661EFIAPI
3662ShellFileExists(
3663 IN CONST CHAR16 *Name
3664 )
3665{
3666 EFI_STATUS Status;
3667 EFI_SHELL_FILE_INFO *List;
3668
3669 ASSERT(Name != NULL);
3670
3671 List = NULL;
3672 Status = ShellOpenFileMetaArg((CHAR16*)Name, EFI_FILE_MODE_READ, &List);
3673 if (EFI_ERROR(Status)) {
3674 return (Status);
3675 }
3676
3677 ShellCloseFileMetaArg(&List);
3678
3679 return (EFI_SUCCESS);
3680}
jcarsey252d9452011-03-25 20:49:53 +00003681
3682/**
darylm503b0934ac2011-11-12 00:35:11 +00003683 Convert a Unicode character to upper case only if
jcarsey252d9452011-03-25 20:49:53 +00003684 it maps to a valid small-case ASCII character.
3685
3686 This internal function only deal with Unicode character
3687 which maps to a valid small-case ASCII character, i.e.
3688 L'a' to L'z'. For other Unicode character, the input character
3689 is returned directly.
3690
3691 @param Char The character to convert.
3692
3693 @retval LowerCharacter If the Char is with range L'a' to L'z'.
3694 @retval Unchanged Otherwise.
3695
3696**/
3697CHAR16
3698EFIAPI
3699InternalShellCharToUpper (
3700 IN CHAR16 Char
3701 )
3702{
3703 if (Char >= L'a' && Char <= L'z') {
3704 return (CHAR16) (Char - (L'a' - L'A'));
3705 }
3706
3707 return Char;
3708}
3709
3710/**
3711 Convert a Unicode character to numerical value.
3712
3713 This internal function only deal with Unicode character
3714 which maps to a valid hexadecimal ASII character, i.e.
darylm503b0934ac2011-11-12 00:35:11 +00003715 L'0' to L'9', L'a' to L'f' or L'A' to L'F'. For other
jcarsey252d9452011-03-25 20:49:53 +00003716 Unicode character, the value returned does not make sense.
3717
3718 @param Char The character to convert.
3719
3720 @return The numerical value converted.
3721
3722**/
3723UINTN
3724EFIAPI
3725InternalShellHexCharToUintn (
3726 IN CHAR16 Char
3727 )
3728{
3729 if (ShellIsDecimalDigitCharacter (Char)) {
3730 return Char - L'0';
3731 }
3732
3733 return (UINTN) (10 + InternalShellCharToUpper (Char) - L'A');
3734}
3735
3736/**
3737 Convert a Null-terminated Unicode hexadecimal string to a value of type UINT64.
3738
3739 This function returns a value of type UINTN by interpreting the contents
3740 of the Unicode string specified by String as a hexadecimal number.
3741 The format of the input Unicode string String is:
3742
3743 [spaces][zeros][x][hexadecimal digits].
3744
3745 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
3746 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix.
3747 If "x" appears in the input string, it must be prefixed with at least one 0.
3748 The function will ignore the pad space, which includes spaces or tab characters,
3749 before [zeros], [x] or [hexadecimal digit]. The running zero before [x] or
3750 [hexadecimal digit] will be ignored. Then, the decoding starts after [x] or the
3751 first valid hexadecimal digit. Then, the function stops at the first character that is
3752 a not a valid hexadecimal character or NULL, whichever one comes first.
3753
3754 If String has only pad spaces, then zero is returned.
3755 If String has no leading pad spaces, leading zeros or valid hexadecimal digits,
3756 then zero is returned.
3757
3758 @param[in] String A pointer to a Null-terminated Unicode string.
3759 @param[out] Value Upon a successful return the value of the conversion.
3760 @param[in] StopAtSpace FALSE to skip spaces.
3761
3762 @retval EFI_SUCCESS The conversion was successful.
3763 @retval EFI_INVALID_PARAMETER A parameter was NULL or invalid.
3764 @retval EFI_DEVICE_ERROR An overflow occured.
3765**/
3766EFI_STATUS
3767EFIAPI
3768InternalShellStrHexToUint64 (
3769 IN CONST CHAR16 *String,
3770 OUT UINT64 *Value,
3771 IN CONST BOOLEAN StopAtSpace
3772 )
3773{
3774 UINT64 Result;
3775
3776 if (String == NULL || StrSize(String) == 0 || Value == NULL) {
3777 return (EFI_INVALID_PARAMETER);
3778 }
darylm503b0934ac2011-11-12 00:35:11 +00003779
jcarsey252d9452011-03-25 20:49:53 +00003780 //
darylm503b0934ac2011-11-12 00:35:11 +00003781 // Ignore the pad spaces (space or tab)
jcarsey252d9452011-03-25 20:49:53 +00003782 //
3783 while ((*String == L' ') || (*String == L'\t')) {
3784 String++;
3785 }
3786
3787 //
3788 // Ignore leading Zeros after the spaces
3789 //
3790 while (*String == L'0') {
3791 String++;
3792 }
3793
3794 if (InternalShellCharToUpper (*String) == L'X') {
3795 if (*(String - 1) != L'0') {
3796 return 0;
3797 }
3798 //
3799 // Skip the 'X'
3800 //
3801 String++;
3802 }
3803
3804 Result = 0;
darylm503b0934ac2011-11-12 00:35:11 +00003805
jcarsey252d9452011-03-25 20:49:53 +00003806 //
ydong1012ea4692012-07-26 05:42:43 +00003807 // Skip spaces if requested
jcarsey252d9452011-03-25 20:49:53 +00003808 //
ydong1012ea4692012-07-26 05:42:43 +00003809 while (StopAtSpace && *String == L' ') {
3810 String++;
jcarsey252d9452011-03-25 20:49:53 +00003811 }
darylm503b0934ac2011-11-12 00:35:11 +00003812
jcarsey252d9452011-03-25 20:49:53 +00003813 while (ShellIsHexaDecimalDigitCharacter (*String)) {
3814 //
darylm503b0934ac2011-11-12 00:35:11 +00003815 // If the Hex Number represented by String overflows according
jcarsey252d9452011-03-25 20:49:53 +00003816 // to the range defined by UINTN, then ASSERT().
3817 //
3818 if (!(Result <= (RShiftU64((((UINT64) ~0) - InternalShellHexCharToUintn (*String)), 4)))) {
3819// if (!(Result <= ((((UINT64) ~0) - InternalShellHexCharToUintn (*String)) >> 4))) {
3820 return (EFI_DEVICE_ERROR);
3821 }
3822
3823 Result = (LShiftU64(Result, 4));
3824 Result += InternalShellHexCharToUintn (*String);
3825 String++;
3826
3827 //
jcarsey49bd4982012-01-27 18:42:43 +00003828 // stop at spaces if requested
jcarsey252d9452011-03-25 20:49:53 +00003829 //
jcarsey49bd4982012-01-27 18:42:43 +00003830 if (StopAtSpace && *String == L' ') {
3831 break;
jcarsey252d9452011-03-25 20:49:53 +00003832 }
3833 }
3834
3835 *Value = Result;
3836 return (EFI_SUCCESS);
3837}
3838
3839/**
3840 Convert a Null-terminated Unicode decimal string to a value of
3841 type UINT64.
3842
3843 This function returns a value of type UINT64 by interpreting the contents
3844 of the Unicode string specified by String as a decimal number. The format
3845 of the input Unicode string String is:
3846
3847 [spaces] [decimal digits].
3848
3849 The valid decimal digit character is in the range [0-9]. The
3850 function will ignore the pad space, which includes spaces or
3851 tab characters, before [decimal digits]. The running zero in the
3852 beginning of [decimal digits] will be ignored. Then, the function
3853 stops at the first character that is a not a valid decimal character
3854 or a Null-terminator, whichever one comes first.
3855
3856 If String has only pad spaces, then 0 is returned.
3857 If String has no pad spaces or valid decimal digits,
3858 then 0 is returned.
3859
3860 @param[in] String A pointer to a Null-terminated Unicode string.
3861 @param[out] Value Upon a successful return the value of the conversion.
3862 @param[in] StopAtSpace FALSE to skip spaces.
3863
3864 @retval EFI_SUCCESS The conversion was successful.
3865 @retval EFI_INVALID_PARAMETER A parameter was NULL or invalid.
3866 @retval EFI_DEVICE_ERROR An overflow occured.
3867**/
3868EFI_STATUS
3869EFIAPI
3870InternalShellStrDecimalToUint64 (
3871 IN CONST CHAR16 *String,
3872 OUT UINT64 *Value,
3873 IN CONST BOOLEAN StopAtSpace
3874 )
3875{
3876 UINT64 Result;
3877
3878 if (String == NULL || StrSize (String) == 0 || Value == NULL) {
3879 return (EFI_INVALID_PARAMETER);
3880 }
3881
3882 //
3883 // Ignore the pad spaces (space or tab)
3884 //
3885 while ((*String == L' ') || (*String == L'\t')) {
3886 String++;
3887 }
3888
3889 //
3890 // Ignore leading Zeros after the spaces
3891 //
3892 while (*String == L'0') {
3893 String++;
3894 }
3895
3896 Result = 0;
3897
3898 //
3899 // Skip spaces if requested
3900 //
3901 while (StopAtSpace && *String == L' ') {
3902 String++;
3903 }
3904 while (ShellIsDecimalDigitCharacter (*String)) {
3905 //
darylm503b0934ac2011-11-12 00:35:11 +00003906 // If the number represented by String overflows according
jcarsey252d9452011-03-25 20:49:53 +00003907 // to the range defined by UINT64, then ASSERT().
3908 //
darylm503b0934ac2011-11-12 00:35:11 +00003909
jcarsey252d9452011-03-25 20:49:53 +00003910 if (!(Result <= (DivU64x32((((UINT64) ~0) - (*String - L'0')),10)))) {
3911 return (EFI_DEVICE_ERROR);
3912 }
3913
3914 Result = MultU64x32(Result, 10) + (*String - L'0');
3915 String++;
3916
3917 //
3918 // Stop at spaces if requested
3919 //
3920 if (StopAtSpace && *String == L' ') {
3921 break;
3922 }
3923 }
3924
3925 *Value = Result;
darylm503b0934ac2011-11-12 00:35:11 +00003926
jcarsey252d9452011-03-25 20:49:53 +00003927 return (EFI_SUCCESS);
3928}
3929
3930/**
3931 Function to verify and convert a string to its numerical value.
3932
3933 If Hex it must be preceeded with a 0x, 0X, or has ForceHex set TRUE.
3934
3935 @param[in] String The string to evaluate.
3936 @param[out] Value Upon a successful return the value of the conversion.
3937 @param[in] ForceHex TRUE - always assume hex.
3938 @param[in] StopAtSpace FALSE to skip spaces.
darylm503b0934ac2011-11-12 00:35:11 +00003939
jcarsey252d9452011-03-25 20:49:53 +00003940 @retval EFI_SUCCESS The conversion was successful.
3941 @retval EFI_INVALID_PARAMETER String contained an invalid character.
3942 @retval EFI_NOT_FOUND String was a number, but Value was NULL.
3943**/
3944EFI_STATUS
3945EFIAPI
3946ShellConvertStringToUint64(
3947 IN CONST CHAR16 *String,
3948 OUT UINT64 *Value,
3949 IN CONST BOOLEAN ForceHex,
3950 IN CONST BOOLEAN StopAtSpace
3951 )
3952{
3953 UINT64 RetVal;
3954 CONST CHAR16 *Walker;
3955 EFI_STATUS Status;
3956 BOOLEAN Hex;
3957
3958 Hex = ForceHex;
3959
jcarsey658bf432014-11-04 22:33:16 +00003960 if (!InternalShellIsHexOrDecimalNumber(String, Hex, StopAtSpace, FALSE)) {
jcarsey252d9452011-03-25 20:49:53 +00003961 if (!Hex) {
3962 Hex = TRUE;
jcarsey658bf432014-11-04 22:33:16 +00003963 if (!InternalShellIsHexOrDecimalNumber(String, Hex, StopAtSpace, FALSE)) {
jcarsey252d9452011-03-25 20:49:53 +00003964 return (EFI_INVALID_PARAMETER);
3965 }
3966 } else {
3967 return (EFI_INVALID_PARAMETER);
3968 }
3969 }
3970
3971 //
3972 // Chop off leading spaces
3973 //
3974 for (Walker = String; Walker != NULL && *Walker != CHAR_NULL && *Walker == L' '; Walker++);
3975
3976 //
3977 // make sure we have something left that is numeric.
3978 //
jcarsey658bf432014-11-04 22:33:16 +00003979 if (Walker == NULL || *Walker == CHAR_NULL || !InternalShellIsHexOrDecimalNumber(Walker, Hex, StopAtSpace, FALSE)) {
jcarsey252d9452011-03-25 20:49:53 +00003980 return (EFI_INVALID_PARAMETER);
darylm503b0934ac2011-11-12 00:35:11 +00003981 }
jcarsey252d9452011-03-25 20:49:53 +00003982
3983 //
3984 // do the conversion.
3985 //
3986 if (Hex || StrnCmp(Walker, L"0x", 2) == 0 || StrnCmp(Walker, L"0X", 2) == 0){
3987 Status = InternalShellStrHexToUint64(Walker, &RetVal, StopAtSpace);
3988 } else {
3989 Status = InternalShellStrDecimalToUint64(Walker, &RetVal, StopAtSpace);
3990 }
3991
3992 if (Value == NULL && !EFI_ERROR(Status)) {
3993 return (EFI_NOT_FOUND);
3994 }
3995
3996 if (Value != NULL) {
3997 *Value = RetVal;
3998 }
3999
4000 return (Status);
4001}
4002
4003/**
4004 Function to determin if an entire string is a valid number.
4005
4006 If Hex it must be preceeded with a 0x or has ForceHex, set TRUE.
4007
4008 @param[in] String The string to evaluate.
4009 @param[in] ForceHex TRUE - always assume hex.
4010 @param[in] StopAtSpace TRUE to halt upon finding a space, FALSE to keep going.
4011
4012 @retval TRUE It is all numeric (dec/hex) characters.
4013 @retval FALSE There is a non-numeric character.
4014**/
4015BOOLEAN
4016EFIAPI
4017ShellIsHexOrDecimalNumber (
4018 IN CONST CHAR16 *String,
4019 IN CONST BOOLEAN ForceHex,
4020 IN CONST BOOLEAN StopAtSpace
4021 )
4022{
4023 if (ShellConvertStringToUint64(String, NULL, ForceHex, StopAtSpace) == EFI_NOT_FOUND) {
4024 return (TRUE);
4025 }
4026 return (FALSE);
4027}
jcarsey4d0a4fc2011-07-06 22:28:36 +00004028
4029/**
4030 Function to read a single line from a SHELL_FILE_HANDLE. The \n is not included in the returned
4031 buffer. The returned buffer must be callee freed.
4032
4033 If the position upon start is 0, then the Ascii Boolean will be set. This should be
4034 maintained and not changed for all operations with the same file.
4035
ydong104ff7e372011-09-02 08:05:34 +00004036 @param[in] Handle SHELL_FILE_HANDLE to read from.
4037 @param[in, out] Ascii Boolean value for indicating whether the file is
4038 Ascii (TRUE) or UCS2 (FALSE).
jcarsey4d0a4fc2011-07-06 22:28:36 +00004039
jcarseybeab0fc2011-10-10 17:26:25 +00004040 @return The line of text from the file.
4041 @retval NULL There was not enough memory available.
jcarsey4d0a4fc2011-07-06 22:28:36 +00004042
4043 @sa ShellFileHandleReadLine
4044**/
4045CHAR16*
4046EFIAPI
4047ShellFileHandleReturnLine(
4048 IN SHELL_FILE_HANDLE Handle,
4049 IN OUT BOOLEAN *Ascii
4050 )
4051{
4052 CHAR16 *RetVal;
4053 UINTN Size;
4054 EFI_STATUS Status;
4055
4056 Size = 0;
4057 RetVal = NULL;
4058
4059 Status = ShellFileHandleReadLine(Handle, RetVal, &Size, FALSE, Ascii);
4060 if (Status == EFI_BUFFER_TOO_SMALL) {
4061 RetVal = AllocateZeroPool(Size);
jcarseybeab0fc2011-10-10 17:26:25 +00004062 if (RetVal == NULL) {
4063 return (NULL);
4064 }
jcarsey4d0a4fc2011-07-06 22:28:36 +00004065 Status = ShellFileHandleReadLine(Handle, RetVal, &Size, FALSE, Ascii);
darylm503b0934ac2011-11-12 00:35:11 +00004066
jcarsey4d0a4fc2011-07-06 22:28:36 +00004067 }
jcarsey4d0a4fc2011-07-06 22:28:36 +00004068 if (EFI_ERROR(Status) && (RetVal != NULL)) {
4069 FreePool(RetVal);
4070 RetVal = NULL;
4071 }
4072 return (RetVal);
4073}
4074
4075/**
4076 Function to read a single line (up to but not including the \n) from a SHELL_FILE_HANDLE.
4077
4078 If the position upon start is 0, then the Ascii Boolean will be set. This should be
4079 maintained and not changed for all operations with the same file.
4080
ydong104ff7e372011-09-02 08:05:34 +00004081 @param[in] Handle SHELL_FILE_HANDLE to read from.
4082 @param[in, out] Buffer The pointer to buffer to read into.
4083 @param[in, out] Size The pointer to number of bytes in Buffer.
4084 @param[in] Truncate If the buffer is large enough, this has no effect.
4085 If the buffer is is too small and Truncate is TRUE,
4086 the line will be truncated.
4087 If the buffer is is too small and Truncate is FALSE,
4088 then no read will occur.
jcarsey4d0a4fc2011-07-06 22:28:36 +00004089
ydong104ff7e372011-09-02 08:05:34 +00004090 @param[in, out] Ascii Boolean value for indicating whether the file is
4091 Ascii (TRUE) or UCS2 (FALSE).
jcarsey4d0a4fc2011-07-06 22:28:36 +00004092
4093 @retval EFI_SUCCESS The operation was successful. The line is stored in
4094 Buffer.
4095 @retval EFI_INVALID_PARAMETER Handle was NULL.
4096 @retval EFI_INVALID_PARAMETER Size was NULL.
4097 @retval EFI_BUFFER_TOO_SMALL Size was not large enough to store the line.
4098 Size was updated to the minimum space required.
4099**/
4100EFI_STATUS
4101EFIAPI
4102ShellFileHandleReadLine(
4103 IN SHELL_FILE_HANDLE Handle,
4104 IN OUT CHAR16 *Buffer,
4105 IN OUT UINTN *Size,
4106 IN BOOLEAN Truncate,
4107 IN OUT BOOLEAN *Ascii
4108 )
4109{
4110 EFI_STATUS Status;
4111 CHAR16 CharBuffer;
4112 UINTN CharSize;
4113 UINTN CountSoFar;
4114 UINT64 OriginalFilePosition;
4115
4116
4117 if (Handle == NULL
4118 ||Size == NULL
4119 ){
4120 return (EFI_INVALID_PARAMETER);
4121 }
4122 if (Buffer == NULL) {
4123 ASSERT(*Size == 0);
4124 } else {
4125 *Buffer = CHAR_NULL;
4126 }
4127 gEfiShellProtocol->GetFilePosition(Handle, &OriginalFilePosition);
4128 if (OriginalFilePosition == 0) {
4129 CharSize = sizeof(CHAR16);
4130 Status = gEfiShellProtocol->ReadFile(Handle, &CharSize, &CharBuffer);
4131 ASSERT_EFI_ERROR(Status);
4132 if (CharBuffer == gUnicodeFileTag) {
4133 *Ascii = FALSE;
4134 } else {
4135 *Ascii = TRUE;
4136 gEfiShellProtocol->SetFilePosition(Handle, OriginalFilePosition);
4137 }
4138 }
4139
4140 for (CountSoFar = 0;;CountSoFar++){
4141 CharBuffer = 0;
4142 if (*Ascii) {
4143 CharSize = sizeof(CHAR8);
4144 } else {
4145 CharSize = sizeof(CHAR16);
4146 }
4147 Status = gEfiShellProtocol->ReadFile(Handle, &CharSize, &CharBuffer);
4148 if ( EFI_ERROR(Status)
4149 || CharSize == 0
4150 || (CharBuffer == L'\n' && !(*Ascii))
4151 || (CharBuffer == '\n' && *Ascii)
4152 ){
4153 break;
4154 }
4155 //
4156 // if we have space save it...
4157 //
4158 if ((CountSoFar+1)*sizeof(CHAR16) < *Size){
4159 ASSERT(Buffer != NULL);
4160 ((CHAR16*)Buffer)[CountSoFar] = CharBuffer;
4161 ((CHAR16*)Buffer)[CountSoFar+1] = CHAR_NULL;
4162 }
4163 }
4164
4165 //
4166 // if we ran out of space tell when...
4167 //
4168 if ((CountSoFar+1)*sizeof(CHAR16) > *Size){
4169 *Size = (CountSoFar+1)*sizeof(CHAR16);
4170 if (!Truncate) {
4171 gEfiShellProtocol->SetFilePosition(Handle, OriginalFilePosition);
4172 } else {
4173 DEBUG((DEBUG_WARN, "The line was truncated in ShellFileHandleReadLine"));
4174 }
4175 return (EFI_BUFFER_TOO_SMALL);
4176 }
4177 while(Buffer[StrLen(Buffer)-1] == L'\r') {
4178 Buffer[StrLen(Buffer)-1] = CHAR_NULL;
4179 }
4180
4181 return (Status);
4182}
jcarseyfb5278e2013-02-20 18:21:14 +00004183
4184/**
jcarsey365aa982013-03-04 21:54:02 +00004185 Function to print help file / man page content in the spec from the UEFI Shell protocol GetHelpText function.
4186
4187 @param[in] CommandToGetHelpOn Pointer to a string containing the command name of help file to be printed.
4188 @param[in] SectionToGetHelpOn Pointer to the section specifier(s).
4189 @param[in] PrintCommandText If TRUE, prints the command followed by the help content, otherwise prints
4190 the help content only.
4191 @retval EFI_DEVICE_ERROR The help data format was incorrect.
4192 @retval EFI_NOT_FOUND The help data could not be found.
4193 @retval EFI_SUCCESS The operation was successful.
4194**/
4195EFI_STATUS
4196EFIAPI
4197ShellPrintHelp (
4198 IN CONST CHAR16 *CommandToGetHelpOn,
4199 IN CONST CHAR16 *SectionToGetHelpOn,
4200 IN BOOLEAN PrintCommandText
4201 )
4202{
4203 EFI_STATUS Status;
4204 CHAR16 *OutText;
4205
4206 OutText = NULL;
4207
4208 //
4209 // Get the string to print based
4210 //
4211 Status = gEfiShellProtocol->GetHelpText (CommandToGetHelpOn, SectionToGetHelpOn, &OutText);
4212
4213 //
4214 // make sure we got a valid string
4215 //
4216 if (EFI_ERROR(Status)){
4217 return Status;
4218 }
4219 if (OutText == NULL || StrLen(OutText) == 0) {
4220 return EFI_NOT_FOUND;
4221 }
4222
4223 //
4224 // Chop off trailing stuff we dont need
4225 //
4226 while (OutText[StrLen(OutText)-1] == L'\r' || OutText[StrLen(OutText)-1] == L'\n' || OutText[StrLen(OutText)-1] == L' ') {
4227 OutText[StrLen(OutText)-1] = CHAR_NULL;
4228 }
4229
4230 //
4231 // Print this out to the console
4232 //
4233 if (PrintCommandText) {
4234 ShellPrintEx(-1, -1, L"%H%-14s%N- %s\r\n", CommandToGetHelpOn, OutText);
4235 } else {
4236 ShellPrintEx(-1, -1, L"%N%s\r\n", OutText);
4237 }
4238
4239 SHELL_FREE_NON_NULL(OutText);
4240
4241 return EFI_SUCCESS;
4242}
4243
4244/**
jcarseyfb5278e2013-02-20 18:21:14 +00004245 Function to delete a file by name
4246
4247 @param[in] FileName Pointer to file name to delete.
4248
4249 @retval EFI_SUCCESS the file was deleted sucessfully
4250 @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not
4251 deleted
4252 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
4253 @retval EFI_NOT_FOUND The specified file could not be found on the
4254 device or the file system could not be found
4255 on the device.
4256 @retval EFI_NO_MEDIA The device has no medium.
4257 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
4258 medium is no longer supported.
4259 @retval EFI_DEVICE_ERROR The device reported an error.
4260 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
4261 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
4262 @retval EFI_ACCESS_DENIED The file was opened read only.
4263 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
4264 file.
4265 @retval other The file failed to open
4266**/
4267EFI_STATUS
4268EFIAPI
4269ShellDeleteFileByName(
4270 IN CONST CHAR16 *FileName
4271 )
4272{
4273 EFI_STATUS Status;
4274 SHELL_FILE_HANDLE FileHandle;
4275
4276 Status = ShellFileExists(FileName);
4277
4278 if (Status == EFI_SUCCESS){
4279 Status = ShellOpenFileByName(FileName, &FileHandle, EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE, 0x0);
4280 if (Status == EFI_SUCCESS){
4281 Status = ShellDeleteFile(&FileHandle);
4282 }
4283 }
4284
4285 return(Status);
4286
4287}
Qiu Shumin0960ba12014-09-17 07:58:31 +00004288
4289/**
4290 Cleans off all the quotes in the string.
4291
4292 @param[in] OriginalString pointer to the string to be cleaned.
4293 @param[out] CleanString The new string with all quotes removed.
4294 Memory allocated in the function and free
4295 by caller.
4296
4297 @retval EFI_SUCCESS The operation was successful.
4298**/
4299EFI_STATUS
4300EFIAPI
4301InternalShellStripQuotes (
4302 IN CONST CHAR16 *OriginalString,
4303 OUT CHAR16 **CleanString
4304 )
4305{
4306 CHAR16 *Walker;
4307
4308 if (OriginalString == NULL || CleanString == NULL) {
4309 return EFI_INVALID_PARAMETER;
4310 }
4311
4312 *CleanString = AllocateCopyPool (StrSize (OriginalString), OriginalString);
4313 if (*CleanString == NULL) {
4314 return EFI_OUT_OF_RESOURCES;
4315 }
4316
4317 for (Walker = *CleanString; Walker != NULL && *Walker != CHAR_NULL ; Walker++) {
4318 if (*Walker == L'\"') {
4319 CopyMem(Walker, Walker+1, StrSize(Walker) - sizeof(Walker[0]));
4320 }
4321 }
4322
4323 return EFI_SUCCESS;
4324}
4325