From a7bcad4ca478d95b38c2fe7b84f323712a6c0287 Mon Sep 17 00:00:00 2001 From: Sven Rademakers Date: Fri, 8 Sep 2023 07:45:31 +0100 Subject: [PATCH] rockusb_fwudate: Update bootmode detection After discussion with the maintainers, decided is to keep the Bootmode detection logic local to this project. There is no generic solution possible that works for all rockusb devices. Detection is based on behavior of the loader that is installed on the rockchip device, no spec exists that guarantees a certain behavior of this loader. see https://github.com/collabora/rockchiprs/pull/22 Updated the rockusb dependency to the main branch, as one of the latest patches are required. --- Cargo.lock | 3 ++- tpi_rs/Cargo.toml | 5 ++++- .../firmware_update/rockusb_fwudate.rs | 22 ++++++++++++++++--- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cb96330..6c191d1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1707,7 +1707,8 @@ dependencies = [ [[package]] name = "rockusb" version = "0.1.1" -source = "git+https://github.com/svenrademakers/rockchiprs.git?branch=main#2995928f4e80f9386671c383bda759727c7a7be4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aece3109e8da7c4e0b9e26a9390d26231e13bcbc245b8915dae4fbb1eea64083" dependencies = [ "bytes", "crc", diff --git a/tpi_rs/Cargo.toml b/tpi_rs/Cargo.toml index 643d815..20cae09 100644 --- a/tpi_rs/Cargo.toml +++ b/tpi_rs/Cargo.toml @@ -15,7 +15,7 @@ evdev = { version = "0.12.1", features = ["tokio"] } gpiod = { version = "0.2.3", default-features = false } once_cell = "1.18.0" rockfile = { version = "0.1.0"} -rockusb = { git = "https://github.com/svenrademakers/rockchiprs.git", branch="main"} +rockusb = { version = "0.1.0" } rusb = "0.9.2" rustpiboot = { git = "https://github.com/ruslashev/rustpiboot.git", rev="89e6497"} serde = { version = "1.0.183", features = ["derive"] } @@ -30,3 +30,6 @@ simple_logger.workspace = true tokio.workspace = true tokio-util.workspace = true futures.workspace = true + +[patch.crates-io] +rockusb = { git= "https://github.com/collabora/rockchiprs", rev = "dc90ab5" } diff --git a/tpi_rs/src/middleware/firmware_update/rockusb_fwudate.rs b/tpi_rs/src/middleware/firmware_update/rockusb_fwudate.rs index 2f39f29..9bd964a 100644 --- a/tpi_rs/src/middleware/firmware_update/rockusb_fwudate.rs +++ b/tpi_rs/src/middleware/firmware_update/rockusb_fwudate.rs @@ -1,4 +1,5 @@ use super::transport::{StdFwUpdateTransport, StdTransportWrapper}; +use rusb::DeviceDescriptor; use super::{FlashProgress, FlashingError, FlashingErrorExt}; use crate::middleware::firmware_update::FlashStatus; use crate::middleware::usbboot; @@ -7,7 +8,6 @@ use log::info; use rockfile::boot::{ RkBootEntry, RkBootEntryBytes, RkBootHeader, RkBootHeaderBytes, RkBootHeaderEntry, }; -use rockusb::libusb::BootMode; use rockusb::libusb::{Transport, TransportIO}; use rusb::GlobalContext; use std::{mem::size_of, ops::Range, time::Duration}; @@ -23,8 +23,8 @@ pub async fn new_rockusb_transport( let mut transport = Transport::from_usb_device(device.open().map_err_into_logged_usb(logging)?) .map_err(|_| FlashingError::UsbError)?; - if let Ok(BootMode::MaskedRom) = transport.boot_mode() { - info!("MaskedRom mode detected. loading usb-plug.."); + if BootMode::Maskrom == device.device_descriptor().map_err_into_logged_usb(logging)?.into() { + info!("Maskrom mode detected. loading usb-plug.."); transport = download_boot(&mut transport, logging).await?; logging .try_send(FlashProgress { @@ -132,3 +132,19 @@ async fn load_boot_entries( log::debug!("written {} bytes", size); Ok(()) } + +#[derive(Debug, PartialEq, Eq, Copy, Clone)] + pub enum BootMode { + Maskrom = 0, + Loader = 1, + } + +impl From for BootMode { + fn from(dd: DeviceDescriptor) -> BootMode { + match dd.usb_version().sub_minor() & 0x1 { + 0 => BootMode::Maskrom, + 1 => BootMode::Loader, + _ => unreachable!(), + } + } + }