Linux 直接读写磁盘物理地址

EMMC 磁盘物理地址读数据

#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>


/**
 * ./lseek /dev/sdb
 */
int main(int argc, char **argv)
{
	int fd;
	char str[100] = {0};

	if (argc != 2) {
		printf("Usage: %s </dev/sdb or other>\n", argv[0]);
		return -1;
	}

	fd = open(argv[1], O_RDONLY); /* 直接读磁盘,不要读分区 */
	if(fd < 0){
		printf("open %s err!\n", argv[1]);
		return -1;
	}

	/* offset: 0x1B8 = 0x1B0 + 8 */
	lseek(fd, 0x1B8, SEEK_SET); /* 设置读取的位置 */

	read(fd, str, 4);
	str[5] = '\0';

	printf("File content: %s \n", str);

	close(fd);

	return 0;
}

EMMC 磁盘物理地址写数据

操作思路和上面一样,使用 write 函数


版权声明:本文为qq_28837389原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。