Saturday, May 5, 2012

Samsung Galaxy S2 codes

Here are some codes for Samsung Galaxy S2.
  • *#1234#  -- Firmware Version.
  • *#232337#  -- Bluetooth MAC Address.
  • *2767*3855# -- Factory Reset.

Tuesday, April 24, 2012

VirtualBox shared folders

In the Oracle VirtualBox, to add a shared folder to a Linux guest, open Devices>Shared Folders...  and click Add Shared Folder (a tiny "plus" icon on the right). Then fill folder path and folder name.
In Linux, do the mount by the following commands:
> sudo mkdir /home/shared # creating mount point
> sudo mount -t vboxsf VShared /home/shared
# use previously filled folder name
After every reboots the mount command is needed to repeat.
To do it automatically, just add the following lines to fstab.
> sudo echo "VShared    /home/shared    vboxsf    rw    0    0" >> /etc/fstab

Friday, April 20, 2012

find | xargs with spaces in filenames

The following command is one of the simplest way to find a text in the files.
    find * | xargs grep "text"
but if the file name contains a space then the xargs command split it and transfer spited items as a separate files to the grep, and an error occurs:
    grep: "a part of file name": No such file or directory

To overcome this issue the find has an -print0 option, which separates file names with ASCII NUL instead of new line. And the -0 option will signal the xargs to use NUL as a separator instead of blank spaces.
    find * -print0 | xargs -0 grep "text"

Tuesday, April 3, 2012

Compile gdb for embedded devices

To debug a program on embedded devices with limited resources(memory, CPU) the gdbserver is used. The gdbserver is running on the embedded device, while the gdb is running on a regular PC and connect with gdbserver via network. The first step is to compile gdb for embedded device(platform). The following steps are for platform with ARM CPU.

Get the sources from official site:
Compile gdb
>tar -zxf gdb-7.4.tar.gz #extract the sources
>cd gdb-7.4
> ./configure --prefix=/home/programs/gdb/ --build=x86 --host=x86 --target=arm-linux
"--prefix"  - where to install the program
"--build" - specify which platform is used for compilation.
"--host"  - specify which platform is used for run gdb.
"--target" - specify which platform is used for run the debugging program.
>make install

Compile gdbserver(the gcc compiler for arm-linux is necessary)
>tar -zxf gdb-7.4.tar.gz #extract the sources into new folder
>cd gdb/gdbserver
>./configure --host=arm-linux
during the compilation linux-arm-low.c:642: error: `PTRACE_GETSIGINFO' undeclared (first use in this function) error occurs. Seems a include is missing, but by adding the following line before the error line, the problem will be solved.
#define PTRACE_GETSIGINFO    0x4202

Copy the gdbserver executable from current directory to the target device and run:
>gdbserver :<port> <program>
From the PC run gdb and connect to gdbserver vi the network:
(gdb)>target remote ip:port
Now the program can be debuged with usual gdb commands.

Tuesday, February 7, 2012

_init and _fini (obsolete functions)

There are two special functions for library construction and destruction. Despite they are obsolete and replaced by generic constructor/destructor functions, they are used in a lot of libraries. Lets write a simple library with one some_func() function and with _init(), _fini() special functions.

In the above code there are 3 files: the library itself - samplelib.h, samplelib.c  and the application - main.c.

for compiling the library:
    $ gcc -fPIC -c samplelib.c
    $ gcc -nostartfiles -shared -o samplelib.o

Here -fPIC option is standing for position independent code. The -shared option is for shared library creation. By default gcc uses _fini () and _init() defined in crti.o, so -nostartfiles signals to gcc to not do that.
 And to compile the application we run the following.
    $ gcc -o main main.c -L. -lsamplelib
Here -L. option informs to gcc to search libraries in current directory, and -lsamplelib - to use shared library.

Finally by running the program the following will be outputted:
    samplelib: _init is called
    main: main is called
    samplelib: some_func is called
    samplelib: _fini is called