From 179271881aad576eedda12eb78e697ba53e97284 Mon Sep 17 00:00:00 2001 From: sjf Date: Mon, 26 Jun 2023 14:15:33 +0800 Subject: [PATCH] zvm tools: add dtb parser this commit add the code of dtb parser that translate the file to device arrays Signed-off-by: sjf --- include/_zvm/tools/dtb_parser.h | 39 ++++++++++++++ subsys/_zvm/tools/CMakeLists.txt | 4 ++ subsys/_zvm/tools/dtb_parser.c | 92 ++++++++++++++++++++++++++++++++ 3 files changed, 135 insertions(+) create mode 100644 include/_zvm/tools/dtb_parser.h create mode 100644 subsys/_zvm/tools/dtb_parser.c diff --git a/include/_zvm/tools/dtb_parser.h b/include/_zvm/tools/dtb_parser.h new file mode 100644 index 00000000..08557bef --- /dev/null +++ b/include/_zvm/tools/dtb_parser.h @@ -0,0 +1,39 @@ +/* + * Copyright 2021-2022 HNU + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef __ZVM_DTB_PARSER_H_ +#define __ZVM_DTB_PARSER_H_ + +#include +#include +#include +#include +#include + +#include <_zvm/zvm.h> + +typedef struct { + unsigned long address; + unsigned long size; +} zvmReg; + +typedef struct { + const char *name; + const char *compatible; + const char *status; + const char *method; + const char *label; + unsigned int current_speed; + zvmReg reg; +} zvmDevice; + +uint32_t fdt_getprop_u32(const void *fdt, int nodeoffset, const char *name); + +zvmReg fdt_get_reg(const void *fdt, int nodeoffset, const char *name); + +void parse_dtb_file(const char *file, zvmDevice *devices, int *device_count); + +#endif \ No newline at end of file diff --git a/subsys/_zvm/tools/CMakeLists.txt b/subsys/_zvm/tools/CMakeLists.txt index b97e8291..724839d3 100644 --- a/subsys/_zvm/tools/CMakeLists.txt +++ b/subsys/_zvm/tools/CMakeLists.txt @@ -12,3 +12,7 @@ zephyr_sources_ifdef( CONFIG_ZVM_TIME_MEASURE latency_measure.c ) + +zephyr_sources_ifdef( + dtb_parser.c +) \ No newline at end of file diff --git a/subsys/_zvm/tools/dtb_parser.c b/subsys/_zvm/tools/dtb_parser.c new file mode 100644 index 00000000..9e9d3576 --- /dev/null +++ b/subsys/_zvm/tools/dtb_parser.c @@ -0,0 +1,92 @@ +/* + * Copyright 2021-2022 HNU + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include<_zvm/tools/dtb_parser.h> + +uint32_t fdt_getprop_u32(const void *fdt, int nodeoffset, const char *name) +{ + int len; + const uint32_t *prop = fdt_getprop(fdt, nodeoffset, name, &len); + if (!prop || len != sizeof(uint32_t)) { + return 0; + } + + return fdt32_to_cpu(*prop); +} + + +zvmReg fdt_get_reg(const void *fdt, int nodeoffset, const char *name) +{ + zvmReg reg = {0, 0}; + int len; + const uint32_t *prop = fdt_getprop(fdt, nodeoffset, name, &len); + + /* Check that we have four 32-bit numbers */ + if (!prop || len != sizeof(uint32_t) * 4) { + return reg; + } + + /* Combine two 32-bit values to create 64-bit address and size */ + reg.address = ((uint64_t)fdt32_to_cpu(prop[0]) << 32) | fdt32_to_cpu(prop[1]); + reg.size = ((uint64_t)fdt32_to_cpu(prop[2]) << 32) | fdt32_to_cpu(prop[3]); + + return reg; +} + + + +void parse_dtb_file(const char *file, zvmDevice *devices, int *device_count,uint64_t sz) +{ + void *data; + int err; + + if (file == NULL) + { + ZVM_LOG_ERR("Failed to open file"); + return; + } + + data = k_malloc(sz); + if (data == NULL) + { + ZVM_LOG_ERR( "Failed to allocate memory\n"); + k_free(data); + return; + } + + memcpy(data,file,sz); + + err = fdt_check_header(data); + if (err != 0) + { + ZMV_LOG_ERR("Invalid device tree file"); + k_free(data); + return; + } + + int node_offset = 0; + int index = 0; + while ((node_offset = fdt_next_node(data, node_offset, NULL)) >= 0) + { + devices[index].name = fdt_get_name(data, node_offset, NULL); + devices[index].compatible = fdt_getprop(data, node_offset, "compatible", NULL); + devices[index].status = fdt_getprop(data, node_offset, "status", NULL); + devices[index].method = fdt_getprop(data, node_offset, "method", NULL); + devices[index].label = fdt_getprop(data, node_offset, "label", NULL); + devices[index].current_speed = fdt_getprop_u32(data, node_offset, "current-speed"); + devices[index].reg = fdt_get_reg(data, node_offset, "reg"); + + index++; + ZVM_LOG_INFO("Device %d: name:%s, compatible: %s, status: %s, method: %s, label: %s, current_speed:%d, Reg: %lx %lx", index, + devices[index].name, devices[index].compatible, + devices[index].status, devices[index].method,devices[index].label, + devices[index].current_speed, + devices[index].reg.address, devices[index].reg.size); + } + + *device_count = index; + k_free(data); +} \ No newline at end of file -- Gitee