blob: 932a193af9822c5e7c217c68923b42bf14bf0d14 [file] [log] [blame]
David Gibson3da0f9a2006-11-27 16:21:28 +11001/*
2 * libfdt - Flat Device Tree manipulation
3 * Copyright (C) 2006 David Gibson, IBM Corporation.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public License
7 * as published by the Free Software Foundation; either version 2.1 of
8 * the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19#include "libfdt_env.h"
20
21#include <fdt.h>
22#include <libfdt.h>
23
24#include "libfdt_internal.h"
25
David Gibson73d60922006-12-15 15:12:47 +110026int fdt_setprop_inplace(void *fdt, int nodeoffset, const char *name,
David Gibson3da0f9a2006-11-27 16:21:28 +110027 const void *val, int len)
28{
29 void *propval;
30 int proplen;
David Gibson3da0f9a2006-11-27 16:21:28 +110031
David Gibsona6c76f92007-06-13 14:18:10 +100032 propval = fdt_getprop_w(fdt, nodeoffset, name, &proplen);
David Gibsona7ee95d2006-12-15 15:12:49 +110033 if (! propval)
David Gibson9a9fdf52006-12-15 15:12:51 +110034 return proplen;
David Gibson3da0f9a2006-11-27 16:21:28 +110035
36 if (proplen != len)
David Gibson3aea8282006-12-15 15:12:52 +110037 return -FDT_ERR_NOSPACE;
David Gibson3da0f9a2006-11-27 16:21:28 +110038
39 memcpy(propval, val, len);
40 return 0;
41}
42
43static void nop_region(void *start, int len)
44{
45 uint32_t *p;
46
47 for (p = start; (void *)p < (start + len); p++)
David Gibsone25487d2006-12-04 12:52:45 +110048 *p = cpu_to_fdt32(FDT_NOP);
David Gibson3da0f9a2006-11-27 16:21:28 +110049}
50
David Gibson73d60922006-12-15 15:12:47 +110051int fdt_nop_property(void *fdt, int nodeoffset, const char *name)
David Gibson3da0f9a2006-11-27 16:21:28 +110052{
53 struct fdt_property *prop;
54 int len;
David Gibson3da0f9a2006-11-27 16:21:28 +110055
David Gibsona6c76f92007-06-13 14:18:10 +100056 prop = fdt_get_property_w(fdt, nodeoffset, name, &len);
David Gibsona7ee95d2006-12-15 15:12:49 +110057 if (! prop)
David Gibson9a9fdf52006-12-15 15:12:51 +110058 return len;
David Gibson3da0f9a2006-11-27 16:21:28 +110059
60 nop_region(prop, len + sizeof(*prop));
61
62 return 0;
63}
64
David Gibson73d60922006-12-15 15:12:47 +110065int _fdt_node_end_offset(void *fdt, int nodeoffset)
David Gibson3da0f9a2006-11-27 16:21:28 +110066{
67 int level = 0;
David Gibson3da0f9a2006-11-27 16:21:28 +110068 uint32_t tag;
69 int offset, nextoffset;
70
71 tag = _fdt_next_tag(fdt, nodeoffset, &nextoffset);
72 if (tag != FDT_BEGIN_NODE)
David Gibson9a9fdf52006-12-15 15:12:51 +110073 return -FDT_ERR_BADOFFSET;
David Gibson3da0f9a2006-11-27 16:21:28 +110074 do {
75 offset = nextoffset;
76 tag = _fdt_next_tag(fdt, offset, &nextoffset);
77
78 switch (tag) {
79 case FDT_END:
David Gibson7ba551f2006-12-01 16:59:43 +110080 return offset;
David Gibson3da0f9a2006-11-27 16:21:28 +110081
82 case FDT_BEGIN_NODE:
83 level++;
84 break;
85
86 case FDT_END_NODE:
87 level--;
88 break;
89
90 case FDT_PROP:
91 case FDT_NOP:
92 break;
93
94 default:
David Gibson9a9fdf52006-12-15 15:12:51 +110095 return -FDT_ERR_BADSTRUCTURE;
David Gibson3da0f9a2006-11-27 16:21:28 +110096 }
97 } while (level >= 0);
98
David Gibson7ba551f2006-12-01 16:59:43 +110099 return nextoffset;
100}
David Gibson3da0f9a2006-11-27 16:21:28 +1100101
David Gibson73d60922006-12-15 15:12:47 +1100102int fdt_nop_node(void *fdt, int nodeoffset)
David Gibson7ba551f2006-12-01 16:59:43 +1100103{
104 int endoffset;
David Gibson7ba551f2006-12-01 16:59:43 +1100105
106 endoffset = _fdt_node_end_offset(fdt, nodeoffset);
David Gibson9a9fdf52006-12-15 15:12:51 +1100107 if (endoffset < 0)
108 return endoffset;
David Gibson7ba551f2006-12-01 16:59:43 +1100109
David Gibsona6c76f92007-06-13 14:18:10 +1000110 nop_region(fdt_offset_ptr_w(fdt, nodeoffset, 0), endoffset - nodeoffset);
David Gibson7ba551f2006-12-01 16:59:43 +1100111 return 0;
David Gibson3da0f9a2006-11-27 16:21:28 +1100112}