]>
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 |
00534bc3 | 216 | TempSpot = FindFirstCharacter(CmdLine, L"|", L'^');\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 |
00534bc3 | 239 | TempSpot = FindFirstCharacter(TempSpot + 1, L"|", L'^');\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 QS |
718 | ShellFreeEnvVarList ();\r |
719 | \r | |
a308e058 RN |
720 | if (ShellCommandGetExit()) {\r |
721 | return ((EFI_STATUS)ShellCommandGetExitCode());\r | |
b6b22b13 | 722 | }\r |
a308e058 | 723 | return (Status);\r |
a405b86d | 724 | }\r |
725 | \r | |
726 | /**\r | |
727 | Sets all the alias' that were registered with the ShellCommandLib library.\r | |
728 | \r | |
d08a5464 | 729 | @retval EFI_SUCCESS all init commands were run successfully.\r |
a405b86d | 730 | **/\r |
731 | EFI_STATUS\r | |
732 | EFIAPI\r | |
733 | SetBuiltInAlias(\r | |
734 | )\r | |
735 | {\r | |
736 | EFI_STATUS Status;\r | |
737 | CONST ALIAS_LIST *List;\r | |
738 | ALIAS_LIST *Node;\r | |
739 | \r | |
740 | //\r | |
741 | // Get all the commands we want to run\r | |
742 | //\r | |
743 | List = ShellCommandGetInitAliasList();\r | |
744 | \r | |
745 | //\r | |
746 | // for each command in the List\r | |
747 | //\r | |
748 | for ( Node = (ALIAS_LIST*)GetFirstNode(&List->Link)\r | |
749 | ; !IsNull (&List->Link, &Node->Link)\r | |
750 | ; Node = (ALIAS_LIST *)GetNextNode(&List->Link, &Node->Link)\r | |
751 | ){\r | |
752 | //\r | |
753 | // install the alias'\r | |
754 | //\r | |
755 | Status = InternalSetAlias(Node->CommandString, Node->Alias, TRUE);\r | |
756 | ASSERT_EFI_ERROR(Status);\r | |
757 | }\r | |
758 | return (EFI_SUCCESS);\r | |
759 | }\r | |
760 | \r | |
761 | /**\r | |
762 | Internal function to determine if 2 command names are really the same.\r | |
763 | \r | |
764 | @param[in] Command1 The pointer to the first command name.\r | |
765 | @param[in] Command2 The pointer to the second command name.\r | |
766 | \r | |
767 | @retval TRUE The 2 command names are the same.\r | |
768 | @retval FALSE The 2 command names are not the same.\r | |
769 | **/\r | |
770 | BOOLEAN\r | |
771 | EFIAPI\r | |
772 | IsCommand(\r | |
773 | IN CONST CHAR16 *Command1,\r | |
774 | IN CONST CHAR16 *Command2\r | |
775 | )\r | |
776 | {\r | |
777 | if (StringNoCaseCompare(&Command1, &Command2) == 0) {\r | |
778 | return (TRUE);\r | |
779 | }\r | |
780 | return (FALSE);\r | |
781 | }\r | |
782 | \r | |
783 | /**\r | |
784 | Internal function to determine if a command is a script only command.\r | |
785 | \r | |
786 | @param[in] CommandName The pointer to the command name.\r | |
787 | \r | |
788 | @retval TRUE The command is a script only command.\r | |
789 | @retval FALSE The command is not a script only command.\r | |
790 | **/\r | |
791 | BOOLEAN\r | |
792 | EFIAPI\r | |
793 | IsScriptOnlyCommand(\r | |
794 | IN CONST CHAR16 *CommandName\r | |
795 | )\r | |
796 | {\r | |
797 | if (IsCommand(CommandName, L"for")\r | |
798 | ||IsCommand(CommandName, L"endfor")\r | |
799 | ||IsCommand(CommandName, L"if")\r | |
800 | ||IsCommand(CommandName, L"else")\r | |
801 | ||IsCommand(CommandName, L"endif")\r | |
802 | ||IsCommand(CommandName, L"goto")) {\r | |
803 | return (TRUE);\r | |
804 | }\r | |
805 | return (FALSE);\r | |
806 | }\r | |
807 | \r | |
a405b86d | 808 | /**\r |
809 | This function will populate the 2 device path protocol parameters based on the\r | |
810 | global gImageHandle. The DevPath will point to the device path for the handle that has\r | |
811 | loaded image protocol installed on it. The FilePath will point to the device path\r | |
812 | for the file that was loaded.\r | |
813 | \r | |
d08a5464 JP |
814 | @param[in, out] DevPath On a successful return the device path to the loaded image.\r |
815 | @param[in, out] FilePath On a successful return the device path to the file.\r | |
a405b86d | 816 | \r |
d08a5464 | 817 | @retval EFI_SUCCESS The 2 device paths were successfully returned.\r |
a405b86d | 818 | @retval other A error from gBS->HandleProtocol.\r |
819 | \r | |
820 | @sa HandleProtocol\r | |
821 | **/\r | |
822 | EFI_STATUS\r | |
823 | EFIAPI\r | |
824 | GetDevicePathsForImageAndFile (\r | |
825 | IN OUT EFI_DEVICE_PATH_PROTOCOL **DevPath,\r | |
826 | IN OUT EFI_DEVICE_PATH_PROTOCOL **FilePath\r | |
827 | )\r | |
828 | {\r | |
829 | EFI_STATUS Status;\r | |
830 | EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;\r | |
831 | EFI_DEVICE_PATH_PROTOCOL *ImageDevicePath;\r | |
832 | \r | |
833 | ASSERT(DevPath != NULL);\r | |
834 | ASSERT(FilePath != NULL);\r | |
835 | \r | |
836 | Status = gBS->OpenProtocol (\r | |
837 | gImageHandle,\r | |
838 | &gEfiLoadedImageProtocolGuid,\r | |
839 | (VOID**)&LoadedImage,\r | |
840 | gImageHandle,\r | |
841 | NULL,\r | |
842 | EFI_OPEN_PROTOCOL_GET_PROTOCOL\r | |
843 | );\r | |
844 | if (!EFI_ERROR (Status)) {\r | |
845 | Status = gBS->OpenProtocol (\r | |
846 | LoadedImage->DeviceHandle,\r | |
847 | &gEfiDevicePathProtocolGuid,\r | |
848 | (VOID**)&ImageDevicePath,\r | |
849 | gImageHandle,\r | |
850 | NULL,\r | |
851 | EFI_OPEN_PROTOCOL_GET_PROTOCOL\r | |
852 | );\r | |
853 | if (!EFI_ERROR (Status)) {\r | |
854 | *DevPath = DuplicateDevicePath (ImageDevicePath);\r | |
855 | *FilePath = DuplicateDevicePath (LoadedImage->FilePath);\r | |
a49f6a2f | 856 | gBS->CloseProtocol(\r |
857 | LoadedImage->DeviceHandle,\r | |
858 | &gEfiDevicePathProtocolGuid,\r | |
859 | gImageHandle,\r | |
860 | NULL);\r | |
a405b86d | 861 | }\r |
862 | gBS->CloseProtocol(\r | |
863 | gImageHandle,\r | |
864 | &gEfiLoadedImageProtocolGuid,\r | |
865 | gImageHandle,\r | |
866 | NULL);\r | |
867 | }\r | |
868 | return (Status);\r | |
869 | }\r | |
870 | \r | |
a405b86d | 871 | /**\r |
872 | Process all Uefi Shell 2.0 command line options.\r | |
873 | \r | |
874 | see Uefi Shell 2.0 section 3.2 for full details.\r | |
875 | \r | |
876 | the command line must resemble the following:\r | |
877 | \r | |
878 | shell.efi [ShellOpt-options] [options] [file-name [file-name-options]]\r | |
879 | \r | |
880 | ShellOpt-options Options which control the initialization behavior of the shell.\r | |
881 | These options are read from the EFI global variable "ShellOpt"\r | |
882 | and are processed before options or file-name.\r | |
883 | \r | |
884 | options Options which control the initialization behavior of the shell.\r | |
885 | \r | |
886 | file-name The name of a UEFI shell application or script to be executed\r | |
887 | after initialization is complete. By default, if file-name is\r | |
888 | specified, then -nostartup is implied. Scripts are not supported\r | |
889 | by level 0.\r | |
890 | \r | |
891 | file-name-options The command-line options that are passed to file-name when it\r | |
892 | is invoked.\r | |
893 | \r | |
894 | This will initialize the ShellInfoObject.ShellInitSettings global variable.\r | |
895 | \r | |
896 | @retval EFI_SUCCESS The variable is initialized.\r | |
897 | **/\r | |
898 | EFI_STATUS\r | |
899 | EFIAPI\r | |
900 | ProcessCommandLine(\r | |
901 | VOID\r | |
902 | )\r | |
903 | {\r | |
23385d63 BJ |
904 | UINTN Size;\r |
905 | UINTN LoopVar;\r | |
906 | CHAR16 *CurrentArg;\r | |
907 | CHAR16 *DelayValueStr;\r | |
908 | UINT64 DelayValue;\r | |
909 | EFI_STATUS Status;\r | |
910 | EFI_UNICODE_COLLATION_PROTOCOL *UnicodeCollation;\r | |
911 | \r | |
912 | // `file-name-options` will contain arguments to `file-name` that we don't\r | |
913 | // know about. This would cause ShellCommandLineParse to error, so we parse\r | |
914 | // arguments manually, ignoring those after the first thing that doesn't look\r | |
915 | // like a shell option (which is assumed to be `file-name`).\r | |
916 | \r | |
917 | Status = gBS->LocateProtocol (\r | |
f3a51e98 | 918 | &gEfiUnicodeCollation2ProtocolGuid,\r |
23385d63 BJ |
919 | NULL,\r |
920 | (VOID **) &UnicodeCollation\r | |
921 | );\r | |
922 | if (EFI_ERROR (Status)) {\r | |
f3a51e98 MH |
923 | Status = gBS->LocateProtocol (\r |
924 | &gEfiUnicodeCollationProtocolGuid,\r | |
925 | NULL,\r | |
926 | (VOID **) &UnicodeCollation\r | |
927 | );\r | |
928 | if (EFI_ERROR (Status)) {\r | |
929 | return Status;\r | |
930 | }\r | |
23385d63 BJ |
931 | }\r |
932 | \r | |
933 | // Set default options\r | |
934 | ShellInfoObject.ShellInitSettings.BitUnion.Bits.Startup = FALSE;\r | |
935 | ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoStartup = FALSE;\r | |
936 | ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoConsoleOut = FALSE;\r | |
937 | ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoConsoleIn = FALSE;\r | |
938 | ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoInterrupt = FALSE;\r | |
939 | ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoMap = FALSE;\r | |
940 | ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoVersion = FALSE;\r | |
941 | ShellInfoObject.ShellInitSettings.BitUnion.Bits.Delay = FALSE;\r | |
942 | ShellInfoObject.ShellInitSettings.BitUnion.Bits.Exit = FALSE;\r | |
dcbdb8bf | 943 | ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoNest = FALSE;\r |
23385d63 BJ |
944 | ShellInfoObject.ShellInitSettings.Delay = 5;\r |
945 | \r | |
e9d19a80 CP |
946 | //\r |
947 | // Start LoopVar at 0 to parse only optional arguments at Argv[0]\r | |
948 | // and parse other parameters from Argv[1]. This is for use case that\r | |
949 | // UEFI Shell boot option is created, and OptionalData is provided\r | |
950 | // that starts with shell command-line options.\r | |
951 | //\r | |
952 | for (LoopVar = 0 ; LoopVar < gEfiShellParametersProtocol->Argc ; LoopVar++) {\r | |
23385d63 BJ |
953 | CurrentArg = gEfiShellParametersProtocol->Argv[LoopVar];\r |
954 | if (UnicodeCollation->StriColl (\r | |
955 | UnicodeCollation,\r | |
956 | L"-startup",\r | |
957 | CurrentArg\r | |
958 | ) == 0) {\r | |
959 | ShellInfoObject.ShellInitSettings.BitUnion.Bits.Startup = TRUE;\r | |
3c865f20 | 960 | }\r |
23385d63 BJ |
961 | else if (UnicodeCollation->StriColl (\r |
962 | UnicodeCollation,\r | |
963 | L"-nostartup",\r | |
964 | CurrentArg\r | |
965 | ) == 0) {\r | |
966 | ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoStartup = TRUE;\r | |
967 | }\r | |
968 | else if (UnicodeCollation->StriColl (\r | |
969 | UnicodeCollation,\r | |
970 | L"-noconsoleout",\r | |
971 | CurrentArg\r | |
972 | ) == 0) {\r | |
973 | ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoConsoleOut = TRUE;\r | |
974 | }\r | |
975 | else if (UnicodeCollation->StriColl (\r | |
976 | UnicodeCollation,\r | |
977 | L"-noconsolein",\r | |
978 | CurrentArg\r | |
979 | ) == 0) {\r | |
980 | ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoConsoleIn = TRUE;\r | |
981 | }\r | |
982 | else if (UnicodeCollation->StriColl (\r | |
983 | UnicodeCollation,\r | |
984 | L"-nointerrupt",\r | |
985 | CurrentArg\r | |
986 | ) == 0) {\r | |
987 | ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoInterrupt = TRUE;\r | |
988 | }\r | |
989 | else if (UnicodeCollation->StriColl (\r | |
990 | UnicodeCollation,\r | |
991 | L"-nomap",\r | |
992 | CurrentArg\r | |
993 | ) == 0) {\r | |
994 | ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoMap = TRUE;\r | |
995 | }\r | |
996 | else if (UnicodeCollation->StriColl (\r | |
997 | UnicodeCollation,\r | |
998 | L"-noversion",\r | |
999 | CurrentArg\r | |
1000 | ) == 0) {\r | |
1001 | ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoVersion = TRUE;\r | |
1002 | }\r | |
dcbdb8bf QS |
1003 | else if (UnicodeCollation->StriColl (\r |
1004 | UnicodeCollation,\r | |
1005 | L"-nonest",\r | |
1006 | CurrentArg\r | |
1007 | ) == 0) {\r | |
1008 | ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoNest = TRUE;\r | |
1009 | }\r | |
23385d63 BJ |
1010 | else if (UnicodeCollation->StriColl (\r |
1011 | UnicodeCollation,\r | |
1012 | L"-delay",\r | |
1013 | CurrentArg\r | |
1014 | ) == 0) {\r | |
1015 | ShellInfoObject.ShellInitSettings.BitUnion.Bits.Delay = TRUE;\r | |
1016 | // Check for optional delay value following "-delay"\r | |
1017 | DelayValueStr = gEfiShellParametersProtocol->Argv[LoopVar + 1];\r | |
1018 | if (DelayValueStr != NULL){\r | |
1019 | if (*DelayValueStr == L':') {\r | |
1020 | DelayValueStr++;\r | |
1021 | }\r | |
1022 | if (!EFI_ERROR(ShellConvertStringToUint64 (\r | |
1023 | DelayValueStr,\r | |
1024 | &DelayValue,\r | |
1025 | FALSE,\r | |
1026 | FALSE\r | |
1027 | ))) {\r | |
1028 | ShellInfoObject.ShellInitSettings.Delay = (UINTN)DelayValue;\r | |
1029 | LoopVar++;\r | |
1030 | }\r | |
1031 | }\r | |
1032 | } else if (UnicodeCollation->StriColl (\r | |
1033 | UnicodeCollation,\r | |
1034 | L"-_exit",\r | |
1035 | CurrentArg\r | |
1036 | ) == 0) {\r | |
1037 | ShellInfoObject.ShellInitSettings.BitUnion.Bits.Exit = TRUE;\r | |
1038 | } else if (StrnCmp (L"-", CurrentArg, 1) == 0) {\r | |
d08a5464 | 1039 | // Unrecognized option\r |
23385d63 BJ |
1040 | ShellPrintHiiEx(-1, -1, NULL,\r |
1041 | STRING_TOKEN (STR_GEN_PROBLEM),\r | |
1042 | ShellInfoObject.HiiHandle,\r | |
1043 | CurrentArg\r | |
1044 | );\r | |
1045 | return EFI_INVALID_PARAMETER;\r | |
1046 | } else {\r | |
e9d19a80 CP |
1047 | //\r |
1048 | // First argument should be Shell.efi image name\r | |
1049 | //\r | |
1050 | if (LoopVar == 0) {\r | |
1051 | continue;\r | |
1052 | }\r | |
1053 | \r | |
7f79b01e | 1054 | ShellInfoObject.ShellInitSettings.FileName = AllocateCopyPool(StrSize(CurrentArg), CurrentArg);\r |
23385d63 BJ |
1055 | if (ShellInfoObject.ShellInitSettings.FileName == NULL) {\r |
1056 | return (EFI_OUT_OF_RESOURCES);\r | |
1057 | }\r | |
1058 | //\r | |
1059 | // We found `file-name`.\r | |
1060 | //\r | |
1061 | ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoStartup = 1;\r | |
23385d63 BJ |
1062 | LoopVar++;\r |
1063 | \r | |
1064 | // Add `file-name-options`\r | |
ba71f790 | 1065 | for (Size = 0 ; LoopVar < gEfiShellParametersProtocol->Argc ; LoopVar++) {\r |
23385d63 BJ |
1066 | ASSERT((ShellInfoObject.ShellInitSettings.FileOptions == NULL && Size == 0) || (ShellInfoObject.ShellInitSettings.FileOptions != NULL));\r |
1067 | StrnCatGrow(&ShellInfoObject.ShellInitSettings.FileOptions,\r | |
1068 | &Size,\r | |
1069 | L" ",\r | |
1070 | 0);\r | |
1071 | if (ShellInfoObject.ShellInitSettings.FileOptions == NULL) {\r | |
1072 | SHELL_FREE_NON_NULL(ShellInfoObject.ShellInitSettings.FileName);\r | |
1073 | return (EFI_OUT_OF_RESOURCES);\r | |
1074 | }\r | |
1075 | StrnCatGrow(&ShellInfoObject.ShellInitSettings.FileOptions,\r | |
1076 | &Size,\r | |
1077 | gEfiShellParametersProtocol->Argv[LoopVar],\r | |
1078 | 0);\r | |
1079 | if (ShellInfoObject.ShellInitSettings.FileOptions == NULL) {\r | |
1080 | SHELL_FREE_NON_NULL(ShellInfoObject.ShellInitSettings.FileName);\r | |
1081 | return (EFI_OUT_OF_RESOURCES);\r | |
a405b86d | 1082 | }\r |
1083 | }\r | |
a405b86d | 1084 | }\r |
1085 | }\r | |
1086 | \r | |
23385d63 | 1087 | // "-nointerrupt" overrides "-delay"\r |
733f138d | 1088 | if (ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoInterrupt) {\r |
1089 | ShellInfoObject.ShellInitSettings.Delay = 0;\r | |
a405b86d | 1090 | }\r |
a405b86d | 1091 | \r |
23385d63 | 1092 | return EFI_SUCCESS;\r |
a405b86d | 1093 | }\r |
1094 | \r | |
1095 | /**\r | |
1096 | Handles all interaction with the default startup script.\r | |
1097 | \r | |
1098 | this will check that the correct command line parameters were passed, handle the delay, and then start running the script.\r | |
1099 | \r | |
1100 | @param ImagePath the path to the image for shell. first place to look for the startup script\r | |
1101 | @param FilePath the path to the file for shell. second place to look for the startup script.\r | |
1102 | \r | |
1103 | @retval EFI_SUCCESS the variable is initialized.\r | |
1104 | **/\r | |
1105 | EFI_STATUS\r | |
1106 | EFIAPI\r | |
1107 | DoStartupScript(\r | |
a308e058 RN |
1108 | IN EFI_DEVICE_PATH_PROTOCOL *ImagePath,\r |
1109 | IN EFI_DEVICE_PATH_PROTOCOL *FilePath\r | |
a405b86d | 1110 | )\r |
1111 | {\r | |
1112 | EFI_STATUS Status;\r | |
490ce43d | 1113 | EFI_STATUS CalleeStatus;\r |
a405b86d | 1114 | UINTN Delay;\r |
1115 | EFI_INPUT_KEY Key;\r | |
1116 | SHELL_FILE_HANDLE FileHandle;\r | |
1117 | EFI_DEVICE_PATH_PROTOCOL *NewPath;\r | |
1118 | EFI_DEVICE_PATH_PROTOCOL *NamePath;\r | |
1119 | CHAR16 *FileStringPath;\r | |
733f138d | 1120 | CHAR16 *TempSpot;\r |
a405b86d | 1121 | UINTN NewSize;\r |
733f138d | 1122 | CONST CHAR16 *MapName;\r |
a405b86d | 1123 | \r |
1124 | Key.UnicodeChar = CHAR_NULL;\r | |
1125 | Key.ScanCode = 0;\r | |
1126 | FileHandle = NULL;\r | |
1127 | \r | |
1128 | if (!ShellInfoObject.ShellInitSettings.BitUnion.Bits.Startup && ShellInfoObject.ShellInitSettings.FileName != NULL) {\r | |
1129 | //\r | |
1130 | // launch something else instead\r | |
1131 | //\r | |
1132 | NewSize = StrSize(ShellInfoObject.ShellInitSettings.FileName);\r | |
1133 | if (ShellInfoObject.ShellInitSettings.FileOptions != NULL) {\r | |
1134 | NewSize += StrSize(ShellInfoObject.ShellInitSettings.FileOptions) + sizeof(CHAR16);\r | |
1135 | }\r | |
1136 | FileStringPath = AllocateZeroPool(NewSize);\r | |
3c865f20 | 1137 | if (FileStringPath == NULL) {\r |
1138 | return (EFI_OUT_OF_RESOURCES);\r | |
1139 | }\r | |
e75390f0 | 1140 | StrCpyS(FileStringPath, NewSize/sizeof(CHAR16), ShellInfoObject.ShellInitSettings.FileName);\r |
a405b86d | 1141 | if (ShellInfoObject.ShellInitSettings.FileOptions != NULL) {\r |
e75390f0 QS |
1142 | StrnCatS(FileStringPath, NewSize/sizeof(CHAR16), L" ", NewSize/sizeof(CHAR16) - StrLen(FileStringPath) -1);\r |
1143 | StrnCatS(FileStringPath, NewSize/sizeof(CHAR16), ShellInfoObject.ShellInitSettings.FileOptions, NewSize/sizeof(CHAR16) - StrLen(FileStringPath) -1);\r | |
a405b86d | 1144 | }\r |
490ce43d QS |
1145 | Status = RunShellCommand(FileStringPath, &CalleeStatus);\r |
1146 | if (ShellInfoObject.ShellInitSettings.BitUnion.Bits.Exit == TRUE) {\r | |
01e3a976 | 1147 | ShellCommandRegisterExit(gEfiShellProtocol->BatchIsActive(), (UINT64)CalleeStatus);\r |
490ce43d | 1148 | }\r |
a405b86d | 1149 | FreePool(FileStringPath);\r |
1150 | return (Status);\r | |
1151 | \r | |
1152 | }\r | |
1153 | \r | |
1154 | //\r | |
1155 | // for shell level 0 we do no scripts\r | |
1156 | // Without the Startup bit overriding we allow for nostartup to prevent scripts\r | |
1157 | //\r | |
1158 | if ( (PcdGet8(PcdShellSupportLevel) < 1)\r | |
1159 | || (ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoStartup && !ShellInfoObject.ShellInitSettings.BitUnion.Bits.Startup)\r | |
1160 | ){\r | |
1161 | return (EFI_SUCCESS);\r | |
1162 | }\r | |
1163 | \r | |
5559a41a | 1164 | gST->ConOut->EnableCursor(gST->ConOut, FALSE);\r |
a405b86d | 1165 | //\r |
1166 | // print out our warning and see if they press a key\r | |
1167 | //\r | |
284e034f | 1168 | for ( Status = EFI_UNSUPPORTED, Delay = ShellInfoObject.ShellInitSettings.Delay\r |
733f138d | 1169 | ; Delay != 0 && EFI_ERROR(Status)\r |
a405b86d | 1170 | ; Delay--\r |
1171 | ){\r | |
284e034f CP |
1172 | ShellPrintHiiEx(0, gST->ConOut->Mode->CursorRow, NULL, STRING_TOKEN (STR_SHELL_STARTUP_QUESTION), ShellInfoObject.HiiHandle, Delay);\r |
1173 | gBS->Stall (1000000);\r | |
733f138d | 1174 | if (!ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoConsoleIn) {\r |
1175 | Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);\r | |
1176 | }\r | |
a405b86d | 1177 | }\r |
1178 | ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_CRLF), ShellInfoObject.HiiHandle);\r | |
5559a41a | 1179 | gST->ConOut->EnableCursor(gST->ConOut, TRUE);\r |
a405b86d | 1180 | \r |
1181 | //\r | |
1182 | // ESC was pressed\r | |
1183 | //\r | |
1184 | if (Status == EFI_SUCCESS && Key.UnicodeChar == 0 && Key.ScanCode == SCAN_ESC) {\r | |
1185 | return (EFI_SUCCESS);\r | |
1186 | }\r | |
1187 | \r | |
a405b86d | 1188 | //\r |
733f138d | 1189 | // Try the first location (must be file system)\r |
a405b86d | 1190 | //\r |
733f138d | 1191 | MapName = ShellInfoObject.NewEfiShellProtocol->GetMapFromDevicePath(&ImagePath);\r |
1192 | if (MapName != NULL) {\r | |
1193 | FileStringPath = NULL;\r | |
1194 | NewSize = 0;\r | |
1195 | FileStringPath = StrnCatGrow(&FileStringPath, &NewSize, MapName, 0);\r | |
532691c8 | 1196 | if (FileStringPath == NULL) {\r |
1197 | Status = EFI_OUT_OF_RESOURCES;\r | |
1198 | } else {\r | |
1199 | TempSpot = StrStr(FileStringPath, L";");\r | |
1200 | if (TempSpot != NULL) {\r | |
1201 | *TempSpot = CHAR_NULL;\r | |
1202 | }\r | |
1203 | FileStringPath = StrnCatGrow(&FileStringPath, &NewSize, ((FILEPATH_DEVICE_PATH*)FilePath)->PathName, 0);\r | |
1204 | PathRemoveLastItem(FileStringPath);\r | |
1205 | FileStringPath = StrnCatGrow(&FileStringPath, &NewSize, mStartupScript, 0);\r | |
1206 | Status = ShellInfoObject.NewEfiShellProtocol->OpenFileByName(FileStringPath, &FileHandle, EFI_FILE_MODE_READ);\r | |
1207 | FreePool(FileStringPath);\r | |
733f138d | 1208 | }\r |
733f138d | 1209 | }\r |
a405b86d | 1210 | if (EFI_ERROR(Status)) {\r |
733f138d | 1211 | NamePath = FileDevicePath (NULL, mStartupScript);\r |
1212 | NewPath = AppendDevicePathNode (ImagePath, NamePath);\r | |
1213 | FreePool(NamePath);\r | |
1214 | \r | |
a405b86d | 1215 | //\r |
733f138d | 1216 | // Try the location\r |
a405b86d | 1217 | //\r |
a405b86d | 1218 | Status = InternalOpenFileDevicePath(NewPath, &FileHandle, EFI_FILE_MODE_READ, 0);\r |
733f138d | 1219 | FreePool(NewPath);\r |
a405b86d | 1220 | }\r |
a405b86d | 1221 | //\r |
1222 | // If we got a file, run it\r | |
1223 | //\r | |
733f138d | 1224 | if (!EFI_ERROR(Status) && FileHandle != NULL) {\r |
a308e058 | 1225 | Status = RunScriptFile (mStartupScript, FileHandle, L"", ShellInfoObject.NewShellParametersProtocol);\r |
a405b86d | 1226 | ShellInfoObject.NewEfiShellProtocol->CloseFile(FileHandle);\r |
1227 | } else {\r | |
3c865f20 | 1228 | FileStringPath = ShellFindFilePath(mStartupScript);\r |
1229 | if (FileStringPath == NULL) {\r | |
1230 | //\r | |
d08a5464 | 1231 | // we return success since we don't need to have a startup script\r |
3c865f20 | 1232 | //\r |
1233 | Status = EFI_SUCCESS;\r | |
1234 | ASSERT(FileHandle == NULL);\r | |
1235 | } else {\r | |
a308e058 | 1236 | Status = RunScriptFile(FileStringPath, NULL, L"", ShellInfoObject.NewShellParametersProtocol);\r |
3c865f20 | 1237 | FreePool(FileStringPath);\r |
1238 | }\r | |
a405b86d | 1239 | }\r |
1240 | \r | |
a405b86d | 1241 | \r |
1242 | return (Status);\r | |
1243 | }\r | |
1244 | \r | |
1245 | /**\r | |
1246 | Function to perform the shell prompt looping. It will do a single prompt,\r | |
1247 | dispatch the result, and then return. It is expected that the caller will\r | |
1248 | call this function in a loop many times.\r | |
1249 | \r | |
1250 | @retval EFI_SUCCESS\r | |
1251 | @retval RETURN_ABORTED\r | |
1252 | **/\r | |
1253 | EFI_STATUS\r | |
1254 | EFIAPI\r | |
1255 | DoShellPrompt (\r | |
1256 | VOID\r | |
1257 | )\r | |
1258 | {\r | |
1259 | UINTN Column;\r | |
1260 | UINTN Row;\r | |
1261 | CHAR16 *CmdLine;\r | |
1262 | CONST CHAR16 *CurDir;\r | |
1263 | UINTN BufferSize;\r | |
1264 | EFI_STATUS Status;\r | |
594e780b | 1265 | LIST_ENTRY OldBufferList;\r |
a405b86d | 1266 | \r |
1267 | CurDir = NULL;\r | |
1268 | \r | |
1269 | //\r | |
1270 | // Get screen setting to decide size of the command line buffer\r | |
1271 | //\r | |
1272 | gST->ConOut->QueryMode (gST->ConOut, gST->ConOut->Mode->Mode, &Column, &Row);\r | |
1273 | BufferSize = Column * Row * sizeof (CHAR16);\r | |
1274 | CmdLine = AllocateZeroPool (BufferSize);\r | |
1275 | if (CmdLine == NULL) {\r | |
1276 | return EFI_OUT_OF_RESOURCES;\r | |
1277 | }\r | |
1278 | \r | |
594e780b | 1279 | SaveBufferList(&OldBufferList);\r |
a405b86d | 1280 | CurDir = ShellInfoObject.NewEfiShellProtocol->GetEnv(L"cwd");\r |
1281 | \r | |
1282 | //\r | |
1283 | // Prompt for input\r | |
1284 | //\r | |
1285 | gST->ConOut->SetCursorPosition (gST->ConOut, 0, gST->ConOut->Mode->CursorRow);\r | |
1286 | \r | |
1287 | if (CurDir != NULL && StrLen(CurDir) > 1) {\r | |
1288 | ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_CURDIR), ShellInfoObject.HiiHandle, CurDir);\r | |
1289 | } else {\r | |
1290 | ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_SHELL), ShellInfoObject.HiiHandle);\r | |
1291 | }\r | |
1292 | \r | |
1293 | //\r | |
1294 | // Read a line from the console\r | |
1295 | //\r | |
1296 | Status = ShellInfoObject.NewEfiShellProtocol->ReadFile(ShellInfoObject.NewShellParametersProtocol->StdIn, &BufferSize, CmdLine);\r | |
1297 | \r | |
1298 | //\r | |
1299 | // Null terminate the string and parse it\r | |
1300 | //\r | |
1301 | if (!EFI_ERROR (Status)) {\r | |
1302 | CmdLine[BufferSize / sizeof (CHAR16)] = CHAR_NULL;\r | |
a308e058 | 1303 | Status = RunCommand(CmdLine);\r |
4922715d | 1304 | }\r |
a405b86d | 1305 | \r |
1306 | //\r | |
1307 | // Done with this command\r | |
1308 | //\r | |
594e780b | 1309 | RestoreBufferList(&OldBufferList);\r |
a405b86d | 1310 | FreePool (CmdLine);\r |
1311 | return Status;\r | |
1312 | }\r | |
1313 | \r | |
1314 | /**\r | |
1315 | Add a buffer to the Buffer To Free List for safely returning buffers to other\r | |
1316 | places without risking letting them modify internal shell information.\r | |
1317 | \r | |
1318 | @param Buffer Something to pass to FreePool when the shell is exiting.\r | |
1319 | **/\r | |
1320 | VOID*\r | |
1321 | EFIAPI\r | |
1322 | AddBufferToFreeList(\r | |
1323 | VOID *Buffer\r | |
1324 | )\r | |
1325 | {\r | |
1326 | BUFFER_LIST *BufferListEntry;\r | |
1327 | \r | |
1328 | if (Buffer == NULL) {\r | |
1329 | return (NULL);\r | |
1330 | }\r | |
1331 | \r | |
1332 | BufferListEntry = AllocateZeroPool(sizeof(BUFFER_LIST));\r | |
1333 | ASSERT(BufferListEntry != NULL);\r | |
1334 | BufferListEntry->Buffer = Buffer;\r | |
1335 | InsertTailList(&ShellInfoObject.BufferToFreeList.Link, &BufferListEntry->Link);\r | |
1336 | return (Buffer);\r | |
1337 | }\r | |
1338 | \r | |
14810d6b QS |
1339 | \r |
1340 | /**\r | |
1341 | Create a new buffer list and stores the old one to OldBufferList \r | |
1342 | \r | |
1343 | @param OldBufferList The temporary list head used to store the nodes in BufferToFreeList.\r | |
1344 | **/\r | |
1345 | VOID\r | |
1346 | SaveBufferList (\r | |
1347 | OUT LIST_ENTRY *OldBufferList\r | |
1348 | )\r | |
1349 | {\r | |
1350 | CopyMem (OldBufferList, &ShellInfoObject.BufferToFreeList.Link, sizeof (LIST_ENTRY));\r | |
1351 | InitializeListHead (&ShellInfoObject.BufferToFreeList.Link);\r | |
1352 | }\r | |
1353 | \r | |
1354 | /**\r | |
1355 | Restore previous nodes into BufferToFreeList .\r | |
1356 | \r | |
1357 | @param OldBufferList The temporary list head used to store the nodes in BufferToFreeList.\r | |
1358 | **/\r | |
1359 | VOID\r | |
1360 | RestoreBufferList (\r | |
1361 | IN OUT LIST_ENTRY *OldBufferList\r | |
1362 | )\r | |
1363 | {\r | |
1364 | FreeBufferList (&ShellInfoObject.BufferToFreeList);\r | |
1365 | CopyMem (&ShellInfoObject.BufferToFreeList.Link, OldBufferList, sizeof (LIST_ENTRY));\r | |
1366 | }\r | |
1367 | \r | |
1368 | \r | |
a405b86d | 1369 | /**\r |
1370 | Add a buffer to the Line History List\r | |
1371 | \r | |
1372 | @param Buffer The line buffer to add.\r | |
1373 | **/\r | |
1374 | VOID\r | |
1375 | EFIAPI\r | |
1376 | AddLineToCommandHistory(\r | |
1377 | IN CONST CHAR16 *Buffer\r | |
1378 | )\r | |
1379 | {\r | |
1380 | BUFFER_LIST *Node;\r | |
a4f138a4 QS |
1381 | BUFFER_LIST *Walker;\r |
1382 | UINT16 MaxHistoryCmdCount;\r | |
1383 | UINT16 Count;\r | |
dcbdb8bf | 1384 | \r |
a4f138a4 QS |
1385 | Count = 0;\r |
1386 | MaxHistoryCmdCount = PcdGet16(PcdShellMaxHistoryCommandCount);\r | |
1387 | \r | |
1388 | if (MaxHistoryCmdCount == 0) {\r | |
1389 | return ;\r | |
1390 | }\r | |
1391 | \r | |
a405b86d | 1392 | \r |
1393 | Node = AllocateZeroPool(sizeof(BUFFER_LIST));\r | |
1394 | ASSERT(Node != NULL);\r | |
7f79b01e | 1395 | Node->Buffer = AllocateCopyPool(StrSize(Buffer), Buffer);\r |
a405b86d | 1396 | ASSERT(Node->Buffer != NULL);\r |
a405b86d | 1397 | \r |
a4f138a4 QS |
1398 | for ( Walker = (BUFFER_LIST*)GetFirstNode(&ShellInfoObject.ViewingSettings.CommandHistory.Link)\r |
1399 | ; !IsNull(&ShellInfoObject.ViewingSettings.CommandHistory.Link, &Walker->Link)\r | |
1400 | ; Walker = (BUFFER_LIST*)GetNextNode(&ShellInfoObject.ViewingSettings.CommandHistory.Link, &Walker->Link)\r | |
1401 | ){\r | |
1402 | Count++;\r | |
1403 | }\r | |
1404 | if (Count < MaxHistoryCmdCount){\r | |
1405 | InsertTailList(&ShellInfoObject.ViewingSettings.CommandHistory.Link, &Node->Link);\r | |
1406 | } else {\r | |
1407 | Walker = (BUFFER_LIST*)GetFirstNode(&ShellInfoObject.ViewingSettings.CommandHistory.Link);\r | |
1408 | RemoveEntryList(&Walker->Link);\r | |
1409 | if (Walker->Buffer != NULL) {\r | |
1410 | FreePool(Walker->Buffer);\r | |
1411 | }\r | |
1412 | FreePool(Walker);\r | |
1413 | InsertTailList(&ShellInfoObject.ViewingSettings.CommandHistory.Link, &Node->Link);\r | |
1414 | }\r | |
a405b86d | 1415 | }\r |
1416 | \r | |
1417 | /**\r | |
1418 | Checks if a string is an alias for another command. If yes, then it replaces the alias name\r | |
1419 | with the correct command name.\r | |
1420 | \r | |
4ff7e37b ED |
1421 | @param[in, out] CommandString Upon entry the potential alias. Upon return the\r |
1422 | command name if it was an alias. If it was not\r | |
1423 | an alias it will be unchanged. This function may\r | |
1424 | change the buffer to fit the command name.\r | |
a405b86d | 1425 | \r |
1426 | @retval EFI_SUCCESS The name was changed.\r | |
1427 | @retval EFI_SUCCESS The name was not an alias.\r | |
1428 | @retval EFI_OUT_OF_RESOURCES A memory allocation failed.\r | |
1429 | **/\r | |
1430 | EFI_STATUS\r | |
1431 | EFIAPI\r | |
1432 | ShellConvertAlias(\r | |
1433 | IN OUT CHAR16 **CommandString\r | |
1434 | )\r | |
1435 | {\r | |
1436 | CONST CHAR16 *NewString;\r | |
1437 | \r | |
1438 | NewString = ShellInfoObject.NewEfiShellProtocol->GetAlias(*CommandString, NULL);\r | |
1439 | if (NewString == NULL) {\r | |
1440 | return (EFI_SUCCESS);\r | |
1441 | }\r | |
1442 | FreePool(*CommandString);\r | |
7f79b01e | 1443 | *CommandString = AllocateCopyPool(StrSize(NewString), NewString);\r |
a405b86d | 1444 | if (*CommandString == NULL) {\r |
1445 | return (EFI_OUT_OF_RESOURCES);\r | |
1446 | }\r | |
a405b86d | 1447 | return (EFI_SUCCESS);\r |
1448 | }\r | |
1449 | \r | |
d5b5440b JC |
1450 | /**\r |
1451 | This function will eliminate unreplaced (and therefore non-found) environment variables.\r | |
1452 | \r | |
1453 | @param[in,out] CmdLine The command line to update.\r | |
1454 | **/\r | |
1455 | EFI_STATUS\r | |
1456 | EFIAPI\r | |
1457 | StripUnreplacedEnvironmentVariables(\r | |
1458 | IN OUT CHAR16 *CmdLine\r | |
1459 | )\r | |
1460 | {\r | |
1461 | CHAR16 *FirstPercent;\r | |
1462 | CHAR16 *FirstQuote;\r | |
1463 | CHAR16 *SecondPercent;\r | |
1464 | CHAR16 *SecondQuote;\r | |
1465 | CHAR16 *CurrentLocator;\r | |
1466 | \r | |
1467 | for (CurrentLocator = CmdLine ; CurrentLocator != NULL ; ) {\r | |
a95cf8f0 JC |
1468 | FirstQuote = FindNextInstance(CurrentLocator, L"\"", TRUE);\r |
1469 | FirstPercent = FindNextInstance(CurrentLocator, L"%", TRUE);\r | |
1470 | SecondPercent = FirstPercent!=NULL?FindNextInstance(FirstPercent+1, L"%", TRUE):NULL;\r | |
d5b5440b JC |
1471 | if (FirstPercent == NULL || SecondPercent == NULL) {\r |
1472 | //\r | |
d08a5464 | 1473 | // If we ever don't have 2 % we are done.\r |
d5b5440b JC |
1474 | //\r |
1475 | break;\r | |
1476 | }\r | |
1477 | \r | |
fdd52bde | 1478 | if (FirstQuote!= NULL && FirstQuote < FirstPercent) {\r |
a95cf8f0 | 1479 | SecondQuote = FindNextInstance(FirstQuote+1, L"\"", TRUE);\r |
d5b5440b JC |
1480 | //\r |
1481 | // Quote is first found\r | |
1482 | //\r | |
a95cf8f0 | 1483 | \r |
d5b5440b JC |
1484 | if (SecondQuote < FirstPercent) {\r |
1485 | //\r | |
1486 | // restart after the pair of "\r | |
1487 | //\r | |
1488 | CurrentLocator = SecondQuote + 1;\r | |
1489 | } else /* FirstPercent < SecondQuote */{\r | |
1490 | //\r | |
1491 | // Restart on the first percent\r | |
1492 | //\r | |
1493 | CurrentLocator = FirstPercent;\r | |
1494 | }\r | |
1495 | continue;\r | |
1496 | }\r | |
fdd52bde | 1497 | \r |
fe8ec3dd | 1498 | if (FirstQuote == NULL || SecondPercent < FirstQuote) {\r |
42435671 QS |
1499 | if (IsValidEnvironmentVariableName(FirstPercent, SecondPercent)) {\r |
1500 | //\r | |
1501 | // We need to remove from FirstPercent to SecondPercent\r | |
1502 | //\r | |
1503 | CopyMem(FirstPercent, SecondPercent + 1, StrSize(SecondPercent + 1));\r | |
1504 | //\r | |
d08a5464 | 1505 | // don't need to update the locator. both % characters are gone.\r |
42435671 QS |
1506 | //\r |
1507 | } else {\r | |
1508 | CurrentLocator = SecondPercent + 1;\r | |
1509 | }\r | |
d5b5440b JC |
1510 | continue;\r |
1511 | }\r | |
d5b5440b JC |
1512 | CurrentLocator = FirstQuote;\r |
1513 | }\r | |
1514 | return (EFI_SUCCESS);\r | |
1515 | }\r | |
1516 | \r | |
a405b86d | 1517 | /**\r |
1518 | Function allocates a new command line and replaces all instances of environment\r | |
1519 | variable names that are correctly preset to their values.\r | |
1520 | \r | |
1521 | If the return value is not NULL the memory must be caller freed.\r | |
1522 | \r | |
1523 | @param[in] OriginalCommandLine The original command line\r | |
1524 | \r | |
d08a5464 | 1525 | @retval NULL An error occurred.\r |
a405b86d | 1526 | @return The new command line with no environment variables present.\r |
1527 | **/\r | |
1528 | CHAR16*\r | |
1529 | EFIAPI\r | |
1530 | ShellConvertVariables (\r | |
1531 | IN CONST CHAR16 *OriginalCommandLine\r | |
1532 | )\r | |
1533 | {\r | |
1534 | CONST CHAR16 *MasterEnvList;\r | |
1535 | UINTN NewSize;\r | |
1536 | CHAR16 *NewCommandLine1;\r | |
1537 | CHAR16 *NewCommandLine2;\r | |
1538 | CHAR16 *Temp;\r | |
1539 | UINTN ItemSize;\r | |
1540 | CHAR16 *ItemTemp;\r | |
1541 | SCRIPT_FILE *CurrentScriptFile;\r | |
1542 | ALIAS_LIST *AliasListNode;\r | |
1543 | \r | |
1544 | ASSERT(OriginalCommandLine != NULL);\r | |
1545 | \r | |
1546 | ItemSize = 0;\r | |
1547 | NewSize = StrSize(OriginalCommandLine);\r | |
1548 | CurrentScriptFile = ShellCommandGetCurrentScriptFile();\r | |
1549 | Temp = NULL;\r | |
1550 | \r | |
1551 | ///@todo update this to handle the %0 - %9 for scripting only (borrow from line 1256 area) ? ? ?\r | |
1552 | \r | |
1553 | //\r | |
1554 | // calculate the size required for the post-conversion string...\r | |
1555 | //\r | |
1556 | if (CurrentScriptFile != NULL) {\r | |
1557 | for (AliasListNode = (ALIAS_LIST*)GetFirstNode(&CurrentScriptFile->SubstList)\r | |
1558 | ; !IsNull(&CurrentScriptFile->SubstList, &AliasListNode->Link)\r | |
1559 | ; AliasListNode = (ALIAS_LIST*)GetNextNode(&CurrentScriptFile->SubstList, &AliasListNode->Link)\r | |
1560 | ){\r | |
1561 | for (Temp = StrStr(OriginalCommandLine, AliasListNode->Alias)\r | |
1562 | ; Temp != NULL\r | |
1563 | ; Temp = StrStr(Temp+1, AliasListNode->Alias)\r | |
1564 | ){\r | |
1565 | //\r | |
d08a5464 | 1566 | // we need a preceding and if there is space no ^ preceding (if no space ignore)\r |
a405b86d | 1567 | //\r |
1568 | if ((((Temp-OriginalCommandLine)>2) && *(Temp-2) != L'^') || ((Temp-OriginalCommandLine)<=2)) {\r | |
1569 | NewSize += StrSize(AliasListNode->CommandString);\r | |
1570 | }\r | |
1571 | }\r | |
1572 | }\r | |
1573 | }\r | |
1574 | \r | |
1575 | for (MasterEnvList = EfiShellGetEnv(NULL)\r | |
1576 | ; MasterEnvList != NULL && *MasterEnvList != CHAR_NULL //&& *(MasterEnvList+1) != CHAR_NULL\r | |
1577 | ; MasterEnvList += StrLen(MasterEnvList) + 1\r | |
1578 | ){\r | |
1579 | if (StrSize(MasterEnvList) > ItemSize) {\r | |
1580 | ItemSize = StrSize(MasterEnvList);\r | |
1581 | }\r | |
1582 | for (Temp = StrStr(OriginalCommandLine, MasterEnvList)\r | |
1583 | ; Temp != NULL\r | |
1584 | ; Temp = StrStr(Temp+1, MasterEnvList)\r | |
1585 | ){\r | |
1586 | //\r | |
d08a5464 | 1587 | // we need a preceding and following % and if there is space no ^ preceding (if no space ignore)\r |
a405b86d | 1588 | //\r |
1589 | if (*(Temp-1) == L'%' && *(Temp+StrLen(MasterEnvList)) == L'%' &&\r | |
1590 | ((((Temp-OriginalCommandLine)>2) && *(Temp-2) != L'^') || ((Temp-OriginalCommandLine)<=2))) {\r | |
1591 | NewSize+=StrSize(EfiShellGetEnv(MasterEnvList));\r | |
1592 | }\r | |
1593 | }\r | |
1594 | }\r | |
1595 | \r | |
a405b86d | 1596 | //\r |
1597 | // now do the replacements...\r | |
1598 | //\r | |
7f79b01e | 1599 | NewCommandLine1 = AllocateCopyPool(NewSize, OriginalCommandLine);\r |
a405b86d | 1600 | NewCommandLine2 = AllocateZeroPool(NewSize);\r |
1601 | ItemTemp = AllocateZeroPool(ItemSize+(2*sizeof(CHAR16)));\r | |
3c865f20 | 1602 | if (NewCommandLine1 == NULL || NewCommandLine2 == NULL || ItemTemp == NULL) {\r |
1603 | SHELL_FREE_NON_NULL(NewCommandLine1);\r | |
1604 | SHELL_FREE_NON_NULL(NewCommandLine2);\r | |
1605 | SHELL_FREE_NON_NULL(ItemTemp);\r | |
1606 | return (NULL);\r | |
1607 | }\r | |
a405b86d | 1608 | for (MasterEnvList = EfiShellGetEnv(NULL)\r |
7f79b01e | 1609 | ; MasterEnvList != NULL && *MasterEnvList != CHAR_NULL\r |
a405b86d | 1610 | ; MasterEnvList += StrLen(MasterEnvList) + 1\r |
1611 | ){\r | |
e75390f0 QS |
1612 | StrCpyS( ItemTemp, \r |
1613 | ((ItemSize+(2*sizeof(CHAR16)))/sizeof(CHAR16)), \r | |
1614 | L"%"\r | |
1615 | );\r | |
1616 | StrCatS( ItemTemp, \r | |
1617 | ((ItemSize+(2*sizeof(CHAR16)))/sizeof(CHAR16)), \r | |
1618 | MasterEnvList\r | |
1619 | );\r | |
1620 | StrCatS( ItemTemp, \r | |
1621 | ((ItemSize+(2*sizeof(CHAR16)))/sizeof(CHAR16)), \r | |
1622 | L"%"\r | |
1623 | );\r | |
a405b86d | 1624 | ShellCopySearchAndReplace(NewCommandLine1, NewCommandLine2, NewSize, ItemTemp, EfiShellGetEnv(MasterEnvList), TRUE, FALSE);\r |
e75390f0 | 1625 | StrCpyS(NewCommandLine1, NewSize/sizeof(CHAR16), NewCommandLine2);\r |
a405b86d | 1626 | }\r |
1627 | if (CurrentScriptFile != NULL) {\r | |
1628 | for (AliasListNode = (ALIAS_LIST*)GetFirstNode(&CurrentScriptFile->SubstList)\r | |
1629 | ; !IsNull(&CurrentScriptFile->SubstList, &AliasListNode->Link)\r | |
1630 | ; AliasListNode = (ALIAS_LIST*)GetNextNode(&CurrentScriptFile->SubstList, &AliasListNode->Link)\r | |
1631 | ){\r | |
1632 | ShellCopySearchAndReplace(NewCommandLine1, NewCommandLine2, NewSize, AliasListNode->Alias, AliasListNode->CommandString, TRUE, FALSE);\r | |
e75390f0 | 1633 | StrCpyS(NewCommandLine1, NewSize/sizeof(CHAR16), NewCommandLine2);\r |
a405b86d | 1634 | }\r |
1635 | }\r | |
1636 | \r | |
fdd52bde | 1637 | //\r |
d08a5464 | 1638 | // Remove non-existent environment variables\r |
fdd52bde JC |
1639 | //\r |
1640 | StripUnreplacedEnvironmentVariables(NewCommandLine1);\r | |
1641 | \r | |
df07baea JC |
1642 | //\r |
1643 | // Now cleanup any straggler intentionally ignored "%" characters\r | |
1644 | //\r | |
1645 | ShellCopySearchAndReplace(NewCommandLine1, NewCommandLine2, NewSize, L"^%", L"%", TRUE, FALSE);\r | |
e75390f0 | 1646 | StrCpyS(NewCommandLine1, NewSize/sizeof(CHAR16), NewCommandLine2);\r |
df07baea | 1647 | \r |
a405b86d | 1648 | FreePool(NewCommandLine2);\r |
1649 | FreePool(ItemTemp);\r | |
1650 | \r | |
1651 | return (NewCommandLine1);\r | |
1652 | }\r | |
1653 | \r | |
1654 | /**\r | |
1655 | Internal function to run a command line with pipe usage.\r | |
1656 | \r | |
1657 | @param[in] CmdLine The pointer to the command line.\r | |
1658 | @param[in] StdIn The pointer to the Standard input.\r | |
1659 | @param[in] StdOut The pointer to the Standard output.\r | |
1660 | \r | |
1661 | @retval EFI_SUCCESS The split command is executed successfully.\r | |
1662 | @retval other Some error occurs when executing the split command.\r | |
1663 | **/\r | |
1664 | EFI_STATUS\r | |
1665 | EFIAPI\r | |
1666 | RunSplitCommand(\r | |
1667 | IN CONST CHAR16 *CmdLine,\r | |
1668 | IN SHELL_FILE_HANDLE *StdIn,\r | |
a308e058 | 1669 | IN SHELL_FILE_HANDLE *StdOut\r |
a405b86d | 1670 | )\r |
1671 | {\r | |
1672 | EFI_STATUS Status;\r | |
1673 | CHAR16 *NextCommandLine;\r | |
1674 | CHAR16 *OurCommandLine;\r | |
1675 | UINTN Size1;\r | |
1676 | UINTN Size2;\r | |
1677 | SPLIT_LIST *Split;\r | |
1678 | SHELL_FILE_HANDLE *TempFileHandle;\r | |
1679 | BOOLEAN Unicode;\r | |
1680 | \r | |
1681 | ASSERT(StdOut == NULL);\r | |
1682 | \r | |
1683 | ASSERT(StrStr(CmdLine, L"|") != NULL);\r | |
1684 | \r | |
1685 | Status = EFI_SUCCESS;\r | |
1686 | NextCommandLine = NULL;\r | |
1687 | OurCommandLine = NULL;\r | |
1688 | Size1 = 0;\r | |
1689 | Size2 = 0;\r | |
1690 | \r | |
1691 | NextCommandLine = StrnCatGrow(&NextCommandLine, &Size1, StrStr(CmdLine, L"|")+1, 0);\r | |
1692 | OurCommandLine = StrnCatGrow(&OurCommandLine , &Size2, CmdLine , StrStr(CmdLine, L"|") - CmdLine);\r | |
532691c8 | 1693 | \r |
1694 | if (NextCommandLine == NULL || OurCommandLine == NULL) {\r | |
1695 | SHELL_FREE_NON_NULL(OurCommandLine);\r | |
1696 | SHELL_FREE_NON_NULL(NextCommandLine);\r | |
1697 | return (EFI_OUT_OF_RESOURCES);\r | |
19216573 | 1698 | } else if (StrStr(OurCommandLine, L"|") != NULL || Size1 == 0 || Size2 == 0) {\r |
1699 | SHELL_FREE_NON_NULL(OurCommandLine);\r | |
1700 | SHELL_FREE_NON_NULL(NextCommandLine);\r | |
1701 | return (EFI_INVALID_PARAMETER);\r | |
a5382ce0 QS |
1702 | } else if (NextCommandLine[0] == L'a' &&\r |
1703 | (NextCommandLine[1] == L' ' || NextCommandLine[1] == CHAR_NULL)\r | |
1704 | ){\r | |
a405b86d | 1705 | CopyMem(NextCommandLine, NextCommandLine+1, StrSize(NextCommandLine) - sizeof(NextCommandLine[0]));\r |
a5382ce0 QS |
1706 | while (NextCommandLine[0] == L' ') {\r |
1707 | CopyMem(NextCommandLine, NextCommandLine+1, StrSize(NextCommandLine) - sizeof(NextCommandLine[0]));\r | |
1708 | }\r | |
1709 | if (NextCommandLine[0] == CHAR_NULL) {\r | |
1710 | SHELL_FREE_NON_NULL(OurCommandLine);\r | |
1711 | SHELL_FREE_NON_NULL(NextCommandLine);\r | |
1712 | return (EFI_INVALID_PARAMETER);\r | |
1713 | }\r | |
a405b86d | 1714 | Unicode = FALSE;\r |
1715 | } else {\r | |
1716 | Unicode = TRUE;\r | |
1717 | }\r | |
1718 | \r | |
1719 | \r | |
1720 | //\r | |
1721 | // make a SPLIT_LIST item and add to list\r | |
1722 | //\r | |
1723 | Split = AllocateZeroPool(sizeof(SPLIT_LIST));\r | |
1724 | ASSERT(Split != NULL);\r | |
1725 | Split->SplitStdIn = StdIn;\r | |
1726 | Split->SplitStdOut = ConvertEfiFileProtocolToShellHandle(CreateFileInterfaceMem(Unicode), NULL);\r | |
1727 | ASSERT(Split->SplitStdOut != NULL);\r | |
1728 | InsertHeadList(&ShellInfoObject.SplitList.Link, &Split->Link);\r | |
1729 | \r | |
a308e058 | 1730 | Status = RunCommand(OurCommandLine);\r |
a405b86d | 1731 | \r |
1732 | //\r | |
1733 | // move the output from the first to the in to the second.\r | |
1734 | //\r | |
1735 | TempFileHandle = Split->SplitStdOut;\r | |
1736 | if (Split->SplitStdIn == StdIn) {\r | |
1737 | Split->SplitStdOut = NULL;\r | |
1738 | } else {\r | |
1739 | Split->SplitStdOut = Split->SplitStdIn;\r | |
1740 | }\r | |
1741 | Split->SplitStdIn = TempFileHandle;\r | |
1742 | ShellInfoObject.NewEfiShellProtocol->SetFilePosition(ConvertShellHandleToEfiFileProtocol(Split->SplitStdIn), 0);\r | |
1743 | \r | |
1744 | if (!EFI_ERROR(Status)) {\r | |
a308e058 | 1745 | Status = RunCommand(NextCommandLine);\r |
a405b86d | 1746 | }\r |
1747 | \r | |
1748 | //\r | |
1749 | // remove the top level from the ScriptList\r | |
1750 | //\r | |
1751 | ASSERT((SPLIT_LIST*)GetFirstNode(&ShellInfoObject.SplitList.Link) == Split);\r | |
1752 | RemoveEntryList(&Split->Link);\r | |
1753 | \r | |
1754 | //\r | |
1755 | // Note that the original StdIn is now the StdOut...\r | |
1756 | //\r | |
bd3fc813 | 1757 | if (Split->SplitStdOut != NULL) {\r |
a405b86d | 1758 | ShellInfoObject.NewEfiShellProtocol->CloseFile(ConvertShellHandleToEfiFileProtocol(Split->SplitStdOut));\r |
1759 | }\r | |
1760 | if (Split->SplitStdIn != NULL) {\r | |
1761 | ShellInfoObject.NewEfiShellProtocol->CloseFile(ConvertShellHandleToEfiFileProtocol(Split->SplitStdIn));\r | |
bd3fc813 | 1762 | FreePool (Split->SplitStdIn);\r |
a405b86d | 1763 | }\r |
1764 | \r | |
1765 | FreePool(Split);\r | |
1766 | FreePool(NextCommandLine);\r | |
1767 | FreePool(OurCommandLine);\r | |
1768 | \r | |
1769 | return (Status);\r | |
1770 | }\r | |
1771 | \r | |
1ef61d03 JC |
1772 | /**\r |
1773 | Take the original command line, substitute any variables, free \r | |
51429264 | 1774 | the original string, return the modified copy.\r |
1ef61d03 | 1775 | \r |
51429264 | 1776 | @param[in] CmdLine pointer to the command line to update.\r |
1ef61d03 | 1777 | \r |
51429264 SQ |
1778 | @retval EFI_SUCCESS the function was successful.\r |
1779 | @retval EFI_OUT_OF_RESOURCES a memory allocation failed.\r | |
1ef61d03 JC |
1780 | **/\r |
1781 | EFI_STATUS\r | |
1782 | EFIAPI\r | |
1783 | ShellSubstituteVariables(\r | |
1784 | IN CHAR16 **CmdLine\r | |
1785 | )\r | |
1786 | {\r | |
1787 | CHAR16 *NewCmdLine;\r | |
1788 | NewCmdLine = ShellConvertVariables(*CmdLine);\r | |
1789 | SHELL_FREE_NON_NULL(*CmdLine);\r | |
1790 | if (NewCmdLine == NULL) {\r | |
1791 | return (EFI_OUT_OF_RESOURCES);\r | |
1792 | }\r | |
1793 | *CmdLine = NewCmdLine;\r | |
1794 | return (EFI_SUCCESS);\r | |
1795 | }\r | |
1796 | \r | |
ca53c0af JC |
1797 | /**\r |
1798 | Take the original command line, substitute any alias in the first group of space delimited characters, free \r | |
51429264 | 1799 | the original string, return the modified copy.\r |
ca53c0af | 1800 | \r |
51429264 | 1801 | @param[in] CmdLine pointer to the command line to update.\r |
ca53c0af | 1802 | \r |
51429264 SQ |
1803 | @retval EFI_SUCCESS the function was successful.\r |
1804 | @retval EFI_OUT_OF_RESOURCES a memory allocation failed.\r | |
ca53c0af JC |
1805 | **/\r |
1806 | EFI_STATUS\r | |
1807 | EFIAPI\r | |
1808 | ShellSubstituteAliases(\r | |
1809 | IN CHAR16 **CmdLine\r | |
1810 | )\r | |
1811 | {\r | |
1812 | CHAR16 *NewCmdLine;\r | |
1813 | CHAR16 *CommandName;\r | |
1814 | EFI_STATUS Status;\r | |
1815 | UINTN PostAliasSize;\r | |
1816 | ASSERT(CmdLine != NULL);\r | |
1817 | ASSERT(*CmdLine!= NULL);\r | |
1818 | \r | |
1819 | \r | |
1820 | CommandName = NULL;\r | |
1821 | if (StrStr((*CmdLine), L" ") == NULL){\r | |
1822 | StrnCatGrow(&CommandName, NULL, (*CmdLine), 0);\r | |
1823 | } else {\r | |
1824 | StrnCatGrow(&CommandName, NULL, (*CmdLine), StrStr((*CmdLine), L" ") - (*CmdLine));\r | |
1825 | }\r | |
1826 | \r | |
1827 | //\r | |
1828 | // This cannot happen 'inline' since the CmdLine can need extra space.\r | |
1829 | //\r | |
1830 | NewCmdLine = NULL;\r | |
1831 | if (!ShellCommandIsCommandOnList(CommandName)) {\r | |
1832 | //\r | |
1833 | // Convert via alias\r | |
1834 | //\r | |
1835 | Status = ShellConvertAlias(&CommandName);\r | |
1836 | if (EFI_ERROR(Status)){\r | |
1837 | return (Status);\r | |
1838 | }\r | |
1839 | PostAliasSize = 0;\r | |
1840 | NewCmdLine = StrnCatGrow(&NewCmdLine, &PostAliasSize, CommandName, 0);\r | |
1841 | if (NewCmdLine == NULL) {\r | |
1842 | SHELL_FREE_NON_NULL(CommandName);\r | |
1843 | SHELL_FREE_NON_NULL(*CmdLine);\r | |
1844 | return (EFI_OUT_OF_RESOURCES);\r | |
1845 | }\r | |
1846 | NewCmdLine = StrnCatGrow(&NewCmdLine, &PostAliasSize, StrStr((*CmdLine), L" "), 0);\r | |
1847 | if (NewCmdLine == NULL) {\r | |
1848 | SHELL_FREE_NON_NULL(CommandName);\r | |
1849 | SHELL_FREE_NON_NULL(*CmdLine);\r | |
1850 | return (EFI_OUT_OF_RESOURCES);\r | |
1851 | }\r | |
1852 | } else {\r | |
1853 | NewCmdLine = StrnCatGrow(&NewCmdLine, NULL, (*CmdLine), 0);\r | |
1854 | }\r | |
1855 | \r | |
1856 | SHELL_FREE_NON_NULL(*CmdLine);\r | |
1857 | SHELL_FREE_NON_NULL(CommandName);\r | |
1858 | \r | |
1859 | //\r | |
1860 | // re-assign the passed in double pointer to point to our newly allocated buffer\r | |
1861 | //\r | |
1862 | *CmdLine = NewCmdLine;\r | |
1863 | \r | |
1864 | return (EFI_SUCCESS);\r | |
1865 | }\r | |
1866 | \r | |
6ba2921d JC |
1867 | /**\r |
1868 | Takes the Argv[0] part of the command line and determine the meaning of it.\r | |
e7831c90 | 1869 | \r |
51429264 | 1870 | @param[in] CmdName pointer to the command line to update.\r |
e7831c90 | 1871 | \r |
51429264 SQ |
1872 | @retval Internal_Command The name is an internal command.\r |
1873 | @retval File_Sys_Change the name is a file system change.\r | |
1874 | @retval Script_File_Name the name is a NSH script file.\r | |
1875 | @retval Unknown_Invalid the name is unknown.\r | |
1876 | @retval Efi_Application the name is an application (.EFI).\r | |
6ba2921d JC |
1877 | **/\r |
1878 | SHELL_OPERATION_TYPES\r | |
1879 | EFIAPI\r | |
1880 | GetOperationType(\r | |
1881 | IN CONST CHAR16 *CmdName\r | |
1882 | )\r | |
1883 | {\r | |
1884 | CHAR16* FileWithPath;\r | |
1885 | CONST CHAR16* TempLocation;\r | |
1886 | CONST CHAR16* TempLocation2;\r | |
1887 | \r | |
1888 | FileWithPath = NULL;\r | |
1889 | //\r | |
1890 | // test for an internal command.\r | |
1891 | //\r | |
1892 | if (ShellCommandIsCommandOnList(CmdName)) {\r | |
51429264 | 1893 | return (Internal_Command);\r |
6ba2921d JC |
1894 | }\r |
1895 | \r | |
1896 | //\r | |
fc4c7b30 | 1897 | // Test for file system change request. anything ending with first : and cant have spaces.\r |
6ba2921d JC |
1898 | //\r |
1899 | if (CmdName[(StrLen(CmdName)-1)] == L':') {\r | |
fc4c7b30 JC |
1900 | if ( StrStr(CmdName, L" ") != NULL \r |
1901 | || StrLen(StrStr(CmdName, L":")) > 1\r | |
1902 | ) {\r | |
51429264 | 1903 | return (Unknown_Invalid);\r |
6ba2921d | 1904 | }\r |
51429264 | 1905 | return (File_Sys_Change);\r |
6ba2921d JC |
1906 | }\r |
1907 | \r | |
1908 | //\r | |
1909 | // Test for a file\r | |
1910 | //\r | |
1911 | if ((FileWithPath = ShellFindFilePathEx(CmdName, mExecutableExtensions)) != NULL) {\r | |
1912 | //\r | |
1913 | // See if that file has a script file extension\r | |
1914 | //\r | |
1915 | if (StrLen(FileWithPath) > 4) {\r | |
1916 | TempLocation = FileWithPath+StrLen(FileWithPath)-4;\r | |
1917 | TempLocation2 = mScriptExtension;\r | |
1918 | if (StringNoCaseCompare((VOID*)(&TempLocation), (VOID*)(&TempLocation2)) == 0) {\r | |
1919 | SHELL_FREE_NON_NULL(FileWithPath);\r | |
51429264 | 1920 | return (Script_File_Name);\r |
6ba2921d JC |
1921 | }\r |
1922 | }\r | |
1923 | \r | |
1924 | //\r | |
1925 | // Was a file, but not a script. we treat this as an application.\r | |
1926 | //\r | |
1927 | SHELL_FREE_NON_NULL(FileWithPath);\r | |
51429264 | 1928 | return (Efi_Application);\r |
6ba2921d JC |
1929 | }\r |
1930 | \r | |
1931 | SHELL_FREE_NON_NULL(FileWithPath);\r | |
1932 | //\r | |
1933 | // No clue what this is... return invalid flag...\r | |
1934 | //\r | |
51429264 | 1935 | return (Unknown_Invalid);\r |
6ba2921d JC |
1936 | }\r |
1937 | \r | |
de4ebdcf SQ |
1938 | /**\r |
1939 | Determine if the first item in a command line is valid.\r | |
1940 | \r | |
1941 | @param[in] CmdLine The command line to parse.\r | |
1942 | \r | |
1943 | @retval EFI_SUCCESS The item is valid.\r | |
1944 | @retval EFI_OUT_OF_RESOURCES A memory allocation failed.\r | |
1945 | @retval EFI_NOT_FOUND The operation type is unknown or invalid.\r | |
1946 | **/\r | |
6bd64463 JC |
1947 | EFI_STATUS \r |
1948 | EFIAPI\r | |
1949 | IsValidSplit(\r | |
1950 | IN CONST CHAR16 *CmdLine\r | |
1951 | )\r | |
1952 | {\r | |
1953 | CHAR16 *Temp;\r | |
1954 | CHAR16 *FirstParameter;\r | |
1955 | CHAR16 *TempWalker;\r | |
1956 | EFI_STATUS Status;\r | |
1957 | \r | |
1958 | Temp = NULL;\r | |
1959 | \r | |
1960 | Temp = StrnCatGrow(&Temp, NULL, CmdLine, 0);\r | |
1961 | if (Temp == NULL) {\r | |
1962 | return (EFI_OUT_OF_RESOURCES);\r | |
1963 | }\r | |
1964 | \r | |
1965 | FirstParameter = StrStr(Temp, L"|");\r | |
1966 | if (FirstParameter != NULL) {\r | |
1967 | *FirstParameter = CHAR_NULL;\r | |
1968 | }\r | |
1969 | \r | |
1970 | FirstParameter = NULL;\r | |
1971 | \r | |
1972 | //\r | |
1973 | // Process the command line\r | |
1974 | //\r | |
1975 | Status = ProcessCommandLineToFinal(&Temp);\r | |
1976 | \r | |
1977 | if (!EFI_ERROR(Status)) {\r | |
1978 | FirstParameter = AllocateZeroPool(StrSize(CmdLine));\r | |
1979 | if (FirstParameter == NULL) {\r | |
1980 | SHELL_FREE_NON_NULL(Temp);\r | |
1981 | return (EFI_OUT_OF_RESOURCES);\r | |
1982 | }\r | |
1983 | TempWalker = (CHAR16*)Temp;\r | |
d1c275c6 | 1984 | if (!EFI_ERROR(GetNextParameter(&TempWalker, &FirstParameter, StrSize(CmdLine), TRUE))) {\r |
14030c5c JC |
1985 | if (GetOperationType(FirstParameter) == Unknown_Invalid) {\r |
1986 | ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_NOT_FOUND), ShellInfoObject.HiiHandle, FirstParameter);\r | |
1987 | SetLastError(SHELL_NOT_FOUND);\r | |
1988 | Status = EFI_NOT_FOUND;\r | |
1989 | }\r | |
6bd64463 JC |
1990 | }\r |
1991 | }\r | |
1992 | \r | |
1993 | SHELL_FREE_NON_NULL(Temp);\r | |
1994 | SHELL_FREE_NON_NULL(FirstParameter);\r | |
1995 | return Status;\r | |
1996 | }\r | |
1997 | \r | |
1998 | /**\r | |
51429264 | 1999 | Determine if a command line contains with a split contains only valid commands.\r |
6bd64463 JC |
2000 | \r |
2001 | @param[in] CmdLine The command line to parse.\r | |
2002 | \r | |
2003 | @retval EFI_SUCCESS CmdLine has only valid commands, application, or has no split.\r | |
2004 | @retval EFI_ABORTED CmdLine has at least one invalid command or application.\r | |
2005 | **/\r | |
2006 | EFI_STATUS\r | |
2007 | EFIAPI\r | |
2008 | VerifySplit(\r | |
2009 | IN CONST CHAR16 *CmdLine\r | |
2010 | )\r | |
2011 | {\r | |
2012 | CONST CHAR16 *TempSpot;\r | |
2013 | EFI_STATUS Status;\r | |
2014 | \r | |
2015 | //\r | |
a5382ce0 | 2016 | // If this was the only item, then get out\r |
6bd64463 | 2017 | //\r |
a5382ce0 QS |
2018 | if (!ContainsSplit(CmdLine)) {\r |
2019 | return (EFI_SUCCESS);\r | |
6bd64463 JC |
2020 | }\r |
2021 | \r | |
2022 | //\r | |
a5382ce0 | 2023 | // Verify up to the pipe or end character\r |
6bd64463 | 2024 | //\r |
a5382ce0 QS |
2025 | Status = IsValidSplit(CmdLine);\r |
2026 | if (EFI_ERROR(Status)) {\r | |
2027 | return (Status);\r | |
6bd64463 JC |
2028 | }\r |
2029 | \r | |
2030 | //\r | |
2031 | // recurse to verify the next item\r | |
2032 | //\r | |
00534bc3 | 2033 | TempSpot = FindFirstCharacter(CmdLine, L"|", L'^') + 1;\r |
a5382ce0 QS |
2034 | if (*TempSpot == L'a' && \r |
2035 | (*(TempSpot + 1) == L' ' || *(TempSpot + 1) == CHAR_NULL)\r | |
2036 | ) {\r | |
2037 | // If it's an ASCII pipe '|a'\r | |
2038 | TempSpot += 1;\r | |
2039 | }\r | |
2040 | \r | |
6bd64463 JC |
2041 | return (VerifySplit(TempSpot));\r |
2042 | }\r | |
2043 | \r | |
680db511 | 2044 | /**\r |
e7831c90 JC |
2045 | Process a split based operation.\r |
2046 | \r | |
a308e058 | 2047 | @param[in] CmdLine pointer to the command line to process\r |
e7831c90 JC |
2048 | \r |
2049 | @retval EFI_SUCCESS The operation was successful\r | |
d08a5464 | 2050 | @return an error occurred.\r |
680db511 JC |
2051 | **/\r |
2052 | EFI_STATUS\r | |
2053 | EFIAPI\r | |
2054 | ProcessNewSplitCommandLine(\r | |
a308e058 | 2055 | IN CONST CHAR16 *CmdLine\r |
680db511 JC |
2056 | )\r |
2057 | {\r | |
2058 | SPLIT_LIST *Split;\r | |
2059 | EFI_STATUS Status;\r | |
2060 | \r | |
6bd64463 JC |
2061 | Status = VerifySplit(CmdLine);\r |
2062 | if (EFI_ERROR(Status)) {\r | |
2063 | return (Status);\r | |
2064 | }\r | |
2065 | \r | |
680db511 JC |
2066 | Split = NULL;\r |
2067 | \r | |
2068 | //\r | |
2069 | // are we in an existing split???\r | |
2070 | //\r | |
2071 | if (!IsListEmpty(&ShellInfoObject.SplitList.Link)) {\r | |
2072 | Split = (SPLIT_LIST*)GetFirstNode(&ShellInfoObject.SplitList.Link);\r | |
2073 | }\r | |
2074 | \r | |
2075 | if (Split == NULL) {\r | |
a308e058 | 2076 | Status = RunSplitCommand(CmdLine, NULL, NULL);\r |
680db511 | 2077 | } else {\r |
a308e058 | 2078 | Status = RunSplitCommand(CmdLine, Split->SplitStdIn, Split->SplitStdOut);\r |
680db511 JC |
2079 | }\r |
2080 | if (EFI_ERROR(Status)) {\r | |
2081 | ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_INVALID_SPLIT), ShellInfoObject.HiiHandle, CmdLine);\r | |
2082 | }\r | |
2083 | return (Status);\r | |
2084 | }\r | |
daf70584 JC |
2085 | \r |
2086 | /**\r | |
51429264 | 2087 | Handle a request to change the current file system.\r |
daf70584 | 2088 | \r |
51429264 | 2089 | @param[in] CmdLine The passed in command line.\r |
daf70584 | 2090 | \r |
51429264 | 2091 | @retval EFI_SUCCESS The operation was successful.\r |
daf70584 JC |
2092 | **/\r |
2093 | EFI_STATUS\r | |
2094 | EFIAPI\r | |
2095 | ChangeMappedDrive(\r | |
2096 | IN CONST CHAR16 *CmdLine\r | |
2097 | )\r | |
2098 | {\r | |
2099 | EFI_STATUS Status;\r | |
2100 | Status = EFI_SUCCESS;\r | |
2101 | \r | |
2102 | //\r | |
2103 | // make sure we are the right operation\r | |
2104 | //\r | |
2105 | ASSERT(CmdLine[(StrLen(CmdLine)-1)] == L':' && StrStr(CmdLine, L" ") == NULL);\r | |
2106 | \r | |
2107 | //\r | |
2108 | // Call the protocol API to do the work\r | |
2109 | //\r | |
2110 | Status = ShellInfoObject.NewEfiShellProtocol->SetCurDir(NULL, CmdLine);\r | |
2111 | \r | |
2112 | //\r | |
2113 | // Report any errors\r | |
2114 | //\r | |
2115 | if (EFI_ERROR(Status)) {\r | |
2116 | ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_INVALID_MAPPING), ShellInfoObject.HiiHandle, CmdLine);\r | |
2117 | }\r | |
2118 | \r | |
2119 | return (Status);\r | |
2120 | }\r | |
2121 | \r | |
5a5eb806 JC |
2122 | /**\r |
2123 | Reprocess the command line to direct all -? to the help command.\r | |
2124 | \r | |
2125 | if found, will add "help" as argv[0], and move the rest later.\r | |
2126 | \r | |
806c49db | 2127 | @param[in,out] CmdLine pointer to the command line to update\r |
5a5eb806 JC |
2128 | **/\r |
2129 | EFI_STATUS\r | |
2130 | EFIAPI\r | |
806c49db JC |
2131 | DoHelpUpdate(\r |
2132 | IN OUT CHAR16 **CmdLine\r | |
5a5eb806 JC |
2133 | )\r |
2134 | {\r | |
806c49db JC |
2135 | CHAR16 *CurrentParameter;\r |
2136 | CHAR16 *Walker;\r | |
806c49db JC |
2137 | CHAR16 *NewCommandLine;\r |
2138 | EFI_STATUS Status;\r | |
e75390f0 | 2139 | UINTN NewCmdLineSize;\r |
806c49db JC |
2140 | \r |
2141 | Status = EFI_SUCCESS;\r | |
2142 | \r | |
2143 | CurrentParameter = AllocateZeroPool(StrSize(*CmdLine));\r | |
2144 | if (CurrentParameter == NULL) {\r | |
2145 | return (EFI_OUT_OF_RESOURCES);\r | |
2146 | }\r | |
2147 | \r | |
2148 | Walker = *CmdLine;\r | |
2149 | while(Walker != NULL && *Walker != CHAR_NULL) {\r | |
d1c275c6 | 2150 | if (!EFI_ERROR(GetNextParameter(&Walker, &CurrentParameter, StrSize(*CmdLine), TRUE))) {\r |
14030c5c | 2151 | if (StrStr(CurrentParameter, L"-?") == CurrentParameter) {\r |
a8f98806 JC |
2152 | CurrentParameter[0] = L' ';\r |
2153 | CurrentParameter[1] = L' ';\r | |
e75390f0 QS |
2154 | NewCmdLineSize = StrSize(L"help ") + StrSize(*CmdLine);\r |
2155 | NewCommandLine = AllocateZeroPool(NewCmdLineSize);\r | |
14030c5c JC |
2156 | if (NewCommandLine == NULL) {\r |
2157 | Status = EFI_OUT_OF_RESOURCES;\r | |
2158 | break;\r | |
2159 | }\r | |
2160 | \r | |
2161 | //\r | |
2162 | // We know the space is sufficient since we just calculated it.\r | |
2163 | //\r | |
e75390f0 QS |
2164 | StrnCpyS(NewCommandLine, NewCmdLineSize/sizeof(CHAR16), L"help ", 5);\r |
2165 | StrnCatS(NewCommandLine, NewCmdLineSize/sizeof(CHAR16), *CmdLine, StrLen(*CmdLine));\r | |
14030c5c JC |
2166 | SHELL_FREE_NON_NULL(*CmdLine);\r |
2167 | *CmdLine = NewCommandLine;\r | |
806c49db JC |
2168 | break;\r |
2169 | }\r | |
806c49db JC |
2170 | }\r |
2171 | }\r | |
2172 | \r | |
2173 | SHELL_FREE_NON_NULL(CurrentParameter);\r | |
2174 | \r | |
2175 | return (Status);\r | |
2176 | }\r | |
2177 | \r | |
2178 | /**\r | |
51429264 | 2179 | Function to update the shell variable "lasterror".\r |
806c49db | 2180 | \r |
51429264 | 2181 | @param[in] ErrorCode the error code to put into lasterror.\r |
806c49db JC |
2182 | **/\r |
2183 | EFI_STATUS\r | |
2184 | EFIAPI\r | |
2185 | SetLastError(\r | |
6bd64463 | 2186 | IN CONST SHELL_STATUS ErrorCode\r |
806c49db JC |
2187 | )\r |
2188 | {\r | |
2189 | CHAR16 LeString[19];\r | |
2190 | if (sizeof(EFI_STATUS) == sizeof(UINT64)) {\r | |
2191 | UnicodeSPrint(LeString, sizeof(LeString), L"0x%Lx", ErrorCode);\r | |
2192 | } else {\r | |
2193 | UnicodeSPrint(LeString, sizeof(LeString), L"0x%x", ErrorCode);\r | |
2194 | }\r | |
2195 | DEBUG_CODE(InternalEfiShellSetEnv(L"debuglasterror", LeString, TRUE););\r | |
2196 | InternalEfiShellSetEnv(L"lasterror", LeString, TRUE);\r | |
2197 | \r | |
2198 | return (EFI_SUCCESS);\r | |
2199 | }\r | |
2200 | \r | |
2201 | /**\r | |
2202 | Converts the command line to it's post-processed form. this replaces variables and alias' per UEFI Shell spec.\r | |
2203 | \r | |
2204 | @param[in,out] CmdLine pointer to the command line to update\r | |
2205 | \r | |
2206 | @retval EFI_SUCCESS The operation was successful\r | |
2207 | @retval EFI_OUT_OF_RESOURCES A memory allocation failed.\r | |
d08a5464 | 2208 | @return some other error occurred\r |
806c49db JC |
2209 | **/\r |
2210 | EFI_STATUS\r | |
2211 | EFIAPI\r | |
12a27a6d | 2212 | ProcessCommandLineToFinal(\r |
806c49db JC |
2213 | IN OUT CHAR16 **CmdLine\r |
2214 | )\r | |
2215 | {\r | |
2216 | EFI_STATUS Status;\r | |
2217 | TrimSpaces(CmdLine);\r | |
2218 | \r | |
2219 | Status = ShellSubstituteAliases(CmdLine);\r | |
2220 | if (EFI_ERROR(Status)) {\r | |
2221 | return (Status);\r | |
2222 | }\r | |
2223 | \r | |
2224 | TrimSpaces(CmdLine);\r | |
2225 | \r | |
2226 | Status = ShellSubstituteVariables(CmdLine);\r | |
2227 | if (EFI_ERROR(Status)) {\r | |
2228 | return (Status);\r | |
2229 | }\r | |
81cd2f53 | 2230 | ASSERT (*CmdLine != NULL);\r |
806c49db JC |
2231 | \r |
2232 | TrimSpaces(CmdLine);\r | |
2233 | \r | |
2234 | //\r | |
2235 | // update for help parsing\r | |
2236 | //\r | |
2237 | if (StrStr(*CmdLine, L"?") != NULL) {\r | |
2238 | //\r | |
2239 | // This may do nothing if the ? does not indicate help.\r | |
2240 | // Save all the details for in the API below.\r | |
2241 | //\r | |
2242 | Status = DoHelpUpdate(CmdLine);\r | |
2243 | }\r | |
2244 | \r | |
2245 | TrimSpaces(CmdLine);\r | |
2246 | \r | |
2247 | return (EFI_SUCCESS);\r | |
2248 | }\r | |
2249 | \r | |
2250 | /**\r | |
2251 | Run an internal shell command.\r | |
2252 | \r | |
d08a5464 | 2253 | This API will update the shell's environment since these commands are libraries.\r |
806c49db JC |
2254 | \r |
2255 | @param[in] CmdLine the command line to run.\r | |
2256 | @param[in] FirstParameter the first parameter on the command line\r | |
2257 | @param[in] ParamProtocol the shell parameters protocol pointer\r | |
490ce43d | 2258 | @param[out] CommandStatus the status from the command line.\r |
806c49db JC |
2259 | \r |
2260 | @retval EFI_SUCCESS The command was completed.\r | |
2261 | @retval EFI_ABORTED The command's operation was aborted.\r | |
2262 | **/\r | |
2263 | EFI_STATUS\r | |
2264 | EFIAPI\r | |
2265 | RunInternalCommand(\r | |
2266 | IN CONST CHAR16 *CmdLine,\r | |
2267 | IN CHAR16 *FirstParameter,\r | |
490ce43d QS |
2268 | IN EFI_SHELL_PARAMETERS_PROTOCOL *ParamProtocol,\r |
2269 | OUT EFI_STATUS *CommandStatus\r | |
806c49db JC |
2270 | )\r |
2271 | {\r | |
2272 | EFI_STATUS Status;\r | |
2273 | UINTN Argc;\r | |
2274 | CHAR16 **Argv;\r | |
2275 | SHELL_STATUS CommandReturnedStatus;\r | |
2276 | BOOLEAN LastError;\r | |
4b6b543e QS |
2277 | CHAR16 *Walker;\r |
2278 | CHAR16 *NewCmdLine; \r | |
2279 | \r | |
2280 | NewCmdLine = AllocateCopyPool (StrSize (CmdLine), CmdLine);\r | |
2281 | if (NewCmdLine == NULL) {\r | |
2282 | return EFI_OUT_OF_RESOURCES;\r | |
2283 | }\r | |
2284 | \r | |
2285 | for (Walker = NewCmdLine; Walker != NULL && *Walker != CHAR_NULL ; Walker++) {\r | |
2286 | if (*Walker == L'^' && *(Walker+1) == L'#') {\r | |
2287 | CopyMem(Walker, Walker+1, StrSize(Walker) - sizeof(Walker[0]));\r | |
2288 | }\r | |
2289 | }\r | |
806c49db | 2290 | \r |
5a5eb806 | 2291 | //\r |
806c49db | 2292 | // get the argc and argv updated for internal commands\r |
5a5eb806 | 2293 | //\r |
d1c275c6 | 2294 | Status = UpdateArgcArgv(ParamProtocol, NewCmdLine, Internal_Command, &Argv, &Argc);\r |
806c49db | 2295 | if (!EFI_ERROR(Status)) {\r |
5a5eb806 | 2296 | //\r |
806c49db | 2297 | // Run the internal command.\r |
5a5eb806 | 2298 | //\r |
806c49db JC |
2299 | Status = ShellCommandRunCommandHandler(FirstParameter, &CommandReturnedStatus, &LastError);\r |
2300 | \r | |
2301 | if (!EFI_ERROR(Status)) {\r | |
490ce43d QS |
2302 | if (CommandStatus != NULL) {\r |
2303 | if (CommandReturnedStatus != SHELL_SUCCESS) {\r | |
2304 | *CommandStatus = (EFI_STATUS)(CommandReturnedStatus | MAX_BIT);\r | |
2305 | } else {\r | |
2306 | *CommandStatus = EFI_SUCCESS;\r | |
2307 | }\r | |
2308 | }\r | |
2309 | \r | |
5a5eb806 | 2310 | //\r |
806c49db JC |
2311 | // Update last error status.\r |
2312 | // some commands do not update last error.\r | |
5a5eb806 | 2313 | //\r |
806c49db JC |
2314 | if (LastError) {\r |
2315 | SetLastError(CommandReturnedStatus);\r | |
5a5eb806 | 2316 | }\r |
806c49db JC |
2317 | \r |
2318 | //\r | |
2319 | // Pass thru the exitcode from the app.\r | |
2320 | //\r | |
2321 | if (ShellCommandGetExit()) {\r | |
e3eb7d82 JC |
2322 | //\r |
2323 | // An Exit was requested ("exit" command), pass its value up.\r | |
2324 | //\r | |
806c49db | 2325 | Status = CommandReturnedStatus;\r |
e3eb7d82 JC |
2326 | } else if (CommandReturnedStatus != SHELL_SUCCESS && IsScriptOnlyCommand(FirstParameter)) {\r |
2327 | //\r | |
2328 | // Always abort when a script only command fails for any reason\r | |
2329 | //\r | |
2330 | Status = EFI_ABORTED;\r | |
2331 | } else if (ShellCommandGetCurrentScriptFile() != NULL && CommandReturnedStatus == SHELL_ABORTED) {\r | |
2332 | //\r | |
2333 | // Abort when in a script and a command aborted\r | |
2334 | //\r | |
806c49db | 2335 | Status = EFI_ABORTED;\r |
5a5eb806 | 2336 | }\r |
806c49db JC |
2337 | }\r |
2338 | }\r | |
2339 | \r | |
2340 | //\r | |
d08a5464 | 2341 | // This is guaranteed to be called after UpdateArgcArgv no matter what else happened.\r |
806c49db JC |
2342 | // This is safe even if the update API failed. In this case, it may be a no-op.\r |
2343 | //\r | |
2344 | RestoreArgcArgv(ParamProtocol, &Argv, &Argc);\r | |
2345 | \r | |
e3eb7d82 | 2346 | //\r |
d08a5464 | 2347 | // If a script is running and the command is not a script only command, then\r |
e3eb7d82 JC |
2348 | // change return value to success so the script won't halt (unless aborted).\r |
2349 | //\r | |
2350 | // Script only commands have to be able halt the script since the script will\r | |
2351 | // not operate if they are failing.\r | |
2352 | //\r | |
2353 | if ( ShellCommandGetCurrentScriptFile() != NULL\r | |
2354 | && !IsScriptOnlyCommand(FirstParameter)\r | |
2355 | && Status != EFI_ABORTED\r | |
2356 | ) {\r | |
806c49db JC |
2357 | Status = EFI_SUCCESS;\r |
2358 | }\r | |
2359 | \r | |
4b6b543e | 2360 | FreePool (NewCmdLine);\r |
806c49db JC |
2361 | return (Status);\r |
2362 | }\r | |
2363 | \r | |
2364 | /**\r | |
2365 | Function to run the command or file.\r | |
2366 | \r | |
2367 | @param[in] Type the type of operation being run.\r | |
2368 | @param[in] CmdLine the command line to run.\r | |
2369 | @param[in] FirstParameter the first parameter on the command line\r | |
2370 | @param[in] ParamProtocol the shell parameters protocol pointer\r | |
490ce43d | 2371 | @param[out] CommandStatus the status from the command line.\r |
806c49db JC |
2372 | \r |
2373 | @retval EFI_SUCCESS The command was completed.\r | |
2374 | @retval EFI_ABORTED The command's operation was aborted.\r | |
2375 | **/\r | |
2376 | EFI_STATUS\r | |
2377 | EFIAPI\r | |
2378 | RunCommandOrFile(\r | |
2379 | IN SHELL_OPERATION_TYPES Type,\r | |
2380 | IN CONST CHAR16 *CmdLine,\r | |
2381 | IN CHAR16 *FirstParameter,\r | |
490ce43d QS |
2382 | IN EFI_SHELL_PARAMETERS_PROTOCOL *ParamProtocol,\r |
2383 | OUT EFI_STATUS *CommandStatus\r | |
806c49db JC |
2384 | )\r |
2385 | {\r | |
2386 | EFI_STATUS Status;\r | |
cd39fe08 | 2387 | EFI_STATUS StartStatus;\r |
806c49db JC |
2388 | CHAR16 *CommandWithPath;\r |
2389 | EFI_DEVICE_PATH_PROTOCOL *DevPath;\r | |
5223c121 | 2390 | SHELL_STATUS CalleeExitStatus;\r |
806c49db JC |
2391 | \r |
2392 | Status = EFI_SUCCESS;\r | |
2393 | CommandWithPath = NULL;\r | |
2394 | DevPath = NULL;\r | |
71c49eaf | 2395 | CalleeExitStatus = SHELL_INVALID_PARAMETER;\r |
806c49db JC |
2396 | \r |
2397 | switch (Type) {\r | |
51429264 | 2398 | case Internal_Command:\r |
490ce43d | 2399 | Status = RunInternalCommand(CmdLine, FirstParameter, ParamProtocol, CommandStatus);\r |
806c49db | 2400 | break;\r |
51429264 SQ |
2401 | case Script_File_Name:\r |
2402 | case Efi_Application:\r | |
806c49db JC |
2403 | //\r |
2404 | // Process a fully qualified path\r | |
2405 | //\r | |
2406 | if (StrStr(FirstParameter, L":") != NULL) {\r | |
2407 | ASSERT (CommandWithPath == NULL);\r | |
2408 | if (ShellIsFile(FirstParameter) == EFI_SUCCESS) {\r | |
2409 | CommandWithPath = StrnCatGrow(&CommandWithPath, NULL, FirstParameter, 0);\r | |
2410 | }\r | |
2411 | }\r | |
2412 | \r | |
2413 | //\r | |
2414 | // Process a relative path and also check in the path environment variable\r | |
2415 | //\r | |
2416 | if (CommandWithPath == NULL) {\r | |
2417 | CommandWithPath = ShellFindFilePathEx(FirstParameter, mExecutableExtensions);\r | |
2418 | }\r | |
2419 | \r | |
2420 | //\r | |
2421 | // This should be impossible now.\r | |
2422 | //\r | |
2423 | ASSERT(CommandWithPath != NULL);\r | |
2424 | \r | |
2425 | //\r | |
2426 | // Make sure that path is not just a directory (or not found)\r | |
2427 | //\r | |
2428 | if (!EFI_ERROR(ShellIsDirectory(CommandWithPath))) {\r | |
2429 | ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_NOT_FOUND), ShellInfoObject.HiiHandle, FirstParameter);\r | |
6bd64463 | 2430 | SetLastError(SHELL_NOT_FOUND);\r |
806c49db JC |
2431 | }\r |
2432 | switch (Type) {\r | |
51429264 | 2433 | case Script_File_Name:\r |
a308e058 | 2434 | Status = RunScriptFile (CommandWithPath, NULL, CmdLine, ParamProtocol);\r |
806c49db | 2435 | break;\r |
51429264 | 2436 | case Efi_Application:\r |
806c49db JC |
2437 | //\r |
2438 | // Get the device path of the application image\r | |
2439 | //\r | |
2440 | DevPath = ShellInfoObject.NewEfiShellProtocol->GetDevicePathFromFilePath(CommandWithPath);\r | |
2441 | if (DevPath == NULL){\r | |
2442 | Status = EFI_OUT_OF_RESOURCES;\r | |
2443 | break;\r | |
2444 | }\r | |
2445 | \r | |
2446 | //\r | |
2447 | // Execute the device path\r | |
2448 | //\r | |
2449 | Status = InternalShellExecuteDevicePath(\r | |
2450 | &gImageHandle,\r | |
2451 | DevPath,\r | |
2452 | CmdLine,\r | |
2453 | NULL,\r | |
a308e058 | 2454 | &StartStatus\r |
806c49db JC |
2455 | );\r |
2456 | \r | |
2457 | SHELL_FREE_NON_NULL(DevPath);\r | |
2458 | \r | |
71c49eaf SQ |
2459 | if(EFI_ERROR (Status)) {\r |
2460 | CalleeExitStatus = (SHELL_STATUS) (Status & (~MAX_BIT));\r | |
2461 | } else {\r | |
cd39fe08 | 2462 | CalleeExitStatus = (SHELL_STATUS) StartStatus;\r |
71c49eaf SQ |
2463 | }\r |
2464 | \r | |
490ce43d QS |
2465 | if (CommandStatus != NULL) {\r |
2466 | *CommandStatus = CalleeExitStatus;\r | |
2467 | }\r | |
2468 | \r | |
806c49db JC |
2469 | //\r |
2470 | // Update last error status.\r | |
2471 | //\r | |
5223c121 | 2472 | // Status is an EFI_STATUS. Clear top bit to convert to SHELL_STATUS\r |
71c49eaf | 2473 | SetLastError(CalleeExitStatus);\r |
806c49db | 2474 | break;\r |
6fa0da7d LE |
2475 | default:\r |
2476 | //\r | |
2477 | // Do nothing.\r | |
2478 | //\r | |
2479 | break;\r | |
5a5eb806 JC |
2480 | }\r |
2481 | break;\r | |
6fa0da7d LE |
2482 | default:\r |
2483 | //\r | |
2484 | // Do nothing.\r | |
2485 | //\r | |
2486 | break;\r | |
5a5eb806 | 2487 | }\r |
806c49db JC |
2488 | \r |
2489 | SHELL_FREE_NON_NULL(CommandWithPath);\r | |
2490 | \r | |
2491 | return (Status);\r | |
2492 | }\r | |
2493 | \r | |
2494 | /**\r | |
2495 | Function to setup StdIn, StdErr, StdOut, and then run the command or file.\r | |
2496 | \r | |
2497 | @param[in] Type the type of operation being run.\r | |
2498 | @param[in] CmdLine the command line to run.\r | |
2499 | @param[in] FirstParameter the first parameter on the command line.\r | |
2500 | @param[in] ParamProtocol the shell parameters protocol pointer\r | |
490ce43d | 2501 | @param[out] CommandStatus the status from the command line.\r |
806c49db JC |
2502 | \r |
2503 | @retval EFI_SUCCESS The command was completed.\r | |
2504 | @retval EFI_ABORTED The command's operation was aborted.\r | |
2505 | **/\r | |
2506 | EFI_STATUS\r | |
2507 | EFIAPI\r | |
2508 | SetupAndRunCommandOrFile(\r | |
490ce43d QS |
2509 | IN SHELL_OPERATION_TYPES Type,\r |
2510 | IN CHAR16 *CmdLine,\r | |
2511 | IN CHAR16 *FirstParameter,\r | |
2512 | IN EFI_SHELL_PARAMETERS_PROTOCOL *ParamProtocol,\r | |
dcbdb8bf | 2513 | OUT EFI_STATUS *CommandStatus\r |
806c49db JC |
2514 | )\r |
2515 | {\r | |
2516 | EFI_STATUS Status;\r | |
2517 | SHELL_FILE_HANDLE OriginalStdIn;\r | |
2518 | SHELL_FILE_HANDLE OriginalStdOut;\r | |
2519 | SHELL_FILE_HANDLE OriginalStdErr;\r | |
2520 | SYSTEM_TABLE_INFO OriginalSystemTableInfo;\r | |
08222019 | 2521 | CONST SCRIPT_FILE *ConstScriptFile;\r |
806c49db JC |
2522 | \r |
2523 | //\r | |
2524 | // Update the StdIn, StdOut, and StdErr for redirection to environment variables, files, etc... unicode and ASCII\r | |
2525 | //\r | |
2526 | Status = UpdateStdInStdOutStdErr(ParamProtocol, CmdLine, &OriginalStdIn, &OriginalStdOut, &OriginalStdErr, &OriginalSystemTableInfo);\r | |
2527 | \r | |
2528 | //\r | |
2529 | // The StdIn, StdOut, and StdErr are set up.\r | |
2530 | // Now run the command, script, or application\r | |
2531 | //\r | |
2532 | if (!EFI_ERROR(Status)) {\r | |
75eb337f | 2533 | TrimSpaces(&CmdLine);\r |
490ce43d | 2534 | Status = RunCommandOrFile(Type, CmdLine, FirstParameter, ParamProtocol, CommandStatus);\r |
806c49db JC |
2535 | }\r |
2536 | \r | |
2537 | //\r | |
2538 | // Now print errors\r | |
2539 | //\r | |
2540 | if (EFI_ERROR(Status)) {\r | |
08222019 JC |
2541 | ConstScriptFile = ShellCommandGetCurrentScriptFile();\r |
2542 | if (ConstScriptFile == NULL || ConstScriptFile->CurrentCommand == NULL) {\r | |
063aa89b JC |
2543 | ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_ERROR), ShellInfoObject.HiiHandle, (VOID*)(Status));\r |
2544 | } else {\r | |
08222019 | 2545 | ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_ERROR_SCRIPT), ShellInfoObject.HiiHandle, (VOID*)(Status), ConstScriptFile->CurrentCommand->Line);\r |
063aa89b | 2546 | }\r |
806c49db JC |
2547 | }\r |
2548 | \r | |
2549 | //\r | |
2550 | // put back the original StdIn, StdOut, and StdErr\r | |
2551 | //\r | |
2552 | RestoreStdInStdOutStdErr(ParamProtocol, &OriginalStdIn, &OriginalStdOut, &OriginalStdErr, &OriginalSystemTableInfo);\r | |
2553 | \r | |
2554 | return (Status);\r | |
5a5eb806 JC |
2555 | }\r |
2556 | \r | |
a405b86d | 2557 | /**\r |
2558 | Function will process and run a command line.\r | |
2559 | \r | |
2560 | This will determine if the command line represents an internal shell \r | |
2561 | command or dispatch an external application.\r | |
2562 | \r | |
2563 | @param[in] CmdLine The command line to parse.\r | |
490ce43d | 2564 | @param[out] CommandStatus The status from the command line.\r |
a405b86d | 2565 | \r |
2566 | @retval EFI_SUCCESS The command was completed.\r | |
2567 | @retval EFI_ABORTED The command's operation was aborted.\r | |
2568 | **/\r | |
2569 | EFI_STATUS\r | |
2570 | EFIAPI\r | |
490ce43d QS |
2571 | RunShellCommand(\r |
2572 | IN CONST CHAR16 *CmdLine,\r | |
2573 | OUT EFI_STATUS *CommandStatus\r | |
a405b86d | 2574 | )\r |
2575 | {\r | |
2576 | EFI_STATUS Status;\r | |
a405b86d | 2577 | CHAR16 *CleanOriginal;\r |
806c49db JC |
2578 | CHAR16 *FirstParameter;\r |
2579 | CHAR16 *TempWalker;\r | |
2580 | SHELL_OPERATION_TYPES Type;\r | |
a405b86d | 2581 | \r |
2582 | ASSERT(CmdLine != NULL);\r | |
2583 | if (StrLen(CmdLine) == 0) {\r | |
2584 | return (EFI_SUCCESS);\r | |
2585 | }\r | |
2586 | \r | |
a405b86d | 2587 | Status = EFI_SUCCESS;\r |
2588 | CleanOriginal = NULL;\r | |
a405b86d | 2589 | \r |
2590 | CleanOriginal = StrnCatGrow(&CleanOriginal, NULL, CmdLine, 0);\r | |
532691c8 | 2591 | if (CleanOriginal == NULL) {\r |
2592 | return (EFI_OUT_OF_RESOURCES);\r | |
2593 | }\r | |
284e034f | 2594 | \r |
ad2bc854 | 2595 | TrimSpaces(&CleanOriginal);\r |
a405b86d | 2596 | \r |
284e034f | 2597 | //\r |
d41a79a0 CP |
2598 | // NULL out comments (leveraged from RunScriptFileHandle() ).\r |
2599 | // The # character on a line is used to denote that all characters on the same line\r | |
2600 | // and to the right of the # are to be ignored by the shell.\r | |
d08a5464 | 2601 | // Afterwards, again remove spaces, in case any were between the last command-parameter and '#'.\r |
d41a79a0 CP |
2602 | //\r |
2603 | for (TempWalker = CleanOriginal; TempWalker != NULL && *TempWalker != CHAR_NULL; TempWalker++) {\r | |
2604 | if (*TempWalker == L'^') {\r | |
2605 | if (*(TempWalker + 1) == L'#') {\r | |
4b6b543e | 2606 | TempWalker++;\r |
d41a79a0 CP |
2607 | }\r |
2608 | } else if (*TempWalker == L'#') {\r | |
2609 | *TempWalker = CHAR_NULL;\r | |
2610 | }\r | |
2611 | }\r | |
2612 | \r | |
2613 | TrimSpaces(&CleanOriginal);\r | |
2614 | \r | |
2615 | //\r | |
284e034f CP |
2616 | // Handle case that passed in command line is just 1 or more " " characters.\r |
2617 | //\r | |
2618 | if (StrLen (CleanOriginal) == 0) {\r | |
806c49db | 2619 | SHELL_FREE_NON_NULL(CleanOriginal);\r |
284e034f CP |
2620 | return (EFI_SUCCESS);\r |
2621 | }\r | |
2622 | \r | |
12a27a6d | 2623 | Status = ProcessCommandLineToFinal(&CleanOriginal);\r |
ca53c0af | 2624 | if (EFI_ERROR(Status)) {\r |
806c49db | 2625 | SHELL_FREE_NON_NULL(CleanOriginal);\r |
ca53c0af | 2626 | return (Status);\r |
a405b86d | 2627 | }\r |
2628 | \r | |
a405b86d | 2629 | //\r |
d08a5464 | 2630 | // We don't do normal processing with a split command line (output from one command input to another)\r |
a405b86d | 2631 | //\r |
1ef61d03 | 2632 | if (ContainsSplit(CleanOriginal)) {\r |
a308e058 | 2633 | Status = ProcessNewSplitCommandLine(CleanOriginal);\r |
806c49db JC |
2634 | SHELL_FREE_NON_NULL(CleanOriginal);\r |
2635 | return (Status);\r | |
2636 | } \r | |
5b5cd144 | 2637 | \r |
806c49db JC |
2638 | //\r |
2639 | // We need the first parameter information so we can determine the operation type\r | |
2640 | //\r | |
2641 | FirstParameter = AllocateZeroPool(StrSize(CleanOriginal));\r | |
2642 | if (FirstParameter == NULL) {\r | |
2643 | SHELL_FREE_NON_NULL(CleanOriginal);\r | |
2644 | return (EFI_OUT_OF_RESOURCES);\r | |
2645 | }\r | |
2646 | TempWalker = CleanOriginal;\r | |
d1c275c6 | 2647 | if (!EFI_ERROR(GetNextParameter(&TempWalker, &FirstParameter, StrSize(CleanOriginal), TRUE))) {\r |
14030c5c JC |
2648 | //\r |
2649 | // Depending on the first parameter we change the behavior\r | |
2650 | //\r | |
2651 | switch (Type = GetOperationType(FirstParameter)) {\r | |
2652 | case File_Sys_Change:\r | |
2653 | Status = ChangeMappedDrive (FirstParameter);\r | |
2654 | break;\r | |
2655 | case Internal_Command:\r | |
2656 | case Script_File_Name:\r | |
2657 | case Efi_Application:\r | |
490ce43d | 2658 | Status = SetupAndRunCommandOrFile(Type, CleanOriginal, FirstParameter, ShellInfoObject.NewShellParametersProtocol, CommandStatus);\r |
14030c5c JC |
2659 | break;\r |
2660 | default:\r | |
2661 | //\r | |
2662 | // Whatever was typed, it was invalid.\r | |
2663 | //\r | |
2664 | ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_NOT_FOUND), ShellInfoObject.HiiHandle, FirstParameter);\r | |
2665 | SetLastError(SHELL_NOT_FOUND);\r | |
2666 | break;\r | |
2667 | }\r | |
2668 | } else {\r | |
2669 | ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_NOT_FOUND), ShellInfoObject.HiiHandle, FirstParameter);\r | |
2670 | SetLastError(SHELL_NOT_FOUND);\r | |
a405b86d | 2671 | }\r |
806c49db | 2672 | \r |
ca53c0af | 2673 | SHELL_FREE_NON_NULL(CleanOriginal);\r |
806c49db | 2674 | SHELL_FREE_NON_NULL(FirstParameter);\r |
a405b86d | 2675 | \r |
2676 | return (Status);\r | |
2677 | }\r | |
2678 | \r | |
490ce43d QS |
2679 | /**\r |
2680 | Function will process and run a command line.\r | |
2681 | \r | |
2682 | This will determine if the command line represents an internal shell \r | |
2683 | command or dispatch an external application.\r | |
2684 | \r | |
2685 | @param[in] CmdLine The command line to parse.\r | |
2686 | \r | |
2687 | @retval EFI_SUCCESS The command was completed.\r | |
2688 | @retval EFI_ABORTED The command's operation was aborted.\r | |
2689 | **/\r | |
2690 | EFI_STATUS\r | |
2691 | EFIAPI\r | |
2692 | RunCommand(\r | |
2693 | IN CONST CHAR16 *CmdLine\r | |
2694 | )\r | |
2695 | {\r | |
2696 | return (RunShellCommand(CmdLine, NULL));\r | |
2697 | }\r | |
2698 | \r | |
2699 | \r | |
a405b86d | 2700 | STATIC CONST UINT16 InvalidChars[] = {L'*', L'?', L'<', L'>', L'\\', L'/', L'\"', 0x0001, 0x0002};\r |
2701 | /**\r | |
d08a5464 | 2702 | Function determines if the CommandName COULD be a valid command. It does not determine whether\r |
a405b86d | 2703 | this is a valid command. It only checks for invalid characters.\r |
2704 | \r | |
2705 | @param[in] CommandName The name to check\r | |
2706 | \r | |
2707 | @retval TRUE CommandName could be a command name\r | |
2708 | @retval FALSE CommandName could not be a valid command name\r | |
2709 | **/\r | |
2710 | BOOLEAN\r | |
2711 | EFIAPI\r | |
2712 | IsValidCommandName(\r | |
2713 | IN CONST CHAR16 *CommandName\r | |
2714 | )\r | |
2715 | {\r | |
2716 | UINTN Count;\r | |
2717 | if (CommandName == NULL) {\r | |
2718 | ASSERT(FALSE);\r | |
2719 | return (FALSE);\r | |
2720 | }\r | |
2721 | for ( Count = 0\r | |
2722 | ; Count < sizeof(InvalidChars) / sizeof(InvalidChars[0])\r | |
2723 | ; Count++\r | |
2724 | ){\r | |
2725 | if (ScanMem16(CommandName, StrSize(CommandName), InvalidChars[Count]) != NULL) {\r | |
2726 | return (FALSE);\r | |
2727 | }\r | |
2728 | }\r | |
2729 | return (TRUE);\r | |
2730 | }\r | |
2731 | \r | |
2732 | /**\r | |
2733 | Function to process a NSH script file via SHELL_FILE_HANDLE.\r | |
2734 | \r | |
2735 | @param[in] Handle The handle to the already opened file.\r | |
2736 | @param[in] Name The name of the script file.\r | |
2737 | \r | |
d08a5464 | 2738 | @retval EFI_SUCCESS the script completed successfully\r |
a405b86d | 2739 | **/\r |
2740 | EFI_STATUS\r | |
2741 | EFIAPI\r | |
2742 | RunScriptFileHandle (\r | |
a308e058 RN |
2743 | IN SHELL_FILE_HANDLE Handle,\r |
2744 | IN CONST CHAR16 *Name\r | |
a405b86d | 2745 | )\r |
2746 | {\r | |
2747 | EFI_STATUS Status;\r | |
2748 | SCRIPT_FILE *NewScriptFile;\r | |
2749 | UINTN LoopVar;\r | |
4dc0d578 | 2750 | UINTN PrintBuffSize;\r |
a405b86d | 2751 | CHAR16 *CommandLine;\r |
2752 | CHAR16 *CommandLine2;\r | |
2753 | CHAR16 *CommandLine3;\r | |
2754 | SCRIPT_COMMAND_LIST *LastCommand;\r | |
2755 | BOOLEAN Ascii;\r | |
2756 | BOOLEAN PreScriptEchoState;\r | |
848997b5 | 2757 | BOOLEAN PreCommandEchoState;\r |
a405b86d | 2758 | CONST CHAR16 *CurDir;\r |
2759 | UINTN LineCount;\r | |
b6b22b13 | 2760 | CHAR16 LeString[50];\r |
14810d6b | 2761 | LIST_ENTRY OldBufferList;\r |
a405b86d | 2762 | \r |
2763 | ASSERT(!ShellCommandGetScriptExit());\r | |
2764 | \r | |
2765 | PreScriptEchoState = ShellCommandGetEchoState();\r | |
4dc0d578 | 2766 | PrintBuffSize = PcdGet16(PcdShellPrintBufferSize);\r |
a405b86d | 2767 | \r |
2768 | NewScriptFile = (SCRIPT_FILE*)AllocateZeroPool(sizeof(SCRIPT_FILE));\r | |
3c865f20 | 2769 | if (NewScriptFile == NULL) {\r |
2770 | return (EFI_OUT_OF_RESOURCES);\r | |
2771 | }\r | |
a405b86d | 2772 | \r |
2773 | //\r | |
2774 | // Set up the name\r | |
2775 | //\r | |
2776 | ASSERT(NewScriptFile->ScriptName == NULL);\r | |
2777 | NewScriptFile->ScriptName = StrnCatGrow(&NewScriptFile->ScriptName, NULL, Name, 0);\r | |
3c865f20 | 2778 | if (NewScriptFile->ScriptName == NULL) {\r |
2779 | DeleteScriptFileStruct(NewScriptFile);\r | |
2780 | return (EFI_OUT_OF_RESOURCES);\r | |
2781 | }\r | |
a405b86d | 2782 | \r |
2783 | //\r | |
2784 | // Save the parameters (used to replace %0 to %9 later on)\r | |
2785 | //\r | |
2786 | NewScriptFile->Argc = ShellInfoObject.NewShellParametersProtocol->Argc;\r | |
2787 | if (NewScriptFile->Argc != 0) {\r | |
2788 | NewScriptFile->Argv = (CHAR16**)AllocateZeroPool(NewScriptFile->Argc * sizeof(CHAR16*));\r | |
3c865f20 | 2789 | if (NewScriptFile->Argv == NULL) {\r |
2790 | DeleteScriptFileStruct(NewScriptFile);\r | |
2791 | return (EFI_OUT_OF_RESOURCES);\r | |
2792 | }\r | |
a405b86d | 2793 | for (LoopVar = 0 ; LoopVar < 10 && LoopVar < NewScriptFile->Argc; LoopVar++) {\r |
2794 | ASSERT(NewScriptFile->Argv[LoopVar] == NULL);\r | |
2795 | NewScriptFile->Argv[LoopVar] = StrnCatGrow(&NewScriptFile->Argv[LoopVar], NULL, ShellInfoObject.NewShellParametersProtocol->Argv[LoopVar], 0);\r | |
3c865f20 | 2796 | if (NewScriptFile->Argv[LoopVar] == NULL) {\r |
2797 | DeleteScriptFileStruct(NewScriptFile);\r | |
2798 | return (EFI_OUT_OF_RESOURCES);\r | |
2799 | }\r | |
a405b86d | 2800 | }\r |
2801 | } else {\r | |
2802 | NewScriptFile->Argv = NULL;\r | |
2803 | }\r | |
2804 | \r | |
2805 | InitializeListHead(&NewScriptFile->CommandList);\r | |
2806 | InitializeListHead(&NewScriptFile->SubstList);\r | |
2807 | \r | |
2808 | //\r | |
2809 | // Now build the list of all script commands.\r | |
2810 | //\r | |
2811 | LineCount = 0;\r | |
2812 | while(!ShellFileHandleEof(Handle)) {\r | |
2813 | CommandLine = ShellFileHandleReturnLine(Handle, &Ascii);\r | |
2814 | LineCount++;\r | |
4922715d JC |
2815 | if (CommandLine == NULL || StrLen(CommandLine) == 0 || CommandLine[0] == '#') {\r |
2816 | SHELL_FREE_NON_NULL(CommandLine);\r | |
a405b86d | 2817 | continue;\r |
2818 | }\r | |
2819 | NewScriptFile->CurrentCommand = AllocateZeroPool(sizeof(SCRIPT_COMMAND_LIST));\r | |
3c865f20 | 2820 | if (NewScriptFile->CurrentCommand == NULL) {\r |
4922715d | 2821 | SHELL_FREE_NON_NULL(CommandLine);\r |
3c865f20 | 2822 | DeleteScriptFileStruct(NewScriptFile);\r |
2823 | return (EFI_OUT_OF_RESOURCES);\r | |
2824 | }\r | |
a405b86d | 2825 | \r |
2826 | NewScriptFile->CurrentCommand->Cl = CommandLine;\r | |
2827 | NewScriptFile->CurrentCommand->Data = NULL;\r | |
2828 | NewScriptFile->CurrentCommand->Line = LineCount;\r | |
2829 | \r | |
2830 | InsertTailList(&NewScriptFile->CommandList, &NewScriptFile->CurrentCommand->Link);\r | |
2831 | }\r | |
2832 | \r | |
2833 | //\r | |
2834 | // Add this as the topmost script file\r | |
2835 | //\r | |
2836 | ShellCommandSetNewScript (NewScriptFile);\r | |
2837 | \r | |
2838 | //\r | |
2839 | // Now enumerate through the commands and run each one.\r | |
2840 | //\r | |
4dc0d578 | 2841 | CommandLine = AllocateZeroPool(PrintBuffSize);\r |
3c865f20 | 2842 | if (CommandLine == NULL) {\r |
2843 | DeleteScriptFileStruct(NewScriptFile);\r | |
2844 | return (EFI_OUT_OF_RESOURCES);\r | |
2845 | }\r | |
4dc0d578 | 2846 | CommandLine2 = AllocateZeroPool(PrintBuffSize);\r |
3c865f20 | 2847 | if (CommandLine2 == NULL) {\r |
2848 | FreePool(CommandLine);\r | |
2849 | DeleteScriptFileStruct(NewScriptFile);\r | |
2850 | return (EFI_OUT_OF_RESOURCES);\r | |
2851 | }\r | |
a405b86d | 2852 | \r |
2853 | for ( NewScriptFile->CurrentCommand = (SCRIPT_COMMAND_LIST *)GetFirstNode(&NewScriptFile->CommandList)\r | |
2854 | ; !IsNull(&NewScriptFile->CommandList, &NewScriptFile->CurrentCommand->Link)\r | |
2855 | ; // conditional increment in the body of the loop\r | |
2856 | ){\r | |
3c865f20 | 2857 | ASSERT(CommandLine2 != NULL);\r |
4dc0d578 QS |
2858 | StrnCpyS( CommandLine2, \r |
2859 | PrintBuffSize/sizeof(CHAR16), \r | |
2860 | NewScriptFile->CurrentCommand->Cl,\r | |
2861 | PrintBuffSize/sizeof(CHAR16) - 1\r | |
e75390f0 | 2862 | );\r |
a405b86d | 2863 | \r |
14810d6b QS |
2864 | SaveBufferList(&OldBufferList);\r |
2865 | \r | |
a405b86d | 2866 | //\r |
2867 | // NULL out comments\r | |
2868 | //\r | |
2869 | for (CommandLine3 = CommandLine2 ; CommandLine3 != NULL && *CommandLine3 != CHAR_NULL ; CommandLine3++) {\r | |
2870 | if (*CommandLine3 == L'^') {\r | |
4b6b543e | 2871 | if ( *(CommandLine3+1) == L':') {\r |
a405b86d | 2872 | CopyMem(CommandLine3, CommandLine3+1, StrSize(CommandLine3) - sizeof(CommandLine3[0]));\r |
4b6b543e QS |
2873 | } else if (*(CommandLine3+1) == L'#') {\r |
2874 | CommandLine3++;\r | |
a405b86d | 2875 | }\r |
2876 | } else if (*CommandLine3 == L'#') {\r | |
2877 | *CommandLine3 = CHAR_NULL;\r | |
2878 | }\r | |
2879 | }\r | |
2880 | \r | |
2881 | if (CommandLine2 != NULL && StrLen(CommandLine2) >= 1) {\r | |
2882 | //\r | |
2883 | // Due to variability in starting the find and replace action we need to have both buffers the same.\r | |
2884 | //\r | |
4dc0d578 QS |
2885 | StrnCpyS( CommandLine, \r |
2886 | PrintBuffSize/sizeof(CHAR16), \r | |
2887 | CommandLine2,\r | |
2888 | PrintBuffSize/sizeof(CHAR16) - 1\r | |
e75390f0 | 2889 | );\r |
a405b86d | 2890 | \r |
2891 | //\r | |
2892 | // Remove the %0 to %9 from the command line (if we have some arguments)\r | |
2893 | //\r | |
2894 | if (NewScriptFile->Argv != NULL) {\r | |
2895 | switch (NewScriptFile->Argc) {\r | |
2896 | default:\r | |
d1c275c6 | 2897 | Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PrintBuffSize, L"%9", NewScriptFile->Argv[9], FALSE, FALSE);\r |
a405b86d | 2898 | ASSERT_EFI_ERROR(Status);\r |
2899 | case 9:\r | |
d1c275c6 | 2900 | Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PrintBuffSize, L"%8", NewScriptFile->Argv[8], FALSE, FALSE);\r |
a405b86d | 2901 | ASSERT_EFI_ERROR(Status);\r |
2902 | case 8:\r | |
d1c275c6 | 2903 | Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PrintBuffSize, L"%7", NewScriptFile->Argv[7], FALSE, FALSE);\r |
a405b86d | 2904 | ASSERT_EFI_ERROR(Status);\r |
2905 | case 7:\r | |
d1c275c6 | 2906 | Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PrintBuffSize, L"%6", NewScriptFile->Argv[6], FALSE, FALSE);\r |
a405b86d | 2907 | ASSERT_EFI_ERROR(Status);\r |
2908 | case 6:\r | |
d1c275c6 | 2909 | Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PrintBuffSize, L"%5", NewScriptFile->Argv[5], FALSE, FALSE);\r |
a405b86d | 2910 | ASSERT_EFI_ERROR(Status);\r |
2911 | case 5:\r | |
d1c275c6 | 2912 | Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PrintBuffSize, L"%4", NewScriptFile->Argv[4], FALSE, FALSE);\r |
a405b86d | 2913 | ASSERT_EFI_ERROR(Status);\r |
2914 | case 4:\r | |
d1c275c6 | 2915 | Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PrintBuffSize, L"%3", NewScriptFile->Argv[3], FALSE, FALSE);\r |
a405b86d | 2916 | ASSERT_EFI_ERROR(Status);\r |
2917 | case 3:\r | |
d1c275c6 | 2918 | Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PrintBuffSize, L"%2", NewScriptFile->Argv[2], FALSE, FALSE);\r |
a405b86d | 2919 | ASSERT_EFI_ERROR(Status);\r |
2920 | case 2:\r | |
d1c275c6 | 2921 | Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PrintBuffSize, L"%1", NewScriptFile->Argv[1], FALSE, FALSE);\r |
a405b86d | 2922 | ASSERT_EFI_ERROR(Status);\r |
2923 | case 1:\r | |
d1c275c6 | 2924 | Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PrintBuffSize, L"%0", NewScriptFile->Argv[0], FALSE, FALSE);\r |
a405b86d | 2925 | ASSERT_EFI_ERROR(Status);\r |
2926 | break;\r | |
2927 | case 0:\r | |
2928 | break;\r | |
2929 | }\r | |
2930 | }\r | |
4dc0d578 QS |
2931 | Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PrintBuffSize, L"%1", L"\"\"", FALSE, FALSE);\r |
2932 | Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PrintBuffSize, L"%2", L"\"\"", FALSE, FALSE);\r | |
2933 | Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PrintBuffSize, L"%3", L"\"\"", FALSE, FALSE);\r | |
2934 | Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PrintBuffSize, L"%4", L"\"\"", FALSE, FALSE);\r | |
2935 | Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PrintBuffSize, L"%5", L"\"\"", FALSE, FALSE);\r | |
2936 | Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PrintBuffSize, L"%6", L"\"\"", FALSE, FALSE);\r | |
2937 | Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PrintBuffSize, L"%7", L"\"\"", FALSE, FALSE);\r | |
2938 | Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PrintBuffSize, L"%8", L"\"\"", FALSE, FALSE);\r | |
2939 | Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PrintBuffSize, L"%9", L"\"\"", FALSE, FALSE);\r | |
2940 | \r | |
2941 | StrnCpyS( CommandLine2, \r | |
2942 | PrintBuffSize/sizeof(CHAR16), \r | |
2943 | CommandLine,\r | |
2944 | PrintBuffSize/sizeof(CHAR16) - 1\r | |
e75390f0 | 2945 | );\r |
a405b86d | 2946 | \r |
2947 | LastCommand = NewScriptFile->CurrentCommand;\r | |
2948 | \r | |
2949 | for (CommandLine3 = CommandLine2 ; CommandLine3[0] == L' ' ; CommandLine3++);\r | |
2950 | \r | |
3c865f20 | 2951 | if (CommandLine3 != NULL && CommandLine3[0] == L':' ) {\r |
a405b86d | 2952 | //\r |
2953 | // This line is a goto target / label\r | |
2954 | //\r | |
2955 | } else {\r | |
2956 | if (CommandLine3 != NULL && StrLen(CommandLine3) > 0) {\r | |
848997b5 | 2957 | if (CommandLine3[0] == L'@') {\r |
2958 | //\r | |
2959 | // We need to save the current echo state\r | |
2960 | // and disable echo for just this command.\r | |
2961 | //\r | |
2962 | PreCommandEchoState = ShellCommandGetEchoState();\r | |
2963 | ShellCommandSetEchoState(FALSE);\r | |
a308e058 | 2964 | Status = RunCommand(CommandLine3+1);\r |
848997b5 | 2965 | \r |
2966 | //\r | |
284e034f | 2967 | // If command was "@echo -off" or "@echo -on" then don't restore echo state\r |
848997b5 | 2968 | //\r |
284e034f CP |
2969 | if (StrCmp (L"@echo -off", CommandLine3) != 0 &&\r |
2970 | StrCmp (L"@echo -on", CommandLine3) != 0) {\r | |
2971 | //\r | |
2972 | // Now restore the pre-'@' echo state.\r | |
2973 | //\r | |
2974 | ShellCommandSetEchoState(PreCommandEchoState);\r | |
2975 | }\r | |
848997b5 | 2976 | } else {\r |
0d11446d | 2977 | if (ShellCommandGetEchoState()) {\r |
2978 | CurDir = ShellInfoObject.NewEfiShellProtocol->GetEnv(L"cwd");\r | |
2979 | if (CurDir != NULL && StrLen(CurDir) > 1) {\r | |
2980 | ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_CURDIR), ShellInfoObject.HiiHandle, CurDir);\r | |
2981 | } else {\r | |
2982 | ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_SHELL), ShellInfoObject.HiiHandle);\r | |
2983 | }\r | |
2984 | ShellPrintEx(-1, -1, L"%s\r\n", CommandLine2);\r | |
2985 | }\r | |
a308e058 | 2986 | Status = RunCommand(CommandLine3);\r |
848997b5 | 2987 | }\r |
a405b86d | 2988 | }\r |
2989 | \r | |
2990 | if (ShellCommandGetScriptExit()) {\r | |
5b5cd144 CP |
2991 | //\r |
2992 | // ShellCommandGetExitCode() always returns a UINT64\r | |
2993 | //\r | |
a308e058 | 2994 | UnicodeSPrint(LeString, sizeof(LeString), L"0x%Lx", ShellCommandGetExitCode());\r |
5b5cd144 CP |
2995 | DEBUG_CODE(InternalEfiShellSetEnv(L"debuglasterror", LeString, TRUE););\r |
2996 | InternalEfiShellSetEnv(L"lasterror", LeString, TRUE);\r | |
b6b22b13 | 2997 | \r |
2998 | ShellCommandRegisterExit(FALSE, 0);\r | |
a405b86d | 2999 | Status = EFI_SUCCESS;\r |
14810d6b | 3000 | RestoreBufferList(&OldBufferList);\r |
a405b86d | 3001 | break;\r |
3002 | }\r | |
c6a7fef8 | 3003 | if (ShellGetExecutionBreakFlag()) {\r |
14810d6b | 3004 | RestoreBufferList(&OldBufferList);\r |
c6a7fef8 ED |
3005 | break;\r |
3006 | }\r | |
a405b86d | 3007 | if (EFI_ERROR(Status)) {\r |
14810d6b | 3008 | RestoreBufferList(&OldBufferList);\r |
a405b86d | 3009 | break;\r |
3010 | }\r | |
3011 | if (ShellCommandGetExit()) {\r | |
14810d6b | 3012 | RestoreBufferList(&OldBufferList);\r |
a405b86d | 3013 | break;\r |
3014 | }\r | |
3015 | }\r | |
3016 | //\r | |
3017 | // If that commend did not update the CurrentCommand then we need to advance it...\r | |
3018 | //\r | |
3019 | if (LastCommand == NewScriptFile->CurrentCommand) {\r | |
3020 | NewScriptFile->CurrentCommand = (SCRIPT_COMMAND_LIST *)GetNextNode(&NewScriptFile->CommandList, &NewScriptFile->CurrentCommand->Link);\r | |
3021 | if (!IsNull(&NewScriptFile->CommandList, &NewScriptFile->CurrentCommand->Link)) {\r | |
3022 | NewScriptFile->CurrentCommand->Reset = TRUE;\r | |
3023 | }\r | |
3024 | }\r | |
3025 | } else {\r | |
3026 | NewScriptFile->CurrentCommand = (SCRIPT_COMMAND_LIST *)GetNextNode(&NewScriptFile->CommandList, &NewScriptFile->CurrentCommand->Link);\r | |
3027 | if (!IsNull(&NewScriptFile->CommandList, &NewScriptFile->CurrentCommand->Link)) {\r | |
3028 | NewScriptFile->CurrentCommand->Reset = TRUE;\r | |
3029 | }\r | |
3030 | }\r | |
14810d6b | 3031 | RestoreBufferList(&OldBufferList);\r |
a405b86d | 3032 | }\r |
3033 | \r | |
a405b86d | 3034 | \r |
3035 | FreePool(CommandLine);\r | |
3036 | FreePool(CommandLine2);\r | |
3037 | ShellCommandSetNewScript (NULL);\r | |
733f138d | 3038 | \r |
3039 | //\r | |
3040 | // Only if this was the last script reset the state.\r | |
3041 | //\r | |
3042 | if (ShellCommandGetCurrentScriptFile()==NULL) {\r | |
3043 | ShellCommandSetEchoState(PreScriptEchoState);\r | |
3044 | }\r | |
a405b86d | 3045 | return (EFI_SUCCESS);\r |
3046 | }\r | |
3047 | \r | |
3048 | /**\r | |
3049 | Function to process a NSH script file.\r | |
3050 | \r | |
3051 | @param[in] ScriptPath Pointer to the script file name (including file system path).\r | |
e958b946 JC |
3052 | @param[in] Handle the handle of the script file already opened.\r |
3053 | @param[in] CmdLine the command line to run.\r | |
3054 | @param[in] ParamProtocol the shell parameters protocol pointer\r | |
a405b86d | 3055 | \r |
d08a5464 | 3056 | @retval EFI_SUCCESS the script completed successfully\r |
a405b86d | 3057 | **/\r |
3058 | EFI_STATUS\r | |
3059 | EFIAPI\r | |
3060 | RunScriptFile (\r | |
a308e058 RN |
3061 | IN CONST CHAR16 *ScriptPath,\r |
3062 | IN SHELL_FILE_HANDLE Handle OPTIONAL,\r | |
3063 | IN CONST CHAR16 *CmdLine,\r | |
3064 | IN EFI_SHELL_PARAMETERS_PROTOCOL *ParamProtocol\r | |
a405b86d | 3065 | )\r |
3066 | {\r | |
3067 | EFI_STATUS Status;\r | |
3068 | SHELL_FILE_HANDLE FileHandle;\r | |
e958b946 JC |
3069 | UINTN Argc;\r |
3070 | CHAR16 **Argv;\r | |
a405b86d | 3071 | \r |
3072 | if (ShellIsFile(ScriptPath) != EFI_SUCCESS) {\r | |
3073 | return (EFI_INVALID_PARAMETER);\r | |
3074 | }\r | |
3075 | \r | |
e958b946 JC |
3076 | //\r |
3077 | // get the argc and argv updated for scripts\r | |
3078 | //\r | |
d1c275c6 | 3079 | Status = UpdateArgcArgv(ParamProtocol, CmdLine, Script_File_Name, &Argv, &Argc);\r |
e958b946 | 3080 | if (!EFI_ERROR(Status)) {\r |
a405b86d | 3081 | \r |
e958b946 JC |
3082 | if (Handle == NULL) {\r |
3083 | //\r | |
3084 | // open the file\r | |
3085 | //\r | |
3086 | Status = ShellOpenFileByName(ScriptPath, &FileHandle, EFI_FILE_MODE_READ, 0);\r | |
3087 | if (!EFI_ERROR(Status)) {\r | |
3088 | //\r | |
3089 | // run it\r | |
3090 | //\r | |
a308e058 | 3091 | Status = RunScriptFileHandle(FileHandle, ScriptPath);\r |
a405b86d | 3092 | \r |
e958b946 JC |
3093 | //\r |
3094 | // now close the file\r | |
3095 | //\r | |
3096 | ShellCloseFile(&FileHandle);\r | |
3097 | }\r | |
3098 | } else {\r | |
a308e058 | 3099 | Status = RunScriptFileHandle(Handle, ScriptPath);\r |
e958b946 JC |
3100 | }\r |
3101 | }\r | |
3102 | \r | |
3103 | //\r | |
d08a5464 | 3104 | // This is guaranteed to be called after UpdateArgcArgv no matter what else happened.\r |
e958b946 JC |
3105 | // This is safe even if the update API failed. In this case, it may be a no-op.\r |
3106 | //\r | |
3107 | RestoreArgcArgv(ParamProtocol, &Argv, &Argc);\r | |
a405b86d | 3108 | \r |
3109 | return (Status);\r | |
3110 | }\r | |
00534bc3 JC |
3111 | \r |
3112 | /**\r | |
d08a5464 | 3113 | Return the pointer to the first occurrence of any character from a list of characters.\r |
00534bc3 JC |
3114 | \r |
3115 | @param[in] String the string to parse\r | |
3116 | @param[in] CharacterList the list of character to look for\r | |
3117 | @param[in] EscapeCharacter An escape character to skip\r | |
3118 | \r | |
3119 | @return the location of the first character in the string\r | |
3120 | @retval CHAR_NULL no instance of any character in CharacterList was found in String\r | |
3121 | **/\r | |
3122 | CONST CHAR16*\r | |
3123 | EFIAPI\r | |
3124 | FindFirstCharacter(\r | |
3125 | IN CONST CHAR16 *String,\r | |
3126 | IN CONST CHAR16 *CharacterList,\r | |
3127 | IN CONST CHAR16 EscapeCharacter\r | |
3128 | )\r | |
3129 | {\r | |
3130 | UINT32 WalkChar;\r | |
3131 | UINT32 WalkStr;\r | |
3132 | \r | |
3133 | for (WalkStr = 0; WalkStr < StrLen(String); WalkStr++) {\r | |
3134 | if (String[WalkStr] == EscapeCharacter) {\r | |
3135 | WalkStr++;\r | |
3136 | continue;\r | |
3137 | }\r | |
3138 | for (WalkChar = 0; WalkChar < StrLen(CharacterList); WalkChar++) {\r | |
3139 | if (String[WalkStr] == CharacterList[WalkChar]) {\r | |
3140 | return (&String[WalkStr]);\r | |
3141 | }\r | |
3142 | }\r | |
3143 | }\r | |
3144 | return (String + StrLen(String));\r | |
88ff5ba7 | 3145 | }\r |