[Updated] How to port MIUI 7 & MIUI 8 ROMs to Qualcomm powered Smartphones

342 comments
MIUI-8-LOGO
[This Guide is also applicable in porting other ROMs like Color OS, Flyme OS, Sense and more... and this guide has been updated for MIUI 8 as well]

ROM porting is not an easy task for a newbie and porting a highly customizable ROM like MIUI v7 is a way tougher. When I was porting MIUI v7 Lollipop for a Qualcomm Snapdragon device I did not find a definitive guide for porting the latest MIUI v7. So, porting MIUI became a very tough task since I was a newbie in porting ROMs. I learnt porting through experience. Hence I am writing this guide to help the new MIUI porters so that they don’t face the problems which I faced.

Not much history now, let’s move on to our actual task i.e. how to port MIUI v7?

DISCLAIMER: This guide is written to help the emerging devs. Soft bricking is common in porting ROMs. But, I am not responsible if you somehow hard-brick your device. Do it at your own risk.

Prerequisites

Things required for porting:
  • Device for which the ROM is being ported
  • Port ROM i.e. the ROM of other device which you want to port (here we have MIUI 7)
  • Base ROM i.e. the Rom which is exclusively made for your device (CyanogenMod is preferable but if it is not available then Stock ROM may be used)
  • PC with Windows + Ubuntu (either in dual boot or through Virtualbox)
  • Rom Deodexer (for Winidows - JoelDroid Lollipop Batch Deodexer and for Linux - SuperR's Kitchen recommended) [For Android 6.0 Marshmallow ROMs use this tool to deodex: SuperR's Kitchen]
  • Android Image Kitchen to modify boot.img
  • Notepad++ to edit scripts
  • Meld to compare files and folders
  • Winrar or 7zip
  • Apktool (for advanced tasks only)
  • And some common sense

