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