Embbyte of the Day : 31st May , 2013

Let us take some time to Thank God for addr2line     objdump  and nm today. And let us have a look at this thread in stackoverflow on difference between nm and objdump

Embedded Linux training material

A great great link

 

http://free-electrons.com/docs/

 

I am gonna add this to my links section next.

 

I really liked the pdfs which I went through

 

Android boot process from power ON

A very good link to understand the basic underlying process :

ENEA Android Blog : http://www.androidenea.com/2009/06/android-boot-process-from-power-on.html

For Linux booting:   http://www.simtec.co.uk/products/SWLINUX/files/booting_article.html#d0e600

Some commands useful for vi editor

Search STRING forward : /STRING.
Search STRING backward: ?STRING.

Repeat search: n
Repeat search in opposite direction: N (SHIFT-n)

Searching and replacing string:
Between two lines #,#::#,#s/OLD/NEW/g

Replace # with line numbers :0,$ will replace in whole files

Everyday Useful linux Commands

This is an ongoing post and I will keep adding more and more commands here :

1)To find out how much free disk space is available :

df -h                                                                       ( h is for human readable form)

2)To re-execute  a previously issued command :

alt-P and type in few chars of command you had issued earlier

3) To find out RAM memory size

free -m                                                                      ( gives RAM memory size in MB)

4) OS infrmation

uname -a

5)To re-execute a previously issued command with sudo previleges

sudo!!

6)Make a whole directory tree with one command

mkdir -p mydir/yourdir/hisdir/herdir

7)cd tricks

cd    (alone)takes you to the home directory – so does cd ~

cd – returns to the previous directory from where you had issued a cd command and reached here

cd–  goes back twice ,cd— goes back thrice and so on

8)

Printing the size of a binary file

fstat function in fcntl.h (linux) is paricularly useful for printing all the status information of a file.I found it particularly useful where I  had to determine the size of a binary file without reading it till the end….handy and useful.

Here is the sample program :

#include <sys/types.h>

#include<sys/stat.h>

#include<fcntl.h>

int main(void)
{

struct stat file_stat;
int  status;
unsigned long int file_size;

int file_dest = open(“./mydir/abc.bin”,O_RDWR);

// get file status

status = fstat(file_dest, &file_stat);

// print file status information
printf(“file abc.bin size %d\n”, file_stat.st_size);

return 0;

}

and there you go …

Passing Custom parameters to linux kernel at commandline

Parameters can be passed to linux kernel through commandline – if you want to pass some custom/user-defined parameter(in the form of option= value)apart from the parameters from one of the built in options provided for commandline in documentation.This parameter can be passed in different ways depending upon whether you want to pass that parameter to a:

->built in module

->dynamically loadable module

->a global variable/parameter  visible/accessible across the modules within the kernel

I am covering the 3rd scenario here.

You can take a user defined variable : user_var and use it as following in init/main.c

unsigned int user_var;
EXPORT_SYMBOL (user_var);                 //make it global
.
.
.

static int __init set_my_param(char *str)

{

get_option(&str, &user_var);

return 1;

}

__setup(“user_var=”, set_user_var);

Some notes :

__init  places code in a special section, This is only a macro to locate some parts of the linux code into special areas in the final executing binary.__init instructs the compiler to mark this function in a special way. At the end the linker collects all functions with this mark at the end (or beginning) of the binary file. When the kernel starts, this code runs only once (initialization). After it runs, the kernel can free this memory to reuse it
For more information,take a look at the source(include/linux/init.h).

__ (double underscore) does not have a special purpose except to avoid name conflicts.

__setup will be used to set the value of user_var and is not going to be called if you don’t have an “user_var=xxx”  string on the
commandline.

We can  then add “user_var=<value>” to the “kernel” line in grub.conf

and in the kernel code areas where you  need to use this parameter,you can extern it and use it.