Soby Mathew | b9fccca | 2017-11-06 13:56:40 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018, ARM Limited and Contributors. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | /* Helper functions to offer easier navigation of Device Tree Blob */ |
| 8 | |
| 9 | #include <assert.h> |
Antonio Nino Diaz | d85c878 | 2018-06-26 10:34:07 +0100 | [diff] [blame] | 10 | #include <string.h> |
Soby Mathew | b9fccca | 2017-11-06 13:56:40 +0000 | [diff] [blame] | 11 | |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 12 | #include <libfdt.h> |
| 13 | |
| 14 | #include <common/debug.h> |
| 15 | #include <common/fdt_wrappers.h> |
| 16 | |
Soby Mathew | b9fccca | 2017-11-06 13:56:40 +0000 | [diff] [blame] | 17 | /* |
| 18 | * Read cells from a given property of the given node. At most 2 cells of the |
| 19 | * property are read, and pointer is updated. Returns 0 on success, and -1 upon |
| 20 | * error |
| 21 | */ |
| 22 | int fdtw_read_cells(const void *dtb, int node, const char *prop, |
| 23 | unsigned int cells, void *value) |
| 24 | { |
| 25 | const uint32_t *value_ptr; |
| 26 | uint32_t hi = 0, lo; |
| 27 | int value_len; |
| 28 | |
Soby Mathew | cc36484 | 2018-02-21 01:16:39 +0000 | [diff] [blame] | 29 | assert(dtb != NULL); |
| 30 | assert(prop != NULL); |
| 31 | assert(value != NULL); |
Soby Mathew | b9fccca | 2017-11-06 13:56:40 +0000 | [diff] [blame] | 32 | assert(node >= 0); |
| 33 | |
| 34 | /* We expect either 1 or 2 cell property */ |
Soby Mathew | cc36484 | 2018-02-21 01:16:39 +0000 | [diff] [blame] | 35 | assert(cells <= 2U); |
Soby Mathew | b9fccca | 2017-11-06 13:56:40 +0000 | [diff] [blame] | 36 | |
| 37 | /* Access property and obtain its length (in bytes) */ |
Soby Mathew | cc36484 | 2018-02-21 01:16:39 +0000 | [diff] [blame] | 38 | value_ptr = fdt_getprop_namelen(dtb, node, prop, (int)strlen(prop), |
Soby Mathew | b9fccca | 2017-11-06 13:56:40 +0000 | [diff] [blame] | 39 | &value_len); |
| 40 | if (value_ptr == NULL) { |
| 41 | WARN("Couldn't find property %s in dtb\n", prop); |
| 42 | return -1; |
| 43 | } |
| 44 | |
Soby Mathew | b9fccca | 2017-11-06 13:56:40 +0000 | [diff] [blame] | 45 | /* Verify that property length accords with cell length */ |
Soby Mathew | cc36484 | 2018-02-21 01:16:39 +0000 | [diff] [blame] | 46 | if (NCELLS((unsigned int)value_len) != cells) { |
Soby Mathew | b9fccca | 2017-11-06 13:56:40 +0000 | [diff] [blame] | 47 | WARN("Property length mismatch\n"); |
| 48 | return -1; |
| 49 | } |
| 50 | |
Soby Mathew | cc36484 | 2018-02-21 01:16:39 +0000 | [diff] [blame] | 51 | if (cells == 2U) { |
Soby Mathew | b9fccca | 2017-11-06 13:56:40 +0000 | [diff] [blame] | 52 | hi = fdt32_to_cpu(*value_ptr); |
| 53 | value_ptr++; |
| 54 | } |
| 55 | |
| 56 | lo = fdt32_to_cpu(*value_ptr); |
| 57 | |
Soby Mathew | cc36484 | 2018-02-21 01:16:39 +0000 | [diff] [blame] | 58 | if (cells == 2U) |
Soby Mathew | b9fccca | 2017-11-06 13:56:40 +0000 | [diff] [blame] | 59 | *((uint64_t *) value) = ((uint64_t) hi << 32) | lo; |
| 60 | else |
| 61 | *((uint32_t *) value) = lo; |
| 62 | |
| 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | /* |
Antonio Nino Diaz | 3fddfbb | 2018-06-26 10:34:10 +0100 | [diff] [blame] | 67 | * Read cells from a given property of the given node. Any number of 32-bit |
| 68 | * cells of the property can be read. The fdt pointer is updated. Returns 0 on |
| 69 | * success, and -1 on error. |
| 70 | */ |
| 71 | int fdtw_read_array(const void *dtb, int node, const char *prop, |
| 72 | unsigned int cells, void *value) |
| 73 | { |
| 74 | const uint32_t *value_ptr; |
| 75 | int value_len; |
| 76 | |
| 77 | assert(dtb != NULL); |
| 78 | assert(prop != NULL); |
| 79 | assert(value != NULL); |
| 80 | assert(node >= 0); |
| 81 | |
| 82 | /* Access property and obtain its length (in bytes) */ |
| 83 | value_ptr = fdt_getprop_namelen(dtb, node, prop, (int)strlen(prop), |
| 84 | &value_len); |
| 85 | if (value_ptr == NULL) { |
| 86 | WARN("Couldn't find property %s in dtb\n", prop); |
| 87 | return -1; |
| 88 | } |
| 89 | |
| 90 | /* Verify that property length accords with cell length */ |
| 91 | if (NCELLS((unsigned int)value_len) != cells) { |
| 92 | WARN("Property length mismatch\n"); |
| 93 | return -1; |
| 94 | } |
| 95 | |
| 96 | uint32_t *dst = value; |
| 97 | |
| 98 | for (unsigned int i = 0U; i < cells; i++) { |
| 99 | dst[i] = fdt32_to_cpu(value_ptr[i]); |
| 100 | } |
| 101 | |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | /* |
Antonio Nino Diaz | d85c878 | 2018-06-26 10:34:07 +0100 | [diff] [blame] | 106 | * Read string from a given property of the given node. Up to 'size - 1' |
| 107 | * characters are read, and a NUL terminator is added. Returns 0 on success, |
| 108 | * and -1 upon error. |
| 109 | */ |
| 110 | int fdtw_read_string(const void *dtb, int node, const char *prop, |
| 111 | char *str, size_t size) |
| 112 | { |
| 113 | const char *ptr; |
| 114 | size_t len; |
| 115 | |
| 116 | assert(dtb != NULL); |
| 117 | assert(node >= 0); |
| 118 | assert(prop != NULL); |
| 119 | assert(str != NULL); |
| 120 | assert(size > 0U); |
| 121 | |
| 122 | ptr = fdt_getprop_namelen(dtb, node, prop, (int)strlen(prop), NULL); |
| 123 | if (ptr == NULL) { |
| 124 | WARN("Couldn't find property %s in dtb\n", prop); |
| 125 | return -1; |
| 126 | } |
| 127 | |
| 128 | len = strlcpy(str, ptr, size); |
| 129 | if (len >= size) { |
| 130 | WARN("String of property %s in dtb has been truncated\n", prop); |
| 131 | return -1; |
| 132 | } |
| 133 | |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | /* |
Soby Mathew | b9fccca | 2017-11-06 13:56:40 +0000 | [diff] [blame] | 138 | * Write cells in place to a given property of the given node. At most 2 cells |
| 139 | * of the property are written. Returns 0 on success, and -1 upon error. |
| 140 | */ |
| 141 | int fdtw_write_inplace_cells(void *dtb, int node, const char *prop, |
| 142 | unsigned int cells, void *value) |
| 143 | { |
| 144 | int err, len; |
| 145 | |
Soby Mathew | cc36484 | 2018-02-21 01:16:39 +0000 | [diff] [blame] | 146 | assert(dtb != NULL); |
| 147 | assert(prop != NULL); |
| 148 | assert(value != NULL); |
Soby Mathew | b9fccca | 2017-11-06 13:56:40 +0000 | [diff] [blame] | 149 | assert(node >= 0); |
| 150 | |
| 151 | /* We expect either 1 or 2 cell property */ |
Soby Mathew | cc36484 | 2018-02-21 01:16:39 +0000 | [diff] [blame] | 152 | assert(cells <= 2U); |
Soby Mathew | b9fccca | 2017-11-06 13:56:40 +0000 | [diff] [blame] | 153 | |
Soby Mathew | cc36484 | 2018-02-21 01:16:39 +0000 | [diff] [blame] | 154 | if (cells == 2U) |
Soby Mathew | b9fccca | 2017-11-06 13:56:40 +0000 | [diff] [blame] | 155 | *(uint64_t *)value = cpu_to_fdt64(*(uint64_t *)value); |
| 156 | else |
| 157 | *(uint32_t *)value = cpu_to_fdt32(*(uint32_t *)value); |
| 158 | |
Soby Mathew | cc36484 | 2018-02-21 01:16:39 +0000 | [diff] [blame] | 159 | len = (int)cells * 4; |
Soby Mathew | b9fccca | 2017-11-06 13:56:40 +0000 | [diff] [blame] | 160 | |
| 161 | /* Set property value in place */ |
| 162 | err = fdt_setprop_inplace(dtb, node, prop, value, len); |
| 163 | if (err != 0) { |
| 164 | WARN("Modify property %s failed with error %d\n", prop, err); |
| 165 | return -1; |
| 166 | } |
| 167 | |
| 168 | return 0; |
| 169 | } |