Difference between revisions of "Mbr"

From Syslinux Wiki
Jump to: navigation, search
m (Added a link to article Master_Boot_Record)
(Wiki formatting. Minor re-wording. Add internal links. Minor correction.)
Line 1: Line 1:
 
== MBR (Master Boot Record) ==
 
== MBR (Master Boot Record) ==
  
The task of [[Master_Boot_Record|MBR (Master Boot Record)]] is to load boot code from an active partition of a drive.
+
The task of the MBR ([[Master Boot Record]]) is  
 +
to load boot code from an active partition of a drive.
  
Syslinux distribution comes with several versions of mbr code. These are '''mbr.bin''', '''altmbr.bin''' and '''gptmbr.bin''',
+
Syslinux is distributed with several variants of mbr code.  
each in 3 different flavours, -- regular, and with suffixes '''_c''' and '''_f'''.
+
These are <tt>mbr.bin</tt>, <tt>altmbr.bin</tt> and <tt>gptmbr.bin</tt>,  
 +
each in 3 different flavours: regular,  
 +
and with suffixes <tt>_c</tt> and <tt>_f</tt>.
  
'''mbr.bin''' is a regular boot code for msdos-like partition table.
+
<tt>mbr.bin</tt> is a regular boot code for msdos-like partition tables.
  
'''altmbr.bin''' is also for msdos-style partition table, but it boots from a partition with fixed number, see below.
+
<tt>altmbr.bin</tt> is also for msdos-style partition tables,  
 +
but it boots from a partition with a given fixed number,  
 +
ignoring the "boot" flag -- see below.
  
'''gptmbr.bin''' is a boot code that can be used on a drive with GPT partition table.
+
<tt>gptmbr.bin</tt> is a boot code that can be used on a drive with a GPT  
 +
