11#include <drivers/dt/dt.h>
2- #include <drivers/dt/dt_node.h>
3- #include <drivers/dt/dt_parser.h>
4- #include <drivers/dt/dt_props.h>
2+ #include <stdbigos/bitutils.h>
3+ #include <stdbigos/sbi.h>
54#include <stdbigos/string.h>
65
6+ #include "dt_alloc.h"
7+ #include "dt_node.h"
8+ #include "dt_parser.h"
9+
10+ static void sbi_puts (const char * str ) {
11+ while (* str ) sbi_debug_console_write_byte (* str ++ );
12+ }
13+
14+ // There has to be a better way to do this
15+ static void gap (u8 size ) {
16+ for (u8 i = 0 ; i < size ; i ++ ) sbi_puts ("\t" );
17+ }
18+
719struct dt_node * dt_get_root (void ) {
820 return root_node ;
921}
@@ -14,12 +26,12 @@ static struct dt_node* find_child_by_name(struct dt_node* parent, const char* na
1426 return child ;
1527 }
1628 }
17- return (( void * ) 0 ) ;
29+ return nullptr ;
1830}
1931
20- struct dt_node * dt_find_node (const char * path ) {
32+ struct dt_node * dt_node_find (const char * path ) {
2133 if (!path || path [0 ] != '/' || !root_node )
22- return (( void * ) 0 ) ;
34+ return nullptr ;
2335
2436 if (strcmp (path , "/" ) == 0 )
2537 return root_node ;
@@ -37,32 +49,51 @@ struct dt_node* dt_find_node(const char* path) {
3749
3850 char segment [64 ];
3951 if (len >= (int )sizeof (segment ))
40- return (( void * ) 0 ) ;
52+ return nullptr ;
4153
4254 memcpy (segment , p , len );
4355 segment [len ] = '\0' ;
4456
4557 current = find_child_by_name (current , segment );
4658 if (!current )
47- return (( void * ) 0 ) ;
59+ return nullptr ;
4860
4961 p = (* slash == '/' ) ? slash + 1 : slash ;
5062 }
5163
5264 return current ;
5365}
5466
67+ const char * dt_node_get_name (const struct dt_node * node ) {
68+ if (!node )
69+ return nullptr ;
70+
71+ return node -> name ;
72+ }
73+
74+ int dt_node_child_count (const struct dt_node * n ) {
75+ int c = 0 ;
76+ for (struct dt_node * ch = n -> first_child ; ch ; ch = ch -> next_sibling ) c ++ ;
77+ return c ;
78+ }
79+
80+ struct dt_node * dt_node_get_child (const struct dt_node * n , int idx ) {
81+ struct dt_node * ch = n ? n -> first_child : NULL ;
82+ for (; ch && idx > 0 ; ch = ch -> next_sibling , idx -- );
83+ return ch ;
84+ }
85+
5586struct dt_prop * dt_find_prop (const struct dt_node * node , const char * name ) {
5687 if (!node || !name )
57- return (( void * ) 0 ) ;
88+ return nullptr ;
5889
5990 for (struct dt_prop * prop = node -> props ; prop ; prop = prop -> next_prop ) {
6091 if (strcmp (prop -> name , name ) == 0 ) {
6192 return prop ;
6293 }
6394 }
6495
65- return (( void * ) 0 ) ;
96+ return nullptr ;
6697}
6798
6899int dt_prop_read_u32 (const struct dt_node * node , const char * name , u32 * out ) {
@@ -84,11 +115,35 @@ int dt_prop_read_u64(const struct dt_node* node, const char* name, u64* out) {
84115 if (!prop || prop -> data_length < 8 || !prop -> value )
85116 return -1 ;
86117
87- const u8 * val = ( const u8 * ) prop -> value ;
118+ u64 val = read_be64 ( prop -> value ) ;
88119
89- u32 high = read_be32 (val );
90- u32 low = read_be32 (val + 4 );
120+ * out = val ;
91121
92- * out = ((u64 )high << 32 ) | (u64 )low ;
93122 return 0 ;
94123}
124+
125+ void dt_print_props (const struct dt_node * node , u8 depth ) {
126+ struct dt_prop * next = node -> props ;
127+ while (next ) {
128+ gap (depth );
129+ sbi_puts (next -> name );
130+ sbi_puts ("\n" );
131+ next = next -> next_prop ;
132+ }
133+ }
134+
135+ void dt_print_tree (const struct dt_node * node , u8 depth ) {
136+ gap (depth );
137+ sbi_puts ("NODE: " );
138+ sbi_puts (node -> name );
139+ sbi_puts ("\n" );
140+ gap (depth );
141+ sbi_puts ("PROPERTIES:\n" );
142+ dt_print_props (node , depth + 1 );
143+ sbi_puts ("\n" );
144+ struct dt_node * next = node -> first_child ;
145+ while (next ) {
146+ dt_print_tree (next , depth + 1 );
147+ next = next -> next_sibling ;
148+ }
149+ }
0 commit comments