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