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