;Layout of 1st MB of Memory:
;0h-03ffh: Real Mode Interrupt Vector Table (RAM)
;0400h-04ffh: BIOS data area (RAM)
;0500h-09ffff: free memory (RAM) (=09FB00h bytes)..see(1)
;0a0000h-0bffffh: video memory (RAM)  1280-
;0c0000-0c7fff: video bios (ROM)
;0c0000-oeffff: BIOS shadow area
;0f0000-0fffff: System bios (ROM)

;How we're going to make use of this first MB:
;512-byte bootsector after relocation: 0500-06ff
;stack: 0700h-106fch
;gdt: 10700-126ff
;(idt: 12700-146ff) not yet written
;boot: 14700-(09ffff)

%define 512_BASE 0500h
%define STACK_BASE 0700h
%define STACKTOP 0fffch
%define GDT_SIZE    512 ;%!GDT_SIZE
%define GDT_BASE 010700h
%define BOOT_SIZE 512
%define BOOT_BASE 014700h
%define SECTOR_SIZE 512 ;%!SECTOR_SIZE
%define SEGMENTS 5 ;number of segments in gdt by default

	BITS 16

	jmp 07C0h:start
start:
	mov [bootdrive], dl	;store the boot drive

	mov cx, 256	;256 words to copy
	mov ax, 0	
	mov ds, ax	;This is the source address from
	mov si, 07c00h	;where we copy the 512 byte boot code
	mov es, ax	;This is the destination address to
	mov di, 512_BASE	;where we copy the 512 byte boot code
	cld		;clear the direction flag
	rep movsw	;copy the boot code!

	jmp 512_BASE/010h:here	;jump to the relocated boot sector!

here:
	mov ax, cs	
	mov ds, ax		;Initialise
	mov es, ax		;the
	mov fs, ax		;data
	mov gs, ax		;registers
	mov ax, STACK_BASE/010h		;and
	cli			;the
	mov ss, ax		;stack
	mov esp, STACKTOP	;of
	sti			;course
	mov si, rlctn_msg	;Now let's tell the user
	call printstring	;that we have successfully relocated
	
	mov dl, [bootdrive]		;The drive we booted from
	mov ah, 00h ;reset disk drive	;The function to reset drive in dl
	int 13h				;Call interrupt 13h ah
	jc rsterr			;Display error message if drive reset failed
	mov ax, GDT_BASE/010h		;Segment where
	mov es, ax			;to load gdt
	mov bx, 0			;Address in segment es where to load gdt
	mov ah, 02h			;Function to read from drive
	mov al, GDT_SIZE/SECTOR_SIZE	;Read so many sectors
	mov cx, 02h			;Cylinder 0, sector 2
	mov dh, 0			;Head 0
	int 13h
	jc lderr

	mov ax, BOOT_BASE/010h		;Segment where
	mov es, ax			;to load boot
	mov bx, 0			;Address in segment es where to load boot
	mov ah, 02h			;Function to read from drive
	mov al, BOOT_SIZE/SECTOR_SIZE	;Read so many sectors
	mov cx, 04h			;Cylinder 0, sector 4
	mov dh, 0			;Head 0
	int 13h
	jc lderr

	mov ax, cs			;Reset es back to the
	mov es, ax			;base of this code
	mov si, gdtldd_msg
	call printstring
	cli
	lgdt [gdt_desc]
	mov eax, cr0	;Enable
	or eax, 1	;protected
	mov cr0, eax	;mode
	;remap interrupts here
	;reenable interrupts here
	jmp 20:enter_32

	BITS 32
enter_32:
	mov ax, 010h
	mov ds, ax
	mov es, ax
	mov fs, ax
	mov gs, ax

	mov eax , 018h
	mov ss, ax
	mov esp, STACKTOP
	jmp enter_32 ;hang

;	jmp 08:0
;	hlt
;here1:	jmp here1



align 2
rsterr:
	mov si, rsterr_msg
	call printstring
	cli
	hlt
align 2
lderr:
	mov si, lderr_msg
	call printstring
	cli
	hlt
align 2
;routine printstring: will print zero-terminated string from ds:si.
printstring:
	pusha
	pushf
	mov bx, 7 ;use the right page
	mov ah, 0Eh
nxtprintstring:
	lodsb
	or al, al
	jz endprintstring
	int 10h
	jmp nxtprintstring
endprintstring:
	popf
	popa
	ret
align 2
rlctn_msg:
	db 0ah, 0dh, 'Successfully relocated myself and set up data registers...',0
align 2
rsterr_msg:
	db 0ah, 0dh, 'Unable to reset the boot drive!',0
align 2
lderr_msg:
	db 0ah, 0dh, 'Unable to load the GDT from disk!',0
align 2
gdtldd_msg:
	db 0ah, 0dh, 'Successfully loaded the GDT from disk!',0
align 4
gdt_desc:
	dw (8*SEGMENTS)-1	;limit of the gdt
	dd GDT_BASE		;base address of the gdt
align 2
bootdrive:
times 510-($-$$) db 0 ; Fill the file with 0s
dw 0aa55h
