[Note 7 Guides, News, & Discussion] jcadduono:
INTRODUCTION

I create this guide in the hope to jump start development on our lovely Exynos devices.

I expect that before you start, you have a Linux installation in either a virtual machine or on a physical PC.

Debian Jessie, Kali Linux, Mint, or Ubuntu are excellent choices and what I'm familiar with, so if you get stuck it'll be easier for me to help you if you use these.

PREREQUISITES

WARNING: Custom kernels on the Note 7, S7, and S7 edge currently require encryption to be disabled in order to boot. They also have to disable some secure MobiCore firmware. You will have to

format your entire data partition

when going from stock kernel to a custom kernel! Once you're on a custom kernel with non-encrypted data, you shouldn't have to wipe it going to another custom kernel.

Be careful not to disable developer options or OEM unlock, otherwise all your data will be destroyed! Back up your data partition whenever you flash stock. (for upgrades, etc.)

First off, you'll want to download some tools necessary for building and downloading kernel sources:

git-core - you'll want git for downloading and maintaining your sources

build-essential - native gcc & tools for building (needed for build commands)

libncurses5-dev - needed to build menuconfig

diff - used to compare config changes

colordiff - used by diff to provide colorful human readable diff output

apt-get install git-core build-essential libncurses5-dev diff colordiff

PREPARING YOUR ENVIRONMENT

Once you've got that out of the way, you should create an organized environment for working. I like to use ~/build.

mkdir -p ~/build/toolchain ~/build/kernel

DOWNLOADING & INSTALLING A TOOLCHAIN

You'll want to download a toolchain for kernel building. I recommend using Linaro's optimized ARM toolchains.

The Exynos 8890 benefits from the Cortex-A53 code compiling optimizations.

GCC 4.9: https://releases.linaro.org/components/toolchain/binaries/4.9-2016.02/aarch64-linux-gnu/

GCC 5.X: https://releases.linaro.org/components/toolchain/binaries/latest-5/aarch64-linux-gnu/

The actual file that you want ends in -x86_64_aarch64-linux-gnu.tar.xz (assuming you have a 64-bit Linux install, seriously, 32-bit needs to go! :))

We'll use the GCC 5.3.1 2016.5 toolchain in this example.

Let's download and extract it now:

