//! K414: a real PL011 unknown-command error must not poison the next line. use super::*; use turkos_kernel::serial_console::{ConsoleEvent, SerialConsole}; const RXFE: u32 = 1 << 4; const DATA_ERROR: u32 = 0x0f00; fn fail(reason: &str) -> ! { write_str("TURKOS/UNKNOWN-FAIL: "); write_line(reason); park() } fn ticks() -> u64 { let value: u64; unsafe { asm!("mrs {value}, CNTPCT_EL0", value = out(reg) value, options(nomem, nostack, preserves_flags)) }; value } fn prompt(number: u64) { write_str("TURKOS/UNKNOWN-PROMPT: NUMBER=0x"); write_hex(number as usize); write_str("\r\n"); } pub(super) fn smoke_test() { let base = UART_BASE.load(Ordering::Relaxed); let frequency: u64; unsafe { asm!("mrs {value}, CNTFRQ_EL0", value = out(reg) frequency, options(nomem, nostack, preserves_flags)) }; if frequency == 0 { fail("zero counter frequency"); } let mut console = SerialConsole::new(); let mut stage = 0usize; let mut index = 0usize; let mut suppressed = 0usize; prompt(console.prompts()); write_line("TURKOS/UNKNOWN: READY"); loop { let deadline = ticks() .checked_add(frequency.saturating_mul(5)) .unwrap_or(u64::MAX); while unsafe { read_volatile((base + 0x18) as *const u32) } & RXFE != 0 { if ticks() >= deadline { fail("input timeout"); } core::hint::spin_loop(); } let data = unsafe { read_volatile(base as *const u32) }; if data & DATA_ERROR != 0 { fail("PL011 data error"); } let byte = data as u8; let event = match console.feed(byte) { ConsoleEvent::Input => "INPUT", ConsoleEvent::SuppressedLf => { suppressed += 1; "SUPPRESS" } ConsoleEvent::Unhandled(bytes, len) => { if (stage != 0 && stage != 2) || len == 0 || len > 16 { fail("unknown stage or length"); } write_str("TURKOS/UNKNOWN-ERROR: LENGTH=0x"); write_hex(len); write_str(" HEX="); for value in &bytes[..len] { write_hex_byte(*value); } write_str("\r\n"); prompt(console.prompts()); stage += 1; "UNHANDLED" } ConsoleEvent::Help { prompt: number } => { if stage != 1 { fail("help stage"); } write_str("TURKOS/UNKNOWN-HELP: COUNT=0x"); write_hex(console.helps() as usize); write_str("\r\n"); prompt(number); stage += 1; "HELP" } ConsoleEvent::Echo { prompt: number, bytes, len, } => { if stage != 3 || &bytes[..len] != b"OK" { fail("echo stage or bytes"); } write_line("TURKOS/UNKNOWN-ECHO: HEX=4f4b"); prompt(number); stage += 1; "ECHO" } _ => fail("unexpected command or overlong line"), }; write_str("TURKOS/UNKNOWN-BYTE: INDEX=0x"); write_hex(index); write_str(" VALUE=0x"); write_hex(byte as usize); write_str(" EVENT="); write_str(event); write_str(" PROMPTS=0x"); write_hex(console.prompts() as usize); write_str("\r\n"); index += 1; if index > 64 { fail("too many input bytes"); } if stage == 4 { if console.prompts() != 5 || console.unknowns() != 2 || console.helps() != 1 || console.echos() != 1 || console.waits() != 0 || console.runs() != 0 || suppressed != 1 { fail("recovery accounting"); } write_line( "TURKOS/UNKNOWN: PASS ERRORS=0x2 HELPS=0x1 ECHOS=0x1 PROMPTS=0x5 SUPPRESSED=0x1", ); park(); } } }