OpenCoreKernelPatch.c 15.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/** @file
  OpenCore driver.

Copyright (c) 2019, vit9696. All rights reserved.<BR>
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.

**/

15 16
#include <Base.h>

17
#include <Library/OcMainLib.h>
18 19 20 21 22 23 24 25 26 27

#include <Library/BaseLib.h>
#include <Library/DebugLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/OcAfterBootCompatLib.h>
#include <Library/OcAppleKernelLib.h>
#include <Library/OcMiscLib.h>
#include <Library/OcAppleImg4Lib.h>
#include <Library/OcStringLib.h>
#include <Library/OcVirtualFsLib.h>
28
#include <Library/PcdLib.h>
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
#include <Library/PrintLib.h>
#include <Library/UefiBootServicesTableLib.h>

EFI_STATUS
OcKernelApplyQuirk (
  IN     KERNEL_QUIRK_NAME  Quirk,
  IN     KERNEL_CACHE_TYPE  CacheType,
  IN     UINT32             DarwinVersion,
  IN OUT VOID               *Context,
  IN OUT PATCHER_CONTEXT    *KernelPatcher
  )
{
  //
  // Apply kernel quirks to kernel, kext patches to context.
  //
  if (Context == NULL) {
    ASSERT (KernelPatcher != NULL);
    return KernelApplyQuirk (Quirk, KernelPatcher, DarwinVersion);
  }

  if (CacheType == CacheTypeCacheless) {
    return CachelessContextAddQuirk (Context, Quirk);
  }
52

53 54 55 56 57 58 59 60 61 62 63
  if (CacheType == CacheTypeMkext) {
    return MkextContextApplyQuirk (Context, Quirk, DarwinVersion);
  }

  if (CacheType == CacheTypePrelinked) {
    return PrelinkedContextApplyQuirk (Context, Quirk, DarwinVersion);
  }

  return EFI_UNSUPPORTED;
}

64 65 66 67 68 69 70 71 72 73 74
/**
  Retrieve the I/O or MMIO base address register for the PCI UART device.
  This function assumes Root Bus Numer is Zero, and enables I/O and MMIO in PCI UART
  Device if they are not already enabled.
  @return  The base address register of the UART device.
**/
UINTN
GetSerialRegisterBase (
  VOID
  );

