pub struct Mbr {
pub bootstrap_code: [u8; 440],
pub disk_signature: [u8; 4],
pub reserved: [u8; 2],
pub partitions: [PartitionEntry; 4],
pub boot_signature: [u8; 2],
}
Expand description
Master Boot Record structure (512 bytes)
Fields§
§bootstrap_code: [u8; 440]
Bootstrap code (440 bytes)
disk_signature: [u8; 4]
Optional disk signature (4 bytes)
reserved: [u8; 2]
Reserved (usually 0x0000)
partitions: [PartitionEntry; 4]
Partition table (4 entries × 16 bytes = 64 bytes)
boot_signature: [u8; 2]
Boot signature (0x55AA)
Implementations§
Source§impl Mbr
impl Mbr
Sourcepub const MAXIMUM_PARTITIONS_COUNT: usize = 4usize
pub const MAXIMUM_PARTITIONS_COUNT: usize = 4usize
Maximum number of primary partitions in MBR
Sourcepub fn new_with_signature(disk_signature: u32) -> Self
pub fn new_with_signature(disk_signature: u32) -> Self
Create a new MBR with a specific disk signature
Sourcepub fn from_bytes(data: &[u8]) -> Result<Self>
pub fn from_bytes(data: &[u8]) -> Result<Self>
Parse MBR from raw bytes
Sourcepub fn read_from_device(device: &Device) -> Result<Self>
pub fn read_from_device(device: &Device) -> Result<Self>
Read and parse MBR from a device
Sourcepub fn write_to_device(&self, device: &Device) -> Result<()>
pub fn write_to_device(&self, device: &Device) -> Result<()>
Write MBR to a device
Sourcepub fn get_valid_partitions(&self) -> Vec<&PartitionEntry>
pub fn get_valid_partitions(&self) -> Vec<&PartitionEntry>
Get all valid partitions
Sourcepub fn get_valid_partitions_mut(&mut self) -> Vec<&mut PartitionEntry>
pub fn get_valid_partitions_mut(&mut self) -> Vec<&mut PartitionEntry>
Get all valid partitions (mutable)
Sourcepub fn get_bootable_partition(&self) -> Option<&PartitionEntry>
pub fn get_bootable_partition(&self) -> Option<&PartitionEntry>
Get bootable partition (if any)
Sourcepub fn get_bootable_partition_mut(&mut self) -> Option<&mut PartitionEntry>
pub fn get_bootable_partition_mut(&mut self) -> Option<&mut PartitionEntry>
Get bootable partition (mutable, if any)
Sourcepub fn set_bootable_partition(&mut self, index: usize) -> Result<()>
pub fn set_bootable_partition(&mut self, index: usize) -> Result<()>
Set a partition as bootable (clears bootable flag from other partitions)
Sourcepub fn has_gpt_protective_partition(&self) -> bool
pub fn has_gpt_protective_partition(&self) -> bool
Check if this MBR contains a GPT protective partition
Sourcepub fn get_disk_signature(&self) -> u32
pub fn get_disk_signature(&self) -> u32
Get disk signature as u32
Sourcepub fn set_disk_signature(&mut self, signature: u32)
pub fn set_disk_signature(&mut self, signature: u32)
Set disk signature
Sourcepub fn get_free_partition_slot(&self) -> Option<usize>
pub fn get_free_partition_slot(&self) -> Option<usize>
Get the first free partition slot
Sourcepub fn add_partition(
&mut self,
partition_type: PartitionKind,
start_lba: u32,
size_sectors: u32,
bootable: bool,
) -> Result<usize>
pub fn add_partition( &mut self, partition_type: PartitionKind, start_lba: u32, size_sectors: u32, bootable: bool, ) -> Result<usize>
Add a new partition
Sourcepub fn remove_partition(&mut self, index: usize) -> Result<()>
pub fn remove_partition(&mut self, index: usize) -> Result<()>
Remove a partition by index
Sourcepub fn has_overlapping_partitions(&self) -> bool
pub fn has_overlapping_partitions(&self) -> bool
Check for partition overlaps
Sourcepub fn get_partition_count(&self) -> usize
pub fn get_partition_count(&self) -> usize
Get partition count
Sourcepub fn create_all_partition_devices(
&self,
base_device: Device,
) -> Result<Vec<PartitionDevice>>
pub fn create_all_partition_devices( &self, base_device: Device, ) -> Result<Vec<PartitionDevice>>
Create partition devices for all valid partitions in this MBR.
This method iterates through all partition entries in this MBR and creates
PartitionDevice
instances for each valid partition. This is useful
when you need to access all partitions on a disk programmatically.
§Arguments
base_device
- The underlying storage device containing all partitions
§Returns
Ok(Vec<Partition_device_type>)
- Vector of partition devices for all valid partitionsErr(Error)
- Error if any partition device creation fails
§Examples
extern crate alloc;
use file_system::*;
let device = create_device!(Memory_device_type::<512>::new(4 * 1024 * 1024));
// Create an MBR with multiple partitions
let mut mbr = MBR_type::New_with_signature(0x12345678);
mbr.Add_partition(Partition_type_type::Fat32_lba, 2048, 1024, true).unwrap();
mbr.Add_partition(Partition_type_type::Linux, 4096, 2048, false).unwrap();
mbr.Write_to_device(&device).unwrap();
// Read it back and create all partition devices
let mbr = MBR_type::Read_from_device(&device).unwrap();
let partition_devices = mbr.Create_all_partition_devices(device).unwrap();
println!("Created {} partition devices", partition_devices.len());
for (i, partition) in partition_devices.iter().enumerate() {
println!("Partition {}: {} sectors", i, partition.get_sector_count());
}
Sourcepub fn find_partitions_by_type(
&self,
partition_type: PartitionKind,
) -> Vec<(usize, &PartitionEntry)>
pub fn find_partitions_by_type( &self, partition_type: PartitionKind, ) -> Vec<(usize, &PartitionEntry)>
Find partitions of a specific type within this MBR.
This method searches through all partitions in this MBR and returns references to those that match the specified partition type. This is useful for locating specific types of partitions (e.g., FAT32, Linux, etc.) without creating partition devices.
§Arguments
partition_type
- The specific partition type to find
§Returns
A vector of tuples containing the partition index and reference to the partition entry for each matching partition.
§Examples
extern crate alloc;
use file_system::*;
let device = create_device!(Memory_device_type::<512>::new(4 * 1024 * 1024));
// Create an MBR with FAT32 partition
let mut mbr = MBR_type::New_with_signature(0x12345678);
mbr.Add_partition(Partition_type_type::Fat32_lba, 2048, 1024, true).unwrap();
mbr.Write_to_device(&device).unwrap();
// Read it back and find FAT32 partitions
let mbr = MBR_type::Read_from_device(&device).unwrap();
let fat32_partitions = mbr.Find_partitions_by_type(Partition_type_type::Fat32_lba);
println!("Found {} FAT32 partitions", fat32_partitions.len());
Sourcepub fn validate(&self) -> Result<()>
pub fn validate(&self) -> Result<()>
Validate this MBR structure for consistency and correctness.
This method performs comprehensive validation of this MBR structure, checking:
- MBR signature validity (0x55AA boot signature)
- Partition overlap detection
- Bootable partition count (at most one partition should be bootable)
§Returns
Ok(())
- MBR is valid and consistentErr(Error::Corrupted)
- MBR is invalid or corrupted
§Examples
extern crate alloc;
use file_system::*;
let device = create_device!(Memory_device_type::<512>::new(4 * 1024 * 1024));
// First create and write a valid MBR
let mut mbr = MBR_type::New_with_signature(0x12345678);
mbr.Add_partition(Partition_type_type::Fat32_lba, 2048, 1024, true).unwrap();
mbr.Write_to_device(&device).unwrap();
// Read it back and validate
let mbr = MBR_type::Read_from_device(&device).unwrap();
match mbr.Validate() {
Ok(()) => println!("MBR is valid"),
Err(Error::Corrupted) => println!("MBR is corrupted"),
Err(e) => println!("Validation error: {}", e),
}
Sourcepub fn generate_statistics(&self) -> PartitionStatistics
pub fn generate_statistics(&self) -> PartitionStatistics
Generate comprehensive statistics from this MBR.
This method analyzes all partitions in this MBR and generates detailed statistics about partition types, sizes, and other characteristics.
§Returns
A new Partition_statistics_type
containing the computed statistics.
§Examples
extern crate alloc;
use file_system::*;
let device = create_device!(Memory_device_type::<512>::new(4 * 1024 * 1024));
// Create an MBR with some partitions
let mut mbr = MBR_type::New_with_signature(0x12345678);
mbr.Add_partition(Partition_type_type::Fat32_lba, 2048, 1024, true).unwrap();
mbr.Add_partition(Partition_type_type::Linux, 4096, 2048, false).unwrap();
mbr.Write_to_device(&device).unwrap();
// Read it back and analyze
let mbr = MBR_type::Read_from_device(&device).unwrap();
let stats = mbr.Generate_statistics();
if stats.Total_partitions > 0 {
println!("Average partition size: {} sectors",
stats.Total_used_sectors / stats.Total_partitions as u64);
}
Sourcepub fn create_basic(
disk_signature: u32,
partition_type: PartitionKind,
total_sectors: u32,
) -> Result<Self>
pub fn create_basic( disk_signature: u32, partition_type: PartitionKind, total_sectors: u32, ) -> Result<Self>
Create a basic MBR with a single partition covering most of the disk.
This function creates a simple MBR structure with one partition that uses most of the available disk space. It leaves 2048 sectors at the beginning for proper alignment, which is standard practice for modern storage devices.
§Arguments
disk_signature
- Unique 32-bit signature for the diskpartition_type
- Type of partition to create (e.g., FAT32, Linux, etc.)total_sectors
- Total number of sectors available on the disk
§Returns
Ok(MBR_type)
- Successfully created MBR with single partitionErr(Error::InvalidParameter)
- Disk is too small for a partition
§Examples
use file_system::*;
// Create MBR for a 4MB device (8192 sectors)
let mbr = MBR_type::Create_basic(0x12345678, Partition_type_type::Fat32_lba, 8192).unwrap();
// The MBR will have one FAT32 partition starting at sector 2048
let partitions = mbr.get_valid_partitions();
assert_eq!(partitions.len(), 1);
assert_eq!(partitions[0].get_start_lba(), 2048);
Sourcepub fn find_or_create_partition_with_signature(
device: &Device,
target_signature: u32,
partition_type: PartitionKind,
) -> Result<PartitionDevice>
pub fn find_or_create_partition_with_signature( device: &Device, target_signature: u32, partition_type: PartitionKind, ) -> Result<PartitionDevice>
Find or create a partition with a specific disk signature.
This function searches for an MBR with the specified disk signature on the device. If found, it returns a partition device for the first valid partition. If not found, or if no valid partitions exist, it formats the disk with a new MBR containing a single partition that occupies the full disk space.
§Arguments
device
- The storage device to search or formattarget_signature
- The disk signature to look forpartition_type
- The type of partition to create if formatting is needed
§Returns
Ok(Partition_device_type)
- Partition device for the found or created partitionErr(Error)
- Error if operation failed
§Examples
extern crate alloc;
use file_system::*;
let device = create_device!(Memory_device_type::<512>::new(4 * 1024 * 1024));
// Look for partition with signature 0x12345678, create FAT32 partition if not found
let partition = MBR_type::Find_or_create_partition_with_signature(
&device,
0x12345678,
Partition_type_type::Fat32_lba
).unwrap();
// The partition device is ready to use
assert!(partition.is_valid());
Sourcepub fn format_disk_with_signature_and_partition(
device: &Device,
disk_signature: u32,
partition_type: PartitionKind,
) -> Result<PartitionDevice>
pub fn format_disk_with_signature_and_partition( device: &Device, disk_signature: u32, partition_type: PartitionKind, ) -> Result<PartitionDevice>
Format a disk with a specific signature and create a single partition.
This function creates a new MBR with the specified disk signature and a single partition that occupies most of the available disk space, leaving standard alignment space at the beginning.
§Arguments
device
- The storage device to formatdisk_signature
- The disk signature to set in the new MBRpartition_type
- The type of partition to create
§Returns
Ok(Partition_device_type)
- Partition device for the created partitionErr(Error)
- Error if operation failed
§Examples
extern crate alloc;
use file_system::*;
let device = create_device!(Memory_device_type::<512>::new(4 * 1024 * 1024));
// Format disk and create a Linux partition with specific signature
let partition = MBR_type::Format_disk_with_signature_and_partition(
&device,
0xABCDEF00,
Partition_type_type::Linux
).unwrap();
// Verify the partition was created correctly
assert!(partition.is_valid());
assert_eq!(partition.get_start_lba(), 2048); // Standard alignment