cd ~/build/toolchain
wget "https://releases.linaro.org/components/toolchain/binaries/latest-5/aarch64-linux-gnu/gcc-linaro-5.3.1-2016.05-x86_64_aarch64-linux-gnu.tar.xz"
tar xf ./*linux-gnu.tar.xz
rm ./*linux-gnu.tar.xz

That's it for installing the toolchain, easy right?

DOWNLOADING THE KERNEL

Rather than download your kernel sources directly from Samsung full of wacky issues when you change a single configuration option, you can grab it from my GitHub!

Using this method, you have an already working stock kernel prepared to be modified and built at your leisure.

You're also able to grab updates and fixes from me should you want them. I'll be committing new kernel source updates from Samsung to the opensource branch.

The stock-6.0 branch will be rebased on top of opensource when that happens, then stock-6.0.y (stock, with Linux updates) will be rebased on top of that.

Cool concept, right?

First, you'll want to create a GitHub account if you don't have one already.

This will allow you to upload your changes and share your kernel with other interested users and developers.

Once you've got your account, and you're logged in, browse to:

https://github.com/jcadduono/android_kernel_samsung_universal8890

You want to fork the sources to your own account, to do this simply click the [Fork | ] button near the top right of the page:

Now you've got your own copy of the Note 7 / S7 / S7 edge kernel sources on your GitHub!

The next step is to use git to download it to your PC.

Replace "your_username" with your actual GitHub username. Using your_username@github.com you can avoid being asked for your username each time you push new changes.

cd ~/build/kernel
git clone https://your_username@github.com/your_username/android_kernel_samsung_universal8890 samsung_universal8890
cd samsung_universal8890

The default branch is stock-6.0. This is what most users will want. If you wish to start on a kernel that is updated to the latest Linux minor version, then simply:

git checkout stock-6.0.y

There's a twrp-6.0 branch that's used for building the kernel inside the official TWRP for Note 7 / S7 / S7 edge.

There's also a nethunter-6.0 branch used to build the kernel used by Kali NetHunter on those devices.

You can use git log <branch> to view commits, and git cherry-pick <commit id> to copy commits from those branches into yours if you like.

CONFIGURING GIT

Before you start working on your kernel, you will need to set up your git profile.

The user values show up in commit messages to tell people who authored them.

git config --global user.name "Your Name"
git config --global user.email "your@email.com"
git config --global push.default simple

YOUR FIRST COMMIT

We'll want to check out a new branch, and give it your own cool name. I suggest adding a -6.0 suffix to it because you'll probably want to start a new branch when Android 7.0 comes out.

We're going to use "coolname" as our example, so be sure to replace that with what you really want it to be called in the next steps. ;)

git checkout -B coolname-6.0

Next we'll need to modify the build scripts to fit our setup.

Open build.sh, menuconfig.sh, and dtbgen.sh.

You'll want to set the TOOLCHAIN= path in all 3 scripts. If you're following this guide, then it's already correct!

In menuconfig.sh and build.sh, you'll want to set your default target config name.

See the line:

[ "$TARGET" ] || TARGET=samsung

You'll want it to look like this:

[ "$TARGET" ] || TARGET=coolname

Now you want to copy the default samsung kernel configuration so you have your own to work with:

cp arch/arm64/configs/samsung_defconfig arch/arm64/configs/coolname_defconfig

Now you'll have prepared your kernel source for starting work on your own custom kernel!

Let's turn that into a commit, but first look at the changes you've made using:

git diff

Does that look good? If not, fix what's broken before proceeding.

Next you'll want to add files that will be part of your commit.

For simplicity's sake, let's just add all of the changed files into the commit.

git add .

Now to make your commit:

git commit -m "My first commit, setting up my coolname kernel!"

You've done it!

CONFIGURING YOUR KERNEL

We'll use the menuconfig.sh script to launch the kernel menuconfig.

./menuconfig.sh

Change whatever options you're interested in, but don't change a lot all at once, otherwise when or if you run into issues, you won't know which option caused it.

Once you're done playing in the menuconfig, exit and save.

You'll be shown a colorful difference between your old configuration and your new one.

It will ask you if you want to save it, and you just have to type "y" and press enter for it to be saved.

At this point it's a good idea to make another commit to save your configuration changes.

If you need to edit the commit, you can easily use git commit --amend to fix it up.

BUILDING YOUR NEW KERNEL

Let's take your new config for a test drive.

To build your kernel, simply run:

./build.sh gracelte xx

gracelte = Note 7

herolte = S7

hero2lte = S7 edge

xx = International N930F & N930FD

kor = Korean N930K, N930L, & N930S

Yes, you can compile your kernel to all 3 of those devices using just your single config!

The power of device specific config additions. :P

Once your kernel is finished building, the resulting files will be located at:

build/arch/arm64/boot/Image
build/arch/arm64/boot/dtb.img
build/modules/*.ko (if modules are enabled)

INSTALLING YOUR NEW KERNEL

The LazyFlasher project comes to the rescue here. It's the swiss army knife of kernel flashing in TWRP.

There's a specific branch for the Note 7 called kernel-flasher-gracelte. (use kernel-flasher-herolte instead if building for S7/S7 edge!)

To download it (feel free to fork it so you can have a copy on your GitHub to modify instead!):

cd ~/build
git clone -b kernel-flasher-gracelte https://github.com/jcadduono/lazyflasher.git
cd lazyflasher

To use LazyFlasher, you'll probably want to take a look at the Makefile, config.sh, and META-INF/com/google/android/update-binary (a shell script).

There's a few things you can change there to personalize it to your needs.

You should also enter the patch.d folder and delete 060-f2fs-fstab if you don't have f2fs enabled in your kernel, and edit the io_scheduler in 070-kernel-settings if you don't have fiops enabled either.

(make another git commit to save your setup!)

Once the installer is set up to your liking, all you have to do to build it is copy the Image and dtb.img from your build output into the lazyflasher folder.

If you have any modules (.ko files) to install, place them in the modules folder.

Now simply run:

make

A TWRP flashable zip and sha1sum is created!

At this point, you will need to use the [Format Data] button in TWRP

if

your device is encrypted! There is no known way around this.

This will wipe all data from your phone, including your internal storage, essentially making it like new.

Transfer it to TWRP and flash away, you've just lost your custom kernel development virginity.

Go have a few beers to celebrate, or to drown your sorrows in the case of a boot loop. ;)

You should consider taking a look at the patch.d scripts sometime so you know what it's actually doing.

PROBLEMS?

Post a reply here and I'll try to find a solution and add it to this post.

RESOURCES

Need a text editor for coding? I use gedit. It's pretty, light, and you can get some decent plugins for it.

It's a minimal editor, so don't expect anything really fancy. Configure it and enable plugins before you decide to trash it. ;)

apt-get install gedit gedit-plugins

Here's an awesome git starter guide: http://rogerdudler.github.io/git-guide/

THANKS

If not for [MENTION=5899304]Tkkg1994[/MENTION] finding out what changes are needed to get custom kernels up and running, we wouldn't have this lovely guide here!