]>
Commit | Line | Data |
---|---|---|
3c4e084d | 1 | /* main.c |
f3e67112 | 2 | * Main post-paging entry point. Actually, this is a lie. |
3c4e084d JP |
3 | * NetWatch system management mode administration console |
4 | * | |
5 | * Copyright (c) 2008 Jacob Potter and Joshua Wise. All rights reserved. | |
6 | * This program is free software; you can redistribute and/or modify it under | |
7 | * the terms found in the file LICENSE in the root of this source tree. | |
8 | * | |
9 | */ | |
10 | ||
722e5aea JP |
11 | #include <io.h> |
12 | #include <smram.h> | |
13 | #include <video_defines.h> | |
14 | #include <minilib.h> | |
15 | #include <smi.h> | |
16 | #include <pci-bother.h> | |
5cb80fe1 | 17 | #include <fb.h> |
5d467741 | 18 | #include <output.h> |
722e5aea JP |
19 | #include "../net/net.h" |
20 | #include "vga-overlay.h" | |
3116da4b | 21 | #include "packet.h" |
91cc1340 | 22 | #include "keyboard.h" |
722e5aea JP |
23 | |
24 | unsigned int lastctr = 0; | |
25 | extern unsigned int counter; | |
26 | ||
5d467741 JW |
27 | static int _ibf_ready = 0; |
28 | static int _waiting_for_data = 0; | |
722e5aea | 29 | static int curdev = 0; |
ba90bd5c | 30 | static int adding_locks_from_time_to_time = 0; |
722e5aea | 31 | |
692f070e JW |
32 | static int _inject_ready() |
33 | { | |
59ceede5 | 34 | return _ibf_ready && !_waiting_for_data && !adding_locks_from_time_to_time; |
692f070e JW |
35 | } |
36 | ||
37 | void _try_inject() | |
38 | { | |
39 | if (kbd_has_injected_scancode() && _inject_ready()) | |
40 | { | |
41 | smi_disable_event(SMI_EVENT_DEVTRAP_KBC); | |
42 | int i = 1000; | |
43 | while (inb(0x64) & 0x02) /* Busy? */ | |
44 | ; | |
45 | outb(0x64, 0xD2); /* "Inject, please!" */ | |
46 | while (inb(0x64) & 0x02) /* Busy? */ | |
47 | ; | |
48 | outb(0x60, kbd_get_injected_scancode()); /* data */ | |
49 | while ((inb(0x64) & 0x02) && i--) /* wait for completion */ | |
50 | ; | |
337f8b17 JW |
51 | /* On some chipsets, this might set the "device active" bit |
52 | * for the keyboard controller. On ICH2, we appear to get | |
53 | * lucky, but we need a mechanism of saying "I just touched | |
54 | * the keyboard, please don't send me another SMI because of | |
55 | * this"... XXX | |
56 | * ICH2: outl(0x844, 0x1000); | |
57 | */ | |
ba90bd5c | 58 | adding_locks_from_time_to_time++; |
692f070e JW |
59 | smi_enable_event(SMI_EVENT_DEVTRAP_KBC); |
60 | } else if (kbd_has_injected_scancode()) | |
61 | outputf("Would like to inject, but %d %d", _ibf_ready, _waiting_for_data); | |
62 | } | |
63 | ||
722e5aea JP |
64 | void pci_dump() { |
65 | unsigned long cts; | |
66 | ||
67 | cts = inl(0x84C); | |
68 | ||
69 | outl(0x840, 0x0); | |
70 | outl(0x848, 0x0); | |
71 | switch(cts&0xF0000) | |
72 | { | |
73 | case 0x20000: | |
74 | { | |
75 | unsigned char b; | |
76 | ||
77 | switch (cts & 0xFFFF) | |
78 | { | |
79 | case 0x64: | |
80 | /* Read the real hardware and mask in our OBF if need be. */ | |
81 | b = inb(0x64); | |
5d467741 JW |
82 | |
83 | curdev = (b & 0x20 /* KBD_STAT_MOUSE_OBF */) ? 1 : 0; | |
692f070e JW |
84 | |
85 | if ((curdev == 0) && kbd_has_injected_scancode()) | |
86 | b |= 0x1; | |
87 | ||
5d467741 JW |
88 | _ibf_ready = (b & 0x2 /* IBF */) ? 0 : 1; |
89 | ||
722e5aea JP |
90 | break; |
91 | case 0x60: | |
ba90bd5c | 92 | if ((curdev == 0) && kbd_has_injected_scancode() && !adding_locks_from_time_to_time) |
692f070e JW |
93 | b = kbd_get_injected_scancode(); |
94 | else | |
95 | b = inb(0x60); | |
ba90bd5c JW |
96 | if (adding_locks_from_time_to_time) |
97 | adding_locks_from_time_to_time--; | |
722e5aea JP |
98 | if ((curdev == 0) && (b == 0x01)) { /* Escape */ |
99 | outb(0xCF9, 0x4); /* Reboot */ | |
100 | return; | |
101 | } | |
722e5aea | 102 | break; |
cd96f5ed JP |
103 | |
104 | default: | |
105 | b = inb(cts & 0xFFFF); | |
722e5aea | 106 | } |
692f070e JW |
107 | |
108 | dologf("READ : %08x (%02x)", cts, b); | |
722e5aea JP |
109 | *(unsigned char*)0xAFFD0 /* EAX */ = b; |
110 | break; | |
111 | } | |
112 | case 0x30000: | |
113 | { | |
114 | unsigned char b; | |
115 | ||
116 | b = *(unsigned char*)0xAFFD0 /* EAX */; | |
117 | dologf("WRITE: %08x (%02x)", cts, b); | |
5d467741 JW |
118 | if ((cts & 0xFFFF) == 0x64) |
119 | switch(b) | |
120 | { | |
121 | case 0x60 /*KBD_CCMD_WRITE_MODE*/: | |
122 | case 0xD2 /*KBD_CCMD_WRITE_OBUF*/: | |
123 | case 0xD3 /*KBD_CCMD_WRITE_AUX_OBUF*/: | |
124 | case 0xD4 /*KBD_CCMD_WRITE_MOUSE*/: | |
125 | case 0xD1 /*KBD_CCMD_WRITE_OUTPORT*/: | |
126 | _waiting_for_data = 1; /* These all should not be interrupted. */ | |
127 | } | |
128 | else if ((cts & 0xFFFF) == 0x60) | |
129 | _waiting_for_data = 0; | |
722e5aea JP |
130 | outb(cts & 0xFFFF, b); |
131 | break; | |
132 | } | |
133 | default: | |
134 | dolog("Unhandled PCI cycle"); | |
135 | } | |
136 | ||
137 | outl(0x840, 0x0); | |
138 | outl(0x844, 0x1000); | |
139 | outl(0x848, 0x1000); | |
140 | } | |
141 | ||
142 | void timer_handler(smi_event_t ev) | |
143 | { | |
144 | static unsigned int ticks = 0; | |
145 | ||
146 | smi_disable_event(SMI_EVENT_FAST_TIMER); | |
147 | smi_enable_event(SMI_EVENT_FAST_TIMER); | |
148 | ||
692f070e | 149 | _try_inject(); |
722e5aea JP |
150 | |
151 | outb(0x80, (ticks++) & 0xFF); | |
152 | ||
5cb80fe1 JW |
153 | if (!fb || fb->curmode.text) |
154 | outlog(); | |
722e5aea JP |
155 | } |
156 | ||
157 | void kbc_handler(smi_event_t ev) | |
158 | { | |
159 | pci_dump(); | |
160 | } | |
161 | ||
162 | void gbl_rls_handler(smi_event_t ev) | |
163 | { | |
164 | unsigned long ecx; | |
165 | ||
166 | ecx = *(unsigned long*)0xAFFD4; | |
167 | ||
168 | packet_t * packet = check_packet(ecx); | |
169 | if (!packet) | |
170 | { | |
171 | dologf("WARN: bad packet at %08x", ecx); | |
172 | return; | |
173 | } | |
174 | ||
5d467741 | 175 | outputf("Got packet: type %08x", packet->type); |
722e5aea JP |
176 | |
177 | if (packet->type == 42) { | |
178 | dump_log((char *)packet->data); | |
179 | *(unsigned long*)0xAFFD4 = 42; | |
180 | } else if (packet->type == 0xAA) { | |
91cc1340 JP |
181 | kbd_inject_keysym('A', 1); |
182 | kbd_inject_keysym('A', 0); | |
722e5aea JP |
183 | } else { |
184 | *(unsigned long*)0xAFFD4 = 0x2BADD00D; | |
185 | } | |
186 | } |