blob: d8ba8ec60c6ce6c1030e0e11f25ac456a301385a [file] [log] [blame]
developerd92db592022-04-13 17:09:05 +08001// SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause)
2/*
3 * libfdt - Flat Device Tree manipulation
4 * Copyright (C) 2014 David Gibson <david@gibson.dropbear.id.au>
5 * Copyright (C) 2018 embedded brains GmbH
6 */
7#include "libfdt_env.h"
8
9#include <fdt.h>
10#include <libfdt.h>
11
12#include "libfdt_internal.h"
13
14static int fdt_cells(const void *fdt, int nodeoffset, const char *name)
15{
16 const fdt32_t *c;
17 int val;
18 int len;
19
20 c = fdt_getprop(fdt, nodeoffset, name, &len);
21 if (!c)
22 return len;
23
24 if (len != sizeof(*c))
25 return -FDT_ERR_BADNCELLS;
26
27 val = fdt32_to_cpu(*c);
28 if ((val <= 0) || (val > FDT_MAX_NCELLS))
29 return -FDT_ERR_BADNCELLS;
30
31 return val;
32}
33
34int fdt_address_cells(const void *fdt, int nodeoffset)
35{
36 int val;
37
38 val = fdt_cells(fdt, nodeoffset, "#address-cells");
39 if (val == -FDT_ERR_NOTFOUND)
40 return 2;
41 return val;
42}
43
44int fdt_size_cells(const void *fdt, int nodeoffset)
45{
46 int val;
47
48 val = fdt_cells(fdt, nodeoffset, "#size-cells");
49 if (val == -FDT_ERR_NOTFOUND)
50 return 1;
51 return val;
52}
53
54/* This function assumes that [address|size]_cells is 1 or 2 */
55int fdt_appendprop_addrrange(void *fdt, int parent, int nodeoffset,
56 const char *name, uint64_t addr, uint64_t size)
57{
58 int addr_cells, size_cells, ret;
59 uint8_t data[sizeof(fdt64_t) * 2], *prop;
60
61 ret = fdt_address_cells(fdt, parent);
62 if (ret < 0)
63 return ret;
64 addr_cells = ret;
65
66 ret = fdt_size_cells(fdt, parent);
67 if (ret < 0)
68 return ret;
69 size_cells = ret;
70
71 /* check validity of address */
72 prop = data;
73 if (addr_cells == 1) {
74 if ((addr > UINT32_MAX) || ((UINT32_MAX + 1 - addr) < size))
75 return -FDT_ERR_BADVALUE;
76
77 fdt32_st(prop, (uint32_t)addr);
78 } else if (addr_cells == 2) {
79 fdt64_st(prop, addr);
80 } else {
81 return -FDT_ERR_BADNCELLS;
82 }
83
84 /* check validity of size */
85 prop += addr_cells * sizeof(fdt32_t);
86 if (size_cells == 1) {
87 if (size > UINT32_MAX)
88 return -FDT_ERR_BADVALUE;
89
90 fdt32_st(prop, (uint32_t)size);
91 } else if (size_cells == 2) {
92 fdt64_st(prop, size);
93 } else {
94 return -FDT_ERR_BADNCELLS;
95 }
96
97 return fdt_appendprop(fdt, nodeoffset, name, data,
98 (addr_cells + size_cells) * sizeof(fdt32_t));
99}