1 /*
2  * Copyright 2021, The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <stdint.h>
20 
21 /**
22  * DOC: Metrics
23  *
24  * Metrics interface provides a way for Android to get Trusty metrics data.
25  *
26  * Currently, only "push" model is supported. Clients are expected to connect to
27  * metrics service, listen for events, e.g. app crash events, and respond to
28  * every event with a &struct metrics_req.
29  *
30  * Communication is driven by metrics service, i.e. requests/responses are all
31  * sent from/to metrics service.
32  *
33  * Note that the type of the event is not known to the client ahead of time.
34  *
35  * In the future, if we need to have Android "pull" metrics data from Trusty,
36  * that can be done by introducing a separate port.
37  *
38  * This interface is shared between Android and Trusty. There is a copy in each
39  * repository. They must be kept in sync.
40  */
41 
42 #define METRICS_PORT "com.android.trusty.metrics.consumer"
43 
44 #define UUID_STR_SIZE (37)
45 
46 /**
47  * enum metrics_cmd - command identifiers for metrics interface
48  * @METRICS_CMD_RESP_BIT:             message is a response
49  * @METRICS_CMD_REQ_SHIFT:            number of bits used by @METRICS_CMD_RESP_BIT
50  * @METRICS_CMD_REPORT_EVENT_DROP:    report gaps in the event stream
51  * @METRICS_CMD_REPORT_CRASH:         report an app crash event
52  * @METRICS_CMD_REPORT_EXIT:          report an app exit
53  * @METRICS_CMD_REPORT_STORAGE_ERROR: report trusty storage error
54  */
55 enum metrics_cmd {
56     METRICS_CMD_RESP_BIT = 1,
57     METRICS_CMD_REQ_SHIFT = 1,
58 
59     METRICS_CMD_REPORT_EVENT_DROP = (1 << METRICS_CMD_REQ_SHIFT),
60     METRICS_CMD_REPORT_CRASH = (2 << METRICS_CMD_REQ_SHIFT),
61     METRICS_CMD_REPORT_EXIT = (3 << METRICS_CMD_REQ_SHIFT),
62     METRICS_CMD_REPORT_STORAGE_ERROR = (4 << METRICS_CMD_REQ_SHIFT),
63 };
64 
65 /**
66  * enum metrics_error - metrics error codes
67  * @METRICS_NO_ERROR:        no error
68  * @METRICS_ERR_UNKNOWN_CMD: unknown or not implemented command
69  */
70 enum metrics_error {
71     METRICS_NO_ERROR = 0,
72     METRICS_ERR_UNKNOWN_CMD = 1,
73 };
74 
75 /**
76  * struct metrics_req - common structure for metrics requests
77  * @cmd:      command identifier - one of &enum metrics_cmd
78  * @reserved: must be 0
79  */
80 struct metrics_req {
81     uint32_t cmd;
82     uint32_t reserved;
83 } __attribute__((__packed__));
84 
85 /**
86  * struct metrics_resp - common structure for metrics responses
87  * @cmd: command identifier - %METRICS_CMD_RESP_BIT or'ed with a cmd in
88  *                            one of &enum metrics_cmd
89  * @status: response status, one of &enum metrics_error
90  */
91 struct metrics_resp {
92     uint32_t cmd;
93     uint32_t status;
94 } __attribute__((__packed__));
95 
96 /**
97  * struct metrics_report_exit_req - arguments of %METRICS_CMD_REPORT_EXIT
98  *                                   requests
99  * @app_id: app_id in the form UUID in ascii format
100  *          "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
101  * @exit_code: architecture-specific exit code
102  */
103 struct metrics_report_exit_req {
104     char app_id[UUID_STR_SIZE];
105     uint32_t exit_code;
106 } __attribute__((__packed__));
107 
108 /**
109  * struct metrics_report_crash_req - arguments of %METRICS_CMD_REPORT_CRASH
110  *                                   requests
111  * @app_id: app_id in the form UUID in ascii format
112  *          "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
113  * @crash_reason: architecture-specific code representing the reason for the
114  *                crash
115  */
116 struct metrics_report_crash_req {
117     char app_id[UUID_STR_SIZE];
118     uint32_t crash_reason;
119 } __attribute__((__packed__));
120 
121 enum TrustyStorageErrorType {
122   TRUSTY_STORAGE_ERROR_UNKNOWN = 0,
123   TRUSTY_STORAGE_ERROR_SUPERBLOCK_INVALID = 1,
124   TRUSTY_STORAGE_ERROR_BLOCK_MAC_MISMATCH = 2,
125   TRUSTY_STORAGE_ERROR_BLOCK_HEADER_INVALID = 3,
126   TRUSTY_STORAGE_ERROR_RPMB_COUNTER_MISMATCH = 4,
127   TRUSTY_STORAGE_ERROR_RPMB_COUNTER_MISMATCH_RECOVERED = 5,
128   TRUSTY_STORAGE_ERROR_RPMB_COUNTER_READ_FAILURE = 6,
129   TRUSTY_STORAGE_ERROR_RPMB_MAC_MISMATCH = 7,
130   TRUSTY_STORAGE_ERROR_RPMB_ADDR_MISMATCH = 8,
131   TRUSTY_STORAGE_ERROR_RPMB_FAILURE_RESPONSE = 9,
132   TRUSTY_STORAGE_ERROR_RPMB_UNKNOWN = 10,
133   TRUSTY_STORAGE_ERROR_RPMB_SCSI_ERROR = 11,
134   TRUSTY_STORAGE_ERROR_IO_ERROR = 12,
135   TRUSTY_STORAGE_ERROR_PROXY_COMMUNICATION_FAILURE = 13,
136 };
137 
138 enum TrustyFileSystem {
139   TRUSTY_FS_UNKNOWN = 0,
140   TRUSTY_FS_TP = 1,
141   TRUSTY_FS_TD = 2,
142   TRUSTY_FS_TDP = 3,
143   TRUSTY_FS_TDEA = 4,
144   TRUSTY_FS_NSP = 5,
145 };
146 
147 enum TrustyBlockType {
148   TRUSTY_BLOCKTYPE_UNKNOWN = 0,
149   TRUSTY_BLOCKTYPE_FILES_ROOT = 1,
150   TRUSTY_BLOCKTYPE_FREE_ROOT = 2,
151   TRUSTY_BLOCKTYPE_FILES_INTERNAL = 3,
152   TRUSTY_BLOCKTYPE_FREE_INTERNAL = 4,
153   TRUSTY_BLOCKTYPE_FILE_ENTRY = 5,
154   TRUSTY_BLOCKTYPE_FILE_BLOCK_MAP = 6,
155   TRUSTY_BLOCKTYPE_FILE_DATA = 7,
156   TRUSTY_BLOCKTYPE_CHECKPOINT_ROOT = 8,
157   TRUSTY_BLOCKTYPE_CHECKPOINT_FILES_ROOT = 9,
158   TRUSTY_BLOCKTYPE_CHECKPOINT_FREE_ROOT = 10,
159 };
160 
161 struct metrics_report_storage_error_req {
162     enum TrustyStorageErrorType error;
163     char app_id[UUID_STR_SIZE];
164     char client_app_id[UUID_STR_SIZE];
165     uint32_t write;
166     enum TrustyFileSystem file_system;
167     uint64_t file_path_hash;
168     enum TrustyBlockType block_type;
169     uint64_t repair_counter;
170 } __attribute__((__packed__));
171 
172 struct metrics_msg {
173     struct metrics_req req;
174     union {
175         struct metrics_report_crash_req crash_args;
176         struct metrics_report_exit_req exit_args;
177         struct metrics_report_storage_error_req storage_args;
178     };
179 } __attribute__((__packed__));