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