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