xref: /netbsd-src/sys/arch/i386/stand/lib/test/biosdisk_user.c (revision d48f14661dda8638fee055ba15d35bdfb29b9fa8)
1 /*	$NetBSD: biosdisk_user.c,v 1.6 2005/12/11 12:17:49 christos 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  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  */
28 
29 #include "sanamespace.h"
30 
31 #include <stdio.h>
32 #include <unistd.h>
33 #include <fcntl.h>
34 #include <err.h>
35 
36 #include "biosdisk_ll.h"
37 #include "biosdisk_user.h"
38 
39 /*
40  * Replacement for i386/stand/lib/bios_disk.S.
41  * Allows to map BIOS-like device numbers to character
42  * device nodes or plain files.
43  * The actual mapping is defined in the external table
44  * "emuldisktab".
45  */
46 
47 static int currentdev, currentdte;
48 static int fd = -1;
49 
50 int
51 get_diskinfo(dev)
52 	int dev;
53 {
54 	int i, retval;
55 
56 	if (fd != -1) {
57 		close(fd);
58 		fd = -1;
59 	}
60 
61 	i = 0;
62 	for (;;) {
63 		if (emuldisktab[i].biosdev == -1)
64 			break;
65 		if (emuldisktab[i].biosdev == dev)
66 			goto ok;
67 		i++;
68 	}
69 	warnx("unknown device %x", dev);
70 	return (0); /* triggers error in set_geometry() */
71 
72 ok:
73 	fd = open(emuldisktab[i].name, O_RDONLY, 0);
74 	if (fd < 0) {
75 		warn("open %s", emuldisktab[i].name);
76 		return (0);
77 	}
78 
79 	currentdev = dev;
80 	currentdte = i;
81 
82 	retval = ((emuldisktab[i].cyls - 1) & 0xff) << 16;
83 	retval |= ((emuldisktab[i].cyls - 1) & 0x300) << 6;
84 	retval |= emuldisktab[i].spt << 8;
85 	retval |= emuldisktab[i].heads - 1;
86 	return (retval);
87 }
88 
89 int
90 biosread(dev, cyl, head, sec, nsec, buf)
91 	int dev;
92 	int cyl, head, sec;
93 	int nsec;
94 	char *buf;
95 {
96 	if (dev != currentdev) {
97 		warnx("biosread: unexpected device %x", dev);
98 		return (-1);
99 	}
100 
101 	if (lseek(fd, ((cyl * emuldisktab[currentdte].heads + head)
102 		       * emuldisktab[currentdte].spt + sec) * 512,
103 		  SEEK_SET) == -1) {
104 		warn("lseek");
105 		return (-1);
106 	}
107 	if (read(fd, buf, nsec * 512) != nsec * 512) {
108 		warn("read");
109 		return (-1);
110 	}
111 	return (0);
112 }
113 
114 int
115 int13_extension(dev)
116 	int dev;
117 {
118 	return (0);
119 }
120 
121 void
122 int13_getextinfo(dev, info)
123 	int dev;
124 	struct biosdisk_ext13info *info;
125 {
126 }
127 
128 struct ext {
129 	int8_t	size;
130 	int8_t	resvd;
131 	int16_t	cnt;
132 	int16_t	off;
133 	int16_t	seg;
134 	int64_t	sec;
135 };
136 
137 int
138 biosextread(dev, ext)
139 	int dev;
140 	struct ext *ext;
141 {
142 	return (-1);
143 }
144