summaryrefslogtreecommitdiff
path: root/convert
diff options
context:
space:
mode:
Diffstat (limited to 'convert')
-rw-r--r--convert/convert.ha81
1 files changed, 39 insertions, 42 deletions
diff --git a/convert/convert.ha b/convert/convert.ha
index 98ad124..7ba6bf0 100644
--- a/convert/convert.ha
+++ b/convert/convert.ha
@@ -9,66 +9,63 @@ use io;
use fs;
use strings;
use errors;
+use rt;
export fn convert(args: []str) void = {
- if (len(args) < 2) {
+ if (len(args) < 3) {
fmt::fatalf("gptman.convert: needs disk");
};
- let file = os::open(args[1], fs::flags::RDWR)!;
+ let source = os::open(args[1], fs::flags::RDONLY)!;
+ defer io::close(source)!;
+
+ const source = mbr::from(source)!;
+
+ let freeno: size = 0z;
+ for (let i = 0z; i < 4; i += 1) {
+ const entry = &source.bootsector.entries[i];
+
+ if (entry.lba_begin == 0 && entry.length == 0)
+ continue;
+
+ freeno += entry.length: size;
+ };
+
+ let file = os::create(args[2], fs::mode::USER_RWX | fs::mode::GROUP_RX | fs::mode::OTHER_RX, fs::flags::RDWR)!;
defer io::close(file)!;
- // let vol = gpt::create(file, 69);
- // gpt::mkbackup(vol);
- // defer {
- // gpt::chksums(vol);
- // gpt::commit(vol)!;
- // gpt::finish(vol);
- // };
-
- // vol.mbr.entries[0] = mbr::mbr_entry {
- // attributes = 0,
- // part_type = 0xEE,
- // lba_begin = 1,
- // lba_end = -1: u32,
- // };
- // vol.mbr.magic = 0xaa55;
-
- let vol = gpt::from(file)!;
+ let vol = gpt::create(file, freeno);
gpt::mkbackup(vol);
defer {
gpt::chksums(vol);
gpt::commit(vol)!;
gpt::finish(vol);
};
+
+ rt::memcpy(vol.mbr, source.bootsector, size(mbr::bootsector));
+ for (let i = 0z; i < 4; i += 1) {
+ const entry = &source.bootsector.entries[i];
+ if (entry.lba_begin == 0 && entry.length == 0)
+ continue;
- // fmt::printfln("rev {}", vol.primary.header.revision)!;
-
- // for (let i = 0z; i < 4; i += 1) {
- // fmt::printfln("type {} from {} to {}", vol.mbr.entries[i].part_type, vol.mbr.entries[i].lba_begin, vol.mbr.entries[i].lba_end)!;
- // };
-
- // vol.primary.header.disk_guid[0] = 0xcafababe;
- // vol.primary.header.disk_guid[1] = 0xabababababababab;
-
- // // 516E7CB6-6ECF-11D6-8FF8-00022D09712B
+ const reader = mbr::partstream_reader(source, entry)!;
+ const partlength = entry.length;
- // vol.primary.entries[4].part_type[0] = 0x516E7CB66ECF;
- // vol.primary.entries[4].lba_begin = 35;
- // vol.primary.entries[4].lba_end = 37;
- // vol.primary.entries[5].part_type[0] = 0x516E7CB66ECF;
- // vol.primary.entries[5].lba_begin = 40;
- // vol.primary.entries[5].lba_end = 41;
+ const part = gpt::allocate(vol, partlength) as *gpt::entry;
+ const writer = gpt::partstream_writer(vol, part)!;
- const much = 4z;
- const addr = gpt::findfree(vol, much);
- const where = gpt::findfreeentry(vol) as *gpt::entry;
+ io::copy(&writer, &reader)!;
- where.part_type = [0xCAFEBABE, 0xBABABA];
- where.lba_begin = addr;
- where.lba_end = addr + much: u64 - 1;
+ vol.mbr.entries[i].lba_begin = part.lba_begin: u32;
+ vol.mbr.entries[i].length = (part.lba_end - part.lba_begin + 1): u32;
+ };
- fmt::printfln("fitting {}: {} and could be put at {}", much, addr, where)!;
+ vol.mbr.entries[3] = mbr::entry {
+ part_type = 0xEE,
+ lba_begin = 0,
+ length = -1: u32,
+ ...
+ };
};