blob: 6d9a4feef3480f8e8f6409b12be5d89e0fc89544 [file] [log] [blame]
Andrew Walbranf2594882022-03-15 17:32:53 +00001// Copyright 2022, The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Console driver for 8250 UART.
16
Michał Mazurekb366b732024-11-29 15:12:11 +010017use crate::arch::platform;
Andrew Walbranf86d4b02024-07-19 17:23:18 +010018use core::fmt::{write, Arguments, Write};
Andrew Walbranf2594882022-03-15 17:32:53 +000019
Pierre-Clément Tosi075aade2024-06-18 20:08:13 +010020/// Writes a formatted string followed by a newline to the n-th console.
Andrew Walbranf2594882022-03-15 17:32:53 +000021///
Pierre-Clément Tosi075aade2024-06-18 20:08:13 +010022/// Panics if the n-th console was not initialized by calling [`init`] first.
Pierre-Clément Tosib5a3ab12023-09-15 11:18:38 +010023pub fn writeln(n: usize, format_args: Arguments) {
Michał Mazurekb366b732024-11-29 15:12:11 +010024 let uart = &mut *platform::uart(n).lock();
Pierre-Clément Tosibc9fb7e2024-06-19 17:28:47 +010025 write(uart, format_args).unwrap();
26 let _ = uart.write_str("\n");
Andrew Walbranf2594882022-03-15 17:32:53 +000027}
28
Michał Mazurekb366b732024-11-29 15:12:11 +010029/// Reinitializes the emergency UART driver and writes a formatted string followed by a newline to
30/// it.
Andrew Walbranf2594882022-03-15 17:32:53 +000031///
32/// This is intended for use in situations where the UART may be in an unknown state or the global
33/// instance may be locked, such as in an exception handler or panic handler.
Michał Mazurekb366b732024-11-29 15:12:11 +010034pub fn ewriteln(format_args: Arguments) {
35 let mut uart = platform::emergency_uart();
Andrew Walbranc1bcb3c2022-03-31 13:13:57 +000036 let _ = write(&mut uart, format_args);
Pierre-Clément Tosibc9fb7e2024-06-19 17:28:47 +010037 let _ = uart.write_str("\n");
Andrew Walbranc1bcb3c2022-03-31 13:13:57 +000038}
39
Pierre-Clément Tosib5a3ab12023-09-15 11:18:38 +010040/// Prints the given formatted string to the n-th console, followed by a newline.
41///
42/// Panics if the console has not yet been initialized. May hang if used in an exception context;
43/// use `eprintln!` instead.
44#[macro_export]
45macro_rules! console_writeln {
46 ($n:expr, $($arg:tt)*) => ({
47 $crate::console::writeln($n, format_args!($($arg)*))
48 })
49}
50
51pub(crate) use console_writeln;
52
Andrew Walbranc1bcb3c2022-03-31 13:13:57 +000053/// Prints the given formatted string to the console, followed by a newline.
54///
Alice Wangeff58392023-07-04 13:32:09 +000055/// Panics if the console has not yet been initialized. May hang if used in an exception context;
Andrew Walbranc1bcb3c2022-03-31 13:13:57 +000056/// use `eprintln!` instead.
Andrew Walbranc1bcb3c2022-03-31 13:13:57 +000057macro_rules! println {
Pierre-Clément Tosi075aade2024-06-18 20:08:13 +010058 ($($arg:tt)*) => ({
Michał Mazurekb366b732024-11-29 15:12:11 +010059 $crate::console::console_writeln!($crate::arch::platform::DEFAULT_CONSOLE_INDEX, $($arg)*)
Pierre-Clément Tosi075aade2024-06-18 20:08:13 +010060 })
Andrew Walbranc1bcb3c2022-03-31 13:13:57 +000061}
62
Jakob Vukalovicef996292023-04-13 14:28:34 +000063pub(crate) use println; // Make it available in this crate.
64
Andrew Walbranc1bcb3c2022-03-31 13:13:57 +000065/// Prints the given string followed by a newline to the console in an emergency, such as an
66/// exception handler.
67///
68/// Never panics.
69#[macro_export]
70macro_rules! eprintln {
Pierre-Clément Tosi075aade2024-06-18 20:08:13 +010071 ($($arg:tt)*) => ({
Michał Mazurekb366b732024-11-29 15:12:11 +010072 $crate::console::ewriteln(format_args!($($arg)*))
Pierre-Clément Tosi075aade2024-06-18 20:08:13 +010073 })
Andrew Walbranc1bcb3c2022-03-31 13:13:57 +000074}