75 76
VOID
OcKernelApplyPatches (
77 78 79 80 81 82 83 84
  IN     OC_GLOBAL_CONFIG   *Config,
  IN     OC_CPU_INFO        *CpuInfo,
  IN     UINT32             DarwinVersion,
  IN     BOOLEAN            Is32Bit,
  IN     KERNEL_CACHE_TYPE  CacheType,
  IN     VOID               *Context,
  IN OUT UINT8              *Kernel,
  IN     UINT32             Size
85 86 87 88 89 90 91 92 93
  )
{
  EFI_STATUS             Status;
  PATCHER_CONTEXT        KernelPatcher;
  UINT32                 Index;
  PATCHER_GENERIC_PATCH  Patch;
  OC_KERNEL_PATCH_ENTRY  *UserPatch;
  CONST CHAR8            *Target;
  CONST CHAR8            *Comment;
94
  CONST CHAR8            *Arch;
95 96 97
  UINT32                 MaxKernel;
  UINT32                 MinKernel;
  BOOLEAN                IsKernelPatch;
98 99
  UINTN                  RegisterBase;
  UINT32                 RegisterStride;
100 101 102 103 104 105 106

  IsKernelPatch = Context == NULL;

  if (IsKernelPatch) {
    ASSERT (Kernel != NULL);

    Status = PatcherInitContextFromBuffer (
107 108 109 110 111
               &KernelPatcher,
               Kernel,
               Size,
               Is32Bit
               );
112 113 114 115 116 117 118 119 120 121 122

    if (EFI_ERROR (Status)) {
      DEBUG ((DEBUG_ERROR, "OC: Kernel patcher kernel init failure - %r\n", Status));
      return;
    }
  }

  for (Index = 0; Index < Config->Kernel.Patch.Count; ++Index) {
    UserPatch = Config->Kernel.Patch.Values[Index];
    Target    = OC_BLOB_GET (&UserPatch->Identifier);

123
    if (!UserPatch->Enabled || ((AsciiStrCmp (Target, "kernel") == 0) != IsKernelPatch)) {
124 125 126
      continue;
    }

127 128 129 130
    Comment   = OC_BLOB_GET (&UserPatch->Comment);
    Arch      = OC_BLOB_GET (&UserPatch->Arch);
    MaxKernel = OcParseDarwinVersion (OC_BLOB_GET (&UserPatch->MaxKernel));
    MinKernel = OcParseDarwinVersion (OC_BLOB_GET (&UserPatch->MinKernel));
131

132
    if (AsciiStrCmp (Arch, Is32Bit ? "x86_64" : "i386") == 0) {
133 134
      DEBUG ((
        DEBUG_INFO,
135 136
        "OC: %a patcher skips %a (%a) patch at %u due to arch %a != %a\n",
        PRINT_KERNEL_CACHE_TYPE (CacheType),
137 138 139 140 141 142
        Target,
        Comment,
        Index,
        Arch,
        Is32Bit ? "i386" : "x86_64"
        ));
143
      continue;
144 145
    }

146 147 148
    if (!OcMatchDarwinVersion (DarwinVersion, MinKernel, MaxKernel)) {
      DEBUG ((
        DEBUG_INFO,
149 150
        "OC: %a patcher skips %a (%a) patch at %u due to version %u <= %u <= %u\n",
        PRINT_KERNEL_CACHE_TYPE (CacheType),
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
        Target,
        Comment,
        Index,
        MinKernel,
        DarwinVersion,
        MaxKernel
        ));
      continue;
    }

    //
    // Ignore patch if:
    // - There is nothing to replace.
    // - We have neither symbolic base, nor find data.
    // - Find and replace mismatch in size.
    // - Mask and ReplaceMask mismatch in size when are available.
    //
168 169 170 171 172
    if (  (UserPatch->Replace.Size == 0)
       || ((OC_BLOB_GET (&UserPatch->Base)[0] == '\0') && (UserPatch->Find.Size != UserPatch->Replace.Size))
       || ((UserPatch->Mask.Size > 0) && (UserPatch->Find.Size != UserPatch->Mask.Size))
       || ((UserPatch->ReplaceMask.Size > 0) && (UserPatch->Find.Size != UserPatch->ReplaceMask.Size)))
    {
173 174 175 176 177 178 179
      DEBUG ((DEBUG_ERROR, "OC: Kernel patch %u for %a (%a) is borked\n", Index, Target, Comment));
      continue;
    }

    ZeroMem (&Patch, sizeof (Patch));

    if (OC_BLOB_GET (&UserPatch->Comment)[0] != '\0') {
180
      Patch.Comment = OC_BLOB_GET (&UserPatch->Comment);
181 182 183
    }

    if (OC_BLOB_GET (&UserPatch->Base)[0] != '\0') {
184
      Patch.Base = OC_BLOB_GET (&UserPatch->Base);
185 186 187
    }

    if (UserPatch->Find.Size > 0) {
188
      Patch.Find = OC_BLOB_GET (&UserPatch->Find);
189 190 191 192 193
    }

    Patch.Replace = OC_BLOB_GET (&UserPatch->Replace);

    if (UserPatch->Mask.Size > 0) {
194
      Patch.Mask = OC_BLOB_GET (&UserPatch->Mask);
195 196 197 198 199 200
    }

    if (UserPatch->ReplaceMask.Size > 0) {
      Patch.ReplaceMask = OC_BLOB_GET (&UserPatch->ReplaceMask);
    }

201 202 203 204
    Patch.Size  = UserPatch->Replace.Size;
    Patch.Count = UserPatch->Count;
    Patch.Skip  = UserPatch->Skip;
    Patch.Limit = UserPatch->Limit;
205 206 207 208 209 210 211 212 213 214

    if (IsKernelPatch) {
      Status = PatcherApplyGenericPatch (&KernelPatcher, &Patch);
    } else {
      if (CacheType == CacheTypeCacheless) {
        Status = CachelessContextAddPatch (Context, Target, &Patch);
      } else if (CacheType == CacheTypeMkext) {
        Status = MkextContextApplyPatch (Context, Target, &Patch);
      } else if (CacheType == CacheTypePrelinked) {
        Status = PrelinkedContextApplyPatch (Context, Target, &Patch);
215 216
      } else {
        Status = EFI_UNSUPPORTED;
217 218 219 220 221
      }
    }

    DEBUG ((
      EFI_ERROR (Status) ? DEBUG_WARN : DEBUG_INFO,
222 223
      "OC: %a patcher result %u for %a (%a) - %r\n",
      PRINT_KERNEL_CACHE_TYPE (CacheType),
224 225 226 227 228 229 230
      Index,
      Target,
      Comment,
      Status
      ));
  }

231 232 233
  //
  // Handle Quirks/Emulate here...
  //
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
  if (!IsKernelPatch) {
    if (Config->Kernel.Quirks.AppleCpuPmCfgLock) {
      OcKernelApplyQuirk (KernelQuirkAppleCpuPmCfgLock, CacheType, DarwinVersion, Context, NULL);
    }

    if (Config->Kernel.Quirks.ExternalDiskIcons) {
      OcKernelApplyQuirk (KernelQuirkExternalDiskIcons, CacheType, DarwinVersion, Context, NULL);
    }

    if (Config->Kernel.Quirks.ThirdPartyDrives) {
      OcKernelApplyQuirk (KernelQuirkThirdPartyDrives, CacheType, DarwinVersion, Context, NULL);
    }

    if (Config->Kernel.Quirks.XhciPortLimit) {
      OcKernelApplyQuirk (KernelQuirkXhciPortLimit1, CacheType, DarwinVersion, Context, NULL);
      OcKernelApplyQuirk (KernelQuirkXhciPortLimit2, CacheType, DarwinVersion, Context, NULL);
      OcKernelApplyQuirk (KernelQuirkXhciPortLimit3, CacheType, DarwinVersion, Context, NULL);
    }

    if (Config->Kernel.Quirks.DisableIoMapper) {
      OcKernelApplyQuirk (KernelQuirkDisableIoMapper, CacheType, DarwinVersion, Context, NULL);
    }

    if (Config->Kernel.Quirks.DisableRtcChecksum) {
      OcKernelApplyQuirk (KernelQuirkDisableRtcChecksum, CacheType, DarwinVersion, Context, NULL);
    }

    if (Config->Kernel.Quirks.IncreasePciBarSize) {
262
      OcKernelApplyQuirk (KernelQuirkIncreasePciBarSize, CacheType, DarwinVersion, Context, NULL);
263 264 265 266 267 268 269
    }

    if (Config->Kernel.Quirks.CustomSmbiosGuid) {
      OcKernelApplyQuirk (KernelQuirkCustomSmbiosGuid1, CacheType, DarwinVersion, Context, NULL);
      OcKernelApplyQuirk (KernelQuirkCustomSmbiosGuid2, CacheType, DarwinVersion, Context, NULL);
    }

270 271 272
    if (Config->Kernel.Quirks.ExtendBTFeatureFlags) {
      OcKernelApplyQuirk (KernelQuirkExtendBTFeatureFlags, CacheType, DarwinVersion, Context, NULL);
    }
273 274 275 276

    if (Config->Kernel.Quirks.ForceAquantiaEthernet) {
      OcKernelApplyQuirk (KernelQuirkForceAquantiaEthernet, CacheType, DarwinVersion, Context, NULL);
    }
277

278 279 280 281
    if (Config->Kernel.Quirks.ForceSecureBootScheme) {
      OcKernelApplyQuirk (KernelQuirkForceSecureBootScheme, CacheType, DarwinVersion, Context, NULL);
    }

282
    if (Config->Kernel.Emulate.DummyPowerManagement) {
283 284
      MaxKernel = OcParseDarwinVersion (OC_BLOB_GET (&Config->Kernel.Emulate.MaxKernel));
      MinKernel = OcParseDarwinVersion (OC_BLOB_GET (&Config->Kernel.Emulate.MinKernel));
285 286 287 288 289 290 291 292 293 294 295 296 297 298
      if (OcMatchDarwinVersion (DarwinVersion, MinKernel, MaxKernel)) {
        OcKernelApplyQuirk (KernelQuirkDummyPowerManagement, CacheType, DarwinVersion, Context, NULL);
      } else {
        DEBUG ((
          DEBUG_INFO,
          "OC: %a patcher skips DummyPowerManagement patch due to version %u <= %u <= %u\n",
          PRINT_KERNEL_CACHE_TYPE (CacheType),
          Target,
          MinKernel,
          DarwinVersion,
          MaxKernel
          ));
      }
    }
299

300 301 302
    //
    // Ignore timeout -1.
    //
303
    if (Config->Kernel.Quirks.SetApfsTrimTimeout >= 0) {
304
      PatchSetApfsTimeout ((UINT32)Config->Kernel.Quirks.SetApfsTrimTimeout);
305
      OcKernelApplyQuirk (KernelQuirkSetApfsTrimTimeout, CacheType, DarwinVersion, Context, NULL);
306
    }
307 308 309 310 311 312 313 314 315 316 317 318 319
  } else {
    if (Config->Kernel.Quirks.AppleXcpmCfgLock) {
      OcKernelApplyQuirk (KernelQuirkAppleXcpmCfgLock, CacheType, DarwinVersion, NULL, &KernelPatcher);
    }

    if (Config->Kernel.Quirks.AppleXcpmExtraMsrs) {
      OcKernelApplyQuirk (KernelQuirkAppleXcpmExtraMsrs, CacheType, DarwinVersion, NULL, &KernelPatcher);
    }

    if (Config->Kernel.Quirks.AppleXcpmForceBoost) {
      OcKernelApplyQuirk (KernelQuirkAppleXcpmForceBoost, CacheType, DarwinVersion, NULL, &KernelPatcher);
    }

320 321 322 323
    //
    // Only apply the patch when Misc->Serial->Custom is set (i.e. Override).
    //
    if (Config->Misc.Serial.Override && Config->Kernel.Quirks.CustomPciSerialDevice) {
324
      RegisterBase   = GetSerialRegisterBase ();
325
      RegisterStride = PatchPcdGet32 (PcdSerialRegisterStride);
P
PMheart 已提交
326 327
      if (  (((RegisterBase != 0) && (RegisterStride != 0)))
         && ((RegisterBase != 0x3F8U) || (RegisterStride != 1)))
328
      {
329 330 331 332 333 334 335
        PatchSetPciSerialDevice (RegisterBase, RegisterStride);
        OcKernelApplyQuirk (KernelQuirkCustomPciSerialDevice, CacheType, DarwinVersion, NULL, &KernelPatcher);
      } else {
        DEBUG ((DEBUG_INFO, "OC: Aborting patching PciSerialDevice because RegisterBase is zero/default value!\n"));
      }
    }

336 337 338 339
    if (Config->Kernel.Quirks.PanicNoKextDump) {
      OcKernelApplyQuirk (KernelQuirkPanicNoKextDump, CacheType, DarwinVersion, NULL, &KernelPatcher);
    }

340 341 342 343 344 345 346
    if (  (Config->Kernel.Emulate.Cpuid1Data[0] != 0)
       || (Config->Kernel.Emulate.Cpuid1Data[1] != 0)
       || (Config->Kernel.Emulate.Cpuid1Data[2] != 0)
       || (Config->Kernel.Emulate.Cpuid1Data[3] != 0))
    {
      MaxKernel = OcParseDarwinVersion (OC_BLOB_GET (&Config->Kernel.Emulate.MaxKernel));
      MinKernel = OcParseDarwinVersion (OC_BLOB_GET (&Config->Kernel.Emulate.MinKernel));
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
      if (OcMatchDarwinVersion (DarwinVersion, MinKernel, MaxKernel)) {
        PatchKernelCpuId (
          &KernelPatcher,
          CpuInfo,
          Config->Kernel.Emulate.Cpuid1Data,
          Config->Kernel.Emulate.Cpuid1Mask,
          DarwinVersion
          );
      } else {
        DEBUG ((
          DEBUG_INFO,
          "OC: %a patcher skips CPUID patch due to version %u <= %u <= %u\n",
          PRINT_KERNEL_CACHE_TYPE (CacheType),
          Target,
          MinKernel,
          DarwinVersion,
          MaxKernel
          ));
      }
366 367 368 369 370 371 372 373 374
    }

    if (Config->Kernel.Quirks.LapicKernelPanic) {
      OcKernelApplyQuirk (KernelQuirkLapicKernelPanic, CacheType, DarwinVersion, NULL, &KernelPatcher);
    }

    if (Config->Kernel.Quirks.PowerTimeoutKernelPanic) {
      OcKernelApplyQuirk (KernelQuirkPowerTimeoutKernelPanic, CacheType, DarwinVersion, NULL, &KernelPatcher);
    }
375 376

    if (Config->Kernel.Quirks.DisableLinkeditJettison) {
377
      OcKernelApplyQuirk (KernelQuirkSegmentJettison, CacheType, DarwinVersion, NULL, &KernelPatcher);
378
    }
379 380

    if (Config->Kernel.Quirks.LegacyCommpage) {
381
      OcKernelApplyQuirk (KernelQuirkLegacyCommpage, CacheType, DarwinVersion, NULL, &KernelPatcher);
382
    }
383 384

    if (Config->Kernel.Quirks.ProvideCurrentCpuInfo) {
385
      PatchProvideCurrentCpuInfo (&KernelPatcher, CpuInfo, DarwinVersion);
386
    }
387 388 389 390 391
  }
}

