Skip to content

Commit

Permalink
Fix model detection slow path
Browse files Browse the repository at this point in the history
  • Loading branch information
vit9696 committed Mar 30, 2020
1 parent 43ed557 commit 0bafc57
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 36 deletions.
11 changes: 3 additions & 8 deletions Lilu/Headers/kern_devinfo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,25 +276,20 @@ class BaseDeviceInfo {
*/
void updateFirmwareVendor();

/**
* Updates bootloaderVendor
*/
void updateBootloaderVendor();

/**
* Updates model information
*/
void updateModelInfo();
public:
/**
* Board identifier board-id
* Board identifier board-id (VMware has "440BX Desktop Reference Platform", eek)
*/
char boardIdentifier[32] {};
char boardIdentifier[48] {};

/**
* Model identifier
*/
char modelIdentifier[32] {};
char modelIdentifier[48] {};

/**
* Computer model type.
Expand Down
2 changes: 1 addition & 1 deletion Lilu/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
</dict>
</dict>
<key>NSHumanReadableCopyright</key>
<string>Copyright © 2016-2018 vit9696. All rights reserved.</string>
<string>Copyright © 2016-2020 vit9696. All rights reserved.</string>
<key>OSBundleLibraries</key>
<dict>
<key>com.apple.kpi.bsd</key>
Expand Down
99 changes: 72 additions & 27 deletions Lilu/Sources/kern_devinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ void BaseDeviceInfo::updateFirmwareVendor() {
}
}

void BaseDeviceInfo::updateBootloaderVendor() {
void BaseDeviceInfo::updateModelInfo() {
auto entry = IORegistryEntry::fromPath("/efi/platform", gIODTPlane);
if (entry) {
if (entry->getProperty("BEMB")) {
Expand All @@ -389,40 +389,86 @@ void BaseDeviceInfo::updateBootloaderVendor() {
// - "REV" key before it is deleted by VirtualSMC (only if we decided to update DataHub)
}

auto data = OSDynamicCast(OSData, entry->getProperty("Model"));
size_t dataSize = data->getLength();
if (data && dataSize > 0) {
auto bytes = static_cast<const char16_t *>(data->getBytesNoCopy());
size_t i = 0;
while (bytes[i] != '\0' && i < sizeof(modelIdentifier) - 1 && i < dataSize) {
modelIdentifier[i] = static_cast<char>(bytes[i]);
i++;
}
}

if (modelIdentifier[0] != '\0')
DBGLOG("dev", "got %s model from /efi/platform", modelIdentifier);
else
DBGLOG("dev", "failed to get valid model from /efi/platform");

data = OSDynamicCast(OSData, entry->getProperty("board-id"));
if (data && data->getLength() > 0)
lilu_os_strlcpy(boardIdentifier, static_cast<const char *>(data->getBytesNoCopy()), sizeof(boardIdentifier));

if (boardIdentifier[0] != '\0')
DBGLOG("dev", "got %s board-id from /efi/platform", boardIdentifier);
else
DBGLOG("dev", "failed to get valid board-id from /efi/platform");

entry->release();
} else {
SYSLOG("dev", "failed to get /efi/platform");
SYSLOG("dev", "failed to get DT /efi/platform");
}
}

void BaseDeviceInfo::updateModelInfo() {
auto entry = IORegistryEntry::fromPath("/", gIODTPlane);
if (entry) {
auto data = OSDynamicCast(OSData, entry->getProperty("model"));
if (data && data->getLength() > 0) {
lilu_os_strlcpy(modelIdentifier, static_cast<const char *>(data->getBytesNoCopy()), sizeof(modelIdentifier));

if (strstr(modelIdentifier, "Book", strlen("Book")))
modelType = WIOKit::ComputerModel::ComputerLaptop;
else
modelType = WIOKit::ComputerModel::ComputerDesktop;
} else {
DBGLOG("dev", "failed to get valid model property");
modelIdentifier[0] = '\0';
}
// Try the legacy approach for old MacEFI and VMware.
while (modelIdentifier[0] == '\0' || boardIdentifier[0] == '\0') {
auto entry = IORegistryEntry::fromPath("/", gIODTPlane);
if (entry) {
bool modelReady = false;

if (boardIdentifier[0] == '\0') {
auto data = OSDynamicCast(OSData, entry->getProperty("board-id"));
if (data && data->getLength() > 0)
lilu_os_strlcpy(boardIdentifier, static_cast<const char *>(data->getBytesNoCopy()), sizeof(boardIdentifier));

if (boardIdentifier[0] != '\0') {
DBGLOG("dev", "got %s board-id from /", boardIdentifier);
// Otherwise we will get ACPI model.
modelReady = true;
} else {
DBGLOG("dev", "failed to get valid board-id from /");
}
} else {
modelReady = true;
}

data = OSDynamicCast(OSData, entry->getProperty("board-id"));
if (data && data->getLength() > 0) {
lilu_os_strlcpy(boardIdentifier, static_cast<const char *>(data->getBytesNoCopy()), sizeof(boardIdentifier));
if (modelReady && modelIdentifier[0] == '\0') {
auto data = OSDynamicCast(OSData, entry->getProperty("model"));
if (data && data->getLength() > 0)
lilu_os_strlcpy(modelIdentifier, static_cast<const char *>(data->getBytesNoCopy()), sizeof(modelIdentifier));

if (modelIdentifier[0] != '\0')
DBGLOG("dev", "got %s model from /", modelIdentifier);
else
DBGLOG("dev", "failed to get valid model from /");
}



entry->release();
} else {
DBGLOG("dev", "failed to get valid board-id property");
boardIdentifier[0] = '\0';
SYSLOG("dev", "failed to get DT root");
}

entry->release();
} else {
SYSLOG("dev", "failed to get DT root");
if (modelIdentifier[0] == '\0' || boardIdentifier[0] == '\0') {
SYSLOG("dev", "failed to obtain model information, retrying...");
IOSleep(1);
}
}

if (strstr(modelIdentifier, "Book", strlen("Book")))
modelType = WIOKit::ComputerModel::ComputerLaptop;
else
modelType = WIOKit::ComputerModel::ComputerDesktop;
}

const BaseDeviceInfo &BaseDeviceInfo::get() {
Expand All @@ -434,7 +480,6 @@ void BaseDeviceInfo::init() {
CPUInfo::init();

globalBaseDeviceInfo.updateFirmwareVendor();
globalBaseDeviceInfo.updateBootloaderVendor();
globalBaseDeviceInfo.updateModelInfo();
}

0 comments on commit 0bafc57

Please sign in to comment.