[REF] Unpacking and repacking stock rom .img files

Search This thread

jazzor

Senior Member
Mar 6, 2010
70
157
As most of you are aware, the stock rom tarball is comprised of several .img files which don't seem to be of any file system format and it is difficult at first to see what is inside these files without flashing via Odin.

Well, thanks to fguy, this thread http://xdaforums.com/showthread.php?t=1054836 provided me the necessary inspiration to investigate just exactly what are these img made of.

Hopefully, this will allow easy removal of all the Samsung junk, deodexing, rooting etc etc without going through adb.

As it turns out, they are simply sparse ext4 images, and the AOSP source tree already contains the necessary utilities to both unpack and repack these img files via "ext4_utils" in system/extras (https://android.googlesource.com/platform/system/extras/)

For your convenience, I have provided a repackage of the source code for ext4_utils with a simple Makefile, for keen developers to make the necessary tools for unpacking and repacking the stock img files. Making these tools requires a linux development environment, which is beyond the scope of this topic.

To make the tools, simple extract the attached source code and type "make".

To unpack the .img file:
Code:
./simg2img factoryfs.img output.img

To view the contents of .img file:
Code:
mkdir tempdir; mount -o loop output.img tempdir

To repack .img file:
Code:
./mkuserimg.sh -s /some/directory/with/files ./factoryfs_custom.img ext4 ./temp 512M

As I do not have Odin at the moment, I am not able to test the repacked files can be reflashed successfully. I will test this shortly.


UPDATE
I have just tested repacking the stock factoryfs.img. It so happens that the original ext4_utils does not properly preserve permissions on the new ext4 image. I have made a slight modification to the source and reuploaded the zip.

You can now successfully unpack, remove and change the stock apps, root and modify the stock image using instructions above!!

I hope this will help people create their own custom roms.


UPDATE 27th MAY FOR GALAXY S4 TESTING (i9500 i9505)
A few people have messaged me asking why these tools do not work for the new Galaxy S4. Unfortunately I do not have a Galaxy S4 so I can not test any of these utilities. HOWEVER, it seems there has been quite a bit of updates by Google in the ext4_utils package. I have updated the sources according to 4.2.2 which is the latest version of the tools. I must stress that I DO NOT HAVE A GALAXY S4 TO TEST so don't be expecting miracles. In any case I have uploaded the new code now. As it was before, I have provided a MAKEFILE and you simply type "make" in order to build the tools. They are contained in ext4_utils_new.zip.

Thanks for your support guys. And I do apologize for replying late to my inbox...

Update 9th November 2014
Thanks to qwerty_7564 for providing the fixes for cygwin compilation.
Ext4 utils new still missing link arguments to compile. Investigating. I will update the sources to that of the 4.3 branch later.

Update 3rd December 2014
I have updated the tools to 4.3 branch. Note that no attempts has been made to make this compatible with cygwin as I have no cygwin to test.
As I have mentioned earlier, I do not have a newer samsung device to test any of these files, use at your own discretion. The new files are in ext4_utils_r4.3.tar.gz and is based on the jb release 2.0.0 branch.
 

Attachments

  • ext4_utils_cygwin_fixed.zip
    59.8 KB · Views: 17,802
  • ext4_utils_new_cygwin_fixed.tar.gz
    64.9 KB · Views: 13,897
  • ext4_utils_r4.3.tar.gz
    110 KB · Views: 25,414
Last edited:

manuvaidya

Senior Member
Jan 5, 2008
471
8
Bangalore
you mean, with these tools anyone with fair knowledge of linux can create custom ROMs ? what all we can do with this trick to an image/stock ROM ? pls explain
keen to know what change it could bring to the present scenario of tweaks...
 

nightranger73

Senior Member
Oct 20, 2010
979
263
50
Reggio Emilia
OPPO Find X3 Neo
Yes and you can even provide root privileges to stock rom without having to pass through insecure firmware

As you said anyone with a little knowledge of Linux could do that
It's only unpacking and repackaging new rom

Sent from my GT-I9100 using Tapatalk
 
  • Like
Reactions: jaras67

jazzor

Senior Member
Mar 6, 2010
70
157
There is nothing special about the img file. It is just that I have not seen anyone write a guide to modify these img files yet. They are simple compressed ext4 images as explained in the first post. AOSP already recognises this format and have made available utilities for you to modify and create your own img files which can be flashed via Odin
 
  • Like
Reactions: tanclo and cottino

BoRoU

Senior Member
Sep 25, 2007
224
75
Valencia
Hi, I have given make a mistake, it can be?

Thanks.
borou@borou-casa:~/discoduro/BASURILLA/GSII/Varios/ext4_utils$ make
gcc -I. -DANDROID -c make_ext4fs.c
gcc -I. -DANDROID -c make_ext4fs_main.c
gcc -I. -DANDROID -c ext4_utils.c
gcc -I. -DANDROID -c allocate.c
gcc -I. -DANDROID -c backed_block.c
gcc -I. -DANDROID -c output_file.c
output_file.c:25:18: fatal error: zlib.h: No existe el fichero o el directorio
compilation terminated.
make: *** [output_file.o] Error 1
 
  • Like
Reactions: tikcolg

GalaxyS!

Senior Member
Dec 23, 2010
107
23
i get a warning when i try the option make

root@galaxy-1:/home/galaxy/ext4_utils# make
gcc -I. -DANDROID -c make_ext4fs.c
gcc -I. -DANDROID -c make_ext4fs_main.c
gcc -I. -DANDROID -c ext4_utils.c
gcc -I. -DANDROID -c allocate.c
gcc -I. -DANDROID -c backed_block.c
gcc -I. -DANDROID -c output_file.c
gcc -I. -DANDROID -c contents.c
contents.c: In function ‘make_directory’:
contents.c:118:3: warning: format ‘%llu’ expects type ‘long long unsigned int’, but argument 4 has type ‘u32’

how can i fix this?
 
  • Like
Reactions: wangzhenxing

sgstwo

Senior Member
May 12, 2011
67
9
i get a warning when i try the option make

root@galaxy-1:/home/galaxy/ext4_utils# make
gcc -I. -DANDROID -c make_ext4fs.c
gcc -I. -DANDROID -c make_ext4fs_main.c
gcc -I. -DANDROID -c ext4_utils.c
gcc -I. -DANDROID -c allocate.c
gcc -I. -DANDROID -c backed_block.c
gcc -I. -DANDROID -c output_file.c
gcc -I. -DANDROID -c contents.c
contents.c: In function ‘make_directory’:
contents.c:118:3: warning: format ‘%llu’ expects type ‘long long unsigned int’, but argument 4 has type ‘u32’

how can i fix this?


Code:
error("failed to allocate %llu extents", (unsigned long long)len);
 

RyanZA

Senior Member
Jan 21, 2006
2,023
784
JHB
Yes, it will still compile. Since you ignore warnings, I take it you're not a programmer. ;)

And if you look closely, it's also a bug. Nothing serious, but still a bug.

No, there really is nothing to fix. ;)

