]> Joshua Wise's Git repositories - netwatch.git/blob - netwatch/main.c
Merge nyus.joshuawise.com:/storage/git/netwatch
[netwatch.git] / netwatch / main.c
1 #include <io.h>
2 #include <smram.h>
3 #include <video_defines.h>
4 #include <minilib.h>
5 #include <smi.h>
6 #include <pci-bother.h>
7 #include <fb.h>
8 #include <output.h>
9 #include "../net/net.h"
10 #include "vga-overlay.h"
11 #include "packet.h"
12 #include "keyboard.h"
13
14 unsigned int lastctr = 0;
15 extern unsigned int counter;
16
17 static int _ibf_ready = 0;
18 static int _waiting_for_data = 0;
19 static int curdev = 0;
20 static int adding_locks_from_time_to_time = 0;
21
22 static int _inject_ready()
23 {
24         return _ibf_ready && !_waiting_for_data && !adding_locks_from_time_to_time;
25 }
26
27 void _try_inject()
28 {
29         if (kbd_has_injected_scancode() && _inject_ready())
30         {
31                 smi_disable_event(SMI_EVENT_DEVTRAP_KBC);
32                 int i = 1000;
33                 while (inb(0x64) & 0x02)        /* Busy? */
34                         ;
35                 outb(0x64, 0xD2);       /* "Inject, please!" */
36                 while (inb(0x64) & 0x02)        /* Busy? */
37                         ;
38                 outb(0x60, kbd_get_injected_scancode());        /* data */
39                 while ((inb(0x64) & 0x02) && i--)       /* wait for completion */
40                         ;
41                 outl(0x844, 0x1000);
42                 adding_locks_from_time_to_time++;
43                 smi_enable_event(SMI_EVENT_DEVTRAP_KBC);
44         } else if (kbd_has_injected_scancode())
45                 outputf("Would like to inject, but %d %d", _ibf_ready, _waiting_for_data);
46 }
47
48 void pci_dump() {
49         unsigned long cts;
50                 
51         cts = inl(0x84C);
52         
53         outl(0x840, 0x0);
54         outl(0x848, 0x0);
55         switch(cts&0xF0000)
56         {
57         case 0x20000:
58         {
59                 unsigned char b;
60                 
61                 switch (cts & 0xFFFF)
62                 {
63                 case 0x64:
64                         /* Read the real hardware and mask in our OBF if need be. */
65                         b = inb(0x64);
66                         
67                         curdev = (b & 0x20 /* KBD_STAT_MOUSE_OBF */) ? 1 : 0;
68                         
69                         if ((curdev == 0) && kbd_has_injected_scancode())
70                                 b |= 0x1;
71
72                         _ibf_ready = (b & 0x2 /* IBF */) ? 0 : 1;
73                         
74                         break;
75                 case 0x60:
76                         if ((curdev == 0) && kbd_has_injected_scancode() && !adding_locks_from_time_to_time)
77                                 b = kbd_get_injected_scancode();
78                         else
79                                 b = inb(0x60);
80                         if (adding_locks_from_time_to_time)
81                                 adding_locks_from_time_to_time--;
82                         if ((curdev == 0) && (b == 0x01)) {     /* Escape */
83                                 outb(0xCF9, 0x4);       /* Reboot */
84                                 return;
85                         }
86                         break;
87
88                 default:
89                         b = inb(cts & 0xFFFF);
90                 }
91                 
92                 dologf("READ : %08x (%02x)", cts, b);
93                 *(unsigned char*)0xAFFD0 /* EAX */ = b;
94                 break;
95         }
96         case 0x30000:
97         {
98                 unsigned char b;
99                 
100                 b = *(unsigned char*)0xAFFD0 /* EAX */;
101                 dologf("WRITE: %08x (%02x)", cts, b);
102                 if ((cts & 0xFFFF) == 0x64)
103                         switch(b)
104                         {
105                         case 0x60 /*KBD_CCMD_WRITE_MODE*/:
106                         case 0xD2 /*KBD_CCMD_WRITE_OBUF*/:
107                         case 0xD3 /*KBD_CCMD_WRITE_AUX_OBUF*/:
108                         case 0xD4 /*KBD_CCMD_WRITE_MOUSE*/:
109                         case 0xD1 /*KBD_CCMD_WRITE_OUTPORT*/:
110                                 _waiting_for_data = 1;  /* These all should not be interrupted. */
111                         }
112                 else if ((cts & 0xFFFF) == 0x60)
113                         _waiting_for_data = 0;
114                 outb(cts & 0xFFFF, b);
115                 break;
116         }
117         default:
118                 dolog("Unhandled PCI cycle");
119         }
120         
121         outl(0x840, 0x0);
122         outl(0x844, 0x1000);
123         outl(0x848, 0x1000);
124 }
125
126 void timer_handler(smi_event_t ev)
127 {
128         static unsigned int ticks = 0;
129         
130         smi_disable_event(SMI_EVENT_FAST_TIMER);
131         smi_enable_event(SMI_EVENT_FAST_TIMER);
132         
133         _try_inject();
134         
135         outb(0x80, (ticks++) & 0xFF);
136         
137         if (!fb || fb->curmode.text)
138                 outlog();
139 }
140
141 void kbc_handler(smi_event_t ev)
142 {
143         pci_dump();
144 }
145
146 void gbl_rls_handler(smi_event_t ev)
147 {
148         unsigned long ecx;
149         
150         ecx = *(unsigned long*)0xAFFD4;
151
152         packet_t * packet = check_packet(ecx);
153         if (!packet)
154         {
155                 dologf("WARN: bad packet at %08x", ecx);
156                 return;
157         }
158
159         outputf("Got packet: type %08x", packet->type);
160
161         if (packet->type == 42) {
162                 dump_log((char *)packet->data);
163                 *(unsigned long*)0xAFFD4 = 42;
164         } else if (packet->type == 0xAA) {
165                 kbd_inject_keysym('A', 1);
166                 kbd_inject_keysym('A', 0);
167         } else {
168                 *(unsigned long*)0xAFFD4 = 0x2BADD00D;
169         }
170 }
This page took 0.038677 seconds and 4 git commands to generate.