aboutsummaryrefslogtreecommitdiff
path: root/embassy-boot/boot/src
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 /embassy-boot/boot/src
parentb23e40f72242a9f4759d8a6d4a924c8c9d041c67 (diff)
Remove legacy Partition type and use the one from embedded-hal
Diffstat (limited to 'embassy-boot/boot/src')
-rw-r--r--embassy-boot/boot/src/lib.rs2
-rw-r--r--embassy-boot/boot/src/partition.rs144
2 files changed, 0 insertions, 146 deletions
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}