简体   繁体   中英

Find Free Floppy Disk Space from FAT12

I'm trying to figure out how much remaining free space is on a FAT12 floppy disk file system by reading the hex values of a disk image. My plan is to count the number of FAT entries with the value 0x000 (empty space). I don't understand how to get the FAT entries into a readable format.

My code:

#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;
  }

Looking in my hex editor the first three bytes starting at the byte offset 512 bytes are: FO FF FF

The output of printf("THREE BYTES: %X \\n", fat_entry); is: FFFFF0

From the fat specifications I've read it is my understanding that if I have 3 bytes uv wx yz the corresponding 12bit FAT12 entries would be xuv yzw. So I want to get FFFFF0 into the form FF0 FFF so I can check if either entry is 0x000.

I've become horribly confused as to how to read the correct FAT12 value. I know it has something to do with little endianess and some trickery because I'm loading 2 FAT12 values at the same time to get an even number of bytes but I can't figure out where to go from here.

The following function gets the next cluster. The function is from my archive.

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 */
}

The code is not complete (needs lots of headers and other C files), so see it as pseudo code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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