Blame tests/references.c

Packit Service 0ee8e1
// SPDX-License-Identifier: LGPL-2.1-or-later
Packit 2ad57b
/*
Packit 2ad57b
 * libfdt - Flat Device Tree manipulation
Packit 2ad57b
 *	Testcase for phandle references in dtc
Packit 2ad57b
 * Copyright (C) 2006 David Gibson, IBM Corporation.
Packit 2ad57b
 */
Packit 2ad57b
#include <stdlib.h>
Packit 2ad57b
#include <stdio.h>
Packit 2ad57b
#include <string.h>
Packit 2ad57b
#include <stdint.h>
Packit 2ad57b
Packit 2ad57b
#include <libfdt.h>
Packit 2ad57b
Packit 2ad57b
#include "tests.h"
Packit 2ad57b
#include "testdata.h"
Packit 2ad57b
Packit 2ad57b
static void check_ref(const void *fdt, int node, uint32_t checkref)
Packit 2ad57b
{
Packit 2ad57b
	const fdt32_t *p;
Packit 2ad57b
	uint32_t ref;
Packit 2ad57b
	int len;
Packit 2ad57b
Packit 2ad57b
	p = fdt_getprop(fdt, node, "ref", &len;;
Packit 2ad57b
	if (!p)
Packit 2ad57b
		FAIL("fdt_getprop(%d, \"ref\"): %s", node, fdt_strerror(len));
Packit 2ad57b
	if (len != sizeof(*p))
Packit 2ad57b
		FAIL("'ref' in node at %d has wrong size (%d instead of %zd)",
Packit 2ad57b
		     node, len, sizeof(*p));
Packit 2ad57b
	ref = fdt32_to_cpu(*p);
Packit 2ad57b
	if (ref != checkref)
Packit 2ad57b
		FAIL("'ref' in node at %d has value 0x%x instead of 0x%x",
Packit 2ad57b
		     node, ref, checkref);
Packit 2ad57b
Packit 2ad57b
	p = fdt_getprop(fdt, node, "lref", &len;;
Packit 2ad57b
	if (!p)
Packit 2ad57b
		FAIL("fdt_getprop(%d, \"lref\"): %s", node, fdt_strerror(len));
Packit 2ad57b
	if (len != sizeof(*p))
Packit 2ad57b
		FAIL("'lref' in node at %d has wrong size (%d instead of %zd)",
Packit 2ad57b
		     node, len, sizeof(*p));
Packit 2ad57b
	ref = fdt32_to_cpu(*p);
Packit 2ad57b
	if (ref != checkref)
Packit 2ad57b
		FAIL("'lref' in node at %d has value 0x%x instead of 0x%x",
Packit 2ad57b
		     node, ref, checkref);
Packit 2ad57b
}
Packit 2ad57b
Packit 2ad57b
static void check_rref(const void *fdt)
Packit 2ad57b
{
Packit 2ad57b
	const fdt32_t *p;
Packit 2ad57b
	uint32_t ref;
Packit 2ad57b
	int len;
Packit 2ad57b
Packit 2ad57b
	p = fdt_getprop(fdt, 0, "rref", &len;;
Packit 2ad57b
	if (!p)
Packit 2ad57b
		FAIL("fdt_getprop(0, \"rref\"): %s", fdt_strerror(len));
Packit 2ad57b
	if (len != sizeof(*p))
Packit 2ad57b
		FAIL("'rref' in root node has wrong size (%d instead of %zd)",
Packit 2ad57b
		     len, sizeof(*p));
Packit 2ad57b
	ref = fdt32_to_cpu(*p);
Packit 2ad57b
	if (ref != fdt_get_phandle(fdt, 0))
Packit 2ad57b
		FAIL("'rref' in root node has value 0x%x instead of 0x0", ref);
Packit 2ad57b
}
Packit 2ad57b
Packit 2ad57b
int main(int argc, char *argv[])
Packit 2ad57b
{
Packit 2ad57b
	void *fdt;
Packit Service 0ee8e1
	int n1, n2, n3, n4, n5, n6, err;
Packit Service 0ee8e1
	uint32_t h1, h2, h4, h5, h6, hn;
Packit 2ad57b
Packit 2ad57b
	test_init(argc, argv);
Packit 2ad57b
	fdt = load_blob_arg(argc, argv);
Packit 2ad57b
Packit 2ad57b
	n1 = fdt_path_offset(fdt, "/node1");
Packit 2ad57b
	if (n1 < 0)
Packit 2ad57b
		FAIL("fdt_path_offset(/node1): %s", fdt_strerror(n1));
Packit 2ad57b
	n2 = fdt_path_offset(fdt, "/node2");
Packit 2ad57b
	if (n2 < 0)
Packit 2ad57b
		FAIL("fdt_path_offset(/node2): %s", fdt_strerror(n2));
Packit 2ad57b
	n3 = fdt_path_offset(fdt, "/node3");
Packit 2ad57b
	if (n3 < 0)
Packit 2ad57b
		FAIL("fdt_path_offset(/node3): %s", fdt_strerror(n3));
Packit 2ad57b
	n4 = fdt_path_offset(fdt, "/node4");
Packit 2ad57b
	if (n4 < 0)
Packit 2ad57b
		FAIL("fdt_path_offset(/node4): %s", fdt_strerror(n4));
Packit 2ad57b
	n5 = fdt_path_offset(fdt, "/node5");
Packit 2ad57b
	if (n5 < 0)
Packit 2ad57b
		FAIL("fdt_path_offset(/node5): %s", fdt_strerror(n5));
Packit Service 0ee8e1
	n6 = fdt_path_offset(fdt, "/node6");
Packit Service 0ee8e1
	if (n6 < 0)
Packit Service 0ee8e1
		FAIL("fdt_path_offset(/node6): %s", fdt_strerror(n6));
Packit 2ad57b
Packit 2ad57b
	h1 = fdt_get_phandle(fdt, n1);
Packit 2ad57b
	h2 = fdt_get_phandle(fdt, n2);
Packit 2ad57b
	h4 = fdt_get_phandle(fdt, n4);
Packit 2ad57b
	h5 = fdt_get_phandle(fdt, n5);
Packit Service 0ee8e1
	h6 = fdt_get_phandle(fdt, n6);
Packit 2ad57b
Packit 2ad57b
	if (h1 != 0x2000)
Packit 2ad57b
		FAIL("/node1 has wrong phandle, 0x%x instead of 0x%x",
Packit 2ad57b
		     h1, 0x2000);
Packit 2ad57b
	if (h2 != 0x1)
Packit 2ad57b
		FAIL("/node2 has wrong phandle, 0x%x instead of 0x%x",
Packit 2ad57b
		     h2, 0x1);
Packit Service 0ee8e1
	if (h6 != FDT_MAX_PHANDLE)
Packit Service 0ee8e1
		FAIL("/node6 has wrong phandle, 0x%x instead of 0x%x",
Packit Service 0ee8e1
		     h6, FDT_MAX_PHANDLE);
Packit 2ad57b
	if ((h4 == 0x2000) || (h4 == 0x1) || (h4 == 0))
Packit 2ad57b
		FAIL("/node4 has bad phandle, 0x%x", h4);
Packit 2ad57b
Packit 2ad57b
	if ((h5 == 0) || (h5 == -1))
Packit 2ad57b
		FAIL("/node5 has bad phandle, 0x%x", h5);
Packit 2ad57b
	if ((h5 == h4) || (h5 == h2) || (h5 == h1))
Packit 2ad57b
		FAIL("/node5 has duplicate phandle, 0x%x", h5);
Packit 2ad57b
Packit Service 0ee8e1
	/*
Packit Service 0ee8e1
	 * /node6 has phandle FDT_MAX_PHANDLE, so fdt_generate_phandle() is
Packit Service 0ee8e1
	 * expected to fail.
Packit Service 0ee8e1
	 */
Packit Service 0ee8e1
	err = fdt_generate_phandle(fdt, &hn;;
Packit Service 0ee8e1
	if (err != -FDT_ERR_NOPHANDLES)
Packit Service 0ee8e1
		FAIL("generated invalid phandle 0x%x\n", hn);
Packit Service 0ee8e1
Packit 2ad57b
	check_ref(fdt, n1, h2);
Packit 2ad57b
	check_ref(fdt, n2, h1);
Packit 2ad57b
	check_ref(fdt, n3, h4);
Packit 2ad57b
Packit 2ad57b
	check_rref(fdt);
Packit 2ad57b
Packit 2ad57b
	PASS();
Packit 2ad57b
}