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
impl OTAImage
Sourcepub fn get_subimages(&self) -> &[SubImage]
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.
Sourcepub fn get_subimages_mut(&mut self) -> &mut [SubImage]
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.
Sourcepub fn get_subimage(&self, index: usize) -> Option<&SubImage>
pub fn get_subimage(&self, index: usize) -> Option<&SubImage>
Sourcepub fn get_subimage_mut(&mut self, index: usize) -> Option<&mut SubImage>
pub fn get_subimage_mut(&mut self, index: usize) -> Option<&mut SubImage>
Sourcepub fn add_subimage(&mut self, subimage: SubImage)
pub fn add_subimage(&mut self, subimage: SubImage)
Sourcepub fn rem_subimage_at(&mut self, index: usize)
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.
Sourcepub fn get_ota_signature(&self) -> &[u8; 32]
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.
Sourcepub fn get_public_key(&self, index: u8) -> DataRefType<'_, 32>
pub fn get_public_key(&self, index: u8) -> DataRefType<'_, 32>
Source§impl OTAImage
impl OTAImage
Sourcepub fn build_ota_signature(&self, key: Option<&[u8]>) -> Result<Vec<u8>, Error>
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 beNone
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
isNone
).
Sourcepub fn ota_signature_from_stream<R>(
reader: &mut R,
algo: HashAlgo,
key: Option<&[u8]>,
) -> Result<Vec<u8>, Error>
pub fn ota_signature_from_stream<R>( reader: &mut R, algo: HashAlgo, key: Option<&[u8]>, ) -> Result<Vec<u8>, Error>
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 implementsio::Read
andio::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 beNone
.
§Returns:
Result<Vec<u8>, crate::error::Error>
: Returns the computed signature as aVec<u8>
, or an error if there is an issue reading the data or computing the hash.
Sourcepub fn set_ota_signature(&mut self, signature: &[u8])
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.
Sourcepub fn build_checksum(&self) -> Result<u32, Error>
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.
Sourcepub fn update_checksum(&mut self) -> Result<(), Error>
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);
}
Sourcepub fn update_ota_signature(&mut self, key: Option<&[u8]>) -> Result<(), Error>
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.
Sourcepub fn checksum_from_buffer(buf: &[u8]) -> u32
pub fn checksum_from_buffer(buf: &[u8]) -> u32
Sourcepub fn checksum_from_stream<R>(reader: &mut R) -> Result<u32, Error>
pub fn checksum_from_stream<R>(reader: &mut R) -> Result<u32, Error>
Calculates a checksum from a stream by reading the content into a buffer and computing its checksum.
§Arguments:
reader
: A reader that implementsio::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 FromStream for OTAImage
impl FromStream for OTAImage
Source§fn read_from<R>(&mut self, reader: &mut R) -> Result<(), Error>
fn read_from<R>(&mut self, reader: &mut R) -> Result<(), Error>
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 bothio::Read
andio::Seek
.
§Returns:
Result<(), Error>
: ReturnsOk(())
if the data is read and parsed successfully, or anError
if something goes wrong (e.g., invalid format, stream read errors).