1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
|
/* $OpenBSD: wd.c,v 1.12 2015/10/01 16:08:19 krw Exp $ */
/* $NetBSD: wd.c,v 1.5 2005/12/11 12:17:06 christos Exp $ */
/*-
* Copyright (c) 2003 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Manuel Bouyer.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/param.h>
#include <sys/stdint.h>
#include "libsa.h"
#include "wdvar.h"
void wdprobe(void);
int wd_get_params(struct wd_softc *wd);
int wdgetdisklabel(struct wd_softc *wd);
void wdgetdefaultlabel(struct wd_softc *wd, struct disklabel *lp);
struct wd_softc wd_devs[NUNITS];
int wd_ndevs = -1;
void
wdprobe(void)
{
struct wd_softc *wd = wd_devs;
u_int chan, drive, unit = 0;
for (chan = 0; chan < PCIIDE_NUM_CHANNELS; chan++) {
if (wdc_init(wd, chan) != 0)
continue;
for (drive = 0; drive < wd->sc_channel.ndrives; drive++) {
wd->sc_unit = unit;
wd->sc_drive = drive;
if (wd_get_params(wd) != 0)
continue;
DPRINTF(("wd%d: channel %d drive %d\n",
unit, chan, drive));
unit++;
wd++;
}
}
wd_ndevs = unit;
}
/*
* Get drive parameters through 'device identify' command.
*/
int
wd_get_params(wd)
struct wd_softc *wd;
{
int error;
unsigned char buf[DEV_BSIZE];
if ((error = wdc_exec_identify(wd, buf)) != 0)
return (error);
wd->sc_params = *(struct ataparams *)buf;
/* 48-bit LBA addressing */
if ((wd->sc_params.atap_cmd2_en & ATAPI_CMD2_48AD) != 0) {
DPRINTF(("Drive supports LBA48.\n"));
#if defined(_ENABLE_LBA48)
wd->sc_flags |= WDF_LBA48;
#endif
}
/* Prior to ATA-4, LBA was optional. */
if ((wd->sc_params.atap_capabilities1 & WDC_CAP_LBA) != 0) {
DPRINTF(("Drive supports LBA.\n"));
wd->sc_flags |= WDF_LBA;
}
return (0);
}
/*
* Initialize disk label to the default value.
*/
void
wdgetdefaultlabel(wd, lp)
struct wd_softc *wd;
struct disklabel *lp;
{
memset(lp, 0, sizeof(struct disklabel));
lp->d_secsize = DEV_BSIZE;
lp->d_ntracks = wd->sc_params.atap_heads;
lp->d_nsectors = wd->sc_params.atap_sectors;
lp->d_ncylinders = wd->sc_params.atap_cylinders;
lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
if (strcmp(wd->sc_params.atap_model, "ST506") == 0)
lp->d_type = DTYPE_ST506;
else
lp->d_type = DTYPE_ESDI;
strncpy(lp->d_typename, wd->sc_params.atap_model, 16);
strncpy(lp->d_packname, "fictitious", 16);
if (wd->sc_capacity > UINT32_MAX)
DL_SETDSIZE(lp, UINT32_MAX);
else
DL_SETDSIZE(lp, wd->sc_capacity);
lp->d_flags = 0;
DL_SETPOFFSET(&lp->d_partitions[RAW_PART], 0);
DL_SETPSIZE(&lp->d_partitions[RAW_PART], DL_GETDSIZE(lp));
lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
lp->d_npartitions = MAXPARTITIONS;
lp->d_magic = DISKMAGIC;
lp->d_magic2 = DISKMAGIC;
lp->d_checksum = dkcksum(lp);
}
/*
* Read disk label from the device.
*/
int
wdgetdisklabel(wd)
struct wd_softc *wd;
{
char *msg;
int sector;
size_t rsize;
struct disklabel *lp;
unsigned char buf[DEV_BSIZE];
wdgetdefaultlabel(wd, &wd->sc_label);
/*
* Find OpenBSD Partition in DOS partition table.
*/
sector = 0;
if (wdstrategy(wd, F_READ, DOSBBSECTOR, DEV_BSIZE, buf, &rsize))
return EOFFSET;
if (*(u_int16_t *)&buf[DOSMBR_SIGNATURE_OFF] == DOSMBR_SIGNATURE) {
int i;
struct dos_partition *dp = (struct dos_partition *)buf;
/*
* Lookup OpenBSD slice. If there is none, go ahead
* and try to read the disklabel off sector #0.
*/
memcpy(dp, &buf[DOSPARTOFF], NDOSPART * sizeof(*dp));
for (i = 0; i < NDOSPART; i++) {
if (dp[i].dp_typ == DOSPTYP_OPENBSD) {
sector = letoh32(dp[i].dp_start);
break;
}
}
}
if (wdstrategy(wd, F_READ, sector + LABELSECTOR, DEV_BSIZE,
buf, &rsize))
return EOFFSET;
if ((msg = getdisklabel(buf + LABELOFFSET, &wd->sc_label)))
printf("wd%d: getdisklabel: %s\n", wd->sc_unit, msg);
lp = &wd->sc_label;
/* check partition */
if ((wd->sc_part >= lp->d_npartitions) ||
(lp->d_partitions[wd->sc_part].p_fstype == FS_UNUSED)) {
DPRINTF(("illegal partition\n"));
return (EPART);
}
DPRINTF(("label info: d_secsize %d, d_nsectors %d, d_ncylinders %d,"
"d_ntracks %d, d_secpercyl %d\n",
wd->sc_label.d_secsize,
wd->sc_label.d_nsectors,
wd->sc_label.d_ncylinders,
wd->sc_label.d_ntracks,
wd->sc_label.d_secpercyl));
return (0);
}
/*
* Open device (read drive parameters and disklabel)
*/
int
wdopen(struct open_file *f, ...)
{
int error;
va_list ap;
u_int unit, part, drive;
struct wd_softc *wd;
va_start(ap, f);
unit = va_arg(ap, u_int);
part = va_arg(ap, u_int);
va_end(ap);
DPRINTF(("wdopen: wd%d%c\n", unit, 'a' + part));
if (unit < 0 || unit >= NUNITS)
return (ENXIO);
if (wd_ndevs == -1)
wdprobe();
if (unit >= wd_ndevs)
return (ENXIO);
wd = &wd_devs[unit];
wd->sc_part = part;
if ((error = wdgetdisklabel(wd)) != 0)
return (error);
f->f_devdata = wd;
return (0);
}
/*
* Close device.
*/
int
wdclose(struct open_file *f)
{
return 0;
}
/*
* Read some data.
*/
int
wdstrategy(f, rw, dblk, size, buf, rsize)
void *f;
int rw;
daddr32_t dblk;
size_t size;
void *buf;
size_t *rsize;
{
int i, nsect;
daddr32_t blkno;
struct wd_softc *wd = f;
if (size == 0)
return (0);
if (rw != F_READ)
return EOPNOTSUPP;
nsect = howmany(size, wd->sc_label.d_secsize);
blkno = dblk + wd->sc_label.d_partitions[wd->sc_part].p_offset;
for (i = 0; i < nsect; i++, blkno++) {
int error;
if ((error = wdc_exec_read(wd, WDCC_READ, blkno, buf)) != 0)
return (error);
buf += wd->sc_label.d_secsize;
}
*rsize = size;
return (0);
}
|