summaryrefslogtreecommitdiff
path: root/EmbeddedPkg/Ebl/Main.c
blob: 8f6f4ea9e1579fbf06c10fe95471936738b14c46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
/** @file
  Basic command line parser for EBL (Embedded Boot Loader)

  Copyright (c) 2007, Intel Corporation. All rights reserved.<BR>
  Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>

  This program and the accompanying materials
  are licensed and made available under the terms and conditions of the BSD License
  which accompanies this distribution.  The full text of the license may be found at
  http://opensource.org/licenses/bsd-license.php

  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.


**/

#include "Ebl.h"

// Globals for command history processing
INTN mCmdHistoryEnd     = -1;
INTN mCmdHistoryStart   = -1;
INTN mCmdHistoryCurrent = -1;
CHAR8 mCmdHistory[MAX_CMD_HISTORY][MAX_CMD_LINE];
CHAR8 *mCmdBlank = "";

// Globals to remember current screen geometry
UINTN gScreenColumns;
UINTN gScreenRows;

// Global to turn on/off breaking commands with prompts before they scroll the screen
BOOLEAN gPageBreak = TRUE;

VOID 
RingBufferIncrement (
  IN  INTN  *Value
  )
{
  *Value = *Value + 1;
  
  if (*Value >= MAX_CMD_HISTORY) {
    *Value = 0;
  }
}

VOID
RingBufferDecrement (
  IN  INTN  *Value
  )
{
  *Value = *Value - 1;
  
  if (*Value < 0) {
    *Value = MAX_CMD_HISTORY - 1;
  }
}

/**
  Save this command in the circular history buffer. Older commands are 
  overwritten with newer commands.

  @param  Cmd   Command line to archive the history of.

  @return None

**/
VOID
SetCmdHistory (
  IN  CHAR8 *Cmd
  )
{
  // Don't bother adding empty commands to the list
  if (AsciiStrLen(Cmd) != 0) {
  
    // First entry
    if (mCmdHistoryStart == -1) {
      mCmdHistoryStart   = 0;
      mCmdHistoryEnd     = 0;
    } else {
      // Record the new command at the next index
      RingBufferIncrement(&mCmdHistoryStart);
  
      // If the next index runs into the end index, shuffle end back by one
      if (mCmdHistoryStart == mCmdHistoryEnd) {
        RingBufferIncrement(&mCmdHistoryEnd);
      }
    }
  
    // Copy the new command line into the ring buffer
    AsciiStrnCpy(&mCmdHistory[mCmdHistoryStart][0], Cmd, MAX_CMD_LINE);
  }
  
  // Reset the command history for the next up arrow press
  mCmdHistoryCurrent = mCmdHistoryStart;  
}


/**
  Retreave data from the Command History buffer. Direction maps into up arrow
  an down arrow on the command line

  @param  Direction  Command forward or back

  @return The Command history based on the Direction

**/
CHAR8 *
GetCmdHistory (
  IN UINT16   Direction
  )
{
  CHAR8 *HistoricalCommand = NULL;
  
  // No history yet?
  if (mCmdHistoryCurrent == -1) {
    HistoricalCommand = mCmdBlank;
    goto Exit;
  }
  
  if (Direction == SCAN_UP) {
    HistoricalCommand = &mCmdHistory[mCmdHistoryCurrent][0];
    
    // if we just echoed the last command, hang out there, don't wrap around
    if (mCmdHistoryCurrent == mCmdHistoryEnd) {
      goto Exit;
    }
    
    // otherwise, back up by one
    RingBufferDecrement(&mCmdHistoryCurrent);
    
  } else if (Direction == SCAN_DOWN) {
    
    // if we last echoed the start command, put a blank prompt out
    if (mCmdHistoryCurrent == mCmdHistoryStart) {
      HistoricalCommand = mCmdBlank;
      goto Exit;
    }
    
    // otherwise increment the current pointer and return that command
    RingBufferIncrement(&mCmdHistoryCurrent);
    RingBufferIncrement(&mCmdHistoryCurrent);
    
    HistoricalCommand = &mCmdHistory[mCmdHistoryCurrent][0];
    RingBufferDecrement(&mCmdHistoryCurrent);
  }

Exit:  
  return HistoricalCommand;
}


