gendepex.c

来自「EFI BIOS是Intel提出的下一代的BIOS标准。这里上传的Edk源代码是」· C语言 代码 · 共 913 行 · 第 1/2 页

C
913
字号
/*++

Copyright (c) 2004 - 2006, Intel Corporation                                                         
All rights reserved. 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.             

Module Name:

  GenDepex.c

Abstract:

  Generate Dependency Expression ("GenDepex")

  Infix to Postfix Algorithm

  This code has been scrubbed to be free of having any EFI core tree dependencies.
  It should build in any environment that supports a standard C-library w/ string
  operations and File I/O services.

  As an example of usage, consider the following:

  The input user file could be something like "Sample.DXS" whose contents are

    #include "Tiano.h"

    DEPENDENCY_START
      NOT (DISK_IO_PROTOCOL AND SIMPLE_FILE_SYSTEM_PROTOCOL) 
        OR EFI_PXE_BASE_CODE_PROTOCOL
    DEPENDENCY_END

  This file is then washed through the C-preprocessor, viz.,

    cl /EP Sample.DXS > Sample.TMP1

  This yields the following file "Sample.TMP1" whose contents are

    DEPENDENCY_START
      NOT ({ 0xce345171, 0xba0b, 0x11d2, 0x8e, 0x4f, 0x0, 0xa0, 0xc9, 0x69, 0x72,
        0x3b } AND { 0x964e5b22, 0x6459, 0x11d2, 0x8e, 0x39, 0x0, 0xa0, 0xc9, 0x69,
        0x72, 0x3b }) OR { 0x03c4e603, 0xac28, 0x11d3, 0x9a, 0x2d, 0x00, 0x90, 0x27,
        0x3f, 0xc1, 0x4d }
    DEPENDENCY_END

  This file, in turn, will be fed into the utility, viz.,

    GenDepex Sample.TMP1 Sample.TMP2

  With a file that is 55 bytes long:

     55 bytes for the grammar binary
        PUSH opcode         - 1  byte
        GUID Instance       - 16 bytes
        PUSH opcode         - 1  byte
        GUID Instance       - 16 bytes
        AND opcode          - 1  byte
        NOT opcode          - 1  byte
        PUSH opcode         - 1  byte
        GUID Instance       - 16 bytes
        OR opcode           - 1  byte
        END opcode          - 1  byte

  The file "Sample.TMP2" could be fed via a Section-builder utility 
  (GenSection) that would be used for the creation of a dependency
  section file (.DPX) which in turn would be used by a generate FFS
  utility (GenFfsFile) to produce a DXE driver/core (.DXE) or 
  a DXE application (.APP) file.

  Complies with Tiano C Coding Standards Document, version 0.31, 12 Dec 2000.

--*/

#include "GenDepex.h"

#define TOOL_NAME "GenDepex"

extern
BOOLEAN
ParseDepex (
  IN      INT8      *Pbegin,
  IN      UINT32    length
  );

VOID
PrintGenDepexUtilityInfo (
  VOID
  )
/*++

Routine Description:

  Displays the standard utility information to SDTOUT.

Arguments:

  None

Returns:

  None

--*/
{
  printf (
    "%s, Tiano Dependency Expression Generation Utility. Version %d.%d.\n",
    UTILITY_NAME,
    UTILITY_MAJOR_VERSION,
    UTILITY_MINOR_VERSION
    );
  printf ("Copyright (C) 1996-2002 Intel Corporation.  All rights reserved.\n\n");
}

VOID
PrintGenDepexUsageInfo (
  VOID
  )
/*++

Routine Description:

  Displays the utility usage syntax to STDOUT.

Arguments:

  None

Returns:

  None

--*/
{
  printf (
    "Usage: %s -I <INFILE> -O <OUTFILE> [-P <Optional Boundary for padding up>] \n",
    UTILITY_NAME
    );
  printf (" Where:\n");
  printf ("  <INFILE> is the input pre-processed dependency text files name.\n");
  printf ("  <OUTFILE> is the output binary dependency files name.\n");
  printf ("  <Optional Boundary for padding up> is the padding integer value.\n");
  printf ("    This is the boundary to align the output file size to.\n");
}

