Hawkboard/Programming Guide -Linux(ARM)

Linux Programmer’s Guide

Prerequisite
This guide assume basic knowledge of Kernel(uImage) and RootFS and prior knowledge of C programming.

Introduction to Toolchain
Toolchain are Chain or set of programming tools for building an apps, it could include following :
 * Editor for writing program
 * Assembler
 * Compiler
 * Preprocessor
 * Linker loader
 * Debugger
 * Binutils etc.

Toolchain Type
a)Native toolchain

Native toolchains are tools which makes binaries for the environment where toolchain itself is being run.i.e Host and target are same platform. This type of Compilation process is called Native compilation .Meaning that the program is compiled on same platform where it has to be run. Most common example of Native compilation is compilation for normal PC based program ,they are compiled and run on same machine.

Native compilation doesn't involve any Makefile changes hence ease in compilation.

Earlier embedded devices had very less memory and space and also limited Input-output peripherals, hence Native compilation was not possible or very difficult. But with the advent of powerful embedded ARM processor its now possible to make application on the platform itself. Same can be done in Hawkboard too but Compilation will be slower as compare to that in x86 machine.

b)Croos Toolchain

Toolchain which are used to compile the application that will run of different machine(architecture) .This is usually used for embedded apps (e.g ARM programming) You program/compile in x86 environment and run in ARM Board. This type of compiling is called cross compiling.

Since native compilation is either not possible or very slow ,Cross compilation present a better alternative for faster development .Only drawback is to properly set build environment and Makefiles.Cross Compilation is most common way of Development in Embedded System.

c)Hybrid Toolchain

It similar to native toolchain,but infact toolchain run in virtualised environment on Host.Simple example is QEMU -ARM Running on X86 Linux & you are developing application inside this Virtual environment.

This has advantage of both Native and Cross compilation.Speed of cross compilation & Ease of Native development.

Various solution exists, One is scratchbox ,another is ChrootEABI feature in Ubuntu.

d)Canadian Toolchain The Canadian Cross is a technique for building cross compilers for other machines. Given three machines A, B, and C, one uses machine A to build a cross compiler that runs on machine B to create executables for machine C. Not practiced normally.

Native Toolchain & Compilation
There are various ways to compile the application natively on hawkboard.

1. Using Ubuntu Rootfs
If you are using Ubuntu as Development Host,Ubuntu provides a Script Rootstock that provides a easy way of making Root Filesystem.Give gcc,vi,g++ as seed parameter to rootstock script and you will have a GNU Toolchain inside the rootfs.

Then write/compile/run as you do for x86 program.

This needs rootstock to installed on Ubuntu (9.10)

apt-get install rootstock

Once rootstock is installed run following line on host pc to make Ubuntu Rootfs

host$sudo rootstock --fqdn localhost.com --login --password --imagesize 2G --dist jaunty --serial ttyS2 --seed ubuntu-minimal,gcc,vim,g++,openssh,subversion,apt

is user name is password that will be required once this rootfs is booted on hawkboard

imagesize is max size allocated to rootfs.you can keep it same as ur USB Drive /SD card.

Seed parameter tells which packages to be included in rootfs.include atleast gcc,vim (any editor),build-essential packages.

This will take couple of hours to completely download packages you have mentioned and to make a rootfs.

After completion of command it will create some file such as armel-rootfs-2010xxxxxxxx.tgz This is compressed rootfilesytem extract it on a SD card or NFS Folder and boot using one of the method described in beginners/guide

Set proper user group and rights of File

sudo tar xfp armel-rootfs-**********.tgz chown -R root.root * chmod -R 777 * chmod 440 etc/sudoers chown root:root usr/bin/sudo chmod 4111 usr/bin/sudo

The sample RootFS Containing gcc,vi etc is available for Download at

http://hawktool.googlecode.com/files/RootFS_v1.tar.bz2

2.Angstrom Root FS
Select Hawkboard from the List http://www.angstrom-distribution.org/narcissus/

Angstrom Distribution provides following dev packages
 * 1) Python
 * 2) Perl
 * 3) Mono (C#, .NET)
 * 4) Toolchain
 * 5) OProfile
 * GDB
 * 1) Busybox replacements

Select atleast Toolchain to get GCC.

3. Fedora ARM RootFS
Fedora like ubuntu's rootstock provide a script rfsbuild that generates rootfs you can include gcc in package list to include it as native toolchain.

More info http://fedoraproject.org/wiki/Architectures/ARM/RfsBuild

