2006-11-27 06:21:28 +01:00
|
|
|
/*
|
|
|
|
* libfdt - Flat Device Tree manipulation
|
|
|
|
* Testcase common utility functions
|
|
|
|
* Copyright (C) 2006 David Gibson, IBM Corporation.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2.1 of
|
|
|
|
* the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
dtc: Address an assortment of portability problems
I've recently worked with a FreeBSD developer, getting dtc and libfdt
working on FreeBSD. This showed up a number of portability problems
in the dtc package which this patch addresses. Changes are as
follows:
- the parent_offset and supernode_atdepth_offset testcases
used the glibc extension functions strchrnul() and strndupa(). Those
are removed, using slightly longer coding with standard C functions
instead.
- some other testcases had a #define _GNU_SOURCE for no
particular reason. This is removed.
- run_tests.sh has bash specific constructs removed, and the
interpreter changed to /bin/sh. This apparently now runs fine on
FreeBSD's /bin/sh, and I've also tested it with both ash and dash.
- convert-dtsv0-lexer.l has some extra #includes added. These
must have been included indirectly with Linux and glibc, but aren't on
FreeBSD.
- the endian handling functions in libfdt_env.h, based on
endian.h and byteswap.h are replaced with some portable open-coded
versions. Unfortunately, these result in fairly crappy code when
compiled, but as far as I can determine there doesn't seem to be any
POSIX, SUS or de facto standard way of determining endianness at
compile time, nor standard names for byteswapping functions.
- some more endian handling, from testdata.h using the
problematic endian.h is simply removed, since it wasn't actually being
used anyway.
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2008-06-26 03:03:49 +02:00
|
|
|
#define _GNU_SOURCE /* for strsignal() in glibc. FreeBSD has it either way */
|
2006-11-27 06:21:28 +01:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2007-03-23 05:16:54 +01:00
|
|
|
#include <stdint.h>
|
2006-11-27 06:21:28 +01:00
|
|
|
#include <limits.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <unistd.h>
|
2006-11-28 07:20:01 +01:00
|
|
|
#include <fcntl.h>
|
2006-11-27 06:21:28 +01:00
|
|
|
|
2018-03-17 10:05:24 +01:00
|
|
|
#include <valgrind/memcheck.h>
|
|
|
|
|
2006-11-27 06:21:28 +01:00
|
|
|
#include <libfdt.h>
|
|
|
|
|
|
|
|
#include "tests.h"
|
|
|
|
|
2018-03-17 09:02:58 +01:00
|
|
|
/* For FDT_SW_MAGIC */
|
|
|
|
#include "libfdt_internal.h"
|
|
|
|
|
2006-11-27 06:21:28 +01:00
|
|
|
int verbose_test = 1;
|
|
|
|
char *test_name;
|
|
|
|
|
|
|
|
void __attribute__((weak)) cleanup(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sigint_handler(int signum, siginfo_t *si, void *uc)
|
|
|
|
{
|
|
|
|
cleanup();
|
|
|
|
fprintf(stderr, "%s: %s (pid=%d)\n", test_name,
|
|
|
|
strsignal(signum), getpid());
|
|
|
|
exit(RC_BUG);
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_init(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
struct sigaction sa_int = {
|
|
|
|
.sa_sigaction = sigint_handler,
|
|
|
|
};
|
|
|
|
|
|
|
|
test_name = argv[0];
|
|
|
|
|
|
|
|
err = sigaction(SIGINT, &sa_int, NULL);
|
|
|
|
if (err)
|
|
|
|
FAIL("Can't install SIGINT handler");
|
|
|
|
|
|
|
|
if (getenv("QUIET_TEST"))
|
|
|
|
verbose_test = 0;
|
|
|
|
|
|
|
|
verbose_printf("Starting testcase \"%s\", pid %d\n",
|
|
|
|
test_name, getpid());
|
|
|
|
}
|
|
|
|
|
2007-10-10 09:12:12 +02:00
|
|
|
void check_mem_rsv(void *fdt, int n, uint64_t addr, uint64_t size)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
uint64_t addr_v, size_v;
|
|
|
|
|
|
|
|
err = fdt_get_mem_rsv(fdt, n, &addr_v, &size_v);
|
|
|
|
if (err < 0)
|
|
|
|
FAIL("fdt_get_mem_rsv(%d): %s", n, fdt_strerror(err));
|
|
|
|
if ((addr_v != addr) || (size_v != size))
|
|
|
|
FAIL("fdt_get_mem_rsv() returned (0x%llx,0x%llx) "
|
|
|
|
"instead of (0x%llx,0x%llx)",
|
|
|
|
(unsigned long long)addr_v, (unsigned long long)size_v,
|
|
|
|
(unsigned long long)addr, (unsigned long long)size);
|
|
|
|
}
|
|
|
|
|
2006-12-15 05:12:47 +01:00
|
|
|
void check_property(void *fdt, int nodeoffset, const char *name,
|
2006-11-27 06:21:28 +01:00
|
|
|
int len, const void *val)
|
|
|
|
{
|
|
|
|
const struct fdt_property *prop;
|
2017-11-14 12:45:56 +01:00
|
|
|
int retlen, namelen;
|
2006-11-27 06:21:28 +01:00
|
|
|
uint32_t tag, nameoff, proplen;
|
|
|
|
const char *propname;
|
|
|
|
|
|
|
|
verbose_printf("Checking property \"%s\"...", name);
|
2007-02-23 04:40:10 +01:00
|
|
|
prop = fdt_get_property(fdt, nodeoffset, name, &retlen);
|
2006-11-27 06:21:28 +01:00
|
|
|
verbose_printf("pointer %p\n", prop);
|
|
|
|
if (! prop)
|
2007-02-23 04:40:10 +01:00
|
|
|
FAIL("Error retreiving \"%s\" pointer: %s", name,
|
|
|
|
fdt_strerror(retlen));
|
2006-11-27 06:21:28 +01:00
|
|
|
|
|
|
|
tag = fdt32_to_cpu(prop->tag);
|
|
|
|
nameoff = fdt32_to_cpu(prop->nameoff);
|
|
|
|
proplen = fdt32_to_cpu(prop->len);
|
|
|
|
|
|
|
|
if (tag != FDT_PROP)
|
|
|
|
FAIL("Incorrect tag 0x%08x on property \"%s\"", tag, name);
|
|
|
|
|
2017-11-14 12:45:56 +01:00
|
|
|
propname = fdt_get_string(fdt, nameoff, &namelen);
|
|
|
|
if (!propname)
|
|
|
|
FAIL("Couldn't get property name: %s", fdt_strerror(namelen));
|
|
|
|
if (namelen != strlen(propname))
|
|
|
|
FAIL("Incorrect prop name length: %d instead of %zd",
|
|
|
|
namelen, strlen(propname));
|
|
|
|
if (!streq(propname, name))
|
2006-11-27 06:21:28 +01:00
|
|
|
FAIL("Property name mismatch \"%s\" instead of \"%s\"",
|
|
|
|
propname, name);
|
2007-02-23 04:40:10 +01:00
|
|
|
if (proplen != retlen)
|
|
|
|
FAIL("Length retrieved for \"%s\" by fdt_get_property()"
|
|
|
|
" differs from stored length (%d != %d)",
|
|
|
|
name, retlen, proplen);
|
2006-11-27 06:21:28 +01:00
|
|
|
if (proplen != len)
|
|
|
|
FAIL("Size mismatch on property \"%s\": %d insead of %d",
|
|
|
|
name, proplen, len);
|
2018-07-20 06:35:12 +02:00
|
|
|
if (len && memcmp(val, prop->data, len) != 0)
|
2006-11-27 06:21:28 +01:00
|
|
|
FAIL("Data mismatch on property \"%s\"", name);
|
|
|
|
}
|
|
|
|
|
2007-06-13 06:18:10 +02:00
|
|
|
const void *check_getprop(void *fdt, int nodeoffset, const char *name,
|
|
|
|
int len, const void *val)
|
2006-11-27 06:21:28 +01:00
|
|
|
{
|
2007-06-13 06:18:10 +02:00
|
|
|
const void *propval;
|
2006-11-27 06:21:28 +01:00
|
|
|
int proplen;
|
|
|
|
|
|
|
|
propval = fdt_getprop(fdt, nodeoffset, name, &proplen);
|
2006-12-15 05:12:49 +01:00
|
|
|
if (! propval)
|
2007-02-23 04:40:10 +01:00
|
|
|
FAIL("fdt_getprop(\"%s\"): %s", name, fdt_strerror(proplen));
|
2006-11-27 06:21:28 +01:00
|
|
|
|
|
|
|
if (proplen != len)
|
|
|
|
FAIL("Size mismatch on property \"%s\": %d insead of %d",
|
|
|
|
name, proplen, len);
|
2018-07-20 06:35:12 +02:00
|
|
|
if (len && memcmp(val, propval, len) != 0)
|
2006-11-27 06:21:28 +01:00
|
|
|
FAIL("Data mismatch on property \"%s\"", name);
|
|
|
|
|
|
|
|
return propval;
|
|
|
|
}
|
2006-11-28 07:20:01 +01:00
|
|
|
|
2007-09-28 07:51:04 +02:00
|
|
|
int nodename_eq(const char *s1, const char *s2)
|
|
|
|
{
|
|
|
|
int len = strlen(s2);
|
|
|
|
|
|
|
|
if (strncmp(s1, s2, len) != 0)
|
|
|
|
return 0;
|
|
|
|
if (s1[len] == '\0')
|
|
|
|
return 1;
|
|
|
|
else if (!memchr(s2, '@', len) && (s1[len] == '@'))
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-03-17 09:02:58 +01:00
|
|
|
void vg_prepare_blob(void *fdt, size_t bufsize)
|
|
|
|
{
|
|
|
|
char *blob = fdt;
|
|
|
|
int off_memrsv, off_strings, off_struct;
|
2018-07-23 04:16:09 +02:00
|
|
|
int num_memrsv;
|
2018-03-17 09:02:58 +01:00
|
|
|
size_t size_memrsv, size_strings, size_struct;
|
|
|
|
|
2018-07-23 04:16:09 +02:00
|
|
|
off_memrsv = fdt_off_mem_rsvmap(fdt);
|
|
|
|
num_memrsv = fdt_num_mem_rsv(fdt);
|
|
|
|
if (num_memrsv < 0)
|
|
|
|
size_memrsv = fdt_totalsize(fdt) - off_memrsv;
|
|
|
|
else
|
|
|
|
size_memrsv = (num_memrsv + 1)
|
|
|
|
* sizeof(struct fdt_reserve_entry);
|
2018-03-17 09:02:58 +01:00
|
|
|
|
|
|
|
VALGRIND_MAKE_MEM_UNDEFINED(blob, bufsize);
|
|
|
|
VALGRIND_MAKE_MEM_DEFINED(blob, FDT_V1_SIZE);
|
|
|
|
VALGRIND_MAKE_MEM_DEFINED(blob, fdt_header_size(fdt));
|
|
|
|
|
|
|
|
if (fdt_magic(fdt) == FDT_MAGIC) {
|
|
|
|
off_strings = fdt_off_dt_strings(fdt);
|
|
|
|
if (fdt_version(fdt) >= 3)
|
|
|
|
size_strings = fdt_size_dt_strings(fdt);
|
|
|
|
else
|
|
|
|
size_strings = fdt_totalsize(fdt) - off_strings;
|
|
|
|
|
|
|
|
off_struct = fdt_off_dt_struct(fdt);
|
|
|
|
if (fdt_version(fdt) >= 17)
|
|
|
|
size_struct = fdt_size_dt_struct(fdt);
|
|
|
|
else
|
|
|
|
size_struct = fdt_totalsize(fdt) - off_struct;
|
|
|
|
} else if (fdt_magic(fdt) == FDT_SW_MAGIC) {
|
|
|
|
size_strings = fdt_size_dt_strings(fdt);
|
|
|
|
off_strings = fdt_off_dt_strings(fdt) - size_strings;
|
|
|
|
|
|
|
|
off_struct = fdt_off_dt_struct(fdt);
|
|
|
|
size_struct = fdt_size_dt_struct(fdt);
|
|
|
|
size_struct = fdt_totalsize(fdt) - off_struct;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
CONFIG("Bad magic on vg_prepare_blob()");
|
|
|
|
}
|
|
|
|
|
|
|
|
VALGRIND_MAKE_MEM_DEFINED(blob + off_memrsv, size_memrsv);
|
|
|
|
VALGRIND_MAKE_MEM_DEFINED(blob + off_strings, size_strings);
|
|
|
|
VALGRIND_MAKE_MEM_DEFINED(blob + off_struct, size_struct);
|
|
|
|
}
|
|
|
|
|
2006-11-28 07:20:01 +01:00
|
|
|
void *load_blob(const char *filename)
|
|
|
|
{
|
2011-09-22 19:11:03 +02:00
|
|
|
char *blob;
|
2018-03-17 09:02:58 +01:00
|
|
|
size_t len;
|
|
|
|
int ret = utilfdt_read_err(filename, &blob, &len);
|
2006-11-28 07:20:01 +01:00
|
|
|
|
2011-09-22 19:11:03 +02:00
|
|
|
if (ret)
|
|
|
|
CONFIG("Couldn't open blob from \"%s\": %s", filename,
|
|
|
|
strerror(ret));
|
2018-03-17 09:02:58 +01:00
|
|
|
|
|
|
|
vg_prepare_blob(blob, len);
|
|
|
|
|
2011-09-22 19:11:03 +02:00
|
|
|
return blob;
|
2006-11-28 07:20:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void *load_blob_arg(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
if (argc != 2)
|
|
|
|
CONFIG("Usage: %s <dtb file>", argv[0]);
|
|
|
|
return load_blob(argv[1]);
|
|
|
|
}
|
2006-11-29 06:45:46 +01:00
|
|
|
|
2006-12-15 05:12:47 +01:00
|
|
|
void save_blob(const char *filename, void *fdt)
|
2006-11-29 06:45:46 +01:00
|
|
|
{
|
2018-03-17 10:05:24 +01:00
|
|
|
size_t size = fdt_totalsize(fdt);
|
|
|
|
void *tmp;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Make a temp copy of the blob so that valgrind won't check
|
|
|
|
* about uninitialized bits in the pieces between blocks */
|
|
|
|
tmp = xmalloc(size);
|
|
|
|
fdt_move(fdt, tmp, size);
|
|
|
|
VALGRIND_MAKE_MEM_DEFINED(tmp, size);
|
|
|
|
ret = utilfdt_write_err(filename, tmp);
|
2011-09-22 19:11:03 +02:00
|
|
|
if (ret)
|
|
|
|
CONFIG("Couldn't write blob to \"%s\": %s", filename,
|
|
|
|
strerror(ret));
|
2018-03-17 10:05:24 +01:00
|
|
|
free(tmp);
|
2006-11-29 06:45:46 +01:00
|
|
|
}
|
2007-11-01 01:37:31 +01:00
|
|
|
|
|
|
|
void *open_blob_rw(void *blob)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
void *buf = blob;
|
|
|
|
|
|
|
|
err = fdt_open_into(blob, buf, fdt_totalsize(blob));
|
|
|
|
if (err == -FDT_ERR_NOSPACE) {
|
|
|
|
/* Ran out of space converting to v17 */
|
|
|
|
int newsize = fdt_totalsize(blob) + 8;
|
|
|
|
|
|
|
|
buf = xmalloc(newsize);
|
|
|
|
err = fdt_open_into(blob, buf, newsize);
|
|
|
|
}
|
|
|
|
if (err)
|
|
|
|
FAIL("fdt_open_into(): %s", fdt_strerror(err));
|
|
|
|
return buf;
|
|
|
|
}
|