Skip to content

Commit

Permalink
change bootloader
Browse files Browse the repository at this point in the history
Signed-off-by: aarch64 <gutierrezaverruz0@hotmail.com>
  • Loading branch information
agostino64 committed Dec 1, 2023
1 parent 59753cf commit d635b4c
Show file tree
Hide file tree
Showing 71 changed files with 2,475 additions and 181 deletions.
78 changes: 49 additions & 29 deletions Makefile
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,38 +1,58 @@
include toolchain.mk
include build_scripts/config.mk

SOURCES_ASM := $(wildcard $(addsuffix /*.asm, $(SRC_DIRS)))
OBJECTS_ASM := $(patsubst %.asm, %.asm.o, $(SOURCES_ASM))
.PHONY: all floppy_image kernel bootloader clean always

SOURCES_C := $(wildcard $(addsuffix /*.c, $(SRC_DIRS)))
OBJECTS_C := $(patsubst %.c, %.c.o, $(SOURCES_C))
all: floppy_image

all: $(OBJECTS_ASM) $(OBJECTS_C) link
include build_scripts/toolchain.mk

%.asm.o: %.asm
@mkdir -p $(BUILD_DIR)
@$(ASM) $(ASMFLAGS) -o $@ $<
@mv $@ $(notdir $@/build)
@echo -e "${MGTA}--> Compiled:${NC} ${CSV}" $< "${NC}"
#
# Floppy image
#
floppy_image: $(BUILD_DIR)/xnix_floppy.img

%.c.o: %.c
@mkdir -p $(BUILD_DIR)
@$(CC) $(CFLAGS) -I./include -c $< -o $@
@mv $@ $(notdir $@/build)
@echo -e "${MGTA}--> Compiled:${NC} ${CSV}" $< "${NC}"
$(BUILD_DIR)/xnix_floppy.img: bootloader kernel
@dd if=/dev/zero of=$@ bs=512 count=2880 >/dev/null
@mkfs.fat -F 12 -n "xnix" $@ >/dev/null
@dd if=$(BUILD_DIR)/stage1.bin of=$@ conv=notrunc >/dev/null
@mcopy -i $@ $(BUILD_DIR)/stage2.bin "::stage2.bin"
@mcopy -i $@ $(BUILD_DIR)/kernel.bin "::kernel.bin"
@echo "--> Created: " $@

link:
@mkdir -p $(BUILD_DIR)
@$(LD) $(LDFLAGS) -o $(BUILD_DIR)/$(OUTELF) $(addprefix build/, *.o)
@echo -e "\n${MGTA}--> Created:${NC} ${BOLD}XnixOS" "${NC}"
#
# Bootloader
#
bootloader: stage1 stage2

run:
@qemu-system-i386 -kernel $(BUILD_DIR)/$(OUTELF)
stage1: $(BUILD_DIR)/stage1.bin

clean:
@rm -rf build
$(BUILD_DIR)/stage1.bin: always
@$(MAKE) -C boot/stage1 BUILD_DIR=$(abspath $(BUILD_DIR))

stage2: $(BUILD_DIR)/stage2.bin

$(BUILD_DIR)/stage2.bin: always
@$(MAKE) -C boot/stage2 BUILD_DIR=$(abspath $(BUILD_DIR))

mkiso:
@mkdir -p $(ISO_DIR)/boot/grub
@cp -r boot/grub.cfg $(ISO_DIR)/boot/grub/
@cp -r $(BUILD_DIR)/$(OUTELF) $(ISO_DIR)/boot/
@grub2-mkrescue -o $(OUTELF).iso $(ISO_DIR)
#
# Kernel
#
kernel: $(BUILD_DIR)/kernel.bin

$(BUILD_DIR)/kernel.bin: always
@$(MAKE) -C kernel BUILD_DIR=$(abspath $(BUILD_DIR))

#
# Always
#
always:
@mkdir -p $(BUILD_DIR)

#
# Clean
#
clean:
@rm -rf $(BUILD_DIR)

run:
qemu-system-i386 -fda $(BUILD_DIR)/xnix_floppy.img
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,29 @@ Resources:
[Low Level](http://www.lowlevel.eu/)
|
[OS Dev](https://wiki.osdev.org/)

Inspired:
[CakeOS](https://github.com/lukem512/cakeos/)
|
[NanobyteOS](https://github.com/nanobyte-dev/nanobyte_os)

## Building
First, install the following dependencies:

```
# Ubuntu, Debian:
$ sudo apt install build-essential bison flex libgmp3-dev libmpc-dev libmpfr-dev texinfo \
nasm mtools qemu-system-x86
# Fedora:
$ sudo dnf install gcc gcc-c++ make bison flex gmp-devel libmpc-devel mpfr-devel texinfo \
nasm mtools qemu-system-x86
# Arch & Arch-based:
$ paru -S gcc make bison flex libgmp-static libmpc mpfr texinfo nasm mtools qemu-system-x86
```
NOTE: to install all the required packages on Arch, you need an [AUR helper](https://wiki.archlinux.org/title/AUR_helpers).

After that, run `make toolchain`, this should download and build the required tools (binutils and GCC). If you encounter errors during this step, you might have to modify `build_scripts/config.mk` and try a different version of **binutils** and **gcc**. Using the same version as the one bundled with your distribution is your best bet.

Finally, you should be able to run `make`. Use `make run` to test your OS using qemu.
45 changes: 0 additions & 45 deletions boot/boot.asm

This file was deleted.

7 changes: 0 additions & 7 deletions boot/grub.cfg

This file was deleted.

15 changes: 15 additions & 0 deletions boot/stage1/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
BUILD_DIR?=build/
ASM?=nasm

.PHONY: all clean

all: stage1

stage1: $(BUILD_DIR)/stage1.bin

$(BUILD_DIR)/stage1.bin: boot.asm
@$(ASM) $< -f bin -o $@
@echo "--> Created stage1.bin"

clean:
@rm -f $(BUILD_DIR)/stage1.bin
Loading

0 comments on commit d635b4c

Please sign in to comment.