summaryrefslogtreecommitdiff
path: root/cmd/part/part.ha
blob: a4b114f12aee17a8389001f1acc8fbe8a8dd0029 (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
use fmt;
use io;
use os;
use strconv;

use gpt;
use cmd;

export fn part(vol: str, args: []str) void = {
	if (len(args) <= 1) {
		partlist(vol, args[1..]);
		return;
	};
	
	match (strconv::stou(args[1])) {
	case let i: uint =>
		const vol = cmd::opengpt(vol);
		defer gpt::finish(vol);
		partno(vol, i, args[2..]);
		return;
	case =>
		yield;
	};

	switch (args[1]) {
	case "new" => partnew(vol, args[1..]);
	case "list" => partlist(vol, args[1..]);
	};
};

export fn partnew(vol: str, args: []str) void = {
	const vol = cmd::opengpt(vol);
	defer {
		gpt::chksums(vol);
		gpt::commit(vol)!;
		gpt::finish(vol);
	};

	const sz = 71z;

	const entry = match (gpt::allocate(vol, sz)) {
	case let e: *gpt::entry =>
		yield e;
	case =>
		fmt::fatalf("gpt.part: could not allocate entry of size {}", sz);
	};
};

export fn partlist(vol: str, args: []str) void = {
	const vol = cmd::opengpt(vol);
	defer gpt::finish(vol);
	const header = vol.primary.header;
	const entries = vol.primary.entries;

	for (let i = 0z; i < header.entries_len; i += 1) {
		const entry = &entries[i];
		if (entry.lba_begin == 0 && entry.lba_end == 0)
			continue;
	
		partinfo(vol, i);
	};
};

fn partinfo(vol: *gpt::gpt, i: size) void = {
	const entry = &vol.primary.entries[i];

	fmt::printfln("# Partition {}", i)!;
	fmt::printfln("entries[{}].part_type([{},{}])", i, entry.part_type[0], entry.part_type[1])!;
	fmt::printfln("entries[{}].part([{},{}])", i, entry.part[0], entry.part[1])!;
	fmt::printfln("entries[{}].lba_begin({})", i, entry.lba_begin)!;
	fmt::printfln("entries[{}].lba_end({})", i, entry.lba_end)!;
	fmt::printfln("entries[{}].attributes({})", i, entry.attributes)!;

	// XXX name
};

fn partno(vol: *gpt::gpt, i: size, args: []str) void = {
	const header = vol.primary.header;

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

	if (len(args) == 0) {
		partinfo(vol, i);
		return;
	};

	switch (args[0]) {
	case "fill" =>
		partfill(vol, i);
	case "dump" =>
		partdump(vol, i);
	};
};

fn partfill(vol: *gpt::gpt, i: size) void = {
	const entry = &vol.primary.entries[i];

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

	const writer = match (gpt::partstream_writer(vol, entry)) {
	case let l: io::limitstream =>
		yield l;
	case =>
		fmt::fatalf("gpt.part: could not create a writing stream");
	};

	io::copy(&writer, os::stdin)!;
};

fn partdump(vol: *gpt::gpt, i: size) void = {
	const entry = &vol.primary.entries[i];

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

	const reader = match (gpt::partstream_reader(vol, entry)) {
	case let l: io::limitstream =>
		yield l;
	case =>
		fmt::fatalf("gpt.part: could not create a reading stream");
	};

	io::copy(os::stdout, &reader)!;
};