fwvolume.c

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

C
1,503
字号
/*++

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:

  FWVolume.c

Abstract:

  This module contains functionality to keep track of files destined for
  multiple firmware volues. It saves them up, and when told to, dumps the
  file names out to some files used as input to other utilities that
  actually generate the FVs.

--*/

#include <windows.h>                        // for max_path definition
#include <stdio.h>
#include <string.h>
#include <stdlib.h>                         // for malloc()
#include "Common.h"
#include "DSCFile.h"
#include "FWVolume.h"

#define FV_INF_DIR          "FV_INF_DIR"    // where we create the INF file
#define FV_FILENAME         "FV_FILENAME"   // symbol for the current FV.INF filename
#define EFI_BASE_ADDRESS    "EFI_BASE_ADDRESS"
#define DEFAULT_FV_INF_DIR  "FV"            // where we create the INF file
#define DEFAULT_FV_DIR      "$(BUILD_DIR)"  // where the FV file comes from
#define MALLOC(size)        malloc (size)
#define FREE(ptr)           free (ptr)

//
// Disable warning for unused function arguments
//
#pragma warning(disable : 4100)
//
// Disable warning for while(1) code
//
// #pragma warning (disable : 4127)
//
typedef struct {
  char  *ComponentType;
  char  *Extension;
} COMP_TYPE_EXTENSION;

//
// Use a linked list of these to keep track of all the FV names used
//
typedef struct _FV_LIST {
  struct _FV_LIST *Next;
  char            FVFileName[MAX_PATH];
  char            BaseAddress[MAX_LINE_LEN];
  FILE            *FVFilePtr;
  FILE            *AprioriFilePtr;
  char            *Processor;
  int             ComponentsInstance; // highest [components.n] section with a file for this FV
} FV_LIST;

//
// Use a linked list of these to keep track of all FFS files built. When
// we're done, we turn the info into the FV INF files used to build the
// firmware volumes.
//
typedef struct _FILE_LIST {
  struct _FILE_LIST *Next;
  char              *FileName;
  char              *BaseFileName;
  char              *FVs;               // from FV=x,y,z
  char              *BaseName;          // only needed for duplicate basename check
  char              *Processor;         // only needed for duplicate basename check
  char              Apriori[100];       // of format "FVRecovery:1,FVMain:2" from APRIORI define
  char              *Guid;              // guid string
  int               ComponentsInstance; // which [components.n] section it's in
} FILE_LIST;

typedef struct _LINKED_LIST {
  struct _LINKED_LIST *Next;
  void                *Data;
} LINKED_LIST;

static FILE_LIST                  *mFileList;
static FILE_LIST                  *mLastFile;
static char                       *mXRefFileName  = NULL;
static FV_LIST                    *mNonFfsFVList  = NULL;

//
// Whenever an FV name is referenced, then add it to our list of known
// FV's using these.
//
static FV_LIST                    *mFVList      = NULL;
static FV_LIST                    *mFVListLast  = NULL;

//
// We use this list so that from a given component type, we can determine
// the name of the file on disk. For example, if we're given a file's
// guid and base name, and we know it's a "bs_driver", then we can look
// up "bs_driver" in this array and know that the file (after it's built)
// name is GUID-BASENAME.DXE
//
static const COMP_TYPE_EXTENSION  mCompTypeExtension[] = {
  {
    "bs_driver",
    ".dxe"
  },
  {
    "rt_driver",
    ".dxe"
  },
  {
    "sal_rt_driver",
    ".dxe"
  },
  {
    "security_core",
    ".sec"
  },
  {
    "pei_core",
    ".pei"
  },
  {
    "pic_peim",
    ".pei"
  },
  {
    "pe32_peim",
    ".pei"
  },
  {
    "relocatable_peim",
    ".pei"
  },
  {
    "binary",
    ".ffs"
  },
  {
    "application",
    ".app"
  },
  {
    "file",
    ".ffs"
  },
  {
    "fvimagefile",
    ".fvi"
  },
  {
    "raw_file",
    ".raw"
  },
  {
    "apriori",
    ".ffs"
  },
  {
    "combined_peim_driver",
    ".pei"
  },
  {
    NULL,
    NULL
  }
};

static
void
CFVFreeFileList (
  VOID
  );

static
char                              *
UpperCaseString (
  char *Str
  );

static
void
AddFirmwareVolumes (
  char          *FVs,
  int           ComponentsInstance,
  FILE_LIST     *FileListPtr
  );

