summaryrefslogtreecommitdiff
path: root/components/drivers/fdt/src/dtb_access.c
blob: e80192e7b46adfb403be1f39f09239f251573db9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
/*
 * Copyright (c) 2006-2023, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 */
#include "libfdt.h"
#include "dtb_node.h"

/* "/aliaes" node */
static struct dtb_node *fdt_aliases;

/**
 * of_find_property_value_of_size() - find property of given size
 *
 * Search for a property in a device node and validate the requested size.
 *
 * @np:     device node from which the property value is to be read.
 * @propname:   name of the property to be searched.
 * @len:    requested length of property value
 *
 * @return the property value on success, -EINVAL if the property does not
 * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
 * property data isn't large enough.
 */
static void *dtb_node_find_property_value_of_size(const struct dtb_node *dn,
                                             const char *propname, uint32_t len)
{
    struct dtb_property *prop = dtb_node_get_dtb_node_property(dn, propname, NULL);

    if (!prop)
        return ERR_PTR(-EINVAL);
    if (!prop->value)
        return ERR_PTR(-ENODATA);
    if (len > prop->size)
        return ERR_PTR(-EOVERFLOW);
    return prop->value;
}

int dtb_node_read_u32(const struct dtb_node *dn, const char *propname, uint32_t *outp)
{
    const uint32_t *val;

    debug("%s: %s: \n", __func__, propname);
    if (!dn)
        return -EINVAL;
    val = dtb_node_find_property_value_of_size(dn, propname, sizeof(*outp));
    if (IS_ERR(val))
    {
        debug("(not found)\n");
        return PTR_ERR(val);
    }

    *outp = fdt32_to_cpu(*val);
    debug("%#x (%d)\n", *outp, *outp);

    return 0;
}

uint32_t dtb_node_read_u32_default(const struct dtb_node *node, const char *propname, uint32_t def)
{
    dtb_node_read_u32(node, propname, &def);

    return def;
}

int dtb_node_read_u32_array(const struct dtb_node *dn, const char *propname,
                       uint32_t *out_values, size_t sz)
{
    const uint32_t *val;

    debug("%s: %s: ", __func__, propname);
    val = dtb_node_find_property_value_of_size(dn, propname,
                                          sz * sizeof(*out_values));

    if (IS_ERR(val))
        return PTR_ERR(val);

    debug("size %zd, val:%d\n", sz, *val);
    while (sz--)
        *out_values++ = fdt32_to_cpu(*val++);

    return 0;
}

uint32_t dtb_node_read_u32_index_default(const struct dtb_node *node, const char *propname, int index,
                  uint32_t def)
{
    RT_ASSERT(dtb_node_valid(node));
    dtb_node_read_u32_index(node, propname, index, &def);

    return def;
}

int dtb_node_read_s32_default(const struct dtb_node *node, const char *propname, int32_t def)
{
    RT_ASSERT(dtb_node_valid(node));
    dtb_node_read_u32(node, propname, (uint32_t *)&def);

    return def;
}

int dtb_node_read_u32_index(const struct dtb_node *dn, const char *propname,
                       int index, uint32_t *outp)
{
    const uint32_t *val;

    debug("%s: %s: ", __func__, propname);
    if (!dn)
        return -EINVAL;

    val = dtb_node_find_property_value_of_size(dn, propname,
                                          sizeof(*outp) * (index + 1));
    if (IS_ERR(val))
    {
        debug("(not found)\n");
        return PTR_ERR(val);
    }

    *outp = fdt32_to_cpu(val[index]);
    debug("%#x (%d)\n", *outp, *outp);

    return 0;
}

int dtb_node_read_u64(const struct dtb_node *dn, const char *propname, uint64_t *outp)
{
    const uint64_t *val;

    debug("%s: %s: ", __func__, propname);
    if (!dn)
        return -EINVAL;
    val = dtb_node_find_property_value_of_size(dn, propname, sizeof(*outp));
    if (IS_ERR(val))
    {
        debug("(not found)\n");
        return PTR_ERR(val);
    }

    *outp = fdt64_to_cpu(*val);
    debug("%#llx (%lld)\n", (unsigned long long)*outp,
          (unsigned long long)*outp);

    return 0;
}

