Newer
Older
tree-os / src / boot / boot.asm
BITS 16
ORG 0x7C00

section .text

loadOS:
    mov [BOOT_DRIVE], dl

	xor ax, ax
    mov ds, ax
    mov es, ax
    mov ss, ax
    mov fs, ax
    mov gs, ax

    mov bp, 0x9000
    mov sp, bp
resetDisk:
	xor ah, ah
	mov dl, [BOOT_DRIVE]
	; int 0x13
load:
	; mov ah, 0x02
	; mov al, 1
	; mov cx, 0x01
	; mov bx, CODE_SEG
	; mov es, bx
	; mov bx, KERNEL_POSITION
	; mov dl, [BOOT_DRIVE]
	; mov dh, 0x00
	; int 0x13
	; jc error
	; jmp dump
	; mov ah, 0x42
	; mov dl, [BOOT_DRIVE]
	; mov cx, 0x00
	; mov ds, cx
	; mov si, diskAddrebx
	; mov eax, 0x0001
	; mov dl, [BOOT_DRIVE]
   	; mov di, KERNEL_POSITION
   	; mov cx, 0x01

diskLoad:
	; mov ah, 0x02 ; read
	; mov al, 0x01 ; sector count
	; mov cx, 0x0002 ; cylinder & sector
	; mov dh, 0x00 ; head
	; mov dl, [BOOT_DRIVE] ; drive
	; ; xor bx, bx ; position
	; ; mov es, bx
	; xor bx, bx
	; mov es, bx
	; mov bx, KERNEL_POSITION
	; mov ah, 0x42
	; mov dx, DAP
	; mov si, DAP
	; xor dh, dh
	mov dl, [BOOT_DRIVE]
    mov ah, 0x02   ;Read Disk Sectors
    mov al, 0x05   ;Read one sector only
    mov ch, 0x00   ;Track 0
    mov cl, 0x02   ;Sector 2
    mov dh, 0x00   ;Head 0
    ; mov dl, 0x00   ;Drive 0 (Floppy 1)
    ; mov bx, 0x800 ;Segment 0x2000
    ; mov es, bx
    mov bx, KERNEL_POSITION ;Start of Segment

	int 0x13

	mov [errorByte], ah

switchToX86:
    cli

    in al, 0x70
    or al, 0x08
    out 0x70, al

	lgdt [gdtEnd]

    mov eax, cr0
    or  eax, 0x01
    mov cr0, eax

    jmp CODE_SEG:_start

bits 32
_start:
    mov ax, 16
    mov ds, ax
    mov ss, ax
    mov es, ax
    mov fs, ax
    mov gs, ax
	mov al, [BOOT_DRIVE]
	call print_hex_byte
    mov ebx, hello
	call printString
	mov al, [errorByte]
	call print_hex_byte
dump:
	mov ebx, KERNEL_POSITION
	mov cl, 5
.loop:
	mov al, [ebx]
	call print_hex_byte
	inc ebx
	dec cl
	jnz .loop
	jmp $

printString:
	push ebx
	push ax
.loop:
	mov al, [ebx]
	or al, al
	jz .return
	call printChar
	inc ebx
	jmp .loop
.return:
	pop ax
	pop ebx
	ret


print_hex_byte: 
   mov [.temp],al
   shr al,4
   cmp al,10
   sbb al,69h
   das

   call printChar
 
   mov al,[.temp]
   ror al,4
   shr al,4
   cmp al,10
   sbb al,69h
   das
   call printChar
 
   ret
.temp db 0x00

printChar:
	push ebx
	mov ebx, [.position]
	mov [ebx], byte al
	inc ebx
	mov [ebx], byte 0x07
	inc ebx
	mov [.position], ebx
	pop ebx
	ret

.position dd 0xb8000

ALIGN 4

gdt32:
	dq 0

code:
	dw 0xffff
	dw 0
	db 0
	db 10011010b
	db 11001111b
	db 0

data:
	dw 0xffff
	dw 0
	db 0
	db 10010010b
	db 11001111b
	db 0
gdtEnd:
    dw gdtEnd - gdt32 - 1
    dd gdt32

CODE_SEG equ 8
DATA_SEG equ 16

BOOT_DRIVE: db 0
ALIGN 4
DAP:
.size      db 0x10
.reserved  db 0x00
.sectors   dw 0x0001
.offset    dw KERNEL_POSITION
.segment   dw 0x0000
.lbaLsd    dd 0x00000000
.lbaMsd    dd 0x00000002

errorByte db 0x00

hello db 'hello world! ', 0x00
errorMessage db 'error! ', 0x00



padding:
    times 510-($-$$) db 0x00
	dw 0xAA55

KERNEL_POSITION equ 0x7e00
KERNEL_SIZE equ 1