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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
use fmt;
use getopt;
use io;
use os;
use strconv;
use cmd;
use gpt;
export fn part(args: []str) void = {
const fd = cmd::openfile();
defer io::close(fd)!;
const vol = cmd::opengpt(fd);
defer gpt::finish(vol);
if (len(args) == 1) {
partlist(vol);
return;
};
switch (args[1]) {
case "new" =>
partnew(vol, args[1..]);
return;
case "list" =>
partlist(vol);
return;
case => yield;
};
const entid = match (strconv::stou(args[1])) {
case let i: uint =>
yield i;
case => fmt::fatalf("gpt.part: invalid number");
};
partno(vol, entid, args[2..]);
};
export fn partnew(vol: *gpt::gpt, args: []str) void = {
defer {
gpt::chksums(vol);
gpt::commit(vol)!;
};
const c = getopt::parse(args,
('f', "file", "specify a file that the partition must fit"),
('b', "bytes", "specify another length margin (bytes)"),
('s', "sectors", "specify another length margin (sectors, 512 bytes)")
);
defer getopt::finish(&c);
let length = 0z;
for (let i = 0z; i < len(c.opts); i += 1) {
const opt = c.opts[i];
// XXX incorrect calculations and handle incorrect input
switch (opt.0) {
case 'l' =>
length += (strconv::stoi64(opt.1)! / 512 + 1): size;
case 's' =>
length += strconv::stou64(opt.1)!: size;
case 'f' =>
const stat = os::stat(opt.1)!;
length += stat.sz / 512 + 1;
};
};
const entry = match (gpt::allocate(vol, length)) {
case let e: *gpt::entry =>
yield e;
case =>
fmt::fatalf("gpt.part: could not allocate entry of size {}", length);
};
let id = (entry: uintptr - vol.primary.entries: uintptr): size / size(gpt::entry);
fmt::printfln("{}", id)!;
};
export fn partlist(vol: *gpt::gpt) void = {
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 = cmd::getgptentry(vol, 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 = cmd::getgptentry(vol, 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 = cmd::getgptentry(vol, 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)!;
};
|