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;
}

 

發佈了330 篇原創文章 · 獲贊 228 · 訪問量 315萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章