xref: /netbsd-src/sys/arch/alpha/stand/common/blkdev.c (revision 481fca6e59249d8ffcf24fef7cfbe7b131bfb080)
1 /* $NetBSD: blkdev.c,v 1.3 1999/11/13 21:38:20 thorpej Exp $ */
2 
3 /*
4  * Copyright (c) 1999 Christopher G. Demetriou.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *      This product includes software developed by Christopher G. Demetriou
17  *	for the NetBSD Project.
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 /*
34  * Copyright (c) 1992, 1993
35  *	The Regents of the University of California.  All rights reserved.
36  *
37  * This code is derived from software contributed to Berkeley by
38  * Van Jacobson of Lawrence Berkeley Laboratory and Ralph Campbell.
39  *
40  * Redistribution and use in source and binary forms, with or without
41  * modification, are permitted provided that the following conditions
42  * are met:
43  * 1. Redistributions of source code must retain the above copyright
44  *    notice, this list of conditions and the following disclaimer.
45  * 2. Redistributions in binary form must reproduce the above copyright
46  *    notice, this list of conditions and the following disclaimer in the
47  *    documentation and/or other materials provided with the distribution.
48  * 3. All advertising materials mentioning features or use of this software
49  *    must display the following acknowledgement:
50  *	This product includes software developed by the University of
51  *	California, Berkeley and its contributors.
52  * 4. Neither the name of the University nor the names of its contributors
53  *    may be used to endorse or promote products derived from this software
54  *    without specific prior written permission.
55  *
56  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
57  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
59  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
60  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
61  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
62  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
64  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
65  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66  * SUCH DAMAGE.
67  *
68  *	@(#)rz.c	8.1 (Berkeley) 6/10/93
69  */
70 
71 #include <lib/libsa/stand.h>
72 #include <lib/libkern/libkern.h>
73 
74 #include <sys/param.h>
75 #include <sys/disklabel.h>
76 
77 #include <machine/prom.h>
78 
79 #include "stand/common/common.h"
80 #include "blkdev.h"
81 
82 /*
83  * If BOOTXX_FS_TYPE is defined, we'll try to find and use the first
84  * partition with that type mentioned in the disklabel, else default to
85  * using block 0.
86  *
87  * The old boot blocks used to look for a file system starting at block
88  * 0.  It's not immediately obvious that change here is necessary or good,
89  * so for now we don't bother looking for the specific type.
90  */
91 #undef BOOTXX_FS_TYPE
92 
93 int		blkdev_is_open;
94 u_int32_t	blkdev_part_offset;
95 
96 /*
97  * Since we have only one device, and want to squeeze space, we just
98  * short-circuit devopen() to do the disk open as well.
99  *
100  * Devopen is supposed to decode the string 'fname', open the device,
101  * and make 'file' point to the remaining file name.  Since we don't
102  * do any device munging, we can just set *file to fname.
103  */
104 int
105 devopen(f, fname, file)
106 	struct open_file *f;
107 	const char *fname;
108 	char **file;	/* out */
109 {
110 #if defined(BOOTXX_FS_TYPE)
111 	int i;
112 	size_t cnt;
113 	char *msg, buf[DEV_BSIZE];
114 	struct disklabel l;
115 #endif /* defined(BOOTXX_FS_TYPE) */
116 
117 	if (blkdev_is_open)
118 		return (EBUSY);
119 
120 	*file = (char *)fname;
121 
122 #if 0
123 	f->f_devdata = NULL;			/* no point */
124 #endif
125 
126 	/* Try to read disk label and partition table information. */
127 	blkdev_part_offset = 0;
128 #if defined(BOOTXX_FS_TYPE)
129 	i = diskstrategy(NULL, F_READ,
130 	    (daddr_t)LABELSECTOR, DEV_BSIZE, buf, &cnt);
131 	if (i || cnt != DEV_BSIZE) {
132 		return (ENXIO);
133 	}
134 	msg = getdisklabel(buf, &l);
135 	if (msg == NULL) {
136 		/*
137 		 * there's a label.  find the first partition of the
138 		 * type we want and use its offset.  if none are
139 		 * found, we just use offset 0.
140 		 */
141 		for (i = 0; i < l.d_npartitions; i++) {
142 			if (l.d_partitions[i].p_fstype == BOOTXX_FS_TYPE) {
143 				blkdev_part_offset = l.d_partitions[i].p_offset;
144 				break;
145 			}
146 		}
147 	} else {
148 		/* just use offset 0; it's already set that way */
149 	}
150 #endif /* defined(BOOTXX_FS_TYPE) */
151 
152 	blkdev_is_open = 1;
153 	return (0);
154 }
155 
156 int
157 blkdevstrategy(devdata, rw, bn, reqcnt, addrvoid, cnt)
158 	void *devdata;
159 	int rw;
160 	daddr_t bn;
161 	size_t reqcnt;
162 	void *addrvoid;
163 	size_t *cnt;	/* out: number of bytes transfered */
164 {
165 	char *addr = addrvoid;
166 	prom_return_t ret;
167 
168 	if ((reqcnt & 0xffffff) != reqcnt ||
169 	    reqcnt == 0)
170 		asm("call_pal 0");
171 
172 	twiddle();
173 
174 	/* Partial-block transfers not handled. */
175 	if (reqcnt & (DEV_BSIZE - 1)) {
176 		*cnt = 0;
177 		return (EINVAL);
178 	}
179 
180 	ret.bits = prom_read(booted_dev_fd, reqcnt, addr,
181 	    bn + blkdev_part_offset);
182 	if (ret.u.status)
183 		return (EIO);
184 	*cnt = ret.u.retval;
185 	return (0);
186 }
187 
188 #if !defined(LIBSA_NO_FS_CLOSE)
189 int
190 blkdevclose(struct open_file *f)
191 {
192 
193 	blkdev_is_open = 0;
194 	return (0);
195 }
196 #endif /* !defined(LIBSA_NO_FS_CLOSE) */
197