Skip to content

Commit

Permalink
OcPciIoLib: Add CpuIo/PciRootBridgeIo/PciIo fix for Aptio IV with Abo…
Browse files Browse the repository at this point in the history
…ve 4G BARs (acidanthera#459)
  • Loading branch information
xCuri0 authored Jun 4, 2023
1 parent 46a78f6 commit d84e9bb
Show file tree
Hide file tree
Showing 13 changed files with 819 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Docs/Configuration.tex
Original file line number Diff line number Diff line change
Expand Up @@ -8632,6 +8632,15 @@ \subsection{ProtocolOverrides Properties}\label{uefiprotoprops}
version. This protocol is typically used by the firmware and other
applications to receive notifications from the macOS bootloader.

\item
\texttt{PciIo}\\
\textbf{Type}: \texttt{plist\ boolean}\\
\textbf{Failsafe}: \texttt{false}\\
\textbf{Description}: Replaces functions in CpuIo and PciRootBridgeIo with
64-bit MMIO compatible ones to fix \texttt{Invalid Parameter} when using 4G Decoding.
This affects UEFI drivers such as \texttt{AudioDxe} which access 64-bit MMIO devices.
Platforms earlier than APTIO V (Haswell and older) are typically affected.

\item
\texttt{UnicodeCollation}\\
\textbf{Type}: \texttt{plist\ boolean}\\
Expand Down
2 changes: 2 additions & 0 deletions Docs/Sample.plist
Original file line number Diff line number Diff line change
Expand Up @@ -1894,6 +1894,8 @@
<false/>
<key>OSInfo</key>
<false/>
<key>PciIo</key>
<false/>
<key>UnicodeCollation</key>
<false/>
</dict>
Expand Down
2 changes: 2 additions & 0 deletions Docs/SampleCustom.plist
Original file line number Diff line number Diff line change
Expand Up @@ -2262,6 +2262,8 @@
<false/>
<key>OSInfo</key>
<false/>
<key>PciIo</key>
<false/>
<key>UnicodeCollation</key>
<false/>
</dict>
Expand Down
1 change: 1 addition & 0 deletions Include/Acidanthera/Library/OcConfigurationLib.h
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,7 @@ OC_DECLARE (OC_UEFI_OUTPUT)
_(BOOLEAN , FirmwareVolume , , FALSE , ()) \
_(BOOLEAN , HashServices , , FALSE , ()) \
_(BOOLEAN , OSInfo , , FALSE , ()) \
_(BOOLEAN , PciIo , , FALSE , ()) \
_(BOOLEAN , UnicodeCollation , , FALSE , ())
OC_DECLARE (OC_UEFI_PROTOCOL_OVERRIDES)

Expand Down
35 changes: 35 additions & 0 deletions Include/Acidanthera/Library/OcPciIoLib.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/** @file
Copyright (C) 2023, xCuri0. All rights reserved.
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.
**/

#ifndef OC_PCI_IO_LIB_H
#define OC_PCI_IO_LIB_H

#include <Protocol/CpuIo2.h>

/**
The user Entry Point for PciIo.
This function fixes PciIo related protocols.
@param[in] Reinstall Replace any installed protocol.
@returns Installed protocol.
@retval NULL There was an error installing the protocol.
**/
EFI_CPU_IO2_PROTOCOL *
OcPciIoInstallProtocol (
IN BOOLEAN Reinstall
);

#endif // OC_PCI_IO_LIB_H
1 change: 1 addition & 0 deletions Library/OcConfigurationLib/OcConfigurationLib.c
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,7 @@ OC_SCHEMA
OC_SCHEMA_BOOLEAN_IN ("FirmwareVolume", OC_GLOBAL_CONFIG, Uefi.ProtocolOverrides.FirmwareVolume),
OC_SCHEMA_BOOLEAN_IN ("HashServices", OC_GLOBAL_CONFIG, Uefi.ProtocolOverrides.HashServices),
OC_SCHEMA_BOOLEAN_IN ("OSInfo", OC_GLOBAL_CONFIG, Uefi.ProtocolOverrides.OSInfo),
OC_SCHEMA_BOOLEAN_IN ("PciIo", OC_GLOBAL_CONFIG, Uefi.ProtocolOverrides.PciIo),
OC_SCHEMA_BOOLEAN_IN ("UnicodeCollation", OC_GLOBAL_CONFIG, Uefi.ProtocolOverrides.UnicodeCollation)
};

