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
38 static void expand_buf(struct bufstate *buf, int newsize)
40 buf->buf = realloc(buf->buf, newsize);
42 CONFIG("Allocation failure");
46 static void new_header(struct bufstate *buf, int version, const void *fdt)
51 hdrsize = FDT_V16_SIZE;
52 else if (version == 17)
53 hdrsize = FDT_V17_SIZE;
55 CONFIG("Bad version %d", version);
57 expand_buf(buf, hdrsize);
58 memset(buf->buf, 0, hdrsize);
60 fdt_set_magic(buf->buf, FDT_MAGIC);
61 fdt_set_version(buf->buf, version);
62 fdt_set_last_comp_version(buf->buf, 16);
63 fdt_set_boot_cpuid_phys(buf->buf, fdt_boot_cpuid_phys(fdt));
66 static void add_block(struct bufstate *buf, int version, char block, const void *fdt)
74 /* Memory reserve map */
76 src = (const char *)fdt + fdt_off_mem_rsvmap(fdt);
77 size = (fdt_num_mem_rsv(fdt) + 1)
78 * sizeof(struct fdt_reserve_entry);
84 src = (const char *)fdt + fdt_off_dt_struct(fdt);
85 size = fdt_size_dt_struct(fdt);
91 src = (const char *)fdt + fdt_off_dt_strings(fdt);
92 size = fdt_size_dt_strings(fdt);
95 CONFIG("Bad block '%c'", block);
98 offset = ALIGN(buf->size, align);
100 expand_buf(buf, offset+size);
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);