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