Skip to content

Commit f2ec05a

Browse files
committed
invs: update read/write test app
1 parent 39e4b3f commit f2ec05a

File tree

3 files changed

+54
-100
lines changed

3 files changed

+54
-100
lines changed

examples/tests/isolated_nonvolatile_storage/invs_read_write/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ TOCK_USERLAND_BASE_DIR = ../../../..
66
# Which files to compile.
77
C_SRCS := $(wildcard *.c)
88

9+
# Set the SHA256 hash for the credential checker on the `nrf52840dk-test-invs`
10+
# board.
11+
ELF2TAB_ARGS += --sha256
12+
913
# Include userland master makefile. Contains rules and flags for actually
1014
# building the application.
1115
include $(TOCK_USERLAND_BASE_DIR)/AppMakefile.mk
Lines changed: 28 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,28 @@
1-
Nonvolatile Storage Test App
2-
============================
3-
4-
This app writes to flash storage and reads it back to test that flash storage
5-
is working. It requires that a
6-
`capsules::nonvolatile_storage_driver::NonvolatileStorage` interface be provided
7-
to userland.
8-
9-
10-
11-
Example Hail Setup
12-
------------------
13-
14-
One way to provide the Driver interface with Hail looks like:
15-
16-
```diff
17-
--- a/boards/hail/src/main.rs
18-
+++ b/boards/hail/src/main.rs
19-
@@ -74,6 +74,7 @@ struct Hail {
20-
ipc: kernel::ipc::IPC,
21-
crc: &'static capsules::crc::Crc<'static, sam4l::crccu::Crccu<'static>>,
22-
dac: &'static capsules::dac::Dac<'static>,
23-
+ nv: &'static capsules::nonvolatile_storage_driver::NonvolatileStorage<'static>,
24-
}
25-
26-
/// Mapping of integer syscalls to objects that implement syscalls.
27-
@@ -104,6 +105,8 @@ impl Platform for Hail {
28-
capsules::dac::DRIVER_NUM => f(Some(self.dac)),
29-
30-
kernel::ipc::DRIVER_NUM => f(Some(&self.ipc)),
31-
+
32-
+ 27 => f(Some(self.nv)),
33-
_ => f(None),
34-
}
35-
}
36-
@@ -443,6 +446,38 @@ pub unsafe fn reset_handler() {
37-
capsules::dac::Dac::new(&mut sam4l::dac::DAC)
38-
);
39-
40-
+ // Flash
41-
+ let mux_flash = static_init!(
42-
+ capsules::virtual_flash::MuxFlash<'static, sam4l::flashcalw::FLASHCALW>,
43-
+ capsules::virtual_flash::MuxFlash::new(&sam4l::flashcalw::FLASH_CONTROLLER));
44-
+ hil::flash::HasClient::set_client(&sam4l::flashcalw::FLASH_CONTROLLER, mux_flash);
45-
+
46-
+ // Nonvolatile Storage
47-
+ let virtual_flash_nv = static_init!(
48-
+ capsules::virtual_flash::FlashUser<'static, sam4l::flashcalw::FLASHCALW>,
49-
+ capsules::virtual_flash::FlashUser::new(mux_flash));
50-
+ pub static mut NV_PAGEBUFFER: sam4l::flashcalw::Sam4lPage = sam4l::flashcalw::Sam4lPage::new();
51-
+
52-
+ let nv_nv_to_page = static_init!(
53-
+ capsules::nonvolatile_to_pages::NonvolatileToPages<'static,
54-
+ capsules::virtual_flash::FlashUser<'static, sam4l::flashcalw::FLASHCALW>>,
55-
+ capsules::nonvolatile_to_pages::NonvolatileToPages::new(
56-
+ virtual_flash_nv,
57-
+ &mut NV_PAGEBUFFER));
58-
+ hil::flash::HasClient::set_client(virtual_flash_nv, nv_nv_to_page);
59-
+
60-
+ pub static mut NV_BUFFER: [u8; 512] = [0; 512];
61-
+ let nv = static_init!(
62-
+ capsules::nonvolatile_storage_driver::NonvolatileStorage<'static>,
63-
+ capsules::nonvolatile_storage_driver::NonvolatileStorage::new(
64-
+ nv_nv_to_page, kernel::Grant::create(),
65-
+ 0x60000, // Start address for userspace accessible region
66-
+ 0x20000, // Length of userspace accessible region
67-
+ 0, // Start address of kernel accessible region
68-
+ 0, // Length of kernel accessible region
69-
+ &mut NV_BUFFER));
70-
+ hil::nonvolatile_storage::NonvolatileStorage::set_client(nv_nv_to_page, nv);
71-
+
72-
let hail = Hail {
73-
console: console,
74-
gpio: gpio,
75-
@@ -460,6 +495,7 @@ pub unsafe fn reset_handler() {
76-
ipc: kernel::ipc::IPC::new(),
77-
crc: crc,
78-
dac: dac,
79-
+ nv: nv,
80-
};
81-
82-
// Need to reset the nRF on boot
83-
```
1+
Isolated Nonvolatile Storage Test App
2+
=====================================
3+
4+
This app writes to flash storage and reads it back to test that isolated storage
5+
is working.
6+
7+
Example Output
8+
--------------
9+
10+
```
11+
[TEST] Isolated Nonvolatile Storage
12+
Have 4096 bytes of nonvolatile storage
13+
Test with read size 14 and write size 256 ...
14+
Test with read size 14 and write size 256 ...
15+
Test with read size 512 and write size 512 ...
16+
Write to end of region (offset 3584)
17+
Test with read size 512 and write size 500 ...
18+
Write beyond end region, should fail (offset 4096)
19+
Test with read size 512 and write size 501 ...
20+
ERROR calling write. returncode: -6
21+
Write starts beyond end region, should fail (offset 4097)
22+
Test with read size 512 and write size 1 ...
23+
ERROR calling write. returncode: -6
24+
Write starts before start region, should fail (offset -1)
25+
Test with read size 512 and write size 512 ...
26+
ERROR calling write. returncode: -6
27+
All tests succeeded
28+
```

