Author Topic: making a usable mbr backup  (Read 23602 times)

Offline flyhigh427

  • Jr. Member
  • *
  • Posts: 60
making a usable mbr backup
« on: July 30, 2011, 11:50:28 AM »
what kind of file do you  save mbr to?
i know hard disks  and floppy use different metheds..
can both be done with nasm?
thanks

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: making a usable mbr backup
« Reply #1 on: July 30, 2011, 12:37:26 PM »
You pretty much did it. Read the MBR into a 512 byte buffer, write the buffer to "mbr.bin". We got distracted by "hex" and reading the MBR using IDE controller ports instead of int 13h, but you've got the fundamentals there. To "use" it, read "mbr.bin" into a 512 byte buffer and write it to a hard drive using int 13h (or another method). Since you seem to be having a lot of trouble with it, I'd be pretty cautious with that part, if I were you!

Best,
Frank


Offline flyhigh427

  • Jr. Member
  • *
  • Posts: 60
Re: making a usable mbr backup
« Reply #2 on: July 30, 2011, 12:59:13 PM »
im scared
im still testing on floppy then usb hd.
i still have alot to do ,it gives me something to do.
p.s. i have to put it in a bin filenot a txt file you say?

Offline flyhigh427

  • Jr. Member
  • *
  • Posts: 60
Re: making a usable mbr backup
« Reply #3 on: July 30, 2011, 08:37:11 PM »
now that ive backed up everthing on my c: drive  and my usb drive ..
are  ntfs  drives going to be way different then floppys?
thanks

Offline MJaoune

  • Jr. Member
  • *
  • Posts: 94
Re: making a usable mbr backup
« Reply #4 on: July 31, 2011, 12:06:49 AM »
now that ive backed up everthing on my c: drive  and my usb drive ..
are  ntfs  drives going to be way different then floppys?
thanks

Yes, but not way different, since a floppy has only 1.5 MB of space, and are slower than drives, also they have their own drive to be put in. That is what I know, but for your chemical test it might work.

Best,
Mahmoud

Offline flyhigh427

  • Jr. Member
  • *
  • Posts: 60
Re: making a usable mbr backup
« Reply #5 on: July 31, 2011, 01:38:10 PM »
hey yall im getting access denied is that cause im running from windows xp?


Code: [Select]
;read mbr
        mov     dx,81h                  ;DX Data register  ; read MBR of target drive
                                        ;drive a=ooh,b=01h,80h=first hard drive.81h=second

        mov     cx,1h                   ;CX count register for string operations ; it's always the first sector

        mov     bx, textbuf             ;BX index register for MOVE ; point to our buffer

        mov     ax, 0201h               ;AX Accumulator/multiply/divide ; read one sector

        int     13h                     ;13h is Drive Table
        jc      ReadError   



Offline Rob Neff

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 429
  • Country: us
Re: making a usable mbr backup
« Reply #6 on: July 31, 2011, 01:48:14 PM »
Windows XP runs in protected mode and will not allow you to have direct access to hardware.  Try using an emulator or simply boot with DOS if that is your goal...

Offline flyhigh427

  • Jr. Member
  • *
  • Posts: 60
Re: making a usable mbr backup
« Reply #7 on: July 31, 2011, 02:01:08 PM »
can i run nasm code in protected mode too?
thanks

Offline Keith Kanios

  • Full Member
  • **
  • Posts: 383
  • Country: us
    • Personal Homepage
Re: making a usable mbr backup
« Reply #8 on: July 31, 2011, 03:56:11 PM »
can i run nasm code in protected mode too?
thanks

RTFM :)

Offline Serge

  • Jr. Member
  • *
  • Posts: 26
  • Country: ru
Re: making a usable mbr backup
« Reply #9 on: August 01, 2011, 02:46:35 PM »
can i run nasm code in protected mode too?
NASM knows nothing about environment where you plan to run your code, – real or protected mode, 16/32 or 64 bit code. It is up to you – write the correct code for proper environment. It's just an assembler – translates mnemonics to opcodes. But as an assembler NASM really can do everything.
« Last Edit: August 01, 2011, 02:48:48 PM by Serge »

Offline flyhigh427

  • Jr. Member
  • *
  • Posts: 60
Re: making a usable mbr backup
« Reply #10 on: August 03, 2011, 12:24:08 AM »
hey yall i tested in vmware and i get nothing.
but i rebooted and tested it and got the first sector
even though i couldnt see the drive. do u think its a good copy of c: drive ?
if im saving it to bin with this code do you think i could write it back to hd to see?
thanks


Code: [Select]

org 100h
section .text

start:



;read mbr
        mov     dx,80h                  ;DX Data register  ; read MBR of target drive
                                        ;drive a=ooh,b=01h,80h=first hard drive.81h=second

        mov     cx,1h                   ;CX count register for string operations ; it's always the first sector

        mov     bx, textbuf             ;BX index register for MOVE ; point to our buffer

        mov     ax, 0201h               ;AX Accumulator/multiply/divide ; read one sector

        int     13h                     ;13h is Drive Table
        jc      ReadError   

;create test2.bin


                mov ah,3Ch               ; the open/create-a-file function
                mov cx,2                 ; file attribute - normal file
                mov dx,filename2         ; address of a ZERO TERMINATED! filename string
                int 021h                 ; call on Good Old Dos
                jc      BadOpen
                mov [filehnd2],ax        ; returns a file handle (probably 5)



;write to test2.txt


                mov ah,040h              ; write-to-a-file
                mov bx,[filehnd2]        ; file handle for standard output
                mov cx,512               ; bytes to write - same number we read :)
                mov dx,textbuf           ; buffer to write from
                int 021h                 ; call on Good Old Dos
         

;close test2.txt

                mov ah,03Eh               ; close the file
                mov bx,[filehnd2]         ; yeah, file handle in bx
                int 021h                  ; call on Good Old Dos
                jc      CloseError
                jmp exit
CloseError:

           mov dx,msgclose
           mov ah,09           
           int 21h   
           xor ax,ax
           int 016h
           jmp exit
ReadError:
           mov dx,msgread
           mov ah,09
           int 21h
           xor ax,ax
           int 016h
           jmp exit 
BadOpen:

           mov dx,msgopen
           mov ah,09
           int 21h
           xor ax,ax
           int 016h
           jmp exit 
exit:
    mov ah,04Ch         ; terminate-program function
    int 021h            ; you guessed it!
 


;--------------------------------------------------
section .data
   
    msgclose db "unable to close file...", 0x0d, 0x0a, '$'
    msgread db "read error...", 0x0d, 0x0a, '$'
    msgopen db "open error...", 0x0d, 0x0a, '$'



    filename2 db 'test2.bin',0
    ;filename1 db 'test1.txt',0
;--------------------------------------------------

    textbuf times 512 db 0
   
   
   
    filehnd1 dw 0    ;file handles need dw for word
    filehnd2 dw 0    ;file handles need dw for word

    read_len  dw 0


Offline flyhigh427

  • Jr. Member
  • *
  • Posts: 60
Re: making a usable mbr backup
« Reply #11 on: August 03, 2011, 01:01:47 AM »
hey
on the write to file comment
if i change dl to 80h in both places ,,thats the right place to put for c: drive right?
thanks
Code: [Select]
;rfloppy opens a bin file test2.bin witch you have to have allready made
;with test2.asm witch makes a backup of a: drive floppy and saves it
;as test2.bin

[BITS 16]
[ORG 0x100]
;open file
   mov  ax,3D00h           ;Open the file
   mov     al, 0           ;Open for reading
   mov  dx,filename        ;Presume DS points at filename
   int  21h                ;call dos
   jc      justError       ; check for errors here!
   mov     [filehnd1], ax  ;Save file handle
 
;read from file
   mov  bx,[filehnd1]      ;Get file handle value
   mov  ah,3Fh             ;Read data from the file
   mov  dx,buffer          ;Address of data buffer
   mov  cx,512
   int  21h
   jc      justError       ; check for errors here!
;close the file
   mov     bx, [filehnd1]
   mov  ah,3Eh
   int  21h
   jc      justError       ; check for errors here! 
;write to floppy
   
xor ax,ax
mov dl,0          ;is this the place to put 80h
int 0x13
jc justError
mov ah,0x09
mov dx,InitSuccess
int 0x21
loop1:
mov ah,0
mov dl,0          ;is this the place to put 80h
int 0x13
mov al,1
mov ah,3
        mov cx,1
mov dx,0         
mov bx,buffer
int 0x13
jnc WriteOK
dec byte [Counter]
jz  justError
jmp loop1
WriteOK:
mov ah,0x09
mov dx,WriteSuccess
int 0x21
        xor ax,ax
        int 016h
        jmp exit





exit:
   mov ax, 4C00h
   int 21h

justError:

           mov dx,msgclose
           mov ah,09           
           int 21h   
           xor ax,ax
           int 016h
           jmp exit
;--------------------------------------------------
section .data
   
    msgclose db "error...", 0x0d, 0x0a, '$'
    InitSuccess: db "Floppy initialised successfully.",0x0d, 0x0a, '$'
    WriteSuccess: db "Bootsector written successfully.",0x0d, 0x0a, '$'
    Counter: db 3
filename  db 'test2.bin',0
filehnd1 dw 0    ;file handles need dw for word
buffer    times 512 db 0
;---------------------------



Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: making a usable mbr backup
« Reply #12 on: August 03, 2011, 04:37:40 AM »
Three places...

Code: [Select]
; write to floppy
   
xor ax,ax
mov dl,0          ;is this the place to put 80h
int 0x13
jc justError
mov ah,0x09
mov dx,InitSuccess
int 0x21
loop1:
mov ah,0
mov dl,0          ;is this the place to put 80h
int 0x13
mov al,1
mov ah,3
        mov cx,1
mov dx,0          ; also here!
mov bx,buffer
int 0x13
        ...

The drive number goes in dl, for the "reset" function (ah=0), the "read" function (ah=2), and the "write" function (ah=3) of int 13h. As I recall, bios assigns "80h" to whichever (physical) drive is booted from (as selected in "setup"). Then, the "code part" of the MBR reads the partition table and loads the "real bootsector" from the partition marked "active". This is what dos will call "drive C:". Dos assigns other drive letters to... physical drives first, then logical partitions (or is it the other way around?). In any case, if you've got multiple drives with multiple partitions, it is quite possible to become confused as to the relationship between "80h", "81h", etc. and "C:", "D:", etc. (ask me how i know!). Be careful!

Your code looks correct to me - you're making good progress... If you're working with a "test machine" that it wouldn't kill ya to lose, "go for it". If you feel the need to "be more careful", you could read the proposed .bin file into a buffer, display the partition table (at least), confirm with the user, read the drive/head/cylinder/sector that you propose to replace into a (different!) buffer, display the partition table (at least), confirm, and write the buffer to the drive... making sure that it's the same as what you displayed - maybe use a common subroutine to set up cx and dx, even? At this point, you're writing an "fdisk" clone, more or less...

Just to make sure you know, a floppy drive doesn't have an MBR. I don't know about a USB device... I would guess "no" if it's emulating a floppy, "yes" otherwise? No idea what an emulator does - whatever you tell it to, I suppose...

I don't remember, for sure, just what the attached file does... I think it just writes to a .dat file and doesn't try to write anything with int 13h, so it should be "safe" to try(?). It'll generate a number of warnings with "modern" Nasm, due to labels with no ':'. Should be okay to ignore 'em... or fix 'em. There may be something in there you can use...

Best,
Frank


Offline flyhigh427

  • Jr. Member
  • *
  • Posts: 60
Re: making a usable mbr backup
« Reply #13 on: August 03, 2011, 02:59:05 PM »
thanks frank i thought maybe 3 places wasnt sure ,i lkie to comment  these ,im doing it now.
again thanks to all .
hey i found one way in vb6 but it was way to complacated for me right now ,it deals with locking the drive and  unlocking it afterwords ,,anyways thanks again

Offline flyhigh427

  • Jr. Member
  • *
  • Posts: 60
Re: making a usable mbr backup
« Reply #14 on: August 03, 2011, 03:42:24 PM »
hey just  tested it on my c hard drive  it was scary but worked.