From 7a1c92972f68dfa6ab423461bfc4446515d8de93 Mon Sep 17 00:00:00 2001 From: Vincenzo Aleksey Brocato Date: Sun, 28 Jul 2024 00:00:11 +0200 Subject: [PATCH] Add bootloader's 2nd stage written in C and asm Now the bootloader is divided in stage1 and stage 2, the kernel isn't loaded so it's useless (for now). The Stage 1 of the bootloader prints some strings and loads the second stage into memory. The second stage prints a hello world message. After this commit I'll divide the main branch into stable and coding (stable is working code, like all the commits I did until this day, coding will be the branch where I push all the code I write, so there may be a lot of errors or the code doesn't compile. I you want to use or just try XanvicOS use the stable branch) --- Makefile | 25 +++++++-- src/bootloader/stage1/Makefile | 15 +++++ .../{boot.asm => stage1/stage1.asm} | 55 +++++++++--------- src/bootloader/stage2/Makefile | 34 +++++++++++ src/bootloader/stage2/stage2.asm | 56 +++++++++++++++++++ src/kernel/Makefile | 17 ++++++ 6 files changed, 168 insertions(+), 34 deletions(-) create mode 100644 src/bootloader/stage1/Makefile rename src/bootloader/{boot.asm => stage1/stage1.asm} (83%) create mode 100644 src/bootloader/stage2/Makefile create mode 100644 src/bootloader/stage2/stage2.asm create mode 100644 src/kernel/Makefile diff --git a/Makefile b/Makefile index b76f2f2..a090345 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,12 @@ ASM=nasm +CC=gcc +CC16=/usr/bin/watcom/binl/wcc +LD16=/usr/bin/watcom/binl/wlink SRC_DIR=src BUILD_DIR=build -.PHONY: all floppy_image kernel bootloader clean always +.PHONY: all immagine_floppy kernel bootloader clean always #Immagine floppy @@ -13,8 +16,10 @@ immagine_floppy: $(BUILD_DIR)/main_floppy.img $(BUILD_DIR)/main_floppy.img: bootloader kernel dd if=/dev/zero of=$(BUILD_DIR)/main_floppy.img bs=512 count=2880 mkfs.fat -F 12 -n "XanvOS" $(BUILD_DIR)/main_floppy.img - dd if=$(BUILD_DIR)/bootloader.bin of=$(BUILD_DIR)/main_floppy.img conv=notrunc + dd if=$(BUILD_DIR)/stage1.bin of=$(BUILD_DIR)/main_floppy.img conv=notrunc + mcopy -i $(BUILD_DIR)/main_floppy.img $(BUILD_DIR)/stage2.bin "::stage2.bin" mcopy -i $(BUILD_DIR)/main_floppy.img $(BUILD_DIR)/kernel.bin "::kernel.bin" + mcopy -i $(BUILD_DIR)/main_floppy.img test.txt "::test.txt" #Kernel @@ -22,15 +27,23 @@ $(BUILD_DIR)/main_floppy.img: bootloader kernel kernel: $(BUILD_DIR)/kernel.bin $(BUILD_DIR)/kernel.bin: always - $(ASM) $(SRC_DIR)/kernel/kernel.asm -f bin -o $(BUILD_DIR)/kernel.bin + $(MAKE) -C $(SRC_DIR)/kernel BUILD_DIR=$(abspath $(BUILD_DIR)) #Bootloader -bootloader: $(BUILD_DIR)/bootloader.bin +bootloader: stage1 stage2 -$(BUILD_DIR)/bootloader.bin: always - $(ASM) $(SRC_DIR)/bootloader/boot.asm -f bin -o $(BUILD_DIR)/bootloader.bin +stage1: $(BUILD_DIR)/stage1.bin + +$(BUILD_DIR)/stage1.bin: always + $(MAKE) -C $(SRC_DIR)/bootloader/stage1 BUILD_DIR=$(abspath $(BUILD_DIR)) + + +stage2: $(BUILD_DIR)/stage2.bin + +$(BUILD_DIR)/stage2.bin: always + $(MAKE) -C $(SRC_DIR)/bootloader/stage2 BUILD_DIR=$(abspath $(BUILD_DIR)) #Always diff --git a/src/bootloader/stage1/Makefile b/src/bootloader/stage1/Makefile new file mode 100644 index 0000000..4559923 --- /dev/null +++ b/src/bootloader/stage1/Makefile @@ -0,0 +1,15 @@ +BUILD_DIR?=build/ +ASM?=nasm + + +.PHONY: all clean + +all: stage1 + +stage1: $(BUILD_DIR)/stage1.bin + +$(BUILD_DIR)/stage1.bin: + $(ASM) stage1.asm -f bin -o $(BUILD_DIR)/stage1.bin + +clean: + rm -f $(BUILD_DIR)/stage1.bin \ No newline at end of file diff --git a/src/bootloader/boot.asm b/src/bootloader/stage1/stage1.asm similarity index 83% rename from src/bootloader/boot.asm rename to src/bootloader/stage1/stage1.asm index 1ad257c..2b930de 100644 --- a/src/bootloader/boot.asm +++ b/src/bootloader/stage1/stage1.asm @@ -101,28 +101,28 @@ start: xor bx, bx mov di, buffer -.cerca_kernel: - mov si, file_kernel_bin +.cerca_stage2: + mov si, file_stage2_bin mov cx, 11 push di repe cmpsb pop di - je .kernel_trovato + je .stage2_trovato add di, 32 inc bx cmp bx, [dir_entries_count] - jl .cerca_kernel + jl .cerca_stage2 ;Kernel non trovato - jmp errore_kernel_non_trovato + jmp errore_stage2_non_trovato -.kernel_trovato: - mov si, msg_kernel_trovato +.stage2_trovato: + mov si, msg_stage2_trovato call puts mov ax, [di + 26] - mov [kernel_cluster], ax + mov [stage2_cluster], ax mov ax, [reserved_sectors] mov bx, buffer @@ -130,12 +130,12 @@ start: mov dl, [ebr_drive_number] call lettura_disco - mov bx, KERNEL_LOAD_SEGMENT + mov bx, STAGE2_LOAD_SEGMENT mov es, bx - mov bx, KERNEL_LOAD_OFFSET + mov bx, STAGE2_LOAD_OFFSET -.carica_kernel: - mov ax, [kernel_cluster] +.carica_stage2: + mov ax, [stage2_cluster] add ax, 31 mov cl, 1 @@ -144,7 +144,7 @@ start: add bx, [bytes_per_sector] - mov ax, [kernel_cluster] + mov ax, [stage2_cluster] mov cx, 3 mul cx mov cx, 2 @@ -168,18 +168,17 @@ start: cmp ax, 0x0FF8 jae .fine_lettura - mov [kernel_cluster], ax - jmp .carica_kernel + mov [stage2_cluster], ax + jmp .carica_stage2 .fine_lettura: mov dl, [ebr_drive_number] - mov ax, KERNEL_LOAD_SEGMENT + mov ax, STAGE2_LOAD_SEGMENT mov ds, ax mov es, ax - jmp KERNEL_LOAD_SEGMENT:KERNEL_LOAD_OFFSET - jmp premi_per_riavviare + jmp STAGE2_LOAD_SEGMENT:STAGE2_LOAD_OFFSET ;Trasforma/Converte un indirizzo LBA in uno CHS @@ -280,11 +279,11 @@ msg_caricamento: msg_errore_lettura: db 'Errore del disco', ENDL, 0 -msg_kernel_non_trovato: - db 'Kernel non trovato', ENDL, 0 +msg_stage2_non_trovato: + db 'Stage 2 non trovato', ENDL, 0 -msg_kernel_trovato: - db 'Kernel trovato', ENDL, 0 +msg_stage2_trovato: + db 'Stage 2 trovato', ENDL, 0 ;Errori @@ -293,17 +292,17 @@ errore_floppy: call puts jmp premi_per_riavviare -errore_kernel_non_trovato: - mov si, msg_kernel_non_trovato +errore_stage2_non_trovato: + mov si, msg_stage2_non_trovato call puts jmp premi_per_riavviare ;Variabili -file_kernel_bin: db 'KERNEL BIN' -kernel_cluster: dw 0 -KERNEL_LOAD_SEGMENT equ 0x2000 -KERNEL_LOAD_OFFSET equ 0 +file_stage2_bin: db 'STAGE2 BIN' +stage2_cluster: dw 0 +STAGE2_LOAD_SEGMENT equ 0x2000 +STAGE2_LOAD_OFFSET equ 0 ;Cose che non so dove mettere diff --git a/src/bootloader/stage2/Makefile b/src/bootloader/stage2/Makefile new file mode 100644 index 0000000..56f6818 --- /dev/null +++ b/src/bootloader/stage2/Makefile @@ -0,0 +1,34 @@ +BUILD_DIR?=build/ +ASM?=nasm +ASMFLAGS?=-f obj +CC?=gcc +CC16?=/usr/bin/watcom/binl/wcc +CFLAGS16?= +LD16?=/usr/bin/watcom/binl/wlink + +SOURCES_C=$(wildcard *.c) +SOURCES_ASM=$(wildcard *.asm) +OBJECTS_C=$(patsubst %.c, $(BUILD_DIR)/stage2/c/%.obj, $(SOURCES_C)) +OBJECTS_ASM=$(patsubst %.asm, $(BUILD_DIR)/stage2/asm/%.obj, $(SOURCES_ASM)) + +.PHONY: all stage2 clean always + +all: stage2 + +stage2: $(BUILD_DIR)/stage2.bin + +$(BUILD_DIR)/stage2.bin: $OBJECTS_ASM $OBJECTS_C + $(LD16) NAME $(BUILD_DIR)/stage2.bin FILE \{ $(OBJECTS_ASM) $(OBJECTS_C) \} OPTION_MAP=$(BUILD_DIR)/stage2.map @linker.lnk + +$(BUILD_DIR)/stage2/c/%.obj: %.c always + $(CC16) $(CFLAGS16) -fo=$@ $< + +$(BUILD_DIR)/stage2/asm/%.obj: %.asm always + $(ASM) $(ASMFLAGS) -o $@ $< + +always: + mkdir -p $(BUILD_DIR)/stage2/c + mkdir -p $(BUILD_DIR)/stage2/asm + +clean: + rm -f $(BUILD_DIR)/stage2.bin \ No newline at end of file diff --git a/src/bootloader/stage2/stage2.asm b/src/bootloader/stage2/stage2.asm new file mode 100644 index 0000000..bb692f3 --- /dev/null +++ b/src/bootloader/stage2/stage2.asm @@ -0,0 +1,56 @@ +org 0x0 +bits 16 + + +%define ENDL 0x0D, 0x0A + +;Codice del kernel +start: + mov si, msg_helloworld + call puts + call premi_per_riavviare + +.halt: + cli + hlt + +puts: + ; save registers we will modify + push si + push ax + push bx + +.loop: + lodsb + or al, al + jz .fine + + mov ah, 0x0E + mov bh, 0 + int 0x10 + + jmp .loop + +.fine: + pop bx + pop ax + pop si + ret + +;Stringhe +msg_helloworld: + db 'Hello World dallo stage2 del Bootloader!', ENDL, 0 + +msg_premi_per_riavviare: + db 'Premi un tasto per riavviare...', ENDL, 0 +;Errori + +;Variabili + +;Cose che non so dove mettere +premi_per_riavviare: + mov si, msg_premi_per_riavviare + call puts + mov ah, 0 + int 16h + jmp 0FFFFh:0 \ No newline at end of file diff --git a/src/kernel/Makefile b/src/kernel/Makefile new file mode 100644 index 0000000..5b888b6 --- /dev/null +++ b/src/kernel/Makefile @@ -0,0 +1,17 @@ +BUILD_DIR?=build/ +ASM?=nasm +CC?=gcc +CC16?=/usr/bin/watcom/binl/wcc +LD16?=/usr/bin/watcom/binl/wlink + +.PHONY: all clean + +all: kernel + +kernel: $(BUILD_DIR)/kernel.bin + +$(BUILD_DIR)/kernel.bin: + $(ASM) kernel.asm -f bin -o $(BUILD_DIR)/kernel.bin + +clean: + rm -f $(BUILD_DIR)/kernel.bin \ No newline at end of file