Skip to content

Commit 36b38e7

Browse files
committed
Load and execute Stage 2 BT. In addition we can print using C
1 parent f4046bf commit 36b38e7

File tree

6 files changed

+97
-68
lines changed

6 files changed

+97
-68
lines changed

Makefile

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,18 @@ BUILD_DIR = build
33

44
.PHONY: all clean
55

6+
# Files
67
bt_stage1 = $(BUILD_DIR)/bt_stage1
7-
bt_stage2_o = $(BUILD_DIR)/bt_stage2.o
8+
bt_stage2_c_o = $(BUILD_DIR)/bt_stage2_c.o
89
bt_stage2_asm_o = $(BUILD_DIR)/bt_stage2_asm.o
910
bt_stage2 = $(BUILD_DIR)/bt_stage2
1011
image_vmdk = $(BUILD_DIR)/image.vmdk
1112

13+
# Parameters
14+
BT_STAGE2_SECTOR_COUNT = 11 # In Hex
15+
16+
rebuild: clean all
17+
1218
all: images binaries
1319

1420
images: $(image_vmdk)
@@ -19,20 +25,41 @@ $(image_vmdk): $(bt_stage1) $(bt_stage2)
1925
dd bs=512 count=2 if=$(bt_stage1) of=$@
2026
/bin/echo -ne "\x55\xaa" | dd seek=510 bs=1 of=$@
2127
dd seek=1 bs=512 if=$(bt_stage2) of=$@
22-
dd seek=2 bs=512 count=1 if=/dev/zero of=$@
28+
truncate --size=%512 $@
29+
@echo "Stage 1 Size : " $$(stat -c %s $(bt_stage1))
30+
@echo "Stage 2 Size : " $$(stat -c %s $(bt_stage2))
31+
@echo "Stage 2 LoadSize : " $$(( $$(printf "%d\n" "0x"$(BT_STAGE2_SECTOR_COUNT)) * 512 ))
32+
@echo "Image Size : " $$(stat -c %s $@)
33+
@echo "Want BT_STAGE2_SECTOR_COUNT : 0x"$$(printf "%x\n" $$(( $$(stat -c %s $@) / 512 - 1)) )
34+
@echo "Got BT_STAGE2_SECTOR_COUNT : 0x"$(BT_STAGE2_SECTOR_COUNT)
2335

2436
$(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)/ $<
37+
nasm -o $@ -f bin -i $(BL_SRC_DIR)/ -D BT_STAGE2_SECTOR_COUNT=$(BT_STAGE2_SECTOR_COUNT) $<
38+
39+
$(bt_stage2): $(bt_stage2_asm_o) $(bt_stage2_c_o)
40+
ld --oformat binary -m elf_i386 -Ttext 0x8000 --strip-all -o $@ $^
41+
42+
$(bt_stage2_c_o): $(BL_SRC_DIR)/stage2.c
43+
gcc -m16 -fno-pie -c -o $@ $<
44+
45+
$(bt_stage2_asm_o): $(BL_SRC_DIR)/stage2.asm $(BL_SRC_DIR)/constants.asm $(BL_SRC_DIR)/io.asm $(bt_stage2_o)
46+
nasm -o $@ -f elf32 -i $(BL_SRC_DIR)/ $<
47+
48+
debug_stage1: $(bt_stage1)
49+
objdump -b binary -mi386 -Maddr16,data16 -D $<
50+
xxd $<
2651

27-
# Compile in 32-bit mode
28-
$(bt_stage2_o): $(BL_SRC_DIR)/stage2.c
29-
gcc -c -o $@ $<
52+
debug_stage2: $(bt_stage2)
53+
objdump -b binary -mi386 -Maddr16,data16 -D $<
54+
xxd $<
3055

31-
# $(bt_stage2_asm_o): $(BL_SRC_DIR)/stage2.asm
32-
# gcc -c -o $@ $<
56+
debug_stage2_c: $(bt_stage2_c_o)
57+
objdump -d -Maddr16,data16 $<
58+
xxd $<
3359

34-
$(bt_stage2): $(BL_SRC_DIR)/stage2.asm $(BL_SRC_DIR)/constants.asm $(BL_SRC_DIR)/io.asm $(bt_stage2_o)
35-
nasm -o $@ -f bin -i $(BL_SRC_DIR)/ $<
60+
debug_stage2_asm: $(bt_stage2_asm_o)
61+
objdump -d -Maddr16,data16 $<
62+
xxd $<
3663

3764
clean:
38-
rm -f $(image_vmdk) $(bt_stage1) $(bt_stage2) $(bt_stage2_o) $(bt_stage2_asm_o)
65+
rm -f $(image_vmdk) $(bt_stage1) $(bt_stage2) $(bt_stage2_c_o) $(bt_stage2_asm_o)

src/bootloader/main.c

Lines changed: 0 additions & 4 deletions
This file was deleted.

src/bootloader/main_low.asm

Lines changed: 0 additions & 33 deletions
This file was deleted.

src/bootloader/stage1.asm

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,34 @@
1919
MOV es, ax ; es := 0
2020

