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