DEPENDENCY_OPCODE
PopOpCode (
  IN OUT VOID **Stack
  )
/*++

Routine Description:

  Pop an element from the Opcode stack.

Arguments:

  Stack               Current top of the OpCode stack location

Returns:

  DEPENDENCY_OPCODE   OpCode at the top of the OpCode stack.
  Stack               New top of the OpCode stack location


--*/
{
  DEPENDENCY_OPCODE *OpCodePtr;

  OpCodePtr = *Stack;
  OpCodePtr--;
  *Stack = OpCodePtr;
  return *OpCodePtr;
}

VOID
PushOpCode (
  IN OUT  VOID                **Stack,
  IN      DEPENDENCY_OPCODE   OpCode
  )
/*++

Routine Description:

  Push an element onto the Opcode Stack

Arguments:

  Stack     Current top of the OpCode stack location
  OpCode    OpCode to push onto the stack

Returns:

  Stack     New top of the OpCode stack location

--*/
{
  DEPENDENCY_OPCODE *OpCodePtr;

  OpCodePtr   = *Stack;
  *OpCodePtr  = OpCode;
  OpCodePtr++;
  *Stack = OpCodePtr;
}

EFI_STATUS
GenerateDependencyExpression (
  IN     FILE           *InFile,
  IN OUT FILE           *OutFile,
  IN     UINT8          Padding  OPTIONAL
  )
