Parse and verify the fixed-order .afw container, add the CV1835 board module and an SSH transport with the stock update-daemon root stage, a read-only preflight, a held p1 backup, whole-partition read-back, live verification and network restore. Add the headless afw CLI and offline fixtures for good, tampered, wrong-board, truncated, traversal and oversize bundles. Co-Authored-By: DeepSeek Harness <noreply@deepseek.com>
407 lines
13 KiB
Rust
407 lines
13 KiB
Rust
// SPDX-License-Identifier: MIT
|
|
//! CV1835 board module: the only write is the 16 MiB p1 FIT.
|
|
//!
|
|
//! The vendor boot0, BL2, BL31 and U-Boot chain is preserved. The board plan is
|
|
//! read from the verified `.afw` manifest, not guessed at runtime.
|
|
|
|
use crate::md5::md5_hex;
|
|
use alkane_artifacts::afw::{
|
|
AfwImage, AfwManifest, AfwPartition, BootloaderPolicy, VerifiedAfw, CV1835_P1_SIZE,
|
|
};
|
|
use thiserror::Error;
|
|
|
|
pub const BOARD: &str = "cv1835";
|
|
pub const ARCH: &str = "arm64";
|
|
pub const P1_DEVICE: &str = "/dev/mmcblk0p1";
|
|
pub const MARKER: &str = "CVBOOT";
|
|
pub const UPDATE_DAEMON_PORT: u16 = 12887;
|
|
pub const GOLDEN_LBA_BYTES: u64 = 0x300000 * 512;
|
|
|
|
#[derive(Debug, Error)]
|
|
pub enum CvPlanError {
|
|
#[error("bundle is for {0}, not cv1835")]
|
|
WrongBoard(String),
|
|
#[error("bundle arch is {0}, cv1835 needs arm64")]
|
|
WrongArch(String),
|
|
#[error("cv1835 requires bootloader_policy preserve")]
|
|
BootloaderPolicy,
|
|
#[error("cv1835 bundle carries a bootloader member")]
|
|
BootloaderMember,
|
|
#[error("cv1835 bundle must install exactly p1")]
|
|
MissingP1,
|
|
#[error("cv1835 p1 is {0} bytes, not the 16 MiB padded FIT")]
|
|
P1Size(u64),
|
|
#[error("cv1835 p1 partition is not /dev/mmcblk0p1")]
|
|
P1Device,
|
|
#[error("cv1835 p1 partition must be 16 MiB, non-secure and written once")]
|
|
P1Geometry,
|
|
#[error("cv1835 p1 is not a bootable FIT: {0}")]
|
|
Fit(String),
|
|
#[error("manifest marker is empty")]
|
|
Marker,
|
|
#[error("manifest kernel_version is empty")]
|
|
KernelVersion,
|
|
#[error("manifest is not an OS payload")]
|
|
PayloadType,
|
|
#[error("manifest preflight is missing {0}")]
|
|
Preflight(String),
|
|
}
|
|
|
|
#[derive(Clone, Debug)]
|
|
pub struct Cv1835Plan {
|
|
pub image: AfwImage,
|
|
pub data: Vec<u8>,
|
|
pub partition: AfwPartition,
|
|
pub expected_md5: String,
|
|
pub expected_sha256: String,
|
|
pub marker: String,
|
|
pub kernel_version: String,
|
|
pub sdk_revision: String,
|
|
pub os_revision: String,
|
|
pub source_os: Vec<String>,
|
|
pub required_commands: Vec<String>,
|
|
pub min_free_bytes: u64,
|
|
pub golden_lba_bytes: u64,
|
|
}
|
|
|
|
impl Cv1835Plan {
|
|
pub fn from_afw(afw: &VerifiedAfw) -> Result<Self, CvPlanError> {
|
|
let manifest = &afw.manifest;
|
|
if manifest.board != BOARD {
|
|
return Err(CvPlanError::WrongBoard(manifest.board.clone()));
|
|
}
|
|
if manifest.arch != ARCH {
|
|
return Err(CvPlanError::WrongArch(manifest.arch.clone()));
|
|
}
|
|
if manifest.payload_type != "os" {
|
|
return Err(CvPlanError::PayloadType);
|
|
}
|
|
if manifest.bootloader_policy != BootloaderPolicy::Preserve {
|
|
return Err(CvPlanError::BootloaderPolicy);
|
|
}
|
|
if manifest
|
|
.images
|
|
.iter()
|
|
.any(|image| image.role == "bootloader")
|
|
{
|
|
return Err(CvPlanError::BootloaderMember);
|
|
}
|
|
if manifest.marker.is_empty() {
|
|
return Err(CvPlanError::Marker);
|
|
}
|
|
if manifest.kernel_version.is_empty() {
|
|
return Err(CvPlanError::KernelVersion);
|
|
}
|
|
|
|
let install: Vec<&AfwImage> = manifest
|
|
.images
|
|
.iter()
|
|
.filter(|image| image.install)
|
|
.collect();
|
|
if install.len() != 1 || install[0].name != "p1" {
|
|
return Err(CvPlanError::MissingP1);
|
|
}
|
|
let image = install[0].clone();
|
|
if image.format != "fit" {
|
|
return Err(CvPlanError::Fit("format is not fit".to_owned()));
|
|
}
|
|
if image.size != CV1835_P1_SIZE {
|
|
return Err(CvPlanError::P1Size(image.size));
|
|
}
|
|
let (_, data) = afw.install_image().ok_or(CvPlanError::MissingP1)?;
|
|
if !data.starts_with(b"\xd0\x0d\xfe\xed") {
|
|
return Err(CvPlanError::Fit("missing FDT/FIT magic".to_owned()));
|
|
}
|
|
if !contains(data, b"config@cv1835_miner_emmc") {
|
|
return Err(CvPlanError::Fit(
|
|
"missing config@cv1835_miner_emmc".to_owned(),
|
|
));
|
|
}
|
|
if contains(data, b"signature@") {
|
|
return Err(CvPlanError::Fit("carries signature@ nodes".to_owned()));
|
|
}
|
|
|
|
let partition = manifest
|
|
.partitions
|
|
.iter()
|
|
.find(|part| part.name == "p1")
|
|
.ok_or(CvPlanError::P1Device)?
|
|
.clone();
|
|
if partition.device != P1_DEVICE {
|
|
return Err(CvPlanError::P1Device);
|
|
}
|
|
if partition.size != CV1835_P1_SIZE
|
|
|| partition.secure
|
|
|| !partition.boot_critical
|
|
|| partition.write_order != 1
|
|
{
|
|
return Err(CvPlanError::P1Geometry);
|
|
}
|
|
if manifest
|
|
.partitions
|
|
.iter()
|
|
.any(|part| part.write_order > 0 && part.name != "p1")
|
|
{
|
|
return Err(CvPlanError::MissingP1);
|
|
}
|
|
|
|
let source_os = json_strings(&manifest.preflight, "source_os");
|
|
let required_commands = json_strings(&manifest.preflight, "required_commands");
|
|
let min_free_bytes = manifest
|
|
.preflight
|
|
.get("min_free_bytes")
|
|
.and_then(|value| value.as_u64())
|
|
.unwrap_or(32 * 1024 * 1024);
|
|
let golden_lba_bytes = manifest
|
|
.recovery
|
|
.get("golden_lba_bytes")
|
|
.and_then(|value| value.as_u64())
|
|
.unwrap_or(GOLDEN_LBA_BYTES);
|
|
|
|
Ok(Self {
|
|
expected_md5: md5_hex(data),
|
|
expected_sha256: image.sha256.clone(),
|
|
image,
|
|
data: data.to_vec(),
|
|
partition,
|
|
marker: manifest.marker.clone(),
|
|
kernel_version: manifest.kernel_version.clone(),
|
|
sdk_revision: manifest.sdk_revision.clone(),
|
|
os_revision: manifest.os_revision.clone(),
|
|
source_os,
|
|
required_commands,
|
|
min_free_bytes,
|
|
golden_lba_bytes,
|
|
})
|
|
}
|
|
|
|
pub fn render(&self) -> String {
|
|
format!(
|
|
"board cv1835 arch arm64\n\
|
|
partition {} {} {} B secure=false boot_critical=true write_order={}\n\
|
|
image {} {} {} B sha256 {}\n\
|
|
image md5 {}\n\
|
|
bootloader preserve (vendor boot0/BL2/BL31/U-Boot kept)\n\
|
|
boot switch partition-overwrite (p1 is the switch)\n\
|
|
marker {} kernel {} sdk {} os {}",
|
|
self.partition.name,
|
|
self.partition.device,
|
|
self.partition.size,
|
|
self.partition.write_order,
|
|
self.image.name,
|
|
self.image.file,
|
|
self.image.size,
|
|
self.image.sha256,
|
|
self.expected_md5,
|
|
self.marker,
|
|
self.kernel_version,
|
|
short_revision(&self.sdk_revision),
|
|
short_revision(&self.os_revision),
|
|
)
|
|
}
|
|
}
|
|
|
|
fn short_revision(revision: &str) -> &str {
|
|
revision.get(..12).unwrap_or(revision)
|
|
}
|
|
|
|
fn contains(haystack: &[u8], needle: &[u8]) -> bool {
|
|
haystack
|
|
.windows(needle.len())
|
|
.any(|window| window == needle)
|
|
}
|
|
|
|
fn json_strings(value: &serde_json::Value, key: &str) -> Vec<String> {
|
|
value
|
|
.get(key)
|
|
.and_then(|item| item.as_array())
|
|
.map(|items| {
|
|
items
|
|
.iter()
|
|
.filter_map(|item| item.as_str().map(str::to_owned))
|
|
.collect()
|
|
})
|
|
.unwrap_or_default()
|
|
}
|
|
|
|
pub fn manifest_from_json(value: serde_json::Value) -> Result<AfwManifest, serde_json::Error> {
|
|
serde_json::from_value(value)
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use alkane_artifacts::afw::AfwMember;
|
|
use serde_json::json;
|
|
use sha2::{Digest, Sha256};
|
|
|
|
const P1: usize = CV1835_P1_SIZE as usize;
|
|
const REV: &str = "0000000000000000000000000000000000000000";
|
|
|
|
fn sha_hex(data: &[u8]) -> String {
|
|
Sha256::digest(data)
|
|
.iter()
|
|
.map(|byte| format!("{byte:02x}"))
|
|
.collect()
|
|
}
|
|
|
|
fn p1_image() -> Vec<u8> {
|
|
let mut fit = vec![0_u8; P1];
|
|
fit[..4].copy_from_slice(b"\xd0\x0d\xfe\xed");
|
|
let marker = b"config@cv1835_miner_emmc";
|
|
fit[0x100..0x100 + marker.len()].copy_from_slice(marker);
|
|
fit
|
|
}
|
|
|
|
fn manifest_value(p1: &[u8]) -> serde_json::Value {
|
|
json!({
|
|
"schema": 2,
|
|
"format": "afw",
|
|
"board": "cv1835",
|
|
"arch": "arm64",
|
|
"version": "0.1.0",
|
|
"build_date": "2026-11-04T00:00:00Z",
|
|
"sdk_revision": REV,
|
|
"os_revision": REV,
|
|
"payload_type": "os",
|
|
"bootloader_policy": "preserve",
|
|
"signature": {"type": "unsigned", "member": null, "public_key_hex": null},
|
|
"partitions": [
|
|
{"name": "p1", "device": "/dev/mmcblk0p1", "offset": 0,
|
|
"size": CV1835_P1_SIZE, "secure": false, "boot_critical": true,
|
|
"write_order": 1, "image": "p1"},
|
|
{"name": "boot0", "device": "/dev/mmcblk0boot0", "offset": 0,
|
|
"size": 4194304, "secure": true, "boot_critical": true,
|
|
"write_order": 0, "image": null}
|
|
],
|
|
"images": [
|
|
{"name": "p1", "file": "payload/p1.fit", "role": "boot",
|
|
"format": "fit", "size": p1.len(),
|
|
"sha256": sha_hex(p1),
|
|
"partition": "p1", "install": true}
|
|
],
|
|
"boot_switch": {"method": "partition-overwrite"},
|
|
"preflight": {"required_commands": ["dd", "md5sum", "sync"],
|
|
"source_os": ["stock-bitmain", "vnish", "braiins", "ouros", "alkane"],
|
|
"min_free_bytes": 33554432},
|
|
"recovery": {"network": "afw restore", "golden_lba_bytes": GOLDEN_LBA_BYTES},
|
|
"marker": "CVBOOT",
|
|
"kernel_version": "6.18.48"
|
|
})
|
|
}
|
|
|
|
fn afw(p1: &[u8]) -> VerifiedAfw {
|
|
let manifest = manifest_from_json(manifest_value(p1)).unwrap();
|
|
VerifiedAfw {
|
|
manifest,
|
|
members: vec![AfwMember {
|
|
name: "payload/p1.fit".to_owned(),
|
|
data: p1.to_vec(),
|
|
}],
|
|
manifest_sha256: "0".repeat(64),
|
|
archive_sha256: "0".repeat(64),
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn accepts_the_cv_plan() {
|
|
let data = p1_image();
|
|
let plan = Cv1835Plan::from_afw(&afw(&data)).unwrap();
|
|
assert_eq!(plan.partition.device, "/dev/mmcblk0p1");
|
|
assert_eq!(plan.image.size, CV1835_P1_SIZE);
|
|
assert_eq!(plan.marker, "CVBOOT");
|
|
assert!(plan
|
|
.render()
|
|
.contains("partition p1 /dev/mmcblk0p1 16777216"));
|
|
}
|
|
|
|
#[test]
|
|
fn refuses_a_bundle_for_another_board() {
|
|
let data = p1_image();
|
|
let mut verified = afw(&data);
|
|
verified.manifest.board = "zynq-7010".to_owned();
|
|
assert!(matches!(
|
|
Cv1835Plan::from_afw(&verified),
|
|
Err(CvPlanError::WrongBoard(_))
|
|
));
|
|
}
|
|
|
|
#[test]
|
|
fn refuses_include_bootloader_policy() {
|
|
let data = p1_image();
|
|
let mut verified = afw(&data);
|
|
verified.manifest.bootloader_policy = BootloaderPolicy::Include;
|
|
assert!(matches!(
|
|
Cv1835Plan::from_afw(&verified),
|
|
Err(CvPlanError::BootloaderPolicy)
|
|
));
|
|
}
|
|
|
|
#[test]
|
|
fn refuses_a_bootloader_member() {
|
|
let data = p1_image();
|
|
let mut verified = afw(&data);
|
|
verified.manifest.images.push(AfwImage {
|
|
name: "u-boot".to_owned(),
|
|
file: "payload/u-boot.img".to_owned(),
|
|
role: "bootloader".to_owned(),
|
|
format: "uimage".to_owned(),
|
|
size: 4,
|
|
sha256: "0".repeat(64),
|
|
partition: None,
|
|
install: false,
|
|
});
|
|
assert!(matches!(
|
|
Cv1835Plan::from_afw(&verified),
|
|
Err(CvPlanError::BootloaderMember)
|
|
));
|
|
}
|
|
|
|
#[test]
|
|
fn refuses_a_short_p1() {
|
|
let data = vec![0_u8; 1024];
|
|
let verified = afw(&data);
|
|
assert!(matches!(
|
|
Cv1835Plan::from_afw(&verified),
|
|
Err(CvPlanError::P1Size(1024))
|
|
));
|
|
}
|
|
|
|
#[test]
|
|
fn refuses_a_secure_p1() {
|
|
let data = p1_image();
|
|
let mut verified = afw(&data);
|
|
verified.manifest.partitions[0].secure = true;
|
|
assert!(matches!(
|
|
Cv1835Plan::from_afw(&verified),
|
|
Err(CvPlanError::P1Geometry)
|
|
));
|
|
}
|
|
|
|
#[test]
|
|
fn refuses_a_wrong_device() {
|
|
let data = p1_image();
|
|
let mut verified = afw(&data);
|
|
verified.manifest.partitions[0].device = "/dev/mmcblk0p3".to_owned();
|
|
assert!(matches!(
|
|
Cv1835Plan::from_afw(&verified),
|
|
Err(CvPlanError::P1Device)
|
|
));
|
|
}
|
|
|
|
#[test]
|
|
fn refuses_a_fit_without_the_board_config() {
|
|
let mut data = p1_image();
|
|
for byte in data.iter_mut() {
|
|
if *byte == b'c' {
|
|
*byte = b'x';
|
|
}
|
|
}
|
|
let verified = afw(&data);
|
|
assert!(matches!(
|
|
Cv1835Plan::from_afw(&verified),
|
|
Err(CvPlanError::Fit(_))
|
|
));
|
|
}
|
|
}
|