Skip to content

Commit

Permalink
grub bootloader
Browse files Browse the repository at this point in the history
  • Loading branch information
BitInit committed Jan 8, 2024
1 parent 0315f03 commit d0eda16
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ bochsout.txt
bimg
!bimg/
*.dSYM
iso/
build_dir/
HelloOS
*.s~
*.i
Expand Down
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -19,4 +18,5 @@ cleanall:
$(HIDE)make -C arch cleanall

run:
qemu-system-x86_64 -m 1G -cdrom arch/$(TARGET_ISO) -nographic
qemu-system-x86_64 -m 1G -cdrom arch/$(TARGET_ISO) -nographic

14 changes: 9 additions & 5 deletions arch/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
$(HIDE)for dir in $(SRCS); do \
make -C $$dir clean || exit 1; \
done
28 changes: 23 additions & 5 deletions arch/x86/Makefile
Original file line number Diff line number Diff line change
@@ -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
6 changes: 0 additions & 6 deletions arch/x86/boot/Makefile

This file was deleted.

5 changes: 5 additions & 0 deletions arch/x86/boot/header.S
Original file line number Diff line number Diff line change
Expand Up @@ -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:
4 changes: 0 additions & 4 deletions arch/x86/kernel/Makefile

This file was deleted.

2 changes: 1 addition & 1 deletion arch/x86/kernel/head_64.S
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.code32
.global startup_64
startup_64:

hlt
4 changes: 3 additions & 1 deletion init/Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@

build:
echo "init"
echo "init"

clean:
2 changes: 2 additions & 0 deletions kernel/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
build:
echo $(baseDir)

clean:

5 changes: 4 additions & 1 deletion mm/Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@

build:
echo "mm"
echo "mm"

clean:

0 comments on commit d0eda16

Please sign in to comment.