1
- use crate :: error:: {
2
- CSVError , DuplicatePartitionsError , InvalidSubTypeError , OverlappingPartitionsError ,
3
- PartitionTableError , UnalignedPartitionError ,
1
+ use std:: {
2
+ cmp:: { max, min} ,
3
+ fmt:: { Display , Formatter , Write as _} ,
4
+ io:: Write ,
5
+ ops:: Rem ,
4
6
} ;
7
+
5
8
use md5:: { Context , Digest } ;
6
9
use regex:: Regex ;
7
10
use serde:: { Deserialize , Deserializer , Serialize } ;
8
- use std :: cmp :: { max , min } ;
9
- use std :: fmt :: Write as _ ;
10
- use std :: fmt :: { Display , Formatter } ;
11
- use std :: io :: Write ;
12
- use std :: ops :: Rem ;
11
+
12
+ use crate :: error :: {
13
+ CSVError , DuplicatePartitionsError , InvalidSubTypeError , NoFactoryAppError ,
14
+ OverlappingPartitionsError , PartitionTableError , UnalignedPartitionError ,
15
+ } ;
13
16
14
17
const MAX_PARTITION_LENGTH : usize = 0xC00 ;
15
18
const PARTITION_TABLE_SIZE : usize = 0x1000 ;
@@ -28,16 +31,9 @@ impl Type {
28
31
match self {
29
32
Type :: App => "'factory', 'ota_0' through 'ota_15' and 'test'" . into ( ) ,
30
33
Type :: Data => {
34
+ use DataType :: * ;
31
35
let types = [
32
- DataType :: Ota ,
33
- DataType :: Phy ,
34
- DataType :: Nvs ,
35
- DataType :: CoreDump ,
36
- DataType :: NvsKeys ,
37
- DataType :: EFuse ,
38
- DataType :: EspHttpd ,
39
- DataType :: Fat ,
40
- DataType :: Spiffs ,
36
+ Ota , Phy , Nvs , CoreDump , NvsKeys , EFuse , EspHttpd , Fat , Spiffs ,
41
37
] ;
42
38
43
39
let mut out = format ! ( "'{}'" , serde_plain:: to_string( & types[ 0 ] ) . unwrap( ) ) ;
@@ -57,8 +53,7 @@ impl Type {
57
53
58
54
impl Display for Type {
59
55
fn fmt ( & self , f : & mut Formatter < ' _ > ) -> std:: fmt:: Result {
60
- let ser = serde_plain:: to_string ( self ) . unwrap ( ) ;
61
- write ! ( f, "{}" , ser)
56
+ write ! ( f, "{}" , serde_plain:: to_string( self ) . unwrap( ) )
62
57
}
63
58
}
64
59
@@ -246,13 +241,18 @@ impl PartitionTable {
246
241
Ok ( ( ) )
247
242
}
248
243
244
+ pub fn find ( & self , name : & str ) -> Option < & Partition > {
245
+ self . partitions . iter ( ) . find ( |& p| p. name == name)
246
+ }
247
+
249
248
fn validate ( & self , source : & str ) -> Result < ( ) , PartitionTableError > {
250
249
for partition in & self . partitions {
251
250
if let Some ( line) = & partition. line {
252
251
let expected_type = match partition. sub_type {
253
252
SubType :: App ( _) => Type :: App ,
254
253
SubType :: Data ( _) => Type :: Data ,
255
254
} ;
255
+
256
256
if expected_type != partition. ty {
257
257
return Err ( InvalidSubTypeError :: new (
258
258
source,
@@ -262,6 +262,7 @@ impl PartitionTable {
262
262
)
263
263
. into ( ) ) ;
264
264
}
265
+
265
266
if partition. ty == Type :: App && partition. offset . rem ( 0x10000 ) != 0 {
266
267
return Err ( UnalignedPartitionError :: new ( source, * line) . into ( ) ) ;
267
268
}
@@ -277,12 +278,14 @@ impl PartitionTable {
277
278
OverlappingPartitionsError :: new ( source, * line1, * line2) . into ( )
278
279
) ;
279
280
}
281
+
280
282
if partition1. name == partition2. name {
281
283
return Err ( DuplicatePartitionsError :: new (
282
284
source, * line1, * line2, "name" ,
283
285
)
284
286
. into ( ) ) ;
285
287
}
288
+
286
289
if partition1. sub_type == partition2. sub_type {
287
290
return Err ( DuplicatePartitionsError :: new (
288
291
source, * line1, * line2, "sub-type" ,
@@ -294,14 +297,20 @@ impl PartitionTable {
294
297
}
295
298
}
296
299
300
+ if self . find ( "factory" ) . is_none ( ) {
301
+ return Err ( PartitionTableError :: NoFactoryApp ( NoFactoryAppError :: new (
302
+ source,
303
+ ) ) ) ;
304
+ }
305
+
297
306
Ok ( ( ) )
298
307
}
299
308
}
300
309
301
310
const PARTITION_SIZE : usize = 32 ;
302
311
303
312
#[ derive( Debug , Deserialize ) ]
304
- struct Partition {
313
+ pub struct Partition {
305
314
#[ serde( deserialize_with = "deserialize_partition_name" ) ]
306
315
name : String ,
307
316
ty : Type ,
@@ -358,6 +367,10 @@ impl Partition {
358
367
Ok ( ( ) )
359
368
}
360
369
370
+ pub fn offset ( & self ) -> u32 {
371
+ self . offset
372
+ }
373
+
361
374
fn overlaps ( & self , other : & Partition ) -> bool {
362
375
max ( self . offset , other. offset ) < min ( self . offset + self . size , other. offset + other. size )
363
376
}
0 commit comments