Skip to content

Commit

Permalink
Merge pull request #12 from FrameworkComputer/windows-driver-oct2023
Browse files Browse the repository at this point in the history
Fix windows driver
  • Loading branch information
JohnAZoidberg authored Oct 28, 2023
2 parents 9c6a2c8 + 2893618 commit 4115a75
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 17 deletions.
14 changes: 5 additions & 9 deletions framework_lib/src/ccgx/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use core::prelude::rust_2021::derive;
use crate::ccgx::{AppVersion, BaseVersion, ControllerVersion};
use crate::chromium_ec::command::EcCommands;
use crate::chromium_ec::{CrosEc, CrosEcDriver, EcError, EcResponseStatus, EcResult};
use crate::util::{self, Config, Platform};
use crate::util::{self, assert_win_len, Config, Platform};
use std::mem::size_of;

use super::*;
Expand Down Expand Up @@ -245,8 +245,7 @@ impl PdController {

pub fn get_silicon_id(&self) -> EcResult<u16> {
let data = self.ccgx_read(ControlRegisters::SiliconId, 2)?;
assert!(data.len() >= 2);
debug_assert_eq!(data.len(), 2);
assert_win_len(data.len(), 2);
Ok(u16::from_le_bytes([data[0], data[1]]))
}

Expand Down Expand Up @@ -306,8 +305,7 @@ impl PdController {
}
};

assert!(data.len() >= 8);
debug_assert_eq!(data.len(), 8);
assert_win_len(data.len(), 8);
let base_ver = BaseVersion::from(&data[..4]);
let app_ver = AppVersion::from(&data[4..]);
println!(
Expand All @@ -317,8 +315,7 @@ impl PdController {

let data = self.ccgx_read(ControlRegisters::Firmware1Version, 8);
let data = data.unwrap();
assert!(data.len() >= 8);
debug_assert_eq!(data.len(), 8);
assert_win_len(data.len(), 8);
let base_ver = BaseVersion::from(&data[..4]);
let app_ver = AppVersion::from(&data[4..]);
println!(
Expand All @@ -328,8 +325,7 @@ impl PdController {

let data = self.ccgx_read(ControlRegisters::Firmware2Version, 8);
let data = data.unwrap();
assert!(data.len() >= 8);
debug_assert_eq!(data.len(), 8);
assert_win_len(data.len(), 8);
let base_ver = BaseVersion::from(&data[..4]);
let app_ver = AppVersion::from(&data[4..]);
println!(
Expand Down
9 changes: 7 additions & 2 deletions framework_lib/src/chromium_ec/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,14 @@ pub trait EcRequestRaw<R> {
Self: Sized,
{
let response = self.send_command_vec_extra(ec, extra_data)?;
if response.len() != std::mem::size_of::<R>() {
// TODO: The Windows driver seems to return 20 more bytes than expected
#[cfg(feature = "win_driver")]
let expected = response.len() != std::mem::size_of::<R>() + 20;
#[cfg(not(feature = "win_driver"))]
let expected = response.len() != std::mem::size_of::<R>();
if expected {
return Err(EcError::DeviceError(format!(
"Returned data is not the expected ({}) size: {}",
"Returned data size {} is not the expted size: {}",
response.len(),
std::mem::size_of::<R>()
)));
Expand Down
7 changes: 5 additions & 2 deletions framework_lib/src/chromium_ec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::os_specific;
use crate::smbios;
#[cfg(feature = "uefi")]
use crate::uefi::shell_get_execution_break_flag;
use crate::util::assert_win_len;

use num_derive::FromPrimitive;

Expand Down Expand Up @@ -235,7 +236,8 @@ impl CrosEc {
// does not return any data
let limits = &[ChargeLimitControlModes::Set as u8, max, min];
let data = self.send_command(EcCommands::ChargeLimitControl as u16, 0, limits)?;
assert_eq!(data.len(), 0);

assert_win_len(data.len(), 0);

Ok(())
}
Expand All @@ -262,7 +264,8 @@ impl CrosEc {
// does not return any data
let limits = &[level as u8, 0x00];
let data = self.send_command(EcCommands::FpLedLevelControl as u16, 0, limits)?;
assert_eq!(data.len(), 0);

assert_win_len(data.len(), 0);

Ok(())
}
Expand Down
5 changes: 4 additions & 1 deletion framework_lib/src/chromium_ec/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,10 @@ pub fn send_command(command: u16, command_version: u8, data: &[u8]) -> EcResult<
Some(status) => return Err(EcError::Response(status)),
}

let out_buffer = &cmd.buffer[..(returned as usize)];
// TODO: Figure out why that's sometimes bigger
let end = std::cmp::min(returned, 256);

let out_buffer = &cmd.buffer[..(end as usize)];
Ok(out_buffer.to_vec())
}

Expand Down
25 changes: 22 additions & 3 deletions framework_lib/src/util.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Miscellaneous utility functions to use across modules
use num::{Num, NumCast};
use std::prelude::v1::*;

#[cfg(feature = "uefi")]
Expand Down Expand Up @@ -131,16 +132,25 @@ fn print_ascii_buffer(buffer: &[u8], newline: bool) {
///
/// Example
///
/// print_multiline_buffer(&[0xa0, 0x00, 0x00, 0x36, 0x62, 0x6e, 0x03, 0x00, 0xc5, 0x11, 0x80, 0x35, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], 0x2000)
/// print_multiline_buffer(&[0xa0, 0x00, 0x00, 0x36, 0x62, 0x6e, 0x03, 0x00, 0xc5, 0x11, 0x80, 0x35, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], 0x2000)
/// Output:
/// 00000000: a000 0036 626e 0300 c511 8035 0000 0000 ...6bn.....5....
/// 00000010: 0000 0000 0000 0000 0000 0000 0000 0000 ................
/// 00002000: a000 0036 626e 0300 c511 8035 0000 0000 ...6bn.....5....
/// 00002010: 0000 0000 0000 0000 0000 0000 0000 00 ................
pub fn print_multiline_buffer(buffer: &[u8], offset: usize) {
let chunk_size = 16;
for (i, chunk) in buffer.chunks(chunk_size).enumerate() {
print!("{:08x}:", offset + i * chunk_size);
print_chunk(chunk, false);

// Make sure ASCII section aligns, even if less than 16 byte chunks
if chunk.len() < 16 {
let byte_padding = 16 - chunk.len();
let space_padding = byte_padding / 2;
let padding = byte_padding * 2 + space_padding;
print!("{}", " ".repeat(padding));
}
print!(" ");

print_ascii_buffer(chunk, true);
}
}
Expand All @@ -151,3 +161,12 @@ pub fn find_sequence(haystack: &[u8], needle: &[u8]) -> Option<usize> {
.windows(needle.len())
.position(|window| window == needle)
}

/// Assert length of an EC response from the windows driver
/// It's always 20 more than expected. TODO: Figure out why
pub fn assert_win_len<N: Num + std::fmt::Debug + Ord + NumCast + Copy>(left: N, right: N) {
#[cfg(feature = "win_driver")]
assert_eq!(left, right + NumCast::from(20).unwrap());
#[cfg(not(feature = "win_driver"))]
assert_eq!(left, right);
}

0 comments on commit 4115a75

Please sign in to comment.