Skip to content

Commit cab8269

Browse files
committed
more memory loading
Signed-off-by: Fredrik Lönnegren <[email protected]>
1 parent 47c4a0e commit cab8269

File tree

3 files changed

+48
-15
lines changed

3 files changed

+48
-15
lines changed

Dockerfile

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,4 @@ RUN zypper install -y kernel-default dracut systemd && \
44
dracut --regenerate-all -f
55

66
FROM scratch
7-
COPY --from=build /boot/vmlinu* /boot
8-
COPY --from=build /boot/init* /boot
7+
COPY --from=build /boot /boot

src/main.zig

+43-8
Original file line numberDiff line numberDiff line change
@@ -101,31 +101,33 @@ pub fn efi_main() !uefi.Status {
101101
return e;
102102
};
103103

104-
const fileinfo = try tar.stat(tar_fp.reader(), "boot/vmlinuz");
105-
try console.printf("Found kernel: {s}\n", .{fileinfo.?.filename()});
106-
104+
const link = try tar.stat(tar_fp.reader(), "boot/vmlinuz");
105+
try console.printf("Found kernel symlink: {s}\n", .{link.?.filename()});
107106
try tar_fp.setPosition(0).err();
108107

108+
const fileinfo = try tar.stat(tar_fp.reader(), link.?.filename());
109+
try console.printf("Found kernel: {s} with size {}\n", .{ fileinfo.?.filename(), fileinfo.?.size });
110+
try tar_fp.setPosition(0).err();
111+
const size = fileinfo.?.size;
109112
var kernel: [*]align(8) u8 = undefined;
110-
const const_size = 100 * 1000 * 1000;
111-
try uefi.system_table.boot_services.?.allocatePool(uefi.efi_pool_memory_type, const_size, &kernel).err();
113+
try uefi.system_table.boot_services.?.allocatePool(uefi.efi_pool_memory_type, size, &kernel).err();
112114
defer uefi.system_table.boot_services.?.freePool(kernel).err() catch {};
113-
const size = try tar.readFile(tar_fp.reader(), fileinfo.?.filename(), kernel);
115+
_ = try tar.readFile(tar_fp.reader(), fileinfo.?.filename(), kernel);
114116

115117
try console.printf("Kernel size {}\n", .{size});
116118

117119
var args = try std.mem.concat(
118120
pool_alloc,
119121
u16,
120122
&[_][]const u16{
121-
utf16str("root=testitest console=ttyS0"),
123+
utf16str("root=/dev/sda console=ttyS0"),
122124
&[_]u16{0},
123125
},
124126
);
125127
defer pool_alloc.free(args);
126128

127129
var img: ?uefi.Handle = undefined;
128-
try boot_services.loadImage(false, uefi.handle, null, kernel, const_size, &img).err();
130+
try boot_services.loadImage(false, uefi.handle, null, kernel, size, &img).err();
129131

130132
try console.printf("Loaded image\n", .{});
131133

@@ -144,3 +146,36 @@ pub fn efi_main() !uefi.Status {
144146

145147
return uefi.Status.Success;
146148
}
149+
150+
pub fn create_memory_device_path(self: *proto.DevicePath, allocator: Allocator, path: [:0]align(1) const u16) !*proto.DevicePath {
151+
var path_size = self.size();
152+
153+
// 2 * (path.len + 1) for the path and its null terminator, which are u16s
154+
// DevicePath for the extra node before the end
155+
var buf = try allocator.alloc(u8, path_size + 2 * (path.len + 1) + @sizeOf(proto.DevicePath));
156+
157+
@memcpy(buf[0..path_size], @as([*]const u8, @ptrCast(self))[0..path_size]);
158+
159+
// Pointer to the copy of the end node of the current chain, which is - 4 from the buffer
160+
// as the end node itself is 4 bytes (type: u8 + subtype: u8 + length: u16).
161+
var new = @as(*uefi.DevicePath.Hardware.MemoryMappedDevicePath, @ptrCast(buf.ptr + path_size - 4));
162+
163+
new.type = .Hardware;
164+
new.subtype = .MemoryMapped;
165+
new.length = @sizeOf(uefi.DevicePath.Hardware.MemoryMappedDevicePath) + 2 * (@as(u16, @intCast(path.len)) + 1);
166+
167+
// The same as new.getPath(), but not const as we're filling it in.
168+
var ptr = @as([*:0]align(1) u16, @ptrCast(@as([*]u8, @ptrCast(new)) + @sizeOf(uefi.DevicePath.Hardware.MemoryMappedDevicePath)));
169+
170+
for (path, 0..) |s, i|
171+
ptr[i] = s;
172+
173+
ptr[path.len] = 0;
174+
175+
var end = @as(*uefi.DevicePath.End.EndEntireDevicePath, @ptrCast(@as(*proto.DevicePath, @ptrCast(new)).next().?));
176+
end.type = .End;
177+
end.subtype = .EndEntire;
178+
end.length = @sizeOf(uefi.DevicePath.End.EndEntireDevicePath);
179+
180+
return @as(*proto.DevicePath, @ptrCast(buf.ptr));
181+
}

src/tar.zig

+4-5
Original file line numberDiff line numberDiff line change
@@ -165,15 +165,14 @@ pub fn stat(reader: anytype, fileName: []const u8) !?FileInfo {
165165
.normal => {
166166
if (file_size == 0 and unstripped_file_name.len == 0) return null;
167167

168-
if (std.mem.eql(u8, fileName, unstripped_file_name)) {
169-
try console.printf("Found {s}\n", .{fileName});
170-
const link = header.name();
168+
if (std.mem.endsWith(u8, unstripped_file_name, fileName)) {
169+
try console.printf("Found normal {s}\n", .{fileName});
171170
var ret = FileInfo{
172171
.file_name = undefined,
173-
.len = link.len,
172+
.len = unstripped_file_name.len,
174173
.size = file_size,
175174
};
176-
@memcpy(ret.file_name[0..link.len], link);
175+
@memcpy(ret.file_name[0..unstripped_file_name.len], unstripped_file_name);
177176
return ret;
178177
}
179178

0 commit comments

Comments
 (0)