summaryrefslogtreecommitdiff
path: root/posttool.c
blob: 54513a588143e0e22b0b7ce6610e4c75716f6d38 (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
/*

Hacked on by Dave Airlie <airlied@redhat.com>
ported to libpciaccess, and vgaaccess
Copyright Matthew Garrett <mjg59@srcf.ucam.org>, heavily based on
vbetest.c from the lrmi package and read-edid.c by John Fremlin

This program is released under the terms of the GNU General Public License,
version 2
*/

#include <pciaccess.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/io.h>
#include <sys/kd.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <errno.h>

#include <libx86.h>

#define access_ptr_register(reg_frame,reg) (reg_frame -> reg)
#define access_seg_register(reg_frame,es) reg_frame.es
#define real_mode_int(interrupt,reg_frame_ptr) !LRMI_int(interrupt,reg_frame_ptr)

#define DPMS_STATE_ON 0x0000
#define DPMS_STATE_STANDBY 0x0100
#define DPMS_STATE_SUSPEND 0x0200
#define DPMS_STATE_OFF 0x0400
#define DPMS_STATE_LOW 0x0800

/* 1 on first boot, 0 on suspend/resume */
int boot_flag;

int vbetool_init (void) {
	if (!LRMI_init()) {
		fprintf(stderr, "Failed to initialise LRMI (Linux Real-Mode Interface).\n");
		exit(1);
	}
	
	ioperm(0, 1024, 1);
	iopl(3);
	
	pci_system_init();

	return 0;
}

#ifndef S2RAM
int main(int argc, char *argv[])
{
	if (argc < 2) {
		fprintf(stderr,"usage: posttool <bootflag>: bootflag = 0 for s/r, 1 for bootup\n");
		return 0;
	}
	boot_flag = atoi(argv[1]);
	
	vbetool_init();
	/* Again, we don't really want to do this while X is in 
	   control */
	int err = check_console();
	if (err) {
		return err;
	}

	return do_post();
}
#endif

int do_real_post(unsigned pci_device)
{
	int error = 0;
	struct LRMI_regs r;
	memset(&r, 0, sizeof(r));

	/* Several machines seem to want the device that they're POSTing in
	   here */
	r.eax = pci_device;

	/* 0xc000 is the video option ROM.  The init code for each
	   option ROM is at 0x0003 - so jump to c000:0003 and start running */
	r.cs = 0xc000;
	r.ip = 0x0003;

	/* This is all heavily cargo culted but seems to work */
	r.edx = 0x80;
	r.ds = 0x0040;

	if (!LRMI_call(&r)) {
		fprintf(stderr,
			"Error: something went wrong performing real mode call\n");
		error = 1;
	}

	return error;
}

void *rom_cseg;
unsigned char romfile[65536];

int do_vga_post(struct pci_device *dev)
{
	int error = 0;
	/* need to pull the ROM file */
	unsigned int pci_id;
	int ret;

	pci_device_vgaarb_set_target(dev);
	pci_device_vgaarb_lock();
	ret = pci_device_read_rom(dev, romfile);
	if (ret) {
		pci_device_vgaarb_unlock();
		fprintf(stderr,"rom read returned %d\n");
		return 0;
	}

	memcpy(rom_cseg, romfile, 65536);

	pci_id = (dev->bus << 8) + (dev->dev << 3) +
		    (dev->func & 0x7);
	error = do_real_post(pci_id);
	if (error != 0)
		fprintf(stderr,"POST FAILED!\n");

	pci_device_vgaarb_unlock();

	return error;
}

int do_post(void)
{
	struct pci_dev *p;
	unsigned int c;
	int error;
	struct pci_id_match dev_match = {
	PCI_MATCH_ANY, PCI_MATCH_ANY, PCI_MATCH_ANY, PCI_MATCH_ANY,
	(0x03 << 16), 0xff000, 0 };
	struct pci_device *dev, *first_dev = NULL;
	struct pci_device_iterator *iter;

	/* setup rom at 0xc0000 section */
	munmap((void*)0xc0000, 64*1024);
	rom_cseg = mmap((void*)0xc0000, 64*1024, PROT_READ|PROT_WRITE|PROT_EXEC,
			MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
	if (!rom_cseg) {
		fprintf(stderr,"unable to map rom\n");
		return 0;
	}
	
	pci_device_vgaarb_init();
	iter = pci_id_match_iterator_create(&dev_match);
	if (iter == NULL) {
		return 1;
	}

	while ((dev = pci_device_next(iter)) != NULL) {
		if (!first_dev)
			first_dev = dev;
		if (pci_device_is_boot_vga(dev) && (boot_flag == 1)) {
			fprintf(stderr,"not posting %d:%d:%d is firstboot and is boot VGA device\n", dev->bus, dev->dev, dev->func);
			continue;
		}
		if (pci_device_has_kernel_driver(dev)) {
			fprintf(stderr,"not posting %d:%d:%d has kernel driver\n", dev->bus, dev->dev, dev->func);
			continue;
		}
		fprintf(stderr,"pretend posting %d:%d:%d\n", dev->bus, dev->dev, dev->func);
		/* need to route VGA in here */
		error = do_vga_post(dev);
		if (error)
			return error;

	}
	pci_device_vgaarb_set_target(first_dev);
	pci_iterator_destroy(iter);
	pci_device_vgaarb_fini();
	return 0;
}

int check_console()
{
	struct stat stat;

	return 0;

	if (fstat(0, &stat) != 0) {
		fprintf(stderr, "Can't stat() stdin\n");
		return 10;
	}

	if ((stat.st_rdev & 0xff00) != 0x400 || (stat.st_rdev & 0xff) > 63) {
		fprintf(stderr, "To perform this operation, "
			"this program must be run from the console\n");
		return 11;
	}

	ioctl(0, KDSETMODE, KD_GRAPHICS);
	return 0;
}