Welcome to XDA

Search to go directly to your device's forum

Register an account

Unlock full posting privileges

Ask a question

No registration required
Post Reply

[GUIDE]Building a Kernel from source{Mediatek}

OP MasterAwesome

17th May 2014, 04:24 PM   |  #1  
MasterAwesome's Avatar
OP Senior Member
Thanks Meter: 798
 
1,136 posts
Join Date:Joined: Jun 2013
More
Guide to Mediatek Building(Guide 1:=Kernel)

Here, we'll be talking about how to build kernels and how they are different than the ones we've built for Qualcomm.

Requisites
A linux machine
Required Packages(Attached in 2nd post)
C programming and a knowledge about Makefiles
NDK


Lets begin,

Step 1(Setting up the build environment):=As always we have to set up the build environment so that the linux knows we are building something.
This is how we do it
Code:
export TARGET_BUILD_VARIANT=user TARGET_PRODUCT=devicename MTK_ROOT_CUSTOM=../mediatek/custom/ TARGET_KERNEL_VERSION=devicename &&export PATH=pathToDir/arm-eabi-4.6/bin:$PATH&&export CROSS_COMPILE=arm-eabi-&&export ARCH=arm
Let me explain what this does
The BUILD_VARIANT specifies what the kernel is going to be built for. A very good article i came across for explaining the build-variants is here.
The TARGET_PRODUCT/TARGET_KERNEL_PRODUCT tells the linux machine what "device specific" files to use
The MTK_ROOT_CUSTOM specifies the directory of the mediatek/custom folder. remember this mide be in the same directory as the kernel source as well.
The PATHThis sets your toolchain executables to your path. Download toolchain(preferably arm-eabi-4.6) Or, if you want to, you could build your own toolchain from source code
The CROSS_COMPILE, A cross compiler is a compiler capable of creating executable code for a platform other than the one on which the compiler is running. The toolchain facilitates this function
ARCH=arm, ARM is a family of instruction set architectures for computer processors based on a reduced instruction set computing (RISC) architecture developed by British company ARM Holdings. ARM is also used in Android. Here by typing
Code:
export ARCH=arm
we are telling linux that we are going to be building for the CPU architecture "ARM".

Thats enough theory, lets get our hands dirty

Step 2(Configuring your kernel):=This is the heart to what your phone does so be careful when you do this if you're doubtful about something dont hesitate to ask me in the thread.. the base config is usually present in the kernel_source/mediatek/config/devicename/autoconfig/kconfig/platform. this is your base config which you will use and build with different requirments(Example:SELinux). You can always build a config from the start, I advice you dont do that until you have compiled a few kernels before. for using existing config as a base
Code:
cd kernel_source
cp mediatek/config/devicename/autoconfig/kconfig/platform .config
Now,
Code:
make menuconfig
This will give a graphical interface for you to choose what features you want to add to your phone(Example:I/O Schedule,CPU Governors..) More about this here. You're now ready for compiling

Step 3(Compiling):=This is the moment of truth; This is what youve been waiting for! Now, type
Code:
make zImage
You will get something which says
Code:
arch/arm/boot/zImage Ready
Step 4(Adding MediaTek headers):=[GUIDE]Building Mediatek boot.img{Appending headers}. This is my next guide, follow this to your way to compile a whole ROM for your phone.

Now, use your source code to build a ramdisk for you by typing make bootimage. in the out folder you'll get a folder called root. This folder is the initramfs or the ramdisk. to this follow my next guide to understand how to append mediatek headers


Code:
make modules
This is almost the most important step or your ROMS wont boot. after this is complete go to your sourcedir then in search type ".ko" you'll get a bunch of .ko files copy these to your device tree/ROM to make the rom to work.

