xref: /freebsd/sys/contrib/libfdt/fdt_strerror.c (revision 6780e684d49034610f82bea5d3bfb04d42e91628)
121fdc27aSRafal Jaworowski /*
221fdc27aSRafal Jaworowski  * libfdt - Flat Device Tree manipulation
321fdc27aSRafal Jaworowski  * Copyright (C) 2006 David Gibson, IBM Corporation.
421fdc27aSRafal Jaworowski  *
521fdc27aSRafal Jaworowski  * libfdt is dual licensed: you can use it either under the terms of
621fdc27aSRafal Jaworowski  * the GPL, or the BSD license, at your option.
721fdc27aSRafal Jaworowski  *
821fdc27aSRafal Jaworowski  *  a) This library is free software; you can redistribute it and/or
921fdc27aSRafal Jaworowski  *     modify it under the terms of the GNU General Public License as
1021fdc27aSRafal Jaworowski  *     published by the Free Software Foundation; either version 2 of the
1121fdc27aSRafal Jaworowski  *     License, or (at your option) any later version.
1221fdc27aSRafal Jaworowski  *
1321fdc27aSRafal Jaworowski  *     This library is distributed in the hope that it will be useful,
1421fdc27aSRafal Jaworowski  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
1521fdc27aSRafal Jaworowski  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1621fdc27aSRafal Jaworowski  *     GNU General Public License for more details.
1721fdc27aSRafal Jaworowski  *
1821fdc27aSRafal Jaworowski  *     You should have received a copy of the GNU General Public
1921fdc27aSRafal Jaworowski  *     License along with this library; if not, write to the Free
2021fdc27aSRafal Jaworowski  *     Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
2121fdc27aSRafal Jaworowski  *     MA 02110-1301 USA
2221fdc27aSRafal Jaworowski  *
2321fdc27aSRafal Jaworowski  * Alternatively,
2421fdc27aSRafal Jaworowski  *
2521fdc27aSRafal Jaworowski  *  b) Redistribution and use in source and binary forms, with or
2621fdc27aSRafal Jaworowski  *     without modification, are permitted provided that the following
2721fdc27aSRafal Jaworowski  *     conditions are met:
2821fdc27aSRafal Jaworowski  *
2921fdc27aSRafal Jaworowski  *     1. Redistributions of source code must retain the above
3021fdc27aSRafal Jaworowski  *        copyright notice, this list of conditions and the following
3121fdc27aSRafal Jaworowski  *        disclaimer.
3221fdc27aSRafal Jaworowski  *     2. Redistributions in binary form must reproduce the above
3321fdc27aSRafal Jaworowski  *        copyright notice, this list of conditions and the following
3421fdc27aSRafal Jaworowski  *        disclaimer in the documentation and/or other materials
3521fdc27aSRafal Jaworowski  *        provided with the distribution.
3621fdc27aSRafal Jaworowski  *
3721fdc27aSRafal Jaworowski  *     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
3821fdc27aSRafal Jaworowski  *     CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
3921fdc27aSRafal Jaworowski  *     INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
4021fdc27aSRafal Jaworowski  *     MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
4121fdc27aSRafal Jaworowski  *     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
4221fdc27aSRafal Jaworowski  *     CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
4321fdc27aSRafal Jaworowski  *     SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
4421fdc27aSRafal Jaworowski  *     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
4521fdc27aSRafal Jaworowski  *     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4621fdc27aSRafal Jaworowski  *     HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
4721fdc27aSRafal Jaworowski  *     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
4821fdc27aSRafal Jaworowski  *     OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
4921fdc27aSRafal Jaworowski  *     EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
5021fdc27aSRafal Jaworowski  */
5121fdc27aSRafal Jaworowski #include "libfdt_env.h"
5221fdc27aSRafal Jaworowski 
5321fdc27aSRafal Jaworowski #include <fdt.h>
5421fdc27aSRafal Jaworowski #include <libfdt.h>
5521fdc27aSRafal Jaworowski 
5621fdc27aSRafal Jaworowski #include "libfdt_internal.h"
5721fdc27aSRafal Jaworowski 
5821fdc27aSRafal Jaworowski struct fdt_errtabent {
5921fdc27aSRafal Jaworowski 	const char *str;
6021fdc27aSRafal Jaworowski };
6121fdc27aSRafal Jaworowski 
6221fdc27aSRafal Jaworowski #define FDT_ERRTABENT(val) \
6321fdc27aSRafal Jaworowski 	[(val)] = { .str = #val, }
6421fdc27aSRafal Jaworowski 
6521fdc27aSRafal Jaworowski static struct fdt_errtabent fdt_errtable[] = {
6621fdc27aSRafal Jaworowski 	FDT_ERRTABENT(FDT_ERR_NOTFOUND),
6721fdc27aSRafal Jaworowski 	FDT_ERRTABENT(FDT_ERR_EXISTS),
6821fdc27aSRafal Jaworowski 	FDT_ERRTABENT(FDT_ERR_NOSPACE),
6921fdc27aSRafal Jaworowski 
7021fdc27aSRafal Jaworowski 	FDT_ERRTABENT(FDT_ERR_BADOFFSET),
7121fdc27aSRafal Jaworowski 	FDT_ERRTABENT(FDT_ERR_BADPATH),
72*6780e684SKyle Evans 	FDT_ERRTABENT(FDT_ERR_BADPHANDLE),
7321fdc27aSRafal Jaworowski 	FDT_ERRTABENT(FDT_ERR_BADSTATE),
7421fdc27aSRafal Jaworowski 
7521fdc27aSRafal Jaworowski 	FDT_ERRTABENT(FDT_ERR_TRUNCATED),
7621fdc27aSRafal Jaworowski 	FDT_ERRTABENT(FDT_ERR_BADMAGIC),
7721fdc27aSRafal Jaworowski 	FDT_ERRTABENT(FDT_ERR_BADVERSION),
7821fdc27aSRafal Jaworowski 	FDT_ERRTABENT(FDT_ERR_BADSTRUCTURE),
7921fdc27aSRafal Jaworowski 	FDT_ERRTABENT(FDT_ERR_BADLAYOUT),
80*6780e684SKyle Evans 	FDT_ERRTABENT(FDT_ERR_INTERNAL),
81*6780e684SKyle Evans 	FDT_ERRTABENT(FDT_ERR_BADNCELLS),
82*6780e684SKyle Evans 	FDT_ERRTABENT(FDT_ERR_BADVALUE),
83*6780e684SKyle Evans 	FDT_ERRTABENT(FDT_ERR_BADOVERLAY),
84*6780e684SKyle Evans 	FDT_ERRTABENT(FDT_ERR_NOPHANDLES),
8521fdc27aSRafal Jaworowski };
8621fdc27aSRafal Jaworowski #define FDT_ERRTABSIZE	(sizeof(fdt_errtable) / sizeof(fdt_errtable[0]))
8721fdc27aSRafal Jaworowski 
fdt_strerror(int errval)8821fdc27aSRafal Jaworowski const char *fdt_strerror(int errval)
8921fdc27aSRafal Jaworowski {
9021fdc27aSRafal Jaworowski 	if (errval > 0)
9121fdc27aSRafal Jaworowski 		return "<valid offset/length>";
9221fdc27aSRafal Jaworowski 	else if (errval == 0)
9321fdc27aSRafal Jaworowski 		return "<no error>";
9421fdc27aSRafal Jaworowski 	else if (errval > -FDT_ERRTABSIZE) {
9521fdc27aSRafal Jaworowski 		const char *s = fdt_errtable[-errval].str;
9621fdc27aSRafal Jaworowski 
9721fdc27aSRafal Jaworowski 		if (s)
9821fdc27aSRafal Jaworowski 			return s;
9921fdc27aSRafal Jaworowski 	}
10021fdc27aSRafal Jaworowski 
10121fdc27aSRafal Jaworowski 	return "<unknown error>";
10221fdc27aSRafal Jaworowski }
103