FAT12簇链就停止了,不知道发生了什么事情

问题描述 投票:0回答:1

我正在为我的操作系统开发 FAT12 驱动程序(您可以在这里找到驱动程序),但我遇到了一个非常奇怪的障碍,我不知道是我的代码还是驱动程序。

我有一个名为 long.txt 的文件,其中包含大约 1024 字节的内容,它的目的是测试一系列簇链。然而,代码在一个簇后停止,并表示下一个簇是 0xFFF,它大于 0xFF8,因此 EOF。

这是导致问题的代码片段:

int fatReadInternal(fsNode_t *file, uint8_t *buffer, uint32_t length) {
    if (file) {
        // Calculate and read in the starting physical sector
        uint16_t cluster = file->impl;
        uint32_t sector = ((cluster - 2) * drive->bpb->sectorsPerCluster) + drive->firstDataSector;

        uint8_t *sector_buffer;
        ideReadSectors(drive->driveNum, 1, (uint32_t)sector, sector_buffer);
        


        // Copy the sector buffer to the output buffer
        memcpy(buffer, sector_buffer, length);

        // Locate the File Allocation Table sector
        uint32_t fatOffset = cluster + (cluster / 2);
        uint32_t fatSector = drive->firstFatSector + (fatOffset / 512);
        uint32_t entryOffset = fatOffset % 512;

        // Read the FATs
        ideReadSectors(drive->driveNum, 1, fatSector, (uint8_t*)FAT);
        ideReadSectors(drive->driveNum, 1, fatSector + 1, (uint8_t*)FAT + 512);

         

        // Read entry for the next cluster
        uint16_t nextCluster = *(uint16_t*)&FAT[entryOffset];

        // Convert the cluster
        nextCluster = (cluster & 1) ? nextCluster >> 4 : nextCluster & 0xFFF;

        serialPrintf("The current cluster is 0x%x, the next cluster is 0x%x after modification.\n", cluster, nextCluster);
        

        // Test for EOF
        if (nextCluster >= 0xFF8) {
            return EOF;
        }

        // Test for file corruption
        if (nextCluster == 0) {
            return EOF;
        }

        // Set the next cluster
        file->impl = nextCluster;
    }

    return 0;
}

注意:它应该被多次调用。

它输出当前簇是

0xD
,下一个簇从磁盘读取(尚未转换)是0xFF0,如果条目偏移量是0x13(也应该计算),那么(通过查看十六进制编辑器)它是正确的正确)。

问题是转换后这是 0xFFF,链中的最后一个簇。手动计算和检查十六进制编辑器也是如此。我真的很困惑。

FAT 驱动程序可以读取文件的前 512 个字节,但随后它只返回 EOF。我在十六进制编辑器中检查了下一个簇实际上在哪里,它就在第一个簇之后。

我不确定这是我的代码中的问题还是其他问题,但 Linux 似乎可以很好地读取该文件。

filesystems osdev fat
1个回答
0
投票

乍一看,下一个集群阅读对我来说似乎很好。然而,有两个相关的明显问题:

  1. 该函数假设每个簇有一个扇区 - 它读取单个扇区,然后移动到下一个簇。这可能是您的问题 - 如果您的图像格式为 >=1kb 簇大小怎么办?那么您的 1 kb 文件将存储在单个簇中,并且预期为 0xFFF。
  2. 在类似的派系中,它假设总是会调用它来读取整个集群/扇区。重复
    fsRead(size=1)
    不会像人们期望的那样起作用。
© www.soinside.com 2019 - 2024. All rights reserved.