如果要在内核中读写分区,可参考如下例子。
基本思想是将分区当成普通的文件,在驱动中直接读写文件(可参考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