1 // Copyright 2018 The Fuchsia Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #pragma once
6 
7 #include <zircon/compiler.h>
8 #include <stdint.h>
9 
10 __BEGIN_CDECLS;
11 
12 typedef struct dt_slice {
13     uint8_t *data;
14     uint32_t size;
15 } dt_slice_t;
16 
17 struct devicetree_header {
18     uint32_t magic;
19     uint32_t size;
20     uint32_t off_struct;        // offset from start to DT 'structure'
21     uint32_t off_strings;       // offset from start to stringdata
22     uint32_t off_reserve;       // offset from start to reserve memory map
23     uint32_t version;
24     uint32_t version_compat;    // last compatible version
25     uint32_t boot_cpuid;
26     uint32_t sz_strings;        // size of stringdata
27     uint32_t sz_struct;         // size of DT 'structure'
28 };
29 
30 typedef struct devicetree {
31     dt_slice_t top;
32     dt_slice_t dt;
33     dt_slice_t ds;
34     struct devicetree_header hdr;
35     void (*error)(const char *msg);
36 } devicetree_t;
37 
38 typedef int (*dt_node_cb)(int depth, const char *name, void *cookie);
39 typedef int (*dt_prop_cb)(const char *name, uint8_t *data, uint32_t size, void *cookie);
40 
41 int dt_init(devicetree_t *dt, void *data, uint32_t len);
42 int dt_walk(devicetree_t *dt, dt_node_cb ncb, dt_prop_cb pcb, void *cookie);
43 
44 uint32_t dt_rd32(uint8_t *data);
45 void dt_wr32(uint32_t n, uint8_t *data);
46 
47 __END_CDECLS;
48