mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-18 08:52:45 +00:00
Merge pull request #881 from betrusted-io/u2f-concurrency
remove potential race condition in sending U2F packets over USB
This commit is contained in:
commit
b439d312a4
3 changed files with 87 additions and 41 deletions
|
|
@ -432,6 +432,7 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! {
|
||||||
let mut bw_thresh: u8 = 128;
|
let mut bw_thresh: u8 = 128;
|
||||||
let mut qr_request: Option<xous::MessageEnvelope> = None;
|
let mut qr_request: Option<xous::MessageEnvelope> = None;
|
||||||
let mut kbd_listeners: Vec<(CID, usize)> = Vec::new();
|
let mut kbd_listeners: Vec<(CID, usize)> = Vec::new();
|
||||||
|
let cam_started = Arc::new(AtomicBool::new(false));
|
||||||
#[allow(unused_mut)]
|
#[allow(unused_mut)]
|
||||||
let mut orientation = DisplayOrientation::Normal;
|
let mut orientation = DisplayOrientation::Normal;
|
||||||
loop {
|
loop {
|
||||||
|
|
@ -445,41 +446,6 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! {
|
||||||
#[cfg(not(feature = "hosted-baosec"))]
|
#[cfg(not(feature = "hosted-baosec"))]
|
||||||
GfxOpcode::AcquireQr => {
|
GfxOpcode::AcquireQr => {
|
||||||
if qr_request.is_none() {
|
if qr_request.is_none() {
|
||||||
// reset camera UDMA block
|
|
||||||
udma_global.reset(PeriphId::Cam);
|
|
||||||
|
|
||||||
// display.stash(); // save a copy of the UI
|
|
||||||
// this will defer response until later
|
|
||||||
qr_request = msg_opt.take();
|
|
||||||
|
|
||||||
// power up the camera
|
|
||||||
// starts MCLK
|
|
||||||
iox.setup_pin(
|
|
||||||
cam_clk.0,
|
|
||||||
cam_clk.1,
|
|
||||||
Some(IoxDir::Output),
|
|
||||||
Some(IoxFunction::AF3),
|
|
||||||
None,
|
|
||||||
None,
|
|
||||||
Some(IoxEnable::Disable),
|
|
||||||
Some(IoxDriveStrength::Drive8mA),
|
|
||||||
);
|
|
||||||
timer.wo(utra::pwm::REG_CH_EN, 1);
|
|
||||||
timer.rmwf(utra::pwm::REG_TIM0_CMD_R_TIMER0_START, 1);
|
|
||||||
tt.sleep_ms(2).ok(); // wait for camera to clock-up
|
|
||||||
// bring camera out of powerdown
|
|
||||||
iox.set_gpio_pin_value(cam_pdwn.0, cam_pdwn.1, IoxValue::Low);
|
|
||||||
tt.sleep_ms(3).ok(); // wait for camera to power-up
|
|
||||||
let (pid, mid) = cam.read_id(&mut i2c);
|
|
||||||
log::info!("Camera pid {:x}, mid {:x}", pid, mid);
|
|
||||||
cam.init(&mut i2c, bao1x_api::camera::Resolution::Res320x240);
|
|
||||||
tt.sleep_ms(1).ok();
|
|
||||||
|
|
||||||
let (cols, _rows) = cam.resolution();
|
|
||||||
let border = (cols - IMAGE_WIDTH) / 2;
|
|
||||||
cam.set_slicing((border, 0), (cols - border, IMAGE_HEIGHT));
|
|
||||||
log::info!("320x240 resolution setup with 256x240 slicing");
|
|
||||||
|
|
||||||
// decode dummy data - what this does is load the swapped out QR decoding logic, thus
|
// decode dummy data - what this does is load the swapped out QR decoding logic, thus
|
||||||
// improving the latency of the decoder on the "first hit". The sole purpose of this
|
// improving the latency of the decoder on the "first hit". The sole purpose of this
|
||||||
// is to improve the user experience during scanning.
|
// is to improve the user experience during scanning.
|
||||||
|
|
@ -505,18 +471,84 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! {
|
||||||
} else {
|
} else {
|
||||||
log::error!("test image failed to decode, this shouldn't happen!");
|
log::error!("test image failed to decode, this shouldn't happen!");
|
||||||
}
|
}
|
||||||
|
|
||||||
// turning off preemption makes camera acquisition smoother; the OS will naturally try
|
|
||||||
// to schedule other tasks between camera frames after each
|
|
||||||
// CamIrq interrupt
|
|
||||||
hal.set_preemption(false);
|
|
||||||
// fix orientation if it's upside down
|
// fix orientation if it's upside down
|
||||||
if orientation == DisplayOrientation::UpsideDown {
|
if orientation == DisplayOrientation::UpsideDown {
|
||||||
display
|
display
|
||||||
.flip_vertical(false)
|
.flip_vertical(false)
|
||||||
.unwrap_or_else(|_| display_timeout_handler(&udma_global, &mut display))
|
.unwrap_or_else(|_| display_timeout_handler(&udma_global, &mut display))
|
||||||
}
|
}
|
||||||
// now start acquisition
|
|
||||||
|
// reset camera UDMA block
|
||||||
|
udma_global.reset(PeriphId::Cam);
|
||||||
|
|
||||||
|
// display.stash(); // save a copy of the UI
|
||||||
|
// this will defer response until later
|
||||||
|
qr_request = msg_opt.take();
|
||||||
|
|
||||||
|
// power up the camera
|
||||||
|
// starts MCLK
|
||||||
|
iox.setup_pin(
|
||||||
|
cam_clk.0,
|
||||||
|
cam_clk.1,
|
||||||
|
Some(IoxDir::Output),
|
||||||
|
Some(IoxFunction::AF3),
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
Some(IoxEnable::Disable),
|
||||||
|
Some(IoxDriveStrength::Drive8mA),
|
||||||
|
);
|
||||||
|
timer.wo(utra::pwm::REG_CH_EN, 1);
|
||||||
|
timer.rmwf(utra::pwm::REG_TIM0_CMD_R_TIMER0_START, 1);
|
||||||
|
tt.sleep_ms(5).ok(); // wait for camera to clock-up
|
||||||
|
// bring camera out of powerdown
|
||||||
|
iox.set_gpio_pin_value(cam_pdwn.0, cam_pdwn.1, IoxValue::Low);
|
||||||
|
tt.sleep_ms(10).ok(); // wait for camera to power-up
|
||||||
|
let (pid, mid) = cam.read_id(&mut i2c);
|
||||||
|
log::info!("Camera pid {:x}, mid {:x}", pid, mid);
|
||||||
|
cam.init(&mut i2c, bao1x_api::camera::Resolution::Res320x240);
|
||||||
|
tt.sleep_ms(5).ok();
|
||||||
|
|
||||||
|
let (cols, _rows) = cam.resolution();
|
||||||
|
let border = (cols - IMAGE_WIDTH) / 2;
|
||||||
|
cam.set_slicing((border, 0), (cols - border, IMAGE_HEIGHT));
|
||||||
|
log::info!("320x240 resolution setup with 256x240 slicing");
|
||||||
|
|
||||||
|
cam_started.store(false, Ordering::SeqCst);
|
||||||
|
|
||||||
|
// start watchdog to make sure camera is running
|
||||||
|
let _ = std::thread::spawn({
|
||||||
|
let cid = cid.clone();
|
||||||
|
let cam_started = cam_started.clone();
|
||||||
|
move || {
|
||||||
|
let hal = Hal::new();
|
||||||
|
let start = std::time::Instant::now();
|
||||||
|
while !cam_started.load(Ordering::SeqCst)
|
||||||
|
&& std::time::Instant::now().duration_since(start).as_millis() < 600
|
||||||
|
{
|
||||||
|
std::thread::sleep(std::time::Duration::from_millis(30));
|
||||||
|
}
|
||||||
|
if !cam_started.load(Ordering::SeqCst) {
|
||||||
|
// force an IRQ into the pipe to start the camera
|
||||||
|
xous::try_send_message(
|
||||||
|
cid,
|
||||||
|
xous::Message::new_scalar(
|
||||||
|
GfxOpcode::CamIrq.to_usize().unwrap(),
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.ok();
|
||||||
|
}
|
||||||
|
// turning off preemption makes camera acquisition smoother; the OS will
|
||||||
|
// naturally try to schedule other tasks
|
||||||
|
// between camera frames after each CamIrq interrupt
|
||||||
|
hal.set_preemption(false);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// now start an acquisition
|
||||||
cam.capture_async();
|
cam.capture_async();
|
||||||
}
|
}
|
||||||
// if qr_request is already pending, ignore any new acquisition requests
|
// if qr_request is already pending, ignore any new acquisition requests
|
||||||
|
|
@ -567,6 +599,7 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
GfxOpcode::CamIrq => {
|
GfxOpcode::CamIrq => {
|
||||||
|
cam_started.store(true, Ordering::SeqCst);
|
||||||
// copy the camera data to our FB
|
// copy the camera data to our FB
|
||||||
let fb: &[u32] = cam.rx_buf();
|
let fb: &[u32] = cam.rx_buf();
|
||||||
// fb is an array of IMAGE_WIDTH x IMAGE_HEIGHT x u16
|
// fb is an array of IMAGE_WIDTH x IMAGE_HEIGHT x u16
|
||||||
|
|
|
||||||
|
|
@ -57,6 +57,7 @@ pub struct Bao1xUsb<'a> {
|
||||||
// from the interrupt handler.
|
// from the interrupt handler.
|
||||||
pub double_lock: AtomicBool,
|
pub double_lock: AtomicBool,
|
||||||
pub led_state: KeyboardLedsReport,
|
pub led_state: KeyboardLedsReport,
|
||||||
|
pub irq_serviced: AtomicBool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Bao1xUsb<'a> {
|
impl<'a> Bao1xUsb<'a> {
|
||||||
|
|
@ -122,6 +123,7 @@ impl<'a> Bao1xUsb<'a> {
|
||||||
serial_rx: [0u8; SERIAL_MAX_PACKET_SIZE],
|
serial_rx: [0u8; SERIAL_MAX_PACKET_SIZE],
|
||||||
double_lock: AtomicBool::new(false),
|
double_lock: AtomicBool::new(false),
|
||||||
led_state: KeyboardLedsReport::default(),
|
led_state: KeyboardLedsReport::default(),
|
||||||
|
irq_serviced: AtomicBool::new(false),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -355,6 +357,7 @@ pub(crate) fn composite_handler(_irq_no: usize, arg: *mut usize) {
|
||||||
while let Some(u2f_msg) = usb.fido_tx_queue.borrow_mut().pop_front() {
|
while let Some(u2f_msg) = usb.fido_tx_queue.borrow_mut().pop_front() {
|
||||||
u2f.write_report(&u2f_msg).ok();
|
u2f.write_report(&u2f_msg).ok();
|
||||||
}
|
}
|
||||||
|
usb.irq_serviced.store(true, Ordering::SeqCst);
|
||||||
}
|
}
|
||||||
Some(UsbIrqReq::KbdTx) => {
|
Some(UsbIrqReq::KbdTx) => {
|
||||||
let keyboard = composite.device::<NKROBootKeyboard<'_, _>, _>();
|
let keyboard = composite.device::<NKROBootKeyboard<'_, _>, _>();
|
||||||
|
|
|
||||||
|
|
@ -441,6 +441,7 @@ pub(crate) fn main_hw() -> ! {
|
||||||
unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) };
|
unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) };
|
||||||
let mut u2f_ipc = buffer.to_original::<U2fMsgIpc, _>().unwrap();
|
let mut u2f_ipc = buffer.to_original::<U2fMsgIpc, _>().unwrap();
|
||||||
if cu.device.state() != usb_device::device::UsbDeviceState::Configured {
|
if cu.device.state() != usb_device::device::UsbDeviceState::Configured {
|
||||||
|
log::warn!("U2fTx: HANGUP");
|
||||||
u2f_ipc.code = U2fCode::Hangup;
|
u2f_ipc.code = U2fCode::Hangup;
|
||||||
buffer.replace(u2f_ipc).unwrap();
|
buffer.replace(u2f_ipc).unwrap();
|
||||||
continue;
|
continue;
|
||||||
|
|
@ -454,6 +455,15 @@ pub(crate) fn main_hw() -> ! {
|
||||||
cu.fido_tx_queue.borrow_mut().push_back(u2f_msg);
|
cu.fido_tx_queue.borrow_mut().push_back(u2f_msg);
|
||||||
}
|
}
|
||||||
cu.sw_irq(UsbIrqReq::FidoTx);
|
cu.sw_irq(UsbIrqReq::FidoTx);
|
||||||
|
// ensure that the interrupt has happened, because the interrupt can create
|
||||||
|
// concurrency issues on the fido_tx_queue if we've re-entered this function
|
||||||
|
// before the interrupt has processed
|
||||||
|
while !cu.irq_serviced.load(Ordering::SeqCst) {
|
||||||
|
xous::yield_slice();
|
||||||
|
}
|
||||||
|
// clear the serviced flag here - this is OK because this is the only other thread
|
||||||
|
// that can access the variable.
|
||||||
|
cu.irq_serviced.store(false, Ordering::SeqCst);
|
||||||
log::debug!("enqueued U2F packet {:x?}", u2f_ipc.data);
|
log::debug!("enqueued U2F packet {:x?}", u2f_ipc.data);
|
||||||
u2f_ipc.code = U2fCode::TxAck;
|
u2f_ipc.code = U2fCode::TxAck;
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue