5,600,292 Members 36,299 Now Online
XDA Developers Android and Mobile Development Forum

[DEV][TOOLS] Unpack Repack boot.img (Kernel) Files And Modify Ramdisk

Tip us?
 
iridaki
Old
(Last edited by iridaki; 4th February 2012 at 03:20 AM.)
#1  
iridaki's Avatar
Retired Forum Moderator / Recognized Themer - OP
Thanks Meter 4622
Posts: 3,675
Join Date: Feb 2007
Location: Athens

 
DONATE TO ME
Talking [DEV][TOOLS] Unpack Repack boot.img (Kernel) Files And Modify Ramdisk

Hello!

This is -at least I hope it is- an easy to follow guide on how to unpack boot.img files (aka custom kernels) and modify the ramdisk.
Now, why would you want to do that?
First and simplest thing that comes to mind is for editing the kernel boot logo (the one that appears before the boot animation).
There are also a lot of other things you can edit in the ramdisk, but they require a bit more advanced knowledge, so we won't talk about them here.

Requirements:
1. Linux (any form will do, for example virtual box etc)
2. Perl (already included in Ubuntu, me thinks)
3. The Xperia Boot Tools package attached in this post
4. The mkbootimg binary (I already compiled it for Linux x86 and included it in the attachment)
5. Reading this post VERY VERY VERY carefully

Step 1: The mkbootimg binary

Let's install mkbootimg (we need it in order to repack the boot.img).
In a terminal window, cd to the directory where you extracted the mkbootimg file and type:

Code:
sudo cp mkbootimg /bin/
And then

Code:
sudo chmod 755 /bin/mkbootimg
Done!

* After first command, terminal will ask for your user password, type it, press enter and the command will be executed immediately.

Step 2: Splitting the boot.img

At this point, we need a boot.img file to play with.
Pick your favorite custom kernel and get the .img.
Place it in a folder along with the 2 perl scripts from the attached package.
In a terminal window, cd to the above-mentioned folder.
Next, type this command:

Code:
perl split_bootimg.pl boot.img
The command will return something like this:
Page size: 2048 (0x00000800)
Kernel size: 3132176 (0x002fcb10)
Ramdisk size: 3484496 (0x00352b50)
Second size: 0 (0x00000000)
Board name:
Command line:
Writing boot.img-kernel ... complete.
Writing boot.img-ramdisk.gz ... complete.


Now the kernel is successfully extracted to your current directory.
It consists of 2 files: boot.img-kernel and boot.img-ramdisk.gz.

Step 3: Unpacking ramdisk

So we unvealed the ramdisk in our previous step but it is still of no use to us, so we need to un-gzip and then un-cpio it.
In the same terminal window as before, type:

Code:
mkdir ramdisk


Code:
cd ramdisk
Code:
gzip -dc ../boot.img-ramdisk.gz | cpio -i
Last command will return something like:

10503 blocks

Our ramdisk is unpacked now in the newly created /ramdisk folder.

Step 4: Editing ramdisk files

Now you can edit the contents of the ramdisk.
BE EXTREMELY CAREFUL AND NEVER EVER DO ANYTHING "JUST 'CAUSE"...
You need to be fully aware of what you're doing.
Good news is, changing the logo.rle (the static boot logo image) is fairly easy.
You just need to replace it with your .rle file.
I am not going to explain how to make your own .rle files right now.
Feel free to have a look at this thread for more info.
However, I attach the stock SE boot logo in case anyone misses it when he's on custom kernel.

IMPORTANT: Don't accidentally add irrelevant files to the ramdisk directory as cpio will include them too and your new boot.img will be useless.

Also, bear in mind:

Quote:
Originally Posted by DooMLoRD View Post
there is a bug in the semc S1 bootloader... sometimes if the boot.img isnt of a correct size then the device will not boot... the workaround is to add a "filler" file (which is say 512KB file with junk data) to the ramdisk so as to increase the size of the final boot.img file...

thanks to jerpelea for this incredible tip... i must have wasted atleast 10-15 hrs trying to figure out why my kernel failed to boot :P
Step 5: Repack the ramdisk

Now that's all done, we need to pack the kernel back up into a flashable img file.
First, we will pack the ramdisk back to it's original state.
In the same terminal window as before, type:

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

Step 6: Repack the boot.img

In our last step, back into the terminal we go to use the "repack-bootimg" perl script that will give us our brand new boot.img.
Make sure to cd to the directory that the perl scripts are, much like in Step 1.
Type this command:

Code:
perl repack-bootimg.pl '/.../.../.../boot.img-kernel' '/.../.../.../ramdisk' newboot.img
Of course, replace /.../.../ with the path to the boot.img-kernel file and the ramdisk directory respectively.

All done! Your new img is newboot.img and is ready to be flashed!

