blob: db7632d36aeda5a57819088f590fc7e384648dd4 [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>
7#include <errno.h>
8#include <unistd.h>
9#include <fcntl.h>
10#include <sys/mman.h>
11
12#include "binder.h"
13
14#define MAX_BIO_SIZE (1 << 30)
15
16#define TRACE 0
17
18#define LOG_TAG "Binder"
19#include <cutils/log.h>
20
Serban Constantinescubcf38882014-01-10 13:56:27 +000021void bio_init_from_txn(struct binder_io *io, struct binder_transaction_data *txn);
Mike Lockwood94afecf2012-10-24 10:45:23 -070022
23#if TRACE
Serban Constantinescu9b738bb2014-01-10 15:48:29 +000024void hexdump(void *_data, size_t len)
Mike Lockwood94afecf2012-10-24 10:45:23 -070025{
26 unsigned char *data = _data;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +000027 size_t count;
Mike Lockwood94afecf2012-10-24 10:45:23 -070028
29 for (count = 0; count < len; count++) {
30 if ((count & 15) == 0)
Serban Constantinescu9b738bb2014-01-10 15:48:29 +000031 fprintf(stderr,"%04zu:", count);
Mike Lockwood94afecf2012-10-24 10:45:23 -070032 fprintf(stderr," %02x %c", *data,
33 (*data < 32) || (*data > 126) ? '.' : *data);
34 data++;
35 if ((count & 15) == 15)
36 fprintf(stderr,"\n");
37 }
38 if ((count & 15) != 0)
39 fprintf(stderr,"\n");
40}
41
Serban Constantinescubcf38882014-01-10 13:56:27 +000042void binder_dump_txn(struct binder_transaction_data *txn)
Mike Lockwood94afecf2012-10-24 10:45:23 -070043{
Serban Constantinescubcf38882014-01-10 13:56:27 +000044 struct flat_binder_object *obj;
Arve Hjønnevåg399b6c32014-01-28 21:25:12 -080045 binder_size_t *offs = (binder_size_t *)(uintptr_t)txn->data.ptr.offsets;
46 size_t count = txn->offsets_size / sizeof(binder_size_t);
Mike Lockwood94afecf2012-10-24 10:45:23 -070047
Arve Hjønnevåg399b6c32014-01-28 21:25:12 -080048 fprintf(stderr," target %016"PRIx64" cookie %016"PRIx64" code %08x flags %08x\n",
49 (uint64_t)txn->target.ptr, (uint64_t)txn->cookie, txn->code, txn->flags);
50 fprintf(stderr," pid %8d uid %8d data %"PRIu64" offs %"PRIu64"\n",
51 txn->sender_pid, txn->sender_euid, (uint64_t)txn->data_size, (uint64_t)txn->offsets_size);
52 hexdump((void *)(uintptr_t)txn->data.ptr.buffer, txn->data_size);
Mike Lockwood94afecf2012-10-24 10:45:23 -070053 while (count--) {
Arve Hjønnevåg399b6c32014-01-28 21:25:12 -080054 obj = (struct flat_binder_object *) (((char*)(uintptr_t)txn->data.ptr.buffer) + *offs++);
55 fprintf(stderr," - type %08x flags %08x ptr %016"PRIx64" cookie %016"PRIx64"\n",
56 obj->type, obj->flags, (uint64_t)obj->binder, (uint64_t)obj->cookie);
Mike Lockwood94afecf2012-10-24 10:45:23 -070057 }
58}
59
60#define NAME(n) case n: return #n
61const char *cmd_name(uint32_t cmd)
62{
63 switch(cmd) {
64 NAME(BR_NOOP);
65 NAME(BR_TRANSACTION_COMPLETE);
66 NAME(BR_INCREFS);
67 NAME(BR_ACQUIRE);
68 NAME(BR_RELEASE);
69 NAME(BR_DECREFS);
70 NAME(BR_TRANSACTION);
71 NAME(BR_REPLY);
72 NAME(BR_FAILED_REPLY);
73 NAME(BR_DEAD_REPLY);
74 NAME(BR_DEAD_BINDER);
75 default: return "???";
76 }
77}
78#else
79#define hexdump(a,b) do{} while (0)
80#define binder_dump_txn(txn) do{} while (0)
81#endif
82
83#define BIO_F_SHARED 0x01 /* needs to be buffer freed */
84#define BIO_F_OVERFLOW 0x02 /* ran out of space */
85#define BIO_F_IOERROR 0x04
86#define BIO_F_MALLOCED 0x08 /* needs to be free()'d */
87
88struct binder_state
89{
90 int fd;
91 void *mapped;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +000092 size_t mapsize;
Mike Lockwood94afecf2012-10-24 10:45:23 -070093};
94
Serban Constantinescu9b738bb2014-01-10 15:48:29 +000095struct binder_state *binder_open(size_t mapsize)
Mike Lockwood94afecf2012-10-24 10:45:23 -070096{
97 struct binder_state *bs;
Serban Constantinescua44542c2014-01-30 15:16:45 +000098 struct binder_version vers;
Mike Lockwood94afecf2012-10-24 10:45:23 -070099
100 bs = malloc(sizeof(*bs));
101 if (!bs) {
102 errno = ENOMEM;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000103 return NULL;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700104 }
105
106 bs->fd = open("/dev/binder", O_RDWR);
107 if (bs->fd < 0) {
108 fprintf(stderr,"binder: cannot open device (%s)\n",
109 strerror(errno));
110 goto fail_open;
111 }
112
Serban Constantinescua44542c2014-01-30 15:16:45 +0000113 if ((ioctl(bs->fd, BINDER_VERSION, &vers) == -1) ||
114 (vers.protocol_version != BINDER_CURRENT_PROTOCOL_VERSION)) {
115 fprintf(stderr, "binder: driver version differs from user space\n");
116 goto fail_open;
117 }
118
Mike Lockwood94afecf2012-10-24 10:45:23 -0700119 bs->mapsize = mapsize;
120 bs->mapped = mmap(NULL, mapsize, PROT_READ, MAP_PRIVATE, bs->fd, 0);
121 if (bs->mapped == MAP_FAILED) {
122 fprintf(stderr,"binder: cannot map device (%s)\n",
123 strerror(errno));
124 goto fail_map;
125 }
126
Mike Lockwood94afecf2012-10-24 10:45:23 -0700127 return bs;
128
129fail_map:
130 close(bs->fd);
131fail_open:
132 free(bs);
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000133 return NULL;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700134}
135
136void binder_close(struct binder_state *bs)
137{
138 munmap(bs->mapped, bs->mapsize);
139 close(bs->fd);
140 free(bs);
141}
142
143int binder_become_context_manager(struct binder_state *bs)
144{
145 return ioctl(bs->fd, BINDER_SET_CONTEXT_MGR, 0);
146}
147
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000148int binder_write(struct binder_state *bs, void *data, size_t len)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700149{
150 struct binder_write_read bwr;
151 int res;
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000152
Mike Lockwood94afecf2012-10-24 10:45:23 -0700153 bwr.write_size = len;
154 bwr.write_consumed = 0;
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000155 bwr.write_buffer = (uintptr_t) data;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700156 bwr.read_size = 0;
157 bwr.read_consumed = 0;
158 bwr.read_buffer = 0;
159 res = ioctl(bs->fd, BINDER_WRITE_READ, &bwr);
160 if (res < 0) {
161 fprintf(stderr,"binder_write: ioctl failed (%s)\n",
162 strerror(errno));
163 }
164 return res;
165}
166
167void binder_send_reply(struct binder_state *bs,
168 struct binder_io *reply,
Arve Hjønnevåg399b6c32014-01-28 21:25:12 -0800169 binder_uintptr_t buffer_to_free,
Mike Lockwood94afecf2012-10-24 10:45:23 -0700170 int status)
171{
172 struct {
173 uint32_t cmd_free;
Arve Hjønnevåg399b6c32014-01-28 21:25:12 -0800174 binder_uintptr_t buffer;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700175 uint32_t cmd_reply;
Serban Constantinescubcf38882014-01-10 13:56:27 +0000176 struct binder_transaction_data txn;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700177 } __attribute__((packed)) data;
178
179 data.cmd_free = BC_FREE_BUFFER;
Arve Hjønnevåg399b6c32014-01-28 21:25:12 -0800180 data.buffer = buffer_to_free;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700181 data.cmd_reply = BC_REPLY;
Serban Constantinescubcf38882014-01-10 13:56:27 +0000182 data.txn.target.ptr = 0;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700183 data.txn.cookie = 0;
184 data.txn.code = 0;
185 if (status) {
186 data.txn.flags = TF_STATUS_CODE;
187 data.txn.data_size = sizeof(int);
Serban Constantinescubcf38882014-01-10 13:56:27 +0000188 data.txn.offsets_size = 0;
Arve Hjønnevåg399b6c32014-01-28 21:25:12 -0800189 data.txn.data.ptr.buffer = (uintptr_t)&status;
Serban Constantinescubcf38882014-01-10 13:56:27 +0000190 data.txn.data.ptr.offsets = 0;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700191 } else {
192 data.txn.flags = 0;
193 data.txn.data_size = reply->data - reply->data0;
Serban Constantinescubcf38882014-01-10 13:56:27 +0000194 data.txn.offsets_size = ((char*) reply->offs) - ((char*) reply->offs0);
Arve Hjønnevåg399b6c32014-01-28 21:25:12 -0800195 data.txn.data.ptr.buffer = (uintptr_t)reply->data0;
196 data.txn.data.ptr.offsets = (uintptr_t)reply->offs0;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700197 }
198 binder_write(bs, &data, sizeof(data));
199}
200
201int binder_parse(struct binder_state *bs, struct binder_io *bio,
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000202 uintptr_t ptr, size_t size, binder_handler func)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700203{
204 int r = 1;
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000205 uintptr_t end = ptr + (uintptr_t) size;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700206
207 while (ptr < end) {
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000208 uint32_t cmd = *(uint32_t *) ptr;
209 ptr += sizeof(uint32_t);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700210#if TRACE
211 fprintf(stderr,"%s:\n", cmd_name(cmd));
212#endif
213 switch(cmd) {
214 case BR_NOOP:
215 break;
216 case BR_TRANSACTION_COMPLETE:
217 break;
218 case BR_INCREFS:
219 case BR_ACQUIRE:
220 case BR_RELEASE:
221 case BR_DECREFS:
222#if TRACE
Arve Hjønnevåg399b6c32014-01-28 21:25:12 -0800223 fprintf(stderr," %p, %p\n", (void *)ptr, (void *)(ptr + sizeof(void *)));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700224#endif
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000225 ptr += sizeof(struct binder_ptr_cookie);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700226 break;
227 case BR_TRANSACTION: {
Serban Constantinescubcf38882014-01-10 13:56:27 +0000228 struct binder_transaction_data *txn = (struct binder_transaction_data *) ptr;
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000229 if ((end - ptr) < sizeof(*txn)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700230 ALOGE("parse: txn too small!\n");
231 return -1;
232 }
233 binder_dump_txn(txn);
234 if (func) {
235 unsigned rdata[256/4];
236 struct binder_io msg;
237 struct binder_io reply;
238 int res;
239
240 bio_init(&reply, rdata, sizeof(rdata), 4);
241 bio_init_from_txn(&msg, txn);
242 res = func(bs, txn, &msg, &reply);
Serban Constantinescubcf38882014-01-10 13:56:27 +0000243 binder_send_reply(bs, &reply, txn->data.ptr.buffer, res);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700244 }
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000245 ptr += sizeof(*txn);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700246 break;
247 }
248 case BR_REPLY: {
Serban Constantinescubcf38882014-01-10 13:56:27 +0000249 struct binder_transaction_data *txn = (struct binder_transaction_data *) ptr;
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000250 if ((end - ptr) < sizeof(*txn)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700251 ALOGE("parse: reply too small!\n");
252 return -1;
253 }
254 binder_dump_txn(txn);
255 if (bio) {
256 bio_init_from_txn(bio, txn);
257 bio = 0;
258 } else {
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000259 /* todo FREE BUFFER */
Mike Lockwood94afecf2012-10-24 10:45:23 -0700260 }
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000261 ptr += sizeof(*txn);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700262 r = 0;
263 break;
264 }
265 case BR_DEAD_BINDER: {
Arve Hjønnevåg399b6c32014-01-28 21:25:12 -0800266 struct binder_death *death = (struct binder_death *)(uintptr_t) *(binder_uintptr_t *)ptr;
267 ptr += sizeof(binder_uintptr_t);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700268 death->func(bs, death->ptr);
269 break;
270 }
271 case BR_FAILED_REPLY:
272 r = -1;
273 break;
274 case BR_DEAD_REPLY:
275 r = -1;
276 break;
277 default:
278 ALOGE("parse: OOPS %d\n", cmd);
279 return -1;
280 }
281 }
282
283 return r;
284}
285
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000286void binder_acquire(struct binder_state *bs, uint32_t target)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700287{
288 uint32_t cmd[2];
289 cmd[0] = BC_ACQUIRE;
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000290 cmd[1] = target;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700291 binder_write(bs, cmd, sizeof(cmd));
292}
293
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000294void binder_release(struct binder_state *bs, uint32_t target)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700295{
296 uint32_t cmd[2];
297 cmd[0] = BC_RELEASE;
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000298 cmd[1] = target;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700299 binder_write(bs, cmd, sizeof(cmd));
300}
301
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000302void binder_link_to_death(struct binder_state *bs, uint32_t target, struct binder_death *death)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700303{
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000304 struct {
305 uint32_t cmd;
306 struct binder_handle_cookie payload;
307 } __attribute__((packed)) data;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700308
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000309 data.cmd = BC_REQUEST_DEATH_NOTIFICATION;
310 data.payload.handle = target;
311 data.payload.cookie = (uintptr_t) death;
312 binder_write(bs, &data, sizeof(data));
313}
Mike Lockwood94afecf2012-10-24 10:45:23 -0700314
315int binder_call(struct binder_state *bs,
316 struct binder_io *msg, struct binder_io *reply,
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000317 uint32_t target, uint32_t code)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700318{
319 int res;
320 struct binder_write_read bwr;
321 struct {
322 uint32_t cmd;
Serban Constantinescubcf38882014-01-10 13:56:27 +0000323 struct binder_transaction_data txn;
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000324 } __attribute__((packed)) writebuf;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700325 unsigned readbuf[32];
326
327 if (msg->flags & BIO_F_OVERFLOW) {
328 fprintf(stderr,"binder: txn buffer overflow\n");
329 goto fail;
330 }
331
332 writebuf.cmd = BC_TRANSACTION;
Serban Constantinescubcf38882014-01-10 13:56:27 +0000333 writebuf.txn.target.handle = target;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700334 writebuf.txn.code = code;
335 writebuf.txn.flags = 0;
336 writebuf.txn.data_size = msg->data - msg->data0;
Serban Constantinescubcf38882014-01-10 13:56:27 +0000337 writebuf.txn.offsets_size = ((char*) msg->offs) - ((char*) msg->offs0);
Arve Hjønnevåg399b6c32014-01-28 21:25:12 -0800338 writebuf.txn.data.ptr.buffer = (uintptr_t)msg->data0;
339 writebuf.txn.data.ptr.offsets = (uintptr_t)msg->offs0;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700340
341 bwr.write_size = sizeof(writebuf);
342 bwr.write_consumed = 0;
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000343 bwr.write_buffer = (uintptr_t) &writebuf;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000344
Mike Lockwood94afecf2012-10-24 10:45:23 -0700345 hexdump(msg->data0, msg->data - msg->data0);
346 for (;;) {
347 bwr.read_size = sizeof(readbuf);
348 bwr.read_consumed = 0;
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000349 bwr.read_buffer = (uintptr_t) readbuf;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700350
351 res = ioctl(bs->fd, BINDER_WRITE_READ, &bwr);
352
353 if (res < 0) {
354 fprintf(stderr,"binder: ioctl failed (%s)\n", strerror(errno));
355 goto fail;
356 }
357
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000358 res = binder_parse(bs, reply, (uintptr_t) readbuf, bwr.read_consumed, 0);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700359 if (res == 0) return 0;
360 if (res < 0) goto fail;
361 }
362
363fail:
364 memset(reply, 0, sizeof(*reply));
365 reply->flags |= BIO_F_IOERROR;
366 return -1;
367}
368
369void binder_loop(struct binder_state *bs, binder_handler func)
370{
371 int res;
372 struct binder_write_read bwr;
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000373 uint32_t readbuf[32];
Mike Lockwood94afecf2012-10-24 10:45:23 -0700374
375 bwr.write_size = 0;
376 bwr.write_consumed = 0;
377 bwr.write_buffer = 0;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000378
Mike Lockwood94afecf2012-10-24 10:45:23 -0700379 readbuf[0] = BC_ENTER_LOOPER;
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000380 binder_write(bs, readbuf, sizeof(uint32_t));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700381
382 for (;;) {
383 bwr.read_size = sizeof(readbuf);
384 bwr.read_consumed = 0;
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000385 bwr.read_buffer = (uintptr_t) readbuf;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700386
387 res = ioctl(bs->fd, BINDER_WRITE_READ, &bwr);
388
389 if (res < 0) {
390 ALOGE("binder_loop: ioctl failed (%s)\n", strerror(errno));
391 break;
392 }
393
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000394 res = binder_parse(bs, 0, (uintptr_t) readbuf, bwr.read_consumed, func);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700395 if (res == 0) {
396 ALOGE("binder_loop: unexpected reply?!\n");
397 break;
398 }
399 if (res < 0) {
400 ALOGE("binder_loop: io error %d %s\n", res, strerror(errno));
401 break;
402 }
403 }
404}
405
Serban Constantinescubcf38882014-01-10 13:56:27 +0000406void bio_init_from_txn(struct binder_io *bio, struct binder_transaction_data *txn)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700407{
Arve Hjønnevåg399b6c32014-01-28 21:25:12 -0800408 bio->data = bio->data0 = (char *)(intptr_t)txn->data.ptr.buffer;
409 bio->offs = bio->offs0 = (binder_size_t *)(intptr_t)txn->data.ptr.offsets;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700410 bio->data_avail = txn->data_size;
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000411 bio->offs_avail = txn->offsets_size / sizeof(size_t);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700412 bio->flags = BIO_F_SHARED;
413}
414
415void bio_init(struct binder_io *bio, void *data,
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000416 size_t maxdata, size_t maxoffs)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700417{
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000418 size_t n = maxoffs * sizeof(size_t);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700419
420 if (n > maxdata) {
421 bio->flags = BIO_F_OVERFLOW;
422 bio->data_avail = 0;
423 bio->offs_avail = 0;
424 return;
425 }
426
427 bio->data = bio->data0 = (char *) data + n;
428 bio->offs = bio->offs0 = data;
429 bio->data_avail = maxdata - n;
430 bio->offs_avail = maxoffs;
431 bio->flags = 0;
432}
433
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000434static void *bio_alloc(struct binder_io *bio, size_t size)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700435{
436 size = (size + 3) & (~3);
437 if (size > bio->data_avail) {
438 bio->flags |= BIO_F_OVERFLOW;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000439 return NULL;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700440 } else {
441 void *ptr = bio->data;
442 bio->data += size;
443 bio->data_avail -= size;
444 return ptr;
445 }
446}
447
448void binder_done(struct binder_state *bs,
449 struct binder_io *msg,
450 struct binder_io *reply)
451{
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000452 struct {
453 uint32_t cmd;
454 uintptr_t buffer;
455 } __attribute__((packed)) data;
456
Mike Lockwood94afecf2012-10-24 10:45:23 -0700457 if (reply->flags & BIO_F_SHARED) {
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000458 data.cmd = BC_FREE_BUFFER;
459 data.buffer = (uintptr_t) reply->data0;
460 binder_write(bs, &data, sizeof(data));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700461 reply->flags = 0;
462 }
463}
464
Serban Constantinescubcf38882014-01-10 13:56:27 +0000465static struct flat_binder_object *bio_alloc_obj(struct binder_io *bio)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700466{
Serban Constantinescubcf38882014-01-10 13:56:27 +0000467 struct flat_binder_object *obj;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700468
469 obj = bio_alloc(bio, sizeof(*obj));
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000470
Mike Lockwood94afecf2012-10-24 10:45:23 -0700471 if (obj && bio->offs_avail) {
472 bio->offs_avail--;
473 *bio->offs++ = ((char*) obj) - ((char*) bio->data0);
474 return obj;
475 }
476
477 bio->flags |= BIO_F_OVERFLOW;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000478 return NULL;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700479}
480
481void bio_put_uint32(struct binder_io *bio, uint32_t n)
482{
483 uint32_t *ptr = bio_alloc(bio, sizeof(n));
484 if (ptr)
485 *ptr = n;
486}
487
488void bio_put_obj(struct binder_io *bio, void *ptr)
489{
Serban Constantinescubcf38882014-01-10 13:56:27 +0000490 struct flat_binder_object *obj;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700491
492 obj = bio_alloc_obj(bio);
493 if (!obj)
494 return;
495
496 obj->flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
497 obj->type = BINDER_TYPE_BINDER;
Arve Hjønnevåg399b6c32014-01-28 21:25:12 -0800498 obj->binder = (uintptr_t)ptr;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700499 obj->cookie = 0;
500}
501
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000502void bio_put_ref(struct binder_io *bio, uint32_t handle)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700503{
Serban Constantinescubcf38882014-01-10 13:56:27 +0000504 struct flat_binder_object *obj;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700505
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000506 if (handle)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700507 obj = bio_alloc_obj(bio);
508 else
509 obj = bio_alloc(bio, sizeof(*obj));
510
511 if (!obj)
512 return;
513
514 obj->flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
515 obj->type = BINDER_TYPE_HANDLE;
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000516 obj->handle = handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700517 obj->cookie = 0;
518}
519
520void bio_put_string16(struct binder_io *bio, const uint16_t *str)
521{
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000522 size_t len;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700523 uint16_t *ptr;
524
525 if (!str) {
526 bio_put_uint32(bio, 0xffffffff);
527 return;
528 }
529
530 len = 0;
531 while (str[len]) len++;
532
533 if (len >= (MAX_BIO_SIZE / sizeof(uint16_t))) {
534 bio_put_uint32(bio, 0xffffffff);
535 return;
536 }
537
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000538 /* Note: The payload will carry 32bit size instead of size_t */
539 bio_put_uint32(bio, (uint32_t) len);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700540 len = (len + 1) * sizeof(uint16_t);
541 ptr = bio_alloc(bio, len);
542 if (ptr)
543 memcpy(ptr, str, len);
544}
545
546void bio_put_string16_x(struct binder_io *bio, const char *_str)
547{
548 unsigned char *str = (unsigned char*) _str;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000549 size_t len;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700550 uint16_t *ptr;
551
552 if (!str) {
553 bio_put_uint32(bio, 0xffffffff);
554 return;
555 }
556
557 len = strlen(_str);
558
559 if (len >= (MAX_BIO_SIZE / sizeof(uint16_t))) {
560 bio_put_uint32(bio, 0xffffffff);
561 return;
562 }
563
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000564 /* Note: The payload will carry 32bit size instead of size_t */
Mike Lockwood94afecf2012-10-24 10:45:23 -0700565 bio_put_uint32(bio, len);
566 ptr = bio_alloc(bio, (len + 1) * sizeof(uint16_t));
567 if (!ptr)
568 return;
569
570 while (*str)
571 *ptr++ = *str++;
572 *ptr++ = 0;
573}
574
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000575static void *bio_get(struct binder_io *bio, size_t size)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700576{
577 size = (size + 3) & (~3);
578
579 if (bio->data_avail < size){
580 bio->data_avail = 0;
581 bio->flags |= BIO_F_OVERFLOW;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000582 return NULL;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700583 } else {
584 void *ptr = bio->data;
585 bio->data += size;
586 bio->data_avail -= size;
587 return ptr;
588 }
589}
590
591uint32_t bio_get_uint32(struct binder_io *bio)
592{
593 uint32_t *ptr = bio_get(bio, sizeof(*ptr));
594 return ptr ? *ptr : 0;
595}
596
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000597uint16_t *bio_get_string16(struct binder_io *bio, size_t *sz)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700598{
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000599 size_t len;
600
601 /* Note: The payload will carry 32bit size instead of size_t */
602 len = (size_t) bio_get_uint32(bio);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700603 if (sz)
604 *sz = len;
605 return bio_get(bio, (len + 1) * sizeof(uint16_t));
606}
607
Serban Constantinescubcf38882014-01-10 13:56:27 +0000608static struct flat_binder_object *_bio_get_obj(struct binder_io *bio)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700609{
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000610 size_t n;
611 size_t off = bio->data - bio->data0;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700612
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000613 /* TODO: be smarter about this? */
Mike Lockwood94afecf2012-10-24 10:45:23 -0700614 for (n = 0; n < bio->offs_avail; n++) {
615 if (bio->offs[n] == off)
Serban Constantinescubcf38882014-01-10 13:56:27 +0000616 return bio_get(bio, sizeof(struct flat_binder_object));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700617 }
618
619 bio->data_avail = 0;
620 bio->flags |= BIO_F_OVERFLOW;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000621 return NULL;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700622}
623
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000624uint32_t bio_get_ref(struct binder_io *bio)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700625{
Serban Constantinescubcf38882014-01-10 13:56:27 +0000626 struct flat_binder_object *obj;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700627
628 obj = _bio_get_obj(bio);
629 if (!obj)
630 return 0;
631
632 if (obj->type == BINDER_TYPE_HANDLE)
Serban Constantinescubcf38882014-01-10 13:56:27 +0000633 return obj->handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700634
635 return 0;
636}