繁体   English   中英

读取FAT32文件系统的引导扇区

[英]Reading boot sector of a FAT32 file system

我正在编写一个程序,我需要访问引导区中有关已安装的FAT32文件系统的一些信息。

这是我所做的,充分评论。

int main (void) {
    unsigned short *i;                  //2 byte unsigned integer pointer
    char tmp[1024];                     //buffer
    int fd;                             //file descriptor from opening device
    fd =  open("/dev/sdf", O_RDONLY);   //open the device file
    lseek(fd, 14, SEEK_SET);            //set offset to 14 (0x0E), i.e. storing "no. of reserved sectors" of the FAT32 system
    read(fd, tmp, 2);                   //read off 2 bytes, "no. of reserved sectors" is represented by 2 bytes
    i = &tmp;                           //point j at those 2 bytes
    printf("*j: %d\n", *j);             //print *j out
    close(fd);                          //close device
    return 0;
}

* i的输出为38,这是胡说八道。 我用mkfs.vfat格式化了文件系统。 我将“保留扇区数”设置为32。

我试过的

  • i =(unsigned short *)&tmp,进行强制转换,这在我编译时消除了警告,但无济于事

  • read(fd,tmp,512),将整个引导扇区(512字节)加载到tmp中,然后从缓冲区中读取,但没有帮助,尽管结果仍然为38。

  • 摆弄偏移量,即在我弄错索引的情况下将14更改为13或15。 它分别输出13的9744和15的512,所以不起作用。

我不确定我是否做对了。 有人可以指出正确的方向吗?

提前致谢,

菲勒汀。

尝试运行:

$ dd if=/dev/sdf of=/tmp/x.bin bs=512 count=1

接着:

$ hd /tmp/x.bin

要么

$ od -tx2 /tmp/x.bin

并发布第一行。

您的fattools可能会自己添加6个额外的保留扇区。 然后在显示数据之前先减去它们。

unsigned short *i;                  //2 byte unsigned integer pointer
char tmp[1024];  
 [...] 
i = &tmp;                           //point j at those 2 bytes

tmpchar[]&tmp命令char** 再想一想,您不希望&在这里。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM