From 722e5aea4f6704ee7a3e7779c5326c157a98195b Mon Sep 17 00:00:00 2001 From: Jacob Potter Date: Sun, 9 Nov 2008 18:22:41 -0500 Subject: [PATCH] some more haxing --- aseg-paging/Makefile | 49 ++++++++++++- aseg-paging/aseg.lds | 2 +- aseg-paging/firstrun.c | 19 ++--- aseg-paging/main.c | 148 +++++++++++++++++++++++++++++++++++++++ aseg-paging/pagetable.c | 59 ---------------- aseg-paging/pagetable.h | 0 aseg-paging/pagingstub.c | 5 +- aseg-paging/smi.c | 61 ++++++---------- 8 files changed, 224 insertions(+), 119 deletions(-) create mode 100644 aseg-paging/main.c delete mode 100644 aseg-paging/pagetable.c delete mode 100644 aseg-paging/pagetable.h diff --git a/aseg-paging/Makefile b/aseg-paging/Makefile index a3a52f8..58d2652 100644 --- a/aseg-paging/Makefile +++ b/aseg-paging/Makefile @@ -6,7 +6,54 @@ CFLAGS=-I../include -I../include/raw \ STUBOBJS=entry.o pagingstub-asm.o pagingstub.o -OBJS=smi.o ../ich2/smi.o ../ich2/smram-ich2.o vga-overlay.o ../pci/pci-raw.o ../lib/minilib.o ../lib/doprnt.o ../lib/sprintf.o ../lib/console.o ../lib/serial.o +LWIP_OBJS = \ + ../lwip/src/core/dhcp.o \ + ../lwip/src/core/dns.o \ + ../lwip/src/core/init.o \ + ../lwip/src/core/ipv4/autoip.o \ + ../lwip/src/core/ipv4/icmp.o \ + ../lwip/src/core/ipv4/igmp.o \ + ../lwip/src/core/ipv4/inet.o \ + ../lwip/src/core/ipv4/inet_chksum.o \ + ../lwip/src/core/ipv4/ip.o \ + ../lwip/src/core/ipv4/ip_addr.o \ + ../lwip/src/core/ipv4/ip_frag.o \ + ../lwip/src/core/mem.o \ + ../lwip/src/core/memp.o \ + ../lwip/src/core/netif.o \ + ../lwip/src/core/pbuf.o \ + ../lwip/src/core/raw.o \ + ../lwip/src/core/stats.o \ + ../lwip/src/core/sys.o \ + ../lwip/src/core/tcp.o \ + ../lwip/src/core/tcp_in.o \ + ../lwip/src/core/tcp_out.o \ + ../lwip/src/core/udp.o \ + ../lwip/src/netif/etharp.o \ + ../lwip/src/netif/ethernetif.o + + +OBJS = ../ich2/smi.o \ + ../ich2/smram-ich2.o \ + ../ich2/ich2-timer.o \ + ../pci/pci.o \ + ../pci/pci-raw.o \ + ../pci/pci-bother.o \ + ../net/net.o \ + ../net/3c90x.o \ + ../lib/minilib.o \ + ../lib/doprnt.o \ + ../lib/sprintf.o \ + ../lib/console.o \ + ../lib/serial.o \ + ../aseg/keyboard.o \ + ../aseg/packet.o \ + $(LWIP_OBJS) \ + smi.o \ + vga-overlay.o \ + main.o \ + firstrun.o + all: aseg.elf diff --git a/aseg-paging/aseg.lds b/aseg-paging/aseg.lds index 760fe92..7c70a08 100644 --- a/aseg-paging/aseg.lds +++ b/aseg-paging/aseg.lds @@ -39,7 +39,7 @@ SECTIONS .info : { _start = .; LONG(0x5754454E); - LONG(__firstrun_start); + LONG(__firstrun_stub); } } diff --git a/aseg-paging/firstrun.c b/aseg-paging/firstrun.c index 97e8692..ca611fa 100644 --- a/aseg-paging/firstrun.c +++ b/aseg-paging/firstrun.c @@ -13,20 +13,12 @@ extern void timer_handler(smi_event_t ev); extern void kbc_handler(smi_event_t ev); extern void gbl_rls_handler(smi_event_t ev); -void __firstrun_start() { - unsigned char *bp; +void smi_init() { smram_state_t smram; smram = smram_save_state(); smram_tseg_set_state(SMRAM_TSEG_OPEN); - serial_init(); -/* - for (bp = (void *)&_bss; (void *)bp < (void *)&_bssend; bp++) - *bp = 0; - - vga_flush_imm(0); -*/ - + outputf("NetWatch running"); /* Try really hard to shut up USB_LEGKEY. */ @@ -37,19 +29,18 @@ void __firstrun_start() { /* Turn on the SMIs we want */ smi_disable(); - /* eth_init(); - + smi_register_handler(SMI_EVENT_FAST_TIMER, timer_handler); smi_enable_event(SMI_EVENT_FAST_TIMER); - + smi_register_handler(SMI_EVENT_DEVTRAP_KBC, kbc_handler); smi_enable_event(SMI_EVENT_DEVTRAP_KBC); smi_register_handler(SMI_EVENT_GBL_RLS, gbl_rls_handler); smi_enable_event(SMI_EVENT_GBL_RLS); -*/ + smi_enable(); vga_flush_imm(1); diff --git a/aseg-paging/main.c b/aseg-paging/main.c new file mode 100644 index 0000000..7b5c093 --- /dev/null +++ b/aseg-paging/main.c @@ -0,0 +1,148 @@ +#include +#include +#include +#include +#include +#include +#include "../net/net.h" +#include "vga-overlay.h" +#include "../aseg/packet.h" +#include "../aseg/keyboard.h" + +unsigned int lastctr = 0; +extern unsigned int counter; + +static int curdev = 0; + +static void cause_kbd_irq() +{ + outl(0x844, 0x0); + outl(0x848, 0x0); + while (inb(0x64) & 0x1) + inb(0x60); + outb(0x60, 0xee); /* Cause an IRQ. */ + while (inb(0x60) != 0xEE) + ; +} + +void pci_dump() { + unsigned long cts; + + cts = inl(0x84C); + + outl(0x840, 0x0); + outl(0x848, 0x0); + switch(cts&0xF0000) + { + case 0x20000: + { + unsigned char b; + + switch (cts & 0xFFFF) + { + case 0x64: + /* Read the real hardware and mask in our OBF if need be. */ + b = inb(0x64); + if (kbd_has_injected_scancode()) + { + dologf("OS wants to know; we have data"); + lastctr = counter; + b |= 0x01; + b &= ~0x20; /* no mouse for you! */ + curdev = 0; + } else + curdev = (b & 0x20) ? 1 : 0; + *(unsigned char*)0xAFFD0 /* EAX */ = b; + break; + case 0x60: + if (kbd_has_injected_scancode()) + { + b = kbd_get_injected_scancode(); + lastctr = counter; + while (inb(0x64) & 0x1) + inb(0x60); + } else + b = inb(0x60); + if ((curdev == 0) && (b == 0x01)) { /* Escape */ + outb(0xCF9, 0x4); /* Reboot */ + return; + } + + /* If there is more nus to come, generate another IRQ. */ + if (kbd_has_injected_scancode()) + cause_kbd_irq(); + + *(unsigned char*)0xAFFD0 /* EAX */ = b; + break; + } + + *(unsigned char*)0xAFFD0 /* EAX */ = b; + break; + } + case 0x30000: + { + unsigned char b; + + b = *(unsigned char*)0xAFFD0 /* EAX */; + dologf("WRITE: %08x (%02x)", cts, b); + outb(cts & 0xFFFF, b); + break; + } + default: + dolog("Unhandled PCI cycle"); + } + + outl(0x840, 0x0); + outl(0x844, 0x1000); + outl(0x848, 0x1000); +} + +void timer_handler(smi_event_t ev) +{ + static unsigned int ticks = 0; + + smi_disable_event(SMI_EVENT_FAST_TIMER); + smi_enable_event(SMI_EVENT_FAST_TIMER); + + if (kbd_has_injected_scancode() && (counter > (lastctr + 2))) + { + smi_disable_event(SMI_EVENT_DEVTRAP_KBC); + dolog("Kicking timer"); + cause_kbd_irq(); + smi_enable_event(SMI_EVENT_DEVTRAP_KBC); + } + + outb(0x80, (ticks++) & 0xFF); + + outlog(); +} + +void kbc_handler(smi_event_t ev) +{ + pci_dump(); +} + +void gbl_rls_handler(smi_event_t ev) +{ + unsigned long ecx; + + ecx = *(unsigned long*)0xAFFD4; + + packet_t * packet = check_packet(ecx); + if (!packet) + { + dologf("WARN: bad packet at %08x", ecx); + return; + } + + dologf("Got packet: type %08x", packet->type); + + if (packet->type == 42) { + dump_log((char *)packet->data); + *(unsigned long*)0xAFFD4 = 42; + } else if (packet->type == 0xAA) { + kbd_inject_key('A'); + } else { + *(unsigned long*)0xAFFD4 = 0x2BADD00D; + } +} diff --git a/aseg-paging/pagetable.c b/aseg-paging/pagetable.c deleted file mode 100644 index 83a1e54..0000000 --- a/aseg-paging/pagetable.c +++ /dev/null @@ -1,59 +0,0 @@ -#include "vm_flags.h" -#include - -#define MAP_FLAGS (PTE_PRESENT | PTE_READ_WRITE) - -void * pt_setup(int smbase) { - int i; - outb(0x80, 0x51); - - /* The page directory and page table live at SMBASE and SMBASE + 0x1000, - * respectively; clear them. */ - int * pagedirectory = (int *) smbase; - int * pagetable = (int *) (smbase + 0x1000); - - /* Clear out the page directory except for one entry pointing to the - * page table, and clear the page table entirely. */ - outb(0x80, 0x52); - pagedirectory[0] = (smbase + 0x1000) | PTE_PRESENT | PTE_READ_WRITE; - outb(0x80, 0x53); - for (i = 1; i < 1024; i++) - { - pagedirectory[i] = 0; - } - - outb(0x80, 0x54); - for (i = 0; i < 1024; i++) - { - pagetable[i] = 0; - } - outb(0x80, 0x55); - - /* The page at 0x10000 - 0x10FFF points to the SMI entry point, - * SMBASE + 0x8000. */ - pagetable[16] = (0x8000 + smbase) | MAP_FLAGS; - - /* 0x11000 to 0x1EFFF map to the rest of ASEG up to SMBASE + 0xF000; - * the page containing the saved state is not mappped to our code - * region. */ - - for (i = 0; i < 8; i++) - { - pagetable[17 + i] = (i * 0x1000 + smbase) | MAP_FLAGS; - } - - for (i = 0; i < 6; i++) - { - pagetable[25 + i] = (smbase + 0x9000 + i * 0x1000) | MAP_FLAGS; - } - - outb(0x80, 0x56); - /* Map 0xA8000 to itself. */ - pagetable[0xA8] = 0xA8000 | MAP_FLAGS; - pagetable[0xA9] = 0xA9000 | MAP_FLAGS; - - /* No TSEG yet. */ - - outb(0x80, 0x57); - return pagedirectory; -} diff --git a/aseg-paging/pagetable.h b/aseg-paging/pagetable.h deleted file mode 100644 index e69de29..0000000 diff --git a/aseg-paging/pagingstub.c b/aseg-paging/pagingstub.c index 0a79385..d839966 100644 --- a/aseg-paging/pagingstub.c +++ b/aseg-paging/pagingstub.c @@ -8,8 +8,8 @@ #include "../net/net.h" #include "vga-overlay.h" +extern void smi_init(); #include "vm_flags.h" -#include "pagetable.h" void set_cr0(unsigned int); void ps_switch_stack (void (*call)(), int stack); @@ -115,7 +115,6 @@ void c_entry(void) outb(0x80, 0x43); if (!entry_initialized) { - extern void __firstrun_start(); /* If needed, copy in data. */ for (bp = (void *)0x200000; (void *)bp < (void *)&_bss; bp++) @@ -124,7 +123,7 @@ void c_entry(void) *bp = 0; serial_init(); dolog("Paging enabled."); - __firstrun_start(); /* Now initialize BSS, etc. */ + smi_init(); entry_initialized = 1; } diff --git a/aseg-paging/smi.c b/aseg-paging/smi.c index 9bc124e..eada78d 100644 --- a/aseg-paging/smi.c +++ b/aseg-paging/smi.c @@ -8,31 +8,10 @@ #include "../net/net.h" #include "vga-overlay.h" -#include "pagetable.h" - unsigned int counter = 0; -unsigned int lastctr = 0; unsigned long pcisave = 0; unsigned char vgasave = 0; -void set_cr0(unsigned int); - -#define get_cr0() \ - ({ \ - register unsigned int _temp__; \ - asm volatile("mov %%cr0, %0" : "=r" (_temp__)); \ - _temp__; \ - }) - - -#define set_cr3(value) \ - { \ - register unsigned int _temp__ = (value); \ - asm volatile("mov %0, %%cr3" : : "r" (_temp__)); \ - } -#define CR0_PG 0x80000000 - - void smi_entry(void) { char statstr[512]; @@ -45,13 +24,14 @@ void smi_entry(void) */ counter++; outb(0x80, 0x2B); - sprintf(statstr, "15-412! %08x %08x", smi_status(), counter); + sprintf(statstr, "NetWatch! %08x %08x", smi_status(), counter); outb(0x80, 0x3B); strblit(statstr, 0, 0); outb(0x80, 0x4B); serial_init(); - dolog("wee!"); +/* dolog("wee!"); + */ /* eth_poll(); @@ -75,30 +55,29 @@ void smi_entry(void) outb(0x3D4, vgasave); } -void timer_handler(smi_event_t ev) -{ - static unsigned int ticks = 0; - - smi_disable_event(SMI_EVENT_FAST_TIMER); - smi_enable_event(SMI_EVENT_FAST_TIMER); - - outb(0x80, (ticks++) & 0xFF); +extern void timer_handler(smi_event_t ev); +extern void kbc_handler(smi_event_t ev); +extern void gbl_rls_handler(smi_event_t ev); - outlog(); -} +void __firstrun_stub() { + /* Try really hard to shut up USB_LEGKEY. */ + pci_write16(0, 31, 2, 0xC0, pci_read16(0, 31, 2, 0xC0)); + pci_write16(0, 31, 2, 0xC0, 0); + pci_write16(0, 31, 4, 0xC0, pci_read16(0, 31, 4, 0xC0)); + pci_write16(0, 31, 4, 0xC0, 0); -void __firstrun_start() { - smram_state_t smram; - - smram = smram_save_state(); - smram_tseg_set_state(SMRAM_TSEG_OPEN); - smi_disable(); - outb(0x80, 0x41); + /* Turn on the SMIs we want */ + smi_disable(); smi_register_handler(SMI_EVENT_FAST_TIMER, timer_handler); smi_enable_event(SMI_EVENT_FAST_TIMER); + smi_register_handler(SMI_EVENT_DEVTRAP_KBC, kbc_handler); + smi_enable_event(SMI_EVENT_DEVTRAP_KBC); + + smi_register_handler(SMI_EVENT_GBL_RLS, gbl_rls_handler); + smi_enable_event(SMI_EVENT_GBL_RLS); + smi_enable(); } - -- 2.39.2