Customize the Android firmware

Preface

There are two ways to customize the Android firmware:

  • Modify the source code and compile it to generate the firmware.

  • Cut out on the basis of existing firmware.

You can customize Android at various levels with greater degrees of freedom by using the previous method, but the requirements for the compiling environment and technical are relatively high, please refer to Compile the Android firmware.Now, the latter method is introduced and divided into three stages: unpack, customize and pack. The operating system for the host is Linux, and the tools used are open source software.

Firmware format

Unified firmware release_update.img, which contains the boot loader loader.img and firmware data update.img

release_update.img
|- loader.img
`- update.img

The update.img is a composite file containing multiple files, it is described by the package-file. A typical package-file is:

# NAME          Relative path
#
#HWDEF          HWDEF
package-file    package-file
bootloader      RK3288Loader_uboot_Apr212014_134842.bin
parameter       rk3288-3.10-uboot-data1G.parameter.txt
uboot           Image/uboot.img
misc            Image/misc.img
resource        Image/resource.img
kernel          Image/kernel.img
boot            Image/boot.img
recovery        Image/recovery.img
system          Image/system.img
backup          RESERVED
update-script   update-script
recover-script  recover-script
  • package-file: Packing instruction file for the update.img, a package-file is also contained in the update.img.

  • RK3288Loader_uboot_Apr212014_134842.bin :Boot loader(i.e. bootloader).

  • rk3288-3.10-uboot-data1G.parameter.txt :Parameter file, which can be used to set the kernel startup parameters, and there are important partition information in it.

  • Image/misc.img :Image of the partition, which is used to control whether the Android starts normally or enters the emergency mode(Recovery Mode).

  • Image/kernel.img :Android kernel.

  • Image/resource.img :Resource image, which contains the boot image and the device tree blob information of the kernel.

  • Image/boot.img :The memory boot disk (initrd) of the Android kernel, which is the root file system that is loaded first after the kernel startup and contains important initialization actions, in general, the change is not required.

  • Image/recovery.img :Image in the emergency mode for Android, which contains the root file system of the kernel and emergency modes.

  • Image/system.img :Corresponding to the /system partition of Android, and it is the following custom object.

Unpack, extract the update.img from release _ update.img and extract the multiple files declared by the package-file.Pack, this is an inverse process, synthetic the files listed in the package-file into the upload.img, add them into loader.img, and finally generate the release_upload.img.

Tools preparation

git clone https://github.com/TeeFirefly/rk2918_tools.git
cd rk2918_tools
make
sudo cp afptool img_unpack img_maker mkkrnlimg /usr/local/bin

Unpack

  • Extract the release_update.img

$ cd /path/to/your/firmware/dir
$ img_unpack Firefly-RK3288_Android4.4_20140818.img img
rom version: 4.4.2
build time: 2014-08-18 14:25:57
chip: 80
checking md5sum....OK
  • Extract the update.img

$ cd img
$ afptool -unpack update.img update
Check file...OK
------- UNPACK -------
package-file	0x00000800	0x00000285
RK3288Loader_uboot_Apr212014_134842.bin	0x00001000	0x0004694E
rk3288-3.10-uboot-data1G.parameter.txt	0x00048000	0x000005A1
Image/misc.img	0x00048800	0x0000C000
Image/kernel.img	0x00055000	0x00578E3C
Image/resource.img	0x005CE000	0x0001C400
Image/boot.img	0x005EA800	0x0011F6CF
Image/recovery.img	0x0070A000	0x0040F6AE
Image/system.img	0x00B19800	0x180EF000
RESERVED	0x00000000	0x00000000
update-script	0x18C09000	0x000003A5
recover-script	0x18C09800	0x0000010A
UnPack OK!
  • View the file tree under the update directory

$ cd update/
$ tree
.
├── Image
│   ├── boot.img
│   ├── kernel.img
│   ├── misc.img
│   ├── recovery.img
│   ├── resource.img
│   ├── uboot.img
│   └── system.img
├── package-file
├── recover-script
├── RESERVED
├── rk3288-3.10-uboot-data1G.parameter.txt
├── RK3288Loader_uboot_Apr212014_134842.bin
└── update-script

In this way,the firmware is successfully unpacked.Now let’s start with the customization.

Customize

Customize the system.img

The system.img is an image file in ext4 file system format that can be mounted directly to the system for modification:

sudo mkdir -p /mnt/system
sudo mount -o loop Image/system.img /mnt/system
cd /mnt/system
# When modifying the content inside, you should pay attention to the remaining space and cannot add too many APKs
# After modification, you need to uninstall it
cd /
sudo umount /mnt/system

Note that the remaining space of the system. img is basically 0. If you do not want to delete files, you need to expand the system.img and adjust the partition settings in the parameter file accordingly depending on the size of the last file.Here is an example of how to extend the space, and before extending, you need to run the mount to view the system mounting and make sure the system.img is uninstalled:

# Add a 128M of space
dd if=/dev/zero bs=1M count=128 >> Image/system.img
# Extended file system information
e2fsck -f Image/system.img
resize2fs Image/system.img

Pack

First, you should check the size of the system. img and make the necessary size adjustments based on the partitioning of the parameter file (refer to the document Parameter file format.For example,you can find the CMDLINE line depending on the system partition size in the rk3288-3.10-uboot-data1G.parameter.txt file, and then find the system string:

0x00180000@0x00092000(system)

The size of the partition is in the front of @, and the unit is 512 bytes, so the size of the system partition is:

$ echo $(( 0x00180000 * 512 / 1024 / 1024))M
768M

If the size of the system.img does not exceed 768M, the parameter file does not need to be changed.If the partition is not required to be changed, the new system.img can be flashed directly into the system partition of the development board with the flashing tool for testing. Otherwise, you will need to make a new firmware and flash it before testing.The following steps are required to pack into a unified firmware update.img:

  • Synthetic the update.img:

# The current directory is still update/, which contains the package-file, and the files listed by the package-file exist
# Copy the parameter file to the paramter, by default, the afptool is used to
$ cp rk3288-3.10-uboot-data1G.parameter.txt parameter
$ afptool -pack . ../update_new.img
------ PACKAGE ------
Add file: ./package-file
Add file: ./RK3288Loader_uboot_Apr212014_134842.bin
Add file: ./rk3288-3.10-uboot-data1G.parameter.txt
Add file: ./Image/uboot.img
Add file: ./Image/misc.img
Add file: ./Image/kernel.img
Add file: ./Image/resource.img
Add file: ./Image/boot.img
Add file: ./Image/recovery.img
Add file: ./Image/system.img
Add file: ./RESERVED
Add file: ./update-script
Add file: ./recover-script
Add CRC...
------ OK ------
Pack OK!
  • Synthetic the release_update.img:

$ img_maker -rk32 loader.img update_new.img release_update_new.img
generate image...
append md5sum...
success!

The release_update_new.img is the unified firmware file that is finally generated and flashable.

FAQ

Where to set firmware version

Locate the following line in the parameter file and modify it; note that the version number is a number and the two dots in the middle cannot be omitted.

FIRMWARE_VER:5.1.0