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