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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
|
/* $NetBSD: sdcd.c,v 1.18 2022/06/23 12:32:22 isaki Exp $ */
/*
* Copyright (c) 2001 MINOURA Makoto.
* All rights reserved.
*
* 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 AUTHOR ``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 AUTHOR 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/bitops.h>
#include <sys/disklabel.h>
#include <lib/libkern/libkern.h>
#include <lib/libsa/stand.h>
#include "libx68k.h"
#include "sdcdvar.h"
#include "iocs.h"
static int current_id = -1;
static int current_blkbytes;
static int current_blkshift;
static int current_devsize, current_npart;
static struct boot_partinfo partitions[MAXPARTITIONS];
static uint human2blk(uint);
static uint human2bsd(uint);
static uint bsd2blk(uint);
static int readdisklabel(int);
static int check_unit(int);
#ifdef DEBUG
#define DPRINTF(x) printf x
#else
#define DPRINTF(x)
#endif
/*
* Convert the number of sectors on Human68k
* into the number of blocks on the current device.
*/
static uint
human2blk(uint n)
{
uint blk_per_sect;
/* Human68k uses 1024 byte/sector. */
blk_per_sect = 4 >> current_blkshift;
if (blk_per_sect == 0)
blk_per_sect = 1;
return blk_per_sect * n;
}
/*
* Convert the number of sectors on Human68k
* into the number of DEV_BSIZE sectors.
*/
static uint
human2bsd(uint n)
{
return n * (1024 / DEV_BSIZE);
}
/*
* Convert the number of DEV_BSIZE sectors
* into the number of blocks on the current device.
*/
static uint
bsd2blk(uint n)
{
return ((DEV_BSIZE / 256) * n) >> current_blkshift;
}
static int
check_unit(int id)
{
#define BUFFER_SIZE 8192
int error;
void *buffer = alloca(BUFFER_SIZE);
if (current_id == id)
return 0;
current_id = -1;
error = IOCS_S_TESTUNIT(id);
if (error < 0) { /* not ready */
error = ENXIO;
goto out;
}
{
struct iocs_inquiry *inqdata = buffer;
error = IOCS_S_INQUIRY(sizeof(*inqdata), id, inqdata);
if (error < 0) { /* WHY??? */
error = ENXIO;
goto out;
}
if ((inqdata->unit != 0) && /* direct */
(inqdata->unit != 5) && /* cdrom */
(inqdata->unit != 7)) { /* optical */
error = EUNIT;
goto out;
}
}
{
struct iocs_readcap *rcdata = buffer;
error = IOCS_S_READCAP(id, rcdata);
if (error < 0) { /* WHY??? */
error = EUNIT;
goto out;
}
current_blkbytes = rcdata->size;
current_blkshift = fls32(current_blkbytes) - 9;
current_devsize = rcdata->block;
}
{
error = IOCS_S_READ(0, 1, id, current_blkshift, buffer);
if (error < 0) {
error = EIO;
goto out;
}
if (strncmp((char *)buffer, "X68SCSI1", 8) != 0) {
error = EUNLAB;
goto out;
}
}
out:
return error;
}
static int
readdisklabel(int id)
{
int error, i;
char *buffer;
struct disklabel *label;
struct dos_partition *parttbl;
if (current_id == id)
return 0;
current_id = -1;
error = check_unit(id);
if (error)
return error;
if (current_blkbytes > 2048) {
printf("FATAL: Unsupported block size %d.\n",
current_blkbytes);
return ERDLAB;
}
/* Try BSD disklabel first */
buffer = alloca(2048);
error = IOCS_S_READ(LABELSECTOR, 1, id, current_blkshift, buffer);
if (error < 0)
return EIO;
label = (void *)(buffer + LABELOFFSET);
if (label->d_magic == DISKMAGIC &&
label->d_magic2 == DISKMAGIC) {
for (i = 0; i < label->d_npartitions; i++) {
partitions[i].start = label->d_partitions[i].p_offset;
partitions[i].size = label->d_partitions[i].p_size;
}
current_npart = label->d_npartitions;
goto done;
}
/* Try Human68K-style partition table */
error = IOCS_S_READ(human2blk(2), 1, id, current_blkshift, buffer);
if (error < 0)
return EIO;
parttbl = (void *)(buffer + DOSBBSECTOR);
if (strncmp(buffer, "X68K", 4) != 0)
return EUNLAB;
parttbl++;
for (current_npart = 0, i = 0;
current_npart < MAXPARTITIONS && i < 15 && parttbl[i].dp_size;
i++) {
partitions[current_npart].start
= human2bsd(parttbl[i].dp_start);
partitions[current_npart].size
= human2bsd(parttbl[i].dp_size);
if (++current_npart == RAW_PART) {
partitions[current_npart].start = 0;
partitions[current_npart].size = -1; /* XXX */
current_npart++;
}
}
done:
#ifdef DEBUG
for (i = 0; i < current_npart; i++) {
printf ("%d: starts %d, size %d\n", i,
partitions[i].start,
partitions[i].size);
}
#endif
current_id = id;
return 0;
}
int
sd_getbsdpartition(int id, int humanpart)
{
int error, i;
char *buffer;
struct dos_partition *parttbl;
unsigned parttop;
if (humanpart < 2)
humanpart++;
error = readdisklabel(id);
if (error) {
printf("Reading disklabel: %s\n", strerror(error));
return -1;
}
buffer = alloca(2048);
error = IOCS_S_READ(human2blk(2), 1, id, current_blkshift, buffer);
if (error < 0) {
printf("Reading partition table: %s\n", strerror(error));
return -1;
}
parttbl = (void *)(buffer + DOSBBSECTOR);
if (strncmp(buffer, "X68K", 4) != 0)
return 0;
parttop = human2bsd(parttbl[humanpart].dp_start);
for (i = 0; i < current_npart; i++) {
if (partitions[i].start == parttop)
return i;
}
printf("Could not determine the boot partition.\n");
return -1;
}
struct sdcd_softc {
int sc_part;
struct boot_partinfo sc_partinfo;
};
/* sdopen(struct open_file *f, int id, int part) */
int
sdopen(struct open_file *f, ...)
{
int error;
struct sdcd_softc *sc;
int id, part;
va_list ap;
va_start(ap, f);
id = va_arg(ap, int);
part = va_arg(ap, int);
va_end(ap);
if (id < 0 || id > 7)
return ENXIO;
if (current_id != id) {
error = readdisklabel(id);
if (error)
return error;
}
if (part >= current_npart)
return ENXIO;
sc = alloc(sizeof(struct sdcd_softc));
sc->sc_part = part;
sc->sc_partinfo = partitions[part];
f->f_devdata = sc;
return 0;
}
int
sdcdclose(struct open_file *f)
{
dealloc(f->f_devdata, sizeof(struct sdcd_softc));
return 0;
}
int
sdcdstrategy(void *arg, int rw, daddr_t dblk, size_t size,
void *buf, size_t *rsize)
{
struct sdcd_softc *sc = arg;
uint32_t start = sc->sc_partinfo.start + dblk;
size_t nblks;
int error;
if (size == 0) {
if (rsize)
*rsize = 0;
return 0;
}
start = bsd2blk(start);
nblks = howmany(size, current_blkbytes);
if (start < 0x200000 && nblks < 256) {
if (rw & F_WRITE)
error = IOCS_S_WRITE(start, nblks, current_id,
current_blkshift, buf);
else
error = IOCS_S_READ(start, nblks, current_id,
current_blkshift, buf);
} else {
if (rw & F_WRITE)
error = IOCS_S_WRITEEXT(start, nblks, current_id,
current_blkshift, buf);
else
error = IOCS_S_READEXT(start, nblks, current_id,
current_blkshift, buf);
}
if (error < 0)
return EIO;
if (rsize)
*rsize = size;
return 0;
}
/* cdopen(struct open_file *f, int id, int part) */
int
cdopen(struct open_file *f, ...)
{
int error;
struct sdcd_softc *sc;
int id, part;
va_list ap;
va_start(ap, f);
id = va_arg(ap, int);
part = va_arg(ap, int);
va_end(ap);
if (id < 0 || id > 7)
return ENXIO;
if (part != 0 && part != 2)
return ENXIO;
if (current_id != id) {
error = check_unit(id);
if (error)
return error;
}
sc = alloc(sizeof(struct sdcd_softc));
current_npart = 3;
sc->sc_part = 0;
sc->sc_partinfo.start = 0;
sc->sc_partinfo.size = current_devsize;
f->f_devdata = sc;
current_id = id;
return 0;
}
|