Skip to content

Commit

Permalink
wip: first table header section
Browse files Browse the repository at this point in the history
  • Loading branch information
swysocki committed Nov 8, 2021
1 parent fc1523c commit 0f8d9ef
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 7 deletions.
4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@
"tests"
],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true
"python.testing.pytestEnabled": true,
"python.linting.flake8Enabled": true,
"python.linting.enabled": true
}
55 changes: 54 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions pygpt_disk/disk.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@


class Disk:
sector_size = 512

def __init__(self, size: int, name: str):
self.size = size
self.name = name
self.disk = io.BytesIO()
self.buffer = io.BytesIO() # this is bad, it makes Disk.disk :(

def create(self) -> None:
self.disk.seek(self.size - 1)
self.disk.write(b"\0")
self.buffer.seek(self.size - 1)
self.buffer.write(b"\0")

def write(self) -> None:
"""Write a disk buffer to file"""
with open(self.name, "wb") as f:
f.write(self.disk.getvalue())
f.write(self.buffer.getvalue())
29 changes: 29 additions & 0 deletions pygpt_disk/table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"""
LBA 0 is the protective MBR
LBA 1 is where the header starts
Byte 0 - 7 (Signature) "EFI PART" or 45h 46h 49h 20h 50h 41h 52h
Byte 8 - 11 (Revision) "1.0" or 00h 00h 01h 00h
Byte 12 - 15 (Header size LE) 5Ch 00h 00h 00h (92 bytes)
Byte 16 - 19 CRC32 of header from 0 to 15
Byte 20 - 23 reserved (must be zero)
Byte 24 - 31 Current LBA location
Byte 32 - 39 Backup LBA location
LBA 2 - 33 partitions
"""
from pygpt_disk.disk import Disk


class Table:
_header_sig = b"\x45\x46\x49\x20\x50\x41\x52\x54"

def __init__(self, disk: Disk):
self.disk = disk

def create(self):
"""Create blank GPT Table Header"""
# move to LBA 1
self.disk.buffer.seek(self.disk.sector_size - 1)
self.disk.buffer.write(Table._header_sig)
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ python = "^3.8"
black = "^21.10b0"
pytest = "^6.2.5"
mypy = "^0.910"
flake8 = "^4.0.1"

[build-system]
requires = ["poetry-core>=1.0.0"]
Expand Down
3 changes: 2 additions & 1 deletion tests/test_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ def test_init():
d = disk.Disk(DISK_SIZE, DISK_NAME)
assert d.size == DISK_SIZE
assert d.name == DISK_NAME
assert d.sector_size == 512


def test_create():
d = disk.Disk(DISK_SIZE, DISK_NAME)
d.create()
assert len(d.disk.getvalue()) == DISK_SIZE
assert len(d.buffer.getvalue()) == DISK_SIZE


def test_write(tmp_path):
Expand Down
15 changes: 15 additions & 0 deletions tests/test_table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from pygpt_disk import table, disk
import pytest


@pytest.fixture
def fresh_disk(tmp_path):
image_path = tmp_path / "table-test.img"
return disk.Disk(8 * 1024 * 1024, image_path)


def test_init(fresh_disk: disk.Disk):
t = table.Table(fresh_disk)
t.create()
t.disk.buffer.seek(512 - 1)
assert t.disk.buffer.read(8) == b"EFI PART"
Binary file added tests/testdata/image.img
Binary file not shown.

0 comments on commit 0f8d9ef

Please sign in to comment.