summaryrefslogtreecommitdiff
path: root/xc/programs/Xserver/hw/xfree86/int10/pci.c
blob: 49512cd28b54d7fe4f7f3ac6c1bb3a7939a19b6d (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
/* $XFree86: xc/programs/Xserver/hw/xfree86/int10/pci.c,v 1.4 2000/11/03 18:46:16 eich Exp $ */

/*
 *                   XFree86 int10 module
 *   execute BIOS int 10h calls in x86 real mode environment
 *                 Copyright 1999 Egbert Eich
 */
#include "xf86Pci.h"
#include "xf86.h"
#include "xf86str.h"
#include "xf86_ansic.h"
#define _INT10_PRIVATE
#include "xf86int10.h"

int
mapPciRom(xf86Int10InfoPtr pInt, unsigned char * address)
{
    PCITAG tag;
    unsigned long offset = 0;
    unsigned char *mem, *ptr;
    unsigned char *scratch = NULL;
    int length = 0;

    pciVideoPtr pvp = xf86GetPciInfoForEntity(pInt->entityIndex);
    
    if (pvp == NULL) {
#ifdef DEBUG
 	ErrorF("mapPciRom: no PCI info\n");
#endif
	return 0;
    }
    
    tag = pciTag(pvp->bus,pvp->device,pvp->func);
    
    mem = ptr = xnfcalloc(0x10000, 1);
    if (! xf86ReadPciBIOS(offset,tag,-1,mem,0xFFFF) < 0) {
	xfree(mem);
#ifdef DEBUG
 	ErrorF("mapPciRom: cannot read BIOS\n");
#endif
	return 0;
    }

    while ( *ptr == 0x55 && *(ptr+1) == 0xAA) {
	unsigned short data_off = *(ptr+0x18) | (*(ptr+0x19)<< 8);
	unsigned char *data = ptr + data_off;
	unsigned char type;
	
	if (*data!='P' || *(data+1)!='C' || *(data+2)!='I' || *(data+3)!='R')
	    break;
	type = *(data + 0x14);
#ifdef PRINT_PCI
	ErrorF("data segment in BIOS: 0x%x, type: 0x%x ",data_off,type);
#endif
	if (type != 0)	{ /* not PC-AT image: find next one */
	    unsigned int image_length;
	    unsigned char indicator = *(data + 0x15);
	    if (indicator & 0x80) /* last image */
		break;
	    image_length = (*(data + 0x10)
			     | (*(data + 0x11) << 8)) << 9;
#ifdef PRINT_PCI
	    ErrorF("data image length: 0x%x, ind: 0x%x\n",
		     image_length,indicator);
#endif
	     offset = offset + image_length;
	     if (xf86ReadPciBIOS(offset,tag,-1,mem,0xFFFF) < 0) {
		 xfree(mem);
#ifdef DEBUG
	ErrorF("mapPciRom: cannot read BIOS\n");
#endif
		 return 0;
	     }
	     continue;
	 }
	 /* OK, we have a PC Image */
	 length = (*(ptr + 2) << 9);
#ifdef PRINT_PCI
	 ErrorF("BIOS length: 0x%x\n",length);
#endif
	 scratch = (unsigned char *)xnfalloc(length);

	 if (xf86ReadPciBIOS(offset,tag,-1,scratch,length) < 0) {
	     xfree(mem);
	     xfree(scratch);
#ifdef DEBUG
	ErrorF("mapPciRom: cannot read BIOS\n");
#endif
	     return 0;
	 }
	 break;
     }
    /* unmap/close/disable PCI bios mem */
    xfree(mem);

    if (scratch && length) {
	memcpy(address, scratch, length);
	xfree(scratch);
    }
#ifdef DEBUG
    if (!length)
	ErrorF("mapPciRom: no BIOS found\n");
#ifdef PRINT_PCI
    if (length)
	dprint(address,0x20);
#endif
#endif
    return length;
}