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