From d9ff07a32f0e215ef9b6c9b5f2a2c92e5c767aee Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 30 Sep 2025 17:29:06 +0800 Subject: [PATCH] redo the fat emulation layer present a 128MiB FAT32 disk based on a dumped image that was clean-initialized on a windows machine (windows is the most picky so it is used as the template maker). add hooks for emulating sparse reads of file system structures that are well outside our available memory range. add hooks for writing uf2 files when valid sectors are encountered. this basically works, you can whack a uf2 file into the disk and the update happens. the problem now is that something breaks in the repl loop when USB disk traffic happens, this commit gives us a checkpoint to work against. --- bao1x-boot/boot1/Cargo.toml | 2 +- bao1x-boot/boot1/src/main.rs | 5 +- bao1x-boot/boot1/src/platform/bao1x/bao1x.rs | 4 + .../boot1/src/platform/bao1x/usb/driver.rs | 23 +- .../src/platform/bao1x/usb/fat32_base.rs | 565 ++++++++++++++++++ .../boot1/src/platform/bao1x/usb/glue.rs | 67 ++- .../boot1/src/platform/bao1x/usb/handlers.rs | 201 +++---- .../boot1/src/platform/bao1x/usb/mod.rs | 2 +- bao1x-boot/boot1/src/repl.rs | 34 +- 9 files changed, 763 insertions(+), 140 deletions(-) create mode 100644 bao1x-boot/boot1/src/platform/bao1x/usb/fat32_base.rs diff --git a/bao1x-boot/boot1/Cargo.toml b/bao1x-boot/boot1/Cargo.toml index 1099ccc44..dcdb6337b 100644 --- a/bao1x-boot/boot1/Cargo.toml +++ b/bao1x-boot/boot1/Cargo.toml @@ -31,7 +31,7 @@ bao1x = [ "utralib/bao1x", "linked_list_allocator", "bao1x-hal/loader-baosec", - "bao1x-hal/debug-print-usb", + # "bao1x-hal/debug-print-usb", "bao1x-api", ] unsafe-debug = [] diff --git a/bao1x-boot/boot1/src/main.rs b/bao1x-boot/boot1/src/main.rs index 8b8000803..21c2398a7 100644 --- a/bao1x-boot/boot1/src/main.rs +++ b/bao1x-boot/boot1/src/main.rs @@ -84,7 +84,7 @@ pub unsafe extern "C" fn rust_entry() -> ! { } if boot_wait == BootWaitCoding::Enable { - crate::println!("Boot bypassed because autoboot was disabled"); + crate::println!("Boot bypassed because bootwait was enabled"); } else if current_key.is_some() { crate::println!("Boot bypassed with keypress: {:?}", current_key); } @@ -134,6 +134,7 @@ pub unsafe extern "C" fn rust_entry() -> ! { // provide feedback when connection is established if new_usb_state != last_usb_state { + crate::println_d!("new state {:?}", new_usb_state); if new_usb_state == UsbDeviceState::Configured { crate::println!("USB is connected!"); last_usb_state = new_usb_state; @@ -186,7 +187,9 @@ pub unsafe extern "C" fn rust_entry() -> ! { // break out of the loop when USB is disconnected if new_portsc != portsc { portsc = new_portsc; + crate::println_d!("new portsc {:x}", portsc); if glue::is_disconnected(portsc) && new_usb_state == UsbDeviceState::Configured { + crate::println_d!("USB disconnected!"); USB_CONNECTED.store(false, core::sync::atomic::Ordering::SeqCst); // last_usb_state = UsbDeviceState::NotAttached; break; diff --git a/bao1x-boot/boot1/src/platform/bao1x/bao1x.rs b/bao1x-boot/boot1/src/platform/bao1x/bao1x.rs index 75d4f4681..fede2a521 100644 --- a/bao1x-boot/boot1/src/platform/bao1x/bao1x.rs +++ b/bao1x-boot/boot1/src/platform/bao1x/bao1x.rs @@ -25,6 +25,10 @@ pub const HEAP_LEN: usize = 1024 * 256; // total occupied area is [HEAP_START + HEAP_LEN..HEAP_START + HEAP_LEN + 8192] pub const SCRATCH_PAGE: usize = HEAP_START + HEAP_LEN + 4096; +pub const FREE_MEM_START: usize = SCRATCH_PAGE + 16384; +pub const STACK_LEN: usize = 128 * 1024; // 128k for stack is more than enough (usually <16k) +pub const FREE_MEM_LEN: usize = (RAM_BASE + RAM_SIZE) - FREE_MEM_START - STACK_LEN; + // NOTE: this forces the mapping to be the same on both baosec and dabao pub const UART_IFRAM_ADDR: usize = bao1x_hal::board::UART_DMA_TX_BUF_PHYS; diff --git a/bao1x-boot/boot1/src/platform/bao1x/usb/driver.rs b/bao1x-boot/boot1/src/platform/bao1x/usb/driver.rs index 06ecba28f..f9549e96c 100644 --- a/bao1x-boot/boot1/src/platform/bao1x/usb/driver.rs +++ b/bao1x-boot/boot1/src/platform/bao1x/usb/driver.rs @@ -53,21 +53,10 @@ pub unsafe fn init_usb() { // initialize the "disk" area let disk = handlers::conjure_disk(); disk.fill(0); - // setup MBR - disk[..MBR_TEMPLATE.len()].copy_from_slice(&MBR_TEMPLATE); - //set storage size - disk[0x20..0x24].copy_from_slice(&(RAMDISK_LEN as u32 / SECTOR_SIZE as u32).to_le_bytes()); - - // populate an initial "windows-friendly" FAT layout, generated by plugging the empty disk - // into windows and letting it...do its thing, then snapshotting the data. - disk[TABLE_OFFSET_FAT1..TABLE_OFFSET_FAT1 + FAT_TABLE.len()].copy_from_slice(&FAT_TABLE); - disk[TABLE_OFFSET_FAT2..TABLE_OFFSET_FAT2 + FAT_TABLE.len()].copy_from_slice(&FAT_TABLE); - - disk[ROOT_DIR_OFFSET..ROOT_DIR_OFFSET + ROOT_DIR.len()].copy_from_slice(&ROOT_DIR); - disk[FAT_DATA1_OFFSET..FAT_DATA1_OFFSET + FAT_DATA1.len()].copy_from_slice(&FAT_DATA1); - disk[FAT_DATA2_OFFSET..FAT_DATA2_OFFSET + FAT_DATA2.len()].copy_from_slice(&FAT_DATA2); - disk[FAT_DATA3_OFFSET..FAT_DATA3_OFFSET + FAT_DATA3.len()].copy_from_slice(&FAT_DATA3); + for (offset, block) in super::fat32_base::FAT_DATA { + disk[offset..offset + 32].copy_from_slice(&block); + } // install the interrupt handler // setup the stack & controller @@ -281,13 +270,15 @@ fn handle_event(this: &mut CorigineUsb, event_trb: &mut EventTrbS) -> CrgEvent { } USB_REQ_CLEAR_FEATURE => { crate::println_d!("USB_REQ_CLEAR_FEATURE"); - crate::println_d!("*** UNSUPPORTED ***"); + crate::println_d!("*** UNSUPPORTED (stall) ***"); + this.ep_halt(0, USB_RECV); /* do clear feature */ // clear_feature_request(setup_pkt.b_request_type, w_index, w_value); } USB_REQ_SET_FEATURE => { crate::println_d!("USB_REQ_SET_FEATURE\r\n"); - crate::println_d!("*** UNSUPPORTED ***"); + crate::println_d!("*** UNSUPPORTED (stall) ***"); + this.ep_halt(0, USB_RECV); /* do set feature */ /* if crg_udc_get_device_state() == USB_STATE_CONFIGURED { diff --git a/bao1x-boot/boot1/src/platform/bao1x/usb/fat32_base.rs b/bao1x-boot/boot1/src/platform/bao1x/usb/fat32_base.rs new file mode 100644 index 000000000..7feaf200c --- /dev/null +++ b/bao1x-boot/boot1/src/platform/bao1x/usb/fat32_base.rs @@ -0,0 +1,565 @@ +pub const FAT_DATA: [(usize, [u8; 32]); 62] = [ + ( + 0x0, + [ + 0xfa, 0x33, 0xc0, 0x8e, 0xd0, 0xbc, 0x00, 0x7c, 0x8b, 0xf4, 0x50, 0x07, 0x50, 0x1f, 0xfb, 0xfc, + 0xbf, 0x00, 0x06, 0xb9, 0x00, 0x01, 0xf2, 0xa5, 0xea, 0x1d, 0x06, 0x00, 0x00, 0xbe, 0xbe, 0x07, + ], + ), + ( + 0x20, + [ + 0xb3, 0x04, 0x80, 0x3c, 0x80, 0x74, 0x0e, 0x80, 0x3c, 0x00, 0x75, 0x1c, 0x83, 0xc6, 0x10, 0xfe, + 0xcb, 0x75, 0xef, 0xcd, 0x18, 0x8b, 0x14, 0x8b, 0x4c, 0x02, 0x8b, 0xee, 0x83, 0xc6, 0x10, 0xfe, + ], + ), + ( + 0x40, + [ + 0xcb, 0x74, 0x1a, 0x80, 0x3c, 0x00, 0x74, 0xf4, 0xbe, 0x8b, 0x06, 0xac, 0x3c, 0x00, 0x74, 0x0b, + 0x56, 0xbb, 0x07, 0x00, 0xb4, 0x0e, 0xcd, 0x10, 0x5e, 0xeb, 0xf0, 0xeb, 0xfe, 0xbf, 0x05, 0x00, + ], + ), + ( + 0x60, + [ + 0xbb, 0x00, 0x7c, 0xb8, 0x01, 0x02, 0x57, 0xcd, 0x13, 0x5f, 0x73, 0x0c, 0x33, 0xc0, 0xcd, 0x13, + 0x4f, 0x75, 0xed, 0xbe, 0xa3, 0x06, 0xeb, 0xd3, 0xbe, 0xc2, 0x06, 0xbf, 0xfe, 0x7d, 0x81, 0x3d, + ], + ), + ( + 0x80, + [ + 0x55, 0xaa, 0x75, 0xc7, 0x8b, 0xf5, 0xea, 0x00, 0x7c, 0x00, 0x00, 0x49, 0x6e, 0x76, 0x61, 0x6c, + 0x69, 0x64, 0x20, 0x70, 0x61, 0x72, 0x74, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x74, 0x61, 0x62, + ], + ), + ( + 0xa0, + [ + 0x6c, 0x65, 0x00, 0x45, 0x72, 0x72, 0x6f, 0x72, 0x20, 0x6c, 0x6f, 0x61, 0x64, 0x69, 0x6e, 0x67, + 0x20, 0x6f, 0x70, 0x65, 0x72, 0x61, 0x74, 0x69, 0x6e, 0x67, 0x20, 0x73, 0x79, 0x73, 0x74, 0x65, + ], + ), + ( + 0xc0, + [ + 0x6d, 0x00, 0x4d, 0x69, 0x73, 0x73, 0x69, 0x6e, 0x67, 0x20, 0x6f, 0x70, 0x65, 0x72, 0x61, 0x74, + 0x69, 0x6e, 0x67, 0x20, 0x73, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + ], + ), + ( + 0x1a0, + [ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf6, 0x7a, 0xd9, 0xe6, 0x00, 0x00, 0x00, 0x02, + ], + ), + ( + 0x1c0, + [ + 0x03, 0x00, 0x0c, 0xfe, 0x3f, 0x0e, 0x80, 0x00, 0x00, 0x00, 0x00, 0xb8, 0x03, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + ], + ), + ( + 0x1e0, + [ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0xaa, + ], + ), + ( + 0x10000, + [ + 0xeb, 0x58, 0x90, 0x4d, 0x53, 0x44, 0x4f, 0x53, 0x35, 0x2e, 0x30, 0x00, 0x02, 0x01, 0x9e, 0x11, + 0x02, 0x00, 0x00, 0x00, 0x00, 0xf8, 0x00, 0x00, 0x3f, 0x00, 0xff, 0x00, 0x80, 0x00, 0x00, 0x00, + ], + ), + ( + 0x10020, + [ + 0x00, 0xb8, 0x03, 0x00, 0x31, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, + 0x01, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + ], + ), + ( + 0x10040, + [ + 0x80, 0x00, 0x29, 0xcd, 0xcf, 0xdc, 0x46, 0x4e, 0x4f, 0x20, 0x4e, 0x41, 0x4d, 0x45, 0x20, 0x20, + 0x20, 0x20, 0x46, 0x41, 0x54, 0x33, 0x32, 0x20, 0x20, 0x20, 0x33, 0xc9, 0x8e, 0xd1, 0xbc, 0xf4, + ], + ), + ( + 0x10060, + [ + 0x7b, 0x8e, 0xc1, 0x8e, 0xd9, 0xbd, 0x00, 0x7c, 0x88, 0x56, 0x40, 0x88, 0x4e, 0x02, 0x8a, 0x56, + 0x40, 0xb4, 0x41, 0xbb, 0xaa, 0x55, 0xcd, 0x13, 0x72, 0x10, 0x81, 0xfb, 0x55, 0xaa, 0x75, 0x0a, + ], + ), + ( + 0x10080, + [ + 0xf6, 0xc1, 0x01, 0x74, 0x05, 0xfe, 0x46, 0x02, 0xeb, 0x2d, 0x8a, 0x56, 0x40, 0xb4, 0x08, 0xcd, + 0x13, 0x73, 0x05, 0xb9, 0xff, 0xff, 0x8a, 0xf1, 0x66, 0x0f, 0xb6, 0xc6, 0x40, 0x66, 0x0f, 0xb6, + ], + ), + ( + 0x100a0, + [ + 0xd1, 0x80, 0xe2, 0x3f, 0xf7, 0xe2, 0x86, 0xcd, 0xc0, 0xed, 0x06, 0x41, 0x66, 0x0f, 0xb7, 0xc9, + 0x66, 0xf7, 0xe1, 0x66, 0x89, 0x46, 0xf8, 0x83, 0x7e, 0x16, 0x00, 0x75, 0x39, 0x83, 0x7e, 0x2a, + ], + ), + ( + 0x100c0, + [ + 0x00, 0x77, 0x33, 0x66, 0x8b, 0x46, 0x1c, 0x66, 0x83, 0xc0, 0x0c, 0xbb, 0x00, 0x80, 0xb9, 0x01, + 0x00, 0xe8, 0x2c, 0x00, 0xe9, 0xa8, 0x03, 0xa1, 0xf8, 0x7d, 0x80, 0xc4, 0x7c, 0x8b, 0xf0, 0xac, + ], + ), + ( + 0x100e0, + [ + 0x84, 0xc0, 0x74, 0x17, 0x3c, 0xff, 0x74, 0x09, 0xb4, 0x0e, 0xbb, 0x07, 0x00, 0xcd, 0x10, 0xeb, + 0xee, 0xa1, 0xfa, 0x7d, 0xeb, 0xe4, 0xa1, 0x7d, 0x80, 0xeb, 0xdf, 0x98, 0xcd, 0x16, 0xcd, 0x19, + ], + ), + ( + 0x10100, + [ + 0x66, 0x60, 0x80, 0x7e, 0x02, 0x00, 0x0f, 0x84, 0x20, 0x00, 0x66, 0x6a, 0x00, 0x66, 0x50, 0x06, + 0x53, 0x66, 0x68, 0x10, 0x00, 0x01, 0x00, 0xb4, 0x42, 0x8a, 0x56, 0x40, 0x8b, 0xf4, 0xcd, 0x13, + ], + ), + ( + 0x10120, + [ + 0x66, 0x58, 0x66, 0x58, 0x66, 0x58, 0x66, 0x58, 0xeb, 0x33, 0x66, 0x3b, 0x46, 0xf8, 0x72, 0x03, + 0xf9, 0xeb, 0x2a, 0x66, 0x33, 0xd2, 0x66, 0x0f, 0xb7, 0x4e, 0x18, 0x66, 0xf7, 0xf1, 0xfe, 0xc2, + ], + ), + ( + 0x10140, + [ + 0x8a, 0xca, 0x66, 0x8b, 0xd0, 0x66, 0xc1, 0xea, 0x10, 0xf7, 0x76, 0x1a, 0x86, 0xd6, 0x8a, 0x56, + 0x40, 0x8a, 0xe8, 0xc0, 0xe4, 0x06, 0x0a, 0xcc, 0xb8, 0x01, 0x02, 0xcd, 0x13, 0x66, 0x61, 0x0f, + ], + ), + ( + 0x10160, + [ + 0x82, 0x74, 0xff, 0x81, 0xc3, 0x00, 0x02, 0x66, 0x40, 0x49, 0x75, 0x94, 0xc3, 0x42, 0x4f, 0x4f, + 0x54, 0x4d, 0x47, 0x52, 0x20, 0x20, 0x20, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + ], + ), + ( + 0x101a0, + [ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0d, 0x0a, 0x44, 0x69, + 0x73, 0x6b, 0x20, 0x65, 0x72, 0x72, 0x6f, 0x72, 0xff, 0x0d, 0x0a, 0x50, 0x72, 0x65, 0x73, 0x73, + ], + ), + ( + 0x101c0, + [ + 0x20, 0x61, 0x6e, 0x79, 0x20, 0x6b, 0x65, 0x79, 0x20, 0x74, 0x6f, 0x20, 0x72, 0x65, 0x73, 0x74, + 0x61, 0x72, 0x74, 0x0d, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + ], + ), + ( + 0x101e0, + [ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xac, 0x01, 0xb9, 0x01, 0x00, 0x00, 0x55, 0xaa, + ], + ), + ( + 0x10200, + [ + 0x52, 0x52, 0x61, 0x41, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + ], + ), + ( + 0x103e0, + [ + 0x00, 0x00, 0x00, 0x00, 0x72, 0x72, 0x41, 0x61, 0xfc, 0x97, 0x03, 0x00, 0x06, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0xaa, + ], + ), + ( + 0x105e0, + [ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0xaa, + ], + ), + ( + 0x10c00, + [ + 0xeb, 0x58, 0x90, 0x4d, 0x53, 0x44, 0x4f, 0x53, 0x35, 0x2e, 0x30, 0x00, 0x02, 0x01, 0x9e, 0x11, + 0x02, 0x00, 0x00, 0x00, 0x00, 0xf8, 0x00, 0x00, 0x3f, 0x00, 0xff, 0x00, 0x80, 0x00, 0x00, 0x00, + ], + ), + ( + 0x10c20, + [ + 0x00, 0xb8, 0x03, 0x00, 0x31, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, + 0x01, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + ], + ), + ( + 0x10c40, + [ + 0x80, 0x00, 0x29, 0xcd, 0xcf, 0xdc, 0x46, 0x4e, 0x4f, 0x20, 0x4e, 0x41, 0x4d, 0x45, 0x20, 0x20, + 0x20, 0x20, 0x46, 0x41, 0x54, 0x33, 0x32, 0x20, 0x20, 0x20, 0x33, 0xc9, 0x8e, 0xd1, 0xbc, 0xf4, + ], + ), + ( + 0x10c60, + [ + 0x7b, 0x8e, 0xc1, 0x8e, 0xd9, 0xbd, 0x00, 0x7c, 0x88, 0x56, 0x40, 0x88, 0x4e, 0x02, 0x8a, 0x56, + 0x40, 0xb4, 0x41, 0xbb, 0xaa, 0x55, 0xcd, 0x13, 0x72, 0x10, 0x81, 0xfb, 0x55, 0xaa, 0x75, 0x0a, + ], + ), + ( + 0x10c80, + [ + 0xf6, 0xc1, 0x01, 0x74, 0x05, 0xfe, 0x46, 0x02, 0xeb, 0x2d, 0x8a, 0x56, 0x40, 0xb4, 0x08, 0xcd, + 0x13, 0x73, 0x05, 0xb9, 0xff, 0xff, 0x8a, 0xf1, 0x66, 0x0f, 0xb6, 0xc6, 0x40, 0x66, 0x0f, 0xb6, + ], + ), + ( + 0x10ca0, + [ + 0xd1, 0x80, 0xe2, 0x3f, 0xf7, 0xe2, 0x86, 0xcd, 0xc0, 0xed, 0x06, 0x41, 0x66, 0x0f, 0xb7, 0xc9, + 0x66, 0xf7, 0xe1, 0x66, 0x89, 0x46, 0xf8, 0x83, 0x7e, 0x16, 0x00, 0x75, 0x39, 0x83, 0x7e, 0x2a, + ], + ), + ( + 0x10cc0, + [ + 0x00, 0x77, 0x33, 0x66, 0x8b, 0x46, 0x1c, 0x66, 0x83, 0xc0, 0x0c, 0xbb, 0x00, 0x80, 0xb9, 0x01, + 0x00, 0xe8, 0x2c, 0x00, 0xe9, 0xa8, 0x03, 0xa1, 0xf8, 0x7d, 0x80, 0xc4, 0x7c, 0x8b, 0xf0, 0xac, + ], + ), + ( + 0x10ce0, + [ + 0x84, 0xc0, 0x74, 0x17, 0x3c, 0xff, 0x74, 0x09, 0xb4, 0x0e, 0xbb, 0x07, 0x00, 0xcd, 0x10, 0xeb, + 0xee, 0xa1, 0xfa, 0x7d, 0xeb, 0xe4, 0xa1, 0x7d, 0x80, 0xeb, 0xdf, 0x98, 0xcd, 0x16, 0xcd, 0x19, + ], + ), + ( + 0x10d00, + [ + 0x66, 0x60, 0x80, 0x7e, 0x02, 0x00, 0x0f, 0x84, 0x20, 0x00, 0x66, 0x6a, 0x00, 0x66, 0x50, 0x06, + 0x53, 0x66, 0x68, 0x10, 0x00, 0x01, 0x00, 0xb4, 0x42, 0x8a, 0x56, 0x40, 0x8b, 0xf4, 0xcd, 0x13, + ], + ), + ( + 0x10d20, + [ + 0x66, 0x58, 0x66, 0x58, 0x66, 0x58, 0x66, 0x58, 0xeb, 0x33, 0x66, 0x3b, 0x46, 0xf8, 0x72, 0x03, + 0xf9, 0xeb, 0x2a, 0x66, 0x33, 0xd2, 0x66, 0x0f, 0xb7, 0x4e, 0x18, 0x66, 0xf7, 0xf1, 0xfe, 0xc2, + ], + ), + ( + 0x10d40, + [ + 0x8a, 0xca, 0x66, 0x8b, 0xd0, 0x66, 0xc1, 0xea, 0x10, 0xf7, 0x76, 0x1a, 0x86, 0xd6, 0x8a, 0x56, + 0x40, 0x8a, 0xe8, 0xc0, 0xe4, 0x06, 0x0a, 0xcc, 0xb8, 0x01, 0x02, 0xcd, 0x13, 0x66, 0x61, 0x0f, + ], + ), + ( + 0x10d60, + [ + 0x82, 0x74, 0xff, 0x81, 0xc3, 0x00, 0x02, 0x66, 0x40, 0x49, 0x75, 0x94, 0xc3, 0x42, 0x4f, 0x4f, + 0x54, 0x4d, 0x47, 0x52, 0x20, 0x20, 0x20, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + ], + ), + ( + 0x10da0, + [ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0d, 0x0a, 0x44, 0x69, + 0x73, 0x6b, 0x20, 0x65, 0x72, 0x72, 0x6f, 0x72, 0xff, 0x0d, 0x0a, 0x50, 0x72, 0x65, 0x73, 0x73, + ], + ), + ( + 0x10dc0, + [ + 0x20, 0x61, 0x6e, 0x79, 0x20, 0x6b, 0x65, 0x79, 0x20, 0x74, 0x6f, 0x20, 0x72, 0x65, 0x73, 0x74, + 0x61, 0x72, 0x74, 0x0d, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + ], + ), + ( + 0x10de0, + [ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xac, 0x01, 0xb9, 0x01, 0x00, 0x00, 0x55, 0xaa, + ], + ), + ( + 0x10e00, + [ + 0x52, 0x52, 0x61, 0x41, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + ], + ), + ( + 0x10fe0, + [ + 0x00, 0x00, 0x00, 0x00, 0x72, 0x72, 0x41, 0x61, 0xff, 0xff, 0xff, 0xff, 0x02, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0xaa, + ], + ), + ( + 0x111e0, + [ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0xaa, + ], + ), + ( + 0x11800, + [ + 0x0d, 0x0a, 0x41, 0x6e, 0x20, 0x6f, 0x70, 0x65, 0x72, 0x61, 0x74, 0x69, 0x6e, 0x67, 0x20, 0x73, + 0x79, 0x73, 0x74, 0x65, 0x6d, 0x20, 0x77, 0x61, 0x73, 0x6e, 0x27, 0x74, 0x20, 0x66, 0x6f, 0x75, + ], + ), + ( + 0x11820, + [ + 0x6e, 0x64, 0x2e, 0x20, 0x54, 0x72, 0x79, 0x20, 0x64, 0x69, 0x73, 0x63, 0x6f, 0x6e, 0x6e, 0x65, + 0x63, 0x74, 0x69, 0x6e, 0x67, 0x20, 0x61, 0x6e, 0x79, 0x20, 0x64, 0x72, 0x69, 0x76, 0x65, 0x73, + ], + ), + ( + 0x11840, + [ + 0x20, 0x74, 0x68, 0x61, 0x74, 0x20, 0x64, 0x6f, 0x6e, 0x27, 0x74, 0x0d, 0x0a, 0x63, 0x6f, 0x6e, + 0x74, 0x61, 0x69, 0x6e, 0x20, 0x61, 0x6e, 0x20, 0x6f, 0x70, 0x65, 0x72, 0x61, 0x74, 0x69, 0x6e, + ], + ), + ( + 0x11860, + [ + 0x67, 0x20, 0x73, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x66, + ], + ), + ( + 0x11880, + [ + 0x0f, 0xb6, 0x46, 0x10, 0x66, 0x8b, 0x4e, 0x24, 0x66, 0xf7, 0xe1, 0x66, 0x03, 0x46, 0x1c, 0x66, + 0x0f, 0xb7, 0x56, 0x0e, 0x66, 0x03, 0xc2, 0x66, 0x89, 0x46, 0xfc, 0x66, 0xc7, 0x46, 0xf4, 0xff, + ], + ), + ( + 0x118a0, + [ + 0xff, 0xff, 0xff, 0x66, 0x8b, 0x46, 0x2c, 0x66, 0x83, 0xf8, 0x02, 0x0f, 0x82, 0x47, 0xfc, 0x66, + 0x3d, 0xf8, 0xff, 0xff, 0x0f, 0x0f, 0x83, 0x3d, 0xfc, 0x66, 0x50, 0x66, 0x83, 0xe8, 0x02, 0x66, + ], + ), + ( + 0x118c0, + [ + 0x0f, 0xb6, 0x5e, 0x0d, 0x8b, 0xf3, 0x66, 0xf7, 0xe3, 0x66, 0x03, 0x46, 0xfc, 0xbb, 0x00, 0x82, + 0x8b, 0xfb, 0xb9, 0x01, 0x00, 0xe8, 0x28, 0xfc, 0x38, 0x2d, 0x74, 0x1e, 0xb1, 0x0b, 0x56, 0xbe, + ], + ), + ( + 0x118e0, + [ + 0x6d, 0x7d, 0xf3, 0xa6, 0x5e, 0x74, 0x1b, 0x03, 0xf9, 0x83, 0xc7, 0x15, 0x3b, 0xfb, 0x72, 0xe8, + 0x4e, 0x75, 0xda, 0x66, 0x58, 0xe8, 0x65, 0x00, 0x72, 0xbf, 0x83, 0xc4, 0x04, 0xe9, 0xf6, 0xfb, + ], + ), + ( + 0x11900, + [ + 0x00, 0x20, 0x83, 0xc4, 0x04, 0x8b, 0x75, 0x09, 0x8b, 0x7d, 0x0f, 0x8b, 0xc6, 0x66, 0xc1, 0xe0, + 0x10, 0x8b, 0xc7, 0x66, 0x83, 0xf8, 0x02, 0x0f, 0x82, 0xdb, 0xfb, 0x66, 0x3d, 0xf8, 0xff, 0xff, + ], + ), + ( + 0x11920, + [ + 0x0f, 0x0f, 0x83, 0xd1, 0xfb, 0x66, 0x50, 0x66, 0x83, 0xe8, 0x02, 0x66, 0x0f, 0xb6, 0x4e, 0x0d, + 0x66, 0xf7, 0xe1, 0x66, 0x03, 0x46, 0xfc, 0xbb, 0x00, 0x00, 0x06, 0x8e, 0x06, 0x00, 0x81, 0xe8, + ], + ), + ( + 0x11940, + [ + 0xbe, 0xfb, 0x07, 0x66, 0x58, 0xc1, 0xeb, 0x04, 0x01, 0x1e, 0x00, 0x81, 0xe8, 0x0e, 0x00, 0x0f, + 0x83, 0x02, 0x00, 0x72, 0xd0, 0x8a, 0x56, 0x40, 0xea, 0x00, 0x00, 0x00, 0x20, 0x66, 0xc1, 0xe0, + ], + ), + ( + 0x11960, + [ + 0x02, 0xe8, 0x11, 0x00, 0x26, 0x66, 0x8b, 0x01, 0x66, 0x25, 0xff, 0xff, 0xff, 0x0f, 0x66, 0x3d, + 0xf8, 0xff, 0xff, 0x0f, 0xc3, 0xbf, 0x00, 0x7e, 0x66, 0x0f, 0xb7, 0x4e, 0x0b, 0x66, 0x33, 0xd2, + ], + ), + ( + 0x11980, + [ + 0x66, 0xf7, 0xf1, 0x66, 0x3b, 0x46, 0xf4, 0x74, 0x3a, 0x66, 0x89, 0x46, 0xf4, 0x66, 0x03, 0x46, + 0x1c, 0x66, 0x0f, 0xb7, 0x4e, 0x0e, 0x66, 0x03, 0xc1, 0x66, 0x0f, 0xb7, 0x5e, 0x28, 0x83, 0xe3, + ], + ), + ( + 0x119a0, + [ + 0x0f, 0x74, 0x16, 0x3a, 0x5e, 0x10, 0x0f, 0x83, 0x4c, 0xfb, 0x52, 0x66, 0x8b, 0xc8, 0x66, 0x8b, + 0x46, 0x24, 0x66, 0xf7, 0xe3, 0x66, 0x03, 0xc1, 0x5a, 0x52, 0x8b, 0xdf, 0xb9, 0x01, 0x00, 0xe8, + ], + ), + ( + 0x119c0, + [ + 0x3e, 0xfb, 0x5a, 0x8b, 0xda, 0xc3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + ], + ), + ( + 0x119e0, + [ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0xaa, + ], + ), +]; + +pub const SPARSE_FILE_DATA: [(usize, [u8; 32]); 18] = [ + ( + 0x243c00, + [ + 0xf8, 0xff, 0xff, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0f, 0xff, 0xff, 0xff, 0x0f, + 0xff, 0xff, 0xff, 0x0f, 0xff, 0xff, 0xff, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + ], + ), + ( + 0x329e00, + [ + 0xf8, 0xff, 0xff, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0f, 0xff, 0xff, 0xff, 0x0f, + 0xff, 0xff, 0xff, 0x0f, 0xff, 0xff, 0xff, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + ], + ), + ( + 0x410000, + [ + 0x42, 0x41, 0x4f, 0x43, 0x48, 0x49, 0x50, 0x20, 0x20, 0x20, 0x20, 0x08, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x99, 0x7b, 0x3e, 0x5b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + ], + ), + ( + 0x410020, + [ + 0x42, 0x20, 0x00, 0x49, 0x00, 0x6e, 0x00, 0x66, 0x00, 0x6f, 0x00, 0x0f, 0x00, 0x72, 0x72, 0x00, + 0x6d, 0x00, 0x61, 0x00, 0x74, 0x00, 0x69, 0x00, 0x6f, 0x00, 0x00, 0x00, 0x6e, 0x00, 0x00, 0x00, + ], + ), + ( + 0x410040, + [ + 0x01, 0x53, 0x00, 0x79, 0x00, 0x73, 0x00, 0x74, 0x00, 0x65, 0x00, 0x0f, 0x00, 0x72, 0x6d, 0x00, + 0x20, 0x00, 0x56, 0x00, 0x6f, 0x00, 0x6c, 0x00, 0x75, 0x00, 0x00, 0x00, 0x6d, 0x00, 0x65, 0x00, + ], + ), + ( + 0x410060, + [ + 0x53, 0x59, 0x53, 0x54, 0x45, 0x4d, 0x7e, 0x31, 0x20, 0x20, 0x20, 0x16, 0x00, 0xb7, 0x98, 0x7b, + 0x3e, 0x5b, 0x3e, 0x5b, 0x00, 0x00, 0x99, 0x7b, 0x3e, 0x5b, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, + ], + ), + ( + 0x410200, + [ + 0x2e, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x10, 0x00, 0xb7, 0x98, 0x7b, + 0x3e, 0x5b, 0x3e, 0x5b, 0x00, 0x00, 0x99, 0x7b, 0x3e, 0x5b, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, + ], + ), + ( + 0x410220, + [ + 0x2e, 0x2e, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x10, 0x00, 0xb7, 0x98, 0x7b, + 0x3e, 0x5b, 0x3e, 0x5b, 0x00, 0x00, 0x99, 0x7b, 0x3e, 0x5b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + ], + ), + ( + 0x410240, + [ + 0x42, 0x74, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0f, 0x00, 0xce, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, + ], + ), + ( + 0x410260, + [ + 0x01, 0x57, 0x00, 0x50, 0x00, 0x53, 0x00, 0x65, 0x00, 0x74, 0x00, 0x0f, 0x00, 0xce, 0x74, 0x00, + 0x69, 0x00, 0x6e, 0x00, 0x67, 0x00, 0x73, 0x00, 0x2e, 0x00, 0x00, 0x00, 0x64, 0x00, 0x61, 0x00, + ], + ), + ( + 0x410280, + [ + 0x57, 0x50, 0x53, 0x45, 0x54, 0x54, 0x7e, 0x31, 0x44, 0x41, 0x54, 0x20, 0x00, 0xb8, 0x98, 0x7b, + 0x3e, 0x5b, 0x3e, 0x5b, 0x00, 0x00, 0x99, 0x7b, 0x3e, 0x5b, 0x04, 0x00, 0x0c, 0x00, 0x00, 0x00, + ], + ), + ( + 0x4102a0, + [ + 0x42, 0x47, 0x00, 0x75, 0x00, 0x69, 0x00, 0x64, 0x00, 0x00, 0x00, 0x0f, 0x00, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, + ], + ), + ( + 0x4102c0, + [ + 0x01, 0x49, 0x00, 0x6e, 0x00, 0x64, 0x00, 0x65, 0x00, 0x78, 0x00, 0x0f, 0x00, 0xff, 0x65, 0x00, + 0x72, 0x00, 0x56, 0x00, 0x6f, 0x00, 0x6c, 0x00, 0x75, 0x00, 0x00, 0x00, 0x6d, 0x00, 0x65, 0x00, + ], + ), + ( + 0x4102e0, + [ + 0x49, 0x4e, 0x44, 0x45, 0x58, 0x45, 0x7e, 0x31, 0x20, 0x20, 0x20, 0x20, 0x00, 0xbc, 0x9a, 0x7b, + 0x3e, 0x5b, 0x3e, 0x5b, 0x00, 0x00, 0x9b, 0x7b, 0x3e, 0x5b, 0x05, 0x00, 0x4c, 0x00, 0x00, 0x00, + ], + ), + ( + 0x410400, + [ + 0x0c, 0x00, 0x00, 0x00, 0xa1, 0xc2, 0x8e, 0x0b, 0x20, 0x83, 0x91, 0x54, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + ], + ), + ( + 0x410600, + [ + 0x7b, 0x00, 0x43, 0x00, 0x46, 0x00, 0x41, 0x00, 0x35, 0x00, 0x43, 0x00, 0x45, 0x00, 0x30, 0x00, + 0x43, 0x00, 0x2d, 0x00, 0x34, 0x00, 0x37, 0x00, 0x35, 0x00, 0x43, 0x00, 0x2d, 0x00, 0x34, 0x00, + ], + ), + ( + 0x410620, + [ + 0x44, 0x00, 0x34, 0x00, 0x31, 0x00, 0x2d, 0x00, 0x39, 0x00, 0x33, 0x00, 0x30, 0x00, 0x36, 0x00, + 0x2d, 0x00, 0x44, 0x00, 0x41, 0x00, 0x44, 0x00, 0x30, 0x00, 0x45, 0x00, 0x45, 0x00, 0x43, 0x00, + ], + ), + ( + 0x410640, + [ + 0x41, 0x00, 0x39, 0x00, 0x45, 0x00, 0x39, 0x00, 0x31, 0x00, 0x7d, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + ], + ), +]; diff --git a/bao1x-boot/boot1/src/platform/bao1x/usb/glue.rs b/bao1x-boot/boot1/src/platform/bao1x/usb/glue.rs index 6ac129dd6..a504e5007 100644 --- a/bao1x-boot/boot1/src/platform/bao1x/usb/glue.rs +++ b/bao1x-boot/boot1/src/platform/bao1x/usb/glue.rs @@ -1,7 +1,13 @@ -use bao1x_hal::usb::driver::UsbDeviceState; +use core::cell::RefCell; +use bao1x_hal::usb::driver::UsbDeviceState; +use critical_section::Mutex; + +use crate::uf2::Uf2Block; use crate::usb; +pub(crate) static SECTOR: Mutex> = Mutex::new(RefCell::new(Uf2Sector::new(0))); + // Empirically measured PORTSC when the port is unplugged. This might be a brittle way // to detect if the device is unplugged. const DISCONNECT_STATE: u32 = 0x40b; // 01_0_0000_0_1_01_1 @@ -11,9 +17,10 @@ pub fn is_disconnected(state: u32) -> bool { state == DISCONNECT_STATE_HS || sta pub fn setup() -> (UsbDeviceState, u32) { crate::println!( - "RAM disk starts at {:x} and is {}kiB in length", + "RAM disk starts at {:x} and advertises {}kiB in length, but is actually {}kiB of storage", usb::RAMDISK_ADDRESS, - usb::RAMDISK_LEN / 1024 + usb::RAMDISK_LEN / 1024, + usb::RAMDISK_ACTUAL_LEN / 1024, ); // safety: this is safe because we're calling this before any access to `USB` static mut @@ -48,7 +55,7 @@ pub fn setup() -> (UsbDeviceState, u32) { let last_usb_state = usb.get_device_state(); let portsc = usb.portsc_val(); - crate::println!("USB state: {:?}, {:x}", last_usb_state, portsc); + crate::println_d!("USB state: {:?}, {:x}", last_usb_state, portsc); (last_usb_state, portsc) } else { panic!("USB core not allocated, can't proceed!"); @@ -77,3 +84,55 @@ pub fn flush_tx() { } } } + +pub struct Uf2Sector { + pub address: usize, + pub data: [u8; 512], + // pointer that tracks how far we've written into the `data` array + pub progress: usize, +} + +impl Uf2Sector { + pub const fn new(address: usize) -> Self { Self { address, data: [0u8; 512], progress: 0 } } + + /// Takes in a slice of incoming data, and a notional "disk address" into which it should be writing + pub fn extend_from_slice(&mut self, address: usize, slice: &[u8]) -> (Option, Option) { + if address != self.address + self.progress { + crate::println!( + "Resetting sector address tracker, expected {:x} got {:x}", + self.address + self.progress, + address + ); + self.address = address % 512; + } + + let copylen = (self.data.len() - self.progress).min(slice.len()); + for (dst, &src) in self.data[self.progress..].iter_mut().zip(slice.iter()) { + *dst = src; + } + self.progress += copylen; + + // note that the Uf2Block::from_bytes() function gracefully fails and returns None + // in the case that the user wrote non-uf2 data to our "disk" + let decoded = if self.progress >= self.data.len() { Uf2Block::from_bytes(&self.data) } else { None }; + + let new_sector = if copylen < slice.len() { + // handle the case that we had too much data for this sector + let mut sector = Uf2Sector::new(self.address + self.data.len()); + for (dst, &src) in sector.data.iter_mut().zip(&slice[copylen..]) { + *dst = src; + } + sector.progress = slice.len() - copylen; + Some(sector) + } else { + if copylen == slice.len() && self.progress >= self.data.len() { + // handles the case that we just finished the sector nicely + Some(Uf2Sector::new(self.address + self.data.len())) + } else { + None + } + }; + + (new_sector, decoded) + } +} diff --git a/bao1x-boot/boot1/src/platform/bao1x/usb/handlers.rs b/bao1x-boot/boot1/src/platform/bao1x/usb/handlers.rs index 49038eacb..05e2dbf25 100644 --- a/bao1x-boot/boot1/src/platform/bao1x/usb/handlers.rs +++ b/bao1x-boot/boot1/src/platform/bao1x/usb/handlers.rs @@ -2,110 +2,41 @@ use core::sync::atomic::Ordering; use bao1x_hal::usb::driver::CorigineUsb; use bao1x_hal::usb::driver::*; +use utralib::HW_IFRAM1_MEM; use super::*; pub static TX_IDLE: core::sync::atomic::AtomicBool = core::sync::atomic::AtomicBool::new(true); // Locate the "disk" -pub(crate) const RAMDISK_ADDRESS: usize = crate::platform::HEAP_START + crate::platform::HEAP_LEN; -pub(crate) const STACK_SIZE: usize = 128 * 1024; // 128k for stack is enough? maybe? if the ramdisk overflows, it smashes stack - dangerous! -pub(crate) const RAMDISK_LEN: usize = - crate::platform::RAM_SIZE - (RAMDISK_ADDRESS - crate::platform::RAM_BASE) - STACK_SIZE; +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; -// MBR template -// 0x0b~0x0C 2 bytes means block size, default 0x200 bytes -// 0x20~0x23 4 bytes means block number, default 0x400 block -#[rustfmt::skip] // keep this in 16-byte width -pub(crate) const MBR_TEMPLATE: [u8; SECTOR_SIZE as usize] = [ - 0xEB, 0x3C, 0x90, 0x4D, 0x53, 0x44, 0x4F, 0x53, 0x35, 0x2E, 0x30, 0x00, 0x02, 0x20, 0x06, 0x00, - 0x02, 0x00, 0x02, 0x00, 0x0C, 0xF8, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x29, 0xBB, 0xBF, 0xB7, 0xB0, 0x4E, 0x4F, 0x20, 0x4E, 0x41, - 0x4D, 0x45, 0x20, 0x20, 0x20, 0x20, 0x46, 0x41, 0x54, 0x31, 0x32, 0x20, 0x20, 0x20, 0x33, 0xC9, - 0x8E, 0xD1, 0xBC, 0xF0, 0x7B, 0x8E, 0xD9, 0xB8, 0x00, 0x20, 0x8E, 0xC0, 0xFC, 0xBD, 0x00, 0x7C, - 0x38, 0x4E, 0x24, 0x7D, 0x24, 0x8B, 0xC1, 0x99, 0xE8, 0x3C, 0x01, 0x72, 0x1C, 0x83, 0xEB, 0x3A, - 0x66, 0xA1, 0x1C, 0x7C, 0x26, 0x66, 0x3B, 0x07, 0x26, 0x8A, 0x57, 0xFC, 0x75, 0x06, 0x80, 0xCA, - 0x02, 0x88, 0x56, 0x02, 0x80, 0xC3, 0x10, 0x73, 0xEB, 0x33, 0xC9, 0x8A, 0x46, 0x10, 0x98, 0xF7, - 0x66, 0x16, 0x03, 0x46, 0x1C, 0x13, 0x56, 0x1E, 0x03, 0x46, 0x0E, 0x13, 0xD1, 0x8B, 0x76, 0x11, - 0x60, 0x89, 0x46, 0xFC, 0x89, 0x56, 0xFE, 0xB8, 0x20, 0x00, 0xF7, 0xE6, 0x8B, 0x5E, 0x0B, 0x03, - 0xC3, 0x48, 0xF7, 0xF3, 0x01, 0x46, 0xFC, 0x11, 0x4E, 0xFE, 0x61, 0xBF, 0x00, 0x00, 0xE8, 0xE6, - 0x00, 0x72, 0x39, 0x26, 0x38, 0x2D, 0x74, 0x17, 0x60, 0xB1, 0x0B, 0xBE, 0xA1, 0x7D, 0xF3, 0xA6, - 0x61, 0x74, 0x32, 0x4E, 0x74, 0x09, 0x83, 0xC7, 0x20, 0x3B, 0xFB, 0x72, 0xE6, 0xEB, 0xDC, 0xA0, - 0xFB, 0x7D, 0xB4, 0x7D, 0x8B, 0xF0, 0xAC, 0x98, 0x40, 0x74, 0x0C, 0x48, 0x74, 0x13, 0xB4, 0x0E, - 0xBB, 0x07, 0x00, 0xCD, 0x10, 0xEB, 0xEF, 0xA0, 0xFD, 0x7D, 0xEB, 0xE6, 0xA0, 0xFC, 0x7D, 0xEB, - 0xE1, 0xCD, 0x16, 0xCD, 0x19, 0x26, 0x8B, 0x55, 0x1A, 0x52, 0xB0, 0x01, 0xBB, 0x00, 0x00, 0xE8, - 0x3B, 0x00, 0x72, 0xE8, 0x5B, 0x8A, 0x56, 0x24, 0xBE, 0x0B, 0x7C, 0x8B, 0xFC, 0xC7, 0x46, 0xF0, - 0x3D, 0x7D, 0xC7, 0x46, 0xF4, 0x29, 0x7D, 0x8C, 0xD9, 0x89, 0x4E, 0xF2, 0x89, 0x4E, 0xF6, 0xC6, - 0x06, 0x96, 0x7D, 0xCB, 0xEA, 0x03, 0x00, 0x00, 0x20, 0x0F, 0xB6, 0xC8, 0x66, 0x8B, 0x46, 0xF8, - 0x66, 0x03, 0x46, 0x1C, 0x66, 0x8B, 0xD0, 0x66, 0xC1, 0xEA, 0x10, 0xEB, 0x5E, 0x0F, 0xB6, 0xC8, - 0x4A, 0x4A, 0x8A, 0x46, 0x0D, 0x32, 0xE4, 0xF7, 0xE2, 0x03, 0x46, 0xFC, 0x13, 0x56, 0xFE, 0xEB, - 0x4A, 0x52, 0x50, 0x06, 0x53, 0x6A, 0x01, 0x6A, 0x10, 0x91, 0x8B, 0x46, 0x18, 0x96, 0x92, 0x33, - 0xD2, 0xF7, 0xF6, 0x91, 0xF7, 0xF6, 0x42, 0x87, 0xCA, 0xF7, 0x76, 0x1A, 0x8A, 0xF2, 0x8A, 0xE8, - 0xC0, 0xCC, 0x02, 0x0A, 0xCC, 0xB8, 0x01, 0x02, 0x80, 0x7E, 0x02, 0x0E, 0x75, 0x04, 0xB4, 0x42, - 0x8B, 0xF4, 0x8A, 0x56, 0x24, 0xCD, 0x13, 0x61, 0x61, 0x72, 0x0B, 0x40, 0x75, 0x01, 0x42, 0x03, - 0x5E, 0x0B, 0x49, 0x75, 0x06, 0xF8, 0xC3, 0x41, 0xBB, 0x00, 0x00, 0x60, 0x66, 0x6A, 0x00, 0xEB, - 0xB0, 0x42, 0x4F, 0x4F, 0x54, 0x4D, 0x47, 0x52, 0x20, 0x20, 0x20, 0x20, 0x0D, 0x0A, 0x52, 0x65, - 0x6D, 0x6F, 0x76, 0x65, 0x20, 0x64, 0x69, 0x73, 0x6B, 0x73, 0x20, 0x6F, 0x72, 0x20, 0x6F, 0x74, - 0x68, 0x65, 0x72, 0x20, 0x6D, 0x65, 0x64, 0x69, 0x61, 0x2E, 0xFF, 0x0D, 0x0A, 0x44, 0x69, 0x73, - 0x6B, 0x20, 0x65, 0x72, 0x72, 0x6F, 0x72, 0xFF, 0x0D, 0x0A, 0x50, 0x72, 0x65, 0x73, 0x73, 0x20, - 0x61, 0x6E, 0x79, 0x20, 0x6B, 0x65, 0x79, 0x20, 0x74, 0x6F, 0x20, 0x72, 0x65, 0x73, 0x74, 0x61, - 0x72, 0x74, 0x0D, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xAC, 0xCB, 0xD8, 0x55, 0xAA, -]; +fn fill_sparse_data(dest: &mut [u8], offset: usize) { + let dest_start = offset; + let dest_end = offset + dest.len(); + // initialize to 0 + dest.fill(0); + // then fill in sparse data + for &(addr, block) in super::fat32_base::SPARSE_FILE_DATA.iter() { + // blocks always aligned to the dest, so copies always start from 0 on the block + assert!(offset % block.len() == 0); + let block_start = addr; + let block_end = addr + block.len(); -pub(crate) const FAT_TABLE: [u8; 0x08] = [0xF8, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F]; -pub(crate) const TABLE_OFFSET_FAT1: usize = 0xC00; -pub(crate) const TABLE_OFFSET_FAT2: usize = 0xE00; + // Check if block overlaps the destination window + if block_end <= dest_start || block_start >= dest_end { + continue; // no overlap + } -#[rustfmt::skip] // keep this in 16-byte width -pub (crate) const ROOT_DIR: [u8; 0x80] = [ - 0x42, 0x41, 0x4F, 0x53, 0x45, 0x43, 0x20, 0x20, 0x20, 0x20, 0x20, 0x08, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xCD, 0x78, 0x74, 0x59, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x42, 0x20, 0x00, 0x49, 0x00, 0x6E, 0x00, 0x66, 0x00, 0x6F, 0x00, 0x0F, 0x00, 0x72, 0x72, 0x00, - 0x6D, 0x00, 0x61, 0x00, 0x74, 0x00, 0x69, 0x00, 0x6F, 0x00, 0x00, 0x00, 0x6E, 0x00, 0x00, 0x00, - 0x01, 0x53, 0x00, 0x79, 0x00, 0x73, 0x00, 0x74, 0x00, 0x65, 0x00, 0x0F, 0x00, 0x72, 0x6D, 0x00, - 0x20, 0x00, 0x56, 0x00, 0x6F, 0x00, 0x6C, 0x00, 0x75, 0x00, 0x00, 0x00, 0x6D, 0x00, 0x65, 0x00, - 0x53, 0x59, 0x53, 0x54, 0x45, 0x4D, 0x7E, 0x31, 0x20, 0x20, 0x20, 0x16, 0x00, 0x36, 0xCF, 0x78, - 0x74, 0x59, 0x74, 0x59, 0x00, 0x00, 0xD0, 0x78, 0x74, 0x59, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, -]; -pub(crate) const ROOT_DIR_OFFSET: usize = 0x1000; + let write_start_in_dest = block_start.saturating_sub(dest_start); + let copy_len = (dest.len().saturating_sub(write_start_in_dest)).min(block.len()); -#[rustfmt::skip] // keep this in 16-byte width -pub (crate) const FAT_DATA1: [u8; 0x100] = [ - 0x2E, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x10, 0x00, 0x36, 0xCF, 0x78, - 0x74, 0x59, 0x74, 0x59, 0x00, 0x00, 0xD0, 0x78, 0x74, 0x59, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x2E, 0x2E, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x10, 0x00, 0x36, 0xCF, 0x78, - 0x74, 0x59, 0x74, 0x59, 0x00, 0x00, 0xD0, 0x78, 0x74, 0x59, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x42, 0x47, 0x00, 0x75, 0x00, 0x69, 0x00, 0x64, 0x00, 0x00, 0x00, 0x0F, 0x00, 0xFF, 0xFF, 0xFF, - 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, - 0x01, 0x49, 0x00, 0x6E, 0x00, 0x64, 0x00, 0x65, 0x00, 0x78, 0x00, 0x0F, 0x00, 0xFF, 0x65, 0x00, - 0x72, 0x00, 0x56, 0x00, 0x6F, 0x00, 0x6C, 0x00, 0x75, 0x00, 0x00, 0x00, 0x6D, 0x00, 0x65, 0x00, - 0x49, 0x4E, 0x44, 0x45, 0x58, 0x45, 0x7E, 0x31, 0x20, 0x20, 0x20, 0x20, 0x00, 0x4E, 0xCF, 0x78, - 0x74, 0x59, 0x74, 0x59, 0x00, 0x00, 0xD0, 0x78, 0x74, 0x59, 0x03, 0x00, 0x4C, 0x00, 0x00, 0x00, - 0x42, 0x74, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F, 0x00, 0xCE, 0xFF, 0xFF, - 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, - 0x01, 0x57, 0x00, 0x50, 0x00, 0x53, 0x00, 0x65, 0x00, 0x74, 0x00, 0x0F, 0x00, 0xCE, 0x74, 0x00, - 0x69, 0x00, 0x6E, 0x00, 0x67, 0x00, 0x73, 0x00, 0x2E, 0x00, 0x00, 0x00, 0x64, 0x00, 0x61, 0x00, - 0x57, 0x50, 0x53, 0x45, 0x54, 0x54, 0x7E, 0x31, 0x44, 0x41, 0x54, 0x20, 0x00, 0xB5, 0xD8, 0x78, - 0x74, 0x59, 0x74, 0x59, 0x00, 0x00, 0xD9, 0x78, 0x74, 0x59, 0x04, 0x00, 0x0C, 0x00, 0x00, 0x00, -]; -pub(crate) const FAT_DATA1_OFFSET: usize = 0x5000; - -#[rustfmt::skip] // keep this in 16-byte width -pub(crate) const FAT_DATA2: [u8; 0x50] = [ - 0x7B, 0x00, 0x34, 0x00, 0x33, 0x00, 0x39, 0x00, 0x44, 0x00, 0x41, 0x00, 0x31, 0x00, 0x32, 0x00, - 0x44, 0x00, 0x2D, 0x00, 0x37, 0x00, 0x33, 0x00, 0x42, 0x00, 0x39, 0x00, 0x2D, 0x00, 0x34, 0x00, - 0x43, 0x00, 0x41, 0x00, 0x34, 0x00, 0x2D, 0x00, 0x38, 0x00, 0x38, 0x00, 0x46, 0x00, 0x39, 0x00, - 0x2D, 0x00, 0x44, 0x00, 0x41, 0x00, 0x32, 0x00, 0x43, 0x00, 0x46, 0x00, 0x33, 0x00, 0x31, 0x00, - 0x38, 0x00, 0x45, 0x00, 0x32, 0x00, 0x37, 0x00, 0x35, 0x00, 0x7D, 0x00, 0x00, 0x00, 0x00, 0x00, -]; -pub(crate) const FAT_DATA2_OFFSET: usize = 0x9000; - -#[rustfmt::skip] // keep this in 16-byte width -pub(crate) const FAT_DATA3: [u8; 0x10] = [ - 0x0C, 0x00, 0x00, 0x00, 0x28, 0x34, 0x07, 0x9C, 0x7C, 0xBD, 0x65, 0xF9, 0x00, 0x00, 0x00, 0x00, -]; -pub(crate) const FAT_DATA3_OFFSET: usize = 0xD000; + dest[write_start_in_dest..write_start_in_dest + copy_len].copy_from_slice(&block[..copy_len]); + } +} pub(crate) const CSW_ADDR: usize = CRG_UDC_MEMBASE + CRG_UDC_APP_BUFOFFSET + CRG_UDC_APP_BUF_LEN; pub(crate) const CBW_ADDR: usize = CRG_UDC_MEMBASE + CRG_UDC_APP_BUFOFFSET; @@ -249,13 +180,53 @@ pub fn usb_ep1_bulk_out_complete( this.ep_halt(1, USB_RECV); // invalid_cbw = 1; } else if UmsState::DataPhase == this.ms_state { - crate::println_d!("data"); + // crate::println_d!("data"); //DATA if let Some((write_offset, len)) = this.callback_wr.take() { let app_buf = conjure_app_buf(); let disk = conjure_disk(); - // update the received data to the disk - disk[write_offset..write_offset + len].copy_from_slice(&app_buf[..len]); + if false { + // update the received data to the disk + disk[write_offset..write_offset + len].copy_from_slice(&app_buf[..len]); + } else { + // process the received data as if it were a uf2 sector + for (i, chunk) in app_buf[..len].chunks(512).enumerate() { + let (new_block, uf2_data) = critical_section::with(|cs| { + super::glue::SECTOR + .borrow(cs) + .borrow_mut() + .extend_from_slice(write_offset + i * 512, chunk) + }); + // program the flash if a valid u2f block was found + if let Some(record) = uf2_data { + if record.address() as usize >= bao1x_api::BAREMETAL_START + && (record.address() as usize) + < utralib::HW_RERAM_MEM + bao1x_api::RRAM_STORAGE_LEN + && record.family() == bao1x_api::BAOCHIP_1X_UF2_FAMILY + { + let mut rram = bao1x_hal::rram::Reram::new(); + let offset = record.address() as usize - utralib::HW_RERAM_MEM; + rram.write_slice(offset, record.data()); + crate::println_d!( + "Wrote {} to 0x{:x}: {:x?}", + record.data().len(), + record.address(), + &record.data()[..8] + ); + } else { + crate::println_d!("Invalid write address {:x}, block ignored!", record.address()); + } + } + // replace the tracking block with the new one, if a new one was provided + if let Some(block) = new_block { + critical_section::with(|cs| super::glue::SECTOR.borrow(cs).replace(block)); + } + } + if write_offset + len < disk.len() { + // update the received data to the disk if it fits within the allocated region + disk[write_offset..write_offset + len].copy_from_slice(&app_buf[..len]); + } + } if let Some((offset, remaining_len)) = this.remaining_wr.take() { this.setup_big_write(app_buf_addr(), app_buf_len(), offset, remaining_len); this.ms_state = UmsState::DataPhase; @@ -287,7 +258,7 @@ pub fn usb_ep1_bulk_in_complete( if let Some((offset, len)) = this.remaining_rd.take() { let app_buf = conjure_app_buf(); let disk = conjure_disk(); - this.setup_big_read(app_buf, disk, offset, len); + this.setup_big_read(app_buf, disk, offset, len, Some(fill_sparse_data)); this.ms_state = UmsState::DataPhase; } else { this.bulk_xfer(1, USB_SEND, CSW_ADDR, 13, 0, 0); @@ -656,7 +627,13 @@ fn process_read_command(this: &mut CorigineUsb, cbw: Cbw) { } let app_buf = conjure_app_buf(); let disk = conjure_disk(); - this.setup_big_read(app_buf, disk, lba as usize * SECTOR_SIZE as usize, length as usize); + this.setup_big_read( + app_buf, + disk, + lba as usize * SECTOR_SIZE as usize, + length as usize, + Some(fill_sparse_data), + ); this.ms_state = UmsState::DataPhase; } } @@ -679,7 +656,13 @@ fn process_write_command(this: &mut CorigineUsb, cbw: Cbw) { csw.status = 2; let app_buf = conjure_app_buf(); let disk = conjure_disk(); - this.setup_big_read(app_buf, disk, lba as usize * SECTOR_SIZE as usize, length as usize); + this.setup_big_read( + app_buf, + disk, + lba as usize * SECTOR_SIZE as usize, + length as usize, + Some(fill_sparse_data), + ); this.ms_state = UmsState::DataPhase; csw.update_hw(); return; @@ -743,7 +726,13 @@ fn process_write12_command(this: &mut CorigineUsb, cbw: Cbw) { // does let app_buf = conjure_app_buf(); let disk = conjure_disk(); - this.setup_big_read(app_buf, disk, lba as usize * SECTOR_SIZE as usize, length as usize); + this.setup_big_read( + app_buf, + disk, + lba as usize * SECTOR_SIZE as usize, + length as usize, + Some(fill_sparse_data), + ); this.ms_state = UmsState::DataPhase; csw.update_hw(); return; @@ -776,14 +765,14 @@ fn process_write12_command(this: &mut CorigineUsb, cbw: Cbw) { csw.update_hw(); } -// the 1024 is reserved for the CSW/CBW records -pub(crate) fn app_buf_addr() -> usize { CRG_UDC_MEMBASE + CRG_UDC_APP_BUFOFFSET + 1024 } -// the length subtracts the reserved 1024, and adds one page as a hack for testing - TODO: fix that -pub(crate) fn app_buf_len() -> usize { CRG_UDC_APP_BUFSIZE - 1024 + 4096 } +// Place the application buf in IFRAM1 range - this removes any conflicts with the USB stack's IFRAM0 +// allocations +pub(crate) fn app_buf_addr() -> usize { HW_IFRAM1_MEM } +pub(crate) fn app_buf_len() -> usize { 4096 * 2 } pub(crate) fn conjure_app_buf() -> &'static mut [u8] { - unsafe { core::slice::from_raw_parts_mut(app_buf_addr() as *mut u8, app_buf_len()) } // added +1 page in CRG_UDC_MEMBASE + unsafe { core::slice::from_raw_parts_mut(app_buf_addr() as *mut u8, app_buf_len()) } } pub(crate) fn conjure_disk() -> &'static mut [u8] { - unsafe { core::slice::from_raw_parts_mut(RAMDISK_ADDRESS as *mut u8, RAMDISK_LEN) } + unsafe { core::slice::from_raw_parts_mut(RAMDISK_ADDRESS as *mut u8, RAMDISK_ACTUAL_LEN) } } diff --git a/bao1x-boot/boot1/src/platform/bao1x/usb/mod.rs b/bao1x-boot/boot1/src/platform/bao1x/usb/mod.rs index 452ebf9f0..156e1e139 100644 --- a/bao1x-boot/boot1/src/platform/bao1x/usb/mod.rs +++ b/bao1x-boot/boot1/src/platform/bao1x/usb/mod.rs @@ -1,7 +1,7 @@ mod driver; +mod fat32_base; pub mod glue; mod handlers; - pub use driver::*; pub use handlers::*; diff --git a/bao1x-boot/boot1/src/repl.rs b/bao1x-boot/boot1/src/repl.rs index f02dce11b..79f698d3c 100644 --- a/bao1x-boot/boot1/src/repl.rs +++ b/bao1x-boot/boot1/src/repl.rs @@ -123,17 +123,29 @@ impl Repl { self.local_echo = false; } } - "autoboot" => { - // this toggles the autoboot flag by incrementing the one-way counter associated with - // autobooting. - let one_way = OneWayCounter::new(); - match one_way.inc_coded::() { - Ok(_) => { - let autoboot_state = - one_way.get_decoded::().expect("couldn't fetch flag"); - crate::println!("autoboot is now set to {:?}", autoboot_state); + "bootwait" => { + if args.len() != 1 { + return Err(Error::help("bootwait [check | toggle]")); + } + if args[0] == "toggle" { + // this toggles the bootwait flag by incrementing its one-way counter + let one_way = OneWayCounter::new(); + match one_way.inc_coded::() { + Ok(_) => { + let state = one_way + .get_decoded::() + .expect("couldn't fetch flag"); + crate::println!("bootwait is now set to {:?}", state); + } + Err(e) => crate::println!("Couldn't toggle bootwait: {:?}", e), } - Err(e) => crate::println!("Couldn't toggle autoboot: {:?}", e), + } else if args[0] == "check" { + let one_way = OneWayCounter::new(); + let state = + one_way.get_decoded::().expect("couldn't fetch flag"); + crate::println!("bootwait is {:?}", state); + } else { + return Err(Error::help("bootwait [check | toggle]")); } } #[cfg(feature = "unsafe-debug")] @@ -174,7 +186,7 @@ impl Repl { } _ => { crate::println!("Command not recognized: {}", cmd); - crate::print!("Commands include: reset, echo, boot, autoboot, localecho, uf2"); + crate::print!("Commands include: reset, echo, boot, bootwait, localecho, uf2"); crate::println!(""); } }