How to unlock Bootloader, flash Custom ROM and Root Xiaomi Mi A2

Introduction

This tutorial will explain how to unlock the bootloader, flash a recovery image, flash a custom ROM and then root a Xiaomi Mi A2. This tutorial is not compatible with 6X devices.

To avoid bricking your phone, follow every single step precisely as described. If you miss a step or use a wrong version for something, you might render your device unusable.

Environment

Hardware amd64 PC, at least 10GB disk space
OS Debian 11 Bullseye

Dependencies

  1. https://github.com/cyxx/extract_android_ota_payload
  2. https://developer.android.com/studio/releases/platform-tools
  3. https://sourceforge.net/projects/kangos/files/Jasmine_Sprout/
  4. https://eu.dl.twrp.me/jasmine_sprout/twrp-installer-3.3.1-0-jasmine_sprout.zip.html
  5. https://eu.dl.twrp.me/jasmine_sprout/twrp-3.3.1-0-jasmine_sprout.img.html
  6. https://github.com/topjohnwu/Magisk/releases

Step by Step Instructions

0. Backup important data

Usually it's enough to do a adb pull /sdcard. For further information on this, do your own research.

It is highly recommended to do a clean install and the tutorial supports only that.

Before using adb commands, make sure USB Debugging is enabled and you allowed your computer to use USB Debugging on your phone.

1. Unlock the Bootloader

  1. Enter "Settings" on your phone.
  2. Go to "System".
  3. Go to "About Phone".
  4. Tap on "Build Number" at least 7 times.
  5. Go to "Settings" and look for "Developer Options".
  6. Enable "OEM unlocking".
  7. Enable "USB Debugging".
  8. Disable automatic updates.
  9. Reboot your phone into Fastboot mode by holding down the power key and the "volume down" button at the same time, until the little communist rabbit appears.
  10. Connect your phone to your computer by using a USB cable.
  11. Download platform-tools and extract the platform-tools archive into a folder of your choice. Let's assume you extracted it into your home folder, resulting in a folder like this: `~/platform-tools`.
  12. Now cd into that folder and type:
    fastboot flashing unlock
    fastboot flashing unlock_critical
  13. Use your volume keys and power button to confirm that you want to unlock your phone's bootloader.

2. Flash TWRP, a recovery image

  1. Move twrp-installer-3.3.1-0-jasmine_sprout.zip to your phone's storage.
    Can be achieved with adb push twrp-installer-3.3.1-0-jasmine_sprout.zip /sdcard/twrp-installer-3.3.1-0-jasmine_sprout.zip.
  2. Move KangOS-11.2-R-Rockers-Gapps-OFFICIAL-jasmine_sprout-20201129-0543.zip (or its equivalent without GApps) to your phone's storage.
    Can be achieved with adb push KangOS-11.2-R-Rockers-Gapps-OFFICIAL-jasmine_sprout-20201129-0543.zip /sdcard/KangOS-11.2-R-Rockers-Gapps-OFFICIAL-jasmine_sprout-20201129-0543.zip.
  3. Now boot into recovery like this, after rebooting into Fastboot mode:
    fastboot boot recovery.img
  4. Now "Flash ZIP" and select the TWRP Installer ZIP we pushed to the /sdcard location in step 1.
  5. Reboot into recovery by keeping the power key and the "volume up" key pressed, until you see the TWRP logo.

3. Flash Kang OS, a custom ROM

  1. Within TWRP navigate to:
  2. Wipe
  3. Advanced Wipe
  4. Select everything except "Internal Storage" (which has the ZIP files from earlier on it).
  5. Slide to Wipe. This operation is irreversible!
  6. Go back to the Main Menu and navigate to Reboot.
  7. Change to Slot B.
  8. Repeat the above wiping procedure.
  9. Change to Slot A.
  10. Go back to the Main Menu and navigate to "Install ZIP".
  11. Reboot into system and disable the installation of the TWRP app.

4. Root your device

  1. Install the Magisk Manager app on your phone, after the previous boot into system finished.
  2. If you want to do the boot patching yourself, follow the next steps. If you use the attachment from this tutorial for your Magisk flashing, then you can skip the next steps and proceed with the step starting with "!!".
  3. Install python3 and git on your PC like this: sudo apt install -y python3 python3-pip git.
  4. Make room for a Git repository: mkdir ~/src ; cd ~/src ; git clone https://github.com/cyxx/extract_android_ota_payload.git ; cd extract_android_ota_payload.
  5. Install the tool's dependencies: sudo pip3 install -r requirements.txt.
  6. Extract payload.bin from the Kang OS zip file into the current folder.
  7. Extract the boot.img like this: python3 extract_android_ota_payload.py payload.bin ..
  8. Now copy the resulting boot.img file to your phone.
  9. In the Magisk Manager app, tap "Install" on the Magisk card.
  10. Proceed to "Select and patch image".
  11. Navigate to the previously copied boot.img file and select this one.
  12. Now get the file onto your PC, like this: adb pull /sdcard/Download/magisk_patched_123.img. The "123" has to be replaced with the actual name suffix in the Download folder of your phone.
  13. !!(This is where the manual part ends. If you downloaded the zip file with the pre-patched boot image from this page, then you may proceed from here, skipping above steps.) Reboot into Fastboot mode by pressing the power key and the "volume down" buttons on your phone at the same time, until the communist rabbit returns.
  14. Flash the Magisk patched boot image:
    fastboot flash boot_a magisk_patched_123.img
    fastboot flash boot_b magisk_patched_123.img
    Remember to replace the image name with the actual name of your image. The "123" will be different in your case.

Reboot into the system and we are done here. Your phone has an unlocked bootloader, runs a custom recovery, a custom ROM and is rooted!

 

Sources