static
BOOLEAN
OrderInFvList (
  char    *FvList,
  char    *FvName,
  int     *Order
  );

int
GetBaseAddress (
  char *Name,
  char *BaseAddress
  )
{
  char  *Start;
  char  *Cptr;
  char  CSave;
  char  *Value;

  Start = Name;
  while (*Name && isspace (*Name)) {
    Name++;
  }

  if (!*Name) {
    return STATUS_ERROR;
  }
  //
  // Find the end of the name. Either space or a '='.
  //
  for (Value = Name; *Value && !isspace (*Value) && (*Value != '='); Value++)
    ;
  if (!*Value) {
    return STATUS_ERROR;
  }
  //
  // Look for the '='
  //
  Cptr = Value;
  while (*Value && (*Value != '=')) {
    Value++;
  }

  if (!*Value) {
    return STATUS_ERROR;
  }
  //
  // Now truncate the name
  //
  CSave = *Cptr;
  *Cptr = 0;
  if (_stricmp (Name, EFI_BASE_ADDRESS) != 0) {
    return STATUS_ERROR;
  }

  *Cptr = CSave;
  //
  // Skip over the = and then any spaces
  //
  Value++;
  while (*Value && isspace (*Value)) {
    Value++;
  }
  //
  // Find end of string, checking for quoted string
  //
  if (*Value == '\"') {
    Value++;
    for (Cptr = Value; *Cptr && *Cptr != '\"'; Cptr++)
      ;
  } else {
    for (Cptr = Value; *Cptr && !isspace (*Cptr); Cptr++)
      ;
  }
  //
  // Null terminate the value string
  //
  CSave = *Cptr;
  *Cptr = 0;
  strcpy (BaseAddress, Value);
  *Cptr = CSave;

  return STATUS_SUCCESS;
}

int
CFVAddFVFile (
  char  *Name,
  char  *ComponentType,
  char  *FVs,
  int   ComponentsInstance,
  char  *FFSExt,
  char  *Processor,
  char  *Apriori,
  char  *BaseName,
  char  *Guid
  )
