Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 64 additions & 3 deletions rust/src/adt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,38 @@ pub struct ADTProperty {
value: [u8],
}

/// ADTPropertyStringIterator
#[derive(Debug)]
pub struct ADTPropertyStringIterator<'a> {
value: &'a [u8],
}

impl<'a> Iterator for ADTPropertyStringIterator<'a> {
type Item = &'a str;

fn next(&mut self) -> Option<&'a str> {
match CStr::from_bytes_until_nul(self.value) {
Ok(cs) => match cs.to_str() {
Ok(s) => {
match self.value.iter().position(|&x| x == 0) {
Some(nul) => self.value = &self.value[(nul + 1)..],
None => self.value = &[],
}
Some(s)
}
Err(_) => {
self.value = &[];
None
}
},
Err(_) => {
self.value = &[];
None
}
}
}
}

#[repr(C, packed(1))]
pub struct ADTSegmentRanges {
phys: u64,
Expand Down Expand Up @@ -245,17 +277,22 @@ impl ADTNode {
}
}

/// Get a reference to the root node
pub fn root() -> Result<&'static ADTNode, AdtError> {
let ptr: *const ADTNode = unsafe { adt as *const ADTNode };
ADTNode::from_ptr(ptr)
}

/// Get a reference to a node at a specified path, tracing the path to the
/// desired node
pub fn from_path_trace(
path: &str,
mut breadcrumbs: Option<&mut [Option<&'static ADTNode>]>,
) -> Result<&'static ADTNode, AdtError> {
let ptr: *const ADTNode = unsafe { adt as *const ADTNode };
let mut p = path;
let mut bc_idx: usize = 0;

let head = ADTNode::from_ptr(ptr)?;
let head = ADTNode::root()?;
let mut n = head;

while !p.is_empty() {
Expand Down Expand Up @@ -421,7 +458,13 @@ impl ADTNode {
}

pub fn is_compatible(&self, compatible: &str) -> Result<bool, AdtError> {
Ok(self.named_prop("compatible")?.str()?.contains(compatible))
let prop = self.named_prop("compatible")?;
Ok(prop.str_iter().any(|c| c == compatible))
}

pub fn compatible(&self, index: usize) -> Option<&str> {
let prop = self.named_prop("compatible").ok()?;
prop.str_iter().nth(index)
}
}

Expand Down Expand Up @@ -534,6 +577,12 @@ impl ADTProperty {
}
}

pub fn str_iter<'a>(&'a self) -> ADTPropertyStringIterator<'a> {
ADTPropertyStringIterator {
value: unsafe { core::slice::from_raw_parts(self.value.as_ptr(), self.size as usize) },
}
}

pub fn set(&mut self, val: &[u8]) -> Result<usize, AdtError> {
if val.len() != self.size as usize {
return Err(AdtError::BadLength);
Expand Down Expand Up @@ -769,6 +818,18 @@ pub unsafe extern "C" fn adt_is_compatible(
.unwrap()
}

#[no_mangle]
pub unsafe extern "C" fn adt_is_compatible_at(
_dt: *const c_void,
offset: c_int,
compat: *const c_char,
index: usize,
) -> bool {
let strcompat: &str = unsafe { CStr::from_ptr(compat).to_str().unwrap() };
let ptr: *const ADTNode = unsafe { adt.add(offset as usize) as *const ADTNode };
ADTNode::from_ptr(ptr).unwrap().compatible(index).unwrap() == strcompat
}

#[no_mangle]
pub unsafe extern "C" fn adt_get_name(_dt: *const c_void, offset: c_int) -> *const c_char {
let ptr: *const ADTNode = unsafe { adt.add(offset as usize) as *const ADTNode };
Expand Down
1 change: 1 addition & 0 deletions src/adt.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ int adt_getprop_copy(const void *adt, int nodeoffset, const char *name, void *ou

int adt_get_reg(const void *adt, int *path, const char *prop, int idx, u64 *addr, u64 *size);
bool adt_is_compatible(const void *adt, int nodeoffset, const char *compat);
bool adt_is_compatible_at(const void *adt, int nodeoffset, const char *compat, size_t index);

#define ADT_FOREACH_CHILD(adt, node) \
for (int _child_count = adt_get_child_count(adt, node); _child_count; _child_count = 0) \
Expand Down
2 changes: 1 addition & 1 deletion src/kboot_atc.c
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ static int dt_append_atc_fuses_helper(void *dt, int fdt_node, const struct atc_f
static int dt_append_fuses(void *dt, int adt_node, int fdt_node, int port)
{
for (size_t i = 0; i < ARRAY_SIZE(atc_fuses); ++i) {
if (!adt_is_compatible(adt, adt_node, atc_fuses[i].compatible))
if (!adt_is_compatible_at(adt, adt_node, atc_fuses[i].compatible, 0))
continue;
if (atc_fuses[i].port >= 0 && port != atc_fuses[i].port)
continue;
Expand Down