summaryrefslogtreecommitdiff
path: root/inttool.c
blob: 810f85ac0be50cef43c1374e8c6275b29ab204f8 (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
/*
Run (video) BIOS code for various purposes

Copyright Stuart Bennett <sb476@cam.ac.uk>

Based on vbetool.c, Copyright Matthew Garrett <mjg59@srcf.ucam.org>

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

#include <pci/pci.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 <errno.h>

#include "lrmi.h"

int do_int(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;
}

int main(int argc, char *argv[])
{
	static struct pci_access *pacc;
	struct pci_dev *p;
	unsigned int c;
	unsigned int pci_id;
	int error;

	if (!LRMI_init()) {
		fprintf(stderr, "Failed to initialise LRMI (Linux Real-Mode Interface).\n");
		exit(1);
	}
	
	ioperm(0, 1024, 1);
	iopl(3);
	
	pacc = pci_alloc();
	pacc->numeric_ids = 1;
	pci_init(pacc);

	pci_scan_bus(pacc);

	for (p = pacc->devices; p; p = p->next) {
		c = pci_read_word(p, PCI_CLASS_DEVICE);
		if (c == 0x300) {
			pci_id = (p->bus << 8) + (p->dev << 3) + (p->func & 0x7);
			return (do_int(pci_id));
		}
	}

	return 0;
}