From 035d7af7dd4727735ec57f8195a4842e31ffc997 Mon Sep 17 00:00:00 2001 From: Joshua Wise Date: Fri, 5 Sep 2008 20:42:56 -0400 Subject: [PATCH] Move more functionality out to C --- aseg/Makefile | 6 +++-- aseg/aseg.asm | 70 +++----------------------------------------------- aseg/counter.c | 51 +++++++++++++++++++++++++++++++++--- include/io.h | 11 ++++++++ pci/pci-smm.c | 43 +++++++++++++++++++++++++++++++ 5 files changed, 110 insertions(+), 71 deletions(-) create mode 100644 include/io.h create mode 100644 pci/pci-smm.c diff --git a/aseg/Makefile b/aseg/Makefile index 20e5ae9..5676489 100644 --- a/aseg/Makefile +++ b/aseg/Makefile @@ -1,3 +1,5 @@ +CFLAGS=-I../include + all: aseg.elf aseg.bin: aseg.asm @@ -6,5 +8,5 @@ aseg.bin: aseg.asm aseg.o: aseg.bin objcopy -I binary -B i386 -O elf32-i386 aseg.bin aseg.o -aseg.elf: aseg.o aseg.lds counter.o - ld -o aseg.elf -T aseg.lds counter.o +aseg.elf: aseg.o aseg.lds counter.o ../pci/pci-smm.o + ld -o aseg.elf -T aseg.lds counter.o ../pci/pci-smm.o diff --git a/aseg/aseg.asm b/aseg/aseg.asm index 244cafc..93b61d7 100644 --- a/aseg/aseg.asm +++ b/aseg/aseg.asm @@ -23,65 +23,6 @@ continue: mov ss, ax mov esp, [dataptr] -; mov al, [cstat] -; add al, 1 -; out 0x80, al -; mov [cstat], al - - mov dx, 0xCF8 ; save off the old config value - in dword eax, dx - mov [esp-4], eax - - mov eax, 0x80000070 ; load in smramc - out dx, eax - mov dx, 0xCFC - in byte al, dx - mov [esp-5], al - and al, 0xF3 ; Allow graphics access - or al, 0x08 - out dx, al - - xor eax, eax - mov dx, 0x3D4 - in byte al, dx - mov [esp-6], al ; save off the old VGA command - mov al, 0xC - out dx, al - inc dx - in al, dx - mov ah, al - dec dx - mov al, 0xD - out dx, al - inc dx - in al, dx - shl eax, 1 - add eax, 0xB8000 ; yay - mov byte [eax+0], '1' - mov byte [eax+1], 0x1F - mov byte [eax+2], '5' - mov byte [eax+3], 0x1F - mov byte [eax+4], '-' - mov byte [eax+5], 0x1F - mov byte [eax+6], '4' - mov byte [eax+7], 0x1F - mov byte [eax+8], '1' - mov byte [eax+9], 0x1F - mov byte [eax+10], '2' - mov byte [eax+11], 0x1F - - mov dx, 0x3D4 ; restore the old stuff - mov al, [esp-6] - out dx, al - - mov dx, 0xCFC ; restore smramc - mov al, [esp-5] - out dx, al - - mov dx, 0xCF8 ; restore the old PCI config value - mov eax, [esp-4] - out dx, eax - mov al, [needclear] cmp al, 0 jz noclear @@ -109,9 +50,6 @@ noclear: rsm ; and leave SMM -needclear: - db 0x01 - align 0x4 gdtr: db 0x27, 0x00 @@ -123,12 +61,12 @@ gdt: db 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x9B, 0xCF, 0x00 ; code segment db 0xFF, 0xFF, 0x00, 0x80, 0x0A, 0x9B, 0xCF, 0x00 ; code segment for trampoline -cstat: - db 0x00 +needclear: + db 0x01 -TIMES 512-($-$$) DB 0 dataptr: ; 4 bytes of stack top ; 4 bytes of BSS start ; 4 bytes of BSS length - ; 4 bytes of C entry point \ No newline at end of file + ; 4 bytes of C entry point + ; These show up diff --git a/aseg/counter.c b/aseg/counter.c index edad021..de82574 100644 --- a/aseg/counter.c +++ b/aseg/counter.c @@ -1,11 +1,56 @@ +#include + char counter = 0; +unsigned long pcisave; +unsigned char vgasave; +unsigned char thestr[512]; -#define outb(port, val) \ -({ asm volatile("outb %0, %%dx" : : "a" ((unsigned char)(val)) , "d" ((unsigned short)(port))); }) +void memcpy(char *dst, char *src, int c) +{ + while (c--) + *(dst++) = *(src++); +} + +void strcpy(char *dst, char *src) +{ + while (*src) + *(dst++) = *(src++); +} + +unsigned char vgaread(unsigned char idx) +{ + outb(0x3D4, idx); + inb(0x3D5); +} + +void strblit(char *src) +{ + char *destp = (char*)(0xB8000 | (vgaread(0xC) << 5) | (vgaread(0xD) << 1)); + while (*src) + { + *(destp++) = *(src++); + *(destp++) = 0x1F; + } +} void __start (void) { + unsigned char smramc; + + pcisave = inl(0xCF8); + vgasave = inb(0x3D4); + counter++; - outb (0x80, counter); + outb(0x80, counter); + + strcpy(thestr, "15-412!"); + + smramc = pci_read8(0, 0, 0, 0x70); + pci_write8(0, 0, 0, 0x70, (smramc & 0xF3) | 0x08); + strblit(thestr); + pci_write8(0, 0, 0, 0x70, smramc); + + outl(0xCF8, pcisave); + outb(0x3D4, vgasave); } diff --git a/include/io.h b/include/io.h new file mode 100644 index 0000000..5fd0b19 --- /dev/null +++ b/include/io.h @@ -0,0 +1,11 @@ +#ifndef __IO_H +#define __IO_H + +#define inb(port) ({ unsigned char val; asm volatile("inb %%dx, %0" : "=a" ((unsigned char)(val)) : "d" ((unsigned short)(port))); val; }) +#define inw(port) ({ unsigned short val; asm volatile("inw %%dx, %0" : "=a" ((unsigned short)(val)) : "d" ((unsigned short)(port))); val; }) +#define inl(port) ({ unsigned long val; asm volatile("inl %%dx, %0" : "=a" ((unsigned long)(val)) : "d" ((unsigned short)(port))); val; }) +#define outb(port, val) ({ asm volatile("outb %0, %%dx" : : "a" ((unsigned char)(val)) , "d" ((unsigned short)(port))); }) +#define outw(port, val) ({ asm volatile("outw %0, %%dx" : : "a" ((unsigned short)(val)) , "d" ((unsigned short)(port))); }) +#define outl(port, val) ({ asm volatile("outl %0, %%dx" : : "a" ((unsigned long)(val)) , "d" ((unsigned short)(port))); }) + +#endif diff --git a/pci/pci-smm.c b/pci/pci-smm.c new file mode 100644 index 0000000..117a8b5 --- /dev/null +++ b/pci/pci-smm.c @@ -0,0 +1,43 @@ +#include +#include + +static void __pci_config(int bus, int slot, int fn, int addr) +{ + outl(0xCF8, 0x80000000ULL | (bus << 16) | (slot << 11) | (fn << 8) | addr); +} + +void pci_write32(int bus, int slot, int fn, int addr, uint32_t data) +{ + __pci_config(bus, slot, fn, addr); + outl(0xCFC, data); +} + +void pci_write16(int bus, int slot, int fn, int addr, uint16_t data) +{ + __pci_config(bus, slot, fn, addr); + outw(0xCFC, data); +} + +void pci_write8(int bus, int slot, int fn, int addr, uint8_t data) +{ + __pci_config(bus, slot, fn, addr); + outb(0xCFC, data); +} + +uint32_t pci_read32(int bus, int slot, int fn, int addr) +{ + __pci_config(bus, slot, fn, addr); + return inl(0xCFC); +} + +uint16_t pci_read16(int bus, int slot, int fn, int addr) +{ + __pci_config(bus, slot, fn, addr); + return inw(0xCFC); +} + +uint8_t pci_read8(int bus, int slot, int fn, int addr) +{ + __pci_config(bus, slot, fn, addr); + return inb(0xCFC); +} -- 2.43.0