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