home *** CD-ROM | disk | FTP | other *** search
/ Chip 2000 May / Chip_2000-05_cd2.bin / dosutils / ext2tool / src / diskio.c next >
C/C++ Source or Header  |  1995-05-10  |  2KB  |  61 lines

  1. /***************************************************************************
  2.  * diskio.c - Basic disk I/O routines
  3.  *
  4.  * Copyright (C) 1995 Claus Tondering, ct@login.dknet.dk
  5.  * This file may be redistributed under the terms of the GNU Public License.
  6.  ***************************************************************************/
  7.  
  8. #include <bios.h>
  9. #include <sys/types.h>
  10. #include "diskio.h"
  11.  
  12. static int cyls, heads, sects;
  13.  
  14. /**********************************************************************
  15.  * getdiskparm finds the physical disk paramters
  16.  **********************************************************************/
  17.  
  18. static int
  19. getdiskparm(int disk)
  20. {
  21.     int i;
  22.     u_char buf[4];
  23.  
  24.     i=biosdisk(8,disk,0,0,0,0,&buf);
  25.  
  26.     if (i!=0)
  27.         return i;
  28.  
  29.     sects = buf[0] & 0x3f;
  30.     cyls = ((buf[0] & 0xc0 ) << 2) + buf[1];
  31.     heads = buf[3] + 1;
  32.     return 0;
  33. }
  34.  
  35. /**********************************************************************
  36.  * readdisk read a number of 512 byte sectors from the physical disk
  37.  **********************************************************************/
  38.  
  39. int
  40. readdisk(int disk, int start, int nsect, void *buf)
  41. {
  42.     int cyl, head, sect, err;
  43.  
  44.     if (sects==0) {
  45.         err=getdiskparm(disk);
  46.         if (err) return err;
  47.     }
  48.  
  49.     sect = start % sects + 1;
  50.     start /= sects;
  51.     head = start % heads;
  52.     cyl = start / heads;
  53.  
  54.     err = biosdisk(2, disk, head, cyl, sect, nsect, buf);
  55.     if (err==6)
  56.         /* Disk changed - try once more */
  57.         err = biosdisk(2, disk, head, cyl, sect, nsect, buf);
  58.  
  59.     return err;
  60. }
  61.