Skip to content

std: implement basic io for UEFI #22226

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion lib/compiler/test_runner.zig
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var fba = std.heap.FixedBufferAllocator.init(&fba_buffer);

const crippled = switch (builtin.zig_backend) {
.stage2_riscv64 => true,
else => false,
else => builtin.os.tag == .uefi,
};

pub fn main() void {
Expand Down
8 changes: 8 additions & 0 deletions lib/std/Thread.zig
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ else if (native_os == .linux)
LinuxThreadImpl
else if (native_os == .wasi)
WasiThreadImpl
else if (native_os == .uefi)
UefiThreadImpl
else
UnsupportedImpl;

Expand Down Expand Up @@ -1154,6 +1156,12 @@ const WasiThreadImpl = struct {
}
};

const UefiThreadImpl = struct {
fn getCurrentId() Id {
return 1;
}
};

const LinuxThreadImpl = struct {
const linux = std.os.linux;

Expand Down
10 changes: 10 additions & 0 deletions lib/std/debug/SelfInfo.zig
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ pub fn getModuleForAddress(self: *SelfInfo, address: usize) !*Module {
return self.lookupModuleHaiku(address);
} else if (builtin.target.cpu.arch.isWasm()) {
return self.lookupModuleWasm(address);
} else if (native_os == .uefi) {
return self.lookupModuleUefi(address);
} else {
return self.lookupModuleDl(address);
}
Expand All @@ -146,6 +148,8 @@ pub fn getModuleNameForAddress(self: *SelfInfo, address: usize) ?[]const u8 {
return null;
} else if (builtin.target.cpu.arch.isWasm()) {
return null;
} else if (native_os == .uefi) {
return null;
} else {
return self.lookupModuleNameDl(address);
}
Expand Down Expand Up @@ -494,6 +498,12 @@ fn lookupModuleHaiku(self: *SelfInfo, address: usize) !*Module {
@panic("TODO implement lookup module for Haiku");
}

fn lookupModuleUefi(self: *SelfInfo, address: usize) !*Module {
_ = self;
_ = address;
@panic("TODO implement lookup module for UEFI");
}

fn lookupModuleWasm(self: *SelfInfo, address: usize) !*Module {
_ = self;
_ = address;
Expand Down
2 changes: 1 addition & 1 deletion lib/std/fs/Dir.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2785,4 +2785,4 @@ const assert = std.debug.assert;
const linux = std.os.linux;
const windows = std.os.windows;
const native_os = builtin.os.tag;
const have_flock = @TypeOf(posix.system.flock) != void;
const have_flock = @hasDecl(posix.system, "flock") and @TypeOf(posix.system.flock) != void;
4 changes: 4 additions & 0 deletions lib/std/fs/File.zig
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,10 @@ pub fn supportsAnsiEscapeCodes(self: File) bool {
// stderr is always sanitized.
return false;
}
if (builtin.os.tag == .uefi) {
// UEFI has it's own way of terminal control without ANSI.
return false;
}
if (self.isTty()) {
if (self.handle == posix.STDOUT_FILENO or self.handle == posix.STDERR_FILENO) {
if (posix.getenvZ("TERM")) |term| {
Expand Down
13 changes: 13 additions & 0 deletions lib/std/io.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const builtin = @import("builtin");
const root = @import("root");
const c = std.c;
const is_windows = builtin.os.tag == .windows;
const is_uefi = builtin.os.tag == .uefi;
const windows = std.os.windows;
const posix = std.posix;

Expand All @@ -19,6 +20,10 @@ fn getStdOutHandle() posix.fd_t {
return windows.peb().ProcessParameters.hStdOutput;
}

if (is_uefi) {
return .{ .simple_output = std.os.uefi.system_table.con_out.? };
}

if (@hasDecl(root, "os") and @hasDecl(root.os, "io") and @hasDecl(root.os.io, "getStdOutHandle")) {
return root.os.io.getStdOutHandle();
}
Expand All @@ -35,6 +40,10 @@ fn getStdErrHandle() posix.fd_t {
return windows.peb().ProcessParameters.hStdError;
}

if (is_uefi) {
return .{ .simple_output = std.os.uefi.system_table.std_err.? };
}

if (@hasDecl(root, "os") and @hasDecl(root.os, "io") and @hasDecl(root.os.io, "getStdErrHandle")) {
return root.os.io.getStdErrHandle();
}
Expand All @@ -51,6 +60,10 @@ fn getStdInHandle() posix.fd_t {
return windows.peb().ProcessParameters.hStdInput;
}

if (is_uefi) {
return .{ .simple_output = std.os.uefi.system_table.con_in.? };
}

if (@hasDecl(root, "os") and @hasDecl(root.os, "io") and @hasDecl(root.os.io, "getStdInHandle")) {
return root.os.io.getStdInHandle();
}
Expand Down
4 changes: 4 additions & 0 deletions lib/std/os/uefi.zig
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ pub const Status = @import("uefi/status.zig").Status;
pub const Error = UnexpectedError || Status.Error;
pub const tables = @import("uefi/tables.zig");

pub const posix = @import("uefi/posix.zig");

pub var working_directory: posix.fd_t = .none;

/// The memory type to allocate when using the pool.
/// Defaults to `.loader_data`, the default data allocation type
/// used by UEFI applications to allocate pool memory.
Expand Down
Loading
Loading