uint64_t dtb_node_read_u64_default(const struct dtb_node *node, const char *propname, uint64_t def)
{
    RT_ASSERT(dtb_node_valid(node));
    dtb_node_read_u64(node, propname, &def);

    return def;
}

int dtb_node_n_addr_cells(const struct dtb_node *dn)
{
    const uint32_t *ip;

    do
    {
        if (dn->parent)
            dn = dn->parent;
        ip = dtb_node_get_dtb_node_property_value(dn, "#address-cells", NULL);
        if (ip)
            return fdt32_to_cpu(*ip);
    } while (dn->parent);

    /* No #address-cells property for the root node */
    return DEV_ROOT_NODE_ADDR_CELLS_DEFAULT;
}

int dtb_node_n_size_cells(const struct dtb_node *dn)
{
    const uint32_t *ip;

    do
    {
        if (dn->parent)
            dn = dn->parent;
        ip = dtb_node_get_dtb_node_property_value(dn, "#size-cells", NULL);
        if (ip)
            return fdt32_to_cpu(*ip);
    } while (dn->parent);

    /* No #size-cells property for the root node */
    return DEV_ROOT_NODE_SIZE_CELLS_DEFAULT;
}

int dtb_node_simple_addr_cells(const struct dtb_node *dn)
{
    const uint32_t *ip;

    ip = dtb_node_get_dtb_node_property_value(dn, "#address-cells", NULL);
    if (ip)
        return fdt32_to_cpu(*ip);

    /* Return a default of 2 to match fdt_address_cells()*/
    return 2;
}

int dtb_node_simple_size_cells(const struct dtb_node *dn)
{
    const uint32_t *ip;

    ip = dtb_node_get_dtb_node_property_value(dn, "#size-cells", NULL);
    if (ip)
        return fdt32_to_cpu(*ip);

    /* Return a default of 2 to match fdt_size_cells()*/
    return 2;
}

struct dtb_property *dtb_node_get_dtb_node_property(const struct dtb_node *dtb_node, const char *property_name, int *property_size)
{
    struct dtb_property *dtb_property = NULL;

    if (dtb_node != NULL && property_name != NULL)
    {
        dtb_property = dtb_node->properties;

        while (dtb_property != NULL)
        {
            if (!strcmp(dtb_property->name, property_name))
            {
                if (property_size != NULL)
                {
                    *property_size = dtb_property->size;
                }
                return dtb_property;
            }
            dtb_property = dtb_property->next;
        }
    }

    return dtb_property;
}

#define for_each_property_of_node(dn, pp) \
    for (pp = dn->properties; pp != NULL; pp = pp->next)

struct dtb_node *dtb_node_find_node_opts_by_path(const char *path,
                                            const char **opts)
{
    struct dtb_node *np = NULL;
    struct dtb_property *pp;
    const char *separator = strchr(path, ':');

    if (opts)
        *opts = separator ? separator + 1 : NULL;

    if (strcmp(path, "/") == 0)
        return dtb_node_get(get_dtb_node_head());

    /* The path could begin with an alias */
    if (*path != '/')
    {
        int len;
        const char *p = separator;

        if (!p)
            p = strchrnul(path, '/');
        len = p - path;

        /* of_aliases must not be NULL */
        if (!fdt_aliases)
            return NULL;

        for_each_property_of_node(fdt_aliases, pp)
        {
            if (strlen(pp->name) == len && !strncmp(pp->name, path,
                                                    len))
            {
                np = dtb_node_find_node_by_path(pp->value);
                break;
            }
        }
        if (!np)
            return NULL;
        path = p;
    }

    /* Step down the tree matching path components */
    if (!np)
        np = dtb_node_get(get_dtb_node_head());
    while (np && *path == '/')
    {
        struct dtb_node *tmp = np;

        path++; /* Increment past '/' delimiter */
        np = dtb_node_get_dtb_node_by_path(np, path);
        dtb_node_put(tmp);
        path = strchrnul(path, '/');
        if (separator && separator < path)
            break;
    }

    return np;
}

struct dtb_node *dtb_node_find_compatible_node(struct dtb_node *from, const char *compatible)
{
    struct dtb_node *dn;

    for_each_of_allnodes_from(from, dn)
    {
        if (dtb_node_get_dtb_node_compatible_match(dn, compatible) &&
            dtb_node_get(dn))
            break;
    }
    dtb_node_put(from);

