blob: ae76b4208075f94b824c63d9dea35b36b6419bb7 [file] [log] [blame]
jcarsey94b17fa2009-05-07 18:46:18 +00001/** @file
2 Provides interface to shell functionality for shell commands and applications.
3
jcarseya31bd332009-11-20 18:21:03 +00004Copyright (c) 2006 - 2009, Intel Corporation<BR>
jcarsey94b17fa2009-05-07 18:46:18 +00005All 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;
jcarseye2f82972009-12-01 05:40:24 +000057STATIC EFI_FILE_HANDLE StdOut;
58STATIC EFI_FILE_HANDLE StdErr;
jcarsey2247dde2009-11-09 18:08:58 +000059
60/**
61 Check if a Unicode character is a hexadecimal character.
62
63 This internal function checks if a Unicode character is a
64 decimal character. The valid hexadecimal character is
65 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.
66
67
68 @param Char The character to check against.
69
70 @retval TRUE If the Char is a hexadecmial character.
71 @retval FALSE If the Char is not a hexadecmial character.
72
73**/
74BOOLEAN
75EFIAPI
76ShellInternalIsHexaDecimalDigitCharacter (
77 IN CHAR16 Char
78 ) {
79 return (BOOLEAN) ((Char >= L'0' && Char <= L'9') || (Char >= L'A' && Char <= L'F') || (Char >= L'a' && Char <= L'f'));
80}
jcarsey94b17fa2009-05-07 18:46:18 +000081
82/**
83 helper function to find ShellEnvironment2 for constructor
84**/
85EFI_STATUS
86EFIAPI
87ShellFindSE2 (
88 IN EFI_HANDLE ImageHandle
jcarsey2247dde2009-11-09 18:08:58 +000089 ) {
jcarsey94b17fa2009-05-07 18:46:18 +000090 EFI_STATUS Status;
91 EFI_HANDLE *Buffer;
92 UINTN BufferSize;
93 UINTN HandleIndex;
94
95 BufferSize = 0;
96 Buffer = NULL;
97 Status = gBS->OpenProtocol(ImageHandle,
98 &gEfiShellEnvironment2Guid,
99 (VOID **)&mEfiShellEnvironment2,
100 ImageHandle,
101 NULL,
102 EFI_OPEN_PROTOCOL_GET_PROTOCOL
103 );
104 //
105 // look for the mEfiShellEnvironment2 protocol at a higher level
106 //
jcarsey9eb53ac2009-07-08 17:26:58 +0000107 if (EFI_ERROR (Status) || !(CompareGuid (&mEfiShellEnvironment2->SESGuid, &gEfiShellEnvironment2ExtGuid) != FALSE)){
jcarsey94b17fa2009-05-07 18:46:18 +0000108 //
109 // figure out how big of a buffer we need.
110 //
111 Status = gBS->LocateHandle (ByProtocol,
112 &gEfiShellEnvironment2Guid,
113 NULL, // ignored for ByProtocol
114 &BufferSize,
115 Buffer
116 );
jcarsey2247dde2009-11-09 18:08:58 +0000117 //
118 // maybe it's not there???
119 //
120 if (Status == EFI_BUFFER_TOO_SMALL) {
121 Buffer = (EFI_HANDLE*)AllocatePool(BufferSize);
122 ASSERT(Buffer != NULL);
123 Status = gBS->LocateHandle (ByProtocol,
124 &gEfiShellEnvironment2Guid,
125 NULL, // ignored for ByProtocol
126 &BufferSize,
127 Buffer
128 );
129 }
jcarsey94b17fa2009-05-07 18:46:18 +0000130 if (!EFI_ERROR (Status)) {
131 //
132 // now parse the list of returned handles
133 //
134 Status = EFI_NOT_FOUND;
135 for (HandleIndex = 0; HandleIndex < (BufferSize/sizeof(Buffer[0])); HandleIndex++) {
136 Status = gBS->OpenProtocol(Buffer[HandleIndex],
137 &gEfiShellEnvironment2Guid,
138 (VOID **)&mEfiShellEnvironment2,
139 ImageHandle,
140 NULL,
141 EFI_OPEN_PROTOCOL_GET_PROTOCOL
142 );
jcarsey9eb53ac2009-07-08 17:26:58 +0000143 if (CompareGuid (&mEfiShellEnvironment2->SESGuid, &gEfiShellEnvironment2ExtGuid) != FALSE) {
jcarsey94b17fa2009-05-07 18:46:18 +0000144 mEfiShellEnvironment2Handle = Buffer[HandleIndex];
145 Status = EFI_SUCCESS;
146 break;
147 }
148 }
149 }
150 }
151 if (Buffer != NULL) {
152 FreePool (Buffer);
153 }
154 return (Status);
155}
156
jcarsey94b17fa2009-05-07 18:46:18 +0000157EFI_STATUS
158EFIAPI
jcarseyd2b45642009-05-11 18:02:16 +0000159ShellLibConstructorWorker (
jcarsey94b17fa2009-05-07 18:46:18 +0000160 IN EFI_HANDLE ImageHandle,
161 IN EFI_SYSTEM_TABLE *SystemTable
jcarsey2247dde2009-11-09 18:08:58 +0000162 ) {
jcarsey94b17fa2009-05-07 18:46:18 +0000163 EFI_STATUS Status;
164
jcarsey94b17fa2009-05-07 18:46:18 +0000165 //
jcarsey2247dde2009-11-09 18:08:58 +0000166 // Set the parameter count to an invalid number
167 //
168 mTotalParameterCount = (UINTN)(-1);
169
170 //
jcarsey94b17fa2009-05-07 18:46:18 +0000171 // UEFI 2.0 shell interfaces (used preferentially)
172 //
173 Status = gBS->OpenProtocol(ImageHandle,
174 &gEfiShellProtocolGuid,
175 (VOID **)&mEfiShellProtocol,
176 ImageHandle,
177 NULL,
178 EFI_OPEN_PROTOCOL_GET_PROTOCOL
179 );
180 if (EFI_ERROR(Status)) {
181 mEfiShellProtocol = NULL;
182 }
183 Status = gBS->OpenProtocol(ImageHandle,
184 &gEfiShellParametersProtocolGuid,
185 (VOID **)&mEfiShellParametersProtocol,
186 ImageHandle,
187 NULL,
188 EFI_OPEN_PROTOCOL_GET_PROTOCOL
189 );
190 if (EFI_ERROR(Status)) {
191 mEfiShellParametersProtocol = NULL;
192 }
193
194 if (mEfiShellParametersProtocol == NULL || mEfiShellProtocol == NULL) {
195 //
196 // Moved to seperate function due to complexity
197 //
198 Status = ShellFindSE2(ImageHandle);
199
200 if (EFI_ERROR(Status)) {
201 DEBUG((DEBUG_ERROR, "Status: 0x%08x\r\n", Status));
202 mEfiShellEnvironment2 = NULL;
203 }
204 Status = gBS->OpenProtocol(ImageHandle,
205 &gEfiShellInterfaceGuid,
206 (VOID **)&mEfiShellInterface,
207 ImageHandle,
208 NULL,
209 EFI_OPEN_PROTOCOL_GET_PROTOCOL
210 );
211 if (EFI_ERROR(Status)) {
212 mEfiShellInterface = NULL;
213 }
214 }
215 //
216 // only success getting 2 of either the old or new, but no 1/2 and 1/2
217 //
218 if ((mEfiShellEnvironment2 != NULL && mEfiShellInterface != NULL) ||
219 (mEfiShellProtocol != NULL && mEfiShellParametersProtocol != NULL) ) {
jcarseyd2b45642009-05-11 18:02:16 +0000220 if (mEfiShellProtocol != NULL) {
221 FileFunctionMap.GetFileInfo = mEfiShellProtocol->GetFileInfo;
222 FileFunctionMap.SetFileInfo = mEfiShellProtocol->SetFileInfo;
223 FileFunctionMap.ReadFile = mEfiShellProtocol->ReadFile;
224 FileFunctionMap.WriteFile = mEfiShellProtocol->WriteFile;
225 FileFunctionMap.CloseFile = mEfiShellProtocol->CloseFile;
226 FileFunctionMap.DeleteFile = mEfiShellProtocol->DeleteFile;
227 FileFunctionMap.GetFilePosition = mEfiShellProtocol->GetFilePosition;
228 FileFunctionMap.SetFilePosition = mEfiShellProtocol->SetFilePosition;
229 FileFunctionMap.FlushFile = mEfiShellProtocol->FlushFile;
230 FileFunctionMap.GetFileSize = mEfiShellProtocol->GetFileSize;
231 } else {
232 FileFunctionMap.GetFileInfo = FileHandleGetInfo;
233 FileFunctionMap.SetFileInfo = FileHandleSetInfo;
234 FileFunctionMap.ReadFile = FileHandleRead;
235 FileFunctionMap.WriteFile = FileHandleWrite;
236 FileFunctionMap.CloseFile = FileHandleClose;
237 FileFunctionMap.DeleteFile = FileHandleDelete;
238 FileFunctionMap.GetFilePosition = FileHandleGetPosition;
239 FileFunctionMap.SetFilePosition = FileHandleSetPosition;
240 FileFunctionMap.FlushFile = FileHandleFlush;
241 FileFunctionMap.GetFileSize = FileHandleGetSize;
242 }
jcarsey94b17fa2009-05-07 18:46:18 +0000243 return (EFI_SUCCESS);
244 }
245 return (EFI_NOT_FOUND);
246}
jcarseyd2b45642009-05-11 18:02:16 +0000247/**
248 Constructor for the Shell library.
249
250 Initialize the library and determine if the underlying is a UEFI Shell 2.0 or an EFI shell.
251
252 @param ImageHandle the image handle of the process
253 @param SystemTable the EFI System Table pointer
254
255 @retval EFI_SUCCESS the initialization was complete sucessfully
256 @return others an error ocurred during initialization
257**/
258EFI_STATUS
259EFIAPI
260ShellLibConstructor (
261 IN EFI_HANDLE ImageHandle,
262 IN EFI_SYSTEM_TABLE *SystemTable
jcarsey2247dde2009-11-09 18:08:58 +0000263 ) {
jcarseyd2b45642009-05-11 18:02:16 +0000264
265
266 mEfiShellEnvironment2 = NULL;
267 mEfiShellProtocol = NULL;
268 mEfiShellParametersProtocol = NULL;
269 mEfiShellInterface = NULL;
270 mEfiShellEnvironment2Handle = NULL;
271
jcarseyd2b45642009-05-11 18:02:16 +0000272 //
273 // verify that auto initialize is not set false
274 //
275 if (PcdGetBool(PcdShellLibAutoInitialize) == 0) {
276 return (EFI_SUCCESS);
277 }
278
279 return (ShellLibConstructorWorker(ImageHandle, SystemTable));
280}
jcarsey94b17fa2009-05-07 18:46:18 +0000281
282/**
283 Destructory for the library. free any resources.
284**/
285EFI_STATUS
286EFIAPI
287ShellLibDestructor (
288 IN EFI_HANDLE ImageHandle,
289 IN EFI_SYSTEM_TABLE *SystemTable
jcarsey2247dde2009-11-09 18:08:58 +0000290 ) {
jcarsey94b17fa2009-05-07 18:46:18 +0000291 if (mEfiShellEnvironment2 != NULL) {
292 gBS->CloseProtocol(mEfiShellEnvironment2Handle==NULL?ImageHandle:mEfiShellEnvironment2Handle,
293 &gEfiShellEnvironment2Guid,
294 ImageHandle,
295 NULL);
jcarseyd2b45642009-05-11 18:02:16 +0000296 mEfiShellEnvironment2 = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +0000297 }
298 if (mEfiShellInterface != NULL) {
299 gBS->CloseProtocol(ImageHandle,
300 &gEfiShellInterfaceGuid,
301 ImageHandle,
302 NULL);
jcarseyd2b45642009-05-11 18:02:16 +0000303 mEfiShellInterface = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +0000304 }
305 if (mEfiShellProtocol != NULL) {
306 gBS->CloseProtocol(ImageHandle,
307 &gEfiShellProtocolGuid,
308 ImageHandle,
jcarseyd2b45642009-05-11 18:02:16 +0000309 NULL);
310 mEfiShellProtocol = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +0000311 }
312 if (mEfiShellParametersProtocol != NULL) {
313 gBS->CloseProtocol(ImageHandle,
314 &gEfiShellParametersProtocolGuid,
315 ImageHandle,
316 NULL);
jcarseyd2b45642009-05-11 18:02:16 +0000317 mEfiShellParametersProtocol = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +0000318 }
jcarseyd2b45642009-05-11 18:02:16 +0000319 mEfiShellEnvironment2Handle = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +0000320 return (EFI_SUCCESS);
321}
jcarseyd2b45642009-05-11 18:02:16 +0000322
323/**
324 This function causes the shell library to initialize itself. If the shell library
325 is already initialized it will de-initialize all the current protocol poitners and
326 re-populate them again.
327
328 When the library is used with PcdShellLibAutoInitialize set to true this function
329 will return EFI_SUCCESS and perform no actions.
330
331 This function is intended for internal access for shell commands only.
332
333 @retval EFI_SUCCESS the initialization was complete sucessfully
334
335**/
336EFI_STATUS
337EFIAPI
338ShellInitialize (
339 ) {
340 //
341 // if auto initialize is not false then skip
342 //
343 if (PcdGetBool(PcdShellLibAutoInitialize) != 0) {
344 return (EFI_SUCCESS);
345 }
346
347 //
348 // deinit the current stuff
349 //
350 ASSERT_EFI_ERROR(ShellLibDestructor(gImageHandle, gST));
351
352 //
353 // init the new stuff
354 //
355 return (ShellLibConstructorWorker(gImageHandle, gST));
356}
357
jcarsey94b17fa2009-05-07 18:46:18 +0000358/**
359 This function will retrieve the information about the file for the handle
360 specified and store it in allocated pool memory.
361
qhuang869817bf2009-05-20 14:42:48 +0000362 This function allocates a buffer to store the file's information. It is the
363 caller's responsibility to free the buffer
jcarsey94b17fa2009-05-07 18:46:18 +0000364
365 @param FileHandle The file handle of the file for which information is
366 being requested.
367
368 @retval NULL information could not be retrieved.
369
370 @return the information about the file
371**/
372EFI_FILE_INFO*
373EFIAPI
374ShellGetFileInfo (
375 IN EFI_FILE_HANDLE FileHandle
jcarsey2247dde2009-11-09 18:08:58 +0000376 ) {
jcarseyd2b45642009-05-11 18:02:16 +0000377 return (FileFunctionMap.GetFileInfo(FileHandle));
jcarsey94b17fa2009-05-07 18:46:18 +0000378}
379
380/**
381 This function will set the information about the file for the opened handle
382 specified.
383
384 @param FileHandle The file handle of the file for which information
385 is being set
386
387 @param FileInfo The infotmation to set.
388
389 @retval EFI_SUCCESS The information was set.
390 @retval EFI_UNSUPPORTED The InformationType is not known.
391 @retval EFI_NO_MEDIA The device has no medium.
392 @retval EFI_DEVICE_ERROR The device reported an error.
393 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
394 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
395 @retval EFI_ACCESS_DENIED The file was opened read only.
396 @retval EFI_VOLUME_FULL The volume is full.
397**/
398EFI_STATUS
399EFIAPI
400ShellSetFileInfo (
401 IN EFI_FILE_HANDLE FileHandle,
402 IN EFI_FILE_INFO *FileInfo
jcarsey2247dde2009-11-09 18:08:58 +0000403 ) {
jcarseyd2b45642009-05-11 18:02:16 +0000404 return (FileFunctionMap.SetFileInfo(FileHandle, FileInfo));
jcarsey94b17fa2009-05-07 18:46:18 +0000405}
406
407 /**
408 This function will open a file or directory referenced by DevicePath.
409
410 This function opens a file with the open mode according to the file path. The
411 Attributes is valid only for EFI_FILE_MODE_CREATE.
412
413 @param FilePath on input the device path to the file. On output
414 the remaining device path.
415 @param DeviceHandle pointer to the system device handle.
416 @param FileHandle pointer to the file handle.
417 @param OpenMode the mode to open the file with.
418 @param Attributes the file's file attributes.
419
420 @retval EFI_SUCCESS The information was set.
421 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
422 @retval EFI_UNSUPPORTED Could not open the file path.
423 @retval EFI_NOT_FOUND The specified file could not be found on the
424 device or the file system could not be found on
425 the device.
426 @retval EFI_NO_MEDIA The device has no medium.
427 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
428 medium is no longer supported.
429 @retval EFI_DEVICE_ERROR The device reported an error.
430 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
431 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
432 @retval EFI_ACCESS_DENIED The file was opened read only.
433 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
434 file.
435 @retval EFI_VOLUME_FULL The volume is full.
436**/
437EFI_STATUS
438EFIAPI
439ShellOpenFileByDevicePath(
440 IN OUT EFI_DEVICE_PATH_PROTOCOL **FilePath,
441 OUT EFI_HANDLE *DeviceHandle,
442 OUT EFI_FILE_HANDLE *FileHandle,
443 IN UINT64 OpenMode,
444 IN UINT64 Attributes
jcarsey2247dde2009-11-09 18:08:58 +0000445 ) {
jcarsey94b17fa2009-05-07 18:46:18 +0000446 CHAR16 *FileName;
447 EFI_STATUS Status;
448 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *EfiSimpleFileSystemProtocol;
449 EFI_FILE_HANDLE LastHandle;
450
451 //
452 // ASERT for FileHandle, FilePath, and DeviceHandle being NULL
453 //
454 ASSERT(FilePath != NULL);
455 ASSERT(FileHandle != NULL);
456 ASSERT(DeviceHandle != NULL);
457 //
458 // which shell interface should we use
459 //
460 if (mEfiShellProtocol != NULL) {
461 //
462 // use UEFI Shell 2.0 method.
463 //
464 FileName = mEfiShellProtocol->GetFilePathFromDevicePath(*FilePath);
465 if (FileName == NULL) {
466 return (EFI_INVALID_PARAMETER);
467 }
468 Status = ShellOpenFileByName(FileName, FileHandle, OpenMode, Attributes);
469 FreePool(FileName);
470 return (Status);
jcarseyd2b45642009-05-11 18:02:16 +0000471 }
472
473
474 //
475 // use old shell method.
476 //
477 Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid,
478 FilePath,
479 DeviceHandle);
480 if (EFI_ERROR (Status)) {
481 return Status;
482 }
483 Status = gBS->OpenProtocol(*DeviceHandle,
484 &gEfiSimpleFileSystemProtocolGuid,
jcarseyb1f95a02009-06-16 00:23:19 +0000485 (VOID**)&EfiSimpleFileSystemProtocol,
jcarseyd2b45642009-05-11 18:02:16 +0000486 gImageHandle,
487 NULL,
488 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
489 if (EFI_ERROR (Status)) {
490 return Status;
491 }
492 Status = EfiSimpleFileSystemProtocol->OpenVolume(EfiSimpleFileSystemProtocol, FileHandle);
493 if (EFI_ERROR (Status)) {
494 FileHandle = NULL;
495 return Status;
496 }
497
498 //
499 // go down directories one node at a time.
500 //
501 while (!IsDevicePathEnd (*FilePath)) {
jcarsey94b17fa2009-05-07 18:46:18 +0000502 //
jcarseyd2b45642009-05-11 18:02:16 +0000503 // For file system access each node should be a file path component
jcarsey94b17fa2009-05-07 18:46:18 +0000504 //
jcarseyd2b45642009-05-11 18:02:16 +0000505 if (DevicePathType (*FilePath) != MEDIA_DEVICE_PATH ||
506 DevicePathSubType (*FilePath) != MEDIA_FILEPATH_DP
507 ) {
jcarsey94b17fa2009-05-07 18:46:18 +0000508 FileHandle = NULL;
jcarseyd2b45642009-05-11 18:02:16 +0000509 return (EFI_INVALID_PARAMETER);
jcarsey94b17fa2009-05-07 18:46:18 +0000510 }
jcarseyd2b45642009-05-11 18:02:16 +0000511 //
512 // Open this file path node
513 //
514 LastHandle = *FileHandle;
515 *FileHandle = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +0000516
517 //
jcarseyd2b45642009-05-11 18:02:16 +0000518 // Try to test opening an existing file
jcarsey94b17fa2009-05-07 18:46:18 +0000519 //
jcarseyd2b45642009-05-11 18:02:16 +0000520 Status = LastHandle->Open (
521 LastHandle,
522 FileHandle,
523 ((FILEPATH_DEVICE_PATH*)*FilePath)->PathName,
524 OpenMode &~EFI_FILE_MODE_CREATE,
525 0
526 );
jcarsey94b17fa2009-05-07 18:46:18 +0000527
jcarseyd2b45642009-05-11 18:02:16 +0000528 //
529 // see if the error was that it needs to be created
530 //
531 if ((EFI_ERROR (Status)) && (OpenMode != (OpenMode &~EFI_FILE_MODE_CREATE))) {
jcarsey94b17fa2009-05-07 18:46:18 +0000532 Status = LastHandle->Open (
533 LastHandle,
534 FileHandle,
535 ((FILEPATH_DEVICE_PATH*)*FilePath)->PathName,
jcarseyd2b45642009-05-11 18:02:16 +0000536 OpenMode,
537 Attributes
jcarsey94b17fa2009-05-07 18:46:18 +0000538 );
jcarsey94b17fa2009-05-07 18:46:18 +0000539 }
jcarseyd2b45642009-05-11 18:02:16 +0000540 //
541 // Close the last node
542 //
543 LastHandle->Close (LastHandle);
544
545 if (EFI_ERROR(Status)) {
546 return (Status);
547 }
548
549 //
550 // Get the next node
551 //
552 *FilePath = NextDevicePathNode (*FilePath);
jcarsey94b17fa2009-05-07 18:46:18 +0000553 }
jcarseyd2b45642009-05-11 18:02:16 +0000554 return (EFI_SUCCESS);
jcarsey94b17fa2009-05-07 18:46:18 +0000555}
556
557/**
558 This function will open a file or directory referenced by filename.
559
qhuang869817bf2009-05-20 14:42:48 +0000560 If return is EFI_SUCCESS, the Filehandle is the opened file's handle;
jcarsey94b17fa2009-05-07 18:46:18 +0000561 otherwise, the Filehandle is NULL. The Attributes is valid only for
562 EFI_FILE_MODE_CREATE.
563
564 if FileNAme is NULL then ASSERT()
565
566 @param FileName pointer to file name
567 @param FileHandle pointer to the file handle.
568 @param OpenMode the mode to open the file with.
569 @param Attributes the file's file attributes.
570
571 @retval EFI_SUCCESS The information was set.
572 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
573 @retval EFI_UNSUPPORTED Could not open the file path.
574 @retval EFI_NOT_FOUND The specified file could not be found on the
575 device or the file system could not be found
576 on the device.
577 @retval EFI_NO_MEDIA The device has no medium.
578 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
579 medium is no longer supported.
580 @retval EFI_DEVICE_ERROR The device reported an error.
581 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
582 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
583 @retval EFI_ACCESS_DENIED The file was opened read only.
584 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
585 file.
586 @retval EFI_VOLUME_FULL The volume is full.
587**/
588EFI_STATUS
589EFIAPI
590ShellOpenFileByName(
jcarseyb82bfcc2009-06-29 16:28:23 +0000591 IN CONST CHAR16 *FileName,
jcarsey94b17fa2009-05-07 18:46:18 +0000592 OUT EFI_FILE_HANDLE *FileHandle,
593 IN UINT64 OpenMode,
594 IN UINT64 Attributes
jcarsey2247dde2009-11-09 18:08:58 +0000595 ) {
jcarsey94b17fa2009-05-07 18:46:18 +0000596 EFI_HANDLE DeviceHandle;
597 EFI_DEVICE_PATH_PROTOCOL *FilePath;
jcarseyb1f95a02009-06-16 00:23:19 +0000598 EFI_STATUS Status;
599 EFI_FILE_INFO *FileInfo;
jcarsey94b17fa2009-05-07 18:46:18 +0000600
601 //
602 // ASSERT if FileName is NULL
603 //
604 ASSERT(FileName != NULL);
605
606 if (mEfiShellProtocol != NULL) {
607 //
608 // Use UEFI Shell 2.0 method
609 //
jcarseyb1f95a02009-06-16 00:23:19 +0000610 Status = mEfiShellProtocol->OpenFileByName(FileName,
611 FileHandle,
612 OpenMode);
jcarsey2247dde2009-11-09 18:08:58 +0000613 if (!EFI_ERROR(Status) && ((OpenMode & EFI_FILE_MODE_CREATE) != 0)){
614 FileInfo = FileFunctionMap.GetFileInfo(*FileHandle);
jcarseyb1f95a02009-06-16 00:23:19 +0000615 ASSERT(FileInfo != NULL);
616 FileInfo->Attribute = Attributes;
jcarsey2247dde2009-11-09 18:08:58 +0000617 Status = FileFunctionMap.SetFileInfo(*FileHandle, FileInfo);
618 FreePool(FileInfo);
jcarseyb1f95a02009-06-16 00:23:19 +0000619 }
620 return (Status);
jcarsey94b17fa2009-05-07 18:46:18 +0000621 }
622 //
623 // Using EFI Shell version
624 // this means convert name to path and call that function
625 // since this will use EFI method again that will open it.
626 //
627 ASSERT(mEfiShellEnvironment2 != NULL);
jcarseyb82bfcc2009-06-29 16:28:23 +0000628 FilePath = mEfiShellEnvironment2->NameToPath ((CHAR16*)FileName);
jcarsey94b17fa2009-05-07 18:46:18 +0000629 if (FileDevicePath != NULL) {
630 return (ShellOpenFileByDevicePath(&FilePath,
631 &DeviceHandle,
632 FileHandle,
633 OpenMode,
634 Attributes ));
635 }
636 return (EFI_DEVICE_ERROR);
637}
638/**
639 This function create a directory
640
641 If return is EFI_SUCCESS, the Filehandle is the opened directory's handle;
642 otherwise, the Filehandle is NULL. If the directory already existed, this
643 function opens the existing directory.
644
645 @param DirectoryName pointer to directory name
646 @param FileHandle pointer to the file handle.
647
648 @retval EFI_SUCCESS The information was set.
649 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
650 @retval EFI_UNSUPPORTED Could not open the file path.
651 @retval EFI_NOT_FOUND The specified file could not be found on the
652 device or the file system could not be found
653 on the device.
654 @retval EFI_NO_MEDIA The device has no medium.
655 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
656 medium is no longer supported.
657 @retval EFI_DEVICE_ERROR The device reported an error.
658 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
659 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
660 @retval EFI_ACCESS_DENIED The file was opened read only.
661 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
662 file.
663 @retval EFI_VOLUME_FULL The volume is full.
664 @sa ShellOpenFileByName
665**/
666EFI_STATUS
667EFIAPI
668ShellCreateDirectory(
jcarseyb82bfcc2009-06-29 16:28:23 +0000669 IN CONST CHAR16 *DirectoryName,
jcarsey94b17fa2009-05-07 18:46:18 +0000670 OUT EFI_FILE_HANDLE *FileHandle
jcarsey2247dde2009-11-09 18:08:58 +0000671 ) {
672 if (mEfiShellProtocol != NULL) {
673 //
674 // Use UEFI Shell 2.0 method
675 //
676 return (mEfiShellProtocol->CreateFile(DirectoryName,
677 EFI_FILE_DIRECTORY,
678 FileHandle
679 ));
680 } else {
681 return (ShellOpenFileByName(DirectoryName,
682 FileHandle,
683 EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE,
684 EFI_FILE_DIRECTORY
685 ));
686 }
jcarsey94b17fa2009-05-07 18:46:18 +0000687}
688
689/**
690 This function reads information from an opened file.
691
692 If FileHandle is not a directory, the function reads the requested number of
qhuang869817bf2009-05-20 14:42:48 +0000693 bytes from the file at the file's current position and returns them in Buffer.
jcarsey94b17fa2009-05-07 18:46:18 +0000694 If the read goes beyond the end of the file, the read length is truncated to the
qhuang869817bf2009-05-20 14:42:48 +0000695 end of the file. The file's current position is increased by the number of bytes
jcarsey94b17fa2009-05-07 18:46:18 +0000696 returned. If FileHandle is a directory, the function reads the directory entry
qhuang869817bf2009-05-20 14:42:48 +0000697 at the file's current position and returns the entry in Buffer. If the Buffer
jcarsey94b17fa2009-05-07 18:46:18 +0000698 is not large enough to hold the current directory entry, then
699 EFI_BUFFER_TOO_SMALL is returned and the current file position is not updated.
700 BufferSize is set to be the size of the buffer needed to read the entry. On
701 success, the current position is updated to the next directory entry. If there
702 are no more directory entries, the read returns a zero-length buffer.
703 EFI_FILE_INFO is the structure returned as the directory entry.
704
705 @param FileHandle the opened file handle
706 @param BufferSize on input the size of buffer in bytes. on return
707 the number of bytes written.
708 @param Buffer the buffer to put read data into.
709
710 @retval EFI_SUCCESS Data was read.
711 @retval EFI_NO_MEDIA The device has no media.
712 @retval EFI_DEVICE_ERROR The device reported an error.
713 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
714 @retval EFI_BUFFER_TO_SMALL Buffer is too small. ReadSize contains required
715 size.
716
717**/
718EFI_STATUS
719EFIAPI
720ShellReadFile(
721 IN EFI_FILE_HANDLE FileHandle,
722 IN OUT UINTN *BufferSize,
723 OUT VOID *Buffer
jcarsey2247dde2009-11-09 18:08:58 +0000724 ) {
jcarseyd2b45642009-05-11 18:02:16 +0000725 return (FileFunctionMap.ReadFile(FileHandle, BufferSize, Buffer));
jcarsey94b17fa2009-05-07 18:46:18 +0000726}
727
728
729/**
730 Write data to a file.
731
732 This function writes the specified number of bytes to the file at the current
733 file position. The current file position is advanced the actual number of bytes
734 written, which is returned in BufferSize. Partial writes only occur when there
qhuang869817bf2009-05-20 14:42:48 +0000735 has been a data error during the write attempt (such as "volume space full").
jcarsey94b17fa2009-05-07 18:46:18 +0000736 The file is automatically grown to hold the data if required. Direct writes to
737 opened directories are not supported.
738
739 @param FileHandle The opened file for writing
740 @param BufferSize on input the number of bytes in Buffer. On output
741 the number of bytes written.
742 @param Buffer the buffer containing data to write is stored.
743
744 @retval EFI_SUCCESS Data was written.
745 @retval EFI_UNSUPPORTED Writes to an open directory are not supported.
746 @retval EFI_NO_MEDIA The device has no media.
747 @retval EFI_DEVICE_ERROR The device reported an error.
748 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
749 @retval EFI_WRITE_PROTECTED The device is write-protected.
750 @retval EFI_ACCESS_DENIED The file was open for read only.
751 @retval EFI_VOLUME_FULL The volume is full.
752**/
753EFI_STATUS
754EFIAPI
755ShellWriteFile(
756 IN EFI_FILE_HANDLE FileHandle,
757 IN OUT UINTN *BufferSize,
758 IN VOID *Buffer
jcarsey2247dde2009-11-09 18:08:58 +0000759 ) {
jcarseyd2b45642009-05-11 18:02:16 +0000760 return (FileFunctionMap.WriteFile(FileHandle, BufferSize, Buffer));
jcarsey94b17fa2009-05-07 18:46:18 +0000761}
762
763/**
764 Close an open file handle.
765
qhuang869817bf2009-05-20 14:42:48 +0000766 This function closes a specified file handle. All "dirty" cached file data is
jcarsey94b17fa2009-05-07 18:46:18 +0000767 flushed to the device, and the file is closed. In all cases the handle is
768 closed.
769
770@param FileHandle the file handle to close.
771
772@retval EFI_SUCCESS the file handle was closed sucessfully.
773**/
774EFI_STATUS
775EFIAPI
776ShellCloseFile (
777 IN EFI_FILE_HANDLE *FileHandle
jcarsey2247dde2009-11-09 18:08:58 +0000778 ) {
jcarseyd2b45642009-05-11 18:02:16 +0000779 return (FileFunctionMap.CloseFile(*FileHandle));
jcarsey94b17fa2009-05-07 18:46:18 +0000780}
781
782/**
783 Delete a file and close the handle
784
785 This function closes and deletes a file. In all cases the file handle is closed.
786 If the file cannot be deleted, the warning code EFI_WARN_DELETE_FAILURE is
787 returned, but the handle is still closed.
788
789 @param FileHandle the file handle to delete
790
791 @retval EFI_SUCCESS the file was closed sucessfully
792 @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not
793 deleted
794 @retval INVALID_PARAMETER One of the parameters has an invalid value.
795**/
796EFI_STATUS
797EFIAPI
798ShellDeleteFile (
799 IN EFI_FILE_HANDLE *FileHandle
jcarsey2247dde2009-11-09 18:08:58 +0000800 ) {
jcarseyd2b45642009-05-11 18:02:16 +0000801 return (FileFunctionMap.DeleteFile(*FileHandle));
jcarsey94b17fa2009-05-07 18:46:18 +0000802}
803
804/**
805 Set the current position in a file.
806
807 This function sets the current file position for the handle to the position
808 supplied. With the exception of seeking to position 0xFFFFFFFFFFFFFFFF, only
809 absolute positioning is supported, and seeking past the end of the file is
810 allowed (a subsequent write would grow the file). Seeking to position
811 0xFFFFFFFFFFFFFFFF causes the current position to be set to the end of the file.
812 If FileHandle is a directory, the only position that may be set is zero. This
813 has the effect of starting the read process of the directory entries over.
814
815 @param FileHandle The file handle on which the position is being set
816 @param Position Byte position from begining of file
817
818 @retval EFI_SUCCESS Operation completed sucessfully.
819 @retval EFI_UNSUPPORTED the seek request for non-zero is not valid on
820 directories.
821 @retval INVALID_PARAMETER One of the parameters has an invalid value.
822**/
823EFI_STATUS
824EFIAPI
825ShellSetFilePosition (
826 IN EFI_FILE_HANDLE FileHandle,
827 IN UINT64 Position
jcarsey2247dde2009-11-09 18:08:58 +0000828 ) {
jcarseyd2b45642009-05-11 18:02:16 +0000829 return (FileFunctionMap.SetFilePosition(FileHandle, Position));
jcarsey94b17fa2009-05-07 18:46:18 +0000830}
831
832/**
833 Gets a file's current position
834
835 This function retrieves the current file position for the file handle. For
836 directories, the current file position has no meaning outside of the file
837 system driver and as such the operation is not supported. An error is returned
838 if FileHandle is a directory.
839
840 @param FileHandle The open file handle on which to get the position.
841 @param Position Byte position from begining of file.
842
843 @retval EFI_SUCCESS the operation completed sucessfully.
844 @retval INVALID_PARAMETER One of the parameters has an invalid value.
845 @retval EFI_UNSUPPORTED the request is not valid on directories.
846**/
847EFI_STATUS
848EFIAPI
849ShellGetFilePosition (
850 IN EFI_FILE_HANDLE FileHandle,
851 OUT UINT64 *Position
jcarsey2247dde2009-11-09 18:08:58 +0000852 ) {
jcarseyd2b45642009-05-11 18:02:16 +0000853 return (FileFunctionMap.GetFilePosition(FileHandle, Position));
jcarsey94b17fa2009-05-07 18:46:18 +0000854}
855/**
856 Flushes data on a file
857
858 This function flushes all modified data associated with a file to a device.
859
860 @param FileHandle The file handle on which to flush data
861
862 @retval EFI_SUCCESS The data was flushed.
863 @retval EFI_NO_MEDIA The device has no media.
864 @retval EFI_DEVICE_ERROR The device reported an error.
865 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
866 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
867 @retval EFI_ACCESS_DENIED The file was opened for read only.
868**/
869EFI_STATUS
870EFIAPI
871ShellFlushFile (
872 IN EFI_FILE_HANDLE FileHandle
jcarsey2247dde2009-11-09 18:08:58 +0000873 ) {
jcarseyd2b45642009-05-11 18:02:16 +0000874 return (FileFunctionMap.FlushFile(FileHandle));
jcarsey94b17fa2009-05-07 18:46:18 +0000875}
876
877/**
878 Retrieves the first file from a directory
879
qhuang869817bf2009-05-20 14:42:48 +0000880 This function opens a directory and gets the first file's info in the
jcarsey94b17fa2009-05-07 18:46:18 +0000881 directory. Caller can use ShellFindNextFile() to get other files. When
882 complete the caller is responsible for calling FreePool() on Buffer.
883
884 @param DirHandle The file handle of the directory to search
885 @param Buffer Pointer to buffer for file's information
886
887 @retval EFI_SUCCESS Found the first file.
888 @retval EFI_NOT_FOUND Cannot find the directory.
889 @retval EFI_NO_MEDIA The device has no media.
890 @retval EFI_DEVICE_ERROR The device reported an error.
891 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
892 @return Others status of ShellGetFileInfo, ShellSetFilePosition,
893 or ShellReadFile
894**/
895EFI_STATUS
896EFIAPI
897ShellFindFirstFile (
898 IN EFI_FILE_HANDLE DirHandle,
jcarseyd2b45642009-05-11 18:02:16 +0000899 OUT EFI_FILE_INFO **Buffer
jcarsey2247dde2009-11-09 18:08:58 +0000900 ) {
jcarsey94b17fa2009-05-07 18:46:18 +0000901 //
jcarseyd2b45642009-05-11 18:02:16 +0000902 // pass to file handle lib
jcarsey94b17fa2009-05-07 18:46:18 +0000903 //
jcarseyd2b45642009-05-11 18:02:16 +0000904 return (FileHandleFindFirstFile(DirHandle, Buffer));
jcarsey94b17fa2009-05-07 18:46:18 +0000905}
906/**
907 Retrieves the next file in a directory.
908
909 To use this function, caller must call the LibFindFirstFile() to get the
910 first file, and then use this function get other files. This function can be
911 called for several times to get each file's information in the directory. If
912 the call of ShellFindNextFile() got the last file in the directory, the next
913 call of this function has no file to get. *NoFile will be set to TRUE and the
914 Buffer memory will be automatically freed.
915
916 @param DirHandle the file handle of the directory
917 @param Buffer pointer to buffer for file's information
918 @param NoFile pointer to boolean when last file is found
919
920 @retval EFI_SUCCESS Found the next file, or reached last file
921 @retval EFI_NO_MEDIA The device has no media.
922 @retval EFI_DEVICE_ERROR The device reported an error.
923 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
924**/
925EFI_STATUS
926EFIAPI
927ShellFindNextFile(
928 IN EFI_FILE_HANDLE DirHandle,
929 OUT EFI_FILE_INFO *Buffer,
930 OUT BOOLEAN *NoFile
jcarsey2247dde2009-11-09 18:08:58 +0000931 ) {
jcarsey94b17fa2009-05-07 18:46:18 +0000932 //
jcarseyd2b45642009-05-11 18:02:16 +0000933 // pass to file handle lib
jcarsey94b17fa2009-05-07 18:46:18 +0000934 //
jcarseyd2b45642009-05-11 18:02:16 +0000935 return (FileHandleFindNextFile(DirHandle, Buffer, NoFile));
jcarsey94b17fa2009-05-07 18:46:18 +0000936}
937/**
938 Retrieve the size of a file.
939
940 if FileHandle is NULL then ASSERT()
941 if Size is NULL then ASSERT()
942
qhuang869817bf2009-05-20 14:42:48 +0000943 This function extracts the file size info from the FileHandle's EFI_FILE_INFO
jcarsey94b17fa2009-05-07 18:46:18 +0000944 data.
945
946 @param FileHandle file handle from which size is retrieved
947 @param Size pointer to size
948
949 @retval EFI_SUCCESS operation was completed sucessfully
950 @retval EFI_DEVICE_ERROR cannot access the file
951**/
952EFI_STATUS
953EFIAPI
954ShellGetFileSize (
955 IN EFI_FILE_HANDLE FileHandle,
956 OUT UINT64 *Size
jcarsey2247dde2009-11-09 18:08:58 +0000957 ) {
jcarseyd2b45642009-05-11 18:02:16 +0000958 return (FileFunctionMap.GetFileSize(FileHandle, Size));
jcarsey94b17fa2009-05-07 18:46:18 +0000959}
960/**
961 Retrieves the status of the break execution flag
962
963 this function is useful to check whether the application is being asked to halt by the shell.
964
965 @retval TRUE the execution break is enabled
966 @retval FALSE the execution break is not enabled
967**/
968BOOLEAN
969EFIAPI
970ShellGetExecutionBreakFlag(
971 VOID
972 )
973{
974 //
975 // Check for UEFI Shell 2.0 protocols
976 //
977 if (mEfiShellProtocol != NULL) {
978
979 //
980 // We are using UEFI Shell 2.0; see if the event has been triggered
981 //
982 if (gBS->CheckEvent(mEfiShellProtocol->ExecutionBreak) != EFI_SUCCESS) {
983 return (FALSE);
984 }
985 return (TRUE);
986 }
987
988 //
989 // using EFI Shell; call the function to check
990 //
991 ASSERT(mEfiShellEnvironment2 != NULL);
992 return (mEfiShellEnvironment2->GetExecutionBreak());
993}
994/**
995 return the value of an environment variable
996
997 this function gets the value of the environment variable set by the
998 ShellSetEnvironmentVariable function
999
1000 @param EnvKey The key name of the environment variable.
1001
1002 @retval NULL the named environment variable does not exist.
1003 @return != NULL pointer to the value of the environment variable
1004**/
1005CONST CHAR16*
1006EFIAPI
1007ShellGetEnvironmentVariable (
jcarsey9b3bf082009-06-23 21:15:07 +00001008 IN CONST CHAR16 *EnvKey
jcarsey94b17fa2009-05-07 18:46:18 +00001009 )
1010{
1011 //
1012 // Check for UEFI Shell 2.0 protocols
1013 //
1014 if (mEfiShellProtocol != NULL) {
1015 return (mEfiShellProtocol->GetEnv(EnvKey));
1016 }
1017
1018 //
1019 // ASSERT that we must have EFI shell
1020 //
1021 ASSERT(mEfiShellEnvironment2 != NULL);
1022
1023 //
1024 // using EFI Shell
1025 //
jcarsey9b3bf082009-06-23 21:15:07 +00001026 return (mEfiShellEnvironment2->GetEnv((CHAR16*)EnvKey));
jcarsey94b17fa2009-05-07 18:46:18 +00001027}
1028/**
1029 set the value of an environment variable
1030
1031This function changes the current value of the specified environment variable. If the
1032environment variable exists and the Value is an empty string, then the environment
1033variable is deleted. If the environment variable exists and the Value is not an empty
1034string, then the value of the environment variable is changed. If the environment
1035variable does not exist and the Value is an empty string, there is no action. If the
1036environment variable does not exist and the Value is a non-empty string, then the
1037environment variable is created and assigned the specified value.
1038
1039 This is not supported pre-UEFI Shell 2.0.
1040
1041 @param EnvKey The key name of the environment variable.
1042 @param EnvVal The Value of the environment variable
1043 @param Volatile Indicates whether the variable is non-volatile (FALSE) or volatile (TRUE).
1044
1045 @retval EFI_SUCCESS the operation was completed sucessfully
1046 @retval EFI_UNSUPPORTED This operation is not allowed in pre UEFI 2.0 Shell environments
1047**/
1048EFI_STATUS
1049EFIAPI
1050ShellSetEnvironmentVariable (
1051 IN CONST CHAR16 *EnvKey,
1052 IN CONST CHAR16 *EnvVal,
1053 IN BOOLEAN Volatile
1054 )
1055{
1056 //
1057 // Check for UEFI Shell 2.0 protocols
1058 //
1059 if (mEfiShellProtocol != NULL) {
1060 return (mEfiShellProtocol->SetEnv(EnvKey, EnvVal, Volatile));
1061 }
1062
1063 //
1064 // This feature does not exist under EFI shell
1065 //
1066 return (EFI_UNSUPPORTED);
1067}
1068/**
1069 cause the shell to parse and execute a command line.
1070
1071 This function creates a nested instance of the shell and executes the specified
1072command (CommandLine) with the specified environment (Environment). Upon return,
1073the status code returned by the specified command is placed in StatusCode.
1074If Environment is NULL, then the current environment is used and all changes made
1075by the commands executed will be reflected in the current environment. If the
1076Environment is non-NULL, then the changes made will be discarded.
1077The CommandLine is executed from the current working directory on the current
1078device.
1079
1080EnvironmentVariables and Status are only supported for UEFI Shell 2.0.
1081Output is only supported for pre-UEFI Shell 2.0
1082
1083 @param ImageHandle Parent image that is starting the operation
1084 @param CommandLine pointer to null terminated command line.
1085 @param Output true to display debug output. false to hide it.
1086 @param EnvironmentVariables optional pointer to array of environment variables
1087 in the form "x=y". if NULL current set is used.
1088 @param Status the status of the run command line.
1089
1090 @retval EFI_SUCCESS the operation completed sucessfully. Status
1091 contains the status code returned.
1092 @retval EFI_INVALID_PARAMETER a parameter contains an invalid value
1093 @retval EFI_OUT_OF_RESOURCES out of resources
1094 @retval EFI_UNSUPPORTED the operation is not allowed.
1095**/
1096EFI_STATUS
1097EFIAPI
1098ShellExecute (
1099 IN EFI_HANDLE *ParentHandle,
1100 IN CHAR16 *CommandLine OPTIONAL,
1101 IN BOOLEAN Output OPTIONAL,
1102 IN CHAR16 **EnvironmentVariables OPTIONAL,
1103 OUT EFI_STATUS *Status OPTIONAL
1104 )
1105{
1106 //
1107 // Check for UEFI Shell 2.0 protocols
1108 //
1109 if (mEfiShellProtocol != NULL) {
1110 //
1111 // Call UEFI Shell 2.0 version (not using Output parameter)
1112 //
1113 return (mEfiShellProtocol->Execute(ParentHandle,
1114 CommandLine,
1115 EnvironmentVariables,
1116 Status));
1117 }
1118 //
1119 // ASSERT that we must have EFI shell
1120 //
1121 ASSERT(mEfiShellEnvironment2 != NULL);
1122 //
1123 // Call EFI Shell version (not using EnvironmentVariables or Status parameters)
1124 // Due to oddity in the EFI shell we want to dereference the ParentHandle here
1125 //
1126 return (mEfiShellEnvironment2->Execute(*ParentHandle,
1127 CommandLine,
1128 Output));
1129}
1130/**
1131 Retreives the current directory path
1132
qhuang869817bf2009-05-20 14:42:48 +00001133 If the DeviceName is NULL, it returns the current device's current directory
jcarsey94b17fa2009-05-07 18:46:18 +00001134 name. If the DeviceName is not NULL, it returns the current directory name
1135 on specified drive.
1136
1137 @param DeviceName the name of the drive to get directory on
1138
1139 @retval NULL the directory does not exist
1140 @return != NULL the directory
1141**/
1142CONST CHAR16*
1143EFIAPI
1144ShellGetCurrentDir (
1145 IN CHAR16 *DeviceName OPTIONAL
1146 )
1147{
1148 //
1149 // Check for UEFI Shell 2.0 protocols
1150 //
1151 if (mEfiShellProtocol != NULL) {
1152 return (mEfiShellProtocol->GetCurDir(DeviceName));
1153 }
1154 //
1155 // ASSERT that we must have EFI shell
1156 //
1157 ASSERT(mEfiShellEnvironment2 != NULL);
1158 return (mEfiShellEnvironment2->CurDir(DeviceName));
1159}
1160/**
1161 sets (enabled or disabled) the page break mode
1162
1163 when page break mode is enabled the screen will stop scrolling
1164 and wait for operator input before scrolling a subsequent screen.
1165
1166 @param CurrentState TRUE to enable and FALSE to disable
1167**/
1168VOID
1169EFIAPI
1170ShellSetPageBreakMode (
1171 IN BOOLEAN CurrentState
1172 )
1173{
1174 //
1175 // check for enabling
1176 //
1177 if (CurrentState != 0x00) {
1178 //
1179 // check for UEFI Shell 2.0
1180 //
1181 if (mEfiShellProtocol != NULL) {
1182 //
1183 // Enable with UEFI 2.0 Shell
1184 //
1185 mEfiShellProtocol->EnablePageBreak();
1186 return;
1187 } else {
1188 //
1189 // ASSERT that must have EFI Shell
1190 //
1191 ASSERT(mEfiShellEnvironment2 != NULL);
1192 //
1193 // Enable with EFI Shell
1194 //
1195 mEfiShellEnvironment2->EnablePageBreak (DEFAULT_INIT_ROW, DEFAULT_AUTO_LF);
1196 return;
1197 }
1198 } else {
1199 //
1200 // check for UEFI Shell 2.0
1201 //
1202 if (mEfiShellProtocol != NULL) {
1203 //
1204 // Disable with UEFI 2.0 Shell
1205 //
1206 mEfiShellProtocol->DisablePageBreak();
1207 return;
1208 } else {
1209 //
1210 // ASSERT that must have EFI Shell
1211 //
1212 ASSERT(mEfiShellEnvironment2 != NULL);
1213 //
1214 // Disable with EFI Shell
1215 //
1216 mEfiShellEnvironment2->DisablePageBreak ();
1217 return;
1218 }
1219 }
1220}
1221
1222///
1223/// version of EFI_SHELL_FILE_INFO struct, except has no CONST pointers.
1224/// This allows for the struct to be populated.
1225///
1226typedef struct {
jcarseyd2b45642009-05-11 18:02:16 +00001227 LIST_ENTRY Link;
jcarsey94b17fa2009-05-07 18:46:18 +00001228 EFI_STATUS Status;
1229 CHAR16 *FullName;
1230 CHAR16 *FileName;
1231 EFI_FILE_HANDLE Handle;
1232 EFI_FILE_INFO *Info;
1233} EFI_SHELL_FILE_INFO_NO_CONST;
1234
1235/**
1236 Converts a EFI shell list of structures to the coresponding UEFI Shell 2.0 type of list.
1237
1238 if OldStyleFileList is NULL then ASSERT()
1239
1240 this function will convert a SHELL_FILE_ARG based list into a callee allocated
1241 EFI_SHELL_FILE_INFO based list. it is up to the caller to free the memory via
1242 the ShellCloseFileMetaArg function.
1243
jcarsey9b3bf082009-06-23 21:15:07 +00001244 @param[in] FileList the EFI shell list type
jcarseyb82bfcc2009-06-29 16:28:23 +00001245 @param[in,out] ListHead the list to add to
jcarsey94b17fa2009-05-07 18:46:18 +00001246
1247 @retval the resultant head of the double linked new format list;
1248**/
1249LIST_ENTRY*
1250EFIAPI
1251InternalShellConvertFileListType (
jcarsey9b3bf082009-06-23 21:15:07 +00001252 IN LIST_ENTRY *FileList,
1253 IN OUT LIST_ENTRY *ListHead
jcarsey125c2cf2009-11-18 21:36:50 +00001254 )
1255{
jcarsey94b17fa2009-05-07 18:46:18 +00001256 SHELL_FILE_ARG *OldInfo;
jcarsey9b3bf082009-06-23 21:15:07 +00001257 LIST_ENTRY *Link;
jcarsey94b17fa2009-05-07 18:46:18 +00001258 EFI_SHELL_FILE_INFO_NO_CONST *NewInfo;
1259
1260 //
jcarsey9b3bf082009-06-23 21:15:07 +00001261 // ASSERTs
jcarsey94b17fa2009-05-07 18:46:18 +00001262 //
jcarsey9b3bf082009-06-23 21:15:07 +00001263 ASSERT(FileList != NULL);
1264 ASSERT(ListHead != NULL);
jcarsey94b17fa2009-05-07 18:46:18 +00001265
1266 //
1267 // enumerate through each member of the old list and copy
1268 //
jcarseyd2b45642009-05-11 18:02:16 +00001269 for (Link = FileList->ForwardLink; Link != FileList; Link = Link->ForwardLink) {
jcarsey94b17fa2009-05-07 18:46:18 +00001270 OldInfo = CR (Link, SHELL_FILE_ARG, Link, SHELL_FILE_ARG_SIGNATURE);
1271
1272 //
1273 // make sure the old list was valid
1274 //
1275 ASSERT(OldInfo != NULL);
1276 ASSERT(OldInfo->Info != NULL);
1277 ASSERT(OldInfo->FullName != NULL);
1278 ASSERT(OldInfo->FileName != NULL);
1279
1280 //
1281 // allocate a new EFI_SHELL_FILE_INFO object
1282 //
1283 NewInfo = AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));
1284
1285 //
1286 // copy the simple items
1287 //
1288 NewInfo->Handle = OldInfo->Handle;
1289 NewInfo->Status = OldInfo->Status;
1290
jcarseyd2b45642009-05-11 18:02:16 +00001291 // old shell checks for 0 not NULL
1292 OldInfo->Handle = 0;
1293
jcarsey94b17fa2009-05-07 18:46:18 +00001294 //
1295 // allocate new space to copy strings and structure
1296 //
1297 NewInfo->FullName = AllocateZeroPool(StrSize(OldInfo->FullName));
1298 NewInfo->FileName = AllocateZeroPool(StrSize(OldInfo->FileName));
1299 NewInfo->Info = AllocateZeroPool((UINTN)OldInfo->Info->Size);
1300
1301 //
1302 // make sure all the memory allocations were sucessful
1303 //
1304 ASSERT(NewInfo->FullName != NULL);
1305 ASSERT(NewInfo->FileName != NULL);
1306 ASSERT(NewInfo->Info != NULL);
1307
1308 //
1309 // Copt the strings and structure
1310 //
1311 StrCpy(NewInfo->FullName, OldInfo->FullName);
1312 StrCpy(NewInfo->FileName, OldInfo->FileName);
1313 gBS->CopyMem (NewInfo->Info, OldInfo->Info, (UINTN)OldInfo->Info->Size);
1314
1315 //
1316 // add that to the list
1317 //
jcarsey9b3bf082009-06-23 21:15:07 +00001318 InsertTailList(ListHead, &NewInfo->Link);
jcarsey94b17fa2009-05-07 18:46:18 +00001319 }
1320 return (ListHead);
1321}
1322/**
1323 Opens a group of files based on a path.
1324
1325 This function uses the Arg to open all the matching files. Each matched
1326 file has a SHELL_FILE_ARG structure to record the file information. These
1327 structures are placed on the list ListHead. Users can get the SHELL_FILE_ARG
1328 structures from ListHead to access each file. This function supports wildcards
1329 and will process '?' and '*' as such. the list must be freed with a call to
1330 ShellCloseFileMetaArg().
1331
jcarsey5f7431d2009-07-10 18:06:01 +00001332 If you are NOT appending to an existing list *ListHead must be NULL. If
1333 *ListHead is NULL then it must be callee freed.
jcarsey94b17fa2009-05-07 18:46:18 +00001334
1335 @param Arg pointer to path string
1336 @param OpenMode mode to open files with
1337 @param ListHead head of linked list of results
1338
1339 @retval EFI_SUCCESS the operation was sucessful and the list head
1340 contains the list of opened files
1341 #retval EFI_UNSUPPORTED a previous ShellOpenFileMetaArg must be closed first.
1342 *ListHead is set to NULL.
1343 @return != EFI_SUCCESS the operation failed
1344
1345 @sa InternalShellConvertFileListType
1346**/
1347EFI_STATUS
1348EFIAPI
1349ShellOpenFileMetaArg (
1350 IN CHAR16 *Arg,
1351 IN UINT64 OpenMode,
1352 IN OUT EFI_SHELL_FILE_INFO **ListHead
1353 )
1354{
1355 EFI_STATUS Status;
jcarsey9b3bf082009-06-23 21:15:07 +00001356 LIST_ENTRY mOldStyleFileList;
jcarseyd2b45642009-05-11 18:02:16 +00001357
jcarsey94b17fa2009-05-07 18:46:18 +00001358 //
1359 // ASSERT that Arg and ListHead are not NULL
1360 //
1361 ASSERT(Arg != NULL);
1362 ASSERT(ListHead != NULL);
1363
1364 //
1365 // Check for UEFI Shell 2.0 protocols
1366 //
1367 if (mEfiShellProtocol != NULL) {
jcarsey5f7431d2009-07-10 18:06:01 +00001368 if (*ListHead == NULL) {
1369 *ListHead = (EFI_SHELL_FILE_INFO*)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));
1370 if (*ListHead == NULL) {
1371 return (EFI_OUT_OF_RESOURCES);
1372 }
1373 InitializeListHead(&((*ListHead)->Link));
1374 }
jcarsey2247dde2009-11-09 18:08:58 +00001375 Status = mEfiShellProtocol->OpenFileList(Arg,
jcarsey94b17fa2009-05-07 18:46:18 +00001376 OpenMode,
jcarsey2247dde2009-11-09 18:08:58 +00001377 ListHead);
1378 if (EFI_ERROR(Status)) {
1379 mEfiShellProtocol->RemoveDupInFileList(ListHead);
1380 } else {
1381 Status = mEfiShellProtocol->RemoveDupInFileList(ListHead);
1382 }
1383 return (Status);
jcarsey94b17fa2009-05-07 18:46:18 +00001384 }
1385
1386 //
1387 // ASSERT that we must have EFI shell
1388 //
1389 ASSERT(mEfiShellEnvironment2 != NULL);
1390
1391 //
jcarsey94b17fa2009-05-07 18:46:18 +00001392 // make sure the list head is initialized
1393 //
jcarsey9b3bf082009-06-23 21:15:07 +00001394 InitializeListHead(&mOldStyleFileList);
jcarsey94b17fa2009-05-07 18:46:18 +00001395
1396 //
1397 // Get the EFI Shell list of files
1398 //
jcarsey9b3bf082009-06-23 21:15:07 +00001399 Status = mEfiShellEnvironment2->FileMetaArg(Arg, &mOldStyleFileList);
jcarsey94b17fa2009-05-07 18:46:18 +00001400 if (EFI_ERROR(Status)) {
1401 *ListHead = NULL;
1402 return (Status);
1403 }
1404
jcarsey9b3bf082009-06-23 21:15:07 +00001405 if (*ListHead == NULL) {
1406 *ListHead = (EFI_SHELL_FILE_INFO *)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));
1407 if (*ListHead == NULL) {
1408 return (EFI_OUT_OF_RESOURCES);
1409 }
1410 }
1411
jcarsey94b17fa2009-05-07 18:46:18 +00001412 //
1413 // Convert that to equivalent of UEFI Shell 2.0 structure
1414 //
jcarsey9b3bf082009-06-23 21:15:07 +00001415 InternalShellConvertFileListType(&mOldStyleFileList, &(*ListHead)->Link);
jcarsey94b17fa2009-05-07 18:46:18 +00001416
1417 //
jcarseyd2b45642009-05-11 18:02:16 +00001418 // Free the EFI Shell version that was converted.
1419 //
jcarsey9b3bf082009-06-23 21:15:07 +00001420 mEfiShellEnvironment2->FreeFileList(&mOldStyleFileList);
jcarsey94b17fa2009-05-07 18:46:18 +00001421
1422 return (Status);
1423}
1424/**
1425 Free the linked list returned from ShellOpenFileMetaArg
1426
1427 if ListHead is NULL then ASSERT()
1428
1429 @param ListHead the pointer to free
1430
1431 @retval EFI_SUCCESS the operation was sucessful
1432**/
1433EFI_STATUS
1434EFIAPI
1435ShellCloseFileMetaArg (
1436 IN OUT EFI_SHELL_FILE_INFO **ListHead
1437 )
1438{
1439 LIST_ENTRY *Node;
1440
1441 //
1442 // ASSERT that ListHead is not NULL
1443 //
1444 ASSERT(ListHead != NULL);
1445
1446 //
1447 // Check for UEFI Shell 2.0 protocols
1448 //
1449 if (mEfiShellProtocol != NULL) {
1450 return (mEfiShellProtocol->FreeFileList(ListHead));
1451 } else {
1452 //
jcarsey94b17fa2009-05-07 18:46:18 +00001453 // Since this is EFI Shell version we need to free our internally made copy
1454 // of the list
1455 //
jcarsey9b3bf082009-06-23 21:15:07 +00001456 for ( Node = GetFirstNode(&(*ListHead)->Link)
1457 ; IsListEmpty(&(*ListHead)->Link) == FALSE
1458 ; Node = GetFirstNode(&(*ListHead)->Link)) {
jcarsey94b17fa2009-05-07 18:46:18 +00001459 RemoveEntryList(Node);
jcarseyd2b45642009-05-11 18:02:16 +00001460 ((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->Handle->Close(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->Handle);
jcarsey94b17fa2009-05-07 18:46:18 +00001461 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->FullName);
1462 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->FileName);
1463 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->Info);
1464 FreePool((EFI_SHELL_FILE_INFO_NO_CONST*)Node);
1465 }
1466 return EFI_SUCCESS;
1467 }
1468}
1469
jcarsey125c2cf2009-11-18 21:36:50 +00001470/**
1471 Find a file by searching the CWD and then the path.
1472
1473 if FileName is NULL then ASSERT.
1474
1475 if the return value is not NULL then the memory must be caller freed.
1476
1477 @param FileName Filename string.
1478
1479 @retval NULL the file was not found
1480 @return !NULL the full path to the file.
1481**/
1482CHAR16 *
1483EFIAPI
1484ShellFindFilePath (
1485 IN CONST CHAR16 *FileName
1486 )
1487{
1488 CONST CHAR16 *Path;
1489 EFI_FILE_HANDLE Handle;
1490 EFI_STATUS Status;
1491 CHAR16 *RetVal;
1492 CHAR16 *TestPath;
1493 CONST CHAR16 *Walker;
jcarsey36a9d672009-11-20 21:13:41 +00001494 UINTN Size;
jcarsey125c2cf2009-11-18 21:36:50 +00001495
1496 RetVal = NULL;
1497
1498 Path = ShellGetEnvironmentVariable(L"cwd");
1499 if (Path != NULL) {
jcarsey36a9d672009-11-20 21:13:41 +00001500 Size = StrSize(Path);
1501 Size += StrSize(FileName);
1502 TestPath = AllocateZeroPool(Size);
jcarsey125c2cf2009-11-18 21:36:50 +00001503 StrCpy(TestPath, Path);
1504 StrCat(TestPath, FileName);
1505 Status = ShellOpenFileByName(TestPath, &Handle, EFI_FILE_MODE_READ, 0);
1506 if (!EFI_ERROR(Status)){
1507 RetVal = StrnCatGrow(&RetVal, NULL, TestPath, 0);
1508 ShellCloseFile(&Handle);
1509 FreePool(TestPath);
1510 return (RetVal);
1511 }
1512 FreePool(TestPath);
1513 }
1514 Path = ShellGetEnvironmentVariable(L"path");
1515 if (Path != NULL) {
jcarsey36a9d672009-11-20 21:13:41 +00001516 Size = StrSize(Path);
1517 Size += StrSize(FileName);
1518 TestPath = AllocateZeroPool(Size);
jcarsey125c2cf2009-11-18 21:36:50 +00001519 Walker = (CHAR16*)Path;
1520 do {
1521 CopyMem(TestPath, Walker, StrSize(Walker));
1522 if (StrStr(TestPath, L";") != NULL) {
1523 *(StrStr(TestPath, L";")) = CHAR_NULL;
1524 }
1525 StrCat(TestPath, FileName);
1526 if (StrStr(Walker, L";") != NULL) {
1527 Walker = StrStr(Walker, L";") + 1;
1528 } else {
1529 Walker = NULL;
1530 }
1531 Status = ShellOpenFileByName(TestPath, &Handle, EFI_FILE_MODE_READ, 0);
1532 if (!EFI_ERROR(Status)){
1533 RetVal = StrnCatGrow(&RetVal, NULL, TestPath, 0);
1534 ShellCloseFile(&Handle);
1535 break;
1536 }
1537 } while (Walker != NULL && Walker[0] != CHAR_NULL);
1538 FreePool(TestPath);
1539 }
1540 return (RetVal);
1541}
1542
jcarsey94b17fa2009-05-07 18:46:18 +00001543typedef struct {
jcarsey9b3bf082009-06-23 21:15:07 +00001544 LIST_ENTRY Link;
jcarsey94b17fa2009-05-07 18:46:18 +00001545 CHAR16 *Name;
1546 ParamType Type;
1547 CHAR16 *Value;
1548 UINTN OriginalPosition;
1549} SHELL_PARAM_PACKAGE;
1550
1551/**
1552 Checks the list of valid arguments and returns TRUE if the item was found. If the
1553 return value is TRUE then the type parameter is set also.
1554
1555 if CheckList is NULL then ASSERT();
1556 if Name is NULL then ASSERT();
1557 if Type is NULL then ASSERT();
1558
1559 @param Type pointer to type of parameter if it was found
1560 @param Name pointer to Name of parameter found
1561 @param CheckList List to check against
1562
1563 @retval TRUE the Parameter was found. Type is valid.
1564 @retval FALSE the Parameter was not found. Type is not valid.
1565**/
1566BOOLEAN
1567EFIAPI
jcarseyd2b45642009-05-11 18:02:16 +00001568InternalIsOnCheckList (
jcarsey94b17fa2009-05-07 18:46:18 +00001569 IN CONST CHAR16 *Name,
1570 IN CONST SHELL_PARAM_ITEM *CheckList,
1571 OUT ParamType *Type
jcarsey2247dde2009-11-09 18:08:58 +00001572 ) {
jcarsey94b17fa2009-05-07 18:46:18 +00001573 SHELL_PARAM_ITEM *TempListItem;
1574
1575 //
1576 // ASSERT that all 3 pointer parameters aren't NULL
1577 //
1578 ASSERT(CheckList != NULL);
1579 ASSERT(Type != NULL);
1580 ASSERT(Name != NULL);
1581
1582 //
jcarseyd2b45642009-05-11 18:02:16 +00001583 // question mark and page break mode are always supported
1584 //
1585 if ((StrCmp(Name, L"-?") == 0) ||
1586 (StrCmp(Name, L"-b") == 0)
1587 ) {
1588 return (TRUE);
1589 }
1590
1591 //
jcarsey94b17fa2009-05-07 18:46:18 +00001592 // Enumerate through the list
1593 //
1594 for (TempListItem = (SHELL_PARAM_ITEM*)CheckList ; TempListItem->Name != NULL ; TempListItem++) {
1595 //
jcarsey9eb53ac2009-07-08 17:26:58 +00001596 // If the Type is TypeStart only check the first characters of the passed in param
1597 // If it matches set the type and return TRUE
jcarsey94b17fa2009-05-07 18:46:18 +00001598 //
jcarsey9eb53ac2009-07-08 17:26:58 +00001599 if (TempListItem->Type == TypeStart && StrnCmp(Name, TempListItem->Name, StrLen(TempListItem->Name)) == 0) {
1600 *Type = TempListItem->Type;
1601 return (TRUE);
1602 } else if (StrCmp(Name, TempListItem->Name) == 0) {
jcarsey94b17fa2009-05-07 18:46:18 +00001603 *Type = TempListItem->Type;
1604 return (TRUE);
1605 }
1606 }
jcarsey2247dde2009-11-09 18:08:58 +00001607
jcarsey94b17fa2009-05-07 18:46:18 +00001608 return (FALSE);
1609}
1610/**
jcarseyd2b45642009-05-11 18:02:16 +00001611 Checks the string for indicators of "flag" status. this is a leading '/', '-', or '+'
jcarsey94b17fa2009-05-07 18:46:18 +00001612
1613 @param Name pointer to Name of parameter found
1614
1615 @retval TRUE the Parameter is a flag.
1616 @retval FALSE the Parameter not a flag
1617**/
1618BOOLEAN
1619EFIAPI
jcarseyd2b45642009-05-11 18:02:16 +00001620InternalIsFlag (
jcarsey2247dde2009-11-09 18:08:58 +00001621 IN CONST CHAR16 *Name,
1622 IN BOOLEAN AlwaysAllowNumbers
jcarsey94b17fa2009-05-07 18:46:18 +00001623 )
1624{
1625 //
1626 // ASSERT that Name isn't NULL
1627 //
1628 ASSERT(Name != NULL);
1629
1630 //
jcarsey2247dde2009-11-09 18:08:58 +00001631 // If we accept numbers then dont return TRUE. (they will be values)
1632 //
1633 if (((Name[0] == L'-' || Name[0] == L'+') && ShellInternalIsHexaDecimalDigitCharacter(Name[1])) && AlwaysAllowNumbers == TRUE) {
1634 return (FALSE);
1635 }
1636
1637 //
jcarsey94b17fa2009-05-07 18:46:18 +00001638 // If the Name has a / or - as the first character return TRUE
1639 //
jcarseyd2b45642009-05-11 18:02:16 +00001640 if ((Name[0] == L'/') ||
1641 (Name[0] == L'-') ||
1642 (Name[0] == L'+')
1643 ) {
jcarsey94b17fa2009-05-07 18:46:18 +00001644 return (TRUE);
1645 }
1646 return (FALSE);
1647}
1648
1649/**
1650 Checks the command line arguments passed against the list of valid ones.
1651
1652 If no initialization is required, then return RETURN_SUCCESS.
1653
1654 @param CheckList pointer to list of parameters to check
1655 @param CheckPackage pointer to pointer to list checked values
1656 @param ProblemParam optional pointer to pointer to unicode string for
jcarseyd2b45642009-05-11 18:02:16 +00001657 the paramater that caused failure. If used then the
1658 caller is responsible for freeing the memory.
jcarsey94b17fa2009-05-07 18:46:18 +00001659 @param AutoPageBreak will automatically set PageBreakEnabled for "b" parameter
1660 @param Argc Count of parameters in Argv
1661 @param Argv pointer to array of parameters
1662
1663 @retval EFI_SUCCESS The operation completed sucessfully.
1664 @retval EFI_OUT_OF_RESOURCES A memory allocation failed
1665 @retval EFI_INVALID_PARAMETER A parameter was invalid
1666 @retval EFI_VOLUME_CORRUPTED the command line was corrupt. an argument was
1667 duplicated. the duplicated command line argument
1668 was returned in ProblemParam if provided.
1669 @retval EFI_NOT_FOUND a argument required a value that was missing.
1670 the invalid command line argument was returned in
1671 ProblemParam if provided.
1672**/
jcarsey2247dde2009-11-09 18:08:58 +00001673STATIC
jcarsey94b17fa2009-05-07 18:46:18 +00001674EFI_STATUS
1675EFIAPI
1676InternalCommandLineParse (
1677 IN CONST SHELL_PARAM_ITEM *CheckList,
1678 OUT LIST_ENTRY **CheckPackage,
1679 OUT CHAR16 **ProblemParam OPTIONAL,
1680 IN BOOLEAN AutoPageBreak,
1681 IN CONST CHAR16 **Argv,
jcarsey2247dde2009-11-09 18:08:58 +00001682 IN UINTN Argc,
1683 IN BOOLEAN AlwaysAllowNumbers
1684 ) {
jcarsey94b17fa2009-05-07 18:46:18 +00001685 UINTN LoopCounter;
jcarsey94b17fa2009-05-07 18:46:18 +00001686 ParamType CurrentItemType;
1687 SHELL_PARAM_PACKAGE *CurrentItemPackage;
jcarsey125c2cf2009-11-18 21:36:50 +00001688 UINTN GetItemValue;
1689 UINTN ValueSize;
jcarsey94b17fa2009-05-07 18:46:18 +00001690
1691 CurrentItemPackage = NULL;
jcarsey2247dde2009-11-09 18:08:58 +00001692 mTotalParameterCount = 0;
jcarsey125c2cf2009-11-18 21:36:50 +00001693 GetItemValue = 0;
1694 ValueSize = 0;
jcarsey94b17fa2009-05-07 18:46:18 +00001695
1696 //
1697 // If there is only 1 item we dont need to do anything
1698 //
1699 if (Argc <= 1) {
1700 *CheckPackage = NULL;
1701 return (EFI_SUCCESS);
1702 }
1703
1704 //
jcarsey2247dde2009-11-09 18:08:58 +00001705 // ASSERTs
1706 //
1707 ASSERT(CheckList != NULL);
1708 ASSERT(Argv != NULL);
1709
1710 //
jcarsey94b17fa2009-05-07 18:46:18 +00001711 // initialize the linked list
1712 //
1713 *CheckPackage = (LIST_ENTRY*)AllocateZeroPool(sizeof(LIST_ENTRY));
1714 InitializeListHead(*CheckPackage);
1715
1716 //
1717 // loop through each of the arguments
1718 //
1719 for (LoopCounter = 0 ; LoopCounter < Argc ; ++LoopCounter) {
1720 if (Argv[LoopCounter] == NULL) {
1721 //
1722 // do nothing for NULL argv
1723 //
jcarseyd2b45642009-05-11 18:02:16 +00001724 } else if (InternalIsOnCheckList(Argv[LoopCounter], CheckList, &CurrentItemType) == TRUE) {
jcarsey94b17fa2009-05-07 18:46:18 +00001725 //
jcarsey2247dde2009-11-09 18:08:58 +00001726 // We might have leftover if last parameter didnt have optional value
1727 //
jcarsey125c2cf2009-11-18 21:36:50 +00001728 if (GetItemValue != 0) {
1729 GetItemValue = 0;
jcarsey2247dde2009-11-09 18:08:58 +00001730 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
1731 }
1732 //
jcarsey94b17fa2009-05-07 18:46:18 +00001733 // this is a flag
1734 //
1735 CurrentItemPackage = AllocatePool(sizeof(SHELL_PARAM_PACKAGE));
1736 ASSERT(CurrentItemPackage != NULL);
1737 CurrentItemPackage->Name = AllocatePool(StrSize(Argv[LoopCounter]));
1738 ASSERT(CurrentItemPackage->Name != NULL);
1739 StrCpy(CurrentItemPackage->Name, Argv[LoopCounter]);
1740 CurrentItemPackage->Type = CurrentItemType;
1741 CurrentItemPackage->OriginalPosition = (UINTN)(-1);
jcarseyb1f95a02009-06-16 00:23:19 +00001742 CurrentItemPackage->Value = NULL;
jcarsey94b17fa2009-05-07 18:46:18 +00001743
1744 //
1745 // Does this flag require a value
1746 //
jcarsey125c2cf2009-11-18 21:36:50 +00001747 switch (CurrentItemPackage->Type) {
jcarsey94b17fa2009-05-07 18:46:18 +00001748 //
jcarsey125c2cf2009-11-18 21:36:50 +00001749 // possibly trigger the next loop(s) to populate the value of this item
1750 //
1751 case TypeValue:
1752 GetItemValue = 1;
1753 ValueSize = 0;
1754 break;
1755 case TypeDoubleValue:
1756 GetItemValue = 2;
1757 ValueSize = 0;
1758 break;
1759 case TypeMaxValue:
1760 GetItemValue = (UINTN)(-1);
1761 ValueSize = 0;
1762 break;
1763 default:
1764 //
1765 // this item has no value expected; we are done
1766 //
1767 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
1768 ASSERT(GetItemValue == 0);
1769 break;
jcarsey94b17fa2009-05-07 18:46:18 +00001770 }
jcarsey125c2cf2009-11-18 21:36:50 +00001771 } else if (GetItemValue != 0 && InternalIsFlag(Argv[LoopCounter], AlwaysAllowNumbers) == FALSE) {
jcarseyb1f95a02009-06-16 00:23:19 +00001772 ASSERT(CurrentItemPackage != NULL);
1773 //
jcarsey125c2cf2009-11-18 21:36:50 +00001774 // get the item VALUE for a previous flag
jcarseyb1f95a02009-06-16 00:23:19 +00001775 //
jcarsey125c2cf2009-11-18 21:36:50 +00001776 CurrentItemPackage->Value = ReallocatePool(ValueSize, ValueSize + StrSize(Argv[LoopCounter]) + sizeof(CHAR16), CurrentItemPackage->Value);
jcarseyb1f95a02009-06-16 00:23:19 +00001777 ASSERT(CurrentItemPackage->Value != NULL);
jcarsey125c2cf2009-11-18 21:36:50 +00001778 if (ValueSize == 0) {
1779 StrCpy(CurrentItemPackage->Value, Argv[LoopCounter]);
1780 } else {
1781 StrCat(CurrentItemPackage->Value, L" ");
1782 StrCat(CurrentItemPackage->Value, Argv[LoopCounter]);
1783 }
1784 ValueSize += StrSize(Argv[LoopCounter]) + sizeof(CHAR16);
1785 GetItemValue--;
1786 if (GetItemValue == 0) {
1787 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
1788 }
jcarsey2247dde2009-11-09 18:08:58 +00001789 } else if (InternalIsFlag(Argv[LoopCounter], AlwaysAllowNumbers) == FALSE) {
jcarseyb1f95a02009-06-16 00:23:19 +00001790 //
1791 // add this one as a non-flag
1792 //
1793 CurrentItemPackage = AllocatePool(sizeof(SHELL_PARAM_PACKAGE));
1794 ASSERT(CurrentItemPackage != NULL);
1795 CurrentItemPackage->Name = NULL;
1796 CurrentItemPackage->Type = TypePosition;
1797 CurrentItemPackage->Value = AllocatePool(StrSize(Argv[LoopCounter]));
1798 ASSERT(CurrentItemPackage->Value != NULL);
1799 StrCpy(CurrentItemPackage->Value, Argv[LoopCounter]);
jcarsey2247dde2009-11-09 18:08:58 +00001800 CurrentItemPackage->OriginalPosition = mTotalParameterCount++;
jcarsey9b3bf082009-06-23 21:15:07 +00001801 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
jcarsey94b17fa2009-05-07 18:46:18 +00001802 } else if (ProblemParam) {
1803 //
1804 // this was a non-recognised flag... error!
1805 //
jcarseyd2b45642009-05-11 18:02:16 +00001806 *ProblemParam = AllocatePool(StrSize(Argv[LoopCounter]));
1807 ASSERT(*ProblemParam != NULL);
1808 StrCpy(*ProblemParam, Argv[LoopCounter]);
jcarsey94b17fa2009-05-07 18:46:18 +00001809 ShellCommandLineFreeVarList(*CheckPackage);
1810 *CheckPackage = NULL;
1811 return (EFI_VOLUME_CORRUPTED);
1812 } else {
1813 ShellCommandLineFreeVarList(*CheckPackage);
1814 *CheckPackage = NULL;
1815 return (EFI_VOLUME_CORRUPTED);
1816 }
1817 }
jcarsey125c2cf2009-11-18 21:36:50 +00001818 if (GetItemValue != 0) {
1819 GetItemValue = 0;
1820 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
1821 }
jcarsey94b17fa2009-05-07 18:46:18 +00001822 //
1823 // support for AutoPageBreak
1824 //
1825 if (AutoPageBreak && ShellCommandLineGetFlag(*CheckPackage, L"-b")) {
1826 ShellSetPageBreakMode(TRUE);
1827 }
1828 return (EFI_SUCCESS);
1829}
1830
1831/**
1832 Checks the command line arguments passed against the list of valid ones.
1833 Optionally removes NULL values first.
1834
1835 If no initialization is required, then return RETURN_SUCCESS.
1836
1837 @param CheckList pointer to list of parameters to check
1838 @param CheckPackage pointer to pointer to list checked values
1839 @param ProblemParam optional pointer to pointer to unicode string for
1840 the paramater that caused failure.
1841 @param AutoPageBreak will automatically set PageBreakEnabled for "b" parameter
1842
1843 @retval EFI_SUCCESS The operation completed sucessfully.
1844 @retval EFI_OUT_OF_RESOURCES A memory allocation failed
1845 @retval EFI_INVALID_PARAMETER A parameter was invalid
1846 @retval EFI_VOLUME_CORRUPTED the command line was corrupt. an argument was
1847 duplicated. the duplicated command line argument
1848 was returned in ProblemParam if provided.
1849 @retval EFI_DEVICE_ERROR the commands contained 2 opposing arguments. one
1850 of the command line arguments was returned in
1851 ProblemParam if provided.
1852 @retval EFI_NOT_FOUND a argument required a value that was missing.
1853 the invalid command line argument was returned in
1854 ProblemParam if provided.
1855**/
1856EFI_STATUS
1857EFIAPI
jcarsey2247dde2009-11-09 18:08:58 +00001858ShellCommandLineParseEx (
jcarsey94b17fa2009-05-07 18:46:18 +00001859 IN CONST SHELL_PARAM_ITEM *CheckList,
1860 OUT LIST_ENTRY **CheckPackage,
1861 OUT CHAR16 **ProblemParam OPTIONAL,
jcarsey2247dde2009-11-09 18:08:58 +00001862 IN BOOLEAN AutoPageBreak,
1863 IN BOOLEAN AlwaysAllowNumbers
1864 ) {
jcarsey94b17fa2009-05-07 18:46:18 +00001865 //
1866 // ASSERT that CheckList and CheckPackage aren't NULL
1867 //
1868 ASSERT(CheckList != NULL);
1869 ASSERT(CheckPackage != NULL);
1870
1871 //
1872 // Check for UEFI Shell 2.0 protocols
1873 //
1874 if (mEfiShellParametersProtocol != NULL) {
1875 return (InternalCommandLineParse(CheckList,
1876 CheckPackage,
1877 ProblemParam,
1878 AutoPageBreak,
jljusten08d7f8e2009-06-15 18:42:13 +00001879 (CONST CHAR16**) mEfiShellParametersProtocol->Argv,
jcarsey2247dde2009-11-09 18:08:58 +00001880 mEfiShellParametersProtocol->Argc,
1881 AlwaysAllowNumbers));
jcarsey94b17fa2009-05-07 18:46:18 +00001882 }
1883
1884 //
1885 // ASSERT That EFI Shell is not required
1886 //
1887 ASSERT (mEfiShellInterface != NULL);
1888 return (InternalCommandLineParse(CheckList,
1889 CheckPackage,
1890 ProblemParam,
1891 AutoPageBreak,
jljusten08d7f8e2009-06-15 18:42:13 +00001892 (CONST CHAR16**) mEfiShellInterface->Argv,
jcarsey2247dde2009-11-09 18:08:58 +00001893 mEfiShellInterface->Argc,
1894 AlwaysAllowNumbers));
jcarsey94b17fa2009-05-07 18:46:18 +00001895}
1896
1897/**
1898 Frees shell variable list that was returned from ShellCommandLineParse.
1899
1900 This function will free all the memory that was used for the CheckPackage
1901 list of postprocessed shell arguments.
1902
1903 this function has no return value.
1904
1905 if CheckPackage is NULL, then return
1906
1907 @param CheckPackage the list to de-allocate
1908 **/
1909VOID
1910EFIAPI
1911ShellCommandLineFreeVarList (
1912 IN LIST_ENTRY *CheckPackage
jcarsey2247dde2009-11-09 18:08:58 +00001913 ) {
jcarsey94b17fa2009-05-07 18:46:18 +00001914 LIST_ENTRY *Node;
1915
1916 //
1917 // check for CheckPackage == NULL
1918 //
1919 if (CheckPackage == NULL) {
1920 return;
1921 }
1922
1923 //
1924 // for each node in the list
1925 //
jcarsey9eb53ac2009-07-08 17:26:58 +00001926 for ( Node = GetFirstNode(CheckPackage)
jcarsey2247dde2009-11-09 18:08:58 +00001927 ; IsListEmpty(CheckPackage) == FALSE
jcarsey9eb53ac2009-07-08 17:26:58 +00001928 ; Node = GetFirstNode(CheckPackage)
1929 ){
jcarsey94b17fa2009-05-07 18:46:18 +00001930 //
1931 // Remove it from the list
1932 //
1933 RemoveEntryList(Node);
1934
1935 //
1936 // if it has a name free the name
1937 //
1938 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {
1939 FreePool(((SHELL_PARAM_PACKAGE*)Node)->Name);
1940 }
1941
1942 //
1943 // if it has a value free the value
1944 //
1945 if (((SHELL_PARAM_PACKAGE*)Node)->Value != NULL) {
1946 FreePool(((SHELL_PARAM_PACKAGE*)Node)->Value);
1947 }
1948
1949 //
1950 // free the node structure
1951 //
1952 FreePool((SHELL_PARAM_PACKAGE*)Node);
1953 }
1954 //
1955 // free the list head node
1956 //
1957 FreePool(CheckPackage);
1958}
1959/**
1960 Checks for presence of a flag parameter
1961
1962 flag arguments are in the form of "-<Key>" or "/<Key>", but do not have a value following the key
1963
1964 if CheckPackage is NULL then return FALSE.
1965 if KeyString is NULL then ASSERT()
1966
1967 @param CheckPackage The package of parsed command line arguments
1968 @param KeyString the Key of the command line argument to check for
1969
1970 @retval TRUE the flag is on the command line
1971 @retval FALSE the flag is not on the command line
1972 **/
1973BOOLEAN
1974EFIAPI
1975ShellCommandLineGetFlag (
1976 IN CONST LIST_ENTRY *CheckPackage,
1977 IN CHAR16 *KeyString
jcarsey2247dde2009-11-09 18:08:58 +00001978 ) {
jcarsey94b17fa2009-05-07 18:46:18 +00001979 LIST_ENTRY *Node;
1980
1981 //
1982 // ASSERT that both CheckPackage and KeyString aren't NULL
1983 //
1984 ASSERT(KeyString != NULL);
1985
1986 //
1987 // return FALSE for no package
1988 //
1989 if (CheckPackage == NULL) {
1990 return (FALSE);
1991 }
1992
1993 //
1994 // enumerate through the list of parametrs
1995 //
jcarsey9eb53ac2009-07-08 17:26:58 +00001996 for ( Node = GetFirstNode(CheckPackage)
1997 ; !IsNull (CheckPackage, Node)
1998 ; Node = GetNextNode(CheckPackage, Node)
1999 ){
jcarsey94b17fa2009-05-07 18:46:18 +00002000 //
2001 // If the Name matches, return TRUE (and there may be NULL name)
2002 //
2003 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {
jcarsey9eb53ac2009-07-08 17:26:58 +00002004 //
2005 // If Type is TypeStart then only compare the begining of the strings
2006 //
2007 if ( ((SHELL_PARAM_PACKAGE*)Node)->Type == TypeStart
2008 && StrnCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name, StrLen(KeyString)) == 0
2009 ){
2010 return (TRUE);
2011 } else if (StrCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {
jcarsey94b17fa2009-05-07 18:46:18 +00002012 return (TRUE);
2013 }
2014 }
2015 }
2016 return (FALSE);
2017}
2018/**
2019 returns value from command line argument
2020
2021 value parameters are in the form of "-<Key> value" or "/<Key> value"
2022
2023 if CheckPackage is NULL, then return NULL;
2024
2025 @param CheckPackage The package of parsed command line arguments
2026 @param KeyString the Key of the command line argument to check for
2027
2028 @retval NULL the flag is not on the command line
2029 @return !=NULL pointer to unicode string of the value
2030 **/
2031CONST CHAR16*
2032EFIAPI
2033ShellCommandLineGetValue (
2034 IN CONST LIST_ENTRY *CheckPackage,
2035 IN CHAR16 *KeyString
jcarsey2247dde2009-11-09 18:08:58 +00002036 ) {
jcarsey94b17fa2009-05-07 18:46:18 +00002037 LIST_ENTRY *Node;
2038
2039 //
2040 // check for CheckPackage == NULL
2041 //
2042 if (CheckPackage == NULL) {
2043 return (NULL);
2044 }
2045
2046 //
2047 // enumerate through the list of parametrs
2048 //
jcarsey9eb53ac2009-07-08 17:26:58 +00002049 for ( Node = GetFirstNode(CheckPackage)
2050 ; !IsNull (CheckPackage, Node)
2051 ; Node = GetNextNode(CheckPackage, Node)
2052 ){
jcarsey94b17fa2009-05-07 18:46:18 +00002053 //
2054 // If the Name matches, return the value (name can be NULL)
2055 //
2056 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {
jcarsey9eb53ac2009-07-08 17:26:58 +00002057 //
2058 // If Type is TypeStart then only compare the begining of the strings
2059 //
2060 if ( ((SHELL_PARAM_PACKAGE*)Node)->Type == TypeStart
2061 && StrnCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name, StrLen(KeyString)) == 0
2062 ){
2063 //
2064 // return the string part after the flag
2065 //
2066 return (((SHELL_PARAM_PACKAGE*)Node)->Name + StrLen(KeyString));
2067 } else if (StrCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {
2068 //
2069 // return the value
2070 //
jcarsey94b17fa2009-05-07 18:46:18 +00002071 return (((SHELL_PARAM_PACKAGE*)Node)->Value);
2072 }
2073 }
2074 }
2075 return (NULL);
2076}
2077/**
2078 returns raw value from command line argument
2079
2080 raw value parameters are in the form of "value" in a specific position in the list
2081
2082 if CheckPackage is NULL, then return NULL;
2083
2084 @param CheckPackage The package of parsed command line arguments
2085 @param Position the position of the value
2086
2087 @retval NULL the flag is not on the command line
2088 @return !=NULL pointer to unicode string of the value
2089 **/
2090CONST CHAR16*
2091EFIAPI
2092ShellCommandLineGetRawValue (
2093 IN CONST LIST_ENTRY *CheckPackage,
2094 IN UINT32 Position
jcarsey2247dde2009-11-09 18:08:58 +00002095 ) {
jcarsey94b17fa2009-05-07 18:46:18 +00002096 LIST_ENTRY *Node;
2097
2098 //
2099 // check for CheckPackage == NULL
2100 //
2101 if (CheckPackage == NULL) {
2102 return (NULL);
2103 }
2104
2105 //
2106 // enumerate through the list of parametrs
2107 //
jcarseyb82bfcc2009-06-29 16:28:23 +00002108 for ( Node = GetFirstNode(CheckPackage)
2109 ; !IsNull (CheckPackage, Node)
2110 ; Node = GetNextNode(CheckPackage, Node)
2111 ){
jcarsey94b17fa2009-05-07 18:46:18 +00002112 //
2113 // If the position matches, return the value
2114 //
2115 if (((SHELL_PARAM_PACKAGE*)Node)->OriginalPosition == Position) {
2116 return (((SHELL_PARAM_PACKAGE*)Node)->Value);
2117 }
2118 }
2119 return (NULL);
jcarseyb1f95a02009-06-16 00:23:19 +00002120}
jcarsey2247dde2009-11-09 18:08:58 +00002121
2122/**
2123 returns the number of command line value parameters that were parsed.
2124
2125 this will not include flags.
2126
2127 @retval (UINTN)-1 No parsing has ocurred
2128 @return other The number of value parameters found
2129**/
2130UINTN
2131EFIAPI
2132ShellCommandLineGetCount(
2133 VOID
jcarsey125c2cf2009-11-18 21:36:50 +00002134 )
2135{
jcarsey2247dde2009-11-09 18:08:58 +00002136 return (mTotalParameterCount);
2137}
2138
jcarsey975136a2009-06-16 19:03:54 +00002139/**
jcarsey36a9d672009-11-20 21:13:41 +00002140 Determins if a parameter is duplicated.
2141
2142 If Param is not NULL then it will point to a callee allocated string buffer
2143 with the parameter value if a duplicate is found.
2144
2145 If CheckPackage is NULL, then ASSERT.
2146
2147 @param[in] CheckPackage The package of parsed command line arguments.
2148 @param[out] Param Upon finding one, a pointer to the duplicated parameter.
2149
2150 @retval EFI_SUCCESS No parameters were duplicated.
2151 @retval EFI_DEVICE_ERROR A duplicate was found.
2152 **/
2153EFI_STATUS
2154EFIAPI
2155ShellCommandLineCheckDuplicate (
2156 IN CONST LIST_ENTRY *CheckPackage,
2157 OUT CHAR16 **Param
2158 )
2159{
2160 LIST_ENTRY *Node1;
2161 LIST_ENTRY *Node2;
2162
2163 ASSERT(CheckPackage != NULL);
2164
2165 for ( Node1 = GetFirstNode(CheckPackage)
2166 ; !IsNull (CheckPackage, Node1)
2167 ; Node1 = GetNextNode(CheckPackage, Node1)
2168 ){
2169 for ( Node2 = GetNextNode(CheckPackage, Node1)
2170 ; !IsNull (CheckPackage, Node2)
2171 ; Node2 = GetNextNode(CheckPackage, Node2)
2172 ){
2173 if (StrCmp(((SHELL_PARAM_PACKAGE*)Node1)->Name, ((SHELL_PARAM_PACKAGE*)Node2)->Name) == 0) {
2174 if (Param != NULL) {
2175 *Param = NULL;
2176 *Param = StrnCatGrow(Param, NULL, ((SHELL_PARAM_PACKAGE*)Node1)->Name, 0);
2177 }
2178 return (EFI_DEVICE_ERROR);
2179 }
2180 }
2181 }
2182 return (EFI_SUCCESS);
2183}
2184
2185/**
jcarsey975136a2009-06-16 19:03:54 +00002186 This is a find and replace function. it will return the NewString as a copy of
2187 SourceString with each instance of FindTarget replaced with ReplaceWith.
2188
2189 If the string would grow bigger than NewSize it will halt and return error.
2190
2191 @param[in] SourceString String with source buffer
jcarseyb82bfcc2009-06-29 16:28:23 +00002192 @param[in,out] NewString String with resultant buffer
jcarsey975136a2009-06-16 19:03:54 +00002193 @param[in] NewSize Size in bytes of NewString
2194 @param[in] FindTarget String to look for
2195 @param[in[ ReplaceWith String to replace FindTarget with
2196
2197 @retval EFI_INVALID_PARAMETER SourceString was NULL
2198 @retval EFI_INVALID_PARAMETER NewString was NULL
2199 @retval EFI_INVALID_PARAMETER FindTarget was NULL
2200 @retval EFI_INVALID_PARAMETER ReplaceWith was NULL
2201 @retval EFI_INVALID_PARAMETER FindTarget had length < 1
2202 @retval EFI_INVALID_PARAMETER SourceString had length < 1
2203 @retval EFI_BUFFER_TOO_SMALL NewSize was less than the minimum size to hold
2204 the new string (truncation occurred)
2205 @retval EFI_SUCCESS the string was sucessfully copied with replacement
2206**/
2207
2208EFI_STATUS
2209EFIAPI
2210CopyReplace(
2211 IN CHAR16 CONST *SourceString,
2212 IN CHAR16 *NewString,
2213 IN UINTN NewSize,
2214 IN CONST CHAR16 *FindTarget,
2215 IN CONST CHAR16 *ReplaceWith
jcarsey2247dde2009-11-09 18:08:58 +00002216 )
2217{
jcarsey01582942009-07-10 19:46:17 +00002218 UINTN Size;
jcarsey975136a2009-06-16 19:03:54 +00002219 if ( (SourceString == NULL)
2220 || (NewString == NULL)
2221 || (FindTarget == NULL)
2222 || (ReplaceWith == NULL)
2223 || (StrLen(FindTarget) < 1)
2224 || (StrLen(SourceString) < 1)
2225 ){
2226 return (EFI_INVALID_PARAMETER);
2227 }
jcarsey2247dde2009-11-09 18:08:58 +00002228 NewString = SetMem16(NewString, NewSize, CHAR_NULL);
2229 while (*SourceString != CHAR_NULL) {
jcarsey975136a2009-06-16 19:03:54 +00002230 if (StrnCmp(SourceString, FindTarget, StrLen(FindTarget)) == 0) {
2231 SourceString += StrLen(FindTarget);
jcarsey01582942009-07-10 19:46:17 +00002232 Size = StrSize(NewString);
2233 if ((Size + (StrLen(ReplaceWith)*sizeof(CHAR16))) > NewSize) {
jcarsey975136a2009-06-16 19:03:54 +00002234 return (EFI_BUFFER_TOO_SMALL);
2235 }
2236 StrCat(NewString, ReplaceWith);
2237 } else {
jcarsey01582942009-07-10 19:46:17 +00002238 Size = StrSize(NewString);
2239 if (Size + sizeof(CHAR16) > NewSize) {
jcarsey975136a2009-06-16 19:03:54 +00002240 return (EFI_BUFFER_TOO_SMALL);
2241 }
2242 StrnCat(NewString, SourceString, 1);
2243 SourceString++;
2244 }
2245 }
2246 return (EFI_SUCCESS);
2247}
jcarseyb1f95a02009-06-16 00:23:19 +00002248
2249/**
jcarseye2f82972009-12-01 05:40:24 +00002250 Internal worker function to output a string.
2251
2252 This function will output a string to the correct StdOut.
2253
2254 @param[in] String The string to print out.
2255
2256 @retval EFI_SUCCESS The operation was sucessful.
2257 @retval !EFI_SUCCESS The operation failed.
2258**/
2259EFI_STATUS
2260EFIAPI
2261InternalPrintTo (
2262 IN CONST CHAR16 *String
2263 )
2264{
2265 UINTN Size;
2266 Size = StrSize(String) - sizeof(CHAR16);
2267 if (mEfiShellParametersProtocol != NULL) {
2268 return (mEfiShellParametersProtocol->StdOut->Write(mEfiShellParametersProtocol->StdOut, &Size, (VOID*)String));
2269 }
2270 if (mEfiShellInterface != NULL) {
2271 return ( mEfiShellInterface->StdOut->Write(mEfiShellInterface->StdOut, &Size, (VOID*)String));
2272 }
2273 ASSERT(FALSE);
2274 return (EFI_UNSUPPORTED);
2275}
2276
2277/**
jcarseyb1f95a02009-06-16 00:23:19 +00002278 Print at a specific location on the screen.
2279
jcarseyf1b87e72009-06-17 00:52:11 +00002280 This function will move the cursor to a given screen location and print the specified string
jcarseyb1f95a02009-06-16 00:23:19 +00002281
2282 If -1 is specified for either the Row or Col the current screen location for BOTH
jcarseyf1b87e72009-06-17 00:52:11 +00002283 will be used.
jcarseyb1f95a02009-06-16 00:23:19 +00002284
2285 if either Row or Col is out of range for the current console, then ASSERT
2286 if Format is NULL, then ASSERT
2287
2288 In addition to the standard %-based flags as supported by UefiLib Print() this supports
2289 the following additional flags:
2290 %N - Set output attribute to normal
2291 %H - Set output attribute to highlight
2292 %E - Set output attribute to error
2293 %B - Set output attribute to blue color
2294 %V - Set output attribute to green color
2295
2296 Note: The background color is controlled by the shell command cls.
2297
2298 @param[in] Row the row to print at
2299 @param[in] Col the column to print at
2300 @param[in] Format the format string
jcarsey2247dde2009-11-09 18:08:58 +00002301 @param[in] Marker the marker for the variable argument list
jcarseyb1f95a02009-06-16 00:23:19 +00002302
2303 @return the number of characters printed to the screen
2304**/
2305
2306UINTN
2307EFIAPI
jcarsey2247dde2009-11-09 18:08:58 +00002308InternalShellPrintWorker(
jcarseyb1f95a02009-06-16 00:23:19 +00002309 IN INT32 Col OPTIONAL,
2310 IN INT32 Row OPTIONAL,
2311 IN CONST CHAR16 *Format,
jcarsey2247dde2009-11-09 18:08:58 +00002312 VA_LIST Marker
2313 )
2314{
jcarseyb1f95a02009-06-16 00:23:19 +00002315 UINTN BufferSize;
jcarsey975136a2009-06-16 19:03:54 +00002316 CHAR16 *PostReplaceFormat;
2317 CHAR16 *PostReplaceFormat2;
jcarseyb1f95a02009-06-16 00:23:19 +00002318 UINTN Return;
jcarseyb1f95a02009-06-16 00:23:19 +00002319 EFI_STATUS Status;
jcarsey975136a2009-06-16 19:03:54 +00002320 UINTN NormalAttribute;
2321 CHAR16 *ResumeLocation;
2322 CHAR16 *FormatWalker;
jcarsey975136a2009-06-16 19:03:54 +00002323
jcarseye2f82972009-12-01 05:40:24 +00002324 BufferSize = PcdGet16 (PcdShellLibMaxPrintBufferSize);
2325 ASSERT(PcdGet16 (PcdShellLibMaxPrintBufferSize) < PcdGet32 (PcdMaximumUnicodeStringLength));
jcarsey975136a2009-06-16 19:03:54 +00002326 PostReplaceFormat = AllocateZeroPool (BufferSize);
2327 ASSERT (PostReplaceFormat != NULL);
2328 PostReplaceFormat2 = AllocateZeroPool (BufferSize);
2329 ASSERT (PostReplaceFormat2 != NULL);
2330
2331 //
2332 // Back and forth each time fixing up 1 of our flags...
2333 //
2334 Status = CopyReplace(Format, PostReplaceFormat, BufferSize, L"%N", L"%%N");
2335 ASSERT_EFI_ERROR(Status);
2336 Status = CopyReplace(PostReplaceFormat, PostReplaceFormat2, BufferSize, L"%E", L"%%E");
2337 ASSERT_EFI_ERROR(Status);
2338 Status = CopyReplace(PostReplaceFormat2, PostReplaceFormat, BufferSize, L"%H", L"%%H");
2339 ASSERT_EFI_ERROR(Status);
2340 Status = CopyReplace(PostReplaceFormat, PostReplaceFormat2, BufferSize, L"%B", L"%%B");
2341 ASSERT_EFI_ERROR(Status);
2342 Status = CopyReplace(PostReplaceFormat2, PostReplaceFormat, BufferSize, L"%V", L"%%V");
2343 ASSERT_EFI_ERROR(Status);
2344
2345 //
2346 // Use the last buffer from replacing to print from...
2347 //
2348 Return = UnicodeVSPrint (PostReplaceFormat2, BufferSize, PostReplaceFormat, Marker);
2349
2350 FreePool(PostReplaceFormat);
jcarseyb1f95a02009-06-16 00:23:19 +00002351
2352 if (Col != -1 && Row != -1) {
jcarseyb1f95a02009-06-16 00:23:19 +00002353 Status = gST->ConOut->SetCursorPosition(gST->ConOut, Col, Row);
2354 ASSERT_EFI_ERROR(Status);
jcarsey975136a2009-06-16 19:03:54 +00002355 }
2356
2357 NormalAttribute = gST->ConOut->Mode->Attribute;
2358 FormatWalker = PostReplaceFormat2;
jcarsey2247dde2009-11-09 18:08:58 +00002359 while (*FormatWalker != CHAR_NULL) {
jcarsey975136a2009-06-16 19:03:54 +00002360 //
2361 // Find the next attribute change request
2362 //
2363 ResumeLocation = StrStr(FormatWalker, L"%");
2364 if (ResumeLocation != NULL) {
jcarsey2247dde2009-11-09 18:08:58 +00002365 *ResumeLocation = CHAR_NULL;
jcarsey975136a2009-06-16 19:03:54 +00002366 }
2367 //
2368 // print the current FormatWalker string
2369 //
jcarseye2f82972009-12-01 05:40:24 +00002370 Status = InternalPrintTo(FormatWalker);
jcarsey975136a2009-06-16 19:03:54 +00002371 ASSERT_EFI_ERROR(Status);
2372 //
2373 // update the attribute
2374 //
2375 if (ResumeLocation != NULL) {
2376 switch (*(ResumeLocation+1)) {
2377 case (L'N'):
2378 gST->ConOut->SetAttribute(gST->ConOut, NormalAttribute);
2379 break;
2380 case (L'E'):
2381 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_YELLOW, ((NormalAttribute&(BIT4|BIT5|BIT6))>>4)));
2382 break;
2383 case (L'H'):
2384 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_WHITE, ((NormalAttribute&(BIT4|BIT5|BIT6))>>4)));
2385 break;
2386 case (L'B'):
2387 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_BLUE, ((NormalAttribute&(BIT4|BIT5|BIT6))>>4)));
2388 break;
2389 case (L'V'):
2390 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_GREEN, ((NormalAttribute&(BIT4|BIT5|BIT6))>>4)));
2391 break;
2392 default:
jcarseye2f82972009-12-01 05:40:24 +00002393 //
2394 // Print a simple '%' symbol
2395 //
2396 Status = InternalPrintTo(L"%");
2397 ASSERT_EFI_ERROR(Status);
2398 ResumeLocation = ResumeLocation - 1;
jcarsey975136a2009-06-16 19:03:54 +00002399 break;
2400 }
2401 } else {
2402 //
2403 // reset to normal now...
2404 //
2405 gST->ConOut->SetAttribute(gST->ConOut, NormalAttribute);
2406 break;
2407 }
2408
2409 //
2410 // update FormatWalker to Resume + 2 (skip the % and the indicator)
2411 //
2412 FormatWalker = ResumeLocation + 2;
2413 }
jcarseyb1f95a02009-06-16 00:23:19 +00002414
jcarsey975136a2009-06-16 19:03:54 +00002415 FreePool(PostReplaceFormat2);
2416
jcarseyb1f95a02009-06-16 00:23:19 +00002417 return (Return);
jcarsey5f7431d2009-07-10 18:06:01 +00002418}
jcarsey2247dde2009-11-09 18:08:58 +00002419
2420/**
2421 Print at a specific location on the screen.
2422
jcarseye2f82972009-12-01 05:40:24 +00002423 This function will move the cursor to a given screen location and print the specified string.
jcarsey2247dde2009-11-09 18:08:58 +00002424
2425 If -1 is specified for either the Row or Col the current screen location for BOTH
2426 will be used.
2427
jcarseye2f82972009-12-01 05:40:24 +00002428 If either Row or Col is out of range for the current console, then ASSERT.
2429 If Format is NULL, then ASSERT.
jcarsey2247dde2009-11-09 18:08:58 +00002430
2431 In addition to the standard %-based flags as supported by UefiLib Print() this supports
2432 the following additional flags:
2433 %N - Set output attribute to normal
2434 %H - Set output attribute to highlight
2435 %E - Set output attribute to error
2436 %B - Set output attribute to blue color
2437 %V - Set output attribute to green color
2438
2439 Note: The background color is controlled by the shell command cls.
2440
2441 @param[in] Row the row to print at
2442 @param[in] Col the column to print at
2443 @param[in] Format the format string
2444
2445 @return the number of characters printed to the screen
2446**/
2447
2448UINTN
2449EFIAPI
2450ShellPrintEx(
2451 IN INT32 Col OPTIONAL,
2452 IN INT32 Row OPTIONAL,
2453 IN CONST CHAR16 *Format,
2454 ...
2455 )
2456{
2457 VA_LIST Marker;
jcarseye2f82972009-12-01 05:40:24 +00002458 EFI_STATUS Status;
jcarsey2247dde2009-11-09 18:08:58 +00002459 VA_START (Marker, Format);
jcarseye2f82972009-12-01 05:40:24 +00002460 Status = InternalShellPrintWorker(Col, Row, Format, Marker);
2461 VA_END(Marker);
2462 return(Status);
jcarsey2247dde2009-11-09 18:08:58 +00002463}
2464
2465/**
2466 Print at a specific location on the screen.
2467
jcarseye2f82972009-12-01 05:40:24 +00002468 This function will move the cursor to a given screen location and print the specified string.
jcarsey2247dde2009-11-09 18:08:58 +00002469
2470 If -1 is specified for either the Row or Col the current screen location for BOTH
jcarseye2f82972009-12-01 05:40:24 +00002471 will be used.
jcarsey2247dde2009-11-09 18:08:58 +00002472
jcarseye2f82972009-12-01 05:40:24 +00002473 If either Row or Col is out of range for the current console, then ASSERT.
2474 If Format is NULL, then ASSERT.
jcarsey2247dde2009-11-09 18:08:58 +00002475
2476 In addition to the standard %-based flags as supported by UefiLib Print() this supports
2477 the following additional flags:
2478 %N - Set output attribute to normal
2479 %H - Set output attribute to highlight
2480 %E - Set output attribute to error
2481 %B - Set output attribute to blue color
2482 %V - Set output attribute to green color
2483
2484 Note: The background color is controlled by the shell command cls.
2485
2486 @param[in] Row the row to print at
2487 @param[in] Col the column to print at
2488 @param[in] HiiFormatStringId the format string Id for getting from Hii
2489 @param[in] HiiFormatHandle the format string Handle for getting from Hii
2490
2491 @return the number of characters printed to the screen
2492**/
2493UINTN
2494EFIAPI
2495ShellPrintHiiEx(
2496 IN INT32 Col OPTIONAL,
2497 IN INT32 Row OPTIONAL,
2498 IN CONST EFI_STRING_ID HiiFormatStringId,
2499 IN CONST EFI_HANDLE HiiFormatHandle,
2500 ...
2501 )
2502{
2503 VA_LIST Marker;
2504 CHAR16 *HiiFormatString;
2505 UINTN RetVal;
2506
2507 VA_START (Marker, HiiFormatHandle);
2508 HiiFormatString = HiiGetString(HiiFormatHandle, HiiFormatStringId, NULL);
2509 ASSERT(HiiFormatString != NULL);
2510
2511 RetVal = InternalShellPrintWorker(Col, Row, HiiFormatString, Marker);
2512
2513 FreePool(HiiFormatString);
jcarseye2f82972009-12-01 05:40:24 +00002514 VA_END(Marker);
jcarsey2247dde2009-11-09 18:08:58 +00002515
2516 return (RetVal);
2517}
2518
2519/**
2520 Function to determine if a given filename represents a file or a directory.
2521
2522 @param[in] DirName Path to directory to test.
2523
2524 @retval EFI_SUCCESS The Path represents a directory
2525 @retval EFI_NOT_FOUND The Path does not represent a directory
2526 @return other The path failed to open
2527**/
2528EFI_STATUS
2529EFIAPI
2530ShellIsDirectory(
2531 IN CONST CHAR16 *DirName
2532 )
2533{
2534 EFI_STATUS Status;
2535 EFI_FILE_HANDLE Handle;
2536
2537 Handle = NULL;
2538
2539 Status = ShellOpenFileByName(DirName, &Handle, EFI_FILE_MODE_READ, 0);
2540 if (EFI_ERROR(Status)) {
2541 return (Status);
2542 }
2543
2544 if (FileHandleIsDirectory(Handle) == EFI_SUCCESS) {
2545 ShellCloseFile(&Handle);
2546 return (EFI_SUCCESS);
2547 }
2548 ShellCloseFile(&Handle);
2549 return (EFI_NOT_FOUND);
2550}
2551
jcarsey125c2cf2009-11-18 21:36:50 +00002552/**
jcarsey36a9d672009-11-20 21:13:41 +00002553 Function to determine if a given filename represents a file.
2554
2555 @param[in] Name Path to file to test.
2556
2557 @retval EFI_SUCCESS The Path represents a file.
2558 @retval EFI_NOT_FOUND The Path does not represent a file.
2559 @retval other The path failed to open.
2560**/
2561EFI_STATUS
2562EFIAPI
2563ShellIsFile(
2564 IN CONST CHAR16 *Name
2565 )
2566{
2567 EFI_STATUS Status;
2568 EFI_FILE_HANDLE Handle;
2569
2570 Handle = NULL;
2571
2572 Status = ShellOpenFileByName(Name, &Handle, EFI_FILE_MODE_READ, 0);
2573 if (EFI_ERROR(Status)) {
2574 return (Status);
2575 }
2576
2577 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {
2578 ShellCloseFile(&Handle);
2579 return (EFI_SUCCESS);
2580 }
2581 ShellCloseFile(&Handle);
2582 return (EFI_NOT_FOUND);
2583}
2584
2585/**
jcarsey125c2cf2009-11-18 21:36:50 +00002586 Function to determine whether a string is decimal or hex representation of a number
2587 and return the number converted from the string.
2588
2589 @param[in] String String representation of a number
2590
2591 @retval all the number
2592**/
2593UINTN
2594EFIAPI
2595ShellStrToUintn(
2596 IN CONST CHAR16 *String
2597 )
2598{
2599 CONST CHAR16 *Walker;
2600 for (Walker = String; Walker != NULL && *Walker != CHAR_NULL && *Walker == L' '; Walker = Walker + 1);
2601 if (StrnCmp(Walker, L"0x", 2) == 0 || StrnCmp(Walker, L"0X", 2) == 0){
2602 return (StrHexToUintn(Walker));
2603 }
2604 return (StrDecimalToUintn(Walker));
2605}
2606
2607/**
2608 Safely append with automatic string resizing given length of Destination and
2609 desired length of copy from Source.
2610
2611 append the first D characters of Source to the end of Destination, where D is
2612 the lesser of Count and the StrLen() of Source. If appending those D characters
2613 will fit within Destination (whose Size is given as CurrentSize) and
2614 still leave room for a null terminator, then those characters are appended,
2615 starting at the original terminating null of Destination, and a new terminating
2616 null is appended.
2617
2618 If appending D characters onto Destination will result in a overflow of the size
2619 given in CurrentSize the string will be grown such that the copy can be performed
2620 and CurrentSize will be updated to the new size.
2621
2622 If Source is NULL, there is nothing to append, just return the current buffer in
2623 Destination.
2624
2625 if Destination is NULL, then ASSERT()
2626 if Destination's current length (including NULL terminator) is already more then
2627 CurrentSize, then ASSERT()
2628
2629 @param[in,out] Destination The String to append onto
2630 @param[in,out] CurrentSize on call the number of bytes in Destination. On
2631 return possibly the new size (still in bytes). if NULL
2632 then allocate whatever is needed.
2633 @param[in] Source The String to append from
2634 @param[in] Count Maximum number of characters to append. if 0 then
2635 all are appended.
2636
2637 @return Destination return the resultant string.
2638**/
2639CHAR16*
2640EFIAPI
2641StrnCatGrow (
2642 IN OUT CHAR16 **Destination,
2643 IN OUT UINTN *CurrentSize,
2644 IN CONST CHAR16 *Source,
2645 IN UINTN Count
2646 )
2647{
2648 UINTN DestinationStartSize;
2649 UINTN NewSize;
2650
2651 //
2652 // ASSERTs
2653 //
2654 ASSERT(Destination != NULL);
2655
2656 //
2657 // If there's nothing to do then just return Destination
2658 //
2659 if (Source == NULL) {
2660 return (*Destination);
2661 }
2662
2663 //
2664 // allow for un-initialized pointers, based on size being 0
2665 //
2666 if (CurrentSize != NULL && *CurrentSize == 0) {
2667 *Destination = NULL;
2668 }
2669
2670 //
2671 // allow for NULL pointers address as Destination
2672 //
2673 if (*Destination != NULL) {
2674 ASSERT(CurrentSize != 0);
2675 DestinationStartSize = StrSize(*Destination);
2676 ASSERT(DestinationStartSize <= *CurrentSize);
2677 } else {
2678 DestinationStartSize = 0;
2679// ASSERT(*CurrentSize == 0);
2680 }
2681
2682 //
2683 // Append all of Source?
2684 //
2685 if (Count == 0) {
2686 Count = StrLen(Source);
2687 }
2688
2689 //
2690 // Test and grow if required
2691 //
2692 if (CurrentSize != NULL) {
2693 NewSize = *CurrentSize;
2694 while (NewSize < (DestinationStartSize + (Count*sizeof(CHAR16)))) {
2695 NewSize += 2 * Count * sizeof(CHAR16);
2696 }
2697 *Destination = ReallocatePool(*CurrentSize, NewSize, *Destination);
2698 *CurrentSize = NewSize;
2699 } else {
2700 *Destination = AllocateZeroPool((Count+1)*sizeof(CHAR16));
2701 }
2702
2703 //
2704 // Now use standard StrnCat on a big enough buffer
2705 //
2706 return StrnCat(*Destination, Source, Count);
2707}