Advertisement
Guest User

mbr

a guest
Dec 19th, 2020
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. #include <fstream>
  2. #include <iostream>
  3. #include <ostream>
  4.  
  5. namespace
  6. {
  7. constexpr int BLOCK_SIZE = 512; // Not sure if MBR is aware of advanced format
  8. // https://en.wikipedia.org/wiki/Master_boot_record#Sector_layout
  9. #pragma pack(1)
  10. struct PartitionRecord
  11. {
  12. uint8_t m_status;
  13. uint8_t m_chs[3];
  14. uint8_t m_type; //4
  15. uint8_t m_kakoe_to_govno[7];
  16. uint32_t m_sectors;
  17.  
  18. uint64_t Megabytes() const
  19. {
  20. return (static_cast<uint64_t>(m_sectors) * BLOCK_SIZE) >> 20;
  21. }
  22. };
  23.  
  24. void ReportPartitionRecord(const uint8_t* data)
  25. {
  26. const auto* p = reinterpret_cast<const PartitionRecord*>(data);
  27. switch (p->m_status)
  28. {
  29. case 0x80:
  30. std::cout << "Active";
  31. break;
  32. case 0x0:
  33. std::cout << "Inactive";
  34. break;
  35. default:
  36. std::cout << "Invalid";
  37. break;
  38. }
  39. // See type: https://en.wikipedia.org/wiki/Partition_type
  40. // 0 means "free"
  41. // 0xef is service efi etc
  42. std::cout << " type: " << std::hex << static_cast<int>(p->m_type);
  43. std::cout << " sectors: " << std::dec << static_cast<int>(p->m_sectors);
  44. std::cout << " megabytes: " << p->Megabytes();
  45. }
  46.  
  47.  
  48. void ReportMbr(const char* path)
  49. {
  50. std::cout << path << std::endl;
  51. uint8_t mbr[512];
  52. std::ifstream f(path, std::ios::in | std::ios::binary);
  53. f.read(reinterpret_cast<char*>(mbr), sizeof(mbr));
  54. // Must have 55:aa here, see https://en.wikipedia.org/wiki/Master_boot_record
  55. std::cout << "Signature: ";
  56. const auto* signature = reinterpret_cast<const uint16_t*>(&mbr[510]);
  57. std::cout << std::hex << *signature;
  58. if (*signature != 0xAA55)
  59. {
  60. std::cout << "Ne mbr, a govno";
  61. return;
  62. }
  63.  
  64.  
  65. std::cout << std::endl;
  66. for (unsigned int i = 0; i < 4; i++)
  67. {
  68. std::cout << "Partition " << i << ": ";
  69. // https://en.wikipedia.org/wiki/Master_boot_record
  70. // First Partition record starts at 446, and occupies 16 bytes.
  71. // There are 4 records
  72. ReportPartitionRecord(&mbr[446 + i * 16]);
  73. std::cout << std::endl;
  74. }
  75. }
  76. }
  77.  
  78. int main()
  79. {
  80. ReportMbr("\\\\.\\PhysicalDrive0");
  81. ReportMbr("c:\\temp\\CentOS-8.2.2004-x86_64-dvd1.iso");
  82. ReportMbr("c:\\temp\\slackware64-current-install-dvd.iso");
  83. ReportMbr("C:\\temp\\Windows 7 with SP1 U Russian\\ru_windows_7_professional_with_sp1_x64_dvd_u_677024.iso");
  84. return 0;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement