linux/android kernel层读写二进制数据我找了些示例代码

#define NODE "/dev/block/mmcblk0p48"
int get_partition_info(const char *filename, char *buf, loff_t offset, int length, bool flag)
{
        struct file *filep;
        mm_segment_t old_fs;

        filep= filp_open(filename, O_RDONLY, 0);
        if(IS_ERR(filep))
        {
                CDBG("%s:%d open %s err!\n",__func__,__LINE__,filename);
                return -ENODEV;
        }

        old_fs = get_fs();
        set_fs(KERNEL_DS);
        CDBG("%s:%d offset:%d\n",__func__,__LINE__, (int)offset);
        filep->f_op->llseek(filep, offset, SEEK_CUR);
        if(flag==READ_FLAG)
                length=filep->f_op->read(filep, buf, length, &filep->f_pos);
        else
                length=filep->f_op->write(filep, buf, length, &filep->f_pos);
        set_fs(old_fs);
        filp_close(filep, 0);
        return length;
}

 

你可能感兴趣的:(Android驱动开发,linux内核,Linux驱动)