4. Rootfs without GCC
Even though you havent selected gcc as packages in any of the above distribution you can use the native gcc toolchain from Impactlinux. Download from here http://impactlinux.com/fwl/downloads/binaries/native-compiler-armv5l.tar.bz2

Extract and properly export the path of native-compiler-armv5l/bin and you can compile the apps on hawkboard. use [Static linking] instead of dynamic linking. gcc hello-world.c -static

This toolchain uses uClibc instead of standard glibc as library which better suited for embedded environment.

Cross Toolchain & Compilation
There are Lots of readily available tool chains that you can extract and start compiling apps on x86 architecture and finally load and run on hawkboard.

1.Codesourcery Toolchain
This is the most common toolchain used for OMAP.Codesourcery provides both a free version and licensed release toolchain. Free version can be downloaded from the link below

http://www.codesourcery.com/public/gnu_toolchain/arm-none-linux-gnueabi/arm-2009q3-67-arm-none-linux-gnueabi-i686-pc-linux-gnu.tar.bz2

host$ chmod +x arm-2009q3-67-arm-none-linux-gnueabi.bin Run the installer and follow the steps it presents to install the package. host$ ./arm-2009q3-67-arm-none-linux-gnueabi.bin or

if you are using tar file than extract

host$Tar xvf arm-2009q3-67-arm-none-linux-gnueabi-i686-pc-linux-gnu.tar.bz2

If you installed in the default path, you can find the files at /home/ /CodeSourcery. Ensure the Code Sourcery tools are in the path by adding the following host$ export PATH=/home/ /arm-2009q3/bin:$PATH to the ~/.bashrc file. You can then source the ~/.bashrc file with the following command or restart the bash shell

host$ source ~/.bashrc

arm-none-linux-gnueab-gcc -Wall -o hello hello.c

Note: 1.On ubuntu it might fail as it uses 'dash' as a shell (the Debian shell). To reconfigure for the bash shell, use the following command: host$ sudo dpkg-reconfigure -plow dash 2.If you are using 64-bit Linux host, ensure that ia32-libs are installed host$ sudo apt-get install ia32-libs

2.ImpactLinux Toolchain
ImpactLinux Provides a uClibc based Toolchain

http://impactlinux.com/fwl/downloads/binaries/cross-compiler-armv5l.tar.bz2

Extract native-compiler-armv5l.tar.bz2 using tar xvf cross-compiler-armv5l.tar.bz2

Source the Path of cross-compiler-armv5l\bin and compile the C program using

3.Cross Toolchain by Fedora Arm Project
If you are using a Fedora as host environment than easiest way is to use Fedora ARM Toolchain. Fedora provides arm toolchain packages for i386 and x86_64 that are built from the same sources as the Fedora native toolchain packages (binutils, gcc, gdb, glibc.) To install this toolchain on your Fedora machine, do:
 * 1) cd /etc/yum.repos.d/
 * 2) wget http://fedora-arm.wantstofly.org/cross/cross.repo
 * 3) yum install armv5tel-redhat-linux-gnueabi-gcc

This will install everything necessary to run the C compiler and cross-build ARM libraries and binaries that are entirely binary compatible with the native Fedora/ARM libraries and binaries.

Cross-compiling ARM userland binaries

To cross-compile the canonical Hello World example for ARM, do: $ armv5tel-redhat-linux-gnueabi-gcc -Wall -o hello hello.c

Cross-compiling ARM kernels

You can also use the cross-toolchain to cross-build an ARM Linux kernel, by changing these two lines in the top-level Makefile: ARCH           ?= $(SUBARCH) CROSS_COMPILE  ?= to: ARCH           ?= arm CROSS_COMPILE  ?= armv5tel-redhat-linux-gnueabi-

and then building your kernel as usual.

4. Custom Toolchain
There are various Toolchain building Scripts to make a Custom toolchain ,If you are not planning a prebuilt Toolchain from Codesoucery.

1.Crosstool-NG

 * Crosstool-NG is script to make toolchain,its very versatile tool.
 * Some notable features are


 * menuconfig like interface
 * Choice of Library uClibc, glibc or eglibc
 * Multiple Target Support -ARM,MIPS,PPC..
 * Linux or Bare Metal

2.Buildroot
Buildroot is a set of Makefiles and patches that makes it easy to generate a cross-compilation toolchain and root filesystem for your target Linux system using the uClibc C library. Buildroot is useful mainly for people working with small or embedded system.