    return dn;
}

void *dtb_node_get_dtb_node_property_value(const struct dtb_node *dtb_node, const char *property_name, int *property_size)
{
    struct dtb_property *dtb_property = dtb_node_get_dtb_node_property(dtb_node, property_name, NULL);

    if (!dtb_property || !dtb_property->value || !dtb_property->size)
    {
        return NULL;
    }

    if (property_size != NULL)
    {
        *property_size = dtb_property->size;
    }

    return dtb_property->value;
}

const struct dtb_node *dtb_node_find_node_by_prop_value(struct dtb_node *from,
                                                   const char *propname,
                                                   const void *propval, int proplen)
{
    struct dtb_node *np;
    void *value;
    for_each_of_allnodes_from(from, np)
    {
        value = dtb_node_get_dtb_node_property_value(np, propname, &proplen);
        if (!memcmp(value, propval, proplen) && dtb_node_get(np))
            break;
    }
    dtb_node_put(from);

    return np;
}

struct dtb_node *dtb_node_find_all_nodes(const struct dtb_node *prev)
{
    const struct dtb_node *dn;

    if (!prev)
    {
        dn = get_dtb_node_head();
    }
    else if (prev->child)
    {
        dn = prev->child;
    }
    else
    {
        /*
         * Walk back up looking for a sibling, or the end of the
         * structure
         */
        dn = prev;
        while (dn->parent && !dn->sibling)
            dn = dn->parent;
        dn = dn->sibling; /* Might be null at the end of the tree */
    }

    return (struct dtb_node *)dn;
}

rt_bool_t dtb_node_device_is_available(const struct dtb_node *device)
{
    const char *status;
    int statlen;

    if (!device)
        return RT_FALSE;

    status = dtb_node_get_dtb_node_property_value(device, "status", &statlen);
    if (status == NULL)
        return RT_TRUE;

    if (statlen > 0)
    {
        if (!strcmp(status, "okay"))
            return RT_TRUE;
    }

    return RT_FALSE;
}

struct dtb_node *dtb_node_get_parent(const struct dtb_node *node)
{
    const struct dtb_node *dn;

    if (!node)
        return NULL;

    dn = dtb_node_get(node->parent);

    return (struct dtb_node *)dn;
}

struct dtb_node *dtb_node_find_node_by_phandle(phandle handle)
{
    struct dtb_node *dn;

    if (!handle)
        return NULL;

    for_each_of_allnodes(dn) if (dn->handle == handle) break;
    (void)dtb_node_get(dn);

    return dn;
}

int dtb_node_property_match_string(const struct dtb_node *dn, const char *propname,
                              const char *string)
{
    const struct dtb_property *prop = dtb_node_get_dtb_node_property(dn, propname, NULL);
    size_t l;
    int i;
    const char *p, *end;

    if (!prop)
        return -EINVAL;
    if (!prop->value)
        return -ENODATA;

    p = prop->value;
    end = p + prop->size;

    for (i = 0; p < end; i++, p += l)
    {
        l = strnlen(p, end - p) + 1;
        if (p + l > end)
            return -EILSEQ;
        debug("comparing %s with %s\n", string, p);
        if (strcmp(string, p) == 0)
            return i; /* Found it; return index */
    }
    return -ENODATA;
}

/**
 * of_property_read_string_helper() - Utility helper for parsing string properties
 * @np:     device node from which the property value is to be read.
 * @propname:   name of the property to be searched.
 * @out_strs:   output array of string pointers.
 * @sz:     number of array elements to read.
 * @skip:   Number of strings to skip over at beginning of list.
 *
 * Don't call this function directly. It is a utility helper for the
 * of_property_read_string*() family of functions.
 */
int dtb_node_property_read_string_helper(const struct dtb_node *dn,
                                    const char *propname, const char **out_strs,
                                    size_t sz, int skip)
{
    const struct dtb_property *prop = dtb_node_get_dtb_node_property(dn, propname, NULL);
    int l = 0, i = 0;
    const char *p, *end;

    if (!prop)
        return -EINVAL;
    if (!prop->value)
        return -ENODATA;
    p = prop->value;
    end = p + prop->size;

    for (i = 0; p < end && (!out_strs || i < skip + sz); i++, p += l)
    {
        l = strnlen(p, end - p) + 1;
        if (p + l > end)
            return -EILSEQ;
        if (out_strs && i >= skip)
            *out_strs++ = p;
    }
    i -= skip;
    return i <= 0 ? -ENODATA : i;
}