Common Information

  • This guide will teach you to port ROMs from devices with similar hardware specs and same Qualcomm Snapdragon chipset.
  • The Port ROM and the Base ROM should have the same android version.
  • ROMs containing DAT files should be properly unpacked. For base ROM you can easily do it by flashing the ROM and taking the TWRP backup of ‘system’ and extracting the resultant file with Winrar/7zip. For Port ROM follow this guide: [DEV] Lollipop/Marshmallow DAT files | Compression and Decompression
  • ROMs should be properly deodexed before porting (SuperR's Kitchen worked better in deodexing MIUI 7 Lollipop) if your device's /system partition size is smaller than P Port ROMs system files.
  • The size of the system folder of your Port ROM should be lesser than that of the system partition of your device, else you'll not be able to flash the ROM. To decrease the size you need to deodex your ROM and you may further need to debloat you Port ROM which is discussed later.
  • I am writing this guide on the basis of this configuration:

    • Base device: YU Yureka (ARM64)(MSM8939)
    • Base ROM: CyanogenMod 12.0
    • Android Version: 5.0.2 Lollipop
    • Port device: MI4i (ARM64)(MSM8939)
    • Port ROM: MIUI v7 Lollipop
Now let’s start porting.

Preparing Porting Environment

  1. Make sure you have plenty of hard-disk space in your PC.
  2. Make a folder named MIUI-PORTING-PROJECT (you may take any other name)
  3. Inside that folder create two folders: (i) BOOT-EDITING  (ii) ROM-PORTING
  4. In ROM-PORTING folder create two folders named BASE and PORT (this is the main working folder)
  5. Similarly inside the BOOT-EDITING folder create two folders named BOOT-BASE and BOOT-PORT
  6. Now place the extracted deodexed Port ROM (i.e. MIUI v7) in PORT folder and Base ROM (i.e. CM 12.0) in BASE folder.
  7. Similarly place the MIUI v7 boot.img in BOOT-PORT folder and CM12 boot.img in BOOT-BASE folder.
These steps are required to properly organize the project and will be helpful in our later discussions.

Cleaning up:
Just delete some unwnted files (.mbn files) and keep only ‘META-INF’ folder, ‘data’ folder (if available), ‘system’ folder, ‘boot.img’ file and ‘file_contexts’ file (if available) in PORT folder.

Now let’s do some work to make the Port ROM boot.

META-INF editing

  1. Open the updater-script file in PORT/META-INF/com/google/android/ with Notepad++.
  2. Delete the first line containing “get_device_compatible(….” or “assert(getprop….” which restricts the flashing process to other devices.
  3. Then delete the lines containing scripts for flashing radio image, modem image etc. which are not required for our base device.
Now you are ready to go.

boot.img editing

1. Use Android Image Kitchen to unpack the base and port boot.img in BOOT-BASE and BOOT-PORT folder.

2. Replace the files boot.img-zImage, boot.img-dtb in BOOT-PORT/split_img/ with that of BOOT-BASE/split_img.

Editing Boot IMG
Replacing boot.img files
3. Then in the BOOT-PORT/ramdisk/ folder compare the file fstab.qcom with that of the base i.e. BOOT-BASE/ramdisk/fstab.qcom. (You can use Meld for it.)
Editing fstab.qcom Step 1
Editing fstab.qcom using Meld: Step 1
Editing fstab.qcom Step 2
Editing fstab.qcom using Meld: Step 2
After this repack your boot.img and place it in your port ROM folder (i.e. PORT) and create a flashable zip. You will be able to boot this time. But, you may face sensors, sound, camera, ext sd-card etc. issues. So let us move on to fix those issues through boot.img first.

4. If the port device doesn’t support external sd-card but your base device does then you make the following changes to your BOOT-PORT/ramdisk/init.qcom.rc file:

Remove ‘#’ from the lines containing scripts for mounting or fusing ‘sdcard1’. You can do this simply by comparing the port init.qcom.rc with that of base through Meld application. (Note: You may also need to add some lines if missing in the port init.qcom.rc)

5. After sd-card, let’s move towards fixing camera, led, sensors etc. issues. For that we need to edit the init.rc and init.target.rc files.

=> Editing init.rc file:
Following lines should be added to init.rc file:

#Add all these lines under 'on post-fs-data' in appropriate places
on post-fs-data

#Add all these lines under 'on boot' in appropriate places
on boot

=> Editing init.target.rc file:
Following lines should be added to init.target.rc file:

#Add all these lines under 'on post-fs-data' in appropriate places
on post-fs-data

#Add all these lines under 'on boot' in appropriate places
on boot

6. Even after doing the above steps you may find that sensors are not working. So for that you have to do the following step –

Compare the port ueventd.qcom.rc in BOOT-PORT/ramdisk folder with that of base and add the missing lines to the port. This will fix your sensors issue.

7. Similarly compare other BOOT-PORT files like uneventd.rc, file_contexts etc. with that of BOOT-BASE and use your brain to replace required lines.

8. To fix other issues use your common sense.

9. If you are doing boot.img editing at the beginning of your project, then some of you may find that even after doing all these steps, sensors, led, camera etc. are not working. But don’t lose heart, for that you have to start your ROM Porting.

ROM Porting

Before starting ROM porting, your base and port ROM should be properly deodexed as I discussed before. If you have correctly edited the boot.img then the following steps will help you to solve your bugs.

1. Debloating Port ROM: Just delete unwanted apps or bloatwares from your port ROM. You will find them in /data/, /system/app/ and /system/priv-app/ folders.

2. According to my previous instructions you should have placed your port ROM to PORT folder and base ROM to BASE folder.

3. Adding missing files and folders: You may add missing files from BASE/system/bin/ to PORT/system/bin/. For this you can just copy all the files from BASE/system/bin/ to your PORT/system/bin/ without replacing any file.
Similarly you can do the same process in system/xbin/, system/fonts/, system/etc/, system/usr/ etc. and their sub-folders.

But always use your brain to decide which files you should add and which you should not.

You may also add some missing folders from your base ROM  to your port ROM, e.g. system/tts folder, system/etc/init.d and other important folders.

4. Fixing permission related issues: Now you have to work on system/etc/permissions folder. Just compare handheld_core_hardware.xml and platform.xml of port ROM with that of base and add or replace some lines if required.

You can do the same for some other permission files if required, but as I am always saying use your common sense.

5. Replacing files: Copy and replace files in system/etc/firmware and in system/etc/wifi from base to port.

Copy and replace the files goodix.kl, gpio-keys.kl, Generic.kl in system/usr/keylayout/ from base to port.

We will discuss more about replacing files in bug-fixing section.

6. Open system/framework/framework-res.apk of both base and port with Winrar in two different windows but don’t extract. Then navigate to framework-res.apk/res/xml/ of base and copy storage_list.xml and power_profile (optional) and paste to the Winrar window of port framework-res.apk/res/xml/ and replace the existing files. Then close the Winrar window.

Now let’s move on to our bug-fixing section.

Bug-Fixing

If you have properly edited the boot.img then only these fixes will help you.

Copy and replace the following files from Base (ROM-PORTING/BASE) to Port (ROM-PORTING/PORT) to solve the given issues:

No sound bug:
system/lib/hw/
  • audio.primary.msm****.so
  • audio.primary.default.so
system/lib64/hw/
  • audio.primary.msm****.so
  • audio.primary.default.so

Sound working in speaker but not working through headphones or vice-versa:
system/etc/

  • mixer_paths.xml (you can also try patching it with the base)

Distorted Outgoing Sound during Phone call or Partial Sound:
system/etc/acdbdata/MTP/
  • MTP_***.acdb (all files)

Home or any other button not working:
system/usr/keylayout/
  • goodix.kl
  • gpio-keys.kl (important)
  • Generik.kl

External sd-card not showing:
Follow the Step 6 in ROM Porting section.

Video hw encoding/decoding not working:
system/etc/firmware/
  • venus.*** (all files starting with ‘venus’)

Sensors not working:
  • system/lib/hw/sensors.msm****.so (or sensors.lsm6db0.so)
  • system/lib64/hw/sensors.msm****.so (or sensors.lsm6db0.so)
  • system/lib/libsensorservice.so
  • system/lib64/libsensorservice.so

Led notification not working:
  • system/lib/hw/lights.msm****.so
  • system/lib64/hw/lights.msm****.so

Vibration not working:
  • system/lib/libhardware_legacy.so
  • system/lib64/libhardware_legacy.so

FM not working:
  • system/lib/qcomfm_jni.so
  • system/lib64/qcomfm_jni.so

Wifi not working:
  • system/lib/modules/wlan.ko
[Note: In MIUI, wlan.ko is renamed as pronto_wlan.ko and is also kept in system/lib/modules/pronto/ folder, so you have to replace in both the places.]

In some phones you may not find wlan.ko file. In that case you may try replacing these files (thanks @Divyansh747):

  • system/lib/libwcnss_qmi.so
  • system/lib64/libwcnss_qmi.so

For slow WiFi network and other WiFi related issues replace these files:

  • system/etc/wifi/ -- all files
  • system/etc/firmware/wlan/ -- all files


Camera and flash not working:
system/vendor/lib/
  • libactuator_***.so (all files starting with ‘libactuator’)
  • libchromatix_***.so (all files starting with ‘libchromatix’)
  • libmmcamera_***.so (all files starting with ‘libmmcamera’)
system/lib/
  • libmm-qcamera.so
  • libmmcamera_interface.co
system/lib/hw/
  • camera.msm****.so
[Note: If such files are also available in your BASE/system/vendor/lib64/ then you have to copy those files also to their respective place in PORT]

WARNING: Do not replace any file unnecessarily, because it may give rise to new bugs in place of solving it. Always be specific!

build.prop editing

You can edit required parts of build.prop to reflect the brand, model no. etc. according to your preference. You may also add some tweaks and other things to save battery and to add features.
(But in MIUI 7 according to my experience changing the ro.product.manufacturer gives rise to issue in video recording through third-party apps and changing the ro.product.device trims some features from the ROM.)

Now your Porting Project is ready to be flashed and to be reviewed. Congrats!

Giving Credits

Last but not least when you successfully port your ROM, always give credits to the Port and Base Rom developers. Also give credits to the people from whom you took help. Always be generous in giving credits because it will not take anything from you, on the other hand it will increase your reputation and strengthen people’s faith in your work.


If you are taking help from this guide in your ROM Porting then don't forget to give some credit to me too.

My profile

XDA Forums: nitesh9
YU Forums: Nitesh Prasad
MIUI Forums: nitesh9

My works

MIUI v7 Lollipop Global and China Beta for YU Yureka: XDA Forums | YU Forums | MIUI Forums

MIUI 8 Global and China Beta ROM for Yureka and Yureka Plus: OS Busters | XDA Forums | YU Forums


For any queries or discussion please comment below...