Skip to content

Commit 3e8e413

Browse files
authored
Merge pull request #28 from xobs/add-verify-support
flash-algorithm: add Verify support
2 parents 65a59f9 + d2c729e commit 3e8e413

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ version = "0.1.0"
99
cortex-m = "0.7.0"
1010
flash-algorithm = { version = "0.5.0", default-features = false, features = [
1111
"panic-handler",
12+
"verify",
1213
] }
1314

1415
# this lets you use `cargo fix`!

build.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,5 @@ cat <<EOF
3030
pc_uninit: $(sym UnInit)
3131
pc_program_page: $(sym ProgramPage)
3232
pc_erase_sector: $(sym EraseSector)
33+
pc_verify: $(sym Verify)
3334
EOF

src/main.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,32 @@ impl FlashAlgorithm for RP2Algo {
183183
);
184184
Ok(())
185185
}
186+
187+
fn verify(&mut self, address: u32, size: u32, data: Option<&[u8]>) -> Result<(), ErrorCode> {
188+
let Some(data) = data else {
189+
return Ok(());
190+
};
191+
(self.funcs.flash_flush_cache)();
192+
(self.funcs.flash_enter_cmd_xip)();
193+
let check = unsafe { core::slice::from_raw_parts(address as *const u8, size as usize) };
194+
for (offset, (check, data)) in check.iter().zip(data.iter()).enumerate() {
195+
if *check != *data {
196+
(self.funcs.flash_exit_xip)();
197+
// Return the first address that failed.
198+
return ErrorCode::new(offset as u32 + address)
199+
.map(|e| Err(e))
200+
.unwrap_or(Ok(()));
201+
}
202+
}
203+
204+
(self.funcs.flash_exit_xip)();
205+
206+
// Return the last address in the flash range. Any other value (including `Ok(())`)
207+
// is an error.
208+
ErrorCode::new(address + size)
209+
.map(|e| Err(e))
210+
.unwrap_or(Ok(()))
211+
}
186212
}
187213

188214
impl Drop for RP2Algo {

0 commit comments

Comments
 (0)