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