* Created a header file that currently hard points to ich2.h.
  * Allows for saving and restoring of SMRAM state.
  * Moved PCI to include.
  * Moved conflicting includes to include/raw, which is only included on
    non-Linux build targets.
  * Fixed some messages here and there.
 CC=gcc
-CFLAGS=-I../include -nostdlib -nostdinc -fno-builtin
+CFLAGS=-I../include -I../include/raw -nostdlib -nostdinc -fno-builtin
 OBJS=counter.o ../pci/pci-raw.o ../lib/minilib.o ../lib/console.o
 
 all: aseg.elf
 
 OBJS=multiboot_c.o multiboot_asm.o realmode.o loader.o ../pci/pci-raw.o ../lib/minilib.o ../lib/console.o
 CC=gcc
-CFLAGS=-nostdlib -I../include -I. -fno-builtin -nostdinc
+CFLAGS=-nostdlib -I../include -I../include/raw -I. -fno-builtin -nostdinc
 
 all: multiboot
 
 
        
        void (*realmode)() = (void (*)()) 0x4000;
        
-       puts("Magic is: ");
-       puthex(magic);
-       puts("\nMultiboot header is: ");
-       puthex(wee);
-       puts("\n");
        show_cursor();
+       puts("NetWatch loader\n");
+       
+       if (magic != 0x2BADB002)
+       {
+               puts("Bootloader was not multiboot compliant; cannot continue.\n");
+               while(1) asm("hlt");
+       }
        
-       puts("Grubptr is: ");
-       puthex(*grubptr);
-       puts("\n");
-
-
        for (i = 0; i < wee->mod_cnt; i++)
        {
-               puts("Module:\n");
+               puts("Module found:\n");
                puts("  Start: "); puthex(wee->mods[i].mod_start); puts("\n");
                puts("  Size: "); puthex(wee->mods[i].mod_end - wee->mods[i].mod_start); puts("\n");
                puts("  Name: "); puts(wee->mods[i].mod_string); puts("\n");
        puts("Current SMI state is: "); puthex(inl(0x830)); puts("\n");
        puts("Current SMRAMC state is: "); puthex(pci_read8(0, 0, 0, 0x70)); puts("\n");
        
-       outl(0x830, inl(0x830) & ~0x2001);      /* turn off SMIs */
+       outl(0x830, inl(0x830) & ~0x1); /* turn off SMIs */
        
        /* Try really hard to shut up USB_LEGKEY. */
        pci_write16(0, 31, 2, 0xC0, pci_read16(0, 31, 2, 0xC0));
 
 #include "reg-82815.h"
+#include <pci.h>
+#include <smram.h>
 
-unsigned long memsz[] = {
+#ifdef __linux__
+
+static unsigned long memsz[] = {
        0,                      // 0
        32*1024*1024,           // 1
        32*1024*1024,           // 2
        512*1024*1024           // F
 };
 
-#ifdef __linux__
-
 void smram_aseg_dump(void) {
 
        unsigned char smramc, drp, drp2;
 }
 #endif
 
+int smram_locked()
+{
+       unsigned char smramc = pci_read8(0, 0, 0, SMRAMC);
+       
+       return (smramc & SMRAMC_LOCK) ? 1 : 0;
+}
+
+smram_state_t smram_save_state()
+{
+       return pci_read8(0, 0, 0, SMRAMC);
+}
+
+void smram_restore_state(smram_state_t state)
+{
+       return pci_write8(0, 0, 0, SMRAMC, state); 
+}
+
 int smram_aseg_set_state (int open) {
        unsigned char smramc;
+
+       if (smram_locked())
+               return -1;
+               
        smramc = pci_read8(0, 0, 0, SMRAMC);
 
-       if (smramc & SMRAMC_LOCK)
+       switch (open)
        {
-               /* SMRAM is locked; can't load anything. */
-               return 1;
-       }
-
-       if (open) {
-               /* Set LSMM to 01 (ABseg = system RAM) */
+       case SMRAM_ASEG_CLOSED:
+               smramc = (smramc & 0xF0) | 0x00;
+               break;
+       case SMRAM_ASEG_OPEN:
                smramc = (smramc & 0xF0) | 0x04;
-       } else {
-               /* Set LSMM to 11 (ABseg = SMM RAM) */
+               break;
+       case SMRAM_ASEG_SMMCODE:
+               smramc = (smramc & 0xF0) | 0x08;
+               break;
+       case SMRAM_ASEG_SMMONLY:
                smramc = (smramc & 0xF0) | 0x0C;
+               break;
+       default:
+               return -1;
        }
 
-       pci_write8(0, 0, 0, SMRAMC);
+       pci_write8(0, 0, 0, SMRAMC, smramc);
 
        return 0;
 }
 
--- /dev/null
+#ifndef __SMRAM_ICH2_H
+#define __SMRAM_ICH2_H
+
+typedef unsigned char smram_state_t;
+
+#endif
 
 #ifndef PCI_H
 #define PCI_H
 
+#include <stdint.h>
+
 /* General PCI functions. This is implemented by pci-linux.c and pci-raw.c; the
  * former uses Linux's /proc/bus/pci interface for access from userspace, while
  * the latter accesses the PCI hardware directly.
 
--- /dev/null
+#ifndef __SMRAM_H
+#define __SMRAM_H
+
+#include "../ich2/smram-ich2.h"
+
+extern int smram_locked();
+extern smram_state_t smram_save_state();
+extern void smram_restore_state(smram_state_t state);
+extern int smram_aseg_set_state (int open);
+
+#define SMRAM_ASEG_CLOSED 0    /* SMRAM is not readable. */
+#define SMRAM_ASEG_OPEN 1      /* SMRAM is readable by everybody. */
+#define SMRAM_ASEG_SMMCODE 2   /* SMRAM is readable as SMM code only. */
+#define SMRAM_ASEG_SMMONLY 3   /* SMRAM is readable as SMM code and data only. */
+
+#endif
 
 #include <stdio.h>
 #include <inttypes.h>
 
-#include "pci.h"
+#include "../include/pci.h"
 
 static int _open(int bus, int slot, int fn)
 {
 
 #include <io.h>
 #include <stdint.h>
-#include "pci.h"
+#include <pci.h>
 
 static void __pci_config(int bus, int slot, int fn, int addr)
 {
 
-smram-ich2: smram-linux-tool.c ../pci/pci-linux.c ../ich2/smram-ich2.c 
-       gcc -o smram-ich2 smram-linux-tool.c ../pci/pci-linux.c ../ich2/smram-ich2.c 
+CFLAGS=-I../include
+CC=gcc
+SMRAM_ICH2_OBJS=smram-linux-tool.o ../pci/pci-linux.o ../ich2/smram-ich2.o
+
+smram-ich2: $(SMRAM_ICH2_OBJS)
+       gcc -o smram-ich2 $(SMRAM_ICH2_OBJS)
 
 #include <getopt.h>
 #include <stdlib.h>
 #include <stdio.h>
+#include <smram.h>
 
 static struct option longopts[] = {
        { "open",       no_argument,    NULL,   'o'     },
 {
        if (geteuid() != 0)
        {
-               printf("This program must be run as root, dumbass.\n");
+               printf("%s: This program must be run as root.\n", argv[0]);
                return 1;
        }
        
 
        if (op & OP_SET)
        {
-               smram_aseg_set_state(do_open);
+               smram_aseg_set_state(do_open ? SMRAM_ASEG_OPEN : SMRAM_ASEG_SMMONLY);
        }
        
        return 0;