aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--common/debug.c2
-rw-r--r--common/pmem.c20
-rw-r--r--include/libfdt.h34
3 files changed, 39 insertions, 17 deletions
diff --git a/common/debug.c b/common/debug.c
index 7118733..ff9dc18 100644
--- a/common/debug.c
+++ b/common/debug.c
@@ -201,7 +201,7 @@ static struct dbg_info __dbg_from_fdt(const void *fdt)
/* get serial device address */
struct cell_info ci = get_reginfo(fdt, stdout);
void *reg_ptr = (void *)fdt_getprop(fdt, stdout_offset, "reg", NULL);
- pm_t dbg_ptr = (pm_t)fdt_load_int_ptr(ci.addr_cells, reg_ptr);
+ pm_t dbg_ptr = (pm_t)fdt_load_reg_addr(ci, reg_ptr, 0);
/* get serial device offset if present */
size_t shift = 0;
diff --git a/common/pmem.c b/common/pmem.c
index 83c6ca3..80599ef 100644
--- a/common/pmem.c
+++ b/common/pmem.c
@@ -534,17 +534,11 @@ static void __mark_reserved_mem(void *fdt)
uint8_t *rmem_reg =
(uint8_t *)fdt_getprop(fdt, node, "reg", NULL);
- pm_t base = (pm_t)fdt_load_int_ptr(ci.addr_cells, rmem_reg);
-
- if (ci.addr_cells == 2)
- rmem_reg += sizeof(fdt64_t);
- else
- rmem_reg += sizeof(fdt32_t);
+ pm_t base = (pm_t)fdt_load_reg_addr(ci, rmem_reg, 0);
/** @todo make sure the top of a reserved memory area doesn't go
* against our assumptions in FW_MAX_SIZE? */
- pm_t top =
- (pm_t)fdt_load_int_ptr(ci.size_cells, rmem_reg) + base;
+ pm_t top = (pm_t)fdt_load_reg_size(ci, rmem_reg, 0) + base;
__mark_area_used((pm_t)__va(base), (pm_t)__va(top));
info("marked [%lx - %lx] reserved\n",
(pm_t)__va(base), (pm_t)__va(top));
@@ -563,14 +557,8 @@ static pm_t __get_ramtop(void *fdt)
int mem_offset = fdt_path_offset(fdt, "/memory");
uint8_t *mem_reg = (uint8_t *)fdt_getprop(fdt, mem_offset, "reg", NULL);
- pm_t base = (pm_t)fdt_load_int_ptr(ci.addr_cells, mem_reg);
-
- if (ci.addr_cells == 2)
- mem_reg += sizeof(fdt64_t);
- else
- mem_reg += sizeof(fdt32_t);
-
- return (pm_t)fdt_load_int_ptr(ci.size_cells, mem_reg) + base;
+ pm_t base = (pm_t)fdt_load_reg_addr(ci, mem_reg, 0);
+ return (pm_t)fdt_load_reg_size(ci, mem_reg, 0) + base;
}
/**
diff --git a/include/libfdt.h b/include/libfdt.h
index 37247a3..58173f9 100644
--- a/include/libfdt.h
+++ b/include/libfdt.h
@@ -11,6 +11,7 @@
#include "../dtc/libfdt/libfdt.h"
#include <kmi/unaligned.h>
+#include <kmi/assert.h>
#include <kmi/types.h>
/* kmi additions, implementation can be found in common/fdt.c */
@@ -95,4 +96,37 @@ void __dbg_fdt(const void *fdt, int node_offset, int depth);
#define fdt_load_int_ptr(c, p) \
((c) == 2 ? fdt_load_int64_ptr(p) : fdt_load_int32_ptr(p))
+/**
+ * Helper for reading address value from reg nodes.
+ * No larger than 64 bit values allowed.
+ *
+ * @param ci Cell info.
+ * @param p Pointer to node.
+ * @param i Index of address to read.
+ * @return Address in reg cell.
+ */
+static inline fdt64_t fdt_load_reg_addr(struct cell_info ci, void *p, size_t i)
+{
+ hard_assert(ci.addr_cells == 2 || ci.addr_cells == 1, 0);
+ size_t offset = i * (ci.addr_cells + ci.size_cells) * sizeof(fdt32_t);
+ char *addr = ((char *)p) + 0;
+ return fdt_load_int_ptr(ci.addr_cells, addr + offset);
+}
+
+/**
+ * Helper for reading the size cell of a reg node.
+ *
+ * @param ci Cell info.
+ * @param p Pointer to node.
+ * @param i Index of size to read.
+ * @return Size in reg cell.
+ */
+static inline fdt64_t fdt_load_reg_size(struct cell_info ci, void *p, size_t i)
+{
+ hard_assert(ci.size_cells == 2 || ci.size_cells == 1, 0);
+ size_t offset = i * (ci.addr_cells + ci.size_cells) * sizeof(fdt32_t);
+ char *addr = ((char *)p) + ci.addr_cells * sizeof(fdt32_t);
+ return fdt_load_int_ptr(ci.size_cells, addr + offset);
+}
+
#endif