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