If you're moving to a device tree make sure to add this to your device_makefile
Code:
PRODUCT_COPY_FILES += \
$(MOD_SRC)/ccci.ko:$(MOD_TGT)/ccci.ko \
$(MOD_SRC)/ccci_plat.ko:$(MOD_TGT)/ccci_plat.ko \
$(MOD_SRC)/devapc.ko:$(MOD_TGT)/devapc.ko \
$(MOD_SRC)/devinfo.ko:$(MOD_TGT)/devinfo.ko \
$(MOD_SRC)/mtk_fm_drv.ko:$(MOD_TGT)/mtk_fm_drv.ko \
$(MOD_SRC)/mtk_hif_sdio.ko:$(MOD_TGT)/mtk_hif_sdio.ko \
$(MOD_SRC)/mtk_stp_bt.ko:$(MOD_TGT)/mtk_stp_bt.ko \
$(MOD_SRC)/mtk_stp_gps.ko:$(MOD_TGT)/mtk_stp_gps.ko \
$(MOD_SRC)/mtk_stp_uart.ko:$(MOD_TGT)/mtk_stp_uart.ko \
$(MOD_SRC)/mtk_stp_wmt.ko:$(MOD_TGT)/mtk_stp_wmt.ko \
$(MOD_SRC)/mtk_wmt_wifi.ko:$(MOD_TGT)/mtk_wmt_wifi.ko \
$(MOD_SRC)/mtklfb.ko:$(MOD_TGT)/mtklfb.ko \
$(MOD_SRC)/pvrsrvkm.ko:$(MOD_TGT)/pvrsrvkm.ko \
$(MOD_SRC)/scsi_tgt.ko:$(MOD_TGT)/scsi_tgt.ko \
$(MOD_SRC)/scsi_wait_scan.ko:$(MOD_TGT)/scsi_wait_scan.ko \
$(MOD_SRC)/sec.ko:$(MOD_TGT)/sec.ko \
$(MOD_SRC)/vcodec_kernel_driver.ko:$(MOD_TGT)/vcodec_kernel_driver.ko \
$(MOD_SRC)/wlan_mt6628.ko:$(MOD_TGT)/wlan_mt6628.ko
This copies your custom built modules to the Module out folder. In BoardConfig.mk add
Code:
TARGET_PREBUILT_KERNEL := $(DEVICE_FOLDER)/prebuilt/kernel
..

Remember :If you're building kernel for already existing 4.2 roms disable SELINUX support by going into security options inside menuconfig

You're done!!

All Questions and feedbacks are welcome in the thread
Be sure to drop a thanks if you thought this was usefull

Happy Building.

--------------------------------------------------
Links to my next Guides
Building the boot.img{MediaTek}
Building the ROM{MediaTek}
Last edited by MasterAwesome; 28th August 2014 at 04:55 PM.
The Following 14 Users Say Thank You to MasterAwesome For This Useful Post: [ View ]
17th May 2014, 04:25 PM   |  #2  
MasterAwesome's Avatar
OP Senior Member
Thanks Meter: 798
 
1,136 posts
Join Date:Joined: Jun 2013
More
Build Packages
Required packages before building



Python 2.6 -- 2.7, which you can download from python.org.

GNU Make 3.81 -- 3.82, which you can download from gnu.org,

JDK 6 if you wish to build Gingerbread or newer; JDK 5 for Froyo or older. You can download both from java.sun.com.

Git 1.7 or newer. You can find it at git-scm.com.



#Required packages

