David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 1 | //! Describe flash areas. |
| 2 | |
David Brown | 2cbc470 | 2017-07-06 14:18:58 -0600 | [diff] [blame] | 3 | use simflash::{Flash, SimFlash, Sector}; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 4 | use std::ptr; |
Fabio Utzig | 1caef13 | 2018-10-26 15:40:53 -0300 | [diff] [blame] | 5 | use std::collections::HashMap; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 6 | |
| 7 | /// Structure to build up the boot area table. |
David Brown | 3f687dc | 2017-11-06 13:41:18 -0700 | [diff] [blame] | 8 | #[derive(Debug, Clone)] |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 9 | pub struct AreaDesc { |
| 10 | areas: Vec<Vec<FlashArea>>, |
| 11 | whole: Vec<FlashArea>, |
Fabio Utzig | 1caef13 | 2018-10-26 15:40:53 -0300 | [diff] [blame] | 12 | sectors: HashMap<u8, Vec<Sector>>, |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 13 | } |
| 14 | |
| 15 | impl AreaDesc { |
Fabio Utzig | 1caef13 | 2018-10-26 15:40:53 -0300 | [diff] [blame] | 16 | pub fn new() -> AreaDesc { |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 17 | AreaDesc { |
| 18 | areas: vec![], |
| 19 | whole: vec![], |
Fabio Utzig | 1caef13 | 2018-10-26 15:40:53 -0300 | [diff] [blame] | 20 | sectors: HashMap::new(), |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 21 | } |
| 22 | } |
| 23 | |
Fabio Utzig | 1caef13 | 2018-10-26 15:40:53 -0300 | [diff] [blame] | 24 | pub fn add_flash_sectors(&mut self, id: u8, flash: &SimFlash) { |
| 25 | self.sectors.insert(id, flash.sector_iter().collect()); |
| 26 | } |
| 27 | |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 28 | /// Add a slot to the image. The slot must align with erasable units in the flash device. |
| 29 | /// Panics if the description is not valid. There are also bootloader assumptions that the |
| 30 | /// slots are SLOT0, SLOT1, and SCRATCH in that order. |
Fabio Utzig | 1caef13 | 2018-10-26 15:40:53 -0300 | [diff] [blame] | 31 | pub fn add_image(&mut self, base: usize, len: usize, id: FlashId, dev_id: u8) { |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 32 | let nid = id as usize; |
| 33 | let orig_base = base; |
| 34 | let orig_len = len; |
| 35 | let mut base = base; |
| 36 | let mut len = len; |
| 37 | |
| 38 | while nid > self.areas.len() { |
| 39 | self.areas.push(vec![]); |
| 40 | self.whole.push(Default::default()); |
| 41 | } |
| 42 | |
| 43 | if nid != self.areas.len() { |
| 44 | panic!("Flash areas not added in order"); |
| 45 | } |
| 46 | |
| 47 | let mut area = vec![]; |
| 48 | |
Fabio Utzig | 1caef13 | 2018-10-26 15:40:53 -0300 | [diff] [blame] | 49 | for sector in &self.sectors[&dev_id] { |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 50 | if len == 0 { |
| 51 | break; |
| 52 | }; |
| 53 | if base > sector.base + sector.size - 1 { |
| 54 | continue; |
| 55 | } |
| 56 | if sector.base != base { |
| 57 | panic!("Image does not start on a sector boundary"); |
| 58 | } |
| 59 | |
| 60 | area.push(FlashArea { |
| 61 | flash_id: id, |
Fabio Utzig | 1caef13 | 2018-10-26 15:40:53 -0300 | [diff] [blame] | 62 | device_id: dev_id, |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 63 | pad16: 0, |
| 64 | off: sector.base as u32, |
| 65 | size: sector.size as u32, |
| 66 | }); |
| 67 | |
| 68 | base += sector.size; |
| 69 | len -= sector.size; |
| 70 | } |
| 71 | |
| 72 | if len != 0 { |
| 73 | panic!("Image goes past end of device"); |
| 74 | } |
| 75 | |
| 76 | self.areas.push(area); |
| 77 | self.whole.push(FlashArea { |
| 78 | flash_id: id, |
Fabio Utzig | 1caef13 | 2018-10-26 15:40:53 -0300 | [diff] [blame] | 79 | device_id: dev_id, |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 80 | pad16: 0, |
| 81 | off: orig_base as u32, |
| 82 | size: orig_len as u32, |
| 83 | }); |
| 84 | } |
| 85 | |
David Brown | 90c1913 | 2017-01-23 10:21:28 -0700 | [diff] [blame] | 86 | // Add a simple slot to the image. This ignores the device layout, and just adds the area as a |
| 87 | // single unit. It assumes that the image lines up with image boundaries. This tests |
| 88 | // configurations where the partition table uses larger sectors than the underlying flash |
| 89 | // device. |
Fabio Utzig | 1caef13 | 2018-10-26 15:40:53 -0300 | [diff] [blame] | 90 | pub fn add_simple_image(&mut self, base: usize, len: usize, id: FlashId, dev_id: u8) { |
David Brown | 90c1913 | 2017-01-23 10:21:28 -0700 | [diff] [blame] | 91 | let area = vec![FlashArea { |
| 92 | flash_id: id, |
Fabio Utzig | 1caef13 | 2018-10-26 15:40:53 -0300 | [diff] [blame] | 93 | device_id: dev_id, |
David Brown | 90c1913 | 2017-01-23 10:21:28 -0700 | [diff] [blame] | 94 | pad16: 0, |
| 95 | off: base as u32, |
| 96 | size: len as u32, |
| 97 | }]; |
| 98 | |
| 99 | self.areas.push(area); |
| 100 | self.whole.push(FlashArea { |
| 101 | flash_id: id, |
Fabio Utzig | 1caef13 | 2018-10-26 15:40:53 -0300 | [diff] [blame] | 102 | device_id: dev_id, |
David Brown | 90c1913 | 2017-01-23 10:21:28 -0700 | [diff] [blame] | 103 | pad16: 0, |
| 104 | off: base as u32, |
| 105 | size: len as u32, |
| 106 | }); |
| 107 | } |
| 108 | |
David Brown | 5c6b679 | 2017-03-20 12:51:28 -0600 | [diff] [blame] | 109 | // Look for the image with the given ID, and return its base and size. Panics if the area is |
| 110 | // not present. |
| 111 | pub fn find(&self, id: FlashId) -> (usize, usize) { |
| 112 | for area in &self.whole { |
Fabio Utzig | 1caef13 | 2018-10-26 15:40:53 -0300 | [diff] [blame] | 113 | // FIXME: should we ensure id is not duplicated over multiple devices? |
David Brown | 5c6b679 | 2017-03-20 12:51:28 -0600 | [diff] [blame] | 114 | if area.flash_id == id { |
| 115 | return (area.off as usize, area.size as usize); |
| 116 | } |
| 117 | } |
| 118 | panic!("Requesting area that is not present in flash"); |
| 119 | } |
| 120 | |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 121 | pub fn get_c(&self) -> CAreaDesc { |
| 122 | let mut areas: CAreaDesc = Default::default(); |
| 123 | |
| 124 | assert_eq!(self.areas.len(), self.whole.len()); |
| 125 | |
| 126 | for (i, area) in self.areas.iter().enumerate() { |
| 127 | if area.len() > 0 { |
| 128 | areas.slots[i].areas = &area[0]; |
| 129 | areas.slots[i].whole = self.whole[i].clone(); |
| 130 | areas.slots[i].num_areas = area.len() as u32; |
| 131 | areas.slots[i].id = area[0].flash_id; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | areas.num_slots = self.areas.len() as u32; |
| 136 | |
| 137 | areas |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | /// The area descriptor, C format. |
| 142 | #[repr(C)] |
| 143 | #[derive(Debug, Default)] |
David Brown | 0daa36c | 2017-03-07 12:08:27 +0100 | [diff] [blame] | 144 | pub struct CAreaDesc { |
| 145 | slots: [CArea; 16], |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 146 | num_slots: u32, |
| 147 | } |
| 148 | |
| 149 | #[repr(C)] |
| 150 | #[derive(Debug)] |
David Brown | 0daa36c | 2017-03-07 12:08:27 +0100 | [diff] [blame] | 151 | pub struct CArea { |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 152 | whole: FlashArea, |
| 153 | areas: *const FlashArea, |
| 154 | num_areas: u32, |
Fabio Utzig | 1caef13 | 2018-10-26 15:40:53 -0300 | [diff] [blame] | 155 | // FIXME: is this not already available on whole/areas? |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 156 | id: FlashId, |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 157 | } |
| 158 | |
David Brown | 0daa36c | 2017-03-07 12:08:27 +0100 | [diff] [blame] | 159 | impl Default for CArea { |
| 160 | fn default() -> CArea { |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 161 | CArea { |
| 162 | areas: ptr::null(), |
| 163 | whole: Default::default(), |
| 164 | id: FlashId::BootLoader, |
| 165 | num_areas: 0, |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | /// Flash area map. |
| 171 | #[repr(u8)] |
| 172 | #[derive(Copy, Clone, Debug, Eq, PartialEq)] |
| 173 | #[allow(dead_code)] |
| 174 | pub enum FlashId { |
| 175 | BootLoader = 0, |
| 176 | Image0 = 1, |
| 177 | Image1 = 2, |
| 178 | ImageScratch = 3, |
| 179 | Nffs = 4, |
| 180 | Core = 5, |
| 181 | RebootLog = 6 |
| 182 | } |
| 183 | |
| 184 | impl Default for FlashId { |
| 185 | fn default() -> FlashId { |
| 186 | FlashId::BootLoader |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | #[repr(C)] |
| 191 | #[derive(Debug, Clone, Default)] |
| 192 | pub struct FlashArea { |
| 193 | flash_id: FlashId, |
| 194 | device_id: u8, |
| 195 | pad16: u16, |
| 196 | off: u32, |
| 197 | size: u32, |
| 198 | } |
| 199 | |