Thread: help on boot sector plzzzzz

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    9

    help on boot sector plzzzzz

    here is the code:
    Code:
    /* ===== SharedDisk.h ===== */
    
    #ifndef __SHAREDISK__
    #define __SHAREDISK__
    
    
    typedef struct
    {
    	unsigned short	Jump;
    		char 	Manufacturer[ 9 ];
    	unsigned short 	BytesPerSector;
    	unsigned char 	SectorsPerCluster;
    	unsigned short 	ReservedSectors;
    	unsigned char	FATs;
    	unsigned short	RootDirectoryEntries;
    	unsigned short	LogicalSectors;
    	unsigned char	MediaDescriptorByte;
    	unsigned short	SectorsPerFAT;
    	unsigned short	SectorsPerTrack;
    	unsigned short	Surfaces;
    	unsigned short	HiddenSectors;
    } Disk;
    
    #endif
    
    /* ===== FDisk.c ===== */
    
    #include <stdio.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <ctype.h>
    #include <string.h>
    #include "SharedDisk.h"
    #include "FDisk.h"
    
    
    int DiskOpen( Disk *theDisk )
    {
    	int 	fd;
    	int 	len;
    	char	buffer[SIZE]; 
    
    	
    	// ===== Initialize theDisk ===== //
    	memset( theDisk, '\0', sizeof( Disk ) );
    	
    	// ===== Open the file ===== //
    	fd = open ( "disk.img", O_RDONLY );
    	
    	// ===== Read boot sector information in the file to array buffer ===== //
        	len = read ( fd, buffer, 512 );
    
    	// ===== Check if it reads file sucessful ===== //
    	if ( len == -1 )
    	{
    		printf( "Unable to read file.\n" );
    	 }
    
    
    ===== i have problem from here it is not correct ===== 
    ===== i do not know how to put each inforamtion to each member of the struct Disk. =====
    
    	// ===== Copy the information to each member of the Disk ===== //
    	memcpy( theDisk->Jump, &buffer[ 0 ], 3 );
    	memcpy( theDisk->Manufacturer, &buffer[ 3 ], 8 );
    	memcpy( theDisk->BytesPerSector, &buffer[ 11 ], 2 );
    	memcpy( theDisk->SectorsPerCluster, &buffer[ 13 ], 1 );
    	memcpy( theDisk->ReservedSectors, &buffer[ 14 ], 2 );
    	memcpy( theDisk->FATs, &buffer[ 16 ], 1 );
    	memcpy( theDisk->RootDirectoryEntries, &buffer[ 17 ], 2 );
    	memcpy( theDisk->LogicalSectors, &buffer[ 19 ], 2 );
    	memcpy( theDisk->MediaDescriptorByte, &buffer[ 21 ], 1 );
    	memcpy( theDisk->SectorsPerFAT, &buffer[ 22 ], 2 );
    	memcpy( theDisk->SectorsPerTrack, &buffer[ 24 ], 2 );
    	memcpy( theDisk->Surfaces, &buffer[ 26 ], 2 );
    	memcpy( theDisk->HiddenSectors, &buffer[ 28 ], 2 );
    
    	return	fd;
    }
    after i have read all information on the boot sector to buffer,
    how do i put each information to each member of the struct Disk?

    i have heard that i have to deal with offset bytes to get each information like shifting byte left.
    i do not know how.

    can some one tell me how?

    if u need more information, please tell me.
    i will provide more information.

    thx a lot

  2. #2
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    How was the data in "disk.img" written, as a structure identical to "Disk"? If yes, you don't need to:
    Code:
    len = read ( fd, buffer, 512 );
    Instead:
    Code:
    len = read ( fd, theDisk, sizeof(Disk) );
    >fd = open ( "disk.img", O_RDONLY );
    One more thing, what if open() failed?

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    9
    the data in "disk.img" is written, as a structure identical to "Disk".
    this file is made from the floopy disk.
    floppy disks have a logical sector size of 512 bytes.
    that is why 512 is there.
    buffer only stores one sector at a time.
    read the Boot Sector and fill in the appropriate structures in theDisk.
    display a human readable summary of the disk information read from the boot sector on stdout.

  4. #4
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    Then the best solution is:
    Code:
    len = read ( fd, theDisk, sizeof(Disk) );
    But ofcourse, make sure to allocate enough memory for 'theDisk'. And keep reading that way until 512 bytes have been read (512 / sizeof(Disk) times)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vista/XP Dual Boot Help
    By alpha in forum Tech Board
    Replies: 1
    Last Post: 06-29-2007, 03:50 AM
  2. freebsd and redhat dual boot.
    By xddxogm3 in forum Linux Programming
    Replies: 1
    Last Post: 05-09-2004, 06:06 PM
  3. Boot Disks
    By Jperensky in forum C++ Programming
    Replies: 2
    Last Post: 06-19-2003, 04:37 AM
  4. Linux and Windows Duel Boot
    By The15th in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-26-2002, 04:59 AM
  5. CD Boot - Need Help
    By (TNT) in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-19-2001, 12:29 PM