Wednesday, December 19, 2012

Video for Linux

1st place to understand basic structure of V4L2 interface is here. vivi driver is a sample source which could be used for either testing V4L2 based application or to start developing new driver. More information about vivi could be found here. Driver writer's guide is available here. Once all above links are covered, linux media interface could be looked here to go inside.

Although link is not for current scope, but it is good to have this link as a quick reference for lot of other interfaces.


Other references:
http://www.linuxtv.org/downloads/legacy/video4linux/v4l2dwgNew.html
Good sample to learn: v4l2 loopback-master

Check for
linux/videodev2.h
media/v4l2-ioctl.h
media/v4l2-common.h
http://www.kernel.org/doc/Documentation/video4linux/


Saturday, December 8, 2012

Linux Windowing System

If your system doesn't have windowing system, probably you will be limited to console. You might be working from remote or you are an admin. But if end user is a consumer who is interested to see at-least a minimal GUI, you have to provide windowing system. X windowing system is one of the common windowing system used these days. X works on client server structure. Where server is installed in the system you are interested to interact and client could be anywhere, either local to the system or connected through network. A project developed with really a well thought.

If you have installed server in your system, it is up to the client how to mange the layout. Server just provides the service for windowing and how to put it in screen, this is clients responsibility and interest. Each window on screen is a client of X.


Refer:
http://linuxdevcenter.com/pub/a/linux/2005/08/25/whatisXwindow.html?page=1
http://xwinman.org/
http://en.wikipedia.org/wiki/X_Window_System

Wednesday, November 21, 2012

Modem and linux

Most of the modems are available with USB interface. USB interface is a good option as it supports high speed communication (35MBPS by USB 2.0).  There are certain good utilities available to troubleshoot devices connected to USB interface.
"lsusb" command lists USB devices with device id, vendor id and its description.
To see more details of devices such as driver in use for the device, device class, product name and manufacturer name, etc "usb-devices" is another good utility. Same details could be seen at /sys/kernel/debug/usb/devices.

Coming back to our purpose. Here we will be communicating to modem over serial interface. And so we need a USB to serial converter driver. Once serial data is available we will be seeing the network packet and so will be usb_wwan driver. From above command we can find 3G modem device's vendor id and product id. Lets go to make things working.


$modprobe usbserial vendor=0x<vendor id> product=<product id>
$modprobe usb_wwan option


Now driver lets go to network interface part.
Create node using ip link and bring it up with dhcp client to get ip address.


http://antipastohw.blogspot.in/2012/05/how-to-install-3g4g-cellular-modem-on.html
http://freedune.wordpress.com/2011/06/23/success-with-usb-3g-modems-on-android/

USB modem:

https://wiki.archlinux.org/index.php/USB_3G_Modem
http://www.draisberghof.de/usb_modeswitch/
http://www.draisberghof.de/usb_modeswitch/bb/viewtopic.php?t=960

DHCP:

http://www.cyberciti.biz/faq/howto-linux-renew-dhcp-client-ip-address/

Friday, November 16, 2012

Hello JNI

http://marakana.com/forums/android/examples/49.html
I was working on created java native interface for one of my Android app. Its old during Android 2.1 days. Today after couple of years, I tried to create interface again, in my Ubuntu host system, and got some issues. I though better to keep one hello world post for JNI.

Create a project with name of your choice and copy this minimal code in class, lets say in callnative.java


package com.example.ih;

public class callnative {

static {
System.loadLibrary("my_lib");  //to load library
}

public native int add(int x, int y); //calling native function

public native String hello();

}

Go to /workspace/ih/bin path and give below command,

$javah -jni -classpath /home/abhishekkd/workspace/ih/bin/classes com.example.ih.callnative

$cat com_example_ih_callnative.h

You will see a heard file generated something like,


/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_example_ih_callnative */

#ifndef _Included_com_example_ih_callnative
#define _Included_com_example_ih_callnative
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     com_example_ih_callnative
 * Method:    add
 * Signature: (II)I
 */
JNIEXPORT jint JNICALL Java_com_example_ih_callnative_add
  (JNIEnv *, jobject, jint, jint);

