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)
This commit is contained in:
Vincenzo Aleksey Brocato 2024-07-28 00:00:11 +02:00 committed by VinceAle7082
parent 9476a54f13
commit 7a1c92972f
6 changed files with 168 additions and 34 deletions

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
@ -13,8 +16,10 @@ 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"
mcopy -i $(BUILD_DIR)/main_floppy.img test.txt "::test.txt"
#Kernel #Kernel
@ -22,15 +27,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?=
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

View File

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

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