xref: /netbsd/sys/arch/i386/stand/lib/test/biosdisk_user.c (revision bf9ec67e)
1 /*	$NetBSD: biosdisk_user.c,v 1.4 1999/04/01 16:09:49 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_ll.h"
43 #include "biosdisk_user.h"
44 
45 /*
46  * Replacement for i386/stand/lib/bios_disk.S.
47  * Allows to map BIOS-like device numbers to character
48  * device nodes or plain files.
49  * The actual mapping is defined in the external table
50  * "emuldisktab".
51  */
52 
53 static int currentdev, currentdte;
54 static int fd = -1;
55 
56 int
57 get_diskinfo(dev)
58 	int dev;
59 {
60 	int i, retval;
61 
62 	if (fd != -1) {
63 		close(fd);
64 		fd = -1;
65 	}
66 
67 	i = 0;
68 	for (;;) {
69 		if (emuldisktab[i].biosdev == -1)
70 			break;
71 		if (emuldisktab[i].biosdev == dev)
72 			goto ok;
73 		i++;
74 	}
75 	warnx("unknown device %x", dev);
76 	return (0); /* triggers error in set_geometry() */
77 
78 ok:
79 	fd = open(emuldisktab[i].name, O_RDONLY, 0);
80 	if (fd < 0) {
81 		warn("open %s", emuldisktab[i].name);
82 		return (0);
83 	}
84 
85 	currentdev = dev;
86 	currentdte = i;
87 
88 	retval = ((emuldisktab[i].cyls - 1) & 0xff) << 16;
89 	retval |= ((emuldisktab[i].cyls - 1) & 0x300) << 6;
90 	retval |= emuldisktab[i].spt << 8;
91 	retval |= emuldisktab[i].heads - 1;
92 	return (retval);
93 }
94 
95 int
96 biosread(dev, cyl, head, sec, nsec, buf)
97 	int dev;
98 	int cyl, head, sec;
99 	int nsec;
100 	char *buf;
101 {
102 	if (dev != currentdev) {
103 		warnx("biosread: unexpected device %x", dev);
104 		return (-1);
105 	}
106 
107 	if (lseek(fd, ((cyl * emuldisktab[currentdte].heads + head)
108 		       * emuldisktab[currentdte].spt + sec) * 512,
109 		  SEEK_SET) == -1) {
110 		warn("lseek");
111 		return (-1);
112 	}
113 	if (read(fd, buf, nsec * 512) != nsec * 512) {
114 		warn("read");
115 		return (-1);
116 	}
117 	return (0);
118 }
119 
120 int
121 int13_extension(dev)
122 	int dev;
123 {
124 	return (0);
125 }
126 
127 void
128 int13_getextinfo(dev, info)
129 	int dev;
130 	struct biosdisk_ext13info *info;
131 {
132 }
133 
134 struct ext {
135 	int8_t	size;
136 	int8_t	resvd;
137 	int16_t	cnt;
138 	int16_t	off;
139 	int16_t	seg;
140 	int64_t	sec;
141 };
142 
143 int
144 biosextread(dev, ext)
145 	int dev;
146 	struct ext *ext;
147 {
148 	return (-1);
149 }
150