Home | History | Annotate | Download | only in tests
      1 /*
      2  * libfdt - Flat Device Tree manipulation
      3  *	Test labels within values
      4  * Copyright (C) 2008 David Gibson, IBM Corporation.
      5  *
      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.
     10  *
     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.
     15  *
     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
     19  */
     20 
     21 #include <stdlib.h>
     22 #include <stdio.h>
     23 #include <string.h>
     24 #include <stdint.h>
     25 #include <errno.h>
     26 
     27 #include <dlfcn.h>
     28 
     29 #include <libfdt.h>
     30 
     31 #include "tests.h"
     32 #include "testdata.h"
     33 
     34 struct val_label {
     35 	const char *labelname;
     36 	int propoff;
     37 };
     38 
     39 static struct val_label labels1[] = {
     40 	{ "start1", 0 },
     41 	{ "mid1", 2 },
     42 	{ "end1", -1 },
     43 };
     44 
     45 static struct val_label labels2[] = {
     46 	{ "start2", 0 },
     47 	{ "innerstart2", 0 },
     48 	{ "innermid2", 4 },
     49 	{ "innerend2", -1 },
     50 	{ "end2", -1 },
     51 };
     52 
     53 static struct val_label labels3[] = {
     54 	{ "start3", 0 },
     55 	{ "innerstart3", 0 },
     56 	{ "innermid3", 1 },
     57 	{ "innerend3", -1 },
     58 	{ "end3", -1 },
     59 };
     60 
     61 static void check_prop_labels(void *sohandle, void *fdt, const char *name,
     62 			      const struct val_label* labels, int n)
     63 {
     64 	const struct fdt_property *prop;
     65 	const char *p;
     66 	int len;
     67 	int i;
     68 
     69 	prop = fdt_get_property(fdt, 0, name, &len);
     70 	if (!prop)
     71 		FAIL("Couldn't locate property \"%s\"", name);
     72 
     73 	p = dlsym(sohandle, name);
     74 	if (!p)
     75 		FAIL("Couldn't locate label symbol \"%s\"", name);
     76 
     77 	if (p != (const char *)prop)
     78 		FAIL("Label \"%s\" does not point to correct property", name);
     79 
     80 	for (i = 0; i < n; i++) {
     81 		int off = labels[i].propoff;
     82 
     83 		if (off == -1)
     84 			off = len;
     85 
     86 		p = dlsym(sohandle, labels[i].labelname);
     87 		if (!p)
     88 			FAIL("Couldn't locate label symbol \"%s\"", name);
     89 
     90 		if ((p - prop->data) != off)
     91 			FAIL("Label \"%s\" points to offset %ld instead of %d"
     92 			     "in property \"%s\"", labels[i].labelname,
     93 			     (long)(p - prop->data), off, name);
     94 	}
     95 }
     96 
     97 int main(int argc, char *argv[])
     98 {
     99 	void *sohandle;
    100 	void *fdt;
    101 	int err;
    102 
    103 	test_init(argc, argv);
    104 	if (argc != 2)
    105 		CONFIG("Usage: %s <so file>", argv[0]);
    106 
    107 	sohandle = dlopen(argv[1], RTLD_NOW);
    108 	if (!sohandle)
    109 		FAIL("Couldn't dlopen() %s", argv[1]);
    110 
    111 	fdt = dlsym(sohandle, "dt_blob_start");
    112 	if (!fdt)
    113 		FAIL("Couldn't locate \"dt_blob_start\" symbol in %s",
    114 		     argv[1]);
    115 
    116 	err = fdt_check_header(fdt);
    117 	if (err != 0)
    118 		FAIL("%s contains invalid tree: %s", argv[1],
    119 		     fdt_strerror(err));
    120 
    121 
    122 	check_prop_labels(sohandle, fdt, "prop1", labels1, ARRAY_SIZE(labels1));
    123 	check_prop_labels(sohandle, fdt, "prop2", labels2, ARRAY_SIZE(labels2));
    124 	check_prop_labels(sohandle, fdt, "prop3", labels3, ARRAY_SIZE(labels3));
    125 
    126 	PASS();
    127 }
    128