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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
|
/*
* Storage Device.
*/
#include "u.h"
#include "mem.h"
#include "lib.h"
#include "dat.h"
#include "fns.h"
#include "io.h"
#include "ureg.h"
#include "error.h"
#include "sd.h"
#include "fs.h"
#define parttrace 0
extern SDifc* sdifc[];
static SDev* sdlist;
static SDunit** sdunit;
static int sdnunit;
static int _sdmask;
static int cdmask;
static int sdmask;
enum {
Rawcmd,
Rawdata,
Rawstatus,
};
void
sdaddpart(SDunit* unit, char* name, uvlong start, uvlong end)
{
SDpart *pp;
int i, partno;
if(parttrace)
print("add %d %s %s %lld %lld\n", unit->npart, unit->name, name, start, end);
/*
* Check name not already used
* and look for a free slot.
*/
if(unit->part != nil){
partno = -1;
for(i = 0; i < SDnpart; i++){
pp = &unit->part[i];
if(!pp->valid){
if(partno == -1)
partno = i;
break;
}
if(strcmp(name, pp->name) == 0){
if(pp->start == start && pp->end == end){
if(parttrace)
print("already present\n");
return;
}
}
}
}else{
if((unit->part = malloc(sizeof(SDpart)*SDnpart)) == nil){
if(parttrace)
print("malloc failed\n");
return;
}
partno = 0;
}
/*
* Check there is a free slot and size and extent are valid.
*/
if(partno == -1 || start > end || end > unit->sectors){
print("cannot add %s!%s [%llud,%llud) to disk [0,%llud): %s\n",
unit->name, name, start, end, unit->sectors,
partno==-1 ? "no free partitions" : "partition boundaries out of range");
return;
}
pp = &unit->part[partno];
pp->start = start;
pp->end = end;
strncpy(pp->name, name, NAMELEN);
pp->valid = 1;
unit->npart++;
}
void
sddelpart(SDunit* unit, char* name)
{
int i;
SDpart *pp;
if(parttrace)
print("del %d %s %s\n", unit->npart, unit->name, name);
/*
* Look for the partition to delete.
* Can't delete if someone still has it open.
* If it's the last valid partition zap the
* whole table.
*/
pp = unit->part;
for(i = 0; i < SDnpart; i++){
if(strncmp(name, pp->name, NAMELEN) == 0)
break;
pp++;
}
if(i >= SDnpart)
return;
pp->valid = 0;
unit->npart--;
if(unit->npart == 0){
free(unit->part);
unit->part = nil;
}
}
static int
sdinitpart(SDunit* unit)
{
unit->sectors = unit->secsize = 0;
unit->npart = 0;
if(unit->part){
free(unit->part);
unit->part = nil;
}
if(unit->inquiry[0] & 0xC0)
return 0;
switch(unit->inquiry[0] & 0x1F){
case 0x00: /* DA */
case 0x04: /* WORM */
case 0x05: /* CD-ROM */
case 0x07: /* MO */
break;
default:
return 0;
}
if(unit->dev->ifc->online == nil || unit->dev->ifc->online(unit) == 0)
return 0;
sdaddpart(unit, "data", 0, unit->sectors);
return 1;
}
static SDunit*
sdgetunit(SDev* sdev, int subno)
{
int index;
SDunit *unit;
/*
* Associate a unit with a given device and sub-unit
* number on that device.
* The device will be probed if it has not already been
* successfully accessed.
*/
qlock(&sdqlock);
index = sdev->index+subno;
unit = sdunit[index];
if(unit == nil){
if((unit = malloc(sizeof(SDunit))) == nil){
qunlock(&sdqlock);
return nil;
}
if(sdev->enabled == 0 && sdev->ifc->enable)
sdev->ifc->enable(sdev);
sdev->enabled = 1;
snprint(unit->name, NAMELEN, "sd%c%d", sdev->idno, subno);
unit->subno = subno;
unit->dev = sdev;
/*
* No need to lock anything here as this is only
* called before the unit is made available in the
* sdunit[] array.
*/
if(unit->dev->ifc->verify(unit) == 0){
qunlock(&sdqlock);
free(unit);
return nil;
}
sdunit[index] = unit;
}
qunlock(&sdqlock);
return unit;
}
static SDunit*
sdindex2unit(int index)
{
SDev *sdev;
/*
* Associate a unit with a given index into the top-level
* device directory.
* The device will be probed if it has not already been
* successfully accessed.
*/
for(sdev = sdlist; sdev != nil; sdev = sdev->next){
if(index >= sdev->index && index < sdev->index+sdev->nunit)
return sdgetunit(sdev, index-sdev->index);
}
return nil;
}
static void
_sddetach(void)
{
SDev *sdev;
for(sdev = sdlist; sdev != nil; sdev = sdev->next){
if(sdev->enabled == 0)
continue;
if(sdev->ifc->disable)
sdev->ifc->disable(sdev);
sdev->enabled = 0;
}
}
static void
sddump(void)
{
SDev *sdev;
print("sdevs:\n");
for(sdev = sdlist; sdev != nil; sdev = sdev->next){
print("sdev %c index %d nunit %d: ",
sdev->idno, sdev->index, sdev->nunit);
print("\n");
}
}
static int
_sdinit(void)
{
ulong m;
int i;
SDev *sdev, *tail;
SDunit *unit;
/*
* Probe all configured controllers and make a list
* of devices found, accumulating a possible maximum number
* of units attached and marking each device with an index
* into the linear top-level directory array of units.
*/
tail = nil;
for(i = 0; sdifc[i] != nil; i++){
if((sdev = sdifc[i]->pnp()) == nil)
continue;
if(sdlist != nil)
tail->next = sdev;
else
sdlist = sdev;
for(tail = sdev; tail->next != nil; tail = tail->next){
tail->index = sdnunit;
sdnunit += tail->nunit;
}
tail->index = sdnunit;
sdnunit += tail->nunit;
}
/*
* Legacy and option code goes here. This will be hard...
*/
/*
* The maximum number of possible units is known, allocate
* placeholders for their datastructures; the units will be
* probed and structures allocated when attached.
* Allocate controller names for the different types.
*/
if(sdnunit == 0)
return 0;
if((sdunit = malloc(sdnunit*sizeof(SDunit*))) == nil)
return 0;
sddetach = _sddetach;
for(i = 0; sdifc[i] != nil; i++){
if(sdifc[i]->id)
sdifc[i]->id(sdlist);
}
if (0)
sddump();
m = 0;
cdmask = sdmask = 0;
for(i=0; i<sdnunit && i < 32; i++) {
unit = sdindex2unit(i);
if(unit == nil)
continue;
sdinitpart(unit);
partition(unit);
if(unit->npart > 0){ /* BUG */
if((unit->inquiry[0] & 0x1F) == 0x05)
cdmask |= (1<<i);
else
sdmask |= (1<<i);
m |= (1<<i);
}
}
//notesdinfo();
_sdmask = m;
return m;
}
int
cdinit(void)
{
/* native access to disks seems to interfere with bios loading */
if(sdnunit == 0 && !biosload)
_sdinit();
return cdmask;
}
int
sdinit(void)
{
if(sdnunit == 0)
_sdinit();
return sdmask;
}
void
sdinitdev(int i, char *s)
{
SDunit *unit;
unit = sdindex2unit(i);
strcpy(s, unit->name);
}
void
sdprintdevs(int i)
{
char *s;
SDunit *unit;
unit = sdindex2unit(i);
for(i=0; i<unit->npart; i++){
s = unit->part[i].name;
if(strncmp(s, "dos", 3) == 0
|| strncmp(s, "9fat", 4) == 0
|| strncmp(s, "fs", 2) == 0)
print(" %s!%s", unit->name, s);
}
}
SDpart*
sdfindpart(SDunit *unit, char *name)
{
int i;
if(parttrace)
print("findpart %d %s %s\t\n", unit->npart, unit->name, name);
for(i=0; i<unit->npart; i++) {
if(parttrace)
print("%s...", unit->part[i].name);
if(strcmp(unit->part[i].name, name) == 0){
if(parttrace)
print("\n");
return &unit->part[i];
}
}
if(parttrace)
print("not found\n");
return nil;
}
typedef struct Scsicrud Scsicrud;
struct Scsicrud {
Fs fs;
vlong offset;
SDunit *unit;
SDpart *part;
};
long
sdread(Fs *vcrud, void *v, long n)
{
Scsicrud *crud;
long x;
crud = (Scsicrud*)vcrud;
x = sdbio(crud->unit, crud->part, v, n, crud->offset);
if(x > 0)
crud->offset += x;
return x;
}
vlong
sdseek(Fs *vcrud, vlong seek)
{
((Scsicrud*)vcrud)->offset = seek;
return seek;
}
void*
sdgetfspart(int i, char *s, int chatty)
{
SDunit *unit;
SDpart *p;
Scsicrud *crud;
if(cdmask&(1<<i)){
if(strcmp(s, "cdboot") != 0)
return nil;
}else if(sdmask&(1<<i)){
if(strcmp(s, "cdboot") == 0)
return nil;
}
unit = sdindex2unit(i);
if((p = sdfindpart(unit, s)) == nil){
if(chatty)
print("unknown partition %s!%s\n", unit->name, s);
return nil;
}
if(p->crud == nil) {
crud = malloc(sizeof(Scsicrud));
crud->fs.dev = i;
crud->fs.diskread = sdread;
crud->fs.diskseek = sdseek;
// crud->start = 0;
crud->unit = unit;
crud->part = p;
if(dosinit(&crud->fs) < 0 && dosinit(&crud->fs) < 0 && kfsinit(&crud->fs) < 0){
if(chatty)
print("partition %s!%s does not contain a DOS or KFS file system\n",
unit->name, s);
return nil;
}
p->crud = crud;
}
return p->crud;
}
/*
* Leave partitions around for devsd to pick up.
* (Needed by boot process; more extensive
* partitioning is done by termrc or cpurc).
*/
void
sdaddconf(int i)
{
SDunit *unit;
SDpart *pp;
unit = sdindex2unit(i);
/*
* If there were no partitions (just data and partition), don't bother.
*/
if(unit->npart<= 1 || (unit->npart==2 && strcmp(unit->part[1].name, "partition")==0))
return;
addconf("%spart=", unit->name);
for(i=1, pp=&unit->part[i]; i<unit->npart; i++, pp++) /* skip 0, which is "data" */
addconf("%s%s %lld %lld", i==1 ? "" : "/", pp->name,
pp->start, pp->end);
addconf("\n");
}
int
sdboot(int dev, char *pname, Boot *b)
{
char *file;
Fs *fs;
if((file = strchr(pname, '!')) == nil) {
print("syntax is sdC0!partition!file\n");
return -1;
}
*file++ = '\0';
fs = sdgetfspart(dev, pname, 1);
if(fs == nil)
return -1;
return fsboot(fs, file, b);
}
long
sdbio(SDunit *unit, SDpart *pp, void* va, long len, vlong off)
{
long l;
ulong offset;
uvlong bno, max, nb;
char *a;
static ulong bsz;
static uchar *b;
a = va;
memset(a, 0xDA, len);
qlock(&unit->ctl);
if(unit->changed){
qunlock(&unit->ctl);
return 0;
}
/*
* Check the request is within bounds.
* Removeable drives are locked throughout the I/O
* in case the media changes unexpectedly.
* Non-removeable drives are not locked during the I/O
* to allow the hardware to optimise if it can; this is
* a little fast and loose.
* It's assumed that non-removable media parameters
* (sectors, secsize) can't change once the drive has
* been brought online.
*/
if (unit->secsize == 0)
panic("sdbio: zero sector size");
bno = (off/unit->secsize) + pp->start;
nb = ((off+len+unit->secsize-1)/unit->secsize) + pp->start - bno;
max = SDmaxio/unit->secsize;
if(nb > max)
nb = max;
if(bno+nb > pp->end)
nb = pp->end - bno;
if(bno >= pp->end || nb == 0){
qunlock(&unit->ctl);
return 0;
}
if(!(unit->inquiry[1] & 0x80))
qunlock(&unit->ctl);
if(bsz < nb*unit->secsize){
b = malloc(nb*unit->secsize);
bsz = nb*unit->secsize;
}
// b = sdmalloc(nb*unit->secsize);
// if(b == nil)
// return 0;
offset = off % unit->secsize;
if((l = unit->dev->ifc->bio(unit, 0, 0, b, nb, bno)) < 0) {
// sdfree(b);
return 0;
}
if(l < offset)
len = 0;
else if(len > l - offset)
len = l - offset;
if(len)
memmove(a, b+offset, len);
// sdfree(b);
if(unit->inquiry[1] & 0x80)
qunlock(&unit->ctl);
return len;
}
#ifdef DMA
long
sdrio(SDreq *r, void* a, long n)
{
if(n >= SDmaxio || n < 0)
return 0;
r->data = nil;
if(n){
if((r->data = malloc(n)) == nil)
return 0;
if(r->write)
memmove(r->data, a, n);
}
r->dlen = n;
if(r->unit->dev->ifc->rio(r) != SDok){
// cgascreenputs("1", 1);
if(r->data != nil){
sdfree(r->data);
r->data = nil;
}
return 0;
}
// cgascreenputs("2", 1);
if(!r->write && r->rlen > 0)
memmove(a, r->data, r->rlen);
// cgascreenputs("3", 1);
if(r->data != nil){
sdfree(r->data);
r->data = nil;
}
// cgascreenputs("4", 1);
return r->rlen;
}
#endif /* DMA */
void
sleep(void*, int (*fn)(void*), void *v)
{
int x;
x = spllo();
while(!fn(v))
;
splx(x);
}
void
tsleep(void*, int (*fn)(void*), void *v, int msec)
{
int x;
ulong start;
x = spllo();
for(start = m->ticks; TK2MS(m->ticks - start) < msec && !fn(v); )
;
splx(x);
}
void*
sdmalloc(void *p, ulong sz)
{
if(p != nil) {
memset(p, 0, sz);
return p;
}
return malloc(sz);
}
|