Skip to content

Commit

Permalink
- Fix a name conflict for config variable
Browse files Browse the repository at this point in the history
- Improve pci patch (allow to write to PCI config command register, but bit `memory space` must be always set )
  • Loading branch information
lvs1974 committed Jun 6, 2018
1 parent 0e19192 commit 3d567c0
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 36 deletions.
4 changes: 4 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,7 @@ HibernationFixup Changelog
#### v1.2.1
- Save hibernation keys in NVRAM only if boot-arg `-hbfx-dump-nvram` is specified or if the second bank of RTC memory (next block of 128 bytes) is not available
- PCI Family patch is always enabled, boot-arg `-hbfx-patch-pci` is obsolete. A new boot arg `-hbfx-disable-patch-pci` is introduced to disable any patching

#### v1.2.2
- Fix a name conflict for config variable
- Improve pci patch (allow to write to PCI config command register, but bit `memory space` must be always set )
4 changes: 2 additions & 2 deletions HibernationFixup.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@
MODULE_NAME = as.lvs1974.HibernationFixup;
MODULE_START = "$(PRODUCT_NAME)_kern_start";
MODULE_STOP = "$(PRODUCT_NAME)_kern_stop";
MODULE_VERSION = 1.2.1;
MODULE_VERSION = 1.2.2;
OTHER_CFLAGS = (
"-mmmx",
"-msse",
Expand Down Expand Up @@ -408,7 +408,7 @@
MODULE_NAME = as.lvs1974.HibernationFixup;
MODULE_START = "$(PRODUCT_NAME)_kern_start";
MODULE_STOP = "$(PRODUCT_NAME)_kern_stop";
MODULE_VERSION = 1.2.1;
MODULE_VERSION = 1.2.2;
OTHER_CFLAGS = (
"-mmmx",
"-msse",
Expand Down
Binary file not shown.
3 changes: 2 additions & 1 deletion HibernationFixup/kern_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#ifndef kern_config_private_h
#define kern_config_private_h

#include <Headers/kern_util.hpp>

class Configuration {
public:
Expand Down Expand Up @@ -45,6 +46,6 @@ class Configuration {
Configuration() = default;
};

extern Configuration config;
extern Configuration ADDPR(hbfx_config);

#endif /* kern_config_private_h */
50 changes: 32 additions & 18 deletions HibernationFixup/kern_hbfx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,22 @@ enum
kMachineRestoreTunnels = 0x00000008,
};

/* Command register definitions */
enum {
kIOPCICommandIOSpace = 0x0001,
kIOPCICommandMemorySpace = 0x0002,
kIOPCICommandBusMaster = 0x0004,
kIOPCICommandSpecialCycles = 0x0008,
kIOPCICommandMemWrInvalidate = 0x0010,
kIOPCICommandPaletteSnoop = 0x0020,
kIOPCICommandParityError = 0x0040,
kIOPCICommandAddressStepping = 0x0080,
kIOPCICommandSERR = 0x0100,
kIOPCICommandFastBack2Back = 0x0200,
kIOPCICommandInterruptDisable = 0x0400
};


static const char *kextIOPCIFamilyPath[] { "/System/Library/Extensions/IOPCIFamily.kext/IOPCIFamily" };

static KernelPatcher::KextInfo kextList[] {
Expand Down Expand Up @@ -120,7 +136,7 @@ IOReturn HBFX::IOHibernateSystemSleep(void)
SYSLOG("HBFX", "IOHibernateSMCVariablesKey can't be written to NVRAM.");
}

if (config.dumpNvram)
if (ADDPR(hbfx_config).dumpNvram)
{
uint32_t size;
if (uint8_t *buf = callbackHBFX->nvstorage.read(kGlobalBoot0082Key, size, NVStorage::OptRaw))
Expand Down Expand Up @@ -218,12 +234,12 @@ IOReturn HBFX::restoreMachineState(IOService *that, IOOptionBits options, IOServ
if (callbackHBFX && callbackHBFX->orgRestoreMachineState)
{
if (kMachineRestoreDehibernate & options)
callbackHBFX->disable_pci_config_command = true;
callbackHBFX->correct_pci_config_command = true;

result = callbackHBFX->orgRestoreMachineState(that, options, device);

if (kMachineRestoreDehibernate & options)
callbackHBFX->disable_pci_config_command = false;
callbackHBFX->correct_pci_config_command = false;
}

return result;
Expand All @@ -235,18 +251,18 @@ void HBFX::extendedConfigWrite16(IOService *that, UInt64 offset, UInt16 data)
{
if (callbackHBFX && callbackHBFX->orgExtendedConfigWrite16)
{
if (callbackHBFX->disable_pci_config_command && offset == WIOKit::PCIRegister::kIOPCIConfigCommand)
if (callbackHBFX->correct_pci_config_command && offset == WIOKit::PCIRegister::kIOPCIConfigCommand)
{
if (strlen(config.ignored_device_list) == 0 || strstr(config.ignored_device_list, that->getName()) != nullptr)
if (strlen(ADDPR(hbfx_config).ignored_device_list) == 0 || strstr(ADDPR(hbfx_config).ignored_device_list, that->getName()) != nullptr)
{
#ifdef DEBUG
UInt16 status = callbackHBFX->orgExtendedConfigRead16 ? callbackHBFX->orgExtendedConfigRead16(that, WIOKit::PCIRegister::kIOPCIConfigStatus) : 0;
DBGLOG("HBFX", "extendedConfigWrite16 won't be called for device %s, offset = %08llX, data = %04X, status reg = %04X", that->getName(), offset, data, status);
#endif
return;
if (!(data & kIOPCICommandMemorySpace))
{
DBGLOG("HBFX", "HBFX will add flag kIOPCICommandMemorySpace for deivce %s, offset = %08llX, data = %04X", that->getName(), offset, data);
data |= kIOPCICommandMemorySpace;
}
}
}
callbackHBFX->orgExtendedConfigWrite16(that, offset, data);
}
}
Expand All @@ -255,7 +271,7 @@ void HBFX::extendedConfigWrite16(IOService *that, UInt64 offset, UInt16 data)

void HBFX::processKernel(KernelPatcher &patcher)
{
if (config.dumpNvram == false && checkRTCExtendedMemory())
if (ADDPR(hbfx_config).dumpNvram == false && checkRTCExtendedMemory())
{
SYSLOG("HBFX", "all kernel patches will be skipped since the second bank of RTC memory is available");
return;
Expand Down Expand Up @@ -303,7 +319,7 @@ void HBFX::processKernel(KernelPatcher &patcher)
SYSLOG("HBFX", "failed to resolve _ml_set_interrupts_enabled");
}

if (config.dumpNvram)
if (ADDPR(hbfx_config).dumpNvram)
{
method_address = patcher.solveSymbol(KernelPatcher::KernelID, "_sync");
if (method_address) {
Expand Down Expand Up @@ -372,7 +388,7 @@ void HBFX::processKext(KernelPatcher &patcher, size_t index, mach_vm_address_t a

if (progressState != ProcessingState::EverythingDone)
{
if (config.patchPCIFamily)
if (ADDPR(hbfx_config).patchPCIFamily)
{
for (size_t i = 0; i < kextListSize; i++)
{
Expand Down Expand Up @@ -407,8 +423,6 @@ void HBFX::processKext(KernelPatcher &patcher, size_t index, mach_vm_address_t a
} else {
SYSLOG("HBFX", "failed to resolve __ZN11IOPCIDevice21extendedConfigWrite16Eyt");
}

orgExtendedConfigRead16 = reinterpret_cast<t_extended_config_read16>(patcher.solveSymbol(index, "__ZN11IOPCIDevice20extendedConfigRead16Ey", address, size));
}

progressState |= ProcessingState::IOPCIFamilyRouted;
Expand Down Expand Up @@ -450,13 +464,13 @@ bool HBFX::initialize_nvstorage()

nvstorage_initialized = true;

if (!config.dumpNvram)
if (!ADDPR(hbfx_config).dumpNvram)
{
OSData *data = nvstorage.read("EmuVariableUefiPresent", NVStorage::OptRaw);
if (data && data->isEqualTo(OSString::withCStringNoCopy("Yes")))
{
DBGLOG("HBFX", "EmuVariableUefiPresent is detected, set dumpNvram to true");
config.dumpNvram = true;
ADDPR(hbfx_config).dumpNvram = true;
}
OSSafeReleaseNULL(data);
}
Expand Down
9 changes: 2 additions & 7 deletions HibernationFixup/kern_hbfx.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,7 @@ class HBFX {
* extendedConfigWrite16 callback type
*/
using t_extended_config_write16 = void (*) (IOService *that, UInt64 offset, UInt16 data);

/**
* extendedConfigRead16 callback type
*/
using t_extended_config_read16 = UInt16 (*) (IOService *that, UInt64 offset);



/**
Expand All @@ -104,7 +100,6 @@ class HBFX {
t_pack_a orgPackA {nullptr};
t_restore_machine_state orgRestoreMachineState {nullptr};
t_extended_config_write16 orgExtendedConfigWrite16 {nullptr};
t_extended_config_read16 orgExtendedConfigRead16 {nullptr};


/**
Expand Down Expand Up @@ -134,7 +129,7 @@ class HBFX {
using t_iopolled_file_pollers_open = IOReturn (*) (void * vars, uint32_t state, bool abortable);
t_iopolled_file_pollers_open IOPolledFilePollersOpen {nullptr};

bool disable_pci_config_command {false};
bool correct_pci_config_command {false};

/**
* Current progress mask
Expand Down
16 changes: 8 additions & 8 deletions HibernationFixup/kern_start.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const char *Configuration::bootargBeta[] {
};


Configuration config;
Configuration ADDPR(hbfx_config);


void Configuration::readArguments() {
Expand Down Expand Up @@ -56,16 +56,16 @@ PluginConfiguration ADDPR(config) {
xStringify(PRODUCT_NAME),
parseModuleVersion(xStringify(MODULE_VERSION)),
LiluAPI::AllowNormal,
config.bootargOff,
arrsize(config.bootargOff),
config.bootargDebug,
arrsize(config.bootargDebug),
config.bootargBeta,
arrsize(config.bootargBeta),
ADDPR(hbfx_config).bootargOff,
arrsize(ADDPR(hbfx_config).bootargOff),
ADDPR(hbfx_config).bootargDebug,
arrsize(ADDPR(hbfx_config).bootargDebug),
ADDPR(hbfx_config).bootargBeta,
arrsize(ADDPR(hbfx_config).bootargBeta),
KernelVersion::MountainLion,
KernelVersion::HighSierra,
[]() {
config.readArguments();
ADDPR(hbfx_config).readArguments();
hbfx.init();
}
};
Expand Down

0 comments on commit 3d567c0

Please sign in to comment.