Damjan Marion | f508e07 | 2024-08-27 18:21:02 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright (c) 2024 Cisco Systems, Inc. |
| 3 | */ |
| 4 | |
| 5 | #include <vppinfra/clib.h> |
| 6 | #include <vppinfra/devicetree.h> |
| 7 | |
| 8 | #ifdef __linux |
| 9 | #include <sys/types.h> |
| 10 | #include <sys/stat.h> |
| 11 | #include <fcntl.h> |
| 12 | #include <dirent.h> |
| 13 | |
Damjan Marion | f508e07 | 2024-08-27 18:21:02 +0200 | [diff] [blame] | 14 | static_always_inline clib_dt_node_t * |
| 15 | clib_dt_node_add_child (clib_dt_main_t *dm, clib_dt_node_t *n, char *name) |
| 16 | { |
| 17 | clib_dt_node_t *cn; |
| 18 | |
| 19 | cn = clib_mem_alloc (sizeof (clib_dt_node_t)); |
| 20 | *cn = (clib_dt_node_t){ .parent = n, .depth = n ? n->depth + 1 : 0 }; |
| 21 | vec_add1 (dm->nodes, cn); |
| 22 | |
| 23 | if (n == 0) |
| 24 | { |
| 25 | ASSERT (dm->root == 0); |
| 26 | dm->root = cn; |
| 27 | return cn; |
| 28 | } |
| 29 | |
| 30 | vec_add1 (n->child_nodes, cn); |
| 31 | cn->path = format (0, "%v/%s", n->path, name); |
| 32 | cn->dt_main = dm; |
| 33 | hash_set_mem (dm->node_by_path, cn->path, cn); |
| 34 | if (vec_len (n->child_nodes) > 1) |
| 35 | { |
| 36 | clib_dt_node_t *prev = n->child_nodes[vec_len (n->child_nodes) - 2]; |
| 37 | prev->next = cn; |
| 38 | cn->prev = prev; |
| 39 | } |
| 40 | |
| 41 | return cn; |
| 42 | } |
Tom Jones | 33fec83 | 2024-09-11 15:42:32 +0000 | [diff] [blame] | 43 | #endif |
Damjan Marion | f508e07 | 2024-08-27 18:21:02 +0200 | [diff] [blame] | 44 | |
| 45 | void |
| 46 | clib_dt_main_free (clib_dt_main_t *dm) |
| 47 | { |
| 48 | vec_foreach_pointer (n, dm->nodes) |
| 49 | { |
| 50 | vec_foreach_pointer (p, n->properties) |
| 51 | clib_mem_free (p); |
| 52 | vec_free (n->child_nodes); |
| 53 | vec_free (n->path); |
| 54 | vec_free (n->properties); |
| 55 | } |
| 56 | |
| 57 | vec_free (dm->nodes); |
| 58 | hash_free (dm->node_by_path); |
| 59 | hash_free (dm->node_by_phandle); |
| 60 | } |
| 61 | |
| 62 | #ifdef __linux |
| 63 | __clib_export clib_error_t * |
| 64 | clib_dt_read_from_sysfs (clib_dt_main_t *dm) |
| 65 | { |
| 66 | DIR *dir, **dir_stack = 0; |
| 67 | struct dirent *e; |
| 68 | clib_dt_node_t *n; |
| 69 | u8 *path = 0; |
| 70 | u32 path_prefix_len; |
| 71 | clib_error_t *err = 0; |
| 72 | |
| 73 | path = format (0, CLIB_DT_LINUX_PREFIX); |
| 74 | path_prefix_len = vec_len (path); |
| 75 | vec_add1 (path, 0); |
| 76 | |
| 77 | dir = opendir ((char *) path); |
| 78 | if (!dir) |
| 79 | { |
| 80 | err = clib_error_return (0, "'%s' opendir failed", path); |
| 81 | goto done; |
| 82 | } |
| 83 | |
| 84 | dm->node_by_path = hash_create_vec (0, sizeof (u8), sizeof (uword)); |
| 85 | dm->node_by_phandle = hash_create (0, sizeof (uword)); |
| 86 | vec_set_len (path, path_prefix_len); |
| 87 | n = clib_dt_node_add_child (dm, 0, 0); |
| 88 | |
| 89 | while (1) |
| 90 | { |
| 91 | e = readdir (dir); |
| 92 | |
| 93 | if (!e) |
| 94 | { |
| 95 | closedir (dir); |
| 96 | if (vec_len (dir_stack) == 0) |
| 97 | break; |
| 98 | |
| 99 | dir = dir_stack[vec_len (dir_stack) - 1]; |
| 100 | vec_pop (dir_stack); |
| 101 | n = n->parent; |
| 102 | continue; |
| 103 | } |
| 104 | |
| 105 | if (e->d_type == DT_REG) |
| 106 | { |
| 107 | path = format (path, "%v/%s%c", n->path, e->d_name, 0); |
| 108 | int fd = open ((char *) path, 0); |
| 109 | if (fd >= 0) |
| 110 | { |
| 111 | struct stat st; |
| 112 | if (fstat (fd, &st) == 0) |
| 113 | { |
| 114 | u32 sz = sizeof (clib_dt_property_t) + st.st_size; |
| 115 | clib_dt_property_t *p = clib_mem_alloc (sz); |
| 116 | clib_memset (p, 0, sz); |
| 117 | |
| 118 | if (read (fd, p->data, st.st_size) == st.st_size) |
| 119 | { |
| 120 | strncpy (p->name, e->d_name, sizeof (p->name)); |
| 121 | p->size = st.st_size; |
| 122 | vec_add1 (n->properties, p); |
| 123 | if (strncmp ("name", p->name, 5) == 0) |
| 124 | n->name = p; |
| 125 | if ((strncmp ("phandle", p->name, 8) == 0) && |
| 126 | (p->size == 4)) |
| 127 | { |
| 128 | u32 phandle = |
| 129 | clib_net_to_host_u32 (*(u32u *) p->data); |
| 130 | hash_set (dm->node_by_phandle, phandle, n); |
| 131 | } |
| 132 | } |
| 133 | else |
| 134 | { |
| 135 | clib_mem_free (p); |
| 136 | err = clib_error_return (0, "'%s' read failed", path); |
| 137 | close (fd); |
| 138 | goto done; |
| 139 | } |
| 140 | } |
| 141 | else |
| 142 | { |
| 143 | err = clib_error_return (0, "'%s' fstat failed", path); |
| 144 | close (fd); |
| 145 | goto done; |
| 146 | } |
| 147 | close (fd); |
| 148 | } |
| 149 | else |
| 150 | { |
| 151 | err = clib_error_return (0, "'%s' open failed", path); |
| 152 | goto done; |
| 153 | } |
| 154 | |
| 155 | vec_set_len (path, path_prefix_len); |
| 156 | } |
| 157 | else if (e->d_type == DT_DIR) |
| 158 | { |
| 159 | DIR *subdir; |
| 160 | if (strncmp (".", e->d_name, 2) == 0 || |
| 161 | strncmp ("..", e->d_name, 3) == 0) |
| 162 | continue; |
| 163 | |
| 164 | path = format (path, "%v/%s%c", n->path, e->d_name, 0); |
| 165 | subdir = opendir ((char *) path); |
| 166 | vec_set_len (path, path_prefix_len); |
| 167 | if (subdir) |
| 168 | { |
| 169 | vec_add1 (dir_stack, dir); |
| 170 | dir = subdir; |
| 171 | n = clib_dt_node_add_child (dm, n, e->d_name); |
| 172 | } |
| 173 | else |
| 174 | { |
| 175 | err = clib_error_return (0, "'%s' opendir failed", path); |
| 176 | goto done; |
| 177 | } |
| 178 | } |
| 179 | else |
| 180 | err = |
| 181 | clib_error_return (0, "unknown entry %s [%u]", e->d_name, e->d_type); |
| 182 | } |
| 183 | |
| 184 | done: |
| 185 | if (err) |
| 186 | clib_dt_main_free (dm); |
| 187 | while (vec_len (dir_stack)) |
| 188 | closedir (vec_pop (dir_stack)); |
| 189 | vec_free (dir_stack); |
| 190 | vec_free (path); |
| 191 | return err; |
| 192 | } |
| 193 | #endif |
| 194 | |
Damjan Marion | 96a5624 | 2024-10-08 20:47:38 +0200 | [diff] [blame] | 195 | __clib_export clib_dt_node_t * |
| 196 | clib_dt_get_child_node (clib_dt_node_t *n, char *fmt, ...) |
Damjan Marion | f508e07 | 2024-08-27 18:21:02 +0200 | [diff] [blame] | 197 | { |
Damjan Marion | 96a5624 | 2024-10-08 20:47:38 +0200 | [diff] [blame] | 198 | u8 *s; |
| 199 | va_list va; |
| 200 | va_start (va, fmt); |
| 201 | s = va_format (0, fmt, &va); |
| 202 | va_end (va); |
| 203 | vec_add1 (s, 0); |
| 204 | |
Damjan Marion | f508e07 | 2024-08-27 18:21:02 +0200 | [diff] [blame] | 205 | vec_foreach_pointer (cn, n->child_nodes) |
| 206 | { |
| 207 | u8 *p = cn->path + vec_len (cn->path) - 1; |
| 208 | u32 i = 0; |
| 209 | |
| 210 | while (p > cn->path && p[-1] != '/') |
| 211 | p--; |
| 212 | |
| 213 | if (p[-1] != '/') |
| 214 | continue; |
| 215 | |
Damjan Marion | 96a5624 | 2024-10-08 20:47:38 +0200 | [diff] [blame] | 216 | while (p[i] == s[i] && s[i] != 0) |
Damjan Marion | f508e07 | 2024-08-27 18:21:02 +0200 | [diff] [blame] | 217 | i++; |
| 218 | |
Damjan Marion | 96a5624 | 2024-10-08 20:47:38 +0200 | [diff] [blame] | 219 | if (s[i] != 0) |
Damjan Marion | f508e07 | 2024-08-27 18:21:02 +0200 | [diff] [blame] | 220 | continue; |
| 221 | |
Damjan Marion | 96a5624 | 2024-10-08 20:47:38 +0200 | [diff] [blame] | 222 | vec_free (s); |
Damjan Marion | f508e07 | 2024-08-27 18:21:02 +0200 | [diff] [blame] | 223 | return cn; |
| 224 | } |
| 225 | |
Damjan Marion | 96a5624 | 2024-10-08 20:47:38 +0200 | [diff] [blame] | 226 | vec_free (s); |
Damjan Marion | f508e07 | 2024-08-27 18:21:02 +0200 | [diff] [blame] | 227 | return 0; |
| 228 | } |
| 229 | |
| 230 | __clib_export clib_dt_node_t * |
| 231 | clib_dt_get_node_with_path (clib_dt_main_t *dm, char *fmt, ...) |
| 232 | { |
| 233 | u8 *s; |
| 234 | uword *p; |
| 235 | |
| 236 | va_list va; |
| 237 | va_start (va, fmt); |
| 238 | s = va_format (0, fmt, &va); |
| 239 | va_end (va); |
| 240 | |
| 241 | if (s[0] != '/') |
| 242 | return 0; |
| 243 | |
| 244 | p = hash_get_mem (dm->node_by_path, s); |
| 245 | if (p) |
| 246 | return (clib_dt_node_t *) p[0]; |
| 247 | |
| 248 | return 0; |
| 249 | } |
| 250 | |
| 251 | __clib_export clib_dt_property_t * |
| 252 | clib_dt_get_node_property_by_name (clib_dt_node_t *n, char *name) |
| 253 | { |
| 254 | vec_foreach_pointer (p, n->properties) |
| 255 | if (strncmp (name, p->name, sizeof (p->name)) == 0) |
| 256 | return p; |
| 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | __clib_export int |
| 261 | clib_dt_node_is_compatible (clib_dt_node_t *n, char *comp) |
| 262 | { |
| 263 | clib_dt_property_t *p; |
| 264 | char *s; |
| 265 | |
| 266 | p = clib_dt_get_node_property_by_name (n, "compatible"); |
| 267 | |
| 268 | if (!p) |
| 269 | return 0; |
| 270 | |
| 271 | s = (char *) p->data; |
| 272 | for (u32 i = 1, len = 1; i <= p->size; i++) |
| 273 | { |
| 274 | if (p->data[i - 1] == 0) |
| 275 | { |
| 276 | if (strncmp (comp, s, len) == 0) |
| 277 | return 1; |
| 278 | s = (char *) p->data + i; |
| 279 | len = 1; |
| 280 | } |
| 281 | else |
| 282 | len++; |
| 283 | } |
| 284 | |
| 285 | return 0; |
| 286 | } |
| 287 | |
| 288 | __clib_export u8 * |
| 289 | format_clib_dt_property_data (u8 *s, va_list *args) |
| 290 | { |
| 291 | clib_dt_property_t *p = va_arg (*args, clib_dt_property_t *); |
| 292 | u32 sz = p->size, is_printable = 0; |
| 293 | u32 n_nulls = 0; |
| 294 | |
| 295 | if (sz > 2 && p->data[sz - 1] == 0 && p->data[0] != 0) |
| 296 | { |
| 297 | is_printable = 1; |
| 298 | for (u32 i = 1; i < sz - 1; i++) |
| 299 | { |
| 300 | u8 c = p->data[i]; |
| 301 | if (c == 0) |
| 302 | { |
| 303 | if (p->data[i - 1] == 0) |
| 304 | { |
| 305 | is_printable = 0; |
| 306 | break; |
| 307 | } |
| 308 | n_nulls++; |
| 309 | } |
| 310 | else if ((c < 0x20) || (c > 0x7f)) |
| 311 | { |
| 312 | is_printable = 0; |
| 313 | break; |
| 314 | } |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | if (is_printable) |
| 319 | { |
| 320 | s = format (s, "'%s'", p->data); |
| 321 | if (n_nulls) |
| 322 | { |
| 323 | for (u32 i = 2; i < p->size; i++) |
| 324 | if (((u8 *) p->data)[i - 1] == 0) |
| 325 | s = format (s, ", '%s'", ((u8 *) p->data) + i); |
| 326 | } |
| 327 | } |
| 328 | else |
| 329 | { |
| 330 | s = format (s, "< %02x", p->data[0]); |
| 331 | for (u32 i = 0; i < p->size; i++) |
| 332 | s = format (s, " %02x", p->data[i]); |
| 333 | s = format (s, " >"); |
| 334 | } |
| 335 | return s; |
| 336 | } |
| 337 | |
| 338 | __clib_export clib_dt_node_t * |
| 339 | clib_dt_dereference_node (clib_dt_node_t *n, char *name) |
| 340 | { |
| 341 | clib_dt_property_t *p; |
| 342 | uword *h; |
| 343 | |
| 344 | p = clib_dt_get_node_property_by_name (n, name); |
| 345 | if (!p || (p->size != sizeof (u32))) |
| 346 | return 0; |
| 347 | |
| 348 | h = hash_get (n->dt_main->node_by_phandle, |
| 349 | clib_net_to_host_u32 (*(u32u *) p->data)); |
| 350 | |
| 351 | if (h) |
| 352 | return (clib_dt_node_t *) h[0]; |
| 353 | |
| 354 | return 0; |
| 355 | } |