2019-06-20 23:19:39 +02:00
|
|
|
// SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause)
|
2014-05-11 05:13:46 +02:00
|
|
|
/*
|
|
|
|
* libfdt - Flat Device Tree manipulation
|
|
|
|
* Copyright (C) 2014 David Gibson <david@gibson.dropbear.id.au>
|
2018-07-19 08:19:43 +02:00
|
|
|
* Copyright (C) 2018 embedded brains GmbH
|
2014-05-11 05:13:46 +02:00
|
|
|
*/
|
|
|
|
#include "libfdt_env.h"
|
|
|
|
|
|
|
|
#include <fdt.h>
|
|
|
|
#include <libfdt.h>
|
|
|
|
|
|
|
|
#include "libfdt_internal.h"
|
|
|
|
|
2018-07-19 08:19:43 +02:00
|
|
|
static int fdt_cells(const void *fdt, int nodeoffset, const char *name)
|
2014-05-11 05:13:46 +02:00
|
|
|
{
|
2018-07-19 08:19:43 +02:00
|
|
|
const fdt32_t *c;
|
2019-07-28 08:25:51 +02:00
|
|
|
uint32_t val;
|
2014-05-11 05:13:46 +02:00
|
|
|
int len;
|
|
|
|
|
2018-07-19 08:19:43 +02:00
|
|
|
c = fdt_getprop(fdt, nodeoffset, name, &len);
|
|
|
|
if (!c)
|
2018-11-02 02:46:22 +01:00
|
|
|
return len;
|
2014-05-11 05:13:46 +02:00
|
|
|
|
2018-07-19 08:19:43 +02:00
|
|
|
if (len != sizeof(*c))
|
2014-05-11 05:13:46 +02:00
|
|
|
return -FDT_ERR_BADNCELLS;
|
|
|
|
|
2018-07-19 08:19:43 +02:00
|
|
|
val = fdt32_to_cpu(*c);
|
2019-07-28 08:25:51 +02:00
|
|
|
if (val > FDT_MAX_NCELLS)
|
2014-05-11 05:13:46 +02:00
|
|
|
return -FDT_ERR_BADNCELLS;
|
|
|
|
|
2019-07-28 08:25:51 +02:00
|
|
|
return (int)val;
|
2014-05-11 05:13:46 +02:00
|
|
|
}
|
|
|
|
|
2018-07-19 08:19:43 +02:00
|
|
|
int fdt_address_cells(const void *fdt, int nodeoffset)
|
2014-05-11 05:13:46 +02:00
|
|
|
{
|
2018-11-02 02:46:22 +01:00
|
|
|
int val;
|
|
|
|
|
|
|
|
val = fdt_cells(fdt, nodeoffset, "#address-cells");
|
2019-07-28 08:25:51 +02:00
|
|
|
if (val == 0)
|
|
|
|
return -FDT_ERR_BADNCELLS;
|
2018-11-02 02:46:22 +01:00
|
|
|
if (val == -FDT_ERR_NOTFOUND)
|
|
|
|
return 2;
|
|
|
|
return val;
|
2018-07-19 08:19:43 +02:00
|
|
|
}
|
2014-05-11 05:13:46 +02:00
|
|
|
|
2018-07-19 08:19:43 +02:00
|
|
|
int fdt_size_cells(const void *fdt, int nodeoffset)
|
|
|
|
{
|
2018-11-02 02:46:22 +01:00
|
|
|
int val;
|
|
|
|
|
|
|
|
val = fdt_cells(fdt, nodeoffset, "#size-cells");
|
|
|
|
if (val == -FDT_ERR_NOTFOUND)
|
|
|
|
return 1;
|
|
|
|
return val;
|
2014-05-11 05:13:46 +02:00
|
|
|
}
|
2019-03-27 07:15:52 +01:00
|
|
|
|
|
|
|
/* This function assumes that [address|size]_cells is 1 or 2 */
|
|
|
|
int fdt_appendprop_addrrange(void *fdt, int parent, int nodeoffset,
|
|
|
|
const char *name, uint64_t addr, uint64_t size)
|
|
|
|
{
|
|
|
|
int addr_cells, size_cells, ret;
|
|
|
|
uint8_t data[sizeof(fdt64_t) * 2], *prop;
|
|
|
|
|
|
|
|
ret = fdt_address_cells(fdt, parent);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
addr_cells = ret;
|
|
|
|
|
|
|
|
ret = fdt_size_cells(fdt, parent);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
size_cells = ret;
|
|
|
|
|
|
|
|
/* check validity of address */
|
|
|
|
prop = data;
|
|
|
|
if (addr_cells == 1) {
|
2022-05-30 21:57:34 +02:00
|
|
|
if ((addr > UINT32_MAX) || (((uint64_t) UINT32_MAX + 1 - addr) < size))
|
2019-03-27 07:15:52 +01:00
|
|
|
return -FDT_ERR_BADVALUE;
|
|
|
|
|
|
|
|
fdt32_st(prop, (uint32_t)addr);
|
|
|
|
} else if (addr_cells == 2) {
|
|
|
|
fdt64_st(prop, addr);
|
|
|
|
} else {
|
|
|
|
return -FDT_ERR_BADNCELLS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check validity of size */
|
|
|
|
prop += addr_cells * sizeof(fdt32_t);
|
|
|
|
if (size_cells == 1) {
|
|
|
|
if (size > UINT32_MAX)
|
|
|
|
return -FDT_ERR_BADVALUE;
|
|
|
|
|
|
|
|
fdt32_st(prop, (uint32_t)size);
|
|
|
|
} else if (size_cells == 2) {
|
|
|
|
fdt64_st(prop, size);
|
|
|
|
} else {
|
|
|
|
return -FDT_ERR_BADNCELLS;
|
|
|
|
}
|
|
|
|
|
|
|
|
return fdt_appendprop(fdt, nodeoffset, name, data,
|
|
|
|
(addr_cells + size_cells) * sizeof(fdt32_t));
|
|
|
|
}
|