Solved

dos boot loading MBR

Posted on 2004-04-20
21
2,038 Views
Last Modified: 2008-01-16
when i boot from a DOS bootable diskette, is it possible to load the MBR sector of the hard and run it, just as if the computer was booted from hard disk ?
i've found many code examples showing how to load the MBR in memory but i don't find a way to run this code and boot the system.

thank you for your help.
0
Comment
Question by:psadac
  • 6
  • 6
  • 5
  • +3
21 Comments
 
LVL 11

Assisted Solution

by:dimitry
dimitry earned 125 total points
ID: 10870794
Take a look here:
http://home.no.net/tkos/info/hd.html

So what is the problem ? You do not know how to compile the code of bootloader ?
You do not now how to insert bootloader into diskette ?
You do not know how to change diskette bootloader to boot from HD ?

What is your main task ? You can simply return from diskete bootloader and BIOS shpuld continue to boot from HD.
0
 
LVL 8

Assisted Solution

by:manish_regmi
manish_regmi earned 125 total points
ID: 10876027
hi,
 If i understand your problem correctly, You want to load a bootsector from within dos and jump to it.
It can be done.
1. Read the sector of the disk. Bootsector is sector 0, U can use int 13.
2. Store the read buffer to the segment 07c0:0. (I am using this address because bios use this address to boot the computer. Just overwriting it. )
3. make a jump to that address.

you are done.

regards manish.
0
 
LVL 8

Expert Comment

by:manish_regmi
ID: 10876225
here is an example how to do it, Some people might say it is risky. But i dont thinkm so. Loadlin does the same.


mov ah, 0x02 /* function 2 */
mov al, 0x1  /* no of sectors */
mov dl, 0x80 /* disk no. 0x80 = 1st hard disk, 0= first floppy */
lea bx, buff  /* this can be any buffer or any address */
int 0x13

/* now the boot sector is loaded at the specified address  */
/* make a jump */

regards manish


0
 
LVL 19

Assisted Solution

by:BrianGEFF719
BrianGEFF719 earned 125 total points
ID: 10884994
to add on to manish's code...I've done this too, your best bet is NASM.


using int 13h, 02h, load it to memory address like 5000:5000 or something. Then just do a simple JMP 5000:5000.

Above manish posted the complete code to read the MBR.
Using his code you might want to do...

jmp [buff]


-Brian
0
 
LVL 14

Author Comment

by:psadac
ID: 10898653
thank you for all your suggestions. I've tried with any kind of jump instruction and various tricks with ret, but i get always the same result : it begins to boot and it hangs up without launching the OS. here is the code :

Present segment Para 'code'
            assume CS:Present, DS:Present, ES:Present, SS:MyStack
            
main PROC far

      org 200h

      mov ax,07C0h
      mov ds, ax
      
      mov dx, 07C0h      
      mov es, dx
      mov bx, 00h
                mov dl, 80h
      mov dh,0h      
      xor ch, ch
      mov cl, 1h
      mov ax, 0201h
      int 13h
           
        mov ax,07C0h
        push ax            ; cs : 07C0h
       
        xor ax,ax
        push ax            ; ip : 0
       
        ret      
            
main ENDP

Present ENDS

MyStack segment para STACK 'STACK'
      byte 200h dup (?)
MyStack ENDS

END main


is there something wrong ?

0
 
LVL 11

Expert Comment

by:dimitry
ID: 10902028
Try 'retf' instead of 'ret'.

How are you putting it into floppy bootsector ?
0
 
LVL 8

Expert Comment

by:manish_regmi
ID: 10910790
hi,
 Flags are not the problem.
 The main problem is that si must be pointing to the partition table. The table entry starts at offset 0xbe.

just add a line
mov si, 0xbe

regards manish
0
 
LVL 8

Expert Comment

by:manish_regmi
ID: 10910886
no, no,
 sorry about by last post. it is also not the case.
 Si i points to partition table, in the code of MBR.

I have the disassembly if you would like to see.

try using a far jump
jmp far seg:off

regards


0
 
LVL 14

