Gerald Van Baren | 3574817 | 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 | 3508476 | 2013-07-27 01:12:38 +0200 | [diff] [blame] | 4 | * SPDX-License-Identifier: GPL-2.0+ BSD-2-Clause |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 5 | */ |
| 6 | #include "libfdt_env.h" |
| 7 | |
| 8 | #include <fdt.h> |
| 9 | #include <libfdt.h> |
| 10 | |
| 11 | #include "libfdt_internal.h" |
| 12 | |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 13 | static int _fdt_sw_check_header(void *fdt) |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 14 | { |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 15 | if (fdt_magic(fdt) != FDT_SW_MAGIC) |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 16 | return -FDT_ERR_BADMAGIC; |
David Gibson | 2f08bfa | 2008-05-20 17:19:11 +1000 | [diff] [blame] | 17 | /* FIXME: should check more details about the header state */ |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 18 | return 0; |
| 19 | } |
| 20 | |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 21 | #define FDT_SW_CHECK_HEADER(fdt) \ |
David Gibson | 2f08bfa | 2008-05-20 17:19:11 +1000 | [diff] [blame] | 22 | { \ |
| 23 | int err; \ |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 24 | if ((err = _fdt_sw_check_header(fdt)) != 0) \ |
David Gibson | 2f08bfa | 2008-05-20 17:19:11 +1000 | [diff] [blame] | 25 | return err; \ |
| 26 | } |
| 27 | |
Emil Medve | 13d93f3 | 2009-02-23 10:43:36 -0600 | [diff] [blame] | 28 | static void *_fdt_grab_space(void *fdt, size_t len) |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 29 | { |
| 30 | int offset = fdt_size_dt_struct(fdt); |
| 31 | int spaceleft; |
| 32 | |
| 33 | spaceleft = fdt_totalsize(fdt) - fdt_off_dt_struct(fdt) |
| 34 | - fdt_size_dt_strings(fdt); |
| 35 | |
| 36 | if ((offset + len < offset) || (offset + len > spaceleft)) |
| 37 | return NULL; |
| 38 | |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 39 | fdt_set_size_dt_struct(fdt, offset + len); |
David Gibson | a22d9cf | 2009-02-06 14:03:24 +1100 | [diff] [blame] | 40 | return _fdt_offset_ptr_w(fdt, offset); |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | int fdt_create(void *buf, int bufsize) |
| 44 | { |
| 45 | void *fdt = buf; |
| 46 | |
| 47 | if (bufsize < sizeof(struct fdt_header)) |
| 48 | return -FDT_ERR_NOSPACE; |
| 49 | |
| 50 | memset(buf, 0, bufsize); |
| 51 | |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 52 | fdt_set_magic(fdt, FDT_SW_MAGIC); |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 53 | fdt_set_version(fdt, FDT_LAST_SUPPORTED_VERSION); |
| 54 | fdt_set_last_comp_version(fdt, FDT_FIRST_SUPPORTED_VERSION); |
| 55 | fdt_set_totalsize(fdt, bufsize); |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 56 | |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 57 | fdt_set_off_mem_rsvmap(fdt, FDT_ALIGN(sizeof(struct fdt_header), |
| 58 | sizeof(struct fdt_reserve_entry))); |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 59 | fdt_set_off_dt_struct(fdt, fdt_off_mem_rsvmap(fdt)); |
| 60 | fdt_set_off_dt_strings(fdt, bufsize); |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 61 | |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | int fdt_add_reservemap_entry(void *fdt, uint64_t addr, uint64_t size) |
| 66 | { |
| 67 | struct fdt_reserve_entry *re; |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 68 | int offset; |
| 69 | |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 70 | FDT_SW_CHECK_HEADER(fdt); |
David Gibson | 2f08bfa | 2008-05-20 17:19:11 +1000 | [diff] [blame] | 71 | |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 72 | if (fdt_size_dt_struct(fdt)) |
| 73 | return -FDT_ERR_BADSTATE; |
| 74 | |
| 75 | offset = fdt_off_dt_struct(fdt); |
| 76 | if ((offset + sizeof(*re)) > fdt_totalsize(fdt)) |
| 77 | return -FDT_ERR_NOSPACE; |
| 78 | |
David Gibson | ef4e8ce | 2008-07-07 10:10:48 +1000 | [diff] [blame] | 79 | re = (struct fdt_reserve_entry *)((char *)fdt + offset); |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 80 | re->address = cpu_to_fdt64(addr); |
| 81 | re->size = cpu_to_fdt64(size); |
| 82 | |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 83 | fdt_set_off_dt_struct(fdt, offset + sizeof(*re)); |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 84 | |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | int fdt_finish_reservemap(void *fdt) |
| 89 | { |
| 90 | return fdt_add_reservemap_entry(fdt, 0, 0); |
| 91 | } |
| 92 | |
| 93 | int fdt_begin_node(void *fdt, const char *name) |
| 94 | { |
| 95 | struct fdt_node_header *nh; |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 96 | int namelen = strlen(name) + 1; |
| 97 | |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 98 | FDT_SW_CHECK_HEADER(fdt); |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 99 | |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 100 | nh = _fdt_grab_space(fdt, sizeof(*nh) + FDT_TAGALIGN(namelen)); |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 101 | if (! nh) |
| 102 | return -FDT_ERR_NOSPACE; |
| 103 | |
| 104 | nh->tag = cpu_to_fdt32(FDT_BEGIN_NODE); |
| 105 | memcpy(nh->name, name, namelen); |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | int fdt_end_node(void *fdt) |
| 110 | { |
Kim Phillips | b2ba62a | 2013-01-16 13:59:50 +0000 | [diff] [blame] | 111 | fdt32_t *en; |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 112 | |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 113 | FDT_SW_CHECK_HEADER(fdt); |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 114 | |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 115 | en = _fdt_grab_space(fdt, FDT_TAGSIZE); |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 116 | if (! en) |
| 117 | return -FDT_ERR_NOSPACE; |
| 118 | |
| 119 | *en = cpu_to_fdt32(FDT_END_NODE); |
| 120 | return 0; |
| 121 | } |
| 122 | |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 123 | static int _fdt_find_add_string(void *fdt, const char *s) |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 124 | { |
| 125 | char *strtab = (char *)fdt + fdt_totalsize(fdt); |
| 126 | const char *p; |
| 127 | int strtabsize = fdt_size_dt_strings(fdt); |
| 128 | int len = strlen(s) + 1; |
| 129 | int struct_top, offset; |
| 130 | |
| 131 | p = _fdt_find_string(strtab - strtabsize, strtabsize, s); |
| 132 | if (p) |
| 133 | return p - strtab; |
| 134 | |
| 135 | /* Add it */ |
| 136 | offset = -strtabsize - len; |
| 137 | struct_top = fdt_off_dt_struct(fdt) + fdt_size_dt_struct(fdt); |
| 138 | if (fdt_totalsize(fdt) + offset < struct_top) |
| 139 | return 0; /* no more room :( */ |
| 140 | |
| 141 | memcpy(strtab + offset, s, len); |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 142 | fdt_set_size_dt_strings(fdt, strtabsize + len); |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 143 | return offset; |
| 144 | } |
| 145 | |
| 146 | int fdt_property(void *fdt, const char *name, const void *val, int len) |
| 147 | { |
| 148 | struct fdt_property *prop; |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 149 | int nameoff; |
| 150 | |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 151 | FDT_SW_CHECK_HEADER(fdt); |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 152 | |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 153 | nameoff = _fdt_find_add_string(fdt, name); |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 154 | if (nameoff == 0) |
| 155 | return -FDT_ERR_NOSPACE; |
| 156 | |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 157 | prop = _fdt_grab_space(fdt, sizeof(*prop) + FDT_TAGALIGN(len)); |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 158 | if (! prop) |
| 159 | return -FDT_ERR_NOSPACE; |
| 160 | |
| 161 | prop->tag = cpu_to_fdt32(FDT_PROP); |
| 162 | prop->nameoff = cpu_to_fdt32(nameoff); |
| 163 | prop->len = cpu_to_fdt32(len); |
| 164 | memcpy(prop->data, val, len); |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | int fdt_finish(void *fdt) |
| 169 | { |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 170 | char *p = (char *)fdt; |
Kim Phillips | b2ba62a | 2013-01-16 13:59:50 +0000 | [diff] [blame] | 171 | fdt32_t *end; |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 172 | int oldstroffset, newstroffset; |
| 173 | uint32_t tag; |
| 174 | int offset, nextoffset; |
| 175 | |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 176 | FDT_SW_CHECK_HEADER(fdt); |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 177 | |
| 178 | /* Add terminator */ |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 179 | end = _fdt_grab_space(fdt, sizeof(*end)); |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 180 | if (! end) |
| 181 | return -FDT_ERR_NOSPACE; |
| 182 | *end = cpu_to_fdt32(FDT_END); |
| 183 | |
| 184 | /* Relocate the string table */ |
| 185 | oldstroffset = fdt_totalsize(fdt) - fdt_size_dt_strings(fdt); |
| 186 | newstroffset = fdt_off_dt_struct(fdt) + fdt_size_dt_struct(fdt); |
| 187 | memmove(p + newstroffset, p + oldstroffset, fdt_size_dt_strings(fdt)); |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 188 | fdt_set_off_dt_strings(fdt, newstroffset); |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 189 | |
| 190 | /* Walk the structure, correcting string offsets */ |
| 191 | offset = 0; |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 192 | while ((tag = fdt_next_tag(fdt, offset, &nextoffset)) != FDT_END) { |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 193 | if (tag == FDT_PROP) { |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 194 | struct fdt_property *prop = |
David Gibson | a22d9cf | 2009-02-06 14:03:24 +1100 | [diff] [blame] | 195 | _fdt_offset_ptr_w(fdt, offset); |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 196 | int nameoff; |
| 197 | |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 198 | nameoff = fdt32_to_cpu(prop->nameoff); |
| 199 | nameoff += fdt_size_dt_strings(fdt); |
| 200 | prop->nameoff = cpu_to_fdt32(nameoff); |
| 201 | } |
| 202 | offset = nextoffset; |
| 203 | } |
David Gibson | a22d9cf | 2009-02-06 14:03:24 +1100 | [diff] [blame] | 204 | if (nextoffset < 0) |
| 205 | return nextoffset; |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 206 | |
| 207 | /* Finally, adjust the header */ |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 208 | fdt_set_totalsize(fdt, newstroffset + fdt_size_dt_strings(fdt)); |
| 209 | fdt_set_magic(fdt, FDT_MAGIC); |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 210 | return 0; |
| 211 | } |