From d0eda16941157da169549ff54f621b04f0b2cd9f Mon Sep 17 00:00:00 2001 From: John Wang Date: Mon, 8 Jan 2024 17:15:45 +0800 Subject: [PATCH] grub bootloader --- .gitignore | 2 +- Makefile | 8 ++++---- arch/Makefile | 14 +++++++++----- arch/x86/Makefile | 28 +++++++++++++++++++++++----- arch/x86/boot/Makefile | 6 ------ arch/x86/boot/header.S | 5 +++++ arch/x86/kernel/Makefile | 4 ---- arch/x86/kernel/head_64.S | 2 +- init/Makefile | 4 +++- kernel/Makefile | 2 ++ mm/Makefile | 5 ++++- 11 files changed, 52 insertions(+), 28 deletions(-) delete mode 100644 arch/x86/boot/Makefile delete mode 100644 arch/x86/kernel/Makefile diff --git a/.gitignore b/.gitignore index ce621b1..ae52b71 100644 --- a/.gitignore +++ b/.gitignore @@ -9,7 +9,7 @@ bochsout.txt bimg !bimg/ *.dSYM -iso/ +build_dir/ HelloOS *.s~ *.i diff --git a/Makefile b/Makefile index 8d8721e..f4fc75f 100644 --- a/Makefile +++ b/Makefile @@ -1,12 +1,11 @@ +MAKEFLAGS = --no-print-directory export BASE_DIR=$(shell pwd) export TARGET_ISO=HelloOS.iso export CONFIG_x86_64=y export CC=gcc export AS=as export HIDE=@ -ifneq ($(HIDE),) - MAKEFLAGS += --no-print-dirctory -endif +export DEBUG= HelloOS: @@ -19,4 +18,5 @@ cleanall: $(HIDE)make -C arch cleanall run: - qemu-system-x86_64 -m 1G -cdrom arch/$(TARGET_ISO) -nographic \ No newline at end of file + qemu-system-x86_64 -m 1G -cdrom arch/$(TARGET_ISO) -nographic + diff --git a/arch/Makefile b/arch/Makefile index d0570c1..04fcc5b 100644 --- a/arch/Makefile +++ b/arch/Makefile @@ -7,12 +7,12 @@ else $(HIDE)echo "unkown arch..." endif -$(TARGET_ISO): $(kernel_target) +$(TARGET_ISO): clean $(kernel_target) $(HIDE)rm -rf build_dir $(HIDE)mkdir -p build_dir/boot/grub $(HIDE)cp $(kernel_target) build_dir/boot - $(HIDE)echo 'set timeout=0' > build_dir/boot/grub/grub.cfg + $(HIDE)echo 'set timeout=1' > build_dir/boot/grub/grub.cfg $(HIDE)echo 'set default=0' >> build_dir/boot/grub/grub.cfg $(HIDE)echo 'menuentry "HelloOS" {' >> build_dir/boot/grub/grub.cfg $(HIDE)echo ' multiboot2 /boot/kernel.elf "HelloOS"' >> build_dir/boot/grub/grub.cfg @@ -21,14 +21,18 @@ $(TARGET_ISO): $(kernel_target) $(HIDE)grub-mkrescue -o $(TARGET_ISO) build_dir $(HIDE)rm -rf build_dir -$(kernel_target): +$(kernel_target): compile + $(HIDE)ld -b elf64-x86-64 -z muldefs -o $(kernel_target) -Map=./kernel.map -T $(lds) $(shell find $(SRCS) -name "*.o") + +compile: $(HIDE)for dir in $(SRCS); do \ make -C $$dir || exit 1; \ done - $(HIDE)ld -b elf64-x86-64 -z muldefs -o $(kernel_target) -Map=./kernel.map -T $(lds) $(shell find $(SRCS) -name "*.o") clean: $(HIDE)rm -rf $(kernel_target) $(TARGET_ISO) kernel.map cleanall: clean - $(HIDE)rm -rf $(kernel_target) $(TARGET_ISO) \ No newline at end of file + $(HIDE)for dir in $(SRCS); do \ + make -C $$dir clean || exit 1; \ + done \ No newline at end of file diff --git a/arch/x86/Makefile b/arch/x86/Makefile index 4044ecd..3517e4f 100644 --- a/arch/x86/Makefile +++ b/arch/x86/Makefile @@ -1,9 +1,27 @@ export ASFLAGS=--64 export CFLAGS = -I$(BASE_DIR)/include --static -fno-pie -fno-builtin -fno-stack-protector -m64 -x86_srcs = boot kernel +x86_srcs = $(wildcard ./boot/*.c) $(wildcard ./kernel/*.c) $(wildcard ./*.c) +x86_objs = $(patsubst %.c, %.o, $(x86_srcs)) + +x86_asm_srcs = $(wildcard ./boot/*.S) $(wildcard ./kernel/*.S) +x86_asm_tmp_srcs = $(patsubst %.S, %.i, $(x86_asm_srcs)) +x86_asm_objs = $(patsubst %.S, %.o, $(x86_asm_srcs)) + +build: $(x86_asm_objs) $(x86_objs) + +$(x86_objs): $(x86_srcs) +$(x86_asm_objs): $(x86_asm_srcs) + +%.o: %.c + @echo "compile $<" + $(CC) $(CFLAGS) $(DEBUG) $< -o $@ + +%.o: %.S + @echo "compile $<" + $(HIDE)$(CC) -E $(CFLAGS) $< > $<.i + $(HIDE)$(AS) $(ASFLAGS) -o $@ $<.i + +clean: + $(HIDE)rm -rf $(x86_objs) $(x86_asm_objs) $(x86_asm_tmp_srcs) -build: - $(HIDE)for dir in $(x86_srcs); do \ - make -C $$dir || exit 1; \ - done \ No newline at end of file diff --git a/arch/x86/boot/Makefile b/arch/x86/boot/Makefile deleted file mode 100644 index 8015748..0000000 --- a/arch/x86/boot/Makefile +++ /dev/null @@ -1,6 +0,0 @@ - -build: header.o - -header.o: header.S - $(HIDE)$(CC) -E $(CFLAGS) $< > header.i - $(HIDE)$(AS) $(ASFLAGS) -o $@ header.i diff --git a/arch/x86/boot/header.S b/arch/x86/boot/header.S index fc13a1a..07853ad 100644 --- a/arch/x86/boot/header.S +++ b/arch/x86/boot/header.S @@ -16,4 +16,9 @@ multiboot_header: .long multiboot_header_end - multiboot_header /* header length */ .long -(MULTIBOOT2_HEADER_MAGIC + MULTIBOOT_ARCHITECTURE_I386 + (multiboot_header_end - multiboot_header)) + /* required end tag */ + .align 8 + .short MULTIBOOT_HEADER_TAG_END # type + .short 0 # flags + .long 8 # size multiboot_header_end: diff --git a/arch/x86/kernel/Makefile b/arch/x86/kernel/Makefile deleted file mode 100644 index f527665..0000000 --- a/arch/x86/kernel/Makefile +++ /dev/null @@ -1,4 +0,0 @@ - -head_64.o: head_64.S - $(HIDE)$(CC) -E $(CFLAGS) $< > head_64.i - $(HIDE)$(AS) $(ASFLAGS) -o $@ head_64.i \ No newline at end of file diff --git a/arch/x86/kernel/head_64.S b/arch/x86/kernel/head_64.S index e7f7769..9fa4714 100644 --- a/arch/x86/kernel/head_64.S +++ b/arch/x86/kernel/head_64.S @@ -1,4 +1,4 @@ .code32 .global startup_64 startup_64: - + hlt diff --git a/init/Makefile b/init/Makefile index f04bf7b..46a759b 100644 --- a/init/Makefile +++ b/init/Makefile @@ -1,3 +1,5 @@ build: - echo "init" \ No newline at end of file + echo "init" + +clean: diff --git a/kernel/Makefile b/kernel/Makefile index 6c490cd..7ac964a 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -2,3 +2,5 @@ build: echo $(baseDir) +clean: + \ No newline at end of file diff --git a/mm/Makefile b/mm/Makefile index 4ce1b39..b3fcf73 100644 --- a/mm/Makefile +++ b/mm/Makefile @@ -1,3 +1,6 @@ build: - echo "mm" \ No newline at end of file + echo "mm" + +clean: + \ No newline at end of file