# Compiling Linux Kernel 5.4.x LTS for Acqua

<abstract>
This article illustrates how to generate a bootable Linux Kernel image for Acqua SOM starting from the Linux
Kernel sources available on the The Linux Kernel Archives 
</abstract>

## Prerequisite

A Linux PC in required to cross compile the Kernel image. 

This procedure has been tested on:

* Ubuntu 18.04.4 LTS (amd64)
* Debian Buster 10 (amd64)

Follow this article to install the Toolchain requested to compile the sources:

* @article='arm9_toolchain'

Moving from the Kernel 4.19 to 5.4 it is requested to compile the at91bootstrap bootloader following this guide:

* https://www.acmesystems.it/at91bootstrap

The previous version of at91bootstrap 3.9.1 will not work correctly

It is advisable to have a Debug Port Interface like [DPI](/DPI) or [USB-3V-SER](/USB-3V-SER) 
to check the error messages during the Kernel bootstrap

## Step-by-step procedure

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

	wget https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.4.163.tar.xz

extract the Kernel sources from the compressed file by typing:

	tar xvfJ linux-5.4.163.tar.xz

move inside the folder created:

	cd linux-5.4.163

copy the basic kernel defconfig file suggested from us 

	wget https://www.acmesystems.it/www/acqua_compile_kernel_5_4/acme-acqua_defconfig -O arch/arm/configs/acme-acqua_defconfig

Set the defconfig:

	make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- acme-acqua_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.

### Note to use the 7 inch display on the legacy BERTA-A5 display boards

If you are using a TFT with your Acme board change the file 

	drivers/gpu/drm/panel/panel-simple.c
	
using these commands:

	rm drivers/gpu/drm/panel/panel-simple.c
	wget https://www.acmesystems.it/www/acqua_compile_kernel_5_4/panel-simple.c -O drivers/gpu/drm/panel/panel-simple.c

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

Download the Acqua device tree file:

	wget https://www.acmesystems.it/www/acqua_compile_kernel_5_4/at91-sama5d3_acqua.dts -O arch/arm/boot/dts/at91-sama5d3_acqua.dts

Now compile the device tree file:

	make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- at91-sama5d3_acqua.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-gnueabi- 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-gnueabi-
	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 and the device tree blob in the first microSD partition:
	
	sudo cp arch/arm/boot/zImage /media/$USER/boot
	sudo cp arch/arm/boot/dts/at91-sama5d3_acqua.dtb /media/$USER/boot/at91-sama5d3_acqua.dtb

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

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

## Links

* [Kernel configuration (acme-acqua_defconfig)](./acme-acqua_defconfig)
* [Device tree (at91-sama5d3_acqua.dts)](./at91-sama5d3_acqua.dts)
* Kernel source changed:
	* [drivers/gpu/drm/panel/panel-simple.c](./panel-simple.c)

@include='bio_tanzilli'

