]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - ShellPkg/Library/UefiShellLib/UefiShellLib.c
InOsEmuPkg: Fix minor typo and Xcode build breaks. Seems Xcode is more picky about...
[mirror_edk2.git] / ShellPkg / Library / UefiShellLib / UefiShellLib.c
... / ...
CommitLineData
1/** @file\r
2 Provides interface to shell functionality for shell commands and applications.\r
3\r
4 Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>\r
5 This program and the accompanying materials\r
6 are licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution. The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php\r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include "UefiShellLib.h"\r
16#include <ShellBase.h>\r
17#include <Library/SortLib.h>\r
18\r
19#define FIND_XXXXX_FILE_BUFFER_SIZE (SIZE_OF_EFI_FILE_INFO + MAX_FILE_NAME_LEN)\r
20\r
21//\r
22// globals...\r
23//\r
24SHELL_PARAM_ITEM EmptyParamList[] = {\r
25 {NULL, TypeMax}\r
26 };\r
27SHELL_PARAM_ITEM SfoParamList[] = {\r
28 {L"-sfo", TypeFlag},\r
29 {NULL, TypeMax}\r
30 };\r
31EFI_SHELL_ENVIRONMENT2 *mEfiShellEnvironment2;\r
32EFI_SHELL_INTERFACE *mEfiShellInterface;\r
33EFI_SHELL_PROTOCOL *mEfiShellProtocol;\r
34EFI_SHELL_PARAMETERS_PROTOCOL *mEfiShellParametersProtocol;\r
35EFI_HANDLE mEfiShellEnvironment2Handle;\r
36FILE_HANDLE_FUNCTION_MAP FileFunctionMap;\r
37\r
38/**\r
39 Check if a Unicode character is a hexadecimal character.\r
40\r
41 This internal function checks if a Unicode character is a\r
42 numeric character. The valid hexadecimal characters are\r
43 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.\r
44\r
45 @param Char The character to check against.\r
46\r
47 @retval TRUE If the Char is a hexadecmial character.\r
48 @retval FALSE If the Char is not a hexadecmial character.\r
49\r
50**/\r
51BOOLEAN\r
52EFIAPI\r
53ShellIsHexaDecimalDigitCharacter (\r
54 IN CHAR16 Char\r
55 )\r
56{\r
57 return (BOOLEAN) ((Char >= L'0' && Char <= L'9') || (Char >= L'A' && Char <= L'F') || (Char >= L'a' && Char <= L'f'));\r
58}\r
59\r
60/**\r
61 Check if a Unicode character is a decimal character.\r
62\r
63 This internal function checks if a Unicode character is a\r
64 decimal character. The valid characters are\r
65 L'0' to L'9'.\r
66\r
67\r
68 @param Char The character to check against.\r
69\r
70 @retval TRUE If the Char is a hexadecmial character.\r
71 @retval FALSE If the Char is not a hexadecmial character.\r
72\r
73**/\r
74BOOLEAN\r
75EFIAPI\r
76ShellIsDecimalDigitCharacter (\r
77 IN CHAR16 Char\r
78 )\r
79{\r
80 return (BOOLEAN) (Char >= L'0' && Char <= L'9');\r
81}\r
82\r
83/**\r
84 Helper function to find ShellEnvironment2 for constructor.\r
85\r
86 @param[in] ImageHandle A copy of the calling image's handle.\r
87**/\r
88EFI_STATUS\r
89EFIAPI\r
90ShellFindSE2 (\r
91 IN EFI_HANDLE ImageHandle\r
92 )\r
93{\r
94 EFI_STATUS Status;\r
95 EFI_HANDLE *Buffer;\r
96 UINTN BufferSize;\r
97 UINTN HandleIndex;\r
98\r
99 BufferSize = 0;\r
100 Buffer = NULL;\r
101 Status = gBS->OpenProtocol(ImageHandle,\r
102 &gEfiShellEnvironment2Guid,\r
103 (VOID **)&mEfiShellEnvironment2,\r
104 ImageHandle,\r
105 NULL,\r
106 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
107 );\r
108 //\r
109 // look for the mEfiShellEnvironment2 protocol at a higher level\r
110 //\r
111 if (EFI_ERROR (Status) || !(CompareGuid (&mEfiShellEnvironment2->SESGuid, &gEfiShellEnvironment2ExtGuid))){\r
112 //\r
113 // figure out how big of a buffer we need.\r
114 //\r
115 Status = gBS->LocateHandle (ByProtocol,\r
116 &gEfiShellEnvironment2Guid,\r
117 NULL, // ignored for ByProtocol\r
118 &BufferSize,\r
119 Buffer\r
120 );\r
121 //\r
122 // maybe it's not there???\r
123 //\r
124 if (Status == EFI_BUFFER_TOO_SMALL) {\r
125 Buffer = (EFI_HANDLE*)AllocateZeroPool(BufferSize);\r
126 ASSERT(Buffer != NULL);\r
127 Status = gBS->LocateHandle (ByProtocol,\r
128 &gEfiShellEnvironment2Guid,\r
129 NULL, // ignored for ByProtocol\r
130 &BufferSize,\r
131 Buffer\r
132 );\r
133 }\r
134 if (!EFI_ERROR (Status) && Buffer != NULL) {\r
135 //\r
136 // now parse the list of returned handles\r
137 //\r
138 Status = EFI_NOT_FOUND;\r
139 for (HandleIndex = 0; HandleIndex < (BufferSize/sizeof(Buffer[0])); HandleIndex++) {\r
140 Status = gBS->OpenProtocol(Buffer[HandleIndex],\r
141 &gEfiShellEnvironment2Guid,\r
142 (VOID **)&mEfiShellEnvironment2,\r
143 ImageHandle,\r
144 NULL,\r
145 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
146 );\r
147 if (CompareGuid (&mEfiShellEnvironment2->SESGuid, &gEfiShellEnvironment2ExtGuid)) {\r
148 mEfiShellEnvironment2Handle = Buffer[HandleIndex];\r
149 Status = EFI_SUCCESS;\r
150 break;\r
151 }\r
152 }\r
153 }\r
154 }\r
155 if (Buffer != NULL) {\r
156 FreePool (Buffer);\r
157 }\r
158 return (Status);\r
159}\r
160\r
161/**\r
162 Function to do most of the work of the constructor. Allows for calling \r
163 multiple times without complete re-initialization.\r
164\r
165 @param[in] ImageHandle A copy of the ImageHandle.\r
166 @param[in] SystemTable A pointer to the SystemTable for the application.\r
167\r
168 @retval EFI_SUCCESS The operationw as successful.\r
169**/\r
170EFI_STATUS\r
171EFIAPI\r
172ShellLibConstructorWorker (\r
173 IN EFI_HANDLE ImageHandle,\r
174 IN EFI_SYSTEM_TABLE *SystemTable\r
175 )\r
176{\r
177 EFI_STATUS Status;\r
178\r
179 //\r
180 // UEFI 2.0 shell interfaces (used preferentially)\r
181 //\r
182 Status = gBS->OpenProtocol(\r
183 ImageHandle,\r
184 &gEfiShellProtocolGuid,\r
185 (VOID **)&mEfiShellProtocol,\r
186 ImageHandle,\r
187 NULL,\r
188 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
189 );\r
190 if (EFI_ERROR(Status)) {\r
191 //\r
192 // Search for the shell protocol\r
193 //\r
194 Status = gBS->LocateProtocol(\r
195 &gEfiShellProtocolGuid,\r
196 NULL,\r
197 (VOID **)&mEfiShellProtocol\r
198 );\r
199 if (EFI_ERROR(Status)) {\r
200 mEfiShellProtocol = NULL;\r
201 }\r
202 }\r
203 Status = gBS->OpenProtocol(\r
204 ImageHandle,\r
205 &gEfiShellParametersProtocolGuid,\r
206 (VOID **)&mEfiShellParametersProtocol,\r
207 ImageHandle,\r
208 NULL,\r
209 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
210 );\r
211 if (EFI_ERROR(Status)) {\r
212 mEfiShellParametersProtocol = NULL;\r
213 }\r
214\r
215 if (mEfiShellParametersProtocol == NULL || mEfiShellProtocol == NULL) {\r
216 //\r
217 // Moved to seperate function due to complexity\r
218 //\r
219 Status = ShellFindSE2(ImageHandle);\r
220\r
221 if (EFI_ERROR(Status)) {\r
222 DEBUG((DEBUG_ERROR, "Status: 0x%08x\r\n", Status));\r
223 mEfiShellEnvironment2 = NULL;\r
224 }\r
225 Status = gBS->OpenProtocol(ImageHandle,\r
226 &gEfiShellInterfaceGuid,\r
227 (VOID **)&mEfiShellInterface,\r
228 ImageHandle,\r
229 NULL,\r
230 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
231 );\r
232 if (EFI_ERROR(Status)) {\r
233 mEfiShellInterface = NULL;\r
234 }\r
235 }\r
236\r
237 //\r
238 // only success getting 2 of either the old or new, but no 1/2 and 1/2\r
239 //\r
240 if ((mEfiShellEnvironment2 != NULL && mEfiShellInterface != NULL) ||\r
241 (mEfiShellProtocol != NULL && mEfiShellParametersProtocol != NULL) ) {\r
242 if (mEfiShellProtocol != NULL) {\r
243 FileFunctionMap.GetFileInfo = mEfiShellProtocol->GetFileInfo;\r
244 FileFunctionMap.SetFileInfo = mEfiShellProtocol->SetFileInfo;\r
245 FileFunctionMap.ReadFile = mEfiShellProtocol->ReadFile;\r
246 FileFunctionMap.WriteFile = mEfiShellProtocol->WriteFile;\r
247 FileFunctionMap.CloseFile = mEfiShellProtocol->CloseFile;\r
248 FileFunctionMap.DeleteFile = mEfiShellProtocol->DeleteFile;\r
249 FileFunctionMap.GetFilePosition = mEfiShellProtocol->GetFilePosition;\r
250 FileFunctionMap.SetFilePosition = mEfiShellProtocol->SetFilePosition;\r
251 FileFunctionMap.FlushFile = mEfiShellProtocol->FlushFile;\r
252 FileFunctionMap.GetFileSize = mEfiShellProtocol->GetFileSize;\r
253 } else {\r
254 FileFunctionMap.GetFileInfo = (EFI_SHELL_GET_FILE_INFO)FileHandleGetInfo;\r
255 FileFunctionMap.SetFileInfo = (EFI_SHELL_SET_FILE_INFO)FileHandleSetInfo;\r
256 FileFunctionMap.ReadFile = (EFI_SHELL_READ_FILE)FileHandleRead;\r
257 FileFunctionMap.WriteFile = (EFI_SHELL_WRITE_FILE)FileHandleWrite;\r
258 FileFunctionMap.CloseFile = (EFI_SHELL_CLOSE_FILE)FileHandleClose;\r
259 FileFunctionMap.DeleteFile = (EFI_SHELL_DELETE_FILE)FileHandleDelete;\r
260 FileFunctionMap.GetFilePosition = (EFI_SHELL_GET_FILE_POSITION)FileHandleGetPosition;\r
261 FileFunctionMap.SetFilePosition = (EFI_SHELL_SET_FILE_POSITION)FileHandleSetPosition;\r
262 FileFunctionMap.FlushFile = (EFI_SHELL_FLUSH_FILE)FileHandleFlush;\r
263 FileFunctionMap.GetFileSize = (EFI_SHELL_GET_FILE_SIZE)FileHandleGetSize;\r
264 }\r
265 return (EFI_SUCCESS);\r
266 }\r
267 return (EFI_NOT_FOUND);\r
268}\r
269/**\r
270 Constructor for the Shell library.\r
271\r
272 Initialize the library and determine if the underlying is a UEFI Shell 2.0 or an EFI shell.\r
273\r
274 @param ImageHandle the image handle of the process\r
275 @param SystemTable the EFI System Table pointer\r
276\r
277 @retval EFI_SUCCESS the initialization was complete sucessfully\r
278 @return others an error ocurred during initialization\r
279**/\r
280EFI_STATUS\r
281EFIAPI\r
282ShellLibConstructor (\r
283 IN EFI_HANDLE ImageHandle,\r
284 IN EFI_SYSTEM_TABLE *SystemTable\r
285 )\r
286{\r
287 mEfiShellEnvironment2 = NULL;\r
288 mEfiShellProtocol = NULL;\r
289 mEfiShellParametersProtocol = NULL;\r
290 mEfiShellInterface = NULL;\r
291 mEfiShellEnvironment2Handle = NULL;\r
292\r
293 //\r
294 // verify that auto initialize is not set false\r
295 //\r
296 if (PcdGetBool(PcdShellLibAutoInitialize) == 0) {\r
297 return (EFI_SUCCESS);\r
298 }\r
299\r
300 return (ShellLibConstructorWorker(ImageHandle, SystemTable));\r
301}\r
302\r
303/**\r
304 Destructor for the library. free any resources.\r
305\r
306 @param[in] ImageHandle A copy of the ImageHandle.\r
307 @param[in] SystemTable A pointer to the SystemTable for the application.\r
308\r
309 @retval EFI_SUCCESS The operation was successful.\r
310 @return An error from the CloseProtocol function.\r
311**/\r
312EFI_STATUS\r
313EFIAPI\r
314ShellLibDestructor (\r
315 IN EFI_HANDLE ImageHandle,\r
316 IN EFI_SYSTEM_TABLE *SystemTable\r
317 )\r
318{\r
319 if (mEfiShellEnvironment2 != NULL) {\r
320 gBS->CloseProtocol(mEfiShellEnvironment2Handle==NULL?ImageHandle:mEfiShellEnvironment2Handle,\r
321 &gEfiShellEnvironment2Guid,\r
322 ImageHandle,\r
323 NULL);\r
324 mEfiShellEnvironment2 = NULL;\r
325 }\r
326 if (mEfiShellInterface != NULL) {\r
327 gBS->CloseProtocol(ImageHandle,\r
328 &gEfiShellInterfaceGuid,\r
329 ImageHandle,\r
330 NULL);\r
331 mEfiShellInterface = NULL;\r
332 }\r
333 if (mEfiShellProtocol != NULL) {\r
334 gBS->CloseProtocol(ImageHandle,\r
335 &gEfiShellProtocolGuid,\r
336 ImageHandle,\r
337 NULL);\r
338 mEfiShellProtocol = NULL;\r
339 }\r
340 if (mEfiShellParametersProtocol != NULL) {\r
341 gBS->CloseProtocol(ImageHandle,\r
342 &gEfiShellParametersProtocolGuid,\r
343 ImageHandle,\r
344 NULL);\r
345 mEfiShellParametersProtocol = NULL;\r
346 }\r
347 mEfiShellEnvironment2Handle = NULL;\r
348\r
349 return (EFI_SUCCESS);\r
350}\r
351\r
352/**\r
353 This function causes the shell library to initialize itself. If the shell library\r
354 is already initialized it will de-initialize all the current protocol poitners and\r
355 re-populate them again.\r
356\r
357 When the library is used with PcdShellLibAutoInitialize set to true this function\r
358 will return EFI_SUCCESS and perform no actions.\r
359\r
360 This function is intended for internal access for shell commands only.\r
361\r
362 @retval EFI_SUCCESS the initialization was complete sucessfully\r
363\r
364**/\r
365EFI_STATUS\r
366EFIAPI\r
367ShellInitialize (\r
368 )\r
369{\r
370 //\r
371 // if auto initialize is not false then skip\r
372 //\r
373 if (PcdGetBool(PcdShellLibAutoInitialize) != 0) {\r
374 return (EFI_SUCCESS);\r
375 }\r
376\r
377 //\r
378 // deinit the current stuff\r
379 //\r
380 ASSERT_EFI_ERROR(ShellLibDestructor(gImageHandle, gST));\r
381\r
382 //\r
383 // init the new stuff\r
384 //\r
385 return (ShellLibConstructorWorker(gImageHandle, gST));\r
386}\r
387\r
388/**\r
389 This function will retrieve the information about the file for the handle\r
390 specified and store it in allocated pool memory.\r
391\r
392 This function allocates a buffer to store the file's information. It is the\r
393 caller's responsibility to free the buffer\r
394\r
395 @param FileHandle The file handle of the file for which information is\r
396 being requested.\r
397\r
398 @retval NULL information could not be retrieved.\r
399\r
400 @return the information about the file\r
401**/\r
402EFI_FILE_INFO*\r
403EFIAPI\r
404ShellGetFileInfo (\r
405 IN SHELL_FILE_HANDLE FileHandle\r
406 )\r
407{\r
408 return (FileFunctionMap.GetFileInfo(FileHandle));\r
409}\r
410\r
411/**\r
412 This function sets the information about the file for the opened handle\r
413 specified.\r
414\r
415 @param[in] FileHandle The file handle of the file for which information\r
416 is being set.\r
417\r
418 @param[in] FileInfo The information to set.\r
419\r
420 @retval EFI_SUCCESS The information was set.\r
421 @retval EFI_INVALID_PARAMETER A parameter was out of range or invalid.\r
422 @retval EFI_UNSUPPORTED The FileHandle does not support FileInfo.\r
423 @retval EFI_NO_MEDIA The device has no medium.\r
424 @retval EFI_DEVICE_ERROR The device reported an error.\r
425 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
426 @retval EFI_WRITE_PROTECTED The file or medium is write protected.\r
427 @retval EFI_ACCESS_DENIED The file was opened read only.\r
428 @retval EFI_VOLUME_FULL The volume is full.\r
429**/\r
430EFI_STATUS\r
431EFIAPI\r
432ShellSetFileInfo (\r
433 IN SHELL_FILE_HANDLE FileHandle,\r
434 IN EFI_FILE_INFO *FileInfo\r
435 )\r
436{\r
437 return (FileFunctionMap.SetFileInfo(FileHandle, FileInfo));\r
438}\r
439\r
440 /**\r
441 This function will open a file or directory referenced by DevicePath.\r
442\r
443 This function opens a file with the open mode according to the file path. The\r
444 Attributes is valid only for EFI_FILE_MODE_CREATE.\r
445\r
446 @param FilePath on input the device path to the file. On output\r
447 the remaining device path.\r
448 @param DeviceHandle pointer to the system device handle.\r
449 @param FileHandle pointer to the file handle.\r
450 @param OpenMode the mode to open the file with.\r
451 @param Attributes the file's file attributes.\r
452\r
453 @retval EFI_SUCCESS The information was set.\r
454 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.\r
455 @retval EFI_UNSUPPORTED Could not open the file path.\r
456 @retval EFI_NOT_FOUND The specified file could not be found on the\r
457 device or the file system could not be found on\r
458 the device.\r
459 @retval EFI_NO_MEDIA The device has no medium.\r
460 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the\r
461 medium is no longer supported.\r
462 @retval EFI_DEVICE_ERROR The device reported an error.\r
463 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
464 @retval EFI_WRITE_PROTECTED The file or medium is write protected.\r
465 @retval EFI_ACCESS_DENIED The file was opened read only.\r
466 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the\r
467 file.\r
468 @retval EFI_VOLUME_FULL The volume is full.\r
469**/\r
470EFI_STATUS\r
471EFIAPI\r
472ShellOpenFileByDevicePath(\r
473 IN OUT EFI_DEVICE_PATH_PROTOCOL **FilePath,\r
474 OUT EFI_HANDLE *DeviceHandle,\r
475 OUT SHELL_FILE_HANDLE *FileHandle,\r
476 IN UINT64 OpenMode,\r
477 IN UINT64 Attributes\r
478 )\r
479{\r
480 CHAR16 *FileName;\r
481 EFI_STATUS Status;\r
482 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *EfiSimpleFileSystemProtocol;\r
483 EFI_FILE_PROTOCOL *Handle1;\r
484 EFI_FILE_PROTOCOL *Handle2;\r
485\r
486 //\r
487 // ASERT for FileHandle, FilePath, and DeviceHandle being NULL\r
488 //\r
489 ASSERT(FilePath != NULL);\r
490 ASSERT(FileHandle != NULL);\r
491 ASSERT(DeviceHandle != NULL);\r
492 //\r
493 // which shell interface should we use\r
494 //\r
495 if (mEfiShellProtocol != NULL) {\r
496 //\r
497 // use UEFI Shell 2.0 method.\r
498 //\r
499 FileName = mEfiShellProtocol->GetFilePathFromDevicePath(*FilePath);\r
500 if (FileName == NULL) {\r
501 return (EFI_INVALID_PARAMETER);\r
502 }\r
503 Status = ShellOpenFileByName(FileName, FileHandle, OpenMode, Attributes);\r
504 FreePool(FileName);\r
505 return (Status);\r
506 }\r
507\r
508\r
509 //\r
510 // use old shell method.\r
511 //\r
512 Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid,\r
513 FilePath,\r
514 DeviceHandle);\r
515 if (EFI_ERROR (Status)) {\r
516 return Status;\r
517 }\r
518 Status = gBS->OpenProtocol(*DeviceHandle,\r
519 &gEfiSimpleFileSystemProtocolGuid,\r
520 (VOID**)&EfiSimpleFileSystemProtocol,\r
521 gImageHandle,\r
522 NULL,\r
523 EFI_OPEN_PROTOCOL_GET_PROTOCOL);\r
524 if (EFI_ERROR (Status)) {\r
525 return Status;\r
526 }\r
527 Status = EfiSimpleFileSystemProtocol->OpenVolume(EfiSimpleFileSystemProtocol, &Handle1);\r
528 if (EFI_ERROR (Status)) {\r
529 FileHandle = NULL;\r
530 return Status;\r
531 }\r
532\r
533 //\r
534 // go down directories one node at a time.\r
535 //\r
536 while (!IsDevicePathEnd (*FilePath)) {\r
537 //\r
538 // For file system access each node should be a file path component\r
539 //\r
540 if (DevicePathType (*FilePath) != MEDIA_DEVICE_PATH ||\r
541 DevicePathSubType (*FilePath) != MEDIA_FILEPATH_DP\r
542 ) {\r
543 FileHandle = NULL;\r
544 return (EFI_INVALID_PARAMETER);\r
545 }\r
546 //\r
547 // Open this file path node\r
548 //\r
549 Handle2 = Handle1;\r
550 Handle1 = NULL;\r
551\r
552 //\r
553 // Try to test opening an existing file\r
554 //\r
555 Status = Handle2->Open (\r
556 Handle2,\r
557 &Handle1,\r
558 ((FILEPATH_DEVICE_PATH*)*FilePath)->PathName,\r
559 OpenMode &~EFI_FILE_MODE_CREATE,\r
560 0\r
561 );\r
562\r
563 //\r
564 // see if the error was that it needs to be created\r
565 //\r
566 if ((EFI_ERROR (Status)) && (OpenMode != (OpenMode &~EFI_FILE_MODE_CREATE))) {\r
567 Status = Handle2->Open (\r
568 Handle2,\r
569 &Handle1,\r
570 ((FILEPATH_DEVICE_PATH*)*FilePath)->PathName,\r
571 OpenMode,\r
572 Attributes\r
573 );\r
574 }\r
575 //\r
576 // Close the last node\r
577 //\r
578 Handle2->Close (Handle2);\r
579\r
580 if (EFI_ERROR(Status)) {\r
581 return (Status);\r
582 }\r
583\r
584 //\r
585 // Get the next node\r
586 //\r
587 *FilePath = NextDevicePathNode (*FilePath);\r
588 }\r
589\r
590 //\r
591 // This is a weak spot since if the undefined SHELL_FILE_HANDLE format changes this must change also!\r
592 //\r
593 *FileHandle = (VOID*)Handle1;\r
594 return (EFI_SUCCESS);\r
595}\r
596\r
597/**\r
598 This function will open a file or directory referenced by filename.\r
599\r
600 If return is EFI_SUCCESS, the Filehandle is the opened file's handle;\r
601 otherwise, the Filehandle is NULL. The Attributes is valid only for\r
602 EFI_FILE_MODE_CREATE.\r
603\r
604 if FileNAme is NULL then ASSERT()\r
605\r
606 @param FileName pointer to file name\r
607 @param FileHandle pointer to the file handle.\r
608 @param OpenMode the mode to open the file with.\r
609 @param Attributes the file's file attributes.\r
610\r
611 @retval EFI_SUCCESS The information was set.\r
612 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.\r
613 @retval EFI_UNSUPPORTED Could not open the file path.\r
614 @retval EFI_NOT_FOUND The specified file could not be found on the\r
615 device or the file system could not be found\r
616 on the device.\r
617 @retval EFI_NO_MEDIA The device has no medium.\r
618 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the\r
619 medium is no longer supported.\r
620 @retval EFI_DEVICE_ERROR The device reported an error.\r
621 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
622 @retval EFI_WRITE_PROTECTED The file or medium is write protected.\r
623 @retval EFI_ACCESS_DENIED The file was opened read only.\r
624 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the\r
625 file.\r
626 @retval EFI_VOLUME_FULL The volume is full.\r
627**/\r
628EFI_STATUS\r
629EFIAPI\r
630ShellOpenFileByName(\r
631 IN CONST CHAR16 *FileName,\r
632 OUT SHELL_FILE_HANDLE *FileHandle,\r
633 IN UINT64 OpenMode,\r
634 IN UINT64 Attributes\r
635 )\r
636{\r
637 EFI_HANDLE DeviceHandle;\r
638 EFI_DEVICE_PATH_PROTOCOL *FilePath;\r
639 EFI_STATUS Status;\r
640 EFI_FILE_INFO *FileInfo;\r
641\r
642 //\r
643 // ASSERT if FileName is NULL\r
644 //\r
645 ASSERT(FileName != NULL);\r
646\r
647 if (FileName == NULL) {\r
648 return (EFI_INVALID_PARAMETER);\r
649 }\r
650\r
651 if (mEfiShellProtocol != NULL) {\r
652 if ((OpenMode & EFI_FILE_MODE_CREATE) == EFI_FILE_MODE_CREATE && (Attributes & EFI_FILE_DIRECTORY) == EFI_FILE_DIRECTORY) {\r
653 return ShellCreateDirectory(FileName, FileHandle);\r
654 }\r
655 //\r
656 // Use UEFI Shell 2.0 method\r
657 //\r
658 Status = mEfiShellProtocol->OpenFileByName(FileName,\r
659 FileHandle,\r
660 OpenMode);\r
661 if (StrCmp(FileName, L"NUL") != 0 && !EFI_ERROR(Status) && ((OpenMode & EFI_FILE_MODE_CREATE) != 0)){\r
662 FileInfo = FileFunctionMap.GetFileInfo(*FileHandle);\r
663 ASSERT(FileInfo != NULL);\r
664 FileInfo->Attribute = Attributes;\r
665 Status = FileFunctionMap.SetFileInfo(*FileHandle, FileInfo);\r
666 FreePool(FileInfo);\r
667 }\r
668 return (Status);\r
669 }\r
670 //\r
671 // Using EFI Shell version\r
672 // this means convert name to path and call that function\r
673 // since this will use EFI method again that will open it.\r
674 //\r
675 ASSERT(mEfiShellEnvironment2 != NULL);\r
676 FilePath = mEfiShellEnvironment2->NameToPath ((CHAR16*)FileName);\r
677 if (FilePath != NULL) {\r
678 return (ShellOpenFileByDevicePath(&FilePath,\r
679 &DeviceHandle,\r
680 FileHandle,\r
681 OpenMode,\r
682 Attributes));\r
683 }\r
684 return (EFI_DEVICE_ERROR);\r
685}\r
686/**\r
687 This function create a directory\r
688\r
689 If return is EFI_SUCCESS, the Filehandle is the opened directory's handle;\r
690 otherwise, the Filehandle is NULL. If the directory already existed, this\r
691 function opens the existing directory.\r
692\r
693 @param DirectoryName pointer to directory name\r
694 @param FileHandle pointer to the file handle.\r
695\r
696 @retval EFI_SUCCESS The information was set.\r
697 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.\r
698 @retval EFI_UNSUPPORTED Could not open the file path.\r
699 @retval EFI_NOT_FOUND The specified file could not be found on the\r
700 device or the file system could not be found\r
701 on the device.\r
702 @retval EFI_NO_MEDIA The device has no medium.\r
703 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the\r
704 medium is no longer supported.\r
705 @retval EFI_DEVICE_ERROR The device reported an error.\r
706 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
707 @retval EFI_WRITE_PROTECTED The file or medium is write protected.\r
708 @retval EFI_ACCESS_DENIED The file was opened read only.\r
709 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the\r
710 file.\r
711 @retval EFI_VOLUME_FULL The volume is full.\r
712 @sa ShellOpenFileByName\r
713**/\r
714EFI_STATUS\r
715EFIAPI\r
716ShellCreateDirectory(\r
717 IN CONST CHAR16 *DirectoryName,\r
718 OUT SHELL_FILE_HANDLE *FileHandle\r
719 )\r
720{\r
721 if (mEfiShellProtocol != NULL) {\r
722 //\r
723 // Use UEFI Shell 2.0 method\r
724 //\r
725 return (mEfiShellProtocol->CreateFile(DirectoryName,\r
726 EFI_FILE_DIRECTORY,\r
727 FileHandle\r
728 ));\r
729 } else {\r
730 return (ShellOpenFileByName(DirectoryName,\r
731 FileHandle,\r
732 EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE,\r
733 EFI_FILE_DIRECTORY\r
734 ));\r
735 }\r
736}\r
737\r
738/**\r
739 This function reads information from an opened file.\r
740\r
741 If FileHandle is not a directory, the function reads the requested number of\r
742 bytes from the file at the file's current position and returns them in Buffer.\r
743 If the read goes beyond the end of the file, the read length is truncated to the\r
744 end of the file. The file's current position is increased by the number of bytes\r
745 returned. If FileHandle is a directory, the function reads the directory entry\r
746 at the file's current position and returns the entry in Buffer. If the Buffer\r
747 is not large enough to hold the current directory entry, then\r
748 EFI_BUFFER_TOO_SMALL is returned and the current file position is not updated.\r
749 BufferSize is set to be the size of the buffer needed to read the entry. On\r
750 success, the current position is updated to the next directory entry. If there\r
751 are no more directory entries, the read returns a zero-length buffer.\r
752 EFI_FILE_INFO is the structure returned as the directory entry.\r
753\r
754 @param FileHandle the opened file handle\r
755 @param BufferSize on input the size of buffer in bytes. on return\r
756 the number of bytes written.\r
757 @param Buffer the buffer to put read data into.\r
758\r
759 @retval EFI_SUCCESS Data was read.\r
760 @retval EFI_NO_MEDIA The device has no media.\r
761 @retval EFI_DEVICE_ERROR The device reported an error.\r
762 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
763 @retval EFI_BUFFER_TO_SMALL Buffer is too small. ReadSize contains required\r
764 size.\r
765\r
766**/\r
767EFI_STATUS\r
768EFIAPI\r
769ShellReadFile(\r
770 IN SHELL_FILE_HANDLE FileHandle,\r
771 IN OUT UINTN *BufferSize,\r
772 OUT VOID *Buffer\r
773 )\r
774{\r
775 return (FileFunctionMap.ReadFile(FileHandle, BufferSize, Buffer));\r
776}\r
777\r
778\r
779/**\r
780 Write data to a file.\r
781\r
782 This function writes the specified number of bytes to the file at the current\r
783 file position. The current file position is advanced the actual number of bytes\r
784 written, which is returned in BufferSize. Partial writes only occur when there\r
785 has been a data error during the write attempt (such as "volume space full").\r
786 The file is automatically grown to hold the data if required. Direct writes to\r
787 opened directories are not supported.\r
788\r
789 @param FileHandle The opened file for writing\r
790 @param BufferSize on input the number of bytes in Buffer. On output\r
791 the number of bytes written.\r
792 @param Buffer the buffer containing data to write is stored.\r
793\r
794 @retval EFI_SUCCESS Data was written.\r
795 @retval EFI_UNSUPPORTED Writes to an open directory are not supported.\r
796 @retval EFI_NO_MEDIA The device has no media.\r
797 @retval EFI_DEVICE_ERROR The device reported an error.\r
798 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
799 @retval EFI_WRITE_PROTECTED The device is write-protected.\r
800 @retval EFI_ACCESS_DENIED The file was open for read only.\r
801 @retval EFI_VOLUME_FULL The volume is full.\r
802**/\r
803EFI_STATUS\r
804EFIAPI\r
805ShellWriteFile(\r
806 IN SHELL_FILE_HANDLE FileHandle,\r
807 IN OUT UINTN *BufferSize,\r
808 IN VOID *Buffer\r
809 )\r
810{\r
811 return (FileFunctionMap.WriteFile(FileHandle, BufferSize, Buffer));\r
812}\r
813\r
814/**\r
815 Close an open file handle.\r
816\r
817 This function closes a specified file handle. All "dirty" cached file data is\r
818 flushed to the device, and the file is closed. In all cases the handle is\r
819 closed.\r
820\r
821@param FileHandle the file handle to close.\r
822\r
823@retval EFI_SUCCESS the file handle was closed sucessfully.\r
824**/\r
825EFI_STATUS\r
826EFIAPI\r
827ShellCloseFile (\r
828 IN SHELL_FILE_HANDLE *FileHandle\r
829 )\r
830{\r
831 return (FileFunctionMap.CloseFile(*FileHandle));\r
832}\r
833\r
834/**\r
835 Delete a file and close the handle\r
836\r
837 This function closes and deletes a file. In all cases the file handle is closed.\r
838 If the file cannot be deleted, the warning code EFI_WARN_DELETE_FAILURE is\r
839 returned, but the handle is still closed.\r
840\r
841 @param FileHandle the file handle to delete\r
842\r
843 @retval EFI_SUCCESS the file was closed sucessfully\r
844 @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not\r
845 deleted\r
846 @retval INVALID_PARAMETER One of the parameters has an invalid value.\r
847**/\r
848EFI_STATUS\r
849EFIAPI\r
850ShellDeleteFile (\r
851 IN SHELL_FILE_HANDLE *FileHandle\r
852 )\r
853{\r
854 return (FileFunctionMap.DeleteFile(*FileHandle));\r
855}\r
856\r
857/**\r
858 Set the current position in a file.\r
859\r
860 This function sets the current file position for the handle to the position\r
861 supplied. With the exception of seeking to position 0xFFFFFFFFFFFFFFFF, only\r
862 absolute positioning is supported, and seeking past the end of the file is\r
863 allowed (a subsequent write would grow the file). Seeking to position\r
864 0xFFFFFFFFFFFFFFFF causes the current position to be set to the end of the file.\r
865 If FileHandle is a directory, the only position that may be set is zero. This\r
866 has the effect of starting the read process of the directory entries over.\r
867\r
868 @param FileHandle The file handle on which the position is being set\r
869 @param Position Byte position from begining of file\r
870\r
871 @retval EFI_SUCCESS Operation completed sucessfully.\r
872 @retval EFI_UNSUPPORTED the seek request for non-zero is not valid on\r
873 directories.\r
874 @retval INVALID_PARAMETER One of the parameters has an invalid value.\r
875**/\r
876EFI_STATUS\r
877EFIAPI\r
878ShellSetFilePosition (\r
879 IN SHELL_FILE_HANDLE FileHandle,\r
880 IN UINT64 Position\r
881 )\r
882{\r
883 return (FileFunctionMap.SetFilePosition(FileHandle, Position));\r
884}\r
885\r
886/**\r
887 Gets a file's current position\r
888\r
889 This function retrieves the current file position for the file handle. For\r
890 directories, the current file position has no meaning outside of the file\r
891 system driver and as such the operation is not supported. An error is returned\r
892 if FileHandle is a directory.\r
893\r
894 @param FileHandle The open file handle on which to get the position.\r
895 @param Position Byte position from begining of file.\r
896\r
897 @retval EFI_SUCCESS the operation completed sucessfully.\r
898 @retval INVALID_PARAMETER One of the parameters has an invalid value.\r
899 @retval EFI_UNSUPPORTED the request is not valid on directories.\r
900**/\r
901EFI_STATUS\r
902EFIAPI\r
903ShellGetFilePosition (\r
904 IN SHELL_FILE_HANDLE FileHandle,\r
905 OUT UINT64 *Position\r
906 )\r
907{\r
908 return (FileFunctionMap.GetFilePosition(FileHandle, Position));\r
909}\r
910/**\r
911 Flushes data on a file\r
912\r
913 This function flushes all modified data associated with a file to a device.\r
914\r
915 @param FileHandle The file handle on which to flush data\r
916\r
917 @retval EFI_SUCCESS The data was flushed.\r
918 @retval EFI_NO_MEDIA The device has no media.\r
919 @retval EFI_DEVICE_ERROR The device reported an error.\r
920 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
921 @retval EFI_WRITE_PROTECTED The file or medium is write protected.\r
922 @retval EFI_ACCESS_DENIED The file was opened for read only.\r
923**/\r
924EFI_STATUS\r
925EFIAPI\r
926ShellFlushFile (\r
927 IN SHELL_FILE_HANDLE FileHandle\r
928 )\r
929{\r
930 return (FileFunctionMap.FlushFile(FileHandle));\r
931}\r
932\r
933/**\r
934 Retrieves the first file from a directory\r
935\r
936 This function opens a directory and gets the first file's info in the\r
937 directory. Caller can use ShellFindNextFile() to get other files. When\r
938 complete the caller is responsible for calling FreePool() on Buffer.\r
939\r
940 @param DirHandle The file handle of the directory to search\r
941 @param Buffer Pointer to buffer for file's information\r
942\r
943 @retval EFI_SUCCESS Found the first file.\r
944 @retval EFI_NOT_FOUND Cannot find the directory.\r
945 @retval EFI_NO_MEDIA The device has no media.\r
946 @retval EFI_DEVICE_ERROR The device reported an error.\r
947 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
948 @return Others status of ShellGetFileInfo, ShellSetFilePosition,\r
949 or ShellReadFile\r
950**/\r
951EFI_STATUS\r
952EFIAPI\r
953ShellFindFirstFile (\r
954 IN SHELL_FILE_HANDLE DirHandle,\r
955 OUT EFI_FILE_INFO **Buffer\r
956 )\r
957{\r
958 //\r
959 // pass to file handle lib\r
960 //\r
961 return (FileHandleFindFirstFile(DirHandle, Buffer));\r
962}\r
963/**\r
964 Retrieves the next file in a directory.\r
965\r
966 To use this function, caller must call the ShellFindFirstFile() to get the\r
967 first file, and then use this function get other files. This function can be\r
968 called for several times to get each file's information in the directory. If\r
969 the call of ShellFindNextFile() got the last file in the directory, the next\r
970 call of this function has no file to get. *NoFile will be set to TRUE and the\r
971 Buffer memory will be automatically freed.\r
972\r
973 @param DirHandle the file handle of the directory\r
974 @param Buffer pointer to buffer for file's information\r
975 @param NoFile pointer to boolean when last file is found\r
976\r
977 @retval EFI_SUCCESS Found the next file, or reached last file\r
978 @retval EFI_NO_MEDIA The device has no media.\r
979 @retval EFI_DEVICE_ERROR The device reported an error.\r
980 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
981**/\r
982EFI_STATUS\r
983EFIAPI\r
984ShellFindNextFile(\r
985 IN SHELL_FILE_HANDLE DirHandle,\r
986 OUT EFI_FILE_INFO *Buffer,\r
987 OUT BOOLEAN *NoFile\r
988 )\r
989{\r
990 //\r
991 // pass to file handle lib\r
992 //\r
993 return (FileHandleFindNextFile(DirHandle, Buffer, NoFile));\r
994}\r
995/**\r
996 Retrieve the size of a file.\r
997\r
998 if FileHandle is NULL then ASSERT()\r
999 if Size is NULL then ASSERT()\r
1000\r
1001 This function extracts the file size info from the FileHandle's EFI_FILE_INFO\r
1002 data.\r
1003\r
1004 @param FileHandle file handle from which size is retrieved\r
1005 @param Size pointer to size\r
1006\r
1007 @retval EFI_SUCCESS operation was completed sucessfully\r
1008 @retval EFI_DEVICE_ERROR cannot access the file\r
1009**/\r
1010EFI_STATUS\r
1011EFIAPI\r
1012ShellGetFileSize (\r
1013 IN SHELL_FILE_HANDLE FileHandle,\r
1014 OUT UINT64 *Size\r
1015 )\r
1016{\r
1017 return (FileFunctionMap.GetFileSize(FileHandle, Size));\r
1018}\r
1019/**\r
1020 Retrieves the status of the break execution flag\r
1021\r
1022 this function is useful to check whether the application is being asked to halt by the shell.\r
1023\r
1024 @retval TRUE the execution break is enabled\r
1025 @retval FALSE the execution break is not enabled\r
1026**/\r
1027BOOLEAN\r
1028EFIAPI\r
1029ShellGetExecutionBreakFlag(\r
1030 VOID\r
1031 )\r
1032{\r
1033 //\r
1034 // Check for UEFI Shell 2.0 protocols\r
1035 //\r
1036 if (mEfiShellProtocol != NULL) {\r
1037\r
1038 //\r
1039 // We are using UEFI Shell 2.0; see if the event has been triggered\r
1040 //\r
1041 if (gBS->CheckEvent(mEfiShellProtocol->ExecutionBreak) != EFI_SUCCESS) {\r
1042 return (FALSE);\r
1043 }\r
1044 return (TRUE);\r
1045 }\r
1046\r
1047 //\r
1048 // using EFI Shell; call the function to check\r
1049 //\r
1050 ASSERT(mEfiShellEnvironment2 != NULL);\r
1051 return (mEfiShellEnvironment2->GetExecutionBreak());\r
1052}\r
1053/**\r
1054 return the value of an environment variable\r
1055\r
1056 this function gets the value of the environment variable set by the\r
1057 ShellSetEnvironmentVariable function\r
1058\r
1059 @param EnvKey The key name of the environment variable.\r
1060\r
1061 @retval NULL the named environment variable does not exist.\r
1062 @return != NULL pointer to the value of the environment variable\r
1063**/\r
1064CONST CHAR16*\r
1065EFIAPI\r
1066ShellGetEnvironmentVariable (\r
1067 IN CONST CHAR16 *EnvKey\r
1068 )\r
1069{\r
1070 //\r
1071 // Check for UEFI Shell 2.0 protocols\r
1072 //\r
1073 if (mEfiShellProtocol != NULL) {\r
1074 return (mEfiShellProtocol->GetEnv(EnvKey));\r
1075 }\r
1076\r
1077 //\r
1078 // ASSERT that we must have EFI shell\r
1079 //\r
1080 ASSERT(mEfiShellEnvironment2 != NULL);\r
1081\r
1082 //\r
1083 // using EFI Shell\r
1084 //\r
1085 return (mEfiShellEnvironment2->GetEnv((CHAR16*)EnvKey));\r
1086}\r
1087/**\r
1088 set the value of an environment variable\r
1089\r
1090This function changes the current value of the specified environment variable. If the\r
1091environment variable exists and the Value is an empty string, then the environment\r
1092variable is deleted. If the environment variable exists and the Value is not an empty\r
1093string, then the value of the environment variable is changed. If the environment\r
1094variable does not exist and the Value is an empty string, there is no action. If the\r
1095environment variable does not exist and the Value is a non-empty string, then the\r
1096environment variable is created and assigned the specified value.\r
1097\r
1098 This is not supported pre-UEFI Shell 2.0.\r
1099\r
1100 @param EnvKey The key name of the environment variable.\r
1101 @param EnvVal The Value of the environment variable\r
1102 @param Volatile Indicates whether the variable is non-volatile (FALSE) or volatile (TRUE).\r
1103\r
1104 @retval EFI_SUCCESS the operation was completed sucessfully\r
1105 @retval EFI_UNSUPPORTED This operation is not allowed in pre UEFI 2.0 Shell environments\r
1106**/\r
1107EFI_STATUS\r
1108EFIAPI\r
1109ShellSetEnvironmentVariable (\r
1110 IN CONST CHAR16 *EnvKey,\r
1111 IN CONST CHAR16 *EnvVal,\r
1112 IN BOOLEAN Volatile\r
1113 )\r
1114{\r
1115 //\r
1116 // Check for UEFI Shell 2.0 protocols\r
1117 //\r
1118 if (mEfiShellProtocol != NULL) {\r
1119 return (mEfiShellProtocol->SetEnv(EnvKey, EnvVal, Volatile));\r
1120 }\r
1121\r
1122 //\r
1123 // This feature does not exist under EFI shell\r
1124 //\r
1125 return (EFI_UNSUPPORTED);\r
1126}\r
1127\r
1128/**\r
1129 Cause the shell to parse and execute a command line.\r
1130\r
1131 This function creates a nested instance of the shell and executes the specified\r
1132 command (CommandLine) with the specified environment (Environment). Upon return,\r
1133 the status code returned by the specified command is placed in StatusCode.\r
1134 If Environment is NULL, then the current environment is used and all changes made\r
1135 by the commands executed will be reflected in the current environment. If the\r
1136 Environment is non-NULL, then the changes made will be discarded.\r
1137 The CommandLine is executed from the current working directory on the current\r
1138 device.\r
1139\r
1140 The EnvironmentVariables and Status parameters are ignored in a pre-UEFI Shell 2.0\r
1141 environment. The values pointed to by the parameters will be unchanged by the\r
1142 ShellExecute() function. The Output parameter has no effect in a\r
1143 UEFI Shell 2.0 environment.\r
1144\r
1145 @param[in] ParentHandle The parent image starting the operation.\r
1146 @param[in] CommandLine The pointer to a NULL terminated command line.\r
1147 @param[in] Output True to display debug output. False to hide it.\r
1148 @param[in] EnvironmentVariables Optional pointer to array of environment variables\r
1149 in the form "x=y". If NULL, the current set is used.\r
1150 @param[out] Status The status of the run command line.\r
1151\r
1152 @retval EFI_SUCCESS The operation completed sucessfully. Status\r
1153 contains the status code returned.\r
1154 @retval EFI_INVALID_PARAMETER A parameter contains an invalid value.\r
1155 @retval EFI_OUT_OF_RESOURCES Out of resources.\r
1156 @retval EFI_UNSUPPORTED The operation is not allowed.\r
1157**/\r
1158EFI_STATUS\r
1159EFIAPI\r
1160ShellExecute (\r
1161 IN EFI_HANDLE *ParentHandle,\r
1162 IN CHAR16 *CommandLine OPTIONAL,\r
1163 IN BOOLEAN Output OPTIONAL,\r
1164 IN CHAR16 **EnvironmentVariables OPTIONAL,\r
1165 OUT EFI_STATUS *Status OPTIONAL\r
1166 )\r
1167{\r
1168 //\r
1169 // Check for UEFI Shell 2.0 protocols\r
1170 //\r
1171 if (mEfiShellProtocol != NULL) {\r
1172 //\r
1173 // Call UEFI Shell 2.0 version (not using Output parameter)\r
1174 //\r
1175 return (mEfiShellProtocol->Execute(ParentHandle,\r
1176 CommandLine,\r
1177 EnvironmentVariables,\r
1178 Status));\r
1179 }\r
1180 //\r
1181 // ASSERT that we must have EFI shell\r
1182 //\r
1183 ASSERT(mEfiShellEnvironment2 != NULL);\r
1184 //\r
1185 // Call EFI Shell version (not using EnvironmentVariables or Status parameters)\r
1186 // Due to oddity in the EFI shell we want to dereference the ParentHandle here\r
1187 //\r
1188 return (mEfiShellEnvironment2->Execute(*ParentHandle,\r
1189 CommandLine,\r
1190 Output));\r
1191}\r
1192/**\r
1193 Retreives the current directory path\r
1194\r
1195 If the DeviceName is NULL, it returns the current device's current directory\r
1196 name. If the DeviceName is not NULL, it returns the current directory name\r
1197 on specified drive.\r
1198\r
1199 @param DeviceName the name of the drive to get directory on\r
1200\r
1201 @retval NULL the directory does not exist\r
1202 @return != NULL the directory\r
1203**/\r
1204CONST CHAR16*\r
1205EFIAPI\r
1206ShellGetCurrentDir (\r
1207 IN CHAR16 * CONST DeviceName OPTIONAL\r
1208 )\r
1209{\r
1210 //\r
1211 // Check for UEFI Shell 2.0 protocols\r
1212 //\r
1213 if (mEfiShellProtocol != NULL) {\r
1214 return (mEfiShellProtocol->GetCurDir(DeviceName));\r
1215 }\r
1216 //\r
1217 // ASSERT that we must have EFI shell\r
1218 //\r
1219 ASSERT(mEfiShellEnvironment2 != NULL);\r
1220 return (mEfiShellEnvironment2->CurDir(DeviceName));\r
1221}\r
1222/**\r
1223 sets (enabled or disabled) the page break mode\r
1224\r
1225 when page break mode is enabled the screen will stop scrolling\r
1226 and wait for operator input before scrolling a subsequent screen.\r
1227\r
1228 @param CurrentState TRUE to enable and FALSE to disable\r
1229**/\r
1230VOID\r
1231EFIAPI\r
1232ShellSetPageBreakMode (\r
1233 IN BOOLEAN CurrentState\r
1234 )\r
1235{\r
1236 //\r
1237 // check for enabling\r
1238 //\r
1239 if (CurrentState != 0x00) {\r
1240 //\r
1241 // check for UEFI Shell 2.0\r
1242 //\r
1243 if (mEfiShellProtocol != NULL) {\r
1244 //\r
1245 // Enable with UEFI 2.0 Shell\r
1246 //\r
1247 mEfiShellProtocol->EnablePageBreak();\r
1248 return;\r
1249 } else {\r
1250 //\r
1251 // ASSERT that must have EFI Shell\r
1252 //\r
1253 ASSERT(mEfiShellEnvironment2 != NULL);\r
1254 //\r
1255 // Enable with EFI Shell\r
1256 //\r
1257 mEfiShellEnvironment2->EnablePageBreak (DEFAULT_INIT_ROW, DEFAULT_AUTO_LF);\r
1258 return;\r
1259 }\r
1260 } else {\r
1261 //\r
1262 // check for UEFI Shell 2.0\r
1263 //\r
1264 if (mEfiShellProtocol != NULL) {\r
1265 //\r
1266 // Disable with UEFI 2.0 Shell\r
1267 //\r
1268 mEfiShellProtocol->DisablePageBreak();\r
1269 return;\r
1270 } else {\r
1271 //\r
1272 // ASSERT that must have EFI Shell\r
1273 //\r
1274 ASSERT(mEfiShellEnvironment2 != NULL);\r
1275 //\r
1276 // Disable with EFI Shell\r
1277 //\r
1278 mEfiShellEnvironment2->DisablePageBreak ();\r
1279 return;\r
1280 }\r
1281 }\r
1282}\r
1283\r
1284///\r
1285/// version of EFI_SHELL_FILE_INFO struct, except has no CONST pointers.\r
1286/// This allows for the struct to be populated.\r
1287///\r
1288typedef struct {\r
1289 LIST_ENTRY Link;\r
1290 EFI_STATUS Status;\r
1291 CHAR16 *FullName;\r
1292 CHAR16 *FileName;\r
1293 SHELL_FILE_HANDLE Handle;\r
1294 EFI_FILE_INFO *Info;\r
1295} EFI_SHELL_FILE_INFO_NO_CONST;\r
1296\r
1297/**\r
1298 Converts a EFI shell list of structures to the coresponding UEFI Shell 2.0 type of list.\r
1299\r
1300 if OldStyleFileList is NULL then ASSERT()\r
1301\r
1302 this function will convert a SHELL_FILE_ARG based list into a callee allocated\r
1303 EFI_SHELL_FILE_INFO based list. it is up to the caller to free the memory via\r
1304 the ShellCloseFileMetaArg function.\r
1305\r
1306 @param[in] FileList the EFI shell list type\r
1307 @param[in,out] ListHead the list to add to\r
1308\r
1309 @retval the resultant head of the double linked new format list;\r
1310**/\r
1311LIST_ENTRY*\r
1312EFIAPI\r
1313InternalShellConvertFileListType (\r
1314 IN LIST_ENTRY *FileList,\r
1315 IN OUT LIST_ENTRY *ListHead\r
1316 )\r
1317{\r
1318 SHELL_FILE_ARG *OldInfo;\r
1319 LIST_ENTRY *Link;\r
1320 EFI_SHELL_FILE_INFO_NO_CONST *NewInfo;\r
1321\r
1322 //\r
1323 // ASSERTs\r
1324 //\r
1325 ASSERT(FileList != NULL);\r
1326 ASSERT(ListHead != NULL);\r
1327\r
1328 //\r
1329 // enumerate through each member of the old list and copy\r
1330 //\r
1331 for (Link = FileList->ForwardLink; Link != FileList; Link = Link->ForwardLink) {\r
1332 OldInfo = CR (Link, SHELL_FILE_ARG, Link, SHELL_FILE_ARG_SIGNATURE);\r
1333 ASSERT(OldInfo != NULL);\r
1334\r
1335 //\r
1336 // Skip ones that failed to open...\r
1337 //\r
1338 if (OldInfo->Status != EFI_SUCCESS) {\r
1339 continue;\r
1340 }\r
1341\r
1342 //\r
1343 // make sure the old list was valid\r
1344 //\r
1345 ASSERT(OldInfo->Info != NULL);\r
1346 ASSERT(OldInfo->FullName != NULL);\r
1347 ASSERT(OldInfo->FileName != NULL);\r
1348\r
1349 //\r
1350 // allocate a new EFI_SHELL_FILE_INFO object\r
1351 //\r
1352 NewInfo = AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));\r
1353 ASSERT(NewInfo != NULL);\r
1354 if (NewInfo == NULL) {\r
1355 break;\r
1356 }\r
1357\r
1358 //\r
1359 // copy the simple items\r
1360 //\r
1361 NewInfo->Handle = OldInfo->Handle;\r
1362 NewInfo->Status = OldInfo->Status;\r
1363\r
1364 // old shell checks for 0 not NULL\r
1365 OldInfo->Handle = 0;\r
1366\r
1367 //\r
1368 // allocate new space to copy strings and structure\r
1369 //\r
1370 NewInfo->FullName = AllocateZeroPool(StrSize(OldInfo->FullName));\r
1371 NewInfo->FileName = AllocateZeroPool(StrSize(OldInfo->FileName));\r
1372 NewInfo->Info = AllocateZeroPool((UINTN)OldInfo->Info->Size);\r
1373\r
1374 //\r
1375 // make sure all the memory allocations were sucessful\r
1376 //\r
1377 ASSERT(NewInfo->FullName != NULL);\r
1378 ASSERT(NewInfo->FileName != NULL);\r
1379 ASSERT(NewInfo->Info != NULL);\r
1380\r
1381 //\r
1382 // Copt the strings and structure\r
1383 //\r
1384 StrCpy(NewInfo->FullName, OldInfo->FullName);\r
1385 StrCpy(NewInfo->FileName, OldInfo->FileName);\r
1386 gBS->CopyMem (NewInfo->Info, OldInfo->Info, (UINTN)OldInfo->Info->Size);\r
1387\r
1388 //\r
1389 // add that to the list\r
1390 //\r
1391 InsertTailList(ListHead, &NewInfo->Link);\r
1392 }\r
1393 return (ListHead);\r
1394}\r
1395/**\r
1396 Opens a group of files based on a path.\r
1397\r
1398 This function uses the Arg to open all the matching files. Each matched\r
1399 file has a SHELL_FILE_ARG structure to record the file information. These\r
1400 structures are placed on the list ListHead. Users can get the SHELL_FILE_ARG\r
1401 structures from ListHead to access each file. This function supports wildcards\r
1402 and will process '?' and '*' as such. the list must be freed with a call to\r
1403 ShellCloseFileMetaArg().\r
1404\r
1405 If you are NOT appending to an existing list *ListHead must be NULL. If\r
1406 *ListHead is NULL then it must be callee freed.\r
1407\r
1408 @param Arg pointer to path string\r
1409 @param OpenMode mode to open files with\r
1410 @param ListHead head of linked list of results\r
1411\r
1412 @retval EFI_SUCCESS the operation was sucessful and the list head\r
1413 contains the list of opened files\r
1414 @return != EFI_SUCCESS the operation failed\r
1415\r
1416 @sa InternalShellConvertFileListType\r
1417**/\r
1418EFI_STATUS\r
1419EFIAPI\r
1420ShellOpenFileMetaArg (\r
1421 IN CHAR16 *Arg,\r
1422 IN UINT64 OpenMode,\r
1423 IN OUT EFI_SHELL_FILE_INFO **ListHead\r
1424 )\r
1425{\r
1426 EFI_STATUS Status;\r
1427 LIST_ENTRY mOldStyleFileList;\r
1428\r
1429 //\r
1430 // ASSERT that Arg and ListHead are not NULL\r
1431 //\r
1432 ASSERT(Arg != NULL);\r
1433 ASSERT(ListHead != NULL);\r
1434\r
1435 //\r
1436 // Check for UEFI Shell 2.0 protocols\r
1437 //\r
1438 if (mEfiShellProtocol != NULL) {\r
1439 if (*ListHead == NULL) {\r
1440 *ListHead = (EFI_SHELL_FILE_INFO*)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));\r
1441 if (*ListHead == NULL) {\r
1442 return (EFI_OUT_OF_RESOURCES);\r
1443 }\r
1444 InitializeListHead(&((*ListHead)->Link));\r
1445 }\r
1446 Status = mEfiShellProtocol->OpenFileList(Arg,\r
1447 OpenMode,\r
1448 ListHead);\r
1449 if (EFI_ERROR(Status)) {\r
1450 mEfiShellProtocol->RemoveDupInFileList(ListHead);\r
1451 } else {\r
1452 Status = mEfiShellProtocol->RemoveDupInFileList(ListHead);\r
1453 }\r
1454 if (*ListHead != NULL && IsListEmpty(&(*ListHead)->Link)) {\r
1455 FreePool(*ListHead);\r
1456 *ListHead = NULL;\r
1457 return (EFI_NOT_FOUND);\r
1458 }\r
1459 return (Status);\r
1460 }\r
1461\r
1462 //\r
1463 // ASSERT that we must have EFI shell\r
1464 //\r
1465 ASSERT(mEfiShellEnvironment2 != NULL);\r
1466\r
1467 //\r
1468 // make sure the list head is initialized\r
1469 //\r
1470 InitializeListHead(&mOldStyleFileList);\r
1471\r
1472 //\r
1473 // Get the EFI Shell list of files\r
1474 //\r
1475 Status = mEfiShellEnvironment2->FileMetaArg(Arg, &mOldStyleFileList);\r
1476 if (EFI_ERROR(Status)) {\r
1477 *ListHead = NULL;\r
1478 return (Status);\r
1479 }\r
1480\r
1481 if (*ListHead == NULL) {\r
1482 *ListHead = (EFI_SHELL_FILE_INFO *)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));\r
1483 if (*ListHead == NULL) {\r
1484 return (EFI_OUT_OF_RESOURCES);\r
1485 }\r
1486 InitializeListHead(&((*ListHead)->Link));\r
1487 }\r
1488\r
1489 //\r
1490 // Convert that to equivalent of UEFI Shell 2.0 structure\r
1491 //\r
1492 InternalShellConvertFileListType(&mOldStyleFileList, &(*ListHead)->Link);\r
1493\r
1494 //\r
1495 // Free the EFI Shell version that was converted.\r
1496 //\r
1497 mEfiShellEnvironment2->FreeFileList(&mOldStyleFileList);\r
1498\r
1499 if ((*ListHead)->Link.ForwardLink == (*ListHead)->Link.BackLink && (*ListHead)->Link.BackLink == &((*ListHead)->Link)) {\r
1500 FreePool(*ListHead);\r
1501 *ListHead = NULL;\r
1502 Status = EFI_NOT_FOUND;\r
1503 }\r
1504\r
1505 return (Status);\r
1506}\r
1507/**\r
1508 Free the linked list returned from ShellOpenFileMetaArg.\r
1509\r
1510 if ListHead is NULL then ASSERT().\r
1511\r
1512 @param ListHead the pointer to free.\r
1513\r
1514 @retval EFI_SUCCESS the operation was sucessful.\r
1515**/\r
1516EFI_STATUS\r
1517EFIAPI\r
1518ShellCloseFileMetaArg (\r
1519 IN OUT EFI_SHELL_FILE_INFO **ListHead\r
1520 )\r
1521{\r
1522 LIST_ENTRY *Node;\r
1523\r
1524 //\r
1525 // ASSERT that ListHead is not NULL\r
1526 //\r
1527 ASSERT(ListHead != NULL);\r
1528\r
1529 //\r
1530 // Check for UEFI Shell 2.0 protocols\r
1531 //\r
1532 if (mEfiShellProtocol != NULL) {\r
1533 return (mEfiShellProtocol->FreeFileList(ListHead));\r
1534 } else {\r
1535 //\r
1536 // Since this is EFI Shell version we need to free our internally made copy\r
1537 // of the list\r
1538 //\r
1539 for ( Node = GetFirstNode(&(*ListHead)->Link)\r
1540 ; *ListHead != NULL && !IsListEmpty(&(*ListHead)->Link)\r
1541 ; Node = GetFirstNode(&(*ListHead)->Link)) {\r
1542 RemoveEntryList(Node);\r
1543 ((EFI_FILE_PROTOCOL*)((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->Handle)->Close(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->Handle);\r
1544 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->FullName);\r
1545 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->FileName);\r
1546 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->Info);\r
1547 FreePool((EFI_SHELL_FILE_INFO_NO_CONST*)Node);\r
1548 }\r
1549 return EFI_SUCCESS;\r
1550 }\r
1551}\r
1552\r
1553/**\r
1554 Find a file by searching the CWD and then the path.\r
1555\r
1556 If FileName is NULL then ASSERT.\r
1557\r
1558 If the return value is not NULL then the memory must be caller freed.\r
1559\r
1560 @param FileName Filename string.\r
1561\r
1562 @retval NULL the file was not found\r
1563 @return !NULL the full path to the file.\r
1564**/\r
1565CHAR16 *\r
1566EFIAPI\r
1567ShellFindFilePath (\r
1568 IN CONST CHAR16 *FileName\r
1569 )\r
1570{\r
1571 CONST CHAR16 *Path;\r
1572 SHELL_FILE_HANDLE Handle;\r
1573 EFI_STATUS Status;\r
1574 CHAR16 *RetVal;\r
1575 CHAR16 *TestPath;\r
1576 CONST CHAR16 *Walker;\r
1577 UINTN Size;\r
1578 CHAR16 *TempChar;\r
1579\r
1580 RetVal = NULL;\r
1581\r
1582 //\r
1583 // First make sure its not an absolute path.\r
1584 //\r
1585 Status = ShellOpenFileByName(FileName, &Handle, EFI_FILE_MODE_READ, 0);\r
1586 if (!EFI_ERROR(Status)){\r
1587 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {\r
1588 ASSERT(RetVal == NULL);\r
1589 RetVal = StrnCatGrow(&RetVal, NULL, FileName, 0);\r
1590 ShellCloseFile(&Handle);\r
1591 return (RetVal);\r
1592 } else {\r
1593 ShellCloseFile(&Handle);\r
1594 }\r
1595 }\r
1596\r
1597 Path = ShellGetEnvironmentVariable(L"cwd");\r
1598 if (Path != NULL) {\r
1599 Size = StrSize(Path);\r
1600 Size += StrSize(FileName);\r
1601 TestPath = AllocateZeroPool(Size);\r
1602 ASSERT(TestPath != NULL);\r
1603 if (TestPath == NULL) {\r
1604 return (NULL);\r
1605 }\r
1606 StrCpy(TestPath, Path);\r
1607 StrCat(TestPath, FileName);\r
1608 Status = ShellOpenFileByName(TestPath, &Handle, EFI_FILE_MODE_READ, 0);\r
1609 if (!EFI_ERROR(Status)){\r
1610 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {\r
1611 ASSERT(RetVal == NULL);\r
1612 RetVal = StrnCatGrow(&RetVal, NULL, TestPath, 0);\r
1613 ShellCloseFile(&Handle);\r
1614 FreePool(TestPath);\r
1615 return (RetVal);\r
1616 } else {\r
1617 ShellCloseFile(&Handle);\r
1618 }\r
1619 }\r
1620 FreePool(TestPath);\r
1621 }\r
1622 Path = ShellGetEnvironmentVariable(L"path");\r
1623 if (Path != NULL) {\r
1624 Size = StrSize(Path)+sizeof(CHAR16);\r
1625 Size += StrSize(FileName);\r
1626 TestPath = AllocateZeroPool(Size);\r
1627 if (TestPath == NULL) {\r
1628 return (NULL);\r
1629 }\r
1630 Walker = (CHAR16*)Path;\r
1631 do {\r
1632 CopyMem(TestPath, Walker, StrSize(Walker));\r
1633 if (TestPath != NULL) {\r
1634 TempChar = StrStr(TestPath, L";");\r
1635 if (TempChar != NULL) {\r
1636 *TempChar = CHAR_NULL;\r
1637 }\r
1638 if (TestPath[StrLen(TestPath)-1] != L'\\') {\r
1639 StrCat(TestPath, L"\\");\r
1640 }\r
1641 if (FileName[0] == L'\\') {\r
1642 FileName++;\r
1643 }\r
1644 StrCat(TestPath, FileName);\r
1645 if (StrStr(Walker, L";") != NULL) {\r
1646 Walker = StrStr(Walker, L";") + 1;\r
1647 } else {\r
1648 Walker = NULL;\r
1649 }\r
1650 Status = ShellOpenFileByName(TestPath, &Handle, EFI_FILE_MODE_READ, 0);\r
1651 if (!EFI_ERROR(Status)){\r
1652 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {\r
1653 ASSERT(RetVal == NULL);\r
1654 RetVal = StrnCatGrow(&RetVal, NULL, TestPath, 0);\r
1655 ShellCloseFile(&Handle);\r
1656 break;\r
1657 } else {\r
1658 ShellCloseFile(&Handle);\r
1659 }\r
1660 }\r
1661 }\r
1662 } while (Walker != NULL && Walker[0] != CHAR_NULL);\r
1663 FreePool(TestPath);\r
1664 }\r
1665 return (RetVal);\r
1666}\r
1667\r
1668/**\r
1669 Find a file by searching the CWD and then the path with a variable set of file\r
1670 extensions. If the file is not found it will append each extension in the list\r
1671 in the order provided and return the first one that is successful.\r
1672\r
1673 If FileName is NULL, then ASSERT.\r
1674 If FileExtension is NULL, then behavior is identical to ShellFindFilePath.\r
1675\r
1676 If the return value is not NULL then the memory must be caller freed.\r
1677\r
1678 @param[in] FileName Filename string.\r
1679 @param[in] FileExtension Semi-colon delimeted list of possible extensions.\r
1680\r
1681 @retval NULL The file was not found.\r
1682 @retval !NULL The path to the file.\r
1683**/\r
1684CHAR16 *\r
1685EFIAPI\r
1686ShellFindFilePathEx (\r
1687 IN CONST CHAR16 *FileName,\r
1688 IN CONST CHAR16 *FileExtension\r
1689 )\r
1690{\r
1691 CHAR16 *TestPath;\r
1692 CHAR16 *RetVal;\r
1693 CONST CHAR16 *ExtensionWalker;\r
1694 UINTN Size;\r
1695 CHAR16 *TempChar;\r
1696 CHAR16 *TempChar2;\r
1697\r
1698 ASSERT(FileName != NULL);\r
1699 if (FileExtension == NULL) {\r
1700 return (ShellFindFilePath(FileName));\r
1701 }\r
1702 RetVal = ShellFindFilePath(FileName);\r
1703 if (RetVal != NULL) {\r
1704 return (RetVal);\r
1705 }\r
1706 Size = StrSize(FileName);\r
1707 Size += StrSize(FileExtension);\r
1708 TestPath = AllocateZeroPool(Size);\r
1709 ASSERT(TestPath != NULL);\r
1710 if (TestPath == NULL) {\r
1711 return (NULL);\r
1712 }\r
1713 for (ExtensionWalker = FileExtension, TempChar2 = (CHAR16*)FileExtension; TempChar2 != NULL ; ExtensionWalker = TempChar2 + 1){\r
1714 StrCpy(TestPath, FileName);\r
1715 if (ExtensionWalker != NULL) {\r
1716 StrCat(TestPath, ExtensionWalker);\r
1717 }\r
1718 TempChar = StrStr(TestPath, L";");\r
1719 if (TempChar != NULL) {\r
1720 *TempChar = CHAR_NULL;\r
1721 }\r
1722 RetVal = ShellFindFilePath(TestPath);\r
1723 if (RetVal != NULL) {\r
1724 break;\r
1725 }\r
1726 ASSERT(ExtensionWalker != NULL);\r
1727 TempChar2 = StrStr(ExtensionWalker, L";");\r
1728 }\r
1729 FreePool(TestPath);\r
1730 return (RetVal);\r
1731}\r
1732\r
1733typedef struct {\r
1734 LIST_ENTRY Link;\r
1735 CHAR16 *Name;\r
1736 SHELL_PARAM_TYPE Type;\r
1737 CHAR16 *Value;\r
1738 UINTN OriginalPosition;\r
1739} SHELL_PARAM_PACKAGE;\r
1740\r
1741/**\r
1742 Checks the list of valid arguments and returns TRUE if the item was found. If the\r
1743 return value is TRUE then the type parameter is set also.\r
1744\r
1745 if CheckList is NULL then ASSERT();\r
1746 if Name is NULL then ASSERT();\r
1747 if Type is NULL then ASSERT();\r
1748\r
1749 @param Name pointer to Name of parameter found\r
1750 @param CheckList List to check against\r
1751 @param Type pointer to type of parameter if it was found\r
1752\r
1753 @retval TRUE the Parameter was found. Type is valid.\r
1754 @retval FALSE the Parameter was not found. Type is not valid.\r
1755**/\r
1756BOOLEAN\r
1757EFIAPI\r
1758InternalIsOnCheckList (\r
1759 IN CONST CHAR16 *Name,\r
1760 IN CONST SHELL_PARAM_ITEM *CheckList,\r
1761 OUT SHELL_PARAM_TYPE *Type\r
1762 )\r
1763{\r
1764 SHELL_PARAM_ITEM *TempListItem;\r
1765 CHAR16 *TempString;\r
1766\r
1767 //\r
1768 // ASSERT that all 3 pointer parameters aren't NULL\r
1769 //\r
1770 ASSERT(CheckList != NULL);\r
1771 ASSERT(Type != NULL);\r
1772 ASSERT(Name != NULL);\r
1773\r
1774 //\r
1775 // question mark and page break mode are always supported\r
1776 //\r
1777 if ((StrCmp(Name, L"-?") == 0) ||\r
1778 (StrCmp(Name, L"-b") == 0)\r
1779 ) {\r
1780 *Type = TypeFlag;\r
1781 return (TRUE);\r
1782 }\r
1783\r
1784 //\r
1785 // Enumerate through the list\r
1786 //\r
1787 for (TempListItem = (SHELL_PARAM_ITEM*)CheckList ; TempListItem->Name != NULL ; TempListItem++) {\r
1788 //\r
1789 // If the Type is TypeStart only check the first characters of the passed in param\r
1790 // If it matches set the type and return TRUE\r
1791 //\r
1792 if (TempListItem->Type == TypeStart) { \r
1793 if (StrnCmp(Name, TempListItem->Name, StrLen(TempListItem->Name)) == 0) {\r
1794 *Type = TempListItem->Type;\r
1795 return (TRUE);\r
1796 }\r
1797 TempString = NULL;\r
1798 TempString = StrnCatGrow(&TempString, NULL, Name, StrLen(TempListItem->Name));\r
1799 if (TempString != NULL) {\r
1800 if (StringNoCaseCompare(&TempString, &TempListItem->Name) == 0) {\r
1801 *Type = TempListItem->Type;\r
1802 FreePool(TempString);\r
1803 return (TRUE);\r
1804 }\r
1805 FreePool(TempString);\r
1806 }\r
1807 } else if (StringNoCaseCompare(&Name, &TempListItem->Name) == 0) {\r
1808 *Type = TempListItem->Type;\r
1809 return (TRUE);\r
1810 }\r
1811 }\r
1812\r
1813 return (FALSE);\r
1814}\r
1815/**\r
1816 Checks the string for indicators of "flag" status. this is a leading '/', '-', or '+'\r
1817\r
1818 @param[in] Name pointer to Name of parameter found\r
1819 @param[in] AlwaysAllowNumbers TRUE to allow numbers, FALSE to not.\r
1820\r
1821 @retval TRUE the Parameter is a flag.\r
1822 @retval FALSE the Parameter not a flag.\r
1823**/\r
1824BOOLEAN\r
1825EFIAPI\r
1826InternalIsFlag (\r
1827 IN CONST CHAR16 *Name,\r
1828 IN BOOLEAN AlwaysAllowNumbers\r
1829 )\r
1830{\r
1831 //\r
1832 // ASSERT that Name isn't NULL\r
1833 //\r
1834 ASSERT(Name != NULL);\r
1835\r
1836 //\r
1837 // If we accept numbers then dont return TRUE. (they will be values)\r
1838 //\r
1839 if (((Name[0] == L'-' || Name[0] == L'+') && ShellIsHexaDecimalDigitCharacter(Name[1])) && AlwaysAllowNumbers) {\r
1840 return (FALSE);\r
1841 }\r
1842\r
1843 //\r
1844 // If the Name has a /, +, or - as the first character return TRUE\r
1845 //\r
1846 if ((Name[0] == L'/') ||\r
1847 (Name[0] == L'-') ||\r
1848 (Name[0] == L'+')\r
1849 ) {\r
1850 return (TRUE);\r
1851 }\r
1852 return (FALSE);\r
1853}\r
1854\r
1855/**\r
1856 Checks the command line arguments passed against the list of valid ones.\r
1857\r
1858 If no initialization is required, then return RETURN_SUCCESS.\r
1859\r
1860 @param[in] CheckList pointer to list of parameters to check\r
1861 @param[out] CheckPackage pointer to pointer to list checked values\r
1862 @param[out] ProblemParam optional pointer to pointer to unicode string for\r
1863 the paramater that caused failure. If used then the\r
1864 caller is responsible for freeing the memory.\r
1865 @param[in] AutoPageBreak will automatically set PageBreakEnabled for "b" parameter\r
1866 @param[in] Argv pointer to array of parameters\r
1867 @param[in] Argc Count of parameters in Argv\r
1868 @param[in] AlwaysAllowNumbers TRUE to allow numbers always, FALSE otherwise.\r
1869\r
1870 @retval EFI_SUCCESS The operation completed sucessfully.\r
1871 @retval EFI_OUT_OF_RESOURCES A memory allocation failed\r
1872 @retval EFI_INVALID_PARAMETER A parameter was invalid\r
1873 @retval EFI_VOLUME_CORRUPTED the command line was corrupt. an argument was\r
1874 duplicated. the duplicated command line argument\r
1875 was returned in ProblemParam if provided.\r
1876 @retval EFI_NOT_FOUND a argument required a value that was missing.\r
1877 the invalid command line argument was returned in\r
1878 ProblemParam if provided.\r
1879**/\r
1880EFI_STATUS\r
1881EFIAPI\r
1882InternalCommandLineParse (\r
1883 IN CONST SHELL_PARAM_ITEM *CheckList,\r
1884 OUT LIST_ENTRY **CheckPackage,\r
1885 OUT CHAR16 **ProblemParam OPTIONAL,\r
1886 IN BOOLEAN AutoPageBreak,\r
1887 IN CONST CHAR16 **Argv,\r
1888 IN UINTN Argc,\r
1889 IN BOOLEAN AlwaysAllowNumbers\r
1890 )\r
1891{\r
1892 UINTN LoopCounter;\r
1893 SHELL_PARAM_TYPE CurrentItemType;\r
1894 SHELL_PARAM_PACKAGE *CurrentItemPackage;\r
1895 UINTN GetItemValue;\r
1896 UINTN ValueSize;\r
1897 UINTN Count;\r
1898 CONST CHAR16 *TempPointer;\r
1899\r
1900 CurrentItemPackage = NULL;\r
1901 GetItemValue = 0;\r
1902 ValueSize = 0;\r
1903 Count = 0;\r
1904\r
1905 //\r
1906 // If there is only 1 item we dont need to do anything\r
1907 //\r
1908 if (Argc < 1) {\r
1909 *CheckPackage = NULL;\r
1910 return (EFI_SUCCESS);\r
1911 }\r
1912\r
1913 //\r
1914 // ASSERTs\r
1915 //\r
1916 ASSERT(CheckList != NULL);\r
1917 ASSERT(Argv != NULL);\r
1918\r
1919 //\r
1920 // initialize the linked list\r
1921 //\r
1922 *CheckPackage = (LIST_ENTRY*)AllocateZeroPool(sizeof(LIST_ENTRY));\r
1923 InitializeListHead(*CheckPackage);\r
1924\r
1925 //\r
1926 // loop through each of the arguments\r
1927 //\r
1928 for (LoopCounter = 0 ; LoopCounter < Argc ; ++LoopCounter) {\r
1929 if (Argv[LoopCounter] == NULL) {\r
1930 //\r
1931 // do nothing for NULL argv\r
1932 //\r
1933 } else if (InternalIsOnCheckList(Argv[LoopCounter], CheckList, &CurrentItemType)) {\r
1934 //\r
1935 // We might have leftover if last parameter didnt have optional value\r
1936 //\r
1937 if (GetItemValue != 0) {\r
1938 GetItemValue = 0;\r
1939 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);\r
1940 }\r
1941 //\r
1942 // this is a flag\r
1943 //\r
1944 CurrentItemPackage = AllocateZeroPool(sizeof(SHELL_PARAM_PACKAGE));\r
1945 ASSERT(CurrentItemPackage != NULL);\r
1946 CurrentItemPackage->Name = AllocateZeroPool(StrSize(Argv[LoopCounter]));\r
1947 ASSERT(CurrentItemPackage->Name != NULL);\r
1948 StrCpy(CurrentItemPackage->Name, Argv[LoopCounter]);\r
1949 CurrentItemPackage->Type = CurrentItemType;\r
1950 CurrentItemPackage->OriginalPosition = (UINTN)(-1);\r
1951 CurrentItemPackage->Value = NULL;\r
1952\r
1953 //\r
1954 // Does this flag require a value\r
1955 //\r
1956 switch (CurrentItemPackage->Type) {\r
1957 //\r
1958 // possibly trigger the next loop(s) to populate the value of this item\r
1959 //\r
1960 case TypeValue:\r
1961 GetItemValue = 1;\r
1962 ValueSize = 0;\r
1963 break;\r
1964 case TypeDoubleValue:\r
1965 GetItemValue = 2;\r
1966 ValueSize = 0;\r
1967 break;\r
1968 case TypeMaxValue:\r
1969 GetItemValue = (UINTN)(-1);\r
1970 ValueSize = 0;\r
1971 break;\r
1972 default:\r
1973 //\r
1974 // this item has no value expected; we are done\r
1975 //\r
1976 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);\r
1977 ASSERT(GetItemValue == 0);\r
1978 break;\r
1979 }\r
1980 } else if (GetItemValue != 0 && !InternalIsFlag(Argv[LoopCounter], AlwaysAllowNumbers)) {\r
1981 ASSERT(CurrentItemPackage != NULL);\r
1982 //\r
1983 // get the item VALUE for a previous flag\r
1984 //\r
1985 CurrentItemPackage->Value = ReallocatePool(ValueSize, ValueSize + StrSize(Argv[LoopCounter]) + sizeof(CHAR16), CurrentItemPackage->Value);\r
1986 ASSERT(CurrentItemPackage->Value != NULL);\r
1987 if (ValueSize == 0) {\r
1988 StrCpy(CurrentItemPackage->Value, Argv[LoopCounter]);\r
1989 } else {\r
1990 StrCat(CurrentItemPackage->Value, L" ");\r
1991 StrCat(CurrentItemPackage->Value, Argv[LoopCounter]);\r
1992 }\r
1993 ValueSize += StrSize(Argv[LoopCounter]) + sizeof(CHAR16);\r
1994 GetItemValue--;\r
1995 if (GetItemValue == 0) {\r
1996 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);\r
1997 }\r
1998 } else if (!InternalIsFlag(Argv[LoopCounter], AlwaysAllowNumbers) ){ //|| ProblemParam == NULL) {\r
1999 //\r
2000 // add this one as a non-flag\r
2001 //\r
2002\r
2003 TempPointer = Argv[LoopCounter];\r
2004 if ((*TempPointer == L'^' && *(TempPointer+1) == L'-') \r
2005 || (*TempPointer == L'^' && *(TempPointer+1) == L'/')\r
2006 || (*TempPointer == L'^' && *(TempPointer+1) == L'+')\r
2007 ){\r
2008 TempPointer++;\r
2009 }\r
2010 CurrentItemPackage = AllocateZeroPool(sizeof(SHELL_PARAM_PACKAGE));\r
2011 ASSERT(CurrentItemPackage != NULL);\r
2012 CurrentItemPackage->Name = NULL;\r
2013 CurrentItemPackage->Type = TypePosition;\r
2014 CurrentItemPackage->Value = AllocateZeroPool(StrSize(TempPointer));\r
2015 ASSERT(CurrentItemPackage->Value != NULL);\r
2016 StrCpy(CurrentItemPackage->Value, TempPointer);\r
2017 CurrentItemPackage->OriginalPosition = Count++;\r
2018 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);\r
2019 } else {\r
2020 //\r
2021 // this was a non-recognised flag... error!\r
2022 //\r
2023 if (ProblemParam != NULL) {\r
2024 *ProblemParam = AllocateZeroPool(StrSize(Argv[LoopCounter]));\r
2025 ASSERT(*ProblemParam != NULL);\r
2026 StrCpy(*ProblemParam, Argv[LoopCounter]); \r
2027 }\r
2028 ShellCommandLineFreeVarList(*CheckPackage);\r
2029 *CheckPackage = NULL;\r
2030 return (EFI_VOLUME_CORRUPTED);\r
2031 }\r
2032 }\r
2033 if (GetItemValue != 0) {\r
2034 GetItemValue = 0;\r
2035 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);\r
2036 }\r
2037 //\r
2038 // support for AutoPageBreak\r
2039 //\r
2040 if (AutoPageBreak && ShellCommandLineGetFlag(*CheckPackage, L"-b")) {\r
2041 ShellSetPageBreakMode(TRUE);\r
2042 }\r
2043 return (EFI_SUCCESS);\r
2044}\r
2045\r
2046/**\r
2047 Checks the command line arguments passed against the list of valid ones.\r
2048 Optionally removes NULL values first.\r
2049\r
2050 If no initialization is required, then return RETURN_SUCCESS.\r
2051\r
2052 @param[in] CheckList The pointer to list of parameters to check.\r
2053 @param[out] CheckPackage The package of checked values.\r
2054 @param[out] ProblemParam Optional pointer to pointer to unicode string for\r
2055 the paramater that caused failure.\r
2056 @param[in] AutoPageBreak Will automatically set PageBreakEnabled.\r
2057 @param[in] AlwaysAllowNumbers Will never fail for number based flags.\r
2058\r
2059 @retval EFI_SUCCESS The operation completed sucessfully.\r
2060 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.\r
2061 @retval EFI_INVALID_PARAMETER A parameter was invalid.\r
2062 @retval EFI_VOLUME_CORRUPTED The command line was corrupt.\r
2063 @retval EFI_DEVICE_ERROR The commands contained 2 opposing arguments. One\r
2064 of the command line arguments was returned in\r
2065 ProblemParam if provided.\r
2066 @retval EFI_NOT_FOUND A argument required a value that was missing.\r
2067 The invalid command line argument was returned in\r
2068 ProblemParam if provided.\r
2069**/\r
2070EFI_STATUS\r
2071EFIAPI\r
2072ShellCommandLineParseEx (\r
2073 IN CONST SHELL_PARAM_ITEM *CheckList,\r
2074 OUT LIST_ENTRY **CheckPackage,\r
2075 OUT CHAR16 **ProblemParam OPTIONAL,\r
2076 IN BOOLEAN AutoPageBreak,\r
2077 IN BOOLEAN AlwaysAllowNumbers\r
2078 )\r
2079{\r
2080 //\r
2081 // ASSERT that CheckList and CheckPackage aren't NULL\r
2082 //\r
2083 ASSERT(CheckList != NULL);\r
2084 ASSERT(CheckPackage != NULL);\r
2085\r
2086 //\r
2087 // Check for UEFI Shell 2.0 protocols\r
2088 //\r
2089 if (mEfiShellParametersProtocol != NULL) {\r
2090 return (InternalCommandLineParse(CheckList,\r
2091 CheckPackage,\r
2092 ProblemParam,\r
2093 AutoPageBreak,\r
2094 (CONST CHAR16**) mEfiShellParametersProtocol->Argv,\r
2095 mEfiShellParametersProtocol->Argc,\r
2096 AlwaysAllowNumbers));\r
2097 }\r
2098\r
2099 //\r
2100 // ASSERT That EFI Shell is not required\r
2101 //\r
2102 ASSERT (mEfiShellInterface != NULL);\r
2103 return (InternalCommandLineParse(CheckList,\r
2104 CheckPackage,\r
2105 ProblemParam,\r
2106 AutoPageBreak,\r
2107 (CONST CHAR16**) mEfiShellInterface->Argv,\r
2108 mEfiShellInterface->Argc,\r
2109 AlwaysAllowNumbers));\r
2110}\r
2111\r
2112/**\r
2113 Frees shell variable list that was returned from ShellCommandLineParse.\r
2114\r
2115 This function will free all the memory that was used for the CheckPackage\r
2116 list of postprocessed shell arguments.\r
2117\r
2118 this function has no return value.\r
2119\r
2120 if CheckPackage is NULL, then return\r
2121\r
2122 @param CheckPackage the list to de-allocate\r
2123 **/\r
2124VOID\r
2125EFIAPI\r
2126ShellCommandLineFreeVarList (\r
2127 IN LIST_ENTRY *CheckPackage\r
2128 )\r
2129{\r
2130 LIST_ENTRY *Node;\r
2131\r
2132 //\r
2133 // check for CheckPackage == NULL\r
2134 //\r
2135 if (CheckPackage == NULL) {\r
2136 return;\r
2137 }\r
2138\r
2139 //\r
2140 // for each node in the list\r
2141 //\r
2142 for ( Node = GetFirstNode(CheckPackage)\r
2143 ; !IsListEmpty(CheckPackage)\r
2144 ; Node = GetFirstNode(CheckPackage)\r
2145 ){\r
2146 //\r
2147 // Remove it from the list\r
2148 //\r
2149 RemoveEntryList(Node);\r
2150\r
2151 //\r
2152 // if it has a name free the name\r
2153 //\r
2154 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {\r
2155 FreePool(((SHELL_PARAM_PACKAGE*)Node)->Name);\r
2156 }\r
2157\r
2158 //\r
2159 // if it has a value free the value\r
2160 //\r
2161 if (((SHELL_PARAM_PACKAGE*)Node)->Value != NULL) {\r
2162 FreePool(((SHELL_PARAM_PACKAGE*)Node)->Value);\r
2163 }\r
2164\r
2165 //\r
2166 // free the node structure\r
2167 //\r
2168 FreePool((SHELL_PARAM_PACKAGE*)Node);\r
2169 }\r
2170 //\r
2171 // free the list head node\r
2172 //\r
2173 FreePool(CheckPackage);\r
2174}\r
2175/**\r
2176 Checks for presence of a flag parameter\r
2177\r
2178 flag arguments are in the form of "-<Key>" or "/<Key>", but do not have a value following the key\r
2179\r
2180 if CheckPackage is NULL then return FALSE.\r
2181 if KeyString is NULL then ASSERT()\r
2182\r
2183 @param CheckPackage The package of parsed command line arguments\r
2184 @param KeyString the Key of the command line argument to check for\r
2185\r
2186 @retval TRUE the flag is on the command line\r
2187 @retval FALSE the flag is not on the command line\r
2188 **/\r
2189BOOLEAN\r
2190EFIAPI\r
2191ShellCommandLineGetFlag (\r
2192 IN CONST LIST_ENTRY * CONST CheckPackage,\r
2193 IN CONST CHAR16 * CONST KeyString\r
2194 )\r
2195{\r
2196 LIST_ENTRY *Node;\r
2197 CHAR16 *TempString;\r
2198\r
2199 //\r
2200 // ASSERT that both CheckPackage and KeyString aren't NULL\r
2201 //\r
2202 ASSERT(KeyString != NULL);\r
2203\r
2204 //\r
2205 // return FALSE for no package\r
2206 //\r
2207 if (CheckPackage == NULL) {\r
2208 return (FALSE);\r
2209 }\r
2210\r
2211 //\r
2212 // enumerate through the list of parametrs\r
2213 //\r
2214 for ( Node = GetFirstNode(CheckPackage)\r
2215 ; !IsNull (CheckPackage, Node)\r
2216 ; Node = GetNextNode(CheckPackage, Node)\r
2217 ){\r
2218 //\r
2219 // If the Name matches, return TRUE (and there may be NULL name)\r
2220 //\r
2221 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {\r
2222 //\r
2223 // If Type is TypeStart then only compare the begining of the strings\r
2224 //\r
2225 if (((SHELL_PARAM_PACKAGE*)Node)->Type == TypeStart) {\r
2226 if (StrnCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name, StrLen(KeyString)) == 0) {\r
2227 return (TRUE);\r
2228 }\r
2229 TempString = NULL;\r
2230 TempString = StrnCatGrow(&TempString, NULL, KeyString, StrLen(((SHELL_PARAM_PACKAGE*)Node)->Name));\r
2231 if (TempString != NULL) {\r
2232 if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {\r
2233 FreePool(TempString);\r
2234 return (TRUE);\r
2235 }\r
2236 FreePool(TempString);\r
2237 }\r
2238 } else if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {\r
2239 return (TRUE);\r
2240 }\r
2241 }\r
2242 }\r
2243 return (FALSE);\r
2244}\r
2245/**\r
2246 Returns value from command line argument.\r
2247\r
2248 Value parameters are in the form of "-<Key> value" or "/<Key> value".\r
2249\r
2250 If CheckPackage is NULL, then return NULL.\r
2251\r
2252 @param[in] CheckPackage The package of parsed command line arguments.\r
2253 @param[in] KeyString The Key of the command line argument to check for.\r
2254\r
2255 @retval NULL The flag is not on the command line.\r
2256 @retval !=NULL The pointer to unicode string of the value.\r
2257**/\r
2258CONST CHAR16*\r
2259EFIAPI\r
2260ShellCommandLineGetValue (\r
2261 IN CONST LIST_ENTRY *CheckPackage,\r
2262 IN CHAR16 *KeyString\r
2263 )\r
2264{\r
2265 LIST_ENTRY *Node;\r
2266 CHAR16 *TempString;\r
2267\r
2268 //\r
2269 // check for CheckPackage == NULL\r
2270 //\r
2271 if (CheckPackage == NULL) {\r
2272 return (NULL);\r
2273 }\r
2274\r
2275 //\r
2276 // enumerate through the list of parametrs\r
2277 //\r
2278 for ( Node = GetFirstNode(CheckPackage)\r
2279 ; !IsNull (CheckPackage, Node)\r
2280 ; Node = GetNextNode(CheckPackage, Node)\r
2281 ){\r
2282 //\r
2283 // If the Name matches, return TRUE (and there may be NULL name)\r
2284 //\r
2285 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {\r
2286 //\r
2287 // If Type is TypeStart then only compare the begining of the strings\r
2288 //\r
2289 if (((SHELL_PARAM_PACKAGE*)Node)->Type == TypeStart) {\r
2290 if (StrnCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name, StrLen(KeyString)) == 0) {\r
2291 return (((SHELL_PARAM_PACKAGE*)Node)->Name + StrLen(KeyString));\r
2292 }\r
2293 TempString = NULL;\r
2294 TempString = StrnCatGrow(&TempString, NULL, KeyString, StrLen(((SHELL_PARAM_PACKAGE*)Node)->Name));\r
2295 if (TempString != NULL) {\r
2296 if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {\r
2297 FreePool(TempString);\r
2298 return (((SHELL_PARAM_PACKAGE*)Node)->Name + StrLen(KeyString));\r
2299 }\r
2300 FreePool(TempString);\r
2301 }\r
2302 } else if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {\r
2303 return (((SHELL_PARAM_PACKAGE*)Node)->Value);\r
2304 }\r
2305 }\r
2306 }\r
2307 return (NULL);\r
2308}\r
2309\r
2310/**\r
2311 Returns raw value from command line argument.\r
2312\r
2313 Raw value parameters are in the form of "value" in a specific position in the list.\r
2314\r
2315 If CheckPackage is NULL, then return NULL.\r
2316\r
2317 @param[in] CheckPackage The package of parsed command line arguments.\r
2318 @param[in] Position The position of the value.\r
2319\r
2320 @retval NULL The flag is not on the command line.\r
2321 @retval !=NULL The pointer to unicode string of the value.\r
2322 **/\r
2323CONST CHAR16*\r
2324EFIAPI\r
2325ShellCommandLineGetRawValue (\r
2326 IN CONST LIST_ENTRY * CONST CheckPackage,\r
2327 IN UINTN Position\r
2328 )\r
2329{\r
2330 LIST_ENTRY *Node;\r
2331\r
2332 //\r
2333 // check for CheckPackage == NULL\r
2334 //\r
2335 if (CheckPackage == NULL) {\r
2336 return (NULL);\r
2337 }\r
2338\r
2339 //\r
2340 // enumerate through the list of parametrs\r
2341 //\r
2342 for ( Node = GetFirstNode(CheckPackage)\r
2343 ; !IsNull (CheckPackage, Node)\r
2344 ; Node = GetNextNode(CheckPackage, Node)\r
2345 ){\r
2346 //\r
2347 // If the position matches, return the value\r
2348 //\r
2349 if (((SHELL_PARAM_PACKAGE*)Node)->OriginalPosition == Position) {\r
2350 return (((SHELL_PARAM_PACKAGE*)Node)->Value);\r
2351 }\r
2352 }\r
2353 return (NULL);\r
2354}\r
2355\r
2356/**\r
2357 returns the number of command line value parameters that were parsed.\r
2358\r
2359 this will not include flags.\r
2360\r
2361 @param[in] CheckPackage The package of parsed command line arguments.\r
2362\r
2363 @retval (UINTN)-1 No parsing has ocurred\r
2364 @return other The number of value parameters found\r
2365**/\r
2366UINTN\r
2367EFIAPI\r
2368ShellCommandLineGetCount(\r
2369 IN CONST LIST_ENTRY *CheckPackage\r
2370 )\r
2371{\r
2372 LIST_ENTRY *Node1;\r
2373 UINTN Count;\r
2374\r
2375 if (CheckPackage == NULL) {\r
2376 return (0);\r
2377 }\r
2378 for ( Node1 = GetFirstNode(CheckPackage), Count = 0\r
2379 ; !IsNull (CheckPackage, Node1)\r
2380 ; Node1 = GetNextNode(CheckPackage, Node1)\r
2381 ){\r
2382 if (((SHELL_PARAM_PACKAGE*)Node1)->Name == NULL) {\r
2383 Count++;\r
2384 }\r
2385 }\r
2386 return (Count);\r
2387}\r
2388\r
2389/**\r
2390 Determins if a parameter is duplicated.\r
2391\r
2392 If Param is not NULL then it will point to a callee allocated string buffer\r
2393 with the parameter value if a duplicate is found.\r
2394\r
2395 If CheckPackage is NULL, then ASSERT.\r
2396\r
2397 @param[in] CheckPackage The package of parsed command line arguments.\r
2398 @param[out] Param Upon finding one, a pointer to the duplicated parameter.\r
2399\r
2400 @retval EFI_SUCCESS No parameters were duplicated.\r
2401 @retval EFI_DEVICE_ERROR A duplicate was found.\r
2402 **/\r
2403EFI_STATUS\r
2404EFIAPI\r
2405ShellCommandLineCheckDuplicate (\r
2406 IN CONST LIST_ENTRY *CheckPackage,\r
2407 OUT CHAR16 **Param\r
2408 )\r
2409{\r
2410 LIST_ENTRY *Node1;\r
2411 LIST_ENTRY *Node2;\r
2412\r
2413 ASSERT(CheckPackage != NULL);\r
2414\r
2415 for ( Node1 = GetFirstNode(CheckPackage)\r
2416 ; !IsNull (CheckPackage, Node1)\r
2417 ; Node1 = GetNextNode(CheckPackage, Node1)\r
2418 ){\r
2419 for ( Node2 = GetNextNode(CheckPackage, Node1)\r
2420 ; !IsNull (CheckPackage, Node2)\r
2421 ; Node2 = GetNextNode(CheckPackage, Node2)\r
2422 ){\r
2423 if ((((SHELL_PARAM_PACKAGE*)Node1)->Name != NULL) && (((SHELL_PARAM_PACKAGE*)Node2)->Name != NULL) && StrCmp(((SHELL_PARAM_PACKAGE*)Node1)->Name, ((SHELL_PARAM_PACKAGE*)Node2)->Name) == 0) {\r
2424 if (Param != NULL) {\r
2425 *Param = NULL;\r
2426 *Param = StrnCatGrow(Param, NULL, ((SHELL_PARAM_PACKAGE*)Node1)->Name, 0);\r
2427 }\r
2428 return (EFI_DEVICE_ERROR);\r
2429 }\r
2430 }\r
2431 }\r
2432 return (EFI_SUCCESS);\r
2433}\r
2434\r
2435/**\r
2436 This is a find and replace function. Upon successful return the NewString is a copy of\r
2437 SourceString with each instance of FindTarget replaced with ReplaceWith.\r
2438\r
2439 If SourceString and NewString overlap the behavior is undefined.\r
2440\r
2441 If the string would grow bigger than NewSize it will halt and return error.\r
2442\r
2443 @param[in] SourceString The string with source buffer.\r
2444 @param[in,out] NewString The string with resultant buffer.\r
2445 @param[in] NewSize The size in bytes of NewString.\r
2446 @param[in] FindTarget The string to look for.\r
2447 @param[in] ReplaceWith The string to replace FindTarget with.\r
2448 @param[in] SkipPreCarrot If TRUE will skip a FindTarget that has a '^'\r
2449 immediately before it.\r
2450 @param[in] ParameterReplacing If TRUE will add "" around items with spaces.\r
2451\r
2452 @retval EFI_INVALID_PARAMETER SourceString was NULL.\r
2453 @retval EFI_INVALID_PARAMETER NewString was NULL.\r
2454 @retval EFI_INVALID_PARAMETER FindTarget was NULL.\r
2455 @retval EFI_INVALID_PARAMETER ReplaceWith was NULL.\r
2456 @retval EFI_INVALID_PARAMETER FindTarget had length < 1.\r
2457 @retval EFI_INVALID_PARAMETER SourceString had length < 1.\r
2458 @retval EFI_BUFFER_TOO_SMALL NewSize was less than the minimum size to hold\r
2459 the new string (truncation occurred).\r
2460 @retval EFI_SUCCESS The string was successfully copied with replacement.\r
2461**/\r
2462EFI_STATUS\r
2463EFIAPI\r
2464ShellCopySearchAndReplace(\r
2465 IN CHAR16 CONST *SourceString,\r
2466 IN OUT CHAR16 *NewString,\r
2467 IN UINTN NewSize,\r
2468 IN CONST CHAR16 *FindTarget,\r
2469 IN CONST CHAR16 *ReplaceWith,\r
2470 IN CONST BOOLEAN SkipPreCarrot,\r
2471 IN CONST BOOLEAN ParameterReplacing\r
2472 )\r
2473{\r
2474 UINTN Size;\r
2475 CHAR16 *Replace;\r
2476\r
2477 if ( (SourceString == NULL)\r
2478 || (NewString == NULL)\r
2479 || (FindTarget == NULL)\r
2480 || (ReplaceWith == NULL)\r
2481 || (StrLen(FindTarget) < 1)\r
2482 || (StrLen(SourceString) < 1)\r
2483 ){\r
2484 return (EFI_INVALID_PARAMETER);\r
2485 }\r
2486 Replace = NULL;\r
2487 if (StrStr(ReplaceWith, L" ") == NULL || !ParameterReplacing) {\r
2488 Replace = StrnCatGrow(&Replace, NULL, ReplaceWith, 0);\r
2489 } else {\r
2490 Replace = AllocateZeroPool(StrSize(ReplaceWith) + 2*sizeof(CHAR16));\r
2491 UnicodeSPrint(Replace, StrSize(ReplaceWith) + 2*sizeof(CHAR16), L"\"%s\"", ReplaceWith);\r
2492 }\r
2493 if (Replace == NULL) {\r
2494 return (EFI_OUT_OF_RESOURCES);\r
2495 }\r
2496 NewString = SetMem16(NewString, NewSize, CHAR_NULL);\r
2497 while (*SourceString != CHAR_NULL) {\r
2498 //\r
2499 // if we find the FindTarget and either Skip == FALSE or Skip and we\r
2500 // dont have a carrot do a replace...\r
2501 //\r
2502 if (StrnCmp(SourceString, FindTarget, StrLen(FindTarget)) == 0\r
2503 && ((SkipPreCarrot && *(SourceString-1) != L'^') || !SkipPreCarrot)\r
2504 ){\r
2505 SourceString += StrLen(FindTarget);\r
2506 Size = StrSize(NewString);\r
2507 if ((Size + (StrLen(Replace)*sizeof(CHAR16))) > NewSize) {\r
2508 FreePool(Replace);\r
2509 return (EFI_BUFFER_TOO_SMALL);\r
2510 }\r
2511 StrCat(NewString, Replace);\r
2512 } else {\r
2513 Size = StrSize(NewString);\r
2514 if (Size + sizeof(CHAR16) > NewSize) {\r
2515 FreePool(Replace);\r
2516 return (EFI_BUFFER_TOO_SMALL);\r
2517 }\r
2518 StrnCat(NewString, SourceString, 1);\r
2519 SourceString++;\r
2520 }\r
2521 }\r
2522 FreePool(Replace);\r
2523 return (EFI_SUCCESS);\r
2524}\r
2525\r
2526/**\r
2527 Internal worker function to output a string.\r
2528\r
2529 This function will output a string to the correct StdOut.\r
2530\r
2531 @param[in] String The string to print out.\r
2532\r
2533 @retval EFI_SUCCESS The operation was sucessful.\r
2534 @retval !EFI_SUCCESS The operation failed.\r
2535**/\r
2536EFI_STATUS\r
2537EFIAPI\r
2538InternalPrintTo (\r
2539 IN CONST CHAR16 *String\r
2540 )\r
2541{\r
2542 UINTN Size;\r
2543 Size = StrSize(String) - sizeof(CHAR16);\r
2544 if (Size == 0) {\r
2545 return (EFI_SUCCESS);\r
2546 }\r
2547 if (mEfiShellParametersProtocol != NULL) {\r
2548 return (mEfiShellProtocol->WriteFile(mEfiShellParametersProtocol->StdOut, &Size, (VOID*)String));\r
2549 }\r
2550 if (mEfiShellInterface != NULL) {\r
2551 //\r
2552 // Divide in half for old shell. Must be string length not size.\r
2553 //\r
2554 Size /= 2;\r
2555 return (mEfiShellInterface->StdOut->Write(mEfiShellInterface->StdOut, &Size, (VOID*)String));\r
2556 }\r
2557 ASSERT(FALSE);\r
2558 return (EFI_UNSUPPORTED);\r
2559}\r
2560\r
2561/**\r
2562 Print at a specific location on the screen.\r
2563\r
2564 This function will move the cursor to a given screen location and print the specified string\r
2565\r
2566 If -1 is specified for either the Row or Col the current screen location for BOTH\r
2567 will be used.\r
2568\r
2569 if either Row or Col is out of range for the current console, then ASSERT\r
2570 if Format is NULL, then ASSERT\r
2571\r
2572 In addition to the standard %-based flags as supported by UefiLib Print() this supports\r
2573 the following additional flags:\r
2574 %N - Set output attribute to normal\r
2575 %H - Set output attribute to highlight\r
2576 %E - Set output attribute to error\r
2577 %B - Set output attribute to blue color\r
2578 %V - Set output attribute to green color\r
2579\r
2580 Note: The background color is controlled by the shell command cls.\r
2581\r
2582 @param[in] Col the column to print at\r
2583 @param[in] Row the row to print at\r
2584 @param[in] Format the format string\r
2585 @param[in] Marker the marker for the variable argument list\r
2586\r
2587 @return EFI_SUCCESS The operation was successful.\r
2588 @return EFI_DEVICE_ERROR The console device reported an error.\r
2589**/\r
2590EFI_STATUS\r
2591EFIAPI\r
2592InternalShellPrintWorker(\r
2593 IN INT32 Col OPTIONAL,\r
2594 IN INT32 Row OPTIONAL,\r
2595 IN CONST CHAR16 *Format,\r
2596 IN VA_LIST Marker\r
2597 )\r
2598{\r
2599 EFI_STATUS Status;\r
2600 CHAR16 *ResumeLocation;\r
2601 CHAR16 *FormatWalker;\r
2602 UINTN OriginalAttribute;\r
2603 CHAR16 *mPostReplaceFormat;\r
2604 CHAR16 *mPostReplaceFormat2;\r
2605\r
2606 mPostReplaceFormat = AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize));\r
2607 mPostReplaceFormat2 = AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize));\r
2608\r
2609 if (mPostReplaceFormat == NULL || mPostReplaceFormat2 == NULL) {\r
2610 SHELL_FREE_NON_NULL(mPostReplaceFormat);\r
2611 SHELL_FREE_NON_NULL(mPostReplaceFormat2);\r
2612 return (EFI_OUT_OF_RESOURCES);\r
2613 }\r
2614\r
2615 Status = EFI_SUCCESS;\r
2616 OriginalAttribute = gST->ConOut->Mode->Attribute;\r
2617\r
2618 //\r
2619 // Back and forth each time fixing up 1 of our flags...\r
2620 //\r
2621 Status = ShellCopySearchAndReplace(Format, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%N", L"%%N", FALSE, FALSE);\r
2622 ASSERT_EFI_ERROR(Status);\r
2623 Status = ShellCopySearchAndReplace(mPostReplaceFormat, mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), L"%E", L"%%E", FALSE, FALSE);\r
2624 ASSERT_EFI_ERROR(Status);\r
2625 Status = ShellCopySearchAndReplace(mPostReplaceFormat2, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%H", L"%%H", FALSE, FALSE);\r
2626 ASSERT_EFI_ERROR(Status);\r
2627 Status = ShellCopySearchAndReplace(mPostReplaceFormat, mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), L"%B", L"%%B", FALSE, FALSE);\r
2628 ASSERT_EFI_ERROR(Status);\r
2629 Status = ShellCopySearchAndReplace(mPostReplaceFormat2, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%V", L"%%V", FALSE, FALSE);\r
2630 ASSERT_EFI_ERROR(Status);\r
2631\r
2632 //\r
2633 // Use the last buffer from replacing to print from...\r
2634 //\r
2635 UnicodeVSPrint (mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), mPostReplaceFormat, Marker);\r
2636\r
2637 if (Col != -1 && Row != -1) {\r
2638 Status = gST->ConOut->SetCursorPosition(gST->ConOut, Col, Row);\r
2639 }\r
2640\r
2641 FormatWalker = mPostReplaceFormat2;\r
2642 while (*FormatWalker != CHAR_NULL) {\r
2643 //\r
2644 // Find the next attribute change request\r
2645 //\r
2646 ResumeLocation = StrStr(FormatWalker, L"%");\r
2647 if (ResumeLocation != NULL) {\r
2648 *ResumeLocation = CHAR_NULL;\r
2649 }\r
2650 //\r
2651 // print the current FormatWalker string\r
2652 //\r
2653 if (StrLen(FormatWalker)>0) {\r
2654 Status = InternalPrintTo(FormatWalker);\r
2655 if (EFI_ERROR(Status)) {\r
2656 break;\r
2657 }\r
2658 }\r
2659\r
2660 //\r
2661 // update the attribute\r
2662 //\r
2663 if (ResumeLocation != NULL) {\r
2664 switch (*(ResumeLocation+1)) {\r
2665 case (L'N'):\r
2666 gST->ConOut->SetAttribute(gST->ConOut, OriginalAttribute);\r
2667 break;\r
2668 case (L'E'):\r
2669 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_YELLOW, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));\r
2670 break;\r
2671 case (L'H'):\r
2672 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_WHITE, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));\r
2673 break;\r
2674 case (L'B'):\r
2675 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_BLUE, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));\r
2676 break;\r
2677 case (L'V'):\r
2678 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_GREEN, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));\r
2679 break;\r
2680 default:\r
2681 //\r
2682 // Print a simple '%' symbol\r
2683 //\r
2684 Status = InternalPrintTo(L"%");\r
2685 if (EFI_ERROR(Status)) {\r
2686 break;\r
2687 }\r
2688 ResumeLocation = ResumeLocation - 1;\r
2689 break;\r
2690 }\r
2691 } else {\r
2692 //\r
2693 // reset to normal now...\r
2694 //\r
2695 break;\r
2696 }\r
2697\r
2698 //\r
2699 // update FormatWalker to Resume + 2 (skip the % and the indicator)\r
2700 //\r
2701 FormatWalker = ResumeLocation + 2;\r
2702 }\r
2703\r
2704 gST->ConOut->SetAttribute(gST->ConOut, OriginalAttribute);\r
2705\r
2706 SHELL_FREE_NON_NULL(mPostReplaceFormat);\r
2707 SHELL_FREE_NON_NULL(mPostReplaceFormat2);\r
2708 return (Status);\r
2709}\r
2710\r
2711/**\r
2712 Print at a specific location on the screen.\r
2713\r
2714 This function will move the cursor to a given screen location and print the specified string.\r
2715\r
2716 If -1 is specified for either the Row or Col the current screen location for BOTH\r
2717 will be used.\r
2718\r
2719 If either Row or Col is out of range for the current console, then ASSERT.\r
2720 If Format is NULL, then ASSERT.\r
2721\r
2722 In addition to the standard %-based flags as supported by UefiLib Print() this supports\r
2723 the following additional flags:\r
2724 %N - Set output attribute to normal\r
2725 %H - Set output attribute to highlight\r
2726 %E - Set output attribute to error\r
2727 %B - Set output attribute to blue color\r
2728 %V - Set output attribute to green color\r
2729\r
2730 Note: The background color is controlled by the shell command cls.\r
2731\r
2732 @param[in] Col the column to print at\r
2733 @param[in] Row the row to print at\r
2734 @param[in] Format the format string\r
2735 @param[in] ... The variable argument list.\r
2736\r
2737 @return EFI_SUCCESS The printing was successful.\r
2738 @return EFI_DEVICE_ERROR The console device reported an error.\r
2739**/\r
2740EFI_STATUS\r
2741EFIAPI\r
2742ShellPrintEx(\r
2743 IN INT32 Col OPTIONAL,\r
2744 IN INT32 Row OPTIONAL,\r
2745 IN CONST CHAR16 *Format,\r
2746 ...\r
2747 )\r
2748{\r
2749 VA_LIST Marker;\r
2750 EFI_STATUS RetVal;\r
2751 if (Format == NULL) {\r
2752 return (EFI_INVALID_PARAMETER);\r
2753 }\r
2754 VA_START (Marker, Format);\r
2755 RetVal = InternalShellPrintWorker(Col, Row, Format, Marker);\r
2756 VA_END(Marker);\r
2757 return(RetVal);\r
2758}\r
2759\r
2760/**\r
2761 Print at a specific location on the screen.\r
2762\r
2763 This function will move the cursor to a given screen location and print the specified string.\r
2764\r
2765 If -1 is specified for either the Row or Col the current screen location for BOTH\r
2766 will be used.\r
2767\r
2768 If either Row or Col is out of range for the current console, then ASSERT.\r
2769 If Format is NULL, then ASSERT.\r
2770\r
2771 In addition to the standard %-based flags as supported by UefiLib Print() this supports\r
2772 the following additional flags:\r
2773 %N - Set output attribute to normal.\r
2774 %H - Set output attribute to highlight.\r
2775 %E - Set output attribute to error.\r
2776 %B - Set output attribute to blue color.\r
2777 %V - Set output attribute to green color.\r
2778\r
2779 Note: The background color is controlled by the shell command cls.\r
2780\r
2781 @param[in] Col The column to print at.\r
2782 @param[in] Row The row to print at.\r
2783 @param[in] Language The language of the string to retrieve. If this parameter\r
2784 is NULL, then the current platform language is used.\r
2785 @param[in] HiiFormatStringId The format string Id for getting from Hii.\r
2786 @param[in] HiiFormatHandle The format string Handle for getting from Hii.\r
2787 @param[in] ... The variable argument list.\r
2788\r
2789 @return EFI_SUCCESS The printing was successful.\r
2790 @return EFI_DEVICE_ERROR The console device reported an error.\r
2791**/\r
2792EFI_STATUS\r
2793EFIAPI\r
2794ShellPrintHiiEx(\r
2795 IN INT32 Col OPTIONAL,\r
2796 IN INT32 Row OPTIONAL,\r
2797 IN CONST CHAR8 *Language OPTIONAL,\r
2798 IN CONST EFI_STRING_ID HiiFormatStringId,\r
2799 IN CONST EFI_HANDLE HiiFormatHandle,\r
2800 ...\r
2801 )\r
2802{\r
2803 VA_LIST Marker;\r
2804 CHAR16 *HiiFormatString;\r
2805 EFI_STATUS RetVal;\r
2806\r
2807 VA_START (Marker, HiiFormatHandle);\r
2808 HiiFormatString = HiiGetString(HiiFormatHandle, HiiFormatStringId, Language);\r
2809 ASSERT(HiiFormatString != NULL);\r
2810\r
2811 RetVal = InternalShellPrintWorker(Col, Row, HiiFormatString, Marker);\r
2812\r
2813 SHELL_FREE_NON_NULL(HiiFormatString);\r
2814 VA_END(Marker);\r
2815\r
2816 return (RetVal);\r
2817}\r
2818\r
2819/**\r
2820 Function to determine if a given filename represents a file or a directory.\r
2821\r
2822 @param[in] DirName Path to directory to test.\r
2823\r
2824 @retval EFI_SUCCESS The Path represents a directory\r
2825 @retval EFI_NOT_FOUND The Path does not represent a directory\r
2826 @return other The path failed to open\r
2827**/\r
2828EFI_STATUS\r
2829EFIAPI\r
2830ShellIsDirectory(\r
2831 IN CONST CHAR16 *DirName\r
2832 )\r
2833{\r
2834 EFI_STATUS Status;\r
2835 SHELL_FILE_HANDLE Handle;\r
2836 CHAR16 *TempLocation;\r
2837 CHAR16 *TempLocation2;\r
2838\r
2839 ASSERT(DirName != NULL);\r
2840\r
2841 Handle = NULL;\r
2842 TempLocation = NULL;\r
2843\r
2844 Status = ShellOpenFileByName(DirName, &Handle, EFI_FILE_MODE_READ, 0);\r
2845 if (EFI_ERROR(Status)) {\r
2846 //\r
2847 // try good logic first.\r
2848 //\r
2849 if (mEfiShellProtocol != NULL) {\r
2850 TempLocation = StrnCatGrow(&TempLocation, NULL, DirName, 0);\r
2851 TempLocation2 = StrStr(TempLocation, L":");\r
2852 if (TempLocation2 != NULL && StrLen(StrStr(TempLocation, L":")) == 2) {\r
2853 *(TempLocation2+1) = CHAR_NULL;\r
2854 }\r
2855 if (mEfiShellProtocol->GetDevicePathFromMap(TempLocation) != NULL) {\r
2856 FreePool(TempLocation);\r
2857 return (EFI_SUCCESS);\r
2858 }\r
2859 FreePool(TempLocation);\r
2860 } else {\r
2861 //\r
2862 // probably a map name?!?!!?\r
2863 //\r
2864 TempLocation = StrStr(DirName, L"\\");\r
2865 if (TempLocation != NULL && *(TempLocation+1) == CHAR_NULL) {\r
2866 return (EFI_SUCCESS);\r
2867 }\r
2868 }\r
2869 return (Status);\r
2870 }\r
2871\r
2872 if (FileHandleIsDirectory(Handle) == EFI_SUCCESS) {\r
2873 ShellCloseFile(&Handle);\r
2874 return (EFI_SUCCESS);\r
2875 }\r
2876 ShellCloseFile(&Handle);\r
2877 return (EFI_NOT_FOUND);\r
2878}\r
2879\r
2880/**\r
2881 Function to determine if a given filename represents a file.\r
2882\r
2883 @param[in] Name Path to file to test.\r
2884\r
2885 @retval EFI_SUCCESS The Path represents a file.\r
2886 @retval EFI_NOT_FOUND The Path does not represent a file.\r
2887 @retval other The path failed to open.\r
2888**/\r
2889EFI_STATUS\r
2890EFIAPI\r
2891ShellIsFile(\r
2892 IN CONST CHAR16 *Name\r
2893 )\r
2894{\r
2895 EFI_STATUS Status;\r
2896 SHELL_FILE_HANDLE Handle;\r
2897\r
2898 ASSERT(Name != NULL);\r
2899\r
2900 Handle = NULL;\r
2901\r
2902 Status = ShellOpenFileByName(Name, &Handle, EFI_FILE_MODE_READ, 0);\r
2903 if (EFI_ERROR(Status)) {\r
2904 return (Status);\r
2905 }\r
2906\r
2907 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {\r
2908 ShellCloseFile(&Handle);\r
2909 return (EFI_SUCCESS);\r
2910 }\r
2911 ShellCloseFile(&Handle);\r
2912 return (EFI_NOT_FOUND);\r
2913}\r
2914\r
2915/**\r
2916 Function to determine if a given filename represents a file.\r
2917\r
2918 This will search the CWD and then the Path.\r
2919\r
2920 If Name is NULL, then ASSERT.\r
2921\r
2922 @param[in] Name Path to file to test.\r
2923\r
2924 @retval EFI_SUCCESS The Path represents a file.\r
2925 @retval EFI_NOT_FOUND The Path does not represent a file.\r
2926 @retval other The path failed to open.\r
2927**/\r
2928EFI_STATUS\r
2929EFIAPI\r
2930ShellIsFileInPath(\r
2931 IN CONST CHAR16 *Name\r
2932 )\r
2933{\r
2934 CHAR16 *NewName;\r
2935 EFI_STATUS Status;\r
2936\r
2937 if (!EFI_ERROR(ShellIsFile(Name))) {\r
2938 return (EFI_SUCCESS);\r
2939 }\r
2940\r
2941 NewName = ShellFindFilePath(Name);\r
2942 if (NewName == NULL) {\r
2943 return (EFI_NOT_FOUND);\r
2944 }\r
2945 Status = ShellIsFile(NewName);\r
2946 FreePool(NewName);\r
2947 return (Status);\r
2948}\r
2949\r
2950/**\r
2951 Function to determine whether a string is decimal or hex representation of a number\r
2952 and return the number converted from the string.\r
2953\r
2954 @param[in] String String representation of a number\r
2955\r
2956 @return the number\r
2957 @retval (UINTN)(-1) An error ocurred.\r
2958**/\r
2959UINTN\r
2960EFIAPI\r
2961ShellStrToUintn(\r
2962 IN CONST CHAR16 *String\r
2963 )\r
2964{\r
2965 UINT64 RetVal;\r
2966 BOOLEAN Hex;\r
2967\r
2968 Hex = FALSE;\r
2969\r
2970 if (!InternalShellIsHexOrDecimalNumber(String, Hex, TRUE)) {\r
2971 Hex = TRUE;\r
2972 }\r
2973\r
2974 if (!EFI_ERROR(ShellConvertStringToUint64(String, &RetVal, Hex, TRUE))) {\r
2975 return ((UINTN)RetVal);\r
2976 }\r
2977 return ((UINTN)(-1));\r
2978}\r
2979\r
2980/**\r
2981 Safely append with automatic string resizing given length of Destination and\r
2982 desired length of copy from Source.\r
2983\r
2984 append the first D characters of Source to the end of Destination, where D is\r
2985 the lesser of Count and the StrLen() of Source. If appending those D characters\r
2986 will fit within Destination (whose Size is given as CurrentSize) and\r
2987 still leave room for a NULL terminator, then those characters are appended,\r
2988 starting at the original terminating NULL of Destination, and a new terminating\r
2989 NULL is appended.\r
2990\r
2991 If appending D characters onto Destination will result in a overflow of the size\r
2992 given in CurrentSize the string will be grown such that the copy can be performed\r
2993 and CurrentSize will be updated to the new size.\r
2994\r
2995 If Source is NULL, there is nothing to append, just return the current buffer in\r
2996 Destination.\r
2997\r
2998 if Destination is NULL, then ASSERT()\r
2999 if Destination's current length (including NULL terminator) is already more then\r
3000 CurrentSize, then ASSERT()\r
3001\r
3002 @param[in,out] Destination The String to append onto\r
3003 @param[in,out] CurrentSize on call the number of bytes in Destination. On\r
3004 return possibly the new size (still in bytes). if NULL\r
3005 then allocate whatever is needed.\r
3006 @param[in] Source The String to append from\r
3007 @param[in] Count Maximum number of characters to append. if 0 then\r
3008 all are appended.\r
3009\r
3010 @return Destination return the resultant string.\r
3011**/\r
3012CHAR16*\r
3013EFIAPI\r
3014StrnCatGrow (\r
3015 IN OUT CHAR16 **Destination,\r
3016 IN OUT UINTN *CurrentSize,\r
3017 IN CONST CHAR16 *Source,\r
3018 IN UINTN Count\r
3019 )\r
3020{\r
3021 UINTN DestinationStartSize;\r
3022 UINTN NewSize;\r
3023\r
3024 //\r
3025 // ASSERTs\r
3026 //\r
3027 ASSERT(Destination != NULL);\r
3028\r
3029 //\r
3030 // If there's nothing to do then just return Destination\r
3031 //\r
3032 if (Source == NULL) {\r
3033 return (*Destination);\r
3034 }\r
3035\r
3036 //\r
3037 // allow for un-initialized pointers, based on size being 0\r
3038 //\r
3039 if (CurrentSize != NULL && *CurrentSize == 0) {\r
3040 *Destination = NULL;\r
3041 }\r
3042\r
3043 //\r
3044 // allow for NULL pointers address as Destination\r
3045 //\r
3046 if (*Destination != NULL) {\r
3047 ASSERT(CurrentSize != 0);\r
3048 DestinationStartSize = StrSize(*Destination);\r
3049 ASSERT(DestinationStartSize <= *CurrentSize);\r
3050 } else {\r
3051 DestinationStartSize = 0;\r
3052// ASSERT(*CurrentSize == 0);\r
3053 }\r
3054\r
3055 //\r
3056 // Append all of Source?\r
3057 //\r
3058 if (Count == 0) {\r
3059 Count = StrLen(Source);\r
3060 }\r
3061\r
3062 //\r
3063 // Test and grow if required\r
3064 //\r
3065 if (CurrentSize != NULL) {\r
3066 NewSize = *CurrentSize;\r
3067 while (NewSize < (DestinationStartSize + (Count*sizeof(CHAR16)))) {\r
3068 NewSize += 2 * Count * sizeof(CHAR16);\r
3069 }\r
3070 *Destination = ReallocatePool(*CurrentSize, NewSize, *Destination);\r
3071 ASSERT(*Destination != NULL);\r
3072 *CurrentSize = NewSize;\r
3073 } else {\r
3074 *Destination = AllocateZeroPool((Count+1)*sizeof(CHAR16));\r
3075 ASSERT(*Destination != NULL);\r
3076 }\r
3077\r
3078 //\r
3079 // Now use standard StrnCat on a big enough buffer\r
3080 //\r
3081 if (*Destination == NULL) {\r
3082 return (NULL);\r
3083 }\r
3084 return StrnCat(*Destination, Source, Count);\r
3085}\r
3086\r
3087/**\r
3088 Prompt the user and return the resultant answer to the requestor.\r
3089\r
3090 This function will display the requested question on the shell prompt and then\r
3091 wait for an apropriate answer to be input from the console.\r
3092\r
3093 if the SHELL_PROMPT_REQUEST_TYPE is SHELL_PROMPT_REQUEST_TYPE_YESNO, ShellPromptResponseTypeQuitContinue\r
3094 or SHELL_PROMPT_REQUEST_TYPE_YESNOCANCEL then *Response is of type SHELL_PROMPT_RESPONSE.\r
3095\r
3096 if the SHELL_PROMPT_REQUEST_TYPE is ShellPromptResponseTypeFreeform then *Response is of type\r
3097 CHAR16*.\r
3098\r
3099 In either case *Response must be callee freed if Response was not NULL;\r
3100\r
3101 @param Type What type of question is asked. This is used to filter the input\r
3102 to prevent invalid answers to question.\r
3103 @param Prompt Pointer to string prompt to use to request input.\r
3104 @param Response Pointer to Response which will be populated upon return.\r
3105\r
3106 @retval EFI_SUCCESS The operation was sucessful.\r
3107 @retval EFI_UNSUPPORTED The operation is not supported as requested.\r
3108 @retval EFI_INVALID_PARAMETER A parameter was invalid.\r
3109 @return other The operation failed.\r
3110**/\r
3111EFI_STATUS\r
3112EFIAPI\r
3113ShellPromptForResponse (\r
3114 IN SHELL_PROMPT_REQUEST_TYPE Type,\r
3115 IN CHAR16 *Prompt OPTIONAL,\r
3116 IN OUT VOID **Response OPTIONAL\r
3117 )\r
3118{\r
3119 EFI_STATUS Status;\r
3120 EFI_INPUT_KEY Key;\r
3121 UINTN EventIndex;\r
3122 SHELL_PROMPT_RESPONSE *Resp;\r
3123 UINTN Size;\r
3124 CHAR16 *Buffer;\r
3125\r
3126 Status = EFI_UNSUPPORTED;\r
3127 Resp = NULL;\r
3128 Buffer = NULL;\r
3129 Size = 0;\r
3130 if (Type != ShellPromptResponseTypeFreeform) {\r
3131 Resp = (SHELL_PROMPT_RESPONSE*)AllocateZeroPool(sizeof(SHELL_PROMPT_RESPONSE));\r
3132 if (Resp == NULL) {\r
3133 return (EFI_OUT_OF_RESOURCES);\r
3134 }\r
3135 }\r
3136\r
3137 switch(Type) {\r
3138 case ShellPromptResponseTypeQuitContinue:\r
3139 if (Prompt != NULL) {\r
3140 ShellPrintEx(-1, -1, L"%s", Prompt);\r
3141 }\r
3142 //\r
3143 // wait for valid response\r
3144 //\r
3145 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);\r
3146 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);\r
3147 ASSERT_EFI_ERROR(Status);\r
3148 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);\r
3149 if (Key.UnicodeChar == L'Q' || Key.UnicodeChar ==L'q') {\r
3150 *Resp = ShellPromptResponseQuit;\r
3151 } else {\r
3152 *Resp = ShellPromptResponseContinue;\r
3153 }\r
3154 break;\r
3155 case ShellPromptResponseTypeYesNoCancel:\r
3156 if (Prompt != NULL) {\r
3157 ShellPrintEx(-1, -1, L"%s", Prompt);\r
3158 }\r
3159 //\r
3160 // wait for valid response\r
3161 //\r
3162 *Resp = ShellPromptResponseMax;\r
3163 while (*Resp == ShellPromptResponseMax) {\r
3164 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);\r
3165 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);\r
3166 ASSERT_EFI_ERROR(Status);\r
3167 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);\r
3168 switch (Key.UnicodeChar) {\r
3169 case L'Y':\r
3170 case L'y':\r
3171 *Resp = ShellPromptResponseYes;\r
3172 break;\r
3173 case L'N':\r
3174 case L'n':\r
3175 *Resp = ShellPromptResponseNo;\r
3176 break;\r
3177 case L'C':\r
3178 case L'c':\r
3179 *Resp = ShellPromptResponseCancel;\r
3180 break;\r
3181 }\r
3182 }\r
3183 break; case ShellPromptResponseTypeYesNoAllCancel:\r
3184 if (Prompt != NULL) {\r
3185 ShellPrintEx(-1, -1, L"%s", Prompt);\r
3186 }\r
3187 //\r
3188 // wait for valid response\r
3189 //\r
3190 *Resp = ShellPromptResponseMax;\r
3191 while (*Resp == ShellPromptResponseMax) {\r
3192 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);\r
3193 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);\r
3194 ASSERT_EFI_ERROR(Status);\r
3195 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);\r
3196 switch (Key.UnicodeChar) {\r
3197 case L'Y':\r
3198 case L'y':\r
3199 *Resp = ShellPromptResponseYes;\r
3200 break;\r
3201 case L'N':\r
3202 case L'n':\r
3203 *Resp = ShellPromptResponseNo;\r
3204 break;\r
3205 case L'A':\r
3206 case L'a':\r
3207 *Resp = ShellPromptResponseAll;\r
3208 break;\r
3209 case L'C':\r
3210 case L'c':\r
3211 *Resp = ShellPromptResponseCancel;\r
3212 break;\r
3213 }\r
3214 }\r
3215 break;\r
3216 case ShellPromptResponseTypeEnterContinue:\r
3217 case ShellPromptResponseTypeAnyKeyContinue:\r
3218 if (Prompt != NULL) {\r
3219 ShellPrintEx(-1, -1, L"%s", Prompt);\r
3220 }\r
3221 //\r
3222 // wait for valid response\r
3223 //\r
3224 *Resp = ShellPromptResponseMax;\r
3225 while (*Resp == ShellPromptResponseMax) {\r
3226 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);\r
3227 if (Type == ShellPromptResponseTypeEnterContinue) {\r
3228 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);\r
3229 ASSERT_EFI_ERROR(Status);\r
3230 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);\r
3231 if (Key.UnicodeChar == CHAR_CARRIAGE_RETURN) {\r
3232 *Resp = ShellPromptResponseContinue;\r
3233 break;\r
3234 }\r
3235 }\r
3236 if (Type == ShellPromptResponseTypeAnyKeyContinue) {\r
3237 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);\r
3238 ASSERT_EFI_ERROR(Status);\r
3239 *Resp = ShellPromptResponseContinue;\r
3240 break;\r
3241 }\r
3242 }\r
3243 break;\r
3244 case ShellPromptResponseTypeYesNo:\r
3245 if (Prompt != NULL) {\r
3246 ShellPrintEx(-1, -1, L"%s", Prompt);\r
3247 }\r
3248 //\r
3249 // wait for valid response\r
3250 //\r
3251 *Resp = ShellPromptResponseMax;\r
3252 while (*Resp == ShellPromptResponseMax) {\r
3253 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);\r
3254 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);\r
3255 ASSERT_EFI_ERROR(Status);\r
3256 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);\r
3257 switch (Key.UnicodeChar) {\r
3258 case L'Y':\r
3259 case L'y':\r
3260 *Resp = ShellPromptResponseYes;\r
3261 break;\r
3262 case L'N':\r
3263 case L'n':\r
3264 *Resp = ShellPromptResponseNo;\r
3265 break;\r
3266 }\r
3267 }\r
3268 break;\r
3269 case ShellPromptResponseTypeFreeform:\r
3270 if (Prompt != NULL) {\r
3271 ShellPrintEx(-1, -1, L"%s", Prompt);\r
3272 }\r
3273 while(1) {\r
3274 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);\r
3275 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);\r
3276 ASSERT_EFI_ERROR(Status);\r
3277 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);\r
3278 if (Key.UnicodeChar == CHAR_CARRIAGE_RETURN) {\r
3279 break;\r
3280 }\r
3281 ASSERT((Buffer == NULL && Size == 0) || (Buffer != NULL));\r
3282 StrnCatGrow(&Buffer, &Size, &Key.UnicodeChar, 1);\r
3283 }\r
3284 break;\r
3285 //\r
3286 // This is the location to add new prompt types.\r
3287 //\r
3288 default:\r
3289 ASSERT(FALSE);\r
3290 }\r
3291\r
3292 if (Response != NULL) {\r
3293 if (Resp != NULL) {\r
3294 *Response = Resp;\r
3295 } else if (Buffer != NULL) {\r
3296 *Response = Buffer;\r
3297 }\r
3298 } else {\r
3299 if (Resp != NULL) {\r
3300 FreePool(Resp);\r
3301 }\r
3302 if (Buffer != NULL) {\r
3303 FreePool(Buffer);\r
3304 }\r
3305 }\r
3306\r
3307 ShellPrintEx(-1, -1, L"\r\n");\r
3308 return (Status);\r
3309}\r
3310\r
3311/**\r
3312 Prompt the user and return the resultant answer to the requestor.\r
3313\r
3314 This function is the same as ShellPromptForResponse, except that the prompt is\r
3315 automatically pulled from HII.\r
3316\r
3317 @param Type What type of question is asked. This is used to filter the input\r
3318 to prevent invalid answers to question.\r
3319 @param[in] HiiFormatStringId The format string Id for getting from Hii.\r
3320 @param[in] HiiFormatHandle The format string Handle for getting from Hii.\r
3321 @param Response Pointer to Response which will be populated upon return.\r
3322\r
3323 @retval EFI_SUCCESS the operation was sucessful.\r
3324 @return other the operation failed.\r
3325\r
3326 @sa ShellPromptForResponse\r
3327**/\r
3328EFI_STATUS\r
3329EFIAPI\r
3330ShellPromptForResponseHii (\r
3331 IN SHELL_PROMPT_REQUEST_TYPE Type,\r
3332 IN CONST EFI_STRING_ID HiiFormatStringId,\r
3333 IN CONST EFI_HANDLE HiiFormatHandle,\r
3334 IN OUT VOID **Response\r
3335 )\r
3336{\r
3337 CHAR16 *Prompt;\r
3338 EFI_STATUS Status;\r
3339\r
3340 Prompt = HiiGetString(HiiFormatHandle, HiiFormatStringId, NULL);\r
3341 Status = ShellPromptForResponse(Type, Prompt, Response);\r
3342 FreePool(Prompt);\r
3343 return (Status);\r
3344}\r
3345\r
3346/**\r
3347 Function to determin if an entire string is a valid number.\r
3348\r
3349 If Hex it must be preceeded with a 0x or has ForceHex, set TRUE.\r
3350\r
3351 @param[in] String The string to evaluate.\r
3352 @param[in] ForceHex TRUE - always assume hex.\r
3353 @param[in] StopAtSpace TRUE to halt upon finding a space, FALSE to keep going.\r
3354\r
3355 @retval TRUE It is all numeric (dec/hex) characters.\r
3356 @retval FALSE There is a non-numeric character.\r
3357**/\r
3358BOOLEAN\r
3359EFIAPI\r
3360InternalShellIsHexOrDecimalNumber (\r
3361 IN CONST CHAR16 *String,\r
3362 IN CONST BOOLEAN ForceHex,\r
3363 IN CONST BOOLEAN StopAtSpace\r
3364 )\r
3365{\r
3366 BOOLEAN Hex;\r
3367\r
3368 //\r
3369 // chop off a single negative sign\r
3370 //\r
3371 if (String != NULL && *String == L'-') {\r
3372 String++;\r
3373 }\r
3374 \r
3375 if (String == NULL) {\r
3376 return (FALSE);\r
3377 }\r
3378\r
3379 //\r
3380 // chop leading zeroes\r
3381 //\r
3382 while(String != NULL && *String == L'0'){\r
3383 String++;\r
3384 }\r
3385 //\r
3386 // allow '0x' or '0X', but not 'x' or 'X'\r
3387 //\r
3388 if (String != NULL && (*String == L'x' || *String == L'X')) {\r
3389 if (*(String-1) != L'0') {\r
3390 //\r
3391 // we got an x without a preceeding 0\r
3392 //\r
3393 return (FALSE);\r
3394 }\r
3395 String++;\r
3396 Hex = TRUE;\r
3397 } else if (ForceHex) {\r
3398 Hex = TRUE;\r
3399 } else {\r
3400 Hex = FALSE;\r
3401 }\r
3402\r
3403 //\r
3404 // loop through the remaining characters and use the lib function\r
3405 //\r
3406 for ( ; String != NULL && *String != CHAR_NULL && !(StopAtSpace && *String == L' ') ; String++){\r
3407 if (Hex) {\r
3408 if (!ShellIsHexaDecimalDigitCharacter(*String)) {\r
3409 return (FALSE);\r
3410 }\r
3411 } else {\r
3412 if (!ShellIsDecimalDigitCharacter(*String)) {\r
3413 return (FALSE);\r
3414 }\r
3415 }\r
3416 }\r
3417\r
3418 return (TRUE);\r
3419}\r
3420\r
3421/**\r
3422 Function to determine if a given filename exists.\r
3423\r
3424 @param[in] Name Path to test.\r
3425\r
3426 @retval EFI_SUCCESS The Path represents a file.\r
3427 @retval EFI_NOT_FOUND The Path does not represent a file.\r
3428 @retval other The path failed to open.\r
3429**/\r
3430EFI_STATUS\r
3431EFIAPI\r
3432ShellFileExists(\r
3433 IN CONST CHAR16 *Name\r
3434 )\r
3435{\r
3436 EFI_STATUS Status;\r
3437 EFI_SHELL_FILE_INFO *List;\r
3438\r
3439 ASSERT(Name != NULL);\r
3440\r
3441 List = NULL;\r
3442 Status = ShellOpenFileMetaArg((CHAR16*)Name, EFI_FILE_MODE_READ, &List);\r
3443 if (EFI_ERROR(Status)) {\r
3444 return (Status);\r
3445 }\r
3446\r
3447 ShellCloseFileMetaArg(&List);\r
3448\r
3449 return (EFI_SUCCESS);\r
3450}\r
3451\r
3452/**\r
3453 Convert a Unicode character to upper case only if \r
3454 it maps to a valid small-case ASCII character.\r
3455\r
3456 This internal function only deal with Unicode character\r
3457 which maps to a valid small-case ASCII character, i.e.\r
3458 L'a' to L'z'. For other Unicode character, the input character\r
3459 is returned directly.\r
3460\r
3461 @param Char The character to convert.\r
3462\r
3463 @retval LowerCharacter If the Char is with range L'a' to L'z'.\r
3464 @retval Unchanged Otherwise.\r
3465\r
3466**/\r
3467CHAR16\r
3468EFIAPI\r
3469InternalShellCharToUpper (\r
3470 IN CHAR16 Char\r
3471 )\r
3472{\r
3473 if (Char >= L'a' && Char <= L'z') {\r
3474 return (CHAR16) (Char - (L'a' - L'A'));\r
3475 }\r
3476\r
3477 return Char;\r
3478}\r
3479\r
3480/**\r
3481 Convert a Unicode character to numerical value.\r
3482\r
3483 This internal function only deal with Unicode character\r
3484 which maps to a valid hexadecimal ASII character, i.e.\r
3485 L'0' to L'9', L'a' to L'f' or L'A' to L'F'. For other \r
3486 Unicode character, the value returned does not make sense.\r
3487\r
3488 @param Char The character to convert.\r
3489\r
3490 @return The numerical value converted.\r
3491\r
3492**/\r
3493UINTN\r
3494EFIAPI\r
3495InternalShellHexCharToUintn (\r
3496 IN CHAR16 Char\r
3497 )\r
3498{\r
3499 if (ShellIsDecimalDigitCharacter (Char)) {\r
3500 return Char - L'0';\r
3501 }\r
3502\r
3503 return (UINTN) (10 + InternalShellCharToUpper (Char) - L'A');\r
3504}\r
3505\r
3506/**\r
3507 Convert a Null-terminated Unicode hexadecimal string to a value of type UINT64.\r
3508\r
3509 This function returns a value of type UINTN by interpreting the contents\r
3510 of the Unicode string specified by String as a hexadecimal number.\r
3511 The format of the input Unicode string String is:\r
3512\r
3513 [spaces][zeros][x][hexadecimal digits].\r
3514\r
3515 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].\r
3516 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix.\r
3517 If "x" appears in the input string, it must be prefixed with at least one 0.\r
3518 The function will ignore the pad space, which includes spaces or tab characters,\r
3519 before [zeros], [x] or [hexadecimal digit]. The running zero before [x] or\r
3520 [hexadecimal digit] will be ignored. Then, the decoding starts after [x] or the\r
3521 first valid hexadecimal digit. Then, the function stops at the first character that is\r
3522 a not a valid hexadecimal character or NULL, whichever one comes first.\r
3523\r
3524 If String has only pad spaces, then zero is returned.\r
3525 If String has no leading pad spaces, leading zeros or valid hexadecimal digits,\r
3526 then zero is returned.\r
3527\r
3528 @param[in] String A pointer to a Null-terminated Unicode string.\r
3529 @param[out] Value Upon a successful return the value of the conversion.\r
3530 @param[in] StopAtSpace FALSE to skip spaces.\r
3531\r
3532 @retval EFI_SUCCESS The conversion was successful.\r
3533 @retval EFI_INVALID_PARAMETER A parameter was NULL or invalid.\r
3534 @retval EFI_DEVICE_ERROR An overflow occured.\r
3535**/\r
3536EFI_STATUS\r
3537EFIAPI\r
3538InternalShellStrHexToUint64 (\r
3539 IN CONST CHAR16 *String,\r
3540 OUT UINT64 *Value,\r
3541 IN CONST BOOLEAN StopAtSpace\r
3542 )\r
3543{\r
3544 UINT64 Result;\r
3545\r
3546 if (String == NULL || StrSize(String) == 0 || Value == NULL) {\r
3547 return (EFI_INVALID_PARAMETER);\r
3548 }\r
3549 \r
3550 //\r
3551 // Ignore the pad spaces (space or tab) \r
3552 //\r
3553 while ((*String == L' ') || (*String == L'\t')) {\r
3554 String++;\r
3555 }\r
3556\r
3557 //\r
3558 // Ignore leading Zeros after the spaces\r
3559 //\r
3560 while (*String == L'0') {\r
3561 String++;\r
3562 }\r
3563\r
3564 if (InternalShellCharToUpper (*String) == L'X') {\r
3565 if (*(String - 1) != L'0') {\r
3566 return 0;\r
3567 }\r
3568 //\r
3569 // Skip the 'X'\r
3570 //\r
3571 String++;\r
3572 }\r
3573\r
3574 Result = 0;\r
3575 \r
3576 //\r
3577 // Skip spaces if requested\r
3578 //\r
3579 while (StopAtSpace && *String == L' ') {\r
3580 String++;\r
3581 }\r
3582 \r
3583 while (ShellIsHexaDecimalDigitCharacter (*String)) {\r
3584 //\r
3585 // If the Hex Number represented by String overflows according \r
3586 // to the range defined by UINTN, then ASSERT().\r
3587 //\r
3588 if (!(Result <= (RShiftU64((((UINT64) ~0) - InternalShellHexCharToUintn (*String)), 4)))) {\r
3589// if (!(Result <= ((((UINT64) ~0) - InternalShellHexCharToUintn (*String)) >> 4))) {\r
3590 return (EFI_DEVICE_ERROR);\r
3591 }\r
3592\r
3593 Result = (LShiftU64(Result, 4));\r
3594 Result += InternalShellHexCharToUintn (*String);\r
3595 String++;\r
3596\r
3597 //\r
3598 // Skip spaces if requested\r
3599 //\r
3600 while (StopAtSpace && *String == L' ') {\r
3601 String++;\r
3602 }\r
3603 }\r
3604\r
3605 *Value = Result;\r
3606 return (EFI_SUCCESS);\r
3607}\r
3608\r
3609/**\r
3610 Convert a Null-terminated Unicode decimal string to a value of\r
3611 type UINT64.\r
3612\r
3613 This function returns a value of type UINT64 by interpreting the contents\r
3614 of the Unicode string specified by String as a decimal number. The format\r
3615 of the input Unicode string String is:\r
3616\r
3617 [spaces] [decimal digits].\r
3618\r
3619 The valid decimal digit character is in the range [0-9]. The\r
3620 function will ignore the pad space, which includes spaces or\r
3621 tab characters, before [decimal digits]. The running zero in the\r
3622 beginning of [decimal digits] will be ignored. Then, the function\r
3623 stops at the first character that is a not a valid decimal character\r
3624 or a Null-terminator, whichever one comes first.\r
3625\r
3626 If String has only pad spaces, then 0 is returned.\r
3627 If String has no pad spaces or valid decimal digits,\r
3628 then 0 is returned.\r
3629\r
3630 @param[in] String A pointer to a Null-terminated Unicode string.\r
3631 @param[out] Value Upon a successful return the value of the conversion.\r
3632 @param[in] StopAtSpace FALSE to skip spaces.\r
3633\r
3634 @retval EFI_SUCCESS The conversion was successful.\r
3635 @retval EFI_INVALID_PARAMETER A parameter was NULL or invalid.\r
3636 @retval EFI_DEVICE_ERROR An overflow occured.\r
3637**/\r
3638EFI_STATUS\r
3639EFIAPI\r
3640InternalShellStrDecimalToUint64 (\r
3641 IN CONST CHAR16 *String,\r
3642 OUT UINT64 *Value,\r
3643 IN CONST BOOLEAN StopAtSpace\r
3644 )\r
3645{\r
3646 UINT64 Result;\r
3647\r
3648 if (String == NULL || StrSize (String) == 0 || Value == NULL) {\r
3649 return (EFI_INVALID_PARAMETER);\r
3650 }\r
3651\r
3652 //\r
3653 // Ignore the pad spaces (space or tab)\r
3654 //\r
3655 while ((*String == L' ') || (*String == L'\t')) {\r
3656 String++;\r
3657 }\r
3658\r
3659 //\r
3660 // Ignore leading Zeros after the spaces\r
3661 //\r
3662 while (*String == L'0') {\r
3663 String++;\r
3664 }\r
3665\r
3666 Result = 0;\r
3667\r
3668 //\r
3669 // Skip spaces if requested\r
3670 //\r
3671 while (StopAtSpace && *String == L' ') {\r
3672 String++;\r
3673 }\r
3674 while (ShellIsDecimalDigitCharacter (*String)) {\r
3675 //\r
3676 // If the number represented by String overflows according \r
3677 // to the range defined by UINT64, then ASSERT().\r
3678 //\r
3679 \r
3680 if (!(Result <= (DivU64x32((((UINT64) ~0) - (*String - L'0')),10)))) {\r
3681 return (EFI_DEVICE_ERROR);\r
3682 }\r
3683\r
3684 Result = MultU64x32(Result, 10) + (*String - L'0');\r
3685 String++;\r
3686\r
3687 //\r
3688 // Stop at spaces if requested\r
3689 //\r
3690 if (StopAtSpace && *String == L' ') {\r
3691 break;\r
3692 }\r
3693 }\r
3694\r
3695 *Value = Result;\r
3696 \r
3697 return (EFI_SUCCESS);\r
3698}\r
3699\r
3700/**\r
3701 Function to verify and convert a string to its numerical value.\r
3702\r
3703 If Hex it must be preceeded with a 0x, 0X, or has ForceHex set TRUE.\r
3704\r
3705 @param[in] String The string to evaluate.\r
3706 @param[out] Value Upon a successful return the value of the conversion.\r
3707 @param[in] ForceHex TRUE - always assume hex.\r
3708 @param[in] StopAtSpace FALSE to skip spaces.\r
3709 \r
3710 @retval EFI_SUCCESS The conversion was successful.\r
3711 @retval EFI_INVALID_PARAMETER String contained an invalid character.\r
3712 @retval EFI_NOT_FOUND String was a number, but Value was NULL.\r
3713**/\r
3714EFI_STATUS\r
3715EFIAPI\r
3716ShellConvertStringToUint64(\r
3717 IN CONST CHAR16 *String,\r
3718 OUT UINT64 *Value,\r
3719 IN CONST BOOLEAN ForceHex,\r
3720 IN CONST BOOLEAN StopAtSpace\r
3721 )\r
3722{\r
3723 UINT64 RetVal;\r
3724 CONST CHAR16 *Walker;\r
3725 EFI_STATUS Status;\r
3726 BOOLEAN Hex;\r
3727\r
3728 Hex = ForceHex;\r
3729\r
3730 if (!InternalShellIsHexOrDecimalNumber(String, Hex, StopAtSpace)) {\r
3731 if (!Hex) {\r
3732 Hex = TRUE;\r
3733 if (!InternalShellIsHexOrDecimalNumber(String, Hex, StopAtSpace)) {\r
3734 return (EFI_INVALID_PARAMETER);\r
3735 }\r
3736 } else {\r
3737 return (EFI_INVALID_PARAMETER);\r
3738 }\r
3739 }\r
3740\r
3741 //\r
3742 // Chop off leading spaces\r
3743 //\r
3744 for (Walker = String; Walker != NULL && *Walker != CHAR_NULL && *Walker == L' '; Walker++);\r
3745\r
3746 //\r
3747 // make sure we have something left that is numeric.\r
3748 //\r
3749 if (Walker == NULL || *Walker == CHAR_NULL || !InternalShellIsHexOrDecimalNumber(Walker, Hex, StopAtSpace)) {\r
3750 return (EFI_INVALID_PARAMETER);\r
3751 } \r
3752\r
3753 //\r
3754 // do the conversion.\r
3755 //\r
3756 if (Hex || StrnCmp(Walker, L"0x", 2) == 0 || StrnCmp(Walker, L"0X", 2) == 0){\r
3757 Status = InternalShellStrHexToUint64(Walker, &RetVal, StopAtSpace);\r
3758 } else {\r
3759 Status = InternalShellStrDecimalToUint64(Walker, &RetVal, StopAtSpace);\r
3760 }\r
3761\r
3762 if (Value == NULL && !EFI_ERROR(Status)) {\r
3763 return (EFI_NOT_FOUND);\r
3764 }\r
3765\r
3766 if (Value != NULL) {\r
3767 *Value = RetVal;\r
3768 }\r
3769\r
3770 return (Status);\r
3771}\r
3772\r
3773/**\r
3774 Function to determin if an entire string is a valid number.\r
3775\r
3776 If Hex it must be preceeded with a 0x or has ForceHex, set TRUE.\r
3777\r
3778 @param[in] String The string to evaluate.\r
3779 @param[in] ForceHex TRUE - always assume hex.\r
3780 @param[in] StopAtSpace TRUE to halt upon finding a space, FALSE to keep going.\r
3781\r
3782 @retval TRUE It is all numeric (dec/hex) characters.\r
3783 @retval FALSE There is a non-numeric character.\r
3784**/\r
3785BOOLEAN\r
3786EFIAPI\r
3787ShellIsHexOrDecimalNumber (\r
3788 IN CONST CHAR16 *String,\r
3789 IN CONST BOOLEAN ForceHex,\r
3790 IN CONST BOOLEAN StopAtSpace\r
3791 )\r
3792{\r
3793 if (ShellConvertStringToUint64(String, NULL, ForceHex, StopAtSpace) == EFI_NOT_FOUND) {\r
3794 return (TRUE);\r
3795 }\r
3796 return (FALSE);\r
3797}\r