MOS Source Code
Loading...
Searching...
No Matches
libfdt++.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-3.0-or-later
2
3#include "libfdt++.hpp"
4
5#include <libfdt.h>
6
7unsigned int dt_root_addr_cells = 2;
8unsigned int dt_root_size_cells = 1;
9
10static int pair_bytes()
11{
12 return (dt_root_addr_cells + dt_root_size_cells) * sizeof(u32);
13}
14
15/* Helper to read a big number; size is in cells (not bytes) */
16// Adapted from Linux kernel's of_read_number()
17static inline u64 of_read_number(const u32 *cell, int size)
18{
19 u64 r = 0;
20 for (; size--; cell++)
21 r = (r << 32) | be32_to_cpu(*cell);
22 return r;
23}
24
26{
27 if (!prop.len())
28 return true; // zero-length reg property is valid
29
30 if (prop.len() % pair_bytes())
31 return false; // invalid length (total size is not a multiple of pair size)
32
33 return true;
34}
35
36std::pair<ptr_t, size_t> dt_reg::iterator::operator*() const
37{
38 u32 *pcell = (u32 *) data;
39 const ptr_t base = of_read_number(pcell, dt_root_addr_cells);
41 return std::make_pair(base, size);
42}
43
45{
46 data += pair_bytes();
47 return *this;
48}
49
50dt_node::dt_node(const dt_root &root, const char *path) : m_root(root), m_offset(-1)
51{
53}
54
55const char *dt_node::get_name() const
56{
57 return fdt_get_name(m_root.fdt(), m_offset, nullptr);
58}
59
65
70
72{
73 return iterator(*this, -FDT_ERR_NOTFOUND);
74}
75
77{
78 return iterator(this->node, fdt_first_property_offset(node.root().fdt(), node.offset()));
79}
80
85
91
92bool dt_node::has_property(const char *name) const
93{
94 return fdt_get_property(m_root.fdt(), m_offset, name, nullptr) != nullptr;
95}
96
97dt_property::dt_property(const dt_node &node, const char *name) : m_node(node), m_name(name)
98{
99 m_propdata = fdt_getprop(node.root().fdt(), node.offset(), name, &m_len);
100}
101
102dt_property::dt_property(const dt_node &node, int poffset) : m_node(node)
103{
104 m_propdata = fdt_getprop_by_offset(node.root().fdt(), poffset, &m_name, &m_len);
105}
106
108{
109 return fdt32_to_cpu(*(const u32 *) m_propdata);
110}
111
113{
114 return fdt64_to_cpu(*(const u64 *) m_propdata);
115}
116
117const char *dt_property::get_string() const
118{
119 return (const char *) m_propdata;
120}
const dt_node & node
Definition libfdt++.hpp:94
iterator & operator++()
Definition libfdt++.cpp:60
iterator begin() const
Definition libfdt++.cpp:76
dt_node(const dt_root &root, unsigned long offset)
Definition libfdt++.hpp:129
const dt_root & root() const
Definition libfdt++.hpp:145
iterator end() const
Definition libfdt++.cpp:71
const char * get_name() const
Definition libfdt++.cpp:55
const dt_root & m_root
Definition libfdt++.hpp:152
long offset() const
Definition libfdt++.hpp:136
unsigned long m_offset
Definition libfdt++.hpp:153
iterator begin() const
Definition libfdt++.cpp:66
bool has_property(const char *name) const
Definition libfdt++.cpp:92
u64 get_u64() const
Definition libfdt++.cpp:112
dt_property(const dt_node &node, const char *name)
Definition libfdt++.cpp:97
const char * get_string() const
Definition libfdt++.cpp:117
int len() const
Definition libfdt++.hpp:28
const void * m_propdata
Definition libfdt++.hpp:37
const char * m_name
Definition libfdt++.hpp:36
u32 get_u32() const
Definition libfdt++.cpp:107
std::pair< ptr_t, size_t > operator*() const
Definition libfdt++.cpp:36
iterator & operator++()
Definition libfdt++.cpp:44
const char * data
Definition libfdt++.hpp:62
const dt_property & prop
Definition libfdt++.hpp:75
bool verify_validity() const
Definition libfdt++.cpp:25
void * fdt() const
Definition libfdt++.hpp:163
#define be32_to_cpu(x)
Definition compiler.h:32
int fdt_first_subnode(const void *fdt, int offset)
Definition fdt.c:287
int fdt_next_subnode(const void *fdt, int offset)
Definition fdt.c:298
int fdt_path_offset(const void *fdt, const char *path)
Definition fdt_ro.c:295
const char * fdt_get_name(const void *fdt, int nodeoffset, int *len)
Definition fdt_ro.c:300
const struct fdt_property * fdt_get_property(const void *fdt, int nodeoffset, const char *name, int *lenp)
Definition fdt_ro.c:443
const void * fdt_getprop(const void *fdt, int nodeoffset, const char *name, int *lenp)
Definition fdt_ro.c:502
int fdt_first_property_offset(const void *fdt, int nodeoffset)
Definition fdt_ro.c:338
int fdt_next_property_offset(const void *fdt, int offset)
Definition fdt_ro.c:348
const void * fdt_getprop_by_offset(const void *fdt, int offset, const char **namep, int *lenp)
Definition fdt_ro.c:469
unsigned int dt_root_addr_cells
Definition libfdt++.cpp:7
unsigned int dt_root_size_cells
Definition libfdt++.cpp:8
static int pair_bytes()
Definition libfdt++.cpp:10
static u64 of_read_number(const u32 *cell, int size)
Definition libfdt++.cpp:17
unsigned int dt_root_addr_cells
Definition libfdt++.cpp:7
unsigned int dt_root_size_cells
Definition libfdt++.cpp:8
#define FDT_ERR_NOTFOUND
Definition libfdt.h:21
static uint64_t fdt64_to_cpu(fdt64_t x)
Definition libfdt_env.h:56
static uint32_t fdt32_to_cpu(fdt32_t x)
Definition libfdt_env.h:47
size_t size
Definition slab.c:30
const char * name
Definition slab.c:31
unsigned int u32
Definition types.h:21
unsigned long ptr_t
Definition types.h:25
unsigned long long u64
Definition types.h:23