1 /*
2  * Copyright (C) 2023 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 #include "error_reporting.h"
18 #include <uapi/err.h>
19 
20 #ifdef STORAGE_ENABLE_ERROR_REPORTING
21 
22 #include <lib/tipc/tipc.h>
23 #include <lib/tipc/tipc_srv.h>
24 #include <trusty/uuid.h>
25 #include <trusty_log.h>
26 
27 #include <storage_consts.h>
28 #include <interface/metrics/metrics.h>
29 #include "block_device_tipc.h"
30 
31 #define TLOG_TAG "ss-err_rep"
32 
33 struct uuid storage_service_uuid = STORAGE_SERVICE_UUID;
34 char storage_service_uuid_str[UUID_STR_SIZE];
35 handle_t metrics_chan;
36 
init_uuid_str(void)37 static void init_uuid_str(void) {
38     if (storage_service_uuid_str[0] == '\0') {
39         uuid_to_str(&storage_service_uuid, storage_service_uuid_str);
40     }
41 }
42 
map_fs_name(const char * name)43 static enum trusty_file_system map_fs_name(const char* name) {
44     if (name == file_system_id_tp) {
45         return TRUSTY_FS_TP;
46     }
47     if (name == file_system_id_tdp) {
48         return TRUSTY_FS_TDP;
49     }
50     if (name == file_system_id_td) {
51         return TRUSTY_FS_TD;
52     }
53     if (name == file_system_id_tdea) {
54         return TRUSTY_FS_TDEA;
55     }
56     if (name == file_system_id_nsp) {
57         return TRUSTY_FS_NSP;
58     }
59     TLOGI("Unknown fs name: %s\n", name);
60     return TRUSTY_FS_UNKNOWN;
61 }
62 
do_error_report(enum trusty_storage_error_type type,const char * fs_name,enum trusty_block_type block_type)63 void do_error_report(enum trusty_storage_error_type type,
64                      const char* fs_name,
65                      enum trusty_block_type block_type) {
66     init_uuid_str();
67 
68     struct metrics_report_storage_error_req atom = {
69             .error = TRUSTY_STORAGE_ERROR_UNKNOWN,
70             .app_id = {},
71             .client_app_id = {},
72             .write = 0,
73             .file_system = TRUSTY_FS_UNKNOWN,
74             .file_path_hash = 0,
75             .block_type = TRUSTY_BLOCKTYPE_UNKNOWN,
76             .repair_counter = 0,
77     };
78 
79     struct metrics_req req;
80     req.cmd = METRICS_CMD_REPORT_STORAGE_ERROR;
81 
82     memcpy(storage_service_uuid_str, atom.app_id, UUID_STR_SIZE);
83 
84     struct iovec iovs[] = {
85             {
86                     .iov_base = &req,
87                     .iov_len = sizeof(req),
88             },
89             {
90                     .iov_base = &atom,
91                     .iov_len = sizeof(atom),
92             },
93     };
94     struct ipc_msg msg = {
95             .num_iov = countof(iovs),
96             .iov = iovs,
97     };
98 
99     size_t total_len = sizeof(req) + sizeof(atom);
100 
101     int rc = send_msg(metrics_chan, &msg);
102     if (rc != (int)total_len) {
103         TLOGE("failed (%d) to send storage event\n", rc);
104     }
105 }
106 
connect_metrics_ta(void)107 int connect_metrics_ta(void) {
108     handle_t chan;
109     int rc = tipc_connect(&chan, METRICS_CONSUMER_PORT);
110     if (rc != NO_ERROR) {
111         TLOGE("failed (%d) to connect to metrics TA\n", rc);
112         return rc;
113     }
114     metrics_chan = chan;
115     return NO_ERROR;
116 }
117 
118 #else /* STORAGE_ENABLE_ERROR_REPORTING */
119 
do_error_report(enum trusty_storage_error_type type,const char * fs_name,enum trusty_block_type block_type)120 void do_error_report(enum trusty_storage_error_type type,
121                      const char* fs_name,
122                      enum trusty_block_type block_type) {}
123 
connect_metrics_ta(void)124 int connect_metrics_ta(void) {
125     return NO_ERROR;
126 }
127 
128 #endif /* STORAGE_ENABLE_ERROR_REPORTING */
129