Skip to content

Commit e2ee4ee

Browse files
authored
Merge pull request #94 from Kamilosok/dt_rsvmap
Memory reservation map access
2 parents d9b7f84 + 64e4d3a commit e2ee4ee

5 files changed

Lines changed: 69 additions & 2 deletions

File tree

include/dt/dt.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,20 @@ typedef struct {
3232
dt_node_t root_node; /**< Root node offset. */
3333
u32 total_size; /**< Total size of the FDT. */
3434
u32 struct_off; /**< Structure block offset. */
35+
u32 rsvmap_off; /**< Memory reservation map offset. */
3536
u32 strings_off; /**< Strings block offset. */
3637
u32 struct_size; /**< Structure block size. */
3738
u32 fdt_version; /**< FDT version. */
3839
} fdt_t;
3940

41+
/**
42+
* @brief Reserved memory entry structure.
43+
*/
44+
typedef struct {
45+
u64 address; /**< Entry address. */
46+
u64 size; /**< Entry size. */
47+
} fdt_rsv_entry;
48+
4049
/**
4150
* @brief Read the header at fdt and set fields of obj.
4251
* @param fdt Pointer to the flattened device tree data.
@@ -214,6 +223,19 @@ error_t dt_get_prop_name_ptr(const fdt_t* fdt, dt_prop_t prop, const char** ptrO
214223
[[gnu::nonnull(3)]]
215224
error_t dt_get_prop_buffer(const fdt_t* fdt, dt_prop_t prop, buffer_t* bufOUT);
216225

226+
/**
227+
* @brief Get a reserved memory entry at index from the Memory Reservation Block.
228+
* @param fdt Pointer to the fdt object.
229+
* @param index Index of the entry to get.
230+
* @param[out] entryOUT Entry at index.
231+
* @retval ERR_NONE on success
232+
* @retval ERR_BAD_ARG on nullptr args
233+
* @retval ERR_NOT_VALID if the FDT is invalid
234+
* @retval ERR_OUT_OF_BOUNDS if index is out of bounds or the Memory Reservation Block is malformed
235+
*/
236+
[[gnu::nonnull(3)]]
237+
error_t dt_get_rsv_mem_entry(const fdt_t* fdt, u32 index, fdt_rsv_entry* entryOUT);
238+
217239
/// @}
218240

219241
#endif // !DT_DT

src/example_dtree/entry.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,14 @@ void main([[maybe_unused]] u32 hartid, const void* fdt) {
5454
DEBUG_PRINTF("First prop: %s\n", prop_name);
5555
error = dt_get_prop_name_ptr(&fdt_obj, next_prop, &prop_name);
5656
DEBUG_PRINTF("Next prop: %s\n", prop_name);
57+
58+
fdt_rsv_entry entry;
59+
error = dt_get_rsv_mem_entry(&fdt_obj, 0, &entry);
60+
61+
if (error != ERR_NONE) {
62+
// To show that there's only one (terminating) entry in the rsvmap
63+
DEBUG_PRINTF("%u %u\n", fdt_obj.rsvmap_off, fdt_obj.struct_off);
64+
DEBUG_PRINTF("Failed to get reserved memory entry: %d\n", error);
65+
return;
66+
}
5767
}

src/lib/dt/dt_access.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,3 +363,37 @@ error_t dt_get_prop_buffer(const fdt_t* fdt, dt_prop_t prop, buffer_t* bufOUT) {
363363

364364
return ERR_NONE;
365365
}
366+
367+
error_t dt_get_rsv_mem_entry(const fdt_t* fdt, u32 index, fdt_rsv_entry* entryOUT) {
368+
if (fdt == nullptr)
369+
return ERR_BAD_ARG;
370+
371+
buffer_t fdt_buf = fdt->fdt_buffer;
372+
373+
u32 curr_index = 0;
374+
375+
u32 max_offset = fdt->struct_off;
376+
377+
fdt_rsv_entry entry;
378+
379+
for (u32 curr_offset = fdt->rsvmap_off; curr_offset + 2 * sizeof(u64) <= max_offset;
380+
curr_offset += 2 * sizeof(u64)) {
381+
if (!buffer_read_u64_be(fdt_buf, curr_offset, &entry.address) ||
382+
!buffer_read_u64_be(fdt_buf, curr_offset + sizeof(u64), &entry.size)) {
383+
return ERR_NOT_VALID;
384+
}
385+
386+
if (entry.address == 0 && entry.size == 0) {
387+
return ERR_OUT_OF_BOUNDS;
388+
}
389+
390+
if (curr_index == index) {
391+
*entryOUT = entry;
392+
return ERR_NONE;
393+
}
394+
395+
curr_index += 1;
396+
}
397+
398+
return ERR_NOT_VALID;
399+
}

src/lib/dt/dt_defines.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#define FDT_OFF_TOTAL_SIZE 0x04 /**< Offset to total size. */
2121
#define FDT_OFF_OFF_DT_STRUCT 0x08 /**< Offset to structure block offset. */
2222
#define FDT_OFF_OFF_DT_STRINGS 0x0C /**< Offset to strings block offset. */
23-
#define FDT_OFF_MEM_RSVMAP 0x10 /**< Offset to memory reservation map. */
23+
#define FDT_OFF_OFF_MEM_RSVMAP 0x10 /**< Offset to memory reservation map offset. */
2424
#define FDT_OFF_VERSION 0x14 /**< Offset to version. */
2525
#define FDT_OFF_LAST_COMP_VERSION 0x18 /**< Offset to last compatible version. */
2626
#define FDT_OFF_BOOT_CPUID_PHYS 0x1C /**< Offset to boot CPU physical ID. */

src/lib/dt/dt_utils.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@ error_t dt_init(const void* fdt, fdt_t* obj) {
3131
if (!buffer_read_u32_be(fdt_buf, FDT_OFF_TOTAL_SIZE, &obj->total_size) ||
3232
!buffer_read_u32_be(fdt_buf, FDT_OFF_OFF_DT_STRUCT, &obj->struct_off) ||
3333
!buffer_read_u32_be(fdt_buf, FDT_OFF_OFF_DT_STRINGS, &obj->strings_off) ||
34+
!buffer_read_u32_be(fdt_buf, FDT_OFF_OFF_MEM_RSVMAP, &obj->rsvmap_off) ||
3435
!buffer_read_u32_be(fdt_buf, FDT_OFF_SIZE_DT_STRUCT, &obj->struct_size) ||
3536
!buffer_read_u32_be(fdt_buf, FDT_OFF_VERSION, &obj->fdt_version) ||
36-
!buffer_read_u32_be(fdt_buf, FDT_OFF_VERSION, &last_comp_version)) {
37+
!buffer_read_u32_be(fdt_buf, FDT_OFF_LAST_COMP_VERSION, &last_comp_version)) {
3738
obj->fdt_buffer = make_buffer(nullptr, 0);
3839
return ERR_NOT_VALID;
3940
}

0 commit comments

Comments
 (0)