File: boot.c

package info (click to toggle)
faumachine 20100527-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 53,836 kB
  • ctags: 20,552
  • sloc: ansic: 179,550; asm: 3,645; makefile: 3,611; perl: 2,103; sh: 1,529; python: 600; xml: 563; lex: 210; vhdl: 204
file content (468 lines) | stat: -rw-r--r-- 9,691 bytes parent folder | download | duplicates (2)
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
/* $Id: boot.c,v 1.89 2009-03-20 08:33:40 vrsieh Exp $ 
 *
 * Copyright (C) 2004-2009 FAUmachine Team <info@faumachine.org>.
 * This program is free software. You can redistribute it and/or modify it
 * under the terms of the GNU General Public License, either version 2 of
 * the License, or (at your option) any later version. See COPYING.
 */

#include "build_config.h"
#include "compiler.h"

/* ==================== RUNTIME_RM ==================== */
#ifdef RUNTIME_RM

CODE16;

#include "assert.h"
#include "debug.h"
#include "stdio.h"
#include "ptrace.h"

void
bios_19_xxxx(struct regs *regs)
{
}

__attribute__((__noreturn__)) void
bios_18_xxxx(struct regs *regs)
{
	putcstr("\n");
	putcstr("No operating system found.\n");

	asm volatile (
		"cli\n"
		"hlt\n"
	);

	/*NOTREACHED*/
	assert(0);
}

#endif /* RUNTIME_RM */
/* ==================== REAL-MODE INIT ==================== */
#ifdef INIT_RM

CODE16;

#include "var.h"
#include "disk.h"
#include "debug.h"
#include "stdio.h"
#include "ptrace.h"
#include "entry.h"
#include "cmos.h"
#include "el_torito.h"
#include "assert.h"

static int
boot_floppy(void)
{
	unsigned char drive_type;
	uint16_t ax, bx, cx, dx;

	/*
	 * Lookup first floppy
	 */
	drive_type = cmos_get(fd_config) >> 4;

	if (drive_type == 0) {
		/* No floppy A present */
		return -1;
	}

	/* Read boot record. */
	asm volatile (
		"\tmovw %%di, %%es\n"
		"\tint $0x13\n"
		"\tpushfw\n"
		"\tpopw %%ax\n"
		: "=a" (ax), "=b" (bx), "=c" (cx), "=d" (dx)
		: "0" ((uint16_t) ((0x02 << 8) | (1 << 0))), /* Read, # Secs */
		  "1" ((uint16_t) 0x7c00),                   /* Offset */
		  "2" ((uint16_t) ((0 << 8) | (1 << 0))),    /* Cyl, Sec */
		  "3" ((uint16_t) ((0 << 8) | (0x00 << 0))), /* Head, Drive */
		  "D" ((uint16_t) 0x0000)                    /* Segment */
	);
	if (ax & (1 << 0)) {
		/* Read error. */
		return -1;
	}

#if 0
	/*
	 * Dump boot sector
	 */
	int i;
	bprintf("\n");
	for (i = 0; i < 512; i += 2) {
		bprintf("%04x ", get_word(0x0000, (unsigned short *) (0x7c00 + i)));
	}
#endif
	
	/* Check for boot signature. */
	if (get_word(0x0000, 0x7c00 + 0x1fe) != 0xaa55) {
		/* No boot block on floppy */
		return -1;
	}

	return 0x00;
}

static int
boot_cdrom(void)
{
	unsigned char device;
	struct boot_record_volume *brv;
	struct validation *validation;
	struct initial *initial;
	unsigned char ret;
	unsigned char remapemul;

	/*
	 * Lookup first CDROM.
	 */
	device = ebda_get(cdidmap[0xe0 - 0xe0]);
	if (MAX_BIOS_HDS <= device) {
		/* No cdrom found. */
		return -1;
	}

	/*
	 * Read boot record volume descriptor.
	 */
	ret = read_sector_lba_atapi(device, 1, BRV_SECTOR,
			0x0400, 0x0000, 2048);
	if (ret != 0) {
		return -1;
	}
	assert(ret == 0);

	brv = (struct boot_record_volume *) 0x4000;
	if (brv->indicator != 0) {
		/* No bootable CDROM. */
		ebda_put(cdemu.active, 0);
		return -1;
	}

	/*
	 * Read boot catalog.
	 */
	ret = read_sector_lba_atapi(device, 1, brv->bootcat_p,
			0x0400, 0x0000, 2048);
	if (ret != 0) {
		return -1;
	}
	assert(ret == 0);

	validation = (struct validation *) 0x4000;
	assert(validation->header_id == 0x01);
	assert(validation->platform_id == 0x0);

	initial = (struct initial *) ((char *) 0x4000 + sizeof(struct validation));
	if (initial->boot_indicator != 0x88) {
		/* No bootable CDROM. */
		ebda_put(cdemu.active, 0);
		return -1;
	}

	/*
	 * Save values.
	 */
	ebda_put(cdemu.media, initial->boot_media);
	if (initial->boot_media == 0x00) {
		/*
		 * FIXME ElTorito Hardcoded. cdrom is hardcoded as device 0xE0.
		 * Win2000 cd boot needs to know it booted from cd
		 */
		ebda_put(cdemu.emulated_drive, 0xe0);
	} else if (initial->boot_media < 4) {
		ebda_put(cdemu.emulated_drive, 0x00);
	} else {
		ebda_put(cdemu.emulated_drive, 0x80);
	}

	/* FIXME ElTorito Harddisk. current code can only emulate a floppy */

	ebda_put(cdemu.controller_index, device / 2);
	ebda_put(cdemu.device_spec, device % 2);

	ebda_put(cdemu.load_segment, initial->load_segment);
	ebda_put(cdemu.buffer_segment, 0x0000);

	ebda_put(cdemu.sector_count, initial->sector_count);

	ebda_put(cdemu.ilba, initial->load_rba);

	if (ebda_get(cdemu.media) == 0x01) {
		/* 1.2M floppy */
		ebda_put(cdemu.vdevice.spt, 15);
		ebda_put(cdemu.vdevice.cylinders, 80);
		ebda_put(cdemu.vdevice.heads, 2);
	} else if (ebda_get(cdemu.media) == 0x02) {
		/* 1.44M floppy */
		ebda_put(cdemu.vdevice.spt, 18);
		ebda_put(cdemu.vdevice.cylinders, 80);
		ebda_put(cdemu.vdevice.heads, 2);
	} else if (ebda_get(cdemu.media) == 0x03) {
		/* 2.88M floppy */
		ebda_put(cdemu.vdevice.spt, 36);
		ebda_put(cdemu.vdevice.cylinders, 80);
		ebda_put(cdemu.vdevice.heads, 2);
	} else if (ebda_get(cdemu.media) == 0x04) {
		/* Harddisk */
		assert(0);      /* FIXME VOSSI */
	}

	/* Everything is ok, so from now on, the emulation is active. */
	if (ebda_get(cdemu.media) != 0x00) {
		ebda_put(cdemu.active, 1);
	}

	if (ebda_get(cdemu.media) == 0x00) {
		/* No Emulation */
		/* Nothing to do... */

	} else if (0x01 <= ebda_get(cdemu.media)
		&& ebda_get(cdemu.media) <= 0x03) {
		/* Floppy Emulation */
		/* Increment number of floppies. */
		uint16_t sys_conf;
		uint8_t num;

		sys_conf = var_get(sys_conf);

		if ((sys_conf >> 0) & 1) {
			num = ((sys_conf >> 6) & 3) + 1;
		} else {
			num = 0;
		}
		assert(num <= 2);
		num++;

		sys_conf &= ~(1 << 0);
		sys_conf &= ~(3 << 6);
		sys_conf |= 1 << 0;
		sys_conf |= (num - 1) << 6;

		var_put(sys_conf, sys_conf);

	} else if (ebda_get(cdemu.media) == 0x04) {
		/* Harddisk Emulation */
		/* Increment number of harddisks. */
		uint8_t hd_num;

		hd_num = var_get(hd_num);

		hd_num++;

		var_put(hd_num, hd_num);

	} else {
		assert(0);
	}

	remapemul = ebda_get(cdemu.media);

	bprintf("Emulation: ");

	if (remapemul == 0x00) {
		/* No emulation, read load_rba directly. */
		bprintf("None - ");

	} else if (remapemul == 0x01) {
		bprintf("1.2 MB disk - ");

	} else if (remapemul == 0x02) {
		bprintf("1.44 MB disk - ");

	} else if (remapemul == 0x03) {
		bprintf("2.88 MB disk - ");

	} else if (remapemul == 0x04) {
		bprintf("Hard Disk - ");

	} else {
		bprintf("Illegal emulation.\n");
		for (;;);

	}

	/* We should read 512 byte blocks! FIXME VOSSI */
	ret = read_sector_lba_atapi(device,
			1 + (ebda_get(cdemu.sector_count) - 1) / 4,
			ebda_get(cdemu.ilba),
			0x0000, 0x7c00, 2048);
	if (ret != 0) {
		/* Read error. */
		return -1;
	}

	return ebda_get(cdemu.emulated_drive);
}

