Skip to content

Commit 12aba04

Browse files
committed
Stage 1 BT is able to load and execute Stage 2 BT
1 parent ff2b965 commit 12aba04

File tree

7 files changed

+147
-44
lines changed

7 files changed

+147
-44
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
*.vmdk
2+
*.o
23
build/

Makefile

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,35 @@ BUILD_DIR = build
44
.PHONY: all clean
55

66
bt_stage1 = $(BUILD_DIR)/bt_stage1
7+
bt_stage2_o = $(BUILD_DIR)/bt_stage2.o
8+
bt_stage2_asm_o = $(BUILD_DIR)/bt_stage2_asm.o
9+
bt_stage2 = $(BUILD_DIR)/bt_stage2
710
image_vmdk = $(BUILD_DIR)/image.vmdk
811

912
all: images binaries
1013

1114
images: $(image_vmdk)
1215

13-
binaries: $(bt_stage1)
16+
binaries: $(bt_stage1) $(bt_stage2)
1417

15-
$(image_vmdk): $(bt_stage1)
16-
dd bs=512 if=$< of=$@
18+
$(image_vmdk): $(bt_stage1) $(bt_stage2)
19+
dd bs=512 count=2 if=$(bt_stage1) of=$@
1720
/bin/echo -ne "\x55\xaa" | dd seek=510 bs=1 of=$@
21+
dd seek=1 bs=512 if=$(bt_stage2) of=$@
22+
dd seek=2 bs=512 count=1 if=/dev/zero of=$@
1823

19-
$(bt_stage1): $(BL_SRC_DIR)/stage1.asm $(BL_SRC_DIR)/constants.asm $(BL_SRC_DIR)/io.asm
24+
$(bt_stage1): $(BL_SRC_DIR)/stage1.asm $(BL_SRC_DIR)/constants.asm $(BL_SRC_DIR)/io.asm $(BL_SRC_DIR)/disk.asm
25+
nasm -o $@ -f bin -i $(BL_SRC_DIR)/ $<
26+
27+
# Compile in 32-bit mode
28+
$(bt_stage2_o): $(BL_SRC_DIR)/stage2.c
29+
gcc -c -o $@ $<
30+
31+
# $(bt_stage2_asm_o): $(BL_SRC_DIR)/stage2.asm
32+
# gcc -c -o $@ $<
33+
34+
$(bt_stage2): $(BL_SRC_DIR)/stage2.asm $(BL_SRC_DIR)/constants.asm $(BL_SRC_DIR)/io.asm $(bt_stage2_o)
2035
nasm -o $@ -f bin -i $(BL_SRC_DIR)/ $<
2136

2237
clean:
23-
rm -f $(image_vmdk) $(bt_stage1)
38+
rm -f $(image_vmdk) $(bt_stage1) $(bt_stage2) $(bt_stage2_o) $(bt_stage2_asm_o)

src/bootloader/disk.asm

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
%macro disk_read 6
2+
; Args: (sector_count, drive 8bit, cylinder 10bit, head 8bit, sector 6bit, write_add)
3+
; check es
4+
mov ax, 0x02%1 ; (read sectors, sector count)
5+
mov cx, 0x%3%5 ; (cylinder 10bit, sector 6bit)
6+
mov dx, 0x%4%2 ; (head, drive index)
7+
mov bx, 0x%6 ; (es:bx as write address)
8+
int 0x13
9+
%endmacro
10+
11+
%macro disk_write 6
12+
; Args: (sector_count, drive 8bit, cylinder 10bit, head 8bit, sector 6bit, read_add)
13+
; check es
14+
mov ax, 0x03%1 ; (write sectors, sector count)
15+
mov cx, 0x%3%5 ; (cylinder 10bit, sector 6bit)
16+
mov dx, 0x%4%2 ; (head, drive index)
17+
mov bx, 0x%6 ; (es:bx as write address)
18+
int 0x13
19+
%endmacro
20+
21+
%macro disk_success 1
22+
; Args: (drive index 8 bit)
23+
; Result: (AH: status code, set CF on error)
24+
mov ah, 0x01 ; (get status of last drive operation)
25+
mov dl, 0x%1 ; (drive index)
26+
int 0x13
27+
%endmacro

src/bootloader/io.asm

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,44 @@
3535
int 0x16
3636
%endmacro
3737

38+
%macro set_cursor_xy 3
39+
; Args: (x, y, page)
40+
mov ah, 0x02 ; (set custor position)
41+
mov bh, 0x%3 ; (page number)
42+
mov dx, 0x%2%1 ; (pos_y, pos_x)
43+
int 0x10
44+
%endmacro
45+
46+
%macro print_hex_string_ext 4
47+
; Args: (str, len, fg_color, page)
48+
mov si, %1 ; (str)
49+
mov dx, %2 ; (len)
50+
mov bx, 0x%4%3 ; (page, text color)
51+
mov ah, 0x0e ; (write tty)
52+
mov cx, 1 ; (count)
53+
label_marker:
54+
push bx
55+
mov bl, [si]
56+
and bx, 0x00F0
57+
shr bx, 4
58+
add bx, digit_to_hex
59+
mov al, [bx]
60+
pop bx
61+
int 0x10
62+
63+
push bx
64+
mov bl, [si]
65+
and bx, 0x000F
66+
add bx, digit_to_hex
67+
mov al, [bx]
68+
pop bx
69+
int 0x10
70+
71+
inc si
72+
sub dx, 1
73+
jnc label_marker
74+
%endmacro
75+
3876
%macro set_blinking 1
3977
; Args: (should_blink)
4078
; check es

