1 /*
2 * Copyright (C) 2006 David Gibson, IBM Corporation.
3 *
4 * This file is part of libxl, and was originally taken from libfdt.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published
8 * by the Free Software Foundation; version 2.1 only. with the special
9 * exception on linking described in file LICENSE.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * Additionally, this particular file is dual licensed. That is,
17 * alternatively, at your option:
18 *
19 * Redistribution and use in source and binary forms, with or
20 * without modification, are permitted provided that the following
21 * conditions are met:
22 *
23 * 1. Redistributions of source code must retain the above
24 * copyright notice, this list of conditions and the following
25 * disclaimer.
26 * 2. Redistributions in binary form must reproduce the above
27 * copyright notice, this list of conditions and the following
28 * disclaimer in the documentation and/or other materials
29 * provided with the distribution.
30 *
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
32 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
33 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
34 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
35 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
36 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
37 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
38 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
39 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
41 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
42 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
43 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 *
45 * Note that this applies only to this file, and other files with a
46 * similar notice. Also, note that when the same code is distributed
47 * along with the rest of libxl, you must comply with the terms of the
48 * LGPLv2.1 for the whole of libxl including this file.
49 *
50 * The intent is to permit, in particular, upstream libfdt to
51 * incorporate improvements to this file within upstream libfdt. At
52 * the time of writing, upstream libfdt is dual licensed: 2-clause BSD
53 * (as above) and GPLv2-or-later. The 2-clause BSD licence is
54 * compatible with both GPLv2-or-later and LGPLv2.1-only; this permits
55 * copying in both directions, and the optional licence upgrade to a
56 * copyleft licence by libdft upstream or the Xen Project,
57 * respectively.
58 */
59
60 #include <libfdt.h>
61
62 #include "libxl_libfdt_compat.h"
63
64 #ifndef HAVE_FDT_FIRST_SUBNODE
fdt_first_subnode(const void * fdt,int offset)65 _hidden int fdt_first_subnode(const void *fdt, int offset)
66 {
67 int depth = 0;
68
69 offset = fdt_next_node(fdt, offset, &depth);
70 if (offset < 0 || depth != 1)
71 return -FDT_ERR_NOTFOUND;
72
73 return offset;
74 }
75 #endif
76
77 #ifndef HAVE_FDT_NEXT_SUBNODE
fdt_next_subnode(const void * fdt,int offset)78 _hidden int fdt_next_subnode(const void *fdt, int offset)
79 {
80 int depth = 1;
81
82 /*
83 * With respect to the parent, the depth of the next subnode will be
84 * the same as the last.
85 */
86 do {
87 offset = fdt_next_node(fdt, offset, &depth);
88 if (offset < 0 || depth < 1)
89 return -FDT_ERR_NOTFOUND;
90 } while (depth > 1);
91
92 return offset;
93 }
94 #endif
95