Skip to content

isolated nonvolatile storage capsule with test #478

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Makefile for user application

# Specify this directory relative to the current application.
TOCK_USERLAND_BASE_DIR = ../../../..

# Which files to compile.
C_SRCS := $(wildcard *.c)

# Set the SHA256 hash for the credential checker on the `nrf52840dk-test-invs`
# board. Also set the write_id to 0 meaning no storage permissions.
ELF2TAB_ARGS += --sha256 --write_id 0

# Include userland master makefile. Contains rules and flags for actually
# building the application.
include $(TOCK_USERLAND_BASE_DIR)/AppMakefile.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Isolated Nonvolatile Storage Test App With No Permissions
=========================================================

This app tries to use the isolated nonvolatile storage capsule but is granted no
storage access permissions (its `write_id` in the TBF header is 0). This
verifies that the read and write calls return `ENOSUPPORT`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

#include <libtock-sync/storage/isolated_nonvolatile_storage.h>


static bool test_exists(void) {
bool exists = libtock_isolated_nonvolatile_storage_exists();

if (exists) {
return true;
}
return false;
}

static bool test_size(void) {
uint64_t num_bytes;
returncode_t ret;

ret = libtocksync_isolated_nonvolatile_storage_get_number_bytes(&num_bytes);

if (ret == RETURNCODE_ENOSUPPORT) {
return true;
}
return false;
}

static bool test_read(void) {
returncode_t ret;

uint8_t readbuf[512];

uint32_t offset = 0;
uint32_t length = 10;

ret = libtocksync_isolated_nonvolatile_storage_read(offset, readbuf, length);
if (ret == RETURNCODE_ENOSUPPORT) {
return true;
}
return false;
}


static bool test_write(void) {
returncode_t ret;

uint8_t writebuf[512] = {0x3f};

uint32_t offset = 0;
uint32_t length = 10;

ret = libtocksync_isolated_nonvolatile_storage_write(offset, writebuf, length);
if (ret == RETURNCODE_ENOSUPPORT) {
return true;
}
return false;
}


