How to unpack and repack boot and ramdisk files easily

extracted ramdisk file

Considering that firmware modification has been as old as the Android OS itself, getting into the game for newbies can be quite intimidating. For instance, flashing certain custom ROMs require multiple steps, such as extracting the official boot image and flashing it on to the device before finally completing the process and flashing the custom ROMs files along with it.

While in some guides you’ll find online include the boot.img files included other times you might need to modify the boot image yourself and then repack it. Since the Android operating system for mobile phones is derived from Linux, the only way to do so is using a Linux-based platform or emulating one on your Windows PC.

Scripts to unpack and repack the boot.img file

XDA user CNexus sieved through the internet to find and compile a list of tools for unpacking and repacking the boot image file, all of which can be downloaded using the link given below.

→ Download Boot Image Tools

Commands you should know

  1. boot_info file included in the package allows you to the base address and the ramdisk address of the boot.img file which you will later need for repackaging.
  2. split_boot (split_bootimg.pl) helps you extract the ramdisk and zImage from the boot.img file into a separate folder.
  3. unpack_ramdisk allows you to unpack the ramdisk file using the following command:
    Usage: unpack_ramdisk <ramdiskFile>
  4. repack_ramdisk feature will let you repack the ramdisk file from the given directory using the following command:
    Usage: repack_ramdisk <ramdiskDirectory> [outputFile]
  5. mkbootimg command helps you create a brand new boot.img file from the ramdisk and zImage files you have (useful for creating modified boot.img files).
  6. umkbootimg does the very opposite and splits up the kernel and boot image.
  7. unpack will simply extract the boot.img file in one directory and the ramdisk in a separate directory for convenience.

Windows compatibility: Things to know

  1. If you don’t have a Linux operating system up and running, you can emulate it on your Windows PC using this official guide.
  2. The tool has been tested to work with Debian but not confirmed to work with Cygwin.
  3. All of the scripts in the .ZIP file must be saved in a folder in the path of the Linux system you’re running for them to be detected (example: ~/bin)

How to use the unpack and repack boot and extract ramdisk file

Well, we got to use the split_boot file as it holds the script to first get unpack the boot.img file, get the ramdisk file from it, and then extract the ramdisk file.

  1. Download the boot tool from here.
  2. Now, extract it to a separate folder to get these files.
    boot img unpack tools
  3. Now, provide the required permission to these files. So, run the command below in a folder where you have all the files.
    chmod +x boot_info mkbootimg repack_ramdisk split_boot unpack_ramdisk

    file permissions boot tool

  4. Place the boot.img file that you want to extract in the same folder as these files on your PC.
  5. To extract the boot image file and ramdisk file, run the command below.
    ./split_boot boot.img
  6. This will create a new folder called boot in your current folder. The boot folder will contain the boot image kernel file, the ramdisk file, and a ramdisk folder that contains data extracted from the ramdisk file.
    extracted boot image file
  7. That’s it. Make changes to the ramdisk folder’s file as you need.
  8. Copy all the main files of the tool (boot_info, mkbootimg, repack_ramdisk, split_boot, and unpack_ramdisk) to the boot folder which contains the ramdisk folder.
  9. Repack the ramdisk file using the command below. This will create a new ramdisk file called new-boot.img-ramdisk.cpio.gz in the boot folder. (You can change the output filename from new-boot.img-ramdisk.cpio.gz to any of your choice.)
    ./repack_ramdisk ramdisk new-boot.img-ramdisk.cpio.gz
  10. Repack the boot file using the command below. This will create a new boot image file called new-boot.img in the boot folder.
    ./mkbootimg --kernel /var/www/html/dl/boot-tool/boot/boot.img-kernel --ramdisk new-boot.img-ramdisk.cpio.gz -o new-boot.img
  11. That’s it. Job done! You have successfully unpacked and repacked the boot and ramdisk files. Your directory would look like this now. Notice the new boot and ramdisk files in the screenshot below.
    repacking boot image and ramdisk

So, how was it? Were you able to use the tools easily?

While most mainstream users will seldom find the need to unpack, modify and repack the boot.img file even when flashing a custom firmware, it’s handy to have this tool at your disposal either way.

Posted by
Husain

Sharing a love-hate relationship with Biotechnology in college, Husain moved on to his true calling of being a tech geek through and through. While briefly being persuaded by the iPhone, he made it back to the Android camp with the OnePlus One and has been a loyal comrade since.