/*
 * Class:     com_example_ih_callnative
 * Method:    hello
 * Signature: ()Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_com_example_ih_callnative_hello
  (JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif

Now, implement declared functions,

$gedit hello_lib.c

#include "com_example_ih_callnative.h"

JNIEXPORT jstring JNICALL Java_com_marakana_NativeLib_hello
  (JNIEnv * env, jobject obj) {
return (*env)->NewStringUTF(env, "Hello World!");
}

JNIEXPORT jint JNICALL Java_com_marakana_NativeLib_add
  (JNIEnv * env, jobject obj, jint value1, jint value2) {
return (value1 + value2);
}



Audio System

It is not simple to discuss about audio codec at one page as there are large verity for different purposes and SOC supports. This post is just to trigger one in right direction. Low level understanding is good for architecture such as ARM. For x86, you should directly go to ALSA.

When you are choosing an Audio codec you will be looking for:

Capability -
mono, stereo,  5.1
Number of output lines, number of input lines

Quality -
8kHz to 96kHz ADC audio
8kHz to 96kHz DAC audio
Price -
Depends on above two.

All of above choice depends on the purpose of use such as mobile phone or DOLBI sound system or is it a box for DJ.
You do consider power consumption and signal to noise ratio when you have options.

If we see for media player such as your portable music player or phone, you do consider Capability, you might consider what all are the means of inputs
  • Application/multimedia processor
  • FM
  • Compressed audio codec
  • Bluetooth Modem
Once you have listed your requirement, you are are ready for suitable vendor in market to chose one codec for your self. I will be considering that you are connecting codec direct to application processor and getting raw audio to play.




Have you connected the codec through I2C or SPI (to application processor). You could use UART or I2S, PCI, USB also based on interfaces available through the codec chip or sound card.
You have to write BSP to communicate with the card. Here you will be following datasheet of the product, how to use control lines and passing/getting data to and from the codec to application processor. 
Well, once you have written these codes, now you hare ready to write driver for your kernel. Sound sub-system is already available for you and so you can use API's to develop you application. Alsa good  for Linux and already available in kernel. Just make sure you do proper implementation of your BSP and coupled it with ALSA driver. Now rest is only to play sample app and test it.

Do we need to discuss more on it? There are some good explanation at an introduction to linux audio. Also palying with ALSA utilities, such as alsa mixer, is a good way to learn.

It is good to check official site of driver to get better understanding. 


Links:

Friday, November 9, 2012

D-Bus


to connect to D-bus,
conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, BLUEZ_NAME, &err);
check in      ./external/bluetooth/android-obex-obexd/src/manager.c

Connection failure could be checked in "err".
This will be giving service.

To invoke message in remote object, which must be connected to D-bus,

DBusMessage * dbus_message_new_method_call (const char *destination, const char *path, const char *interface, const char *method)
  Constructs a new message to invoke a method on a remote object.




Check this program:
int main(int argc, char *argv[])
{
struct sigaction sa;
DBusConnection *conn;
DBusError err;

dbus_error_init(&err);
debug("obex-client start to run");
conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, CLIENT_SERVICE, &err);
if (conn == NULL) {
if (dbus_error_is_set(&err) == TRUE) {
fprintf(stderr, "%s\n", err.message);
dbus_error_free(&err);
} else
fprintf(stderr, "Can't register with session bus\n");
exit(EXIT_FAILURE);
}

if (g_dbus_register_interface(conn, CLIENT_PATH, CLIENT_INTERFACE,
client_methods, NULL, NULL,
NULL, NULL) == FALSE) {
fprintf(stderr, "Can't register client interface\n");
dbus_connection_unref(conn);
exit(EXIT_FAILURE);
}

event_loop = g_main_loop_new(NULL, FALSE);

memset(&sa, 0, sizeof(sa));
sa.sa_handler = sig_term;
sigaction(SIGINT, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);

g_main_loop_run(event_loop);

g_dbus_unregister_interface(conn, CLIENT_PATH, CLIENT_INTERFACE);

dbus_connection_unref(conn);

g_main_loop_unref(event_loop);

return 0;
}

Thursday, October 11, 2012

Short and useful

Working with Bluez

To enable bluetooth driver in kernel, correct kernel configuration file to include Bluetooth subsystem, add required protocol supports, like rfcomm, l2cap, etc. Enable HCI support for the connection interface like USB or SDIO or which ever connection interface is required.

Now we have to include bluez-lib and bluez-utils in file system, which could be downloaded from bluez official site.
Once kernel and file system are in place, we have to bring bluetooth daemon up.
#/etc/init.d/bluetooth start

Now bluetooth service is ready to be used.

Useful tools for troubleshooting Bluetooth:

hcitool - to scan device remote device. There have few good options, but I have seen some are not working like connection.

hciconfig - To check and configure local bluetooth device

hidd - Bluetooth HID daemon

sdptool - control and interrogate SDP servers. provides the interface for performing SDP queries on Bluetooth devices, and administering a local sdpd.

obexftp - obexftp  is used to access files on mobile equipment, i.e. cell phones. With obexftpd you can transfer files between any computers using IrDA, Bluetooth and TCP/IP.
This tool lets you access the ObexFTP library by the means of a command line interface.  You might consider using  the  other  means  available. E.g. the ObexFS filesystem for Linux.

obexpushd - obexpushd is a program that can be used to receive files using OBEX (OBject EXchange) protocol over Bluetooth, IrDA or network connection. It can be used to receive files from mobile phones and other devices.
When run without -d or -n options obexpushd puts itself to the background and starts to listen for incoming connections via Bluetooth (default) and/or IrDA and/or network connection(TCP). obexpushd saves all received files to it's current directory but can alternativly forward all received data to a script for further processing.

obextool - A graphical frontend to browse obex file systems. able to communicate with mobiles and other communication devices using a standard communication protocol. Files can be uploaded to the mobile device, downloaded, renamed, copied, moved and deleted.  Detailed file properties may be displayed.

Links:



Saturday, September 15, 2012

Hawkboard getting started

Install necessery softwares in your linux system.

$ sudo apt-get install mono-complete wine
$ sudo apt-get remove libmono-uia-* mono-mcs

Now follow http://elinux.org/Hawkboard/BeginnersGuide and download following:
uImage
Rootfs
http://www-s.ti.com/sc/techlit/sprab41.zip

Now your system is ready to work on.

$cd ~/.wine/drive_c/Program Files/Texas Instruments/AISgen for D800K008


http://elinux.org/Hawkboard#Command_prompts_in_this_guide

Friday, August 24, 2012

ARM quick reference

ARM has three branches, application processor(Ax), RTOS processor (Rx), micro-controller series(Mx). From here on we will be discussing about application processor.

ARM arch & Mode

Cortex A5
Cortex A7
Cortex A8
Cortex A9
Cortex A15
bit.LITTLE processing - application switches between Cortex A7(energy efficient) and Cortex A15(high performance) to give high performance as well as good battery life(battery life increases by up to 70%)

SIMD - came 1st in ARMv6
Thumb - understanding
Thumb2
Trust Zone
Jazelle
AMBA
Floating point
DSP & SIMD
Virtualization
ARM Physical IP
ARM Classic Processors - ARM11, ARM9, ARM7
ARM SecureCore

CPU Mode:



-User mode
The only non-privileged mode.
-System mode
The only privileged mode that is not entered by an exception. It can only be entered by executing an instruction that explicitly writes to the mode bits of the CPSR.
-Supervisor (svc) mode
A privileged mode entered whenever the CPU is reset or when a SWI instruction is executed.
-Abort mode
A privileged mode that is entered whenever a prefetch abort or data abort exception occurs.
-Undefined mode
A privileged mode that is entered whenever an undefined instruction exception occurs.
-Interrupt mode
A privileged mode that is entered whenever the processor accepts an IRQ interrupt.
-Fast Interrupt mode
A privileged mode that is entered whenever the processor accepts an FIQ interrupt.
-Hyp mode
A hypervisor mode introduced in armv-7a for cortex-A15 processor for providing hardware virtualization support.


Switching Mode:

In the case of system calls on ARM, normally the system call causes a SWI instruction to be executed. Anytime the processor executes a SWI (software interrupt) instruction, it goes into SVC mode, which is privileged, and jumps to the SWI exception handler. The SWI handler then looks at the cause of the interrupt (embedded in the instruction) and then does whatever the OS programmer decided it should do. 
(save PC-4 to LR_svc, CPSR to SPSR, clear I flag, clear T flag, get vector from IVT and execute ISR/top half, enable I flag, resotor T flag)
The other exceptions - reset, undefined instruction, prefetch abort, data abort, interrupt, and fast interrupt - all also cause the processor to enter privileged modes.
How file handling works is entirely up to whoever wrote your operating system - there's nothing ARM specific about that at all.

Switching to Thumb:

Take next instruction in PC and add 1 i.e. PC+1, and then do break operation BR. Once BR executes, if LSB in PC is set, system executes immediate next instruction from BR in Thumb mode.

Thumb Mode Execution:

CPU doesn’t differentiate between thumb mode and ARM mode. It is a separate hardware within processor core which transforms 16 bit Thumb instruction to 32 bit ARM instruction without missing any single clock pulse. This operation happens during execution fetch(fetch -> decode -> execute).



DSP:

The ARM DSP instruction set extensions increase the DSP processing capability of ARM solutions in high-performance applications, while offering the low power consumption required by portable, battery-powered devices.

Features:

  • Single-cycle 16x16 and 32x16 MAC implementations
  • 2-3 x DSP performance improvement over ARM7™ processor-based CPU products
  • Zero overhead saturation extension support
  • New instructions to load and store pairs of registers, with enhanced addressing modes
  • New CLZ instruction improves normalization in arithmetic operations and improves divide performance
  • Full support in the ARMv5TE, ARMv6 and ARMv7 architectures

Applications:

  • Audio encode/decode (MP3: AAC, WMA)
  • Servo motor control (HDD/DVD)
  • MPEG4 decode
  • Voice and handwriting recognition
  • Embedded control
  • Bit exact algorithms (GSM-AMR)

The ARM DSP extensions enable increased DSP performance without the need for very high clock frequencies. This performance comes with almost no increase in power consumption on a typical implementation.The DSP extensions can often eliminate the need for additional hardware accelerators.

SIMD Extensions for Multimedia


SIMD extensions in the ARMv6 and ARMv7 architectures
Optimized for a broad range of software applications including video and audio codecs, where the extensions increase performance by up to 75% or more.  

ARMv6 SIMD Features:

  • 75% performance increase for audio and video processing
  • Simultaneous computation of 2x16-bit or 4x8-bit operands
  • Fractional arithmetic
  • User definable saturation modes (arbitrary word-width)
  • Dual 16x16 multiply-add/subtract 32x32 fractional MAC
  • Simultaneous 8/16-bit select operations
  • Performance up to 3.2 GOPS at 800MHz
  • Performance is achieved with a "near zero" increase in power consumption on a typical implementation

Applications:

  • Media streaming
  • Internet appliance
  • MPEG4 and H264 encode/decode
  • Voice and handwriting recognition
  • FFT processing
  • Complex arithmetic
  • Viterbi processing


NEON:

NEON™ technology builds on the concept of SIMD with a dedicated module to provide 128-bit wide vector operations, compared to the 32bit wide SIMD in the ARMv6 architecture. NEON technology introduced in the ARMv7 architecture is only available with ARM Cortex-A class processors. Advanced SIMD, supported by both Thumb instruction and ARM instructions.
Came with ARMv7, and Cortex A8 was the 1st processor to support it.

Vector Floating point(VFP):

ARM Floating Point architecture (VFP) provides hardware support for floating point operations in half-, single- and double-precision floating point arithmetic.
Using hardware floating point combined with the NEON™ multimedia processing capability, performance of imaging applications such as scaling, 2D and 3D transforms, font generation, and digital filters can be increased.


Thumb :

Good for low dencity code, thumb take 16 bit instruction size where as ARM uses 32 bit. Only good to work on thumb when memory is concerned. Becuase thumb dosen’t offer all reatures of ARM. Like talking to co-processor register. Execution wise also thumb and arm works same at CPU. Only while fetching thumb instructon, arm uses seperate hardware within core to convert 16 bit instruction to the replica of 32 bit instruction, without loss of cpu clock bit. Disadvantage with Thumb instruction comes when we want to implement some instruction with thumb doesn’t support. So there we need multiple instruction to do a job which could be done in one instruction by ARM mode code. understanding

How to jump to thumb mode? Whell don’t jomp by setting T bit of CPSR, it is dangerous. One way is by branch instruciton. Give address to branch instruction by setting LSB as 1. When cpu checks lsd as 1, it understands to run jumped location in THUB mode. There are other ways too.

Thumb-2

It is a blended instruction set combining both 16-bit and 32-bit instructions designed to deliver the best balance of density and performance enabling.
Thumb-2 instructions overcomes limitations of Thumb instruction sets as,

The main enhancements are:
  • 32-bit instructions added to the Thumb instruction set to:
    • provide support for exception handling in Thumb state
    • provide access to coprocessors
    • include Digital Signal Processing (DSP) and media instructions
    • improve performance in cases where a single 16-bit instruction restricts functions available to the compiler.

Amba:

Somewhat like PCI arch in Intel. This is open std. by arm to make design reliability.
The AMBA protocol is an open standard, on-chip interconnect specification for the connection and management of functional blocks in a System-on-Chip (SoC). It facilitates right-first-time development of multi-processor designs with large numbers of controllers and peripherals.
AMBA was introduced by ARM Ltd in 1996. The first AMBA buses were Advanced System Bus (ASB) and Advanced Peripheral Bus (APB).
In its 2nd version, AMBA 2, ARMadded AMBA High-performance Bus (AHB) that is a single clock-edge protocol.
In 2003, ARM introduced the 3rd generation, AMBA 3, including AXI to reach even higher performance interconnect and the Advanced Trace Bus (ATB) as part of the CoreSight on-chip debug and trace solution. These protocols are today the de-facto standard for 32-bit embedded processors because they are well documented and can be used without royalties.


TrustZone:

TrustZone technology, tightly integrated tightly into Cortex™-A processors, extends throughout the system via the AMBA® AXI™ bus and specific TrustZone System IP blocks. This system approach means that it is possible to secure peripherals such as secure memory, crypto blocks, keyboard and screen to ensure they can be protected from software attack.

Jazelle:

ARM Jazelle DBX (Direct Bytecode eXecution) technology for direct bytecode execution of JavaTM delivers an unparalleled combination of Java performance and the world's leading 32-bit embedded RISC architecture - giving platform developers the freedom to run Java applications alongside established OS, middleware and application code on a single processor.

ARM Jazelle RCT (Runtime Compilation Target) technology supports efficient ahead-of-time (AOT) and just-in-time (JIT) compilation with Java and other execution environments.




--------------------

References:

ARM reference for below all technology
http://www.arm.com/products/processors/technologies/instruction-set-architectures.php
http://www.arm.com/products/processors/cortex-a/index.php
http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0066d/Bcegdedj.html
IQ: http://embedded-telecom-interview.blogspot.in/2011/06/arm-processor-interview-questions.html

Thursday, August 16, 2012

Lets go to User Space


Compile busybox statically linked:

$make ARCH=arm CROSS_COMPILE=/home/abhishek/arm-2009q3/bin/arm-none-linux-gnueabi- menuconfig

$LDFLAGS="--static" make ARCH=arm CROSS_COMPILE=/home/abhishek/arm-2009q3/bin/arm-none-linux-gnueabi- 

$make ARCH=arm CROSS_COMPILE=/home/abhishek/arm-2009q3/bin/arm-none-linux-gnueabi- CONFIG_PREFIX=/home/abhishek/rpi/busybox/INSTALL-DIR install

Running Script from your application:

http://www.tuxation.com/setuid-on-shell-scripts.html



http://www.tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html



---------------------------------------------------------------------------------------------------------



Ethernet setup:

Check output of ifconfig in target system, if could see ethernet link then your ethernet link is up.
If you couldn't find ethernet interface then you need to bring interface up.
$ ip link show

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state DOWN qlen 1000
    link/ether 00:xx:xx:xx:xx:xx brd ff:ff:ff:ff:ff:ff
3: wlan0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 1000
    link/ether 00:xx:xx:xx:xx:xx brd ff:ff:ff:ff:ff:ff
6: ppp0: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 3
    link/ppp 


$ ip link show eth0

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state DOWN qlen 1000
    link/ether 00:xx:xx:xx:xx:xx brd ff:ff:ff:ff:ff:ff


$ ip link set eth0 up

$ifconfig eth0

Now you can see ethernet device up. But wont see ip address. So, here you have to assign device ip address,


$ ifconfig eth0 10.0.0.1 netmask 255.255.255.0 up


Starting GUI from console:

Your system is booted in shell prompt and you want to launch GUI window from there. Reason could be any, so basically you have to put system into correct run level. Command is simple,

$init <run level>
e.g. $ init 5

So lets understand run level, but again this may slightly vary (run level 3-5) with distributions. Which could be seen in /etc/inittab.

init 0 : shutdown/halt
init 1 : single user/admin task
init 2 : multi-user without network
init 3 : start normally with console/multi-user with network
init 4 : <user defined>
init 5 : start normally with GUI
init 6 : re-boot



References:


Working with Ethernet: http://linux-ip.net/html/tools-ip-link.html
http://people.debian.org/~ultrotter/talks/dc10/networking.html


Serial over Ethernet: http://www.novell.com/communities/node/4753/netconsole-howto-send-kernel-boot-messages-over-ethernet


---------------------------------------------------------------------------------------------------

Kernel

kprobes: A good tool to put break point in kernel execution and to debug.

Proc file system:

Build own linux based system:

--------------------------------------------------------------------------------------------------
Firefox OS

http://www.ifadey.com/2012/07/firefox-os-boot-to-gecko/
http://www.youtube.com/watch?v=yNWRMTqtQEI
https://wiki.mozilla.org/Gaia


-------------------------------------------------------------------------------------------------
Windowing System for new OS

http://xwinman.org/others.php
https://help.ubuntu.com/community/Installation/LowMemorySystems

Linux Distributions

http://distrowatch.com

--------------------------------------------------------------------------------------------------
Linux Boot-up



--------------------------------------------------------------------------------------------------
Worth Reading


Porting U-Boot: 

Monday, August 6, 2012

Raspberry Pi Getting started

First Look

Couple of days a go I received my raspberry pi, pre-ordered a month before.
In dimension, it exactly matches the size of my credit card, as they said. By finishing and clarity of the device, it is average, but no one will give such a board at such cost. Realy appricatlabe designing  which looks worth and logical for usablity and to reduce cost.  Few of appreciating designs are:

- Power supply by mini usb (worth as I already had my Mobile charger with rating of 5V-700mA)
- Searial connection through expansion pins
- No power on/off button
- JTAG and SD card  muxed to same pins
- 1st level of booting only through SD card, so you would be happy that you will not brick your board during development work.

I couldn't get detailed internals for the BCM2835 Soc, which disappointed me.

Lets Explore the device

Lets 1st try to understand how booting sequence goes on in the board, you may get these information from raspberry pi forum.
- When device is powered on, only GPU core gets turned on (ARM core, SDRAM and SD card controller is still off state)
- Bootloader from BCM2385 Soc ROM starts executing and scans SD card for bootcode.bin
- Here chain loading sequence is observed with broadcom firmware. ROM BL loads bootcode.bin as first bootloader, visible to user and present in FAT32 partition of SD card, to L2 cache. (note that SDRAM is still off and not available). So you can say, 1st stage initiates communication with SD card controller and also understands FAT32 file system only, as like many other 1st stage bootloader I have seen.
- bootcode.bin now enables SDRAM and loads loader.bin which basically understands elf format.
- loader.bin then loads start.elf at top of memory. Till this point nothing is visible in bootlog message.
- start.elf then does its jobs like split memory between ARM CPU and GPU, reads commandline.txt for getting information of root partition, its format and kernel parameters. I guess start.elf turns on peripherals for console/display. As start.elf could show some colourful screen through HDMI, which seams un-initialized. I guess here display subsystem is mapped to RAM memory area.
- start.elf program finds and loads kernel.img from root partition to SDRAM. Till this time, all  instructions were running in GPU core.
- Now control goes to ARM core to execute kernel image.

In short: GPU core(GPU start) -> BL from Soc ROM -> bootcode.bin -> loader.bin -> start.elf(GPU end) -> (CPU start) kernel.img

This boot sequence is fundamentally same across different board, but the way start.elf reads cmdline.txt, it shows similarity with GRUB BL.
We can play with our code only from kernel.img file or later. It is better if can put u-boot in the system to have more fun were we can do debugging traces.

Running Linux into the board

So now lets start with how to start porting Linux into the board:
Take appropriate SD card as in raspberry specification. Format it to FAT32 bootable format.

$ mkfs.vfat /dev/mmcxxxx
Now make two partitions in mmc card, one for boot device as FAT32 file system and another root partition as ext4 file system.

/boot  -- in FAT32 format to keep raspberry GPU firmwares, bootcode.bin, loader.bin, start.elf and kernel directive file cmdline.txt.

/root -- proper root device should be mentioned cmdline.txt file.

We need certain toolchains to cross compile linux into Ubuntu OS.

Install git and the cross-compilation toolchain:
$ sudo apt-get install git-core gcc-4.6-arm-linux-gnueabi

Make a directory for the sources and tools, then clone them with git:
$mkdir raspberrypi
$cd raspberrypi
$git clone https://github.com/raspberrypi/tools.git
$git clone https://github.com/raspberrypi/linux.git
$cd linux
Generate the .config file from the pre-packaged raspberry pi one:
$make ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- bcmrpi_cutdown_defconfig
For doing any configuration changes, open configuration menu and mark your changes,
$make ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- menuconfig
Once configuration file is prepared, start compiling kernel for r-pi device,
$make ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- -k -j5

mkdir ../modules

Then compile and ‘install’ the loadable modules to the temp directory:
$make modules_install ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- INSTALL_MOD_PATH=../modules/
Now we need to use imagetool-uncompressed.py to generate un-compressed kernel image for r-pi.
See below for necessary download links
$cd ../tools/mkimage/
$./imagetool-uncompressed.py ../../linux/arch/arm/boot/Image

Here you will find generated kernel.img.
Now we can copy kernel.img in mmc card.

$sudo cp -f kernel.img /media/<boot partition>/
Now copy generated library and firmwares from modules directory to mmc
$cd ../../modules/
$sudo cp -rf lib/ /media/<boot partition >/lib/
$sync

Now put mmc card into the r-pi mmc slot and power-on the device. HDMI cable was my 1st preference to see the output message, as my serial setup was not working initially. To get minimum indication of your source code execution, observe green led, next to red power led in r-pi board. If your instructions are executing, then you will see continuous blinks of green led.

So, if you see you kernel message, you are done with one stage, now you have to move ahead to execute user space code. Well hopefully in the end of above code execution, you might see kernel panic as your kernel would not be able to find root partition if you are not done with it.


Raspberry Pi on QEMU

Install latest QEMU version in your system. I have verified support in QEMU v1.5.

# qemu-system-arm -cpu ?|grep arm11

If you find arm1176 in list then you have support for rpi in qemu.

Download arch linux from raspberry pi official site and verify if it boots well.

# qemu-system-arm -cpu arm1176 -kernel kernel-qemu -m 256 -M versatilepb -no-reboot -serial stdio -append "root=/dev/sda2 panic=0" -hda archlinux-hf-2013-02-11.img

If it boots then it is allright to go for your custom image. Just use same arch linux image, mount at some place and use your custom kernel, or boot loader, or library.

Link: http://www.v13.gr/blog/?p=276

Links



Downloads

Follow https://github.com/raspberrypi/ link and download most of the required tools,


$git clone git://github.com/raspberrypi/tools.git
$git clone git://github.com/raspberrypi/linux.git
$git clone git://github.com/gonzoua/u-boot-pi.git
$git clone https://github.com/raspberrypi/firmware/



http://kernelnomicon.org/?m=201205
http://wiki.gentoo.org/wiki/Raspberry_Pi


References: http://raspberrypi.org/phpBB3/viewtopic.php?f=63&t=6685

Wednesday, July 25, 2012

Kernel Cross Compile for ARM


ARM core Socs are dominant in embedded market. For portable devices, power consumption is always a major concern and there ARM always stood tall, bringing newer Socs for new market demand, name it for a limited featured micro-controller or processor for smart devices.
Coming to cross compiling kernel for ARM in x86 host machine, here we go with the steps,

Kernel Source: kernel.org
Cross compiler tool chain: arm-2009q3-67

$tar -xvf linux-3.5.tar.bz2
$tar -xvf arm-2009q3-67-arm-none-linux-gnueabi-i686-pc-linux-gnu.tar.bz2

Extract kernel source and go to the kernel source directory.
Now you have to select kernel configuration file. You may start looking into <kernel>/arch/arm/configs/ folder to see available configurations in kernel tree. You can chose the configuration close to your device. If you are doing it just for experiment, you can chose vexpress_defconfig.

$cd linux-3.5
$ls ./arch/arm/configs
$make ARCH=arm vexpress_defconfig

In above command, make utility will look for vexpress_defconfig configuration file in arm architecture. If incase you miss to give ARCH directive, default x86 arch will be considered and will be searched for the given configuration file. This command will simply copy the configuration file in .config file name in kernel parent directory.

Once you have extracted arm toolchain, you have to pass tool path and prefixes to the compiler through make. You can see all available tools,

$ls arm-2009q3/bin/
You will find all tools are prefixed with arm-none-linux-gnueabi-
Well, now we can go for start compiling our kernel source as for ARM as,

$make ARCH=arm CROSS_COMPILE=/home/abhi/arm-2009q3/bin/arm-none-linux-gnueabi- -j4
$make ARCH=arm CROSS_COMPILE=/home/abhi/arm-2009q3/bin/arm-none-linux-gnueabi- modules_install INSTALL_MOD_PATH=$TARGETDIR -j4

Last parameter, -j4 is only to make use both the cores of my system processor. If your system is quadcore, use -j8 and so on.

Once your kernel is compiled you will get vmlinuz generated.

If you want to test with emulator, just follow with qemu tutorial here.

Monday, July 23, 2012

Begin with Compiling Linux Kernel


I was doing some work and got few issues. I started looking here and there in books, in posts. Then I realized to write a post for some other guy who would be in same situation as I am or I was.

This guide is for those who are new to linux kernel and want to compile kernel for the 1st time. You might get some deviation with coming new versions but once you get what you are doing you can do in newer version too. Well, you can google, why do you want to compile kernel.

Below steps are tested with kernel v2.6.38.2. I am suggesting you to follow each step sincearly otherwise most likely you will get a very common FATAL ERROR: /lib/modules/<kernel>/modules.dep not found. Don't blame me if you won't get success. Most of the people don't get sucess at 1st attempt. And your system configuration, current kernel, gcc, etc.. these all matters when you start.

-Remember that you are compiling kernel, not cross compiling.
-Remember, don't touch your old working kernel image, its Config, System.map, Modules, entry at grub.cfg. Your old kernel will save you if your new kernel dosn't boot.

1) Download kernel source source from kernel.org, throughout the discussion I will refer my kernel version and you should stick to your version in respective places.
$ linux-2.6.38.2.tar.bz2

2) untar the kernel source
$ tar -xvf linux-2.6.38.2.tar.bz2
Check README.txt file from your parent kernel directory. You will find which gcc version you should use and verify it in your system, $ gcc -v

3) If you had already tried compiling kernel, you should clean previosly generated objects and bundels.
$ make clean

4) You have to make kernel configuration. There are number of ways to do it which gives different UI fashion but in the end, you will be generation .config file. Or best way is to copy .config file which was used in your working kernel.
 $make menuconfig
or
 $make xconfig
Or any other way to make kernel configureation, ultimately you are doing same thing with your interest. And then make your choise from various options.
    After you are done, you will see .config file created. This .config file will be present in your parent kernel source tree. Check with command if it get listed out. $ls -a|grep .config
You can directly execute below instruction to copy config of your current working kernel.
$ cp /boot/config-`uname -r` ./.config

5) Now you are going to create kernel image, bzImage. This step may take some time, so you can have your lunch or dinner in meantime. Hopefully you wont get error inbetween ;).
  $make -j4

Here -j4 is optional, which tells compiler to use both the cores of your system if your system is dual core. Usually kernel has two thread so in -jn, n=2x(number of cores) so for quad core, it is 8.

6) If you are using x86 processor, afer above step, you will see bzImage as <source parent>/arch/x86/bzImage. Do below steps, hopefully you understand what you are doing here. You may need root permission. Be very careful about naming version. You had downloaded linux version "2.6.38.2". Most of the time people do mistake at this step.

cp ./arch/x86/bzImage /boot/vmlinuz-2.6.38.2
cp ./.config /boot config-2.6.38.2
cp ./System.map  System.map-2.6.38.2

7) create init ramdisk image.
   initramfs -k -o ./initrd.img-2.6.38.2 2.6.38.2

8) Copy initrdimage to boot folder.

   cp ./initrd.img-2.6.38.2 /boot/

9)  And register your kernel to your bootloader. In may case, GRUB
   update-grub
   you will see some action happening, like grub.cfg...
   Now check grub.cfg
   vi /boot/grub/grub.cfg
----------------------------------------------------------------------------------------------------
   menuentry 'Ubuntu, with Linux 2.6.38.2' --class ubuntu --class gnu-linux --class gnu --class os {
        recordfail
        insmod part_msdos
        insmod ext2
        set root='(hd0,msdos6)'
        search --no-floppy --fs-uuid --set 2fde25ee-ea28-4acf-96d7-e972527cd820
        linux   /boot/vmlinuz-2.6.38.2 root=UUID=2fde25ee-ea28-4acf-96d7-e972527cd820 ro   quiet splash
        initrd  /boot/initrd.img-2.6.38.2
   }
----------------------------------------------------------------------------------------------------
 If you wont find proper entry at "initrd" or "root". Most likely you have done somthing wrong, possibly your initrd.img is not correct. Google and correct.
 I would suggest you to compare above like entry with your working kernel version, and try to understand what that is. It will help you to reach near to the problem.

 10) now check at /lib/modules, you will find kernel modules of working kernel. Now this is the to make your kernel module and install.
  make modules
  make modules_install
 If you are successful, you will find entry of your kernel module at /lib/modules

 11) If you have done all steps properly, you might have built and installed your kernel/kernel modules. Comeon reboot your system and boot with your new kernel.

All the best. Many of the people don't get success at 1st attempt, unless he is luckey.