Add Skip value in ACPI DSDT patches.

This commit is contained in:
jief666 2022-04-14 18:07:46 +02:00
parent f18a5127ad
commit 562ffe5ff0
5 changed files with 62 additions and 24 deletions

View File

@ -502,11 +502,15 @@ void PatchAllTables()
if ( gSettings.ACPI.DSDT.DSDTPatchArray[i].PatchDsdtTgt.isEmpty() ) { if ( gSettings.ACPI.DSDT.DSDTPatchArray[i].PatchDsdtTgt.isEmpty() ) {
Len = FixAny((UINT8*)NewTable, Len, Len = FixAny((UINT8*)NewTable, Len,
gSettings.ACPI.DSDT.DSDTPatchArray[i].PatchDsdtFind, gSettings.ACPI.DSDT.DSDTPatchArray[i].PatchDsdtFind,
gSettings.ACPI.DSDT.DSDTPatchArray[i].PatchDsdtReplace); gSettings.ACPI.DSDT.DSDTPatchArray[i].PatchDsdtReplace,
gSettings.ACPI.DSDT.DSDTPatchArray[i].Skip);
//DBG(" OK\n"); //DBG(" OK\n");
}else{ }else{
//DBG("Patching: renaming in bridge\n"); //DBG("Patching: renaming in bridge\n");
Len = FixRenameByBridge2((UINT8*)NewTable, Len, gSettings.ACPI.DSDT.DSDTPatchArray[i].PatchDsdtTgt, gSettings.ACPI.DSDT.DSDTPatchArray[i].PatchDsdtFind, gSettings.ACPI.DSDT.DSDTPatchArray[i].PatchDsdtReplace); Len = FixRenameByBridge2((UINT8*)NewTable, Len, gSettings.ACPI.DSDT.DSDTPatchArray[i].PatchDsdtTgt,
gSettings.ACPI.DSDT.DSDTPatchArray[i].PatchDsdtFind,
gSettings.ACPI.DSDT.DSDTPatchArray[i].PatchDsdtReplace,
gSettings.ACPI.DSDT.DSDTPatchArray[i].Skip);
} }
} }
} }

View File

