Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 1 | /* |
| 2 | * libfdt - Flat Device Tree manipulation |
| 3 | * Copyright (C) 2006 David Gibson, IBM Corporation. |
Roger Meier | 3eaedea | 2013-07-27 01:12:38 +0200 | [diff] [blame] | 4 | * SPDX-License-Identifier: GPL-2.0+ BSD-2-Clause |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 5 | */ |
Robert P. J. Day | 55019c5 | 2016-05-23 05:40:55 -0400 | [diff] [blame] | 6 | #include <libfdt_env.h> |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 7 | |
Bartlomiej Sieka | 6125086 | 2008-02-29 16:00:24 +0100 | [diff] [blame] | 8 | #ifndef USE_HOSTCC |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 9 | #include <fdt.h> |
| 10 | #include <libfdt.h> |
Bartlomiej Sieka | 6125086 | 2008-02-29 16:00:24 +0100 | [diff] [blame] | 11 | #else |
| 12 | #include "fdt_host.h" |
| 13 | #endif |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 14 | |
| 15 | #include "libfdt_internal.h" |
| 16 | |
Maxime Ripard | 3559c79 | 2016-07-05 10:26:43 +0200 | [diff] [blame] | 17 | int fdt_setprop_inplace_namelen_partial(void *fdt, int nodeoffset, |
| 18 | const char *name, int namelen, |
Simon Glass | dfdcd6f | 2016-10-02 17:59:27 -0600 | [diff] [blame] | 19 | uint32_t idx, const void *val, |
Maxime Ripard | 3559c79 | 2016-07-05 10:26:43 +0200 | [diff] [blame] | 20 | 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 Glass | dfdcd6f | 2016-10-02 17:59:27 -0600 | [diff] [blame] | 30 | if (proplen < (len + idx)) |
Maxime Ripard | 3559c79 | 2016-07-05 10:26:43 +0200 | [diff] [blame] | 31 | return -FDT_ERR_NOSPACE; |
| 32 | |
Simon Glass | dfdcd6f | 2016-10-02 17:59:27 -0600 | [diff] [blame] | 33 | memcpy((char *)propval + idx, val, len); |
Maxime Ripard | 3559c79 | 2016-07-05 10:26:43 +0200 | [diff] [blame] | 34 | return 0; |
| 35 | } |
| 36 | |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 37 | int fdt_setprop_inplace(void *fdt, int nodeoffset, const char *name, |
| 38 | const void *val, int len) |
| 39 | { |
Maxime Ripard | 3559c79 | 2016-07-05 10:26:43 +0200 | [diff] [blame] | 40 | const void *propval; |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 41 | int proplen; |
| 42 | |
Maxime Ripard | 3559c79 | 2016-07-05 10:26:43 +0200 | [diff] [blame] | 43 | propval = fdt_getprop(fdt, nodeoffset, name, &proplen); |
Robert P. J. Day | 55019c5 | 2016-05-23 05:40:55 -0400 | [diff] [blame] | 44 | if (!propval) |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 45 | return proplen; |
| 46 | |
| 47 | if (proplen != len) |
| 48 | return -FDT_ERR_NOSPACE; |
| 49 | |
Maxime Ripard | 3559c79 | 2016-07-05 10:26:43 +0200 | [diff] [blame] | 50 | return fdt_setprop_inplace_namelen_partial(fdt, nodeoffset, name, |
| 51 | strlen(name), 0, |
| 52 | val, len); |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 53 | } |
| 54 | |
David Gibson | 9457d9d | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 55 | static void _fdt_nop_region(void *start, int len) |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 56 | { |
Kim Phillips | 38c3815 | 2013-01-16 13:59:50 +0000 | [diff] [blame] | 57 | fdt32_t *p; |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 58 | |
David Gibson | b4141b8 | 2008-07-07 10:10:48 +1000 | [diff] [blame] | 59 | for (p = start; (char *)p < ((char *)start + len); p++) |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 60 | *p = cpu_to_fdt32(FDT_NOP); |
| 61 | } |
| 62 | |
| 63 | int fdt_nop_property(void *fdt, int nodeoffset, const char *name) |
| 64 | { |
| 65 | struct fdt_property *prop; |
| 66 | int len; |
| 67 | |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 68 | prop = fdt_get_property_w(fdt, nodeoffset, name, &len); |
Robert P. J. Day | 55019c5 | 2016-05-23 05:40:55 -0400 | [diff] [blame] | 69 | if (!prop) |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 70 | return len; |
| 71 | |
David Gibson | 9457d9d | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 72 | _fdt_nop_region(prop, len + sizeof(*prop)); |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 73 | |
| 74 | return 0; |
| 75 | } |
| 76 | |
David Gibson | 954914a | 2009-02-06 14:01:56 +1100 | [diff] [blame] | 77 | int _fdt_node_end_offset(void *fdt, int offset) |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 78 | { |
David Gibson | 954914a | 2009-02-06 14:01:56 +1100 | [diff] [blame] | 79 | int depth = 0; |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 80 | |
David Gibson | 954914a | 2009-02-06 14:01:56 +1100 | [diff] [blame] | 81 | while ((offset >= 0) && (depth >= 0)) |
| 82 | offset = fdt_next_node(fdt, offset, &depth); |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 83 | |
David Gibson | 954914a | 2009-02-06 14:01:56 +1100 | [diff] [blame] | 84 | return offset; |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | int 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 Gibson | 9457d9d | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 95 | _fdt_nop_region(fdt_offset_ptr_w(fdt, nodeoffset, 0), |
| 96 | endoffset - nodeoffset); |
Gerald Van Baren | c077a13 | 2007-04-14 22:46:41 -0400 | [diff] [blame] | 97 | return 0; |
| 98 | } |