Virtualbox floppy disk `bootloader'

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
DiscobarMolokai
Posts: 5
Joined: Wed Dec 16, 2015 11:54 am

Virtualbox floppy disk `bootloader'

Post by DiscobarMolokai »

Greetings,

So yes, I'm another new guy, let's get that out of the way.
I decided to try my hand at writing a boot loader (or rather, something that could potentially become a boot loader), figuring that if I was unable to create an MBR binary that uses the BIOS to produce the compulsory "HELLO WORLD", I wouldn't have to try doing anything else related to OS development.

Right. To the point. I managed to get GNU as and ld to spit out a 512 byte flat binary, which I simply use as a floppy image and boot a Virtualbox instance from. It works, and messing with e. g. the addressing the linker script generates or the specific assembly instructions makes everything behave as I expect. All good.

However, the reason I'm posting here is that I'm starting to appreciate the complexity of all this OS dev magic and I want to be absolutely sure that I know what I'm doing. I do apologize if this is somehow a duplicate question or something like that -- I'm not trying to waste anybodies time; I did try multiple searches on this forum and the wiki.

The issue: the Virtualbox BIOS doesn't seem to care what it finds on the floppy image. If there's something in the first 512-byte sector, it will happily load it and transfer control to it. I can have the Virtualbox `boot' random garbage if I want to (no idea what that actually does, from what I observed it appears to just end up in some infinite loop). Needless to say, the signature 0x55AA at the end of the binary is ignored (I can leave it out and the program still works) whereas I read multiple times that BIOSes expect it to be there. What's going on here?
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: Virtualbox floppy disk `bootloader'

Post by Brendan »

Hi,
DiscobarMolokai wrote:The issue: the Virtualbox BIOS doesn't seem to care what it finds on the floppy image. If there's something in the first 512-byte sector, it will happily load it and transfer control to it. I can have the Virtualbox `boot' random garbage if I want to (no idea what that actually does, from what I observed it appears to just end up in some infinite loop). Needless to say, the signature 0x55AA at the end of the binary is ignored (I can leave it out and the program still works) whereas I read multiple times that BIOSes expect it to be there. What's going on here?
The first thing to understand about "80x86 PC" is that different computers are different. Most will check the 0x55AA signature, but a few don't. A few (very rare now) Compaq computers used to check if the first byte of the boot loader was a "jmp" instruction. Some computers boot with A20 already enabled and some don't. Some start your code with "CS=0x0000, IP=0x7C00" and some use "CS=0x07C0, IP=0x0000" instead. These differences permeate everything, all the way up to the amount of RAM and number of CPUs, and which types of devices are present.

Just because something works on one computer doesn't mean it's going to work on all computers. For an example; a while ago I wrote some code that stored variables in a special "boot information page". It filled this page with zeros initially, then relied on variables being pre-set to zero - things like "number of bytes loaded so far", "A20 state (0 = unknown, 1 = disabled, 2 = enabled)", etc. It turns out that my code was buggy and filled the wrong area with zeros. It worked perfectly on 9 different computers despite the bug (because for those computers the BIOS happened to leave that memory area full of zeros anyway), for one computer it worked half the time but did strange things the other half of the time, and for one computer it always did strange things.

Mostly; you want to test your OS on as many (real and virtual) computers as possible. For example; it shouldn't be too hard to use VirtualBox, Qemu and Bochs (and not just VirtualBox alone). It also shouldn't be too hard to configure multiple virtual machines differently (e.g. one "Bochs virtual machine" that emulates an 80486 with 4 MiB of memory, another configured to emulate a 32-bit AMD CPU with 500 MiB of memory, another that emulates a modern Core i7 with 8 GiB of memory, etc). You might also want to start collecting real test machines (e.g. maybe you have friends/family that have an old computer they'll let you have for free, maybe you can buy some cheap/dodgy second hand computers, etc).

Don't get me wrong here - a lot is the same on all "80x86 PC" too, and backward compatibility is something that computer manufacturers do care about. Mostly, it's a bit like this:
  • Works on 90% of computers: Relatively easy
    Works on 95% of computers: Takes some care, and testing on maybe 5 different computers
    Works on 99% of computers: Takes much more care, and testing on maybe 15 different computers
    Works on 99.9% of computers: Takes a lot of care, and thorough testing on maybe 50 different computers
    Works on 99.999% of computers: Extremely hard (multiple years of being in use by thousands of people).

Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
DiscobarMolokai
Posts: 5
Joined: Wed Dec 16, 2015 11:54 am

Re: Virtualbox floppy disk `bootloader'

