summaryrefslogtreecommitdiff
path: root/hw/xfree86/int10/helper_mem.c
blob: c62377ca82bc154f82e7d5604b713e9a39c32b36 (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
/*
 *                   XFree86 int10 module
 *   execute BIOS int 10h calls in x86 real mode environment
 *                 Copyright 1999 Egbert Eich
 */
#ifdef HAVE_XORG_CONFIG_H
#include <xorg-config.h>
#endif

#include <string.h>
#include <stdlib.h>

#include "xf86.h"
#include "xf86_OSproc.h"
#include "compiler.h"
#include "xf86Pci.h"
#define _INT10_PRIVATE
#if 0
#include "int10Defines.h"
#endif
#include "xf86int10.h"

#define REG pInt

typedef enum {
    OPT_NOINT10,
    OPT_INIT_PRIMARY,
    OPT_BIOS_LOCATION
} INT10Opts;

static const OptionInfoRec INT10Options[] = {
    {OPT_NOINT10,       "NoINT10",      OPTV_BOOLEAN,   {0},    FALSE },
    {OPT_INIT_PRIMARY,  "InitPrimary",  OPTV_BOOLEAN,   {0},    FALSE },   
    {OPT_BIOS_LOCATION, "BiosLocation",	OPTV_STRING,	{0},	FALSE },
    { -1,		NULL,		OPTV_NONE,	{0},	FALSE },
};

#ifdef DEBUG
void
dprint(unsigned long start, unsigned long size)
{
    int i,j;
    char *c = (char *)start;

    for (j = 0; j < (size >> 4); j++) {
	char *d = c;
	ErrorF("\n0x%lx:  ",(unsigned long)c);
	for (i = 0; i<16; i++)
	    ErrorF("%2.2x ",(unsigned char) (*(c++)));
	c = d;
	for (i = 0; i<16; i++) {
	    ErrorF("%c",((((CARD8)(*c)) > 32) && (((CARD8)(*c)) < 128)) ?
		   (unsigned char) (*(c)): '.');
	    c++;
	}
    }
    ErrorF("\n");
}
#endif

#ifndef _PC
/*
 * here we are really paranoid about faking a "real"
 * BIOS. Most of this information was pulled from
 * dosemu.
 */
void
setup_int_vect(xf86Int10InfoPtr pInt)
{
    int i;

    /* let the int vects point to the SYS_BIOS seg */
    for (i = 0; i < 0x80; i++) {
	MEM_WW(pInt, i << 2, 0);
	MEM_WW(pInt, (i << 2) + 2, SYS_BIOS >> 4);
    }

    reset_int_vect(pInt);
    /* font tables default location (int 1F) */
    MEM_WW(pInt,0x1f<<2,0xfa6e);

    /* int 11 default location (Get Equipment Configuration) */
    MEM_WW(pInt, 0x11 << 2, 0xf84d);
    /* int 12 default location (Get Conventional Memory Size) */
    MEM_WW(pInt, 0x12 << 2, 0xf841);
    /* int 15 default location (I/O System Extensions) */
    MEM_WW(pInt, 0x15 << 2, 0xf859);
    /* int 1A default location (RTC, PCI and others) */
    MEM_WW(pInt, 0x1a << 2, 0xff6e);
    /* int 05 default location (Bound Exceeded) */
    MEM_WW(pInt, 0x05 << 2, 0xff54);
    /* int 08 default location (Double Fault) */
    MEM_WW(pInt, 0x08 << 2, 0xfea5);
    /* int 13 default location (Disk) */
    MEM_WW(pInt, 0x13 << 2, 0xec59);
    /* int 0E default location (Page Fault) */
    MEM_WW(pInt, 0x0e << 2, 0xef57);
    /* int 17 default location (Parallel Port) */
    MEM_WW(pInt, 0x17 << 2, 0xefd2);
    /* fdd table default location (int 1e) */
    MEM_WW(pInt, 0x1e << 2, 0xefc7);

    /* Set Equipment flag to VGA */
    i = MEM_RB(pInt, 0x0410) & 0xCF;
    MEM_WB(pInt, 0x0410, i);
    /* XXX Perhaps setup more of the BDA here.  See also int42(0x00). */
}
#endif

int
setup_system_bios(void *base_addr)
{
    char *base = (char *) base_addr;

    /*
     * we trap the "industry standard entry points" to the BIOS
     * and all other locations by filling them with "hlt"
     * TODO: implement hlt-handler for these
     */
    memset(base, 0xf4, 0x10000);

    /* set bios date */
    strcpy(base + 0x0FFF5, "06/11/99");
    /* set up eisa ident string */
    strcpy(base + 0x0FFD9, "PCI_ISA");
    /* write system model id for IBM-AT */
    *((unsigned char *)(base + 0x0FFFE)) = 0xfc;

    return 1;
}

void
reset_int_vect(xf86Int10InfoPtr pInt)
{
    /*
     * This table is normally located at 0xF000:0xF0A4.  However, int 0x42,
     * function 0 (Mode Set) expects it (or a copy) somewhere in the bottom
     * 64kB.  Note that because this data doesn't survive POST, int 0x42 should
     * only be used during EGA/VGA BIOS initialisation.
     */
    static const CARD8 VideoParms[] = {
	/* Timing for modes 0x00 & 0x01 */
	0x38, 0x28, 0x2d, 0x0a, 0x1f, 0x06, 0x19, 0x1c,
	0x02, 0x07, 0x06, 0x07, 0x00, 0x00, 0x00, 0x00,
	/* Timing for modes 0x02 & 0x03 */
	0x71, 0x50, 0x5a, 0x0a, 0x1f, 0x06, 0x19, 0x1c,
	0x02, 0x07, 0x06, 0x07, 0x00, 0x00, 0x00, 0x00,
	/* Timing for modes 0x04, 0x05 & 0x06 */
	0x38, 0x28, 0x2d, 0x0a, 0x7f, 0x06, 0x64, 0x70,
	0x02, 0x01, 0x06, 0x07, 0x00, 0x00, 0x00, 0x00,
	/* Timing for mode 0x07 */
	0x61, 0x50, 0x52, 0x0f, 0x19, 0x06, 0x19, 0x19,
	0x02, 0x0d, 0x0b, 0x0c, 0x00, 0x00, 0x00, 0x00,
	/* Display page lengths in little endian order */
	0x00, 0x08, /* Modes 0x00 and 0x01 */
	0x00, 0x10, /* Modes 0x02 and 0x03 */
	0x00, 0x40, /* Modes 0x04 and 0x05 */
	0x00, 0x40, /* Modes 0x06 and 0x07 */
	/* Number of columns for each mode */
	40, 40, 80, 80, 40, 40, 80, 80,
	/* CGA Mode register value for each mode */
	0x2c, 0x28, 0x2d, 0x29, 0x2a, 0x2e, 0x1e, 0x29,
	/* Padding */
	0x00, 0x00, 0x00, 0x00
	};
    int i;

    for (i = 0; i < sizeof(VideoParms); i++)
	MEM_WB(pInt, i + (0x1000 - sizeof(VideoParms)), VideoParms[i]);
    MEM_WW(pInt,  0x1d << 2, 0x1000 - sizeof(VideoParms));
    MEM_WW(pInt, (0x1d << 2) + 2, 0);

    MEM_WW(pInt,  0x10 << 2, 0xf065);
    MEM_WW(pInt, (0x10 << 2) + 2, SYS_BIOS >> 4);
    MEM_WW(pInt,  0x42 << 2, 0xf065);
    MEM_WW(pInt, (0x42 << 2) + 2, SYS_BIOS >> 4);
    MEM_WW(pInt,  0x6D << 2, 0xf065);
    MEM_WW(pInt, (0x6D << 2) + 2, SYS_BIOS >> 4);
}

void
set_return_trap(xf86Int10InfoPtr pInt)
{
    /*
     * Here we set the exit condition:  We return when we encounter
     * 'hlt' (=0xf4), which we locate at address 0x600 in x86 memory.
     */
    MEM_WB(pInt, 0x0600, 0xf4);

    /*
     * Allocate a segment for the stack
     */
    xf86Int10AllocPages(pInt, 1, &pInt->stackseg);
}

void *
xf86HandleInt10Options(ScrnInfoPtr pScrn, int entityIndex)
{
    EntityInfoPtr pEnt = xf86GetEntityInfo(entityIndex);
    OptionInfoPtr options = NULL;
    
    if (pEnt->device) {
	pointer configOptions = NULL;

	/* Check if xf86CollectOptions() has already been called */
	if (((pEnt->index < 0) ||
	    !pScrn ||
	    !(configOptions = pScrn->options)) &&
	    pEnt->device)
	    configOptions = pEnt->device->options;

	if (configOptions) {
	    if (!(options = (OptionInfoPtr) xalloc(sizeof(INT10Options))))
		return NULL;
	     
	    (void)memcpy(options, INT10Options, sizeof(INT10Options));
	    xf86ProcessOptions(pScrn->scrnIndex, configOptions, options);
	}
    }
    xfree(pEnt);

    return options;
}

Bool
int10skip(const void* options)
{
    Bool noint10 = FALSE;

    if (!options) return FALSE;
    
    xf86GetOptValBool(options, OPT_NOINT10, &noint10);
    return noint10;
}

Bool
int10_check_bios(int scrnIndex, int codeSeg, const unsigned char* vbiosMem)
{
    int size;

    if ((codeSeg & 0x1f) ||	/* Not 512-byte aligned otherwise */
	((codeSeg << 4) < V_BIOS) ||
	((codeSeg << 4) >= SYS_SIZE))
	return FALSE;

    if (xf86IsPc98())
	return FALSE;

    if ((*vbiosMem != 0x55) || (*(vbiosMem+1) != 0xAA) || !*(vbiosMem+2))
	return FALSE;

    size = *(vbiosMem + 2) * 512;

    if ((size + (codeSeg << 4)) > SYS_SIZE)
	return FALSE;

    if (bios_checksum(vbiosMem, size))
	xf86DrvMsg(scrnIndex, X_WARNING, "Bad V_BIOS checksum\n");

    return TRUE;
}

Bool
initPrimary(const void* options)
{
    Bool initPrimary = FALSE;

    if (!options) return FALSE;
    
    xf86GetOptValBool(options, OPT_INIT_PRIMARY, &initPrimary);
    return initPrimary;
}

/*
 * xf86int10ParseBiosLocation(): allows to set the location of the
 * BIOS. One may select a BIOS of another card for posting or the
 * legacy V_BIOS range located at 0xc0000 or an alternative address
 * (BUS_ISA).
 * This is only useful under very special circumstances and should
 * be used with extreme care.
 */
void
xf86int10ParseBiosLocation(const void* options,
			   xf86int10BiosLocationPtr bios)
{
    const char *p;
    const char *str;

    bios->bus = BUS_NONE;

    if ((options == NULL) 
	|| ((str = xf86GetOptValString(options, OPT_BIOS_LOCATION)) == NULL)) {
	return;
    }

    if (strncasecmp(str, "pci", 3) == 0) {
	bios->bus = BUS_PCI;
    } else if (strncasecmp(str, "primary", 7) == 0) {
	bios->bus = BUS_ISA;
    }
    else {
	return;
    }
    
    p = strchr(str, ':');
		     
    switch (bios->bus) {
    case BUS_ISA:
	bios->location.legacy = (p != NULL) ? atoi(++p) : 0;
	break;
    case BUS_PCI:
	if (p) {
	    bios->location.pci.bus = atoi(++p);
	    if ((p = strchr(p, ':'))) {
		bios->location.pci.dev = atoi(++p);
		if ((p = strchr(p, ':'))) {
		    bios->location.pci.func = atoi(++p);
		    break;
		}
	    }
	}
	/* fall through */
	bios->bus = BUS_NONE;
	break;
    default:
	break;
    }
}


BusType
xf86int10GetBiosLocationType(const xf86Int10InfoPtr pInt,
			     const xf86int10BiosLocationPtr bios)
{
    BusType location_type = bios->bus;

    switch (location_type) {
    case BUS_PCI:
	xf86DrvMsg(pInt->scrnIndex,X_CONFIG,"Overriding bios location: "
		   "PCI:%i:%i%i\n",bios->location.pci.bus,
		   bios->location.pci.dev,bios->location.pci.func);
	break;
    case BUS_ISA:
	if (bios->location.legacy)
	    xf86DrvMsg(pInt->scrnIndex,X_CONFIG,"Overriding bios location: "
		       "Legacy:0x%x\n",bios->location.legacy);
	else
	    xf86DrvMsg(pInt->scrnIndex,X_CONFIG,"Overriding bios location: "
		       "Legacy\n");
	break;
    case BUS_NONE: {
	EntityInfoPtr pEnt = xf86GetEntityInfo(pInt->entityIndex);
	location_type = pEnt->location.type;
	xfree(pEnt);
	break;
    }
    default:
	break;
    }

    return location_type;
}


#define CHECK_V_SEGMENT_RANGE(x)   \
    if (((x) << 4) < V_BIOS) { \
	xf86DrvMsg(pInt->scrnIndex, X_ERROR, \
		   "V_BIOS address 0x%lx out of range\n", \
		   (unsigned long)(x) << 4); \
	return FALSE; \
    }

Bool
xf86int10GetBiosSegment(xf86Int10InfoPtr pInt,
			const xf86int10BiosLocationPtr bios, void * base)
{
    unsigned i;
    int cs = ~0;
    int segments[4];
    const char * format;


    if (bios->bus == BUS_ISA && bios->location.legacy) {
	xf86DrvMsg(pInt->scrnIndex, X_CONFIG, 
		   "Overriding BIOS location: 0x%x\n",
		   bios->location.legacy);

	segments[0] = bios->location.legacy >> 4;
	segments[1] = ~0;

	format = "No V_BIOS at specified address 0x%lx\n";
    } else {
	if (bios->bus == BUS_PCI) {
	    xf86DrvMsg(pInt->scrnIndex, X_WARNING,
		       "Option BiosLocation for primary device ignored: "
		       "It points to PCI.\n");
	    xf86DrvMsg(pInt->scrnIndex, X_WARNING,
		       "You must set Option InitPrimary also\n");
	}

	segments[0] = MEM_RW(pInt, (0x10 << 2) + 2);
	segments[1] = MEM_RW(pInt, (0x42 << 2) + 2);
	segments[2] = V_BIOS >> 4;
	segments[3] = ~0;

	format = "No V_BIOS found\n";
    }

    for (i = 0; segments[i] != ~0; i++) {
	unsigned char * vbiosMem;

	cs = segments[i];

	CHECK_V_SEGMENT_RANGE(cs);
	vbiosMem = (unsigned char *)base + (cs << 4);
	if (int10_check_bios(pInt->scrnIndex, cs, vbiosMem)) {
	    break;
	}
    }

    if (segments[i] == ~0) {
	xf86DrvMsg(pInt->scrnIndex, X_ERROR, format, (unsigned long)cs << 4);
	return FALSE;
    }

    xf86DrvMsg(pInt->scrnIndex, X_INFO, "Primary V_BIOS segment is: 0x%lx\n",
	       (unsigned long)cs);

    pInt->BIOSseg = cs;
    return TRUE;
}