efi_loader: correct includes in efi_variable.c
[oweals/u-boot.git] / lib / libfdt / fdt_ro.c
index fc08981a2c8c86c1d6cf4c9d8d68e98b527c3248..693de9aa5ad8032375b22749a25bc236dc15d950 100644 (file)
@@ -1,13 +1,13 @@
+// SPDX-License-Identifier: GPL-2.0+ OR BSD-2-Clause
 /*
  * libfdt - Flat Device Tree manipulation
  * Copyright (C) 2006 David Gibson, IBM Corporation.
- * SPDX-License-Identifier:    GPL-2.0+ BSD-2-Clause
  */
-#include <libfdt_env.h>
+#include <linux/libfdt_env.h>
 
 #ifndef USE_HOSTCC
 #include <fdt.h>
-#include <libfdt.h>
+#include <linux/libfdt.h>
 #else
 #include "fdt_host.h"
 #endif
@@ -73,11 +73,42 @@ uint32_t fdt_get_max_phandle(const void *fdt)
        return 0;
 }
 
+int fdt_generate_phandle(const void *fdt, uint32_t *phandle)
+{
+       uint32_t max = 0;
+       int offset = -1;
+
+       while (true) {
+               uint32_t value;
+
+               offset = fdt_next_node(fdt, offset, NULL);
+               if (offset < 0) {
+                       if (offset == -FDT_ERR_NOTFOUND)
+                               break;
+
+                       return offset;
+               }
+
+               value = fdt_get_phandle(fdt, offset);
+
+               if (value > max)
+                       max = value;
+       }
+
+       if (max == FDT_MAX_PHANDLE)
+               return -FDT_ERR_NOPHANDLES;
+
+       if (phandle)
+               *phandle = max + 1;
+
+       return 0;
+}
+
 int fdt_get_mem_rsv(const void *fdt, int n, uint64_t *address, uint64_t *size)
 {
        FDT_CHECK_HEADER(fdt);
-       *address = fdt64_to_cpu(_fdt_mem_rsv(fdt, n)->address);
-       *size = fdt64_to_cpu(_fdt_mem_rsv(fdt, n)->size);
+       *address = fdt64_to_cpu(fdt_mem_rsv_(fdt, n)->address);
+       *size = fdt64_to_cpu(fdt_mem_rsv_(fdt, n)->size);
        return 0;
 }
 
@@ -85,7 +116,7 @@ int fdt_num_mem_rsv(const void *fdt)
 {
        int i = 0;
 
-       while (fdt64_to_cpu(_fdt_mem_rsv(fdt, i)->size) != 0)
+       while (fdt64_to_cpu(fdt_mem_rsv_(fdt, i)->size) != 0)
                i++;
        return i;
 }
@@ -211,11 +242,11 @@ int fdt_path_offset(const void *fdt, const char *path)
 
 const char *fdt_get_name(const void *fdt, int nodeoffset, int *len)
 {
-       const struct fdt_node_header *nh = _fdt_offset_ptr(fdt, nodeoffset);
+       const struct fdt_node_header *nh = fdt_offset_ptr_(fdt, nodeoffset);
        int err;
 
        if (((err = fdt_check_header(fdt)) != 0)
-           || ((err = _fdt_check_node_offset(fdt, nodeoffset)) < 0))
+           || ((err = fdt_check_node_offset_(fdt, nodeoffset)) < 0))
                        goto fail;
 
        if (len)
@@ -233,7 +264,7 @@ int fdt_first_property_offset(const void *fdt, int nodeoffset)
 {
        int offset;
 
-       if ((offset = _fdt_check_node_offset(fdt, nodeoffset)) < 0)
+       if ((offset = fdt_check_node_offset_(fdt, nodeoffset)) < 0)
                return offset;
 
        return _nextprop(fdt, offset);
@@ -241,7 +272,7 @@ int fdt_first_property_offset(const void *fdt, int nodeoffset)
 
 int fdt_next_property_offset(const void *fdt, int offset)
 {
-       if ((offset = _fdt_check_prop_offset(fdt, offset)) < 0)
+       if ((offset = fdt_check_prop_offset_(fdt, offset)) < 0)
                return offset;
 
        return _nextprop(fdt, offset);
@@ -254,13 +285,13 @@ const struct fdt_property *fdt_get_property_by_offset(const void *fdt,
        int err;
        const struct fdt_property *prop;
 
-       if ((err = _fdt_check_prop_offset(fdt, offset)) < 0) {
+       if ((err = fdt_check_prop_offset_(fdt, offset)) < 0) {
                if (lenp)
                        *lenp = err;
                return NULL;
        }
 
-       prop = _fdt_offset_ptr(fdt, offset);
+       prop = fdt_offset_ptr_(fdt, offset);
 
        if (lenp)
                *lenp = fdt32_to_cpu(prop->len);
@@ -576,7 +607,7 @@ int fdt_stringlist_search(const void *fdt, int nodeoffset, const char *property,
 
        list = fdt_getprop(fdt, nodeoffset, property, &length);
        if (!list)
-               return -length;
+               return length;
 
        len = strlen(string) + 1;
        end = list + length;