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