|
| 1 | +// |
| 2 | +// Innie.cpp |
| 3 | +// Innie |
| 4 | +// |
| 5 | +// Copyright © 2021 cdf. All rights reserved. |
| 6 | +// |
| 7 | + |
| 8 | +#include <IOKit/IOLib.h> |
| 9 | +#include <IOKit/IORegistryEntry.h> |
| 10 | +#include <IOKit/IODeviceTreeSupport.h> |
| 11 | + |
| 12 | +#include "Innie.hpp" |
| 13 | + |
| 14 | +#define super IOService |
| 15 | + |
| 16 | +OSDefineMetaClassAndStructors(Innie, IOService) |
| 17 | + |
| 18 | +bool Innie::init(OSDictionary *dict) { |
| 19 | + if (!super::init()) |
| 20 | + return false; |
| 21 | + |
| 22 | + return true; |
| 23 | +} |
| 24 | + |
| 25 | +void Innie::free(void) { |
| 26 | + super::free(); |
| 27 | +} |
| 28 | + |
| 29 | +IOService *Innie::probe(IOService *provider, SInt32 *score) { |
| 30 | + if (super::probe(provider, score)==0) |
| 31 | + return 0; |
| 32 | + |
| 33 | + return this; |
| 34 | +} |
| 35 | + |
| 36 | +bool Innie::start(IOService *provider) { |
| 37 | + DBGLOG("starting\n"); |
| 38 | + |
| 39 | + if (!super::start(provider)) |
| 40 | + return false; |
| 41 | + |
| 42 | + processRoot(); |
| 43 | + super::registerService(); |
| 44 | + return true; |
| 45 | +} |
| 46 | + |
| 47 | +void Innie::stop(IOService *provider) { |
| 48 | + super::stop(provider); |
| 49 | +} |
| 50 | + |
| 51 | +void Innie::processRoot() { |
| 52 | + if (auto entry = IORegistryEntry::fromPath("/AppleACPIPlatformExpert", gIOServicePlane)) { |
| 53 | + IORegistryEntry *pciRoot = nullptr; |
| 54 | + size_t repeat = 0; |
| 55 | + bool found = false; |
| 56 | + |
| 57 | + do { |
| 58 | + if (auto iterator = entry->getChildIterator(gIOServicePlane)) { |
| 59 | + while ((pciRoot = OSDynamicCast(IORegistryEntry, iterator->getNextObject())) != nullptr) { |
| 60 | + const char *name = pciRoot->getName(); |
| 61 | + if (name && !strncmp("PC", name, 2)) { |
| 62 | + found = true; |
| 63 | + DBGLOG("found PCI root %s", pciRoot->getName()); |
| 64 | + while (OSDynamicCast(OSBoolean, pciRoot->getProperty("IOPCIConfigured")) != kOSBooleanTrue) { |
| 65 | + DBGLOG("waiting for PCI root to be configured"); |
| 66 | + IOSleep(1); |
| 67 | + } |
| 68 | + recurseBridge(pciRoot); |
| 69 | + } |
| 70 | + } |
| 71 | + iterator->release(); |
| 72 | + } |
| 73 | + } while (repeat++ < 0x10000000 && !found); |
| 74 | + |
| 75 | + DBGLOG("found PCI root in %lu attempts", repeat); |
| 76 | + entry->release(); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +void Innie::recurseBridge(IORegistryEntry *entry) { |
| 81 | + if (auto iterator = entry->getChildIterator(gIODTPlane)) { |
| 82 | + IORegistryEntry *childEntry = nullptr; |
| 83 | + |
| 84 | + // Go through child entries of bridge, finding every other bridge and every SATA and NVMe device |
| 85 | + while ((childEntry = OSDynamicCast(IORegistryEntry, iterator->getNextObject())) != nullptr) { |
| 86 | + uint32_t code = 0; |
| 87 | + if (auto class_code = childEntry->getProperty("class-code")) { |
| 88 | + if (auto codeData = OSDynamicCast(OSData, class_code)) { |
| 89 | + code=*(uint32_t*)codeData->getBytesNoCopy(); |
| 90 | + if (code == classCode::SATADevice || code == classCode::NVMeDevice){ |
| 91 | + DBGLOG("found device %s", childEntry->getName()); |
| 92 | + if (auto built_in = childEntry->getProperty("built-in")) { |
| 93 | + DBGLOG("device is already built-in"); |
| 94 | + } else { |
| 95 | + internalizeDevice(childEntry); |
| 96 | + } |
| 97 | + break; |
| 98 | + } |
| 99 | + if (code == classCode::PCIBridge) { |
| 100 | + DBGLOG("found bridge %s", childEntry->getName()); |
| 101 | + while (OSDynamicCast(OSBoolean, childEntry->getProperty("IOPCIResourced")) != kOSBooleanTrue) { |
| 102 | + DBGLOG("waiting for bridge to be resourced"); |
| 103 | + IOSleep(1); |
| 104 | + } |
| 105 | + recurseBridge(childEntry); |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + iterator->release(); |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +void Innie::internalizeDevice(IORegistryEntry *entry) { |
| 115 | + DBGLOG("adding built-in property"); |
| 116 | + |
| 117 | + setBuiltIn(entry); |
| 118 | + |
| 119 | + // Stop if entry is not yet resourced |
| 120 | + if (OSDynamicCast(OSBoolean, entry->getProperty("IOPCIResourced")) != kOSBooleanTrue) |
| 121 | + return; |
| 122 | + |
| 123 | + // Otherwise update existing properties |
| 124 | + if (auto driverIterator = IORegistryIterator::iterateOver(entry, gIOServicePlane, kIORegistryIterateRecursively)) { |
| 125 | + IORegistryEntry *driverEntry = nullptr; |
| 126 | + while ((driverEntry = OSDynamicCast(IORegistryEntry, driverIterator->getNextObject())) != nullptr) { |
| 127 | + DBGLOG("updating other properties"); |
| 128 | + updateOtherProperties(driverEntry); |
| 129 | + } |
| 130 | + driverIterator->release(); |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +void Innie::setBuiltIn(IORegistryEntry *entry) { |
| 135 | + if (entry) { |
| 136 | + char dummy = '\0'; |
| 137 | + entry->setProperty("built-in", &dummy, 1); |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +void Innie::updateOtherProperties(IORegistryEntry *entry) { |
| 142 | + if (entry) { |
| 143 | + OSString *internal = OSString::withCString("Internal"); |
| 144 | + OSString *internalIcon = OSString::withCString("Internal.icns"); |
| 145 | + |
| 146 | + // Update icon |
| 147 | + if (auto icon = entry->getProperty("IOMediaIcon")) { |
| 148 | + if (auto dict = OSDynamicCast(OSDictionary, icon)) { |
| 149 | + dict = OSDictionary::withDictionary(dict); |
| 150 | + if (auto res = dict->getObject("IOBundleResourceFile")) { |
| 151 | + dict->setObject("IOBundleResourceFile", internalIcon); |
| 152 | + entry->setProperty("IOMediaIcon", dict); |
| 153 | + } |
| 154 | + dict->release(); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + // Update interconnect |
| 159 | + if (auto loc = entry->getProperty("Physical Interconnect Location")) { |
| 160 | + if (auto prop = OSDynamicCast(OSString, loc)) { |
| 161 | + entry->setProperty("Physical Interconnect Location", internal); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + // Update update protocol characteristics |
| 166 | + if (auto proto = entry->getProperty("Protocol Characteristics")) { |
| 167 | + if (auto dict = OSDynamicCast(OSDictionary, proto)) { |
| 168 | + dict = OSDictionary::withDictionary(dict); |
| 169 | + if (auto loc = dict->getObject("Physical Interconnect Location")) { |
| 170 | + if (auto prop = OSDynamicCast(OSString, loc)) { |
| 171 | + dict->setObject("Physical Interconnect Location", internal); |
| 172 | + entry->setProperty("Protocol Characteristics", dict); |
| 173 | + } |
| 174 | + } |
| 175 | + dict->release(); |
| 176 | + } |
| 177 | + } |
| 178 | + internal->release(); |
| 179 | + internalIcon->release(); |
| 180 | + } |
| 181 | +} |
0 commit comments