static int __dtb_node_parse_phandle_with_args(const struct dtb_node *dn,
                                         const char *list_name,
                                         const char *cells_name,
                                         int cell_count, int index,
                                         struct fdt_phandle_args *out_args)
{
    const uint32_t *list, *list_end;
    int rc = 0, cur_index = 0;
    uint32_t count = 0;
    struct dtb_node *node = NULL;
    phandle phandle;
    int size;

    /* Retrieve the phandle list property */
    list = dtb_node_get_dtb_node_property_value(dn, list_name, &size);
    if (!list)
        return -ENOENT;
    list_end = list + size / sizeof(*list);

    /* Loop over the phandles until all the requested entry is found */
    while (list < list_end)
    {
        rc = -EINVAL;
        count = 0;

        /*
         * If phandle is 0, then it is an empty entry with no
         * arguments.  Skip forward to the next entry.
         */
        phandle = fdt32_to_cpu(*(list++));
        if (phandle)
        {
            /*
             * Find the provider node and parse the #*-cells
             * property to determine the argument length.
             *
             * This is not needed if the cell count is hard-coded
             * (i.e. cells_name not set, but cell_count is set),
             * except when we're going to return the found node
             * below.
             */
            if (cells_name || cur_index == index)
            {
                node = dtb_node_find_node_by_phandle(phandle);
                if (!node)
                {
                    debug("%s: could not find phandle\n",
                          dn->path);
                    goto err;
                }
            }

            if (cells_name)
            {
                if (dtb_node_read_u32(node, cells_name, &count))
                {
                    debug("%s: could not get %s for %s\n",
                          dn->path, cells_name,
                          node->path);
                    goto err;
                }
            }
            else
            {
                count = cell_count;
            }

            /*
             * Make sure that the arguments actually fit in the
             * remaining property data length
             */
            if (list + count > list_end)
            {
                debug("%s: arguments longer than property\n",
                      dn->path);
                goto err;
            }
        }

        /*
         * All of the error cases above bail out of the loop, so at
         * this point, the parsing is successful. If the requested
         * index matches, then fill the out_args structure and return,
         * or return -ENOENT for an empty entry.
         */
        rc = -ENOENT;
        if (cur_index == index)
        {
            if (!phandle)
                goto err;

            if (out_args)
            {
                int i;
                if (count > FDT_MAX_PHANDLE_ARGS)
                    count = FDT_MAX_PHANDLE_ARGS;
                out_args->np = node;
                out_args->args_count = count;
                for (i = 0; i < count; i++)
                    out_args->args[i] =
                        fdt32_to_cpu(*(list++));
            }
            else
            {
                dtb_node_put(node);
            }

            /* Found it! return success */
            return 0;
        }

        dtb_node_put(node);
        node = NULL;
        list += count;
        cur_index++;
    }

    /*
     * Unlock node before returning result; will be one of:
     * -ENOENT : index is for empty phandle
     * -EINVAL : parsing error on data
     * [1..n]  : Number of phandle (count mode; when index = -1)
     */
    rc = index < 0 ? cur_index : -ENOENT;
err:
    if (node)
        dtb_node_put(node);
    return rc;
}

struct dtb_node *dtb_node_parse_phandle(const struct dtb_node *dn,
                                   const char *phandle_name, int index)
{
    struct fdt_phandle_args args;

    if (index < 0)
        return NULL;

    if (__dtb_node_parse_phandle_with_args(dn, phandle_name, NULL, 0, index,
                                      &args))
        return NULL;

    return args.np;
}

int dtb_node_parse_phandle_with_args(const struct dtb_node *dn,
                                const char *list_name, const char *cells_name,
                                int index, struct fdt_phandle_args *out_args)
{
    if (index < 0)
        return -EINVAL;

    return __dtb_node_parse_phandle_with_args(dn, list_name, cells_name, 0,
                                         index, out_args);
}

int dtb_node_count_phandle_with_args(const struct dtb_node *dn,
                                const char *list_name, const char *cells_name)
{
    return __dtb_node_parse_phandle_with_args(dn, list_name, cells_name, 0,
                                         -1, NULL);
}