ADVICE: Use fastboot boot and not fastboot flash boot the first time, to test that the new kernel boots. If all is well, proceed with the actual flashing!

FAQ:

- Can I do this on Windows?
- Not that I know of, no. You can use Vmware or VirtualBox to install a raw Ubuntu image (pure command line) on your Windows PC though.
- Great! How can I do this then?
- Wish I knew! I have a Linux partition on my PC, it has been like that forever. Google is your friend!

- This is so complicated, is there an easier way? Can you make a zip for me to use in recovery?
-
- OK, don't get upset. Can you do it for me?
- I will have to say no, because I know that the minute word gets out that I take requests, I will be overwhelmed.
Took me several hours to figure all this out but it will take you less than 10 minutes to do it yourself if you carefully follow the instructions.

- I think this or that is wrong and it should be like this or that.
- Thank you very much for correcting me! I am still learning! Please post your corrections here or PM me and I will update the thread ASAP!

References:

- HOWTO: Unpack, Edit, and Re-Pack Boot Images
- How to Port Android to Another Device
- [How-To] Make custom bootscreen/bootlogo (image to rle)/(rle to image)

Thanks to:

- Riyal for giving me the idea to get into this.
- pvyParts because without him I'd only be "pulling Irises"...


Cheers!
Attached Files
File Type: zip xperiaboottools.zip - [Click for QR Code] (18.8 KB, 7818 views)
File Type: zip default_SE_bootlogo.zip - [Click for QR Code] (1.3 KB, 3316 views)
The Following 101 Users Say Thank You to iridaki For This Useful Post: [ Click to Expand ]
 
doomed151
Old
#2  
doomed151's Avatar
Senior Member
Thanks Meter 2047
Posts: 1,341
Join Date: Dec 2009
Thank you very much. I've been looking for something like this.

Anyways, use VirtualBox instead of VMware. It's a thousand times better.
If I don't reply to your PM, it simply means that I've nothing useful to say.
The Following 3 Users Say Thank You to doomed151 For This Useful Post: [ Click to Expand ]
 
rashid.fairus
Old
(Last edited by rashid.fairus; 3rd February 2012 at 10:42 AM.)
#3  
rashid.fairus's Avatar
Senior Member
Thanks Meter 229
Posts: 1,057
Join Date: Dec 2011
Location: Klang
or try pendrivelinux,boot linux from windows..great tutorial,gonna try it
so everyone can be doomlord
question,if i manage to change boot image and repack the it back,is there any chance for me to "brick" the kernel?
Sent from my LT18i
The Following User Says Thank You to rashid.fairus For This Useful Post: [ Click to Expand ]
 
glaudrem
Old
#4  
Member
Thanks Meter 21
Posts: 88
Join Date: Nov 2011
Location: Valencia
Angry Locked Bootloader

Hello Iridaki,

many thanks for your apports (this and the init.d script). I'm actually user from a Spanish forum called www.htcmania.com (you may have heard about thedr0idboy, which is a Neo developer) and we were trying to pack kernels in order to flash it in locked bootloader devices or at least try to create an Overclock module for stock kernel that can be flashed in locked devices.

Do you think we can use this to achieve this goal? I just hate Arc S with it's damn 1.4 GHz processor :S. It was released only a month after I got my Arc.

Many thanks for your time and your efforts!!


P.D: a girl developer!!!
The Following User Says Thank You to glaudrem For This Useful Post: [ Click to Expand ]
 
iridaki
Old
#5  
iridaki's Avatar
Retired Forum Moderator / Recognized Themer - OP
Thanks Meter 4622
Posts: 3,675
Join Date: Feb 2007
Location: Athens

 
DONATE TO ME
Quote:
Originally Posted by rashid.fairus View Post
so everyone can be doomlord
question,if i manage to change boot image and repack the it back,is there any chance for me to "brick" the kernel?
Nah, it takes a lot more to be DoomLord..!!!
If there are no errors in the cmd, boot.img should be OK.
By using flashboot boot command, the kernel is sent to the phone and it tries to boot it. It's not flashing it, so if it fails, it immediately goes back to previous kernel.
So don't worry! Imagine that until I found the correct script, I had tried more or less 10 crapernels (crappy kernels)!!!

iRĄD@k!* via Tapatalk
The Following User Says Thank You to iridaki For This Useful Post: [ Click to Expand ]
 
rashid.fairus
Old
#6  
rashid.fairus's Avatar
Senior Member
Thanks Meter 229
Posts: 1,057
Join Date: Dec 2011
Location: Klang
Quote:
Originally Posted by iridaki View Post
Nah, it takes a lot more to be DoomLord..!!!
If there are no errors in the cmd, boot.img should be OK.
By using flashboot boot command, the kernel is sent to the phone and it tries to boot it. It's not flashing it, so if it fails, it immediately goes back to previous kernel.
So don't worry! Imagine that until I found the correct script, I had tried more or less 10 crapernels (crappy kernels)!!!

