blob: a2b4da2f69205b09476a3fdf9191b2212c5c2101 [file] [log] [blame]
Mike Lockwood94afecf2012-10-24 10:45:23 -07001/* Copyright 2008 The Android Open Source Project
2 */
3
Mark Salyzyna5e161b2016-09-29 08:08:05 -07004#define LOG_TAG "Binder"
5
6#include <errno.h>
7#include <fcntl.h>
Arve Hjønnevåg399b6c32014-01-28 21:25:12 -08008#include <inttypes.h>
Mike Lockwood94afecf2012-10-24 10:45:23 -07009#include <stdio.h>
10#include <stdlib.h>
Elliott Hughes824e30e2015-01-29 22:32:32 -080011#include <string.h>
Mike Lockwood94afecf2012-10-24 10:45:23 -070012#include <sys/mman.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070013#include <unistd.h>
14
Mark Salyzyn7823e122016-09-29 08:08:05 -070015#include <log/log.h>
Mike Lockwood94afecf2012-10-24 10:45:23 -070016
17#include "binder.h"
18
19#define MAX_BIO_SIZE (1 << 30)
20
21#define TRACE 0
22
Serban Constantinescubcf38882014-01-10 13:56:27 +000023void bio_init_from_txn(struct binder_io *io, struct binder_transaction_data *txn);
Mike Lockwood94afecf2012-10-24 10:45:23 -070024
25#if TRACE
Serban Constantinescu9b738bb2014-01-10 15:48:29 +000026void hexdump(void *_data, size_t len)
Mike Lockwood94afecf2012-10-24 10:45:23 -070027{
28 unsigned char *data = _data;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +000029 size_t count;
Mike Lockwood94afecf2012-10-24 10:45:23 -070030
31 for (count = 0; count < len; count++) {
32 if ((count & 15) == 0)
Serban Constantinescu9b738bb2014-01-10 15:48:29 +000033 fprintf(stderr,"%04zu:", count);
Mike Lockwood94afecf2012-10-24 10:45:23 -070034 fprintf(stderr," %02x %c", *data,
35 (*data < 32) || (*data > 126) ? '.' : *data);
36 data++;
37 if ((count & 15) == 15)
38 fprintf(stderr,"\n");
39 }
40 if ((count & 15) != 0)
41 fprintf(stderr,"\n");
42}
43
Serban Constantinescubcf38882014-01-10 13:56:27 +000044void binder_dump_txn(struct binder_transaction_data *txn)
Mike Lockwood94afecf2012-10-24 10:45:23 -070045{
Serban Constantinescubcf38882014-01-10 13:56:27 +000046 struct flat_binder_object *obj;
Arve Hjønnevåg399b6c32014-01-28 21:25:12 -080047 binder_size_t *offs = (binder_size_t *)(uintptr_t)txn->data.ptr.offsets;
48 size_t count = txn->offsets_size / sizeof(binder_size_t);
Mike Lockwood94afecf2012-10-24 10:45:23 -070049
Arve Hjønnevåg399b6c32014-01-28 21:25:12 -080050 fprintf(stderr," target %016"PRIx64" cookie %016"PRIx64" code %08x flags %08x\n",
51 (uint64_t)txn->target.ptr, (uint64_t)txn->cookie, txn->code, txn->flags);
52 fprintf(stderr," pid %8d uid %8d data %"PRIu64" offs %"PRIu64"\n",
53 txn->sender_pid, txn->sender_euid, (uint64_t)txn->data_size, (uint64_t)txn->offsets_size);
54 hexdump((void *)(uintptr_t)txn->data.ptr.buffer, txn->data_size);
Mike Lockwood94afecf2012-10-24 10:45:23 -070055 while (count--) {
Arve Hjønnevåg399b6c32014-01-28 21:25:12 -080056 obj = (struct flat_binder_object *) (((char*)(uintptr_t)txn->data.ptr.buffer) + *offs++);
57 fprintf(stderr," - type %08x flags %08x ptr %016"PRIx64" cookie %016"PRIx64"\n",
58 obj->type, obj->flags, (uint64_t)obj->binder, (uint64_t)obj->cookie);
Mike Lockwood94afecf2012-10-24 10:45:23 -070059 }
60}
61
62#define NAME(n) case n: return #n
63const char *cmd_name(uint32_t cmd)
64{
65 switch(cmd) {
66 NAME(BR_NOOP);
67 NAME(BR_TRANSACTION_COMPLETE);
68 NAME(BR_INCREFS);
69 NAME(BR_ACQUIRE);
70 NAME(BR_RELEASE);
71 NAME(BR_DECREFS);
72 NAME(BR_TRANSACTION);
73 NAME(BR_REPLY);
74 NAME(BR_FAILED_REPLY);
75 NAME(BR_DEAD_REPLY);
76 NAME(BR_DEAD_BINDER);
77 default: return "???";
78 }
79}
80#else
81#define hexdump(a,b) do{} while (0)
82#define binder_dump_txn(txn) do{} while (0)
83#endif
84
85#define BIO_F_SHARED 0x01 /* needs to be buffer freed */
86#define BIO_F_OVERFLOW 0x02 /* ran out of space */
87#define BIO_F_IOERROR 0x04
88#define BIO_F_MALLOCED 0x08 /* needs to be free()'d */
89
90struct binder_state
91{
92 int fd;
93 void *mapped;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +000094 size_t mapsize;
Mike Lockwood94afecf2012-10-24 10:45:23 -070095};
96
Martijn Coenen69b05152017-03-21 10:00:38 -070097struct binder_state *binder_open(const char* driver, size_t mapsize)
Mike Lockwood94afecf2012-10-24 10:45:23 -070098{
99 struct binder_state *bs;
Serban Constantinescua44542c2014-01-30 15:16:45 +0000100 struct binder_version vers;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700101
102 bs = malloc(sizeof(*bs));
103 if (!bs) {
104 errno = ENOMEM;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000105 return NULL;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700106 }
107
Martijn Coenen69b05152017-03-21 10:00:38 -0700108 bs->fd = open(driver, O_RDWR | O_CLOEXEC);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700109 if (bs->fd < 0) {
Martijn Coenen69b05152017-03-21 10:00:38 -0700110 fprintf(stderr,"binder: cannot open %s (%s)\n",
111 driver, strerror(errno));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700112 goto fail_open;
113 }
114
Serban Constantinescua44542c2014-01-30 15:16:45 +0000115 if ((ioctl(bs->fd, BINDER_VERSION, &vers) == -1) ||
116 (vers.protocol_version != BINDER_CURRENT_PROTOCOL_VERSION)) {
Serban Constantinescu018cf412014-02-19 15:34:02 +0000117 fprintf(stderr,
118 "binder: kernel driver version (%d) differs from user space version (%d)\n",
119 vers.protocol_version, BINDER_CURRENT_PROTOCOL_VERSION);
Serban Constantinescua44542c2014-01-30 15:16:45 +0000120 goto fail_open;
121 }
122
Mike Lockwood94afecf2012-10-24 10:45:23 -0700123 bs->mapsize = mapsize;
124 bs->mapped = mmap(NULL, mapsize, PROT_READ, MAP_PRIVATE, bs->fd, 0);
125 if (bs->mapped == MAP_FAILED) {
126 fprintf(stderr,"binder: cannot map device (%s)\n",
127 strerror(errno));
128 goto fail_map;
129 }
130
Mike Lockwood94afecf2012-10-24 10:45:23 -0700131 return bs;
132
133fail_map:
134 close(bs->fd);
135fail_open:
136 free(bs);
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000137 return NULL;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700138}
139
140void binder_close(struct binder_state *bs)
141{
142 munmap(bs->mapped, bs->mapsize);
143 close(bs->fd);
144 free(bs);
145}
146
147int binder_become_context_manager(struct binder_state *bs)
148{
Steven Moreland3d795472019-01-04 17:51:30 -0800149 struct flat_binder_object obj;
150 memset(&obj, 0, sizeof(obj));
151 obj.flags = FLAT_BINDER_FLAG_TXN_SECURITY_CTX;
152
153 int result = ioctl(bs->fd, BINDER_SET_CONTEXT_MGR_EXT, &obj);
154
155 // fallback to original method
156 if (result != 0) {
157#ifndef VENDORSERVICEMANAGER
158 android_errorWriteLog(0x534e4554, "121035042");
159#endif
160
161 result = ioctl(bs->fd, BINDER_SET_CONTEXT_MGR, 0);
162 }
163 return result;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700164}
165
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000166int binder_write(struct binder_state *bs, void *data, size_t len)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700167{
168 struct binder_write_read bwr;
169 int res;
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000170
Mike Lockwood94afecf2012-10-24 10:45:23 -0700171 bwr.write_size = len;
172 bwr.write_consumed = 0;
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000173 bwr.write_buffer = (uintptr_t) data;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700174 bwr.read_size = 0;
175 bwr.read_consumed = 0;
176 bwr.read_buffer = 0;
177 res = ioctl(bs->fd, BINDER_WRITE_READ, &bwr);
178 if (res < 0) {
179 fprintf(stderr,"binder_write: ioctl failed (%s)\n",
180 strerror(errno));
181 }
182 return res;
183}
184
dcashman51f592c2016-03-11 10:38:10 -0800185void binder_free_buffer(struct binder_state *bs,
186 binder_uintptr_t buffer_to_free)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700187{
188 struct {
189 uint32_t cmd_free;
Arve Hjønnevåg399b6c32014-01-28 21:25:12 -0800190 binder_uintptr_t buffer;
dcashman51f592c2016-03-11 10:38:10 -0800191 } __attribute__((packed)) data;
192 data.cmd_free = BC_FREE_BUFFER;
193 data.buffer = buffer_to_free;
194 binder_write(bs, &data, sizeof(data));
195}
196
197void binder_send_reply(struct binder_state *bs,
198 struct binder_io *reply,
Mark Salyzyn52aa4a72016-05-25 13:14:10 -0700199 binder_uintptr_t buffer_to_free,
dcashman51f592c2016-03-11 10:38:10 -0800200 int status)
201{
202 struct {
Mark Salyzyn52aa4a72016-05-25 13:14:10 -0700203 uint32_t cmd_free;
204 binder_uintptr_t buffer;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700205 uint32_t cmd_reply;
Serban Constantinescubcf38882014-01-10 13:56:27 +0000206 struct binder_transaction_data txn;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700207 } __attribute__((packed)) data;
208
Mark Salyzyn52aa4a72016-05-25 13:14:10 -0700209 data.cmd_free = BC_FREE_BUFFER;
210 data.buffer = buffer_to_free;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700211 data.cmd_reply = BC_REPLY;
Serban Constantinescubcf38882014-01-10 13:56:27 +0000212 data.txn.target.ptr = 0;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700213 data.txn.cookie = 0;
214 data.txn.code = 0;
215 if (status) {
216 data.txn.flags = TF_STATUS_CODE;
217 data.txn.data_size = sizeof(int);
Serban Constantinescubcf38882014-01-10 13:56:27 +0000218 data.txn.offsets_size = 0;
Arve Hjønnevåg399b6c32014-01-28 21:25:12 -0800219 data.txn.data.ptr.buffer = (uintptr_t)&status;
Serban Constantinescubcf38882014-01-10 13:56:27 +0000220 data.txn.data.ptr.offsets = 0;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700221 } else {
222 data.txn.flags = 0;
223 data.txn.data_size = reply->data - reply->data0;
Serban Constantinescubcf38882014-01-10 13:56:27 +0000224 data.txn.offsets_size = ((char*) reply->offs) - ((char*) reply->offs0);
Arve Hjønnevåg399b6c32014-01-28 21:25:12 -0800225 data.txn.data.ptr.buffer = (uintptr_t)reply->data0;
226 data.txn.data.ptr.offsets = (uintptr_t)reply->offs0;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700227 }
228 binder_write(bs, &data, sizeof(data));
229}
230
231int binder_parse(struct binder_state *bs, struct binder_io *bio,
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000232 uintptr_t ptr, size_t size, binder_handler func)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700233{
234 int r = 1;
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000235 uintptr_t end = ptr + (uintptr_t) size;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700236
237 while (ptr < end) {
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000238 uint32_t cmd = *(uint32_t *) ptr;
239 ptr += sizeof(uint32_t);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700240#if TRACE
241 fprintf(stderr,"%s:\n", cmd_name(cmd));
242#endif
243 switch(cmd) {
244 case BR_NOOP:
245 break;
246 case BR_TRANSACTION_COMPLETE:
247 break;
248 case BR_INCREFS:
249 case BR_ACQUIRE:
250 case BR_RELEASE:
251 case BR_DECREFS:
252#if TRACE
Arve Hjønnevåg399b6c32014-01-28 21:25:12 -0800253 fprintf(stderr," %p, %p\n", (void *)ptr, (void *)(ptr + sizeof(void *)));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700254#endif
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000255 ptr += sizeof(struct binder_ptr_cookie);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700256 break;
Steven Moreland3d795472019-01-04 17:51:30 -0800257 case BR_TRANSACTION_SEC_CTX:
Mike Lockwood94afecf2012-10-24 10:45:23 -0700258 case BR_TRANSACTION: {
Steven Moreland3d795472019-01-04 17:51:30 -0800259 struct binder_transaction_data_secctx txn;
260 if (cmd == BR_TRANSACTION_SEC_CTX) {
261 if ((end - ptr) < sizeof(struct binder_transaction_data_secctx)) {
262 ALOGE("parse: txn too small (binder_transaction_data_secctx)!\n");
263 return -1;
264 }
265 memcpy(&txn, (void*) ptr, sizeof(struct binder_transaction_data_secctx));
266 ptr += sizeof(struct binder_transaction_data_secctx);
267 } else /* BR_TRANSACTION */ {
268 if ((end - ptr) < sizeof(struct binder_transaction_data)) {
269 ALOGE("parse: txn too small (binder_transaction_data)!\n");
270 return -1;
271 }
272 memcpy(&txn.transaction_data, (void*) ptr, sizeof(struct binder_transaction_data));
273 ptr += sizeof(struct binder_transaction_data);
274
275 txn.secctx = 0;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700276 }
Steven Moreland3d795472019-01-04 17:51:30 -0800277
278 binder_dump_txn(&txn.transaction_data);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700279 if (func) {
280 unsigned rdata[256/4];
281 struct binder_io msg;
282 struct binder_io reply;
283 int res;
284
285 bio_init(&reply, rdata, sizeof(rdata), 4);
Steven Moreland3d795472019-01-04 17:51:30 -0800286 bio_init_from_txn(&msg, &txn.transaction_data);
287 res = func(bs, &txn, &msg, &reply);
288 if (txn.transaction_data.flags & TF_ONE_WAY) {
289 binder_free_buffer(bs, txn.transaction_data.data.ptr.buffer);
Mark Salyzyn52aa4a72016-05-25 13:14:10 -0700290 } else {
Steven Moreland3d795472019-01-04 17:51:30 -0800291 binder_send_reply(bs, &reply, txn.transaction_data.data.ptr.buffer, res);
Mark Salyzyn52aa4a72016-05-25 13:14:10 -0700292 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700293 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700294 break;
295 }
296 case BR_REPLY: {
Serban Constantinescubcf38882014-01-10 13:56:27 +0000297 struct binder_transaction_data *txn = (struct binder_transaction_data *) ptr;
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000298 if ((end - ptr) < sizeof(*txn)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700299 ALOGE("parse: reply too small!\n");
300 return -1;
301 }
302 binder_dump_txn(txn);
303 if (bio) {
304 bio_init_from_txn(bio, txn);
305 bio = 0;
306 } else {
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000307 /* todo FREE BUFFER */
Mike Lockwood94afecf2012-10-24 10:45:23 -0700308 }
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000309 ptr += sizeof(*txn);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700310 r = 0;
311 break;
312 }
313 case BR_DEAD_BINDER: {
Arve Hjønnevåg399b6c32014-01-28 21:25:12 -0800314 struct binder_death *death = (struct binder_death *)(uintptr_t) *(binder_uintptr_t *)ptr;
315 ptr += sizeof(binder_uintptr_t);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700316 death->func(bs, death->ptr);
317 break;
318 }
319 case BR_FAILED_REPLY:
320 r = -1;
321 break;
322 case BR_DEAD_REPLY:
323 r = -1;
324 break;
325 default:
326 ALOGE("parse: OOPS %d\n", cmd);
327 return -1;
328 }
329 }
330
331 return r;
332}
333
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000334void binder_acquire(struct binder_state *bs, uint32_t target)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700335{
336 uint32_t cmd[2];
337 cmd[0] = BC_ACQUIRE;
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000338 cmd[1] = target;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700339 binder_write(bs, cmd, sizeof(cmd));
340}
341
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000342void binder_release(struct binder_state *bs, uint32_t target)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700343{
344 uint32_t cmd[2];
345 cmd[0] = BC_RELEASE;
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000346 cmd[1] = target;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700347 binder_write(bs, cmd, sizeof(cmd));
348}
349
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000350void binder_link_to_death(struct binder_state *bs, uint32_t target, struct binder_death *death)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700351{
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000352 struct {
353 uint32_t cmd;
354 struct binder_handle_cookie payload;
355 } __attribute__((packed)) data;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700356
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000357 data.cmd = BC_REQUEST_DEATH_NOTIFICATION;
358 data.payload.handle = target;
359 data.payload.cookie = (uintptr_t) death;
360 binder_write(bs, &data, sizeof(data));
361}
Mike Lockwood94afecf2012-10-24 10:45:23 -0700362
363int binder_call(struct binder_state *bs,
364 struct binder_io *msg, struct binder_io *reply,
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000365 uint32_t target, uint32_t code)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700366{
367 int res;
368 struct binder_write_read bwr;
369 struct {
370 uint32_t cmd;
Serban Constantinescubcf38882014-01-10 13:56:27 +0000371 struct binder_transaction_data txn;
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000372 } __attribute__((packed)) writebuf;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700373 unsigned readbuf[32];
374
375 if (msg->flags & BIO_F_OVERFLOW) {
376 fprintf(stderr,"binder: txn buffer overflow\n");
377 goto fail;
378 }
379
380 writebuf.cmd = BC_TRANSACTION;
Serban Constantinescubcf38882014-01-10 13:56:27 +0000381 writebuf.txn.target.handle = target;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700382 writebuf.txn.code = code;
383 writebuf.txn.flags = 0;
384 writebuf.txn.data_size = msg->data - msg->data0;
Serban Constantinescubcf38882014-01-10 13:56:27 +0000385 writebuf.txn.offsets_size = ((char*) msg->offs) - ((char*) msg->offs0);
Arve Hjønnevåg399b6c32014-01-28 21:25:12 -0800386 writebuf.txn.data.ptr.buffer = (uintptr_t)msg->data0;
387 writebuf.txn.data.ptr.offsets = (uintptr_t)msg->offs0;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700388
389 bwr.write_size = sizeof(writebuf);
390 bwr.write_consumed = 0;
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000391 bwr.write_buffer = (uintptr_t) &writebuf;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000392
Mike Lockwood94afecf2012-10-24 10:45:23 -0700393 hexdump(msg->data0, msg->data - msg->data0);
394 for (;;) {
395 bwr.read_size = sizeof(readbuf);
396 bwr.read_consumed = 0;
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000397 bwr.read_buffer = (uintptr_t) readbuf;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700398
399 res = ioctl(bs->fd, BINDER_WRITE_READ, &bwr);
400
401 if (res < 0) {
402 fprintf(stderr,"binder: ioctl failed (%s)\n", strerror(errno));
403 goto fail;
404 }
405
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000406 res = binder_parse(bs, reply, (uintptr_t) readbuf, bwr.read_consumed, 0);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700407 if (res == 0) return 0;
408 if (res < 0) goto fail;
409 }
410
411fail:
412 memset(reply, 0, sizeof(*reply));
413 reply->flags |= BIO_F_IOERROR;
414 return -1;
415}
416
417void binder_loop(struct binder_state *bs, binder_handler func)
418{
419 int res;
420 struct binder_write_read bwr;
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000421 uint32_t readbuf[32];
Mike Lockwood94afecf2012-10-24 10:45:23 -0700422
423 bwr.write_size = 0;
424 bwr.write_consumed = 0;
425 bwr.write_buffer = 0;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000426
Mike Lockwood94afecf2012-10-24 10:45:23 -0700427 readbuf[0] = BC_ENTER_LOOPER;
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000428 binder_write(bs, readbuf, sizeof(uint32_t));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700429
430 for (;;) {
431 bwr.read_size = sizeof(readbuf);
432 bwr.read_consumed = 0;
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000433 bwr.read_buffer = (uintptr_t) readbuf;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700434
435 res = ioctl(bs->fd, BINDER_WRITE_READ, &bwr);
436
437 if (res < 0) {
438 ALOGE("binder_loop: ioctl failed (%s)\n", strerror(errno));
439 break;
440 }
441
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000442 res = binder_parse(bs, 0, (uintptr_t) readbuf, bwr.read_consumed, func);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700443 if (res == 0) {
444 ALOGE("binder_loop: unexpected reply?!\n");
445 break;
446 }
447 if (res < 0) {
448 ALOGE("binder_loop: io error %d %s\n", res, strerror(errno));
449 break;
450 }
451 }
452}
453
Serban Constantinescubcf38882014-01-10 13:56:27 +0000454void bio_init_from_txn(struct binder_io *bio, struct binder_transaction_data *txn)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700455{
Arve Hjønnevåg399b6c32014-01-28 21:25:12 -0800456 bio->data = bio->data0 = (char *)(intptr_t)txn->data.ptr.buffer;
457 bio->offs = bio->offs0 = (binder_size_t *)(intptr_t)txn->data.ptr.offsets;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700458 bio->data_avail = txn->data_size;
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000459 bio->offs_avail = txn->offsets_size / sizeof(size_t);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700460 bio->flags = BIO_F_SHARED;
461}
462
463void bio_init(struct binder_io *bio, void *data,
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000464 size_t maxdata, size_t maxoffs)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700465{
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000466 size_t n = maxoffs * sizeof(size_t);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700467
468 if (n > maxdata) {
469 bio->flags = BIO_F_OVERFLOW;
470 bio->data_avail = 0;
471 bio->offs_avail = 0;
472 return;
473 }
474
475 bio->data = bio->data0 = (char *) data + n;
476 bio->offs = bio->offs0 = data;
477 bio->data_avail = maxdata - n;
478 bio->offs_avail = maxoffs;
479 bio->flags = 0;
480}
481
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000482static void *bio_alloc(struct binder_io *bio, size_t size)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700483{
484 size = (size + 3) & (~3);
485 if (size > bio->data_avail) {
486 bio->flags |= BIO_F_OVERFLOW;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000487 return NULL;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700488 } else {
489 void *ptr = bio->data;
490 bio->data += size;
491 bio->data_avail -= size;
492 return ptr;
493 }
494}
495
496void binder_done(struct binder_state *bs,
Ian Pedowitzd57d9b92016-02-19 08:34:43 +0000497 __unused struct binder_io *msg,
Mike Lockwood94afecf2012-10-24 10:45:23 -0700498 struct binder_io *reply)
499{
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000500 struct {
501 uint32_t cmd;
502 uintptr_t buffer;
503 } __attribute__((packed)) data;
504
Mike Lockwood94afecf2012-10-24 10:45:23 -0700505 if (reply->flags & BIO_F_SHARED) {
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000506 data.cmd = BC_FREE_BUFFER;
507 data.buffer = (uintptr_t) reply->data0;
508 binder_write(bs, &data, sizeof(data));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700509 reply->flags = 0;
510 }
511}
512
Serban Constantinescubcf38882014-01-10 13:56:27 +0000513static struct flat_binder_object *bio_alloc_obj(struct binder_io *bio)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700514{
Serban Constantinescubcf38882014-01-10 13:56:27 +0000515 struct flat_binder_object *obj;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700516
517 obj = bio_alloc(bio, sizeof(*obj));
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000518
Mike Lockwood94afecf2012-10-24 10:45:23 -0700519 if (obj && bio->offs_avail) {
520 bio->offs_avail--;
521 *bio->offs++ = ((char*) obj) - ((char*) bio->data0);
522 return obj;
523 }
524
525 bio->flags |= BIO_F_OVERFLOW;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000526 return NULL;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700527}
528
529void bio_put_uint32(struct binder_io *bio, uint32_t n)
530{
531 uint32_t *ptr = bio_alloc(bio, sizeof(n));
532 if (ptr)
533 *ptr = n;
534}
535
536void bio_put_obj(struct binder_io *bio, void *ptr)
537{
Serban Constantinescubcf38882014-01-10 13:56:27 +0000538 struct flat_binder_object *obj;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700539
540 obj = bio_alloc_obj(bio);
541 if (!obj)
542 return;
543
544 obj->flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700545 obj->hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg399b6c32014-01-28 21:25:12 -0800546 obj->binder = (uintptr_t)ptr;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700547 obj->cookie = 0;
548}
549
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000550void bio_put_ref(struct binder_io *bio, uint32_t handle)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700551{
Serban Constantinescubcf38882014-01-10 13:56:27 +0000552 struct flat_binder_object *obj;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700553
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000554 if (handle)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700555 obj = bio_alloc_obj(bio);
556 else
557 obj = bio_alloc(bio, sizeof(*obj));
558
559 if (!obj)
560 return;
561
562 obj->flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700563 obj->hdr.type = BINDER_TYPE_HANDLE;
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000564 obj->handle = handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700565 obj->cookie = 0;
566}
567
568void bio_put_string16(struct binder_io *bio, const uint16_t *str)
569{
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000570 size_t len;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700571 uint16_t *ptr;
572
573 if (!str) {
574 bio_put_uint32(bio, 0xffffffff);
575 return;
576 }
577
578 len = 0;
579 while (str[len]) len++;
580
581 if (len >= (MAX_BIO_SIZE / sizeof(uint16_t))) {
582 bio_put_uint32(bio, 0xffffffff);
583 return;
584 }
585
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000586 /* Note: The payload will carry 32bit size instead of size_t */
587 bio_put_uint32(bio, (uint32_t) len);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700588 len = (len + 1) * sizeof(uint16_t);
589 ptr = bio_alloc(bio, len);
590 if (ptr)
591 memcpy(ptr, str, len);
592}
593
594void bio_put_string16_x(struct binder_io *bio, const char *_str)
595{
596 unsigned char *str = (unsigned char*) _str;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000597 size_t len;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700598 uint16_t *ptr;
599
600 if (!str) {
601 bio_put_uint32(bio, 0xffffffff);
602 return;
603 }
604
605 len = strlen(_str);
606
607 if (len >= (MAX_BIO_SIZE / sizeof(uint16_t))) {
608 bio_put_uint32(bio, 0xffffffff);
609 return;
610 }
611
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000612 /* Note: The payload will carry 32bit size instead of size_t */
Mike Lockwood94afecf2012-10-24 10:45:23 -0700613 bio_put_uint32(bio, len);
614 ptr = bio_alloc(bio, (len + 1) * sizeof(uint16_t));
615 if (!ptr)
616 return;
617
618 while (*str)
619 *ptr++ = *str++;
620 *ptr++ = 0;
621}
622
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000623static void *bio_get(struct binder_io *bio, size_t size)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700624{
625 size = (size + 3) & (~3);
626
627 if (bio->data_avail < size){
628 bio->data_avail = 0;
629 bio->flags |= BIO_F_OVERFLOW;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000630 return NULL;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700631 } else {
632 void *ptr = bio->data;
633 bio->data += size;
634 bio->data_avail -= size;
635 return ptr;
636 }
637}
638
639uint32_t bio_get_uint32(struct binder_io *bio)
640{
641 uint32_t *ptr = bio_get(bio, sizeof(*ptr));
642 return ptr ? *ptr : 0;
643}
644
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000645uint16_t *bio_get_string16(struct binder_io *bio, size_t *sz)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700646{
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000647 size_t len;
648
649 /* Note: The payload will carry 32bit size instead of size_t */
650 len = (size_t) bio_get_uint32(bio);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700651 if (sz)
652 *sz = len;
653 return bio_get(bio, (len + 1) * sizeof(uint16_t));
654}
655
Serban Constantinescubcf38882014-01-10 13:56:27 +0000656static struct flat_binder_object *_bio_get_obj(struct binder_io *bio)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700657{
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000658 size_t n;
659 size_t off = bio->data - bio->data0;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700660
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000661 /* TODO: be smarter about this? */
Mike Lockwood94afecf2012-10-24 10:45:23 -0700662 for (n = 0; n < bio->offs_avail; n++) {
663 if (bio->offs[n] == off)
Serban Constantinescubcf38882014-01-10 13:56:27 +0000664 return bio_get(bio, sizeof(struct flat_binder_object));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700665 }
666
667 bio->data_avail = 0;
668 bio->flags |= BIO_F_OVERFLOW;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000669 return NULL;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700670}
671
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000672uint32_t bio_get_ref(struct binder_io *bio)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700673{
Serban Constantinescubcf38882014-01-10 13:56:27 +0000674 struct flat_binder_object *obj;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700675
676 obj = _bio_get_obj(bio);
677 if (!obj)
678 return 0;
679
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700680 if (obj->hdr.type == BINDER_TYPE_HANDLE)
Serban Constantinescubcf38882014-01-10 13:56:27 +0000681 return obj->handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700682
683 return 0;
684}