FAT FS BPB problem!

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
codemastersnake
Member
Member
Posts: 148
Joined: Sun Nov 07, 2004 12:00 am
Contact:

FAT FS BPB problem!

Post by codemastersnake »

Hi All!

Well at first I thought that my FDC driver is not working, But now I have figured out the problem. It's with the FAT12 BPB struct.

Following is the code:

fat.h

Code: Select all

#include "kore\kmsgs.h"
#include "iostream.h"
#include "drivers\fdc\fdc.h"
#include "kore\kalloc.h"

#if !defined ( __FAT_H_ )
#define __FAT_H_

struct fat_bpb
{
	unsigned char jmp[3];
	unsigned char oem_name[8];
	unsigned int bytes_per_sector;
	unsigned char sectors_per_cluster;
	unsigned int num_reserved_sectors;
	unsigned char num_fats;
	unsigned int num_root_directories;
	unsigned int total_sectors;
	unsigned char media_desc;
	unsigned int num_sectors_per_fat_16;
	unsigned int sectors_per_track;
	unsigned int num_heads;
	unsigned long num_hidden_sectors;
	unsigned long num_sectors_32;
	unsigned char logical_drive_num;
	unsigned char reserved;
	unsigned char extended_signature;
	unsigned long serial_num;
	unsigned char volume_lable[12];
	unsigned char fs_type[8];
	unsigned char boot_code[448];
	unsigned int magic_num;
}__attribute__ ((packed));
	

extern const char * FAT_ID;

class FAT
{
 private:
	fat_bpb fat12_boot_info;
 public:
 	FAT();
	~FAT();
	int init();
};

extern FAT fat;

#endif


fat.cpp

Code: Select all

#include "drivers\FAT\FAT.h"

#if !defined ( __FAT_CPP_ )
#define __FAT_CPP_

const char * FAT_ID = "FAT";

FAT::FAT()
{
 ;
}

FAT::~FAT()
{
 ;
}

int FAT::init()
{
 cout<<endl;
 cout<<"FAT: ";
 cout<<"\n\tNot Implemented";
 unsigned char * ch = (unsigned char *) &fat12_boot_info;
  fdc.read_block(0, (unsigned char *) &fat12_boot_info);
 printf("\n0x%x\n", &fat12_boot_info);
 printf("0x%x\n", ch);
 printf("%d\n", sizeof(fat12_boot_info));
 printf("%s\n", fat12_boot_info.volume_lable);
 unsigned char che[512];
 unsigned char * chp = (unsigned char *) &che;
 fdc.read_block(0, chp);
 int ctr = 4;
 while(ctr !=9)
 {
  printf("%c", che[ctr]);
  ctr++;
 }
 //fdc.read_block(0, (unsigned char *)&fat12_boot_info);
 /*printf("%s", fat12_boot_info.oem_name);
 printf("\n%d", fat12_boot_info.sec_size);
 printf("\n%s", fat12_boot_info.volume_label);*/
 return KMSG_FALIURE;
}

FAT fat;

#endif

I have tried to get the info using an array as above and it showed me the correct result, but i am not able to store the values in the structure. I have tried manier time and it didn't work. more over my structure shows that it's of size greater than 512 bytes. I have even written it according to the MS specs again.... still the same problem.

can anyone help?
Attachments
untitled.GIF
untitled.GIF (19.15 KiB) Viewed 1152 times
User avatar
xyjamepa
Member
Member
Posts: 397
Joined: Fri Sep 29, 2006 8:59 am

Post by xyjamepa »

here's mine :

Code: Select all

struct fat_bootsector_t			
{
        byte  jump[3];                  /* 16-bit JMP to boot code, or 8-bit JMP + NOP */
        byte  oem_id[8];                /* e.g. 'MSWIN4.0' */
	word  bytes_per_sector;	        /* usu. =512 */
        byte  sectors_per_cluster;
	word  num_boot_sectors;	        /* usu. =1 */
        byte  num_fats;                 /* usu. =2 */
	word  num_root_dir_ents;
        word  total_sectors;            /* 16-bit; 0 if num sectors > 65535 */
        byte  media_ID_byte;            /* usu. =0F0h */
	word  sectors_per_fat;
	word  sectors_per_track;
	word  heads;
	qword hidden_sectors;	        /* =LBA partition start */
        qword total_sectors_large;      /* 32-bit; 0 if num sectors < 65536 */
        byte  boot_code[474];
        byte  magic[2];                 /* 55h, 0AAh */
};
it's working with me.
The man who follows the crowd will usually get no further than the crowd.
The man who walks alone is likely to find himself in places
no one has ever been before.
Ready4Dis
Member
Member
Posts: 571
Joined: Sat Nov 18, 2006 9:11 am

Post by Ready4Dis »

Do yourself a favor, and check the size of the fat_bpb struct, i think you'll find that it's not 512 bytes like it should be. Please, instead of using char, int, and long as data types, start using something that actually means it's size, because if you decide to change to x86-64, it will break a LOT of your code that depends on a long or int being a specific size. That said, I see you are using int where a word should be, gcc, msvc, and most other 32-bit compilers have 32-bit integers, not 16. Change int to short, and see what happens for testing, but in my OS I have a single include file of defines, I use s8, s16, s32, s64, u8, u16, u32, u64 (pretty self explanatory), this way I know the size of the variable I am using, and if I switch compilers I modify my define's to be accurate on the new compiler. Makes these 'need to be a specific size structure' problems go away much easier.
User avatar
codemastersnake
Member
Member
Posts: 148
Joined: Sun Nov 07, 2004 12:00 am
Contact:

Post by codemastersnake »

Thanks for the suggestion Ready4Dis. I think you are right about the size of int. I'll test it and see if it works or not.

By the way the size is greater that 512 as shown in the posted image.
Ready4Dis
Member
Member
Posts: 571
Joined: Sat Nov 18, 2006 9:11 am

Post by Ready4Dis »

Snake wrote:Thanks for the suggestion Ready4Dis. I think you are right about the size of int. I'll test it and see if it works or not.

By the way the size is greater that 512 as shown in the posted image.
Yes, I realized this afterwards, but it still needs to be fixed, I Don't think it will solve your problem completely, because the oem_name should still appear correctly (although, be careful displaying it as you are, because it is not required to be null terminated, typically i have a function that displays a string up to a maximum # of characters OR if it hits a null to avoid these issues). I would write try to verify that the actual data on the floppy is correct, then try it in the OS and see if it works. I have my own FAT formatting utility, which can also load up a fat partition and display it's contents, so I can verify a disk image before testing in my OS to make sure it's holding the correct data. If you can't get your code to work in your OS, try writing a utility that isn't part of your OS, if it loads/displays the correct information, then you know it's your floppy reading code, if it still shows garbage, then you know the disk may have garbage being written to it.
User avatar
codemastersnake
Member
Member
Posts: 148
Joined: Sun Nov 07, 2004 12:00 am
Contact:

Post by codemastersnake »

I have corrected the data types. Thats for telling me.....

Problem is now solved!
Post Reply