disk.c revision 1.2.2.2 1 1.2.2.2 thorpej /* $NetBSD: disk.c,v 1.2.2.2 2002/01/10 19:48:35 thorpej Exp $ */
2 1.2.2.2 thorpej
3 1.2.2.2 thorpej /*
4 1.2.2.2 thorpej * Copyright (c) 1992, 1993
5 1.2.2.2 thorpej * The Regents of the University of California. All rights reserved.
6 1.2.2.2 thorpej *
7 1.2.2.2 thorpej * This code is derived from software contributed to Berkeley by
8 1.2.2.2 thorpej * Van Jacobson of Lawrence Berkeley Laboratory and Ralph Campbell.
9 1.2.2.2 thorpej *
10 1.2.2.2 thorpej * Redistribution and use in source and binary forms, with or without
11 1.2.2.2 thorpej * modification, are permitted provided that the following conditions
12 1.2.2.2 thorpej * are met:
13 1.2.2.2 thorpej * 1. Redistributions of source code must retain the above copyright
14 1.2.2.2 thorpej * notice, this list of conditions and the following disclaimer.
15 1.2.2.2 thorpej * 2. Redistributions in binary form must reproduce the above copyright
16 1.2.2.2 thorpej * notice, this list of conditions and the following disclaimer in the
17 1.2.2.2 thorpej * documentation and/or other materials provided with the distribution.
18 1.2.2.2 thorpej * 3. All advertising materials mentioning features or use of this software
19 1.2.2.2 thorpej * must display the following acknowledgement:
20 1.2.2.2 thorpej * This product includes software developed by the University of
21 1.2.2.2 thorpej * California, Berkeley and its contributors.
22 1.2.2.2 thorpej * 4. Neither the name of the University nor the names of its contributors
23 1.2.2.2 thorpej * may be used to endorse or promote products derived from this software
24 1.2.2.2 thorpej * without specific prior written permission.
25 1.2.2.2 thorpej *
26 1.2.2.2 thorpej * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 1.2.2.2 thorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 1.2.2.2 thorpej * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 1.2.2.2 thorpej * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 1.2.2.2 thorpej * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 1.2.2.2 thorpej * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 1.2.2.2 thorpej * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 1.2.2.2 thorpej * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 1.2.2.2 thorpej * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 1.2.2.2 thorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 1.2.2.2 thorpej * SUCH DAMAGE.
37 1.2.2.2 thorpej *
38 1.2.2.2 thorpej * @(#)disk.c 8.1 (Berkeley) 6/10/93
39 1.2.2.2 thorpej */
40 1.2.2.2 thorpej
41 1.2.2.2 thorpej #include <lib/libsa/stand.h>
42 1.2.2.2 thorpej #include <machine/stdarg.h>
43 1.2.2.2 thorpej
44 1.2.2.2 thorpej #include <sys/param.h>
45 1.2.2.2 thorpej #include <sys/disklabel.h>
46 1.2.2.2 thorpej
47 1.2.2.2 thorpej #include <dev/arcbios/arcbios.h>
48 1.2.2.2 thorpej
49 1.2.2.2 thorpej #include "common.h"
50 1.2.2.2 thorpej #include "disk.h"
51 1.2.2.2 thorpej
52 1.2.2.2 thorpej #define RF_PROTECTED_SECTORS 64 /* XXX refer to <.../rf_optnames.h> */
53 1.2.2.2 thorpej
54 1.2.2.2 thorpej extern const struct arcbios_fv *ARCBIOS;
55 1.2.2.2 thorpej
56 1.2.2.2 thorpej struct disk_softc {
57 1.2.2.2 thorpej int sc_fd; /* PROM file id */
58 1.2.2.2 thorpej int sc_part; /* disk partition number */
59 1.2.2.2 thorpej struct disklabel sc_label; /* disk label for this disk */
60 1.2.2.2 thorpej };
61 1.2.2.2 thorpej
62 1.2.2.2 thorpej int
63 1.2.2.2 thorpej diskstrategy(devdata, rw, bn, reqcnt, addr, cnt)
64 1.2.2.2 thorpej void *devdata;
65 1.2.2.2 thorpej int rw;
66 1.2.2.2 thorpej daddr_t bn;
67 1.2.2.2 thorpej size_t reqcnt;
68 1.2.2.2 thorpej void *addr;
69 1.2.2.2 thorpej size_t *cnt; /* out: number of bytes transfered */
70 1.2.2.2 thorpej {
71 1.2.2.2 thorpej struct disk_softc *sc = (struct disk_softc *)devdata;
72 1.2.2.2 thorpej int part = sc->sc_part;
73 1.2.2.2 thorpej struct partition *pp = &sc->sc_label.d_partitions[part];
74 1.2.2.2 thorpej int s;
75 1.2.2.2 thorpej int64_t offset;
76 1.2.2.2 thorpej int count;
77 1.2.2.2 thorpej
78 1.2.2.2 thorpej offset = bn;
79 1.2.2.2 thorpej
80 1.2.2.2 thorpej /*
81 1.2.2.2 thorpej * Partial-block transfers not handled.
82 1.2.2.2 thorpej */
83 1.2.2.2 thorpej if (reqcnt & (DEV_BSIZE - 1)) {
84 1.2.2.2 thorpej *cnt = 0;
85 1.2.2.2 thorpej return (EINVAL);
86 1.2.2.2 thorpej }
87 1.2.2.2 thorpej
88 1.2.2.2 thorpej offset += pp->p_offset;
89 1.2.2.2 thorpej
90 1.2.2.2 thorpej if (pp->p_fstype == FS_RAID)
91 1.2.2.2 thorpej offset += RF_PROTECTED_SECTORS;
92 1.2.2.2 thorpej
93 1.2.2.2 thorpej /*
94 1.2.2.2 thorpej * Convert from blocks to bytes.
95 1.2.2.2 thorpej */
96 1.2.2.2 thorpej offset *= DEV_BSIZE;
97 1.2.2.2 thorpej
98 1.2.2.2 thorpej s = ARCBIOS->Seek(sc->sc_fd, &offset, 0);
99 1.2.2.2 thorpej s = ARCBIOS->Read(sc->sc_fd, addr, reqcnt, &count);
100 1.2.2.2 thorpej
101 1.2.2.2 thorpej if (s < 0)
102 1.2.2.2 thorpej return (EIO);
103 1.2.2.2 thorpej
104 1.2.2.2 thorpej *cnt = count;
105 1.2.2.2 thorpej return (0);
106 1.2.2.2 thorpej }
107 1.2.2.2 thorpej
108 1.2.2.2 thorpej int
109 1.2.2.2 thorpej diskopen(struct open_file *f, ...)
110 1.2.2.2 thorpej {
111 1.2.2.2 thorpej int ctlr, unit, part;
112 1.2.2.2 thorpej
113 1.2.2.2 thorpej struct disk_softc *sc;
114 1.2.2.2 thorpej struct disklabel *lp;
115 1.2.2.2 thorpej #ifdef arc
116 1.2.2.2 thorpej char *msg, buf[DEV_BSIZE];
117 1.2.2.2 thorpej #endif
118 1.2.2.2 thorpej int i, cnt;
119 1.2.2.2 thorpej char *device;
120 1.2.2.2 thorpej va_list ap;
121 1.2.2.2 thorpej
122 1.2.2.2 thorpej va_start(ap, f);
123 1.2.2.2 thorpej
124 1.2.2.2 thorpej device = va_arg(ap, char *);
125 1.2.2.2 thorpej
126 1.2.2.2 thorpej /*
127 1.2.2.2 thorpej * For NetBSD/sgimips, since we use the SGI partition map directly,
128 1.2.2.2 thorpej * we fake an in-core NetBSD disklabel with offset of 0.
129 1.2.2.2 thorpej *
130 1.2.2.2 thorpej * For NetBSD/arc, there is a MBR partition map on the disk, which we
131 1.2.2.2 thorpej * then expect to find a NetBSD disklabel within the MBR partition.
132 1.2.2.2 thorpej * We require that the kernel be located in first partition in the
133 1.2.2.2 thorpej * NetBSD disklabel, because we have not other way to represent the
134 1.2.2.2 thorpej * root partition.
135 1.2.2.2 thorpej */
136 1.2.2.2 thorpej part = 0;
137 1.2.2.2 thorpej
138 1.2.2.2 thorpej if (part >= 16)
139 1.2.2.2 thorpej return (ENXIO);
140 1.2.2.2 thorpej
141 1.2.2.2 thorpej if (ARCBIOS->Open(device, 0, &i)) {
142 1.2.2.2 thorpej printf("open failed\n");
143 1.2.2.2 thorpej return (ENXIO);
144 1.2.2.2 thorpej }
145 1.2.2.2 thorpej
146 1.2.2.2 thorpej sc = alloc(sizeof(struct disk_softc));
147 1.2.2.2 thorpej memset(sc, 0, sizeof(struct disk_softc));
148 1.2.2.2 thorpej f->f_devdata = (void *)sc;
149 1.2.2.2 thorpej
150 1.2.2.2 thorpej sc->sc_fd = i;
151 1.2.2.2 thorpej sc->sc_part = part;
152 1.2.2.2 thorpej
153 1.2.2.2 thorpej /* try to read disk label and partition table information */
154 1.2.2.2 thorpej lp = &sc->sc_label;
155 1.2.2.2 thorpej lp->d_secsize = DEV_BSIZE;
156 1.2.2.2 thorpej lp->d_secpercyl = 1;
157 1.2.2.2 thorpej lp->d_npartitions = MAXPARTITIONS;
158 1.2.2.2 thorpej lp->d_partitions[part].p_offset = 0;
159 1.2.2.2 thorpej lp->d_partitions[part].p_size = 0x7fffffff;
160 1.2.2.2 thorpej
161 1.2.2.2 thorpej #ifdef arc
162 1.2.2.2 thorpej i = diskstrategy(sc, F_READ, (daddr_t)LABELSECTOR, DEV_BSIZE,
163 1.2.2.2 thorpej buf, &cnt);
164 1.2.2.2 thorpej if (i || cnt != DEV_BSIZE) {
165 1.2.2.2 thorpej printf("%s: error reading disk label\n", device);
166 1.2.2.2 thorpej free(sc, sizeof(struct disk_softc));
167 1.2.2.2 thorpej return (ENXIO);
168 1.2.2.2 thorpej }
169 1.2.2.2 thorpej msg = getdisklabel(buf, lp);
170 1.2.2.2 thorpej if (msg) {
171 1.2.2.2 thorpej /* If no label, just assume 0 and return */
172 1.2.2.2 thorpej return (0);
173 1.2.2.2 thorpej }
174 1.2.2.2 thorpej #endif
175 1.2.2.2 thorpej
176 1.2.2.2 thorpej if (part >= lp->d_npartitions || lp->d_partitions[part].p_size == 0) {
177 1.2.2.2 thorpej free(sc, sizeof(struct disk_softc));
178 1.2.2.2 thorpej return (ENXIO);
179 1.2.2.2 thorpej }
180 1.2.2.2 thorpej return (0);
181 1.2.2.2 thorpej }
182 1.2.2.2 thorpej
183 1.2.2.2 thorpej #ifndef LIBSA_NO_DEV_CLOSE
184 1.2.2.2 thorpej int
185 1.2.2.2 thorpej diskclose(f)
186 1.2.2.2 thorpej struct open_file *f;
187 1.2.2.2 thorpej {
188 1.2.2.2 thorpej ARCBIOS->Close(((struct disk_softc *)(f->f_devdata))->sc_fd);
189 1.2.2.2 thorpej free(f->f_devdata, sizeof(struct disk_softc));
190 1.2.2.2 thorpej f->f_devdata = (void *)0;
191 1.2.2.2 thorpej return (0);
192 1.2.2.2 thorpej }
193 1.2.2.2 thorpej #endif
194