hc
2023-12-11 d2ccde1c8e90d38cee87a1b0309ad2827f3fd30d
kernel/scripts/dtc/libfdt/libfdt.h
....@@ -1,58 +1,17 @@
1
+/* SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause) */
12 #ifndef LIBFDT_H
23 #define LIBFDT_H
34 /*
45 * libfdt - Flat Device Tree manipulation
56 * Copyright (C) 2006 David Gibson, IBM Corporation.
6
- *
7
- * libfdt is dual licensed: you can use it either under the terms of
8
- * the GPL, or the BSD license, at your option.
9
- *
10
- * a) This library is free software; you can redistribute it and/or
11
- * modify it under the terms of the GNU General Public License as
12
- * published by the Free Software Foundation; either version 2 of the
13
- * License, or (at your option) any later version.
14
- *
15
- * This library is distributed in the hope that it will be useful,
16
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
- * GNU General Public License for more details.
19
- *
20
- * You should have received a copy of the GNU General Public
21
- * License along with this library; if not, write to the Free
22
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
23
- * MA 02110-1301 USA
24
- *
25
- * Alternatively,
26
- *
27
- * b) Redistribution and use in source and binary forms, with or
28
- * without modification, are permitted provided that the following
29
- * conditions are met:
30
- *
31
- * 1. Redistributions of source code must retain the above
32
- * copyright notice, this list of conditions and the following
33
- * disclaimer.
34
- * 2. Redistributions in binary form must reproduce the above
35
- * copyright notice, this list of conditions and the following
36
- * disclaimer in the documentation and/or other materials
37
- * provided with the distribution.
38
- *
39
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
40
- * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
41
- * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
42
- * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
44
- * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
49
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
50
- * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
51
- * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
527 */
538
549 #include "libfdt_env.h"
5510 #include "fdt.h"
11
+
12
+#ifdef __cplusplus
13
+extern "C" {
14
+#endif
5615
5716 #define FDT_FIRST_SUPPORTED_VERSION 0x02
5817 #define FDT_LAST_SUPPORTED_VERSION 0x11
....@@ -90,8 +49,9 @@
9049
9150 /* Error codes: codes for bad device tree blobs */
9251 #define FDT_ERR_TRUNCATED 8
93
- /* FDT_ERR_TRUNCATED: Structure block of the given device tree
94
- * ends without an FDT_END tag. */
52
+ /* FDT_ERR_TRUNCATED: FDT or a sub-block is improperly
53
+ * terminated (overflows, goes outside allowed bounds, or
54
+ * isn't properly terminated). */
9555 #define FDT_ERR_BADMAGIC 9
9656 /* FDT_ERR_BADMAGIC: Given "device tree" appears not to be a
9757 * device tree at all - it is missing the flattened device
....@@ -137,7 +97,15 @@
13797 /* FDT_ERR_NOPHANDLES: The device tree doesn't have any
13898 * phandle available anymore without causing an overflow */
13999
140
-#define FDT_ERR_MAX 17
100
+#define FDT_ERR_BADFLAGS 18
101
+ /* FDT_ERR_BADFLAGS: The function was passed a flags field that
102
+ * contains invalid flags or an invalid combination of flags. */
103
+
104
+#define FDT_ERR_MAX 18
105
+
106
+/* constants */
107
+#define FDT_MAX_PHANDLE 0xfffffffe
108
+ /* Valid values for phandles range from 1 to 2^32-2. */
141109
142110 /**********************************************************************/
143111 /* Low-level functions (you probably don't need these) */
....@@ -152,6 +120,61 @@
152120 }
153121
154122 uint32_t fdt_next_tag(const void *fdt, int offset, int *nextoffset);
123
+
124
+/*
125
+ * Alignment helpers:
126
+ * These helpers access words from a device tree blob. They're
127
+ * built to work even with unaligned pointers on platforms (ike
128
+ * ARM) that don't like unaligned loads and stores
129
+ */
130
+
131
+static inline uint32_t fdt32_ld(const fdt32_t *p)
132
+{
133
+ const uint8_t *bp = (const uint8_t *)p;
134
+
135
+ return ((uint32_t)bp[0] << 24)
136
+ | ((uint32_t)bp[1] << 16)
137
+ | ((uint32_t)bp[2] << 8)
138
+ | bp[3];
139
+}
140
+
141
+static inline void fdt32_st(void *property, uint32_t value)
142
+{
143
+ uint8_t *bp = (uint8_t *)property;
144
+
145
+ bp[0] = value >> 24;
146
+ bp[1] = (value >> 16) & 0xff;
147
+ bp[2] = (value >> 8) & 0xff;
148
+ bp[3] = value & 0xff;
149
+}
150
+
151
+static inline uint64_t fdt64_ld(const fdt64_t *p)
152
+{
153
+ const uint8_t *bp = (const uint8_t *)p;
154
+
155
+ return ((uint64_t)bp[0] << 56)
156
+ | ((uint64_t)bp[1] << 48)
157
+ | ((uint64_t)bp[2] << 40)
158
+ | ((uint64_t)bp[3] << 32)
159
+ | ((uint64_t)bp[4] << 24)
160
+ | ((uint64_t)bp[5] << 16)
161
+ | ((uint64_t)bp[6] << 8)
162
+ | bp[7];
163
+}
164
+
165
+static inline void fdt64_st(void *property, uint64_t value)
166
+{
167
+ uint8_t *bp = (uint8_t *)property;
168
+
169
+ bp[0] = value >> 56;
170
+ bp[1] = (value >> 48) & 0xff;
171
+ bp[2] = (value >> 40) & 0xff;
172
+ bp[3] = (value >> 32) & 0xff;
173
+ bp[4] = (value >> 24) & 0xff;
174
+ bp[5] = (value >> 16) & 0xff;
175
+ bp[6] = (value >> 8) & 0xff;
176
+ bp[7] = value & 0xff;
177
+}
155178
156179 /**********************************************************************/
157180 /* Traversal functions */
....@@ -195,7 +218,7 @@
195218 * ...
196219 * }
197220 *
198
- * if ((node < 0) && (node != -FDT_ERR_NOT_FOUND)) {
221
+ * if ((node < 0) && (node != -FDT_ERR_NOTFOUND)) {
199222 * Error handling
200223 * }
201224 *
....@@ -213,7 +236,7 @@
213236 /* General functions */
214237 /**********************************************************************/
215238 #define fdt_get_header(fdt, field) \
216
- (fdt32_to_cpu(((const struct fdt_header *)(fdt))->field))
239
+ (fdt32_ld(&((const struct fdt_header *)(fdt))->field))
217240 #define fdt_magic(fdt) (fdt_get_header(fdt, magic))
218241 #define fdt_totalsize(fdt) (fdt_get_header(fdt, totalsize))
219242 #define fdt_off_dt_struct(fdt) (fdt_get_header(fdt, off_dt_struct))
....@@ -244,18 +267,32 @@
244267 #undef fdt_set_hdr_
245268
246269 /**
247
- * fdt_check_header - sanity check a device tree or possible device tree
270
+ * fdt_header_size - return the size of the tree's header
271
+ * @fdt: pointer to a flattened device tree
272
+ */
273
+size_t fdt_header_size(const void *fdt);
274
+
275
+/**
276
+ * fdt_header_size_ - internal function which takes a version number
277
+ */
278
+size_t fdt_header_size_(uint32_t version);
279
+
280
+/**
281
+ * fdt_check_header - sanity check a device tree header
282
+
248283 * @fdt: pointer to data which might be a flattened device tree
249284 *
250285 * fdt_check_header() checks that the given buffer contains what
251
- * appears to be a flattened device tree with sane information in its
252
- * header.
286
+ * appears to be a flattened device tree, and that the header contains
287
+ * valid information (to the extent that can be determined from the
288
+ * header alone).
253289 *
254290 * returns:
255291 * 0, if the buffer appears to contain a valid device tree
256292 * -FDT_ERR_BADMAGIC,
257293 * -FDT_ERR_BADVERSION,
258
- * -FDT_ERR_BADSTATE, standard meanings, as above
294
+ * -FDT_ERR_BADSTATE,
295
+ * -FDT_ERR_TRUNCATED, standard meanings, as above
259296 */
260297 int fdt_check_header(const void *fdt);
261298
....@@ -284,6 +321,24 @@
284321 /* Read-only functions */
285322 /**********************************************************************/
286323
324
+int fdt_check_full(const void *fdt, size_t bufsize);
325
+
326
+/**
327
+ * fdt_get_string - retrieve a string from the strings block of a device tree
328
+ * @fdt: pointer to the device tree blob
329
+ * @stroffset: offset of the string within the strings block (native endian)
330
+ * @lenp: optional pointer to return the string's length
331
+ *
332
+ * fdt_get_string() retrieves a pointer to a single string from the
333
+ * strings block of the device tree blob at fdt, and optionally also
334
+ * returns the string's length in *lenp.
335
+ *
336
+ * returns:
337
+ * a pointer to the string, on success
338
+ * NULL, if stroffset is out of bounds, or doesn't point to a valid string
339
+ */
340
+const char *fdt_get_string(const void *fdt, int stroffset, int *lenp);
341
+
287342 /**
288343 * fdt_string - retrieve a string from the strings block of a device tree
289344 * @fdt: pointer to the device tree blob
....@@ -294,9 +349,23 @@
294349 *
295350 * returns:
296351 * a pointer to the string, on success
297
- * NULL, if stroffset is out of bounds
352
+ * NULL, if stroffset is out of bounds, or doesn't point to a valid string
298353 */
299354 const char *fdt_string(const void *fdt, int stroffset);
355
+
356
+/**
357
+ * fdt_find_max_phandle - find and return the highest phandle in a tree
358
+ * @fdt: pointer to the device tree blob
359
+ * @phandle: return location for the highest phandle value found in the tree
360
+ *
361
+ * fdt_find_max_phandle() finds the highest phandle value in the given device
362
+ * tree. The value returned in @phandle is only valid if the function returns
363
+ * success.
364
+ *
365
+ * returns:
366
+ * 0 on success or a negative error code on failure
367
+ */
368
+int fdt_find_max_phandle(const void *fdt, uint32_t *phandle);
300369
301370 /**
302371 * fdt_get_max_phandle - retrieves the highest phandle in a tree
....@@ -306,12 +375,39 @@
306375 * device tree. This will ignore badly formatted phandles, or phandles
307376 * with a value of 0 or -1.
308377 *
378
+ * This function is deprecated in favour of fdt_find_max_phandle().
379
+ *
309380 * returns:
310381 * the highest phandle on success
311382 * 0, if no phandle was found in the device tree
312383 * -1, if an error occurred
313384 */
314
-uint32_t fdt_get_max_phandle(const void *fdt);
385
+static inline uint32_t fdt_get_max_phandle(const void *fdt)
386
+{
387
+ uint32_t phandle;
388
+ int err;
389
+
390
+ err = fdt_find_max_phandle(fdt, &phandle);
391
+ if (err < 0)
392
+ return (uint32_t)-1;
393
+
394
+ return phandle;
395
+}
396
+
397
+/**
398
+ * fdt_generate_phandle - return a new, unused phandle for a device tree blob
399
+ * @fdt: pointer to the device tree blob
400
+ * @phandle: return location for the new phandle
401
+ *
402
+ * Walks the device tree blob and looks for the highest phandle value. On
403
+ * success, the new, unused phandle value (one higher than the previously
404
+ * highest phandle value in the device tree blob) will be returned in the
405
+ * @phandle parameter.
406
+ *
407
+ * Returns:
408
+ * 0 on success or a negative error-code on failure
409
+ */
410
+int fdt_generate_phandle(const void *fdt, uint32_t *phandle);
315411
316412 /**
317413 * fdt_num_mem_rsv - retrieve the number of memory reserve map entries
....@@ -503,7 +599,7 @@
503599 * ...
504600 * }
505601 *
506
- * if ((property < 0) && (property != -FDT_ERR_NOT_FOUND)) {
602
+ * if ((property < 0) && (property != -FDT_ERR_NOTFOUND)) {
507603 * Error handling
508604 * }
509605 *
....@@ -606,7 +702,7 @@
606702 /**
607703 * fdt_getprop_by_offset - retrieve the value of a property at a given offset
608704 * @fdt: pointer to the device tree blob
609
- * @ffset: offset of the property to read
705
+ * @offset: offset of the property to read
610706 * @namep: pointer to a string variable (will be overwritten) or NULL
611707 * @lenp: pointer to an integer variable (will be overwritten) or NULL
612708 *
....@@ -1090,7 +1186,7 @@
10901186 *
10911187 * returns:
10921188 * 0 <= n < FDT_MAX_NCELLS, on success
1093
- * 2, if the node has no #address-cells property
1189
+ * 1, if the node has no #size-cells property
10941190 * -FDT_ERR_BADNCELLS, if the node has a badly formatted or invalid
10951191 * #size-cells property
10961192 * -FDT_ERR_BADMAGIC,
....@@ -1297,7 +1393,45 @@
12971393 /* Sequential write functions */
12981394 /**********************************************************************/
12991395
1396
+/* fdt_create_with_flags flags */
1397
+#define FDT_CREATE_FLAG_NO_NAME_DEDUP 0x1
1398
+ /* FDT_CREATE_FLAG_NO_NAME_DEDUP: Do not try to de-duplicate property
1399
+ * names in the fdt. This can result in faster creation times, but
1400
+ * a larger fdt. */
1401
+
1402
+#define FDT_CREATE_FLAGS_ALL (FDT_CREATE_FLAG_NO_NAME_DEDUP)
1403
+
1404
+/**
1405
+ * fdt_create_with_flags - begin creation of a new fdt
1406
+ * @fdt: pointer to memory allocated where fdt will be created
1407
+ * @bufsize: size of the memory space at fdt
1408
+ * @flags: a valid combination of FDT_CREATE_FLAG_ flags, or 0.
1409
+ *
1410
+ * fdt_create_with_flags() begins the process of creating a new fdt with
1411
+ * the sequential write interface.
1412
+ *
1413
+ * fdt creation process must end with fdt_finished() to produce a valid fdt.
1414
+ *
1415
+ * returns:
1416
+ * 0, on success
1417
+ * -FDT_ERR_NOSPACE, bufsize is insufficient for a minimal fdt
1418
+ * -FDT_ERR_BADFLAGS, flags is not valid
1419
+ */
1420
+int fdt_create_with_flags(void *buf, int bufsize, uint32_t flags);
1421
+
1422
+/**
1423
+ * fdt_create - begin creation of a new fdt
1424
+ * @fdt: pointer to memory allocated where fdt will be created
1425
+ * @bufsize: size of the memory space at fdt
1426
+ *
1427
+ * fdt_create() is equivalent to fdt_create_with_flags() with flags=0.
1428
+ *
1429
+ * returns:
1430
+ * 0, on success
1431
+ * -FDT_ERR_NOSPACE, bufsize is insufficient for a minimal fdt
1432
+ */
13001433 int fdt_create(void *buf, int bufsize);
1434
+
13011435 int fdt_resize(void *fdt, void *buf, int bufsize);
13021436 int fdt_add_reservemap_entry(void *fdt, uint64_t addr, uint64_t size);
13031437 int fdt_finish_reservemap(void *fdt);
....@@ -1313,10 +1447,13 @@
13131447 fdt64_t tmp = cpu_to_fdt64(val);
13141448 return fdt_property(fdt, name, &tmp, sizeof(tmp));
13151449 }
1450
+
1451
+#ifndef SWIG /* Not available in Python */
13161452 static inline int fdt_property_cell(void *fdt, const char *name, uint32_t val)
13171453 {
13181454 return fdt_property_u32(fdt, name, val);
13191455 }
1456
+#endif
13201457
13211458 /**
13221459 * fdt_property_placeholder - add a new property and return a ptr to its value
....@@ -1766,6 +1903,43 @@
17661903 fdt_appendprop((fdt), (nodeoffset), (name), (str), strlen(str)+1)
17671904
17681905 /**
1906
+ * fdt_appendprop_addrrange - append a address range property
1907
+ * @fdt: pointer to the device tree blob
1908
+ * @parent: offset of the parent node
1909
+ * @nodeoffset: offset of the node to add a property at
1910
+ * @name: name of property
1911
+ * @addr: start address of a given range
1912
+ * @size: size of a given range
1913
+ *
1914
+ * fdt_appendprop_addrrange() appends an address range value (start
1915
+ * address and size) to the value of the named property in the given
1916
+ * node, or creates a new property with that value if it does not
1917
+ * already exist.
1918
+ * If "name" is not specified, a default "reg" is used.
1919
+ * Cell sizes are determined by parent's #address-cells and #size-cells.
1920
+ *
1921
+ * This function may insert data into the blob, and will therefore
1922
+ * change the offsets of some existing nodes.
1923
+ *
1924
+ * returns:
1925
+ * 0, on success
1926
+ * -FDT_ERR_BADLAYOUT,
1927
+ * -FDT_ERR_BADMAGIC,
1928
+ * -FDT_ERR_BADNCELLS, if the node has a badly formatted or invalid
1929
+ * #address-cells property
1930
+ * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1931
+ * -FDT_ERR_BADSTATE,
1932
+ * -FDT_ERR_BADSTRUCTURE,
1933
+ * -FDT_ERR_BADVERSION,
1934
+ * -FDT_ERR_BADVALUE, addr or size doesn't fit to respective cells size
1935
+ * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1936
+ * contain a new property
1937
+ * -FDT_ERR_TRUNCATED, standard meanings
1938
+ */
1939
+int fdt_appendprop_addrrange(void *fdt, int parent, int nodeoffset,
1940
+ const char *name, uint64_t addr, uint64_t size);
1941
+
1942
+/**
17691943 * fdt_delprop - delete a property
17701944 * @fdt: pointer to the device tree blob
17711945 * @nodeoffset: offset of the node whose property to nop
....@@ -1899,4 +2073,8 @@
18992073
19002074 const char *fdt_strerror(int errval);
19012075
2076
+#ifdef __cplusplus
2077
+}
2078
+#endif
2079
+
19022080 #endif /* LIBFDT_H */