[Pixel C Original Android Development] jcadduono:
INTRODUCTION

Hello Pixel C users and developers! I saw your device forum is a little lonely, so I offer you this in the hopes it may become slightly more eventful and 2017 becomes the year of the Pixel C development!

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.

NEED LINUX? I'VE GOT YOU COVERED

If you don't have a Linux installation, consider installing the latest version of Oracle VM VirtualBox.

You can download a minimal Debian VM from my site: https://build.nethunter.com/possibly-the-coolest-things/Debian.ova (SHA1SUM)

It should be fairly simple to "Import Appliance..." from VirtualBox and set up the Shared Folders properly, everything else is set up for you!

Username: root

Password: xda

Once logged in, create your own user account from LXTerminal:

adduser [b]yourname[/b]
# answer the questions, you can leave it all blank except the password if you want>
echo "[b]yourname[/b] ALL=(root) NOPASSWD: /bin/su" > /etc/sudoers.d/me

The second command will allow you to type "su" to become root whenever you need.

You should then log out and log in to your new account instead.

PREREQUISITES

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

bc - a tool needed by the kernel build system

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

diffutils - used to compare config changes

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

apt-get install bc git-core build-essential libncurses5-dev diffutils 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 ARM64 (aarch64) toolchains.

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

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_google_chromeos

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 Google ChromeOS (dragon / ryu) 3.18 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_google_chromeos
cd android_kernel_google_chromeos

The default branch is android-7.1. This is what most users will want. I've added build and menuconfig scripts to assist in building for you.

There may be some additional branches available to choose from.

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 -7.1 suffix to it because you'll probably want to start a new branch for each time a new version of Android releases.

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-7.1

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

Open build.sh and menuconfig.sh.

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

You will also want to set your default target config name.

See the line:

[ "$TARGET" ] || TARGET=google

You'll want it to look like this:

[ "$TARGET" ] || TARGET=coolname

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

cp arch/arm64/configs/google_dragon_defconfig arch/arm64/configs/coolname_dragon_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 dragon

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

build/arch/arm64/boot/Image.fit
build/lib/modules/ (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.

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 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.

(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.fit from your build output into the lazyflasher folder.

If you have built with kernel modules, copy build/lib/modules -> lazyflasher/modules.

Now simply run:

make

A TWRP flashable zip and sha1sum is created!

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. ;)

UPDATING THE KERNEL SOURCES

Of course my repository will become out of date after a while. In this situation, you want to add a remote that is more updated.

We'll use Google's official Pixel C kernel repository here.

Inside your kernel source directory, run the command:

git remote add google -t chromeos-3.18 https://chromium.googlesource.com/chromiumos/third_party/kernel

Every time you want to fetch updates from Google's branch, run the command:

git fetch google --tags

After fetching updates, you have to apply them. You can choose between a rebase strategy or a merge strategy.

In most cases you'll want to use the merge strategy.

The merge strategy will merge Google's updates into your branch.

The rebase strategy will move your work on top of Google's latest instead.

# the merge strategy (pick one, recommended)
git merge google/chromeos-3.18

# the rebase strategy (will require a force push and rewrite history, not recommended)
git rebase google/chromeos-3.18

JUST WANT TO DISABLE VERITY/ENCRYPTION?

You can build lazyflasher by itself, empty, without a kernel Image.fit or modules and flash it!

It's already set up to automatically disable verity and make encryption optional.

Alternatively, there's a branch already set up called no-verity-opt-encrypt. You can find prebuilt official zips at: https://build.nethunter.com/android-tools/no-verity-opt-encrypt/

It should work on any and all kernels!

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/