xtest: add --clear-storage option
When unexpected errors occur in the secure storage tests
(regression_6xxx) some persistent objects might be left over, causing
errors in further tests which expect to start from a clean state.
This situation cannot be addressed fully by error handling in xtest or
in the storage TA, because there are unrecoverable conditions (data
abort, kill -9...). Instead, implement a new --clear-storage option
which invokes the storage TA to enumerate and delete any objects it may
own. The TA is invoked twice (because the same code is exposed via two
UUIDS), and each invocation iterates on the two possible filesystems
(TEE_STORAGE_PRIVATE_REE, TEE_STORAGE_PRIVATE_RPMB).
Signed-off-by: Jerome Forissier <jerome.forissier@linaro.org>
Reviewed-by: Joakim Bech <joakim.bech@linaro.org>
Reviewed-by: Jens Wiklander <jens.wiklander@linaro.org>
Reviewed-by: Etienne Carriere <etienne.carriere@foss.st.com>
diff --git a/ta/include/ta_storage.h b/ta/include/ta_storage.h
index 96bc560..f494f5c 100644
--- a/ta/include/ta_storage.h
+++ b/ta/include/ta_storage.h
@@ -50,5 +50,6 @@
#define TA_STORAGE_CMD_CREATE_ID_IN_SHM 23
#define TA_STORAGE_CMD_CREATEOVER_ID_IN_SHM 24
#define TA_STORAGE_CMD_RENAME_ID_IN_SHM 25
+#define TA_STORAGE_CMD_CLEAR_STORAGE 26
#endif /*__TA_STORAGE_H*/
diff --git a/ta/storage/include/storage.h b/ta/storage/include/storage.h
index ecbda8a..50a99d6 100644
--- a/ta/storage/include/storage.h
+++ b/ta/storage/include/storage.h
@@ -39,5 +39,7 @@
TEE_Result ta_storage_cmd_reset_obj(uint32_t param_types, TEE_Param params[4]);
TEE_Result ta_storage_cmd_get_obj_info(uint32_t param_types,
TEE_Param params[4]);
+TEE_Result ta_storage_cmd_clear_storage(uint32_t param_types,
+ TEE_Param params[4]);
#endif /*STORAGE_H */
diff --git a/ta/storage/storage.c b/ta/storage/storage.c
index 93548a3..f570732 100644
--- a/ta/storage/storage.c
+++ b/ta/storage/storage.c
@@ -8,7 +8,9 @@
#include "ta_storage.h"
#include <tee_api.h>
+#include <tee_api_defines_extensions.h>
#include <trace.h>
+#include <user_ta_header_defines.h>
#define ASSERT_PARAM_TYPE(pt) \
do { \
@@ -662,3 +664,77 @@
return res;
}
+
+static TEE_Result clear_storage(uint32_t storage_id)
+{
+ TEE_ObjectEnumHandle oe = TEE_HANDLE_NULL;
+ TEE_Result enum_res = TEE_ERROR_GENERIC;
+ TEE_ObjectHandle o = TEE_HANDLE_NULL;
+ TEE_Result res = TEE_ERROR_GENERIC;
+ TEE_UUID uuid = TA_UUID;
+ TEE_ObjectInfo oi = { };
+ size_t obj_id_sz = 0;
+ void *obj_id = NULL;
+ size_t i = 0;
+
+ IMSG("Clearing TA storage (UUID: %pUl, storage ID: 0x%x)",
+ (void *)&uuid, storage_id);
+ res = TEE_AllocatePersistentObjectEnumerator(&oe);
+ if (res)
+ return res;
+ res = TEE_StartPersistentObjectEnumerator(oe, storage_id);
+ if (res == TEE_ERROR_ITEM_NOT_FOUND) {
+ IMSG("No object found");
+ res = TEE_SUCCESS;
+ goto out;
+ }
+ if (res)
+ goto out;
+ obj_id = TEE_Malloc(TEE_OBJECT_ID_MAX_LEN, 0);
+ if (!obj_id) {
+ res = TEE_ERROR_OUT_OF_MEMORY;
+ goto out;
+ }
+
+ while (true) {
+ enum_res = TEE_GetNextPersistentObject(oe, &oi, obj_id,
+ &obj_id_sz);
+ if (enum_res == TEE_ERROR_ITEM_NOT_FOUND)
+ break;
+ if (enum_res) {
+ res = enum_res;
+ break;
+ }
+ IMSG("Deleting persistent object #%zu", i);
+ res = TEE_OpenPersistentObject(storage_id, obj_id, obj_id_sz,
+ TEE_DATA_FLAG_ACCESS_WRITE_META,
+ &o);
+ if (res)
+ break;
+ TEE_CloseAndDeletePersistentObject1(o);
+ i++;
+ }
+
+out:
+ TEE_FreePersistentObjectEnumerator(oe);
+ TEE_Free(obj_id);
+ return res;
+}
+
+TEE_Result ta_storage_cmd_clear_storage(uint32_t param_types,
+ TEE_Param params[4])
+{
+ uint32_t id[] = { TEE_STORAGE_PRIVATE_REE, TEE_STORAGE_PRIVATE_RPMB };
+ TEE_Result res = TEE_ERROR_GENERIC;
+ size_t i = 0;
+
+ (void)param_types;
+ (void)params;
+
+ for (i = 0; i < sizeof(id) / sizeof(id[0]); i++) {
+ res = clear_storage(id[i]);
+ if (res)
+ break;
+ }
+ return res;
+}
diff --git a/ta/storage/ta_entry.c b/ta/storage/ta_entry.c
index c507c61..f3a93db 100644
--- a/ta/storage/ta_entry.c
+++ b/ta/storage/ta_entry.c
@@ -119,6 +119,9 @@
case TA_STORAGE_CMD_GET_OBJ_INFO:
return ta_storage_cmd_get_obj_info(nParamTypes, pParams);
+ case TA_STORAGE_CMD_CLEAR_STORAGE:
+ return ta_storage_cmd_clear_storage(nParamTypes, pParams);
+
default:
return TEE_ERROR_BAD_PARAMETERS;
}