1 /* 2 * libfdt - Flat Device Tree manipulation 3 * Copyright (C) 2014 David Gibson <david@gibson.dropbear.id.au> 4 * Copyright (C) 2018 embedded brains GmbH 5 * 6 * libfdt is dual licensed: you can use it either under the terms of 7 * the GPL, or the BSD license, at your option. 8 * 9 * a) This library is free software; you can redistribute it and/or 10 * modify it under the terms of the GNU General Public License as 11 * published by the Free Software Foundation; either version 2 of the 12 * License, or (at your option) any later version. 13 * 14 * This library is distributed in the hope that it will be useful, 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 * GNU General Public License for more details. 18 * 19 * You should have received a copy of the GNU General Public 20 * License along with this library; if not, write to the Free 21 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, 22 * MA 02110-1301 USA 23 * 24 * Alternatively, 25 * 26 * b) Redistribution and use in source and binary forms, with or 27 * without modification, are permitted provided that the following 28 * conditions are met: 29 * 30 * 1. Redistributions of source code must retain the above 31 * copyright notice, this list of conditions and the following 32 * disclaimer. 33 * 2. Redistributions in binary form must reproduce the above 34 * copyright notice, this list of conditions and the following 35 * disclaimer in the documentation and/or other materials 36 * provided with the distribution. 37 * 38 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 39 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 40 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 41 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 42 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 43 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 48 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 49 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 50 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 51 */ 52 #include "libfdt_env.h" 53 54 #include <fdt.h> 55 #include <libfdt.h> 56 57 #include "libfdt_internal.h" 58 59 static int fdt_cells(const void *fdt, int nodeoffset, const char *name) 60 { 61 const fdt32_t *c; 62 int val; 63 int len; 64 65 c = fdt_getprop(fdt, nodeoffset, name, &len); 66 if (!c) 67 return len; 68 69 if (len != sizeof(*c)) 70 return -FDT_ERR_BADNCELLS; 71 72 val = fdt32_to_cpu(*c); 73 if ((val <= 0) || (val > FDT_MAX_NCELLS)) 74 return -FDT_ERR_BADNCELLS; 75 76 return val; 77 } 78 79 int fdt_address_cells(const void *fdt, int nodeoffset) 80 { 81 int val; 82 83 val = fdt_cells(fdt, nodeoffset, "#address-cells"); 84 if (val == -FDT_ERR_NOTFOUND) 85 return 2; 86 return val; 87 } 88 89 int fdt_size_cells(const void *fdt, int nodeoffset) 90 { 91 int val; 92 93 val = fdt_cells(fdt, nodeoffset, "#size-cells"); 94 if (val == -FDT_ERR_NOTFOUND) 95 return 1; 96 return val; 97 } 98 99 /* This function assumes that [address|size]_cells is 1 or 2 */ 100 int fdt_appendprop_addrrange(void *fdt, int parent, int nodeoffset, 101 const char *name, uint64_t addr, uint64_t size) 102 { 103 int addr_cells, size_cells, ret; 104 uint8_t data[sizeof(fdt64_t) * 2], *prop; 105 106 ret = fdt_address_cells(fdt, parent); 107 if (ret < 0) 108 return ret; 109 addr_cells = ret; 110 111 ret = fdt_size_cells(fdt, parent); 112 if (ret < 0) 113 return ret; 114 size_cells = ret; 115 116 /* check validity of address */ 117 prop = data; 118 if (addr_cells == 1) { 119 if ((addr > UINT32_MAX) || ((UINT32_MAX + 1 - addr) < size)) 120 return -FDT_ERR_BADVALUE; 121 122 fdt32_st(prop, (uint32_t)addr); 123 } else if (addr_cells == 2) { 124 fdt64_st(prop, addr); 125 } else { 126 return -FDT_ERR_BADNCELLS; 127 } 128 129 /* check validity of size */ 130 prop += addr_cells * sizeof(fdt32_t); 131 if (size_cells == 1) { 132 if (size > UINT32_MAX) 133 return -FDT_ERR_BADVALUE; 134 135 fdt32_st(prop, (uint32_t)size); 136 } else if (size_cells == 2) { 137 fdt64_st(prop, size); 138 } else { 139 return -FDT_ERR_BADNCELLS; 140 } 141 142 return fdt_appendprop(fdt, nodeoffset, name, data, 143 (addr_cells + size_cells) * sizeof(fdt32_t)); 144 } 145