blob: cb557aa62e3d688a123c57dd1558deeb833b36f3 [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
15#include <android/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
Serban Constantinescu9b738bb2014-01-10 15:48:29 +000097struct binder_state *binder_open(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
Nick Kralevich0fe7ce32015-12-23 18:58:05 -0800108 bs->fd = open("/dev/binder", O_RDWR | O_CLOEXEC);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700109 if (bs->fd < 0) {
110 fprintf(stderr,"binder: cannot open device (%s)\n",
111 strerror(errno));
112 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{
149 return ioctl(bs->fd, BINDER_SET_CONTEXT_MGR, 0);
150}
151
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000152int binder_write(struct binder_state *bs, void *data, size_t len)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700153{
154 struct binder_write_read bwr;
155 int res;
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000156
Mike Lockwood94afecf2012-10-24 10:45:23 -0700157 bwr.write_size = len;
158 bwr.write_consumed = 0;
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000159 bwr.write_buffer = (uintptr_t) data;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700160 bwr.read_size = 0;
161 bwr.read_consumed = 0;
162 bwr.read_buffer = 0;
163 res = ioctl(bs->fd, BINDER_WRITE_READ, &bwr);
164 if (res < 0) {
165 fprintf(stderr,"binder_write: ioctl failed (%s)\n",
166 strerror(errno));
167 }
168 return res;
169}
170
dcashman51f592c2016-03-11 10:38:10 -0800171void binder_free_buffer(struct binder_state *bs,
172 binder_uintptr_t buffer_to_free)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700173{
174 struct {
175 uint32_t cmd_free;
Arve Hjønnevåg399b6c32014-01-28 21:25:12 -0800176 binder_uintptr_t buffer;
dcashman51f592c2016-03-11 10:38:10 -0800177 } __attribute__((packed)) data;
178 data.cmd_free = BC_FREE_BUFFER;
179 data.buffer = buffer_to_free;
180 binder_write(bs, &data, sizeof(data));
181}
182
183void binder_send_reply(struct binder_state *bs,
184 struct binder_io *reply,
Mark Salyzyn52aa4a72016-05-25 13:14:10 -0700185 binder_uintptr_t buffer_to_free,
dcashman51f592c2016-03-11 10:38:10 -0800186 int status)
187{
188 struct {
Mark Salyzyn52aa4a72016-05-25 13:14:10 -0700189 uint32_t cmd_free;
190 binder_uintptr_t buffer;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700191 uint32_t cmd_reply;
Serban Constantinescubcf38882014-01-10 13:56:27 +0000192 struct binder_transaction_data txn;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700193 } __attribute__((packed)) data;
194
Mark Salyzyn52aa4a72016-05-25 13:14:10 -0700195 data.cmd_free = BC_FREE_BUFFER;
196 data.buffer = buffer_to_free;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700197 data.cmd_reply = BC_REPLY;
Serban Constantinescubcf38882014-01-10 13:56:27 +0000198 data.txn.target.ptr = 0;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700199 data.txn.cookie = 0;
200 data.txn.code = 0;
201 if (status) {
202 data.txn.flags = TF_STATUS_CODE;
203 data.txn.data_size = sizeof(int);
Serban Constantinescubcf38882014-01-10 13:56:27 +0000204 data.txn.offsets_size = 0;
Arve Hjønnevåg399b6c32014-01-28 21:25:12 -0800205 data.txn.data.ptr.buffer = (uintptr_t)&status;
Serban Constantinescubcf38882014-01-10 13:56:27 +0000206 data.txn.data.ptr.offsets = 0;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700207 } else {
208 data.txn.flags = 0;
209 data.txn.data_size = reply->data - reply->data0;
Serban Constantinescubcf38882014-01-10 13:56:27 +0000210 data.txn.offsets_size = ((char*) reply->offs) - ((char*) reply->offs0);
Arve Hjønnevåg399b6c32014-01-28 21:25:12 -0800211 data.txn.data.ptr.buffer = (uintptr_t)reply->data0;
212 data.txn.data.ptr.offsets = (uintptr_t)reply->offs0;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700213 }
214 binder_write(bs, &data, sizeof(data));
215}
216
217int binder_parse(struct binder_state *bs, struct binder_io *bio,
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000218 uintptr_t ptr, size_t size, binder_handler func)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700219{
220 int r = 1;
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000221 uintptr_t end = ptr + (uintptr_t) size;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700222
223 while (ptr < end) {
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000224 uint32_t cmd = *(uint32_t *) ptr;
225 ptr += sizeof(uint32_t);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700226#if TRACE
227 fprintf(stderr,"%s:\n", cmd_name(cmd));
228#endif
229 switch(cmd) {
230 case BR_NOOP:
231 break;
232 case BR_TRANSACTION_COMPLETE:
233 break;
234 case BR_INCREFS:
235 case BR_ACQUIRE:
236 case BR_RELEASE:
237 case BR_DECREFS:
238#if TRACE
Arve Hjønnevåg399b6c32014-01-28 21:25:12 -0800239 fprintf(stderr," %p, %p\n", (void *)ptr, (void *)(ptr + sizeof(void *)));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700240#endif
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000241 ptr += sizeof(struct binder_ptr_cookie);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700242 break;
243 case BR_TRANSACTION: {
Serban Constantinescubcf38882014-01-10 13:56:27 +0000244 struct binder_transaction_data *txn = (struct binder_transaction_data *) ptr;
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000245 if ((end - ptr) < sizeof(*txn)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700246 ALOGE("parse: txn too small!\n");
247 return -1;
248 }
249 binder_dump_txn(txn);
250 if (func) {
251 unsigned rdata[256/4];
252 struct binder_io msg;
253 struct binder_io reply;
254 int res;
255
256 bio_init(&reply, rdata, sizeof(rdata), 4);
257 bio_init_from_txn(&msg, txn);
258 res = func(bs, txn, &msg, &reply);
Mark Salyzyn52aa4a72016-05-25 13:14:10 -0700259 if (txn->flags & TF_ONE_WAY) {
260 binder_free_buffer(bs, txn->data.ptr.buffer);
261 } else {
262 binder_send_reply(bs, &reply, txn->data.ptr.buffer, res);
263 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700264 }
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000265 ptr += sizeof(*txn);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700266 break;
267 }
268 case BR_REPLY: {
Serban Constantinescubcf38882014-01-10 13:56:27 +0000269 struct binder_transaction_data *txn = (struct binder_transaction_data *) ptr;
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000270 if ((end - ptr) < sizeof(*txn)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700271 ALOGE("parse: reply too small!\n");
272 return -1;
273 }
274 binder_dump_txn(txn);
275 if (bio) {
276 bio_init_from_txn(bio, txn);
277 bio = 0;
278 } else {
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000279 /* todo FREE BUFFER */
Mike Lockwood94afecf2012-10-24 10:45:23 -0700280 }
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000281 ptr += sizeof(*txn);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700282 r = 0;
283 break;
284 }
285 case BR_DEAD_BINDER: {
Arve Hjønnevåg399b6c32014-01-28 21:25:12 -0800286 struct binder_death *death = (struct binder_death *)(uintptr_t) *(binder_uintptr_t *)ptr;
287 ptr += sizeof(binder_uintptr_t);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700288 death->func(bs, death->ptr);
289 break;
290 }
291 case BR_FAILED_REPLY:
292 r = -1;
293 break;
294 case BR_DEAD_REPLY:
295 r = -1;
296 break;
297 default:
298 ALOGE("parse: OOPS %d\n", cmd);
299 return -1;
300 }
301 }
302
303 return r;
304}
305
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000306void binder_acquire(struct binder_state *bs, uint32_t target)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700307{
308 uint32_t cmd[2];
309 cmd[0] = BC_ACQUIRE;
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_release(struct binder_state *bs, uint32_t target)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700315{
316 uint32_t cmd[2];
317 cmd[0] = BC_RELEASE;
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000318 cmd[1] = target;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700319 binder_write(bs, cmd, sizeof(cmd));
320}
321
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000322void binder_link_to_death(struct binder_state *bs, uint32_t target, struct binder_death *death)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700323{
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000324 struct {
325 uint32_t cmd;
326 struct binder_handle_cookie payload;
327 } __attribute__((packed)) data;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700328
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000329 data.cmd = BC_REQUEST_DEATH_NOTIFICATION;
330 data.payload.handle = target;
331 data.payload.cookie = (uintptr_t) death;
332 binder_write(bs, &data, sizeof(data));
333}
Mike Lockwood94afecf2012-10-24 10:45:23 -0700334
335int binder_call(struct binder_state *bs,
336 struct binder_io *msg, struct binder_io *reply,
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000337 uint32_t target, uint32_t code)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700338{
339 int res;
340 struct binder_write_read bwr;
341 struct {
342 uint32_t cmd;
Serban Constantinescubcf38882014-01-10 13:56:27 +0000343 struct binder_transaction_data txn;
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000344 } __attribute__((packed)) writebuf;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700345 unsigned readbuf[32];
346
347 if (msg->flags & BIO_F_OVERFLOW) {
348 fprintf(stderr,"binder: txn buffer overflow\n");
349 goto fail;
350 }
351
352 writebuf.cmd = BC_TRANSACTION;
Serban Constantinescubcf38882014-01-10 13:56:27 +0000353 writebuf.txn.target.handle = target;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700354 writebuf.txn.code = code;
355 writebuf.txn.flags = 0;
356 writebuf.txn.data_size = msg->data - msg->data0;
Serban Constantinescubcf38882014-01-10 13:56:27 +0000357 writebuf.txn.offsets_size = ((char*) msg->offs) - ((char*) msg->offs0);
Arve Hjønnevåg399b6c32014-01-28 21:25:12 -0800358 writebuf.txn.data.ptr.buffer = (uintptr_t)msg->data0;
359 writebuf.txn.data.ptr.offsets = (uintptr_t)msg->offs0;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700360
361 bwr.write_size = sizeof(writebuf);
362 bwr.write_consumed = 0;
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000363 bwr.write_buffer = (uintptr_t) &writebuf;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000364
Mike Lockwood94afecf2012-10-24 10:45:23 -0700365 hexdump(msg->data0, msg->data - msg->data0);
366 for (;;) {
367 bwr.read_size = sizeof(readbuf);
368 bwr.read_consumed = 0;
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000369 bwr.read_buffer = (uintptr_t) readbuf;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700370
371 res = ioctl(bs->fd, BINDER_WRITE_READ, &bwr);
372
373 if (res < 0) {
374 fprintf(stderr,"binder: ioctl failed (%s)\n", strerror(errno));
375 goto fail;
376 }
377
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000378 res = binder_parse(bs, reply, (uintptr_t) readbuf, bwr.read_consumed, 0);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700379 if (res == 0) return 0;
380 if (res < 0) goto fail;
381 }
382
383fail:
384 memset(reply, 0, sizeof(*reply));
385 reply->flags |= BIO_F_IOERROR;
386 return -1;
387}
388
389void binder_loop(struct binder_state *bs, binder_handler func)
390{
391 int res;
392 struct binder_write_read bwr;
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000393 uint32_t readbuf[32];
Mike Lockwood94afecf2012-10-24 10:45:23 -0700394
395 bwr.write_size = 0;
396 bwr.write_consumed = 0;
397 bwr.write_buffer = 0;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000398
Mike Lockwood94afecf2012-10-24 10:45:23 -0700399 readbuf[0] = BC_ENTER_LOOPER;
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000400 binder_write(bs, readbuf, sizeof(uint32_t));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700401
402 for (;;) {
403 bwr.read_size = sizeof(readbuf);
404 bwr.read_consumed = 0;
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000405 bwr.read_buffer = (uintptr_t) readbuf;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700406
407 res = ioctl(bs->fd, BINDER_WRITE_READ, &bwr);
408
409 if (res < 0) {
410 ALOGE("binder_loop: ioctl failed (%s)\n", strerror(errno));
411 break;
412 }
413
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000414 res = binder_parse(bs, 0, (uintptr_t) readbuf, bwr.read_consumed, func);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700415 if (res == 0) {
416 ALOGE("binder_loop: unexpected reply?!\n");
417 break;
418 }
419 if (res < 0) {
420 ALOGE("binder_loop: io error %d %s\n", res, strerror(errno));
421 break;
422 }
423 }
424}
425
Serban Constantinescubcf38882014-01-10 13:56:27 +0000426void bio_init_from_txn(struct binder_io *bio, struct binder_transaction_data *txn)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700427{
Arve Hjønnevåg399b6c32014-01-28 21:25:12 -0800428 bio->data = bio->data0 = (char *)(intptr_t)txn->data.ptr.buffer;
429 bio->offs = bio->offs0 = (binder_size_t *)(intptr_t)txn->data.ptr.offsets;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700430 bio->data_avail = txn->data_size;
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000431 bio->offs_avail = txn->offsets_size / sizeof(size_t);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700432 bio->flags = BIO_F_SHARED;
433}
434
435void bio_init(struct binder_io *bio, void *data,
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000436 size_t maxdata, size_t maxoffs)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700437{
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000438 size_t n = maxoffs * sizeof(size_t);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700439
440 if (n > maxdata) {
441 bio->flags = BIO_F_OVERFLOW;
442 bio->data_avail = 0;
443 bio->offs_avail = 0;
444 return;
445 }
446
447 bio->data = bio->data0 = (char *) data + n;
448 bio->offs = bio->offs0 = data;
449 bio->data_avail = maxdata - n;
450 bio->offs_avail = maxoffs;
451 bio->flags = 0;
452}
453
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000454static void *bio_alloc(struct binder_io *bio, size_t size)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700455{
456 size = (size + 3) & (~3);
457 if (size > bio->data_avail) {
458 bio->flags |= BIO_F_OVERFLOW;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000459 return NULL;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700460 } else {
461 void *ptr = bio->data;
462 bio->data += size;
463 bio->data_avail -= size;
464 return ptr;
465 }
466}
467
468void binder_done(struct binder_state *bs,
Ian Pedowitzd57d9b92016-02-19 08:34:43 +0000469 __unused struct binder_io *msg,
Mike Lockwood94afecf2012-10-24 10:45:23 -0700470 struct binder_io *reply)
471{
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000472 struct {
473 uint32_t cmd;
474 uintptr_t buffer;
475 } __attribute__((packed)) data;
476
Mike Lockwood94afecf2012-10-24 10:45:23 -0700477 if (reply->flags & BIO_F_SHARED) {
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000478 data.cmd = BC_FREE_BUFFER;
479 data.buffer = (uintptr_t) reply->data0;
480 binder_write(bs, &data, sizeof(data));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700481 reply->flags = 0;
482 }
483}
484
Serban Constantinescubcf38882014-01-10 13:56:27 +0000485static struct flat_binder_object *bio_alloc_obj(struct binder_io *bio)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700486{
Serban Constantinescubcf38882014-01-10 13:56:27 +0000487 struct flat_binder_object *obj;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700488
489 obj = bio_alloc(bio, sizeof(*obj));
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000490
Mike Lockwood94afecf2012-10-24 10:45:23 -0700491 if (obj && bio->offs_avail) {
492 bio->offs_avail--;
493 *bio->offs++ = ((char*) obj) - ((char*) bio->data0);
494 return obj;
495 }
496
497 bio->flags |= BIO_F_OVERFLOW;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000498 return NULL;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700499}
500
501void bio_put_uint32(struct binder_io *bio, uint32_t n)
502{
503 uint32_t *ptr = bio_alloc(bio, sizeof(n));
504 if (ptr)
505 *ptr = n;
506}
507
508void bio_put_obj(struct binder_io *bio, void *ptr)
509{
Serban Constantinescubcf38882014-01-10 13:56:27 +0000510 struct flat_binder_object *obj;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700511
512 obj = bio_alloc_obj(bio);
513 if (!obj)
514 return;
515
516 obj->flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
517 obj->type = BINDER_TYPE_BINDER;
Arve Hjønnevåg399b6c32014-01-28 21:25:12 -0800518 obj->binder = (uintptr_t)ptr;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700519 obj->cookie = 0;
520}
521
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000522void bio_put_ref(struct binder_io *bio, uint32_t handle)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700523{
Serban Constantinescubcf38882014-01-10 13:56:27 +0000524 struct flat_binder_object *obj;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700525
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000526 if (handle)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700527 obj = bio_alloc_obj(bio);
528 else
529 obj = bio_alloc(bio, sizeof(*obj));
530
531 if (!obj)
532 return;
533
534 obj->flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
535 obj->type = BINDER_TYPE_HANDLE;
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000536 obj->handle = handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700537 obj->cookie = 0;
538}
539
540void bio_put_string16(struct binder_io *bio, const uint16_t *str)
541{
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000542 size_t len;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700543 uint16_t *ptr;
544
545 if (!str) {
546 bio_put_uint32(bio, 0xffffffff);
547 return;
548 }
549
550 len = 0;
551 while (str[len]) len++;
552
553 if (len >= (MAX_BIO_SIZE / sizeof(uint16_t))) {
554 bio_put_uint32(bio, 0xffffffff);
555 return;
556 }
557
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000558 /* Note: The payload will carry 32bit size instead of size_t */
559 bio_put_uint32(bio, (uint32_t) len);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700560 len = (len + 1) * sizeof(uint16_t);
561 ptr = bio_alloc(bio, len);
562 if (ptr)
563 memcpy(ptr, str, len);
564}
565
566void bio_put_string16_x(struct binder_io *bio, const char *_str)
567{
568 unsigned char *str = (unsigned char*) _str;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000569 size_t len;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700570 uint16_t *ptr;
571
572 if (!str) {
573 bio_put_uint32(bio, 0xffffffff);
574 return;
575 }
576
577 len = strlen(_str);
578
579 if (len >= (MAX_BIO_SIZE / sizeof(uint16_t))) {
580 bio_put_uint32(bio, 0xffffffff);
581 return;
582 }
583
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000584 /* Note: The payload will carry 32bit size instead of size_t */
Mike Lockwood94afecf2012-10-24 10:45:23 -0700585 bio_put_uint32(bio, len);
586 ptr = bio_alloc(bio, (len + 1) * sizeof(uint16_t));
587 if (!ptr)
588 return;
589
590 while (*str)
591 *ptr++ = *str++;
592 *ptr++ = 0;
593}
594
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000595static void *bio_get(struct binder_io *bio, size_t size)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700596{
597 size = (size + 3) & (~3);
598
599 if (bio->data_avail < size){
600 bio->data_avail = 0;
601 bio->flags |= BIO_F_OVERFLOW;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000602 return NULL;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700603 } else {
604 void *ptr = bio->data;
605 bio->data += size;
606 bio->data_avail -= size;
607 return ptr;
608 }
609}
610
611uint32_t bio_get_uint32(struct binder_io *bio)
612{
613 uint32_t *ptr = bio_get(bio, sizeof(*ptr));
614 return ptr ? *ptr : 0;
615}
616
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000617uint16_t *bio_get_string16(struct binder_io *bio, size_t *sz)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700618{
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000619 size_t len;
620
621 /* Note: The payload will carry 32bit size instead of size_t */
622 len = (size_t) bio_get_uint32(bio);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700623 if (sz)
624 *sz = len;
625 return bio_get(bio, (len + 1) * sizeof(uint16_t));
626}
627
Serban Constantinescubcf38882014-01-10 13:56:27 +0000628static struct flat_binder_object *_bio_get_obj(struct binder_io *bio)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700629{
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000630 size_t n;
631 size_t off = bio->data - bio->data0;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700632
Serban Constantinescu3a345f02013-12-19 09:11:41 +0000633 /* TODO: be smarter about this? */
Mike Lockwood94afecf2012-10-24 10:45:23 -0700634 for (n = 0; n < bio->offs_avail; n++) {
635 if (bio->offs[n] == off)
Serban Constantinescubcf38882014-01-10 13:56:27 +0000636 return bio_get(bio, sizeof(struct flat_binder_object));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700637 }
638
639 bio->data_avail = 0;
640 bio->flags |= BIO_F_OVERFLOW;
Serban Constantinescu9b738bb2014-01-10 15:48:29 +0000641 return NULL;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700642}
643
Serban Constantinescu5fb1b882014-01-30 14:07:34 +0000644uint32_t bio_get_ref(struct binder_io *bio)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700645{
Serban Constantinescubcf38882014-01-10 13:56:27 +0000646 struct flat_binder_object *obj;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700647
648 obj = _bio_get_obj(bio);
649 if (!obj)
650 return 0;
651
652 if (obj->type == BINDER_TYPE_HANDLE)
Serban Constantinescubcf38882014-01-10 13:56:27 +0000653 return obj->handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700654
655 return 0;
656}