Page 1 of 3

My FirstOS

Posted: Thu Oct 27, 2011 5:20 pm
by kendfrey
Hey anyone who wants to feel free to test this bootloader. I made it from the ground up. I would be particularly interested in ways to improve the code. It comes with a simple kernel that prints a message and hangs. You may certainly substitute your own kernel. Tested in Bochs, not on real hardware yet.

Outline:
KERNEL.BIN loads and begins execution at 0x1000:0x0000 in real mode. ds and es are 0x1000, ax, bx, cx, dx, di and si are 0x0000. The screen is in text mode, and filled with spaces with color attribute 0x07. The cursor is at the top left corner of the screen.

Floppy image: floppy.img

Bootloader (MASM):

Code: Select all

.186
.model small
.code

kernel segment at 1000h
org 0
start label far
kernel ends

org 7c00h

first:
jmp short Main
nop

OSName            db "FIRSTOS "
BytesPerSector    dw 512
SectorsPerCluster db 1
ReservedSectors   dw 1
NumberOfFATs      db 2
RootDirEntries    dw 224
NumberOfSectors   dw 2880
MediaType         db 0f0h
SectorsPerFAT     dw 9
SectorsPerTrack   dw 18
NumberOfHeads     dw 2
HiddenSectors     dd 0
DWSectors         dd 0
DriveNumber       db 0
Reserved          db 0
BootSignature     db 29h
SerialNumber      dd 0
VolumeLabel       db "FOSBOOTDISK"
FileSystem        db "FAT12   "

Main:
; set up segments & stack
cli
xor ax, ax
mov ds, ax
mov es, ax
jmp es:MainSetCS
MainSetCS:
mov ss, ax
mov sp, 0f000h
sti
; load root directory
mov ax, 0500h
mov es, ax
mov cx, 4
MainRetryRoot:
push cx
xor ax, ax
mov al, [NumberOfFATs]
mul [SectorsPerFAT]
add ax, [ReservedSectors]
mov [LBAFirstData], ax
call LBAToCHS
mov ax, 0020h
push dx
mul [RootDirEntries]
div [BytesPerSector]
pop dx
add [LBAFirstData], ax
mov ah, 02h
xor bx, bx
int 13h
pop cx
jnc MainSuccessRoot
loop MainRetryRoot
int 18h
MainSuccessRoot:
; read root directory
mov cx, [RootDirEntries]
xor di, di
MainNextEntry:
push cx
mov cx, 11
mov si, offset KernelFile
push di
repe cmpsb
pop di
pop cx
je MainEntryFound
add di, 0020h
loop MainNextEntry
int 18h
MainEntryFound:
add di, 26
mov ax, es:[di]
mov [Cluster], ax
; load FAT
mov cx, 4
MainRetryFAT:
push cx
mov ax, [ReservedSectors]
call LBAToCHS
mov ax, [SectorsPerFAT]
mov ah, 02h
int 13h
pop cx
jnc MainSuccessFAT
loop MainRetryFAT
int 18h
MainSuccessFAT:
; read FAT and load clusters
MainReadCluster:
mov ax, 1000h
mov es, ax
mov cx, 4
MainRetryCluster:
push cx
mov ax, [Cluster]
sub ax, 2
xor dx, dx
mov dl, [SectorsPerCluster]
mul dx
add ax, [LBAFirstData]
call LBAToCHS
mov al, [SectorsPerCluster]
mov ah, 02h
int 13h
pop cx
jnc MainNextCluster
loop MainRetryCluster
int 18h
MainNextCluster:
mov ax, 0500h
mov es, ax
mov ax, [Cluster]
mov dx, ax
shr ax, 1
add ax, dx
push bx
mov bx, ax
mov ax, es:[bx]
pop bx
test dx, 0001h
jnz MainOddCluster
and ax, 0fffh
jmp MainClusterNumber
MainOddCluster:
shr ax, 4
MainClusterNumber:
add bx, [BytesPerSector]
mov [Cluster], ax
cmp ax, 0ff0h
jb MainReadCluster
; clear screen and move cursor
mov ax, 0b800h
mov es, ax
mov ax, 0720h
xor bx, bx
MainNextChar:
mov es:[bx], ax
add bx, 2
cmp bx, 4000
jb MainNextChar
xor bx, bx
xor dx, dx
mov ah, 02h
int 10h
; set up registers and call kernel
mov ax, kernel
mov ds, ax
mov es, ax
xor ax, ax
xor bx, bx
xor cx, cx
xor dx, dx
xor di, di
jmp kernel:start

LBAToCHS:
xor dx, dx
div [SectorsPerTrack]
mov cl, dl
inc cl
xor dx, dx
div [NumberOfHeads]
mov ch, al
mov dh, dl
mov dl, [DriveNumber]
ret

