ocvalidate.c 3.4 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 20
#include <OpenCore.h>

21
#include <File.h>
22

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

  ErrorCount = 0;

43 44
  DEBUG ((DEBUG_ERROR, "\nNOTE: This version of ocvalidate is only compatible with OpenCore version %a!\n\n", OPEN_CORE_VERSION));

45 46 47 48 49
  //
  // Pass config structure to all checkers.
  //
  for (Index = 0; Index < ARRAY_SIZE (ConfigCheckers); ++Index) {
    ErrorCount += ConfigCheckers[Index] (Config);
50 51
  }

52 53
  return ErrorCount;
}
54

55
int ENTRY_POINT(int argc, const char *argv[]) {
56 57 58 59
  UINT8              *ConfigFileBuffer;
  UINT32             ConfigFileSize;
  CONST CHAR8        *ConfigFileName;
  INT64              ExecTimeStart;
60
  OC_GLOBAL_CONFIG   Config;
61
  EFI_STATUS         Status;
62 63 64 65 66 67 68 69 70 71
  UINT32             ErrorCount;

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

  //
72
  // Read config file (Only one single config is supported).
73
  //
74 75 76 77 78
  if (argc != 2) {
    DEBUG ((DEBUG_ERROR, "\nUsage: %a <path/to/config.plist>\n\n", argv[0]));
    return -1;
  }
  ConfigFileName   = argv[1];
79 80 81 82 83
  ConfigFileBuffer = readFile (ConfigFileName, &ConfigFileSize);
  if (ConfigFileBuffer == NULL) {
    DEBUG ((DEBUG_ERROR, "Failed to read %a\n", ConfigFileName));
    return -1;
  }
84

85 86 87 88 89 90 91 92 93 94 95
  //
  // 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"));
96 97
    return -1;
  }
98

99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
  ErrorCount = CheckConfig (&Config);
  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"
      ));
  }
117 118

  OcConfigurationFree (&Config);
119
  free (ConfigFileBuffer);
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134

  return 0;
}

INT32 LLVMFuzzerTestOneInput(CONST UINT8 *Data, UINTN Size) {
  VOID *NewData = AllocatePool (Size);
  if (NewData) {
    CopyMem (NewData, Data, Size);
    OC_GLOBAL_CONFIG   Config;
    OcConfigurationInit (&Config, NewData, Size);
    OcConfigurationFree (&Config);
    FreePool (NewData);
  }
  return 0;
}