Sunday, April 27, 2014

Show “Hello World” on Qemu

Show “Hello World” on Qemu

Before we continue, make sure your Linux already has Qemu inside. If you want to install, you can refer to my previous posting, Install Qemu on Ubuntu. You also need to check whether your Linux already has GCC cross compiler or not. To check whether the compiler already installed on your linux, you can check by using:
$ arm-linux-gnueabi-gcc --version
And it will show, example:
arm-linux-gnueabi-gcc (Ubuntu/Linaro 4.7.3-1ubuntu1) 4.7.3
Copyright (C) 2012 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
If your Linux doesn’t has the compiler, you can install it:
$ sudo apt-get install gcc-arm-linux-gnueabi
Then, you need to prepare directory for your project. Example:
$ cd
$ mkdir Project
$ cd Project
Now we are in Project directory. In this directory, we will create sample code using C language to print “Hello World”, and compile linux source code. Then we will launch our software on Qemu.
Create our sample code, and name it as init.c. This is the code:
#include <stdio.h>

void main() {
  printf("Hello World!\n");
  while(1);
}
Compile the code use gcc cross compiler and put in ramdisk:
$ arm-linux-gnueabi-gcc -static init.c -o init
$ echo init|cpio -o --format=newc > initramfs
The next step is preparing directory for our Linux source code.
$ mkdir Sources
$ cd Sources
Now we already in directory ~/Project/Sources.
To get the latest Linux source code you can download from this is link https://www.kernel.org/pub/linux/kernel/v3.x/linux-3.9.tar.xz. Extract the file:
$ tar -xJvf linux-3.9.tar.xz
Go to Linux soure code directory and compile the code:
$ cd linux-3.9
$ make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- vexpress_defconfig
$ make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- all
It will take times depend on how fast your computer. The result of compilation is zImage. You can copy that file to our Project directory.
$ cp arch/arm/boot/zImage ~/Project
$ cd ~/Project
Then let’s try to launch our code on Qemu:
$ qemu-system-arm -M vexpress-a9 -kernel zImage -initrd initramfs -serial stdio -append "console=tty1"
If everything is smooth, you will see text “Hello World” will be shown on Qemu console.

Busybox run on Qemu

Preparation
  1. Install developer ncurses library. It will be used during linux kernel configuration.
  2. Install latest Qemu from Linaro repository. You can refer to my previous posting, Install Qemu on Ubuntu.
  3. Download arm cross compiler from CodeSourcery. Make sure you already become member of mentor.com website. Currently latest version of compiler is 2012.09-64. But I already tried that version and when always be fail. I also tried previous version, it is 2012.03-57, and the result is same. And I have been successfully compile linux kernel and install busybox using version 2011.09-70.
  4. Download latest linux source code from kernel.org. Currently I am using linux version 3.9. File name should be linux-3.9.tar.xz.
  5. Download latest busybox from busybox.net. Currently, latest version of busybox is busybox-1.21.0.
Install Developer ncurses Library
$ sudo apt-get install libncurses5-dev
Files and Folders Arrangement
  1. Create folder for our project.
    # cd
    # mkdir Project
    # cd Project
  2. Create folder for our source code that we already downloaded previously.
    # mkdir Sources
    # cd Sources
  3. Copy all files (encrypted files) to Sources folder. Make sure we already collected these files,
    linux-3.9.tar.xz, busybox-1.21.0.tar.bz2.tar.bz2, and arm-2011.09-70-arm-none-linux-gnueabi-i686-pc-linux-gnu.tar.bz2.tar.bz2.
  4. Extract all those files.
    # tar -xJvf linux-3.9.tar.xz
    # tar -xjvf busybox-1.21.0.tar.bz2.tar.bz2
    # tar -xjvf arm-2011.09-70-arm-none-linux-gnueabi-i686-pc-linux-gnu.tar.bz2.tar.bz2
    Now we already have 3 new folders: busybox-1.21.0, linux-3.9, and arm-2011.09.
Make Path for The Compiler
  1. Open ~/.bashrc file and add text in order to inform OS that compiler will be added to path.
    $ nano ~/.bashrc
    After open .bashrc file, add this text,
    export PATH=/home/your_user/Project/arm-2011.09/bin:$PATH
    Exit from editor, then refresh .bashrc file,
    $ source ~/.
  2. Test whether your compiler already successfully added to path or not.
    $ arm-none-linux-gnueabi-gcc --version
Compile Linux Kernel
  1. Go to linux source code folder.
    $ cd ~/Project/Sources/linux-3.9
  2. Preparation before compile linux source code.
    $ make mrproper
    $ make ARCH=arm CROSS_COMPILE=arm-none-linux-gnueabi- versatile_defconfig
    $ make ARCH=arm CROSS_COMPILE=arm-none-linux-gnueabi- menuconfig
    In linux kernel configuration window, go to “Enable loadable module support”. Press space or N key button, to un-check this feature. Next is go to Kernel Features, then activate ARM EABI compilation feature.
  3. Compile linux kernel.
    $ make ARCH=arm CROSS_COMPILE=arm-none-linux-gnueabi- all
    In this process, it will take a few minutes, depend on your computer speed.
  4. After compilation finished, we will get linux kernel image (zImage) from folder ./arch/arm/boot. Copy the image into your project folder.
    $ cp ./arch/arm/boot/zImage ~/Project
    Now, our linux kernel image is ready to be used.
Install BusyBox
  1. Go to busybox source code folder.
    $ cd ~/Project/Sources/busybox-1.21.0
  2. Preparation before install BusyBox.
    $ make ARCH=arm CROSS_COMPILE=arm-none-linux-gnueabi- defconfig
    $ make ARCH=arm CROSS_COMPILE=arm-none-linux-gnueabi- menuconfig
    In Busybox Configuration window, go to Busybox Setting -> Build Options -> check Build BusyBox as a static binary. Then go to Busybox Setting -> Installation Options -> Installation Prefix. Remember that folder name, because that folder will be destination of our installation files.
  3. Install BusyBox.
    $ make ARCH=arm CROSS_COMPILE=arm-none-linux-gnueabi- install
  4. Go to _install folder and add some new folders.
    $ cd _install
    $ mkdir proc sys dev etc etc/init.d
  5. Add rcS file in folder init.d, and add code to mount proc and sysfs when launch kernel on Qemu.
    $ touch etc/init.d/rcS
    $ nano etc/init.d/rcS
    Add this code,
    #!/bin/sh
    mount -t proc none /proc
    mount -t sysfs none /sys
    /sbin/mdev -s
    Change permission of rcS file,
    $ chmod +x etc/init.d/rcS
  6. Convert _install folder to rootfs.img, and copy to Project folder.
    $ find . | cpio -o --format=newc > ..rootfs.img
    $ cd ..
    $ gzip -c rootfs.img > rootfs.img.gz
    $ cp rootfs.img.gz ~/Project
Launch Qemu
$ cd ~/Project
$ qemu-system-arm -M versatilepb -m 128M -kernel zImage -initrd rootfs.img.gz -append "root=/dev/ram rdinit=/bin/sh"
After qemu launched, suppose it will show # sign. And you can type some commands same as linux commands.