ocvalidate.c 3.9 KB
Newer Older
1 2
/** @file
  Copyright (C) 2018, vit9696. All rights reserved.
3
  Copyright (C) 2020, PMheart. All rights reserved.
4 5 6 7 8 9 10 11 12 13 14 15

  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.
**/

16 17
#include "ocvalidate.h"
#include "OcValidateLib.h"
18

19
#include <Library/OcMainLib.h>
20

P
PMheart 已提交
21
#include <UserFile.h>
22

23 24 25 26 27
UINT32
CheckConfig (
  IN  OC_GLOBAL_CONFIG  *Config
  )
{
28 29 30 31
  UINT32               ErrorCount;
  UINT32               CurrErrorCount;
  UINTN                Index;
  STATIC CONFIG_CHECK  ConfigCheckers[] = {
32 33 34 35 36
    &CheckACPI,
    &CheckBooter,
    &CheckDeviceProperties,
    &CheckKernel,
    &CheckMisc,
37
    &CheckNvram,
38 39 40 41
    &CheckPlatformInfo,
    &CheckUEFI
  };

42 43
  ErrorCount     = 0;
  CurrErrorCount = 0;
44 45 46 47 48

  //
  // Pass config structure to all checkers.
  //
  for (Index = 0; Index < ARRAY_SIZE (ConfigCheckers); ++Index) {
49 50 51 52 53 54 55 56 57
    CurrErrorCount = ConfigCheckers[Index] (Config);

    if (CurrErrorCount != 0) {
      //
      // Print an extra newline on error.
      //
      DEBUG ((DEBUG_WARN, "\n"));
      ErrorCount += CurrErrorCount;
    }
58 59
  }

60 61
  return ErrorCount;
}
62

63
int ENTRY_POINT(int argc, const char *argv[]) {
64 65 66 67
  UINT8              *ConfigFileBuffer;
  UINT32             ConfigFileSize;
  CONST CHAR8        *ConfigFileName;
  INT64              ExecTimeStart;
68
  OC_GLOBAL_CONFIG   Config;
69
  EFI_STATUS         Status;
70 71 72 73 74 75 76 77 78 79
  UINT32             ErrorCount;

  //
  // Enable PCD debug logging.
  //
  PcdGet8  (PcdDebugPropertyMask)         |= DEBUG_PROPERTY_DEBUG_CODE_ENABLED;
  PcdGet32 (PcdFixedDebugPrintErrorLevel) |= DEBUG_INFO;
  PcdGet32 (PcdDebugPrintErrorLevel)      |= DEBUG_INFO;

  //
80
  // Print usage.
81
  //
82 83 84
  if (argc != 2 || (argc > 1 && AsciiStrCmp (argv[1], "--version") == 0)) {
    DEBUG ((DEBUG_ERROR, "\nNOTE: This version of ocvalidate is only compatible with OpenCore version %a!\n\n", OPEN_CORE_VERSION));
    DEBUG ((DEBUG_ERROR, "Usage: %a <path/to/config.plist>\n\n", argv[0]));
85 86
    return -1;
  }
87 88 89 90

  //
  // Read config file (Only one single config is supported).
  //
91
  ConfigFileName   = argv[1];
P
PMheart 已提交
92
  ConfigFileBuffer = UserReadFile (ConfigFileName, &ConfigFileSize);
93 94 95 96
  if (ConfigFileBuffer == NULL) {
    DEBUG ((DEBUG_ERROR, "Failed to read %a\n", ConfigFileName));
    return -1;
  }
97

98 99 100 101 102 103 104 105 106 107 108
  //
  // Record the current time when action starts.
  //
  ExecTimeStart = GetCurrentTimestamp ();

  //
  // Initialise config structure to be checked, and exit on error.
  //
  Status = OcConfigurationInit (&Config, ConfigFileBuffer, ConfigFileSize);
  if (EFI_ERROR (Status)) {
    DEBUG ((DEBUG_ERROR, "Invalid config\n"));
109 110
    return -1;
  }
111

112 113 114 115
  //
  // Print a newline that splits errors between OcConfigurationInit and config checkers.
  //
  DEBUG ((DEBUG_ERROR, "\n"));
116
  ErrorCount = CheckConfig (&Config);
117 118 119 120

  OcConfigurationFree (&Config);
  FreePool (ConfigFileBuffer);

121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
  if (ErrorCount == 0) {
    DEBUG ((
      DEBUG_ERROR,
      "Done checking %a in %llu ms\n",
      ConfigFileName,
      GetCurrentTimestamp () - ExecTimeStart
      ));
  } else {
    DEBUG ((
      DEBUG_ERROR,
      "Done checking %a in %llu ms, but it has %u %a to be fixed\n",
      ConfigFileName,
      GetCurrentTimestamp () - ExecTimeStart,
      ErrorCount,
      ErrorCount > 1 ? "errors" : "error"
      ));
137

138 139
    return EXIT_FAILURE;
  }
140

141
  return 0;
142 143 144
}

INT32 LLVMFuzzerTestOneInput(CONST UINT8 *Data, UINTN Size) {
145 146 147 148 149
  VOID              *NewData;
  OC_GLOBAL_CONFIG  Config;

  NewData = AllocatePool (Size);
  if (NewData != NULL) {
150 151 152 153 154
    CopyMem (NewData, Data, Size);
    OcConfigurationInit (&Config, NewData, Size);
    OcConfigurationFree (&Config);
    FreePool (NewData);
  }
155
  
156 157
  return 0;
}