Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Add APOB messages to host_sp_comms #2006

Draft
wants to merge 3 commits 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
22 changes: 3 additions & 19 deletions drv/cosmo-hf/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,9 @@ enum Trace {

counted_ringbuf!(Trace, 32, Trace::None);

/// Size in bytes of a single page of data (i.e., the max length of slice we
/// accept for `page_program()` and `read_memory()`).
///
/// This value is really a property of the flash we're talking to and not this
/// driver, but it's correct for all our current parts. If that changes, this
/// will need to change to something more flexible.
pub const PAGE_SIZE_BYTES: usize = 256;

/// Size in bytes of a single sector of data (i.e., the size of the data erased
/// by a call to `sector_erase()`).
///
/// This value is really a property of the flash we're talking to and not this
/// driver, but it's correct for all our current parts. If that changes, this
/// will need to change to something more flexible.
///
/// **Note:** the datasheet refers to a "sector" as a 4K block, but also
/// supports 64K block erases, so we call the latter a sector to match the
/// behavior of the Gimlet host flash driver.
pub const SECTOR_SIZE_BYTES: u32 = 65_536;
// Re-export constants from the generic host flash API
pub use drv_hf_api::PAGE_SIZE_BYTES;
pub const SECTOR_SIZE_BYTES: u32 = drv_hf_api::SECTOR_SIZE_BYTES as u32;

/// Total flash size is 128 MiB
pub const FLASH_SIZE_BYTES: u32 = 128 * 1024 * 1024;
Expand Down
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 @@ -134,6 +134,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 +138 to +140
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 @@ -160,6 +165,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 @@ -967,6 +997,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 @@ -995,6 +1034,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