[Xiaomi Mi 6 Guides, News, & Discussion] wojtoo:
How to avoid TWRP Errors related to wrong Firmware for custom ROM & prepare FIRMWARE

One of the most frequent question related to flash custom ROM is what firmware should I use to this particular ROM

That is probably because you had, or you would like to avoid ERROR 7 during flashing in TWRP which is caused while the ROM’s updater script file checks to see if the phone model of your phone is compatible with the ROM you are installing

At the begging of flashing process updater-script check this:

getprop("ro.product.device") == "sagit" || abort("E3004: This package is for "sagit" devices; this is a "" + getprop("ro.product.device") + "".");

and trustzone

assert(sagit.verify_trustzone("TZ.BF.4.0.6-00124") == "1"); *

*(this version of TZ.BF is only example, I will describe that in the end)

Sometimes Developer allow to use wider TZBF like:

assert(sagit.verify_trustzone("TZ.BF.4.0.6-00124","TZ.BF.4.0.6-00130") == "1");

I assume that you fulfill the first condition (you have sagit, right?), so we will take to look deeper into second condition

How to know what trustzone is in the phone?

There are three methods:

1) when you lately flashed your phone with downloaded firmware or full miui rom (fastbooot or recovery) just unzip rom file (7-zip recommended) and look for tz.mbn file (see below)

2) Use app like Kernel Adiutor -> Statistics -> Device -> Trustzone (recommended method)

3) pull tz.mbn file directly from your phone

to do that you need adb tool, so under TWRP:

adb shell
cd /dev/block/bootdevice/by-name
ls -all

you should see list list of partition but what we looking for is that:

lrwxrwxrwx 1 root root 16 May 28 1970 tz -> /dev/block/sde15

copy partition to your storage

dd if=/dev/block/sde15 of=/sdcard/tz.img

now exit adb shell

exit

and copy tz.img to computer

adb pull /sdcard/tz.img

now we need hex editor to open tz.img or tz.mbn file

I use https://mh-nexus.de/en/hxd/ but you can your favorite hex editor

open tz file, press find and write TZ.BF, there is only one TZ.BF

QC_IMAGE_VERSION_STRING=TZ.BF.4.0.6-00124...IMAGE

Now, check what trust zone is needed in ROM which you would like to flash

It should be written in TrustZone requirements file named board-info.txt. It should be in github in root

In our case :

require version-trustzone=TZ.BF.4.0.6-00124

It's also written in your ROM file META-INF\com\google\android\updater-script

Despite of that you could use one of the firmware which have the same trustzone as needed I recommend that you follow firmware demanded/recommended by your ROM's Developer

version of TZBF in particular firmware:

8.2.2.0.NCAMIEC TZ.BF.4.0.6-00105

7.8.31 global TZ.BF.4.0.6-00124

7.8.31 china TZ.BF.4.0.6-00124

7.9.7 global TZ.BF.4.0.6-00124

7.9.8 china TZ.BF.4.0.6-00124

7.9.14 china TZ.BF.4.0.6-00124

7.9.22 global TZ.BF.4.0.6-00130

7.10.12 china TZ.BF.4.0.6-00130

7.10.12 global TZ.BF.4.0.6-00130

7.10.19 china TZ.BF.4.0.6-00130

China or Global?

In attached file.md5 there are md5sums of firmware files which I checked.

It seems, there is no difference between China or Global files from the same week (miui name ROM pattern version.mm.dd)

Do it at your Own Risk. I am not responsible for any loss or damage

EDIT 2017.11.14

Now its time to prepare your firmware based on XIAOMI ROM FILE

Choose version you need and download full ROM (not fastboot) from XIAOMI servers here

extract all files

go to extracted folder and delete all files/folders except folder firmware-update and META-INF

open META-INF/com and delete folder miui

download and extract updater-script from this tutorial

open \META-INF\com\google\android and repleace updater-script with this attached (mentioned above,downloaded and extracted)

Open it with eg notepad++ and correct /7.11.13 with version with you have and save it

go to main folder with firmware-update and META-INF and mark it, click righ mouse button and choose 7zip add to achrive

choose archive format zip, compression level store

copy to your phone and flash it with TWRP

that's it :)