1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// Copyright 2022 Red Hat, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Helper types for console argument.

use anyhow::{bail, Context, Error, Result};
use lazy_static::lazy_static;
use regex::Regex;
use serde_with::{DeserializeFromStr, SerializeDisplay};
use std::fmt;
use std::str::FromStr;

const KARG_PREFIX: &str = "console=";

#[derive(Clone, Debug, DeserializeFromStr, SerializeDisplay, PartialEq, Eq)]
pub enum Console {
    Graphical(GraphicalConsole),
    Serial(SerialConsole),
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct GraphicalConsole {
    device: String,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SerialConsole {
    prefix: String,
    port: u8,
    speed: u32,
    data_bits: u8,
    parity: Parity,
    // Linux console doesn't support stop bits
    // GRUB doesn't support RTS/CTS flow control
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
enum Parity {
    None,
    Odd,
    Even,
}

impl Parity {
    fn for_grub(&self) -> &'static str {
        match self {
            Self::None => "no",
            Self::Odd => "odd",
            Self::Even => "even",
        }
    }

    fn for_karg(&self) -> &'static str {
        match self {
            Self::None => "n",
            Self::Odd => "o",
            Self::Even => "e",
        }
    }
}

impl Console {
    pub fn grub_terminal(&self) -> &'static str {
        match self {
            Self::Graphical(_) => "console",
            Self::Serial(_) => "serial",
        }
    }

    pub fn grub_command(&self) -> Option<String> {
        match self {
            Self::Graphical(_) => None,
            Self::Serial(c) => Some(format!(
                "serial --unit={} --speed={} --word={} --parity={}",
                c.port,
                c.speed,
                c.data_bits,
                c.parity.for_grub()
            )),
        }
    }

    pub fn karg(&self) -> String {
        format!("{KARG_PREFIX}{self}")
    }

    /// Write a warning message to stdout if kargs contains "console="
    /// arguments we can parse and no "console=" arguments we can't.  The
    /// warning suggests that users use console_option instead of
    /// karg_option to specify the desired console.
    pub fn maybe_warn_on_kargs(kargs: &[String], karg_option: &str, console_option: &str) {
        use textwrap::{fill, Options, WordSplitter};
        if let Some(args) = Self::maybe_console_args_from_kargs(kargs) {
            // automatically wrap the message, but use Unicode non-breaking
            // spaces to avoid wrapping in the middle of the argument
            // strings, and then replace the non-breaking spaces afterward
            const NBSP: &str = "\u{a0}";
            let msg = format!(
                "Note: consider using \"{}\" instead of \"{}\" to configure both kernel and bootloader consoles.",
                args.iter()
                    .map(|a| format!("{console_option}{NBSP}{a}"))
                    .collect::<Vec<String>>()
                    .join(NBSP),
                args.iter()
                    .map(|a| format!("{karg_option}{NBSP}console={a}"))
                    .collect::<Vec<String>>()
                    .join(NBSP),
            );
            let wrapped = fill(
                &msg,
                Options::new(80)
                    .break_words(false)
                    .word_splitter(WordSplitter::NoHyphenation),
            )
            .replace(NBSP, " ");
            eprintln!("\n{wrapped}\n");
        }
    }

    /// If kargs contains at least one console argument and all console
    /// arguments are parseable as consoles, return a vector of verbatim
    /// (unparsed) console arguments with the console= prefixes removed.
    fn maybe_console_args_from_kargs(kargs: &[String]) -> Option<Vec<&str>> {
        let (parseable, unparseable): (Vec<&str>, Vec<&str>) = kargs
            .iter()
            .filter(|a| a.starts_with(KARG_PREFIX))
            .map(|a| &a[KARG_PREFIX.len()..])
            .partition(|a| Console::from_str(a).is_ok());
        if !parseable.is_empty() && unparseable.is_empty() {
            Some(parseable)
        } else {
            None
        }
    }
}

