blob: 01218c9958a0c9781b0db72910df1512f015ea26 [file] [log] [blame]
Mike Lockwood94afecf2012-10-24 10:45:23 -07001/* Copyright 2008 The Android Open Source Project
2 */
3
Arve Hjønnevåg399b6c32014-01-28 21:25:12 -08004#include <inttypes.h>
Mike Lockwood94afecf2012-10-24 10:45:23 -07005#include <stdio.h>
6#include <stdlib.h>
Elliott Hughes824e30e2015-01-29 22:32:32 -08007#include <string.h>
Mike Lockwood94afecf2012-10-24 10:45:23 -07008#include <errno.h>
9#include <unistd.h>
10#include <fcntl.h>
11#include <sys/mman.h>
12
13#include "binder.h"
14
15#define MAX_BIO_SIZE (1 << 30)
16
17#define TRACE 0
18
19#define LOG_TAG "Binder"
20#include <cutils/log.h>
21
Serban Constantinescubcf38882014-01-10 13:56:27 +000022void bio_init_from_txn(struct binder_io *io, struct binder_transaction_data *txn);
Mike Lockwood94afecf2012-10-24 10:45:23 -070023
24#if TRACE
Serban Constantinescu9b738bb2014-01-10 15:48:29 +000025void hexdump(void *_data, size_t len)
Mike Lockwood94afecf2012-10-24 10:45:23 -070026{
27 unsigned char *data = _data;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +000028 size_t count;
Mike Lockwood94afecf2012-10-24 10:45:23 -070029
30 for (count = 0; count < len; count++) {
31 if ((count & 15) == 0)
Serban Constantinescu9b738bb2014-01-10 15:48:29 +000032 fprintf(stderr,"%04zu:", count);
Mike Lockwood94afecf2012-10-24 10:45:23 -070033 fprintf(stderr," %02x %c", *data,
34 (*data < 32) || (*data > 126) ? '.' : *data);
35 data++;
36 if ((count & 15) == 15)
37 fprintf(stderr,"\n");
38 }
39 if ((count & 15) != 0)
40 fprintf(stderr,"\n");
41}
42
Serban Constantinescubcf38882014-01-10 13:56:27 +000043void binder_dump_txn(struct binder_transaction_data *txn)
Mike Lockwood94afecf2012-10-24 10:45:23 -070044{
Serban Constantinescubcf38882014-01-10 13:56:27 +000045 struct flat_binder_object *obj;
Arve Hjønnevåg399b6c32014-01-28 21:25:12 -080046 binder_size_t *offs = (binder_size_t *)(uintptr_t)txn->data.ptr.offsets;
47 size_t count = txn->offsets_size / sizeof(binder_size_t);
Mike Lockwood94afecf2012-10-24 10:45:23 -070048
Arve Hjønnevåg399b6c32014-01-28 21:25:12 -080049 fprintf(stderr," target %016"PRIx64" cookie %016"PRIx64" code %08x flags %08x\n",
50 (uint64_t)txn->target.ptr, (uint64_t)txn->cookie, txn->code, txn->flags);
51 fprintf(stderr," pid %8d uid %8d data %"PRIu64" offs %"PRIu64"\n",
52 txn->sender_pid, txn->sender_euid, (uint64_t)txn->data_size, (uint64_t)txn->offsets_size);
53 hexdump((void *)(uintptr_t)txn->data.ptr.buffer, txn->data_size);
Mike Lockwood94afecf2012-10-24 10:45:23 -070054 while (count--) {
Arve Hjønnevåg399b6c32014-01-28 21:25:12 -080055 obj = (struct flat_binder_object *) (((char*)(uintptr_t)txn->data.ptr.buffer) + *offs++);
56 fprintf(stderr," - type %08x flags %08x ptr %016"PRIx64" cookie %016"PRIx64"\n",
57 obj->type, obj->flags, (uint64_t)obj->binder, (uint64_t)obj->cookie);
Mike Lockwood94afecf2012-10-24 10:45:23 -070058 }
59}
60
61#define NAME(n) case n: return #n
62const char *cmd_name(uint32_t cmd)
63{
64 switch(cmd) {
65 NAME(BR_NOOP);
66 NAME(BR_TRANSACTION_COMPLETE);
67 NAME(BR_INCREFS);
68 NAME(BR_ACQUIRE);
69 NAME(BR_RELEASE);
70 NAME(BR_DECREFS);
71 NAME(BR_TRANSACTION);
72 NAME(BR_REPLY);
73 NAME(BR_FAILED_REPLY);
74 NAME(BR_DEAD_REPLY);
75 NAME(BR_DEAD_BINDER);
76 default: return "???";
77 }
78}
79#else
80#define hexdump(a,b) do{} while (0)
81#define binder_dump_txn(txn) do{} while (0)
82#endif
83
84#define BIO_F_SHARED 0x01 /* needs to be buffer freed */
85#define BIO_F_OVERFLOW 0x02 /* ran out of space */
86#define BIO_F_IOERROR 0x04
87#define BIO_F_MALLOCED 0x08 /* needs to be free()'d */
88
89struct binder_state
90{
91 int fd;
92 void *mapped;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +000093 size_t mapsize;
Mike Lockwood94afecf2012-10-24 10:45:23 -070094};
95
Serban Constantinescu9b738bb2014-01-10 15:48:29 +000096struct binder_state *binder_open(size_t mapsize)
Mike Lockwood94afecf2012-10-24 10:45:23 -070097{
98 struct binder_state *bs;
Serban Constantinescua44542c2014-01-30 15:16:45 +000099 struct binder_version vers;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700100
101 bs = malloc(sizeof(*bs));
102 if (!bs) {
103 errno = ENOMEM;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000104 return NULL;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700105 }
106
Nick Kralevich0fe7ce32015-12-23 18:58:05 -0800107 bs->fd = open("/dev/binder", O_RDWR | O_CLOEXEC);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700108 if (bs->fd < 0) {
109 fprintf(stderr,"binder: cannot open device (%s)\n",
110 strerror(errno));
111 goto fail_open;
112 }
113
Serban Constantinescua44542c2014-01-30 15:16:45 +0000114 if ((ioctl(bs->fd, BINDER_VERSION, &vers) == -1) ||
115 (vers.protocol_version != BINDER_CURRENT_PROTOCOL_VERSION)) {
Serban Constantinescu018cf412014-02-19 15:34:02 +0000116 fprintf(stderr,
117 "binder: kernel driver version (%d) differs from user space version (%d)\n",
118 vers.protocol_version, BINDER_CURRENT_PROTOCOL_VERSION);
Serban Constantinescua44542c2014-01-30 15:16:45 +0000119 goto fail_open;
120 }
121
Mike Lockwood94afecf2012-10-24 10:45:23 -0700122 bs->mapsize = mapsize;
123 bs->mapped = mmap(NULL, mapsize, PROT_READ, MAP_PRIVATE, bs->fd, 0);
124 if (bs->mapped == MAP_FAILED) {
125 fprintf(stderr,"binder: cannot map device (%s)\n",
126 strerror(errno));
127 goto fail_map;
128 }
129
Mike Lockwood94afecf2012-10-24 10:45:23 -0700130 return bs;
131
132fail_map:
133 close(bs->fd);
134fail_open:
135 free(bs);
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000136 return NULL;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700137}
138
139void binder_close(struct binder_state *bs)
140{
141 munmap(bs->mapped, bs->mapsize);
142 close(bs->fd);
143 free(bs);
144}
145
146int binder_become_context_manager(struct binder_state *bs)
147{
148 return ioctl(bs->fd, BINDER_SET_CONTEXT_MGR, 0);
149}
150
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000151int binder_write(struct binder_state *bs, void *data, size_t len)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700152{
153 struct binder_write_read bwr;
154 int res;
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000155
Mike Lockwood94afecf2012-10-24 10:45:23 -0700156 bwr.write_size = len;
157 bwr.write_consumed = 0;
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000158 bwr.write_buffer = (uintptr_t) data;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700159 bwr.read_size = 0;
160 bwr.read_consumed = 0;
161 bwr.read_buffer = 0;
162 res = ioctl(bs->fd, BINDER_WRITE_READ, &bwr);
163 if (res < 0) {
164 fprintf(stderr,"binder_write: ioctl failed (%s)\n",
165 strerror(errno));
166 }
167 return res;
168}
169
dcashman51f592c2016-03-11 10:38:10 -0800170void binder_free_buffer(struct binder_state *bs,
171 binder_uintptr_t buffer_to_free)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700172{
173 struct {
174 uint32_t cmd_free;
Arve Hjønnevåg399b6c32014-01-28 21:25:12 -0800175 binder_uintptr_t buffer;
dcashman51f592c2016-03-11 10:38:10 -0800176 } __attribute__((packed)) data;
177 data.cmd_free = BC_FREE_BUFFER;
178 data.buffer = buffer_to_free;
179 binder_write(bs, &data, sizeof(data));
180}
181
182void binder_send_reply(struct binder_state *bs,
183 struct binder_io *reply,
184 int status)
185{
186 struct {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700187 uint32_t cmd_reply;
Serban Constantinescubcf38882014-01-10 13:56:27 +0000188 struct binder_transaction_data txn;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700189 } __attribute__((packed)) data;
190
Mike Lockwood94afecf2012-10-24 10:45:23 -0700191 data.cmd_reply = BC_REPLY;
Serban Constantinescubcf38882014-01-10 13:56:27 +0000192 data.txn.target.ptr = 0;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700193 data.txn.cookie = 0;
194 data.txn.code = 0;
195 if (status) {
196 data.txn.flags = TF_STATUS_CODE;
197 data.txn.data_size = sizeof(int);
Serban Constantinescubcf38882014-01-10 13:56:27 +0000198 data.txn.offsets_size = 0;
Arve Hjønnevåg399b6c32014-01-28 21:25:12 -0800199 data.txn.data.ptr.buffer = (uintptr_t)&status;
Serban Constantinescubcf38882014-01-10 13:56:27 +0000200 data.txn.data.ptr.offsets = 0;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700201 } else {
202 data.txn.flags = 0;
203 data.txn.data_size = reply->data - reply->data0;
Serban Constantinescubcf38882014-01-10 13:56:27 +0000204 data.txn.offsets_size = ((char*) reply->offs) - ((char*) reply->offs0);
Arve Hjønnevåg399b6c32014-01-28 21:25:12 -0800205 data.txn.data.ptr.buffer = (uintptr_t)reply->data0;
206 data.txn.data.ptr.offsets = (uintptr_t)reply->offs0;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700207 }
208 binder_write(bs, &data, sizeof(data));
209}
210
211int binder_parse(struct binder_state *bs, struct binder_io *bio,
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000212 uintptr_t ptr, size_t size, binder_handler func)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700213{
214 int r = 1;
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000215 uintptr_t end = ptr + (uintptr_t) size;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700216
217 while (ptr < end) {
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000218 uint32_t cmd = *(uint32_t *) ptr;
219 ptr += sizeof(uint32_t);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700220#if TRACE
221 fprintf(stderr,"%s:\n", cmd_name(cmd));
222#endif
223 switch(cmd) {
224 case BR_NOOP:
225 break;
226 case BR_TRANSACTION_COMPLETE:
227 break;
228 case BR_INCREFS:
229 case BR_ACQUIRE:
230 case BR_RELEASE:
231 case BR_DECREFS:
232#if TRACE
Arve Hjønnevåg399b6c32014-01-28 21:25:12 -0800233 fprintf(stderr," %p, %p\n", (void *)ptr, (void *)(ptr + sizeof(void *)));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700234#endif
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000235 ptr += sizeof(struct binder_ptr_cookie);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700236 break;
237 case BR_TRANSACTION: {
Serban Constantinescubcf38882014-01-10 13:56:27 +0000238 struct binder_transaction_data *txn = (struct binder_transaction_data *) ptr;
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000239 if ((end - ptr) < sizeof(*txn)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700240 ALOGE("parse: txn too small!\n");
241 return -1;
242 }
243 binder_dump_txn(txn);
244 if (func) {
245 unsigned rdata[256/4];
246 struct binder_io msg;
247 struct binder_io reply;
248 int res;
249
250 bio_init(&reply, rdata, sizeof(rdata), 4);
251 bio_init_from_txn(&msg, txn);
252 res = func(bs, txn, &msg, &reply);
dcashman51f592c2016-03-11 10:38:10 -0800253 binder_free_buffer(bs, txn->data.ptr.buffer);
254 if ((txn->flags & TF_ONE_WAY) == 0)
255 binder_send_reply(bs, &reply, res);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700256 }
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000257 ptr += sizeof(*txn);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700258 break;
259 }
260 case BR_REPLY: {
Serban Constantinescubcf38882014-01-10 13:56:27 +0000261 struct binder_transaction_data *txn = (struct binder_transaction_data *) ptr;
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000262 if ((end - ptr) < sizeof(*txn)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700263 ALOGE("parse: reply too small!\n");
264 return -1;
265 }
266 binder_dump_txn(txn);
267 if (bio) {
268 bio_init_from_txn(bio, txn);
269 bio = 0;
270 } else {
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000271 /* todo FREE BUFFER */
Mike Lockwood94afecf2012-10-24 10:45:23 -0700272 }
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000273 ptr += sizeof(*txn);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700274 r = 0;
275 break;
276 }
277 case BR_DEAD_BINDER: {
Arve Hjønnevåg399b6c32014-01-28 21:25:12 -0800278 struct binder_death *death = (struct binder_death *)(uintptr_t) *(binder_uintptr_t *)ptr;
279 ptr += sizeof(binder_uintptr_t);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700280 death->func(bs, death->ptr);
281 break;
282 }
283 case BR_FAILED_REPLY:
284 r = -1;
285 break;
286 case BR_DEAD_REPLY:
287 r = -1;
288 break;
289 default:
290 ALOGE("parse: OOPS %d\n", cmd);
291 return -1;
292 }
293 }
294
295 return r;
296}
297
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000298void binder_acquire(struct binder_state *bs, uint32_t target)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700299{
300 uint32_t cmd[2];
301 cmd[0] = BC_ACQUIRE;
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000302 cmd[1] = target;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700303 binder_write(bs, cmd, sizeof(cmd));
304}
305
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000306void binder_release(struct binder_state *bs, uint32_t target)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700307{
308 uint32_t cmd[2];
309 cmd[0] = BC_RELEASE;
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000310 cmd[1] = target;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700311 binder_write(bs, cmd, sizeof(cmd));
312}
313
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000314void binder_link_to_death(struct binder_state *bs, uint32_t target, struct binder_death *death)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700315{
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000316 struct {
317 uint32_t cmd;
318 struct binder_handle_cookie payload;
319 } __attribute__((packed)) data;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700320
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000321 data.cmd = BC_REQUEST_DEATH_NOTIFICATION;
322 data.payload.handle = target;
323 data.payload.cookie = (uintptr_t) death;
324 binder_write(bs, &data, sizeof(data));
325}
Mike Lockwood94afecf2012-10-24 10:45:23 -0700326
327int binder_call(struct binder_state *bs,
328 struct binder_io *msg, struct binder_io *reply,
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000329 uint32_t target, uint32_t code)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700330{
331 int res;
332 struct binder_write_read bwr;
333 struct {
334 uint32_t cmd;
Serban Constantinescubcf38882014-01-10 13:56:27 +0000335 struct binder_transaction_data txn;
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000336 } __attribute__((packed)) writebuf;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700337 unsigned readbuf[32];
338
339 if (msg->flags & BIO_F_OVERFLOW) {
340 fprintf(stderr,"binder: txn buffer overflow\n");
341 goto fail;
342 }
343
344 writebuf.cmd = BC_TRANSACTION;
Serban Constantinescubcf38882014-01-10 13:56:27 +0000345 writebuf.txn.target.handle = target;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700346 writebuf.txn.code = code;
347 writebuf.txn.flags = 0;
348 writebuf.txn.data_size = msg->data - msg->data0;
Serban Constantinescubcf38882014-01-10 13:56:27 +0000349 writebuf.txn.offsets_size = ((char*) msg->offs) - ((char*) msg->offs0);
Arve Hjønnevåg399b6c32014-01-28 21:25:12 -0800350 writebuf.txn.data.ptr.buffer = (uintptr_t)msg->data0;
351 writebuf.txn.data.ptr.offsets = (uintptr_t)msg->offs0;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700352
353 bwr.write_size = sizeof(writebuf);
354 bwr.write_consumed = 0;
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000355 bwr.write_buffer = (uintptr_t) &writebuf;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000356
Mike Lockwood94afecf2012-10-24 10:45:23 -0700357 hexdump(msg->data0, msg->data - msg->data0);
358 for (;;) {
359 bwr.read_size = sizeof(readbuf);
360 bwr.read_consumed = 0;
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000361 bwr.read_buffer = (uintptr_t) readbuf;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700362
363 res = ioctl(bs->fd, BINDER_WRITE_READ, &bwr);
364
365 if (res < 0) {
366 fprintf(stderr,"binder: ioctl failed (%s)\n", strerror(errno));
367 goto fail;
368 }
369
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000370 res = binder_parse(bs, reply, (uintptr_t) readbuf, bwr.read_consumed, 0);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700371 if (res == 0) return 0;
372 if (res < 0) goto fail;
373 }
374
375fail:
376 memset(reply, 0, sizeof(*reply));
377 reply->flags |= BIO_F_IOERROR;
378 return -1;
379}
380
381void binder_loop(struct binder_state *bs, binder_handler func)
382{
383 int res;
384 struct binder_write_read bwr;
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000385 uint32_t readbuf[32];
Mike Lockwood94afecf2012-10-24 10:45:23 -0700386
387 bwr.write_size = 0;
388 bwr.write_consumed = 0;
389 bwr.write_buffer = 0;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000390
Mike Lockwood94afecf2012-10-24 10:45:23 -0700391 readbuf[0] = BC_ENTER_LOOPER;
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000392 binder_write(bs, readbuf, sizeof(uint32_t));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700393
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 ALOGE("binder_loop: ioctl failed (%s)\n", strerror(errno));
403 break;
404 }
405
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000406 res = binder_parse(bs, 0, (uintptr_t) readbuf, bwr.read_consumed, func);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700407 if (res == 0) {
408 ALOGE("binder_loop: unexpected reply?!\n");
409 break;
410 }
411 if (res < 0) {
412 ALOGE("binder_loop: io error %d %s\n", res, strerror(errno));
413 break;
414 }
415 }
416}
417
Serban Constantinescubcf38882014-01-10 13:56:27 +0000418void bio_init_from_txn(struct binder_io *bio, struct binder_transaction_data *txn)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700419{
Arve Hjønnevåg399b6c32014-01-28 21:25:12 -0800420 bio->data = bio->data0 = (char *)(intptr_t)txn->data.ptr.buffer;
421 bio->offs = bio->offs0 = (binder_size_t *)(intptr_t)txn->data.ptr.offsets;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700422 bio->data_avail = txn->data_size;
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000423 bio->offs_avail = txn->offsets_size / sizeof(size_t);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700424 bio->flags = BIO_F_SHARED;
425}
426
427void bio_init(struct binder_io *bio, void *data,
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000428 size_t maxdata, size_t maxoffs)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700429{
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000430 size_t n = maxoffs * sizeof(size_t);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700431
432 if (n > maxdata) {
433 bio->flags = BIO_F_OVERFLOW;
434 bio->data_avail = 0;
435 bio->offs_avail = 0;
436 return;
437 }
438
439 bio->data = bio->data0 = (char *) data + n;
440 bio->offs = bio->offs0 = data;
441 bio->data_avail = maxdata - n;
442 bio->offs_avail = maxoffs;
443 bio->flags = 0;
444}
445
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000446static void *bio_alloc(struct binder_io *bio, size_t size)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700447{
448 size = (size + 3) & (~3);
449 if (size > bio->data_avail) {
450 bio->flags |= BIO_F_OVERFLOW;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000451 return NULL;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700452 } else {
453 void *ptr = bio->data;
454 bio->data += size;
455 bio->data_avail -= size;
456 return ptr;
457 }
458}
459
460void binder_done(struct binder_state *bs,
Ian Pedowitzd57d9b92016-02-19 08:34:43 +0000461 __unused struct binder_io *msg,
Mike Lockwood94afecf2012-10-24 10:45:23 -0700462 struct binder_io *reply)
463{
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000464 struct {
465 uint32_t cmd;
466 uintptr_t buffer;
467 } __attribute__((packed)) data;
468
Mike Lockwood94afecf2012-10-24 10:45:23 -0700469 if (reply->flags & BIO_F_SHARED) {
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000470 data.cmd = BC_FREE_BUFFER;
471 data.buffer = (uintptr_t) reply->data0;
472 binder_write(bs, &data, sizeof(data));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700473 reply->flags = 0;
474 }
475}
476
Serban Constantinescubcf38882014-01-10 13:56:27 +0000477static struct flat_binder_object *bio_alloc_obj(struct binder_io *bio)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700478{
Serban Constantinescubcf38882014-01-10 13:56:27 +0000479 struct flat_binder_object *obj;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700480
481 obj = bio_alloc(bio, sizeof(*obj));
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000482
Mike Lockwood94afecf2012-10-24 10:45:23 -0700483 if (obj && bio->offs_avail) {
484 bio->offs_avail--;
485 *bio->offs++ = ((char*) obj) - ((char*) bio->data0);
486 return obj;
487 }
488
489 bio->flags |= BIO_F_OVERFLOW;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000490 return NULL;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700491}
492
493void bio_put_uint32(struct binder_io *bio, uint32_t n)
494{
495 uint32_t *ptr = bio_alloc(bio, sizeof(n));
496 if (ptr)
497 *ptr = n;
498}
499
500void bio_put_obj(struct binder_io *bio, void *ptr)
501{
Serban Constantinescubcf38882014-01-10 13:56:27 +0000502 struct flat_binder_object *obj;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700503
504 obj = bio_alloc_obj(bio);
505 if (!obj)
506 return;
507
508 obj->flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
509 obj->type = BINDER_TYPE_BINDER;
Arve Hjønnevåg399b6c32014-01-28 21:25:12 -0800510 obj->binder = (uintptr_t)ptr;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700511 obj->cookie = 0;
512}
513
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000514void bio_put_ref(struct binder_io *bio, uint32_t handle)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700515{
Serban Constantinescubcf38882014-01-10 13:56:27 +0000516 struct flat_binder_object *obj;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700517
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000518 if (handle)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700519 obj = bio_alloc_obj(bio);
520 else
521 obj = bio_alloc(bio, sizeof(*obj));
522
523 if (!obj)
524 return;
525
526 obj->flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
527 obj->type = BINDER_TYPE_HANDLE;
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000528 obj->handle = handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700529 obj->cookie = 0;
530}
531
532void bio_put_string16(struct binder_io *bio, const uint16_t *str)
533{
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000534 size_t len;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700535 uint16_t *ptr;
536
537 if (!str) {
538 bio_put_uint32(bio, 0xffffffff);
539 return;
540 }
541
542 len = 0;
543 while (str[len]) len++;
544
545 if (len >= (MAX_BIO_SIZE / sizeof(uint16_t))) {
546 bio_put_uint32(bio, 0xffffffff);
547 return;
548 }
549
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000550 /* Note: The payload will carry 32bit size instead of size_t */
551 bio_put_uint32(bio, (uint32_t) len);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700552 len = (len + 1) * sizeof(uint16_t);
553 ptr = bio_alloc(bio, len);
554 if (ptr)
555 memcpy(ptr, str, len);
556}
557
558void bio_put_string16_x(struct binder_io *bio, const char *_str)
559{
560 unsigned char *str = (unsigned char*) _str;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000561 size_t len;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700562 uint16_t *ptr;
563
564 if (!str) {
565 bio_put_uint32(bio, 0xffffffff);
566 return;
567 }
568
569 len = strlen(_str);
570
571 if (len >= (MAX_BIO_SIZE / sizeof(uint16_t))) {
572 bio_put_uint32(bio, 0xffffffff);
573 return;
574 }
575
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000576 /* Note: The payload will carry 32bit size instead of size_t */
Mike Lockwood94afecf2012-10-24 10:45:23 -0700577 bio_put_uint32(bio, len);
578 ptr = bio_alloc(bio, (len + 1) * sizeof(uint16_t));
579 if (!ptr)
580 return;
581
582 while (*str)
583 *ptr++ = *str++;
584 *ptr++ = 0;
585}
586
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000587static void *bio_get(struct binder_io *bio, size_t size)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700588{
589 size = (size + 3) & (~3);
590
591 if (bio->data_avail < size){
592 bio->data_avail = 0;
593 bio->flags |= BIO_F_OVERFLOW;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000594 return NULL;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700595 } else {
596 void *ptr = bio->data;
597 bio->data += size;
598 bio->data_avail -= size;
599 return ptr;
600 }
601}
602
603uint32_t bio_get_uint32(struct binder_io *bio)
604{
605 uint32_t *ptr = bio_get(bio, sizeof(*ptr));
606 return ptr ? *ptr : 0;
607}
608
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000609uint16_t *bio_get_string16(struct binder_io *bio, size_t *sz)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700610{
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000611 size_t len;
612
613 /* Note: The payload will carry 32bit size instead of size_t */
614 len = (size_t) bio_get_uint32(bio);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700615 if (sz)
616 *sz = len;
617 return bio_get(bio, (len + 1) * sizeof(uint16_t));
618}
619
Serban Constantinescubcf38882014-01-10 13:56:27 +0000620static struct flat_binder_object *_bio_get_obj(struct binder_io *bio)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700621{
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000622 size_t n;
623 size_t off = bio->data - bio->data0;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700624
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000625 /* TODO: be smarter about this? */
Mike Lockwood94afecf2012-10-24 10:45:23 -0700626 for (n = 0; n < bio->offs_avail; n++) {
627 if (bio->offs[n] == off)
Serban Constantinescubcf38882014-01-10 13:56:27 +0000628 return bio_get(bio, sizeof(struct flat_binder_object));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700629 }
630
631 bio->data_avail = 0;
632 bio->flags |= BIO_F_OVERFLOW;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000633 return NULL;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700634}
635
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000636uint32_t bio_get_ref(struct binder_io *bio)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700637{
Serban Constantinescubcf38882014-01-10 13:56:27 +0000638 struct flat_binder_object *obj;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700639
640 obj = _bio_get_obj(bio);
641 if (!obj)
642 return 0;
643
644 if (obj->type == BINDER_TYPE_HANDLE)
Serban Constantinescubcf38882014-01-10 13:56:27 +0000645 return obj->handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700646
647 return 0;
648}