1 /* $NetBSD: subnode_iterate.c,v 1.1.1.2 2017/06/08 15:59:27 skrll Exp $ */ 2 3 /* 4 * libfdt - Flat Device Tree manipulation 5 * Tests that fdt_next_subnode() works as expected 6 * 7 * Copyright (C) 2013 Google, Inc 8 * 9 * Copyright (C) 2007 David Gibson, IBM Corporation. 10 * 11 * This library is free software; you can redistribute it and/or 12 * modify it under the terms of the GNU Lesser General Public License 13 * as published by the Free Software Foundation; either version 2.1 of 14 * the License, or (at your option) any later version. 15 * 16 * This library is distributed in the hope that it will be useful, but 17 * WITHOUT ANY WARRANTY; without even the implied warranty of 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 * Lesser General Public License for more details. 20 * 21 * You should have received a copy of the GNU Lesser General Public 22 * License along with this library; if not, write to the Free Software 23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 24 */ 25 26 #include <stdlib.h> 27 #include <stdio.h> 28 #include <string.h> 29 #include <stdint.h> 30 31 #include <libfdt.h> 32 33 #include "tests.h" 34 #include "testdata.h" 35 36 static void test_node(void *fdt, int parent_offset) 37 { 38 uint32_t subnodes; 39 const fdt32_t *prop; 40 int offset; 41 int count; 42 int len; 43 44 /* This property indicates the number of subnodes to expect */ 45 prop = fdt_getprop(fdt, parent_offset, "subnodes", &len); 46 if (!prop || len != sizeof(fdt32_t)) { 47 FAIL("Missing/invalid subnodes property at '%s'", 48 fdt_get_name(fdt, parent_offset, NULL)); 49 } 50 subnodes = fdt32_to_cpu(*prop); 51 52 count = 0; 53 fdt_for_each_subnode(offset, fdt, parent_offset) 54 count++; 55 56 if (count != subnodes) { 57 FAIL("Node '%s': Expected %d subnodes, got %d\n", 58 fdt_get_name(fdt, parent_offset, NULL), subnodes, 59 count); 60 } 61 } 62 63 static void check_fdt_next_subnode(void *fdt) 64 { 65 int offset; 66 int count = 0; 67 68 fdt_for_each_subnode(offset, fdt, 0) { 69 test_node(fdt, offset); 70 count++; 71 } 72 73 if (count != 2) 74 FAIL("Expected %d tests, got %d\n", 2, count); 75 } 76 77 int main(int argc, char *argv[]) 78 { 79 void *fdt; 80 81 test_init(argc, argv); 82 if (argc != 2) 83 CONFIG("Usage: %s <dtb file>", argv[0]); 84 85 fdt = load_blob(argv[1]); 86 if (!fdt) 87 FAIL("No device tree available"); 88 89 check_fdt_next_subnode(fdt); 90 91 PASS(); 92 } 93