;--------------------------------------------------;
;                      VectorOS                    ;
;                  By Omar Mohammad                ;
;                                                  ;
; mbr.asm                                          ;
; Master Boot Record                               ;
;--------------------------------------------------;

bits 16

mbr_entry:
	cli
	mov ax, 0
	mov ss, ax
	mov sp, 0xffff
	sti

	mov bx, dx

	mov ax, 0
	mov es, ax
	mov ds, ax

	cli
	cld
	mov si, 0x7C00
	mov di, 0x500
	mov ecx, 512
	rep movsb
	jmp 0x50:main

main:
	mov ax, 0x50
	mov ds, ax
	sti

checkpart1:
	cmp word[part1.bootFlag], 0x80
	jne checkpart2
	mov di, part1
	jmp load_bootsect

checkpart2:
	cmp word[part2.bootFlag], 0x80
	jne checkpart3
	mov di, part2
	jmp load_bootsect

checkpart3:
	cmp word[part3.bootFlag], 0x80
	jne checkpart4
	mov di, part3
	jmp load_bootsect

checkpart4:
	cmp word[part4.bootFlag], 0x80
	jne no_active_part
	mov di, part4
	jmp load_bootsect

load_bootsect:
	mov eax, dword[di+8]
	mov dword[packet.lba], eax
	mov si, packet
	mov ah, 42h
	int 13h
	jc .disk_error

	mov ax, 0
	mov ds, ax
	lea si, [di+0x500]

	mov ax, 0
	mov ds, ax
	mov ax, 0x50
	mov es, ax

	mov si, 0x7DFE
	mov di, boot_signature
	cmpsw
	jne .boot_error

	mov dx, bx
	jmp 0:7C00h

.disk_error:
	mov si, disk_error_msg
	call print_string

	jmp $

.boot_error:
	mov ax, 0x50
	mov ds, ax

	mov si, not_bootable_msg
	call print_string

	jmp $

no_active_part:
	mov si, no_active_part_msg
	call print_string

	jmp $

packet:
.size			db 16
.reserved		db 0
.sectorstoread		dw 1
.location		dd 0x00007c00
.lba			dd 0
			dd 0

print_string:
	pusha
	mov ah, 0Eh

.loop:
	lodsb
	cmp al, 0
	je .done
	int 10h
	jmp .loop

.done:
	popa
	ret

disk_error_msg				db "Disk I/O failure.",0
no_active_part_msg			db "No active partition found.",0
not_bootable_msg			db "Partition marked bootable but without boot signature, corrupt MBR?",0
boot_signature				dw 0xAA55

times 0x1BE - ($-$$) db 0

part1:
.bootFlag			db 0x80
.startinghead			db 0
.startingsector			db 2
.startingcylinder		db 0
.filesystemID			db 0xF3
.endinghead			db 255
.endingsector			db 255
.endingcylinder			db 255
.lba				dd 1
.totalsectors			dd 65000

part2:
.bootFlag		db 0
.startinghead		db 0
.startingsector		db 0
.startingcylinder	db 0
.filesystemID		db 0
.endinghead		db 0
.endingsector		db 0
.endingcylinder		db 0
.lba			dd 0
.totalsectors		dd 0

part3:
.bootFlag		db 0
.startinghead		db 0
.startingsector		db 0
.startingcylinder	db 0
.filesystemID		db 0
.endinghead		db 0
.endingsector		db 0
.endingcylinder		db 0
.lba			dd 0
.totalsectors		dd 0

part4:
.bootFlag		db 0
.startinghead		db 0
.startingsector		db 0
.startingcylinder	db 0
.filesystemID		db 0
.endinghead		db 0
.endingsector		db 0
.endingcylinder		db 0
.lba			dd 0
.totalsectors		dd 0

times 510 - ($-$$) db 0
dw 0xAA55
