What I learn about howto cross compile c source code for ARM CPU on an x86 ubuntu machine

1. Install the gnu c compiler for ARM (from: cross-compiling-static-c )
$ sudo apt-get install gcc-arm-linux-gnueabi

And what do 'eabi' mean? EABI is the new Application Binary Interface (ABI) specification for the ARM Architecture. (from: geekwentfreak) In short, we can link binaries which compiled by any compilers who follow the EABI specification.

2. Write a simple hello.c program and cross compile it
$ arm-linux-gnueabi-gcc hello.c -o hello

3. So, we get a binary file 'hello', which can execute on ARM platform. But, how can we run and test it? Of course, we can copy it to an ARM machine, like Raspberry Pi, Pandaboard, BeagleBoard, and more. But more convenient way is using emulator, so We can do all the job on one machine. Let's install QEMU: (from: cnx-software)

$ sudo apt-get install qemu-kvm qemu-kvm-extras
$ qemu-system-arm -version
QEMU emulator version 1.2.0 (Debian 1.2.0-2012.09-0ubuntu1), Copyright (c) 2003-2008 Fabrice Bellard

Open web browser, go to http://people.debian.org/~aurel32/qemu/armel/
At this web page, Aurélien Jarno of Debian has prepared a number of pre-packaged Debian qemu images for ARM architectures. I chose to use 2.6.32-5 desktop version. So, let's download 3 files from this page:

debian_squeeze_armel_desktop.qcow2
initrd.img-2.6.32-5-versatile
vmlinuz-2.6.32-5-versatile

And using these files and QEMU software we have install before, we can start the emulator using command below:
$ qemu-system-arm -M versatilepb -kernel vmlinuz-2.6.32-5-versatile -initrd initrd.img-2.6.32-5-versatile -hda debian_squeeze_armel_desktop.qcow2 -append "root=/dev/sda1"

PS: this image preset two users (root and user), having the password the same as their name.
PS: the default setting using 128MB memory, we can set to 256MB by adding '-m 256' to the command line after 'qemu-system-arm'.
PS: more about the qemu image see QEMU/Images
PS: qemu can run Debian image for Raspberry Pi, see raspberry-pi-releases-1st-sd-card-image

By default, qemu using 'user mode networking', which mean the guest machine can access outside but outside can't access it. So, it's behavior like NAT. But, ICMP packet not working. So, we can't ping anything. I have try to open web browse and it work. I alse try to do 'apt-get update', it also work.
But we can't transfer file from host machine to the guest machine (emulator), that's very inconvenient. Sure, there's many way to overcome this problem. One of the most easy way is using redirect port(from: QEMU/Networking). I will redirect host port 2001 to guest machine port 22. So, when we access host machine port 2001, actually we are access the guest machine port 22. In this way, we can ssh to emulator and using scp command to copy file to emulator. How easy? Just add '-redir tcp:2001::22' to the command line after 'qemu-system-arm'. That's it.

But wait, we still need to check if guest machine have ssh server running? And, I have check that, this image do not install ssh server. So, we need to install it. (see: debian-linux-install-openssh-sshd-server) Please go to the guest machine:

$ su -
# apt-get update
# apt-get install openssh-server
# service ssh start
# ssh localhost

Ok, everything should be fine. Then let's go back to our host machine. And at the terminal, type: (from: linux-scp-file)

$ ssh -P 2001 user@localhost
user@localhost's password:
Linux debian-armel 2.6.32-5-versatile #1 Wed Jan 12 23:05:11 UTC 2011 armv5tejl

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Tue Aug  6 03:56:18 2013 from 10.0.2.2
user@debian-armel:~$
-----------------------------------------------------------------------------------------------------

See that? We can ssh to our guest machine from host machine. And actually, we can ssh to our guest machine from another computer, if the host machine can be reach by it.

So, to transfer file to the guest machine could be done by this command: (on host)
$ scp -P 2001 ~/hi user@localhost:/home/user/hi

And, inversely we can transfer file from guest to host by: (on host)
$ scp -P 2001 user@localhost:/home/user/hi ~/hi

Finally, We have a emulator for ARM. We can cross compile c source code on our host, and using scp to put that binary executable file to emulator. And running that program to see the result. If ok, we can copy that file to the true ARM board. The development process would be more convenient now.

Note: the final qemu command would be :
qemu-system-arm -m 256 -M versatilepb -kernel vmlinuz-2.6.32-5-versatile -initrd initrd.img-2.6.32-5-versatile -hda debian_squeeze_armel_desktop.qcow2 -redir tcp:2001::22 -append "root=/dev/sda1"

Note: the emulator's performance just ok. (mean: not good!)
And one more source for the same question: debian_arm_qemu
A video show the same thing:
video

留言

這個網誌中的熱門文章

D-BUS學習筆記

關於藍牙裝置找尋(inquiry, scan)兩三事

Cisco Switch學習筆記: EtherChannel