]> Joshua Wise's Git repositories - netwatch.git/blob - aseg-paging/main.c
Add crc32.
[netwatch.git] / aseg-paging / 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 "../aseg/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                 
89                 dologf("READ : %08x (%02x)", cts, b);
90                 *(unsigned char*)0xAFFD0 /* EAX */ = b;
91                 break;
92         }
93         case 0x30000:
94         {
95                 unsigned char b;
96                 
97                 b = *(unsigned char*)0xAFFD0 /* EAX */;
98                 dologf("WRITE: %08x (%02x)", cts, b);
99                 if ((cts & 0xFFFF) == 0x64)
100                         switch(b)
101                         {
102                         case 0x60 /*KBD_CCMD_WRITE_MODE*/:
103                         case 0xD2 /*KBD_CCMD_WRITE_OBUF*/:
104                         case 0xD3 /*KBD_CCMD_WRITE_AUX_OBUF*/:
105                         case 0xD4 /*KBD_CCMD_WRITE_MOUSE*/:
106                         case 0xD1 /*KBD_CCMD_WRITE_OUTPORT*/:
107                                 _waiting_for_data = 1;  /* These all should not be interrupted. */
108                         }
109                 else if ((cts & 0xFFFF) == 0x60)
110                         _waiting_for_data = 0;
111                 outb(cts & 0xFFFF, b);
112                 break;
113         }
114         default:
115                 dolog("Unhandled PCI cycle");
116         }
117         
118         outl(0x840, 0x0);
119         outl(0x844, 0x1000);
120         outl(0x848, 0x1000);
121 }
122
123 void timer_handler(smi_event_t ev)
124 {
125         static unsigned int ticks = 0;
126         
127         smi_disable_event(SMI_EVENT_FAST_TIMER);
128         smi_enable_event(SMI_EVENT_FAST_TIMER);
129         
130         _try_inject();
131         
132         outb(0x80, (ticks++) & 0xFF);
133         
134         if (!fb || fb->curmode.text)
135                 outlog();
136 }
137
138 void kbc_handler(smi_event_t ev)
139 {
140         pci_dump();
141 }
142
143 void gbl_rls_handler(smi_event_t ev)
144 {
145         unsigned long ecx;
146         
147         ecx = *(unsigned long*)0xAFFD4;
148
149         packet_t * packet = check_packet(ecx);
150         if (!packet)
151         {
152                 dologf("WARN: bad packet at %08x", ecx);
153                 return;
154         }
155
156         outputf("Got packet: type %08x", packet->type);
157
158         if (packet->type == 42) {
159                 dump_log((char *)packet->data);
160                 *(unsigned long*)0xAFFD4 = 42;
161         } else if (packet->type == 0xAA) {
162                 kbd_inject_keysym('A', 1);
163                 kbd_inject_keysym('A', 0);
164         } else {
165                 *(unsigned long*)0xAFFD4 = 0x2BADD00D;
166         }
167 }
This page took 0.029323 seconds and 4 git commands to generate.