1 /*
2 * Copyright (C) 2022 Huawei Technologies Co., Ltd.
3 * Decription: agent manager function definition, such as register and send cmd
4 *
5 * This software is licensed under the terms of the GNU General Public
6 * License version 2, as published by the Free Software Foundation, and
7 * may be copied, distributed, and modified under those terms.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14 #ifndef AGENT_H
15 #define AGENT_H
16 #include <linux/fs.h>
17 #include "teek_ns_client.h"
18
19 #define MAX_PATH_SIZE 512
20 #define AGENT_FS_ID 0x46536673 /* FSfs */
21 #define AGENT_MISC_ID 0x4d495343 /* MISC */
22
23 #ifdef CONFIG_RPMB_AGENT
24 #define TEE_RPMB_AGENT_ID 0x4abe6198 /* RPMB */
25 #endif
26
27 #define AGENT_SOCKET_ID 0x69e85664 /* socket */
28 #define SECFILE_LOAD_AGENT_ID 0x4c4f4144 /* SECFILE-LOAD-AGENT */
29 #define TEE_SECE_AGENT_ID 0x53656345 /* npu agent id */
30 #define TEE_FACE_AGENT1_ID 0x46616365 /* face agent id */
31 #define TEE_FACE_AGENT2_ID 0x46616345 /* face agent id */
32 #define TEE_VLTMM_AGENT_ID 0x564c544d /* vltmm agent id */
33 #define SYSTEM_UID 1000
34 #define MS_TO_NS 1000000
35
36 enum agent_state_type {
37 AGENT_CRASHED = 0,
38 AGENT_REGISTERED,
39 AGENT_READY,
40 };
41
42 enum agent_status {
43 AGENT_ALIVE = 1,
44 AGENT_DEAD = 0,
45 };
46
47 /* for secure agent */
48 struct smc_event_data {
49 unsigned int agent_id;
50 atomic_t agent_ready;
51 wait_queue_head_t wait_event_wq;
52 int ret_flag; /* indicate whether agent is returned from TEE */
53 wait_queue_head_t send_response_wq;
54 struct list_head head;
55 struct tc_ns_smc_cmd cmd;
56 struct tc_ns_dev_file *owner;
57 void *agent_buff_kernel;
58 void *agent_buff_user; /* used for unmap */
59 unsigned int agent_buff_size;
60 atomic_t usage;
61 wait_queue_head_t ca_pending_wq;
62 /* indicate whether agent is allowed to return to TEE */
63 atomic_t ca_run;
64 };
65
66 struct tee_agent_kernel_ops {
67 const char *agent_name;
68 unsigned int agent_id;
69 int (*tee_agent_init)(struct tee_agent_kernel_ops *agent_instance);
70 int (*tee_agent_run)(struct tee_agent_kernel_ops *agent_instance);
71 int (*tee_agent_work)(struct tee_agent_kernel_ops *agent_instance);
72 int (*tee_agent_stop)(struct tee_agent_kernel_ops *agent_instance);
73 int (*tee_agent_exit)(struct tee_agent_kernel_ops *agent_instance);
74 int (*tee_agent_crash_work)(
75 struct tee_agent_kernel_ops *agent_instance,
76 struct tc_ns_client_context *context,
77 unsigned int dev_file_id);
78 struct task_struct *agent_thread;
79 void *agent_data;
80 void *agent_buff;
81 unsigned int agent_buff_size;
82 struct list_head list;
83 };
84
85 struct ca_info {
86 char path[MAX_PATH_SIZE];
87 uint32_t uid;
88 uint32_t agent_id;
89 };
90
get_agent_event(struct smc_event_data *event_data)91 static inline void get_agent_event(struct smc_event_data *event_data)
92 {
93 if (event_data)
94 atomic_inc(&event_data->usage);
95 }
96
put_agent_event(struct smc_event_data *event_data)97 static inline void put_agent_event(struct smc_event_data *event_data)
98 {
99 if (event_data) {
100 if (atomic_dec_and_test(&event_data->usage))
101 kfree(event_data);
102 }
103 }
104
105 int is_allowed_agent_ca(const struct ca_info *ca,
106 bool check_agent_id);
107 void agent_init(void);
108 void free_agent(void);
109 struct smc_event_data *find_event_control(unsigned int agent_id);
110 void send_event_response(unsigned int agent_id);
111 int agent_process_work(const struct tc_ns_smc_cmd *smc_cmd, unsigned int agent_id);
112 int is_agent_alive(unsigned int agent_id);
113 int tc_ns_set_native_hash(unsigned long arg, unsigned int cmd_id);
114 int tc_ns_late_init(unsigned long arg);
115 int tc_ns_register_agent(struct tc_ns_dev_file *dev_file, unsigned int agent_id,
116 unsigned int buffer_size, void **buffer, bool user_agent);
117 int tc_ns_unregister_agent(unsigned int agent_id);
118 void send_crashed_event_response_all(const struct tc_ns_dev_file *dev_file);
119 int tc_ns_wait_event(unsigned int agent_id);
120 int tc_ns_send_event_response(unsigned int agent_id);
121 void send_event_response_single(const struct tc_ns_dev_file *dev_file);
122 int sync_system_time_from_user(const struct tc_ns_client_time *user_time);
123 void sync_system_time_from_kernel(void);
124 int tee_agent_clear_work(struct tc_ns_client_context *context,
125 unsigned int dev_file_id);
126 int tee_agent_kernel_register(struct tee_agent_kernel_ops *new_agent);
127 bool is_system_agent(const struct tc_ns_dev_file *dev_file);
128 void tee_agent_clear_dev_owner(const struct tc_ns_dev_file *dev_file);
129 char *get_proc_dpath(char *path, int path_len);
130 int check_ext_agent_access(uint32_t agent_id);
131
132 #endif
133