Code:
        data = inode_allocate_data_extents(inode, len, len);
        if (data == NULL) {
                error("failed to allocate %llu extents", len);
                return EXT4_ALLOCATE_FAILED;
        }

Worst case scenario: the error message displays a weird number. ;)

EDIT: Not saying you don't get this (obviously you do - haha), but fixing this for someone asking what the warning means is a bit overkill. :p
 
Last edited:

jazzor

Senior Member
Mar 6, 2010
70
157
You can make the tools for windows, but it will still be useless since you also need an ext4 driver and a loop mounter. And then theres the issue of maintaining file permissions on said ext4 driver. Unfortunately, windows is not a very friendly environment for such tasks.
 
  • Like
Reactions: Shahrist

pulser_g2

Admin Emeritus / Senior Recognized Developer
Nov 27, 2009
19,544
11,630
Can someone make a simple guide? I have never use linux before :)
Thanks

This is the development section. It's not an easy process. Take a read, and google the bits you don't know. The info in the first post is all you need :) Get a virtualbox set up with ubuntu in it or something :)
 

Mackzen

Senior Member
Apr 20, 2009
125
40
This is the development section. It's not an easy process. Take a read, and google the bits you don't know. The info in the first post is all you need :) Get a virtualbox set up with ubuntu in it or something :)

Thanks for the answer

I have write this and place factoryfs.img inside ext4_utils. What I'm doing wrong?

PHP:
marcus@ubuntu:~$ cd ~/Desktop/ext4_utils/
marcus@ubuntu:~/Desktop/ext4_utils$ ./simg2img factoryfs.img output.img
bash: ./simg2img: No such file or directory
 