VOID
OcKernelBlockKexts (
392 393 394 395 396
  IN     OC_GLOBAL_CONFIG   *Config,
  IN     UINT32             DarwinVersion,
  IN     BOOLEAN            Is32Bit,
  IN     KERNEL_CACHE_TYPE  CacheType,
  IN     VOID               *Context
397 398 399 400 401 402 403
  )
{
  EFI_STATUS             Status;
  UINT32                 Index;
  OC_KERNEL_BLOCK_ENTRY  *Kext;
  CONST CHAR8            *Target;
  CONST CHAR8            *Comment;
404
  CONST CHAR8            *Arch;
405 406
  CONST CHAR8            *Strategy;
  BOOLEAN                Exclude;
407 408 409 410
  UINT32                 MaxKernel;
  UINT32                 MinKernel;

  for (Index = 0; Index < Config->Kernel.Block.Count; ++Index) {
411
    Kext = Config->Kernel.Block.Values[Index];
412 413 414 415 416

    if (!Kext->Enabled) {
      continue;
    }

417 418 419 420 421 422
    Target    = OC_BLOB_GET (&Kext->Identifier);
    Comment   = OC_BLOB_GET (&Kext->Comment);
    Arch      = OC_BLOB_GET (&Kext->Arch);
    Strategy  = OC_BLOB_GET (&Kext->Strategy);
    MaxKernel = OcParseDarwinVersion (OC_BLOB_GET (&Kext->MaxKernel));
    MinKernel = OcParseDarwinVersion (OC_BLOB_GET (&Kext->MinKernel));
423 424 425 426

    if (AsciiStrCmp (Arch, Is32Bit ? "x86_64" : "i386") == 0) {
      DEBUG ((
        DEBUG_INFO,
427 428
        "OC: %a blocker skips %a (%a) block at %u due to arch %a != %a\n",
        PRINT_KERNEL_CACHE_TYPE (CacheType),
429 430 431 432 433 434 435 436
        Target,
        Comment,
        Index,
        Arch,
        Is32Bit ? "i386" : "x86_64"
        ));
      return;
    }
437 438 439 440

    if (!OcMatchDarwinVersion (DarwinVersion, MinKernel, MaxKernel)) {
      DEBUG ((
        DEBUG_INFO,
441 442
        "OC: %a blocker skips %a (%a) block at %u due to version %u <= %u <= %u\n",
        PRINT_KERNEL_CACHE_TYPE (CacheType),
443 444 445 446 447 448 449 450 451 452
        Target,
        Comment,
        Index,
        MinKernel,
        DarwinVersion,
        MaxKernel
        ));
      continue;
    }

453 454
    Exclude = AsciiStrCmp (Strategy, "Exclude") == 0;
    //
P
PMheart 已提交
455
    // TODO: Implement cacheless and mkext exclusion if possible.
456
    //
457
    if (CacheType == CacheTypeCacheless) {
458
      Status = CachelessContextBlock (Context, Target, Exclude);
459
    } else if (CacheType == CacheTypeMkext) {
460
      Status = MkextContextBlock (Context, Target, Exclude);
461
    } else if (CacheType == CacheTypePrelinked) {
462
      Status = PrelinkedContextBlock (Context, Target, Exclude);
P
PMheart 已提交
463 464
    } else {
      Status = EFI_UNSUPPORTED;
465 466 467 468
    }

    DEBUG ((
      EFI_ERROR (Status) ? DEBUG_WARN : DEBUG_INFO,
P
PMheart 已提交
469
      "OC: %a blocker (%a) result %u for %a (%a) - %r\n",
470
      PRINT_KERNEL_CACHE_TYPE (CacheType),
P
PMheart 已提交
471
      Exclude ? "Exclude" : "Disable",
472
      Index,
473 474 475 476 477 478
      Target,
      Comment,
      Status
      ));
  }
}