zoukankan      html  css  js  c++  java
  • Linux下按扇区读写块设备

    本文介绍Linux下按扇区读写块设备(示例TF卡),实际应用是在Android系统上,主要方法如下:

    1、找到sdcard的挂载点,在android2.1系统下应该为/dev/block/mmcblk0p1,或是/dev/block/mmcblk0,而不是/sdcard或/mnt/sdcard

    2、修改权限,默认是没有权限按扇区读写块设备的(下文打开句柄为空),连接手机到计算机,通过adb Shell进入手机,将/dev/block/mmcblk0p1权限设置成777,命令如下:

    chmod 777 -R /dev/block/mmcblk0p1

    3、编写读写程序,示例代码如下

    int     fd = open("/dev/block/mmcblk0", O_RDWR|O_DIRECT,606);

    4、编译并安装到手机上,就可以对扇区直接操作。

    附读写扇区程序(参数第一个是文件句柄,第二个是读写缓冲区):

    int WriteSectors(int fd, char *p){
    //lseek(fd, 0 ,SEEK_SET);
    //if (lseek(fd, 1024, SEEK_CUR) == -1 ){
    // return (-1);//seek failed
    //}

    if (lseek(fd, 1024 ,SEEK_SET) == -1 ){
    return (-1);//seek failed
    }

    return write(fd, p, 512);
    }

    int ReadSectors(int fd, char *p){

    //lseek(fd, 0 ,SEEK_SET);
    //if (lseek(fd, 1024, SEEK_CUR) == -1 ){
    // return (-1);//seek failed
    //}

    if (lseek(fd, 1024 ,SEEK_SET) == -1 ){
    return (-1);//seek failed
    }

    return read(fd, p, 512);
    }
    上面的发送接收缓冲区必须符合内存对齐:

    char* iobuf = NULL;

    iobuf = memalign(getpagesize(),getpagesize());//2.6version = 512;
    ---------------------
    作者:xrdeng
    来源:CSDN
    原文:https://blog.csdn.net/xuerongdeng/article/details/78289132
    版权声明:本文为博主原创文章,转载请附上博文链接!

  • 相关阅读:
    WPF之感触
    C# WinForm 给DataTable中指定位置添加列
    MyEclipse 8.6 download 官方下载地址
    将博客搬至CSDN
    Building Microservices with Spring Cloud
    Building Microservices with Spring Cloud
    Building Microservices with Spring Cloud
    Building Microservices with Spring Cloud
    Building Microservices with Spring Cloud
    Building Microservices with Spring Cloud
  • 原文地址:https://www.cnblogs.com/Ph-one/p/10689705.html
Copyright © 2011-2022 走看看