From 575efa6129794150ef9b2ee02adf2da1e60e3b15 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sat, 14 Feb 2026 19:35:45 +0800 Subject: [PATCH] refactor function call to use macro the macro will give relevant positional information when the assert fails, whereas the function always fails in the same place and we lose that valuable debugging info --- .../boot1/src/platform/bao1x/usb/driver.rs | 16 +++++---- .../boot1/src/platform/bao1x/usb/handlers.rs | 34 ++++++------------- .../boot1/src/platform/bao1x/usb/mod.rs | 24 +++++++++++-- 3 files changed, 41 insertions(+), 33 deletions(-) diff --git a/bao1x-boot/boot1/src/platform/bao1x/usb/driver.rs b/bao1x-boot/boot1/src/platform/bao1x/usb/driver.rs index ac11a0969..46d9a5658 100644 --- a/bao1x-boot/boot1/src/platform/bao1x/usb/driver.rs +++ b/bao1x-boot/boot1/src/platform/bao1x/usb/driver.rs @@ -7,6 +7,7 @@ use bao1x_hal::usb::driver::*; use bao1x_hal::usb::utra::*; use super::*; +use crate::udc_pointer_check; pub(crate) static mut USB: Option = None; @@ -61,7 +62,7 @@ fn get_status_request(this: &mut CorigineUsb, request_type: u8, index: u16) { CRG_UDC_EP0_REQBUFSIZE, ) }; - udc_pointer_check(ep0_buf.as_ptr() as usize, ep0_buf.len()); + udc_pointer_check!(ep0_buf.as_ptr() as usize, ep0_buf.len()); let mut status_val: u32 = 0; let recipient = request_type & 0x1f; @@ -135,7 +136,7 @@ fn handle_event(this: &mut CorigineUsb, event_trb: &mut EventTrbS) -> CrgEvent { let residual_length = event_trb.dw2.trb_tran_len() as u16; // update the dequeue pointer // crate::println_d!("event_transfer {:x?}", event_trb); - udc_pointer_check(event_trb.dw0 as usize, size_of::() * 2); + udc_pointer_check!(event_trb.dw0 as usize, size_of::() * 2); // safety: checked that there's at least space for the increment of the pointer let deq_pt = unsafe { (event_trb.dw0 as *mut TransferTrbS).add(1).as_mut().expect("Couldn't deref ptr") }; @@ -144,6 +145,7 @@ fn handle_event(this: &mut CorigineUsb, event_trb: &mut EventTrbS) -> CrgEvent { } else { udc_ep.deq_pt = AtomicPtr::new(deq_pt as *mut TransferTrbS); } + udc_pointer_check!(udc_ep.deq_pt.load(Ordering::SeqCst) as usize, size_of::()); // crate::println_d!("EventTransfer: comp_code {:?}, PEI {}", comp_code, pei); let dir = (pei & 1) != 0; @@ -164,7 +166,7 @@ fn handle_event(this: &mut CorigineUsb, event_trb: &mut EventTrbS) -> CrgEvent { // crate::println_d!("EP{} xfer event, dir {}", ep_num, if dir { "OUT" } else { "IN" }); // xfer_complete if let Some(f) = this.udc_ep[pei as usize].completion_handler { - udc_pointer_check(event_trb.dw0 as usize, size_of::()); + udc_pointer_check!(event_trb.dw0 as usize, size_of::()); // so unsafe. so unsafe. We're counting on the hardware to hand us a raw pointer // that isn't corrupted. The check above assures that the pointer is at least within // the designated range of memory for the UDC hardware controller @@ -285,7 +287,7 @@ fn handle_event(this: &mut CorigineUsb, event_trb: &mut EventTrbS) -> CrgEvent { CRG_UDC_EP0_REQBUFSIZE, ) }; - udc_pointer_check(ep0_buf.as_ptr() as usize, ep0_buf.len()); + udc_pointer_check!(ep0_buf.as_ptr() as usize, ep0_buf.len()); if this.get_device_state() != UsbDeviceState::Configured { ep0_buf[0] = 0; } else { @@ -307,7 +309,7 @@ fn handle_event(this: &mut CorigineUsb, event_trb: &mut EventTrbS) -> CrgEvent { CRG_UDC_EP0_REQBUFSIZE, ) }; - udc_pointer_check(ep0_buf.as_ptr() as usize, ep0_buf.len()); + udc_pointer_check!(ep0_buf.as_ptr() as usize, ep0_buf.len()); ep0_buf[0] = this.cur_interface_num; this.ep0_send(ep0_buf.as_ptr() as usize, 1, 0); } @@ -351,7 +353,7 @@ fn handle_event(this: &mut CorigineUsb, event_trb: &mut EventTrbS) -> CrgEvent { CRG_UDC_EP0_REQBUFSIZE, ) }; - udc_pointer_check(ep0_buf.as_ptr() as usize, ep0_buf.len()); + udc_pointer_check!(ep0_buf.as_ptr() as usize, ep0_buf.len()); ep0_buf[0] = 0; this.ep0_send(ep0_buf.as_ptr() as usize, 1, 0); } @@ -377,7 +379,7 @@ fn handle_event(this: &mut CorigineUsb, event_trb: &mut EventTrbS) -> CrgEvent { CRG_UDC_EP0_REQBUFSIZE, ) }; - udc_pointer_check(ep0_buf.as_ptr() as usize, ep0_buf.len()); + udc_pointer_check!(ep0_buf.as_ptr() as usize, ep0_buf.len()); // Fill with a dummy 115200 8N1 config ep0_buf[0..4].copy_from_slice(&115200u32.to_le_bytes()); // dwDTERate ep0_buf[4] = 0; // 1 stop bit diff --git a/bao1x-boot/boot1/src/platform/bao1x/usb/handlers.rs b/bao1x-boot/boot1/src/platform/bao1x/usb/handlers.rs index 759c5ca17..436a2e44b 100644 --- a/bao1x-boot/boot1/src/platform/bao1x/usb/handlers.rs +++ b/bao1x-boot/boot1/src/platform/bao1x/usb/handlers.rs @@ -12,7 +12,7 @@ use bao1x_hal::usb::driver::*; use utralib::*; use super::*; -use crate::{APP_BYTES, BAREMETAL_BYTES, IS_BAOSEC, KERNEL_BYTES, SWAP_BYTES}; +use crate::{APP_BYTES, BAREMETAL_BYTES, IS_BAOSEC, KERNEL_BYTES, SWAP_BYTES, udc_pointer_check}; pub static TX_IDLE: core::sync::atomic::AtomicBool = core::sync::atomic::AtomicBool::new(true); @@ -23,18 +23,6 @@ pub(crate) const RAMDISK_ADDRESS: usize = crate::FREE_MEM_START; pub(crate) const RAMDISK_ACTUAL_LEN: usize = crate::FREE_MEM_LEN; pub(crate) const RAMDISK_LEN: usize = 128 * 1024 * 1024; // present as a 128MiB disk. Big enough for "any" update? pub(crate) const SECTOR_SIZE: u16 = 512; -pub(crate) const APP_BUF_LEN: usize = 4096 * 2; - -#[inline(always)] -/// This function checks that any raw pointers passed back from the hardware -/// are within the allocated ranges for the UDC block to use. -pub(crate) fn udc_pointer_check(base: usize, len: usize) { - assert!( - (base >= HW_IFRAM1_MEM && (base + len) <= HW_IFRAM1_MEM + APP_BUF_LEN) - || (base >= bao1x_hal::board::CRG_UDC_MEMBASE - && (base + len) <= (bao1x_hal::board::CRG_UDC_MEMBASE + CRG_IFRAM_PAGES * 4096)), - ); -} fn fill_sparse_data(dest: &mut [u8], offset: usize) { let dest_start = offset; @@ -118,7 +106,7 @@ pub fn get_descriptor_request(this: &mut CorigineUsb, value: u16, _index: usize, CRG_UDC_EP0_REQBUFSIZE, ) }; - udc_pointer_check(ep0_buf.as_ptr() as usize, ep0_buf.len()); + udc_pointer_check!(ep0_buf.as_ptr() as usize, ep0_buf.len()); match (value >> 8) as u8 { USB_DT_DEVICE => { @@ -199,7 +187,7 @@ pub fn usb_ep1_bulk_out_complete( _residual: u16, ) { let length = info & 0xFFFF; - udc_pointer_check(buf_addr, info as usize & 0xFFFF); + udc_pointer_check!(buf_addr, info as usize & 0xFFFF); // safety: the buffer is checked to be in-range for the UDC handler let buf = unsafe { core::slice::from_raw_parts(buf_addr as *const u8, info as usize & 0xFFFF) }; let mut cbw = Cbw::default(); @@ -464,7 +452,7 @@ pub fn usb_ep3_bulk_out_complete( } // Slice of received data - udc_pointer_check(buf_addr, actual); + udc_pointer_check!(buf_addr, actual); // safety: buffer is checked to be within the UDC memory range let buf = unsafe { core::slice::from_raw_parts(buf_addr as *const u8, actual as usize) }; @@ -650,7 +638,7 @@ fn process_request_sense(this: &mut CorigineUsb, cbw: Cbw) { // crate::println_d!("UMS_STATE_STATUS_PHASE\r\n"); } else if cbw.flags & 0x80 != 0 { if cbw.data_transfer_length < 18 { - udc_pointer_check(EP1_IN_BUF, EP1_IN_BUF_LEN); + udc_pointer_check!(EP1_IN_BUF, EP1_IN_BUF_LEN); let ep1_in = unsafe { core::slice::from_raw_parts_mut(EP1_IN_BUF as *mut u8, EP1_IN_BUF_LEN) }; ep1_in[..18].fill(0); this.bulk_xfer(1, USB_SEND, EP1_IN_BUF, cbw.data_transfer_length as usize, 0, 0); @@ -658,7 +646,7 @@ fn process_request_sense(this: &mut CorigineUsb, cbw: Cbw) { csw.residue = 0; csw.status = 0; } else if cbw.data_transfer_length >= 18 { - udc_pointer_check(EP1_IN_BUF, EP1_IN_BUF_LEN); + udc_pointer_check!(EP1_IN_BUF, EP1_IN_BUF_LEN); let ep1_in = unsafe { core::slice::from_raw_parts_mut(EP1_IN_BUF as *mut u8, EP1_IN_BUF_LEN) }; ep1_in[..18].fill(0); this.bulk_xfer(1, USB_SEND, EP1_IN_BUF, 18, 0, 0); @@ -692,7 +680,7 @@ fn process_inquiry_command(this: &mut CorigineUsb, cbw: Cbw) { csw.send(this); } else if cbw.flags & 0x80 != 0 { if cbw.data_transfer_length < 36 { - udc_pointer_check(EP1_IN_BUF, EP1_IN_BUF_LEN); + udc_pointer_check!(EP1_IN_BUF, EP1_IN_BUF_LEN); let ep1_in = unsafe { core::slice::from_raw_parts_mut(EP1_IN_BUF as *mut u8, EP1_IN_BUF_LEN) }; ep1_in[..36].fill(0); this.bulk_xfer(1, USB_SEND, EP1_IN_BUF, cbw.data_transfer_length as usize, 0, 0); @@ -700,7 +688,7 @@ fn process_inquiry_command(this: &mut CorigineUsb, cbw: Cbw) { csw.residue = 0; csw.status = 0; } else if cbw.data_transfer_length as usize >= size_of::() { - udc_pointer_check(EP1_IN_BUF, EP1_IN_BUF_LEN); + udc_pointer_check!(EP1_IN_BUF, EP1_IN_BUF_LEN); let ep1_in = unsafe { core::slice::from_raw_parts_mut(EP1_IN_BUF as *mut u8, EP1_IN_BUF_LEN) }; ep1_in[..size_of::()].copy_from_slice(inquiry_data.as_ref()); @@ -731,7 +719,7 @@ fn process_report_capacity(this: &mut CorigineUsb, cbw: Cbw) { capacity[4..].copy_from_slice(&rc_bl.to_be_bytes()); if cbw.flags & 0x80 != 0 { - udc_pointer_check(EP1_IN_BUF, EP1_IN_BUF_LEN); + udc_pointer_check!(EP1_IN_BUF, EP1_IN_BUF_LEN); let ep1_in = unsafe { core::slice::from_raw_parts_mut(EP1_IN_BUF as *mut u8, EP1_IN_BUF_LEN) }; ep1_in[..8].fill(0); ep1_in[..8].copy_from_slice(&capacity); @@ -752,7 +740,7 @@ fn process_read_capacity_16(this: &mut CorigineUsb, _cbw: Cbw) { let mut response = [0u8; 32]; response[..8].copy_from_slice(&rc_lba.to_be_bytes()); response[8..12].copy_from_slice(&rc_bl.to_be_bytes()); - udc_pointer_check(EP1_IN_BUF, EP1_IN_BUF_LEN); + udc_pointer_check!(EP1_IN_BUF, EP1_IN_BUF_LEN); let ep1_in = unsafe { core::slice::from_raw_parts_mut(EP1_IN_BUF as *mut u8, EP1_IN_BUF_LEN) }; ep1_in[..32].copy_from_slice(&response); this.bulk_xfer(1, USB_SEND, EP1_IN_BUF, 32, 0, 0); @@ -968,7 +956,7 @@ fn process_write12_command(this: &mut CorigineUsb, cbw: Cbw) { pub(crate) fn app_buf_addr() -> usize { HW_IFRAM1_MEM } pub(crate) fn app_buf_len() -> usize { APP_BUF_LEN } pub(crate) fn conjure_app_buf() -> &'static mut [u8] { - udc_pointer_check(app_buf_addr(), app_buf_len()); + udc_pointer_check!(app_buf_addr(), app_buf_len()); unsafe { core::slice::from_raw_parts_mut(app_buf_addr() as *mut u8, app_buf_len()) } } diff --git a/bao1x-boot/boot1/src/platform/bao1x/usb/mod.rs b/bao1x-boot/boot1/src/platform/bao1x/usb/mod.rs index 6ef966571..247f06dfe 100644 --- a/bao1x-boot/boot1/src/platform/bao1x/usb/mod.rs +++ b/bao1x-boot/boot1/src/platform/bao1x/usb/mod.rs @@ -8,6 +8,24 @@ mod page_defrag; use crate::irq::*; +pub(crate) const APP_BUF_LEN: usize = 4096 * 2; + +/// This macro checks that any raw pointers passed back from the hardware +/// are within the allocated ranges for the UDC block to use. A macro is used +/// because we want the line number information on failure. A function would always +/// fail at the same spot, stripping out context. +#[macro_export] +macro_rules! udc_pointer_check { + ($base:expr, $len:expr) => { + assert!( + ($base >= utralib::HW_IFRAM1_MEM && ($base + $len) <= utralib::HW_IFRAM1_MEM + APP_BUF_LEN) + || ($base >= bao1x_hal::board::CRG_UDC_MEMBASE + && ($base + $len) + <= (bao1x_hal::board::CRG_UDC_MEMBASE + bao1x_hal::board::CRG_IFRAM_PAGES * 4096)), + ); + }; +} + const USB_TYPE_MASK: u8 = 0x03 << 5; const USB_TYPE_STANDARD: u8 = 0x00 << 5; const USB_TYPE_CLASS: u8 = 0x01 << 5; @@ -600,7 +618,7 @@ struct Csw { impl Csw { fn derive() -> Csw { let mut csw = Csw::default(); - udc_pointer_check(handlers::CSW_ADDR, size_of::()); + udc_pointer_check!(handlers::CSW_ADDR, size_of::()); csw.as_mut().copy_from_slice(unsafe { core::slice::from_raw_parts(handlers::CSW_ADDR as *mut u8, size_of::()) }); @@ -608,13 +626,13 @@ impl Csw { } fn update_hw(&self) { - udc_pointer_check(handlers::CSW_ADDR, size_of::()); + udc_pointer_check!(handlers::CSW_ADDR, size_of::()); let csw_buf = unsafe { core::slice::from_raw_parts_mut(CSW_ADDR as *mut u8, size_of::()) }; csw_buf.copy_from_slice(self.as_ref()); } fn send(&self, usb: &mut bao1x_hal::usb::driver::CorigineUsb) { - udc_pointer_check(handlers::CSW_ADDR, size_of::()); + udc_pointer_check!(handlers::CSW_ADDR, size_of::()); let csw_buf = unsafe { core::slice::from_raw_parts_mut(CSW_ADDR as *mut u8, size_of::()) }; csw_buf.copy_from_slice(self.as_ref()); usb.bulk_xfer(1, bao1x_hal::usb::driver::USB_SEND, CSW_ADDR, size_of::(), 0, 0);