This guide shows how to build a Linux boot image for a LEON3 embedded system. All software is manually compiled from source. No pre-built binaries or magical build scripts are needed.
There are other ways to get Linux running on the LEON3. For an overview, see LEON Linux Development overview (PDF) by Daniel Hellstrom of Aeroflex Gaisler. For example, Gaisler provides pre-built toolchains and scripts to build a custom Linux environment. If you want to get started quickly, you may want to look there first. On the other hand, if you want to build everything from scratch and understand how the pieces fit together, keep reading this guide.
Our boot image will be based on Linux, uClibc and BusyBox. A root filesystem will be built into the boot image as an initramfs. The root filesystem is thus not mounted from a block device like a normal PC would do.
Builing the Linux image is a three-step process:
- Build a toolchain (binutils, gcc, uClibc);
- Prepare a root filesystem (busybox, scripts);
- Build a Linux kernel with root filesystem in initramfs.
Necessary tools:
- Knowledge of the LEON3 processor and experience with GRMON;
- FPGA board with a working LEON3 and at least 32 MB RAM;
- LEON3 configured with MMU enabled and APBUART serial port;
- Linux PC (for example Debian 6.0) to compile the software.
WARNING:
Users and processes are not isolated from each other in a secure way when Linux runs on a LEON3.
The reason is that the LEON3 allows unprivileged processes to write to system management registers %asr17
and %asr19
.
Furthermore, on a LEON3 without FPU, unprivileged processes can crash the system by executing an FPU instruction.
Finally the LEON3 multiply-accumulate instruction is not suported under Linux because the contents of %asr18
are not saved during task switches.
This guide is strongly based on Cross-Linux-From-Scratch.
Other sources are the MKLINUXIMG User Manual (PDF) by Daniel Hellstrom
and Building GCC with uClibc for XScale.
Step 1: Building a cross-compilation toolchain
The goal of this step is to build a cross-compilation toolchain which runs on a normal PC (e.g. x86) but compiles C programs for the LEON3. Later steps will use this toolchain to compile the Linux kernel and to compile system tools.
Step 1.1: Preparation
Create a directory where the cross-compilation toolchain will be installed.
Add the cross-compilation toolchain to the path.
Set ${TOOLS}
to point to the location of the cross-compilation toolchain, for easy reference in the rest of this manual.
Step 1.2: binutils
Download binutils binutils-2.23 from http://www.gnu.org/software/binutils/
Step 1.3: kernel headers
Download Linux 3.8 from http://kernel.org/
Step 1.4: GCC pass 1 (without libc)
The full compiler can not yet be built because it depends on libc. But libc can not be installed without a compiler. To escape this paradox, we first do a partial installation of GCC which can be used to build libc.
Download GCC-4.7.2 from http://gcc.gnu.org/
Step 1.5: uClibc
Download uClibc-0.9.33.2 from http://www.uclibc.org/
Target Architecture: | sparc |
Target Options: | |
Processor type: | SPARC v8 |
Target has MMU: | yes |
Utilize MMU: | yes |
Enable floating point: | yes |
Target has FPU: | no |
Enable C99 math: | yes |
KERNEL_HEADERS: | /somewhere/sparc-linux/usr/include |
General Library Settings: | |
Thread support: | native POSIX threading |
SUSv3 legacy functions: | yes |
SUSv3 legacy macros: | yes |
SUSv4 legacy functions: | yes |
Networking: | |
IPv6: | yes |
RPC support: | yes |
Full RPC: | no |
DNS resolver functions: | yes |
String support: | |
ctype argument checking: | detect and handle |
Installation options: | |
RUNTIME_PREFIX: | / |
DEVEL_PREFIX: | /usr |
HARDWIRED_ABSPATH: | no |
Development: | |
CROSS_COMPILER_PREFIX: | sparc-leon3-linux- |
Step 1.6: GCC pass 2 (full installation)
Now that uClibc is in place, we can do a full installation of GCC. The following commands build a compiler for C and C++. The C++ run-time library takes 600 kByte extra, so you may want to disable C++ if you are not going to use it.
The cross-compiler is now installed in ${TOOLS}/usr/bin
.
It can be invoked as sparc-leon3-linux-gcc
on the command-line to build C programs for the LEON3.
However, some LEON3 support tools from Gaisler (mkprom, mklinuximg) expect the compiler to be named sparc-linux-gcc
instead of sparc-leon3-linux-gcc
.
The name of the compiler is derived from the --target
value during GCC configuration, and this value MUST be "sparc-leon3-linux
", otherwise GCC will not work properly.
We will just create aliases for the essential compiler commands so that the Gaisler tools can find them:
Step 2: Root filesystem
The goal of this step is to create a directory containing a complete set of system libraries, system tools and boot scripts to act as the root filesystem on the LEON3.
Step 2.1: Preparation
Create a directory where the LEON3 root filesystem will be installed.
Set ${SYSROOT}
to point to the location of the root filesystem for easy reference in the rest of this manual.
The root filesystem is put together as a normal user (no root privs needed). Files may be owned by a normal user; ownership will be changed to root when the final filesystem image is built.
Step 2.2: System libraries
Some shared libraries were already installed by GCC and uClibc. These are run-time libraries, so they are needed in the root filesystem. Let's copy them there.
Step 2.3: Busybox
Download busybox-1.20.2 from http://www.busybox.net/
BusyBox Settings: | |
General configuration: | |
Don't use /usr | yes |
Support Unicode | no |
Build Options: | |
CONFIG_CROSS_COMPILER_PREFIX: | sparc-leon3-linux- |
CONFIG_SYSROOT: (this is not the same thing as our ${SYSROOT} ) | /somewhere/sparc-linux |
CONFIG_EXTRA_CFLAGS: | -D_XOPEN_SOURCE |
Library Tuning: | |
systemd support: | no |
Use clock_gettime(CLOCK_MONOTONIC): | yes |
Support infiniband: | no |
Print Utilities: | (disable all) |
Mail Utilities: | (disable all) |
Shell Utilities: | |
hush: | no |
Step 2.4: Base configuration
Boot scripts and configuration files must be installed in ${SYSROOT}/etc
.
The easiest way to explain what is needed there is by providing the whole set of files as a download.
Download http://jorisvr.nl/files/leon3/leon3-linux-stuff-20130228.tar.gz.
This package contains boot scripts derived from Cross-Linux-From-Scratch.
The following will be set up:
etc/inittab | (enable login on console and serial port) |
etc/passwd | (just a "root" account with empty password) |
etc/group | (default system groups) |
etc/fstab | (mount proc filesystem) |
etc/hosts | (mapping IP addresses to names) |
etc/shells | (list of allowed user shells) |
etc/protocols | (copied from Debian) |
etc/services | (copied from Debian) |
etc/issue | (welcome message) |
etc/rc.d | (startup scripts for network, telnet, dropbear) |
The LEON3 will boot with IP address 192.168.0.80 for the Ethernet port.
Edit ${SYSROOT}/etc/rc.d/init.d/network
if you want to change the IP address.
Edit ${SYSROOT}/etc/rc.d/init.d/telnetd
if you want to start the telnet daemon during system boot.
Step 2.5: Zlib (optional, but required for Dropbear)
Download zlib-1.2.7 from http://www.zlib.net/
Step 2.6: Dropbear SSH (optional)
Download dropbear-2012.55 from http://matt.ucc.asn.au/dropbear/dropbear.html
Prepare an RSA host key for Dropbear:
Step 2.7: IPtables (optional)
Download iptables-1.4.17 from http://www.netfilter.org/projects/iptables/
Step 2.8: IPerf (optional)
Download iperf-2.0.5 from http://sourceforge.net/projects/iperf/
(Many thanks to Marc Singer for his explanation of rpl_malloc.)
Step 2.9: Strace (optional)
Download strace-4.7 from http://sourceforge.net/projects/strace/
Step 2.10: SoCat (optional)
Download socat-1.7.2.1 from http://www.dest-unreach.org/socat/
Step 2.11: RSync (optional)
Download rsync-3.0.9 from http://rsync.samba.org/
Step 3: Linux kernel
The goal of this step is to create a Linux kernel boot image. The root filesystem, built in step 2, will be included in the Linux kernel as an initramfs image.
Step 3.1: Configure the kernel
Download the Linux kernel from kernel.org.
Set ${LINUXSRC}
to point to the Linux source directory, for easy reference in the rest of this manual.
A patch must be applied to avoid crashing the kernel on FPU instructions. Without this patch, the system will boot and work fine, but any user can crash the system by executing an invalid instruction.
A patch must be applied to add support for PS/2 keyboard and mouse devices that are attached to the LEON3 processor via APBPS2. (Patch by Daniel Hellstrom from Gaisler. More kernel patches are available in the leon-linux package from Gaisler.)
Configure the Linux kernel.
Many defaults can be disabled to make the kernel smaller.
(Optionally copy leon3-linux-stuff-20130228/linux-3.8-config
to .config
and use it as a starting point.)
64-bit kernel: | no |
General: | |
Support paging (swap): | no |
Timers, High resolution: | no |
CPU accounting: | disable all |
Initramfs: | yes |
Initramfs source file: | ../sysroot.cpio |
Initramfs compression: | gzip |
Loadable modules: | no |
Block layer: | |
Partitions, Adv sel: | no |
IO schedulers: | disable all |
Processor: | |
SMP: | no |
Sparc Leon family: | yes |
Bus options: | |
Support for PCI: | yes (REQUIRED!) |
GRPCI: | yes |
Network: | |
Options: | |
PF_KEY: | no |
Large receive offload: | no |
802.1d bridging: | no |
IPX: | no |
Appletalk: | no |
QoS: | no |
IrDA: | no |
Device Drivers: | |
SCSI device support: | no |
Serial ATA: | no |
Multiple devices (RAID): | no |
Network device: | |
Ethernet, Aeroflex Gaisler GRETH: | yes |
Ethernet, all others: | no |
USB: | disable all |
Wireless: | no |
Input: | |
Mouse, PS/2: | yes |
Hardware IO, GRLIB APBPS2: | yes (keyboard IRQ 5) |
Character devices: | |
Serial, GRLIB APBUART: | yes (console: yes) |
Multimedia: | no |
Graphics: | |
Framebuf, Aeroflex Gaisler: | yes |
Framebuf, Console: | enable Sparc 8x16 font |
Sound card: | no |
USB: | no |
Real Time Clock: | no |
IOMMU: | no |
File systems: | (disable most, but keep proc and tmpfs) |
Step 3.2: Assemble root filesystem image
Run a script from the Linux kernel source to make a list of files that are to be included in the initramfs image. Give setuid permission to the busybox binary. Add a bunch of device nodes and mount points. Run a helper program from the Linux kernel source to build the actual CPIO archive containing the root filesystem.
(Or use the script leon3-linux-stuff-20130228/make_initramfs.sh
to do this entire step.)
Step 3.3: Compile the kernel
Step 3.4: Create executable image for LEON3
Before the kernel can run, it must be loaded in a specific location in RAM and the MMU must be initialized. The kernel also needs the OpenBoot API to handle platform-specific functions. These functions are provided by the LEON Linux RAM loader from Gaisler.
Download mklinuximg-2.6.36-2.0.3 from http://www.gaisler.com/index.php/downloads/linux
Two patches must be applied. The first patch makes the loader work with recent versions of the Linux kernel. The second patch works around a linker issue with binutils 2.23.
Run a script to compile the loader and merge it with the Linux kernel from the previous step.
The file linux.dsu
is proper LEON3 executable.
It can be loaded and executed with GRMON:
Step 3.5: Create PROM image for LEON3 (optional)
Linux can boot directly from a flash PROM. In this case, a PROM image must be created to write into the flash device. We will use MKPROM2, the boot loader from Gaisler.
Download MKPROM2 from http://gaisler.com/index.php/downloads/compilers.
Compile MKPROM2:
Run MKPROM2 to create a Linux PROM image. MKPROM2 needs command-line options that are specific for the board and LEON3 configuration (type and amount of memory, system clock frequency, etc.).
The file linux.prom
can be flashed into the PROM device, for example with GRMON.
-- The End