Merge pull request #163 from ia0/v2_lib
Create new store and add storage API
This commit is contained in:
6
.github/workflows/cargo_fmt.yml
vendored
6
.github/workflows/cargo_fmt.yml
vendored
@@ -62,6 +62,12 @@ jobs:
|
||||
command: fmt
|
||||
args: --manifest-path libraries/crypto/Cargo.toml --all -- --check
|
||||
|
||||
- name: Cargo format libraries/persistent_store
|
||||
uses: actions-rs/cargo@v1
|
||||
with:
|
||||
command: fmt
|
||||
args: --manifest-path libraries/persistent_store/Cargo.toml --all -- --check
|
||||
|
||||
- name: Cargo format tools/heapviz
|
||||
uses: actions-rs/cargo@v1
|
||||
with:
|
||||
|
||||
26
.github/workflows/persistent_store_test.yml
vendored
Normal file
26
.github/workflows/persistent_store_test.yml
vendored
Normal file
@@ -0,0 +1,26 @@
|
||||
---
|
||||
name: Persistent store tests
|
||||
on:
|
||||
push:
|
||||
paths:
|
||||
- 'libraries/peristent_store/**'
|
||||
pull_request:
|
||||
types: [opened, synchronize, reopened]
|
||||
|
||||
jobs:
|
||||
persistent_store_test:
|
||||
runs-on: ubuntu-18.04
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
|
||||
- name: Unit testing of Persistent store library (release mode)
|
||||
uses: actions-rs/cargo@v1
|
||||
with:
|
||||
command: test
|
||||
args: --manifest-path libraries/persistent_store/Cargo.toml --release --features std
|
||||
|
||||
- name: Unit testing of Persistent store library (debug mode)
|
||||
uses: actions-rs/cargo@v1
|
||||
with:
|
||||
command: test
|
||||
args: --manifest-path libraries/persistent_store/Cargo.toml --features std
|
||||
11
libraries/persistent_store/Cargo.toml
Normal file
11
libraries/persistent_store/Cargo.toml
Normal file
@@ -0,0 +1,11 @@
|
||||
[package]
|
||||
name = "persistent_store"
|
||||
version = "0.1.0"
|
||||
authors = ["Julien Cretin <cretin@google.com>"]
|
||||
license = "Apache-2.0"
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
|
||||
[features]
|
||||
std = []
|
||||
19
libraries/persistent_store/src/lib.rs
Normal file
19
libraries/persistent_store/src/lib.rs
Normal file
@@ -0,0 +1,19 @@
|
||||
// Copyright 2019-2020 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
|
||||
mod storage;
|
||||
|
||||
pub use self::storage::{Storage, StorageError, StorageIndex, StorageResult};
|
||||
95
libraries/persistent_store/src/storage.rs
Normal file
95
libraries/persistent_store/src/storage.rs
Normal file
@@ -0,0 +1,95 @@
|
||||
// Copyright 2019-2020 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
/// Represents a byte position in a storage.
|
||||
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
||||
pub struct StorageIndex {
|
||||
pub page: usize,
|
||||
pub byte: usize,
|
||||
}
|
||||
|
||||
/// Represents a possible storage error.
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub enum StorageError {
|
||||
/// Arguments are not correctly aligned.
|
||||
NotAligned,
|
||||
|
||||
/// Arguments are out of bounds.
|
||||
OutOfBounds,
|
||||
|
||||
/// Implementation-specific error.
|
||||
CustomError,
|
||||
}
|
||||
|
||||
pub type StorageResult<T> = Result<T, StorageError>;
|
||||
|
||||
/// Abstracts a flash storage.
|
||||
pub trait Storage {
|
||||
/// The size of a word in bytes.
|
||||
fn word_size(&self) -> usize;
|
||||
|
||||
/// The size of a page in bytes.
|
||||
fn page_size(&self) -> usize;
|
||||
|
||||
/// The number of pages in the storage.
|
||||
fn num_pages(&self) -> usize;
|
||||
|
||||
/// Maximum number of times a word can be written between page erasures.
|
||||
fn max_word_writes(&self) -> usize;
|
||||
|
||||
/// Maximum number of times a page can be erased.
|
||||
fn max_page_erases(&self) -> usize;
|
||||
|
||||
/// Reads a byte slice from the storage.
|
||||
///
|
||||
/// The `index` must designate `length` bytes in the storage.
|
||||
fn read_slice(&self, index: StorageIndex, length: usize) -> StorageResult<&[u8]>;
|
||||
|
||||
/// Writes a word slice to the storage.
|
||||
///
|
||||
/// The following pre-conditions must hold:
|
||||
/// - The `index` must designate `value.len()` bytes in the storage.
|
||||
/// - Both `index` and `value.len()` must be word-aligned.
|
||||
/// - The written words should not have been written too many times since last page erasure.
|
||||
fn write_slice(&mut self, index: StorageIndex, value: &[u8]) -> StorageResult<()>;
|
||||
|
||||
/// Erases a page of the storage.
|
||||
///
|
||||
/// The `page` must be in the storage.
|
||||
fn erase_page(&mut self, page: usize) -> StorageResult<()>;
|
||||
}
|
||||
|
||||
impl StorageIndex {
|
||||
/// Whether a slice fits in a storage page.
|
||||
fn is_valid(self, length: usize, storage: &impl Storage) -> bool {
|
||||
let page_size = storage.page_size();
|
||||
self.page < storage.num_pages() && length <= page_size && self.byte <= page_size - length
|
||||
}
|
||||
|
||||
/// Returns the range of a valid slice.
|
||||
///
|
||||
/// The range starts at `self` with `length` bytes.
|
||||
pub fn range(
|
||||
self,
|
||||
length: usize,
|
||||
storage: &impl Storage,
|
||||
) -> StorageResult<core::ops::Range<usize>> {
|
||||
if self.is_valid(length, storage) {
|
||||
let start = self.page * storage.page_size() + self.byte;
|
||||
Ok(start..start + length)
|
||||
} else {
|
||||
Err(StorageError::OutOfBounds)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,9 @@
|
||||
dd5920dfb172d9371b29d019b6a37fae1a995bf9d814000944d9ef36bad31513 third_party/tock/target/thumbv7em-none-eabi/release/nrf52840dk.bin
|
||||
e1b147fefa75befe6f280e8aec41fa62b02e5c01bb1c940736b1903b597afe13 target/nrf52840dk_merged.hex
|
||||
1518f10da8515c321dcb19847701dcf15511e44d8a9b80576cb36e13258353fb target/nrf52840dk_merged.hex
|
||||
e4acfa602a5cc5d7c61d465f873918e8e0858628d0e5f8e0db26a7b7dd0b94d4 third_party/tock/target/thumbv7em-none-eabi/release/nrf52840_dongle.bin
|
||||
99b69a33320a4c5218b9e5084f2ad56957e04465dbbb53b0ab1051a176ba73a0 target/nrf52840_dongle_merged.hex
|
||||
ba5c294725ba98230427601ab9e09291b3de10d60a78a5e1078ac2357c991fb7 target/nrf52840_dongle_merged.hex
|
||||
c0ace9f13ef3fd18c576a735ae23b3956bf8dd346f20c6217086e748d6bad8a2 third_party/tock/target/thumbv7em-none-eabi/release/nrf52840_dongle_dfu.bin
|
||||
3bde950d855ffa68eb334033388b292e98cdbf97993a57eca4524de101166f8b target/nrf52840_dongle_dfu_merged.hex
|
||||
e33940f7277c16825e847762d23a4eea3ad8fe104f20b858ef339687c74e81bc target/nrf52840_dongle_dfu_merged.hex
|
||||
06a38a0d6d356145467a73c765e28a945878f663664016f888393207097bfe10 third_party/tock/target/thumbv7em-none-eabi/release/nrf52840_mdk_dfu.bin
|
||||
f3ed663311204ac709ed05dfb3d09ff283c3df7dbc5480ad3ec050523b0a2ed2 target/nrf52840_mdk_dfu_merged.hex
|
||||
a2ee6798d20da62ed2c3ea7164620253d4b2319a3ed2f2dbc2973c1a5dd838a9 target/tab/ctap2.tab
|
||||
152f6a7d0eba3ce7e9577bc90d6cb7466cf1eacb5e257ae1d8d09794b504ab38 target/nrf52840_mdk_dfu_merged.hex
|
||||
49d5b242ae2d63b0d243cea3271c2847d074a5fd15798c728c841126cdfe86ee target/tab/ctap2.tab
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
2426ee9a6c75e325537818081d45445d95468a4c0a77feacdc6133d7d9aa227a third_party/tock/target/thumbv7em-none-eabi/release/nrf52840dk.bin
|
||||
0038c7256f2bb8406364ca2deb8b228363e524a076263e23625fa2a3b4b35df6 target/nrf52840dk_merged.hex
|
||||
f8c54ac3f2e153e3cf6d4651663fc12df1f3d61fefd40f953b3181c4d9e8b43c target/nrf52840dk_merged.hex
|
||||
c53d1e1db72df25950fa6d28699a2d38757def0dcbeb0d09d2366481cf0149a6 third_party/tock/target/thumbv7em-none-eabi/release/nrf52840_dongle.bin
|
||||
183365121eab69bb15746e7907bb0d66b9cc766d26a6a0dab301583c4bb8c4c3 target/nrf52840_dongle_merged.hex
|
||||
1fff9a157e779d92245420053147dc8e894ea281f4de2a524ab6d815614b050a target/nrf52840_dongle_merged.hex
|
||||
233b5ba4459523759e3171cee83cdb3a383bbe65727c8ece64dfe5321d6ebe34 third_party/tock/target/thumbv7em-none-eabi/release/nrf52840_dongle_dfu.bin
|
||||
95be6da19f67a9a85e83d97bdaaf5f062c82da00b9a767dd0bc49eaa2c74bb26 target/nrf52840_dongle_dfu_merged.hex
|
||||
5597a6c3c4a9b1bd72d0d377b084b17627301043724fd1e67fe7d87f0e2fab25 target/nrf52840_dongle_dfu_merged.hex
|
||||
1baaf518a74c6077cb936d9cf178b6dd0232e7562fa56174886b05b77886cc32 third_party/tock/target/thumbv7em-none-eabi/release/nrf52840_mdk_dfu.bin
|
||||
39c94b1dd8e65438d481835663c4ec2cda99311011031403c9244ed5095230c7 target/nrf52840_mdk_dfu_merged.hex
|
||||
b02eb9439df1f8a3c21eb29f39c3b72c0f709b05a4e8a968441e73678cfb55df target/tab/ctap2.tab
|
||||
53801f162b40c861ab2612c2972fe363889c7c7c313e6f80c1c68ce2dc8049fb target/nrf52840_mdk_dfu_merged.hex
|
||||
d7146c0b30cc08a34531895c37420bdd36056ffcb5f537b92ee34876a42557a6 target/tab/ctap2.tab
|
||||
|
||||
@@ -6,8 +6,8 @@ Min RAM size from segments in ELF: 20 bytes
|
||||
Number of writeable flash regions: 0
|
||||
Adding .crt0_header section. Offset: 64 (0x40). Length: 64 (0x40) bytes.
|
||||
Entry point is in .text section
|
||||
Adding .text section. Offset: 128 (0x80). Length: 187280 (0x2db90) bytes.
|
||||
Adding .stack section. Offset: 187408 (0x2dc10). Length: 16384 (0x4000) bytes.
|
||||
Adding .text section. Offset: 128 (0x80). Length: 186976 (0x2da60) bytes.
|
||||
Adding .stack section. Offset: 187104 (0x2dae0). Length: 16384 (0x4000) bytes.
|
||||
Searching for .rel.X sections to add.
|
||||
TBF Header:
|
||||
version: 2 0x2
|
||||
@@ -30,8 +30,8 @@ Min RAM size from segments in ELF: 20 bytes
|
||||
Number of writeable flash regions: 0
|
||||
Adding .crt0_header section. Offset: 64 (0x40). Length: 64 (0x40) bytes.
|
||||
Entry point is in .text section
|
||||
Adding .text section. Offset: 128 (0x80). Length: 187280 (0x2db90) bytes.
|
||||
Adding .stack section. Offset: 187408 (0x2dc10). Length: 16384 (0x4000) bytes.
|
||||
Adding .text section. Offset: 128 (0x80). Length: 186976 (0x2da60) bytes.
|
||||
Adding .stack section. Offset: 187104 (0x2dae0). Length: 16384 (0x4000) bytes.
|
||||
Searching for .rel.X sections to add.
|
||||
TBF Header:
|
||||
version: 2 0x2
|
||||
@@ -54,8 +54,8 @@ Min RAM size from segments in ELF: 20 bytes
|
||||
Number of writeable flash regions: 0
|
||||
Adding .crt0_header section. Offset: 64 (0x40). Length: 64 (0x40) bytes.
|
||||
Entry point is in .text section
|
||||
Adding .text section. Offset: 128 (0x80). Length: 187280 (0x2db90) bytes.
|
||||
Adding .stack section. Offset: 187408 (0x2dc10). Length: 16384 (0x4000) bytes.
|
||||
Adding .text section. Offset: 128 (0x80). Length: 186976 (0x2da60) bytes.
|
||||
Adding .stack section. Offset: 187104 (0x2dae0). Length: 16384 (0x4000) bytes.
|
||||
Searching for .rel.X sections to add.
|
||||
TBF Header:
|
||||
version: 2 0x2
|
||||
@@ -78,8 +78,8 @@ Min RAM size from segments in ELF: 20 bytes
|
||||
Number of writeable flash regions: 0
|
||||
Adding .crt0_header section. Offset: 64 (0x40). Length: 64 (0x40) bytes.
|
||||
Entry point is in .text section
|
||||
Adding .text section. Offset: 128 (0x80). Length: 187280 (0x2db90) bytes.
|
||||
Adding .stack section. Offset: 187408 (0x2dc10). Length: 16384 (0x4000) bytes.
|
||||
Adding .text section. Offset: 128 (0x80). Length: 186976 (0x2da60) bytes.
|
||||
Adding .stack section. Offset: 187104 (0x2dae0). Length: 16384 (0x4000) bytes.
|
||||
Searching for .rel.X sections to add.
|
||||
TBF Header:
|
||||
version: 2 0x2
|
||||
|
||||
@@ -6,8 +6,8 @@ Min RAM size from segments in ELF: 20 bytes
|
||||
Number of writeable flash regions: 0
|
||||
Adding .crt0_header section. Offset: 64 (0x40). Length: 64 (0x40) bytes.
|
||||
Entry point is in .text section
|
||||
Adding .text section. Offset: 128 (0x80). Length: 187264 (0x2db80) bytes.
|
||||
Adding .stack section. Offset: 187392 (0x2dc00). Length: 16384 (0x4000) bytes.
|
||||
Adding .text section. Offset: 128 (0x80). Length: 186976 (0x2da60) bytes.
|
||||
Adding .stack section. Offset: 187104 (0x2dae0). Length: 16384 (0x4000) bytes.
|
||||
Searching for .rel.X sections to add.
|
||||
TBF Header:
|
||||
version: 2 0x2
|
||||
@@ -30,8 +30,8 @@ Min RAM size from segments in ELF: 20 bytes
|
||||
Number of writeable flash regions: 0
|
||||
Adding .crt0_header section. Offset: 64 (0x40). Length: 64 (0x40) bytes.
|
||||
Entry point is in .text section
|
||||
Adding .text section. Offset: 128 (0x80). Length: 187264 (0x2db80) bytes.
|
||||
Adding .stack section. Offset: 187392 (0x2dc00). Length: 16384 (0x4000) bytes.
|
||||
Adding .text section. Offset: 128 (0x80). Length: 186976 (0x2da60) bytes.
|
||||
Adding .stack section. Offset: 187104 (0x2dae0). Length: 16384 (0x4000) bytes.
|
||||
Searching for .rel.X sections to add.
|
||||
TBF Header:
|
||||
version: 2 0x2
|
||||
@@ -54,8 +54,8 @@ Min RAM size from segments in ELF: 20 bytes
|
||||
Number of writeable flash regions: 0
|
||||
Adding .crt0_header section. Offset: 64 (0x40). Length: 64 (0x40) bytes.
|
||||
Entry point is in .text section
|
||||
Adding .text section. Offset: 128 (0x80). Length: 187264 (0x2db80) bytes.
|
||||
Adding .stack section. Offset: 187392 (0x2dc00). Length: 16384 (0x4000) bytes.
|
||||
Adding .text section. Offset: 128 (0x80). Length: 186976 (0x2da60) bytes.
|
||||
Adding .stack section. Offset: 187104 (0x2dae0). Length: 16384 (0x4000) bytes.
|
||||
Searching for .rel.X sections to add.
|
||||
TBF Header:
|
||||
version: 2 0x2
|
||||
@@ -78,8 +78,8 @@ Min RAM size from segments in ELF: 20 bytes
|
||||
Number of writeable flash regions: 0
|
||||
Adding .crt0_header section. Offset: 64 (0x40). Length: 64 (0x40) bytes.
|
||||
Entry point is in .text section
|
||||
Adding .text section. Offset: 128 (0x80). Length: 187264 (0x2db80) bytes.
|
||||
Adding .stack section. Offset: 187392 (0x2dc00). Length: 16384 (0x4000) bytes.
|
||||
Adding .text section. Offset: 128 (0x80). Length: 186976 (0x2da60) bytes.
|
||||
Adding .stack section. Offset: 187104 (0x2dae0). Length: 16384 (0x4000) bytes.
|
||||
Searching for .rel.X sections to add.
|
||||
TBF Header:
|
||||
version: 2 0x2
|
||||
|
||||
@@ -23,6 +23,9 @@ cd ../..
|
||||
cd libraries/crypto
|
||||
cargo fmt --all -- --check
|
||||
cd ../..
|
||||
cd libraries/persistent_store
|
||||
cargo fmt --all -- --check
|
||||
cd ../..
|
||||
cd tools/heapviz
|
||||
cargo fmt --all -- --check
|
||||
cd ../..
|
||||
@@ -84,6 +87,9 @@ then
|
||||
cd libraries/crypto
|
||||
RUSTFLAGS='-C target-feature=+aes' cargo test --release --features std,derive_debug
|
||||
cd ../..
|
||||
cd libraries/persistent_store
|
||||
cargo test --release --features std
|
||||
cd ../..
|
||||
cargo test --release --features std
|
||||
|
||||
echo "Running unit tests on the desktop (debug mode)..."
|
||||
@@ -93,6 +99,9 @@ then
|
||||
cd libraries/crypto
|
||||
RUSTFLAGS='-C target-feature=+aes' cargo test --features std,derive_debug
|
||||
cd ../..
|
||||
cd libraries/persistent_store
|
||||
cargo test --features std
|
||||
cd ../..
|
||||
cargo test --features std
|
||||
|
||||
echo "Running unit tests on the desktop (release mode + CTAP1)..."
|
||||
|
||||
Reference in New Issue
Block a user