Commit 8e9685715bf78421da48a84040aba3801f66bf47

Authored by Maxime Ripard
Committed by Tom Rini
1 parent 6f5f92c60b

libfdt: Add fdt_path_offset_namelen

Add a namelen variant of fdt_path_offset to retrieve the node offset using
only a fixed number of characters.

Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Maxime Ripard <maxime.ripard@free-electrons.com>

Showing 2 changed files with 25 additions and 9 deletions Side-by-side Diff

... ... @@ -366,6 +366,17 @@
366 366 int fdt_subnode_offset(const void *fdt, int parentoffset, const char *name);
367 367  
368 368 /**
  369 + * fdt_path_offset_namelen - find a tree node based on substring
  370 + * @fdt: pointer to the device tree blob
  371 + * @path: full path of the node to locate
  372 + * @namelen: number of characters of name to consider
  373 + *
  374 + * Identical to fdt_path_offset(), but only examine the first
  375 + * namelen characters of path for matching the node path.
  376 + */
  377 +int fdt_path_offset_namelen(const void *fdt, const char *path, int namelen);
  378 +
  379 +/**
369 380 * fdt_path_offset - find a tree node by its full path
370 381 * @fdt: pointer to the device tree blob
371 382 * @path: full path of the node to locate
... ... @@ -387,7 +398,10 @@
387 398 * -FDT_ERR_BADSTRUCTURE,
388 399 * -FDT_ERR_TRUNCATED, standard meanings.
389 400 */
390   -int fdt_path_offset(const void *fdt, const char *path);
  401 +static inline int fdt_path_offset(const void *fdt, const char *path)
  402 +{
  403 + return fdt_path_offset_namelen(fdt, path, strlen(path));
  404 +}
391 405  
392 406 /**
393 407 * fdt_get_name - retrieve the name of a given node
... ... @@ -145,10 +145,10 @@
145 145 * "foo/bar:option" and "bar:option/otheroption", both of which happen, so
146 146 * first searching for either ':' or '/' does not work.
147 147 */
148   -static const char *fdt_path_next_separator(const char *path)
  148 +static const char *fdt_path_next_separator(const char *path, int len)
149 149 {
150   - const char *sep1 = strchr(path, '/');
151   - const char *sep2 = strchr(path, ':');
  150 + const void *sep1 = memchr(path, '/', len);
  151 + const void *sep2 = memchr(path, ':', len);
152 152  
153 153 if (sep1 && sep2)
154 154 return (sep1 < sep2) ? sep1 : sep2;
155 155  
... ... @@ -158,9 +158,9 @@
158 158 return sep2;
159 159 }
160 160  
161   -int fdt_path_offset(const void *fdt, const char *path)
  161 +int fdt_path_offset_namelen(const void *fdt, const char *path, int namelen)
162 162 {
163   - const char *end = path + strlen(path);
  163 + const char *end = path + namelen;
164 164 const char *p = path;
165 165 int offset = 0;
166 166  
... ... @@ -168,7 +168,7 @@
168 168  
169 169 /* see if we have an alias */
170 170 if (*path != '/') {
171   - const char *q = fdt_path_next_separator(path);
  171 + const char *q = fdt_path_next_separator(path, namelen);
172 172  
173 173 if (!q)
174 174 q = end;
175 175  
176 176  
... ... @@ -181,14 +181,16 @@
181 181 p = q;
182 182 }
183 183  
184   - while (*p) {
  184 + while (*p && (p < end)) {
185 185 const char *q;
186 186  
187 187 while (*p == '/')
188 188 p++;
  189 +
189 190 if (*p == '\0' || *p == ':')
190 191 return offset;
191   - q = fdt_path_next_separator(p);
  192 +
  193 + q = fdt_path_next_separator(p, end - p);
192 194 if (!q)
193 195 q = end;
194 196