Simplifies Env upgrade API (#551)
* removes read_partition and partition_length from upgrade API * renames partition to bundle, also data type change from slice to Vec * removes hash from Env API * fixes comment
This commit is contained in:
28
src/env/test/upgrade_storage.rs
vendored
28
src/env/test/upgrade_storage.rs
vendored
@@ -31,9 +31,8 @@ impl BufferUpgradeStorage {
|
||||
partition: vec![0xff; PARTITION_LENGTH].into_boxed_slice(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl UpgradeStorage for BufferUpgradeStorage {
|
||||
#[cfg(test)]
|
||||
fn read_partition(&self, offset: usize, length: usize) -> StorageResult<&[u8]> {
|
||||
if length == 0 {
|
||||
return Err(StorageError::OutOfBounds);
|
||||
@@ -45,8 +44,10 @@ impl UpgradeStorage for BufferUpgradeStorage {
|
||||
Err(StorageError::OutOfBounds)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn write_partition(&mut self, offset: usize, data: &[u8]) -> StorageResult<()> {
|
||||
impl UpgradeStorage for BufferUpgradeStorage {
|
||||
fn write_bundle(&mut self, offset: usize, data: Vec<u8>) -> StorageResult<()> {
|
||||
if offset == 0 && data.len() != METADATA_LENGTH {
|
||||
return Err(StorageError::OutOfBounds);
|
||||
}
|
||||
@@ -55,21 +56,17 @@ impl UpgradeStorage for BufferUpgradeStorage {
|
||||
}
|
||||
let partition_range = ModRange::new(0, self.partition.len());
|
||||
if partition_range.contains_range(&ModRange::new(offset, data.len())) {
|
||||
self.partition[offset..][..data.len()].copy_from_slice(data);
|
||||
self.partition[offset..][..data.len()].copy_from_slice(&data);
|
||||
Ok(())
|
||||
} else {
|
||||
Err(StorageError::OutOfBounds)
|
||||
}
|
||||
}
|
||||
|
||||
fn partition_identifier(&self) -> u32 {
|
||||
fn bundle_identifier(&self) -> u32 {
|
||||
0x60000
|
||||
}
|
||||
|
||||
fn partition_length(&self) -> usize {
|
||||
PARTITION_LENGTH
|
||||
}
|
||||
|
||||
fn running_firmware_version(&self) -> u64 {
|
||||
0
|
||||
}
|
||||
@@ -80,13 +77,13 @@ mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn read_write_partition() {
|
||||
fn read_write_bundle() {
|
||||
let mut storage = BufferUpgradeStorage::new().unwrap();
|
||||
assert_eq!(storage.read_partition(0, 2).unwrap(), &[0xFF, 0xFF]);
|
||||
assert!(storage.write_partition(1, &[0x88, 0x88]).is_ok());
|
||||
assert!(storage.write_bundle(1, vec![0x88, 0x88]).is_ok());
|
||||
assert_eq!(storage.read_partition(0, 2).unwrap(), &[0xFF, 0x88]);
|
||||
assert_eq!(
|
||||
storage.write_partition(PARTITION_LENGTH - 1, &[0x88, 0x88]),
|
||||
storage.write_bundle(PARTITION_LENGTH - 1, vec![0x88, 0x88],),
|
||||
Err(StorageError::OutOfBounds)
|
||||
);
|
||||
assert_eq!(
|
||||
@@ -98,11 +95,11 @@ mod tests {
|
||||
Err(StorageError::OutOfBounds)
|
||||
);
|
||||
assert_eq!(
|
||||
storage.write_partition(4, &[]),
|
||||
storage.write_bundle(4, vec![]),
|
||||
Err(StorageError::OutOfBounds)
|
||||
);
|
||||
assert_eq!(
|
||||
storage.write_partition(PARTITION_LENGTH + 4, &[]),
|
||||
storage.write_bundle(PARTITION_LENGTH + 4, vec![]),
|
||||
Err(StorageError::OutOfBounds)
|
||||
);
|
||||
assert_eq!(storage.read_partition(4, 0), Err(StorageError::OutOfBounds));
|
||||
@@ -115,7 +112,6 @@ mod tests {
|
||||
#[test]
|
||||
fn partition_slice() {
|
||||
let storage = BufferUpgradeStorage::new().unwrap();
|
||||
assert_eq!(storage.partition_identifier(), 0x60000);
|
||||
assert_eq!(storage.partition_length(), PARTITION_LENGTH);
|
||||
assert_eq!(storage.bundle_identifier(), 0x60000);
|
||||
}
|
||||
}
|
||||
|
||||
36
src/env/tock/storage.rs
vendored
36
src/env/tock/storage.rs
vendored
@@ -352,18 +352,7 @@ impl TockUpgradeStorage {
|
||||
}
|
||||
|
||||
impl UpgradeStorage for TockUpgradeStorage {
|
||||
fn read_partition(&self, offset: usize, length: usize) -> StorageResult<&[u8]> {
|
||||
if length == 0 {
|
||||
return Err(StorageError::OutOfBounds);
|
||||
}
|
||||
if let Some(address) = self.partition.find_address(offset, length) {
|
||||
Ok(unsafe { read_slice(address, length) })
|
||||
} else {
|
||||
Err(StorageError::OutOfBounds)
|
||||
}
|
||||
}
|
||||
|
||||
fn write_partition(&mut self, offset: usize, data: &[u8]) -> StorageResult<()> {
|
||||
fn write_bundle(&mut self, offset: usize, data: Vec<u8>) -> StorageResult<()> {
|
||||
if data.is_empty() {
|
||||
return Err(StorageError::OutOfBounds);
|
||||
}
|
||||
@@ -382,23 +371,23 @@ impl UpgradeStorage for TockUpgradeStorage {
|
||||
for address in write_range.aligned_iter(self.page_size) {
|
||||
erase_page(address, self.page_size)?;
|
||||
}
|
||||
write_slice(address, data)?;
|
||||
write_slice(address, &data)?;
|
||||
let written_slice = unsafe { read_slice(address, data.len()) };
|
||||
if written_slice != data {
|
||||
return Err(StorageError::CustomError);
|
||||
}
|
||||
// Case: Last slice is written.
|
||||
if data.len() == self.partition_length() - offset {
|
||||
if data.len() == self.partition.length() - offset {
|
||||
let metadata = unsafe { read_slice(self.metadata.start(), self.metadata.length()) };
|
||||
self.check_partition_hash(&metadata)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn partition_identifier(&self) -> u32 {
|
||||
fn bundle_identifier(&self) -> u32 {
|
||||
self.identifier
|
||||
}
|
||||
|
||||
fn partition_length(&self) -> usize {
|
||||
self.partition.length()
|
||||
}
|
||||
|
||||
fn running_firmware_version(&self) -> u64 {
|
||||
let running_metadata = unsafe {
|
||||
read_slice(
|
||||
@@ -438,7 +427,7 @@ fn check_metadata(
|
||||
}
|
||||
|
||||
let metadata_address = LittleEndian::read_u32(&metadata[METADATA_SIGN_OFFSET + 8..][..4]);
|
||||
if metadata_address != upgrade_locations.partition_identifier() {
|
||||
if metadata_address != upgrade_locations.bundle_identifier() {
|
||||
return Err(StorageError::CustomError);
|
||||
}
|
||||
|
||||
@@ -495,13 +484,8 @@ mod test {
|
||||
let mut metadata = vec![0xFF; METADATA_LEN];
|
||||
LittleEndian::write_u32(&mut metadata[METADATA_SIGN_OFFSET + 8..][..4], 0x60000);
|
||||
|
||||
let partition_length = upgrade_locations.partition_length();
|
||||
let mut signed_over_data = metadata[METADATA_SIGN_OFFSET..].to_vec();
|
||||
signed_over_data.extend(
|
||||
upgrade_locations
|
||||
.read_partition(0, partition_length)
|
||||
.unwrap(),
|
||||
);
|
||||
signed_over_data.extend(&[0xFF; 0x20000]);
|
||||
let signed_hash = Sha256::hash(&signed_over_data);
|
||||
|
||||
metadata[..32].copy_from_slice(&signed_hash);
|
||||
|
||||
Reference in New Issue
Block a user