Skip to content

Add APOB messages to host_sp_comms #2006

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
5 changes: 5 additions & 0 deletions lib/host-sp-messages/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ pub enum HostToSp {
// We use a raw `u8` here for the same reason as in `KeyLookup` above.
key: u8,
},
// APOB is followed by a binary data blob, which should be written to flash
APOB {
Comment on lines +126 to +127
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suuuper nitpicky, feel free to disregard me: I think the recommended Rust naming conventions for casing is that acronyms should be treated as a "word" in camel-case, and only the first letter should be uppercased, so this would be:

Suggested change
// APOB is followed by a binary data blob, which should be written to flash
APOB {
// APOB is followed by a binary data blob, which should be written to flash
Apob {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that other variants of this enum follow this convention (e.g. GetMacIdentity rather than GetMACIdentity; RotRequest rather than ROTRequest, and so on).

offset: u64,
},
}

/// The order of these cases is critical! We are relying on hubpack's encoding
Expand Down Expand Up @@ -185,6 +189,7 @@ pub enum SpToHost {
name: [u8; 32],
},
KeySetResult(#[count(children)] KeySetResult),
APOBResult(u8),
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, num_derive::FromPrimitive)]
Expand Down
84 changes: 84 additions & 0 deletions task/host-sp-comms/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ enum Trace {
#[count(children)]
message: SpToHost,
},
APOBWriteError {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

similarly, i think this would be

Suggested change
APOBWriteError {
ApobWriteError {

offset: u64,
#[count(children)]
err: APOBError,
Comment on lines +141 to +143
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be worth leaving a comment here indicating that the offset field in the ringbuf entry is the initial offset of the write, while the offset fields in the APOBError variant are the offset of the page we were writing when the error actually occurred? Having two fields with the same names but potentially differing values could be confusing.

Or, perhaps we should call the APOBError field "page_offset" or something, to distinguish these?

},
}

counted_ringbuf!(Trace, 20, Trace::None);
Expand All @@ -163,6 +168,31 @@ enum Timers {
TxPeriodicZeroByte,
}

#[derive(Copy, Clone, Debug, Eq, PartialEq, counters::Count)]
enum APOBError {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aaaaand my stupid capitalization thing again:

Suggested change
enum APOBError {
enum ApobError {

OffsetOverflow {
offset: u64,
},
NotErased {
offset: u32,
},
EraseFailed {
offset: u32,
#[count(children)]
err: drv_hf_api::HfError,
},
WriteFailed {
offset: u32,
#[count(children)]
err: drv_hf_api::HfError,
},
ReadFailed {
offset: u32,
#[count(children)]
err: drv_hf_api::HfError,
},
}

#[export_name = "main"]
fn main() -> ! {
let mut server = ServerImpl::claim_static_resources();
Expand Down Expand Up @@ -965,6 +995,15 @@ impl ServerImpl {
}),
}
}
HostToSp::APOB { offset } => {
Some(match Self::apob_write(&self.hf, offset, data) {
Ok(()) => SpToHost::APOBResult(0),
Err(err) => {
ringbuf_entry!(Trace::APOBWriteError { offset, err });
SpToHost::APOBResult(1)
}
})
}
};

if let Some(response) = response {
Expand Down Expand Up @@ -993,6 +1032,51 @@ impl ServerImpl {
Ok(())
}

/// Write data to the bonus region of flash
///
/// This does not take `&self` because we need to force a split borrow
fn apob_write(
hf: &HostFlash,
mut offset: u64,
data: &[u8],
) -> Result<(), APOBError> {
for chunk in data.chunks(drv_hf_api::PAGE_SIZE_BYTES) {
Self::apob_write_page(
hf,
offset
.try_into()
.map_err(|_| APOBError::OffsetOverflow { offset })?,
chunk,
)?;
offset += chunk.len() as u64;
}
Ok(())
}

/// Write a single page of data to the bonus region of flash
///
/// This does not take `&self` because we need to force a split borrow
fn apob_write_page(
hf: &HostFlash,
offset: u32,
data: &[u8],
) -> Result<(), APOBError> {
if offset as usize % drv_hf_api::SECTOR_SIZE_BYTES == 0 {
hf.bonus_sector_erase(offset)
.map_err(|err| APOBError::EraseFailed { offset, err })?;
} else {
// Read back the page and confirm that it's all empty
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this fail if there's a blip in the IPCC path and the host resends an APOB request? (I'm not sure what the expectations are for the offsets the host is providing.)

let mut scratch = [0u8; drv_hf_api::PAGE_SIZE_BYTES];
hf.bonus_read(offset, &mut scratch[..data.len()])
.map_err(|err| APOBError::ReadFailed { offset, err })?;
if !scratch[..data.len()].iter().all(|b| *b == 0xFF) {
return Err(APOBError::NotErased { offset });
}
}
hf.bonus_page_program(offset, data)
.map_err(|err| APOBError::WriteFailed { offset, err })
}

fn handle_sprot(
&mut self,
sequence: u64,
Expand Down
Loading