OSDev.org

The Place to Start for Operating System Developers
It is currently Wed Nov 25, 2020 10:21 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: Testing your MBR code
PostPosted: Thu Oct 15, 2020 9:34 pm 
Offline
Member
Member
User avatar

Joined: Sat Nov 22, 2014 6:33 pm
Posts: 744
Location: USA
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:
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:
mpart /s:mbr_test.txt

This will create a small (256k) image file with the following format:
Code:
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:
; 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:
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:
   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


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 11 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group