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 #pragma once
18 
19 #include "fs.h"
20 #include <lk/compiler.h>
21 #include <trusty/uuid.h>
22 #include <trusty_ipc.h>
23 
24 #define METRICS_CONSUMER_PORT "com.android.trusty.metrics.consumer"
25 
26 enum trusty_storage_error_type {
27   TRUSTY_STORAGE_ERROR_UNKNOWN = 0,
28   TRUSTY_STORAGE_ERROR_SUPERBLOCK_INVALID = 1,
29   TRUSTY_STORAGE_ERROR_BLOCK_MAC_MISMATCH = 2,
30   TRUSTY_STORAGE_ERROR_BLOCK_HEADER_INVALID = 3,
31   TRUSTY_STORAGE_ERROR_RPMB_COUNTER_MISMATCH = 4,
32   TRUSTY_STORAGE_ERROR_RPMB_COUNTER_MISMATCH_RECOVERED = 5,
33   TRUSTY_STORAGE_ERROR_RPMB_COUNTER_READ_FAILURE = 6,
34   TRUSTY_STORAGE_ERROR_RPMB_MAC_MISMATCH = 7,
35   TRUSTY_STORAGE_ERROR_RPMB_ADDR_MISMATCH = 8,
36   TRUSTY_STORAGE_ERROR_RPMB_FAILURE_RESPONSE = 9,
37   TRUSTY_STORAGE_ERROR_RPMB_UNKNOWN = 10,
38   TRUSTY_STORAGE_ERROR_RPMB_SCSI_ERROR = 11,
39   TRUSTY_STORAGE_ERROR_IO_ERROR = 12,
40   TRUSTY_STORAGE_ERROR_PROXY_COMMUNICATION_FAILURE = 13,
41 };
42 
43 enum trusty_file_system {
44   TRUSTY_FS_UNKNOWN = 0,
45   TRUSTY_FS_TP = 1,
46   TRUSTY_FS_TD = 2,
47   TRUSTY_FS_TDP = 3,
48   TRUSTY_FS_TDEA = 4,
49   TRUSTY_FS_NSP = 5,
50 };
51 
52 enum trusty_block_type {
53   TRUSTY_BLOCKTYPE_UNKNOWN = 0,
54   TRUSTY_BLOCKTYPE_FILES_ROOT = 1,
55   TRUSTY_BLOCKTYPE_FREE_ROOT = 2,
56   TRUSTY_BLOCKTYPE_FILES_INTERNAL = 3,
57   TRUSTY_BLOCKTYPE_FREE_INTERNAL = 4,
58   TRUSTY_BLOCKTYPE_FILE_ENTRY = 5,
59   TRUSTY_BLOCKTYPE_FILE_BLOCK_MAP = 6,
60   TRUSTY_BLOCKTYPE_FILE_DATA = 7,
61   TRUSTY_BLOCKTYPE_CHECKPOINT_ROOT = 8,
62   TRUSTY_BLOCKTYPE_CHECKPOINT_FILES_ROOT = 9,
63   TRUSTY_BLOCKTYPE_CHECKPOINT_FREE_ROOT = 10,
64 };
65 
66 struct metrics_report_storage_error_req {
67     enum trusty_storage_error_type error;
68     char app_id[UUID_STR_SIZE];
69     char client_app_id[UUID_STR_SIZE];
70     uint32_t write;
71     enum trusty_file_system file_system;
72     uint64_t file_path_hash;
73     enum trusty_block_type block_type;
74     uint64_t repair_counter;
75 } __attribute__((__packed__));
76 
77 void do_error_report(enum trusty_storage_error_type type,
78                      const char* fs_name,
79                      enum trusty_block_type block_type);
80 
81 int connect_metrics_ta(void);
82 
error_report_superblock_invalid(const char * fs_name)83 static inline void error_report_superblock_invalid(const char* fs_name) {
84     do_error_report(TRUSTY_STORAGE_ERROR_SUPERBLOCK_INVALID, fs_name,
85                     TRUSTY_BLOCKTYPE_UNKNOWN);
86 }
87 
error_report_block_mac_mismatch(const char * fs_name,enum trusty_block_type block_type)88 static inline void error_report_block_mac_mismatch(
89         const char* fs_name,
90         enum trusty_block_type block_type) {
91     do_error_report(TRUSTY_STORAGE_ERROR_BLOCK_MAC_MISMATCH, fs_name,
92                     block_type);
93 }
94 
error_report_rpmb_counter_mismatch(void)95 static inline void error_report_rpmb_counter_mismatch(void) {
96     do_error_report(TRUSTY_STORAGE_ERROR_RPMB_COUNTER_MISMATCH, "unknown",
97                     TRUSTY_BLOCKTYPE_UNKNOWN);
98 }
99 
error_report_rpmb_counter_mismatch_recovered(void)100 static inline void error_report_rpmb_counter_mismatch_recovered(void) {
101     do_error_report(TRUSTY_STORAGE_ERROR_RPMB_COUNTER_MISMATCH_RECOVERED,
102                     "unknown", TRUSTY_BLOCKTYPE_UNKNOWN);
103 }
104 
error_report_rpmb_counter_read_failure(void)105 static inline void error_report_rpmb_counter_read_failure(void) {
106     do_error_report(TRUSTY_STORAGE_ERROR_RPMB_COUNTER_READ_FAILURE, "unknown",
107                     TRUSTY_BLOCKTYPE_UNKNOWN);
108 }
109 
error_report_rpmb_mac_mismatch(void)110 static inline void error_report_rpmb_mac_mismatch(void) {
111     do_error_report(TRUSTY_STORAGE_ERROR_RPMB_MAC_MISMATCH, "unknown",
112                     TRUSTY_BLOCKTYPE_UNKNOWN);
113 }
114 
error_report_rpmb_addr_mismatch(void)115 static inline void error_report_rpmb_addr_mismatch(void) {
116     do_error_report(TRUSTY_STORAGE_ERROR_RPMB_ADDR_MISMATCH, "unknown",
117                     TRUSTY_BLOCKTYPE_UNKNOWN);
118 }
119