Thursday, February 19, 2015

Learning C Functions :_bios_disk ( Dos Only )

C Programming :
Function :
_bios_disk

Syntax :

#include <bios.h>
unsigned _bios_disk(unsigned cmd, struct diskinfo_t *dinfo);

Description :

_bios_disk uses interrupt 0x13 to issue disk operations directly to the BIOS . The cmd argument specifies the operation to perform, and dinfo points
to a diskinfo_t structure that contains the remaining parameters required by the operation .
The diskinfo_t structure (defined in bios.h) has the following format :

struct diskinfo_t {
    unsigned drive, head, track, sector, nsectors;
    void far *buffer;
};

drive is a number that specifies which disk drive is to be used: 0 for the first floppy disk drive, 1 for the second floppy disk drive, 2 for the third, and
so on. For hard disk drives, a drive value of 0x80 specifies the first drive, 0x81 specifies the second, 0x82 the third, and so forth.
For hard disks, the physical drive is specified, not the disk partition. If necessary, the application program must interpret the partition table
information itself.
Depending on the value of cmd, the other parameters in the diskinfo_t structure may or may not be needed.
The possible values for cmd (defined in bios.h) are the following :

_DISK_RESET    Resets disk system, forcing the drive controller to do a hard reset. All diskinfo_t parameters are ignored .
_DISK_STATUS    Returns the status of the last disk operation. All diskinfo_t parameters are ignored.
_DISK_READ    Reads one or more disk sectors into memory. The starting sector to read is given by head, track, and sector. The number of
sectors is given by nsectors. The data is read, 512 bytes per sector, into buffer. If the operation is successful, the high byte of
the return value will be 0 and the low byte will contain the number of sectors. If an error occurred , the high byte of the return
value will have one of the following values :

     0x01    Bad command .
     0x02    Address mark not found .
     0x03    Attempt to write to write-protected disk .
     0x04    Sector not found .
     0x05    Reset failed (hard disk) .
     0x06    Disk changed since last operation .
     0x07    Drive parameter activity failed .
     0x08    Direct memory access (DMA) overrun .
     0x09    Attempt to perform DMA across 64K boundary .
     0x0A    Bad sector detected .
     0x0B    Bad track detected .
     0x0C    Unsupported track .
     0x10    Bad CRC/ECC on disk read .
     0x11    CRC/ECC corrected data error .
     0x20    Controller has failed .
     0x40    Seek operation failed .
     0x80    Attachment failed to respond .
     0xAA    Drive not ready (hard disk only).
     0xBB    Undefined error occurred (hard disk only).
     0xCC    Write fault occurred .
     0xE0    Status error .
     0xFF    Sense operation failed .
     0x11    is not an error because the data is correct. The value is returned to give the application an opportunity to decide for tself .

_DISK_WRITE    Writes one or more disk sectors from memory. The starting sector to write is given by head, track, and sector . The number of
sectors is given by  nsectors . The data is written, 512 bytes per sector, from buffer. See _DISK_READ (above) for a description of the return value.
_DISK_VERIFY    Verifies one or more sectors. The starting sector is given by head, track, and sector. The number of sectors is given by nsectors.
See _DISK_READ (above) for a description of the return value.
_DISK_FORMAT    Formats a track. The track is specified by head and track. buffer points to a table of sector headers to be written on the named track. See the Technical Reference Manual for the IBM PC for a description of this table and the format operation.

Return Value

_bios_disk returns the value of the AX register set by the INT 0x13 BIOS call.


Example :

/*  _bios_disk example  */

#include <bios.h>
#include <stdio.h>

int main(void)
{
  struct diskinfo_t dinfo;
  int result;
  static char dbuf[512];

  dinfo.drive =  0;        /*  drive number for A: */
  dinfo.head  =  0;       /*  disk head number */
  dinfo.track =  0;        /*  track number */
  dinfo.sector  =  1;     /*  sector number */
  dinfo.nsectors =  1;   /*  sector count */
  dinfo.buffer = dbuf;  /*  data buffer */

  printf("Attempting to read from drive A:\n");
  result = _bios_disk(_DISK_READ, &dinfo);
  if ((result & 0xff00) == 0)
  {
    printf("Disk read from A: successful.\n");
    printf("First three bytes read are 0x%02x 0x%02x 0x%02x\n",
      dbuf[0] & 0xff, dbuf[1] & 0xff, dbuf[2] & 0xff);
  }
  else
    printf("Cannot read drive A, status = 0x%02x\n", result);

  return 0;
}

No comments:

Post a Comment