May 24, 2010

gnuarm and qemu

This tutorial explains compiling arm programs with gnuarm and porting it into qemu emulator.

Requirements: ( ubuntu linux )
  1. gnuarm toolchain ( www.gnuarm.com )
  2. qemu emulator ( sudo apt-get install qemu )
okie.. so here is the step

save the code in add.s
------------------------------------------
.text

start:
mov r0, #5 @r0 <----- 5
mov r1, #4 @r0 <----- 4
add r2, r1, r0
stop:
b stop @infinite loop to stop execution
------------------------------------------

.text is an assembler directive which says that the following section have to be assembled into the code section, rather than the .data section. So what is sections ?

So next is compiling the code with gnuarm toolchain.

arm-elf-as -o add.o add.s
this will assemble the code into an outputfile

arm-elf-ld -Ttext=0x0 -o add.elf add.o
-Ttext=0x0, specified that address should be assigned to the labels, such that the instructions were starting from address 0x0

arm-elf-nm add.elf
To view the address assigned for various labels.

The output file created by ld is in elf format. there are various formats available for storing executable code.
The elf format works fine when you have the os around.
Here we are going to run the program on a bare metal, we have to convert it into a simple binary format.

arm-elf-objcopy -O binary add.elf add.bin
to convert elf to binary format

ls -al add.bin
-rwxr-xr-x 1 akku akku 16 2010-05-24 10:17 add.bin
check the size it is 16bytes, 4 instructions ---> 4bytes ( 32 bit ) each

So we got the simple binary file which has to be flashed into the board.

So next is porting the binary file into qemu arm emulator

ARM processor on reset it will start executing from 0x0 ( whatever there is.. ram, rom, flash ).
On connex board 16MB flash is located at that address. So ARM executes instructions from 0x0 of the flash.
So we need a flash to use with qemu. For that we create a flash file and load it with full of zeros first.

dd if=/dev/zero of=flash.bin bs=4096 count=4096

add.bin file is copied into the beginning of this flash file
dd if=add.bin of=flash.bin bs=4096 conv=notrunc
this is equivalent of programming the bin file onto the flash memory.

Next start qemu
qemu-system-arm -M connex -pflash flash.bin -nographic -serial /dev/null

-M connex : specifies the machine connex to be emulated
-pflash : specifies that flash.bin represents the flash memory
-nographic : specifies that simulation of a graphic display is not required.
-serial /dev/null : specifies that serial port of the connex board is to be connected to /dev/null, so that the serial port data is discarded.

(qemu) info registers

check register R02 it will be 00000009

Referance:

For Web Developer

  open -a "Google Chrome" --args --disable-web-security