summaryrefslogtreecommitdiff
path: root/mbr/mbr.ha
blob: 31a9534d4d1124689f1a94617c5a90c6e4205cdf (plain)
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
use io;

use sector;
use errors;

def mbr_lba: size = 0;

export type nombr = !void;

export type mbr = struct {
	bootsector_sec: sector::sector,
	bootsector: *bootsector
};

export type bootsector = struct {
	@offset(0) bootstrap: [440]u8,
	@offset(440) udid: u32,
	@offset(444) reserved: u16,
	@offset(446) entries: [4]entry,
	@offset(510) magic: u16,
};

export type entry = struct {
	@offset(0) attributes: u8,
	@offset(4) part_type: u8,
	@offset(8) lba_begin: u32,
	@offset(12) length: u32, 
};

export fn bootsector_validate(self: *bootsector) bool = {
	if (self.magic != 0xaa55) {
		return false;
	};

	return true;
};

export fn from(fd: io::file) (*mbr | nombr | io::error) = {
	const bootsector_sec = sector::map(fd, 0, 1);
	sector::fetch(&bootsector_sec)?;

	const bootsector = bootsector_sec.buf: *[*]u8: *bootsector;
	if (!bootsector_validate(bootsector)) {
		sector::finish(&bootsector_sec);
		return nombr;
	};

	let self = alloc(mbr {
		bootsector_sec = bootsector_sec,
		bootsector = bootsector
	});

	return self;
};

export fn partstream_reader(self: *mbr, entry: *entry) (io::limitstream | io::error) = {
	const fd = self.bootsector_sec.fd;
	const part = entry.lba_begin * sector::sector_length;
	const length = entry.length * sector::sector_length;

	io::seek(fd, part: io::off, io::whence::SET)?;
	return io::limitreader(fd, length);
};