1 /* $NetBSD: sw_tree1.c,v 1.1.1.2 2017/06/08 15:59:27 skrll Exp $ */ 2 3 /* 4 * libfdt - Flat Device Tree manipulation 5 * Testcase for fdt_nop_node() 6 * Copyright (C) 2006 David Gibson, IBM Corporation. 7 * 8 * This library is free software; you can redistribute it and/or 9 * modify it under the terms of the GNU Lesser General Public License 10 * as published by the Free Software Foundation; either version 2.1 of 11 * the License, or (at your option) any later version. 12 * 13 * This library is distributed in the hope that it will be useful, but 14 * WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 * Lesser General Public License for more details. 17 * 18 * You should have received a copy of the GNU Lesser General Public 19 * License along with this library; if not, write to the Free Software 20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 21 */ 22 23 #include <stdlib.h> 24 #include <stdio.h> 25 #include <string.h> 26 #include <ctype.h> 27 #include <stdint.h> 28 29 #include <libfdt.h> 30 31 #include "tests.h" 32 #include "testdata.h" 33 34 #define SPACE 65536 35 36 static enum { 37 FIXED = 0, 38 RESIZE, 39 REALLOC, 40 } alloc_mode; 41 42 static void realloc_fdt(void **fdt, size_t *size, bool created) 43 { 44 switch (alloc_mode) { 45 case FIXED: 46 if (!(*fdt)) 47 *fdt = xmalloc(*size); 48 else 49 FAIL("Ran out of space"); 50 return; 51 52 case RESIZE: 53 if (!(*fdt)) { 54 *fdt = xmalloc(SPACE); 55 } else if (*size < SPACE) { 56 *size += 1; 57 fdt_resize(*fdt, *fdt, *size); 58 } else { 59 FAIL("Ran out of space"); 60 } 61 return; 62 63 case REALLOC: 64 *size += 1; 65 *fdt = xrealloc(*fdt, *size); 66 if (created) 67 fdt_resize(*fdt, *fdt, *size); 68 return; 69 70 default: 71 CONFIG("Bad allocation mode"); 72 } 73 } 74 75 #define CHECK(code) \ 76 do { \ 77 err = (code); \ 78 if (err == -FDT_ERR_NOSPACE) \ 79 realloc_fdt(&fdt, &size, created); \ 80 else if (err) \ 81 FAIL(#code ": %s", fdt_strerror(err)); \ 82 } while (err != 0) 83 84 int main(int argc, char *argv[]) 85 { 86 void *fdt = NULL; 87 size_t size; 88 int err; 89 bool created = false; 90 91 test_init(argc, argv); 92 93 if (argc == 1) { 94 alloc_mode = FIXED; 95 size = SPACE; 96 } else if (argc == 2) { 97 if (streq(argv[1], "resize")) { 98 alloc_mode = REALLOC; 99 size = 0; 100 } else if (streq(argv[1], "realloc")) { 101 alloc_mode = REALLOC; 102 size = 0; 103 } else { 104 char *endp; 105 106 size = strtoul(argv[1], &endp, 0); 107 if (*endp == '\0') 108 alloc_mode = FIXED; 109 else 110 CONFIG("Bad allocation mode \"%s\" specified", 111 argv[1]); 112 } 113 } 114 115 fdt = xmalloc(size); 116 CHECK(fdt_create(fdt, size)); 117 118 created = true; 119 120 CHECK(fdt_add_reservemap_entry(fdt, TEST_ADDR_1, TEST_SIZE_1)); 121 122 CHECK(fdt_add_reservemap_entry(fdt, TEST_ADDR_2, TEST_SIZE_2)); 123 CHECK(fdt_finish_reservemap(fdt)); 124 125 CHECK(fdt_begin_node(fdt, "")); 126 CHECK(fdt_property_string(fdt, "compatible", "test_tree1")); 127 CHECK(fdt_property_u32(fdt, "prop-int", TEST_VALUE_1)); 128 CHECK(fdt_property_u64(fdt, "prop-int64", TEST_VALUE64_1)); 129 CHECK(fdt_property_string(fdt, "prop-str", TEST_STRING_1)); 130 CHECK(fdt_property_u32(fdt, "#address-cells", 1)); 131 CHECK(fdt_property_u32(fdt, "#size-cells", 0)); 132 133 CHECK(fdt_begin_node(fdt, "subnode@1")); 134 CHECK(fdt_property_string(fdt, "compatible", "subnode1")); 135 CHECK(fdt_property_u32(fdt, "reg", 1)); 136 CHECK(fdt_property_cell(fdt, "prop-int", TEST_VALUE_1)); 137 CHECK(fdt_begin_node(fdt, "subsubnode")); 138 CHECK(fdt_property(fdt, "compatible", "subsubnode1\0subsubnode", 139 23)); 140 CHECK(fdt_property_cell(fdt, "prop-int", TEST_VALUE_1)); 141 CHECK(fdt_end_node(fdt)); 142 CHECK(fdt_begin_node(fdt, "ss1")); 143 CHECK(fdt_end_node(fdt)); 144 CHECK(fdt_end_node(fdt)); 145 146 CHECK(fdt_begin_node(fdt, "subnode@2")); 147 CHECK(fdt_property_u32(fdt, "reg", 2)); 148 CHECK(fdt_property_cell(fdt, "linux,phandle", PHANDLE_1)); 149 CHECK(fdt_property_cell(fdt, "prop-int", TEST_VALUE_2)); 150 CHECK(fdt_property_u32(fdt, "#address-cells", 1)); 151 CHECK(fdt_property_u32(fdt, "#size-cells", 0)); 152 CHECK(fdt_begin_node(fdt, "subsubnode@0")); 153 CHECK(fdt_property_u32(fdt, "reg", 0)); 154 CHECK(fdt_property_cell(fdt, "phandle", PHANDLE_2)); 155 CHECK(fdt_property(fdt, "compatible", "subsubnode2\0subsubnode", 156 23)); 157 CHECK(fdt_property_cell(fdt, "prop-int", TEST_VALUE_2)); 158 CHECK(fdt_end_node(fdt)); 159 CHECK(fdt_begin_node(fdt, "ss2")); 160 CHECK(fdt_end_node(fdt)); 161 162 CHECK(fdt_end_node(fdt)); 163 164 CHECK(fdt_end_node(fdt)); 165 166 save_blob("unfinished_tree1.test.dtb", fdt); 167 168 CHECK(fdt_finish(fdt)); 169 170 verbose_printf("Completed tree, totalsize = %d\n", 171 fdt_totalsize(fdt)); 172 173 save_blob("sw_tree1.test.dtb", fdt); 174 175 PASS(); 176 } 177