summaryrefslogtreecommitdiff
path: root/sys/src/boot/pc/devbios.c
blob: 8144cea7b51c7d78ffd87e8813d326deb8b52104 (plain)
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
/*
 * boot driver for BIOS LBA devices
 * heavily dependent upon correct BIOS implementation
 */
#include <u.h>
#include "lib.h"
#include "mem.h"
#include "dat.h"
#include "fns.h"
#include "io.h"
#include "ureg.h"
#include "fs.h"

typedef uvlong Devbytes, Devsects;

typedef struct Biosdrive Biosdrive;	/* 1 drive -> ndevs */
typedef struct Biosdev Biosdev;

enum {
	Debug = 0,
	Pause = 0,			/* delay to read debugging */

	Maxdevs		= 8,
	CF		= 1,		/* carry flag: indicates an error */
	Flopid		= 0,		/* first floppy */
	Baseid		= 0x80,		/* first disk */

	/* cx bits of Biosckext results */
	Dap		= 1<<0,
	Drlock		= 1<<1,
	Edd		= 1<<2,

	/* bios calls: int 0x13 disk services */
	Biosinit	= 0,		/* initialise disk & floppy ctlrs */
	Biosdrvsts,
	Bioschsrdsects,
	Biosdrvparam	= 8,
	Biosctlrinit,
	Biosreset	=  0xd,		/* reset disk */
	Biosdrvrdy	= 0x10,
	Biosdrvtype	= 0x15,
	Biosckext	= 0x41,
	Biosrdsect,
	Biosedrvparam	= 0x48,

	/* disk types */
	Typenone	= 0,
	Typedisk	= 3,

	/* magic numbers for bios calls */
	Imok		= 0x55aa,
	Youreok		= 0xaa55,
};

struct Biosdrive {
	int	ndevs;
};

struct Biosdev {
	Devbytes size;
	Devbytes offset;
	uchar	id;			/* drive number; e.g., 0x80 */
	char	type;
	ushort	sectsz;
};

typedef struct Extread {		/* a device address packet */
	uchar	size;
	uchar	unused1;
	uchar	nsects;
	uchar	unused2;
	union {
		ulong	addr;		/* actual address (nominally seg:off) */
		struct {
			ushort	addroff;	/* :offset */
			ushort	addrseg;	/* segment: */
		};
	};
	uvlong	stsect;			/* starting sector */
	/* from edd 3.0: */
	uvlong	buffer;			/* instead of addr, if addr is ~0 */
} Extread;

typedef struct Edrvparam {
	/* from edd 1.1 spec */
	ushort	size;			/* max. buffer (struct) size */
	ushort	flags;
	ulong	physcyls;
	ulong	physheads;
	ulong	phystracksects;
	uvlong	physsects;
	ushort	sectsz;
	/* pointer is required to be unaligned, bytes 26-29.  ick. */
//	void	*dpte;			/* ~0ull: invalid */
	uchar	dpte[4];

	/* remainder from edd 3.0 spec */
	ushort	key;			/* 0xbedd if present */
	uchar	dpilen;
	uchar	unused1;
	ushort	unused2;
	char	bustype[4];		/* "PCI" or "ISA" */
	char	ifctype[8]; /* "ATA", "ATAPI", "SCSI", "USB", "1394", "FIBRE" */
	uvlong	ifcpath;
	uvlong	devpath;
	uchar	unused3;
	uchar	dpicksum;
} Edrvparam;

void	realmode(int intr, Ureg *ureg);		/* from trap.c */

int onlybios0;
int biosinited;

static Biosdev bdev[Maxdevs];
static Biosdrive bdrive;
static Ureg regs;

static int	dreset(uchar drive);
static Devbytes	extgetsize(Biosdev *);
static Devsects	getsize(uchar drive, char *type);
static int	islba(uchar drive);

void
stopbiosload(void)
{
	biosload = 0;
	print("disabling bios loading\n");
}

/* convert ah error code to a string (just common cases) */
static char *
strerr(uchar err)
{
	switch (err) {
	case 0:
		return "none";
	case 0x80:
		return "disk timeout";
	default:
		return "unknown";
	}
}

/*
 * caller must zero or otherwise initialise *rp,
 * other than ax, bx, dx, si & ds.
 */
