Wednesday, September 26, 2012

Adding System Call in Android bionic library

Finally after long time gap I am in my blog again. This time I will explore adding system call in bionic libc.

For simplicity sake let me tell about getpid() system call which is already present

Kernel Changes :

1.  kernel/include/linux/syscalls.h
        This header file provides syscall function prototype
eg:   asmlinkage long sys_getpid(void);
return type: long
asmlinkage macro tells the compiler to pass all function arguments on the stack.

2. kernel/kernel/timer.c
        This source file contains actuall function definition

SYSCALL_DEFINE0(getpid)
{
     // Body of getpid() function.
}

3. kernel/arch/arm/include/asm/unistd.h

The system call need to be given a number in kernel that piece of code is done here
eg: #define __NR_getpid         (__NR_SYSCALL_BASE+ 20)

4. kernel/arch/arm/kernel/calls.S

The declared system call need to be exposed in syscall_table that is done here
eg: CALL(sys_getpid)

This completes Kernel part of exposing syscall getpid()


Bionic Changes:

5. bionic/libc/SYSCALLS.TXT

This is the only place getpid need to exposed in userpace, rest all is taken care automatically by gensyscalls.py python script.
The syntax goes likes this
return_type func_name[:syscall_name][:call_id]([parameter_list])  (#syscall_number|stub)

eg: pid_t getpid()  20

gensyscalls.py script automatically creates getpid.S Stub file and placed in 2 different locations as given below
a. bionic/libc/arch-arm/syscalls.mk
     eg: syscall_src += arch-arm/syscalls/getpid.S

b. bionic/libc/arch-arm/syscalls/getpid.S


With both the changes cross-compile kernel and platform binaries so that you can enjoy the syscall interface between userspace and kernel space.

Please do comment if something is missed...


Always brighter side of the world...
-chandu













Tuesday, May 1, 2012

Creating Symbolic Links, Copy files using Android.mk and learn about permission changing inside init.rc

Its been long time I blogged, I learnt few things in Android.mk which I wanted to share with fellow members.

I started working on ICS Codebase where I have requirement to create Symbolic links, copy files/folders as part of prebuilt to /data and /system partitions and change the permissions during boot time via init.rc

Symbolic Links:
when you want to create symbolic link for a binary which is present in /system/bin the below code snippet will help you achieve the task
/system/bin/xyz -> /system/bin/abc

Pre-requisites:
Inside Android.mk create binary for abc which goes inside /system/bin

COMMANDS = xyz
SYMLINKS := $(addprefix $(TARGET_OUT_EXECUTABLES)/,$(COMMANDS))
$(SYMLINKS): ABC_BINARY := abc
$(SYMLINKS): $(LOCAL_INSTALLED_MODULE) $(LOCAL_PATH)/Android.mk
                   @echo "Symlink: $@ -> $(ABC_BINARY)"
                   @mkdir -p $(dir $@)
                   @rm -rf $@
                   $(hide) ln -sf $(ABC_BINARY) $@
ALL_DEFAULT_INSTALLED_MODULES += $(SYMLINKS)

Explanation:
xyz would be your symbolic link for abc binary. The above code is self explanatory, please ping me if you dont understand.

Note: symlinks can be created inside init.rc also
ex: symlink /system/etc /etc

Copy files/folders:

PRODUCE_COPY_FILES += external/abc_folder/abc.conf:data/etc/abc.conf

The above command copies abc.conf file from external/abc_folder to /data/etc/ location. when you build and flash the binary you can verify the same in the adb shell

Changing Permissions and creating folders as part of system boot:


init.rc is the location which you need to address for achieving this task,

under on post-fs-data trigger inside init.rc
create below mentioned commands which will be reflected during your boot time.

mkdir /data/var
chmod 0755 /data/var






Wednesday, February 15, 2012

Making Android apps as System Apps during factory building

Today I was asked to build an android application which should go into /system/app rather than being 3rd pary app /data/app

I had written below make file which looks pretty similar to Email app which comes by default with the android source code.

I will explain you what exactly some of the tags means in Android.mk

Note: Place your application inside $(Android_root_path)/packages/apps
Your android application might contains below files/folders

Example: if your application name is "xyz"
1. src/
2. res/
3. libs/
4. AndroidManifest.xml
5. assets/
6. default.properties etc...

Place this Android.mk in your application folder has the files placed above and compile entire source code  $(Android_root_path)# make

Output: You will generate apk file out of your application which will go with your /system/apps. Everytime you need not have to install this application like your 3rd party applications.

LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)

#This tag is must for making it system apps
LOCAL_MODULE_TAGS := optional

LOCAL_SRC_FILES := $(call all-java-files-under, src)

#Any aidl files in your application can be added here
LOCAL_SRC_FILES += \
    src/com/android/xyz/services/IxyzServiceCallback.aidl \
    src/com/android/xyz/services/IxyzService.aidl

LOCAL_JAVA_STATIC_LIBRARIES := \

# include the prebuilt static library which is mentioned in
#LOCAL_PREBUILT_STATIC_JAVA_LIBARIES
LOCAL_STATIC_JAVA_LIBRARIES := liblog4j

LOCAL_PACKAGE_NAME := xyz

LOCAL_PROGUARD_FLAGS := -include $(LOCAL_PATH)/proguard.cfg

include $(BUILD_PACKAGE)

include $(CLEAR_VARS)


#include any prebuilt jars you are using in your application which is present in
#libs folder of your package
LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES := liblog4j:libs/log4j.jar

include $(BUILD_MULTI_PREBUILT)

include $(call all-makefiles-under,$(LOCAL_PATH))