diff --git a/ledger_device_sdk/src/io.rs b/ledger_device_sdk/src/io.rs index a187bc7e..01dcd3c8 100644 --- a/ledger_device_sdk/src/io.rs +++ b/ledger_device_sdk/src/io.rs @@ -177,7 +177,7 @@ impl Comm { let mut spi_buffer = [0u8; 256]; while sys_seph::is_status_sent() { sys_seph::seph_recv(&mut spi_buffer, 0); - seph::handle_event(&mut self.apdu_buffer, &spi_buffer); + seph::handle_event(&mut self.apdu_buffer, &mut spi_buffer); } match unsafe { G_io_app.apdu_state } { @@ -208,7 +208,7 @@ impl Comm { sys_seph::send_general_status() } sys_seph::seph_recv(&mut spi_buffer, 0); - seph::handle_event(&mut self.apdu_buffer, &spi_buffer); + seph::handle_event(&mut self.apdu_buffer, &mut spi_buffer); } self.tx = 0; self.rx = 0; diff --git a/ledger_device_sdk/src/seph.rs b/ledger_device_sdk/src/seph.rs index b429a0ac..73acab8e 100644 --- a/ledger_device_sdk/src/seph.rs +++ b/ledger_device_sdk/src/seph.rs @@ -64,65 +64,21 @@ impl From for UsbEp { } } -/// FFI bindings to USBD functions inlined here for clarity -/// and also because some of the generated ones are incorrectly -/// assuming mutable pointers when they are not -#[repr(C)] -#[derive(Copy, Clone)] -pub struct apdu_buffer_s { - pub buf: *mut u8, - pub len: u16, -} -impl Default for apdu_buffer_s { - fn default() -> Self { - unsafe { ::core::mem::zeroed() } - } -} -pub type ApduBufferT = apdu_buffer_s; -extern "C" { - pub static mut USBD_Device: USBD_HandleTypeDef; - pub fn USBD_LL_SetupStage( - pdev: *mut USBD_HandleTypeDef, - psetup: *const u8, - ) -> USBD_StatusTypeDef; - pub fn USBD_LL_DataOutStage( - pdev: *mut USBD_HandleTypeDef, - epnum: u8, - pdata: *const u8, - arg1: *mut ApduBufferT, - ) -> USBD_StatusTypeDef; - pub fn USBD_LL_DataInStage( - pdev: *mut USBD_HandleTypeDef, - epnum: u8, - pdata: *const u8, - ) -> USBD_StatusTypeDef; - pub fn USBD_LL_Reset(pdev: *mut USBD_HandleTypeDef) -> USBD_StatusTypeDef; - pub fn USBD_LL_SetSpeed( - pdev: *mut USBD_HandleTypeDef, - speed: USBD_SpeedTypeDef, - ) -> USBD_StatusTypeDef; - pub fn USBD_LL_Suspend(pdev: *mut USBD_HandleTypeDef) -> USBD_StatusTypeDef; - pub fn USBD_LL_Resume(pdev: *mut USBD_HandleTypeDef) -> USBD_StatusTypeDef; - pub fn USBD_LL_SOF(pdev: *mut USBD_HandleTypeDef) -> USBD_StatusTypeDef; -} - /// Below is a straightforward translation of the corresponding functions /// in the C SDK, they could be improved pub fn handle_usb_event(event: u8) { match Events::from(event) { - Events::USBEventReset => { - unsafe { - USBD_LL_SetSpeed(&raw mut USBD_Device, 1 /*USBD_SPEED_FULL*/); - USBD_LL_Reset(&raw mut USBD_Device); + Events::USBEventReset => unsafe { + USBD_LL_SetSpeed(&raw mut USBD_Device, USBD_SPEED_FULL); + USBD_LL_Reset(&raw mut USBD_Device); - if G_io_app.apdu_media != IO_APDU_MEDIA_NONE { - return; - } - - G_io_app.usb_ep_xfer_len = core::mem::zeroed(); - G_io_app.usb_ep_timeouts = core::mem::zeroed(); + if G_io_app.apdu_media != IO_APDU_MEDIA_NONE { + return; } - } + + G_io_app.usb_ep_xfer_len = core::mem::zeroed(); + G_io_app.usb_ep_timeouts = core::mem::zeroed(); + }, Events::USBEventSOF => unsafe { USBD_LL_SOF(&raw mut USBD_Device); }, @@ -136,17 +92,17 @@ pub fn handle_usb_event(event: u8) { } } -pub fn handle_usb_ep_xfer_event(apdu_buffer: &mut [u8], buffer: &[u8]) { +pub fn handle_usb_ep_xfer_event(apdu_buffer: &mut [u8], buffer: &mut [u8]) { let endpoint = buffer[3] & 0x7f; match UsbEp::from(buffer[4]) { UsbEp::USBEpXFERSetup => unsafe { - USBD_LL_SetupStage(&raw mut USBD_Device, &buffer[6]); + USBD_LL_SetupStage(&raw mut USBD_Device, &mut buffer[6]); }, UsbEp::USBEpXFERIn => { if (endpoint as u32) < IO_USB_MAX_ENDPOINTS { unsafe { G_io_app.usb_ep_timeouts[endpoint as usize].timeout = 0; - USBD_LL_DataInStage(&raw mut USBD_Device, endpoint, &buffer[6]); + USBD_LL_DataInStage(&raw mut USBD_Device, endpoint, &mut buffer[6]); } } } @@ -154,11 +110,16 @@ pub fn handle_usb_ep_xfer_event(apdu_buffer: &mut [u8], buffer: &[u8]) { if (endpoint as u32) < IO_USB_MAX_ENDPOINTS { unsafe { G_io_app.usb_ep_xfer_len[endpoint as usize] = buffer[5]; - let mut apdu_buf = ApduBufferT { + let mut apdu_buf = apdu_buffer_s { buf: apdu_buffer.as_mut_ptr(), len: 260, }; - USBD_LL_DataOutStage(&raw mut USBD_Device, endpoint, &buffer[6], &mut apdu_buf); + USBD_LL_DataOutStage( + &raw mut USBD_Device, + endpoint, + &mut buffer[6], + &mut apdu_buf, + ); } } } @@ -185,7 +146,7 @@ pub fn handle_capdu_event(apdu_buffer: &mut [u8], buffer: &[u8]) { } } -pub fn handle_event(apdu_buffer: &mut [u8], spi_buffer: &[u8]) { +pub fn handle_event(apdu_buffer: &mut [u8], spi_buffer: &mut [u8]) { let len = u16::from_be_bytes([spi_buffer[1], spi_buffer[2]]); match Events::from(spi_buffer[0]) { Events::USBEvent => { diff --git a/ledger_secure_sdk_sys/build.rs b/ledger_secure_sdk_sys/build.rs index b2f2eb6d..15a63f35 100644 --- a/ledger_secure_sdk_sys/build.rs +++ b/ledger_secure_sdk_sys/build.rs @@ -493,6 +493,7 @@ impl SDKBuilder { "include/os_ux.h", "include/ox.h", /* crypto-related syscalls */ "lib_stusb/STM32_USB_Device_Library/Core/Inc/usbd_def.h", + "lib_stusb/STM32_USB_Device_Library/Core/Inc/usbd_core.h", "include/os_io_usb.h", "lib_standard_app/swap_lib_calls.h", ],