1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2023, Linaro Limited
4  */
5 
6 #include <err.h>
7 #include <ta_storage.h>
8 #include <tee_client_api.h>
9 #include <stdlib.h>
10 #include <util.h>
11 
12 #include "clear_storage.h"
13 
clear_storage_for_ta(TEEC_UUID * uuid)14 static int clear_storage_for_ta(TEEC_UUID *uuid)
15 {
16 	TEEC_Result res = TEEC_ERROR_GENERIC;
17 	TEEC_Context ctx = { };
18 	TEEC_Session sess = { };
19 	TEEC_Operation op = { };
20 	uint32_t eo = 0;
21 
22 	res = TEEC_InitializeContext(NULL, &ctx);
23 	if (res)
24 		errx(EXIT_FAILURE, "TEEC_InitializeContext: %#"PRIx32, res);
25 
26 	res = TEEC_OpenSession(&ctx, &sess, uuid, TEEC_LOGIN_PUBLIC, NULL,
27 			       NULL, &eo);
28 	if (res)
29 		errx(EXIT_FAILURE,
30 		     "TEEC_OpenSession: res %#"PRIx32" err_orig %#"PRIx32,
31 			res, eo);
32 
33 	op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE,
34 					 TEEC_NONE);
35 	res = TEEC_InvokeCommand(&sess, TA_STORAGE_CMD_CLEAR_STORAGE, &op, &eo);
36 	if (res)
37 		errx(EXIT_FAILURE,
38 		     "TEEC_InvokeCommand: res %#"PRIx32" err_orig %#"PRIx32,
39 		     res, eo);
40 
41 	TEEC_CloseSession(&sess);
42 	TEEC_FinalizeContext(&ctx);
43 	return 0;
44 }
45 
clear_storage(void)46 int clear_storage(void)
47 {
48 	TEEC_UUID uuid[] = { TA_STORAGE_UUID, TA_STORAGE2_UUID };
49 	size_t i = 0;
50 	int res = 0;
51 
52 	for (i = 0; i < ARRAY_SIZE(uuid); i++) {
53 		res = clear_storage_for_ta(uuid + i);
54 		if (res)
55 			break;
56 	}
57 	return res;
58 }
59