rz.c revision 1.14
1/*	$NetBSD: rz.c,v 1.14 1999/11/27 03:09:42 simonb Exp $	*/
2
3/*
4 * Copyright (c) 1992, 1993
5 *	The Regents of the University of California.  All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Van Jacobson of Lawrence Berkeley Laboratory and Ralph Campbell.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 *    must display the following acknowledgement:
20 *	This product includes software developed by the University of
21 *	California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 *    may be used to endorse or promote products derived from this software
24 *    without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 *	@(#)rz.c	8.1 (Berkeley) 6/10/93
39 */
40
41#include <stand.h>
42#include <sys/param.h>
43#include <sys/disklabel.h>
44#include <machine/dec_prom.h>
45#include <machine/stdarg.h>
46#include <common.h>
47#include <rz.h>
48
49struct	rz_softc {
50	int	sc_fd;			/* PROM file id */
51	int	sc_ctlr;		/* controller number */
52	int	sc_unit;		/* disk unit number */
53	int	sc_part;		/* disk partition number */
54	struct	disklabel sc_label;	/* disk label for this disk */
55};
56
57int
58rzstrategy(devdata, rw, bn, reqcnt, addr, cnt)
59	void *devdata;
60	int rw;
61	daddr_t bn;
62	size_t reqcnt;
63	void *addr;
64	size_t *cnt;	/* out: number of bytes transfered */
65{
66	register struct rz_softc *sc = (struct rz_softc *)devdata;
67	register int part = sc->sc_part;
68	register struct partition *pp = &sc->sc_label.d_partitions[part];
69	register int s;
70	long offset;
71
72	offset = bn * DEV_BSIZE;
73
74	/*
75	 * Partial-block transfers not handled.
76	 */
77	if (reqcnt & (DEV_BSIZE - 1)) {
78		*cnt = 0;
79		return (EINVAL);
80	}
81
82	offset += pp->p_offset * DEV_BSIZE;
83
84	if (callv == &callvec) {
85		/* No REX on this machine */
86		if (prom_lseek(sc->sc_fd, offset, 0) < 0)
87			return (EIO);
88		s = prom_read(sc->sc_fd, addr, reqcnt);
89	} else
90		s = bootread (offset / 512, addr, reqcnt);
91	if (s < 0)
92		return (EIO);
93
94	*cnt = s;
95	return (0);
96}
97
98int
99rzopen(struct open_file *f, ...)
100{
101	register int ctlr, unit, part;
102
103	register struct rz_softc *sc;
104	register struct disklabel *lp;
105	register int i;
106	char *msg;
107	char buf[DEV_BSIZE];
108	int cnt;
109	static char device[] = "rz(0,0,0)";
110	va_list ap;
111
112	va_start(ap, f);
113
114	ctlr = va_arg(ap, int);
115	unit = va_arg(ap, int);
116	part = va_arg(ap, int);
117	if (unit >= 8 || part >= 8)
118		return (ENXIO);
119	device[5] = '0' + unit;
120	/* NOTE: only support reads for now */
121	/* Another NOTE: bootinit on the TurboChannel doesn't look at
122	   the device string - it's provided for compatibility with
123	   the DS3100 PROMs.   As a consequence, it may be possible to
124	   boot from some other drive with these bootblocks on the 3100,
125	   but will not be possible on any TurboChannel machine. */
126
127	if (callv == &callvec)
128		i = prom_open(device, 0);
129	else
130		i = bootinit (device);
131	if (i < 0) {
132		printf("open failed\n");
133		return (ENXIO);
134	}
135
136	sc = alloc(sizeof(struct rz_softc));
137	memset(sc, 0, sizeof(struct rz_softc));
138	f->f_devdata = (void *)sc;
139
140	sc->sc_fd = i;
141	sc->sc_ctlr = ctlr;
142	sc->sc_unit = unit;
143	sc->sc_part = part;
144
145	/* try to read disk label and partition table information */
146	lp = &sc->sc_label;
147	lp->d_secsize = DEV_BSIZE;
148	lp->d_secpercyl = 1;
149	lp->d_npartitions = MAXPARTITIONS;
150	lp->d_partitions[part].p_offset = 0;
151	lp->d_partitions[part].p_size = 0x7fffffff;
152	i = rzstrategy(sc, F_READ, (daddr_t)LABELSECTOR, DEV_BSIZE, buf, &cnt);
153	if (i || cnt != DEV_BSIZE) {
154		printf("rz%d: error reading disk label\n", unit);
155		goto bad;
156	} else {
157		msg = getdisklabel(buf, lp);
158		if (msg) {
159			printf("rz%d: %s\n", unit, msg);
160			goto bad;
161		}
162	}
163
164	if (part >= lp->d_npartitions || lp->d_partitions[part].p_size == 0) {
165	bad:
166#ifndef BOOTRZ	/* Smaller code for first stage disk bootloader */
167		free(sc, sizeof(struct rz_softc));
168#endif
169		return (ENXIO);
170	}
171	return (0);
172}
173
174#ifndef LIBSA_NO_DEV_CLOSE
175int
176rzclose(f)
177	struct open_file *f;
178{
179	if (callv == &callvec)
180		prom_close(((struct rz_softc *)f->f_devdata)->sc_fd);
181
182	free(f->f_devdata, sizeof(struct rz_softc));
183	f->f_devdata = (void *)0;
184	return (0);
185}
186#endif
187