]> Joshua Wise's Git repositories - netwatch.git/blame - netwatch/main.c
Move the Multiboot code around to have more sane filenames.
[netwatch.git] / netwatch / main.c
CommitLineData
3c4e084d
JP
1/* main.c
2 * Main post-paging entry point.
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
24unsigned int lastctr = 0;
25extern unsigned int counter;
26
5d467741
JW
27static int _ibf_ready = 0;
28static int _waiting_for_data = 0;
722e5aea 29static int curdev = 0;
ba90bd5c 30static int adding_locks_from_time_to_time = 0;
722e5aea 31
692f070e
JW
32static int _inject_ready()
33{
59ceede5 34 return _ibf_ready && !_waiting_for_data && !adding_locks_from_time_to_time;
692f070e
JW
35}
36
37void _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 ;
633292e9 51 outl(0x844, 0x1000);
ba90bd5c 52 adding_locks_from_time_to_time++;
692f070e
JW
53 smi_enable_event(SMI_EVENT_DEVTRAP_KBC);
54 } else if (kbd_has_injected_scancode())
55 outputf("Would like to inject, but %d %d", _ibf_ready, _waiting_for_data);
56}
57
722e5aea
JP
58void pci_dump() {
59 unsigned long cts;
60
61 cts = inl(0x84C);
62
63 outl(0x840, 0x0);
64 outl(0x848, 0x0);
65 switch(cts&0xF0000)
66 {
67 case 0x20000:
68 {
69 unsigned char b;
70
71 switch (cts & 0xFFFF)
72 {
73 case 0x64:
74 /* Read the real hardware and mask in our OBF if need be. */
75 b = inb(0x64);
5d467741
JW
76
77 curdev = (b & 0x20 /* KBD_STAT_MOUSE_OBF */) ? 1 : 0;
692f070e
JW
78
79 if ((curdev == 0) && kbd_has_injected_scancode())
80 b |= 0x1;
81
5d467741
JW
82 _ibf_ready = (b & 0x2 /* IBF */) ? 0 : 1;
83
722e5aea
JP
84 break;
85 case 0x60:
ba90bd5c 86 if ((curdev == 0) && kbd_has_injected_scancode() && !adding_locks_from_time_to_time)
692f070e
JW
87 b = kbd_get_injected_scancode();
88 else
89 b = inb(0x60);
ba90bd5c
JW
90 if (adding_locks_from_time_to_time)
91 adding_locks_from_time_to_time--;
722e5aea
JP
92 if ((curdev == 0) && (b == 0x01)) { /* Escape */
93 outb(0xCF9, 0x4); /* Reboot */
94 return;
95 }
722e5aea 96 break;
cd96f5ed
JP
97
98 default:
99 b = inb(cts & 0xFFFF);
722e5aea 100 }
692f070e
JW
101
102 dologf("READ : %08x (%02x)", cts, b);
722e5aea
JP
103 *(unsigned char*)0xAFFD0 /* EAX */ = b;
104 break;
105 }
106 case 0x30000:
107 {
108 unsigned char b;
109
110 b = *(unsigned char*)0xAFFD0 /* EAX */;
111 dologf("WRITE: %08x (%02x)", cts, b);
5d467741
JW
112 if ((cts & 0xFFFF) == 0x64)
113 switch(b)
114 {
115 case 0x60 /*KBD_CCMD_WRITE_MODE*/:
116 case 0xD2 /*KBD_CCMD_WRITE_OBUF*/:
117 case 0xD3 /*KBD_CCMD_WRITE_AUX_OBUF*/:
118 case 0xD4 /*KBD_CCMD_WRITE_MOUSE*/:
119 case 0xD1 /*KBD_CCMD_WRITE_OUTPORT*/:
120 _waiting_for_data = 1; /* These all should not be interrupted. */
121 }
122 else if ((cts & 0xFFFF) == 0x60)
123 _waiting_for_data = 0;
722e5aea
JP
124 outb(cts & 0xFFFF, b);
125 break;
126 }
127 default:
128 dolog("Unhandled PCI cycle");
129 }
130
131 outl(0x840, 0x0);
132 outl(0x844, 0x1000);
133 outl(0x848, 0x1000);
134}
135
136void timer_handler(smi_event_t ev)
137{
138 static unsigned int ticks = 0;
139
140 smi_disable_event(SMI_EVENT_FAST_TIMER);
141 smi_enable_event(SMI_EVENT_FAST_TIMER);
142
692f070e 143 _try_inject();
722e5aea
JP
144
145 outb(0x80, (ticks++) & 0xFF);
146
5cb80fe1
JW
147 if (!fb || fb->curmode.text)
148 outlog();
722e5aea
JP
149}
150
151void kbc_handler(smi_event_t ev)
152{
153 pci_dump();
154}
155
156void gbl_rls_handler(smi_event_t ev)
157{
158 unsigned long ecx;
159
160 ecx = *(unsigned long*)0xAFFD4;
161
162 packet_t * packet = check_packet(ecx);
163 if (!packet)
164 {
165 dologf("WARN: bad packet at %08x", ecx);
166 return;
167 }
168
5d467741 169 outputf("Got packet: type %08x", packet->type);
722e5aea
JP
170
171 if (packet->type == 42) {
172 dump_log((char *)packet->data);
173 *(unsigned long*)0xAFFD4 = 42;
174 } else if (packet->type == 0xAA) {
91cc1340
JP
175 kbd_inject_keysym('A', 1);
176 kbd_inject_keysym('A', 0);
722e5aea
JP
177 } else {
178 *(unsigned long*)0xAFFD4 = 0x2BADD00D;
179 }
180}
This page took 0.045281 seconds and 4 git commands to generate.