Genericize finding of memory nodes in the FDT.
Change-Id: Idab2453578e9d79ca6fff889febbb1c3e8838e95
diff --git a/inc/hf/fdt_handler.h b/inc/hf/fdt_handler.h
index 70521b5..4e18afc 100644
--- a/inc/hf/fdt_handler.h
+++ b/inc/hf/fdt_handler.h
@@ -20,6 +20,7 @@
#include "hf/fdt.h"
#include "hf/mm.h"
#include "hf/mpool.h"
+#include "hf/string.h"
struct fdt_header *fdt_map(struct mm_stage1_locked stage1_locked,
paddr_t fdt_addr, struct fdt_node *n,
@@ -28,7 +29,10 @@
struct mpool *ppool);
bool fdt_find_cpus(const struct fdt_node *root, cpu_id_t *cpu_ids,
size_t *cpu_count);
-bool fdt_find_memory_ranges(const struct fdt_node *root, struct boot_params *p);
+bool fdt_find_memory_ranges(const struct fdt_node *root,
+ struct string *device_type,
+ struct mem_range *mem_ranges,
+ size_t *mem_ranges_count, size_t mem_range_limit);
bool fdt_find_initrd(const struct fdt_node *root, paddr_t *begin, paddr_t *end);
/** Apply an update to the FDT. */
diff --git a/src/boot_flow/common.c b/src/boot_flow/common.c
index c256eac..597c421 100644
--- a/src/boot_flow/common.c
+++ b/src/boot_flow/common.c
@@ -25,13 +25,16 @@
bool boot_flow_get_params(struct boot_params *p,
const struct fdt_node *fdt_root)
{
+ struct string memory = STRING_INIT("memory");
+
p->mem_ranges_count = 0;
p->kernel_arg = plat_boot_flow_get_kernel_arg();
return plat_boot_flow_get_initrd_range(fdt_root, &p->initrd_begin,
&p->initrd_end) &&
fdt_find_cpus(fdt_root, p->cpu_ids, &p->cpu_count) &&
- fdt_find_memory_ranges(fdt_root, p);
+ fdt_find_memory_ranges(fdt_root, &memory, p->mem_ranges,
+ &p->mem_ranges_count, MAX_MEM_RANGES);
}
/**
diff --git a/src/fdt_handler.c b/src/fdt_handler.c
index ada6a15..8af1401 100644
--- a/src/fdt_handler.c
+++ b/src/fdt_handler.c
@@ -168,7 +168,10 @@
return true;
}
-bool fdt_find_memory_ranges(const struct fdt_node *root, struct boot_params *p)
+bool fdt_find_memory_ranges(const struct fdt_node *root,
+ struct string *device_type,
+ struct mem_range *mem_ranges,
+ size_t *mem_ranges_count, size_t mem_range_limit)
{
struct fdt_node n = *root;
const char *name;
@@ -202,8 +205,8 @@
uint32_t size;
if (!fdt_read_property(&n, "device_type", &data, &size) ||
- size != sizeof("memory") ||
- memcmp(data, "memory", sizeof("memory")) != 0 ||
+ strncmp(data, string_data(device_type), STRING_MAX_SIZE) !=
+ 0 ||
!fdt_read_property(&n, "reg", &data, &size)) {
continue;
}
@@ -217,27 +220,26 @@
CHECK(fdt_parse_number(data + address_size, size_size,
&len));
- if (mem_range_index < MAX_MEM_RANGES) {
- p->mem_ranges[mem_range_index].begin =
+ if (mem_range_index < mem_range_limit) {
+ mem_ranges[mem_range_index].begin =
pa_init(addr);
- p->mem_ranges[mem_range_index].end =
+ mem_ranges[mem_range_index].end =
pa_init(addr + len);
++mem_range_index;
} else {
dlog_error(
- "Found memory range %u in FDT but only "
- "%u supported, ignoring additional "
- "range of size %u.\n",
- mem_range_index, MAX_MEM_RANGES, len);
+ "Found %s range %u in FDT but only %u "
+ "supported, ignoring additional range "
+ "of size %u.\n",
+ string_data(device_type),
+ mem_range_index, mem_range_limit, len);
}
size -= entry_size;
data += entry_size;
}
} while (fdt_next_sibling(&n, &name));
- p->mem_ranges_count = mem_range_index;
-
- /* TODO: Check for "reserved-memory" nodes. */
+ *mem_ranges_count = mem_range_index;
return true;
}
diff --git a/src/fdt_handler_test.cc b/src/fdt_handler_test.cc
index acd355e..8788f77 100644
--- a/src/fdt_handler_test.cc
+++ b/src/fdt_handler_test.cc
@@ -106,11 +106,13 @@
struct boot_params params = {};
struct mm_stage1_locked mm_stage1_locked = mm_lock_stage1();
+ struct string memory = STRING_INIT("memory");
fdt = fdt_map(mm_stage1_locked, pa_init((uintpaddr_t)&test_dtb), &n,
&ppool);
ASSERT_THAT(fdt, NotNull());
ASSERT_TRUE(fdt_find_child(&n, ""));
- fdt_find_memory_ranges(&n, ¶ms);
+ fdt_find_memory_ranges(&n, &memory, params.mem_ranges,
+ ¶ms.mem_ranges_count, MAX_MEM_RANGES);
ASSERT_TRUE(fdt_unmap(mm_stage1_locked, fdt, &ppool));
mm_unlock_stage1(&mm_stage1_locked);