Skip to content

Instantly share code, notes, and snippets.

@hackolite
Created March 20, 2015 13:17
Show Gist options
  • Save hackolite/f796a49bf18e276585f4 to your computer and use it in GitHub Desktop.
Save hackolite/f796a49bf18e276585f4 to your computer and use it in GitHub Desktop.
fdisk.py
#!/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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment