Monday 23 November 2015

Qemu setup for ARM-Cortex-A9 Part-1




Many of you people would have used simulators for Microcontrollers like, Proteus and other platform dependent simulators integrated in IDEs.

But for large Embedded systems there is an open source option available to us. QEMU.


One of the most versatile tool for emulating number of microprocessors and other peripherals around it. It gives near native performance by emulating processors on your host system without actually having a hardware board.



QEMU runs in two modes.



  • To run programs build for specific processors
  • To run OS and Kernel build for specific processors
Most of the embedded systems run on ARM architectures, so this tutorial is ARM centered.

In the first case, you can run programs and binaries for ARM without installing ARM OS and in the second case you can install ARM kernel and try to boot the ARM system virtually.



Installing and Compiling QEMU for ARM

First you need to download latest version of QEMU from the official website. There would be a tar.bz2 file downloaded in your linux machine in the Downloads folder. Till this date of writing the latest released version is qemu-2.4.1.tar.bz2. But you can mostly apply the same steps for future versions also just by changing version specific names of the files.

You need to extract the files, configure and install it using below commands of series.


$ tar -jxvf qemu-2.4.1.tar.bz2

You will have a folder named "qemu-2.4.1". change to that directory.



$ cd qemu-0.14.0
$ ./configure –target-list=arm-softmmu
$ make
$ sudo make install

Here, notice that in ./configure command we have selected --target-list=arm-softmmu, as we want to run programs and OS for ARM processor.


Command make will take some time to complete.


After completion of this step you will have two output binaries.



  • qemu-arm
  • qemu-system-arm
The first is to run the ARM programs without installing ARM OS and second is to boot the ARM OS.


Installing ARM Cross-Compile Toolchain

You need to download Linaro tool chain which supports latest ARM Cortex A and Arm Cortex R processors.

You can download the tool chain from launchpad here.

Till this writing the latest release file available for linux is gcc-arm-none-eabi-4_9-2015q3-20150921-linux.tar.bz2 which is about 73 MB on disk. This is pre-built toolchain, so, you don't need to build it. We just need to extract it to proper location and we can use it.

Now extract the bz2 file and will move generated directory to safe place and add it to the PATH environment variable using following commands.



$ cd ~/Downloads
$ tar -jxvf gcc-arm-none-eabi-4_9-2015q3-20150921-linux.tar.bz2
$ mv gcc-arm-none-eabi-4_9-2015q3 ~/
$ export PATH=$PATH:~/gcc-arm-none-eabi-4_9-2015q3/bin/
$ echo $PATH

The first command is to go to Downloads folder. Second command is to extract the files compressed in bz2. After execution of this command you will have new directory named "gcc-arm-none-eabi-4_9-2015q3". We will move it to home directory so we accidentally don't delete the directory from Downloads.


Fourth command is to add the path of bin directory to PATH environment variable. The last command is to test by printing the value of PATH variable. You will notice there will be a path to your bin directory of gcc-arm tool-chain in the value of variable.



Cross Compile test program for ARM

We will make one test file which just prints "hello world...!". You can create file using your choice of text editors like gedit, gvim, emacs or nano. Add the following code to the file and save it with name hello.c.


#include<stdio.h>
int main(){
    printf("Hello World!\n");
}

We will compile this file using arm compiler using below command. After that we will check the file type of object file generated using file command.


$ arm-none-eabi-gcc --specs=rdimon.specs -lgcc -lc -lm -lrdimon -o hello hello.c
$ file hello
hello: ELF 32-bit LSB  executable, ARM, EABI5 version 1 (SYSV), statically linked, not stripped

Here, in first command arm-none-eabi-gcc is the name of compiler and after that some specific arguments follows which are required to compile program for Arm Cortex-A processors. The last part of command is object file name to be generated using -o option and the C file to be compiled.


The second command is to know the file type of the object file which we have just generated. It shows the file hello is of type ELF 32-bit LSB executable, on ARM architecture.


Now we will execute the object file on our x86 machine using QEMU which can generally only be executed on ARM architecture only using below command.



$ qemu-arm -L gcc-arm-none-eabi-4_9-2015q3/arm-none-eabi/lib ./hello
Hello World!

Here, we can see that file hello has printed Hello World! on the terminal. So, we have run the program build for ARM processor on our host PC x86 using QEMU.


We have seen the first mode of QEMU which runs binaries of ARM without installing OS.


In the next part we will see the second mode, in which we will try to install and boot OS using QEMU for arm.


If you like the content, like our facebook page.


No comments:

Post a Comment