[GUIDE][TOOL] How Build/Compile your own kernel for LG G2

This guide is useful?


  • Total voters
    15
Search This thread
F

ferreirawax

Guest
There is still no "how to" specific to the LG G2, so I'll try to share the maximum of my experience (or almost) with ["kernel", "build", "LG G2"] here. I'll show two methods different using CM12 kernel, The method for LG ROMs is very similar, I show some tips for LG ROMs also. The first method is the "manual" and second is the "automatic" using my "tool" called DCC. So come on! For this you will need a computer running Linux (x64) and have knowledge about basic Linux commands. I'm using a laptop (i3-2328M and 4GB of RAM) running Ubuntu 14.10 :good:

Setting Up the Environment
First install the essential tools (common environment for those already working with C/C++):

Code:
sudo apt-get install bison build-essential curl flex git gnupg gperf libesd0-dev liblz4-tool libncurses5-dev libsdl1.2-dev libwxgtk2.8-dev libxml2 libxml2-utils lzop openjdk-7-jdk openjdk-7-jre pngcrush schedtool squashfs-tools xsltproc zip zlib1g-dev g++-multilib gcc-multilib lib32ncurses5-dev lib32readline-gplv2-dev lib32z1-dev

Check Java and Java Compiler version:

Code:
java -version && javac -version

If the default Java is 1.6.x, Use this command and select java-7-openjdk as default:

Code:
sudo update-alternatives --config java

Setting Up the kernel Environment
Code:
mkdir -p ~/android/mykernel && cd ~/android/mykernel && mkdir out

This will create a folder and two subfolder in your home, Within mykernel folder will get all the tools that we will set up. Now we set the toolchain, Particularly I use Linaro 4.9.3, the version optimized for Cortex-A15 by @Christopher83, so I will use it here.

Clone Linaro GCC 4.9.3:

Code:
git clone https://github.com/Christopher83/arm-cortex_a15-linux-gnueabihf-linaro_4.9

Now the required binaries (Common essential binaries to work with msm8974):

Code:
git clone https://github.com/ferreirawax/mkboot_exec ~/android/mykernel/executables

Clone the RAM disk compiled from CM12:

Code:
git clone -b aosp https://github.com/ferreirawax/ramdisk_g2 ~/android/mykernel/ramdisk

For stock Kitkat:

This RAM disk was extracted from my d805 (d805 is a d802 which was released in latin america), then so will work on the d802, d805 and d806 (d806 is a d802 as the d805) If the variant is another you will have to draw your own device using a tool such AIK or search repositories of other developers.

Code:
git clone -b lge https://github.com/ferreirawax/ramdisk_g2 ~/android/mykernel/ramdisk

Now finally the source code of the kernel, Here I will use a configured kernel to compile the code without CM12 source, you can use the source code that is in the official repositories of CM12, but you will face some errors and warnings during the build that can be solved using the commits that are in my source and supply of other developers.

Clone kernel source:

Code:
git clone -b cm-12.0 https://github.com/ferreirawax/kernel_lge_msm8974-patched ~/android/mykernel/msm8974

For stock KitKat:

Code:
git clone -b lge-kitkat https://github.com/ferreirawax/kernel_lge_msm8974-patched ~/android/mykernel/msm8974

Apparently not but we already have a complete environment, see the folders in mykernel to have a shadow on it. Now let's the good part of the story. :fingers-crossed:

Compiling
Enter in kernel folder:

Code:
cd msm8974

Export Architecture and Toolchain:

Code:
export ARCH=arm
export CROSS_COMPILE=~/android/mykernel/arm-cortex_a15-linux-gnueabihf-linaro_4.9/bin/arm-eabi-

For stock KitKat you need export LZ4 path:

Code:
export PATH=$PATH:tools/lz4demo

Select the model of the device you want to compile your pointing due defconfig, If you are using source code directly from the repositories of the CM12, defconfig each supported model is called "cyanogenmod_MODEL_defconfig".

Select model to build (In my case I will compile for d802):

Code:
make d802_defconfig

Now let's clear the specific waste of d802:

Code:
make clean && make mrproper

Select model again to generate new .config:

Code:
make d802_defconfig