impl FromStr for Console {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        // help the user with possible misunderstandings
        for prefix in [KARG_PREFIX, "/dev/"] {
            if s.starts_with(prefix) {
                bail!(r#"spec should not start with "{prefix}""#);
            }
        }

        // first, parse serial console parameters
        lazy_static! {
            static ref SERIAL_REGEX: Regex = Regex::new("^(?P<prefix>ttyS|ttyAMA)(?P<port>[0-9]+)(?:,(?P<speed>[0-9]+)(?:(?P<parity>n|o|e)(?P<data_bits>[5-8])?)?)?$").expect("compiling console regex");
        }
        if let Some(c) = SERIAL_REGEX.captures(s) {
            return Ok(Console::Serial(SerialConsole {
                prefix: c
                    .name("prefix")
                    .expect("prefix is mandatory")
                    .as_str()
                    .to_string(),
                port: c
                    .name("port")
                    .expect("port is mandatory")
                    .as_str()
                    .parse()
                    .context("couldn't parse port")?,
                speed: c
                    .name("speed")
                    .map(|v| v.as_str().parse().context("couldn't parse speed"))
                    .unwrap_or(Ok(9600))?,
                data_bits: c
                    .name("data_bits")
                    .map(|v| v.as_str().parse().expect("unexpected data bits"))
                    .unwrap_or(8),
                parity: match c.name("parity").map(|v| v.as_str()) {
                    // default
                    None => Parity::None,
                    Some("n") => Parity::None,
                    Some("e") => Parity::Even,
                    Some("o") => Parity::Odd,
                    _ => unreachable!(),
                },
            }));
        }

        // then try hardcoded strings for graphical consoles
        match s {
            "tty0" | "hvc0" | "ttysclp0" => Ok(Console::Graphical(GraphicalConsole {
                device: s.to_string(),
            })),
            _ => bail!("invalid or unsupported console argument"),
        }
    }
}

impl fmt::Display for Console {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Graphical(c) => write!(f, "{}", c.device),
            Self::Serial(c) => write!(
                f,
                "{}{},{}{}{}",
                c.prefix,
                c.port,
                c.speed,
                c.parity.for_karg(),
                c.data_bits
            ),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn valid_console_args() {
        let cases = vec![
            ("tty0", "console=tty0", "console", None),
            ("hvc0", "console=hvc0", "console", None),
            ("ttysclp0", "console=ttysclp0", "console", None),
            (
                "ttyS1",
                "console=ttyS1,9600n8",
                "serial",
                Some("serial --unit=1 --speed=9600 --word=8 --parity=no"),
            ),
            (
                "ttyAMA1",
                "console=ttyAMA1,9600n8",
                "serial",
                Some("serial --unit=1 --speed=9600 --word=8 --parity=no"),
            ),
            (
                "ttyS1,1234567e5",
                "console=ttyS1,1234567e5",
                "serial",
                Some("serial --unit=1 --speed=1234567 --word=5 --parity=even"),
            ),
            (
                "ttyS2,5o",
                "console=ttyS2,5o8",
                "serial",
                Some("serial --unit=2 --speed=5 --word=8 --parity=odd"),
            ),
            (
                "ttyS3,17",
                "console=ttyS3,17n8",
                "serial",
                Some("serial --unit=3 --speed=17 --word=8 --parity=no"),
            ),
        ];
        for (input, karg, grub_terminal, grub_command) in cases {
            let console = Console::from_str(input).unwrap();
            assert_eq!(
                console.grub_terminal(),
                grub_terminal,
                "GRUB terminal for {input}"
            );
            assert_eq!(
                console.grub_command().as_deref(),
                grub_command,
                "GRUB command for {input}"
            );
            assert_eq!(console.karg(), karg, "karg for {input}");
        }
    }

    #[test]
    fn invalid_console_args() {
        let cases = vec![
            "foo",
            "/dev/tty0",
            "/dev/ttyS0",
            "console=tty0",
            "console=ttyS0",
            "ztty0",
            "zttyS0",
            "tty0z",
            "ttyS0z",
            "tty1",
            "hvc1",
            "ttysclp1",
            "ttyS0,",
            "ttyS0,z",
            "ttyS0,115200p8",
            "ttyS0,115200n4",
            "ttyS0,115200n8r",
            "ttyB0",
            "ttyS9999999999999999999",
            "ttyS0,999999999999999999999",
        ];
        for input in cases {
            Console::from_str(input).unwrap_err();
        }
    }

    #[test]
    fn maybe_console_args_from_kargs() {
        assert_eq!(
            Console::maybe_console_args_from_kargs(&[
                "foo".into(),
                "console=ttyS0".into(),
                "bar".into()
            ]),
            Some(vec!["ttyS0"])
        );
        assert_eq!(
            Console::maybe_console_args_from_kargs(&[
                "foo".into(),
                "console=ttyS0".into(),
                "console=tty0".into(),
                "console=tty0".into(),
                "console=ttyAMA1,115200n8".into(),
                "bar".into()
            ]),
            Some(vec!["ttyS0", "tty0", "tty0", "ttyAMA1,115200n8"])
        );
        assert_eq!(
            Console::maybe_console_args_from_kargs(&[
                "foo".into(),
                "console=ttyS0".into(),
                "console=ttyS1z".into(),
                "console=tty0".into(),
                "bar".into()
            ]),
            None
        );
        assert_eq!(
            Console::maybe_console_args_from_kargs(&["foo".into(), "bar".into()]),
            None
        );
        assert_eq!(Console::maybe_console_args_from_kargs(&[]), None);
    }
}