1/*
2Copyright (c) 2016 Raspberry Pi (Trading) Ltd.
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions are met:
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright
10 notice, this list of conditions and the following disclaimer in the
11 documentation and/or other materials provided with the distribution.
12 * Neither the name of the copyright holder nor the
13 names of its contributors may be used to endorse or promote products
14 derived from this software without specific prior written permission.
15
16THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*/
27
28#ifndef DTOVERLAY_H
29#define DTOVERLAY_H
30
31#include <stdarg.h>
32
33#define BE4(x) ((x)>>24)&0xff, ((x)>>16)&0xff, ((x)>>8)&0xff, ((x)>>0)&0xff
34#define GETBE4(p, off) ((((unsigned char *)p)[off + 0]<<24) + (((unsigned char *)p)[off + 1]<<16) + \
35 (((unsigned char *)p)[off + 2]<<8) + (((unsigned char *)p)[off + 3]<<0))
36#define SETBE4(p, off, x) do { \
37 ((unsigned char *)p)[off + 0] = ((x>>24) & 0xff); \
38 ((unsigned char *)p)[off + 1] = ((x>>16) & 0xff); \
39 ((unsigned char *)p)[off + 2] = ((x>>8) & 0xff); \
40 ((unsigned char *)p)[off + 3] = ((x>>0) & 0xff); \
41} while (0)
42
43#define NON_FATAL(err) (((err) < 0) ? -(err) : (err))
44#define IS_FATAL(err) ((err) < 0)
45#define ONLY_FATAL(err) (IS_FATAL(err) ? (err) : 0)
46
47#define DTOVERLAY_PADDING(size) (-(size))
48
49typedef enum
50{
51 DTOVERLAY_ERROR,
52 DTOVERLAY_DEBUG
53} dtoverlay_logging_type_t;
54
55typedef struct dtoverlay_struct
56{
57 const char *param;
58 int len;
59 const char *b;
60} DTOVERLAY_PARAM_T;
61
62typedef struct dtblob_struct
63{
64 void *fdt;
65 char fdt_is_malloced;
66 char trailer_is_malloced;
67 char fixups_applied;
68 int min_phandle;
69 int max_phandle;
70 void *trailer;
71 int trailer_len;
72} DTBLOB_T;
73
74typedef struct pin_iter_struct
75{
76 DTBLOB_T *dtb;
77 const void *pinctrl;
78 int pinctrl_len;
79 int pinctrl_off;
80 const void *pins;
81 const void *funcs;
82 const void *pulls;
83 int pins_len;
84 int pin_off;
85 int funcs_len;
86 int pulls_len;
87} PIN_ITER_T;
88
89typedef void DTOVERLAY_LOGGING_FUNC(dtoverlay_logging_type_t type,
90 const char *fmt, va_list args);
91
92typedef int (*override_callback_t)(int override_type,
93 const char *override_value,
94 DTBLOB_T *dtb, int node_off,
95 const char *prop_name, int target_phandle,
96 int target_off, int target_size,
97 void *callback_state);
98
99uint8_t dtoverlay_read_u8(const void *src, int off);
100uint16_t dtoverlay_read_u16(const void *src, int off);
101uint32_t dtoverlay_read_u32(const void *src, int off);
102uint64_t dtoverlay_read_u64(const void *src, int off);
103void dtoverlay_write_u8(void *dst, int off, uint32_t val);
104void dtoverlay_write_u16(void *dst, int off, uint32_t val);
105void dtoverlay_write_u32(void *dst, int off, uint32_t val);
106void dtoverlay_write_u64(void *dst, int off, uint64_t val);
107
108/* Return values: -ve = fatal error, positive = non-fatal error */
109int dtoverlay_create_node(DTBLOB_T *dtb, const char *node_name, int path_len);
110
111int dtoverlay_delete_node(DTBLOB_T *dtb, const char *node_name, int path_len);
112
113int dtoverlay_find_node(DTBLOB_T *dtb, const char *node_path, int path_len);
114
115int dtoverlay_set_node_properties(DTBLOB_T *dtb, const char *node_path,
116 DTOVERLAY_PARAM_T *properties,
117 unsigned int num_properties);
118
119int dtoverlay_create_prop_fragment(DTBLOB_T *dtb, int idx, int target_phandle,
120 const char *prop_name, const void *prop_data,
121 int prop_len);
122
123int dtoverlay_fixup_overlay(DTBLOB_T *base_dtb, DTBLOB_T *overlay_dtb);
124
125int dtoverlay_merge_overlay(DTBLOB_T *base_dtb, DTBLOB_T *overlay_dtb);
126
127int dtoverlay_merge_params(DTBLOB_T *dtb, const DTOVERLAY_PARAM_T *params,
128 unsigned int num_params);
129
130const char *dtoverlay_find_override(DTBLOB_T *dtb, const char *override_name,
131 int *data_len);
132
133int dtoverlay_override_one_target(int override_type,
134 const char *override_value,
135 DTBLOB_T *dtb, int node_off,
136 const char *prop_name, int target_phandle,
137 int target_off, int target_size,
138 void *callback_state);
139
140int dtoverlay_foreach_override_target(DTBLOB_T *dtb, const char *override_name,
141 const char *override_data, int data_len,
142 const char *override_value,
143 override_callback_t callback,
144 void *callback_value);
145
146int dtoverlay_apply_override(DTBLOB_T *dtb, const char *override_name,
147 const char *override_data, int data_len,
148 const char *override_value);
149
150int dtoverlay_set_synonym(DTBLOB_T *dtb, const char *dst, const char *src);
151
152int dtoverlay_dup_property(DTBLOB_T *dtb, const char *node_name,
153 const char *dst, const char *src);
154
155DTBLOB_T *dtoverlay_create_dtb(int max_size);
156
157DTBLOB_T *dtoverlay_load_dtb_from_fp(FILE *fp, int max_size);
158
159DTBLOB_T *dtoverlay_load_dtb(const char *filename, int max_size);
160
161DTBLOB_T *dtoverlay_import_fdt(void *fdt, int max_size);
162
163int dtoverlay_save_dtb(const DTBLOB_T *dtb, const char *filename);
164
165int dtoverlay_extend_dtb(DTBLOB_T *dtb, int new_size);
166
167int dtoverlay_dtb_totalsize(DTBLOB_T *dtb);
168
169void dtoverlay_pack_dtb(DTBLOB_T *dtb);
170
171void dtoverlay_free_dtb(DTBLOB_T *dtb);
172
173static inline void *dtoverlay_dtb_trailer(DTBLOB_T *dtb)
174{
175 return dtb->trailer;
176}
177
178static inline int dtoverlay_dtb_trailer_len(DTBLOB_T *dtb)
179{
180 return dtb->trailer_len;
181}
182
183static inline void dtoverlay_dtb_set_trailer(DTBLOB_T *dtb,
184 void *trailer,
185 int trailer_len)
186{
187 dtb->trailer = trailer;
188 dtb->trailer_len = trailer_len;
189 dtb->trailer_is_malloced = 0;
190}
191
192int dtoverlay_find_pins_for_device(DTBLOB_T *dtb, const char *symbol,
193 PIN_ITER_T *iter);
194
195int dtoverlay_next_pin(PIN_ITER_T *iter, int *pin, int *func, int *pull);
196
197int dtoverlay_find_phandle(DTBLOB_T *dtb, int phandle);
198
199int dtoverlay_find_symbol(DTBLOB_T *dtb, const char *symbol_name);
200
201int dtoverlay_find_matching_node(DTBLOB_T *dtb, const char **node_names,
202 int pos);
203
204int dtoverlay_node_is_enabled(DTBLOB_T *dtb, int pos);
205
206const void *dtoverlay_get_property(DTBLOB_T *dtb, int pos,
207 const char *prop_name, int *prop_len);
208
209int dtoverlay_set_property(DTBLOB_T *dtb, int pos,
210 const char *prop_name, const void *prop, int prop_len);
211
212const char *dtoverlay_get_alias(DTBLOB_T *dtb, const char *alias_name);
213
214int dtoverlay_set_alias(DTBLOB_T *dtb, const char *alias_name, const char *value);
215
216void dtoverlay_set_logging_func(DTOVERLAY_LOGGING_FUNC *func);
217
218void dtoverlay_enable_debug(int enable);
219
220void dtoverlay_error(const char *fmt, ...);
221
222void dtoverlay_debug(const char *fmt, ...);
223
224#endif
225