2023-05-26 21:28:51 +00:00
|
|
|
#include "fs/MBR.h"
|
|
|
|
#include "Log.h"
|
|
|
|
|
|
|
|
namespace MBR
|
|
|
|
{
|
2023-06-04 09:25:16 +00:00
|
|
|
Result<bool> identify(SharedPtr<Device> device)
|
2023-05-26 21:28:51 +00:00
|
|
|
{
|
2023-06-04 09:25:16 +00:00
|
|
|
// Cannot read a partition table from a character device! Who is even coming up with this silliness?
|
|
|
|
if (!device->is_block_device()) return false;
|
2023-05-26 21:28:51 +00:00
|
|
|
|
|
|
|
DiskHeader hdr;
|
2023-06-04 09:25:16 +00:00
|
|
|
usize nread = TRY(device->read((u8*)&hdr, 0, sizeof(hdr)));
|
2023-05-26 21:28:51 +00:00
|
|
|
check(nread == 512);
|
|
|
|
|
|
|
|
if (hdr.signature[0] != MBR_SIGNATURE_1 || hdr.signature[1] != MBR_SIGNATURE_2) return false;
|
|
|
|
|
|
|
|
for (int i = 0; i < 4; i++)
|
|
|
|
{
|
|
|
|
const auto& part = hdr.partitions[i];
|
|
|
|
if (part.partition_type == 0) continue; // Not active.
|
|
|
|
|
|
|
|
bool bootable = part.attributes & MBR_BOOTABLE;
|
|
|
|
kinfoln("mbr: Partition #%d is active: bootable=%d, type=%x, start=%d, sectors=%d", i, bootable,
|
|
|
|
part.partition_type, part.start_lba, part.num_sectors);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|