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