#Compiling Linux Kernel 4.2.6

<div class="row">
	<div class="col-md-2">
		<img src="tuxcase_small.jpg" class="img-thumbnail"/>
	</div>
	<div class="col-md-10">
		<div class="text-success lead">
			This article illustrates how to generate a bootable Linux Kernel
			image for Acqua A5, Aria G25 and Arietta G25 boards
		</div>
	</div>
</div>

## Notes

* This procedure was tested on a Linux PC with Ubuntu 15.04
* More info about this Kernel version are [available here](http://free-electrons.com/blog/linux-4-2/)
* It is advisable to have a [Debug Port Interface](/DPI) during the Kernel bootstrap
* Before following this step by step procedure install [these packages](/arm9_toolchain)

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

Download and save in your home directory the Linux Kernel sources:

* [linux-4.2.6.tar.xz](https://www.kernel.org/pub/linux/kernel/v4.x/linux-4.2.6.tar.xz)

Extract the Kernel sources from the compressed file by typing:

<pre class="terminal">
~$ <b>tar xvfJ linux-4.2.6.tar.xz</b>
...
</pre>

Move inside the new folder:

<pre class="terminal">
~$ <b>cd linux-4.2.6</b>
~/linux-4.2.6$
</pre>

At this point I suggest to create a your own personal branch
of Linux Kernel sources with [git](https://git-scm.com/) to keep a trace of any changes from the
Kernel mainline using these commands.

<pre class="terminal">
~/linux-4.2.6$ <b>git init</b>
~/linux-4.2.6$ <b>git add .</b>
~/linux-4.2.6$ <b>git commit -m "Linux vanilla"</b>
~/linux-4.2.6$ <b>git branch acme</b>
~/linux-4.2.6$ <b>git checkout acme</b>
</pre>

Download this patch file from GitHub:

* [linux-4.2.6.patch](https://raw.githubusercontent.com/AcmeSystems/acmepatches/master/linux-4.2.6.patch)

and apply it by typing:

<pre class="terminal">
~/linux-4.2.6$ <b>patch -p1 < linux-4.2.6.patch</b>
</pre>

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

#### Acqua A5
<pre class="terminal">
~/linux-4.2.6$ <b>make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- acme-acqua_defconfig</b>
</pre>

#### Arietta G25
<pre class="terminal">
~/linux-4.2.6$ <b>make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- acme-arietta_defconfig</b>
</pre>

#### Aria G25
<pre class="terminal">
~/linux-4.2.6$ <b>make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- acme-aria_defconfig</b>
</pre>

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

<pre class="terminal">
~/linux-4.2.6$ <b>make ARCH=arm menuconfig</b>
</pre>

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

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

Now compile the the device tree file requested by your board by typing one of these commands:

#### Acqua A5
<pre class="terminal">
~/linux-4.2.6$ <b>make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- acme-acqua.dtb</b>
</pre>

#### Arietta G25
<pre class="terminal">
~/linux-4.2.6$ <b>make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- acme-arietta.dtb</b>
</pre>

#### Aria G25
<pre class="terminal">
~/linux-4.2.6$ <b>make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- acme-aria.dtb</b>
</pre>

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

<pre class="terminal">
~/linux-4.2.6$ <b>make -j8 ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- zImage</b>
...
  Image arch/arm/boot/zImage is ready
</pre>

###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 <b>/lib</b> 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:

<pre class="terminal">
~/linux-4.2.6$ <b>make modules -j8 ARCH=arm CROSS_COMPILE=arm-linux-gnueabi-</b>
...
~/linux-4.2.6$ <b>make modules_install INSTALL_MOD_PATH=./modules ARCH=arm</b>
...
</pre>

##Copy the boot files into the first microSD partition

<ul class="nav nav-tabs">
	<li class="nav active"><a href="#local" data-toggle="tab">Local write</a></li>
	<li class="nav"><a href="#remote" data-toggle="tab">Remote write</a></li>
</ul>

<div class="tab-content">
	<div class="tab-pane fade in active" id="local" markdown="1">

###Direct write on microSD

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

Write the Linux Kernel image, the Device tree blog files in the first microSD
partition and uncompress the modules in <b>/modules/lib</b> directory inside the second
microSD partition:

####Acqua A5

<pre class="terminal">
~/linux-4.2.6$ <b>cp arch/arm/boot/dts/acme-acqua.dtb /media/$USER/boot/at91-sama5d3_acqua.dtb</b>
~/linux-4.2.6$ <b>cp arch/arm/boot/zImage /media/$USER/boot</b>
~/linux-4.2.6$ <b>sudo rsync -avc modules/lib/. /media/$USER/rootfs/lib/.</b>
</pre>

####Arietta G25

<pre class="terminal">
~/linux-4.2.6$ <b>cp arch/arm/boot/dts/acme-arietta.dtb /media/$USER/boot/acme-arietta.dtb</b>
~/linux-4.2.6$ <b>cp arch/arm/boot/zImage /media/$USER/boot</b>
~/linux-4.2.6$ <b>sudo rsync -avc modules/lib/. /media/$USER/rootfs/lib/.</b>
</pre>

####Aria G25

<pre class="terminal">
~/linux-4.2.6$ <b>cp arch/arm/boot/dts/acme-aria.dtb /media/$USER/boot/at91-ariag25.dtb</b>
~/linux-4.2.6$ <b>cp arch/arm/boot/zImage /media/$USER/boot</b>
~/linux-4.2.6$ <b>sudo rsync -avc modules/lib/. /media/$USER/rootfs/lib/.</b>
</pre>

	</div>

	<div class="tab-pane fade" id="remote" markdown="1">


###Remote writing via ssh 

Use this method if you have a working board accessible via LAN, WiFi or USB

Write the Linux Kernel image, the Device tree blog files in the first microSD
partition and uncompress the modules in <b>/modules/lib</b> directory inside the second
microSD partition:

####Acqua A5

<pre class="terminal">
~/linux-4.2.6$ <b>scp arch/arm/boot/dts/acme-acqua.dtb root@</b>ip address<b>:/boot/at91-sama5d3_acqua.dtb</b>
~/linux-4.2.6$ <b>scp arch/arm/boot/zImage root@</b>ip address<b>:/boot</b>
~/linux-4.2.6$ <b>rsync -avc modules/lib/. root@</b>ip address<b>:/lib/.</b>
</pre>

####Arietta G25

<pre class="terminal">
~/linux-4.2.6$ <b>scp arch/arm/boot/dts/acme-arietta.dtb root@192.168.10.10:/boot/acme-arietta.dtb</b>
~/linux-4.2.6$ <b>scp arch/arm/boot/zImage root@192.168.10.10:/boot</b>
~/linux-4.2.6$ <b>rsync -avc modules/lib/. root@192.168.10.10:/lib/.</b>
</pre>

####Aria G25

<pre class="terminal">
~/linux-4.2.6$ <b>scp arch/arm/boot/dts/acme-aria.dtb root@</b>ip address<b>:/media/mmc_p1/at91-ariag25.dtb</b>
~/linux-4.2.6$ <b>scp arch/arm/boot/zImage root@</b>ip address<b>:/media/mmc_p1</b>
~/linux-4.2.6$ <b>rsync -avc modules/lib/. root@</b>ip address<b>:/lib/.</b>
</pre>

<span class="smallnotes">
If this command doesn't work verify if rsync in installed on your board. Use <b>apt-get install rsync</b>.
</span>

	</div>
</div>

##After the first boot

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

<pre class="terminal">
~# <b>depmod -a</b>
</pre>

@include='tanzilli'
