From 71dc893426568f01d19e3412ab9a89e00b002587 Mon Sep 17 00:00:00 2001 From: Vincenzo Aleksey Brocato Date: Sun, 28 Jul 2024 00:08:48 +0200 Subject: [PATCH] Now the Stage 1 of the bootloader loads Stage 2 in memory. I already wrote this but my computer crashed, so I won't write as much as before. Now the bootloader is divided in stage1 and stage2, I added and edited some makefiles, stage 2 is written in Asm and C, the kernel isn't loaded into memory. PS: after this commit I'll add another branch called "coding", the code in that branch won't work 100% because it's missing something, but I don't want to do a "mega commit" every time (those commits will still be in the main branch). To everyone who wants to try XanvicOS or """using""" it, use the main branch, because the main branch's code is working. I'll write this in the README.md right after this commit. --- .gitignore | 3 +- Makefile | 3 +- src/bootloader/stage2/Makefile | 8 ++-- src/bootloader/stage2/linker.lnk | 12 ++++++ src/bootloader/stage2/main.c | 8 ++++ src/bootloader/stage2/main.err | 3 ++ src/bootloader/stage2/stage2.asm | 63 +++++++------------------------- src/bootloader/stage2/stdint.h | 12 ++++++ src/bootloader/stage2/stdio.c | 15 ++++++++ src/bootloader/stage2/stdio.err | 2 + src/bootloader/stage2/stdio.h | 4 ++ src/bootloader/stage2/x86.asm | 23 ++++++++++++ src/bootloader/stage2/x86.h | 4 ++ 13 files changed, 104 insertions(+), 56 deletions(-) create mode 100644 src/bootloader/stage2/linker.lnk create mode 100644 src/bootloader/stage2/main.c create mode 100644 src/bootloader/stage2/main.err create mode 100644 src/bootloader/stage2/stdint.h create mode 100644 src/bootloader/stage2/stdio.c create mode 100644 src/bootloader/stage2/stdio.err create mode 100644 src/bootloader/stage2/stdio.h create mode 100644 src/bootloader/stage2/x86.asm create mode 100644 src/bootloader/stage2/x86.h diff --git a/.gitignore b/.gitignore index d163863..07b498d 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -build/ \ No newline at end of file +build/ +.mailmap \ No newline at end of file diff --git a/Makefile b/Makefile index a090345..55e3575 100644 --- a/Makefile +++ b/Makefile @@ -15,11 +15,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 + mkfs.fat -F 12 -n "XANVOS" $(BUILD_DIR)/main_floppy.img 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 diff --git a/src/bootloader/stage2/Makefile b/src/bootloader/stage2/Makefile index 56f6818..da46177 100644 --- a/src/bootloader/stage2/Makefile +++ b/src/bootloader/stage2/Makefile @@ -3,7 +3,7 @@ ASM?=nasm ASMFLAGS?=-f obj CC?=gcc CC16?=/usr/bin/watcom/binl/wcc -CFLAGS16?= +CFLAGS16?= -4 -d3 -s -wx -ms -zl -zq LD16?=/usr/bin/watcom/binl/wlink SOURCES_C=$(wildcard *.c) @@ -11,14 +11,14 @@ 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 +.PHONY: all 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.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=$@ $< diff --git a/src/bootloader/stage2/linker.lnk b/src/bootloader/stage2/linker.lnk new file mode 100644 index 0000000..2942bbb --- /dev/null +++ b/src/bootloader/stage2/linker.lnk @@ -0,0 +1,12 @@ +FORMAT RAW BIN +OPTION QUIET, + NODEFAULTLIBS, + START=entry, + VERBOSE, + OFFSET=0, + STACK=0X200 +ORDER + CLNAME CODE + SEGMENT _ENTRY + SEGMENT _TEXT + CLNAME DATA diff --git a/src/bootloader/stage2/main.c b/src/bootloader/stage2/main.c new file mode 100644 index 0000000..7592ea9 --- /dev/null +++ b/src/bootloader/stage2/main.c @@ -0,0 +1,8 @@ +#include "stdint.h" +#include "stdio.h" + +void _cdecl cstart_(uint16_t bootDrive) +{ + puts("Hello World from the Stage 2 of the bootloader written in C!"); + for (;;); +} \ No newline at end of file diff --git a/src/bootloader/stage2/main.err b/src/bootloader/stage2/main.err new file mode 100644 index 0000000..f07be74 --- /dev/null +++ b/src/bootloader/stage2/main.err @@ -0,0 +1,3 @@ +stdio.h(4): Warning! W138: No newline at end of file +main.c(8): Warning! W138: No newline at end of file +main.c(4): Warning! W303: Parameter 'bootDrive' has been defined, but not referenced diff --git a/src/bootloader/stage2/stage2.asm b/src/bootloader/stage2/stage2.asm index bb692f3..f47ed63 100644 --- a/src/bootloader/stage2/stage2.asm +++ b/src/bootloader/stage2/stage2.asm @@ -1,56 +1,21 @@ -org 0x0 bits 16 +section _ENTRY class=CODE -%define ENDL 0x0D, 0x0A +extern _cstart_ +global entry -;Codice del kernel -start: - mov si, msg_helloworld - call puts - call premi_per_riavviare - -.halt: +entry: cli - hlt + mov ax, ds + mov ss, ax + mov sp, 0 + mov bp, sp + sti -puts: - ; save registers we will modify - push si - push ax - push bx + xor dh, dh + push dx + call _cstart_ -.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 + cli + hlt \ No newline at end of file diff --git a/src/bootloader/stage2/stdint.h b/src/bootloader/stage2/stdint.h new file mode 100644 index 0000000..a250c54 --- /dev/null +++ b/src/bootloader/stage2/stdint.h @@ -0,0 +1,12 @@ +#pragma once + +typedef signed char int8_t; +typedef unsigned char uint8_t; +typedef signed short int16_t; +typedef unsigned short uint16_t; +typedef signed long int int32_t; +typedef unsigned long int uint32_t; +typedef signed long int int64_t; +typedef unsigned long int uint64_t; + + diff --git a/src/bootloader/stage2/stdio.c b/src/bootloader/stage2/stdio.c new file mode 100644 index 0000000..327355c --- /dev/null +++ b/src/bootloader/stage2/stdio.c @@ -0,0 +1,15 @@ +#include "stdio.h" +#include "x86.h" + +void putc(char c) +{ + x86_Video_WriteCharTeletype(c, 0); +} +void puts(const char *str) +{ + while(*str) + { + putc(*str); + str++; + } +} diff --git a/src/bootloader/stage2/stdio.err b/src/bootloader/stage2/stdio.err new file mode 100644 index 0000000..f5ae572 --- /dev/null +++ b/src/bootloader/stage2/stdio.err @@ -0,0 +1,2 @@ +stdio.h(4): Warning! W138: No newline at end of file +x86.h(4): Warning! W138: No newline at end of file diff --git a/src/bootloader/stage2/stdio.h b/src/bootloader/stage2/stdio.h new file mode 100644 index 0000000..f25dc1c --- /dev/null +++ b/src/bootloader/stage2/stdio.h @@ -0,0 +1,4 @@ +#pragma once + +void putc(char c); +void puts(const char *str); \ No newline at end of file diff --git a/src/bootloader/stage2/x86.asm b/src/bootloader/stage2/x86.asm new file mode 100644 index 0000000..14cdc90 --- /dev/null +++ b/src/bootloader/stage2/x86.asm @@ -0,0 +1,23 @@ +bits 16 + +section _TEXT class=CODE + +global _x86_Video_WriteCharTeletype + +_x86_Video_WriteCharTeletype: + push bp + mov bp, sp + + push bx + mov ah, 0Eh + mov al, [bp + 4] + mov bh, [bp + 6] + + int 10h + + pop bx + + mov sp, bp + pop bp + + ret diff --git a/src/bootloader/stage2/x86.h b/src/bootloader/stage2/x86.h new file mode 100644 index 0000000..21cf724 --- /dev/null +++ b/src/bootloader/stage2/x86.h @@ -0,0 +1,4 @@ +#pragma once +#include "stdint.h" + +void _cdecl x86_Video_WriteCharTeletype(char c, uint8_t page); \ No newline at end of file