[Miscellaneous Android Development] MasterAwesome:
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

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

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 :cool:

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

cd kernel_source
cp mediatek/config/devicename/autoconfig/kconfig/platform .config

Now,

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

make zImage

You will get something which says

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

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

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

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}