Now we start to build:

Code:
make -j4

-j4 is suitable for dual-core processors and -j5 for quad-core.

Depending on the configuration of your PC can take up to 20 minutes. In my laptop the build time is about 10 minutes. If you are using VirtualBox, can take much longer. Upon completion, will generate a zImage and some dtb (Device tree binary) files, The dtb files will be used to create a specific image (dt.img) file containing board informations. Now let's create a RAM disk and then dt.img to later put it all together and result in a final boot.img.

Enter in executables folder:

Code:
cd ~/android/mykernel/executables

Create GNUZip compressed RAM disk:

Code:
./mkbootfs ~/android/mykernel/ramdisk | gzip > ~/android/mykernel/out/ramdisk.gz

Create dt.img:

First copy the dtc file that is in executables folder to /usr/bin

Code:
gksu nautilus

This will open the file manager with root privileges, After click the left mouse button and select the dtc as executable.
You may need to restart, After that create the dt.img with the following command.

Code:
./dtbTool -s 2048 -o ~/android/mykernel/out/dt.img ~/android/mykernel/msm8974/arch/arm/boot/

Finally assemble boot.img file:

Code:
./mkbootimg --kernel ~/android/mykernel/msm8974/arch/arm/boot/zImage --ramdisk ~/android/mykernel/out/ramdisk.gz --cmdline "XXXX" --base 0x00000000 --pagesize 2048 --offset 0x05000000 --tags-addr 0x04800000 --dt ~/android/mykernel/out/dt.img -o ~/android/mykernel/out/boot.img

Replace the XXXX for the following:

console=ttyHSL0,115200,n8 androidboot.hardware=g2 user_debug=31 msm_rtb.filter=0x0 mdss_mdp.panel=1:dsi:0:qcom,mdss_dsi_g2_lgd_cmd androidboot.selinux=permissive

Do not remove the quotes. Replace the panel model (red) for your model. if you are compiling for stock KitKat, remove everything that is green.

Now let's add a "signature" in your kernel for the damn locked LG G2 bootloader so you can not be invalidated. If you do not do this procedure, the bootloader of your LG G2 will verify that this kernel has a particular signature, as it does not have your device will be unable to boot. We have two tools for this, Loki is able to fool the bootloader and open_bump (This tool is controversy) is able to add a "valid" signature using illegal means. I'll show how to use both. To this is very simple..

open_bump method:

You need Python to run any Python code, open_bump requires Python 2.x

Code:
git clone https://github.com/CyboLabs/Open_Bump
cd Open_Bump && python open_bump.py ~/android/mykernel/out/boot.img

You will now have an output "boot_bumped.img".

loki_tool method:

First get a copy of the bootloader of your G2, For this you need the adb installed and your G2 properly connected to your PC.

Get bootloader:

Code:
sudo apt-get install android-tools-adb
adb devices && adb shell "su -c dd if=/dev/block/platform/msm_sdcc.1/by-name/aboot of=/sdcard/aboot.img"
adb pull /sdcard/aboot.img

Do not forget the USB debugging enabled

Patch kernel:

Code:
./loki_patch boot aboot.img ~/android/mykernel/out/boot.img ~/android/mykernel/out/loki_boot.img

Will generate an output "loki_boot.img" on mykernel/out folder.

You can install using "my easy installer" (Attached at the end of post):

Download the attachment and extracted, Place your boot.img in the delta folder and
copy all the modules that are in your kernel source for the modules folder:

Code:
find  ~/android/mykernel/msm8974/. -name "*.ko" -type f -exec cp {} ~/android/mykernel/installer/system/lib/modules \;

Now zip folder and install on your device. This installer already have panel detection script by @dr87

mdU8JHY.png


koqu4yL.png


Finally now you have your own kernel! Now I will show the second method of how to automatic compile your kernel using my tool.

To rebuild you have to use the following:

Code:
make clean && make mrproper
make MODEL_defconfig
make -j4

Some tips
To not export the toolchain path every time you restart the system, Create a Shell executable in /etc/profile.d as follows:

