1 /* $NetBSD: nopulate.c,v 1.1.1.2 2017/06/08 15:59:27 skrll Exp $ */ 2 3 /* 4 * libfdt - Flat Device Tree manipulation 5 * Testcase/tool for rearranging blocks of a dtb 6 * Copyright (C) 2006 David Gibson, IBM Corporation. 7 * 8 * This library is free software; you can redistribute it and/or 9 * modify it under the terms of the GNU Lesser General Public License 10 * as published by the Free Software Foundation; either version 2.1 of 11 * the License, or (at your option) any later version. 12 * 13 * This library is distributed in the hope that it will be useful, but 14 * WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 * Lesser General Public License for more details. 17 * 18 * You should have received a copy of the GNU Lesser General Public 19 * License along with this library; if not, write to the Free Software 20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 21 */ 22 23 #include <stdlib.h> 24 #include <stdio.h> 25 #include <string.h> 26 #include <limits.h> 27 #include <stdint.h> 28 29 #include <libfdt.h> 30 31 #include "tests.h" 32 #include "testdata.h" 33 34 static int nopulate_struct(char *buf, const char *fdt) 35 { 36 int offset, nextoffset = 0; 37 uint32_t tag; 38 char *p; 39 40 p = buf; 41 42 do { 43 offset = nextoffset; 44 tag = fdt_next_tag(fdt, offset, &nextoffset); 45 46 memcpy(p, (const char *)fdt + fdt_off_dt_struct(fdt) + offset, 47 nextoffset - offset); 48 p += nextoffset - offset; 49 50 *((fdt32_t *)p) = cpu_to_fdt32(FDT_NOP); 51 p += FDT_TAGSIZE; 52 53 } while (tag != FDT_END); 54 55 return p - buf; 56 } 57 58 int main(int argc, char *argv[]) 59 { 60 char *fdt, *fdt2, *buf; 61 int newsize, struct_start, struct_end_old, struct_end_new, delta; 62 const char *inname; 63 char outname[PATH_MAX]; 64 65 test_init(argc, argv); 66 if (argc != 2) 67 CONFIG("Usage: %s <dtb file>", argv[0]); 68 69 inname = argv[1]; 70 fdt = load_blob(argv[1]); 71 sprintf(outname, "noppy.%s", inname); 72 73 if (fdt_version(fdt) < 17) 74 FAIL("Can't deal with version <17"); 75 76 buf = xmalloc(2 * fdt_size_dt_struct(fdt)); 77 78 newsize = nopulate_struct(buf, fdt); 79 80 verbose_printf("Nopulated structure block has new size %d\n", newsize); 81 82 /* Replace old strcutre block with the new */ 83 84 fdt2 = xmalloc(fdt_totalsize(fdt) + newsize); 85 86 struct_start = fdt_off_dt_struct(fdt); 87 delta = newsize - fdt_size_dt_struct(fdt); 88 struct_end_old = struct_start + fdt_size_dt_struct(fdt); 89 struct_end_new = struct_start + newsize; 90 91 memcpy(fdt2, fdt, struct_start); 92 memcpy(fdt2 + struct_start, buf, newsize); 93 memcpy(fdt2 + struct_end_new, fdt + struct_end_old, 94 fdt_totalsize(fdt) - struct_end_old); 95 96 fdt_set_totalsize(fdt2, fdt_totalsize(fdt) + delta); 97 fdt_set_size_dt_struct(fdt2, newsize); 98 99 if (fdt_off_mem_rsvmap(fdt) > struct_start) 100 fdt_set_off_mem_rsvmap(fdt2, fdt_off_mem_rsvmap(fdt) + delta); 101 if (fdt_off_dt_strings(fdt) > struct_start) 102 fdt_set_off_dt_strings(fdt2, fdt_off_dt_strings(fdt) + delta); 103 104 save_blob(outname, fdt2); 105 106 PASS(); 107 } 108