# Compiling Linux Kernel 4.19 LTS

<abstract>
	This article illustrates how to generate a bootable Linux Kernel image for RoadRunner SOM
</abstract>

<img class="img-responsive" width="50%" src="./roadrunner_tux.jpg">

##Step-by-step Kernel cross-compilation procedure:

* It is advisable to have a [Debug Port Interface](/DPI) or [similar](/USB-3V-SER) during the kernel bootstrap

This article has been tested on a __Linux Ubuntu 18.04.4 PC Intel/64bit__ and on a __Raspberry Pi 4 Raspbian Buster Lite__,

<div class="row">
	<div class="col-md-6">
		<div class="panel panel-default">
			<div class="panel-heading">
				<h3 class="panel-title">Ubuntu</h3>
			</div>
			<div class="panel-body">
				Install <a href="/arm9_toolchain">these packages</a>
			</div>
		</div>	
	</div>
	<div class="col-md-6">
		<div class="panel panel-default">
			<div class="panel-heading">
				<h3 class="panel-title">Raspberry Pi</h3>
			</div>
			<div class="panel-body">
			
				The toolchain (gcc, g++, etc) required to compile the Linux Kernel is already installed by default on Raspbian Buster.
				Install just these few packages:
				
				<pre>
				sudo apt update
				sudo apt install bc bison libssl-dev
				</pre>
				
				It is possible to use the Raspberry Pi native gcc compiler so remove:
				
				<code>
				CROSS_COMPILE=arm-linux-gnueabihf-
				</code>
				
				from the command provided below
		
			</div>
		</div>	
	</div>
</div>	

Open a terminal on your Linux PC and download the Linux Kernel sources from the main stream repository:

	wget https://www.kernel.org/pub/linux/kernel/v4.x/linux-4.19.134.tar.xz

Extract the Kernel sources from the compressed file by typing:

	tar xvfJ linux-4.19.134.tar.xz

Move inside the new folder:

	cd linux-4.19.134

Create a your own personal branch of Linux Kernel sources using [git](https://git-scm.com/) to keep a trace of any changes from the
Kernel mainline.

Install git on your PC:

	sudo apt update
	sudo apt install git

Create a local git repository and a new branch called "acme": 

	git init; git add .; git commit -m "Linux vanilla"; git branch acme; git checkout acme

Download the Acme Systems patch for this Kernel version. It add just the Acme boards defconfig, device trees and
small fewbug fixes:

	wget https://raw.githubusercontent.com/AcmeSystems/acmepatches/master/linux-4.19.x.patch

and apply it by typing:

	patch -p1 < linux-4.19.x.patch

Then select from the following list the right Linux configuration for your board by typing one of these commands:

	make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- acme-roadrunner_defconfig

### Customize the default Linux Kernel configuration:

If you need to customize the Kernel configuration or you just want to take a look
around the Kernel setup type:

	make ARCH=arm menuconfig

and navigate inside the Kernel configuration using the arrow keys and 
following the help provided by the menuconfig interface.

### Create a defconfig of your own Kernel configuration

	make ARCH=arm savedefconfig

The file <code>defconfig</code> contains your configuration. You could rename and copy it in <code>arch/arm/configs/</code>

	cp defconfig arch/arm/configs/my_defconfig

### Generate the Device Tree Blob file (.dtb)

Now compile the device tree file requested by your board by selecting one of the following commands:

	make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- acme-roadrunner.dtb

### Compile the Kernel image

Compile the Linux Kernel sources and generate the binary compressed 
image file to save in the first partition of microSD card.

	make -j8 ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- zImage
	...
	Image arch/arm/boot/zImage is ready

### Compile the Kernel modules

The image generated contains the Linux Kernel and all the built-in 
device drivers (option <b>[*]</b> in menuconfig) compiled with it.

Al the drivers compiled as external modules (option <b>[M]</b> in menuconfig)
need to be compiled and saved in the rootfs <code>/lib</code> directory on the second 
partition of the microSD. We didn't use any [M] flag in our defconfig so this procedure 
is not requested but. in case you add something. these are the commands to use
to compile them:

	make modules -j8 ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf-
	make modules_install INSTALL_MOD_PATH=./modules ARCH=arm

### Copy the boot files into the first microSD partition

Insert a [formatted microSD](/microsd_format) with the boot loader ([at91bootstrap](/at91bootstrap))
and the rootfs contents already created in your Linux PC and copy on it these files:

Write the Linux Kernel image (zImage) and the device tree blob (acme-roadrunner.dtb) 
inside the first microSD partition:

	sudo cp arch/arm/boot/dts/acme-roadrunner.dtb /media/$USER/boot
	sudo cp arch/arm/boot/zImage /media/$USER/boot
	
Write the Linux Kernel modules inside the second microSD partition:
	
	sudo rsync -avc modules/lib/. /media/$USER/rootfs/lib/.

### After the first boot

At the first access to the board command line update the module dependencies by typing this command:

	depmod -a

## Links

* [The Linux Kernel Archives](https://www.kernel.org)
* [Linux versions changelog](https://kernelnewbies.org/LinuxVersions)

## Usefull command

If you want to create a patch with the changes you have made on the vanilla Linux kernel
use these commands:

	git checkout acme
	git diff master > acme.patch

To revert a patch applied type:

	patch -p1 -R < ../acme.patch