Code:
sudo nano /etc/profile.d/export_gcc.sh
Copy and paste:
export PATH=~/android/mykernel/arm-cortex_a15-linux-gnueabihf-linaro_4.9/bin:$PATH
CTRL + X, Y, ENTER

Now you just use:

Code:
export ARCH=arm
export CROSS_COMPILE=arm-eabi-

Useful guides:

If you are new, you can start learning about git, One of the main tools for working with modifications to your kernel. Below I leave some links with some guides that can help you.

How to use Github
How to cherry-pick a github commit

 

Attachments

  • installer.zip
    168.7 KB · Views: 250
Last edited:
F

ferreirawax

Guest
DCC is a tool I developed to simplify my day, DCC is just a simple Python code that can help you save your time! See the features:

  • Command-line arguments like M$'s DiskPart
  • Real-time Shell executer
  • Manage builds for AOSP and LG ROMs
  • Build for single or ALL variants in same time
  • Manage the parameters of your kernel
  • DCC works with embedded open_bump
  • Manage your favorite toolchain
  • Easy to configure and use

I'll show how to use DCC is easy

Setup kernel environment:
Code:
reset && cd $HOME
mkdir dev && cd dev
git clone https://github.com/ferreirawax/dcc
cd dcc && find . -name '.gitignore' -delete
git clone https://github.com/ferreirawax/ramdisk_g2_aosp  ramdisk/aosp
git clone https://github.com/ferreirawax/kernel_lge_msm8974-patched msm8974/aosp

Build kernel:
Code:
python dcc.py
do boot d802
exit

You know what happens to these magical commands? An immediate flashable zip of your kernel ready to flash. But for that you need to "setup" to your taste, see the page on github.
 
Last edited:

Boris31

Senior Member
Feb 11, 2012
206
363
Novi Sad
@ferreirawax this is great i was waiting for something like this so much, tommorow i will try your guide and post the results :D:D:D

EDIT
I followed everything and got stuck here:

simex@Simex:~/android/mykernel/msm8974$ make clean && make mrproper
make: /home/simex/android/mykenel/arm-cortex_a15-linux-gnueabihf-linaro_4.9/bin/arm-eabi-gcc: Command not found

THANKS for help :)

EDIT 2
I tried method with DCC and im stuck here:

simex@Simex:~/dev/dcc$ python dcc.py
Traceback (most recent call last):
File "dcc.py", line 19, in <module>
from colorama import *
ImportError: No module named colorama
simex@Simex:~/dev/dcc$

Sorry if im annoying but i hope someone can help me :)
 
Last edited:
F

ferreirawax

Guest
@ferreirawax this is great i was waiting for something like this so much, tommorow i will try your guide and post the results :D:D:D

EDIT
I followed everything and got stuck here:

simex@Simex:~/android/mykernel/msm8974$ make clean && make mrproper
make: /home/simex/android/mykenel/arm-cortex_a15-linux-gnueabihf-linaro_4.9/bin/arm-eabi-gcc: Command not found

THANKS for help :)

EDIT 2
I tried method with DCC and im stuck here:

simex@Simex:~/dev/dcc$ python dcc.py
Traceback (most recent call last):
File "dcc.py", line 19, in <module>
from colorama import *
ImportError: No module named colorama
simex@Simex:~/dev/dcc$

Sorry if im annoying but i hope someone can help me :)

This error happens when the toolchain path is wrong.

Colorama module need to use DCC (I forgot to mention that)
Code:
sudo pip install colorama
 

Boris31

Senior Member
Feb 11, 2012
206
363
Novi Sad
This error happens when the toolchain path is wrong.

Colorama module need to use DCC (I forgot to mention that)
Code:
sudo pip install colorama

ok thanks i got i working

---------- Post added at 10:43 AM ---------- Previous post was at 10:21 AM --------- @ferreirawax you got a typing mistake here thats why method 1 didnt work i finaly figured it out:

Export Architecture and Toolchain:

Code:

export ARCH=arm
export CROSS_COMPILE=~/android/mykenel/arm-cortex_a15-linux-gnueabihf-linaro_4.9/bin/arm-eabi-

It should be myKERNEL and that it works ;)

