aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRasmus Melchior Jacobsen <[email protected]>2023-05-30 13:44:12 +0200
committerRasmus Melchior Jacobsen <[email protected]>2023-05-30 13:44:12 +0200
commit551f76c70067bfa14b159a198cdb92f810f40607 (patch)
treee63d5646a933199c22a9ab7558878f124468a1e8
parentb23e40f72242a9f4759d8a6d4a924c8c9d041c67 (diff)
Remove legacy Partition type and use the one from embedded-hal
-rw-r--r--embassy-boot/boot/Cargo.toml7
-rw-r--r--embassy-boot/boot/src/lib.rs2
-rw-r--r--embassy-boot/boot/src/partition.rs144
3 files changed, 4 insertions, 149 deletions
diff --git a/embassy-boot/boot/Cargo.toml b/embassy-boot/boot/Cargo.toml
index 3fdf69c5e..415d7960f 100644
--- a/embassy-boot/boot/Cargo.toml
+++ b/embassy-boot/boot/Cargo.toml
@@ -27,9 +27,10 @@ defmt = { version = "0.3", optional = true }
27digest = "0.10" 27digest = "0.10"
28log = { version = "0.4", optional = true } 28log = { version = "0.4", optional = true }
29ed25519-dalek = { version = "1.0.1", default_features = false, features = ["u32_backend"], optional = true } 29ed25519-dalek = { version = "1.0.1", default_features = false, features = ["u32_backend"], optional = true }
30embassy-embedded-hal = { version = "0.1.0", path = "../../embassy-embedded-hal" }
30embassy-sync = { version = "0.2.0", path = "../../embassy-sync" } 31embassy-sync = { version = "0.2.0", path = "../../embassy-sync" }
31embedded-storage = "0.3.0" 32embedded-storage = "0.3.0"
32embedded-storage-async = { version = "0.4.0", optional = true} 33embedded-storage-async = { version = "0.4.0", optional = true }
33salty = { git = "https://github.com/ycrypto/salty.git", rev = "a9f17911a5024698406b75c0fac56ab5ccf6a8c7", optional = true } 34salty = { git = "https://github.com/ycrypto/salty.git", rev = "a9f17911a5024698406b75c0fac56ab5ccf6a8c7", optional = true }
34signature = { version = "1.6.4", default-features = false } 35signature = { version = "1.6.4", default-features = false }
35 36
@@ -39,7 +40,7 @@ env_logger = "0.9"
39rand = "0.7" # ed25519-dalek v1.0.1 depends on this exact version 40rand = "0.7" # ed25519-dalek v1.0.1 depends on this exact version
40futures = { version = "0.3", features = ["executor"] } 41futures = { version = "0.3", features = ["executor"] }
41sha1 = "0.10.5" 42sha1 = "0.10.5"
42embassy-embedded-hal = { version = "0.1.0", path = "../../embassy-embedded-hal" } 43critical-section = { version = "1.1.1", features = ["std"] }
43 44
44[dev-dependencies.ed25519-dalek] 45[dev-dependencies.ed25519-dalek]
45default_features = false 46default_features = false
@@ -49,7 +50,7 @@ features = ["rand", "std", "u32_backend"]
49ed25519-dalek = ["dep:ed25519-dalek", "_verify"] 50ed25519-dalek = ["dep:ed25519-dalek", "_verify"]
50ed25519-salty = ["dep:salty", "_verify"] 51ed25519-salty = ["dep:salty", "_verify"]
51 52
52nightly = ["dep:embedded-storage-async"] 53nightly = ["dep:embedded-storage-async", "embassy-embedded-hal/nightly"]
53 54
54#Internal features 55#Internal features
55_verify = [] 56_verify = []
diff --git a/embassy-boot/boot/src/lib.rs b/embassy-boot/boot/src/lib.rs
index c76087ff1..15d3a4f21 100644
--- a/embassy-boot/boot/src/lib.rs
+++ b/embassy-boot/boot/src/lib.rs
@@ -9,11 +9,9 @@ mod digest_adapters;
9mod firmware_updater; 9mod firmware_updater;
10#[cfg(test)] 10#[cfg(test)]
11mod mem_flash; 11mod mem_flash;
12mod partition;
13#[cfg(test)] 12#[cfg(test)]
14mod test_flash; 13mod test_flash;
15 14
16pub use partition::Partition;
17// The expected value of the flash after an erase 15// The expected value of the flash after an erase
18// TODO: Use the value provided by NorFlash when available 16// TODO: Use the value provided by NorFlash when available
19pub(crate) const STATE_ERASE_VALUE: u8 = 0xFF; 17pub(crate) const STATE_ERASE_VALUE: u8 = 0xFF;
diff --git a/embassy-boot/boot/src/partition.rs b/embassy-boot/boot/src/partition.rs
deleted file mode 100644
index 7b56a8240..000000000
--- a/embassy-boot/boot/src/partition.rs
+++ /dev/null
@@ -1,144 +0,0 @@
1use embedded_storage::nor_flash::{NorFlash, ReadNorFlash};
2#[cfg(feature = "nightly")]
3use embedded_storage_async::nor_flash::{NorFlash as AsyncNorFlash, ReadNorFlash as AsyncReadNorFlash};
4
5/// A region in flash used by the bootloader.
6#[derive(Copy, Clone, Debug)]
7#[cfg_attr(feature = "defmt", derive(defmt::Format))]
8pub struct Partition {
9 /// The offset into the flash where the partition starts.
10 pub from: u32,
11 /// The offset into the flash where the partition ends.
12 pub to: u32,
13}
14
15impl Partition {
16 /// Create a new partition with the provided range
17 pub const fn new(from: u32, to: u32) -> Self {
18 Self { from, to }
19 }
20
21 /// Return the size of the partition
22 pub const fn size(&self) -> u32 {
23 self.to - self.from
24 }
25
26 /// Read from the partition on the provided flash
27 #[cfg(feature = "nightly")]
28 pub async fn read<F: AsyncReadNorFlash>(
29 &self,
30 flash: &mut F,
31 offset: u32,
32 bytes: &mut [u8],
33 ) -> Result<(), F::Error> {
34 let offset = self.from as u32 + offset;
35 flash.read(offset, bytes).await
36 }
37
38 /// Write to the partition on the provided flash
39 #[cfg(feature = "nightly")]
40 pub async fn write<F: AsyncNorFlash>(&self, flash: &mut F, offset: u32, bytes: &[u8]) -> Result<(), F::Error> {
41 let offset = self.from as u32 + offset;
42 flash.write(offset, bytes).await?;
43 trace!("Wrote from 0x{:x} len {}", offset, bytes.len());
44 Ok(())
45 }
46
47 /// Erase part of the partition on the provided flash
48 #[cfg(feature = "nightly")]
49 pub async fn erase<F: AsyncNorFlash>(&self, flash: &mut F, from: u32, to: u32) -> Result<(), F::Error> {
50 let from = self.from as u32 + from;
51 let to = self.from as u32 + to;
52 flash.erase(from, to).await?;
53 trace!("Erased from 0x{:x} to 0x{:x}", from, to);
54 Ok(())
55 }
56
57 /// Erase the entire partition
58 #[cfg(feature = "nightly")]
59 pub(crate) async fn wipe<F: AsyncNorFlash>(&self, flash: &mut F) -> Result<(), F::Error> {
60 let from = self.from as u32;
61 let to = self.to as u32;
62 flash.erase(from, to).await?;
63 trace!("Wiped from 0x{:x} to 0x{:x}", from, to);
64 Ok(())
65 }
66
67 /// Read from the partition on the provided flash
68 pub fn read_blocking<F: ReadNorFlash>(&self, flash: &mut F, offset: u32, bytes: &mut [u8]) -> Result<(), F::Error> {
69 let offset = self.from as u32 + offset;
70 flash.read(offset, bytes)
71 }
72
73 /// Write to the partition on the provided flash
74 pub fn write_blocking<F: NorFlash>(&self, flash: &mut F, offset: u32, bytes: &[u8]) -> Result<(), F::Error> {
75 let offset = self.from as u32 + offset;
76 flash.write(offset, bytes)?;
77 trace!("Wrote from 0x{:x} len {}", offset, bytes.len());
78 Ok(())
79 }
80
81 /// Erase part of the partition on the provided flash
82 pub fn erase_blocking<F: NorFlash>(&self, flash: &mut F, from: u32, to: u32) -> Result<(), F::Error> {
83 let from = self.from as u32 + from;
84 let to = self.from as u32 + to;
85 flash.erase(from, to)?;
86 trace!("Erased from 0x{:x} to 0x{:x}", from, to);
87 Ok(())
88 }
89
90 /// Erase the entire partition
91 pub(crate) fn wipe_blocking<F: NorFlash>(&self, flash: &mut F) -> Result<(), F::Error> {
92 let from = self.from as u32;
93 let to = self.to as u32;
94 flash.erase(from, to)?;
95 trace!("Wiped from 0x{:x} to 0x{:x}", from, to);
96 Ok(())
97 }
98}
99
100#[cfg(test)]
101mod tests {
102 use crate::mem_flash::MemFlash;
103 use crate::Partition;
104
105 #[test]
106 fn can_erase() {
107 let mut flash = MemFlash::<1024, 64, 4>::new(0x00);
108 let partition = Partition::new(256, 512);
109
110 partition.erase_blocking(&mut flash, 64, 192).unwrap();
111
112 for (index, byte) in flash.mem.iter().copied().enumerate().take(256 + 64) {
113 assert_eq!(0x00, byte, "Index {}", index);
114 }
115
116 for (index, byte) in flash.mem.iter().copied().enumerate().skip(256 + 64).take(128) {
117 assert_eq!(0xFF, byte, "Index {}", index);
118 }
119
120 for (index, byte) in flash.mem.iter().copied().enumerate().skip(256 + 64 + 128) {
121 assert_eq!(0x00, byte, "Index {}", index);
122 }
123 }
124
125 #[test]
126 fn can_wipe() {
127 let mut flash = MemFlash::<1024, 64, 4>::new(0x00);
128 let partition = Partition::new(256, 512);
129
130 partition.wipe_blocking(&mut flash).unwrap();
131
132 for (index, byte) in flash.mem.iter().copied().enumerate().take(256) {
133 assert_eq!(0x00, byte, "Index {}", index);
134 }
135
136 for (index, byte) in flash.mem.iter().copied().enumerate().skip(256).take(256) {
137 assert_eq!(0xFF, byte, "Index {}", index);
138 }
139
140 for (index, byte) in flash.mem.iter().copied().enumerate().skip(512) {
141 assert_eq!(0x00, byte, "Index {}", index);
142 }
143 }
144}