static int
biosdiskcall(Ureg *up, uchar op, ulong bx, ulong dx, ulong si)
{
	int s;
	uchar err;

	s = splhi();		/* don't let the bios call be interrupted */

	up->ax = op << 8;
	up->bx = bx;
	up->dx = dx;		/* often drive id */
	/*
	 * ensure that dap addr fits in a short.
	 */
	if((si & 0xffff0000) != 0)
		print("biosdiskcall: dap address %#lux not a short\n", si);

	/* assume si is in first 64K */
	if((si & 0xffff0000) != ((si + 512 - 1) & 0xffff0000))
		print("biosdiskcall: dap address %#lux too near segment boundary\n",
			si);
	up->si = si;		/* ds:si forms data access packet addr */
	up->ds = 0;
	up->di = 0;

	/*
	 * *up is copied into low memory (realmoderegs) and thence into
	 * the machine registers before the BIOS call, and the registers are
	 * copied into realmoderegs and thence into *up after.
	 *
	 * realmode loads these registers: di, si, ax, bx, cx, dx, ds, es.
	 */
	realmode(0x13, up);

	splx(s);

	if (up->flags & CF) {
		if (Debug && dx == Baseid) {
			err = up->ax >> 8;
			print("\nbiosdiskcall: int 0x13 op %#ux drive %#lux "
				"failed, ah error code %#ux (%s)\n",
				op, dx, err, strerr(err));
		}
		return -1;
	}
	return 0;
}

/*
 * Find out what the bios knows about devices.
 * our boot device could be usb; ghod only knows where it will appear.
 */
int
biosinit(void)
{
	int devid, lba, mask, lastbit, ndrive;
	Devbytes size;
	char type;
	Biosdev *bdp;
	static int beenhere;

	delay(Pause);		/* pause to read the screen (DEBUG) */

	/*
	 * 9pxeload can't use bios int 13 calls; they wedge the machine.
	 */
	if (pxe || !biosload || onlybios0 || biosinited || beenhere)
		return 0;
	beenhere = 1;

	ndrive = *(uchar *)KADDR(0x475);
	if (Debug)
		print("bios claims %d drive(s)\n", ndrive);
	mask = lastbit = 0;
	for (devid = Baseid; devid < (1 << 8) && bdrive.ndevs < Maxdevs;
	     devid++) {
		lba = islba(devid);
		if (lba < 0)
			break;

		/* don't reset; it seems to hang the bios */
		if(!lba /* || devid != Baseid && dreset(devid) < 0 */ )
			continue;
		type = Typenone;
		if (getsize(devid, &type) == 0)		/* no device */
			continue;
		lastbit = 1 << bdrive.ndevs;
		mask |= lastbit;
		bdp = &bdev[bdrive.ndevs];
		bdp->id = devid;
		bdp->type = type;
		size = extgetsize(bdp);
		bdp->size = size;
		print("bios%d: drive %#ux: %,llud bytes, type %d\n",
			bdrive.ndevs, devid, size, type);
		bdrive.ndevs++;
	}
	USED(lastbit);
//	islba(Baseid);	/* do a successful operation to make bios happy again */

	if (Debug && ndrive != bdrive.ndevs)
		print("bios: ndrive %d != bdrive.ndevs %d\n",
			ndrive, bdrive.ndevs);
	if (ndrive < bdrive.ndevs)
		bdrive.ndevs = ndrive;		/* use smaller estimate */

	/*
	 * some bioses seem to only be able to read from drive number 0x80
	 * and certainly can't read from the highest drive number when we
	 * call them, even if there is only one.
	 */
	if (bdrive.ndevs > 0)
		biosinited = 1;
	delay(Pause);		/* pause to read the screen (DEBUG) */

	return mask;
}

void
biosinitdev(int i, char *name)
{
	if(i >= bdrive.ndevs)
		panic("biosinitdev");
	sprint(name, "bios%d", i);
}

void
biosprintdevs(int i)
{
	if(i >= bdrive.ndevs){
		print("got a print for %d, only got %d\n", i, bdrive.ndevs);
		panic("biosprintdevs");
	}
	print(" bios%d", i);
}

int
biosboot(int dev, char *file, Boot *b)
{
	Fs *fs;

	if(strncmp(file, "dos!", 4) == 0)
		file += 4;
	if(strchr(file, '!') != nil || strcmp(file, "") == 0) {
		print("syntax is bios0!file\n");
		return -1;
	}

	fs = biosgetfspart(dev, "9fat", 1);
	if(fs == nil)
		return -1;
	return fsboot(fs, file, b);
}

