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