diff options
Diffstat (limited to 'embassy-boot/boot/src/boot_loader.rs')
| -rw-r--r-- | embassy-boot/boot/src/boot_loader.rs | 452 |
1 files changed, 170 insertions, 282 deletions
diff --git a/embassy-boot/boot/src/boot_loader.rs b/embassy-boot/boot/src/boot_loader.rs index b959de2c4..a8c19197b 100644 --- a/embassy-boot/boot/src/boot_loader.rs +++ b/embassy-boot/boot/src/boot_loader.rs | |||
| @@ -1,6 +1,11 @@ | |||
| 1 | use embedded_storage::nor_flash::{ErrorType, NorFlash, NorFlashError, NorFlashErrorKind, ReadNorFlash}; | 1 | use core::cell::RefCell; |
| 2 | 2 | ||
| 3 | use crate::{Partition, State, BOOT_MAGIC, SWAP_MAGIC}; | 3 | use embassy_embedded_hal::flash::partition::BlockingPartition; |
| 4 | use embassy_sync::blocking_mutex::raw::NoopRawMutex; | ||
| 5 | use embassy_sync::blocking_mutex::Mutex; | ||
| 6 | use embedded_storage::nor_flash::{NorFlash, NorFlashError, NorFlashErrorKind}; | ||
| 7 | |||
| 8 | use crate::{State, BOOT_MAGIC, STATE_ERASE_VALUE, SWAP_MAGIC}; | ||
| 4 | 9 | ||
| 5 | /// Errors returned by bootloader | 10 | /// Errors returned by bootloader |
| 6 | #[derive(PartialEq, Eq, Debug)] | 11 | #[derive(PartialEq, Eq, Debug)] |
| @@ -30,63 +35,96 @@ where | |||
| 30 | } | 35 | } |
| 31 | } | 36 | } |
| 32 | 37 | ||
| 33 | /// Trait defining the flash handles used for active and DFU partition. | 38 | /// Bootloader flash configuration holding the three flashes used by the bootloader |
| 34 | pub trait FlashConfig { | 39 | /// |
| 35 | /// The erase value of the state flash. Typically the default of 0xFF is used, but some flashes use a different value. | 40 | /// If only a single flash is actually used, then that flash should be partitioned into three partitions before use. |
| 36 | const STATE_ERASE_VALUE: u8 = 0xFF; | 41 | /// The easiest way to do this is to use [`BootLoaderConfig::from_linkerfile_blocking`] which will partition |
| 42 | /// the provided flash according to symbols defined in the linkerfile. | ||
| 43 | pub struct BootLoaderConfig<ACTIVE, DFU, STATE> { | ||
| 44 | /// Flash type used for the active partition - the partition which will be booted from. | ||
| 45 | pub active: ACTIVE, | ||
| 46 | /// Flash type used for the dfu partition - the partition which will be swapped in when requested. | ||
| 47 | pub dfu: DFU, | ||
| 37 | /// Flash type used for the state partition. | 48 | /// Flash type used for the state partition. |
| 38 | type STATE: NorFlash; | 49 | pub state: STATE, |
| 39 | /// Flash type used for the active partition. | ||
| 40 | type ACTIVE: NorFlash; | ||
| 41 | /// Flash type used for the dfu partition. | ||
| 42 | type DFU: NorFlash; | ||
| 43 | |||
| 44 | /// Return flash instance used to write/read to/from active partition. | ||
| 45 | fn active(&mut self) -> &mut Self::ACTIVE; | ||
| 46 | /// Return flash instance used to write/read to/from dfu partition. | ||
| 47 | fn dfu(&mut self) -> &mut Self::DFU; | ||
| 48 | /// Return flash instance used to write/read to/from bootloader state. | ||
| 49 | fn state(&mut self) -> &mut Self::STATE; | ||
| 50 | } | 50 | } |
| 51 | 51 | ||
| 52 | trait FlashConfigEx { | 52 | impl<'a, FLASH: NorFlash> |
| 53 | fn page_size() -> u32; | 53 | BootLoaderConfig< |
| 54 | } | 54 | BlockingPartition<'a, NoopRawMutex, FLASH>, |
| 55 | BlockingPartition<'a, NoopRawMutex, FLASH>, | ||
| 56 | BlockingPartition<'a, NoopRawMutex, FLASH>, | ||
| 57 | > | ||
| 58 | { | ||
| 59 | /// Create a bootloader config from the flash and address symbols defined in the linkerfile | ||
| 60 | // #[cfg(target_os = "none")] | ||
| 61 | pub fn from_linkerfile_blocking(flash: &'a Mutex<NoopRawMutex, RefCell<FLASH>>) -> Self { | ||
| 62 | extern "C" { | ||
| 63 | static __bootloader_state_start: u32; | ||
| 64 | static __bootloader_state_end: u32; | ||
| 65 | static __bootloader_active_start: u32; | ||
| 66 | static __bootloader_active_end: u32; | ||
| 67 | static __bootloader_dfu_start: u32; | ||
| 68 | static __bootloader_dfu_end: u32; | ||
| 69 | } | ||
| 55 | 70 | ||
| 56 | impl<T: FlashConfig> FlashConfigEx for T { | 71 | let active = unsafe { |
| 57 | /// Get the page size which is the "unit of operation" within the bootloader. | 72 | let start = &__bootloader_active_start as *const u32 as u32; |
| 58 | fn page_size() -> u32 { | 73 | let end = &__bootloader_active_end as *const u32 as u32; |
| 59 | core::cmp::max(T::ACTIVE::ERASE_SIZE, T::DFU::ERASE_SIZE) as u32 | 74 | trace!("ACTIVE: 0x{:x} - 0x{:x}", start, end); |
| 75 | |||
| 76 | BlockingPartition::new(flash, start, end - start) | ||
| 77 | }; | ||
| 78 | let dfu = unsafe { | ||
| 79 | let start = &__bootloader_dfu_start as *const u32 as u32; | ||
| 80 | let end = &__bootloader_dfu_end as *const u32 as u32; | ||
| 81 | trace!("DFU: 0x{:x} - 0x{:x}", start, end); | ||
| 82 | |||
| 83 | BlockingPartition::new(flash, start, end - start) | ||
| 84 | }; | ||
| 85 | let state = unsafe { | ||
| 86 | let start = &__bootloader_state_start as *const u32 as u32; | ||
| 87 | let end = &__bootloader_state_end as *const u32 as u32; | ||
| 88 | trace!("STATE: 0x{:x} - 0x{:x}", start, end); | ||
| 89 | |||
| 90 | BlockingPartition::new(flash, start, end - start) | ||
| 91 | }; | ||
| 92 | |||
| 93 | Self { active, dfu, state } | ||
| 60 | } | 94 | } |
| 61 | } | 95 | } |
| 62 | 96 | ||
| 63 | /// BootLoader works with any flash implementing embedded_storage. | 97 | /// BootLoader works with any flash implementing embedded_storage. |
| 64 | pub struct BootLoader { | 98 | pub struct BootLoader<ACTIVE: NorFlash, DFU: NorFlash, STATE: NorFlash> { |
| 65 | // Page with current state of bootloader. The state partition has the following format: | 99 | active: ACTIVE, |
| 66 | // All ranges are in multiples of WRITE_SIZE bytes. | 100 | dfu: DFU, |
| 67 | // | Range | Description | | 101 | /// The state partition has the following format: |
| 68 | // | 0..1 | Magic indicating bootloader state. BOOT_MAGIC means boot, SWAP_MAGIC means swap. | | 102 | /// All ranges are in multiples of WRITE_SIZE bytes. |
| 69 | // | 1..2 | Progress validity. ERASE_VALUE means valid, !ERASE_VALUE means invalid. | | 103 | /// | Range | Description | |
| 70 | // | 2..2 + N | Progress index used while swapping or reverting | | 104 | /// | 0..1 | Magic indicating bootloader state. BOOT_MAGIC means boot, SWAP_MAGIC means swap. | |
| 71 | state: Partition, | 105 | /// | 1..2 | Progress validity. ERASE_VALUE means valid, !ERASE_VALUE means invalid. | |
| 72 | // Location of the partition which will be booted from | 106 | /// | 2..2 + N | Progress index used while swapping or reverting |
| 73 | active: Partition, | 107 | state: STATE, |
| 74 | // Location of the partition which will be swapped in when requested | ||
| 75 | dfu: Partition, | ||
| 76 | } | 108 | } |
| 77 | 109 | ||
| 78 | impl BootLoader { | 110 | impl<ACTIVE: NorFlash, DFU: NorFlash, STATE: NorFlash> BootLoader<ACTIVE, DFU, STATE> { |
| 79 | /// Create a new instance of a bootloader with the given partitions. | 111 | /// Get the page size which is the "unit of operation" within the bootloader. |
| 112 | const PAGE_SIZE: u32 = if ACTIVE::ERASE_SIZE > DFU::ERASE_SIZE { | ||
| 113 | ACTIVE::ERASE_SIZE as u32 | ||
| 114 | } else { | ||
| 115 | DFU::ERASE_SIZE as u32 | ||
| 116 | }; | ||
| 117 | |||
| 118 | /// Create a new instance of a bootloader with the flash partitions. | ||
| 80 | /// | 119 | /// |
| 81 | /// - All partitions must be aligned with the PAGE_SIZE const generic parameter. | 120 | /// - All partitions must be aligned with the PAGE_SIZE const generic parameter. |
| 82 | /// - The dfu partition must be at least PAGE_SIZE bigger than the active partition. | 121 | /// - The dfu partition must be at least PAGE_SIZE bigger than the active partition. |
| 83 | pub fn new(active: Partition, dfu: Partition, state: Partition) -> Self { | 122 | pub fn new(config: BootLoaderConfig<ACTIVE, DFU, STATE>) -> Self { |
| 84 | Self { active, dfu, state } | 123 | Self { |
| 85 | } | 124 | active: config.active, |
| 86 | 125 | dfu: config.dfu, | |
| 87 | /// Return the offset of the active partition into the active flash. | 126 | state: config.state, |
| 88 | pub fn boot_address(&self) -> usize { | 127 | } |
| 89 | self.active.from as usize | ||
| 90 | } | 128 | } |
| 91 | 129 | ||
| 92 | /// Perform necessary boot preparations like swapping images. | 130 | /// Perform necessary boot preparations like swapping images. |
| @@ -175,195 +213,174 @@ impl BootLoader { | |||
| 175 | /// | DFU | 3 | 3 | 2 | 1 | 3 | | 213 | /// | DFU | 3 | 3 | 2 | 1 | 3 | |
| 176 | /// +-----------+--------------+--------+--------+--------+--------+ | 214 | /// +-----------+--------------+--------+--------+--------+--------+ |
| 177 | /// | 215 | /// |
| 178 | pub fn prepare_boot<P: FlashConfig>(&mut self, p: &mut P, aligned_buf: &mut [u8]) -> Result<State, BootError> { | 216 | pub fn prepare_boot(&mut self, aligned_buf: &mut [u8]) -> Result<State, BootError> { |
| 179 | // Ensure we have enough progress pages to store copy progress | 217 | // Ensure we have enough progress pages to store copy progress |
| 180 | assert_eq!(0, P::page_size() % aligned_buf.len() as u32); | 218 | assert_eq!(0, Self::PAGE_SIZE % aligned_buf.len() as u32); |
| 181 | assert_eq!(0, P::page_size() % P::ACTIVE::WRITE_SIZE as u32); | 219 | assert_eq!(0, Self::PAGE_SIZE % ACTIVE::WRITE_SIZE as u32); |
| 182 | assert_eq!(0, P::page_size() % P::ACTIVE::ERASE_SIZE as u32); | 220 | assert_eq!(0, Self::PAGE_SIZE % ACTIVE::ERASE_SIZE as u32); |
| 183 | assert_eq!(0, P::page_size() % P::DFU::WRITE_SIZE as u32); | 221 | assert_eq!(0, Self::PAGE_SIZE % DFU::WRITE_SIZE as u32); |
| 184 | assert_eq!(0, P::page_size() % P::DFU::ERASE_SIZE as u32); | 222 | assert_eq!(0, Self::PAGE_SIZE % DFU::ERASE_SIZE as u32); |
| 185 | assert!(aligned_buf.len() >= P::STATE::WRITE_SIZE); | 223 | assert!(aligned_buf.len() >= STATE::WRITE_SIZE); |
| 186 | assert_eq!(0, aligned_buf.len() % P::ACTIVE::WRITE_SIZE); | 224 | assert_eq!(0, aligned_buf.len() % ACTIVE::WRITE_SIZE); |
| 187 | assert_eq!(0, aligned_buf.len() % P::DFU::WRITE_SIZE); | 225 | assert_eq!(0, aligned_buf.len() % DFU::WRITE_SIZE); |
| 188 | assert_partitions(self.active, self.dfu, self.state, P::page_size(), P::STATE::WRITE_SIZE); | 226 | |
| 227 | assert_partitions(&self.active, &self.dfu, &self.state, Self::PAGE_SIZE); | ||
| 189 | 228 | ||
| 190 | // Copy contents from partition N to active | 229 | // Copy contents from partition N to active |
| 191 | let state = self.read_state(p, aligned_buf)?; | 230 | let state = self.read_state(aligned_buf)?; |
| 192 | if state == State::Swap { | 231 | if state == State::Swap { |
| 193 | // | 232 | // |
| 194 | // Check if we already swapped. If we're in the swap state, this means we should revert | 233 | // Check if we already swapped. If we're in the swap state, this means we should revert |
| 195 | // since the app has failed to mark boot as successful | 234 | // since the app has failed to mark boot as successful |
| 196 | // | 235 | // |
| 197 | if !self.is_swapped(p, aligned_buf)? { | 236 | if !self.is_swapped(aligned_buf)? { |
| 198 | trace!("Swapping"); | 237 | trace!("Swapping"); |
| 199 | self.swap(p, aligned_buf)?; | 238 | self.swap(aligned_buf)?; |
| 200 | trace!("Swapping done"); | 239 | trace!("Swapping done"); |
| 201 | } else { | 240 | } else { |
| 202 | trace!("Reverting"); | 241 | trace!("Reverting"); |
| 203 | self.revert(p, aligned_buf)?; | 242 | self.revert(aligned_buf)?; |
| 204 | 243 | ||
| 205 | let state_flash = p.state(); | 244 | let state_word = &mut aligned_buf[..STATE::WRITE_SIZE]; |
| 206 | let state_word = &mut aligned_buf[..P::STATE::WRITE_SIZE]; | ||
| 207 | 245 | ||
| 208 | // Invalidate progress | 246 | // Invalidate progress |
| 209 | state_word.fill(!P::STATE_ERASE_VALUE); | 247 | state_word.fill(!STATE_ERASE_VALUE); |
| 210 | self.state | 248 | self.state.write(STATE::WRITE_SIZE as u32, state_word)?; |
| 211 | .write_blocking(state_flash, P::STATE::WRITE_SIZE as u32, state_word)?; | ||
| 212 | 249 | ||
| 213 | // Clear magic and progress | 250 | // Clear magic and progress |
| 214 | self.state.wipe_blocking(state_flash)?; | 251 | self.state.erase(0, self.state.capacity() as u32)?; |
| 215 | 252 | ||
| 216 | // Set magic | 253 | // Set magic |
| 217 | state_word.fill(BOOT_MAGIC); | 254 | state_word.fill(BOOT_MAGIC); |
| 218 | self.state.write_blocking(state_flash, 0, state_word)?; | 255 | self.state.write(0, state_word)?; |
| 219 | } | 256 | } |
| 220 | } | 257 | } |
| 221 | Ok(state) | 258 | Ok(state) |
| 222 | } | 259 | } |
| 223 | 260 | ||
| 224 | fn is_swapped<P: FlashConfig>(&mut self, p: &mut P, aligned_buf: &mut [u8]) -> Result<bool, BootError> { | 261 | fn is_swapped(&mut self, aligned_buf: &mut [u8]) -> Result<bool, BootError> { |
| 225 | let page_count = (self.active.size() / P::page_size()) as usize; | 262 | let page_count = self.active.capacity() / Self::PAGE_SIZE as usize; |
| 226 | let progress = self.current_progress(p, aligned_buf)?; | 263 | let progress = self.current_progress(aligned_buf)?; |
| 227 | 264 | ||
| 228 | Ok(progress >= page_count * 2) | 265 | Ok(progress >= page_count * 2) |
| 229 | } | 266 | } |
| 230 | 267 | ||
| 231 | fn current_progress<P: FlashConfig>(&mut self, config: &mut P, aligned_buf: &mut [u8]) -> Result<usize, BootError> { | 268 | fn current_progress(&mut self, aligned_buf: &mut [u8]) -> Result<usize, BootError> { |
| 232 | let write_size = P::STATE::WRITE_SIZE as u32; | 269 | let write_size = STATE::WRITE_SIZE as u32; |
| 233 | let max_index = (((self.state.size() - write_size) / write_size) - 2) as usize; | 270 | let max_index = ((self.state.capacity() - STATE::WRITE_SIZE) / STATE::WRITE_SIZE) - 2; |
| 234 | let state_flash = config.state(); | ||
| 235 | let state_word = &mut aligned_buf[..write_size as usize]; | 271 | let state_word = &mut aligned_buf[..write_size as usize]; |
| 236 | 272 | ||
| 237 | self.state.read_blocking(state_flash, write_size, state_word)?; | 273 | self.state.read(write_size, state_word)?; |
| 238 | if state_word.iter().any(|&b| b != P::STATE_ERASE_VALUE) { | 274 | if state_word.iter().any(|&b| b != STATE_ERASE_VALUE) { |
| 239 | // Progress is invalid | 275 | // Progress is invalid |
| 240 | return Ok(max_index); | 276 | return Ok(max_index); |
| 241 | } | 277 | } |
| 242 | 278 | ||
| 243 | for index in 0..max_index { | 279 | for index in 0..max_index { |
| 244 | self.state | 280 | self.state.read((2 + index) as u32 * write_size, state_word)?; |
| 245 | .read_blocking(state_flash, (2 + index) as u32 * write_size, state_word)?; | ||
| 246 | 281 | ||
| 247 | if state_word.iter().any(|&b| b == P::STATE_ERASE_VALUE) { | 282 | if state_word.iter().any(|&b| b == STATE_ERASE_VALUE) { |
| 248 | return Ok(index); | 283 | return Ok(index); |
| 249 | } | 284 | } |
| 250 | } | 285 | } |
| 251 | Ok(max_index) | 286 | Ok(max_index) |
| 252 | } | 287 | } |
| 253 | 288 | ||
| 254 | fn update_progress<P: FlashConfig>( | 289 | fn update_progress(&mut self, progress_index: usize, aligned_buf: &mut [u8]) -> Result<(), BootError> { |
| 255 | &mut self, | 290 | let state_word = &mut aligned_buf[..STATE::WRITE_SIZE]; |
| 256 | progress_index: usize, | 291 | state_word.fill(!STATE_ERASE_VALUE); |
| 257 | p: &mut P, | 292 | self.state |
| 258 | aligned_buf: &mut [u8], | 293 | .write((2 + progress_index) as u32 * STATE::WRITE_SIZE as u32, state_word)?; |
| 259 | ) -> Result<(), BootError> { | ||
| 260 | let state_word = &mut aligned_buf[..P::STATE::WRITE_SIZE]; | ||
| 261 | state_word.fill(!P::STATE_ERASE_VALUE); | ||
| 262 | self.state.write_blocking( | ||
| 263 | p.state(), | ||
| 264 | (2 + progress_index) as u32 * P::STATE::WRITE_SIZE as u32, | ||
| 265 | state_word, | ||
| 266 | )?; | ||
| 267 | Ok(()) | 294 | Ok(()) |
| 268 | } | 295 | } |
| 269 | 296 | ||
| 270 | fn copy_page_once_to_active<P: FlashConfig>( | 297 | fn copy_page_once_to_active( |
| 271 | &mut self, | 298 | &mut self, |
| 272 | progress_index: usize, | 299 | progress_index: usize, |
| 273 | from_offset: u32, | 300 | from_offset: u32, |
| 274 | to_offset: u32, | 301 | to_offset: u32, |
| 275 | p: &mut P, | ||
| 276 | aligned_buf: &mut [u8], | 302 | aligned_buf: &mut [u8], |
| 277 | ) -> Result<(), BootError> { | 303 | ) -> Result<(), BootError> { |
| 278 | if self.current_progress(p, aligned_buf)? <= progress_index { | 304 | if self.current_progress(aligned_buf)? <= progress_index { |
| 279 | let page_size = P::page_size() as u32; | 305 | let page_size = Self::PAGE_SIZE as u32; |
| 280 | 306 | ||
| 281 | self.active | 307 | self.active.erase(to_offset, to_offset + page_size)?; |
| 282 | .erase_blocking(p.active(), to_offset, to_offset + page_size)?; | ||
| 283 | 308 | ||
| 284 | for offset_in_page in (0..page_size).step_by(aligned_buf.len()) { | 309 | for offset_in_page in (0..page_size).step_by(aligned_buf.len()) { |
| 285 | self.dfu | 310 | self.dfu.read(from_offset + offset_in_page as u32, aligned_buf)?; |
| 286 | .read_blocking(p.dfu(), from_offset + offset_in_page as u32, aligned_buf)?; | 311 | self.active.write(to_offset + offset_in_page as u32, aligned_buf)?; |
| 287 | self.active | ||
| 288 | .write_blocking(p.active(), to_offset + offset_in_page as u32, aligned_buf)?; | ||
| 289 | } | 312 | } |
| 290 | 313 | ||
| 291 | self.update_progress(progress_index, p, aligned_buf)?; | 314 | self.update_progress(progress_index, aligned_buf)?; |
| 292 | } | 315 | } |
| 293 | Ok(()) | 316 | Ok(()) |
| 294 | } | 317 | } |
| 295 | 318 | ||
| 296 | fn copy_page_once_to_dfu<P: FlashConfig>( | 319 | fn copy_page_once_to_dfu( |
| 297 | &mut self, | 320 | &mut self, |
| 298 | progress_index: usize, | 321 | progress_index: usize, |
| 299 | from_offset: u32, | 322 | from_offset: u32, |
| 300 | to_offset: u32, | 323 | to_offset: u32, |
| 301 | p: &mut P, | ||
| 302 | aligned_buf: &mut [u8], | 324 | aligned_buf: &mut [u8], |
| 303 | ) -> Result<(), BootError> { | 325 | ) -> Result<(), BootError> { |
| 304 | if self.current_progress(p, aligned_buf)? <= progress_index { | 326 | if self.current_progress(aligned_buf)? <= progress_index { |
| 305 | let page_size = P::page_size() as u32; | 327 | let page_size = Self::PAGE_SIZE as u32; |
| 306 | 328 | ||
| 307 | self.dfu | 329 | self.dfu.erase(to_offset as u32, to_offset + page_size)?; |
| 308 | .erase_blocking(p.dfu(), to_offset as u32, to_offset + page_size)?; | ||
| 309 | 330 | ||
| 310 | for offset_in_page in (0..page_size).step_by(aligned_buf.len()) { | 331 | for offset_in_page in (0..page_size).step_by(aligned_buf.len()) { |
| 311 | self.active | 332 | self.active.read(from_offset + offset_in_page as u32, aligned_buf)?; |
| 312 | .read_blocking(p.active(), from_offset + offset_in_page as u32, aligned_buf)?; | 333 | self.dfu.write(to_offset + offset_in_page as u32, aligned_buf)?; |
| 313 | self.dfu | ||
| 314 | .write_blocking(p.dfu(), to_offset + offset_in_page as u32, aligned_buf)?; | ||
| 315 | } | 334 | } |
| 316 | 335 | ||
| 317 | self.update_progress(progress_index, p, aligned_buf)?; | 336 | self.update_progress(progress_index, aligned_buf)?; |
| 318 | } | 337 | } |
| 319 | Ok(()) | 338 | Ok(()) |
| 320 | } | 339 | } |
| 321 | 340 | ||
| 322 | fn swap<P: FlashConfig>(&mut self, p: &mut P, aligned_buf: &mut [u8]) -> Result<(), BootError> { | 341 | fn swap(&mut self, aligned_buf: &mut [u8]) -> Result<(), BootError> { |
| 323 | let page_size = P::page_size(); | 342 | let page_count = self.active.capacity() as u32 / Self::PAGE_SIZE; |
| 324 | let page_count = self.active.size() / page_size; | ||
| 325 | for page_num in 0..page_count { | 343 | for page_num in 0..page_count { |
| 326 | let progress_index = (page_num * 2) as usize; | 344 | let progress_index = (page_num * 2) as usize; |
| 327 | 345 | ||
| 328 | // Copy active page to the 'next' DFU page. | 346 | // Copy active page to the 'next' DFU page. |
| 329 | let active_from_offset = (page_count - 1 - page_num) * page_size; | 347 | let active_from_offset = (page_count - 1 - page_num) * Self::PAGE_SIZE; |
| 330 | let dfu_to_offset = (page_count - page_num) * page_size; | 348 | let dfu_to_offset = (page_count - page_num) * Self::PAGE_SIZE; |
| 331 | //trace!("Copy active {} to dfu {}", active_from_offset, dfu_to_offset); | 349 | //trace!("Copy active {} to dfu {}", active_from_offset, dfu_to_offset); |
| 332 | self.copy_page_once_to_dfu(progress_index, active_from_offset, dfu_to_offset, p, aligned_buf)?; | 350 | self.copy_page_once_to_dfu(progress_index, active_from_offset, dfu_to_offset, aligned_buf)?; |
| 333 | 351 | ||
| 334 | // Copy DFU page to the active page | 352 | // Copy DFU page to the active page |
| 335 | let active_to_offset = (page_count - 1 - page_num) * page_size; | 353 | let active_to_offset = (page_count - 1 - page_num) * Self::PAGE_SIZE; |
| 336 | let dfu_from_offset = (page_count - 1 - page_num) * page_size; | 354 | let dfu_from_offset = (page_count - 1 - page_num) * Self::PAGE_SIZE; |
| 337 | //trace!("Copy dfy {} to active {}", dfu_from_offset, active_to_offset); | 355 | //trace!("Copy dfy {} to active {}", dfu_from_offset, active_to_offset); |
| 338 | self.copy_page_once_to_active(progress_index + 1, dfu_from_offset, active_to_offset, p, aligned_buf)?; | 356 | self.copy_page_once_to_active(progress_index + 1, dfu_from_offset, active_to_offset, aligned_buf)?; |
| 339 | } | 357 | } |
| 340 | 358 | ||
| 341 | Ok(()) | 359 | Ok(()) |
| 342 | } | 360 | } |
| 343 | 361 | ||
| 344 | fn revert<P: FlashConfig>(&mut self, p: &mut P, aligned_buf: &mut [u8]) -> Result<(), BootError> { | 362 | fn revert(&mut self, aligned_buf: &mut [u8]) -> Result<(), BootError> { |
| 345 | let page_size = P::page_size(); | 363 | let page_count = self.active.capacity() as u32 / Self::PAGE_SIZE; |
| 346 | let page_count = self.active.size() / page_size; | ||
| 347 | for page_num in 0..page_count { | 364 | for page_num in 0..page_count { |
| 348 | let progress_index = (page_count * 2 + page_num * 2) as usize; | 365 | let progress_index = (page_count * 2 + page_num * 2) as usize; |
| 349 | 366 | ||
| 350 | // Copy the bad active page to the DFU page | 367 | // Copy the bad active page to the DFU page |
| 351 | let active_from_offset = page_num * page_size; | 368 | let active_from_offset = page_num * Self::PAGE_SIZE; |
| 352 | let dfu_to_offset = page_num * page_size; | 369 | let dfu_to_offset = page_num * Self::PAGE_SIZE; |
| 353 | self.copy_page_once_to_dfu(progress_index, active_from_offset, dfu_to_offset, p, aligned_buf)?; | 370 | self.copy_page_once_to_dfu(progress_index, active_from_offset, dfu_to_offset, aligned_buf)?; |
| 354 | 371 | ||
| 355 | // Copy the DFU page back to the active page | 372 | // Copy the DFU page back to the active page |
| 356 | let active_to_offset = page_num * page_size; | 373 | let active_to_offset = page_num * Self::PAGE_SIZE; |
| 357 | let dfu_from_offset = (page_num + 1) * page_size; | 374 | let dfu_from_offset = (page_num + 1) * Self::PAGE_SIZE; |
| 358 | self.copy_page_once_to_active(progress_index + 1, dfu_from_offset, active_to_offset, p, aligned_buf)?; | 375 | self.copy_page_once_to_active(progress_index + 1, dfu_from_offset, active_to_offset, aligned_buf)?; |
| 359 | } | 376 | } |
| 360 | 377 | ||
| 361 | Ok(()) | 378 | Ok(()) |
| 362 | } | 379 | } |
| 363 | 380 | ||
| 364 | fn read_state<P: FlashConfig>(&mut self, config: &mut P, aligned_buf: &mut [u8]) -> Result<State, BootError> { | 381 | fn read_state(&mut self, aligned_buf: &mut [u8]) -> Result<State, BootError> { |
| 365 | let state_word = &mut aligned_buf[..P::STATE::WRITE_SIZE]; | 382 | let state_word = &mut aligned_buf[..STATE::WRITE_SIZE]; |
| 366 | self.state.read_blocking(config.state(), 0, state_word)?; | 383 | self.state.read(0, state_word)?; |
| 367 | 384 | ||
| 368 | if !state_word.iter().any(|&b| b != SWAP_MAGIC) { | 385 | if !state_word.iter().any(|&b| b != SWAP_MAGIC) { |
| 369 | Ok(State::Swap) | 386 | Ok(State::Swap) |
| @@ -373,161 +390,32 @@ impl BootLoader { | |||
| 373 | } | 390 | } |
| 374 | } | 391 | } |
| 375 | 392 | ||
| 376 | fn assert_partitions(active: Partition, dfu: Partition, state: Partition, page_size: u32, state_write_size: usize) { | 393 | fn assert_partitions<ACTIVE: NorFlash, DFU: NorFlash, STATE: NorFlash>( |
| 377 | assert_eq!(active.size() % page_size, 0); | 394 | active: &ACTIVE, |
| 378 | assert_eq!(dfu.size() % page_size, 0); | 395 | dfu: &DFU, |
| 379 | assert!(dfu.size() - active.size() >= page_size); | 396 | state: &STATE, |
| 380 | assert!(2 + 2 * (active.size() / page_size) <= state.size() / state_write_size as u32); | 397 | page_size: u32, |
| 381 | } | 398 | ) { |
| 382 | 399 | assert_eq!(active.capacity() as u32 % page_size, 0); | |
| 383 | /// A flash wrapper implementing the Flash and embedded_storage traits. | 400 | assert_eq!(dfu.capacity() as u32 % page_size, 0); |
| 384 | pub struct BootFlash<F> | 401 | assert!(dfu.capacity() as u32 - active.capacity() as u32 >= page_size); |
| 385 | where | 402 | assert!(2 + 2 * (active.capacity() as u32 / page_size) <= state.capacity() as u32 / STATE::WRITE_SIZE as u32); |
| 386 | F: NorFlash, | ||
| 387 | { | ||
| 388 | flash: F, | ||
| 389 | } | ||
| 390 | |||
| 391 | impl<F> BootFlash<F> | ||
| 392 | where | ||
| 393 | F: NorFlash, | ||
| 394 | { | ||
| 395 | /// Create a new instance of a bootable flash | ||
| 396 | pub fn new(flash: F) -> Self { | ||
| 397 | Self { flash } | ||
| 398 | } | ||
| 399 | } | ||
| 400 | |||
| 401 | impl<F> ErrorType for BootFlash<F> | ||
| 402 | where | ||
| 403 | F: NorFlash, | ||
| 404 | { | ||
| 405 | type Error = F::Error; | ||
| 406 | } | ||
| 407 | |||
| 408 | impl<F> NorFlash for BootFlash<F> | ||
| 409 | where | ||
| 410 | F: NorFlash, | ||
| 411 | { | ||
| 412 | const WRITE_SIZE: usize = F::WRITE_SIZE; | ||
| 413 | const ERASE_SIZE: usize = F::ERASE_SIZE; | ||
| 414 | |||
| 415 | fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> { | ||
| 416 | F::erase(&mut self.flash, from, to) | ||
| 417 | } | ||
| 418 | |||
| 419 | fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> { | ||
| 420 | F::write(&mut self.flash, offset, bytes) | ||
| 421 | } | ||
| 422 | } | ||
| 423 | |||
| 424 | impl<F> ReadNorFlash for BootFlash<F> | ||
| 425 | where | ||
| 426 | F: NorFlash, | ||
| 427 | { | ||
| 428 | const READ_SIZE: usize = F::READ_SIZE; | ||
| 429 | |||
| 430 | fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> { | ||
| 431 | F::read(&mut self.flash, offset, bytes) | ||
| 432 | } | ||
| 433 | |||
| 434 | fn capacity(&self) -> usize { | ||
| 435 | F::capacity(&self.flash) | ||
| 436 | } | ||
| 437 | } | ||
| 438 | |||
| 439 | /// Convenience provider that uses a single flash for all partitions. | ||
| 440 | pub struct SingleFlashConfig<'a, F> | ||
| 441 | where | ||
| 442 | F: NorFlash, | ||
| 443 | { | ||
| 444 | flash: &'a mut F, | ||
| 445 | } | ||
| 446 | |||
| 447 | impl<'a, F> SingleFlashConfig<'a, F> | ||
| 448 | where | ||
| 449 | F: NorFlash, | ||
| 450 | { | ||
| 451 | /// Create a provider for a single flash. | ||
| 452 | pub fn new(flash: &'a mut F) -> Self { | ||
| 453 | Self { flash } | ||
| 454 | } | ||
| 455 | } | ||
| 456 | |||
| 457 | impl<'a, F> FlashConfig for SingleFlashConfig<'a, F> | ||
| 458 | where | ||
| 459 | F: NorFlash, | ||
| 460 | { | ||
| 461 | type STATE = F; | ||
| 462 | type ACTIVE = F; | ||
| 463 | type DFU = F; | ||
| 464 | |||
| 465 | fn active(&mut self) -> &mut Self::STATE { | ||
| 466 | self.flash | ||
| 467 | } | ||
| 468 | fn dfu(&mut self) -> &mut Self::ACTIVE { | ||
| 469 | self.flash | ||
| 470 | } | ||
| 471 | fn state(&mut self) -> &mut Self::DFU { | ||
| 472 | self.flash | ||
| 473 | } | ||
| 474 | } | ||
| 475 | |||
| 476 | /// Convenience flash provider that uses separate flash instances for each partition. | ||
| 477 | pub struct MultiFlashConfig<'a, ACTIVE, STATE, DFU> | ||
| 478 | where | ||
| 479 | ACTIVE: NorFlash, | ||
| 480 | STATE: NorFlash, | ||
| 481 | DFU: NorFlash, | ||
| 482 | { | ||
| 483 | active: &'a mut ACTIVE, | ||
| 484 | state: &'a mut STATE, | ||
| 485 | dfu: &'a mut DFU, | ||
| 486 | } | ||
| 487 | |||
| 488 | impl<'a, ACTIVE, STATE, DFU> MultiFlashConfig<'a, ACTIVE, STATE, DFU> | ||
| 489 | where | ||
| 490 | ACTIVE: NorFlash, | ||
| 491 | STATE: NorFlash, | ||
| 492 | DFU: NorFlash, | ||
| 493 | { | ||
| 494 | /// Create a new flash provider with separate configuration for all three partitions. | ||
| 495 | pub fn new(active: &'a mut ACTIVE, state: &'a mut STATE, dfu: &'a mut DFU) -> Self { | ||
| 496 | Self { active, state, dfu } | ||
| 497 | } | ||
| 498 | } | ||
| 499 | |||
| 500 | impl<'a, ACTIVE, STATE, DFU> FlashConfig for MultiFlashConfig<'a, ACTIVE, STATE, DFU> | ||
| 501 | where | ||
| 502 | ACTIVE: NorFlash, | ||
| 503 | STATE: NorFlash, | ||
| 504 | DFU: NorFlash, | ||
| 505 | { | ||
| 506 | type STATE = STATE; | ||
| 507 | type ACTIVE = ACTIVE; | ||
| 508 | type DFU = DFU; | ||
| 509 | |||
| 510 | fn active(&mut self) -> &mut Self::ACTIVE { | ||
| 511 | self.active | ||
| 512 | } | ||
| 513 | fn dfu(&mut self) -> &mut Self::DFU { | ||
| 514 | self.dfu | ||
| 515 | } | ||
| 516 | fn state(&mut self) -> &mut Self::STATE { | ||
| 517 | self.state | ||
| 518 | } | ||
| 519 | } | 403 | } |
| 520 | 404 | ||
| 521 | #[cfg(test)] | 405 | #[cfg(test)] |
| 522 | mod tests { | 406 | mod tests { |
| 523 | use super::*; | 407 | use super::*; |
| 408 | use crate::mem_flash::MemFlash; | ||
| 524 | 409 | ||
| 525 | #[test] | 410 | #[test] |
| 526 | #[should_panic] | 411 | #[should_panic] |
| 527 | fn test_range_asserts() { | 412 | fn test_range_asserts() { |
| 528 | const ACTIVE: Partition = Partition::new(4096, 4194304); | 413 | const ACTIVE_SIZE: usize = 4194304 - 4096; |
| 529 | const DFU: Partition = Partition::new(4194304, 2 * 4194304); | 414 | const DFU_SIZE: usize = 4194304; |
| 530 | const STATE: Partition = Partition::new(0, 4096); | 415 | const STATE_SIZE: usize = 4096; |
| 531 | assert_partitions(ACTIVE, DFU, STATE, 4096, 4); | 416 | static ACTIVE: MemFlash<ACTIVE_SIZE, 4, 4> = MemFlash::new(0xFF); |
| 417 | static DFU: MemFlash<DFU_SIZE, 4, 4> = MemFlash::new(0xFF); | ||
| 418 | static STATE: MemFlash<STATE_SIZE, 4, 4> = MemFlash::new(0xFF); | ||
| 419 | assert_partitions(&ACTIVE, &DFU, &STATE, 4096); | ||
| 532 | } | 420 | } |
| 533 | } | 421 | } |
