Compare commits

...

2 Commits

Author SHA1 Message Date
075f7ab4ca 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.
2024-10-03 21:35:03 +02:00
7a1c92972f 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)
2024-10-03 21:34:56 +02:00
16 changed files with 218 additions and 36 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
build/ build/
.mailmap

View File

@ -1,9 +1,12 @@
ASM=nasm ASM=nasm
CC=gcc
CC16=/usr/bin/watcom/binl/wcc
LD16=/usr/bin/watcom/binl/wlink
SRC_DIR=src SRC_DIR=src
BUILD_DIR=build BUILD_DIR=build
.PHONY: all floppy_image kernel bootloader clean always .PHONY: all immagine_floppy kernel bootloader clean always
#Immagine floppy #Immagine floppy
@ -12,8 +15,9 @@ immagine_floppy: $(BUILD_DIR)/main_floppy.img
$(BUILD_DIR)/main_floppy.img: bootloader kernel $(BUILD_DIR)/main_floppy.img: bootloader kernel
dd if=/dev/zero of=$(BUILD_DIR)/main_floppy.img bs=512 count=2880 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)/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 $(BUILD_DIR)/kernel.bin "::kernel.bin"
@ -22,15 +26,23 @@ $(BUILD_DIR)/main_floppy.img: bootloader kernel
kernel: $(BUILD_DIR)/kernel.bin kernel: $(BUILD_DIR)/kernel.bin
$(BUILD_DIR)/kernel.bin: always $(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
bootloader: $(BUILD_DIR)/bootloader.bin bootloader: stage1 stage2
$(BUILD_DIR)/bootloader.bin: always stage1: $(BUILD_DIR)/stage1.bin
$(ASM) $(SRC_DIR)/bootloader/boot.asm -f bin -o $(BUILD_DIR)/bootloader.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 #Always

View File

@ -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

View File

@ -101,28 +101,28 @@ start:
xor bx, bx xor bx, bx
mov di, buffer mov di, buffer
.cerca_kernel: .cerca_stage2:
mov si, file_kernel_bin mov si, file_stage2_bin
mov cx, 11 mov cx, 11
push di push di
repe cmpsb repe cmpsb
pop di pop di
je .kernel_trovato je .stage2_trovato
add di, 32 add di, 32
inc bx inc bx
cmp bx, [dir_entries_count] cmp bx, [dir_entries_count]
jl .cerca_kernel jl .cerca_stage2
;Kernel non trovato ;Kernel non trovato
jmp errore_kernel_non_trovato jmp errore_stage2_non_trovato
.kernel_trovato: .stage2_trovato:
mov si, msg_kernel_trovato mov si, msg_stage2_trovato
call puts call puts
mov ax, [di + 26] mov ax, [di + 26]
mov [kernel_cluster], ax mov [stage2_cluster], ax
mov ax, [reserved_sectors] mov ax, [reserved_sectors]
mov bx, buffer mov bx, buffer
@ -130,12 +130,12 @@ start:
mov dl, [ebr_drive_number] mov dl, [ebr_drive_number]
call lettura_disco call lettura_disco
mov bx, KERNEL_LOAD_SEGMENT mov bx, STAGE2_LOAD_SEGMENT
mov es, bx mov es, bx
mov bx, KERNEL_LOAD_OFFSET mov bx, STAGE2_LOAD_OFFSET
.carica_kernel: .carica_stage2:
mov ax, [kernel_cluster] mov ax, [stage2_cluster]
add ax, 31 add ax, 31
mov cl, 1 mov cl, 1
@ -144,7 +144,7 @@ start:
add bx, [bytes_per_sector] add bx, [bytes_per_sector]
mov ax, [kernel_cluster] mov ax, [stage2_cluster]
mov cx, 3 mov cx, 3
mul cx mul cx
mov cx, 2 mov cx, 2
@ -168,18 +168,17 @@ start:
cmp ax, 0x0FF8 cmp ax, 0x0FF8
jae .fine_lettura jae .fine_lettura
mov [kernel_cluster], ax mov [stage2_cluster], ax
jmp .carica_kernel jmp .carica_stage2
.fine_lettura: .fine_lettura:
mov dl, [ebr_drive_number] mov dl, [ebr_drive_number]
mov ax, KERNEL_LOAD_SEGMENT mov ax, STAGE2_LOAD_SEGMENT
mov ds, ax mov ds, ax
mov es, ax mov es, ax
jmp KERNEL_LOAD_SEGMENT:KERNEL_LOAD_OFFSET jmp STAGE2_LOAD_SEGMENT:STAGE2_LOAD_OFFSET
jmp premi_per_riavviare
;Trasforma/Converte un indirizzo LBA in uno CHS ;Trasforma/Converte un indirizzo LBA in uno CHS
@ -280,11 +279,11 @@ msg_caricamento:
msg_errore_lettura: msg_errore_lettura:
db 'Errore del disco', ENDL, 0 db 'Errore del disco', ENDL, 0
msg_kernel_non_trovato: msg_stage2_non_trovato:
db 'Kernel non trovato', ENDL, 0 db 'Stage 2 non trovato', ENDL, 0
msg_kernel_trovato: msg_stage2_trovato:
db 'Kernel trovato', ENDL, 0 db 'Stage 2 trovato', ENDL, 0
;Errori ;Errori
@ -293,17 +292,17 @@ errore_floppy:
call puts call puts
jmp premi_per_riavviare jmp premi_per_riavviare
errore_kernel_non_trovato: errore_stage2_non_trovato:
mov si, msg_kernel_non_trovato mov si, msg_stage2_non_trovato
call puts call puts
jmp premi_per_riavviare jmp premi_per_riavviare
;Variabili ;Variabili
file_kernel_bin: db 'KERNEL BIN' file_stage2_bin: db 'STAGE2 BIN'
kernel_cluster: dw 0 stage2_cluster: dw 0
KERNEL_LOAD_SEGMENT equ 0x2000 STAGE2_LOAD_SEGMENT equ 0x2000
KERNEL_LOAD_OFFSET equ 0 STAGE2_LOAD_OFFSET equ 0
;Cose che non so dove mettere ;Cose che non so dove mettere

View File

@ -0,0 +1,34 @@
BUILD_DIR?=build/
ASM?=nasm
ASMFLAGS?=-f obj
CC?=gcc
CC16?=/usr/bin/watcom/binl/wcc
CFLAGS16?= -4 -d3 -s -wx -ms -zl -zq
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 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

View File

@ -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

View File

@ -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 (;;);
}

View File

@ -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

View File

@ -0,0 +1,21 @@
bits 16
section _ENTRY class=CODE
extern _cstart_
global entry
entry:
cli
mov ax, ds
mov ss, ax
mov sp, 0
mov bp, sp
sti
xor dh, dh
push dx
call _cstart_
cli
hlt

View File

@ -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;

View File

@ -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++;
}
}

View File

@ -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

View File

@ -0,0 +1,4 @@
#pragma once
void putc(char c);
void puts(const char *str);

View File

@ -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

View File

@ -0,0 +1,4 @@
#pragma once
#include "stdint.h"
void _cdecl x86_Video_WriteCharTeletype(char c, uint8_t page);

17
src/kernel/Makefile Normal file
View File

@ -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