22

I am trying to see the content in a boot.img file from an Android image.

I googled and found this article to extract system.img, but it doesn't work for boot.img. When trying to do this for boot.img, it is showing the following:

Invalid sparse file format at header magi
Failed to read sparse file

Is simg2img used only for extracting system.img?

  1. If so, Is there any other method to extract boot.img?
  2. If not, what is the problem for not extracting boot.img?
Firelord
  • 342
  • 1
  • 17
Dhasneem
  • 573
  • 1
  • 4
  • 10

4 Answers4

22

boot.img is a small(ish) file that contain two main parts.

  • kernel (important for android)
  • ramdisk (a core set of instruction & binaries)

Unpack boot.img:

It contains the following steps:

  1. Download the tool using

    wget https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/android-serialport-api/android_bootimg_tools.tar.gz
    
  2. Extract the file using

    tar xvzf android_bootimg_tools.tar.gz
    

    It contains two binaries:

    • unpackbootimg
    • mkbootimg
  3. Then execute

    ./unpackbootimg -i <filename.img> -o <output_directory>
    

    The output_directory will contain:

    • boot.img-zImage (kernel)
    • boot.img-ramdisk.gz (ramdisk)

We can extract the ramdisk also, using the following command

gunzip -c boot.img-ramdisk.gz | cpio -i

After changing the files, we can again pack those files as boot.img using mkbootimg

AdminBee
  • 21,637
  • 21
  • 47
  • 71
Dhasneem
  • 573
  • 1
  • 4
  • 10
  • 1
    I am getting error at third step please help me...the terminal says unpackbootimg command not found – RMB Jun 17 '14 at 10:04
  • 1
    Getting the same error for step 3 on Mint 17 here. Tried running them by sudo and after chmod 755 to no avail. – Nisse Sep 11 '14 at 04:41
  • 1
    In step 3, make sure `output_path` already exists, otherwise `unpackbootimg` will segfault. – Aldaviva Feb 13 '16 at 03:07
  • 2
    The link for the tool is dead. – Addison Crump Jan 18 '17 at 15:05
  • Just clone the code from github: https://github.com/osm0sis/mkbootimg – Albus Dumbledore Mar 15 '17 at 05:37
  • 1
    Fix for: "unpackbootimg command not found", you are running 32bit binary on a 64 bit machine without 32bit dependencies. Install them with "apt-get install gcc-multilib" – WHol Jul 27 '17 at 19:44
  • do I need to connect it in fastboot mode, or just simply connect? – user1325696 Feb 11 '20 at 21:02
  • I'm getting `Segmentation fault (core dumped)`. I tried when I was connecting using adb, then I tried in fastboot mode after I started `adb reboot bootloader`. I also tried to unlock OEM in DevTools, and I still get segmentation fault. – user1325696 Feb 11 '20 at 21:40
9

Install abootimg (available as a package, for example in Debian/Ubuntu and openSUSE).

To extract (boot|recovery).img:

$ abootimg -x (boot|recovery).img
$ ls
boot.img  bootimg.cfg  initrd.img  zImage

To repack (boot|recovery).img after modifying one of bootimg.cfg, zImage or initrd.img:

abootimg --create (boot|recovery).img -f bootimg.cfg -k zImage -r initrd.img
Stephen Kitt
  • 411,918
  • 54
  • 1,065
  • 1,164
user199575
  • 91
  • 1
  • 1
  • This project is not maintained and did not work with my boot.img file, however the answer from @cfig worked. – Étienne Jul 05 '23 at 10:11
6

boot.img is not a compressed filesystem image like system.img. It is read by the bootloader, and contains little more than a kernel image and a ramdisk image.

Some binary distribution ship the kernel and ramdisk images separately. In that case you don't need to do anything with boot.img, just regenerate a new one with mkbootimg.

If you need to extract information from a boot.img, try split_bootimg (by William Enck, via the Android wiki).

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
3

You can use the following tool to extract and re-pack Android boot image

$ git clone https://github.com/cfig/Android_boot_image_editor.git

copy your boot.img to the cloned git repository. Run:

$ ./gradlew unpack

First time run will need to download necessary libs from internet, be patient. You can get the contents at "build/unzip_boot/", like this:

build/unzip_boot/
├── bootimg.json (boot image info)
├── kernel
├── second (2nd bootloader, if exists)
├── boot.img.avb.json (AVB only)
└── root
cfig
  • 31
  • 1