Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 20 additions & 2 deletions task/control-plane-agent/src/inventory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,21 @@ impl Inventory {
if !device.sensors.is_empty() {
capabilities |= DeviceCapabilities::HAS_MEASUREMENT_CHANNELS;
}

// NOTE: the `from_bstr_unchecked` method expects that:
//
// 1. The given bytes contain utf-8 data
// 2. The given slice is <= SpComponent::MAX_ID_LENGTH
//
// Since we pass the bytes of a `str` (always good utf-8!), and our
// `str`s are built (and length-checked) at compile time, use of this
// method is justified. You don't see an unsafe block here, because
// SpComponent can be received over the wire, so even if we violated
// the rules above, there would be no potential soundness concerns.
let component = SpComponent::from_bstr_unchecked(device.id.as_bytes());

DeviceDescription {
component: SpComponent { id: device.id },
component,
device: device.device,
description: device.description,
capabilities,
Expand Down Expand Up @@ -178,8 +191,13 @@ impl TryFrom<&'_ SpComponent> for Index {
type Error = SpError;

fn try_from(component: &'_ SpComponent) -> Result<Self, Self::Error> {
// TODO(AJM): implement PartialEq/PartialOrd for `SpComponent` et. al,
// then make this nicer. We'll want this for some follow-up PMBus
// changes as well.
if let Ok(entry_idx) = task_validate_api::DEVICE_INDICES_BY_SORTED_ID
.binary_search_by_key(&component.id, |&(id, _)| id)
.binary_search_by_key(&component.as_bstr(), |&(id, _)| {
id.as_bytes()
})
Comment thread
jamesmunns marked this conversation as resolved.
{
let &(_, index) = task_validate_api::DEVICE_INDICES_BY_SORTED_ID
.get(entry_idx)
Expand Down
2 changes: 1 addition & 1 deletion task/control-plane-agent/src/update/rot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl ComponentUpdater for RotUpdate {
.map_err(SpError::OtherComponentUpdateInProgress)?;

// Which target are we updating?
ringbuf_entry!(Trace::Target(update.component.id[0], update.slot));
ringbuf_entry!(Trace::Target(update.component.id()[0], update.slot));
let target = match (update.component, update.slot) {
(SpComponent::ROT, 0) => UpdateTarget::ImageA,
(SpComponent::ROT, 1) => UpdateTarget::ImageB,
Expand Down
12 changes: 6 additions & 6 deletions task/validate-api/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ fn write_pub_device_descriptions() -> anyhow::Result<()> {
writeln!(file, " DeviceDescription {{")?;
writeln!(file, " device: {:?},", dev.device)?;
writeln!(file, " description: {:?},", dev.description)?;
if let Some(id) = dev.device_id {
if let Ok(component) = SpComponent::try_from(id.as_ref()) {
writeln!(file, " id: {:?},", component.id)?;
if id2idx.insert(component.id, idx).is_some() {
if let Some(id) = dev.device_id.as_ref() {
if id.len() <= SpComponent::MAX_ID_LENGTH {
writeln!(file, " id: \"{id}\",")?;
if id2idx.insert(id.to_string(), idx).is_some() {
println!("cargo::error=duplicate device id {id:?}",);
duplicate_ids += 1;
}
Expand Down Expand Up @@ -106,11 +106,11 @@ fn write_pub_device_descriptions() -> anyhow::Result<()> {

writeln!(
file,
"pub static DEVICE_INDICES_BY_SORTED_ID: [([u8; MAX_ID_LENGTH], usize); {}] = [",
"pub static DEVICE_INDICES_BY_SORTED_ID: [(&str, usize); {}] = [",
id2idx.len()
)?;
for (id, idx) in id2idx {
writeln!(file, " ({id:?}, {idx}),")?;
writeln!(file, " (\"{id}\", {idx}),")?;
}
writeln!(file, "];")?;

Expand Down
2 changes: 1 addition & 1 deletion task/validate-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub struct DeviceDescription {
pub device: &'static str,
pub description: &'static str,
pub sensors: &'static [SensorDescription],
pub id: [u8; MAX_ID_LENGTH],
pub id: &'static str,
pub is_pmbus: bool,
}

Expand Down
Loading