/*++

Routine Description:

  This takes the pre-compiled dependency text file and 
  converts it into a binary dependency file.

  The BNF for the dependency expression is as follows 
  (from the DXE 1.0 Draft specification).

  The inputted BNF grammar is thus:
    <depex> ::= sor <dep> | 
                before GUID <dep> | 
                after GUID <dep> | 
                <bool>

    <dep> ::=   <bool> |

    <bool> ::=  <bool> and <term> | 
                <bool> or <term> | 
                <term>

    <term> ::=  not <factor> | 
                <factor>

    <factor> ::= ( <bool> ) | 
                 <term> <term> | 
                 GUID | 
                 <boolval>

    <boolval> ::= true | 
                  false

  The outputed binary grammer is thus:
    <depex> ::= sor <dep> | 
                before <depinst> <dep> | 
                after <depinst> <dep> | 
                <bool>

    <dep> ::=   <bool> |

    <bool> ::=  <bool> and <term> | 
                <bool> or <term> | <term>

    <term> ::=  not <factor> | 
                <factor>

    <factor> ::= ( <bool> ) | 
                 <term> <term> | 
                 <boolval> | 
                 <depinst> | 
                 <termval>

    <boolval> ::= true | 
                  false

    <depinst> ::= push GUID

    <termval> ::= end

  BugBug: A correct grammer is parsed correctly. A file that violates the
          grammer may parse when it should generate an error. There is some
          error checking and it covers most of the case when it's an include
          of definition issue. An ill formed expresion may not be detected.

Arguments:

  InFile -  Input pre-compiled text file of the dependency expression.
            This needs to be in ASCII.
            The file pointer can not be NULL.

  OutFile - Binary dependency file.
            The file pointer can not be NULL.

  Padding - OPTIONAL integer value to pad the output file to.


Returns:

  EFI_SUCCESS             The function completed successfully.
  EFI_INVALID_PARAMETER   One of the parameters in the text file was invalid.
  EFI_OUT_OF_RESOURCES    Unable to allocate memory.
  EFI_ABORTED             An misc error occurred.

--*/
{
  INT8              *Ptrx;
  INT8              *Pend;
  INT8              *EvaluationStack;
  INT8              *StackPtr;
  INT8              *Buffer;
  INT8              Line[LINESIZE];
  UINTN             Index;
  UINTN             OutFileSize;
  UINTN             FileSize;
  UINTN             Results;
  BOOLEAN           NotDone;
  BOOLEAN           Before_Flag;
  BOOLEAN           After_Flag;
  BOOLEAN           Dep_Flag;
  BOOLEAN           SOR_Flag;
  EFI_GUID          Guid;
  UINTN             ArgCountParsed;
  DEPENDENCY_OPCODE Opcode;

  Before_Flag = FALSE;
  After_Flag  = FALSE;
  Dep_Flag    = FALSE;
  SOR_Flag    = FALSE;

  memset (Line, 0, LINESIZE);

  OutFileSize     = 0;

  EvaluationStack = (INT8 *) malloc (EVAL_STACK_SIZE);

  if (EvaluationStack != NULL) {
    StackPtr = EvaluationStack;
  } else {
    printf ("Unable to allocate memory to EvaluationStack - Out of resources\n");
    return EFI_OUT_OF_RESOURCES;
  }

  Results = (UINTN) fseek (InFile, 0, SEEK_END);

  if (Results != 0) {
    printf ("FSEEK failed - Aborted\n");
    return EFI_ABORTED;
  }

  FileSize = ftell (InFile);

  if (FileSize == -1L) {
    printf ("FTELL failed - Aborted\n");
    return EFI_ABORTED;
  }

  Buffer = (INT8 *) malloc (FileSize + BUFFER_SIZE);

  if (Buffer == NULL) {
    printf ("Unable to allocate memory to Buffer - Out of resources\n");
    free (EvaluationStack);

    Results = (UINTN) fclose (InFile);
    if (Results != 0) {
      printf ("FCLOSE failed\n");
    }

    Results = (UINTN) fclose (OutFile);
    if (Results != 0) {
      printf ("FCLOSE failed\n");
    }

    return EFI_OUT_OF_RESOURCES;
  }

  Results = (UINTN) fseek (InFile, 0, SEEK_SET);

  if (Results != 0) {
    printf ("FSEEK failed - Aborted\n");
    return EFI_ABORTED;
  }

  fread (Buffer, FileSize, 1, InFile);

  Ptrx    = Buffer;
  Pend    = Ptrx + FileSize - strlen (DEPENDENCY_END);
  Index   = FileSize;

  NotDone = TRUE;
  while ((Index--) && NotDone) {

    if (strncmp (Pend, DEPENDENCY_END, strlen (DEPENDENCY_END)) == 0) {
      NotDone = FALSE;
    } else {
      Pend--;
    }
  }

  if (NotDone) {
    printf ("Couldn't find end string %s\n", DEPENDENCY_END);

    Results = (UINTN) fclose (InFile);
    if (Results != 0) {
      printf ("FCLOSE failed\n");
    }

    Results = (UINTN) fclose (OutFile);
    if (Results != 0) {
      printf ("FCLOSE failed\n");
    }

    free (Buffer);
    free (EvaluationStack);

    return EFI_INVALID_PARAMETER;
  }

  Index   = FileSize;

  NotDone = TRUE;
  while ((Index--) && NotDone) {

    if (strncmp (Ptrx, DEPENDENCY_START, strlen (DEPENDENCY_START)) == 0) {
      Ptrx += strlen (DEPENDENCY_START);
      NotDone = FALSE;
      //
      // BUGBUG -- should Index be decremented by sizeof(DEPENDENCY_START)?
      //
    } else {
      Ptrx++;
    }
  }

  if (NotDone) {
    printf ("Couldn't find start string %s\n", DEPENDENCY_START);

    Results = (UINTN) fclose (InFile);
    if (Results != 0) {
      printf ("FCLOSE failed\n");
    }

    Results = (UINTN) fclose (OutFile);
    if (Results != 0) {
      printf ("FCLOSE failed\n");
    }

    free (Buffer);
    free (EvaluationStack);

    return EFI_INVALID_PARAMETER;
  }
  //
  //  validate the syntax of expression
  //
  if (!ParseDepex (Ptrx, Pend - Ptrx - 1)) {
    printf ("The syntax of expression is wrong\n");

    Results = (UINTN) fclose (InFile);
    if (Results != 0) {
      printf ("FCLOSE failed\n");
    }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?