amebazii/types/image/mod.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
use super::{FromStream, ToStream};
use crate::error::Error;
use std::io;
pub mod boot;
pub use boot::BootImage;
pub mod ota;
pub use ota::{OTAImage, SubImage};
pub mod pt;
pub use pt::{PartTab, PartitionTableImage, Record, TrapConfig};
pub type RawImage = Vec<u8>;
/// A generic enum representing either encrypted or plain data.
///
/// The `EncryptedOr` enum is used to differentiate between encrypted data and unencrypted (plain) data.
/// It allows to store either type of data in the same structure while providing methods to safely access
/// or mutate the contents, depending on whether the data is encrypted or not.
#[derive(Debug)]
pub enum EncryptedOr<T> {
/// Contains encrypted data as a vector of bytes.
Encrypted(Vec<u8>),
/// Contains plain, unencrypted data of type `T`.
Plain(T),
}
impl<T> AsRef<T> for EncryptedOr<T> {
/// Returns a reference to the plain data if available.
///
/// # Panics
/// Panics if the data is encrypted, as the method expects plain data.
fn as_ref(&self) -> &T {
match self {
EncryptedOr::Encrypted(_) => {
panic!("Cannot get reference to encrypted data when plain is expected")
}
EncryptedOr::Plain(t) => t,
}
}
}
impl<T> AsRef<[u8]> for EncryptedOr<T> {
/// Returns a reference to the encrypted data if available.
///
/// # Panics
/// Panics if the data is plain, as the method expects encrypted data.
fn as_ref(&self) -> &[u8] {
match self {
EncryptedOr::Encrypted(v) => v,
EncryptedOr::Plain(_) => {
panic!("Cannot get reference to plain data when encrypted is expected")
}
}
}
}
impl<T> EncryptedOr<T> {
/// Returns `true` if the data is encrypted.
///
/// This method allows checking if the current instance of `EncryptedOr` contains encrypted data.
pub fn is_encrypted(&self) -> bool {
match self {
EncryptedOr::Encrypted(_) => true,
EncryptedOr::Plain(_) => false,
}
}
/// Returns `true` if the data is plain.
///
/// This method allows checking if the current instance of `EncryptedOr` contains plain (unencrypted) data.
pub fn is_plain(&self) -> bool {
match self {
EncryptedOr::Encrypted(_) => false,
EncryptedOr::Plain(_) => true,
}
}
pub fn unwrap(self) -> T {
match self {
EncryptedOr::Encrypted(_) => panic!("Attempted to unwrap encrypted data"),
EncryptedOr::Plain(t) => t,
}
}
}
impl<T> AsMut<T> for EncryptedOr<T> {
/// Returns a mutable reference to the plain data if available.
///
/// # Panics
/// Panics if the data is encrypted, as the method expects plain data.
fn as_mut(&mut self) -> &mut T {
match self {
EncryptedOr::Encrypted(_) => {
panic!("Cannot get mutable reference to encrypted data when plain is expected")
}
EncryptedOr::Plain(t) => t,
}
}
}
impl<T> AsMut<[u8]> for EncryptedOr<T> {
/// Returns a mutable reference to the encrypted data if available.
///
/// # Panics
/// Panics if the data is plain, as the method expects encrypted data.
fn as_mut(&mut self) -> &mut [u8] {
match self {
EncryptedOr::Encrypted(v) => v,
EncryptedOr::Plain(_) => {
panic!("Cannot get mutable reference to encrypted data when plain is expected")
}
}
}
}
impl<T: ToStream> ToStream for EncryptedOr<T> {
/// Writes the data to a stream, either encrypted or plain.
///
/// This method is used to serialize the data into a stream. If the data is encrypted, it writes the encrypted byte vector,
/// otherwise it serializes the plain data of type `T`.
fn write_to<W>(&self, writer: &mut W) -> Result<(), Error>
where
W: io::Write + io::Seek,
{
match self {
EncryptedOr::Encrypted(v) => writer.write_all(v)?,
EncryptedOr::Plain(t) => return t.write_to(writer),
}
Ok(())
}
}
impl<T: ToStream> ToStream for EncryptedOr<Vec<T>> {
fn write_to<W>(&self, writer: &mut W) -> Result<(), Error>
where
W: io::Write + io::Seek,
{
match self {
EncryptedOr::Encrypted(v) => writer.write_all(v)?,
EncryptedOr::Plain(t) => {
for item in t {
item.write_to(writer)?;
}
}
}
Ok(())
}
}
impl<T: FromStream> FromStream for EncryptedOr<T> {
/// Reads the data from a stream, either encrypted or plain.
///
/// This method deserializes the data from a stream. If the data is encrypted, it reads the encrypted byte vector,
/// otherwise it reads the plain data of type `T`.
fn read_from<R>(&mut self, reader: &mut R) -> Result<(), Error>
where
R: io::Read + io::Seek,
{
match self {
EncryptedOr::Encrypted(v) => reader.read_exact(v)?,
EncryptedOr::Plain(t) => return t.read_from(reader),
}
Ok(())
}
}
/// A trait that provides common functionality for image-like objects,
/// such as computing and setting the segment size and signature.
pub trait AsImage {
/// Computes the segment size for the image.
///
/// The segment size typically represents the total size of the image segment,
/// including all of its components (e.g., header, records, user data, etc.).
///
/// # Returns:
/// - `u32` representing the segment size.
///
/// # Example:
/// ```rust
/// let segment_size = image.build_segment_size();
/// ```
fn build_segment_size(&self) -> u32;
/// Sets the segment size for the image.
///
/// This method allows setting the segment size, typically after calculating
/// it or modifying the image in some way.
///
/// # Arguments:
/// - `size`: The new segment size to set.
///
/// # Example:
/// ```rust
/// image.set_segment_size(1024);
/// ```
fn set_segment_size(&mut self, size: u32);
/// Computes the signature for the image using the provided key.
///
/// The signature is usually a hash or HMAC generated from the image data and
/// a secret key, often used for verification or authentication purposes.
///
/// # Arguments:
/// - `key`: The key used to compute the signature.
///
/// # Returns:
/// - `Result<Vec<u8>, crate::error::Error>`: The signature as a `Vec<u8>`, or an error.
///
/// # Example:
/// ```rust
/// let signature = image.build_signature(&key);
/// ```
fn build_signature(&self, key: Option<&[u8]>) -> Result<Vec<u8>, crate::error::Error>;
/// Sets the signature for the image.
///
/// This method allows setting the signature after computing it or for some
/// validation process.
///
/// # Arguments:
/// - `signature`: The computed signature to set.
///
/// # Example:
/// ```rust
/// image.set_signature(&signature);
/// ```
fn set_signature(&mut self, signature: &[u8]);
}
/// Builds the signature for a given image.
///
/// This function uses the `build_signature` method from the `AsImage` trait to generate
/// the signature for the image using the provided key.
///
/// # Arguments:
/// - `image`: The image-like object that implements `AsImage`.
/// - `key`: The key used to compute the signature.
///
/// # Returns:
/// - `Result<Vec<u8>, crate::error::Error>`: The computed signature.
pub fn build_default_signature<I>(
image: &I,
key: Option<&[u8]>,
) -> Result<Vec<u8>, crate::error::Error>
where
I: AsImage,
{
image.build_signature(key)
}
/// Sets the signature for a given image.
///
/// This function computes the signature using `build_default_signature` and then sets
/// the signature for the image using `set_signature`.
///
/// # Arguments:
/// - `image`: The image-like object that implements `AsImage`.
/// - `key`: The key used to compute the signature.
///
/// # Returns:
/// - `Result<(), crate::error::Error>`: An empty result on success, or an error.
pub fn set_default_signature<I>(
image: &mut I,
key: Option<&[u8]>,
) -> Result<(), crate::error::Error>
where
I: AsImage,
{
let signature = build_default_signature(image, key)?;
image.set_signature(&signature);
Ok(())
}
/// Builds the segment size for a given image.
///
/// # Arguments:
/// - `image`: The image-like object that implements `AsImage`.
///
/// # Returns:
/// - `u32`: The computed segment size.
pub fn build_segment_size<I>(image: &I) -> u32
where
I: AsImage,
{
image.build_segment_size()
}
/// Sets the segment size for a given image.
///
/// # Arguments:
/// - `image`: The image-like object that implements `AsImage`.
///
/// # Returns:
/// - `()`: An empty result on success.
pub fn set_default_segment_size<I>(image: &mut I)
where
I: AsImage,
{
image.set_segment_size(image.build_segment_size())
}