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
|
use fmt;
use os;
use io;
use fs;
use strings;
use errors;
use rt;
use getopt;
use cmd;
use mbr;
use gpt;
use sector;
export fn convert(args: []str) void = {
let nombr = false;
let notranslation = false;
let nohybrid = false;
const c = getopt::parse(args,
('m', "do not overwrite the MBR bootstrap"),
('t', "do not translate partitions to GPT"),
('H', "do not create a hybrid MBR"),
"disk file"
);
defer getopt::finish(&c);
for (let i = 0z; i < len(c.opts); i += 1) {
const opt = c.opts[i];
switch (opt.0) {
case 'm' => nombr = true;
case 't' => notranslation = true;
case 'H' => nohybrid = true;
};
};
if (len(c.args) < 1)
fmt::fatalf("gpt.convert: need a disk file");
let sourcefd = match (os::open(c.args[0], fs::flags::RDONLY)) {
case let f: io::file =>
yield f;
case => fmt::fatalf("gpt.convert: could not open file {}", args[1]);
};
defer io::close(sourcefd)!;
const source = mbr::from(sourcefd)!;
defer mbr::finish(source);
let fd = cmd::openfile();
defer io::close(fd)!;
let vol = cmd::opengpt(fd);
defer {
vol.mbr.entries[3] = mbr::entry {
part_type = 0xEE,
lba_begin = 0,
length = -1: u32,
...
};
gpt::chksums(vol);
gpt::commit(vol)!;
gpt::finish(vol);
};
if (!nombr)
rt::memcpy(vol.mbr, source.bootsector, size(mbr::bootsector));
if (notranslation)
return;
for (let i = 0z; i < 4; i += 1) {
const entry = &source.bootsector.entries[i];
if (entry.lba_begin == 0 && entry.length == 0)
continue;
const reader = mbr::partstream_reader(source, entry)!;
const partlength = entry.length;
const part = match (gpt::allocate(vol, partlength)) {
case let e: *gpt::entry =>
yield e;
case =>
fmt::errorfln("gpt.convert: could not allocate size needed for MBR partition {}", i)!;
continue;
};
const writer = gpt::partstream_writer(vol, part)!;
io::copy(&writer, &reader)!;
if (!nohybrid) {
vol.mbr.entries[i].lba_begin = part.lba_begin: u32;
vol.mbr.entries[i].length = (part.lba_end - part.lba_begin + 1): u32;
};
};
};
|