Author Comment

by:psadac
ID: 10912796
let me clarify the process :
-i compile a dos executable file
-i boot from a dos bootable diskette (a win98 boot diskette)
-i run the executable previously compiled
-i've tried it on many computers, on a dual boot system with lilo (linux boot loader) it begins to launch lilo then it hangs.
-i've tried your suggestions (retf, far jump) but i get the same result.

so i thnk the jump is not the problem, there must be something else to do before the jump
0
 
LVL 11

Expert Comment

by:dimitry
ID: 10913323
You want to have a bootloader but what are you doing is not the same at all.
You need to insert it into the bootsector of the floppy and you need to take ORG 200h out.
And you need to reboot your computer and give the BIOS opportunity to boot with the help of your bootloader.
And you can copy your 512 bytes bootloader to floppy with the help of diskeditor or any other utility that
allows you sector copy.
In Linux you can use 'dd':
% dd if=mybootldr.bin of=/dev/fd0 bs=512 count=512
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 14

Author Comment

by:psadac
ID: 10913361
dimitry, i already know you can use dd on linux to read or write boot sector, but that's not what i want.
basically i have a dos boot diskette, and on a given condition i want to boot from the hard drive. i thought it was possible to download the boot sector of the hard drive, make a jump to the boot code, but maybe it's not possible.
0
 
LVL 11

Expert Comment

by:dimitry
ID: 10913599
No, my point is that DOS executable is not the same as Bootloader you are trying to run.
Run your code as Bootloader and not as DOS Executable.
0
 
LVL 3

Accepted Solution

by:
Dancie earned 125 total points
ID: 10922773
Why not just execute INT 19h. This is the dos restart interrupt.

From Ralph Brown's interrupt list

This interrupt reboots the system without clearing memory or restoring
        interrupt vectors.  Because interrupt vectors are preserved, this
        interrupt usually causes a system hang if any TSRs have hooked
        vectors from 00h through 1Ch, particularly INT 08.
