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