blob: d4aa7a26c576c528a3511aa22f76f5e329179ecd [file] [log] [blame]
Andrew Scull18834872018-10-12 11:48:09 +01001/*
Andrew Walbran692b3252019-03-07 15:51:31 +00002 * Copyright 2018 The Hafnium Authors.
Andrew Scull18834872018-10-12 11:48:09 +01003 *
Andrew Walbrane959ec12020-06-17 15:01:09 +01004 * Use of this source code is governed by a BSD-style
5 * license that can be found in the LICENSE file or at
6 * https://opensource.org/licenses/BSD-3-Clause.
Andrew Scull18834872018-10-12 11:48:09 +01007 */
8
Andrew Scullfbc938a2018-08-20 14:09:28 +01009#pragma once
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010010
David Brazdilb856be62020-03-25 10:14:55 +000011#include "hf/memiter.h"
12#include "hf/string.h"
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010013
David Brazdilb856be62020-03-25 10:14:55 +000014/**
15 * Wrapper around a pointer to a Flattened Device Tree (FDT) structure located
16 * somewhere in mapped main memory. Sanity checks are performed on initilization
17 * to ensure it is pointing to a valid FDT and most libfdt API calls check for
18 * the presence of the FDT magic.
19 */
20struct fdt {
21 struct memiter buf;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010022};
23
David Brazdilb856be62020-03-25 10:14:55 +000024/**
25 * Wrapper around a pointer to a valid Device Tree node inside a FDT structure.
26 */
27struct fdt_node {
28 struct fdt fdt;
29 int offset;
30};
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010031
David Brazdilb856be62020-03-25 10:14:55 +000032#define FDT_V17_HEADER_SIZE (10 * sizeof(uint32_t))
33
34bool fdt_size_from_header(const void *ptr, size_t *val);
35
36bool fdt_init_from_ptr(struct fdt *fdt, const void *ptr, size_t len);
37bool fdt_init_from_memiter(struct fdt *fdt, const struct memiter *it);
38void fdt_fini(struct fdt *fdt);
39
40const void *fdt_base(const struct fdt *fdt);
41size_t fdt_size(const struct fdt *fdt);
42
43bool fdt_find_node(const struct fdt *fdt, const char *path,
44 struct fdt_node *node);
David Brazdilf4925382020-03-25 13:33:51 +000045bool fdt_is_compatible(struct fdt_node *node, const char *compat);
David Brazdilb856be62020-03-25 10:14:55 +000046bool fdt_address_size(const struct fdt_node *node, size_t *addr_size);
47bool fdt_size_size(const struct fdt_node *node, size_t *size);
48
49bool fdt_first_child(struct fdt_node *node);
50bool fdt_next_sibling(struct fdt_node *node);
51bool fdt_find_child(struct fdt_node *node, const struct string *name);
52
53bool fdt_read_property(const struct fdt_node *node, const char *name,
54 struct memiter *data);
55bool fdt_read_number(const struct fdt_node *node, const char *name,
56 uint64_t *val);
57bool fdt_parse_number(struct memiter *data, size_t size, uint64_t *val);