内核中读写分区的例子

  如果要在内核中读写分区,可参考如下例子。

  基本思想是将分区当成普通的文件,在驱动中直接读写文件(可参考http://blog.csdn.net/mike8825/article/details/50906429),例子如下

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <asm-generic/uaccess.h>

#define  NODE "/dev/block/mmcblk0p19"

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)){
        printk(KERN_CRIT"open %s err!\n",filename);
        return -1;
    }
 
    old_fs = get_fs();
    set_fs(KERNEL_DS);
    filep->f_op->llseek(filep, offset, SEEK_SET);
    if(flag==0)
        length=vfs_read(filep, buf, length, &filep->f_pos);
    else
        length=vfs_write(filep, buf, length, &filep->f_pos);
    set_fs(old_fs);
    filp_close(filep, 0);
    return length;
}

static int __init partition_init(void) {
    char *buf;
    char *str="####*****####";
    int size;
    printk("%s,%s,%d\n",__FILE__,__func__,__LINE__);
    buf=kmalloc(100,GFP_KERNEL);
    size=strlen(str);
    memcpy(buf,str,size);
    get_partition_info(NODE,buf,0,size,1);
    get_partition_info(NODE,buf,0,size,0);
    *(buf+size)='\0';
    printk("the partition info is %s\n",buf);
    return 0;
}

static void __exit partition_exit(void){
    printk("%s,%s,%d\n",__FILE__,__func__,__LINE__);
}

module_init(partition_init);
module_exit(partition_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("www");

要写裸分区,也可借助c应用层来实现,可用write接口(nand需要ioctl&&write)来实现,最简单的案例

cat /system/logo.bmp > /dev/block/mmcblockp19

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值