home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1035 / diskio.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-28  |  4.9 KB  |  236 lines

  1. /***************************************************************
  2.  *
  3.  *    Low Level Read/Write routines to access the info on the disk
  4.  *    and the contents of the CMOS RAM chip
  5.  *
  6.  *    init(         unit )        initialize the specified drive
  7.  *    done(         unit )        close down the specified drive
  8.  *    read_sector(  unit, buf, cyl, head, sector)
  9.  *    write_sector( unit, buf, cyl, head, sector)
  10.  *    raw_io(       unit, buf, cyl, head, sector, op)
  11.  *
  12.  *    readCMOS( buffer )    read the contents of the CMOS RAM
  13.  *    writeCMOS( buffer )    write to the CMOS RAM
  14.  *
  15.  ***************************************************************
  16.  *
  17.  *    Revision Control Information
  18.  *
  19.  *    By:        $Author: root $
  20.  *            $Revision: 1.2 $
  21.  *    Last modified:    $Date: 87/09/16 02:12:24 $
  22.  *    $Source: /u/src/microport/newfdisk/test/RCS/getput.c,v $
  23.  *
  24.  *    Release state:    $State: Exp $
  25.  *
  26.  ***************************************************************
  27.  *
  28.  * Modification Log
  29.  * ----------------
  30.  * $Log:    getput.c,v $
  31.  * Revision 1.2  87/09/16  02:12:24  root
  32.  * maintenance update
  33.  * 
  34.  * Revision 1.1  88/04/07  23:11:20  root
  35.  * Initial revision
  36.  * 
  37.  ***************************************************************
  38.  */
  39.  
  40. #ifndef lint
  41.     static char rcsid[] =
  42.     "$Header: getput.c,v 1.2 87/09/16 02:12:24 root Exp $";
  43. #endif
  44.  
  45. #include <fcntl.h>
  46. #include <errno.h>
  47. #include <sys/param.h>
  48. #include <sys/types.h>
  49. #include <malloc.h>
  50. #include "cmos.h"        /* cmos layout */
  51.  
  52. /* #define PRODUCTION    /* Define to eliminate error messages on stderr */
  53. #define NOWRITE            /* Define to DISABLE absolute writes to the disk */
  54.  
  55. #ifdef PRODUCTION
  56. # ifdef NOWRITE
  57.     ERROR: Why do you want a production version which can not write to the disk?
  58. # endif
  59. #endif
  60.  
  61. static int fds[2] = { -1, -1 };    /* fds for both hard disk drives */
  62.  
  63. static char *unitname[] = {    /* entire disk... */
  64.     "/dev/rdsk/0s0",
  65.     "/dev/rdsk/1s0"
  66. };
  67.  
  68. /*
  69. **    Open a file which corrosponds to the entire disk.
  70. **    All other operations require that this be done before they call
  71. **    any disk I/O routines!
  72. */
  73.  
  74. int init( unit )
  75. int unit;
  76. {
  77.     char dummyblock[512];
  78.  
  79.     if (unit < 0 || unit > 1)
  80.     return ERROR;
  81.  
  82.     if ( fds[unit] != -1 )
  83.     closeunit( unit );
  84.  
  85.     if ((fds[unit] = open(unitname[unit], O_RDWR)) == -1) {
  86.         fprintf(stderr, "Can't open %s, errno=%d\n", unitname[unit], errno);
  87.         exit(errno);
  88.     }
  89.  
  90.     /*
  91.     **    WHY is this needed???
  92.     */
  93.  
  94.     read(fds[unit],dummyblock,512);    /* dummy read to force wnsweep */
  95.  
  96.     return OK;
  97. }
  98.  
  99. done( unit )
  100. int unit;
  101. {
  102.     close( fds[unit] );
  103.     fds[unit] = -1;
  104.     return OK;
  105. }
  106.  
  107. readCMOS( cmosp )
  108. struct cmos *cmosp;
  109. {
  110.     int    cmosfd;
  111.  
  112.     if ((cmosfd = open(CMOSDEV, 0)) == -1) {
  113.         perror("Opening cmos device");
  114.         exit(1);
  115.     }
  116.     if ( sizeof(struct cmos) != read(cmosfd, cmosp, sizeof(struct cmos))) {
  117.         perror("Reading cmos device");
  118.         exit(1);
  119.     }
  120.     close(cmosfd);
  121. }
  122.  
  123. writeCMOS( cmosp )
  124. struct cmos *cmosp;
  125. {
  126.     unsigned int  cksum, i;
  127.     unsigned char *c;
  128.     int    cmosfds[unit];
  129.  
  130.         /* calculate new checksum */
  131.     c = (unsigned char *) &cmos;
  132.     cksum = 0;
  133.     for(i=0x10; i<0x21; i++)
  134.     cksum += *(c+i);
  135.         /* Set new checksum */
  136.     *(c + 0x2e ) = cksum >> 8;    /* this is also known as cmos.cksum */
  137.     *(c + 0x2f ) = cksum ;
  138.  
  139.     if ((cmosfd = open(CMOSDEV, 1)) == -1) {
  140.         perror("Opening cmos device");
  141.         exit(1);
  142.     }
  143.     if ( sizeof(struct cmos) != write(cmosfd, cmosp, sizeof(struct cmos))) {
  144.         perror("writing cmos device");
  145.         exit(1);
  146.     }
  147.     close(cmosfd);
  148. }
  149.  
  150. /*
  151.  *  read or write a sector by disk address (cyl,head,sector)
  152.  */
  153.  
  154. read_sector(unit, buf, cyl, head, sector)
  155. int unit;                /* NOTE THIS CHANGE! */
  156. byte *buf,head,sector;
  157. int cyl;
  158. {
  159.     int i;
  160.     struct i1010iopb *io, iopb;
  161.  
  162.     if (fds[unit] == -1) {
  163.         init( unit );
  164.     }
  165.     io = &iopb;
  166.     io->i_addr = (long) buf;
  167.     io->i_actcylinder = cyl;
  168.     io->i_acthead = head;
  169.     io->i_sector = sector;
  170.     io->i_funct = WD_READ_OP;
  171.     i = ioctl(fds[unit],I1010_RAWIO,io);
  172. #ifndef PRODUCTION
  173.     if (i)
  174.         fprintf(stderr, "\nread_sector(U=%d, C=%d, H=%d, S=%d) errno= %d\n",
  175.                     unit, cyl, head, sector, i);
  176. #endif
  177.     return(i);
  178. }
  179.  
  180. write_sector(unit,buf,cyl,head,sector)
  181. int unit;
  182. byte *buf,head,sector;
  183. int cyl;
  184. {
  185.     int i;
  186.     struct i1010iopb *io, iopb;
  187.  
  188.     if (fds[unit] == -1) {
  189.         init( unit );
  190.     }
  191.  
  192. #ifdef NOWRITE
  193. return(0);
  194. #endif
  195.  
  196.     io = &iopb;
  197.     io->i_addr = (long) buf;
  198.     io->i_actcylinder = cyl;
  199.     io->i_acthead = head;
  200.     io->i_sector = sector;
  201.     io->i_funct = WD_WRITE_OP;
  202.     i = ioctl(fds[ unit ], I1010_RAWIO, io);
  203. #ifndef PRODUCTION
  204.     if (i)
  205.         fprintf(stderr, "\nwrite_sector(U=%d, C=%d, H=%d, S=%d) errno= %d\n",
  206.                     unit, cyl, head, sector, i);
  207. #endif
  208.     return(i);
  209. }
  210.  
  211. raw_io(unit,buf,cyl,head,sector,op)
  212. int unit;
  213. byte *buf,head,sector;
  214. int cyl,op;
  215. {
  216.     int i;
  217.     struct i1010iopb *io, iopb;
  218.  
  219.     if (fds[unit] == -1) {
  220.         init( unit );
  221.     }
  222.     io = &iopb;
  223.     io->i_addr = (long) buf;
  224.     io->i_actcylinder = cyl;
  225.     io->i_acthead = head;
  226.     io->i_sector = sector;
  227.     io->i_funct = op;
  228.     i = ioctl(fds[ unit ], I1010_RAWIO, io);
  229. #ifndef PRODUCTION
  230.     if (i) printf("\nRaw I/O( U=%d C=%d H=%d S=%d op=%d) errno= %d\n",
  231.                   unit, cyl, head, sector, op, i);
  232. #endif
  233.     return(i);
  234. }
  235.  
  236.