Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <iostream>
- #include <ostream>
- namespace
- {
- constexpr int BLOCK_SIZE = 512; // Not sure if MBR is aware of advanced format
- // https://en.wikipedia.org/wiki/Master_boot_record#Sector_layout
- #pragma pack(1)
- struct PartitionRecord
- {
- uint8_t m_status;
- uint8_t m_chs[3];
- uint8_t m_type; //4
- uint8_t m_kakoe_to_govno[7];
- uint32_t m_sectors;
- uint64_t Megabytes() const
- {
- return (static_cast<uint64_t>(m_sectors) * BLOCK_SIZE) >> 20;
- }
- };
- void ReportPartitionRecord(const uint8_t* data)
- {
- const auto* p = reinterpret_cast<const PartitionRecord*>(data);
- switch (p->m_status)
- {
- case 0x80:
- std::cout << "Active";
- break;
- case 0x0:
- std::cout << "Inactive";
- break;
- default:
- std::cout << "Invalid";
- break;
- }
- // See type: https://en.wikipedia.org/wiki/Partition_type
- // 0 means "free"
- // 0xef is service efi etc
- std::cout << " type: " << std::hex << static_cast<int>(p->m_type);
- std::cout << " sectors: " << std::dec << static_cast<int>(p->m_sectors);
- std::cout << " megabytes: " << p->Megabytes();
- }
- void ReportMbr(const char* path)
- {
- std::cout << path << std::endl;
- uint8_t mbr[512];
- std::ifstream f(path, std::ios::in | std::ios::binary);
- f.read(reinterpret_cast<char*>(mbr), sizeof(mbr));
- // Must have 55:aa here, see https://en.wikipedia.org/wiki/Master_boot_record
- std::cout << "Signature: ";
- const auto* signature = reinterpret_cast<const uint16_t*>(&mbr[510]);
- std::cout << std::hex << *signature;
- if (*signature != 0xAA55)
- {
- std::cout << "Ne mbr, a govno";
- return;
- }
- std::cout << std::endl;
- for (unsigned int i = 0; i < 4; i++)
- {
- std::cout << "Partition " << i << ": ";
- // https://en.wikipedia.org/wiki/Master_boot_record
- // First Partition record starts at 446, and occupies 16 bytes.
- // There are 4 records
- ReportPartitionRecord(&mbr[446 + i * 16]);
- std::cout << std::endl;
- }
- }
- }
- int main()
- {
- ReportMbr("\\\\.\\PhysicalDrive0");
- ReportMbr("c:\\temp\\CentOS-8.2.2004-x86_64-dvd1.iso");
- ReportMbr("c:\\temp\\slackware64-current-install-dvd.iso");
- ReportMbr("C:\\temp\\Windows 7 with SP1 U Russian\\ru_windows_7_professional_with_sp1_x64_dvd_u_677024.iso");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement