diff options
author | Himbeer <himbeer@disroot.org> | 2024-05-18 10:58:01 +0200 |
---|---|---|
committer | Himbeer <himbeer@disroot.org> | 2024-05-18 10:58:01 +0200 |
commit | 573f58487f899563e1ae08fd829eb4053a1f10ac (patch) | |
tree | e051c2169533aea52885c1cd12e0636afe468399 /src/Console.zig | |
parent | 71a8c414d0ffcb6426f440542ea97e54133034f6 (diff) |
logging: Use UART directly if available
Some SBIs don't implement the Debug Console extension. Using the UART is one solution, supporting the SBI Legacy extension might work too if it exists.
Diffstat (limited to 'src/Console.zig')
-rw-r--r-- | src/Console.zig | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/src/Console.zig b/src/Console.zig new file mode 100644 index 0000000..a54bb41 --- /dev/null +++ b/src/Console.zig @@ -0,0 +1,46 @@ +// SPDX-FileCopyrightText: 2024 Himbeer <himbeer@disroot.org> +// +// SPDX-License-Identifier: AGPL-3.0-or-later + +const std = @import("std"); + +const debug_console = @import("sbi/debug_console.zig"); +const fdt = @import("fdt.zig"); +const paging = @import("paging.zig"); +const uart = @import("uart.zig"); + +provider: Provider, + +const Self = @This(); + +pub const Provider = union(enum) { + uart_port: uart.Port.Writer, + sbi_debug: debug_console.Writer, +}; + +pub fn init(kmem: *paging.Table, dt: *const fdt.Tree, allocator: std.mem.Allocator) !void { + if (uart.default == null) { + try uart.init(kmem, dt, allocator); + } +} + +pub fn autoChoose() ?Self { + if (uart.default) |uart_con| { + return .{ + .provider = .{ .uart_port = uart_con.writer() }, + }; + } else if (debug_console.writer()) |sbi_con| { + return .{ + .provider = .{ .sbi_debug = sbi_con }, + }; + } else |_| {} + + return null; +} + +pub fn writer(console: *const Self) std.io.AnyWriter { + switch (console.provider) { + .uart_port => return console.provider.uart_port.any(), + .sbi_debug => return console.provider.sbi_debug.any(), + } +} |