]>
Commit | Line | Data |
---|---|---|
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 | |
c9d92df0 | 200 | \r |
94b17fa1 | 201 | //\r |
202 | // only success getting 2 of either the old or new, but no 1/2 and 1/2\r | |
203 | //\r | |
1e6e84c7 | 204 | if ((mEfiShellEnvironment2 != NULL && mEfiShellInterface != NULL) ||\r |
94b17fa1 | 205 | (mEfiShellProtocol != NULL && mEfiShellParametersProtocol != NULL) ) {\r |
d2b4564b | 206 | if (mEfiShellProtocol != NULL) {\r |
207 | FileFunctionMap.GetFileInfo = mEfiShellProtocol->GetFileInfo;\r | |
208 | FileFunctionMap.SetFileInfo = mEfiShellProtocol->SetFileInfo;\r | |
209 | FileFunctionMap.ReadFile = mEfiShellProtocol->ReadFile;\r | |
210 | FileFunctionMap.WriteFile = mEfiShellProtocol->WriteFile;\r | |
211 | FileFunctionMap.CloseFile = mEfiShellProtocol->CloseFile;\r | |
212 | FileFunctionMap.DeleteFile = mEfiShellProtocol->DeleteFile;\r | |
213 | FileFunctionMap.GetFilePosition = mEfiShellProtocol->GetFilePosition;\r | |
214 | FileFunctionMap.SetFilePosition = mEfiShellProtocol->SetFilePosition;\r | |
215 | FileFunctionMap.FlushFile = mEfiShellProtocol->FlushFile;\r | |
216 | FileFunctionMap.GetFileSize = mEfiShellProtocol->GetFileSize;\r | |
217 | } else {\r | |
218 | FileFunctionMap.GetFileInfo = FileHandleGetInfo;\r | |
219 | FileFunctionMap.SetFileInfo = FileHandleSetInfo;\r | |
220 | FileFunctionMap.ReadFile = FileHandleRead;\r | |
221 | FileFunctionMap.WriteFile = FileHandleWrite;\r | |
222 | FileFunctionMap.CloseFile = FileHandleClose;\r | |
223 | FileFunctionMap.DeleteFile = FileHandleDelete;\r | |
224 | FileFunctionMap.GetFilePosition = FileHandleGetPosition;\r | |
225 | FileFunctionMap.SetFilePosition = FileHandleSetPosition;\r | |
226 | FileFunctionMap.FlushFile = FileHandleFlush;\r | |
227 | FileFunctionMap.GetFileSize = FileHandleGetSize;\r | |
228 | }\r | |
94b17fa1 | 229 | return (EFI_SUCCESS);\r |
230 | }\r | |
231 | return (EFI_NOT_FOUND);\r | |
232 | }\r | |
d2b4564b | 233 | /**\r |
234 | Constructor for the Shell library.\r | |
235 | \r | |
236 | Initialize the library and determine if the underlying is a UEFI Shell 2.0 or an EFI shell.\r | |
237 | \r | |
238 | @param ImageHandle the image handle of the process\r | |
239 | @param SystemTable the EFI System Table pointer\r | |
240 | \r | |
241 | @retval EFI_SUCCESS the initialization was complete sucessfully\r | |
242 | @return others an error ocurred during initialization\r | |
243 | **/\r | |
244 | EFI_STATUS\r | |
245 | EFIAPI\r | |
246 | ShellLibConstructor (\r | |
247 | IN EFI_HANDLE ImageHandle,\r | |
248 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
2247dde4 | 249 | ) {\r |
d2b4564b | 250 | \r |
d2b4564b | 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 | |
c9d92df0 | 1281 | ASSERT(NewInfo != NULL);\r |
1282 | if (NewInfo == NULL) {\r | |
1283 | break;\r | |
1284 | }\r | |
1e6e84c7 | 1285 | \r |
1286 | //\r | |
94b17fa1 | 1287 | // copy the simple items\r |
1288 | //\r | |
1289 | NewInfo->Handle = OldInfo->Handle;\r | |
1290 | NewInfo->Status = OldInfo->Status;\r | |
1291 | \r | |
d2b4564b | 1292 | // old shell checks for 0 not NULL\r |
1293 | OldInfo->Handle = 0;\r | |
1294 | \r | |
94b17fa1 | 1295 | //\r |
1296 | // allocate new space to copy strings and structure\r | |
1297 | //\r | |
1298 | NewInfo->FullName = AllocateZeroPool(StrSize(OldInfo->FullName));\r | |
1299 | NewInfo->FileName = AllocateZeroPool(StrSize(OldInfo->FileName));\r | |
1300 | NewInfo->Info = AllocateZeroPool((UINTN)OldInfo->Info->Size);\r | |
1e6e84c7 | 1301 | \r |
94b17fa1 | 1302 | //\r |
1303 | // make sure all the memory allocations were sucessful\r | |
1304 | //\r | |
1305 | ASSERT(NewInfo->FullName != NULL);\r | |
1306 | ASSERT(NewInfo->FileName != NULL);\r | |
1307 | ASSERT(NewInfo->Info != NULL);\r | |
1308 | \r | |
1309 | //\r | |
1310 | // Copt the strings and structure\r | |
1311 | //\r | |
1312 | StrCpy(NewInfo->FullName, OldInfo->FullName);\r | |
1313 | StrCpy(NewInfo->FileName, OldInfo->FileName);\r | |
1314 | gBS->CopyMem (NewInfo->Info, OldInfo->Info, (UINTN)OldInfo->Info->Size);\r | |
1315 | \r | |
1316 | //\r | |
1317 | // add that to the list\r | |
1318 | //\r | |
9b3bf083 | 1319 | InsertTailList(ListHead, &NewInfo->Link);\r |
94b17fa1 | 1320 | }\r |
1321 | return (ListHead);\r | |
1322 | }\r | |
1323 | /**\r | |
1324 | Opens a group of files based on a path.\r | |
1325 | \r | |
1e6e84c7 | 1326 | This function uses the Arg to open all the matching files. Each matched\r |
1327 | file has a SHELL_FILE_ARG structure to record the file information. These\r | |
1328 | structures are placed on the list ListHead. Users can get the SHELL_FILE_ARG\r | |
94b17fa1 | 1329 | structures from ListHead to access each file. This function supports wildcards\r |
1e6e84c7 | 1330 | and will process '?' and '*' as such. the list must be freed with a call to\r |
94b17fa1 | 1331 | ShellCloseFileMetaArg().\r |
1332 | \r | |
1e6e84c7 | 1333 | If you are NOT appending to an existing list *ListHead must be NULL. If\r |
5f7431d0 | 1334 | *ListHead is NULL then it must be callee freed.\r |
94b17fa1 | 1335 | \r |
1336 | @param Arg pointer to path string\r | |
1337 | @param OpenMode mode to open files with\r | |
1338 | @param ListHead head of linked list of results\r | |
1339 | \r | |
1e6e84c7 | 1340 | @retval EFI_SUCCESS the operation was sucessful and the list head\r |
94b17fa1 | 1341 | contains the list of opened files\r |
1342 | #retval EFI_UNSUPPORTED a previous ShellOpenFileMetaArg must be closed first.\r | |
1343 | *ListHead is set to NULL.\r | |
1344 | @return != EFI_SUCCESS the operation failed\r | |
1345 | \r | |
1346 | @sa InternalShellConvertFileListType\r | |
1347 | **/\r | |
1348 | EFI_STATUS\r | |
1349 | EFIAPI\r | |
1350 | ShellOpenFileMetaArg (\r | |
1351 | IN CHAR16 *Arg,\r | |
1352 | IN UINT64 OpenMode,\r | |
1353 | IN OUT EFI_SHELL_FILE_INFO **ListHead\r | |
1354 | )\r | |
1355 | {\r | |
1356 | EFI_STATUS Status;\r | |
9b3bf083 | 1357 | LIST_ENTRY mOldStyleFileList;\r |
1e6e84c7 | 1358 | \r |
94b17fa1 | 1359 | //\r |
1360 | // ASSERT that Arg and ListHead are not NULL\r | |
1361 | //\r | |
1362 | ASSERT(Arg != NULL);\r | |
1363 | ASSERT(ListHead != NULL);\r | |
1364 | \r | |
1e6e84c7 | 1365 | //\r |
94b17fa1 | 1366 | // Check for UEFI Shell 2.0 protocols\r |
1367 | //\r | |
1368 | if (mEfiShellProtocol != NULL) {\r | |
5f7431d0 | 1369 | if (*ListHead == NULL) {\r |
1370 | *ListHead = (EFI_SHELL_FILE_INFO*)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));\r | |
1371 | if (*ListHead == NULL) {\r | |
1372 | return (EFI_OUT_OF_RESOURCES);\r | |
1373 | }\r | |
1374 | InitializeListHead(&((*ListHead)->Link));\r | |
1e6e84c7 | 1375 | }\r |
1376 | Status = mEfiShellProtocol->OpenFileList(Arg,\r | |
1377 | OpenMode,\r | |
2247dde4 | 1378 | ListHead);\r |
1379 | if (EFI_ERROR(Status)) {\r | |
1380 | mEfiShellProtocol->RemoveDupInFileList(ListHead);\r | |
1381 | } else {\r | |
1382 | Status = mEfiShellProtocol->RemoveDupInFileList(ListHead);\r | |
1383 | }\r | |
1384 | return (Status);\r | |
1e6e84c7 | 1385 | }\r |
94b17fa1 | 1386 | \r |
1387 | //\r | |
1388 | // ASSERT that we must have EFI shell\r | |
1389 | //\r | |
1390 | ASSERT(mEfiShellEnvironment2 != NULL);\r | |
1391 | \r | |
94b17fa1 | 1392 | //\r |
1393 | // make sure the list head is initialized\r | |
1394 | //\r | |
9b3bf083 | 1395 | InitializeListHead(&mOldStyleFileList);\r |
94b17fa1 | 1396 | \r |
1397 | //\r | |
1398 | // Get the EFI Shell list of files\r | |
1399 | //\r | |
9b3bf083 | 1400 | Status = mEfiShellEnvironment2->FileMetaArg(Arg, &mOldStyleFileList);\r |
94b17fa1 | 1401 | if (EFI_ERROR(Status)) {\r |
1402 | *ListHead = NULL;\r | |
1403 | return (Status);\r | |
1404 | }\r | |
1405 | \r | |
9b3bf083 | 1406 | if (*ListHead == NULL) {\r |
1407 | *ListHead = (EFI_SHELL_FILE_INFO *)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));\r | |
1408 | if (*ListHead == NULL) {\r | |
1409 | return (EFI_OUT_OF_RESOURCES);\r | |
1410 | }\r | |
1411 | }\r | |
1412 | \r | |
94b17fa1 | 1413 | //\r |
1414 | // Convert that to equivalent of UEFI Shell 2.0 structure\r | |
1415 | //\r | |
9b3bf083 | 1416 | InternalShellConvertFileListType(&mOldStyleFileList, &(*ListHead)->Link);\r |
94b17fa1 | 1417 | \r |
d2b4564b | 1418 | //\r |
1419 | // Free the EFI Shell version that was converted.\r | |
1420 | //\r | |
9b3bf083 | 1421 | mEfiShellEnvironment2->FreeFileList(&mOldStyleFileList);\r |
94b17fa1 | 1422 | \r |
1423 | return (Status);\r | |
1424 | }\r | |
1425 | /**\r | |
1426 | Free the linked list returned from ShellOpenFileMetaArg\r | |
1427 | \r | |
1428 | if ListHead is NULL then ASSERT()\r | |
1429 | \r | |
1430 | @param ListHead the pointer to free\r | |
1431 | \r | |
1432 | @retval EFI_SUCCESS the operation was sucessful\r | |
1433 | **/\r | |
1434 | EFI_STATUS\r | |
1435 | EFIAPI\r | |
1436 | ShellCloseFileMetaArg (\r | |
1437 | IN OUT EFI_SHELL_FILE_INFO **ListHead\r | |
1438 | )\r | |
1439 | {\r | |
1440 | LIST_ENTRY *Node;\r | |
1441 | \r | |
1442 | //\r | |
1443 | // ASSERT that ListHead is not NULL\r | |
1444 | //\r | |
1445 | ASSERT(ListHead != NULL);\r | |
1446 | \r | |
1e6e84c7 | 1447 | //\r |
94b17fa1 | 1448 | // Check for UEFI Shell 2.0 protocols\r |
1449 | //\r | |
1450 | if (mEfiShellProtocol != NULL) {\r | |
1451 | return (mEfiShellProtocol->FreeFileList(ListHead));\r | |
1452 | } else {\r | |
94b17fa1 | 1453 | //\r |
1e6e84c7 | 1454 | // Since this is EFI Shell version we need to free our internally made copy\r |
94b17fa1 | 1455 | // of the list\r |
1456 | //\r | |
1e6e84c7 | 1457 | for ( Node = GetFirstNode(&(*ListHead)->Link)\r |
1458 | ; IsListEmpty(&(*ListHead)->Link) == FALSE\r | |
9b3bf083 | 1459 | ; Node = GetFirstNode(&(*ListHead)->Link)) {\r |
94b17fa1 | 1460 | RemoveEntryList(Node);\r |
d2b4564b | 1461 | ((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->Handle->Close(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->Handle);\r |
94b17fa1 | 1462 | FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->FullName);\r |
1463 | FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->FileName);\r | |
1464 | FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->Info);\r | |
1465 | FreePool((EFI_SHELL_FILE_INFO_NO_CONST*)Node);\r | |
1466 | }\r | |
1467 | return EFI_SUCCESS;\r | |
1468 | }\r | |
1469 | }\r | |
1470 | \r | |
125c2cf4 | 1471 | /**\r |
1472 | Find a file by searching the CWD and then the path.\r | |
1473 | \r | |
b3011f40 | 1474 | If FileName is NULL then ASSERT.\r |
125c2cf4 | 1475 | \r |
b3011f40 | 1476 | If the return value is not NULL then the memory must be caller freed.\r |
125c2cf4 | 1477 | \r |
1478 | @param FileName Filename string.\r | |
1479 | \r | |
1480 | @retval NULL the file was not found\r | |
1481 | @return !NULL the full path to the file.\r | |
1482 | **/\r | |
1483 | CHAR16 *\r | |
1484 | EFIAPI\r | |
1485 | ShellFindFilePath (\r | |
1486 | IN CONST CHAR16 *FileName\r | |
1487 | )\r | |
1488 | {\r | |
1489 | CONST CHAR16 *Path;\r | |
1490 | EFI_FILE_HANDLE Handle;\r | |
1491 | EFI_STATUS Status;\r | |
1492 | CHAR16 *RetVal;\r | |
1493 | CHAR16 *TestPath;\r | |
1494 | CONST CHAR16 *Walker;\r | |
36a9d672 | 1495 | UINTN Size;\r |
1cd45e78 | 1496 | CHAR16 *TempChar;\r |
125c2cf4 | 1497 | \r |
1498 | RetVal = NULL;\r | |
1499 | \r | |
1500 | Path = ShellGetEnvironmentVariable(L"cwd");\r | |
1501 | if (Path != NULL) {\r | |
36a9d672 | 1502 | Size = StrSize(Path);\r |
1503 | Size += StrSize(FileName);\r | |
1504 | TestPath = AllocateZeroPool(Size);\r | |
c9d92df0 | 1505 | ASSERT(TestPath != NULL);\r |
1506 | if (TestPath == NULL) {\r | |
1507 | return (NULL);\r | |
1508 | }\r | |
125c2cf4 | 1509 | StrCpy(TestPath, Path);\r |
1510 | StrCat(TestPath, FileName);\r | |
1511 | Status = ShellOpenFileByName(TestPath, &Handle, EFI_FILE_MODE_READ, 0);\r | |
1512 | if (!EFI_ERROR(Status)){\r | |
1513 | RetVal = StrnCatGrow(&RetVal, NULL, TestPath, 0);\r | |
1514 | ShellCloseFile(&Handle);\r | |
1515 | FreePool(TestPath);\r | |
1516 | return (RetVal);\r | |
1517 | }\r | |
1518 | FreePool(TestPath);\r | |
1519 | }\r | |
1520 | Path = ShellGetEnvironmentVariable(L"path");\r | |
1521 | if (Path != NULL) {\r | |
36a9d672 | 1522 | Size = StrSize(Path);\r |
1523 | Size += StrSize(FileName);\r | |
1524 | TestPath = AllocateZeroPool(Size);\r | |
1e6e84c7 | 1525 | Walker = (CHAR16*)Path;\r |
125c2cf4 | 1526 | do {\r |
1527 | CopyMem(TestPath, Walker, StrSize(Walker));\r | |
1cd45e78 | 1528 | TempChar = StrStr(TestPath, L";");\r |
1529 | if (TempChar != NULL) {\r | |
1530 | *TempChar = CHAR_NULL;\r | |
125c2cf4 | 1531 | }\r |
1532 | StrCat(TestPath, FileName);\r | |
1533 | if (StrStr(Walker, L";") != NULL) {\r | |
1534 | Walker = StrStr(Walker, L";") + 1;\r | |
1535 | } else {\r | |
1536 | Walker = NULL;\r | |
1537 | }\r | |
1538 | Status = ShellOpenFileByName(TestPath, &Handle, EFI_FILE_MODE_READ, 0);\r | |
1539 | if (!EFI_ERROR(Status)){\r | |
1540 | RetVal = StrnCatGrow(&RetVal, NULL, TestPath, 0);\r | |
1541 | ShellCloseFile(&Handle);\r | |
1542 | break;\r | |
1543 | }\r | |
1544 | } while (Walker != NULL && Walker[0] != CHAR_NULL);\r | |
1545 | FreePool(TestPath);\r | |
1546 | }\r | |
1547 | return (RetVal);\r | |
1548 | }\r | |
1549 | \r | |
b3011f40 | 1550 | /**\r |
1e6e84c7 | 1551 | Find a file by searching the CWD and then the path with a variable set of file\r |
1552 | extensions. If the file is not found it will append each extension in the list\r | |
b3011f40 | 1553 | in the order provided and return the first one that is successful.\r |
1554 | \r | |
1555 | If FileName is NULL, then ASSERT.\r | |
1556 | If FileExtension is NULL, then behavior is identical to ShellFindFilePath.\r | |
1557 | \r | |
1558 | If the return value is not NULL then the memory must be caller freed.\r | |
1559 | \r | |
1560 | @param[in] FileName Filename string.\r | |
1561 | @param[in] FileExtension Semi-colon delimeted list of possible extensions.\r | |
1562 | \r | |
1563 | @retval NULL The file was not found.\r | |
1564 | @retval !NULL The path to the file.\r | |
1565 | **/\r | |
1566 | CHAR16 *\r | |
1567 | EFIAPI\r | |
1568 | ShellFindFilePathEx (\r | |
1569 | IN CONST CHAR16 *FileName,\r | |
1570 | IN CONST CHAR16 *FileExtension\r | |
1571 | )\r | |
1572 | {\r | |
1573 | CHAR16 *TestPath;\r | |
1574 | CHAR16 *RetVal;\r | |
1575 | CONST CHAR16 *ExtensionWalker;\r | |
9e926b69 | 1576 | UINTN Size;\r |
1cd45e78 | 1577 | CHAR16 *TempChar;\r |
c9d92df0 | 1578 | CHAR16 *TempChar2;\r |
1cd45e78 | 1579 | \r |
b3011f40 | 1580 | ASSERT(FileName != NULL);\r |
1581 | if (FileExtension == NULL) {\r | |
1582 | return (ShellFindFilePath(FileName));\r | |
1583 | }\r | |
1584 | RetVal = ShellFindFilePath(FileName);\r | |
1585 | if (RetVal != NULL) {\r | |
1586 | return (RetVal);\r | |
1587 | }\r | |
9e926b69 | 1588 | Size = StrSize(FileName);\r |
1589 | Size += StrSize(FileExtension);\r | |
1590 | TestPath = AllocateZeroPool(Size);\r | |
c9d92df0 | 1591 | ASSERT(TestPath != NULL);\r |
1592 | if (TestPath == NULL) {\r | |
1593 | return (NULL);\r | |
1594 | }\r | |
1595 | for (ExtensionWalker = FileExtension, TempChar2 = (CHAR16*)FileExtension; TempChar2 != NULL ; ExtensionWalker = TempChar2 + 1 ){\r | |
b3011f40 | 1596 | StrCpy(TestPath, FileName);\r |
1cd45e78 | 1597 | if (ExtensionWalker != NULL) {\r |
1598 | StrCat(TestPath, ExtensionWalker);\r | |
1599 | }\r | |
1600 | TempChar = StrStr(TestPath, L";");\r | |
1601 | if (TempChar != NULL) {\r | |
1602 | *TempChar = CHAR_NULL;\r | |
b3011f40 | 1603 | }\r |
1604 | RetVal = ShellFindFilePath(TestPath);\r | |
1605 | if (RetVal != NULL) {\r | |
1606 | break;\r | |
1607 | }\r | |
c9d92df0 | 1608 | TempChar2 = StrStr(ExtensionWalker, L";");\r |
b3011f40 | 1609 | }\r |
1610 | FreePool(TestPath);\r | |
1611 | return (RetVal);\r | |
1612 | }\r | |
1613 | \r | |
94b17fa1 | 1614 | typedef struct {\r |
9b3bf083 | 1615 | LIST_ENTRY Link;\r |
94b17fa1 | 1616 | CHAR16 *Name;\r |
1617 | ParamType Type;\r | |
1618 | CHAR16 *Value;\r | |
1619 | UINTN OriginalPosition;\r | |
1620 | } SHELL_PARAM_PACKAGE;\r | |
1621 | \r | |
1622 | /**\r | |
1e6e84c7 | 1623 | Checks the list of valid arguments and returns TRUE if the item was found. If the\r |
94b17fa1 | 1624 | return value is TRUE then the type parameter is set also.\r |
1e6e84c7 | 1625 | \r |
94b17fa1 | 1626 | if CheckList is NULL then ASSERT();\r |
1627 | if Name is NULL then ASSERT();\r | |
1628 | if Type is NULL then ASSERT();\r | |
1629 | \r | |
1630 | @param Type pointer to type of parameter if it was found\r | |
1631 | @param Name pointer to Name of parameter found\r | |
1632 | @param CheckList List to check against\r | |
1633 | \r | |
1634 | @retval TRUE the Parameter was found. Type is valid.\r | |
1635 | @retval FALSE the Parameter was not found. Type is not valid.\r | |
1636 | **/\r | |
1637 | BOOLEAN\r | |
1638 | EFIAPI\r | |
d2b4564b | 1639 | InternalIsOnCheckList (\r |
94b17fa1 | 1640 | IN CONST CHAR16 *Name,\r |
1641 | IN CONST SHELL_PARAM_ITEM *CheckList,\r | |
1642 | OUT ParamType *Type\r | |
2247dde4 | 1643 | ) {\r |
94b17fa1 | 1644 | SHELL_PARAM_ITEM *TempListItem;\r |
1645 | \r | |
1646 | //\r | |
1647 | // ASSERT that all 3 pointer parameters aren't NULL\r | |
1648 | //\r | |
1649 | ASSERT(CheckList != NULL);\r | |
1650 | ASSERT(Type != NULL);\r | |
1651 | ASSERT(Name != NULL);\r | |
1652 | \r | |
d2b4564b | 1653 | //\r |
1654 | // question mark and page break mode are always supported\r | |
1655 | //\r | |
1656 | if ((StrCmp(Name, L"-?") == 0) ||\r | |
1657 | (StrCmp(Name, L"-b") == 0)\r | |
1658 | ) {\r | |
1659 | return (TRUE);\r | |
1660 | }\r | |
1661 | \r | |
94b17fa1 | 1662 | //\r |
1663 | // Enumerate through the list\r | |
1664 | //\r | |
1665 | for (TempListItem = (SHELL_PARAM_ITEM*)CheckList ; TempListItem->Name != NULL ; TempListItem++) {\r | |
1666 | //\r | |
9eb53ac3 | 1667 | // If the Type is TypeStart only check the first characters of the passed in param\r |
1668 | // If it matches set the type and return TRUE\r | |
94b17fa1 | 1669 | //\r |
9eb53ac3 | 1670 | if (TempListItem->Type == TypeStart && StrnCmp(Name, TempListItem->Name, StrLen(TempListItem->Name)) == 0) {\r |
1671 | *Type = TempListItem->Type;\r | |
1672 | return (TRUE);\r | |
1673 | } else if (StrCmp(Name, TempListItem->Name) == 0) {\r | |
94b17fa1 | 1674 | *Type = TempListItem->Type;\r |
1675 | return (TRUE);\r | |
1676 | }\r | |
1677 | }\r | |
2247dde4 | 1678 | \r |
94b17fa1 | 1679 | return (FALSE);\r |
1680 | }\r | |
1681 | /**\r | |
d2b4564b | 1682 | Checks the string for indicators of "flag" status. this is a leading '/', '-', or '+'\r |
94b17fa1 | 1683 | \r |
1684 | @param Name pointer to Name of parameter found\r | |
1685 | \r | |
1686 | @retval TRUE the Parameter is a flag.\r | |
1687 | @retval FALSE the Parameter not a flag\r | |
1688 | **/\r | |
1689 | BOOLEAN\r | |
1690 | EFIAPI\r | |
d2b4564b | 1691 | InternalIsFlag (\r |
2247dde4 | 1692 | IN CONST CHAR16 *Name,\r |
1693 | IN BOOLEAN AlwaysAllowNumbers\r | |
94b17fa1 | 1694 | )\r |
1695 | {\r | |
1696 | //\r | |
1697 | // ASSERT that Name isn't NULL\r | |
1698 | //\r | |
1699 | ASSERT(Name != NULL);\r | |
1700 | \r | |
2247dde4 | 1701 | //\r |
1702 | // If we accept numbers then dont return TRUE. (they will be values)\r | |
1703 | //\r | |
969c783b | 1704 | if (((Name[0] == L'-' || Name[0] == L'+') && ShellIsHexaDecimalDigitCharacter(Name[1])) && AlwaysAllowNumbers != FALSE) {\r |
2247dde4 | 1705 | return (FALSE);\r |
1706 | }\r | |
1707 | \r | |
94b17fa1 | 1708 | //\r |
1709 | // If the Name has a / or - as the first character return TRUE\r | |
1710 | //\r | |
1e6e84c7 | 1711 | if ((Name[0] == L'/') ||\r |
d2b4564b | 1712 | (Name[0] == L'-') ||\r |
1713 | (Name[0] == L'+')\r | |
1714 | ) {\r | |
94b17fa1 | 1715 | return (TRUE);\r |
1716 | }\r | |
1717 | return (FALSE);\r | |
1718 | }\r | |
1719 | \r | |
1720 | /**\r | |
1e6e84c7 | 1721 | Checks the command line arguments passed against the list of valid ones.\r |
94b17fa1 | 1722 | \r |
1723 | If no initialization is required, then return RETURN_SUCCESS.\r | |
1e6e84c7 | 1724 | \r |
94b17fa1 | 1725 | @param CheckList pointer to list of parameters to check\r |
1726 | @param CheckPackage pointer to pointer to list checked values\r | |
1e6e84c7 | 1727 | @param ProblemParam optional pointer to pointer to unicode string for\r |
d2b4564b | 1728 | the paramater that caused failure. If used then the\r |
1729 | caller is responsible for freeing the memory.\r | |
94b17fa1 | 1730 | @param AutoPageBreak will automatically set PageBreakEnabled for "b" parameter\r |
1731 | @param Argc Count of parameters in Argv\r | |
1732 | @param Argv pointer to array of parameters\r | |
1733 | \r | |
1734 | @retval EFI_SUCCESS The operation completed sucessfully.\r | |
1735 | @retval EFI_OUT_OF_RESOURCES A memory allocation failed\r | |
1736 | @retval EFI_INVALID_PARAMETER A parameter was invalid\r | |
1e6e84c7 | 1737 | @retval EFI_VOLUME_CORRUPTED the command line was corrupt. an argument was\r |
1738 | duplicated. the duplicated command line argument\r | |
94b17fa1 | 1739 | was returned in ProblemParam if provided.\r |
1e6e84c7 | 1740 | @retval EFI_NOT_FOUND a argument required a value that was missing.\r |
94b17fa1 | 1741 | the invalid command line argument was returned in\r |
1742 | ProblemParam if provided.\r | |
1743 | **/\r | |
2247dde4 | 1744 | STATIC\r |
94b17fa1 | 1745 | EFI_STATUS\r |
1746 | EFIAPI\r | |
1747 | InternalCommandLineParse (\r | |
1748 | IN CONST SHELL_PARAM_ITEM *CheckList,\r | |
1749 | OUT LIST_ENTRY **CheckPackage,\r | |
1750 | OUT CHAR16 **ProblemParam OPTIONAL,\r | |
1751 | IN BOOLEAN AutoPageBreak,\r | |
1752 | IN CONST CHAR16 **Argv,\r | |
2247dde4 | 1753 | IN UINTN Argc,\r |
1754 | IN BOOLEAN AlwaysAllowNumbers\r | |
1755 | ) {\r | |
94b17fa1 | 1756 | UINTN LoopCounter;\r |
94b17fa1 | 1757 | ParamType CurrentItemType;\r |
1758 | SHELL_PARAM_PACKAGE *CurrentItemPackage;\r | |
125c2cf4 | 1759 | UINTN GetItemValue;\r |
1760 | UINTN ValueSize;\r | |
94b17fa1 | 1761 | \r |
1762 | CurrentItemPackage = NULL;\r | |
2247dde4 | 1763 | mTotalParameterCount = 0;\r |
125c2cf4 | 1764 | GetItemValue = 0;\r |
1765 | ValueSize = 0;\r | |
94b17fa1 | 1766 | \r |
1767 | //\r | |
1768 | // If there is only 1 item we dont need to do anything\r | |
1769 | //\r | |
1770 | if (Argc <= 1) {\r | |
1771 | *CheckPackage = NULL;\r | |
1772 | return (EFI_SUCCESS);\r | |
1773 | }\r | |
1774 | \r | |
2247dde4 | 1775 | //\r |
1776 | // ASSERTs\r | |
1777 | //\r | |
1778 | ASSERT(CheckList != NULL);\r | |
1779 | ASSERT(Argv != NULL);\r | |
1780 | \r | |
94b17fa1 | 1781 | //\r |
1782 | // initialize the linked list\r | |
1783 | //\r | |
1784 | *CheckPackage = (LIST_ENTRY*)AllocateZeroPool(sizeof(LIST_ENTRY));\r | |
1785 | InitializeListHead(*CheckPackage);\r | |
1786 | \r | |
1787 | //\r | |
1788 | // loop through each of the arguments\r | |
1789 | //\r | |
1790 | for (LoopCounter = 0 ; LoopCounter < Argc ; ++LoopCounter) {\r | |
1791 | if (Argv[LoopCounter] == NULL) {\r | |
1792 | //\r | |
1793 | // do nothing for NULL argv\r | |
1794 | //\r | |
b3011f40 | 1795 | } else if (InternalIsOnCheckList(Argv[LoopCounter], CheckList, &CurrentItemType) != FALSE) {\r |
2247dde4 | 1796 | //\r |
1797 | // We might have leftover if last parameter didnt have optional value\r | |
1798 | //\r | |
125c2cf4 | 1799 | if (GetItemValue != 0) {\r |
1800 | GetItemValue = 0;\r | |
2247dde4 | 1801 | InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);\r |
1802 | }\r | |
94b17fa1 | 1803 | //\r |
1804 | // this is a flag\r | |
1805 | //\r | |
1806 | CurrentItemPackage = AllocatePool(sizeof(SHELL_PARAM_PACKAGE));\r | |
1807 | ASSERT(CurrentItemPackage != NULL);\r | |
1808 | CurrentItemPackage->Name = AllocatePool(StrSize(Argv[LoopCounter]));\r | |
1809 | ASSERT(CurrentItemPackage->Name != NULL);\r | |
1810 | StrCpy(CurrentItemPackage->Name, Argv[LoopCounter]);\r | |
1811 | CurrentItemPackage->Type = CurrentItemType;\r | |
1812 | CurrentItemPackage->OriginalPosition = (UINTN)(-1);\r | |
b1f95a06 | 1813 | CurrentItemPackage->Value = NULL;\r |
94b17fa1 | 1814 | \r |
1815 | //\r | |
1816 | // Does this flag require a value\r | |
1817 | //\r | |
125c2cf4 | 1818 | switch (CurrentItemPackage->Type) {\r |
94b17fa1 | 1819 | //\r |
125c2cf4 | 1820 | // possibly trigger the next loop(s) to populate the value of this item\r |
1e6e84c7 | 1821 | //\r |
125c2cf4 | 1822 | case TypeValue:\r |
1e6e84c7 | 1823 | GetItemValue = 1;\r |
125c2cf4 | 1824 | ValueSize = 0;\r |
1825 | break;\r | |
1826 | case TypeDoubleValue:\r | |
1827 | GetItemValue = 2;\r | |
1828 | ValueSize = 0;\r | |
1829 | break;\r | |
1830 | case TypeMaxValue:\r | |
1831 | GetItemValue = (UINTN)(-1);\r | |
1832 | ValueSize = 0;\r | |
1833 | break;\r | |
1834 | default:\r | |
1835 | //\r | |
1836 | // this item has no value expected; we are done\r | |
1837 | //\r | |
1838 | InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);\r | |
1839 | ASSERT(GetItemValue == 0);\r | |
1840 | break;\r | |
94b17fa1 | 1841 | }\r |
125c2cf4 | 1842 | } else if (GetItemValue != 0 && InternalIsFlag(Argv[LoopCounter], AlwaysAllowNumbers) == FALSE) {\r |
b1f95a06 | 1843 | ASSERT(CurrentItemPackage != NULL);\r |
1844 | //\r | |
125c2cf4 | 1845 | // get the item VALUE for a previous flag\r |
b1f95a06 | 1846 | //\r |
125c2cf4 | 1847 | CurrentItemPackage->Value = ReallocatePool(ValueSize, ValueSize + StrSize(Argv[LoopCounter]) + sizeof(CHAR16), CurrentItemPackage->Value);\r |
b1f95a06 | 1848 | ASSERT(CurrentItemPackage->Value != NULL);\r |
125c2cf4 | 1849 | if (ValueSize == 0) {\r |
1850 | StrCpy(CurrentItemPackage->Value, Argv[LoopCounter]);\r | |
1851 | } else {\r | |
1852 | StrCat(CurrentItemPackage->Value, L" ");\r | |
1853 | StrCat(CurrentItemPackage->Value, Argv[LoopCounter]);\r | |
1854 | }\r | |
1855 | ValueSize += StrSize(Argv[LoopCounter]) + sizeof(CHAR16);\r | |
1856 | GetItemValue--;\r | |
1857 | if (GetItemValue == 0) {\r | |
1858 | InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);\r | |
1859 | }\r | |
2247dde4 | 1860 | } else if (InternalIsFlag(Argv[LoopCounter], AlwaysAllowNumbers) == FALSE) {\r |
b1f95a06 | 1861 | //\r |
1862 | // add this one as a non-flag\r | |
1863 | //\r | |
1864 | CurrentItemPackage = AllocatePool(sizeof(SHELL_PARAM_PACKAGE));\r | |
1865 | ASSERT(CurrentItemPackage != NULL);\r | |
1866 | CurrentItemPackage->Name = NULL;\r | |
1867 | CurrentItemPackage->Type = TypePosition;\r | |
1868 | CurrentItemPackage->Value = AllocatePool(StrSize(Argv[LoopCounter]));\r | |
1869 | ASSERT(CurrentItemPackage->Value != NULL);\r | |
1870 | StrCpy(CurrentItemPackage->Value, Argv[LoopCounter]);\r | |
2247dde4 | 1871 | CurrentItemPackage->OriginalPosition = mTotalParameterCount++;\r |
9b3bf083 | 1872 | InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);\r |
94b17fa1 | 1873 | } else if (ProblemParam) {\r |
1874 | //\r | |
1875 | // this was a non-recognised flag... error!\r | |
1876 | //\r | |
d2b4564b | 1877 | *ProblemParam = AllocatePool(StrSize(Argv[LoopCounter]));\r |
1878 | ASSERT(*ProblemParam != NULL);\r | |
1879 | StrCpy(*ProblemParam, Argv[LoopCounter]);\r | |
94b17fa1 | 1880 | ShellCommandLineFreeVarList(*CheckPackage);\r |
1881 | *CheckPackage = NULL;\r | |
1882 | return (EFI_VOLUME_CORRUPTED);\r | |
1883 | } else {\r | |
1884 | ShellCommandLineFreeVarList(*CheckPackage);\r | |
1885 | *CheckPackage = NULL;\r | |
1886 | return (EFI_VOLUME_CORRUPTED);\r | |
1887 | }\r | |
1888 | }\r | |
125c2cf4 | 1889 | if (GetItemValue != 0) {\r |
1890 | GetItemValue = 0;\r | |
1891 | InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);\r | |
1892 | }\r | |
94b17fa1 | 1893 | //\r |
1894 | // support for AutoPageBreak\r | |
1895 | //\r | |
1896 | if (AutoPageBreak && ShellCommandLineGetFlag(*CheckPackage, L"-b")) {\r | |
1897 | ShellSetPageBreakMode(TRUE);\r | |
1898 | }\r | |
1899 | return (EFI_SUCCESS);\r | |
1900 | }\r | |
1901 | \r | |
1902 | /**\r | |
1e6e84c7 | 1903 | Checks the command line arguments passed against the list of valid ones.\r |
94b17fa1 | 1904 | Optionally removes NULL values first.\r |
1e6e84c7 | 1905 | \r |
94b17fa1 | 1906 | If no initialization is required, then return RETURN_SUCCESS.\r |
1e6e84c7 | 1907 | \r |
94b17fa1 | 1908 | @param CheckList pointer to list of parameters to check\r |
1909 | @param CheckPackage pointer to pointer to list checked values\r | |
1e6e84c7 | 1910 | @param ProblemParam optional pointer to pointer to unicode string for\r |
94b17fa1 | 1911 | the paramater that caused failure.\r |
1912 | @param AutoPageBreak will automatically set PageBreakEnabled for "b" parameter\r | |
1913 | \r | |
1914 | @retval EFI_SUCCESS The operation completed sucessfully.\r | |
1915 | @retval EFI_OUT_OF_RESOURCES A memory allocation failed\r | |
1916 | @retval EFI_INVALID_PARAMETER A parameter was invalid\r | |
1e6e84c7 | 1917 | @retval EFI_VOLUME_CORRUPTED the command line was corrupt. an argument was\r |
1918 | duplicated. the duplicated command line argument\r | |
94b17fa1 | 1919 | was returned in ProblemParam if provided.\r |
1920 | @retval EFI_DEVICE_ERROR the commands contained 2 opposing arguments. one\r | |
1e6e84c7 | 1921 | of the command line arguments was returned in\r |
94b17fa1 | 1922 | ProblemParam if provided.\r |
1e6e84c7 | 1923 | @retval EFI_NOT_FOUND a argument required a value that was missing.\r |
94b17fa1 | 1924 | the invalid command line argument was returned in\r |
1925 | ProblemParam if provided.\r | |
1926 | **/\r | |
1927 | EFI_STATUS\r | |
1928 | EFIAPI\r | |
2247dde4 | 1929 | ShellCommandLineParseEx (\r |
94b17fa1 | 1930 | IN CONST SHELL_PARAM_ITEM *CheckList,\r |
1931 | OUT LIST_ENTRY **CheckPackage,\r | |
1932 | OUT CHAR16 **ProblemParam OPTIONAL,\r | |
2247dde4 | 1933 | IN BOOLEAN AutoPageBreak,\r |
1934 | IN BOOLEAN AlwaysAllowNumbers\r | |
1935 | ) {\r | |
1e6e84c7 | 1936 | //\r |
94b17fa1 | 1937 | // ASSERT that CheckList and CheckPackage aren't NULL\r |
1938 | //\r | |
1939 | ASSERT(CheckList != NULL);\r | |
1940 | ASSERT(CheckPackage != NULL);\r | |
1941 | \r | |
1e6e84c7 | 1942 | //\r |
94b17fa1 | 1943 | // Check for UEFI Shell 2.0 protocols\r |
1944 | //\r | |
1945 | if (mEfiShellParametersProtocol != NULL) {\r | |
1e6e84c7 | 1946 | return (InternalCommandLineParse(CheckList,\r |
1947 | CheckPackage,\r | |
1948 | ProblemParam,\r | |
1949 | AutoPageBreak,\r | |
08d7f8e8 | 1950 | (CONST CHAR16**) mEfiShellParametersProtocol->Argv,\r |
2247dde4 | 1951 | mEfiShellParametersProtocol->Argc,\r |
1952 | AlwaysAllowNumbers));\r | |
94b17fa1 | 1953 | }\r |
1954 | \r | |
1e6e84c7 | 1955 | //\r |
94b17fa1 | 1956 | // ASSERT That EFI Shell is not required\r |
1957 | //\r | |
1958 | ASSERT (mEfiShellInterface != NULL);\r | |
1e6e84c7 | 1959 | return (InternalCommandLineParse(CheckList,\r |
1960 | CheckPackage,\r | |
1961 | ProblemParam,\r | |
1962 | AutoPageBreak,\r | |
08d7f8e8 | 1963 | (CONST CHAR16**) mEfiShellInterface->Argv,\r |
2247dde4 | 1964 | mEfiShellInterface->Argc,\r |
1965 | AlwaysAllowNumbers));\r | |
94b17fa1 | 1966 | }\r |
1967 | \r | |
1968 | /**\r | |
1969 | Frees shell variable list that was returned from ShellCommandLineParse.\r | |
1970 | \r | |
1971 | This function will free all the memory that was used for the CheckPackage\r | |
1972 | list of postprocessed shell arguments.\r | |
1973 | \r | |
1974 | this function has no return value.\r | |
1975 | \r | |
1976 | if CheckPackage is NULL, then return\r | |
1977 | \r | |
1978 | @param CheckPackage the list to de-allocate\r | |
1979 | **/\r | |
1980 | VOID\r | |
1981 | EFIAPI\r | |
1982 | ShellCommandLineFreeVarList (\r | |
1983 | IN LIST_ENTRY *CheckPackage\r | |
2247dde4 | 1984 | ) {\r |
94b17fa1 | 1985 | LIST_ENTRY *Node;\r |
1986 | \r | |
1987 | //\r | |
1988 | // check for CheckPackage == NULL\r | |
1989 | //\r | |
1990 | if (CheckPackage == NULL) {\r | |
1991 | return;\r | |
1992 | }\r | |
1993 | \r | |
1994 | //\r | |
1995 | // for each node in the list\r | |
1996 | //\r | |
9eb53ac3 | 1997 | for ( Node = GetFirstNode(CheckPackage)\r |
2247dde4 | 1998 | ; IsListEmpty(CheckPackage) == FALSE\r |
9eb53ac3 | 1999 | ; Node = GetFirstNode(CheckPackage)\r |
2000 | ){\r | |
94b17fa1 | 2001 | //\r |
2002 | // Remove it from the list\r | |
2003 | //\r | |
2004 | RemoveEntryList(Node);\r | |
2005 | \r | |
2006 | //\r | |
2007 | // if it has a name free the name\r | |
2008 | //\r | |
2009 | if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {\r | |
2010 | FreePool(((SHELL_PARAM_PACKAGE*)Node)->Name);\r | |
2011 | }\r | |
2012 | \r | |
2013 | //\r | |
2014 | // if it has a value free the value\r | |
2015 | //\r | |
2016 | if (((SHELL_PARAM_PACKAGE*)Node)->Value != NULL) {\r | |
2017 | FreePool(((SHELL_PARAM_PACKAGE*)Node)->Value);\r | |
2018 | }\r | |
1e6e84c7 | 2019 | \r |
94b17fa1 | 2020 | //\r |
2021 | // free the node structure\r | |
2022 | //\r | |
2023 | FreePool((SHELL_PARAM_PACKAGE*)Node);\r | |
2024 | }\r | |
2025 | //\r | |
2026 | // free the list head node\r | |
2027 | //\r | |
2028 | FreePool(CheckPackage);\r | |
2029 | }\r | |
2030 | /**\r | |
2031 | Checks for presence of a flag parameter\r | |
2032 | \r | |
2033 | flag arguments are in the form of "-<Key>" or "/<Key>", but do not have a value following the key\r | |
2034 | \r | |
2035 | if CheckPackage is NULL then return FALSE.\r | |
2036 | if KeyString is NULL then ASSERT()\r | |
1e6e84c7 | 2037 | \r |
94b17fa1 | 2038 | @param CheckPackage The package of parsed command line arguments\r |
2039 | @param KeyString the Key of the command line argument to check for\r | |
2040 | \r | |
2041 | @retval TRUE the flag is on the command line\r | |
2042 | @retval FALSE the flag is not on the command line\r | |
2043 | **/\r | |
2044 | BOOLEAN\r | |
2045 | EFIAPI\r | |
2046 | ShellCommandLineGetFlag (\r | |
2047 | IN CONST LIST_ENTRY *CheckPackage,\r | |
2048 | IN CHAR16 *KeyString\r | |
2247dde4 | 2049 | ) {\r |
94b17fa1 | 2050 | LIST_ENTRY *Node;\r |
2051 | \r | |
2052 | //\r | |
2053 | // ASSERT that both CheckPackage and KeyString aren't NULL\r | |
2054 | //\r | |
2055 | ASSERT(KeyString != NULL);\r | |
2056 | \r | |
2057 | //\r | |
2058 | // return FALSE for no package\r | |
2059 | //\r | |
2060 | if (CheckPackage == NULL) {\r | |
2061 | return (FALSE);\r | |
2062 | }\r | |
2063 | \r | |
2064 | //\r | |
2065 | // enumerate through the list of parametrs\r | |
2066 | //\r | |
1e6e84c7 | 2067 | for ( Node = GetFirstNode(CheckPackage)\r |
2068 | ; !IsNull (CheckPackage, Node)\r | |
2069 | ; Node = GetNextNode(CheckPackage, Node)\r | |
9eb53ac3 | 2070 | ){\r |
94b17fa1 | 2071 | //\r |
2072 | // If the Name matches, return TRUE (and there may be NULL name)\r | |
2073 | //\r | |
2074 | if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {\r | |
9eb53ac3 | 2075 | //\r |
2076 | // If Type is TypeStart then only compare the begining of the strings\r | |
2077 | //\r | |
1e6e84c7 | 2078 | if ( ((SHELL_PARAM_PACKAGE*)Node)->Type == TypeStart\r |
9eb53ac3 | 2079 | && StrnCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name, StrLen(KeyString)) == 0\r |
2080 | ){\r | |
2081 | return (TRUE);\r | |
2082 | } else if (StrCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {\r | |
94b17fa1 | 2083 | return (TRUE);\r |
2084 | }\r | |
2085 | }\r | |
2086 | }\r | |
2087 | return (FALSE);\r | |
2088 | }\r | |
2089 | /**\r | |
2090 | returns value from command line argument\r | |
2091 | \r | |
2092 | value parameters are in the form of "-<Key> value" or "/<Key> value"\r | |
1e6e84c7 | 2093 | \r |
94b17fa1 | 2094 | if CheckPackage is NULL, then return NULL;\r |
2095 | \r | |
2096 | @param CheckPackage The package of parsed command line arguments\r | |
2097 | @param KeyString the Key of the command line argument to check for\r | |
2098 | \r | |
2099 | @retval NULL the flag is not on the command line\r | |
2100 | @return !=NULL pointer to unicode string of the value\r | |
2101 | **/\r | |
2102 | CONST CHAR16*\r | |
2103 | EFIAPI\r | |
2104 | ShellCommandLineGetValue (\r | |
2105 | IN CONST LIST_ENTRY *CheckPackage,\r | |
2106 | IN CHAR16 *KeyString\r | |
2247dde4 | 2107 | ) {\r |
94b17fa1 | 2108 | LIST_ENTRY *Node;\r |
2109 | \r | |
2110 | //\r | |
2111 | // check for CheckPackage == NULL\r | |
2112 | //\r | |
2113 | if (CheckPackage == NULL) {\r | |
2114 | return (NULL);\r | |
2115 | }\r | |
2116 | \r | |
2117 | //\r | |
2118 | // enumerate through the list of parametrs\r | |
2119 | //\r | |
1e6e84c7 | 2120 | for ( Node = GetFirstNode(CheckPackage)\r |
2121 | ; !IsNull (CheckPackage, Node)\r | |
2122 | ; Node = GetNextNode(CheckPackage, Node)\r | |
9eb53ac3 | 2123 | ){\r |
94b17fa1 | 2124 | //\r |
2125 | // If the Name matches, return the value (name can be NULL)\r | |
2126 | //\r | |
2127 | if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {\r | |
9eb53ac3 | 2128 | //\r |
2129 | // If Type is TypeStart then only compare the begining of the strings\r | |
2130 | //\r | |
1e6e84c7 | 2131 | if ( ((SHELL_PARAM_PACKAGE*)Node)->Type == TypeStart\r |
9eb53ac3 | 2132 | && StrnCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name, StrLen(KeyString)) == 0\r |
2133 | ){\r | |
2134 | //\r | |
2135 | // return the string part after the flag\r | |
2136 | //\r | |
2137 | return (((SHELL_PARAM_PACKAGE*)Node)->Name + StrLen(KeyString));\r | |
2138 | } else if (StrCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {\r | |
2139 | //\r | |
2140 | // return the value\r | |
2141 | //\r | |
94b17fa1 | 2142 | return (((SHELL_PARAM_PACKAGE*)Node)->Value);\r |
2143 | }\r | |
2144 | }\r | |
2145 | }\r | |
2146 | return (NULL);\r | |
2147 | }\r | |
2148 | /**\r | |
2149 | returns raw value from command line argument\r | |
2150 | \r | |
2151 | raw value parameters are in the form of "value" in a specific position in the list\r | |
1e6e84c7 | 2152 | \r |
94b17fa1 | 2153 | if CheckPackage is NULL, then return NULL;\r |
2154 | \r | |
2155 | @param CheckPackage The package of parsed command line arguments\r | |
1e6e84c7 | 2156 | @param Position the position of the value\r |
94b17fa1 | 2157 | \r |
2158 | @retval NULL the flag is not on the command line\r | |
2159 | @return !=NULL pointer to unicode string of the value\r | |
2160 | **/\r | |
2161 | CONST CHAR16*\r | |
2162 | EFIAPI\r | |
2163 | ShellCommandLineGetRawValue (\r | |
2164 | IN CONST LIST_ENTRY *CheckPackage,\r | |
2165 | IN UINT32 Position\r | |
2247dde4 | 2166 | ) {\r |
94b17fa1 | 2167 | LIST_ENTRY *Node;\r |
2168 | \r | |
2169 | //\r | |
2170 | // check for CheckPackage == NULL\r | |
2171 | //\r | |
2172 | if (CheckPackage == NULL) {\r | |
2173 | return (NULL);\r | |
2174 | }\r | |
2175 | \r | |
2176 | //\r | |
2177 | // enumerate through the list of parametrs\r | |
2178 | //\r | |
1e6e84c7 | 2179 | for ( Node = GetFirstNode(CheckPackage)\r |
2180 | ; !IsNull (CheckPackage, Node)\r | |
2181 | ; Node = GetNextNode(CheckPackage, Node)\r | |
b82bfcc1 | 2182 | ){\r |
94b17fa1 | 2183 | //\r |
2184 | // If the position matches, return the value\r | |
2185 | //\r | |
2186 | if (((SHELL_PARAM_PACKAGE*)Node)->OriginalPosition == Position) {\r | |
2187 | return (((SHELL_PARAM_PACKAGE*)Node)->Value);\r | |
2188 | }\r | |
2189 | }\r | |
2190 | return (NULL);\r | |
b1f95a06 | 2191 | }\r |
2247dde4 | 2192 | \r |
2193 | /**\r | |
1e6e84c7 | 2194 | returns the number of command line value parameters that were parsed.\r |
2195 | \r | |
2247dde4 | 2196 | this will not include flags.\r |
2197 | \r | |
2198 | @retval (UINTN)-1 No parsing has ocurred\r | |
2199 | @return other The number of value parameters found\r | |
2200 | **/\r | |
2201 | UINTN\r | |
2202 | EFIAPI\r | |
2203 | ShellCommandLineGetCount(\r | |
2204 | VOID\r | |
125c2cf4 | 2205 | )\r |
2206 | {\r | |
2247dde4 | 2207 | return (mTotalParameterCount);\r |
2208 | }\r | |
2209 | \r | |
36a9d672 | 2210 | /**\r |
2211 | Determins if a parameter is duplicated.\r | |
2212 | \r | |
1e6e84c7 | 2213 | If Param is not NULL then it will point to a callee allocated string buffer\r |
36a9d672 | 2214 | with the parameter value if a duplicate is found.\r |
2215 | \r | |
2216 | If CheckPackage is NULL, then ASSERT.\r | |
2217 | \r | |
2218 | @param[in] CheckPackage The package of parsed command line arguments.\r | |
2219 | @param[out] Param Upon finding one, a pointer to the duplicated parameter.\r | |
2220 | \r | |
2221 | @retval EFI_SUCCESS No parameters were duplicated.\r | |
2222 | @retval EFI_DEVICE_ERROR A duplicate was found.\r | |
2223 | **/\r | |
2224 | EFI_STATUS\r | |
2225 | EFIAPI\r | |
2226 | ShellCommandLineCheckDuplicate (\r | |
2227 | IN CONST LIST_ENTRY *CheckPackage,\r | |
2228 | OUT CHAR16 **Param\r | |
2229 | )\r | |
2230 | {\r | |
2231 | LIST_ENTRY *Node1;\r | |
2232 | LIST_ENTRY *Node2;\r | |
1e6e84c7 | 2233 | \r |
36a9d672 | 2234 | ASSERT(CheckPackage != NULL);\r |
2235 | \r | |
1e6e84c7 | 2236 | for ( Node1 = GetFirstNode(CheckPackage)\r |
2237 | ; !IsNull (CheckPackage, Node1)\r | |
2238 | ; Node1 = GetNextNode(CheckPackage, Node1)\r | |
36a9d672 | 2239 | ){\r |
1e6e84c7 | 2240 | for ( Node2 = GetNextNode(CheckPackage, Node1)\r |
2241 | ; !IsNull (CheckPackage, Node2)\r | |
2242 | ; Node2 = GetNextNode(CheckPackage, Node2)\r | |
36a9d672 | 2243 | ){\r |
2244 | if (StrCmp(((SHELL_PARAM_PACKAGE*)Node1)->Name, ((SHELL_PARAM_PACKAGE*)Node2)->Name) == 0) {\r | |
2245 | if (Param != NULL) {\r | |
2246 | *Param = NULL;\r | |
2247 | *Param = StrnCatGrow(Param, NULL, ((SHELL_PARAM_PACKAGE*)Node1)->Name, 0);\r | |
2248 | }\r | |
2249 | return (EFI_DEVICE_ERROR);\r | |
2250 | }\r | |
2251 | }\r | |
2252 | }\r | |
2253 | return (EFI_SUCCESS);\r | |
2254 | }\r | |
2255 | \r | |
975136ab | 2256 | /**\r |
1e6e84c7 | 2257 | This is a find and replace function. Upon successful return the NewString is a copy of\r |
975136ab | 2258 | SourceString with each instance of FindTarget replaced with ReplaceWith.\r |
2259 | \r | |
b3011f40 | 2260 | If SourceString and NewString overlap the behavior is undefined.\r |
2261 | \r | |
975136ab | 2262 | If the string would grow bigger than NewSize it will halt and return error.\r |
2263 | \r | |
2264 | @param[in] SourceString String with source buffer\r | |
b82bfcc1 | 2265 | @param[in,out] NewString String with resultant buffer\r |
975136ab | 2266 | @param[in] NewSize Size in bytes of NewString\r |
2267 | @param[in] FindTarget String to look for\r | |
2268 | @param[in[ ReplaceWith String to replace FindTarget with\r | |
969c783b | 2269 | @param[in] SkipPreCarrot If TRUE will skip a FindTarget that has a '^'\r |
2270 | immediately before it.\r | |
2271 | \r | |
2272 | @retval EFI_INVALID_PARAMETER SourceString was NULL.\r | |
2273 | @retval EFI_INVALID_PARAMETER NewString was NULL.\r | |
2274 | @retval EFI_INVALID_PARAMETER FindTarget was NULL.\r | |
2275 | @retval EFI_INVALID_PARAMETER ReplaceWith was NULL.\r | |
2276 | @retval EFI_INVALID_PARAMETER FindTarget had length < 1.\r | |
2277 | @retval EFI_INVALID_PARAMETER SourceString had length < 1.\r | |
1e6e84c7 | 2278 | @retval EFI_BUFFER_TOO_SMALL NewSize was less than the minimum size to hold\r |
969c783b | 2279 | the new string (truncation occurred).\r |
2280 | @retval EFI_SUCCESS the string was sucessfully copied with replacement.\r | |
975136ab | 2281 | **/\r |
2282 | \r | |
2283 | EFI_STATUS\r | |
2284 | EFIAPI\r | |
969c783b | 2285 | ShellCopySearchAndReplace2(\r |
975136ab | 2286 | IN CHAR16 CONST *SourceString,\r |
2287 | IN CHAR16 *NewString,\r | |
2288 | IN UINTN NewSize,\r | |
2289 | IN CONST CHAR16 *FindTarget,\r | |
969c783b | 2290 | IN CONST CHAR16 *ReplaceWith,\r |
2291 | IN CONST BOOLEAN SkipPreCarrot\r | |
1e6e84c7 | 2292 | )\r |
2247dde4 | 2293 | {\r |
0158294b | 2294 | UINTN Size;\r |
975136ab | 2295 | if ( (SourceString == NULL)\r |
2296 | || (NewString == NULL)\r | |
2297 | || (FindTarget == NULL)\r | |
2298 | || (ReplaceWith == NULL)\r | |
2299 | || (StrLen(FindTarget) < 1)\r | |
2300 | || (StrLen(SourceString) < 1)\r | |
2301 | ){\r | |
2302 | return (EFI_INVALID_PARAMETER);\r | |
2303 | }\r | |
2247dde4 | 2304 | NewString = SetMem16(NewString, NewSize, CHAR_NULL);\r |
2305 | while (*SourceString != CHAR_NULL) {\r | |
969c783b | 2306 | //\r |
1e6e84c7 | 2307 | // if we find the FindTarget and either Skip == FALSE or Skip == TRUE and we\r |
969c783b | 2308 | // dont have a carrot do a replace...\r |
2309 | //\r | |
1e6e84c7 | 2310 | if (StrnCmp(SourceString, FindTarget, StrLen(FindTarget)) == 0\r |
969c783b | 2311 | && ((SkipPreCarrot && *(SourceString-1) != L'^') || SkipPreCarrot == FALSE)\r |
2312 | ){\r | |
975136ab | 2313 | SourceString += StrLen(FindTarget);\r |
0158294b | 2314 | Size = StrSize(NewString);\r |
2315 | if ((Size + (StrLen(ReplaceWith)*sizeof(CHAR16))) > NewSize) {\r | |
975136ab | 2316 | return (EFI_BUFFER_TOO_SMALL);\r |
2317 | }\r | |
2318 | StrCat(NewString, ReplaceWith);\r | |
2319 | } else {\r | |
0158294b | 2320 | Size = StrSize(NewString);\r |
2321 | if (Size + sizeof(CHAR16) > NewSize) {\r | |
975136ab | 2322 | return (EFI_BUFFER_TOO_SMALL);\r |
2323 | }\r | |
2324 | StrnCat(NewString, SourceString, 1);\r | |
2325 | SourceString++;\r | |
2326 | }\r | |
2327 | }\r | |
2328 | return (EFI_SUCCESS);\r | |
2329 | }\r | |
b1f95a06 | 2330 | \r |
e2f8297f | 2331 | /**\r |
2332 | Internal worker function to output a string.\r | |
2333 | \r | |
2334 | This function will output a string to the correct StdOut.\r | |
2335 | \r | |
2336 | @param[in] String The string to print out.\r | |
2337 | \r | |
2338 | @retval EFI_SUCCESS The operation was sucessful.\r | |
2339 | @retval !EFI_SUCCESS The operation failed.\r | |
2340 | **/\r | |
2341 | EFI_STATUS\r | |
2342 | EFIAPI\r | |
2343 | InternalPrintTo (\r | |
2344 | IN CONST CHAR16 *String\r | |
2345 | )\r | |
2346 | {\r | |
2347 | UINTN Size;\r | |
2348 | Size = StrSize(String) - sizeof(CHAR16);\r | |
2349 | if (mEfiShellParametersProtocol != NULL) {\r | |
2350 | return (mEfiShellParametersProtocol->StdOut->Write(mEfiShellParametersProtocol->StdOut, &Size, (VOID*)String));\r | |
2351 | }\r | |
2352 | if (mEfiShellInterface != NULL) {\r | |
ecd3d59f | 2353 | //\r |
2354 | // Divide in half for old shell. Must be string length not size.\r | |
2355 | //\r | |
2356 | Size /= 2;\r | |
e2f8297f | 2357 | return ( mEfiShellInterface->StdOut->Write(mEfiShellInterface->StdOut, &Size, (VOID*)String));\r |
2358 | }\r | |
2359 | ASSERT(FALSE);\r | |
2360 | return (EFI_UNSUPPORTED);\r | |
2361 | }\r | |
2362 | \r | |
b1f95a06 | 2363 | /**\r |
2364 | Print at a specific location on the screen.\r | |
2365 | \r | |
f1b87e7a | 2366 | This function will move the cursor to a given screen location and print the specified string\r |
1e6e84c7 | 2367 | \r |
2368 | If -1 is specified for either the Row or Col the current screen location for BOTH\r | |
f1b87e7a | 2369 | will be used.\r |
b1f95a06 | 2370 | \r |
2371 | if either Row or Col is out of range for the current console, then ASSERT\r | |
2372 | if Format is NULL, then ASSERT\r | |
2373 | \r | |
1e6e84c7 | 2374 | In addition to the standard %-based flags as supported by UefiLib Print() this supports\r |
b1f95a06 | 2375 | the following additional flags:\r |
2376 | %N - Set output attribute to normal\r | |
2377 | %H - Set output attribute to highlight\r | |
2378 | %E - Set output attribute to error\r | |
2379 | %B - Set output attribute to blue color\r | |
2380 | %V - Set output attribute to green color\r | |
2381 | \r | |
2382 | Note: The background color is controlled by the shell command cls.\r | |
2383 | \r | |
2384 | @param[in] Row the row to print at\r | |
2385 | @param[in] Col the column to print at\r | |
2386 | @param[in] Format the format string\r | |
2247dde4 | 2387 | @param[in] Marker the marker for the variable argument list\r |
b1f95a06 | 2388 | \r |
2389 | @return the number of characters printed to the screen\r | |
2390 | **/\r | |
2391 | \r | |
2392 | UINTN\r | |
2393 | EFIAPI\r | |
2247dde4 | 2394 | InternalShellPrintWorker(\r |
b1f95a06 | 2395 | IN INT32 Col OPTIONAL,\r |
2396 | IN INT32 Row OPTIONAL,\r | |
2397 | IN CONST CHAR16 *Format,\r | |
2247dde4 | 2398 | VA_LIST Marker\r |
1e6e84c7 | 2399 | )\r |
2247dde4 | 2400 | {\r |
b1f95a06 | 2401 | UINTN Return;\r |
b1f95a06 | 2402 | EFI_STATUS Status;\r |
975136ab | 2403 | UINTN NormalAttribute;\r |
2404 | CHAR16 *ResumeLocation;\r | |
2405 | CHAR16 *FormatWalker;\r | |
1e6e84c7 | 2406 | \r |
975136ab | 2407 | //\r |
2408 | // Back and forth each time fixing up 1 of our flags...\r | |
2409 | //\r | |
b3011f40 | 2410 | Status = ShellLibCopySearchAndReplace(Format, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%N", L"%%N");\r |
975136ab | 2411 | ASSERT_EFI_ERROR(Status);\r |
b3011f40 | 2412 | Status = ShellLibCopySearchAndReplace(mPostReplaceFormat, mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), L"%E", L"%%E");\r |
975136ab | 2413 | ASSERT_EFI_ERROR(Status);\r |
b3011f40 | 2414 | Status = ShellLibCopySearchAndReplace(mPostReplaceFormat2, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%H", L"%%H");\r |
975136ab | 2415 | ASSERT_EFI_ERROR(Status);\r |
b3011f40 | 2416 | Status = ShellLibCopySearchAndReplace(mPostReplaceFormat, mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), L"%B", L"%%B");\r |
975136ab | 2417 | ASSERT_EFI_ERROR(Status);\r |
b3011f40 | 2418 | Status = ShellLibCopySearchAndReplace(mPostReplaceFormat2, mPostReplaceFormat, PcdGet16 (PcdShellPrintBufferSize), L"%V", L"%%V");\r |
975136ab | 2419 | ASSERT_EFI_ERROR(Status);\r |
2420 | \r | |
2421 | //\r | |
2422 | // Use the last buffer from replacing to print from...\r | |
2423 | //\r | |
b3011f40 | 2424 | Return = UnicodeVSPrint (mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), mPostReplaceFormat, Marker);\r |
b1f95a06 | 2425 | \r |
2426 | if (Col != -1 && Row != -1) {\r | |
b1f95a06 | 2427 | Status = gST->ConOut->SetCursorPosition(gST->ConOut, Col, Row);\r |
2428 | ASSERT_EFI_ERROR(Status);\r | |
975136ab | 2429 | }\r |
2430 | \r | |
2431 | NormalAttribute = gST->ConOut->Mode->Attribute;\r | |
ecd3d59f | 2432 | FormatWalker = mPostReplaceFormat2;\r |
2247dde4 | 2433 | while (*FormatWalker != CHAR_NULL) {\r |
975136ab | 2434 | //\r |
2435 | // Find the next attribute change request\r | |
2436 | //\r | |
2437 | ResumeLocation = StrStr(FormatWalker, L"%");\r | |
2438 | if (ResumeLocation != NULL) {\r | |
2247dde4 | 2439 | *ResumeLocation = CHAR_NULL;\r |
975136ab | 2440 | }\r |
2441 | //\r | |
2442 | // print the current FormatWalker string\r | |
2443 | //\r | |
e2f8297f | 2444 | Status = InternalPrintTo(FormatWalker);\r |
b1f95a06 | 2445 | ASSERT_EFI_ERROR(Status);\r |
975136ab | 2446 | //\r |
2447 | // update the attribute\r | |
2448 | //\r | |
2449 | if (ResumeLocation != NULL) {\r | |
2450 | switch (*(ResumeLocation+1)) {\r | |
2451 | case (L'N'):\r | |
2452 | gST->ConOut->SetAttribute(gST->ConOut, NormalAttribute);\r | |
2453 | break;\r | |
2454 | case (L'E'):\r | |
2455 | gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_YELLOW, ((NormalAttribute&(BIT4|BIT5|BIT6))>>4)));\r | |
2456 | break;\r | |
2457 | case (L'H'):\r | |
2458 | gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_WHITE, ((NormalAttribute&(BIT4|BIT5|BIT6))>>4)));\r | |
2459 | break;\r | |
2460 | case (L'B'):\r | |
2461 | gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_BLUE, ((NormalAttribute&(BIT4|BIT5|BIT6))>>4)));\r | |
2462 | break;\r | |
2463 | case (L'V'):\r | |
2464 | gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_GREEN, ((NormalAttribute&(BIT4|BIT5|BIT6))>>4)));\r | |
2465 | break;\r | |
2466 | default:\r | |
e2f8297f | 2467 | //\r |
2468 | // Print a simple '%' symbol\r | |
2469 | //\r | |
2470 | Status = InternalPrintTo(L"%");\r | |
2471 | ASSERT_EFI_ERROR(Status);\r | |
2472 | ResumeLocation = ResumeLocation - 1;\r | |
975136ab | 2473 | break;\r |
2474 | }\r | |
2475 | } else {\r | |
2476 | //\r | |
2477 | // reset to normal now...\r | |
2478 | //\r | |
2479 | gST->ConOut->SetAttribute(gST->ConOut, NormalAttribute);\r | |
2480 | break;\r | |
2481 | }\r | |
2482 | \r | |
2483 | //\r | |
2484 | // update FormatWalker to Resume + 2 (skip the % and the indicator)\r | |
2485 | //\r | |
2486 | FormatWalker = ResumeLocation + 2;\r | |
2487 | }\r | |
b1f95a06 | 2488 | \r |
b1f95a06 | 2489 | return (Return);\r |
5f7431d0 | 2490 | }\r |
2247dde4 | 2491 | \r |
2492 | /**\r | |
2493 | Print at a specific location on the screen.\r | |
2494 | \r | |
e2f8297f | 2495 | This function will move the cursor to a given screen location and print the specified string.\r |
1e6e84c7 | 2496 | \r |
2497 | If -1 is specified for either the Row or Col the current screen location for BOTH\r | |
2247dde4 | 2498 | will be used.\r |
2499 | \r | |
e2f8297f | 2500 | If either Row or Col is out of range for the current console, then ASSERT.\r |
2501 | If Format is NULL, then ASSERT.\r | |
2247dde4 | 2502 | \r |
1e6e84c7 | 2503 | In addition to the standard %-based flags as supported by UefiLib Print() this supports\r |
2247dde4 | 2504 | the following additional flags:\r |
2505 | %N - Set output attribute to normal\r | |
2506 | %H - Set output attribute to highlight\r | |
2507 | %E - Set output attribute to error\r | |
2508 | %B - Set output attribute to blue color\r | |
2509 | %V - Set output attribute to green color\r | |
2510 | \r | |
2511 | Note: The background color is controlled by the shell command cls.\r | |
2512 | \r | |
2513 | @param[in] Row the row to print at\r | |
2514 | @param[in] Col the column to print at\r | |
2515 | @param[in] Format the format string\r | |
2516 | \r | |
2517 | @return the number of characters printed to the screen\r | |
2518 | **/\r | |
2519 | \r | |
2520 | UINTN\r | |
2521 | EFIAPI\r | |
2522 | ShellPrintEx(\r | |
2523 | IN INT32 Col OPTIONAL,\r | |
2524 | IN INT32 Row OPTIONAL,\r | |
2525 | IN CONST CHAR16 *Format,\r | |
2526 | ...\r | |
1e6e84c7 | 2527 | )\r |
2247dde4 | 2528 | {\r |
2529 | VA_LIST Marker;\r | |
e2f8297f | 2530 | EFI_STATUS Status;\r |
2247dde4 | 2531 | VA_START (Marker, Format);\r |
e2f8297f | 2532 | Status = InternalShellPrintWorker(Col, Row, Format, Marker);\r |
2533 | VA_END(Marker);\r | |
2534 | return(Status);\r | |
2247dde4 | 2535 | }\r |
2536 | \r | |
2537 | /**\r | |
2538 | Print at a specific location on the screen.\r | |
2539 | \r | |
e2f8297f | 2540 | This function will move the cursor to a given screen location and print the specified string.\r |
1e6e84c7 | 2541 | \r |
2542 | If -1 is specified for either the Row or Col the current screen location for BOTH\r | |
e2f8297f | 2543 | will be used.\r |
2247dde4 | 2544 | \r |
e2f8297f | 2545 | If either Row or Col is out of range for the current console, then ASSERT.\r |
2546 | If Format is NULL, then ASSERT.\r | |
2247dde4 | 2547 | \r |
1e6e84c7 | 2548 | In addition to the standard %-based flags as supported by UefiLib Print() this supports\r |
2247dde4 | 2549 | the following additional flags:\r |
1e6e84c7 | 2550 | %N - Set output attribute to normal.\r |
2551 | %H - Set output attribute to highlight.\r | |
2552 | %E - Set output attribute to error.\r | |
2553 | %B - Set output attribute to blue color.\r | |
2554 | %V - Set output attribute to green color.\r | |
2247dde4 | 2555 | \r |
2556 | Note: The background color is controlled by the shell command cls.\r | |
2557 | \r | |
1e6e84c7 | 2558 | @param[in] Row The row to print at.\r |
2559 | @param[in] Col The column to print at.\r | |
2560 | @param[in] Language The language of the string to retrieve. If this parameter\r | |
2561 | is NULL, then the current platform language is used.\r | |
2562 | @param[in] HiiFormatStringId The format string Id for getting from Hii.\r | |
2563 | @param[in] HiiFormatHandle The format string Handle for getting from Hii.\r | |
2247dde4 | 2564 | \r |
1e6e84c7 | 2565 | @return the number of characters printed to the screen.\r |
2247dde4 | 2566 | **/\r |
2567 | UINTN\r | |
2568 | EFIAPI\r | |
2569 | ShellPrintHiiEx(\r | |
2570 | IN INT32 Col OPTIONAL,\r | |
2571 | IN INT32 Row OPTIONAL,\r | |
1e6e84c7 | 2572 | IN CONST CHAR8 *Language OPTIONAL,\r |
2247dde4 | 2573 | IN CONST EFI_STRING_ID HiiFormatStringId,\r |
2574 | IN CONST EFI_HANDLE HiiFormatHandle,\r | |
2575 | ...\r | |
2576 | )\r | |
2577 | {\r | |
2578 | VA_LIST Marker;\r | |
2579 | CHAR16 *HiiFormatString;\r | |
2580 | UINTN RetVal;\r | |
2581 | \r | |
2582 | VA_START (Marker, HiiFormatHandle);\r | |
1e6e84c7 | 2583 | HiiFormatString = HiiGetString(HiiFormatHandle, HiiFormatStringId, Language);\r |
2247dde4 | 2584 | ASSERT(HiiFormatString != NULL);\r |
2585 | \r | |
2586 | RetVal = InternalShellPrintWorker(Col, Row, HiiFormatString, Marker);\r | |
2587 | \r | |
2588 | FreePool(HiiFormatString);\r | |
e2f8297f | 2589 | VA_END(Marker);\r |
2247dde4 | 2590 | \r |
2591 | return (RetVal);\r | |
2592 | }\r | |
2593 | \r | |
2594 | /**\r | |
2595 | Function to determine if a given filename represents a file or a directory.\r | |
2596 | \r | |
2597 | @param[in] DirName Path to directory to test.\r | |
2598 | \r | |
2599 | @retval EFI_SUCCESS The Path represents a directory\r | |
2600 | @retval EFI_NOT_FOUND The Path does not represent a directory\r | |
2601 | @return other The path failed to open\r | |
2602 | **/\r | |
2603 | EFI_STATUS\r | |
2604 | EFIAPI\r | |
2605 | ShellIsDirectory(\r | |
2606 | IN CONST CHAR16 *DirName\r | |
2607 | )\r | |
2608 | {\r | |
2609 | EFI_STATUS Status;\r | |
2610 | EFI_FILE_HANDLE Handle;\r | |
2611 | \r | |
ecd3d59f | 2612 | ASSERT(DirName != NULL);\r |
2613 | \r | |
2247dde4 | 2614 | Handle = NULL;\r |
2615 | \r | |
2616 | Status = ShellOpenFileByName(DirName, &Handle, EFI_FILE_MODE_READ, 0);\r | |
2617 | if (EFI_ERROR(Status)) {\r | |
2618 | return (Status);\r | |
2619 | }\r | |
2620 | \r | |
2621 | if (FileHandleIsDirectory(Handle) == EFI_SUCCESS) {\r | |
2622 | ShellCloseFile(&Handle);\r | |
2623 | return (EFI_SUCCESS);\r | |
2624 | }\r | |
2625 | ShellCloseFile(&Handle);\r | |
2626 | return (EFI_NOT_FOUND);\r | |
2627 | }\r | |
2628 | \r | |
36a9d672 | 2629 | /**\r |
2630 | Function to determine if a given filename represents a file.\r | |
2631 | \r | |
2632 | @param[in] Name Path to file to test.\r | |
2633 | \r | |
2634 | @retval EFI_SUCCESS The Path represents a file.\r | |
2635 | @retval EFI_NOT_FOUND The Path does not represent a file.\r | |
2636 | @retval other The path failed to open.\r | |
2637 | **/\r | |
2638 | EFI_STATUS\r | |
2639 | EFIAPI\r | |
2640 | ShellIsFile(\r | |
2641 | IN CONST CHAR16 *Name\r | |
2642 | )\r | |
2643 | {\r | |
2644 | EFI_STATUS Status;\r | |
2645 | EFI_FILE_HANDLE Handle;\r | |
2646 | \r | |
ecd3d59f | 2647 | ASSERT(Name != NULL);\r |
2648 | \r | |
36a9d672 | 2649 | Handle = NULL;\r |
2650 | \r | |
2651 | Status = ShellOpenFileByName(Name, &Handle, EFI_FILE_MODE_READ, 0);\r | |
2652 | if (EFI_ERROR(Status)) {\r | |
2653 | return (Status);\r | |
2654 | }\r | |
2655 | \r | |
2656 | if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {\r | |
2657 | ShellCloseFile(&Handle);\r | |
2658 | return (EFI_SUCCESS);\r | |
2659 | }\r | |
2660 | ShellCloseFile(&Handle);\r | |
2661 | return (EFI_NOT_FOUND);\r | |
2662 | }\r | |
2663 | \r | |
b3011f40 | 2664 | /**\r |
2665 | Function to determine if a given filename represents a file.\r | |
2666 | \r | |
2667 | This will search the CWD and then the Path.\r | |
2668 | \r | |
2669 | If Name is NULL, then ASSERT.\r | |
2670 | \r | |
2671 | @param[in] Name Path to file to test.\r | |
2672 | \r | |
2673 | @retval EFI_SUCCESS The Path represents a file.\r | |
2674 | @retval EFI_NOT_FOUND The Path does not represent a file.\r | |
2675 | @retval other The path failed to open.\r | |
2676 | **/\r | |
2677 | EFI_STATUS\r | |
2678 | EFIAPI\r | |
2679 | ShellIsFileInPath(\r | |
2680 | IN CONST CHAR16 *Name\r | |
2681 | ) {\r | |
2682 | CHAR16 *NewName;\r | |
2683 | EFI_STATUS Status;\r | |
2684 | \r | |
2685 | if (!EFI_ERROR(ShellIsFile(Name))) {\r | |
2686 | return (TRUE);\r | |
2687 | }\r | |
2688 | \r | |
2689 | NewName = ShellFindFilePath(Name);\r | |
2690 | if (NewName == NULL) {\r | |
2691 | return (EFI_NOT_FOUND);\r | |
2692 | }\r | |
2693 | Status = ShellIsFile(NewName);\r | |
2694 | FreePool(NewName);\r | |
2695 | return (Status);\r | |
2696 | }\r | |
125c2cf4 | 2697 | /**\r |
1e6e84c7 | 2698 | Function to determine whether a string is decimal or hex representation of a number\r |
125c2cf4 | 2699 | and return the number converted from the string.\r |
2700 | \r | |
2701 | @param[in] String String representation of a number\r | |
2702 | \r | |
2703 | @retval all the number\r | |
2704 | **/\r | |
2705 | UINTN\r | |
2706 | EFIAPI\r | |
2707 | ShellStrToUintn(\r | |
2708 | IN CONST CHAR16 *String\r | |
2709 | )\r | |
2710 | {\r | |
2711 | CONST CHAR16 *Walker;\r | |
b3011f40 | 2712 | for (Walker = String; Walker != NULL && *Walker != CHAR_NULL && *Walker == L' '; Walker++);\r |
1cd45e78 | 2713 | if (Walker == NULL || *Walker == CHAR_NULL) {\r |
2714 | ASSERT(FALSE);\r | |
2715 | return ((UINTN)(-1));\r | |
2716 | } else {\r | |
2717 | if (StrnCmp(Walker, L"0x", 2) == 0 || StrnCmp(Walker, L"0X", 2) == 0){\r | |
2718 | return (StrHexToUintn(Walker));\r | |
2719 | }\r | |
2720 | return (StrDecimalToUintn(Walker));\r | |
125c2cf4 | 2721 | }\r |
125c2cf4 | 2722 | }\r |
2723 | \r | |
2724 | /**\r | |
2725 | Safely append with automatic string resizing given length of Destination and\r | |
2726 | desired length of copy from Source.\r | |
2727 | \r | |
2728 | append the first D characters of Source to the end of Destination, where D is\r | |
2729 | the lesser of Count and the StrLen() of Source. If appending those D characters\r | |
2730 | will fit within Destination (whose Size is given as CurrentSize) and\r | |
1e6e84c7 | 2731 | still leave room for a NULL terminator, then those characters are appended,\r |
2732 | starting at the original terminating NULL of Destination, and a new terminating\r | |
2733 | NULL is appended.\r | |
125c2cf4 | 2734 | \r |
2735 | If appending D characters onto Destination will result in a overflow of the size\r | |
2736 | given in CurrentSize the string will be grown such that the copy can be performed\r | |
2737 | and CurrentSize will be updated to the new size.\r | |
2738 | \r | |
2739 | If Source is NULL, there is nothing to append, just return the current buffer in\r | |
2740 | Destination.\r | |
2741 | \r | |
2742 | if Destination is NULL, then ASSERT()\r | |
2743 | if Destination's current length (including NULL terminator) is already more then\r | |
2744 | CurrentSize, then ASSERT()\r | |
2745 | \r | |
2746 | @param[in,out] Destination The String to append onto\r | |
2747 | @param[in,out] CurrentSize on call the number of bytes in Destination. On\r | |
2748 | return possibly the new size (still in bytes). if NULL\r | |
2749 | then allocate whatever is needed.\r | |
2750 | @param[in] Source The String to append from\r | |
2751 | @param[in] Count Maximum number of characters to append. if 0 then\r | |
2752 | all are appended.\r | |
2753 | \r | |
2754 | @return Destination return the resultant string.\r | |
2755 | **/\r | |
2756 | CHAR16*\r | |
2757 | EFIAPI\r | |
2758 | StrnCatGrow (\r | |
2759 | IN OUT CHAR16 **Destination,\r | |
2760 | IN OUT UINTN *CurrentSize,\r | |
2761 | IN CONST CHAR16 *Source,\r | |
2762 | IN UINTN Count\r | |
2763 | )\r | |
2764 | {\r | |
2765 | UINTN DestinationStartSize;\r | |
2766 | UINTN NewSize;\r | |
2767 | \r | |
2768 | //\r | |
2769 | // ASSERTs\r | |
2770 | //\r | |
2771 | ASSERT(Destination != NULL);\r | |
2772 | \r | |
2773 | //\r | |
2774 | // If there's nothing to do then just return Destination\r | |
2775 | //\r | |
2776 | if (Source == NULL) {\r | |
2777 | return (*Destination);\r | |
2778 | }\r | |
2779 | \r | |
2780 | //\r | |
2781 | // allow for un-initialized pointers, based on size being 0\r | |
2782 | //\r | |
2783 | if (CurrentSize != NULL && *CurrentSize == 0) {\r | |
2784 | *Destination = NULL;\r | |
2785 | }\r | |
2786 | \r | |
2787 | //\r | |
2788 | // allow for NULL pointers address as Destination\r | |
2789 | //\r | |
2790 | if (*Destination != NULL) {\r | |
2791 | ASSERT(CurrentSize != 0);\r | |
2792 | DestinationStartSize = StrSize(*Destination);\r | |
2793 | ASSERT(DestinationStartSize <= *CurrentSize);\r | |
2794 | } else {\r | |
2795 | DestinationStartSize = 0;\r | |
2796 | // ASSERT(*CurrentSize == 0);\r | |
2797 | }\r | |
2798 | \r | |
2799 | //\r | |
2800 | // Append all of Source?\r | |
2801 | //\r | |
2802 | if (Count == 0) {\r | |
2803 | Count = StrLen(Source);\r | |
2804 | }\r | |
2805 | \r | |
2806 | //\r | |
2807 | // Test and grow if required\r | |
2808 | //\r | |
2809 | if (CurrentSize != NULL) {\r | |
2810 | NewSize = *CurrentSize;\r | |
2811 | while (NewSize < (DestinationStartSize + (Count*sizeof(CHAR16)))) {\r | |
2812 | NewSize += 2 * Count * sizeof(CHAR16);\r | |
2813 | }\r | |
2814 | *Destination = ReallocatePool(*CurrentSize, NewSize, *Destination);\r | |
c9d92df0 | 2815 | ASSERT(*Destination != NULL);\r |
125c2cf4 | 2816 | *CurrentSize = NewSize;\r |
2817 | } else {\r | |
2818 | *Destination = AllocateZeroPool((Count+1)*sizeof(CHAR16));\r | |
c9d92df0 | 2819 | ASSERT(*Destination != NULL);\r |
125c2cf4 | 2820 | }\r |
2821 | \r | |
2822 | //\r | |
2823 | // Now use standard StrnCat on a big enough buffer\r | |
2824 | //\r | |
c9d92df0 | 2825 | if (*Destination == NULL) {\r |
2826 | return (NULL);\r | |
2827 | }\r | |
125c2cf4 | 2828 | return StrnCat(*Destination, Source, Count);\r |
2829 | }\r | |
c9d92df0 | 2830 | \r |
2831 | /**\r | |
2832 | Prompt the user and return the resultant answer to the requestor.\r | |
2833 | \r | |
2834 | This function will display the requested question on the shell prompt and then\r | |
2835 | wait for an apropriate answer to be input from the console.\r | |
2836 | \r | |
2837 | if the SHELL_PROMPT_REQUEST_TYPE is SHELL_PROMPT_REQUEST_TYPE_YESNO, SHELL_PROMPT_REQUEST_TYPE_QUIT_CONTINUE\r | |
2838 | or SHELL_PROMPT_REQUEST_TYPE_YESNOCANCEL then *Response is of type SHELL_PROMPT_RESPONSE.\r | |
2839 | \r | |
2840 | if the SHELL_PROMPT_REQUEST_TYPE is SHELL_PROMPT_REQUEST_TYPE_FREEFORM then *Response is of type\r | |
2841 | CHAR16*.\r | |
2842 | \r | |
2843 | In either case *Response must be callee freed if Response was not NULL;\r | |
2844 | \r | |
2845 | @param Type What type of question is asked. This is used to filter the input\r | |
2846 | to prevent invalid answers to question.\r | |
2847 | @param Prompt Pointer to string prompt to use to request input.\r | |
2848 | @param Response Pointer to Response which will be populated upon return.\r | |
2849 | \r | |
2850 | @retval EFI_SUCCESS The operation was sucessful.\r | |
2851 | @retval EFI_UNSUPPORTED The operation is not supported as requested.\r | |
2852 | @retval EFI_INVALID_PARAMETER A parameter was invalid.\r | |
2853 | @return other The operation failed.\r | |
2854 | **/\r | |
2855 | EFI_STATUS\r | |
2856 | EFIAPI\r | |
2857 | ShellPromptForResponse (\r | |
2858 | IN SHELL_PROMPT_REQUEST_TYPE Type,\r | |
2859 | IN CHAR16 *Prompt OPTIONAL,\r | |
2860 | IN OUT VOID **Response OPTIONAL\r | |
2861 | )\r | |
2862 | {\r | |
2863 | EFI_STATUS Status;\r | |
2864 | EFI_INPUT_KEY Key;\r | |
2865 | UINTN EventIndex;\r | |
2866 | SHELL_PROMPT_RESPONSE *Resp;\r | |
2867 | \r | |
2868 | Status = EFI_SUCCESS;\r | |
2869 | Resp = (SHELL_PROMPT_RESPONSE*)AllocatePool(sizeof(SHELL_PROMPT_RESPONSE));\r | |
2870 | \r | |
2871 | switch(Type) {\r | |
2872 | case SHELL_PROMPT_REQUEST_TYPE_QUIT_CONTINUE:\r | |
2873 | if (Prompt != NULL) {\r | |
2874 | ShellPrintEx(-1, -1, L"%s", Prompt);\r | |
2875 | }\r | |
2876 | //\r | |
2877 | // wait for valid response\r | |
2878 | //\r | |
2879 | gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);\r | |
2880 | Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);\r | |
2881 | ASSERT_EFI_ERROR(Status);\r | |
2882 | ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);\r | |
2883 | if (Key.UnicodeChar == L'Q' || Key.UnicodeChar ==L'q') {\r | |
2884 | *Resp = SHELL_PROMPT_RESPONSE_QUIT;\r | |
2885 | } else {\r | |
2886 | *Resp = SHELL_PROMPT_RESPONSE_CONTINUE;\r | |
2887 | }\r | |
2888 | break;\r | |
2889 | case SHELL_PROMPT_REQUEST_TYPE_YES_NO_ALL_CANCEL:\r | |
2890 | if (Prompt != NULL) {\r | |
2891 | ShellPrintEx(-1, -1, L"%s", Prompt);\r | |
2892 | }\r | |
2893 | //\r | |
2894 | // wait for valid response\r | |
2895 | //\r | |
2896 | *Resp = SHELL_PROMPT_RESPONSE_MAX;\r | |
2897 | while (*Resp == SHELL_PROMPT_RESPONSE_MAX) {\r | |
2898 | gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);\r | |
2899 | Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);\r | |
2900 | ASSERT_EFI_ERROR(Status);\r | |
2901 | ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);\r | |
2902 | switch (Key.UnicodeChar) {\r | |
2903 | case L'Y':\r | |
2904 | case L'y':\r | |
2905 | *Resp = SHELL_PROMPT_RESPONSE_YES;\r | |
2906 | break;\r | |
2907 | case L'N':\r | |
2908 | case L'n':\r | |
2909 | *Resp = SHELL_PROMPT_RESPONSE_NO;\r | |
2910 | break;\r | |
2911 | case L'A':\r | |
2912 | case L'a':\r | |
2913 | *Resp = SHELL_PROMPT_RESPONSE_ALL;\r | |
2914 | break;\r | |
2915 | case L'C':\r | |
2916 | case L'c':\r | |
2917 | *Resp = SHELL_PROMPT_RESPONSE_CANCEL;\r | |
2918 | break;\r | |
2919 | }\r | |
2920 | }\r | |
2921 | break;\r | |
2922 | case SHELL_PROMPT_REQUEST_TYPE_ENTER_TO_COMTINUE:\r | |
2923 | case SHELL_PROMPT_REQUEST_TYPE_ANYKEY_TO_COMTINUE:\r | |
2924 | if (Prompt != NULL) {\r | |
2925 | ShellPrintEx(-1, -1, L"%s", Prompt);\r | |
2926 | }\r | |
2927 | //\r | |
2928 | // wait for valid response\r | |
2929 | //\r | |
2930 | *Resp = SHELL_PROMPT_RESPONSE_MAX;\r | |
2931 | while (*Resp == SHELL_PROMPT_RESPONSE_MAX) {\r | |
2932 | gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);\r | |
2933 | if (Type == SHELL_PROMPT_REQUEST_TYPE_ENTER_TO_COMTINUE) {\r | |
2934 | Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);\r | |
2935 | ASSERT_EFI_ERROR(Status);\r | |
2936 | ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);\r | |
2937 | if (Key.UnicodeChar == CHAR_CARRIAGE_RETURN) {\r | |
2938 | *Resp = SHELL_PROMPT_RESPONSE_CONTINUE;\r | |
2939 | break;\r | |
2940 | }\r | |
2941 | }\r | |
2942 | if (Type == SHELL_PROMPT_REQUEST_TYPE_ANYKEY_TO_COMTINUE) {\r | |
2943 | *Resp = SHELL_PROMPT_RESPONSE_CONTINUE;\r | |
2944 | break;\r | |
2945 | }\r | |
2946 | }\r | |
2947 | break;\r | |
2948 | ///@todo add more request types here!\r | |
2949 | default:\r | |
2950 | Status = EFI_UNSUPPORTED;\r | |
2951 | }\r | |
2952 | \r | |
2953 | if (Response != NULL) {\r | |
2954 | *Response = Resp;\r | |
2955 | } else {\r | |
2956 | FreePool(Resp);\r | |
2957 | }\r | |
2958 | \r | |
2959 | return (Status);\r | |
2960 | }\r | |
2961 | \r | |
2962 | /**\r | |
2963 | Prompt the user and return the resultant answer to the requestor.\r | |
2964 | \r | |
2965 | This function is the same as ShellPromptForResponse, except that the prompt is\r | |
2966 | automatically pulled from HII.\r | |
2967 | \r | |
2968 | @param Type What type of question is asked. This is used to filter the input\r | |
2969 | to prevent invalid answers to question.\r | |
2970 | @param Prompt Pointer to string prompt to use to request input.\r | |
2971 | @param Response Pointer to Response which will be populated upon return.\r | |
2972 | \r | |
2973 | @retval EFI_SUCCESS the operation was sucessful.\r | |
2974 | @return other the operation failed.\r | |
2975 | \r | |
2976 | @sa ShellPromptForResponse\r | |
2977 | **/\r | |
2978 | EFI_STATUS\r | |
2979 | EFIAPI\r | |
2980 | ShellPromptForResponseHii (\r | |
2981 | IN SHELL_PROMPT_REQUEST_TYPE Type,\r | |
2982 | IN CONST EFI_STRING_ID HiiFormatStringId,\r | |
2983 | IN CONST EFI_HANDLE HiiFormatHandle,\r | |
2984 | IN OUT VOID **Response\r | |
2985 | )\r | |
2986 | {\r | |
2987 | CHAR16 *Prompt;\r | |
2988 | EFI_STATUS Status;\r | |
2989 | \r | |
2990 | Prompt = HiiGetString(HiiFormatHandle, HiiFormatStringId, NULL);\r | |
2991 | Status = ShellPromptForResponse(Type, Prompt, Response);\r | |
2992 | FreePool(Prompt);\r | |
2993 | return (Status);\r | |
2994 | }\r | |
2995 | \r | |
2996 | \r |