ocvalidate.c 4.0 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
  UINT32             ErrorCount;

72 73
  ErrorCount = 0;

74 75 76 77 78 79 80 81
  //
  // Enable PCD debug logging.
  //
  PcdGet8  (PcdDebugPropertyMask)         |= DEBUG_PROPERTY_DEBUG_CODE_ENABLED;
  PcdGet32 (PcdFixedDebugPrintErrorLevel) |= DEBUG_INFO;
  PcdGet32 (PcdDebugPrintErrorLevel)      |= DEBUG_INFO;

  //
82
  // Print usage.
83
  //
84 85 86
  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]));
87 88
    return -1;
  }
89 90 91 92

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

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

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

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

  OcConfigurationFree (&Config);
  FreePool (ConfigFileBuffer);

123 124 125
  if (ErrorCount == 0) {
    DEBUG ((
      DEBUG_ERROR,
126
      "Completed validating %a in %llu ms. No issues found.\n",
127 128 129 130 131 132
      ConfigFileName,
      GetCurrentTimestamp () - ExecTimeStart
      ));
  } else {
    DEBUG ((
      DEBUG_ERROR,
133
      "Completed validating %a in %llu ms. Found %u %a requiring attention.\n",
134 135 136
      ConfigFileName,
      GetCurrentTimestamp () - ExecTimeStart,
      ErrorCount,
137
      ErrorCount > 1 ? "issues" : "issue"
138
      ));
139

140 141
    return EXIT_FAILURE;
  }
142

143
  return 0;
144 145 146
}

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

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

158 159
  return 0;
}