firmware is losing its length?
This commit is contained in:
		| @@ -18,21 +18,23 @@ pub(crate) async fn upload_bluetooth_firmware<PWR: OutputPin, SPI: SpiBusCyw43>( | ||||
|     firmware_offsets: &[(u32, usize)], | ||||
|     firmware: &[u8], | ||||
| ) { | ||||
|     assert!(firmware.len() == 5952); | ||||
|     // read version | ||||
|     let version_length = firmware[0]; | ||||
|     let _version = &firmware[1..=version_length as usize]; | ||||
|     // skip version + 1 extra byte as per cybt_shared_bus_driver.c | ||||
|     let firmware = &firmware[version_length as usize + 2..]; | ||||
|     // buffer | ||||
|     let mut data_buffer: [u8; 0x100] = [0; 0x100]; | ||||
|     let mut aligned_data_buffer: [u8; 0x100] = [0; 0x100]; | ||||
|     // structs | ||||
|     let mut pointer = 0; | ||||
|     for (index, &(dest_addr, num_fw_bytes)) in firmware_offsets.iter().enumerate() { | ||||
|         Timer::after(Duration::from_millis(100)).await; | ||||
|         let fw_bytes = &firmware[(pointer)..(pointer + num_fw_bytes)]; | ||||
|         assert!(fw_bytes.len() == num_fw_bytes); | ||||
|         pointer += num_fw_bytes; | ||||
|         debug!("index = {}/{} dest_addr = {:08x} num_fw_bytes = {} fw_bytes = {:02x}", index, firmware_offsets.len(), dest_addr, num_fw_bytes, fw_bytes); | ||||
|         //debug!("index = {}/{} dest_addr = {:08x} num_fw_bytes = {} fw_bytes = {:02x}", index, firmware_offsets.len(), dest_addr, num_fw_bytes, fw_bytes); | ||||
|         debug!("index = {}/{} dest_addr = {:08x} num_fw_bytes = {} pointer = {}", index, firmware_offsets.len(), dest_addr, num_fw_bytes, pointer); | ||||
|         assert!(firmware.len() == 5952); | ||||
|         let mut dest_start_addr = dest_addr; | ||||
|         let mut aligned_data_buffer_index: usize = 0; | ||||
|         // pad start | ||||
| @@ -41,7 +43,7 @@ pub(crate) async fn upload_bluetooth_firmware<PWR: OutputPin, SPI: SpiBusCyw43>( | ||||
|             let padded_dest_start_addr = round_down(dest_start_addr, 4); | ||||
|             let mut memory_value_bytes = [0; 4]; | ||||
|             bus.bp_read(padded_dest_start_addr, &mut memory_value_bytes).await; | ||||
|             debug!("pad start padded_dest_start_addr = {:08x} memory_value_bytes = {:02x}", padded_dest_start_addr, memory_value_bytes); | ||||
|             //debug!("pad start padded_dest_start_addr = {:08x} memory_value_bytes = {:02x}", padded_dest_start_addr, memory_value_bytes); | ||||
|             // Copy the previous memory value's bytes to the start | ||||
|             for i in 0..num_pad_bytes as usize { | ||||
|                 aligned_data_buffer[aligned_data_buffer_index] = memory_value_bytes[i]; | ||||
| @@ -68,7 +70,7 @@ pub(crate) async fn upload_bluetooth_firmware<PWR: OutputPin, SPI: SpiBusCyw43>( | ||||
|             let padded_dest_end_addr = round_down(dest_end_addr, 4); | ||||
|             let mut memory_value_bytes = [0; 4]; | ||||
|             bus.bp_read(padded_dest_end_addr, &mut memory_value_bytes).await; | ||||
|             debug!("pad end padded_dest_end_addr = {:08x} memory_value_bytes = {:02x}", padded_dest_end_addr, memory_value_bytes); | ||||
|             //debug!("pad end padded_dest_end_addr = {:08x} memory_value_bytes = {:02x}", padded_dest_end_addr, memory_value_bytes); | ||||
|             // Append the necessary memory bytes to pad the end of aligned_data_buffer | ||||
|             for i in offset..4 { | ||||
|                 aligned_data_buffer[aligned_data_buffer_index] = memory_value_bytes[i as usize]; | ||||
| @@ -79,11 +81,13 @@ pub(crate) async fn upload_bluetooth_firmware<PWR: OutputPin, SPI: SpiBusCyw43>( | ||||
|             // pad end alignment not needed | ||||
|         } | ||||
|         let buffer_to_write = &aligned_data_buffer[0..aligned_data_buffer_index as usize]; | ||||
|         debug!("upload_bluetooth_firmware: dest_start_addr = {:x} dest_end_addr = {:x} aligned_data_buffer_index = {} buffer_to_write = {:02x}", dest_start_addr, dest_end_addr, aligned_data_buffer_index, buffer_to_write); | ||||
|         //debug!("upload_bluetooth_firmware: dest_start_addr = {:x} dest_end_addr = {:x} aligned_data_buffer_index = {} buffer_to_write = {:02x}", dest_start_addr, dest_end_addr, aligned_data_buffer_index, buffer_to_write); | ||||
|         assert!(dest_start_addr % 4 == 0); | ||||
|         assert!(dest_end_addr % 4 == 0); | ||||
|         assert!(aligned_data_buffer_index % 4 == 0); | ||||
|         bus.bp_write(dest_start_addr, buffer_to_write).await; | ||||
|         // increment pointer | ||||
|         pointer += num_fw_bytes; | ||||
|     } | ||||
| } | ||||
|  | ||||
| @@ -154,6 +158,7 @@ pub(crate) async fn bt_set_intr<PWR: OutputPin, SPI: SpiBusCyw43>(bus: &mut Bus< | ||||
| } | ||||
|  | ||||
| pub(crate) async fn init_bluetooth<PWR: OutputPin, SPI: SpiBusCyw43>(bus: &mut Bus<PWR, SPI>, firmware_offsets: &[(u32, usize)], firmware: &[u8]) { | ||||
|     assert!(firmware.len() == 5952); | ||||
|     bus.bp_write32(CHIP.bluetooth_base_address + BT2WLAN_PWRUP_ADDR, BT2WLAN_PWRUP_WAKE) | ||||
|         .await; | ||||
|     upload_bluetooth_firmware(bus, firmware_offsets, firmware).await; | ||||
|   | ||||
| @@ -226,6 +226,7 @@ where | ||||
|  | ||||
|     let mut runner = Runner::new(ch_runner, Bus::new(pwr, spi), &state.ioctl_state, &state.events); | ||||
|  | ||||
|     assert!(bluetooth_firmware.len() == 5952); | ||||
|     runner.init(firmware, bluetooth_firmware_offsets, bluetooth_firmware).await; | ||||
|  | ||||
|     ( | ||||
|   | ||||
| @@ -74,6 +74,8 @@ where | ||||
|     } | ||||
|  | ||||
|     pub(crate) async fn init(&mut self, firmware: &[u8], bluetooth_firmware_offsets: &[(u32, usize)], bluetooth_firmware: &[u8]) { | ||||
|         assert!(bluetooth_firmware.len() == 5952); | ||||
|  | ||||
|         self.bus.init().await; | ||||
|  | ||||
|         // Init ALP (Active Low Power) clock | ||||
| @@ -116,9 +118,6 @@ where | ||||
|         debug!("loading fw"); | ||||
|         self.bus.bp_write(ram_addr, firmware).await; | ||||
|  | ||||
|         debug!("loading bluetooth fw"); | ||||
|         bluetooth::init_bluetooth(&mut self.bus, bluetooth_firmware_offsets, bluetooth_firmware).await; | ||||
|  | ||||
|         debug!("loading nvram"); | ||||
|         // Round up to 4 bytes. | ||||
|         let nvram_len = (NVRAM.len() + 3) / 4 * 4; | ||||
| @@ -198,6 +197,10 @@ where | ||||
|         while self.bus.read8(FUNC_BACKPLANE, REG_BACKPLANE_CHIP_CLOCK_CSR).await & 0x80 == 0 {} | ||||
|         debug!("clock ok"); | ||||
|  | ||||
|         debug!("loading bluetooth fw"); | ||||
|         assert!(bluetooth_firmware.len() == 5952); | ||||
|         bluetooth::init_bluetooth(&mut self.bus, bluetooth_firmware_offsets, bluetooth_firmware).await; | ||||
|  | ||||
|         #[cfg(feature = "firmware-logs")] | ||||
|         self.log_init().await; | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user