; ### DISCRIPTION ############################################################
; ###
; ### Version list
; ### ver 1.0:
; ###   Just looks up for the osloader in a FAT12 disc an loads it in to the
; ###   memory. If no file was found, it prints an error-message.
; ###   The bootloader is written in standard c-calling convention.
; ###   All segement and offset adresses are like 0000:XXXX
; ###
; ### written by Robert Hanke.
; ### Copyright (c) 2007
; ###
; ### DISCRIPTION END ########################################################





; ### MACRO SECTION ##########################################################

%include "../PRJ/SRC/includes/macros.asm"

; ### MACRO SECTION END ######################################################





; ### COMPILER SECTION #######################################################

[ORG 0x7C00]
[BITS 16]
[CPU 386]

; ### COMPILER SECTION END ###################################################





; ### FATINFOBLOCK JUMP SECTION ##############################################

JUMP:
	jmp short CODE_SECTION
	nop

; ### FATINFOBLOCK JUMP SECTION END ##########################################





; ### FATINFOBLOCK SECTION ###################################################

FAT_TABLE:
	OSName		db "NEWOS001"
	BytesPerSec	dw 0x0200
	SecPerClus	db 0x01
	ResvdSecCnt	dw 0x0001
	NumFATs		db 0x02
	RootEntCnt	dw 0x00E0
	TotSec		dw 0x0B40
	MediType	db 0xF0
	FATSize		dw 0x0009
	SecPerTrack	dw 0x0012
	NumHeads	dw 0x0002
	HiddenSec	dd 0x00000000
	TotSec32	dd 0x00000000
	DrvNum		db 0x00
	Reserved	db 0x00
	BootSig		db 0x29
	VolumeID	dd 0x00000001
	VolumeLabel	db "NO NAME    "
	FileSystype	db "FAT12   "

; ### FATINFOBLOCK SECTION END ###############################################





; ### CODE SECTION ###########################################################

CODE_SECTION:
	cli
	cld

	mov	ax, 0x9000			; set stack
	mov	ss, ax
	mov	sp, 0xFFFF

	test	dh, 0x20			; test if drive supports 13h
	jnz	readerror

	push	ds				; reset drive
	xor	ax, ax
	int	13h
	pop	ds
	jc	readerror			; error while reset

	push	READ_BUFFER			; buffer
	push	0x000E				; length
	push	0x0013				; start
	call	_lsn_read
	add	sp, 6

	mov	cx, FAT_RootEntCnt		; search file
	mov	ax, READ_BUFFER			; DS:SI => File name from
	xor	bx, bx				; RootDirectory
	mov	ds, bx				; ES:DI => FileName
	mov	es, bx
read_root_ent:
	push	cx
	mov	cl, DIR_NAME_SIZE
	mov	si, ax
	mov	di, FileName
	repe	cmpsb
	jz	short file_found		; found the file, leave loop
	add	ax, DIR_ETNRY_SIZE
	pop	cx
	loop	read_root_ent

	or	cx, cx				; file not found
	jz	short booterror

file_found:
	add	si, 0x0F			; point to DIR_Fst_Clus_Low
	lodsw
	mov	[FILE_START_SEC], ax		; save first sectoradress

	push	READ_BUFFER
	push	FAT_FATSize
	push	FAT_ResvdSecCnt
	call	_lsn_read
	add	sp, 6

	xor	cx, cx
read_next_sector:
	mov	ax, cx
	mov	bx, 0x0200
	mul	bx
	add	ax, OSLOADER_BUFFER
	push	ax				; OSLOADER_BUFFER + 512*n
	push	word 0x01			; read just 1 sector

	mov	ax, [FILE_START_SEC]
	add	ax, 0x1F			; add 31 for lsn align
	push	ax				; push real fdd sec
	call	_lsn_read
	add	sp, 6

	inc	cx				; increment the sectorcounter

	mov	ax, word [FILE_START_SEC]
	mov	bx, 0x03
	mul	bx
	mov	bx, 0x02
	xor	dx, dx
	div	bx				; ax = offset dx = shifting

	mov	bx, READ_BUFFER
	add	bx, ax

	mov	ax, [ES:BX]

	cmp	dx, 0x01			; dx=1 -> shr 4
	je	short shift			; dx=0 -> AND 0x0FFF

	and	ax, 0x0FFF
	jmp	short cont
