Home | History | Annotate | Line # | Download | only in libtos
diskio.c revision 1.3
      1  1.3  cegger /*	$NetBSD: diskio.c,v 1.3 2009/03/18 10:22:26 cegger 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.2     dsl disk_open(char *name)
     63  1.1     leo {
     64  1.1     leo 	disk_t	*dd;
     65  1.1     leo 
     66  1.1     leo 	dd = xmalloc(sizeof *dd);
     67  1.1     leo 	memset(dd, 0, sizeof *dd);
     68  1.1     leo 
     69  1.1     leo 	if (setmami(dd, name) || setnames(dd) || setsizes(dd)) {
     70  1.1     leo 		disk_close(dd);
     71  1.1     leo 		return(NULL);
     72  1.1     leo 	}
     73  1.1     leo 	return(dd);
     74  1.1     leo }
     75  1.1     leo 
     76  1.1     leo void
     77  1.2     dsl disk_close(disk_t *dd)
     78  1.1     leo {
     79  1.1     leo 	if (dd) {
     80  1.1     leo 		free(dd->product);
     81  1.1     leo 		free(dd->sname);
     82  1.1     leo 		free(dd->fname);
     83  1.1     leo 		if (dd->xtra_info != NULL)
     84  1.1     leo 			free(dd->xtra_info);
     85  1.1     leo 		free(dd);
     86  1.1     leo 	}
     87  1.1     leo }
     88  1.1     leo 
     89  1.1     leo void *
     90  1.1     leo disk_read(dd, start, count)
     91  1.1     leo 	disk_t	*dd;
     92  1.1     leo 	u_int	start,
     93  1.1     leo 		count;
     94  1.1     leo {
     95  1.1     leo 	char	*buffer;
     96  1.1     leo 	int	bdev;
     97  1.1     leo 	long	e;
     98  1.1     leo 
     99  1.1     leo 	buffer = xmalloc(count * dd->bsize);
    100  1.1     leo 
    101  1.1     leo 	e = XHReadWrite(dd->major, dd->minor, 0, start, count, buffer);
    102  1.1     leo 	if (!e)
    103  1.1     leo 		return(buffer);
    104  1.1     leo 	if (e == -32 || (e == -1 && XHGetVersion() == -1)) {
    105  1.1     leo 		if (!ahdi_compatible())
    106  1.1     leo 		    fatal(-1, "AHDI 3.0 compatible harddisk driver required");
    107  1.1     leo 		bdev = BIOSDEV(dd->major, dd->minor);
    108  1.1     leo 		if (bdev && !bios_read(buffer, start, count, bdev))
    109  1.1     leo 			return(buffer);
    110  1.1     leo 	}
    111  1.1     leo 
    112  1.1     leo 	free(buffer);
    113  1.1     leo 	return(NULL);
    114  1.1     leo }
    115  1.1     leo 
    116  1.1     leo int
    117  1.1     leo disk_write(dd, start, count, buffer)
    118  1.1     leo 	disk_t	*dd;
    119  1.1     leo 	u_int	start,
    120  1.1     leo 		count;
    121  1.1     leo 	void	*buffer;
    122  1.1     leo {
    123  1.1     leo 	int	bdev;
    124  1.1     leo 	long	e;
    125  1.1     leo 
    126  1.1     leo 	e = XHReadWrite(dd->major, dd->minor, 1, start, count, buffer);
    127  1.1     leo 	if (e == -32 || (e == -1 && XHGetVersion() == -1)) {
    128  1.1     leo 		if (!ahdi_compatible())
    129  1.1     leo 		    fatal(-1, "AHDI 3.0 compatible harddisk driver required");
    130  1.1     leo 		bdev = BIOSDEV(dd->major, dd->minor);
    131  1.1     leo 		if (bdev)
    132  1.1     leo 			e = bios_write(buffer, start, count, bdev);
    133  1.1     leo 	}
    134  1.1     leo 
    135  1.1     leo 	return((int)e);
    136  1.1     leo }
    137  1.1     leo 
    138  1.1     leo static int
    139  1.3  cegger ahdi_compatible(void)
    140  1.1     leo {
    141  1.1     leo 	static int	ahdi_compat;
    142  1.1     leo 
    143  1.1     leo 	if (!ahdi_compat) {
    144  1.1     leo 		long		oldsp = Super(0L);
    145  1.1     leo 		struct pun_info	*punp = *((struct pun_info **)0x0516);
    146  1.1     leo 		(void)Super(oldsp);
    147  1.1     leo 		if (punp && punp->P_cookie == 0x41484449
    148  1.1     leo 				&& punp->P_cookptr == &punp->P_cookie
    149  1.1     leo 				&& punp->P_version >= 0x0300)
    150  1.1     leo 			ahdi_compat = 1;
    151  1.1     leo 	}
    152  1.1     leo 	return(ahdi_compat);
    153  1.1     leo }
    154  1.1     leo 
    155  1.1     leo static int
    156  1.2     dsl setmami(disk_t *dd, char *name)
    157  1.1     leo {
    158  1.1     leo 	char	*p = name;
    159  1.1     leo 	u_int	target, lun;
    160  1.1     leo 	bus_t	bus;
    161  1.1     leo 
    162  1.1     leo 	if (*p == 'i') {
    163  1.1     leo 		bus = IDE;
    164  1.1     leo 		if (*++p < '0' || *p > '1') {
    165  1.1     leo 			if (*p)
    166  1.1     leo 			    error(-1, "%s: invalid IDE target `%c'", name, *p);
    167  1.1     leo 			else
    168  1.1     leo 			    error(-1, "%s: missing IDE target", name);
    169  1.1     leo 			return(-1);
    170  1.1     leo 		}
    171  1.1     leo 		target = *p++ - '0';
    172  1.1     leo 		lun = 0;
    173  1.1     leo 	} else {
    174  1.1     leo 		char	*b;
    175  1.1     leo 
    176  1.1     leo 		if (*p == 'a') {
    177  1.1     leo 			bus = ACSI;
    178  1.1     leo 			b = "ACSI";
    179  1.1     leo 		} else if (*p == 's') {
    180  1.1     leo 			bus = SCSI;
    181  1.1     leo 			b = "SCSI";
    182  1.1     leo 		} else {
    183  1.1     leo 			error(-1, "%s: invalid DISK argument", name);
    184  1.1     leo 			return(-1);
    185  1.1     leo 		}
    186  1.1     leo 		if (*++p < '0' || *p > '7') {
    187  1.1     leo 			if (*p)
    188  1.1     leo 				error(-1, "%s: invalid %s target `%c'", name,
    189  1.1     leo 									b, *p);
    190  1.1     leo 			else
    191  1.1     leo 				error(-1, "%s: missing %s target", name, b);
    192  1.1     leo 			return(-1);
    193  1.1     leo 		}
    194  1.1     leo 		target = *p++ - '0';
    195  1.1     leo 
    196  1.1     leo 		if (*p < '0' || *p > '7') {
    197  1.1     leo 			if (*p) {
    198  1.1     leo 				error(-1, "%s: invalid %s lun `%c'", name,
    199  1.1     leo 								     b, *p);
    200  1.1     leo 				return(-1);
    201  1.1     leo 			}
    202  1.1     leo 			lun = 0;
    203  1.1     leo 		} else
    204  1.1     leo 			lun = *p++ - '0';
    205  1.1     leo 	}
    206  1.1     leo 	if (*p) {
    207  1.1     leo 		error(-1, "%s: invalid DISK argument", name);
    208  1.1     leo 		return(-1);
    209  1.1     leo 	}
    210  1.1     leo 	dd->major = MAJOR(bus, target, lun);
    211  1.1     leo 	dd->minor = MINOR(bus, target, lun);
    212  1.1     leo 	return(0);
    213  1.1     leo }
    214  1.1     leo 
    215  1.1     leo static int
    216  1.2     dsl setnames(disk_t *dd)
    217  1.1     leo {
    218  1.1     leo 	char	sn[16], us[16], ls[16], *bs;
    219  1.1     leo 	int	b, u, l;
    220  1.1     leo 
    221  1.1     leo 	b = BUS(dd->major, dd->minor);
    222  1.1     leo 	u = TARGET(dd->major, dd->minor);
    223  1.1     leo 	l = LUN(dd->major, dd->minor);
    224  1.1     leo 
    225  1.1     leo 	switch (b) {
    226  1.1     leo 	case IDE:	bs = "IDE";
    227  1.1     leo 			break;
    228  1.1     leo 	case ACSI:	bs = "ACSI";
    229  1.1     leo 			break;
    230  1.1     leo 	case SCSI:	bs = "SCSI";
    231  1.1     leo 			break;
    232  1.1     leo 	default:	error(-1, "invalid bus no. %d", b);
    233  1.1     leo 			return(-1);
    234  1.1     leo 	}
    235  1.1     leo 
    236  1.1     leo 	if (u < 0 || u > 7 || (b == IDE && u > 1)) {
    237  1.1     leo 		error(-1, "invalid %s target `%d'", bs, u);
    238  1.1     leo 		return(-1);
    239  1.1     leo 	}
    240  1.1     leo 	sprintf(us, " target %d", u);
    241  1.1     leo 
    242  1.1     leo 	if (l < 0 || l > 7 || (b == IDE && l > 0)) {
    243  1.1     leo 		error(-1, "invalid %s lun `%d'", bs, l);
    244  1.1     leo 		return(-1);
    245  1.1     leo 	}
    246  1.1     leo 	if (b == IDE) {
    247  1.1     leo 		sprintf(sn, "i%d", u);
    248  1.1     leo 		ls[0] = '\0';
    249  1.1     leo 	} else {
    250  1.1     leo 		sprintf(sn, "%c%d%d", tolower(*bs), u, l);
    251  1.1     leo 		sprintf(ls, " lun %d", l);
    252  1.1     leo 	}
    253  1.1     leo 
    254  1.1     leo 	dd->fname = strbd(bs, us, ls, NULL);
    255  1.1     leo 	dd->sname = strbd(sn, NULL);
    256  1.1     leo 	return(0);
    257  1.1     leo }
    258  1.1     leo 
    259  1.1     leo static int
    260  1.2     dsl setsizes(disk_t *dd)
    261  1.1     leo {
    262  1.1     leo 	if (XHGetVersion() != -1) {
    263  1.1     leo 	    char	*p, prod[1024];
    264  1.1     leo 
    265  1.1     leo 	    if (XHInqTarget2(dd->major, dd->minor, &dd->bsize, NULL, prod,
    266  1.1     leo 								sizeof(prod))) {
    267  1.1     leo 		if (XHInqTarget(dd->major, dd->minor, &dd->bsize, NULL, prod)) {
    268  1.1     leo 			error(-1, "%s: device not configured", dd->sname);
    269  1.1     leo 			return(-1);
    270  1.1     leo 		}
    271  1.1     leo 	    }
    272  1.1     leo 	    p = strrchr(prod, '\0');
    273  1.1     leo 	    while (isspace(*--p))
    274  1.1     leo 		*p = '\0';
    275  1.1     leo 	    dd->product = strbd(prod, NULL);
    276  1.1     leo 	    if (!XHGetCapacity(dd->major, dd->minor, &dd->msize, &dd->bsize))
    277  1.1     leo 		return(0);
    278  1.1     leo 	} else {
    279  1.1     leo 	    dd->product = strbd("unknown", NULL);
    280  1.1     leo 	    dd->bsize = AHDI_BSIZE;		/* XXX */
    281  1.1     leo 	}
    282  1.1     leo 
    283  1.1     leo 	/* Trial&error search for last sector on medium */
    284  1.1     leo 	{
    285  1.1     leo 		u_int	u, l, m;
    286  1.1     leo 		void	*p, (*oldvec)();
    287  1.1     leo 
    288  1.1     leo 		/* turn off etv_critic handler */
    289  1.1     leo 		oldvec = Setexc(257, bios_critic);
    290  1.1     leo 
    291  1.1     leo 		u = (u_int)-2; l = 0;
    292  1.1     leo 		while (u != l) {
    293  1.1     leo 			m = l + ((u - l + 1) / 2);
    294  1.1     leo 			p = disk_read(dd, m, 1);
    295  1.1     leo 			free(p);
    296  1.1     leo 			if (p == NULL)
    297  1.1     leo 				u = m - 1;
    298  1.1     leo 			else
    299  1.1     leo 				l = m;
    300  1.1     leo 		}
    301  1.1     leo 
    302  1.1     leo 		/* turn on etv_critic handler */
    303  1.1     leo 		(void)Setexc(257, oldvec);
    304  1.1     leo 
    305  1.1     leo 		if (l) {
    306  1.1     leo 			dd->msize = l + 1;
    307  1.1     leo 			return(0);
    308  1.1     leo 		}
    309  1.1     leo 		error(-1, "%s: device not configured", dd->sname);
    310  1.1     leo 		return(-1);
    311  1.1     leo 	}
    312  1.1     leo }
    313  1.1     leo 
    314  1.1     leo static char *
    315  1.2     dsl strbd(char *string1)
    316  1.1     leo {
    317  1.1     leo 	char		*p, *result;
    318  1.1     leo 	size_t		length = 1;
    319  1.1     leo 	va_list		ap;
    320  1.1     leo 
    321  1.1     leo 	va_start(ap, string1);
    322  1.1     leo 	for (p = string1; p; p = va_arg(ap, char *))
    323  1.1     leo 		length += strlen(p);
    324  1.1     leo 	va_end(ap);
    325  1.1     leo 
    326  1.1     leo 	*(result = xmalloc(length)) = '\0';
    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 		strcat(result, p);
    331  1.1     leo 	va_end(ap);
    332  1.1     leo 
    333  1.1     leo 	return(result);
    334  1.1     leo }
    335