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