@ -1773,9 +1773,9 @@ UINT32 FixADP1 (UINT8* dsdt, UINT32 len)
return len; return len;
} }
UINT32 FixAny (UINT8* dsdt, UINT32 len, const XBuffer<UINT8>& ToFind, const XBuffer<UINT8>& ToReplace) UINT32 FixAny (UINT8* dsdt, UINT32 len, const XBuffer<UINT8> ToFind, const XBuffer<UINT8>& ToReplace, uint64_t Skip)
{ {
size_t sizeoffset; INT32 sizeoffset = 0; // Initialization just to silence warning.
INT32 adr; INT32 adr;
UINT32 i; UINT32 i;
XBool found = false; XBool found = false;
@ -1788,11 +1788,21 @@ UINT32 FixAny (UINT8* dsdt, UINT32 len, const XBuffer<UINT8>& ToFind, const XBuf
MsgLog(" the patch is too large!\n"); MsgLog(" the patch is too large!\n");
return len; return len;
} }
sizeoffset = ToReplace.size() - ToFind.size(); if (
if ( sizeoffset > MAX_INT32 ) { (ToReplace.size() >= ToFind.size() && ToReplace.size() - ToFind.size() > MAX_INT32) ||
DBG(" invalid patches (sizeoffset > MAX_INT32)!\n"); (ToReplace.size() < ToFind.size() && ToFind.size() - ToReplace.size() > MAX_INT32) ) {
MsgLog(" invalid patch (sizeoffset > MAX_INT32)!\n");
return len; return len;
} }
if ( ToReplace.size() >= ToFind.size() ) {
sizeoffset = (INT32)(ToReplace.size() - ToFind.size()); // Safe cast because of earlier check
}
if ( ToFind.size() > ToReplace.size() ) {
// I know that, on little endian machine, substracting 2 unsigned and assign it in a signed var would work.
// But it's good practice to not rely on the architecture.
sizeoffset = (INT32)(ToFind.size() - ToReplace.size()); // Safe cast because of earlier check
sizeoffset = -sizeoffset;
}
for (i = 20; i < len; ) { for (i = 20; i < len; ) {
adr = FindBin(dsdt + i, len - i, ToFind); adr = FindBin(dsdt + i, len - i, ToFind);
if (adr < 0) { if (adr < 0) {
@ -1811,42 +1821,53 @@ UINT32 FixAny (UINT8* dsdt, UINT32 len, const XBuffer<UINT8>& ToFind, const XBuf
// MsgLog(" (%X)", adr); // MsgLog(" (%X)", adr);
found = true; found = true;
len = move_data(adr + i, dsdt, len, (INT32)sizeoffset); // safe cast, sizeoffset < MAX_INT32 if ( Skip == 0 )
{
len = move_data(adr + i, dsdt, len, sizeoffset);
CopyMem(dsdt + adr + i, ToReplace.data(), ToReplace.size()); CopyMem(dsdt + adr + i, ToReplace.data(), ToReplace.size());
len = CorrectOuterMethod(dsdt, len, adr + i - 2, (INT32)sizeoffset); // safe cast, sizeoffset < MAX_INT32 len = CorrectOuterMethod(dsdt, len, adr + i - 2, sizeoffset);
len = CorrectOuters(dsdt, len, adr + i - 3, (INT32)sizeoffset); // safe cast, sizeoffset < MAX_INT32 len = CorrectOuters(dsdt, len, adr + i - 3, sizeoffset);
i += (UINT32)(adr + ToReplace.size()); // if there is no bug before, it should be safe cast. i += (UINT32)(adr + ToReplace.size()); // if there is no bug before, it should be safe cast.
}else{
MsgLog("Skip i=%d ",i);
i += (UINT32)(adr + ToFind.size()); // if there is no bug before, it should be safe cast.
Skip--;
}
} }
MsgLog(" ]\n"); //should not be here MsgLog(" ]\n"); //should not be here
return len; return len;
} }
//new method. by goodwin_c //new method. by goodwin_c
UINT32 FixRenameByBridge2 (UINT8* dsdt, UINT32 len, const XBuffer<UINT8>& TgtBrgName, const XBuffer<UINT8>& ToFind, const XBuffer<UINT8>& ToReplace) UINT32 FixRenameByBridge2(UINT8* dsdt, UINT32 len, const XBuffer<UINT8>& TgtBrgName, const XBuffer<UINT8>& ToFind, const XBuffer<UINT8>& ToReplace, uint64_t Skip)
{ {
INT32 adr; INT32 adr;
XBool found = false; XBool found = false;
UINT32 i, k;
UINT32 BrdADR = 0, BridgeSize; UINT32 BrdADR = 0, BridgeSize;
if ( ToFind.isEmpty() || ToReplace.isEmpty() ) { if ( ToFind.isEmpty() || ToReplace.isEmpty() ) {
DBG(" invalid patches!\n"); MsgLog(" invalid patch!\n");
return len; return len;
} }
if (ToFind.size() != ToReplace.size()) { if (ToFind.size() != ToReplace.size()) {
DBG(" find/replace different size!\n"); MsgLog(" find/replace different size!\n");
return len; return len;
} }
DBG(" pattern %02hhX%02hhX%02hhX%02hhX,", ToFind[0], ToFind[1], ToFind[2], ToFind[3]); MsgLog(" pattern %02hhX%02hhX%02hhX%02hhX,", ToFind[0], ToFind[1], ToFind[2], ToFind[3]);
if ( TgtBrgName.notEmpty() ) {
MsgLog("TgtBridge ");
for (size_t i=0 ; i<TgtBrgName.size() ; ++i) MsgLog("%02hhX", TgtBrgName[i]);
MsgLog(", ");
}
if ((ToFind.size() + sizeof(EFI_ACPI_DESCRIPTION_HEADER)) > len) { if ((ToFind.size() + sizeof(EFI_ACPI_DESCRIPTION_HEADER)) > len) {
DBG(" the patch is too large!\n"); DBG(" the patch is too large!\n");
return len; return len;
} }
DBG("Start ByBridge Rename Fix\n"); DBG("Start ByBridge Rename Fix\n");
for (i=0x20; len >= 10 && i < len - 10; i++) { for (UINT32 i=0x20; len >= 10 && i < len - 10; i++) {
if (CmpDev(dsdt, i, TgtBrgName)) { if (CmpDev(dsdt, i, TgtBrgName)) {
BrdADR = devFind(dsdt, i); BrdADR = devFind(dsdt, i);
if (!BrdADR) { if (!BrdADR) {
@ -1856,7 +1877,7 @@ UINT32 FixRenameByBridge2 (UINT8* dsdt, UINT32 len, const XBuffer<UINT8>& TgtBrg
if(!BridgeSize) continue; if(!BridgeSize) continue;
if(BridgeSize <= ToFind.size()) continue; if(BridgeSize <= ToFind.size()) continue;
k = 0; UINT32 k = 0;
found = false; found = false;
while (k <= 100) { while (k <= 100) {
adr = FindBin(dsdt + BrdADR, BridgeSize, ToFind); adr = FindBin(dsdt + BrdADR, BridgeSize, ToFind);
@ -1876,7 +1897,12 @@ UINT32 FixRenameByBridge2 (UINT8* dsdt, UINT32 len, const XBuffer<UINT8>& TgtBrg
DBG(" (%X)", adr); DBG(" (%X)", adr);
found = true; found = true;
if ( ToReplace.notEmpty() ) { if ( ToReplace.notEmpty() ) {
if ( Skip == 0 ) {
CopyMem(dsdt + BrdADR + adr, ToReplace.data(), ToReplace.size()); CopyMem(dsdt + BrdADR + adr, ToReplace.data(), ToReplace.size());
} else {
MsgLog("Skip adr=%d ",adr);
Skip--;
}
} }
k++; k++;
} }
@ -5388,14 +5414,16 @@ void FixBiosDsdt(UINT8* temp, EFI_ACPI_2_0_FIXED_ACPI_DESCRIPTION_TABLE* fadt, c
if (gSettings.ACPI.DSDT.DSDTPatchArray[i].PatchDsdtTgt.isEmpty()) { if (gSettings.ACPI.DSDT.DSDTPatchArray[i].PatchDsdtTgt.isEmpty()) {
DsdtLen = FixAny(temp, DsdtLen, DsdtLen = FixAny(temp, DsdtLen,
gSettings.ACPI.DSDT.DSDTPatchArray[i].PatchDsdtFind, gSettings.ACPI.DSDT.DSDTPatchArray[i].PatchDsdtFind,
gSettings.ACPI.DSDT.DSDTPatchArray[i].PatchDsdtReplace); gSettings.ACPI.DSDT.DSDTPatchArray[i].PatchDsdtReplace,
gSettings.ACPI.DSDT.DSDTPatchArray[i].Skip);
}else{ }else{
// DBG("Patching: renaming in bridge\n"); // DBG("Patching: renaming in bridge\n");
DsdtLen = FixRenameByBridge2(temp, DsdtLen, DsdtLen = FixRenameByBridge2(temp, DsdtLen,
gSettings.ACPI.DSDT.DSDTPatchArray[i].PatchDsdtTgt, gSettings.ACPI.DSDT.DSDTPatchArray[i].PatchDsdtTgt,
gSettings.ACPI.DSDT.DSDTPatchArray[i].PatchDsdtFind, gSettings.ACPI.DSDT.DSDTPatchArray[i].PatchDsdtFind,
gSettings.ACPI.DSDT.DSDTPatchArray[i].PatchDsdtReplace); gSettings.ACPI.DSDT.DSDTPatchArray[i].PatchDsdtReplace,
gSettings.ACPI.DSDT.DSDTPatchArray[i].Skip);
} }
} else { } else {

View File

@ -50,8 +50,8 @@ FindBin (
INT32 FindBin (UINT8 *dsdt, size_t len, const XBuffer<UINT8>& bin); INT32 FindBin (UINT8 *dsdt, size_t len, const XBuffer<UINT8>& bin);
UINT32 FixAny (UINT8* dsdt, UINT32 len, const XBuffer<UINT8>& ToFind, const XBuffer<UINT8>& ToReplace); UINT32 FixAny (UINT8* dsdt, UINT32 len, const XBuffer<UINT8> ToFind, const XBuffer<UINT8>& ToReplace, uint64_t Skip);
UINT32 FixRenameByBridge2 (UINT8* dsdt, UINT32 len, const XBuffer<UINT8>& TgtBrgName, const XBuffer<UINT8>& ToFind, const XBuffer<UINT8>& ToReplace); UINT32 FixRenameByBridge2 (UINT8* dsdt, UINT32 len, const XBuffer<UINT8>& TgtBrgName, const XBuffer<UINT8>& ToFind, const XBuffer<UINT8>& ToReplace, uint64_t Skip);
#endif /* PLATFORM_FIXBIOSDSDT_H_ */ #endif /* PLATFORM_FIXBIOSDSDT_H_ */

View File

@ -836,6 +836,7 @@ public:
XBuffer<UINT8> PatchDsdtReplace = XBuffer<UINT8>(); XBuffer<UINT8> PatchDsdtReplace = XBuffer<UINT8>();
XBuffer<UINT8> PatchDsdtTgt = XBuffer<UINT8>(); XBuffer<UINT8> PatchDsdtTgt = XBuffer<UINT8>();
INPUT_ITEM PatchDsdtMenuItem = INPUT_ITEM(); // Not read from config.plist. Should be moved out. INPUT_ITEM PatchDsdtMenuItem = INPUT_ITEM(); // Not read from config.plist. Should be moved out.
uint64_t Skip = 0;
#if __cplusplus > 201703L #if __cplusplus > 201703L
XBool operator == (const DSDT_Patch&) const = default; XBool operator == (const DSDT_Patch&) const = default;
@ -848,6 +849,7 @@ public:
if ( !(PatchDsdtReplace == other.PatchDsdtReplace) ) return false; if ( !(PatchDsdtReplace == other.PatchDsdtReplace) ) return false;
if ( !(PatchDsdtTgt == other.PatchDsdtTgt) ) return false; if ( !(PatchDsdtTgt == other.PatchDsdtTgt) ) return false;
if ( !(PatchDsdtMenuItem == other.PatchDsdtMenuItem) ) return false; if ( !(PatchDsdtMenuItem == other.PatchDsdtMenuItem) ) return false;
if ( !(Skip == other.Skip) ) return false;
return true; return true;
} }
void takeValueFrom(const ConfigPlistClass::ACPI_Class::DSDT_Class::ACPI_DSDT_Patch_Class& other) void takeValueFrom(const ConfigPlistClass::ACPI_Class::DSDT_Class::ACPI_DSDT_Patch_Class& other)
@ -858,6 +860,7 @@ public:
PatchDsdtReplace = other.dgetPatchDsdtReplace(); PatchDsdtReplace = other.dgetPatchDsdtReplace();
PatchDsdtTgt = other.dgetPatchDsdtTgt(); PatchDsdtTgt = other.dgetPatchDsdtTgt();
PatchDsdtMenuItem.BValue = !other.dgetDisabled(); PatchDsdtMenuItem.BValue = !other.dgetDisabled();
Skip = other.dgetSkip();
} }
}; };

View File

@ -163,13 +163,15 @@ if ( xmlPath.contains("ACPI/DSDT/Patches[15]"_XS8) ) {
XmlData Find = XmlData(); XmlData Find = XmlData();
XmlData Replace = XmlData(); XmlData Replace = XmlData();
TgtBridgeClass TgtBridge = TgtBridgeClass(); TgtBridgeClass TgtBridge = TgtBridgeClass();
XmlUInt64 Skip = XmlUInt64();
XmlDictField m_fields[5] = { XmlDictField m_fields[6] = {
{"Comment", Comment}, {"Comment", Comment},
{"Disabled", Disabled}, {"Disabled", Disabled},
{"Find", Find}, {"Find", Find},
{"Replace", Replace}, {"Replace", Replace},
{"TgtBridge", TgtBridge}, {"TgtBridge", TgtBridge},
{"Skip", Skip},
}; };
virtual void getFields(XmlDictField** fields, size_t* nb) override { *fields = m_fields; *nb = sizeof(m_fields)/sizeof(m_fields[0]); }; virtual void getFields(XmlDictField** fields, size_t* nb) override { *fields = m_fields; *nb = sizeof(m_fields)/sizeof(m_fields[0]); };
@ -180,6 +182,7 @@ if ( xmlPath.contains("ACPI/DSDT/Patches[15]"_XS8) ) {
const XBuffer<UINT8>& dgetPatchDsdtFind() const { return Find.isDefined() ? Find.value() : XBuffer<UINT8>::NullXBuffer; }; const XBuffer<UINT8>& dgetPatchDsdtFind() const { return Find.isDefined() ? Find.value() : XBuffer<UINT8>::NullXBuffer; };
const XBuffer<UINT8>& dgetPatchDsdtReplace() const { return Replace.isDefined() ? Replace.value() : XBuffer<UINT8>::NullXBuffer; }; const XBuffer<UINT8>& dgetPatchDsdtReplace() const { return Replace.isDefined() ? Replace.value() : XBuffer<UINT8>::NullXBuffer; };
const XBuffer<UINT8>& dgetPatchDsdtTgt() const { return TgtBridge.isDefined() ? TgtBridge.value() : XBuffer<UINT8>::NullXBuffer; }; const XBuffer<UINT8>& dgetPatchDsdtTgt() const { return TgtBridge.isDefined() ? TgtBridge.value() : XBuffer<UINT8>::NullXBuffer; };
const decltype(Skip)::ValueType& dgetSkip() const { return Skip.isDefined() ? Skip.value() : Skip.nullValue; };
}; };
protected: protected: