1 /*
2  * Copyright 2018 The Hafnium Authors.
3  *
4  * 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.
7  */
8 
9 #pragma once
10 
11 #include "hf/memiter.h"
12 #include "hf/string.h"
13 
14 /**
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  */
20 struct fdt {
21 	struct memiter buf;
22 };
23 
24 /**
25  * Wrapper around a pointer to a valid Device Tree node inside a FDT structure.
26  */
27 struct fdt_node {
28 	struct fdt fdt;
29 	int offset;
30 };
31 
32 #define FDT_V17_HEADER_SIZE (10 * sizeof(uint32_t))
33 
34 bool fdt_size_from_header(const void *ptr, size_t *val);
35 
36 bool fdt_init_from_ptr(struct fdt *fdt, const void *ptr, size_t len);
37 bool fdt_init_from_memiter(struct fdt *fdt, const struct memiter *it);
38 void fdt_fini(struct fdt *fdt);
39 
40 const void *fdt_base(const struct fdt *fdt);
41 size_t fdt_size(const struct fdt *fdt);
42 
43 bool fdt_find_node(const struct fdt *fdt, const char *path,
44 		   struct fdt_node *node);
45 bool fdt_is_compatible(struct fdt_node *node, const char *compat);
46 bool fdt_address_size(const struct fdt_node *node, size_t *addr_size);
47 bool fdt_size_size(const struct fdt_node *node, size_t *size);
48 
49 bool fdt_first_child(struct fdt_node *node);
50 bool fdt_next_sibling(struct fdt_node *node);
51 bool fdt_find_child(struct fdt_node *node, const struct string *name);
52 
53 bool fdt_read_property(const struct fdt_node *node, const char *name,
54 		       struct memiter *data);
55 bool fdt_read_number(const struct fdt_node *node, const char *name,
56 		     uint64_t *val);
57 bool fdt_parse_number(struct memiter *data, size_t size, uint64_t *val);
58