/**
  Parse the CmdLine and break it up into Argc (arg count) and Argv (array of
  pointers to each argument). The Cmd buffer is altered and seperators are 
  converted to string terminators. This allows Argv to point into CmdLine.
  A CmdLine can support multiple commands. The next command in the command line
  is returned if it exists.

  @param  CmdLine String to parse for a set of commands
  @param  Argc    Returns the number of arguments in the CmdLine current command
  @param  Argv    Argc pointers to each string in CmdLine

  @return Next Command in the command line or NULL if non exists
**/
CHAR8 *
ParseArguments (
  IN  CHAR8  *CmdLine,
  OUT UINTN  *Argc,
  OUT CHAR8  **Argv
  )
{
  UINTN   Arg;
  CHAR8   *Char;
  BOOLEAN LookingForArg;
  BOOLEAN InQuote;

  *Argc = 0;
  if (AsciiStrLen (CmdLine) == 0) {
    return NULL;
  }

  // Walk a single command line. A CMD_SEPERATOR allows mult commands on a single line
  InQuote       = FALSE;
  LookingForArg = TRUE;
  for (Char = CmdLine, Arg = 0; *Char != '\0'; Char++) {
    if (!InQuote && *Char == CMD_SEPERATOR) {
      break;
    }

    // Perform any text conversion here
    if (*Char == '\t') {
      // TAB to space
      *Char = ' ';
    }

    if (LookingForArg) {
      // Look for the beging of an Argv[] entry
      if (*Char == '"') {
        Argv[Arg++] = ++Char;
        LookingForArg = FALSE;
        InQuote = TRUE;
      } else if (*Char != ' ') {
        Argv[Arg++] = Char;
        LookingForArg = FALSE;
      } 
    } else {
      // Looking for the terminator of an Argv[] entry
      if (!InQuote && (*Char == ' ')) {
        *Char = '\0';
        LookingForArg = TRUE;
      } else if (!InQuote && (*Char == '"') && (*(Char-1) != '\\')) {
        InQuote = TRUE;
      } else if (InQuote && (*Char == '"') && (*(Char-1) != '\\')) {
        *Char = '\0';
        InQuote = FALSE;
      }
    }    
  }

  *Argc = Arg;

  if (*Char == CMD_SEPERATOR) {
    // Replace the command delimeter with null and return pointer to next command line
    *Char = '\0';
    return ++Char;
  }

  return NULL;
}


/**
  Return a keypress or optionally timeout if a timeout value was passed in.
  An optional callback funciton is called evey second when waiting for a
  timeout.

  @param  Key           EFI Key information returned
  @param  TimeoutInSec  Number of seconds to wait to timeout
  @param  CallBack      Callback called every second during the timeout wait 

  @return EFI_SUCCESS  Key was returned
  @return EFI_TIMEOUT  If the TimoutInSec expired

**/
EFI_STATUS
EblGetCharKey (
  IN OUT EFI_INPUT_KEY            *Key,
  IN     UINTN                    TimeoutInSec,
  IN     EBL_GET_CHAR_CALL_BACK   CallBack   OPTIONAL
  )
{
  EFI_STATUS    Status;
  UINTN         WaitCount;
  UINTN         WaitIndex;
  EFI_EVENT     WaitList[2];

  WaitCount   = 1;
  WaitList[0] = gST->ConIn->WaitForKey;
  if (TimeoutInSec != 0) {
    // Create a time event for 1 sec duration if we have a timeout
    gBS->CreateEvent (EVT_TIMER, 0, NULL, NULL, &WaitList[1]);
    gBS->SetTimer (WaitList[1], TimerPeriodic, EFI_SET_TIMER_TO_SECOND);
    WaitCount++;
  }

  for (;;) {
    Status = gBS->WaitForEvent (WaitCount, WaitList, &WaitIndex);
    ASSERT_EFI_ERROR (Status);

    switch (WaitIndex) {
    case 0:
      // Key event signaled
      Status = gST->ConIn->ReadKeyStroke (gST->ConIn, Key);
      if (!EFI_ERROR (Status)) {
        if (WaitCount == 2) {
          gBS->CloseEvent (WaitList[1]);
        }
        return EFI_SUCCESS; 
      }
      break;

    case 1:
      // Periodic 1 sec timer signaled 
      TimeoutInSec--;
      if (CallBack != NULL) {
        // Call the users callback function if registered 
        CallBack (TimeoutInSec);
      }
      if (TimeoutInSec == 0) {
        gBS->CloseEvent (WaitList[1]);
        return EFI_TIMEOUT;
      }
      break;
    default:
      ASSERT (FALSE);
    }
  } 
}


/**
  This routine is used prevent command output data from scrolling off the end
  of the screen. The global gPageBreak is used to turn on or off this feature.
  If the CurrentRow is near the end of the screen pause and print out a prompt
  If the use hits Q to quit return TRUE else for any other key return FALSE.
  PrefixNewline is used to figure out if a newline is needed before the prompt
  string. This depends on the last print done before calling this function.
  CurrentRow is updated by one on a call or set back to zero if a prompt is 
  needed.

  @param  CurrentRow  Used to figure out if its the end of the page and updated
  @param  PrefixNewline  Did previous print issue a newline

  @return TRUE if Q was hit to quit, FALSE in all other cases.

**/
BOOLEAN
EblAnyKeyToContinueQtoQuit (
  IN  UINTN   *CurrentRow,
  IN  BOOLEAN PrefixNewline
  )
{
  EFI_INPUT_KEY     InputKey;

  if (!gPageBreak) {
    // global disable for this feature
    return FALSE;
  }

  if (*CurrentRow >= (gScreenRows - 2)) {
    if (PrefixNewline) {
      AsciiPrint ("\n");
    }
    AsciiPrint ("Any key to continue (Q to quit): ");
    EblGetCharKey (&InputKey, 0, NULL);
    AsciiPrint ("\n");

    // Time to promt to stop the screen. We have to leave space for the prompt string
    *CurrentRow = 0;
    if (InputKey.UnicodeChar == 'Q' || InputKey.UnicodeChar == 'q') {
      return TRUE;
    }
  } else {
    *CurrentRow += 1;
  }

  return FALSE;
}


/**
  Set the text color of the EFI Console. If a zero is passed in reset to 
  default text/background color.

  @param  Attribute   For text and background color

**/
VOID
EblSetTextColor (
  UINTN   Attribute
  )
{
  if (Attribute == 0) {
    // Set the text color back to default
    Attribute = (UINTN)PcdGet32 (PcdEmbeddedDefaultTextColor);
  }

  gST->ConOut->SetAttribute (gST->ConOut, Attribute);
}


/**
  Collect the keyboard input for a cmd line. Carage Return, New Line, or ESC
  terminates the command line. You can edit the command line via left arrow,
  delete and backspace and they all back up and erase the command line. 
  No edit of commnad line is possible without deletion at this time!
  The up arrow and down arrow fill Cmd with information from the history 
  buffer.

  @param  Cmd         Command line to return 
  @param  CmdMaxSize  Maximum size of Cmd

  @return The Status of EblGetCharKey()

**/
EFI_STATUS
GetCmd (
  IN OUT  CHAR8   *Cmd,
  IN      UINTN   CmdMaxSize
  )
{
  EFI_STATUS    Status;
  UINTN         Index;
  UINTN         Index2;
  CHAR8         Char;
  CHAR8         *History;
  EFI_INPUT_KEY Key;

  for (Index = 0; Index < CmdMaxSize - 1;) {
    Status = EblGetCharKey (&Key, 0, NULL);
    if (EFI_ERROR (Status)) {
      Cmd[Index] = '\0';
      AsciiPrint ("\n");
      return Status;
    }

    Char = (CHAR8)Key.UnicodeChar;
    if ((Char == '\n') || (Char == '\r') || (Char == 0x7f)) {
      Cmd[Index] = '\0';
      if (FixedPcdGetBool(PcdEmbeddedShellCharacterEcho) == TRUE) {
        AsciiPrint ("\n\r");
      }
      return EFI_SUCCESS;
    } else if ((Char == '\b') || (Key.ScanCode == SCAN_LEFT) || (Key.ScanCode == SCAN_DELETE)){
      if (Index != 0) {
        Index--;
        //
        // Update the display
        //
        AsciiPrint ("\b \b");
      }
    } else if ((Key.ScanCode == SCAN_UP) || Key.ScanCode == SCAN_DOWN) {
      History = GetCmdHistory (Key.ScanCode);
      //
      // Clear display line
      //
      for (Index2 = 0; Index2 < Index; Index2++) {
        AsciiPrint ("\b \b");
      }
      AsciiPrint (History);
      Index = AsciiStrLen (History);
      AsciiStrnCpy (Cmd, History, CmdMaxSize);
    } else {
      Cmd[Index++] = Char;
      if (FixedPcdGetBool(PcdEmbeddedShellCharacterEcho) == TRUE) {
        AsciiPrint ("%c", Char);
      }
    }
  }

  return EFI_SUCCESS;
}


