fdt.c: node-scoped lookup extension (FABRIC-3.md SSIV.3/SSV.3 shared item)
Adds fdt_find_node_by_compatible() and fdt_find_prop_in_node() to the
minimal FDT reader -- the extension fdt.h's own header comment already
flagged as a known future need ("item 0.6 will need node-scoped reg
lookups"), now with real consumers: the Pi 5's UART/mailbox register
addresses (native boot, no ACPI) and the Milk-V Mars's real PLIC base
address (currently hardcoded to QEMU-virt's own value).
fdt_find_node_by_compatible() matches any entry in a node's
NUL-separated "compatible" list, first match in document order.
fdt_find_prop_in_node() scopes to that one node's own direct
properties only -- stops at the first child node or the node's own
end, per the DT spec's ordering guarantee that a node's properties
always precede its children. Same minimal, non-tree-building,
single-linear-scan-per-call style as the existing reader; no new
state, no allocation.
Compile-only verification -- no caller wired in yet, this is the
shared primitive both boards' own punch-list items will call once
built. Verified 3-arch boot to ok> (amd64/aarch64/riscv64, each in
the foreground).
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019YcT3H2PQeyujrzjqS3Var
This commit is contained in:
co-authored by
Claude Sonnet 5
parent
bffd87615d
commit
5e46f18fd9
@@ -156,3 +156,151 @@ int fdt_prop_u32(const void* fdt, const char* name, uint32_t* out)
|
||||
*out = be32(val);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test whether NUL-separated string list @p list (length @p len)
|
||||
* contains @p want as one of its entries.
|
||||
*
|
||||
* "compatible" properties are formatted as one or more NUL-terminated
|
||||
* strings concatenated (DT spec §2.3.1) -- each entry's own embedded NUL
|
||||
* is real property data, not synthesized, so str_eq() (which walks until
|
||||
* either side's NUL) terminates correctly at each entry's real boundary.
|
||||
*/
|
||||
static int compat_list_contains(const char* list, uint32_t len, const char* want)
|
||||
{
|
||||
uint32_t i = 0;
|
||||
|
||||
while (i < len)
|
||||
{
|
||||
const char* s = list + i;
|
||||
uint32_t slen = 0;
|
||||
while (i + slen < len && s[slen]) slen++;
|
||||
if (str_eq(s, want)) return 1;
|
||||
i += slen + 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
const void* fdt_find_node_by_compatible(const void* fdt, const char* compatible)
|
||||
{
|
||||
const fdt_header_t* h = (const fdt_header_t*)fdt;
|
||||
const unsigned char *base, *p, *end, *strings;
|
||||
uint32_t size_struct;
|
||||
int fresh_node = 0; /* 1 = haven't yet seen a non-PROP token since the
|
||||
* last FDT_BEGIN_NODE -- i.e. still inside that
|
||||
* node's own leading property list, per the DT
|
||||
* spec's ordering guarantee (all of a node's
|
||||
* direct properties precede its children). */
|
||||
const void* node_body_start = (void*)0;
|
||||
|
||||
if (!fdt_valid(fdt) || !compatible) return (void*)0;
|
||||
|
||||
base = (const unsigned char*)fdt;
|
||||
size_struct = be32(&h->size_dt_struct);
|
||||
p = base + be32(&h->off_dt_struct);
|
||||
end = p + size_struct;
|
||||
strings = base + be32(&h->off_dt_strings);
|
||||
|
||||
while (p + 4 <= end)
|
||||
{
|
||||
uint32_t token = be32(p);
|
||||
p += 4;
|
||||
|
||||
if (token == FDT_BEGIN_NODE)
|
||||
{
|
||||
const unsigned char* q = p;
|
||||
while (q < end && *q) q++;
|
||||
if (q >= end) break;
|
||||
p = (const unsigned char*)(((uintptr_t)(q + 1) + 3u) & ~(uintptr_t)3u);
|
||||
fresh_node = 1;
|
||||
node_body_start = (const void*)p;
|
||||
}
|
||||
else if (token == FDT_PROP)
|
||||
{
|
||||
uint32_t len, nameoff;
|
||||
const unsigned char* val;
|
||||
|
||||
if (p + 8 > end) break;
|
||||
len = be32(p);
|
||||
nameoff = be32(p + 4);
|
||||
p += 8;
|
||||
val = p;
|
||||
if (len > (uint32_t)(end - p)) break;
|
||||
|
||||
if (fresh_node &&
|
||||
str_eq((const char*)(strings + nameoff), "compatible") &&
|
||||
compat_list_contains((const char*)val, len, compatible))
|
||||
{
|
||||
return node_body_start;
|
||||
}
|
||||
|
||||
p = (const unsigned char*)(((uintptr_t)(p + len) + 3u) & ~(uintptr_t)3u);
|
||||
/* fresh_node stays 1 -- still within this same node's own
|
||||
* leading property list. */
|
||||
}
|
||||
else if (token == FDT_END_NODE)
|
||||
{
|
||||
fresh_node = 0;
|
||||
}
|
||||
else if (token == FDT_NOP)
|
||||
{
|
||||
/* no payload; does not end the leading property list */
|
||||
}
|
||||
else
|
||||
{
|
||||
/* FDT_END, or a token this reader does not know: stop. */
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return (void*)0;
|
||||
}
|
||||
|
||||
const void* fdt_find_prop_in_node(const void* fdt, const void* node,
|
||||
const char* name, uint32_t* len_out)
|
||||
{
|
||||
const fdt_header_t* h = (const fdt_header_t*)fdt;
|
||||
const unsigned char *p, *end, *strings;
|
||||
|
||||
if (!fdt_valid(fdt) || !node || !name) return (void*)0;
|
||||
|
||||
p = (const unsigned char*)node;
|
||||
end = (const unsigned char*)fdt + be32(&h->off_dt_struct) + be32(&h->size_dt_struct);
|
||||
strings = (const unsigned char*)fdt + be32(&h->off_dt_strings);
|
||||
|
||||
/* node points just past this node's own FDT_BEGIN_NODE name -- scan
|
||||
* only its leading property list; stop at the first non-PROP/NOP
|
||||
* token (a child's FDT_BEGIN_NODE, or this node's own FDT_END_NODE),
|
||||
* matching fdt_find_node_by_compatible()'s own scoping rule. Never
|
||||
* descends into children, never continues into a sibling. */
|
||||
while (p + 4 <= end)
|
||||
{
|
||||
uint32_t token = be32(p);
|
||||
p += 4;
|
||||
|
||||
if (token == FDT_NOP) continue;
|
||||
if (token != FDT_PROP) break;
|
||||
|
||||
{
|
||||
uint32_t len, nameoff;
|
||||
const unsigned char* val;
|
||||
|
||||
if (p + 8 > end) break;
|
||||
len = be32(p);
|
||||
nameoff = be32(p + 4);
|
||||
p += 8;
|
||||
val = p;
|
||||
if (len > (uint32_t)(end - p)) break;
|
||||
|
||||
if (str_eq((const char*)(strings + nameoff), name))
|
||||
{
|
||||
if (len_out) *len_out = len;
|
||||
return (const void*)val;
|
||||
}
|
||||
|
||||
p = (const unsigned char*)(((uintptr_t)(p + len) + 3u) & ~(uintptr_t)3u);
|
||||
}
|
||||
}
|
||||
|
||||
return (void*)0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user