Commit | Line | Data |
---|---|---|
a405b86d | 1 | /** @file\r |
2 | Member functions of EFI_SHELL_PARAMETERS_PROTOCOL and functions for creation,\r | |
3 | manipulation, and initialization of EFI_SHELL_PARAMETERS_PROTOCOL.\r | |
4 | \r | |
583448b4 | 5 | (C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>\r |
94c2a044 | 6 | Copyright (C) 2014, Red Hat, Inc.\r |
c011b6c9 | 7 | (C) Copyright 2013 Hewlett-Packard Development Company, L.P.<BR>\r |
ba0014b9 | 8 | Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>\r |
a405b86d | 9 | This program and the accompanying materials\r |
10 | are licensed and made available under the terms and conditions of the BSD License\r | |
11 | which accompanies this distribution. The full text of the license may be found at\r | |
12 | http://opensource.org/licenses/bsd-license.php\r | |
13 | \r | |
14 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
15 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
16 | \r | |
17 | **/\r | |
18 | \r | |
0406a571 | 19 | #include "Shell.h"\r |
a405b86d | 20 | \r |
48cb33ec QS |
21 | BOOLEAN AsciiRedirection = FALSE;\r |
22 | \r | |
1219c85d RN |
23 | /**\r |
24 | Return the next parameter's end from a command line string.\r | |
25 | \r | |
26 | @param[in] String the string to parse\r | |
27 | **/\r | |
28 | CONST CHAR16*\r | |
1219c85d RN |
29 | FindEndOfParameter(\r |
30 | IN CONST CHAR16 *String\r | |
31 | )\r | |
32 | {\r | |
33 | CONST CHAR16 *First;\r | |
34 | CONST CHAR16 *CloseQuote;\r | |
35 | \r | |
36 | First = FindFirstCharacter(String, L" \"", L'^');\r | |
37 | \r | |
38 | //\r | |
39 | // nothing, all one parameter remaining\r | |
40 | //\r | |
41 | if (*First == CHAR_NULL) {\r | |
42 | return (First);\r | |
43 | }\r | |
44 | \r | |
45 | //\r | |
46 | // If space before a quote (or neither found, i.e. both CHAR_NULL),\r | |
47 | // then that's the end.\r | |
48 | //\r | |
49 | if (*First == L' ') {\r | |
50 | return (First);\r | |
51 | }\r | |
52 | \r | |
53 | CloseQuote = FindFirstCharacter (First+1, L"\"", L'^');\r | |
54 | \r | |
55 | //\r | |
56 | // We did not find a terminator...\r | |
57 | //\r | |
58 | if (*CloseQuote == CHAR_NULL) {\r | |
59 | return (NULL);\r | |
60 | }\r | |
61 | \r | |
62 | return (FindEndOfParameter (CloseQuote+1));\r | |
63 | }\r | |
64 | \r | |
65 | /**\r | |
66 | Return the next parameter from a command line string.\r | |
67 | \r | |
68 | This function moves the next parameter from Walker into TempParameter and moves\r | |
69 | Walker up past that parameter for recursive calling. When the final parameter\r | |
70 | is moved *Walker will be set to NULL;\r | |
71 | \r | |
72 | Temp Parameter must be large enough to hold the parameter before calling this\r | |
73 | function.\r | |
74 | \r | |
75 | This will also remove all remaining ^ characters after processing.\r | |
76 | \r | |
77 | @param[in, out] Walker pointer to string of command line. Adjusted to\r | |
78 | reminaing command line on return\r | |
79 | @param[in, out] TempParameter pointer to string of command line item extracted.\r | |
80 | @param[in] Length buffer size of TempParameter.\r | |
81 | @param[in] StripQuotation if TRUE then strip the quotation marks surrounding\r | |
82 | the parameters.\r | |
83 | \r | |
84 | @return EFI_INALID_PARAMETER A required parameter was NULL or pointed to a NULL or empty string.\r | |
85 | @return EFI_NOT_FOUND A closing " could not be found on the specified string\r | |
86 | **/\r | |
87 | EFI_STATUS\r | |
1219c85d RN |
88 | GetNextParameter(\r |
89 | IN OUT CHAR16 **Walker,\r | |
90 | IN OUT CHAR16 **TempParameter,\r | |
91 | IN CONST UINTN Length,\r | |
92 | IN BOOLEAN StripQuotation\r | |
93 | )\r | |
94 | {\r | |
95 | CONST CHAR16 *NextDelim;\r | |
96 | \r | |
97 | if (Walker == NULL\r | |
98 | ||*Walker == NULL\r | |
99 | ||TempParameter == NULL\r | |
100 | ||*TempParameter == NULL\r | |
101 | ){\r | |
102 | return (EFI_INVALID_PARAMETER);\r | |
103 | }\r | |
104 | \r | |
105 | \r | |
106 | //\r | |
107 | // make sure we dont have any leading spaces\r | |
108 | //\r | |
109 | while ((*Walker)[0] == L' ') {\r | |
110 | (*Walker)++;\r | |
111 | }\r | |
112 | \r | |
113 | //\r | |
114 | // make sure we still have some params now...\r | |
115 | //\r | |
116 | if (StrLen(*Walker) == 0) {\r | |
117 | DEBUG_CODE_BEGIN();\r | |
118 | *Walker = NULL;\r | |
119 | DEBUG_CODE_END();\r | |
120 | return (EFI_INVALID_PARAMETER);\r | |
121 | }\r | |
122 | \r | |
123 | NextDelim = FindEndOfParameter(*Walker);\r | |
124 | \r | |
125 | if (NextDelim == NULL){\r | |
126 | DEBUG_CODE_BEGIN();\r | |
127 | *Walker = NULL;\r | |
128 | DEBUG_CODE_END();\r | |
129 | return (EFI_NOT_FOUND);\r | |
130 | }\r | |
131 | \r | |
132 | StrnCpyS(*TempParameter, Length / sizeof(CHAR16), (*Walker), NextDelim - *Walker);\r | |
133 | \r | |
134 | //\r | |
135 | // Add a CHAR_NULL if we didnt get one via the copy\r | |
136 | //\r | |
137 | if (*NextDelim != CHAR_NULL) {\r | |
138 | (*TempParameter)[NextDelim - *Walker] = CHAR_NULL;\r | |
139 | }\r | |
140 | \r | |
141 | //\r | |
142 | // Update Walker for the next iteration through the function\r | |
143 | //\r | |
144 | *Walker = (CHAR16*)NextDelim;\r | |
145 | \r | |
146 | //\r | |
147 | // Remove any non-escaped quotes in the string\r | |
148 | // Remove any remaining escape characters in the string\r | |
149 | //\r | |
ba0014b9 LG |
150 | for (NextDelim = FindFirstCharacter(*TempParameter, L"\"^", CHAR_NULL)\r |
151 | ; *NextDelim != CHAR_NULL\r | |
1219c85d RN |
152 | ; NextDelim = FindFirstCharacter(NextDelim, L"\"^", CHAR_NULL)\r |
153 | ) {\r | |
154 | if (*NextDelim == L'^') {\r | |
155 | \r | |
156 | //\r | |
157 | // eliminate the escape ^\r | |
158 | //\r | |
159 | CopyMem ((CHAR16*)NextDelim, NextDelim + 1, StrSize (NextDelim + 1));\r | |
160 | NextDelim++;\r | |
161 | } else if (*NextDelim == L'\"') {\r | |
162 | \r | |
163 | //\r | |
164 | // eliminate the unescaped quote\r | |
165 | //\r | |
166 | if (StripQuotation) {\r | |
167 | CopyMem ((CHAR16*)NextDelim, NextDelim + 1, StrSize (NextDelim + 1));\r | |
ba0014b9 | 168 | } else{\r |
1219c85d | 169 | NextDelim++;\r |
ba0014b9 | 170 | }\r |
1219c85d RN |
171 | }\r |
172 | }\r | |
173 | \r | |
174 | return EFI_SUCCESS;\r | |
175 | }\r | |
176 | \r | |
a405b86d | 177 | /**\r |
733f138d | 178 | Function to populate Argc and Argv.\r |
a405b86d | 179 | \r |
180 | This function parses the CommandLine and divides it into standard C style Argc/Argv\r | |
181 | parameters for inclusion in EFI_SHELL_PARAMETERS_PROTOCOL. this supports space\r | |
182 | delimited and quote surrounded parameter definition.\r | |
183 | \r | |
ba0014b9 | 184 | All special character processing (alias, environment variable, redirection,\r |
14030c5c JC |
185 | etc... must be complete before calling this API.\r |
186 | \r | |
d1c275c6 QS |
187 | @param[in] CommandLine String of command line to parse\r |
188 | @param[in] StripQuotation if TRUE then strip the quotation marks surrounding\r | |
189 | the parameters.\r | |
190 | @param[in, out] Argv pointer to array of strings; one for each parameter\r | |
191 | @param[in, out] Argc pointer to number of strings in Argv array\r | |
a405b86d | 192 | \r |
193 | @return EFI_SUCCESS the operation was sucessful\r | |
194 | @return EFI_OUT_OF_RESOURCES a memory allocation failed.\r | |
195 | **/\r | |
196 | EFI_STATUS\r | |
a405b86d | 197 | ParseCommandLineToArgs(\r |
198 | IN CONST CHAR16 *CommandLine,\r | |
d1c275c6 QS |
199 | IN BOOLEAN StripQuotation,\r |
200 | IN OUT CHAR16 ***Argv,\r | |
201 | IN OUT UINTN *Argc\r | |
a405b86d | 202 | )\r |
203 | {\r | |
204 | UINTN Count;\r | |
205 | CHAR16 *TempParameter;\r | |
206 | CHAR16 *Walker;\r | |
207 | CHAR16 *NewParam;\r | |
1fab9176 | 208 | CHAR16 *NewCommandLine;\r |
a405b86d | 209 | UINTN Size;\r |
1fab9176 | 210 | EFI_STATUS Status;\r |
a405b86d | 211 | \r |
212 | ASSERT(Argc != NULL);\r | |
213 | ASSERT(Argv != NULL);\r | |
214 | \r | |
215 | if (CommandLine == NULL || StrLen(CommandLine)==0) {\r | |
216 | (*Argc) = 0;\r | |
217 | (*Argv) = NULL;\r | |
218 | return (EFI_SUCCESS);\r | |
219 | }\r | |
220 | \r | |
1fab9176 QS |
221 | NewCommandLine = AllocateCopyPool(StrSize(CommandLine), CommandLine);\r |
222 | if (NewCommandLine == NULL){\r | |
223 | return (EFI_OUT_OF_RESOURCES);\r | |
224 | }\r | |
225 | \r | |
226 | TrimSpaces(&NewCommandLine);\r | |
227 | Size = StrSize(NewCommandLine);\r | |
a405b86d | 228 | TempParameter = AllocateZeroPool(Size);\r |
229 | if (TempParameter == NULL) {\r | |
1fab9176 | 230 | SHELL_FREE_NON_NULL(NewCommandLine);\r |
a405b86d | 231 | return (EFI_OUT_OF_RESOURCES);\r |
232 | }\r | |
233 | \r | |
234 | for ( Count = 0\r | |
1fab9176 | 235 | , Walker = (CHAR16*)NewCommandLine\r |
a405b86d | 236 | ; Walker != NULL && *Walker != CHAR_NULL\r |
14030c5c JC |
237 | ; Count++\r |
238 | ) {\r | |
1219c85d | 239 | if (EFI_ERROR(GetNextParameter(&Walker, &TempParameter, Size, TRUE))) {\r |
14030c5c JC |
240 | break;\r |
241 | }\r | |
242 | }\r | |
a405b86d | 243 | \r |
a405b86d | 244 | //\r |
245 | // lets allocate the pointer array\r | |
246 | //\r | |
247 | (*Argv) = AllocateZeroPool((Count)*sizeof(CHAR16*));\r | |
248 | if (*Argv == NULL) {\r | |
1fab9176 QS |
249 | Status = EFI_OUT_OF_RESOURCES;\r |
250 | goto Done;\r | |
a405b86d | 251 | }\r |
252 | \r | |
253 | *Argc = 0;\r | |
1fab9176 | 254 | Walker = (CHAR16*)NewCommandLine;\r |
a405b86d | 255 | while(Walker != NULL && *Walker != CHAR_NULL) {\r |
256 | SetMem16(TempParameter, Size, CHAR_NULL);\r | |
1219c85d | 257 | if (EFI_ERROR(GetNextParameter(&Walker, &TempParameter, Size, StripQuotation))) {\r |
1fab9176 QS |
258 | Status = EFI_INVALID_PARAMETER;\r |
259 | goto Done;\r | |
14030c5c JC |
260 | }\r |
261 | \r | |
7f79b01e JC |
262 | NewParam = AllocateCopyPool(StrSize(TempParameter), TempParameter);\r |
263 | if (NewParam == NULL){\r | |
1fab9176 QS |
264 | Status = EFI_OUT_OF_RESOURCES;\r |
265 | goto Done;\r | |
7f79b01e | 266 | }\r |
a405b86d | 267 | ((CHAR16**)(*Argv))[(*Argc)] = NewParam;\r |
268 | (*Argc)++;\r | |
269 | }\r | |
270 | ASSERT(Count >= (*Argc));\r | |
1fab9176 | 271 | Status = EFI_SUCCESS;\r |
ba0014b9 | 272 | \r |
1fab9176 | 273 | Done:\r |
3f869579 | 274 | SHELL_FREE_NON_NULL(TempParameter);\r |
1fab9176 QS |
275 | SHELL_FREE_NON_NULL(NewCommandLine);\r |
276 | return (Status);\r | |
a405b86d | 277 | }\r |
278 | \r | |
279 | /**\r | |
280 | creates a new EFI_SHELL_PARAMETERS_PROTOCOL instance and populates it and then\r | |
281 | installs it on our handle and if there is an existing version of the protocol\r | |
282 | that one is cached for removal later.\r | |
283 | \r | |
4ff7e37b | 284 | @param[in, out] NewShellParameters on a successful return, a pointer to pointer\r |
a405b86d | 285 | to the newly installed interface.\r |
4ff7e37b | 286 | @param[in, out] RootShellInstance on a successful return, pointer to boolean.\r |
a405b86d | 287 | TRUE if this is the root shell instance.\r |
288 | \r | |
289 | @retval EFI_SUCCESS the operation completed successfully.\r | |
290 | @return other the operation failed.\r | |
291 | @sa ReinstallProtocolInterface\r | |
292 | @sa InstallProtocolInterface\r | |
293 | @sa ParseCommandLineToArgs\r | |
294 | **/\r | |
295 | EFI_STATUS\r | |
a405b86d | 296 | CreatePopulateInstallShellParametersProtocol (\r |
297 | IN OUT EFI_SHELL_PARAMETERS_PROTOCOL **NewShellParameters,\r | |
298 | IN OUT BOOLEAN *RootShellInstance\r | |
299 | )\r | |
300 | {\r | |
301 | EFI_STATUS Status;\r | |
302 | EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;\r | |
303 | CHAR16 *FullCommandLine;\r | |
304 | UINTN Size;\r | |
305 | \r | |
306 | Size = 0;\r | |
307 | FullCommandLine = NULL;\r | |
308 | LoadedImage = NULL;\r | |
309 | \r | |
310 | //\r | |
311 | // Assert for valid parameters\r | |
312 | //\r | |
313 | ASSERT(NewShellParameters != NULL);\r | |
314 | ASSERT(RootShellInstance != NULL);\r | |
315 | \r | |
316 | //\r | |
317 | // See if we have a shell parameters placed on us\r | |
318 | //\r | |
319 | Status = gBS->OpenProtocol (\r | |
320 | gImageHandle,\r | |
321 | &gEfiShellParametersProtocolGuid,\r | |
322 | (VOID **) &ShellInfoObject.OldShellParameters,\r | |
323 | gImageHandle,\r | |
324 | NULL,\r | |
325 | EFI_OPEN_PROTOCOL_GET_PROTOCOL\r | |
326 | );\r | |
327 | //\r | |
328 | // if we don't then we must be the root shell (error is expected)\r | |
329 | //\r | |
330 | if (EFI_ERROR (Status)) {\r | |
331 | *RootShellInstance = TRUE;\r | |
332 | }\r | |
333 | \r | |
334 | //\r | |
335 | // Allocate the new structure\r | |
336 | //\r | |
337 | *NewShellParameters = AllocateZeroPool(sizeof(EFI_SHELL_PARAMETERS_PROTOCOL));\r | |
3c865f20 | 338 | if ((*NewShellParameters) == NULL) {\r |
339 | return (EFI_OUT_OF_RESOURCES);\r | |
340 | }\r | |
a405b86d | 341 | \r |
342 | //\r | |
343 | // get loaded image protocol\r | |
344 | //\r | |
345 | Status = gBS->OpenProtocol (\r | |
346 | gImageHandle,\r | |
347 | &gEfiLoadedImageProtocolGuid,\r | |
348 | (VOID **) &LoadedImage,\r | |
349 | gImageHandle,\r | |
350 | NULL,\r | |
351 | EFI_OPEN_PROTOCOL_GET_PROTOCOL\r | |
352 | );\r | |
353 | ASSERT_EFI_ERROR(Status);\r | |
354 | //\r | |
355 | // Build the full command line\r | |
356 | //\r | |
e886b675 | 357 | Status = SHELL_GET_ENVIRONMENT_VARIABLE(L"ShellOpt", &Size, FullCommandLine);\r |
a405b86d | 358 | if (Status == EFI_BUFFER_TOO_SMALL) {\r |
359 | FullCommandLine = AllocateZeroPool(Size + LoadedImage->LoadOptionsSize);\r | |
e886b675 | 360 | Status = SHELL_GET_ENVIRONMENT_VARIABLE(L"ShellOpt", &Size, FullCommandLine);\r |
a405b86d | 361 | }\r |
362 | if (Status == EFI_NOT_FOUND) {\r | |
363 | //\r | |
364 | // no parameters via environment... ok\r | |
365 | //\r | |
366 | } else {\r | |
3c865f20 | 367 | if (EFI_ERROR(Status)) {\r |
368 | return (Status);\r | |
369 | }\r | |
a405b86d | 370 | }\r |
371 | if (Size == 0 && LoadedImage->LoadOptionsSize != 0) {\r | |
3c865f20 | 372 | ASSERT(FullCommandLine == NULL);\r |
a405b86d | 373 | //\r |
374 | // Now we need to include a NULL terminator in the size.\r | |
375 | //\r | |
376 | Size = LoadedImage->LoadOptionsSize + sizeof(FullCommandLine[0]);\r | |
377 | FullCommandLine = AllocateZeroPool(Size);\r | |
378 | }\r | |
a405b86d | 379 | if (FullCommandLine != NULL) {\r |
2c5a8aed | 380 | CopyMem (FullCommandLine, LoadedImage->LoadOptions, LoadedImage->LoadOptionsSize);\r |
a405b86d | 381 | //\r |
382 | // Populate Argc and Argv\r | |
383 | //\r | |
384 | Status = ParseCommandLineToArgs(FullCommandLine,\r | |
d1c275c6 | 385 | TRUE,\r |
a405b86d | 386 | &(*NewShellParameters)->Argv,\r |
387 | &(*NewShellParameters)->Argc);\r | |
388 | \r | |
389 | FreePool(FullCommandLine);\r | |
390 | \r | |
391 | ASSERT_EFI_ERROR(Status);\r | |
392 | } else {\r | |
393 | (*NewShellParameters)->Argv = NULL;\r | |
394 | (*NewShellParameters)->Argc = 0;\r | |
395 | }\r | |
396 | \r | |
397 | //\r | |
398 | // Populate the 3 faked file systems...\r | |
399 | //\r | |
400 | if (*RootShellInstance) {\r | |
401 | (*NewShellParameters)->StdIn = &FileInterfaceStdIn;\r | |
402 | (*NewShellParameters)->StdOut = &FileInterfaceStdOut;\r | |
403 | (*NewShellParameters)->StdErr = &FileInterfaceStdErr;\r | |
404 | Status = gBS->InstallProtocolInterface(&gImageHandle,\r | |
405 | &gEfiShellParametersProtocolGuid,\r | |
406 | EFI_NATIVE_INTERFACE,\r | |
407 | (VOID*)(*NewShellParameters));\r | |
408 | } else {\r | |
409 | //\r | |
410 | // copy from the existing ones\r | |
411 | //\r | |
412 | (*NewShellParameters)->StdIn = ShellInfoObject.OldShellParameters->StdIn;\r | |
413 | (*NewShellParameters)->StdOut = ShellInfoObject.OldShellParameters->StdOut;\r | |
414 | (*NewShellParameters)->StdErr = ShellInfoObject.OldShellParameters->StdErr;\r | |
415 | Status = gBS->ReinstallProtocolInterface(gImageHandle,\r | |
416 | &gEfiShellParametersProtocolGuid,\r | |
417 | (VOID*)ShellInfoObject.OldShellParameters,\r | |
418 | (VOID*)(*NewShellParameters));\r | |
419 | }\r | |
420 | \r | |
421 | return (Status);\r | |
422 | }\r | |
423 | \r | |
424 | /**\r | |
425 | frees all memory used by createion and installation of shell parameters protocol\r | |
426 | and if there was an old version installed it will restore that one.\r | |
427 | \r | |
428 | @param NewShellParameters the interface of EFI_SHELL_PARAMETERS_PROTOCOL that is\r | |
429 | being cleaned up.\r | |
430 | \r | |
431 | @retval EFI_SUCCESS the cleanup was successful\r | |
432 | @return other the cleanup failed\r | |
433 | @sa ReinstallProtocolInterface\r | |
434 | @sa UninstallProtocolInterface\r | |
435 | **/\r | |
436 | EFI_STATUS\r | |
a405b86d | 437 | CleanUpShellParametersProtocol (\r |
438 | IN OUT EFI_SHELL_PARAMETERS_PROTOCOL *NewShellParameters\r | |
439 | )\r | |
440 | {\r | |
441 | EFI_STATUS Status;\r | |
442 | UINTN LoopCounter;\r | |
443 | \r | |
444 | //\r | |
445 | // If the old exists we need to restore it\r | |
446 | //\r | |
447 | if (ShellInfoObject.OldShellParameters != NULL) {\r | |
448 | Status = gBS->ReinstallProtocolInterface(gImageHandle,\r | |
449 | &gEfiShellParametersProtocolGuid,\r | |
450 | (VOID*)NewShellParameters,\r | |
451 | (VOID*)ShellInfoObject.OldShellParameters);\r | |
452 | DEBUG_CODE(ShellInfoObject.OldShellParameters = NULL;);\r | |
453 | } else {\r | |
454 | //\r | |
455 | // No old one, just uninstall us...\r | |
456 | //\r | |
457 | Status = gBS->UninstallProtocolInterface(gImageHandle,\r | |
458 | &gEfiShellParametersProtocolGuid,\r | |
459 | (VOID*)NewShellParameters);\r | |
460 | }\r | |
461 | if (NewShellParameters->Argv != NULL) {\r | |
462 | for ( LoopCounter = 0\r | |
463 | ; LoopCounter < NewShellParameters->Argc\r | |
464 | ; LoopCounter++\r | |
465 | ){\r | |
466 | FreePool(NewShellParameters->Argv[LoopCounter]);\r | |
467 | }\r | |
468 | FreePool(NewShellParameters->Argv);\r | |
469 | }\r | |
470 | FreePool(NewShellParameters);\r | |
471 | return (Status);\r | |
472 | }\r | |
473 | \r | |
ae724571 | 474 | /**\r |
475 | Determin if a file name represents a unicode file.\r | |
476 | \r | |
477 | @param[in] FileName Pointer to the filename to open.\r | |
478 | \r | |
479 | @retval EFI_SUCCESS The file is a unicode file.\r | |
480 | @return An error upon failure.\r | |
481 | **/\r | |
733f138d | 482 | EFI_STATUS\r |
733f138d | 483 | IsUnicodeFile(\r |
484 | IN CONST CHAR16 *FileName\r | |
485 | )\r | |
486 | {\r | |
487 | SHELL_FILE_HANDLE Handle;\r | |
488 | EFI_STATUS Status;\r | |
489 | UINT64 OriginalFilePosition;\r | |
490 | UINTN CharSize;\r | |
491 | CHAR16 CharBuffer;\r | |
492 | \r | |
493 | Status = gEfiShellProtocol->OpenFileByName(FileName, &Handle, EFI_FILE_MODE_READ);\r | |
494 | if (EFI_ERROR(Status)) {\r | |
495 | return (Status);\r | |
496 | }\r | |
497 | gEfiShellProtocol->GetFilePosition(Handle, &OriginalFilePosition);\r | |
498 | gEfiShellProtocol->SetFilePosition(Handle, 0);\r | |
499 | CharSize = sizeof(CHAR16);\r | |
500 | Status = gEfiShellProtocol->ReadFile(Handle, &CharSize, &CharBuffer);\r | |
501 | if (EFI_ERROR(Status) || CharBuffer != gUnicodeFileTag) {\r | |
502 | Status = EFI_BUFFER_TOO_SMALL;\r | |
503 | }\r | |
504 | gEfiShellProtocol->SetFilePosition(Handle, OriginalFilePosition);\r | |
505 | gEfiShellProtocol->CloseFile(Handle);\r | |
ba0014b9 | 506 | return (Status);\r |
733f138d | 507 | }\r |
508 | \r | |
fb84495a | 509 | /**\r |
510 | Strips out quotes sections of a string.\r | |
511 | \r | |
512 | All of the characters between quotes is replaced with spaces.\r | |
a1d4bfcc | 513 | \r |
4ff7e37b | 514 | @param[in, out] TheString A pointer to the string to update.\r |
fb84495a | 515 | **/\r |
516 | VOID\r | |
fb84495a | 517 | StripQuotes (\r |
518 | IN OUT CHAR16 *TheString\r | |
519 | )\r | |
520 | {\r | |
521 | BOOLEAN RemoveNow;\r | |
522 | \r | |
523 | for (RemoveNow = FALSE ; TheString != NULL && *TheString != CHAR_NULL ; TheString++) {\r | |
524 | if (*TheString == L'^' && *(TheString + 1) == L'\"') {\r | |
525 | TheString++;\r | |
526 | } else if (*TheString == L'\"') {\r | |
527 | RemoveNow = (BOOLEAN)!RemoveNow;\r | |
528 | } else if (RemoveNow) {\r | |
529 | *TheString = L' ';\r | |
530 | }\r | |
531 | }\r | |
532 | }\r | |
533 | \r | |
cf6a8f14 | 534 | /**\r |
535 | Calcualte the 32-bit CRC in a EFI table using the service provided by the\r | |
536 | gRuntime service.\r | |
537 | \r | |
538 | @param Hdr Pointer to an EFI standard header\r | |
539 | \r | |
540 | **/\r | |
541 | VOID\r | |
542 | CalculateEfiHdrCrc (\r | |
543 | IN OUT EFI_TABLE_HEADER *Hdr\r | |
544 | )\r | |
545 | {\r | |
546 | UINT32 Crc;\r | |
547 | \r | |
548 | Hdr->CRC32 = 0;\r | |
549 | \r | |
550 | //\r | |
551 | // If gBS->CalculateCrce32 () == CoreEfiNotAvailableYet () then\r | |
552 | // Crc will come back as zero if we set it to zero here\r | |
553 | //\r | |
554 | Crc = 0;\r | |
555 | gBS->CalculateCrc32 ((UINT8 *)Hdr, Hdr->HeaderSize, &Crc);\r | |
556 | Hdr->CRC32 = Crc;\r | |
557 | }\r | |
558 | \r | |
031acf63 | 559 | /**\r |
560 | Fix a string to only have the file name, removing starting at the first space of whatever is quoted.\r | |
561 | \r | |
562 | @param[in] FileName The filename to start with.\r | |
563 | \r | |
564 | @retval NULL FileName was invalid.\r | |
565 | @return The modified FileName.\r | |
566 | **/\r | |
567 | CHAR16*\r | |
031acf63 | 568 | FixFileName (\r |
569 | IN CHAR16 *FileName\r | |
570 | )\r | |
571 | {\r | |
572 | CHAR16 *Copy;\r | |
573 | CHAR16 *TempLocation;\r | |
574 | \r | |
575 | if (FileName == NULL) {\r | |
576 | return (NULL);\r | |
577 | }\r | |
578 | \r | |
579 | if (FileName[0] == L'\"') {\r | |
580 | Copy = FileName+1;\r | |
581 | if ((TempLocation = StrStr(Copy , L"\"")) != NULL) {\r | |
582 | TempLocation[0] = CHAR_NULL;\r | |
ba0014b9 | 583 | }\r |
031acf63 | 584 | } else {\r |
585 | Copy = FileName;\r | |
f9aefb6a | 586 | while(Copy[0] == L' ') {\r |
587 | Copy++;\r | |
588 | }\r | |
031acf63 | 589 | if ((TempLocation = StrStr(Copy , L" ")) != NULL) {\r |
590 | TempLocation[0] = CHAR_NULL;\r | |
ba0014b9 | 591 | }\r |
031acf63 | 592 | }\r |
593 | \r | |
594 | if (Copy[0] == CHAR_NULL) {\r | |
595 | return (NULL);\r | |
596 | }\r | |
597 | \r | |
598 | return (Copy);\r | |
599 | }\r | |
600 | \r | |
6813ba40 JC |
601 | /**\r |
602 | Fix a string to only have the environment variable name, removing starting at the first space of whatever is quoted and removing the leading and trailing %.\r | |
603 | \r | |
604 | @param[in] FileName The filename to start with.\r | |
605 | \r | |
606 | @retval NULL FileName was invalid.\r | |
607 | @return The modified FileName.\r | |
608 | **/\r | |
609 | CHAR16*\r | |
6813ba40 JC |
610 | FixVarName (\r |
611 | IN CHAR16 *FileName\r | |
612 | )\r | |
613 | {\r | |
614 | CHAR16 *Copy;\r | |
615 | CHAR16 *TempLocation;\r | |
616 | \r | |
617 | Copy = FileName;\r | |
618 | \r | |
619 | if (FileName[0] == L'%') {\r | |
620 | Copy = FileName+1;\r | |
621 | if ((TempLocation = StrStr(Copy , L"%")) != NULL) {\r | |
622 | TempLocation[0] = CHAR_NULL;\r | |
ba0014b9 | 623 | }\r |
6813ba40 JC |
624 | }\r |
625 | \r | |
626 | return (FixFileName(Copy));\r | |
627 | }\r | |
628 | \r | |
49bb76ff | 629 | \r |
94c2a044 LE |
630 | /**\r |
631 | Write the unicode file tag to the specified file.\r | |
632 | \r | |
633 | It is the caller's responsibility to ensure that\r | |
634 | ShellInfoObject.NewEfiShellProtocol has been initialized before calling this\r | |
635 | function.\r | |
636 | \r | |
637 | @param[in] FileHandle The file to write the unicode file tag to.\r | |
638 | \r | |
639 | @return Status code from ShellInfoObject.NewEfiShellProtocol->WriteFile.\r | |
640 | **/\r | |
94c2a044 LE |
641 | EFI_STATUS\r |
642 | WriteFileTag (\r | |
643 | IN SHELL_FILE_HANDLE FileHandle\r | |
644 | )\r | |
645 | {\r | |
646 | CHAR16 FileTag;\r | |
647 | UINTN Size;\r | |
648 | EFI_STATUS Status;\r | |
649 | \r | |
650 | FileTag = gUnicodeFileTag;\r | |
651 | Size = sizeof FileTag;\r | |
652 | Status = ShellInfoObject.NewEfiShellProtocol->WriteFile (FileHandle, &Size,\r | |
653 | &FileTag);\r | |
654 | ASSERT (EFI_ERROR (Status) || Size == sizeof FileTag);\r | |
655 | return Status;\r | |
656 | }\r | |
657 | \r | |
658 | \r | |
a405b86d | 659 | /**\r |
660 | Funcion will replace the current StdIn and StdOut in the ShellParameters protocol\r | |
661 | structure by parsing NewCommandLine. The current values are returned to the\r | |
662 | user.\r | |
663 | \r | |
8be0ba36 | 664 | This will also update the system table.\r |
a405b86d | 665 | \r |
4ff7e37b ED |
666 | @param[in, out] ShellParameters Pointer to parameter structure to modify.\r |
667 | @param[in] NewCommandLine The new command line to parse and use.\r | |
668 | @param[out] OldStdIn Pointer to old StdIn.\r | |
669 | @param[out] OldStdOut Pointer to old StdOut.\r | |
670 | @param[out] OldStdErr Pointer to old StdErr.\r | |
671 | @param[out] SystemTableInfo Pointer to old system table information.\r | |
a405b86d | 672 | \r |
673 | @retval EFI_SUCCESS Operation was sucessful, Argv and Argc are valid.\r | |
674 | @retval EFI_OUT_OF_RESOURCES A memory allocation failed.\r | |
675 | **/\r | |
676 | EFI_STATUS\r | |
a405b86d | 677 | UpdateStdInStdOutStdErr(\r |
678 | IN OUT EFI_SHELL_PARAMETERS_PROTOCOL *ShellParameters,\r | |
fb84495a | 679 | IN CHAR16 *NewCommandLine,\r |
a405b86d | 680 | OUT SHELL_FILE_HANDLE *OldStdIn,\r |
681 | OUT SHELL_FILE_HANDLE *OldStdOut,\r | |
8be0ba36 | 682 | OUT SHELL_FILE_HANDLE *OldStdErr,\r |
683 | OUT SYSTEM_TABLE_INFO *SystemTableInfo\r | |
a405b86d | 684 | )\r |
685 | {\r | |
686 | CHAR16 *CommandLineCopy;\r | |
687 | CHAR16 *CommandLineWalker;\r | |
688 | CHAR16 *StdErrFileName;\r | |
689 | CHAR16 *StdOutFileName;\r | |
690 | CHAR16 *StdInFileName;\r | |
691 | CHAR16 *StdInVarName;\r | |
692 | CHAR16 *StdOutVarName;\r | |
693 | CHAR16 *StdErrVarName;\r | |
694 | EFI_STATUS Status;\r | |
695 | SHELL_FILE_HANDLE TempHandle;\r | |
696 | UINT64 FileSize;\r | |
697 | BOOLEAN OutUnicode;\r | |
698 | BOOLEAN InUnicode;\r | |
699 | BOOLEAN ErrUnicode;\r | |
700 | BOOLEAN OutAppend;\r | |
701 | BOOLEAN ErrAppend;\r | |
702 | UINTN Size;\r | |
a405b86d | 703 | SPLIT_LIST *Split;\r |
fb84495a | 704 | CHAR16 *FirstLocation;\r |
31e5b912 | 705 | BOOLEAN Volatile;\r |
a405b86d | 706 | \r |
a405b86d | 707 | OutUnicode = TRUE;\r |
708 | InUnicode = TRUE;\r | |
48cb33ec | 709 | AsciiRedirection = FALSE;\r |
a405b86d | 710 | ErrUnicode = TRUE;\r |
711 | StdInVarName = NULL;\r | |
712 | StdOutVarName = NULL;\r | |
713 | StdErrVarName = NULL;\r | |
714 | StdErrFileName = NULL;\r | |
715 | StdInFileName = NULL;\r | |
716 | StdOutFileName = NULL;\r | |
717 | ErrAppend = FALSE;\r | |
718 | OutAppend = FALSE;\r | |
719 | CommandLineCopy = NULL;\r | |
5f2915f5 | 720 | FirstLocation = NULL;\r |
a405b86d | 721 | \r |
8be0ba36 | 722 | if (ShellParameters == NULL || SystemTableInfo == NULL || OldStdIn == NULL || OldStdOut == NULL || OldStdErr == NULL) {\r |
723 | return (EFI_INVALID_PARAMETER);\r | |
a405b86d | 724 | }\r |
725 | \r | |
8be0ba36 | 726 | SystemTableInfo->ConIn = gST->ConIn;\r |
727 | SystemTableInfo->ConInHandle = gST->ConsoleInHandle;\r | |
728 | SystemTableInfo->ConOut = gST->ConOut;\r | |
729 | SystemTableInfo->ConOutHandle = gST->ConsoleOutHandle;\r | |
4ccd9214 ED |
730 | SystemTableInfo->ErrOut = gST->StdErr;\r |
731 | SystemTableInfo->ErrOutHandle = gST->StandardErrorHandle;\r | |
8be0ba36 | 732 | *OldStdIn = ShellParameters->StdIn;\r |
733 | *OldStdOut = ShellParameters->StdOut;\r | |
734 | *OldStdErr = ShellParameters->StdErr;\r | |
735 | \r | |
a405b86d | 736 | if (NewCommandLine == NULL) {\r |
737 | return (EFI_SUCCESS);\r | |
738 | }\r | |
739 | \r | |
740 | CommandLineCopy = StrnCatGrow(&CommandLineCopy, NULL, NewCommandLine, 0);\r | |
532691c8 | 741 | if (CommandLineCopy == NULL) {\r |
742 | return (EFI_OUT_OF_RESOURCES);\r | |
743 | }\r | |
a405b86d | 744 | Status = EFI_SUCCESS;\r |
745 | Split = NULL;\r | |
5f2915f5 | 746 | FirstLocation = CommandLineCopy + StrLen(CommandLineCopy);\r |
a405b86d | 747 | \r |
fb84495a | 748 | StripQuotes(CommandLineCopy);\r |
749 | \r | |
a405b86d | 750 | if (!IsListEmpty(&ShellInfoObject.SplitList.Link)) {\r |
751 | Split = (SPLIT_LIST*)GetFirstNode(&ShellInfoObject.SplitList.Link);\r | |
752 | if (Split != NULL && Split->SplitStdIn != NULL) {\r | |
753 | ShellParameters->StdIn = Split->SplitStdIn;\r | |
754 | }\r | |
755 | if (Split != NULL && Split->SplitStdOut != NULL) {\r | |
756 | ShellParameters->StdOut = Split->SplitStdOut;\r | |
757 | }\r | |
758 | }\r | |
759 | \r | |
760 | if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" 2>>v ")) != NULL) {\r | |
fb84495a | 761 | FirstLocation = MIN(CommandLineWalker, FirstLocation);\r |
733f138d | 762 | SetMem16(CommandLineWalker, 12, L' ');\r |
a405b86d | 763 | StdErrVarName = CommandLineWalker += 6;\r |
764 | ErrAppend = TRUE;\r | |
733f138d | 765 | if (StrStr(CommandLineWalker, L" 2>>v ") != NULL) {\r |
766 | Status = EFI_NOT_FOUND;\r | |
767 | }\r | |
a405b86d | 768 | }\r |
769 | if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" 1>>v ")) != NULL) {\r | |
fb84495a | 770 | FirstLocation = MIN(CommandLineWalker, FirstLocation);\r |
733f138d | 771 | SetMem16(CommandLineWalker, 12, L' ');\r |
a405b86d | 772 | StdOutVarName = CommandLineWalker += 6;\r |
773 | OutAppend = TRUE;\r | |
733f138d | 774 | if (StrStr(CommandLineWalker, L" 1>>v ") != NULL) {\r |
775 | Status = EFI_NOT_FOUND;\r | |
776 | }\r | |
a405b86d | 777 | } else if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" >>v ")) != NULL) {\r |
fb84495a | 778 | FirstLocation = MIN(CommandLineWalker, FirstLocation);\r |
733f138d | 779 | SetMem16(CommandLineWalker, 10, L' ');\r |
a405b86d | 780 | StdOutVarName = CommandLineWalker += 5;\r |
781 | OutAppend = TRUE;\r | |
733f138d | 782 | if (StrStr(CommandLineWalker, L" >>v ") != NULL) {\r |
783 | Status = EFI_NOT_FOUND;\r | |
784 | }\r | |
a405b86d | 785 | } else if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" >v ")) != NULL) {\r |
fb84495a | 786 | FirstLocation = MIN(CommandLineWalker, FirstLocation);\r |
733f138d | 787 | SetMem16(CommandLineWalker, 8, L' ');\r |
a405b86d | 788 | StdOutVarName = CommandLineWalker += 4;\r |
789 | OutAppend = FALSE;\r | |
733f138d | 790 | if (StrStr(CommandLineWalker, L" >v ") != NULL) {\r |
791 | Status = EFI_NOT_FOUND;\r | |
792 | }\r | |
a405b86d | 793 | }\r |
794 | if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" 1>>a ")) != NULL) {\r | |
fb84495a | 795 | FirstLocation = MIN(CommandLineWalker, FirstLocation);\r |
733f138d | 796 | SetMem16(CommandLineWalker, 12, L' ');\r |
a405b86d | 797 | StdOutFileName = CommandLineWalker += 6;\r |
798 | OutAppend = TRUE;\r | |
799 | OutUnicode = FALSE;\r | |
733f138d | 800 | if (StrStr(CommandLineWalker, L" 1>>a ") != NULL) {\r |
801 | Status = EFI_NOT_FOUND;\r | |
802 | }\r | |
a405b86d | 803 | }\r |
804 | if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" 1>> ")) != NULL) {\r | |
fb84495a | 805 | FirstLocation = MIN(CommandLineWalker, FirstLocation);\r |
733f138d | 806 | SetMem16(CommandLineWalker, 10, L' ');\r |
a405b86d | 807 | if (StdOutFileName != NULL) {\r |
808 | Status = EFI_INVALID_PARAMETER;\r | |
809 | } else {\r | |
810 | StdOutFileName = CommandLineWalker += 5;\r | |
811 | OutAppend = TRUE;\r | |
812 | }\r | |
733f138d | 813 | if (StrStr(CommandLineWalker, L" 1>> ") != NULL) {\r |
814 | Status = EFI_NOT_FOUND;\r | |
815 | }\r | |
ba0014b9 | 816 | }\r |
a405b86d | 817 | if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" >> ")) != NULL) {\r |
fb84495a | 818 | FirstLocation = MIN(CommandLineWalker, FirstLocation);\r |
733f138d | 819 | SetMem16(CommandLineWalker, 8, L' ');\r |
a405b86d | 820 | if (StdOutFileName != NULL) {\r |
821 | Status = EFI_INVALID_PARAMETER;\r | |
822 | } else {\r | |
823 | StdOutFileName = CommandLineWalker += 4;\r | |
824 | OutAppend = TRUE;\r | |
825 | }\r | |
733f138d | 826 | if (StrStr(CommandLineWalker, L" >> ") != NULL) {\r |
827 | Status = EFI_NOT_FOUND;\r | |
828 | }\r | |
a405b86d | 829 | }\r |
830 | if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" >>a ")) != NULL) {\r | |
fb84495a | 831 | FirstLocation = MIN(CommandLineWalker, FirstLocation);\r |
733f138d | 832 | SetMem16(CommandLineWalker, 10, L' ');\r |
a405b86d | 833 | if (StdOutFileName != NULL) {\r |
834 | Status = EFI_INVALID_PARAMETER;\r | |
835 | } else {\r | |
836 | StdOutFileName = CommandLineWalker += 5;\r | |
837 | OutAppend = TRUE;\r | |
838 | OutUnicode = FALSE;\r | |
839 | }\r | |
733f138d | 840 | if (StrStr(CommandLineWalker, L" >>a ") != NULL) {\r |
841 | Status = EFI_NOT_FOUND;\r | |
842 | }\r | |
ba0014b9 | 843 | }\r |
a405b86d | 844 | if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" 1>a ")) != NULL) {\r |
fb84495a | 845 | FirstLocation = MIN(CommandLineWalker, FirstLocation);\r |
733f138d | 846 | SetMem16(CommandLineWalker, 10, L' ');\r |
a405b86d | 847 | if (StdOutFileName != NULL) {\r |
848 | Status = EFI_INVALID_PARAMETER;\r | |
849 | } else {\r | |
850 | StdOutFileName = CommandLineWalker += 5;\r | |
851 | OutAppend = FALSE;\r | |
852 | OutUnicode = FALSE;\r | |
853 | }\r | |
733f138d | 854 | if (StrStr(CommandLineWalker, L" 1>a ") != NULL) {\r |
855 | Status = EFI_NOT_FOUND;\r | |
856 | }\r | |
ba0014b9 | 857 | }\r |
a405b86d | 858 | if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" >a ")) != NULL) {\r |
fb84495a | 859 | FirstLocation = MIN(CommandLineWalker, FirstLocation);\r |
733f138d | 860 | SetMem16(CommandLineWalker, 8, L' ');\r |
a405b86d | 861 | if (StdOutFileName != NULL) {\r |
862 | Status = EFI_INVALID_PARAMETER;\r | |
863 | } else {\r | |
864 | StdOutFileName = CommandLineWalker += 4;\r | |
865 | OutAppend = FALSE;\r | |
866 | OutUnicode = FALSE;\r | |
867 | }\r | |
733f138d | 868 | if (StrStr(CommandLineWalker, L" >a ") != NULL) {\r |
869 | Status = EFI_NOT_FOUND;\r | |
870 | }\r | |
a405b86d | 871 | }\r |
872 | if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" 2>> ")) != NULL) {\r | |
fb84495a | 873 | FirstLocation = MIN(CommandLineWalker, FirstLocation);\r |
733f138d | 874 | SetMem16(CommandLineWalker, 10, L' ');\r |
a405b86d | 875 | if (StdErrFileName != NULL) {\r |
876 | Status = EFI_INVALID_PARAMETER;\r | |
877 | } else {\r | |
878 | StdErrFileName = CommandLineWalker += 5;\r | |
879 | ErrAppend = TRUE;\r | |
880 | }\r | |
733f138d | 881 | if (StrStr(CommandLineWalker, L" 2>> ") != NULL) {\r |
882 | Status = EFI_NOT_FOUND;\r | |
883 | }\r | |
a405b86d | 884 | }\r |
885 | \r | |
886 | if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" 2>v ")) != NULL) {\r | |
fb84495a | 887 | FirstLocation = MIN(CommandLineWalker, FirstLocation);\r |
733f138d | 888 | SetMem16(CommandLineWalker, 10, L' ');\r |
a405b86d | 889 | if (StdErrVarName != NULL) {\r |
890 | Status = EFI_INVALID_PARAMETER;\r | |
891 | } else {\r | |
892 | StdErrVarName = CommandLineWalker += 5;\r | |
893 | ErrAppend = FALSE;\r | |
894 | }\r | |
733f138d | 895 | if (StrStr(CommandLineWalker, L" 2>v ") != NULL) {\r |
896 | Status = EFI_NOT_FOUND;\r | |
897 | }\r | |
a405b86d | 898 | }\r |
899 | if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" 1>v ")) != NULL) {\r | |
fb84495a | 900 | FirstLocation = MIN(CommandLineWalker, FirstLocation);\r |
733f138d | 901 | SetMem16(CommandLineWalker, 10, L' ');\r |
a405b86d | 902 | if (StdOutVarName != NULL) {\r |
903 | Status = EFI_INVALID_PARAMETER;\r | |
904 | } else {\r | |
905 | StdOutVarName = CommandLineWalker += 5;\r | |
906 | OutAppend = FALSE;\r | |
907 | }\r | |
733f138d | 908 | if (StrStr(CommandLineWalker, L" 1>v ") != NULL) {\r |
909 | Status = EFI_NOT_FOUND;\r | |
910 | }\r | |
a405b86d | 911 | }\r |
912 | if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" 2>a ")) != NULL) {\r | |
fb84495a | 913 | FirstLocation = MIN(CommandLineWalker, FirstLocation);\r |
733f138d | 914 | SetMem16(CommandLineWalker, 10, L' ');\r |
a405b86d | 915 | if (StdErrFileName != NULL) {\r |
916 | Status = EFI_INVALID_PARAMETER;\r | |
917 | } else {\r | |
918 | StdErrFileName = CommandLineWalker += 5;\r | |
919 | ErrAppend = FALSE;\r | |
920 | ErrUnicode = FALSE;\r | |
921 | }\r | |
733f138d | 922 | if (StrStr(CommandLineWalker, L" 2>a ") != NULL) {\r |
923 | Status = EFI_NOT_FOUND;\r | |
924 | }\r | |
a405b86d | 925 | }\r |
926 | if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" 2> ")) != NULL) {\r | |
fb84495a | 927 | FirstLocation = MIN(CommandLineWalker, FirstLocation);\r |
733f138d | 928 | SetMem16(CommandLineWalker, 8, L' ');\r |
a405b86d | 929 | if (StdErrFileName != NULL) {\r |
930 | Status = EFI_INVALID_PARAMETER;\r | |
931 | } else {\r | |
932 | StdErrFileName = CommandLineWalker += 4;\r | |
933 | ErrAppend = FALSE;\r | |
934 | }\r | |
733f138d | 935 | if (StrStr(CommandLineWalker, L" 2> ") != NULL) {\r |
936 | Status = EFI_NOT_FOUND;\r | |
937 | }\r | |
a405b86d | 938 | }\r |
939 | \r | |
940 | if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" 1> ")) != NULL) {\r | |
fb84495a | 941 | FirstLocation = MIN(CommandLineWalker, FirstLocation);\r |
733f138d | 942 | SetMem16(CommandLineWalker, 8, L' ');\r |
a405b86d | 943 | if (StdOutFileName != NULL) {\r |
944 | Status = EFI_INVALID_PARAMETER;\r | |
945 | } else {\r | |
946 | StdOutFileName = CommandLineWalker += 4;\r | |
947 | OutAppend = FALSE;\r | |
948 | }\r | |
733f138d | 949 | if (StrStr(CommandLineWalker, L" 1> ") != NULL) {\r |
950 | Status = EFI_NOT_FOUND;\r | |
951 | }\r | |
a405b86d | 952 | }\r |
953 | \r | |
954 | if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" > ")) != NULL) {\r | |
fb84495a | 955 | FirstLocation = MIN(CommandLineWalker, FirstLocation);\r |
733f138d | 956 | SetMem16(CommandLineWalker, 6, L' ');\r |
a405b86d | 957 | if (StdOutFileName != NULL) {\r |
958 | Status = EFI_INVALID_PARAMETER;\r | |
959 | } else {\r | |
960 | StdOutFileName = CommandLineWalker += 3;\r | |
961 | OutAppend = FALSE;\r | |
962 | }\r | |
733f138d | 963 | if (StrStr(CommandLineWalker, L" > ") != NULL) {\r |
964 | Status = EFI_NOT_FOUND;\r | |
965 | }\r | |
a405b86d | 966 | }\r |
967 | \r | |
968 | if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" < ")) != NULL) {\r | |
fb84495a | 969 | FirstLocation = MIN(CommandLineWalker, FirstLocation);\r |
733f138d | 970 | SetMem16(CommandLineWalker, 6, L' ');\r |
a405b86d | 971 | if (StdInFileName != NULL) {\r |
972 | Status = EFI_INVALID_PARAMETER;\r | |
973 | } else {\r | |
974 | StdInFileName = CommandLineWalker += 3;\r | |
733f138d | 975 | }\r |
976 | if (StrStr(CommandLineWalker, L" < ") != NULL) {\r | |
977 | Status = EFI_NOT_FOUND;\r | |
a405b86d | 978 | }\r |
979 | }\r | |
980 | if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" <a ")) != NULL) {\r | |
fb84495a | 981 | FirstLocation = MIN(CommandLineWalker, FirstLocation);\r |
733f138d | 982 | SetMem16(CommandLineWalker, 8, L' ');\r |
a405b86d | 983 | if (StdInFileName != NULL) {\r |
984 | Status = EFI_INVALID_PARAMETER;\r | |
985 | } else {\r | |
733f138d | 986 | StdInFileName = CommandLineWalker += 4;\r |
987 | InUnicode = FALSE;\r | |
48cb33ec | 988 | AsciiRedirection = TRUE;\r |
733f138d | 989 | }\r |
990 | if (StrStr(CommandLineWalker, L" <a ") != NULL) {\r | |
991 | Status = EFI_NOT_FOUND;\r | |
a405b86d | 992 | }\r |
993 | }\r | |
994 | if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" <v ")) != NULL) {\r | |
fb84495a | 995 | FirstLocation = MIN(CommandLineWalker, FirstLocation);\r |
733f138d | 996 | SetMem16(CommandLineWalker, 8, L' ');\r |
a405b86d | 997 | if (StdInVarName != NULL) {\r |
998 | Status = EFI_INVALID_PARAMETER;\r | |
999 | } else {\r | |
1000 | StdInVarName = CommandLineWalker += 4;\r | |
733f138d | 1001 | }\r |
1002 | if (StrStr(CommandLineWalker, L" <v ") != NULL) {\r | |
1003 | Status = EFI_NOT_FOUND;\r | |
a405b86d | 1004 | }\r |
1005 | }\r | |
1006 | \r | |
031acf63 | 1007 | //\r |
1008 | // re-populate the string to support any filenames that were in quotes.\r | |
1009 | //\r | |
e75390f0 | 1010 | StrnCpyS(CommandLineCopy, StrSize(CommandLineCopy)/sizeof(CHAR16), NewCommandLine, StrLen(NewCommandLine));\r |
031acf63 | 1011 | \r |
5f2915f5 | 1012 | if (FirstLocation != CommandLineCopy + StrLen(CommandLineCopy)\r |
810c635d | 1013 | && (((UINTN)FirstLocation - (UINTN)CommandLineCopy)/sizeof(CHAR16) < StrLen(NewCommandLine))\r |
fb84495a | 1014 | ){\r |
810c635d | 1015 | *(NewCommandLine + ((UINTN)FirstLocation - (UINTN)CommandLineCopy)/sizeof(CHAR16)) = CHAR_NULL;\r |
fb84495a | 1016 | }\r |
1017 | \r | |
a405b86d | 1018 | if (!EFI_ERROR(Status)) {\r |
031acf63 | 1019 | \r |
1020 | if (StdErrFileName != NULL) {\r | |
1021 | if ((StdErrFileName = FixFileName(StdErrFileName)) == NULL) {\r | |
1022 | Status = EFI_INVALID_PARAMETER;\r | |
1023 | }\r | |
a405b86d | 1024 | }\r |
031acf63 | 1025 | if (StdOutFileName != NULL) {\r |
1026 | if ((StdOutFileName = FixFileName(StdOutFileName)) == NULL) {\r | |
1027 | Status = EFI_INVALID_PARAMETER;\r | |
1028 | }\r | |
a405b86d | 1029 | }\r |
031acf63 | 1030 | if (StdInFileName != NULL) {\r |
1031 | if ((StdInFileName = FixFileName(StdInFileName)) == NULL) {\r | |
1032 | Status = EFI_INVALID_PARAMETER;\r | |
1033 | }\r | |
a405b86d | 1034 | }\r |
031acf63 | 1035 | if (StdErrVarName != NULL) {\r |
6813ba40 | 1036 | if ((StdErrVarName = FixVarName(StdErrVarName)) == NULL) {\r |
031acf63 | 1037 | Status = EFI_INVALID_PARAMETER;\r |
1038 | }\r | |
a405b86d | 1039 | }\r |
031acf63 | 1040 | if (StdOutVarName != NULL) {\r |
6813ba40 | 1041 | if ((StdOutVarName = FixVarName(StdOutVarName)) == NULL) {\r |
031acf63 | 1042 | Status = EFI_INVALID_PARAMETER;\r |
1043 | }\r | |
a405b86d | 1044 | }\r |
031acf63 | 1045 | if (StdInVarName != NULL) {\r |
6813ba40 | 1046 | if ((StdInVarName = FixVarName(StdInVarName)) == NULL) {\r |
031acf63 | 1047 | Status = EFI_INVALID_PARAMETER;\r |
1048 | }\r | |
a405b86d | 1049 | }\r |
1050 | \r | |
0c956e0d | 1051 | //\r |
1052 | // Verify not the same and not duplicating something from a split\r | |
1053 | //\r | |
733f138d | 1054 | if (\r |
1055 | //\r | |
1056 | // Check that no 2 filenames are the same\r | |
1057 | //\r | |
fb84495a | 1058 | (StdErrFileName != NULL && StdOutFileName!= NULL && StringNoCaseCompare(&StdErrFileName, &StdOutFileName) == 0)\r |
a405b86d | 1059 | ||(StdErrFileName != NULL && StdInFileName != NULL && StringNoCaseCompare(&StdErrFileName, &StdInFileName ) == 0)\r |
1060 | ||(StdOutFileName != NULL && StdInFileName != NULL && StringNoCaseCompare(&StdOutFileName, &StdInFileName ) == 0)\r | |
733f138d | 1061 | //\r |
1062 | // Check that no 2 variable names are the same\r | |
1063 | //\r | |
a405b86d | 1064 | ||(StdErrVarName != NULL && StdInVarName != NULL && StringNoCaseCompare(&StdErrVarName , &StdInVarName ) == 0)\r |
1065 | ||(StdOutVarName != NULL && StdInVarName != NULL && StringNoCaseCompare(&StdOutVarName , &StdInVarName ) == 0)\r | |
1066 | ||(StdErrVarName != NULL && StdOutVarName != NULL && StringNoCaseCompare(&StdErrVarName , &StdOutVarName ) == 0)\r | |
733f138d | 1067 | //\r |
1068 | // When a split (using | operator) is in place some are not allowed\r | |
1069 | //\r | |
a405b86d | 1070 | ||(Split != NULL && Split->SplitStdIn != NULL && (StdInVarName != NULL || StdInFileName != NULL))\r |
1071 | ||(Split != NULL && Split->SplitStdOut != NULL && (StdOutVarName != NULL || StdOutFileName != NULL))\r | |
733f138d | 1072 | //\r |
1073 | // Check that nothing is trying to be output to 2 locations.\r | |
1074 | //\r | |
a405b86d | 1075 | ||(StdErrFileName != NULL && StdErrVarName != NULL)\r |
1076 | ||(StdOutFileName != NULL && StdOutVarName != NULL)\r | |
1077 | ||(StdInFileName != NULL && StdInVarName != NULL)\r | |
733f138d | 1078 | //\r |
733f138d | 1079 | // Check for no volatile environment variables\r |
1080 | //\r | |
31e5b912 RN |
1081 | ||(StdErrVarName != NULL && !EFI_ERROR (IsVolatileEnv (StdErrVarName, &Volatile)) && !Volatile)\r |
1082 | ||(StdOutVarName != NULL && !EFI_ERROR (IsVolatileEnv (StdOutVarName, &Volatile)) && !Volatile)\r | |
733f138d | 1083 | //\r |
1084 | // Cant redirect during a reconnect operation.\r | |
1085 | //\r | |
ba0014b9 | 1086 | ||(StrStr(NewCommandLine, L"connect -r") != NULL\r |
3c865f20 | 1087 | && (StdOutVarName != NULL || StdOutFileName != NULL || StdErrFileName != NULL || StdErrVarName != NULL))\r |
733f138d | 1088 | //\r |
1089 | // Check that filetypes (Unicode/Ascii) do not change during an append\r | |
1090 | //\r | |
1091 | ||(StdOutFileName != NULL && OutUnicode && OutAppend && (!EFI_ERROR(ShellFileExists(StdOutFileName)) && EFI_ERROR(IsUnicodeFile(StdOutFileName))))\r | |
1092 | ||(StdErrFileName != NULL && ErrUnicode && ErrAppend && (!EFI_ERROR(ShellFileExists(StdErrFileName)) && EFI_ERROR(IsUnicodeFile(StdErrFileName))))\r | |
1093 | ||(StdOutFileName != NULL && !OutUnicode && OutAppend && (!EFI_ERROR(ShellFileExists(StdOutFileName)) && !EFI_ERROR(IsUnicodeFile(StdOutFileName))))\r | |
1094 | ||(StdErrFileName != NULL && !ErrUnicode && ErrAppend && (!EFI_ERROR(ShellFileExists(StdErrFileName)) && !EFI_ERROR(IsUnicodeFile(StdErrFileName))))\r | |
a405b86d | 1095 | ){\r |
1096 | Status = EFI_INVALID_PARAMETER;\r | |
a9ca0684 | 1097 | ShellParameters->StdIn = *OldStdIn;\r |
1098 | ShellParameters->StdOut = *OldStdOut;\r | |
1099 | ShellParameters->StdErr = *OldStdErr;\r | |
fb84495a | 1100 | } else if (!EFI_ERROR(Status)){\r |
a405b86d | 1101 | //\r |
1102 | // Open the Std<Whatever> and we should not have conflicts here...\r | |
1103 | //\r | |
1104 | \r | |
1105 | //\r | |
1106 | // StdErr to a file\r | |
1107 | //\r | |
1108 | if (StdErrFileName != NULL) {\r | |
1109 | if (!ErrAppend) {\r | |
1110 | //\r | |
1111 | // delete existing file.\r | |
1112 | //\r | |
1113 | ShellInfoObject.NewEfiShellProtocol->DeleteFileByName(StdErrFileName);\r | |
1114 | }\r | |
1115 | Status = ShellOpenFileByName(StdErrFileName, &TempHandle, EFI_FILE_MODE_WRITE|EFI_FILE_MODE_READ|EFI_FILE_MODE_CREATE,0);\r | |
a405b86d | 1116 | if (!ErrAppend && ErrUnicode && !EFI_ERROR(Status)) {\r |
94c2a044 | 1117 | Status = WriteFileTag (TempHandle);\r |
a405b86d | 1118 | }\r |
1119 | if (!ErrUnicode && !EFI_ERROR(Status)) {\r | |
1120 | TempHandle = CreateFileInterfaceFile(TempHandle, FALSE);\r | |
1121 | ASSERT(TempHandle != NULL);\r | |
1122 | }\r | |
1123 | if (!EFI_ERROR(Status)) {\r | |
1124 | ShellParameters->StdErr = TempHandle;\r | |
dcf9b428 | 1125 | gST->StdErr = CreateSimpleTextOutOnFile(TempHandle, &gST->StandardErrorHandle, gST->StdErr);\r |
a405b86d | 1126 | }\r |
1127 | }\r | |
1128 | \r | |
1129 | //\r | |
1130 | // StdOut to a file\r | |
1131 | //\r | |
1132 | if (!EFI_ERROR(Status) && StdOutFileName != NULL) {\r | |
1133 | if (!OutAppend) {\r | |
1134 | //\r | |
1135 | // delete existing file.\r | |
1136 | //\r | |
1137 | ShellInfoObject.NewEfiShellProtocol->DeleteFileByName(StdOutFileName);\r | |
1138 | }\r | |
1139 | Status = ShellOpenFileByName(StdOutFileName, &TempHandle, EFI_FILE_MODE_WRITE|EFI_FILE_MODE_READ|EFI_FILE_MODE_CREATE,0);\r | |
3c865f20 | 1140 | if (TempHandle == NULL) {\r |
1141 | Status = EFI_INVALID_PARAMETER;\r | |
1142 | } else {\r | |
583448b4 | 1143 | if (gUnicodeCollation->MetaiMatch (gUnicodeCollation, StdOutFileName, L"NUL")) {\r |
733f138d | 1144 | //no-op\r |
1145 | } else if (!OutAppend && OutUnicode && !EFI_ERROR(Status)) {\r | |
94c2a044 | 1146 | Status = WriteFileTag (TempHandle);\r |
3c865f20 | 1147 | } else if (OutAppend) {\r |
3c865f20 | 1148 | Status = ShellInfoObject.NewEfiShellProtocol->GetFileSize(TempHandle, &FileSize);\r |
1149 | if (!EFI_ERROR(Status)) {\r | |
5967886d LE |
1150 | //\r |
1151 | // When appending to a new unicode file, write the file tag.\r | |
1152 | // Otherwise (ie. when appending to a new ASCII file, or an\r | |
1153 | // existent file with any encoding), just seek to the end.\r | |
1154 | //\r | |
1155 | Status = (FileSize == 0 && OutUnicode) ?\r | |
1156 | WriteFileTag (TempHandle) :\r | |
1157 | ShellInfoObject.NewEfiShellProtocol->SetFilePosition (\r | |
1158 | TempHandle,\r | |
1159 | FileSize);\r | |
3c865f20 | 1160 | }\r |
1161 | }\r | |
1162 | if (!OutUnicode && !EFI_ERROR(Status)) {\r | |
1163 | TempHandle = CreateFileInterfaceFile(TempHandle, FALSE);\r | |
1164 | ASSERT(TempHandle != NULL);\r | |
1165 | }\r | |
a405b86d | 1166 | if (!EFI_ERROR(Status)) {\r |
3c865f20 | 1167 | ShellParameters->StdOut = TempHandle;\r |
dcf9b428 | 1168 | gST->ConOut = CreateSimpleTextOutOnFile(TempHandle, &gST->ConsoleOutHandle, gST->ConOut);\r |
a405b86d | 1169 | }\r |
1170 | }\r | |
a405b86d | 1171 | }\r |
1172 | \r | |
1173 | //\r | |
1174 | // StdOut to a var\r | |
1175 | //\r | |
1176 | if (!EFI_ERROR(Status) && StdOutVarName != NULL) {\r | |
1177 | if (!OutAppend) {\r | |
1178 | //\r | |
1179 | // delete existing variable.\r | |
1180 | //\r | |
1181 | SHELL_SET_ENVIRONMENT_VARIABLE_V(StdOutVarName, 0, L"");\r | |
1182 | }\r | |
1183 | TempHandle = CreateFileInterfaceEnv(StdOutVarName);\r | |
1184 | ASSERT(TempHandle != NULL);\r | |
a405b86d | 1185 | ShellParameters->StdOut = TempHandle;\r |
dcf9b428 | 1186 | gST->ConOut = CreateSimpleTextOutOnFile(TempHandle, &gST->ConsoleOutHandle, gST->ConOut);\r |
a405b86d | 1187 | }\r |
1188 | \r | |
1189 | //\r | |
1190 | // StdErr to a var\r | |
1191 | //\r | |
1192 | if (!EFI_ERROR(Status) && StdErrVarName != NULL) {\r | |
1193 | if (!ErrAppend) {\r | |
1194 | //\r | |
1195 | // delete existing variable.\r | |
1196 | //\r | |
1197 | SHELL_SET_ENVIRONMENT_VARIABLE_V(StdErrVarName, 0, L"");\r | |
1198 | }\r | |
1199 | TempHandle = CreateFileInterfaceEnv(StdErrVarName);\r | |
1200 | ASSERT(TempHandle != NULL);\r | |
a405b86d | 1201 | ShellParameters->StdErr = TempHandle;\r |
dcf9b428 | 1202 | gST->StdErr = CreateSimpleTextOutOnFile(TempHandle, &gST->StandardErrorHandle, gST->StdErr);\r |
a405b86d | 1203 | }\r |
1204 | \r | |
1205 | //\r | |
1206 | // StdIn from a var\r | |
1207 | //\r | |
1208 | if (!EFI_ERROR(Status) && StdInVarName != NULL) {\r | |
1209 | TempHandle = CreateFileInterfaceEnv(StdInVarName);\r | |
532691c8 | 1210 | if (TempHandle == NULL) {\r |
1211 | Status = EFI_OUT_OF_RESOURCES;\r | |
a405b86d | 1212 | } else {\r |
532691c8 | 1213 | if (!InUnicode) {\r |
1214 | TempHandle = CreateFileInterfaceFile(TempHandle, FALSE);\r | |
1215 | }\r | |
1216 | Size = 0;\r | |
96962f0a | 1217 | if (TempHandle == NULL || ((EFI_FILE_PROTOCOL*)TempHandle)->Read(TempHandle, &Size, NULL) != EFI_BUFFER_TOO_SMALL) {\r |
532691c8 | 1218 | Status = EFI_INVALID_PARAMETER;\r |
1219 | } else {\r | |
1220 | ShellParameters->StdIn = TempHandle;\r | |
1221 | gST->ConIn = CreateSimpleTextInOnFile(TempHandle, &gST->ConsoleInHandle);\r | |
1222 | }\r | |
a405b86d | 1223 | }\r |
1224 | }\r | |
1225 | \r | |
1226 | //\r | |
1227 | // StdIn from a file\r | |
1228 | //\r | |
1229 | if (!EFI_ERROR(Status) && StdInFileName != NULL) {\r | |
1230 | Status = ShellOpenFileByName(\r | |
1231 | StdInFileName,\r | |
1232 | &TempHandle,\r | |
1233 | EFI_FILE_MODE_READ,\r | |
1234 | 0);\r | |
a405b86d | 1235 | if (!EFI_ERROR(Status)) {\r |
231ad7d8 QS |
1236 | if (!InUnicode) {\r |
1237 | //\r | |
1238 | // Create the ASCII->Unicode conversion layer\r | |
1239 | //\r | |
1240 | TempHandle = CreateFileInterfaceFile(TempHandle, FALSE);\r | |
1241 | }\r | |
a405b86d | 1242 | ShellParameters->StdIn = TempHandle;\r |
8be0ba36 | 1243 | gST->ConIn = CreateSimpleTextInOnFile(TempHandle, &gST->ConsoleInHandle);\r |
a405b86d | 1244 | }\r |
1245 | }\r | |
1246 | }\r | |
1247 | }\r | |
1248 | FreePool(CommandLineCopy);\r | |
8be0ba36 | 1249 | \r |
cf6a8f14 | 1250 | CalculateEfiHdrCrc(&gST->Hdr);\r |
1251 | \r | |
8be0ba36 | 1252 | if (gST->ConIn == NULL ||gST->ConOut == NULL) {\r |
cc31ac1e | 1253 | Status = EFI_OUT_OF_RESOURCES;\r |
8be0ba36 | 1254 | }\r |
cc31ac1e JC |
1255 | \r |
1256 | if (Status == EFI_NOT_FOUND) {\r | |
1257 | ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_REDUNDA_REDIR), ShellInfoObject.HiiHandle);\r | |
1258 | } else if (EFI_ERROR(Status)) {\r | |
1259 | ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_INVALID_REDIR), ShellInfoObject.HiiHandle);\r | |
1260 | }\r | |
1261 | \r | |
a405b86d | 1262 | return (Status);\r |
1263 | }\r | |
1264 | \r | |
1265 | /**\r | |
1266 | Funcion will replace the current StdIn and StdOut in the ShellParameters protocol\r | |
1267 | structure with StdIn and StdOut. The current values are de-allocated.\r | |
1268 | \r | |
4ff7e37b ED |
1269 | @param[in, out] ShellParameters Pointer to parameter structure to modify.\r |
1270 | @param[in] OldStdIn Pointer to old StdIn.\r | |
1271 | @param[in] OldStdOut Pointer to old StdOut.\r | |
1272 | @param[in] OldStdErr Pointer to old StdErr.\r | |
1273 | @param[in] SystemTableInfo Pointer to old system table information.\r | |
a405b86d | 1274 | **/\r |
1275 | EFI_STATUS\r | |
a405b86d | 1276 | RestoreStdInStdOutStdErr (\r |
1277 | IN OUT EFI_SHELL_PARAMETERS_PROTOCOL *ShellParameters,\r | |
8be0ba36 | 1278 | IN SHELL_FILE_HANDLE *OldStdIn,\r |
1279 | IN SHELL_FILE_HANDLE *OldStdOut,\r | |
1280 | IN SHELL_FILE_HANDLE *OldStdErr,\r | |
1281 | IN SYSTEM_TABLE_INFO *SystemTableInfo\r | |
a405b86d | 1282 | )\r |
1283 | {\r | |
1284 | SPLIT_LIST *Split;\r | |
8be0ba36 | 1285 | \r |
ba0014b9 | 1286 | if (ShellParameters == NULL\r |
8be0ba36 | 1287 | ||OldStdIn == NULL\r |
1288 | ||OldStdOut == NULL\r | |
1289 | ||OldStdErr == NULL\r | |
1290 | ||SystemTableInfo == NULL) {\r | |
1291 | return (EFI_INVALID_PARAMETER);\r | |
1292 | }\r | |
a405b86d | 1293 | if (!IsListEmpty(&ShellInfoObject.SplitList.Link)) {\r |
1294 | Split = (SPLIT_LIST*)GetFirstNode(&ShellInfoObject.SplitList.Link);\r | |
1295 | } else {\r | |
1296 | Split = NULL;\r | |
1297 | }\r | |
8be0ba36 | 1298 | if (ShellParameters->StdIn != *OldStdIn) {\r |
a405b86d | 1299 | if ((Split != NULL && Split->SplitStdIn != ShellParameters->StdIn) || Split == NULL) {\r |
1300 | gEfiShellProtocol->CloseFile(ShellParameters->StdIn);\r | |
1301 | }\r | |
8be0ba36 | 1302 | ShellParameters->StdIn = *OldStdIn;\r |
a405b86d | 1303 | }\r |
8be0ba36 | 1304 | if (ShellParameters->StdOut != *OldStdOut) {\r |
a405b86d | 1305 | if ((Split != NULL && Split->SplitStdOut != ShellParameters->StdOut) || Split == NULL) {\r |
1306 | gEfiShellProtocol->CloseFile(ShellParameters->StdOut);\r | |
1307 | }\r | |
8be0ba36 | 1308 | ShellParameters->StdOut = *OldStdOut;\r |
1309 | }\r | |
1310 | if (ShellParameters->StdErr != *OldStdErr) {\r | |
1311 | gEfiShellProtocol->CloseFile(ShellParameters->StdErr);\r | |
1312 | ShellParameters->StdErr = *OldStdErr;\r | |
1313 | }\r | |
1314 | \r | |
1315 | if (gST->ConIn != SystemTableInfo->ConIn) {\r | |
1316 | CloseSimpleTextInOnFile(gST->ConIn);\r | |
1317 | gST->ConIn = SystemTableInfo->ConIn;\r | |
1318 | gST->ConsoleInHandle = SystemTableInfo->ConInHandle;\r | |
a405b86d | 1319 | }\r |
8be0ba36 | 1320 | if (gST->ConOut != SystemTableInfo->ConOut) {\r |
1321 | CloseSimpleTextOutOnFile(gST->ConOut);\r | |
1322 | gST->ConOut = SystemTableInfo->ConOut;\r | |
1323 | gST->ConsoleOutHandle = SystemTableInfo->ConOutHandle;\r | |
1324 | }\r | |
4ccd9214 | 1325 | if (gST->StdErr != SystemTableInfo->ErrOut) {\r |
8be0ba36 | 1326 | CloseSimpleTextOutOnFile(gST->StdErr);\r |
4ccd9214 ED |
1327 | gST->StdErr = SystemTableInfo->ErrOut;\r |
1328 | gST->StandardErrorHandle = SystemTableInfo->ErrOutHandle;\r | |
8be0ba36 | 1329 | }\r |
1330 | \r | |
cf6a8f14 | 1331 | CalculateEfiHdrCrc(&gST->Hdr);\r |
1332 | \r | |
a405b86d | 1333 | return (EFI_SUCCESS);\r |
1334 | }\r | |
1335 | /**\r | |
1336 | Funcion will replace the current Argc and Argv in the ShellParameters protocol\r | |
1337 | structure by parsing NewCommandLine. The current values are returned to the\r | |
1338 | user.\r | |
1339 | \r | |
1340 | If OldArgv or OldArgc is NULL then that value is not returned.\r | |
1341 | \r | |
4ff7e37b ED |
1342 | @param[in, out] ShellParameters Pointer to parameter structure to modify.\r |
1343 | @param[in] NewCommandLine The new command line to parse and use.\r | |
d1c275c6 | 1344 | @param[in] Type The type of operation.\r |
4ff7e37b ED |
1345 | @param[out] OldArgv Pointer to old list of parameters.\r |
1346 | @param[out] OldArgc Pointer to old number of items in Argv list.\r | |
a405b86d | 1347 | \r |
1348 | @retval EFI_SUCCESS Operation was sucessful, Argv and Argc are valid.\r | |
1349 | @retval EFI_OUT_OF_RESOURCES A memory allocation failed.\r | |
1350 | **/\r | |
1351 | EFI_STATUS\r | |
a405b86d | 1352 | UpdateArgcArgv(\r |
1353 | IN OUT EFI_SHELL_PARAMETERS_PROTOCOL *ShellParameters,\r | |
1354 | IN CONST CHAR16 *NewCommandLine,\r | |
d1c275c6 | 1355 | IN SHELL_OPERATION_TYPES Type,\r |
a405b86d | 1356 | OUT CHAR16 ***OldArgv OPTIONAL,\r |
1357 | OUT UINTN *OldArgc OPTIONAL\r | |
1358 | )\r | |
1359 | {\r | |
d1c275c6 | 1360 | BOOLEAN StripParamQuotation;\r |
ba0014b9 | 1361 | \r |
a405b86d | 1362 | ASSERT(ShellParameters != NULL);\r |
d1c275c6 | 1363 | StripParamQuotation = TRUE;\r |
a405b86d | 1364 | \r |
1365 | if (OldArgc != NULL) {\r | |
1366 | *OldArgc = ShellParameters->Argc;\r | |
1367 | }\r | |
1368 | if (OldArgc != NULL) {\r | |
1369 | *OldArgv = ShellParameters->Argv;\r | |
1370 | }\r | |
1371 | \r | |
d1c275c6 QS |
1372 | if (Type == Script_File_Name) {\r |
1373 | StripParamQuotation = FALSE;\r | |
1374 | }\r | |
ba0014b9 LG |
1375 | \r |
1376 | return ParseCommandLineToArgs( NewCommandLine,\r | |
1377 | StripParamQuotation,\r | |
1378 | &(ShellParameters->Argv),\r | |
d1c275c6 QS |
1379 | &(ShellParameters->Argc)\r |
1380 | );\r | |
a405b86d | 1381 | }\r |
1382 | \r | |
1383 | /**\r | |
1384 | Funcion will replace the current Argc and Argv in the ShellParameters protocol\r | |
1385 | structure with Argv and Argc. The current values are de-allocated and the\r | |
1386 | OldArgv must not be deallocated by the caller.\r | |
1387 | \r | |
4ff7e37b ED |
1388 | @param[in, out] ShellParameters pointer to parameter structure to modify\r |
1389 | @param[in] OldArgv pointer to old list of parameters\r | |
1390 | @param[in] OldArgc pointer to old number of items in Argv list\r | |
a405b86d | 1391 | **/\r |
1392 | VOID\r | |
a405b86d | 1393 | RestoreArgcArgv(\r |
1394 | IN OUT EFI_SHELL_PARAMETERS_PROTOCOL *ShellParameters,\r | |
1395 | IN CHAR16 ***OldArgv,\r | |
1396 | IN UINTN *OldArgc\r | |
1397 | )\r | |
1398 | {\r | |
1399 | UINTN LoopCounter;\r | |
1400 | ASSERT(ShellParameters != NULL);\r | |
1401 | ASSERT(OldArgv != NULL);\r | |
1402 | ASSERT(OldArgc != NULL);\r | |
1403 | \r | |
1404 | if (ShellParameters->Argv != NULL) {\r | |
1405 | for ( LoopCounter = 0\r | |
1406 | ; LoopCounter < ShellParameters->Argc\r | |
1407 | ; LoopCounter++\r | |
1408 | ){\r | |
1409 | FreePool(ShellParameters->Argv[LoopCounter]);\r | |
1410 | }\r | |
1411 | FreePool(ShellParameters->Argv);\r | |
1412 | }\r | |
1413 | ShellParameters->Argv = *OldArgv;\r | |
1414 | *OldArgv = NULL;\r | |
1415 | ShellParameters->Argc = *OldArgc;\r | |
1416 | *OldArgc = 0;\r | |
1417 | }\r |