2 * libfdt - Flat Device Tree manipulation
3 * Testcase/tool for rearranging blocks of a dtb
4 * Copyright (C) 2006 David Gibson, IBM Corporation.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public License
8 * as published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
37 static void expand_buf(struct bufstate *buf, int newsize)
39 buf->buf = realloc(buf->buf, newsize);
41 CONFIG("Allocation failure");
45 static void new_header(struct bufstate *buf, int version, const void *fdt)
50 hdrsize = FDT_V16_SIZE;
51 else if (version == 17)
52 hdrsize = FDT_V17_SIZE;
54 CONFIG("Bad version %d", version);
56 expand_buf(buf, hdrsize);
57 memset(buf->buf, 0, hdrsize);
59 fdt_set_magic(buf->buf, FDT_MAGIC);
60 fdt_set_version(buf->buf, version);
61 fdt_set_last_comp_version(buf->buf, 16);
62 fdt_set_boot_cpuid_phys(buf->buf, fdt_boot_cpuid_phys(fdt));
65 static void add_block(struct bufstate *buf, int version, char block, const void *fdt)
67 int align, size, oldsize;
73 /* Memory reserve map */
75 src = (const char *)fdt + fdt_off_mem_rsvmap(fdt);
76 size = (fdt_num_mem_rsv(fdt) + 1)
77 * sizeof(struct fdt_reserve_entry);
83 src = (const char *)fdt + fdt_off_dt_struct(fdt);
84 size = fdt_size_dt_struct(fdt);
90 src = (const char *)fdt + fdt_off_dt_strings(fdt);
91 size = fdt_size_dt_strings(fdt);
94 CONFIG("Bad block '%c'", block);
98 offset = ALIGN(oldsize, align);
99 expand_buf(buf, offset+size);
100 memset(buf->buf + oldsize, 0, offset - oldsize);
102 memcpy(buf->buf + offset, src, size);
106 fdt_set_off_mem_rsvmap(buf->buf, offset);
110 fdt_set_off_dt_struct(buf->buf, offset);
112 fdt_set_size_dt_struct(buf->buf, size);
116 fdt_set_off_dt_strings(buf->buf, offset);
117 fdt_set_size_dt_strings(buf->buf, size);
122 int main(int argc, char *argv[])
126 const char *blockorder;
127 struct bufstate buf = {NULL, 0};
130 char outname[PATH_MAX];
132 test_init(argc, argv);
134 CONFIG("Usage: %s <dtb file> <version> <block order>", argv[0]);
137 fdt = load_blob(argv[1]);
138 version = atoi(argv[2]);
139 blockorder = argv[3];
140 sprintf(outname, "v%d.%s.%s", version, blockorder, inname);
142 if ((version != 16) && (version != 17))
143 CONFIG("Version must be 16 or 17");
145 if (fdt_version(fdt) < 17)
146 CONFIG("Input tree must be v17");
148 new_header(&buf, version, fdt);
150 while (*blockorder) {
151 add_block(&buf, version, *blockorder, fdt);
155 fdt_set_totalsize(buf.buf, buf.size);
157 err = fdt_check_header(buf.buf);
159 FAIL("Output tree fails check: %s", fdt_strerror(err));
161 save_blob(outname, buf.buf);