/*++

Routine Description:

  Add a file to the list of files in one or more firmware volumes.

Arguments:

  Name          - $(FILE_GUID)-$(BASE_NAME), or filename
  ComponentType - type of component being added. Required so we know the
                  resultant file name after it has been built
  FVs           - string of commma-separated FVs that the given file is
                  to be added to. For example, FVs="FV0001,FV0002"
  FFSExt        - FFS filename extension of the file after it has been built.
                  This is passed in to us in case we don't know the default
                  filename extension based on the component type.
  Processor     - the target processor which the FV is being built for
  Apriori       - pointer to the definition of APRIORI. For example APRIORI="FvRecovery:1,FvMain:4"

Returns:

  STATUS_SUCCESS if successful

--*/
{
  FILE_LIST *Ptr;
  char      FileName[MAX_PATH];
  char      Str[MAX_PATH];
  int       i;
  char      *Sym;

  //
  // If the file's not destined for an FV then return
  //
  if ((FVs == NULL) || (FVs[0] == 0)) {
    return STATUS_SUCCESS;
  }
  //
  // If they provided a filename extension for this type of file, then use it.
  // If they did not provide a filename extension, search our list for a
  // matching component type and use the extension appropriate for this
  // component type.
  //
  if (FFSExt == NULL) {
    //
    // They didn't give us a filename extension. Figure it out from the
    // component type.
    //
    for (i = 0; mCompTypeExtension[i].ComponentType != NULL; i++) {
      if (_stricmp (ComponentType, mCompTypeExtension[i].ComponentType) == 0) {
        FFSExt = mCompTypeExtension[i].Extension;
        break;
      }
    }
    //
    // If we don't know the file extension, then error out. Just means
    // the need to define "FFS_EXT = raw" in the component INF file.
    //
    if (mCompTypeExtension[i].ComponentType == NULL) {
      Error (
        NULL,
        0,
        0,
        ComponentType,
        "unknown component type - must define FFS_EXT for built filename extension in component INF file"
        );
      return STATUS_ERROR;
    }
  }
  //
  // We now have all the parts to the FFS filename. Prepend the path to it if
  // it's not a full pathname.
  // See if they overrode the default base directory for the FV files.
  //
  if (Name[1] != ':') {
    Sym = GetSymbolValue (FV_DIR);
    if (Sym == NULL) {
      Sym = DEFAULT_FV_DIR;
    }
    //
    // Create the file path. Something like $(BUILD_DIR)\$(PROCESSOR)\$(GUID)-$(BASE_NAME).ext
    // If the extension is non-zero length, then make sure there's a dot in it.
    //
    if ((strlen (FFSExt) > 0) && (FFSExt[0] != '.')) {
      sprintf (Str, "%s\\%s\\%s.%s", Sym, Processor, Name, FFSExt);
    } else {
      sprintf (Str, "%s\\%s\\%s%s", Sym, Processor, Name, FFSExt);
    }

    ExpandMacros (Str, FileName, sizeof (FileName), 0);
  } else {
    strcpy (FileName, Name);
  }
  //
  // Traverse the list of files we have so far and make sure we don't have
  // any duplicate basenames. If the base name and processor match, then we'll
  // have build issues, so don't allow it.
  //
  Ptr = mFileList;
  while (Ptr != NULL) {
    if ((Ptr->BaseName != NULL) && (BaseName != NULL) && (_stricmp (BaseName, Ptr->BaseName) == 0)) {
      if ((Ptr->Processor != NULL) && (Processor != NULL) && (_stricmp (Processor, Ptr->Processor) == 0)) {
        Error (NULL, 0, 0, BaseName, "duplicate base name specified");
        return STATUS_ERROR;
      }
    }

    Ptr = Ptr->Next;
  }
  //
  // Allocate a new structure so we can add this file to the list of
  // files.
  //
  Ptr = (FILE_LIST *) malloc (sizeof (FILE_LIST));
  if (Ptr == NULL) {
    Error (NULL, 0, 0, NULL, "failed to allocate memory");
    return STATUS_ERROR;
  }

  memset ((char *) Ptr, 0, sizeof (FILE_LIST));
  Ptr->FileName = (char *) malloc (strlen (FileName) + 1);
  if (Ptr->FileName == NULL) {
    Error (NULL, 0, 0, NULL, "failed to allocate memory");
    return STATUS_ERROR;
  }

  strcpy (Ptr->FileName, FileName);
  Ptr->ComponentsInstance = ComponentsInstance;
  //
  // Allocate memory to save the FV list if it's going into an FV.
  //
  if ((FVs != NULL) && (FVs[0] != 0)) {
    Ptr->FVs = (char *) malloc (strlen (FVs) + 1);
    if (Ptr->FVs == NULL) {
      Error (NULL, 0, 0, NULL, "failed to allocate memory");
      return STATUS_ERROR;
    }

    strcpy (Ptr->FVs, FVs);
  }

  Ptr->BaseFileName = (char *) malloc (strlen (Name) + 1);
  if (Ptr->BaseFileName == NULL) {
    Error (NULL, 0, 0, NULL, "failed to allocate memory");
    return STATUS_ERROR;
  }

  strcpy (Ptr->BaseFileName, Name);
  //
  // Allocate memory for the basename if they gave us one. May not have one
  // if the user is simply adding pre-existing binary files to the image.
  //
  if (BaseName != NULL) {
    Ptr->BaseName = (char *) malloc (strlen (BaseName) + 1);
    if (Ptr->BaseName == NULL) {
      Error (NULL, 0, 0, NULL, "failed to allocate memory");
      return STATUS_ERROR;
    }

    strcpy (Ptr->BaseName, BaseName);
  }
  //
  // Allocate memory for the processor name
  //
  if (Processor != NULL) {
    Ptr->Processor = (char *) malloc (strlen (Processor) + 1);
    if (Ptr->Processor == NULL) {
      Error (NULL, 0, 0, NULL, "failed to allocate memory");
      return STATUS_ERROR;
    }

    strcpy (Ptr->Processor, Processor);
  }
  //
  // Allocate memory for the guid name
  //
  if (Guid != NULL) {
    Ptr->Guid = (char *) malloc (strlen (Guid) + 1);
    if (Ptr->Guid == NULL) {
      Error (NULL, 0, 0, NULL, "failed to allocate memory");
      return STATUS_ERROR;
    }

    strcpy (Ptr->Guid, Guid);
  }
  //
  // If non-null apriori symbol, then save the apriori list for this file
  //
  if (Apriori != NULL) {
    strcpy (Ptr->Apriori, Apriori);
  }

  if (mFileList == NULL) {
    mFileList = Ptr;
  } else {
    mLastFile->Next = Ptr;
  }

  mLastFile = Ptr;
  //
  // Add these firmware volumes to the list of known firmware
  // volume names.
  //
  AddFirmwareVolumes (FVs, ComponentsInstance, Ptr);

  return STATUS_SUCCESS;
}

void

⌨️ 快捷键说明

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