Bienvenido! - Willkommen! - Welcome!

Bitácora Técnica de Tux&Cía., Santa Cruz de la Sierra, BO
Bitácora Central: Tux&Cía.
Bitácora de Información Avanzada: Tux&Cía.-Información
May the source be with you!

Monday, August 8, 2011

mbrfix.exe

Source
Usage:
MbrFix /drive  {/partition }  { /yes } { /byte }
Requirements:
The program is made for Windows NT, Windows 2000, Windows XP, Windows Server 2003, Windows Vista, Windows Server 2008, Windows 7 and Windows PE. Both 32-bit and 64-bit x64 editions are supported.
You need administrative privileges to make it work!
Purpose:
Perform several Master Boot Record (MBR) tasks, like backing up, restoring, fixing the boot code in the MBR, etc. The utility should not be used for GUID Partition Table (GPT) disks. The utility now, by popular demand, also come in a x64-version running unde x64-editions of Windows and PE.

Commands:

MbrFix /drive  driveinfo              Display drive information
    MbrFix /drive  drivesize              Returns drive size in MB as return value
    MbrFix /drive  listpartitions         Display partition information
    MbrFix /drive  savembr          Save MBR and partitions to file
    MbrFix /drive  restorembr       Restore MBR and partitions from file
    MbrFix /drive  fixmbr {/vista|/win7}  Update MBR code to W2K/XP/2003, Vista or Win7
    MbrFix /drive  clean                  Delete all partitions on the selected disk
    MbrFix /drive  readsignature {/byte}  Read disk signature from MBR
    MbrFix /drive  writesignature    Write disk signature to MBR
    MbrFix /drive  generatesignature      Generate disk signature in MBR
    MbrFix /drive  readstate              Read state from byte 0x1b2 in MBR
    MbrFix /drive  writestate      Write state to byte 0x1b2 in MBR
    MbrFix /drive  readdrive   
                                               Save sectors from drive to file
    MbrFix /drive  /partition  fixbootsector      
                                               Update Boot code in boot sector
    MbrFix /drive  /partition  getpartitiontype 
                                               Get partition type
    MbrFix /drive  /partition  setpartitiontype      
                                               Set partition type
    MbrFix /drive  /partition  setactivepartition 
                                               Set active partition
    MbrFix /drive  getactivepartition     Get active partition
    MbrFix volumeinformation driveletter       Get volume information for partition
    MbrFix flush {driveletter(s)}              Flush files to disk for partition
    MbrFix listpartitiontypes                  List partition types
Drive numbering starts on 0.
Partition numbering starts on 1.

Commands restorembr, fixmbr, generatesignature, writestate, clean, setactivepartition and fixbootsector will ask for confirmation unless /yes is included. All these commands may lead to data loss if used incorrectly or without intention.
If the /byte option is given for the readsignature command, the signature is returned as a byte array instead of as a DWORD. Writesignature requires the new signature to be specified as 8 hex digits.
If /vista is used with the fixmbr command, a Windows Vista MBR is written, if /win7 is used, a Windows 7 MBR is written instead of the default Win2000/XP/2003 MBR. The Vista or Win7 MBR should work with the other OS'es.
When fixing boot sector of a chosen partition, the wanted OS version must be specified. For now, the only valid choices are WIN98, DOS5 and DOS6.
The purpose of the fixbootsector command is to insert boot code for loading DOS from a partition created and formatted as FAT using the Windows 2000/XP/2003 tools, since those tools inserts boot code for loading NTLDR.

Need of this utilityWell, I was testing Windows Embedded XP using a virtual PC, and was booting Windows PE from a CD-ROM to partition a brand new harddisk (quite cheap, since it was virtual !). Using the diskpart.exe utility (found in Windows XP and Windows Server 2003, available as a download for Windows 2000), I created the partition, made it Active, assigned it a drive letter. I then used the format.exe utility to format it to NTFS (format C: /fs:ntfs).
DISKPART> detail part
Partition 1
Type: 07
Hidden: No
Active: Yes

 Volume ###  Letter  Lable       Fs     Type        Size     Status     Info.
 ----------  ---  -----------  -----  ----------  -------   ---------  --------
* Volume 1     C    WinXP        NTFS   Partition    149 MB   OK        System            
        
I then mounted a network drive to my physical computer and copied the boot.ini, ntldr, ntdetect.com, windows folder, program files folder, etc. from the Windows Embedded XP image directory. Everything to make it boot.
Well everything should be OK, so I rebooted the (virtual) computer and expected it to boot up Windows Embedded XP.
But not so! It just sat there with a nice black screen. No error message at all.
Some investigation later on the Internet, I found this very informative page by Daniel B. Sedory: http://thestarman.narod.ru/asm/mbr/index.html, containing a wealth of MBR info. The info on one of the pages states that the MBR code is installed on the drive when the Windows setup program runs and no MBR code exists already. Hmm.., seemed to be right on the mark.
I searched further on the Internet, but the only utilities I found which could install MBR code was for other operating systems, like FDISK.exe /MBR for DOS (but I wouldn't boot to DOS...) or the fixmbr command in the Windows 2000/XP/2003 Recovery Console. Didn't want that either. Wanted something to work on a Windows 32 bit platform, to be able to fix the problem in other cases when using Win PE and just plain copying in the operating system files. So, then I was left to make the utility myself!
Problem solved:
After booting up Windows PE from CD again, the command  
MBRFix /drive 0 fixmbr /yes

solved it all. Embedded XP booted like a charm.
Samples
C:\> MbrFix /drive 0 listpartitions
# Boot Size (MB) Type
1  Yes     151001    6  DOS 3.31+ 16-bit FAT (over 32M)
2          1623   12  WIN95 OSR2 32-bit FAT, LBA-mapped
3             0    0  None
4             0    0  None
C:\> MbrFix /drive 0 savembr Backup_MBR_0.bin
C:\> MbrFix /drive 0 restorembr Backup_MBR_0.bin
You are about to Restore MBR,
are you sure (Y/N)? Y
C:\> MbrFix /drive 0 fixmbr /yes
C:\> MbrFix /drive 0 fixmbr /vista /yes
C:\> MbrFix /drive 0 /partition 2 fixbootsector WIN98 /yes
C:\> MbrFix /drive 0 /partition 2 setpartitiontype 14 /yes
C:\> MbrFix flush            (flush all drives)
C:\> MbrFix flush cde        (flushes C:, D: and E:)
C:\> mbrfix readsignature
C:\> mbrfix writesignature 473d4f07
Readstate and Writestate:
The commands readstate and writestate use a byte at offset 0x1b2 to store state. This was changed from offset 0x1b0 used in earlier versions to be compatible with Windows Vista MBR. These commands are only useful in a scenario where you use Windows PE to roll out other MS operating systems, and you have no other place to store information about where you are in the installation process. This location in the MBR is unused as far as known, from the days of DOS 3.30 until Vista.
Added support for Windows 7 MBR.

No comments: