Building the kernel - crossdev

Probably you'll never need to build your own kernel due to the huge amount of drivers you have already on the base system we provide to you, but if you need to recompile it this "how to" will be really usefull.

To compile your own kernel you have to build a toolchain able to produce binary files that can run on your Netus system. Netus board is powered by a ARM926EJ-S™ ARM® Thumb® Processor, this means that you have to prepare a compiler for ARMV5TEJL architecture.

Requirements

A Gentoo Linux 2008.0 running system on your PC


You can simply install GENTOO by downloading it from here:
Gentoo 2008.0 LiveCD
(up to 710 megabytes depending on arch)
i686 amd64

Boot your PC from the live cd and use the wizard to install the system.
Gentoo installation, network and system configuration is fully explained in the handbook available here.


What we are going to do

First of all you need to create a cross-compiler. What is cross compiling? Cross-compiling is building binaries for one architecture (the target) on another (the host). The target is NETUS the host is your PC. To perform this job Gentoo offers you a great tool, crossdev.
What is crossdev? Gentoo created the crossdev script to automate the complex process of building a cross compiler.

Let's go! Crossdev installation

On the host machine check if you have a overlay. A overlay is a "portage layer". To check if you have this launch:

cat /etc/make.conf

Your file should look like this:

localhost ~ # cat /etc/make.conf
# These settings were set by the catalyst build script that automatically
# built this stage.
# Please consult /etc/make.conf.example for a more detailed example.
CFLAGS="-O2 -march=i686 -pipe"
CXXFLAGS="-O2 -march=i686 -pipe"
# WARNING: Changing your CHOST is not something that should be done lightly.
# Please consult http://www.gentoo.org/doc/en/change-chost.xml before changing.
CHOST="i686-pc-linux-gnu"
PORTDIR_OVERLAY="/usr/local/portage"
SYNC="rsync://rsync.europe.gentoo.org/gentoo-portage"

If the overlay is not present add the line PORTDIR_OVERLAY="/usr/local/portage" to your file
You are ready to launch emerge crossdev.
emerge crossdev

This will provide you with the crossdev script. This script automates the steps necessary to build a toolchain. These steps are, in short:

  1. binutils: Build a cross-binutils, which links and processes for the target architecture.
  2. linux-headers: Install a set of C library and kernel headers for the target architecture.
  3. glibc-headers: Additional header files
  4. gcc/c (lite): Build a basic (stage 1) gcc cross-compiler. This will be used to compile the C library. It will be unable to build anything almost else (because it can't link against the C library it doesn't have).
  5. c-libraries: Build the cross-compiled C library (using the stage 1 cross compiler).
  6. gcc/c (full): Build a full (stage 2) C cross-compiler.

All cross toolchains will be kept locally in the overlay, separate from native tools.

Note: crossdev uses the first overlay (which is the least important one), so if you use layman you'd probably like to have lines like following in make.conf
File: /etc/make.conf
PORTDIR_OVERLAY="/usr/local/portage"
source /usr/portage/local/layman/make.conf
PORTDIR_OVERLAY="/usr/local/portage $PORTDIR_OVERLAY"
Layman places additional overlays before your one, so the third lines is needed to make your one the first one.

The script I have used is:

USE=-* crossdev --b 2.18-r3 --g 4.3.3 --l 2.6.1 --k 2.6.28-r3 -t armv5tejl-softfloat-linux-gnueabi

This will build a cross-compiling toochain for ARM® armv5te machines.

The meaning of armv5tejl-softfloat-linux-gnueabi is arch-vendor-OS-libc, arch specifies the CPU architecture, vendor specifies the hardware platform or vendor, OS is the operating system, and libc is the C library to use. Only arch is strictly required in all cases—if you omit one or more of the remaining fields crossdev attempts to fill in the missing values with its best guess. Nevertheless—for Linux machines at least—it's good practice to specify all four fields. The libc field is not supported for Gentoo/FreeBSD, so for these machines it must always be omitted.

