]>
Commit | Line | Data |
---|---|---|
a405b86d | 1 | /** @file\r |
2 | This is THE shell (application)\r | |
3 | \r | |
dcbdb8bf | 4 | Copyright (c) 2009 - 2016, Intel Corporation. All rights reserved.<BR>\r |
c011b6c9 | 5 | (C) Copyright 2013-2014 Hewlett-Packard Development Company, L.P.<BR>\r |
a405b86d | 6 | This program and the accompanying materials\r |
7 | are licensed and made available under the terms and conditions of the BSD License\r | |
8 | which accompanies this distribution. The full text of the license may be found at\r | |
9 | http://opensource.org/licenses/bsd-license.php\r | |
10 | \r | |
11 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
12 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
13 | \r | |
14 | **/\r | |
15 | \r | |
16 | #include "Shell.h"\r | |
17 | \r | |
18 | //\r | |
19 | // Initialize the global structure\r | |
20 | //\r | |
21 | SHELL_INFO ShellInfoObject = {\r | |
22 | NULL,\r | |
23 | NULL,\r | |
24 | FALSE,\r | |
25 | FALSE,\r | |
26 | {\r | |
ce68d3bc SZ |
27 | {{\r |
28 | 0,\r | |
29 | 0,\r | |
30 | 0,\r | |
31 | 0,\r | |
32 | 0,\r | |
33 | 0,\r | |
34 | 0,\r | |
35 | 0,\r | |
dcbdb8bf | 36 | 0,\r |
ce68d3bc SZ |
37 | 0\r |
38 | }},\r | |
a405b86d | 39 | 0,\r |
40 | NULL,\r | |
41 | NULL\r | |
42 | },\r | |
ce68d3bc | 43 | {{NULL, NULL}, NULL},\r |
a405b86d | 44 | {\r |
ce68d3bc | 45 | {{NULL, NULL}, NULL},\r |
a405b86d | 46 | 0,\r |
47 | 0,\r | |
48 | TRUE\r | |
49 | },\r | |
50 | NULL,\r | |
51 | 0,\r | |
52 | NULL,\r | |
53 | NULL,\r | |
54 | NULL,\r | |
55 | NULL,\r | |
56 | NULL,\r | |
ce68d3bc SZ |
57 | {{NULL, NULL}, NULL, NULL},\r |
58 | {{NULL, NULL}, NULL, NULL},\r | |
59 | NULL,\r | |
60 | NULL,\r | |
61 | NULL,\r | |
62 | NULL,\r | |
8be0ba36 | 63 | NULL,\r |
64 | NULL,\r | |
65 | NULL,\r | |
a49f6a2f | 66 | NULL,\r |
67 | FALSE\r | |
a405b86d | 68 | };\r |
69 | \r | |
70 | STATIC CONST CHAR16 mScriptExtension[] = L".NSH";\r | |
71 | STATIC CONST CHAR16 mExecutableExtensions[] = L".NSH;.EFI";\r | |
3c865f20 | 72 | STATIC CONST CHAR16 mStartupScript[] = L"startup.nsh";\r |
dcbdb8bf QS |
73 | CONST CHAR16 mNoNestingEnvVarName[] = L"nonesting";\r |
74 | CONST CHAR16 mNoNestingTrue[] = L"True";\r | |
75 | CONST CHAR16 mNoNestingFalse[] = L"False";\r | |
a405b86d | 76 | \r |
ad2bc854 | 77 | /**\r |
51429264 | 78 | Cleans off leading and trailing spaces and tabs.\r |
ad2bc854 | 79 | \r |
51429264 | 80 | @param[in] String pointer to the string to trim them off.\r |
ad2bc854 JC |
81 | **/\r |
82 | EFI_STATUS\r | |
83 | EFIAPI\r | |
84 | TrimSpaces(\r | |
85 | IN CHAR16 **String\r | |
86 | )\r | |
87 | {\r | |
88 | ASSERT(String != NULL);\r | |
89 | ASSERT(*String!= NULL);\r | |
90 | //\r | |
91 | // Remove any spaces and tabs at the beginning of the (*String).\r | |
92 | //\r | |
93 | while (((*String)[0] == L' ') || ((*String)[0] == L'\t')) {\r | |
94 | CopyMem((*String), (*String)+1, StrSize((*String)) - sizeof((*String)[0]));\r | |
95 | }\r | |
96 | \r | |
97 | //\r | |
404b3f43 | 98 | // Remove any spaces and tabs at the end of the (*String).\r |
ad2bc854 | 99 | //\r |
404b3f43 | 100 | while ((StrLen (*String) > 0) && (((*String)[StrLen((*String))-1] == L' ') || ((*String)[StrLen((*String))-1] == L'\t'))) {\r |
ad2bc854 JC |
101 | (*String)[StrLen((*String))-1] = CHAR_NULL;\r |
102 | }\r | |
103 | \r | |
104 | return (EFI_SUCCESS);\r | |
105 | }\r | |
106 | \r | |
a95cf8f0 JC |
107 | /**\r |
108 | Parse for the next instance of one string within another string. Can optionally make sure that \r | |
109 | the string was not escaped (^ character) per the shell specification.\r | |
110 | \r | |
111 | @param[in] SourceString The string to search within\r | |
112 | @param[in] FindString The string to look for\r | |
113 | @param[in] CheckForEscapeCharacter TRUE to skip escaped instances of FinfString, otherwise will return even escaped instances\r | |
114 | **/\r | |
115 | CHAR16*\r | |
116 | EFIAPI\r | |
117 | FindNextInstance(\r | |
118 | IN CONST CHAR16 *SourceString,\r | |
119 | IN CONST CHAR16 *FindString,\r | |
120 | IN CONST BOOLEAN CheckForEscapeCharacter\r | |
121 | )\r | |
122 | {\r | |
123 | CHAR16 *Temp;\r | |
124 | if (SourceString == NULL) {\r | |
125 | return (NULL);\r | |
126 | }\r | |
127 | Temp = StrStr(SourceString, FindString);\r | |
128 | \r | |
129 | //\r | |
d08a5464 | 130 | // If nothing found, or we don't care about escape characters\r |
a95cf8f0 JC |
131 | //\r |
132 | if (Temp == NULL || !CheckForEscapeCharacter) {\r | |
133 | return (Temp);\r | |
134 | }\r | |
135 | \r | |
136 | //\r | |
137 | // If we found an escaped character, try again on the remainder of the string\r | |
138 | //\r | |
139 | if ((Temp > (SourceString)) && *(Temp-1) == L'^') {\r | |
140 | return FindNextInstance(Temp+1, FindString, CheckForEscapeCharacter);\r | |
141 | }\r | |
142 | \r | |
143 | //\r | |
144 | // we found the right character\r | |
145 | //\r | |
146 | return (Temp);\r | |
147 | }\r | |
148 | \r | |
42435671 | 149 | /**\r |
d08a5464 | 150 | Check whether the string between a pair of % is a valid environment variable name.\r |
42435671 QS |
151 | \r |
152 | @param[in] BeginPercent pointer to the first percent.\r | |
153 | @param[in] EndPercent pointer to the last percent.\r | |
154 | \r | |
155 | @retval TRUE is a valid environment variable name.\r | |
156 | @retval FALSE is NOT a valid environment variable name.\r | |
157 | **/\r | |
158 | BOOLEAN\r | |
159 | IsValidEnvironmentVariableName(\r | |
160 | IN CONST CHAR16 *BeginPercent,\r | |
161 | IN CONST CHAR16 *EndPercent\r | |
162 | )\r | |
163 | {\r | |
164 | CONST CHAR16 *Walker;\r | |
165 | \r | |
166 | Walker = NULL;\r | |
167 | \r | |
168 | ASSERT (BeginPercent != NULL);\r | |
169 | ASSERT (EndPercent != NULL);\r | |
170 | ASSERT (BeginPercent < EndPercent);\r | |
171 | \r | |
172 | if ((BeginPercent + 1) == EndPercent) {\r | |
173 | return FALSE;\r | |
174 | }\r | |
175 | \r | |
176 | for (Walker = BeginPercent + 1; Walker < EndPercent; Walker++) {\r | |
177 | if (\r | |
178 | (*Walker >= L'0' && *Walker <= L'9') ||\r | |
179 | (*Walker >= L'A' && *Walker <= L'Z') ||\r | |
180 | (*Walker >= L'a' && *Walker <= L'z') ||\r | |
181 | (*Walker == L'_')\r | |
182 | ) {\r | |
183 | if (Walker == BeginPercent + 1 && (*Walker >= L'0' && *Walker <= L'9')) {\r | |
184 | return FALSE;\r | |
185 | } else {\r | |
186 | continue;\r | |
187 | }\r | |
188 | } else {\r | |
189 | return FALSE;\r | |
190 | }\r | |
191 | }\r | |
192 | \r | |
193 | return TRUE;\r | |
194 | }\r | |
195 | \r | |
518c8cdc JC |
196 | /**\r |
197 | Determine if a command line contains a split operation\r | |
198 | \r | |
199 | @param[in] CmdLine The command line to parse.\r | |
200 | \r | |
201 | @retval TRUE CmdLine has a valid split.\r | |
202 | @retval FALSE CmdLine does not have a valid split.\r | |
203 | **/\r | |
204 | BOOLEAN\r | |
205 | EFIAPI\r | |
206 | ContainsSplit(\r | |
207 | IN CONST CHAR16 *CmdLine\r | |
208 | )\r | |
209 | {\r | |
210 | CONST CHAR16 *TempSpot;\r | |
6f6792b8 QS |
211 | CONST CHAR16 *FirstQuote;\r |
212 | CONST CHAR16 *SecondQuote;\r | |
213 | \r | |
a95cf8f0 | 214 | FirstQuote = FindNextInstance (CmdLine, L"\"", TRUE);\r |
6f6792b8 | 215 | SecondQuote = NULL;\r |
c0bcd343 | 216 | TempSpot = ShellFindFirstCharacter(CmdLine, L"|", TRUE);\r |
6f6792b8 | 217 | \r |
fe8ec3dd JC |
218 | if (FirstQuote == NULL || \r |
219 | TempSpot == NULL || \r | |
220 | TempSpot == CHAR_NULL || \r | |
6f6792b8 QS |
221 | FirstQuote > TempSpot\r |
222 | ) {\r | |
223 | return (BOOLEAN) ((TempSpot != NULL) && (*TempSpot != CHAR_NULL));\r | |
224 | }\r | |
225 | \r | |
226 | while ((TempSpot != NULL) && (*TempSpot != CHAR_NULL)) {\r | |
fe8ec3dd | 227 | if (FirstQuote == NULL || FirstQuote > TempSpot) {\r |
6f6792b8 QS |
228 | break;\r |
229 | } \r | |
a95cf8f0 | 230 | SecondQuote = FindNextInstance (FirstQuote + 1, L"\"", TRUE);\r |
fe8ec3dd | 231 | if (SecondQuote == NULL) {\r |
6f6792b8 QS |
232 | break;\r |
233 | }\r | |
234 | if (SecondQuote < TempSpot) {\r | |
a95cf8f0 | 235 | FirstQuote = FindNextInstance (SecondQuote + 1, L"\"", TRUE);\r |
6f6792b8 QS |
236 | continue;\r |
237 | } else {\r | |
a95cf8f0 | 238 | FirstQuote = FindNextInstance (SecondQuote + 1, L"\"", TRUE);\r |
c0bcd343 | 239 | TempSpot = ShellFindFirstCharacter(TempSpot + 1, L"|", TRUE);\r |
6f6792b8 QS |
240 | continue;\r |
241 | } \r | |
242 | }\r | |
243 | \r | |
8dcd84b9 | 244 | return (BOOLEAN) ((TempSpot != NULL) && (*TempSpot != CHAR_NULL));\r |
19a44972 JC |
245 | }\r |
246 | \r | |
733f138d | 247 | /**\r |
248 | Function to start monitoring for CTRL-S using SimpleTextInputEx. This \r | |
249 | feature's enabled state was not known when the shell initially launched.\r | |
250 | \r | |
251 | @retval EFI_SUCCESS The feature is enabled.\r | |
d08a5464 | 252 | @retval EFI_OUT_OF_RESOURCES There is not enough memory available.\r |
733f138d | 253 | **/\r |
254 | EFI_STATUS\r | |
255 | EFIAPI\r | |
256 | InternalEfiShellStartCtrlSMonitor(\r | |
257 | VOID\r | |
258 | )\r | |
259 | {\r | |
260 | EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *SimpleEx;\r | |
261 | EFI_KEY_DATA KeyData;\r | |
262 | EFI_STATUS Status;\r | |
263 | \r | |
264 | Status = gBS->OpenProtocol(\r | |
265 | gST->ConsoleInHandle,\r | |
266 | &gEfiSimpleTextInputExProtocolGuid,\r | |
267 | (VOID**)&SimpleEx,\r | |
268 | gImageHandle,\r | |
269 | NULL,\r | |
270 | EFI_OPEN_PROTOCOL_GET_PROTOCOL);\r | |
271 | if (EFI_ERROR(Status)) {\r | |
272 | ShellPrintHiiEx(\r | |
273 | -1, \r | |
274 | -1, \r | |
275 | NULL,\r | |
276 | STRING_TOKEN (STR_SHELL_NO_IN_EX),\r | |
277 | ShellInfoObject.HiiHandle);\r | |
278 | return (EFI_SUCCESS);\r | |
279 | }\r | |
280 | \r | |
281 | KeyData.KeyState.KeyToggleState = 0;\r | |
282 | KeyData.Key.ScanCode = 0;\r | |
283 | KeyData.KeyState.KeyShiftState = EFI_SHIFT_STATE_VALID|EFI_LEFT_CONTROL_PRESSED;\r | |
284 | KeyData.Key.UnicodeChar = L's';\r | |
285 | \r | |
286 | Status = SimpleEx->RegisterKeyNotify(\r | |
287 | SimpleEx,\r | |
288 | &KeyData,\r | |
289 | NotificationFunction,\r | |
290 | &ShellInfoObject.CtrlSNotifyHandle1);\r | |
291 | \r | |
292 | KeyData.KeyState.KeyShiftState = EFI_SHIFT_STATE_VALID|EFI_RIGHT_CONTROL_PRESSED;\r | |
293 | if (!EFI_ERROR(Status)) {\r | |
294 | Status = SimpleEx->RegisterKeyNotify(\r | |
295 | SimpleEx,\r | |
296 | &KeyData,\r | |
297 | NotificationFunction,\r | |
298 | &ShellInfoObject.CtrlSNotifyHandle2);\r | |
299 | }\r | |
300 | KeyData.KeyState.KeyShiftState = EFI_SHIFT_STATE_VALID|EFI_LEFT_CONTROL_PRESSED;\r | |
301 | KeyData.Key.UnicodeChar = 19;\r | |
302 | \r | |
303 | if (!EFI_ERROR(Status)) {\r | |
304 | Status = SimpleEx->RegisterKeyNotify(\r | |
305 | SimpleEx,\r | |
306 | &KeyData,\r | |
307 | NotificationFunction,\r | |
ed8b5297 | 308 | &ShellInfoObject.CtrlSNotifyHandle3);\r |
733f138d | 309 | } \r |
310 | KeyData.KeyState.KeyShiftState = EFI_SHIFT_STATE_VALID|EFI_RIGHT_CONTROL_PRESSED;\r | |
311 | if (!EFI_ERROR(Status)) {\r | |
312 | Status = SimpleEx->RegisterKeyNotify(\r | |
313 | SimpleEx,\r | |
314 | &KeyData,\r | |
315 | NotificationFunction,\r | |
ed8b5297 | 316 | &ShellInfoObject.CtrlSNotifyHandle4);\r |
733f138d | 317 | }\r |
318 | return (Status);\r | |
319 | }\r | |
320 | \r | |
321 | \r | |
322 | \r | |
a405b86d | 323 | /**\r |
324 | The entry point for the application.\r | |
325 | \r | |
326 | @param[in] ImageHandle The firmware allocated handle for the EFI image.\r | |
327 | @param[in] SystemTable A pointer to the EFI System Table.\r | |
328 | \r | |
329 | @retval EFI_SUCCESS The entry point is executed successfully.\r | |
330 | @retval other Some error occurs when executing this entry point.\r | |
331 | \r | |
332 | **/\r | |
333 | EFI_STATUS\r | |
334 | EFIAPI\r | |
335 | UefiMain (\r | |
336 | IN EFI_HANDLE ImageHandle,\r | |
337 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
338 | )\r | |
339 | {\r | |
a49f6a2f | 340 | EFI_STATUS Status;\r |
341 | CHAR16 *TempString;\r | |
342 | UINTN Size;\r | |
343 | EFI_HANDLE ConInHandle;\r | |
344 | EFI_SIMPLE_TEXT_INPUT_PROTOCOL *OldConIn;\r | |
bd3fc813 | 345 | SPLIT_LIST *Split;\r |
a405b86d | 346 | \r |
347 | if (PcdGet8(PcdShellSupportLevel) > 3) {\r | |
348 | return (EFI_UNSUPPORTED);\r | |
349 | }\r | |
350 | \r | |
351 | //\r | |
352 | // Clear the screen\r | |
353 | //\r | |
354 | Status = gST->ConOut->ClearScreen(gST->ConOut);\r | |
3c865f20 | 355 | if (EFI_ERROR(Status)) {\r |
356 | return (Status);\r | |
357 | }\r | |
a405b86d | 358 | \r |
359 | //\r | |
360 | // Populate the global structure from PCDs\r | |
361 | //\r | |
362 | ShellInfoObject.ImageDevPath = NULL;\r | |
363 | ShellInfoObject.FileDevPath = NULL;\r | |
364 | ShellInfoObject.PageBreakEnabled = PcdGetBool(PcdShellPageBreakDefault);\r | |
365 | ShellInfoObject.ViewingSettings.InsertMode = PcdGetBool(PcdShellInsertModeDefault);\r | |
366 | ShellInfoObject.LogScreenCount = PcdGet8 (PcdShellScreenLogCount );\r | |
367 | \r | |
368 | //\r | |
369 | // verify we dont allow for spec violation\r | |
370 | //\r | |
371 | ASSERT(ShellInfoObject.LogScreenCount >= 3);\r | |
372 | \r | |
373 | //\r | |
374 | // Initialize the LIST ENTRY objects...\r | |
375 | //\r | |
376 | InitializeListHead(&ShellInfoObject.BufferToFreeList.Link);\r | |
377 | InitializeListHead(&ShellInfoObject.ViewingSettings.CommandHistory.Link);\r | |
378 | InitializeListHead(&ShellInfoObject.SplitList.Link);\r | |
379 | \r | |
380 | //\r | |
381 | // Check PCDs for optional features that are not implemented yet.\r | |
382 | //\r | |
383 | if ( PcdGetBool(PcdShellSupportOldProtocols)\r | |
384 | || !FeaturePcdGet(PcdShellRequireHiiPlatform)\r | |
385 | || FeaturePcdGet(PcdShellSupportFrameworkHii)\r | |
386 | ) {\r | |
387 | return (EFI_UNSUPPORTED);\r | |
388 | }\r | |
389 | \r | |
390 | //\r | |
391 | // turn off the watchdog timer\r | |
392 | //\r | |
393 | gBS->SetWatchdogTimer (0, 0, 0, NULL);\r | |
394 | \r | |
395 | //\r | |
396 | // install our console logger. This will keep a log of the output for back-browsing\r | |
397 | //\r | |
398 | Status = ConsoleLoggerInstall(ShellInfoObject.LogScreenCount, &ShellInfoObject.ConsoleInfo);\r | |
3c865f20 | 399 | if (!EFI_ERROR(Status)) {\r |
400 | //\r | |
401 | // Enable the cursor to be visible\r | |
402 | //\r | |
403 | gST->ConOut->EnableCursor (gST->ConOut, TRUE);\r | |
a405b86d | 404 | \r |
3c865f20 | 405 | //\r |
406 | // If supporting EFI 1.1 we need to install HII protocol\r | |
407 | // only do this if PcdShellRequireHiiPlatform == FALSE\r | |
408 | //\r | |
409 | // remove EFI_UNSUPPORTED check above when complete.\r | |
410 | ///@todo add support for Framework HII\r | |
a405b86d | 411 | \r |
3c865f20 | 412 | //\r |
413 | // install our (solitary) HII package\r | |
414 | //\r | |
415 | ShellInfoObject.HiiHandle = HiiAddPackages (&gEfiCallerIdGuid, gImageHandle, ShellStrings, NULL);\r | |
a405b86d | 416 | if (ShellInfoObject.HiiHandle == NULL) {\r |
3c865f20 | 417 | if (PcdGetBool(PcdShellSupportFrameworkHii)) {\r |
418 | ///@todo Add our package into Framework HII\r | |
419 | }\r | |
420 | if (ShellInfoObject.HiiHandle == NULL) {\r | |
0477054b BJ |
421 | Status = EFI_NOT_STARTED;\r |
422 | goto FreeResources;\r | |
3c865f20 | 423 | }\r |
a405b86d | 424 | }\r |
a405b86d | 425 | \r |
3c865f20 | 426 | //\r |
427 | // create and install the EfiShellParametersProtocol\r | |
428 | //\r | |
429 | Status = CreatePopulateInstallShellParametersProtocol(&ShellInfoObject.NewShellParametersProtocol, &ShellInfoObject.RootShellInstance);\r | |
430 | ASSERT_EFI_ERROR(Status);\r | |
431 | ASSERT(ShellInfoObject.NewShellParametersProtocol != NULL);\r | |
a405b86d | 432 | \r |
3c865f20 | 433 | //\r |
434 | // create and install the EfiShellProtocol\r | |
435 | //\r | |
436 | Status = CreatePopulateInstallShellProtocol(&ShellInfoObject.NewEfiShellProtocol);\r | |
437 | ASSERT_EFI_ERROR(Status);\r | |
438 | ASSERT(ShellInfoObject.NewEfiShellProtocol != NULL);\r | |
a405b86d | 439 | \r |
3c865f20 | 440 | //\r |
441 | // Now initialize the shell library (it requires Shell Parameters protocol)\r | |
442 | //\r | |
443 | Status = ShellInitialize();\r | |
444 | ASSERT_EFI_ERROR(Status);\r | |
a405b86d | 445 | \r |
3c865f20 | 446 | Status = CommandInit();\r |
447 | ASSERT_EFI_ERROR(Status);\r | |
a405b86d | 448 | \r |
b62bb885 QS |
449 | Status = ShellInitEnvVarList ();\r |
450 | \r | |
3c865f20 | 451 | //\r |
452 | // Check the command line\r | |
453 | //\r | |
da92bf85 BJ |
454 | Status = ProcessCommandLine ();\r |
455 | if (EFI_ERROR (Status)) {\r | |
456 | goto FreeResources;\r | |
457 | }\r | |
a405b86d | 458 | \r |
3c865f20 | 459 | //\r |
460 | // If shell support level is >= 1 create the mappings and paths\r | |
461 | //\r | |
462 | if (PcdGet8(PcdShellSupportLevel) >= 1) {\r | |
463 | Status = ShellCommandCreateInitialMappingsAndPaths();\r | |
464 | }\r | |
a405b86d | 465 | \r |
dcbdb8bf QS |
466 | //\r |
467 | // Set the environment variable for nesting support\r | |
468 | //\r | |
469 | Size = 0;\r | |
470 | TempString = NULL;\r | |
471 | if (!ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoNest) {\r | |
472 | //\r | |
473 | // No change. require nesting in Shell Protocol Execute()\r | |
474 | //\r | |
475 | StrnCatGrow(&TempString,\r | |
476 | &Size,\r | |
477 | L"False",\r | |
478 | 0);\r | |
479 | } else {\r | |
480 | StrnCatGrow(&TempString,\r | |
481 | &Size,\r | |
482 | mNoNestingTrue,\r | |
483 | 0);\r | |
484 | }\r | |
485 | Status = InternalEfiShellSetEnv(mNoNestingEnvVarName, TempString, TRUE);\r | |
486 | SHELL_FREE_NON_NULL(TempString);\r | |
487 | Size = 0;\r | |
488 | \r | |
3c865f20 | 489 | //\r |
490 | // save the device path for the loaded image and the device path for the filepath (under loaded image)\r | |
491 | // These are where to look for the startup.nsh file\r | |
492 | //\r | |
493 | Status = GetDevicePathsForImageAndFile(&ShellInfoObject.ImageDevPath, &ShellInfoObject.FileDevPath);\r | |
a405b86d | 494 | ASSERT_EFI_ERROR(Status);\r |
a405b86d | 495 | \r |
3c865f20 | 496 | //\r |
497 | // Display the version\r | |
498 | //\r | |
499 | if (!ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoVersion) {\r | |
500 | ShellPrintHiiEx (\r | |
501 | 0,\r | |
502 | gST->ConOut->Mode->CursorRow,\r | |
503 | NULL,\r | |
284e034f | 504 | STRING_TOKEN (STR_VER_OUTPUT_MAIN_SHELL),\r |
3c865f20 | 505 | ShellInfoObject.HiiHandle,\r |
506 | SupportLevel[PcdGet8(PcdShellSupportLevel)],\r | |
507 | gEfiShellProtocol->MajorVersion,\r | |
284e034f CP |
508 | gEfiShellProtocol->MinorVersion\r |
509 | );\r | |
510 | \r | |
511 | ShellPrintHiiEx (\r | |
512 | -1,\r | |
513 | -1,\r | |
514 | NULL,\r | |
515 | STRING_TOKEN (STR_VER_OUTPUT_MAIN_SUPPLIER),\r | |
516 | ShellInfoObject.HiiHandle,\r | |
517 | (CHAR16 *) PcdGetPtr (PcdShellSupplier)\r | |
518 | );\r | |
519 | \r | |
520 | ShellPrintHiiEx (\r | |
521 | -1,\r | |
522 | -1,\r | |
523 | NULL,\r | |
524 | STRING_TOKEN (STR_VER_OUTPUT_MAIN_UEFI),\r | |
525 | ShellInfoObject.HiiHandle,\r | |
3c865f20 | 526 | (gST->Hdr.Revision&0xffff0000)>>16,\r |
527 | (gST->Hdr.Revision&0x0000ffff),\r | |
528 | gST->FirmwareVendor,\r | |
529 | gST->FirmwareRevision\r | |
530 | );\r | |
531 | }\r | |
a405b86d | 532 | \r |
3c865f20 | 533 | //\r |
534 | // Display the mapping\r | |
535 | //\r | |
536 | if (PcdGet8(PcdShellSupportLevel) >= 2 && !ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoMap) {\r | |
a308e058 | 537 | Status = RunCommand(L"map");\r |
3c865f20 | 538 | ASSERT_EFI_ERROR(Status);\r |
539 | }\r | |
a405b86d | 540 | \r |
3c865f20 | 541 | //\r |
542 | // init all the built in alias'\r | |
543 | //\r | |
544 | Status = SetBuiltInAlias();\r | |
545 | ASSERT_EFI_ERROR(Status);\r | |
a405b86d | 546 | \r |
3c865f20 | 547 | //\r |
548 | // Initialize environment variables\r | |
549 | //\r | |
550 | if (ShellCommandGetProfileList() != NULL) {\r | |
551 | Status = InternalEfiShellSetEnv(L"profiles", ShellCommandGetProfileList(), TRUE);\r | |
552 | ASSERT_EFI_ERROR(Status);\r | |
553 | }\r | |
a405b86d | 554 | \r |
3c865f20 | 555 | Size = 100;\r |
556 | TempString = AllocateZeroPool(Size);\r | |
a405b86d | 557 | \r |
3c865f20 | 558 | UnicodeSPrint(TempString, Size, L"%d", PcdGet8(PcdShellSupportLevel));\r |
559 | Status = InternalEfiShellSetEnv(L"uefishellsupport", TempString, TRUE);\r | |
560 | ASSERT_EFI_ERROR(Status);\r | |
a405b86d | 561 | \r |
3c865f20 | 562 | UnicodeSPrint(TempString, Size, L"%d.%d", ShellInfoObject.NewEfiShellProtocol->MajorVersion, ShellInfoObject.NewEfiShellProtocol->MinorVersion);\r |
563 | Status = InternalEfiShellSetEnv(L"uefishellversion", TempString, TRUE);\r | |
564 | ASSERT_EFI_ERROR(Status);\r | |
a405b86d | 565 | \r |
3c865f20 | 566 | UnicodeSPrint(TempString, Size, L"%d.%d", (gST->Hdr.Revision & 0xFFFF0000) >> 16, gST->Hdr.Revision & 0x0000FFFF);\r |
567 | Status = InternalEfiShellSetEnv(L"uefiversion", TempString, TRUE);\r | |
568 | ASSERT_EFI_ERROR(Status);\r | |
a405b86d | 569 | \r |
3c865f20 | 570 | FreePool(TempString);\r |
a405b86d | 571 | \r |
3c865f20 | 572 | if (!EFI_ERROR(Status)) {\r |
573 | if (!ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoInterrupt) {\r | |
a405b86d | 574 | //\r |
3c865f20 | 575 | // Set up the event for CTRL-C monitoring...\r |
a405b86d | 576 | //\r |
8be0ba36 | 577 | Status = InernalEfiShellStartMonitor();\r |
3c865f20 | 578 | }\r |
579 | \r | |
733f138d | 580 | if (!EFI_ERROR(Status) && !ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoConsoleIn) {\r |
581 | //\r | |
582 | // Set up the event for CTRL-S monitoring...\r | |
583 | //\r | |
584 | Status = InternalEfiShellStartCtrlSMonitor();\r | |
585 | }\r | |
586 | \r | |
a49f6a2f | 587 | if (!EFI_ERROR(Status) && ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoConsoleIn) {\r |
588 | //\r | |
589 | // close off the gST->ConIn\r | |
590 | //\r | |
591 | OldConIn = gST->ConIn;\r | |
592 | ConInHandle = gST->ConsoleInHandle;\r | |
593 | gST->ConIn = CreateSimpleTextInOnFile((SHELL_FILE_HANDLE)&FileInterfaceNulFile, &gST->ConsoleInHandle);\r | |
594 | } else {\r | |
595 | OldConIn = NULL;\r | |
596 | ConInHandle = NULL;\r | |
597 | }\r | |
598 | \r | |
3c865f20 | 599 | if (!EFI_ERROR(Status) && PcdGet8(PcdShellSupportLevel) >= 1) {\r |
a405b86d | 600 | //\r |
3c865f20 | 601 | // process the startup script or launch the called app.\r |
a405b86d | 602 | //\r |
a308e058 | 603 | Status = DoStartupScript(ShellInfoObject.ImageDevPath, ShellInfoObject.FileDevPath);\r |
3c865f20 | 604 | }\r |
a405b86d | 605 | \r |
ca79c798 | 606 | if (!ShellInfoObject.ShellInitSettings.BitUnion.Bits.Exit && !ShellCommandGetExit() && (PcdGet8(PcdShellSupportLevel) >= 3 || PcdGetBool(PcdShellForceConsole)) && !EFI_ERROR(Status) && !ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoConsoleIn) {\r |
a405b86d | 607 | //\r |
3c865f20 | 608 | // begin the UI waiting loop\r |
a405b86d | 609 | //\r |
3c865f20 | 610 | do {\r |
611 | //\r | |
612 | // clean out all the memory allocated for CONST <something> * return values\r | |
613 | // between each shell prompt presentation\r | |
614 | //\r | |
615 | if (!IsListEmpty(&ShellInfoObject.BufferToFreeList.Link)){\r | |
616 | FreeBufferList(&ShellInfoObject.BufferToFreeList);\r | |
617 | }\r | |
618 | \r | |
619 | //\r | |
620 | // Reset page break back to default.\r | |
621 | //\r | |
622 | ShellInfoObject.PageBreakEnabled = PcdGetBool(PcdShellPageBreakDefault);\r | |
81cd2f53 | 623 | ASSERT (ShellInfoObject.ConsoleInfo != NULL);\r |
3c865f20 | 624 | ShellInfoObject.ConsoleInfo->Enabled = TRUE;\r |
625 | ShellInfoObject.ConsoleInfo->RowCounter = 0;\r | |
626 | \r | |
8be0ba36 | 627 | //\r |
628 | // Reset the CTRL-C event (yes we ignore the return values)\r | |
629 | //\r | |
630 | Status = gBS->CheckEvent (ShellInfoObject.NewEfiShellProtocol->ExecutionBreak);\r | |
631 | \r | |
3c865f20 | 632 | //\r |
633 | // Display Prompt\r | |
634 | //\r | |
635 | Status = DoShellPrompt();\r | |
636 | } while (!ShellCommandGetExit());\r | |
637 | }\r | |
a49f6a2f | 638 | if (OldConIn != NULL && ConInHandle != NULL) {\r |
639 | CloseSimpleTextInOnFile (gST->ConIn);\r | |
640 | gST->ConIn = OldConIn;\r | |
641 | gST->ConsoleInHandle = ConInHandle;\r | |
642 | }\r | |
a405b86d | 643 | }\r |
644 | }\r | |
3c865f20 | 645 | \r |
0477054b | 646 | FreeResources:\r |
a405b86d | 647 | //\r |
648 | // uninstall protocols / free memory / etc...\r | |
649 | //\r | |
650 | if (ShellInfoObject.UserBreakTimer != NULL) {\r | |
651 | gBS->CloseEvent(ShellInfoObject.UserBreakTimer);\r | |
652 | DEBUG_CODE(ShellInfoObject.UserBreakTimer = NULL;);\r | |
653 | }\r | |
a405b86d | 654 | if (ShellInfoObject.ImageDevPath != NULL) {\r |
655 | FreePool(ShellInfoObject.ImageDevPath);\r | |
656 | DEBUG_CODE(ShellInfoObject.ImageDevPath = NULL;);\r | |
657 | }\r | |
658 | if (ShellInfoObject.FileDevPath != NULL) {\r | |
659 | FreePool(ShellInfoObject.FileDevPath);\r | |
660 | DEBUG_CODE(ShellInfoObject.FileDevPath = NULL;);\r | |
661 | }\r | |
662 | if (ShellInfoObject.NewShellParametersProtocol != NULL) {\r | |
663 | CleanUpShellParametersProtocol(ShellInfoObject.NewShellParametersProtocol);\r | |
664 | DEBUG_CODE(ShellInfoObject.NewShellParametersProtocol = NULL;);\r | |
665 | }\r | |
666 | if (ShellInfoObject.NewEfiShellProtocol != NULL){\r | |
8be0ba36 | 667 | if (ShellInfoObject.NewEfiShellProtocol->IsRootShell()){\r |
92670471 | 668 | InternalEfiShellSetEnv(L"cwd", NULL, TRUE);\r |
8be0ba36 | 669 | }\r |
a405b86d | 670 | CleanUpShellProtocol(ShellInfoObject.NewEfiShellProtocol);\r |
671 | DEBUG_CODE(ShellInfoObject.NewEfiShellProtocol = NULL;);\r | |
672 | }\r | |
673 | \r | |
674 | if (!IsListEmpty(&ShellInfoObject.BufferToFreeList.Link)){\r | |
675 | FreeBufferList(&ShellInfoObject.BufferToFreeList);\r | |
676 | }\r | |
677 | \r | |
678 | if (!IsListEmpty(&ShellInfoObject.SplitList.Link)){\r | |
bd3fc813 MH |
679 | ASSERT(FALSE); ///@todo finish this de-allocation (free SplitStdIn/Out when needed).\r |
680 | \r | |
681 | for ( Split = (SPLIT_LIST*)GetFirstNode (&ShellInfoObject.SplitList.Link)\r | |
682 | ; !IsNull (&ShellInfoObject.SplitList.Link, &Split->Link)\r | |
683 | ; Split = (SPLIT_LIST *)GetNextNode (&ShellInfoObject.SplitList.Link, &Split->Link)\r | |
684 | ) {\r | |
685 | RemoveEntryList (&Split->Link);\r | |
686 | FreePool (Split);\r | |
687 | }\r | |
688 | \r | |
689 | DEBUG_CODE (InitializeListHead (&ShellInfoObject.SplitList.Link););\r | |
a405b86d | 690 | }\r |
691 | \r | |
692 | if (ShellInfoObject.ShellInitSettings.FileName != NULL) {\r | |
693 | FreePool(ShellInfoObject.ShellInitSettings.FileName);\r | |
694 | DEBUG_CODE(ShellInfoObject.ShellInitSettings.FileName = NULL;);\r | |
695 | }\r | |
696 | \r | |
697 | if (ShellInfoObject.ShellInitSettings.FileOptions != NULL) {\r | |
698 | FreePool(ShellInfoObject.ShellInitSettings.FileOptions);\r | |
699 | DEBUG_CODE(ShellInfoObject.ShellInitSettings.FileOptions = NULL;);\r | |
700 | }\r | |
701 | \r | |
702 | if (ShellInfoObject.HiiHandle != NULL) {\r | |
703 | HiiRemovePackages(ShellInfoObject.HiiHandle);\r | |
704 | DEBUG_CODE(ShellInfoObject.HiiHandle = NULL;);\r | |
705 | }\r | |
706 | \r | |
707 | if (!IsListEmpty(&ShellInfoObject.ViewingSettings.CommandHistory.Link)){\r | |
708 | FreeBufferList(&ShellInfoObject.ViewingSettings.CommandHistory);\r | |
709 | }\r | |
710 | \r | |
711 | ASSERT(ShellInfoObject.ConsoleInfo != NULL);\r | |
712 | if (ShellInfoObject.ConsoleInfo != NULL) {\r | |
713 | ConsoleLoggerUninstall(ShellInfoObject.ConsoleInfo);\r | |
714 | FreePool(ShellInfoObject.ConsoleInfo);\r | |
715 | DEBUG_CODE(ShellInfoObject.ConsoleInfo = NULL;);\r | |
716 | }\r | |
717 | \r | |
b62bb885 | 718 | ShellFreeEnvVarList ();\r |
c0bcd343 | 719 | ShellSetRawCmdLine (NULL);\r |
b62bb885 | 720 | \r |
a308e058 RN |
721 | if (ShellCommandGetExit()) {\r |
722 | return ((EFI_STATUS)ShellCommandGetExitCode());\r | |
b6b22b13 | 723 | }\r |
a308e058 | 724 | return (Status);\r |
a405b86d | 725 | }\r |
726 | \r | |
727 | /**\r | |
728 | Sets all the alias' that were registered with the ShellCommandLib library.\r | |
729 | \r | |
d08a5464 | 730 | @retval EFI_SUCCESS all init commands were run successfully.\r |
a405b86d | 731 | **/\r |
732 | EFI_STATUS\r | |
733 | EFIAPI\r | |
734 | SetBuiltInAlias(\r | |
735 | )\r | |
736 | {\r | |
737 | EFI_STATUS Status;\r | |
738 | CONST ALIAS_LIST *List;\r | |
739 | ALIAS_LIST *Node;\r | |
740 | \r | |
741 | //\r | |
742 | // Get all the commands we want to run\r | |
743 | //\r | |
744 | List = ShellCommandGetInitAliasList();\r | |
745 | \r | |
746 | //\r | |
747 | // for each command in the List\r | |
748 | //\r | |
749 | for ( Node = (ALIAS_LIST*)GetFirstNode(&List->Link)\r | |
750 | ; !IsNull (&List->Link, &Node->Link)\r | |
751 | ; Node = (ALIAS_LIST *)GetNextNode(&List->Link, &Node->Link)\r | |
752 | ){\r | |
753 | //\r | |
754 | // install the alias'\r | |
755 | //\r | |
756 | Status = InternalSetAlias(Node->CommandString, Node->Alias, TRUE);\r | |
757 | ASSERT_EFI_ERROR(Status);\r | |
758 | }\r | |
759 | return (EFI_SUCCESS);\r | |
760 | }\r | |
761 | \r | |
762 | /**\r | |
763 | Internal function to determine if 2 command names are really the same.\r | |
764 | \r | |
765 | @param[in] Command1 The pointer to the first command name.\r | |
766 | @param[in] Command2 The pointer to the second command name.\r | |
767 | \r | |
768 | @retval TRUE The 2 command names are the same.\r | |
769 | @retval FALSE The 2 command names are not the same.\r | |
770 | **/\r | |
771 | BOOLEAN\r | |
772 | EFIAPI\r | |
773 | IsCommand(\r | |
774 | IN CONST CHAR16 *Command1,\r | |
775 | IN CONST CHAR16 *Command2\r | |
776 | )\r | |
777 | {\r | |
778 | if (StringNoCaseCompare(&Command1, &Command2) == 0) {\r | |
779 | return (TRUE);\r | |
780 | }\r | |
781 | return (FALSE);\r | |
782 | }\r | |
783 | \r | |
784 | /**\r | |
785 | Internal function to determine if a command is a script only command.\r | |
786 | \r | |
787 | @param[in] CommandName The pointer to the command name.\r | |
788 | \r | |
789 | @retval TRUE The command is a script only command.\r | |
790 | @retval FALSE The command is not a script only command.\r | |
791 | **/\r | |
792 | BOOLEAN\r | |
793 | EFIAPI\r | |
794 | IsScriptOnlyCommand(\r | |
795 | IN CONST CHAR16 *CommandName\r | |
796 | )\r | |
797 | {\r | |
798 | if (IsCommand(CommandName, L"for")\r | |
799 | ||IsCommand(CommandName, L"endfor")\r | |
800 | ||IsCommand(CommandName, L"if")\r | |
801 | ||IsCommand(CommandName, L"else")\r | |
802 | ||IsCommand(CommandName, L"endif")\r | |
803 | ||IsCommand(CommandName, L"goto")) {\r | |
804 | return (TRUE);\r | |
805 | }\r | |
806 | return (FALSE);\r | |
807 | }\r | |
808 | \r | |
a405b86d | 809 | /**\r |
810 | This function will populate the 2 device path protocol parameters based on the\r | |
811 | global gImageHandle. The DevPath will point to the device path for the handle that has\r | |
812 | loaded image protocol installed on it. The FilePath will point to the device path\r | |
813 | for the file that was loaded.\r | |
814 | \r | |
d08a5464 JP |
815 | @param[in, out] DevPath On a successful return the device path to the loaded image.\r |
816 | @param[in, out] FilePath On a successful return the device path to the file.\r | |
a405b86d | 817 | \r |
d08a5464 | 818 | @retval EFI_SUCCESS The 2 device paths were successfully returned.\r |
a405b86d | 819 | @retval other A error from gBS->HandleProtocol.\r |
820 | \r | |
821 | @sa HandleProtocol\r | |
822 | **/\r | |
823 | EFI_STATUS\r | |
824 | EFIAPI\r | |
825 | GetDevicePathsForImageAndFile (\r | |
826 | IN OUT EFI_DEVICE_PATH_PROTOCOL **DevPath,\r | |
827 | IN OUT EFI_DEVICE_PATH_PROTOCOL **FilePath\r | |
828 | )\r | |
829 | {\r | |
830 | EFI_STATUS Status;\r | |
831 | EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;\r | |
832 | EFI_DEVICE_PATH_PROTOCOL *ImageDevicePath;\r | |
833 | \r | |
834 | ASSERT(DevPath != NULL);\r | |
835 | ASSERT(FilePath != NULL);\r | |
836 | \r | |
837 | Status = gBS->OpenProtocol (\r | |
838 | gImageHandle,\r | |
839 | &gEfiLoadedImageProtocolGuid,\r | |
840 | (VOID**)&LoadedImage,\r | |
841 | gImageHandle,\r | |
842 | NULL,\r | |
843 | EFI_OPEN_PROTOCOL_GET_PROTOCOL\r | |
844 | );\r | |
845 | if (!EFI_ERROR (Status)) {\r | |
846 | Status = gBS->OpenProtocol (\r | |
847 | LoadedImage->DeviceHandle,\r | |
848 | &gEfiDevicePathProtocolGuid,\r | |
849 | (VOID**)&ImageDevicePath,\r | |
850 | gImageHandle,\r | |
851 | NULL,\r | |
852 | EFI_OPEN_PROTOCOL_GET_PROTOCOL\r | |
853 | );\r | |
854 | if (!EFI_ERROR (Status)) {\r | |
855 | *DevPath = DuplicateDevicePath (ImageDevicePath);\r | |
856 | *FilePath = DuplicateDevicePath (LoadedImage->FilePath);\r | |
a49f6a2f | 857 | gBS->CloseProtocol(\r |
858 | LoadedImage->DeviceHandle,\r | |
859 | &gEfiDevicePathProtocolGuid,\r | |
860 | gImageHandle,\r | |
861 | NULL);\r | |
a405b86d | 862 | }\r |
863 | gBS->CloseProtocol(\r | |
864 | gImageHandle,\r | |
865 | &gEfiLoadedImageProtocolGuid,\r | |
866 | gImageHandle,\r | |
867 | NULL);\r | |
868 | }\r | |
869 | return (Status);\r | |
870 | }\r | |
871 | \r | |
a405b86d | 872 | /**\r |
873 | Process all Uefi Shell 2.0 command line options.\r | |
874 | \r | |
875 | see Uefi Shell 2.0 section 3.2 for full details.\r | |
876 | \r | |
877 | the command line must resemble the following:\r | |
878 | \r | |
879 | shell.efi [ShellOpt-options] [options] [file-name [file-name-options]]\r | |
880 | \r | |
881 | ShellOpt-options Options which control the initialization behavior of the shell.\r | |
882 | These options are read from the EFI global variable "ShellOpt"\r | |
883 | and are processed before options or file-name.\r | |
884 | \r | |
885 | options Options which control the initialization behavior of the shell.\r | |
886 | \r | |
887 | file-name The name of a UEFI shell application or script to be executed\r | |
888 | after initialization is complete. By default, if file-name is\r | |
889 | specified, then -nostartup is implied. Scripts are not supported\r | |
890 | by level 0.\r | |
891 | \r | |
892 | file-name-options The command-line options that are passed to file-name when it\r | |
893 | is invoked.\r | |
894 | \r | |
895 | This will initialize the ShellInfoObject.ShellInitSettings global variable.\r | |
896 | \r | |
897 | @retval EFI_SUCCESS The variable is initialized.\r | |
898 | **/\r | |
899 | EFI_STATUS\r | |
900 | EFIAPI\r | |
901 | ProcessCommandLine(\r | |
902 | VOID\r | |
903 | )\r | |
904 | {\r | |
23385d63 BJ |
905 | UINTN Size;\r |
906 | UINTN LoopVar;\r | |
907 | CHAR16 *CurrentArg;\r | |
908 | CHAR16 *DelayValueStr;\r | |
909 | UINT64 DelayValue;\r | |
910 | EFI_STATUS Status;\r | |
911 | EFI_UNICODE_COLLATION_PROTOCOL *UnicodeCollation;\r | |
912 | \r | |
913 | // `file-name-options` will contain arguments to `file-name` that we don't\r | |
914 | // know about. This would cause ShellCommandLineParse to error, so we parse\r | |
915 | // arguments manually, ignoring those after the first thing that doesn't look\r | |
916 | // like a shell option (which is assumed to be `file-name`).\r | |
917 | \r | |
918 | Status = gBS->LocateProtocol (\r | |
f3a51e98 | 919 | &gEfiUnicodeCollation2ProtocolGuid,\r |
23385d63 BJ |
920 | NULL,\r |
921 | (VOID **) &UnicodeCollation\r | |
922 | );\r | |
923 | if (EFI_ERROR (Status)) {\r | |
f3a51e98 MH |
924 | Status = gBS->LocateProtocol (\r |
925 | &gEfiUnicodeCollationProtocolGuid,\r | |
926 | NULL,\r | |
927 | (VOID **) &UnicodeCollation\r | |
928 | );\r | |
929 | if (EFI_ERROR (Status)) {\r | |
930 | return Status;\r | |
931 | }\r | |
23385d63 BJ |
932 | }\r |
933 | \r | |
934 | // Set default options\r | |
935 | ShellInfoObject.ShellInitSettings.BitUnion.Bits.Startup = FALSE;\r | |
936 | ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoStartup = FALSE;\r | |
937 | ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoConsoleOut = FALSE;\r | |
938 | ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoConsoleIn = FALSE;\r | |
939 | ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoInterrupt = FALSE;\r | |
940 | ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoMap = FALSE;\r | |
941 | ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoVersion = FALSE;\r | |
942 | ShellInfoObject.ShellInitSettings.BitUnion.Bits.Delay = FALSE;\r | |
943 | ShellInfoObject.ShellInitSettings.BitUnion.Bits.Exit = FALSE;\r | |
dcbdb8bf | 944 | ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoNest = FALSE;\r |
23385d63 BJ |
945 | ShellInfoObject.ShellInitSettings.Delay = 5;\r |
946 | \r | |
e9d19a80 CP |
947 | //\r |
948 | // Start LoopVar at 0 to parse only optional arguments at Argv[0]\r | |
949 | // and parse other parameters from Argv[1]. This is for use case that\r | |
950 | // UEFI Shell boot option is created, and OptionalData is provided\r | |
951 | // that starts with shell command-line options.\r | |
952 | //\r | |
953 | for (LoopVar = 0 ; LoopVar < gEfiShellParametersProtocol->Argc ; LoopVar++) {\r | |
23385d63 BJ |
954 | CurrentArg = gEfiShellParametersProtocol->Argv[LoopVar];\r |
955 | if (UnicodeCollation->StriColl (\r | |
956 | UnicodeCollation,\r | |
957 | L"-startup",\r | |
958 | CurrentArg\r | |
959 | ) == 0) {\r | |
960 | ShellInfoObject.ShellInitSettings.BitUnion.Bits.Startup = TRUE;\r | |
3c865f20 | 961 | }\r |
23385d63 BJ |
962 | else if (UnicodeCollation->StriColl (\r |
963 | UnicodeCollation,\r | |
964 | L"-nostartup",\r | |
965 | CurrentArg\r | |
966 | ) == 0) {\r | |
967 | ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoStartup = TRUE;\r | |
968 | }\r | |
969 | else if (UnicodeCollation->StriColl (\r | |
970 | UnicodeCollation,\r | |
971 | L"-noconsoleout",\r | |
972 | CurrentArg\r | |
973 | ) == 0) {\r | |
974 | ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoConsoleOut = TRUE;\r | |
975 | }\r | |
976 | else if (UnicodeCollation->StriColl (\r | |
977 | UnicodeCollation,\r | |
978 | L"-noconsolein",\r | |
979 | CurrentArg\r | |
980 | ) == 0) {\r | |
981 | ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoConsoleIn = TRUE;\r | |
982 | }\r | |
983 | else if (UnicodeCollation->StriColl (\r | |
984 | UnicodeCollation,\r | |
985 | L"-nointerrupt",\r | |
986 | CurrentArg\r | |
987 | ) == 0) {\r | |
988 | ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoInterrupt = TRUE;\r | |
989 | }\r | |
990 | else if (UnicodeCollation->StriColl (\r | |
991 | UnicodeCollation,\r | |
992 | L"-nomap",\r | |
993 | CurrentArg\r | |
994 | ) == 0) {\r | |
995 | ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoMap = TRUE;\r | |
996 | }\r | |
997 | else if (UnicodeCollation->StriColl (\r | |
998 | UnicodeCollation,\r | |
999 | L"-noversion",\r | |
1000 | CurrentArg\r | |
1001 | ) == 0) {\r | |
1002 | ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoVersion = TRUE;\r | |
1003 | }\r | |
dcbdb8bf QS |
1004 | else if (UnicodeCollation->StriColl (\r |
1005 | UnicodeCollation,\r | |
1006 | L"-nonest",\r | |
1007 | CurrentArg\r | |
1008 | ) == 0) {\r | |
1009 | ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoNest = TRUE;\r | |
1010 | }\r | |
23385d63 BJ |
1011 | else if (UnicodeCollation->StriColl (\r |
1012 | UnicodeCollation,\r | |
1013 | L"-delay",\r | |
1014 | CurrentArg\r | |
1015 | ) == 0) {\r | |
1016 | ShellInfoObject.ShellInitSettings.BitUnion.Bits.Delay = TRUE;\r | |
1017 | // Check for optional delay value following "-delay"\r | |
1018 | DelayValueStr = gEfiShellParametersProtocol->Argv[LoopVar + 1];\r | |
1019 | if (DelayValueStr != NULL){\r | |
1020 | if (*DelayValueStr == L':') {\r | |
1021 | DelayValueStr++;\r | |
1022 | }\r | |
1023 | if (!EFI_ERROR(ShellConvertStringToUint64 (\r | |
1024 | DelayValueStr,\r | |
1025 | &DelayValue,\r | |
1026 | FALSE,\r | |
1027 | FALSE\r | |
1028 | ))) {\r | |
1029 | ShellInfoObject.ShellInitSettings.Delay = (UINTN)DelayValue;\r | |
1030 | LoopVar++;\r | |
1031 | }\r | |
1032 | }\r | |
1033 | } else if (UnicodeCollation->StriColl (\r | |
1034 | UnicodeCollation,\r | |
1035 | L"-_exit",\r | |
1036 | CurrentArg\r | |
1037 | ) == 0) {\r | |
1038 | ShellInfoObject.ShellInitSettings.BitUnion.Bits.Exit = TRUE;\r | |
1039 | } else if (StrnCmp (L"-", CurrentArg, 1) == 0) {\r | |
d08a5464 | 1040 | // Unrecognized option\r |
23385d63 BJ |
1041 | ShellPrintHiiEx(-1, -1, NULL,\r |
1042 | STRING_TOKEN (STR_GEN_PROBLEM),\r | |
1043 | ShellInfoObject.HiiHandle,\r | |
1044 | CurrentArg\r | |
1045 | );\r | |
1046 | return EFI_INVALID_PARAMETER;\r | |
1047 | } else {\r | |
e9d19a80 CP |
1048 | //\r |
1049 | // First argument should be Shell.efi image name\r | |
1050 | //\r | |
1051 | if (LoopVar == 0) {\r | |
1052 | continue;\r | |
1053 | }\r | |
1054 | \r | |
7f79b01e | 1055 | ShellInfoObject.ShellInitSettings.FileName = AllocateCopyPool(StrSize(CurrentArg), CurrentArg);\r |
23385d63 BJ |
1056 | if (ShellInfoObject.ShellInitSettings.FileName == NULL) {\r |
1057 | return (EFI_OUT_OF_RESOURCES);\r | |
1058 | }\r | |
1059 | //\r | |
1060 | // We found `file-name`.\r | |
1061 | //\r | |
1062 | ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoStartup = 1;\r | |
23385d63 BJ |
1063 | LoopVar++;\r |
1064 | \r | |
1065 | // Add `file-name-options`\r | |
ba71f790 | 1066 | for (Size = 0 ; LoopVar < gEfiShellParametersProtocol->Argc ; LoopVar++) {\r |
23385d63 BJ |
1067 | ASSERT((ShellInfoObject.ShellInitSettings.FileOptions == NULL && Size == 0) || (ShellInfoObject.ShellInitSettings.FileOptions != NULL));\r |
1068 | StrnCatGrow(&ShellInfoObject.ShellInitSettings.FileOptions,\r | |
1069 | &Size,\r | |
1070 | L" ",\r | |
1071 | 0);\r | |
1072 | if (ShellInfoObject.ShellInitSettings.FileOptions == NULL) {\r | |
1073 | SHELL_FREE_NON_NULL(ShellInfoObject.ShellInitSettings.FileName);\r | |
1074 | return (EFI_OUT_OF_RESOURCES);\r | |
1075 | }\r | |
1076 | StrnCatGrow(&ShellInfoObject.ShellInitSettings.FileOptions,\r | |
1077 | &Size,\r | |
1078 | gEfiShellParametersProtocol->Argv[LoopVar],\r | |
1079 | 0);\r | |
1080 | if (ShellInfoObject.ShellInitSettings.FileOptions == NULL) {\r | |
1081 | SHELL_FREE_NON_NULL(ShellInfoObject.ShellInitSettings.FileName);\r | |
1082 | return (EFI_OUT_OF_RESOURCES);\r | |
a405b86d | 1083 | }\r |
1084 | }\r | |
a405b86d | 1085 | }\r |
1086 | }\r | |
1087 | \r | |
23385d63 | 1088 | // "-nointerrupt" overrides "-delay"\r |
733f138d | 1089 | if (ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoInterrupt) {\r |
1090 | ShellInfoObject.ShellInitSettings.Delay = 0;\r | |
a405b86d | 1091 | }\r |
a405b86d | 1092 | \r |
23385d63 | 1093 | return EFI_SUCCESS;\r |
a405b86d | 1094 | }\r |
1095 | \r | |
1096 | /**\r | |
1097 | Handles all interaction with the default startup script.\r | |
1098 | \r | |
1099 | this will check that the correct command line parameters were passed, handle the delay, and then start running the script.\r | |
1100 | \r | |
1101 | @param ImagePath the path to the image for shell. first place to look for the startup script\r | |
1102 | @param FilePath the path to the file for shell. second place to look for the startup script.\r | |
1103 | \r | |
1104 | @retval EFI_SUCCESS the variable is initialized.\r | |
1105 | **/\r | |
1106 | EFI_STATUS\r | |
1107 | EFIAPI\r | |
1108 | DoStartupScript(\r | |
a308e058 RN |
1109 | IN EFI_DEVICE_PATH_PROTOCOL *ImagePath,\r |
1110 | IN EFI_DEVICE_PATH_PROTOCOL *FilePath\r | |
a405b86d | 1111 | )\r |
1112 | {\r | |
1113 | EFI_STATUS Status;\r | |
490ce43d | 1114 | EFI_STATUS CalleeStatus;\r |
a405b86d | 1115 | UINTN Delay;\r |
1116 | EFI_INPUT_KEY Key;\r | |
1117 | SHELL_FILE_HANDLE FileHandle;\r | |
1118 | EFI_DEVICE_PATH_PROTOCOL *NewPath;\r | |
1119 | EFI_DEVICE_PATH_PROTOCOL *NamePath;\r | |
1120 | CHAR16 *FileStringPath;\r | |
733f138d | 1121 | CHAR16 *TempSpot;\r |
a405b86d | 1122 | UINTN NewSize;\r |
733f138d | 1123 | CONST CHAR16 *MapName;\r |
a405b86d | 1124 | \r |
1125 | Key.UnicodeChar = CHAR_NULL;\r | |
1126 | Key.ScanCode = 0;\r | |
1127 | FileHandle = NULL;\r | |
1128 | \r | |
1129 | if (!ShellInfoObject.ShellInitSettings.BitUnion.Bits.Startup && ShellInfoObject.ShellInitSettings.FileName != NULL) {\r | |
1130 | //\r | |
1131 | // launch something else instead\r | |
1132 | //\r | |
1133 | NewSize = StrSize(ShellInfoObject.ShellInitSettings.FileName);\r | |
1134 | if (ShellInfoObject.ShellInitSettings.FileOptions != NULL) {\r | |
1135 | NewSize += StrSize(ShellInfoObject.ShellInitSettings.FileOptions) + sizeof(CHAR16);\r | |
1136 | }\r | |
1137 | FileStringPath = AllocateZeroPool(NewSize);\r | |
3c865f20 | 1138 | if (FileStringPath == NULL) {\r |
1139 | return (EFI_OUT_OF_RESOURCES);\r | |
1140 | }\r | |
e75390f0 | 1141 | StrCpyS(FileStringPath, NewSize/sizeof(CHAR16), ShellInfoObject.ShellInitSettings.FileName);\r |
a405b86d | 1142 | if (ShellInfoObject.ShellInitSettings.FileOptions != NULL) {\r |
e75390f0 QS |
1143 | StrnCatS(FileStringPath, NewSize/sizeof(CHAR16), L" ", NewSize/sizeof(CHAR16) - StrLen(FileStringPath) -1);\r |
1144 | StrnCatS(FileStringPath, NewSize/sizeof(CHAR16), ShellInfoObject.ShellInitSettings.FileOptions, NewSize/sizeof(CHAR16) - StrLen(FileStringPath) -1);\r | |
a405b86d | 1145 | }\r |
490ce43d QS |
1146 | Status = RunShellCommand(FileStringPath, &CalleeStatus);\r |
1147 | if (ShellInfoObject.ShellInitSettings.BitUnion.Bits.Exit == TRUE) {\r | |
01e3a976 | 1148 | ShellCommandRegisterExit(gEfiShellProtocol->BatchIsActive(), (UINT64)CalleeStatus);\r |
490ce43d | 1149 | }\r |
a405b86d | 1150 | FreePool(FileStringPath);\r |
1151 | return (Status);\r | |
1152 | \r | |
1153 | }\r | |
1154 | \r | |
1155 | //\r | |
1156 | // for shell level 0 we do no scripts\r | |
1157 | // Without the Startup bit overriding we allow for nostartup to prevent scripts\r | |
1158 | //\r | |
1159 | if ( (PcdGet8(PcdShellSupportLevel) < 1)\r | |
1160 | || (ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoStartup && !ShellInfoObject.ShellInitSettings.BitUnion.Bits.Startup)\r | |
1161 | ){\r | |
1162 | return (EFI_SUCCESS);\r | |
1163 | }\r | |
1164 | \r | |
5559a41a | 1165 | gST->ConOut->EnableCursor(gST->ConOut, FALSE);\r |
a405b86d | 1166 | //\r |
1167 | // print out our warning and see if they press a key\r | |
1168 | //\r | |
284e034f | 1169 | for ( Status = EFI_UNSUPPORTED, Delay = ShellInfoObject.ShellInitSettings.Delay\r |
733f138d | 1170 | ; Delay != 0 && EFI_ERROR(Status)\r |
a405b86d | 1171 | ; Delay--\r |
1172 | ){\r | |
284e034f CP |
1173 | ShellPrintHiiEx(0, gST->ConOut->Mode->CursorRow, NULL, STRING_TOKEN (STR_SHELL_STARTUP_QUESTION), ShellInfoObject.HiiHandle, Delay);\r |
1174 | gBS->Stall (1000000);\r | |
733f138d | 1175 | if (!ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoConsoleIn) {\r |
1176 | Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);\r | |
1177 | }\r | |
a405b86d | 1178 | }\r |
1179 | ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_CRLF), ShellInfoObject.HiiHandle);\r | |
5559a41a | 1180 | gST->ConOut->EnableCursor(gST->ConOut, TRUE);\r |
a405b86d | 1181 | \r |
1182 | //\r | |
1183 | // ESC was pressed\r | |
1184 | //\r | |
1185 | if (Status == EFI_SUCCESS && Key.UnicodeChar == 0 && Key.ScanCode == SCAN_ESC) {\r | |
1186 | return (EFI_SUCCESS);\r | |
1187 | }\r | |
1188 | \r | |
a405b86d | 1189 | //\r |
733f138d | 1190 | // Try the first location (must be file system)\r |
a405b86d | 1191 | //\r |
733f138d | 1192 | MapName = ShellInfoObject.NewEfiShellProtocol->GetMapFromDevicePath(&ImagePath);\r |
1193 | if (MapName != NULL) {\r | |
1194 | FileStringPath = NULL;\r | |
1195 | NewSize = 0;\r | |
1196 | FileStringPath = StrnCatGrow(&FileStringPath, &NewSize, MapName, 0);\r | |
532691c8 | 1197 | if (FileStringPath == NULL) {\r |
1198 | Status = EFI_OUT_OF_RESOURCES;\r | |
1199 | } else {\r | |
1200 | TempSpot = StrStr(FileStringPath, L";");\r | |
1201 | if (TempSpot != NULL) {\r | |
1202 | *TempSpot = CHAR_NULL;\r | |
1203 | }\r | |
1204 | FileStringPath = StrnCatGrow(&FileStringPath, &NewSize, ((FILEPATH_DEVICE_PATH*)FilePath)->PathName, 0);\r | |
1205 | PathRemoveLastItem(FileStringPath);\r | |
1206 | FileStringPath = StrnCatGrow(&FileStringPath, &NewSize, mStartupScript, 0);\r | |
1207 | Status = ShellInfoObject.NewEfiShellProtocol->OpenFileByName(FileStringPath, &FileHandle, EFI_FILE_MODE_READ);\r | |
1208 | FreePool(FileStringPath);\r | |
733f138d | 1209 | }\r |
733f138d | 1210 | }\r |
a405b86d | 1211 | if (EFI_ERROR(Status)) {\r |
733f138d | 1212 | NamePath = FileDevicePath (NULL, mStartupScript);\r |
1213 | NewPath = AppendDevicePathNode (ImagePath, NamePath);\r | |
1214 | FreePool(NamePath);\r | |
1215 | \r | |
a405b86d | 1216 | //\r |
733f138d | 1217 | // Try the location\r |
a405b86d | 1218 | //\r |
a405b86d | 1219 | Status = InternalOpenFileDevicePath(NewPath, &FileHandle, EFI_FILE_MODE_READ, 0);\r |
733f138d | 1220 | FreePool(NewPath);\r |
a405b86d | 1221 | }\r |
a405b86d | 1222 | //\r |
1223 | // If we got a file, run it\r | |
1224 | //\r | |
733f138d | 1225 | if (!EFI_ERROR(Status) && FileHandle != NULL) {\r |
a308e058 | 1226 | Status = RunScriptFile (mStartupScript, FileHandle, L"", ShellInfoObject.NewShellParametersProtocol);\r |
a405b86d | 1227 | ShellInfoObject.NewEfiShellProtocol->CloseFile(FileHandle);\r |
1228 | } else {\r | |
3c865f20 | 1229 | FileStringPath = ShellFindFilePath(mStartupScript);\r |
1230 | if (FileStringPath == NULL) {\r | |
1231 | //\r | |
d08a5464 | 1232 | // we return success since we don't need to have a startup script\r |
3c865f20 | 1233 | //\r |
1234 | Status = EFI_SUCCESS;\r | |
1235 | ASSERT(FileHandle == NULL);\r | |
1236 | } else {\r | |
a308e058 | 1237 | Status = RunScriptFile(FileStringPath, NULL, L"", ShellInfoObject.NewShellParametersProtocol);\r |
3c865f20 | 1238 | FreePool(FileStringPath);\r |
1239 | }\r | |
a405b86d | 1240 | }\r |
1241 | \r | |
a405b86d | 1242 | \r |
1243 | return (Status);\r | |
1244 | }\r | |
1245 | \r | |
1246 | /**\r | |
1247 | Function to perform the shell prompt looping. It will do a single prompt,\r | |
1248 | dispatch the result, and then return. It is expected that the caller will\r | |
1249 | call this function in a loop many times.\r | |
1250 | \r | |
1251 | @retval EFI_SUCCESS\r | |
1252 | @retval RETURN_ABORTED\r | |
1253 | **/\r | |
1254 | EFI_STATUS\r | |
1255 | EFIAPI\r | |
1256 | DoShellPrompt (\r | |
1257 | VOID\r | |
1258 | )\r | |
1259 | {\r | |
1260 | UINTN Column;\r | |
1261 | UINTN Row;\r | |
1262 | CHAR16 *CmdLine;\r | |
1263 | CONST CHAR16 *CurDir;\r | |
1264 | UINTN BufferSize;\r | |
1265 | EFI_STATUS Status;\r | |
594e780b | 1266 | LIST_ENTRY OldBufferList;\r |
a405b86d | 1267 | \r |
1268 | CurDir = NULL;\r | |
1269 | \r | |
1270 | //\r | |
1271 | // Get screen setting to decide size of the command line buffer\r | |
1272 | //\r | |
1273 | gST->ConOut->QueryMode (gST->ConOut, gST->ConOut->Mode->Mode, &Column, &Row);\r | |
1274 | BufferSize = Column * Row * sizeof (CHAR16);\r | |
1275 | CmdLine = AllocateZeroPool (BufferSize);\r | |
1276 | if (CmdLine == NULL) {\r | |
1277 | return EFI_OUT_OF_RESOURCES;\r | |
1278 | }\r | |
1279 | \r | |
594e780b | 1280 | SaveBufferList(&OldBufferList);\r |
a405b86d | 1281 | CurDir = ShellInfoObject.NewEfiShellProtocol->GetEnv(L"cwd");\r |
1282 | \r | |
1283 | //\r | |
1284 | // Prompt for input\r | |
1285 | //\r | |
1286 | gST->ConOut->SetCursorPosition (gST->ConOut, 0, gST->ConOut->Mode->CursorRow);\r | |
1287 | \r | |
1288 | if (CurDir != NULL && StrLen(CurDir) > 1) {\r | |
1289 | ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_CURDIR), ShellInfoObject.HiiHandle, CurDir);\r | |
1290 | } else {\r | |
1291 | ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_SHELL), ShellInfoObject.HiiHandle);\r | |
1292 | }\r | |
1293 | \r | |
1294 | //\r | |
1295 | // Read a line from the console\r | |
1296 | //\r | |
1297 | Status = ShellInfoObject.NewEfiShellProtocol->ReadFile(ShellInfoObject.NewShellParametersProtocol->StdIn, &BufferSize, CmdLine);\r | |
1298 | \r | |
1299 | //\r | |
1300 | // Null terminate the string and parse it\r | |
1301 | //\r | |
1302 | if (!EFI_ERROR (Status)) {\r | |
1303 | CmdLine[BufferSize / sizeof (CHAR16)] = CHAR_NULL;\r | |
a308e058 | 1304 | Status = RunCommand(CmdLine);\r |
4922715d | 1305 | }\r |
a405b86d | 1306 | \r |
1307 | //\r | |
1308 | // Done with this command\r | |
1309 | //\r | |
594e780b | 1310 | RestoreBufferList(&OldBufferList);\r |
a405b86d | 1311 | FreePool (CmdLine);\r |
1312 | return Status;\r | |
1313 | }\r | |
1314 | \r | |
1315 | /**\r | |
1316 | Add a buffer to the Buffer To Free List for safely returning buffers to other\r | |
1317 | places without risking letting them modify internal shell information.\r | |
1318 | \r | |
1319 | @param Buffer Something to pass to FreePool when the shell is exiting.\r | |
1320 | **/\r | |
1321 | VOID*\r | |
1322 | EFIAPI\r | |
b2c036a7 | 1323 | AddBufferToFreeList (\r |
a405b86d | 1324 | VOID *Buffer\r |
1325 | )\r | |
1326 | {\r | |
1327 | BUFFER_LIST *BufferListEntry;\r | |
1328 | \r | |
1329 | if (Buffer == NULL) {\r | |
1330 | return (NULL);\r | |
1331 | }\r | |
1332 | \r | |
b2c036a7 RN |
1333 | BufferListEntry = AllocateZeroPool (sizeof (BUFFER_LIST));\r |
1334 | if (BufferListEntry == NULL) {\r | |
1335 | return NULL;\r | |
1336 | }\r | |
1337 | \r | |
a405b86d | 1338 | BufferListEntry->Buffer = Buffer;\r |
b2c036a7 | 1339 | InsertTailList (&ShellInfoObject.BufferToFreeList.Link, &BufferListEntry->Link);\r |
a405b86d | 1340 | return (Buffer);\r |
1341 | }\r | |
1342 | \r | |
14810d6b QS |
1343 | \r |
1344 | /**\r | |
1345 | Create a new buffer list and stores the old one to OldBufferList \r | |
1346 | \r | |
1347 | @param OldBufferList The temporary list head used to store the nodes in BufferToFreeList.\r | |
1348 | **/\r | |
1349 | VOID\r | |
1350 | SaveBufferList (\r | |
1351 | OUT LIST_ENTRY *OldBufferList\r | |
1352 | )\r | |
1353 | {\r | |
1354 | CopyMem (OldBufferList, &ShellInfoObject.BufferToFreeList.Link, sizeof (LIST_ENTRY));\r | |
1355 | InitializeListHead (&ShellInfoObject.BufferToFreeList.Link);\r | |
1356 | }\r | |
1357 | \r | |
1358 | /**\r | |
1359 | Restore previous nodes into BufferToFreeList .\r | |
1360 | \r | |
1361 | @param OldBufferList The temporary list head used to store the nodes in BufferToFreeList.\r | |
1362 | **/\r | |
1363 | VOID\r | |
1364 | RestoreBufferList (\r | |
1365 | IN OUT LIST_ENTRY *OldBufferList\r | |
1366 | )\r | |
1367 | {\r | |
1368 | FreeBufferList (&ShellInfoObject.BufferToFreeList);\r | |
1369 | CopyMem (&ShellInfoObject.BufferToFreeList.Link, OldBufferList, sizeof (LIST_ENTRY));\r | |
1370 | }\r | |
1371 | \r | |
1372 | \r | |
a405b86d | 1373 | /**\r |
1374 | Add a buffer to the Line History List\r | |
1375 | \r | |
1376 | @param Buffer The line buffer to add.\r | |
1377 | **/\r | |
1378 | VOID\r | |
1379 | EFIAPI\r | |
1380 | AddLineToCommandHistory(\r | |
1381 | IN CONST CHAR16 *Buffer\r | |
1382 | )\r | |
1383 | {\r | |
1384 | BUFFER_LIST *Node;\r | |
a4f138a4 QS |
1385 | BUFFER_LIST *Walker;\r |
1386 | UINT16 MaxHistoryCmdCount;\r | |
1387 | UINT16 Count;\r | |
dcbdb8bf | 1388 | \r |
a4f138a4 QS |
1389 | Count = 0;\r |
1390 | MaxHistoryCmdCount = PcdGet16(PcdShellMaxHistoryCommandCount);\r | |
1391 | \r | |
1392 | if (MaxHistoryCmdCount == 0) {\r | |
1393 | return ;\r | |
1394 | }\r | |
1395 | \r | |
a405b86d | 1396 | \r |
1397 | Node = AllocateZeroPool(sizeof(BUFFER_LIST));\r | |
b2c036a7 RN |
1398 | if (Node == NULL) {\r |
1399 | return;\r | |
1400 | }\r | |
1401 | \r | |
1402 | Node->Buffer = AllocateCopyPool (StrSize (Buffer), Buffer);\r | |
1403 | if (Node->Buffer == NULL) {\r | |
1404 | FreePool (Node);\r | |
1405 | return;\r | |
1406 | }\r | |
a405b86d | 1407 | \r |
a4f138a4 QS |
1408 | for ( Walker = (BUFFER_LIST*)GetFirstNode(&ShellInfoObject.ViewingSettings.CommandHistory.Link)\r |
1409 | ; !IsNull(&ShellInfoObject.ViewingSettings.CommandHistory.Link, &Walker->Link)\r | |
1410 | ; Walker = (BUFFER_LIST*)GetNextNode(&ShellInfoObject.ViewingSettings.CommandHistory.Link, &Walker->Link)\r | |
1411 | ){\r | |
1412 | Count++;\r | |
1413 | }\r | |
1414 | if (Count < MaxHistoryCmdCount){\r | |
1415 | InsertTailList(&ShellInfoObject.ViewingSettings.CommandHistory.Link, &Node->Link);\r | |
1416 | } else {\r | |
1417 | Walker = (BUFFER_LIST*)GetFirstNode(&ShellInfoObject.ViewingSettings.CommandHistory.Link);\r | |
1418 | RemoveEntryList(&Walker->Link);\r | |
1419 | if (Walker->Buffer != NULL) {\r | |
1420 | FreePool(Walker->Buffer);\r | |
1421 | }\r | |
1422 | FreePool(Walker);\r | |
1423 | InsertTailList(&ShellInfoObject.ViewingSettings.CommandHistory.Link, &Node->Link);\r | |
1424 | }\r | |
a405b86d | 1425 | }\r |
1426 | \r | |
1427 | /**\r | |
1428 | Checks if a string is an alias for another command. If yes, then it replaces the alias name\r | |
1429 | with the correct command name.\r | |
1430 | \r | |
4ff7e37b ED |
1431 | @param[in, out] CommandString Upon entry the potential alias. Upon return the\r |
1432 | command name if it was an alias. If it was not\r | |
1433 | an alias it will be unchanged. This function may\r | |
1434 | change the buffer to fit the command name.\r | |
a405b86d | 1435 | \r |
1436 | @retval EFI_SUCCESS The name was changed.\r | |
1437 | @retval EFI_SUCCESS The name was not an alias.\r | |
1438 | @retval EFI_OUT_OF_RESOURCES A memory allocation failed.\r | |
1439 | **/\r | |
1440 | EFI_STATUS\r | |
1441 | EFIAPI\r | |
1442 | ShellConvertAlias(\r | |
1443 | IN OUT CHAR16 **CommandString\r | |
1444 | )\r | |
1445 | {\r | |
1446 | CONST CHAR16 *NewString;\r | |
1447 | \r | |
1448 | NewString = ShellInfoObject.NewEfiShellProtocol->GetAlias(*CommandString, NULL);\r | |
1449 | if (NewString == NULL) {\r | |
1450 | return (EFI_SUCCESS);\r | |
1451 | }\r | |
1452 | FreePool(*CommandString);\r | |
7f79b01e | 1453 | *CommandString = AllocateCopyPool(StrSize(NewString), NewString);\r |
a405b86d | 1454 | if (*CommandString == NULL) {\r |
1455 | return (EFI_OUT_OF_RESOURCES);\r | |
1456 | }\r | |
a405b86d | 1457 | return (EFI_SUCCESS);\r |
1458 | }\r | |
1459 | \r | |
d5b5440b JC |
1460 | /**\r |
1461 | This function will eliminate unreplaced (and therefore non-found) environment variables.\r | |
1462 | \r | |
1463 | @param[in,out] CmdLine The command line to update.\r | |
1464 | **/\r | |
1465 | EFI_STATUS\r | |
1466 | EFIAPI\r | |
1467 | StripUnreplacedEnvironmentVariables(\r | |
1468 | IN OUT CHAR16 *CmdLine\r | |
1469 | )\r | |
1470 | {\r | |
1471 | CHAR16 *FirstPercent;\r | |
1472 | CHAR16 *FirstQuote;\r | |
1473 | CHAR16 *SecondPercent;\r | |
1474 | CHAR16 *SecondQuote;\r | |
1475 | CHAR16 *CurrentLocator;\r | |
1476 | \r | |
1477 | for (CurrentLocator = CmdLine ; CurrentLocator != NULL ; ) {\r | |
a95cf8f0 JC |
1478 | FirstQuote = FindNextInstance(CurrentLocator, L"\"", TRUE);\r |
1479 | FirstPercent = FindNextInstance(CurrentLocator, L"%", TRUE);\r | |
1480 | SecondPercent = FirstPercent!=NULL?FindNextInstance(FirstPercent+1, L"%", TRUE):NULL;\r | |
d5b5440b JC |
1481 | if (FirstPercent == NULL || SecondPercent == NULL) {\r |
1482 | //\r | |
d08a5464 | 1483 | // If we ever don't have 2 % we are done.\r |
d5b5440b JC |
1484 | //\r |
1485 | break;\r | |
1486 | }\r | |
1487 | \r | |
fdd52bde | 1488 | if (FirstQuote!= NULL && FirstQuote < FirstPercent) {\r |
a95cf8f0 | 1489 | SecondQuote = FindNextInstance(FirstQuote+1, L"\"", TRUE);\r |
d5b5440b JC |
1490 | //\r |
1491 | // Quote is first found\r | |
1492 | //\r | |
a95cf8f0 | 1493 | \r |
d5b5440b JC |
1494 | if (SecondQuote < FirstPercent) {\r |
1495 | //\r | |
1496 | // restart after the pair of "\r | |
1497 | //\r | |
1498 | CurrentLocator = SecondQuote + 1;\r | |
1499 | } else /* FirstPercent < SecondQuote */{\r | |
1500 | //\r | |
1501 | // Restart on the first percent\r | |
1502 | //\r | |
1503 | CurrentLocator = FirstPercent;\r | |
1504 | }\r | |
1505 | continue;\r | |
1506 | }\r | |
fdd52bde | 1507 | \r |
fe8ec3dd | 1508 | if (FirstQuote == NULL || SecondPercent < FirstQuote) {\r |
42435671 QS |
1509 | if (IsValidEnvironmentVariableName(FirstPercent, SecondPercent)) {\r |
1510 | //\r | |
1511 | // We need to remove from FirstPercent to SecondPercent\r | |
1512 | //\r | |
1513 | CopyMem(FirstPercent, SecondPercent + 1, StrSize(SecondPercent + 1));\r | |
1514 | //\r | |
d08a5464 | 1515 | // don't need to update the locator. both % characters are gone.\r |
42435671 QS |
1516 | //\r |
1517 | } else {\r | |
1518 | CurrentLocator = SecondPercent + 1;\r | |
1519 | }\r | |
d5b5440b JC |
1520 | continue;\r |
1521 | }\r | |
d5b5440b JC |
1522 | CurrentLocator = FirstQuote;\r |
1523 | }\r | |
1524 | return (EFI_SUCCESS);\r | |
1525 | }\r | |
1526 | \r | |
a405b86d | 1527 | /**\r |
1528 | Function allocates a new command line and replaces all instances of environment\r | |
1529 | variable names that are correctly preset to their values.\r | |
1530 | \r | |
1531 | If the return value is not NULL the memory must be caller freed.\r | |
1532 | \r | |
1533 | @param[in] OriginalCommandLine The original command line\r | |
1534 | \r | |
d08a5464 | 1535 | @retval NULL An error occurred.\r |
a405b86d | 1536 | @return The new command line with no environment variables present.\r |
1537 | **/\r | |
1538 | CHAR16*\r | |
1539 | EFIAPI\r | |
1540 | ShellConvertVariables (\r | |
1541 | IN CONST CHAR16 *OriginalCommandLine\r | |
1542 | )\r | |
1543 | {\r | |
1544 | CONST CHAR16 *MasterEnvList;\r | |
1545 | UINTN NewSize;\r | |
1546 | CHAR16 *NewCommandLine1;\r | |
1547 | CHAR16 *NewCommandLine2;\r | |
1548 | CHAR16 *Temp;\r | |
1549 | UINTN ItemSize;\r | |
1550 | CHAR16 *ItemTemp;\r | |
1551 | SCRIPT_FILE *CurrentScriptFile;\r | |
1552 | ALIAS_LIST *AliasListNode;\r | |
1553 | \r | |
1554 | ASSERT(OriginalCommandLine != NULL);\r | |
1555 | \r | |
1556 | ItemSize = 0;\r | |
1557 | NewSize = StrSize(OriginalCommandLine);\r | |
1558 | CurrentScriptFile = ShellCommandGetCurrentScriptFile();\r | |
1559 | Temp = NULL;\r | |
1560 | \r | |
1561 | ///@todo update this to handle the %0 - %9 for scripting only (borrow from line 1256 area) ? ? ?\r | |
1562 | \r | |
1563 | //\r | |
1564 | // calculate the size required for the post-conversion string...\r | |
1565 | //\r | |
1566 | if (CurrentScriptFile != NULL) {\r | |
1567 | for (AliasListNode = (ALIAS_LIST*)GetFirstNode(&CurrentScriptFile->SubstList)\r | |
1568 | ; !IsNull(&CurrentScriptFile->SubstList, &AliasListNode->Link)\r | |
1569 | ; AliasListNode = (ALIAS_LIST*)GetNextNode(&CurrentScriptFile->SubstList, &AliasListNode->Link)\r | |
1570 | ){\r | |
1571 | for (Temp = StrStr(OriginalCommandLine, AliasListNode->Alias)\r | |
1572 | ; Temp != NULL\r | |
1573 | ; Temp = StrStr(Temp+1, AliasListNode->Alias)\r | |
1574 | ){\r | |
1575 | //\r | |
d08a5464 | 1576 | // we need a preceding and if there is space no ^ preceding (if no space ignore)\r |
a405b86d | 1577 | //\r |
1578 | if ((((Temp-OriginalCommandLine)>2) && *(Temp-2) != L'^') || ((Temp-OriginalCommandLine)<=2)) {\r | |
1579 | NewSize += StrSize(AliasListNode->CommandString);\r | |
1580 | }\r | |
1581 | }\r | |
1582 | }\r | |
1583 | }\r | |
1584 | \r | |
1585 | for (MasterEnvList = EfiShellGetEnv(NULL)\r | |
1586 | ; MasterEnvList != NULL && *MasterEnvList != CHAR_NULL //&& *(MasterEnvList+1) != CHAR_NULL\r | |
1587 | ; MasterEnvList += StrLen(MasterEnvList) + 1\r | |
1588 | ){\r | |
1589 | if (StrSize(MasterEnvList) > ItemSize) {\r | |
1590 | ItemSize = StrSize(MasterEnvList);\r | |
1591 | }\r | |
1592 | for (Temp = StrStr(OriginalCommandLine, MasterEnvList)\r | |
1593 | ; Temp != NULL\r | |
1594 | ; Temp = StrStr(Temp+1, MasterEnvList)\r | |
1595 | ){\r | |
1596 | //\r | |
d08a5464 | 1597 | // we need a preceding and following % and if there is space no ^ preceding (if no space ignore)\r |
a405b86d | 1598 | //\r |
1599 | if (*(Temp-1) == L'%' && *(Temp+StrLen(MasterEnvList)) == L'%' &&\r | |
1600 | ((((Temp-OriginalCommandLine)>2) && *(Temp-2) != L'^') || ((Temp-OriginalCommandLine)<=2))) {\r | |
1601 | NewSize+=StrSize(EfiShellGetEnv(MasterEnvList));\r | |
1602 | }\r | |
1603 | }\r | |
1604 | }\r | |
1605 | \r | |
a405b86d | 1606 | //\r |
1607 | // now do the replacements...\r | |
1608 | //\r | |
7f79b01e | 1609 | NewCommandLine1 = AllocateCopyPool(NewSize, OriginalCommandLine);\r |
a405b86d | 1610 | NewCommandLine2 = AllocateZeroPool(NewSize);\r |
1611 | ItemTemp = AllocateZeroPool(ItemSize+(2*sizeof(CHAR16)));\r | |
3c865f20 | 1612 | if (NewCommandLine1 == NULL || NewCommandLine2 == NULL || ItemTemp == NULL) {\r |
1613 | SHELL_FREE_NON_NULL(NewCommandLine1);\r | |
1614 | SHELL_FREE_NON_NULL(NewCommandLine2);\r | |
1615 | SHELL_FREE_NON_NULL(ItemTemp);\r | |
1616 | return (NULL);\r | |
1617 | }\r | |
a405b86d | 1618 | for (MasterEnvList = EfiShellGetEnv(NULL)\r |
7f79b01e | 1619 | ; MasterEnvList != NULL && *MasterEnvList != CHAR_NULL\r |
a405b86d | 1620 | ; MasterEnvList += StrLen(MasterEnvList) + 1\r |
1621 | ){\r | |
e75390f0 QS |
1622 | StrCpyS( ItemTemp, \r |
1623 | ((ItemSize+(2*sizeof(CHAR16)))/sizeof(CHAR16)), \r | |
1624 | L"%"\r | |
1625 | );\r | |
1626 | StrCatS( ItemTemp, \r | |
1627 | ((ItemSize+(2*sizeof(CHAR16)))/sizeof(CHAR16)), \r | |
1628 | MasterEnvList\r | |
1629 | );\r | |
1630 | StrCatS( ItemTemp, \r | |
1631 | ((ItemSize+(2*sizeof(CHAR16)))/sizeof(CHAR16)), \r | |
1632 | L"%"\r | |
1633 | );\r | |
a405b86d | 1634 | ShellCopySearchAndReplace(NewCommandLine1, NewCommandLine2, NewSize, ItemTemp, EfiShellGetEnv(MasterEnvList), TRUE, FALSE);\r |
e75390f0 | 1635 | StrCpyS(NewCommandLine1, NewSize/sizeof(CHAR16), NewCommandLine2);\r |
a405b86d | 1636 | }\r |
1637 | if (CurrentScriptFile != NULL) {\r | |
1638 | for (AliasListNode = (ALIAS_LIST*)GetFirstNode(&CurrentScriptFile->SubstList)\r | |
1639 | ; !IsNull(&CurrentScriptFile->SubstList, &AliasListNode->Link)\r | |
1640 | ; AliasListNode = (ALIAS_LIST*)GetNextNode(&CurrentScriptFile->SubstList, &AliasListNode->Link)\r | |
1641 | ){\r | |
1642 | ShellCopySearchAndReplace(NewCommandLine1, NewCommandLine2, NewSize, AliasListNode->Alias, AliasListNode->CommandString, TRUE, FALSE);\r | |
e75390f0 | 1643 | StrCpyS(NewCommandLine1, NewSize/sizeof(CHAR16), NewCommandLine2);\r |
a405b86d | 1644 | }\r |
1645 | }\r | |
1646 | \r | |
fdd52bde | 1647 | //\r |
d08a5464 | 1648 | // Remove non-existent environment variables\r |
fdd52bde JC |
1649 | //\r |
1650 | StripUnreplacedEnvironmentVariables(NewCommandLine1);\r | |
1651 | \r | |
df07baea JC |
1652 | //\r |
1653 | // Now cleanup any straggler intentionally ignored "%" characters\r | |
1654 | //\r | |
1655 | ShellCopySearchAndReplace(NewCommandLine1, NewCommandLine2, NewSize, L"^%", L"%", TRUE, FALSE);\r | |
e75390f0 | 1656 | StrCpyS(NewCommandLine1, NewSize/sizeof(CHAR16), NewCommandLine2);\r |
df07baea | 1657 | \r |
a405b86d | 1658 | FreePool(NewCommandLine2);\r |
1659 | FreePool(ItemTemp);\r | |
1660 | \r | |
1661 | return (NewCommandLine1);\r | |
1662 | }\r | |
1663 | \r | |
1664 | /**\r | |
1665 | Internal function to run a command line with pipe usage.\r | |
1666 | \r | |
1667 | @param[in] CmdLine The pointer to the command line.\r | |
1668 | @param[in] StdIn The pointer to the Standard input.\r | |
1669 | @param[in] StdOut The pointer to the Standard output.\r | |
1670 | \r | |
1671 | @retval EFI_SUCCESS The split command is executed successfully.\r | |
1672 | @retval other Some error occurs when executing the split command.\r | |
1673 | **/\r | |
1674 | EFI_STATUS\r | |
1675 | EFIAPI\r | |
1676 | RunSplitCommand(\r | |
1677 | IN CONST CHAR16 *CmdLine,\r | |
1678 | IN SHELL_FILE_HANDLE *StdIn,\r | |
a308e058 | 1679 | IN SHELL_FILE_HANDLE *StdOut\r |
a405b86d | 1680 | )\r |
1681 | {\r | |
1682 | EFI_STATUS Status;\r | |
1683 | CHAR16 *NextCommandLine;\r | |
1684 | CHAR16 *OurCommandLine;\r | |
1685 | UINTN Size1;\r | |
1686 | UINTN Size2;\r | |
1687 | SPLIT_LIST *Split;\r | |
1688 | SHELL_FILE_HANDLE *TempFileHandle;\r | |
1689 | BOOLEAN Unicode;\r | |
1690 | \r | |
1691 | ASSERT(StdOut == NULL);\r | |
1692 | \r | |
1693 | ASSERT(StrStr(CmdLine, L"|") != NULL);\r | |
1694 | \r | |
1695 | Status = EFI_SUCCESS;\r | |
1696 | NextCommandLine = NULL;\r | |
1697 | OurCommandLine = NULL;\r | |
1698 | Size1 = 0;\r | |
1699 | Size2 = 0;\r | |
1700 | \r | |
1701 | NextCommandLine = StrnCatGrow(&NextCommandLine, &Size1, StrStr(CmdLine, L"|")+1, 0);\r | |
1702 | OurCommandLine = StrnCatGrow(&OurCommandLine , &Size2, CmdLine , StrStr(CmdLine, L"|") - CmdLine);\r | |
532691c8 | 1703 | \r |
1704 | if (NextCommandLine == NULL || OurCommandLine == NULL) {\r | |
1705 | SHELL_FREE_NON_NULL(OurCommandLine);\r | |
1706 | SHELL_FREE_NON_NULL(NextCommandLine);\r | |
1707 | return (EFI_OUT_OF_RESOURCES);\r | |
19216573 | 1708 | } else if (StrStr(OurCommandLine, L"|") != NULL || Size1 == 0 || Size2 == 0) {\r |
1709 | SHELL_FREE_NON_NULL(OurCommandLine);\r | |
1710 | SHELL_FREE_NON_NULL(NextCommandLine);\r | |
1711 | return (EFI_INVALID_PARAMETER);\r | |
a5382ce0 QS |
1712 | } else if (NextCommandLine[0] == L'a' &&\r |
1713 | (NextCommandLine[1] == L' ' || NextCommandLine[1] == CHAR_NULL)\r | |
1714 | ){\r | |
a405b86d | 1715 | CopyMem(NextCommandLine, NextCommandLine+1, StrSize(NextCommandLine) - sizeof(NextCommandLine[0]));\r |
a5382ce0 QS |
1716 | while (NextCommandLine[0] == L' ') {\r |
1717 | CopyMem(NextCommandLine, NextCommandLine+1, StrSize(NextCommandLine) - sizeof(NextCommandLine[0]));\r | |
1718 | }\r | |
1719 | if (NextCommandLine[0] == CHAR_NULL) {\r | |
1720 | SHELL_FREE_NON_NULL(OurCommandLine);\r | |
1721 | SHELL_FREE_NON_NULL(NextCommandLine);\r | |
1722 | return (EFI_INVALID_PARAMETER);\r | |
1723 | }\r | |
a405b86d | 1724 | Unicode = FALSE;\r |
1725 | } else {\r | |
1726 | Unicode = TRUE;\r | |
1727 | }\r | |
1728 | \r | |
1729 | \r | |
1730 | //\r | |
1731 | // make a SPLIT_LIST item and add to list\r | |
1732 | //\r | |
1733 | Split = AllocateZeroPool(sizeof(SPLIT_LIST));\r | |
b2c036a7 RN |
1734 | if (Split == NULL) {\r |
1735 | return EFI_OUT_OF_RESOURCES;\r | |
1736 | }\r | |
a405b86d | 1737 | Split->SplitStdIn = StdIn;\r |
1738 | Split->SplitStdOut = ConvertEfiFileProtocolToShellHandle(CreateFileInterfaceMem(Unicode), NULL);\r | |
1739 | ASSERT(Split->SplitStdOut != NULL);\r | |
1740 | InsertHeadList(&ShellInfoObject.SplitList.Link, &Split->Link);\r | |
1741 | \r | |
a308e058 | 1742 | Status = RunCommand(OurCommandLine);\r |
a405b86d | 1743 | \r |
1744 | //\r | |
1745 | // move the output from the first to the in to the second.\r | |
1746 | //\r | |
1747 | TempFileHandle = Split->SplitStdOut;\r | |
1748 | if (Split->SplitStdIn == StdIn) {\r | |
1749 | Split->SplitStdOut = NULL;\r | |
1750 | } else {\r | |
1751 | Split->SplitStdOut = Split->SplitStdIn;\r | |
1752 | }\r | |
1753 | Split->SplitStdIn = TempFileHandle;\r | |
1754 | ShellInfoObject.NewEfiShellProtocol->SetFilePosition(ConvertShellHandleToEfiFileProtocol(Split->SplitStdIn), 0);\r | |
1755 | \r | |
1756 | if (!EFI_ERROR(Status)) {\r | |
a308e058 | 1757 | Status = RunCommand(NextCommandLine);\r |
a405b86d | 1758 | }\r |
1759 | \r | |
1760 | //\r | |
1761 | // remove the top level from the ScriptList\r | |
1762 | //\r | |
1763 | ASSERT((SPLIT_LIST*)GetFirstNode(&ShellInfoObject.SplitList.Link) == Split);\r | |
1764 | RemoveEntryList(&Split->Link);\r | |
1765 | \r | |
1766 | //\r | |
1767 | // Note that the original StdIn is now the StdOut...\r | |
1768 | //\r | |
bd3fc813 | 1769 | if (Split->SplitStdOut != NULL) {\r |
a405b86d | 1770 | ShellInfoObject.NewEfiShellProtocol->CloseFile(ConvertShellHandleToEfiFileProtocol(Split->SplitStdOut));\r |
1771 | }\r | |
1772 | if (Split->SplitStdIn != NULL) {\r | |
1773 | ShellInfoObject.NewEfiShellProtocol->CloseFile(ConvertShellHandleToEfiFileProtocol(Split->SplitStdIn));\r | |
bd3fc813 | 1774 | FreePool (Split->SplitStdIn);\r |
a405b86d | 1775 | }\r |
1776 | \r | |
1777 | FreePool(Split);\r | |
1778 | FreePool(NextCommandLine);\r | |
1779 | FreePool(OurCommandLine);\r | |
1780 | \r | |
1781 | return (Status);\r | |
1782 | }\r | |
1783 | \r | |
1ef61d03 JC |
1784 | /**\r |
1785 | Take the original command line, substitute any variables, free \r | |
51429264 | 1786 | the original string, return the modified copy.\r |
1ef61d03 | 1787 | \r |
51429264 | 1788 | @param[in] CmdLine pointer to the command line to update.\r |
1ef61d03 | 1789 | \r |
51429264 SQ |
1790 | @retval EFI_SUCCESS the function was successful.\r |
1791 | @retval EFI_OUT_OF_RESOURCES a memory allocation failed.\r | |
1ef61d03 JC |
1792 | **/\r |
1793 | EFI_STATUS\r | |
1794 | EFIAPI\r | |
1795 | ShellSubstituteVariables(\r | |
1796 | IN CHAR16 **CmdLine\r | |
1797 | )\r | |
1798 | {\r | |
1799 | CHAR16 *NewCmdLine;\r | |
1800 | NewCmdLine = ShellConvertVariables(*CmdLine);\r | |
1801 | SHELL_FREE_NON_NULL(*CmdLine);\r | |
1802 | if (NewCmdLine == NULL) {\r | |
1803 | return (EFI_OUT_OF_RESOURCES);\r | |
1804 | }\r | |
1805 | *CmdLine = NewCmdLine;\r | |
1806 | return (EFI_SUCCESS);\r | |
1807 | }\r | |
1808 | \r | |
ca53c0af JC |
1809 | /**\r |
1810 | Take the original command line, substitute any alias in the first group of space delimited characters, free \r | |
51429264 | 1811 | the original string, return the modified copy.\r |
ca53c0af | 1812 | \r |
51429264 | 1813 | @param[in] CmdLine pointer to the command line to update.\r |
ca53c0af | 1814 | \r |
51429264 SQ |
1815 | @retval EFI_SUCCESS the function was successful.\r |
1816 | @retval EFI_OUT_OF_RESOURCES a memory allocation failed.\r | |
ca53c0af JC |
1817 | **/\r |
1818 | EFI_STATUS\r | |
1819 | EFIAPI\r | |
1820 | ShellSubstituteAliases(\r | |
1821 | IN CHAR16 **CmdLine\r | |
1822 | )\r | |
1823 | {\r | |
1824 | CHAR16 *NewCmdLine;\r | |
1825 | CHAR16 *CommandName;\r | |
1826 | EFI_STATUS Status;\r | |
1827 | UINTN PostAliasSize;\r | |
1828 | ASSERT(CmdLine != NULL);\r | |
1829 | ASSERT(*CmdLine!= NULL);\r | |
1830 | \r | |
1831 | \r | |
1832 | CommandName = NULL;\r | |
1833 | if (StrStr((*CmdLine), L" ") == NULL){\r | |
1834 | StrnCatGrow(&CommandName, NULL, (*CmdLine), 0);\r | |
1835 | } else {\r | |
1836 | StrnCatGrow(&CommandName, NULL, (*CmdLine), StrStr((*CmdLine), L" ") - (*CmdLine));\r | |
1837 | }\r | |
1838 | \r | |
1839 | //\r | |
1840 | // This cannot happen 'inline' since the CmdLine can need extra space.\r | |
1841 | //\r | |
1842 | NewCmdLine = NULL;\r | |
1843 | if (!ShellCommandIsCommandOnList(CommandName)) {\r | |
1844 | //\r | |
1845 | // Convert via alias\r | |
1846 | //\r | |
1847 | Status = ShellConvertAlias(&CommandName);\r | |
1848 | if (EFI_ERROR(Status)){\r | |
1849 | return (Status);\r | |
1850 | }\r | |
1851 | PostAliasSize = 0;\r | |
1852 | NewCmdLine = StrnCatGrow(&NewCmdLine, &PostAliasSize, CommandName, 0);\r | |
1853 | if (NewCmdLine == NULL) {\r | |
1854 | SHELL_FREE_NON_NULL(CommandName);\r | |
1855 | SHELL_FREE_NON_NULL(*CmdLine);\r | |
1856 | return (EFI_OUT_OF_RESOURCES);\r | |
1857 | }\r | |
1858 | NewCmdLine = StrnCatGrow(&NewCmdLine, &PostAliasSize, StrStr((*CmdLine), L" "), 0);\r | |
1859 | if (NewCmdLine == NULL) {\r | |
1860 | SHELL_FREE_NON_NULL(CommandName);\r | |
1861 | SHELL_FREE_NON_NULL(*CmdLine);\r | |
1862 | return (EFI_OUT_OF_RESOURCES);\r | |
1863 | }\r | |
1864 | } else {\r | |
1865 | NewCmdLine = StrnCatGrow(&NewCmdLine, NULL, (*CmdLine), 0);\r | |
1866 | }\r | |
1867 | \r | |
1868 | SHELL_FREE_NON_NULL(*CmdLine);\r | |
1869 | SHELL_FREE_NON_NULL(CommandName);\r | |
1870 | \r | |
1871 | //\r | |
1872 | // re-assign the passed in double pointer to point to our newly allocated buffer\r | |
1873 | //\r | |
1874 | *CmdLine = NewCmdLine;\r | |
1875 | \r | |
1876 | return (EFI_SUCCESS);\r | |
1877 | }\r | |
1878 | \r | |
6ba2921d JC |
1879 | /**\r |
1880 | Takes the Argv[0] part of the command line and determine the meaning of it.\r | |
e7831c90 | 1881 | \r |
51429264 | 1882 | @param[in] CmdName pointer to the command line to update.\r |
e7831c90 | 1883 | \r |
51429264 SQ |
1884 | @retval Internal_Command The name is an internal command.\r |
1885 | @retval File_Sys_Change the name is a file system change.\r | |
1886 | @retval Script_File_Name the name is a NSH script file.\r | |
1887 | @retval Unknown_Invalid the name is unknown.\r | |
1888 | @retval Efi_Application the name is an application (.EFI).\r | |
6ba2921d JC |
1889 | **/\r |
1890 | SHELL_OPERATION_TYPES\r | |
1891 | EFIAPI\r | |
1892 | GetOperationType(\r | |
1893 | IN CONST CHAR16 *CmdName\r | |
1894 | )\r | |
1895 | {\r | |
1896 | CHAR16* FileWithPath;\r | |
1897 | CONST CHAR16* TempLocation;\r | |
1898 | CONST CHAR16* TempLocation2;\r | |
1899 | \r | |
1900 | FileWithPath = NULL;\r | |
1901 | //\r | |
1902 | // test for an internal command.\r | |
1903 | //\r | |
1904 | if (ShellCommandIsCommandOnList(CmdName)) {\r | |
51429264 | 1905 | return (Internal_Command);\r |
6ba2921d JC |
1906 | }\r |
1907 | \r | |
1908 | //\r | |
fc4c7b30 | 1909 | // Test for file system change request. anything ending with first : and cant have spaces.\r |
6ba2921d JC |
1910 | //\r |
1911 | if (CmdName[(StrLen(CmdName)-1)] == L':') {\r | |
fc4c7b30 JC |
1912 | if ( StrStr(CmdName, L" ") != NULL \r |
1913 | || StrLen(StrStr(CmdName, L":")) > 1\r | |
1914 | ) {\r | |
51429264 | 1915 | return (Unknown_Invalid);\r |
6ba2921d | 1916 | }\r |
51429264 | 1917 | return (File_Sys_Change);\r |
6ba2921d JC |
1918 | }\r |
1919 | \r | |
1920 | //\r | |
1921 | // Test for a file\r | |
1922 | //\r | |
1923 | if ((FileWithPath = ShellFindFilePathEx(CmdName, mExecutableExtensions)) != NULL) {\r | |
1924 | //\r | |
1925 | // See if that file has a script file extension\r | |
1926 | //\r | |
1927 | if (StrLen(FileWithPath) > 4) {\r | |
1928 | TempLocation = FileWithPath+StrLen(FileWithPath)-4;\r | |
1929 | TempLocation2 = mScriptExtension;\r | |
1930 | if (StringNoCaseCompare((VOID*)(&TempLocation), (VOID*)(&TempLocation2)) == 0) {\r | |
1931 | SHELL_FREE_NON_NULL(FileWithPath);\r | |
51429264 | 1932 | return (Script_File_Name);\r |
6ba2921d JC |
1933 | }\r |
1934 | }\r | |
1935 | \r | |
1936 | //\r | |
1937 | // Was a file, but not a script. we treat this as an application.\r | |
1938 | //\r | |
1939 | SHELL_FREE_NON_NULL(FileWithPath);\r | |
51429264 | 1940 | return (Efi_Application);\r |
6ba2921d JC |
1941 | }\r |
1942 | \r | |
1943 | SHELL_FREE_NON_NULL(FileWithPath);\r | |
1944 | //\r | |
1945 | // No clue what this is... return invalid flag...\r | |
1946 | //\r | |
51429264 | 1947 | return (Unknown_Invalid);\r |
6ba2921d JC |
1948 | }\r |
1949 | \r | |
de4ebdcf SQ |
1950 | /**\r |
1951 | Determine if the first item in a command line is valid.\r | |
1952 | \r | |
1953 | @param[in] CmdLine The command line to parse.\r | |
1954 | \r | |
1955 | @retval EFI_SUCCESS The item is valid.\r | |
1956 | @retval EFI_OUT_OF_RESOURCES A memory allocation failed.\r | |
1957 | @retval EFI_NOT_FOUND The operation type is unknown or invalid.\r | |
1958 | **/\r | |
6bd64463 JC |
1959 | EFI_STATUS \r |
1960 | EFIAPI\r | |
1961 | IsValidSplit(\r | |
1962 | IN CONST CHAR16 *CmdLine\r | |
1963 | )\r | |
1964 | {\r | |
1965 | CHAR16 *Temp;\r | |
1966 | CHAR16 *FirstParameter;\r | |
1967 | CHAR16 *TempWalker;\r | |
1968 | EFI_STATUS Status;\r | |
1969 | \r | |
1970 | Temp = NULL;\r | |
1971 | \r | |
1972 | Temp = StrnCatGrow(&Temp, NULL, CmdLine, 0);\r | |
1973 | if (Temp == NULL) {\r | |
1974 | return (EFI_OUT_OF_RESOURCES);\r | |
1975 | }\r | |
1976 | \r | |
1977 | FirstParameter = StrStr(Temp, L"|");\r | |
1978 | if (FirstParameter != NULL) {\r | |
1979 | *FirstParameter = CHAR_NULL;\r | |
1980 | }\r | |
1981 | \r | |
1982 | FirstParameter = NULL;\r | |
1983 | \r | |
1984 | //\r | |
1985 | // Process the command line\r | |
1986 | //\r | |
1987 | Status = ProcessCommandLineToFinal(&Temp);\r | |
1988 | \r | |
1989 | if (!EFI_ERROR(Status)) {\r | |
1990 | FirstParameter = AllocateZeroPool(StrSize(CmdLine));\r | |
1991 | if (FirstParameter == NULL) {\r | |
1992 | SHELL_FREE_NON_NULL(Temp);\r | |
1993 | return (EFI_OUT_OF_RESOURCES);\r | |
1994 | }\r | |
1995 | TempWalker = (CHAR16*)Temp;\r | |
c0bcd343 | 1996 | if (!EFI_ERROR (ShellGetNextParameter (&TempWalker, FirstParameter, StrSize(CmdLine), TRUE))) {\r |
14030c5c JC |
1997 | if (GetOperationType(FirstParameter) == Unknown_Invalid) {\r |
1998 | ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_NOT_FOUND), ShellInfoObject.HiiHandle, FirstParameter);\r | |
1999 | SetLastError(SHELL_NOT_FOUND);\r | |
2000 | Status = EFI_NOT_FOUND;\r | |
2001 | }\r | |
6bd64463 JC |
2002 | }\r |
2003 | }\r | |
2004 | \r | |
2005 | SHELL_FREE_NON_NULL(Temp);\r | |
2006 | SHELL_FREE_NON_NULL(FirstParameter);\r | |
2007 | return Status;\r | |
2008 | }\r | |
2009 | \r | |
2010 | /**\r | |
51429264 | 2011 | Determine if a command line contains with a split contains only valid commands.\r |
6bd64463 JC |
2012 | \r |
2013 | @param[in] CmdLine The command line to parse.\r | |
2014 | \r | |
2015 | @retval EFI_SUCCESS CmdLine has only valid commands, application, or has no split.\r | |
2016 | @retval EFI_ABORTED CmdLine has at least one invalid command or application.\r | |
2017 | **/\r | |
2018 | EFI_STATUS\r | |
2019 | EFIAPI\r | |
2020 | VerifySplit(\r | |
2021 | IN CONST CHAR16 *CmdLine\r | |
2022 | )\r | |
2023 | {\r | |
2024 | CONST CHAR16 *TempSpot;\r | |
2025 | EFI_STATUS Status;\r | |
2026 | \r | |
2027 | //\r | |
a5382ce0 | 2028 | // If this was the only item, then get out\r |
6bd64463 | 2029 | //\r |
a5382ce0 QS |
2030 | if (!ContainsSplit(CmdLine)) {\r |
2031 | return (EFI_SUCCESS);\r | |
6bd64463 JC |
2032 | }\r |
2033 | \r | |
2034 | //\r | |
a5382ce0 | 2035 | // Verify up to the pipe or end character\r |
6bd64463 | 2036 | //\r |
a5382ce0 QS |
2037 | Status = IsValidSplit(CmdLine);\r |
2038 | if (EFI_ERROR(Status)) {\r | |
2039 | return (Status);\r | |
6bd64463 JC |
2040 | }\r |
2041 | \r | |
2042 | //\r | |
2043 | // recurse to verify the next item\r | |
2044 | //\r | |
c0bcd343 | 2045 | TempSpot = ShellFindFirstCharacter(CmdLine, L"|", TRUE) + 1;\r |
a5382ce0 QS |
2046 | if (*TempSpot == L'a' && \r |
2047 | (*(TempSpot + 1) == L' ' || *(TempSpot + 1) == CHAR_NULL)\r | |
2048 | ) {\r | |
2049 | // If it's an ASCII pipe '|a'\r | |
2050 | TempSpot += 1;\r | |
2051 | }\r | |
2052 | \r | |
6bd64463 JC |
2053 | return (VerifySplit(TempSpot));\r |
2054 | }\r | |
2055 | \r | |
680db511 | 2056 | /**\r |
e7831c90 JC |
2057 | Process a split based operation.\r |
2058 | \r | |
a308e058 | 2059 | @param[in] CmdLine pointer to the command line to process\r |
e7831c90 JC |
2060 | \r |
2061 | @retval EFI_SUCCESS The operation was successful\r | |
d08a5464 | 2062 | @return an error occurred.\r |
680db511 JC |
2063 | **/\r |
2064 | EFI_STATUS\r | |
2065 | EFIAPI\r | |
2066 | ProcessNewSplitCommandLine(\r | |
a308e058 | 2067 | IN CONST CHAR16 *CmdLine\r |
680db511 JC |
2068 | )\r |
2069 | {\r | |
2070 | SPLIT_LIST *Split;\r | |
2071 | EFI_STATUS Status;\r | |
2072 | \r | |
6bd64463 JC |
2073 | Status = VerifySplit(CmdLine);\r |
2074 | if (EFI_ERROR(Status)) {\r | |
2075 | return (Status);\r | |
2076 | }\r | |
2077 | \r | |
680db511 JC |
2078 | Split = NULL;\r |
2079 | \r | |
2080 | //\r | |
2081 | // are we in an existing split???\r | |
2082 | //\r | |
2083 | if (!IsListEmpty(&ShellInfoObject.SplitList.Link)) {\r | |
2084 | Split = (SPLIT_LIST*)GetFirstNode(&ShellInfoObject.SplitList.Link);\r | |
2085 | }\r | |
2086 | \r | |
2087 | if (Split == NULL) {\r | |
a308e058 | 2088 | Status = RunSplitCommand(CmdLine, NULL, NULL);\r |
680db511 | 2089 | } else {\r |
a308e058 | 2090 | Status = RunSplitCommand(CmdLine, Split->SplitStdIn, Split->SplitStdOut);\r |
680db511 JC |
2091 | }\r |
2092 | if (EFI_ERROR(Status)) {\r | |
2093 | ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_INVALID_SPLIT), ShellInfoObject.HiiHandle, CmdLine);\r | |
2094 | }\r | |
2095 | return (Status);\r | |
2096 | }\r | |
daf70584 JC |
2097 | \r |
2098 | /**\r | |
51429264 | 2099 | Handle a request to change the current file system.\r |
daf70584 | 2100 | \r |
51429264 | 2101 | @param[in] CmdLine The passed in command line.\r |
daf70584 | 2102 | \r |
51429264 | 2103 | @retval EFI_SUCCESS The operation was successful.\r |
daf70584 JC |
2104 | **/\r |
2105 | EFI_STATUS\r | |
2106 | EFIAPI\r | |
2107 | ChangeMappedDrive(\r | |
2108 | IN CONST CHAR16 *CmdLine\r | |
2109 | )\r | |
2110 | {\r | |
2111 | EFI_STATUS Status;\r | |
2112 | Status = EFI_SUCCESS;\r | |
2113 | \r | |
2114 | //\r | |
2115 | // make sure we are the right operation\r | |
2116 | //\r | |
2117 | ASSERT(CmdLine[(StrLen(CmdLine)-1)] == L':' && StrStr(CmdLine, L" ") == NULL);\r | |
2118 | \r | |
2119 | //\r | |
2120 | // Call the protocol API to do the work\r | |
2121 | //\r | |
2122 | Status = ShellInfoObject.NewEfiShellProtocol->SetCurDir(NULL, CmdLine);\r | |
2123 | \r | |
2124 | //\r | |
2125 | // Report any errors\r | |
2126 | //\r | |
2127 | if (EFI_ERROR(Status)) {\r | |
2128 | ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_INVALID_MAPPING), ShellInfoObject.HiiHandle, CmdLine);\r | |
2129 | }\r | |
2130 | \r | |
2131 | return (Status);\r | |
2132 | }\r | |
2133 | \r | |
5a5eb806 JC |
2134 | /**\r |
2135 | Reprocess the command line to direct all -? to the help command.\r | |
2136 | \r | |
2137 | if found, will add "help" as argv[0], and move the rest later.\r | |
2138 | \r | |
806c49db | 2139 | @param[in,out] CmdLine pointer to the command line to update\r |
5a5eb806 JC |
2140 | **/\r |
2141 | EFI_STATUS\r | |
2142 | EFIAPI\r | |
806c49db JC |
2143 | DoHelpUpdate(\r |
2144 | IN OUT CHAR16 **CmdLine\r | |
5a5eb806 JC |
2145 | )\r |
2146 | {\r | |
806c49db JC |
2147 | CHAR16 *CurrentParameter;\r |
2148 | CHAR16 *Walker;\r | |
806c49db JC |
2149 | CHAR16 *NewCommandLine;\r |
2150 | EFI_STATUS Status;\r | |
e75390f0 | 2151 | UINTN NewCmdLineSize;\r |
806c49db JC |
2152 | \r |
2153 | Status = EFI_SUCCESS;\r | |
2154 | \r | |
2155 | CurrentParameter = AllocateZeroPool(StrSize(*CmdLine));\r | |
2156 | if (CurrentParameter == NULL) {\r | |
2157 | return (EFI_OUT_OF_RESOURCES);\r | |
2158 | }\r | |
2159 | \r | |
2160 | Walker = *CmdLine;\r | |
2161 | while(Walker != NULL && *Walker != CHAR_NULL) {\r | |
c0bcd343 | 2162 | if (!EFI_ERROR (ShellGetNextParameter (&Walker, CurrentParameter, StrSize(*CmdLine), TRUE))) {\r |
14030c5c | 2163 | if (StrStr(CurrentParameter, L"-?") == CurrentParameter) {\r |
a8f98806 JC |
2164 | CurrentParameter[0] = L' ';\r |
2165 | CurrentParameter[1] = L' ';\r | |
e75390f0 QS |
2166 | NewCmdLineSize = StrSize(L"help ") + StrSize(*CmdLine);\r |
2167 | NewCommandLine = AllocateZeroPool(NewCmdLineSize);\r | |
14030c5c JC |
2168 | if (NewCommandLine == NULL) {\r |
2169 | Status = EFI_OUT_OF_RESOURCES;\r | |
2170 | break;\r | |
2171 | }\r | |
2172 | \r | |
2173 | //\r | |
2174 | // We know the space is sufficient since we just calculated it.\r | |
2175 | //\r | |
e75390f0 QS |
2176 | StrnCpyS(NewCommandLine, NewCmdLineSize/sizeof(CHAR16), L"help ", 5);\r |
2177 | StrnCatS(NewCommandLine, NewCmdLineSize/sizeof(CHAR16), *CmdLine, StrLen(*CmdLine));\r | |
14030c5c JC |
2178 | SHELL_FREE_NON_NULL(*CmdLine);\r |
2179 | *CmdLine = NewCommandLine;\r | |
806c49db JC |
2180 | break;\r |
2181 | }\r | |
806c49db JC |
2182 | }\r |
2183 | }\r | |
2184 | \r | |
2185 | SHELL_FREE_NON_NULL(CurrentParameter);\r | |
2186 | \r | |
2187 | return (Status);\r | |
2188 | }\r | |
2189 | \r | |
2190 | /**\r | |
51429264 | 2191 | Function to update the shell variable "lasterror".\r |
806c49db | 2192 | \r |
51429264 | 2193 | @param[in] ErrorCode the error code to put into lasterror.\r |
806c49db JC |
2194 | **/\r |
2195 | EFI_STATUS\r | |
2196 | EFIAPI\r | |
2197 | SetLastError(\r | |
6bd64463 | 2198 | IN CONST SHELL_STATUS ErrorCode\r |
806c49db JC |
2199 | )\r |
2200 | {\r | |
2201 | CHAR16 LeString[19];\r | |
2202 | if (sizeof(EFI_STATUS) == sizeof(UINT64)) {\r | |
2203 | UnicodeSPrint(LeString, sizeof(LeString), L"0x%Lx", ErrorCode);\r | |
2204 | } else {\r | |
2205 | UnicodeSPrint(LeString, sizeof(LeString), L"0x%x", ErrorCode);\r | |
2206 | }\r | |
2207 | DEBUG_CODE(InternalEfiShellSetEnv(L"debuglasterror", LeString, TRUE););\r | |
2208 | InternalEfiShellSetEnv(L"lasterror", LeString, TRUE);\r | |
2209 | \r | |
2210 | return (EFI_SUCCESS);\r | |
2211 | }\r | |
2212 | \r | |
2213 | /**\r | |
2214 | Converts the command line to it's post-processed form. this replaces variables and alias' per UEFI Shell spec.\r | |
2215 | \r | |
2216 | @param[in,out] CmdLine pointer to the command line to update\r | |
2217 | \r | |
2218 | @retval EFI_SUCCESS The operation was successful\r | |
2219 | @retval EFI_OUT_OF_RESOURCES A memory allocation failed.\r | |
d08a5464 | 2220 | @return some other error occurred\r |
806c49db JC |
2221 | **/\r |
2222 | EFI_STATUS\r | |
2223 | EFIAPI\r | |
12a27a6d | 2224 | ProcessCommandLineToFinal(\r |
806c49db JC |
2225 | IN OUT CHAR16 **CmdLine\r |
2226 | )\r | |
2227 | {\r | |
2228 | EFI_STATUS Status;\r | |
2229 | TrimSpaces(CmdLine);\r | |
2230 | \r | |
2231 | Status = ShellSubstituteAliases(CmdLine);\r | |
2232 | if (EFI_ERROR(Status)) {\r | |
2233 | return (Status);\r | |
2234 | }\r | |
2235 | \r | |
2236 | TrimSpaces(CmdLine);\r | |
2237 | \r | |
2238 | Status = ShellSubstituteVariables(CmdLine);\r | |
2239 | if (EFI_ERROR(Status)) {\r | |
2240 | return (Status);\r | |
2241 | }\r | |
81cd2f53 | 2242 | ASSERT (*CmdLine != NULL);\r |
806c49db JC |
2243 | \r |
2244 | TrimSpaces(CmdLine);\r | |
2245 | \r | |
2246 | //\r | |
2247 | // update for help parsing\r | |
2248 | //\r | |
2249 | if (StrStr(*CmdLine, L"?") != NULL) {\r | |
2250 | //\r | |
2251 | // This may do nothing if the ? does not indicate help.\r | |
2252 | // Save all the details for in the API below.\r | |
2253 | //\r | |
2254 | Status = DoHelpUpdate(CmdLine);\r | |
2255 | }\r | |
2256 | \r | |
2257 | TrimSpaces(CmdLine);\r | |
2258 | \r | |
2259 | return (EFI_SUCCESS);\r | |
2260 | }\r | |
2261 | \r | |
2262 | /**\r | |
2263 | Run an internal shell command.\r | |
2264 | \r | |
d08a5464 | 2265 | This API will update the shell's environment since these commands are libraries.\r |
806c49db JC |
2266 | \r |
2267 | @param[in] CmdLine the command line to run.\r | |
2268 | @param[in] FirstParameter the first parameter on the command line\r | |
2269 | @param[in] ParamProtocol the shell parameters protocol pointer\r | |
490ce43d | 2270 | @param[out] CommandStatus the status from the command line.\r |
806c49db JC |
2271 | \r |
2272 | @retval EFI_SUCCESS The command was completed.\r | |
2273 | @retval EFI_ABORTED The command's operation was aborted.\r | |
2274 | **/\r | |
2275 | EFI_STATUS\r | |
2276 | EFIAPI\r | |
2277 | RunInternalCommand(\r | |
2278 | IN CONST CHAR16 *CmdLine,\r | |
2279 | IN CHAR16 *FirstParameter,\r | |
490ce43d QS |
2280 | IN EFI_SHELL_PARAMETERS_PROTOCOL *ParamProtocol,\r |
2281 | OUT EFI_STATUS *CommandStatus\r | |
806c49db JC |
2282 | )\r |
2283 | {\r | |
2284 | EFI_STATUS Status;\r | |
2285 | UINTN Argc;\r | |
2286 | CHAR16 **Argv;\r | |
2287 | SHELL_STATUS CommandReturnedStatus;\r | |
2288 | BOOLEAN LastError;\r | |
4b6b543e QS |
2289 | CHAR16 *Walker;\r |
2290 | CHAR16 *NewCmdLine; \r | |
2291 | \r | |
2292 | NewCmdLine = AllocateCopyPool (StrSize (CmdLine), CmdLine);\r | |
2293 | if (NewCmdLine == NULL) {\r | |
2294 | return EFI_OUT_OF_RESOURCES;\r | |
2295 | }\r | |
2296 | \r | |
2297 | for (Walker = NewCmdLine; Walker != NULL && *Walker != CHAR_NULL ; Walker++) {\r | |
2298 | if (*Walker == L'^' && *(Walker+1) == L'#') {\r | |
2299 | CopyMem(Walker, Walker+1, StrSize(Walker) - sizeof(Walker[0]));\r | |
2300 | }\r | |
2301 | }\r | |
806c49db | 2302 | \r |
5a5eb806 | 2303 | //\r |
806c49db | 2304 | // get the argc and argv updated for internal commands\r |
5a5eb806 | 2305 | //\r |
d1c275c6 | 2306 | Status = UpdateArgcArgv(ParamProtocol, NewCmdLine, Internal_Command, &Argv, &Argc);\r |
806c49db | 2307 | if (!EFI_ERROR(Status)) {\r |
5a5eb806 | 2308 | //\r |
806c49db | 2309 | // Run the internal command.\r |
5a5eb806 | 2310 | //\r |
806c49db JC |
2311 | Status = ShellCommandRunCommandHandler(FirstParameter, &CommandReturnedStatus, &LastError);\r |
2312 | \r | |
2313 | if (!EFI_ERROR(Status)) {\r | |
490ce43d QS |
2314 | if (CommandStatus != NULL) {\r |
2315 | if (CommandReturnedStatus != SHELL_SUCCESS) {\r | |
2316 | *CommandStatus = (EFI_STATUS)(CommandReturnedStatus | MAX_BIT);\r | |
2317 | } else {\r | |
2318 | *CommandStatus = EFI_SUCCESS;\r | |
2319 | }\r | |
2320 | }\r | |
2321 | \r | |
5a5eb806 | 2322 | //\r |
806c49db JC |
2323 | // Update last error status.\r |
2324 | // some commands do not update last error.\r | |
5a5eb806 | 2325 | //\r |
806c49db JC |
2326 | if (LastError) {\r |
2327 | SetLastError(CommandReturnedStatus);\r | |
5a5eb806 | 2328 | }\r |
806c49db JC |
2329 | \r |
2330 | //\r | |
2331 | // Pass thru the exitcode from the app.\r | |
2332 | //\r | |
2333 | if (ShellCommandGetExit()) {\r | |
e3eb7d82 JC |
2334 | //\r |
2335 | // An Exit was requested ("exit" command), pass its value up.\r | |
2336 | //\r | |
806c49db | 2337 | Status = CommandReturnedStatus;\r |
e3eb7d82 JC |
2338 | } else if (CommandReturnedStatus != SHELL_SUCCESS && IsScriptOnlyCommand(FirstParameter)) {\r |
2339 | //\r | |
2340 | // Always abort when a script only command fails for any reason\r | |
2341 | //\r | |
2342 | Status = EFI_ABORTED;\r | |
2343 | } else if (ShellCommandGetCurrentScriptFile() != NULL && CommandReturnedStatus == SHELL_ABORTED) {\r | |
2344 | //\r | |
2345 | // Abort when in a script and a command aborted\r | |
2346 | //\r | |
806c49db | 2347 | Status = EFI_ABORTED;\r |
5a5eb806 | 2348 | }\r |
806c49db JC |
2349 | }\r |
2350 | }\r | |
2351 | \r | |
2352 | //\r | |
d08a5464 | 2353 | // This is guaranteed to be called after UpdateArgcArgv no matter what else happened.\r |
806c49db JC |
2354 | // This is safe even if the update API failed. In this case, it may be a no-op.\r |
2355 | //\r | |
2356 | RestoreArgcArgv(ParamProtocol, &Argv, &Argc);\r | |
2357 | \r | |
e3eb7d82 | 2358 | //\r |
d08a5464 | 2359 | // If a script is running and the command is not a script only command, then\r |
e3eb7d82 JC |
2360 | // change return value to success so the script won't halt (unless aborted).\r |
2361 | //\r | |
2362 | // Script only commands have to be able halt the script since the script will\r | |
2363 | // not operate if they are failing.\r | |
2364 | //\r | |
2365 | if ( ShellCommandGetCurrentScriptFile() != NULL\r | |
2366 | && !IsScriptOnlyCommand(FirstParameter)\r | |
2367 | && Status != EFI_ABORTED\r | |
2368 | ) {\r | |
806c49db JC |
2369 | Status = EFI_SUCCESS;\r |
2370 | }\r | |
2371 | \r | |
4b6b543e | 2372 | FreePool (NewCmdLine);\r |
806c49db JC |
2373 | return (Status);\r |
2374 | }\r | |
2375 | \r | |
2376 | /**\r | |
2377 | Function to run the command or file.\r | |
2378 | \r | |
2379 | @param[in] Type the type of operation being run.\r | |
2380 | @param[in] CmdLine the command line to run.\r | |
2381 | @param[in] FirstParameter the first parameter on the command line\r | |
2382 | @param[in] ParamProtocol the shell parameters protocol pointer\r | |
490ce43d | 2383 | @param[out] CommandStatus the status from the command line.\r |
806c49db JC |
2384 | \r |
2385 | @retval EFI_SUCCESS The command was completed.\r | |
2386 | @retval EFI_ABORTED The command's operation was aborted.\r | |
2387 | **/\r | |
2388 | EFI_STATUS\r | |
2389 | EFIAPI\r | |
2390 | RunCommandOrFile(\r | |
2391 | IN SHELL_OPERATION_TYPES Type,\r | |
2392 | IN CONST CHAR16 *CmdLine,\r | |
2393 | IN CHAR16 *FirstParameter,\r | |
490ce43d QS |
2394 | IN EFI_SHELL_PARAMETERS_PROTOCOL *ParamProtocol,\r |
2395 | OUT EFI_STATUS *CommandStatus\r | |
806c49db JC |
2396 | )\r |
2397 | {\r | |
2398 | EFI_STATUS Status;\r | |
cd39fe08 | 2399 | EFI_STATUS StartStatus;\r |
806c49db JC |
2400 | CHAR16 *CommandWithPath;\r |
2401 | EFI_DEVICE_PATH_PROTOCOL *DevPath;\r | |
5223c121 | 2402 | SHELL_STATUS CalleeExitStatus;\r |
806c49db JC |
2403 | \r |
2404 | Status = EFI_SUCCESS;\r | |
2405 | CommandWithPath = NULL;\r | |
2406 | DevPath = NULL;\r | |
71c49eaf | 2407 | CalleeExitStatus = SHELL_INVALID_PARAMETER;\r |
806c49db JC |
2408 | \r |
2409 | switch (Type) {\r | |
51429264 | 2410 | case Internal_Command:\r |
490ce43d | 2411 | Status = RunInternalCommand(CmdLine, FirstParameter, ParamProtocol, CommandStatus);\r |
806c49db | 2412 | break;\r |
51429264 SQ |
2413 | case Script_File_Name:\r |
2414 | case Efi_Application:\r | |
806c49db JC |
2415 | //\r |
2416 | // Process a fully qualified path\r | |
2417 | //\r | |
2418 | if (StrStr(FirstParameter, L":") != NULL) {\r | |
2419 | ASSERT (CommandWithPath == NULL);\r | |
2420 | if (ShellIsFile(FirstParameter) == EFI_SUCCESS) {\r | |
2421 | CommandWithPath = StrnCatGrow(&CommandWithPath, NULL, FirstParameter, 0);\r | |
2422 | }\r | |
2423 | }\r | |
2424 | \r | |
2425 | //\r | |
2426 | // Process a relative path and also check in the path environment variable\r | |
2427 | //\r | |
2428 | if (CommandWithPath == NULL) {\r | |
2429 | CommandWithPath = ShellFindFilePathEx(FirstParameter, mExecutableExtensions);\r | |
2430 | }\r | |
2431 | \r | |
2432 | //\r | |
2433 | // This should be impossible now.\r | |
2434 | //\r | |
2435 | ASSERT(CommandWithPath != NULL);\r | |
2436 | \r | |
2437 | //\r | |
2438 | // Make sure that path is not just a directory (or not found)\r | |
2439 | //\r | |
2440 | if (!EFI_ERROR(ShellIsDirectory(CommandWithPath))) {\r | |
2441 | ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_NOT_FOUND), ShellInfoObject.HiiHandle, FirstParameter);\r | |
6bd64463 | 2442 | SetLastError(SHELL_NOT_FOUND);\r |
806c49db JC |
2443 | }\r |
2444 | switch (Type) {\r | |
51429264 | 2445 | case Script_File_Name:\r |
a308e058 | 2446 | Status = RunScriptFile (CommandWithPath, NULL, CmdLine, ParamProtocol);\r |
806c49db | 2447 | break;\r |
51429264 | 2448 | case Efi_Application:\r |
806c49db JC |
2449 | //\r |
2450 | // Get the device path of the application image\r | |
2451 | //\r | |
2452 | DevPath = ShellInfoObject.NewEfiShellProtocol->GetDevicePathFromFilePath(CommandWithPath);\r | |
2453 | if (DevPath == NULL){\r | |
2454 | Status = EFI_OUT_OF_RESOURCES;\r | |
2455 | break;\r | |
2456 | }\r | |
2457 | \r | |
2458 | //\r | |
2459 | // Execute the device path\r | |
2460 | //\r | |
2461 | Status = InternalShellExecuteDevicePath(\r | |
2462 | &gImageHandle,\r | |
2463 | DevPath,\r | |
2464 | CmdLine,\r | |
2465 | NULL,\r | |
a308e058 | 2466 | &StartStatus\r |
806c49db JC |
2467 | );\r |
2468 | \r | |
2469 | SHELL_FREE_NON_NULL(DevPath);\r | |
2470 | \r | |
71c49eaf SQ |
2471 | if(EFI_ERROR (Status)) {\r |
2472 | CalleeExitStatus = (SHELL_STATUS) (Status & (~MAX_BIT));\r | |
2473 | } else {\r | |
cd39fe08 | 2474 | CalleeExitStatus = (SHELL_STATUS) StartStatus;\r |
71c49eaf SQ |
2475 | }\r |
2476 | \r | |
490ce43d QS |
2477 | if (CommandStatus != NULL) {\r |
2478 | *CommandStatus = CalleeExitStatus;\r | |
2479 | }\r | |
2480 | \r | |
806c49db JC |
2481 | //\r |
2482 | // Update last error status.\r | |
2483 | //\r | |
5223c121 | 2484 | // Status is an EFI_STATUS. Clear top bit to convert to SHELL_STATUS\r |
71c49eaf | 2485 | SetLastError(CalleeExitStatus);\r |
806c49db | 2486 | break;\r |
6fa0da7d LE |
2487 | default:\r |
2488 | //\r | |
2489 | // Do nothing.\r | |
2490 | //\r | |
2491 | break;\r | |
5a5eb806 JC |
2492 | }\r |
2493 | break;\r | |
6fa0da7d LE |
2494 | default:\r |
2495 | //\r | |
2496 | // Do nothing.\r | |
2497 | //\r | |
2498 | break;\r | |
5a5eb806 | 2499 | }\r |
806c49db JC |
2500 | \r |
2501 | SHELL_FREE_NON_NULL(CommandWithPath);\r | |
2502 | \r | |
2503 | return (Status);\r | |
2504 | }\r | |
2505 | \r | |
2506 | /**\r | |
2507 | Function to setup StdIn, StdErr, StdOut, and then run the command or file.\r | |
2508 | \r | |
2509 | @param[in] Type the type of operation being run.\r | |
2510 | @param[in] CmdLine the command line to run.\r | |
2511 | @param[in] FirstParameter the first parameter on the command line.\r | |
2512 | @param[in] ParamProtocol the shell parameters protocol pointer\r | |
490ce43d | 2513 | @param[out] CommandStatus the status from the command line.\r |
806c49db JC |
2514 | \r |
2515 | @retval EFI_SUCCESS The command was completed.\r | |
2516 | @retval EFI_ABORTED The command's operation was aborted.\r | |
2517 | **/\r | |
2518 | EFI_STATUS\r | |
2519 | EFIAPI\r | |
2520 | SetupAndRunCommandOrFile(\r | |
490ce43d QS |
2521 | IN SHELL_OPERATION_TYPES Type,\r |
2522 | IN CHAR16 *CmdLine,\r | |
2523 | IN CHAR16 *FirstParameter,\r | |
2524 | IN EFI_SHELL_PARAMETERS_PROTOCOL *ParamProtocol,\r | |
dcbdb8bf | 2525 | OUT EFI_STATUS *CommandStatus\r |
806c49db JC |
2526 | )\r |
2527 | {\r | |
2528 | EFI_STATUS Status;\r | |
2529 | SHELL_FILE_HANDLE OriginalStdIn;\r | |
2530 | SHELL_FILE_HANDLE OriginalStdOut;\r | |
2531 | SHELL_FILE_HANDLE OriginalStdErr;\r | |
2532 | SYSTEM_TABLE_INFO OriginalSystemTableInfo;\r | |
08222019 | 2533 | CONST SCRIPT_FILE *ConstScriptFile;\r |
806c49db JC |
2534 | \r |
2535 | //\r | |
2536 | // Update the StdIn, StdOut, and StdErr for redirection to environment variables, files, etc... unicode and ASCII\r | |
2537 | //\r | |
2538 | Status = UpdateStdInStdOutStdErr(ParamProtocol, CmdLine, &OriginalStdIn, &OriginalStdOut, &OriginalStdErr, &OriginalSystemTableInfo);\r | |
2539 | \r | |
2540 | //\r | |
2541 | // The StdIn, StdOut, and StdErr are set up.\r | |
2542 | // Now run the command, script, or application\r | |
2543 | //\r | |
2544 | if (!EFI_ERROR(Status)) {\r | |
75eb337f | 2545 | TrimSpaces(&CmdLine);\r |
490ce43d | 2546 | Status = RunCommandOrFile(Type, CmdLine, FirstParameter, ParamProtocol, CommandStatus);\r |
806c49db JC |
2547 | }\r |
2548 | \r | |
2549 | //\r | |
2550 | // Now print errors\r | |
2551 | //\r | |
2552 | if (EFI_ERROR(Status)) {\r | |
08222019 JC |
2553 | ConstScriptFile = ShellCommandGetCurrentScriptFile();\r |
2554 | if (ConstScriptFile == NULL || ConstScriptFile->CurrentCommand == NULL) {\r | |
063aa89b JC |
2555 | ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_ERROR), ShellInfoObject.HiiHandle, (VOID*)(Status));\r |
2556 | } else {\r | |
08222019 | 2557 | ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_ERROR_SCRIPT), ShellInfoObject.HiiHandle, (VOID*)(Status), ConstScriptFile->CurrentCommand->Line);\r |
063aa89b | 2558 | }\r |
806c49db JC |
2559 | }\r |
2560 | \r | |
2561 | //\r | |
2562 | // put back the original StdIn, StdOut, and StdErr\r | |
2563 | //\r | |
2564 | RestoreStdInStdOutStdErr(ParamProtocol, &OriginalStdIn, &OriginalStdOut, &OriginalStdErr, &OriginalSystemTableInfo);\r | |
2565 | \r | |
2566 | return (Status);\r | |
5a5eb806 JC |
2567 | }\r |
2568 | \r | |
a405b86d | 2569 | /**\r |
2570 | Function will process and run a command line.\r | |
2571 | \r | |
2572 | This will determine if the command line represents an internal shell \r | |
2573 | command or dispatch an external application.\r | |
2574 | \r | |
2575 | @param[in] CmdLine The command line to parse.\r | |
490ce43d | 2576 | @param[out] CommandStatus The status from the command line.\r |
a405b86d | 2577 | \r |
2578 | @retval EFI_SUCCESS The command was completed.\r | |
2579 | @retval EFI_ABORTED The command's operation was aborted.\r | |
2580 | **/\r | |
2581 | EFI_STATUS\r | |
2582 | EFIAPI\r | |
490ce43d QS |
2583 | RunShellCommand(\r |
2584 | IN CONST CHAR16 *CmdLine,\r | |
2585 | OUT EFI_STATUS *CommandStatus\r | |
a405b86d | 2586 | )\r |
2587 | {\r | |
2588 | EFI_STATUS Status;\r | |
a405b86d | 2589 | CHAR16 *CleanOriginal;\r |
806c49db JC |
2590 | CHAR16 *FirstParameter;\r |
2591 | CHAR16 *TempWalker;\r | |
2592 | SHELL_OPERATION_TYPES Type;\r | |
c0bcd343 | 2593 | CHAR16 *OldCmdLine;\r |
a405b86d | 2594 | \r |
2595 | ASSERT(CmdLine != NULL);\r | |
2596 | if (StrLen(CmdLine) == 0) {\r | |
2597 | return (EFI_SUCCESS);\r | |
2598 | }\r | |
2599 | \r | |
a405b86d | 2600 | Status = EFI_SUCCESS;\r |
c0bcd343 | 2601 | FirstParameter = NULL;\r |
a405b86d | 2602 | CleanOriginal = NULL;\r |
c0bcd343 | 2603 | OldCmdLine = NULL;\r |
a405b86d | 2604 | \r |
2605 | CleanOriginal = StrnCatGrow(&CleanOriginal, NULL, CmdLine, 0);\r | |
532691c8 | 2606 | if (CleanOriginal == NULL) {\r |
c0bcd343 RN |
2607 | Status = EFI_OUT_OF_RESOURCES;\r |
2608 | goto Done;\r | |
532691c8 | 2609 | }\r |
284e034f | 2610 | \r |
ad2bc854 | 2611 | TrimSpaces(&CleanOriginal);\r |
a405b86d | 2612 | \r |
284e034f | 2613 | //\r |
d41a79a0 CP |
2614 | // NULL out comments (leveraged from RunScriptFileHandle() ).\r |
2615 | // The # character on a line is used to denote that all characters on the same line\r | |
2616 | // and to the right of the # are to be ignored by the shell.\r | |
d08a5464 | 2617 | // Afterwards, again remove spaces, in case any were between the last command-parameter and '#'.\r |
d41a79a0 CP |
2618 | //\r |
2619 | for (TempWalker = CleanOriginal; TempWalker != NULL && *TempWalker != CHAR_NULL; TempWalker++) {\r | |
2620 | if (*TempWalker == L'^') {\r | |
2621 | if (*(TempWalker + 1) == L'#') {\r | |
4b6b543e | 2622 | TempWalker++;\r |
d41a79a0 CP |
2623 | }\r |
2624 | } else if (*TempWalker == L'#') {\r | |
2625 | *TempWalker = CHAR_NULL;\r | |
2626 | }\r | |
2627 | }\r | |
2628 | \r | |
2629 | TrimSpaces(&CleanOriginal);\r | |
2630 | \r | |
2631 | //\r | |
284e034f CP |
2632 | // Handle case that passed in command line is just 1 or more " " characters.\r |
2633 | //\r | |
2634 | if (StrLen (CleanOriginal) == 0) {\r | |
c0bcd343 RN |
2635 | Status = EFI_SUCCESS;\r |
2636 | goto Done;\r | |
284e034f CP |
2637 | }\r |
2638 | \r | |
12a27a6d | 2639 | Status = ProcessCommandLineToFinal(&CleanOriginal);\r |
ca53c0af | 2640 | if (EFI_ERROR(Status)) {\r |
c0bcd343 | 2641 | goto Done;\r |
a405b86d | 2642 | }\r |
2643 | \r | |
c0bcd343 RN |
2644 | OldCmdLine = ShellGetRawCmdLine ();\r |
2645 | ShellSetRawCmdLine (CleanOriginal);\r | |
2646 | \r | |
a405b86d | 2647 | //\r |
d08a5464 | 2648 | // We don't do normal processing with a split command line (output from one command input to another)\r |
a405b86d | 2649 | //\r |
1ef61d03 | 2650 | if (ContainsSplit(CleanOriginal)) {\r |
a308e058 | 2651 | Status = ProcessNewSplitCommandLine(CleanOriginal);\r |
c0bcd343 RN |
2652 | goto Done;\r |
2653 | }\r | |
5b5cd144 | 2654 | \r |
806c49db JC |
2655 | //\r |
2656 | // We need the first parameter information so we can determine the operation type\r | |
2657 | //\r | |
2658 | FirstParameter = AllocateZeroPool(StrSize(CleanOriginal));\r | |
2659 | if (FirstParameter == NULL) {\r | |
c0bcd343 RN |
2660 | Status = EFI_OUT_OF_RESOURCES;\r |
2661 | goto Done;\r | |
806c49db JC |
2662 | }\r |
2663 | TempWalker = CleanOriginal;\r | |
c0bcd343 | 2664 | if (!EFI_ERROR (ShellGetNextParameter (&TempWalker, FirstParameter, StrSize(CleanOriginal), TRUE))) {\r |
14030c5c JC |
2665 | //\r |
2666 | // Depending on the first parameter we change the behavior\r | |
2667 | //\r | |
2668 | switch (Type = GetOperationType(FirstParameter)) {\r | |
2669 | case File_Sys_Change:\r | |
2670 | Status = ChangeMappedDrive (FirstParameter);\r | |
2671 | break;\r | |
2672 | case Internal_Command:\r | |
2673 | case Script_File_Name:\r | |
2674 | case Efi_Application:\r | |
490ce43d | 2675 | Status = SetupAndRunCommandOrFile(Type, CleanOriginal, FirstParameter, ShellInfoObject.NewShellParametersProtocol, CommandStatus);\r |
14030c5c JC |
2676 | break;\r |
2677 | default:\r | |
2678 | //\r | |
2679 | // Whatever was typed, it was invalid.\r | |
2680 | //\r | |
2681 | ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_NOT_FOUND), ShellInfoObject.HiiHandle, FirstParameter);\r | |
2682 | SetLastError(SHELL_NOT_FOUND);\r | |
2683 | break;\r | |
2684 | }\r | |
2685 | } else {\r | |
2686 | ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_NOT_FOUND), ShellInfoObject.HiiHandle, FirstParameter);\r | |
2687 | SetLastError(SHELL_NOT_FOUND);\r | |
a405b86d | 2688 | }\r |
c0bcd343 RN |
2689 | \r |
2690 | Done:\r | |
2691 | ShellSetRawCmdLine (OldCmdLine);\r | |
2692 | SHELL_FREE_NON_NULL (OldCmdLine);\r | |
2693 | SHELL_FREE_NON_NULL (CleanOriginal);\r | |
2694 | SHELL_FREE_NON_NULL (FirstParameter);\r | |
a405b86d | 2695 | \r |
2696 | return (Status);\r | |
2697 | }\r | |
2698 | \r | |
490ce43d QS |
2699 | /**\r |
2700 | Function will process and run a command line.\r | |
2701 | \r | |
2702 | This will determine if the command line represents an internal shell \r | |
2703 | command or dispatch an external application.\r | |
2704 | \r | |
2705 | @param[in] CmdLine The command line to parse.\r | |
2706 | \r | |
2707 | @retval EFI_SUCCESS The command was completed.\r | |
2708 | @retval EFI_ABORTED The command's operation was aborted.\r | |
2709 | **/\r | |
2710 | EFI_STATUS\r | |
2711 | EFIAPI\r | |
2712 | RunCommand(\r | |
2713 | IN CONST CHAR16 *CmdLine\r | |
2714 | )\r | |
2715 | {\r | |
2716 | return (RunShellCommand(CmdLine, NULL));\r | |
2717 | }\r | |
2718 | \r | |
2719 | \r | |
a405b86d | 2720 | STATIC CONST UINT16 InvalidChars[] = {L'*', L'?', L'<', L'>', L'\\', L'/', L'\"', 0x0001, 0x0002};\r |
2721 | /**\r | |
d08a5464 | 2722 | Function determines if the CommandName COULD be a valid command. It does not determine whether\r |
a405b86d | 2723 | this is a valid command. It only checks for invalid characters.\r |
2724 | \r | |
2725 | @param[in] CommandName The name to check\r | |
2726 | \r | |
2727 | @retval TRUE CommandName could be a command name\r | |
2728 | @retval FALSE CommandName could not be a valid command name\r | |
2729 | **/\r | |
2730 | BOOLEAN\r | |
2731 | EFIAPI\r | |
2732 | IsValidCommandName(\r | |
2733 | IN CONST CHAR16 *CommandName\r | |
2734 | )\r | |
2735 | {\r | |
2736 | UINTN Count;\r | |
2737 | if (CommandName == NULL) {\r | |
2738 | ASSERT(FALSE);\r | |
2739 | return (FALSE);\r | |
2740 | }\r | |
2741 | for ( Count = 0\r | |
2742 | ; Count < sizeof(InvalidChars) / sizeof(InvalidChars[0])\r | |
2743 | ; Count++\r | |
2744 | ){\r | |
2745 | if (ScanMem16(CommandName, StrSize(CommandName), InvalidChars[Count]) != NULL) {\r | |
2746 | return (FALSE);\r | |
2747 | }\r | |
2748 | }\r | |
2749 | return (TRUE);\r | |
2750 | }\r | |
2751 | \r | |
2752 | /**\r | |
2753 | Function to process a NSH script file via SHELL_FILE_HANDLE.\r | |
2754 | \r | |
2755 | @param[in] Handle The handle to the already opened file.\r | |
2756 | @param[in] Name The name of the script file.\r | |
2757 | \r | |
d08a5464 | 2758 | @retval EFI_SUCCESS the script completed successfully\r |
a405b86d | 2759 | **/\r |
2760 | EFI_STATUS\r | |
2761 | EFIAPI\r | |
2762 | RunScriptFileHandle (\r | |
a308e058 RN |
2763 | IN SHELL_FILE_HANDLE Handle,\r |
2764 | IN CONST CHAR16 *Name\r | |
a405b86d | 2765 | )\r |
2766 | {\r | |
2767 | EFI_STATUS Status;\r | |
2768 | SCRIPT_FILE *NewScriptFile;\r | |
2769 | UINTN LoopVar;\r | |
4dc0d578 | 2770 | UINTN PrintBuffSize;\r |
a405b86d | 2771 | CHAR16 *CommandLine;\r |
2772 | CHAR16 *CommandLine2;\r | |
2773 | CHAR16 *CommandLine3;\r | |
2774 | SCRIPT_COMMAND_LIST *LastCommand;\r | |
2775 | BOOLEAN Ascii;\r | |
2776 | BOOLEAN PreScriptEchoState;\r | |
848997b5 | 2777 | BOOLEAN PreCommandEchoState;\r |
a405b86d | 2778 | CONST CHAR16 *CurDir;\r |
2779 | UINTN LineCount;\r | |
b6b22b13 | 2780 | CHAR16 LeString[50];\r |
14810d6b | 2781 | LIST_ENTRY OldBufferList;\r |
a405b86d | 2782 | \r |
2783 | ASSERT(!ShellCommandGetScriptExit());\r | |
2784 | \r | |
2785 | PreScriptEchoState = ShellCommandGetEchoState();\r | |
4dc0d578 | 2786 | PrintBuffSize = PcdGet16(PcdShellPrintBufferSize);\r |
a405b86d | 2787 | \r |
2788 | NewScriptFile = (SCRIPT_FILE*)AllocateZeroPool(sizeof(SCRIPT_FILE));\r | |
3c865f20 | 2789 | if (NewScriptFile == NULL) {\r |
2790 | return (EFI_OUT_OF_RESOURCES);\r | |
2791 | }\r | |
a405b86d | 2792 | \r |
2793 | //\r | |
2794 | // Set up the name\r | |
2795 | //\r | |
2796 | ASSERT(NewScriptFile->ScriptName == NULL);\r | |
2797 | NewScriptFile->ScriptName = StrnCatGrow(&NewScriptFile->ScriptName, NULL, Name, 0);\r | |
3c865f20 | 2798 | if (NewScriptFile->ScriptName == NULL) {\r |
2799 | DeleteScriptFileStruct(NewScriptFile);\r | |
2800 | return (EFI_OUT_OF_RESOURCES);\r | |
2801 | }\r | |
a405b86d | 2802 | \r |
2803 | //\r | |
2804 | // Save the parameters (used to replace %0 to %9 later on)\r | |
2805 | //\r | |
2806 | NewScriptFile->Argc = ShellInfoObject.NewShellParametersProtocol->Argc;\r | |
2807 | if (NewScriptFile->Argc != 0) {\r | |
2808 | NewScriptFile->Argv = (CHAR16**)AllocateZeroPool(NewScriptFile->Argc * sizeof(CHAR16*));\r | |
3c865f20 | 2809 | if (NewScriptFile->Argv == NULL) {\r |
2810 | DeleteScriptFileStruct(NewScriptFile);\r | |
2811 | return (EFI_OUT_OF_RESOURCES);\r | |
2812 | }\r | |
a405b86d | 2813 | for (LoopVar = 0 ; LoopVar < 10 && LoopVar < NewScriptFile->Argc; LoopVar++) {\r |
2814 | ASSERT(NewScriptFile->Argv[LoopVar] == NULL);\r | |
2815 | NewScriptFile->Argv[LoopVar] = StrnCatGrow(&NewScriptFile->Argv[LoopVar], NULL, ShellInfoObject.NewShellParametersProtocol->Argv[LoopVar], 0);\r | |
3c865f20 | 2816 | if (NewScriptFile->Argv[LoopVar] == NULL) {\r |
2817 | DeleteScriptFileStruct(NewScriptFile);\r | |
2818 | return (EFI_OUT_OF_RESOURCES);\r | |
2819 | }\r | |
a405b86d | 2820 | }\r |
2821 | } else {\r | |
2822 | NewScriptFile->Argv = NULL;\r | |
2823 | }\r | |
2824 | \r | |
2825 | InitializeListHead(&NewScriptFile->CommandList);\r | |
2826 | InitializeListHead(&NewScriptFile->SubstList);\r | |
2827 | \r | |
2828 | //\r | |
2829 | // Now build the list of all script commands.\r | |
2830 | //\r | |
2831 | LineCount = 0;\r | |
2832 | while(!ShellFileHandleEof(Handle)) {\r | |
2833 | CommandLine = ShellFileHandleReturnLine(Handle, &Ascii);\r | |
2834 | LineCount++;\r | |
4922715d JC |
2835 | if (CommandLine == NULL || StrLen(CommandLine) == 0 || CommandLine[0] == '#') {\r |
2836 | SHELL_FREE_NON_NULL(CommandLine);\r | |
a405b86d | 2837 | continue;\r |
2838 | }\r | |
2839 | NewScriptFile->CurrentCommand = AllocateZeroPool(sizeof(SCRIPT_COMMAND_LIST));\r | |
3c865f20 | 2840 | if (NewScriptFile->CurrentCommand == NULL) {\r |
4922715d | 2841 | SHELL_FREE_NON_NULL(CommandLine);\r |
3c865f20 | 2842 | DeleteScriptFileStruct(NewScriptFile);\r |
2843 | return (EFI_OUT_OF_RESOURCES);\r | |
2844 | }\r | |
a405b86d | 2845 | \r |
2846 | NewScriptFile->CurrentCommand->Cl = CommandLine;\r | |
2847 | NewScriptFile->CurrentCommand->Data = NULL;\r | |
2848 | NewScriptFile->CurrentCommand->Line = LineCount;\r | |
2849 | \r | |
2850 | InsertTailList(&NewScriptFile->CommandList, &NewScriptFile->CurrentCommand->Link);\r | |
2851 | }\r | |
2852 | \r | |
2853 | //\r | |
2854 | // Add this as the topmost script file\r | |
2855 | //\r | |
2856 | ShellCommandSetNewScript (NewScriptFile);\r | |
2857 | \r | |
2858 | //\r | |
2859 | // Now enumerate through the commands and run each one.\r | |
2860 | //\r | |
4dc0d578 | 2861 | CommandLine = AllocateZeroPool(PrintBuffSize);\r |
3c865f20 | 2862 | if (CommandLine == NULL) {\r |
2863 | DeleteScriptFileStruct(NewScriptFile);\r | |
2864 | return (EFI_OUT_OF_RESOURCES);\r | |
2865 | }\r | |
4dc0d578 | 2866 | CommandLine2 = AllocateZeroPool(PrintBuffSize);\r |
3c865f20 | 2867 | if (CommandLine2 == NULL) {\r |
2868 | FreePool(CommandLine);\r | |
2869 | DeleteScriptFileStruct(NewScriptFile);\r | |
2870 | return (EFI_OUT_OF_RESOURCES);\r | |
2871 | }\r | |
a405b86d | 2872 | \r |
2873 | for ( NewScriptFile->CurrentCommand = (SCRIPT_COMMAND_LIST *)GetFirstNode(&NewScriptFile->CommandList)\r | |
2874 | ; !IsNull(&NewScriptFile->CommandList, &NewScriptFile->CurrentCommand->Link)\r | |
2875 | ; // conditional increment in the body of the loop\r | |
2876 | ){\r | |
3c865f20 | 2877 | ASSERT(CommandLine2 != NULL);\r |
4dc0d578 QS |
2878 | StrnCpyS( CommandLine2, \r |
2879 | PrintBuffSize/sizeof(CHAR16), \r | |
2880 | NewScriptFile->CurrentCommand->Cl,\r | |
2881 | PrintBuffSize/sizeof(CHAR16) - 1\r | |
e75390f0 | 2882 | );\r |
a405b86d | 2883 | \r |
14810d6b QS |
2884 | SaveBufferList(&OldBufferList);\r |
2885 | \r | |
a405b86d | 2886 | //\r |
2887 | // NULL out comments\r | |
2888 | //\r | |
2889 | for (CommandLine3 = CommandLine2 ; CommandLine3 != NULL && *CommandLine3 != CHAR_NULL ; CommandLine3++) {\r | |
2890 | if (*CommandLine3 == L'^') {\r | |
4b6b543e | 2891 | if ( *(CommandLine3+1) == L':') {\r |
a405b86d | 2892 | CopyMem(CommandLine3, CommandLine3+1, StrSize(CommandLine3) - sizeof(CommandLine3[0]));\r |
4b6b543e QS |
2893 | } else if (*(CommandLine3+1) == L'#') {\r |
2894 | CommandLine3++;\r | |
a405b86d | 2895 | }\r |
2896 | } else if (*CommandLine3 == L'#') {\r | |
2897 | *CommandLine3 = CHAR_NULL;\r | |
2898 | }\r | |
2899 | }\r | |
2900 | \r | |
2901 | if (CommandLine2 != NULL && StrLen(CommandLine2) >= 1) {\r | |
2902 | //\r | |
2903 | // Due to variability in starting the find and replace action we need to have both buffers the same.\r | |
2904 | //\r | |
4dc0d578 QS |
2905 | StrnCpyS( CommandLine, \r |
2906 | PrintBuffSize/sizeof(CHAR16), \r | |
2907 | CommandLine2,\r | |
2908 | PrintBuffSize/sizeof(CHAR16) - 1\r | |
e75390f0 | 2909 | );\r |
a405b86d | 2910 | \r |
2911 | //\r | |
2912 | // Remove the %0 to %9 from the command line (if we have some arguments)\r | |
2913 | //\r | |
2914 | if (NewScriptFile->Argv != NULL) {\r | |
2915 | switch (NewScriptFile->Argc) {\r | |
2916 | default:\r | |
d1c275c6 | 2917 | Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PrintBuffSize, L"%9", NewScriptFile->Argv[9], FALSE, FALSE);\r |
a405b86d | 2918 | ASSERT_EFI_ERROR(Status);\r |
2919 | case 9:\r | |
d1c275c6 | 2920 | Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PrintBuffSize, L"%8", NewScriptFile->Argv[8], FALSE, FALSE);\r |
a405b86d | 2921 | ASSERT_EFI_ERROR(Status);\r |
2922 | case 8:\r | |
d1c275c6 | 2923 | Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PrintBuffSize, L"%7", NewScriptFile->Argv[7], FALSE, FALSE);\r |
a405b86d | 2924 | ASSERT_EFI_ERROR(Status);\r |
2925 | case 7:\r | |
d1c275c6 | 2926 | Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PrintBuffSize, L"%6", NewScriptFile->Argv[6], FALSE, FALSE);\r |
a405b86d | 2927 | ASSERT_EFI_ERROR(Status);\r |
2928 | case 6:\r | |
d1c275c6 | 2929 | Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PrintBuffSize, L"%5", NewScriptFile->Argv[5], FALSE, FALSE);\r |
a405b86d | 2930 | ASSERT_EFI_ERROR(Status);\r |
2931 | case 5:\r | |
d1c275c6 | 2932 | Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PrintBuffSize, L"%4", NewScriptFile->Argv[4], FALSE, FALSE);\r |
a405b86d | 2933 | ASSERT_EFI_ERROR(Status);\r |
2934 | case 4:\r | |
d1c275c6 | 2935 | Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PrintBuffSize, L"%3", NewScriptFile->Argv[3], FALSE, FALSE);\r |
a405b86d | 2936 | ASSERT_EFI_ERROR(Status);\r |
2937 | case 3:\r | |
d1c275c6 | 2938 | Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PrintBuffSize, L"%2", NewScriptFile->Argv[2], FALSE, FALSE);\r |
a405b86d | 2939 | ASSERT_EFI_ERROR(Status);\r |
2940 | case 2:\r | |
d1c275c6 | 2941 | Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PrintBuffSize, L"%1", NewScriptFile->Argv[1], FALSE, FALSE);\r |
a405b86d | 2942 | ASSERT_EFI_ERROR(Status);\r |
2943 | case 1:\r | |
d1c275c6 | 2944 | Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PrintBuffSize, L"%0", NewScriptFile->Argv[0], FALSE, FALSE);\r |
a405b86d | 2945 | ASSERT_EFI_ERROR(Status);\r |
2946 | break;\r | |
2947 | case 0:\r | |
2948 | break;\r | |
2949 | }\r | |
2950 | }\r | |
4dc0d578 QS |
2951 | Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PrintBuffSize, L"%1", L"\"\"", FALSE, FALSE);\r |
2952 | Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PrintBuffSize, L"%2", L"\"\"", FALSE, FALSE);\r | |
2953 | Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PrintBuffSize, L"%3", L"\"\"", FALSE, FALSE);\r | |
2954 | Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PrintBuffSize, L"%4", L"\"\"", FALSE, FALSE);\r | |
2955 | Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PrintBuffSize, L"%5", L"\"\"", FALSE, FALSE);\r | |
2956 | Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PrintBuffSize, L"%6", L"\"\"", FALSE, FALSE);\r | |
2957 | Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PrintBuffSize, L"%7", L"\"\"", FALSE, FALSE);\r | |
2958 | Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PrintBuffSize, L"%8", L"\"\"", FALSE, FALSE);\r | |
2959 | Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PrintBuffSize, L"%9", L"\"\"", FALSE, FALSE);\r | |
2960 | \r | |
2961 | StrnCpyS( CommandLine2, \r | |
2962 | PrintBuffSize/sizeof(CHAR16), \r | |
2963 | CommandLine,\r | |
2964 | PrintBuffSize/sizeof(CHAR16) - 1\r | |
e75390f0 | 2965 | );\r |
a405b86d | 2966 | \r |
2967 | LastCommand = NewScriptFile->CurrentCommand;\r | |
2968 | \r | |
2969 | for (CommandLine3 = CommandLine2 ; CommandLine3[0] == L' ' ; CommandLine3++);\r | |
2970 | \r | |
3c865f20 | 2971 | if (CommandLine3 != NULL && CommandLine3[0] == L':' ) {\r |
a405b86d | 2972 | //\r |
2973 | // This line is a goto target / label\r | |
2974 | //\r | |
2975 | } else {\r | |
2976 | if (CommandLine3 != NULL && StrLen(CommandLine3) > 0) {\r | |
848997b5 | 2977 | if (CommandLine3[0] == L'@') {\r |
2978 | //\r | |
2979 | // We need to save the current echo state\r | |
2980 | // and disable echo for just this command.\r | |
2981 | //\r | |
2982 | PreCommandEchoState = ShellCommandGetEchoState();\r | |
2983 | ShellCommandSetEchoState(FALSE);\r | |
a308e058 | 2984 | Status = RunCommand(CommandLine3+1);\r |
848997b5 | 2985 | \r |
2986 | //\r | |
284e034f | 2987 | // If command was "@echo -off" or "@echo -on" then don't restore echo state\r |
848997b5 | 2988 | //\r |
284e034f CP |
2989 | if (StrCmp (L"@echo -off", CommandLine3) != 0 &&\r |
2990 | StrCmp (L"@echo -on", CommandLine3) != 0) {\r | |
2991 | //\r | |
2992 | // Now restore the pre-'@' echo state.\r | |
2993 | //\r | |
2994 | ShellCommandSetEchoState(PreCommandEchoState);\r | |
2995 | }\r | |
848997b5 | 2996 | } else {\r |
0d11446d | 2997 | if (ShellCommandGetEchoState()) {\r |
2998 | CurDir = ShellInfoObject.NewEfiShellProtocol->GetEnv(L"cwd");\r | |
2999 | if (CurDir != NULL && StrLen(CurDir) > 1) {\r | |
3000 | ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_CURDIR), ShellInfoObject.HiiHandle, CurDir);\r | |
3001 | } else {\r | |
3002 | ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_SHELL), ShellInfoObject.HiiHandle);\r | |
3003 | }\r | |
3004 | ShellPrintEx(-1, -1, L"%s\r\n", CommandLine2);\r | |
3005 | }\r | |
a308e058 | 3006 | Status = RunCommand(CommandLine3);\r |
848997b5 | 3007 | }\r |
a405b86d | 3008 | }\r |
3009 | \r | |
3010 | if (ShellCommandGetScriptExit()) {\r | |
5b5cd144 CP |
3011 | //\r |
3012 | // ShellCommandGetExitCode() always returns a UINT64\r | |
3013 | //\r | |
a308e058 | 3014 | UnicodeSPrint(LeString, sizeof(LeString), L"0x%Lx", ShellCommandGetExitCode());\r |
5b5cd144 CP |
3015 | DEBUG_CODE(InternalEfiShellSetEnv(L"debuglasterror", LeString, TRUE););\r |
3016 | InternalEfiShellSetEnv(L"lasterror", LeString, TRUE);\r | |
b6b22b13 | 3017 | \r |
3018 | ShellCommandRegisterExit(FALSE, 0);\r | |
a405b86d | 3019 | Status = EFI_SUCCESS;\r |
14810d6b | 3020 | RestoreBufferList(&OldBufferList);\r |
a405b86d | 3021 | break;\r |
3022 | }\r | |
c6a7fef8 | 3023 | if (ShellGetExecutionBreakFlag()) {\r |
14810d6b | 3024 | RestoreBufferList(&OldBufferList);\r |
c6a7fef8 ED |
3025 | break;\r |
3026 | }\r | |
a405b86d | 3027 | if (EFI_ERROR(Status)) {\r |
14810d6b | 3028 | RestoreBufferList(&OldBufferList);\r |
a405b86d | 3029 | break;\r |
3030 | }\r | |
3031 | if (ShellCommandGetExit()) {\r | |
14810d6b | 3032 | RestoreBufferList(&OldBufferList);\r |
a405b86d | 3033 | break;\r |
3034 | }\r | |
3035 | }\r | |
3036 | //\r | |
3037 | // If that commend did not update the CurrentCommand then we need to advance it...\r | |
3038 | //\r | |
3039 | if (LastCommand == NewScriptFile->CurrentCommand) {\r | |
3040 | NewScriptFile->CurrentCommand = (SCRIPT_COMMAND_LIST *)GetNextNode(&NewScriptFile->CommandList, &NewScriptFile->CurrentCommand->Link);\r | |
3041 | if (!IsNull(&NewScriptFile->CommandList, &NewScriptFile->CurrentCommand->Link)) {\r | |
3042 | NewScriptFile->CurrentCommand->Reset = TRUE;\r | |
3043 | }\r | |
3044 | }\r | |
3045 | } else {\r | |
3046 | NewScriptFile->CurrentCommand = (SCRIPT_COMMAND_LIST *)GetNextNode(&NewScriptFile->CommandList, &NewScriptFile->CurrentCommand->Link);\r | |
3047 | if (!IsNull(&NewScriptFile->CommandList, &NewScriptFile->CurrentCommand->Link)) {\r | |
3048 | NewScriptFile->CurrentCommand->Reset = TRUE;\r | |
3049 | }\r | |
3050 | }\r | |
14810d6b | 3051 | RestoreBufferList(&OldBufferList);\r |
a405b86d | 3052 | }\r |
3053 | \r | |
a405b86d | 3054 | \r |
3055 | FreePool(CommandLine);\r | |
3056 | FreePool(CommandLine2);\r | |
3057 | ShellCommandSetNewScript (NULL);\r | |
733f138d | 3058 | \r |
3059 | //\r | |
3060 | // Only if this was the last script reset the state.\r | |
3061 | //\r | |
3062 | if (ShellCommandGetCurrentScriptFile()==NULL) {\r | |
3063 | ShellCommandSetEchoState(PreScriptEchoState);\r | |
3064 | }\r | |
a405b86d | 3065 | return (EFI_SUCCESS);\r |
3066 | }\r | |
3067 | \r | |
3068 | /**\r | |
3069 | Function to process a NSH script file.\r | |
3070 | \r | |
3071 | @param[in] ScriptPath Pointer to the script file name (including file system path).\r | |
e958b946 JC |
3072 | @param[in] Handle the handle of the script file already opened.\r |
3073 | @param[in] CmdLine the command line to run.\r | |
3074 | @param[in] ParamProtocol the shell parameters protocol pointer\r | |
a405b86d | 3075 | \r |
d08a5464 | 3076 | @retval EFI_SUCCESS the script completed successfully\r |
a405b86d | 3077 | **/\r |
3078 | EFI_STATUS\r | |
3079 | EFIAPI\r | |
3080 | RunScriptFile (\r | |
a308e058 RN |
3081 | IN CONST CHAR16 *ScriptPath,\r |
3082 | IN SHELL_FILE_HANDLE Handle OPTIONAL,\r | |
3083 | IN CONST CHAR16 *CmdLine,\r | |
3084 | IN EFI_SHELL_PARAMETERS_PROTOCOL *ParamProtocol\r | |
a405b86d | 3085 | )\r |
3086 | {\r | |
3087 | EFI_STATUS Status;\r | |
3088 | SHELL_FILE_HANDLE FileHandle;\r | |
e958b946 JC |
3089 | UINTN Argc;\r |
3090 | CHAR16 **Argv;\r | |
a405b86d | 3091 | \r |
3092 | if (ShellIsFile(ScriptPath) != EFI_SUCCESS) {\r | |
3093 | return (EFI_INVALID_PARAMETER);\r | |
3094 | }\r | |
3095 | \r | |
e958b946 JC |
3096 | //\r |
3097 | // get the argc and argv updated for scripts\r | |
3098 | //\r | |
d1c275c6 | 3099 | Status = UpdateArgcArgv(ParamProtocol, CmdLine, Script_File_Name, &Argv, &Argc);\r |
e958b946 | 3100 | if (!EFI_ERROR(Status)) {\r |
a405b86d | 3101 | \r |
e958b946 JC |
3102 | if (Handle == NULL) {\r |
3103 | //\r | |
3104 | // open the file\r | |
3105 | //\r | |
3106 | Status = ShellOpenFileByName(ScriptPath, &FileHandle, EFI_FILE_MODE_READ, 0);\r | |
3107 | if (!EFI_ERROR(Status)) {\r | |
3108 | //\r | |
3109 | // run it\r | |
3110 | //\r | |
a308e058 | 3111 | Status = RunScriptFileHandle(FileHandle, ScriptPath);\r |
a405b86d | 3112 | \r |
e958b946 JC |
3113 | //\r |
3114 | // now close the file\r | |
3115 | //\r | |
3116 | ShellCloseFile(&FileHandle);\r | |
3117 | }\r | |
3118 | } else {\r | |
a308e058 | 3119 | Status = RunScriptFileHandle(Handle, ScriptPath);\r |
e958b946 JC |
3120 | }\r |
3121 | }\r | |
3122 | \r | |
3123 | //\r | |
d08a5464 | 3124 | // This is guaranteed to be called after UpdateArgcArgv no matter what else happened.\r |
e958b946 JC |
3125 | // This is safe even if the update API failed. In this case, it may be a no-op.\r |
3126 | //\r | |
3127 | RestoreArgcArgv(ParamProtocol, &Argv, &Argc);\r | |
a405b86d | 3128 | \r |
3129 | return (Status);\r | |
3130 | }\r | |
00534bc3 | 3131 | \r |