Linux Commands / Instructions / Tools for Unpacking and Repacking System Images for MTK devices - EXT4 and YAFFS

roman2025's picture
Package icon img-tools.zip832.46 KB
Description and Instructions on Use: 

NOTE: THE ATTACHED TOOLS ARE FOR x86/32-bit Linux distributions ONLY! - I spent an hour scratching my head on a ubuntu 64-bit install as to why I couldn't run these tools. You have to be using a 32-bit flavor of linux. I receommend LUbuntu if you are running in virtual box, which can be had from here:

For EXT4 formatted system images you can use the following commands inside of a linux environment to unpack and mount and then repack stock system images for MTK devices:

The above zip file contains a folder with all of the binary files referenced by the commands below. (which should work on Ubuntu 12+ for sure as that is what I work on for my dev efforts).

---First unpack that directory above somewhere on your system. For ease of explanation, I am going to use my home folder as the location where I put the unpacked folder containing all the bin files. So /home/roman/ will be our base. We will place our system.img file from our stock firmware into our base home folder. Furthermore there will be another directory in my home folder already created called "working" (so /home/roman/working) that will be the mount point for our unpacked system.img so we can modify files---

1. Open up a terminal command line prompt and become root with the following command:
sudo su

2. change into your img-tools directory:
cd /home/roman/img-tools

3. Use the simg2img program to "un-sparse" your stock image file as a raw ext4 image file:
.simg2img /home/roman/system.img sys.raw

4. Now mount that raw file into your directory for modification of files:
mount -t ext4 -o loop /home/roman/img-tools/sys.raw /home/roman/working/

5. Open up a new terminal window and use the following command to launch the nautilus file browser (ubuntu) as the root user:
sudo nautilus

6. Now a new file browser window appears. Browse to /home/roman/working/ and you should see your firmware files... Mod away!

7. When you are all done making changes and want to build a new system.img for flashing from that directory, do the following.
cd /home/roman/img-tools
./make_ext4fs -s -l 750M -a system newsystem.img /home/roman/working/

8. You should now have a file called "newsystem.img" in your /home/roman/img-tools directory, this is your new system.img file and it can be flashed using SPFlash tool!

I vastly prefer this method to using a prepackaged "kitchen" - not that those don't have their place for making life easier with some things. The nice thing about working with a system.img vs. creating an though is that you don't need a custom recovery environment to install it. This method therefore should work on a much wider range of devices assuming you can get access to stock firmware.

--- For YAFF Formatted Images---

Similar to the above but you use a different tool set with the image file. The commands look like this:

cd /home/roman

mv system.img working/system.img

cd /home/roman/img-tools

./unyaffs /home/roman/working/system.img

-----modify files in working directory and then you need to move your system.img file out of that directory before repacking. So..

mv /home/roman/working/system.img /home/roman/system.img

Once that is moved back out, you can go ahead and create your image.

./mkfs.yaffs2.x86 /home/roman/working /home/roman/newsystem.img

Brief explanation for moving the system.img file about:
Unyaffs is going to extract the contents of system.img to wherever the system.img file is stored. So it is a good idea to move the system.img file into whatever folder you want your rom contents to end up in. Hence we move it above. Before repacking the rom though you want to make sure the original system.img isn't still sitting in that working directory. Hence we move it back out before repacking.

Final note:

The above methods also work on the userdata.img (tested against EXT4 and I am assuming a YAFFS formattated userdata.img file would also work) which is where manufacturers often put a lot of the bloatware applications for installation into the system during first boot. You can remove those apk files and clean the system up. You can also place other APK files in userdata.img for installation into your rom. The nice thing about applications placed in here is that the user can easily uninstall them if they don't want them.