diff --git a/.gitignore b/.gitignore index c6127b3..90ad045 100644 --- a/.gitignore +++ b/.gitignore @@ -50,3 +50,5 @@ modules.order Module.symvers Mkfile.old dkms.conf + +*.img diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..98e1d00 --- /dev/null +++ b/Makefile @@ -0,0 +1,59 @@ +.RECIPEPREFIX += +.POSIX: + +BIN = bin +SRC = src +CC = gcc +CFLAGS = -Wextra -fno-builtin -fno-stack-protector -m32 -nostdlib -nostdinc -std=gnu99 -ggdb3 +LINK = ld +LINKFLAGS = -melf_i386 -Tlink.ld +CASM = nasm +ASMFLAGS = -f elf +GRUB = grub-mkrescue +GRUBFLAGS = iso + +BOOT = iso/boot +ELF = $(BIN)/main.elf +IMG = $(BIN)/main.img + +OBJS := $(foreach IN_EXT, .c .asm, $(patsubst %$(IN_EXT), %.o, $(wildcard $(SRC)/*$(IN_EXT)))) $(SRC)/boot.o + +all: $(IMG) + +$(IMG): $(ELF) + @echo " GRUB $<" + @cp '$<' '$(BOOT)' + @$(GRUB) -o '$@' $(GRUBFLAGS) + +$(ELF): $(OBJS) + @echo " LINK $<" + @$(LINK) -o '$(ELF)' $(LINKFLAGS) $^ + +$(SRC)/%.o: $(SRC)/%.c + @mkdir -p $(BIN) + @echo " CC $<" + @$(CC) $(CFLAGS) -o '$@' -c '$<' $(CLIBS) + +$(SRC)/%.o: $(SRC)/%.asm + @echo " CASM $<" + @$(CASM) $(ASMFLAGS) $< -o $@ + +bochs: $(IMG) + CYLINDERS="$$(($$(stat -c '%s' '$<') / 512))" && \ + bochs -qf /dev/null \ + 'ata0-master: type=disk, path="$<", mode=flat, cylinders='"$$CYLINDERS"', heads=1, spt=1' \ + 'boot: disk' \ + 'display_library: sdl' \ + 'megs: 128' + +clean: + rm -rf $(BIN) $(SRC)/*.o $(BOOT)/*.elf + +debug: $(IMG) + qemu-system-i386 -hda '$<' -S -s & + @gdb $(ELF) -x gdb.gdb + +qemu: $(IMG) + qemu-system-i386 -drive file=$<,format=raw + +.PHONY: all bochs clean debug qemu diff --git a/iso/boot/grub/grub.cfg b/iso/boot/grub/grub.cfg new file mode 100644 index 0000000..a8cc94c --- /dev/null +++ b/iso/boot/grub/grub.cfg @@ -0,0 +1,5 @@ +set timeout=0 +set default="0" +menuentry "main" { + multiboot /boot/main.elf +} diff --git a/link.ld b/link.ld new file mode 100644 index 0000000..d6fe316 --- /dev/null +++ b/link.ld @@ -0,0 +1,32 @@ +ENTRY(start) +SECTIONS +{ + + .mbheader 0x100000 : + { + *(.mbheader) + } + .text : + { + code = .; _code = .; __code = .; + *(.text) + . = ALIGN(4096); + } + + .data : + { + data = .; _data = .; __data = .; + *(.data) + *(.rodata) + . = ALIGN(4096); + } + + .bss : + { + bss = .; _bss = .; __bss = .; + *(.bss) + . = ALIGN(4096); + } + + end = .; _end = .; __end = .; +} diff --git a/src/boot.asm b/src/boot.asm new file mode 100644 index 0000000..ab04570 --- /dev/null +++ b/src/boot.asm @@ -0,0 +1,39 @@ +MBOOT_PAGE_ALIGN equ 1<<0 ; Load kernel and modules on a page boundary +MBOOT_MEM_INFO equ 1<<1 ; Provide your kernel with memory info +MBOOT_HEADER_MAGIC equ 0x1BADB002 ; Multiboot Magic value +; NOTE: We do not use MBOOT_AOUT_KLUDGE. It means that GRUB does not +; pass us a symbol table. +MBOOT_HEADER_FLAGS equ MBOOT_PAGE_ALIGN | MBOOT_MEM_INFO +MBOOT_CHECKSUM equ -(MBOOT_HEADER_MAGIC + MBOOT_HEADER_FLAGS) + + +[BITS 32] + +[GLOBAL mboot] +[EXTERN code] +[EXTERN bss] +[EXTERN end] + +section .mbheader + +mboot: + dd MBOOT_HEADER_MAGIC + dd MBOOT_HEADER_FLAGS + dd MBOOT_CHECKSUM + dd mboot + dd code + dd bss + dd end + dd start + +section .text + +[GLOBAL start] +[EXTERN main] + +start: + ; Load multiboot information: + push ebx + cli + call main + jmp $ diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..400ba67 --- /dev/null +++ b/src/main.c @@ -0,0 +1,5 @@ +struct multiboot; + +int main(struct multiboot *mboot_ptr) { + return 0xDEADBABA; +}