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
|
use io;
use sector;
use errors;
def mbr_lba: size = 0;
export type nombr = !void;
export type mbr = struct {
@offset(0) bootstrap: [440]u8,
@offset(440) udid: u32,
@offset(444) reserved: u16,
@offset(446) entries: [4]mbr_entry,
@offset(510) magic: u16,
};
export type mbr_entry = struct {
@offset(0) attributes: u8,
@offset(4) part_type: u8,
@offset(8) lba_begin: u32,
@offset(12) lba_end: u32,
};
export fn validate(self: *mbr) bool = {
if (self.magic != 0xaa55) {
return false;
};
return true;
};
// Gets a handle to the MBR of a partition.
// Return value is returned to user. Resource should be freed with mbr::finish.
export fn from(fd: io::file) (*mbr | nombr | errors::error) = {
const self = io::mmap(null, sector::length, io::prot::READ | io::prot::WRITE, io::mflags::SHARED, fd, sector::lba(mbr_lba))!: *mbr;
if (!validate(self)) {
io::munmap(self, sector::length)?;
return nombr;
};
return self;
};
// Frees the resources associated with the MBR partition.
// User revokes ownership.
export fn finish(self: *mbr) (void | errors::error) = {
io::munmap(self, sector::length)?;
};
|