summaryrefslogtreecommitdiff
path: root/cmd/util.ha
blob: 5b418301334b80c1c9ec2287ea246588cf3b612b (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
use fmt;
use fs;
use io;
use os;

use gpt;

export fn mkfile() io::file = {
	const vol = os::args[1];

	return match (os::create(vol, fs::mode::USER_RWX | fs::mode::GROUP_RX | fs::mode::OTHER_RX, fs::flags::RDWR)) {
	case let v: io::file =>
		yield v;
	case =>
		fmt::fatalf("gpt: cannot open file {}", vol);
	};
};

export fn openfile() io::file = {
	const vol = os::args[1];

	return match (os::open(vol, fs::flags::RDWR)) {
	case let v: io::file =>
		yield v;
	case =>
		fmt::fatalf("gpt: cannot open file {}", vol);
	};
};

export fn opengpt(fd: io::file) *gpt::gpt = {
	return match (gpt::from(fd)) {
	case let g: *gpt::gpt =>
		yield g;
	case io::error =>
		fmt::fatalf("gpt: could not properly read disk");
	case =>
		fmt::fatalf("gpt: no valid gpt in disk");
	};
};

export fn getgptentry(vol: *gpt::gpt, i: size) *gpt::entry = {
	const header = vol.primary.header;

	if (i >= header.entries_len)
		fmt::fatalf("gpt.part: invalid entry index");

	const entry = &vol.primary.entries[i];

	if (entry.lba_begin == 0 && entry.lba_end == 0)
		fmt::fatalf("gpt.part: entry {} is not allocated", i);

	return entry;
};