By default, the newest stable version of the binutils, libraries, and C compiler will be used, but generally they do not work and you have to search for the right tuple, use the suggested tuple that is running for me. It is quite often the case they will not compile themselves through the entire build process. Less bleeding edge versions can be specified with additional flags:

--b 2.18-r3   # specifies the version of binutils
--g 4.3.3     # specifies the version of gcc
--l 2.6.1     # specifies the version of the tuple-specified libc
--k 2.6.28-r3    # specifies the version of the kernel headers

It is recommended trying older versions, particularly of gcc an binutils, if the script fails.

If you want to remove a toolchain, use the clean flag:

crossdev -C armv5tejl-softfloat-linux-gnueabi

This will unmerge the packages created by crossdev, everytime it fails compiling restart always from clean.

I have also created a toolchain using UCLIBC using the following tuple:

USE=-* crossdev -v --b 2.19.1 --g 4.1.2 --l 2.9_p20081201-r1 --k 2.6.28-r1 --target armv5te-softfloat-linux-uclibceabi

But I have opted for GLIBC because due to the lot of space on the system I prefer to use the standard library that is usually compatible with a wide range of applications.

If the toolchain compiles correctly we can proceed with cross scripts for compiling and emerging sources.
To check if the toolchain works simply launch:

localhost ~ # armv5tejl-softfloat-linux-gnueabi-gcc --version
armv5tejl-softfloat-linux-gnueabi-gcc (Gentoo 4.3.3 p1.0, pie-10.1.5) 4.3.3
Copyright (C) 2008 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

localhost ~ #

If all is ok you'll obtain the output above.


PREPARING THE HOST ENVIRONMENT
Setting up sysroot for cross development

When cross developing, you'll usually want to avoid polluting the host system's environment with the products of cross development activity, and to also provide Portage with an alternate configuration and environment for cross emerge operations. The simplest solution for this is to use sysroot.

For convenience let's set the environment variable SYSROOT on our host system to point to the directory of the cross toolchain we'll be working with.

export SYSROOT="/usr/armv5tejl-softfloat-linux-gnueabi"

Now you have to create the make.conf inside the SYSROOT /usr/armv5tejl-softfloat-linux-gnueabi/etc/make.conf

The make.conf file should look like this:

#
CHOST=armv5tejl-softfloat-linux-gnueabi
CBUILD=i686-pc-linux-gnu
ARCH="arm"
GENTOO_MIRRORS="http://open-systems.ufl.edu/mirrors/gentoo \
http://prometheus.cs.wmich.edu/gentoo \
http://mirror.datapipe.net/gentoo \
http://ftp.ucsb.edu/pub/mirrors/linux/gentoo/"
ROOT=/usr/${CHOST}/
ACCEPT_KEYWORDS="~x86 arm ~arm"
USE="${ARCH} zlib bindist make-symlinks minimal \
input_devices_keyboard input_devices_evdev \
video_cards_fbdev video_cards_dummy"

#VIDEO_CARDS="fbdev dummy"

INPUT_DEVICES="keyboard"
#USE_EXPAND="video_cards input_devices"
MARCH_TUNE="-march=armv5t -mtune=arm926ej-s"
CFLAGS="-Os -pipe ${MARCH_TUNE} -fomit-frame-pointer -I${ROOT}/usr/include/ -I${ROOT}/include/"

CXXFLAGS="${CFLAGS}"
LDFLAGS="-L${ROOT}/usr/lib -L${ROOT}/lib"

PKG_CONFIG_PATH="${ROOT}/usr/lib/pkgconfig/"
MAKEOPTS="-j8"
FEATURES="-collision-protect sandbox buildpkg noman noinfo nodoc"

PORTDIR_OVERLAY="/usr/local/portage"
PKGDIR=${ROOT}/packages/
PORTAGE_TMPDIR=${ROOT}/tmp/
PORTAGE_WORKDIR_MODE=2775
PORTAGE_ECLASS_WARNING_ENABLE=0

