guidchk.c

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

C
2,349
字号
    "    -f filename    exclude searching of a file",
    "    -e extension   exclude searching of files by extension",
    "    -p             print all GUIDS found",
    "    -g             check for duplicate guids",
    "    -s             check for duplicate signatures",
    "    -x             print guid+defined symbol name",
    "    -b outfile     write internal GUID+basename list to outfile",
    "    -u dirname     exclude searching all subdirectories of a directory",
    "    -h -?          print this help text",
    " ",
    "   Example: GuidChk -g -u build -d fv -f make.inf -e .pkg",
    "",
    NULL
  };
  for (Index = 0; Str[Index] != NULL; Index++) {
    fprintf (stdout, "%s\n", Str[Index]);
  }
}
//
// Process an entire directory by name
//
static
STATUS
ProcessDirectory (
  INT8          *Path,
  INT8          *DirectoryName
  )
{
  FILE_SEARCH_DATA  FSData;
  char              *FileMask;
  BOOLEAN           Done;
  UINT32            Len;
  BOOLEAN           NoSubdirs;
  STRING_LIST       *SLPtr;

  //
  // Root directory may be null
  //
  if (DirectoryName != NULL) {
    //
    // printf ("Processing directory: %s\n", DirectoryName);
    //
  }
  //
  // Initialize our file searching
  //
  FileSearchInit (&FSData);

  //
  // Exclude some directories, files, and extensions
  //
  FileSearchExcludeDirs (&FSData, gOptions.ExcludeDirs);
  FileSearchExcludeExtensions (&FSData, gOptions.ExcludeExtensions);
  FileSearchExcludeFiles (&FSData, gOptions.ExcludeFiles);
  //
  // See if this directory is in the list of directories that they
  // don't want to process subdirectories of
  //
  NoSubdirs = FALSE;
  if (DirectoryName != NULL) {
    for (SLPtr = gOptions.ExcludeSubDirs; SLPtr != NULL; SLPtr = SLPtr->Next) {
      if (_stricmp (SLPtr->Str, DirectoryName) == 0) {
        //
        // printf ("not processing subdirectories of %s\n", DirectoryName);
        //
        NoSubdirs = TRUE;
        break;
      }
    }
  }
  //
  // Create a filemask of files to search for. We'll append "\*.*" on the
  // end, so allocate some extra bytes.
  //
  Len = strlen (Path) + 10;
  if (DirectoryName != NULL) {
    Len += strlen (DirectoryName);
  }

  FileMask = malloc (Len);
  if (FileMask == NULL) {
    Error (NULL, 0, 0, "memory allocation failure", NULL);
    return STATUS_ERROR;
  }
  //
  // Now put it all together
  //
  strcpy (FileMask, Path);
  if ((DirectoryName != NULL) && (strlen (DirectoryName) > 0)) {
    strcat (FileMask, "\\");
    strcat (FileMask, DirectoryName);
  }

  strcat (FileMask, "\\*.*");

  //
  // Start file searching for files and directories
  //
  FileSearchStart (&FSData, FileMask, FILE_SEARCH_FILE | FILE_SEARCH_DIR);

  //
  // Now hack the "\*.*" off the end of the filemask so we can use it to pass
  // the full directory path on recursive calls to process directories.
  //
  FileMask[strlen (FileMask) - 4] = 0;

  //
  // Loop until no more files
  //
  Done = FALSE;
  while (!Done) {
    //
    // printf ("Found %s...", FSData.FileName);
    //
    if (FSData.FileFlags & FILE_SEARCH_DIR) {
      //
      // printf ("directory\n");
      //
      if (!NoSubdirs) {
        ProcessDirectory (FileMask, FSData.FileName);
      }
    } else if (FSData.FileFlags & FILE_SEARCH_FILE) {
      //
      // printf ("file\n");
      //
      ProcessFile (FileMask, FSData.FileName);
    } else {
      //
      // printf ("unknown\n");
      //
    }

    if (FileSearchFindNext (&FSData) != STATUS_SUCCESS) {
      Done = TRUE;
    }
  }
  //
  // Free up allocated memory
  //
  free (FileMask);

  //
  // Free up our file searching
  //
  FileSearchDestroy (&FSData);

  return STATUS_SUCCESS;
}
//
// Process a single file.
//
static
STATUS
ProcessFile (
  INT8                *DirectoryName,
  INT8                *FileName
  )
{
  STATUS  Status;
  UINT32  FileExtension;
  INT8    FullFileName[MAX_PATH];

  Status = STATUS_SUCCESS;

  sprintf (FullFileName, "%s\\%s", DirectoryName, FileName);
  //
  // printf ("Found file: %s\n", FullFileName);
  //
  FileExtension = GetFileExtension (FileName);

  //
  // Process these for GUID checks
  //
  if (gOptions.CheckGuids) {
    switch (FileExtension) {
    case FILE_EXTENSION_C:
    case FILE_EXTENSION_H:
      Status = ProcessCFileGuids (FullFileName);
      break;

    case FILE_EXTENSION_PKG:
      Status = ProcessPkgFileGuids (FullFileName);
      break;

    case FILE_EXTENSION_IA32_INC:
    case FILE_EXTENSION_IA32_ASM:
      Status = ProcessIA32FileGuids (FullFileName);
      break;

    case FILE_EXTENSION_INF:
      Status = ProcessINFFileGuids (FullFileName);
      break;

    case FILE_EXTENSION_IA64_INC:
    case FILE_EXTENSION_IA64_ASM:
      Status = ProcessIA64FileGuids (FullFileName);
      break;

    default:
      //
      // No errors anyway
      //
      Status = STATUS_SUCCESS;
      break;
    }
  }

  if (gOptions.CheckSignatures) {
    switch (FileExtension) {
    case FILE_EXTENSION_C:
    case FILE_EXTENSION_H:
      Status = ProcessCFileSigs (FullFileName);
      break;

    default:
      //
      // No errors anyway
      //
      Status = STATUS_SUCCESS;
      break;
    }
  }

  return Status;
}
//
// Return a code indicating the file name extension.
//
static
UINT32
GetFileExtension (
  INT8        *FileName
  )
{
  INT8  *Extension;
  int   Index;

  //
  // Look back for a filename extension
  //
  for (Extension = FileName + strlen (FileName) - 1; Extension >= FileName; Extension--) {
    if (*Extension == '.') {
      for (Index = 0; FileTypeTable[Index].Extension != NULL; Index++) {
        if (_stricmp (FileTypeTable[Index].Extension, Extension) == 0) {
          return FileTypeTable[Index].ExtensionCode;
        }
      }
    }
  }

  return FILE_TYPE_UNKNOWN;
}
//
// Process a .pkg file.
//
// Look for FFS_FILEGUID=35b898ca-b6a9-49ce-8c72-904735cc49b7
//
static
STATUS
ProcessPkgFileGuids (
  INT8    *FileName
  )
{
  FILE    *Fptr;
  INT8    Line[MAX_LINE_LEN * 2];
  INT8    *Cptr;
  INT8    *Cptr2;
  UINT32  GuidScan[11];
  UINT64  Guid64;

  if ((Fptr = fopen (FileName, "r")) == NULL) {
    Error (NULL, 0, 0, FileName, "could not open input file for reading");
    return STATUS_ERROR;
  }
  //
  // Read lines from the file until done
  //
  while (fgets (Line, sizeof (Line), Fptr) != NULL) {
    Cptr = Line;
    Cptr += SkipWhiteSpace (Line);
    if (strncmp (Cptr, "FFS_FILEGUID", 12) == 0) {
      Cptr += 12;
      Cptr += SkipWhiteSpace (Cptr);
      if (*Cptr == '=') {
        Cptr++;
        Cptr += SkipWhiteSpace (Cptr + 1);
        //
        // Blank out dashes on the line.
        //
        for (Cptr2 = Cptr; *Cptr2; Cptr2++) {
          if (*Cptr2 == '-') {
            *Cptr2 = ' ';
          }
        }

        if (sscanf (
              Cptr,
              "%X %X %X %X %I64X",
              &GuidScan[0],
              &GuidScan[1],
              &GuidScan[2],
              &GuidScan[3],
              &Guid64
              ) == 5) {
          AddPkgGuid (FileName, GuidScan, &Guid64);
        } else {
          DebugMsg (NULL, 0, 0, FileName, "GUID scan failed");
        }
      }
    }
  }

  fclose (Fptr);
  return STATUS_SUCCESS;
}
//
// Process an IA32 assembly file.
//
// Look for:
// FIND_FD_GUID_VAL equ  01h, 01h, 01h, 01h, 01h, 01h, 01h, 01h, 01h, 01h, 01h, 01h, 01h, 01h, 01h, 01h
// PEI_GUID_FileNameGuid_Gmch815  equ  081818181h, 08181h, 08181h, 081h, 081h, 081h, 081h, 081h, 081h, 081h, 081h
//
static
STATUS
ProcessIA32FileGuids (
  INT8    *FileName
  )
{
  FILE    *Fptr;
  INT8    Line[MAX_LINE_LEN];
  INT8    *Cptr;
  INT8    CSave;
  INT8    *CSavePtr;
  UINT32  Len;
  UINT32  GuidData[16];
  UINT32  Index;

  if ((Fptr = fopen (FileName, "r")) == NULL) {
    Error (NULL, 0, 0, FileName, "could not open input file for reading");
    return STATUS_ERROR;
  }
  //
  // Read lines from the file until done
  //
  while (fgets (Line, sizeof (Line), Fptr) != NULL) {
    Cptr = Line;
    Cptr += SkipWhiteSpace (Line);
    //
    // Look for xxxGUIDyyy equ 01h, 02h, 03h, ...
    //
    Len = ValidSymbolName (Cptr);
    if (Len) {
      //
      // Terminate the line after the symbol name, then look for "guid" in
      // the name.
      //
      CSavePtr  = Cptr + Len;
      CSave     = *CSavePtr;
      *CSavePtr = 0;
      while (*Cptr) {
        if (_strnicmp (Cptr, "guid", 4) == 0) {
          break;
        }

        Cptr++;
      }
      //
      // If we found the string "guid", continue
      //
      if (*Cptr) {
        //
        // Restore the character on the line where we null-terminated the symbol
        //
        *CSavePtr = CSave;
        Cptr      = CSavePtr;
        Len       = SkipWhiteSpace (Cptr);
        //
        // Had to be some white space
        //
        if (Len) {
          Cptr += Len;
          //
          // now look for "equ"
          //
          if (_strnicmp (Cptr, "equ", 3) == 0) {
            Cptr += 3;
            Cptr += SkipWhiteSpace (Cptr);
            //
            // Now scan all the data
            //
            for (Index = 0; Index < 16; Index++) {
              if (sscanf (Cptr, "%X", &GuidData[Index]) != 1) {
                break;
              }
              //
              // Skip to next
              //
              while (isxdigit (*Cptr)) {
                Cptr++;
              }

              if ((*Cptr != 'h') && (*Cptr != 'H')) {
                break;
              } else {
                Cptr++;
                while (*Cptr && (isspace (*Cptr) || (*Cptr == ','))) {
                  Cptr++;
                }
              }
            }
            //
            // Now see which form we had
            //
            if (Index == 16) {
              AddGuid16 (FileName, GuidData);
            } else if (Index == 11) {
              AddGuid11 (FileName, GuidData, NULL);
            }
          }
        }
      }
    }
  }

  fclose (Fptr);
  return STATUS_SUCCESS;
}
//
// Found and parsed an IA32 assembly code guid. Save the 16 bytes off in the list
// of guids.
//
static
STATUS
AddGuid16 (
  INT8      *FileName,
  UINT32    *Data
  )
{
  GUID_RECORD *NewRec;
  int         Index;

  //
  // Sanity check the data
  //
  if (!CheckGuidData (Data, 16)) {
    return STATUS_ERROR;
  }
  //
  // Allocate memory for a new guid structure
  //
  NewRec = malloc (sizeof (GUID_RECORD));
  if (NewRec == NULL) {
    Error (NULL, 0, 0, "memory allocation failure", NULL);
    return STATUS_ERROR;
  }

  memset ((char *) NewRec, 0, sizeof (GUID_RECORD));
  NewRec->FileName = malloc (strlen (FileName) + 1);
  if (NewRec->FileName == NULL) {
    free (NewRec);
    Error (NULL, 0, 0, "memory allocation failure", NULL);
    return STATUS_ERROR;
  }

  strcpy (NewRec->FileName, FileName);
  NewRec->Guid.Data1  = (UINT32) (Data[0] | (Data[1] << 8) | (Data[2] << 16) | (Data[3] << 24));
  NewRec->Guid.Data2  = (UINT16) (Data[4] | (Data[5] << 8));
  NewRec->Guid.Data3  = (UINT16) (Data[6] | (Data[7] << 8));
  for (Index = 0; Index < 8; Index++) {
    NewRec->Guid.Data4[Index] = (UINT8) Data[Index + 8];
  }
  //
  // Add it to the list
  //
  NewRec->Next  = gGuidList;
  gGuidList     = NewRec;

  //
  // Report it
  // ReportGuid (FileName, NewRec);
  //
  return STATUS_SUCCESS;
}
//
// Add a GUID defined as GuidLow: 0x1122334455667788
//                       GuidHi:  0x99AABBCCDDEEFF00
//
// These are equivalent:
// { 0x11223344, 0x5566, 0x7788, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x00 }
//    and:
// Low: 00FFEEDDCCBBAA99
// Hi:  7788556611223344
//
static
STATUS
AddGuid64x2 (
  INT8      *FileName,
  UINT32    DataHH, // Upper 32-bits of upper 64 bits of guid
  UINT32    DataHL, // Lower 32-bits of upper 64 bits
  UINT32    DataLH,
  UINT32    DataLL
  )
{
  GUID_RECORD *NewRec;
  int         Index;

  //
  // Allocate memory for a new guid structure
  //
  NewRec = malloc (sizeof (GUID_RECORD));
  if (NewRec == NULL) {
    Error (NULL, 0, 0, "memory allocation failure", NULL);
    return STATUS_ERROR;
  }

  memset ((char *) NewRec, 0, sizeof (GUID_RECORD));
  NewRec->FileName = malloc (strlen (FileName) + 1);
  if (NewRec->FileName == NULL) {
    free (NewRec);
    Error (NULL, 0, 0, "memory allocation failure", NULL);
    return STATUS_ERROR;
  }

  strcpy (NewRec->FileName, FileName);
  NewRec->Guid.Data1  = DataHL;
  NewRec->Guid.Data2  = (UINT16) DataHH;
  NewRec->Guid.Data3  = (UINT16) (DataHH >> 16);
  for (Index = 0; Index < 4; Index++) {
    NewRec->Guid.Data4[Index] = (UINT8) DataLL;
    DataLL >>= 8;
  }

  for (Index = 0; Index < 4; Index++) {
    NewRec->Guid.Data4[Index + 4] = (UINT8) DataLH;
    DataLH >>= 8;
  }
  //
  // Add it to the list
  //
  NewRec->Next  = gGuidList;
  gGuidList     = NewRec;

  //
  // Report it
  // ReportGuid (FileName, NewRec);
  //
  return STATUS_SUCCESS;
}
//
// Process INF files. Look for:
// FILE_GUID            = 240612B6-A063-11d4-9A3A-0090273FC14D
//
static
STATUS
ProcessINFFileGuids (
  INT8    *FileName
  )
{
  FILE    *Fptr;
  INT8    Line[MAX_LINE_LEN * 2];
  INT8    *Cptr;
  INT8    *Cptr2;
  UINT32  GuidScan[11];
  UINT64  Guid64;

  if ((Fptr = fopen (FileName, "r")) == NULL) {
    Error (NULL, 0, 0, FileName, "could not open input file for reading");
    return STATUS_ERROR;
  }
  //
  // Read lines from the file until done
  //
  while (fgets (Line, sizeof (Line), Fptr) != NULL) {
    Cptr = Line;
    Cptr += SkipWhiteSpace (Line);
    if (strncmp (Cptr, "FILE_GUID", 9) == 0) {
      Cptr += 9;
      Cptr += SkipWhiteSpace (Cptr);
      if (*Cptr == '=') {
        Cptr++;
        Cptr += SkipWhiteSpace (Cptr + 1);
        //
        // Blank out dashes on the line.
        //
        for (Cptr2 = Cptr; *Cptr2; Cptr2++) {
          if (*Cptr2 == '-') {
            *Cptr2 = ' ';
          }

⌨️ 快捷键说明

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