shift:	shr	ax, 0x04

cont:	cmp	ax, 0x0FFF
	je	short execute			; we're finished, execute!

	mov	word [FILE_START_SEC], ax		; store the new sector
	jmp	short read_next_sector		; for the next loop



execute:	jmp	0000h:OSLOADER_BUFFER		; jump to kernel

booterror:
	push	MsgBootError
	call	_write
	inc	sp
	inc	sp
	jmp	short reboot

readerror:
	push	MsgReadError
	call	_write
	inc	sp
	inc	sp

reboot:
	push	MsgReboot
	call	_write
	inc	sp
	inc	sp

	xor	ax, ax				; Reboot after keypress
	int	0x16

	db	0xEA
	dw	0x0000
	dw	0xFFFF

; ### CODE SECTION END #######################################################





; ### FUNCTIONS SECTION ######################################################

FUNCTIONS_SECTION:
; --- _write -----------------------------------------------------------------
; -[ Param0 = String	]
_write:
	enter	0, 0
	pusha

	xor	ax, ax
	mov	ds, ax
	mov	si, [Param0]
	mov	ah, 0x0E
	mov	bx, 0x0007
__write_1:
	lodsb
	test	al, 0xFF			; al AND 0xFF but no storage
	jz	short __write_2
	int	10h
	jmp	short __write_1
__write_2:
	popa
	leave
	ret
; --- _write END -------------------------------------------------------------

; --- _lsn_read --------------------------------------------------------------
; -[ Param0 = LSN	] [3]
; -[ Param1 = nSectors	] [2]
; -[ Param2 = Buffer	] [1]
_lsn_read:
	enter 0, 0
	pusha

	jmp	short __lsn_read_code

	var0	db 0x00
	var1	db 0x00
	var2	db 0x00

__lsn_read_code:
	mov	ax, [Param0]			; LSN

	xor	dx, dx
	mov	bx, 0x12			; sector =
	div	bx				;          (LSN % 18)
	inc	dx				;                     + 1
	mov	byte [var2], dl			; var2 = sector
	xor	dx, dx
	mov	bx, 2				; head = (LSN / 18) % 2
	div	bx
	mov	byte [var1], dl			; var1 = head

	mov	ax, [Param0]			; LSN wiederherstellen
	mov	bx, 0x24			; cylinder = LSN / 36
	xor	dx, dx
	div	bx
	mov	byte [var0], al			; var0 = cylinder

__lsn_read_err:
	xor	bx, bx				; ES:BX Buffer
	mov	es, bx
	mov	bx, [Param2]

	mov	ax, [Param1]
	mov	ah, 0x02
	mov	ch, byte [var0]
	mov	cl, byte [var2]
	mov	dl, 0x00
	mov	dh, byte [var1]
	int	13h
	jc	short __lsn_read_err		; error

	popa
	leave
	ret
; --- _lsn_read END ----------------------------------------------------------

; ### FUNCTIONS SECTION END ##################################################





; ### PLACEHOLDER SECTION ####################################################

PLACEHOLDER_SECTION:
	times	510-(BOOTSTRAP_SECTION-DATA_SECTION)-($-$$) db 0x00

; ### PLACEHOLDER SECTION END ################################################





; ### DATA SECTION ###########################################################

DATA_SECTION:
	MsgBootError	db "OSLDR fehlt", 13, 10, 0
	MsgReadError	db "Fehler beim Lesen", 13, 10,  0
	MsgReboot	db "Neustart mit Tastendruck", 0
	FileName	db "OSLDR      "

; ### DATA SECTION END #######################################################





; ### BOOTSTRAP SECTION ######################################################

BOOTSTRAP_SECTION:
	dw	0xAA55

; ### BOOTSTRAP SECTION END ##################################################