1 /*
2  * Copyright (C) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef OHOS_IPC_SYS_BINDER_H
17 #define OHOS_IPC_SYS_BINDER_H
18 
19 #include <sys/types.h>
20 
21 #ifndef _UAPI_LINUX_BINDER_H
22 #define _UAPI_LINUX_BINDER_H
23 
24 #include <linux/types.h>
25 #include <linux/ioctl.h>
26 
27 #ifndef B_PACK_CHARS
28 #define B_PACK_CHARS(c1, c2, c3, c4) ((((c1) << 24)) | (((c2) << 16)) | (((c3) << 8)) | (c4))
29 #endif
30 
31 #define B_TYPE_LARGE 0x85
32 enum {
33     BINDER_TYPE_BINDER = B_PACK_CHARS('s', 'b', '*', B_TYPE_LARGE),
34     BINDER_TYPE_WEAK_BINDER = B_PACK_CHARS('w', 'b', '*', B_TYPE_LARGE),
35     BINDER_TYPE_HANDLE = B_PACK_CHARS('s', 'h', '*', B_TYPE_LARGE),
36     BINDER_TYPE_WEAK_HANDLE = B_PACK_CHARS('w', 'h', '*', B_TYPE_LARGE),
37     BINDER_TYPE_FD = B_PACK_CHARS('f', 'd', '*', B_TYPE_LARGE),
38     BINDER_TYPE_FDA = B_PACK_CHARS('f', 'd', 'a', B_TYPE_LARGE),
39     BINDER_TYPE_PTR = B_PACK_CHARS('p', 't', '*', B_TYPE_LARGE),
40 };
41 enum {
42     FLAT_BINDER_FLAG_PRIORITY_MASK = 0xff,
43     FLAT_BINDER_FLAG_ACCEPTS_FDS = 0x100,
44     FLAT_BINDER_FLAG_TXN_SECURITY_CTX = 0x1000,
45 };
46 #ifdef BINDER_IPC_32BIT
47 typedef __u32 binder_size_t;
48 typedef __u32 binder_uintptr_t;
49 #else
50 typedef __u64 binder_size_t;
51 typedef __u64 binder_uintptr_t;
52 #endif
53 struct binder_object_header {
54     __u32 type;
55 };
56 struct flat_binder_object {
57     struct binder_object_header hdr;
58     __u32 flags;
59     union {
60         binder_uintptr_t binder;
61         __u32 handle;
62     };
63     binder_uintptr_t cookie;
64 };
65 
66 struct binder_fd_object {
67     struct binder_object_header hdr;
68     __u32 pad_flags;
69     union {
70         binder_uintptr_t pad_binder;
71         __u32 fd;
72     };
73     binder_uintptr_t cookie;
74 };
75 struct binder_buffer_object {
76     struct binder_object_header hdr;
77     __u32 flags;
78     binder_uintptr_t buffer;
79     binder_size_t length;
80     binder_size_t parent;
81     binder_size_t parent_offset;
82 };
83 enum {
84     BINDER_BUFFER_FLAG_HAS_PARENT = 0x01,
85     BINDER_BUFFER_FLAG_HAS_DBINDER = 0x01 << 31,
86 };
87 struct binder_fd_array_object {
88     struct binder_object_header hdr;
89     __u32 pad;
90     binder_size_t num_fds;
91     binder_size_t parent;
92     binder_size_t parent_offset;
93 };
94 struct binder_write_read {
95     binder_size_t write_size;
96     binder_size_t write_consumed;
97     binder_uintptr_t write_buffer;
98     binder_size_t read_size;
99     binder_size_t read_consumed;
100     binder_uintptr_t read_buffer;
101 };
102 struct binder_version {
103     __s32 protocol_version;
104 };
105 #ifdef BINDER_IPC_32BIT
106 #define BINDER_CURRENT_PROTOCOL_VERSION 7
107 #else
108 #define BINDER_CURRENT_PROTOCOL_VERSION 8
109 #endif
110 struct binder_node_debug_info {
111     binder_uintptr_t ptr;
112     binder_uintptr_t cookie;
113     __u32 has_strong_ref;
114     __u32 has_weak_ref;
115 };
116 
117 struct binder_node_info_for_ref {
118     __u32 handle;
119     __u32 strong_count;
120     __u32 weak_count;
121     __u32 reserved1;
122     __u32 reserved2;
123     __u32 reserved3;
124 };
125 
126 #define ACCESS_TOKENID_GET_TOKENID _IOR('A', 1, unsigned long long)
127 #define ACCESS_TOKENID_GET_FTOKENID _IOR('A', 3, unsigned long long)
128 #define ACCESS_TOKEN_FAETURE_MASK (1 << 0)
129 #define SENDER_INFO_FAETURE_MASK (1 << 2)
130 //      ACTV_BINDER_FEATURE_MASK  (1 << 1)
131 
132 struct binder_feature_set {
133     __u64 feature_set;
134 };
135 
136 struct access_token {
137     __u64 sender_tokenid;
138     __u64 first_tokenid;
139     __u64 reserved[2];
140 };
141 
142 struct binder_sender_info {
143     struct access_token tokens;
144     __u64 sender_pid_nr;
145 };
146 
147 #define BINDER_WRITE_READ _IOWR('b', 1, struct binder_write_read)
148 #define BINDER_SET_IDLE_TIMEOUT _IOW('b', 3, __s64)
149 #define BINDER_SET_MAX_THREADS _IOW('b', 5, __u32)
150 #define BINDER_SET_IDLE_PRIORITY _IOW('b', 6, __s32)
151 #define BINDER_SET_CONTEXT_MGR _IOW('b', 7, __s32)
152 #define BINDER_THREAD_EXIT _IOW('b', 8, __s32)
153 #define BINDER_VERSION _IOWR('b', 9, struct binder_version)
154 #define BINDER_GET_NODE_DEBUG_INFO _IOWR('b', 11, struct binder_node_debug_info)
155 #define BINDER_GET_NODE_INFO_FOR_REF _IOWR('b', 12, struct binder_node_info_for_ref)
156 #define BINDER_SET_CONTEXT_MGR_EXT _IOW('b', 13, struct flat_binder_object)
157 #define BINDER_FEATURE_SET _IOWR('b', 30, struct binder_feature_set)
158 #define BINDER_GET_ACCESS_TOKEN _IOWR('b', 31, struct access_token)
159 #define BINDER_GET_SENDER_INFO _IOWR('b', 32, struct binder_sender_info)
160 
161 enum transaction_flags {
162     TF_ONE_WAY = 0x01,
163     TF_ROOT_OBJECT = 0x04,
164     TF_STATUS_CODE = 0x08,
165     TF_ACCEPT_FDS = 0x10,
166     TF_HITRACE = 0x80, // add flag for hitrace
167 };
168 struct binder_transaction_data {
169     union {
170         __u32 handle;
171         binder_uintptr_t ptr;
172     } target;
173     binder_uintptr_t cookie;
174     __u32 code;
175     __u32 flags;
176     pid_t sender_pid;
177     uid_t sender_euid;
178     binder_size_t data_size;
179     binder_size_t offsets_size;
180     union {
181         struct {
182             binder_uintptr_t buffer;
183             binder_uintptr_t offsets;
184         } ptr;
185         __u8 buf[8];
186     } data;
187 };
188 
189 struct binder_transaction_data_secctx {
190     struct binder_transaction_data transaction_data;
191     binder_uintptr_t secctx;
192 };
193 
194 struct binder_transaction_data_sg {
195     struct binder_transaction_data transaction_data;
196     binder_size_t buffers_size;
197 };
198 
199 struct binder_ptr_cookie {
200     binder_uintptr_t ptr;
201     binder_uintptr_t cookie;
202 };
203 struct binder_handle_cookie {
204     __u32 handle;
205     binder_uintptr_t cookie;
206 } __attribute__((__packed__));
207 struct binder_pri_desc {
208     __s32 priority;
209     __u32 desc;
210 };
211 struct binder_pri_ptr_cookie {
212     __s32 priority;
213     binder_uintptr_t ptr;
214     binder_uintptr_t cookie;
215 };
216 
217 /* 31~30(2bit) rw, 29~16(14bit) size, 15~8(8bit) magic, 7~0(8bit) seq */
218 enum binder_driver_return_protocol {
219     BR_ERROR = _IOR('r', 0, __s32),
220     BR_OK = _IO('r', 1),
221     BR_TRANSACTION_SEC_CTX = _IOR('r', 2, struct binder_transaction_data_secctx),
222     BR_TRANSACTION = _IOR('r', 2, struct binder_transaction_data),
223     BR_REPLY = _IOR('r', 3, struct binder_transaction_data),
224     BR_ACQUIRE_RESULT = _IOR('r', 4, __s32),
225     BR_DEAD_REPLY = _IO('r', 5),
226     BR_TRANSACTION_COMPLETE = _IO('r', 6),
227     BR_INCREFS = _IOR('r', 7, struct binder_ptr_cookie),
228     BR_ACQUIRE = _IOR('r', 8, struct binder_ptr_cookie),
229     BR_RELEASE = _IOR('r', 9, struct binder_ptr_cookie),
230     BR_DECREFS = _IOR('r', 10, struct binder_ptr_cookie),
231     BR_ATTEMPT_ACQUIRE = _IOR('r', 11, struct binder_pri_ptr_cookie),
232     BR_NOOP = _IO('r', 12),
233     BR_SPAWN_LOOPER = _IO('r', 13),
234     BR_FINISHED = _IO('r', 14),
235     BR_DEAD_BINDER = _IOR('r', 15, binder_uintptr_t),
236     BR_CLEAR_DEATH_NOTIFICATION_DONE = _IOR('r', 16, binder_uintptr_t),
237     BR_FAILED_REPLY = _IO('r', 17),
238     BR_RELEASE_NODE = _IO('r', 18),
239     BR_TRANSLATION_COMPLETE = _IOR('r', 20, __u32),
240 };
241 enum binder_driver_command_protocol {
242     BC_TRANSACTION = _IOW('c', 0, struct binder_transaction_data),
243     BC_REPLY = _IOW('c', 1, struct binder_transaction_data),
244     BC_ACQUIRE_RESULT = _IOW('c', 2, __s32),
245     BC_FREE_BUFFER = _IOW('c', 3, binder_uintptr_t),
246     BC_INCREFS = _IOW('c', 4, __u32),
247     BC_ACQUIRE = _IOW('c', 5, __u32),
248     BC_RELEASE = _IOW('c', 6, __u32),
249     BC_DECREFS = _IOW('c', 7, __u32),
250     BC_INCREFS_DONE = _IOW('c', 8, struct binder_ptr_cookie),
251     BC_ACQUIRE_DONE = _IOW('c', 9, struct binder_ptr_cookie),
252     BC_ATTEMPT_ACQUIRE = _IOW('c', 10, struct binder_pri_desc),
253     BC_REGISTER_LOOPER = _IO('c', 11),
254     BC_ENTER_LOOPER = _IO('c', 12),
255     BC_EXIT_LOOPER = _IO('c', 13),
256     BC_REQUEST_DEATH_NOTIFICATION = _IOW('c', 14, struct binder_handle_cookie),
257     BC_CLEAR_DEATH_NOTIFICATION = _IOW('c', 15, struct binder_handle_cookie),
258     BC_DEAD_BINDER_DONE = _IOW('c', 16, binder_uintptr_t),
259     BC_TRANSACTION_SG = _IOW('c', 17, struct binder_transaction_data_sg),
260     BC_REPLY_SG = _IOW('c', 18, struct binder_transaction_data_sg),
261     BC_SEND_RAWDATA = _IOW('c', 20, __u32),
262     BC_TRANSLATION = _IOW('c', 21, struct flat_binder_object),
263 };
264 #endif /* * _UAPI_LINUX_BINDER_H * */
265 
266 static const uint32_t DBINDER_MAGICWORD = 0X4442494E;
267 static const uint32_t ENCRYPT_HEAD_LEN = 28;
268 static const uint32_t DEVICEID_LENGTH = 64;
269 static const uint32_t PID_LEN = 32;
270 static const uint32_t RPC_DEFAULT_VERSION_NUM = 1;
271 static const uint32_t SUPPORT_TOKENID_VERSION_NUM = 2;
272 static const uint32_t TOKENID_MAGIC = 0X544F4B49;
273 static const uint32_t SESSION_NAME_LENGTH = 64;
274 static const uint32_t DBINDER_DESC_LENGTH = 64;
275 
276 enum {
277     BINDER_TYPE_REMOTE_BINDER = B_PACK_CHARS('r', 'b', '*', B_TYPE_LARGE),
278     BINDER_TYPE_REMOTE_HANDLE = B_PACK_CHARS('r', 'h', '*', B_TYPE_LARGE),
279     ZBINDER_TYPE_REMOTE_NODE = B_PACK_CHARS('r', 'n', '*', B_TYPE_LARGE),
280     ZBINDER_TYPE_NODE = B_PACK_CHARS('s', 'n', '*', B_TYPE_LARGE),
281     BINDER_TYPE_FDR = B_PACK_CHARS('f', 'd', 'r', B_TYPE_LARGE),
282     BINDER_TYPE_INVALID_HANDLE = B_PACK_CHARS('r', 'h', 'e', B_TYPE_LARGE),
283     BINDER_TYPE_INVALID_BINDER = B_PACK_CHARS('r', 'b', 'e', B_TYPE_LARGE),
284     BINDER_TYPE_INVALID_TYPE = B_PACK_CHARS('r', 't', 'e', B_TYPE_LARGE),
285 };
286 
287 struct binder_ptr_count {
288     binder_uintptr_t ptr;
289     uint32_t count;
290 };
291 
292 struct dbinder_transaction_data {
293     __u32 sizeOfSelf;
294     __u32 magic;
295     __u32 version;
296     int cmd;
297     __u32 code;
298     __u32 flags;
299     __u64 cookie;
300     __u64 seqNumber;
301     binder_size_t buffer_size;
302     binder_size_t offsets_size;
303     binder_uintptr_t offsets;
304     unsigned char buffer[];
305 };
306 
307 struct dbinder_negotiation_data {
308     __u32 proto;
309     __u32 tokenid;
310     __u64 stub_index;
311     char target_name[SESSION_NAME_LENGTH + 1];
312     char local_name[SESSION_NAME_LENGTH + 1];
313     char target_device[DEVICEID_LENGTH + 1];
314     char local_device[DEVICEID_LENGTH + 1];
315     char16_t desc[DBINDER_DESC_LENGTH + 1];
316     char reserved[3];
317 };
318 #endif // OHOS_IPC_SYS_BINDER_H
319