;
;                                         LemonOS boot sector
;                                           By Andrew Davis
;                        with LoadSectors func from tutorial by Matthew Blagden


[BITS 16]

  [ORG 0x7C00]

    jmp start

    OSID		db "LemonOS"
    BytesPerSector	dw 0200h
    SectorsPersector	db 01h
    LeadingSectors	dw 0001h
    NumberOfFATs	db 02h
    RootEntryMax	dw 00E0h
    TotalSectors	dw 0B40h
    MediaType		db 0F0h
    SectorsPerFAT	dw 0009h
    SectorsPerTrack	dw 0012h
    NumberOfHeads	dw 0002h
    HiddenSectors	dd 00000000h
    TotalSectors2	dd 00000000h
    DriveNumber 	db 00h
    Reserved		db 00h
    BootSignaure	db 29h
    VolumeID		dd 69696969h
    VolumeLabel 	db "LemonOS    "
    FATID		db "FAT12   "

    LoadSectors:
	    push ax
	    push bx
	    push cx
	    mov dl, 12h
	    div dl
	    inc ah
	    mov cl, ah
	    mov dh, al
	    and dh, 01h
	    shr al, 1
	    mov ch, al
	    mov dl, 00h
	    Read:
		mov ah, 02h
		mov al, 01h
		int 13h
		jc Read
	    pop cx
	    pop bx
	    pop ax
	    add bx, 0200h
	    inc ax
	    loop LoadSectors
	    ret

    start:
	    cli
	    mov  ax, 07C0h
	    mov  ds, ax
	    mov  es, ax
	    mov  ax, 9000h
	    mov  ss, ax
	    mov  sp, 0FFFFh
	    sti

    floppy_reset:
	    mov ah, 0
	    int 13h
	    or ah, ah
	    jnz floppy_reset

    kernel_load:
	    mov ax, 0
	    mov es, ax
	    mov bx, 0x1000
	    mov ax, 1
	    mov cx, 53
	    call LoadSectors


    gdt:
	    cli
	    xor ax, ax
	    mov ds, ax
	    lgdt [toc]
    pmode:
	    mov eax, cr0
	    or eax, 1
	    mov cr0, eax
	    jmp 08h:clean_up

[BITS 32]
    clean_up:
	    mov ax, 10h
	    mov ds, ax
	    mov ss, ax
	    mov esp, 090000h
    stop_floppy:
	    mov dx,3F2h
	    xor al,al
	    out dx,al
    kernel:
	    jmp 08h:01000h

    gdt_data:
	    dd 0
	    dd 0
	    dw 0FFFFh
	    dw 0
	    db 0
	    db 10011010b
	    db 11001111b
	    db 0
	    dw 0FFFFh
	    dw 0
	    db 0
	    db 10010010b
	    db 11001111b
	    db 0
    end_of_gdt:

    toc:
	    dw end_of_gdt - gdt_data - 1
	    dd gdt_data

    times 510-($-$$) db 0
    dw 0AA55h
