Android驱动加载方法

1在终端上insmod xxx.ko,适合调试阶段。


2将驱动编译进内核。


3从int.rc中运行某一个脚本

service loaddriver /system/driver/load.sh
   class main
   user root
   group root
   oneshot

sevice loaddriver /system/driver/load.sh启动loaddriver(名字随意)服务,相当于在终端上运行/system/driver/load.sh,后面还可接参数,

如sevice xxx /system/bin/xxxx A B

load.sh脚本的内容为

#! /system/bin/sh
insmod /system/driver/touchscreen.ko
insmod /system/driver/hello.ko

4从int.rc中运行某一个应用程序

如load.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>  
#include <sys/types.h>
#include <cutils/log.h>
#include <errno.h>  
#define LOG_TAG  "loaddriver"
#define filename "/system/driver/load.conf"
pid_t status;
void systemstatus(int status){
    if (-1 == status) {  
        SLOGE("system error!");  
    }  
    else  {  
        SLOGE("exit status value = [0x%x]\n", status);  
        if (WIFEXITED(status)){  
            if (0 == WEXITSTATUS(status)){  
                SLOGE("run shell script successfully.\n");  
            }  
            else{  
                SLOGE("run shell script fail, script exit code: %d\n", WEXITSTATUS(status));  
            }  
        }  
        else{  
            SLOGE("exit status = [%d]\n", WEXITSTATUS(status));  
        }  
    }   
} 

int main(int argc,char **argv)
{
   FILE *fp;
   char str[1024];
   char *buf;
   buf=(char *)malloc(1024);
   if(buf==NULL){
       SLOGE("malloc memory err\n");
       return -1;
   }
   SLOGE("malloc memory ok\n");
   fp=fopen(filename,"r");
   if(fp==NULL){
	SLOGE("open err!\n");
        return -1;
   }
   SLOGE("open ok!\n");
   while(!feof(fp)){
	if(fgets(str,1024,fp)==NULL)
  	  break;
	SLOGE("the msg is %s and the length is %d %d\n",str,strlen(str),sizeof(str));
        system(str);
        systemstatus(status);
	SLOGE("-----------------------------------------------------------------------\n");	     
   }
   fclose(fp);
   return 0;
}


load.conf的文件内容为                                  

ls -al
insmod /system/driver/touchscreen.ko
insmod /system/driver/hello.ko

相应的Android.mk内容为

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_PRELINK_MODULE := false
LOCAL_SHARED_LIBRARIES := liblog
LOCAL_MODULE := load
LOCAL_SRC_FILES := $(call all-subdir-c-files)
include $(BUILD_EXECUTABLE)

然后在init.rc中添加

service loaddrivers /system/driver/load
   class main
   user root
   group root
   oneshot

你可能感兴趣的:(Android驱动加载方法)