2 Basic command line parser for EBL (Embedded Boot Loader)
4 Copyright (c) 2007, Intel Corporation. All rights reserved.<BR>
5 Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
20 // Globals for command history processing
21 INTN mCmdHistoryEnd
= -1;
22 INTN mCmdHistoryStart
= -1;
23 INTN mCmdHistoryCurrent
= -1;
24 CHAR8 mCmdHistory
[MAX_CMD_HISTORY
][MAX_CMD_LINE
];
25 CHAR8
*mCmdBlank
= "";
27 // Globals to remember current screen geometry
31 // Global to turn on/off breaking commands with prompts before they scroll the screen
32 BOOLEAN gPageBreak
= TRUE
;
41 if (*Value
>= MAX_CMD_HISTORY
) {
54 *Value
= MAX_CMD_HISTORY
- 1;
59 Save this command in the circular history buffer. Older commands are
60 overwritten with newer commands.
62 @param Cmd Command line to archive the history of.
72 // Don't bother adding empty commands to the list
73 if (AsciiStrLen(Cmd
) != 0) {
76 if (mCmdHistoryStart
== -1) {
80 // Record the new command at the next index
81 RingBufferIncrement(&mCmdHistoryStart
);
83 // If the next index runs into the end index, shuffle end back by one
84 if (mCmdHistoryStart
== mCmdHistoryEnd
) {
85 RingBufferIncrement(&mCmdHistoryEnd
);
89 // Copy the new command line into the ring buffer
90 AsciiStrnCpy(&mCmdHistory
[mCmdHistoryStart
][0], Cmd
, MAX_CMD_LINE
);
93 // Reset the command history for the next up arrow press
94 mCmdHistoryCurrent
= mCmdHistoryStart
;
99 Retreave data from the Command History buffer. Direction maps into up arrow
100 an down arrow on the command line
102 @param Direction Command forward or back
104 @return The Command history based on the Direction
112 CHAR8
*HistoricalCommand
= NULL
;
115 if (mCmdHistoryCurrent
== -1) {
116 HistoricalCommand
= mCmdBlank
;
120 if (Direction
== SCAN_UP
) {
121 HistoricalCommand
= &mCmdHistory
[mCmdHistoryCurrent
][0];
123 // if we just echoed the last command, hang out there, don't wrap around
124 if (mCmdHistoryCurrent
== mCmdHistoryEnd
) {
128 // otherwise, back up by one
129 RingBufferDecrement(&mCmdHistoryCurrent
);
131 } else if (Direction
== SCAN_DOWN
) {
133 // if we last echoed the start command, put a blank prompt out
134 if (mCmdHistoryCurrent
== mCmdHistoryStart
) {
135 HistoricalCommand
= mCmdBlank
;
139 // otherwise increment the current pointer and return that command
140 RingBufferIncrement(&mCmdHistoryCurrent
);
141 RingBufferIncrement(&mCmdHistoryCurrent
);
143 HistoricalCommand
= &mCmdHistory
[mCmdHistoryCurrent
][0];
144 RingBufferDecrement(&mCmdHistoryCurrent
);
148 return HistoricalCommand
;
153 Parse the CmdLine and break it up into Argc (arg count) and Argv (array of
154 pointers to each argument). The Cmd buffer is altered and seperators are
155 converted to string terminators. This allows Argv to point into CmdLine.
156 A CmdLine can support multiple commands. The next command in the command line
157 is returned if it exists.
159 @param CmdLine String to parse for a set of commands
160 @param Argc Returns the number of arguments in the CmdLine current command
161 @param Argv Argc pointers to each string in CmdLine
163 @return Next Command in the command line or NULL if non exists
174 BOOLEAN LookingForArg
;
178 if (AsciiStrLen (CmdLine
) == 0) {
182 // Walk a single command line. A CMD_SEPERATOR allows mult commands on a single line
184 LookingForArg
= TRUE
;
185 for (Char
= CmdLine
, Arg
= 0; *Char
!= '\0'; Char
++) {
186 if (!InQuote
&& *Char
== CMD_SEPERATOR
) {
190 // Perform any text coversion here
197 // Look for the beging of an Argv[] entry
199 Argv
[Arg
++] = ++Char
;
200 LookingForArg
= FALSE
;
202 } else if (*Char
!= ' ') {
204 LookingForArg
= FALSE
;
207 // Looking for the terminator of an Argv[] entry
208 if ((InQuote
&& (*Char
== '"')) || (!InQuote
&& (*Char
== ' '))) {
210 LookingForArg
= TRUE
;
217 if (*Char
== CMD_SEPERATOR
) {
218 // Replace the command delimeter with null and return pointer to next command line
228 Return a keypress or optionally timeout if a timeout value was passed in.
229 An optional callback funciton is called evey second when waiting for a
232 @param Key EFI Key information returned
233 @param TimeoutInSec Number of seconds to wait to timeout
234 @param CallBack Callback called every second during the timeout wait
236 @return EFI_SUCCESS Key was returned
237 @return EFI_TIMEOUT If the TimoutInSec expired
242 IN OUT EFI_INPUT_KEY
*Key
,
243 IN UINTN TimeoutInSec
,
244 IN EBL_GET_CHAR_CALL_BACK CallBack OPTIONAL
250 EFI_EVENT WaitList
[2];
253 WaitList
[0] = gST
->ConIn
->WaitForKey
;
254 if (TimeoutInSec
!= 0) {
255 // Create a time event for 1 sec duration if we have a timeout
256 gBS
->CreateEvent (EVT_TIMER
, 0, NULL
, NULL
, &WaitList
[1]);
257 gBS
->SetTimer (WaitList
[1], TimerPeriodic
, EFI_SET_TIMER_TO_SECOND
);
262 Status
= gBS
->WaitForEvent (WaitCount
, WaitList
, &WaitIndex
);
263 ASSERT_EFI_ERROR (Status
);
267 // Key event signaled
268 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, Key
);
269 if (!EFI_ERROR (Status
)) {
270 if (WaitCount
== 2) {
271 gBS
->CloseEvent (WaitList
[1]);
278 // Periodic 1 sec timer signaled
280 if (CallBack
!= NULL
) {
281 // Call the users callback function if registered
282 CallBack (TimeoutInSec
);
284 if (TimeoutInSec
== 0) {
285 gBS
->CloseEvent (WaitList
[1]);
297 This routine is used prevent command output data from scrolling off the end
298 of the screen. The global gPageBreak is used to turn on or off this feature.
299 If the CurrentRow is near the end of the screen pause and print out a prompt
300 If the use hits Q to quit return TRUE else for any other key return FALSE.
301 PrefixNewline is used to figure out if a newline is needed before the prompt
302 string. This depends on the last print done before calling this function.
303 CurrentRow is updated by one on a call or set back to zero if a prompt is
306 @param CurrentRow Used to figure out if its the end of the page and updated
307 @param PrefixNewline Did previous print issue a newline
309 @return TRUE if Q was hit to quit, FALSE in all other cases.
313 EblAnyKeyToContinueQtoQuit (
314 IN UINTN
*CurrentRow
,
315 IN BOOLEAN PrefixNewline
318 EFI_INPUT_KEY InputKey
;
321 // global disable for this feature
325 if (*CurrentRow
>= (gScreenRows
- 2)) {
329 AsciiPrint ("Any key to continue (Q to quit): ");
330 EblGetCharKey (&InputKey
, 0, NULL
);
333 // Time to promt to stop the screen. We have to leave space for the prompt string
335 if (InputKey
.UnicodeChar
== 'Q' || InputKey
.UnicodeChar
== 'q') {
347 Set the text color of the EFI Console. If a zero is passed in reset to
348 default text/background color.
350 @param Attribute For text and background color
358 if (Attribute
== 0) {
359 // Set the text color back to default
360 Attribute
= (UINTN
)PcdGet32 (PcdEmbeddedDefaultTextColor
);
363 gST
->ConOut
->SetAttribute (gST
->ConOut
, Attribute
);
368 Collect the keyboard input for a cmd line. Carage Return, New Line, or ESC
369 terminates the command line. You can edit the command line via left arrow,
370 delete and backspace and they all back up and erase the command line.
371 No edit of commnad line is possible without deletion at this time!
372 The up arrow and down arrow fill Cmd with information from the history
375 @param Cmd Command line to return
376 @param CmdMaxSize Maximum size of Cmd
378 @return The Status of EblGetCharKey()
394 for (Index
= 0; Index
< CmdMaxSize
- 1;) {
395 Status
= EblGetCharKey (&Key
, 0, NULL
);
396 if (EFI_ERROR (Status
)) {
402 Char
= (CHAR8
)Key
.UnicodeChar
;
403 if ((Char
== '\n') || (Char
== '\r') || (Char
== 0x7f)) {
405 if (FixedPcdGetBool(PcdEmbeddedShellCharacterEcho
) == TRUE
) {
409 } else if ((Char
== '\b') || (Key
.ScanCode
== SCAN_LEFT
) || (Key
.ScanCode
== SCAN_DELETE
)){
413 // Update the display
415 AsciiPrint ("\b \b");
417 } else if ((Key
.ScanCode
== SCAN_UP
) || Key
.ScanCode
== SCAN_DOWN
) {
418 History
= GetCmdHistory (Key
.ScanCode
);
420 // Clear display line
422 for (Index2
= 0; Index2
< Index
; Index2
++) {
423 AsciiPrint ("\b \b");
425 AsciiPrint (History
);
426 Index
= AsciiStrLen (History
);
427 AsciiStrnCpy (Cmd
, History
, CmdMaxSize
);
430 if (FixedPcdGetBool(PcdEmbeddedShellCharacterEcho
) == TRUE
) {
431 AsciiPrint ("%c", Char
);
441 Print the boot up banner for the EBL.
444 EblPrintStartupBanner (
448 AsciiPrint ("Embedded Boot Loader (");
449 EblSetTextColor (EFI_YELLOW
);
452 AsciiPrint (") prototype. Built at %a on %a\n",__TIME__
, __DATE__
);
453 AsciiPrint ("THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN 'AS IS' BASIS,\nWITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\n");
454 AsciiPrint ("Please send feedback to edk2-devel@lists.sourceforge.net\n");
459 Send null requests to all removable media block IO devices so the a media add/remove/change
460 can be detected in real before we execute a command.
462 This is mainly due to the fact that the FAT driver does not do this today so you can get stale
463 dir commands after an SD Card has been removed.
466 EblProbeRemovableMedia (
475 // Probe for media insertion/removal in removable media devices
477 Max
= EfiGetDeviceCounts (EfiOpenBlockIo
);
479 for (Index
= 0; Index
< Max
; Index
++) {
480 File
= EfiDeviceOpenByType (EfiOpenBlockIo
, Index
);
482 if (File
->FsBlockIoMedia
->RemovableMedia
) {
483 // Probe to see if media is present (or not) or media changed
484 // this causes the ReinstallProtocolInterface() to fire in the
485 // block io driver to update the system about media change events
486 File
->FsBlockIo
->ReadBlocks (File
->FsBlockIo
, File
->FsBlockIo
->Media
->MediaId
, (EFI_LBA
)0, 0, NULL
);
498 Print the prompt for the EBL.
505 EblSetTextColor (EFI_YELLOW
);
506 AsciiPrint ((CHAR8
*)PcdGetPtr (PcdEmbeddedPrompt
), EfiGetCwd ());
508 AsciiPrint ("%a", ">");
514 Parse a command line and execute the commands. The ; seperator allows
515 multiple commands for each command line. Stop processing if one of the
516 commands returns an error.
518 @param CmdLine Command Line to process.
519 @param MaxCmdLineSize MaxSize of the Command line
521 @return EFI status of the Command
527 IN UINTN MaxCmdLineSize
531 EBL_COMMAND_TABLE
*Cmd
;
534 CHAR8
*Argv
[MAX_ARGS
];
536 // Parse the command line. The loop processes commands seperated by ;
537 for (Ptr
= CmdLine
, Status
= EFI_SUCCESS
; Ptr
!= NULL
;) {
538 Ptr
= ParseArguments (Ptr
, &Argc
, Argv
);
540 Cmd
= EblGetCommand (Argv
[0]);
542 // Execute the Command!
543 Status
= Cmd
->Command (Argc
, Argv
);
544 if (Status
== EFI_ABORTED
) {
545 // exit command so lets exit
547 } else if (Status
== EFI_TIMEOUT
) {
548 // pause command got imput so don't process any more cmd on this cmd line
550 } else if (EFI_ERROR (Status
)) {
551 AsciiPrint ("%a returned %r error\n", Cmd
->Name
, Status
);
552 // if any command fails stop processing CmdLine
565 Embedded Boot Loader (EBL) - A simple EFI command line application for embedded
566 devices. PcdEmbeddedAutomaticBootCommand is a complied in commnad line that
567 gets executed automatically. The ; seperator allows multiple commands
568 for each command line.
570 @param ImageHandle EFI ImageHandle for this application.
571 @param SystemTable EFI system table
573 @return EFI status of the applicaiton
579 IN EFI_HANDLE ImageHandle
,
580 IN EFI_SYSTEM_TABLE
*SystemTable
584 CHAR8 CmdLine
[MAX_CMD_LINE
];
585 CHAR16
*CommandLineVariable
= NULL
;
586 CHAR16
*CommandLineVariableName
= L
"default-cmdline";
587 UINTN CommandLineVariableSize
= 0;
590 // Initialize tables of commnads
591 EblInitializeCmdTable ();
592 EblInitializeDeviceCmd ();
593 EblInitializemdHwDebugCmds ();
594 EblInitializemdHwIoDebugCmds ();
595 EblInitializeDirCmd ();
596 EblInitializeHobCmd ();
597 EblInitializeScriptCmd ();
598 EblInitializeExternalCmd ();
599 EblInitializeNetworkCmd();
601 // Disable the 5 minute EFI watchdog time so we don't get automatically reset
602 gBS
->SetWatchdogTimer (0, 0, 0, NULL
);
604 if (FeaturePcdGet (PcdEmbeddedMacBoot
)) {
605 // A MAC will boot in graphics mode, so turn it back to text here
606 // This protocol was removed from edk2. It is only an edk thing. We need to make our own copy.
607 // DisableQuietBoot ();
609 // Enable the biggest output screen size possible
610 gST
->ConOut
->SetMode (gST
->ConOut
, (UINTN
)gST
->ConOut
->Mode
->MaxMode
- 1);
614 // Save current screen mode
615 gST
->ConOut
->QueryMode (gST
->ConOut
, gST
->ConOut
->Mode
->Mode
, &gScreenColumns
, &gScreenRows
);
617 EblPrintStartupBanner ();
619 // Parse command line and handle commands seperated by ;
620 // The loop prints the prompt gets user input and saves history
622 // Look for a variable with a default command line, otherwise use the Pcd
623 ZeroMem(&VendorGuid
, sizeof(EFI_GUID
));
625 Status
= gRT
->GetVariable(CommandLineVariableName
, &VendorGuid
, NULL
, &CommandLineVariableSize
, CommandLineVariable
);
626 if (Status
== EFI_BUFFER_TOO_SMALL
) {
627 CommandLineVariable
= AllocatePool(CommandLineVariableSize
);
629 Status
= gRT
->GetVariable(CommandLineVariableName
, &VendorGuid
, NULL
, &CommandLineVariableSize
, CommandLineVariable
);
630 if (!EFI_ERROR(Status
)) {
631 UnicodeStrToAsciiStr(CommandLineVariable
, CmdLine
);
634 FreePool(CommandLineVariable
);
637 if (EFI_ERROR(Status
)) {
638 AsciiStrCpy (CmdLine
, (CHAR8
*)PcdGetPtr (PcdEmbeddedAutomaticBootCommand
));
642 Status
= ProcessCmdLine (CmdLine
, MAX_CMD_LINE
);
643 if (Status
== EFI_ABORTED
) {
644 // if a command returns EFI_ABORTED then exit the EBL
645 EblShutdownExternalCmdTable ();
649 // get the command line from the user
651 GetCmd (CmdLine
, MAX_CMD_LINE
);
652 SetCmdHistory (CmdLine
);
654 if (FeaturePcdGet (PcdEmbeddedProbeRemovable
)) {
655 // Probe removable media devices to see if media has been inserted or removed.
656 EblProbeRemovableMedia ();