Skip to content

Commit 5b2078a

Browse files
committed
Push u64 -> u32 conversion upstream
1 parent 899f105 commit 5b2078a

File tree

7 files changed

+83
-62
lines changed

7 files changed

+83
-62
lines changed

app/cosmo/base.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ features = ["stm32h753", "usart6", "baud_rate_3M", "hardware_flow_control", "vla
256256
uses = ["usart6", "dbgmcu"]
257257
interrupts = {"usart6.irq" = "usart-irq"}
258258
priority = 9
259-
max-sizes = {flash = 68672, ram = 65536}
259+
max-sizes = {flash = 69120, ram = 65536}
260260
stacksize = 5400
261261
start = true
262262
task-slots = ["sys", { cpu_seq = "cosmo_seq" }, "hf", "control_plane_agent", "net", "packrat", "i2c_driver", { spi_driver = "spi2_driver" }, "sprot", "auxflash"]

drv/cosmo-hf/src/apob.rs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -463,16 +463,15 @@ impl ApobState {
463463
pub(crate) fn begin(
464464
&mut self,
465465
drv: &mut FlashDriver,
466-
length: u64,
466+
length: u32,
467467
algorithm: ApobHash,
468468
) -> Result<(), ApobBeginError> {
469469
drv.check_flash_mux_state()
470470
.map_err(|_| ApobBeginError::InvalidState)?;
471-
if length > u64::from(APOB_SLOT_SIZE) {
471+
if length > APOB_SLOT_SIZE {
472472
// XXX should this lock the state machine?
473473
return Err(ApobBeginError::BadDataLength);
474474
}
475-
let length: u32 = length.try_into().unwrap_lite();
476475
match *self {
477476
ApobState::Waiting { write_slot, .. } => {
478477
*self = ApobState::Ready {
@@ -509,15 +508,15 @@ impl ApobState {
509508
pub(crate) fn write(
510509
&mut self,
511510
drv: &mut FlashDriver,
512-
offset: u64,
511+
offset: u32,
513512
data: Leased<R, [u8]>,
514513
) -> Result<(), ApobWriteError> {
515514
// Check that the flash is muxed to the SP
516515
drv.check_flash_mux_state()
517516
.map_err(|_| ApobWriteError::InvalidState)?;
518517

519518
// Check that the offset is within the slot
520-
if offset > u64::from(APOB_SLOT_SIZE) {
519+
if offset > APOB_SLOT_SIZE {
521520
return Err(ApobWriteError::InvalidOffset);
522521
}
523522

@@ -537,14 +536,11 @@ impl ApobState {
537536

538537
// Check that the end of the data range is within our expected length
539538
if offset
540-
.checked_add(data.len() as u64)
541-
.is_none_or(|d| d > u64::from(expected_length))
539+
.checked_add(data.len() as u32)
540+
.is_none_or(|d| d > expected_length)
542541
{
543542
return Err(ApobWriteError::InvalidSize);
544543
}
545-
let Ok(offset) = u32::try_from(offset) else {
546-
return Err(ApobWriteError::InvalidSize);
547-
};
548544
let mut out_buf = [0u8; PAGE_SIZE_BYTES];
549545
let mut scratch_buf = [0u8; PAGE_SIZE_BYTES];
550546
for i in (0..data.len()).step_by(PAGE_SIZE_BYTES) {
@@ -581,15 +577,15 @@ impl ApobState {
581577
pub(crate) fn read(
582578
&mut self,
583579
drv: &mut FlashDriver,
584-
offset: u64,
580+
offset: u32,
585581
data: Leased<W, [u8]>,
586582
) -> Result<usize, ApobReadError> {
587583
// Check that the flash is muxed to the SP
588584
drv.check_flash_mux_state()
589585
.map_err(|_| ApobReadError::InvalidState)?;
590586

591587
// Check that the offset is within the slot
592-
if offset > u64::from(APOB_SLOT_SIZE) {
588+
if offset > APOB_SLOT_SIZE {
593589
return Err(ApobReadError::InvalidOffset);
594590
}
595591

@@ -603,8 +599,8 @@ impl ApobState {
603599

604600
// Check that the end of the data range is within a slot size
605601
if offset
606-
.checked_add(data.len() as u64)
607-
.is_none_or(|d| d > u64::from(APOB_SLOT_SIZE))
602+
.checked_add(data.len() as u32)
603+
.is_none_or(|d| d > APOB_SLOT_SIZE)
608604
{
609605
return Err(ApobReadError::InvalidSize);
610606
}
@@ -614,7 +610,7 @@ impl ApobState {
614610
// Read data from the lease into local storage
615611
let n = (data.len() - i).min(PAGE_SIZE_BYTES);
616612
let addr = read_slot
617-
.flash_addr(u32::try_from(i as u64 + offset).unwrap_lite())
613+
.flash_addr(u32::try_from(i as u32 + offset).unwrap_lite())
618614
.unwrap_lite();
619615

620616
// Read back the current data, then write it to the lease

drv/cosmo-hf/src/hf.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ impl idl::InOrderHostFlashImpl for ServerImpl {
509509
fn apob_begin(
510510
&mut self,
511511
_: &RecvMessage,
512-
length: u64,
512+
length: u32,
513513
algorithm: drv_hf_api::ApobHash,
514514
) -> Result<(), RequestError<drv_hf_api::ApobBeginError>> {
515515
self.apob_state
@@ -520,7 +520,7 @@ impl idl::InOrderHostFlashImpl for ServerImpl {
520520
fn apob_write(
521521
&mut self,
522522
_: &RecvMessage,
523-
offset: u64,
523+
offset: u32,
524524
data: Leased<R, [u8]>,
525525
) -> Result<(), RequestError<drv_hf_api::ApobWriteError>> {
526526
self.apob_state
@@ -540,7 +540,7 @@ impl idl::InOrderHostFlashImpl for ServerImpl {
540540
fn apob_read(
541541
&mut self,
542542
_: &RecvMessage,
543-
offset: u64,
543+
offset: u32,
544544
data: Leased<W, [u8]>,
545545
) -> Result<usize, RequestError<drv_hf_api::ApobReadError>> {
546546
self.apob_state
@@ -967,7 +967,7 @@ impl idl::InOrderHostFlashImpl for FailServer {
967967
fn apob_begin(
968968
&mut self,
969969
_: &RecvMessage,
970-
_length: u64,
970+
_length: u32,
971971
_alg: drv_hf_api::ApobHash,
972972
) -> Result<(), RequestError<drv_hf_api::ApobBeginError>> {
973973
Err(drv_hf_api::ApobBeginError::InvalidState.into())
@@ -976,7 +976,7 @@ impl idl::InOrderHostFlashImpl for FailServer {
976976
fn apob_write(
977977
&mut self,
978978
_: &RecvMessage,
979-
_offset: u64,
979+
_offset: u32,
980980
_data: Leased<R, [u8]>,
981981
) -> Result<(), RequestError<drv_hf_api::ApobWriteError>> {
982982
Err(drv_hf_api::ApobWriteError::InvalidState.into())
@@ -992,7 +992,7 @@ impl idl::InOrderHostFlashImpl for FailServer {
992992
fn apob_read(
993993
&mut self,
994994
_: &RecvMessage,
995-
_offset: u64,
995+
_offset: u32,
996996
_data: Leased<W, [u8]>,
997997
) -> Result<usize, RequestError<drv_hf_api::ApobReadError>> {
998998
Err(drv_hf_api::ApobReadError::InvalidState.into())

drv/gimlet-hf-server/src/main.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -918,7 +918,7 @@ impl idl::InOrderHostFlashImpl for ServerImpl {
918918
fn apob_begin(
919919
&mut self,
920920
_: &RecvMessage,
921-
_length: u64,
921+
_length: u32,
922922
_alg: drv_hf_api::ApobHash,
923923
) -> Result<(), RequestError<drv_hf_api::ApobBeginError>> {
924924
Err(drv_hf_api::ApobBeginError::NotImplemented.into())
@@ -927,7 +927,7 @@ impl idl::InOrderHostFlashImpl for ServerImpl {
927927
fn apob_write(
928928
&mut self,
929929
_: &RecvMessage,
930-
_offset: u64,
930+
_offset: u32,
931931
_data: Leased<R, [u8]>,
932932
) -> Result<(), RequestError<drv_hf_api::ApobWriteError>> {
933933
Err(drv_hf_api::ApobWriteError::NotImplemented.into())
@@ -943,7 +943,7 @@ impl idl::InOrderHostFlashImpl for ServerImpl {
943943
fn apob_read(
944944
&mut self,
945945
_: &RecvMessage,
946-
_offset: u64,
946+
_offset: u32,
947947
_data: Leased<W, [u8]>,
948948
) -> Result<usize, RequestError<drv_hf_api::ApobReadError>> {
949949
Err(drv_hf_api::ApobReadError::NotImplemented.into())
@@ -1144,7 +1144,7 @@ impl idl::InOrderHostFlashImpl for FailServer {
11441144
fn apob_begin(
11451145
&mut self,
11461146
_: &RecvMessage,
1147-
_length: u64,
1147+
_length: u32,
11481148
_alg: drv_hf_api::ApobHash,
11491149
) -> Result<(), RequestError<drv_hf_api::ApobBeginError>> {
11501150
Err(drv_hf_api::ApobBeginError::NotImplemented.into())
@@ -1153,7 +1153,7 @@ impl idl::InOrderHostFlashImpl for FailServer {
11531153
fn apob_write(
11541154
&mut self,
11551155
_: &RecvMessage,
1156-
_offset: u64,
1156+
_offset: u32,
11571157
_data: Leased<R, [u8]>,
11581158
) -> Result<(), RequestError<drv_hf_api::ApobWriteError>> {
11591159
Err(drv_hf_api::ApobWriteError::NotImplemented.into())
@@ -1169,7 +1169,7 @@ impl idl::InOrderHostFlashImpl for FailServer {
11691169
fn apob_read(
11701170
&mut self,
11711171
_: &RecvMessage,
1172-
_offset: u64,
1172+
_offset: u32,
11731173
_data: Leased<W, [u8]>,
11741174
) -> Result<usize, RequestError<drv_hf_api::ApobReadError>> {
11751175
Err(drv_hf_api::ApobReadError::NotImplemented.into())

drv/mock-gimlet-hf-server/src/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ impl idl::InOrderHostFlashImpl for ServerImpl {
242242
fn apob_begin(
243243
&mut self,
244244
_: &RecvMessage,
245-
_length: u64,
245+
_length: u32,
246246
_alg: drv_hf_api::ApobHash,
247247
) -> Result<(), RequestError<drv_hf_api::ApobBeginError>> {
248248
Err(drv_hf_api::ApobBeginError::NotImplemented.into())
@@ -251,7 +251,7 @@ impl idl::InOrderHostFlashImpl for ServerImpl {
251251
fn apob_write(
252252
&mut self,
253253
_: &RecvMessage,
254-
_offset: u64,
254+
_offset: u32,
255255
_data: Leased<R, [u8]>,
256256
) -> Result<(), RequestError<drv_hf_api::ApobWriteError>> {
257257
Err(drv_hf_api::ApobWriteError::NotImplemented.into())
@@ -267,7 +267,7 @@ impl idl::InOrderHostFlashImpl for ServerImpl {
267267
fn apob_read(
268268
&mut self,
269269
_: &RecvMessage,
270-
_offset: u64,
270+
_offset: u32,
271271
_data: Leased<W, [u8]>,
272272
) -> Result<usize, RequestError<drv_hf_api::ApobReadError>> {
273273
Err(drv_hf_api::ApobReadError::InvalidState.into())

idl/hf.idol

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ Interface(
254254
"apob_begin": (
255255
description: "begin writing APOB data to bonus flash",
256256
args: {
257-
"length": "u64",
257+
"length": "u32",
258258
"algorithm": "ApobHash",
259259
},
260260
reply: Result(
@@ -267,7 +267,7 @@ Interface(
267267
"apob_write": (
268268
description: "writes to the current APOB slot",
269269
args: {
270-
"offset": "u64",
270+
"offset": "u32",
271271
},
272272
leases: {
273273
"data": (type: "[u8]", read: true),
@@ -289,7 +289,7 @@ Interface(
289289
"apob_read": (
290290
description: "reads from the current APOB slot",
291291
args: {
292-
"offset": "u64",
292+
"offset": "u32",
293293
},
294294
leases: {
295295
"data": (type: "[u8]", write: true),

task/host-sp-comms/src/main.rs

Lines changed: 53 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,12 @@ enum Trace {
146146
message: SpToHost,
147147
},
148148
ApobWriteError {
149-
offset: u64,
149+
offset: u32,
150150
#[count(children)]
151151
err: drv_hf_api::ApobWriteError,
152152
},
153153
ApobReadError {
154-
offset: u64,
154+
offset: u32,
155155
#[count(children)]
156156
err: drv_hf_api::ApobReadError,
157157
},
@@ -1035,32 +1035,9 @@ impl ServerImpl {
10351035
}
10361036
}
10371037
HostToSp::ApobBegin { length, algorithm } => {
1038-
// Decode into internal types, then call into `hf`
1039-
// XXX should bad hash algorithms or lengths lock the APOB?
1040-
use drv_hf_api::{ApobBeginError, ApobHash};
1041-
use host_sp_messages::ApobBeginResult;
1042-
Some(SpToHost::ApobBegin(match algorithm {
1043-
0 => {
1044-
if let Ok(d) = data.try_into() {
1045-
let hash = ApobHash::Sha256(d);
1046-
match self.hf.apob_begin(length, hash) {
1047-
Ok(()) => ApobBeginResult::Ok,
1048-
Err(ApobBeginError::NotImplemented) => {
1049-
ApobBeginResult::NotImplemented
1050-
}
1051-
Err(ApobBeginError::InvalidState) => {
1052-
ApobBeginResult::InvalidState
1053-
}
1054-
Err(ApobBeginError::BadDataLength) => {
1055-
ApobBeginResult::BadDataLength
1056-
}
1057-
}
1058-
} else {
1059-
ApobBeginResult::BadHashLength
1060-
}
1061-
}
1062-
_ => ApobBeginResult::InvalidAlgorithm,
1063-
}))
1038+
Some(SpToHost::ApobBegin(Self::apob_begin(
1039+
&self.hf, length, algorithm, data,
1040+
)))
10641041
}
10651042
HostToSp::ApobCommit => {
10661043
// Call into `hf` to do the work here
@@ -1118,6 +1095,43 @@ impl ServerImpl {
11181095
Ok(())
11191096
}
11201097

1098+
fn apob_begin(
1099+
hf: &HostFlash,
1100+
length: u64,
1101+
algorithm: u8,
1102+
data: &[u8],
1103+
) -> host_sp_messages::ApobBeginResult {
1104+
// Decode into internal types, then call into `hf`
1105+
// XXX should bad hash algorithms or lengths lock the APOB?
1106+
use drv_hf_api::{ApobBeginError, ApobHash};
1107+
use host_sp_messages::ApobBeginResult;
1108+
let Ok(length) = u32::try_from(length) else {
1109+
return host_sp_messages::ApobBeginResult::BadDataLength;
1110+
};
1111+
match algorithm {
1112+
0 => {
1113+
if let Ok(d) = data.try_into() {
1114+
let hash = ApobHash::Sha256(d);
1115+
match hf.apob_begin(length, hash) {
1116+
Ok(()) => ApobBeginResult::Ok,
1117+
Err(ApobBeginError::NotImplemented) => {
1118+
ApobBeginResult::NotImplemented
1119+
}
1120+
Err(ApobBeginError::InvalidState) => {
1121+
ApobBeginResult::InvalidState
1122+
}
1123+
Err(ApobBeginError::BadDataLength) => {
1124+
ApobBeginResult::BadDataLength
1125+
}
1126+
}
1127+
} else {
1128+
ApobBeginResult::BadHashLength
1129+
}
1130+
}
1131+
_ => ApobBeginResult::InvalidAlgorithm,
1132+
}
1133+
}
1134+
11211135
/// Write data to the bonus region of flash
11221136
///
11231137
/// This does not take `&self` because we need to force a split borrow
@@ -1128,6 +1142,9 @@ impl ServerImpl {
11281142
) -> host_sp_messages::ApobDataResult {
11291143
use drv_hf_api::ApobWriteError;
11301144
use host_sp_messages::ApobDataResult;
1145+
let Ok(offset) = u32::try_from(offset) else {
1146+
return ApobDataResult::InvalidOffset;
1147+
};
11311148
match hf.apob_write(offset, data) {
11321149
Ok(()) => ApobDataResult::Ok,
11331150
Err(err) => {
@@ -1162,6 +1179,14 @@ impl ServerImpl {
11621179
);
11631180
return;
11641181
};
1182+
let Ok(offset) = u32::try_from(offset) else {
1183+
self.tx_buf.encode_response(
1184+
sequence,
1185+
&SpToHost::ApobRead(ApobReadResult::InvalidOffset),
1186+
|_buf| 0,
1187+
);
1188+
return;
1189+
};
11651190
self.tx_buf.try_encode_response(
11661191
sequence,
11671192
&SpToHost::ApobRead(ApobReadResult::Ok),

0 commit comments

Comments
 (0)