3.Scratchbox
Scratchbox is a cross-compilation toolkit designed to make embedded Linux application development easier. It also provides a full set of tools to integrate and cross-compile an entire Linux distribution.

4.Bitbake(Openembedded)
Bitbake is the tool used by OpenEmbedded. The best way to get started is probably by just building an existing distribution that uses openembedded (e.g. Ångström, see http://www.angstrom-distribution.org for details).

Transferring Binaries/Executable to Hawkboard
Once the application has been compiled on the Host ,It needs to Transferred to Hawkboard. One of many ways could be used.

1.NFS Boot:- If you are using NFS Boot to load RootFS,Then you can simple copy paste the Binary in home dir or any folder of NFS Shared folder.Once Binary has been transferred execute it as usual.

2.SD/USB Drive-Binary can also be transferred if you SD Card or USB Port is empty by inserting the Card or USB Drive Containing the Binary,Mounting and subsequently accessing it.

3.FTP /SCP :-If RootFS contains ftp or scp you can also used that to copy compiled binary from Host to Target(Hawkboard)

3.Wget Preferred method would be to wget the file from Host,It avoids need of setting FTP or SCP,only thing is that you need to share the File containing binary through HTTP Server.

Python has a simple HTTP Server in built,And since most Linux machine will containing python to share the binary over HTTP on host host$ cd programs host$ python -m SimpleHTTPServer

This will start http server on port 8000 ,on Target simply run wget to access the file

Target$ wget http:// :8000/a.out

build-arm-chroot
There is module in ubuntu that allows binaries of other machine(ARM,MIPS etc) to allowed to run along with X86 binaries sudo apt-get update sudo apt-get install qemu-arm-static sudo build-arm-chroot jaunty eabi-chroot

This creates a chroot environment in folder /home/ /eabi-chroot

user@loval$ sudo chroot eabi-chroot root@local:/# uname -m armv5tel



Now you can build/test packages etc, the chroot works like any other x86 chroot on your system as long as you have the qemu-arm-static package installed with the advantage that you can make use of all resources your host system provides (CPU speed, RAM, diskspeed etc) To copy files or Native toolchain to Chroot environment you must exit out of chroot and copy as normal then chroot to "chroot-eabi" directory.

Native compilation in chroot environment
Without chrooting i.e. cd to /home/ /eabi-chroot & Copy the ImpactLinux's Native Toolchain

(native-compiler-armv5l.tar.bz2) to /home/user/eabi-chroot/

Export path of /home/user/eabi-chroot/native-compiler-armv5l/bin Now compile any program as Gcc hello.c –static

C libraries - uClibc & GlibC
A C library is precompiled object file of Standard C funtions.

GNU Toolchain i.e GCC uses Glibc as Standard C library.

While most of the Embedded Devices uses Size optimize Library uClibc. Hence Statically Linked binaries using uClibc are much smaller than Glibc Linked Binary

Below shows a comparison of Hello-World Program Statically combined on two Libraries
 * Left uses Codesoucery Toolchain that uses Glibc (634kB)
 * Right uses Impactlinux Toolchain that uses uClibc (22kB)



Static Vs Dynamic Linking
Static linking is to combine an application program with the parts of various library routines it uses.

Main advantage in using this is that you don't care about dependencies ,binary will be able to run on different

machine.Also Statically Linked binary runs faster

Disadvantage is the RAM & Flash is wasted by duplicating the same library code in every static linked process,as statically linked file is significantly of greater size.

to Statically link libraries to Code add -Static to gcc. gcc -static Hello-world.c

output of ldd & File' command $ gcc hello.c -static $ file a.out a.out: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), statically linked, for GNU/Linux 2.6.15, not stripped $ ldd a.out not a dynamic executable

Dynamic Linking Dynamic linking involves loading the subroutines of a library into an application program at load time or runtime, rather than linking them in at compile time; the subroutines remain as separate files on disk.Libraries can be called by multiple programs. This saves lots of space ,only drawback being porting of application you need to have depended libraries copied to another system when you are porting your apps.

Normally a program when compiled is dynamically linked ,this can be check using ldd & File commmand

$ gcc hello.c $ file a.out a.out: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.15, not  stripped $ ldd a.out libc.so.6 => /lib/tls/i686/cmov/libc.so.6 (0x00b0d000) /lib/ld-linux.so.2 (0x00711000)

Shows libc.so (Standard C library)

Shows libraries being used



Useful Linux Commands

 * ldd
 * 1) file