Contribute
Register

<< Solved >> OpenCore battery patch

Status
Not open for further replies.
@KungFuJoe

Please try to boot the attached EFI, I just noticed an ultra stupid mistake I made, I can't really judge if that fixed it tho.

Got a little further this time, but hung at the apple logo with empty bar.

Just to confirm...the only file you changed this time around was config.plist?

Attaching oc log and screenshots (hope they are readable...things were scrolling pretty fast).
 

Attachments

  • screenshots.zip
    6.1 MB · Views: 40
  • opencore-2020-08-06-202959.txt
    256 KB · Views: 45
  • screenshots.zip
    6.1 MB · Views: 31
@KungFuJoe

This seems weird... Thank you for the nice images tho! To be honest here, battery patching through SSDTs is just way too error prone. I need some time to debug this.

I'm also going to try to come up with some tools that may help me on the way, just to make this waay to big and waay to repetetive process a bit simpler. Please be patient. I know it sucks if you wait for something, but I need to do this properly, since I want to do it for as many people as possible.
 
@KungFuJoe

This seems weird... Thank you for the nice images tho! To be honest here, battery patching through SSDTs is just way too error prone. I need some time to debug this.

I'm also going to try to come up with some tools that may help me on the way, just to make this waay to big and waay to repetetive process a bit simpler. Please be patient. I know it sucks if you wait for something, but I need to do this properly, since I want to do it for as many people as possible.

Absolutely no rush on this. This laptop is still very usable when I just use the patched DSDT. Just doesn't sleep (keeps waking up right away) but not a big deal because its almost always plugged in.

I appreciate all your help!
 
In advance: This is no solution, just a way to help troubleshooting I want to share.

So I wanted to make 100% sure that the method declaration renames were applied correctly (since it's a lot of searching with HexFiend - which could introduce some errors). I wrote a small Java class, which lets you transform a binary (DSDT.aml) file based on what renames are enabled within config.plist, just hand both paths to it.

The resulting DSDT has all those renames applied, so you can check it out with MaciASL. It won't compile, because all of the renamed methods are not implemented yet. But this helps you to search for the renames and make sure they got applied at the right position inside the file (and only once).

My class:
Java:
import org.apache.commons.configuration2.plist.XMLPropertyListConfiguration;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.commons.lang3.tuple.Triple;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

public class RenameExecutor {

  private final String configPath, binaryPath;
  private final List<Triple<String, byte[], byte[]>> patches;

  public RenameExecutor(String configPath, String binaryPath) {
    this.binaryPath = binaryPath;
    this.configPath = configPath;
    this.patches = new ArrayList<>();

    readPatchMap();
    patchFile();
  }

  private void patchFile() {
    try {
      FileInputStream fis = new FileInputStream(new File(this.binaryPath));
      byte[] bytes = fis.readAllBytes();
      fis.close();

      for(Triple<String, byte[], byte[]> patch : patches)
        replaceBytes(bytes, patch.getMiddle(), patch.getRight(), patch.getLeft());

      FileOutputStream fos = new FileOutputStream(this.binaryPath);
      fos.write(bytes);
      fos.close();
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  private void replaceBytes(byte[] bytes, byte[] search, byte[] replace, String comment) {
    if(search.length != replace.length)
      throw new IllegalStateException("Search and replace array must be the same size!");

    System.out.print("* Replacing <" + new String(search) + "> to <" + new String(replace) + ">, ");

    int c = 0;
    for(int i = 0; i < bytes.length - search.length + 1; i++) {
      boolean match = true;
      for(int j = 0; j < search.length; j++) {
        byte curr = bytes[i + j];
        if(curr != search[j]) {
          match = false;
          break;
        }
      }

      if(!match)
        continue;

      System.arraycopy(replace, 0, bytes, i, search.length);
      c++;
    }

    System.out.println(c + " occurrances, (" + comment + ")");
  }

  private void readPatchMap() {
    try {
      XMLPropertyListConfiguration plist = new XMLPropertyListConfiguration();
      FileReader fr = new FileReader(new File(configPath));
      plist.read(fr);
      fr.close();

      XMLPropertyListConfiguration[] arr = (XMLPropertyListConfiguration[]) plist.getArray(XMLPropertyListConfiguration.class, "ACPI.Patch");
      for(XMLPropertyListConfiguration a : arr) {
        if(!a.getBoolean("Enabled"))
          continue;

        byte[] find = (byte[]) a.getProperty("Find");
        byte[] replace = (byte[]) a.getProperty("Replace");

        patches.add(Triple.of(a.getString("Comment"), find, replace));
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

You'll need the following dependencies:
XML:
<dependencies>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-configuration2</artifactId>
        <version>2.7</version>
    </dependency>

    <dependency>
        <groupId>commons-codec</groupId>
        <artifactId>commons-codec</artifactId>
        <version>1.6</version>
    </dependency>
</dependencies>

@KungFuJoe

I will debug it further, if I find any errors, I'll fix them and send you a new version. Thank you for your patience! :)
 
I'm sorry for my English, someone can help me with my battery, I have everything working except the battery that is frozen really charge but the measured is frozen. tengo un hp pavilion 15 i7 8750h,
 

Attachments

  • DSDT.aml
    317.1 KB · Views: 41
@KungFuJoe

Please give the attached EFI a shot. Thank you!
 

Attachments

  • EFI.zip
    6.7 MB · Views: 66

Attachments

  • screenshots.zip
    15.5 MB · Views: 48
  • opencore-2020-08-08-232547.txt
    256 KB · Views: 52
@KungFuJoe

I'm actually running out of ideas on how to fix this... I don't see any other mistakes, other than what I've attached now. Please try it out again, but it probably will result in the same issues. Post a picture anyways, please - I don't want to give up on it.

What's basically the problem, AFAIK (to document this, for myself and others):
The native (untouched) Method _BST from ACPI has those calls inside of it's body:
Code:
Store (^^LPCB.EC0.BSTS (), Local0)
Store (^^LPCB.EC0.BCRT (), Local1)
Store (^^LPCB.EC0.BRCP (), Local2)
Store (^^LPCB.EC0.BVOT (), Local3)

EC0 gets replaced to EC, don't worry about that. It needs to evaluate the return values of BSTS, BCRT, BRCP and BVOT, one after the other, to store them into method-local variables.

Since your images only talk about BCRT, BSTS should be fine to call. I also overrode BSTS, and it uses the Fields declared inside the IndexField body, just like BCRT - which doesn't work...

I forgot to set the other regions to Lock instead of NoLock, which basically means that you have to wait to access the fields until they are not in use, if I understood that correctly. Maybe that will stop some kind of race-condition here?

The problem with all of this is that I don't know how IndexField works. I understand OperationRegion and it's corresponding Field assignment block, so I know how to override it. Maybe it's a whole different story with IndexField, but it's so damn hard to find informations about it :(.
 

Attachments

  • EFI.zip
    6.7 MB · Views: 65
Note to self:

I tried to debug further and came accross something I don't understand...
If you run iasl -da -dl DSDT.aml, you can disassemble it. Running iasl -l DSDT.dsl on the result should give a DSDT.lst file, which basically contains the code & the assembled bytes, I wanted to use it to check if the search/replace are correct.

After running that command, it changed the actual binary (DSDT.aml) and the hex method signatures of FBST and WMNB changed.

WTH? Seriously confused now.
 
Status
Not open for further replies.
Back
Top