• Introducing XDA Computing: Discussion zones for Hardware, Software, and more!    Check it out!

[GUIDE]Building Mediatek boot.img{Appending headers}

Whats missing in the thread?

  • Nothing, Thread's awesome!

    Votes: 10 47.6%
  • Lack of informations

    Votes: 11 52.4%
  • Wrong information

    Votes: 0 0.0%

  • Total voters
    21
Search This thread

patraanjan23

Senior Member
Nov 1, 2012
186
131
So I'm trying to build Cyanogenmod for Mediatek MT6582 based phone. The problem I have is I can't make a working recovery image from the build system. Also while unpacking a working recovery using mtk unpack tool I see the kernel header is 2KB for the recovery and 512 bytes for the ramdisk. I'm using a custom boot.mk file for adding kernel header to the images inside the build process but It's creating a 512 B header for the kernel. I don't know if that's the reason I'm never booting into recovery mode while flashing a compiled image. Also I tried repacking it but I think it did not work :3 Can you suggest some advanced stuff? I'm not a beginner and have built kernels n recoveries n cm before. Just new to MTK. Thanks :)
 

elyts

Senior Member
Apr 13, 2012
93
12
Guide to Mediatek Building(Guide 2:=boot.img)

A common misconception on XDA is that mediatek devices have boot.img similar to qualcomm.. I'll try to explain why it isn't and how to make one.

Mediatek appends a header to its Ramdisk(initramfs) and its kernel so that the board can figure out that its a ramdisk/kernel. Without these headers the boot.img you build will won't work; If you've been getting bad results when making a boot.img this could be one of the reasons

Requisite
Any Linux based system
the mkimage i've attached in the post




Adding the MediaTek header

A executable named the 'mkimage' is needed for this(attached in post). If you're one of the kernel devs or for the ones who have the mediatek kernel source, this file is present in the kernel_source/mediatek/build/tools/mkimage.

What does it do?
it appends the mediatek header to the first 512 bytes of your ramdisk/kernel


Adding the meditek header
For the kernel,
Code:
mkimage kernel KERNEL > kernel_with_meditek_header

For the ramdisk,
Code:
mkbootfs ./root/ | minigzip >ramdisk.gz
mkimage ramdisk.gz ROOTFS > ramdisk.img

Removing the MediaTek header

If you want to remove header from kernel,
Code:
dd if=kernel of=kernel_without_header bs=1 skip=512
If you want to remove header from ramdisk
Code:
dd if=ramdisk of=ramdisk_without_header bs=1 skip=512

the bs tells dd how many bytes to read and write at a time.
the skip tells it that how many bytes to skip at start. here its 512 since the mediatek header occupies 512bytes. we can use count if you want

Here's how you manually pack boot.img after adding headers
Code:
mkbootimg --kernel zImage_with_header --ramdisk ramdisk_with_header.img -o new_boot.img

You can now use this boot.img to flash to your phone

Modules!!
Modules are very important, Read previous guide(Building kernel) about how to build modules(Guide attached to signature). Modules are very kernel specific and the rom wont boot if you don't push these modules to the /system/lib/modules replacing the ones currently there.

Extra Information!

to delete parts of file from the end of file which is 100 bytes. for example:
Code:
dd if=file of=file1 bs=1 skip=10 count=85
/*count=filesize-startsskip-endskip*/
here,count=100-10-5=85.

Hope this guide helped you.

All questions are welcome in the thread

Regards and Good luck

Links to my next Guides
Building the ROM{MediaTek}
Building the Kernel{MediaTek}

I successfully compiled a zImage from my mobiles source and want to make the boot.img now. Could you help me with the following things:
- mkimage kernel KERNEL > kernel_with_meditek_header: is "kernel" a reference to my zImage? What is "KERNEL"? Just an argument for mkimage? I copied my zImage to the same folder as mkimage and ran "mkimage zImage KERNEL > zImage_with_mediatek_header. Is that correct or do I have to replace "KERNEL" with something else?

- mkbootfs ./root/ | minigzip >ramdisk.gz: First off: Is this step specific for building a ROM? Where do i find mkbootfs? What directory is ./root/ ? What should be inside ./root/?
 

MasterAwesome

Recognized Developer
Jun 23, 2013
2,017
3,985
Appending headers