Expand Down
1 change: 1 addition & 0 deletions Library/OcMainLib/OcMainLib.inf
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
OcMachoLib
OcMiscLib
OcOSInfoLib
OcPciIoLib
OcSmbiosLib
OcSmcLib
OcStorageLib
Expand Down
5 changes: 5 additions & 0 deletions Library/OcMainLib/OpenCoreUefi.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
#include <Library/OcSmcLib.h>
#include <Library/OcOSInfoLib.h>
#include <Library/OcUnicodeCollationEngGenericLib.h>
#include <Library/OcPciIoLib.h>
#include <Library/OcVariableLib.h>
#include <Library/PrintLib.h>
#include <Library/UefiBootServicesTableLib.h>
Expand Down Expand Up @@ -426,6 +427,10 @@ OcReinstallProtocols (
DEBUG ((DEBUG_INFO, "OC: Failed to install key map protocols\n"));
}

if (OcPciIoInstallProtocol (Config->Uefi.ProtocolOverrides.PciIo) == NULL) {
DEBUG ((DEBUG_INFO, "OC: Failed to install cpuio/pcirootbridgeio overrides\n"));
}

InstallAppleEvent = TRUE;
OverrideAppleEvent = FALSE;

Expand Down
73 changes: 73 additions & 0 deletions Library/OcPciIoLib/OcPciIoLib.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/** @file
PciIo overrides
Copyright (c) 2023, xCuri0. All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
**/

#include "OcPciIoU.h"
#include <Library/OcPciIoLib.h>

EFI_CPU_IO2_PROTOCOL *
OcPciIoInstallProtocol (
IN BOOLEAN Reinstall
)
{
EFI_TPL Tpl;
UINTN Index;
EFI_STATUS Status;
EFI_CPU_IO2_PROTOCOL *CpuIo;
UINTN HandleCount;
EFI_HANDLE *HandleBuffer;
EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *PciRootBridgeIo;

CpuIo = InitializeCpuIo2 ();
if (CpuIo == NULL) {
return NULL;
}

if (!Reinstall) {
return CpuIo;
}

Tpl = gBS->RaiseTPL (TPL_HIGH_LEVEL);
Status = gBS->LocateHandleBuffer (
ByProtocol,
&gEfiPciRootBridgeIoProtocolGuid,
NULL,
&HandleCount,
&HandleBuffer
);

if (EFI_ERROR (Status)) {
gBS->RestoreTPL (Tpl);
return NULL;
}

DEBUG ((DEBUG_INFO, "OCPIO: Fixing CpuIo2\n"));
// Override with 64-bit MMIO compatible functions
CpuIo->Mem.Read = CpuMemoryServiceRead;
CpuIo->Mem.Write = CpuMemoryServiceWrite;

for (Index = 0; Index < HandleCount; ++Index) {
DEBUG ((DEBUG_INFO, "OCPIO: Fixing PciRootBridgeIo %d\n", Index));

Status = gBS->HandleProtocol (
HandleBuffer[Index],
&gEfiPciRootBridgeIoProtocolGuid,
(VOID **)&PciRootBridgeIo
);
if (EFI_ERROR (Status)) {
break;
}

// Override with 64-bit MMIO compatible functions
PciRootBridgeIo->Mem.Read = RootBridgeIoMemRead;
PciRootBridgeIo->Mem.Write = RootBridgeIoMemWrite;
}

gBS->RestoreTPL (Tpl);
FreePool (HandleBuffer);
return CpuIo;
}
49 changes: 49 additions & 0 deletions Library/OcPciIoLib/OcPciIoLib.inf
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
## @file
#
# Component description file for the library producing the English version of Unicode Collation Protocol.
#
# Copyright (c) 2006 - 2011, Intel Corporation. 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.
#
##

[Defines]
INF_VERSION = 0x00010005
BASE_NAME = OcPciIoLib
FILE_GUID = BC045E46-093A-4217-B836-8FF2CD890FCC
MODULE_TYPE = DXE_DRIVER
VERSION_STRING = 1.0
LIBRARY_CLASS = OcPciIoLib|DXE_DRIVER DXE_RUNTIME_DRIVER UEFI_DRIVER UEFI_APPLICATION DXE_SMM_DRIVER DXE_SAL_DRIVER

#
# The following information is for reference only and not required by the build tools.
#
# VALID_ARCHITECTURES = IA32 X64 IPF EBC
#

[Sources]
OcPciIoU.c
OcPciIoLib.c
OcPciIoU.h

[Packages]
MdePkg/MdePkg.dec
OpenCorePkg/OpenCorePkg.dec

[Protocols]
gEfiPciIoProtocolGuid ## SOMETIMES_PRODUCES
gEfiCpuIo2ProtocolGuid ## SOMETIMES_CONSUMES

[LibraryClasses]
BaseMemoryLib
DebugLib
MemoryAllocationLib
OcMiscLib
UefiBootServicesTableLib
Loading

0 comments on commit d84e9bb

Please sign in to comment.