]> Joshua Wise's Git repositories - netwatch.git/blob - net/net.c
Don't worry about me, I'm kind of a noob
[netwatch.git] / net / net.c
1 #include <pci.h>
2 #include <io.h>
3 #include <video_defines.h>
4 #include <smram.h>
5 #include <pci-bother.h>
6 #include <output.h>
7 #include <minilib.h>
8 #include <lwip/init.h>
9 #include "net.h"
10 #include "../aseg/keyboard.h"
11
12 static struct nic *_nic = 0x0;
13
14 extern struct pci_driver a3c90x_driver;
15
16 static char test[1024] = {0};
17
18 static char packet[4096] = {0};
19
20 typedef struct packet_t {
21         char from[6];
22         char to[6];
23         unsigned short ethertype;
24         unsigned short datalen;
25         unsigned char command;
26         char data[];
27 } packet_t;
28
29 static unsigned char vga_read(unsigned char idx)
30 {
31         outb(CRTC_IDX_REG, idx);
32         return inb(CRTC_DATA_REG);
33 }
34
35 static unsigned int vga_base()
36 {
37         return (((unsigned int) vga_read(CRTC_START_ADDR_MSB_IDX)) << 9)
38              + (((unsigned int) vga_read(CRTC_START_ADDR_LSB_IDX)) << 1);
39 }
40
41 void handle_command(packet_t * p)
42 {
43         uint16_t dl = htons(p->datalen);
44         int i;
45
46         outputf("NIC: Command: 0x%x, %d bytes", p->command, dl);
47
48         switch (p->command) {
49         case 0x42:
50                 for (i = 0; i < dl; i++)
51                         kbd_inject_key(p->data[i]);
52                 break;
53         case 0xFE:
54                 outb(0xCF9, 0x4);       /* Reboot */
55                 break;
56         }
57 }
58
59 void eth_poll()
60 {
61         int i;
62 //      static int c;
63         static short pos = 0x0;
64         unsigned short base = vga_base();
65         unsigned char *p = (unsigned char *)0xB8000;
66         smram_state_t old_state;
67         
68         if (!_nic)
69                 return;
70
71         if (_nic->poll(_nic, 0)) {
72                 _nic->packet = packet;
73                 _nic->poll(_nic, 1);
74
75                 packet_t * p = (packet_t *) packet;
76
77                 outputf("NIC: Packet: %d 0x%x", _nic->packetlen, htons(p->ethertype));
78                 if (htons(p->ethertype) == 0x1338) {
79                         if (htons(p->datalen) + sizeof(packet_t) > _nic->packetlen) {
80                                 outputf("NIC: Malformed packet");
81                         } else {
82                                 handle_command(p);
83                         }
84                 }
85         }
86         smram_tseg_set_state(SMRAM_TSEG_OPEN);
87         old_state = smram_save_state();
88         
89 //      if ((c++) % 2)
90 //              return;
91
92         if (((base + 80*25*2)%0x8000) < base)
93         {
94                 if ((pos > ((base + 80*25*2)%0x8000)) && (pos < base))
95                         pos = base;
96         } else if ((pos > base + 80*25*2) || (pos < base))
97                 pos = base;
98         
99         test[0] = pos >> 8;
100         test[1] = pos & 0xFF;
101         test[2] = base >> 8;
102         test[3] = base & 0xFF;
103
104         smram_aseg_set_state(SMRAM_ASEG_SMMCODE);
105         
106         for (i = 4; i < 1024; i++)
107         {
108                 test[i] = p[pos++];
109                 pos %= 0x8000;
110         }
111         smram_restore_state(old_state);
112         _nic->transmit("\x00\x03\x93\x87\x84\x8C", 0x1337, 1024, test);
113 }
114
115 int eth_register(struct nic *nic)
116 {
117         if (_nic)
118                 return -1;
119         _nic = nic;
120         return 0;
121 }
122
123 void eth_init()
124 {
125         /* Required for DMA to work. :( */
126         smram_tseg_set_state(SMRAM_TSEG_OPEN);
127         pci_probe_driver(a3c90x_driver);
128         lwip_init();
129 }
This page took 0.033912 seconds and 4 git commands to generate.