LineageOS on Xiaomi

Updated 28-Jun-2024

Major update to this post as of April 2024, nearly 4 years later

  • The below is woefully out of date. Trying to do the same thing with the same product (but different device) results in tears, mainly around Xiaomi OEM unlocking.
  • The unlock tools simply don't work on the Linux and Windows devices I've tried to use.
  • However, there is a clever and cleaner solution, which is using an Android device and Termux

Sadly, the device I worked on, while I could get to the point of receiving the unlock code from Xiaomi (a major undertaking and nice moment), I could never get the device to receive the mi-fastboot commands which would do the actual unlocking. I think there is something wrong with the hardware / os itself (bought second-hand).

Resources for Termux unlocking Xiaomi

Introduction

I've run custom roms in the past, when Cyanogenmod was the rage, on an HTC Desire (bravo). The main reason was the 576mb of ram made increasingly bloated Google services untenable. I think I modded another HTC after that, but then went down the rabbit hole of the Iphone 5. After that one got tiresome/wore out, I turned to Xiaomi, which was reasonable hardware and a great price. This includes:

  • Xiaomi Redmi 4A (a slim device, but underpowered with 2gb of RAM and 16gb of storage, and non-HD screen)
  • Xiaomi Mi A1 (a bit heavy, good screen, overall nice specs, comes with Android Experience which has significant problems with wifi/bluetooth speeds)
  • Xiaomi Mi Pad 4 (an excellent 8" tablet)

Nowadays I am running Xiaomi hardware and it is the combination of Xiaomi and Google bloatware and general cluelessness (the latest rom update for the A1 actually breaks WIFI significantly if bluetooth is turned on) has turned me back to the side of lightness and truth. This is about 5 years later, and Cyanogenmod is LineageOS, and I've got two devices to manage:

  • The Xiaomi Redmi 4A (died due to a broken screen, prompting the A1 purchase)
  • Xiaomi Mi A1 (2017, 4gb ram/64 gb storage -- 3 year old model but new device, replaces the 4A as the daily driver)
  • Xiaomi MiPad 4 (2018, 3gb ram/32gb storage -- mostly used by my children, a capable 8" tablet)

What follows are steps I took to deal with these devices and get them fully functional, without any Google or Xiaomi bloatware or spyware.

Developer Settings

First one must tap 7 times on the build number in Settings > About phone to enable the developer options menu.

OEM unlocking & USB debugging

These are two settings to enable in the Settings > System > Advanced > Developer options:

  • OEM unlocking
  • USB debugging

See also a Cyanogenmods forum topic on this subject.

Install ADB & Fastboot

ADB and Fastboot are Android utilities. I downloaded from the Cyanogenmods.org site, and did two things:

  • Installed the ADB and Fastboot from Google, and
  • Ran the sh /path/to/install/script/ADB-Install-Linux.sh script, and
  • Downloaded the latest Linux platform tools, and copied over the adb and fastboot binaries to /usr/bin. (This last step might not be necessary.)

Much of this information is found on the Cyanogenmods forum. The 2012 adb-fastboot-install from Google.

Interlude - Xiaomi OEM Unlocking

Some early devices, and most since 2016 have locked bootloaders. The exception is the Android Experience devices (such as the Xiaomi Mi A1). The Xiaomi Redmi 4A was released in late 2016 and does have a locked bootloader. The Xiaomi MiPad 4 (2018) does have a locked bootloader.

In order to unlock these bootloaders, one needs to go through a process:

  • Go to Mi Unlock status in developer seettings, for unlocking the Xiaomi bootlocker
  • Bind the device to a verified Mi account. This requires unique email address and phone number, not used with other devices, not necessarily a phone number used by the device itself.
  • Use a tool to unlock the bootloader. The Xiaomi tool is Windows only. However, this tool has been ported to Linux and is called the XiaoMiTool V2 (XMT2_Linux_9.11.10.run).

Unlock Bootloader

Besides the Xiaomi 2016+ procedure, most devices reqiure only adb / fastboot commands such as:

  • sudo fastboot devices, and
  • sudo fastboot oem unlock

Boot into TWRP.img

Each device has its own unique TWRP image. This image allows for executing various utilities and backup/restore functions. The key is to boot the image, not try to install it (that is an older procedure no longer supported). Reading the comments on this useful Cyanogenmods TWRP Install article reveals this.

Finding the appropriate TWRP is not the easiest task. For the MiPad 4, I used recovery-TWRP-3.2.3-0930-XIAOMI_PAD4(PLUS)-CN-wzsx150.img.

For Tissot (Xiaomi 1A), i used [twrp-2.2.1-0-tissot.img](https://dl.twrp.me/tissot/twrp-3.3.1-0-tissot.img.html).

I copy the files into shorter names for ease of duplication of the process. Also the commands have a problem with parentheses in the filename.

  • sudo fastboot boot twrp.img

Clean Wipe the Device in TWRP

Clean wipe the device (all various memory components).

Essentially wipe everything. If you encounter an error, you may need to create the data partition, then attempt to wipe again (advanced setting).

Enable Sideloading

After wiping the device, the idea is to enable sideloading under Advanced, and then back on the computer.

Note to wipe memory before turning on adb sideloading otherwise booting up won't be clean.

Install LineageOS

Again, finding the right distribution file is not the easiest task. For the MiPad, I used lineage-17.1-20200106-UNOFFICIAL-clover.zip, which is the latest and greatest LineageOS 17.1. Again, not the brightest idea, as Android 9 is more stable. On a second installation, i used [lineage-16.0-20190126-UNOFFICIAL-clover.zip](https://androidfilehost.com/?fid=11410963190603908610)

For the Xiaomi 1A, I used [lineage-16.0-20190817-UNOFFICIAL-tissot.zip](https://sourceforge.net/projects/lineage-tissot/files/release/lineage-16.0-20190817-UNOFFICIAL-tissot.zip/download).

Renamed, the command becomes:

  • sudo adb push lineage.zip /

Note that Android 10 (Q) is a bit buggy (and the buttons change a bit) and LineageOS 16.x Android 9 (Pie) is probably the better, stable option.

From TWRP then choose Install.

Use sudo on adb and fastboot

The above uses adb push instead of sideloading, which requires an extra step of connecting to the device in fastboot mode.

However, sideloading will work (when booted into TWRP), without needing to reboot, if sudo is used for adb and fastboot. If either were started without sudo then:

adb kill-server
sudo adb start-server

Then detach and reattach the device. If there is any kind of timeout these steps will help, for example between sideloading different applications.

Install GAPPS or use FDroid/Aurora

For Tissot, I used MindTheGapps-9.0.0-arm64-20190615_031441.zip.

Push (or sideload) this as well, before booting into Lineage OS.

sudo adb push gapps.zip /

If you don't need or want the FDroid store, then you are done.

Install the FDroid.apk file

Find and install the FDroid.apk file using adb.

adb install FDroid.apk

There are other ways to sideload apps, but this seems the most obvious.

Once this is done there is the basic store available. Enable installation from its sources and then install Aurorastore to get applications in the Google store.

The best approach is to use anonymous (and not use any paid apps), or to create a new gmail account and use that for any purchased apps. Google can remove any account used in this way to access the store as it is against the terms of service. Though in practice this has not happened.

Root not needed

It seems that root is not needed for these operations. Root if or when necessary.

Problem with needing Google Play Services

Some apps complain about needing Google Play services, and stop running (Gboard, some banking apps). And some apps do not show up in the store due to some version checking.

Some apps complain about needing Google but work anyway, such as Mi Fit and Transferwise.

The microg project is meant to address some of these issues.

See also the Fake GAPPS project.

Banking apps in general

Banking and other apps such as Netflix may or may not need microg or other fixes.

Treble ROMs

There is a thing called Treble ROMS, which might mean trouble. Magisk has special instructions for Treble ROMs.

Microg

More info here...

Notes and To Update

  • Make it clear that to enter fastboot, turn off device, then turn on device with power+vol-up
  • Once in fastboot, push over the twrp image, which then boots into recovery
  • You can then fastboot pus files over and install them

Resources for Upgrading to 17.1 (or 17.0, depending on stability)