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