Code:
sudo apt-get install git gnupg flex bison gperf build-essential zip curl libc6-dev libncurses5-dev:i386 x11proto-core-dev libx11-dev:i386 libreadline6-dev:i386 libgl1-mesa-glx:i386 libgl1-mesa-dev g++-multilib mingw32 tofrodos python-markdown libxml2-utils xsltproc zlib1g-dev:i386 git-core lzop ccache gnupg flex bison gperf build-essential zip curl zlib1g-dev zlib1g-dev:i386 libc6-dev lib32ncurses5 lib32z1 lib32bz2-1.0 lib32ncurses5-dev x11proto-core-dev libx11-dev:i386 libreadline6-dev:i386 lib32z-dev libgl1-mesa-glx:i386 libgl1-mesa-dev g++-multilib mingw32 tofrodos python-markdown libxml2-utils xsltproc readline-common libreadline6-dev libreadline6 lib32readline-gplv2-dev libncurses5-dev lib32readline5 lib32readline6 libreadline-dev libreadline6-dev:i386 libreadline6:i386 bzip2 libbz2-dev libbz2-1.0 libghc-bzlib-dev lib32bz2-dev libsdl1.2-dev libesd0-dev squashfs-tools pngcrush schedtool libwxgtk2.8-dev python gcc g++ cpp gcc-4.8 g++-4.8 && sudo ln -s /usr/lib/i386-linux-gnu/mesa/libGL.so.1 /usr/lib/i386-linux-gnu/libGL.so
Go to etc/udev/rules.d/51-android.rules

Code:
# adb protocol on passion (Nexus One)
SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", ATTR{idProduct}=="4e12", MODE="0600", OWNER="<username>"
# fastboot protocol on passion (Nexus One)
SUBSYSTEM=="usb", ATTR{idVendor}=="0bb4", ATTR{idProduct}=="0fff", MODE="0600", OWNER="<username>"
# adb protocol on crespo/crespo4g (Nexus S)
SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", ATTR{idProduct}=="4e22", MODE="0600", OWNER="<username>"
# fastboot protocol on crespo/crespo4g (Nexus S)
SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", ATTR{idProduct}=="4e20", MODE="0600", OWNER="<username>"
# adb protocol on stingray/wingray (Xoom)
SUBSYSTEM=="usb", ATTR{idVendor}=="22b8", ATTR{idProduct}=="70a9", MODE="0600", OWNER="<username>"
# fastboot protocol on stingray/wingray (Xoom)
SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", ATTR{idProduct}=="708c", MODE="0600", OWNER="<username>"
# adb protocol on maguro/toro (Galaxy Nexus)
SUBSYSTEM=="usb", ATTR{idVendor}=="04e8", ATTR{idProduct}=="6860", MODE="0600", OWNER="<username>"
# fastboot protocol on maguro/toro (Galaxy Nexus)
SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", ATTR{idProduct}=="4e30", MODE="0600", OWNER="<username>"
# adb protocol on panda (PandaBoard)
SUBSYSTEM=="usb", ATTR{idVendor}=="0451", ATTR{idProduct}=="d101", MODE="0600", OWNER="<username>"
# adb protocol on panda (PandaBoard ES)
SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", ATTR{idProduct}=="d002", MODE="0600", OWNER="<username>"
# fastboot protocol on panda (PandaBoard)
SUBSYSTEM=="usb", ATTR{idVendor}=="0451", ATTR{idProduct}=="d022", MODE="0600", OWNER="<username>"
# usbboot protocol on panda (PandaBoard)
SUBSYSTEM=="usb", ATTR{idVendor}=="0451", ATTR{idProduct}=="d00f", MODE="0600", OWNER="<username>"
# usbboot protocol on panda (PandaBoard ES)
SUBSYSTEM=="usb", ATTR{idVendor}=="0451", ATTR{idProduct}=="d010", MODE="0600", OWNER="<username>"
# adb protocol on grouper/tilapia (Nexus 7)
SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", ATTR{idProduct}=="4e42", MODE="0600", OWNER="<username>"
# fastboot protocol on grouper/tilapia (Nexus 7)
SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", ATTR{idProduct}=="4e40", MODE="0600", OWNER="<username>"
# adb protocol on manta (Nexus 10)
SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", ATTR{idProduct}=="4ee2", MODE="0600", OWNER="<username>"
# fastboot protocol on manta (Nexus 10)
SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", ATTR{idProduct}=="4ee0", MODE="0600", OWNER="<username>"



