format binary as 'img'
use16
org 0x7c00
;############################## Start of 1st part ##############################
part1:
jmp bootcode ;Jump to bootcode
;The code below is based on the FAT16 definition.
times 3-($-$$) db 0
DB "BosByte" ;Microsoft wants us to put "MSWIN4.1" in here... fuck them!
times 11-($-$$) db 0
DW 512 ;Bytes per sector (do not change!)
DB 1 ;Sectors per Cluster
DW 1 ;Reserved sector count
DB 0x02 ;Num FATs (is ALWAYS 0x02)
DW 512 ;Root entry's count (Should be 512 for FAT16)
DW 2880 ;Sector count (2880 for 1.44 MB floppy)
DB 0xF0 ;Media (0xF0 is common)
DW 9 ;FatSize
DW 18 ;Sectors per Track (used by Interupt)
DW 2 ;Heads count (should be 2 for 1.44 MB floppy)
DD 0 ;Hidden sectors
DD 0 ;Used by FAT32
;Should be at offset 36 now . . .
DB 0x00 ;Drivenum (0 = floppy1)
DB 0 ;Reserved for windows NT, fuck windows!
DB 0x29 ;Indicates that the following three fields are present
DD 12345 ;VolumeID (just random shizzle)
DB "W00t-OS" ;VolumeName (max 11 chars)
times 54-($-$$) db " " ;Fill Volumename up to 11 chars
DB "FAT16" ;FileSysType (max 8 chars)
times 62-($-$$) db " " ;Fill FileSysType up to 8 chars
;End of FAT16 header
times 64-($-$$) db 0
;Now start the bootcode at offset 64:
bootcode:
;Creating stack . . .
cli
mov ax, 0x9000
mov ss, ax
mov ax, 0xFFFF
mov sp, ax
sti
;Stack created
call initscreen
mov si, BootMsg1
call puts
mov bl, 0x0C
mov si, BootMsg2
call putcs
call loadpart2
jmp part2
BootMsg1: DB "NM-SOFT W00t-OS [Version 0.0.1]",13,10,"Copyright (C) 2008 NM-Soft. All rights reserved.",10,13,0
BootMsg2: DB 10,13,"Please wait while loading W00t-OS from drive A . . .",10,13,0
;-------------- Functions ---------------
include 'functions1.inc.asm'
;Loadpart2(void)
loadpart2:
mov ah, 0x02
mov al, 3 ;number of sectors to read
mov cl, 2 ;Sector number
mov dl, 0x00 ;Drive number (0=A:, 1=2nd floppy, 80h=drive 0, 81h=drive 1)
mov ch, 0x00
mov dh, 0x00
push 0x0000
pop es
mov bx, part2 ; ES:BX = pointer to buffer
int 0x13 ; int IO (ah = 0x02 means READ) sets CF on error, clears it on succes
jnc .return
mov si,.errormsg
call puts
.error:
jmp .error
.return:
ret
.errormsg:
DB "An error occured while loading sectors 0x02 to 0x04.",0
;############################## End of 1st part ##############################
times 510-($-$$) db 0
dw 0xAA55
;############################## Start of 2nd part ##############################
part2:
call mode13
mov si, .msg1
call puts
;Create GDT
mov ax, 0x0100
mov es, ax
mov di, 0x0000
;Making a GDT POINTER:
; bx base low (word)
; dx base high (word)
; cx limit (word)
.gdtPointer:
mov bx, 0x1006 ;base low (word)
mov dx, 0 ;base high (word)
mov cx, 5*8-1 ;limit (word) = count(entries) * sizeof(entry) - 1
call gdtWritePointer ;Write GDT POINTER
;Making the GDT itself:
mov bx, 0 ;base low
mov dx, 0 ;base high
mov cx, 0 ;limit
mov al, 0 ;access
mov ah, 0 ;gran
call gdtWriteEntry ;First entry should be NULL
mov bx, 0 ;base low (word)
mov dx, 0 ;base high (word)
mov cx, 0xFfFF ;limit low (word)
mov ah, 0x0F + 16*1100b ;limit high (nibble) + gran (nibble)
mov al, 0x9A ;access (byte)
call gdtWriteEntry ;Write CODE segment (covers all memory)
mov bx, 0 ;base low (word)
mov dx, 0 ;base high (word)
mov cx, 0xFFFF ;limit low (word)
mov ah, 0x0F + 16*1100b ;limit high (nibble) + gran (nibble)
mov al, 0x92 ;access (byte)
call gdtWriteEntry ;Write DATA segment (covers all memory)
mov bx, 0x0000 ;base low (word) base = 90000
mov dx, 0x0009 ;base high (word)
mov cx, 0xFFFF ;limit low (word) limit = A0000
mov ah, 0x00 + 16*0100b ;limit high (nibble) + gran (nibble)
mov al, 0x92 ;access (byte)
call gdtWriteEntry ;Write STACK segment (0x9000:0 - 0x9000:0xFFFF)
mov bx, 0x0000 ;base low (word) base = A0000
mov dx, 0x000A ;base high (word)
mov cx, 0xFFFF ;limit low (word) limit = C0000
mov ah, 0x01 + 16*0100b ;limit high (nibble) + gran (nibble)
mov al, 0x92 ;access (byte)
call gdtWriteEntry ;Write VIDEO segment (0xA000:0 - 0xB000:0xFFFF)
mov si, .msg2
call puts
;Now entering Protected Mode:
mov al, '0'
call putc
mov al, '1'
call putc
lgdt [.gdtPointer] ;Let the CPU know where the GDT (pointer) is
mov al, '2'
call putc
mov ax,2401
int 15 ;enable A20
mov al, '3'
call putc
mov al, 'a'
call putc
mov eax, cr0
mov al, 'b'
call putc
or eax, 1 ;Set de PE bit in the MSW register
mov al, 'c'
call putc
mov cr0, eax
mov si, .msg1
call fputs
mov ax, 8*2 ; item 2 in GDT is the DATA segment . . .
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
mov al, '5'
call putc
mov ax, 8*3 ; item 3 in GDT is the STACK segment . . .
mov ss, ax
mov al, '6'
call putc
cli ;Disable interupts
jmp 0x08:ProtectedMode ;Far jump to protected mode (0x08 referers to item 8 in the GDT, that's the CODE segment)
.msg1:
DB "W00t-OS was succesfully loaded from drive A.",10,13,10,13,"Creating GDT . . .",10,13,0
.msg2:
DB 10,13,10,13,"Entering protected mode (32 bit) . . .",10,13,0
ProtectedMode:
call fprintcolors
.end:
jmp .end
;-------------- Functions ---------------
include 'functions2.inc.asm'
include 'gdt.inc.asm'
;############################## End of 2nd part ##############################
times 2048-($-$$) db 0
;############################## End of everyting ##############################
times 1474560-($-$$) db 0
It doesn't work on x64 because the first line has "extra characters on line", but it does work on 32 bit XP.