Linux executable without main() Function | Write a C Executable program without main() function

Very Short version :

1) Create the program without main() function.

			
rajkumar.r@17:56:58:~/workspace/raj/workouts/ex_without_main$ cat exe_wo_main.c 
#include 
#include 
const char my_interp[] __attribute__((section(".interp"))) = "/lib/ld-linux.so.2";
int fn_wo_main() {
	printf("This is a function without main\n");
	exit(0);
}
rajkumar.r@17:57:02:~/workspace/raj/workouts/ex_without_main$

2) Compile it as a Shared object

gcc -shared -Wl,-e,fn_wo_main exe_wo_main.c -o exe_wo_main.so

3) Run it!!

			
rajkumar.r@17:57:36:~/workspace/raj/workouts/ex_without_main$ ./exe_wo_main.so 
This is a function without main
rajkumar.r@17:58:04:~/workspace/raj/workouts/ex_without_main$

Longer Version with Explanation :

Generally, all of the C programs starts at main() function. This is a standard defined fro C programming. When we look at the internals of this, we can understand how the system actually works in the background. C is a compiled language, which means, the source code is converted into the executable, before the execution starts. The general C program compilation process goes through the following steps, which is well documented in several other websites.

C Source code
Preprocessor – Intermediate files
Compiler – Object files
Linker – Linked Object file [ Executable file ]
Loader – Loads the executable and executes.

For our goal, we have to understand the linking process. We write our code starting from main(). But before the main(), there are several things happens in the background. Like, setting up of the environment, fetching input, configuring console etc. These are beautifully abstracted by GCC and the host system which hides those process from the user.

Here, we will unravel a small step of the long and complex process. The entry point of the execution. When the source code is preprocessed and compiled, the object codes are generated. After this, the standard and user built object codes are linked to generate the executable. The linking is done by the linker script which directs how to create the binary executable. This is where, the entry point of the executable is defined and configured as main().

Before main(), there happens few configurations. One among them is, configuration of dynamic loader, which loads the required symbols at run time. Usually in Linux, it is /lib/ld-linux.so.2, which inturn links to the corresponding loader provided by the compiler collection. In my case, it seems to be as below.

rajkumar.r@17:59:50:~/workspace/raj/workouts/ex_without_main$ ls -l /lib/ld-linux.so.2 
lrwxrwxrwx 1 root root 25 Jan 28  2013 /lib/ld-linux.so.2 -> i386-linux-gnu/ld-2.15.so

GCC Compiler is a very powerful and sophisticated compiler. GCC also offers a lot of control to the developers. One among several wonderful option is, -Wl,option. According to gcc man page,

-Wl,option
	   Pass option as an option to the linker.  If option contains commas, it is split
	   into multiple options at the commas.  You can use this syntax to pass an argument
	   to the option.  For example, -Wl,-Map,output.map passes -Map output.map to the
	   linker.  When using the GNU linker, you can also get the same effect with 
	   -Wl,-Map=output.map

From ld manual @t http://ftp.gnu.org/pub/old-gnu/Manuals/ld-2.9.1/html_node/ld_24.html,

The linker command language includes a command specifically for defining the first executable instruction in an output file (its entry point). Its argument is a symbol name:

ENTRY(symbol)

Like symbol assignments, the ENTRY command may be placed either as an independent command in the command file, or among the section definitions within the SECTIONS command--whatever makes the most sense for your layout.

