Tuesday, February 28, 2017

root access - How to unpack/repack system.img of android ROM?


I have OnePlus 6T device which has A/B partition system and has a ROM of user type i.e [ro.build.type]: [user]. This device is rooted with Magisk. I have a requirement(Want to place customized sepolicy file under system_root directory) to modify system.img.


I have tried different tools like:



  1. simg2img :



OMEN-by-HP-Laptop-15-dc0xxx:~/WorkArea/img-tools$ ./simg2img system.img sys.raw
Invalid sparse file format at header magi
Failed to read sparse file
OMEN-by-HP-Laptop-15-dc0xxx:~/WorkArea/img-tools$


  1. imgtools


OMEN-by-HP-Laptop-15-dc0xxx:~/WorkArea/imgtool$ sudo ./imgtool system.img extract
[sudo] password for OMEN:

system.img is not a recognized image. Sorry
OMEN-by-HP-Laptop-15-dc0xxx:~/WorkArea/imgtool$


  1. and more tools even on windows..


but none of them is capable of parsing my system.img.


I have copied system.img directly from OnePlus6T ROM setup which installs Android 9 on this device without any issue.


Any help on:




  1. How to fix the system.img so it will be extracted and repacked fine?

  2. Any command that can unpack/repack system.img?

  3. Any working tool to accomplish this task?


Update 1: I have run file system.img and I found that its ext2 image and the tools support ext4.


system.img: Linux rev 1.0 ext2 filesystem data, UUID=d09c08e9-628d-590e-a610-3a14de2a8db0 (extents) (large files) (huge files)


Update 2: Tried to find the magic number and have following result:


OMEN-by-HP-Laptop-15-dc0xxx:~/WorkArea/imgtool$ xxd system.img | head

00000000: 0000 0000 0000 0000 0000 0000 0000 0000 ................
00000010: 0000 0000 0000 0000 0000 0000 0000 0000 ................
00000020: 0000 0000 0000 0000 0000 0000 0000 0000 ................
00000030: 0000 0000 0000 0000 0000 0000 0000 0000 ................
00000040: 0000 0000 0000 0000 0000 0000 0000 0000 ................
00000050: 0000 0000 0000 0000 0000 0000 0000 0000 ................
00000060: 0000 0000 0000 0000 0000 0000 0000 0000 ................
00000070: 0000 0000 0000 0000 0000 0000 0000 0000 ................
00000080: 0000 0000 0000 0000 0000 0000 0000 0000 ................
00000090: 0000 0000 0000 0000 0000 0000 0000 0000 ................

OMEN-by-HP-Laptop-15-dc0xxx:~/WorkArea/imgtool$

Update 3 Fing the image already unpacked. So to add the required file, I have mount the image as sudo mount -o loop system.img system_mount and then tried to copy the contents to another folder with cp system_mount/* system/ so that I can add the required file and make new image out of it but I got following errors:


root@OMEN-by-HP-Laptop-15-dc0xxx:~/WorkArea# mkdir system && cp system_mount/* system/
cp: omitting directory 'system_mount/acct'
cp: cannot stat 'system_mount/bin': No such file or directory
cp: cannot stat 'system_mount/bt_firmware': No such file or directory
cp: cannot stat 'system_mount/bugreports': No such file or directory
cp: cannot stat 'system_mount/cache': No such file or directory
cp: cannot stat 'system_mount/charger': No such file or directory

cp: cannot stat 'system_mount/charger_log': No such file or directory
cp: omitting directory 'system_mount/config'
cp: omitting directory 'system_mount/d'
cp: omitting directory 'system_mount/data'
cp: omitting directory 'system_mount/dev'
cp: cannot stat 'system_mount/dsp': No such file or directory
cp: cannot stat 'system_mount/etc': No such file or directory
cp: cannot stat 'system_mount/firmware': No such file or directory
cp: omitting directory 'system_mount/lost+found'
cp: omitting directory 'system_mount/mnt'

cp: omitting directory 'system_mount/odm'
cp: omitting directory 'system_mount/oem'
cp: omitting directory 'system_mount/op1'
cp: omitting directory 'system_mount/op2'
cp: cannot stat 'system_mount/persist': No such file or directory
cp: omitting directory 'system_mount/postinstall'
cp: omitting directory 'system_mount/proc'
cp: cannot stat 'system_mount/product': No such file or directory
cp: omitting directory 'system_mount/res'
cp: omitting directory 'system_mount/sbin'

cp: cannot stat 'system_mount/sdcard': No such file or directory
cp: omitting directory 'system_mount/storage'
cp: omitting directory 'system_mount/sys'
cp: omitting directory 'system_mount/system'
cp: omitting directory 'system_mount/vendor'
root@OMEN-by-HP-Laptop-15-dc0xxx:~/WorkArea#


Answer



To Unpack-Modify-Pach the system.img, I have followed the following procedure:


a) Unpacking




  • Run file system.img and make sure that system.img is Android Sparse Image.

  • Rename system.img to system.img.ext4. // Not required if you will use other name for raw image in below steps.

  • With simg2img system.img.ext4 system.img, you will get a raw image file named system.img

  • With mkdir system, create directory to mount system.img

  • With sudo mount -t ext4 -o loop system.img system/ you will get all files of system.img in system folder


b) Modifying



  • With ls -l system/init.rc note permissions: 750


  • With sudo chmod 777 system/init.rc give write permissions

  • With sudo echo "#MODIFICATION " >> system/init.rc done some modification in init.rc

  • With sudo chmod 750 init.rc reset init.rc to the noted permissions


c) Calculate system sector size



  • With tune2fs -l system.img | grep "Block size\|Block count" you will get block size and count

  • With echo $((1553064 * 4096)) multiply both results. I got 6361350144


d) Packing




  • With sudo make_ext4fs -s -l 6361350144 -a system system_new.img sys/ you will get system_new.img “Android Sparse Image” that has all changes


No comments:

Post a Comment

samsung galaxy s 2 - Cannot restore Kies backup after firmware upgrade

I backed up my Samsung Galaxy S2 on Kies before updating to Ice Cream Sandwich. After the upgrade I tried to restore, but the restore fails ...