Thu, 21 Dec 2017 23:07:31 +0100
7 files changed,
67 insertions(+),
7 deletions(-)
A
include/linker.h
@@ -0,0 +1,22 @@
+#ifndef LINKER_H +#define LINKER_H + +#include <stdint.h> + +extern uint8_t __SS_VMA; +extern uint8_t __SS_LMA; +extern uint8_t __SS_SIZE; + +extern uint8_t __KERNEL_VMA; +extern uint8_t __KERNEL_LMA; +extern uint8_t __KERNEL_SIZE; + +#define SS_VMA (&__SS_VMA) +#define SS_LMA (&__SS_LMA) +#define SS_SIZE (uint32_t)(&__SS_SIZE) + +#define KERNEL_VMA (&__KERNEL_VMA) +#define KERNEL_LMA (&__KERNEL_LMA) +#define KERNEL_SIZE (uint32_t)(&__KERNEL_SIZE) + +#endif
M
link.txt
→
link.txt
@@ -8,18 +8,31 @@ MEMORY
{ BOOT : ORIGIN = 0x00007C00, LENGTH = 0x00000200 SECOND_STAGE : ORIGIN = 0x00010000, LENGTH = 0x00090000 + KERNEL : ORIGIN = 0xC0000000, LENGTH = 0x30000000 } SECTIONS { BOOT : AT(0x0000) { - build/boot.o(.text) + build/boot.o(.*) } >BOOT SECOND_STAGE : AT(LOADADDR(BOOT) + SIZEOF(BOOT)) { - build/second_stage.o(.text) - build/second_stage.o(.data) + build/second_stage.o(.*) } >SECOND_STAGE + + KERNEL : AT(LOADADDR(SECOND_STAGE) + SIZEOF(SECOND_STAGE)) + { + + } >KERNEL } + +__SS_LMA = LOADADDR(SECOND_STAGE); +__SS_VMA = ADDR(SECOND_STAGE); +__SS_SIZE = SIZEOF(SECOND_STAGE); + +__KERNEL_LMA = LOADADDR(KERNEL); +__KERNEL_VMA = ADDR(KERNEL); +__KERNEL_SIZE = SIZEOF(KERNEL);
M
second_stage/entry.s
→
second_stage/entry.s
@@ -18,6 +18,8 @@
# TODO: # - copy the kernel code to 0x00100000 + call copy_kernel + # - create a page directory for the kernel that maps it to 0xC0000000 # and identity maps the first 1M of memory # - enable paging
M
second_stage/gdt.c
→
second_stage/gdt.c
@@ -20,4 +20,4 @@ GDT_MAKE_ENTRY(0x00000000, 0x000FFFFF, 0x9A, 0xC),
// identity mapping (data) GDT_MAKE_ENTRY(0x00000000, 0x000FFFFF, 0x92, 0xC) -};+};
M
second_stage/link.txt
→
second_stage/link.txt
@@ -1,4 +1,4 @@
-INPUT(build/entry.o build/gdt.o) +INPUT(build/entry.o build/gdt.o build/main.o) OUTPUT_FORMAT(elf32-i386) OUTPUT_ARCH(i386) ENTRY(_ss_start)@@ -9,11 +9,13 @@ .text :
{ build/entry.o(.text) build/gdt.o(.text) + build/main.o(.text) } .data : { build/entry.o(.data) build/gdt.o(.data) + build/main.o(.data) } }
A
second_stage/main.c
@@ -0,0 +1,18 @@
+#include "linker.h" + +void copy_kernel(void) { + uint8_t *display = (uint8_t*)0xB8000; + uint8_t *text = (uint8_t*)"THIS WORKS"; + + while (*text) { + *display++ = *text++; + *display++ = 0x0F; + } + + uint8_t *kernel_dest = (uint8_t*)0x00100000; + uint8_t *kernel_src = (SS_VMA + (KERNEL_LMA - SS_LMA)); + + for (uint32_t i = 0; i < KERNEL_SIZE; i++) { + kernel_dest[i] = kernel_src[i]; + } +}
M
second_stage/makefile
→
second_stage/makefile
@@ -1,8 +1,8 @@
.RECIPEPREFIX = > .PHONY: build -build: build/entry.o build/gdt.o -> $(GCC_PREFIX)ld -T link.txt -o $(BUILD_DIR)/second_stage.o --oformat elf32-i386 +build: build/entry.o build/gdt.o build/main.o +> $(GCC_PREFIX)ld -r -T link.txt -o $(BUILD_DIR)/second_stage.o --oformat elf32-i386 .PHONY: clear clear:@@ -17,4 +17,7 @@ build/entry.o: entry.s
> $(GCC_PREFIX)as -o $@ $< build/gdt.o: gdt.c gdt.h +> $(GCC_PREFIX)gcc -c -I$(INCLUDE_DIR) --prefix=$(GCC_PREFIX) $(GCC_OPTIONS) -o $@ $< + +build/main.o: main.c > $(GCC_PREFIX)gcc -c -I$(INCLUDE_DIR) --prefix=$(GCC_PREFIX) $(GCC_OPTIONS) -o $@ $<