Skip to content

Commit e4e200d

Browse files
committed
add check that app partitions are aligned to 64k
1 parent 88fef26 commit e4e200d

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

espflash/src/error.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,9 @@ pub enum PartitionTableError {
280280
#[error(transparent)]
281281
#[diagnostic(transparent)]
282282
InvalidSubType(#[from] InvalidSubTypeError),
283+
#[error(transparent)]
284+
#[diagnostic(transparent)]
285+
UnalignedPartitionError(#[from] UnalignedPartitionError),
283286
}
284287

285288
#[derive(Debug, Error, Diagnostic)]
@@ -430,6 +433,25 @@ impl InvalidSubTypeError {
430433
}
431434
}
432435

436+
#[derive(Debug, Error, Diagnostic)]
437+
#[error("Unaligned partition")]
438+
#[diagnostic(code(espflash::partition_table::unaligned))]
439+
pub struct UnalignedPartitionError {
440+
#[source_code]
441+
source_code: String,
442+
#[label("App partition is not aligned to 64k (0x10000)")]
443+
span: SourceSpan,
444+
}
445+
446+
impl UnalignedPartitionError {
447+
pub fn new(source: &str, line: usize) -> Self {
448+
UnalignedPartitionError {
449+
source_code: source.into(),
450+
span: line_to_span(&source, line),
451+
}
452+
}
453+
}
454+
433455
#[derive(Debug, Error)]
434456
#[error("{0}")]
435457
pub struct ElfError(&'static str);

espflash/src/partition_table.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::error::{
22
CSVError, DuplicatePartitionsError, InvalidSubTypeError, OverlappingPartitionsError,
3-
PartitionTableError,
3+
PartitionTableError, UnalignedPartitionError,
44
};
55
use md5::{Context, Digest};
66
use regex::Regex;
@@ -9,6 +9,7 @@ use std::cmp::{max, min};
99
use std::fmt::Write as _;
1010
use std::fmt::{Display, Formatter};
1111
use std::io::Write;
12+
use std::ops::Rem;
1213

1314
const MAX_PARTITION_LENGTH: usize = 0xC00;
1415
const PARTITION_TABLE_SIZE: usize = 0x1000;
@@ -261,6 +262,9 @@ impl PartitionTable {
261262
)
262263
.into());
263264
}
265+
if partition.ty == Type::App && partition.offset.rem(0x10000) != 0 {
266+
return Err(UnalignedPartitionError::new(source, *line).into());
267+
}
264268
}
265269
}
266270

0 commit comments

Comments
 (0)