]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - ShellPkg/Library/UefiShellLib/UefiShellLib.c
allow for failure return when called without a shell present.
[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 // Check for EFI shell\r
1218 //\r
1219 if (mEfiShellEnvironment2 != NULL) {\r
1220 return (mEfiShellEnvironment2->CurDir(DeviceName));\r
1221 }\r
1222\r
1223 return (NULL);\r
1224}\r
1225/**\r
1226 sets (enabled or disabled) the page break mode\r
1227\r
1228 when page break mode is enabled the screen will stop scrolling\r
1229 and wait for operator input before scrolling a subsequent screen.\r
1230\r
1231 @param CurrentState TRUE to enable and FALSE to disable\r
1232**/\r
1233VOID\r
1234EFIAPI\r
1235ShellSetPageBreakMode (\r
1236 IN BOOLEAN CurrentState\r
1237 )\r
1238{\r
1239 //\r
1240 // check for enabling\r
1241 //\r
1242 if (CurrentState != 0x00) {\r
1243 //\r
1244 // check for UEFI Shell 2.0\r
1245 //\r
1246 if (mEfiShellProtocol != NULL) {\r
1247 //\r
1248 // Enable with UEFI 2.0 Shell\r
1249 //\r
1250 mEfiShellProtocol->EnablePageBreak();\r
1251 return;\r
1252 } else {\r
1253 //\r
1254 // ASSERT that must have EFI Shell\r
1255 //\r
1256 ASSERT(mEfiShellEnvironment2 != NULL);\r
1257 //\r
1258 // Enable with EFI Shell\r
1259 //\r
1260 mEfiShellEnvironment2->EnablePageBreak (DEFAULT_INIT_ROW, DEFAULT_AUTO_LF);\r
1261 return;\r
1262 }\r
1263 } else {\r
1264 //\r
1265 // check for UEFI Shell 2.0\r
1266 //\r
1267 if (mEfiShellProtocol != NULL) {\r
1268 //\r
1269 // Disable with UEFI 2.0 Shell\r
1270 //\r
1271 mEfiShellProtocol->DisablePageBreak();\r
1272 return;\r
1273 } else {\r
1274 //\r
1275 // ASSERT that must have EFI Shell\r
1276 //\r
1277 ASSERT(mEfiShellEnvironment2 != NULL);\r
1278 //\r
1279 // Disable with EFI Shell\r
1280 //\r
1281 mEfiShellEnvironment2->DisablePageBreak ();\r
1282 return;\r
1283 }\r
1284 }\r
1285}\r
1286\r
1287///\r
1288/// version of EFI_SHELL_FILE_INFO struct, except has no CONST pointers.\r
1289/// This allows for the struct to be populated.\r
1290///\r
1291typedef struct {\r
1292 LIST_ENTRY Link;\r
1293 EFI_STATUS Status;\r
1294 CHAR16 *FullName;\r
1295 CHAR16 *FileName;\r
1296 SHELL_FILE_HANDLE Handle;\r
1297 EFI_FILE_INFO *Info;\r
1298} EFI_SHELL_FILE_INFO_NO_CONST;\r
1299\r
1300/**\r
1301 Converts a EFI shell list of structures to the coresponding UEFI Shell 2.0 type of list.\r
1302\r
1303 if OldStyleFileList is NULL then ASSERT()\r
1304\r
1305 this function will convert a SHELL_FILE_ARG based list into a callee allocated\r
1306 EFI_SHELL_FILE_INFO based list. it is up to the caller to free the memory via\r
1307 the ShellCloseFileMetaArg function.\r
1308\r
1309 @param[in] FileList the EFI shell list type\r
1310 @param[in,out] ListHead the list to add to\r
1311\r
1312 @retval the resultant head of the double linked new format list;\r
1313**/\r
1314LIST_ENTRY*\r
1315EFIAPI\r
1316InternalShellConvertFileListType (\r
1317 IN LIST_ENTRY *FileList,\r
1318 IN OUT LIST_ENTRY *ListHead\r
1319 )\r
1320{\r
1321 SHELL_FILE_ARG *OldInfo;\r
1322 LIST_ENTRY *Link;\r
1323 EFI_SHELL_FILE_INFO_NO_CONST *NewInfo;\r
1324\r
1325 //\r
1326 // ASSERTs\r
1327 //\r
1328 ASSERT(FileList != NULL);\r
1329 ASSERT(ListHead != NULL);\r
1330\r
1331 //\r
1332 // enumerate through each member of the old list and copy\r
1333 //\r
1334 for (Link = FileList->ForwardLink; Link != FileList; Link = Link->ForwardLink) {\r
1335 OldInfo = CR (Link, SHELL_FILE_ARG, Link, SHELL_FILE_ARG_SIGNATURE);\r
1336 ASSERT(OldInfo != NULL);\r
1337\r
1338 //\r
1339 // Skip ones that failed to open...\r
1340 //\r
1341 if (OldInfo->Status != EFI_SUCCESS) {\r
1342 continue;\r
1343 }\r
1344\r
1345 //\r
1346 // make sure the old list was valid\r
1347 //\r
1348 ASSERT(OldInfo->Info != NULL);\r
1349 ASSERT(OldInfo->FullName != NULL);\r
1350 ASSERT(OldInfo->FileName != NULL);\r
1351\r
1352 //\r
1353 // allocate a new EFI_SHELL_FILE_INFO object\r
1354 //\r
1355 NewInfo = AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));\r
1356 ASSERT(NewInfo != NULL);\r
1357 if (NewInfo == NULL) {\r
1358 break;\r
1359 }\r
1360\r
1361 //\r
1362 // copy the simple items\r
1363 //\r
1364 NewInfo->Handle = OldInfo->Handle;\r
1365 NewInfo->Status = OldInfo->Status;\r
1366\r
1367 // old shell checks for 0 not NULL\r
1368 OldInfo->Handle = 0;\r
1369\r
1370 //\r
1371 // allocate new space to copy strings and structure\r
1372 //\r
1373 NewInfo->FullName = AllocateZeroPool(StrSize(OldInfo->FullName));\r
1374 NewInfo->FileName = AllocateZeroPool(StrSize(OldInfo->FileName));\r
1375 NewInfo->Info = AllocateZeroPool((UINTN)OldInfo->Info->Size);\r
1376\r
1377 //\r
1378 // make sure all the memory allocations were sucessful\r
1379 //\r
1380 ASSERT(NewInfo->FullName != NULL);\r
1381 ASSERT(NewInfo->FileName != NULL);\r
1382 ASSERT(NewInfo->Info != NULL);\r
1383\r
1384 //\r
1385 // Copt the strings and structure\r
1386 //\r
1387 StrCpy(NewInfo->FullName, OldInfo->FullName);\r
1388 StrCpy(NewInfo->FileName, OldInfo->FileName);\r
1389 gBS->CopyMem (NewInfo->Info, OldInfo->Info, (UINTN)OldInfo->Info->Size);\r
1390\r
1391 //\r
1392 // add that to the list\r
1393 //\r
1394 InsertTailList(ListHead, &NewInfo->Link);\r
1395 }\r
1396 return (ListHead);\r
1397}\r
1398/**\r
1399 Opens a group of files based on a path.\r
1400\r
1401 This function uses the Arg to open all the matching files. Each matched\r
1402 file has a SHELL_FILE_ARG structure to record the file information. These\r
1403 structures are placed on the list ListHead. Users can get the SHELL_FILE_ARG\r
1404 structures from ListHead to access each file. This function supports wildcards\r
1405 and will process '?' and '*' as such. the list must be freed with a call to\r
1406 ShellCloseFileMetaArg().\r
1407\r
1408 If you are NOT appending to an existing list *ListHead must be NULL. If\r
1409 *ListHead is NULL then it must be callee freed.\r
1410\r
1411 @param Arg pointer to path string\r
1412 @param OpenMode mode to open files with\r
1413 @param ListHead head of linked list of results\r
1414\r
1415 @retval EFI_SUCCESS the operation was sucessful and the list head\r
1416 contains the list of opened files\r
1417 @return != EFI_SUCCESS the operation failed\r
1418\r
1419 @sa InternalShellConvertFileListType\r
1420**/\r
1421EFI_STATUS\r
1422EFIAPI\r
1423ShellOpenFileMetaArg (\r
1424 IN CHAR16 *Arg,\r
1425 IN UINT64 OpenMode,\r
1426 IN OUT EFI_SHELL_FILE_INFO **ListHead\r
1427 )\r
1428{\r
1429 EFI_STATUS Status;\r
1430 LIST_ENTRY mOldStyleFileList;\r
1431\r
1432 //\r
1433 // ASSERT that Arg and ListHead are not NULL\r
1434 //\r
1435 ASSERT(Arg != NULL);\r
1436 ASSERT(ListHead != NULL);\r
1437\r
1438 //\r
1439 // Check for UEFI Shell 2.0 protocols\r
1440 //\r
1441 if (mEfiShellProtocol != NULL) {\r
1442 if (*ListHead == NULL) {\r
1443 *ListHead = (EFI_SHELL_FILE_INFO*)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));\r
1444 if (*ListHead == NULL) {\r
1445 return (EFI_OUT_OF_RESOURCES);\r
1446 }\r
1447 InitializeListHead(&((*ListHead)->Link));\r
1448 }\r
1449 Status = mEfiShellProtocol->OpenFileList(Arg,\r
1450 OpenMode,\r
1451 ListHead);\r
1452 if (EFI_ERROR(Status)) {\r
1453 mEfiShellProtocol->RemoveDupInFileList(ListHead);\r
1454 } else {\r
1455 Status = mEfiShellProtocol->RemoveDupInFileList(ListHead);\r
1456 }\r
1457 if (*ListHead != NULL && IsListEmpty(&(*ListHead)->Link)) {\r
1458 FreePool(*ListHead);\r
1459 *ListHead = NULL;\r
1460 return (EFI_NOT_FOUND);\r
1461 }\r
1462 return (Status);\r
1463 }\r
1464\r
1465 //\r
1466 // ASSERT that we must have EFI shell\r
1467 //\r
1468 ASSERT(mEfiShellEnvironment2 != NULL);\r
1469\r
1470 //\r
1471 // make sure the list head is initialized\r
1472 //\r
1473 InitializeListHead(&mOldStyleFileList);\r
1474\r
1475 //\r
1476 // Get the EFI Shell list of files\r
1477 //\r
1478 Status = mEfiShellEnvironment2->FileMetaArg(Arg, &mOldStyleFileList);\r
1479 if (EFI_ERROR(Status)) {\r
1480 *ListHead = NULL;\r
1481 return (Status);\r
1482 }\r
1483\r
1484 if (*ListHead == NULL) {\r
1485 *ListHead = (EFI_SHELL_FILE_INFO *)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));\r
1486 if (*ListHead == NULL) {\r
1487 return (EFI_OUT_OF_RESOURCES);\r
1488 }\r
1489 InitializeListHead(&((*ListHead)->Link));\r
1490 }\r
1491\r
1492 //\r
1493 // Convert that to equivalent of UEFI Shell 2.0 structure\r
1494 //\r
1495 InternalShellConvertFileListType(&mOldStyleFileList, &(*ListHead)->Link);\r
1496\r
1497 //\r
1498 // Free the EFI Shell version that was converted.\r
1499 //\r
1500 mEfiShellEnvironment2->FreeFileList(&mOldStyleFileList);\r
1501\r
1502 if ((*ListHead)->Link.ForwardLink == (*ListHead)->Link.BackLink && (*ListHead)->Link.BackLink == &((*ListHead)->Link)) {\r
1503 FreePool(*ListHead);\r
1504 *ListHead = NULL;\r
1505 Status = EFI_NOT_FOUND;\r
1506 }\r
1507\r
1508 return (Status);\r
1509}\r
1510/**\r
1511 Free the linked list returned from ShellOpenFileMetaArg.\r
1512\r
1513 if ListHead is NULL then ASSERT().\r
1514\r
1515 @param ListHead the pointer to free.\r
1516\r
1517 @retval EFI_SUCCESS the operation was sucessful.\r
1518**/\r
1519EFI_STATUS\r
1520EFIAPI\r
1521ShellCloseFileMetaArg (\r
1522 IN OUT EFI_SHELL_FILE_INFO **ListHead\r
1523 )\r
1524{\r
1525 LIST_ENTRY *Node;\r
1526\r
1527 //\r
1528 // ASSERT that ListHead is not NULL\r
1529 //\r
1530 ASSERT(ListHead != NULL);\r
1531\r
1532 //\r
1533 // Check for UEFI Shell 2.0 protocols\r
1534 //\r
1535 if (mEfiShellProtocol != NULL) {\r
1536 return (mEfiShellProtocol->FreeFileList(ListHead));\r
1537 } else {\r
1538 //\r
1539 // Since this is EFI Shell version we need to free our internally made copy\r
1540 // of the list\r
1541 //\r
1542 for ( Node = GetFirstNode(&(*ListHead)->Link)\r
1543 ; *ListHead != NULL && !IsListEmpty(&(*ListHead)->Link)\r
1544 ; Node = GetFirstNode(&(*ListHead)->Link)) {\r
1545 RemoveEntryList(Node);\r
1546 ((EFI_FILE_PROTOCOL*)((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->Handle)->Close(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->Handle);\r
1547 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->FullName);\r
1548 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->FileName);\r
1549 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->Info);\r
1550 FreePool((EFI_SHELL_FILE_INFO_NO_CONST*)Node);\r
1551 }\r
1552 return EFI_SUCCESS;\r
1553 }\r
1554}\r
1555\r
1556/**\r
1557 Find a file by searching the CWD and then the path.\r
1558\r
1559 If FileName is NULL then ASSERT.\r
1560\r
1561 If the return value is not NULL then the memory must be caller freed.\r
1562\r
1563 @param FileName Filename string.\r
1564\r
1565 @retval NULL the file was not found\r
1566 @return !NULL the full path to the file.\r
1567**/\r
1568CHAR16 *\r
1569EFIAPI\r
1570ShellFindFilePath (\r
1571 IN CONST CHAR16 *FileName\r
1572 )\r
1573{\r
1574 CONST CHAR16 *Path;\r
1575 SHELL_FILE_HANDLE Handle;\r
1576 EFI_STATUS Status;\r
1577 CHAR16 *RetVal;\r
1578 CHAR16 *TestPath;\r
1579 CONST CHAR16 *Walker;\r
1580 UINTN Size;\r
1581 CHAR16 *TempChar;\r
1582\r
1583 RetVal = NULL;\r
1584\r
1585 //\r
1586 // First make sure its not an absolute path.\r
1587 //\r
1588 Status = ShellOpenFileByName(FileName, &Handle, EFI_FILE_MODE_READ, 0);\r
1589 if (!EFI_ERROR(Status)){\r
1590 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {\r
1591 ASSERT(RetVal == NULL);\r
1592 RetVal = StrnCatGrow(&RetVal, NULL, FileName, 0);\r
1593 ShellCloseFile(&Handle);\r
1594 return (RetVal);\r
1595 } else {\r
1596 ShellCloseFile(&Handle);\r
1597 }\r
1598 }\r
1599\r
1600 Path = ShellGetEnvironmentVariable(L"cwd");\r
1601 if (Path != NULL) {\r
1602 Size = StrSize(Path);\r
1603 Size += StrSize(FileName);\r
1604 TestPath = AllocateZeroPool(Size);\r
1605 ASSERT(TestPath != NULL);\r
1606 if (TestPath == NULL) {\r
1607 return (NULL);\r
1608 }\r
1609 StrCpy(TestPath, Path);\r
1610 StrCat(TestPath, FileName);\r
1611 Status = ShellOpenFileByName(TestPath, &Handle, EFI_FILE_MODE_READ, 0);\r
1612 if (!EFI_ERROR(Status)){\r
1613 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {\r
1614 ASSERT(RetVal == NULL);\r
1615 RetVal = StrnCatGrow(&RetVal, NULL, TestPath, 0);\r
1616 ShellCloseFile(&Handle);\r
1617 FreePool(TestPath);\r
1618 return (RetVal);\r
1619 } else {\r
1620 ShellCloseFile(&Handle);\r
1621 }\r
1622 }\r
1623 FreePool(TestPath);\r
1624 }\r
1625 Path = ShellGetEnvironmentVariable(L"path");\r
1626 if (Path != NULL) {\r
1627 Size = StrSize(Path)+sizeof(CHAR16);\r
1628 Size += StrSize(FileName);\r
1629 TestPath = AllocateZeroPool(Size);\r
1630 if (TestPath == NULL) {\r
1631 return (NULL);\r
1632 }\r
1633 Walker = (CHAR16*)Path;\r
1634 do {\r
1635 CopyMem(TestPath, Walker, StrSize(Walker));\r
1636 if (TestPath != NULL) {\r
1637 TempChar = StrStr(TestPath, L";");\r
1638 if (TempChar != NULL) {\r
1639 *TempChar = CHAR_NULL;\r
1640 }\r
1641 if (TestPath[StrLen(TestPath)-1] != L'\\') {\r
1642 StrCat(TestPath, L"\\");\r
1643 }\r
1644 if (FileName[0] == L'\\') {\r
1645 FileName++;\r
1646 }\r
1647 StrCat(TestPath, FileName);\r
1648 if (StrStr(Walker, L";") != NULL) {\r
1649 Walker = StrStr(Walker, L";") + 1;\r
1650 } else {\r
1651 Walker = NULL;\r
1652 }\r
1653 Status = ShellOpenFileByName(TestPath, &Handle, EFI_FILE_MODE_READ, 0);\r
1654 if (!EFI_ERROR(Status)){\r
1655 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {\r
1656 ASSERT(RetVal == NULL);\r
1657 RetVal = StrnCatGrow(&RetVal, NULL, TestPath, 0);\r
1658 ShellCloseFile(&Handle);\r
1659 break;\r
1660 } else {\r
1661 ShellCloseFile(&Handle);\r
1662 }\r
1663 }\r
1664 }\r
1665 } while (Walker != NULL && Walker[0] != CHAR_NULL);\r
1666 FreePool(TestPath);\r
1667 }\r
1668 return (RetVal);\r
1669}\r
1670\r
1671/**\r
1672 Find a file by searching the CWD and then the path with a variable set of file\r
1673 extensions. If the file is not found it will append each extension in the list\r
1674 in the order provided and return the first one that is successful.\r
1675\r
1676 If FileName is NULL, then ASSERT.\r
1677 If FileExtension is NULL, then behavior is identical to ShellFindFilePath.\r
1678\r
1679 If the return value is not NULL then the memory must be caller freed.\r
1680\r
1681 @param[in] FileName Filename string.\r
1682 @param[in] FileExtension Semi-colon delimeted list of possible extensions.\r
1683\r
1684 @retval NULL The file was not found.\r
1685 @retval !NULL The path to the file.\r
1686**/\r
1687CHAR16 *\r
1688EFIAPI\r
1689ShellFindFilePathEx (\r
1690 IN CONST CHAR16 *FileName,\r
1691 IN CONST CHAR16 *FileExtension\r
1692 )\r
1693{\r
1694 CHAR16 *TestPath;\r
1695 CHAR16 *RetVal;\r
1696 CONST CHAR16 *ExtensionWalker;\r
1697 UINTN Size;\r
1698 CHAR16 *TempChar;\r
1699 CHAR16 *TempChar2;\r
1700\r
1701 ASSERT(FileName != NULL);\r
1702 if (FileExtension == NULL) {\r
1703 return (ShellFindFilePath(FileName));\r
1704 }\r
1705 RetVal = ShellFindFilePath(FileName);\r
1706 if (RetVal != NULL) {\r
1707 return (RetVal);\r
1708 }\r
1709 Size = StrSize(FileName);\r
1710 Size += StrSize(FileExtension);\r
1711 TestPath = AllocateZeroPool(Size);\r
1712 ASSERT(TestPath != NULL);\r
1713 if (TestPath == NULL) {\r
1714 return (NULL);\r
1715 }\r
1716 for (ExtensionWalker = FileExtension, TempChar2 = (CHAR16*)FileExtension; TempChar2 != NULL ; ExtensionWalker = TempChar2 + 1){\r
1717 StrCpy(TestPath, FileName);\r
1718 if (ExtensionWalker != NULL) {\r
1719 StrCat(TestPath, ExtensionWalker);\r
1720 }\r
1721 TempChar = StrStr(TestPath, L";");\r
1722 if (TempChar != NULL) {\r
1723 *TempChar = CHAR_NULL;\r
1724 }\r
1725 RetVal = ShellFindFilePath(TestPath);\r
1726 if (RetVal != NULL) {\r
1727 break;\r
1728 }\r
1729 ASSERT(ExtensionWalker != NULL);\r
1730 TempChar2 = StrStr(ExtensionWalker, L";");\r
1731 }\r
1732 FreePool(TestPath);\r
1733 return (RetVal);\r
1734}\r
1735\r
1736typedef struct {\r
1737 LIST_ENTRY Link;\r
1738 CHAR16 *Name;\r
1739 SHELL_PARAM_TYPE Type;\r
1740 CHAR16 *Value;\r
1741 UINTN OriginalPosition;\r
1742} SHELL_PARAM_PACKAGE;\r
1743\r
1744/**\r
1745 Checks the list of valid arguments and returns TRUE if the item was found. If the\r
1746 return value is TRUE then the type parameter is set also.\r
1747\r
1748 if CheckList is NULL then ASSERT();\r
1749 if Name is NULL then ASSERT();\r
1750 if Type is NULL then ASSERT();\r
1751\r
1752 @param Name pointer to Name of parameter found\r
1753 @param CheckList List to check against\r
1754 @param Type pointer to type of parameter if it was found\r
1755\r
1756 @retval TRUE the Parameter was found. Type is valid.\r
1757 @retval FALSE the Parameter was not found. Type is not valid.\r
1758**/\r
1759BOOLEAN\r
1760EFIAPI\r
1761InternalIsOnCheckList (\r
1762 IN CONST CHAR16 *Name,\r
1763 IN CONST SHELL_PARAM_ITEM *CheckList,\r
1764 OUT SHELL_PARAM_TYPE *Type\r
1765 )\r
1766{\r
1767 SHELL_PARAM_ITEM *TempListItem;\r
1768 CHAR16 *TempString;\r
1769\r
1770 //\r
1771 // ASSERT that all 3 pointer parameters aren't NULL\r
1772 //\r
1773 ASSERT(CheckList != NULL);\r
1774 ASSERT(Type != NULL);\r
1775 ASSERT(Name != NULL);\r
1776\r
1777 //\r
1778 // question mark and page break mode are always supported\r
1779 //\r
1780 if ((StrCmp(Name, L"-?") == 0) ||\r
1781 (StrCmp(Name, L"-b") == 0)\r
1782 ) {\r
1783 *Type = TypeFlag;\r
1784 return (TRUE);\r
1785 }\r
1786\r
1787 //\r
1788 // Enumerate through the list\r
1789 //\r
1790 for (TempListItem = (SHELL_PARAM_ITEM*)CheckList ; TempListItem->Name != NULL ; TempListItem++) {\r
1791 //\r
1792 // If the Type is TypeStart only check the first characters of the passed in param\r
1793 // If it matches set the type and return TRUE\r
1794 //\r
1795 if (TempListItem->Type == TypeStart) { \r
1796 if (StrnCmp(Name, TempListItem->Name, StrLen(TempListItem->Name)) == 0) {\r
1797 *Type = TempListItem->Type;\r
1798 return (TRUE);\r
1799 }\r
1800 TempString = NULL;\r
1801 TempString = StrnCatGrow(&TempString, NULL, Name, StrLen(TempListItem->Name));\r
1802 if (TempString != NULL) {\r
1803 if (StringNoCaseCompare(&TempString, &TempListItem->Name) == 0) {\r
1804 *Type = TempListItem->Type;\r
1805 FreePool(TempString);\r
1806 return (TRUE);\r
1807 }\r
1808 FreePool(TempString);\r
1809 }\r
1810 } else if (StringNoCaseCompare(&Name, &TempListItem->Name) == 0) {\r
1811 *Type = TempListItem->Type;\r
1812 return (TRUE);\r
1813 }\r
1814 }\r
1815\r
1816 return (FALSE);\r
1817}\r
1818/**\r
1819 Checks the string for indicators of "flag" status. this is a leading '/', '-', or '+'\r
1820\r
1821 @param[in] Name pointer to Name of parameter found\r
1822 @param[in] AlwaysAllowNumbers TRUE to allow numbers, FALSE to not.\r
1823\r
1824 @retval TRUE the Parameter is a flag.\r
1825 @retval FALSE the Parameter not a flag.\r
1826**/\r
1827BOOLEAN\r
1828EFIAPI\r
1829InternalIsFlag (\r
1830 IN CONST CHAR16 *Name,\r
1831 IN BOOLEAN AlwaysAllowNumbers\r
1832 )\r
1833{\r
1834 //\r
1835 // ASSERT that Name isn't NULL\r
1836 //\r
1837 ASSERT(Name != NULL);\r
1838\r
1839 //\r
1840 // If we accept numbers then dont return TRUE. (they will be values)\r
1841 //\r
1842 if (((Name[0] == L'-' || Name[0] == L'+') && ShellIsHexaDecimalDigitCharacter(Name[1])) && AlwaysAllowNumbers) {\r
1843 return (FALSE);\r
1844 }\r
1845\r
1846 //\r
1847 // If the Name has a /, +, or - as the first character return TRUE\r
1848 //\r
1849 if ((Name[0] == L'/') ||\r
1850 (Name[0] == L'-') ||\r
1851 (Name[0] == L'+')\r
1852 ) {\r
1853 return (TRUE);\r
1854 }\r
1855 return (FALSE);\r
1856}\r
1857\r
1858/**\r
1859 Checks the command line arguments passed against the list of valid ones.\r
1860\r
1861 If no initialization is required, then return RETURN_SUCCESS.\r
1862\r
1863 @param[in] CheckList pointer to list of parameters to check\r
1864 @param[out] CheckPackage pointer to pointer to list checked values\r
1865 @param[out] ProblemParam optional pointer to pointer to unicode string for\r
1866 the paramater that caused failure. If used then the\r
1867 caller is responsible for freeing the memory.\r
1868 @param[in] AutoPageBreak will automatically set PageBreakEnabled for "b" parameter\r
1869 @param[in] Argv pointer to array of parameters\r
1870 @param[in] Argc Count of parameters in Argv\r
1871 @param[in] AlwaysAllowNumbers TRUE to allow numbers always, FALSE otherwise.\r
1872\r
1873 @retval EFI_SUCCESS The operation completed sucessfully.\r
1874 @retval EFI_OUT_OF_RESOURCES A memory allocation failed\r
1875 @retval EFI_INVALID_PARAMETER A parameter was invalid\r
1876 @retval EFI_VOLUME_CORRUPTED the command line was corrupt. an argument was\r
1877 duplicated. the duplicated command line argument\r
1878 was returned in ProblemParam if provided.\r
1879 @retval EFI_NOT_FOUND a argument required a value that was missing.\r
1880 the invalid command line argument was returned in\r
1881 ProblemParam if provided.\r
1882**/\r
1883EFI_STATUS\r
1884EFIAPI\r
1885InternalCommandLineParse (\r
1886 IN CONST SHELL_PARAM_ITEM *CheckList,\r
1887 OUT LIST_ENTRY **CheckPackage,\r
1888 OUT CHAR16 **ProblemParam OPTIONAL,\r
1889 IN BOOLEAN AutoPageBreak,\r
1890 IN CONST CHAR16 **Argv,\r
1891 IN UINTN Argc,\r
1892 IN BOOLEAN AlwaysAllowNumbers\r
1893 )\r
1894{\r
1895 UINTN LoopCounter;\r
1896 SHELL_PARAM_TYPE CurrentItemType;\r
1897 SHELL_PARAM_PACKAGE *CurrentItemPackage;\r
1898 UINTN GetItemValue;\r
1899 UINTN ValueSize;\r
1900 UINTN Count;\r
1901 CONST CHAR16 *TempPointer;\r
1902\r
1903 CurrentItemPackage = NULL;\r
1904 GetItemValue = 0;\r
1905 ValueSize = 0;\r
1906 Count = 0;\r
1907\r
1908 //\r
1909 // If there is only 1 item we dont need to do anything\r
1910 //\r
1911 if (Argc < 1) {\r
1912 *CheckPackage = NULL;\r
1913 return (EFI_SUCCESS);\r
1914 }\r
1915\r
1916 //\r
1917 // ASSERTs\r
1918 //\r
1919 ASSERT(CheckList != NULL);\r
1920 ASSERT(Argv != NULL);\r
1921\r
1922 //\r
1923 // initialize the linked list\r
1924 //\r
1925 *CheckPackage = (LIST_ENTRY*)AllocateZeroPool(sizeof(LIST_ENTRY));\r
1926 InitializeListHead(*CheckPackage);\r
1927\r
1928 //\r
1929 // loop through each of the arguments\r
1930 //\r
1931 for (LoopCounter = 0 ; LoopCounter < Argc ; ++LoopCounter) {\r
1932 if (Argv[LoopCounter] == NULL) {\r
1933 //\r
1934 // do nothing for NULL argv\r
1935 //\r
1936 } else if (InternalIsOnCheckList(Argv[LoopCounter], CheckList, &CurrentItemType)) {\r
1937 //\r
1938 // We might have leftover if last parameter didnt have optional value\r
1939 //\r
1940 if (GetItemValue != 0) {\r
1941 GetItemValue = 0;\r
1942 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);\r
1943 }\r
1944 //\r
1945 // this is a flag\r
1946 //\r
1947 CurrentItemPackage = AllocateZeroPool(sizeof(SHELL_PARAM_PACKAGE));\r
1948 ASSERT(CurrentItemPackage != NULL);\r
1949 CurrentItemPackage->Name = AllocateZeroPool(StrSize(Argv[LoopCounter]));\r
1950 ASSERT(CurrentItemPackage->Name != NULL);\r
1951 StrCpy(CurrentItemPackage->Name, Argv[LoopCounter]);\r
1952 CurrentItemPackage->Type = CurrentItemType;\r
1953 CurrentItemPackage->OriginalPosition = (UINTN)(-1);\r
1954 CurrentItemPackage->Value = NULL;\r
1955\r
1956 //\r
1957 // Does this flag require a value\r
1958 //\r
1959 switch (CurrentItemPackage->Type) {\r
1960 //\r
1961 // possibly trigger the next loop(s) to populate the value of this item\r
1962 //\r
1963 case TypeValue:\r
1964 GetItemValue = 1;\r
1965 ValueSize = 0;\r
1966 break;\r
1967 case TypeDoubleValue:\r
1968 GetItemValue = 2;\r
1969 ValueSize = 0;\r
1970 break;\r
1971 case TypeMaxValue:\r
1972 GetItemValue = (UINTN)(-1);\r
1973 ValueSize = 0;\r
1974 break;\r
1975 default:\r
1976 //\r
1977 // this item has no value expected; we are done\r
1978 //\r
1979 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);\r
1980 ASSERT(GetItemValue == 0);\r
1981 break;\r
1982 }\r
1983 } else if (GetItemValue != 0 && !InternalIsFlag(Argv[LoopCounter], AlwaysAllowNumbers)) {\r
1984 ASSERT(CurrentItemPackage != NULL);\r
1985 //\r
1986 // get the item VALUE for a previous flag\r
1987 //\r
1988 CurrentItemPackage->Value = ReallocatePool(ValueSize, ValueSize + StrSize(Argv[LoopCounter]) + sizeof(CHAR16), CurrentItemPackage->Value);\r
1989 ASSERT(CurrentItemPackage->Value != NULL);\r
1990 if (ValueSize == 0) {\r
1991 StrCpy(CurrentItemPackage->Value, Argv[LoopCounter]);\r
1992 } else {\r
1993 StrCat(CurrentItemPackage->Value, L" ");\r
1994 StrCat(CurrentItemPackage->Value, Argv[LoopCounter]);\r
1995 }\r
1996 ValueSize += StrSize(Argv[LoopCounter]) + sizeof(CHAR16);\r
1997 GetItemValue--;\r
1998 if (GetItemValue == 0) {\r
1999 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);\r
2000 }\r
2001 } else if (!InternalIsFlag(Argv[LoopCounter], AlwaysAllowNumbers) ){ //|| ProblemParam == NULL) {\r
2002 //\r
2003 // add this one as a non-flag\r
2004 //\r
2005\r
2006 TempPointer = Argv[LoopCounter];\r
2007 if ((*TempPointer == L'^' && *(TempPointer+1) == L'-') \r
2008 || (*TempPointer == L'^' && *(TempPointer+1) == L'/')\r
2009 || (*TempPointer == L'^' && *(TempPointer+1) == L'+')\r
2010 ){\r
2011 TempPointer++;\r
2012 }\r
2013 CurrentItemPackage = AllocateZeroPool(sizeof(SHELL_PARAM_PACKAGE));\r
2014 ASSERT(CurrentItemPackage != NULL);\r
2015 CurrentItemPackage->Name = NULL;\r
2016 CurrentItemPackage->Type = TypePosition;\r
2017 CurrentItemPackage->Value = AllocateZeroPool(StrSize(TempPointer));\r
2018 ASSERT(CurrentItemPackage->Value != NULL);\r
2019 StrCpy(CurrentItemPackage->Value, TempPointer);\r
2020 CurrentItemPackage->OriginalPosition = Count++;\r
2021 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);\r
2022 } else {\r
2023 //\r
2024 // this was a non-recognised flag... error!\r
2025 //\r
2026 if (ProblemParam != NULL) {\r
2027 *ProblemParam = AllocateZeroPool(StrSize(Argv[LoopCounter]));\r
2028 ASSERT(*ProblemParam != NULL);\r
2029 StrCpy(*ProblemParam, Argv[LoopCounter]); \r
2030 }\r
2031 ShellCommandLineFreeVarList(*CheckPackage);\r
2032 *CheckPackage = NULL;\r
2033 return (EFI_VOLUME_CORRUPTED);\r
2034 }\r
2035 }\r
2036 if (GetItemValue != 0) {\r
2037 GetItemValue = 0;\r
2038 InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);\r
2039 }\r
2040 //\r
2041 // support for AutoPageBreak\r
2042 //\r
2043 if (AutoPageBreak && ShellCommandLineGetFlag(*CheckPackage, L"-b")) {\r
2044 ShellSetPageBreakMode(TRUE);\r
2045 }\r
2046 return (EFI_SUCCESS);\r
2047}\r
2048\r
2049/**\r
2050 Checks the command line arguments passed against the list of valid ones.\r
2051 Optionally removes NULL values first.\r
2052\r
2053 If no initialization is required, then return RETURN_SUCCESS.\r
2054\r
2055 @param[in] CheckList The pointer to list of parameters to check.\r
2056 @param[out] CheckPackage The package of checked values.\r
2057 @param[out] ProblemParam Optional pointer to pointer to unicode string for\r
2058 the paramater that caused failure.\r
2059 @param[in] AutoPageBreak Will automatically set PageBreakEnabled.\r
2060 @param[in] AlwaysAllowNumbers Will never fail for number based flags.\r
2061\r
2062 @retval EFI_SUCCESS The operation completed sucessfully.\r
2063 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.\r
2064 @retval EFI_INVALID_PARAMETER A parameter was invalid.\r
2065 @retval EFI_VOLUME_CORRUPTED The command line was corrupt.\r
2066 @retval EFI_DEVICE_ERROR The commands contained 2 opposing arguments. One\r
2067 of the command line arguments was returned in\r
2068 ProblemParam if provided.\r
2069 @retval EFI_NOT_FOUND A argument required a value that was missing.\r
2070 The invalid command line argument was returned in\r
2071 ProblemParam if provided.\r
2072**/\r
2073EFI_STATUS\r
2074EFIAPI\r
2075ShellCommandLineParseEx (\r
2076 IN CONST SHELL_PARAM_ITEM *CheckList,\r
2077 OUT LIST_ENTRY **CheckPackage,\r
2078 OUT CHAR16 **ProblemParam OPTIONAL,\r
2079 IN BOOLEAN AutoPageBreak,\r
2080 IN BOOLEAN AlwaysAllowNumbers\r
2081 )\r
2082{\r
2083 //\r
2084 // ASSERT that CheckList and CheckPackage aren't NULL\r
2085 //\r
2086 ASSERT(CheckList != NULL);\r
2087 ASSERT(CheckPackage != NULL);\r
2088\r
2089 //\r
2090 // Check for UEFI Shell 2.0 protocols\r
2091 //\r
2092 if (mEfiShellParametersProtocol != NULL) {\r
2093 return (InternalCommandLineParse(CheckList,\r
2094 CheckPackage,\r
2095 ProblemParam,\r
2096 AutoPageBreak,\r
2097 (CONST CHAR16**) mEfiShellParametersProtocol->Argv,\r
2098 mEfiShellParametersProtocol->Argc,\r
2099 AlwaysAllowNumbers));\r
2100 }\r
2101\r
2102 //\r
2103 // ASSERT That EFI Shell is not required\r
2104 //\r
2105 ASSERT (mEfiShellInterface != NULL);\r
2106 return (InternalCommandLineParse(CheckList,\r
2107 CheckPackage,\r
2108 ProblemParam,\r
2109 AutoPageBreak,\r
2110 (CONST CHAR16**) mEfiShellInterface->Argv,\r
2111 mEfiShellInterface->Argc,\r
2112 AlwaysAllowNumbers));\r
2113}\r
2114\r
2115/**\r
2116 Frees shell variable list that was returned from ShellCommandLineParse.\r
2117\r
2118 This function will free all the memory that was used for the CheckPackage\r
2119 list of postprocessed shell arguments.\r
2120\r
2121 this function has no return value.\r
2122\r
2123 if CheckPackage is NULL, then return\r
2124\r
2125 @param CheckPackage the list to de-allocate\r
2126 **/\r
2127VOID\r
2128EFIAPI\r
2129ShellCommandLineFreeVarList (\r
2130 IN LIST_ENTRY *CheckPackage\r
2131 )\r
2132{\r
2133 LIST_ENTRY *Node;\r
2134\r
2135 //\r
2136 // check for CheckPackage == NULL\r
2137 //\r
2138 if (CheckPackage == NULL) {\r
2139 return;\r
2140 }\r
2141\r
2142 //\r
2143 // for each node in the list\r
2144 //\r
2145 for ( Node = GetFirstNode(CheckPackage)\r
2146 ; !IsListEmpty(CheckPackage)\r
2147 ; Node = GetFirstNode(CheckPackage)\r
2148 ){\r
2149 //\r
2150 // Remove it from the list\r
2151 //\r
2152 RemoveEntryList(Node);\r
2153\r
2154 //\r
2155 // if it has a name free the name\r
2156 //\r
2157 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {\r
2158 FreePool(((SHELL_PARAM_PACKAGE*)Node)->Name);\r
2159 }\r
2160\r
2161 //\r
2162 // if it has a value free the value\r
2163 //\r
2164 if (((SHELL_PARAM_PACKAGE*)Node)->Value != NULL) {\r
2165 FreePool(((SHELL_PARAM_PACKAGE*)Node)->Value);\r
2166 }\r
2167\r
2168 //\r
2169 // free the node structure\r
2170 //\r
2171 FreePool((SHELL_PARAM_PACKAGE*)Node);\r
2172 }\r
2173 //\r
2174 // free the list head node\r
2175 //\r
2176 FreePool(CheckPackage);\r
2177}\r
2178/**\r
2179 Checks for presence of a flag parameter\r
2180\r
2181 flag arguments are in the form of "-<Key>" or "/<Key>", but do not have a value following the key\r
2182\r
2183 if CheckPackage is NULL then return FALSE.\r
2184 if KeyString is NULL then ASSERT()\r
2185\r
2186 @param CheckPackage The package of parsed command line arguments\r
2187 @param KeyString the Key of the command line argument to check for\r
2188\r
2189 @retval TRUE the flag is on the command line\r
2190 @retval FALSE the flag is not on the command line\r
2191 **/\r
2192BOOLEAN\r
2193EFIAPI\r
2194ShellCommandLineGetFlag (\r
2195 IN CONST LIST_ENTRY * CONST CheckPackage,\r
2196 IN CONST CHAR16 * CONST KeyString\r
2197 )\r
2198{\r
2199 LIST_ENTRY *Node;\r
2200 CHAR16 *TempString;\r
2201\r
2202 //\r
2203 // ASSERT that both CheckPackage and KeyString aren't NULL\r
2204 //\r
2205 ASSERT(KeyString != NULL);\r
2206\r
2207 //\r
2208 // return FALSE for no package\r
2209 //\r
2210 if (CheckPackage == NULL) {\r
2211 return (FALSE);\r
2212 }\r
2213\r
2214 //\r
2215 // enumerate through the list of parametrs\r
2216 //\r
2217 for ( Node = GetFirstNode(CheckPackage)\r
2218 ; !IsNull (CheckPackage, Node)\r
2219 ; Node = GetNextNode(CheckPackage, Node)\r
2220 ){\r
2221 //\r
2222 // If the Name matches, return TRUE (and there may be NULL name)\r
2223 //\r
2224 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {\r
2225 //\r
2226 // If Type is TypeStart then only compare the begining of the strings\r
2227 //\r
2228 if (((SHELL_PARAM_PACKAGE*)Node)->Type == TypeStart) {\r
2229 if (StrnCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name, StrLen(KeyString)) == 0) {\r
2230 return (TRUE);\r
2231 }\r
2232 TempString = NULL;\r
2233 TempString = StrnCatGrow(&TempString, NULL, KeyString, StrLen(((SHELL_PARAM_PACKAGE*)Node)->Name));\r
2234 if (TempString != NULL) {\r
2235 if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {\r
2236 FreePool(TempString);\r
2237 return (TRUE);\r
2238 }\r
2239 FreePool(TempString);\r
2240 }\r
2241 } else if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {\r
2242 return (TRUE);\r
2243 }\r
2244 }\r
2245 }\r
2246 return (FALSE);\r
2247}\r
2248/**\r
2249 Returns value from command line argument.\r
2250\r
2251 Value parameters are in the form of "-<Key> value" or "/<Key> value".\r
2252\r
2253 If CheckPackage is NULL, then return NULL.\r
2254\r
2255 @param[in] CheckPackage The package of parsed command line arguments.\r
2256 @param[in] KeyString The Key of the command line argument to check for.\r
2257\r
2258 @retval NULL The flag is not on the command line.\r
2259 @retval !=NULL The pointer to unicode string of the value.\r
2260**/\r
2261CONST CHAR16*\r
2262EFIAPI\r
2263ShellCommandLineGetValue (\r
2264 IN CONST LIST_ENTRY *CheckPackage,\r
2265 IN CHAR16 *KeyString\r
2266 )\r
2267{\r
2268 LIST_ENTRY *Node;\r
2269 CHAR16 *TempString;\r
2270\r
2271 //\r
2272 // check for CheckPackage == NULL\r
2273 //\r
2274 if (CheckPackage == NULL) {\r
2275 return (NULL);\r
2276 }\r
2277\r
2278 //\r
2279 // enumerate through the list of parametrs\r
2280 //\r
2281 for ( Node = GetFirstNode(CheckPackage)\r
2282 ; !IsNull (CheckPackage, Node)\r
2283 ; Node = GetNextNode(CheckPackage, Node)\r
2284 ){\r
2285 //\r
2286 // If the Name matches, return TRUE (and there may be NULL name)\r
2287 //\r
2288 if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {\r
2289 //\r
2290 // If Type is TypeStart then only compare the begining of the strings\r
2291 //\r
2292 if (((SHELL_PARAM_PACKAGE*)Node)->Type == TypeStart) {\r
2293 if (StrnCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name, StrLen(KeyString)) == 0) {\r
2294 return (((SHELL_PARAM_PACKAGE*)Node)->Name + StrLen(KeyString));\r
2295 }\r
2296 TempString = NULL;\r
2297 TempString = StrnCatGrow(&TempString, NULL, KeyString, StrLen(((SHELL_PARAM_PACKAGE*)Node)->Name));\r
2298 if (TempString != NULL) {\r
2299 if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {\r
2300 FreePool(TempString);\r
2301 return (((SHELL_PARAM_PACKAGE*)Node)->Name + StrLen(KeyString));\r
2302 }\r
2303 FreePool(TempString);\r
2304 }\r
2305 } else if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {\r
2306 return (((SHELL_PARAM_PACKAGE*)Node)->Value);\r
2307 }\r
2308 }\r
2309 }\r
2310 return (NULL);\r
2311}\r
2312\r
2313/**\r
2314 Returns raw value from command line argument.\r
2315\r
2316 Raw value parameters are in the form of "value" in a specific position in the list.\r
2317\r
2318 If CheckPackage is NULL, then return NULL.\r
2319\r
2320 @param[in] CheckPackage The package of parsed command line arguments.\r
2321 @param[in] Position The position of the value.\r
2322\r
2323 @retval NULL The flag is not on the command line.\r
2324 @retval !=NULL The pointer to unicode string of the value.\r
2325 **/\r
2326CONST CHAR16*\r
2327EFIAPI\r
2328ShellCommandLineGetRawValue (\r
2329 IN CONST LIST_ENTRY * CONST CheckPackage,\r
2330 IN UINTN Position\r
2331 )\r
2332{\r
2333 LIST_ENTRY *Node;\r
2334\r
2335 //\r
2336 // check for CheckPackage == NULL\r
2337 //\r
2338 if (CheckPackage == NULL) {\r
2339 return (NULL);\r
2340 }\r
2341\r
2342 //\r
2343 // enumerate through the list of parametrs\r
2344 //\r
2345 for ( Node = GetFirstNode(CheckPackage)\r
2346 ; !IsNull (CheckPackage, Node)\r
2347 ; Node = GetNextNode(CheckPackage, Node)\r
2348 ){\r
2349 //\r
2350 // If the position matches, return the value\r
2351 //\r
2352 if (((SHELL_PARAM_PACKAGE*)Node)->OriginalPosition == Position) {\r
2353 return (((SHELL_PARAM_PACKAGE*)Node)->Value);\r
2354 }\r
2355 }\r
2356 return (NULL);\r
2357}\r
2358\r
2359/**\r
2360 returns the number of command line value parameters that were parsed.\r
2361\r
2362 this will not include flags.\r
2363\r
2364 @param[in] CheckPackage The package of parsed command line arguments.\r
2365\r
2366 @retval (UINTN)-1 No parsing has ocurred\r
2367 @return other The number of value parameters found\r
2368**/\r
2369UINTN\r
2370EFIAPI\r
2371ShellCommandLineGetCount(\r
2372 IN CONST LIST_ENTRY *CheckPackage\r
2373 )\r
2374{\r
2375 LIST_ENTRY *Node1;\r
2376 UINTN Count;\r
2377\r
2378 if (CheckPackage == NULL) {\r
2379 return (0);\r
2380 }\r
2381 for ( Node1 = GetFirstNode(CheckPackage), Count = 0\r
2382 ; !IsNull (CheckPackage, Node1)\r
2383 ; Node1 = GetNextNode(CheckPackage, Node1)\r
2384 ){\r
2385 if (((SHELL_PARAM_PACKAGE*)Node1)->Name == NULL) {\r
2386 Count++;\r
2387 }\r
2388 }\r
2389 return (Count);\r
2390}\r
2391\r
2392/**\r
2393 Determins if a parameter is duplicated.\r
2394\r
2395 If Param is not NULL then it will point to a callee allocated string buffer\r
2396 with the parameter value if a duplicate is found.\r
2397\r
2398 If CheckPackage is NULL, then ASSERT.\r
2399\r
2400 @param[in] CheckPackage The package of parsed command line arguments.\r
2401 @param[out] Param Upon finding one, a pointer to the duplicated parameter.\r
2402\r
2403 @retval EFI_SUCCESS No parameters were duplicated.\r
2404 @retval EFI_DEVICE_ERROR A duplicate was found.\r
2405 **/\r
2406EFI_STATUS\r
2407EFIAPI\r
2408ShellCommandLineCheckDuplicate (\r
2409 IN CONST LIST_ENTRY *CheckPackage,\r
2410 OUT CHAR16 **Param\r
2411 )\r
2412{\r
2413 LIST_ENTRY *Node1;\r
2414 LIST_ENTRY *Node2;\r
2415\r
2416 ASSERT(CheckPackage != NULL);\r
2417\r
2418 for ( Node1 = GetFirstNode(CheckPackage)\r
2419 ; !IsNull (CheckPackage, Node1)\r
2420 ; Node1 = GetNextNode(CheckPackage, Node1)\r
2421 ){\r
2422 for ( Node2 = GetNextNode(CheckPackage, Node1)\r
2423 ; !IsNull (CheckPackage, Node2)\r
2424 ; Node2 = GetNextNode(CheckPackage, Node2)\r
2425 ){\r
2426 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
2427 if (Param != NULL) {\r
2428 *Param = NULL;\r
2429 *Param = StrnCatGrow(Param, NULL, ((SHELL_PARAM_PACKAGE*)Node1)->Name, 0);\r
2430 }\r
2431 return (EFI_DEVICE_ERROR);\r
2432 }\r
2433 }\r
2434 }\r
2435 return (EFI_SUCCESS);\r
2436}\r
2437\r
2438/**\r
2439 This is a find and replace function. Upon successful return the NewString is a copy of\r
2440 SourceString with each instance of FindTarget replaced with ReplaceWith.\r
2441\r
2442 If SourceString and NewString overlap the behavior is undefined.\r
2443\r
2444 If the string would grow bigger than NewSize it will halt and return error.\r
2445\r
2446 @param[in] SourceString The string with source buffer.\r
2447 @param[in,out] NewString The string with resultant buffer.\r
2448 @param[in] NewSize The size in bytes of NewString.\r
2449 @param[in] FindTarget The string to look for.\r
2450 @param[in] ReplaceWith The string to replace FindTarget with.\r
2451 @param[in] SkipPreCarrot If TRUE will skip a FindTarget that has a '^'\r
2452 immediately before it.\r
2453 @param[in] ParameterReplacing If TRUE will add "" around items with spaces.\r
2454\r
2455 @retval EFI_INVALID_PARAMETER SourceString was NULL.\r
2456 @retval EFI_INVALID_PARAMETER NewString was NULL.\r
2457 @retval EFI_INVALID_PARAMETER FindTarget was NULL.\r
2458 @retval EFI_INVALID_PARAMETER ReplaceWith was NULL.\r
2459 @retval EFI_INVALID_PARAMETER FindTarget had length < 1.\r
2460 @retval EFI_INVALID_PARAMETER SourceString had length < 1.\r
2461 @retval EFI_BUFFER_TOO_SMALL NewSize was less than the minimum size to hold\r
2462 the new string (truncation occurred).\r
2463 @retval EFI_SUCCESS The string was successfully copied with replacement.\r
2464**/\r
2465EFI_STATUS\r
2466EFIAPI\r
2467ShellCopySearchAndReplace(\r
2468 IN CHAR16 CONST *SourceString,\r
2469 IN OUT CHAR16 *NewString,\r
2470 IN UINTN NewSize,\r
2471 IN CONST CHAR16 *FindTarget,\r
2472 IN CONST CHAR16 *ReplaceWith,\r
2473 IN CONST BOOLEAN SkipPreCarrot,\r
2474 IN CONST BOOLEAN ParameterReplacing\r
2475 )\r
2476{\r
2477 UINTN Size;\r
2478 CHAR16 *Replace;\r
2479\r
2480 if ( (SourceString == NULL)\r
2481 || (NewString == NULL)\r
2482 || (FindTarget == NULL)\r
2483 || (ReplaceWith == NULL)\r
2484 || (StrLen(FindTarget) < 1)\r
2485 || (StrLen(SourceString) < 1)\r
2486 ){\r
2487 return (EFI_INVALID_PARAMETER);\r
2488 }\r
2489 Replace = NULL;\r
2490 if (StrStr(ReplaceWith, L" ") == NULL || !ParameterReplacing) {\r
2491 Replace = StrnCatGrow(&Replace, NULL, ReplaceWith, 0);\r
2492 } else {\r
2493 Replace = AllocateZeroPool(StrSize(ReplaceWith) + 2*sizeof(CHAR16));\r
2494 UnicodeSPrint(Replace, StrSize(ReplaceWith) + 2*sizeof(CHAR16), L"\"%s\"", ReplaceWith);\r
2495 }\r
2496 if (Replace == NULL) {\r
2497 return (EFI_OUT_OF_RESOURCES);\r
2498 }\r
2499 NewString = SetMem16(NewString, NewSize, CHAR_NULL);\r
2500 while (*SourceString != CHAR_NULL) {\r
2501 //\r
2502 // if we find the FindTarget and either Skip == FALSE or Skip and we\r
2503 // dont have a carrot do a replace...\r
2504 //\r
2505 if (StrnCmp(SourceString, FindTarget, StrLen(FindTarget)) == 0\r
2506 && ((SkipPreCarrot && *(SourceString-1) != L'^') || !SkipPreCarrot)\r
2507 ){\r
2508 SourceString += StrLen(FindTarget);\r
2509 Size = StrSize(NewString);\r
2510 if ((Size + (StrLen(Replace)*sizeof(CHAR16))) > NewSize) {\r
2511 FreePool(Replace);\r
2512 return (EFI_BUFFER_TOO_SMALL);\r
2513 }\r
2514 StrCat(NewString, Replace);\r
2515 } else {\r
2516 Size = StrSize(NewString);\r
2517 if (Size + sizeof(CHAR16) > NewSize) {\r
2518 FreePool(Replace);\r
2519 return (EFI_BUFFER_TOO_SMALL);\r
2520 }\r
2521 StrnCat(NewString, SourceString, 1);\r
2522 SourceString++;\r
2523 }\r
2524 }\r
2525 FreePool(Replace);\r
2526 return (EFI_SUCCESS);\r
2527}\r
2528\r
2529/**\r
2530 Internal worker function to output a string.\r
2531\r
2532 This function will output a string to the correct StdOut.\r
2533\r
2534 @param[in] String The string to print out.\r
2535\r
2536 @retval EFI_SUCCESS The operation was sucessful.\r
2537 @retval !EFI_SUCCESS The operation failed.\r
2538**/\r
2539EFI_STATUS\r
2540EFIAPI\r
2541InternalPrintTo (\r
2542 IN CONST CHAR16 *String\r
2543 )\r
2544{\r
2545 UINTN Size;\r
2546 Size = StrSize(String) - sizeof(CHAR16);\r
2547 if (Size == 0) {\r
2548 return (EFI_SUCCESS);\r
2549 }\r
2550 if (mEfiShellParametersProtocol != NULL) {\r
2551 return (mEfiShellProtocol->WriteFile(mEfiShellParametersProtocol->StdOut, &Size, (VOID*)String));\r
2552 }\r
2553 if (mEfiShellInterface != NULL) {\r
2554 //\r
2555 // Divide in half for old shell. Must be string length not size.\r
2556 //\r
2557 Size /= 2;\r
2558 return (mEfiShellInterface->StdOut->Write(mEfiShellInterface->StdOut, &Size, (VOID*)String));\r
2559 }\r
2560 ASSERT(FALSE);\r
2561 return (EFI_UNSUPPORTED);\r
2562}\r
2563\r
2564/**\r
2565 Print at a specific location on the screen.\r
2566\r
2567 This function will move the cursor to a given screen location and print the specified string\r
2568\r
2569 If -1 is specified for either the Row or Col the current screen location for BOTH\r
2570 will be used.\r
2571\r
2572 if either Row or Col is out of range for the current console, then ASSERT\r
2573 if Format is NULL, then ASSERT\r
2574\r
2575 In addition to the standard %-based flags as supported by UefiLib Print() this supports\r
2576 the following additional flags:\r
2577 %N - Set output attribute to normal\r
2578 %H - Set output attribute to highlight\r
2579 %E - Set output attribute to error\r
2580 %B - Set output attribute to blue color\r
2581 %V - Set output attribute to green color\r
2582\r
2583 Note: The background color is controlled by the shell command cls.\r
2584\r
2585 @param[in] Col the column to print at\r
2586 @param[in] Row the row to print at\r
2587 @param[in] Format the format string\r
2588 @param[in] Marker the marker for the variable argument list\r
2589\r
2590 @return EFI_SUCCESS The operation was successful.\r
2591 @return EFI_DEVICE_ERROR The console device reported an error.\r
2592**/\r
2593EFI_STATUS\r
2594EFIAPI\r
2595InternalShellPrintWorker(\r
2596 IN INT32 Col OPTIONAL,\r
2597 IN INT32 Row OPTIONAL,\r
2598 IN CONST CHAR16 *Format,\r
2599 IN VA_LIST Marker\r
2600 )\r
2601{\r
2602 EFI_STATUS Status;\r
2603 CHAR16 *ResumeLocation;\r
2604 CHAR16 *FormatWalker;\r
2605 UINTN OriginalAttribute;\r
2606 CHAR16 *mPostReplaceFormat;\r
2607 CHAR16 *mPostReplaceFormat2;\r
2608\r
2609 mPostReplaceFormat = AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize));\r
2610 mPostReplaceFormat2 = AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize));\r
2611\r
2612 if (mPostReplaceFormat == NULL || mPostReplaceFormat2 == NULL) {\r
2613 SHELL_FREE_NON_NULL(mPostReplaceFormat);\r
2614 SHELL_FREE_NON_NULL(mPostReplaceFormat2);\r
2615 return (EFI_OUT_OF_RESOURCES);\r
2616 }\r
2617\r
2618 Status = EFI_SUCCESS;\r
2619 OriginalAttribute = gST->ConOut->Mode->Attribute;\r
2620\r
2621 //\r
2622 // Back and forth each time fixing up 1 of our flags...\r
2623 //\r
2624 Status = ShellCopySearchAndReplace(Format, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%N", L"%%N", FALSE, FALSE);\r
2625 ASSERT_EFI_ERROR(Status);\r
2626 Status = ShellCopySearchAndReplace(mPostReplaceFormat, mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), L"%E", L"%%E", FALSE, FALSE);\r
2627 ASSERT_EFI_ERROR(Status);\r
2628 Status = ShellCopySearchAndReplace(mPostReplaceFormat2, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%H", L"%%H", FALSE, FALSE);\r
2629 ASSERT_EFI_ERROR(Status);\r
2630 Status = ShellCopySearchAndReplace(mPostReplaceFormat, mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), L"%B", L"%%B", FALSE, FALSE);\r
2631 ASSERT_EFI_ERROR(Status);\r
2632 Status = ShellCopySearchAndReplace(mPostReplaceFormat2, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%V", L"%%V", FALSE, FALSE);\r
2633 ASSERT_EFI_ERROR(Status);\r
2634\r
2635 //\r
2636 // Use the last buffer from replacing to print from...\r
2637 //\r
2638 UnicodeVSPrint (mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), mPostReplaceFormat, Marker);\r
2639\r
2640 if (Col != -1 && Row != -1) {\r
2641 Status = gST->ConOut->SetCursorPosition(gST->ConOut, Col, Row);\r
2642 }\r
2643\r
2644 FormatWalker = mPostReplaceFormat2;\r
2645 while (*FormatWalker != CHAR_NULL) {\r
2646 //\r
2647 // Find the next attribute change request\r
2648 //\r
2649 ResumeLocation = StrStr(FormatWalker, L"%");\r
2650 if (ResumeLocation != NULL) {\r
2651 *ResumeLocation = CHAR_NULL;\r
2652 }\r
2653 //\r
2654 // print the current FormatWalker string\r
2655 //\r
2656 if (StrLen(FormatWalker)>0) {\r
2657 Status = InternalPrintTo(FormatWalker);\r
2658 if (EFI_ERROR(Status)) {\r
2659 break;\r
2660 }\r
2661 }\r
2662\r
2663 //\r
2664 // update the attribute\r
2665 //\r
2666 if (ResumeLocation != NULL) {\r
2667 switch (*(ResumeLocation+1)) {\r
2668 case (L'N'):\r
2669 gST->ConOut->SetAttribute(gST->ConOut, OriginalAttribute);\r
2670 break;\r
2671 case (L'E'):\r
2672 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_YELLOW, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));\r
2673 break;\r
2674 case (L'H'):\r
2675 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_WHITE, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));\r
2676 break;\r
2677 case (L'B'):\r
2678 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_BLUE, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));\r
2679 break;\r
2680 case (L'V'):\r
2681 gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_GREEN, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));\r
2682 break;\r
2683 default:\r
2684 //\r
2685 // Print a simple '%' symbol\r
2686 //\r
2687 Status = InternalPrintTo(L"%");\r
2688 if (EFI_ERROR(Status)) {\r
2689 break;\r
2690 }\r
2691 ResumeLocation = ResumeLocation - 1;\r
2692 break;\r
2693 }\r
2694 } else {\r
2695 //\r
2696 // reset to normal now...\r
2697 //\r
2698 break;\r
2699 }\r
2700\r
2701 //\r
2702 // update FormatWalker to Resume + 2 (skip the % and the indicator)\r
2703 //\r
2704 FormatWalker = ResumeLocation + 2;\r
2705 }\r
2706\r
2707 gST->ConOut->SetAttribute(gST->ConOut, OriginalAttribute);\r
2708\r
2709 SHELL_FREE_NON_NULL(mPostReplaceFormat);\r
2710 SHELL_FREE_NON_NULL(mPostReplaceFormat2);\r
2711 return (Status);\r
2712}\r
2713\r
2714/**\r
2715 Print at a specific location on the screen.\r
2716\r
2717 This function will move the cursor to a given screen location and print the specified string.\r
2718\r
2719 If -1 is specified for either the Row or Col the current screen location for BOTH\r
2720 will be used.\r
2721\r
2722 If either Row or Col is out of range for the current console, then ASSERT.\r
2723 If Format is NULL, then ASSERT.\r
2724\r
2725 In addition to the standard %-based flags as supported by UefiLib Print() this supports\r
2726 the following additional flags:\r
2727 %N - Set output attribute to normal\r
2728 %H - Set output attribute to highlight\r
2729 %E - Set output attribute to error\r
2730 %B - Set output attribute to blue color\r
2731 %V - Set output attribute to green color\r
2732\r
2733 Note: The background color is controlled by the shell command cls.\r
2734\r
2735 @param[in] Col the column to print at\r
2736 @param[in] Row the row to print at\r
2737 @param[in] Format the format string\r
2738 @param[in] ... The variable argument list.\r
2739\r
2740 @return EFI_SUCCESS The printing was successful.\r
2741 @return EFI_DEVICE_ERROR The console device reported an error.\r
2742**/\r
2743EFI_STATUS\r
2744EFIAPI\r
2745ShellPrintEx(\r
2746 IN INT32 Col OPTIONAL,\r
2747 IN INT32 Row OPTIONAL,\r
2748 IN CONST CHAR16 *Format,\r
2749 ...\r
2750 )\r
2751{\r
2752 VA_LIST Marker;\r
2753 EFI_STATUS RetVal;\r
2754 if (Format == NULL) {\r
2755 return (EFI_INVALID_PARAMETER);\r
2756 }\r
2757 VA_START (Marker, Format);\r
2758 RetVal = InternalShellPrintWorker(Col, Row, Format, Marker);\r
2759 VA_END(Marker);\r
2760 return(RetVal);\r
2761}\r
2762\r
2763/**\r
2764 Print at a specific location on the screen.\r
2765\r
2766 This function will move the cursor to a given screen location and print the specified string.\r
2767\r
2768 If -1 is specified for either the Row or Col the current screen location for BOTH\r
2769 will be used.\r
2770\r
2771 If either Row or Col is out of range for the current console, then ASSERT.\r
2772 If Format is NULL, then ASSERT.\r
2773\r
2774 In addition to the standard %-based flags as supported by UefiLib Print() this supports\r
2775 the following additional flags:\r
2776 %N - Set output attribute to normal.\r
2777 %H - Set output attribute to highlight.\r
2778 %E - Set output attribute to error.\r
2779 %B - Set output attribute to blue color.\r
2780 %V - Set output attribute to green color.\r
2781\r
2782 Note: The background color is controlled by the shell command cls.\r
2783\r
2784 @param[in] Col The column to print at.\r
2785 @param[in] Row The row to print at.\r
2786 @param[in] Language The language of the string to retrieve. If this parameter\r
2787 is NULL, then the current platform language is used.\r
2788 @param[in] HiiFormatStringId The format string Id for getting from Hii.\r
2789 @param[in] HiiFormatHandle The format string Handle for getting from Hii.\r
2790 @param[in] ... The variable argument list.\r
2791\r
2792 @return EFI_SUCCESS The printing was successful.\r
2793 @return EFI_DEVICE_ERROR The console device reported an error.\r
2794**/\r
2795EFI_STATUS\r
2796EFIAPI\r
2797ShellPrintHiiEx(\r
2798 IN INT32 Col OPTIONAL,\r
2799 IN INT32 Row OPTIONAL,\r
2800 IN CONST CHAR8 *Language OPTIONAL,\r
2801 IN CONST EFI_STRING_ID HiiFormatStringId,\r
2802 IN CONST EFI_HANDLE HiiFormatHandle,\r
2803 ...\r
2804 )\r
2805{\r
2806 VA_LIST Marker;\r
2807 CHAR16 *HiiFormatString;\r
2808 EFI_STATUS RetVal;\r
2809\r
2810 VA_START (Marker, HiiFormatHandle);\r
2811 HiiFormatString = HiiGetString(HiiFormatHandle, HiiFormatStringId, Language);\r
2812 ASSERT(HiiFormatString != NULL);\r
2813\r
2814 RetVal = InternalShellPrintWorker(Col, Row, HiiFormatString, Marker);\r
2815\r
2816 SHELL_FREE_NON_NULL(HiiFormatString);\r
2817 VA_END(Marker);\r
2818\r
2819 return (RetVal);\r
2820}\r
2821\r
2822/**\r
2823 Function to determine if a given filename represents a file or a directory.\r
2824\r
2825 @param[in] DirName Path to directory to test.\r
2826\r
2827 @retval EFI_SUCCESS The Path represents a directory\r
2828 @retval EFI_NOT_FOUND The Path does not represent a directory\r
2829 @return other The path failed to open\r
2830**/\r
2831EFI_STATUS\r
2832EFIAPI\r
2833ShellIsDirectory(\r
2834 IN CONST CHAR16 *DirName\r
2835 )\r
2836{\r
2837 EFI_STATUS Status;\r
2838 SHELL_FILE_HANDLE Handle;\r
2839 CHAR16 *TempLocation;\r
2840 CHAR16 *TempLocation2;\r
2841\r
2842 ASSERT(DirName != NULL);\r
2843\r
2844 Handle = NULL;\r
2845 TempLocation = NULL;\r
2846\r
2847 Status = ShellOpenFileByName(DirName, &Handle, EFI_FILE_MODE_READ, 0);\r
2848 if (EFI_ERROR(Status)) {\r
2849 //\r
2850 // try good logic first.\r
2851 //\r
2852 if (mEfiShellProtocol != NULL) {\r
2853 TempLocation = StrnCatGrow(&TempLocation, NULL, DirName, 0);\r
2854 TempLocation2 = StrStr(TempLocation, L":");\r
2855 if (TempLocation2 != NULL && StrLen(StrStr(TempLocation, L":")) == 2) {\r
2856 *(TempLocation2+1) = CHAR_NULL;\r
2857 }\r
2858 if (mEfiShellProtocol->GetDevicePathFromMap(TempLocation) != NULL) {\r
2859 FreePool(TempLocation);\r
2860 return (EFI_SUCCESS);\r
2861 }\r
2862 FreePool(TempLocation);\r
2863 } else {\r
2864 //\r
2865 // probably a map name?!?!!?\r
2866 //\r
2867 TempLocation = StrStr(DirName, L"\\");\r
2868 if (TempLocation != NULL && *(TempLocation+1) == CHAR_NULL) {\r
2869 return (EFI_SUCCESS);\r
2870 }\r
2871 }\r
2872 return (Status);\r
2873 }\r
2874\r
2875 if (FileHandleIsDirectory(Handle) == EFI_SUCCESS) {\r
2876 ShellCloseFile(&Handle);\r
2877 return (EFI_SUCCESS);\r
2878 }\r
2879 ShellCloseFile(&Handle);\r
2880 return (EFI_NOT_FOUND);\r
2881}\r
2882\r
2883/**\r
2884 Function to determine if a given filename represents a file.\r
2885\r
2886 @param[in] Name Path to file to test.\r
2887\r
2888 @retval EFI_SUCCESS The Path represents a file.\r
2889 @retval EFI_NOT_FOUND The Path does not represent a file.\r
2890 @retval other The path failed to open.\r
2891**/\r
2892EFI_STATUS\r
2893EFIAPI\r
2894ShellIsFile(\r
2895 IN CONST CHAR16 *Name\r
2896 )\r
2897{\r
2898 EFI_STATUS Status;\r
2899 SHELL_FILE_HANDLE Handle;\r
2900\r
2901 ASSERT(Name != NULL);\r
2902\r
2903 Handle = NULL;\r
2904\r
2905 Status = ShellOpenFileByName(Name, &Handle, EFI_FILE_MODE_READ, 0);\r
2906 if (EFI_ERROR(Status)) {\r
2907 return (Status);\r
2908 }\r
2909\r
2910 if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {\r
2911 ShellCloseFile(&Handle);\r
2912 return (EFI_SUCCESS);\r
2913 }\r
2914 ShellCloseFile(&Handle);\r
2915 return (EFI_NOT_FOUND);\r
2916}\r
2917\r
2918/**\r
2919 Function to determine if a given filename represents a file.\r
2920\r
2921 This will search the CWD and then the Path.\r
2922\r
2923 If Name is NULL, then ASSERT.\r
2924\r
2925 @param[in] Name Path to file to test.\r
2926\r
2927 @retval EFI_SUCCESS The Path represents a file.\r
2928 @retval EFI_NOT_FOUND The Path does not represent a file.\r
2929 @retval other The path failed to open.\r
2930**/\r
2931EFI_STATUS\r
2932EFIAPI\r
2933ShellIsFileInPath(\r
2934 IN CONST CHAR16 *Name\r
2935 )\r
2936{\r
2937 CHAR16 *NewName;\r
2938 EFI_STATUS Status;\r
2939\r
2940 if (!EFI_ERROR(ShellIsFile(Name))) {\r
2941 return (EFI_SUCCESS);\r
2942 }\r
2943\r
2944 NewName = ShellFindFilePath(Name);\r
2945 if (NewName == NULL) {\r
2946 return (EFI_NOT_FOUND);\r
2947 }\r
2948 Status = ShellIsFile(NewName);\r
2949 FreePool(NewName);\r
2950 return (Status);\r
2951}\r
2952\r
2953/**\r
2954 Function to determine whether a string is decimal or hex representation of a number\r
2955 and return the number converted from the string.\r
2956\r
2957 @param[in] String String representation of a number\r
2958\r
2959 @return the number\r
2960 @retval (UINTN)(-1) An error ocurred.\r
2961**/\r
2962UINTN\r
2963EFIAPI\r
2964ShellStrToUintn(\r
2965 IN CONST CHAR16 *String\r
2966 )\r
2967{\r
2968 UINT64 RetVal;\r
2969 BOOLEAN Hex;\r
2970\r
2971 Hex = FALSE;\r
2972\r
2973 if (!InternalShellIsHexOrDecimalNumber(String, Hex, TRUE)) {\r
2974 Hex = TRUE;\r
2975 }\r
2976\r
2977 if (!EFI_ERROR(ShellConvertStringToUint64(String, &RetVal, Hex, TRUE))) {\r
2978 return ((UINTN)RetVal);\r
2979 }\r
2980 return ((UINTN)(-1));\r
2981}\r
2982\r
2983/**\r
2984 Safely append with automatic string resizing given length of Destination and\r
2985 desired length of copy from Source.\r
2986\r
2987 append the first D characters of Source to the end of Destination, where D is\r
2988 the lesser of Count and the StrLen() of Source. If appending those D characters\r
2989 will fit within Destination (whose Size is given as CurrentSize) and\r
2990 still leave room for a NULL terminator, then those characters are appended,\r
2991 starting at the original terminating NULL of Destination, and a new terminating\r
2992 NULL is appended.\r
2993\r
2994 If appending D characters onto Destination will result in a overflow of the size\r
2995 given in CurrentSize the string will be grown such that the copy can be performed\r
2996 and CurrentSize will be updated to the new size.\r
2997\r
2998 If Source is NULL, there is nothing to append, just return the current buffer in\r
2999 Destination.\r
3000\r
3001 if Destination is NULL, then ASSERT()\r
3002 if Destination's current length (including NULL terminator) is already more then\r
3003 CurrentSize, then ASSERT()\r
3004\r
3005 @param[in,out] Destination The String to append onto\r
3006 @param[in,out] CurrentSize on call the number of bytes in Destination. On\r
3007 return possibly the new size (still in bytes). if NULL\r
3008 then allocate whatever is needed.\r
3009 @param[in] Source The String to append from\r
3010 @param[in] Count Maximum number of characters to append. if 0 then\r
3011 all are appended.\r
3012\r
3013 @return Destination return the resultant string.\r
3014**/\r
3015CHAR16*\r
3016EFIAPI\r
3017StrnCatGrow (\r
3018 IN OUT CHAR16 **Destination,\r
3019 IN OUT UINTN *CurrentSize,\r
3020 IN CONST CHAR16 *Source,\r
3021 IN UINTN Count\r
3022 )\r
3023{\r
3024 UINTN DestinationStartSize;\r
3025 UINTN NewSize;\r
3026\r
3027 //\r
3028 // ASSERTs\r
3029 //\r
3030 ASSERT(Destination != NULL);\r
3031\r
3032 //\r
3033 // If there's nothing to do then just return Destination\r
3034 //\r
3035 if (Source == NULL) {\r
3036 return (*Destination);\r
3037 }\r
3038\r
3039 //\r
3040 // allow for un-initialized pointers, based on size being 0\r
3041 //\r
3042 if (CurrentSize != NULL && *CurrentSize == 0) {\r
3043 *Destination = NULL;\r
3044 }\r
3045\r
3046 //\r
3047 // allow for NULL pointers address as Destination\r
3048 //\r
3049 if (*Destination != NULL) {\r
3050 ASSERT(CurrentSize != 0);\r
3051 DestinationStartSize = StrSize(*Destination);\r
3052 ASSERT(DestinationStartSize <= *CurrentSize);\r
3053 } else {\r
3054 DestinationStartSize = 0;\r
3055// ASSERT(*CurrentSize == 0);\r
3056 }\r
3057\r
3058 //\r
3059 // Append all of Source?\r
3060 //\r
3061 if (Count == 0) {\r
3062 Count = StrLen(Source);\r
3063 }\r
3064\r
3065 //\r
3066 // Test and grow if required\r
3067 //\r
3068 if (CurrentSize != NULL) {\r
3069 NewSize = *CurrentSize;\r
3070 while (NewSize < (DestinationStartSize + (Count*sizeof(CHAR16)))) {\r
3071 NewSize += 2 * Count * sizeof(CHAR16);\r
3072 }\r
3073 *Destination = ReallocatePool(*CurrentSize, NewSize, *Destination);\r
3074 ASSERT(*Destination != NULL);\r
3075 *CurrentSize = NewSize;\r
3076 } else {\r
3077 *Destination = AllocateZeroPool((Count+1)*sizeof(CHAR16));\r
3078 ASSERT(*Destination != NULL);\r
3079 }\r
3080\r
3081 //\r
3082 // Now use standard StrnCat on a big enough buffer\r
3083 //\r
3084 if (*Destination == NULL) {\r
3085 return (NULL);\r
3086 }\r
3087 return StrnCat(*Destination, Source, Count);\r
3088}\r
3089\r
3090/**\r
3091 Prompt the user and return the resultant answer to the requestor.\r
3092\r
3093 This function will display the requested question on the shell prompt and then\r
3094 wait for an apropriate answer to be input from the console.\r
3095\r
3096 if the SHELL_PROMPT_REQUEST_TYPE is SHELL_PROMPT_REQUEST_TYPE_YESNO, ShellPromptResponseTypeQuitContinue\r
3097 or SHELL_PROMPT_REQUEST_TYPE_YESNOCANCEL then *Response is of type SHELL_PROMPT_RESPONSE.\r
3098\r
3099 if the SHELL_PROMPT_REQUEST_TYPE is ShellPromptResponseTypeFreeform then *Response is of type\r
3100 CHAR16*.\r
3101\r
3102 In either case *Response must be callee freed if Response was not NULL;\r
3103\r
3104 @param Type What type of question is asked. This is used to filter the input\r
3105 to prevent invalid answers to question.\r
3106 @param Prompt Pointer to string prompt to use to request input.\r
3107 @param Response Pointer to Response which will be populated upon return.\r
3108\r
3109 @retval EFI_SUCCESS The operation was sucessful.\r
3110 @retval EFI_UNSUPPORTED The operation is not supported as requested.\r
3111 @retval EFI_INVALID_PARAMETER A parameter was invalid.\r
3112 @return other The operation failed.\r
3113**/\r
3114EFI_STATUS\r
3115EFIAPI\r
3116ShellPromptForResponse (\r
3117 IN SHELL_PROMPT_REQUEST_TYPE Type,\r
3118 IN CHAR16 *Prompt OPTIONAL,\r
3119 IN OUT VOID **Response OPTIONAL\r
3120 )\r
3121{\r
3122 EFI_STATUS Status;\r
3123 EFI_INPUT_KEY Key;\r
3124 UINTN EventIndex;\r
3125 SHELL_PROMPT_RESPONSE *Resp;\r
3126 UINTN Size;\r
3127 CHAR16 *Buffer;\r
3128\r
3129 Status = EFI_UNSUPPORTED;\r
3130 Resp = NULL;\r
3131 Buffer = NULL;\r
3132 Size = 0;\r
3133 if (Type != ShellPromptResponseTypeFreeform) {\r
3134 Resp = (SHELL_PROMPT_RESPONSE*)AllocateZeroPool(sizeof(SHELL_PROMPT_RESPONSE));\r
3135 if (Resp == NULL) {\r
3136 return (EFI_OUT_OF_RESOURCES);\r
3137 }\r
3138 }\r
3139\r
3140 switch(Type) {\r
3141 case ShellPromptResponseTypeQuitContinue:\r
3142 if (Prompt != NULL) {\r
3143 ShellPrintEx(-1, -1, L"%s", Prompt);\r
3144 }\r
3145 //\r
3146 // wait for valid response\r
3147 //\r
3148 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);\r
3149 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);\r
3150 ASSERT_EFI_ERROR(Status);\r
3151 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);\r
3152 if (Key.UnicodeChar == L'Q' || Key.UnicodeChar ==L'q') {\r
3153 *Resp = ShellPromptResponseQuit;\r
3154 } else {\r
3155 *Resp = ShellPromptResponseContinue;\r
3156 }\r
3157 break;\r
3158 case ShellPromptResponseTypeYesNoCancel:\r
3159 if (Prompt != NULL) {\r
3160 ShellPrintEx(-1, -1, L"%s", Prompt);\r
3161 }\r
3162 //\r
3163 // wait for valid response\r
3164 //\r
3165 *Resp = ShellPromptResponseMax;\r
3166 while (*Resp == ShellPromptResponseMax) {\r
3167 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);\r
3168 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);\r
3169 ASSERT_EFI_ERROR(Status);\r
3170 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);\r
3171 switch (Key.UnicodeChar) {\r
3172 case L'Y':\r
3173 case L'y':\r
3174 *Resp = ShellPromptResponseYes;\r
3175 break;\r
3176 case L'N':\r
3177 case L'n':\r
3178 *Resp = ShellPromptResponseNo;\r
3179 break;\r
3180 case L'C':\r
3181 case L'c':\r
3182 *Resp = ShellPromptResponseCancel;\r
3183 break;\r
3184 }\r
3185 }\r
3186 break; case ShellPromptResponseTypeYesNoAllCancel:\r
3187 if (Prompt != NULL) {\r
3188 ShellPrintEx(-1, -1, L"%s", Prompt);\r
3189 }\r
3190 //\r
3191 // wait for valid response\r
3192 //\r
3193 *Resp = ShellPromptResponseMax;\r
3194 while (*Resp == ShellPromptResponseMax) {\r
3195 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);\r
3196 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);\r
3197 ASSERT_EFI_ERROR(Status);\r
3198 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);\r
3199 switch (Key.UnicodeChar) {\r
3200 case L'Y':\r
3201 case L'y':\r
3202 *Resp = ShellPromptResponseYes;\r
3203 break;\r
3204 case L'N':\r
3205 case L'n':\r
3206 *Resp = ShellPromptResponseNo;\r
3207 break;\r
3208 case L'A':\r
3209 case L'a':\r
3210 *Resp = ShellPromptResponseAll;\r
3211 break;\r
3212 case L'C':\r
3213 case L'c':\r
3214 *Resp = ShellPromptResponseCancel;\r
3215 break;\r
3216 }\r
3217 }\r
3218 break;\r
3219 case ShellPromptResponseTypeEnterContinue:\r
3220 case ShellPromptResponseTypeAnyKeyContinue:\r
3221 if (Prompt != NULL) {\r
3222 ShellPrintEx(-1, -1, L"%s", Prompt);\r
3223 }\r
3224 //\r
3225 // wait for valid response\r
3226 //\r
3227 *Resp = ShellPromptResponseMax;\r
3228 while (*Resp == ShellPromptResponseMax) {\r
3229 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);\r
3230 if (Type == ShellPromptResponseTypeEnterContinue) {\r
3231 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);\r
3232 ASSERT_EFI_ERROR(Status);\r
3233 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);\r
3234 if (Key.UnicodeChar == CHAR_CARRIAGE_RETURN) {\r
3235 *Resp = ShellPromptResponseContinue;\r
3236 break;\r
3237 }\r
3238 }\r
3239 if (Type == ShellPromptResponseTypeAnyKeyContinue) {\r
3240 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);\r
3241 ASSERT_EFI_ERROR(Status);\r
3242 *Resp = ShellPromptResponseContinue;\r
3243 break;\r
3244 }\r
3245 }\r
3246 break;\r
3247 case ShellPromptResponseTypeYesNo:\r
3248 if (Prompt != NULL) {\r
3249 ShellPrintEx(-1, -1, L"%s", Prompt);\r
3250 }\r
3251 //\r
3252 // wait for valid response\r
3253 //\r
3254 *Resp = ShellPromptResponseMax;\r
3255 while (*Resp == ShellPromptResponseMax) {\r
3256 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);\r
3257 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);\r
3258 ASSERT_EFI_ERROR(Status);\r
3259 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);\r
3260 switch (Key.UnicodeChar) {\r
3261 case L'Y':\r
3262 case L'y':\r
3263 *Resp = ShellPromptResponseYes;\r
3264 break;\r
3265 case L'N':\r
3266 case L'n':\r
3267 *Resp = ShellPromptResponseNo;\r
3268 break;\r
3269 }\r
3270 }\r
3271 break;\r
3272 case ShellPromptResponseTypeFreeform:\r
3273 if (Prompt != NULL) {\r
3274 ShellPrintEx(-1, -1, L"%s", Prompt);\r
3275 }\r
3276 while(1) {\r
3277 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);\r
3278 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);\r
3279 ASSERT_EFI_ERROR(Status);\r
3280 ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);\r
3281 if (Key.UnicodeChar == CHAR_CARRIAGE_RETURN) {\r
3282 break;\r
3283 }\r
3284 ASSERT((Buffer == NULL && Size == 0) || (Buffer != NULL));\r
3285 StrnCatGrow(&Buffer, &Size, &Key.UnicodeChar, 1);\r
3286 }\r
3287 break;\r
3288 //\r
3289 // This is the location to add new prompt types.\r
3290 //\r
3291 default:\r
3292 ASSERT(FALSE);\r
3293 }\r
3294\r
3295 if (Response != NULL) {\r
3296 if (Resp != NULL) {\r
3297 *Response = Resp;\r
3298 } else if (Buffer != NULL) {\r
3299 *Response = Buffer;\r
3300 }\r
3301 } else {\r
3302 if (Resp != NULL) {\r
3303 FreePool(Resp);\r
3304 }\r
3305 if (Buffer != NULL) {\r
3306 FreePool(Buffer);\r
3307 }\r
3308 }\r
3309\r
3310 ShellPrintEx(-1, -1, L"\r\n");\r
3311 return (Status);\r
3312}\r
3313\r
3314/**\r
3315 Prompt the user and return the resultant answer to the requestor.\r
3316\r
3317 This function is the same as ShellPromptForResponse, except that the prompt is\r
3318 automatically pulled from HII.\r
3319\r
3320 @param Type What type of question is asked. This is used to filter the input\r
3321 to prevent invalid answers to question.\r
3322 @param[in] HiiFormatStringId The format string Id for getting from Hii.\r
3323 @param[in] HiiFormatHandle The format string Handle for getting from Hii.\r
3324 @param Response Pointer to Response which will be populated upon return.\r
3325\r
3326 @retval EFI_SUCCESS the operation was sucessful.\r
3327 @return other the operation failed.\r
3328\r
3329 @sa ShellPromptForResponse\r
3330**/\r
3331EFI_STATUS\r
3332EFIAPI\r
3333ShellPromptForResponseHii (\r
3334 IN SHELL_PROMPT_REQUEST_TYPE Type,\r
3335 IN CONST EFI_STRING_ID HiiFormatStringId,\r
3336 IN CONST EFI_HANDLE HiiFormatHandle,\r
3337 IN OUT VOID **Response\r
3338 )\r
3339{\r
3340 CHAR16 *Prompt;\r
3341 EFI_STATUS Status;\r
3342\r
3343 Prompt = HiiGetString(HiiFormatHandle, HiiFormatStringId, NULL);\r
3344 Status = ShellPromptForResponse(Type, Prompt, Response);\r
3345 FreePool(Prompt);\r
3346 return (Status);\r
3347}\r
3348\r
3349/**\r
3350 Function to determin if an entire string is a valid number.\r
3351\r
3352 If Hex it must be preceeded with a 0x or has ForceHex, set TRUE.\r
3353\r
3354 @param[in] String The string to evaluate.\r
3355 @param[in] ForceHex TRUE - always assume hex.\r
3356 @param[in] StopAtSpace TRUE to halt upon finding a space, FALSE to keep going.\r
3357\r
3358 @retval TRUE It is all numeric (dec/hex) characters.\r
3359 @retval FALSE There is a non-numeric character.\r
3360**/\r
3361BOOLEAN\r
3362EFIAPI\r
3363InternalShellIsHexOrDecimalNumber (\r
3364 IN CONST CHAR16 *String,\r
3365 IN CONST BOOLEAN ForceHex,\r
3366 IN CONST BOOLEAN StopAtSpace\r
3367 )\r
3368{\r
3369 BOOLEAN Hex;\r
3370\r
3371 //\r
3372 // chop off a single negative sign\r
3373 //\r
3374 if (String != NULL && *String == L'-') {\r
3375 String++;\r
3376 }\r
3377 \r
3378 if (String == NULL) {\r
3379 return (FALSE);\r
3380 }\r
3381\r
3382 //\r
3383 // chop leading zeroes\r
3384 //\r
3385 while(String != NULL && *String == L'0'){\r
3386 String++;\r
3387 }\r
3388 //\r
3389 // allow '0x' or '0X', but not 'x' or 'X'\r
3390 //\r
3391 if (String != NULL && (*String == L'x' || *String == L'X')) {\r
3392 if (*(String-1) != L'0') {\r
3393 //\r
3394 // we got an x without a preceeding 0\r
3395 //\r
3396 return (FALSE);\r
3397 }\r
3398 String++;\r
3399 Hex = TRUE;\r
3400 } else if (ForceHex) {\r
3401 Hex = TRUE;\r
3402 } else {\r
3403 Hex = FALSE;\r
3404 }\r
3405\r
3406 //\r
3407 // loop through the remaining characters and use the lib function\r
3408 //\r
3409 for ( ; String != NULL && *String != CHAR_NULL && !(StopAtSpace && *String == L' ') ; String++){\r
3410 if (Hex) {\r
3411 if (!ShellIsHexaDecimalDigitCharacter(*String)) {\r
3412 return (FALSE);\r
3413 }\r
3414 } else {\r
3415 if (!ShellIsDecimalDigitCharacter(*String)) {\r
3416 return (FALSE);\r
3417 }\r
3418 }\r
3419 }\r
3420\r
3421 return (TRUE);\r
3422}\r
3423\r
3424/**\r
3425 Function to determine if a given filename exists.\r
3426\r
3427 @param[in] Name Path to test.\r
3428\r
3429 @retval EFI_SUCCESS The Path represents a file.\r
3430 @retval EFI_NOT_FOUND The Path does not represent a file.\r
3431 @retval other The path failed to open.\r
3432**/\r
3433EFI_STATUS\r
3434EFIAPI\r
3435ShellFileExists(\r
3436 IN CONST CHAR16 *Name\r
3437 )\r
3438{\r
3439 EFI_STATUS Status;\r
3440 EFI_SHELL_FILE_INFO *List;\r
3441\r
3442 ASSERT(Name != NULL);\r
3443\r
3444 List = NULL;\r
3445 Status = ShellOpenFileMetaArg((CHAR16*)Name, EFI_FILE_MODE_READ, &List);\r
3446 if (EFI_ERROR(Status)) {\r
3447 return (Status);\r
3448 }\r
3449\r
3450 ShellCloseFileMetaArg(&List);\r
3451\r
3452 return (EFI_SUCCESS);\r
3453}\r
3454\r
3455/**\r
3456 Convert a Unicode character to upper case only if \r
3457 it maps to a valid small-case ASCII character.\r
3458\r
3459 This internal function only deal with Unicode character\r
3460 which maps to a valid small-case ASCII character, i.e.\r
3461 L'a' to L'z'. For other Unicode character, the input character\r
3462 is returned directly.\r
3463\r
3464 @param Char The character to convert.\r
3465\r
3466 @retval LowerCharacter If the Char is with range L'a' to L'z'.\r
3467 @retval Unchanged Otherwise.\r
3468\r
3469**/\r
3470CHAR16\r
3471EFIAPI\r
3472InternalShellCharToUpper (\r
3473 IN CHAR16 Char\r
3474 )\r
3475{\r
3476 if (Char >= L'a' && Char <= L'z') {\r
3477 return (CHAR16) (Char - (L'a' - L'A'));\r
3478 }\r
3479\r
3480 return Char;\r
3481}\r
3482\r
3483/**\r
3484 Convert a Unicode character to numerical value.\r
3485\r
3486 This internal function only deal with Unicode character\r
3487 which maps to a valid hexadecimal ASII character, i.e.\r
3488 L'0' to L'9', L'a' to L'f' or L'A' to L'F'. For other \r
3489 Unicode character, the value returned does not make sense.\r
3490\r
3491 @param Char The character to convert.\r
3492\r
3493 @return The numerical value converted.\r
3494\r
3495**/\r
3496UINTN\r
3497EFIAPI\r
3498InternalShellHexCharToUintn (\r
3499 IN CHAR16 Char\r
3500 )\r
3501{\r
3502 if (ShellIsDecimalDigitCharacter (Char)) {\r
3503 return Char - L'0';\r
3504 }\r
3505\r
3506 return (UINTN) (10 + InternalShellCharToUpper (Char) - L'A');\r
3507}\r
3508\r
3509/**\r
3510 Convert a Null-terminated Unicode hexadecimal string to a value of type UINT64.\r
3511\r
3512 This function returns a value of type UINTN by interpreting the contents\r
3513 of the Unicode string specified by String as a hexadecimal number.\r
3514 The format of the input Unicode string String is:\r
3515\r
3516 [spaces][zeros][x][hexadecimal digits].\r
3517\r
3518 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].\r
3519 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix.\r
3520 If "x" appears in the input string, it must be prefixed with at least one 0.\r
3521 The function will ignore the pad space, which includes spaces or tab characters,\r
3522 before [zeros], [x] or [hexadecimal digit]. The running zero before [x] or\r
3523 [hexadecimal digit] will be ignored. Then, the decoding starts after [x] or the\r
3524 first valid hexadecimal digit. Then, the function stops at the first character that is\r
3525 a not a valid hexadecimal character or NULL, whichever one comes first.\r
3526\r
3527 If String has only pad spaces, then zero is returned.\r
3528 If String has no leading pad spaces, leading zeros or valid hexadecimal digits,\r
3529 then zero is returned.\r
3530\r
3531 @param[in] String A pointer to a Null-terminated Unicode string.\r
3532 @param[out] Value Upon a successful return the value of the conversion.\r
3533 @param[in] StopAtSpace FALSE to skip spaces.\r
3534\r
3535 @retval EFI_SUCCESS The conversion was successful.\r
3536 @retval EFI_INVALID_PARAMETER A parameter was NULL or invalid.\r
3537 @retval EFI_DEVICE_ERROR An overflow occured.\r
3538**/\r
3539EFI_STATUS\r
3540EFIAPI\r
3541InternalShellStrHexToUint64 (\r
3542 IN CONST CHAR16 *String,\r
3543 OUT UINT64 *Value,\r
3544 IN CONST BOOLEAN StopAtSpace\r
3545 )\r
3546{\r
3547 UINT64 Result;\r
3548\r
3549 if (String == NULL || StrSize(String) == 0 || Value == NULL) {\r
3550 return (EFI_INVALID_PARAMETER);\r
3551 }\r
3552 \r
3553 //\r
3554 // Ignore the pad spaces (space or tab) \r
3555 //\r
3556 while ((*String == L' ') || (*String == L'\t')) {\r
3557 String++;\r
3558 }\r
3559\r
3560 //\r
3561 // Ignore leading Zeros after the spaces\r
3562 //\r
3563 while (*String == L'0') {\r
3564 String++;\r
3565 }\r
3566\r
3567 if (InternalShellCharToUpper (*String) == L'X') {\r
3568 if (*(String - 1) != L'0') {\r
3569 return 0;\r
3570 }\r
3571 //\r
3572 // Skip the 'X'\r
3573 //\r
3574 String++;\r
3575 }\r
3576\r
3577 Result = 0;\r
3578 \r
3579 //\r
3580 // Skip spaces if requested\r
3581 //\r
3582 while (StopAtSpace && *String == L' ') {\r
3583 String++;\r
3584 }\r
3585 \r
3586 while (ShellIsHexaDecimalDigitCharacter (*String)) {\r
3587 //\r
3588 // If the Hex Number represented by String overflows according \r
3589 // to the range defined by UINTN, then ASSERT().\r
3590 //\r
3591 if (!(Result <= (RShiftU64((((UINT64) ~0) - InternalShellHexCharToUintn (*String)), 4)))) {\r
3592// if (!(Result <= ((((UINT64) ~0) - InternalShellHexCharToUintn (*String)) >> 4))) {\r
3593 return (EFI_DEVICE_ERROR);\r
3594 }\r
3595\r
3596 Result = (LShiftU64(Result, 4));\r
3597 Result += InternalShellHexCharToUintn (*String);\r
3598 String++;\r
3599\r
3600 //\r
3601 // Skip spaces if requested\r
3602 //\r
3603 while (StopAtSpace && *String == L' ') {\r
3604 String++;\r
3605 }\r
3606 }\r
3607\r
3608 *Value = Result;\r
3609 return (EFI_SUCCESS);\r
3610}\r
3611\r
3612/**\r
3613 Convert a Null-terminated Unicode decimal string to a value of\r
3614 type UINT64.\r
3615\r
3616 This function returns a value of type UINT64 by interpreting the contents\r
3617 of the Unicode string specified by String as a decimal number. The format\r
3618 of the input Unicode string String is:\r
3619\r
3620 [spaces] [decimal digits].\r
3621\r
3622 The valid decimal digit character is in the range [0-9]. The\r
3623 function will ignore the pad space, which includes spaces or\r
3624 tab characters, before [decimal digits]. The running zero in the\r
3625 beginning of [decimal digits] will be ignored. Then, the function\r
3626 stops at the first character that is a not a valid decimal character\r
3627 or a Null-terminator, whichever one comes first.\r
3628\r
3629 If String has only pad spaces, then 0 is returned.\r
3630 If String has no pad spaces or valid decimal digits,\r
3631 then 0 is returned.\r
3632\r
3633 @param[in] String A pointer to a Null-terminated Unicode string.\r
3634 @param[out] Value Upon a successful return the value of the conversion.\r
3635 @param[in] StopAtSpace FALSE to skip spaces.\r
3636\r
3637 @retval EFI_SUCCESS The conversion was successful.\r
3638 @retval EFI_INVALID_PARAMETER A parameter was NULL or invalid.\r
3639 @retval EFI_DEVICE_ERROR An overflow occured.\r
3640**/\r
3641EFI_STATUS\r
3642EFIAPI\r
3643InternalShellStrDecimalToUint64 (\r
3644 IN CONST CHAR16 *String,\r
3645 OUT UINT64 *Value,\r
3646 IN CONST BOOLEAN StopAtSpace\r
3647 )\r
3648{\r
3649 UINT64 Result;\r
3650\r
3651 if (String == NULL || StrSize (String) == 0 || Value == NULL) {\r
3652 return (EFI_INVALID_PARAMETER);\r
3653 }\r
3654\r
3655 //\r
3656 // Ignore the pad spaces (space or tab)\r
3657 //\r
3658 while ((*String == L' ') || (*String == L'\t')) {\r
3659 String++;\r
3660 }\r
3661\r
3662 //\r
3663 // Ignore leading Zeros after the spaces\r
3664 //\r
3665 while (*String == L'0') {\r
3666 String++;\r
3667 }\r
3668\r
3669 Result = 0;\r
3670\r
3671 //\r
3672 // Skip spaces if requested\r
3673 //\r
3674 while (StopAtSpace && *String == L' ') {\r
3675 String++;\r
3676 }\r
3677 while (ShellIsDecimalDigitCharacter (*String)) {\r
3678 //\r
3679 // If the number represented by String overflows according \r
3680 // to the range defined by UINT64, then ASSERT().\r
3681 //\r
3682 \r
3683 if (!(Result <= (DivU64x32((((UINT64) ~0) - (*String - L'0')),10)))) {\r
3684 return (EFI_DEVICE_ERROR);\r
3685 }\r
3686\r
3687 Result = MultU64x32(Result, 10) + (*String - L'0');\r
3688 String++;\r
3689\r
3690 //\r
3691 // Stop at spaces if requested\r
3692 //\r
3693 if (StopAtSpace && *String == L' ') {\r
3694 break;\r
3695 }\r
3696 }\r
3697\r
3698 *Value = Result;\r
3699 \r
3700 return (EFI_SUCCESS);\r
3701}\r
3702\r
3703/**\r
3704 Function to verify and convert a string to its numerical value.\r
3705\r
3706 If Hex it must be preceeded with a 0x, 0X, or has ForceHex set TRUE.\r
3707\r
3708 @param[in] String The string to evaluate.\r
3709 @param[out] Value Upon a successful return the value of the conversion.\r
3710 @param[in] ForceHex TRUE - always assume hex.\r
3711 @param[in] StopAtSpace FALSE to skip spaces.\r
3712 \r
3713 @retval EFI_SUCCESS The conversion was successful.\r
3714 @retval EFI_INVALID_PARAMETER String contained an invalid character.\r
3715 @retval EFI_NOT_FOUND String was a number, but Value was NULL.\r
3716**/\r
3717EFI_STATUS\r
3718EFIAPI\r
3719ShellConvertStringToUint64(\r
3720 IN CONST CHAR16 *String,\r
3721 OUT UINT64 *Value,\r
3722 IN CONST BOOLEAN ForceHex,\r
3723 IN CONST BOOLEAN StopAtSpace\r
3724 )\r
3725{\r
3726 UINT64 RetVal;\r
3727 CONST CHAR16 *Walker;\r
3728 EFI_STATUS Status;\r
3729 BOOLEAN Hex;\r
3730\r
3731 Hex = ForceHex;\r
3732\r
3733 if (!InternalShellIsHexOrDecimalNumber(String, Hex, StopAtSpace)) {\r
3734 if (!Hex) {\r
3735 Hex = TRUE;\r
3736 if (!InternalShellIsHexOrDecimalNumber(String, Hex, StopAtSpace)) {\r
3737 return (EFI_INVALID_PARAMETER);\r
3738 }\r
3739 } else {\r
3740 return (EFI_INVALID_PARAMETER);\r
3741 }\r
3742 }\r
3743\r
3744 //\r
3745 // Chop off leading spaces\r
3746 //\r
3747 for (Walker = String; Walker != NULL && *Walker != CHAR_NULL && *Walker == L' '; Walker++);\r
3748\r
3749 //\r
3750 // make sure we have something left that is numeric.\r
3751 //\r
3752 if (Walker == NULL || *Walker == CHAR_NULL || !InternalShellIsHexOrDecimalNumber(Walker, Hex, StopAtSpace)) {\r
3753 return (EFI_INVALID_PARAMETER);\r
3754 } \r
3755\r
3756 //\r
3757 // do the conversion.\r
3758 //\r
3759 if (Hex || StrnCmp(Walker, L"0x", 2) == 0 || StrnCmp(Walker, L"0X", 2) == 0){\r
3760 Status = InternalShellStrHexToUint64(Walker, &RetVal, StopAtSpace);\r
3761 } else {\r
3762 Status = InternalShellStrDecimalToUint64(Walker, &RetVal, StopAtSpace);\r
3763 }\r
3764\r
3765 if (Value == NULL && !EFI_ERROR(Status)) {\r
3766 return (EFI_NOT_FOUND);\r
3767 }\r
3768\r
3769 if (Value != NULL) {\r
3770 *Value = RetVal;\r
3771 }\r
3772\r
3773 return (Status);\r
3774}\r
3775\r
3776/**\r
3777 Function to determin if an entire string is a valid number.\r
3778\r
3779 If Hex it must be preceeded with a 0x or has ForceHex, set TRUE.\r
3780\r
3781 @param[in] String The string to evaluate.\r
3782 @param[in] ForceHex TRUE - always assume hex.\r
3783 @param[in] StopAtSpace TRUE to halt upon finding a space, FALSE to keep going.\r
3784\r
3785 @retval TRUE It is all numeric (dec/hex) characters.\r
3786 @retval FALSE There is a non-numeric character.\r
3787**/\r
3788BOOLEAN\r
3789EFIAPI\r
3790ShellIsHexOrDecimalNumber (\r
3791 IN CONST CHAR16 *String,\r
3792 IN CONST BOOLEAN ForceHex,\r
3793 IN CONST BOOLEAN StopAtSpace\r
3794 )\r
3795{\r
3796 if (ShellConvertStringToUint64(String, NULL, ForceHex, StopAtSpace) == EFI_NOT_FOUND) {\r
3797 return (TRUE);\r
3798 }\r
3799 return (FALSE);\r
3800}\r