Boot sector
A boot sector is the sector of a persistent data storage device which contains machine code to be loaded into random-access memory and then executed by a computer system's built-in firmware.
Usually, the first sector of the hard disk is the boot sector, regardless of sector size and partitioning flavor.
The purpose of defining one particular sector as the boot sector is inter-operability between firmware and various operating systems.
The purpose of chain-loading, first firmware, then code in the boot sector, and then, for example, an operating system, is maximal flexibility.
The IBM PC and compatible computers
On an IBM PC compatible machine, the BIOS selects a boot device, then copies the first sector from the device, into physical memory at memory address 0x7C00. On other systems, the process may be quite different.Unified Extensible Firmware Interface (UEFI)
The UEFI does not rely on boot sectors, UEFI system loads the boot loader directly. Additionally, the UEFI specification also contains "secure boot", which basically wants the UEFI code to be digitally signed.Damage to the boot sector
In case a boot sector receives physical damage, the hard disk will no longer be bootable, unless used with a custom BIOS that defines a non-damaged sector as the boot sector. However, since the very first sector additionally contains data regarding the partitioning of the hard disk, the hard disk will become entirely unusable except when used in conjunction with custom software.Partition tables
A disk can be partitioned into multiple partitions and, on conventional systems, it is expected to be. There are two definitions on how to store the information regarding the partitioning:- A master boot record is the first sector of a data storage device that has been partitioned. The MBR sector may contain code to locate the active partition and invoke its volume boot record.
- A volume boot record is the first sector of a data storage device that has not been partitioned, or the first sector of an individual partition on a data storage device that has been partitioned. It may contain code to load an operating system installed on that device or within that partition.
The signature is checked for by most system BIOSes since the IBM PC/AT. Even more so, it is also checked by most MBR boot loaders before passing control to the boot sector. Some BIOSes perform the check only for fixed disk/removable drives, while for floppies and superfloppies, it is enough to start with a byte greater or equal to 06h and the first nine words not to contain the same value, before the boot sector is accepted as valid, thereby avoiding the explicit test for 0x55, 0xAA on floppies. Since old boot sectors sometimes do not feature this signature despite the fact that they can be booted successfully, the check can be disabled in some environments.
If the BIOS or MBR code does not detect a valid boot sector and therefore cannot pass execution to the boot sector code, it will try the next boot device in the row. If they all fail it will typically display an error message and invoke INT 18h. This will either start up optional resident software in ROM, reboot the system via INT 19h after user confirmation or cause the system to halt the bootstrapping process until the next power-up.
Systems not following the above described design are:
- CD-ROMs usually have their own structure of boot sectors; for IBM PC compatible systems this is subject to El Torito specifications.
- C128 or C64 software on Commodore DOS disks where data on Track 1, Sector 0 began with a magic number corresponding to string "CBM".
- IBM mainframe computers place a small amount of boot code in the first and second track of the first cylinder of the disk, and the root directory, called the Volume Table of Contents, is also located at the fixed location of the third track of the first cylinder of the disk.
- Other PC systems may have different boot sector formats on their disk devices.
Operation
Furthermore, whatever is stored in the first sector of a floppy diskette, USB device, hard disk or any other bootable storage device, is not required to immediately load any bootstrap code for an OS, if ever. The BIOS merely passes control to whatever exists there, as long as the sector meets the very simple qualification of having the boot record signature of 0x55, 0xAA in its last two bytes. This is why it is easy to replace the usual bootstrap code found in an MBR with more complex loaders, even large multi-functional boot managers, allowing users a number of choices in what occurs next. With this kind of freedom, abuse often occurs in the form of boot sector viruses.
Boot-sector viruses
Since code in the boot sector is executed automatically, boot sectors have historically been a common attack vector for computer viruses.To combat this behavior, the system BIOS often includes an option to prevent software from writing to the first sector of any attached hard drives; it could thereby protect the master boot record containing the partition table from being overwritten accidentally, but not the volume boot records in the bootable partitions. Depending on the BIOS, attempts to write to the protected sector may be blocked with or without user interaction. Most BIOSes, however, will display a popup message giving the user a chance to override the setting.
The BIOS option is disabled by default because the message may not be displayed correctly in graphics mode and blocking access to the MBR may cause problems with operating system setup programs or disk access, encryption or partitioning tools like FDISK, which may not have been written to be aware of that possibility, causing them to abort ungracefully and possibly leaving the disk partitioning in an inconsistent state.
As an example, the malware NotPetya attempts to gain administrative privileges on an operating system, and then would attempt to overwrite the boot sector of a computer. The CIA has also developed malware that attempts to modify the boot sector in order to load additional drivers to be used by other malware.
Another Malware that overwrites boot sector is the MEMZ.