1  /*
2   * Copyright (C) 2022 Huawei Technologies Co., Ltd.
3   * Licensed under the Mulan PSL v2.
4   * You can use this software according to the terms and conditions of the Mulan PSL v2.
5   * You may obtain a copy of Mulan PSL v2 at:
6   *     http://license.coscl.org.cn/MulanPSL2
7   * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
8   * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
9   * PURPOSE.
10   * See the Mulan PSL v2 for more details.
11   */
12  
13  #ifndef LIBTEEC_FS_WORK_AGENT_H
14  #define LIBTEEC_FS_WORK_AGENT_H
15  
16  #include <stdint.h>
17  #include <stdio.h>
18  #include <unistd.h>
19  #include "fs_work_agent_define.h"
20  
21  #define FILE_NAME_MAX_BUF       256
22  #define FILE_NUM_LIMIT_MAX      1024
23  #define KINDS_OF_SSA_MODE       4
24  
25  #define AID_SYSTEM 1000
26  
27  #define SFS_PARTITION_PERSISTENT "sec_storage/"
28  
29  #define SFS_PARTITION_USER_SYMLINK "sec_storage_data_users/"
30  
31  #define SEC_STORAGE_DATA_USERS  USER_DATA_DIR"sec_storage_data_users/"
32  #define SEC_STORAGE_DATA_USER_0 USER_DATA_DIR"sec_storage_data_users/0"
33  #define SEC_STORAGE_DATA_DIR    USER_DATA_DIR"sec_storage_data/"
34  
35  #define TRANS_BUFF_SIZE (4 * 1024) /* agent transfer share buffer size */
36  
37  #define SEC_STORAGE_ROOT_DIR      "/" SFS_PARTITION_PERSISTENT
38  
39  /* 0600 only root can read and write sec_storage folder */
40  #define ROOT_DIR_PERM                   (S_IRUSR | S_IWUSR)
41  #define SFS_PARTITION_TRANSIENT         "sec_storage_data/"
42  #define SFS_PARTITION_TRANSIENT_PRIVATE "sec_storage_data/_private"
43  #define SFS_PARTITION_TRANSIENT_PERSO   "sec_storage_data/_perso"
44  
45  #define FILE_NAME_INVALID_STR "../" // file name path must not contain ../
46  
47  #define SEC_STORAGE_DATA_CE         "/data/service/el2/"
48  #define SEC_STORAGE_DATA_CE_SUFFIX_DIR   "/tee/" SFS_PARTITION_TRANSIENT
49  #define TEE_OBJECT_STORAGE_CE       0x80000002
50  
51  
52  /* static func declare */
53  enum FsCmdType {
54      SEC_OPEN,
55      SEC_CLOSE,
56      SEC_READ,
57      SEC_WRITE,
58      SEC_SEEK,
59      SEC_REMOVE,
60      SEC_TRUNCATE,
61      SEC_RENAME,
62      SEC_CREATE,
63      SEC_INFO,
64      SEC_ACCESS,
65      SEC_ACCESS2,
66      SEC_FSYNC,
67      SEC_CP,
68      SEC_DISKUSAGE,
69      SEC_DELETE_ALL,
70      SEC_MAX
71  };
72  
73  enum {
74      SEC_WRITE_SLOG,
75      SEC_WRITE_SSA,
76  };
77  
78  struct SecStorageType {
79      enum FsCmdType cmd; /* for s to n */
80      int32_t ret;   /* fxxx call's return */
81      int32_t ret2;  /* fread: end-of-file or error;fwrite:the sendor is SSA or SLOG */
82      uint32_t userId;
83      uint32_t storageId;
84      uint32_t magic;
85      uint32_t error;
86      union Args1 {
87          struct {
88              char mode[KINDS_OF_SSA_MODE];
89              uint32_t nameLen;
90              uint32_t name[1];
91          } open;
92          struct {
93              int32_t fd;
94          } close;
95          struct {
96              int32_t fd;
97              uint32_t count;
98              uint32_t buffer[1]; /* the same as name[0] --> name[1] */
99          } read;
100          struct {
101              int32_t fd;
102              uint32_t count;
103              uint32_t buffer[1];
104          } write;
105          struct {
106              int32_t fd;
107              int32_t offset;
108              uint32_t whence;
109          } seek;
110          struct {
111              uint32_t nameLen;
112              uint32_t name[1];
113          } remove;
114          struct {
115              uint32_t len;
116              uint32_t nameLen;
117              uint32_t name[1];
118          } truncate;
119          struct {
120              uint32_t oldNameLen;
121              uint32_t newNameLen;
122              uint32_t buffer[1]; /* old_name + new_name */
123          } rename;
124          struct {
125              uint32_t fromPathLen;
126              uint32_t toPathLen;
127              uint32_t buffer[1]; /* from_path+to_path */
128          } cp;
129          struct {
130              char mode[KINDS_OF_SSA_MODE];
131              uint32_t nameLen;
132              uint32_t name[1];
133          } create;
134          struct {
135              int32_t fd;
136              uint32_t curPos;
137              uint32_t fileLen;
138          } info;
139          struct {
140              int mode;
141              uint32_t nameLen;
142              uint32_t name[1];
143          } access;
144          struct {
145              int32_t fd;
146          } fsync;
147          struct {
148              uint32_t secStorage;
149              uint32_t data;
150          } diskUsage;
151          struct {
152              uint32_t pathLen;
153              uint32_t path[1];
154          } deleteAll;
155      } args;
156  };
157  
158  struct OpenedFile {
159      FILE *file;
160      struct OpenedFile *next;
161      struct OpenedFile *prev;
162  };
163  
164  void *FsWorkThread(void *control);
165  void SetFileNumLimit(void);
166  
167  #endif
168