static int
boot_harddisk(void)
{
	unsigned char device;
	uint16_t ax, bx, cx, dx;

	/* Lookup first harddisk. */
	device = ebda_get(hdidmap[0x80 - 0x80]);
	if (MAX_BIOS_HDS <= device) {
		/* No harddisk found. */
		return -1;
	}

	/* Read boot record. */
	asm volatile (
		"\tmovw %%di, %%es\n"
		"\tint $0x13\n"
		"\tmovw %%ds, %%di\n"
		"\tmovw %%di, %%es\n"
		"\tpushfw\n"
		"\tpopw %%ax\n"
		: "=a" (ax), "=b" (bx), "=c" (cx), "=d" (dx)
		: "0" ((uint16_t) ((0x02 << 8) | (1 << 0))), /* Read, # Secs */
		  "1" ((uint16_t) 0x7c00),                   /* Offset */
		  "2" ((uint16_t) ((0 << 8) | (1 << 0))),    /* Cyl, Sec */
		  "3" ((uint16_t) ((0 << 8) | (0x80 << 0))), /* Head, Drive */
		  "D" ((uint16_t) 0x0000)                    /* Segment */
	);
	if (ax & (1 << 0)) {
		/* Read error. */
		return -1;
	}

	/* Check for boot signature. */
	if (get_word(0x0000, 0x7c00 + 0x1fe) != 0xaa55) {
		/* No boot block on hard disk. */
		return -1;
	}

	return 0x80;
}

static int
boot_int18h(void)
{
	asm volatile (
		"\tint $0x18\n"
	);
	/* never returns */
	return -1;
}

static int
boot_int19h(void)
{
	asm volatile (
		"\tint $0x19\n"
	);
	/* only returns if no boot device available */
	return -1;
}

uint8_t
boot(void)
{
	enum {
		BOOT_NONE,
		BOOT_A,
		BOOT_CDROM,
		BOOT_C,
		BOOT_D,
		BOOT_E,
		BOOT_F,
		BOOT_LS_ZIP,
		BOOT_SCSI,
	};
	static const uint8_t seq[][3] = {
		{ BOOT_A, BOOT_C, BOOT_SCSI },
		{ BOOT_C, BOOT_A, BOOT_SCSI },
		{ BOOT_C, BOOT_CDROM, BOOT_A },
		{ BOOT_CDROM, BOOT_C, BOOT_A },
		{ BOOT_D, BOOT_A, BOOT_SCSI },
		{ BOOT_E, BOOT_A, BOOT_SCSI },
		{ BOOT_F, BOOT_A, BOOT_SCSI },
		{ BOOT_SCSI, BOOT_A, BOOT_C },
		{ BOOT_SCSI, BOOT_C, BOOT_A },
		{ BOOT_C, BOOT_NONE, BOOT_NONE },
		{ BOOT_LS_ZIP, BOOT_C, BOOT_NONE },
	};
	uint8_t cmos_seq;
	int bootdev;
	int i;

	bprintf("\n\nChecking boot devices ...\n");

	cmos_seq = cmos_get(x3c) & 0xf;
	if (sizeof(seq) / sizeof(seq[0]) <= cmos_seq) {
		cmos_seq = 0;
	}

	bootdev = -1;
	for (i = 0; i < 3; i++) {
		uint8_t dev;

		dev = seq[cmos_seq][i];

		if (dev == BOOT_NONE) {
			/* Nothing... */

		} else if (dev == BOOT_A) {
			/* Try to boot from floppy. */
			DEBUGPRINT(3, "Trying to boot from floppy... ");
			bprintf(" - Floppy:   ");
			bootdev = boot_floppy();

		} else if (dev == BOOT_CDROM) {
			/* Try to boot from cdrom. */
			DEBUGPRINT(3, "Trying to boot from cdrom... ");
			bprintf(" - CDROM:    ");
			bootdev = boot_cdrom();

		} else if (dev == BOOT_C) {
			/* Try to boot from harddisk. */
			DEBUGPRINT(3, "Trying to boot from harddisk... ");
			bprintf(" - Harddisk: ");
			bootdev = boot_harddisk();

		} else if (dev == BOOT_SCSI) {
			/* Try to boot from external device. */
			DEBUGPRINT(3, "Trying to boot from external device... ");
			bprintf(" - Int 19h:  ");
			bootdev = boot_int19h();

		} else {
			/* FIXME */
			bprintf("Unsupported boot entry in CMOS, "
				"please run setup.\n");
			continue;
		}

		if (bootdev != -1) {
			DEBUGPRINT(3, "OK\n");
			bprintf("OK\n");
			break;
		} else {
			DEBUGPRINT(3, "Failed\n");
			bprintf("Failed\n");
		}
	}

#if 0
	/* Switch off A20 gate. */
	outb(0, 0x92);
#endif

	if (bootdev == -1) {
		/* last chance - never returns */
		DEBUGPRINT(3, "Trying int18h... \n");
		boot_int18h();
	}

	return bootdev;
}

#endif /* INIT_RM */