iRĄD@k!* via Tapatalk
looks like our themes and apps forum have a new section,kernel theming,,(iridaki,you have purple theme for apps and roms,how about kernel?)

Sent from my LT18i
 
dp94
Old
#7  
dp94's Avatar
Senior Member
Thanks Meter 123
Posts: 629
Join Date: Feb 2011
Location: Linz
A big thanks, iridaki!

I was looking forward to such thread.
I did also a lot of search on google too, but somehow I couldn't find any match.
I am an experienced Linux user, so that shouldn't be a problem for me.
I'll try everything this weekend.
Afaik, CWM is also stored in the kernel, so is it possible to modify it with the tool?
I don't want to have a step by step guide, just a reference would be great (of course if you know one).

Anyway, thanks for your great contributions in XDA.

Sent from my LT18i using xda premium
Nokia Lumia 920
The Following User Says Thank You to dp94 For This Useful Post: [ Click to Expand ]
 
theduke89
Old
#8  
theduke89's Avatar
Senior Member
Thanks Meter 71
Posts: 403
Join Date: Feb 2011
OMG... :-o Iris you are a genius.

Sent from my Arc S LT18i using Xda Premium App


 
DooMLoRD
Old
#9  
DooMLoRD's Avatar
Recognized Developer
Thanks Meter 34193
Posts: 12,245
Join Date: Jul 2010
Location: Pune, Maharashtra
Quote:
Originally Posted by glaudrem View Post
Hello Iridaki,

many thanks for your apports (this and the init.d script). I'm actually user from a Spanish forum called www.htcmania.com (you may have heard about thedr0idboy, which is a Neo developer) and we were trying to pack kernels in order to flash it in locked bootloader devices or at least try to create an Overclock module for stock kernel that can be flashed in locked devices.

Do you think we can use this to achieve this goal? I just hate Arc S with it's damn 1.4 GHz processor :S. It was released only a month after I got my Arc.

Many thanks for your time and your efforts!!


P.D: a girl developer!!!

currently we cant use custom kernels on locked bootloader...

there is a way to make overclock module for stock kernel... i am looking into that possibility & i have contacted a few devs... i will work on it when i get time...


@Iris


there is a bug in the semc S1 bootloader... sometimes if the boot.img isnt of a correct size then the device will not boot... the workaround is to add a "filler" file (which is say 512KB file with junk data) to the ramdisk so as to increase the size of the final boot.img file...

thanks to jerpelea for this incredible tip... i must have wasted atleast 10-15 hrs trying to figure out why my kernel failed to boot :P
Like my work? Please consider donating via PayPal

Lead Developer of Team UtterChaos & Member of Team ICX

Creator of DooMKernel making those Snapdragons fly higher and faster

Android Devices I use : Sony Xperia Z1, Z, SP, T | WISHLIST: Sony Xperia Z2

Contributions : Generic Android / Xperia X10 / Xperia 2011 devices / Samsung Galaxy S2 / Huawei Ideos X6 / Samsung Galaxy SCL (i9003) / HTC Flyer / Kindle Fire / HTC JetStream / Xperia P, Sola, U / HTC One X / Xperia S / Xperia T, V, TX / Xperia Z, ZL, Tablet Z / Xperia SP / Xperia Z1 / Sony ZU GPe / Xperia Z1 Compact / Xperia Z2, Z2 Tablet / Xperia T2 Ultra

My Apps on Google Play Store | Follow me on Twitter!
The Following 4 Users Say Thank You to DooMLoRD For This Useful Post: [ Click to Expand ]
 
iridaki
Old
(Last edited by iridaki; 4th February 2012 at 03:21 AM.)
#10  
iridaki's Avatar
Retired Forum Moderator / Recognized Themer - OP
Thanks Meter 4622
Posts: 3,675
Join Date: Feb 2007
Location: Athens

 
DONATE TO ME
Quote:
Originally Posted by DooMLoRD View Post
@Iris


there is a bug in the semc S1 bootloader... sometimes if the boot.img isnt of a correct size then the device will not boot... the workaround is to add a "filler" file (which is say 512KB file with junk data) to the ramdisk so as to increase the size of the final boot.img file...

thanks to jerpelea for this incredible tip... i must have wasted atleast 10-15 hrs trying to figure out why my kernel failed to boot :P
Great tip indeed!!! Added to the OP!
I haven't come across the issue yet because I am only editing some tweaks and the logo.rle but say someone uses a much smaller logo and deletes some scripts, would he need to use a filler file, even if it is for 20-30 KB?
THANKS!

Tags
boot.img, logo.rle, perl, repack, unpack
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes