Link to home
Start Free TrialLog in
Avatar of psadac
psadacFlag for France

asked on

dos boot loading MBR

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.
SOLUTION
Avatar of dimitry
dimitry

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of manish_regmi
manish_regmi

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


SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of psadac

ASKER

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 ?

Try 'retf' instead of 'ret'.

How are you putting it into floppy bootsector ?
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
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


Avatar of psadac

ASKER

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
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
Avatar of psadac

ASKER

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.
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.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Avatar of psadac

ASKER

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 ?
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.


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.
Avatar of psadac

ASKER

ok, it's time to close, i don't have a solution to the problem but thank you for your help.
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
Hi psadac,
Do you need additional help ?
I wonder if someone finally could make and exe file, that succesfully loads MBR from hards disk, being on DOS without removing the flopply?