Understanding the loading of stage2

If you are new to OS Development, plan on spending some time here first before going into the other forums.

Moderator:Moderators

Post Reply
Believer424
Posts:7
Joined:Sat Apr 24, 2010 4:19 am
Understanding the loading of stage2

Post by Believer424 » Sat Apr 24, 2010 4:28 am

Could someone please explain this to me. When the bootloader calls ReadSectors it always defines a value to bx first. Now what I'm confused about is what bx = 0x0200 two times. We set it to that before the loading of the root directory and before the loading of the FATs. When you loaded the FATs wouldn't it overwrite the root directory we loaded in?

And I have another question unrelated to this (kind of). I like to test the code on a real compute every once in a while using an old floppy disk I had. It was working fine until I ran into loading stage2. When I tried to copy it to the drive it says (generally) there is no current file system on the drive. And soon after I realized that I can't even open up the drive regularly. Windows says it needs to be formatted and asks me if I want to. So I say yes but then it says the drive is unable to be formatted.

Someone please explain these two problems to me

Thanks

Andyhhp
Moderator
Posts:387
Joined:Tue Oct 23, 2007 10:05 am
Location:127.0.0.1
Contact:

Re: Understanding the loading of stage2

Post by Andyhhp » Sat Apr 24, 2010 1:10 pm

When you loaded the FATs wouldn't it overwrite the root directory we loaded in
Yes it will. The thing you have to realise is that this is a 1 off load from disk. The root directory is used to find the first block on disk, and the FAT is used to find the next block given the first.

As a result, when you have got the information from the root directory, you no longer need it to completely load the file.


As for your formatting problem, Are you sure that the floppy disk had write protection disabled? The fact of not having having a valid file system could mean that you have accidently corrupted the bpb in your code. Another explanation could be that the disk itself is broken in which case, you need a new disk. It is hard to suggest more without seeing the problem.

~Andrew
Image

Believer424
Posts:7
Joined:Sat Apr 24, 2010 4:19 am

Re: Understanding the loading of stage2

Post by Believer424 » Sat Apr 24, 2010 4:00 pm

Ahh that makes sense.

As for the floppy disk though. The actual problem began when I was compiling the demo's tutorial on the MSCV++ for the first kernel demo. I compiled it and it worked fine on Bochs. So I wanted to test it on my other computer. So I put it on the floppy it and worked fine. Now I brought the floppy back and soon realized that I couldn't format it. I tried to clean the solution from MSCV++ and it said unable to delete KRNL32.EXE. That must have meant it was unable to access it. And now that file is stuck on the floppy disk forever now I guess. But the weird thing is though that I can still compile and partcopy the bootloader to the disk and that will still work fine. It's just copying other files to it

Andyhhp
Moderator
Posts:387
Joined:Tue Oct 23, 2007 10:05 am
Location:127.0.0.1
Contact:

Re: Understanding the loading of stage2

Post by Andyhhp » Sat Apr 24, 2010 4:15 pm

That means that uyour bootloader is not a valid FAT12 image, meaning that your BPB is corrupted.

~Andrew
Image

Believer424
Posts:7
Joined:Sat Apr 24, 2010 4:19 am

Re: Understanding the loading of stage2

Post by Believer424 » Sat Apr 24, 2010 4:19 pm

I'm not sure how, I was looking right off Mike's when I typed it. Here it is:

bpbOEM: db "My OS "
bpbBytesPerSector: dw 512
bpbSectorsPerCluster: db 1
bpbReservedSectors: dw 1
bpbNumberOfFATs: db 2
bpbRootEntries: dw 224
bpbTotalSectors: dw 2880
bpbMedia: db 0xF0
bpbSectorsPerFAT: dw 9
bpbSectorsPerTrack: dw 18
bpbHeadsPerCylinder: dw 2
bpbHiddenSectors: dd 0
bpbTotalSectorsBig: dd 0
bsDriveNumber: db 0
bsUnused: db 0
bsExtBootSignature: db 0x29
bsSerialNumber: dd 0xa0a1a2a3
bsVolumeLabel: db "MOS FLOPPY "
bsFileSystem: db "FAT12 "

I honestly don't see what I did wrong.

halofreak1990
Posts:92
Joined:Thu May 27, 2010 8:54 pm
Location:Netherlands

Re: Understanding the loading of stage2

Post by halofreak1990 » Fri Jun 04, 2010 7:10 pm

Try and open the boot1.bin file on your floppy with a hex editor, and see if you can find the "My OS " and the "MOS FLOPPY " strings.
If you can't find those and only see some gibberish, it means the bootloader was not copied to the floppy correctly.

My bootloader boots fine on real hardware as well as in VirtualPC, but as soon as stage 2 is loaded it somehow moves to the next device in the boot sequence, instead of loading my kernel from the floppy disk. It doesn't display any errors about a corrupt kernel image or so, so I'm a little confused here.

Post Reply