### Example boot-args for HibernationFixup Source: https://github.com/acidanthera/hibernationfixup/blob/master/_autodocs/api-reference/kern_start.md Shows example kernel command line / NVRAM boot-args for various HibernationFixup features: disabling the kext, enabling debug logging with NVRAM dump, ignoring PCI devices, setting auto-hibernate mode, and disabling the IOPCIFamily patch. ```text boot-args = "-hbfxoff" # completely disable the kext boot-args = "-hbfxdbg -hbfx-dump-nvram" # debug logging + NVRAM dump boot-args = "hbfx-patch-pci=XHC,IMEI,IGPU" # ignore these devices in the PCI patch boot-args = "hbfx-ahbm=5" # EnableAutoHibernation(1) + WhenExternalPowerIsDisconnected(4) boot-args = "-hbfx-disable-patch-pci" # disable the IOPCIFamily patch ``` -------------------------------- ### HBFX::init() invocation example Source: https://github.com/acidanthera/hibernationfixup/blob/master/_autodocs/api-reference/kern_hbfx.md Shows how Lilu invokes init() inside the plugin start callback. It returns true and installs patcher/kext callbacks. ```cpp // Inside the plugin start callback (kern_start.cpp:83-84) ADDPR(hbfx_config).readArguments(); hbfx.init(); // returns true; installs patcher/kext callbacks ``` -------------------------------- ### Consumer perspective: checking and invoking the plugin configuration Source: https://github.com/acidanthera/hibernationfixup/blob/master/_autodocs/api-reference/kern_start.md Shows how Lilu inspects the PluginConfiguration: verifies the kernel version is within the supported range and the -hbfxoff boot argument is absent, then invokes the start callback which runs readArguments() and hbfx.init(). ```cpp // What Lilu inspects: a PluginConfiguration with name "HibernationFixup", // version 1.5.5, mode AllowNormal, kernel range MountainLion..Tahoe. PluginConfiguration cfg = ADDPR(config); bool withinRange = cfg.minKernel <= getKernelVersion() && cfg.maxKernel >= getKernelVersion(); if (withinRange && !checkKernelArgument("-hbfxoff")) { cfg.startCallback(); // -> readArguments(); hbfx.init(); } ``` -------------------------------- ### Configuration::readArguments() declaration Source: https://github.com/acidanthera/hibernationfixup/blob/master/_autodocs/api-reference/kern_start.md Declares the method that parses boot arguments not handled by Lilu's disable/debug/beta machinery. Called by the plugin start callback before HBFX::init(). ```cpp void Configuration::readArguments(); ``` -------------------------------- ### PluginConfiguration ADDPR(config) definition in kern_start.cpp Source: https://github.com/acidanthera/hibernationfixup/blob/master/_autodocs/api-reference/kern_start.md Defines the Lilu plugin configuration for HibernationFixup. Initializes plugin name, version, API mode, boot-arg tables, kernel version range (MountainLion to Tahoe), and a start callback that parses boot-args and calls HBFX::init(). The callback returns void and never throws; failures are reported via SYSLOG. ```cpp PluginConfiguration ADDPR(config) { xStringify(PRODUCT_NAME), parseModuleVersion(xStringify(MODULE_VERSION)), LiluAPI::AllowNormal, 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::Tahoe, []() { ADDPR(hbfx_config).readArguments(); hbfx.init(); } }; ``` -------------------------------- ### HBFX::deinit() invocation example Source: https://github.com/acidanthera/hibernationfixup/blob/master/_autodocs/api-reference/kern_hbfx.md Shows the kext-stop equivalent call to deinit(), which invokes nvstorage.deinit(). ```cpp // Kext-stop equivalent hbfx.deinit(); // nvstorage.deinit() ``` -------------------------------- ### Basic use of gmtime_r Source: https://github.com/acidanthera/hibernationfixup/blob/master/_autodocs/api-reference/gmtime.md Example from kern_hbfx.cpp:78-84 showing how to get the current time and format it as a calendar date. Uses microtime() to fill a timeval and gmtime_r to convert to struct tm for logging. ```cpp #include "gmtime.h" #include struct timeval tv; struct tm tm; microtime(&tv); gmtime_r(tv.tv_sec, &tm); DBGLOG("HBFX", "%02d.%02d.%04d %02d:%02d:%02d", tm.tm_mday, tm.tm_mon, tm.tm_year, tm.tm_hour, tm.tm_min, tm.tm_sec); ``` -------------------------------- ### HBFX::init() declaration Source: https://github.com/acidanthera/hibernationfixup/blob/master/_autodocs/api-reference/kern_hbfx.md Declares the public initializer for the HBFX plugin. It takes no arguments and returns bool, always true in practice. Called by the PluginConfiguration start callback. ```cpp bool init(); ``` -------------------------------- ### Set and remove HibernationFixup NVRAM variables with nvram Source: https://github.com/acidanthera/hibernationfixup/blob/master/_autodocs/configuration.md Use these commands to configure HibernationFixup options via NVRAM under the Lilu read-only GUID E09B9297-7928-4440-9AAB-D1F8536FBF0A. The first three set hbfx-dump-nvram, hbfx-patch-pci, and hbfx-ahbm; the last removes hbfx-ahbm. Values must match size checks: booleans exactly sizeof(bool), hbfx-ahbm exactly sizeof(int), and hbfx-patch-pci at least 4 bytes. ```sh sudo nvram E09B9297-7928-4440-9AAB-D1F8536FBF0A:hbfx-dump-nvram=%01 sudo nvram E09B9297-7928-4440-9AAB-D1F8536FBF0A:hbfx-patch-pci="XHC,IMEI,IGPU" sudo nvram E09B9297-7928-4440-9AAB-D1F8536FBF0A:hbfx-ahbm=%0a # 10 decimal = WhenBatteryIsNotCharging sudo nvram -d E09B9297-7928-4440-9AAB-D1F8536FBF0A:hbfx-ahbm # remove again ``` -------------------------------- ### Computing a postponed RTC alarm Source: https://github.com/acidanthera/hibernationfixup/blob/master/_autodocs/api-reference/gmtime.md Example from kern_hbfx.cpp:262-278 showing how to compute a postponed RTC alarm by adding a standby delay to the current time and converting to an IOPMCalendarStruct. Uses gmtime_r to fill the calendar fields. ```cpp struct tm tm; struct timeval tv; microtime(&tv); tv.tv_sec += standby_delay; // postpone by the standby delay gmtime_r(tv.tv_sec, &tm); IOPMCalendarStruct overriden_calendar { static_cast(tm.tm_year), static_cast(tm.tm_mon), static_cast(tm.tm_mday), static_cast(tm.tm_hour), static_cast(tm.tm_min), static_cast(tm.tm_sec), calendar->selector }; ``` -------------------------------- ### ProcessingState progress bitmask enum Source: https://github.com/acidanthera/hibernationfixup/blob/master/_autodocs/api-reference/kern_hbfx.md Defines the bitmask used to track routing progress for IOPCIFamily, Kernel, AppleRTC, and X86Plugin stages. Guards each routing stage so hooks are installed at most once. Source: kern_hbfx.hpp:146-155. ```cpp struct ProcessingState { enum { NothingReady = 0, IOPCIFamilyRouted = 1, KernelRouted = 2, AppleRTCRouted = 4, X86PluginRouted = 8, EverythingDone = IOPCIFamilyRouted | KernelRouted | AppleRTCRouted | X86PluginRouted, }; }; ``` -------------------------------- ### Calendar helper functions Source: https://github.com/acidanthera/hibernationfixup/blob/master/_autodocs/api-reference/gmtime.md Internal helpers in gmtime.cpp used by gmtime_r for leap-year and day/month calculations. Note that daysInMonth(0, year) indexes days[0] == -1 and reduceDaysToMonths starts month at 0, so behavior for inputs before 1970 is not defined. ```cpp bool isLeapYear(int year); // gmtime.cpp:14 — ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0) int daysInYear(int year); // gmtime.cpp:19 — 366 : 365 int daysInMonth(int month, int year); // gmtime.cpp:24 — table lookup, Feb=29 on leap years int reduceDaysToYear(time_t &days); // gmtime.cpp:31 — subtracts whole years from 1970, returns year int reduceDaysToMonths(time_t &days, int year); // gmtime.cpp:39 — subtracts whole months, returns month ``` -------------------------------- ### Boot-arg tables: bootargOff, bootargDebug, bootargBeta Source: https://github.com/acidanthera/hibernationfixup/blob/master/_autodocs/api-reference/kern_start.md Defines the boot-argument strings used by Lilu's disable/debug/beta machinery. -hbfxoff disables kext loading, -hbfxdbg enables debug logging, -hbfxbeta enables loading on unsupported macOS versions. ```cpp const char *Configuration::bootargOff[] { "-hbfxoff" }; const char *Configuration::bootargDebug[] { "-hbfxdbg" }; const char *Configuration::bootargBeta[] { "-hbfxbeta" }; ``` -------------------------------- ### Global state: HBFX instance and Configuration object Source: https://github.com/acidanthera/hibernationfixup/blob/master/_autodocs/api-reference/kern_start.md Defines the single HBFX instance and the global Configuration object shared by all translation units. The Configuration object is exported via the ADDPR() macro for Lilu. ```cpp static HBFX hbfx; // kern_start.cpp:14 Configuration ADDPR(hbfx_config); // kern_start.cpp:26 (extern declared in kern_config.hpp:82) ``` -------------------------------- ### Configuration class definition with boot arguments and AutoHibernateModes enum Source: https://github.com/acidanthera/hibernationfixup/blob/master/_autodocs/api-reference/kern_config.md Defines the central configuration object for the kext, holding resolved boot-args/NVRAM values and the AutoHibernateModes bit-flag enum. The class declares static boot argument constants, methods like readArguments(), and fields such as dumpNvram, patchPCIFamily, ignored_device_list, and autoHibernateMode. The global instance is exported via ADDPR(hbfx_config) and instantiated in kern_start.cpp. ```cpp class Configuration { public: /** * Possible boot arguments */ static const char *bootargOff[]; static const char *bootargDebug[]; static const char *bootargBeta[]; static constexpr const char *bootargDumpNvram {"-hbfx-dump-nvram"}; // write NVRAM to file static constexpr const char *bootargPatchPCIWithList {"hbfx-patch-pci"}; // patch pci family ignored device list static constexpr const char *bootargDisablePatchPCI {"-hbfx-disable-patch-pci"}; // disable patch pci family static constexpr const char *bootargAutoHibernateMode {"hbfx-ahbm"}; // auto hibernate mode public: /** * Retrieve boot arguments */ void readArguments(); /** * dump nvram to /nvram.plist */ bool dumpNvram {false}; /** * patch PCI Family */ bool patchPCIFamily {true}; /** * device list (can be separated by comma, space or something like that) */ char ignored_device_list[64] {}; /* Flags used to control automatic hibernation behavior (takes place only when system goes to sleep) */ enum AutoHibernateModes { // If this flag is set, system will hibernate instead of regular sleep (flags below can be used to limit this behavior) EnableAutoHibernation = 1, // Auto hibernation can happen when lid is closed (if bit is not set - no matter which status lid has) WhenLidIsClosed = 2, // Auto hibernation can happen when external power is disconnected (if bit is not set - no matter whether it is connected) WhenExternalPowerIsDisconnected = 4, // Auto hibernation can happen when battery is not charging (if bit is not set - no matter whether it is charging) WhenBatteryIsNotCharging = 8, // Auto hibernation can happen when battery is at warning level (osx is responsible for this level) WhenBatteryIsAtWarnLevel = 16, // Auto hibernation can happen when battery is at critical level (osx is responsible for this level) WhenBatteryAtCriticalLevel = 32, // Do not alter next wake up time, macOS is fully responsible for sleep maintenance dark wakes DoNotOverrideWakeUpTime = 64, // Disable power event kStimulusDarkWakeActivityTickle in kernel, so this event cannot be a trigger for switching from dark wake to full wake DisableStimulusDarkWakeActivityTickle = 128, // Next 4 bits are used to specify minimal capacity percent remaining value when hibernation will be forced. // Can be used together with WhenBatteryIsAtWarnLevel or WhenBatteryAtCriticalLevel, when IOPMPowerSource cannot detect // warning or critical battery level RemainCapacityBit1 = 256, RemainCapacityBit2 = 512, RemainCapacityBit3 = 1024, RemainCapacityBit4 = 2048 }; int autoHibernateMode {0}; Configuration() = default; }; extern Configuration ADDPR(hbfx_config); ``` -------------------------------- ### IOPCIBridge_restoreMachineState declaration Source: https://github.com/acidanthera/hibernationfixup/blob/master/_autodocs/api-reference/kern_hbfx.md Sets correct_pci_config_command while options include kMachineRestoreDehibernate, forwards, clears the flag, cancels nextSleepTimer, and reschedules checkCapacityTimer with a 60000 ms delay. ```cpp static IOReturn IOPCIBridge_restoreMachineState(IOService *that, IOOptionBits options, IOService * device); ``` -------------------------------- ### packA declaration Source: https://github.com/acidanthera/hibernationfixup/blob/master/_autodocs/api-reference/kern_hbfx.md Panic-dump support. Forwards to the original _packA, enables interrupts/preemption when not in interrupt context, and if initializeNVStorage() succeeds, splits panic info into ≤768-byte chunks written as AAPL,PanicInfo%04d, saves to /nvram.plist, and calls sync(). ```cpp static int packA(char *inbuf, uint32_t length, uint32_t buflen); ``` -------------------------------- ### Machine-restore options enum (kMachineRestoreDehibernate) Source: https://github.com/acidanthera/hibernationfixup/blob/master/_autodocs/api-reference/osx_defines.md Defines machine-restore option flags. kMachineRestoreDehibernate gates the PCI config-command correction in IOPCIBridge_restoreMachineState. ```cpp enum { kMachineRestoreBridges = 0x00000001, kMachineRestoreEarlyDevices = 0x00000002, kMachineRestoreDehibernate = 0x00000004, kMachineRestoreTunnels = 0x00000008, }; ``` -------------------------------- ### Hibernate state enum (kIOHibernateStateKey) Source: https://github.com/acidanthera/hibernationfixup/blob/master/_autodocs/api-reference/osx_defines.md Defines hibernate state values. Compared in HBFX::IOHibernateSystemSleep against the PMRootDomain IOHibernateState property. ```cpp enum { kIOHibernateStateInactive = 0, kIOHibernateStateHibernating = 1, /* writing image */ kIOHibernateStateWakingFromHibernate = 2 /* booted and restored image */ }; ``` -------------------------------- ### IOPCIDevice_extendedConfigWrite16 declaration Source: https://github.com/acidanthera/hibernationfixup/blob/master/_autodocs/api-reference/kern_hbfx.md When correct_pci_config_command is set and the offset is kIOPCIConfigCommand, and the device is not in ignored_device_list (or the list is empty), forcibly sets kIOPCICommandMemorySpace in data before forwarding. Prevents the memory-space bit from being cleared during dehibernate, fixing hang or black screen after resume. ```cpp static void IOPCIDevice_extendedConfigWrite16(IOService *that, UInt64 offset, UInt16 data); ``` -------------------------------- ### Grep HBFX errors with dmesg Source: https://github.com/acidanthera/hibernationfixup/blob/master/_autodocs/errors.md Equivalent to log show on older systems. Searches kernel ring buffer for HBFX messages. ```sh dmesg | grep HBFX ``` -------------------------------- ### Configuration::readArguments() declaration Source: https://github.com/acidanthera/hibernationfixup/blob/master/_autodocs/api-reference/kern_config.md Declares the method that parses kernel command-line arguments and forces patchPCIFamily to false on macOS older than Sierra 10.12.1. ```cpp void readArguments(); ``` -------------------------------- ### IOPMSystemSleepPolicyVariables and IOPMSystemSleepParameters structs Source: https://github.com/acidanthera/hibernationfixup/blob/master/_autodocs/api-reference/osx_defines.md Structs passed to the X86PlatformPlugin_sleepPolicyHandler hook; HBFX mutates them to force hibernate. Field tables are in types.md. ```cpp struct IOPMSystemSleepPolicyVariables { uint32_t signature; // kIOPMSystemSleepPolicySignature uint32_t version; // kIOPMSystemSleepPolicyVersion uint64_t currentCapability; // current system capability bits uint64_t highestCapability; // highest system capability bits uint64_t sleepFactors; // sleep factor bits uint32_t sleepReason; // kIOPMSleepReason* uint32_t sleepPhase; // identify the sleep phase uint32_t hibernateMode; // current hibernate mode uint32_t standbyDelay; // standby delay in seconds uint32_t standbyTimer; // standby timer in seconds uint32_t poweroffDelay; // auto-poweroff delay in seconds uint32_t scheduledAlarms; // bitmask of scheduled alarm types uint32_t poweroffTimer; // auto-poweroff timer in seconds uint32_t reserved[49]; // pad sizeof 256 bytes }; struct IOPMSystemSleepParameters { uint16_t version; uint16_t reserved1; uint32_t sleepType; uint32_t sleepFlags; uint32_t ecWakeEvents; uint32_t ecWakeTimer; uint32_t ecPoweroffTimer; uint32_t reserved2[10]; } __attribute__((packed)); ``` -------------------------------- ### HBFX class declaration with hook methods Source: https://github.com/acidanthera/hibernationfixup/blob/master/_autodocs/api-reference/kern_hbfx.md Defines the core HBFX class, instantiated once as static HBFX hbfx; and accessed via static HBFX *callbackHBFX. Includes private methods for kernel/kext patching, NVRAM storage, and static hook methods for power management and RTC. ```cpp class HBFX { public: bool init(); void deinit(); private: /** * Patch kernel * * @param patcher KernelPatcher instance */ void processKernel(KernelPatcher &patcher); /** * Patch kext if needed and prepare other patches * * @param patcher KernelPatcher instance * @param index kinfo handle * @param address kinfo load address * @param size kinfo memory size */ void processKext(KernelPatcher &patcher, size_t index, mach_vm_address_t address, size_t size); /** * Check the second RTC memory bank availability */ bool checkRTCExtendedMemory(); /** * Initialize NVStorage * */ bool initializeNVStorage(); // read supported options from NVRAM void readConfigFromNVRAM(); // return pointer to IOPMPowerSource IOPMPowerSource *getPowerSource(); // return true if standby/autopoweroff is enabled bool isStandbyEnabled(IOPMrootDomain* pm_root, uint32_t &standby_delay, bool &pmset_default_mode); IOReturn explicitlyCallSetMaintenanceWakeCalendar(); void checkCapacity(); /** * Hooked methods / callbacks */ static IOReturn IOHibernateSystemSleep(void); static IOReturn IOHibernateSystemWake(void); static void IOPMrootDomain_evaluatePolicy(IOPMrootDomain* that, int stimulus, uint32_t arg); static void IOPMrootDomain_requestFullWake(IOPMrootDomain* that, uint32_t reason); static IOReturn IOPMrootDomain_setMaintenanceWakeCalendar(IOPMrootDomain* that, const IOPMCalendarStruct* calendar); static IOReturn AppleRTC_setupDateTimeAlarm(void *that, void* rtcDateTime); static IOReturn X86PlatformPlugin_sleepPolicyHandler(void * target, IOPMSystemSleepPolicyVariables * vars, IOPMSystemSleepParameters * params); static int packA(char *inbuf, uint32_t length, uint32_t buflen); static IOReturn IOPCIBridge_restoreMachineState(IOService *that, IOOptionBits options, IOService * device); static void IOPCIDevice_extendedConfigWrite16(IOService *that, UInt64 offset, UInt16 data); }; ``` -------------------------------- ### Combining AutoHibernateModes flags for hbfx-ahbm Source: https://github.com/acidanthera/hibernationfixup/blob/master/_autodocs/api-reference/kern_config.md Shows how to combine AutoHibernateModes flags to force hibernate when the lid is closed or battery falls to ≤10% remaining capacity, but only on battery power. The same value is normally supplied via the hbfx-ahbm boot-arg or NVRAM variable. ```cpp // Force hibernate when the lid is closed OR battery falls to ≤ 10 % remaining capacity, // but only while on battery power (values: WhenLidIsClosed(2) + WhenBatteryIsNotCharging(8) // + RemainCapacityBit4(2048) + RemainCapacityBit2(512)). ADDPR(hbfx_config).autoHibernateMode = Configuration::WhenLidIsClosed | Configuration::WhenBatteryIsNotCharging | Configuration::RemainCapacityBit4 | Configuration::RemainCapacityBit2; // 2+8+2048+512 = 2570 ``` -------------------------------- ### Show historical HBFX errors with log show Source: https://github.com/acidanthera/hibernationfixup/blob/master/_autodocs/errors.md Retrieve HBFX error messages from the last hour. Useful for diagnosing past sleep/hibernate failures. ```sh log show --last 1h --predicate 'eventMessage CONTAINS "HBFX" AND messageType == error' ``` -------------------------------- ### FullWakeReason enum (for requestFullWake) Source: https://github.com/acidanthera/hibernationfixup/blob/master/_autodocs/api-reference/osx_defines.md Defines full wake reasons for `requestFullWake`. `HBFX::IOPMrootDomain_requestFullWake` resets sleep-tracking state only for `kFullWakeReasonLocalUser` and `fFullWakeReasonDisplayOnAndLocalUser`. Source: `osx_defines.h:216-222`. ```cpp enum FullWakeReason { kFullWakeReasonNone = 0, kFullWakeReasonLocalUser = 1, kFullWakeReasonDisplayOn = 2, fFullWakeReasonDisplayOnAndLocalUser = 3 }; ``` -------------------------------- ### Watch HBFX errors with log stream Source: https://github.com/acidanthera/hibernationfixup/blob/master/_autodocs/errors.md Use this command to monitor HBFX error logs in real time while reproducing a sleep/hibernate problem. It filters for messages containing 'HBFX' at error level. ```sh log stream --predicate 'eventMessage CONTAINS "HBFX"' --level error ``` -------------------------------- ### kIOPMSystemSleepPolicySignature and kIOPMSystemSleepPolicyVersion constants Source: https://github.com/acidanthera/hibernationfixup/blob/master/_autodocs/api-reference/osx_defines.md Related constants used by the sleep policy structs. ```cpp #define kIOPMSystemSleepPolicySignature 0x54504c53 #define kIOPMSystemSleepPolicyVersion 2 ``` -------------------------------- ### Force patchPCIFamily false on macOS < 10.12.1 Source: https://github.com/acidanthera/hibernationfixup/blob/master/_autodocs/configuration.md On macOS older than Sierra 10.12.1, the patchPCIFamily option is unconditionally forced to false because the PCI patch is only required/beneficial on 10.12.1+. This is the else branch at kern_start.cpp:56-59. ```cpp if ((getKernelVersion() == KernelVersion::Sierra && getKernelMinorVersion() >= 1) || getKernelVersion() >= KernelVersion::HighSierra) { // parse hbfx-patch-pci / -hbfx-disable-patch-pci } else { patchPCIFamily = false; // kern_start.cpp:50-53 } ``` -------------------------------- ### hibernate_write_image return values enum Source: https://github.com/acidanthera/hibernationfixup/blob/master/_autodocs/api-reference/osx_defines.md Defines return values for `hibernate_write_image`: `kIOHibernatePostWriteSleep` (0), `kIOHibernatePostWriteWake` (1), `kIOHibernatePostWriteHalt` (2), and `kIOHibernatePostWriteRestart` (3). ```cpp enum{ kIOHibernatePostWriteSleep = 0, kIOHibernatePostWriteWake = 1, kIOHibernatePostWriteHalt = 2, kIOHibernatePostWriteRestart = 3 }; ``` -------------------------------- ### X86PlatformPlugin_sleepPolicyHandler declaration Source: https://github.com/acidanthera/hibernationfixup/blob/master/_autodocs/api-reference/kern_hbfx.md Declares the auto-hibernation decision core. Forwards to the original handler and returns its error immediately if not KERN_SUCCESS. Caches hibernate mode, standby delay, poweroff delay, sleep phase, and sleep factors/reason/type/flags. Evaluates power source and lid state to set forceHibernate, and forces standby/hibernate sleep types in certain phases. ```cpp static IOReturn X86PlatformPlugin_sleepPolicyHandler(void * target, IOPMSystemSleepPolicyVariables * vars, IOPMSystemSleepParameters * params); ``` -------------------------------- ### Function-pointer aliases for kernel and AppleRTC symbols Source: https://github.com/acidanthera/hibernationfixup/blob/master/_autodocs/types.md Private members of HBFX that hold resolved symbols from the kernel or AppleRTC. All aliases default to nullptr and are resolved in HBFX::processKernel (kernel symbols) and HBFX::processKext (AppleRTC helpers). Consumed by HBFX::packA, HBFX::AppleRTC_setupDateTimeAlarm, and HBFX::processKernel's nextSleepTimer lambda. ```cpp using t_sync = int (*) (__unused proc_t p, __unused struct sync_args *uap, __unused int32_t *retval); t_sync sync {nullptr}; using t_preemption_enabled = boolean_t (*) (void); t_preemption_enabled preemption_enabled {nullptr}; using t_enable_preemption = void (*) (void); t_enable_preemption enable_preemption {nullptr}; using t_disable_preemption = void (*) (void); t_disable_preemption disable_preemption {nullptr}; using t_ml_at_interrupt_context = boolean_t (*) (void); t_ml_at_interrupt_context ml_at_interrupt_context {nullptr}; using t_ml_get_interrupts_enabled = boolean_t (*) (void); t_ml_get_interrupts_enabled ml_get_interrupts_enabled {nullptr}; using t_ml_set_interrupts_enabled = boolean_t (*) (boolean_t enable); t_ml_set_interrupts_enabled ml_set_interrupts_enabled {nullptr}; using t_convertDateTimeToSeconds = int64_t (*) (void *rtcDateTime); t_convertDateTimeToSeconds convertDateTimeToSeconds {nullptr}; using t_convertSecondsToDateTime = int64_t (*) (int64_t seconds, void *rtcDateTime); t_convertSecondsToDateTime convertSecondsToDateTime {nullptr}; using t_checkSystemSleepEnabled = bool (*) (IOPMrootDomain* that); t_checkSystemSleepEnabled checkSystemSleepEnabled {nullptr}; ``` -------------------------------- ### Hibernate mode flags enum (kIOHibernateModeKey / gIOHibernateMode) Source: https://github.com/acidanthera/hibernationfixup/blob/master/_autodocs/api-reference/osx_defines.md Defines hibernate mode flags. HBFX::isStandbyEnabled treats kIOHibernateModeOn | kIOHibernateModeSleep (hibernatemode 3) as the pmset default mode for auto-hibernation. ```cpp enum { kIOHibernateModeOn = 0x00000001, kIOHibernateModeSleep = 0x00000002, kIOHibernateModeEncrypt = 0x00000004, kIOHibernateModeDiscardCleanInactive = 0x00000008, kIOHibernateModeDiscardCleanActive = 0x00000010, kIOHibernateModeSwitch = 0x00000020, kIOHibernateModeRestart = 0x00000040, kIOHibernateModeSSDInvert = 0x00000080, kIOHibernateModeFileResize = 0x00000100, }; ``` -------------------------------- ### evaluatePolicy stimuli enum (kStimulusDisplayWranglerSleep ... kStimulusLeaveUserActiveState) Source: https://github.com/acidanthera/hibernationfixup/blob/master/_autodocs/api-reference/osx_defines.md Defines stimuli for `evaluatePolicy`. `kStimulusDarkWakeActivityTickle` (5) is suppressed by `HBFX::IOPMrootDomain_evaluatePolicy` when `DisableStimulusDarkWakeActivityTickle` is set in `hbfx-ahbm`. ```cpp enum { kStimulusDisplayWranglerSleep, // 0 kStimulusDisplayWranglerWake, // 1 kStimulusAggressivenessChanged, // 2 kStimulusDemandSystemSleep, // 3 kStimulusAllowSystemSleepChanged, // 4 kStimulusDarkWakeActivityTickle, // 5 kStimulusDarkWakeEntry, // 6 kStimulusDarkWakeReentry, // 7 kStimulusDarkWakeEvaluate, // 8 kStimulusNoIdleSleepPreventers, // 9 kStimulusEnterUserActiveState, // 10 kStimulusLeaveUserActiveState // 11 }; ``` -------------------------------- ### Calendar selector enum (kPMCalendarTypeMaintenance, kPMCalendarTypeSleepService) Source: https://github.com/acidanthera/hibernationfixup/blob/master/_autodocs/api-reference/osx_defines.md Defines values for IOPMCalendarStruct.selector. kPMCalendarTypeMaintenance is used by HBFX::explicitlyCallSetMaintenanceWakeCalendar. ```cpp enum { kPMCalendarTypeMaintenance = 1, kPMCalendarTypeSleepService = 2 }; ``` -------------------------------- ### Sleep phases enum (kIOPMSleepPhase0, kIOPMSleepPhase1, kIOPMSleepPhase2) Source: https://github.com/acidanthera/hibernationfixup/blob/master/_autodocs/api-reference/osx_defines.md Defines sleep phases. `kIOPMSleepPhase0` caches the original sleep values; `kIOPMSleepPhase2` is where the hibernate decision is finalized by `X86PlatformPlugin_sleepPolicyHandler`. ```cpp enum { kIOPMSleepPhase0 = 0, kIOPMSleepPhase1, kIOPMSleepPhase2 }; ``` -------------------------------- ### HBFX::deinit() declaration Source: https://github.com/acidanthera/hibernationfixup/blob/master/_autodocs/api-reference/kern_hbfx.md Declares the public teardown method. It returns void and releases the NVRAM storage backend via nvstorage.deinit(). ```cpp void deinit(); ``` -------------------------------- ### struct IOPMSystemSleepPolicyVariables definition (osx_defines.h:47-67) Source: https://github.com/acidanthera/hibernationfixup/blob/master/_autodocs/types.md Defines the 256-byte sleep policy structure used by the X86PlatformPlugin sleep policy handler. Fields include capability bits, sleep factors, reason, phase, hibernate mode, standby/poweroff timers, and reserved padding. The hook reads most fields and writes sleepFactors/sleepReason to force hibernation. ```cpp struct IOPMSystemSleepPolicyVariables { uint32_t signature; // kIOPMSystemSleepPolicySignature uint32_t version; // kIOPMSystemSleepPolicyVersion uint64_t currentCapability; // current system capability bits uint64_t highestCapability; // highest system capability bits uint64_t sleepFactors; // sleep factor bits uint32_t sleepReason; // kIOPMSleepReason* uint32_t sleepPhase; // identify the sleep phase uint32_t hibernateMode; // current hibernate mode uint32_t standbyDelay; // standby delay in seconds uint32_t standbyTimer; // standby timer in seconds uint32_t poweroffDelay; // auto-poweroff delay in seconds uint32_t scheduledAlarms; // bitmask of scheduled alarm types uint32_t poweroffTimer; // auto-poweroff timer in seconds uint32_t reserved[49]; // pad sizeof 256 bytes }; ``` -------------------------------- ### gmtime_r(time_t timer, struct tm * timeptr) Source: https://github.com/acidanthera/hibernationfixup/blob/master/_autodocs/api-reference/gmtime.md Converts seconds since the Unix epoch to UTC calendar fields. This is a kernel-safe reimplementation of the standard gmtime_r function, filling the tm_sec, tm_min, tm_hour, tm_mday, tm_mon, and tm_year fields of the provided struct tm. ```APIDOC ## gmtime_r(time_t timer, struct tm * timeptr) ### Description Converts seconds since the Unix epoch to UTC calendar fields. This is a kernel-safe reimplementation of the standard gmtime_r function, filling the tm_sec, tm_min, tm_hour, tm_mday, tm_mon, and tm_year fields of the provided struct tm. ### Method Function ### Signature ```cpp struct tm * gmtime_r(time_t timer, struct tm * timeptr); ``` ### Parameters - **timer** (time_t) - Required - Seconds since the Unix epoch (1970-01-01 00:00:00 UTC) - **timeptr** (struct tm *) - Required - Output buffer; must be non-null and writable ### Returns - **struct tm *** - The same `timeptr` pointer on success (the function always returns `timeptr`). ### Throws None ### Implementation Logic 1. `tm_sec = timer % 60; tm_min = timer % 60; tm_hour = timer % 24` after successive divisions; 2. remaining days are reduced by whole years starting from 1970 (`reduceDaysToYear`), then by months (`reduceDaysToMonths`) using `daysInMonth(month, year)` (Feb → 29 on leap years via `isLeapYear`), leaving `tm_mday` as the residual day count. ### Example ```cpp #include "gmtime.h" #include struct timeval tv; struct tm tm; microtime(&tv); gmtime_r(tv.tv_sec, &tm); DBGLOG("HBFX", "%02d.%02d.%04d %02d:%02d:%02d", tm.tm_mday, tm.tm_mon, tm.tm_year, tm.tm_hour, tm.tm_min, tm.tm_sec); ``` ### Notes - Only fills `tm_sec`, `tm_min`, `tm_hour`, `tm_mday`, `tm_mon` and `tm_year`; `tm_wday`, `tm_yday` and `tm_isdst` are left untouched. - Behavior for inputs before 1970 is not defined by this implementation. ``` -------------------------------- ### struct IOPMSystemSleepParameters (osx_defines.h:69-79) Source: https://github.com/acidanthera/hibernationfixup/blob/master/_autodocs/types.md Packed C struct defining the system sleep parameters structure used by the HBFX::X86PlatformPlugin_sleepPolicyHandler hook. The hook forces sleepType to kIOPMSleepTypeStandby or kIOPMSleepTypeHibernate and sets the kIOPMSleepFlagHibernate bit in sleepFlags. The __attribute__((packed)) ensures no padding is added. ```cpp struct IOPMSystemSleepParameters { uint16_t version; uint16_t reserved1; uint32_t sleepType; uint32_t sleepFlags; uint32_t ecWakeEvents; uint32_t ecWakeTimer; uint32_t ecPoweroffTimer; uint32_t reserved2[10]; } __attribute__((packed)); ``` -------------------------------- ### Sleep flags enum (kIOPMSleepFlagHibernate, kIOPMSleepFlagSleepTimerEnable) Source: https://github.com/acidanthera/hibernationfixup/blob/master/_autodocs/api-reference/osx_defines.md Defines sleep flags used in `params->sleepFlags`. `kIOPMSleepFlagHibernate` is 0x00000001 and `kIOPMSleepFlagSleepTimerEnable` is 0x00000002. ```cpp enum { kIOPMSleepFlagHibernate = 0x00000001, kIOPMSleepFlagSleepTimerEnable = 0x00000002 }; ``` -------------------------------- ### gmtime_r function declaration Source: https://github.com/acidanthera/hibernationfixup/blob/master/_autodocs/api-reference/gmtime.md Declares the gmtime_r function that converts time_t seconds since the Unix epoch to UTC calendar fields. It always returns the timeptr pointer on success and does not throw. ```cpp struct tm * gmtime_r(time_t timer, struct tm * timeptr); ``` -------------------------------- ### enum Configuration::AutoHibernateModes (kern_config.hpp:49-74) Source: https://github.com/acidanthera/hibernationfixup/blob/master/_autodocs/types.md Bitmask enum defining auto-hibernation mode flags stored in Configuration::autoHibernateMode (int, default 0). Consumed by HBFX::processKernel, HBFX::processKext, HBFX::X86PlatformPlugin_sleepPolicyHandler, HBFX::IOPMrootDomain_evaluatePolicy, HBFX::checkCapacity, and HBFX::explicitlyCallSetMaintenanceWakeCalendar. Values are powers of two for bitwise combination. ```cpp enum AutoHibernateModes { EnableAutoHibernation = 1, WhenLidIsClosed = 2, WhenExternalPowerIsDisconnected = 4, WhenBatteryIsNotCharging = 8, WhenBatteryIsAtWarnLevel = 16, WhenBatteryAtCriticalLevel = 32, DoNotOverrideWakeUpTime = 64, DisableStimulusDarkWakeActivityTickle = 128, RemainCapacityBit1 = 256, RemainCapacityBit2 = 512, RemainCapacityBit3 = 1024, RemainCapacityBit4 = 2048 }; ``` -------------------------------- ### struct tm definition (HibernationFixup/gmtime.h:11-29) Source: https://github.com/acidanthera/hibernationfixup/blob/master/_autodocs/types.md Named type exported by HibernationFixup for calendar time. Produced by gmtime_r(time_t, struct tm*) and consumed by HBFX::IOHibernateSystemSleep, HBFX::IOPMrootDomain_setMaintenanceWakeCalendar, HBFX::AppleRTC_setupDateTimeAlarm, HBFX::X86PlatformPlugin_sleepPolicyHandler, and HBFX::explicitlyCallSetMaintenanceWakeCalendar for logging and IOPMCalendarStruct construction. Note: tm_wday, tm_yday, and tm_isdst are not written by this gmtime_r. ```cpp struct tm { int tm_sec; // seconds after the minute - [0,59] int tm_min; // minutes after the hour - [0,59] int tm_hour; // hours since midnight - [0,23] int tm_mday; // day of the month - [1,31] int tm_mon; // months since January - [0,11] int tm_year; // years since 1900 int tm_wday; // days since Sunday - [0,6] int tm_yday; // days since January 1 - [0,365] int tm_isdst; // daylight savings time flag }; ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.