KernelFile db "KERNEL  BIN"
LBAFirstData dw 0
Cluster dw 0
;db (510 - ($ - first)) dup (0)
;dw 0aa55h
end
Kernel (MASM):

Code: Select all

.model tiny
.code
mov si, offset msg
Next:
lodsb
or al, al
jz Done
mov ah, 0eh
int 10h
jmp Next
Done:
jmp $

msg db "In the kernel! :)", 0
end

Re: My FirstOS

Posted: Fri Oct 28, 2011 2:26 am
by Combuster

Code: Select all

jmp es:MainSetCS
That does not at all do what you think it does.


Edit: Actually it gets assembled as:

Code: Select all

jmp MainSetCS
nop 
nop

Re: My FirstOS

Posted: Fri Oct 28, 2011 6:19 am
by kendfrey
You're right! e9 02 00 90 90
How do I set the CS register then?

Re: My FirstOS

Posted: Fri Oct 28, 2011 7:42 am
by kendfrey
I can't find it on the wiki. I searched and browsed, no luck.

Re: My FirstOS

Posted: Fri Oct 28, 2011 8:27 am
by Chandra
kendfrey wrote:You're right! e9 02 00 90 90
How do I set the CS register then?
Read the processor manual. There are different ways to do this.

Re: My FirstOS

Posted: Fri Oct 28, 2011 8:38 am
by kendfrey
Where would I find that information?

Re: My FirstOS

Posted: Fri Oct 28, 2011 8:55 am
by shikhin
Hi,
Chandra wrote:Read the processor manual. There are different ways to do this.
kendfrey wrote:Where would I find that information?
Perhaps the processor manual?

Regards,
Shikhin

Re: My FirstOS

Posted: Fri Oct 28, 2011 8:58 am
by kendfrey
I meant, where would I find the processor manual?

Re: My FirstOS

Posted: Fri Oct 28, 2011 9:01 am
by bluemoon
kendfrey wrote:I meant, where would I find the processor manual?
Is that so difficult to search the f***ing manual?
http://www.lmgtfy.com/?q=intel%20manual
http://www.lmgtfy.com/?q=amd%20manual

Are you sure to engage for OS development mission? Check this out: Getting_Started

Re: My FirstOS

Posted: Fri Oct 28, 2011 9:18 am
by kendfrey
I googled and didn't find anything that really said "This is the manual". I was kinda confused. I will download the 5 volume manual from Intel's software development manuals page and hope I can find something in there.

Re: My FirstOS

Posted: Fri Oct 28, 2011 12:48 pm
by DavidCooper
kendfrey wrote:I googled and didn't find anything that really said "This is the manual". I was kinda confused. I will download the 5 volume manual from Intel's software development manuals page and hope I can find something in there.
What you really need is a manual for MASM rather than for the processor, and someone at http://www.masm32.com/board/index.php? should be able to help you find it or maybe even take the trouble to solve your problem directly. For help using your tools, always ask at forums related to those tools first and save OSDev for issues related to actual OS development.

Re: My FirstOS

Posted: Fri Oct 28, 2011 2:00 pm
by kendfrey
I couldn't find any such thing in the license. I did see that it says no open source software, but not anything about developing OS's.

Re: My FirstOS

Posted: Fri Oct 28, 2011 3:34 pm
by Darwin
berkus wrote:Also note that MASM license explicitly forbids to use it for OSdev.
Not according to the wiki page for MASM.

Re: My FirstOS

Posted: Fri Oct 28, 2011 3:35 pm
by JAAman
@berkus, kendfrey
you are both wrong, because both of you have been reading the wrong license

this is a common myth... there is nothing in the MASM license forbidding its use for OSdev

there is a section in the MASM32 license forbidding the use of MASM32 in OSdev (#4 under the heading: what can you not do with the MASM32 project) but those are in fact, 2 completely separate products, created by 2 completely separate companies:

MASM: the Microsoft Asembler, used by MS compilers, and also a free download from the Microsoft website, nothing in its license forbidding its use for anything other than simple redistributing itself (basically, you cant make a program that is nothing more than the code MS provides for you, and distribute that as your own product -- also, the downloadable version also forbids commercial use, for that, use the version that comes with the for-pay VS Pro)


MASM32: a non-Microsoft add-on product sometimes used with MASM for developing windows programs in assembly, includes a license forbidding its use in developing anything not intended to run under windows, and forbidding its use to create GPL software (note that the masm32 website states that it forbids "open source" but that is not true, it only forbids open source software using certain licenses)

Re: My FirstOS

Posted: Fri Oct 28, 2011 3:46 pm
by kendfrey
I don't see how MASM32 forbids developing operating systems. It forbids developing "software for Non-Microsoft Operating Systems". I am not doing that. I am not developing software thats runs in an operating system, therefore I am not developing software that runs in a non-Microsoft operating system. (Since a non-MS OS is an OS.) The only thing I can see that would be restricting me is using MASM32 for developing software for my operating system.