Linux unpack boot img

How to unpack boot.img modify the file and repack the image then flash

Refer my previous article “HOW TO BUILD ANDROID KERNEL AND FLASH IT TO THE TARGET” to get the boot.img for device.

you can extract the image from the device or you can download it from manufacturer’s website.

Precondition:

To decompress, modify and Re-compress the boot image you need following tools.

Download thoes tools into a folder:

$ mkdir down_tools

$ mkdir tool-chain

$ cd down_tools

$ git clone https://github.com/osm0sis/mkbootimg.git

$ git clone https://github.com/osm0sis/mkbootfs.git

$ cd mkbootimg

$ make

$ cp mkbootimg unmkbootimg ../tool-chain

$ cd ..

$ cd mkbootfs

$ make

$ cp mkbootfs ../tool-chain

$ export PATH=tool-chain-path/bin:$PATH

Extract ramdisk from boot.img:

$ unmkbootimg -i boot.img
kernel written to ‘kernel’ (6682776 bytes)
ramdisk written to ‘ramdisk.cpio.gz’ (913311 bytes)

To rebuild this boot image, you can use the command:
mkbootimg –base 0 –pagesize 2048 –kernel_offset 0x80208000 –ramdisk_offset 0x82200000 –second_offset 0x81100000 –tags_offset 0x80200100 –cmdline ‘console=ttyHSL0,115200,n8 androidboot.hardware=flo user_debug=31 msm_rtb.filter=0x3F ehci-hcd.park=3 vmalloc=340M’ –kernel kernel –ramdisk ramdisk.cpio.gz -o boot.img

Now you will get two files – kernel and ramdisk.cpio.gz

Mark command to re-build the boot.img later.

Decompress ramdisk to the real files:

The ramdisk format is a bit odd. The files have first been concatenated into an archive using cpio (this means the files are all strung together into one single big file, but not compressed), then compressed using gzip. So, to undo all this, we need to first gunzip the ramdisk and then use cpio to extract the files from the archive. This is mentioned, for example, here.

So, make a new directory called ramdisk. cd into it. Then use a combination of gunzip and cpio to extract the initramfs.cpio.gz file. Here’s the commands:

$ mkdir ramdisk $ cd ramdisk $ gunzip -c ../ramdisk.cpio.gz | cpio -i $ ls charger file_contexts init.flo.diag.rc init.trace.rc oem sbin service_contexts ueventd.rc data fstab.flo init.flo.rc init.usb.configfs.rc proc seapp_contexts sys default.prop init init.flo.usb.rc init.usb.rc property_contexts selinux_version system dev init.environ.rc init.rc init.zygote32.rc res sepolicy ueventd.flo.rc

The -c switch on gunzip makes it send its output to the standard output (i.e. the terminal). This is then piped (the | symbol) to cpio. The -i switch with cpio makes it work in copy-in mode – it copies files in from an archive, and outputs them as real files. Now have a look at the contents of the ramdisk directory.

Читайте также:  Обновить mozilla firefox astra linux

You may have some changes to make for this directory.

Now it’s time to modify the ramdisk!

Compress the files back into a ramdisk:

I tried method from online tutorial which saying that using cpio and gzip will work for creating the ramdisk like following:

However, when carried through to completion, this does not result in a bootable image. I don’t know what mkbootfs does, but it seems irrelevant – why not just reverse the action of gunzip and cpio to get back to a ramdisk file? In fact, this is exactly what’s described here. While still in the ramdisk directory, run the following:

find . | cpio -o -H newc | gzip > ../newramdisk.cpio.gz

This consists of three parts. find . spits out (to the terminal) a list of all filenames in the current (ramdisk) directory, including files in sub-directories. This is piped to cpio. It uses the switches -o for copy-out mode, so it combines files from find . into an archive, and -H newc to specify the output format. This is then piped to gzipwhich compresses the output from cpio into a final, new ramdisk file, called newramdisk.cpio.gz. Here’s the resulting files in the recovery directory now

However that failed me with system unbootable.

so I tried with other method – using mkbootfs:

$ mkbootfs ramdisk | gzip > ramdisk-new.gz

Then finally it worked for me. don’t know reason. and didn’t have time to figure it out.

(If you’re interested, compare the file sizes of the ramdisk files produced by mkbootfs and the other command – they are different, which probably explains why the final image doesn’t boot. On mine, mkbootfs produced a file 2035130 bytes in size, while the find . | cpio -o -H newc | gzip > ../newramdisk.cpio.gz command produced 2033625 bytes.)

the tutorial says mkbootfs didn’t work out for him, but oppositely the cpio and gz didn’t work out for me.

Now let’s recompress the boot.img

$ cd ..

$ mkbootimg –base 0 –pagesize 2048 –kernel_offset 0x80208000 –ramdisk_offset 0x82200000 –second_offset 0x81100000 –tags_offset 0x80200100 –cmdline ‘console=ttyHSL0,115200,n8 androidboot.hardware=flo user_debug=31 msm_rtb.filter=0x3F ehci-hcd.park=3 vmalloc=340M’ –kernel zImage –ramdisk ramdisk-new.gz -o newboot.img

Flash the device:

$ adb reboot-bootloader

$ sudo fastboot flash boot newboot.img

$ sudo fastboot reboot

walah, you will able to see the ramdisk is changed now!

Читайте также:  Обновить биос через линукс

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

sayedather/bootimg_tools

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

prints information about the boot.img passed to it, including the base address and ramdisk address. This tool prints out everything needed to repack the boot.img correctly.

More commonly known as split_bootimg.pl, this rips apart the boot.img to extract the ramdisk and zImage. It has been modified by me to split the boot.img into a separate folder (specified by the file name of the boot.img passed to it) and to extract the ramdisk into a sub-folder as well (extracts the cpio from the gz and then extracts the actual files from the cpio archive)

unpack_ramdisk — unpacks the given ramdisk file.

repack_ramdisk — repacks the ramdisk from the given directory (found online and modified slightly to take a directory)

repack_ramdisk [outputFile] 

mkbootimg binary that creates a boot.img file from the given ramdisk and zImage. Updated to a version compiled by me to support the —ramdiskaddr option (ramdisk address) so that even nonstandard boot.img’s can be repacked correctly (Use with boot_info for best results).

included for convenience. Not made by me. Original thread here.

wrapper script made by me for the umkbootimg binary^ to unpack the boot.img into a separate directory and then unpack the ramdisk into a sub-directory.

Note: These tools were made for Linux. They may also work on Cygwin, but I have not personally tested them.

Читайте также:  Запустить службу postgresql linux

Источник

How to extract boot.img?

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 
  1. If so, Is there any other method to extract boot.img ?
  2. If not, what is the problem for not extracting boot.img ?

4 Answers 4

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

Unpack boot.img :

It contains the following steps:

wget https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/android-serialport-api/android_bootimg_tools.tar.gz 
tar xvzf android_bootimg_tools.tar.gz 

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

Getting the same error for step 3 on Mint 17 here. Tried running them by sudo and after chmod 755 to no avail.

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»

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 

This project is not maintained and did not work with my boot.img file, however the answer from @cfig worked.

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).

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:

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 

Источник

Оцените статью
Adblock
detector