2121
set_blinking 0
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
22+
print_string_ext bl_welcome, bl_welcome_len, 02, 02, C_MAGENTA, C_WHITE, 0
23+
print_string_ext bls1, bls1_len, 04, 04, C_WHITE, C_BLACK, 0
2424

2525
; Attempt to load Bootloader Stage 2 in Memory
26-
disk_read 01, 80, 00, 00, 02, 8000
26+
disk_read BT_STAGE2_SECTOR_COUNT, 80, 00, 00, 02, 8000
2727
disk_success 80
2828
JNC label_bts2_loaded
29-
print_string_ext bls2_load_fail, bls2_load_fail_len, 05, 06, C_RED, C_BLACK, 0
29+
print_string_ext bls2_load_fail, bls2_load_fail_len, 06, 06, C_RED, C_BLACK, 0
3030
JMP label_exit
3131

3232
label_bts2_loaded:
33-
print_string_ext bls2_loaded, bls2_loaded_len, 05, 06, C_WHITE, C_BLACK, 0
33+
set_cursor_xy 06, 07, 00
34+
print_hex_string_ext 0x8000, 10, C_WHITE, 0
35+
print_string_ext bls2_loaded, bls2_loaded_len, 06, 06, C_WHITE, C_BLACK, 0
36+
3437
JMP 0x8000
3538
JMP label_exit
3639

3740
label_exit:
3841
HLT
42+
JMP label_exit
3943

4044
[SECTION .data]
4145
bl_welcome db " Fuzzy OS... (^_^) "
4246
bl_welcome_len equ ($-bl_welcome)
4347
bls1 db "Bootloader: Stage 1"
4448
bls1_len equ ($-bls1)
45-
bls2_loaded db "Stage 2 Loaded."
49+
bls2_loaded db "Stage 2 Loaded. Trying to launch..."
4650
bls2_loaded_len equ ($-bls2_loaded)
4751
bls2_load_fail db "Stage 2 Load Failed!"
4852
bls2_load_fail_len equ ($-bls2_load_fail)

src/bootloader/stage2.asm

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,45 @@
22
%include "constants.asm"
33
%include "io.asm"
44

5-
[ORG 0x8000]
6-
[BITS 16]
5+
[BITS 16]
76

7+
global _low_print
8+
extern entry_stage2
89

910
[SECTION .text]
10-
11+
CLI
1112
MOV ax, 0x0000
1213
MOV es, ax ; es := 0
13-
1414
set_blinking 0
15-
print_string_ext bl_stage_2, bl_stage_2_len, 05, 04, C_WHITE, C_BLACK, 0
15+
16+
print_string_ext bl_stage_2, bl_stage_2_len, 04, 09, C_WHITE, C_BLACK, 0
17+
call entry_stage2
1618

1719
JMP label_exit
1820

1921
_low_print:
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
22+
push ebp
23+
mov ebp, esp
24+
25+
; check es, bx
26+
mov ax, 0x1301 ; (print string, update cursor)
27+
mov bh, 0 ; (pagenumber)
28+
mov bl, [ebp + 0x18] ; (attribute)
29+
mov dh, [ebp + 0x14] ; (pos_y)
30+
mov dl, [ebp + 0x10] ; (pos_x)
31+
mov ecx, [ebp + 0xc] ; (string length)
32+
mov ebp, [ebp + 0x8] ; (es:bp string pointer)
33+
int 0x10
34+
2335
mov ax, 0
24-
leave
36+
mov esp, ebp
37+
pop ebp
2538
ret
2639

2740
label_exit:
2841
HLT
42+
JMP label_exit
2943

3044
[SECTION .data]
31-
bl_stage_2 db "We are at Bootloader: Stage 2"
45+
bl_stage_2 db "Bootloader: Stage 2"
3246
bl_stage_2_len equ ($-bl_stage_2)

src/bootloader/stage2.c

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,28 @@
1-
//extern void _low_print();
2-
void test () {
1+
#define C_BLACK 0x0
2+
#define C_BLUE 0x1
3+
#define C_GREEN 0x2
4+
#define C_CYAN 0x3
5+
#define C_RED 0x4
6+
#define C_MAGENTA 0x5
7+
#define C_BROWN 0x6
8+
#define C_LIGHT_GRAY 0x7
9+
#define C_DARK_GRAY 0x8
10+
#define C_LIGHT_BLUE 0x9
11+
#define C_LIGHT_GREEN 0xA
12+
#define C_LIGHT_CYAN 0xB
13+
#define C_LIGHT_RED 0xC
14+
#define C_LIGHT_MAGENTA 0xD
15+
#define C_YELLOW 0xE
16+
#define C_WHITE 0xF
17+
18+
#define make_color(fg, bg) ((unsigned char)((fg) + ((bg)<<4)))
19+
20+
extern void _low_print(char str[], unsigned short int n,
21+
unsigned char x,unsigned char y,
22+
unsigned char color);
23+
24+
char message_welcome[] = "C says 'Hello World'";
325

4-
}
526
void entry_stage2() {
6-
test();
27+
_low_print(message_welcome,sizeof(message_welcome)-1,6,11, make_color(C_GREEN, C_BLACK));
728
}

0 commit comments

Comments
 (0)