blob: c40ba094f1f87a4f85828fc25023f521111faf63 [file] [log] [blame]
Madhukar Pappireddy596fe0a2020-06-15 17:19:09 -05001// SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause)
Antonio Nino Diazb2db96f2018-10-19 00:56:54 +01002/*
3 * libfdt - Flat Device Tree manipulation
4 * Copyright (C) 2014 David Gibson <david@gibson.dropbear.id.au>
Madhukar Pappireddy596fe0a2020-06-15 17:19:09 -05005 * Copyright (C) 2018 embedded brains GmbH
Antonio Nino Diazb2db96f2018-10-19 00:56:54 +01006 */
7#include "libfdt_env.h"
8
9#include <fdt.h>
10#include <libfdt.h>
11
12#include "libfdt_internal.h"
13
Madhukar Pappireddy596fe0a2020-06-15 17:19:09 -050014static int fdt_cells(const void *fdt, int nodeoffset, const char *name)
Antonio Nino Diazb2db96f2018-10-19 00:56:54 +010015{
Madhukar Pappireddy596fe0a2020-06-15 17:19:09 -050016 const fdt32_t *c;
17 uint32_t val;
Antonio Nino Diazb2db96f2018-10-19 00:56:54 +010018 int len;
19
Madhukar Pappireddy596fe0a2020-06-15 17:19:09 -050020 c = fdt_getprop(fdt, nodeoffset, name, &len);
21 if (!c)
22 return len;
Antonio Nino Diazb2db96f2018-10-19 00:56:54 +010023
Madhukar Pappireddy596fe0a2020-06-15 17:19:09 -050024 if (len != sizeof(*c))
Antonio Nino Diazb2db96f2018-10-19 00:56:54 +010025 return -FDT_ERR_BADNCELLS;
26
Madhukar Pappireddy596fe0a2020-06-15 17:19:09 -050027 val = fdt32_to_cpu(*c);
28 if (val > FDT_MAX_NCELLS)
Antonio Nino Diazb2db96f2018-10-19 00:56:54 +010029 return -FDT_ERR_BADNCELLS;
30
Madhukar Pappireddy596fe0a2020-06-15 17:19:09 -050031 return (int)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 == 0)
40 return -FDT_ERR_BADNCELLS;
41 if (val == -FDT_ERR_NOTFOUND)
42 return 2;
Antonio Nino Diazb2db96f2018-10-19 00:56:54 +010043 return val;
44}
45
Antonio Nino Diazb2db96f2018-10-19 00:56:54 +010046int fdt_size_cells(const void *fdt, int nodeoffset)
47{
Antonio Nino Diaz39cb8292018-10-29 15:08:50 +000048 int val;
Antonio Nino Diaz39cb8292018-10-29 15:08:50 +000049
Madhukar Pappireddy596fe0a2020-06-15 17:19:09 -050050 val = fdt_cells(fdt, nodeoffset, "#size-cells");
51 if (val == -FDT_ERR_NOTFOUND)
52 return 1;
53 return val;
54}
55
56/* This function assumes that [address|size]_cells is 1 or 2 */
57int fdt_appendprop_addrrange(void *fdt, int parent, int nodeoffset,
58 const char *name, uint64_t addr, uint64_t size)
59{
60 int addr_cells, size_cells, ret;
61 uint8_t data[sizeof(fdt64_t) * 2], *prop;
Antonio Nino Diaz39cb8292018-10-29 15:08:50 +000062
Madhukar Pappireddy596fe0a2020-06-15 17:19:09 -050063 ret = fdt_address_cells(fdt, parent);
64 if (ret < 0)
65 return ret;
66 addr_cells = ret;
67
68 ret = fdt_size_cells(fdt, parent);
69 if (ret < 0)
70 return ret;
71 size_cells = ret;
72
73 /* check validity of address */
74 prop = data;
75 if (addr_cells == 1) {
Daniel Boulby2625ac02022-09-23 16:22:27 +010076 if ((addr > UINT32_MAX) || (((uint64_t) UINT32_MAX + 1 - addr) < size))
Madhukar Pappireddy596fe0a2020-06-15 17:19:09 -050077 return -FDT_ERR_BADVALUE;
78
79 fdt32_st(prop, (uint32_t)addr);
80 } else if (addr_cells == 2) {
81 fdt64_st(prop, addr);
82 } else {
Antonio Nino Diaz39cb8292018-10-29 15:08:50 +000083 return -FDT_ERR_BADNCELLS;
Madhukar Pappireddy596fe0a2020-06-15 17:19:09 -050084 }
Antonio Nino Diaz39cb8292018-10-29 15:08:50 +000085
Madhukar Pappireddy596fe0a2020-06-15 17:19:09 -050086 /* check validity of size */
87 prop += addr_cells * sizeof(fdt32_t);
88 if (size_cells == 1) {
89 if (size > UINT32_MAX)
90 return -FDT_ERR_BADVALUE;
91
92 fdt32_st(prop, (uint32_t)size);
93 } else if (size_cells == 2) {
94 fdt64_st(prop, size);
95 } else {
Antonio Nino Diaz39cb8292018-10-29 15:08:50 +000096 return -FDT_ERR_BADNCELLS;
Madhukar Pappireddy596fe0a2020-06-15 17:19:09 -050097 }
Antonio Nino Diaz39cb8292018-10-29 15:08:50 +000098
Madhukar Pappireddy596fe0a2020-06-15 17:19:09 -050099 return fdt_appendprop(fdt, nodeoffset, name, data,
100 (addr_cells + size_cells) * sizeof(fdt32_t));
Antonio Nino Diazb2db96f2018-10-19 00:56:54 +0100101}