Add am335x-stock (tier-3) from-stock flash support to the toolkit
The am335x flash module now selects a per-tier Profile from the bundle's board: tier 2 (am335x, preserve) or tier 3 (am335x-stock, include). The byte-write engine, preflight and readback are shared; only the policy, preserved set, 12-partition map (mtd4 becomes a write target) and image list differ. - boards/am335x.rs: Profile/profile_for/owns_board; MTD_MAP_STOCK (mtd4 writable, kernel at write_order 4 so U-Boot lands first); from_afw accepts am335x-stock, permits exactly the u-boot bootloader member, still rejects any SPL member and a u-boot member on tier 2; check_uimage expects firmware type 5 for u-boot; the plan carries its board + preserved set; render is tier-aware. - restore() takes the preserved set per profile; SPL copies (mtd0-3) are never a restore target on any tier; mtd4 is restorable on tier 3. - alkane-cli: route am335x-stock to the module (plan/preflight/install); restore is profile-driven and adds --held-uboot (mtd4) for the from-stock set. - alkane-artifacts: board_profile knows am335x-stock (arm, include). - tests: tier-3 from_afw acceptance (4 targets, U-Boot before kernel, mtd4 not preserved), wrong-U-Boot-type and SPL-write-target rejection, and a tier-3 restore test (mtd4 allowed, SPL refused). Full flash suite 41 green; tier-2 and cv1835 unchanged. The real bundle parses and verifies through the CLI. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01B1W6qHxYpv7yf7Ah57g4rQ
This commit is contained in:
@@ -71,6 +71,9 @@ pub struct BoardProfile {
|
||||
pub fn board_profile(board: &str) -> Option<BoardProfile> {
|
||||
let (arch, bootloader_policy) = match board {
|
||||
"am335x" => ("arm", BootloaderPolicy::Preserve),
|
||||
// Tier-3 from-stock AM335x: writes our non-RSA U-Boot into mtd4, so its
|
||||
// policy is include (the flash module preserves the SPL mtd0-3).
|
||||
"am335x-stock" => ("arm", BootloaderPolicy::Include),
|
||||
"cv1835" => ("arm64", BootloaderPolicy::Preserve),
|
||||
"zynq-7010" => ("armv7", BootloaderPolicy::Preserve),
|
||||
"a113d" => ("armv7", BootloaderPolicy::Preserve),
|
||||
|
||||
@@ -316,7 +316,7 @@ fn run_plan(args: &[String]) -> Result<(), Box<dyn std::error::Error>> {
|
||||
.first()
|
||||
.ok_or("usage: alkane-cli afw plan <file> --host <ip>")?;
|
||||
let verified = load_afw(path, &opts)?;
|
||||
if verified.manifest.board == "am335x" {
|
||||
if am335x::owns_board(&verified.manifest.board) {
|
||||
let plan = Am335xPlan::from_afw(&verified)?;
|
||||
let (ssh, _secret) = ssh_options(&opts, DEFAULT_USER)?;
|
||||
let mut transport = SshTransport::connect(ssh, true)?;
|
||||
@@ -390,7 +390,7 @@ fn run_preflight(args: &[String]) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let verified = load_afw(path, &opts)?;
|
||||
let (ssh, _secret) = ssh_options(&opts, DEFAULT_USER)?;
|
||||
let mut transport = SshTransport::connect(ssh, true)?;
|
||||
if verified.manifest.board == "am335x" {
|
||||
if am335x::owns_board(&verified.manifest.board) {
|
||||
let plan = Am335xPlan::from_afw(&verified)?;
|
||||
let report = am335x::preflight(&plan, &mut transport, opts.value("host").unwrap_or(""))?;
|
||||
print!("{}", report.render());
|
||||
@@ -445,7 +445,7 @@ fn run_install(args: &[String]) -> Result<(), Box<dyn std::error::Error>> {
|
||||
.value("backup-dir")
|
||||
.map(PathBuf::from)
|
||||
.unwrap_or_else(|| platform::config_path("backups"));
|
||||
if verified.manifest.board == "am335x" {
|
||||
if am335x::owns_board(&verified.manifest.board) {
|
||||
let plan = Am335xPlan::from_afw(&verified)?;
|
||||
let install_options = AmInstallOptions {
|
||||
dry_run: opts.flag("dry-run"),
|
||||
@@ -626,24 +626,33 @@ fn run_restore(args: &[String]) -> Result<(), Box<dyn std::error::Error>> {
|
||||
args,
|
||||
&[
|
||||
ssh_values(),
|
||||
&["held-backup", "backup-dir", "confirm", "board", "held-kernel", "held-dtb"],
|
||||
&[
|
||||
"held-backup", "backup-dir", "confirm", "board", "held-kernel",
|
||||
"held-dtb", "held-uboot",
|
||||
],
|
||||
]
|
||||
.concat(),
|
||||
&["no-reboot"],
|
||||
)?;
|
||||
let (ssh, _secret) = ssh_options(&opts, DEFAULT_USER)?;
|
||||
let mut transport = SshTransport::connect(ssh, true)?;
|
||||
if opts.value("board") == Some("am335x") {
|
||||
if let Some(profile) = opts.value("board").and_then(am335x::profile_for) {
|
||||
let dir = opts
|
||||
.value("backup-dir")
|
||||
.map(PathBuf::from)
|
||||
.unwrap_or_else(|| platform::config_path("backups"));
|
||||
let mut backups: Vec<(String, PathBuf)> = Vec::new();
|
||||
for (name, value, prefix) in [
|
||||
// The held set is the profile's write targets: mtd8/mtd7/mtd6 on both
|
||||
// tiers, plus mtd4 (our U-Boot) on the tier-3 from-stock profile.
|
||||
let mut table: Vec<(&str, Option<&str>, &str)> = vec![
|
||||
("mtd8", opts.value("held-backup"), "mtd8-"),
|
||||
("mtd7", opts.value("held-kernel"), "mtd7-"),
|
||||
("mtd6", opts.value("held-dtb"), "mtd6-"),
|
||||
] {
|
||||
];
|
||||
if profile.targets.iter().any(|(part, ..)| *part == "mtd4") {
|
||||
table.push(("mtd4", opts.value("held-uboot"), "mtd4-"));
|
||||
}
|
||||
let mut backups: Vec<(String, PathBuf)> = Vec::new();
|
||||
for (name, value, prefix) in table {
|
||||
let path = match value {
|
||||
Some(path) => PathBuf::from(path),
|
||||
None => latest_backup_for(&dir, prefix).ok_or_else(|| {
|
||||
@@ -655,6 +664,7 @@ fn run_restore(args: &[String]) -> Result<(), Box<dyn std::error::Error>> {
|
||||
};
|
||||
backups.push((name.to_owned(), path));
|
||||
}
|
||||
let preserve: Vec<String> = profile.preserve.iter().map(|s| (*s).to_owned()).collect();
|
||||
let receipt = am335x::restore(
|
||||
&backups,
|
||||
&mut transport,
|
||||
@@ -662,6 +672,7 @@ fn run_restore(args: &[String]) -> Result<(), Box<dyn std::error::Error>> {
|
||||
confirm: opts.value("confirm").map(str::to_owned),
|
||||
no_reboot: opts.flag("no-reboot"),
|
||||
},
|
||||
&preserve,
|
||||
)?;
|
||||
for (name, path) in &receipt.restored {
|
||||
println!("restored {name} from {}", path.display());
|
||||
|
||||
@@ -50,6 +50,83 @@ const TARGET_IMAGES: [(&str, &str, u64); 3] = [
|
||||
("mtd7", "kernel", 0x500000),
|
||||
];
|
||||
|
||||
// Tier-3 (from-stock) variant. The board is on genuine Bitmain stock, whose
|
||||
// U-Boot RSA-verifies the kernel, so the install writes our non-RSA U-Boot into
|
||||
// mtd4 as well (a legacy firmware uImage the preserved SPL chainloads). The SPL
|
||||
// copies (mtd0-3) and bootenv (mtd5) stay preserved; mtd4 is now a write target
|
||||
// at write_order 3 and the kernel moves to write_order 4 (U-Boot lands first).
|
||||
pub const BOARD_STOCK: &str = "am335x-stock";
|
||||
const MTD_MAP_STOCK: [(&str, u64, u64, bool, bool, i64, Option<&str>); 12] = [
|
||||
("mtd0", 0x0000000, 0x20000, true, true, 0, None),
|
||||
("mtd1", 0x0020000, 0x20000, true, true, 0, None),
|
||||
("mtd2", 0x0040000, 0x20000, true, true, 0, None),
|
||||
("mtd3", 0x0060000, 0x20000, true, true, 0, None),
|
||||
("mtd4", 0x0080000, 0x1C0000, false, true, 3, Some("u-boot")),
|
||||
("mtd5", 0x0240000, 0x20000, true, true, 0, None),
|
||||
("mtd6", 0x0260000, 0x20000, false, true, 2, Some("dtb")),
|
||||
("mtd7", 0x0280000, 0x500000, false, true, 4, Some("kernel")),
|
||||
("mtd8", 0x0800000, 0x1400000, false, true, 1, Some("rootfs")),
|
||||
("mtd9", 0x1C00000, 0x200000, true, false, 0, None),
|
||||
("mtd10", 0x1E00000, 0x200000, true, false, 0, None),
|
||||
("mtd11", 0x2000000, 0x6000000, true, false, 0, None),
|
||||
];
|
||||
const PRESERVE_STOCK: [&str; 5] = ["mtd0", "mtd1", "mtd2", "mtd3", "mtd5"];
|
||||
/// The four SPL copies are the sole recovery net and are never a restore target
|
||||
/// on any tier, independent of the per-profile preserved set.
|
||||
const SPL_NEVER_RESTORE: [&str; 4] = ["mtd0", "mtd1", "mtd2", "mtd3"];
|
||||
const TARGET_IMAGES_STOCK: [(&str, &str, u64); 4] = [
|
||||
("mtd8", "rootfs", 0x1400000),
|
||||
("mtd6", "dtb", 0x20000),
|
||||
("mtd4", "u-boot", 0x1C0000),
|
||||
("mtd7", "kernel", 0x500000),
|
||||
];
|
||||
|
||||
/// Per-tier differences between the tier-2 update (`am335x`) and the tier-3
|
||||
/// from-stock install (`am335x-stock`). The byte-write engine is shared; only
|
||||
/// the policy, the preserved set, the partition map and the image list differ.
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct Profile {
|
||||
pub board: &'static str,
|
||||
pub policy: BootloaderPolicy,
|
||||
pub mtd_map: &'static [(&'static str, u64, u64, bool, bool, i64, Option<&'static str>)],
|
||||
pub preserve: &'static [&'static str],
|
||||
pub targets: &'static [(&'static str, &'static str, u64)],
|
||||
/// Tier 3 ships our U-Boot as a `bootloader` member; tier 2 never does.
|
||||
pub allow_bootloader_member: bool,
|
||||
}
|
||||
|
||||
const TIER2: Profile = Profile {
|
||||
board: BOARD,
|
||||
policy: BootloaderPolicy::Preserve,
|
||||
mtd_map: &MTD_MAP,
|
||||
preserve: &PRESERVE,
|
||||
targets: &TARGET_IMAGES,
|
||||
allow_bootloader_member: false,
|
||||
};
|
||||
const TIER3_STOCK: Profile = Profile {
|
||||
board: BOARD_STOCK,
|
||||
policy: BootloaderPolicy::Include,
|
||||
mtd_map: &MTD_MAP_STOCK,
|
||||
preserve: &PRESERVE_STOCK,
|
||||
targets: &TARGET_IMAGES_STOCK,
|
||||
allow_bootloader_member: true,
|
||||
};
|
||||
|
||||
/// Select the install profile for a bundle's board family, or `None` if this
|
||||
/// module does not own that board.
|
||||
pub fn profile_for(board: &str) -> Option<Profile> {
|
||||
match board {
|
||||
BOARD => Some(TIER2),
|
||||
BOARD_STOCK => Some(TIER3_STOCK),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// True when this module handles the given board family.
|
||||
pub fn owns_board(board: &str) -> bool {
|
||||
profile_for(board).is_some()
|
||||
}
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum AmPlanError {
|
||||
#[error("bundle is for {0}, not am335x")]
|
||||
@@ -117,6 +194,11 @@ pub struct AmTarget {
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Am335xPlan {
|
||||
/// Board family this plan is for: "am335x" (tier 2) or "am335x-stock" (tier 3).
|
||||
pub board: String,
|
||||
/// Partitions this plan must never erase or write (SPL copies, and on tier 3
|
||||
/// also bootenv). The installer refuses to touch any of these.
|
||||
pub preserve: Vec<String>,
|
||||
pub targets: Vec<AmTarget>,
|
||||
pub partitions: Vec<AfwPartition>,
|
||||
pub marker: String,
|
||||
@@ -132,28 +214,31 @@ pub struct Am335xPlan {
|
||||
impl Am335xPlan {
|
||||
pub fn from_afw(afw: &VerifiedAfw) -> Result<Self, AmPlanError> {
|
||||
let manifest = &afw.manifest;
|
||||
if manifest.board != BOARD {
|
||||
return Err(AmPlanError::WrongBoard(manifest.board.clone()));
|
||||
}
|
||||
let profile = profile_for(&manifest.board)
|
||||
.ok_or_else(|| AmPlanError::WrongBoard(manifest.board.clone()))?;
|
||||
if manifest.arch != ARCH {
|
||||
return Err(AmPlanError::WrongArch(manifest.arch.clone()));
|
||||
}
|
||||
if manifest.payload_type != "os" {
|
||||
return Err(AmPlanError::PayloadType);
|
||||
}
|
||||
if manifest.bootloader_policy != BootloaderPolicy::Preserve {
|
||||
if manifest.bootloader_policy != profile.policy {
|
||||
return Err(AmPlanError::BootloaderPolicy);
|
||||
}
|
||||
if manifest
|
||||
.images
|
||||
.iter()
|
||||
.any(|image| image.role == "bootloader")
|
||||
{
|
||||
return Err(AmPlanError::BootloaderMember);
|
||||
}
|
||||
// Tier 2 carries no bootloader member at all; tier 3 carries exactly our
|
||||
// U-Boot (role bootloader, named u-boot), and nothing else bootloader-ish.
|
||||
for image in &manifest.images {
|
||||
if image.role == "bootloader" && !profile.allow_bootloader_member {
|
||||
return Err(AmPlanError::BootloaderMember);
|
||||
}
|
||||
let lowered = format!("{} {}", image.name, image.file).to_lowercase();
|
||||
if lowered.contains("spl") || lowered.contains("u-boot") || lowered.contains("uboot") {
|
||||
// An SPL member is never allowed on either tier (mtd0-3 are the
|
||||
// recovery net). A u-boot member is allowed only on tier 3.
|
||||
if lowered.contains("spl") {
|
||||
return Err(AmPlanError::SplUbootMember(image.file.clone()));
|
||||
}
|
||||
let is_uboot = lowered.contains("u-boot") || lowered.contains("uboot");
|
||||
if is_uboot && !(profile.allow_bootloader_member && image.name == "u-boot") {
|
||||
return Err(AmPlanError::SplUbootMember(image.file.clone()));
|
||||
}
|
||||
}
|
||||
@@ -169,14 +254,14 @@ impl Am335xPlan {
|
||||
.iter()
|
||||
.filter(|image| image.install)
|
||||
.collect();
|
||||
if install.len() != 3 {
|
||||
if install.len() != profile.targets.len() {
|
||||
return Err(AmPlanError::InstallCount);
|
||||
}
|
||||
|
||||
validate_partitions(&manifest.partitions)?;
|
||||
validate_partitions(&manifest.partitions, &profile)?;
|
||||
|
||||
let mut targets = Vec::with_capacity(3);
|
||||
for (partition_name, image_name, ceiling) in TARGET_IMAGES {
|
||||
let mut targets = Vec::with_capacity(profile.targets.len());
|
||||
for &(partition_name, image_name, ceiling) in profile.targets {
|
||||
let image = manifest
|
||||
.images
|
||||
.iter()
|
||||
@@ -233,6 +318,8 @@ impl Am335xPlan {
|
||||
targets.sort_by_key(|target| target.partition.write_order);
|
||||
|
||||
Ok(Self {
|
||||
board: profile.board.to_owned(),
|
||||
preserve: profile.preserve.iter().map(|name| (*name).to_owned()).collect(),
|
||||
targets,
|
||||
partitions: manifest.partitions.clone(),
|
||||
marker: manifest.marker.clone(),
|
||||
@@ -251,14 +338,23 @@ impl Am335xPlan {
|
||||
}
|
||||
|
||||
pub fn render(&self) -> String {
|
||||
let stock = self.board == BOARD_STOCK;
|
||||
let mut out = String::new();
|
||||
out.push_str("board am335x arch arm\n");
|
||||
out.push_str(&format!(
|
||||
"bootloader preserve (mtd0-5 SPL/U-Boot/bootenv kept, never written)\n"
|
||||
));
|
||||
out.push_str(&format!(
|
||||
"image format legacy-uimage (kernel type 2, ramdisk type 3), never FIT\n"
|
||||
));
|
||||
out.push_str(&format!("board {} arch arm\n", self.board));
|
||||
if stock {
|
||||
out.push_str(
|
||||
"tier 3 from stock: write U-Boot to mtd4 (legacy firmware uimage the SPL \
|
||||
chainloads); SPL mtd0-3 and bootenv mtd5 kept, never written\n",
|
||||
);
|
||||
out.push_str(
|
||||
"image format legacy-uimage (u-boot type 5, kernel type 2, ramdisk type 3), never FIT\n",
|
||||
);
|
||||
} else {
|
||||
out.push_str("bootloader preserve (mtd0-5 SPL/U-Boot/bootenv kept, never written)\n");
|
||||
out.push_str(
|
||||
"image format legacy-uimage (kernel type 2, ramdisk type 3), never FIT\n",
|
||||
);
|
||||
}
|
||||
out.push_str(&format!(
|
||||
"nand hole 0x{:x}-0x{:x} (never written)\n",
|
||||
HOLE_OFFSET,
|
||||
@@ -288,11 +384,11 @@ impl Am335xPlan {
|
||||
}
|
||||
}
|
||||
|
||||
fn validate_partitions(partitions: &[AfwPartition]) -> Result<(), AmPlanError> {
|
||||
if partitions.len() != MTD_MAP.len() {
|
||||
fn validate_partitions(partitions: &[AfwPartition], profile: &Profile) -> Result<(), AmPlanError> {
|
||||
if partitions.len() != profile.mtd_map.len() {
|
||||
return Err(AmPlanError::MapSize);
|
||||
}
|
||||
for (name, offset, size, secure, boot_critical, write_order, image) in MTD_MAP {
|
||||
for &(name, offset, size, secure, boot_critical, write_order, image) in profile.mtd_map {
|
||||
let part = partitions
|
||||
.iter()
|
||||
.find(|part| part.name == name)
|
||||
@@ -310,7 +406,7 @@ fn validate_partitions(partitions: &[AfwPartition]) -> Result<(), AmPlanError> {
|
||||
if part.offset < HOLE_OFFSET + HOLE_SIZE && part.offset + part.size > HOLE_OFFSET {
|
||||
return Err(AmPlanError::Hole(name.to_owned()));
|
||||
}
|
||||
if PRESERVE.contains(&name) && (part.write_order != 0 || part.image.is_some()) {
|
||||
if profile.preserve.contains(&name) && (part.write_order != 0 || part.image.is_some()) {
|
||||
return Err(AmPlanError::PreservedTarget(name.to_owned()));
|
||||
}
|
||||
}
|
||||
@@ -333,7 +429,12 @@ fn check_uimage(name: &str, data: &[u8]) -> Result<(), AmPlanError> {
|
||||
if data[29] != 2 {
|
||||
return Err(AmPlanError::UImage(name.to_owned()));
|
||||
}
|
||||
let expected = if name == "rootfs" { 3 } else { 2 };
|
||||
// ih_type: ramdisk 3, firmware (our U-Boot in mtd4) 5, kernel 2.
|
||||
let expected = match name {
|
||||
"rootfs" => 3,
|
||||
"u-boot" => 5,
|
||||
_ => 2,
|
||||
};
|
||||
if data[30] != expected {
|
||||
return Err(AmPlanError::UImageType {
|
||||
image: name.to_owned(),
|
||||
@@ -422,7 +523,7 @@ impl AmPreflightReport {
|
||||
"free tmp {} MiB\n",
|
||||
self.free_bytes / (1024 * 1024)
|
||||
));
|
||||
out.push_str("preserved mtd0-5: never erased and never written\n");
|
||||
out.push_str("preserved partitions (see preserve-spl check): never erased and never written\n");
|
||||
for check in &self.checks {
|
||||
out.push_str(&format!(
|
||||
"check {} {}: {}\n",
|
||||
@@ -594,12 +695,15 @@ pub fn preflight(
|
||||
let preserve_ok = plan
|
||||
.partitions
|
||||
.iter()
|
||||
.filter(|part| PRESERVE.contains(&part.name.as_str()))
|
||||
.filter(|part| plan.preserve.contains(&part.name))
|
||||
.all(|part| part.write_order == 0 && part.image.is_none() && part.secure);
|
||||
checks.push(AmCheck {
|
||||
name: "preserve-mtd0-5".to_owned(),
|
||||
name: "preserve-spl".to_owned(),
|
||||
ok: preserve_ok,
|
||||
detail: "mtd0-5 are secure, image-free and outside the write order".to_owned(),
|
||||
detail: format!(
|
||||
"preserved partitions ({}) are secure, image-free and outside the write order",
|
||||
plan.preserve.join(", ")
|
||||
),
|
||||
});
|
||||
|
||||
Ok(AmPreflightReport {
|
||||
@@ -933,6 +1037,7 @@ pub fn restore(
|
||||
backups: &[(String, PathBuf)],
|
||||
remote: &mut dyn Remote,
|
||||
options: &AmRestoreOptions,
|
||||
preserve: &[String],
|
||||
) -> Result<AmRestoreReceipt, AmError> {
|
||||
if options.confirm.as_deref() != Some("FLASH") {
|
||||
return Err(AmError::Confirm);
|
||||
@@ -953,7 +1058,9 @@ pub fn restore(
|
||||
.iter()
|
||||
.find(|(mtd, ..)| mtd == name)
|
||||
.ok_or_else(|| AmError::BadRestoreTarget(name.clone()))?;
|
||||
if PRESERVE.contains(&name.as_str()) {
|
||||
// The SPL copies (mtd0-3) are the recovery net and are never restored on
|
||||
// any tier; the rest of the preserved set is passed in per profile.
|
||||
if SPL_NEVER_RESTORE.contains(&name.as_str()) || preserve.iter().any(|p| p == name) {
|
||||
return Err(AmError::PreservedTarget(name.clone()));
|
||||
}
|
||||
if *size == 0 {
|
||||
@@ -1311,6 +1418,98 @@ mod tests {
|
||||
(uimage(2, b"kernel"), dtb(), uimage(3, b"ramdisk"))
|
||||
}
|
||||
|
||||
// --- tier-3 (am335x-stock) fixtures ---
|
||||
|
||||
fn stock_afw(uboot: &[u8], kernel: &[u8], dtb_data: &[u8], rootfs: &[u8]) -> VerifiedAfw {
|
||||
let partitions = MTD_MAP_STOCK
|
||||
.iter()
|
||||
.map(|(name, offset, size, secure, boot_critical, write_order, image)| {
|
||||
partition(name, &format!("/dev/{name}"), *offset, *size, *secure,
|
||||
*boot_critical, *write_order, *image)
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
let value = json!({
|
||||
"schema": 2, "format": "afw", "board": "am335x-stock", "arch": "arm",
|
||||
"version": "0.1.0", "build_date": "2026-11-05T00:00:00Z",
|
||||
"sdk_revision": REV, "os_revision": REV, "payload_type": "os",
|
||||
"bootloader_policy": "include",
|
||||
"signature": {"type": "unsigned", "member": null, "public_key_hex": null},
|
||||
"partitions": partitions,
|
||||
"images": [
|
||||
{"name": "u-boot", "file": "payload/alkane-am335x-uboot-mtd4.uImage",
|
||||
"role": "bootloader", "format": "uimage", "size": uboot.len(),
|
||||
"sha256": sha_hex(uboot), "partition": "mtd4", "install": true},
|
||||
{"name": "kernel", "file": "payload/alkane-am335x.uImage",
|
||||
"role": "kernel", "format": "uimage", "size": kernel.len(),
|
||||
"sha256": sha_hex(kernel), "partition": "mtd7", "install": true},
|
||||
{"name": "dtb", "file": "payload/am335x-alkane.dtb",
|
||||
"role": "dtb", "format": "fdt", "size": dtb_data.len(),
|
||||
"sha256": sha_hex(dtb_data), "partition": "mtd6", "install": true},
|
||||
{"name": "rootfs", "file": "payload/alkane-am335x-ramdisk.uImage",
|
||||
"role": "rootfs", "format": "uimage", "size": rootfs.len(),
|
||||
"sha256": sha_hex(rootfs), "partition": "mtd8", "install": true}
|
||||
],
|
||||
"boot_switch": {"method": "spl-chainloads-mtd4-uboot-go"},
|
||||
"preflight": {"required_commands": ["flash_erase", "nandwrite", "nanddump", "md5sum", "sync"],
|
||||
"source_os": ["stock-bitmain"], "min_free_bytes": 33554432},
|
||||
"recovery": {"network": "afw restore", "held_partition": "mtd8"},
|
||||
"marker": "Alkane", "kernel_version": "6.18.48"
|
||||
});
|
||||
let manifest = manifest_from_json(value).unwrap();
|
||||
VerifiedAfw {
|
||||
manifest,
|
||||
members: vec![
|
||||
AfwMember { name: "payload/alkane-am335x-uboot-mtd4.uImage".to_owned(), data: uboot.to_vec() },
|
||||
AfwMember { name: "payload/alkane-am335x.uImage".to_owned(), data: kernel.to_vec() },
|
||||
AfwMember { name: "payload/am335x-alkane.dtb".to_owned(), data: dtb_data.to_vec() },
|
||||
AfwMember { name: "payload/alkane-am335x-ramdisk.uImage".to_owned(), data: rootfs.to_vec() },
|
||||
],
|
||||
manifest_sha256: "0".repeat(64),
|
||||
archive_sha256: "0".repeat(64),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn accepts_a_good_stock_plan_and_writes_uboot_before_kernel() {
|
||||
let (uboot, kernel, dtb_data, rootfs) =
|
||||
(uimage(5, b"u-boot"), uimage(2, b"kernel"), dtb(), uimage(3, b"ramdisk"));
|
||||
let plan = Am335xPlan::from_afw(&stock_afw(&uboot, &kernel, &dtb_data, &rootfs)).unwrap();
|
||||
assert_eq!(plan.board, "am335x-stock");
|
||||
assert_eq!(plan.targets.len(), 4);
|
||||
// write order: rootfs(1), dtb(2), u-boot(3), kernel(4) - U-Boot before kernel.
|
||||
let order: Vec<&str> = plan.targets.iter().map(|t| t.partition.name.as_str()).collect();
|
||||
assert_eq!(order, ["mtd8", "mtd6", "mtd4", "mtd7"]);
|
||||
assert!(plan.preserve.iter().all(|p| p != "mtd4"), "mtd4 must not be preserved on tier 3");
|
||||
assert!(plan.preserve.contains(&"mtd5".to_owned()) && plan.preserve.contains(&"mtd0".to_owned()));
|
||||
assert!(plan.render().contains("tier 3 from stock"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stock_rejects_a_uboot_that_is_not_firmware_type() {
|
||||
// U-Boot payload wrongly built as a type-2 kernel uImage must be caught.
|
||||
let (uboot, kernel, dtb_data, rootfs) =
|
||||
(uimage(2, b"u-boot"), uimage(2, b"kernel"), dtb(), uimage(3, b"ramdisk"));
|
||||
let result = Am335xPlan::from_afw(&stock_afw(&uboot, &kernel, &dtb_data, &rootfs));
|
||||
assert!(matches!(result, Err(AmPlanError::UImageType { expected: 5, .. })));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stock_rejects_making_an_spl_copy_a_write_target() {
|
||||
let (uboot, kernel, dtb_data, rootfs) =
|
||||
(uimage(5, b"u-boot"), uimage(2, b"kernel"), dtb(), uimage(3, b"ramdisk"));
|
||||
let mut verified = stock_afw(&uboot, &kernel, &dtb_data, &rootfs);
|
||||
// Turn mtd0 (an SPL copy) into a write target.
|
||||
for part in &mut verified.manifest.partitions {
|
||||
if part.name == "mtd0" {
|
||||
part.secure = false;
|
||||
part.write_order = 5;
|
||||
part.image = Some("u-boot".to_owned());
|
||||
}
|
||||
}
|
||||
let result = Am335xPlan::from_afw(&verified);
|
||||
assert!(matches!(result, Err(AmPlanError::PartitionGeometry(_)) | Err(AmPlanError::PreservedTarget(_))));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn accepts_a_good_plan_and_orders_writes_root_dtb_kernel() {
|
||||
let (kernel, dtb_data, rootfs) = good();
|
||||
@@ -1663,6 +1862,7 @@ mod tests {
|
||||
let temp = tempfile::tempdir().unwrap();
|
||||
let path = temp.path().join("mtd4.img");
|
||||
fs::write(&path, vec![0_u8; 0x1C0000]).unwrap();
|
||||
let tier2_preserve: Vec<String> = PRESERVE.iter().map(|s| (*s).to_owned()).collect();
|
||||
let result = restore(
|
||||
&[("mtd4".to_owned(), path)],
|
||||
&mut remote,
|
||||
@@ -1670,8 +1870,50 @@ mod tests {
|
||||
confirm: Some("FLASH".to_owned()),
|
||||
no_reboot: true,
|
||||
},
|
||||
&tier2_preserve,
|
||||
);
|
||||
assert!(matches!(result, Err(AmError::PreservedTarget(_))));
|
||||
assert!(remote.writes.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tier3_restore_of_mtd4_is_allowed_but_spl_is_never() {
|
||||
// Under the tier-3 preserved set (mtd0-3 + mtd5), mtd4 (our U-Boot) is a
|
||||
// legitimate restore target, while an SPL copy must always be refused.
|
||||
let temp = tempfile::tempdir().unwrap();
|
||||
let path = temp.path().join("mtd4.img");
|
||||
fs::write(&path, vec![0_u8; 0x1C0000]).unwrap();
|
||||
let tier3_preserve: Vec<String> =
|
||||
PRESERVE_STOCK.iter().map(|s| (*s).to_owned()).collect();
|
||||
|
||||
// mtd4 restore is accepted (it reaches the write path, not the guard).
|
||||
let mut remote = MockRemote::new(0x1C0000);
|
||||
let ok = restore(
|
||||
&[("mtd4".to_owned(), path.clone())],
|
||||
&mut remote,
|
||||
&AmRestoreOptions {
|
||||
confirm: Some("FLASH".to_owned()),
|
||||
no_reboot: true,
|
||||
},
|
||||
&tier3_preserve,
|
||||
);
|
||||
assert!(ok.is_ok(), "tier-3 mtd4 restore should be allowed: {ok:?}");
|
||||
|
||||
// An SPL copy is refused even though it is not in the tier-3 preserve
|
||||
// list, because SPL is never a restore target on any tier.
|
||||
let spl = temp.path().join("mtd0.img");
|
||||
fs::write(&spl, vec![0_u8; 0x20000]).unwrap();
|
||||
let mut remote2 = MockRemote::new(0x20000);
|
||||
let refused = restore(
|
||||
&[("mtd0".to_owned(), spl)],
|
||||
&mut remote2,
|
||||
&AmRestoreOptions {
|
||||
confirm: Some("FLASH".to_owned()),
|
||||
no_reboot: true,
|
||||
},
|
||||
&tier3_preserve,
|
||||
);
|
||||
assert!(matches!(refused, Err(AmError::PreservedTarget(_))));
|
||||
assert!(remote2.writes.is_empty());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user