微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 手机设计讨论 > MTK手机平台交流 > 如何在kernel中读写raw数据

如何在kernel中读写raw数据

时间:10-02 整理:3721RD 点击:
[DESCRIPTION]
在kernel起来之后,在创建的进程中读写raw数据,这里给出一段code,仅供参考。
[SOLUTION]
这段code仅供参考,在实际使用过程中,需要根据实际情况进行修改和完善。需要说明的是,读写数据的时候,要按sector或者pagesize来操作。
///////////////////////////////////////////////////////////
struct mtd_info_user info;
struct erase_info_user erase_info;
int do_read_write(){
int fd,of,i=0,n,srcSize,pageSize;
char *buffer = NULL;
char src[16]="0123456789abcdef";
pageSize =2048;
srcSize = sizeof(src);
printf("size of src is :%d\n",srcSize);
buffer = (char*)malloc(pageSize);
if(buffer == NULL)
{
printf("ERROR buffer malloc fail!");
return -1;
}
mEMSet(buffer, 0, pageSize);
meMCPy(buffer,src,srcSize);
printf("buffer:%s,size:%d\n",buffer,sizeof(buffer));
printf("src:%s,size:%d\n",src,sizeof(src));
char PN[20];
sprintf(PN,"/dev/nvram");
printf("PN:%s\n",PN);//open your raw data partiton
fd= open(PN,O_RDWR);
if(fd<= 0){
printf("ERROR open fail %d\n",fd);
return -1;
}
//getinfo
n=ioctl(fd,MEMGETINFO,&info);
if(n<0){
printf("Error:get info error\n");
close(fd);
}
//erase
erase_info.start=0;
erase_info.length=info.erasesize;
n=ioctl(fd, MEMERASE, &erase_info);
if(n<0){
printf("erase error\n");
close(fd);
return -1;
}
of=lseek(fd,0,SEEK_SET);
printf("lseek offset: %d\n",of);
if(of == -1)
{
printf("ERROR lseek file fail!\n");
return -1;
}
printf("sizeof(src)=%d\n",srcSize);
n=write(fd,buffer,2048);
sync();
printf("write size=%d\n",n);
if(n != pageSize){
printf("ERROR write fail\n");
close(fd);
return -1;
}
//check the result
buffer = NULL;
buffer = (char*)malloc(pageSize);
if(buffer == NULL)
{
printf("ERROR buffer malloc fail!");
return -1;
}
of=lseek(fd,0,SEEK_SET);
printf("lseek offset: %d\n",of);
if(of == -1)
{
printf("ERROR lseek file fail!\n");
return -1;
}
memset(buffer,0,pageSize);
n=read(fd,buffer,pageSize);
if(n != pageSize){
printf("ERROR read fail\n");
close(fd);
return -1;
}
printf("result:%s",buffer);
close(fd);
return 0;
}
///////////////////////////////////////////////////////////////////////////////////

挺好的,支持下

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top