[BITS 16]
[ORG 0x00]

	jmp	0x07C0:start		;Goto segment 0x07C0

start:

	cli
	mov	ax,cs
	mov	ds,ax
	mov	es,ax
	mov	ss,ax
	mov	sp,0xffff
	sti

	mov	si,msgFirst
	call	PrintMsg

	mov	si,msgBIOS
	call	PrintMsg
	call	ata_read3
	mov	ax,0x60
	mov	ds,ax
	mov	si,0x0000
	call	PrintMsg
	mov	ax,cs
	mov	ds,ax

	mov	si,msgLBA
	call	PrintMsg
	call	ata_read
	mov	ax,0x60
	mov	ds,ax
	mov	si,0x0000
	call	PrintMsg
	mov	ax,cs
	mov	ds,ax

	mov	si,msgCHS
	call	PrintMsg
	call	ata_read2
	mov	ax,0x60
	mov	ds,ax
	mov	si,0x0000
	call	PrintMsg
	mov	ax,cs
	mov	ds,ax


	jmp	$

PrintMsg:
	pushad
.repeat:
	lodsb				;load next character
	or	al,al			;test for NUL character
	jz	.done
	mov	ah,0x0E			;BIOS teletype
	mov	bh,0x00			;display page 0
	mov	bl,0x07			;text attribute
	int	0x10			;invoke BIOS
	jmp	.repeat
.done:
	popad
	ret

ata_read:
	pushad

; Try the MSB of the status register(BUSY)
; and loop if the bit is set
	mov	dx,0x1f7
.loop1:
	in	al,dx
	bt	ax,7
	jc	.loop1

	cli
; Try bit 6(Device ready) and loop if the bit is 0
	mov	dx,0x1f7
.loop2:
	in	al,dx
	bt	ax,6
	jnc	.loop2
	
; The following code will read from HDD to memory
	mov	al,2
	mov	dx,0x1f2
	out	dx,al				;Set the number of sectors

	mov	al,0x02
	mov	dx,0x1f3
	out	dx,al				;Set LBA(0:7)

	mov	al,0x00
	mov	dx,0x1f4
	out	dx,al				;Set LBA(8:15)

	mov	al,0x00
	mov	dx,0x1f5
	out	dx,al				;Set LBA(16:23)
	
	mov	al,11100000b				;Set LBA(24:27),LBA mode and drive 0
	mov	dx,0x1f6
	out	dx,al

	mov	dx,0x1f7
	mov	al,0x20
	out	dx,al				;Set the 0x20 command(Read sector with retry)
.loop3:
	in	al,dx
	bt	ax,3
	jnc	.loop3

	
	mov	ax,0x0060
	mov	es,ax
	mov	di,0x0000
	mov	ecx,256
	mov	dx,0x1f0			;This is the data port
	cld
	rep	insw

	popad
	ret

ata_read2:
	pushad

; Try the MSB of the status register(BUSY)
; and loop if the bit is set
	mov	dx,0x1f7
.loop1:
	in	al,dx
	bt	ax,7
	jc	.loop1

	cli
; Try bit 6(Device ready) and loop if the bit is 0
	mov	dx,0x1f7
.loop2:
	in	al,dx
	bt	ax,6
	jnc	.loop2
	
; The following code will read from HDD to memory
	mov	al,2
	mov	dx,0x1f2
	out	dx,al				;Set the number of sectors

	mov	al,0x02
	mov	dx,0x1f3
	out	dx,al				;Set sector number

	mov	al,0x00
	mov	dx,0x1f4
	out	dx,al				;Set cylinder low

	mov	al,0x00
	mov	dx,0x1f5
	out	dx,al				;Set cylinder high
	
	mov	al,10100000b			;Set LBA(24:27),LBA mode and drive 0
	mov	dx,0x1f6
	out	dx,al

	mov	dx,0x1f7
	mov	al,0x20
	out	dx,al				;Set the 0x20 command(Read sector with retry)
.loop3:
	in	al,dx
	bt	ax,3
	jnc	.loop3

	
	mov	ax,0x0060
	mov	es,ax
	mov	di,0x0000
	mov	ecx,256
	mov	dx,0x1f0			;This is the data port
	cld
	rep	insw

	popad
	ret

ata_read3:
	mov	ecx,5
.read:
	mov	dl,0x80
	mov	si,packet_begin
	mov	ah,42h
	int	13h			; Read!
	jc	.error
	ret
.error:
	mov	si,msgDot
	call	PrintMsg
	loop	.read
	ret

msgDot		db ".",0x00
msgLBA		db 0x0d,0x0a,"LBA read  ",0x00
msgCHS		db 0x0d,0x0a,"CHS read  ",0x00
msgBIOS		db 0x0d,0x0a,"BIOS read ",0x00
msgFirst	db 0x0D,0x0A,"MBR booted",0x00

packet_begin:
	db	packet_end-packet_begin-1
	db	0x0		;Reserved
	db	1		;Blocks to transfer
	db	0x0		;Reserved

	dw	0x0000		;Transfer buffer offset
	dw	0x0060		;Transfer buffer segment

	dd	0x00000002		;LBA 1
	dd	0x00000000		;LBA 2
packet_end:

times 510-($-$$)	db 0
end			dw 0xAA55
			db "Second block(1).First block is 0",0x00
times 1024-($-$$)	db 0
			db "third block(2).First block is 0",0x00
times 2048-($-$$)	db 0
