Skip to content

Commit a968bba

Browse files
committed
real implementations are dumb
we need exactly 0x80 entries with the extras zeroed out but still CRCd. its dumb but this fixes it.
1 parent 705993e commit a968bba

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

src/components/part/GptPartitionTable.zig

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -159,15 +159,15 @@ pub fn render(table: *PartTable, stream: *dim.BinaryStream) dim.Content.RenderEr
159159

160160
var pe_ofs: usize = 0;
161161

162-
var next_lba: u64 = 2 + (std.math.divCeil(u64, table.partitions.len * 0x80, block_size) catch |e| switch (e) {
163-
error.DivisionByZero => unreachable,
164-
inline else => |e2| return e2,
165-
});
166-
const pe_end_plus_one_lba = next_lba;
162+
if (table.partitions.len > 0x80) {
163+
std.log.err("gpt with {} (> 128) partitions is not supported by most implementations", .{table.partitions.len});
164+
}
165+
166+
const pe_end_plus_one_lba = 33;
167167
for (table.partitions[0..], 0..) |partition, i| {
168168
@memset(&pe_block, 0);
169169

170-
const offset = partition.offset orelse next_lba * block_size;
170+
const offset = partition.offset orelse 33 * block_size;
171171
const size = partition.size orelse if (i == table.partitions.len - 1)
172172
((max_partition_lba + 1) * block_size) - offset
173173
else
@@ -210,9 +210,18 @@ pub fn render(table: *PartTable, stream: *dim.BinaryStream) dim.Content.RenderEr
210210
var sub_view = try stream.slice(offset, size);
211211
try partition.contains.render(&sub_view);
212212

213-
next_lba = end_lba + 1;
214213
pe_ofs += 0x80;
215214
}
215+
if (table.partitions.len < 0x80) {
216+
@branchHint(.likely);
217+
@memset(&pe_block, 0);
218+
for (table.partitions.len..0x80) |_| {
219+
pe_crc.update(&pe_block);
220+
try stream.write(block_size * 2 + pe_ofs, &pe_block);
221+
try stream.write(block_size * secondary_pe_array_lba + pe_ofs, &pe_block);
222+
pe_ofs += 0x80;
223+
}
224+
}
216225

217226
const pe_array_crc32 = pe_crc.final();
218227

@@ -241,7 +250,7 @@ pub fn render(table: *PartTable, stream: *dim.BinaryStream) dim.Content.RenderEr
241250
std.mem.writeInt(u64, gpt_header[0x30..0x38], max_partition_lba, .little); // Last usable LBA
242251
(table.disk_id orelse Guid.rand(random)).write(gpt_header[0x38..0x48]);
243252
std.mem.writeInt(u64, gpt_header[0x48..0x50], 2, .little); // First LBA of the partition entry array
244-
std.mem.writeInt(u32, gpt_header[0x50..0x54], @intCast(table.partitions.len), .little); // Number of partition entries
253+
std.mem.writeInt(u32, gpt_header[0x50..0x54], 0x80, .little); // Number of partition entries
245254
std.mem.writeInt(u32, gpt_header[0x54..0x58], 0x80, .little); // Size of a partition entry
246255

247256
var backup_gpt_header_block: [block_size]u8 = gpt_header_block;

0 commit comments

Comments
 (0)