static void
dump(void *addr, int wds)
{
	unsigned i;
	ulong *p = addr;

	for (i = 0; i < wds; i++)
		print("%lux ", p[i]);
		if (i % 8 == 7)
			print("\n");
	print("\n");
}

/* realmode loads these registers: di, si, ax, bx, cx, dx, ds, es */
static void
initrealregs(Ureg *up)
{
	memset(up, 0, sizeof *up);
}

/* read n bytes at sector offset into a from drive id */
long
sectread(Biosdev *bdp, void *a, long n, Devsects offset)
{
	uchar *biosparam, *cp;
	Extread *erp;

	if(n < 0 || n > bdp->sectsz)
		return -1;
	if(Debug)
		/* scribble on the buffer to provoke trouble */
		memset((uchar *)BIOSXCHG, 'r', bdp->sectsz);

	/* space for a big, optical-size sector, just in case... */
	biosparam = (uchar *)BIOSXCHG + 2*1024;

	/* read into BIOSXCHG */
	erp = (Extread *)biosparam;
	memset(erp, 0, sizeof *erp);
	erp->size = sizeof *erp;
	erp->nsects = 1;
	erp->stsect = offset;
	erp->buffer = PADDR(BIOSXCHG);

	erp->addr = PADDR(BIOSXCHG);
	erp->addroff = PADDR(BIOSXCHG);		/* pedantic seg:off */
	erp->addrseg = 0;
	/*
	 * ensure that buffer addr fits in a short.
	 */
	if((erp->addr & 0xffff0000) != 0)
		print("sectread: address %#lux not a short\n", erp->addr);
	if((erp->addr & 0xffff0000) != ((erp->addr + 512 - 1) & 0xffff0000))
		print("sectread: address %#lux too near seg boundary\n",
			erp->addr);
	if (Debug)
		print("reading drive %#ux sector %lld -> %#lux...",
			bdp->id, offset, erp->addr);

	initrealregs(&regs);
//	regs.es = erp->addr >> 16;
	regs.es = 0;
	delay(Pause);			/* pause to read the screen (DEBUG) */
	/*
	 * int 13 read sector expects, buffer seg in di?,
	 * dap (erp) in si, 0x42 in ah, drive in dl.
	 */
	if (biosdiskcall(&regs, Biosrdsect, erp->addr, bdp->id, PADDR(erp)) < 0) {
		print("sectread: bios failed to read %ld @ sector %lld of %#ux\n",
			n, offset, bdp->id);
		stopbiosload();		/* stop trying before we wedge */
		return -1;
	}
	if (Debug)
		print("OK\n");

	/* copy into caller's buffer */
	memmove(a, (char *)BIOSXCHG, n);
	if(0 && Debug){
		cp = (uchar *)BIOSXCHG;
		print("-%ux %ux %ux %ux--%16.16s-\n",
			cp[0], cp[1], cp[2], cp[3], (char *)cp + 480);
	}
	delay(Pause);		/* pause to read the screen (DEBUG) */
	return n;
}

/* not tested yet. */
static int
dreset(uchar drive)
{
	print("devbios: resetting %#ux...", drive);
	initrealregs(&regs);
	if (biosdiskcall(&regs, Biosinit, 0, drive, 0) < 0)
		print("failed");
	print("\n");
	return regs.ax? -1: 0;		/* ax!=0 on error */
}

static int
islba(uchar drive)
{
	initrealregs(&regs);
	if (biosdiskcall(&regs, Biosckext, Imok, drive, 0) < 0)
		/*
		 * we have an old bios without extensions, in theory.
		 * in practice, there may just be no drive for this number.
		 */
		return -1;
	if(regs.bx != Youreok){
		print("islba: buggy bios: drive %#ux extension check returned "
			"%lux in bx\n", drive, regs.bx);
		stopbiosload();
		return -1;
	}
	if (Debug) {
		print("islba: drive %#ux extensions version %d.%d cx %#lux\n",
			drive, (uchar)(regs.ax >> 8), (uchar)regs.ax, regs.cx);
		print("\tsubsets supported:");
		if (regs.cx & Dap)
			print(" disk access;");
		if (regs.cx & Drlock)
			print(" drive locking;");
		if (regs.cx & Edd)
			print(" enhanced disk support;");
		print("\n");
	}
	delay(Pause);			/* pause to read the screen (DEBUG) */
	return regs.cx & Dap;
}

