从FAT12中查找免费的软盘空间

时间:2015-11-28 21:25:08

标签: c operating-system fat

我正在尝试通过读取磁盘映像的十六进制值来确定FAT12软盘文件系统上剩余的可用空间量。我的计划是计算值为0x000(空白空间)的FAT条目数。我不明白如何将FAT条目变成可读格式。

我的代码:

#include<stdio.h>
#include<stdlib.h>

   int freeSpace(FILE *fp){
     int fat_entry;

     fseek(fp, 512L, SEEK_SET); //start of first FAT
     fread(&fat_entry,1,3,fp);

     printf("THREE BYTES: %X \n", fat_entry);
     //Finish calculating free space
     return 0;
   }

   int main(int argc, char** argv)
   { 
     FILE *fp;
     int free_space;

     if ((fp=fopen(argv[1],"rb")))
    { 
      printf("Successfully opened the image file.\n");

      free_space = freeSpace(fp) 
      printf("Free Space: %d\n", free_space);
    }
    else
      printf("Fail to open the image file.\n");

    fclose(fp);
    return 0;
  }

查看我的十六进制编辑器,从字节偏移512字节开始的前三个字节是:FO FF FF

printf("THREE BYTES: %X \n", fat_entry);的输出为:FFFFF0

根据脂肪规格,我已经阅读了我的理解,如果我有3个字节uv wx yz相应的12位FAT12条目将是xuv yzw。所以我想把FFFFF0变成FF0 FFF格式,这样我就可以检查是否有一个条目是0x000。

我对如何读取正确的FAT12值感到非常困惑。我知道这与小的endianess和一些技巧有关,因为我同时加载2个FAT12值以获得偶数个字节,但我无法弄清楚从哪里开始。

1 个答案:

答案 0 :(得分:1)

以下函数获取下一个集群。该功能来自我的档案。

word nextclust12 (cluster, BPB)
struct BPB *BPB;    // BIOS parameter block
word cluster;       // curent cluster: get next one
{
    word *FAT, index, bytenum, j;
    dword bloknum;

    bytenum= cluster + cluster/2;               /* multiply with 1.5 */
    bloknum= bytenum / (3*512);                 /* in which 3-group is block */
    index=   bytenum % (3*512);                 /* position in 3-group */
                                                /* read part of FAT */
    if (!dosread (FAT_buf, BPB->disk, bloknum + BPB->reserved_bloks, 3))
        return (BADBLOK12);

    FAT= (word *) &FAT_buf[index];      /* get a word from that place */
    if (even (cluster))
        return ( *FAT & 0x0fff);        /* set high 4 bits to zero */
    else
        return ( *FAT >> 4);            /* shift FAT-entry to right */
}

代码不完整(需要大量标题和其他C文件),因此请将其视为伪代码。

相关问题