partition scheme.
  
  
The different flavours differs in the way how they determine boot drive.
+
The different flavours differ in the way they determine boot drive.  
The regular code uses information about boot drive from the BIOS (passed in DL register).  In some cases, this information may be somehow broken or incorrect, and for this case there's '''_c''' version, which checks if '''Ctrl''' key is held during boot and if it is, the code boots from BIOS drive 0x80, or else it uses regular way.  And the '''_f''' version which always boots from BIOS drive 0x80.
+
The regular code uses information about the boot drive from the  
 +
BIOS (passed in the DL register).
  
In all cases the mbr code is 440 bytes long and can be written into start of a drive (with appropriate partition table) to make it bootable.  MS-DOS style partition table is located in the first sector of the drive as well, right _after_ the mbr code (starting with 440 byte).
+
In some cases, this information may be somehow broken or incorrect,  
 +
and for such cases (among others) there are "flavours" available.
  
The altmbr.bin (all 3 variants) is only 439 bytes long, -- one extra byte should be set to the number of partition to boot from.
+
The <tt>_c</tt> flavour checks if the <code>Ctrl</code> key is held
 +
during boot, and, if it is,
 +
then the code boots from BIOS drive {{nowrap|0x80}},
 +
otherwise it uses the regular way.
 +
 
 +
The <tt>_f</tt> flavour always boots from BIOS drive 0x80.
 +
 
 +
See also [[Isohybrid#MBR_selection]].
 +
 
 +
In all cases, the mbr code is 440 bytes long and can be written into the
 +
start of a drive (with an appropriate partition table) to make it bootable.
 +
The msdos style partition table is located in the first sector
 +
of the drive as well, right _after_ the mbr code (starting with an
 +
offset of 446 bytes).
 +
 
 +
All 3 flavours of the <tt>altmbr.bin</tt> variant are only 439  
 +
bytes long; one extra byte should be set to  
 +
the partition number to boot from.
  
 
== Write ==
 
== Write ==
Some people may recommend redirecting cat's output directly to the disk however this method carries a lot of danger and potentially dire consequences (data loss).  A safe approach is:
+
Some people may recommend redirecting cat's output directly to the disk;
 +
however, this method carries a lot of danger and potentially dire  
 +
consequences (data loss).  A safe approach is:
  
 
   dd bs=440 count=1 conv=notrunc if=mbr/mbr.bin of=/dev/sda
 
   dd bs=440 count=1 conv=notrunc if=mbr/mbr.bin of=/dev/sda
  
This sets the input and output transfer block to 440 bytes, 1 block, and prevents the output from truncating the file (if the output is not a block device, like an image file).
+
This sets the input and output transfer block to 440 bytes, 1 block,  
 +
and prevents the output from truncating the file (if the output is not a  
 +
block device, like an image file).
  
An easy way to set the desired boot partition to 1 with altmbr.bin is:
+
An easy way to set the desired boot partition to "1" with <tt>altmbr.bin</tt> is:
 
   printf '\1' | cat altmbr.bin - | dd bs=440 count=1 iflag=fullblock conv=notrunc of=/dev/sda
 
   printf '\1' | cat altmbr.bin - | dd bs=440 count=1 iflag=fullblock conv=notrunc of=/dev/sda
  
Line 33: Line 62:
 
   printf '\027'
 
   printf '\027'
 
   printf '\x17'
 
   printf '\x17'
 +
<!--  -->
 +
<small>About printf: <!--  (in Linux shell)...    -->
 +
* The "hex" syntax format is preferred.
 +
* Reminder: do _not_ use leading zeros:
 +
printf '\x9' is <font color=green>OK</font>
 +
printf '\x09' is <font color=red>NOT</font>
 +
</small>
 +
<!--  -->

Revision as of 17:42, 27 October 2018

MBR (Master Boot Record)

The task of the MBR (Master Boot Record) is to load boot code from an active partition of a drive.

Syslinux is distributed with several variants of mbr code. These are mbr.bin, altmbr.bin and gptmbr.bin, each in 3 different flavours: regular, and with suffixes _c and _f.

mbr.bin is a regular boot code for msdos-like partition tables.

altmbr.bin is also for msdos-style partition tables, but it boots from a partition with a given fixed number, ignoring the "boot" flag -- see below.

gptmbr.bin is a boot code that can be used on a drive with a GPT partition scheme.


The different flavours differ in the way they determine boot drive. The regular code uses information about the boot drive from the BIOS (passed in the DL register).

In some cases, this information may be somehow broken or incorrect, and for such cases (among others) there are "flavours" available.

The _c flavour checks if the Ctrl key is held during boot, and, if it is, then the code boots from BIOS drive 0x80, otherwise it uses the regular way.

The _f flavour always boots from BIOS drive 0x80.

See also Isohybrid#MBR_selection.

In all cases, the mbr code is 440 bytes long and can be written into the start of a drive (with an appropriate partition table) to make it bootable. The msdos style partition table is located in the first sector of the drive as well, right _after_ the mbr code (starting with an offset of 446 bytes).

All 3 flavours of the altmbr.bin variant are only 439 bytes long; one extra byte should be set to the partition number to boot from.

Write

Some people may recommend redirecting cat's output directly to the disk; however, this method carries a lot of danger and potentially dire consequences (data loss). A safe approach is:

 dd bs=440 count=1 conv=notrunc if=mbr/mbr.bin of=/dev/sda

This sets the input and output transfer block to 440 bytes, 1 block, and prevents the output from truncating the file (if the output is not a block device, like an image file).

An easy way to set the desired boot partition to "1" with altmbr.bin is:

 printf '\1' | cat altmbr.bin - | dd bs=440 count=1 iflag=fullblock conv=notrunc of=/dev/sda

Setting it to 23 (17h, 27o) is as simple as these substitutions:

 printf '\027'
 printf '\x17'

About printf:

  • The "hex" syntax format is preferred.
  • Reminder: do _not_ use leading zeros:
printf '\x9' is OK
printf '\x09' is NOT