• 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

MasterAwesome

Recognized Developer
Jun 23, 2013
2,017
3,985
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}
 

Attachments

  • mkimage.zip
    3.1 KB · Views: 2,011
Last edited:

superdragonpt

Recognized Developer
Apr 27, 2013
4,923
19,266
Lisbon / Taiwan / USA
www.caixamagica.pt
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
 

MasterAwesome

Recognized Developer
Jun 23, 2013
2,017
3,985
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
 

superdragonpt

Recognized Developer
Apr 27, 2013
4,923
19,266
Lisbon / Taiwan / USA
www.caixamagica.pt
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

Your more than welcome;) we are only an few working with Mediatek's.

We all know how MTK works, and their Closed Source / Anti-GPL Atittude...

Best Regards ;)
 
  • Like
Reactions: kickban

giaur

Senior Member
Aug 27, 2013
4,079
1,174
Nice tutorial but still not clear for me. So i have built zImage, and then what? I need to use mkimage on zImage I have to produce boot.img, then push boot.img to my device?
 

MasterAwesome

Recognized Developer
Jun 23, 2013
2,017
3,985
Nice tutorial but still not clear for me. So i have built zImage, and then what? I need to use mkimage on zImage I have to produce boot.img, then push boot.img to my device?

yes you apply mkimage on the zImage and ramdisk The we mkbootimg for converting it into a boot.img and then you push to device.

MasterAwesome

Sent from my Moto G
 

giaur

Senior Member
Aug 27, 2013
4,079
1,174
Thanks, I think now it's clear. I think it would be useful for newbies if you also add this to first post. Also - don't I need any modules (.ko) also to push to device?
 

MasterAwesome

Recognized Developer
Jun 23, 2013
2,017
3,985
Thanks, I think now it's clear. I think it would be useful for newbies if you also add this to first post. Also - don't I need any modules (.ko) also to push to device?

Yes, you need to push the modules to your phones since they are kernel specific. To reduce size strip them off of unneeded assembler messages.

Regards
MasterAwesome

Sent from my Moto G
 

midi_1996

Senior Member
Aug 8, 2011
304
63
WOW! good guide, I only have one problem, if I have a premade boot.img and I want to change the kernel, how do I do that? Thanks for this guide :D
 

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