src/bootloader/stage1.asm

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,48 @@
1-
; Fuzzy Bootloader
1+
; Fuzzy Bootloader Stage 1
2+
3+
; Assumptions:
4+
; - Stage 2 starts from Sector 1 to Sector 1
5+
; - Stage 2 has ORG 0x8000
6+
27
%include "constants.asm"
38
%include "io.asm"
9+
%include "disk.asm"
410

511
[ORG 0x7C00]
612
[BITS 16]
713

814

915
[SECTION .text]
1016

11-
cli
12-
mov ax, 0x0000
13-
mov es, ax ; es := 0
17+
CLI
18+
MOV ax, 0x0000
19+
MOV es, ax ; es := 0
1420

1521
set_blinking 0
16-
print_string_ext bl_welcome, bl_welcome_len, 02, 02, C_BLACK, C_WHITE, 0
17-
print_string_ext bl_stage_2, bl_stage_2_len, 05, 04, C_WHITE, C_BLACK, 0
18-
hlt
22+
print_string_ext bl_welcome, bl_welcome_len, 02, 02, C_GREEN, C_WHITE, 0
23+
print_string_ext bls1, bls1_len, 05, 04, C_WHITE, C_BLACK, 0
24+
25+
; Attempt to load Bootloader Stage 2 in Memory
26+
disk_read 01, 80, 00, 00, 02, 8000
27+
disk_success 80
28+
JNC label_bts2_loaded
29+
print_string_ext bls2_load_fail, bls2_load_fail_len, 05, 06, C_RED, C_BLACK, 0
30+
JMP label_exit
31+
32+
label_bts2_loaded:
33+
print_string_ext bls2_loaded, bls2_loaded_len, 05, 06, C_WHITE, C_BLACK, 0
34+
JMP 0x8000
35+
JMP label_exit
36+
37+
label_exit:
38+
HLT
1939

2040
[SECTION .data]
21-
bl_welcome db "Fuzzy OS... (^_^)"
41+
bl_welcome db " Fuzzy OS... (^_^) "
2242
bl_welcome_len equ ($-bl_welcome)
23-
bl_stage_2 db "Loading Bootloader: Stage 2"
24-
bl_stage_2_len equ ($-bl_stage_2)
43+
bls1 db "Bootloader: Stage 1"
44+
bls1_len equ ($-bls1)
45+
bls2_loaded db "Stage 2 Loaded."
46+
bls2_loaded_len equ ($-bls2_loaded)
47+
bls2_load_fail db "Stage 2 Load Failed!"
48+
bls2_load_fail_len equ ($-bls2_load_fail)

src/bootloader/stage2.asm

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,32 @@
1+
; Fuzzy Bootloader Stage 2
12
%include "constants.asm"
3+
%include "io.asm"
4+
5+
[ORG 0x8000]
6+
[BITS 16]
7+
28

3-
%macro print_string 4
4-
; Args: (str, len, x, y)
5-
; check es, bx
6-
mov ax, 0x1301 ; (print string, update cursor)
7-
mov bp, %1 ; (es:bp string pointer)
8-
mov cx, %2 ; (string length)
9-
mov dx, 0x%4%3 ; (pos_y, pos_x)
10-
int 0x10
11-
%endmacro
12-
13-
%macro print_string_ext 7
14-
; Args: (str, len, x, y, fg_color, bg_color, page)
15-
; check es
16-
mov bx, 0x%7%6%5 ; (pagenumber, attribute)
17-
print_string %1, %2, %3, %4
18-
%endmacro
19-
20-
global _low_print
219
[SECTION .text]
2210

11+
MOV ax, 0x0000
12+
MOV es, ax ; es := 0
13+
14+
set_blinking 0
15+
print_string_ext bl_stage_2, bl_stage_2_len, 05, 04, C_WHITE, C_BLACK, 0
16+
17+
JMP label_exit
18+
2319
_low_print:
24-
push rbp
25-
mov rbp, rsp
26-
print_string_ext bl_welcome, bl_welcome_len, 02, 02, C_BLACK, C_WHITE, 0
27-
mov rax, 15
20+
push bp
21+
mov bp, sp
22+
print_string_ext bl_stage_2, bl_stage_2_len, 02, 02, C_BLACK, C_WHITE, 0
23+
mov ax, 0
2824
leave
2925
ret
3026

27+
label_exit:
28+
HLT
29+
3130
[SECTION .data]
32-
bl_welcome db "Bootloader Stage 2"
33-
bl_welcome_len equ ($-bl_welcome)
31+
bl_stage_2 db "We are at Bootloader: Stage 2"
32+
bl_stage_2_len equ ($-bl_stage_2)

src/bootloader/stage2.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
int test () {
2-
int z=1;
3-
return z *2;
1+
//extern void _low_print();
2+
void test () {
3+
44
}
5-
extern int _low_print();
65
void entry_stage2() {
7-
int status = _low_print();
6+
test();
87
}

0 commit comments

Comments
 (0)