In bash.rc
Code:
export USE_CCACHE=1
Beter way installing java
Code:
sudo apt-get purge openjdk-\* icedtea-\* icedtea6-\*
sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update && sudo apt-get install oracle-java6-installer
Reuqired package check
Code:
sudo apt-get install git-core lzop ccache gnupg flex bison gperf build-essential zip curl zlib1g-dev zlib1g-dev:i386 libc6-dev lib32ncurses5 lib32z1 lib32bz2-1.0 lib32ncurses5-dev x11proto-core-dev libx11-dev:i386 libreadline6-dev:i386 lib32z-dev libgl1-mesa-glx:i386 libgl1-mesa-dev g++-multilib mingw32 tofrodos python-markdown libxml2-utils xsltproc readline-common libreadline6-dev libreadline6 lib32readline-gplv2-dev libncurses5-dev lib32readline5 lib32readline6 libreadline-dev libreadline6-dev:i386 libreadline6:i386 bzip2 libbz2-dev libbz2-1.0 libghc-bzlib-dev lib32bz2-dev libsdl1.2-dev libesd0-dev squashfs-tools pngcrush schedtool libwxgtk2.8-dev python gcc g++ cpp gcc-4.8 g++-4.8
THIS
Code:
sudo ln -s /usr/lib/i386-linux-gnu/mesa/libGL.so.1 /usr/lib/i386-linux-gnu/libGL.so



You're done
The Following 6 Users Say Thank You to MasterAwesome For This Useful Post: [ View ]
17th May 2014, 04:44 PM   |  #3  
CODE_WORKX's Avatar
Senior Member
Flag Ahmedabad
Thanks Meter: 507
 
428 posts
Join Date:Joined: Aug 2013
More
@MasterAwesome this went a little bit over my head....

i understood till making zimage,,,,,after that how to make it a boot img...
17th May 2014, 04:56 PM   |  #4  
MasterAwesome's Avatar
OP Senior Member
Thanks Meter: 798
 
1,136 posts
Join Date:Joined: Jun 2013
More
Quote:
Originally Posted by Avenger4droid

@MasterAwesome this went a little bit over my head....

i understood till making zimage,,,,,after that how to make it a boot img...

After compiling the zImage, move it to your device tree in your source code. under a folder called prebuilts. Look at my git repo for more reference a210_device_tree. As i said add a flag in the BoardConfig.mk
Code:
TARGET_PREBUILT_KERNEL:=sourcetokernel
. then, we use the the PRODUCT_COPY_FILES to copy the kernel specific files. since modules are kernel specific you'd have to use the modules you built or you'll get a boot loop.
The Following 2 Users Say Thank You to MasterAwesome For This Useful Post: [ View ]
18th May 2014, 04:56 AM   |  #5  
MasterAwesome's Avatar
OP Senior Member
Thanks Meter: 798
 
1,136 posts
Join Date:Joined: Jun 2013
More
Suggestions for new guides are welcome and I'll try my best to do them.

Sent from my Moto G using XDA Free mobile app


Sent from my Moto G using XDA Free mobile app
The Following 2 Users Say Thank You to MasterAwesome For This Useful Post: [ View ]
18th May 2014, 08:09 PM   |  #6  
Senior Member
Flag ▂ ▃ ▅ ▆ █ C:/India/Chen
Thanks Meter: 656
 
500 posts
Join Date:Joined: Oct 2013
Donate to Me
More
Thumbs up
@MasterAwesome

Hello..Glad that someone has given a tutorial thread for Kernel developement. I have a few queries for which i am banging my head on for a while...Though i have used slightly diff commands from what u have said the following are my queries.

I tried building kernel for two Micromax devices A117 ( Used Blu life One M source ) & A250 ( Used Blu life One X source ). These are clone devices of Micromax

I personally own a Blu life One M & have booted the kernel succesfully from the source. But i am unable to boot it up in A117.

