blob: 7274c81b62ee75d622cf4f09dc3b5a0f935126d7 [file] [log] [blame]
Gerald Van Baren9e61ed82007-03-31 12:00:56 -04001/*
2 * libfdt - Flat Device Tree manipulation
3 * Copyright (C) 2006 David Gibson, IBM Corporation.
Roger Meier3eaedea2013-07-27 01:12:38 +02004 * SPDX-License-Identifier: GPL-2.0+ BSD-2-Clause
Gerald Van Baren9e61ed82007-03-31 12:00:56 -04005 */
Robert P. J. Day55019c52016-05-23 05:40:55 -04006#include <libfdt_env.h>
Gerald Van Baren9e61ed82007-03-31 12:00:56 -04007
Bartlomiej Sieka61250862008-02-29 16:00:24 +01008#ifndef USE_HOSTCC
Gerald Van Baren9e61ed82007-03-31 12:00:56 -04009#include <fdt.h>
10#include <libfdt.h>
Bartlomiej Sieka61250862008-02-29 16:00:24 +010011#else
12#include "fdt_host.h"
13#endif
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040014
15#include "libfdt_internal.h"
16
Maxime Ripard3559c792016-07-05 10:26:43 +020017int fdt_setprop_inplace_namelen_partial(void *fdt, int nodeoffset,
18 const char *name, int namelen,
Simon Glassdfdcd6f2016-10-02 17:59:27 -060019 uint32_t idx, const void *val,
Maxime Ripard3559c792016-07-05 10:26:43 +020020 int len)
21{
22 void *propval;
23 int proplen;
24
25 propval = fdt_getprop_namelen_w(fdt, nodeoffset, name, namelen,
26 &proplen);
27 if (!propval)
28 return proplen;
29
Simon Glassdfdcd6f2016-10-02 17:59:27 -060030 if (proplen < (len + idx))
Maxime Ripard3559c792016-07-05 10:26:43 +020031 return -FDT_ERR_NOSPACE;
32
Simon Glassdfdcd6f2016-10-02 17:59:27 -060033 memcpy((char *)propval + idx, val, len);
Maxime Ripard3559c792016-07-05 10:26:43 +020034 return 0;
35}
36
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040037int fdt_setprop_inplace(void *fdt, int nodeoffset, const char *name,
38 const void *val, int len)
39{
Maxime Ripard3559c792016-07-05 10:26:43 +020040 const void *propval;
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040041 int proplen;
42
Maxime Ripard3559c792016-07-05 10:26:43 +020043 propval = fdt_getprop(fdt, nodeoffset, name, &proplen);
Robert P. J. Day55019c52016-05-23 05:40:55 -040044 if (!propval)
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040045 return proplen;
46
47 if (proplen != len)
48 return -FDT_ERR_NOSPACE;
49
Maxime Ripard3559c792016-07-05 10:26:43 +020050 return fdt_setprop_inplace_namelen_partial(fdt, nodeoffset, name,
51 strlen(name), 0,
52 val, len);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040053}
54
David Gibson9457d9d2008-07-09 14:10:24 +100055static void _fdt_nop_region(void *start, int len)
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040056{
Kim Phillips38c38152013-01-16 13:59:50 +000057 fdt32_t *p;
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040058
David Gibsonb4141b82008-07-07 10:10:48 +100059 for (p = start; (char *)p < ((char *)start + len); p++)
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040060 *p = cpu_to_fdt32(FDT_NOP);
61}
62
63int fdt_nop_property(void *fdt, int nodeoffset, const char *name)
64{
65 struct fdt_property *prop;
66 int len;
67
Kumar Galac8ab7052007-10-24 11:04:22 -050068 prop = fdt_get_property_w(fdt, nodeoffset, name, &len);
Robert P. J. Day55019c52016-05-23 05:40:55 -040069 if (!prop)
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040070 return len;
71
David Gibson9457d9d2008-07-09 14:10:24 +100072 _fdt_nop_region(prop, len + sizeof(*prop));
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040073
74 return 0;
75}
76
David Gibson954914a2009-02-06 14:01:56 +110077int _fdt_node_end_offset(void *fdt, int offset)
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040078{
David Gibson954914a2009-02-06 14:01:56 +110079 int depth = 0;
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040080
David Gibson954914a2009-02-06 14:01:56 +110081 while ((offset >= 0) && (depth >= 0))
82 offset = fdt_next_node(fdt, offset, &depth);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040083
David Gibson954914a2009-02-06 14:01:56 +110084 return offset;
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040085}
86
87int fdt_nop_node(void *fdt, int nodeoffset)
88{
89 int endoffset;
90
91 endoffset = _fdt_node_end_offset(fdt, nodeoffset);
92 if (endoffset < 0)
93 return endoffset;
94
David Gibson9457d9d2008-07-09 14:10:24 +100095 _fdt_nop_region(fdt_offset_ptr_w(fdt, nodeoffset, 0),
96 endoffset - nodeoffset);
Gerald Van Barenc077a132007-04-14 22:46:41 -040097 return 0;
98}