Home | History | Annotate | Line # | Download | only in libtos
      1 /*	$NetBSD: diskio.c,v 1.4 2014/03/26 18:04:34 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1995 Waldi Ravens.
      5  * 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 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.
     20  *
     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.
     31  */
     32 
     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>
     43 
     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];
     53 };
     54 
     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));
     60 
     61 disk_t *
     62 disk_open(char *name)
     63 {
     64 	disk_t	*dd;
     65 
     66 	dd = xmalloc(sizeof *dd);
     67 	memset(dd, 0, sizeof *dd);
     68 
     69 	if (setmami(dd, name) || setnames(dd) || setsizes(dd)) {
     70 		disk_close(dd);
     71 		return(NULL);
     72 	}
     73 	return(dd);
     74 }
     75 
     76 void
     77 disk_close(disk_t *dd)
     78 {
     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);
     86 	}
     87 }
     88 
     89 void *
     90 disk_read(dd, start, count)
     91 	disk_t	*dd;
     92 	u_int	start,
     93 		count;
     94 {
     95 	char	*buffer;
     96 	int	bdev;
     97 	long	e;
     98 
     99 	buffer = xmalloc(count * dd->bsize);
    100 
    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);
    110 	}
    111 
    112 	free(buffer);
    113 	return(NULL);
    114 }
    115 
    116 int
    117 disk_write(dd, start, count, buffer)
    118 	disk_t	*dd;
    119 	u_int	start,
    120 		count;
    121 	void	*buffer;
    122 {
    123 	int	bdev;
    124 	long	e;
    125 
    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);
    133 	}
    134 
    135 	return((int)e);
    136 }
    137 
    138 static int
    139 ahdi_compatible(void)
    140 {
    141 	static int	ahdi_compat;
    142 
    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;
    151 	}
    152 	return(ahdi_compat);
    153 }
    154 
    155 static int
    156 setmami(disk_t *dd, char *name)
    157 {
    158 	char	*p = name;
    159 	u_int	target, lun;
    160 	bus_t	bus;
    161 
    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);
    170 		}
    171 		target = *p++ - '0';
    172 		lun = 0;
    173 	} else {
    174 		char	*b;
    175 
    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);
    185 		}
    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);
    193 		}
    194 		target = *p++ - '0';
    195 
    196 		if (*p < '0' || *p > '7') {
    197 			if (*p) {
    198 				error(-1, "%s: invalid %s lun `%c'", name,
    199 								     b, *p);
    200 				return(-1);
    201 			}
    202 			lun = 0;
    203 		} else
    204 			lun = *p++ - '0';
    205 	}
    206 	if (*p) {
    207 		error(-1, "%s: invalid DISK argument", name);
    208 		return(-1);
    209 	}
    210 	dd->major = MAJOR(bus, target, lun);
    211 	dd->minor = MINOR(bus, target, lun);
    212 	return(0);
    213 }
    214 
    215 static int
    216 setnames(disk_t *dd)
    217 {
    218 	char	sn[16], us[16], ls[16], *bs;
    219 	int	b, u, l;
    220 
    221 	b = BUS(dd->major, dd->minor);
    222 	u = TARGET(dd->major, dd->minor);
    223 	l = LUN(dd->major, dd->minor);
    224 
    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);
    234 	}
    235 
    236 	if (u < 0 || u > 7 || (b == IDE && u > 1)) {
    237 		error(-1, "invalid %s target `%d'", bs, u);
    238 		return(-1);
    239 	}
    240 	snprintf(us, sizeof(us), " target %d", u);
    241 
    242 	if (l < 0 || l > 7 || (b == IDE && l > 0)) {
    243 		error(-1, "invalid %s lun `%d'", bs, l);
    244 		return(-1);
    245 	}
    246 	if (b == IDE) {
    247 		snprintf(sn, sizeof(sn), "i%d", u);
    248 		ls[0] = '\0';
    249 	} else {
    250 		snprintf(sn, sizeof(sn), "%c%d%d", tolower(*bs), u, l);
    251 		snprintf(ls, sizeof(ls), " lun %d", l);
    252 	}
    253 
    254 	dd->fname = strbd(bs, us, ls, NULL);
    255 	dd->sname = strbd(sn, NULL);
    256 	return(0);
    257 }
    258 
    259 static int
    260 setsizes(disk_t *dd)
    261 {
    262 	if (XHGetVersion() != -1) {
    263 	    char	*p, prod[1024];
    264 
    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);
    270 		}
    271 	    }
    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 */
    281 	}
    282 
    283 	/* Trial&error search for last sector on medium */
    284 	{
    285 		u_int	u, l, m;
    286 		void	*p, (*oldvec)();
    287 
    288 		/* turn off etv_critic handler */
    289 		oldvec = Setexc(257, bios_critic);
    290 
    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;
    300 		}
    301 
    302 		/* turn on etv_critic handler */
    303 		(void)Setexc(257, oldvec);
    304 
    305 		if (l) {
    306 			dd->msize = l + 1;
    307 			return(0);
    308 		}
    309 		error(-1, "%s: device not configured", dd->sname);
    310 		return(-1);
    311 	}
    312 }
    313 
    314 static char *
    315 strbd(char *string1)
    316 {
    317 	char		*p, *result;
    318 	size_t		length = 1;
    319 	va_list		ap;
    320 
    321 	va_start(ap, string1);
    322 	for (p = string1; p; p = va_arg(ap, char *))
    323 		length += strlen(p);
    324 	va_end(ap);
    325 
    326 	*(result = xmalloc(length)) = '\0';
    327 
    328 	va_start(ap, string1);
    329 	for (p = string1; p; p = va_arg(ap, char *))
    330 		strcat(result, p);
    331 	va_end(ap);
    332 
    333 	return(result);
    334 }
    335