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
//! Writers of the partition table each family needs, and of the linker script and constants.
#[cfg(feature = "table-espressif")]
pub mod espressif;
#[cfg(feature = "table-linker")]
pub mod linker;
#[cfg(feature = "table-mbr")]
pub mod mbr;
use alloc::string::String;
use core::fmt;
use serde::Serialize;
/// A layout that a partition table writer cannot express.
///
/// Shared by every writer, so a variant may only be produced by some of them.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(tag = "kind", rename_all = "kebab-case")]
pub enum TableError {
/// An attribute the writer needs is neither given nor implied by the role.
MissingAttribute {
/// The partition.
partition: String,
/// The writer, such as `espressif`.
writer: &'static str,
/// The attribute, such as `subtype`.
attribute: &'static str,
},
/// An attribute holds a name or number the writer does not know.
UnknownValue {
/// The writer, such as `espressif`.
writer: &'static str,
/// The partition.
partition: String,
/// The attribute, such as `subtype`.
attribute: &'static str,
/// The value as given.
value: String,
},
/// A partition name is longer than the table allows.
LabelTooLong {
/// The writer, such as `espressif`.
writer: &'static str,
/// The partition.
partition: String,
},
/// A partition name holds characters outside ASCII.
NotAscii {
/// The writer, such as `espressif`.
writer: &'static str,
/// The partition.
partition: String,
},
/// An offset or a size is not a multiple of what the table requires.
Misaligned {
/// The writer, such as `espressif`.
writer: &'static str,
/// The partition.
partition: String,
/// What is misaligned, `offset` or `size`.
what: &'static str,
/// Its value.
value: u64,
/// The required alignment.
required: u64,
},
/// An entry starts inside the first sector, which the table itself occupies.
CoversFirstSector {
/// The writer, such as `mbr`.
writer: &'static str,
/// The partition.
partition: String,
/// Its offset.
offset: u64,
/// The first offset an entry may use.
minimum: u64,
},
/// A partition has a size the table does not allow.
WrongSize {
/// The writer, such as `espressif`.
writer: &'static str,
/// The partition.
partition: String,
/// Its size.
size: u64,
/// The size the table requires.
required: u64,
/// What the partition is, such as `otadata`.
what: &'static str,
},
/// Two partitions turn into the same identifier.
NameCollision {
/// The writer, such as `linker`.
writer: &'static str,
/// The first partition.
first: String,
/// The second partition.
second: String,
/// The identifier both turn into.
identifier: String,
},
/// An offset, size or number does not fit the field of the table.
OutOfRange {
/// The writer, such as `espressif`.
writer: &'static str,
/// The partition.
partition: String,
},
/// The layout has more partitions than the table holds.
TooManyPartitions {
/// The writer, such as `espressif`.
writer: &'static str,
/// The number of partitions to write.
count: usize,
/// The number the table holds.
maximum: usize,
},
/// A partition the table depends on is missing.
MissingPartition {
/// The writer, such as `espressif`.
writer: &'static str,
/// What is missing.
role: &'static str,
/// The partition that needs it, when one does.
required_by: Option<String>,
},
/// The partition meant to hold the table is too small for it.
TableDoesNotFit {
/// The writer, such as `espressif`.
writer: &'static str,
/// The partition.
partition: String,
/// Its size.
size: u64,
/// The size the table needs.
required: u64,
},
/// Two partitions both claim to be the firmware.
SeveralFirmware {
/// The writer, such as `espressif`.
writer: &'static str,
/// The first partition.
first: String,
/// The second partition.
second: String,
},
/// A writer that describes what a firmware can count on with any flash was given a layout
/// resolved for a larger flash.
NotAtMinimum {
/// The writer, such as `linker`.
writer: &'static str,
/// The flash size the layout was resolved for.
flash_size: u64,
/// The minimum flash size of the layout.
minimum_flash_size: u64,
},
/// A memory region given to the linker writer has the name of a region of the layout.
DuplicateRegion {
/// The name of the region.
name: String,
},
/// A memory region given to the linker writer has a name that a linker script cannot hold.
InvalidRegionName {
/// The name of the region as given.
name: String,
},
/// Two memory regions given to the linker writer have the same name.
RepeatedRegion {
/// The name of the region.
name: String,
},
}
impl fmt::Display for TableError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
TableError::MissingAttribute {
partition,
writer,
attribute,
} => write!(
formatter,
"the {writer} writer cannot place partition \"{partition}\": it needs the \"{attribute}\" attribute, set under [partition.{writer}]"
),
TableError::UnknownValue {
partition,
writer,
attribute,
value,
} => write!(
formatter,
"the {writer} writer cannot place partition \"{partition}\": \"{value}\" is not a known {attribute}"
),
TableError::LabelTooLong { partition, writer } => write!(
formatter,
"the {writer} writer cannot place partition \"{partition}\": its name is longer than 16 bytes"
),
TableError::NotAscii { partition, writer } => write!(
formatter,
"the {writer} writer cannot place partition \"{partition}\": its name is not ASCII"
),
TableError::Misaligned {
partition,
writer,
what,
value,
required,
} => write!(
formatter,
"the {writer} writer cannot place partition \"{partition}\": {what} {value:#x} is not a multiple of {required:#x}"
),
TableError::CoversFirstSector {
writer,
partition,
offset,
minimum,
} => write!(
formatter,
"the {writer} writer cannot place partition \"{partition}\": its offset {offset:#x} is below {minimum:#x}, so it covers the first sector, which holds the table"
),
TableError::WrongSize {
writer,
partition,
size,
required,
what,
} => write!(
formatter,
"the {writer} writer cannot place partition \"{partition}\": {what} must be exactly {required:#x} bytes but it is {size:#x}"
),
TableError::NameCollision {
writer,
first,
second,
identifier,
} => write!(
formatter,
"the {writer} writer cannot place partitions \"{first}\" and \"{second}\": both become \"{identifier}\""
),
TableError::OutOfRange { partition, writer } => write!(
formatter,
"the {writer} writer cannot place partition \"{partition}\": its offset, size, slot or number is out of range"
),
TableError::TooManyPartitions {
writer,
count,
maximum,
} => write!(
formatter,
"the {writer} writer holds at most {maximum} partitions but the layout has {count}"
),
TableError::MissingPartition {
writer,
role,
required_by,
} => {
write!(
formatter,
"the {writer} writer needs a partition for {role}, and the layout has none"
)?;
if let Some(partition) = required_by {
write!(formatter, " (required by partition \"{partition}\")")?;
}
Ok(())
}
TableError::TableDoesNotFit {
partition,
writer,
size,
required,
} => write!(
formatter,
"the {writer} writer cannot fit its table in partition \"{partition}\": it is {size:#x} bytes and the table needs {required:#x}"
),
TableError::SeveralFirmware {
writer,
first,
second,
} => write!(
formatter,
"the {writer} writer cannot place partitions \"{first}\" and \"{second}\": both are the factory application"
),
TableError::NotAtMinimum {
writer,
flash_size,
minimum_flash_size,
} => write!(
formatter,
"the {writer} writer describes what a firmware can count on with any flash, so it needs the layout resolved for its minimum flash of {minimum_flash_size:#x} bytes, not for {flash_size:#x}"
),
TableError::DuplicateRegion { name } => write!(
formatter,
"the region {name} is both a partition of the layout and a memory region given to the build: rename one"
),
TableError::InvalidRegionName { name } => write!(
formatter,
"the memory region \"{name}\" given to the build has a name that the linker cannot read: use ASCII letters, digits and _, and do not start with a digit"
),
TableError::RepeatedRegion { name } => write!(
formatter,
"the memory region {name} is given to the build more than once: give each region once"
),
}
}
}
impl core::error::Error for TableError {}