Skip to content

Commit abd5356

Browse files
committed
[flash] Eliminate the mostly useless Ptr type
Signed-off-by: Miguel Young de la Sota <[email protected]>
1 parent 3e975be commit abd5356

File tree

2 files changed

+41
-71
lines changed

2 files changed

+41
-71
lines changed

src/hardware/flash.rs

Lines changed: 38 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ pub unsafe trait Flash {
7676
fn size(&self) -> Result<u32, Error>;
7777

7878
/// Attempts to read `out.len()` bytes starting at `offset`.
79-
fn read(&self, offset: Ptr, out: &mut [u8]) -> Result<(), Error>;
79+
fn read(&self, offset: u32, out: &mut [u8]) -> Result<(), Error>;
8080

8181
/// Attempts to perform a "direct read" of the given `Region`.
8282
///
@@ -99,7 +99,7 @@ pub unsafe trait Flash {
9999
/// # use manticore::hardware::flash::*;
100100
/// # struct Foo;
101101
/// # impl Foo {
102-
/// # fn read(&self, offset: Ptr, out: &mut [u8]) -> Result<(), Error> {
102+
/// # fn read(&self, offset: u32, out: &mut [u8]) -> Result<(), Error> {
103103
/// # Ok(())
104104
/// # }
105105
/// fn read_direct<'a: 'c, 'b: 'c, 'c>(
@@ -109,7 +109,7 @@ pub unsafe trait Flash {
109109
/// align: usize,
110110
/// ) -> Result<&'c [u8], Error> {
111111
/// let mut buf = arena.alloc_aligned(region.len as usize, align)?;
112-
/// self.read(region.ptr, &mut buf)?;
112+
/// self.read(region.offset, &mut buf)?;
113113
/// Ok(buf)
114114
/// }
115115
/// # }
@@ -134,7 +134,7 @@ pub unsafe trait Flash {
134134
/// Implementations are, as an optimization, permitted to assume that
135135
/// writes will be serial and localized, so as to minimize clearing
136136
/// operations on flash hardware.
137-
fn program(&mut self, offset: Ptr, buf: &[u8]) -> Result<(), Error>;
137+
fn program(&mut self, offset: u32, buf: &[u8]) -> Result<(), Error>;
138138

139139
/// Flushes any pending `program()` operations.
140140
fn flush(&mut self) -> Result<(), Error> {
@@ -150,7 +150,7 @@ unsafe impl<F: Flash> Flash for &F {
150150
}
151151

152152
#[inline]
153-
fn read(&self, offset: Ptr, out: &mut [u8]) -> Result<(), Error> {
153+
fn read(&self, offset: u32, out: &mut [u8]) -> Result<(), Error> {
154154
F::read(self, offset, out)
155155
}
156156

@@ -165,7 +165,7 @@ unsafe impl<F: Flash> Flash for &F {
165165
}
166166

167167
#[inline]
168-
fn program(&mut self, _: Ptr, _: &[u8]) -> Result<(), Error> {
168+
fn program(&mut self, _: u32, _: &[u8]) -> Result<(), Error> {
169169
Err(Error::Locked)
170170
}
171171

@@ -182,7 +182,7 @@ unsafe impl<F: Flash> Flash for &mut F {
182182
}
183183

184184
#[inline]
185-
fn read(&self, offset: Ptr, out: &mut [u8]) -> Result<(), Error> {
185+
fn read(&self, offset: u32, out: &mut [u8]) -> Result<(), Error> {
186186
F::read(self, offset, out)
187187
}
188188

@@ -197,7 +197,7 @@ unsafe impl<F: Flash> Flash for &mut F {
197197
}
198198

199199
#[inline]
200-
fn program(&mut self, offset: Ptr, buf: &[u8]) -> Result<(), Error> {
200+
fn program(&mut self, offset: u32, buf: &[u8]) -> Result<(), Error> {
201201
F::program(self, offset, buf)
202202
}
203203

@@ -217,7 +217,7 @@ pub trait FlashExt<'flash> {
217217
/// See [`ArenaExt::alloc()`](../../mem/arena/trait.ArenaExt.html#tymethod.alloc).
218218
fn read_object<'b: 'c, 'c, T>(
219219
self,
220-
offset: Ptr,
220+
offset: u32,
221221
arena: &'b dyn Arena,
222222
) -> Result<&'c T, Error>
223223
where
@@ -229,7 +229,7 @@ pub trait FlashExt<'flash> {
229229
/// See [`ArenaExt::alloc_slice()`](../../mem/arena/trait.ArenaExt.html#tymethod.alloc_slice).
230230
fn read_slice<'b: 'c, 'c, T>(
231231
self,
232-
offset: Ptr,
232+
offset: u32,
233233
n: usize,
234234
arena: &'b dyn Arena,
235235
) -> Result<&'c [T], Error>
@@ -241,15 +241,15 @@ pub trait FlashExt<'flash> {
241241
impl<'flash, F: Flash> FlashExt<'flash> for &'flash F {
242242
fn read_object<'b: 'c, 'c, T>(
243243
self,
244-
offset: Ptr,
244+
offset: u32,
245245
arena: &'b dyn Arena,
246246
) -> Result<&'c T, Error>
247247
where
248248
'flash: 'c,
249249
T: AsBytes + FromBytes + Copy,
250250
{
251251
let bytes = self.read_direct(
252-
Region::new(offset.address, mem::size_of::<T>() as u32),
252+
Region::new(offset, mem::size_of::<T>() as u32),
253253
arena,
254254
mem::align_of::<T>(),
255255
)?;
@@ -261,7 +261,7 @@ impl<'flash, F: Flash> FlashExt<'flash> for &'flash F {
261261

262262
fn read_slice<'b: 'c, 'c, T>(
263263
self,
264-
offset: Ptr,
264+
offset: u32,
265265
n: usize,
266266
arena: &'b dyn Arena,
267267
) -> Result<&'c [T], Error>
@@ -272,7 +272,7 @@ impl<'flash, F: Flash> FlashExt<'flash> for &'flash F {
272272
let bytes_requested =
273273
mem::size_of::<T>().checked_mul(n).ok_or(OutOfMemory)?;
274274
let bytes = self.read_direct(
275-
Region::new(offset.address, bytes_requested as u32),
275+
Region::new(offset, bytes_requested as u32),
276276
arena,
277277
mem::align_of::<T>(),
278278
)?;
@@ -302,9 +302,9 @@ unsafe impl<Bytes: AsRef<[u8]>> Flash for Ram<Bytes> {
302302
}
303303

304304
#[inline]
305-
fn read(&self, offset: Ptr, out: &mut [u8]) -> Result<(), Error> {
305+
fn read(&self, offset: u32, out: &mut [u8]) -> Result<(), Error> {
306306
out.copy_from_slice(self.read_direct(
307-
Region::new(offset.address, out.len() as u32),
307+
Region::new(offset, out.len() as u32),
308308
&OutOfMemory,
309309
1,
310310
)?);
@@ -317,7 +317,7 @@ unsafe impl<Bytes: AsRef<[u8]>> Flash for Ram<Bytes> {
317317
arena: &'b dyn Arena,
318318
align: usize,
319319
) -> Result<&'c [u8], Error> {
320-
let start = region.ptr.address as usize;
320+
let start = region.offset as usize;
321321
let end = start
322322
.checked_add(region.len as usize)
323323
.ok_or(Error::OutOfRange)?;
@@ -336,7 +336,7 @@ unsafe impl<Bytes: AsRef<[u8]>> Flash for Ram<Bytes> {
336336
Ok(buf)
337337
}
338338

339-
fn program(&mut self, _: Ptr, _: &[u8]) -> Result<(), Error> {
339+
fn program(&mut self, _: u32, _: &[u8]) -> Result<(), Error> {
340340
Err(Error::Locked)
341341
}
342342
}
@@ -360,9 +360,9 @@ unsafe impl<Bytes: AsRef<[u8]> + AsMut<[u8]>> Flash for RamMut<Bytes> {
360360
}
361361

362362
#[inline]
363-
fn read(&self, offset: Ptr, out: &mut [u8]) -> Result<(), Error> {
363+
fn read(&self, offset: u32, out: &mut [u8]) -> Result<(), Error> {
364364
out.copy_from_slice(self.read_direct(
365-
Region::new(offset.address, out.len() as u32),
365+
Region::new(offset, out.len() as u32),
366366
&OutOfMemory,
367367
1,
368368
)?);
@@ -375,7 +375,7 @@ unsafe impl<Bytes: AsRef<[u8]> + AsMut<[u8]>> Flash for RamMut<Bytes> {
375375
arena: &'b dyn Arena,
376376
align: usize,
377377
) -> Result<&'c [u8], Error> {
378-
let start = region.ptr.address as usize;
378+
let start = region.offset as usize;
379379
let end = start
380380
.checked_add(region.len as usize)
381381
.ok_or(Error::OutOfRange)?;
@@ -394,8 +394,8 @@ unsafe impl<Bytes: AsRef<[u8]> + AsMut<[u8]>> Flash for RamMut<Bytes> {
394394
Ok(buf)
395395
}
396396

397-
fn program(&mut self, offset: Ptr, buf: &[u8]) -> Result<(), Error> {
398-
let start = offset.address as usize;
397+
fn program(&mut self, offset: u32, buf: &[u8]) -> Result<(), Error> {
398+
let start = offset as usize;
399399
let end = start.checked_add(buf.len()).ok_or(Error::OutOfRange)?;
400400
if end > self.0.as_ref().len() {
401401
return Err(Error::OutOfRange);
@@ -450,7 +450,7 @@ impl<F: Flash> FlashIo<F> {
450450
/// Adapts this `FlashIo` to only read bytes out from the selected
451451
/// `Region`.
452452
pub fn reslice(&mut self, region: Region) {
453-
self.cursor = region.ptr.address;
453+
self.cursor = region.offset;
454454
self.len = region.end();
455455
}
456456
}
@@ -462,7 +462,7 @@ impl<F: Flash> io::Read for FlashIo<F> {
462462
}
463463

464464
self.flash
465-
.read(Ptr::new(self.cursor), out)
465+
.read(self.cursor, out)
466466
.map_err(|_| io::Error::Internal)?;
467467
self.cursor += out.len() as u32;
468468
Ok(())
@@ -481,7 +481,7 @@ impl<F: Flash> Iterator for FlashIo<F> {
481481
}
482482

483483
let mut byte = [0];
484-
if let Err(e) = self.flash.read(Ptr::new(self.cursor), &mut byte) {
484+
if let Err(e) = self.flash.read(self.cursor, &mut byte) {
485485
return Some(Err(e));
486486
}
487487
self.cursor += 1;
@@ -496,61 +496,34 @@ impl<F: Flash> io::Write for FlashIo<F> {
496496
}
497497

498498
self.flash
499-
.program(Ptr::new(self.cursor), buf)
499+
.program(self.cursor, buf)
500500
.map_err(|_| io::Error::Internal)?;
501501
self.cursor += buf.len() as u32;
502502
Ok(())
503503
}
504504
}
505505

506-
/// An abstract pointer into a [`Flash`] type.
506+
/// A region within a [`Flash`] type.
507507
///
508-
/// A `Ptr` needs to be used in conjunction with a [`Flash`]
509-
/// implementation to be read from or written to.
508+
/// A `Region` needs to be interpreted with respect to a [`Flash`]
509+
/// implementation; it is otherwise a dumb pointer-length pair.
510510
///
511511
/// [`Flash`]: trait.Flash.html
512512
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, AsBytes, FromBytes)]
513-
#[repr(transparent)]
514-
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
515-
pub struct Ptr {
516-
/// The abstract address of this pointer.
517-
pub address: u32,
518-
}
519-
520-
impl Ptr {
521-
/// Convenience method for creating a `Ptr` without having to use
522-
/// a struct literal.
523-
pub const fn new(address: u32) -> Self {
524-
Self { address }
525-
}
526-
}
527-
528-
/// A region within a [`Flash`] type.
529-
///
530-
/// Much like a [`Ptr`], a `Region` needs to be interpreted with
531-
/// respect to a [`Flash`] implementation.
532-
///
533-
/// [`Flash`]: trait.Flash.html
534-
/// [`Ptr`]: struct.Ptr.html
535-
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, AsBytes, FromBytes)]
536513
#[repr(C)]
537514
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
538515
pub struct Region {
539516
/// The base pointer for this slice.
540-
#[cfg_attr(feature = "serde", serde(flatten))]
541-
pub ptr: Ptr,
517+
pub offset: u32,
542518
/// The length of the slice, in bytes.
543519
pub len: u32,
544520
}
545521

546522
impl Region {
547523
/// Convenience method for creating a `Region` without having to use
548524
/// a struct literal.
549-
pub const fn new(ptr: u32, len: u32) -> Self {
550-
Self {
551-
ptr: Ptr::new(ptr),
552-
len,
553-
}
525+
pub const fn new(offset: u32, len: u32) -> Self {
526+
Self { offset, len }
554527
}
555528

556529
/// Returns a `Region` big enough to hold a `T`.
@@ -568,7 +541,7 @@ impl Region {
568541

569542
/// Returns the end address of `self`, pointing one past the end of it.
570543
pub fn end(self) -> u32 {
571-
self.ptr.address.saturating_add(self.len)
544+
self.offset.saturating_add(self.len)
572545
}
573546

574547
/// Returns a new `Region` that comes immediately after `self`, with the
@@ -582,14 +555,11 @@ impl Region {
582555
///
583556
/// Returns `None` if `sub` is not a subregion of `self`.
584557
pub fn subregion(self, sub: Region) -> Option<Self> {
585-
if sub.len.saturating_add(sub.ptr.address) > self.len {
558+
if sub.len.saturating_add(sub.offset) > self.len {
586559
return None;
587560
}
588561

589-
Some(Region::new(
590-
self.ptr.address.checked_add(sub.ptr.address)?,
591-
sub.len,
592-
))
562+
Some(Region::new(self.offset.checked_add(sub.offset)?, sub.len))
593563
}
594564

595565
/// Contracts `self` by dropping the first `n` bytes.
@@ -598,7 +568,7 @@ impl Region {
598568
/// occurs.
599569
pub fn skip(self, n: u32) -> Option<Self> {
600570
Some(Region::new(
601-
self.ptr.address.checked_add(n)?,
571+
self.offset.checked_add(n)?,
602572
self.len.checked_sub(n)?,
603573
))
604574
}
@@ -607,6 +577,6 @@ impl Region {
607577
///
608578
/// Returns `None` if `n` is greater than `self.len`.
609579
pub fn take(self, n: u32) -> Option<Self> {
610-
Some(Region::new(self.ptr.address, self.len.checked_sub(n)?))
580+
Some(Region::new(self.offset, self.len.checked_sub(n)?))
611581
}
612582
}

src/manifest/fpm.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ impl<'m, Provenance> Fpm<'m, Provenance> {
255255
pub fn unparse(&self, mut out: impl io::Write) -> Result<(), Error> {
256256
out.write_le(self.versions.len() as u32)?;
257257
for version in self.versions() {
258-
out.write_le(version.version_region.ptr.address)?;
258+
out.write_le(version.version_region.offset)?;
259259

260260
let signed_len: u16 = version
261261
.signed_region
@@ -288,11 +288,11 @@ impl<'m, Provenance> Fpm<'m, Provenance> {
288288
}
289289

290290
for slice in version.signed_region.iter() {
291-
out.write_le(slice.ptr.address)?;
291+
out.write_le(slice.offset)?;
292292
out.write_le(slice.len)?;
293293
}
294294
for slice in version.write_region.iter() {
295-
out.write_le(slice.ptr.address)?;
295+
out.write_le(slice.offset)?;
296296
out.write_le(slice.len)?;
297297
}
298298
out.write_bytes(&*version.signed_region_hash)?;

0 commit comments

Comments
 (0)