* [Important update (read me first!)](#important=update)
* [Purpose](#purpose)
   * [Software needed](#software-needed)
* [Overview of steps](#overview-of-steps)
* [Detailed explanation of steps](#detailed-explanation-of-steps)
   * [Step 1: disable S0](#step-1-disable-s0)
   * [Step 2: install Clover to ESP](#step-2-install-clover-to-esp)
   * [Step 3: disable Secure boot](#step-3-disable-secure-boot)
   * [Step 4: set Clover as default](#step-4-set-clover-as-default)
   * [Step 5: dump ACPI](#step-5-dump-acpi)
   * [Step 6: patch DSDT](#step-6-patch-dsdt)
   * [Step 7: install new DSDT](#step-7-install-new-dsdt)
   * [Step 8: configure Clover](#step-8-configure-clover)
   * [Step 9: Clover themes](#step-9-clover-themes)
* [Troubleshooting](#troubleshooting)
   * [Battery missing](#battery-missing)
   * [BSOD upon wakeup](#bsod-upon-wakeup)
   * [Flashing lights while sleeping](#flashing-lights-while-sleeping)
   * [Reverting changes](#reverting-changes)
* [Future work](#future-work)

<!-- Created by https://github.com/ekalinin/github-markdown-toc -->
# Important update
A much simpler method was described by @nullsvm in [this comment](https://gist.github.com/raenye/d6645d7039a6136ccfb055e0f8517698?permalink_comment_id=4391477#gistcomment-4391477), which involves modifying a UEFI variable directly using the `RU.EFI` BIOS editor.
It worked for me on X13 2021 (bios versions 412, 413, 414) and for some others ([here](https://gist.github.com/raenye/d6645d7039a6136ccfb055e0f8517698?permalink_comment_id=4394317#gistcomment-4394317) and [here](https://gist.github.com/raenye/d6645d7039a6136ccfb055e0f8517698?permalink_comment_id=4579899#gistcomment-4579899)) on X13 2022, but some 2022 owners report it doesn't work for them (see comments).

I'm keeping the original text here even though I'm not using the Clover method anymore; IMHO you should try the `RU.EFI` route before using Clover.

# Purpose
This gist documents a workaround I'm using on my ASUS GV301QE (X13 flow 2021) to replace *S0 sleep* (AKA _modern standby_ or _S0ix_) by *S3 sleep* (AKA _deep sleep_ or _suspend-to-RAM_).

![screenshot showing the output of powercfg /a](https://user-images.githubusercontent.com/59210462/190847583-decbd0a5-7b69-4c44-8c44-0852c44ced62.png)

This involves modifying one of the ACPI tables, called the DSDT. The modification is not permanent - nothing is written to the firmware itself - but rather through a bootloader that loads the modified DSDT before Windows starts.

I chose to go through [Clover EFI bootloader](https://github.com/CloverHackyColor/CloverBootloader); the same can be achieved on Linux via an initrd (see, e.g., [here](https://github.com/nvllsvm/asus-flow-x13-linux)). Clover's main aim is to enable running MacOS on non-Apple systems, but we only care about its ability to load custom ACPI tables before booting Windows.

## Software needed
- [Clover](https://github.com/CloverHackyColor/CloverBootloader/releases/) - `CLOVERX64.efi.zip` is enough, I used r5149.
- [IASL compiler](https://www.acpica.org/downloads/binary-tools) - we only need `iasl.exe`

I assume you downloaded these and extracted the files `CLOVERX64.efi` and `iasl.exe` to `C:\Users\YourUserName\Downloads`.
Replace `YourUserName` here and in the instructions below with your actual user name.

# Overview of steps
1. Disable S0 sleep via Windows registry.[^s0s3]
2. Install Clover to the [ESP](https://en.wikipedia.org/wiki/EFI_system_partition).
3. Disable Secure boot in UEFI setup.
4. Set Clover as the default boot option in UEFI setup.
5. Dump current DSDT using Clover.[^dump]
6. Patch DSDT to enable S3 sleep using `iasl`.
7. Place patched DSDT in Clover's directory within the ESP.
8. Configure Clover to automatically load Windows upon reboot.
9. Optional: install a Clover theme.

With this order of steps, only two reboots are necessary :)

[^s0s3]: This is needed since S0 takes precedence over S3 when both are available.
[^dump]: Dumping must be done with Clover, since using `acpidump.exe` will get you a partially valid DSDT. Ask me how I know :P

# Detailed explanation of steps
## Step 1: disable S0
Run Command Prompt as Admin.
![image](https://user-images.githubusercontent.com/59210462/190849399-41e5c4f4-16ea-4185-abc0-947fb1e16b72.png)

To disable S0 sleep, type in the Admin Command Prompt:
```
reg add HKLM\System\CurrentControlSet\Control\Power /v PlatformAoAcOverride /t REG_DWORD /d 0
```
Keep Command Prompt open for the next step.

## Step 2: install Clover to ESP
Mount ESP:
```
mountvol R: /s
```
Create directory structure for Clover:
```
mkdir R:\EFI\CLOVER\ACPI\Windows
mkdir R:\EFI\CLOVER\ACPI\origin
```
Install Clover:
```
copy C:\Users\YourUserName\Downloads\CLOVERX64.efi R:\EFI\CLOVER
```

~~Clover works fine even without a configuration file, but we'll install a basic one in Step 8.~~

Edit: `ACPI_BIOS_ERROR` BSODs are reported with an empty configuration file, let's do it now.

Create a basic configuration file for Clover:
```
notepad R:\EFI\CLOVER\config.plist
```
Copy and paste the following in it:
```xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>Boot</key>
	<dict>
		<key>Timeout</key>
		<integer>-1</integer>
		<key>Fast</key>
		<false/>		
	</dict>
	<key>GUI</key>
	<dict>
		<key>Scan</key>
		<dict>
			<key>Entries</key>
			<true/>
			<key>Legacy</key>
			<false/>
		</dict>
	</dict>
</dict>
</plist>
```

## Step 3: disable Secure boot
Restart the computer, and press `Esc` to show Boot Menu.
Enter UEFI Setup (last option).
![image](https://user-images.githubusercontent.com/59210462/190853527-7fa380d5-285e-4690-83f2-2e53ed82080e.png)

Press F7 to switch to Advanced Mode.
From the Security tab, disable Secure Boot if it is enabled:
![image](https://user-images.githubusercontent.com/59210462/190853422-8207e391-421a-4ca1-89af-69192c0e43ed.png)
![image](https://user-images.githubusercontent.com/59210462/190853360-e08f23a5-426b-43ef-9b0d-3cb0fea88932.png)

Keep UEFI Setup open for the next step.

## Step 4: set Clover as default
From the Boot tab, add a boot option for Clover:
![image](https://user-images.githubusercontent.com/59210462/190853495-a112b2ad-73c0-4cc0-937d-2ee1c126220e.png)

Select Clover as Boot option #1:
![image](https://user-images.githubusercontent.com/59210462/190853476-255dcf0d-4aa1-4b6a-b9c6-98071dc4335e.png)

Save and exit UEFI Setup, Clover should boot.
![image](https://user-images.githubusercontent.com/59210462/190853614-720f468b-e110-40b3-8ad7-6df7764af428.png)

## Step 5: dump ACPI
Press F4 to dump ACPI tables.
There is no visual feedback, but Clover saves them to `EFI\CLOVER\ACPI\origin`.

Boot Windows ("Microsoft EFI Boot", usually leftmost option).

## Step 6: patch DSDT
Run Command Prompt as Admin.

To verify that S0 is disabled, type:
```
powercfg /a
```
![image](https://user-images.githubusercontent.com/59210462/190853093-486651df-ed05-4b72-9222-e97264d7251f.png)

The important line is
>     Standby (S0 Low Power Idle)
>        The system firmware does not support this standby state.
        
Mount ESP and copy original DSDT to working directory:
```
cd \Users\YourUserName\Downloads
mountvol R: /s
copy R:\EFI\CLOVER\ACPI\origin\DSDT.aml .
```

Decompile DSDT using IASL and edit it using notepad:
```
iasl DSDT.aml
notepad DSDT.dsl
```

Two changes are required: bump the version and enable S3.
1. Increase the last number in `DefinitionBlock` (e.g. from `0x01072009` to `0x0107200A`):

   ![image](https://user-images.githubusercontent.com/59210462/190851198-cd5970d0-7f44-48be-a80e-af170eb2ed40.png)

2. Search for `XS3` and change it to `_S3`:

   ![image](https://user-images.githubusercontent.com/59210462/190851226-ac00834c-5bf0-4c84-9855-795d55760251.png)

Save DSDT.dsl and recompile:
```
iasl DSDT.dsl
```
There should be warnings, but not errors.
![image](https://user-images.githubusercontent.com/59210462/190851170-da93b4d6-7190-4b99-9634-6b0105833c91.png)

Keep Command Prompt open for the next step.

## Step 7: install new DSDT
Copy the modified DSDT to the ESP:
```
copy DSDT.aml R:\EFI\CLOVER\ACPI\WINDOWS\
```

Keep Command Prompt open for the next step.

## Step 8: configure Clover

Edit Clover's configuration file:
```
notepad R:\EFI\CLOVER\config.plist
```

Either change the `Timeout` parameter from `-1` to `2` to make Clover boot Windows in 2 seconds, or change the `Fast` parameter from `false` to `true` if you don't want to even see Clover during boot.
Save the file and reboot.

## Step 9: Clover themes
Place your favorite Clover theme in `\EFI\CLOVER\themes` and reference it in `config.plist`:
![image](https://user-images.githubusercontent.com/59210462/190854687-bb4dc1ee-34af-4b72-898d-888aaf3c916b.png)

See [here](https://github.com/CloverHackyColor/CloverThemes) for a list of themes.

# Troubleshooting
## Battery missing
If S3 sleep works, but the battery is not detected, then you might have dumped the ACPI tables incorrectly.
Specifically, using `acpidump.exe` from ACPICA tools is known to create this problem.
Dump from Clover, using F4, and patch it (Steps 5-6-7).

## BSOD upon wakeup
If you get intermittent blue screens when waking up from S3 sleep, check whether the driver involved is `AMDACPBUS.SYS` (AMD Audio CoProcessor). ~~Version 6.0.0.29 was reported doing this; version 6.0.0.50 seems OK.~~
![image](https://user-images.githubusercontent.com/59210462/191001036-eb92d6cb-dac8-4ebd-a35f-6ccbe01e3980.png)

Edit: all versions of `AMDACPBUS.SYS` seem to handle S3 sleep badly. One particular annoying behaviour is that upgrading the AMD graphics driver causes a BSOD ("Driver unloaded before cancelling pending operations"). I keep this device disabled.

To fix, disable the AMD Audio CoProcessor device in Device Manager. Not idea what it actually does.

~~To fix, update the AMD graphics driver ([5800HS/5900HS](https://www.amd.com/en/support/apu/amd-ryzen-processors/amd-ryzen-9-mobile-processors-radeon-graphics/amd-ryzen-9-5900hx) or [6800HS/6900HS](https://www.amd.com/en/support/apu/amd-ryzen-processors/amd-ryzen-7-mobile-processors-radeon-graphics/amd-ryzen-7-6800h)). Maybe it also helps to install the latest [AMD chipset drivers](https://www.amd.com/en/support/chipsets/socket-fp5-mobile/amd-ryzen-and-athlon-mobile-chipset).~~


## BSOD after BIOS update
ACPI tables are stored in the BIOS, so after upgrading most likely you get partial behaviour or BSOD upon boot.

To fix, re-dump and re-patch DSDT (Steps 5-6-7). I actually had to do it twice afte upgrading to BIOS version 413 (after upgrade, I got a BSOD upon boot; after first iteration, S3 was working but battery wasn't detected; after second iteration, everything is OK).

## Flashing lights while sleeping
During S3 sleep, the power on led on the left side double-blinks. If you're annoyed by the keyboard backlight double-blinking, this can be disabled in Armoury Crate->System->Lighting->Settings:
![image](https://user-images.githubusercontent.com/59210462/190901578-92411115-cbdc-4172-a50e-6c8ceaae9da5.png)
![image](https://user-images.githubusercontent.com/59210462/190901590-3dc3297f-3289-4004-a35b-7502e2757751.png)

## Reverting changes
- To re-enable S0 sleep, open Command Prompt as Admin and type:
```
reg delete HKLM\System\CurrentControlSet\Control\Power /v PlatformAoAcOverride
```
- To disable Clover temporarily, choose Windows Boot Manager from the Boot menu.
- To disable Clover until further notice, enter UEFI Setup and set Windows Boot Manager as boot option #1.
- To uninstall Clover, open Command Prompt as Admin and type:
```
mountvol R: /s
del /s R:\EFI\CLOVER\
```

# Future work
- [ ] Design a proper ROG theme for Clover :)
- [ ] Understand why the ROG red logo isn't showing when Windows boots. I have it working in a slightly different Clover installation, so it must be something about the drivers or the OEM config.
  Update: the OEM logo is referenced by an ACPI table called BGRT, which contains a physical memory address. Maybe this memory area is overwritten by Clover? anyway, it's possible to revert to the default Windows boot logo using `DropTables` in Clover's `config.plist`.
- [ ] Collect some more DSDTs; perhaps it's possible to provide the correct ones for each model in the `ACPI\OEM` directory and skip Steps 5-6-7.
- [X] Understand whether Clover's mechanism for DSDT patching can be used directly, which could replace Step 5 altogether.
  Update: no :( since Clover's `PatchACPI_OtherOS()` function only supports loading ACPI tables from `.aml` files and the `DropTables` command.