blob: 71753cec0bdf98b0d48d23f29695e98949ab4e85 [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 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Andrew Scullfbc938a2018-08-20 14:09:28 +010017#pragma once
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010018
David Brazdilb856be62020-03-25 10:14:55 +000019#include "hf/memiter.h"
20#include "hf/string.h"
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010021
David Brazdilb856be62020-03-25 10:14:55 +000022/**
23 * Wrapper around a pointer to a Flattened Device Tree (FDT) structure located
24 * somewhere in mapped main memory. Sanity checks are performed on initilization
25 * to ensure it is pointing to a valid FDT and most libfdt API calls check for
26 * the presence of the FDT magic.
27 */
28struct fdt {
29 struct memiter buf;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010030};
31
David Brazdilb856be62020-03-25 10:14:55 +000032/**
33 * Wrapper around a pointer to a valid Device Tree node inside a FDT structure.
34 */
35struct fdt_node {
36 struct fdt fdt;
37 int offset;
38};
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010039
David Brazdilb856be62020-03-25 10:14:55 +000040#define FDT_V17_HEADER_SIZE (10 * sizeof(uint32_t))
41
42bool fdt_size_from_header(const void *ptr, size_t *val);
43
44bool fdt_init_from_ptr(struct fdt *fdt, const void *ptr, size_t len);
45bool fdt_init_from_memiter(struct fdt *fdt, const struct memiter *it);
46void fdt_fini(struct fdt *fdt);
47
48const void *fdt_base(const struct fdt *fdt);
49size_t fdt_size(const struct fdt *fdt);
50
51bool fdt_find_node(const struct fdt *fdt, const char *path,
52 struct fdt_node *node);
David Brazdilf4925382020-03-25 13:33:51 +000053bool fdt_is_compatible(struct fdt_node *node, const char *compat);
David Brazdilb856be62020-03-25 10:14:55 +000054bool fdt_address_size(const struct fdt_node *node, size_t *addr_size);
55bool fdt_size_size(const struct fdt_node *node, size_t *size);
56
57bool fdt_first_child(struct fdt_node *node);
58bool fdt_next_sibling(struct fdt_node *node);
59bool fdt_find_child(struct fdt_node *node, const struct string *name);
60
61bool fdt_read_property(const struct fdt_node *node, const char *name,
62 struct memiter *data);
63bool fdt_read_number(const struct fdt_node *node, const char *name,
64 uint64_t *val);
65bool fdt_parse_number(struct memiter *data, size_t size, uint64_t *val);