|
| 1 | +// Copyright lowRISC contributors (OpenTitan project). |
| 2 | +// Licensed under the Apache License, Version 2.0, see LICENSE for details. |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +#![allow(clippy::bool_assert_comparison)] |
| 6 | +use anyhow::{Context, Result, anyhow}; |
| 7 | +use clap::{Args, Parser, Subcommand, ValueEnum}; |
| 8 | + |
| 9 | +use std::io::Cursor; |
| 10 | +use std::path::Path; |
| 11 | +use std::time::Duration; |
| 12 | + |
| 13 | +use opentitanlib::app::TransportWrapper; |
| 14 | +use opentitanlib::chip::boot_log::OwnershipState; |
| 15 | +use opentitanlib::chip::boot_svc::BootSlot; |
| 16 | +use opentitanlib::chip::device_id::DeviceId; |
| 17 | +use opentitanlib::image::image::{self}; |
| 18 | +use opentitanlib::rescue::{EntryMode, RescueParams}; |
| 19 | +use opentitanlib::test_utils::init::InitializeTest; |
| 20 | +use opentitanlib::util::file::FromReader; |
| 21 | + |
| 22 | +#[derive(Debug, Parser)] |
| 23 | +struct Opts { |
| 24 | + #[command(flatten)] |
| 25 | + init: InitializeTest, |
| 26 | + |
| 27 | + #[command(subcommand)] |
| 28 | + command: Commands, |
| 29 | + |
| 30 | + // Device ID represented as a hexadecimal string. |
| 31 | + // This format should correspond to how the ID is structured or stored |
| 32 | + // in the device's OTP. |
| 33 | + #[arg(long)] |
| 34 | + device_id: Option<String>, |
| 35 | + |
| 36 | + /// Console receive timeout. |
| 37 | + #[arg(long, value_parser = humantime::parse_duration, default_value = "10s")] |
| 38 | + timeout: Duration, |
| 39 | +} |
| 40 | + |
| 41 | +#[derive(Clone, Debug, Args)] |
| 42 | +struct RescueCommand { |
| 43 | + #[command(flatten)] |
| 44 | + params: RescueParams, |
| 45 | + |
| 46 | + #[arg(long)] |
| 47 | + action: RescueTestActions, |
| 48 | +} |
| 49 | + |
| 50 | +#[derive(Debug, Subcommand)] |
| 51 | +enum Commands { |
| 52 | + Rescue(RescueCommand), |
| 53 | +} |
| 54 | + |
| 55 | +#[derive(ValueEnum, Debug, Clone, Copy, PartialEq)] |
| 56 | +pub enum RescueTestActions { |
| 57 | + GetDeviceId, |
| 58 | + GetBootLog, |
| 59 | +} |
| 60 | + |
| 61 | +fn get_device_id_test( |
| 62 | + expected_device_id_hex: &String, |
| 63 | + params: &RescueParams, |
| 64 | + transport: &TransportWrapper, |
| 65 | +) -> Result<()> { |
| 66 | + let rescue = params.create(transport)?; |
| 67 | + rescue.enter(transport, EntryMode::Reset)?; |
| 68 | + let actual_device_id_from_rescue = rescue.get_device_id()?; |
| 69 | + let mut bytes_from_hex = hex::decode(expected_device_id_hex).map_err(|e| { |
| 70 | + anyhow!( |
| 71 | + "Failed to decode hex string '{}': {}", |
| 72 | + expected_device_id_hex, |
| 73 | + e |
| 74 | + ) |
| 75 | + })?; |
| 76 | + // This reversal is to swap the byte order of the entire decoded hex sequence |
| 77 | + // to match the endianness expectation of the DeviceId::read function. |
| 78 | + bytes_from_hex.reverse(); |
| 79 | + let mut cursor = Cursor::new(bytes_from_hex); |
| 80 | + let parsed_expected_device_id = DeviceId::read(&mut cursor).unwrap(); |
| 81 | + if parsed_expected_device_id == actual_device_id_from_rescue { |
| 82 | + Ok(()) |
| 83 | + } else { |
| 84 | + Err(anyhow!( |
| 85 | + "Device ID mismatch. Expected: {:?}, but got: {:?}", |
| 86 | + parsed_expected_device_id, |
| 87 | + actual_device_id_from_rescue |
| 88 | + )) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +fn get_boot_log_test( |
| 93 | + binary: &Path, |
| 94 | + params: &RescueParams, |
| 95 | + transport: &TransportWrapper, |
| 96 | +) -> Result<()> { |
| 97 | + const BOOT_LOG_IDENTIFIER: u32 = u32::from_le_bytes(*b"BLOG"); |
| 98 | + let image = image::Image::read_from_file(binary)?; |
| 99 | + let rescue = params.create(transport)?; |
| 100 | + rescue.enter(transport, EntryMode::Reset)?; |
| 101 | + let boot_log = rescue |
| 102 | + .get_boot_log() |
| 103 | + .context("Failed to get boot log from rescue")?; |
| 104 | + let rom_ext_manifest = image |
| 105 | + .subimages()? |
| 106 | + .first() |
| 107 | + .ok_or_else(|| anyhow!("No subimages found in the image"))? |
| 108 | + .manifest; |
| 109 | + if boot_log.rom_ext_major != rom_ext_manifest.version_major { |
| 110 | + return Err(anyhow!( |
| 111 | + "rom_ext_major mismatch. Expected: {}, but got: {}", |
| 112 | + rom_ext_manifest.version_major, |
| 113 | + boot_log.rom_ext_major |
| 114 | + )); |
| 115 | + } |
| 116 | + |
| 117 | + if boot_log.rom_ext_minor != rom_ext_manifest.version_minor { |
| 118 | + return Err(anyhow!( |
| 119 | + "rom_ext_minor mismatch. Expected: {}, but got: {}", |
| 120 | + rom_ext_manifest.version_minor, |
| 121 | + boot_log.rom_ext_minor |
| 122 | + )); |
| 123 | + } |
| 124 | + |
| 125 | + if boot_log.ownership_state != OwnershipState::LockedOwner { |
| 126 | + return Err(anyhow!( |
| 127 | + "ownership_state mismatch. Expected: {}, but got: {}", |
| 128 | + OwnershipState::LockedOwner, |
| 129 | + boot_log.ownership_state |
| 130 | + )); |
| 131 | + } |
| 132 | + |
| 133 | + if boot_log.identifier != BOOT_LOG_IDENTIFIER { |
| 134 | + return Err(anyhow!( |
| 135 | + "identifier mismatch. Expected: {}, but got: {}", |
| 136 | + BOOT_LOG_IDENTIFIER, |
| 137 | + boot_log.identifier |
| 138 | + )); |
| 139 | + } |
| 140 | + |
| 141 | + if boot_log.rom_ext_slot != BootSlot::SlotA { |
| 142 | + return Err(anyhow!( |
| 143 | + "rom_ext_slot mismatch. Expected: {}, but got: {}", |
| 144 | + BootSlot::SlotA, |
| 145 | + boot_log.rom_ext_slot |
| 146 | + )); |
| 147 | + } |
| 148 | + |
| 149 | + Ok(()) |
| 150 | +} |
| 151 | + |
| 152 | +fn main() -> Result<()> { |
| 153 | + let opts = Opts::parse(); |
| 154 | + opts.init.init_logging(); |
| 155 | + let transport = opts.init.init_target()?; |
| 156 | + |
| 157 | + match opts.command { |
| 158 | + Commands::Rescue(rescue) => match rescue.action { |
| 159 | + RescueTestActions::GetDeviceId => { |
| 160 | + let device_id = &opts |
| 161 | + .device_id |
| 162 | + .as_ref() |
| 163 | + .ok_or_else(|| anyhow!("No device_id provided"))?; |
| 164 | + get_device_id_test(device_id, &rescue.params, &transport)?; |
| 165 | + } |
| 166 | + RescueTestActions::GetBootLog => { |
| 167 | + let binary = &opts |
| 168 | + .init |
| 169 | + .bootstrap |
| 170 | + .bootstrap |
| 171 | + .as_ref() |
| 172 | + .ok_or_else(|| anyhow!("No RV32 test binary provided"))?; |
| 173 | + get_boot_log_test(binary, &rescue.params, &transport)?; |
| 174 | + } |
| 175 | + }, |
| 176 | + } |
| 177 | + Ok(()) |
| 178 | +} |
0 commit comments