I successfully compiled a zImage from my mobiles source and want to make the boot.img now. Could you help me with the following things:
- mkimage kernel KERNEL > kernel_with_meditek_header: is "kernel" a reference to my zImage? What is "KERNEL"? Just an argument for mkimage? I copied my zImage to the same folder as mkimage and ran "mkimage zImage KERNEL > zImage_with_mediatek_header. Is that correct or do I have to replace "KERNEL" with something else?

- mkbootfs ./root/ | minigzip >ramdisk.gz: First off: Is this step specific for building a ROM? Where do i find mkbootfs? What directory is ./root/ ? What should be inside ./root/?

Before I start, please dont quote the entire post thread. Come back to the topic on hand, mkimage(Source code here) adds a header to the kernel. In simpler words, Mediatek devices process the header and find out what the image is for. "KERNEL" the 2nd parameter refers to the header. Let me explain the synax

Code:
mkimage "kernel_you_build" KERNEL > "output-kernel-with-the-header-"KERNEL""

So basically your kernel has the first few bytes of it with the kernel header. This can be viewed by using a binary editor in linux use ghex.
To remove the kernel header you need to remove the starting 512bytes of the kernel binary which can be done using 'dd' command like

Code:
dd if=kernel of=kernel_without_header bs=1 skip=512

Regards
 
  • Like
Reactions: z3ntu

bigrammy

Senior Member
Apr 8, 2011
2,939
2,549
huddersfield
Before I start, please dont quote the entire post thread. Come back to the topic on hand, mkimage(Source code here) adds a header to the kernel. In simpler words, Mediatek devices process the header and find out what the image is for. "KERNEL" the 2nd parameter refers to the header. Let me explain the synax

Hi MasterAwesome,

Sorry for the noobish question bro but I am new to Linux and the strange ways of the MTK boot images :eek:
I have a couple of problems at the moment both related to the ways of correctly unpacking and repacking the mtk images both boot and recovery.
I do not wish to boar you with the full story so I will cut to the chase in bullet points.

1: How to correctly Unpack a stock mtk recovery/boot.img so we can edit ramdisk (init.rc) using Linux.
2: How to correctly Re-pack a stock mtk recovery/boot.img after we edit ramdisk (init.rc) using Linux.
I know I can use Bruno's Perl scripts for the above but I would like to know the official way of doing this without any need of Bruno's Scripts.
Can you give me the full commands used and list any other MTK specific tools like the mkimage you already listed.

Reasons:
1: I would like to gain full support for MTK devices added to ArchiKitchen Here and handling the mtk boot.img's using the correct Linux methods is preferred. :good:
2: Problems with the New Zopo mt6595 recovery and boot.img's Which seem to break after unpacking and repacking even without editing (possible compression method :eek:) Thread Here

Sorry if this is all covered in another tread I have missed on xda.
Regards. bigrammy
 

[email protected]

Senior Member
May 8, 2015
98
14
page.nwrk.biz
Does the mediatek build script (makeMTK) add the headers to the kernel, or do I still have to add them by myself?
----
EDIT: Yes makeMTK adds the header to the kernel! If the kernel is built with the mediatek scripts no further patching is required!
 
Last edited:

zadanar

New member
Oct 2, 2017
2
0
help MasterAwesome and XDA...

i have acer liquid x1 bricked no recovery, can you help me how to build images? i have kernel source code and i build my kernel with cross compile and success, but i dont`t know how to build boot.img and recovery.img.. please help me XDA people and MasterAwesome.... im using linux ubuntu 16.04 LTS...
open

open

open
 
Last edited:

Top Liked Posts

  • There are no posts matching your filters.
  • 27
    Guide to Mediatek Building(Guide 2:=boot.img)

    A common misconception on XDA is that mediatek devices have boot.img similar to qualcomm.. I'll try to explain why it isn't and how to make one.

    Mediatek appends a header to its Ramdisk(initramfs) and its kernel so that the board can figure out that its a ramdisk/kernel. Without these headers the boot.img you build will won't work; If you've been getting bad results when making a boot.img this could be one of the reasons

    Requisite
    Any Linux based system
    the mkimage i've attached in the post




    Adding the MediaTek header

    A executable named the 'mkimage' is needed for this(attached in post). If you're one of the kernel devs or for the ones who have the mediatek kernel source, this file is present in the kernel_source/mediatek/build/tools/mkimage.

    What does it do?
    it appends the mediatek header to the first 512 bytes of your ramdisk/kernel


    Adding the meditek header
    For the kernel,
    Code:
    mkimage kernel KERNEL > kernel_with_meditek_header

    For the ramdisk,
    Code:
    mkbootfs ./root/ | minigzip >ramdisk.gz
    mkimage ramdisk.gz ROOTFS > ramdisk.img

    Removing the MediaTek header

    If you want to remove header from kernel,
    Code:
    dd if=kernel of=kernel_without_header bs=1 skip=512
    If you want to remove header from ramdisk
    Code:
    dd if=ramdisk of=ramdisk_without_header bs=1 skip=512

    the bs tells dd how many bytes to read and write at a time.
    the skip tells it that how many bytes to skip at start. here its 512 since the mediatek header occupies 512bytes. we can use count if you want

    Here's how you manually pack boot.img after adding headers
    Code:
    mkbootimg --kernel zImage_with_header --ramdisk ramdisk_with_header.img -o new_boot.img

    You can now use this boot.img to flash to your phone

    Modules!!
    Modules are very important, Read previous guide(Building kernel) about how to build modules(Guide attached to signature). Modules are very kernel specific and the rom wont boot if you don't push these modules to the /system/lib/modules replacing the ones currently there.

    Extra Information!

    to delete parts of file from the end of file which is 100 bytes. for example:
    Code:
    dd if=file of=file1 bs=1 skip=10 count=85
    /*count=filesize-startsskip-endskip*/
    here,count=100-10-5=85.

    Hope this guide helped you.

    All questions are welcome in the thread

    Regards and Good luck

    Links to my next Guides
    Building the ROM{MediaTek}
    Building the Kernel{MediaTek}
    3
    Kernel Development guide coming today :)
    2
    Nice guide :)

    ... but that was already explained on this thread
    The thread was for the MT6577, but concerning this issue (and others) its also valid for the other's MT65xx ; MT81xx ; MT83xx SoC's.

    Also this thread is the best resource on @xda concerning Mediatek kernel / issues , etc.

    But this should help, the users that are new on the "Mediatek" Platform.



    Regards
    2
    Nice guide :)

    ... but that was already explained on this thread
    The thread was for the MT6577, but concerning this issue (and others) its also valid for the other's MT65xx ; MT81xx ; MT83xx SoC's.

    Also this thread is the best resource on @xda concerning Mediatek kernel / issues , etc.

    But this should help, the users that are new on the "Mediatek" Platform.



    Regards

    I'm making a guide for building an rom from source step by step for the new developers. I've started off with the boot.img. Not sure if that's the way to go, but I'll try my best to make people understand the build process and how mediatek works :)

    Sent from my Moto G using XDA Free mobile app
    1
    Appending headers

    I successfully compiled a zImage from my mobiles source and want to make the boot.img now. Could you help me with the following things:
    - mkimage kernel KERNEL > kernel_with_meditek_header: is "kernel" a reference to my zImage? What is "KERNEL"? Just an argument for mkimage? I copied my zImage to the same folder as mkimage and ran "mkimage zImage KERNEL > zImage_with_mediatek_header. Is that correct or do I have to replace "KERNEL" with something else?

    - mkbootfs ./root/ | minigzip >ramdisk.gz: First off: Is this step specific for building a ROM? Where do i find mkbootfs? What directory is ./root/ ? What should be inside ./root/?

    Before I start, please dont quote the entire post thread. Come back to the topic on hand, mkimage(Source code here) adds a header to the kernel. In simpler words, Mediatek devices process the header and find out what the image is for. "KERNEL" the 2nd parameter refers to the header. Let me explain the synax

    Code:
    mkimage "kernel_you_build" KERNEL > "output-kernel-with-the-header-"KERNEL""

    So basically your kernel has the first few bytes of it with the kernel header. This can be viewed by using a binary editor in linux use ghex.
    To remove the kernel header you need to remove the starting 512bytes of the kernel binary which can be done using 'dd' command like

    Code:
    dd if=kernel of=kernel_without_header bs=1 skip=512

    Regards