Reading Sectors from a Floppy Image - Bit Shifting?

120 Views Asked by At

I am trying to read and display the disk geometry of a floppy in C. I was able to manage the first few entries (as far as I know of they're correct at least) such as:

  • Bytes per Sector
  • Sectors per Cluster
  • Reserved Sectors for the Boot Record
  • Number of FATS

My problem is I'm stuck at trying to figure out bit shifting for the rest of the geometry, which is what I was told to do in order to properly read in the values. I don't believe it's a problem with my code, but here is what I'm doing (SECTORSIZE is a const 512)::

void getSector(char *sector, int secNum, FILE *fp) 
{
   fseek(fp, (secNum*SECTORSIZE), SEEK_SET);
   fread(sector, sizeof(char), SECTORSIZE, fp);
}

FILE *fp;
char sector[512];
unsigned int fileSize;
int i;
int diroffset;
char name[8];
name[0] = 0;

fp = fopen("floppy", "r");

//sector 0 contains the disk geometry
getSector(sector, 0, fp);

printf("Bytes per Sector: %d\n", (((unsigned int)sector[0x0c]) << 8u) | (unsigned int)sector[11]);
printf("Sectors per Cluster: %d\n", ((unsigned int)sector[0x0d] ));
printf("Reserved Sectors for the Boot Record: %d\n", (((unsigned int)sector[0x0f]) << 8u) | (unsigned int)sector[0x0e]);
printf("Number of FATS: %d\n", ((unsigned int)sector[0x10]));
//printf("Max # of Root Directory Entries: %d\n", (((unsigned int)sector[0x12]) << 8u) | (unsigned int)sector[0x11]);
//printf("Number of Sectors: %d\n", (((unsigned int)sector[12])) | (unsigned int)sector[11]);
//printf("Sectors per FAT: %d\n", ((unsigned int)sector[13] << 8u));
//printf("Sectors per Track: %d\n", (((unsigned int)sector[12]) << 8u) | (unsigned int)sector[11]);
//printf("Number of Surfaces: %d\n", (((unsigned int)sector[12]) << 8u) | (unsigned int)sector[11]);

The commented out sections are the parts where I'm still working on them. I pretty much just copied the first line and just changed the string name to match. The next one after number of FATS is the max # of root directories which is where I'm running into trouble. I have a list of the hex representation for each geometry location but the bit shifting is what's throwing me off.

However I am also noticing that when I'm displaying filenames I'm displaying an extra name of random characters. Below is how I'm finding filenames:

diroffset = 0;

while(diroffset <= 512) {

    getSector(sector, 19, fp);
    // print name of the file
    if((void *) sector[0] != NULL)
        for(i = 0; i < 8; i++)
            name[i] = sector[diroffset + i];

    if(name[0] != 0) {
        printf("Filename: ");
        for(i = 0; i < 8; i++)
            printf("%c", name[i]);
        putchar('\n');
    }

    name[0] = 0;

    diroffset += 32;
}

It was my understanding that every 32 you would have a new filename, which it works as far as I know except displaying these characters as the last file found:

  • Filename: É·╬╩ 

I would like some more clarification on bit shifting in general, such as when and where to bit shift. I was trying to follow examples provided to me but maybe I'm just over complicating something and I'm not seeing it.

PS: If you're curious why I have so many unsigned int it's because my IDE complains otherwise when using bit operators

0

There are 0 best solutions below