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