Skip to content

Commit

Permalink
Kernel: Use a multiboot header instead of a convoluted two-part bootl…
Browse files Browse the repository at this point in the history
…oader.

The old bootloader was hilariously complicated, requiring a floppy disk with
the kernel on it, and a hard drive with the file system. This patch removes
the floppy disk from the equation and replaces it with a multiboot header.
This means the kernel can now be booted with qemu-system-i386 -kernel kernel
  • Loading branch information
awesomekling committed Apr 1, 2019
1 parent d5a9f45 commit ee4d7c1
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 293 deletions.
3 changes: 1 addition & 2 deletions Kernel/.gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
*.o
*.d
.floppy-image
Boot/boot.bin
Boot/boot.ao
kernel
kernel.map
_fs_contents
Expand Down
64 changes: 64 additions & 0 deletions Kernel/Boot/boot.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
.set MULTIBOOT_MAGIC, 0x1badb002
.set MULTIBOOT_PAGE_ALIGN, 0x1
.set MULTIBOOT_MEMORY_INFO, 0x2
.set MULTIBOOT_VIDEO_MODE, 0x4
.set multiboot_flags, MULTIBOOT_PAGE_ALIGN | MULTIBOOT_MEMORY_INFO | MULTIBOOT_VIDEO_MODE
.set multiboot_checksum, -(MULTIBOOT_MAGIC + multiboot_flags)

.section .multiboot
.align 4

.long MULTIBOOT_MAGIC
.long multiboot_flags
.long multiboot_checksum


/* for MULTIBOOT_MEMORY_INFO */
.long 0x00000000 /* header_addr */
.long 0x00000000 /* load_addr */
.long 0x00000000 /* load_end_addr */
.long 0x00000000 /* bss_end_addr */
.long 0x00000000 /* entry_addr */

/* for MULTIBOOT_VIDEO_MODE */
.long 0x00000000 /* mode_type */
.long 0 /* width */
.long 0 /* height */
.long 32 /* depth */

.section .stack, "aw", @nobits
stack_bottom:
.skip 32768
stack_top:

.section .text

.global start
.type start, @function

.extern init
.type init, @function

start:
mov $stack_top, %esp

and $-16, %esp

pushl %esp
pushl %eax /* Multiboot header magic */
pushl %ebx /* Multiboot header pointer */

cli
call init

pushl $exit_message
call kprintf

cli

loop:
hlt
jmp loop

exit_message:
.asciz "Kernel exited."
252 changes: 0 additions & 252 deletions Kernel/Boot/boot.asm

This file was deleted.

26 changes: 9 additions & 17 deletions Kernel/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
KERNEL_OBJS = \
_start.o \
init.o \
kmalloc.o \
StdLib.o \
Expand Down Expand Up @@ -65,12 +64,10 @@ AK_OBJS = \
../AK/FileSystemPath.o \
../AK/StdLibExtras.o

OBJS = $(KERNEL_OBJS) $(VFS_OBJS) $(AK_OBJS)
CXX_OBJS = $(KERNEL_OBJS) $(VFS_OBJS) $(AK_OBJS)
OBJS = $(CXX_OBJS) Boot/boot.ao

NASM = nasm
KERNEL = kernel
BOOTLOADER = Boot/boot.bin
IMAGE = .floppy-image
ARCH_FLAGS =
STANDARD_FLAGS = -std=c++17 -nostdinc++ -nostdlib -nostdinc
KERNEL_FLAGS =
Expand All @@ -87,30 +84,25 @@ CXXFLAGS = -MMD -MP $(WARNING_FLAGS) $(OPTIMIZATION_FLAGS) $(KERNEL_FLAGS) $(FLA
#CXX = clang $(CLANG_FLAGS)
CXX = i686-elf-g++
LD = i686-elf-ld
AS = i686-elf-as
LDFLAGS = -T linker.ld

all: $(KERNEL) $(IMAGE) kernel.map
all: $(KERNEL) kernel.map

kernel.map: kernel
@echo "MKMAP $@"; sh mkmap.sh

$(KERNEL): $(OBJS)
@echo "LD $@"; $(LD) $(LDFLAGS) -o $@ -Ttext 0x10000 $(OBJS)

$(IMAGE): $(KERNEL) $(BOOTLOADER)
@echo "CREATE $@"; cat $(BOOTLOADER) $(KERNEL) > .tmp-floppy-image
@dd if=/dev/zero bs=2M count=1 >> .tmp-floppy-image 2> /dev/null
@dd if=.tmp-floppy-image of=.floppy-image bs=1440k count=1 2>/dev/null
@rm -f .tmp-floppy-image

$(BOOTLOADER): Boot/boot.asm
@echo "NASM $<"; $(NASM) -f bin -o $@ $<

.cpp.o:
@echo "CXX $<"; $(CXX) $(CXXFLAGS) -o $@ -c $<

-include $(OBJS:%.o=%.d)
%.ao: %.S
@echo "AS $@"; $(AS) -o $@ $<

-include $(CXX_OBJS:%.o=%.d)

clean:
@echo "CLEAN"; rm -f $(KERNEL) $(OBJS) $(BOOTLOADER) $(IMAGE) *.d
@echo "CLEAN"; rm -f $(KERNEL) $(OBJS) *.d

2 changes: 1 addition & 1 deletion Kernel/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ VFS* vfs;
ASSERT_NOT_REACHED();
}

[[noreturn]] void init()
extern "C" [[noreturn]] void init()
{
cli();

Expand Down
Loading

0 comments on commit ee4d7c1

Please sign in to comment.