Can't get right value from mbr

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <cstdlib>
#include <iostream>
#include <windows.h>
using namespace std;
//131
WORD begin_cylinder,end_cylinder;
#pragma pack(1)
typedef struct __PARTITION_RECORD_
{
BYTE           status;            /* 0x80=bootable, 0x00=non-bootable, else invalid*/
BYTE           begin_head;
BYTE           begin_sector:5;
WORD           begin_cylinder:10;

BYTE           partition_type;  
BYTE           end_head;
BYTE           end_sector:5;
WORD           end_cylinder:10;  

DWORD          begin_lba;
DWORD          lba_blocks;      

}PARTITION_RECORD;


typedef struct __MBR_
{
BYTE                 boot_code[440]; 
DWORD                disk_signature; //4 bitai
WORD                 unknown;        
PARTITION_RECORD     partition_table[4]; 
WORD                 signature;          /*AA55*/   
 
}MBR; 
#pragma pack(0)
MBR mbr;




/* Open a hdd and then read the data to struct */       
short get_mbr_data(const char *_pd,MBR *_mbr)
{
DWORD dwRead;
HANDLE hHDD=CreateFile(_pd,GENERIC_READ,FILE_SHARE_VALID_FLAGS,NULL,OPEN_EXISTING,0,0);
if(hHDD==INVALID_HANDLE_VALUE) goto err;

memset(_mbr,0,sizeof(_mbr));
ReadFile(hHDD,_mbr,512,&dwRead,0);
if(dwRead!=512)
    goto err;
CloseHandle(hHDD);
return 0;
err:
CloseHandle(hHDD);
return 1;
}
      /* Write data into mbr */
short write_mbr_data(const char *_pd,MBR *_mbr)
{   
DWORD dwWrite;    
HANDLE hHDD=CreateFile(_pd,GENERIC_WRITE,FILE_SHARE_VALID_FLAGS,NULL,OPEN_EXISTING,0,0);      
if(hHDD==INVALID_HANDLE_VALUE)
    goto err;    

WriteFile(hHDD,_mbr,512,&dwWrite,0);
if(dwWrite!=512) 
    goto err;
CloseHandle(hHDD);  
return 0; 
err:
CloseHandle(hHDD);
return 1;         
}       


                   const char *disk_default="\\\\.\\PhysicalDrive0";
                       
int main()                
{                   
cout <<"MBR of "<<disk_default<<endl<<"========================"<<endl;

                            
get_mbr_data(disk_default,&mbr);
                  

                      
cout <<"Disk signature      "<<mbr.disk_signature<<endl;
cout <<"Unknown             "<<mbr.unknown<<endl;
cout <<"AA55 sign           "<<hex<<mbr.signature<<endl;

cout <<endl<<endl;        
for(int part=0;part<4;part++)
  {  
          
     cout <<"-----------------\nPartition"<<part<<"\n-----------------"<<endl;                  
     cout <<"Status              "<<dec<<(unsigned short)mbr.partition_table[part].status<<endl;
     cout <<"begin head          "<<(unsigned short)mbr.partition_table[part].begin_head<<endl;
     cout <<"Begin cylinder      "<<(unsigned short)mbr.partition_table[part].begin_cylinder<<endl;
     cout <<"Begin sector        "<<(unsigned short)mbr.partition_table[part].begin_sector<<endl;
     cout <<"Partition type      "<<hex<<(unsigned short)mbr.partition_table[part].partition_type<<endl;
                      
     cout <<"End head            "<<dec<<(unsigned short)mbr.partition_table[part].end_head<<endl;
     cout <<"End cylinder        "<<(unsigned short)mbr.partition_table[part].end_cylinder<<endl;
     cout <<"End sector          "<<(unsigned short)mbr.partition_table[part].end_sector<<endl; // problem here
            
     cout <<"begin lba           "<<(unsigned short)mbr.partition_table[part].begin_lba<<endl;
     cout <<"LBA blocks          "<<(unsigned short)mbr.partition_table[part].lba_blocks<<endl;         
  }         
cout <<"\n\n";      



cout <<"Press enter to continue";
getchar();
}


For some reason I get incorrect values (always 31) of mbr.partition_table[part].end_sector
What may be wrong here?
Thanks for help.
Last edited on
Anyone!???
Tried it - but could not get handle - but i was using windows 7 and I get the feeling I need admin rights to get a handle to the physicaldisk0
Topic archived. No new replies allowed.