/**
  Print the boot up banner for the EBL.
**/
VOID
EblPrintStartupBanner (
  VOID
  )
{
  AsciiPrint ("Embedded Boot Loader (");
  EblSetTextColor (EFI_YELLOW);
  AsciiPrint ("EBL");
  EblSetTextColor (0);
  AsciiPrint (") prototype. Built at %a on %a\n",__TIME__, __DATE__);
  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");
  AsciiPrint ("Please send feedback to edk2-devel@lists.sourceforge.net\n");
}


/**
  Send null requests to all removable media block IO devices so the a media add/remove/change
  can be detected in real before we execute a command. 

  This is mainly due to the fact that the FAT driver does not do this today so you can get stale 
  dir commands after an SD Card has been removed.
**/
VOID
EblProbeRemovableMedia (
  VOID
  )
{
  UINTN         Index;
  UINTN         Max;
  EFI_OPEN_FILE *File;

  //
  // Probe for media insertion/removal in removable media devices
  //
  Max = EfiGetDeviceCounts (EfiOpenBlockIo);
  if (Max != 0) {
    for (Index = 0; Index < Max; Index++) {
      File = EfiDeviceOpenByType (EfiOpenBlockIo, Index);
      if (File != NULL) {
        if (File->FsBlockIoMedia->RemovableMedia) {
          // Probe to see if media is present (or not) or media changed
          //  this causes the ReinstallProtocolInterface() to fire in the
          //  block io driver to update the system about media change events
          File->FsBlockIo->ReadBlocks (File->FsBlockIo, File->FsBlockIo->Media->MediaId, (EFI_LBA)0, 0, NULL);
        }
        EfiClose (File);
      }
    }
  }
}




/**
  Print the prompt for the EBL.
**/
VOID
EblPrompt (
  VOID
  )
{
  EblSetTextColor (EFI_YELLOW);
  AsciiPrint ((CHAR8 *)PcdGetPtr (PcdEmbeddedPrompt), EfiGetCwd ());
  EblSetTextColor (0);
  AsciiPrint ("%a", ">");
}



/**
  Parse a command line and execute the commands. The ; seperator allows 
  multiple commands for each command line. Stop processing if one of the
  commands returns an error.

  @param  CmdLine          Command Line to process.
  @param  MaxCmdLineSize   MaxSize of the Command line 

  @return EFI status of the Command

**/
EFI_STATUS
ProcessCmdLine (
  IN CHAR8      *CmdLine,
  IN UINTN      MaxCmdLineSize
  )
{
  EFI_STATUS          Status;
  EBL_COMMAND_TABLE   *Cmd;
  CHAR8               *Ptr;
  UINTN               Argc;
  CHAR8               *Argv[MAX_ARGS];

  // Parse the command line. The loop processes commands seperated by ;
  for (Ptr = CmdLine, Status = EFI_SUCCESS; Ptr != NULL;) {
    Ptr = ParseArguments (Ptr, &Argc, Argv);
    if (Argc != 0) {
      Cmd = EblGetCommand (Argv[0]);
      if (Cmd != NULL) {
        // Execute the Command!
        Status = Cmd->Command (Argc, Argv);
        if (Status == EFI_ABORTED) {
          // exit command so lets exit
          break;
        } else if (Status == EFI_TIMEOUT) {
          // pause command got imput so don't process any more cmd on this cmd line
          break;
        } else if (EFI_ERROR (Status)) {
          AsciiPrint ("%a returned %r error\n", Cmd->Name, Status);
          // if any command fails stop processing CmdLine
          break;
        }
      } 
    } 
  }

  return Status;
}
 


