blob: 9b844d0704d67dd46b7e5298d1dc85557f5a4491 [file] [log] [blame]
David Brownde7729e2017-01-09 10:41:35 -07001//! Describe flash areas.
2
3use flash::{Flash, Sector};
David Brownde7729e2017-01-09 10:41:35 -07004use std::ptr;
5
6/// Structure to build up the boot area table.
7#[derive(Debug)]
8pub struct AreaDesc {
9 areas: Vec<Vec<FlashArea>>,
10 whole: Vec<FlashArea>,
11 sectors: Vec<Sector>,
12}
13
14impl 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 Brown90c19132017-01-23 10:21:28 -070081 // 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 Brownde7729e2017-01-09 10:41:35 -0700104 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 Brown0daa36c2017-03-07 12:08:27 +0100127pub struct CAreaDesc {
128 slots: [CArea; 16],
David Brownde7729e2017-01-09 10:41:35 -0700129 num_slots: u32,
130}
131
132#[repr(C)]
133#[derive(Debug)]
David Brown0daa36c2017-03-07 12:08:27 +0100134pub struct CArea {
David Brownde7729e2017-01-09 10:41:35 -0700135 whole: FlashArea,
136 areas: *const FlashArea,
137 num_areas: u32,
138 id: FlashId,
David Brownde7729e2017-01-09 10:41:35 -0700139}
140
David Brown0daa36c2017-03-07 12:08:27 +0100141impl Default for CArea {
142 fn default() -> CArea {
David Brownde7729e2017-01-09 10:41:35 -0700143 CArea {
144 areas: ptr::null(),
145 whole: Default::default(),
146 id: FlashId::BootLoader,
147 num_areas: 0,
David Brownde7729e2017-01-09 10:41:35 -0700148 }
149 }
150}
151
152/// Flash area map.
153#[repr(u8)]
154#[derive(Copy, Clone, Debug, Eq, PartialEq)]
155#[allow(dead_code)]
156pub 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
166impl Default for FlashId {
167 fn default() -> FlashId {
168 FlashId::BootLoader
169 }
170}
171
172#[repr(C)]
173#[derive(Debug, Clone, Default)]
174pub struct FlashArea {
175 flash_id: FlashId,
176 device_id: u8,
177 pad16: u16,
178 off: u32,
179 size: u32,
180}
181