forked from andreax79/python-cstruct
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfdisk.py
More file actions
89 lines (80 loc) · 3.28 KB
/
fdisk.py
File metadata and controls
89 lines (80 loc) · 3.28 KB
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
#!/usr/bin/python
#*****************************************************************************
#
# Copyright (c) 2013 Andrea Bonomi <[email protected]>
#
# Published under the terms of the MIT license.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
#
#*****************************************************************************
import cstruct
import sys
class Position(cstruct.CStruct):
__byte_order__ = cstruct.LITTLE_ENDIAN
__struct__ = """
unsigned char head;
unsigned char sector;
unsigned char cyl;
"""
class Partition(cstruct.CStruct):
__byte_order__ = cstruct.LITTLE_ENDIAN
__struct__ = """
unsigned char status; /* 0x80 - active */
struct Position start;
unsigned char partition_type;
struct Position end;
unsigned int start_sect; /* starting sector counting from 0 */
unsigned int sectors; /* nr of sectors in partition */
"""
def print_info(self):
print("bootable: %s" % ((self.status & 0x80) and "Y" or "N"))
print("partition_type: %02X" % self.partition_type)
print("start: head: %X sectory: %X cyl: %X" % (self.start.head, self.start.sector, self.start.cyl))
print("end: head: %X sectory: %X cyl: %X" % (self.end.head, self.end.sector, self.end.cyl))
print("starting sector: %08X" % self.start_sect)
print("size MB: %s" % (self.sectors / 2 / 1024))
class MBR(cstruct.CStruct):
__byte_order__ = cstruct.LITTLE_ENDIAN
__struct__ = """
char unused[440];
unsigned char disk_signature[4];
unsigned char usualy_nulls[2];
struct Partition partitions[4];
char signature[2];
"""
def print_info(self):
print("disk signature: %s" % "".join(["%02X" % x for x in self.disk_signature]))
print("usualy nulls: %s" % "".join(["%02X" % x for x in self.usualy_nulls]))
for i, partition in enumerate(self.partitions):
print("")
print("partition: %s" % i)
partition.print_info()
def main():
if len(sys.argv) != 2:
print("usage: %s disk" % sys.argv[0])
sys.exit(2)
f = open(sys.argv[1], "rb")
mbr = MBR()
data = f.read(len(mbr))
mbr.unpack(data)
mbr.print_info()
f.close()
if __name__ == "__main__":
main()