blkdev.c revision 1.2.2.1 1 /* $NetBSD: blkdev.c,v 1.2.2.1 2004/08/03 10:40:00 skrll 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. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
51 *
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * SUCH DAMAGE.
63 *
64 * @(#)rz.c 8.1 (Berkeley) 6/10/93
65 */
66
67 #include <lib/libsa/stand.h>
68 #include <lib/libkern/libkern.h>
69
70 #include <sys/param.h>
71 #include <sys/disklabel.h>
72
73 #include "stand/common/cfe_api.h"
74
75 #include "stand/common/common.h"
76 #include "blkdev.h"
77
78 /*
79 * If BOOTXX_FS_TYPE is defined, we'll try to find and use the first
80 * partition with that type mentioned in the disklabel, else default to
81 * using block 0.
82 *
83 * The old boot blocks used to look for a file system starting at block
84 * 0. It's not immediately obvious that change here is necessary or good,
85 * so for now we don't bother looking for the specific type.
86 */
87 #undef BOOTXX_FS_TYPE
88
89 int blkdev_is_open;
90 u_int32_t blkdev_part_offset;
91
92 /*
93 * Since we have only one device, and want to squeeze space, we just
94 * short-circuit devopen() to do the disk open as well.
95 *
96 * Devopen is supposed to decode the string 'fname', open the device,
97 * and make 'file' point to the remaining file name. Since we don't
98 * do any device munging, we can just set *file to fname.
99 */
100 int
101 devopen(f, fname, file)
102 struct open_file *f;
103 const char *fname;
104 char **file; /* out */
105 {
106 #if defined(BOOTXX_FS_TYPE)
107 int i;
108 size_t cnt;
109 char *msg, buf[DEV_BSIZE];
110 struct disklabel l;
111 #endif /* defined(BOOTXX_FS_TYPE) */
112
113 if (blkdev_is_open) {
114 return (EBUSY);
115 }
116
117 *file = (char *)fname;
118
119 #if 0
120 f->f_devdata = NULL; /* no point */
121 #endif
122
123 /* Try to read disk label and partition table information. */
124 blkdev_part_offset = 0;
125 #if defined(BOOTXX_FS_TYPE)
126
127 i = diskstrategy(NULL, F_READ,
128 (daddr_t)LABELSECTOR, DEV_BSIZE, buf, &cnt);
129 if (i || cnt != DEV_BSIZE) {
130 return (ENXIO);
131 }
132 msg = getdisklabel(buf, &l);
133 if (msg == NULL) {
134 /*
135 * there's a label. find the first partition of the
136 * type we want and use its offset. if none are
137 * found, we just use offset 0.
138 */
139 for (i = 0; i < l.d_npartitions; i++) {
140 if (l.d_partitions[i].p_fstype == BOOTXX_FS_TYPE) {
141 blkdev_part_offset = l.d_partitions[i].p_offset;
142 break;
143 }
144 }
145 } else {
146 /* just use offset 0; it's already set that way */
147 }
148 #endif /* defined(BOOTXX_FS_TYPE) */
149
150 blkdev_is_open = 1;
151 return (0);
152 }
153
154 int
155 blkdevstrategy(devdata, rw, bn, reqcnt, addrvoid, cnt)
156 void *devdata;
157 int rw;
158 daddr_t bn;
159 size_t reqcnt;
160 void *addrvoid;
161 size_t *cnt; /* out: number of bytes transfered */
162 {
163 char *addr = addrvoid;
164 int res;
165
166 #if !defined(LIBSA_NO_TWIDDLE)
167 twiddle();
168 #endif
169
170 /* Partial-block transfers not handled. */
171 if (reqcnt & (DEV_BSIZE - 1)) {
172 *cnt = 0;
173 return (EINVAL);
174 }
175 res = cfe_readblk(booted_dev_fd,(bn+blkdev_part_offset)*DEV_BSIZE,addr,reqcnt);
176 if (res < 0) return EIO;
177
178 *cnt = res;
179 return (0);
180 }
181
182 #if !defined(LIBSA_NO_FS_CLOSE)
183 int
184 blkdevclose(struct open_file *f)
185 {
186
187 blkdev_is_open = 0;
188 return (0);
189 }
190 #endif /* !defined(LIBSA_NO_FS_CLOSE) */
191