Home | History | Annotate | Line # | Download | only in libsa
sdcd.c revision 1.6.28.1
      1  1.6.28.1       ad /*	$NetBSD: sdcd.c,v 1.6.28.1 2007/12/03 18:40:00 ad Exp $	*/
      2       1.1  minoura 
      3       1.1  minoura /*
      4       1.1  minoura  * Copyright (c) 2001 MINOURA Makoto.
      5       1.1  minoura  * All rights reserved.
      6       1.1  minoura  *
      7       1.1  minoura  * Redistribution and use in source and binary forms, with or without
      8       1.1  minoura  * modification, are permitted provided that the following conditions
      9       1.1  minoura  * are met:
     10       1.1  minoura  * 1. Redistributions of source code must retain the above copyright
     11       1.1  minoura  *    notice, this list of conditions and the following disclaimer.
     12       1.1  minoura  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1  minoura  *    notice, this list of conditions and the following disclaimer in the
     14       1.1  minoura  *    documentation and/or other materials provided with the distribution.
     15       1.1  minoura  *
     16       1.1  minoura  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17       1.1  minoura  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18       1.1  minoura  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19       1.1  minoura  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20       1.1  minoura  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21       1.1  minoura  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22       1.1  minoura  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23       1.1  minoura  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24       1.1  minoura  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25       1.1  minoura  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26       1.1  minoura  */
     27       1.1  minoura 
     28       1.1  minoura #include <sys/param.h>
     29       1.1  minoura #include <sys/disklabel.h>
     30  1.6.28.1       ad #include <machine/stdarg.h>
     31       1.1  minoura #include <lib/libkern/libkern.h>
     32       1.1  minoura #include <lib/libsa/stand.h>
     33       1.1  minoura 
     34  1.6.28.1       ad #include "libx68k.h"
     35       1.1  minoura #include "sdcdvar.h"
     36       1.1  minoura #include "iocs.h"
     37       1.1  minoura 
     38       1.1  minoura 
     39       1.1  minoura static int current_id = -1;
     40       1.1  minoura static int current_blklen, current_devsize, current_npart;
     41       1.1  minoura static struct boot_partinfo partitions[MAXPARTITIONS];
     42       1.1  minoura 
     43       1.1  minoura static int readdisklabel(int);
     44       1.1  minoura static int check_unit(int);
     45       1.1  minoura 
     46       1.1  minoura #ifdef DEBUG
     47       1.1  minoura #define DPRINTF(x)	printf x
     48       1.1  minoura #else
     49       1.1  minoura #define DPRINTF(x)
     50       1.1  minoura #endif
     51       1.1  minoura 
     52       1.1  minoura static int
     53       1.1  minoura check_unit(int id)
     54       1.1  minoura {
     55       1.1  minoura #define BUFFER_SIZE	8192
     56       1.1  minoura 	int error;
     57       1.1  minoura 	void *buffer = alloca(BUFFER_SIZE);
     58       1.1  minoura 
     59       1.1  minoura 	if (current_id == id)
     60       1.1  minoura 		return 0;
     61       1.1  minoura 
     62       1.1  minoura 	current_id = -1;
     63       1.1  minoura 
     64       1.1  minoura 	error = IOCS_S_TESTUNIT(id);
     65       1.1  minoura 	if (error < 0) {			/* not ready */
     66       1.1  minoura 		error = ENXIO;
     67       1.1  minoura 		goto out;
     68       1.1  minoura 	}
     69       1.1  minoura 
     70       1.1  minoura 	{
     71       1.1  minoura 		struct iocs_inquiry *inqdata = buffer;
     72       1.1  minoura 
     73       1.1  minoura 		error = IOCS_S_INQUIRY(100, id, inqdata);
     74       1.1  minoura 		if (error < 0) {		/* WHY??? */
     75       1.1  minoura 			error = ENXIO;
     76       1.1  minoura 			goto out;
     77       1.1  minoura 		}
     78       1.1  minoura 		if ((inqdata->unit != 0) &&	/* direct */
     79       1.1  minoura 		    (inqdata->unit != 7)) {	/* optical */
     80       1.1  minoura 			error = EUNIT;
     81       1.1  minoura 			goto out;
     82       1.1  minoura 		}
     83       1.1  minoura 	}
     84       1.1  minoura 
     85       1.1  minoura 	{
     86       1.1  minoura 		struct iocs_readcap *rcdata = buffer;
     87       1.1  minoura 
     88       1.1  minoura 		error = IOCS_S_READCAP(id, rcdata);
     89       1.1  minoura 		if (error < 0) {		/* WHY??? */
     90       1.1  minoura 			error = EUNIT;
     91       1.1  minoura 			goto out;
     92       1.1  minoura 		}
     93       1.1  minoura 		current_blklen = rcdata->size >> 9;
     94       1.1  minoura 		current_devsize = rcdata->block;
     95       1.1  minoura 	}
     96       1.1  minoura 
     97       1.1  minoura 	{
     98       1.1  minoura 		error = IOCS_S_READ(0, 1, id, current_blklen, buffer);
     99       1.1  minoura 		if (error < 0) {
    100       1.1  minoura 			error =  EIO;
    101       1.1  minoura 			goto out;
    102       1.1  minoura 		}
    103  1.6.28.1       ad 		if (strncmp((char *)buffer, "X68SCSI1", 8) != 0) {
    104       1.1  minoura 			error = EUNLAB;
    105       1.1  minoura 			goto out;
    106       1.1  minoura 		}
    107       1.1  minoura 	}
    108       1.1  minoura 
    109       1.1  minoura  out:
    110       1.1  minoura 	return error;
    111       1.1  minoura }
    112       1.1  minoura 
    113       1.1  minoura static int
    114  1.6.28.1       ad readdisklabel(int id)
    115       1.1  minoura {
    116       1.1  minoura 	int error, i;
    117       1.1  minoura 	char *buffer;
    118       1.1  minoura 	struct disklabel *label;
    119       1.1  minoura 	struct dos_partition *parttbl;
    120       1.1  minoura 
    121       1.1  minoura 	if (current_id == id)
    122       1.1  minoura 		return 0;
    123       1.1  minoura 	current_id = -1;
    124       1.1  minoura 
    125       1.1  minoura 	error = check_unit(id);
    126       1.1  minoura 	if (error)
    127       1.1  minoura 		return error;
    128       1.1  minoura 	if (current_blklen > 4) {
    129       1.1  minoura 		printf ("FATAL: Unsupported block size %d.\n",
    130       1.1  minoura 			256 << current_blklen);
    131       1.1  minoura 		return ERDLAB;
    132       1.1  minoura 	}
    133       1.1  minoura 
    134       1.1  minoura 	/* Try BSD disklabel first */
    135       1.1  minoura 	buffer = alloca(2048);
    136       1.1  minoura 	error = IOCS_S_READ(LABELSECTOR, 1, id, current_blklen, buffer);
    137       1.1  minoura 	if (error < 0)
    138       1.1  minoura 		return EIO;
    139  1.6.28.1       ad 	label = (void *)(buffer + LABELOFFSET);
    140       1.1  minoura 	if (label->d_magic == DISKMAGIC &&
    141       1.1  minoura 	    label->d_magic2 == DISKMAGIC) {
    142       1.1  minoura 		for (i = 0; i < label->d_npartitions; i++) {
    143       1.1  minoura 			partitions[i].start = label->d_partitions[i].p_offset;
    144       1.1  minoura 			partitions[i].size  = label->d_partitions[i].p_size;
    145       1.1  minoura 		}
    146       1.1  minoura 		current_npart = label->d_npartitions;
    147       1.1  minoura 
    148       1.3  minoura 		goto done;
    149       1.1  minoura 	}
    150       1.1  minoura 
    151       1.1  minoura 	/* Try Human68K-style partition table */
    152       1.1  minoura #if 0
    153       1.1  minoura 	/* assumes 512byte/sec */
    154       1.1  minoura 	error = IOCS_S_READ(DOSPARTOFF, 2, id, current_blklen, buffer);
    155       1.1  minoura #else
    156       1.1  minoura 	error = IOCS_S_READ(8 >> current_blklen, 8 >> current_blklen,
    157       1.1  minoura 			    id, current_blklen, buffer);
    158       1.1  minoura #endif
    159       1.1  minoura 	if (error < 0)
    160       1.1  minoura 		return EIO;
    161  1.6.28.1       ad 	parttbl = (void *)(buffer + DOSBBSECTOR);
    162  1.6.28.1       ad 	if (strncmp(buffer, "X68K", 4) != 0)
    163       1.1  minoura 		return EUNLAB;
    164       1.1  minoura 	parttbl++;
    165       1.1  minoura 	for (current_npart = 0, i = 0;
    166       1.1  minoura 	     current_npart < MAXPARTITIONS && i < 15 && parttbl[i].dp_size;
    167       1.1  minoura 	     i++) {
    168       1.1  minoura 		partitions[current_npart].start
    169       1.1  minoura 			= parttbl[i].dp_start * 2;
    170       1.1  minoura 		partitions[current_npart].size
    171       1.1  minoura 			= parttbl[i].dp_size  * 2;
    172       1.1  minoura 		if (++current_npart == RAW_PART) {
    173       1.1  minoura 			partitions[current_npart].start = 0;
    174       1.1  minoura 			partitions[current_npart].size = -1; /* XXX */
    175       1.1  minoura 			current_npart++;
    176       1.1  minoura 		}
    177       1.1  minoura 	}
    178       1.3  minoura done:
    179       1.1  minoura #ifdef DEBUG
    180       1.1  minoura 	for (i = 0; i < current_npart; i++) {
    181       1.1  minoura 		printf ("%d: starts %d, size %d\n", i,
    182       1.1  minoura 			partitions[i].start,
    183       1.1  minoura 			partitions[i].size);
    184       1.1  minoura 	}
    185       1.1  minoura #endif
    186       1.1  minoura 	current_id = id;
    187       1.1  minoura 
    188       1.1  minoura 	return 0;
    189       1.1  minoura }
    190       1.1  minoura 
    191       1.1  minoura int
    192  1.6.28.1       ad sd_getbsdpartition(int id, int humanpart)
    193       1.1  minoura {
    194       1.1  minoura 	int error, i;
    195       1.2  minoura 	char *buffer;
    196       1.2  minoura 	struct dos_partition *parttbl;
    197       1.2  minoura 	unsigned parttop;
    198       1.2  minoura 
    199       1.2  minoura 	if (humanpart < 2)
    200       1.2  minoura 		humanpart++;
    201       1.1  minoura 
    202       1.1  minoura 	error = readdisklabel(id);
    203       1.1  minoura 	if (error) {
    204  1.6.28.1       ad 		printf("Reading disklabel: %s\n", strerror(error));
    205       1.1  minoura 		return -1;
    206       1.1  minoura 	}
    207       1.2  minoura 	buffer = alloca(2048);
    208       1.2  minoura 	error = IOCS_S_READ(8 >> current_blklen, 8 >> current_blklen,
    209       1.2  minoura 			    id, current_blklen, buffer);
    210       1.2  minoura 	if (error < 0) {
    211  1.6.28.1       ad 		printf("Reading partition table: %s\n", strerror(error));
    212       1.2  minoura 		return -1;
    213       1.2  minoura 	}
    214  1.6.28.1       ad 	parttbl = (void *)(buffer + DOSBBSECTOR);
    215  1.6.28.1       ad 	if (strncmp(buffer, "X68K", 4) != 0)
    216       1.2  minoura 		return 0;
    217       1.2  minoura 	parttop = parttbl[humanpart].dp_start;
    218  1.6.28.1       ad 	parttop = parttop << (2 - current_blklen);
    219       1.1  minoura 
    220       1.1  minoura 	for (i = 0; i < current_npart; i++) {
    221       1.1  minoura 		if (partitions[i].start == parttop)
    222       1.1  minoura 			return i;
    223       1.1  minoura 	}
    224       1.1  minoura 
    225  1.6.28.1       ad 	printf("Could not determine the boot partition.\n");
    226       1.1  minoura 
    227       1.1  minoura 	return -1;
    228       1.1  minoura }
    229       1.1  minoura 
    230       1.1  minoura struct sdcd_softc {
    231       1.1  minoura 	int			sc_part;
    232       1.1  minoura 	struct boot_partinfo	sc_partinfo;
    233       1.1  minoura 	int			sc_blocksize;
    234       1.1  minoura };
    235       1.1  minoura 
    236  1.6.28.1       ad /* sdopen(struct open_file *f, int id, int part) */
    237       1.1  minoura int
    238  1.6.28.1       ad sdopen(struct open_file *f, ...)
    239       1.1  minoura {
    240       1.1  minoura 	int error;
    241       1.1  minoura 	struct sdcd_softc *sc;
    242  1.6.28.1       ad 	int id, part;
    243  1.6.28.1       ad 	va_list ap;
    244  1.6.28.1       ad 
    245  1.6.28.1       ad 	va_start(ap, f);
    246  1.6.28.1       ad 	id   = va_arg(ap, int);
    247  1.6.28.1       ad 	part = va_arg(ap, int);
    248  1.6.28.1       ad 	va_end(ap);
    249       1.1  minoura 
    250       1.1  minoura 	if (id < 0 || id > 7)
    251       1.1  minoura 		return ENXIO;
    252       1.1  minoura 	if (current_id != id) {
    253       1.1  minoura 		error = readdisklabel(id);
    254       1.1  minoura 		if (error)
    255       1.1  minoura 			return error;
    256       1.1  minoura 	}
    257       1.1  minoura 	if (part >= current_npart)
    258       1.1  minoura 		return ENXIO;
    259       1.1  minoura 
    260  1.6.28.1       ad 	sc = alloc(sizeof(struct sdcd_softc));
    261       1.1  minoura 	sc->sc_part = part;
    262       1.1  minoura 	sc->sc_partinfo = partitions[part];
    263       1.1  minoura 	sc->sc_blocksize = current_blklen << 9;
    264       1.1  minoura 	f->f_devdata = sc;
    265       1.1  minoura 	return 0;
    266       1.1  minoura }
    267       1.1  minoura 
    268       1.1  minoura int
    269  1.6.28.1       ad sdclose(struct open_file *f)
    270       1.1  minoura {
    271  1.6.28.1       ad 	dealloc(f->f_devdata, sizeof(struct sdcd_softc));
    272       1.1  minoura 	return 0;
    273       1.1  minoura }
    274       1.1  minoura 
    275       1.1  minoura int
    276  1.6.28.1       ad sdstrategy(void *arg, int rw, daddr_t dblk, size_t size,
    277  1.6.28.1       ad            void *buf, size_t *rsize)
    278       1.1  minoura {
    279       1.1  minoura 	struct sdcd_softc *sc = arg;
    280       1.1  minoura 	u_int32_t	start = sc->sc_partinfo.start + dblk;
    281       1.1  minoura 	size_t		nblks;
    282       1.1  minoura 	int		error;
    283       1.1  minoura 
    284       1.1  minoura 	if (size == 0) {
    285       1.1  minoura 		if (rsize)
    286       1.1  minoura 			*rsize = 0;
    287       1.1  minoura 		return 0;
    288       1.1  minoura 	}
    289  1.6.28.1       ad 	nblks = howmany(size, 256 << current_blklen);
    290       1.1  minoura 
    291       1.4  minoura 	if ((dblk & 0x1fffff) == 0x1fffff && (nblks & 0xff) == nblks) {
    292       1.1  minoura 		if (rw & F_WRITE)
    293  1.6.28.1       ad 			error = IOCS_S_WRITE(start, nblks, current_id,
    294  1.6.28.1       ad 			                     current_blklen, buf);
    295       1.1  minoura 		else
    296  1.6.28.1       ad 			error = IOCS_S_READ(start, nblks, current_id,
    297  1.6.28.1       ad 			                    current_blklen, buf);
    298       1.1  minoura 	} else {
    299       1.1  minoura 		if (rw & F_WRITE)
    300  1.6.28.1       ad 			error = IOCS_S_WRITEEXT(start, nblks, current_id,
    301  1.6.28.1       ad 			                        current_blklen, buf);
    302       1.1  minoura 		else
    303  1.6.28.1       ad 			error = IOCS_S_READEXT(start, nblks, current_id,
    304  1.6.28.1       ad 			                       current_blklen, buf);
    305       1.1  minoura 	}
    306       1.1  minoura 	if (error < 0)
    307       1.1  minoura 		return EIO;
    308       1.1  minoura 
    309       1.1  minoura 	if (rsize)
    310       1.1  minoura 		*rsize = size;
    311       1.1  minoura 	return 0;
    312       1.1  minoura }
    313       1.1  minoura 
    314  1.6.28.1       ad /* cdopen(struct open_file *f, int id, int part) */
    315       1.1  minoura int
    316  1.6.28.1       ad cdopen(struct open_file *f, ...)
    317       1.1  minoura {
    318       1.1  minoura 	int error;
    319       1.1  minoura 	struct sdcd_softc *sc;
    320  1.6.28.1       ad 	int id, part;
    321  1.6.28.1       ad 	va_list ap;
    322  1.6.28.1       ad 
    323  1.6.28.1       ad 	va_start(ap, f);
    324  1.6.28.1       ad 	id   = va_arg(ap, int);
    325  1.6.28.1       ad 	part = va_arg(ap, int);
    326  1.6.28.1       ad 	va_end(ap);
    327       1.1  minoura 
    328       1.1  minoura 	if (id < 0 || id > 7)
    329       1.1  minoura 		return ENXIO;
    330       1.1  minoura 	if (part == 0 || part == 2)
    331       1.1  minoura 		return ENXIO;
    332       1.1  minoura 	if (current_id != id) {
    333       1.1  minoura 		error = check_unit(id);
    334       1.1  minoura 		if (error)
    335       1.1  minoura 			return error;
    336       1.1  minoura 	}
    337       1.1  minoura 
    338  1.6.28.1       ad 	sc = alloc(sizeof(struct sdcd_softc));
    339       1.1  minoura 	current_npart = 3;
    340       1.1  minoura 	sc->sc_part = 0;
    341       1.1  minoura 	sc->sc_partinfo.size = sc->sc_partinfo.size = current_devsize;
    342       1.1  minoura 	sc->sc_blocksize = current_blklen << 9;
    343       1.1  minoura 	f->f_devdata = sc;
    344       1.1  minoura 	return 0;
    345       1.1  minoura }
    346       1.1  minoura 
    347       1.1  minoura int
    348  1.6.28.1       ad cdclose(struct open_file *f)
    349       1.1  minoura {
    350  1.6.28.1       ad 	dealloc(f->f_devdata, sizeof(struct sdcd_softc));
    351       1.1  minoura 	return 0;
    352       1.1  minoura }
    353       1.1  minoura 
    354       1.1  minoura int
    355  1.6.28.1       ad cdstrategy(void *arg, int rw, daddr_t dblk, size_t size,
    356  1.6.28.1       ad            void *buf, size_t *rsize)
    357       1.1  minoura {
    358       1.1  minoura 	struct sdcd_softc *sc = arg;
    359       1.1  minoura 
    360  1.6.28.1       ad 	return sdstrategy(arg, rw, dblk * DEV_BSIZE / sc->sc_blocksize,
    361  1.6.28.1       ad 	                  size, buf, rsize);
    362       1.1  minoura }
    363