Linux sector by sector read and write block device

  

This article describes the sector read and write block device under Linux (example TF card), the actual application is on the Android system, the main methods are as follows: 1, find the sdcard mount point In android2.1 system should be /dev/block/mmcblk0p1, or /dev/block/mmcblk0, instead of /sdcard or /mnt/sdcard2, modify permissions, the default is no permission to read and write block devices by sector (The open handle is empty), connect the phone to the computer, enter the phone through the adb shell, set the /dev/block/mmcblk0p1 permission to 777, the command is as follows: chmod 777 -R /dev/block/mmcblk0p13, write the read and write program, The sample code is as follows int fd = open("/dev/block/mmcblk0", O_RDWR| O_DIRECT, 606); 4, compile and install to the phone, you can directly operate on the sector. Attached to the read and write sector program (the first parameter is the file handle, the second is the read and write buffer): 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);} The above send and receive buffers must conform to memory alignment: char* iobuf = NULL;

iobuf = memalign( Getpagesize(), getpagesize());//2.6version = 512;

Copyright © Windows knowledge All Rights Reserved