David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 1 | /* |
| 2 | * libfdt - Flat Device Tree manipulation |
| 3 | * Testcase for fdt_subnode_offset() |
| 4 | * Copyright (C) 2006 David Gibson, IBM Corporation. |
| 5 | * |
| 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Lesser General Public License |
| 8 | * as published by the Free Software Foundation; either version 2.1 of |
| 9 | * the License, or (at your option) any later version. |
| 10 | * |
| 11 | * This library is distributed in the hope that it will be useful, but |
| 12 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Lesser General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Lesser General Public |
| 17 | * License along with this library; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | */ |
| 20 | #include <stdlib.h> |
| 21 | #include <stdio.h> |
| 22 | #include <string.h> |
David Gibson | 857f54e | 2007-03-23 15:16:54 +1100 | [diff] [blame] | 23 | #include <stdint.h> |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 24 | |
| 25 | #include <fdt.h> |
| 26 | #include <libfdt.h> |
| 27 | |
| 28 | #include "tests.h" |
| 29 | #include "testdata.h" |
| 30 | |
| 31 | int check_subnode(struct fdt_header *fdt, int parent, const char *name) |
| 32 | { |
| 33 | int offset; |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 34 | struct fdt_node_header *nh; |
| 35 | uint32_t tag; |
| 36 | |
| 37 | verbose_printf("Checking subnode \"%s\" of %d...", name, parent); |
| 38 | offset = fdt_subnode_offset(fdt, parent, name); |
| 39 | verbose_printf("offset %d...", offset); |
David Gibson | 9a9fdf5 | 2006-12-15 15:12:51 +1100 | [diff] [blame] | 40 | if (offset < 0) |
| 41 | FAIL("fdt_subnode_offset(\"%s\"): %s", name, fdt_strerror(offset)); |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 42 | nh = fdt_offset_ptr_typed(fdt, offset, nh); |
| 43 | verbose_printf("pointer %p\n", nh); |
| 44 | if (! nh) |
| 45 | FAIL("NULL retrieving subnode \"%s\"", name); |
| 46 | |
| 47 | tag = fdt32_to_cpu(nh->tag); |
| 48 | |
| 49 | if (tag != FDT_BEGIN_NODE) |
| 50 | FAIL("Incorrect tag 0x%08x on property \"%s\"", tag, name); |
David Gibson | d2a9da0 | 2007-09-28 15:51:04 +1000 | [diff] [blame^] | 51 | if (!nodename_eq(nh->name, name)) |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 52 | FAIL("Subnode name mismatch \"%s\" instead of \"%s\"", |
| 53 | nh->name, name); |
| 54 | |
| 55 | return offset; |
| 56 | } |
| 57 | |
| 58 | int main(int argc, char *argv[]) |
| 59 | { |
David Gibson | 73d6092 | 2006-12-15 15:12:47 +1100 | [diff] [blame] | 60 | void *fdt; |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 61 | int subnode1_offset, subnode2_offset; |
David Gibson | d2a9da0 | 2007-09-28 15:51:04 +1000 | [diff] [blame^] | 62 | int subsubnode1_offset, subsubnode2_offset, subsubnode2_offset2; |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 63 | |
| 64 | test_init(argc, argv); |
David Gibson | 4e6221c | 2006-11-28 17:20:01 +1100 | [diff] [blame] | 65 | fdt = load_blob_arg(argc, argv); |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 66 | |
David Gibson | d2a9da0 | 2007-09-28 15:51:04 +1000 | [diff] [blame^] | 67 | subnode1_offset = check_subnode(fdt, 0, "subnode@1"); |
| 68 | subnode2_offset = check_subnode(fdt, 0, "subnode@2"); |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 69 | |
| 70 | if (subnode1_offset == subnode2_offset) |
| 71 | FAIL("Different subnodes have same offset"); |
| 72 | |
| 73 | check_property_typed(fdt, subnode1_offset, "prop-int", TEST_VALUE_1); |
| 74 | check_property_typed(fdt, subnode2_offset, "prop-int", TEST_VALUE_2); |
| 75 | |
| 76 | subsubnode1_offset = check_subnode(fdt, subnode1_offset, "subsubnode"); |
David Gibson | d2a9da0 | 2007-09-28 15:51:04 +1000 | [diff] [blame^] | 77 | subsubnode2_offset = check_subnode(fdt, subnode2_offset, "subsubnode@0"); |
| 78 | subsubnode2_offset2 = check_subnode(fdt, subnode2_offset, "subsubnode"); |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 79 | |
| 80 | check_property_typed(fdt, subsubnode1_offset, "prop-int", TEST_VALUE_1); |
| 81 | check_property_typed(fdt, subsubnode2_offset, "prop-int", TEST_VALUE_2); |
David Gibson | d2a9da0 | 2007-09-28 15:51:04 +1000 | [diff] [blame^] | 82 | check_property_typed(fdt, subsubnode2_offset2, "prop-int", TEST_VALUE_2); |
| 83 | |
| 84 | if (subsubnode2_offset != subsubnode2_offset2) |
| 85 | FAIL("Different offsets with and without unit address"); |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 86 | |
| 87 | PASS(); |
| 88 | } |