Skip to content

Commit

Permalink
Remove firmware compression
Browse files Browse the repository at this point in the history
Signed-off-by: Visual Ehrmanntraut <[email protected]>
  • Loading branch information
VisualEhrmanntraut committed Apr 18, 2024
1 parent cfad5ec commit e08319a
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 87 deletions.
8 changes: 0 additions & 8 deletions NootedRed.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

/* Begin PBXBuildFile section */
1C748C2D1C21952C0024EED2 /* PluginStart.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1C748C2C1C21952C0024EED2 /* PluginStart.cpp */; };
4002B2682AE478AA00B40D3B /* ZlibAlloc.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4002B2662AE478AA00B40D3B /* ZlibAlloc.cpp */; };
4002B2692AE478AA00B40D3B /* ZlibAlloc.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 4002B2672AE478AA00B40D3B /* ZlibAlloc.hpp */; };
40364DB629B79DFD0070A2B4 /* Model.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 40364DB529B79DFD0070A2B4 /* Model.hpp */; };
4068898B2A229BF600028D22 /* PatcherPlus.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 406889892A229BF600028D22 /* PatcherPlus.cpp */; };
4068898C2A229BF600028D22 /* PatcherPlus.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 4068898A2A229BF600028D22 /* PatcherPlus.hpp */; };
Expand Down Expand Up @@ -48,8 +46,6 @@
1C748C271C21952C0024EED2 /* NootedRed.kext */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = NootedRed.kext; sourceTree = BUILT_PRODUCTS_DIR; };
1C748C2C1C21952C0024EED2 /* PluginStart.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = PluginStart.cpp; sourceTree = "<group>"; };
1C748C2E1C21952C0024EED2 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
4002B2662AE478AA00B40D3B /* ZlibAlloc.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = ZlibAlloc.cpp; sourceTree = "<group>"; };
4002B2672AE478AA00B40D3B /* ZlibAlloc.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = ZlibAlloc.hpp; sourceTree = "<group>"; };
40364DB529B79DFD0070A2B4 /* Model.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = Model.hpp; sourceTree = "<group>"; };
403E67BD2AA7356D00F43943 /* psp_hdcp.bin */ = {isa = PBXFileReference; lastKnownFileType = archive.macbinary; path = psp_hdcp.bin; sourceTree = "<group>"; };
403E67BE2AA7356D00F43943 /* psp_dtm.bin */ = {isa = PBXFileReference; lastKnownFileType = archive.macbinary; path = psp_dtm.bin; sourceTree = "<group>"; };
Expand Down Expand Up @@ -185,8 +181,6 @@
40FC5FE029BF9E2500367F9D /* X6000.hpp */,
40FC5FD329BF995000367F9D /* X6000FB.cpp */,
40FC5FD429BF995000367F9D /* X6000FB.hpp */,
4002B2662AE478AA00B40D3B /* ZlibAlloc.cpp */,
4002B2672AE478AA00B40D3B /* ZlibAlloc.hpp */,
);
path = NootedRed;
sourceTree = "<group>";
Expand Down Expand Up @@ -302,7 +296,6 @@
CEA03B5F20EE825A00BA842F /* NRed.hpp in Headers */,
40FC5FD629BF995000367F9D /* X6000FB.hpp in Headers */,
40FC5FDE29BF996900367F9D /* HWLibs.hpp in Headers */,
4002B2692AE478AA00B40D3B /* ZlibAlloc.hpp in Headers */,
4068898C2A229BF600028D22 /* PatcherPlus.hpp in Headers */,
6C1B36672A407C6200B184DD /* AppleGFXHDA.hpp in Headers */,
);
Expand Down Expand Up @@ -411,7 +404,6 @@
408F2020288ACBE6002EEC15 /* Firmware.cpp in Sources */,
4068898B2A229BF600028D22 /* PatcherPlus.cpp in Sources */,
40FC5FE129BF9E2500367F9D /* X6000.cpp in Sources */,
4002B2682AE478AA00B40D3B /* ZlibAlloc.cpp in Sources */,
CEA03B5E20EE825A00BA842F /* NRed.cpp in Sources */,
CE405ED91E4A080700AA0B3D /* plugin_start.cpp in Sources */,
40FC5FD929BF995E00367F9D /* X5000.cpp in Sources */,
Expand Down
31 changes: 4 additions & 27 deletions NootedRed/Firmware.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,25 @@
//! See LICENSE for details.

#pragma once
#include "ZlibAlloc.hpp"
#include <Headers/kern_util.hpp>
#include <libkern/c++/OSData.h>
#include <libkern/zlib.h>

struct FWDescriptor {
const char *name;
const UInt8 *data;
const UInt32 compressedSize;
const UInt32 uncompressedSize;
const UInt32 length;
};

