Skip to content
Merged
20 changes: 14 additions & 6 deletions Cargo.lock

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

6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ leb128 = { version = "0.2.5", default-features = false }
lpc55-pac = { version = "0.4", default-features = false }
memchr = { version = "2.4", default-features = false }
memoffset = { version = "0.6.5", default-features = false }
minicbor = { version = "0.26.4", default-features = false }
minicbor = { version = "2.1.1", default-features = false }
multimap = { version = "0.8.3", default-features = false }
nb = { version = "1", default-features = false }
num = { version = "0.4", default-features = false }
Expand Down Expand Up @@ -163,6 +163,10 @@ tlvc-text = { git = "https://github.com/oxidecomputer/tlvc", default-features =
transceiver-messages = { git = "https://github.com/oxidecomputer/transceiver-control/", default-features = false }
vsc7448-pac = { git = "https://github.com/oxidecomputer/vsc7448", default-features = false }

[patch.crates-io]
# See https://gitlab.com/robigalia/ssmarshal/-/merge_requests/2
ssmarshal = { git = "https://gitlab.com/de-vri-es/ssmarshal", rev = "10e90cbe389c8f07c52815857551a051946881ab" }
Comment on lines +166 to +168
Copy link
Member Author

Choose a reason for hiding this comment

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

This is necessary due to ssmarshal no longer compiling with Rust 2024, as when the edition is Rust 2024, serde will require core::error::Error implementations for error types, and ssmarshal doesn't have those.

Since the last commit to ssmarshal's main branch was 8 years ago, I'm not convinced this PR gets merged any time soon. It would probably be worth getting rid of our ssmarshal deps at some point, since Hubpack is basically the same thing but more featureful...


[workspace.lints.rust]
elided_lifetimes_in_paths = "warn"

Expand Down
16 changes: 16 additions & 0 deletions lib/minicbor-lease/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "minicbor-lease"
version = "0.1.0"
edition = "2024"

[dependencies]
minicbor = { workspace = true }
idol-runtime = { workspace = true }

[lib]
test = false
doctest = false
bench = false

[lints]
workspace = true
122 changes: 122 additions & 0 deletions lib/minicbor-lease/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

//! An adapter implementing [`minicbor::encode::write::Write`] for
//! [`idol_runtime::Leased`] byte buffers.
#![no_std]

/// An adapter implementing [`minicbor::encode::write::Write`] for
/// [`idol_runtime::Leased`] byte buffers.
pub struct LeasedWriter<'lease, A>
where
A: idol_runtime::AttributeWrite,
{
lease: &'lease mut idol_runtime::Leased<A, [u8]>,
pos: usize,
ran_out_of_space: bool,
}

/// Errors returned by the [`minicbor::encode::write::Write`] implementation for
/// [`LeasedWriter`].
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum Error {
/// The other side of the lease has gone away.
WentAway,
/// Data could not be written as there was no room left in the lease.
EndOfLease,
}

impl core::fmt::Display for Error {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str(match self {
Self::WentAway => "lease went away",
Self::EndOfLease => "end of lease",
})
}
}

impl<A> minicbor::encode::write::Write for LeasedWriter<'_, A>
where
A: idol_runtime::AttributeWrite,
{
type Error = Error;

fn write_all(&mut self, buf: &[u8]) -> Result<(), Self::Error> {
let Some(end) = self.pos.checked_add(buf.len()) else {
self.ran_out_of_space = true;
return Err(Error::EndOfLease);
};
if end >= self.lease.len() {
self.ran_out_of_space = true;
return Err(Error::EndOfLease);
}
self.lease
.write_range(self.pos..end, buf)
.map_err(|_| Error::WentAway)?;

self.pos += buf.len();

Ok(())
}
}

impl<'lease, A> LeasedWriter<'lease, A>
where
A: idol_runtime::AttributeWrite,
{
/// Returns a new `LeasedWriter` starting at byte 0 of the lease.
pub fn new(lease: &'lease mut idol_runtime::Leased<A, [u8]>) -> Self {
Self {
lease,
pos: 0,
ran_out_of_space: false,
}
}

/// Returns a new `LeasedWriter` starting at the specified byte position in
/// the lease.
///
/// This is intended for cases where some data has already been written to
/// the lease.
pub fn starting_at(
position: usize,
lease: &'lease mut idol_runtime::Leased<A, [u8]>,
) -> Self {
Self {
lease,
pos: position,
ran_out_of_space: false,
}
}

/// Returns the current byte position within the lease.
pub fn position(&self) -> usize {
self.pos
}

/// Borrows the underlying lease from the writer.
pub fn lease(&self) -> &idol_runtime::Leased<A, [u8]> {
self.lease
}

/// Returns the underlying lease, consuming the writer.
pub fn into_inner(self) -> &'lease mut idol_runtime::Leased<A, [u8]> {
self.lease
}

/// Returns `true` if the last `w
pub fn ran_out_of_space(&self) -> bool {
self.ran_out_of_space
}
}

impl From<Error> for idol_runtime::ClientError {
fn from(error: Error) -> Self {
match error {
Error::EndOfLease => idol_runtime::ClientError::BadLease,
Error::WentAway => idol_runtime::ClientError::WentAway,
}
}
}
3 changes: 2 additions & 1 deletion task/packrat/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ task-packrat-api = { path = "../packrat-api" }
userlib = { path = "../../sys/userlib", features = ["panic-messages"] }
snitch-core = { version = "0.1.0", path = "../../lib/snitch-core", optional = true, features = ["counters"] }
minicbor = { workspace = true, optional = true }
minicbor-lease = { path = "../../lib/minicbor-lease", optional = true }

[build-dependencies]
anyhow.workspace = true
Expand All @@ -37,7 +38,7 @@ gimlet = ["drv-cpu-seq-api"]
grapefruit = []
boot-kmdb = []
no-ipc-counters = ["idol/no-counters"]
ereport = ["dep:drv-rng-api", "dep:snitch-core", "dep:minicbor", "dep:quote"]
ereport = ["dep:drv-rng-api", "dep:snitch-core", "dep:minicbor", "dep:minicbor-lease", "dep:quote"]

# This section is here to discourage RLS/rust-analyzer from doing test builds,
# since test builds don't work for cross compilation.
Expand Down
Loading
Loading