Home | History | Annotate | Download | only in EblExternCmd
      1 /** @file
      2   Glue code that contains the EFI entry point and converts it to an EBL
      3   ASCII Argc, Argv sytle entry point
      4 
      5 
      6   Copyright (c) 2007, Intel Corporation. All rights reserved.<BR>
      7   Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
      8 
      9   This program and the accompanying materials
     10   are licensed and made available under the terms and conditions of the BSD License
     11   which accompanies this distribution.  The full text of the license may be found at
     12   http://opensource.org/licenses/bsd-license.php
     13 
     14   THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
     15   WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
     16 
     17 
     18 **/
     19 
     20 #include "Ebl.h"
     21 
     22 #define CMD_SEPARATOR     ';'
     23 #define MAX_ARGS          32
     24 
     25 EFI_STATUS
     26 EblMain (
     27   IN UINTN  Argc,
     28   IN CHAR8  **Argv
     29   );
     30 
     31 
     32 ///
     33 /// EdkExternCmdEntry() & ParseArguments() convert the standard EFI entry point
     34 /// into Argc, Argv form that calls EblMain().
     35 ///
     36 
     37 
     38 /**
     39   Parse the CmdLine and break it up into Argc (arg count) and Argv (array of
     40   pointers to each argument). The Cmd buffer is altered and separators are
     41   converted to string terminators. This allows Argv to point into CmdLine.
     42   A CmdLine can support multiple commands. The next command in the command line
     43   is returned if it exists.
     44 
     45   @param  CmdLine     String to parse for a set of commands
     46   @param  CmdLineSize Size of CmdLine in bytes
     47   @param  Argc        Returns the number of arguments in the CmdLine current command
     48   @param  Argv        Argc pointers to each string in CmdLine
     49 
     50   @return Next Command in the command line or NULL if non exists
     51 **/
     52 VOID
     53 ParseArguments (
     54   IN  CHAR8  *CmdLine,
     55   IN  UINTN  CmdLineSize,
     56   OUT UINTN  *Argc,
     57   OUT CHAR8  **Argv
     58   )
     59 {
     60   UINTN   Arg;
     61   CHAR8   *Char;
     62   BOOLEAN LookingForArg;
     63   BOOLEAN InQuote;
     64   UINTN   Index;
     65 
     66   *Argc = 0;
     67   if ((CmdLineSize == 0) || (AsciiStrLen (CmdLine) == 0)) {
     68     // basic error checking failed on the arguments
     69     return;
     70   }
     71 
     72   // Walk a single command line. A CMD_SEPARATOR allows multiple commands on a single line
     73   InQuote       = FALSE;
     74   LookingForArg = TRUE;
     75   for (Char = CmdLine, Arg = 0, Index = 0; *Char != '\0' && *Char != CMD_SEPARATOR; Char++, Index++) {
     76     // Perform any text conversion here
     77     if (*Char == '\t') {
     78       // TAB to space
     79       *Char = ' ';
     80     }
     81 
     82     if (LookingForArg) {
     83       // Look for the beginning of an Argv[] entry
     84       if (*Char == '"') {
     85         Argv[Arg++] = ++Char;
     86         LookingForArg = FALSE;
     87         InQuote = TRUE;
     88       } else if (*Char != ' ') {
     89         Argv[Arg++] = Char;
     90         LookingForArg = FALSE;
     91       }
     92     } else {
     93       // Looking for the terminator of an Argv[] entry
     94       if ((InQuote && (*Char == '"')) || (!InQuote && (*Char == ' '))) {
     95         *Char = '\0';
     96         LookingForArg = TRUE;
     97       }
     98     }
     99 
    100     if ((Arg >= MAX_ARGS) || (Index > CmdLineSize)) {
    101       // Error check buffer and exit since it does not look valid
    102       break;
    103     }
    104   }
    105 
    106   *Argc = Arg;
    107 
    108   if (*Char == CMD_SEPARATOR) {
    109     // Replace the command delimiter with null
    110     *Char = '\0';
    111   }
    112 
    113   return;
    114 }
    115 
    116 
    117 
    118 
    119 /**
    120   Embedded Boot Loader (EBL) - A simple EFI command line application for embedded
    121   devices. PcdEmbeddedAutomaticBootCommand is a complied in command line that
    122   gets executed automatically. The ; separator allows multiple commands
    123   for each command line.
    124 
    125   @param  ImageHandle   EFI ImageHandle for this application.
    126   @param  SystemTable   EFI system table
    127 
    128   @return EFI status of the application
    129 
    130 **/
    131 EFI_STATUS
    132 EFIAPI
    133 EdkExternCmdEntry (
    134   IN EFI_HANDLE                            ImageHandle,
    135   IN EFI_SYSTEM_TABLE                      *SystemTable
    136   )
    137 {
    138   EFI_STATUS                  Status;
    139   EFI_LOADED_IMAGE_PROTOCOL   *ImageInfo;
    140   UINTN                       Argc;
    141   CHAR8                       *Argv[MAX_ARGS];
    142 
    143   Status = gBS->HandleProtocol (ImageHandle, &gEfiLoadedImageProtocolGuid, (VOID **)&ImageInfo);
    144   if (EFI_ERROR (Status)) {
    145     Argc = 0;
    146   } else {
    147     // Looks like valid commands were passed in.
    148     ParseArguments (ImageInfo->LoadOptions, ImageInfo->LoadOptionsSize, &Argc, Argv);
    149   }
    150 
    151   return EblMain (Argc, Argv);
    152 }
    153 
    154 
    155