Now i succesfully build my kernel but im having problems with bump, i cant get past this:
simex@Simex:~$ python open_bump.py ~/android/mykernel/out/boot.img
python: can't open file 'open_bump.py': [Errno 2] No such file or directory

but im sure its there i tryed nearly everything but cant get bumped boot.img :(
 
Last edited:

bolt890

Senior Member
Feb 7, 2014
200
51
Thank you!!!! I have wanted something like this to compile different kernels .:)
 
Last edited:

6ril1

Senior Member
Jan 23, 2010
510
864
Bordeaux
@ferreirawax
I am aware that the rules prohibit posting simple thanks but I feel duty do it here because it is really nice to find shared topics showing how to fish rather than giving fish.
Big thank you to you so, great initiative !
 
Last edited:

bolt890

Senior Member
Feb 7, 2014
200
51
DTB combiner:
Input directory: '/home/bolt890/dev/dcc/msm8974/aosp/arch/arm/boot/'
Output file: '/home/bolt890/dev/dcc/outputs/dt.img'
Found file: msm8974-v2-2-g2-open_com.dtb ... skip, failed to scan for 'qcom,msm-id = <' tag
Found file: msm8974-v2-g2-open_com.dtb ... skip, failed to scan for 'qcom,msm-id = <' tag
Found file: msm8974-g2-open_com.dtb ... skip, failed to scan for 'qcom,msm-id = <' tag
=> Found 0 unique DTB(s)
error: dt.img not found, failed to make target
Keep getting this error:(
 
F

ferreirawax

Guest
DTB combiner:
Input directory: '/home/bolt890/dev/dcc/msm8974/aosp/arch/arm/boot/'
Output file: '/home/bolt890/dev/dcc/outputs/dt.img'
Found file: msm8974-v2-2-g2-open_com.dtb ... skip, failed to scan for 'qcom,msm-id = <' tag
Found file: msm8974-v2-g2-open_com.dtb ... skip, failed to scan for 'qcom,msm-id = <' tag
Found file: msm8974-g2-open_com.dtb ... skip, failed to scan for 'qcom,msm-id = <' tag
=> Found 0 unique DTB(s)
error: dt.img not found, failed to make target
Keep getting this error:(

Seem to lack the DTC binary on your system. Download the attachment and follow:

Code:
sudo cp ~/dtc.zip /usr/bin/dtc

Or you can simply remove the extension and copy the file to /usr/bin any other way, may be necessary to a log-in/out for effect. Then compile again. :)
 

Attachments

  • dtc.zip
    91.8 KB · Views: 91

Top Liked Posts

  • There are no posts matching your filters.
  • 6
    @ferreirawax

    Thanks for taking the time to post this. I would love for more people to try their hand at Custom Kernels! Otherwise you end up with the same kernel, different name.
    3
    Hasn't Render or dorimanx or someone else made an auto screen type detector for their kernel? Could it be possible to show us how to add that to avoid damaged screens etc?
    It's a @dr87 script http://xdaforums.com/lg-g2/development/4-4-kernel-automatically-detect-panel-t2800626
    @dorimanx in this post, where he gave a script for bumping kernel, put it inside his zip : http://xdaforums.com/showpost.php?p=57109988&postcount=376 (look at loki.sh in /RECOVERY/bumped_recovery/temp/loki.sh in the script).
    The detection is made during flash, not in the kernel (During the flash process, before kernel injection, the script detects phone panel, and search in the kernel that we want to flash, if settings for others panel exists inside it, and if yes, replace in the kernel these bad settings by the right settings )
    Code:
    lcdmaker=$(grep -c "lcd_maker_id=1" /proc/cmdline)
    if [ $lcdmaker == 1 ]; then
    	echo "JDI panel detected"
    	find /tmp/loki/recovery.img -type f -exec sed -i 's/console=ttyHSL0,115200,n8 androidboot.hardware=g2 user_debug=31 msm_rtb.filter=0x0 mdss_mdp.panel=1:dsi:0:qcom,mdss_dsi_g2_lgd_cmd/console=ttyHSL0,115200,n8 androidboot.hardware=g2 user_debug=31 msm_rtb.filter=0x0 mdss_mdp.panel=1:dsi:0:qcom,mdss_dsi_g2_jdi_cmd/g' {} \;
    elif [ $lcdmaker == "0" ]; then
    	echo "LGD panel detected"
    	find /tmp/loki/recovery.img -type f -exec sed -i 's/console=ttyHSL0,115200,n8 androidboot.hardware=g2 user_debug=31 msm_rtb.filter=0x0 mdss_mdp.panel=1:dsi:0:qcom,mdss_dsi_g2_jdi_cmd/console=ttyHSL0,115200,n8 androidboot.hardware=g2 user_debug=31 msm_rtb.filter=0x0 mdss_mdp.panel=1:dsi:0:qcom,mdss_dsi_g2_lgd_cmd/g' {} \;
    else
    	echo "lcd_maker_id doesn't exist. Something went wrong."
    fi
    
    # Install BUMPED Recovery
    dd if=/tmp/loki/recovery.img of=/dev/block/platform/msm_sdcc.1/by-name/recovery || exit 1
    rm -rf /tmp/loki
    exit 0
    3
    Wrong... guide, section and panel. But the TOOL woks great ;)

    If you do not have Linux skills you're probably not enabled for it. The guide is not incomplete, if you go back a bit see what some people have managed normally. This guide is not meant to show how to add features.

    Disagreed, "If you do not have Linux skills you're probably not enabled for it". People need a point to start, an anchor point, if you're not able to help them with their beginning you shouldn't even be posting a "GUIDE".

    @fix-this! to add features to your based kernel you need the environment setup, I would recommend you look at this:
    Code:
    http://xdaforums.com/showthread.php?t=2639611
    A good written tutorial to set it up. I would also recommend you use Ubuntu instead of another distro, or Ubutu based ones, like Mint with it's friendly UI if your starting on Linux now.
    Here are some basic commands with explanation: http://www.comptechdoc.org/os/linux/usersguide/linux_ugbasics.html
    You need to setup git as well with SSH and etc but it's in the first link.
    https://www.siteground.com/tutorials/git/commands.htm some commands for git.
    Read this whole website helps a lot as well http://think-like-a-git.net/sections/graph-theory/seven-bridges-of-konigsberg.html

    After all these you can start a git clone or use your own manifest to fetch things.
    You need to cd to the location and run git status to see if your in the right place.
    Then git fetch https://github.com/SaberSunset/kernel_lge_msm8974 the URL of the commits you want.
    git cherry-pick f10fe349135881496035dc092a8ae9eb9f9029cc the commit https://github.com/SaberSunset/kernel_lge_msm8974/commit/f10fe349135881496035dc092a8ae9eb9f9029cc
    git status to be sure it merged with not conflicts
    For conflicts, this may help http://xdaforums.com/showthread.php?t=2763236 generally.
    For changing default toolchain to SaberMod this thread is a very good place: http://xdaforums.com/chef-central/android/guide-switching-to-custom-toolchain-t2927662

    Now you can continue using this guide.

    Code:
    export ARCH=arm
    export CROSS_COMPILE=~/Android/veneno/arm-cortex_a15-linux-gnueabihf-sm_4.9/bin/arm-eabi-

    I prefer to use the Makefile in Kernel, but I compile ROM + Kernel so it's quite easy for me to set things up on Vendor pointing paths.
    make veneno_d802_defconfig
    make -j5 in my case I use make -j32 otapackage but my computer is very overpower, for normal CPU use -j5 as he said.
    If you have problems you can PM me.

    @ferreirawax the tutorial needs some adjustments and don't need to be in Development section, no sense since we have a Developing section for those who want to know more, there we have other G2 developers support. The TOOL is quite good, I tested it quickly, no problems with it.
    2
    I felt a bit of banter in his words, To go forward you do have to have a minimum of skills in Linux, I also pointed out that the thread from beginning (What do you not read). And you also do not realize that the title is "How to build" rather than "How to build and add features", however and went against myself and gave tips on how to do this.

    Sorry if I gave a bad feelin' in my works, the point is that with some adjustments this guide will be fine, and the section like general will make it more worthy, or even in Development section because here all ROMs are posted and when they BUMP UP your tutorial won't be seen. However I'm sure this will help some people on future, mainly the tool :angel:.
    1