[X Style (Pure) Android Development] dirtyhank:
Here's the second part of the kernel build mini-guide. In the first part we saw how to build the kernel image and modules from the source code. Now we'll see how to assemble the DTB image, pack the modules and create a ZIP ready to install from recovery. We'll keep using the same directory structure as in the first mini-guide. Remember these are just the basic commands and you might have to make some minor tweaks here and there, but you'll figure it out.

DTB image: Besides the compiled-in settings the kernel can use Device Tree files to initialize hardware upon boot with some default settings. The dtb files are built from source dts files during the kernel built process (we did that already) but they must me assembled into a DTB image to be deployed alongside the kernel image. The tool for the job is usally called dtbtool and the tricky part is finding the right one, as there are many versions and variants. After much trial and error I found out the best for our purposes is the one from CM13 qcom configuration tools. You can build it very easily like this:

cd ~/android/clark/kernel/tools
git clone https://github.com/CyanogenMod/android_device_qcom_common
cd android_device_qcom_common/dtbtool
gcc -o dtbtool dtbtool.c

In case you don't have a compiler installed I've uploaded an statically linked version of the dtbtool that should work on most modern Linux 64bit distros. Now we have the tool we'll assemble the DTB image running this command from the topdir:

cd ~/android/clark/kernel
./tools/android_device_qcom_common/dtbtool/dtbtool -o dtb ./src/frankenclark/arch/arm/boot/dts/

At the end of this process we should have a dtb file ~4.5MB in size

Kernel modules image: Stock kernel uses kernel modules deployed on the system partition but since we don't want to modify the system partition we'll have to use a trick and mount a loopback disk image containing the new modules over the old ones. In order to do that we'll have to create this loopback disk image and copy all the compiled modules. The process is something like this:

cd ~/android/clark/kernel
dd if=/dev/zero of=frankenclark_modules.img bs=1M count=10
mkfs.ext4 -F frankenclark_modules.img
sudo mount -o loop frankenclark_modules.img /mnt
sudo find ./src/frankenclark ./src/motorola/vendor-qcom-opensource-wlan-qcacld-2.0 -iname *.ko -exec cp \{\} /mnt \;
sudo umount /mnt

At the end of this procress we should have a loopback image (frankenclark_modules.img). Note you might want to use a different mount point than "/mnt"

ZIP file: At this stage we have all the files we need, namely kernel image (zImage), DTB image (dtb) and modules image (frankenclark_modules.img). The easiest way to install the new kernel version is injecting those files into an existing frankenclark ZIP install file. We'll use the ZIP from v0.14, here's the whole process:

cd ~/android/clark/kernel
wget http://tx2.androidfilehost.com/dl/r_85gyIVVe5VMC21jv0yyg/1477454103/24694152805483677/frankenclark-0.14.zip
unzip frankenclark-0.14.zip -d Aroma
cp ./src/frankenclark/arch/arm64/boot/Image Aroma/images/300hz/zImage
cp frankenclark_modules.img Aroma/images/300hz
cp dtb Aroma/images/300hz
cd Aroma
zip -r9 ../mykernel.zip *
cd ..

You should now have a mykernel.zip file you can copy to your device and install from recovery. Remember we've just injected the files for the 300Hz version of the kernel so make sure you select that one during the installation process.