Skip to content

Commit d60b7f9

Browse files
mkeeterAaron-Hartwig
authored andcommitted
More descriptive error type and ringbuf logging
1 parent 7d0f0d3 commit d60b7f9

File tree

1 file changed

+43
-8
lines changed

1 file changed

+43
-8
lines changed

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

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,11 @@ enum Trace {
134134
#[count(children)]
135135
message: SpToHost,
136136
},
137+
APOBWriteError {
138+
offset: u64,
139+
#[count(children)]
140+
err: APOBError,
141+
},
137142
}
138143

139144
counted_ringbuf!(Trace, 20, Trace::None);
@@ -160,6 +165,31 @@ enum Timers {
160165
TxPeriodicZeroByte,
161166
}
162167

168+
#[derive(Copy, Clone, Debug, Eq, PartialEq, counters::Count)]
169+
enum APOBError {
170+
OffsetOverflow {
171+
offset: u64,
172+
},
173+
NotErased {
174+
offset: u32,
175+
},
176+
EraseFailed {
177+
offset: u32,
178+
#[count(children)]
179+
err: drv_hf_api::HfError,
180+
},
181+
WriteFailed {
182+
offset: u32,
183+
#[count(children)]
184+
err: drv_hf_api::HfError,
185+
},
186+
ReadFailed {
187+
offset: u32,
188+
#[count(children)]
189+
err: drv_hf_api::HfError,
190+
},
191+
}
192+
163193
#[export_name = "main"]
164194
fn main() -> ! {
165195
let mut server = ServerImpl::claim_static_resources();
@@ -970,7 +1000,10 @@ impl ServerImpl {
9701000
HostToSp::APOB { offset } => {
9711001
Some(match Self::apob_write(&self.hf, offset, data) {
9721002
Ok(()) => SpToHost::APOBResult(0),
973-
Err(_) => SpToHost::APOBResult(1),
1003+
Err(err) => {
1004+
ringbuf_entry!(Trace::APOBWriteError { offset, err });
1005+
SpToHost::APOBResult(1)
1006+
}
9741007
})
9751008
}
9761009
};
@@ -1008,13 +1041,13 @@ impl ServerImpl {
10081041
hf: &HostFlash,
10091042
mut offset: u64,
10101043
data: &[u8],
1011-
) -> Result<(), drv_hf_api::HfError> {
1044+
) -> Result<(), APOBError> {
10121045
for chunk in data.chunks(drv_hf_api::PAGE_SIZE_BYTES) {
10131046
Self::apob_write_page(
10141047
hf,
10151048
offset
10161049
.try_into()
1017-
.map_err(|_| drv_hf_api::HfError::BadAddress)?,
1050+
.map_err(|_| APOBError::OffsetOverflow { offset })?,
10181051
chunk,
10191052
)?;
10201053
offset += chunk.len() as u64;
@@ -1029,19 +1062,21 @@ impl ServerImpl {
10291062
hf: &HostFlash,
10301063
offset: u32,
10311064
data: &[u8],
1032-
) -> Result<(), drv_hf_api::HfError> {
1065+
) -> Result<(), APOBError> {
10331066
if offset as usize % drv_hf_api::SECTOR_SIZE_BYTES == 0 {
1034-
hf.bonus_sector_erase(offset)?;
1067+
hf.bonus_sector_erase(offset)
1068+
.map_err(|err| APOBError::EraseFailed { offset, err })?;
10351069
} else {
10361070
// Read back the page and confirm that it's all empty
10371071
let mut scratch = [0u8; drv_hf_api::PAGE_SIZE_BYTES];
1038-
hf.bonus_read(offset, &mut scratch[..data.len()])?;
1072+
hf.bonus_read(offset, &mut scratch[..data.len()])
1073+
.map_err(|err| APOBError::ReadFailed { offset, err })?;
10391074
if !scratch[..data.len()].iter().all(|b| *b == 0xFF) {
1040-
// TODO use a different error here?
1041-
return Err(drv_hf_api::HfError::BadAddress);
1075+
return Err(APOBError::NotErased { offset });
10421076
}
10431077
}
10441078
hf.bonus_page_program(offset, data)
1079+
.map_err(|err| APOBError::WriteFailed { offset, err })
10451080
}
10461081

10471082
fn handle_sprot(

0 commit comments

Comments
 (0)