CLEAN_DELAY=0
EPAUSE_IGNORE=1
EBEEP_IGNORE=1

Then copy the host system's /etc/make.globals to the sysroot and create the directory for local profile configuration files:

# cp /etc/make.globals "${SYSROOT}/etc"
# mkdir -p "${SYSROOT}/etc/portage/profile"

We need to create a symlink for the sysroot Portage profile. Our example target system calls for uClibc, so we'll choose a pre-existing Portage profile for a minimal PPC uClibc base system:

# ln -sn /usr/portage/profiles/default/linux/arm/2008.0 "${SYSROOT}/etc/make.profile"

xmerge: A simple cross emerge wrapper

Creating cross-compiled binaries usually entails using emerge in conjunction with a few modified environment variables. This can become tedious, so we can make things easier by handling everything in a script that wraps emerge. Here's an example of such a wrapper we'll call xmerge:

create a file named /bin/xmerge and give it execute permisison

Script content will be:

#!/bin/sh

CHOST=armv5tejl-softfloat-linux-gnueabi

export CBUILD=$(portageq envvar CHOST)
export PORTAGE_CONFIGROOT=${SYSROOT}
if [ "$1" = "--root" ] ; then
export ROOT=$2
shift 2
else
export ROOT=${SYSROOT}
fi
exec emerge "$@"

Now create another command /bin/xmake
xkmake: A cross kernel make wrapper

#!/bin/sh
exec make ARCH="arm" CROSS_COMPILE="armv5tejl-softfloat-linux-gnueabi-" INSTALL_MOD_PATH="${SYSROOT}" "$@"

Cross-compiling a Kernel

# xmerge gentoo-sources

This will put kernel sources inside your /usr/armv5tejl-softfloat-linux-gnueabi/usr/src/linux-2.6.28-gentoo-r2 folder
After the sources will be merged edit the main Makefile

ORIGINAL:


# Set the ARCH and CROSS_COMPILE default values
#ARCH ?= $(SUBARCH)
#CROSS_COMPILE ?=

CHANGE IT IN:

# Set the ARCH and CROSS_COMPILE default values
ARCH ?= arm
CROSS_COMPILE ?= armv5tejl-softfloat-linux-gnueabi-


Then launch

# cd "${SYSROOT}/usr/src/linux"
# xkmake menuconfig


And configure the kernel parameters for the CPU
Atmel ATSAM9G20 as in the screenshots below:

first of all under System type set

ARM system type (Atmel AT91) --->



Atmel AT91 System-on-Chip ---> Atmel AT91 Processor (AT91SAM9G20) --->


Disable [ ] VGA text console Under device drivers > Graphics support ---> Console display driver support ---> [ ] VGA text console


Under Character devices ---> Serial drivers --->

enable

[*] AT91 / AT32 on-chip serial port support
[*] Support for console on AT91 / AT32 serial port
[*] Support DMA transfers on AT91 / AT32 serial port


After you have completed this configuration you are ready to lauch xmake

# xkmake

The kernel will be compiled with the selected options an the Image will be locate into:

/usr/armv5tejl-softfloat-linux-gnueabi/usr/src/linux-2.6.28-gentoo-r2/arch/arm/boot/Image

Now you need to emerge mkimage on your HOST system to create the uImage to flash inside your NETUS target

# emerge dev-embedded/u-boot-tools

Now to create the image just launch

# mkimage -A arm -O linux -C none -T kernel -a 20008000 -e 20008000 -n linux-2.6 -d /usr/armv5tejl-softfloat-linux-gnueabi/usr/src/linux-2.6.28-gentoo-r2/arch/arm/boot/Image /uImage

Flash your new kernel using Atmel SAM-BA inside your NETUS
http://www.linux4sam.org/twiki/bin/view/Linux4SAM/GettingStarted




This how to is extracted and adapted to Netus starting from http://en.gentoo-wiki.com/wiki/Crossdev

Contacts
powered by gentoo