mirror of
https://github.com/Dasharo/coreboot.git
synced 2026-06-13 10:16:48 -07:00
lib/device_tree: Add some FDT helper functions
This adds some helper functions for FDT, since more and more mainboards seem to need FDT nowadays. For example our QEMU boards need it in order to know how much RAM is available. Also all RISC-V boards in our tree need FDT. This also adds some tests in order to test said functions. Signed-off-by: Maximilian Brune <maximilian.brune@9elements.com> Change-Id: I2fb1d93c5b3e1cb2f7d9584db52bbce3767b63d8 Reviewed-on: https://review.coreboot.org/c/coreboot/+/81081 Reviewed-by: Julius Werner <jwerner@chromium.org> Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
This commit is contained in:
committed by
Lean Sheng Tan
parent
25c737d403
commit
33079b8174
@@ -4,6 +4,7 @@
|
||||
#ifndef __DEVICE_TREE_H__
|
||||
#define __DEVICE_TREE_H__
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <commonlib/list.h>
|
||||
@@ -33,6 +34,7 @@ struct fdt_header {
|
||||
#define FDT_TOKEN_BEGIN_NODE 1
|
||||
#define FDT_TOKEN_END_NODE 2
|
||||
#define FDT_TOKEN_PROPERTY 3
|
||||
#define FDT_TOKEN_NOP 4
|
||||
#define FDT_TOKEN_END 9
|
||||
#define FDT_PHANDLE_ILLEGAL 0xdeadbeef
|
||||
|
||||
@@ -47,6 +49,11 @@ struct fdt_property
|
||||
* Unflattened device tree structures.
|
||||
*/
|
||||
|
||||
struct device_tree_region {
|
||||
u64 addr;
|
||||
u64 size;
|
||||
};
|
||||
|
||||
struct device_tree_property
|
||||
{
|
||||
struct fdt_property prop;
|
||||
@@ -91,6 +98,8 @@ struct device_tree
|
||||
* which were consumed reading the requested value.
|
||||
*/
|
||||
|
||||
/* Checks if blob points to a valid FDT */
|
||||
bool fdt_is_valid(const void *blob);
|
||||
/* Read the property at offset, if any exists. */
|
||||
int fdt_next_property(const void *blob, uint32_t offset,
|
||||
struct fdt_property *prop);
|
||||
@@ -100,6 +109,26 @@ int fdt_node_name(const void *blob, uint32_t offset, const char **name);
|
||||
void fdt_print_node(const void *blob, uint32_t offset);
|
||||
int fdt_skip_node(const void *blob, uint32_t offset);
|
||||
|
||||
/* Read property and put into fdt_prop. Returns offset to property */
|
||||
u32 fdt_read_prop(const void *blob, u32 node_offset, const char *prop_name,
|
||||
struct fdt_property *fdt_prop);
|
||||
/* Read reg property and save regions inside 'regions'. Returns number of regions read */
|
||||
u32 fdt_read_reg_prop(const void *blob, u32 node_offset, u32 addr_cells, u32 size_cells,
|
||||
struct device_tree_region regions[], size_t regions_count);
|
||||
/* Find a node by a given path and return the offset */
|
||||
u32 fdt_find_node_by_path(const void *blob, const char *path, u32 *addrcp, u32 *sizecp);
|
||||
/* Find multiple nodes matching a given pattern. Returns number of nodes found */
|
||||
size_t fdt_find_subnodes_by_prefix(const void *blob, u32 node_offset, const char *prefix,
|
||||
u32 *addrcp, u32 *sizecp, u32 results[], size_t results_len);
|
||||
/* Find a node by a given alias and return its offset */
|
||||
u32 fdt_find_node_by_alias(const void *blob, const char *alias_name,
|
||||
u32 *addr_cells, u32 *size_cells);
|
||||
/*
|
||||
* Read the node name into 'name' of the node behind 'node_offset'
|
||||
* and return total bytes used for name
|
||||
*/
|
||||
int fdt_next_node_name(const void *blob, uint32_t node_offset, const char **name);
|
||||
|
||||
/* Read a flattened device tree into a hierarchical structure which refers to
|
||||
the contents of the flattened tree in place. Modifying the flat tree
|
||||
invalidates the unflattened one. */
|
||||
|
||||
+395
-47
File diff suppressed because it is too large
Load Diff
Binary file not shown.
@@ -39,9 +39,15 @@ tests-y += cbfs-lookup-no-mcache-test
|
||||
tests-y += cbfs-lookup-has-mcache-test
|
||||
tests-y += lzma-test
|
||||
tests-y += ux_locales-test
|
||||
tests-y += device_tree-test
|
||||
|
||||
lib-test-srcs += tests/lib/lib-test.c
|
||||
|
||||
device_tree-test-srcs += tests/lib/device_tree-test.c
|
||||
device_tree-test-srcs += tests/stubs/console.c
|
||||
device_tree-test-srcs += src/lib/device_tree.c
|
||||
device_tree-test-syssrcs += tests/helpers/file.c
|
||||
|
||||
string-test-srcs += tests/lib/string-test.c
|
||||
string-test-srcs += src/lib/string.c
|
||||
|
||||
|
||||
@@ -0,0 +1,132 @@
|
||||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
|
||||
#include <console/console.h>
|
||||
#include <device_tree.h>
|
||||
#include <helpers/file.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <tests/test.h>
|
||||
|
||||
static int setup_device_tree_test_group(void **state)
|
||||
{
|
||||
/*
|
||||
* fattest FDT I could find from Linux Kernel to test the worst cases
|
||||
* https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/arch/arm/boot/dts/nvidia/tegra30-ouya.dts?id=443b349019f2d946
|
||||
*/
|
||||
const char dtb_path[] = "lib/devicetree-test/tegra30-ouya.dtb";
|
||||
int file_size = test_get_file_size(dtb_path);
|
||||
assert_int_not_equal(file_size, -1);
|
||||
|
||||
void *big_fat_dtb = test_malloc(file_size);
|
||||
assert_int_not_equal(big_fat_dtb, NULL);
|
||||
assert_int_equal(test_read_file(dtb_path, big_fat_dtb, file_size), file_size);
|
||||
*state = big_fat_dtb;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int teardown_device_tree_test_group(void **state)
|
||||
{
|
||||
test_free(*state);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void test_fdt_find_node_by_path(void **state)
|
||||
{
|
||||
uint32_t addrcp, sizecp;
|
||||
assert_int_equal(0, fdt_find_node_by_path(*state, "test", &addrcp, &sizecp));
|
||||
assert_int_equal(56, fdt_find_node_by_path(*state, "/", &addrcp, &sizecp));
|
||||
assert_int_equal(2, addrcp);
|
||||
assert_int_equal(1, sizecp);
|
||||
assert_int_equal(0, fdt_find_node_by_path(*state, "/test", &addrcp, &sizecp));
|
||||
assert_int_equal(0x181f4, fdt_find_node_by_path(*state, "/chosen", &addrcp, &sizecp));
|
||||
assert_int_equal(1, addrcp);
|
||||
assert_int_equal(1, sizecp);
|
||||
assert_int_equal(0x156d4, fdt_find_node_by_path(*state, "/cpus", &addrcp, &sizecp));
|
||||
assert_int_equal(1, addrcp);
|
||||
assert_int_equal(1, sizecp);
|
||||
assert_int_equal(0x1517c, fdt_find_node_by_path(*state, "/usb@7d004000/ethernet@2", &addrcp, &sizecp));
|
||||
assert_int_equal(1, addrcp);
|
||||
assert_int_equal(0, sizecp);
|
||||
assert_int_equal(0x1517c, fdt_find_node_by_path(*state, "/usb@7d004000/ethernet@2/", &addrcp, &sizecp));
|
||||
assert_int_equal(1, addrcp);
|
||||
assert_int_equal(0, sizecp);
|
||||
|
||||
assert_int_equal(0xee08, fdt_find_node_by_path(*state, "/pinmux@70000868/pinmux/drive_groups",
|
||||
&addrcp, &sizecp));
|
||||
}
|
||||
|
||||
static void test_fdt_find_subnodes_by_prefix(void **state)
|
||||
{
|
||||
uint32_t offset = fdt_find_node_by_path(*state, "/cpus", NULL, NULL);
|
||||
uint32_t results[3] = { 0 };
|
||||
uint32_t addrcp, sizecp;
|
||||
size_t count_results = fdt_find_subnodes_by_prefix(*state, offset, "cpu@",
|
||||
&addrcp, &sizecp, results, 3);
|
||||
assert_int_equal(3, count_results);
|
||||
assert_int_equal(0x15700, results[0]);
|
||||
assert_int_equal(0x157a0, results[1]);
|
||||
assert_int_equal(0x15840, results[2]);
|
||||
|
||||
results[1] = 0xDEADBEEF;
|
||||
results[2] = 0xDEADBEEF;
|
||||
count_results = fdt_find_subnodes_by_prefix(*state, offset, "cpu@",
|
||||
&addrcp, &sizecp, results, 1);
|
||||
assert_int_equal(1, count_results);
|
||||
assert_int_equal(0x15700, results[0]);
|
||||
assert_int_equal(0xDEADBEEF, results[1]);
|
||||
assert_int_equal(0xDEADBEEF, results[2]);
|
||||
}
|
||||
|
||||
static void test_fdt_find_node_by_alias(void **state)
|
||||
{
|
||||
assert_int_equal(0xf298, fdt_find_node_by_alias(*state, "serial0", NULL, NULL));
|
||||
assert_int_equal(0, fdt_find_node_by_alias(*state, "mmc2", NULL, NULL));
|
||||
}
|
||||
|
||||
static void test_fdt_find_prop_in_node(void **state)
|
||||
{
|
||||
uintptr_t cnode_offset = fdt_find_node_by_path(*state, "/clock", NULL, NULL);
|
||||
uintptr_t mnode_offset = fdt_find_node_by_path(*state, "/memory@80000000", NULL, NULL);
|
||||
assert_int_equal(0x18400, cnode_offset);
|
||||
|
||||
struct fdt_property fdt_prop;
|
||||
assert_int_equal(0x1840c, fdt_read_prop(*state, cnode_offset, "compatible", &fdt_prop));
|
||||
assert_string_equal("fixed-clock", (char *)fdt_prop.data);
|
||||
assert_int_equal(0x6094, fdt_read_prop(*state, mnode_offset, "reg", &fdt_prop));
|
||||
assert_int_equal(0x0, fdt_read_prop(*state, cnode_offset, "notfound", &fdt_prop));
|
||||
}
|
||||
|
||||
static void test_fdt_read_reg_prop(void **state)
|
||||
{
|
||||
uint32_t addrcp1, sizecp1, addrcp2, sizecp2;
|
||||
uint64_t node_offset1 = fdt_find_node_by_path(*state, "/memory@80000000", &addrcp1, &sizecp1);
|
||||
uint64_t node_offset2 = fdt_find_node_by_path(*state, "/reserved-memory/ramoops@bfdf0000", &addrcp2, &sizecp2);
|
||||
|
||||
struct device_tree_region regions[3];
|
||||
regions[0].addr = 0xDEADBEEF;
|
||||
regions[0].size = 0xDEADBEEF;
|
||||
fdt_read_reg_prop(*state, node_offset1, addrcp1, sizecp1, regions, 0);
|
||||
assert_int_equal(0xDEADBEEF, regions[0].addr);
|
||||
assert_int_equal(0xDEADBEEF, regions[0].size);
|
||||
fdt_read_reg_prop(*state, node_offset2, addrcp2, sizecp2, regions, 1);
|
||||
assert_int_equal(0xbfdf0000, regions[0].addr);
|
||||
assert_int_equal(0x00010000, regions[0].size);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
const struct CMUnitTest tests[] = {
|
||||
cmocka_unit_test(test_fdt_find_node_by_path),
|
||||
cmocka_unit_test(test_fdt_find_subnodes_by_prefix),
|
||||
cmocka_unit_test(test_fdt_find_node_by_alias),
|
||||
cmocka_unit_test(test_fdt_find_prop_in_node),
|
||||
cmocka_unit_test(test_fdt_read_reg_prop),
|
||||
};
|
||||
|
||||
return cb_run_group_tests(tests, setup_device_tree_test_group,
|
||||
teardown_device_tree_test_group);
|
||||
}
|
||||
@@ -29,6 +29,7 @@ HEADER_EXCLUDED="\
|
||||
^src/lib/stack.c\$|\
|
||||
^src/sbom/TAGS|\
|
||||
^src/vendorcode/|\
|
||||
^tests/data/|\
|
||||
^util/amdtools/example_input/|\
|
||||
^util/cbfstool/lzma/|\
|
||||
^util/cbfstool/lz4/|\
|
||||
|
||||
Reference in New Issue
Block a user