#define FIRMWARE(name_, data_, compressedSize_, uncompressedSize_) \
.name = name_, .data = data_, .compressedSize = compressedSize_, .uncompressedSize = uncompressedSize_

#define FIRMWARE(name_, data_, length_) .name = name_, .data = data_, .length = length_
extern const struct FWDescriptor firmware[];
extern const size_t firmwareCount;

struct Firmware {
UInt8 *data;
UInt32 size;
};

inline const Firmware getFWByName(const char *name) {
inline const FWDescriptor &getFWByName(const char *name) {
for (size_t i = 0; i < firmwareCount; i++) {
if (strcmp(firmware[i].name, name)) { continue; }

UInt32 size = firmware[i].uncompressedSize;
auto *dest = reinterpret_cast<Bytef *>(IOMallocZero(size));
z_stream stream = {
.next_in = const_cast<Bytef *>(reinterpret_cast<const Bytef *>(firmware[i].data)),
.avail_in = firmware[i].compressedSize,
.next_out = dest,
.avail_out = size,
.zalloc = ZLibAlloc,
.zfree = ZLibFree,
};
PANIC_COND(inflateInit(&stream) != Z_OK, "FW", "Failed to initialise inflate stream");
auto err = inflate(&stream, Z_FINISH);
PANIC_COND(err != Z_STREAM_END, "FW", "Failed to decompress '%s': %d", name, err);
SYSLOG_COND(inflateEnd(&stream) != Z_OK, "FW", "Failed to end inflate stream");
return Firmware {.data = dest, .size = firmware[i].uncompressedSize};
return firmware[i];
}
PANIC("FW", "'%s' not found", name);
}
17 changes: 7 additions & 10 deletions NootedRed/HWLibs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,23 +270,21 @@ void X5000HWLibs::wrapPopulateFirmwareDirectory(void *that) {
bool isRenoirDerivative = NRed::callback->chipType >= ChipType::Renoir;

auto *filename = isRenoirDerivative ? "ativvaxy_nv.dat" : "ativvaxy_rv.dat";
const auto vcnFW = getFWByName(filename);
const auto &vcnFW = getFWByName(filename);
DBGLOG("HWLibs", "VCN firmware filename is %s", filename);

//! VCN 2.2, VCN 1.0
auto *fw = callback->orgCreateFirmware(vcnFW.data, vcnFW.size, isRenoirDerivative ? 0x0202 : 0x0100, filename);
auto *fw = callback->orgCreateFirmware(vcnFW.data, vcnFW.length, isRenoirDerivative ? 0x0202 : 0x0100, filename);
PANIC_COND(!fw, "HWLibs", "Failed to create '%s' firmware", filename);
//! Can't deallocate, I think. Too bad!
auto *fwDir = getMember<void *>(that, getKernelVersion() > KernelVersion::BigSur ? 0xB0 : 0xB8);
PANIC_COND(!callback->orgPutFirmware(fwDir, 6, fw), "HWLibs", "Failed to insert '%s' firmware", filename);
}

bool X5000HWLibs::wrapGetIpFw(void *that, UInt32 ipVersion, char *name, void *out) {
if (!strncmp(name, "ativvaxy_rv.dat", 16) || !strncmp(name, "ativvaxy_nv.dat", 16)) {
const auto fwDesc = getFWByName(name);
const auto &fwDesc = getFWByName(name);
getMember<const void *>(out, 0x0) = fwDesc.data;
getMember<UInt32>(out, 0x8) = fwDesc.size;
//! Can't deallocate, too bad!
getMember<UInt32>(out, 0x8) = fwDesc.length;
return true;
}
return FunctionCast(wrapGetIpFw, callback->orgGetIpFw)(that, ipVersion, name, out);
Expand Down Expand Up @@ -528,10 +526,9 @@ CAILResult X5000HWLibs::wrapPspCmdKmSubmit(void *ctx, void *cmd, void *param3, v
return FunctionCast(wrapPspCmdKmSubmit, callback->orgPspCmdKmSubmit)(ctx, cmd, param3, param4);
}

const auto fw = getFWByName(filename);
memcpy(data, fw.data, fw.size);
getMember<UInt32>(cmd, 0xC) = fw.size;
IOFree(fw.data, fw.size);
const auto &fw = getFWByName(filename);
memcpy(data, fw.data, fw.length);
getMember<UInt32>(cmd, 0xC) = fw.length;

return FunctionCast(wrapPspCmdKmSubmit, callback->orgPspCmdKmSubmit)(ctx, cmd, param3, param4);
}
Expand Down
11 changes: 5 additions & 6 deletions NootedRed/NRed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,20 +144,19 @@ void NRed::processPatcher(KernelPatcher &patcher) {

if ((lilu.getRunMode() & LiluAPI::RunningInstallerRecovery) || checkKernelArgument("-CKFBOnly")) { return; }

const auto driversXML = getFWByName("Drivers.xml");
auto *dataNull = new char[driversXML.size + 1];
memcpy(dataNull, driversXML.data, driversXML.size);
dataNull[driversXML.size] = 0;
const auto &driversXML = getFWByName("Drivers.xml");
auto *dataNull = new char[driversXML.length + 1];
memcpy(dataNull, driversXML.data, driversXML.length);
dataNull[driversXML.length] = 0;
OSString *errStr = nullptr;
auto *dataUnserialized = OSUnserializeXML(dataNull, driversXML.size + 1, &errStr);
auto *dataUnserialized = OSUnserializeXML(dataNull, driversXML.length + 1, &errStr);
delete[] dataNull;
PANIC_COND(!dataUnserialized, "NRed", "Failed to unserialize Drivers.xml: %s",
errStr ? errStr->getCStringNoCopy() : "Unspecified");
auto *drivers = OSDynamicCast(OSArray, dataUnserialized);
PANIC_COND(!drivers, "NRed", "Failed to cast Drivers.xml data");
PANIC_COND(!gIOCatalogue->addDrivers(drivers), "NRed", "Failed to add drivers");
OSSafeReleaseNULL(dataUnserialized);
IOFree(driversXML.data, driversXML.size);
}

OSMetaClassBase *NRed::wrapSafeMetaCast(const OSMetaClassBase *anObject, const OSMetaClass *toMeta) {
Expand Down
3 changes: 1 addition & 2 deletions NootedRed/X5000.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -323,15 +323,14 @@ void X5000::wrapGFX9SetupAndInitializeHWCapabilities(void *that) {
char filename[128] = {0};
snprintf(filename, arrsize(filename), "%s_gpu_info.bin",
NRed::callback->chipType < ChipType::Renoir ? NRed::getChipName() : "renoir");
const auto gpuInfoBin = getFWByName(filename);
const auto &gpuInfoBin = getFWByName(filename);
auto *header = reinterpret_cast<const CommonFirmwareHeader *>(gpuInfoBin.data);
auto *gpuInfo = reinterpret_cast<const GPUInfoFirmware *>(gpuInfoBin.data + header->ucodeOff);

setHWCapability<UInt32>(that, HWCapability::SECount, gpuInfo->gcNumSe);
setHWCapability<UInt32>(that, HWCapability::SHPerSE, gpuInfo->gcNumShPerSe);
setHWCapability<UInt32>(that, HWCapability::CUPerSH, gpuInfo->gcNumCuPerSh);

IOFree(gpuInfoBin.data, gpuInfoBin.size);
FunctionCast(wrapGFX9SetupAndInitializeHWCapabilities, callback->orgGFX9SetupAndInitializeHWCapabilities)(that);
}

Expand Down
19 changes: 0 additions & 19 deletions NootedRed/ZlibAlloc.cpp

This file was deleted.

9 changes: 0 additions & 9 deletions NootedRed/ZlibAlloc.hpp

This file was deleted.

8 changes: 2 additions & 6 deletions Scripts/GenerateFirmware.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/python3

import zlib
import os
import struct
import sys
Expand All @@ -20,8 +19,6 @@ def format_file_name(file_name):
def lines_for_file(path, file):
with open(path, "rb") as src_file:
src_data = src_file.read()
src_len_uncompressed = len(src_data)
src_data = zlib.compress(src_data)
src_len = len(src_data)

lines: list[str] = []
Expand All @@ -43,8 +40,7 @@ def lines_for_file(path, file):
.format(*struct.unpack("BBBBBBBBBBBBBBBB", block)))
return lines + [
"};\n",
f"const UInt32 {fw_var_name}_compressedSize = sizeof({fw_var_name});\n",
f"const UInt32 {fw_var_name}_uncompressedSize = {src_len_uncompressed};\n",
f"const UInt32 {fw_var_name}_size = sizeof({fw_var_name});\n",
]


Expand All @@ -58,7 +54,7 @@ def process_files(target_file, dir):
lines += lines_for_file(os.path.join(root, file), file)
fw_var_name = format_file_name(file)
file_list_content += [
f" {{FIRMWARE(\"{file}\", {fw_var_name}, {fw_var_name}_compressedSize, {fw_var_name}_uncompressedSize)}},\n"]
f" {{FIRMWARE(\"{file}\", {fw_var_name}, {fw_var_name}_size)}},\n"]

lines += ["\n", "const struct FWDescriptor firmware[] = {\n"]
lines += file_list_content
Expand Down

0 comments on commit e08319a

Please sign in to comment.