How to debug MBR Code.

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
nirvana111
Posts: 20
Joined: Wed Jun 21, 2006 7:55 am
Location: China

How to debug MBR Code.

Post 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
bontanu
Member
Member
Posts: 134
Joined: Thu Aug 18, 2005 11:00 pm
Location: Sol. Earth. Europe. Romania. Bucuresti
Contact:

Post 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 ;)
User avatar
carbonBased
Member
Member
Posts: 382
Joined: Sat Nov 20, 2004 12:00 am
Location: Wellesley, Ontario, Canada
Contact:

Post 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
nirvana111
Posts: 20
Joined: Wed Jun 21, 2006 7:55 am
Location: China

Post 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
Da_Maestro
Member
Member
Posts: 144
Joined: Tue Oct 26, 2004 11:00 pm
Location: Australia

Post 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
Two things are infinite: The universe and human stupidity. But I'm not quite sure about the universe.
--- Albert Einstein
nirvana111
Posts: 20
Joined: Wed Jun 21, 2006 7:55 am
Location: China

Post by nirvana111 »

Thank you Da_Maestro, i will do it. :arrow: :arrow:
Post Reply