ENTRY is only one of several ways of choosing the entry point. You may indicate it in any of the following ways (shown in descending order of priority: methods higher in the list override methods lower down).

	the `-e' entry command-line option;
	the ENTRY(symbol) command in a linker control script;
	the value of the symbol start, if present;
	the address of the first byte of the .text section, if present;
	The address 0. 

For example, you can use these rules to generate an entry point with an assignment statement: if no symbol start is defined within your input files, you can simply define it, assigning it an appropriate value---

start = 0x2020;

The example shows an absolute address, but you can use any expression. For example, if your input object files use some other symbol-name convention for the entry point, you can just assign the value of whatever symbol contains the start address to start:

start = other_symbol ;

We are going to combine all of these above features to run a program without main function. The steps are..

1) Create the program without main() function.

rajkumar.r@17:56:58:~/workspace/raj/workouts/ex_without_main$ cat exe_wo_main.c 
#include 
#include 
const char my_interp[] __attribute__((section(".interp"))) = "/lib/ld-linux.so.2";
int fn_wo_main() {
	printf("This is a function without main\n");
	exit(0);
}
rajkumar.r@17:57:02:~/workspace/raj/workouts/ex_without_main$

Here, you can see we have a strange thing at line number 2. This is to compensate the missing main() and adding the dynamic loader section. ūüôā

2) Compile it as a Shared object

gcc -shared -Wl,-e,fn_wo_main exe_wo_main.c -o exe_wo_main.so

3) Run it!!

rajkumar.r@17:57:36:~/workspace/raj/workouts/ex_without_main$ ./exe_wo_main.so 
This is a function without main
rajkumar.r@17:57:36:~/workspace/raj/workouts/ex_without_main$

Done!!!

Advertisements

What is Unified modelling language [UML] and diagrams?

Very very short story

  1. Business process modeling with use cases
  2. Class and object modeling
  3. Behavior modeling
  4. Component modeling
  5. Distribution and deployment modeling
  • User model view,
  • ¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬† ‚ÄĘ Use case Diagram:
  • Structural model view,
  • ¬†¬† ¬†¬†¬† ¬†¬†¬† ¬†¬†¬† ‚Äʬ†¬† ¬†Class Diagrams:
  • ¬†¬† ¬†¬†¬† ¬†¬†¬† ¬†¬†¬† ‚Äʬ†¬† ¬†Object Diagrams:
  • Behavioural¬†model view,
  • ¬†¬† ¬†¬†¬† ¬†¬†¬† ¬†¬†¬† ‚Äʬ†¬† ¬†Sequence diagrams:
  • ¬†¬† ¬†¬†¬† ¬†¬†¬† ¬†¬†¬† ‚Äʬ†¬† ¬†Collaboration diagrams:
  • ¬†¬† ¬†¬†¬† ¬†¬†¬† ¬†¬†¬† ‚Äʬ†¬† ¬†State diagrams:
  • ¬†¬† ¬†¬†¬† ¬†¬†¬† ¬†¬†¬† ‚Äʬ†¬† ¬†Activity diagrams:
  • Implementation¬†model view
  • ¬†¬† ¬†¬†¬† ¬†¬†¬† ¬†¬†¬† ‚Äʬ†¬† ¬†Component diagrams:
  • Environment model view.
  • ¬†¬† ¬†¬†¬† ¬†¬†¬† ¬†¬†¬† ‚Äʬ†¬† ¬†Deployment diagrams:

Now, Long story..

Summary :
——————————————————————————-

This post is from here.
If you’re an analyst, developer or architect, the chances are that you have heard of the UML
If you’re not already familiar of using OOA/D design methods such as the UML then there is a fair chance the pressure is on to utilize this standard as part of your analysis and  design process.

To those not familiar with OOAD, either the laymen or seasoned developer alike, the UML can be seen overly comprehensive and daunting to learn.

This article will attempt to provide a fast track introduction for those that need to learn the UML basics and to begin to start understanding UML, so that it can be incorporated into your development project.

  1. I shall begin by clarifying exactly what UML is and is not.
  2. Then question why we should use the UML at all.
  3. Then I will conclude part 1 of this article with a high-level tour of the UML modeling tool-set.
  4. In part 2 of this article I shall continue by applying the models and notation already discussed to a real life business problem with a working example.

What is the UML?
——————————————————————————-
In 1997 the OMG (Object Management Group) developed the UML as a common
architectural framework for modeling object orientated systems & applications.
The UML is derived primarily from the strengths of three notations;

  • Booch OOD (Object-Oriented Design),
  • Rumbaugh OMT (Object Modeling Technique),
  • ¬†Jacobson OOSE (Object-Oriented Software Engineering).

The OMG described UML is a language representing unified best engineering
practices for

  • specifying,
  • visualizing,
  • constructing,and
  • documenting

the elements of business modeling, software and even non-software systems.

‚Äʬ†¬† ¬†Specification:
“what” is required of a system, and
“how” a system may be implemented.
It captures the all important
requirements,
analysis,
design, and
implementation decisions.
that need to be established during a system development lifecycle.

‚Äʬ†¬† ¬†Visualization:
allows the visualization of systems before they are implemented.
shapes with well defined semantics
communicate to a wider audience more succinctly
than a descriptive narrative and
more comprehensively
than what often can be represented by a programming language.

‚Äʬ†¬† ¬†Construction:
used to guide and craft the implementation of a complicated system.
its possible to generate OO source code from UML and vice versa.

‚Äʬ†¬† ¬†Documenting:
It can capture knowledge and documenting deliverables, such as
requirements documents,
functional specifications,
and test plans.
These are all critical in
controlling,
measuring, and
communicating
a system throughout its life cycle.

These four are modeling applications of UML. Not be confused with a process.
There are many processes available which use the UML;
furthermore there are many tools available on the market that aid the UML and,
in some cases also facilitates the following of a particular process.

Therefore the UML is not:
——————————————————————————-
‚ÄĘ A Process:
It is a modeling toolkit with its own notation and syntax.
A process goes further by
describing the steps you take when developing software,
which diagrams are produced and
in which order, who does what and so on.
The premise behind the UML is that it is process-independent,
but enables and facilitates further processes.
‚Äʬ†¬† ¬†Visual Programming Language:
It is a visual modeling language frm which programs can be derived
The notation behind UML modeling is comprised of,
a set of specialized shapes        :    used for the construction
of different kinds of diagrams
while the UML syntax specifies    :    how these shapes can be combined.

Therefore further to learning the basics of UML it is recommend that:
‚ÄĘ A process or methodology is adopted
‚ÄĘ A UML development tool is utilized

UML may be used to support a number of methodologies, such as the
Rational Unified Process.
Some methodologies are more suited to larger enterprise applications
with a large team of architects and developers.
While others are more appropriate for
a single person or small teams working on small embedded systems.

Similarly there are many UML development tools available, such as
Rational Rose (Rational Rose Corporation),
Enterprise Architect (Sparx Systems),
Describe (Embarcadero Technologies) and even
Microsoft Visio.

Why Use UML?
——————————————————————————-
With many of the rapid application development (RAD) tools available
such as Delphi or Visual Basic,
developing an application is fairly easy.

But does this method result in a professional quality application?

Deborh Kurata (1998) states that if an application is to be of a professional
quality, it must:
‚ÄĘ meet the needs of the users
‚ÄĘ be robust
‚ÄĘ be maintainable
‚ÄĘ be documented

Many developers using RAD tools will believe it makes sense to develop an
application rapidly.Write a prototype, and then keep adding more code until
the application is complete.

There is however, a fundamental problem to this approach.
The resulting application will lack a well defined architecture
because it would not have been thought out properly.
This will not compromise fundamental object orientated principles and
result in
undocumented,
inefficient and
difficult to maintain code.

With
the use of UML,
an appropriate UML development tool, and
an applicable process or methodology,

the design and refining of the application is shifted
from the development phase to
an analysis and design phase.

Therefore reducing risks and providing a vehicle for testing the architecture
of a system before it is coding begins.

The analysis and design overhead will eventually pay dividends as the system
has been
user driven,
documented and
generate skeleton code,

that will be
efficient,
object orientated and
promote re-use.

Sinan Si Alhir (1998) describes the UML as enabling:
‚Äú…
the capturing,
communicating,
and leveraging

of
strategic,
tactical,
and operational knowledge

to facilitate increasing value by

increasing quality,
reducing costs,
and reducing time-to-market

while
managing risks &
being proactive in regard to
ever-increasing change &
Complexity.

This is a fairly convincing statement in itself, Sinan states that the UML will
increase quality and
reduce development time
while being flexible enough to respond to changing requirements.

Furthermore, the use of UML will help;
‚ÄĘ The communication of the desired structure & behavior of a system between
analysts,
architects,
developers,
stakeholders and
users.
‚ÄĘ The visualization and control of a systems architecture
‚ÄĘ Promote a deeper understanding of
the system,
exposing opportunities for
simplification
and reuse.
‚ÄĘ Manage Risk.

So what are these models?
——————————————————————————-
So what models are available,
what use are they and
how do they link together?

we need to consider the primary modeling purposes of UML.

These are:

  1. Business process modeling with use cases
  2. Class and object modeling
  3. Behavior modeling
  4. Component modeling
  5. Distribution and deployment modeling

Each model is designed to let
analysts,
developers and
customers
view a system from different perspectives and
with varying levels of abstraction.

Each diagram will fit somewhere into these five architectural views
representing a distinct problem solution space.

These can be described as the;

  1. user  model view,
  2. structural model view,
  3. behavioral model view,
  4. implementation model view and
  5. the environment model view.

The User Model View
——————————————————————————-
The user model view encompasses the models which define a solution to a problem
as understood by the client or stakeholders.This view is often also referred to
as the use case or
scenario view.

‚Äʬ†¬† ¬†Use case Diagram:
These models depict
the functionality required by the system and
the interaction of users and other elements (known as actors)
with respect to the specific solution.

The Structural model View :
——————————————————————————-
The structural view encompasses the models which provide the
static,
structural dimensions and
properties of the modeled system.
This view is often also referred to as the static or logical view.

‚Äʬ†¬† ¬†Class Diagrams:
These models describe the static structure & contents of a system using
elements such as classes,
packages and
objects
to display relationships such as
containment,
inheritance and
associations.
‚Äʬ†¬† ¬†Object Diagrams:
Depict a class or the static structure of a system
at a particular point in time

The Behavioral Model View:
——————————————————————————-
This model describe the behavioral, dynamic features & methods of the modeled
system. This view is often also referred to as the
dynamic,
process,
concurrent, or
collaborative view.
‚Äʬ†¬† ¬†Sequence diagrams:
Describe timing sequence of the objects over a vertical time dimension.
With interactions between objects depicted on a horizontal dimension.
‚Äʬ†¬† ¬†Collaboration diagrams:
Describe the interactions and relationships between
objects and
sequences
of a system organized in time and space.
Numbers are used to show the sequence of messages.
‚Äʬ†¬† ¬†State diagrams:
Describe the sequence, status conditions and appropriate responses or
actions to conditions during the life of the objects within the system.
‚Äʬ†¬† ¬†Activity diagrams:
Describe the methods, activities and resulting transitions after
completion of the elements as flows of processing within a system.

The Implementation Model View:
——————————————————————————-
The implementation view combines the structural and behavioral dimensions
of the solutions realization or implementation.
This view is often also referred to as the component or development view.

‚Äʬ†¬† ¬†Component diagrams:
These depict the high level organization & dependencies of
source code components,
binary components and
executable components
and whether these components exist at compile, link or run time.

The Environment Model View :
——————————————————————————-
These models describe both the structural and behavioral dimensions of the
domain or environment in which the solution in implemented.
This view is often also referred to as the deployment or physical view.

‚Äʬ†¬† ¬†Deployment diagrams:
This models depict & describe
environmental elements &
configuration of runtime processing components,
libraries and
objects that will reside on them.

How do the models fit together?
——————————————————————————-
After a high-level tour of the architectural views and diagrams available,it is
important to remember once again that UML is a not a process, therefore there
is no right or wrong order in which these models should be constructed.

In practice, the only real pre-requisite to a model is a business process model,
use case or a use case diagram. From then on in, a method of refinement on each
model will often be used as many elements of the system will not become obvious
until it is modeled from a different perspective. Therefore the activities of
analysis (what are the objects?) and design (the allocation of behavior) will
be iterative and be a mutually complementary process.

Conclusion
——————————————————————————-
We have taken a look at the origins and definition of the UML to provide a
simplistic understanding of what it is, and what the UML can offer us. We have
also examined how we can benefit from its use on our next development project
and briefly explored the architectural views and models available and how these
can link together. In the concluding part of this article I shall apply the
principles and models discussed and explored in this article to a real life
business problem and development solution using example UML models where
applicable.

References :
——————————————————————————-
Alhir, Sinan Si. The True Value of the Unified Modeling Language (UML)”. Distributed Computing Magazine. DC Corp. July 1998.
Alhir, Sinan Si. UML in a Nutshell. O’Reilly and Associates, Inc. 1998
Alhir, Sinan Si. Understanding the Unified Modelling Language (UML). Methods & Tools. Martinig & Associates. April 1998.
Kurata, Deborah. Develop a Professional Application. Visual Basic Programmer’s Journal . pp 83-86. March 1998.

Further Reading
——————————————————————————-
UML Tutorial, Sparx Systems:
http://www.sparxsystems.com.au/UML_Tutorial.htm
What is UML, Embarcadero Technologies:
http://www.embarcadero.com/support/what_is_uml.asp
Introduction to OMG’s Unified Modeling Language‚ĄĘ. OMG:
http://www.omg.org/gettingstarted/what_is_uml.htm
Understanding the Unified Modeling Language. Sinan Si Alhir:
http://home.earthlink.ne

Building and Booting Linux using Qemu

Previously, we have Built and Booted U-Boot through Qemu.
Now, let us build and boot Linux using Qemu.
Get the latest kernel source from https://www.kernel.org/
I took Stable 3.9.3 as on writing.

mkdir original
mkdir src
cd original
wget -c https://www.kernel.org/pub/linux/kernel/v3.x/linux-3.9.3.tar.xz
cd ../src
tar -xf ../original/linux-3.9.3.tar.xz

Let us define the enviroinment variables that kernel build uses..

export ARCH=arm
export CROSS_COMPILE=arm-none-eabi-

Now, Let us configure this kernel build for Versatile Express.
This config is available at

./arch/arm/configs/vexpress_defconfig

For list of available configs, you can further explore in arch/ directory

make vexpress_defconfig;

Now, We need to make few changes, to make this kernel usable for our
needs in latter times.For this, We can remov module support (for
simplicity) and enabled EABI support as a binary format (allowing also old ABI).
This is necessary to run software compiled with the CodeSourcery toolchain.
Enable

kernel Features ---> Use the ARM EABI to compile the kernel and  
Kernel Features ---> Allow old ABI binaries to run with this kernel 

make menuconfig

We are all set to build the kernel. Now run

make -j4 all

Here, -j4 informs the build to use 4 build Jobs == Number of cores in your machine.
It will take some time to build.

Meanwhile, let us see what is the root file system and why do we need one, and
how to boot the kernel with a simple program.

Here is a definition of Root File System from Linux Information project

	
The root filesystem is the filesystem that is contained on the same 
partition on which the root directory is located, and it is the 
filesystem on which all the other filesystems are mounted (i.e.,
logically attached to the system) as the system is booted up 
(i.e., started up). 

The exact contents of the root filesystem will vary according to the 
computer, but they will include the files that are necessary for 
booting the system and for bringing it up to such a state that
the other filesystems can be mounted as well as tools for fixing a 
broken system and for recovering lost files from backups. The 
contents will include the root directory together with a minimal set 
of subdirectories and files including /boot, /dev, /etc, /bin, /sbin 
and sometimes /tmp (for temporary files).

Hope the kernel would have been built and ready by now.
The build should have completed with a message like,

  OBJCOPY arch/arm/boot/zImage
  Kernel: arch/arm/boot/zImage is ready
rajkumar.r@14:56:56:~/kernel/3_linux_qemu/src/linux-3.9.3$

The kernel will be available at

arch/arm/boot/zImage

Now, we can try to boot the kernel using qemu as below.

qemu-system-arm -M vexpress-a9 -kernel arch/arm/boot/zImage -append\
 "console=tty1"

-M vexpress-a9 : Emulate V Express Board
-kernel arch/arm/boot/zImage : Use this file as kernel
-append "console=tty1" console acts as the tty1
- generall Linux uses tty interface to display console messages

Here, you can read what is tty

But, now, the kernel will end up in panic telling something like,

VFS: Cannot open root device "(null)" or unknown block(0,0): error -6
Please append a correct "root=" boot option;
Kernel panic - not syncing: VFS: Unable to mount root fs on unknown 
block(0,0)

Now, What we discussed about File system comes useful.
As the kernel message is telling, we are missing a root file system. To build
a complete set of root file system is a complex task [ Relatively ūüėõ ] So, we
are going to generate a simple file system.

Creating a Simple Filesystem :

create file test.c in the src directory with the below content.

#include 

void main() {
	while(1) 
		printf("Hello World!\n");  
}

As the program looks, it will run continouesly as kernel expects the first
program to run forever. Compile this program, using cross compiler for Arm
running Linux
[ This is not same as bare metal toolchain. Bare metal toolchain is for ARM
which has no OS. i.e like arm-none-eabi-, which we have exported while
building kernel.]

arm-none-linux-gnueabi-gcc -static test.c -o test

This will Compile and creates an ELF, staticaly liked to all required code,
in a single binary. We need a Filesystem, but we have a binary file now.
So we need to generate Filesystem using some tool. Before that, we should know,
What is initramfs?

https://wiki.ubuntu.com/Initramfs

initramfs, as the name tells, its the Initial Ram File System. This is
introduced for Linux 2.6 kernel, before which initrd is being used.

From ubuntu Wiki

The key parts of initramfs are:

1) CPIO archive, so no filesystems at all are needed in kernel. 
   The archive is simply unpacked into a ram disk.
2) This unpacking happens before do_basic_setup is called. This means 
   that firmware files are available before in-kernel drivers load.
3) The userspace init is called instead of prepare_namespace. All 
   finding of the root device, and md setup happens in userspace.
4) An initramfs can be built into the kernel directly by adding it to 
   the  ELF archive under the section name .init.ramfs initramfs' can be
   stacked. Providing an initramfs to the kernel using the traditional
   initrd mechanisms causes it to be unpacked along side the initramfs'
   that are built into the kernel.
5) All magic naming of the root device goes away. Integrating udev into 
   the initramfs means that the exact same view of the /dev tree can be 
   used throughout the boot sequence. This should solve the majority of 
   the SATA failures that are seen where an install can succeed, but the
   initrd cannot boot.

This initramfs uses a format called newc. Now, to get the cpio archive,
initramfs from the binary, run the below command.

echo test | cpio -o --format=newc > rootfs

Now, we have the zImage kernel and rootfs – Initramfs. Let us load the kernel

qemu-system-arm -M vexpress-a9 -kernel linux-3.9.3/arch/arm/boot/zImage\
-initrd rootfs -append "root=/dev/ram rdinit=/test"

Here, 

-initrd rootfs : Qemu option which tells, rootfs is the Filesystem 
binary image.

root=/dev/ram and 
rdinit=/test are kernel options passed to the kernel we load.

rdinit=/test tells the kernel to run "test" executable we built as init.

Now, we can see the “hello world” being printed.

Voila!! Done!!

7. TFTP Setup in ubuntu

Goal : To setup TFTP Server in ubuntu and test the same.

Installing TFTP Server in Ubuntu

$ sudo apt-get install tftp tftpd-hpa

Once the setup is done, you can view/edit configuration at

/etc/default/tftpd-hpa

It should look something like,

$ cat /etc/default/tftpd-hpa
TFTP_USERNAME="tftp"
TFTP_DIRECTORY="/var/lib/tftpboot"
TFTP_ADDRESS="0.0.0.0:69"
TFTP_OPTIONS="--secure"

Starting and stoping the service: We can start and stop tftp service through the following commands.

service tftpd-hpa status
service tftpd-hpa stop
service tftpd-hpa start
service tftpd-hpa restart
service tftpd-hpa force-reload

Testing TFTP For testing, we are going to try to download a file from tftp server. For this, we need to copy some file to the location given at TFTP_DIRECTORY variable in /etc/default/tftpd-hpa I took a uImage file to the location.

$ cp uImage /var/lib/tftpboot/

Now, start the tftp client as below,

raj@raj-VirtualBox:~$ tftp localhost
tftp>

To check the status, run status command

tftp> status
Connected to localhost.
Mode: netascii Verbose: off Tracing: off
Rexmt-interval: 5 seconds, Max-timeout: 25 seconds
tftp>

Now, try to get the file you have copied to the TFTP_DIRECTORY

tftp> get uImage
Received 2169792 bytes in 0.3 seconds
tftp>

Voila!! You are done.. TFTP is configured and working.

Reference

6. Connecting Qemu to network

Previously, we have Built and booted U-Boot in Qemu as in this post.  Now, We are going to connect the Qemu to the virtual network for further usage. This is based on this post. We are going to connect Qemu to Network bridge and TUN/TAP For this, we need the following installed in out ubuntu machine

$ sudo apt-get install bridge-utils uml-utilities openvpn firestarter

Insert the TUN device module to the kernel

$ sudo modprobe tun

The node ?tun? must have been automatically created in /dev/net folder. If not try to create it manually by typing the following commands

$ sudo mkdir /dev/net
$ sudo mknod /dev/net/tun c 10 200
$ sudo chmod 660 /dev/net/tun

Our machine has the ethernet or Wireless Lan connection. In case of Ethernet connection, it should be named like eth0. We need to tap network from this to our virtual network, tap0. We can do it using following scripts.

Contents of /etc/qemu-ifup

#!/bin/sh

#
# script to bring up the tun device in QEMU in bridged mode
# first parameter is name of tap device (e.g. tap0)
#
# some constants specific to the local host ? change to suit your host
#

ETH0IP=10.0.2.15
GATEWAY=10.0.2.2
BROADCAST=10.0.2.255

#
# First take eth0 down, then bring it up with IP 0.0.0.0
#
ifconfig eth0 down
ifconfig eth0 0.0.0.0 promisc up

#
# Bring up the tap device (name specified as first argument, by QEMU)
#
openvpn --mktun --dev $1 -user 'id -un'
ifconfig $1 0.0.0.0 promisc up

#
# create the bridge between eth0 and the tap device
#
brctl addbr br0
brctl addif br0 eth0
brctl addif br0 $1

#
# only a single bridge so loops are not possible, turn off spanning
# tree protocol
#
brctl stp br0 off

#
# Bring up the bridge with ETH0IP and add the default route
#
ifconfig br0 $ETH0IP netmask 255.255.0.0 broadcast $BROADCAST
route add default gw $GATEWAY

#
# stop firewall ? comment this out if you don?t use Firestarter
#
service firestarter stop

Here, you may need to edit the following according to your network configurations.

ETH0IP=10.0.2.15 		
GATEWAY=10.0.2.2		
BROADCAST=10.0.2.255

Save the file as executables. Current ifconfig shoud return something like this.

$ ifconfig
eth0	  Link encap:Ethernet  HWaddr 08:00:27:d6:16:37  
          inet addr:10.0.2.15  Bcast:10.0.2.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fed6:1637/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:78 errors:0 dropped:0 overruns:0 frame:0
          TX packets:156 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:12215 (12.2 KB)  TX bytes:22482 (22.4 KB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:42 errors:0 dropped:0 overruns:0 frame:0
          TX packets:42 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:3354 (3.3 KB)  TX bytes:3354 (3.3 KB)

Now, we can run the script to connect bridge the network.

raj@raj-VirtualBox:~$ sudo /etc/qemu-ifup tap0
[sudo] password for raj: 
Tue May 21 10:29:29 2013 TUN/TAP device tap0 opened
Tue May 21 10:29:29 2013 Persist state set to: ON
raj@raj-VirtualBox:~$

If you check the ifconfig now, it should look like below with br0 and tap0 added..

raj@raj-VirtualBox:~$ ifconfig 
br0       Link encap:Ethernet  HWaddr 08:00:27:d6:16:37  
          inet addr:10.0.2.15  Bcast:10.0.2.255  Mask:255.255.0.0
          inet6 addr: fe80::a00:27ff:fed6:1637/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:4 errors:0 dropped:0 overruns:0 frame:0
          TX packets:33 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:328 (328.0 B)  TX bytes:7126 (7.1 KB)

eth0      Link encap:Ethernet  HWaddr 08:00:27:d6:16:37  
          inet6 addr: fe80::a00:27ff:fed6:1637/64 Scope:Link
          UP BROADCAST RUNNING PROMISC MULTICAST  MTU:1500  Metric:1
          RX packets:118 errors:0 dropped:0 overruns:0 frame:0
          TX packets:267 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:17802 (17.8 KB)  TX bytes:40277 (40.2 KB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:150 errors:0 dropped:0 overruns:0 frame:0
          TX packets:150 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:10986 (10.9 KB)  TX bytes:10986 (10.9 KB)

tap0      Link encap:Ethernet  HWaddr 9e:43:58:5d:bb:3f  
          UP BROADCAST PROMISC MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:100 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

raj@raj-VirtualBox:~$

Now, let us test the setup with qemu. For this, we need the u-boot.bin that we generated using this.

sudo qemu-system-arm -M versatilepb -nographic \
-net nic -net tap,ifname=tap0 -kernel u-boot.bin

This should give Uboot prompt as below.

U-Boot 2010.03-dirty (May 02 2013 - 10:00:51)

DRAM:   0 kB
## Unknown FLASH on Bank 1 - Size = 0x00000000 = 0 MB
Flash:  0 kB
*** Warning - bad CRC, using default environment

In:    serial
Out:   serial
Err:   serial
Net:   SMC91111-0
VersatilePB #

Now, we have to set an IP address for the qemu system. This can be done by running this command in uboot prompt

VersatilePB # setenv ipaddr 10.0.2.3

Note down your machines IP and now, We can test the connection by doing a ping to our machine.

VersatilePB # ping 10.0.2.15
SMC91111: PHY auto-negotiate timed out
SMC91111: MAC 52:54:00:12:34:56
Using SMC91111-0 device
host 10.0.2.15 is alive
VersatilePB #

Voila!! your system is connected if its telling the host is alive..

Booting Uboot in QEMU

U-Boot is universal bootloader, which is used very widely. Its a starting point to learn the low level hardware interactions for me. Qemu supports Versatile PB and we can try to emulate uboor on versatile PB using QEMU.

Creating workbench:

$ mkdir uboot
$ cd uboot;
$ mkdir original
$ mkdir src
$ cd original

Download U-Boot source code from U-Boot FTP.

$ wget -c ftp://ftp.denx.de/pub/u-boot/u-boot-latest.tar.bz2
$ cd ../src/
$ tar -xf ../original/u-boot-latest.tar.bz2
$ cd u-boot-2010.03

Compile the u-Boot code.

$ export ARCH=arm;
$ export CROSS_COMPILE=arm-none-eabi-;

$ make versatilepb_config 
$ make all

Now, if build went right, you should have u-boot.bin in the folder, and to boot this, run,

$ qemu-system-arm -M versatilepb -m 128M -nographic -kernel u-boot.bin;

Now you will be in uBoot prompt!!

U-Boot 2010.03-dirty (May 17 2013 - 15:52:24)

DRAM:   0 kB
## Unknown FLASH on Bank 1 - Size = 0x00000000 = 0 MB
Flash:  0 kB
*** Warning - bad CRC, using default environment

In:    serial
Out:   serial
Err:   serial
Net:   SMC91111-0
VersatilePB #

Try command like ‘?’, ‘help’ etc..

Bare Metal Programming for ARM VersatilePB II : Hello world

Reference : http://balau82.wordpress.com/2010/02/28/hello-world-for-bare-metal-arm-using-qemu/

As we are working on a bare silicon, we dont have any windowing system to display the prints. But in the silicon, we can verify some signal flow using some LED highs and lows.

These LED’s gets signal through certain peripherals and we decided to use UART as the peripheral to experiment as its available in UBOOT. Communicating to a peripheral is through memory mapped I/O. i.e, every peripheral is given a address and that is used to communicate as its a location.

Few more details for Memory mapped IO for this experiment..

1) QEMU supports versatilePB board. UART0 of verstailePB is mapped to terminal when using -nographic or -serial stdio option in qemu

2) The memory map of the VersatilePB board is implemented in QEMU in this board-specific C source http://git.savannah.gnu.org/cgit/qemu.git/tree/hw/versatilepb.c and we got address of UART0 as 0x101f1000

Okay.. Lets start to write the code..

Contents of test.c
volatile unsigned int * const UART0DR = (unsigned int *)0x101f1000;

void print_uart0(const char *s) {
 while(*s != '') { /* Loop until end of string */
 *UART0DR = (unsigned int)(*s); /* Transmit char */
 s++; /* Next char */
 }
}

void c_entry() {
 print_uart0("Hello world!\n");
 }

As the UART is a dynamic device, we declare it as volatile and we assign the mapped address as we found earlier.

Hope the code is self explanatory.

Contents of test.ld

ENTRY(_Reset)
SECTIONS
{
 . = 0x10000;
 .startup . : { startup.o(.text) }
 .text : { *(.text) }
 .data : { *(.data) }
 .bss : { *(.bss COMMON) }
 . = ALIGN(8);
 . = . + 0x1000; /* 4kB of stack memory */
 stack_top = .;
}

We start at adress 0x100000 as qemu loads the binary from this address.

Contents of starup.s
.global _Reset
_Reset:
 LDR sp, =stack_top
 BL c_entry
 B .

Next, we can compile this code..

$ arm-none-eabi-as -mcpu=arm926ej-s -g startup.s -o startup.o
$ arm-none-eabi-gcc -c -mcpu=arm926ej-s -g test.c -o test.o
$ arm-none-eabi-ld -T test.ld test.o startup.o -o test.elf
$ arm-none-eabi-objcopy -O binary test.elf test.bin

Here, you can notice an addition to last build, that is, creation of binary image..

Its because, U-Boot loads raw binary files..

And now, we can run this using the command,

$ qemu-system-arm -M versatilepb -m 128M -nographic -kernel test.bin

-M versatilepb : specifies qemu to emulate Versatile PB.
-m 128M tells qemu to use 128MB Ram for this system
-nographic : Dont use graphic and provide terminal output
-kernel test.bin : Load this binary as the system image.

You can see Hello World Printed in the screen., Now, to close this, Hit,

'Ctl + a' and then 'x'