Reading Boot Parameters
Reading Boot Parameters
The Boot sector on the floppy disk contains two things, namely, boot parameters and the
disk bootstrap program. The purpose of the disk bootstrap program is to load DOS files from
the disk into memory. Whether the disk bootstrap program would be used or not depends on
whether the disk is being used for booting the computer or not. The boot parameters are
used by DOS at the time a read or write operation is performed on the disk. This is because
the boot parameters contain the vital information about how the disk has been formatted.
From format to format the values of the boot parameters may change, but their order remains
same. The following program display boot parameters of floppy.
# include "dos.h"
# include "stdio.h"
main( )
{
struct boot
{
unsigned char code[3] ;
unsigned char system_id[8] ;
int bytes_per_sec ;
char sec_per_clus ;
int res_sec ;
char fat_copies ;
int root_dir_entry ;
unsigned int no_sects ;
unsigned char format_id ;
int sec_per_fat ;
int sec_per_trk ;
int no_sides ;
int no_sp_res_sect ;
unsigned char rest_code[482] ;
};
struct boot b ;
char temp[4] ;
int val, drive ;
val = absread ( 0, 1, 0, &b ) ;
if ( val == -1 )
{
printf ( "Disk read Error...bad sector\n" ) ;
exit ( 1 ) ;
}
clrscr ( ) ;
printf ( "System id %s\n", b.system_id ) ;
printf ( "Bytes per sector %d\n", b.bytes_per_sec ) ;
printf ( "Sectors per cluster %d\n", b.sec_per_clus ) ;
printf ( "Reserved sectors %d\n", b.res_sec ) ;
printf ( "FAT copies %d\n", b.fat_copies ) ;
printf ( "Root directory entries %d\n", b.root_dir_entry ) ;
printf ( "No. of sectors on disk %u\n", b.no_sects ) ;
printf ( "Format id %X\n", b.format_id ) ;
printf ( "Sectors per FAT %d\n", b.sec_per_fat ) ;
printf ( "Sectors per track %d\n", b.sec_per_trk ) ;
printf ( "No. of sides %d\n", b.no_sides ) ;
printf ( "No. of reserved sectors %d\n", b.no_sp_res_sect ) ;
}
PT exists only on hard disk. This is because today we have hard disks which range in
capacity from 20 mb to 300 mb. So huge is this capacity that it would be foolish to use the
entire hard disk only for DOS. Hence it is divided into several logical parts called partitions.
One partition may contain DOS whereas the other might contain Xenix an so on. The hard
disk is partitioned using DOS's FDISK command. While partitioning the hard disk FDISK
stores the details about where one partition ends and the next begins, which is the bootable
partition etc. in the first physical sector ( side 0, track 0, sector 1 ). It also stores a program
called Master Boot Program in the PT. Thus a PT consists of data part and the code part.
The following figure 1 shows how the PT is logically divided.
Figure 1
The data part begins at 447th byte. The last two bytes in the PT are always 0x55, 0xAA. The
data part is 64 bytes long and is further divided into four parts of 16 bytes each. Each 16
bytes chunk consists of information about a partition on the hard disk. Hence there can be
maximum four partitions on the hard disk. The break-up of 16 bytes is given below.
Byte Meaning
0 Boot indicator. Contains 0x80 for the active partition, 0 for all others. Only
one partition can be active at a time.
1 Side where the partition begins.
2 The low six bits are the sector where the partition begins. The high two bits
are the two high bits of the track where the partition begins.
3 Low order eight bits of the track where the partition begins.
4 Partition type indicator. The contents os this byte indicates the type of the partition.
Following values may exist.
0 - Unused partition
1 - DOS partition that uses a 16-bit FAT
2 - Xenix partition
4 - DOS partition that uses a 16-bit FAT
5 - Extended Partition
6 - Huge Partition
5 Side where the partition ends.
6 Low order six bits are the sector where the partition ends. The high two bits are the
high two bits of the ending track number.
7 Low eight bits of the track number where the partition ends.
8-1 Double word containing the number of sectors preceeding the partition.
Low order word is stored first.
12-15 Double word containing the number of sectors in the partition. Low order word is
stored first.
let us put the theorey into a program which analyses the PT. Here it is.
# include "bios.h"
struct partition
{
unsigned char bootable ; unsigned char start_side ;
unsigned int start_sec_cyl ; unsigned char parttype ;
unsigned char end_side ; unsigned int end_sec_cyl ;
unsigned long part_beg ; unsigned long plen ;
};
struct part
{
unsigned char master_boot[446] ;
struct partition pt[4] ;
int lasttwo ;
};
struct part p ;
main( )
{
unsigned int s_sec, s_trk, e_sec, e_trk, i, t1, t2 ;
char type[20], boot[5] ;
biosdisk ( 2, 0x80, 0, 0, 1, 1, &p ) ;
printf("\nPart. Boot Starting locationEnding Location Relative Number of");
printf("\nType Side Cylinder Sector Side Cylinder Sector Sectors Sectors\n");
for ( i = 0 ; i <= 3 ; i++ )
{
if ( p.pt[i].bootable == 0x80 )
strcpy ( boot, "Yes" ) ;
else
strcpy ( boot, "No" ) ;
switch ( p.pt[i].parttype )
{
case 0 :
strcpy ( type, "Unused" ) ; break ;
case 1 :
strcpy ( type, "12-Bit" ) ; break ;
case 2 :
strcpy ( type, "Xenix" ) ; break ;
case 4 :
strcpy ( type, "16-Bit" ) ; break ;
case 5 :
strcpy ( type, "Extended" ) ; break ;
case 6 :
strcpy ( type, "Huge" ) ; break ;
default :
strcpy ( type, "Unknown" ) ; break ;
}
To amalgamate the disparate bodies in the data part of the PT what we need is a structure.
So first we create a structure struct partition, 16 bytes long. But as we know the PT contains
4 such data blocks, along with 446 bytes code and 0x55, 0xAA at the end. To combine these
we need another structure - hence the definition struct part. Having created the space to
store the contents of PT we make a call to biosdisk( ), to read the contents of side 0, track 0,
sector 1. Once the contents are read we enter into a for loop within which we separate out
the information about each partition and display it on the screen.