Android 10增加硬件抽象层(HAL)模块访问Linux内核驱动程序

学习总纲:

Android10 硬件抽象层(HAL)概要介绍和学习计划

编译环境:

   - android 版本 - android-10.0.0_r41
   - 架构 - aosp_x86_64-eng
   - 内核分支:android-goldfish-4.14-gchips

参考教材:

 罗升阳:https://blog.csdn.net/luoshengyang/article/details/6573809
 
 依赖前文:https://blog.csdn.net/silently_frog/article/details/124261023

目的

记录android 10 用老罗的方法编译后发现有些函数和makefile用法过时

修改如下:

  1. hello.c 中修改
#define LOG_TAG "HelloStub"
 
#include <malloc.h>
#include <stdint.h>
#include <log/log.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <cutils/atomic.h>
#include <stdlib.h>
#include <unistd.h>
#include <hardware/hardware.h>
#include <hardware/hello.h>

 
#define DEVICE_NAME "/dev/hello"
#define MODULE_NAME "Hello"
#define MODULE_AUTHOR "931846027@qq.com"
 
/*设备打开和关闭接口*/
static int hello_device_open(const struct hw_module_t* module, const char* name, struct hw_device_t** device);
static int hello_device_close(struct hw_device_t* device);
 
/*设备访问接口*/
static int hello_set_val(struct hello_device_t* dev, int val);
static int hello_get_val(struct hello_device_t* dev, int* val);
 
/*模块方法表*/
static struct hw_module_methods_t hello_module_methods = {
	open: hello_device_open
};
 
/*模块实例变量*/
struct hello_module_t HAL_MODULE_INFO_SYM = {
	common: {
		tag: HARDWARE_MODULE_TAG,
		version_major: 1,
		version_minor: 0,
		id: HELLO_HARDWARE_MODULE_ID,
		name: MODULE_NAME,
		author: MODULE_AUTHOR,
		methods: &hello_module_methods,
	}
};


static int hello_device_open(const struct hw_module_t* module, const char* name, struct hw_device_t** device) {
	struct hello_device_t* dev;dev = (struct hello_device_t*)malloc(sizeof(struct hello_device_t));
	
	if(!dev) {
		ALOGE("Hello Stub: failed to alloc space");
		return -EFAULT;
	}
 
	memset(dev, 0, sizeof(struct hello_device_t));
	dev->common.tag = HARDWARE_DEVICE_TAG;
	dev->common.version = 0;
	dev->common.module = (hw_module_t*)module;
	dev->common.close = hello_device_close;
	dev->set_val = hello_set_val;dev->get_val = hello_get_val;
 
	if((dev->fd = open(DEVICE_NAME, O_RDWR)) == -1) {
		ALOGE("Hello Stub: failed to open /dev/hello -- %s.", strerror(errno));free(dev);
		return -EFAULT;
	}
 
	*device = &(dev->common);
	ALOGI("Hello Stub: open /dev/hello successfully.");
 
	return 0;
}

static int hello_device_close(struct hw_device_t* device) {
	struct hello_device_t* hello_device = (struct hello_device_t*)device;
 
	if(hello_device) {
		close(hello_device->fd);
		free(hello_device);
	}
	
	return 0;
}
 
static int hello_set_val(struct hello_device_t* dev, int val) {
	ALOGI("Hello Stub: set value %d to device.", val);
 
	write(dev->fd, &val, sizeof(val));
 
	return 0;
}
 
static int hello_get_val(struct hello_device_t* dev, int* val) {
	if(!val) {
		ALOGE("Hello Stub: error val pointer");
		return -EFAULT;
	}
 
	read(dev->fd, val, sizeof(*val));
 
	ALOGI("Hello Stub: get value %d from device", *val);
 
	return 0;
}
  1. Android.mk的修改
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_CFLAGS += -Wno-unused-parameter -Wno-gnu-designator
LOCAL_MODULE_TAGS := optional
LOCAL_PRELINK_MODULE := false
LOCAL_MODULE_RELATIVE_PATH := hw
LOCAL_SHARED_LIBRARIES := liblog
LOCAL_SRC_FILES := hello.c
LOCAL_MODULE := hello.default
include $(BUILD_SHARED_LIBRARY)

  1. 配置selinux
./system/sepolicy/private/file_contexts
./system/sepolicy/private/system_server.te
./system/sepolicy/public/device.te
 
./system/sepolicy/prebuilts/api/26.0/private/file_contexts
./system/sepolicy/prebuilts/api/26.0/private/system_server.te
./system/sepolicy/prebuilts/api/26.0/public/device.te
 
./system/sepolicy/prebuilts/api/27.0/private/file_contexts
./system/sepolicy/prebuilts/api/27.0/private/system_server.te
./system/sepolicy/prebuilts/api/27.0/public/device.te
 
./system/sepolicy/prebuilts/api/28.0/private/file_contexts
./system/sepolicy/prebuilts/api/28.0/private/system_server.te
./system/sepolicy/prebuilts/api/28.0/public/device.te
 
./system/sepolicy/prebuilts/api/29.0/private/file_contexts
./system/sepolicy/prebuilts/api/29.0/private/system_server.te
./system/sepolicy/prebuilts/api/29.0/public/device.te

在每一个file_contexts文件中都增加一行

/dev/hello		u:object_r:hello_device:s0

请添加图片描述
在每一个system_server.te文件中都增加一行

allow system_server hello_device:chr_file rw_file_perms;

请添加图片描述
在每一个device.te文件中都增加一行

type hello_device, dev_type, mlstrustedobject;

请添加图片描述


执行:mmm hardware/libhardware/modules/hello

可以在out/target/product/generic/system/lib/hw 和 lib64/hw目录下看到hello.default.so文件了

再执行make snod 和 make -j8,运行模拟器。并adb shell
就可以在模拟器中的system/lib/hw 和 system/lib64/hw 看到hello.default.so文件
请添加图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值