YoyPa / isw

Ice-Sealed Wyvern
GNU General Public License v3.0
385 stars 78 forks source link

Battery Calibration Tool #249

Open etiennewan opened 2 years ago

etiennewan commented 2 years ago

When the battery calibration tool is launched from Dragon Center, it is able to detect if the AC adapter is plugged in or not and to let the battery discharge even with the AC adapter plugged in.

My goal would be to reproduce this behavior with isw to have more options to preserve battery life coupled with the -t threshold option that could be enforced with a voluntary battery discharge.

I think I need to learn more about how this threshold internally works because the battery reports to be fully charged (between 90% and 100%) in Linux with -t 60 set. My laptop is a GS63-8RE getting a replacement battery soon, my current battery seems to be defective and may alter its behavior with the -t option or the battery manager. I disabled Fast Charging in the EFI to be sure.

These features of the battery calibration tool do not seem to be related with the 0xEF address in memory.

I tried to look in the EC memory for other addresses that could be related while running the battery calibration tool but am not sure if this EC related.

Do you have any method to pinpoint interesting addresses and start experimenting from there?

etiennewan commented 2 years ago

Linking https://github.com/YoyPa/isw/issues/41 and https://github.com/YoyPa/isw/issues/192

etiennewan commented 2 years ago

Experimenting on my GS63 8RE while running Windows 10, I notice address 0xC6 in EC RAM and am rather confident saying that

Address 0xC7 stayed 00h during all my observations.

Address 0xC8 is mostly value 00h (AC adapter unplugged, or charging above 80%? battery charge), but value 01h can be seen when charging from below 80% with sometimes value 80h briefly for less than a second (sometimes 81h that looks like a transition between 80h and 01h). I tried to write 01h when it was 00h while charging above 80%, it immediately was rewritten to 00h and it triggered seconds later a graceful forced Windows shutdown. I do not have any idea yet what this value is, but what happened may be related to an ACPI trigger?

Address 0xC9 is mostly value 06h when AC adapter is unplugged, and there are multiple values when AC adapter is plugged in (ranging in 06h-6Fh according to what I observed). I guess these values reflect the battery voltage.

I will update this message when I get through the MSI Battery Calibration to see if this tool has anything to do with addresses 0xC6-0xC9.

EDIT: the MSI Battery Calibration tool seems to be essentially a wrapper to write 01h to address 0xCF with some security measures: AC adapter is plugged in (= checks if 0xC6=C0h), begin only when battery is fully charged (might need to disassemble the program to check if it reads the EC for the battery level or interact with a Windows API)...

Writing manually 01h to 0xCF works and tells the laptop to discharge even with the AC adapter plugged in. Other addresses gets updated following this, especially 0xC6 is forced to have value 40h (I guess that is why the tool cannot detect anymore the AC adapter is unplugged during the discharge phase), and 0xC9 returns to value 06h like observed before. Writing back 00h to 0xCF lets the battery charge again. For a few seconds, Windows can tell there is no battery detected. However, the battery LED is still blinking until a reboot is made.