/*
 * works so-so... some floppies are 0x80+x when they shouldn't be,
 * and report lba even if they cannot...
 */
static Devsects
getsize(uchar id, char *typep)
{
	int dtype;

	initrealregs(&regs);
	if (biosdiskcall(&regs, Biosdrvtype, Imok, id, 0) < 0)
		return 0;

	*typep = dtype = (ushort)regs.ax >> 8;
	if(dtype == Typenone){
		print("no such device %#ux of type %d\n", id, dtype);
		return 0;
	}
	if(dtype != Typedisk){
		print("non-disk device %#ux of type %d\n", id, dtype);
		return 0;
	}
	return (ushort)regs.cx | regs.dx << 16;
}

/* extended get size */
static Devbytes
extgetsize(Biosdev *bdp)
{
	Edrvparam *edp;

	edp = (Edrvparam *)BIOSXCHG;
	memset(edp, 0, sizeof *edp);
	edp->size = sizeof *edp;
	edp->dpilen = 36;

	initrealregs(&regs);
	if (biosdiskcall(&regs, Biosedrvparam, 0, bdp->id, PADDR(edp)) < 0)
		return 0;		/* old bios without extensions */
	if(Debug) {
		print("extgetsize: drive %#ux info flags %#ux",
			bdp->id, edp->flags);
		if (edp->key == 0xbedd)
			print(" %.4s %.8s", edp->bustype, edp->ifctype);
		print("\n");
	}
	if (edp->sectsz <= 0) {
		print("extgetsize: drive %#ux: sector size <= 0\n", bdp->id);
		edp->sectsz = 1;		/* don't divide by 0 */
	}
	bdp->sectsz = edp->sectsz;
	return edp->physsects * edp->sectsz;
}

long
biosread(Fs *fs, void *a, long n)
{
	int want, got, part;
	long totnr, stuck;
	Devbytes offset;
	Biosdev *bdp;

	if(!biosload || fs->dev > bdrive.ndevs)
		return -1;
	if (n <= 0)
		return n;
	bdp = &bdev[fs->dev];
	offset = bdp->offset;
	stuck = 0;
	for (totnr = 0; totnr < n && stuck < 4; totnr += got) {
		if (bdp->sectsz == 0) {
			print("devbios: zero sector size\n");
			return -1;
		}
		want = bdp->sectsz;
		if (totnr + want > n)
			want = n - totnr;
		if(0 && Debug && debugload)
			print("bios%d, read: %ld @ off %lld, want: %d, id: %#ux\n",
				fs->dev, n, offset, want, bdp->id);
		part = offset % bdp->sectsz;
		if (part != 0) {	/* back up to start of sector */
			offset -= part;
			totnr  -= part;
			if (totnr < 0) {
				print("biosread: negative count %ld\n", totnr);
				return -1;
			}
		}
		if ((vlong)offset < 0) {
			print("biosread: negative offset %lld\n", offset);
			return -1;
		}
		got = sectread(bdp, (char *)a + totnr, want,
			offset / bdp->sectsz);
		if(got <= 0)
			return -1;
		offset += got;
		bdp->offset = offset;
		if (got < bdp->sectsz)
			stuck++;	/* we'll have to re-read this sector */
		else
			stuck = 0;
	}
	return totnr;
}

vlong
biosseek(Fs *fs, vlong off)
{
	if (off < 0) {
		print("biosseek(fs, %lld) is illegal\n", off);
		return -1;
	}
	if(fs->dev > bdrive.ndevs) {
		print("biosseek: fs->dev %d > bdrive.ndevs %d\n",
			fs->dev, bdrive.ndevs);
		return -1;
	}
	bdev[fs->dev].offset = off;	/* do not know size... (yet) */
	return off;
}

void *
biosgetfspart(int i, char *name, int chatty)
{
	static Fs fs;

	if(strcmp(name, "9fat") != 0){
		if(chatty)
			print("unknown partition bios%d!%s (use bios%d!9fat)\n",
				i, name, i);
		return nil;
	}

	fs.dev = i;
	fs.diskread = biosread;
	fs.diskseek = biosseek;

	if(dosinit(&fs) < 0){
		if(chatty)
			print("bios%d!%s does not contain a FAT file system\n",
				i, name);
		return nil;
	}
	return &fs;
}