And i compiled Blu life One X source and tried in A250 and again i wasn't able to bootup. But another guy managed to boot it up in A250 from the same Blu life one X source. ( Proof : http://forum.xda-developers.com/show...&postcount=300 ). On messaging him he said he hasn't made any change in source and just compiled and t booted for him. I wasn't able to get further support for him.

So what could be my mistake. If u can figure out the mistake for me i can boot it up in both Micromax devices. On analysing my logcat i see gpumodule failing !
18th May 2014, 08:45 PM   |  #7  
MasterAwesome's Avatar
OP Senior Member
Thanks Meter: 798
 
1,136 posts
Join Date:Joined: Jun 2013
More
Quote:
Originally Posted by Santhosh M

@MasterAwesome

Hello..Glad that someone has given a tutorial thread for Kernel developement. I have a few queries for which i am banging my head on for a while...Though i have used slightly diff commands from what u have said the following are my queries.

I tried building kernel for two Micromax devices A117 ( Used Blu life One M source ) & A250 ( Used Blu life One X source ). These are clone devices of Micromax

I personally own a Blu life One M & have booted the kernel succesfully from the source. But i am unable to boot it up in A117.

And i compiled Blu life One X source and tried in A250 and again i wasn't able to bootup. But another guy managed to boot it up in A250 from the same Blu life one X source. ( Proof : http://forum.xda-developers.com/show...&postcount=300 ). On messaging him he said he hasn't made any change in source and just compiled and t booted for him. I wasn't able to get further support for him.

So what could be my mistake. If u can figure out the mistake for me i can boot it up in both Micromax devices. On analysing my logcat i see gpumodule failing !

Common errors:
Appending the headers
Not adding newly built modules

If you've done both give me the link to the source I'll make a boot.img check it out on your phone.



Sent from my Moto G using XDA Free mobile app
18th May 2014, 08:55 PM   |  #8  
Senior Member
Flag ▂ ▃ ▅ ▆ █ C:/India/Chen
Thanks Meter: 656
 
500 posts
Join Date:Joined: Oct 2013
Donate to Me
More
Quote:
Originally Posted by MasterAwesome

Common errors:
Appending the headers
Not adding newly built modules

If you've done both give me the link to the source I'll make a boot.img check it out on your phone.



Sent from my Moto G using XDA Free mobile app

zImage Header patch i do always by placing the compiled zImage in mediatek/build/tools and using mkbuild to patch it.

I didn't get the second point of adding newly built modules? ( I use the modules generated while compiling )
18th May 2014, 08:58 PM   |  #9  
MasterAwesome's Avatar
OP Senior Member
Thanks Meter: 798
 
1,136 posts
Join Date:Joined: Jun 2013
More
Quote:
Originally Posted by Santhosh M

zImage Header patch i do always by placing the compiled zImage in mediatek/build/tools and using mkbuild to patch it.

I didn't get the second point of adding newly built modules? ( I use the modules generated while compiling )

Do a arm-eabi-strip - - strip-unneeded *.ko then push the ko files to /system/lib/modules. Since modules are very kernel specific if you use wrong modules you'll get all sorts of errors.

Sent from my Moto G using XDA Free mobile app
The Following User Says Thank You to MasterAwesome For This Useful Post: [ View ]
19th May 2014, 04:59 AM   |  #10  
Senior Member
Flag ▂ ▃ ▅ ▆ █ C:/India/Chen
Thanks Meter: 656
 
500 posts
Join Date:Joined: Oct 2013
Donate to Me
More
How to do this bro ? If u explain me the commands in detail it would be helpful..

I have pm you the commands I use...Could you have a look at it?

( Without the above command it booted for me in Blu life One M. My concern s why it ain't booting in A117 & A250. A250 has been booted by a guy who used the same source )

Sent from my BLU LIFE ONE M using Tapatalk

Post Reply Subscribe to Thread

Tags
building, guide, kernel, mediatek, source
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes