blob: 3aeaf4bc57cf9d626f38ba1c48a5e6f703abbc1c [file] [log] [blame]
jcarsey94b17fa2009-05-07 18:46:18 +00001/** @file
2 Provides interface to shell functionality for shell commands and applications.
3
4Copyright (c) 2006 - 2009, Intel Corporation
5All rights reserved. This program and the accompanying materials
6are licensed and made available under the terms and conditions of the BSD License
7which accompanies this distribution. The full text of the license may be found at
8http://opensource.org/licenses/bsd-license.php
9
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13**/
14
15#include <Uefi.h>
16#include <Library/ShellLib.h>
17#include <Library/UefiBootServicesTableLib.h>
18#include <Library/BaseLib.h>
19#include <Library/BaseMemoryLib.h>
20#include <Library/DebugLib.h>
21#include <Library/MemoryAllocationLib.h>
22#include <Library/DevicePathLib.h>
jcarseyd2b45642009-05-11 18:02:16 +000023#include <Library/PcdLib.h>
24#include <Library/FileHandleLib.h>
jcarseyb1f95a02009-06-16 00:23:19 +000025#include <Library/PrintLib.h>
26#include <Library/UefiLib.h>
jcarsey2247dde2009-11-09 18:08:58 +000027#include <Library/HiiLib.h>
jcarseyb1f95a02009-06-16 00:23:19 +000028
jcarsey94b17fa2009-05-07 18:46:18 +000029#include <Protocol/EfiShellEnvironment2.h>
30#include <Protocol/EfiShellInterface.h>
31#include <Protocol/EfiShell.h>
32#include <Protocol/EfiShellParameters.h>
33#include <Protocol/SimpleFileSystem.h>
34
jcarseyb1f95a02009-06-16 00:23:19 +000035#include "UefiShellLib.h"
jcarseyd2b45642009-05-11 18:02:16 +000036
jcarsey94b17fa2009-05-07 18:46:18 +000037#define MAX_FILE_NAME_LEN 522 // (20 * (6+5+2))+1) unicode characters from EFI FAT spec (doubled for bytes)
38#define FIND_XXXXX_FILE_BUFFER_SIZE (SIZE_OF_EFI_FILE_INFO + MAX_FILE_NAME_LEN)
39
jcarseyd2b45642009-05-11 18:02:16 +000040//
41// This is not static since it's extern in the .h file
42//
43SHELL_PARAM_ITEM EmptyParamList[] = {
44 {NULL, TypeMax}
45 };
46
47//
48// Static file globals for the shell library
49//
50STATIC EFI_SHELL_ENVIRONMENT2 *mEfiShellEnvironment2;
51STATIC EFI_SHELL_INTERFACE *mEfiShellInterface;
52STATIC EFI_SHELL_PROTOCOL *mEfiShellProtocol;
53STATIC EFI_SHELL_PARAMETERS_PROTOCOL *mEfiShellParametersProtocol;
54STATIC EFI_HANDLE mEfiShellEnvironment2Handle;
55STATIC FILE_HANDLE_FUNCTION_MAP FileFunctionMap;
jcarsey2247dde2009-11-09 18:08:58 +000056STATIC UINTN mTotalParameterCount;
57
58/**
59 Check if a Unicode character is a hexadecimal character.
60
61 This internal function checks if a Unicode character is a
62 decimal character. The valid hexadecimal character is
63 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.
64
65
66 @param Char The character to check against.
67
68 @retval TRUE If the Char is a hexadecmial character.
69 @retval FALSE If the Char is not a hexadecmial character.
70
71**/
72BOOLEAN
73EFIAPI
74ShellInternalIsHexaDecimalDigitCharacter (
75 IN CHAR16 Char
76 ) {
77 return (BOOLEAN) ((Char >= L'0' && Char <= L'9') || (Char >= L'A' && Char <= L'F') || (Char >= L'a' && Char <= L'f'));
78}
jcarsey94b17fa2009-05-07 18:46:18 +000079
80/**
81 helper function to find ShellEnvironment2 for constructor
82**/
83EFI_STATUS
84EFIAPI
85ShellFindSE2 (
86 IN EFI_HANDLE ImageHandle
jcarsey2247dde2009-11-09 18:08:58 +000087 ) {
jcarsey94b17fa2009-05-07 18:46:18 +000088 EFI_STATUS Status;
89 EFI_HANDLE *Buffer;
90 UINTN BufferSize;
91 UINTN HandleIndex;
92
93 BufferSize = 0;
94 Buffer = NULL;
95 Status = gBS->OpenProtocol(ImageHandle,
96 &gEfiShellEnvironment2Guid,
97 (VOID **)&mEfiShellEnvironment2,
98 ImageHandle,
99 NULL,
100 EFI_OPEN_PROTOCOL_GET_PROTOCOL
101 );
102 //
103 // look for the mEfiShellEnvironment2 protocol at a higher level
104 //
jcarsey9eb53ac2009-07-08 17:26:58 +0000105 if (EFI_ERROR (Status) || !(CompareGuid (&mEfiShellEnvironment2->SESGuid, &gEfiShellEnvironment2ExtGuid) != FALSE)){
jcarsey94b17fa2009-05-07 18:46:18 +0000106 //
107 // figure out how big of a buffer we need.
108 //
109 Status = gBS->LocateHandle (ByProtocol,
110 &gEfiShellEnvironment2Guid,
111 NULL, // ignored for ByProtocol
112 &BufferSize,
113 Buffer
114 );
jcarsey2247dde2009-11-09 18:08:58 +0000115 //
116 // maybe it's not there???
117 //
118 if (Status == EFI_BUFFER_TOO_SMALL) {
119 Buffer = (EFI_HANDLE*)AllocatePool(BufferSize);
120 ASSERT(Buffer != NULL);
121 Status = gBS->LocateHandle (ByProtocol,
122 &gEfiShellEnvironment2Guid,
123 NULL, // ignored for ByProtocol
124 &BufferSize,
125 Buffer
126 );
127 }
jcarsey94b17fa2009-05-07 18:46:18 +0000128 if (!EFI_ERROR (Status)) {
129 //
130 // now parse the list of returned handles
131 //
132 Status = EFI_NOT_FOUND;
133 for (HandleIndex = 0; HandleIndex < (BufferSize/sizeof(Buffer[0])); HandleIndex++) {
134 Status = gBS->OpenProtocol(Buffer[HandleIndex],
135 &gEfiShellEnvironment2Guid,
136 (VOID **)&mEfiShellEnvironment2,
137 ImageHandle,
138 NULL,
139 EFI_OPEN_PROTOCOL_GET_PROTOCOL
140 );
jcarsey9eb53ac2009-07-08 17:26:58 +0000141 if (CompareGuid (&mEfiShellEnvironment2->SESGuid, &gEfiShellEnvironment2ExtGuid) != FALSE) {
jcarsey94b17fa2009-05-07 18:46:18 +0000142 mEfiShellEnvironment2Handle = Buffer[HandleIndex];
143 Status = EFI_SUCCESS;
144 break;
145 }
146 }
147 }
148 }
149 if (Buffer != NULL) {
150 FreePool (Buffer);
151 }
152 return (Status);
153}
154
jcarsey94b17fa2009-05-07 18:46:18 +0000155EFI_STATUS
156EFIAPI
jcarseyd2b45642009-05-11 18:02:16 +0000157ShellLibConstructorWorker (
jcarsey94b17fa2009-05-07 18:46:18 +0000158 IN EFI_HANDLE ImageHandle,
159 IN EFI_SYSTEM_TABLE *SystemTable
jcarsey2247dde2009-11-09 18:08:58 +0000160 ) {
jcarsey94b17fa2009-05-07 18:46:18 +0000161 EFI_STATUS Status;
162
jcarsey94b17fa2009-05-07 18:46:18 +0000163 //
jcarsey2247dde2009-11-09 18:08:58 +0000164 // Set the parameter count to an invalid number
165 //
166 mTotalParameterCount = (UINTN)(-1);
167
168 //
jcarsey94b17fa2009-05-07 18:46:18 +0000169 // UEFI 2.0 shell interfaces (used preferentially)
170 //
171 Status = gBS->OpenProtocol(ImageHandle,
172 &gEfiShellProtocolGuid,
173 (VOID **)&mEfiShellProtocol,
174 ImageHandle,
175 NULL,
176 EFI_OPEN_PROTOCOL_GET_PROTOCOL
177 );
178 if (EFI_ERROR(Status)) {
179 mEfiShellProtocol = NULL;
180 }
181 Status = gBS->OpenProtocol(ImageHandle,
182 &gEfiShellParametersProtocolGuid,
183 (VOID **)&mEfiShellParametersProtocol,
184 ImageHandle,
185 NULL,
186 EFI_OPEN_PROTOCOL_GET_PROTOCOL
187 );
188 if (EFI_ERROR(Status)) {
189 mEfiShellParametersProtocol = NULL;
190 }
191
192 if (mEfiShellParametersProtocol == NULL || mEfiShellProtocol == NULL) {
193 //
194 // Moved to seperate function due to complexity
195 //
196 Status = ShellFindSE2(ImageHandle);
197
198 if (EFI_ERROR(Status)) {
199 DEBUG((DEBUG_ERROR, "Status: 0x%08x\r\n", Status));
200 mEfiShellEnvironment2 = NULL;
201 }
202 Status = gBS->OpenProtocol(ImageHandle,
203 &gEfiShellInterfaceGuid,
204 (VOID **)&mEfiShellInterface,
205 ImageHandle,
206 NULL,
207 EFI_OPEN_PROTOCOL_GET_PROTOCOL
208 );
209 if (EFI_ERROR(Status)) {
210 mEfiShellInterface = NULL;
211 }
212 }
213 //
214 // only success getting 2 of either the old or new, but no 1/2 and 1/2
215 //
216 if ((mEfiShellEnvironment2 != NULL && mEfiShellInterface != NULL) ||
217 (mEfiShellProtocol != NULL && mEfiShellParametersProtocol != NULL) ) {
jcarseyd2b45642009-05-11 18:02:16 +0000218 if (mEfiShellProtocol != NULL) {
219 FileFunctionMap.GetFileInfo = mEfiShellProtocol->GetFileInfo;
220 FileFunctionMap.SetFileInfo = mEfiShellProtocol->SetFileInfo;
221 FileFunctionMap.ReadFile = mEfiShellProtocol->ReadFile;
222 FileFunctionMap.WriteFile = mEfiShellProtocol->WriteFile;
223 FileFunctionMap.CloseFile = mEfiShellProtocol->CloseFile;
224 FileFunctionMap.DeleteFile = mEfiShellProtocol->DeleteFile;
225 FileFunctionMap.GetFilePosition = mEfiShellProtocol->GetFilePosition;
226 FileFunctionMap.SetFilePosition = mEfiShellProtocol->SetFilePosition;
227 FileFunctionMap.FlushFile = mEfiShellProtocol->FlushFile;
228 FileFunctionMap.GetFileSize = mEfiShellProtocol->GetFileSize;
229 } else {
230 FileFunctionMap.GetFileInfo = FileHandleGetInfo;
231 FileFunctionMap.SetFileInfo = FileHandleSetInfo;
232 FileFunctionMap.ReadFile = FileHandleRead;
233 FileFunctionMap.WriteFile = FileHandleWrite;
234 FileFunctionMap.CloseFile = FileHandleClose;
235 FileFunctionMap.DeleteFile = FileHandleDelete;
236 FileFunctionMap.GetFilePosition = FileHandleGetPosition;
237 FileFunctionMap.SetFilePosition = FileHandleSetPosition;
238 FileFunctionMap.FlushFile = FileHandleFlush;
239 FileFunctionMap.GetFileSize = FileHandleGetSize;
240 }
jcarsey94b17fa2009-05-07 18:46:18 +0000241 return (EFI_SUCCESS);
242 }
243 return (EFI_NOT_FOUND);
244}
jcarseyd2b45642009-05-11 18:02:16 +0000245/**
246 Constructor for the Shell library.
247
248 Initialize the library and determine if the underlying is a UEFI Shell 2.0 or an EFI shell.
249
250 @param ImageHandle the image handle of the process
251 @param SystemTable the EFI System Table pointer
252
253 @retval EFI_SUCCESS the initialization was complete sucessfully
254 @return others an error ocurred during initialization
255**/
256EFI_STATUS
257EFIAPI
258ShellLibConstructor (
259 IN EFI_HANDLE ImageHandle,
260 IN EFI_SYSTEM_TABLE *SystemTable
jcarsey2247dde2009-11-09 18:08:58 +0000261 ) {
jcarseyd2b45642009-05-11 18:02:16 +0000262
263
264 mEfiShellEnvironment2 = NULL;
265 mEfiShellProtocol = NULL;
266 mEfiShellParametersProtocol = NULL;
267 mEfiShellInterface = NULL;
268 mEfiShellEnvironment2Handle = NULL;
269
jcarseyd2b45642009-05-11 18:02:16 +0000270 //
271 // verify that auto initialize is not set false
272 //
273 if (PcdGetBool(PcdShellLibAutoInitialize) == 0) {
274 return (EFI_SUCCESS);
275 }
276
277 return (ShellLibConstructorWorker(ImageHandle, SystemTable));
278}
jcarsey94b17fa2009-05-07 18:46:18 +0000279
280/**
281 Destructory for the library. free any resources.
282**/
283EFI_STATUS
284EFIAPI
285ShellLibDestructor (
286 IN EFI_HANDLE ImageHandle,
287 IN EFI_SYSTEM_TABLE *SystemTable
jcarsey2247dde2009-11-09 18:08:58 +0000288 ) {
jcarsey94b17fa2009-05-07 18:46:18 +0000289 if (mEfiShellEnvironment2 != NULL) {
290 gBS->CloseProtocol(mEfiShellEnvironment2Handle==NULL?ImageHandle:mEfiShellEnvironment2Handle,
291 &gEfiShellEnvironment2Guid,
292 ImageHandle,
293 NULL);
jcarseyd2b45642009-05-11 18:02:16 +0000294 mEfiShellEnvironment2 = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +0000295 }
296 if (mEfiShellInterface != NULL) {
297 gBS->CloseProtocol(ImageHandle,
298 &gEfiShellInterfaceGuid,
299 ImageHandle,
300 NULL);
jcarseyd2b45642009-05-11 18:02:16 +0000301 mEfiShellInterface = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +0000302 }
303 if (mEfiShellProtocol != NULL) {
304 gBS->CloseProtocol(ImageHandle,
305 &gEfiShellProtocolGuid,
306 ImageHandle,
jcarseyd2b45642009-05-11 18:02:16 +0000307 NULL);
308 mEfiShellProtocol = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +0000309 }
310 if (mEfiShellParametersProtocol != NULL) {
311 gBS->CloseProtocol(ImageHandle,
312 &gEfiShellParametersProtocolGuid,
313 ImageHandle,
314 NULL);
jcarseyd2b45642009-05-11 18:02:16 +0000315 mEfiShellParametersProtocol = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +0000316 }
jcarseyd2b45642009-05-11 18:02:16 +0000317 mEfiShellEnvironment2Handle = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +0000318 return (EFI_SUCCESS);
319}
jcarseyd2b45642009-05-11 18:02:16 +0000320
321/**
322 This function causes the shell library to initialize itself. If the shell library
323 is already initialized it will de-initialize all the current protocol poitners and
324 re-populate them again.
325
326 When the library is used with PcdShellLibAutoInitialize set to true this function
327 will return EFI_SUCCESS and perform no actions.
328
329 This function is intended for internal access for shell commands only.
330
331 @retval EFI_SUCCESS the initialization was complete sucessfully
332
333**/
334EFI_STATUS
335EFIAPI
336ShellInitialize (
337 ) {
338 //
339 // if auto initialize is not false then skip
340 //
341 if (PcdGetBool(PcdShellLibAutoInitialize) != 0) {
342 return (EFI_SUCCESS);
343 }
344
345 //
346 // deinit the current stuff
347 //
348 ASSERT_EFI_ERROR(ShellLibDestructor(gImageHandle, gST));
349
350 //
351 // init the new stuff
352 //
353 return (ShellLibConstructorWorker(gImageHandle, gST));
354}
355
jcarsey94b17fa2009-05-07 18:46:18 +0000356/**
357 This function will retrieve the information about the file for the handle
358 specified and store it in allocated pool memory.
359
qhuang869817bf2009-05-20 14:42:48 +0000360 This function allocates a buffer to store the file's information. It is the
361 caller's responsibility to free the buffer
jcarsey94b17fa2009-05-07 18:46:18 +0000362
363 @param FileHandle The file handle of the file for which information is
364 being requested.
365
366 @retval NULL information could not be retrieved.
367
368 @return the information about the file
369**/
370EFI_FILE_INFO*
371EFIAPI
372ShellGetFileInfo (
373 IN EFI_FILE_HANDLE FileHandle
jcarsey2247dde2009-11-09 18:08:58 +0000374 ) {
jcarseyd2b45642009-05-11 18:02:16 +0000375 return (FileFunctionMap.GetFileInfo(FileHandle));
jcarsey94b17fa2009-05-07 18:46:18 +0000376}
377
378/**
379 This function will set the information about the file for the opened handle
380 specified.
381
382 @param FileHandle The file handle of the file for which information
383 is being set
384
385 @param FileInfo The infotmation to set.
386
387 @retval EFI_SUCCESS The information was set.
388 @retval EFI_UNSUPPORTED The InformationType is not known.
389 @retval EFI_NO_MEDIA The device has no medium.
390 @retval EFI_DEVICE_ERROR The device reported an error.
391 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
392 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
393 @retval EFI_ACCESS_DENIED The file was opened read only.
394 @retval EFI_VOLUME_FULL The volume is full.
395**/
396EFI_STATUS
397EFIAPI
398ShellSetFileInfo (
399 IN EFI_FILE_HANDLE FileHandle,
400 IN EFI_FILE_INFO *FileInfo
jcarsey2247dde2009-11-09 18:08:58 +0000401 ) {
jcarseyd2b45642009-05-11 18:02:16 +0000402 return (FileFunctionMap.SetFileInfo(FileHandle, FileInfo));
jcarsey94b17fa2009-05-07 18:46:18 +0000403}
404
405 /**
406 This function will open a file or directory referenced by DevicePath.
407
408 This function opens a file with the open mode according to the file path. The
409 Attributes is valid only for EFI_FILE_MODE_CREATE.
410
411 @param FilePath on input the device path to the file. On output
412 the remaining device path.
413 @param DeviceHandle pointer to the system device handle.
414 @param FileHandle pointer to the file handle.
415 @param OpenMode the mode to open the file with.
416 @param Attributes the file's file attributes.
417
418 @retval EFI_SUCCESS The information was set.
419 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
420 @retval EFI_UNSUPPORTED Could not open the file path.
421 @retval EFI_NOT_FOUND The specified file could not be found on the
422 device or the file system could not be found on
423 the device.
424 @retval EFI_NO_MEDIA The device has no medium.
425 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
426 medium is no longer supported.
427 @retval EFI_DEVICE_ERROR The device reported an error.
428 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
429 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
430 @retval EFI_ACCESS_DENIED The file was opened read only.
431 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
432 file.
433 @retval EFI_VOLUME_FULL The volume is full.
434**/
435EFI_STATUS
436EFIAPI
437ShellOpenFileByDevicePath(
438 IN OUT EFI_DEVICE_PATH_PROTOCOL **FilePath,
439 OUT EFI_HANDLE *DeviceHandle,
440 OUT EFI_FILE_HANDLE *FileHandle,
441 IN UINT64 OpenMode,
442 IN UINT64 Attributes
jcarsey2247dde2009-11-09 18:08:58 +0000443 ) {
jcarsey94b17fa2009-05-07 18:46:18 +0000444 CHAR16 *FileName;
445 EFI_STATUS Status;
446 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *EfiSimpleFileSystemProtocol;
447 EFI_FILE_HANDLE LastHandle;
448
449 //
450 // ASERT for FileHandle, FilePath, and DeviceHandle being NULL
451 //
452 ASSERT(FilePath != NULL);
453 ASSERT(FileHandle != NULL);
454 ASSERT(DeviceHandle != NULL);
455 //
456 // which shell interface should we use
457 //
458 if (mEfiShellProtocol != NULL) {
459 //
460 // use UEFI Shell 2.0 method.
461 //
462 FileName = mEfiShellProtocol->GetFilePathFromDevicePath(*FilePath);
463 if (FileName == NULL) {
464 return (EFI_INVALID_PARAMETER);
465 }
466 Status = ShellOpenFileByName(FileName, FileHandle, OpenMode, Attributes);
467 FreePool(FileName);
468 return (Status);
jcarseyd2b45642009-05-11 18:02:16 +0000469 }
470
471
472 //
473 // use old shell method.
474 //
475 Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid,
476 FilePath,
477 DeviceHandle);
478 if (EFI_ERROR (Status)) {
479 return Status;
480 }
481 Status = gBS->OpenProtocol(*DeviceHandle,
482 &gEfiSimpleFileSystemProtocolGuid,
jcarseyb1f95a02009-06-16 00:23:19 +0000483 (VOID**)&EfiSimpleFileSystemProtocol,
jcarseyd2b45642009-05-11 18:02:16 +0000484 gImageHandle,
485 NULL,
486 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
487 if (EFI_ERROR (Status)) {
488 return Status;
489 }
490 Status = EfiSimpleFileSystemProtocol->OpenVolume(EfiSimpleFileSystemProtocol, FileHandle);
491 if (EFI_ERROR (Status)) {
492 FileHandle = NULL;
493 return Status;
494 }
495
496 //
497 // go down directories one node at a time.
498 //
499 while (!IsDevicePathEnd (*FilePath)) {
jcarsey94b17fa2009-05-07 18:46:18 +0000500 //
jcarseyd2b45642009-05-11 18:02:16 +0000501 // For file system access each node should be a file path component
jcarsey94b17fa2009-05-07 18:46:18 +0000502 //
jcarseyd2b45642009-05-11 18:02:16 +0000503 if (DevicePathType (*FilePath) != MEDIA_DEVICE_PATH ||
504 DevicePathSubType (*FilePath) != MEDIA_FILEPATH_DP
505 ) {
jcarsey94b17fa2009-05-07 18:46:18 +0000506 FileHandle = NULL;
jcarseyd2b45642009-05-11 18:02:16 +0000507 return (EFI_INVALID_PARAMETER);
jcarsey94b17fa2009-05-07 18:46:18 +0000508 }
jcarseyd2b45642009-05-11 18:02:16 +0000509 //
510 // Open this file path node
511 //
512 LastHandle = *FileHandle;
513 *FileHandle = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +0000514
515 //
jcarseyd2b45642009-05-11 18:02:16 +0000516 // Try to test opening an existing file
jcarsey94b17fa2009-05-07 18:46:18 +0000517 //
jcarseyd2b45642009-05-11 18:02:16 +0000518 Status = LastHandle->Open (
519 LastHandle,
520 FileHandle,
521 ((FILEPATH_DEVICE_PATH*)*FilePath)->PathName,
522 OpenMode &~EFI_FILE_MODE_CREATE,
523 0
524 );
jcarsey94b17fa2009-05-07 18:46:18 +0000525
jcarseyd2b45642009-05-11 18:02:16 +0000526 //
527 // see if the error was that it needs to be created
528 //
529 if ((EFI_ERROR (Status)) && (OpenMode != (OpenMode &~EFI_FILE_MODE_CREATE))) {
jcarsey94b17fa2009-05-07 18:46:18 +0000530 Status = LastHandle->Open (
531 LastHandle,
532 FileHandle,
533 ((FILEPATH_DEVICE_PATH*)*FilePath)->PathName,
jcarseyd2b45642009-05-11 18:02:16 +0000534 OpenMode,
535 Attributes
jcarsey94b17fa2009-05-07 18:46:18 +0000536 );
jcarsey94b17fa2009-05-07 18:46:18 +0000537 }
jcarseyd2b45642009-05-11 18:02:16 +0000538 //
539 // Close the last node
540 //
541 LastHandle->Close (LastHandle);
542
543 if (EFI_ERROR(Status)) {
544 return (Status);
545 }
546
547 //
548 // Get the next node
549 //
550 *FilePath = NextDevicePathNode (*FilePath);
jcarsey94b17fa2009-05-07 18:46:18 +0000551 }
jcarseyd2b45642009-05-11 18:02:16 +0000552 return (EFI_SUCCESS);
jcarsey94b17fa2009-05-07 18:46:18 +0000553}
554
555/**
556 This function will open a file or directory referenced by filename.
557
qhuang869817bf2009-05-20 14:42:48 +0000558 If return is EFI_SUCCESS, the Filehandle is the opened file's handle;
jcarsey94b17fa2009-05-07 18:46:18 +0000559 otherwise, the Filehandle is NULL. The Attributes is valid only for
560 EFI_FILE_MODE_CREATE.
561
562 if FileNAme is NULL then ASSERT()
563
564 @param FileName pointer to file name
565 @param FileHandle pointer to the file handle.
566 @param OpenMode the mode to open the file with.
567 @param Attributes the file's file attributes.
568
569 @retval EFI_SUCCESS The information was set.
570 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
571 @retval EFI_UNSUPPORTED Could not open the file path.
572 @retval EFI_NOT_FOUND The specified file could not be found on the
573 device or the file system could not be found
574 on the device.
575 @retval EFI_NO_MEDIA The device has no medium.
576 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
577 medium is no longer supported.
578 @retval EFI_DEVICE_ERROR The device reported an error.
579 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
580 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
581 @retval EFI_ACCESS_DENIED The file was opened read only.
582 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
583 file.
584 @retval EFI_VOLUME_FULL The volume is full.
585**/
586EFI_STATUS
587EFIAPI
588ShellOpenFileByName(
jcarseyb82bfcc2009-06-29 16:28:23 +0000589 IN CONST CHAR16 *FileName,
jcarsey94b17fa2009-05-07 18:46:18 +0000590 OUT EFI_FILE_HANDLE *FileHandle,
591 IN UINT64 OpenMode,
592 IN UINT64 Attributes
jcarsey2247dde2009-11-09 18:08:58 +0000593 ) {
jcarsey94b17fa2009-05-07 18:46:18 +0000594 EFI_HANDLE DeviceHandle;
595 EFI_DEVICE_PATH_PROTOCOL *FilePath;
jcarseyb1f95a02009-06-16 00:23:19 +0000596 EFI_STATUS Status;
597 EFI_FILE_INFO *FileInfo;
jcarsey94b17fa2009-05-07 18:46:18 +0000598
599 //
600 // ASSERT if FileName is NULL
601 //
602 ASSERT(FileName != NULL);
603
604 if (mEfiShellProtocol != NULL) {
605 //
606 // Use UEFI Shell 2.0 method
607 //
jcarseyb1f95a02009-06-16 00:23:19 +0000608 Status = mEfiShellProtocol->OpenFileByName(FileName,
609 FileHandle,
610 OpenMode);
jcarsey2247dde2009-11-09 18:08:58 +0000611 if (!EFI_ERROR(Status) && ((OpenMode & EFI_FILE_MODE_CREATE) != 0)){
612 FileInfo = FileFunctionMap.GetFileInfo(*FileHandle);
jcarseyb1f95a02009-06-16 00:23:19 +0000613 ASSERT(FileInfo != NULL);
614 FileInfo->Attribute = Attributes;
jcarsey2247dde2009-11-09 18:08:58 +0000615 Status = FileFunctionMap.SetFileInfo(*FileHandle, FileInfo);
616 FreePool(FileInfo);
jcarseyb1f95a02009-06-16 00:23:19 +0000617 }
618 return (Status);
jcarsey94b17fa2009-05-07 18:46:18 +0000619 }
620 //
621 // Using EFI Shell version
622 // this means convert name to path and call that function
623 // since this will use EFI method again that will open it.
624 //
625 ASSERT(mEfiShellEnvironment2 != NULL);
jcarseyb82bfcc2009-06-29 16:28:23 +0000626 FilePath = mEfiShellEnvironment2->NameToPath ((CHAR16*)FileName);
jcarsey94b17fa2009-05-07 18:46:18 +0000627 if (FileDevicePath != NULL) {
628 return (ShellOpenFileByDevicePath(&FilePath,
629 &DeviceHandle,
630 FileHandle,
631 OpenMode,
632 Attributes ));
633 }
634 return (EFI_DEVICE_ERROR);
635}
636/**
637 This function create a directory
638
639 If return is EFI_SUCCESS, the Filehandle is the opened directory's handle;
640 otherwise, the Filehandle is NULL. If the directory already existed, this
641 function opens the existing directory.
642
643 @param DirectoryName pointer to directory name
644 @param FileHandle pointer to the file handle.
645
646 @retval EFI_SUCCESS The information was set.
647 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
648 @retval EFI_UNSUPPORTED Could not open the file path.
649 @retval EFI_NOT_FOUND The specified file could not be found on the
650 device or the file system could not be found
651 on the device.
652 @retval EFI_NO_MEDIA The device has no medium.
653 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
654 medium is no longer supported.
655 @retval EFI_DEVICE_ERROR The device reported an error.
656 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
657 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
658 @retval EFI_ACCESS_DENIED The file was opened read only.
659 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
660 file.
661 @retval EFI_VOLUME_FULL The volume is full.
662 @sa ShellOpenFileByName
663**/
664EFI_STATUS
665EFIAPI
666ShellCreateDirectory(
jcarseyb82bfcc2009-06-29 16:28:23 +0000667 IN CONST CHAR16 *DirectoryName,
jcarsey94b17fa2009-05-07 18:46:18 +0000668 OUT EFI_FILE_HANDLE *FileHandle
jcarsey2247dde2009-11-09 18:08:58 +0000669 ) {
670 if (mEfiShellProtocol != NULL) {
671 //
672 // Use UEFI Shell 2.0 method
673 //
674 return (mEfiShellProtocol->CreateFile(DirectoryName,
675 EFI_FILE_DIRECTORY,
676 FileHandle
677 ));
678 } else {
679 return (ShellOpenFileByName(DirectoryName,
680 FileHandle,
681 EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE,
682 EFI_FILE_DIRECTORY
683 ));
684 }
jcarsey94b17fa2009-05-07 18:46:18 +0000685}
686
687/**
688 This function reads information from an opened file.
689
690 If FileHandle is not a directory, the function reads the requested number of
qhuang869817bf2009-05-20 14:42:48 +0000691 bytes from the file at the file's current position and returns them in Buffer.
jcarsey94b17fa2009-05-07 18:46:18 +0000692 If the read goes beyond the end of the file, the read length is truncated to the
qhuang869817bf2009-05-20 14:42:48 +0000693 end of the file. The file's current position is increased by the number of bytes
jcarsey94b17fa2009-05-07 18:46:18 +0000694 returned. If FileHandle is a directory, the function reads the directory entry
qhuang869817bf2009-05-20 14:42:48 +0000695 at the file's current position and returns the entry in Buffer. If the Buffer
jcarsey94b17fa2009-05-07 18:46:18 +0000696 is not large enough to hold the current directory entry, then
697 EFI_BUFFER_TOO_SMALL is returned and the current file position is not updated.
698 BufferSize is set to be the size of the buffer needed to read the entry. On
699 success, the current position is updated to the next directory entry. If there
700 are no more directory entries, the read returns a zero-length buffer.
701 EFI_FILE_INFO is the structure returned as the directory entry.
702
703 @param FileHandle the opened file handle
704 @param BufferSize on input the size of buffer in bytes. on return
705 the number of bytes written.
706 @param Buffer the buffer to put read data into.
707
708 @retval EFI_SUCCESS Data was read.
709 @retval EFI_NO_MEDIA The device has no media.
710 @retval EFI_DEVICE_ERROR The device reported an error.
711 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
712 @retval EFI_BUFFER_TO_SMALL Buffer is too small. ReadSize contains required
713 size.
714
715**/
716EFI_STATUS
717EFIAPI
718ShellReadFile(
719 IN EFI_FILE_HANDLE FileHandle,
720 IN OUT UINTN *BufferSize,
721 OUT VOID *Buffer
jcarsey2247dde2009-11-09 18:08:58 +0000722 ) {
jcarseyd2b45642009-05-11 18:02:16 +0000723 return (FileFunctionMap.ReadFile(FileHandle, BufferSize, Buffer));
jcarsey94b17fa2009-05-07 18:46:18 +0000724}
725
726
727/**
728 Write data to a file.
729
730 This function writes the specified number of bytes to the file at the current
731 file position. The current file position is advanced the actual number of bytes
732 written, which is returned in BufferSize. Partial writes only occur when there
qhuang869817bf2009-05-20 14:42:48 +0000733 has been a data error during the write attempt (such as "volume space full").
jcarsey94b17fa2009-05-07 18:46:18 +0000734 The file is automatically grown to hold the data if required. Direct writes to
735 opened directories are not supported.
736
737 @param FileHandle The opened file for writing
738 @param BufferSize on input the number of bytes in Buffer. On output
739 the number of bytes written.
740 @param Buffer the buffer containing data to write is stored.
741
742 @retval EFI_SUCCESS Data was written.
743 @retval EFI_UNSUPPORTED Writes to an open directory are not supported.
744 @retval EFI_NO_MEDIA The device has no media.
745 @retval EFI_DEVICE_ERROR The device reported an error.
746 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
747 @retval EFI_WRITE_PROTECTED The device is write-protected.
748 @retval EFI_ACCESS_DENIED The file was open for read only.
749 @retval EFI_VOLUME_FULL The volume is full.
750**/
751EFI_STATUS
752EFIAPI
753ShellWriteFile(
754 IN EFI_FILE_HANDLE FileHandle,
755 IN OUT UINTN *BufferSize,
756 IN VOID *Buffer
jcarsey2247dde2009-11-09 18:08:58 +0000757 ) {
jcarseyd2b45642009-05-11 18:02:16 +0000758 return (FileFunctionMap.WriteFile(FileHandle, BufferSize, Buffer));
jcarsey94b17fa2009-05-07 18:46:18 +0000759}
760
761/**
762 Close an open file handle.
763
qhuang869817bf2009-05-20 14:42:48 +0000764 This function closes a specified file handle. All "dirty" cached file data is
jcarsey94b17fa2009-05-07 18:46:18 +0000765 flushed to the device, and the file is closed. In all cases the handle is
766 closed.
767
768@param FileHandle the file handle to close.
769
770@retval EFI_SUCCESS the file handle was closed sucessfully.
771**/
772EFI_STATUS
773EFIAPI
774ShellCloseFile (
775 IN EFI_FILE_HANDLE *FileHandle
jcarsey2247dde2009-11-09 18:08:58 +0000776 ) {
jcarseyd2b45642009-05-11 18:02:16 +0000777 return (FileFunctionMap.CloseFile(*FileHandle));
jcarsey94b17fa2009-05-07 18:46:18 +0000778}
779
780/**
781 Delete a file and close the handle
782
783 This function closes and deletes a file. In all cases the file handle is closed.
784 If the file cannot be deleted, the warning code EFI_WARN_DELETE_FAILURE is
785 returned, but the handle is still closed.
786
787 @param FileHandle the file handle to delete
788
789 @retval EFI_SUCCESS the file was closed sucessfully
790 @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not
791 deleted
792 @retval INVALID_PARAMETER One of the parameters has an invalid value.
793**/
794EFI_STATUS
795EFIAPI
796ShellDeleteFile (
797 IN EFI_FILE_HANDLE *FileHandle
jcarsey2247dde2009-11-09 18:08:58 +0000798 ) {
jcarseyd2b45642009-05-11 18:02:16 +0000799 return (FileFunctionMap.DeleteFile(*FileHandle));
jcarsey94b17fa2009-05-07 18:46:18 +0000800}
801
802/**
803 Set the current position in a file.
804
805 This function sets the current file position for the handle to the position
806 supplied. With the exception of seeking to position 0xFFFFFFFFFFFFFFFF, only
807 absolute positioning is supported, and seeking past the end of the file is
808 allowed (a subsequent write would grow the file). Seeking to position
809 0xFFFFFFFFFFFFFFFF causes the current position to be set to the end of the file.
810 If FileHandle is a directory, the only position that may be set is zero. This
811 has the effect of starting the read process of the directory entries over.
812
813 @param FileHandle The file handle on which the position is being set
814 @param Position Byte position from begining of file
815
816 @retval EFI_SUCCESS Operation completed sucessfully.
817 @retval EFI_UNSUPPORTED the seek request for non-zero is not valid on
818 directories.
819 @retval INVALID_PARAMETER One of the parameters has an invalid value.
820**/
821EFI_STATUS
822EFIAPI
823ShellSetFilePosition (
824 IN EFI_FILE_HANDLE FileHandle,
825 IN UINT64 Position
jcarsey2247dde2009-11-09 18:08:58 +0000826 ) {
jcarseyd2b45642009-05-11 18:02:16 +0000827 return (FileFunctionMap.SetFilePosition(FileHandle, Position));
jcarsey94b17fa2009-05-07 18:46:18 +0000828}
829
830/**
831 Gets a file's current position
832
833 This function retrieves the current file position for the file handle. For
834 directories, the current file position has no meaning outside of the file
835 system driver and as such the operation is not supported. An error is returned
836 if FileHandle is a directory.
837
838 @param FileHandle The open file handle on which to get the position.
839 @param Position Byte position from begining of file.
840
841 @retval EFI_SUCCESS the operation completed sucessfully.
842 @retval INVALID_PARAMETER One of the parameters has an invalid value.
843 @retval EFI_UNSUPPORTED the request is not valid on directories.
844**/
845EFI_STATUS
846EFIAPI
847ShellGetFilePosition (
848 IN EFI_FILE_HANDLE FileHandle,
849 OUT UINT64 *Position
jcarsey2247dde2009-11-09 18:08:58 +0000850 ) {
jcarseyd2b45642009-05-11 18:02:16 +0000851 return (FileFunctionMap.GetFilePosition(FileHandle, Position));
jcarsey94b17fa2009-05-07 18:46:18 +0000852}
853/**
854 Flushes data on a file
855
856 This function flushes all modified data associated with a file to a device.
857
858 @param FileHandle The file handle on which to flush data
859
860 @retval EFI_SUCCESS The data was flushed.
861 @retval EFI_NO_MEDIA The device has no media.
862 @retval EFI_DEVICE_ERROR The device reported an error.
863 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
864 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
865 @retval EFI_ACCESS_DENIED The file was opened for read only.
866**/
867EFI_STATUS
868EFIAPI
869ShellFlushFile (
870 IN EFI_FILE_HANDLE FileHandle
jcarsey2247dde2009-11-09 18:08:58 +0000871 ) {
jcarseyd2b45642009-05-11 18:02:16 +0000872 return (FileFunctionMap.FlushFile(FileHandle));
jcarsey94b17fa2009-05-07 18:46:18 +0000873}
874
875/**
876 Retrieves the first file from a directory
877
qhuang869817bf2009-05-20 14:42:48 +0000878 This function opens a directory and gets the first file's info in the
jcarsey94b17fa2009-05-07 18:46:18 +0000879 directory. Caller can use ShellFindNextFile() to get other files. When
880 complete the caller is responsible for calling FreePool() on Buffer.
881
882 @param DirHandle The file handle of the directory to search
883 @param Buffer Pointer to buffer for file's information
884
885 @retval EFI_SUCCESS Found the first file.
886 @retval EFI_NOT_FOUND Cannot find the directory.
887 @retval EFI_NO_MEDIA The device has no media.
888 @retval EFI_DEVICE_ERROR The device reported an error.
889 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
890 @return Others status of ShellGetFileInfo, ShellSetFilePosition,
891 or ShellReadFile
892**/
893EFI_STATUS
894EFIAPI
895ShellFindFirstFile (
896 IN EFI_FILE_HANDLE DirHandle,
jcarseyd2b45642009-05-11 18:02:16 +0000897 OUT EFI_FILE_INFO **Buffer
jcarsey2247dde2009-11-09 18:08:58 +0000898 ) {
jcarsey94b17fa2009-05-07 18:46:18 +0000899 //
jcarseyd2b45642009-05-11 18:02:16 +0000900 // pass to file handle lib
jcarsey94b17fa2009-05-07 18:46:18 +0000901 //
jcarseyd2b45642009-05-11 18:02:16 +0000902 return (FileHandleFindFirstFile(DirHandle, Buffer));
jcarsey94b17fa2009-05-07 18:46:18 +0000903}
904/**
905 Retrieves the next file in a directory.
906
907 To use this function, caller must call the LibFindFirstFile() to get the
908 first file, and then use this function get other files. This function can be
909 called for several times to get each file's information in the directory. If
910 the call of ShellFindNextFile() got the last file in the directory, the next
911 call of this function has no file to get. *NoFile will be set to TRUE and the
912 Buffer memory will be automatically freed.
913
914 @param DirHandle the file handle of the directory
915 @param Buffer pointer to buffer for file's information
916 @param NoFile pointer to boolean when last file is found
917
918 @retval EFI_SUCCESS Found the next file, or reached last file
919 @retval EFI_NO_MEDIA The device has no media.
920 @retval EFI_DEVICE_ERROR The device reported an error.
921 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
922**/
923EFI_STATUS
924EFIAPI
925ShellFindNextFile(
926 IN EFI_FILE_HANDLE DirHandle,
927 OUT EFI_FILE_INFO *Buffer,
928 OUT BOOLEAN *NoFile
jcarsey2247dde2009-11-09 18:08:58 +0000929 ) {
jcarsey94b17fa2009-05-07 18:46:18 +0000930 //
jcarseyd2b45642009-05-11 18:02:16 +0000931 // pass to file handle lib
jcarsey94b17fa2009-05-07 18:46:18 +0000932 //
jcarseyd2b45642009-05-11 18:02:16 +0000933 return (FileHandleFindNextFile(DirHandle, Buffer, NoFile));
jcarsey94b17fa2009-05-07 18:46:18 +0000934}
935/**
936 Retrieve the size of a file.
937
938 if FileHandle is NULL then ASSERT()
939 if Size is NULL then ASSERT()
940
qhuang869817bf2009-05-20 14:42:48 +0000941 This function extracts the file size info from the FileHandle's EFI_FILE_INFO
jcarsey94b17fa2009-05-07 18:46:18 +0000942 data.
943
944 @param FileHandle file handle from which size is retrieved
945 @param Size pointer to size
946
947 @retval EFI_SUCCESS operation was completed sucessfully
948 @retval EFI_DEVICE_ERROR cannot access the file
949**/
950EFI_STATUS
951EFIAPI
952ShellGetFileSize (
953 IN EFI_FILE_HANDLE FileHandle,
954 OUT UINT64 *Size
jcarsey2247dde2009-11-09 18:08:58 +0000955 ) {
jcarseyd2b45642009-05-11 18:02:16 +0000956 return (FileFunctionMap.GetFileSize(FileHandle, Size));
jcarsey94b17fa2009-05-07 18:46:18 +0000957}
958/**
959 Retrieves the status of the break execution flag
960
961 this function is useful to check whether the application is being asked to halt by the shell.
962
963 @retval TRUE the execution break is enabled
964 @retval FALSE the execution break is not enabled
965**/
966BOOLEAN
967EFIAPI
968ShellGetExecutionBreakFlag(
969 VOID
970 )
971{
972 //
973 // Check for UEFI Shell 2.0 protocols
974 //
975 if (mEfiShellProtocol != NULL) {
976
977 //
978 // We are using UEFI Shell 2.0; see if the event has been triggered
979 //
980 if (gBS->CheckEvent(mEfiShellProtocol->ExecutionBreak) != EFI_SUCCESS) {
981 return (FALSE);
982 }
983 return (TRUE);
984 }
985
986 //
987 // using EFI Shell; call the function to check
988 //
989 ASSERT(mEfiShellEnvironment2 != NULL);
990 return (mEfiShellEnvironment2->GetExecutionBreak());
991}
992/**
993 return the value of an environment variable
994
995 this function gets the value of the environment variable set by the
996 ShellSetEnvironmentVariable function
997
998 @param EnvKey The key name of the environment variable.
999
1000 @retval NULL the named environment variable does not exist.
1001 @return != NULL pointer to the value of the environment variable
1002**/
1003CONST CHAR16*
1004EFIAPI
1005ShellGetEnvironmentVariable (
jcarsey9b3bf082009-06-23 21:15:07 +00001006 IN CONST CHAR16 *EnvKey
jcarsey94b17fa2009-05-07 18:46:18 +00001007 )
1008{
1009 //
1010 // Check for UEFI Shell 2.0 protocols
1011 //
1012 if (mEfiShellProtocol != NULL) {
1013 return (mEfiShellProtocol->GetEnv(EnvKey));
1014 }
1015
1016 //
1017 // ASSERT that we must have EFI shell
1018 //
1019 ASSERT(mEfiShellEnvironment2 != NULL);
1020
1021 //
1022 // using EFI Shell
1023 //
jcarsey9b3bf082009-06-23 21:15:07 +00001024 return (mEfiShellEnvironment2->GetEnv((CHAR16*)EnvKey));
jcarsey94b17fa2009-05-07 18:46:18 +00001025}
1026/**
1027 set the value of an environment variable
1028
1029This function changes the current value of the specified environment variable. If the
1030environment variable exists and the Value is an empty string, then the environment
1031variable is deleted. If the environment variable exists and the Value is not an empty
1032string, then the value of the environment variable is changed. If the environment
1033variable does not exist and the Value is an empty string, there is no action. If the
1034environment variable does not exist and the Value is a non-empty string, then the
1035environment variable is created and assigned the specified value.
1036
1037 This is not supported pre-UEFI Shell 2.0.
1038
1039 @param EnvKey The key name of the environment variable.
1040 @param EnvVal The Value of the environment variable
1041 @param Volatile Indicates whether the variable is non-volatile (FALSE) or volatile (TRUE).
1042
1043 @retval EFI_SUCCESS the operation was completed sucessfully
1044 @retval EFI_UNSUPPORTED This operation is not allowed in pre UEFI 2.0 Shell environments
1045**/
1046EFI_STATUS
1047EFIAPI
1048ShellSetEnvironmentVariable (
1049 IN CONST CHAR16 *EnvKey,
1050 IN CONST CHAR16 *EnvVal,
1051 IN BOOLEAN Volatile
1052 )
1053{
1054 //
1055 // Check for UEFI Shell 2.0 protocols
1056 //
1057 if (mEfiShellProtocol != NULL) {
1058 return (mEfiShellProtocol->SetEnv(EnvKey, EnvVal, Volatile));
1059 }
1060
1061 //
1062 // This feature does not exist under EFI shell
1063 //
1064 return (EFI_UNSUPPORTED);
1065}
1066/**
1067 cause the shell to parse and execute a command line.
1068
1069 This function creates a nested instance of the shell and executes the specified
1070command (CommandLine) with the specified environment (Environment). Upon return,
1071the status code returned by the specified command is placed in StatusCode.
1072If Environment is NULL, then the current environment is used and all changes made
1073by the commands executed will be reflected in the current environment. If the
1074Environment is non-NULL, then the changes made will be discarded.
1075The CommandLine is executed from the current working directory on the current
1076device.
1077
1078EnvironmentVariables and Status are only supported for UEFI Shell 2.0.
1079Output is only supported for pre-UEFI Shell 2.0
1080
1081 @param ImageHandle Parent image that is starting the operation
1082 @param CommandLine pointer to null terminated command line.
1083 @param Output true to display debug output. false to hide it.
1084 @param EnvironmentVariables optional pointer to array of environment variables
1085 in the form "x=y". if NULL current set is used.
1086 @param Status the status of the run command line.
1087
1088 @retval EFI_SUCCESS the operation completed sucessfully. Status
1089 contains the status code returned.
1090 @retval EFI_INVALID_PARAMETER a parameter contains an invalid value
1091 @retval EFI_OUT_OF_RESOURCES out of resources
1092 @retval EFI_UNSUPPORTED the operation is not allowed.
1093**/
1094EFI_STATUS
1095EFIAPI
1096ShellExecute (
1097 IN EFI_HANDLE *ParentHandle,
1098 IN CHAR16 *CommandLine OPTIONAL,
1099 IN BOOLEAN Output OPTIONAL,
1100 IN CHAR16 **EnvironmentVariables OPTIONAL,
1101 OUT EFI_STATUS *Status OPTIONAL
1102 )
1103{
1104 //
1105 // Check for UEFI Shell 2.0 protocols
1106 //
1107 if (mEfiShellProtocol != NULL) {
1108 //
1109 // Call UEFI Shell 2.0 version (not using Output parameter)
1110 //
1111 return (mEfiShellProtocol->Execute(ParentHandle,
1112 CommandLine,
1113 EnvironmentVariables,
1114 Status));
1115 }
1116 //
1117 // ASSERT that we must have EFI shell
1118 //
1119 ASSERT(mEfiShellEnvironment2 != NULL);
1120 //
1121 // Call EFI Shell version (not using EnvironmentVariables or Status parameters)
1122 // Due to oddity in the EFI shell we want to dereference the ParentHandle here
1123 //
1124 return (mEfiShellEnvironment2->Execute(*ParentHandle,
1125 CommandLine,
1126 Output));
1127}
1128/**
1129 Retreives the current directory path
1130
qhuang869817bf2009-05-20 14:42:48 +00001131 If the DeviceName is NULL, it returns the current device's current directory
jcarsey94b17fa2009-05-07 18:46:18 +00001132 name. If the DeviceName is not NULL, it returns the current directory name
1133 on specified drive.
1134
1135 @param DeviceName the name of the drive to get directory on
1136
1137 @retval NULL the directory does not exist
1138 @return != NULL the directory
1139**/
1140CONST CHAR16*
1141EFIAPI
1142ShellGetCurrentDir (
1143 IN CHAR16 *DeviceName OPTIONAL
1144 )
1145{
1146 //
1147 // Check for UEFI Shell 2.0 protocols
1148 //
1149 if (mEfiShellProtocol != NULL) {
1150 return (mEfiShellProtocol->GetCurDir(DeviceName));
1151 }
1152 //
1153 // ASSERT that we must have EFI shell
1154 //
1155 ASSERT(mEfiShellEnvironment2 != NULL);
1156 return (mEfiShellEnvironment2->CurDir(DeviceName));
1157}
1158/**
1159 sets (enabled or disabled) the page break mode
1160
1161 when page break mode is enabled the screen will stop scrolling
1162 and wait for operator input before scrolling a subsequent screen.
1163
1164 @param CurrentState TRUE to enable and FALSE to disable
1165**/
1166VOID
1167EFIAPI
1168ShellSetPageBreakMode (
1169 IN BOOLEAN CurrentState
1170 )
1171{
1172 //
1173 // check for enabling
1174 //
1175 if (CurrentState != 0x00) {
1176 //
1177 // check for UEFI Shell 2.0
1178 //
1179 if (mEfiShellProtocol != NULL) {
1180 //
1181 // Enable with UEFI 2.0 Shell
1182 //
1183 mEfiShellProtocol->EnablePageBreak();
1184 return;
1185 } else {
1186 //
1187 // ASSERT that must have EFI Shell
1188 //
1189 ASSERT(mEfiShellEnvironment2 != NULL);
1190 //
1191 // Enable with EFI Shell
1192 //
1193 mEfiShellEnvironment2->EnablePageBreak (DEFAULT_INIT_ROW, DEFAULT_AUTO_LF);
1194 return;
1195 }
1196 } else {
1197 //
1198 // check for UEFI Shell 2.0
1199 //
1200 if (mEfiShellProtocol != NULL) {
1201 //
1202 // Disable with UEFI 2.0 Shell
1203 //
1204 mEfiShellProtocol->DisablePageBreak();
1205 return;
1206 } else {
1207 //
1208 // ASSERT that must have EFI Shell
1209 //
1210 ASSERT(mEfiShellEnvironment2 != NULL);
1211 //
1212 // Disable with EFI Shell
1213 //
1214 mEfiShellEnvironment2->DisablePageBreak ();
1215 return;
1216 }
1217 }
1218}
1219
1220///
1221/// version of EFI_SHELL_FILE_INFO struct, except has no CONST pointers.
1222/// This allows for the struct to be populated.
1223///
1224typedef struct {
jcarseyd2b45642009-05-11 18:02:16 +00001225 LIST_ENTRY Link;
jcarsey94b17fa2009-05-07 18:46:18 +00001226 EFI_STATUS Status;
1227 CHAR16 *FullName;
1228 CHAR16 *FileName;
1229 EFI_FILE_HANDLE Handle;
1230 EFI_FILE_INFO *Info;
1231} EFI_SHELL_FILE_INFO_NO_CONST;
1232
1233/**
1234 Converts a EFI shell list of structures to the coresponding UEFI Shell 2.0 type of list.
1235
1236 if OldStyleFileList is NULL then ASSERT()
1237
1238 this function will convert a SHELL_FILE_ARG based list into a callee allocated
1239 EFI_SHELL_FILE_INFO based list. it is up to the caller to free the memory via
1240 the ShellCloseFileMetaArg function.
1241
jcarsey9b3bf082009-06-23 21:15:07 +00001242 @param[in] FileList the EFI shell list type
jcarseyb82bfcc2009-06-29 16:28:23 +00001243 @param[in,out] ListHead the list to add to
jcarsey94b17fa2009-05-07 18:46:18 +00001244
1245 @retval the resultant head of the double linked new format list;
1246**/
1247LIST_ENTRY*
1248EFIAPI
1249InternalShellConvertFileListType (
jcarsey9b3bf082009-06-23 21:15:07 +00001250 IN LIST_ENTRY *FileList,
1251 IN OUT LIST_ENTRY *ListHead
jcarsey125c2cf2009-11-18 21:36:50 +00001252 )
1253{
jcarsey94b17fa2009-05-07 18:46:18 +00001254 SHELL_FILE_ARG *OldInfo;
jcarsey9b3bf082009-06-23 21:15:07 +00001255 LIST_ENTRY *Link;
jcarsey94b17fa2009-05-07 18:46:18 +00001256 EFI_SHELL_FILE_INFO_NO_CONST *NewInfo;
1257
1258 //
jcarsey9b3bf082009-06-23 21:15:07 +00001259 // ASSERTs
jcarsey94b17fa2009-05-07 18:46:18 +00001260 //
jcarsey9b3bf082009-06-23 21:15:07 +00001261 ASSERT(FileList != NULL);
1262 ASSERT(ListHead != NULL);
jcarsey94b17fa2009-05-07 18:46:18 +00001263
1264 //
1265 // enumerate through each member of the old list and copy
1266 //
jcarseyd2b45642009-05-11 18:02:16 +00001267 for (Link = FileList->ForwardLink; Link != FileList; Link = Link->ForwardLink) {
jcarsey94b17fa2009-05-07 18:46:18 +00001268 OldInfo = CR (Link, SHELL_FILE_ARG, Link, SHELL_FILE_ARG_SIGNATURE);
1269
1270 //
1271 // make sure the old list was valid
1272 //
1273 ASSERT(OldInfo != NULL);
1274 ASSERT(OldInfo->Info != NULL);
1275 ASSERT(OldInfo->FullName != NULL);
1276 ASSERT(OldInfo->FileName != NULL);
1277
1278 //
1279 // allocate a new EFI_SHELL_FILE_INFO object
1280 //
1281 NewInfo = AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));
1282
1283 //
1284 // copy the simple items
1285 //
1286 NewInfo->Handle = OldInfo->Handle;
1287 NewInfo->Status = OldInfo->Status;
1288
jcarseyd2b45642009-05-11 18:02:16 +00001289 // old shell checks for 0 not NULL
1290 OldInfo->Handle = 0;
1291
jcarsey94b17fa2009-05-07 18:46:18 +00001292 //
1293 // allocate new space to copy strings and structure
1294 //
1295 NewInfo->FullName = AllocateZeroPool(StrSize(OldInfo->FullName));
1296 NewInfo->FileName = AllocateZeroPool(StrSize(OldInfo->FileName));
1297 NewInfo->Info = AllocateZeroPool((UINTN)OldInfo->Info->Size);
1298
1299 //
1300 // make sure all the memory allocations were sucessful
1301 //
1302 ASSERT(NewInfo->FullName != NULL);
1303 ASSERT(NewInfo->FileName != NULL);
1304 ASSERT(NewInfo->Info != NULL);
1305
1306 //
1307 // Copt the strings and structure
1308 //
1309 StrCpy(NewInfo->FullName, OldInfo->FullName);
1310 StrCpy(NewInfo->FileName, OldInfo->FileName);
1311 gBS->CopyMem (NewInfo->Info, OldInfo->Info, (UINTN)OldInfo->Info->Size);
1312
1313 //
1314 // add that to the list
1315 //
jcarsey9b3bf082009-06-23 21:15:07 +00001316 InsertTailList(ListHead, &NewInfo->Link);
jcarsey94b17fa2009-05-07 18:46:18 +00001317 }
1318 return (ListHead);
1319}
1320/**
1321 Opens a group of files based on a path.
1322
1323 This function uses the Arg to open all the matching files. Each matched
1324 file has a SHELL_FILE_ARG structure to record the file information. These
1325 structures are placed on the list ListHead. Users can get the SHELL_FILE_ARG
1326 structures from ListHead to access each file. This function supports wildcards
1327 and will process '?' and '*' as such. the list must be freed with a call to
1328 ShellCloseFileMetaArg().
1329
jcarsey5f7431d2009-07-10 18:06:01 +00001330 If you are NOT appending to an existing list *ListHead must be NULL. If
1331 *ListHead is NULL then it must be callee freed.
jcarsey94b17fa2009-05-07 18:46:18 +00001332
1333 @param Arg pointer to path string
1334 @param OpenMode mode to open files with
1335 @param ListHead head of linked list of results
1336
1337 @retval EFI_SUCCESS the operation was sucessful and the list head
1338 contains the list of opened files
1339 #retval EFI_UNSUPPORTED a previous ShellOpenFileMetaArg must be closed first.
1340 *ListHead is set to NULL.
1341 @return != EFI_SUCCESS the operation failed
1342
1343 @sa InternalShellConvertFileListType
1344**/
1345EFI_STATUS
1346EFIAPI
1347ShellOpenFileMetaArg (
1348 IN CHAR16 *Arg,
1349 IN UINT64 OpenMode,
1350 IN OUT EFI_SHELL_FILE_INFO **ListHead
1351 )
1352{
1353 EFI_STATUS Status;
jcarsey9b3bf082009-06-23 21:15:07 +00001354 LIST_ENTRY mOldStyleFileList;
jcarseyd2b45642009-05-11 18:02:16 +00001355
jcarsey94b17fa2009-05-07 18:46:18 +00001356 //
1357 // ASSERT that Arg and ListHead are not NULL
1358 //
1359 ASSERT(Arg != NULL);
1360 ASSERT(ListHead != NULL);
1361
1362 //
1363 // Check for UEFI Shell 2.0 protocols
1364 //
1365 if (mEfiShellProtocol != NULL) {
jcarsey5f7431d2009-07-10 18:06:01 +00001366 if (*ListHead == NULL) {
1367 *ListHead = (EFI_SHELL_FILE_INFO*)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));
1368 if (*ListHead == NULL) {
1369 return (EFI_OUT_OF_RESOURCES);
1370 }
1371 InitializeListHead(&((*ListHead)->Link));
1372 }
jcarsey2247dde2009-11-09 18:08:58 +00001373 Status = mEfiShellProtocol->OpenFileList(Arg,
jcarsey94b17fa2009-05-07 18:46:18 +00001374 OpenMode,
jcarsey2247dde2009-11-09 18:08:58 +00001375 ListHead);
1376 if (EFI_ERROR(Status)) {
1377 mEfiShellProtocol->RemoveDupInFileList(ListHead);
1378 } else {
1379 Status = mEfiShellProtocol->RemoveDupInFileList(ListHead);
1380 }
1381 return (Status);
jcarsey94b17fa2009-05-07 18:46:18 +00001382 }
1383
1384 //
1385 // ASSERT that we must have EFI shell
1386 //
1387 ASSERT(mEfiShellEnvironment2 != NULL);
1388
1389 //
jcarsey94b17fa2009-05-07 18:46:18 +00001390 // make sure the list head is initialized
1391 //
jcarsey9b3bf082009-06-23 21:15:07 +00001392 InitializeListHead(&mOldStyleFileList);
jcarsey94b17fa2009-05-07 18:46:18 +00001393
1394 //
1395 // Get the EFI Shell list of files
1396 //
jcarsey9b3bf082009-06-23 21:15:07 +00001397 Status = mEfiShellEnvironment2->FileMetaArg(Arg, &mOldStyleFileList);
jcarsey94b17fa2009-05-07 18:46:18 +00001398 if (EFI_ERROR(Status)) {
1399 *ListHead = NULL;
1400 return (Status);
1401 }
1402
jcarsey9b3bf082009-06-23 21:15:07 +00001403 if (*ListHead == NULL) {
1404 *ListHead = (EFI_SHELL_FILE_INFO *)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));
1405 if (*ListHead == NULL) {
1406 return (EFI_OUT_OF_RESOURCES);
1407 }
1408 }
1409
jcarsey94b17fa2009-05-07 18:46:18 +00001410 //
1411 // Convert that to equivalent of UEFI Shell 2.0 structure
1412 //
jcarsey9b3bf082009-06-23 21:15:07 +00001413 InternalShellConvertFileListType(&mOldStyleFileList, &(*ListHead)->Link);
jcarsey94b17fa2009-05-07 18:46:18 +00001414
1415 //
jcarseyd2b45642009-05-11 18:02:16 +00001416 // Free the EFI Shell version that was converted.
1417 //
jcarsey9b3bf082009-06-23 21:15:07 +00001418 mEfiShellEnvironment2->FreeFileList(&mOldStyleFileList);
jcarsey94b17fa2009-05-07 18:46:18 +00001419
1420 return (Status);
1421}
1422/**
1423 Free the linked list returned from ShellOpenFileMetaArg
1424
1425 if ListHead is NULL then ASSERT()
1426
1427 @param ListHead the pointer to free
1428
1429 @retval EFI_SUCCESS the operation was sucessful
1430**/
1431EFI_STATUS
1432EFIAPI
1433ShellCloseFileMetaArg (
1434 IN OUT EFI_SHELL_FILE_INFO **ListHead
1435 )
1436{
1437 LIST_ENTRY *Node;
1438
1439 //
1440 // ASSERT that ListHead is not NULL
1441 //
1442 ASSERT(ListHead != NULL);
1443
1444 //
1445 // Check for UEFI Shell 2.0 protocols
1446 //
1447 if (mEfiShellProtocol != NULL) {
1448 return (mEfiShellProtocol->FreeFileList(ListHead));
1449 } else {
1450 //
jcarsey94b17fa2009-05-07 18:46:18 +00001451 // Since this is EFI Shell version we need to free our internally made copy
1452 // of the list
1453 //
jcarsey9b3bf082009-06-23 21:15:07 +00001454 for ( Node = GetFirstNode(&(*ListHead)->Link)
1455 ; IsListEmpty(&(*ListHead)->Link) == FALSE
1456 ; Node = GetFirstNode(&(*ListHead)->Link)) {
jcarsey94b17fa2009-05-07 18:46:18 +00001457 RemoveEntryList(Node);
jcarseyd2b45642009-05-11 18:02:16 +00001458 ((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->Handle->Close(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->Handle);
jcarsey94b17fa2009-05-07 18:46:18 +00001459 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->FullName);
1460 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->FileName);
1461 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->Info);
1462 FreePool((EFI_SHELL_FILE_INFO_NO_CONST*)Node);
1463 }
1464 return EFI_SUCCESS;
1465 }
1466}
1467
jcarsey125c2cf2009-11-18 21:36:50 +00001468/**
1469 Find a file by searching the CWD and then the path.
1470
1471 if FileName is NULL then ASSERT.
1472
1473 if the return value is not NULL then the memory must be caller freed.
1474
1475 @param FileName Filename string.
1476
1477 @retval NULL the file was not found
1478 @return !NULL the full path to the file.
1479**/
1480CHAR16 *
1481EFIAPI
1482ShellFindFilePath (
1483 IN CONST CHAR16 *FileName
1484 )
1485{
1486 CONST CHAR16 *Path;
1487 EFI_FILE_HANDLE Handle;
1488 EFI_STATUS Status;
1489 CHAR16 *RetVal;
1490 CHAR16 *TestPath;
1491 CONST CHAR16 *Walker;
1492
1493 RetVal = NULL;
1494
1495 Path = ShellGetEnvironmentVariable(L"cwd");
1496 if (Path != NULL) {
jcarsey6c880292009-11-19 21:10:41 +00001497 TestPath = AllocateZeroPool((StrSize(Path) + StrSize(FileName)));
jcarsey125c2cf2009-11-18 21:36:50 +00001498 StrCpy(TestPath, Path);
1499 StrCat(TestPath, FileName);
1500 Status = ShellOpenFileByName(TestPath, &Handle, EFI_FILE_MODE_READ, 0);
1501 if (!EFI_ERROR(Status)){
1502 RetVal = StrnCatGrow(&RetVal, NULL, TestPath, 0);
1503 ShellCloseFile(&Handle);
1504 FreePool(TestPath);
1505 return (RetVal);
1506 }
1507 FreePool(TestPath);
1508 }
1509 Path = ShellGetEnvironmentVariable(L"path");
1510 if (Path != NULL) {
jcarsey6c880292009-11-19 21:10:41 +00001511 TestPath = AllocateZeroPool((StrSize(Path)+StrSize(FileName)));
jcarsey125c2cf2009-11-18 21:36:50 +00001512 Walker = (CHAR16*)Path;
1513 do {
1514 CopyMem(TestPath, Walker, StrSize(Walker));
1515 if (StrStr(TestPath, L";") != NULL) {
1516 *(StrStr(TestPath, L";")) = CHAR_NULL;
1517 }
1518 StrCat(TestPath, FileName);
1519 if (StrStr(Walker, L";") != NULL) {
1520 Walker = StrStr(Walker, L";") + 1;
1521 } else {
1522 Walker = NULL;
1523 }
1524 Status = ShellOpenFileByName(TestPath, &Handle, EFI_FILE_MODE_READ, 0);
1525 if (!EFI_ERROR(Status)){
1526 RetVal = StrnCatGrow(&RetVal, NULL, TestPath, 0);
1527 ShellCloseFile(&Handle);
1528 break;
1529 }
1530 } while (Walker != NULL && Walker[0] != CHAR_NULL);
1531 FreePool(TestPath);
1532 }
1533 return (RetVal);
1534}
1535
jcarsey94b17fa2009-05-07 18:46:18 +00001536typedef struct {
jcarsey9b3bf082009-06-23 21:15:07 +00001537 LIST_ENTRY Link;
jcarsey94b17fa2009-05-07 18:46:18 +00001538 CHAR16 *Name;
1539 ParamType Type;
1540 CHAR16 *Value;
1541 UINTN OriginalPosition;
1542} SHELL_PARAM_PACKAGE;
1543
1544/**
1545 Checks the list of valid arguments and returns TRUE if the item was found. If the
1546 return value is TRUE then the type parameter is set also.
1547
1548 if CheckList is NULL then ASSERT();
1549 if Name is NULL then ASSERT();
1550 if Type is NULL then ASSERT();
1551
1552 @param Type pointer to type of parameter if it was found
1553 @param Name pointer to Name of parameter found
1554 @param CheckList List to check against
1555
1556 @retval TRUE the Parameter was found. Type is valid.
1557 @retval FALSE the Parameter was not found. Type is not valid.
1558**/
1559BOOLEAN
1560EFIAPI
jcarseyd2b45642009-05-11 18:02:16 +00001561InternalIsOnCheckList (
jcarsey94b17fa2009-05-07 18:46:18 +00001562 IN CONST CHAR16 *Name,
1563 IN CONST SHELL_PARAM_ITEM *CheckList,
1564 OUT ParamType *Type
jcarsey2247dde2009-11-09 18:08:58 +00001565 ) {
jcarsey94b17fa2009-05-07 18:46:18 +00001566 SHELL_PARAM_ITEM *TempListItem;
1567
1568 //
1569 // ASSERT that all 3 pointer parameters aren't NULL
1570 //
1571 ASSERT(CheckList != NULL);
1572 ASSERT(Type != NULL);
1573 ASSERT(Name != NULL);
1574
1575 //
jcarseyd2b45642009-05-11 18:02:16 +00001576 // question mark and page break mode are always supported
1577 //
1578 if ((StrCmp(Name, L"-?") == 0) ||
1579 (StrCmp(Name, L"-b") == 0)
1580 ) {
1581 return (TRUE);
1582 }
1583
1584 //
jcarsey94b17fa2009-05-07 18:46:18 +00001585 // Enumerate through the list
1586 //
1587 for (TempListItem = (SHELL_PARAM_ITEM*)CheckList ; TempListItem->Name != NULL ; TempListItem++) {
1588 //
jcarsey9eb53ac2009-07-08 17:26:58 +00001589 // If the Type is TypeStart only check the first characters of the passed in param
1590 // If it matches set the type and return TRUE
jcarsey94b17fa2009-05-07 18:46:18 +00001591 //
jcarsey9eb53ac2009-07-08 17:26:58 +00001592 if (TempListItem->Type == TypeStart && StrnCmp(Name, TempListItem->Name, StrLen(TempListItem->Name)) == 0) {
1593 *Type = TempListItem->Type;
1594 return (TRUE);
1595 } else if (StrCmp(Name, TempListItem->Name) == 0) {
jcarsey94b17fa2009-05-07 18:46:18 +00001596 *Type = TempListItem->Type;
1597 return (TRUE);
1598 }
1599 }
jcarsey2247dde2009-11-09 18:08:58 +00001600
jcarsey94b17fa2009-05-07 18:46:18 +00001601 return (FALSE);
1602}
1603/**
jcarseyd2b45642009-05-11 18:02:16 +00001604 Checks the string for indicators of "flag" status. this is a leading '/', '-', or '+'
jcarsey94b17fa2009-05-07 18:46:18 +00001605
1606 @param Name pointer to Name of parameter found
1607
1608 @retval TRUE the Parameter is a flag.
1609 @retval FALSE the Parameter not a flag
1610**/
1611BOOLEAN
1612EFIAPI
jcarseyd2b45642009-05-11 18:02:16 +00001613InternalIsFlag (
jcarsey2247dde2009-11-09 18:08:58 +00001614 IN CONST CHAR16 *Name,
1615 IN BOOLEAN AlwaysAllowNumbers
jcarsey94b17fa2009-05-07 18:46:18 +00001616 )
1617{
1618 //
1619 // ASSERT that Name isn't NULL
1620 //
1621 ASSERT(Name != NULL);
1622
1623 //
jcarsey2247dde2009-11-09 18:08:58 +00001624 // If we accept numbers then dont return TRUE. (they will be values)
1625 //
1626 if (((Name[0] == L'-' || Name[0] == L'+') && ShellInternalIsHexaDecimalDigitCharacter(Name[1])) && AlwaysAllowNumbers == TRUE) {
1627 return (FALSE);
1628 }
1629
1630 //
jcarsey94b17fa2009-05-07 18:46:18 +00001631 // If the Name has a / or - as the first character return TRUE
1632 //
jcarseyd2b45642009-05-11 18:02:16 +00001633 if ((Name[0] == L'/') ||
1634 (Name[0] == L'-') ||
1635 (Name[0] == L'+')
1636 ) {
jcarsey94b17fa2009-05-07 18:46:18 +00001637 return (TRUE);
1638 }
1639 return (FALSE);
1640}
1641
1642/**
1643 Checks the command line arguments passed against the list of valid ones.
1644
1645 If no initialization is required, then return RETURN_SUCCESS.
1646
1647 @param CheckList pointer to list of parameters to check
1648 @param CheckPackage pointer to pointer to list checked values
1649 @param ProblemParam optional pointer to pointer to unicode string for
jcarseyd2b45642009-05-11 18:02:16 +00001650 the paramater that caused failure. If used then the
1651 caller is responsible for freeing the memory.
jcarsey94b17fa2009-05-07 18:46:18 +00001652 @param AutoPageBreak will automatically set PageBreakEnabled for "b" parameter
1653 @param Argc Count of parameters in Argv
1654 @param Argv pointer to array of parameters
1655
1656 @retval EFI_SUCCESS The operation completed sucessfully.
1657 @retval EFI_OUT_OF_RESOURCES A memory allocation failed
1658 @retval EFI_INVALID_PARAMETER A parameter was invalid
1659 @retval EFI_VOLUME_CORRUPTED the command line was corrupt. an argument was
1660 duplicated. the duplicated command line argument
1661 was returned in ProblemParam if provided.
1662 @retval EFI_NOT_FOUND a argument required a value that was missing.
1663 the invalid command line argument was returned in
1664 ProblemParam if provided.
1665**/
jcarsey2247dde2009-11-09 18:08:58 +00001666STATIC
jcarsey94b17fa2009-05-07 18:46:18 +00001667EFI_STATUS
1668EFIAPI
1669InternalCommandLineParse (
1670 IN CONST SHELL_PARAM_ITEM *CheckList,
1671 OUT LIST_ENTRY **CheckPackage,
1672 OUT CHAR16 **ProblemParam OPTIONAL,
1673 IN BOOLEAN AutoPageBreak,
1674 IN CONST CHAR16 **Argv,
jcarsey2247dde2009-11-09 18:08:58 +00001675 IN UINTN Argc,
1676 IN BOOLEAN AlwaysAllowNumbers
1677 ) {
jcarsey94b17fa2009-05-07 18:46:18 +00001678 UINTN LoopCounter;
jcarsey94b17fa2009-05-07 18:46:18 +00001679 ParamType CurrentItemType;
1680 SHELL_PARAM_PACKAGE *CurrentItemPackage;
jcarsey125c2cf2009-11-18 21:36:50 +00001681 UINTN GetItemValue;
1682 UINTN ValueSize;
jcarsey94b17fa2009-05-07 18:46:18 +00001683
1684 CurrentItemPackage = NULL;
jcarsey2247dde2009-11-09 18:08:58 +00001685 mTotalParameterCount = 0;
jcarsey125c2cf2009-11-18 21:36:50 +00001686 GetItemValue = 0;
1687 ValueSize = 0;
jcarsey94b17fa2009-05-07 18:46:18 +00001688
1689 //
1690 // If there is only 1 item we dont need to do anything
1691 //
1692 if (Argc <= 1) {
1693 *CheckPackage = NULL;
1694 return (EFI_SUCCESS);
1695 }
1696
1697 //
jcarsey2247dde2009-11-09 18:08:58 +00001698 // ASSERTs
1699 //
1700 ASSERT(CheckList != NULL);
1701 ASSERT(Argv != NULL);
1702
1703 //
jcarsey94b17fa2009-05-07 18:46:18 +00001704 // initialize the linked list
1705 //
1706 *CheckPackage = (LIST_ENTRY*)AllocateZeroPool(sizeof(LIST_ENTRY));
1707 InitializeListHead(*CheckPackage);
1708
1709 //
1710 // loop through each of the arguments
1711 //
1712 for (LoopCounter = 0 ; LoopCounter < Argc ; ++LoopCounter) {
1713 if (Argv[LoopCounter] == NULL) {
1714 //
1715 // do nothing for NULL argv
1716 //
jcarseyd2b45642009-05-11 18:02:16 +00001717 } else if (InternalIsOnCheckList(Argv[LoopCounter], CheckList, &CurrentItemType) == TRUE) {
jcarsey94b17fa2009-05-07 18:46:18 +00001718 //
jcarsey2247dde2009-11-09 18:08:58 +00001719 // We might have leftover if last parameter didnt have optional value
1720 //
jcarsey125c2cf2009-11-18 21:36:50 +00001721 if (GetItemValue != 0) {
1722 GetItemValue = 0;
jcarsey2247dde2009-11-09 18:08:58 +00001723 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
1724 }
1725 //
jcarsey94b17fa2009-05-07 18:46:18 +00001726 // this is a flag
1727 //
1728 CurrentItemPackage = AllocatePool(sizeof(SHELL_PARAM_PACKAGE));
1729 ASSERT(CurrentItemPackage != NULL);
1730 CurrentItemPackage->Name = AllocatePool(StrSize(Argv[LoopCounter]));
1731 ASSERT(CurrentItemPackage->Name != NULL);
1732 StrCpy(CurrentItemPackage->Name, Argv[LoopCounter]);
1733 CurrentItemPackage->Type = CurrentItemType;
1734 CurrentItemPackage->OriginalPosition = (UINTN)(-1);
jcarseyb1f95a02009-06-16 00:23:19 +00001735 CurrentItemPackage->Value = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +00001736
1737 //
1738 // Does this flag require a value
1739 //
jcarsey125c2cf2009-11-18 21:36:50 +00001740 switch (CurrentItemPackage->Type) {
jcarsey94b17fa2009-05-07 18:46:18 +00001741 //
jcarsey125c2cf2009-11-18 21:36:50 +00001742 // possibly trigger the next loop(s) to populate the value of this item
1743 //
1744 case TypeValue:
1745 GetItemValue = 1;
1746 ValueSize = 0;
1747 break;
1748 case TypeDoubleValue:
1749 GetItemValue = 2;
1750 ValueSize = 0;
1751 break;
1752 case TypeMaxValue:
1753 GetItemValue = (UINTN)(-1);
1754 ValueSize = 0;
1755 break;
1756 default:
1757 //
1758 // this item has no value expected; we are done
1759 //
1760 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
1761 ASSERT(GetItemValue == 0);
1762 break;
jcarsey94b17fa2009-05-07 18:46:18 +00001763 }
jcarsey125c2cf2009-11-18 21:36:50 +00001764 } else if (GetItemValue != 0 && InternalIsFlag(Argv[LoopCounter], AlwaysAllowNumbers) == FALSE) {
jcarseyb1f95a02009-06-16 00:23:19 +00001765 ASSERT(CurrentItemPackage != NULL);
1766 //
jcarsey125c2cf2009-11-18 21:36:50 +00001767 // get the item VALUE for a previous flag
jcarseyb1f95a02009-06-16 00:23:19 +00001768 //
jcarsey125c2cf2009-11-18 21:36:50 +00001769 CurrentItemPackage->Value = ReallocatePool(ValueSize, ValueSize + StrSize(Argv[LoopCounter]) + sizeof(CHAR16), CurrentItemPackage->Value);
jcarseyb1f95a02009-06-16 00:23:19 +00001770 ASSERT(CurrentItemPackage->Value != NULL);
jcarsey125c2cf2009-11-18 21:36:50 +00001771 if (ValueSize == 0) {
1772 StrCpy(CurrentItemPackage->Value, Argv[LoopCounter]);
1773 } else {
1774 StrCat(CurrentItemPackage->Value, L" ");
1775 StrCat(CurrentItemPackage->Value, Argv[LoopCounter]);
1776 }
1777 ValueSize += StrSize(Argv[LoopCounter]) + sizeof(CHAR16);
1778 GetItemValue--;
1779 if (GetItemValue == 0) {
1780 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
1781 }
jcarsey2247dde2009-11-09 18:08:58 +00001782 } else if (InternalIsFlag(Argv[LoopCounter], AlwaysAllowNumbers) == FALSE) {
jcarseyb1f95a02009-06-16 00:23:19 +00001783 //
1784 // add this one as a non-flag
1785 //
1786 CurrentItemPackage = AllocatePool(sizeof(SHELL_PARAM_PACKAGE));
1787 ASSERT(CurrentItemPackage != NULL);
1788 CurrentItemPackage->Name = NULL;
1789 CurrentItemPackage->Type = TypePosition;
1790 CurrentItemPackage->Value = AllocatePool(StrSize(Argv[LoopCounter]));
1791 ASSERT(CurrentItemPackage->Value != NULL);
1792 StrCpy(CurrentItemPackage->Value, Argv[LoopCounter]);
jcarsey2247dde2009-11-09 18:08:58 +00001793 CurrentItemPackage->OriginalPosition = mTotalParameterCount++;
jcarsey9b3bf082009-06-23 21:15:07 +00001794 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
jcarsey94b17fa2009-05-07 18:46:18 +00001795 } else if (ProblemParam) {
1796 //
1797 // this was a non-recognised flag... error!
1798 //
jcarseyd2b45642009-05-11 18:02:16 +00001799 *ProblemParam = AllocatePool(StrSize(Argv[LoopCounter]));
1800 ASSERT(*ProblemParam != NULL);
1801 StrCpy(*ProblemParam, Argv[LoopCounter]);
jcarsey94b17fa2009-05-07 18:46:18 +00001802 ShellCommandLineFreeVarList(*CheckPackage);
1803 *CheckPackage = NULL;
1804 return (EFI_VOLUME_CORRUPTED);
1805 } else {
1806 ShellCommandLineFreeVarList(*CheckPackage);
1807 *CheckPackage = NULL;
1808 return (EFI_VOLUME_CORRUPTED);
1809 }
1810 }
jcarsey125c2cf2009-11-18 21:36:50 +00001811 if (GetItemValue != 0) {
1812 GetItemValue = 0;
1813 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
1814 }
jcarsey94b17fa2009-05-07 18:46:18 +00001815 //
1816 // support for AutoPageBreak
1817 //
1818 if (AutoPageBreak && ShellCommandLineGetFlag(*CheckPackage, L"-b")) {
1819 ShellSetPageBreakMode(TRUE);
1820 }
1821 return (EFI_SUCCESS);
1822}
1823
1824/**
1825 Checks the command line arguments passed against the list of valid ones.
1826 Optionally removes NULL values first.
1827
1828 If no initialization is required, then return RETURN_SUCCESS.
1829
1830 @param CheckList pointer to list of parameters to check
1831 @param CheckPackage pointer to pointer to list checked values
1832 @param ProblemParam optional pointer to pointer to unicode string for
1833 the paramater that caused failure.
1834 @param AutoPageBreak will automatically set PageBreakEnabled for "b" parameter
1835
1836 @retval EFI_SUCCESS The operation completed sucessfully.
1837 @retval EFI_OUT_OF_RESOURCES A memory allocation failed
1838 @retval EFI_INVALID_PARAMETER A parameter was invalid
1839 @retval EFI_VOLUME_CORRUPTED the command line was corrupt. an argument was
1840 duplicated. the duplicated command line argument
1841 was returned in ProblemParam if provided.
1842 @retval EFI_DEVICE_ERROR the commands contained 2 opposing arguments. one
1843 of the command line arguments was returned in
1844 ProblemParam if provided.
1845 @retval EFI_NOT_FOUND a argument required a value that was missing.
1846 the invalid command line argument was returned in
1847 ProblemParam if provided.
1848**/
1849EFI_STATUS
1850EFIAPI
jcarsey2247dde2009-11-09 18:08:58 +00001851ShellCommandLineParseEx (
jcarsey94b17fa2009-05-07 18:46:18 +00001852 IN CONST SHELL_PARAM_ITEM *CheckList,
1853 OUT LIST_ENTRY **CheckPackage,
1854 OUT CHAR16 **ProblemParam OPTIONAL,
jcarsey2247dde2009-11-09 18:08:58 +00001855 IN BOOLEAN AutoPageBreak,
1856 IN BOOLEAN AlwaysAllowNumbers
1857 ) {
jcarsey94b17fa2009-05-07 18:46:18 +00001858 //
1859 // ASSERT that CheckList and CheckPackage aren't NULL
1860 //
1861 ASSERT(CheckList != NULL);
1862 ASSERT(CheckPackage != NULL);
1863
1864 //
1865 // Check for UEFI Shell 2.0 protocols
1866 //
1867 if (mEfiShellParametersProtocol != NULL) {
1868 return (InternalCommandLineParse(CheckList,
1869 CheckPackage,
1870 ProblemParam,
1871 AutoPageBreak,
jljusten08d7f8e2009-06-15 18:42:13 +00001872 (CONST CHAR16**) mEfiShellParametersProtocol->Argv,
jcarsey2247dde2009-11-09 18:08:58 +00001873 mEfiShellParametersProtocol->Argc,
1874 AlwaysAllowNumbers));
jcarsey94b17fa2009-05-07 18:46:18 +00001875 }
1876
1877 //
1878 // ASSERT That EFI Shell is not required
1879 //
1880 ASSERT (mEfiShellInterface != NULL);
1881 return (InternalCommandLineParse(CheckList,
1882 CheckPackage,
1883 ProblemParam,
1884 AutoPageBreak,
jljusten08d7f8e2009-06-15 18:42:13 +00001885 (CONST CHAR16**) mEfiShellInterface->Argv,
jcarsey2247dde2009-11-09 18:08:58 +00001886 mEfiShellInterface->Argc,
1887 AlwaysAllowNumbers));
jcarsey94b17fa2009-05-07 18:46:18 +00001888}
1889
1890/**
1891 Frees shell variable list that was returned from ShellCommandLineParse.
1892
1893 This function will free all the memory that was used for the CheckPackage
1894 list of postprocessed shell arguments.
1895
1896 this function has no return value.
1897
1898 if CheckPackage is NULL, then return
1899
1900 @param CheckPackage the list to de-allocate
1901 **/
1902VOID
1903EFIAPI
1904ShellCommandLineFreeVarList (
1905 IN LIST_ENTRY *CheckPackage
jcarsey2247dde2009-11-09 18:08:58 +00001906 ) {
jcarsey94b17fa2009-05-07 18:46:18 +00001907 LIST_ENTRY *Node;
1908
1909 //
1910 // check for CheckPackage == NULL
1911 //
1912 if (CheckPackage == NULL) {
1913 return;
1914 }
1915
1916 //
1917 // for each node in the list
1918 //
jcarsey9eb53ac2009-07-08 17:26:58 +00001919 for ( Node = GetFirstNode(CheckPackage)
jcarsey2247dde2009-11-09 18:08:58 +00001920 ; IsListEmpty(CheckPackage) == FALSE
jcarsey9eb53ac2009-07-08 17:26:58 +00001921 ; Node = GetFirstNode(CheckPackage)
1922 ){
jcarsey94b17fa2009-05-07 18:46:18 +00001923 //
1924 // Remove it from the list
1925 //
1926 RemoveEntryList(Node);
1927
1928 //
1929 // if it has a name free the name
1930 //
1931 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {
1932 FreePool(((SHELL_PARAM_PACKAGE*)Node)->Name);
1933 }
1934
1935 //
1936 // if it has a value free the value
1937 //
1938 if (((SHELL_PARAM_PACKAGE*)Node)->Value != NULL) {
1939 FreePool(((SHELL_PARAM_PACKAGE*)Node)->Value);
1940 }
1941
1942 //
1943 // free the node structure
1944 //
1945 FreePool((SHELL_PARAM_PACKAGE*)Node);
1946 }
1947 //
1948 // free the list head node
1949 //
1950 FreePool(CheckPackage);
1951}
1952/**
1953 Checks for presence of a flag parameter
1954
1955 flag arguments are in the form of "-<Key>" or "/<Key>", but do not have a value following the key
1956
1957 if CheckPackage is NULL then return FALSE.
1958 if KeyString is NULL then ASSERT()
1959
1960 @param CheckPackage The package of parsed command line arguments
1961 @param KeyString the Key of the command line argument to check for
1962
1963 @retval TRUE the flag is on the command line
1964 @retval FALSE the flag is not on the command line
1965 **/
1966BOOLEAN
1967EFIAPI
1968ShellCommandLineGetFlag (
1969 IN CONST LIST_ENTRY *CheckPackage,
1970 IN CHAR16 *KeyString
jcarsey2247dde2009-11-09 18:08:58 +00001971 ) {
jcarsey94b17fa2009-05-07 18:46:18 +00001972 LIST_ENTRY *Node;
1973
1974 //
1975 // ASSERT that both CheckPackage and KeyString aren't NULL
1976 //
1977 ASSERT(KeyString != NULL);
1978
1979 //
1980 // return FALSE for no package
1981 //
1982 if (CheckPackage == NULL) {
1983 return (FALSE);
1984 }
1985
1986 //
1987 // enumerate through the list of parametrs
1988 //
jcarsey9eb53ac2009-07-08 17:26:58 +00001989 for ( Node = GetFirstNode(CheckPackage)
1990 ; !IsNull (CheckPackage, Node)
1991 ; Node = GetNextNode(CheckPackage, Node)
1992 ){
jcarsey94b17fa2009-05-07 18:46:18 +00001993 //
1994 // If the Name matches, return TRUE (and there may be NULL name)
1995 //
1996 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {
jcarsey9eb53ac2009-07-08 17:26:58 +00001997 //
1998 // If Type is TypeStart then only compare the begining of the strings
1999 //
2000 if ( ((SHELL_PARAM_PACKAGE*)Node)->Type == TypeStart
2001 && StrnCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name, StrLen(KeyString)) == 0
2002 ){
2003 return (TRUE);
2004 } else if (StrCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {
jcarsey94b17fa2009-05-07 18:46:18 +00002005 return (TRUE);
2006 }
2007 }
2008 }
2009 return (FALSE);
2010}
2011/**
2012 returns value from command line argument
2013
2014 value parameters are in the form of "-<Key> value" or "/<Key> value"
2015
2016 if CheckPackage is NULL, then return NULL;
2017
2018 @param CheckPackage The package of parsed command line arguments
2019 @param KeyString the Key of the command line argument to check for
2020
2021 @retval NULL the flag is not on the command line
2022 @return !=NULL pointer to unicode string of the value
2023 **/
2024CONST CHAR16*
2025EFIAPI
2026ShellCommandLineGetValue (
2027 IN CONST LIST_ENTRY *CheckPackage,
2028 IN CHAR16 *KeyString
jcarsey2247dde2009-11-09 18:08:58 +00002029 ) {
jcarsey94b17fa2009-05-07 18:46:18 +00002030 LIST_ENTRY *Node;
2031
2032 //
2033 // check for CheckPackage == NULL
2034 //
2035 if (CheckPackage == NULL) {
2036 return (NULL);
2037 }
2038
2039 //
2040 // enumerate through the list of parametrs
2041 //
jcarsey9eb53ac2009-07-08 17:26:58 +00002042 for ( Node = GetFirstNode(CheckPackage)
2043 ; !IsNull (CheckPackage, Node)
2044 ; Node = GetNextNode(CheckPackage, Node)
2045 ){
jcarsey94b17fa2009-05-07 18:46:18 +00002046 //
2047 // If the Name matches, return the value (name can be NULL)
2048 //
2049 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {
jcarsey9eb53ac2009-07-08 17:26:58 +00002050 //
2051 // If Type is TypeStart then only compare the begining of the strings
2052 //
2053 if ( ((SHELL_PARAM_PACKAGE*)Node)->Type == TypeStart
2054 && StrnCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name, StrLen(KeyString)) == 0
2055 ){
2056 //
2057 // return the string part after the flag
2058 //
2059 return (((SHELL_PARAM_PACKAGE*)Node)->Name + StrLen(KeyString));
2060 } else if (StrCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {
2061 //
2062 // return the value
2063 //
jcarsey94b17fa2009-05-07 18:46:18 +00002064 return (((SHELL_PARAM_PACKAGE*)Node)->Value);
2065 }
2066 }
2067 }
2068 return (NULL);
2069}
2070/**
2071 returns raw value from command line argument
2072
2073 raw value parameters are in the form of "value" in a specific position in the list
2074
2075 if CheckPackage is NULL, then return NULL;
2076
2077 @param CheckPackage The package of parsed command line arguments
2078 @param Position the position of the value
2079
2080 @retval NULL the flag is not on the command line
2081 @return !=NULL pointer to unicode string of the value
2082 **/
2083CONST CHAR16*
2084EFIAPI
2085ShellCommandLineGetRawValue (
2086 IN CONST LIST_ENTRY *CheckPackage,
2087 IN UINT32 Position
jcarsey2247dde2009-11-09 18:08:58 +00002088 ) {
jcarsey94b17fa2009-05-07 18:46:18 +00002089 LIST_ENTRY *Node;
2090
2091 //
2092 // check for CheckPackage == NULL
2093 //
2094 if (CheckPackage == NULL) {
2095 return (NULL);
2096 }
2097
2098 //
2099 // enumerate through the list of parametrs
2100 //
jcarseyb82bfcc2009-06-29 16:28:23 +00002101 for ( Node = GetFirstNode(CheckPackage)
2102 ; !IsNull (CheckPackage, Node)
2103 ; Node = GetNextNode(CheckPackage, Node)
2104 ){
jcarsey94b17fa2009-05-07 18:46:18 +00002105 //
2106 // If the position matches, return the value
2107 //
2108 if (((SHELL_PARAM_PACKAGE*)Node)->OriginalPosition == Position) {
2109 return (((SHELL_PARAM_PACKAGE*)Node)->Value);
2110 }
2111 }
2112 return (NULL);
jcarseyb1f95a02009-06-16 00:23:19 +00002113}
jcarsey2247dde2009-11-09 18:08:58 +00002114
2115/**
2116 returns the number of command line value parameters that were parsed.
2117
2118 this will not include flags.
2119
2120 @retval (UINTN)-1 No parsing has ocurred
2121 @return other The number of value parameters found
2122**/
2123UINTN
2124EFIAPI
2125ShellCommandLineGetCount(
2126 VOID
jcarsey125c2cf2009-11-18 21:36:50 +00002127 )
2128{
jcarsey2247dde2009-11-09 18:08:58 +00002129 return (mTotalParameterCount);
2130}
2131
jcarsey975136a2009-06-16 19:03:54 +00002132/**
2133 This is a find and replace function. it will return the NewString as a copy of
2134 SourceString with each instance of FindTarget replaced with ReplaceWith.
2135
2136 If the string would grow bigger than NewSize it will halt and return error.
2137
2138 @param[in] SourceString String with source buffer
jcarseyb82bfcc2009-06-29 16:28:23 +00002139 @param[in,out] NewString String with resultant buffer
jcarsey975136a2009-06-16 19:03:54 +00002140 @param[in] NewSize Size in bytes of NewString
2141 @param[in] FindTarget String to look for
2142 @param[in[ ReplaceWith String to replace FindTarget with
2143
2144 @retval EFI_INVALID_PARAMETER SourceString was NULL
2145 @retval EFI_INVALID_PARAMETER NewString was NULL
2146 @retval EFI_INVALID_PARAMETER FindTarget was NULL
2147 @retval EFI_INVALID_PARAMETER ReplaceWith was NULL
2148 @retval EFI_INVALID_PARAMETER FindTarget had length < 1
2149 @retval EFI_INVALID_PARAMETER SourceString had length < 1
2150 @retval EFI_BUFFER_TOO_SMALL NewSize was less than the minimum size to hold
2151 the new string (truncation occurred)
2152 @retval EFI_SUCCESS the string was sucessfully copied with replacement
2153**/
2154
2155EFI_STATUS
2156EFIAPI
2157CopyReplace(
2158 IN CHAR16 CONST *SourceString,
2159 IN CHAR16 *NewString,
2160 IN UINTN NewSize,
2161 IN CONST CHAR16 *FindTarget,
2162 IN CONST CHAR16 *ReplaceWith
jcarsey2247dde2009-11-09 18:08:58 +00002163 )
2164{
jcarsey01582942009-07-10 19:46:17 +00002165 UINTN Size;
jcarsey975136a2009-06-16 19:03:54 +00002166 if ( (SourceString == NULL)
2167 || (NewString == NULL)
2168 || (FindTarget == NULL)
2169 || (ReplaceWith == NULL)
2170 || (StrLen(FindTarget) < 1)
2171 || (StrLen(SourceString) < 1)
2172 ){
2173 return (EFI_INVALID_PARAMETER);
2174 }
jcarsey2247dde2009-11-09 18:08:58 +00002175 NewString = SetMem16(NewString, NewSize, CHAR_NULL);
2176 while (*SourceString != CHAR_NULL) {
jcarsey975136a2009-06-16 19:03:54 +00002177 if (StrnCmp(SourceString, FindTarget, StrLen(FindTarget)) == 0) {
2178 SourceString += StrLen(FindTarget);
jcarsey01582942009-07-10 19:46:17 +00002179 Size = StrSize(NewString);
2180 if ((Size + (StrLen(ReplaceWith)*sizeof(CHAR16))) > NewSize) {
jcarsey975136a2009-06-16 19:03:54 +00002181 return (EFI_BUFFER_TOO_SMALL);
2182 }
2183 StrCat(NewString, ReplaceWith);
2184 } else {
jcarsey01582942009-07-10 19:46:17 +00002185 Size = StrSize(NewString);
2186 if (Size + sizeof(CHAR16) > NewSize) {
jcarsey975136a2009-06-16 19:03:54 +00002187 return (EFI_BUFFER_TOO_SMALL);
2188 }
2189 StrnCat(NewString, SourceString, 1);
2190 SourceString++;
2191 }
2192 }
2193 return (EFI_SUCCESS);
2194}
jcarseyb1f95a02009-06-16 00:23:19 +00002195
2196/**
2197 Print at a specific location on the screen.
2198
jcarseyf1b87e72009-06-17 00:52:11 +00002199 This function will move the cursor to a given screen location and print the specified string
jcarseyb1f95a02009-06-16 00:23:19 +00002200
2201 If -1 is specified for either the Row or Col the current screen location for BOTH
jcarseyf1b87e72009-06-17 00:52:11 +00002202 will be used.
jcarseyb1f95a02009-06-16 00:23:19 +00002203
2204 if either Row or Col is out of range for the current console, then ASSERT
2205 if Format is NULL, then ASSERT
2206
2207 In addition to the standard %-based flags as supported by UefiLib Print() this supports
2208 the following additional flags:
2209 %N - Set output attribute to normal
2210 %H - Set output attribute to highlight
2211 %E - Set output attribute to error
2212 %B - Set output attribute to blue color
2213 %V - Set output attribute to green color
2214
2215 Note: The background color is controlled by the shell command cls.
2216
2217 @param[in] Row the row to print at
2218 @param[in] Col the column to print at
2219 @param[in] Format the format string
jcarsey2247dde2009-11-09 18:08:58 +00002220 @param[in] Marker the marker for the variable argument list
jcarseyb1f95a02009-06-16 00:23:19 +00002221
2222 @return the number of characters printed to the screen
2223**/
2224
2225UINTN
2226EFIAPI
jcarsey2247dde2009-11-09 18:08:58 +00002227InternalShellPrintWorker(
jcarseyb1f95a02009-06-16 00:23:19 +00002228 IN INT32 Col OPTIONAL,
2229 IN INT32 Row OPTIONAL,
2230 IN CONST CHAR16 *Format,
jcarsey2247dde2009-11-09 18:08:58 +00002231 VA_LIST Marker
2232 )
2233{
jcarseyb1f95a02009-06-16 00:23:19 +00002234 UINTN BufferSize;
jcarsey975136a2009-06-16 19:03:54 +00002235 CHAR16 *PostReplaceFormat;
2236 CHAR16 *PostReplaceFormat2;
jcarseyb1f95a02009-06-16 00:23:19 +00002237 UINTN Return;
jcarseyb1f95a02009-06-16 00:23:19 +00002238 EFI_STATUS Status;
jcarsey975136a2009-06-16 19:03:54 +00002239 UINTN NormalAttribute;
2240 CHAR16 *ResumeLocation;
2241 CHAR16 *FormatWalker;
jcarsey975136a2009-06-16 19:03:54 +00002242
2243 BufferSize = (PcdGet32 (PcdUefiLibMaxPrintBufferSize) + 1) * sizeof (CHAR16);
2244 PostReplaceFormat = AllocateZeroPool (BufferSize);
2245 ASSERT (PostReplaceFormat != NULL);
2246 PostReplaceFormat2 = AllocateZeroPool (BufferSize);
2247 ASSERT (PostReplaceFormat2 != NULL);
2248
2249 //
2250 // Back and forth each time fixing up 1 of our flags...
2251 //
2252 Status = CopyReplace(Format, PostReplaceFormat, BufferSize, L"%N", L"%%N");
2253 ASSERT_EFI_ERROR(Status);
2254 Status = CopyReplace(PostReplaceFormat, PostReplaceFormat2, BufferSize, L"%E", L"%%E");
2255 ASSERT_EFI_ERROR(Status);
2256 Status = CopyReplace(PostReplaceFormat2, PostReplaceFormat, BufferSize, L"%H", L"%%H");
2257 ASSERT_EFI_ERROR(Status);
2258 Status = CopyReplace(PostReplaceFormat, PostReplaceFormat2, BufferSize, L"%B", L"%%B");
2259 ASSERT_EFI_ERROR(Status);
2260 Status = CopyReplace(PostReplaceFormat2, PostReplaceFormat, BufferSize, L"%V", L"%%V");
2261 ASSERT_EFI_ERROR(Status);
2262
2263 //
2264 // Use the last buffer from replacing to print from...
2265 //
2266 Return = UnicodeVSPrint (PostReplaceFormat2, BufferSize, PostReplaceFormat, Marker);
2267
2268 FreePool(PostReplaceFormat);
jcarseyb1f95a02009-06-16 00:23:19 +00002269
2270 if (Col != -1 && Row != -1) {
jcarseyb1f95a02009-06-16 00:23:19 +00002271 Status = gST->ConOut->SetCursorPosition(gST->ConOut, Col, Row);
2272 ASSERT_EFI_ERROR(Status);
jcarsey975136a2009-06-16 19:03:54 +00002273 }
2274
2275 NormalAttribute = gST->ConOut->Mode->Attribute;
2276 FormatWalker = PostReplaceFormat2;
jcarsey2247dde2009-11-09 18:08:58 +00002277 while (*FormatWalker != CHAR_NULL) {
jcarsey975136a2009-06-16 19:03:54 +00002278 //
2279 // Find the next attribute change request
2280 //
2281 ResumeLocation = StrStr(FormatWalker, L"%");
2282 if (ResumeLocation != NULL) {
jcarsey2247dde2009-11-09 18:08:58 +00002283 *ResumeLocation = CHAR_NULL;
jcarsey975136a2009-06-16 19:03:54 +00002284 }
2285 //
2286 // print the current FormatWalker string
2287 //
2288 Status = gST->ConOut->OutputString(gST->ConOut, FormatWalker);
2289 ASSERT_EFI_ERROR(Status);
2290 //
2291 // update the attribute
2292 //
2293 if (ResumeLocation != NULL) {
2294 switch (*(ResumeLocation+1)) {
2295 case (L'N'):
2296 gST->ConOut->SetAttribute(gST->ConOut, NormalAttribute);
2297 break;
2298 case (L'E'):
2299 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_YELLOW, ((NormalAttribute&(BIT4|BIT5|BIT6))>>4)));
2300 break;
2301 case (L'H'):
2302 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_WHITE, ((NormalAttribute&(BIT4|BIT5|BIT6))>>4)));
2303 break;
2304 case (L'B'):
2305 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_BLUE, ((NormalAttribute&(BIT4|BIT5|BIT6))>>4)));
2306 break;
2307 case (L'V'):
2308 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_GREEN, ((NormalAttribute&(BIT4|BIT5|BIT6))>>4)));
2309 break;
2310 default:
2311 ASSERT(FALSE);
2312 break;
2313 }
2314 } else {
2315 //
2316 // reset to normal now...
2317 //
2318 gST->ConOut->SetAttribute(gST->ConOut, NormalAttribute);
2319 break;
2320 }
2321
2322 //
2323 // update FormatWalker to Resume + 2 (skip the % and the indicator)
2324 //
2325 FormatWalker = ResumeLocation + 2;
2326 }
jcarseyb1f95a02009-06-16 00:23:19 +00002327
jcarsey975136a2009-06-16 19:03:54 +00002328 FreePool(PostReplaceFormat2);
2329
jcarseyb1f95a02009-06-16 00:23:19 +00002330 return (Return);
jcarsey5f7431d2009-07-10 18:06:01 +00002331}
jcarsey2247dde2009-11-09 18:08:58 +00002332
2333/**
2334 Print at a specific location on the screen.
2335
2336 This function will move the cursor to a given screen location and print the specified string
2337
2338 If -1 is specified for either the Row or Col the current screen location for BOTH
2339 will be used.
2340
2341 if either Row or Col is out of range for the current console, then ASSERT
2342 if Format is NULL, then ASSERT
2343
2344 In addition to the standard %-based flags as supported by UefiLib Print() this supports
2345 the following additional flags:
2346 %N - Set output attribute to normal
2347 %H - Set output attribute to highlight
2348 %E - Set output attribute to error
2349 %B - Set output attribute to blue color
2350 %V - Set output attribute to green color
2351
2352 Note: The background color is controlled by the shell command cls.
2353
2354 @param[in] Row the row to print at
2355 @param[in] Col the column to print at
2356 @param[in] Format the format string
2357
2358 @return the number of characters printed to the screen
2359**/
2360
2361UINTN
2362EFIAPI
2363ShellPrintEx(
2364 IN INT32 Col OPTIONAL,
2365 IN INT32 Row OPTIONAL,
2366 IN CONST CHAR16 *Format,
2367 ...
2368 )
2369{
2370 VA_LIST Marker;
2371 VA_START (Marker, Format);
2372 return (InternalShellPrintWorker(Col, Row, Format, Marker));
2373}
2374
2375/**
2376 Print at a specific location on the screen.
2377
2378 This function will move the cursor to a given screen location, print the specified string,
2379 and return the cursor to the original locaiton.
2380
2381 If -1 is specified for either the Row or Col the current screen location for BOTH
2382 will be used and the cursor's position will not be moved back to an original location.
2383
2384 if either Row or Col is out of range for the current console, then ASSERT
2385 if Format is NULL, then ASSERT
2386
2387 In addition to the standard %-based flags as supported by UefiLib Print() this supports
2388 the following additional flags:
2389 %N - Set output attribute to normal
2390 %H - Set output attribute to highlight
2391 %E - Set output attribute to error
2392 %B - Set output attribute to blue color
2393 %V - Set output attribute to green color
2394
2395 Note: The background color is controlled by the shell command cls.
2396
2397 @param[in] Row the row to print at
2398 @param[in] Col the column to print at
2399 @param[in] HiiFormatStringId the format string Id for getting from Hii
2400 @param[in] HiiFormatHandle the format string Handle for getting from Hii
2401
2402 @return the number of characters printed to the screen
2403**/
2404UINTN
2405EFIAPI
2406ShellPrintHiiEx(
2407 IN INT32 Col OPTIONAL,
2408 IN INT32 Row OPTIONAL,
2409 IN CONST EFI_STRING_ID HiiFormatStringId,
2410 IN CONST EFI_HANDLE HiiFormatHandle,
2411 ...
2412 )
2413{
2414 VA_LIST Marker;
2415 CHAR16 *HiiFormatString;
2416 UINTN RetVal;
2417
2418 VA_START (Marker, HiiFormatHandle);
2419 HiiFormatString = HiiGetString(HiiFormatHandle, HiiFormatStringId, NULL);
2420 ASSERT(HiiFormatString != NULL);
2421
2422 RetVal = InternalShellPrintWorker(Col, Row, HiiFormatString, Marker);
2423
2424 FreePool(HiiFormatString);
2425
2426 return (RetVal);
2427}
2428
2429/**
2430 Function to determine if a given filename represents a file or a directory.
2431
2432 @param[in] DirName Path to directory to test.
2433
2434 @retval EFI_SUCCESS The Path represents a directory
2435 @retval EFI_NOT_FOUND The Path does not represent a directory
2436 @return other The path failed to open
2437**/
2438EFI_STATUS
2439EFIAPI
2440ShellIsDirectory(
2441 IN CONST CHAR16 *DirName
2442 )
2443{
2444 EFI_STATUS Status;
2445 EFI_FILE_HANDLE Handle;
2446
2447 Handle = NULL;
2448
2449 Status = ShellOpenFileByName(DirName, &Handle, EFI_FILE_MODE_READ, 0);
2450 if (EFI_ERROR(Status)) {
2451 return (Status);
2452 }
2453
2454 if (FileHandleIsDirectory(Handle) == EFI_SUCCESS) {
2455 ShellCloseFile(&Handle);
2456 return (EFI_SUCCESS);
2457 }
2458 ShellCloseFile(&Handle);
2459 return (EFI_NOT_FOUND);
2460}
2461
jcarsey125c2cf2009-11-18 21:36:50 +00002462/**
2463 Function to determine whether a string is decimal or hex representation of a number
2464 and return the number converted from the string.
2465
2466 @param[in] String String representation of a number
2467
2468 @retval all the number
2469**/
2470UINTN
2471EFIAPI
2472ShellStrToUintn(
2473 IN CONST CHAR16 *String
2474 )
2475{
2476 CONST CHAR16 *Walker;
2477 for (Walker = String; Walker != NULL && *Walker != CHAR_NULL && *Walker == L' '; Walker = Walker + 1);
2478 if (StrnCmp(Walker, L"0x", 2) == 0 || StrnCmp(Walker, L"0X", 2) == 0){
2479 return (StrHexToUintn(Walker));
2480 }
2481 return (StrDecimalToUintn(Walker));
2482}
2483
2484/**
2485 Safely append with automatic string resizing given length of Destination and
2486 desired length of copy from Source.
2487
2488 append the first D characters of Source to the end of Destination, where D is
2489 the lesser of Count and the StrLen() of Source. If appending those D characters
2490 will fit within Destination (whose Size is given as CurrentSize) and
2491 still leave room for a null terminator, then those characters are appended,
2492 starting at the original terminating null of Destination, and a new terminating
2493 null is appended.
2494
2495 If appending D characters onto Destination will result in a overflow of the size
2496 given in CurrentSize the string will be grown such that the copy can be performed
2497 and CurrentSize will be updated to the new size.
2498
2499 If Source is NULL, there is nothing to append, just return the current buffer in
2500 Destination.
2501
2502 if Destination is NULL, then ASSERT()
2503 if Destination's current length (including NULL terminator) is already more then
2504 CurrentSize, then ASSERT()
2505
2506 @param[in,out] Destination The String to append onto
2507 @param[in,out] CurrentSize on call the number of bytes in Destination. On
2508 return possibly the new size (still in bytes). if NULL
2509 then allocate whatever is needed.
2510 @param[in] Source The String to append from
2511 @param[in] Count Maximum number of characters to append. if 0 then
2512 all are appended.
2513
2514 @return Destination return the resultant string.
2515**/
2516CHAR16*
2517EFIAPI
2518StrnCatGrow (
2519 IN OUT CHAR16 **Destination,
2520 IN OUT UINTN *CurrentSize,
2521 IN CONST CHAR16 *Source,
2522 IN UINTN Count
2523 )
2524{
2525 UINTN DestinationStartSize;
2526 UINTN NewSize;
2527
2528 //
2529 // ASSERTs
2530 //
2531 ASSERT(Destination != NULL);
2532
2533 //
2534 // If there's nothing to do then just return Destination
2535 //
2536 if (Source == NULL) {
2537 return (*Destination);
2538 }
2539
2540 //
2541 // allow for un-initialized pointers, based on size being 0
2542 //
2543 if (CurrentSize != NULL && *CurrentSize == 0) {
2544 *Destination = NULL;
2545 }
2546
2547 //
2548 // allow for NULL pointers address as Destination
2549 //
2550 if (*Destination != NULL) {
2551 ASSERT(CurrentSize != 0);
2552 DestinationStartSize = StrSize(*Destination);
2553 ASSERT(DestinationStartSize <= *CurrentSize);
2554 } else {
2555 DestinationStartSize = 0;
2556// ASSERT(*CurrentSize == 0);
2557 }
2558
2559 //
2560 // Append all of Source?
2561 //
2562 if (Count == 0) {
2563 Count = StrLen(Source);
2564 }
2565
2566 //
2567 // Test and grow if required
2568 //
2569 if (CurrentSize != NULL) {
2570 NewSize = *CurrentSize;
2571 while (NewSize < (DestinationStartSize + (Count*sizeof(CHAR16)))) {
2572 NewSize += 2 * Count * sizeof(CHAR16);
2573 }
2574 *Destination = ReallocatePool(*CurrentSize, NewSize, *Destination);
2575 *CurrentSize = NewSize;
2576 } else {
2577 *Destination = AllocateZeroPool((Count+1)*sizeof(CHAR16));
2578 }
2579
2580 //
2581 // Now use standard StrnCat on a big enough buffer
2582 //
2583 return StrnCat(*Destination, Source, Count);
2584}