examples/tests/isolated_nonvolatile_storage/invs_read_write/main.c

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@
55
#include <libtock-sync/storage/isolated_nonvolatile_storage.h>
66

77
static int test_all(void);
8-
static int test(uint8_t* readbuf, uint8_t* writebuf, size_t size, size_t offset, size_t len);
8+
static int test(uint8_t* readbuf, size_t readsize, uint8_t* writebuf, size_t writesize, size_t offset);
9+
10+
static size_t min(size_t a, size_t b) {
11+
if (a < b) return a;
12+
return b;
13+
}
914

1015
int main(void) {
1116
printf("[TEST] Isolated Nonvolatile Storage\n");
@@ -21,56 +26,56 @@ int main(void) {
2126
}
2227

2328
static int test_all(void) {
24-
int num_bytes;
25-
libtocksync_isolated_nonvolatile_storage_get_number_bytes((uint32_t*) &num_bytes);
29+
uint64_t num_bytes_total;
30+
libtocksync_isolated_nonvolatile_storage_get_number_bytes(&num_bytes_total);
31+
int num_bytes = num_bytes_total;
2632
printf("Have %i bytes of nonvolatile storage\n", num_bytes);
2733

2834
int r;
2935
uint8_t readbuf[512];
3036
uint8_t writebuf[512];
3137

32-
if ((r = test(readbuf, writebuf, 256, 0, 14)) != 0) return r;
33-
if ((r = test(readbuf, writebuf, 256, 20, 14)) != 0) return r;
34-
if ((r = test(readbuf, writebuf, 512, 0, 512)) != 0) return r;
38+
if ((r = test(readbuf, 14, writebuf, 256, 0)) != 0) return r;
39+
if ((r = test(readbuf, 14, writebuf, 256, 20)) != 0) return r;
40+
if ((r = test(readbuf, 512, writebuf, 512, 0)) != 0) return r;
3541

3642
printf("Write to end of region (offset %d)\n", num_bytes - 512);
37-
if ((r = test(readbuf, writebuf, 512, num_bytes - 512, 500)) != 0) return r;
43+
if ((r = test(readbuf, 512, writebuf, 500, num_bytes - 512)) != 0) return r;
3844

3945
printf("Write beyond end region, should fail (offset %d)\n", num_bytes);
40-
if ((r = test(readbuf, writebuf, 512, num_bytes, 501)) == 0) return -1;
46+
if ((r = test(readbuf, 512, writebuf, 501, num_bytes)) == 0) return -1;
4147

4248
printf("Write starts beyond end region, should fail (offset %d)\n", num_bytes + 1);
43-
if ((r = test(readbuf, writebuf, 512, num_bytes + 1, 1)) == 0) return -1;
49+
if ((r = test(readbuf, 512, writebuf, 1, num_bytes + 1)) == 0) return -1;
4450

4551
printf("Write starts before start region, should fail (offset %d)\n", -1);
46-
if ((r = test(readbuf, writebuf, 512, -1, 1)) == 0) return -1;
52+
if ((r = test(readbuf, 512, writebuf, 512, -1)) == 0) return -1;
4753

4854
return 0;
4955
}
5056

51-
static int test(uint8_t* readbuf, uint8_t* writebuf, size_t size, size_t offset, size_t len) {
57+
static int test(uint8_t* readbuf, size_t readsize, uint8_t* writebuf, size_t writesize, size_t offset) {
5258
int ret;
53-
int length_written, length_read;
5459

55-
printf("\tTest with size %d ...\n", size);
60+
printf("\tTest with read size %d and write size %d ...\n", readsize, writesize);
5661

57-
for (size_t i = 0; i < len; i++) {
62+
for (size_t i = 0; i < writesize; i++) {
5863
writebuf[i] = i;
5964
}
6065

61-
ret = libtocksync_isolated_nonvolatile_storage_write(offset, len, writebuf, size, &length_written);
66+
ret = libtocksync_isolated_nonvolatile_storage_write(offset, writebuf, writesize);
6267
if (ret != RETURNCODE_SUCCESS) {
6368
printf("\tERROR calling write. returncode: %d\n", ret);
6469
return ret;
6570
}
6671

67-
ret = libtocksync_isolated_nonvolatile_storage_read(offset, len, readbuf, size, &length_read);
72+
ret = libtocksync_isolated_nonvolatile_storage_read(offset, readbuf, readsize);
6873
if (ret != RETURNCODE_SUCCESS) {
6974
printf("\tERROR calling read. returncode: %d\n", ret);
7075
return ret;
7176
}
7277

73-
for (size_t i = 0; i < len; i++) {
78+
for (size_t i = 0; i < min(readsize, writesize); i++) {
7479
if (readbuf[i] != writebuf[i]) {
7580
printf("\tInconsistency between data written and read at index %u\n", i);
7681
return -1;

0 commit comments

Comments
 (0)