Page 1 of 1

How to debug MBR Code.

Posted: Fri Jul 07, 2006 8:08 pm
by nirvana111
i'm write a mbr code, i rewrite the int 9 ISR, and when the coputer on and press a key,it will goto a little os, it will go windows 2000 when no key press, but the code has some wrong , when i press the key , the coputer will go to death.

so i had recovery the backup mbr and go to windows 2000 to debug the code and rewrite it to first sector. again and again.it looks like so annoying.

any one who have a good idea to debug the mbr code. thanks very much.

nirvana111

Posted: Sat Jul 08, 2006 7:04 am
by bontanu
First there is very little space available for such "additions" in the MBR.
On a HDD the MBR is 512bytes max and in fact you have to substtract the space reserved for the partition table and initial predefined values at the start of MBR... again this leaves very little space available.

Fom my experience the first problem is exactly this: your code grows ggreater than 512bites or overwrites the partition table.

Then you only have BIOS ints avalable and for debugging i would suggest some printf or printstrings... however the print routines and strings will also take up space.

Another technique is to place a here: jmp here infinite loop to see where the code reaches before reboot... together with some "signs" on the text mode screen at segment B800:0000.

Or you could test the MBR code in Bochs and its debugger...but things that do not work in Bochs do work ok in a real machine and things that do not run in a real machine do work perfectly in Bochs... so it will be helpfull but also miss leading at times.

The last resort would be to dissasemble your own boot code in IDA_PRO...
you will have to setup the segments and environment manually in IDA but it can be done

Or you could use Pacifica :D to run your OS insides another OS... but then it does not look like you have the expertise needed... not many have it anyway so do not worry ;)

Posted: Sat Jul 08, 2006 9:09 am
by carbonBased
bontanu wrote:First there is very little space available for such "additions" in the MBR.
On a HDD the MBR is 512bytes max and in fact you have to substtract the space reserved for the partition table and initial predefined values at the start of MBR... again this leaves very little space available.
That's how big the boot sector is, yes. But your boot loader does not, necessarily, need to be limited by this.

The bootsector can do some minimal initialization and load the rest of it from elsewhere on the disk, and jump to it. This is how virtually every bootloader works (I actually can't think of one that does anything extensive that actually fits within 512 bytes). Grub is a good example to look at -- stage1 is 512 bytes, yes, but stage2 is considerably larger (it would appear as though the stage2 that I use for my OS is a (surprising) ~90k!!!).

--Jeff

Posted: Sun Jul 09, 2006 1:08 am
by nirvana111
thanks to reply.
i put the windows MBR to other Secoter. so no problem at this.
i have found the problem, that is i use int 13 in int 9 ISR, because
when occur int 9, the 8259a was closed, so nothing do when call
int 13 in int 9, i write (mov al, 20h out 20h, al) before invoke
int 13 to open the 5259a, now the program goes will.

nirvana111

Posted: Mon Jul 10, 2006 10:12 pm
by Da_Maestro
your other option is to use an emulator like bochs and step through the code. put a breakpoint at 0000:7C00 :D

Posted: Thu Jul 13, 2006 7:37 pm
by nirvana111
Thank you Da_Maestro, i will do it. :arrow: :arrow: