blob: 7189f0142957b55c120aca37111122c534989649 [file] [log] [blame]
Masahiro Yamada938f0392018-01-21 19:19:13 +09001/* SPDX-License-Identifier: GPL-2.0+ BSD-2-Clause */
2#include "fdt_host.h"
3#include "../../scripts/dtc/libfdt/fdt_rw.c"
4
5int fdt_remove_unused_strings(const void *old, void *new)
6{
7 const struct fdt_property *old_prop;
8 struct fdt_property *new_prop;
9 int size = fdt_totalsize(old);
10 int next_offset, offset;
11 const char *str;
12 int ret;
13 int tag = FDT_PROP;
Simon Glassa5e1a582019-10-27 09:47:42 -060014 int allocated;
Masahiro Yamada938f0392018-01-21 19:19:13 +090015
16 /* Make a copy and remove the strings */
17 memcpy(new, old, size);
18 fdt_set_size_dt_strings(new, 0);
19
20 /* Add every property name back into the new string table */
21 for (offset = 0; tag != FDT_END; offset = next_offset) {
22 tag = fdt_next_tag(old, offset, &next_offset);
23 if (tag != FDT_PROP)
24 continue;
25 old_prop = fdt_get_property_by_offset(old, offset, NULL);
26 new_prop = (struct fdt_property *)(unsigned long)
27 fdt_get_property_by_offset(new, offset, NULL);
28 str = fdt_string(old, fdt32_to_cpu(old_prop->nameoff));
Simon Glassa5e1a582019-10-27 09:47:42 -060029 ret = fdt_find_add_string_(new, str, &allocated);
Masahiro Yamada938f0392018-01-21 19:19:13 +090030 if (ret < 0)
31 return ret;
32 new_prop->nameoff = cpu_to_fdt32(ret);
33 }
34
35 return 0;
36}