Rob Herring | c4ffc05 | 2019-06-20 15:19:41 -0600 | [diff] [blame] | 1 | // SPDX-License-Identifier: LGPL-2.1-or-later |
David Gibson | 7ba551f | 2006-12-01 16:59:43 +1100 | [diff] [blame] | 2 | /* |
| 3 | * libfdt - Flat Device Tree manipulation |
| 4 | * Testcase for fdt_nop_node() |
| 5 | * Copyright (C) 2006 David Gibson, IBM Corporation. |
David Gibson | 7ba551f | 2006-12-01 16:59:43 +1100 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <stdlib.h> |
| 9 | #include <stdio.h> |
| 10 | #include <string.h> |
| 11 | #include <ctype.h> |
David Gibson | 857f54e | 2007-03-23 15:16:54 +1100 | [diff] [blame] | 12 | #include <stdint.h> |
David Gibson | 7ba551f | 2006-12-01 16:59:43 +1100 | [diff] [blame] | 13 | |
David Gibson | 7ba551f | 2006-12-01 16:59:43 +1100 | [diff] [blame] | 14 | #include <libfdt.h> |
| 15 | |
| 16 | #include "tests.h" |
| 17 | #include "testdata.h" |
| 18 | |
| 19 | int main(int argc, char *argv[]) |
| 20 | { |
David Gibson | 73d6092 | 2006-12-15 15:12:47 +1100 | [diff] [blame] | 21 | void *fdt; |
David Gibson | 7ba551f | 2006-12-01 16:59:43 +1100 | [diff] [blame] | 22 | int subnode1_offset, subnode2_offset, subsubnode2_offset; |
| 23 | int err; |
| 24 | int oldsize, delsize, newsize; |
| 25 | |
| 26 | test_init(argc, argv); |
| 27 | fdt = load_blob_arg(argc, argv); |
| 28 | |
David Gibson | a041dcd | 2007-11-01 11:37:31 +1100 | [diff] [blame] | 29 | fdt = open_blob_rw(fdt); |
| 30 | |
David Gibson | 7ba551f | 2006-12-01 16:59:43 +1100 | [diff] [blame] | 31 | oldsize = fdt_totalsize(fdt); |
| 32 | |
David Gibson | d2a9da0 | 2007-09-28 15:51:04 +1000 | [diff] [blame] | 33 | subnode1_offset = fdt_path_offset(fdt, "/subnode@1"); |
David Gibson | 9a9fdf5 | 2006-12-15 15:12:51 +1100 | [diff] [blame] | 34 | if (subnode1_offset < 0) |
David Gibson | d2a9da0 | 2007-09-28 15:51:04 +1000 | [diff] [blame] | 35 | FAIL("Couldn't find \"/subnode@1\": %s", |
David Gibson | 9a9fdf5 | 2006-12-15 15:12:51 +1100 | [diff] [blame] | 36 | fdt_strerror(subnode1_offset)); |
David Gibson | 9521dc5 | 2007-11-20 13:35:46 +1100 | [diff] [blame] | 37 | check_getprop_cell(fdt, subnode1_offset, "prop-int", TEST_VALUE_1); |
David Gibson | 7ba551f | 2006-12-01 16:59:43 +1100 | [diff] [blame] | 38 | |
David Gibson | d2a9da0 | 2007-09-28 15:51:04 +1000 | [diff] [blame] | 39 | subnode2_offset = fdt_path_offset(fdt, "/subnode@2"); |
David Gibson | 9a9fdf5 | 2006-12-15 15:12:51 +1100 | [diff] [blame] | 40 | if (subnode2_offset < 0) |
David Gibson | d2a9da0 | 2007-09-28 15:51:04 +1000 | [diff] [blame] | 41 | FAIL("Couldn't find \"/subnode@2\": %s", |
David Gibson | 9a9fdf5 | 2006-12-15 15:12:51 +1100 | [diff] [blame] | 42 | fdt_strerror(subnode2_offset)); |
David Gibson | 9521dc5 | 2007-11-20 13:35:46 +1100 | [diff] [blame] | 43 | check_getprop_cell(fdt, subnode2_offset, "prop-int", TEST_VALUE_2); |
David Gibson | 7ba551f | 2006-12-01 16:59:43 +1100 | [diff] [blame] | 44 | |
David Gibson | d2a9da0 | 2007-09-28 15:51:04 +1000 | [diff] [blame] | 45 | subsubnode2_offset = fdt_path_offset(fdt, "/subnode@2/subsubnode"); |
David Gibson | 9a9fdf5 | 2006-12-15 15:12:51 +1100 | [diff] [blame] | 46 | if (subsubnode2_offset < 0) |
David Gibson | d2a9da0 | 2007-09-28 15:51:04 +1000 | [diff] [blame] | 47 | FAIL("Couldn't find \"/subnode@2/subsubnode\": %s", |
David Gibson | 9a9fdf5 | 2006-12-15 15:12:51 +1100 | [diff] [blame] | 48 | fdt_strerror(subsubnode2_offset)); |
David Gibson | 9521dc5 | 2007-11-20 13:35:46 +1100 | [diff] [blame] | 49 | check_getprop_cell(fdt, subsubnode2_offset, "prop-int", TEST_VALUE_2); |
David Gibson | 7ba551f | 2006-12-01 16:59:43 +1100 | [diff] [blame] | 50 | |
| 51 | err = fdt_del_node(fdt, subnode1_offset); |
| 52 | if (err) |
| 53 | FAIL("fdt_del_node(subnode1): %s", fdt_strerror(err)); |
| 54 | |
David Gibson | d2a9da0 | 2007-09-28 15:51:04 +1000 | [diff] [blame] | 55 | subnode1_offset = fdt_path_offset(fdt, "/subnode@1"); |
David Gibson | 9a9fdf5 | 2006-12-15 15:12:51 +1100 | [diff] [blame] | 56 | if (subnode1_offset != -FDT_ERR_NOTFOUND) |
David Gibson | 7ba551f | 2006-12-01 16:59:43 +1100 | [diff] [blame] | 57 | FAIL("fdt_path_offset(subnode1) returned \"%s\" instead of \"%s\"", |
David Gibson | 9a9fdf5 | 2006-12-15 15:12:51 +1100 | [diff] [blame] | 58 | fdt_strerror(subnode1_offset), |
| 59 | fdt_strerror(-FDT_ERR_NOTFOUND)); |
David Gibson | 7ba551f | 2006-12-01 16:59:43 +1100 | [diff] [blame] | 60 | |
David Gibson | d2a9da0 | 2007-09-28 15:51:04 +1000 | [diff] [blame] | 61 | subnode2_offset = fdt_path_offset(fdt, "/subnode@2"); |
David Gibson | 9a9fdf5 | 2006-12-15 15:12:51 +1100 | [diff] [blame] | 62 | if (subnode2_offset < 0) |
| 63 | FAIL("Couldn't find \"/subnode2\": %s", |
| 64 | fdt_strerror(subnode2_offset)); |
David Gibson | 9521dc5 | 2007-11-20 13:35:46 +1100 | [diff] [blame] | 65 | check_getprop_cell(fdt, subnode2_offset, "prop-int", TEST_VALUE_2); |
David Gibson | 7ba551f | 2006-12-01 16:59:43 +1100 | [diff] [blame] | 66 | |
David Gibson | d2a9da0 | 2007-09-28 15:51:04 +1000 | [diff] [blame] | 67 | subsubnode2_offset = fdt_path_offset(fdt, "/subnode@2/subsubnode"); |
David Gibson | 9a9fdf5 | 2006-12-15 15:12:51 +1100 | [diff] [blame] | 68 | if (subsubnode2_offset < 0) |
David Gibson | d2a9da0 | 2007-09-28 15:51:04 +1000 | [diff] [blame] | 69 | FAIL("Couldn't find \"/subnode@2/subsubnode\": %s", |
David Gibson | 9a9fdf5 | 2006-12-15 15:12:51 +1100 | [diff] [blame] | 70 | fdt_strerror(subsubnode2_offset)); |
David Gibson | 9521dc5 | 2007-11-20 13:35:46 +1100 | [diff] [blame] | 71 | check_getprop_cell(fdt, subsubnode2_offset, "prop-int", TEST_VALUE_2); |
David Gibson | 7ba551f | 2006-12-01 16:59:43 +1100 | [diff] [blame] | 72 | |
| 73 | err = fdt_del_node(fdt, subnode2_offset); |
| 74 | if (err) |
| 75 | FAIL("fdt_del_node(subnode2): %s", fdt_strerror(err)); |
| 76 | |
David Gibson | d2a9da0 | 2007-09-28 15:51:04 +1000 | [diff] [blame] | 77 | subnode1_offset = fdt_path_offset(fdt, "/subnode@1"); |
David Gibson | 9a9fdf5 | 2006-12-15 15:12:51 +1100 | [diff] [blame] | 78 | if (subnode1_offset != -FDT_ERR_NOTFOUND) |
David Gibson | 7ba551f | 2006-12-01 16:59:43 +1100 | [diff] [blame] | 79 | FAIL("fdt_path_offset(subnode1) returned \"%s\" instead of \"%s\"", |
David Gibson | 9a9fdf5 | 2006-12-15 15:12:51 +1100 | [diff] [blame] | 80 | fdt_strerror(subnode1_offset), |
| 81 | fdt_strerror(-FDT_ERR_NOTFOUND)); |
David Gibson | 7ba551f | 2006-12-01 16:59:43 +1100 | [diff] [blame] | 82 | |
David Gibson | d2a9da0 | 2007-09-28 15:51:04 +1000 | [diff] [blame] | 83 | subnode2_offset = fdt_path_offset(fdt, "/subnode@2"); |
David Gibson | 9a9fdf5 | 2006-12-15 15:12:51 +1100 | [diff] [blame] | 84 | if (subnode2_offset != -FDT_ERR_NOTFOUND) |
David Gibson | 7ba551f | 2006-12-01 16:59:43 +1100 | [diff] [blame] | 85 | FAIL("fdt_path_offset(subnode2) returned \"%s\" instead of \"%s\"", |
David Gibson | 9a9fdf5 | 2006-12-15 15:12:51 +1100 | [diff] [blame] | 86 | fdt_strerror(subnode2_offset), |
| 87 | fdt_strerror(-FDT_ERR_NOTFOUND)); |
David Gibson | 7ba551f | 2006-12-01 16:59:43 +1100 | [diff] [blame] | 88 | |
David Gibson | d2a9da0 | 2007-09-28 15:51:04 +1000 | [diff] [blame] | 89 | subsubnode2_offset = fdt_path_offset(fdt, "/subnode@2/subsubnode"); |
David Gibson | 9a9fdf5 | 2006-12-15 15:12:51 +1100 | [diff] [blame] | 90 | if (subsubnode2_offset != -FDT_ERR_NOTFOUND) |
David Gibson | 7ba551f | 2006-12-01 16:59:43 +1100 | [diff] [blame] | 91 | FAIL("fdt_path_offset(subsubnode2) returned \"%s\" instead of \"%s\"", |
David Gibson | 9a9fdf5 | 2006-12-15 15:12:51 +1100 | [diff] [blame] | 92 | fdt_strerror(subsubnode2_offset), |
| 93 | fdt_strerror(-FDT_ERR_NOTFOUND)); |
David Gibson | 7ba551f | 2006-12-01 16:59:43 +1100 | [diff] [blame] | 94 | |
| 95 | delsize = fdt_totalsize(fdt); |
| 96 | |
David Gibson | 73d6092 | 2006-12-15 15:12:47 +1100 | [diff] [blame] | 97 | err = fdt_pack(fdt); |
| 98 | if (err) |
David Gibson | 7ba551f | 2006-12-01 16:59:43 +1100 | [diff] [blame] | 99 | FAIL("fdt_pack(): %s", fdt_strerror(err)); |
| 100 | |
| 101 | newsize = fdt_totalsize(fdt); |
| 102 | |
| 103 | verbose_printf("oldsize = %d, delsize = %d, newsize = %d\n", |
| 104 | oldsize, delsize, newsize); |
| 105 | |
| 106 | if (newsize >= oldsize) |
| 107 | FAIL("Tree failed to shrink after deletions"); |
| 108 | |
| 109 | PASS(); |
| 110 | } |