diff options
| author | Alejandro Sior <aho@sior.be> | 2022-06-21 10:38:45 +0200 |
|---|---|---|
| committer | Alejandro Sior <aho@sior.be> | 2022-06-21 10:38:45 +0200 |
| commit | c99646423f311c9238ace86be17caf531fe61c44 (patch) | |
| tree | b17f04f09ce6eb918e71983c8e7b1589eb0ec87f | |
| parent | fd2f5a94d6ad595e8bed354eca168a82b882e8b4 (diff) | |
gptman: partition info
| -rw-r--r-- | header/header.ha | 39 | ||||
| -rw-r--r-- | info/info.ha | 36 | ||||
| -rw-r--r-- | main.ha | 6 | ||||
| -rw-r--r-- | part/part.ha | 55 | ||||
| -rw-r--r-- | volume/volume.ha | 25 |
5 files changed, 113 insertions, 48 deletions
diff --git a/header/header.ha b/header/header.ha deleted file mode 100644 index 0e93842..0000000 --- a/header/header.ha +++ /dev/null @@ -1,39 +0,0 @@ -use fmt; -use io; - -use gpt; -use volume; - -export fn header(vol: str, args: []str) void = { - const vol = volume::openvol(vol); - const vol = match (gpt::from(vol)) { - case let g: *gpt::gpt => - yield g; - case gpt::nogpt => - fmt::fatalf("gpt.header: disk {} does not have valid gpt", args[0]); - case => - fmt::fatalf("gpt.header: could not access disk {}", args[0]); - }; - defer gpt::finish(vol); - - const header = vol.primary.header; - - fmt::printfln("revision: \t\t{}", header.revision)!; - fmt::printfln("header_size: \t\t{}", header.header_size)!; - fmt::printfln("header_crc32: \t\t{}", header.header_crc32)!; - fmt::printfln("header_lba: \t\t{}", header.header_lba)!; - fmt::printfln("backup_header_lba: \t{}", header.backup_header_lba)!; - fmt::printfln("first_lba: \t\t{}", header.first_lba)!; - fmt::printfln("last_lba: \t\t{}", header.last_lba)!; - fmt::printfln("disk_guid: \t\t[{},{}]", header.disk_guid[0], header.disk_guid[1])!; - fmt::printfln("entries_lba: \t\t{}", header.entries_lba)!; - fmt::printfln("entries_len: \t\t{}", header.entries_len)!; - fmt::printfln("entry_size: \t\t{}", header.entry_size)!; - fmt::printfln("entries_crc32: \t\t{}", header.entries_crc32)!; - - const primary_valid = gpt::header_crc32(header) == header.header_crc32 && gpt::entries_crc32(vol.primary.entries) == header.entries_crc32; - const backup_valid = gpt::header_crc32(vol.backup.header) == vol.backup.header.header_crc32 && gpt::entries_crc32(vol.backup.entries) == vol.backup.header.entries_crc32; - - fmt::printfln("primary_sane: \t\t{}", primary_valid)!; - fmt::printfln("backup_sane: \t\t{}", backup_valid)!; -}; diff --git a/info/info.ha b/info/info.ha new file mode 100644 index 0000000..7df9ee2 --- /dev/null +++ b/info/info.ha @@ -0,0 +1,36 @@ +use fmt; +use io; + +use gpt; +use volume; + +fn booltonum(a: bool) str = if (a) { return "1"; } else { return "0"; }; + +export fn info(vol: str, args: []str) void = { + const name = vol; + const vol = volume::opengpt(vol); + defer gpt::finish(vol); + + const header = vol.primary.header; + + fmt::printfln("# GPT header info {}", name)!; + fmt::printfln("revision({})", header.revision)!; + fmt::printfln("header_size({})", header.header_size)!; + fmt::printfln("header_crc32({})", header.header_crc32)!; + fmt::printfln("header_lba({})", header.header_lba)!; + fmt::printfln("backup_header_lba({})", header.backup_header_lba)!; + fmt::printfln("first_lba({})", header.first_lba)!; + fmt::printfln("last_lba({})", header.last_lba)!; + // XXX Add a comment output for the GUID written in normal form + fmt::printfln("disk_guid([{},{}])", header.disk_guid[0], header.disk_guid[1])!; + fmt::printfln("entries_lba({})", header.entries_lba)!; + fmt::printfln("entries_len({})", header.entries_len)!; + fmt::printfln("entry_size({})", header.entry_size)!; + fmt::printfln("entries_crc32({})", header.entries_crc32)!; + + const primary_sane = gpt::header_crc32(header) == header.header_crc32 && gpt::entries_crc32(vol.primary.entries) == header.entries_crc32; + const backup_sane = gpt::header_crc32(vol.backup.header) == vol.backup.header.header_crc32 && gpt::entries_crc32(vol.backup.entries) == vol.backup.header.entries_crc32; + + fmt::printfln("primary_sane({})", booltonum(primary_sane))!; + fmt::printfln("backup_sane({})", booltonum(backup_sane))!; +}; @@ -9,8 +9,9 @@ use gpt; use convert; use create; -use header; +use info; use mkbackup; +use part; export fn main() void = { @@ -23,8 +24,9 @@ export fn main() void = { switch (os::args[2]) { case "convert" => convert::convert(volume, os::args[2..]); case "create" => create::create(volume, os::args[2..]); - case "header" => header::header(volume, os::args[2..]); + case "info" => info::info(volume, os::args[2..]); case "mkbackup" => mkbackup::mkbackup(volume, os::args[2..]); + case "part" => part::part(volume, os::args[2..]); case => fmt::fatalf("{}: no such command {}", os::args[0], os::args[2]); }; }; diff --git a/part/part.ha b/part/part.ha new file mode 100644 index 0000000..46c6546 --- /dev/null +++ b/part/part.ha @@ -0,0 +1,55 @@ +use fmt; +use strconv; + +use gpt; +use volume; + + +export fn part(vol: str, args: []str) void = { + if (len(args) <= 1) { + list(vol, args[1..]); + return; + }; + + match (strconv::stou(args[1])) { + case let i: uint => + const vol = volume::opengpt(vol); + defer gpt::finish(vol); + partinfo(vol, i); + return; + case => + yield; + }; + + switch (args[1]) { + case "list" => list(vol, args[1..]); + }; +}; + +export fn list(vol: str, args: []str) void = { + const vol = volume::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 +}; diff --git a/volume/volume.ha b/volume/volume.ha index d3bae12..832a96f 100644 --- a/volume/volume.ha +++ b/volume/volume.ha @@ -3,24 +3,35 @@ use fs; use io; use os; +use gpt; + export fn mkvol(vol: str) io::file = { - const vol = match (os::create(vol, fs::mode::USER_RWX | fs::mode::GROUP_RX | fs::mode::OTHER_RX, fs::flags::RDWR)) { + 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("{}: cannot open file {}", os::args[0], os::args[1]); + fmt::fatalf("{}: cannot open file {}", os::args[0], vol); }; - - return vol; }; export fn openvol(vol: str) io::file = { - const vol = match (os::open(vol, fs::flags::RDWR)) { + return match (os::open(vol, fs::flags::RDWR)) { case let v: io::file => yield v; case => - fmt::fatalf("{}: cannot open file {}", os::args[0], os::args[1]); + fmt::fatalf("{}: cannot open file {}", os::args[0], vol); }; +}; - return vol; +export fn opengpt(vol: str) *gpt::gpt = { + const vol = openvol(vol); + + return match (gpt::from(vol)) { + case let g: *gpt::gpt => + yield g; + case io::error => + fmt::fatalf("{}: could not properly read disk {}", os::args[0], os::args[1]); + case => + fmt::fatalf("{}: no valid gpt in disk", os::args[0]); + }; }; |
