amebazii::types::image::ota

Struct OTAImage

Source
pub struct OTAImage {
    pub keyblock: KeyBlock,
    pub checksum: Option<u32>,
    /* private fields */
}
Expand description

Represents an OTA (Over-The-Air) image.

An OTAImage is used in the context of firmware updates, where the image consists of multiple subimages (representing different sections of the firmware), each of which may be signed and encrypted. The keyblock holds encrypted keys, public_keys are used for verifying signatures, and checksum ensures data integrity.

Note: The encryption and signature verification are currently are using the hash key specified in the partition table!

Fields§

§keyblock: KeyBlock

The key block containing cryptographic keys for encryption and signature verification.

§checksum: Option<u32>

A checksum value for verifying the integrity of the OTA image.

Implementations§

Source§

impl OTAImage

Source

pub fn get_subimages(&self) -> &[SubImage]

Returns a slice of the subimages contained in the OTA image.

§Returns:
  • A reference to a slice containing all the subimages.
Source

pub fn get_subimages_mut(&mut self) -> &mut [SubImage]

Returns a mutable slice of the subimages contained in the OTA image.

§Returns:
  • A mutable reference to a slice containing all the subimages.
Source

pub fn get_subimage(&self, index: usize) -> Option<&SubImage>

Retrieves a specific subimage by its index.

§Arguments:
  • index: The index of the subimage to retrieve.
§Returns:
  • Some(SubImage) if the subimage exists at the given index, None otherwise.
Source

pub fn get_subimage_mut(&mut self, index: usize) -> Option<&mut SubImage>

Retrieves a mutable reference to a specific subimage by its index.

§Arguments:
  • index: The index of the subimage to retrieve.
§Returns:
  • Some(&mut SubImage) if the subimage exists at the given index, None otherwise.
Source

pub fn add_subimage(&mut self, subimage: SubImage)

Adds a new subimage to the OTA image.

§Arguments:
  • subimage: The SubImage to add to the OTA image.
Source

pub fn rem_subimage_at(&mut self, index: usize)

Removes a subimage from the OTA image at the specified index.

§Arguments:
  • index: The index of the subimage to remove.
Source

pub fn get_ota_signature(&self) -> &[u8; 32]

Returns the encryption public key from the keyblock, which is used for OTA signature verification.

§Returns:
  • A reference to the encryption public key (32 bytes) used for signature verification.
Source

pub fn get_public_key(&self, index: u8) -> DataRefType<'_, 32>

Retrieves a specific public key used for signature verification from the OTA image.

§Arguments:
  • index: The index (0-4) of the public key to retrieve.
§Returns:
  • A reference to the public key at the specified index, if it exists.
Source§

impl OTAImage

Source

pub fn build_ota_signature(&self, key: Option<&[u8]>) -> Result<Vec<u8>, Error>

Builds the OTA image signature, which is the hash result of the first SubImage’s header.

§Arguments:
  • key: An optional key to be used in the hash calculation (may be None if no key is provided).
§Returns:
  • Result<Vec<u8>, crate::error::Error>: The computed signature as a vector of bytes on success, or an error if the computation cannot be completed (e.g., fst.hash_algo is None).
Source

pub fn ota_signature_from_stream<R>( reader: &mut R, algo: HashAlgo, key: Option<&[u8]>, ) -> Result<Vec<u8>, Error>
where R: Read + Seek,

Reads the OTA signature from a stream and computes its hash using a specified algorithm.

This function reads the OTAImage signature data from the provided reader, computes its hash using the specified HashAlgo, and returns the computed signature.

The function assumes that the data read corresponds to the “OTA signature” section of the OTAImage format, which is typically the first part of the image.

§Arguments:
  • reader: A mutable reference to a reader that implements io::Read and io::Seek. This will be used to read the OTA signature data.
  • algo: The hash algorithm to use for computing the signature (e.g., SHA-256).
  • key: An optional key to be used by certain hash algorithms (e.g., for HMAC). If the algorithm does not require a key, this can be None.
§Returns:
  • Result<Vec<u8>, crate::error::Error>: Returns the computed signature as a Vec<u8>, or an error if there is an issue reading the data or computing the hash.
Source

pub fn set_ota_signature(&mut self, signature: &[u8])

Sets the OTA image signature, specifically the public encryption key in the keyblock.

§Arguments:
  • signature: The signature (encryption public key) to set, which will replace the existing public key.
Source

pub fn build_checksum(&self) -> Result<u32, Error>

Computes the checksum for the OTA image by writing it to a buffer.

This method serializes the current OTAImage object into a byte buffer and then calculates the checksum for the serialized data. The checksum is returned as a 32-bit unsigned integer.

§Errors

This function returns an error if the writing process to the buffer fails.

Source

pub fn update_checksum(&mut self) -> Result<(), Error>

Updates the checksum field of the OTA image.

This method resets the checksum field to None and then calculates and sets the new checksum by calling build_checksum. It ensures that the checksum field is always up-to-date.

§Example
let mut ota_image = /* ... */;
if let Err(e) = ota_image.update_checksum() {
    eprintln!("Failed to update checksum: {}", e);
}
Source

pub fn update_ota_signature(&mut self, key: Option<&[u8]>) -> Result<(), Error>

Updates the OTA image signature using the provided public key.

This method generates a new OTA signature and updates the keyblock field with the signature. If a key is provided, it will be used in the signing process; otherwise, the default behavior is applied.

§Arguments
  • key - An optional reference to a byte slice (&[u8]) representing the public key.
§Errors

Returns an error if the signing process fails.

Source

pub fn checksum_from_buffer(buf: &[u8]) -> u32

Calculates a checksum from a byte buffer by summing all the byte values and applying a bitmask.

§Arguments:
  • buf: The byte buffer to compute the checksum from.
§Returns:
  • i32: The computed checksum as a 32-bit signed integer.
Source

pub fn checksum_from_stream<R>(reader: &mut R) -> Result<u32, Error>
where R: Read + Seek,

Calculates a checksum from a stream by reading the content into a buffer and computing its checksum.

§Arguments:
  • reader: A reader that implements io::Read + io::Seek from which the content will be read.
§Returns:
  • Result<i32, Error>: The checksum computed from the stream as a 32-bit signed integer, or an error if the reading fails.

Trait Implementations§

Source§

impl Debug for OTAImage

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for OTAImage

Source§

fn default() -> Self

Creates a default OTAImage with an empty keyblock, no public keys, no subimages, and a checksum of -1.

Source§

impl FromStream for OTAImage

Source§

fn read_from<R>(&mut self, reader: &mut R) -> Result<(), Error>
where R: Read + Seek,

Reads an OTAImage from a binary stream.

This function assumes that the provided reader is positioned correctly and that the stream contains the expected data format for the OTAImage struct.

§Arguments:
  • reader: A mutable reference to a reader that implements both io::Read and io::Seek.
§Returns:
  • Result<(), Error>: Returns Ok(()) if the data is read and parsed successfully, or an Error if something goes wrong (e.g., invalid format, stream read errors).
Source§

impl ToStream for OTAImage

Source§

fn write_to<W>(&self, writer: &mut W) -> Result<(), Error>
where W: Write + Seek,

Writes an OTAImage to a binary stream.

§Arguments:
  • writer: A mutable reference to a writer that implements both io::Write and io::Seek.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.