1204431Sraj/*
2204431Sraj * libfdt - Flat Device Tree manipulation
3204431Sraj * Copyright (C) 2006 David Gibson, IBM Corporation.
4204431Sraj *
5204431Sraj * libfdt is dual licensed: you can use it either under the terms of
6204431Sraj * the GPL, or the BSD license, at your option.
7204431Sraj *
8204431Sraj *  a) This library is free software; you can redistribute it and/or
9204431Sraj *     modify it under the terms of the GNU General Public License as
10204431Sraj *     published by the Free Software Foundation; either version 2 of the
11204431Sraj *     License, or (at your option) any later version.
12204431Sraj *
13204431Sraj *     This library is distributed in the hope that it will be useful,
14204431Sraj *     but WITHOUT ANY WARRANTY; without even the implied warranty of
15204431Sraj *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16204431Sraj *     GNU General Public License for more details.
17204431Sraj *
18204431Sraj *     You should have received a copy of the GNU General Public
19204431Sraj *     License along with this library; if not, write to the Free
20204431Sraj *     Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
21204431Sraj *     MA 02110-1301 USA
22204431Sraj *
23204431Sraj * Alternatively,
24204431Sraj *
25204431Sraj *  b) Redistribution and use in source and binary forms, with or
26204431Sraj *     without modification, are permitted provided that the following
27204431Sraj *     conditions are met:
28204431Sraj *
29204431Sraj *     1. Redistributions of source code must retain the above
30204431Sraj *        copyright notice, this list of conditions and the following
31204431Sraj *        disclaimer.
32204431Sraj *     2. Redistributions in binary form must reproduce the above
33204431Sraj *        copyright notice, this list of conditions and the following
34204431Sraj *        disclaimer in the documentation and/or other materials
35204431Sraj *        provided with the distribution.
36204431Sraj *
37204431Sraj *     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
38204431Sraj *     CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
39204431Sraj *     INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
40204431Sraj *     MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
41204431Sraj *     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
42204431Sraj *     CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43204431Sraj *     SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44204431Sraj *     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45204431Sraj *     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46204431Sraj *     HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47204431Sraj *     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
48204431Sraj *     OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
49204431Sraj *     EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
50204431Sraj */
51204431Sraj#include "libfdt_env.h"
52204431Sraj
53204431Sraj#include <fdt.h>
54204431Sraj#include <libfdt.h>
55204431Sraj
56204431Sraj#include "libfdt_internal.h"
57204431Sraj
58204431Srajstruct fdt_errtabent {
59204431Sraj	const char *str;
60204431Sraj};
61204431Sraj
62204431Sraj#define FDT_ERRTABENT(val) \
63204431Sraj	[(val)] = { .str = #val, }
64204431Sraj
65204431Srajstatic struct fdt_errtabent fdt_errtable[] = {
66204431Sraj	FDT_ERRTABENT(FDT_ERR_NOTFOUND),
67204431Sraj	FDT_ERRTABENT(FDT_ERR_EXISTS),
68204431Sraj	FDT_ERRTABENT(FDT_ERR_NOSPACE),
69204431Sraj
70204431Sraj	FDT_ERRTABENT(FDT_ERR_BADOFFSET),
71204431Sraj	FDT_ERRTABENT(FDT_ERR_BADPATH),
72204431Sraj	FDT_ERRTABENT(FDT_ERR_BADSTATE),
73204431Sraj
74204431Sraj	FDT_ERRTABENT(FDT_ERR_TRUNCATED),
75204431Sraj	FDT_ERRTABENT(FDT_ERR_BADMAGIC),
76204431Sraj	FDT_ERRTABENT(FDT_ERR_BADVERSION),
77204431Sraj	FDT_ERRTABENT(FDT_ERR_BADSTRUCTURE),
78204431Sraj	FDT_ERRTABENT(FDT_ERR_BADLAYOUT),
79204431Sraj};
80204431Sraj#define FDT_ERRTABSIZE	(sizeof(fdt_errtable) / sizeof(fdt_errtable[0]))
81204431Sraj
82204431Srajconst char *fdt_strerror(int errval)
83204431Sraj{
84204431Sraj	if (errval > 0)
85204431Sraj		return "<valid offset/length>";
86204431Sraj	else if (errval == 0)
87204431Sraj		return "<no error>";
88204431Sraj	else if (errval > -FDT_ERRTABSIZE) {
89204431Sraj		const char *s = fdt_errtable[-errval].str;
90204431Sraj
91204431Sraj		if (s)
92204431Sraj			return s;
93204431Sraj	}
94204431Sraj
95204431Sraj	return "<unknown error>";
96204431Sraj}
97