aboutsummaryrefslogtreecommitdiffstats
path: root/src/boot
diff options
context:
space:
mode:
Diffstat (limited to 'src/boot')
-rw-r--r--src/boot/boot.stage1.asm58
-rw-r--r--src/boot/boot.stage1.print.asm18
-rw-r--r--src/boot/boot.stage2.a20.asm2
-rw-r--r--src/boot/boot.stage2.asm48
-rw-r--r--src/boot/boot.stage2.gdt32.asm33
-rw-r--r--src/boot/boot.stage2.pm.asm28
-rw-r--r--src/boot/boot.stage2.print.asm39
7 files changed, 125 insertions, 101 deletions
diff --git a/src/boot/boot.stage1.asm b/src/boot/boot.stage1.asm
index d8dc84a..bb55dee 100644
--- a/src/boot/boot.stage1.asm
+++ b/src/boot/boot.stage1.asm
@@ -1,16 +1,8 @@
;; boot.stage1.asm
[bits 16]
-
+ [org 0x7c00]
global _start
- %include "boot.stage1.print.asm"
-
- %define READ_SECTORS_NUM 64
- %define BOOT_LOAD_ADDR 0x7c00
- %define STAGE2_ADDR 0x7e00
- %define SECTOR_SIZE 512
-
-section .boot_sector
_start:
;; Setup segment registers
@@ -18,26 +10,31 @@ _start:
mov ds, ax
mov es, ax
mov ss, ax
- mov gs, ax
- mov fs, ax
+ mov sp, STACK_ADDR ; Adjust stack
+
+ cld
+
+ mov [drive], dl ; Get boot drive number
+
+ ;; Show boot message
+ mov si, boot_msg
+ call _print_string
+ ;; Load Stage 2
+ mov ah, 0x42 ; Extended read
+ mov dl, [drive] ; Set drive number
+ xor ax, ax
+ mov ds, ax
mov si, DAP
- mov ah, 0x42 ; Extended read function
- mov dl, 0x80 ; Drive Number (starts at 0x80, thus it is 1)
- int 0x13
+ int 0x13 ; Read disk
jc _disk_read_err
-_ignore_disk_read_err:
- mov si, _jmp_msg
- call _print_string
jmp 0x0000:STAGE2_ADDR
+
_disk_read_err:
- cmp word [DAP.num_sectors], READ_SECTORS_NUM
- jle _ignore_disk_read_err
-
- mov si, _disk_read_err_msg
- call _print_string
+ mov si, disk_read_err_msg
+ call _print_string
.halt: hlt
jmp .halt
@@ -46,14 +43,17 @@ _disk_read_err:
DAP:
db 0x10 ; Size of packet
db 0 ; Zero
- .num_sectors:
dw READ_SECTORS_NUM ; Number of sectors to be read
- dd (BOOT_LOAD_ADDR + SECTOR_SIZE) ; Destination address
+ dw STAGE2_ADDR ; Destination address
+ dw 0x0000 ; Segment
dq 1 ; Which sector to start at
-_disk_read_err_msg: db "Disk error", 13, 10, 0
-_jmp_msg: db "Jumping to Stage 2", 13, 10, 0
+drive: db 0
+disk_read_err_msg: db "Disk error", 13, 10, 0
+boot_msg: db "Booting", 13, 10, 0
+
+ %include "boot.stage1.print.asm"
+ %include "common.asm"
- ;; Padding and magic number
- times 510 - ($ - $$) db 0
- dw 0xaa55
+ times 510-($-$$) db 0
+ dw 0xAA55
diff --git a/src/boot/boot.stage1.print.asm b/src/boot/boot.stage1.print.asm
index ee0efe2..6fa93c1 100644
--- a/src/boot/boot.stage1.print.asm
+++ b/src/boot/boot.stage1.print.asm
@@ -3,19 +3,15 @@
%define BOOT_STAGE1_PRINT_ASM
_print_string:
- pusha
- mov ah, 0x0e
- mov bh, 0x00
-
- .print_loop:
+ .loop:
lodsb
- test al, al
- je .print_return
+ or al, al
+ jz .done
+ mov ah, 0x0e
int 0x10
- jmp .print_loop
+ jmp .loop
+ .done:
+ ret
- .print_return:
- popa
- ret
%endif
diff --git a/src/boot/boot.stage2.a20.asm b/src/boot/boot.stage2.a20.asm
index 8fa3d37..3c69d79 100644
--- a/src/boot/boot.stage2.a20.asm
+++ b/src/boot/boot.stage2.a20.asm
@@ -1,5 +1,5 @@
;; boot.stage2.a20.asm
-
+ section .stage2
_enable_a20:
call _check_a20
test ax, ax
diff --git a/src/boot/boot.stage2.asm b/src/boot/boot.stage2.asm
index 759b784..52c73e5 100644
--- a/src/boot/boot.stage2.asm
+++ b/src/boot/boot.stage2.asm
@@ -1,19 +1,47 @@
;; boot.stage2.asm
[bits 16]
-
- %include "boot.stage2.a20.asm"
- %include "boot.stage2.pm.asm"
- %include "boot.stage1.print.asm"
-
-section .stage2
+ [org 0x7e00]
+
+ jmp 0x0000:_s2_entry
_s2_entry:
- mov si, boot_stage2_msg
+ cli
+ xor ax, ax
+ mov ds, ax
+
+ mov si, boot_s2_msg
call _print_string
- call _enable_a20
- call _enable_pm
+
+ call _enable_a20 ; Enable A20 line
+ lgdt [GDT32_ptr] ; Load GDT
+
+ ;; Setup Protected Mode
+ mov eax, cr0
+ or eax, 1
+ mov cr0, eax
+
+ jmp CODE_SEG32:_protected_mode
+
[bits 32]
+_protected_mode:
+ mov ax, DATA_SEG32
+ mov ds, ax
+ mov esp, 0x90000
+
+ mov esi, boot_protmode_msg
+ call _print_string_pm_vga
+ call 0x10000
+
+ jmp $
+
+ %include "boot.stage1.print.asm"
+ %include "boot.stage2.a20.asm"
+ %include "boot.stage2.gdt32.asm"
+ %include "boot.stage2.print.asm"
-boot_stage2_msg: db "Entering Stage 2", 13, 10, 0
+boot_s2_msg:
+ db "Entering Stage 2", 13, 10, 0
+boot_protmode_msg:
+ db "Enabling Protected Mode", 13, 10, 0
diff --git a/src/boot/boot.stage2.gdt32.asm b/src/boot/boot.stage2.gdt32.asm
index f766d82..2dbc461 100644
--- a/src/boot/boot.stage2.gdt32.asm
+++ b/src/boot/boot.stage2.gdt32.asm
@@ -1,29 +1,18 @@
;; boot.stage2.gdt32.asm
-
GDT32:
- .null:
- dd 0x0
- dd 0x0
- .code:
- dw 0xffff
- dw 0x0000
- db 0x00
- db 10011010b
- db 11001111b
- db 0x00
- .data:
- dw 0xffff
- dw 0x0000
- db 0x00
- db 10010010b
- db 11001111b
- db 0x00
-GDT32_end:
-
+ .null: dq 0x0000000000000000
+ .code: dq 0x00CF9A000000FFFF
+ .data: dq 0x00CF92000000FFFF
+GDT32_end:
+
GDT32_ptr:
.limit: dw GDT32_end - GDT32 - 1
.base: dd GDT32
- CODE_SEG32 equ GDT32.code - GDT32
- DATA_SEG32 equ GDT32.data - GDT32
+
+CODE_SEG32 equ 0x08
+DATA_SEG32 equ 0x10
+
+ ;; CODE_SEG32 equ GDT32.code - GDT32
+ ;; DATA_SEG32 equ GDT32.data - GDT32
diff --git a/src/boot/boot.stage2.pm.asm b/src/boot/boot.stage2.pm.asm
deleted file mode 100644
index 8897be6..0000000
--- a/src/boot/boot.stage2.pm.asm
+++ /dev/null
@@ -1,28 +0,0 @@
- ;; boot.stage2.pm.asm
-
-
- [bits 16]
- %include "boot.stage2.gdt32.asm"
-
-_enable_pm:
- cli
- lgdt [GDT32_ptr]
-
- ;; Enable Protected Mode
- mov eax, cr0
- or eax, 1
- mov cr0, eax
-
- jmp CODE_SEG32:_protected_mode
-
-[bits 32]
-_protected_mode:
- mov ax, DATA_SEG32
- mov ds, ax
- mov ss, ax
- mov es, ax
- mov fs, ax
- mov gs, ax
-
- ret
-
diff --git a/src/boot/boot.stage2.print.asm b/src/boot/boot.stage2.print.asm
new file mode 100644
index 0000000..d567d6d
--- /dev/null
+++ b/src/boot/boot.stage2.print.asm
@@ -0,0 +1,39 @@
+ ;; boot.stage2.print.asm
+
+ section .stage2
+
+ %define VGA_BUFFER 0xb8000
+ %define WB_COLOR 0xf
+
+
+_print_string_pm_vga:
+ mov eax, [esi]
+ lea esi, [esi+1]
+ cmp al, 0
+ jne .print_char_pm_vga
+ add byte [pos_x], 0
+ add byte [pos_y], 1
+ ret
+
+ .print_char_pm_vga:
+ mov ah, WB_COLOR ; White on black
+ mov ecx, eax
+ movzx eax, byte [pos_y]
+ mov edx, 160
+ mul edx
+ movzx ebx, byte [pos_x]
+ shl ebx, 1
+
+ mov edi, VGA_BUFFER ; Video memory start
+ add edi, ebx ; Add X offset
+ add edi, eax ; Add Y offset
+
+ mov eax, ecx ; Restore char
+ mov word [ds:edi], ax
+ add byte [pos_x], 1 ; Advance to right
+ ret
+
+pos_x: db 0
+pos_y: db 0
+
+