Testing your 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
User avatar
BenLunt
Member
Member
Posts: 934
Joined: Sat Nov 22, 2014 6:33 pm
Location: USA
Contact:

Testing your MBR code

Post by BenLunt »

In another thread, I commented a little about the MBR and how it should parse nested Extended Partitions.

I decided to create a small image with multiple nested Extended Partitions to verify that my MBR code indeed did what I advertised it would do.

Using my MPART tool, I gave it a resource input file of:

Filename: "mbr_test.txt"

Code: Select all

out: file="mbr_test.img", spt=63, heads=16  # the image file
mbr: file="mbr.bin"   # use "mbr.bin" as the MBR image file
part: base= 1, size=100, type= 1, active=0, last=0
part: base= 101, size=1, type=15, active=0, last=1
 part: base= 102, size=1, type= 15, active=0, last=0
  part: base= 103, size=1, type=15, active=0, last=0
   part: base= 104, size=100, type=1, active=0, last=1
  part: base= 204, size=1, type=15, active=0, last=1
   part: base= 205, size=1, type=15, active=0, last=1
    part: base= 206, size=100, type=1, active=0, last=1
 part: base= 306, size=1, type= 15, active=0, last=1
  part: base= 307, size=1, type=15, active=0, last=0
   part: file="mbr_test.bin", base= 308, size=100, type=1, active=1, last=1
  part: base= 408, size=100, type=1, active=0, last=1
From the DOS prompt, type:

Code: Select all

mpart /s:mbr_test.txt
This will create a small (256k) image file with the following format:

Code: Select all

standard partition (100 sectors)
extended partition
--extended partition
----extended partition
------standard partition (100 sectors)
----extended partition
------extended partition
--------standard partition (100 sectors)
--extended partition
----extended partition
------standard partition (100 sectors) (marked bootable)
----standard partition (100 sectors)
This assumes you have your MBR binary file, a 512-byte image of your MBR code, named "MBR.BIN" in the same directory. This is the file you will be testing. i.e.: This is your MBR code that goes in LBA 0 of the image file. For testing, I used my MBR.BIN file.

To be able to tell if you succeeded, this also assumes you have a file called "MBR_TEST.BIN" which is simply a bit of code placed in the "bootable" partition to print "Hey, we made it..."

Filename: "mbr_test.asm"

Code: Select all

; assembled with:  http://www.fysnet.net/newbasic.htm

outfile 'mbr_test.bin'    ; name of the file to create

.model tiny
.code
.186

           ; seg = 07C0h and offset 0000h
           org  00h

           ; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
           ; set up the stack and seg registers (real mode)
           mov  ax,07C0h                ; set our stack, DS, ES
           mov  ds,ax                   ;
           xor  ax,ax                   ;
           mov  ss,ax                   ; first push at 0x07BFE
           mov  sp,7C00h                ; ss = 0x0000, sp = 0x7C00
            
           mov  si,offset we_made_it
           call display_string
           
halt:      hlt
           jmp  short halt

we_made_it  db  13,10,' ***** We made it...',13,10,0

; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; display a string to the screen using the BIOS
; (destroys all registers used)
display_string proc near
           mov  ah,0Eh             ; print char service
           xor  bx,bx              ; 
           cld
display_loop:                      ; ds:si = asciiz message
           lodsb
           or   al,al
           jz   short end_string
           int  10h                ; output the character
           jmp  short display_loop
end_string: ret
display_string endp


; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; put sig at end of sector
           org (200h-2)
           
           dw  0AA55h

.end
Of course you can change the file names to whatever you wish, lines 1 and 2 holding the first two filenames:

Code: Select all

out: file="mbr_test.img", spt=63, heads=16  # the image file
mbr: file="mbr.bin"   # use "mbr.bin" as the MBR image file
MBR_TEST.IMG being the out file, the image file being created.
MBR.BIN being the binary 512-byte assembled code you are testing.

Line 13 being the active partition holding the boot code to show we made it (using filename "MBR_TEST.BIN" as the partitions' contents)

Code: Select all

   part: file="mbr_test.bin", base= 308, size=100, type=1, active=1, last=1
Again, your MBR code, if you wish to be ambitious as I was, should parse all nested extended partition tables until if finds the active partition.

You can change the "mbr_test.txt" to have more partitions, deeper nested partitions, or a different active partition just to be sure your code works.

For information on how the MPART tool parses your input file, see the sample file at the URL I gave above.

Hope this helps when testing your MBR code.

Ben
- http://www.fysnet.net/osdesign_book_series.htm
Post Reply