diskio.c revision 1.1 1 1.1 leo /* $NetBSD: diskio.c,v 1.1 2002/02/24 20:51:08 leo Exp $ */
2 1.1 leo
3 1.1 leo /*
4 1.1 leo * Copyright (c) 1995 Waldi Ravens.
5 1.1 leo * All rights reserved.
6 1.1 leo *
7 1.1 leo * Redistribution and use in source and binary forms, with or without
8 1.1 leo * modification, are permitted provided that the following conditions
9 1.1 leo * are met:
10 1.1 leo * 1. Redistributions of source code must retain the above copyright
11 1.1 leo * notice, this list of conditions and the following disclaimer.
12 1.1 leo * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 leo * notice, this list of conditions and the following disclaimer in the
14 1.1 leo * documentation and/or other materials provided with the distribution.
15 1.1 leo * 3. All advertising materials mentioning features or use of this software
16 1.1 leo * must display the following acknowledgement:
17 1.1 leo * This product includes software developed by Waldi Ravens.
18 1.1 leo * 4. The name of the author may not be used to endorse or promote products
19 1.1 leo * derived from this software without specific prior written permission.
20 1.1 leo *
21 1.1 leo * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 1.1 leo * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 1.1 leo * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 1.1 leo * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 1.1 leo * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 1.1 leo * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 1.1 leo * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 1.1 leo * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 1.1 leo * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 1.1 leo * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 1.1 leo */
32 1.1 leo
33 1.1 leo #include <sys/types.h>
34 1.1 leo #include <stdlib.h>
35 1.1 leo #include <string.h>
36 1.1 leo #include <ctype.h>
37 1.1 leo #include <stdio.h>
38 1.1 leo #include <xhdi.h>
39 1.1 leo #include "libtos.h"
40 1.1 leo #include "diskio.h"
41 1.1 leo #include "ahdilbl.h"
42 1.1 leo #include <osbind.h>
43 1.1 leo
44 1.1 leo struct pun_info {
45 1.1 leo u_int16_t puns;
46 1.1 leo u_int8_t pun[16];
47 1.1 leo u_int32_t part_start[16];
48 1.1 leo u_int32_t P_cookie;
49 1.1 leo u_int32_t *P_cookptr;
50 1.1 leo u_int16_t P_version;
51 1.1 leo u_int16_t P_max_sector;
52 1.1 leo u_int32_t reserved[16];
53 1.1 leo };
54 1.1 leo
55 1.1 leo static char * strbd PROTO((char *, ...));
56 1.1 leo static int setmami PROTO((disk_t *, char *));
57 1.1 leo static int setnames PROTO((disk_t *));
58 1.1 leo static int setsizes PROTO((disk_t *));
59 1.1 leo static int ahdi_compatible PROTO((void));
60 1.1 leo
61 1.1 leo disk_t *
62 1.1 leo disk_open(name)
63 1.1 leo char *name;
64 1.1 leo {
65 1.1 leo disk_t *dd;
66 1.1 leo
67 1.1 leo dd = xmalloc(sizeof *dd);
68 1.1 leo memset(dd, 0, sizeof *dd);
69 1.1 leo
70 1.1 leo if (setmami(dd, name) || setnames(dd) || setsizes(dd)) {
71 1.1 leo disk_close(dd);
72 1.1 leo return(NULL);
73 1.1 leo }
74 1.1 leo return(dd);
75 1.1 leo }
76 1.1 leo
77 1.1 leo void
78 1.1 leo disk_close(dd)
79 1.1 leo disk_t *dd;
80 1.1 leo {
81 1.1 leo if (dd) {
82 1.1 leo free(dd->product);
83 1.1 leo free(dd->sname);
84 1.1 leo free(dd->fname);
85 1.1 leo if (dd->xtra_info != NULL)
86 1.1 leo free(dd->xtra_info);
87 1.1 leo free(dd);
88 1.1 leo }
89 1.1 leo }
90 1.1 leo
91 1.1 leo void *
92 1.1 leo disk_read(dd, start, count)
93 1.1 leo disk_t *dd;
94 1.1 leo u_int start,
95 1.1 leo count;
96 1.1 leo {
97 1.1 leo char *buffer;
98 1.1 leo int bdev;
99 1.1 leo long e;
100 1.1 leo
101 1.1 leo buffer = xmalloc(count * dd->bsize);
102 1.1 leo
103 1.1 leo e = XHReadWrite(dd->major, dd->minor, 0, start, count, buffer);
104 1.1 leo if (!e)
105 1.1 leo return(buffer);
106 1.1 leo if (e == -32 || (e == -1 && XHGetVersion() == -1)) {
107 1.1 leo if (!ahdi_compatible())
108 1.1 leo fatal(-1, "AHDI 3.0 compatible harddisk driver required");
109 1.1 leo bdev = BIOSDEV(dd->major, dd->minor);
110 1.1 leo if (bdev && !bios_read(buffer, start, count, bdev))
111 1.1 leo return(buffer);
112 1.1 leo }
113 1.1 leo
114 1.1 leo free(buffer);
115 1.1 leo return(NULL);
116 1.1 leo }
117 1.1 leo
118 1.1 leo int
119 1.1 leo disk_write(dd, start, count, buffer)
120 1.1 leo disk_t *dd;
121 1.1 leo u_int start,
122 1.1 leo count;
123 1.1 leo void *buffer;
124 1.1 leo {
125 1.1 leo int bdev;
126 1.1 leo long e;
127 1.1 leo
128 1.1 leo e = XHReadWrite(dd->major, dd->minor, 1, start, count, buffer);
129 1.1 leo if (e == -32 || (e == -1 && XHGetVersion() == -1)) {
130 1.1 leo if (!ahdi_compatible())
131 1.1 leo fatal(-1, "AHDI 3.0 compatible harddisk driver required");
132 1.1 leo bdev = BIOSDEV(dd->major, dd->minor);
133 1.1 leo if (bdev)
134 1.1 leo e = bios_write(buffer, start, count, bdev);
135 1.1 leo }
136 1.1 leo
137 1.1 leo return((int)e);
138 1.1 leo }
139 1.1 leo
140 1.1 leo static int
141 1.1 leo ahdi_compatible()
142 1.1 leo {
143 1.1 leo static int ahdi_compat;
144 1.1 leo
145 1.1 leo if (!ahdi_compat) {
146 1.1 leo long oldsp = Super(0L);
147 1.1 leo struct pun_info *punp = *((struct pun_info **)0x0516);
148 1.1 leo (void)Super(oldsp);
149 1.1 leo if (punp && punp->P_cookie == 0x41484449
150 1.1 leo && punp->P_cookptr == &punp->P_cookie
151 1.1 leo && punp->P_version >= 0x0300)
152 1.1 leo ahdi_compat = 1;
153 1.1 leo }
154 1.1 leo return(ahdi_compat);
155 1.1 leo }
156 1.1 leo
157 1.1 leo static int
158 1.1 leo setmami(dd, name)
159 1.1 leo disk_t *dd;
160 1.1 leo char *name;
161 1.1 leo {
162 1.1 leo char *p = name;
163 1.1 leo u_int target, lun;
164 1.1 leo bus_t bus;
165 1.1 leo
166 1.1 leo if (*p == 'i') {
167 1.1 leo bus = IDE;
168 1.1 leo if (*++p < '0' || *p > '1') {
169 1.1 leo if (*p)
170 1.1 leo error(-1, "%s: invalid IDE target `%c'", name, *p);
171 1.1 leo else
172 1.1 leo error(-1, "%s: missing IDE target", name);
173 1.1 leo return(-1);
174 1.1 leo }
175 1.1 leo target = *p++ - '0';
176 1.1 leo lun = 0;
177 1.1 leo } else {
178 1.1 leo char *b;
179 1.1 leo
180 1.1 leo if (*p == 'a') {
181 1.1 leo bus = ACSI;
182 1.1 leo b = "ACSI";
183 1.1 leo } else if (*p == 's') {
184 1.1 leo bus = SCSI;
185 1.1 leo b = "SCSI";
186 1.1 leo } else {
187 1.1 leo error(-1, "%s: invalid DISK argument", name);
188 1.1 leo return(-1);
189 1.1 leo }
190 1.1 leo if (*++p < '0' || *p > '7') {
191 1.1 leo if (*p)
192 1.1 leo error(-1, "%s: invalid %s target `%c'", name,
193 1.1 leo b, *p);
194 1.1 leo else
195 1.1 leo error(-1, "%s: missing %s target", name, b);
196 1.1 leo return(-1);
197 1.1 leo }
198 1.1 leo target = *p++ - '0';
199 1.1 leo
200 1.1 leo if (*p < '0' || *p > '7') {
201 1.1 leo if (*p) {
202 1.1 leo error(-1, "%s: invalid %s lun `%c'", name,
203 1.1 leo b, *p);
204 1.1 leo return(-1);
205 1.1 leo }
206 1.1 leo lun = 0;
207 1.1 leo } else
208 1.1 leo lun = *p++ - '0';
209 1.1 leo }
210 1.1 leo if (*p) {
211 1.1 leo error(-1, "%s: invalid DISK argument", name);
212 1.1 leo return(-1);
213 1.1 leo }
214 1.1 leo dd->major = MAJOR(bus, target, lun);
215 1.1 leo dd->minor = MINOR(bus, target, lun);
216 1.1 leo return(0);
217 1.1 leo }
218 1.1 leo
219 1.1 leo static int
220 1.1 leo setnames(dd)
221 1.1 leo disk_t *dd;
222 1.1 leo {
223 1.1 leo char sn[16], us[16], ls[16], *bs;
224 1.1 leo int b, u, l;
225 1.1 leo
226 1.1 leo b = BUS(dd->major, dd->minor);
227 1.1 leo u = TARGET(dd->major, dd->minor);
228 1.1 leo l = LUN(dd->major, dd->minor);
229 1.1 leo
230 1.1 leo switch (b) {
231 1.1 leo case IDE: bs = "IDE";
232 1.1 leo break;
233 1.1 leo case ACSI: bs = "ACSI";
234 1.1 leo break;
235 1.1 leo case SCSI: bs = "SCSI";
236 1.1 leo break;
237 1.1 leo default: error(-1, "invalid bus no. %d", b);
238 1.1 leo return(-1);
239 1.1 leo }
240 1.1 leo
241 1.1 leo if (u < 0 || u > 7 || (b == IDE && u > 1)) {
242 1.1 leo error(-1, "invalid %s target `%d'", bs, u);
243 1.1 leo return(-1);
244 1.1 leo }
245 1.1 leo sprintf(us, " target %d", u);
246 1.1 leo
247 1.1 leo if (l < 0 || l > 7 || (b == IDE && l > 0)) {
248 1.1 leo error(-1, "invalid %s lun `%d'", bs, l);
249 1.1 leo return(-1);
250 1.1 leo }
251 1.1 leo if (b == IDE) {
252 1.1 leo sprintf(sn, "i%d", u);
253 1.1 leo ls[0] = '\0';
254 1.1 leo } else {
255 1.1 leo sprintf(sn, "%c%d%d", tolower(*bs), u, l);
256 1.1 leo sprintf(ls, " lun %d", l);
257 1.1 leo }
258 1.1 leo
259 1.1 leo dd->fname = strbd(bs, us, ls, NULL);
260 1.1 leo dd->sname = strbd(sn, NULL);
261 1.1 leo return(0);
262 1.1 leo }
263 1.1 leo
264 1.1 leo static int
265 1.1 leo setsizes(dd)
266 1.1 leo disk_t *dd;
267 1.1 leo {
268 1.1 leo if (XHGetVersion() != -1) {
269 1.1 leo char *p, prod[1024];
270 1.1 leo
271 1.1 leo if (XHInqTarget2(dd->major, dd->minor, &dd->bsize, NULL, prod,
272 1.1 leo sizeof(prod))) {
273 1.1 leo if (XHInqTarget(dd->major, dd->minor, &dd->bsize, NULL, prod)) {
274 1.1 leo error(-1, "%s: device not configured", dd->sname);
275 1.1 leo return(-1);
276 1.1 leo }
277 1.1 leo }
278 1.1 leo p = strrchr(prod, '\0');
279 1.1 leo while (isspace(*--p))
280 1.1 leo *p = '\0';
281 1.1 leo dd->product = strbd(prod, NULL);
282 1.1 leo if (!XHGetCapacity(dd->major, dd->minor, &dd->msize, &dd->bsize))
283 1.1 leo return(0);
284 1.1 leo } else {
285 1.1 leo dd->product = strbd("unknown", NULL);
286 1.1 leo dd->bsize = AHDI_BSIZE; /* XXX */
287 1.1 leo }
288 1.1 leo
289 1.1 leo /* Trial&error search for last sector on medium */
290 1.1 leo {
291 1.1 leo u_int u, l, m;
292 1.1 leo void *p, (*oldvec)();
293 1.1 leo
294 1.1 leo /* turn off etv_critic handler */
295 1.1 leo oldvec = Setexc(257, bios_critic);
296 1.1 leo
297 1.1 leo u = (u_int)-2; l = 0;
298 1.1 leo while (u != l) {
299 1.1 leo m = l + ((u - l + 1) / 2);
300 1.1 leo p = disk_read(dd, m, 1);
301 1.1 leo free(p);
302 1.1 leo if (p == NULL)
303 1.1 leo u = m - 1;
304 1.1 leo else
305 1.1 leo l = m;
306 1.1 leo }
307 1.1 leo
308 1.1 leo /* turn on etv_critic handler */
309 1.1 leo (void)Setexc(257, oldvec);
310 1.1 leo
311 1.1 leo if (l) {
312 1.1 leo dd->msize = l + 1;
313 1.1 leo return(0);
314 1.1 leo }
315 1.1 leo error(-1, "%s: device not configured", dd->sname);
316 1.1 leo return(-1);
317 1.1 leo }
318 1.1 leo }
319 1.1 leo
320 1.1 leo static char *
321 1.1 leo strbd(string1)
322 1.1 leo char *string1;
323 1.1 leo {
324 1.1 leo char *p, *result;
325 1.1 leo size_t length = 1;
326 1.1 leo va_list ap;
327 1.1 leo
328 1.1 leo va_start(ap, string1);
329 1.1 leo for (p = string1; p; p = va_arg(ap, char *))
330 1.1 leo length += strlen(p);
331 1.1 leo va_end(ap);
332 1.1 leo
333 1.1 leo *(result = xmalloc(length)) = '\0';
334 1.1 leo
335 1.1 leo va_start(ap, string1);
336 1.1 leo for (p = string1; p; p = va_arg(ap, char *))
337 1.1 leo strcat(result, p);
338 1.1 leo va_end(ap);
339 1.1 leo
340 1.1 leo return(result);
341 1.1 leo }
342