Advertising (This ad goes away for registered users. You can Login or Register)

x86 Programming

Discuss about your favorite (gaming...or not) devices here. The most popular ones will end up getting their own categories
Programming discussions for your favorite Device
Forum rules
Forum rule Nº 15 is strictly enforced in this subforum.
Nickolas
Posts: 174
Joined: Sat Jan 22, 2011 3:14 pm
Location: In a black hole...

x86 Programming

Post by Nickolas » Sat Apr 02, 2011 5:33 pm

I actually created this topic in order to have a place to ask certain things abou Operating Systems for x86 computers. I am currently developing my own OS in ASM. I have a simple OS up and running on a Floppy Disk... But my goal is to be able to install it on a hard drive..

So let's start with the questions:

1. How exactly does a floppy differ from a hard drive? (What should I learn about etc..)
2. Is there any comand in the BIOS to copy files or will I have to implement my own?
3. My OS uses FAT12 format.. (it is a 16bit OS). Is there any way to create a virtual floppy disk on a hard drive? (That way I won't have to change my code..)
I know it is possible to do it on a cd (MikeOS Dev Team have achieved that)...



More will come as I develop the OS. Any help is apreciated.
Advertising
Image
Image
Image
Image

Strangelove
Posts: 286
Joined: Thu Nov 25, 2010 6:32 pm

Re: x86 Programming

Post by Strangelove » Sat Apr 02, 2011 6:26 pm

Nickolas wrote:1. How exactly does a floppy differ from a hard drive? (What should I learn about etc..)
The hardware interface is quite different, but you can ignore that since you'll be using BIOS ISRs to start with. The main difference is partitions, while you can install a stage1 loader on the MBR, you'll typically want to do what they call chain-loading, the default MBR contains a simple loader that loads one sector (SBR) from the partition marked as the bootable one, so you install the stage1 loader here. Since you have limited space for it, typically all this loader does is to load a bigger loader or a kernel. this stage2 should be written to disk so that all the sectors follow each other. The easiest way to do this is to combine the stage1 and stage2 loaders in the same binary, this way the first part just loads the rest of the binary from disk.
2. Is there any comand in the BIOS to copy files or will I have to implement my own?
3. My OS uses FAT12 format.. (it is a 16bit OS). Is there any way to create a virtual floppy disk on a hard drive? (That way I won't have to change my code..)
No, the BIOS doesn't understand filesystems. That's the reason why DOS existed. Without DOS you have no way to access files, so you need to write your own code to handle this. Virtual disks can be implemented by either hooking an ISR or you can implement such functionality in a more mature OS or both.

To start with you'll typically hook an ISR. Save the original interrupt vector from the IDT, put the address to your own interrupt handler there. Your own handler has to call the original interrupt routine if the call wasn't meant for the virtual disk.


Another tip: I recommend NASM. Also avoid use of macros, use function calls instead otherwise you'll run out of space quickly.
Advertising
Last edited by Strangelove on Sat Apr 02, 2011 8:24 pm, edited 1 time in total.
"If you have specific questions ... don't hesitate to ask as the more generic the question is the more philosophic the answer will be" - PSPWizard

Nickolas
Posts: 174
Joined: Sat Jan 22, 2011 3:14 pm
Location: In a black hole...

Re: x86 Programming

Post by Nickolas » Sat Apr 02, 2011 8:23 pm

Strangelove wrote:Another tip: I recommend NASM. Also avoid use of macros, use funtion calls instead otherwise you'll run out of space quickly.
I am using NASM... Thanks for the help, though. I've got more questions:

I have heard of a way of using a .flp file containing your bootloader which you write to the boot section of a cd-rom, and that way you can boot. I am willing to sacrifice rewritability in order to get compatibility. How can I do this?

Secondly, I need a tutorial for writing my bootsector to a floppy disk (I can simply copy the rest of the files and kernel, right?).... (This is just for testing... I still prefer the first way...)

Also do you know of any good site or book about fundamental x86 knowledge? After all, I seem to be missing a lot of important stuff....

Lastly I recomend OS programming to everybody (as a hobby, of course..). The task is daunting but it is great fun. ;)

P.S. 1 Can I still buy floppy disks? (Will the guy at my local electronics shop think I am crazy, if I ask?)
P.S. 2 I am using Windows (Ubuntu didn't really appeal to me....)
Image
Image
Image
Image

Nickolas
Posts: 174
Joined: Sat Jan 22, 2011 3:14 pm
Location: In a black hole...

Re: x86 Programming

Post by Nickolas » Sat Apr 02, 2011 8:32 pm

[quote="Strangelove"
The hardware interface is quite different, but you can ignore that since you'll be using BIOS ISRs to start with. The main difference is partitions, while you can install a stage1 loader on the MBR, you'll typically want to do what they call chain-loading, the default MBR contains a simple loader that loads one sector (SBR) from the partition marked as the bootable one, so you install the stage1 loader here. Since you have limited space for it, typically all this loader does is to load a bigger loader or a kernel. this stage2 should be written to disk so that all the sectors follow each other. The easiest way to do this is to combine the stage1 and stage2 loaders in the same binary, this way the first part just loads the rest of the binary from disk.[/quote]

So you suggest that I write a bootloader which will function on a hard drive to load my current bootloader (which functions on a floppy but hopefully will work here, too) which will load my kernel? :?: :!: :?:
Image
Image
Image
Image

Strangelove
Posts: 286
Joined: Thu Nov 25, 2010 6:32 pm

Re: x86 Programming

Post by Strangelove » Sat Apr 02, 2011 8:59 pm

Nickolas wrote:I have heard of a way of using a .flp file containing your bootloader which you write to the boot section of a cd-rom, and that way you can boot. I am willing to sacrifice rewritability in order to get compatibility. How can I do this?
cd-roms use the el-torito standard. there's a couple of ways to boot, harddisk emulation, floppy emulation, no emulation. harddisk emulation is buggy, the best way is no emulation which is what isolinux uses. for max compatibility floppy emulation is the thing you want to use. you can use a cd-burning tool and instruct it to use a raw floppy image to boot from. .flp is probably not a raw 1:1 copy. Anyways... using the floppy emulation method it boots just as it would boot from a floppy.

to avoid burning a lot of cd-rs you can create a bootable iso image and load it using virtualbox to test your code.
Secondly, I need a tutorial for writing my bootsector to a floppy disk (I can simply copy the rest of the files and kernel, right?).... (This is just for testing... I still prefer the first way...)
well, one linux i used the dd tool to write directly to the disc. if you have a binary .BIN or .COM file you can just write it to the start of the floppy or use it as a raw floppy image. though some tools might object if you feed it an image that's not floppy-sized. you'll have to search yourself i guess.
Also do you know of any good site or book about fundamental x86 knowledge? After all, I seem to be missing a lot of important stuff....
from here:
http://ftp.lanet.lv/ftp/mirror/x2ftp/ms ... index.html
grab faqsys18, the inter* files, pcgpe, and whatever strikes your fancy. there's a lot of other interesting and useful stuff there.
more in-depth stuff such as intel assembly docs and the el-torito standard can be get from other sources.
Lastly I recomend OS programming to everybody (as a hobby, of course..). The task is daunting but it is great fun. ;)
it is a lot of fun, but it takes a lot of patience too. hours and days spent drinking coffee with your head deep in technical manuals. ;)
P.S. 1 Can I still buy floppy disks? (Will the guy at my local electronics shop think I am crazy, if I ask?)
you don't need floppy disks. a virtual computer is fine with disc images, and when your want to install the loader you can burn it to an usb stick or a cd-rom. IIRC it was sony who was the last manufacturer to stop production of floppy disks. but there are used ones available still. you can ask but don't blame me if people in white coats come to take you away. :lol:
So you suggest that I write a bootloader which will function on a hard drive to load my current bootloader (which functions on a floppy but hopefully will work here, too) which will load my kernel?
There's no big difference booting from floppy than booting from a harddisk. The first sector of either disc is read by the BIOS and the code is executed. This is only 512 bytes so you can't do much except for loading more data. On harddisks the MBR loads in the first sector from the partition so you can write your existing loader here and it should work. But as I said it's only 512 bytes so besides writing "hello world" to the screen, you'll need it to load in more sectors from disk to do something useful. Your bootloader can read a whole kernel at this point. If you put the bootloader at the start of the kernel binary, it can make things simpler, but you'll need to pad the data so that the kernel data starts on the second sector.
"If you have specific questions ... don't hesitate to ask as the more generic the question is the more philosophic the answer will be" - PSPWizard

Nickolas
Posts: 174
Joined: Sat Jan 22, 2011 3:14 pm
Location: In a black hole...

Re: x86 Programming

Post by Nickolas » Sun Apr 03, 2011 7:03 am

[quote="Strangelove"
So you suggest that I write a bootloader which will function on a hard drive to load my current bootloader (which functions on a floppy but hopefully will work here, too) which will load my kernel?
There's no big difference booting from floppy than booting from a harddisk. The first sector of either disc is read by the BIOS and the code is executed. This is only 512 bytes so you can't do much except for loading more data. On harddisks the MBR loads in the first sector from the partition so you can write your existing loader here and it should work. But as I said it's only 512 bytes so besides writing "hello world" to the screen, you'll need it to load in more sectors from disk to do something useful. Your bootloader can read a whole kernel at this point. If you put the bootloader at the start of the kernel binary, it can make things simpler, but you'll need to pad the data so that the kernel data starts on the second sector.
Hmm weird method... Up to now I've been using the kernel as a "launcher". I mean I have built all the functions outside the kernel in seperate .inc files and in the kernel I just use calls... But once again I am confused.. :D Which way will this method help and make things easier?
And one more question... The proccess (<-- spelling?) of writing the bootloader to the hard disk is the same as writing it to a floppy, right?
Image
Image
Image
Image

Strangelove
Posts: 286
Joined: Thu Nov 25, 2010 6:32 pm

Re: x86 Programming

Post by Strangelove » Sun Apr 03, 2011 3:37 pm

Nickolas wrote:The proccess (<-- spelling?) of writing the bootloader to the hard disk is the same as writing it to a floppy, right?
Figuring out where to put stuff on a harddisk might not be entirely straightforward. If you can't find a tool for it, it may be a good exercise to write your own. It will have to read the partition table and get the address of the active partition. You might want to make routines for reading and writing to disk that uses linear addresses too, instead of CHS addressing.
"If you have specific questions ... don't hesitate to ask as the more generic the question is the more philosophic the answer will be" - PSPWizard

Nickolas
Posts: 174
Joined: Sat Jan 22, 2011 3:14 pm
Location: In a black hole...

Re: x86 Programming

Post by Nickolas » Mon Apr 04, 2011 8:00 pm

Strangelove wrote:
Nickolas wrote:The proccess (<-- spelling?) of writing the bootloader to the hard disk is the same as writing it to a floppy, right?
Figuring out where to put stuff on a harddisk might not be entirely straightforward. If you can't find a tool for it, it may be a good exercise to write your own. It will have to read the partition table and get the address of the active partition. You might want to make routines for reading and writing to disk that uses linear addresses too, instead of CHS addressing.
Sounds difficult. I think that right now I should focus on the kernel... Thanks for the help Strangelove! More questions will come...
Image
Image
Image
Image

User avatar
m0skit0
Guru
Posts: 3817
Joined: Mon Sep 27, 2010 6:01 pm

Re: x86 Programming

Post by m0skit0 » Tue Apr 05, 2011 11:37 am

Code for controlling hardware devices should be independent from kernel, so you can replace it easily.
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"

Nickolas
Posts: 174
Joined: Sat Jan 22, 2011 3:14 pm
Location: In a black hole...

Re: x86 Programming

Post by Nickolas » Tue Apr 05, 2011 3:33 pm

m0skit0 wrote:Code for controlling hardware devices should be independent from kernel, so you can replace it easily.
It is... I just call it.. I have another question... Currently in order to test my OS I use an image of a floppy and i load it on VMWare.. But it takes a LOT of time... Is there any other way to do this? (The floppy image is .flp not .img)
Image
Image
Image
Image

Post Reply

Return to “Programming and Security”