Home | History | Annotate | Line # | Download | only in test
biosdisk_user.c revision 1.1
      1 /*	$NetBSD: biosdisk_user.c,v 1.1 1998/05/15 17:07:15 drochner Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1998
      5  *	Matthias Drochner.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed for the NetBSD Project
     18  *	by Matthias Drochner.
     19  * 4. The name of the author may not be used to endorse or promote products
     20  *    derived from this software without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  *
     33  */
     34 
     35 #include "sanamespace.h"
     36 
     37 #include <stdio.h>
     38 #include <unistd.h>
     39 #include <fcntl.h>
     40 #include <err.h>
     41 
     42 #include "biosdisk_user.h"
     43 
     44 /*
     45  * Replacement for i386/stand/lib/bios_disk.S.
     46  * Allows to map BIOS-like device numbers to character
     47  * device nodes or plain files.
     48  * The actual mapping is defined in the external table
     49  * "emuldisktab".
     50  */
     51 
     52 static int currentdev, currentdte;
     53 static int fd = -1;
     54 
     55 int
     56 get_diskinfo(dev)
     57 	int dev;
     58 {
     59 	int i;
     60 
     61 	if (fd != -1) {
     62 		close(fd);
     63 		fd = -1;
     64 	}
     65 
     66 	i = 0;
     67 	for (;;) {
     68 		if (emuldisktab[i].biosdev == -1)
     69 			break;
     70 		if (emuldisktab[i].biosdev == dev)
     71 			goto ok;
     72 		i++;
     73 	}
     74 	warnx("unknown device %x", dev);
     75 	return (0);
     76 
     77 ok:
     78 	fd = open(emuldisktab[i].name, O_RDONLY, 0);
     79 	if (fd < 0) {
     80 		warn("open %s", emuldisktab[i].name);
     81 		return (0);
     82 	}
     83 
     84 	currentdev = dev;
     85 	currentdte = i;
     86 	return (((emuldisktab[i].heads - 1) << 8)
     87 		+ emuldisktab[i].spt);
     88 }
     89 
     90 int
     91 biosread(dev, cyl, head, sec, nsec, buf)
     92 	int dev;
     93 	int cyl, head, sec;
     94 	int nsec;
     95 	char *buf;
     96 {
     97 	if (dev != currentdev) {
     98 		warnx("biosread: unexpected device %x", dev);
     99 		return (-1);
    100 	}
    101 
    102 	if (lseek(fd, ((cyl * emuldisktab[currentdte].heads + head)
    103 		       * emuldisktab[currentdte].spt + sec) * 512,
    104 		  SEEK_SET) == -1) {
    105 		warn("lseek");
    106 		return (-1);
    107 	}
    108 	if (read(fd, buf, nsec * 512) != nsec * 512) {
    109 		warn("read");
    110 		return (-1);
    111 	}
    112 	return (0);
    113 }
    114