GrabDuck

[HOW-TO] unpack/modify/repack/pack kernels for noobs

:

Hello, everyone!
This is intended for everyone who want to modify kernels they are using. Since I could not find tutorials on how to repack kernels for our Novathor devices, I decided to write my own tutorial. Maybe there are other ways, but this is how I do it and it is 100% failsafe. I took transcendence CM 1.1.6 kernel as an example in this tutorial. So, let's start!

Requirements:

- Flashtool
- Linux (Ubuntu preferable), you can also use builduntu if you don't want to install linux to your hard drive
- Files provided in attachment
- Not to close terminal in linux at any price
- Brains

STEP 1: Unpacking

First of all, you want to unpack the kernel. This is the easiest one.

Fire up Flashtool, select tools/extractors/elf
Easy, right? Now, click on those three dots and find kernel you wish to modify. Kernel MUST have .elf extension for this to work, so if you have "boot.img" at you disposal, please rename it to "kernel.elf" (without quotes). Once you loaded your kernel, just click unpack. Do not close Flashtool yet. In it you will see logs that kernel is unpacked. Please take a screenshot of that window as we will need that report later. Screenshot should look like this:

After you have saved screenshot somewhere, look in the folder where your kernel is, and you will find two or three files additional files, sometimes even four, depending on the kernel itself. They should be named this way:

kernel.elf.ramdisk.gz (very rarely, this file may be called kernel.elf.2)
kernel.elf.Image (sometimes this file may be called kernel.elf.1)
kernel.elf.3
kernel.elf.cmd

It depends on the kernel how much files you're going to have. Now, please rename them, so that you delete kernel.elf from names. Now they should be named like this:

ramdisk.gz
Image

3
cmd

Bolded files are needed for our modifications. Now to the next step!

STEP 2: Modifying

Now that we have needed files at our disposal, it's time to do the modifications.

Image file
This file is zImage of the kernel. This is the core of the kernel, where CPU frequencies, drivers, governors, etc. are stored. This file you cannot really modify but if you have built your own specific zImage, you can replace it. I don't recommend touching this file unless you know what you're doing (in other words, unless you're developer or programmer).
So, noobs: don't modify this file; pros: Go ahead

ramdisk.gz
You want to change that sweet boot image, that's why you want to unpack kernel in the first place? On how to create boot image, go here.

This file contains the rest of the tweaks developers apply to kernel. Here you will find CWM, bootsplash image, autoroot files, etc, all depending on kernel you are modifying. Now, hot to unpack this bloody file? some suggest to use 7zip, but this way you will break symlinks and your kernel will definitely not boot. Proper way of unpacking ramdisk.gz goes like this:

First of all, boot up linux (or load builduntu into virtual machine, your choice).
We want to set up working folders, right? Fire up terminal and enter following commands, one line at the time:

Quote:

mkdir kernel-working
mkdir ramdisk-working
cd ramdisk-working
mkdir ramdisk-contents
cd ramdisk-contents

Don't close terminal, or I will break you fingers Now in your home folder you will find folders we've just created. Nice, isn't it? Remember those files we've unpacked earlier? copy them to these folders. ramdisk.gz goes to ramdisk-working folder, while the rest goes to kernel-working folder.
Download kernel_repack.zip from the attachment und unpack files which are in it to kernel-working folder.
Now we're all set up for modifying

Go back to the terminal. Copy and paste following command and run it:

Quote:

gunzip -c ../ramdisk.gz | cpio -i

Again, don't close the terminal. This time I will break your legs if you do so :P Now ramdisk.gz is unpacked in ramdisk-contents folder (which is in ramdisk-working folder). There you will find bunch of files and folders. After you have done all your modifications, it's time to pack it up. To do so, run the following command in terminal:

Quote:

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

In ramdisk-working folder you will find newramdisk.gz file, besides ramdisk.gz. Copy newramdisk.gz to kernel-working folder and rename it to ramdisk.gz.

Now all files for compiling new kernel are prepared. To the next and final step.

STEP 3: Packing kernel back

In terminal, run following commands (and yes, you run cd .. twice in a row):

Quote:

cd ..
cd ..
cd kernel-working

Remember that screenshot we've took earlier? This is where we need it Told you so
And finally, here is the mother of all commands which neatly packs all of our files back into kernel, but don't use it just yet, I will explain why:

Quote:

sudo python mkelf.py -o kernel.elf Image@0x00008000 ramdisk.gz@0x01000000,ramdisk 3@0x00000000,cmdline

Take a look at the my screenshot and you will see that each of the files have its own adress. For example Image is at the adress 0x00008000 and ramdisk.gz is at the adress 0x01000000,ramdisk and 3 is at the adress 0x00000000,cmdline. By writing proper names and adresses, I'm telling computer where to pack files inside the kernel. You will have to change adresses in this command according to your screenshot, because not all kernel have the same adresses for same files. This is the most important step because if you do this wrong, kernel will not properly compile!

If you did all the work correctly, in kernel-working folder you will find new kernel.elf file. If it was named boot.img before editing, rename it back, if it was named kernel.elf then leave as it is. If you want, you can test if you packed kernel properly by trying to unpack it in Flashtool. If flashtool reports that you have some files to unpack, than all is OK, if it reports nothing, then something went wrong.

Ta-da! Your kernel is finally packed and is ready to be flashed to your phone. Hopefully, if you didn't messed up something when you did your modifications and alterations to ramdisk.gz, it should properly boot.
You can flash kernel in your preffered way.

Final words:
I tried to make this tutorial as user-friendly as possible. It is pretty straightforward once you get into it.
I hope that I clarified some thing, and more importantly, that I helped you.
Cheers everyone, and happy, repacking/flashing/modifying