1/* $NetBSD: get_phandle.c,v 1.1.1.3 2019/12/22 12:34:06 skrll Exp $ */ 2 3// SPDX-License-Identifier: LGPL-2.1-or-later 4/* 5 * libfdt - Flat Device Tree manipulation 6 * Testcase for fdt_get_phandle() 7 * Copyright (C) 2006 David Gibson, IBM Corporation. 8 */ 9#include <stdbool.h> 10#include <stdlib.h> 11#include <stdio.h> 12#include <string.h> 13#include <stdint.h> 14 15#include <libfdt.h> 16 17#include "tests.h" 18#include "testdata.h" 19 20static void check_phandle(void *fdt, const char *path, uint32_t checkhandle) 21{ 22 int offset; 23 uint32_t phandle; 24 25 offset = fdt_path_offset(fdt, path); 26 if (offset < 0) 27 FAIL("Couldn't find %s", path); 28 29 phandle = fdt_get_phandle(fdt, offset); 30 if (phandle != checkhandle) 31 FAIL("fdt_get_phandle(%s) returned 0x%x instead of 0x%x\n", 32 path, phandle, checkhandle); 33} 34 35static void check_phandle_unique(const void *fdt, uint32_t checkhandle) 36{ 37 uint32_t phandle; 38 int offset = -1; 39 40 while (true) { 41 offset = fdt_next_node(fdt, offset, NULL); 42 if (offset < 0) { 43 if (offset == -FDT_ERR_NOTFOUND) 44 break; 45 46 FAIL("error looking for phandle %#x", checkhandle); 47 } 48 49 phandle = fdt_get_phandle(fdt, offset); 50 51 if (phandle == checkhandle) 52 FAIL("generated phandle already exists"); 53 } 54} 55 56int main(int argc, char *argv[]) 57{ 58 uint32_t max, phandle; 59 void *fdt; 60 int err; 61 62 test_init(argc, argv); 63 fdt = load_blob_arg(argc, argv); 64 65 check_phandle(fdt, "/", 0); 66 check_phandle(fdt, "/subnode@2", PHANDLE_1); 67 check_phandle(fdt, "/subnode@2/subsubnode@0", PHANDLE_2); 68 69 err = fdt_find_max_phandle(fdt, &max); 70 if (err < 0) 71 FAIL("fdt_find_max_phandle returned %d instead of 0\n", err); 72 73 if (max != PHANDLE_2) 74 FAIL("fdt_find_max_phandle found 0x%x instead of 0x%x", max, 75 PHANDLE_2); 76 77 max = fdt_get_max_phandle(fdt); 78 if (max != PHANDLE_2) 79 FAIL("fdt_get_max_phandle returned 0x%x instead of 0x%x\n", 80 max, PHANDLE_2); 81 82 err = fdt_generate_phandle(fdt, &phandle); 83 if (err < 0) 84 FAIL("failed to generate phandle: %d", err); 85 86 check_phandle_unique(fdt, phandle); 87 88 PASS(); 89} 90