Skip to content

Commit c52f543

Browse files
committed
slowly getting started
1 parent 9cc8866 commit c52f543

File tree

2 files changed

+106
-44
lines changed

2 files changed

+106
-44
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.zig-cache/
22
zig-out/
33
.vscode/
4-
.dim-out/
4+
.dim-out/
5+
.idea/

src/components/part/GptPartitionTable.zig

Lines changed: 104 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -6,63 +6,124 @@ pub fn execute(ctx: dim.Context) !void {
66
@panic("gpt-part not implemented yet!");
77
}
88

9-
pub const gpt = struct {
10-
pub const Guid = [16]u8;
9+
const block_size = 512;
1110

12-
pub const Table = struct {
13-
disk_id: Guid,
11+
const PartTable = @This();
1412

15-
partitions: []const Partition,
16-
};
13+
disk_id: Guid,
14+
partitions: []Partition,
1715

18-
pub const Partition = struct {
19-
type: Guid,
20-
part_id: Guid,
16+
fn render(table: *PartTable, stream: *dim.BinaryStream) dim.Content.RenderError!void {
17+
{
18+
var pmbr: [block_size]u8 = @splat(0);
19+
var efi_prot: *[16]u8 = &pmbr[0x1BE..][0..16];
20+
std.mem.writeInt(u48, &efi_prot[1], 0x000200, .little);
21+
efi_prot[4] = 0xEE;
22+
// TODO: ending CHS
23+
std.mem.writeInt(u64, &efi_prot[8], 1, .little);
24+
// TODO: size in LBA
2125

22-
offset: ?u64 = null,
23-
size: u64,
26+
pmbr[0x01FE] = 0x55;
27+
pmbr[0x01FF] = 0xAA;
2428

25-
name: [36]u16,
29+
try stream.write(0, &pmbr);
30+
}
31+
{
32+
var gpt_header: [block_size]u8 = @splat(0);
33+
@memcpy(&gpt_header[0..8], "EFI PART");
34+
std.mem.writeInt(u32, &gpt_header[8..12], 0x00010000, .little);
35+
std.mem.writeInt(u32, &gpt_header[12..16], 96, .little);
36+
// TODO: header CRC
37+
std.mem.writeInt(u64, &gpt_header[24..32], 1, .little);
38+
// TODO: alternate lba
39+
// TODO: first usable lba
40+
// TODO: last usable lba
41+
@memcpy(&gpt_header[56..72], &table.disk_id);
42+
std.mem.writeInt(u64, &gpt_header[72..80], 2, .little);
43+
std.mem.writeInt(u32, &gpt_header[80..84], table.partitions.len, .little);
44+
std.mem.writeInt(u32, &gpt_header[84..88], 128, .little);
45+
// TODO: partition array CRC
46+
}
47+
}
2648

27-
attributes: Attributes,
49+
pub const Guid = [16]u8;
2850

29-
// data: Content,
51+
fn parseGuid(str: []const u8) !Guid {
52+
@setEvalBranchQuota(4096);
3053

31-
pub const Attributes = packed struct(u32) {
32-
system: bool,
33-
efi_hidden: bool,
34-
legacy: bool,
35-
read_only: bool,
36-
hidden: bool,
37-
no_automount: bool,
54+
var guid: Guid = undefined;
3855

39-
padding: u26 = 0,
40-
};
41-
};
56+
if (str.len != 36 or std.mem.count(u8, str, "-") != 4 or str[8] != '-' or str[13] != '-' or str[18] != '-' or str[23] != '-') {
57+
return error.InvalidUuid;
58+
}
59+
60+
const set_1 = try std.fmt.parseInt(u32, str[0..8], 16);
61+
const set_2 = try std.fmt.parseInt(u16, str[9..13], 16);
62+
const set_3 = try std.fmt.parseInt(u16, str[14..18], 16);
63+
const set_4 = try std.fmt.parseInt(u16, str[19..23], 16);
64+
const set_5 = try std.fmt.parseInt(u48, str[24..36], 16);
65+
66+
std.mem.writeInt(u32, &guid[0], set_1, .big);
67+
std.mem.writeInt(u16, &guid[4], set_2, .big);
68+
std.mem.writeInt(u16, &guid[6], set_3, .big);
69+
std.mem.writeInt(u16, &guid[8], set_4, .big);
70+
std.mem.writeInt(u48, &guid[10], set_5, .big);
71+
72+
return guid;
73+
}
74+
75+
pub const Partition = struct {
76+
type: Guid,
77+
part_id: Guid,
4278

43-
/// https://en.wikipedia.org/wiki/GUID_Partition_Table#Partition_type_GUIDs
44-
pub const PartitionType = struct {
45-
pub const unused: Guid = .{};
79+
offset: ?u64 = null,
80+
size: u64,
4681

47-
pub const microsoft_basic_data: Guid = .{};
48-
pub const microsoft_reserved: Guid = .{};
82+
name: [35:0]u16,
4983

50-
pub const windows_recovery: Guid = .{};
84+
attributes: Attributes,
5185

52-
pub const plan9: Guid = .{};
86+
data: dim.Content,
5387

54-
pub const linux_swap: Guid = .{};
55-
pub const linux_fs: Guid = .{};
56-
pub const linux_reserved: Guid = .{};
57-
pub const linux_lvm: Guid = .{};
88+
pub const Attributes = packed struct(u32) {
89+
system: bool,
90+
efi_hidden: bool,
91+
legacy: bool,
92+
read_only: bool,
93+
hidden: bool,
94+
no_automount: bool,
95+
96+
padding: u10 = 0,
97+
user: u16,
5898
};
99+
};
59100

60-
pub fn nameLiteral(comptime name: []const u8) [36]u16 {
61-
return comptime blk: {
62-
var buf: [36]u16 = undefined;
63-
const len = std.unicode.utf8ToUtf16Le(&buf, name) catch |err| @compileError(@tagName(err));
64-
@memset(buf[len..], 0);
65-
break :blk &buf;
66-
};
67-
}
101+
/// https://en.wikipedia.org/wiki/GUID_Partition_Table#Partition_type_GUIDs
102+
pub const PartitionType = struct {
103+
pub const unused: Guid = parseGuid("00000000-0000-0000-0000-000000000000") catch unreachable;
104+
105+
pub const esp: Guid = parseGuid("C12A7328-F81F-11D2-BA4B-00A0C93EC93B") catch unreachable;
106+
pub const legacy_mbr: Guid = parseGuid("024DEE41-33E7-11D3-9D69-0008C781F39F") catch unreachable;
107+
pub const bios_boot: Guid = parseGuid("21686148-6449-6E6F-744E-656564454649") catch unreachable;
108+
109+
pub const microsoft_basic_data: Guid = parseGuid("EBD0A0A2-B9E5-4433-87C0-68B6B72699C7") catch unreachable;
110+
pub const microsoft_reserved: Guid = parseGuid("E3C9E316-0B5C-4DB8-817D-F92DF00215AE") catch unreachable;
111+
112+
pub const windows_recovery: Guid = parseGuid("DE94BBA4-06D1-4D40-A16A-BFD50179D6AC") catch unreachable;
113+
114+
pub const plan9: Guid = parseGuid("C91818F9-8025-47AF-89D2-F030D7000C2C") catch unreachable;
115+
116+
pub const linux_swap: Guid = parseGuid("0657FD6D-A4AB-43C4-84E5-0933C84B4F4F") catch unreachable;
117+
pub const linux_fs: Guid = parseGuid("0FC63DAF-8483-4772-8E79-3D69D8477DE4") catch unreachable;
118+
pub const linux_reserved: Guid = parseGuid("8DA63339-0007-60C0-C436-083AC8230908") catch unreachable;
119+
pub const linux_lvm: Guid = parseGuid("E6D6D379-F507-44C2-A23C-238F2A3DF928") catch unreachable;
68120
};
121+
122+
pub fn nameLiteral(comptime name: []const u8) [35:0]u16 {
123+
return comptime blk: {
124+
var buf: [35:0]u16 = undefined;
125+
const len = std.unicode.utf8ToUtf16Le(&buf, name) catch |err| @compileError(@tagName(err));
126+
@memset(buf[len..], 0);
127+
break :blk &buf;
128+
};
129+
}

0 commit comments

Comments
 (0)