Modifying stock Android firmware is a task that requires not only technical skills, but also a deep understanding of the operating system architecture. Unlike installing ready-made custom firmware (for example, LineageOS or AOSP Extended), creating your own version from scratch allows you to specifically optimize performance, remove unnecessary software from the manufacturer (bloatwear) or add unique functions. However, this process is fraught with risks: from โbrickingโ the device to loss of warranty.
This article is intended for users with experience with ADB, Fastboot and basic knowledge Linux. If you have never compiled source code or worked with firmware images, we recommend that you first learn the basics by building ready-made custom firmware as an example. Here we will analyze the process of decompiling stock firmware, its modification and reassembly, taking into account the characteristics of specific devices - from Samsung Galaxy to Xiaomi Redmi.
โ ๏ธ Attention: Modifying the firmware will void the device warranty and may lead to irreversible consequences. The process requires unlocking the bootloader, which is officially prohibited on some devices (for example Huawei or Sony Xperia). Before starting work, make sure that you have a backup copy of your data and access to EDL mode (emergency firmware) for your model.
It is also worth noting that the firmware assembly procedure can take from several hours to a day in depending on the power of your PC. To work you will need at least 16 GB of RAM i 100 GB of free space on an SSD drive. If your device runs on a processor Mediatekbe prepared for additional complications with drivers and tools like SP Flash Tool.
1. Preparation: tools and requirements
Before you begin modifying the firmware, you need to collect a complete set of tools and make sure that your device supports the required operations. The main components you will need:
- ๐ง A computer running Linux (recommended Ubuntu 22.04 LTS or Debian 11). Virtual machines running Windows through WSL2 may be unstable.
- ๐ฑ Unlocked bootloader on the target device. For most brands, this requires official unlocking through the manufacturer's website (for example, Mi Unlock Tool for Xiaomi).
- ๐พ Stock firmware in format
.zip or .img for your model. Download only from official sources (for example, Samsung Firmware or Xiaomi Firmware Updater).
- ๐ ๏ธ Tool package:
Java JDK 11+ (for working with apktool)
Python 3.8+ (for automation scripts)
Git (for cloning repositories)
ADB & Fastboot (for interaction with the device)
7-Zip or tar (for unpacking archives)
Special attention pay attention to version Java. Some tools (for example apktool) do not work correctly with Java 17+so you may need to install a specific version via sdkman or manually configure environment variables.
Also prepare backup the EFS partition (contains IMEI and serial numbers). To do this, in the Fastboot mode:
adb backup -f efs_backup.ab -apk -obb -shared -all -system
Loss of data from the EFS partition makes the device unsuitable for calls and mobile data, even if the firmware is loaded.
2. Extracting and analyzing stock firmware
Stock firmware is usually supplied in the form of archives with the extension .zip, .tar or .md5 (for Samsung). Your task is to extract partition images from them (boot.img, system.img, vendor.img etc.) for further modification.
For devices on Qualcomm Snapdragon use the utility payload_dumper (if the firmware is in the format payload.bin):
git clone https://github.com/vm03/payload_dumper
python payload_dumper.py payload.bin
This will split the firmware into separate files .img. For devices Mediatek you will need SP Flash Tool or MTK Client.
After extraction, check the integrity of the images using sha256sum:
sha256sum boot.img system.img
Compare the hashes with the official values (if they are provided by the manufacturer).
Unpacked the firmware archive|Obtained boot.img and system.img images|Checked integrity hashes|created an EFS backup copy|Connected the debugging cable-->
3. Decompilation and modification of system.img
The main work is being done with the partition system.img, which contains the Android kernel, system applications and frameworks. To modify it you will need:
- Mount
system.img as ext4section:
sudo mount -o loop system.img /mnt/system
- Use
apktool to decompile APK files (for example, Settings.apk or Framework-res.apk):
apktool d Framework-res.apk -o framework
- Make changes to the decompiled files (for example, remove signature verification in
AndroidManifest.xml or modify resources in res/values).
- Rebuild the APK using
apktool b and sign it using signapk.
Modification example: to remove pre-installed applications (bloatware), find them in the folder /mnt/system/priv-app/ or /mnt/system/app/ and delete the corresponding .apk and .odex files. However, be careful - deleting system services. (for example, TeleService.apk on Samsung) may disrupt the operation of telephony.
For devices with dynamic partitions (Android 10+) you will need to work with super.img, which contains several logical partitions. Use lpunpack And lpmake to unpack and reassemble it:
lpunpack super.img
Make changes to the unpacked images
lpmake --device-size=1073741824 --metadata-size=65536 --sparse --output=super_new.img system.img vendor.img product.img
Before deleting system APKs, check their dependencies using the command grep -r "package_name" /mnt/system/. Some applications may be associated with critical services (for example, com.qualcomm.qti.telephonyservice for Qualcomm modems).
4. Working with boot.img: kernel and ramdisk
File boot.img contains the Linux kernel and ramdisk โ a temporary file system loaded into memory when the device starts. To modify it:
- ๐ Parse
boot.img using unpackbootimg (from the package android-bootimg-tools):
unpackbootimg -i boot.img -o boot
This will create a folder with files boot.img-zImage (kernel) and boot.img-ramdisk.gz (compressed ramdisk).
- ๐ Unpack the ramdisk:
gzip -d boot.img-ramdisk.gz
cpio -i -F boot.img-ramdisk
- โ๏ธ Make changes to the ramdisk files (for example, edit
init.rc to disable the check dm-verity or add support Magisk).
- ๐ Rebuild the ramdisk and boot.img:
find . | cpio -H newc -o | gzip > new-ramdisk.gz
repackbootimg -i boot.img -k boot.img-zImage -r new-ramdisk.gz -o new-boot.img
One of the most common modifications is disabling dm-verity (checking the integrity of the system section), which allows you to load modified firmware. To do this, in the file init.rc find the line:
verifyatboot
and replace it with:
#verifyatboot
or delete it completely.
โ ๏ธ Attention: Disabling dm-verity reduces security device, as it allows you to download potentially malicious code. Use this modification only if you plan to further encrypt the partition /data using FDE or FBE.
5. data-i="162">After modifying all the necessary partitions, they need to be assembled back into a format suitable for flashing. The process depends on the type of device:
After modifying all the necessary partitions, they need to be assembled back into a format suitable for flashing. The process depends on the device type:
Device type
Build tool
Command example
Notes
Qualcomm
fastboot
fastboot flash boot new-boot.img
Requires an unlocked bootloader
Mediatek
SP Flash Tool
flash_tool -s scatter.txt
You need a file scatter.txt with a partition map
Samsung (Odin)
heimdall or Odin
heimdall flash --BOOT new-boot.img
For Exynos requires a patched one Odin
Google Pixel
fastboot
fastboot flash --slot=all boot new-boot.img
Support for A/B partitions
For devices with A/B-partitions (for example, Google Pixel or OnePlus) flash changes to both slots:
fastboot --set-active=a
fastboot flash boot new-boot.img
fastboot --set-active=b
fastboot flash boot new-boot.img
After flashing, you need to sign the modified partitions. To do this, use the utility avbtool (for devices with AVB 2.0):
avbtool add_hash_footer --partition_name system --partition_size $(wc -c < system.img | awk '{print $1}') --image system.img --algorithm SHA256_RSA2048 --key rsa_private.key --output system_signed.img
rsa_private.key - your private key (can be generated using openssl).
What to do if the device does not boot after flashing the firmware?
If after flashing the modified boot.img device freezes on the logo or goes to bootloop, try:
1. Boot into Fastboot and flash the original boot.img back.
2. If the bootloader is locked, use EDL mode (for Qualcomm) or Download Mode (for Samsung).
3. Check the boot logs via adb logcat (if the device responds at least a little).
4. On devices with Mediatek a utility can help MTK Bypass to bypass bootloader protection.
6. Testing and debugging
First launch of the modified one. firmware is the most critical stage. Even if the device has booted, check the following parameters:
- ๐ถ Cellular communications and Wi-Fi: Make sure that the IMEI is saved (check via
*#06#), and the modem recognizes SIM cards.
- ๐ Charging and battery: Check the charge level display and charging speed (on some devices after modification
boot.img fast charging disappears).
- ๐ต Sound and multimedia: Test the speakers, microphone and video playback (especially with a codec
H.265).
- ๐ Security: If you disabled
dm-veritycheck the operation of Magisk or other modules for root access.
For debugging, use the commands:
adb logcat | grep -i "error" # View errors in logs
adb shell dmesg | grep -i "fail" # Kernel errors
If the device boots, but interface artifacts or lags occur, the problem may be incompatibility of the modified ones .apkfiles with version Android Runtime (ART). In this case, try:
- Clear the ART cache via
adb shell pm clear-cache.
- Rebuild the APK with a different level of optimization (for example, add a flag
--use-aapt2 to apktool).
- Return original files
framework-res.apk and android.policy.jar.
If, after modifying the firmware, the device overheats or quickly discharges, most likely the problem is in the kernel (boot.img-zImage). Try using the stock kernel with a modified or patched ramdisk init.rc.
7. Optimization and additional modifications
When the base firmware works stably, You can start fine tuning. Here are some popular optimizations:
- โก Speed up animation: Change the values in
framework-res.apk/res/values/integers.xml:
<integer name="config_activityShortDur">100</integer>
<integer name="config_activityDefaultDur">150</integer>
- ๐๏ธ Bloatware cleaning: Remove unnecessary system applications (for example,
Browser2.apk to Samsung or MiBrowserGlobal.apk on Xiaomi), but leave critical services like TelephonyProvider.apk.
- ๐ Scheduler settings kernels: For processors Qualcomm add to
build.prop:
debug.performance.tuning=1
pm.deferred_launch=true
- ๐ Adding Magisk: Patch
boot.img via Magisk Manager to obtain root access without a trigger SafetyNet.
For devices with AMOLED screens you can optimize the color palette by editing files in /vendor/overlay. For example, for more saturated black colors, change the settings in DisplayColorMode.xml.
โ ๏ธ Attention: Changing kernel parameters (for example, processor frequencies in /sys/devices/system/cpu) can lead to overheating or unstable operation. Always test such modifications with temperature logging enabled via adb shell cat /sys/class/thermal/thermal_zone*/temp.
8. Creating a flashable archive for distribution
If you want to share your firmware with other users, package it in flashable-zip, which can be flashed via TWRP or OrangeFox Recovery. The archive structure should include:
META-INF/com/google/android/updater-script โ a recovery script describing the firmware process.
system.img, boot.img and other modified sections.
vendor/ โif modified vendor.img.
install/bin/ โ auxiliary scripts (for example, to check the device model).
A simple example updater-script:
ui_print("Installing modified firmware...");
assert(getprop("ro.product.device") == "RMX1971" || abort("This firmware is only for Realme 6!"));
package_extract_file("boot.img", "/dev/block/bootdevice/by-name/boot");
package_extract_file("system.img", "/dev/block/bootdevice/by-name/system");
ui_print("The firmware is complete! Reboot...");
set_progress(1.0);
To automate the assembly of the archive, use the script on Bash:
#!/bin/bash
zip -r custom_rom.zip META-INF system.img boot.img vendor.img
signapk -w testkey.x509.pem testkey.pk8 custom_rom.zip custom_rom_signed.zip
Before distributing, test the archive on a clean installation (with a full wipe /data) and make sure that the firmware passes check SafetyNet (if this is important for your case).
FAQ: Frequently asked questions
Is it possible to modify the firmware on a device with a locked bootloader?
No, flashing modified images requires an unlocked bootloader. On some devices (for example Huawei or LG) unlocking is officially prohibited, and bypassing the protection can lead to a "brick".
How to return the original firmware if something goes wrong?
For most devices, it is enough to flash the stock firmware via Fastboot, Odin or SP Flash Tool. If the device does not respond, you may need EDL mode (for Qualcomm) or a service center.
Do you need to sign the firmware for personal use?
Signature is required only if you plan to distribute the firmware or flash it through recovery with signature verification. For personal use via fastboot a signature is not required.
Is it possible to modify the firmware on Android 13+ without a SafetyNet trigger?
Starting with Android 12, Google has tightened checks SafetyNet i Play Integrity. Even with a patched boot.img you may need to use modules like Universal SafetyNet Fix or Play Integrity Fix for Magisk.
How to modify the firmware for devices with dynamic partitions (Android 10+)?
To work s super.img use utilities lpunpack i lpmake. The main difficulty is to correctly calculate the sizes of logical partitions (system, vendor, product) when reassembling.