Notes:      Usually, the BIOS will try to read sector 1, head 0, track 0 from drive
        A: to 0000h:7C00h.  If this fails, and a hard disk is installed, the
        BIOS will read sector 1, head 0, track 0 of the first hard disk.
        This sector should contain a master bootstrap loader and a partition
        table (see #00650).  After loading the master boot sector at
        0000h:7C00h, the master bootstrap loader is given control
        (see #00653).      It will scan the partition table for an active
        partition, and will then load the operating system's bootstrap
        loader (contained in the first sector of the active partition) and
        give it control.
      true IBM PCs and most clones issue an INT 18 if neither floppy nor hard
        disk have a valid boot sector
      to accomplish a warm boot equivalent to Ctrl-Alt-Del, store 1234h in
        0040h:0072h and jump to FFFFh:0000h.      For a cold boot equivalent to
        a reset, store 0000h at 0040h:0072h before jumping.
      VDISK.SYS hooks this interrupt to allow applications to find out how
        much extended memory has been used by VDISKs (see #00649).  DOS 3.3+
        PRINT hooks INT 19 but does not set up a correct VDISK header block
        at the beginning of its INT 19 handler segment, thus causing some
        programs to overwrite extended memory which is already in use.
      the default handler is at F000h:E6F2h for 100% compatible BIOSes
      MS-DOS 3.2+ hangs on booting (even from floppy) if the hard disk
        contains extended partitions which point at each other in a loop,
        since it will never find the end of the linked list of extended
        partitions
      under Windows Real and Enhanced modes, calling INT 19 will hang the
        system in the same was as under bare DOS; under Windows Standard
        mode, INT 19 will successfully perform a cold reboot as it appears
        to have been redirected to a MOV AL,0FEh/OUT 64h,AL sequence
BUG:      when loading the remainder of the DOS system files fails, various
        versions of IBMBIO.COM/IO.SYS incorrectly restore INT 1E before
        calling INT 19, assuming that the boot sector had stored the
        contents of INT 1E at DS:SI instead of on the stack as it actually
        does
SeeAlso: INT 14/AH=17h,INT 18"BOOT HOOK",INT 49"Tandy 2000",INT 5B"PC Cluster"
SeeAlso: MEM 0040h:0067h,MEM F000h:FFF0h,CMOS 0Fh
0
 
LVL 8

Expert Comment

by:manish_regmi
ID: 10924813
yes,
 If that algorithm is not a constraint, you can use int 0x19.
Just a single instruction will do the job.
But i was wondering why the first method is not working.

regards
0
 
LVL 14

Author Comment

by:psadac
ID: 10969393
sorry, but i can't use int 0x19 : i boot from a dos diskette and i want to run the hard disk boot loader, but i don't want to eject the diskette (it's an automated job). i've tried on many computers, it loads correctly the boot loader like lilo or grub, but it hangs as soon as i choose a system. i think the problem is related to the interrupt vector table, is there a way to reset the interrupt table vectors to their values after bios execution ?
0
 
LVL 3

Expert Comment

by:Dancie
ID: 10975319
I have done this by first booting from the hard disk with no extra drivers.
By pressing F5. then run debug with these commands

C:\>debug <enter>
ndos.dat   <enter>     ;give a name to write to a file ie. dos.dat
rcs        <enter>       ;set cs register -- gives basis for a data segment
0          <enter>        ; to the first segment
rcx       <enter>        ;set cx to 400h to give the length of the file in bytes
400    <emter>       ;and saves all vectors
w0      <enter>         ;write form the 0 data position
q         <enter>         ;quit

Now you have a data file that can be loaded at 0:0 -- over the interrupt vector table.


0
 
LVL 8

Expert Comment

by:manish_regmi
ID: 10993370
hi psadac,
 I checked the code, It also hangs in my computer. There is some problem,  but i am not figuring out where. hmm,
see this link if it can help,
http://www.geocities.com/thestarman3/asm/mbr/STDMBR.htm

regards.
0
 
LVL 14

Author Comment

by:psadac
ID: 10994992
ok, it's time to close, i don't have a solution to the problem but thank you for your help.
0
 
LVL 11

Expert Comment

by:dimitry
ID: 10998198
Hi psadac,

Sorry for delay. I found several small points we didn't mention to you and the code below is booting from floppy from HD.
I used TASM, so my directives are different.
1) MBR is copied to [0:7C00h] and not to [7C0h:0]. It is same physical address, but not the same segment register value.
2) Your bootloader from floppy is copied to the same address, so you need to move it before MBR read.
3) The code below was loaded from floppy and boot MBR from HD.
4) First jump and last 55AA was added to tell to BIOS that floppy has valid boot sector.

MODEL TINY
.186
.CODE

start:
  org 0h
  jmp loadmbr
loadmbr:

; Self Copy to [0:7A00]
  cli
  xor  ax, ax
  mov  ds, ax
  mov  es, ax
  mov  si, 7C00h + offset startload
  mov  cx, offset endload - startload
  mov  di, 7A00h
  cld
  rep movsb
; Far jump to loader code in system RAM for execution
  push es
  push 7A00h
  retf
startload:
  xor ax, ax
  mov es, ax
  mov bx, 7C00h
  mov dl, 80h
  mov dh, 0h
  mov cx, 1h
  mov ax, 0201h
  int 13h

  push 0           ; cs : 0h
  push 7C00h       ; ip : 7C00h
  retf
endload:
  org 510
  DB  55h, 0AAh
END start
END
0
 
LVL 11

Expert Comment

by:dimitry
ID: 11023731
Hi psadac,
Do you need additional help ?
0
 

Expert Comment

by:RobertoDeAlba
ID: 12615859
I wonder if someone finally could make and exe file, that succesfully loads MBR from hards disk, being on DOS without removing the flopply?
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Short answer to this question: there is no effective WiFi manager in iOS devices as seen in Windows WiFi or Macbook OSx WiFi management, but this article will try and provide some amicable solutions to better suite your needs.
Moving applications to the cloud or switching services to cloud-based ones, is a stressful job.  Here's how you can make it easier.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now