94b17fa1 |
1 | /** @file\r |
2 | Provides interface to shell functionality for shell commands and applications.\r |
3 | \r |
1e6e84c7 |
4 | Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>\r |
5 | This program and the accompanying materials\r |
b3011f40 |
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 |
94b17fa1 |
9 | \r |
b3011f40 |
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 |
94b17fa1 |
12 | \r |
13 | **/\r |
14 | \r |
b1f95a06 |
15 | #include "UefiShellLib.h"\r |
d2b4564b |
16 | \r |
94b17fa1 |
17 | #define MAX_FILE_NAME_LEN 522 // (20 * (6+5+2))+1) unicode characters from EFI FAT spec (doubled for bytes)\r |
18 | #define FIND_XXXXX_FILE_BUFFER_SIZE (SIZE_OF_EFI_FILE_INFO + MAX_FILE_NAME_LEN)\r |
19 | \r |
d2b4564b |
20 | //\r |
21 | // This is not static since it's extern in the .h file\r |
22 | //\r |
23 | SHELL_PARAM_ITEM EmptyParamList[] = {\r |
24 | {NULL, TypeMax}\r |
25 | };\r |
26 | \r |
27 | //\r |
28 | // Static file globals for the shell library\r |
29 | //\r |
30 | STATIC EFI_SHELL_ENVIRONMENT2 *mEfiShellEnvironment2;\r |
31 | STATIC EFI_SHELL_INTERFACE *mEfiShellInterface;\r |
32 | STATIC EFI_SHELL_PROTOCOL *mEfiShellProtocol;\r |
33 | STATIC EFI_SHELL_PARAMETERS_PROTOCOL *mEfiShellParametersProtocol;\r |
34 | STATIC EFI_HANDLE mEfiShellEnvironment2Handle;\r |
35 | STATIC FILE_HANDLE_FUNCTION_MAP FileFunctionMap;\r |
2247dde4 |
36 | STATIC UINTN mTotalParameterCount;\r |
ecd3d59f |
37 | STATIC CHAR16 *mPostReplaceFormat;\r |
38 | STATIC CHAR16 *mPostReplaceFormat2;\r |
b3011f40 |
39 | \r |
2247dde4 |
40 | /**\r |
41 | Check if a Unicode character is a hexadecimal character.\r |
42 | \r |
1e6e84c7 |
43 | This internal function checks if a Unicode character is a\r |
44 | decimal character. The valid hexadecimal character is\r |
2247dde4 |
45 | L'0' to L'9', L'a' to L'f', or L'A' to L'F'.\r |
46 | \r |
47 | \r |
48 | @param Char The character to check against.\r |
49 | \r |
50 | @retval TRUE If the Char is a hexadecmial character.\r |
51 | @retval FALSE If the Char is not a hexadecmial character.\r |
52 | \r |
53 | **/\r |
54 | BOOLEAN\r |
55 | EFIAPI\r |
969c783b |
56 | ShellIsHexaDecimalDigitCharacter (\r |
2247dde4 |
57 | IN CHAR16 Char\r |
58 | ) {\r |
59 | return (BOOLEAN) ((Char >= L'0' && Char <= L'9') || (Char >= L'A' && Char <= L'F') || (Char >= L'a' && Char <= L'f'));\r |
60 | }\r |
94b17fa1 |
61 | \r |
62 | /**\r |
63 | helper function to find ShellEnvironment2 for constructor\r |
64 | **/\r |
65 | EFI_STATUS\r |
66 | EFIAPI\r |
67 | ShellFindSE2 (\r |
68 | IN EFI_HANDLE ImageHandle\r |
2247dde4 |
69 | ) {\r |
94b17fa1 |
70 | EFI_STATUS Status;\r |
71 | EFI_HANDLE *Buffer;\r |
72 | UINTN BufferSize;\r |
73 | UINTN HandleIndex;\r |
74 | \r |
75 | BufferSize = 0;\r |
76 | Buffer = NULL;\r |
1e6e84c7 |
77 | Status = gBS->OpenProtocol(ImageHandle,\r |
94b17fa1 |
78 | &gEfiShellEnvironment2Guid,\r |
79 | (VOID **)&mEfiShellEnvironment2,\r |
80 | ImageHandle,\r |
81 | NULL,\r |
82 | EFI_OPEN_PROTOCOL_GET_PROTOCOL\r |
83 | );\r |
84 | //\r |
85 | // look for the mEfiShellEnvironment2 protocol at a higher level\r |
86 | //\r |
9eb53ac3 |
87 | if (EFI_ERROR (Status) || !(CompareGuid (&mEfiShellEnvironment2->SESGuid, &gEfiShellEnvironment2ExtGuid) != FALSE)){\r |
94b17fa1 |
88 | //\r |
89 | // figure out how big of a buffer we need.\r |
90 | //\r |
91 | Status = gBS->LocateHandle (ByProtocol,\r |
92 | &gEfiShellEnvironment2Guid,\r |
93 | NULL, // ignored for ByProtocol\r |
94 | &BufferSize,\r |
95 | Buffer\r |
96 | );\r |
2247dde4 |
97 | //\r |
98 | // maybe it's not there???\r |
99 | //\r |
100 | if (Status == EFI_BUFFER_TOO_SMALL) {\r |
101 | Buffer = (EFI_HANDLE*)AllocatePool(BufferSize);\r |
102 | ASSERT(Buffer != NULL);\r |
103 | Status = gBS->LocateHandle (ByProtocol,\r |
104 | &gEfiShellEnvironment2Guid,\r |
105 | NULL, // ignored for ByProtocol\r |
106 | &BufferSize,\r |
107 | Buffer\r |
108 | );\r |
109 | }\r |
1cd45e78 |
110 | if (!EFI_ERROR (Status) && Buffer != NULL) {\r |
94b17fa1 |
111 | //\r |
112 | // now parse the list of returned handles\r |
113 | //\r |
114 | Status = EFI_NOT_FOUND;\r |
115 | for (HandleIndex = 0; HandleIndex < (BufferSize/sizeof(Buffer[0])); HandleIndex++) {\r |
1e6e84c7 |
116 | Status = gBS->OpenProtocol(Buffer[HandleIndex],\r |
94b17fa1 |
117 | &gEfiShellEnvironment2Guid,\r |
118 | (VOID **)&mEfiShellEnvironment2,\r |
119 | ImageHandle,\r |
120 | NULL,\r |
121 | EFI_OPEN_PROTOCOL_GET_PROTOCOL\r |
122 | );\r |
9eb53ac3 |
123 | if (CompareGuid (&mEfiShellEnvironment2->SESGuid, &gEfiShellEnvironment2ExtGuid) != FALSE) {\r |
94b17fa1 |
124 | mEfiShellEnvironment2Handle = Buffer[HandleIndex];\r |
125 | Status = EFI_SUCCESS;\r |
126 | break;\r |
127 | }\r |
128 | }\r |
129 | }\r |
130 | }\r |
131 | if (Buffer != NULL) {\r |
132 | FreePool (Buffer);\r |
133 | }\r |
134 | return (Status);\r |
135 | }\r |
136 | \r |
94b17fa1 |
137 | EFI_STATUS\r |
138 | EFIAPI\r |
d2b4564b |
139 | ShellLibConstructorWorker (\r |
94b17fa1 |
140 | IN EFI_HANDLE ImageHandle,\r |
141 | IN EFI_SYSTEM_TABLE *SystemTable\r |
2247dde4 |
142 | ) {\r |
94b17fa1 |
143 | EFI_STATUS Status;\r |
144 | \r |
b3011f40 |
145 | mPostReplaceFormat = AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize));\r |
ecd3d59f |
146 | ASSERT (mPostReplaceFormat != NULL);\r |
b3011f40 |
147 | mPostReplaceFormat2 = AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize));\r |
ecd3d59f |
148 | ASSERT (mPostReplaceFormat2 != NULL);\r |
149 | \r |
2247dde4 |
150 | //\r |
151 | // Set the parameter count to an invalid number\r |
152 | //\r |
153 | mTotalParameterCount = (UINTN)(-1);\r |
154 | \r |
94b17fa1 |
155 | //\r |
156 | // UEFI 2.0 shell interfaces (used preferentially)\r |
157 | //\r |
1e6e84c7 |
158 | Status = gBS->OpenProtocol(ImageHandle,\r |
94b17fa1 |
159 | &gEfiShellProtocolGuid,\r |
160 | (VOID **)&mEfiShellProtocol,\r |
161 | ImageHandle,\r |
162 | NULL,\r |
163 | EFI_OPEN_PROTOCOL_GET_PROTOCOL\r |
164 | );\r |
165 | if (EFI_ERROR(Status)) {\r |
166 | mEfiShellProtocol = NULL;\r |
167 | }\r |
1e6e84c7 |
168 | Status = gBS->OpenProtocol(ImageHandle,\r |
94b17fa1 |
169 | &gEfiShellParametersProtocolGuid,\r |
170 | (VOID **)&mEfiShellParametersProtocol,\r |
171 | ImageHandle,\r |
172 | NULL,\r |
173 | EFI_OPEN_PROTOCOL_GET_PROTOCOL\r |
174 | );\r |
175 | if (EFI_ERROR(Status)) {\r |
176 | mEfiShellParametersProtocol = NULL;\r |
177 | }\r |
178 | \r |
179 | if (mEfiShellParametersProtocol == NULL || mEfiShellProtocol == NULL) {\r |
180 | //\r |
181 | // Moved to seperate function due to complexity\r |
182 | //\r |
183 | Status = ShellFindSE2(ImageHandle);\r |
184 | \r |
185 | if (EFI_ERROR(Status)) {\r |
186 | DEBUG((DEBUG_ERROR, "Status: 0x%08x\r\n", Status));\r |
187 | mEfiShellEnvironment2 = NULL;\r |
188 | }\r |
1e6e84c7 |
189 | Status = gBS->OpenProtocol(ImageHandle,\r |
94b17fa1 |
190 | &gEfiShellInterfaceGuid,\r |
191 | (VOID **)&mEfiShellInterface,\r |
192 | ImageHandle,\r |
193 | NULL,\r |
194 | EFI_OPEN_PROTOCOL_GET_PROTOCOL\r |
195 | );\r |
196 | if (EFI_ERROR(Status)) {\r |
197 | mEfiShellInterface = NULL;\r |
198 | }\r |
199 | }\r |
200 | //\r |
201 | // only success getting 2 of either the old or new, but no 1/2 and 1/2\r |
202 | //\r |
1e6e84c7 |
203 | if ((mEfiShellEnvironment2 != NULL && mEfiShellInterface != NULL) ||\r |
94b17fa1 |
204 | (mEfiShellProtocol != NULL && mEfiShellParametersProtocol != NULL) ) {\r |
d2b4564b |
205 | if (mEfiShellProtocol != NULL) {\r |
206 | FileFunctionMap.GetFileInfo = mEfiShellProtocol->GetFileInfo;\r |
207 | FileFunctionMap.SetFileInfo = mEfiShellProtocol->SetFileInfo;\r |
208 | FileFunctionMap.ReadFile = mEfiShellProtocol->ReadFile;\r |
209 | FileFunctionMap.WriteFile = mEfiShellProtocol->WriteFile;\r |
210 | FileFunctionMap.CloseFile = mEfiShellProtocol->CloseFile;\r |
211 | FileFunctionMap.DeleteFile = mEfiShellProtocol->DeleteFile;\r |
212 | FileFunctionMap.GetFilePosition = mEfiShellProtocol->GetFilePosition;\r |
213 | FileFunctionMap.SetFilePosition = mEfiShellProtocol->SetFilePosition;\r |
214 | FileFunctionMap.FlushFile = mEfiShellProtocol->FlushFile;\r |
215 | FileFunctionMap.GetFileSize = mEfiShellProtocol->GetFileSize;\r |
216 | } else {\r |
217 | FileFunctionMap.GetFileInfo = FileHandleGetInfo;\r |
218 | FileFunctionMap.SetFileInfo = FileHandleSetInfo;\r |
219 | FileFunctionMap.ReadFile = FileHandleRead;\r |
220 | FileFunctionMap.WriteFile = FileHandleWrite;\r |
221 | FileFunctionMap.CloseFile = FileHandleClose;\r |
222 | FileFunctionMap.DeleteFile = FileHandleDelete;\r |
223 | FileFunctionMap.GetFilePosition = FileHandleGetPosition;\r |
224 | FileFunctionMap.SetFilePosition = FileHandleSetPosition;\r |
225 | FileFunctionMap.FlushFile = FileHandleFlush;\r |
226 | FileFunctionMap.GetFileSize = FileHandleGetSize;\r |
227 | }\r |
94b17fa1 |
228 | return (EFI_SUCCESS);\r |
229 | }\r |
230 | return (EFI_NOT_FOUND);\r |
231 | }\r |
d2b4564b |
232 | /**\r |
233 | Constructor for the Shell library.\r |
234 | \r |
235 | Initialize the library and determine if the underlying is a UEFI Shell 2.0 or an EFI shell.\r |
236 | \r |
237 | @param ImageHandle the image handle of the process\r |
238 | @param SystemTable the EFI System Table pointer\r |
239 | \r |
240 | @retval EFI_SUCCESS the initialization was complete sucessfully\r |
241 | @return others an error ocurred during initialization\r |
242 | **/\r |
243 | EFI_STATUS\r |
244 | EFIAPI\r |
245 | ShellLibConstructor (\r |
246 | IN EFI_HANDLE ImageHandle,\r |
247 | IN EFI_SYSTEM_TABLE *SystemTable\r |
2247dde4 |
248 | ) {\r |
d2b4564b |
249 | \r |
250 | \r |
251 | mEfiShellEnvironment2 = NULL;\r |
252 | mEfiShellProtocol = NULL;\r |
253 | mEfiShellParametersProtocol = NULL;\r |
254 | mEfiShellInterface = NULL;\r |
255 | mEfiShellEnvironment2Handle = NULL;\r |
ecd3d59f |
256 | mPostReplaceFormat = NULL;\r |
257 | mPostReplaceFormat2 = NULL;\r |
d2b4564b |
258 | \r |
d2b4564b |
259 | //\r |
260 | // verify that auto initialize is not set false\r |
1e6e84c7 |
261 | //\r |
d2b4564b |
262 | if (PcdGetBool(PcdShellLibAutoInitialize) == 0) {\r |
263 | return (EFI_SUCCESS);\r |
264 | }\r |
1e6e84c7 |
265 | \r |
d2b4564b |
266 | return (ShellLibConstructorWorker(ImageHandle, SystemTable));\r |
267 | }\r |
94b17fa1 |
268 | \r |
269 | /**\r |
270 | Destructory for the library. free any resources.\r |
271 | **/\r |
272 | EFI_STATUS\r |
273 | EFIAPI\r |
274 | ShellLibDestructor (\r |
275 | IN EFI_HANDLE ImageHandle,\r |
276 | IN EFI_SYSTEM_TABLE *SystemTable\r |
2247dde4 |
277 | ) {\r |
94b17fa1 |
278 | if (mEfiShellEnvironment2 != NULL) {\r |
279 | gBS->CloseProtocol(mEfiShellEnvironment2Handle==NULL?ImageHandle:mEfiShellEnvironment2Handle,\r |
280 | &gEfiShellEnvironment2Guid,\r |
281 | ImageHandle,\r |
282 | NULL);\r |
d2b4564b |
283 | mEfiShellEnvironment2 = NULL;\r |
94b17fa1 |
284 | }\r |
285 | if (mEfiShellInterface != NULL) {\r |
286 | gBS->CloseProtocol(ImageHandle,\r |
287 | &gEfiShellInterfaceGuid,\r |
288 | ImageHandle,\r |
1e6e84c7 |
289 | NULL);\r |
d2b4564b |
290 | mEfiShellInterface = NULL;\r |
94b17fa1 |
291 | }\r |
292 | if (mEfiShellProtocol != NULL) {\r |
293 | gBS->CloseProtocol(ImageHandle,\r |
294 | &gEfiShellProtocolGuid,\r |
295 | ImageHandle,\r |
1e6e84c7 |
296 | NULL);\r |
d2b4564b |
297 | mEfiShellProtocol = NULL;\r |
94b17fa1 |
298 | }\r |
299 | if (mEfiShellParametersProtocol != NULL) {\r |
300 | gBS->CloseProtocol(ImageHandle,\r |
301 | &gEfiShellParametersProtocolGuid,\r |
302 | ImageHandle,\r |
1e6e84c7 |
303 | NULL);\r |
d2b4564b |
304 | mEfiShellParametersProtocol = NULL;\r |
94b17fa1 |
305 | }\r |
d2b4564b |
306 | mEfiShellEnvironment2Handle = NULL;\r |
ecd3d59f |
307 | \r |
308 | if (mPostReplaceFormat != NULL) {\r |
309 | FreePool(mPostReplaceFormat);\r |
310 | }\r |
311 | if (mPostReplaceFormat2 != NULL) {\r |
312 | FreePool(mPostReplaceFormat2);\r |
313 | }\r |
314 | mPostReplaceFormat = NULL;\r |
315 | mPostReplaceFormat2 = NULL;\r |
316 | \r |
94b17fa1 |
317 | return (EFI_SUCCESS);\r |
318 | }\r |
d2b4564b |
319 | \r |
320 | /**\r |
321 | This function causes the shell library to initialize itself. If the shell library\r |
322 | is already initialized it will de-initialize all the current protocol poitners and\r |
323 | re-populate them again.\r |
324 | \r |
325 | When the library is used with PcdShellLibAutoInitialize set to true this function\r |
326 | will return EFI_SUCCESS and perform no actions.\r |
327 | \r |
328 | This function is intended for internal access for shell commands only.\r |
329 | \r |
330 | @retval EFI_SUCCESS the initialization was complete sucessfully\r |
331 | \r |
332 | **/\r |
333 | EFI_STATUS\r |
334 | EFIAPI\r |
335 | ShellInitialize (\r |
336 | ) {\r |
337 | //\r |
338 | // if auto initialize is not false then skip\r |
339 | //\r |
340 | if (PcdGetBool(PcdShellLibAutoInitialize) != 0) {\r |
341 | return (EFI_SUCCESS);\r |
342 | }\r |
343 | \r |
344 | //\r |
345 | // deinit the current stuff\r |
346 | //\r |
347 | ASSERT_EFI_ERROR(ShellLibDestructor(gImageHandle, gST));\r |
348 | \r |
349 | //\r |
350 | // init the new stuff\r |
351 | //\r |
352 | return (ShellLibConstructorWorker(gImageHandle, gST));\r |
353 | }\r |
354 | \r |
94b17fa1 |
355 | /**\r |
1e6e84c7 |
356 | This function will retrieve the information about the file for the handle\r |
94b17fa1 |
357 | specified and store it in allocated pool memory.\r |
358 | \r |
1e6e84c7 |
359 | This function allocates a buffer to store the file's information. It is the\r |
69817bf8 |
360 | caller's responsibility to free the buffer\r |
94b17fa1 |
361 | \r |
1e6e84c7 |
362 | @param FileHandle The file handle of the file for which information is\r |
94b17fa1 |
363 | being requested.\r |
364 | \r |
365 | @retval NULL information could not be retrieved.\r |
366 | \r |
367 | @return the information about the file\r |
368 | **/\r |
369 | EFI_FILE_INFO*\r |
370 | EFIAPI\r |
371 | ShellGetFileInfo (\r |
372 | IN EFI_FILE_HANDLE FileHandle\r |
2247dde4 |
373 | ) {\r |
d2b4564b |
374 | return (FileFunctionMap.GetFileInfo(FileHandle));\r |
94b17fa1 |
375 | }\r |
376 | \r |
377 | /**\r |
1e6e84c7 |
378 | This function will set the information about the file for the opened handle\r |
94b17fa1 |
379 | specified.\r |
380 | \r |
1e6e84c7 |
381 | @param FileHandle The file handle of the file for which information\r |
94b17fa1 |
382 | is being set\r |
383 | \r |
384 | @param FileInfo The infotmation to set.\r |
385 | \r |
386 | @retval EFI_SUCCESS The information was set.\r |
387 | @retval EFI_UNSUPPORTED The InformationType is not known.\r |
388 | @retval EFI_NO_MEDIA The device has no medium.\r |
389 | @retval EFI_DEVICE_ERROR The device reported an error.\r |
390 | @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r |
391 | @retval EFI_WRITE_PROTECTED The file or medium is write protected.\r |
392 | @retval EFI_ACCESS_DENIED The file was opened read only.\r |
393 | @retval EFI_VOLUME_FULL The volume is full.\r |
394 | **/\r |
395 | EFI_STATUS\r |
396 | EFIAPI\r |
397 | ShellSetFileInfo (\r |
398 | IN EFI_FILE_HANDLE FileHandle,\r |
399 | IN EFI_FILE_INFO *FileInfo\r |
2247dde4 |
400 | ) {\r |
d2b4564b |
401 | return (FileFunctionMap.SetFileInfo(FileHandle, FileInfo));\r |
1e6e84c7 |
402 | }\r |
403 | \r |
94b17fa1 |
404 | /**\r |
405 | This function will open a file or directory referenced by DevicePath.\r |
406 | \r |
1e6e84c7 |
407 | This function opens a file with the open mode according to the file path. The\r |
94b17fa1 |
408 | Attributes is valid only for EFI_FILE_MODE_CREATE.\r |
409 | \r |
1e6e84c7 |
410 | @param FilePath on input the device path to the file. On output\r |
94b17fa1 |
411 | the remaining device path.\r |
412 | @param DeviceHandle pointer to the system device handle.\r |
413 | @param FileHandle pointer to the file handle.\r |
414 | @param OpenMode the mode to open the file with.\r |
415 | @param Attributes the file's file attributes.\r |
416 | \r |
417 | @retval EFI_SUCCESS The information was set.\r |
418 | @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.\r |
1e6e84c7 |
419 | @retval EFI_UNSUPPORTED Could not open the file path.\r |
420 | @retval EFI_NOT_FOUND The specified file could not be found on the\r |
421 | device or the file system could not be found on\r |
94b17fa1 |
422 | the device.\r |
423 | @retval EFI_NO_MEDIA The device has no medium.\r |
1e6e84c7 |
424 | @retval EFI_MEDIA_CHANGED The device has a different medium in it or the\r |
94b17fa1 |
425 | medium is no longer supported.\r |
426 | @retval EFI_DEVICE_ERROR The device reported an error.\r |
427 | @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r |
428 | @retval EFI_WRITE_PROTECTED The file or medium is write protected.\r |
429 | @retval EFI_ACCESS_DENIED The file was opened read only.\r |
1e6e84c7 |
430 | @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the\r |
94b17fa1 |
431 | file.\r |
432 | @retval EFI_VOLUME_FULL The volume is full.\r |
433 | **/\r |
434 | EFI_STATUS\r |
435 | EFIAPI\r |
436 | ShellOpenFileByDevicePath(\r |
437 | IN OUT EFI_DEVICE_PATH_PROTOCOL **FilePath,\r |
438 | OUT EFI_HANDLE *DeviceHandle,\r |
439 | OUT EFI_FILE_HANDLE *FileHandle,\r |
440 | IN UINT64 OpenMode,\r |
441 | IN UINT64 Attributes\r |
2247dde4 |
442 | ) {\r |
94b17fa1 |
443 | CHAR16 *FileName;\r |
444 | EFI_STATUS Status;\r |
445 | EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *EfiSimpleFileSystemProtocol;\r |
446 | EFI_FILE_HANDLE LastHandle;\r |
447 | \r |
448 | //\r |
449 | // ASERT for FileHandle, FilePath, and DeviceHandle being NULL\r |
450 | //\r |
451 | ASSERT(FilePath != NULL);\r |
452 | ASSERT(FileHandle != NULL);\r |
453 | ASSERT(DeviceHandle != NULL);\r |
1e6e84c7 |
454 | //\r |
94b17fa1 |
455 | // which shell interface should we use\r |
456 | //\r |
457 | if (mEfiShellProtocol != NULL) {\r |
458 | //\r |
459 | // use UEFI Shell 2.0 method.\r |
460 | //\r |
461 | FileName = mEfiShellProtocol->GetFilePathFromDevicePath(*FilePath);\r |
462 | if (FileName == NULL) {\r |
463 | return (EFI_INVALID_PARAMETER);\r |
464 | }\r |
465 | Status = ShellOpenFileByName(FileName, FileHandle, OpenMode, Attributes);\r |
466 | FreePool(FileName);\r |
467 | return (Status);\r |
1e6e84c7 |
468 | }\r |
d2b4564b |
469 | \r |
470 | \r |
471 | //\r |
472 | // use old shell method.\r |
473 | //\r |
1e6e84c7 |
474 | Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid,\r |
475 | FilePath,\r |
d2b4564b |
476 | DeviceHandle);\r |
477 | if (EFI_ERROR (Status)) {\r |
478 | return Status;\r |
479 | }\r |
480 | Status = gBS->OpenProtocol(*DeviceHandle,\r |
481 | &gEfiSimpleFileSystemProtocolGuid,\r |
b1f95a06 |
482 | (VOID**)&EfiSimpleFileSystemProtocol,\r |
d2b4564b |
483 | gImageHandle,\r |
484 | NULL,\r |
485 | EFI_OPEN_PROTOCOL_GET_PROTOCOL);\r |
486 | if (EFI_ERROR (Status)) {\r |
487 | return Status;\r |
488 | }\r |
489 | Status = EfiSimpleFileSystemProtocol->OpenVolume(EfiSimpleFileSystemProtocol, FileHandle);\r |
490 | if (EFI_ERROR (Status)) {\r |
491 | FileHandle = NULL;\r |
492 | return Status;\r |
493 | }\r |
494 | \r |
495 | //\r |
496 | // go down directories one node at a time.\r |
497 | //\r |
498 | while (!IsDevicePathEnd (*FilePath)) {\r |
94b17fa1 |
499 | //\r |
d2b4564b |
500 | // For file system access each node should be a file path component\r |
94b17fa1 |
501 | //\r |
d2b4564b |
502 | if (DevicePathType (*FilePath) != MEDIA_DEVICE_PATH ||\r |
503 | DevicePathSubType (*FilePath) != MEDIA_FILEPATH_DP\r |
504 | ) {\r |
94b17fa1 |
505 | FileHandle = NULL;\r |
d2b4564b |
506 | return (EFI_INVALID_PARAMETER);\r |
94b17fa1 |
507 | }\r |
d2b4564b |
508 | //\r |
509 | // Open this file path node\r |
510 | //\r |
511 | LastHandle = *FileHandle;\r |
512 | *FileHandle = NULL;\r |
94b17fa1 |
513 | \r |
514 | //\r |
d2b4564b |
515 | // Try to test opening an existing file\r |
94b17fa1 |
516 | //\r |
d2b4564b |
517 | Status = LastHandle->Open (\r |
518 | LastHandle,\r |
519 | FileHandle,\r |
520 | ((FILEPATH_DEVICE_PATH*)*FilePath)->PathName,\r |
521 | OpenMode &~EFI_FILE_MODE_CREATE,\r |
522 | 0\r |
523 | );\r |
94b17fa1 |
524 | \r |
d2b4564b |
525 | //\r |
526 | // see if the error was that it needs to be created\r |
527 | //\r |
528 | if ((EFI_ERROR (Status)) && (OpenMode != (OpenMode &~EFI_FILE_MODE_CREATE))) {\r |
94b17fa1 |
529 | Status = LastHandle->Open (\r |
530 | LastHandle,\r |
531 | FileHandle,\r |
532 | ((FILEPATH_DEVICE_PATH*)*FilePath)->PathName,\r |
d2b4564b |
533 | OpenMode,\r |
534 | Attributes\r |
94b17fa1 |
535 | );\r |
d2b4564b |
536 | }\r |
537 | //\r |
538 | // Close the last node\r |
539 | //\r |
540 | LastHandle->Close (LastHandle);\r |
94b17fa1 |
541 | \r |
d2b4564b |
542 | if (EFI_ERROR(Status)) {\r |
543 | return (Status);\r |
94b17fa1 |
544 | }\r |
d2b4564b |
545 | \r |
546 | //\r |
547 | // Get the next node\r |
548 | //\r |
549 | *FilePath = NextDevicePathNode (*FilePath);\r |
94b17fa1 |
550 | }\r |
d2b4564b |
551 | return (EFI_SUCCESS);\r |
94b17fa1 |
552 | }\r |
553 | \r |
554 | /**\r |
555 | This function will open a file or directory referenced by filename.\r |
556 | \r |
1e6e84c7 |
557 | If return is EFI_SUCCESS, the Filehandle is the opened file's handle;\r |
558 | otherwise, the Filehandle is NULL. The Attributes is valid only for\r |
94b17fa1 |
559 | EFI_FILE_MODE_CREATE.\r |
560 | \r |
561 | if FileNAme is NULL then ASSERT()\r |
562 | \r |
563 | @param FileName pointer to file name\r |
564 | @param FileHandle pointer to the file handle.\r |
565 | @param OpenMode the mode to open the file with.\r |
566 | @param Attributes the file's file attributes.\r |
567 | \r |
568 | @retval EFI_SUCCESS The information was set.\r |
569 | @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.\r |
1e6e84c7 |
570 | @retval EFI_UNSUPPORTED Could not open the file path.\r |
571 | @retval EFI_NOT_FOUND The specified file could not be found on the\r |
572 | device or the file system could not be found\r |
94b17fa1 |
573 | on the device.\r |
574 | @retval EFI_NO_MEDIA The device has no medium.\r |
1e6e84c7 |
575 | @retval EFI_MEDIA_CHANGED The device has a different medium in it or the\r |
94b17fa1 |
576 | medium is no longer supported.\r |
577 | @retval EFI_DEVICE_ERROR The device reported an error.\r |
578 | @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r |
579 | @retval EFI_WRITE_PROTECTED The file or medium is write protected.\r |
580 | @retval EFI_ACCESS_DENIED The file was opened read only.\r |
1e6e84c7 |
581 | @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the\r |
94b17fa1 |
582 | file.\r |
583 | @retval EFI_VOLUME_FULL The volume is full.\r |
584 | **/\r |
585 | EFI_STATUS\r |
586 | EFIAPI\r |
587 | ShellOpenFileByName(\r |
b82bfcc1 |
588 | IN CONST CHAR16 *FileName,\r |
94b17fa1 |
589 | OUT EFI_FILE_HANDLE *FileHandle,\r |
590 | IN UINT64 OpenMode,\r |
591 | IN UINT64 Attributes\r |
2247dde4 |
592 | ) {\r |
94b17fa1 |
593 | EFI_HANDLE DeviceHandle;\r |
594 | EFI_DEVICE_PATH_PROTOCOL *FilePath;\r |
b1f95a06 |
595 | EFI_STATUS Status;\r |
596 | EFI_FILE_INFO *FileInfo;\r |
94b17fa1 |
597 | \r |
598 | //\r |
599 | // ASSERT if FileName is NULL\r |
600 | //\r |
601 | ASSERT(FileName != NULL);\r |
602 | \r |
603 | if (mEfiShellProtocol != NULL) {\r |
604 | //\r |
605 | // Use UEFI Shell 2.0 method\r |
606 | //\r |
b1f95a06 |
607 | Status = mEfiShellProtocol->OpenFileByName(FileName,\r |
608 | FileHandle,\r |
609 | OpenMode);\r |
2247dde4 |
610 | if (!EFI_ERROR(Status) && ((OpenMode & EFI_FILE_MODE_CREATE) != 0)){\r |
611 | FileInfo = FileFunctionMap.GetFileInfo(*FileHandle);\r |
b1f95a06 |
612 | ASSERT(FileInfo != NULL);\r |
613 | FileInfo->Attribute = Attributes;\r |
2247dde4 |
614 | Status = FileFunctionMap.SetFileInfo(*FileHandle, FileInfo);\r |
615 | FreePool(FileInfo);\r |
b1f95a06 |
616 | }\r |
617 | return (Status);\r |
1e6e84c7 |
618 | }\r |
94b17fa1 |
619 | //\r |
620 | // Using EFI Shell version\r |
621 | // this means convert name to path and call that function\r |
622 | // since this will use EFI method again that will open it.\r |
623 | //\r |
624 | ASSERT(mEfiShellEnvironment2 != NULL);\r |
b82bfcc1 |
625 | FilePath = mEfiShellEnvironment2->NameToPath ((CHAR16*)FileName);\r |
94b17fa1 |
626 | if (FileDevicePath != NULL) {\r |
627 | return (ShellOpenFileByDevicePath(&FilePath,\r |
628 | &DeviceHandle,\r |
629 | FileHandle,\r |
630 | OpenMode,\r |
631 | Attributes ));\r |
632 | }\r |
633 | return (EFI_DEVICE_ERROR);\r |
634 | }\r |
635 | /**\r |
636 | This function create a directory\r |
637 | \r |
1e6e84c7 |
638 | If return is EFI_SUCCESS, the Filehandle is the opened directory's handle;\r |
639 | otherwise, the Filehandle is NULL. If the directory already existed, this\r |
94b17fa1 |
640 | function opens the existing directory.\r |
641 | \r |
642 | @param DirectoryName pointer to directory name\r |
643 | @param FileHandle pointer to the file handle.\r |
644 | \r |
645 | @retval EFI_SUCCESS The information was set.\r |
646 | @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.\r |
1e6e84c7 |
647 | @retval EFI_UNSUPPORTED Could not open the file path.\r |
648 | @retval EFI_NOT_FOUND The specified file could not be found on the\r |
649 | device or the file system could not be found\r |
94b17fa1 |
650 | on the device.\r |
651 | @retval EFI_NO_MEDIA The device has no medium.\r |
1e6e84c7 |
652 | @retval EFI_MEDIA_CHANGED The device has a different medium in it or the\r |
94b17fa1 |
653 | medium is no longer supported.\r |
654 | @retval EFI_DEVICE_ERROR The device reported an error.\r |
655 | @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r |
656 | @retval EFI_WRITE_PROTECTED The file or medium is write protected.\r |
657 | @retval EFI_ACCESS_DENIED The file was opened read only.\r |
1e6e84c7 |
658 | @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the\r |
94b17fa1 |
659 | file.\r |
660 | @retval EFI_VOLUME_FULL The volume is full.\r |
661 | @sa ShellOpenFileByName\r |
662 | **/\r |
663 | EFI_STATUS\r |
664 | EFIAPI\r |
665 | ShellCreateDirectory(\r |
b82bfcc1 |
666 | IN CONST CHAR16 *DirectoryName,\r |
94b17fa1 |
667 | OUT EFI_FILE_HANDLE *FileHandle\r |
2247dde4 |
668 | ) {\r |
669 | if (mEfiShellProtocol != NULL) {\r |
670 | //\r |
671 | // Use UEFI Shell 2.0 method\r |
672 | //\r |
673 | return (mEfiShellProtocol->CreateFile(DirectoryName,\r |
674 | EFI_FILE_DIRECTORY,\r |
675 | FileHandle\r |
676 | ));\r |
677 | } else {\r |
678 | return (ShellOpenFileByName(DirectoryName,\r |
679 | FileHandle,\r |
680 | EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE,\r |
681 | EFI_FILE_DIRECTORY\r |
682 | ));\r |
683 | }\r |
94b17fa1 |
684 | }\r |
685 | \r |
686 | /**\r |
687 | This function reads information from an opened file.\r |
688 | \r |
1e6e84c7 |
689 | If FileHandle is not a directory, the function reads the requested number of\r |
690 | bytes from the file at the file's current position and returns them in Buffer.\r |
94b17fa1 |
691 | If the read goes beyond the end of the file, the read length is truncated to the\r |
1e6e84c7 |
692 | end of the file. The file's current position is increased by the number of bytes\r |
693 | returned. If FileHandle is a directory, the function reads the directory entry\r |
694 | at the file's current position and returns the entry in Buffer. If the Buffer\r |
695 | is not large enough to hold the current directory entry, then\r |
696 | EFI_BUFFER_TOO_SMALL is returned and the current file position is not updated.\r |
697 | BufferSize is set to be the size of the buffer needed to read the entry. On\r |
698 | success, the current position is updated to the next directory entry. If there\r |
699 | are no more directory entries, the read returns a zero-length buffer.\r |
94b17fa1 |
700 | EFI_FILE_INFO is the structure returned as the directory entry.\r |
701 | \r |
702 | @param FileHandle the opened file handle\r |
1e6e84c7 |
703 | @param BufferSize on input the size of buffer in bytes. on return\r |
94b17fa1 |
704 | the number of bytes written.\r |
705 | @param Buffer the buffer to put read data into.\r |
706 | \r |
707 | @retval EFI_SUCCESS Data was read.\r |
708 | @retval EFI_NO_MEDIA The device has no media.\r |
709 | @retval EFI_DEVICE_ERROR The device reported an error.\r |
710 | @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r |
1e6e84c7 |
711 | @retval EFI_BUFFER_TO_SMALL Buffer is too small. ReadSize contains required\r |
94b17fa1 |
712 | size.\r |
713 | \r |
714 | **/\r |
715 | EFI_STATUS\r |
716 | EFIAPI\r |
717 | ShellReadFile(\r |
718 | IN EFI_FILE_HANDLE FileHandle,\r |
719 | IN OUT UINTN *BufferSize,\r |
720 | OUT VOID *Buffer\r |
2247dde4 |
721 | ) {\r |
d2b4564b |
722 | return (FileFunctionMap.ReadFile(FileHandle, BufferSize, Buffer));\r |
94b17fa1 |
723 | }\r |
724 | \r |
725 | \r |
726 | /**\r |
727 | Write data to a file.\r |
728 | \r |
1e6e84c7 |
729 | This function writes the specified number of bytes to the file at the current\r |
730 | file position. The current file position is advanced the actual number of bytes\r |
731 | written, which is returned in BufferSize. Partial writes only occur when there\r |
732 | has been a data error during the write attempt (such as "volume space full").\r |
733 | The file is automatically grown to hold the data if required. Direct writes to\r |
94b17fa1 |
734 | opened directories are not supported.\r |
735 | \r |
736 | @param FileHandle The opened file for writing\r |
737 | @param BufferSize on input the number of bytes in Buffer. On output\r |
738 | the number of bytes written.\r |
739 | @param Buffer the buffer containing data to write is stored.\r |
740 | \r |
741 | @retval EFI_SUCCESS Data was written.\r |
742 | @retval EFI_UNSUPPORTED Writes to an open directory are not supported.\r |
743 | @retval EFI_NO_MEDIA The device has no media.\r |
744 | @retval EFI_DEVICE_ERROR The device reported an error.\r |
745 | @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r |
746 | @retval EFI_WRITE_PROTECTED The device is write-protected.\r |
747 | @retval EFI_ACCESS_DENIED The file was open for read only.\r |
748 | @retval EFI_VOLUME_FULL The volume is full.\r |
749 | **/\r |
750 | EFI_STATUS\r |
751 | EFIAPI\r |
752 | ShellWriteFile(\r |
753 | IN EFI_FILE_HANDLE FileHandle,\r |
754 | IN OUT UINTN *BufferSize,\r |
755 | IN VOID *Buffer\r |
2247dde4 |
756 | ) {\r |
d2b4564b |
757 | return (FileFunctionMap.WriteFile(FileHandle, BufferSize, Buffer));\r |
94b17fa1 |
758 | }\r |
759 | \r |
1e6e84c7 |
760 | /**\r |
94b17fa1 |
761 | Close an open file handle.\r |
762 | \r |
1e6e84c7 |
763 | This function closes a specified file handle. All "dirty" cached file data is\r |
764 | flushed to the device, and the file is closed. In all cases the handle is\r |
94b17fa1 |
765 | closed.\r |
766 | \r |
767 | @param FileHandle the file handle to close.\r |
768 | \r |
769 | @retval EFI_SUCCESS the file handle was closed sucessfully.\r |
770 | **/\r |
771 | EFI_STATUS\r |
772 | EFIAPI\r |
773 | ShellCloseFile (\r |
774 | IN EFI_FILE_HANDLE *FileHandle\r |
2247dde4 |
775 | ) {\r |
d2b4564b |
776 | return (FileFunctionMap.CloseFile(*FileHandle));\r |
94b17fa1 |
777 | }\r |
778 | \r |
779 | /**\r |
780 | Delete a file and close the handle\r |
781 | \r |
782 | This function closes and deletes a file. In all cases the file handle is closed.\r |
1e6e84c7 |
783 | If the file cannot be deleted, the warning code EFI_WARN_DELETE_FAILURE is\r |
94b17fa1 |
784 | returned, but the handle is still closed.\r |
785 | \r |
786 | @param FileHandle the file handle to delete\r |
787 | \r |
788 | @retval EFI_SUCCESS the file was closed sucessfully\r |
1e6e84c7 |
789 | @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not\r |
94b17fa1 |
790 | deleted\r |
791 | @retval INVALID_PARAMETER One of the parameters has an invalid value.\r |
792 | **/\r |
793 | EFI_STATUS\r |
794 | EFIAPI\r |
795 | ShellDeleteFile (\r |
796 | IN EFI_FILE_HANDLE *FileHandle\r |
2247dde4 |
797 | ) {\r |
d2b4564b |
798 | return (FileFunctionMap.DeleteFile(*FileHandle));\r |
94b17fa1 |
799 | }\r |
800 | \r |
801 | /**\r |
802 | Set the current position in a file.\r |
803 | \r |
1e6e84c7 |
804 | This function sets the current file position for the handle to the position\r |
94b17fa1 |
805 | supplied. With the exception of seeking to position 0xFFFFFFFFFFFFFFFF, only\r |
1e6e84c7 |
806 | absolute positioning is supported, and seeking past the end of the file is\r |
807 | allowed (a subsequent write would grow the file). Seeking to position\r |
94b17fa1 |
808 | 0xFFFFFFFFFFFFFFFF causes the current position to be set to the end of the file.\r |
1e6e84c7 |
809 | If FileHandle is a directory, the only position that may be set is zero. This\r |
94b17fa1 |
810 | has the effect of starting the read process of the directory entries over.\r |
811 | \r |
812 | @param FileHandle The file handle on which the position is being set\r |
813 | @param Position Byte position from begining of file\r |
814 | \r |
815 | @retval EFI_SUCCESS Operation completed sucessfully.\r |
1e6e84c7 |
816 | @retval EFI_UNSUPPORTED the seek request for non-zero is not valid on\r |
94b17fa1 |
817 | directories.\r |
818 | @retval INVALID_PARAMETER One of the parameters has an invalid value.\r |
819 | **/\r |
820 | EFI_STATUS\r |
821 | EFIAPI\r |
822 | ShellSetFilePosition (\r |
823 | IN EFI_FILE_HANDLE FileHandle,\r |
824 | IN UINT64 Position\r |
2247dde4 |
825 | ) {\r |
d2b4564b |
826 | return (FileFunctionMap.SetFilePosition(FileHandle, Position));\r |
94b17fa1 |
827 | }\r |
828 | \r |
1e6e84c7 |
829 | /**\r |
94b17fa1 |
830 | Gets a file's current position\r |
831 | \r |
1e6e84c7 |
832 | This function retrieves the current file position for the file handle. For\r |
833 | directories, the current file position has no meaning outside of the file\r |
94b17fa1 |
834 | system driver and as such the operation is not supported. An error is returned\r |
835 | if FileHandle is a directory.\r |
836 | \r |
837 | @param FileHandle The open file handle on which to get the position.\r |
838 | @param Position Byte position from begining of file.\r |
839 | \r |
840 | @retval EFI_SUCCESS the operation completed sucessfully.\r |
841 | @retval INVALID_PARAMETER One of the parameters has an invalid value.\r |
842 | @retval EFI_UNSUPPORTED the request is not valid on directories.\r |
843 | **/\r |
844 | EFI_STATUS\r |
845 | EFIAPI\r |
846 | ShellGetFilePosition (\r |
847 | IN EFI_FILE_HANDLE FileHandle,\r |
848 | OUT UINT64 *Position\r |
2247dde4 |
849 | ) {\r |
d2b4564b |
850 | return (FileFunctionMap.GetFilePosition(FileHandle, Position));\r |
94b17fa1 |
851 | }\r |
852 | /**\r |
853 | Flushes data on a file\r |
1e6e84c7 |
854 | \r |
94b17fa1 |
855 | This function flushes all modified data associated with a file to a device.\r |
856 | \r |
857 | @param FileHandle The file handle on which to flush data\r |
858 | \r |
859 | @retval EFI_SUCCESS The data was flushed.\r |
860 | @retval EFI_NO_MEDIA The device has no media.\r |
861 | @retval EFI_DEVICE_ERROR The device reported an error.\r |
862 | @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r |
863 | @retval EFI_WRITE_PROTECTED The file or medium is write protected.\r |
864 | @retval EFI_ACCESS_DENIED The file was opened for read only.\r |
865 | **/\r |
866 | EFI_STATUS\r |
867 | EFIAPI\r |
868 | ShellFlushFile (\r |
869 | IN EFI_FILE_HANDLE FileHandle\r |
2247dde4 |
870 | ) {\r |
d2b4564b |
871 | return (FileFunctionMap.FlushFile(FileHandle));\r |
94b17fa1 |
872 | }\r |
873 | \r |
874 | /**\r |
875 | Retrieves the first file from a directory\r |
876 | \r |
1e6e84c7 |
877 | This function opens a directory and gets the first file's info in the\r |
878 | directory. Caller can use ShellFindNextFile() to get other files. When\r |
94b17fa1 |
879 | complete the caller is responsible for calling FreePool() on Buffer.\r |
880 | \r |
881 | @param DirHandle The file handle of the directory to search\r |
882 | @param Buffer Pointer to buffer for file's information\r |
883 | \r |
884 | @retval EFI_SUCCESS Found the first file.\r |
885 | @retval EFI_NOT_FOUND Cannot find the directory.\r |
886 | @retval EFI_NO_MEDIA The device has no media.\r |
887 | @retval EFI_DEVICE_ERROR The device reported an error.\r |
888 | @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r |
889 | @return Others status of ShellGetFileInfo, ShellSetFilePosition,\r |
890 | or ShellReadFile\r |
891 | **/\r |
892 | EFI_STATUS\r |
893 | EFIAPI\r |
894 | ShellFindFirstFile (\r |
895 | IN EFI_FILE_HANDLE DirHandle,\r |
d2b4564b |
896 | OUT EFI_FILE_INFO **Buffer\r |
2247dde4 |
897 | ) {\r |
94b17fa1 |
898 | //\r |
d2b4564b |
899 | // pass to file handle lib\r |
94b17fa1 |
900 | //\r |
d2b4564b |
901 | return (FileHandleFindFirstFile(DirHandle, Buffer));\r |
94b17fa1 |
902 | }\r |
903 | /**\r |
904 | Retrieves the next file in a directory.\r |
905 | \r |
1e6e84c7 |
906 | To use this function, caller must call the LibFindFirstFile() to get the\r |
907 | first file, and then use this function get other files. This function can be\r |
908 | called for several times to get each file's information in the directory. If\r |
909 | the call of ShellFindNextFile() got the last file in the directory, the next\r |
910 | call of this function has no file to get. *NoFile will be set to TRUE and the\r |
911 | Buffer memory will be automatically freed.\r |
94b17fa1 |
912 | \r |
913 | @param DirHandle the file handle of the directory\r |
914 | @param Buffer pointer to buffer for file's information\r |
915 | @param NoFile pointer to boolean when last file is found\r |
916 | \r |
917 | @retval EFI_SUCCESS Found the next file, or reached last file\r |
918 | @retval EFI_NO_MEDIA The device has no media.\r |
919 | @retval EFI_DEVICE_ERROR The device reported an error.\r |
920 | @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r |
921 | **/\r |
922 | EFI_STATUS\r |
923 | EFIAPI\r |
924 | ShellFindNextFile(\r |
925 | IN EFI_FILE_HANDLE DirHandle,\r |
926 | OUT EFI_FILE_INFO *Buffer,\r |
927 | OUT BOOLEAN *NoFile\r |
2247dde4 |
928 | ) {\r |
94b17fa1 |
929 | //\r |
d2b4564b |
930 | // pass to file handle lib\r |
94b17fa1 |
931 | //\r |
d2b4564b |
932 | return (FileHandleFindNextFile(DirHandle, Buffer, NoFile));\r |
94b17fa1 |
933 | }\r |
934 | /**\r |
935 | Retrieve the size of a file.\r |
936 | \r |
937 | if FileHandle is NULL then ASSERT()\r |
938 | if Size is NULL then ASSERT()\r |
939 | \r |
1e6e84c7 |
940 | This function extracts the file size info from the FileHandle's EFI_FILE_INFO\r |
94b17fa1 |
941 | data.\r |
942 | \r |
943 | @param FileHandle file handle from which size is retrieved\r |
944 | @param Size pointer to size\r |
945 | \r |
946 | @retval EFI_SUCCESS operation was completed sucessfully\r |
947 | @retval EFI_DEVICE_ERROR cannot access the file\r |
948 | **/\r |
949 | EFI_STATUS\r |
950 | EFIAPI\r |
951 | ShellGetFileSize (\r |
952 | IN EFI_FILE_HANDLE FileHandle,\r |
953 | OUT UINT64 *Size\r |
2247dde4 |
954 | ) {\r |
d2b4564b |
955 | return (FileFunctionMap.GetFileSize(FileHandle, Size));\r |
94b17fa1 |
956 | }\r |
957 | /**\r |
958 | Retrieves the status of the break execution flag\r |
959 | \r |
960 | this function is useful to check whether the application is being asked to halt by the shell.\r |
961 | \r |
962 | @retval TRUE the execution break is enabled\r |
963 | @retval FALSE the execution break is not enabled\r |
964 | **/\r |
965 | BOOLEAN\r |
966 | EFIAPI\r |
967 | ShellGetExecutionBreakFlag(\r |
968 | VOID\r |
969 | )\r |
970 | {\r |
1e6e84c7 |
971 | //\r |
94b17fa1 |
972 | // Check for UEFI Shell 2.0 protocols\r |
973 | //\r |
974 | if (mEfiShellProtocol != NULL) {\r |
975 | \r |
976 | //\r |
977 | // We are using UEFI Shell 2.0; see if the event has been triggered\r |
978 | //\r |
979 | if (gBS->CheckEvent(mEfiShellProtocol->ExecutionBreak) != EFI_SUCCESS) {\r |
980 | return (FALSE);\r |
981 | }\r |
982 | return (TRUE);\r |
1e6e84c7 |
983 | }\r |
94b17fa1 |
984 | \r |
985 | //\r |
986 | // using EFI Shell; call the function to check\r |
987 | //\r |
988 | ASSERT(mEfiShellEnvironment2 != NULL);\r |
989 | return (mEfiShellEnvironment2->GetExecutionBreak());\r |
990 | }\r |
991 | /**\r |
992 | return the value of an environment variable\r |
993 | \r |
1e6e84c7 |
994 | this function gets the value of the environment variable set by the\r |
94b17fa1 |
995 | ShellSetEnvironmentVariable function\r |
996 | \r |
997 | @param EnvKey The key name of the environment variable.\r |
998 | \r |
999 | @retval NULL the named environment variable does not exist.\r |
1000 | @return != NULL pointer to the value of the environment variable\r |
1001 | **/\r |
1002 | CONST CHAR16*\r |
1003 | EFIAPI\r |
1004 | ShellGetEnvironmentVariable (\r |
9b3bf083 |
1005 | IN CONST CHAR16 *EnvKey\r |
94b17fa1 |
1006 | )\r |
1007 | {\r |
1e6e84c7 |
1008 | //\r |
94b17fa1 |
1009 | // Check for UEFI Shell 2.0 protocols\r |
1010 | //\r |
1011 | if (mEfiShellProtocol != NULL) {\r |
1012 | return (mEfiShellProtocol->GetEnv(EnvKey));\r |
1013 | }\r |
1014 | \r |
1015 | //\r |
1016 | // ASSERT that we must have EFI shell\r |
1017 | //\r |
1018 | ASSERT(mEfiShellEnvironment2 != NULL);\r |
1019 | \r |
1020 | //\r |
1021 | // using EFI Shell\r |
1022 | //\r |
9b3bf083 |
1023 | return (mEfiShellEnvironment2->GetEnv((CHAR16*)EnvKey));\r |
94b17fa1 |
1024 | }\r |
1025 | /**\r |
1026 | set the value of an environment variable\r |
1027 | \r |
1028 | This function changes the current value of the specified environment variable. If the\r |
1029 | environment variable exists and the Value is an empty string, then the environment\r |
1030 | variable is deleted. If the environment variable exists and the Value is not an empty\r |
1031 | string, then the value of the environment variable is changed. If the environment\r |
1032 | variable does not exist and the Value is an empty string, there is no action. If the\r |
1033 | environment variable does not exist and the Value is a non-empty string, then the\r |
1034 | environment variable is created and assigned the specified value.\r |
1035 | \r |
1036 | This is not supported pre-UEFI Shell 2.0.\r |
1037 | \r |
1038 | @param EnvKey The key name of the environment variable.\r |
1039 | @param EnvVal The Value of the environment variable\r |
1040 | @param Volatile Indicates whether the variable is non-volatile (FALSE) or volatile (TRUE).\r |
1041 | \r |
1042 | @retval EFI_SUCCESS the operation was completed sucessfully\r |
1043 | @retval EFI_UNSUPPORTED This operation is not allowed in pre UEFI 2.0 Shell environments\r |
1044 | **/\r |
1045 | EFI_STATUS\r |
1046 | EFIAPI\r |
1047 | ShellSetEnvironmentVariable (\r |
1048 | IN CONST CHAR16 *EnvKey,\r |
1049 | IN CONST CHAR16 *EnvVal,\r |
1050 | IN BOOLEAN Volatile\r |
1051 | )\r |
1052 | {\r |
1e6e84c7 |
1053 | //\r |
94b17fa1 |
1054 | // Check for UEFI Shell 2.0 protocols\r |
1055 | //\r |
1056 | if (mEfiShellProtocol != NULL) {\r |
1057 | return (mEfiShellProtocol->SetEnv(EnvKey, EnvVal, Volatile));\r |
1e6e84c7 |
1058 | }\r |
94b17fa1 |
1059 | \r |
1060 | //\r |
1061 | // This feature does not exist under EFI shell\r |
1062 | //\r |
1063 | return (EFI_UNSUPPORTED);\r |
1064 | }\r |
1065 | /**\r |
1066 | cause the shell to parse and execute a command line.\r |
1067 | \r |
1068 | This function creates a nested instance of the shell and executes the specified\r |
1069 | command (CommandLine) with the specified environment (Environment). Upon return,\r |
1070 | the status code returned by the specified command is placed in StatusCode.\r |
1071 | If Environment is NULL, then the current environment is used and all changes made\r |
1072 | by the commands executed will be reflected in the current environment. If the\r |
1073 | Environment is non-NULL, then the changes made will be discarded.\r |
1074 | The CommandLine is executed from the current working directory on the current\r |
1075 | device.\r |
1076 | \r |
1077 | EnvironmentVariables and Status are only supported for UEFI Shell 2.0.\r |
1078 | Output is only supported for pre-UEFI Shell 2.0\r |
1079 | \r |
1080 | @param ImageHandle Parent image that is starting the operation\r |
1e6e84c7 |
1081 | @param CommandLine pointer to NULL terminated command line.\r |
94b17fa1 |
1082 | @param Output true to display debug output. false to hide it.\r |
1083 | @param EnvironmentVariables optional pointer to array of environment variables\r |
1084 | in the form "x=y". if NULL current set is used.\r |
1085 | @param Status the status of the run command line.\r |
1086 | \r |
1087 | @retval EFI_SUCCESS the operation completed sucessfully. Status\r |
1088 | contains the status code returned.\r |
1089 | @retval EFI_INVALID_PARAMETER a parameter contains an invalid value\r |
1090 | @retval EFI_OUT_OF_RESOURCES out of resources\r |
1091 | @retval EFI_UNSUPPORTED the operation is not allowed.\r |
1092 | **/\r |
1093 | EFI_STATUS\r |
1094 | EFIAPI\r |
1095 | ShellExecute (\r |
1096 | IN EFI_HANDLE *ParentHandle,\r |
1097 | IN CHAR16 *CommandLine OPTIONAL,\r |
1098 | IN BOOLEAN Output OPTIONAL,\r |
1099 | IN CHAR16 **EnvironmentVariables OPTIONAL,\r |
1100 | OUT EFI_STATUS *Status OPTIONAL\r |
1101 | )\r |
1102 | {\r |
1e6e84c7 |
1103 | //\r |
94b17fa1 |
1104 | // Check for UEFI Shell 2.0 protocols\r |
1105 | //\r |
1106 | if (mEfiShellProtocol != NULL) {\r |
1107 | //\r |
1108 | // Call UEFI Shell 2.0 version (not using Output parameter)\r |
1109 | //\r |
1110 | return (mEfiShellProtocol->Execute(ParentHandle,\r |
1111 | CommandLine,\r |
1112 | EnvironmentVariables,\r |
1113 | Status));\r |
1e6e84c7 |
1114 | }\r |
94b17fa1 |
1115 | //\r |
1116 | // ASSERT that we must have EFI shell\r |
1117 | //\r |
1118 | ASSERT(mEfiShellEnvironment2 != NULL);\r |
1119 | //\r |
1120 | // Call EFI Shell version (not using EnvironmentVariables or Status parameters)\r |
1121 | // Due to oddity in the EFI shell we want to dereference the ParentHandle here\r |
1122 | //\r |
1e6e84c7 |
1123 | return (mEfiShellEnvironment2->Execute(*ParentHandle,\r |
1124 | CommandLine,\r |
94b17fa1 |
1125 | Output));\r |
1126 | }\r |
1127 | /**\r |
1128 | Retreives the current directory path\r |
1129 | \r |
1e6e84c7 |
1130 | If the DeviceName is NULL, it returns the current device's current directory\r |
1131 | name. If the DeviceName is not NULL, it returns the current directory name\r |
94b17fa1 |
1132 | on specified drive.\r |
1133 | \r |
1134 | @param DeviceName the name of the drive to get directory on\r |
1135 | \r |
1136 | @retval NULL the directory does not exist\r |
1137 | @return != NULL the directory\r |
1138 | **/\r |
1139 | CONST CHAR16*\r |
1140 | EFIAPI\r |
1141 | ShellGetCurrentDir (\r |
1142 | IN CHAR16 *DeviceName OPTIONAL\r |
1143 | )\r |
1144 | {\r |
1e6e84c7 |
1145 | //\r |
94b17fa1 |
1146 | // Check for UEFI Shell 2.0 protocols\r |
1147 | //\r |
1148 | if (mEfiShellProtocol != NULL) {\r |
1149 | return (mEfiShellProtocol->GetCurDir(DeviceName));\r |
1e6e84c7 |
1150 | }\r |
94b17fa1 |
1151 | //\r |
1152 | // ASSERT that we must have EFI shell\r |
1153 | //\r |
1154 | ASSERT(mEfiShellEnvironment2 != NULL);\r |
1155 | return (mEfiShellEnvironment2->CurDir(DeviceName));\r |
1156 | }\r |
1157 | /**\r |
1158 | sets (enabled or disabled) the page break mode\r |
1159 | \r |
1e6e84c7 |
1160 | when page break mode is enabled the screen will stop scrolling\r |
94b17fa1 |
1161 | and wait for operator input before scrolling a subsequent screen.\r |
1162 | \r |
1163 | @param CurrentState TRUE to enable and FALSE to disable\r |
1164 | **/\r |
1e6e84c7 |
1165 | VOID\r |
94b17fa1 |
1166 | EFIAPI\r |
1167 | ShellSetPageBreakMode (\r |
1168 | IN BOOLEAN CurrentState\r |
1169 | )\r |
1170 | {\r |
1171 | //\r |
1172 | // check for enabling\r |
1173 | //\r |
1174 | if (CurrentState != 0x00) {\r |
1e6e84c7 |
1175 | //\r |
94b17fa1 |
1176 | // check for UEFI Shell 2.0\r |
1177 | //\r |
1178 | if (mEfiShellProtocol != NULL) {\r |
1179 | //\r |
1180 | // Enable with UEFI 2.0 Shell\r |
1181 | //\r |
1182 | mEfiShellProtocol->EnablePageBreak();\r |
1183 | return;\r |
1184 | } else {\r |
1e6e84c7 |
1185 | //\r |
94b17fa1 |
1186 | // ASSERT that must have EFI Shell\r |
1187 | //\r |
1188 | ASSERT(mEfiShellEnvironment2 != NULL);\r |
1189 | //\r |
1190 | // Enable with EFI Shell\r |
1191 | //\r |
1192 | mEfiShellEnvironment2->EnablePageBreak (DEFAULT_INIT_ROW, DEFAULT_AUTO_LF);\r |
1193 | return;\r |
1194 | }\r |
1195 | } else {\r |
1e6e84c7 |
1196 | //\r |
94b17fa1 |
1197 | // check for UEFI Shell 2.0\r |
1198 | //\r |
1199 | if (mEfiShellProtocol != NULL) {\r |
1200 | //\r |
1201 | // Disable with UEFI 2.0 Shell\r |
1202 | //\r |
1203 | mEfiShellProtocol->DisablePageBreak();\r |
1204 | return;\r |
1205 | } else {\r |
1e6e84c7 |
1206 | //\r |
94b17fa1 |
1207 | // ASSERT that must have EFI Shell\r |
1208 | //\r |
1209 | ASSERT(mEfiShellEnvironment2 != NULL);\r |
1210 | //\r |
1211 | // Disable with EFI Shell\r |
1212 | //\r |
1213 | mEfiShellEnvironment2->DisablePageBreak ();\r |
1214 | return;\r |
1215 | }\r |
1216 | }\r |
1217 | }\r |
1218 | \r |
1219 | ///\r |
1220 | /// version of EFI_SHELL_FILE_INFO struct, except has no CONST pointers.\r |
1221 | /// This allows for the struct to be populated.\r |
1222 | ///\r |
1223 | typedef struct {\r |
d2b4564b |
1224 | LIST_ENTRY Link;\r |
94b17fa1 |
1225 | EFI_STATUS Status;\r |
1226 | CHAR16 *FullName;\r |
1227 | CHAR16 *FileName;\r |
1228 | EFI_FILE_HANDLE Handle;\r |
1229 | EFI_FILE_INFO *Info;\r |
1230 | } EFI_SHELL_FILE_INFO_NO_CONST;\r |
1231 | \r |
1232 | /**\r |
1233 | Converts a EFI shell list of structures to the coresponding UEFI Shell 2.0 type of list.\r |
1234 | \r |
1235 | if OldStyleFileList is NULL then ASSERT()\r |
1236 | \r |
1e6e84c7 |
1237 | this function will convert a SHELL_FILE_ARG based list into a callee allocated\r |
94b17fa1 |
1238 | EFI_SHELL_FILE_INFO based list. it is up to the caller to free the memory via\r |
1239 | the ShellCloseFileMetaArg function.\r |
1240 | \r |
9b3bf083 |
1241 | @param[in] FileList the EFI shell list type\r |
b82bfcc1 |
1242 | @param[in,out] ListHead the list to add to\r |
94b17fa1 |
1243 | \r |
1244 | @retval the resultant head of the double linked new format list;\r |
1245 | **/\r |
1246 | LIST_ENTRY*\r |
1247 | EFIAPI\r |
1248 | InternalShellConvertFileListType (\r |
9b3bf083 |
1249 | IN LIST_ENTRY *FileList,\r |
1250 | IN OUT LIST_ENTRY *ListHead\r |
125c2cf4 |
1251 | )\r |
1252 | {\r |
94b17fa1 |
1253 | SHELL_FILE_ARG *OldInfo;\r |
9b3bf083 |
1254 | LIST_ENTRY *Link;\r |
94b17fa1 |
1255 | EFI_SHELL_FILE_INFO_NO_CONST *NewInfo;\r |
1256 | \r |
1257 | //\r |
9b3bf083 |
1258 | // ASSERTs\r |
94b17fa1 |
1259 | //\r |
9b3bf083 |
1260 | ASSERT(FileList != NULL);\r |
1261 | ASSERT(ListHead != NULL);\r |
94b17fa1 |
1262 | \r |
1263 | //\r |
1264 | // enumerate through each member of the old list and copy\r |
1265 | //\r |
d2b4564b |
1266 | for (Link = FileList->ForwardLink; Link != FileList; Link = Link->ForwardLink) {\r |
94b17fa1 |
1267 | OldInfo = CR (Link, SHELL_FILE_ARG, Link, SHELL_FILE_ARG_SIGNATURE);\r |
1268 | \r |
1269 | //\r |
1270 | // make sure the old list was valid\r |
1271 | //\r |
1e6e84c7 |
1272 | ASSERT(OldInfo != NULL);\r |
94b17fa1 |
1273 | ASSERT(OldInfo->Info != NULL);\r |
1274 | ASSERT(OldInfo->FullName != NULL);\r |
1275 | ASSERT(OldInfo->FileName != NULL);\r |
1276 | \r |
1277 | //\r |
1278 | // allocate a new EFI_SHELL_FILE_INFO object\r |
1279 | //\r |
1280 | NewInfo = AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));\r |
1e6e84c7 |
1281 | \r |
1282 | //\r |
94b17fa1 |
1283 | // copy the simple items\r |
1284 | //\r |
1285 | NewInfo->Handle = OldInfo->Handle;\r |
1286 | NewInfo->Status = OldInfo->Status;\r |
1287 | \r |
d2b4564b |
1288 | // old shell checks for 0 not NULL\r |
1289 | OldInfo->Handle = 0;\r |
1290 | \r |
94b17fa1 |
1291 | //\r |
1292 | // allocate new space to copy strings and structure\r |
1293 | //\r |
1294 | NewInfo->FullName = AllocateZeroPool(StrSize(OldInfo->FullName));\r |
1295 | NewInfo->FileName = AllocateZeroPool(StrSize(OldInfo->FileName));\r |
1296 | NewInfo->Info = AllocateZeroPool((UINTN)OldInfo->Info->Size);\r |
1e6e84c7 |
1297 | \r |
94b17fa1 |
1298 | //\r |
1299 | // make sure all the memory allocations were sucessful\r |
1300 | //\r |
1301 | ASSERT(NewInfo->FullName != NULL);\r |
1302 | ASSERT(NewInfo->FileName != NULL);\r |
1303 | ASSERT(NewInfo->Info != NULL);\r |
1304 | \r |
1305 | //\r |
1306 | // Copt the strings and structure\r |
1307 | //\r |
1308 | StrCpy(NewInfo->FullName, OldInfo->FullName);\r |
1309 | StrCpy(NewInfo->FileName, OldInfo->FileName);\r |
1310 | gBS->CopyMem (NewInfo->Info, OldInfo->Info, (UINTN)OldInfo->Info->Size);\r |
1311 | \r |
1312 | //\r |
1313 | // add that to the list\r |
1314 | //\r |
9b3bf083 |
1315 | InsertTailList(ListHead, &NewInfo->Link);\r |
94b17fa1 |
1316 | }\r |
1317 | return (ListHead);\r |
1318 | }\r |
1319 | /**\r |
1320 | Opens a group of files based on a path.\r |
1321 | \r |
1e6e84c7 |
1322 | This function uses the Arg to open all the matching files. Each matched\r |
1323 | file has a SHELL_FILE_ARG structure to record the file information. These\r |
1324 | structures are placed on the list ListHead. Users can get the SHELL_FILE_ARG\r |
94b17fa1 |
1325 | structures from ListHead to access each file. This function supports wildcards\r |
1e6e84c7 |
1326 | and will process '?' and '*' as such. the list must be freed with a call to\r |
94b17fa1 |
1327 | ShellCloseFileMetaArg().\r |
1328 | \r |
1e6e84c7 |
1329 | If you are NOT appending to an existing list *ListHead must be NULL. If\r |
5f7431d0 |
1330 | *ListHead is NULL then it must be callee freed.\r |
94b17fa1 |
1331 | \r |
1332 | @param Arg pointer to path string\r |
1333 | @param OpenMode mode to open files with\r |
1334 | @param ListHead head of linked list of results\r |
1335 | \r |
1e6e84c7 |
1336 | @retval EFI_SUCCESS the operation was sucessful and the list head\r |
94b17fa1 |
1337 | contains the list of opened files\r |
1338 | #retval EFI_UNSUPPORTED a previous ShellOpenFileMetaArg must be closed first.\r |
1339 | *ListHead is set to NULL.\r |
1340 | @return != EFI_SUCCESS the operation failed\r |
1341 | \r |
1342 | @sa InternalShellConvertFileListType\r |
1343 | **/\r |
1344 | EFI_STATUS\r |
1345 | EFIAPI\r |
1346 | ShellOpenFileMetaArg (\r |
1347 | IN CHAR16 *Arg,\r |
1348 | IN UINT64 OpenMode,\r |
1349 | IN OUT EFI_SHELL_FILE_INFO **ListHead\r |
1350 | )\r |
1351 | {\r |
1352 | EFI_STATUS Status;\r |
9b3bf083 |
1353 | LIST_ENTRY mOldStyleFileList;\r |
1e6e84c7 |
1354 | \r |
94b17fa1 |
1355 | //\r |
1356 | // ASSERT that Arg and ListHead are not NULL\r |
1357 | //\r |
1358 | ASSERT(Arg != NULL);\r |
1359 | ASSERT(ListHead != NULL);\r |
1360 | \r |
1e6e84c7 |
1361 | //\r |
94b17fa1 |
1362 | // Check for UEFI Shell 2.0 protocols\r |
1363 | //\r |
1364 | if (mEfiShellProtocol != NULL) {\r |
5f7431d0 |
1365 | if (*ListHead == NULL) {\r |
1366 | *ListHead = (EFI_SHELL_FILE_INFO*)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));\r |
1367 | if (*ListHead == NULL) {\r |
1368 | return (EFI_OUT_OF_RESOURCES);\r |
1369 | }\r |
1370 | InitializeListHead(&((*ListHead)->Link));\r |
1e6e84c7 |
1371 | }\r |
1372 | Status = mEfiShellProtocol->OpenFileList(Arg,\r |
1373 | OpenMode,\r |
2247dde4 |
1374 | ListHead);\r |
1375 | if (EFI_ERROR(Status)) {\r |
1376 | mEfiShellProtocol->RemoveDupInFileList(ListHead);\r |
1377 | } else {\r |
1378 | Status = mEfiShellProtocol->RemoveDupInFileList(ListHead);\r |
1379 | }\r |
1380 | return (Status);\r |
1e6e84c7 |
1381 | }\r |
94b17fa1 |
1382 | \r |
1383 | //\r |
1384 | // ASSERT that we must have EFI shell\r |
1385 | //\r |
1386 | ASSERT(mEfiShellEnvironment2 != NULL);\r |
1387 | \r |
94b17fa1 |
1388 | //\r |
1389 | // make sure the list head is initialized\r |
1390 | //\r |
9b3bf083 |
1391 | InitializeListHead(&mOldStyleFileList);\r |
94b17fa1 |
1392 | \r |
1393 | //\r |
1394 | // Get the EFI Shell list of files\r |
1395 | //\r |
9b3bf083 |
1396 | Status = mEfiShellEnvironment2->FileMetaArg(Arg, &mOldStyleFileList);\r |
94b17fa1 |
1397 | if (EFI_ERROR(Status)) {\r |
1398 | *ListHead = NULL;\r |
1399 | return (Status);\r |
1400 | }\r |
1401 | \r |
9b3bf083 |
1402 | if (*ListHead == NULL) {\r |
1403 | *ListHead = (EFI_SHELL_FILE_INFO *)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));\r |
1404 | if (*ListHead == NULL) {\r |
1405 | return (EFI_OUT_OF_RESOURCES);\r |
1406 | }\r |
1407 | }\r |
1408 | \r |
94b17fa1 |
1409 | //\r |
1410 | // Convert that to equivalent of UEFI Shell 2.0 structure\r |
1411 | //\r |
9b3bf083 |
1412 | InternalShellConvertFileListType(&mOldStyleFileList, &(*ListHead)->Link);\r |
94b17fa1 |
1413 | \r |
d2b4564b |
1414 | //\r |
1415 | // Free the EFI Shell version that was converted.\r |
1416 | //\r |
9b3bf083 |
1417 | mEfiShellEnvironment2->FreeFileList(&mOldStyleFileList);\r |
94b17fa1 |
1418 | \r |
1419 | return (Status);\r |
1420 | }\r |
1421 | /**\r |
1422 | Free the linked list returned from ShellOpenFileMetaArg\r |
1423 | \r |
1424 | if ListHead is NULL then ASSERT()\r |
1425 | \r |
1426 | @param ListHead the pointer to free\r |
1427 | \r |
1428 | @retval EFI_SUCCESS the operation was sucessful\r |
1429 | **/\r |
1430 | EFI_STATUS\r |
1431 | EFIAPI\r |
1432 | ShellCloseFileMetaArg (\r |
1433 | IN OUT EFI_SHELL_FILE_INFO **ListHead\r |
1434 | )\r |
1435 | {\r |
1436 | LIST_ENTRY *Node;\r |
1437 | \r |
1438 | //\r |
1439 | // ASSERT that ListHead is not NULL\r |
1440 | //\r |
1441 | ASSERT(ListHead != NULL);\r |
1442 | \r |
1e6e84c7 |
1443 | //\r |
94b17fa1 |
1444 | // Check for UEFI Shell 2.0 protocols\r |
1445 | //\r |
1446 | if (mEfiShellProtocol != NULL) {\r |
1447 | return (mEfiShellProtocol->FreeFileList(ListHead));\r |
1448 | } else {\r |
94b17fa1 |
1449 | //\r |
1e6e84c7 |
1450 | // Since this is EFI Shell version we need to free our internally made copy\r |
94b17fa1 |
1451 | // of the list\r |
1452 | //\r |
1e6e84c7 |
1453 | for ( Node = GetFirstNode(&(*ListHead)->Link)\r |
1454 | ; IsListEmpty(&(*ListHead)->Link) == FALSE\r |
9b3bf083 |
1455 | ; Node = GetFirstNode(&(*ListHead)->Link)) {\r |
94b17fa1 |
1456 | RemoveEntryList(Node);\r |
d2b4564b |
1457 | ((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->Handle->Close(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->Handle);\r |
94b17fa1 |
1458 | FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->FullName);\r |
1459 | FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->FileName);\r |
1460 | FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->Info);\r |
1461 | FreePool((EFI_SHELL_FILE_INFO_NO_CONST*)Node);\r |
1462 | }\r |
1463 | return EFI_SUCCESS;\r |
1464 | }\r |
1465 | }\r |
1466 | \r |
125c2cf4 |
1467 | /**\r |
1468 | Find a file by searching the CWD and then the path.\r |
1469 | \r |
b3011f40 |
1470 | If FileName is NULL then ASSERT.\r |
125c2cf4 |
1471 | \r |
b3011f40 |
1472 | If the return value is not NULL then the memory must be caller freed.\r |
125c2cf4 |
1473 | \r |
1474 | @param FileName Filename string.\r |
1475 | \r |
1476 | @retval NULL the file was not found\r |
1477 | @return !NULL the full path to the file.\r |
1478 | **/\r |
1479 | CHAR16 *\r |
1480 | EFIAPI\r |
1481 | ShellFindFilePath (\r |
1482 | IN CONST CHAR16 *FileName\r |
1483 | )\r |
1484 | {\r |
1485 | CONST CHAR16 *Path;\r |
1486 | EFI_FILE_HANDLE Handle;\r |
1487 | EFI_STATUS Status;\r |
1488 | CHAR16 *RetVal;\r |
1489 | CHAR16 *TestPath;\r |
1490 | CONST CHAR16 *Walker;\r |
36a9d672 |
1491 | UINTN Size;\r |
1cd45e78 |
1492 | CHAR16 *TempChar;\r |
125c2cf4 |
1493 | \r |
1494 | RetVal = NULL;\r |
1495 | \r |
1496 | Path = ShellGetEnvironmentVariable(L"cwd");\r |
1497 | if (Path != NULL) {\r |
36a9d672 |
1498 | Size = StrSize(Path);\r |
1499 | Size += StrSize(FileName);\r |
1500 | TestPath = AllocateZeroPool(Size);\r |
125c2cf4 |
1501 | StrCpy(TestPath, Path);\r |
1502 | StrCat(TestPath, FileName);\r |
1503 | Status = ShellOpenFileByName(TestPath, &Handle, EFI_FILE_MODE_READ, 0);\r |
1504 | if (!EFI_ERROR(Status)){\r |
1505 | RetVal = StrnCatGrow(&RetVal, NULL, TestPath, 0);\r |
1506 | ShellCloseFile(&Handle);\r |
1507 | FreePool(TestPath);\r |
1508 | return (RetVal);\r |
1509 | }\r |
1510 | FreePool(TestPath);\r |
1511 | }\r |
1512 | Path = ShellGetEnvironmentVariable(L"path");\r |
1513 | if (Path != NULL) {\r |
36a9d672 |
1514 | Size = StrSize(Path);\r |
1515 | Size += StrSize(FileName);\r |
1516 | TestPath = AllocateZeroPool(Size);\r |
1e6e84c7 |
1517 | Walker = (CHAR16*)Path;\r |
125c2cf4 |
1518 | do {\r |
1519 | CopyMem(TestPath, Walker, StrSize(Walker));\r |
1cd45e78 |
1520 | TempChar = StrStr(TestPath, L";");\r |
1521 | if (TempChar != NULL) {\r |
1522 | *TempChar = CHAR_NULL;\r |
125c2cf4 |
1523 | }\r |
1524 | StrCat(TestPath, FileName);\r |
1525 | if (StrStr(Walker, L";") != NULL) {\r |
1526 | Walker = StrStr(Walker, L";") + 1;\r |
1527 | } else {\r |
1528 | Walker = NULL;\r |
1529 | }\r |
1530 | Status = ShellOpenFileByName(TestPath, &Handle, EFI_FILE_MODE_READ, 0);\r |
1531 | if (!EFI_ERROR(Status)){\r |
1532 | RetVal = StrnCatGrow(&RetVal, NULL, TestPath, 0);\r |
1533 | ShellCloseFile(&Handle);\r |
1534 | break;\r |
1535 | }\r |
1536 | } while (Walker != NULL && Walker[0] != CHAR_NULL);\r |
1537 | FreePool(TestPath);\r |
1538 | }\r |
1539 | return (RetVal);\r |
1540 | }\r |
1541 | \r |
b3011f40 |
1542 | /**\r |
1e6e84c7 |
1543 | Find a file by searching the CWD and then the path with a variable set of file\r |
1544 | extensions. If the file is not found it will append each extension in the list\r |
b3011f40 |
1545 | in the order provided and return the first one that is successful.\r |
1546 | \r |
1547 | If FileName is NULL, then ASSERT.\r |
1548 | If FileExtension is NULL, then behavior is identical to ShellFindFilePath.\r |
1549 | \r |
1550 | If the return value is not NULL then the memory must be caller freed.\r |
1551 | \r |
1552 | @param[in] FileName Filename string.\r |
1553 | @param[in] FileExtension Semi-colon delimeted list of possible extensions.\r |
1554 | \r |
1555 | @retval NULL The file was not found.\r |
1556 | @retval !NULL The path to the file.\r |
1557 | **/\r |
1558 | CHAR16 *\r |
1559 | EFIAPI\r |
1560 | ShellFindFilePathEx (\r |
1561 | IN CONST CHAR16 *FileName,\r |
1562 | IN CONST CHAR16 *FileExtension\r |
1563 | )\r |
1564 | {\r |
1565 | CHAR16 *TestPath;\r |
1566 | CHAR16 *RetVal;\r |
1567 | CONST CHAR16 *ExtensionWalker;\r |
9e926b69 |
1568 | UINTN Size;\r |
1cd45e78 |
1569 | CHAR16 *TempChar;\r |
1570 | \r |
b3011f40 |
1571 | ASSERT(FileName != NULL);\r |
1572 | if (FileExtension == NULL) {\r |
1573 | return (ShellFindFilePath(FileName));\r |
1574 | }\r |
1575 | RetVal = ShellFindFilePath(FileName);\r |
1576 | if (RetVal != NULL) {\r |
1577 | return (RetVal);\r |
1578 | }\r |
9e926b69 |
1579 | Size = StrSize(FileName);\r |
1580 | Size += StrSize(FileExtension);\r |
1581 | TestPath = AllocateZeroPool(Size);\r |
b3011f40 |
1582 | for (ExtensionWalker = FileExtension ; ; ExtensionWalker = StrStr(ExtensionWalker, L";") + 1 ){\r |
1583 | StrCpy(TestPath, FileName);\r |
1cd45e78 |
1584 | if (ExtensionWalker != NULL) {\r |
1585 | StrCat(TestPath, ExtensionWalker);\r |
1586 | }\r |
1587 | TempChar = StrStr(TestPath, L";");\r |
1588 | if (TempChar != NULL) {\r |
1589 | *TempChar = CHAR_NULL;\r |
b3011f40 |
1590 | }\r |
1591 | RetVal = ShellFindFilePath(TestPath);\r |
1592 | if (RetVal != NULL) {\r |
1593 | break;\r |
1594 | }\r |
1595 | //\r |
1596 | // Must be after first loop...\r |
1597 | //\r |
1598 | if (StrStr(ExtensionWalker, L";") == NULL) {\r |
1599 | break;\r |
1600 | }\r |
1601 | }\r |
1602 | FreePool(TestPath);\r |
1603 | return (RetVal);\r |
1604 | }\r |
1605 | \r |
94b17fa1 |
1606 | typedef struct {\r |
9b3bf083 |
1607 | LIST_ENTRY Link;\r |
94b17fa1 |
1608 | CHAR16 *Name;\r |
1609 | ParamType Type;\r |
1610 | CHAR16 *Value;\r |
1611 | UINTN OriginalPosition;\r |
1612 | } SHELL_PARAM_PACKAGE;\r |
1613 | \r |
1614 | /**\r |
1e6e84c7 |
1615 | Checks the list of valid arguments and returns TRUE if the item was found. If the\r |
94b17fa1 |
1616 | return value is TRUE then the type parameter is set also.\r |
1e6e84c7 |
1617 | \r |
94b17fa1 |
1618 | if CheckList is NULL then ASSERT();\r |
1619 | if Name is NULL then ASSERT();\r |
1620 | if Type is NULL then ASSERT();\r |
1621 | \r |
1622 | @param Type pointer to type of parameter if it was found\r |
1623 | @param Name pointer to Name of parameter found\r |
1624 | @param CheckList List to check against\r |
1625 | \r |
1626 | @retval TRUE the Parameter was found. Type is valid.\r |
1627 | @retval FALSE the Parameter was not found. Type is not valid.\r |
1628 | **/\r |
1629 | BOOLEAN\r |
1630 | EFIAPI\r |
d2b4564b |
1631 | InternalIsOnCheckList (\r |
94b17fa1 |
1632 | IN CONST CHAR16 *Name,\r |
1633 | IN CONST SHELL_PARAM_ITEM *CheckList,\r |
1634 | OUT ParamType *Type\r |
2247dde4 |
1635 | ) {\r |
94b17fa1 |
1636 | SHELL_PARAM_ITEM *TempListItem;\r |
1637 | \r |
1638 | //\r |
1639 | // ASSERT that all 3 pointer parameters aren't NULL\r |
1640 | //\r |
1641 | ASSERT(CheckList != NULL);\r |
1642 | ASSERT(Type != NULL);\r |
1643 | ASSERT(Name != NULL);\r |
1644 | \r |
d2b4564b |
1645 | //\r |
1646 | // question mark and page break mode are always supported\r |
1647 | //\r |
1648 | if ((StrCmp(Name, L"-?") == 0) ||\r |
1649 | (StrCmp(Name, L"-b") == 0)\r |
1650 | ) {\r |
1651 | return (TRUE);\r |
1652 | }\r |
1653 | \r |
94b17fa1 |
1654 | //\r |
1655 | // Enumerate through the list\r |
1656 | //\r |
1657 | for (TempListItem = (SHELL_PARAM_ITEM*)CheckList ; TempListItem->Name != NULL ; TempListItem++) {\r |
1658 | //\r |
9eb53ac3 |
1659 | // If the Type is TypeStart only check the first characters of the passed in param\r |
1660 | // If it matches set the type and return TRUE\r |
94b17fa1 |
1661 | //\r |
9eb53ac3 |
1662 | if (TempListItem->Type == TypeStart && StrnCmp(Name, TempListItem->Name, StrLen(TempListItem->Name)) == 0) {\r |
1663 | *Type = TempListItem->Type;\r |
1664 | return (TRUE);\r |
1665 | } else if (StrCmp(Name, TempListItem->Name) == 0) {\r |
94b17fa1 |
1666 | *Type = TempListItem->Type;\r |
1667 | return (TRUE);\r |
1668 | }\r |
1669 | }\r |
2247dde4 |
1670 | \r |
94b17fa1 |
1671 | return (FALSE);\r |
1672 | }\r |
1673 | /**\r |
d2b4564b |
1674 | Checks the string for indicators of "flag" status. this is a leading '/', '-', or '+'\r |
94b17fa1 |
1675 | \r |
1676 | @param Name pointer to Name of parameter found\r |
1677 | \r |
1678 | @retval TRUE the Parameter is a flag.\r |
1679 | @retval FALSE the Parameter not a flag\r |
1680 | **/\r |
1681 | BOOLEAN\r |
1682 | EFIAPI\r |
d2b4564b |
1683 | InternalIsFlag (\r |
2247dde4 |
1684 | IN CONST CHAR16 *Name,\r |
1685 | IN BOOLEAN AlwaysAllowNumbers\r |
94b17fa1 |
1686 | )\r |
1687 | {\r |
1688 | //\r |
1689 | // ASSERT that Name isn't NULL\r |
1690 | //\r |
1691 | ASSERT(Name != NULL);\r |
1692 | \r |
2247dde4 |
1693 | //\r |
1694 | // If we accept numbers then dont return TRUE. (they will be values)\r |
1695 | //\r |
969c783b |
1696 | if (((Name[0] == L'-' || Name[0] == L'+') && ShellIsHexaDecimalDigitCharacter(Name[1])) && AlwaysAllowNumbers != FALSE) {\r |
2247dde4 |
1697 | return (FALSE);\r |
1698 | }\r |
1699 | \r |
94b17fa1 |
1700 | //\r |
1701 | // If the Name has a / or - as the first character return TRUE\r |
1702 | //\r |
1e6e84c7 |
1703 | if ((Name[0] == L'/') ||\r |
d2b4564b |
1704 | (Name[0] == L'-') ||\r |
1705 | (Name[0] == L'+')\r |
1706 | ) {\r |
94b17fa1 |
1707 | return (TRUE);\r |
1708 | }\r |
1709 | return (FALSE);\r |
1710 | }\r |
1711 | \r |
1712 | /**\r |
1e6e84c7 |
1713 | Checks the command line arguments passed against the list of valid ones.\r |
94b17fa1 |
1714 | \r |
1715 | If no initialization is required, then return RETURN_SUCCESS.\r |
1e6e84c7 |
1716 | \r |
94b17fa1 |
1717 | @param CheckList pointer to list of parameters to check\r |
1718 | @param CheckPackage pointer to pointer to list checked values\r |
1e6e84c7 |
1719 | @param ProblemParam optional pointer to pointer to unicode string for\r |
d2b4564b |
1720 | the paramater that caused failure. If used then the\r |
1721 | caller is responsible for freeing the memory.\r |
94b17fa1 |
1722 | @param AutoPageBreak will automatically set PageBreakEnabled for "b" parameter\r |
1723 | @param Argc Count of parameters in Argv\r |
1724 | @param Argv pointer to array of parameters\r |
1725 | \r |
1726 | @retval EFI_SUCCESS The operation completed sucessfully.\r |
1727 | @retval EFI_OUT_OF_RESOURCES A memory allocation failed\r |
1728 | @retval EFI_INVALID_PARAMETER A parameter was invalid\r |
1e6e84c7 |
1729 | @retval EFI_VOLUME_CORRUPTED the command line was corrupt. an argument was\r |
1730 | duplicated. the duplicated command line argument\r |
94b17fa1 |
1731 | was returned in ProblemParam if provided.\r |
1e6e84c7 |
1732 | @retval EFI_NOT_FOUND a argument required a value that was missing.\r |
94b17fa1 |
1733 | the invalid command line argument was returned in\r |
1734 | ProblemParam if provided.\r |
1735 | **/\r |
2247dde4 |
1736 | STATIC\r |
94b17fa1 |
1737 | EFI_STATUS\r |
1738 | EFIAPI\r |
1739 | InternalCommandLineParse (\r |
1740 | IN CONST SHELL_PARAM_ITEM *CheckList,\r |
1741 | OUT LIST_ENTRY **CheckPackage,\r |
1742 | OUT CHAR16 **ProblemParam OPTIONAL,\r |
1743 | IN BOOLEAN AutoPageBreak,\r |
1744 | IN CONST CHAR16 **Argv,\r |
2247dde4 |
1745 | IN UINTN Argc,\r |
1746 | IN BOOLEAN AlwaysAllowNumbers\r |
1747 | ) {\r |
94b17fa1 |
1748 | UINTN LoopCounter;\r |
94b17fa1 |
1749 | ParamType CurrentItemType;\r |
1750 | SHELL_PARAM_PACKAGE *CurrentItemPackage;\r |
125c2cf4 |
1751 | UINTN GetItemValue;\r |
1752 | UINTN ValueSize;\r |
94b17fa1 |
1753 | \r |
1754 | CurrentItemPackage = NULL;\r |
2247dde4 |
1755 | mTotalParameterCount = 0;\r |
125c2cf4 |
1756 | GetItemValue = 0;\r |
1757 | ValueSize = 0;\r |
94b17fa1 |
1758 | \r |
1759 | //\r |
1760 | // If there is only 1 item we dont need to do anything\r |
1761 | //\r |
1762 | if (Argc <= 1) {\r |
1763 | *CheckPackage = NULL;\r |
1764 | return (EFI_SUCCESS);\r |
1765 | }\r |
1766 | \r |
2247dde4 |
1767 | //\r |
1768 | // ASSERTs\r |
1769 | //\r |
1770 | ASSERT(CheckList != NULL);\r |
1771 | ASSERT(Argv != NULL);\r |
1772 | \r |
94b17fa1 |
1773 | //\r |
1774 | // initialize the linked list\r |
1775 | //\r |
1776 | *CheckPackage = (LIST_ENTRY*)AllocateZeroPool(sizeof(LIST_ENTRY));\r |
1777 | InitializeListHead(*CheckPackage);\r |
1778 | \r |
1779 | //\r |
1780 | // loop through each of the arguments\r |
1781 | //\r |
1782 | for (LoopCounter = 0 ; LoopCounter < Argc ; ++LoopCounter) {\r |
1783 | if (Argv[LoopCounter] == NULL) {\r |
1784 | //\r |
1785 | // do nothing for NULL argv\r |
1786 | //\r |
b3011f40 |
1787 | } else if (InternalIsOnCheckList(Argv[LoopCounter], CheckList, &CurrentItemType) != FALSE) {\r |
2247dde4 |
1788 | //\r |
1789 | // We might have leftover if last parameter didnt have optional value\r |
1790 | //\r |
125c2cf4 |
1791 | if (GetItemValue != 0) {\r |
1792 | GetItemValue = 0;\r |
2247dde4 |
1793 | InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);\r |
1794 | }\r |
94b17fa1 |
1795 | //\r |
1796 | // this is a flag\r |
1797 | //\r |
1798 | CurrentItemPackage = AllocatePool(sizeof(SHELL_PARAM_PACKAGE));\r |
1799 | ASSERT(CurrentItemPackage != NULL);\r |
1800 | CurrentItemPackage->Name = AllocatePool(StrSize(Argv[LoopCounter]));\r |
1801 | ASSERT(CurrentItemPackage->Name != NULL);\r |
1802 | StrCpy(CurrentItemPackage->Name, Argv[LoopCounter]);\r |
1803 | CurrentItemPackage->Type = CurrentItemType;\r |
1804 | CurrentItemPackage->OriginalPosition = (UINTN)(-1);\r |
b1f95a06 |
1805 | CurrentItemPackage->Value = NULL;\r |
94b17fa1 |
1806 | \r |
1807 | //\r |
1808 | // Does this flag require a value\r |
1809 | //\r |
125c2cf4 |
1810 | switch (CurrentItemPackage->Type) {\r |
94b17fa1 |
1811 | //\r |
125c2cf4 |
1812 | // possibly trigger the next loop(s) to populate the value of this item\r |
1e6e84c7 |
1813 | //\r |
125c2cf4 |
1814 | case TypeValue:\r |
1e6e84c7 |
1815 | GetItemValue = 1;\r |
125c2cf4 |
1816 | ValueSize = 0;\r |
1817 | break;\r |
1818 | case TypeDoubleValue:\r |
1819 | GetItemValue = 2;\r |
1820 | ValueSize = 0;\r |
1821 | break;\r |
1822 | case TypeMaxValue:\r |
1823 | GetItemValue = (UINTN)(-1);\r |
1824 | ValueSize = 0;\r |
1825 | break;\r |
1826 | default:\r |
1827 | //\r |
1828 | // this item has no value expected; we are done\r |
1829 | //\r |
1830 | InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);\r |
1831 | ASSERT(GetItemValue == 0);\r |
1832 | break;\r |
94b17fa1 |
1833 | }\r |
125c2cf4 |
1834 | } else if (GetItemValue != 0 && InternalIsFlag(Argv[LoopCounter], AlwaysAllowNumbers) == FALSE) {\r |
b1f95a06 |
1835 | ASSERT(CurrentItemPackage != NULL);\r |
1836 | //\r |
125c2cf4 |
1837 | // get the item VALUE for a previous flag\r |
b1f95a06 |
1838 | //\r |
125c2cf4 |
1839 | CurrentItemPackage->Value = ReallocatePool(ValueSize, ValueSize + StrSize(Argv[LoopCounter]) + sizeof(CHAR16), CurrentItemPackage->Value);\r |
b1f95a06 |
1840 | ASSERT(CurrentItemPackage->Value != NULL);\r |
125c2cf4 |
1841 | if (ValueSize == 0) {\r |
1842 | StrCpy(CurrentItemPackage->Value, Argv[LoopCounter]);\r |
1843 | } else {\r |
1844 | StrCat(CurrentItemPackage->Value, L" ");\r |
1845 | StrCat(CurrentItemPackage->Value, Argv[LoopCounter]);\r |
1846 | }\r |
1847 | ValueSize += StrSize(Argv[LoopCounter]) + sizeof(CHAR16);\r |
1848 | GetItemValue--;\r |
1849 | if (GetItemValue == 0) {\r |
1850 | InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);\r |
1851 | }\r |
2247dde4 |
1852 | } else if (InternalIsFlag(Argv[LoopCounter], AlwaysAllowNumbers) == FALSE) {\r |
b1f95a06 |
1853 | //\r |
1854 | // add this one as a non-flag\r |
1855 | //\r |
1856 | CurrentItemPackage = AllocatePool(sizeof(SHELL_PARAM_PACKAGE));\r |
1857 | ASSERT(CurrentItemPackage != NULL);\r |
1858 | CurrentItemPackage->Name = NULL;\r |
1859 | CurrentItemPackage->Type = TypePosition;\r |
1860 | CurrentItemPackage->Value = AllocatePool(StrSize(Argv[LoopCounter]));\r |
1861 | ASSERT(CurrentItemPackage->Value != NULL);\r |
1862 | StrCpy(CurrentItemPackage->Value, Argv[LoopCounter]);\r |
2247dde4 |
1863 | CurrentItemPackage->OriginalPosition = mTotalParameterCount++;\r |
9b3bf083 |
1864 | InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);\r |
94b17fa1 |
1865 | } else if (ProblemParam) {\r |
1866 | //\r |
1867 | // this was a non-recognised flag... error!\r |
1868 | //\r |
d2b4564b |
1869 | *ProblemParam = AllocatePool(StrSize(Argv[LoopCounter]));\r |
1870 | ASSERT(*ProblemParam != NULL);\r |
1871 | StrCpy(*ProblemParam, Argv[LoopCounter]);\r |
94b17fa1 |
1872 | ShellCommandLineFreeVarList(*CheckPackage);\r |
1873 | *CheckPackage = NULL;\r |
1874 | return (EFI_VOLUME_CORRUPTED);\r |
1875 | } else {\r |
1876 | ShellCommandLineFreeVarList(*CheckPackage);\r |
1877 | *CheckPackage = NULL;\r |
1878 | return (EFI_VOLUME_CORRUPTED);\r |
1879 | }\r |
1880 | }\r |
125c2cf4 |
1881 | if (GetItemValue != 0) {\r |
1882 | GetItemValue = 0;\r |
1883 | InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);\r |
1884 | }\r |
94b17fa1 |
1885 | //\r |
1886 | // support for AutoPageBreak\r |
1887 | //\r |
1888 | if (AutoPageBreak && ShellCommandLineGetFlag(*CheckPackage, L"-b")) {\r |
1889 | ShellSetPageBreakMode(TRUE);\r |
1890 | }\r |
1891 | return (EFI_SUCCESS);\r |
1892 | }\r |
1893 | \r |
1894 | /**\r |
1e6e84c7 |
1895 | Checks the command line arguments passed against the list of valid ones.\r |
94b17fa1 |
1896 | Optionally removes NULL values first.\r |
1e6e84c7 |
1897 | \r |
94b17fa1 |
1898 | If no initialization is required, then return RETURN_SUCCESS.\r |
1e6e84c7 |
1899 | \r |
94b17fa1 |
1900 | @param CheckList pointer to list of parameters to check\r |
1901 | @param CheckPackage pointer to pointer to list checked values\r |
1e6e84c7 |
1902 | @param ProblemParam optional pointer to pointer to unicode string for\r |
94b17fa1 |
1903 | the paramater that caused failure.\r |
1904 | @param AutoPageBreak will automatically set PageBreakEnabled for "b" parameter\r |
1905 | \r |
1906 | @retval EFI_SUCCESS The operation completed sucessfully.\r |
1907 | @retval EFI_OUT_OF_RESOURCES A memory allocation failed\r |
1908 | @retval EFI_INVALID_PARAMETER A parameter was invalid\r |
1e6e84c7 |
1909 | @retval EFI_VOLUME_CORRUPTED the command line was corrupt. an argument was\r |
1910 | duplicated. the duplicated command line argument\r |
94b17fa1 |
1911 | was returned in ProblemParam if provided.\r |
1912 | @retval EFI_DEVICE_ERROR the commands contained 2 opposing arguments. one\r |
1e6e84c7 |
1913 | of the command line arguments was returned in\r |
94b17fa1 |
1914 | ProblemParam if provided.\r |
1e6e84c7 |
1915 | @retval EFI_NOT_FOUND a argument required a value that was missing.\r |
94b17fa1 |
1916 | the invalid command line argument was returned in\r |
1917 | ProblemParam if provided.\r |
1918 | **/\r |
1919 | EFI_STATUS\r |
1920 | EFIAPI\r |
2247dde4 |
1921 | ShellCommandLineParseEx (\r |
94b17fa1 |
1922 | IN CONST SHELL_PARAM_ITEM *CheckList,\r |
1923 | OUT LIST_ENTRY **CheckPackage,\r |
1924 | OUT CHAR16 **ProblemParam OPTIONAL,\r |
2247dde4 |
1925 | IN BOOLEAN AutoPageBreak,\r |
1926 | IN BOOLEAN AlwaysAllowNumbers\r |
1927 | ) {\r |
1e6e84c7 |
1928 | //\r |
94b17fa1 |
1929 | // ASSERT that CheckList and CheckPackage aren't NULL\r |
1930 | //\r |
1931 | ASSERT(CheckList != NULL);\r |
1932 | ASSERT(CheckPackage != NULL);\r |
1933 | \r |
1e6e84c7 |
1934 | //\r |
94b17fa1 |
1935 | // Check for UEFI Shell 2.0 protocols\r |
1936 | //\r |
1937 | if (mEfiShellParametersProtocol != NULL) {\r |
1e6e84c7 |
1938 | return (InternalCommandLineParse(CheckList,\r |
1939 | CheckPackage,\r |
1940 | ProblemParam,\r |
1941 | AutoPageBreak,\r |
08d7f8e8 |
1942 | (CONST CHAR16**) mEfiShellParametersProtocol->Argv,\r |
2247dde4 |
1943 | mEfiShellParametersProtocol->Argc,\r |
1944 | AlwaysAllowNumbers));\r |
94b17fa1 |
1945 | }\r |
1946 | \r |
1e6e84c7 |
1947 | //\r |
94b17fa1 |
1948 | // ASSERT That EFI Shell is not required\r |
1949 | //\r |
1950 | ASSERT (mEfiShellInterface != NULL);\r |
1e6e84c7 |
1951 | return (InternalCommandLineParse(CheckList,\r |
1952 | CheckPackage,\r |
1953 | ProblemParam,\r |
1954 | AutoPageBreak,\r |
08d7f8e8 |
1955 | (CONST CHAR16**) mEfiShellInterface->Argv,\r |
2247dde4 |
1956 | mEfiShellInterface->Argc,\r |
1957 | AlwaysAllowNumbers));\r |
94b17fa1 |
1958 | }\r |
1959 | \r |
1960 | /**\r |
1961 | Frees shell variable list that was returned from ShellCommandLineParse.\r |
1962 | \r |
1963 | This function will free all the memory that was used for the CheckPackage\r |
1964 | list of postprocessed shell arguments.\r |
1965 | \r |
1966 | this function has no return value.\r |
1967 | \r |
1968 | if CheckPackage is NULL, then return\r |
1969 | \r |
1970 | @param CheckPackage the list to de-allocate\r |
1971 | **/\r |
1972 | VOID\r |
1973 | EFIAPI\r |
1974 | ShellCommandLineFreeVarList (\r |
1975 | IN LIST_ENTRY *CheckPackage\r |
2247dde4 |
1976 | ) {\r |
94b17fa1 |
1977 | LIST_ENTRY *Node;\r |
1978 | \r |
1979 | //\r |
1980 | // check for CheckPackage == NULL\r |
1981 | //\r |
1982 | if (CheckPackage == NULL) {\r |
1983 | return;\r |
1984 | }\r |
1985 | \r |
1986 | //\r |
1987 | // for each node in the list\r |
1988 | //\r |
9eb53ac3 |
1989 | for ( Node = GetFirstNode(CheckPackage)\r |
2247dde4 |
1990 | ; IsListEmpty(CheckPackage) == FALSE\r |
9eb53ac3 |
1991 | ; Node = GetFirstNode(CheckPackage)\r |
1992 | ){\r |
94b17fa1 |
1993 | //\r |
1994 | // Remove it from the list\r |
1995 | //\r |
1996 | RemoveEntryList(Node);\r |
1997 | \r |
1998 | //\r |
1999 | // if it has a name free the name\r |
2000 | //\r |
2001 | if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {\r |
2002 | FreePool(((SHELL_PARAM_PACKAGE*)Node)->Name);\r |
2003 | }\r |
2004 | \r |
2005 | //\r |
2006 | // if it has a value free the value\r |
2007 | //\r |
2008 | if (((SHELL_PARAM_PACKAGE*)Node)->Value != NULL) {\r |
2009 | FreePool(((SHELL_PARAM_PACKAGE*)Node)->Value);\r |
2010 | }\r |
1e6e84c7 |
2011 | \r |
94b17fa1 |
2012 | //\r |
2013 | // free the node structure\r |
2014 | //\r |
2015 | FreePool((SHELL_PARAM_PACKAGE*)Node);\r |
2016 | }\r |
2017 | //\r |
2018 | // free the list head node\r |
2019 | //\r |
2020 | FreePool(CheckPackage);\r |
2021 | }\r |
2022 | /**\r |
2023 | Checks for presence of a flag parameter\r |
2024 | \r |
2025 | flag arguments are in the form of "-<Key>" or "/<Key>", but do not have a value following the key\r |
2026 | \r |
2027 | if CheckPackage is NULL then return FALSE.\r |
2028 | if KeyString is NULL then ASSERT()\r |
1e6e84c7 |
2029 | \r |
94b17fa1 |
2030 | @param CheckPackage The package of parsed command line arguments\r |
2031 | @param KeyString the Key of the command line argument to check for\r |
2032 | \r |
2033 | @retval TRUE the flag is on the command line\r |
2034 | @retval FALSE the flag is not on the command line\r |
2035 | **/\r |
2036 | BOOLEAN\r |
2037 | EFIAPI\r |
2038 | ShellCommandLineGetFlag (\r |
2039 | IN CONST LIST_ENTRY *CheckPackage,\r |
2040 | IN CHAR16 *KeyString\r |
2247dde4 |
2041 | ) {\r |
94b17fa1 |
2042 | LIST_ENTRY *Node;\r |
2043 | \r |
2044 | //\r |
2045 | // ASSERT that both CheckPackage and KeyString aren't NULL\r |
2046 | //\r |
2047 | ASSERT(KeyString != NULL);\r |
2048 | \r |
2049 | //\r |
2050 | // return FALSE for no package\r |
2051 | //\r |
2052 | if (CheckPackage == NULL) {\r |
2053 | return (FALSE);\r |
2054 | }\r |
2055 | \r |
2056 | //\r |
2057 | // enumerate through the list of parametrs\r |
2058 | //\r |
1e6e84c7 |
2059 | for ( Node = GetFirstNode(CheckPackage)\r |
2060 | ; !IsNull (CheckPackage, Node)\r |
2061 | ; Node = GetNextNode(CheckPackage, Node)\r |
9eb53ac3 |
2062 | ){\r |
94b17fa1 |
2063 | //\r |
2064 | // If the Name matches, return TRUE (and there may be NULL name)\r |
2065 | //\r |
2066 | if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {\r |
9eb53ac3 |
2067 | //\r |
2068 | // If Type is TypeStart then only compare the begining of the strings\r |
2069 | //\r |
1e6e84c7 |
2070 | if ( ((SHELL_PARAM_PACKAGE*)Node)->Type == TypeStart\r |
9eb53ac3 |
2071 | && StrnCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name, StrLen(KeyString)) == 0\r |
2072 | ){\r |
2073 | return (TRUE);\r |
2074 | } else if (StrCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {\r |
94b17fa1 |
2075 | return (TRUE);\r |
2076 | }\r |
2077 | }\r |
2078 | }\r |
2079 | return (FALSE);\r |
2080 | }\r |
2081 | /**\r |
2082 | returns value from command line argument\r |
2083 | \r |
2084 | value parameters are in the form of "-<Key> value" or "/<Key> value"\r |
1e6e84c7 |
2085 | \r |
94b17fa1 |
2086 | if CheckPackage is NULL, then return NULL;\r |
2087 | \r |
2088 | @param CheckPackage The package of parsed command line arguments\r |
2089 | @param KeyString the Key of the command line argument to check for\r |
2090 | \r |
2091 | @retval NULL the flag is not on the command line\r |
2092 | @return !=NULL pointer to unicode string of the value\r |
2093 | **/\r |
2094 | CONST CHAR16*\r |
2095 | EFIAPI\r |
2096 | ShellCommandLineGetValue (\r |
2097 | IN CONST LIST_ENTRY *CheckPackage,\r |
2098 | IN CHAR16 *KeyString\r |
2247dde4 |
2099 | ) {\r |
94b17fa1 |
2100 | LIST_ENTRY *Node;\r |
2101 | \r |
2102 | //\r |
2103 | // check for CheckPackage == NULL\r |
2104 | //\r |
2105 | if (CheckPackage == NULL) {\r |
2106 | return (NULL);\r |
2107 | }\r |
2108 | \r |
2109 | //\r |
2110 | // enumerate through the list of parametrs\r |
2111 | //\r |
1e6e84c7 |
2112 | for ( Node = GetFirstNode(CheckPackage)\r |
2113 | ; !IsNull (CheckPackage, Node)\r |
2114 | ; Node = GetNextNode(CheckPackage, Node)\r |
9eb53ac3 |
2115 | ){\r |
94b17fa1 |
2116 | //\r |
2117 | // If the Name matches, return the value (name can be NULL)\r |
2118 | //\r |
2119 | if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {\r |
9eb53ac3 |
2120 | //\r |
2121 | // If Type is TypeStart then only compare the begining of the strings\r |
2122 | //\r |
1e6e84c7 |
2123 | if ( ((SHELL_PARAM_PACKAGE*)Node)->Type == TypeStart\r |
9eb53ac3 |
2124 | && StrnCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name, StrLen(KeyString)) == 0\r |
2125 | ){\r |
2126 | //\r |
2127 | // return the string part after the flag\r |
2128 | //\r |
2129 | return (((SHELL_PARAM_PACKAGE*)Node)->Name + StrLen(KeyString));\r |
2130 | } else if (StrCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {\r |
2131 | //\r |
2132 | // return the value\r |
2133 | //\r |
94b17fa1 |
2134 | return (((SHELL_PARAM_PACKAGE*)Node)->Value);\r |
2135 | }\r |
2136 | }\r |
2137 | }\r |
2138 | return (NULL);\r |
2139 | }\r |
2140 | /**\r |
2141 | returns raw value from command line argument\r |
2142 | \r |
2143 | raw value parameters are in the form of "value" in a specific position in the list\r |
1e6e84c7 |
2144 | \r |
94b17fa1 |
2145 | if CheckPackage is NULL, then return NULL;\r |
2146 | \r |
2147 | @param CheckPackage The package of parsed command line arguments\r |
1e6e84c7 |
2148 | @param Position the position of the value\r |
94b17fa1 |
2149 | \r |
2150 | @retval NULL the flag is not on the command line\r |
2151 | @return !=NULL pointer to unicode string of the value\r |
2152 | **/\r |
2153 | CONST CHAR16*\r |
2154 | EFIAPI\r |
2155 | ShellCommandLineGetRawValue (\r |
2156 | IN CONST LIST_ENTRY *CheckPackage,\r |
2157 | IN UINT32 Position\r |
2247dde4 |
2158 | ) {\r |
94b17fa1 |
2159 | LIST_ENTRY *Node;\r |
2160 | \r |
2161 | //\r |
2162 | // check for CheckPackage == NULL\r |
2163 | //\r |
2164 | if (CheckPackage == NULL) {\r |
2165 | return (NULL);\r |
2166 | }\r |
2167 | \r |
2168 | //\r |
2169 | // enumerate through the list of parametrs\r |
2170 | //\r |
1e6e84c7 |
2171 | for ( Node = GetFirstNode(CheckPackage)\r |
2172 | ; !IsNull (CheckPackage, Node)\r |
2173 | ; Node = GetNextNode(CheckPackage, Node)\r |
b82bfcc1 |
2174 | ){\r |
94b17fa1 |
2175 | //\r |
2176 | // If the position matches, return the value\r |
2177 | //\r |
2178 | if (((SHELL_PARAM_PACKAGE*)Node)->OriginalPosition == Position) {\r |
2179 | return (((SHELL_PARAM_PACKAGE*)Node)->Value);\r |
2180 | }\r |
2181 | }\r |
2182 | return (NULL);\r |
b1f95a06 |
2183 | }\r |
2247dde4 |
2184 | \r |
2185 | /**\r |
1e6e84c7 |
2186 | returns the number of command line value parameters that were parsed.\r |
2187 | \r |
2247dde4 |
2188 | this will not include flags.\r |
2189 | \r |
2190 | @retval (UINTN)-1 No parsing has ocurred\r |
2191 | @return other The number of value parameters found\r |
2192 | **/\r |
2193 | UINTN\r |
2194 | EFIAPI\r |
2195 | ShellCommandLineGetCount(\r |
2196 | VOID\r |
125c2cf4 |
2197 | )\r |
2198 | {\r |
2247dde4 |
2199 | return (mTotalParameterCount);\r |
2200 | }\r |
2201 | \r |
36a9d672 |
2202 | /**\r |
2203 | Determins if a parameter is duplicated.\r |
2204 | \r |
1e6e84c7 |
2205 | If Param is not NULL then it will point to a callee allocated string buffer\r |
36a9d672 |
2206 | with the parameter value if a duplicate is found.\r |
2207 | \r |
2208 | If CheckPackage is NULL, then ASSERT.\r |
2209 | \r |
2210 | @param[in] CheckPackage The package of parsed command line arguments.\r |
2211 | @param[out] Param Upon finding one, a pointer to the duplicated parameter.\r |
2212 | \r |
2213 | @retval EFI_SUCCESS No parameters were duplicated.\r |
2214 | @retval EFI_DEVICE_ERROR A duplicate was found.\r |
2215 | **/\r |
2216 | EFI_STATUS\r |
2217 | EFIAPI\r |
2218 | ShellCommandLineCheckDuplicate (\r |
2219 | IN CONST LIST_ENTRY *CheckPackage,\r |
2220 | OUT CHAR16 **Param\r |
2221 | )\r |
2222 | {\r |
2223 | LIST_ENTRY *Node1;\r |
2224 | LIST_ENTRY *Node2;\r |
1e6e84c7 |
2225 | \r |
36a9d672 |
2226 | ASSERT(CheckPackage != NULL);\r |
2227 | \r |
1e6e84c7 |
2228 | for ( Node1 = GetFirstNode(CheckPackage)\r |
2229 | ; !IsNull (CheckPackage, Node1)\r |
2230 | ; Node1 = GetNextNode(CheckPackage, Node1)\r |
36a9d672 |
2231 | ){\r |
1e6e84c7 |
2232 | for ( Node2 = GetNextNode(CheckPackage, Node1)\r |
2233 | ; !IsNull (CheckPackage, Node2)\r |
2234 | ; Node2 = GetNextNode(CheckPackage, Node2)\r |
36a9d672 |
2235 | ){\r |
2236 | if (StrCmp(((SHELL_PARAM_PACKAGE*)Node1)->Name, ((SHELL_PARAM_PACKAGE*)Node2)->Name) == 0) {\r |
2237 | if (Param != NULL) {\r |
2238 | *Param = NULL;\r |
2239 | *Param = StrnCatGrow(Param, NULL, ((SHELL_PARAM_PACKAGE*)Node1)->Name, 0);\r |
2240 | }\r |
2241 | return (EFI_DEVICE_ERROR);\r |
2242 | }\r |
2243 | }\r |
2244 | }\r |
2245 | return (EFI_SUCCESS);\r |
2246 | }\r |
2247 | \r |
975136ab |
2248 | /**\r |
1e6e84c7 |
2249 | This is a find and replace function. Upon successful return the NewString is a copy of\r |
975136ab |
2250 | SourceString with each instance of FindTarget replaced with ReplaceWith.\r |
2251 | \r |
b3011f40 |
2252 | If SourceString and NewString overlap the behavior is undefined.\r |
2253 | \r |
975136ab |
2254 | If the string would grow bigger than NewSize it will halt and return error.\r |
2255 | \r |
2256 | @param[in] SourceString String with source buffer\r |
b82bfcc1 |
2257 | @param[in,out] NewString String with resultant buffer\r |
975136ab |
2258 | @param[in] NewSize Size in bytes of NewString\r |
2259 | @param[in] FindTarget String to look for\r |
2260 | @param[in[ ReplaceWith String to replace FindTarget with\r |
969c783b |
2261 | @param[in] SkipPreCarrot If TRUE will skip a FindTarget that has a '^'\r |
2262 | immediately before it.\r |
2263 | \r |
2264 | @retval EFI_INVALID_PARAMETER SourceString was NULL.\r |
2265 | @retval EFI_INVALID_PARAMETER NewString was NULL.\r |
2266 | @retval EFI_INVALID_PARAMETER FindTarget was NULL.\r |
2267 | @retval EFI_INVALID_PARAMETER ReplaceWith was NULL.\r |
2268 | @retval EFI_INVALID_PARAMETER FindTarget had length < 1.\r |
2269 | @retval EFI_INVALID_PARAMETER SourceString had length < 1.\r |
1e6e84c7 |
2270 | @retval EFI_BUFFER_TOO_SMALL NewSize was less than the minimum size to hold\r |
969c783b |
2271 | the new string (truncation occurred).\r |
2272 | @retval EFI_SUCCESS the string was sucessfully copied with replacement.\r |
975136ab |
2273 | **/\r |
2274 | \r |
2275 | EFI_STATUS\r |
2276 | EFIAPI\r |
969c783b |
2277 | ShellCopySearchAndReplace2(\r |
975136ab |
2278 | IN CHAR16 CONST *SourceString,\r |
2279 | IN CHAR16 *NewString,\r |
2280 | IN UINTN NewSize,\r |
2281 | IN CONST CHAR16 *FindTarget,\r |
969c783b |
2282 | IN CONST CHAR16 *ReplaceWith,\r |
2283 | IN CONST BOOLEAN SkipPreCarrot\r |
1e6e84c7 |
2284 | )\r |
2247dde4 |
2285 | {\r |
0158294b |
2286 | UINTN Size;\r |
975136ab |
2287 | if ( (SourceString == NULL)\r |
2288 | || (NewString == NULL)\r |
2289 | || (FindTarget == NULL)\r |
2290 | || (ReplaceWith == NULL)\r |
2291 | || (StrLen(FindTarget) < 1)\r |
2292 | || (StrLen(SourceString) < 1)\r |
2293 | ){\r |
2294 | return (EFI_INVALID_PARAMETER);\r |
2295 | }\r |
2247dde4 |
2296 | NewString = SetMem16(NewString, NewSize, CHAR_NULL);\r |
2297 | while (*SourceString != CHAR_NULL) {\r |
969c783b |
2298 | //\r |
1e6e84c7 |
2299 | // if we find the FindTarget and either Skip == FALSE or Skip == TRUE and we\r |
969c783b |
2300 | // dont have a carrot do a replace...\r |
2301 | //\r |
1e6e84c7 |
2302 | if (StrnCmp(SourceString, FindTarget, StrLen(FindTarget)) == 0\r |
969c783b |
2303 | && ((SkipPreCarrot && *(SourceString-1) != L'^') || SkipPreCarrot == FALSE)\r |
2304 | ){\r |
975136ab |
2305 | SourceString += StrLen(FindTarget);\r |
0158294b |
2306 | Size = StrSize(NewString);\r |
2307 | if ((Size + (StrLen(ReplaceWith)*sizeof(CHAR16))) > NewSize) {\r |
975136ab |
2308 | return (EFI_BUFFER_TOO_SMALL);\r |
2309 | }\r |
2310 | StrCat(NewString, ReplaceWith);\r |
2311 | } else {\r |
0158294b |
2312 | Size = StrSize(NewString);\r |
2313 | if (Size + sizeof(CHAR16) > NewSize) {\r |
975136ab |
2314 | return (EFI_BUFFER_TOO_SMALL);\r |
2315 | }\r |
2316 | StrnCat(NewString, SourceString, 1);\r |
2317 | SourceString++;\r |
2318 | }\r |
2319 | }\r |
2320 | return (EFI_SUCCESS);\r |
2321 | }\r |
b1f95a06 |
2322 | \r |
e2f8297f |
2323 | /**\r |
2324 | Internal worker function to output a string.\r |
2325 | \r |
2326 | This function will output a string to the correct StdOut.\r |
2327 | \r |
2328 | @param[in] String The string to print out.\r |
2329 | \r |
2330 | @retval EFI_SUCCESS The operation was sucessful.\r |
2331 | @retval !EFI_SUCCESS The operation failed.\r |
2332 | **/\r |
2333 | EFI_STATUS\r |
2334 | EFIAPI\r |
2335 | InternalPrintTo (\r |
2336 | IN CONST CHAR16 *String\r |
2337 | )\r |
2338 | {\r |
2339 | UINTN Size;\r |
2340 | Size = StrSize(String) - sizeof(CHAR16);\r |
2341 | if (mEfiShellParametersProtocol != NULL) {\r |
2342 | return (mEfiShellParametersProtocol->StdOut->Write(mEfiShellParametersProtocol->StdOut, &Size, (VOID*)String));\r |
2343 | }\r |
2344 | if (mEfiShellInterface != NULL) {\r |
ecd3d59f |
2345 | //\r |
2346 | // Divide in half for old shell. Must be string length not size.\r |
2347 | //\r |
2348 | Size /= 2;\r |
e2f8297f |
2349 | return ( mEfiShellInterface->StdOut->Write(mEfiShellInterface->StdOut, &Size, (VOID*)String));\r |
2350 | }\r |
2351 | ASSERT(FALSE);\r |
2352 | return (EFI_UNSUPPORTED);\r |
2353 | }\r |
2354 | \r |
b1f95a06 |
2355 | /**\r |
2356 | Print at a specific location on the screen.\r |
2357 | \r |
f1b87e7a |
2358 | This function will move the cursor to a given screen location and print the specified string\r |
1e6e84c7 |
2359 | \r |
2360 | If -1 is specified for either the Row or Col the current screen location for BOTH\r |
f1b87e7a |
2361 | will be used.\r |
b1f95a06 |
2362 | \r |
2363 | if either Row or Col is out of range for the current console, then ASSERT\r |
2364 | if Format is NULL, then ASSERT\r |
2365 | \r |
1e6e84c7 |
2366 | In addition to the standard %-based flags as supported by UefiLib Print() this supports\r |
b1f95a06 |
2367 | the following additional flags:\r |
2368 | %N - Set output attribute to normal\r |
2369 | %H - Set output attribute to highlight\r |
2370 | %E - Set output attribute to error\r |
2371 | %B - Set output attribute to blue color\r |
2372 | %V - Set output attribute to green color\r |
2373 | \r |
2374 | Note: The background color is controlled by the shell command cls.\r |
2375 | \r |
2376 | @param[in] Row the row to print at\r |
2377 | @param[in] Col the column to print at\r |
2378 | @param[in] Format the format string\r |
2247dde4 |
2379 | @param[in] Marker the marker for the variable argument list\r |
b1f95a06 |
2380 | \r |
2381 | @return the number of characters printed to the screen\r |
2382 | **/\r |
2383 | \r |
2384 | UINTN\r |
2385 | EFIAPI\r |
2247dde4 |
2386 | InternalShellPrintWorker(\r |
b1f95a06 |
2387 | IN INT32 Col OPTIONAL,\r |
2388 | IN INT32 Row OPTIONAL,\r |
2389 | IN CONST CHAR16 *Format,\r |
2247dde4 |
2390 | VA_LIST Marker\r |
1e6e84c7 |
2391 | )\r |
2247dde4 |
2392 | {\r |
b1f95a06 |
2393 | UINTN Return;\r |
b1f95a06 |
2394 | EFI_STATUS Status;\r |
975136ab |
2395 | UINTN NormalAttribute;\r |
2396 | CHAR16 *ResumeLocation;\r |
2397 | CHAR16 *FormatWalker;\r |
1e6e84c7 |
2398 | \r |
975136ab |
2399 | //\r |
2400 | // Back and forth each time fixing up 1 of our flags...\r |
2401 | //\r |
b3011f40 |
2402 | Status = ShellLibCopySearchAndReplace(Format, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%N", L"%%N");\r |
975136ab |
2403 | ASSERT_EFI_ERROR(Status);\r |
b3011f40 |
2404 | Status = ShellLibCopySearchAndReplace(mPostReplaceFormat, mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), L"%E", L"%%E");\r |
975136ab |
2405 | ASSERT_EFI_ERROR(Status);\r |
b3011f40 |
2406 | Status = ShellLibCopySearchAndReplace(mPostReplaceFormat2, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%H", L"%%H");\r |
975136ab |
2407 | ASSERT_EFI_ERROR(Status);\r |
b3011f40 |
2408 | Status = ShellLibCopySearchAndReplace(mPostReplaceFormat, mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), L"%B", L"%%B");\r |
975136ab |
2409 | ASSERT_EFI_ERROR(Status);\r |
b3011f40 |
2410 | Status = ShellLibCopySearchAndReplace(mPostReplaceFormat2, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%V", L"%%V");\r |
975136ab |
2411 | ASSERT_EFI_ERROR(Status);\r |
2412 | \r |
2413 | //\r |
2414 | // Use the last buffer from replacing to print from...\r |
2415 | //\r |
b3011f40 |
2416 | Return = UnicodeVSPrint (mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), mPostReplaceFormat, Marker);\r |
b1f95a06 |
2417 | \r |
2418 | if (Col != -1 && Row != -1) {\r |
b1f95a06 |
2419 | Status = gST->ConOut->SetCursorPosition(gST->ConOut, Col, Row);\r |
2420 | ASSERT_EFI_ERROR(Status);\r |
975136ab |
2421 | }\r |
2422 | \r |
2423 | NormalAttribute = gST->ConOut->Mode->Attribute;\r |
ecd3d59f |
2424 | FormatWalker = mPostReplaceFormat2;\r |
2247dde4 |
2425 | while (*FormatWalker != CHAR_NULL) {\r |
975136ab |
2426 | //\r |
2427 | // Find the next attribute change request\r |
2428 | //\r |
2429 | ResumeLocation = StrStr(FormatWalker, L"%");\r |
2430 | if (ResumeLocation != NULL) {\r |
2247dde4 |
2431 | *ResumeLocation = CHAR_NULL;\r |
975136ab |
2432 | }\r |
2433 | //\r |
2434 | // print the current FormatWalker string\r |
2435 | //\r |
e2f8297f |
2436 | Status = InternalPrintTo(FormatWalker);\r |
b1f95a06 |
2437 | ASSERT_EFI_ERROR(Status);\r |
975136ab |
2438 | //\r |
2439 | // update the attribute\r |
2440 | //\r |
2441 | if (ResumeLocation != NULL) {\r |
2442 | switch (*(ResumeLocation+1)) {\r |
2443 | case (L'N'):\r |
2444 | gST->ConOut->SetAttribute(gST->ConOut, NormalAttribute);\r |
2445 | break;\r |
2446 | case (L'E'):\r |
2447 | gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_YELLOW, ((NormalAttribute&(BIT4|BIT5|BIT6))>>4)));\r |
2448 | break;\r |
2449 | case (L'H'):\r |
2450 | gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_WHITE, ((NormalAttribute&(BIT4|BIT5|BIT6))>>4)));\r |
2451 | break;\r |
2452 | case (L'B'):\r |
2453 | gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_BLUE, ((NormalAttribute&(BIT4|BIT5|BIT6))>>4)));\r |
2454 | break;\r |
2455 | case (L'V'):\r |
2456 | gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_GREEN, ((NormalAttribute&(BIT4|BIT5|BIT6))>>4)));\r |
2457 | break;\r |
2458 | default:\r |
e2f8297f |
2459 | //\r |
2460 | // Print a simple '%' symbol\r |
2461 | //\r |
2462 | Status = InternalPrintTo(L"%");\r |
2463 | ASSERT_EFI_ERROR(Status);\r |
2464 | ResumeLocation = ResumeLocation - 1;\r |
975136ab |
2465 | break;\r |
2466 | }\r |
2467 | } else {\r |
2468 | //\r |
2469 | // reset to normal now...\r |
2470 | //\r |
2471 | gST->ConOut->SetAttribute(gST->ConOut, NormalAttribute);\r |
2472 | break;\r |
2473 | }\r |
2474 | \r |
2475 | //\r |
2476 | // update FormatWalker to Resume + 2 (skip the % and the indicator)\r |
2477 | //\r |
2478 | FormatWalker = ResumeLocation + 2;\r |
2479 | }\r |
b1f95a06 |
2480 | \r |
b1f95a06 |
2481 | return (Return);\r |
5f7431d0 |
2482 | }\r |
2247dde4 |
2483 | \r |
2484 | /**\r |
2485 | Print at a specific location on the screen.\r |
2486 | \r |
e2f8297f |
2487 | This function will move the cursor to a given screen location and print the specified string.\r |
1e6e84c7 |
2488 | \r |
2489 | If -1 is specified for either the Row or Col the current screen location for BOTH\r |
2247dde4 |
2490 | will be used.\r |
2491 | \r |
e2f8297f |
2492 | If either Row or Col is out of range for the current console, then ASSERT.\r |
2493 | If Format is NULL, then ASSERT.\r |
2247dde4 |
2494 | \r |
1e6e84c7 |
2495 | In addition to the standard %-based flags as supported by UefiLib Print() this supports\r |
2247dde4 |
2496 | the following additional flags:\r |
2497 | %N - Set output attribute to normal\r |
2498 | %H - Set output attribute to highlight\r |
2499 | %E - Set output attribute to error\r |
2500 | %B - Set output attribute to blue color\r |
2501 | %V - Set output attribute to green color\r |
2502 | \r |
2503 | Note: The background color is controlled by the shell command cls.\r |
2504 | \r |
2505 | @param[in] Row the row to print at\r |
2506 | @param[in] Col the column to print at\r |
2507 | @param[in] Format the format string\r |
2508 | \r |
2509 | @return the number of characters printed to the screen\r |
2510 | **/\r |
2511 | \r |
2512 | UINTN\r |
2513 | EFIAPI\r |
2514 | ShellPrintEx(\r |
2515 | IN INT32 Col OPTIONAL,\r |
2516 | IN INT32 Row OPTIONAL,\r |
2517 | IN CONST CHAR16 *Format,\r |
2518 | ...\r |
1e6e84c7 |
2519 | )\r |
2247dde4 |
2520 | {\r |
2521 | VA_LIST Marker;\r |
e2f8297f |
2522 | EFI_STATUS Status;\r |
2247dde4 |
2523 | VA_START (Marker, Format);\r |
e2f8297f |
2524 | Status = InternalShellPrintWorker(Col, Row, Format, Marker);\r |
2525 | VA_END(Marker);\r |
2526 | return(Status);\r |
2247dde4 |
2527 | }\r |
2528 | \r |
2529 | /**\r |
2530 | Print at a specific location on the screen.\r |
2531 | \r |
e2f8297f |
2532 | This function will move the cursor to a given screen location and print the specified string.\r |
1e6e84c7 |
2533 | \r |
2534 | If -1 is specified for either the Row or Col the current screen location for BOTH\r |
e2f8297f |
2535 | will be used.\r |
2247dde4 |
2536 | \r |
e2f8297f |
2537 | If either Row or Col is out of range for the current console, then ASSERT.\r |
2538 | If Format is NULL, then ASSERT.\r |
2247dde4 |
2539 | \r |
1e6e84c7 |
2540 | In addition to the standard %-based flags as supported by UefiLib Print() this supports\r |
2247dde4 |
2541 | the following additional flags:\r |
1e6e84c7 |
2542 | %N - Set output attribute to normal.\r |
2543 | %H - Set output attribute to highlight.\r |
2544 | %E - Set output attribute to error.\r |
2545 | %B - Set output attribute to blue color.\r |
2546 | %V - Set output attribute to green color.\r |
2247dde4 |
2547 | \r |
2548 | Note: The background color is controlled by the shell command cls.\r |
2549 | \r |
1e6e84c7 |
2550 | @param[in] Row The row to print at.\r |
2551 | @param[in] Col The column to print at.\r |
2552 | @param[in] Language The language of the string to retrieve. If this parameter\r |
2553 | is NULL, then the current platform language is used.\r |
2554 | @param[in] HiiFormatStringId The format string Id for getting from Hii.\r |
2555 | @param[in] HiiFormatHandle The format string Handle for getting from Hii.\r |
2247dde4 |
2556 | \r |
1e6e84c7 |
2557 | @return the number of characters printed to the screen.\r |
2247dde4 |
2558 | **/\r |
2559 | UINTN\r |
2560 | EFIAPI\r |
2561 | ShellPrintHiiEx(\r |
2562 | IN INT32 Col OPTIONAL,\r |
2563 | IN INT32 Row OPTIONAL,\r |
1e6e84c7 |
2564 | IN CONST CHAR8 *Language OPTIONAL,\r |
2247dde4 |
2565 | IN CONST EFI_STRING_ID HiiFormatStringId,\r |
2566 | IN CONST EFI_HANDLE HiiFormatHandle,\r |
2567 | ...\r |
2568 | )\r |
2569 | {\r |
2570 | VA_LIST Marker;\r |
2571 | CHAR16 *HiiFormatString;\r |
2572 | UINTN RetVal;\r |
2573 | \r |
2574 | VA_START (Marker, HiiFormatHandle);\r |
1e6e84c7 |
2575 | HiiFormatString = HiiGetString(HiiFormatHandle, HiiFormatStringId, Language);\r |
2247dde4 |
2576 | ASSERT(HiiFormatString != NULL);\r |
2577 | \r |
2578 | RetVal = InternalShellPrintWorker(Col, Row, HiiFormatString, Marker);\r |
2579 | \r |
2580 | FreePool(HiiFormatString);\r |
e2f8297f |
2581 | VA_END(Marker);\r |
2247dde4 |
2582 | \r |
2583 | return (RetVal);\r |
2584 | }\r |
2585 | \r |
2586 | /**\r |
2587 | Function to determine if a given filename represents a file or a directory.\r |
2588 | \r |
2589 | @param[in] DirName Path to directory to test.\r |
2590 | \r |
2591 | @retval EFI_SUCCESS The Path represents a directory\r |
2592 | @retval EFI_NOT_FOUND The Path does not represent a directory\r |
2593 | @return other The path failed to open\r |
2594 | **/\r |
2595 | EFI_STATUS\r |
2596 | EFIAPI\r |
2597 | ShellIsDirectory(\r |
2598 | IN CONST CHAR16 *DirName\r |
2599 | )\r |
2600 | {\r |
2601 | EFI_STATUS Status;\r |
2602 | EFI_FILE_HANDLE Handle;\r |
2603 | \r |
ecd3d59f |
2604 | ASSERT(DirName != NULL);\r |
2605 | \r |
2247dde4 |
2606 | Handle = NULL;\r |
2607 | \r |
2608 | Status = ShellOpenFileByName(DirName, &Handle, EFI_FILE_MODE_READ, 0);\r |
2609 | if (EFI_ERROR(Status)) {\r |
2610 | return (Status);\r |
2611 | }\r |
2612 | \r |
2613 | if (FileHandleIsDirectory(Handle) == EFI_SUCCESS) {\r |
2614 | ShellCloseFile(&Handle);\r |
2615 | return (EFI_SUCCESS);\r |
2616 | }\r |
2617 | ShellCloseFile(&Handle);\r |
2618 | return (EFI_NOT_FOUND);\r |
2619 | }\r |
2620 | \r |
36a9d672 |
2621 | /**\r |
2622 | Function to determine if a given filename represents a file.\r |
2623 | \r |
2624 | @param[in] Name Path to file to test.\r |
2625 | \r |
2626 | @retval EFI_SUCCESS The Path represents a file.\r |
2627 | @retval EFI_NOT_FOUND The Path does not represent a file.\r |
2628 | @retval other The path failed to open.\r |
2629 | **/\r |
2630 | EFI_STATUS\r |
2631 | EFIAPI\r |
2632 | ShellIsFile(\r |
2633 | IN CONST CHAR16 *Name\r |
2634 | )\r |
2635 | {\r |
2636 | EFI_STATUS Status;\r |
2637 | EFI_FILE_HANDLE Handle;\r |
2638 | \r |
ecd3d59f |
2639 | ASSERT(Name != NULL);\r |
2640 | \r |
36a9d672 |
2641 | Handle = NULL;\r |
2642 | \r |
2643 | Status = ShellOpenFileByName(Name, &Handle, EFI_FILE_MODE_READ, 0);\r |
2644 | if (EFI_ERROR(Status)) {\r |
2645 | return (Status);\r |
2646 | }\r |
2647 | \r |
2648 | if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {\r |
2649 | ShellCloseFile(&Handle);\r |
2650 | return (EFI_SUCCESS);\r |
2651 | }\r |
2652 | ShellCloseFile(&Handle);\r |
2653 | return (EFI_NOT_FOUND);\r |
2654 | }\r |
2655 | \r |
b3011f40 |
2656 | /**\r |
2657 | Function to determine if a given filename represents a file.\r |
2658 | \r |
2659 | This will search the CWD and then the Path.\r |
2660 | \r |
2661 | If Name is NULL, then ASSERT.\r |
2662 | \r |
2663 | @param[in] Name Path to file to test.\r |
2664 | \r |
2665 | @retval EFI_SUCCESS The Path represents a file.\r |
2666 | @retval EFI_NOT_FOUND The Path does not represent a file.\r |
2667 | @retval other The path failed to open.\r |
2668 | **/\r |
2669 | EFI_STATUS\r |
2670 | EFIAPI\r |
2671 | ShellIsFileInPath(\r |
2672 | IN CONST CHAR16 *Name\r |
2673 | ) {\r |
2674 | CHAR16 *NewName;\r |
2675 | EFI_STATUS Status;\r |
2676 | \r |
2677 | if (!EFI_ERROR(ShellIsFile(Name))) {\r |
2678 | return (TRUE);\r |
2679 | }\r |
2680 | \r |
2681 | NewName = ShellFindFilePath(Name);\r |
2682 | if (NewName == NULL) {\r |
2683 | return (EFI_NOT_FOUND);\r |
2684 | }\r |
2685 | Status = ShellIsFile(NewName);\r |
2686 | FreePool(NewName);\r |
2687 | return (Status);\r |
2688 | }\r |
125c2cf4 |
2689 | /**\r |
1e6e84c7 |
2690 | Function to determine whether a string is decimal or hex representation of a number\r |
125c2cf4 |
2691 | and return the number converted from the string.\r |
2692 | \r |
2693 | @param[in] String String representation of a number\r |
2694 | \r |
2695 | @retval all the number\r |
2696 | **/\r |
2697 | UINTN\r |
2698 | EFIAPI\r |
2699 | ShellStrToUintn(\r |
2700 | IN CONST CHAR16 *String\r |
2701 | )\r |
2702 | {\r |
2703 | CONST CHAR16 *Walker;\r |
b3011f40 |
2704 | for (Walker = String; Walker != NULL && *Walker != CHAR_NULL && *Walker == L' '; Walker++);\r |
1cd45e78 |
2705 | if (Walker == NULL || *Walker == CHAR_NULL) {\r |
2706 | ASSERT(FALSE);\r |
2707 | return ((UINTN)(-1));\r |
2708 | } else {\r |
2709 | if (StrnCmp(Walker, L"0x", 2) == 0 || StrnCmp(Walker, L"0X", 2) == 0){\r |
2710 | return (StrHexToUintn(Walker));\r |
2711 | }\r |
2712 | return (StrDecimalToUintn(Walker));\r |
125c2cf4 |
2713 | }\r |
125c2cf4 |
2714 | }\r |
2715 | \r |
2716 | /**\r |
2717 | Safely append with automatic string resizing given length of Destination and\r |
2718 | desired length of copy from Source.\r |
2719 | \r |
2720 | append the first D characters of Source to the end of Destination, where D is\r |
2721 | the lesser of Count and the StrLen() of Source. If appending those D characters\r |
2722 | will fit within Destination (whose Size is given as CurrentSize) and\r |
1e6e84c7 |
2723 | still leave room for a NULL terminator, then those characters are appended,\r |
2724 | starting at the original terminating NULL of Destination, and a new terminating\r |
2725 | NULL is appended.\r |
125c2cf4 |
2726 | \r |
2727 | If appending D characters onto Destination will result in a overflow of the size\r |
2728 | given in CurrentSize the string will be grown such that the copy can be performed\r |
2729 | and CurrentSize will be updated to the new size.\r |
2730 | \r |
2731 | If Source is NULL, there is nothing to append, just return the current buffer in\r |
2732 | Destination.\r |
2733 | \r |
2734 | if Destination is NULL, then ASSERT()\r |
2735 | if Destination's current length (including NULL terminator) is already more then\r |
2736 | CurrentSize, then ASSERT()\r |
2737 | \r |
2738 | @param[in,out] Destination The String to append onto\r |
2739 | @param[in,out] CurrentSize on call the number of bytes in Destination. On\r |
2740 | return possibly the new size (still in bytes). if NULL\r |
2741 | then allocate whatever is needed.\r |
2742 | @param[in] Source The String to append from\r |
2743 | @param[in] Count Maximum number of characters to append. if 0 then\r |
2744 | all are appended.\r |
2745 | \r |
2746 | @return Destination return the resultant string.\r |
2747 | **/\r |
2748 | CHAR16*\r |
2749 | EFIAPI\r |
2750 | StrnCatGrow (\r |
2751 | IN OUT CHAR16 **Destination,\r |
2752 | IN OUT UINTN *CurrentSize,\r |
2753 | IN CONST CHAR16 *Source,\r |
2754 | IN UINTN Count\r |
2755 | )\r |
2756 | {\r |
2757 | UINTN DestinationStartSize;\r |
2758 | UINTN NewSize;\r |
2759 | \r |
2760 | //\r |
2761 | // ASSERTs\r |
2762 | //\r |
2763 | ASSERT(Destination != NULL);\r |
2764 | \r |
2765 | //\r |
2766 | // If there's nothing to do then just return Destination\r |
2767 | //\r |
2768 | if (Source == NULL) {\r |
2769 | return (*Destination);\r |
2770 | }\r |
2771 | \r |
2772 | //\r |
2773 | // allow for un-initialized pointers, based on size being 0\r |
2774 | //\r |
2775 | if (CurrentSize != NULL && *CurrentSize == 0) {\r |
2776 | *Destination = NULL;\r |
2777 | }\r |
2778 | \r |
2779 | //\r |
2780 | // allow for NULL pointers address as Destination\r |
2781 | //\r |
2782 | if (*Destination != NULL) {\r |
2783 | ASSERT(CurrentSize != 0);\r |
2784 | DestinationStartSize = StrSize(*Destination);\r |
2785 | ASSERT(DestinationStartSize <= *CurrentSize);\r |
2786 | } else {\r |
2787 | DestinationStartSize = 0;\r |
2788 | // ASSERT(*CurrentSize == 0);\r |
2789 | }\r |
2790 | \r |
2791 | //\r |
2792 | // Append all of Source?\r |
2793 | //\r |
2794 | if (Count == 0) {\r |
2795 | Count = StrLen(Source);\r |
2796 | }\r |
2797 | \r |
2798 | //\r |
2799 | // Test and grow if required\r |
2800 | //\r |
2801 | if (CurrentSize != NULL) {\r |
2802 | NewSize = *CurrentSize;\r |
2803 | while (NewSize < (DestinationStartSize + (Count*sizeof(CHAR16)))) {\r |
2804 | NewSize += 2 * Count * sizeof(CHAR16);\r |
2805 | }\r |
2806 | *Destination = ReallocatePool(*CurrentSize, NewSize, *Destination);\r |
2807 | *CurrentSize = NewSize;\r |
2808 | } else {\r |
2809 | *Destination = AllocateZeroPool((Count+1)*sizeof(CHAR16));\r |
2810 | }\r |
2811 | \r |
2812 | //\r |
2813 | // Now use standard StrnCat on a big enough buffer\r |
2814 | //\r |
2815 | return StrnCat(*Destination, Source, Count);\r |
2816 | }\r |