diff --git a/sys/dev/fdt/fdt_subr.c b/sys/dev/fdt/fdt_subr.c index 473221636d71..991c10092543 100644 --- a/sys/dev/fdt/fdt_subr.c +++ b/sys/dev/fdt/fdt_subr.c @@ -1,4 +1,4 @@ -/* $NetBSD: fdt_subr.c,v 1.6 2017/04/13 22:12:53 jmcneill Exp $ */ +/* $NetBSD: fdt_subr.c,v 1.7 2017/04/21 21:08:57 jmcneill Exp $ */ /*- * Copyright (c) 2015 Jared D. McNeill @@ -27,7 +27,7 @@ */ #include -__KERNEL_RCSID(0, "$NetBSD: fdt_subr.c,v 1.6 2017/04/13 22:12:53 jmcneill Exp $"); +__KERNEL_RCSID(0, "$NetBSD: fdt_subr.c,v 1.7 2017/04/21 21:08:57 jmcneill Exp $"); #include #include @@ -161,7 +161,7 @@ fdtbus_get_reg(int phandle, u_int index, bus_addr_t *paddr, bus_size_t *psize) { bus_addr_t addr; bus_size_t size; - uint8_t *buf; + const uint8_t *buf; int len; const int addr_cells = fdtbus_get_addr_cells(phandle); @@ -169,25 +169,18 @@ fdtbus_get_reg(int phandle, u_int index, bus_addr_t *paddr, bus_size_t *psize) if (addr_cells == -1 || size_cells == -1) return -1; + buf = fdt_getprop(fdtbus_get_data(), + fdtbus_phandle2offset(phandle), "reg", &len); + if (buf == NULL || len <= 0) + return -1; + const u_int reglen = size_cells * 4 + addr_cells * 4; if (reglen == 0) return -1; - len = OF_getproplen(phandle, "reg"); - if (len <= 0) + if (index >= len / reglen) return -1; - const u_int nregs = len / reglen; - - if (index >= nregs) - return -1; - - buf = kmem_alloc(len, KM_SLEEP); - if (buf == NULL) - return -1; - - len = OF_getprop(phandle, "reg", buf, len); - switch (addr_cells) { case 0: addr = 0; @@ -221,7 +214,5 @@ fdtbus_get_reg(int phandle, u_int index, bus_addr_t *paddr, bus_size_t *psize) if (psize) *psize = size; - kmem_free(buf, len); - return 0; }