xref: /aosp_15_r20/external/dtc/tests/move_and_save.c (revision cd60bc56d4bea3af4ec04523e4d71c2b272c8aff)
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /*
3  * libfdt - Flat Device Tree manipulation
4  *	Basic testcase for read-only access
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 
main(int argc,char * argv[])19 int main(int argc, char *argv[])
20 {
21 	void *fdt, *fdt1, *fdt2, *fdt3;
22 	char *buf;
23 	int shuntsize;
24 	int bufsize;
25 	int err;
26 	const char *inname;
27 	char outname[PATH_MAX];
28 
29 	test_init(argc, argv);
30 	fdt = load_blob_arg(argc, argv);
31 	inname = argv[1];
32 
33 	shuntsize = ALIGN(fdt_totalsize(fdt) / 2, sizeof(uint64_t));
34 	bufsize = fdt_totalsize(fdt) + shuntsize;
35 	buf = xmalloc(bufsize);
36 
37 	fdt1 = buf;
38 	err = fdt_move(fdt, fdt1, bufsize);
39 	if (err)
40 		FAIL("Failed to move tree into new buffer: %s",
41 		     fdt_strerror(err));
42 	sprintf(outname, "moved.%s", inname);
43 	save_blob(outname, fdt1);
44 
45 	fdt2 = buf + shuntsize;
46 	err = fdt_move(fdt1, fdt2, bufsize-shuntsize);
47 	if (err)
48 		FAIL("Failed to shunt tree %d bytes: %s",
49 		     shuntsize, fdt_strerror(err));
50 	sprintf(outname, "shunted.%s", inname);
51 	save_blob(outname, fdt2);
52 
53 	fdt3 = buf;
54 	err = fdt_move(fdt2, fdt3, bufsize);
55 	if (err)
56 		FAIL("Failed to deshunt tree %d bytes: %s",
57 		     shuntsize, fdt_strerror(err));
58 	sprintf(outname, "deshunted.%s", inname);
59 	save_blob(outname, fdt3);
60 
61 	PASS();
62 }
63