[Amazon Fire Phone] madushan1000:
Since fire phone doesn't have a bootloader unlock at the moment. There is no point in building a custom kernel. But By building a kernel we can build kernel modules which work on the stock kernel. And yes you can load unsigned kernel modules without a problem since fire phone doesn't use tz apps to verify kernel modules like Samsung does.

Setup

Source

Download the fire phone sources for firmware 4.6.1 from here. And extract the platfrom.tar inside the archive to somewhere(KERNEL_DIR).

toolchain

You can use the android ndk from google, But it requires some setup. I'm using linaro toolchain from here. You can use compiler version 4.7, 4.8 or 4.9. Kernel I'm using (Firmware 4.6.3 - Linux 3.4-perf-g280c96c) is built with gcc-4.7. But I'm using this gcc-4.9. Download it, extract is somewhere(TOOLCHAIN_DIR) and add the $TOOLCHAIN_DIR/bin to your PATH. Theoretically you would be able to build the kernel on windows using Cygwin or MSYS tools but using Linux is better.

config

Connect your phone trough adb and run

adb pull /proc/config.gz
zcat config.gz > $KERNEL_DIR/kernel/qcom/3.4/.config

With this config you will run into some problems because of a missing "trapz_generated_kernel.h". I don't know if this is an auto generated file when they build android as a whole or amazon removed this explicitly(can they do that without violating GPL?). Anyway It looks trapz is some low level kernel debugging function(comment here if you know more about it). We can safely disable it. Open $KERNEL_DIR/kernel/qcom/3.4/.config in a text editor and change the lines

CONFIG_TRAPZ=y
CONFIG_TRAPZ_TP=y
CONFIG_TRAPZ_TRIGGER=y
CONFIG_HAVOK=y

to

#CONFIG_TRAPZ=y
#CONFIG_TRAPZ_TP=y
#CONFIG_TRAPZ_TRIGGER=y
#CONFIG_HAVOK=y

building

Now edit the $KERNEL_DIR/kernel/qcom/3.4/Makefile and add this changes

EXTRAVERSION = -perf-g280c96c

This is at the top of the makefile. If we don't add this, vermagic for the modules will differ from stock kernel and they won't load.

Here arm-linux-gnueabihf- is my cross compiler frefix. Look in $TOOLCHAIN_DIR/bin/ to find it.

Now cd into $KERNEL_DIR/kernel/qcom/3.4/ and do

make

The build will fail a few times complaining about missing headers. Most of the time it's just

#include <myheader.h>

instead of

#include "myheader.h"

Edit the source file where the build fails and change <>s to ""s. (maybe android ndk ignores the difference and include the headers anyway)

After kernel compiles, we are good to go. We can use this kernel sources to build kernel modules for stock kernel.

Kernel modules

To build the kernel modules, we basically need two things. An approximate kernel source and the Module.symvers file from the original kernel. We can get the Module.symvers file by building the complete kernel as explained above or Just extract it from our stock kernel.

To extract the Module.symvers from the stock kernel, extract the boot.img file from firmware update image. Get mkbootimg tools from here compile it and run

unmkbootimg --kernel zImage ---ramdisk ramdisk.cpio.gz -i boot.img

After you get the zImage. Download extract-symvers script from here and run

python2 extract-symvers.py -B 0xc0008000 zImage > Module.symvers

place this file in $KERNEL_DIR/kernel/qcom/3.4/ (You still have to do the changes mentioned above in kernel config and building section run make in the $KERNEL_DIR/kernel/qcom/3.4 and intrupt it after few seconds)

Now you can build loadable modules against this source. Here is a hello world kernel module.

//hello.c
#include<linux/module.h>
#include<linux/kernel.h>
#include<linux/init.h>

static int __init hello_start(void)
{
printk("hello to the world from module");
return 0;
}

static void __exit hello_end(void)
{
printk("heloo exit");
}

module_init(hello_start);
module_exit(hello_end);

#Makefile
KERNEL_DIR=<your kernel dir>/kernel/qcom/3.4

obj-m := hello.o
PWD := $(shell pwd)

default:
$(MAKE) ARCH=arm CROSS_COMPILE=armeb-linux-gnueabi- -C $(KERNEL_DIR) SUBDIRS=$(PWD) modules

Put this files in a folder and run make in it. Change the paths and cross compiler prefix according to your setup. and run make.

After the build push the hello.ko to the phone.

adb push hello.ko /sdcard/
adb shell
su
cd sdcard
insmod hello.ko

run dmesg and you'll see the message.

I'm currently trying to build kexec module from hashcode's sources and USB OTG modules.

I'm attaching a few thing helped me do this.