• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef __linux__
66 #define HMB_ERROR_INFO_LEN 64
67 struct hmb_detailed_err {
68     uint32_t err_code;
69     char err_str[HMB_ERROR_INFO_LEN];
70 };
71 #endif
72 struct binder_fd_object {
73     struct binder_object_header hdr;
74     __u32 pad_flags;
75     union {
76         binder_uintptr_t pad_binder;
77         __u32 fd;
78     };
79     binder_uintptr_t cookie;
80 };
81 struct binder_buffer_object {
82     struct binder_object_header hdr;
83     __u32 flags;
84     binder_uintptr_t buffer;
85     binder_size_t length;
86     binder_size_t parent;
87     binder_size_t parent_offset;
88 };
89 enum {
90     BINDER_BUFFER_FLAG_HAS_PARENT = 0x01,
91     BINDER_BUFFER_FLAG_HAS_DBINDER = 0x01 << 31,
92 };
93 struct binder_fd_array_object {
94     struct binder_object_header hdr;
95     __u32 pad;
96     binder_size_t num_fds;
97     binder_size_t parent;
98     binder_size_t parent_offset;
99 };
100 struct binder_write_read {
101     binder_size_t write_size;
102     binder_size_t write_consumed;
103     binder_uintptr_t write_buffer;
104     binder_size_t read_size;
105     binder_size_t read_consumed;
106     binder_uintptr_t read_buffer;
107 };
108 struct binder_version {
109     __s32 protocol_version;
110 };
111 #ifdef BINDER_IPC_32BIT
112 #define BINDER_CURRENT_PROTOCOL_VERSION 7
113 #else
114 #define BINDER_CURRENT_PROTOCOL_VERSION 8
115 #endif
116 struct binder_node_debug_info {
117     binder_uintptr_t ptr;
118     binder_uintptr_t cookie;
119     __u32 has_strong_ref;
120     __u32 has_weak_ref;
121 };
122 
123 struct binder_node_info_for_ref {
124     __u32 handle;
125     __u32 strong_count;
126     __u32 weak_count;
127     __u32 reserved1;
128     __u32 reserved2;
129     __u32 reserved3;
130 };
131 
132 #define ACCESS_TOKENID_GET_TOKENID _IOR('A', 1, unsigned long long)
133 #define ACCESS_TOKENID_GET_FTOKENID _IOR('A', 3, unsigned long long)
134 #define ACCESS_TOKEN_FAETURE_MASK (1 << 0)
135 #define SENDER_INFO_FAETURE_MASK (1 << 2)
136 //      ACTV_BINDER_FEATURE_MASK  (1 << 1)
137 
138 struct binder_feature_set {
139     __u64 feature_set;
140 };
141 
142 struct access_token {
143     __u64 sender_tokenid;
144     __u64 first_tokenid;
145     __u64 reserved[2];
146 };
147 
148 struct binder_sender_info {
149     struct access_token tokens;
150     __u64 sender_pid_nr;
151 };
152 
153 #define BINDER_WRITE_READ _IOWR('b', 1, struct binder_write_read)
154 #define BINDER_SET_IDLE_TIMEOUT _IOW('b', 3, __s64)
155 #define BINDER_SET_MAX_THREADS _IOW('b', 5, __u32)
156 #define BINDER_SET_IDLE_PRIORITY _IOW('b', 6, __s32)
157 #define BINDER_SET_CONTEXT_MGR _IOW('b', 7, __s32)
158 #define BINDER_THREAD_EXIT _IOW('b', 8, __s32)
159 #define BINDER_VERSION _IOWR('b', 9, struct binder_version)
160 #define BINDER_GET_NODE_DEBUG_INFO _IOWR('b', 11, struct binder_node_debug_info)
161 #define BINDER_GET_NODE_INFO_FOR_REF _IOWR('b', 12, struct binder_node_info_for_ref)
162 #define BINDER_SET_CONTEXT_MGR_EXT _IOW('b', 13, struct flat_binder_object)
163 #define BINDER_FEATURE_SET _IOWR('b', 30, struct binder_feature_set)
164 #define BINDER_GET_ACCESS_TOKEN _IOWR('b', 31, struct access_token)
165 #define BINDER_GET_SENDER_INFO _IOWR('b', 32, struct binder_sender_info)
166 #define BINDER_THREAD_RECLAIM _IOW('b', 33, __s32)
167 
168 enum transaction_flags {
169     TF_ONE_WAY = 0x01,
170     TF_ROOT_OBJECT = 0x04,
171     TF_STATUS_CODE = 0x08,
172     TF_ACCEPT_FDS = 0x10,
173     TF_HITRACE = 0x80, // add flag for hitrace
174 };
175 struct binder_transaction_data {
176     union {
177         __u32 handle;
178         binder_uintptr_t ptr;
179     } target;
180     binder_uintptr_t cookie;
181     __u32 code;
182     __u32 flags;
183     pid_t sender_pid;
184     uid_t sender_euid;
185     binder_size_t data_size;
186     binder_size_t offsets_size;
187     union {
188         struct {
189             binder_uintptr_t buffer;
190             binder_uintptr_t offsets;
191         } ptr;
192         __u8 buf[8];
193     } data;
194 };
195 
196 struct binder_transaction_data_secctx {
197     struct binder_transaction_data transaction_data;
198     binder_uintptr_t secctx;
199 };
200 
201 struct binder_transaction_data_sg {
202     struct binder_transaction_data transaction_data;
203     binder_size_t buffers_size;
204 };
205 
206 struct binder_ptr_cookie {
207     binder_uintptr_t ptr;
208     binder_uintptr_t cookie;
209 };
210 struct binder_handle_cookie {
211     __u32 handle;
212     binder_uintptr_t cookie;
213 } __attribute__((__packed__));
214 struct binder_pri_desc {
215     __s32 priority;
216     __u32 desc;
217 };
218 struct binder_pri_ptr_cookie {
219     __s32 priority;
220     binder_uintptr_t ptr;
221     binder_uintptr_t cookie;
222 };
223 
224 /* 31~30(2bit) rw, 29~16(14bit) size, 15~8(8bit) magic, 7~0(8bit) seq */
225 enum binder_driver_return_protocol {
226     BR_ERROR = _IOR('r', 0, __s32),
227     BR_OK = _IO('r', 1),
228     BR_TRANSACTION_SEC_CTX = _IOR('r', 2, struct binder_transaction_data_secctx),
229     BR_TRANSACTION = _IOR('r', 2, struct binder_transaction_data),
230     BR_REPLY = _IOR('r', 3, struct binder_transaction_data),
231     BR_DEAD_REPLY = _IO('r', 5),
232     BR_TRANSACTION_COMPLETE = _IO('r', 6),
233     BR_INCREFS = _IOR('r', 7, struct binder_ptr_cookie),
234     BR_ACQUIRE = _IOR('r', 8, struct binder_ptr_cookie),
235     BR_RELEASE = _IOR('r', 9, struct binder_ptr_cookie),
236     BR_DECREFS = _IOR('r', 10, struct binder_ptr_cookie),
237     BR_ATTEMPT_ACQUIRE = _IOR('r', 11, struct binder_pri_ptr_cookie),
238     BR_NOOP = _IO('r', 12),
239     BR_SPAWN_LOOPER = _IO('r', 13),
240     BR_FINISHED = _IO('r', 14),
241     BR_DEAD_BINDER = _IOR('r', 15, binder_uintptr_t),
242     BR_CLEAR_DEATH_NOTIFICATION_DONE = _IOR('r', 16, binder_uintptr_t),
243     BR_FAILED_REPLY = _IO('r', 17),
244     BR_RELEASE_NODE = _IO('r', 18),
245 };
246 enum binder_driver_command_protocol {
247     BC_TRANSACTION = _IOW('c', 0, struct binder_transaction_data),
248     BC_REPLY = _IOW('c', 1, struct binder_transaction_data),
249     BC_ACQUIRE_RESULT = _IOW('c', 2, __s32),
250     BC_FREE_BUFFER = _IOW('c', 3, binder_uintptr_t),
251     BC_INCREFS = _IOW('c', 4, __u32),
252     BC_ACQUIRE = _IOW('c', 5, __u32),
253     BC_RELEASE = _IOW('c', 6, __u32),
254     BC_DECREFS = _IOW('c', 7, __u32),
255     BC_INCREFS_DONE = _IOW('c', 8, struct binder_ptr_cookie),
256     BC_ACQUIRE_DONE = _IOW('c', 9, struct binder_ptr_cookie),
257     BC_ATTEMPT_ACQUIRE = _IOW('c', 10, struct binder_pri_desc),
258     BC_REGISTER_LOOPER = _IO('c', 11),
259     BC_ENTER_LOOPER = _IO('c', 12),
260     BC_EXIT_LOOPER = _IO('c', 13),
261     BC_REQUEST_DEATH_NOTIFICATION = _IOW('c', 14, struct binder_handle_cookie),
262     BC_CLEAR_DEATH_NOTIFICATION = _IOW('c', 15, struct binder_handle_cookie),
263     BC_DEAD_BINDER_DONE = _IOW('c', 16, binder_uintptr_t),
264     BC_TRANSACTION_SG = _IOW('c', 17, struct binder_transaction_data_sg),
265     BC_REPLY_SG = _IOW('c', 18, struct binder_transaction_data_sg),
266     BC_SEND_RAWDATA = _IOW('c', 20, __u32),
267     BC_TRANSLATION = _IOW('c', 21, struct flat_binder_object),
268 #ifndef __linux__
269     HMB_GET_DETAILED_ERROR = _IOWR('b', 161, struct hmb_detailed_err),
270 #endif
271 };
272 #endif /* * _UAPI_LINUX_BINDER_H * */
273 
274 static const uint32_t DBINDER_MAGICWORD = 0X4442494E;
275 static const uint32_t ENCRYPT_HEAD_LEN = 28;
276 static const uint32_t DEVICEID_LENGTH = 64;
277 static const uint32_t PID_LEN = 32;
278 static const uint32_t RPC_DEFAULT_VERSION_NUM = 1;
279 static const uint32_t SUPPORT_TOKENID_VERSION_NUM = 2;
280 static const uint32_t TOKENID_MAGIC = 0X544F4B49;
281 static const uint32_t SESSION_NAME_LENGTH = 64;
282 static const uint32_t DBINDER_DESC_LENGTH = 64;
283 
284 enum {
285     BINDER_TYPE_REMOTE_BINDER = B_PACK_CHARS('r', 'b', '*', B_TYPE_LARGE),
286     BINDER_TYPE_REMOTE_HANDLE = B_PACK_CHARS('r', 'h', '*', B_TYPE_LARGE),
287     ZBINDER_TYPE_REMOTE_NODE = B_PACK_CHARS('r', 'n', '*', B_TYPE_LARGE),
288     ZBINDER_TYPE_NODE = B_PACK_CHARS('s', 'n', '*', B_TYPE_LARGE),
289     BINDER_TYPE_FDR = B_PACK_CHARS('f', 'd', 'r', B_TYPE_LARGE),
290     BINDER_TYPE_INVALID_HANDLE = B_PACK_CHARS('r', 'h', 'e', B_TYPE_LARGE),
291     BINDER_TYPE_INVALID_BINDER = B_PACK_CHARS('r', 'b', 'e', B_TYPE_LARGE),
292     BINDER_TYPE_INVALID_TYPE = B_PACK_CHARS('r', 't', 'e', B_TYPE_LARGE),
293 };
294 
295 struct binder_ptr_count {
296     binder_uintptr_t ptr;
297     uint32_t count;
298 };
299 
300 struct dbinder_transaction_data {
301     __u32 sizeOfSelf;
302     __u32 magic;
303     __u32 version;
304     int cmd;
305     __u32 code;
306     __u32 flags;
307     __u64 cookie;
308     __u64 seqNumber;
309     binder_size_t buffer_size;
310     binder_size_t offsets_size;
311     binder_uintptr_t offsets;
312     unsigned char buffer[];
313 };
314 
315 struct dbinder_negotiation_data {
316     __u32 proto;
317     __u32 tokenid;
318     __u64 stub_index;
319     char target_name[SESSION_NAME_LENGTH + 1];
320     char local_name[SESSION_NAME_LENGTH + 1];
321     char target_device[DEVICEID_LENGTH + 1];
322     char local_device[DEVICEID_LENGTH + 1];
323     char16_t desc[DBINDER_DESC_LENGTH + 1];
324     char reserved[3];
325 };
326 #endif // OHOS_IPC_SYS_BINDER_H
327