Sun Jan 24 15:43:23 2021 UTC ()
Use strlist(9).


(thorpej)
diff -r1.38 -r1.39 src/sys/dev/fdt/fdt_subr.c

cvs diff -r1.38 -r1.39 src/sys/dev/fdt/fdt_subr.c (expand / switch to unified diff)

--- src/sys/dev/fdt/fdt_subr.c 2020/07/16 16:39:18 1.38
+++ src/sys/dev/fdt/fdt_subr.c 2021/01/24 15:43:22 1.39
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: fdt_subr.c,v 1.38 2020/07/16 16:39:18 jmcneill Exp $ */ 1/* $NetBSD: fdt_subr.c,v 1.39 2021/01/24 15:43:22 thorpej Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 2015 Jared D. McNeill <jmcneill@invisible.ca> 4 * Copyright (c) 2015 Jared D. McNeill <jmcneill@invisible.ca>
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
@@ -17,27 +17,27 @@ @@ -17,27 +17,27 @@
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE. 26 * SUCH DAMAGE.
27 */ 27 */
28 28
29#include <sys/cdefs.h> 29#include <sys/cdefs.h>
30__KERNEL_RCSID(0, "$NetBSD: fdt_subr.c,v 1.38 2020/07/16 16:39:18 jmcneill Exp $"); 30__KERNEL_RCSID(0, "$NetBSD: fdt_subr.c,v 1.39 2021/01/24 15:43:22 thorpej Exp $");
31 31
32#include "opt_fdt.h" 32#include "opt_fdt.h"
33 33
34#include <sys/param.h> 34#include <sys/param.h>
35#include <sys/bus.h> 35#include <sys/bus.h>
36 36
37#include <libfdt.h> 37#include <libfdt.h>
38#include <dev/fdt/fdtvar.h> 38#include <dev/fdt/fdtvar.h>
39#include <dev/fdt/fdt_private.h> 39#include <dev/fdt/fdt_private.h>
40 40
41#ifndef FDT_DEFAULT_STDOUT_PATH 41#ifndef FDT_DEFAULT_STDOUT_PATH
42#define FDT_DEFAULT_STDOUT_PATH "serial0:115200n8" 42#define FDT_DEFAULT_STDOUT_PATH "serial0:115200n8"
43#endif 43#endif
@@ -492,52 +492,41 @@ const char * @@ -492,52 +492,41 @@ const char *
492fdtbus_get_string(int phandle, const char *prop) 492fdtbus_get_string(int phandle, const char *prop)
493{ 493{
494 const int off = fdtbus_phandle2offset(phandle); 494 const int off = fdtbus_phandle2offset(phandle);
495 495
496 if (strcmp(prop, "name") == 0) 496 if (strcmp(prop, "name") == 0)
497 return fdt_get_name(fdtbus_get_data(), off, NULL); 497 return fdt_get_name(fdtbus_get_data(), off, NULL);
498 else 498 else
499 return fdt_getprop(fdtbus_get_data(), off, prop, NULL); 499 return fdt_getprop(fdtbus_get_data(), off, prop, NULL);
500} 500}
501 501
502const char * 502const char *
503fdtbus_get_string_index(int phandle, const char *prop, u_int index) 503fdtbus_get_string_index(int phandle, const char *prop, u_int index)
504{ 504{
505 const char *names, *name; 505 const char *names;
506 int len, cur; 506 int len;
507 507
508 if ((len = OF_getproplen(phandle, prop)) < 0) 508 if ((len = OF_getproplen(phandle, prop)) < 0)
509 return NULL; 509 return NULL;
510 510
511 names = fdtbus_get_string(phandle, prop); 511 names = fdtbus_get_string(phandle, prop);
512 512
513 for (name = names, cur = 0; len > 0; 513 return strlist_string(names, len, index);
514 len -= strlen(name) + 1, name += strlen(name) + 1, cur++) { 
515 if (index == cur) 
516 return name; 
517 } 
518 
519 return NULL; 
520} 514}
521 515
522int 516int
523fdtbus_get_index(int phandle, const char *prop, const char *name, u_int *idx) 517fdtbus_get_index(int phandle, const char *prop, const char *name, u_int *idx)
524{ 518{
525 const char *p; 519 const char *p;
526 size_t pl; 520 int len, index;
527 u_int index; 
528 int len; 
529 521
530 p = fdtbus_get_prop(phandle, prop, &len); 522 p = fdtbus_get_prop(phandle, prop, &len);
531 if (p == NULL || len <= 0) 523 if (p == NULL || len <= 0)
532 return -1; 524 return -1;
533 525
534 for (index = 0; len > 0; 526 index = strlist_index(p, len, name);
535 pl = strlen(p) + 1, len -= pl, p += pl, index++) { 527 if (index == -1)
536 if (strcmp(p, name) == 0) { 528 return -1;
537 *idx = index; 
538 return 0; 
539 } 
540 } 
541 529
542 return -1; 530 *idx = index;
 531 return 0;
543} 532}