xref: /netbsd-src/external/gpl2/dtc/dist/tests/open_pack.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: open_pack.c,v 1.1.1.2 2017/06/08 15:59:27 skrll Exp $	*/
2 
3 /*
4  * libfdt - Flat Device Tree manipulation
5  *	Basic testcase for read-only access
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 int main(int argc, char *argv[])
35 {
36 	void *fdt, *fdt1;
37 	void *buf;
38 	int oldsize, bufsize, packsize;
39 	int err;
40 	const char *inname;
41 	char outname[PATH_MAX];
42 
43 	test_init(argc, argv);
44 	fdt = load_blob_arg(argc, argv);
45 	inname = argv[1];
46 
47 	oldsize = fdt_totalsize(fdt);
48 
49 	bufsize = oldsize * 2;
50 
51 	buf = xmalloc(bufsize);
52 	/* don't leak uninitialized memory into our output */
53 	memset(buf, 0, bufsize);
54 
55 	fdt1 = buf;
56 	err = fdt_open_into(fdt, fdt1, bufsize);
57 	if (err)
58 		FAIL("fdt_open_into(): %s", fdt_strerror(err));
59 	sprintf(outname, "opened.%s", inname);
60 	save_blob(outname, fdt1);
61 
62 	err = fdt_pack(fdt1);
63 	if (err)
64 		FAIL("fdt_pack(): %s", fdt_strerror(err));
65 	sprintf(outname, "repacked.%s", inname);
66 	save_blob(outname, fdt1);
67 
68 	packsize = fdt_totalsize(fdt1);
69 
70 	verbose_printf("oldsize = %d, bufsize = %d, packsize = %d\n",
71 		       oldsize, bufsize, packsize);
72 	PASS();
73 }
74