74 lines
1.3 KiB
C++
74 lines
1.3 KiB
C++
#pragma once
|
|
|
|
template <typename BlockDev> class MBR {
|
|
|
|
BlockDev& dev;
|
|
bool present = false;
|
|
bool valid = false;
|
|
|
|
/** describes one partition */
|
|
class Partition {
|
|
|
|
friend class MBR;
|
|
|
|
uint8_t type;
|
|
uint32_t firstSector;
|
|
uint32_t numSectors;
|
|
|
|
public:
|
|
|
|
bool isPresent() const {return type != 0;}
|
|
uint8_t getType() const {return type;}
|
|
uint32_t getFirstSector() const {return firstSector;}
|
|
uint32_t getNumSectors() const {return numSectors;}
|
|
|
|
};
|
|
|
|
Partition partitions[4];
|
|
|
|
public:
|
|
|
|
MBR(BlockDev& dev) : dev(dev) {
|
|
read();
|
|
}
|
|
|
|
bool isPresent() const {return present;}
|
|
bool isValid() const {return valid;}
|
|
|
|
Partition& getPartition(uint8_t idx) {
|
|
return partitions[idx];
|
|
}
|
|
|
|
private:
|
|
|
|
void read() {
|
|
|
|
uint8_t buf[512];
|
|
dev.read(0x00000000, 512, buf);
|
|
present = (buf[510] == 0x55) && (buf[511] == 0xAA);
|
|
valid = (buf[0x01BC] == 0x00) && (buf[0x01BD] == 0x00);
|
|
|
|
if (present && valid) {
|
|
|
|
// parse all 4 partitions
|
|
for (uint8_t i = 0; i < 4; ++i) {
|
|
|
|
const uint8_t* src = &buf[0x1BE + i*16];
|
|
Partition* p = &partitions[i];
|
|
|
|
p->type = src[0x4];
|
|
p->firstSector = getU32(src, 8);
|
|
p->numSectors = getU32(src, 12);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
uint32_t getU32(const uint8_t* src, const uint8_t offset) {
|
|
return src[offset+0]<<0 | src[offset+1]<<8 | src[offset+2]<<16 | src[offset+3]<<24;
|
|
}
|
|
|
|
};
|