Merge ../libfdt into dtc-libfdt-merge

This commit is contained in:
David Gibson 2007-06-14 12:21:33 +10:00
commit 400bd98a3a
39 changed files with 3981 additions and 0 deletions

5
libfdt/.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
*.d
*.o
*.a
*.so
*~

340
libfdt/GPL Normal file
View File

@ -0,0 +1,340 @@
GNU GENERAL PUBLIC LICENSE
Version 2, June 1991
Copyright (C) 1989, 1991 Free Software Foundation, Inc.
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
Preamble
The licenses for most software are designed to take away your
freedom to share and change it. By contrast, the GNU General Public
License is intended to guarantee your freedom to share and change free
software--to make sure the software is free for all its users. This
General Public License applies to most of the Free Software
Foundation's software and to any other program whose authors commit to
using it. (Some other Free Software Foundation software is covered by
the GNU Library General Public License instead.) You can apply it to
your programs, too.
When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
this service if you wish), that you receive source code or can get it
if you want it, that you can change the software or use pieces of it
in new free programs; and that you know you can do these things.
To protect your rights, we need to make restrictions that forbid
anyone to deny you these rights or to ask you to surrender the rights.
These restrictions translate to certain responsibilities for you if you
distribute copies of the software, or if you modify it.
For example, if you distribute copies of such a program, whether
gratis or for a fee, you must give the recipients all the rights that
you have. You must make sure that they, too, receive or can get the
source code. And you must show them these terms so they know their
rights.
We protect your rights with two steps: (1) copyright the software, and
(2) offer you this license which gives you legal permission to copy,
distribute and/or modify the software.
Also, for each author's protection and ours, we want to make certain
that everyone understands that there is no warranty for this free
software. If the software is modified by someone else and passed on, we
want its recipients to know that what they have is not the original, so
that any problems introduced by others will not reflect on the original
authors' reputations.
Finally, any free program is threatened constantly by software
patents. We wish to avoid the danger that redistributors of a free
program will individually obtain patent licenses, in effect making the
program proprietary. To prevent this, we have made it clear that any
patent must be licensed for everyone's free use or not licensed at all.
The precise terms and conditions for copying, distribution and
modification follow.
GNU GENERAL PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. This License applies to any program or other work which contains
a notice placed by the copyright holder saying it may be distributed
under the terms of this General Public License. The "Program", below,
refers to any such program or work, and a "work based on the Program"
means either the Program or any derivative work under copyright law:
that is to say, a work containing the Program or a portion of it,
either verbatim or with modifications and/or translated into another
language. (Hereinafter, translation is included without limitation in
the term "modification".) Each licensee is addressed as "you".
Activities other than copying, distribution and modification are not
covered by this License; they are outside its scope. The act of
running the Program is not restricted, and the output from the Program
is covered only if its contents constitute a work based on the
Program (independent of having been made by running the Program).
Whether that is true depends on what the Program does.
1. You may copy and distribute verbatim copies of the Program's
source code as you receive it, in any medium, provided that you
conspicuously and appropriately publish on each copy an appropriate
copyright notice and disclaimer of warranty; keep intact all the
notices that refer to this License and to the absence of any warranty;
and give any other recipients of the Program a copy of this License
along with the Program.
You may charge a fee for the physical act of transferring a copy, and
you may at your option offer warranty protection in exchange for a fee.
2. You may modify your copy or copies of the Program or any portion
of it, thus forming a work based on the Program, and copy and
distribute such modifications or work under the terms of Section 1
above, provided that you also meet all of these conditions:
a) You must cause the modified files to carry prominent notices
stating that you changed the files and the date of any change.
b) You must cause any work that you distribute or publish, that in
whole or in part contains or is derived from the Program or any
part thereof, to be licensed as a whole at no charge to all third
parties under the terms of this License.
c) If the modified program normally reads commands interactively
when run, you must cause it, when started running for such
interactive use in the most ordinary way, to print or display an
announcement including an appropriate copyright notice and a
notice that there is no warranty (or else, saying that you provide
a warranty) and that users may redistribute the program under
these conditions, and telling the user how to view a copy of this
License. (Exception: if the Program itself is interactive but
does not normally print such an announcement, your work based on
the Program is not required to print an announcement.)
These requirements apply to the modified work as a whole. If
identifiable sections of that work are not derived from the Program,
and can be reasonably considered independent and separate works in
themselves, then this License, and its terms, do not apply to those
sections when you distribute them as separate works. But when you
distribute the same sections as part of a whole which is a work based
on the Program, the distribution of the whole must be on the terms of
this License, whose permissions for other licensees extend to the
entire whole, and thus to each and every part regardless of who wrote it.
Thus, it is not the intent of this section to claim rights or contest
your rights to work written entirely by you; rather, the intent is to
exercise the right to control the distribution of derivative or
collective works based on the Program.
In addition, mere aggregation of another work not based on the Program
with the Program (or with a work based on the Program) on a volume of
a storage or distribution medium does not bring the other work under
the scope of this License.
3. You may copy and distribute the Program (or a work based on it,
under Section 2) in object code or executable form under the terms of
Sections 1 and 2 above provided that you also do one of the following:
a) Accompany it with the complete corresponding machine-readable
source code, which must be distributed under the terms of Sections
1 and 2 above on a medium customarily used for software interchange; or,
b) Accompany it with a written offer, valid for at least three
years, to give any third party, for a charge no more than your
cost of physically performing source distribution, a complete
machine-readable copy of the corresponding source code, to be
distributed under the terms of Sections 1 and 2 above on a medium
customarily used for software interchange; or,
c) Accompany it with the information you received as to the offer
to distribute corresponding source code. (This alternative is
allowed only for noncommercial distribution and only if you
received the program in object code or executable form with such
an offer, in accord with Subsection b above.)
The source code for a work means the preferred form of the work for
making modifications to it. For an executable work, complete source
code means all the source code for all modules it contains, plus any
associated interface definition files, plus the scripts used to
control compilation and installation of the executable. However, as a
special exception, the source code distributed need not include
anything that is normally distributed (in either source or binary
form) with the major components (compiler, kernel, and so on) of the
operating system on which the executable runs, unless that component
itself accompanies the executable.
If distribution of executable or object code is made by offering
access to copy from a designated place, then offering equivalent
access to copy the source code from the same place counts as
distribution of the source code, even though third parties are not
compelled to copy the source along with the object code.
4. You may not copy, modify, sublicense, or distribute the Program
except as expressly provided under this License. Any attempt
otherwise to copy, modify, sublicense or distribute the Program is
void, and will automatically terminate your rights under this License.
However, parties who have received copies, or rights, from you under
this License will not have their licenses terminated so long as such
parties remain in full compliance.
5. You are not required to accept this License, since you have not
signed it. However, nothing else grants you permission to modify or
distribute the Program or its derivative works. These actions are
prohibited by law if you do not accept this License. Therefore, by
modifying or distributing the Program (or any work based on the
Program), you indicate your acceptance of this License to do so, and
all its terms and conditions for copying, distributing or modifying
the Program or works based on it.
6. Each time you redistribute the Program (or any work based on the
Program), the recipient automatically receives a license from the
original licensor to copy, distribute or modify the Program subject to
these terms and conditions. You may not impose any further
restrictions on the recipients' exercise of the rights granted herein.
You are not responsible for enforcing compliance by third parties to
this License.
7. If, as a consequence of a court judgment or allegation of patent
infringement or for any other reason (not limited to patent issues),
conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot
distribute so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you
may not distribute the Program at all. For example, if a patent
license would not permit royalty-free redistribution of the Program by
all those who receive copies directly or indirectly through you, then
the only way you could satisfy both it and this License would be to
refrain entirely from distribution of the Program.
If any portion of this section is held invalid or unenforceable under
any particular circumstance, the balance of the section is intended to
apply and the section as a whole is intended to apply in other
circumstances.
It is not the purpose of this section to induce you to infringe any
patents or other property right claims or to contest validity of any
such claims; this section has the sole purpose of protecting the
integrity of the free software distribution system, which is
implemented by public license practices. Many people have made
generous contributions to the wide range of software distributed
through that system in reliance on consistent application of that
system; it is up to the author/donor to decide if he or she is willing
to distribute software through any other system and a licensee cannot
impose that choice.
This section is intended to make thoroughly clear what is believed to
be a consequence of the rest of this License.
8. If the distribution and/or use of the Program is restricted in
certain countries either by patents or by copyrighted interfaces, the
original copyright holder who places the Program under this License
may add an explicit geographical distribution limitation excluding
those countries, so that distribution is permitted only in or among
countries not thus excluded. In such case, this License incorporates
the limitation as if written in the body of this License.
9. The Free Software Foundation may publish revised and/or new versions
of the General Public License from time to time. Such new versions will
be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.
Each version is given a distinguishing version number. If the Program
specifies a version number of this License which applies to it and "any
later version", you have the option of following the terms and conditions
either of that version or of any later version published by the Free
Software Foundation. If the Program does not specify a version number of
this License, you may choose any version ever published by the Free Software
Foundation.
10. If you wish to incorporate parts of the Program into other free
programs whose distribution conditions are different, write to the author
to ask for permission. For software which is copyrighted by the Free
Software Foundation, write to the Free Software Foundation; we sometimes
make exceptions for this. Our decision will be guided by the two goals
of preserving the free status of all derivatives of our free software and
of promoting the sharing and reuse of software generally.
NO WARRANTY
11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
REPAIR OR CORRECTION.
12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGES.
END OF TERMS AND CONDITIONS
How to Apply These Terms to Your New Programs
If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these terms.
To do so, attach the following notices to the program. It is safest
to attach them to the start of each source file to most effectively
convey the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.
<one line to give the program's name and a brief idea of what it does.>
Copyright (C) <year> <name of author>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Also add information on how to contact you by electronic and paper mail.
If the program is interactive, make it output a short notice like this
when it starts in an interactive mode:
Gnomovision version 69, Copyright (C) year name of author
Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
The hypothetical commands `show w' and `show c' should show the appropriate
parts of the General Public License. Of course, the commands you use may
be called something other than `show w' and `show c'; they could even be
mouse-clicks or menu items--whatever suits your program.
You should also get your employer (if you work as a programmer) or your
school, if any, to sign a "copyright disclaimer" for the program, if
necessary. Here is a sample; alter the names:
Yoyodyne, Inc., hereby disclaims all copyright interest in the program
`Gnomovision' (which makes passes at compilers) written by James Hacker.
<signature of Ty Coon>, 1 April 1989
Ty Coon, President of Vice
This General Public License does not permit incorporating your program into
proprietary programs. If your program is a subroutine library, you may
consider it more useful to permit linking proprietary applications with the
library. If this is what you want to do, use the GNU Library General
Public License instead of this License.

88
libfdt/Makefile Normal file
View File

@ -0,0 +1,88 @@
PREFIX = /usr/local
TARGETLIBS = libfdt.a
LIBOBJS = fdt.o fdt_ro.o fdt_wip.o fdt_sw.o fdt_rw.o fdt_strerror.o
SOURCE = $(shell find . -maxdepth 1 ! -name version.h -a -name '*.[h]')
SOURCE += *.c Makefile
NODEPTARGETS=<clean>
CPPFLAGS = -I.
CFLAGS = -Wall -g
LIBDIR = $(PREFIX)/$(LIB32)
EXTRA_DIST = \
README \
HOWTO \
LGPL-2.1
ifdef V
VECHO = :
else
VECHO = echo " "
ARFLAGS = rc
.SILENT:
endif
DEPFILES = $(LIBOBJS:%.o=%.d)
all: libs tests
.PHONY: tests libs
libs: $(TARGETLIBS)
tests: tests/all
tests/%: libs
$(MAKE) -C tests $*
check: all
cd tests; ./run_tests.sh
checkv: all
cd tests; ./run_tests.sh -v
func: all
cd tests; ./run_tests.sh -t func
funcv: all
cd tests; ./run_tests.sh -t func -v
stress: all
cd tests; ./run_tests.sh -t stress
stressv: all
cd tests; ./run_tests.sh -t stress -v
%.o: %.c
@$(VECHO) CC $@
$(CC) $(CPPFLAGS) $(CFLAGS) -o $@ -c $<
libfdt.a: $(LIBOBJS)
@$(VECHO) AR $@
$(AR) $(ARFLAGS) $@ $^
%.i: %.c
@$(VECHO) CPP $@
$(CC) $(CPPFLAGS) -E $< > $@
%.s: %.c
@$(VECHO) CC -S $@
$(CC) $(CPPFLAGS) $(CFLAGS) -o $@ -S $<
clean:
@$(VECHO) CLEAN
rm -f *~ *.o *.so *.a *.d *.i *.s core a.out $(VERSION)
$(MAKE) -C tests clean
%.d: %.c
@$(CC) $(CPPFLAGS) -MM -MT "$*.o $@" $< > $@
# Workaround: Don't build dependencies for certain targets
# When the include below is executed, make will use the %.d target above to
# generate missing files. For certain targets (clean, version.h, etc) we don't
# need or want these dependency files, so don't include them in this case.
ifeq (,$(findstring <$(MAKECMDGOALS)>,$(NODEPTARGETS)))
-include $(DEPFILES)
endif

5
libfdt/TODO Normal file
View File

@ -0,0 +1,5 @@
- Find node by linux,phandle property
- Tree traversal functions
- Graft function
- Merge into dtc
- Make fdt_open_into() re-arrange properly

156
libfdt/fdt.c Normal file
View File

@ -0,0 +1,156 @@
/*
* libfdt - Flat Device Tree manipulation
* Copyright (C) 2006 David Gibson, IBM Corporation.
*
* libfdt is dual licensed: you can use it either under the terms of
* the GPL, or the BSD license, at your option.
*
* a) This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 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 General Public License for more details.
*
* You should have received a copy of the GNU 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
*
* Alternatively,
*
* b) Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* 1. Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer.
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "libfdt_env.h"
#include <fdt.h>
#include <libfdt.h>
#include "libfdt_internal.h"
int _fdt_check_header(const void *fdt)
{
if (fdt_magic(fdt) == FDT_MAGIC) {
/* Complete tree */
if (fdt_version(fdt) < FDT_FIRST_SUPPORTED_VERSION)
return -FDT_ERR_BADVERSION;
if (fdt_last_comp_version(fdt) > FDT_LAST_SUPPORTED_VERSION)
return -FDT_ERR_BADVERSION;
} else if (fdt_magic(fdt) == SW_MAGIC) {
/* Unfinished sequential-write blob */
if (fdt_size_dt_struct(fdt) == 0)
return -FDT_ERR_BADSTATE;
} else {
return -FDT_ERR_BADMAGIC;
}
return 0;
}
const void *fdt_offset_ptr(const void *fdt, int offset, int len)
{
const void *p;
if (fdt_version(fdt) >= 0x11)
if (((offset + len) < offset)
|| ((offset + len) > fdt_size_dt_struct(fdt)))
return NULL;
p = _fdt_offset_ptr(fdt, offset);
if (p + len < p)
return NULL;
return p;
}
uint32_t _fdt_next_tag(const void *fdt, int offset, int *nextoffset)
{
const uint32_t *tagp, *lenp;
uint32_t tag;
const char *p;
if (offset % FDT_TAGSIZE)
return -1;
tagp = fdt_offset_ptr(fdt, offset, FDT_TAGSIZE);
if (! tagp)
return FDT_END; /* premature end */
tag = fdt32_to_cpu(*tagp);
offset += FDT_TAGSIZE;
switch (tag) {
case FDT_BEGIN_NODE:
/* skip name */
do {
p = fdt_offset_ptr(fdt, offset++, 1);
} while (p && (*p != '\0'));
if (! p)
return FDT_END;
break;
case FDT_PROP:
lenp = fdt_offset_ptr(fdt, offset, sizeof(*lenp));
if (! lenp)
return FDT_END;
/* skip name offset, length and value */
offset += 2*FDT_TAGSIZE + fdt32_to_cpu(*lenp);
break;
}
if (nextoffset)
*nextoffset = ALIGN(offset, FDT_TAGSIZE);
return tag;
}
const char *_fdt_find_string(const char *strtab, int tabsize, const char *s)
{
int len = strlen(s) + 1;
const char *last = strtab + tabsize - len;
const char *p;
for (p = strtab; p <= last; p++)
if (memeq(p, s, len))
return p;
return NULL;
}
int fdt_move(const void *fdt, void *buf, int bufsize)
{
int err = _fdt_check_header(fdt);
if (err)
return err;
if (fdt_totalsize(fdt) > bufsize)
return -FDT_ERR_NOSPACE;
memmove(buf, fdt, fdt_totalsize(fdt));
return 0;
}

60
libfdt/fdt.h Normal file
View File

@ -0,0 +1,60 @@
#ifndef _FDT_H
#define _FDT_H
#ifndef __ASSEMBLY__
struct fdt_header {
uint32_t magic; /* magic word FDT_MAGIC */
uint32_t totalsize; /* total size of DT block */
uint32_t off_dt_struct; /* offset to structure */
uint32_t off_dt_strings; /* offset to strings */
uint32_t off_mem_rsvmap; /* offset to memory reserve map */
uint32_t version; /* format version */
uint32_t last_comp_version; /* last compatible version */
/* version 2 fields below */
uint32_t boot_cpuid_phys; /* Which physical CPU id we're
booting on */
/* version 3 fields below */
uint32_t size_dt_strings; /* size of the strings block */
/* version 17 fields below */
uint32_t size_dt_struct; /* size of the structure block */
};
struct fdt_reserve_entry {
uint64_t address;
uint64_t size;
};
struct fdt_node_header {
uint32_t tag;
char name[0];
};
struct fdt_property {
uint32_t tag;
uint32_t len;
uint32_t nameoff;
char data[0];
};
#endif /* !__ASSEMBLY */
#define FDT_MAGIC 0xd00dfeed /* 4: version, 4: total size */
#define FDT_TAGSIZE sizeof(uint32_t)
#define FDT_BEGIN_NODE 0x1 /* Start node: full name */
#define FDT_END_NODE 0x2 /* End node */
#define FDT_PROP 0x3 /* Property: name off,
size, content */
#define FDT_NOP 0x4 /* nop */
#define FDT_END 0x9
#define FDT_V1_SIZE (7*sizeof(uint32_t))
#define FDT_V2_SIZE (FDT_V1_SIZE + sizeof(uint32_t))
#define FDT_V3_SIZE (FDT_V2_SIZE + sizeof(uint32_t))
#define FDT_V16_SIZE FDT_V3_SIZE
#define FDT_V17_SIZE (FDT_V16_SIZE + sizeof(uint32_t))
#endif /* _FDT_H */

261
libfdt/fdt_ro.c Normal file
View File

@ -0,0 +1,261 @@
/*
* libfdt - Flat Device Tree manipulation
* Copyright (C) 2006 David Gibson, IBM Corporation.
*
* libfdt is dual licensed: you can use it either under the terms of
* the GPL, or the BSD license, at your option.
*
* a) This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 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 General Public License for more details.
*
* You should have received a copy of the GNU 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
*
* Alternatively,
*
* b) Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* 1. Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer.
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "libfdt_env.h"
#include <fdt.h>
#include <libfdt.h>
#include "libfdt_internal.h"
#define CHECK_HEADER(fdt) \
{ \
int err; \
if ((err = _fdt_check_header(fdt)) != 0) \
return err; \
}
static int offset_streq(const void *fdt, int offset,
const char *s, int len)
{
const char *p = fdt_offset_ptr(fdt, offset, len+1);
if (! p)
/* short match */
return 0;
if (memcmp(p, s, len) != 0)
return 0;
if (p[len] != '\0')
return 0;
return 1;
}
char *fdt_string(const void *fdt, int stroffset)
{
return (char *)fdt + fdt_off_dt_strings(fdt) + stroffset;
}
int fdt_subnode_offset_namelen(const void *fdt, int parentoffset,
const char *name, int namelen)
{
int level = 0;
uint32_t tag;
int offset, nextoffset;
CHECK_HEADER(fdt);
tag = _fdt_next_tag(fdt, parentoffset, &nextoffset);
if (tag != FDT_BEGIN_NODE)
return -FDT_ERR_BADOFFSET;
do {
offset = nextoffset;
tag = _fdt_next_tag(fdt, offset, &nextoffset);
switch (tag) {
case FDT_END:
return -FDT_ERR_TRUNCATED;
case FDT_BEGIN_NODE:
level++;
if (level != 1)
continue;
if (offset_streq(fdt, offset+FDT_TAGSIZE, name, namelen))
/* Found it! */
return offset;
break;
case FDT_END_NODE:
level--;
break;
case FDT_PROP:
case FDT_NOP:
break;
default:
return -FDT_ERR_BADSTRUCTURE;
}
} while (level >= 0);
return -FDT_ERR_NOTFOUND;
}
int fdt_subnode_offset(const void *fdt, int parentoffset,
const char *name)
{
return fdt_subnode_offset_namelen(fdt, parentoffset, name, strlen(name));
}
int fdt_path_offset(const void *fdt, const char *path)
{
const char *end = path + strlen(path);
const char *p = path;
int offset = 0;
CHECK_HEADER(fdt);
if (*path != '/')
return -FDT_ERR_BADPATH;
while (*p) {
const char *q;
while (*p == '/')
p++;
if (! *p)
return -FDT_ERR_BADPATH;
q = strchr(p, '/');
if (! q)
q = end;
offset = fdt_subnode_offset_namelen(fdt, offset, p, q-p);
if (offset < 0)
return offset;
p = q;
}
return offset;
}
const struct fdt_property *fdt_get_property(const void *fdt,
int nodeoffset,
const char *name, int *lenp)
{
int level = 0;
uint32_t tag;
const struct fdt_property *prop;
int namestroff;
int offset, nextoffset;
int err;
if ((err = _fdt_check_header(fdt)) != 0)
goto fail;
err = -FDT_ERR_BADOFFSET;
if (nodeoffset % FDT_TAGSIZE)
goto fail;
tag = _fdt_next_tag(fdt, nodeoffset, &nextoffset);
if (tag != FDT_BEGIN_NODE)
goto fail;
do {
offset = nextoffset;
tag = _fdt_next_tag(fdt, offset, &nextoffset);
switch (tag) {
case FDT_END:
err = -FDT_ERR_TRUNCATED;
goto fail;
case FDT_BEGIN_NODE:
level++;
break;
case FDT_END_NODE:
level--;
break;
case FDT_PROP:
if (level != 0)
continue;
err = -FDT_ERR_BADSTRUCTURE;
prop = fdt_offset_ptr_typed(fdt, offset, prop);
if (! prop)
goto fail;
namestroff = fdt32_to_cpu(prop->nameoff);
if (streq(fdt_string(fdt, namestroff), name)) {
/* Found it! */
int len = fdt32_to_cpu(prop->len);
prop = fdt_offset_ptr(fdt, offset,
sizeof(*prop)+len);
if (! prop)
goto fail;
if (lenp)
*lenp = len;
return prop;
}
break;
case FDT_NOP:
break;
default:
err = -FDT_ERR_BADSTRUCTURE;
goto fail;
}
} while (level >= 0);
err = -FDT_ERR_NOTFOUND;
fail:
if (lenp)
*lenp = err;
return NULL;
}
const void *fdt_getprop(const void *fdt, int nodeoffset,
const char *name, int *lenp)
{
const struct fdt_property *prop;
prop = fdt_get_property(fdt, nodeoffset, name, lenp);
if (! prop)
return NULL;
return prop->data;
}

325
libfdt/fdt_rw.c Normal file
View File

@ -0,0 +1,325 @@
/*
* libfdt - Flat Device Tree manipulation
* Copyright (C) 2006 David Gibson, IBM Corporation.
*
* libfdt is dual licensed: you can use it either under the terms of
* the GPL, or the BSD license, at your option.
*
* a) This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 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 General Public License for more details.
*
* You should have received a copy of the GNU 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
*
* Alternatively,
*
* b) Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* 1. Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer.
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "libfdt_env.h"
#include <fdt.h>
#include <libfdt.h>
#include "libfdt_internal.h"
static int rw_check_header(void *fdt)
{
int err;
if ((err = _fdt_check_header(fdt)))
return err;
if (fdt_version(fdt) < 0x11)
return -FDT_ERR_BADVERSION;
if (fdt_off_mem_rsvmap(fdt) < ALIGN(sizeof(struct fdt_header), 8))
return -FDT_ERR_BADLAYOUT;
if (fdt_off_dt_struct(fdt) <
(fdt_off_mem_rsvmap(fdt) + sizeof(struct fdt_reserve_entry)))
return -FDT_ERR_BADLAYOUT;
if (fdt_off_dt_strings(fdt) <
(fdt_off_dt_struct(fdt) + fdt_size_dt_struct(fdt)))
return -FDT_ERR_BADLAYOUT;
if (fdt_totalsize(fdt) <
(fdt_off_dt_strings(fdt) + fdt_size_dt_strings(fdt)))
return -FDT_ERR_BADLAYOUT;
return 0;
}
#define RW_CHECK_HEADER(fdt) \
{ \
int err; \
if ((err = rw_check_header(fdt)) != 0) \
return err; \
}
static inline int _blob_data_size(void *fdt)
{
return fdt_off_dt_strings(fdt) + fdt_size_dt_strings(fdt);
}
static int _blob_splice(void *fdt, void *p, int oldlen, int newlen)
{
void *end = fdt + _blob_data_size(fdt);
if (((p + oldlen) < p) || ((p + oldlen) > end))
return -FDT_ERR_BADOFFSET;
if ((end - oldlen + newlen) > (fdt + fdt_totalsize(fdt)))
return -FDT_ERR_NOSPACE;
memmove(p + newlen, p + oldlen, end - p - oldlen);
return 0;
}
static int _blob_splice_struct(void *fdt, void *p,
int oldlen, int newlen)
{
int delta = newlen - oldlen;
int err;
if ((err = _blob_splice(fdt, p, oldlen, newlen)))
return err;
fdt_set_header(fdt, size_dt_struct, fdt_size_dt_struct(fdt) + delta);
fdt_set_header(fdt, off_dt_strings, fdt_off_dt_strings(fdt) + delta);
return 0;
}
static int _blob_splice_string(void *fdt, int newlen)
{
void *p = fdt + fdt_off_dt_strings(fdt) + fdt_size_dt_strings(fdt);
int err;
if ((err = _blob_splice(fdt, p, 0, newlen)))
return err;
fdt_set_header(fdt, size_dt_strings, fdt_size_dt_strings(fdt) + newlen);
return 0;
}
static int _find_add_string(void *fdt, const char *s)
{
char *strtab = (char *)fdt + fdt_off_dt_strings(fdt);
const char *p;
char *new;
int len = strlen(s) + 1;
int err;
p = _fdt_find_string(strtab, fdt_size_dt_strings(fdt), s);
if (p)
/* found it */
return (p - strtab);
new = strtab + fdt_size_dt_strings(fdt);
err = _blob_splice_string(fdt, len);
if (err)
return err;
memcpy(new, s, len);
return (new - strtab);
}
static int _resize_property(void *fdt, int nodeoffset, const char *name, int len,
struct fdt_property **prop)
{
int oldlen;
int err;
*prop = fdt_get_property_w(fdt, nodeoffset, name, &oldlen);
if (! (*prop))
return oldlen;
if ((err = _blob_splice_struct(fdt, (*prop)->data,
ALIGN(oldlen, FDT_TAGSIZE),
ALIGN(len, FDT_TAGSIZE))))
return err;
(*prop)->len = cpu_to_fdt32(len);
return 0;
}
static int _add_property(void *fdt, int nodeoffset, const char *name, int len,
struct fdt_property **prop)
{
uint32_t tag;
int proplen;
int nextoffset;
int namestroff;
int err;
tag = _fdt_next_tag(fdt, nodeoffset, &nextoffset);
if (tag != FDT_BEGIN_NODE)
return -FDT_ERR_BADOFFSET;
namestroff = _find_add_string(fdt, name);
if (namestroff < 0)
return namestroff;
*prop = _fdt_offset_ptr_w(fdt, nextoffset);
proplen = sizeof(**prop) + ALIGN(len, FDT_TAGSIZE);
err = _blob_splice_struct(fdt, *prop, 0, proplen);
if (err)
return err;
(*prop)->tag = cpu_to_fdt32(FDT_PROP);
(*prop)->nameoff = cpu_to_fdt32(namestroff);
(*prop)->len = cpu_to_fdt32(len);
return 0;
}
int fdt_setprop(void *fdt, int nodeoffset, const char *name,
const void *val, int len)
{
struct fdt_property *prop;
int err;
if ((err = rw_check_header(fdt)))
return err;
err = _resize_property(fdt, nodeoffset, name, len, &prop);
if (err == -FDT_ERR_NOTFOUND)
err = _add_property(fdt, nodeoffset, name, len, &prop);
if (err)
return err;
memcpy(prop->data, val, len);
return 0;
}
int fdt_delprop(void *fdt, int nodeoffset, const char *name)
{
struct fdt_property *prop;
int len, proplen;
RW_CHECK_HEADER(fdt);
prop = fdt_get_property_w(fdt, nodeoffset, name, &len);
if (! prop)
return len;
proplen = sizeof(*prop) + ALIGN(len, FDT_TAGSIZE);
return _blob_splice_struct(fdt, prop, proplen, 0);
}
int fdt_add_subnode_namelen(void *fdt, int parentoffset,
const char *name, int namelen)
{
struct fdt_node_header *nh;
int offset, nextoffset;
int nodelen;
int err;
uint32_t tag;
uint32_t *endtag;
RW_CHECK_HEADER(fdt);
offset = fdt_subnode_offset_namelen(fdt, parentoffset, name, namelen);
if (offset >= 0)
return -FDT_ERR_EXISTS;
else if (offset != -FDT_ERR_NOTFOUND)
return offset;
/* Try to place the new node after the parent's properties */
_fdt_next_tag(fdt, parentoffset, &nextoffset); /* skip the BEGIN_NODE */
do {
offset = nextoffset;
tag = _fdt_next_tag(fdt, offset, &nextoffset);
} while (tag == FDT_PROP);
nh = _fdt_offset_ptr_w(fdt, offset);
nodelen = sizeof(*nh) + ALIGN(namelen+1, FDT_TAGSIZE) + FDT_TAGSIZE;
err = _blob_splice_struct(fdt, nh, 0, nodelen);
if (err)
return err;
nh->tag = cpu_to_fdt32(FDT_BEGIN_NODE);
memset(nh->name, 0, ALIGN(namelen+1, FDT_TAGSIZE));
memcpy(nh->name, name, namelen);
endtag = (uint32_t *)((void *)nh + nodelen - FDT_TAGSIZE);
*endtag = cpu_to_fdt32(FDT_END_NODE);
return offset;
}
int fdt_add_subnode(void *fdt, int parentoffset, const char *name)
{
return fdt_add_subnode_namelen(fdt, parentoffset, name, strlen(name));
}
int fdt_del_node(void *fdt, int nodeoffset)
{
int endoffset;
endoffset = _fdt_node_end_offset(fdt, nodeoffset);
if (endoffset < 0)
return endoffset;
return _blob_splice_struct(fdt, _fdt_offset_ptr_w(fdt, nodeoffset),
endoffset - nodeoffset, 0);
}
int fdt_open_into(void *fdt, void *buf, int bufsize)
{
int err;
err = fdt_move(fdt, buf, bufsize);
if (err)
return err;
fdt = buf;
fdt_set_header(fdt, totalsize, bufsize);
/* FIXME: re-order if necessary */
err = rw_check_header(fdt);
if (err)
return err;
return 0;
}
int fdt_pack(void *fdt)
{
int err;
err = rw_check_header(fdt);
if (err)
return err;
/* FIXME: pack components */
fdt_set_header(fdt, totalsize, _blob_data_size(fdt));
return 0;
}

96
libfdt/fdt_strerror.c Normal file
View File

@ -0,0 +1,96 @@
/*
* libfdt - Flat Device Tree manipulation
* Copyright (C) 2006 David Gibson, IBM Corporation.
*
* libfdt is dual licensed: you can use it either under the terms of
* the GPL, or the BSD license, at your option.
*
* a) This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 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 General Public License for more details.
*
* You should have received a copy of the GNU 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
*
* Alternatively,
*
* b) Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* 1. Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer.
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "libfdt_env.h"
#include <fdt.h>
#include <libfdt.h>
#include "libfdt_internal.h"
struct errtabent {
const char *str;
};
#define ERRTABENT(val) \
[(val)] = { .str = #val, }
static struct errtabent errtable[] = {
ERRTABENT(FDT_ERR_NOTFOUND),
ERRTABENT(FDT_ERR_EXISTS),
ERRTABENT(FDT_ERR_NOSPACE),
ERRTABENT(FDT_ERR_BADOFFSET),
ERRTABENT(FDT_ERR_BADPATH),
ERRTABENT(FDT_ERR_BADSTATE),
ERRTABENT(FDT_ERR_TRUNCATED),
ERRTABENT(FDT_ERR_BADMAGIC),
ERRTABENT(FDT_ERR_BADVERSION),
ERRTABENT(FDT_ERR_BADSTRUCTURE),
ERRTABENT(FDT_ERR_BADLAYOUT),
};
#define ERRTABSIZE (sizeof(errtable) / sizeof(errtable[0]))
const char *fdt_strerror(int errval)
{
if (errval > 0)
return "<valid offset/length>";
else if (errval == 0)
return "<no error>";
else if (errval > -ERRTABSIZE) {
const char *s = errtable[-errval].str;
if (s)
return s;
}
return "<unknown error>";
}

258
libfdt/fdt_sw.c Normal file
View File

@ -0,0 +1,258 @@
/*
* libfdt - Flat Device Tree manipulation
* Copyright (C) 2006 David Gibson, IBM Corporation.
*
* libfdt is dual licensed: you can use it either under the terms of
* the GPL, or the BSD license, at your option.
*
* a) This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 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 General Public License for more details.
*
* You should have received a copy of the GNU 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
*
* Alternatively,
*
* b) Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* 1. Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer.
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "libfdt_env.h"
#include <fdt.h>
#include <libfdt.h>
#include "libfdt_internal.h"
static int check_header_sw(void *fdt)
{
if (fdt_magic(fdt) != SW_MAGIC)
return -FDT_ERR_BADMAGIC;
return 0;
}
static void *grab_space(void *fdt, int len)
{
int offset = fdt_size_dt_struct(fdt);
int spaceleft;
spaceleft = fdt_totalsize(fdt) - fdt_off_dt_struct(fdt)
- fdt_size_dt_strings(fdt);
if ((offset + len < offset) || (offset + len > spaceleft))
return NULL;
fdt_set_header(fdt, size_dt_struct, offset + len);
return fdt_offset_ptr_w(fdt, offset, len);
}
int fdt_create(void *buf, int bufsize)
{
void *fdt = buf;
if (bufsize < sizeof(struct fdt_header))
return -FDT_ERR_NOSPACE;
memset(buf, 0, bufsize);
fdt_set_header(fdt, magic, SW_MAGIC);
fdt_set_header(fdt, version, FDT_LAST_SUPPORTED_VERSION);
fdt_set_header(fdt, last_comp_version, FDT_FIRST_SUPPORTED_VERSION);
fdt_set_header(fdt, totalsize, bufsize);
fdt_set_header(fdt, off_mem_rsvmap, ALIGN(sizeof(struct fdt_header),
sizeof(struct fdt_reserve_entry)));
fdt_set_header(fdt, off_dt_struct, fdt_off_mem_rsvmap(fdt));
fdt_set_header(fdt, off_dt_strings, bufsize);
return 0;
}
int fdt_add_reservemap_entry(void *fdt, uint64_t addr, uint64_t size)
{
struct fdt_reserve_entry *re;
int err = check_header_sw(fdt);
int offset;
if (err)
return err;
if (fdt_size_dt_struct(fdt))
return -FDT_ERR_BADSTATE;
offset = fdt_off_dt_struct(fdt);
if ((offset + sizeof(*re)) > fdt_totalsize(fdt))
return -FDT_ERR_NOSPACE;
re = (struct fdt_reserve_entry *)(fdt + offset);
re->address = cpu_to_fdt64(addr);
re->size = cpu_to_fdt64(size);
fdt_set_header(fdt, off_dt_struct, offset + sizeof(*re));
return 0;
}
int fdt_finish_reservemap(void *fdt)
{
return fdt_add_reservemap_entry(fdt, 0, 0);
}
int fdt_begin_node(void *fdt, const char *name)
{
struct fdt_node_header *nh;
int err = check_header_sw(fdt);
int namelen = strlen(name) + 1;
if (err)
return err;
nh = grab_space(fdt, sizeof(*nh) + ALIGN(namelen, FDT_TAGSIZE));
if (! nh)
return -FDT_ERR_NOSPACE;
nh->tag = cpu_to_fdt32(FDT_BEGIN_NODE);
memcpy(nh->name, name, namelen);
return 0;
}
int fdt_end_node(void *fdt)
{
uint32_t *en;
int err = check_header_sw(fdt);
if (err)
return err;
en = grab_space(fdt, FDT_TAGSIZE);
if (! en)
return -FDT_ERR_NOSPACE;
*en = cpu_to_fdt32(FDT_END_NODE);
return 0;
}
static int find_add_string(void *fdt, const char *s)
{
char *strtab = (char *)fdt + fdt_totalsize(fdt);
const char *p;
int strtabsize = fdt_size_dt_strings(fdt);
int len = strlen(s) + 1;
int struct_top, offset;
p = _fdt_find_string(strtab - strtabsize, strtabsize, s);
if (p)
return p - strtab;
/* Add it */
offset = -strtabsize - len;
struct_top = fdt_off_dt_struct(fdt) + fdt_size_dt_struct(fdt);
if (fdt_totalsize(fdt) + offset < struct_top)
return 0; /* no more room :( */
memcpy(strtab + offset, s, len);
fdt_set_header(fdt, size_dt_strings, strtabsize + len);
return offset;
}
int fdt_property(void *fdt, const char *name, const void *val, int len)
{
struct fdt_property *prop;
int err = check_header_sw(fdt);
int nameoff;
if (err)
return err;
nameoff = find_add_string(fdt, name);
if (nameoff == 0)
return -FDT_ERR_NOSPACE;
prop = grab_space(fdt, sizeof(*prop) + ALIGN(len, FDT_TAGSIZE));
if (! prop)
return -FDT_ERR_NOSPACE;
prop->tag = cpu_to_fdt32(FDT_PROP);
prop->nameoff = cpu_to_fdt32(nameoff);
prop->len = cpu_to_fdt32(len);
memcpy(prop->data, val, len);
return 0;
}
int fdt_finish(void *fdt)
{
int err = check_header_sw(fdt);
char *p = (char *)fdt;
uint32_t *end;
int oldstroffset, newstroffset;
uint32_t tag;
int offset, nextoffset;
if (err)
return err;
/* Add terminator */
end = grab_space(fdt, sizeof(*end));
if (! end)
return -FDT_ERR_NOSPACE;
*end = cpu_to_fdt32(FDT_END);
/* Relocate the string table */
oldstroffset = fdt_totalsize(fdt) - fdt_size_dt_strings(fdt);
newstroffset = fdt_off_dt_struct(fdt) + fdt_size_dt_struct(fdt);
memmove(p + newstroffset, p + oldstroffset, fdt_size_dt_strings(fdt));
fdt_set_header(fdt, off_dt_strings, newstroffset);
/* Walk the structure, correcting string offsets */
offset = 0;
while ((tag = _fdt_next_tag(fdt, offset, &nextoffset)) != FDT_END) {
if (tag == FDT_PROP) {
struct fdt_property *prop =
fdt_offset_ptr_w(fdt, offset, sizeof(*prop));
int nameoff;
if (! prop)
return -FDT_ERR_BADSTRUCTURE;
nameoff = fdt32_to_cpu(prop->nameoff);
nameoff += fdt_size_dt_strings(fdt);
prop->nameoff = cpu_to_fdt32(nameoff);
}
offset = nextoffset;
}
/* Finally, adjust the header */
fdt_set_header(fdt, totalsize, newstroffset + fdt_size_dt_strings(fdt));
fdt_set_header(fdt, magic, FDT_MAGIC);
return 0;
}

144
libfdt/fdt_wip.c Normal file
View File

@ -0,0 +1,144 @@
/*
* libfdt - Flat Device Tree manipulation
* Copyright (C) 2006 David Gibson, IBM Corporation.
*
* libfdt is dual licensed: you can use it either under the terms of
* the GPL, or the BSD license, at your option.
*
* a) This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 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 General Public License for more details.
*
* You should have received a copy of the GNU 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
*
* Alternatively,
*
* b) Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* 1. Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer.
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "libfdt_env.h"
#include <fdt.h>
#include <libfdt.h>
#include "libfdt_internal.h"
int fdt_setprop_inplace(void *fdt, int nodeoffset, const char *name,
const void *val, int len)
{
void *propval;
int proplen;
propval = fdt_getprop_w(fdt, nodeoffset, name, &proplen);
if (! propval)
return proplen;
if (proplen != len)
return -FDT_ERR_NOSPACE;
memcpy(propval, val, len);
return 0;
}
static void nop_region(void *start, int len)
{
uint32_t *p;
for (p = start; (void *)p < (start + len); p++)
*p = cpu_to_fdt32(FDT_NOP);
}
int fdt_nop_property(void *fdt, int nodeoffset, const char *name)
{
struct fdt_property *prop;
int len;
prop = fdt_get_property_w(fdt, nodeoffset, name, &len);
if (! prop)
return len;
nop_region(prop, len + sizeof(*prop));
return 0;
}
int _fdt_node_end_offset(void *fdt, int nodeoffset)
{
int level = 0;
uint32_t tag;
int offset, nextoffset;
tag = _fdt_next_tag(fdt, nodeoffset, &nextoffset);
if (tag != FDT_BEGIN_NODE)
return -FDT_ERR_BADOFFSET;
do {
offset = nextoffset;
tag = _fdt_next_tag(fdt, offset, &nextoffset);
switch (tag) {
case FDT_END:
return offset;
case FDT_BEGIN_NODE:
level++;
break;
case FDT_END_NODE:
level--;
break;
case FDT_PROP:
case FDT_NOP:
break;
default:
return -FDT_ERR_BADSTRUCTURE;
}
} while (level >= 0);
return nextoffset;
}
int fdt_nop_node(void *fdt, int nodeoffset)
{
int endoffset;
endoffset = _fdt_node_end_offset(fdt, nodeoffset);
if (endoffset < 0)
return endoffset;
nop_region(fdt_offset_ptr_w(fdt, nodeoffset, 0), endoffset - nodeoffset);
return 0;
}

188
libfdt/libfdt.h Normal file
View File

@ -0,0 +1,188 @@
#ifndef _LIBFDT_H
#define _LIBFDT_H
/*
* libfdt - Flat Device Tree manipulation
* Copyright (C) 2006 David Gibson, IBM Corporation.
*
* libfdt is dual licensed: you can use it either under the terms of
* the GPL, or the BSD license, at your option.
*
* a) This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 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 General Public License for more details.
*
* You should have received a copy of the GNU 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
*
* Alternatively,
*
* b) Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* 1. Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer.
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <fdt.h>
#include <libfdt_env.h>
#define FDT_FIRST_SUPPORTED_VERSION 0x10
#define FDT_LAST_SUPPORTED_VERSION 0x11
/* Error codes: informative error codes */
#define FDT_ERR_NOTFOUND 1
#define FDT_ERR_EXISTS 2
#define FDT_ERR_NOSPACE 3
/* Error codes: codes for bad parameters */
#define FDT_ERR_BADOFFSET 4
#define FDT_ERR_BADPATH 5
#define FDT_ERR_BADSTATE 6
/* Error codes: codes for bad device tree blobs */
#define FDT_ERR_TRUNCATED 7
#define FDT_ERR_BADMAGIC 8
#define FDT_ERR_BADVERSION 9
#define FDT_ERR_BADSTRUCTURE 10
#define FDT_ERR_BADLAYOUT 11
#define FDT_ERR_MAX 11
#define fdt_get_header(fdt, field) \
(fdt32_to_cpu(((const struct fdt_header *)(fdt))->field))
#define fdt_magic(fdt) (fdt_get_header(fdt, magic))
#define fdt_totalsize(fdt) (fdt_get_header(fdt, totalsize))
#define fdt_off_dt_struct(fdt) (fdt_get_header(fdt, off_dt_struct))
#define fdt_off_dt_strings(fdt) (fdt_get_header(fdt, off_dt_strings))
#define fdt_off_mem_rsvmap(fdt) (fdt_get_header(fdt, off_mem_rsvmap))
#define fdt_version(fdt) (fdt_get_header(fdt, version))
#define fdt_last_comp_version(fdt) (fdt_get_header(fdt, last_comp_version))
#define fdt_boot_cpuid_phys(fdt) (fdt_get_header(fdt, boot_cpuid_phys))
#define fdt_size_dt_strings(fdt) (fdt_get_header(fdt, size_dt_strings))
#define fdt_size_dt_struct(fdt) (fdt_get_header(fdt, size_dt_struct))
#define fdt_set_header(fdt, field, val) \
((struct fdt_header *)(fdt))->field = cpu_to_fdt32(val)
const void *fdt_offset_ptr(const void *fdt, int offset, int checklen);
static inline void *fdt_offset_ptr_w(void *fdt, int offset, int checklen)
{
return (void *)fdt_offset_ptr(fdt, offset, checklen);
}
#define fdt_offset_ptr_typed(fdt, offset, var) \
((typeof(var))(fdt_offset_ptr((fdt), (offset), sizeof(*(var)))))
#define fdt_offset_ptr_typed_w(fdt, offset, var) \
((typeof(var))(fdt_offset_ptr_w((fdt), (offset), sizeof(*(var)))))
int fdt_move(const void *fdt, void *buf, int bufsize);
/* Read-only functions */
char *fdt_string(const void *fdt, int stroffset);
int fdt_subnode_offset_namelen(const void *fdt, int parentoffset,
const char *name, int namelen);
int fdt_subnode_offset(const void *fdt, int parentoffset, const char *name);
int fdt_path_offset(const void *fdt, const char *path);
const struct fdt_property *fdt_get_property(const void *fdt, int nodeoffset,
const char *name, int *lenp);
static inline struct fdt_property *fdt_get_property_w(void *fdt, int nodeoffset,
const char *name,
int *lenp)
{
return (struct fdt_property *)fdt_get_property(fdt, nodeoffset,
name, lenp);
}
const void *fdt_getprop(const void *fdt, int nodeoffset,
const char *name, int *lenp);
static inline void *fdt_getprop_w(void *fdt, int nodeoffset,
const char *name, int *lenp)
{
return (void *)fdt_getprop(fdt, nodeoffset, name, lenp);
}
/* Write-in-place functions */
int fdt_setprop_inplace(void *fdt, int nodeoffset, const char *name,
const void *val, int len);
#define fdt_setprop_inplace_typed(fdt, nodeoffset, name, val) \
({ \
typeof(val) x = val; \
fdt_setprop_inplace(fdt, nodeoffset, name, &x, sizeof(x)); \
})
int fdt_nop_property(void *fdt, int nodeoffset, const char *name);
int fdt_nop_node(void *fdt, int nodeoffset);
/* Sequential-write functions */
int fdt_create(void *buf, int bufsize);
int fdt_add_reservemap_entry(void *fdt, uint64_t addr, uint64_t size);
int fdt_finish_reservemap(void *fdt);
int fdt_begin_node(void *fdt, const char *name);
int fdt_property(void *fdt, const char *name, const void *val, int len);
#define fdt_property_typed(fdt, name, val) \
({ \
typeof(val) x = (val); \
fdt_property((fdt), (name), &x, sizeof(x)); \
})
#define fdt_property_string(fdt, name, str) \
fdt_property(fdt, name, str, strlen(str)+1)
int fdt_end_node(void *fdt);
int fdt_finish(void *fdt);
/* Read-write functions */
int fdt_open_into(void *fdt, void *buf, int bufsize);
int fdt_pack(void *fdt);
int fdt_setprop(void *fdt, int nodeoffset, const char *name,
const void *val, int len);
#define fdt_setprop_typed(fdt, nodeoffset, name, val) \
({ \
typeof(val) x = (val); \
fdt_setprop((fdt), (nodeoffset), (name), &x, sizeof(x)); \
})
#define fdt_setprop_string(fdt, nodeoffset, name, str) \
fdt_setprop((fdt), (nodeoffset), (name), (str), strlen(str)+1)
int fdt_delprop(void *fdt, int nodeoffset, const char *name);
int fdt_add_subnode_namelen(void *fdt, int parentoffset,
const char *name, int namelen);
int fdt_add_subnode(void *fdt, int parentoffset, const char *name);
int fdt_del_node(void *fdt, int nodeoffset);
/* Extra functions */
const char *fdt_strerror(int errval);
#endif /* _LIBFDT_H */

22
libfdt/libfdt_env.h Normal file
View File

@ -0,0 +1,22 @@
#ifndef _LIBFDT_ENV_H
#define _LIBFDT_ENV_H
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <endian.h>
#include <byteswap.h>
#if __BYTE_ORDER == __BIG_ENDIAN
#define fdt32_to_cpu(x) (x)
#define cpu_to_fdt32(x) (x)
#define fdt64_to_cpu(x) (x)
#define cpu_to_fdt64(x) (x)
#else
#define fdt32_to_cpu(x) (bswap_32((x)))
#define cpu_to_fdt32(x) (bswap_32((x)))
#define fdt64_to_cpu(x) (bswap_64((x)))
#define cpu_to_fdt64(x) (bswap_64((x)))
#endif
#endif /* _LIBFDT_ENV_H */

78
libfdt/libfdt_internal.h Normal file
View File

@ -0,0 +1,78 @@
#ifndef _LIBFDT_INTERNAL_H
#define _LIBFDT_INTERNAL_H
/*
* libfdt - Flat Device Tree manipulation
* Copyright (C) 2006 David Gibson, IBM Corporation.
*
* libfdt is dual licensed: you can use it either under the terms of
* the GPL, or the BSD license, at your option.
*
* a) This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 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 General Public License for more details.
*
* You should have received a copy of the GNU 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
*
* Alternatively,
*
* b) Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* 1. Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer.
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <fdt.h>
#define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
#define PALIGN(p, a) ((void *)ALIGN((unsigned long)(p), (a)))
#define memeq(p, q, n) (memcmp((p), (q), (n)) == 0)
#define streq(p, q) (strcmp((p), (q)) == 0)
int _fdt_check_header(const void *fdt);
uint32_t _fdt_next_tag(const void *fdt, int startoffset, int *nextoffset);
const char *_fdt_find_string(const char *strtab, int tabsize, const char *s);
int _fdt_node_end_offset(void *fdt, int nodeoffset);
static inline const void *_fdt_offset_ptr(const void *fdt, int offset)
{
return fdt + fdt_off_dt_struct(fdt) + offset;
}
static inline void *_fdt_offset_ptr_w(void *fdt, int offset)
{
return (void *)_fdt_offset_ptr(fdt, offset);
}
#define SW_MAGIC (~FDT_MAGIC)
#endif /* _LIBFDT_INTERNAL_H */

1
libfdt/tests/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
*.dtb

62
libfdt/tests/Makefile Normal file
View File

@ -0,0 +1,62 @@
PREFIX = /usr/local
LIB_TESTS = root_node find_property subnode_offset path_offset getprop \
notfound \
setprop_inplace nop_property nop_node \
sw_tree1 \
move_and_save \
open_pack rw_tree1 setprop del_property del_node
LIBTREE_TESTS = truncated_property
TESTS = $(LIB_TESTS) $(LIBTREE_TESTS)
UTILS = dumptrees
TREES = test_tree1.dtb
CFLAGS = -Wall -g
CPPFLAGS = -I..
LDFLAGS = -L..
LIBFDT = ../libfdt.a
ifdef V
VECHO = :
else
VECHO = echo " "
.SILENT:
endif
DEPFILES = $(TESTS:%=%.d) testutils.d
all: $(TESTS) $(TREES)
%.o: %.c
@$(VECHO) CC $@
$(CC) $(CPPFLAGS) $(CFLAGS) -o $@ -c $<
%.o: %.S
@$(VECHO) AS $@
$(CC) -D__ASSEMBLY__ $(CPPFLAGS) -o $@ -c $<
%: %.o
@$(VECHO) LD $@
$(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS)
$(LIB_TESTS): %: testutils.o $(LIBFDT)
$(LIBTREE_TESTS): %: testutils.o trees.o $(LIBFDT)
dumptrees: %: trees.o
$(TREES): dumptrees
@$(VECHO) DUMPTREES
./dumptrees >/dev/null
clean:
@$(VECHO) CLEAN "(tests)"
rm -f *~ *.o *.so *.a *.d *.s core a.out
rm -f $(TESTS) $(UTILS) *.dtb
%.d: %.c
@$(CC) $(CPPFLAGS) -MM -MT "$*.o $@" $< > $@
-include $(DEPFILES)

122
libfdt/tests/del_node.c Normal file
View File

@ -0,0 +1,122 @@
/*
* libfdt - Flat Device Tree manipulation
* Testcase for fdt_nop_node()
* 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
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdint.h>
#include <fdt.h>
#include <libfdt.h>
#include "tests.h"
#include "testdata.h"
int main(int argc, char *argv[])
{
void *fdt;
int subnode1_offset, subnode2_offset, subsubnode2_offset;
int err;
int oldsize, delsize, newsize;
test_init(argc, argv);
fdt = load_blob_arg(argc, argv);
oldsize = fdt_totalsize(fdt);
subnode1_offset = fdt_path_offset(fdt, "/subnode1");
if (subnode1_offset < 0)
FAIL("Couldn't find \"/subnode1\": %s",
fdt_strerror(subnode1_offset));
check_getprop_typed(fdt, subnode1_offset, "prop-int", TEST_VALUE_1);
subnode2_offset = fdt_path_offset(fdt, "/subnode2");
if (subnode2_offset < 0)
FAIL("Couldn't find \"/subnode2\": %s",
fdt_strerror(subnode2_offset));
check_getprop_typed(fdt, subnode2_offset, "prop-int", TEST_VALUE_2);
subsubnode2_offset = fdt_path_offset(fdt, "/subnode2/subsubnode");
if (subsubnode2_offset < 0)
FAIL("Couldn't find \"/subnode2/subsubnode\": %s",
fdt_strerror(subsubnode2_offset));
check_getprop_typed(fdt, subsubnode2_offset, "prop-int", TEST_VALUE_2);
err = fdt_del_node(fdt, subnode1_offset);
if (err)
FAIL("fdt_del_node(subnode1): %s", fdt_strerror(err));
subnode1_offset = fdt_path_offset(fdt, "/subnode1");
if (subnode1_offset != -FDT_ERR_NOTFOUND)
FAIL("fdt_path_offset(subnode1) returned \"%s\" instead of \"%s\"",
fdt_strerror(subnode1_offset),
fdt_strerror(-FDT_ERR_NOTFOUND));
subnode2_offset = fdt_path_offset(fdt, "/subnode2");
if (subnode2_offset < 0)
FAIL("Couldn't find \"/subnode2\": %s",
fdt_strerror(subnode2_offset));
check_getprop_typed(fdt, subnode2_offset, "prop-int", TEST_VALUE_2);
subsubnode2_offset = fdt_path_offset(fdt, "/subnode2/subsubnode");
if (subsubnode2_offset < 0)
FAIL("Couldn't find \"/subnode2/subsubnode\": %s",
fdt_strerror(subsubnode2_offset));
check_getprop_typed(fdt, subsubnode2_offset, "prop-int", TEST_VALUE_2);
err = fdt_del_node(fdt, subnode2_offset);
if (err)
FAIL("fdt_del_node(subnode2): %s", fdt_strerror(err));
subnode1_offset = fdt_path_offset(fdt, "/subnode1");
if (subnode1_offset != -FDT_ERR_NOTFOUND)
FAIL("fdt_path_offset(subnode1) returned \"%s\" instead of \"%s\"",
fdt_strerror(subnode1_offset),
fdt_strerror(-FDT_ERR_NOTFOUND));
subnode2_offset = fdt_path_offset(fdt, "/subnode2");
if (subnode2_offset != -FDT_ERR_NOTFOUND)
FAIL("fdt_path_offset(subnode2) returned \"%s\" instead of \"%s\"",
fdt_strerror(subnode2_offset),
fdt_strerror(-FDT_ERR_NOTFOUND));
subsubnode2_offset = fdt_path_offset(fdt, "/subnode2/subsubnode");
if (subsubnode2_offset != -FDT_ERR_NOTFOUND)
FAIL("fdt_path_offset(subsubnode2) returned \"%s\" instead of \"%s\"",
fdt_strerror(subsubnode2_offset),
fdt_strerror(-FDT_ERR_NOTFOUND));
delsize = fdt_totalsize(fdt);
err = fdt_pack(fdt);
if (err)
FAIL("fdt_pack(): %s", fdt_strerror(err));
newsize = fdt_totalsize(fdt);
verbose_printf("oldsize = %d, delsize = %d, newsize = %d\n",
oldsize, delsize, newsize);
if (newsize >= oldsize)
FAIL("Tree failed to shrink after deletions");
PASS();
}

View File

@ -0,0 +1,89 @@
/*
* libfdt - Flat Device Tree manipulation
* Testcase for fdt_delprop()
* 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
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdint.h>
#include <fdt.h>
#include <libfdt.h>
#include "tests.h"
#include "testdata.h"
int main(int argc, char *argv[])
{
void *fdt;
const uint32_t *intp;
const char *strp;
int err, lenerr;
int oldsize, delsize, newsize;
test_init(argc, argv);
fdt = load_blob_arg(argc, argv);
oldsize = fdt_totalsize(fdt);
intp = check_getprop_typed(fdt, 0, "prop-int", TEST_VALUE_1);
verbose_printf("int value was 0x%08x\n", *intp);
err = fdt_delprop(fdt, 0, "prop-int");
if (err)
FAIL("Failed to delete \"prop-int\": %s", fdt_strerror(err));
intp = fdt_getprop(fdt, 0, "prop-int", &lenerr);
if (intp)
FAIL("prop-int still present after deletion");
if (lenerr != -FDT_ERR_NOTFOUND)
FAIL("Unexpected error on second getprop: %s",
fdt_strerror(lenerr));
strp = check_getprop(fdt, 0, "prop-str", strlen(TEST_STRING_1)+1,
TEST_STRING_1);
verbose_printf("string value was \"%s\"\n", strp);
err = fdt_delprop(fdt, 0, "prop-str");
if (err)
FAIL("Failed to delete \"prop-str\": %s", fdt_strerror(err));
strp = fdt_getprop(fdt, 0, "prop-str", &lenerr);
if (strp)
FAIL("prop-str still present after deletion");
if (lenerr != -FDT_ERR_NOTFOUND)
FAIL("Unexpected error on second getprop: %s",
fdt_strerror(lenerr));
delsize = fdt_totalsize(fdt);
err = fdt_pack(fdt);
if (err)
FAIL("fdt_pack(): %s\n", fdt_strerror(err));
newsize = fdt_totalsize(fdt);
verbose_printf("oldsize = %d, delsize = %d, newsize = %d\n",
oldsize, delsize, newsize);
if (newsize >= oldsize)
FAIL("Tree failed to shrink after deletions");
PASS();
}

49
libfdt/tests/dumptrees.c Normal file
View File

@ -0,0 +1,49 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdint.h>
#include <fdt.h>
#include <libfdt.h>
#include <libfdt_env.h>
#include "testdata.h"
struct {
void *blob;
const char *filename;
} trees[] = {
#define TREE(name) { &_##name, #name ".dtb" }
TREE(test_tree1),
};
#define NUM_TREES (sizeof(trees) / sizeof(trees[0]))
int main(int argc, char *argv[])
{
int i;
for (i = 0; i < NUM_TREES; i++) {
void *blob = trees[i].blob;
const char *filename = trees[i].filename;
int size;
int fd;
int ret;
size = fdt_totalsize(blob);
printf("Tree \"%s\", %d bytes\n", filename, size);
fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
if (fd < 0)
perror("open()");
ret = write(fd, blob, size);
if (ret != size)
perror("write()");
close(fd);
}
exit(0);
}

View File

@ -0,0 +1,42 @@
/*
* libfdt - Flat Device Tree manipulation
* Testcase for fdt_property_offset()
* 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
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <fdt.h>
#include <libfdt.h>
#include "tests.h"
#include "testdata.h"
int main(int argc, char *argv[])
{
void *fdt;
test_init(argc, argv);
fdt = load_blob_arg(argc, argv);
check_property_typed(fdt, 0, "prop-int", TEST_VALUE_1);
check_property(fdt, 0, "prop-str", strlen(TEST_STRING_1)+1, TEST_STRING_1);
PASS();
}

43
libfdt/tests/getprop.c Normal file
View File

@ -0,0 +1,43 @@
/*
* libfdt - Flat Device Tree manipulation
* Testcase for fdt_getprop()
* 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
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <fdt.h>
#include <libfdt.h>
#include "tests.h"
#include "testdata.h"
int main(int argc, char *argv[])
{
void *fdt;
test_init(argc, argv);
fdt = load_blob_arg(argc, argv);
check_getprop_typed(fdt, 0, "prop-int", TEST_VALUE_1);
check_getprop(fdt, 0, "prop-str", strlen(TEST_STRING_1)+1, TEST_STRING_1);
PASS();
}

View File

@ -0,0 +1,76 @@
/*
* libfdt - Flat Device Tree manipulation
* Basic testcase for read-only access
* 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
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <stdint.h>
#include <fdt.h>
#include <libfdt.h>
#include "tests.h"
#include "testdata.h"
int main(int argc, char *argv[])
{
void *fdt, *fdt1, *fdt2, *fdt3;
void *buf;
int shuntsize;
int bufsize;
int err;
const char *inname;
char outname[PATH_MAX];
test_init(argc, argv);
fdt = load_blob_arg(argc, argv);
inname = argv[1];
shuntsize = ALIGN(fdt_totalsize(fdt) / 2, sizeof(uint64_t));
bufsize = fdt_totalsize(fdt) + shuntsize;
buf = xmalloc(bufsize);
fdt1 = buf;
err = fdt_move(fdt, fdt1, bufsize);
if (err)
FAIL("Failed to move tree into new buffer: %s",
fdt_strerror(err));
sprintf(outname, "moved.%s", inname);
save_blob(outname, fdt1);
fdt2 = buf + shuntsize;
err = fdt_move(fdt1, fdt2, bufsize-shuntsize);
if (err)
FAIL("Failed to shunt tree %d bytes: %s",
shuntsize, fdt_strerror(err));
sprintf(outname, "shunted.%s", inname);
save_blob(outname, fdt2);
fdt3 = buf;
err = fdt_move(fdt2, fdt3, bufsize);
if (err)
FAIL("Failed to deshunt tree %d bytes: %s",
shuntsize, fdt_strerror(err));
sprintf(outname, "deshunted.%s", inname);
save_blob(outname, fdt3);
PASS();
}

105
libfdt/tests/nop_node.c Normal file
View File

@ -0,0 +1,105 @@
/*
* libfdt - Flat Device Tree manipulation
* Testcase for fdt_nop_node()
* 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
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdint.h>
#include <fdt.h>
#include <libfdt.h>
#include "tests.h"
#include "testdata.h"
int main(int argc, char *argv[])
{
void *fdt;
int subnode1_offset, subnode2_offset, subsubnode2_offset;
int err;
test_init(argc, argv);
fdt = load_blob_arg(argc, argv);
subnode1_offset = fdt_path_offset(fdt, "/subnode1");
if (subnode1_offset < 0)
FAIL("Couldn't find \"/subnode1\": %s",
fdt_strerror(subnode1_offset));
check_getprop_typed(fdt, subnode1_offset, "prop-int", TEST_VALUE_1);
subnode2_offset = fdt_path_offset(fdt, "/subnode2");
if (subnode2_offset < 0)
FAIL("Couldn't find \"/subnode2\": %s",
fdt_strerror(subnode2_offset));
check_getprop_typed(fdt, subnode2_offset, "prop-int", TEST_VALUE_2);
subsubnode2_offset = fdt_path_offset(fdt, "/subnode2/subsubnode");
if (subsubnode2_offset < 0)
FAIL("Couldn't find \"/subnode2/subsubnode\": %s",
fdt_strerror(subsubnode2_offset));
check_getprop_typed(fdt, subsubnode2_offset, "prop-int", TEST_VALUE_2);
err = fdt_nop_node(fdt, subnode1_offset);
if (err)
FAIL("fdt_nop_node(subnode1): %s", fdt_strerror(err));
subnode1_offset = fdt_path_offset(fdt, "/subnode1");
if (subnode1_offset != -FDT_ERR_NOTFOUND)
FAIL("fdt_path_offset(subnode1) returned \"%s\" instead of \"%s\"",
fdt_strerror(subnode1_offset),
fdt_strerror(-FDT_ERR_NOTFOUND));
subnode2_offset = fdt_path_offset(fdt, "/subnode2");
if (subnode2_offset < 0)
FAIL("Couldn't find \"/subnode2\": %s",
fdt_strerror(subnode2_offset));
check_getprop_typed(fdt, subnode2_offset, "prop-int", TEST_VALUE_2);
subsubnode2_offset = fdt_path_offset(fdt, "/subnode2/subsubnode");
if (subsubnode2_offset < 0)
FAIL("Couldn't find \"/subnode2/subsubnode\": %s",
fdt_strerror(subsubnode2_offset));
check_getprop_typed(fdt, subsubnode2_offset, "prop-int", TEST_VALUE_2);
err = fdt_nop_node(fdt, subnode2_offset);
if (err)
FAIL("fdt_nop_node(subnode2): %s", fdt_strerror(err));
subnode1_offset = fdt_path_offset(fdt, "/subnode1");
if (subnode1_offset != -FDT_ERR_NOTFOUND)
FAIL("fdt_path_offset(subnode1) returned \"%s\" instead of \"%s\"",
fdt_strerror(subnode1_offset),
fdt_strerror(-FDT_ERR_NOTFOUND));
subnode2_offset = fdt_path_offset(fdt, "/subnode2");
if (subnode2_offset != -FDT_ERR_NOTFOUND)
FAIL("fdt_path_offset(subnode2) returned \"%s\" instead of \"%s\"",
fdt_strerror(subnode2_offset),
fdt_strerror(-FDT_ERR_NOTFOUND));
subsubnode2_offset = fdt_path_offset(fdt, "/subnode2/subsubnode");
if (subsubnode2_offset != -FDT_ERR_NOTFOUND)
FAIL("fdt_path_offset(subsubnode2) returned \"%s\" instead of \"%s\"",
fdt_strerror(subsubnode2_offset),
fdt_strerror(-FDT_ERR_NOTFOUND));
PASS();
}

View File

@ -0,0 +1,71 @@
/*
* libfdt - Flat Device Tree manipulation
* Testcase for fdt_nop_property()
* 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
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdint.h>
#include <fdt.h>
#include <libfdt.h>
#include "tests.h"
#include "testdata.h"
int main(int argc, char *argv[])
{
void *fdt;
const uint32_t *intp;
const char *strp;
int err;
int lenerr;
test_init(argc, argv);
fdt = load_blob_arg(argc, argv);
intp = check_getprop_typed(fdt, 0, "prop-int", TEST_VALUE_1);
verbose_printf("int value was 0x%08x\n", *intp);
err = fdt_nop_property(fdt, 0, "prop-int");
if (err)
FAIL("Failed to nop \"prop-int\": %s", fdt_strerror(err));
intp = fdt_getprop(fdt, 0, "prop-int", &lenerr);
if (intp)
FAIL("prop-int still present after nopping");
if (lenerr != -FDT_ERR_NOTFOUND)
FAIL("Unexpected error on second getprop: %s", fdt_strerror(err));
strp = check_getprop(fdt, 0, "prop-str", strlen(TEST_STRING_1)+1,
TEST_STRING_1);
verbose_printf("string value was \"%s\"\n", strp);
err = fdt_nop_property(fdt, 0, "prop-str");
if (err)
FAIL("Failed to nop \"prop-str\": %s", fdt_strerror(err));
strp = fdt_getprop(fdt, 0, "prop-str", &lenerr);
if (strp)
FAIL("prop-str still present after nopping");
if (lenerr != -FDT_ERR_NOTFOUND)
FAIL("Unexpected error on second getprop: %s", fdt_strerror(err));
PASS();
}

73
libfdt/tests/notfound.c Normal file
View File

@ -0,0 +1,73 @@
/*
* libfdt - Flat Device Tree manipulation
* Testcase for behaviour on searching for a non-existent node
* 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
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <fdt.h>
#include <libfdt.h>
#include "tests.h"
#include "testdata.h"
void check_error(const char *s, int err)
{
if (err != -FDT_ERR_NOTFOUND)
FAIL("%s return error %s instead of -FDT_ERR_NOTFOUND", s,
fdt_strerror(err));
}
int main(int argc, char *argv[])
{
const struct fdt_property *prop;
void *fdt;
int offset;
int subnode1_offset;
const void *val;
int lenerr;
test_init(argc, argv);
fdt = load_blob_arg(argc, argv);
prop = fdt_get_property(fdt, 0, "nonexistant-property", &lenerr);
check_error("fdt_get_property(\"nonexistant-property\")", lenerr);
val = fdt_getprop(fdt, 0, "nonexistant-property", &lenerr);
check_error("fdt_getprop(\"nonexistant-property\"", lenerr);
subnode1_offset = fdt_subnode_offset(fdt, 0, "subnode1");
if (subnode1_offset < 0)
FAIL("Couldn't find subnode1: %s", fdt_strerror(subnode1_offset));
val = fdt_getprop(fdt, subnode1_offset, "prop-str", &lenerr);
check_error("fdt_getprop(\"prop-str\")", lenerr);
offset = fdt_subnode_offset(fdt, 0, "nonexistant-subnode");
check_error("fdt_subnode_offset(\"nonexistant-subnode\")", offset);
offset = fdt_subnode_offset(fdt, 0, "subsubnode");
check_error("fdt_subnode_offset(\"subsubnode\")", offset);
offset = fdt_path_offset(fdt, "/nonexistant-subnode");
check_error("fdt_path_offset(\"/nonexistant-subnode\")", offset);
PASS();
}

70
libfdt/tests/open_pack.c Normal file
View File

@ -0,0 +1,70 @@
/*
* libfdt - Flat Device Tree manipulation
* Basic testcase for read-only access
* 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
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <stdint.h>
#include <fdt.h>
#include <libfdt.h>
#include "tests.h"
#include "testdata.h"
int main(int argc, char *argv[])
{
void *fdt, *fdt1;
void *buf;
int oldsize, bufsize, packsize;
int err;
const char *inname;
char outname[PATH_MAX];
test_init(argc, argv);
fdt = load_blob_arg(argc, argv);
inname = argv[1];
oldsize = fdt_totalsize(fdt);
bufsize = oldsize * 2;
buf = xmalloc(bufsize);
fdt1 = buf;
err = fdt_open_into(fdt, fdt1, bufsize);
if (err)
FAIL("fdt_open_into(): %s", fdt_strerror(err));
sprintf(outname, "opened.%s", inname);
save_blob(outname, fdt1);
err = fdt_pack(fdt1);
if (err)
FAIL("fdt_pack(): %s", fdt_strerror(err));
sprintf(outname, "repacked.%s", inname);
save_blob(outname, fdt1);
packsize = fdt_totalsize(fdt1);
verbose_printf("oldsize = %d, bufsize = %d, packsize = %d\n",
oldsize, bufsize, packsize);
PASS();
}

View File

@ -0,0 +1,98 @@
/*
* libfdt - Flat Device Tree manipulation
* Testcase for fdt_path_offset()
* 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
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <fdt.h>
#include <libfdt.h>
#include "tests.h"
#include "testdata.h"
int check_subnode(void *fdt, int parent, const char *name)
{
int offset;
struct fdt_node_header *nh;
uint32_t tag;
verbose_printf("Checking subnode \"%s\" of %d...", name, parent);
offset = fdt_subnode_offset(fdt, parent, name);
verbose_printf("offset %d...", offset);
if (offset < 0)
FAIL("fdt_subnode_offset(\"%s\"): %s", name, fdt_strerror(offset));
nh = fdt_offset_ptr_typed(fdt, offset, nh);
verbose_printf("pointer %p\n", nh);
if (! nh)
FAIL("NULL retrieving subnode \"%s\"", name);
tag = fdt32_to_cpu(nh->tag);
if (tag != FDT_BEGIN_NODE)
FAIL("Incorrect tag 0x%08x on property \"%s\"", tag, name);
if (!streq(nh->name, name))
FAIL("Subnode name mismatch \"%s\" instead of \"%s\"",
nh->name, name);
return offset;
}
int main(int argc, char *argv[])
{
void *fdt;
int subnode1_offset, subnode2_offset;
int subnode1_offset_p, subnode2_offset_p;
int subsubnode1_offset, subsubnode2_offset;
int subsubnode1_offset_p, subsubnode2_offset_p;
test_init(argc, argv);
fdt = load_blob_arg(argc, argv);
subnode1_offset = check_subnode(fdt, 0, "subnode1");
subnode2_offset = check_subnode(fdt, 0, "subnode2");
subnode1_offset_p = fdt_path_offset(fdt, "/subnode1");
subnode2_offset_p = fdt_path_offset(fdt, "/subnode2");
if (subnode1_offset != subnode1_offset_p)
FAIL("Mismatch between subnode_offset (%d) and path_offset (%d)",
subnode1_offset, subnode1_offset_p);
if (subnode2_offset != subnode2_offset_p)
FAIL("Mismatch between subnode_offset (%d) and path_offset (%d)",
subnode2_offset, subnode2_offset_p);
subsubnode1_offset = check_subnode(fdt, subnode1_offset, "subsubnode");
subsubnode2_offset = check_subnode(fdt, subnode2_offset, "subsubnode");
subsubnode1_offset_p = fdt_path_offset(fdt, "/subnode1/subsubnode");
subsubnode2_offset_p = fdt_path_offset(fdt, "/subnode2/subsubnode");
if (subsubnode1_offset != subsubnode1_offset_p)
FAIL("Mismatch between subnode_offset (%d) and path_offset (%d)",
subsubnode1_offset, subsubnode1_offset_p);
if (subsubnode2_offset != subsubnode2_offset_p)
FAIL("Mismatch between subnode_offset (%d) and path_offset (%d)",
subsubnode2_offset, subsubnode2_offset_p);
PASS();
}

53
libfdt/tests/root_node.c Normal file
View File

@ -0,0 +1,53 @@
/*
* libfdt - Flat Device Tree manipulation
* Basic testcase for read-only access
* 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
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <fdt.h>
#include <libfdt.h>
#include "tests.h"
#include "testdata.h"
int main(int argc, char *argv[])
{
void *fdt;
struct fdt_node_header *nh;
test_init(argc, argv);
fdt = load_blob_arg(argc, argv);
nh = fdt_offset_ptr_typed(fdt, 0, nh);
if (! nh)
FAIL("NULL retrieving root node");
if (fdt32_to_cpu(nh->tag) != FDT_BEGIN_NODE)
FAIL("Wrong tag on root node");
if (strlen(nh->name) != 0)
FAIL("Wrong name for root node, \"%s\" instead of empty",
nh->name);
PASS();
}

94
libfdt/tests/run_tests.sh Executable file
View File

@ -0,0 +1,94 @@
#! /bin/bash
export QUIET_TEST=1
ENV=/usr/bin/env
run_test () {
echo -n "$@: "
PATH=".:$PATH" $ENV "$@"
}
tree1_tests () {
TREE=$1
# Read-only tests
run_test root_node $TREE
run_test find_property $TREE
run_test subnode_offset $TREE
run_test path_offset $TREE
run_test getprop $TREE
run_test notfound $TREE
# Write-in-place tests
run_test setprop_inplace $TREE
run_test nop_property $TREE
run_test nop_node $TREE
}
functional_tests () {
# Make sure we don't have stale blobs lying around
rm -f *.test.dtb
tree1_tests test_tree1.dtb
# Sequential write tests
run_test sw_tree1
tree1_tests sw_tree1.test.dtb
tree1_tests unfinished_tree1.test.dtb
# fdt_move tests
for tree in test_tree1.dtb sw_tree1.test.dtb unfinished_tree1.test.dtb; do
rm -f moved.$tree shunted.$tree deshunted.$tree
run_test move_and_save $tree
tree1_tests moved.$tree
tree1_tests shunted.$tree
tree1_tests deshunted.$tree
done
# Read-write tests
for tree in test_tree1.dtb sw_tree1.test.dtb; do
rm -f opened.$tree repacked.$tree
run_test open_pack $tree
tree1_tests opened.$tree
tree1_tests repacked.$tree
done
run_test setprop test_tree1.dtb
run_test del_property test_tree1.dtb
run_test del_node test_tree1.dtb
run_test rw_tree1
tree1_tests rw_tree1.test.dtb
# Tests for behaviour on various sorts of corrupted trees
run_test truncated_property
}
stress_tests () {
ITERATIONS=10 # Number of iterations for looping tests
}
while getopts "vdt:" ARG ; do
case $ARG in
"v")
unset QUIET_TEST
;;
"t")
TESTSETS=$OPTARG
;;
esac
done
if [ -z "$TESTSETS" ]; then
TESTSETS="func stress"
fi
for set in $TESTSETS; do
case $set in
"func")
functional_tests
;;
"stress")
stress_tests
;;
esac
done

91
libfdt/tests/rw_tree1.c Normal file
View File

@ -0,0 +1,91 @@
/*
* libfdt - Flat Device Tree manipulation
* Testcase for fdt_nop_node()
* 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
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdint.h>
#include <fdt.h>
#include <libfdt.h>
#include "tests.h"
#include "testdata.h"
#define SPACE 65536
#define CHECK(code) \
{ \
err = (code); \
if (err) \
FAIL(#code ": %s", fdt_strerror(err)); \
}
#define OFF_CHECK(off, code) \
{ \
(off) = (code); \
if (off < 0) \
FAIL(#code ": %s", fdt_strerror(off)); \
}
int main(int argc, char *argv[])
{
void *fdt;
int err;
int offset;
test_init(argc, argv);
fdt = xmalloc(SPACE);
/* First create empty tree with SW */
CHECK(fdt_create(fdt, SPACE));
CHECK(fdt_finish_reservemap(fdt));
CHECK(fdt_begin_node(fdt, ""));
CHECK(fdt_end_node(fdt));
CHECK(fdt_finish(fdt));
verbose_printf("Built empty tree, totalsize = %d\n",
fdt_totalsize(fdt));
CHECK(fdt_open_into(fdt, fdt, SPACE));
CHECK(fdt_setprop_typed(fdt, 0, "prop-int", TEST_VALUE_1));
CHECK(fdt_setprop_string(fdt, 0, "prop-str", TEST_STRING_1));
OFF_CHECK(offset, fdt_add_subnode(fdt, 0, "subnode1"));
CHECK(fdt_setprop_typed(fdt, offset, "prop-int", TEST_VALUE_1));
OFF_CHECK(offset, fdt_add_subnode(fdt, offset, "subsubnode"));
CHECK(fdt_setprop_typed(fdt, offset, "prop-int", TEST_VALUE_1));
OFF_CHECK(offset, fdt_add_subnode(fdt, 0, "subnode2"));
CHECK(fdt_setprop_typed(fdt, offset, "prop-int", TEST_VALUE_2));
OFF_CHECK(offset, fdt_add_subnode(fdt, offset, "subsubnode"));
CHECK(fdt_setprop_typed(fdt, offset, "prop-int", TEST_VALUE_2));
CHECK(fdt_pack(fdt));
save_blob("rw_tree1.test.dtb", fdt);
PASS();
}

78
libfdt/tests/setprop.c Normal file
View File

@ -0,0 +1,78 @@
/*
* libfdt - Flat Device Tree manipulation
* Testcase for fdt_setprop()
* 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
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdint.h>
#include <fdt.h>
#include <libfdt.h>
#include "tests.h"
#include "testdata.h"
#define SPACE 65536
#define NEW_STRING "here is quite a long test string, blah blah blah"
int main(int argc, char *argv[])
{
void *fdt;
void *buf;
const uint32_t *intp;
const char *strp;
int err;
test_init(argc, argv);
fdt = load_blob_arg(argc, argv);
buf = xmalloc(SPACE);
err = fdt_open_into(fdt, buf, SPACE);
if (err)
FAIL("fdt_open_into(): %s", fdt_strerror(err));
fdt = buf;
intp = check_getprop_typed(fdt, 0, "prop-int", TEST_VALUE_1);
verbose_printf("Old int value was 0x%08x\n", *intp);
err = fdt_setprop_string(fdt, 0, "prop-int", NEW_STRING);
if (err)
FAIL("Failed to set \"prop-int\" to \"%s\": %s",
NEW_STRING, fdt_strerror(err));
strp = check_getprop_string(fdt, 0, "prop-int", NEW_STRING);
verbose_printf("New value is \"%s\"\n", strp);
strp = check_getprop(fdt, 0, "prop-str", strlen(TEST_STRING_1)+1,
TEST_STRING_1);
verbose_printf("Old string value was \"%s\"\n", strp);
err = fdt_setprop(fdt, 0, "prop-str", NULL, 0);
if (err)
FAIL("Failed to empty \"prop-str\": %s",
fdt_strerror(err));
check_getprop(fdt, 0, "prop-str", 0, NULL);
PASS();
}

View File

@ -0,0 +1,72 @@
/*
* libfdt - Flat Device Tree manipulation
* Testcase for fdt_setprop_inplace()
* 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
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdint.h>
#include <fdt.h>
#include <libfdt.h>
#include "tests.h"
#include "testdata.h"
int main(int argc, char *argv[])
{
void *fdt;
const uint32_t *intp;
const char *strp;
char *xstr;
int xlen, i;
int err;
test_init(argc, argv);
fdt = load_blob_arg(argc, argv);
intp = check_getprop_typed(fdt, 0, "prop-int", TEST_VALUE_1);
verbose_printf("Old int value was 0x%08x\n", *intp);
err = fdt_setprop_inplace_typed(fdt, 0, "prop-int", ~TEST_VALUE_1);
if (err)
FAIL("Failed to set \"prop-int\" to 0x08%x: %s",
~TEST_VALUE_1, fdt_strerror(err));
intp = check_getprop_typed(fdt, 0, "prop-int", ~TEST_VALUE_1);
verbose_printf("New int value is 0x%08x\n", *intp);
strp = check_getprop(fdt, 0, "prop-str", strlen(TEST_STRING_1)+1,
TEST_STRING_1);
verbose_printf("Old string value was \"%s\"\n", strp);
xstr = strdup(strp);
xlen = strlen(xstr);
for (i = 0; i < xlen; i++)
xstr[i] = toupper(xstr[i]);
err = fdt_setprop_inplace(fdt, 0, "prop-str", xstr, xlen+1);
if (err)
FAIL("Failed to set \"prop-str\" to \"%s\": %s",
xstr, fdt_strerror(err));
strp = check_getprop(fdt, 0, "prop-str", xlen+1, xstr);
verbose_printf("New string value is \"%s\"\n", strp);
PASS();
}

View File

@ -0,0 +1,83 @@
/*
* libfdt - Flat Device Tree manipulation
* Testcase for fdt_subnode_offset()
* 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
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <fdt.h>
#include <libfdt.h>
#include "tests.h"
#include "testdata.h"
int check_subnode(struct fdt_header *fdt, int parent, const char *name)
{
int offset;
struct fdt_node_header *nh;
uint32_t tag;
verbose_printf("Checking subnode \"%s\" of %d...", name, parent);
offset = fdt_subnode_offset(fdt, parent, name);
verbose_printf("offset %d...", offset);
if (offset < 0)
FAIL("fdt_subnode_offset(\"%s\"): %s", name, fdt_strerror(offset));
nh = fdt_offset_ptr_typed(fdt, offset, nh);
verbose_printf("pointer %p\n", nh);
if (! nh)
FAIL("NULL retrieving subnode \"%s\"", name);
tag = fdt32_to_cpu(nh->tag);
if (tag != FDT_BEGIN_NODE)
FAIL("Incorrect tag 0x%08x on property \"%s\"", tag, name);
if (!streq(nh->name, name))
FAIL("Subnode name mismatch \"%s\" instead of \"%s\"",
nh->name, name);
return offset;
}
int main(int argc, char *argv[])
{
void *fdt;
int subnode1_offset, subnode2_offset;
int subsubnode1_offset, subsubnode2_offset;
test_init(argc, argv);
fdt = load_blob_arg(argc, argv);
subnode1_offset = check_subnode(fdt, 0, "subnode1");
subnode2_offset = check_subnode(fdt, 0, "subnode2");
if (subnode1_offset == subnode2_offset)
FAIL("Different subnodes have same offset");
check_property_typed(fdt, subnode1_offset, "prop-int", TEST_VALUE_1);
check_property_typed(fdt, subnode2_offset, "prop-int", TEST_VALUE_2);
subsubnode1_offset = check_subnode(fdt, subnode1_offset, "subsubnode");
subsubnode2_offset = check_subnode(fdt, subnode2_offset, "subsubnode");
check_property_typed(fdt, subsubnode1_offset, "prop-int", TEST_VALUE_1);
check_property_typed(fdt, subsubnode2_offset, "prop-int", TEST_VALUE_2);
PASS();
}

83
libfdt/tests/sw_tree1.c Normal file
View File

@ -0,0 +1,83 @@
/*
* libfdt - Flat Device Tree manipulation
* Testcase for fdt_nop_node()
* 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
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdint.h>
#include <fdt.h>
#include <libfdt.h>
#include "tests.h"
#include "testdata.h"
#define SPACE 65536
#define CHECK(code) \
{ \
err = (code); \
if (err) \
FAIL(#code ": %s", fdt_strerror(err)); \
}
int main(int argc, char *argv[])
{
void *fdt;
int err;
test_init(argc, argv);
fdt = xmalloc(SPACE);
CHECK(fdt_create(fdt, SPACE));
CHECK(fdt_finish_reservemap(fdt));
CHECK(fdt_begin_node(fdt, ""));
CHECK(fdt_property_typed(fdt, "prop-int", TEST_VALUE_1));
CHECK(fdt_property_string(fdt, "prop-str", TEST_STRING_1));
CHECK(fdt_begin_node(fdt, "subnode1"));
CHECK(fdt_property_typed(fdt, "prop-int", TEST_VALUE_1));
CHECK(fdt_begin_node(fdt, "subsubnode"));
CHECK(fdt_property_typed(fdt, "prop-int", TEST_VALUE_1));
CHECK(fdt_end_node(fdt));
CHECK(fdt_end_node(fdt));
CHECK(fdt_begin_node(fdt, "subnode2"));
CHECK(fdt_property_typed(fdt, "prop-int", TEST_VALUE_2));
CHECK(fdt_begin_node(fdt, "subsubnode"));
CHECK(fdt_property_typed(fdt, "prop-int", TEST_VALUE_2));
CHECK(fdt_end_node(fdt));
CHECK(fdt_end_node(fdt));
CHECK(fdt_end_node(fdt));
save_blob("unfinished_tree1.test.dtb", fdt);
CHECK(fdt_finish(fdt));
verbose_printf("Completed tree, totalsize = %d\n",
fdt_totalsize(fdt));
save_blob("sw_tree1.test.dtb", fdt);
PASS();
}

9
libfdt/tests/testdata.h Normal file
View File

@ -0,0 +1,9 @@
#define TEST_VALUE_1 0xdeadbeef
#define TEST_VALUE_2 0xabcd1234
#define TEST_STRING_1 "hello world"
#ifndef __ASSEMBLY__
extern struct fdt_header _test_tree1;
extern struct fdt_header _truncated_property;
#endif /* ! __ASSEMBLY */

133
libfdt/tests/tests.h Normal file
View File

@ -0,0 +1,133 @@
#ifndef _TESTS_H
#define _TESTS_H
/*
* libfdt - Flat Device Tree manipulation
* Testcase definitions
* 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
*/
#define DEBUG
/* Test return codes */
#define RC_PASS 0
#define RC_CONFIG 1
#define RC_FAIL 2
#define RC_BUG 99
extern int verbose_test;
extern char *test_name;
void test_init(int argc, char *argv[]);
#define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
#define PALIGN(p, a) ((void *)ALIGN((unsigned long)(p), (a)))
#define streq(s1, s2) (strcmp((s1),(s2)) == 0)
/* Each test case must define this function */
void cleanup(void);
#define verbose_printf(...) \
if (verbose_test) { \
printf(__VA_ARGS__); \
fflush(stdout); \
}
#define ERR "ERR: "
#define ERROR(fmt, args...) fprintf(stderr, ERR fmt, ## args)
#define PASS() \
do { \
cleanup(); \
printf("PASS\n"); \
exit(RC_PASS); \
} while (0)
#define PASS_INCONCLUSIVE() \
do { \
cleanup(); \
printf("PASS (inconclusive)\n"); \
exit(RC_PASS); \
} while (0)
#define IRRELEVANT() \
do { \
cleanup(); \
printf("PASS (irrelevant)\n"); \
exit(RC_PASS); \
} while (0)
/* Look out, gcc extension below... */
#define FAIL(fmt, ...) \
do { \
cleanup(); \
printf("FAIL\t" fmt "\n", ##__VA_ARGS__); \
exit(RC_FAIL); \
} while (0)
#define CONFIG(fmt, ...) \
do { \
cleanup(); \
printf("Bad configuration: " fmt "\n", ##__VA_ARGS__); \
exit(RC_CONFIG); \
} while (0)
#define TEST_BUG(fmt, ...) \
do { \
cleanup(); \
printf("BUG in testsuite: " fmt "\n", ##__VA_ARGS__); \
exit(RC_BUG); \
} while (0)
static inline void *xmalloc(size_t size)
{
void *p = malloc(size);
if (! p)
FAIL("malloc() failure");
return p;
}
static inline void *xrealloc(void *p, size_t size)
{
p = realloc(p, size);
if (! p)
FAIL("realloc() failure");
return p;
}
void check_property(void *fdt, int nodeoffset, const char *name,
int len, const void *val);
#define check_property_typed(fdt, nodeoffset, name, val) \
({ \
typeof(val) x = val; \
check_property(fdt, nodeoffset, name, sizeof(x), &x); \
})
const void *check_getprop(void *fdt, int nodeoffset, const char *name,
int len, const void *val);
#define check_getprop_typed(fdt, nodeoffset, name, val) \
({ \
typeof(val) x = val; \
check_getprop(fdt, nodeoffset, name, sizeof(x), &x); \
})
#define check_getprop_string(fdt, nodeoffset, name, s) \
check_getprop((fdt), (nodeoffset), (name), strlen(s)+1, (s))
//void *load_blob(const char *filename);
void *load_blob_arg(int argc, char *argv[]);
void save_blob(const char *filename, void *blob);
#endif /* _TESTS_H */

193
libfdt/tests/testutils.c Normal file
View File

@ -0,0 +1,193 @@
/*
* 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
*/
#define _GNU_SOURCE /* for strsignal() */
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <limits.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <unistd.h>
#include <fcntl.h>
#include <libfdt.h>
#include "tests.h"
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());
}
void check_property(void *fdt, int nodeoffset, const char *name,
int len, const void *val)
{
const struct fdt_property *prop;
int retlen;
uint32_t tag, nameoff, proplen;
const char *propname;
verbose_printf("Checking property \"%s\"...", name);
prop = fdt_get_property(fdt, nodeoffset, name, &retlen);
verbose_printf("pointer %p\n", prop);
if (! prop)
FAIL("Error retreiving \"%s\" pointer: %s", name,
fdt_strerror(retlen));
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);
propname = fdt_string(fdt, nameoff);
if (!propname || !streq(propname, name))
FAIL("Property name mismatch \"%s\" instead of \"%s\"",
propname, name);
if (proplen != retlen)
FAIL("Length retrieved for \"%s\" by fdt_get_property()"
" differs from stored length (%d != %d)",
name, retlen, proplen);
if (proplen != len)
FAIL("Size mismatch on property \"%s\": %d insead of %d",
name, proplen, len);
if (memcmp(val, prop->data, len) != 0)
FAIL("Data mismatch on property \"%s\"", name);
}
const void *check_getprop(void *fdt, int nodeoffset, const char *name,
int len, const void *val)
{
const void *propval;
int proplen;
propval = fdt_getprop(fdt, nodeoffset, name, &proplen);
if (! propval)
FAIL("fdt_getprop(\"%s\"): %s", name, fdt_strerror(proplen));
if (proplen != len)
FAIL("Size mismatch on property \"%s\": %d insead of %d",
name, proplen, len);
if (memcmp(val, propval, len) != 0)
FAIL("Data mismatch on property \"%s\"", name);
return propval;
}
#define CHUNKSIZE 128
void *load_blob(const char *filename)
{
int fd;
int offset = 0;
int bufsize = 1024;
char *p = NULL;
int ret;
fd = open(filename, O_RDONLY);
if (fd < 0)
CONFIG("Couldn't open blob from \"%s\": %s", filename,
strerror(errno));
p = xmalloc(bufsize);
do {
if (offset == bufsize) {
bufsize *= 2;
p = xrealloc(p, bufsize);
}
ret = read(fd, &p[offset], bufsize - offset);
if (ret < 0)
CONFIG("Couldn't read from \"%s\": %s", filename,
strerror(errno));
offset += ret;
} while (ret != 0);
return p;
}
void *load_blob_arg(int argc, char *argv[])
{
if (argc != 2)
CONFIG("Usage: %s <dtb file>", argv[0]);
return load_blob(argv[1]);
}
void save_blob(const char *filename, void *fdt)
{
int fd;
int totalsize;
int offset;
void *p;
int ret;
fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
if (fd < 0)
CONFIG("Couldn't open \"%s\" to write blob: %s", filename,
strerror(errno));
totalsize = fdt_totalsize(fdt);
offset = 0;
p = fdt;
while (offset < totalsize) {
ret = write(fd, p + offset, totalsize - offset);
if (ret < 0)
CONFIG("Couldn't write to \"%s\": %s", filename,
strerror(errno));
offset += ret;
}
}

116
libfdt/tests/trees.S Normal file
View File

@ -0,0 +1,116 @@
#include <fdt.h>
#include "testdata.h"
#define FDTLONG(val) \
.byte ((val) >> 24) & 0xff ; \
.byte ((val) >> 16) & 0xff ; \
.byte ((val) >> 8) & 0xff ; \
.byte (val) & 0xff
#define FDTQUAD(val) \
.byte ((val) >> 56) & 0xff ; \
.byte ((val) >> 48) & 0xff ; \
.byte ((val) >> 40) & 0xff ; \
.byte ((val) >> 32) & 0xff ; \
.byte ((val) >> 24) & 0xff ; \
.byte ((val) >> 16) & 0xff ; \
.byte ((val) >> 8) & 0xff ; \
.byte (val) & 0xff
#define TREE_HDR(tree) \
.balign 4 ; \
.globl _##tree ; \
_##tree: \
tree: \
FDTLONG(FDT_MAGIC) ; \
FDTLONG(tree##_end - tree) ; \
FDTLONG(tree##_struct - tree) ; \
FDTLONG(tree##_strings - tree) ; \
FDTLONG(tree##_rsvmap - tree) ; \
FDTLONG(0x11) ; \
FDTLONG(0x10) ; \
FDTLONG(0) ; \
FDTLONG(tree##_end - tree##_strings) ; \
FDTLONG(tree##_strings - tree##_struct) ;
#define RSVMAP_ENTRY(addr, len) \
FDTQUAD(addr) ; \
FDTQUAD(len) ;
#define PROPHDR(tree, name, len) \
FDTLONG(FDT_PROP) ; \
FDTLONG(len) ; \
FDTLONG(tree##_##name - tree##_strings) ;
#define PROP_INT(tree, name, val) \
PROPHDR(tree, name, 4) \
/* For ease of testing the property values go in native-endian */ \
.long val
#define PROP_STR(tree, name, str) \
PROPHDR(tree, name, 55f - 54f) \
54: \
.string str ; \
55: \
.balign 4
#define BEGIN_NODE(name) \
FDTLONG(FDT_BEGIN_NODE) ; \
.string name ; \
.balign 4
#define END_NODE \
FDTLONG(FDT_END_NODE) ;
#define STRING(tree, name, str) \
tree##_##name: \
.string str
.data
TREE_HDR(test_tree1)
test_tree1_rsvmap:
RSVMAP_ENTRY(0, 0)
test_tree1_struct:
BEGIN_NODE("")
PROP_INT(test_tree1, prop_int, TEST_VALUE_1)
PROP_STR(test_tree1, prop_str, TEST_STRING_1)
BEGIN_NODE("subnode1")
PROP_INT(test_tree1, prop_int, TEST_VALUE_1)
BEGIN_NODE("subsubnode")
PROP_INT(test_tree1, prop_int, TEST_VALUE_1)
END_NODE
END_NODE
BEGIN_NODE("subnode2")
PROP_INT(test_tree1, prop_int, TEST_VALUE_2)
BEGIN_NODE("subsubnode")
PROP_INT(test_tree1, prop_int, TEST_VALUE_2)
END_NODE
END_NODE
END_NODE
FDTLONG(FDT_END)
test_tree1_strings:
STRING(test_tree1, prop_int, "prop-int")
STRING(test_tree1, prop_str, "prop-str")
test_tree1_end:
TREE_HDR(truncated_property)
truncated_property_rsvmap:
RSVMAP_ENTRY(0, 0)
truncated_property_struct:
BEGIN_NODE("")
PROPHDR(truncated_property, prop_truncated, 4)
/* Oops, no actual property data here */
truncated_property_strings:
STRING(truncated_property, prop_truncated, "truncated")
truncated_property_end:

View File

@ -0,0 +1,49 @@
/*
* libfdt - Flat Device Tree manipulation
* Testcase for misbehaviour on a truncated property
* 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
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <fdt.h>
#include <libfdt.h>
#include "tests.h"
#include "testdata.h"
int main(int argc, char *argv[])
{
void *fdt = &_truncated_property;
const void *prop;
int err;
int len;
test_init(argc, argv);
prop = fdt_getprop(fdt, 0, "truncated", &len);
if (prop)
FAIL("fdt_getprop() succeeded on truncated property");
if (len != -FDT_ERR_BADSTRUCTURE)
FAIL("fdt_getprop() failed with \"%s\" instead of \"%s\"",
fdt_strerror(err), fdt_strerror(-FDT_ERR_BADSTRUCTURE));
PASS();
}