xref: /aosp_15_r20/external/dtc/tests/nopulate.c (revision cd60bc56d4bea3af4ec04523e4d71c2b272c8aff)
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /*
3  * libfdt - Flat Device Tree manipulation
4  *	Testcase/tool for rearranging blocks of a dtb
5  * Copyright (C) 2006 David Gibson, IBM Corporation.
6  */
7 
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <string.h>
11 #include <limits.h>
12 #include <stdint.h>
13 
14 #include <libfdt.h>
15 
16 #include "tests.h"
17 #include "testdata.h"
18 
nopulate_struct(char * buf,const char * fdt)19 static int nopulate_struct(char *buf, const char *fdt)
20 {
21 	int offset, nextoffset = 0;
22 	uint32_t tag;
23 	char *p;
24 
25 	p = buf;
26 
27 	do {
28 		offset = nextoffset;
29 		tag = fdt_next_tag(fdt, offset, &nextoffset);
30 
31 		memcpy(p, (const char *)fdt + fdt_off_dt_struct(fdt) + offset,
32 		       nextoffset - offset);
33 		p += nextoffset - offset;
34 
35 		*((fdt32_t *)p) = cpu_to_fdt32(FDT_NOP);
36 		p += FDT_TAGSIZE;
37 
38 	} while (tag != FDT_END);
39 
40 	return p - buf;
41 }
42 
main(int argc,char * argv[])43 int main(int argc, char *argv[])
44 {
45 	char *fdt, *fdt2, *buf;
46 	int newsize, struct_end_old, struct_end_new, delta;
47 	unsigned int struct_start;
48 	const char *inname;
49 	char outname[PATH_MAX];
50 
51 	test_init(argc, argv);
52 	if (argc != 2)
53 		CONFIG("Usage: %s <dtb file>", argv[0]);
54 
55 	inname = argv[1];
56 	fdt = load_blob(argv[1]);
57 	sprintf(outname, "noppy.%s", inname);
58 
59 	if (fdt_version(fdt) < 17)
60 		FAIL("Can't deal with version <17");
61 
62 	buf = xmalloc(2 * fdt_size_dt_struct(fdt));
63 
64 	newsize = nopulate_struct(buf, fdt);
65 
66 	verbose_printf("Nopulated structure block has new size %d\n", newsize);
67 
68 	/* Replace old strcutre block with the new */
69 
70 	fdt2 = xmalloc(fdt_totalsize(fdt) + newsize);
71 
72 	struct_start = fdt_off_dt_struct(fdt);
73 	delta = newsize - fdt_size_dt_struct(fdt);
74 	struct_end_old = struct_start + fdt_size_dt_struct(fdt);
75 	struct_end_new = struct_start + newsize;
76 
77 	memcpy(fdt2, fdt, struct_start);
78 	memcpy(fdt2 + struct_start, buf, newsize);
79 	memcpy(fdt2 + struct_end_new, fdt + struct_end_old,
80 	       fdt_totalsize(fdt) - struct_end_old);
81 
82 	fdt_set_totalsize(fdt2, fdt_totalsize(fdt) + delta);
83 	fdt_set_size_dt_struct(fdt2, newsize);
84 
85 	if (fdt_off_mem_rsvmap(fdt) > struct_start)
86 		fdt_set_off_mem_rsvmap(fdt2, fdt_off_mem_rsvmap(fdt) + delta);
87 	if (fdt_off_dt_strings(fdt) > struct_start)
88 		fdt_set_off_dt_strings(fdt2, fdt_off_dt_strings(fdt) + delta);
89 
90 	save_blob(outname, fdt2);
91 
92 	PASS();
93 }
94