int main(void) {
printf("[TEST] Isolated Nonvolatile Storage - No Permissions\n");

printf("[INVS TEST] Exists: ");
if (test_exists()) {
printf("Success\n");
} else {
printf("Fail\n");
}

printf("[INVS TEST] Size: ");
if (test_size()) {
printf("Success\n");
} else {
printf("Fail\n");
}

printf("[INVS TEST] Read: ");
if (test_read()) {
printf("Success\n");
} else {
printf("Fail\n");
}

printf("[INVS TEST] Write: ");
if (test_write()) {
printf("Success\n");
} else {
printf("Fail\n");
}

return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Makefile for user application

# Specify this directory relative to the current application.
TOCK_USERLAND_BASE_DIR = ../../../..

# Which files to compile.
C_SRCS := $(wildcard *.c)

# Set the SHA256 hash for the credential checker on the `nrf52840dk-test-invs`
# board.
ELF2TAB_ARGS += --sha256

# Include userland master makefile. Contains rules and flags for actually
# building the application.
include $(TOCK_USERLAND_BASE_DIR)/AppMakefile.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
Isolated Nonvolatile Storage Test App
=====================================

This app writes to flash storage and reads it back to test that isolated storage
is working.

Example Output
--------------

```
[TEST] Isolated Nonvolatile Storage
Have 4096 bytes of nonvolatile storage
Test with read size 14 and write size 256 ...
Test with read size 14 and write size 256 ...
Test with read size 512 and write size 512 ...
Write to end of region (offset 3584)
Test with read size 512 and write size 500 ...
Write beyond end region, should fail (offset 4096)
Test with read size 512 and write size 501 ...
ERROR calling write. returncode: -6
Write starts beyond end region, should fail (offset 4097)
Test with read size 512 and write size 1 ...
ERROR calling write. returncode: -6
Write starts before start region, should fail (offset -1)
Test with read size 512 and write size 512 ...
ERROR calling write. returncode: -6
All tests succeeded
```
86 changes: 86 additions & 0 deletions examples/tests/isolated_nonvolatile_storage/invs_read_write/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

#include <libtock-sync/storage/isolated_nonvolatile_storage.h>

static int test_all(void);
static int test(uint8_t* readbuf, size_t readsize, uint8_t* writebuf, size_t writesize, size_t offset);

static size_t min(size_t a, size_t b) {
if (a < b) return a;
return b;
}

int main(void) {
printf("[TEST] Isolated Nonvolatile Storage\n");

int r = test_all();
if (r == 0) {
printf("All tests succeeded\n");
} else {
printf("Failed with code %d\n", r);
}

return r;
}

static int test_all(void) {
uint64_t num_bytes_total;
libtocksync_isolated_nonvolatile_storage_get_number_bytes(&num_bytes_total);
int num_bytes = num_bytes_total;
printf("Have %i bytes of nonvolatile storage\n", num_bytes);

int r;
uint8_t readbuf[512];
uint8_t writebuf[512];

if ((r = test(readbuf, 14, writebuf, 256, 0)) != 0) return r;
if ((r = test(readbuf, 14, writebuf, 256, 20)) != 0) return r;
if ((r = test(readbuf, 512, writebuf, 512, 0)) != 0) return r;

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

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

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

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

return 0;
}

static int test(uint8_t* readbuf, size_t readsize, uint8_t* writebuf, size_t writesize, size_t offset) {
int ret;

printf("\tTest with read size %d and write size %d ...\n", readsize, writesize);

for (size_t i = 0; i < writesize; i++) {
writebuf[i] = i;
}

ret = libtocksync_isolated_nonvolatile_storage_write(offset, writebuf, writesize);
if (ret != RETURNCODE_SUCCESS) {
printf("\tERROR calling write. returncode: %d\n", ret);
return ret;
}

ret = libtocksync_isolated_nonvolatile_storage_read(offset, readbuf, readsize);
if (ret != RETURNCODE_SUCCESS) {
printf("\tERROR calling read. returncode: %d\n", ret);
return ret;
}

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

return 0;
}
15 changes: 15 additions & 0 deletions examples/tests/isolated_nonvolatile_storage/invs_single/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Makefile for user application

# Specify this directory relative to the current application.
TOCK_USERLAND_BASE_DIR = ../../../..

# Which files to compile.
C_SRCS := $(wildcard *.c)

# Set the SHA256 hash for the credential checker on the `nrf52840dk-test-invs`
# board.
ELF2TAB_ARGS += --sha256

# Include userland master makefile. Contains rules and flags for actually
# building the application.
include $(TOCK_USERLAND_BASE_DIR)/AppMakefile.mk
83 changes: 83 additions & 0 deletions examples/tests/isolated_nonvolatile_storage/invs_single/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
Nonvolatile Storage Test App
============================

This app writes to flash storage and reads it back to test that flash storage
is working. It requires that a
`capsules::nonvolatile_storage_driver::NonvolatileStorage` interface be provided
to userland.



Example Hail Setup
------------------

One way to provide the Driver interface with Hail looks like:

```diff
--- a/boards/hail/src/main.rs
+++ b/boards/hail/src/main.rs
@@ -74,6 +74,7 @@ struct Hail {
ipc: kernel::ipc::IPC,
crc: &'static capsules::crc::Crc<'static, sam4l::crccu::Crccu<'static>>,
dac: &'static capsules::dac::Dac<'static>,
+ nv: &'static capsules::nonvolatile_storage_driver::NonvolatileStorage<'static>,
}

/// Mapping of integer syscalls to objects that implement syscalls.
@@ -104,6 +105,8 @@ impl Platform for Hail {
capsules::dac::DRIVER_NUM => f(Some(self.dac)),

kernel::ipc::DRIVER_NUM => f(Some(&self.ipc)),
+
+ 27 => f(Some(self.nv)),
_ => f(None),
}
}
@@ -443,6 +446,38 @@ pub unsafe fn reset_handler() {
capsules::dac::Dac::new(&mut sam4l::dac::DAC)
);

+ // Flash
+ let mux_flash = static_init!(
+ capsules::virtual_flash::MuxFlash<'static, sam4l::flashcalw::FLASHCALW>,
+ capsules::virtual_flash::MuxFlash::new(&sam4l::flashcalw::FLASH_CONTROLLER));
+ hil::flash::HasClient::set_client(&sam4l::flashcalw::FLASH_CONTROLLER, mux_flash);
+
+ // Nonvolatile Storage
+ let virtual_flash_nv = static_init!(
+ capsules::virtual_flash::FlashUser<'static, sam4l::flashcalw::FLASHCALW>,
+ capsules::virtual_flash::FlashUser::new(mux_flash));
+ pub static mut NV_PAGEBUFFER: sam4l::flashcalw::Sam4lPage = sam4l::flashcalw::Sam4lPage::new();
+
+ let nv_nv_to_page = static_init!(
+ capsules::nonvolatile_to_pages::NonvolatileToPages<'static,
+ capsules::virtual_flash::FlashUser<'static, sam4l::flashcalw::FLASHCALW>>,
+ capsules::nonvolatile_to_pages::NonvolatileToPages::new(
+ virtual_flash_nv,
+ &mut NV_PAGEBUFFER));
+ hil::flash::HasClient::set_client(virtual_flash_nv, nv_nv_to_page);
+
+ pub static mut NV_BUFFER: [u8; 512] = [0; 512];
+ let nv = static_init!(
+ capsules::nonvolatile_storage_driver::NonvolatileStorage<'static>,
+ capsules::nonvolatile_storage_driver::NonvolatileStorage::new(
+ nv_nv_to_page, kernel::Grant::create(),
+ 0x60000, // Start address for userspace accessible region
+ 0x20000, // Length of userspace accessible region
+ 0, // Start address of kernel accessible region
+ 0, // Length of kernel accessible region
+ &mut NV_BUFFER));
+ hil::nonvolatile_storage::NonvolatileStorage::set_client(nv_nv_to_page, nv);
+
let hail = Hail {
console: console,
gpio: gpio,
@@ -460,6 +495,7 @@ pub unsafe fn reset_handler() {
ipc: kernel::ipc::IPC::new(),
crc: crc,
dac: dac,
+ nv: nv,
};

// Need to reset the nRF on boot
```
Loading