1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
//! The errors found while checking a layout.
use super::Role;
use alloc::string::String;
use alloc::vec::Vec;
use core::fmt;
use serde::Serialize;
/// One problem found in a layout.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(tag = "kind", rename_all = "kebab-case")]
pub enum LayoutError {
/// The layout has no partition.
EmptyLayout,
/// The flash has a size of zero.
ZeroFlashSize,
/// An alignment is zero or not a power of two (`None` is the flash default).
InvalidAlignment {
/// The partition, or `None` for the flash default.
partition: Option<String>,
/// The offending alignment.
alignment: u64,
},
/// The flash size is not a multiple of the default alignment.
FlashSizeNotAligned {
/// The flash size.
size: u64,
/// The default alignment.
alignment: u64,
},
/// Two partitions share a name.
DuplicateName {
/// The name.
name: String,
},
/// A partition has an empty name.
EmptyName,
/// A partition has a size of zero.
ZeroSize {
/// The partition.
partition: String,
},
/// A fixed offset is not a multiple of the alignment.
MisalignedOffset {
/// The partition.
partition: String,
/// Its offset.
offset: u64,
/// Its alignment.
alignment: u64,
},
/// Two partitions overlap.
Overlap {
/// The partition that comes first by offset.
first: String,
/// The partition that comes second by offset.
second: String,
/// The exclusive end of the first partition.
first_end: u64,
/// The offset of the second partition.
second_offset: u64,
},
/// A partition ends after the end of the flash.
ExceedsFlash {
/// The partition.
partition: String,
/// Its exclusive end.
end: u64,
/// The flash size.
flash_size: u64,
},
/// A partition taking the remaining space has no space left.
NoSpaceForRemaining {
/// The partition.
partition: String,
/// Where it starts.
offset: u64,
/// The address where the space ends.
limit: u64,
/// The partition that starts at `limit`, or `None` when the flash ends there.
limited_by: Option<String>,
},
/// A partition taking the remaining space shares it with others, and its share rounds down
/// to zero bytes.
NoShareForRemaining {
/// The partition.
partition: String,
/// Where the free space of its segment starts.
start: u64,
/// Where the free space of its segment ends.
limit: u64,
/// Its share is a multiple of this, the largest alignment of the partitions after it.
step: u64,
/// The partition after it whose alignment is `step`, or `None` for a step of one byte.
set_by: Option<String>,
},
/// A fixed offset of a `detect` layout is below an earlier fixed offset.
FixedOffsetBelowPrevious {
/// The partition.
partition: String,
/// Its fixed offset.
offset: u64,
/// The partition with a fixed offset before it.
previous: String,
/// The fixed offset of `previous`.
previous_offset: u64,
},
/// A partition whose role needs a fixed size grows.
RoleCannotGrow {
/// The partition.
partition: String,
/// Its role.
role: Role,
},
/// A growing partition has a maximum below its minimum.
InvalidGrowth {
/// The partition.
partition: String,
/// Its minimum.
minimum: u64,
/// Its maximum.
maximum: u64,
},
/// A partition whose role needs a known offset would move with the flash of the device.
OffsetDependsOnFlash {
/// The partition.
partition: String,
/// The first partition before it that grows with the flash.
after: String,
},
/// A layout with a fixed flash size was asked to resolve for another size.
FlashSizeFixed {
/// The flash size of the layout.
size: u64,
/// The size asked for.
requested: u64,
},
/// The flash is smaller than the minimum of a `detect` layout.
FlashBelowMinimum {
/// The size of the flash.
size: u64,
/// The minimum of the layout.
minimum: u64,
},
/// Two update partitions use the same slot.
DuplicateSlot {
/// The slot number.
slot: u8,
/// The first partition.
first: String,
/// The second partition.
second: String,
},
/// An offset or a size does not fit in 64 bits.
Overflow {
/// The partition.
partition: String,
},
/// A partition name that is not in the layout.
UnknownPartition {
/// The name.
name: String,
},
/// An image does not fit in its partition.
ImageTooLarge {
/// The partition.
partition: String,
/// The size of the image.
image_size: u64,
/// The size of the partition.
partition_size: u64,
},
}
impl fmt::Display for LayoutError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::EmptyLayout => formatter.write_str("the layout has no partition"),
Self::ZeroFlashSize => formatter.write_str("the flash size is zero"),
Self::InvalidAlignment {
partition: Some(partition),
alignment,
} => write!(
formatter,
"the alignment {alignment:#x} of partition \"{partition}\" is not a power of two"
),
Self::InvalidAlignment {
partition: None,
alignment,
} => write!(
formatter,
"the default flash alignment {alignment:#x} is not a power of two"
),
Self::FlashSizeNotAligned { size, alignment } => write!(
formatter,
"the flash size {size:#x} is not a multiple of the alignment {alignment:#x}"
),
Self::DuplicateName { name } => {
write!(formatter, "several partitions are named \"{name}\"")
}
Self::EmptyName => formatter.write_str("a partition has an empty name"),
Self::ZeroSize { partition } => {
write!(formatter, "partition \"{partition}\" has a size of zero")
}
Self::MisalignedOffset {
partition,
offset,
alignment,
} => write!(
formatter,
"partition \"{partition}\" starts at {offset:#x}, which is not a multiple of its alignment {alignment:#x}"
),
Self::Overlap {
first,
second,
first_end,
second_offset,
} => write!(
formatter,
"partitions \"{first}\" and \"{second}\" overlap: \"{first}\" ends at {first_end:#x} but \"{second}\" starts at {second_offset:#x}"
),
Self::ExceedsFlash {
partition,
end,
flash_size,
} => write!(
formatter,
"partition \"{partition}\" ends at {end:#x}, past the end of the flash at {flash_size:#x}"
),
Self::NoSpaceForRemaining {
partition,
offset,
limit,
limited_by: Some(next),
} => write!(
formatter,
"partition \"{partition}\" uses the remaining space but starts at {offset:#x}, and the next partition \"{next}\" starts at {limit:#x}, so no space is left"
),
Self::NoSpaceForRemaining {
partition,
offset,
limit,
limited_by: None,
} => write!(
formatter,
"partition \"{partition}\" uses the remaining space but starts at {offset:#x}, and the flash ends at {limit:#x}, so no space is left"
),
Self::NoShareForRemaining {
partition,
start,
limit,
step,
set_by,
} => {
write!(
formatter,
"partition \"{partition}\" uses the remaining space, but its share of the free space from {start:#x} to {limit:#x} rounds down to zero bytes in steps of {step:#x}"
)?;
match set_by {
Some(set_by) => write!(
formatter,
", the alignment of partition \"{set_by}\" after it"
),
None => Ok(()),
}
}
Self::FixedOffsetBelowPrevious {
partition,
offset,
previous,
previous_offset,
} => write!(
formatter,
"partition \"{partition}\" has a fixed offset of {offset:#x}, below {previous_offset:#x} of \"{previous}\" before it: in a layout with size = \"detect\", fixed offsets ascend in file order"
),
Self::RoleCannotGrow { partition, role } => write!(
formatter,
"partition \"{partition}\" has role {} and cannot grow: give it a fixed size",
role.name()
),
Self::InvalidGrowth {
partition,
minimum,
maximum,
} => write!(
formatter,
"partition \"{partition}\" may grow to a maximum of {maximum:#x} bytes, less than its minimum of {minimum:#x} bytes"
),
Self::OffsetDependsOnFlash { partition, after } => write!(
formatter,
"partition \"{partition}\" comes after \"{after}\", which grows with the flash of the device, so its offset would depend on the device: give \"{partition}\" a fixed offset or put it before \"{after}\""
),
Self::FlashSizeFixed { size, requested } => write!(
formatter,
"the layout has a fixed flash size of {size:#x} bytes and cannot be resolved for {requested:#x}: only a layout with size = \"detect\" can"
),
Self::FlashBelowMinimum { size, minimum } => write!(
formatter,
"the flash of {size:#x} bytes is smaller than the minimum of {minimum:#x} bytes of the layout"
),
Self::DuplicateSlot {
slot,
first,
second,
} => write!(
formatter,
"partitions \"{first}\" and \"{second}\" both use update slot {slot}"
),
Self::Overflow { partition } => write!(
formatter,
"the offset or the size of partition \"{partition}\" does not fit in 64 bits"
),
Self::UnknownPartition { name } => {
write!(formatter, "there is no partition named \"{name}\"")
}
Self::ImageTooLarge {
partition,
image_size,
partition_size,
} => write!(
formatter,
"the image of {image_size:#x} bytes does not fit in partition \"{partition}\" of {partition_size:#x} bytes"
),
}
}
}
impl core::error::Error for LayoutError {}
/// Every problem found in a layout.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct LayoutErrors(pub Vec<LayoutError>);
impl fmt::Display for LayoutErrors {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
for (index, error) in self.0.iter().enumerate() {
if index > 0 {
formatter.write_str("\n")?;
}
write!(formatter, "{error}")?;
}
Ok(())
}
}
impl core::error::Error for LayoutErrors {}