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