]> git.sur5r.net Git - u-boot/blobdiff - libfdt/fdt_ro.c
OMAP3 Move cache routine to cache.S
[u-boot] / libfdt / fdt_ro.c
index b09a6e9eb7e5976333d6e4626f67102074dabdd8..1e1e32209ce71d3087941bde94381f30932c7b23 100644 (file)
@@ -117,15 +117,14 @@ int fdt_subnode_offset_namelen(const void *fdt, int offset,
        FDT_CHECK_HEADER(fdt);
 
        for (depth = 0;
-            offset >= 0;
-            offset = fdt_next_node(fdt, offset, &depth)) {
-               if (depth < 0)
-                       return -FDT_ERR_NOTFOUND;
-               else if ((depth == 1)
-                        && _fdt_nodename_eq(fdt, offset, name, namelen))
+            (offset >= 0) && (depth >= 0);
+            offset = fdt_next_node(fdt, offset, &depth))
+               if ((depth == 1)
+                   && _fdt_nodename_eq(fdt, offset, name, namelen))
                        return offset;
-       }
 
+       if (depth < 0)
+               return -FDT_ERR_NOTFOUND;
        return offset; /* error */
 }
 
@@ -145,17 +144,12 @@ int fdt_path_offset(const void *fdt, const char *path)
 
        /* see if we have an alias */
        if (*path != '/') {
-               const char *q;
-               int aliasoffset = fdt_path_offset(fdt, "/aliases");
-
-               if (aliasoffset < 0)
-                       return -FDT_ERR_BADPATH;
+               const char *q = strchr(path, '/');
 
-               q = strchr(path, '/');
                if (!q)
                        q = end;
 
-               p = fdt_getprop_namelen(fdt, aliasoffset, path, q - p, NULL);
+               p = fdt_get_alias_namelen(fdt, p, q - p);
                if (!p)
                        return -FDT_ERR_BADPATH;
                offset = fdt_path_offset(fdt, p);
@@ -211,7 +205,6 @@ const struct fdt_property *fdt_get_property_namelen(const void *fdt,
 {
        uint32_t tag;
        const struct fdt_property *prop;
-       int namestroff;
        int offset, nextoffset;
        int err;
 
@@ -226,38 +219,24 @@ const struct fdt_property *fdt_get_property_namelen(const void *fdt,
                tag = fdt_next_tag(fdt, offset, &nextoffset);
                switch (tag) {
                case FDT_END:
-                       err = -FDT_ERR_TRUNCATED;
+                       if (nextoffset < 0)
+                               err = nextoffset;
+                       else
+                               /* FDT_END tag with unclosed nodes */
+                               err = -FDT_ERR_BADSTRUCTURE;
                        goto fail;
 
-               case FDT_BEGIN_NODE:
-               case FDT_END_NODE:
-               case FDT_NOP:
-                       break;
-
                case FDT_PROP:
-                       err = -FDT_ERR_BADSTRUCTURE;
-                       prop = fdt_offset_ptr(fdt, offset, sizeof(*prop));
-                       if (! prop)
-                               goto fail;
-                       namestroff = fdt32_to_cpu(prop->nameoff);
-                       if (_fdt_string_eq(fdt, namestroff, name, namelen)) {
+                       prop = _fdt_offset_ptr(fdt, offset);
+                       if (_fdt_string_eq(fdt, fdt32_to_cpu(prop->nameoff),
+                                          name, namelen)) {
                                /* Found it! */
-                               int len = fdt32_to_cpu(prop->len);
-                               prop = fdt_offset_ptr(fdt, offset,
-                                                     sizeof(*prop)+len);
-                               if (! prop)
-                                       goto fail;
-
                                if (lenp)
-                                       *lenp = len;
+                                       *lenp = fdt32_to_cpu(prop->len);
 
                                return prop;
                        }
                        break;
-
-               default:
-                       err = -FDT_ERR_BADSTRUCTURE;
-                       goto fail;
                }
        } while ((tag != FDT_BEGIN_NODE) && (tag != FDT_END_NODE));
 
@@ -306,6 +285,23 @@ uint32_t fdt_get_phandle(const void *fdt, int nodeoffset)
        return fdt32_to_cpu(*php);
 }
 
+const char *fdt_get_alias_namelen(const void *fdt,
+                                 const char *name, int namelen)
+{
+       int aliasoffset;
+
+       aliasoffset = fdt_path_offset(fdt, "/aliases");
+       if (aliasoffset < 0)
+               return NULL;
+
+       return fdt_getprop_namelen(fdt, aliasoffset, name, namelen, NULL);
+}
+
+const char *fdt_get_alias(const void *fdt, const char *name)
+{
+       return fdt_get_alias_namelen(fdt, name, strlen(name));
+}
+
 int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen)
 {
        int pdepth = 0, p = 0;
@@ -320,9 +316,6 @@ int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen)
        for (offset = 0, depth = 0;
             (offset >= 0) && (offset <= nodeoffset);
             offset = fdt_next_node(fdt, offset, &depth)) {
-               if (pdepth < depth)
-                       continue; /* overflowed buffer */
-
                while (pdepth > depth) {
                        do {
                                p--;
@@ -330,14 +323,16 @@ int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen)
                        pdepth--;
                }
 
-               name = fdt_get_name(fdt, offset, &namelen);
-               if (!name)
-                       return namelen;
-               if ((p + namelen + 1) <= buflen) {
-                       memcpy(buf + p, name, namelen);
-                       p += namelen;
-                       buf[p++] = '/';
-                       pdepth++;
+               if (pdepth >= depth) {
+                       name = fdt_get_name(fdt, offset, &namelen);
+                       if (!name)
+                               return namelen;
+                       if ((p + namelen + 1) <= buflen) {
+                               memcpy(buf + p, name, namelen);
+                               p += namelen;
+                               buf[p++] = '/';
+                               pdepth++;
+                       }
                }
 
                if (offset == nodeoffset) {
@@ -347,7 +342,7 @@ int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen)
                        if (p > 1) /* special case so that root path is "/", not "" */
                                p--;
                        buf[p] = '\0';
-                       return p;
+                       return 0;
                }
        }