/**
  Embedded Boot Loader (EBL) - A simple EFI command line application for embedded 
  devices. PcdEmbeddedAutomaticBootCommand is a complied in commnad line that
  gets executed automatically. The ; seperator allows multiple commands 
  for each command line.

  @param  ImageHandle   EFI ImageHandle for this application.
  @param  SystemTable   EFI system table

  @return EFI status of the applicaiton

**/
EFI_STATUS
EFIAPI
EdkBootLoaderEntry (
  IN EFI_HANDLE                            ImageHandle,
  IN EFI_SYSTEM_TABLE                      *SystemTable
  ) 
{
  EFI_STATUS  Status;
  CHAR8       CmdLine[MAX_CMD_LINE];
  CHAR16      *CommandLineVariable = NULL;
  CHAR16      *CommandLineVariableName = L"default-cmdline";
  UINTN       CommandLineVariableSize = 0;
  EFI_GUID    VendorGuid;

  // Initialize tables of commnads
  EblInitializeCmdTable ();
  EblInitializeDeviceCmd ();
  EblInitializemdHwDebugCmds ();
  EblInitializemdHwIoDebugCmds ();
  EblInitializeDirCmd ();
  EblInitializeHobCmd ();
  EblInitializeScriptCmd ();
  EblInitializeExternalCmd ();
  EblInitializeNetworkCmd();
  EblInitializeVariableCmds ();
  
  // Disable the 5 minute EFI watchdog time so we don't get automatically reset
  gBS->SetWatchdogTimer (0, 0, 0, NULL);

  if (FeaturePcdGet (PcdEmbeddedMacBoot)) {
    // A MAC will boot in graphics mode, so turn it back to text here
    // This protocol was removed from edk2. It is only an edk thing. We need to make our own copy.
    // DisableQuietBoot ();

    // Enable the biggest output screen size possible
    gST->ConOut->SetMode (gST->ConOut, (UINTN)gST->ConOut->Mode->MaxMode - 1);

  }

  // Save current screen mode
  gST->ConOut->QueryMode (gST->ConOut, gST->ConOut->Mode->Mode, &gScreenColumns, &gScreenRows);

  EblPrintStartupBanner ();
 
  // Parse command line and handle commands seperated by ;
  // The loop prints the prompt gets user input and saves history
  
  // Look for a variable with a default command line, otherwise use the Pcd
  ZeroMem(&VendorGuid, sizeof(EFI_GUID));

  Status = gRT->GetVariable(CommandLineVariableName, &VendorGuid, NULL, &CommandLineVariableSize, CommandLineVariable);
  if (Status == EFI_BUFFER_TOO_SMALL) {
    CommandLineVariable = AllocatePool(CommandLineVariableSize);
    
    Status = gRT->GetVariable(CommandLineVariableName, &VendorGuid, NULL, &CommandLineVariableSize, CommandLineVariable);
    if (!EFI_ERROR(Status)) {
      UnicodeStrToAsciiStr(CommandLineVariable, CmdLine);
    }
    
    FreePool(CommandLineVariable);
  }
  
  if (EFI_ERROR(Status)) {
    AsciiStrCpy (CmdLine, (CHAR8 *)PcdGetPtr (PcdEmbeddedAutomaticBootCommand));
  }
  
  for (;;) {
    Status = ProcessCmdLine (CmdLine, MAX_CMD_LINE);
    if (Status == EFI_ABORTED) {
      // if a command returns EFI_ABORTED then exit the EBL
      EblShutdownExternalCmdTable ();
      return EFI_SUCCESS;
    }

    // get the command line from the user
    EblPrompt ();
    GetCmd (CmdLine, MAX_CMD_LINE);
    SetCmdHistory (CmdLine);

    if (FeaturePcdGet (PcdEmbeddedProbeRemovable)) {
      // Probe removable media devices to see if media has been inserted or removed.
      EblProbeRemovableMedia ();
    }
  }
}