Top Liked Posts

  • There are no posts matching your filters.
  • 110
    As most of you are aware, the stock rom tarball is comprised of several .img files which don't seem to be of any file system format and it is difficult at first to see what is inside these files without flashing via Odin.

    Well, thanks to fguy, this thread http://xdaforums.com/showthread.php?t=1054836 provided me the necessary inspiration to investigate just exactly what are these img made of.

    Hopefully, this will allow easy removal of all the Samsung junk, deodexing, rooting etc etc without going through adb.

    As it turns out, they are simply sparse ext4 images, and the AOSP source tree already contains the necessary utilities to both unpack and repack these img files via "ext4_utils" in system/extras (https://android.googlesource.com/platform/system/extras/)

    For your convenience, I have provided a repackage of the source code for ext4_utils with a simple Makefile, for keen developers to make the necessary tools for unpacking and repacking the stock img files. Making these tools requires a linux development environment, which is beyond the scope of this topic.

    To make the tools, simple extract the attached source code and type "make".

    To unpack the .img file:
    Code:
    ./simg2img factoryfs.img output.img

    To view the contents of .img file:
    Code:
    mkdir tempdir; mount -o loop output.img tempdir

    To repack .img file:
    Code:
    ./mkuserimg.sh -s /some/directory/with/files ./factoryfs_custom.img ext4 ./temp 512M

    As I do not have Odin at the moment, I am not able to test the repacked files can be reflashed successfully. I will test this shortly.


    UPDATE
    I have just tested repacking the stock factoryfs.img. It so happens that the original ext4_utils does not properly preserve permissions on the new ext4 image. I have made a slight modification to the source and reuploaded the zip.

    You can now successfully unpack, remove and change the stock apps, root and modify the stock image using instructions above!!

    I hope this will help people create their own custom roms.


    UPDATE 27th MAY FOR GALAXY S4 TESTING (i9500 i9505)
    A few people have messaged me asking why these tools do not work for the new Galaxy S4. Unfortunately I do not have a Galaxy S4 so I can not test any of these utilities. HOWEVER, it seems there has been quite a bit of updates by Google in the ext4_utils package. I have updated the sources according to 4.2.2 which is the latest version of the tools. I must stress that I DO NOT HAVE A GALAXY S4 TO TEST so don't be expecting miracles. In any case I have uploaded the new code now. As it was before, I have provided a MAKEFILE and you simply type "make" in order to build the tools. They are contained in ext4_utils_new.zip.

    Thanks for your support guys. And I do apologize for replying late to my inbox...

    Update 9th November 2014
    Thanks to qwerty_7564 for providing the fixes for cygwin compilation.
    Ext4 utils new still missing link arguments to compile. Investigating. I will update the sources to that of the 4.3 branch later.

    Update 3rd December 2014
    I have updated the tools to 4.3 branch. Note that no attempts has been made to make this compatible with cygwin as I have no cygwin to test.
    As I have mentioned earlier, I do not have a newer samsung device to test any of these files, use at your own discretion. The new files are in ext4_utils_r4.3.tar.gz and is based on the jb release 2.0.0 branch.
    6
    I fixed the newest ext4-utils has today.

    Look at the attachment for the source code fixed :)

    To compile, just type make.
    5
    Very educational tutorial, quick and to the point. Though i had to many errors. From start. sorry for being so blunt but the file specificly has to be named factoryfs.img. So to revoke any error i did this instead
    Code:
    simg2img <name of file> system.img.raw
    For one you cant mount tempdir when it already exists so i did this instead
    Code:
    mkdir mountpoint
    Plus you have to be rooted to run mount. So i did this instead
    Code:
    sudo mount -t ext4 -o loop system.img.raw mountpoint/
    Also i kept getting command not found using make_ext4fs. So if you get this error do this. open terminal and execute this command
    Code:
    gedit .bashrc
    go to the very end of the file and add
    Code:
    export PATH=$PATH:/YOUR/WORKING/DIRECTORY/
    save close. then run this command
    Code:
    source .bashrc
    Also got error using your command
    Code:
    ./mkuserimg.sh -s /some/directory/with/files ./factoryfs_custom.img ext4 ./temp 512M
    So i executed this command instead
    Code:
    make_ext4fs -s -l 512M -a system system.img.new mountpoint/
    After that no errors. Also you forgot to mention you need to add the command to unmount the image. So i did this
    Code:
    sudo umount -t ext4 mountpoint
    . Thank you for guiding me in the right direction though.
    5
    the problem is that this version requires a file_contexts file. This can be extracted from boot.img. The filename is file_contexts

    1. extract boot.img (i used AndroidImageKitchen for this)
    2. call make_ext4fs with -S parameter
    i.e.
    Code:
    make_ext4fs -l -s 2690M -a system -S <PATH_TO_FILE_CONTEXTS_FILE> system.img.ext4 <WHEREEVER_YOU_MOUNTED_SYSTEM_TO>

    btw: the name of the mountpoint doesnt matter. So calling your mountpoint sys was no problem
    4
    Hi, I have given make a mistake, it can be?

    Thanks.


    hi BoRoU, try this:

    $ apt-get install zlib1g-dev

    :D