[Description]
驱动访问NvRAM数据,建议添加一个进程,并在init.rc里面启动。
这个做法的好处:NvRAM文件有设访问权限,以避免不必要的访问导致数据篡改。在init.rc添加进程,设置权限,使得访问变得可控。
[Solution]
1. 驱动如要访问NVRAM,需要添加一个NVRAM Agent(进程),如下示意图:
2. 举例添加一个NVRAM Agent(nvram_customop),其对应的源文件nvram_customop.c 简述如下:
1) Read from Nvram(read_nvram) fd = NVM_GetFileDesc(LID, &rec_size, &rec_num, ISWRITE); read(fd, &nvram_gpcustom, rec_num*rec_size); NVM_CloseFileDesc(gp_custom_fd); 2) Write to device file(write_device) device_fd = open("/dev/als_ps", O_RDONLY); ioctl(device_io_fd, ALSPS_SET_PS_MODE, &ps_threshold); 3) Nvram agent main() void main(int argc, char *argv[]) { read_nvram(); write_device(); } |
3. 编译这个Agent为可执行文件,在nvram_customop.c所在目录添加android.mk文件,并修改其内容如下:
LOCAL_PATH := $(my-dir) include $(CLEAR_VARS) LOCAL_MODULE_TAGS := eng LOCAL_SRC_FILES := \ nvram_customop.c LOCAL_C_INCLUDES := \ $(MTK_PATH_SOURCE)/external/nvram/libnvram LOCAL_PRELINK_MODULE := false LOCAL_SHARED_LIBRARIES := \ libnvram \ libnativehelper \ libcutils \ libutils LOCAL_MODULE := nvram_customop include $(BUILD_EXECUTABLE) |
4. 在init.rc中启动这个进程,这个访问nvram的进程nvram_customop需要等待nvram 初始化完成才能工作
service nvram_daemon /system/bin/nvram_daemon user root group nvram oneshot
service nvram_customop /system/bin/nvram_customop user system group system nvram oneshot disabled on property:nvram_init=Ready start nvram_customop
|
Note:
标红部分,不同平台可能会有所不同。
最早是用的user root,无group设定,不符合CTS要求。
因此在ICS2.MP之后修正了root的权限问题,ICS2.MP需要打patch ALPS00292373,ICS.MP需要打patch ALPS00322643。
判断nvram ready的条件:
KK(4.4)版本之前:on property:nvram_init=Ready
L(5.0)版本之后:on property:service.nvram_init=Ready