Post by DiscobarMolokai »

This answer is exactly the reason why I posted. Thank you for your time.

An additional question: I find it very strange that some systems, as you noted, don't do a check like the 0x55AA signature or something similar. Like I said, Virtualbox gladly `boots' garbage. I mean, that's bad, right? If in such a system the BIOS accidentally attempts to boot something that it's not supposed to, there is almost no hope of it finding out and reporting it directly, more likely, the system will just hang or misbehave in some other obscure way. Maybe you forgot to eject your floppy containing some source code (or whatever) before rebooting; next thing you know, the PC starts executing data. It just seems that the signature check is a very easy (?) way to catch a lot of cases where the boot sector isn't actually bootable and to tell you about that. It's no rock solid guarantee that it won't accept garbage (if you assume the bytes are uniformly random, that's 1 chance in 65536, right?) but at least it's something. Is there a reason not to have the check?
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: Virtualbox floppy disk `bootloader'

Post by Brendan »

Hi,
DiscobarMolokai wrote:An additional question: I find it very strange that some systems, as you noted, don't do a check like the 0x55AA signature or something similar. Like I said, Virtualbox gladly `boots' garbage. I mean, that's bad, right?
In theory, all computers (including VirtualBox) should check the signature, so that if you've got a data disk (that isn't intended to be booted) it doesn't try to boot it and just moves on to the next device in the "boot order" list (e.g. "try floppy, then CD, then network, then hard disk"), and/or displays a "No bootable device found" error message if it has to.

In practice, Microsoft are morons. For floppy disks, DOS and Windows traditionally put a boot sector on "data only" disks, where that boot sector displays a "This disk isn't bootable" error message (and includes the 0x55AA boot signature). This destroys the BIOS' ability to automatically skip over "data only" disks and try the next device in the "boot order" list; and forces the user to remove the floppy disk before rebooting instead. If both bootable and "data only" floppy disks have the boot signature, there's not as much reason to check it.

Also note that (if I remember correctly) VirtualBox only has a single boot device and doesn't have a "boot order" list. When there's only a single boot device and that device isn't bootable; the computer can't boot (and the VM is mis-configured/useless) regardless of whether the 0x55AA signature is checked or not. The only difference would be whether a mis-configured VM displays a nice "No bootable device" error message from the BIOS or (potentially) crashes instead.

In any case (regardless of how useful it is/isn't); there's isn't any reason to fail to do the check - it's not like it's going to triple the size/complexity of the BIOS.


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
User avatar
SpyderTL
Member
Member
Posts: 1074
Joined: Sun Sep 19, 2010 10:05 pm

Re: Virtualbox floppy disk `bootloader'

Post by SpyderTL »

VirtualBox has a boot order list in the VM settings.

I also know that BOCHS lets you enable/disable the 0x55aa check in its VM settings under boot devices.

Unfortunately, since software is a lot easier to change than hardware, it's up to you to handle these situations. If you look at the open source Linux drivers, or even Windows DDK drivers, you'll find a lot of code that was just added to handle a single model produced by a single manufacturer that didn't follow the specifications to the letter. Still, your choices are to ignore these "quirky" devices, and just let them crash, or write code specifically for them.

This is just one of the many reasons OS level development is so difficult and time consuming.

The good news is that if you can get an OS working in VirtualBox, which is my personal preferred test platform, you are 80% of the way there. Tracking down quirky hardware issues from there is relatively easy. It just takes time.
Project: OZone
Source: GitHub
Current Task: LIB/OBJ file support
"The more they overthink the plumbing, the easier it is to stop up the drain." - Montgomery Scott
DiscobarMolokai
Posts: 5
Joined: Wed Dec 16, 2015 11:54 am

Re: Virtualbox floppy disk `bootloader'

Post by DiscobarMolokai »

SpyderTL wrote: This is just one of the many reasons OS level development is so difficult and time consuming.
Exactly, me realising this is the reason behind the OP :) . Thanks, guys.

--DM
Post Reply