Sync usage with man page.
[netbsd-mini2440.git] / sys / arch / atari / stand / tostools / libtos / diskio.c
blob00bcfafeffca29db25e68174d153858a4bbf576f
1 /* $NetBSD: diskio.c,v 1.2 2009/03/14 15:36:04 dsl Exp $ */
3 /*
4 * Copyright (c) 1995 Waldi Ravens.
5 * All rights reserved.
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 by Waldi Ravens.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/types.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <ctype.h>
37 #include <stdio.h>
38 #include <xhdi.h>
39 #include "libtos.h"
40 #include "diskio.h"
41 #include "ahdilbl.h"
42 #include <osbind.h>
44 struct pun_info {
45 u_int16_t puns;
46 u_int8_t pun[16];
47 u_int32_t part_start[16];
48 u_int32_t P_cookie;
49 u_int32_t *P_cookptr;
50 u_int16_t P_version;
51 u_int16_t P_max_sector;
52 u_int32_t reserved[16];
55 static char * strbd PROTO((char *, ...));
56 static int setmami PROTO((disk_t *, char *));
57 static int setnames PROTO((disk_t *));
58 static int setsizes PROTO((disk_t *));
59 static int ahdi_compatible PROTO((void));
61 disk_t *
62 disk_open(char *name)
64 disk_t *dd;
66 dd = xmalloc(sizeof *dd);
67 memset(dd, 0, sizeof *dd);
69 if (setmami(dd, name) || setnames(dd) || setsizes(dd)) {
70 disk_close(dd);
71 return(NULL);
73 return(dd);
76 void
77 disk_close(disk_t *dd)
79 if (dd) {
80 free(dd->product);
81 free(dd->sname);
82 free(dd->fname);
83 if (dd->xtra_info != NULL)
84 free(dd->xtra_info);
85 free(dd);
89 void *
90 disk_read(dd, start, count)
91 disk_t *dd;
92 u_int start,
93 count;
95 char *buffer;
96 int bdev;
97 long e;
99 buffer = xmalloc(count * dd->bsize);
101 e = XHReadWrite(dd->major, dd->minor, 0, start, count, buffer);
102 if (!e)
103 return(buffer);
104 if (e == -32 || (e == -1 && XHGetVersion() == -1)) {
105 if (!ahdi_compatible())
106 fatal(-1, "AHDI 3.0 compatible harddisk driver required");
107 bdev = BIOSDEV(dd->major, dd->minor);
108 if (bdev && !bios_read(buffer, start, count, bdev))
109 return(buffer);
112 free(buffer);
113 return(NULL);
117 disk_write(dd, start, count, buffer)
118 disk_t *dd;
119 u_int start,
120 count;
121 void *buffer;
123 int bdev;
124 long e;
126 e = XHReadWrite(dd->major, dd->minor, 1, start, count, buffer);
127 if (e == -32 || (e == -1 && XHGetVersion() == -1)) {
128 if (!ahdi_compatible())
129 fatal(-1, "AHDI 3.0 compatible harddisk driver required");
130 bdev = BIOSDEV(dd->major, dd->minor);
131 if (bdev)
132 e = bios_write(buffer, start, count, bdev);
135 return((int)e);
138 static int
139 ahdi_compatible(void)
141 static int ahdi_compat;
143 if (!ahdi_compat) {
144 long oldsp = Super(0L);
145 struct pun_info *punp = *((struct pun_info **)0x0516);
146 (void)Super(oldsp);
147 if (punp && punp->P_cookie == 0x41484449
148 && punp->P_cookptr == &punp->P_cookie
149 && punp->P_version >= 0x0300)
150 ahdi_compat = 1;
152 return(ahdi_compat);
155 static int
156 setmami(disk_t *dd, char *name)
158 char *p = name;
159 u_int target, lun;
160 bus_t bus;
162 if (*p == 'i') {
163 bus = IDE;
164 if (*++p < '0' || *p > '1') {
165 if (*p)
166 error(-1, "%s: invalid IDE target `%c'", name, *p);
167 else
168 error(-1, "%s: missing IDE target", name);
169 return(-1);
171 target = *p++ - '0';
172 lun = 0;
173 } else {
174 char *b;
176 if (*p == 'a') {
177 bus = ACSI;
178 b = "ACSI";
179 } else if (*p == 's') {
180 bus = SCSI;
181 b = "SCSI";
182 } else {
183 error(-1, "%s: invalid DISK argument", name);
184 return(-1);
186 if (*++p < '0' || *p > '7') {
187 if (*p)
188 error(-1, "%s: invalid %s target `%c'", name,
189 b, *p);
190 else
191 error(-1, "%s: missing %s target", name, b);
192 return(-1);
194 target = *p++ - '0';
196 if (*p < '0' || *p > '7') {
197 if (*p) {
198 error(-1, "%s: invalid %s lun `%c'", name,
199 b, *p);
200 return(-1);
202 lun = 0;
203 } else
204 lun = *p++ - '0';
206 if (*p) {
207 error(-1, "%s: invalid DISK argument", name);
208 return(-1);
210 dd->major = MAJOR(bus, target, lun);
211 dd->minor = MINOR(bus, target, lun);
212 return(0);
215 static int
216 setnames(disk_t *dd)
218 char sn[16], us[16], ls[16], *bs;
219 int b, u, l;
221 b = BUS(dd->major, dd->minor);
222 u = TARGET(dd->major, dd->minor);
223 l = LUN(dd->major, dd->minor);
225 switch (b) {
226 case IDE: bs = "IDE";
227 break;
228 case ACSI: bs = "ACSI";
229 break;
230 case SCSI: bs = "SCSI";
231 break;
232 default: error(-1, "invalid bus no. %d", b);
233 return(-1);
236 if (u < 0 || u > 7 || (b == IDE && u > 1)) {
237 error(-1, "invalid %s target `%d'", bs, u);
238 return(-1);
240 sprintf(us, " target %d", u);
242 if (l < 0 || l > 7 || (b == IDE && l > 0)) {
243 error(-1, "invalid %s lun `%d'", bs, l);
244 return(-1);
246 if (b == IDE) {
247 sprintf(sn, "i%d", u);
248 ls[0] = '\0';
249 } else {
250 sprintf(sn, "%c%d%d", tolower(*bs), u, l);
251 sprintf(ls, " lun %d", l);
254 dd->fname = strbd(bs, us, ls, NULL);
255 dd->sname = strbd(sn, NULL);
256 return(0);
259 static int
260 setsizes(disk_t *dd)
262 if (XHGetVersion() != -1) {
263 char *p, prod[1024];
265 if (XHInqTarget2(dd->major, dd->minor, &dd->bsize, NULL, prod,
266 sizeof(prod))) {
267 if (XHInqTarget(dd->major, dd->minor, &dd->bsize, NULL, prod)) {
268 error(-1, "%s: device not configured", dd->sname);
269 return(-1);
272 p = strrchr(prod, '\0');
273 while (isspace(*--p))
274 *p = '\0';
275 dd->product = strbd(prod, NULL);
276 if (!XHGetCapacity(dd->major, dd->minor, &dd->msize, &dd->bsize))
277 return(0);
278 } else {
279 dd->product = strbd("unknown", NULL);
280 dd->bsize = AHDI_BSIZE; /* XXX */
283 /* Trial&error search for last sector on medium */
285 u_int u, l, m;
286 void *p, (*oldvec)();
288 /* turn off etv_critic handler */
289 oldvec = Setexc(257, bios_critic);
291 u = (u_int)-2; l = 0;
292 while (u != l) {
293 m = l + ((u - l + 1) / 2);
294 p = disk_read(dd, m, 1);
295 free(p);
296 if (p == NULL)
297 u = m - 1;
298 else
299 l = m;
302 /* turn on etv_critic handler */
303 (void)Setexc(257, oldvec);
305 if (l) {
306 dd->msize = l + 1;
307 return(0);
309 error(-1, "%s: device not configured", dd->sname);
310 return(-1);
314 static char *
315 strbd(char *string1)
317 char *p, *result;
318 size_t length = 1;
319 va_list ap;
321 va_start(ap, string1);
322 for (p = string1; p; p = va_arg(ap, char *))
323 length += strlen(p);
324 va_end(ap);
326 *(result = xmalloc(length)) = '\0';
328 va_start(ap, string1);
329 for (p = string1; p; p = va_arg(ap, char *))
330 strcat(result, p);
331 va_end(ap);
333 return(result);