opentitanlib/util/
rom_detect.rs

1// Copyright lowRISC contributors (OpenTitan project).
2// Licensed under the Apache License, Version 2.0, see LICENSE for details.
3// SPDX-License-Identifier: Apache-2.0
4
5use anyhow::Result;
6use std::time::Duration;
7
8use crate::io::console::ConsoleExt;
9use crate::io::uart::Uart;
10use crate::regex;
11use crate::util::usr_access::usr_access_get;
12
13pub struct RomDetect {
14    usr_access: u32,
15    timeout: Duration,
16}
17
18impl RomDetect {
19    pub fn new(bitstream: &[u8], timeout: Option<Duration>) -> Result<RomDetect> {
20        Ok(RomDetect {
21            usr_access: usr_access_get(bitstream)?,
22            timeout: timeout.unwrap_or(Duration::MAX),
23        })
24    }
25
26    pub fn detect(&mut self, uart: &dyn Uart) -> Result<bool> {
27        if let Some(cap) = uart.try_wait_for_line(regex!(r"(\w*ROM):([^\r\n]+)"), self.timeout)? {
28            log::info!("Current bitstream: {:?}", cap[0]);
29            let fpga = u32::from_str_radix(&cap[2], 16)?;
30            return Ok(fpga == self.usr_access);
31        }
32
33        log::info!("Did not detect the ROM identification message.");
34        Ok(false)
35    }
36}