Repository: google/OpenSK
File: bootloader/src/main.rs (Rust)

Code before:
    version: u64,
    address: u32,
}

/// Reads the metadata from a flash page.
impl From<Page> for Metadata {
    fn from(page: Page) -> Self {
        Metadata {
            checksum: page[0..32].try_into().unwrap(),
            _signature: page[32..96].try_into().unwrap(),
            version: LittleEndian::read_u64(&page[140..][..8]),
            address: LittleEndian::read_u32(&page[148..][..4]),
        }
    }
}

/// Location of a firmware partition's data.
struct BootPartition {
    firmware_address: usize,
    metadata_address: usize,
}

impl BootPartition {
    const FIRMWARE_LENGTH: usize = 0x00040000;

    /// Reads the metadata, returns the timestamp if all checks pass.
    pub fn read_version(&self) -> Result<u64, ()> {
        let metadata_page = unsafe { read_page(self.metadata_address) };
        let hash_value = self.compute_upgrade_hash(&metadata_page);
        let metadata = Metadata::from(metadata_page);
        if self.firmware_address != metadata.address as usize {
            #[cfg(debug_assertions)]
            rprintln!(
                "Partition address mismatch: expected 0x{:08X}, metadata 0x{:08X}",
                self.firmware_address,
                metadata.address as usize
            );
            return Err(());
        }
        if hash_value != metadata.checksum {
            #[cfg(debug_assertions)]
            rprintln!("Hash mismatch");
            return Err(());
        }
        Ok(metadata.version)
    }

    /// Computes the SHA256 of metadata information and partition data.
    ///
    /// Assumes that firmware address and length are divisible by the page size.
    /// This is the hardware implementation on the cryptocell.

Review comment (suggestion):
```suggestion
    /// Reads the metadata, returns the firmware version if all checks pass.
```

Code after:
    _signature: [u8; 64],
    version: u64,
    address: u32,
}

/// Reads the metadata from a flash page.
impl From<Page> for Metadata {
    fn from(page: Page) -> Self {
        Metadata {
            checksum: page[0..32].try_into().unwrap(),
            _signature: page[32..96].try_into().unwrap(),
            version: LittleEndian::read_u64(&page[METADATA_SIGN_OFFSET..][..8]),
            address: LittleEndian::read_u32(&page[METADATA_SIGN_OFFSET + 8..][..4]),
        }
    }
}

/// Location of a firmware partition's data.
struct BootPartition {
    firmware_address: usize,
    metadata_address: usize,
}

impl BootPartition {
    const FIRMWARE_LENGTH: usize = 0x00040000;

    /// Reads the metadata, returns the firmware version if all checks pass.
    pub fn read_version(&self) -> Result<u64, ()> {
        let metadata_page = unsafe { read_page(self.metadata_address) };
        let hash_value = self.compute_upgrade_hash(&metadata_page);
        let metadata = Metadata::from(metadata_page);
        if self.firmware_address != metadata.address as usize {
            #[cfg(debug_assertions)]
            rprintln!(
                "Partition address mismatch: expected 0x{:08X}, metadata 0x{:08X}",
                self.firmware_address,
                metadata.address as usize
            );
            return Err(());
        }
        if hash_value != metadata.checksum {
            #[cfg(debug_assertions)]
            rprintln!("Hash mismatch");
            return Err(());
        }
        Ok(metadata.version)
    }

    /// Computes the SHA256 of metadata information and partition data.
    ///
    /// Assumes that firmware address and length are divisible by the page size.