Extract files from stock firmware images

Search This thread

pmzqla

Member
Dec 24, 2013
17
9
Hi,

when I still hadn't the device, I wanted to know exactly what's included in stock ROMs to have a better idea of what to expect. I hence downloaded a stock firmare and the stock system.img (see below for the steps).
Ok, so what? Well, when KK was released I decided to do the same (I was still waiting for the device), but I couldn't. Unlike before, I didn't find a single system.img, but multiple files (3 to be exact, maybe it's too big to be flashed at once with fastboot, I don't know, I'm new to this) and couldn't understand how the original image was splitted to generate those files.

Did anyone see something similar already and sucesfully merged splitted filesystems?

I know I could simply ask for a system dump (or wait for KK), but now I'm curious to know on how to do this. I tried few things but I couldn't find any way to do it. Maybe I could see how fastboot treat these files, but I wonder if anyone already knows the answer.


Anyway, here the steps to mount the system.img of our stock JB firmwares. Maybe there's an easier way, I honestly don't know. As far as I know, converting the sparge image should be enough, but I had to do more:
Code:
#Convert sparse image with simg2img
simg2img system.img system.img.raw.tmp

#UTF8 may slow down grep, switch to C
export LANG=C

#Look for the ext4 magic and calculate its position
magic=`grep -aobP -m1 '\x53\xEF' system.img.raw.tmp | head -1 | cut -d":" -f1`
offset=$(($magic-1080))

#Remove extra header with dd
dd if=system.img.raw.tmp of=system.img.raw ibs=$offset skip=1

#Remove temp file
rm system.img.raw.tmp
Now you can mount system.img.raw as a normal ext4 filesystem.
 

Darkshado

Senior Member
Apr 16, 2011
1,028
501
Montréal
Nvidia Shield Tablet
Nexus 6
Just concatenate the three chunks together like so:

Code:
cat system.img_sparsechunk1 system.img_sparsechunk2 system.img_sparsechunk3 > system.img

Then apply the steps from the OP and voilà!

Edit: Scratch that: the image is accessible, some files are visible but others are missing. To be continued...
 
Last edited:

scott_doyland

Senior Member
Dec 8, 2011
493
145
Just concatenate the three chunks together like so:

Code:
cat system.img_sparsechunk1 system.img_sparsechunk2 system.img_sparsechunk3 > system.img

Then apply the steps from the OP and voilà!

Edit: Scratch that: the image is accessible, some files are visible but others are missing. To be continued...

As you have found that doesn't work, remember that each file will have metadata headers so that may be one reason you can't just cat them together.

To OP - can't you just mount each img as a filesystem and copy all the files from each mounted filesystem to another entirely separate directory. At least that way you have all the files in one place, eg copy

/sparsechunk1/system/file1 to /newdir/system/file1

And so on.
 
Last edited:

pmzqla

Member
Dec 24, 2013
17
9
As you have found that doesn't work, remember that each file will have metadata headers so that may be one reason you can't just cat them together.

To OP - can't you just mount each img as a filesystem and copy all the files from each mounted filesystem to another entirely separate directory. At least that way you have all the files in one place, eg copy

/sparsechunk1/system/file1 to /newdir/system/file1

And so on.
Only the first chunk can be mounted, the other two are not recognized as filesystem and there's no way to mount them.
It's not as if /system was divided in three parts and then an image for each one was created, so that you can treat them as separate files (what you said would work in this case).
One image is created and then it's splitted in three in some unknown way. The first image is the one that holds the informations to access the files, the other two just pieces of files that can't be accessed without the informations in the first chunk.

mfastboot knows how to correctly copy the data from the separate images with the right offsets inside the phone so that in the end all the files can be accessed. Concatenating the files using dd using the correct offsets could maybe work, but after a few attempts I gave up.
 

Al936

Senior Member
Mar 10, 2007
269
248
Any change you happen to be willing to share the contents of or principles behind `sparse2img.exe`?
What kind of principles you expect from me? I just posted the link to one of the method to extract all files and folders from stock firmware's system partition. The tools were not developed by me - I just informed XDA community about it. As you can see from the tread several persons already confirmed that it works.
 

HolySid

Member
Jun 2, 2012
35
10
What kind of principles you expect from me? I just posted the link to one of the method to extract all files and folders from stock firmware's system partition. The tools were not developed by me - I just informed XDA community about it. As you can see from the tread several persons already confirmed that it works.

Oh, I'm sorry, I thought it was your work. I just want to know how to merge the system files. I know the exe is working, but I'm running Linux, so my question it is both out of curiosity and simply because I cannot run the code.
 

HolySid

Member
Jun 2, 2012
35
10
Thanks, I managed it by using another laptop. But still, I'd rather know what happened :)

Sent from my XT1032 using xda app-developers app
 

SenorChang

Member
Oct 21, 2010
38
59
Just concatenate the three chunks together like so:

Code:
cat system.img_sparsechunk1 system.img_sparsechunk2 system.img_sparsechunk3 > system.img

Then apply the steps from the OP and voilà!

Edit: Scratch that: the image is accessible, some files are visible but others are missing. To be continued...

I just replaced the first line in the OP's instructions with this to join the system.img_sparsechunk files:
Code:
simg2img system.img_sparsechunk.* system.img.raw.tmp

And then the rest worked fine. Here were the exact steps I took (I shortened it a tiny bit, but it's the same concept):

Code:
simg2img system.img_sparsechunk.* system.img.raw.tmp
offset=`LANG=C grep -aobP -m1 '\x53\xEF' system.img.raw.tmp | head -1 | awk '{print $1 - 1080}'`
dd if=system.img.raw.tmp of=system.img.raw ibs=$offset skip=1
sudo mkdir /mnt/system
sudo mount system.img.raw /mnt/system
 

Dusty tears

Member
Jan 24, 2013
43
33
Beijing
www.wonderkane.com
I just replaced the first line in the OP's instructions with this to join the system.img_sparsechunk files:
Code:
simg2img system.img_sparsechunk.* system.img.raw.tmp

And then the rest worked fine. Here were the exact steps I took (I shortened it a tiny bit, but it's the same concept):

Code:
simg2img system.img_sparsechunk.* system.img.raw.tmp
offset=`LANG=C grep -aobP -m1 '\x53\xEF' system.img.raw.tmp | head -1 | awk '{print $1 - 1080}'`
dd if=system.img.raw.tmp of=system.img.raw ibs=$offset skip=1
sudo mkdir /mnt/system
sudo mount system.img.raw /mnt/system

It worked. Thank you!:D
 

Top Liked Posts

  • There are no posts matching your filters.
  • 7
    Just concatenate the three chunks together like so:

    Code:
    cat system.img_sparsechunk1 system.img_sparsechunk2 system.img_sparsechunk3 > system.img

    Then apply the steps from the OP and voilà!

    Edit: Scratch that: the image is accessible, some files are visible but others are missing. To be continued...

    I just replaced the first line in the OP's instructions with this to join the system.img_sparsechunk files:
    Code:
    simg2img system.img_sparsechunk.* system.img.raw.tmp

    And then the rest worked fine. Here were the exact steps I took (I shortened it a tiny bit, but it's the same concept):

    Code:
    simg2img system.img_sparsechunk.* system.img.raw.tmp
    offset=`LANG=C grep -aobP -m1 '\x53\xEF' system.img.raw.tmp | head -1 | awk '{print $1 - 1080}'`
    dd if=system.img.raw.tmp of=system.img.raw ibs=$offset skip=1
    sudo mkdir /mnt/system
    sudo mount system.img.raw /mnt/system
    4
    Hi,

    when I still hadn't the device, I wanted to know exactly what's included in stock ROMs to have a better idea of what to expect. I hence downloaded a stock firmare and the stock system.img (see below for the steps).
    Ok, so what? Well, when KK was released I decided to do the same (I was still waiting for the device), but I couldn't. Unlike before, I didn't find a single system.img, but multiple files (3 to be exact, maybe it's too big to be flashed at once with fastboot, I don't know, I'm new to this) and couldn't understand how the original image was splitted to generate those files.

    Did anyone see something similar already and sucesfully merged splitted filesystems?

    I know I could simply ask for a system dump (or wait for KK), but now I'm curious to know on how to do this. I tried few things but I couldn't find any way to do it. Maybe I could see how fastboot treat these files, but I wonder if anyone already knows the answer.


    Anyway, here the steps to mount the system.img of our stock JB firmwares. Maybe there's an easier way, I honestly don't know. As far as I know, converting the sparge image should be enough, but I had to do more:
    Code:
    #Convert sparse image with simg2img
    simg2img system.img system.img.raw.tmp
    
    #UTF8 may slow down grep, switch to C
    export LANG=C
    
    #Look for the ext4 magic and calculate its position
    magic=`grep -aobP -m1 '\x53\xEF' system.img.raw.tmp | head -1 | cut -d":" -f1`
    offset=$(($magic-1080))
    
    #Remove extra header with dd
    dd if=system.img.raw.tmp of=system.img.raw ibs=$offset skip=1
    
    #Remove temp file
    rm system.img.raw.tmp
    Now you can mount system.img.raw as a normal ext4 filesystem.
    1
    There is method to extract files under Windows