Skip to content

Commit

Permalink
[opentitanlib] Use hex formatter for OTP sequences
Browse files Browse the repository at this point in the history
The ROM patches carry actual rv32 instructions in OTP, making them
appear as a sequence of hex formatted strings is much more readable.

Signed-off-by: Samuel Ortiz <sameo@rivosinc.com>
  • Loading branch information
sameo authored and andreaskurth committed Aug 22, 2024
1 parent 6cb5794 commit 7576112
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions sw/host/opentitanlib/src/otp/otp_img.rs
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@ use std::path::Path;
use anyhow::{anyhow, bail, Result};

use serde::de::{self, Unexpected};
use serde::ser::SerializeSeq;
use serde::{Deserialize, Serialize};

use serde_annotate::Annotate;
@@ -19,6 +20,7 @@ use serde_annotate::Annotate;
pub enum OtpImgValue {
Word(u64),
Bool(bool),
#[serde(serialize_with = "serialize_vec_u32_hex")]
Sequence(Vec<u32>),
#[serde(serialize_with = "serialize_random")]
Random,
@@ -31,6 +33,17 @@ where
serializer.serialize_str("<random>")
}

fn serialize_vec_u32_hex<S>(vec: &[u32], serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let mut seq = serializer.serialize_seq(Some(vec.len()))?;
for num in vec {
seq.serialize_element(&format!("0x{:08x}", num))?;
}
seq.end()
}

impl<'de> Deserialize<'de> for OtpImgValue {
fn deserialize<D>(deserializer: D) -> Result<OtpImgValue, D::Error>
where
@@ -278,9 +291,9 @@ mod tests {
{
name: \"CREATOR_SEQ\",
value: [
171,
205,
239
\"0x000000ab\",
\"0x000000cd\",
\"0x000000ef\"
]
}
]

0 comments on commit 7576112

Please sign in to comment.