Skip to content

Commit

Permalink
core: rpmb: get RPMB storage space allocation stats
Browse files Browse the repository at this point in the history
Add rpmb_mem_stats() to get the RPMB secure storage space allocation
statistics.

Signed-off-by: Etienne Carriere <etienne.carriere@foss.st.com>
Acked-by: Jens Wiklander <jens.wiklander@linaro.org>
  • Loading branch information
etienne-lms committed Jan 20, 2025
1 parent a969e99 commit c0a732c
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
11 changes: 11 additions & 0 deletions core/include/tee/tee_fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#ifndef __TEE_TEE_FS_H
#define __TEE_TEE_FS_H

#include <pta_stats.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <tee_api_defines_extensions.h>
Expand Down Expand Up @@ -62,6 +64,9 @@ TEE_Result tee_rpmb_fs_raw_open(const char *fname, bool create,
struct tee_file_handle **fh);
TEE_Result tee_rpmb_reinit(void);

/* Ger RPMB memory allocation statistics */
TEE_Result rpmb_mem_stats(struct pta_stats_alloc *stats, bool reset);

/**
* Weak function which can be overridden by platforms to indicate that the RPMB
* key is ready to be written. Defaults to true, platforms can return false to
Expand All @@ -73,6 +78,12 @@ static inline TEE_Result tee_rpmb_reinit(void)
{
return TEE_ERROR_STORAGE_NOT_AVAILABLE;
}

static inline TEE_Result rpmb_mem_stats(struct pta_stats_alloc *stats __unused,
bool reset __unused)
{
return TEE_ERROR_STORAGE_NOT_AVAILABLE;
}
#endif

/*
Expand Down
65 changes: 65 additions & 0 deletions core/tee/tee_rpmb_fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <string_ext.h>
#include <string.h>
#include <sys/queue.h>
#include <string_ext.h>
#include <tee/tee_fs.h>
#include <tee/tee_fs_key_manager.h>
#include <tee/tee_pobj.h>
Expand Down Expand Up @@ -3173,3 +3174,67 @@ bool __weak plat_rpmb_key_is_ready(void)
{
return true;
}

#ifdef CFG_WITH_STATS
TEE_Result rpmb_mem_stats(struct pta_stats_alloc *stats, bool reset)
{
TEE_Result res = TEE_ERROR_GENERIC;
struct rpmb_fat_entry *fe = NULL;
tee_mm_entry_t *mm = NULL;
tee_mm_pool_t pool = { };
bool pool_result = false;
paddr_size_t pool_sz = 0;

mutex_lock(&rpmb_mutex);

res = rpmb_fs_setup();
if (res)
goto out;

pool_sz = fs_par->max_rpmb_address - RPMB_STORAGE_START_ADDRESS;
pool_result = tee_mm_init(&pool, RPMB_STORAGE_START_ADDRESS,
pool_sz, RPMB_BLOCK_SIZE_SHIFT,
TEE_MM_POOL_HI_ALLOC);
if (!pool_result) {
res = TEE_ERROR_OUT_OF_MEMORY;
goto out;
}

res = fat_entry_dir_init();
if (res)
goto out;

/*
* The pool is used to represent the current RPMB layout. To find
* a slot for the file tee_mm_alloc is called on the pool. Thus
* if it is not NULL the entire FAT must be traversed to fill in
* the pool.
*/
while (true) {
res = fat_entry_dir_get_next(&fe, NULL);
if (res || !fe)
break;

if (!(fe->flags & FILE_IS_ACTIVE) || !fe->data_size)
continue;

mm = tee_mm_alloc2(&pool, fe->start_address, fe->data_size);
if (!mm) {
res = TEE_ERROR_GENERIC;
break;
}
}

fat_entry_dir_deinit();

out:
mutex_unlock(&rpmb_mutex);

if (!res)
tee_mm_get_pool_stats(&pool, stats, reset);

tee_mm_final(&pool);

return res;
}
#endif /*CFG_WITH_STATS*/

0 comments on commit c0a732c

Please sign in to comment.